Annotate ClassDataItems

This commit is contained in:
Ben Gruver 2013-02-28 23:02:22 -08:00
parent cc3be5df1e
commit cd12f13ffc
4 changed files with 208 additions and 7 deletions

View File

@ -0,0 +1,162 @@
/*
* Copyright 2013, Google Inc.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are
* met:
*
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above
* copyright notice, this list of conditions and the following disclaimer
* in the documentation and/or other materials provided with the
* distribution.
* * Neither the name of Google Inc. nor the names of its
* contributors may be used to endorse or promote products derived from
* this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
package org.jf.dexlib2.dexbacked.raw;
import com.google.common.base.Joiner;
import org.jf.dexlib2.AccessFlags;
import org.jf.dexlib2.dexbacked.DexReader;
import org.jf.dexlib2.util.AnnotatedBytes;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import java.util.Map;
public class ClassDataItem {
private abstract static class ClassDataItemAnnotator extends SectionAnnotator {
public ClassDataItemAnnotator() {
}
@Nonnull @Override public String getItemName() {
return "class_data_item";
}
@Override protected void annotateItem(@Nonnull AnnotatedBytes out, @Nonnull RawDexFile dexFile, int itemIndex) {
DexReader reader = dexFile.readerAt(out.getCursor());
int mark = reader.getOffset();
int staticFieldsSize = reader.readSmallUleb128();
out.annotate(reader.getOffset() - mark, "static_fields_size = %d", staticFieldsSize);
mark = reader.getOffset();
int instanceFieldsSize = reader.readSmallUleb128();
out.annotate(reader.getOffset() - mark, "instance_fields_size = %d", instanceFieldsSize);
mark = reader.getOffset();
int directMethodsSize = reader.readSmallUleb128();
out.annotate(reader.getOffset() - mark, "direct_methods_size = %d", directMethodsSize);
mark = reader.getOffset();
int virtualMethodsSize = reader.readSmallUleb128();
out.annotate(reader.getOffset() - mark, "virtual_methods_size = %d", virtualMethodsSize);
int previousIndex = 0;
for (int i=0; i<staticFieldsSize; i++) {
out.annotate(0, "static_field[%d]", i);
out.indent();
previousIndex = annotateEncodedField(out, dexFile, reader, previousIndex);
out.deindent();
}
previousIndex = 0;
for (int i=0; i<instanceFieldsSize; i++) {
out.annotate(0, "instance_field[%d]", i);
out.indent();
previousIndex = annotateEncodedField(out, dexFile, reader, previousIndex);
out.deindent();
}
previousIndex = 0;
for (int i=0; i<directMethodsSize; i++) {
out.annotate(0, "direct_method[%d]", i);
out.indent();
previousIndex = annotateEncodedMethod(out, dexFile, reader, previousIndex);
out.deindent();
}
previousIndex = 0;
for (int i=0; i<virtualMethodsSize; i++) {
out.annotate(0, "virtual_method[%d]", i);
out.indent();
previousIndex = annotateEncodedMethod(out, dexFile, reader, previousIndex);
out.deindent();
}
}
private int annotateEncodedField(@Nonnull AnnotatedBytes out, @Nonnull RawDexFile dexFile,
@Nonnull DexReader reader, int previousIndex) {
int mark = reader.getOffset();
int indexDelta = reader.readSmallUleb128();
int fieldIndex = previousIndex + indexDelta;
out.annotate(reader.getOffset() - mark, "field_idx_diff = %d: %s", indexDelta,
FieldIdItem.getReferenceAnnotation(dexFile, fieldIndex));
mark = reader.getOffset();
int accessFlags = reader.readSmallUleb128();
out.annotate(reader.getOffset() - mark, "access_flags = 0x%x: %s", accessFlags,
Joiner.on('|').join(AccessFlags.getAccessFlagsForField(accessFlags)));
return fieldIndex;
}
private int annotateEncodedMethod(@Nonnull AnnotatedBytes out, @Nonnull RawDexFile dexFile,
@Nonnull DexReader reader, int previousIndex) {
int mark = reader.getOffset();
int indexDelta = reader.readSmallUleb128();
int methodIndex = previousIndex + indexDelta;
out.annotate(reader.getOffset() - mark, "method_idx_diff = %d: %s", indexDelta,
MethodIdItem.getReferenceAnnotation(dexFile, methodIndex));
mark = reader.getOffset();
int accessFlags = reader.readSmallUleb128();
out.annotate(reader.getOffset() - mark, "access_flags = 0x%x: %s", accessFlags,
Joiner.on('|').join(AccessFlags.getAccessFlagsForMethod(accessFlags)));
mark = reader.getOffset();
int codeOffset = reader.readSmallUleb128();
out.annotate(reader.getOffset() - mark, "code_item[0x%x]", codeOffset);
return methodIndex;
}
}
@Nonnull
public static SectionAnnotator getAnnotator() {
return new ClassDataItemAnnotator() {
@Override
public void annotateSection(@Nonnull AnnotatedBytes out, @Nonnull RawDexFile dexFile, int itemCount) {
final Map<Integer, String> classTypeMap = ClassDefItem.getClassDataTypeMap(dexFile);
SectionAnnotator annotator = new ClassDataItemAnnotator() {
@Nullable @Override
public String getItemIdentity(@Nonnull RawDexFile dexFile, int itemIndex, int itemOffset) {
if (classTypeMap != null) {
return classTypeMap.get(itemOffset);
}
return null;
}
};
annotator.annotateSection(out, dexFile, itemCount);
}
};
}
}

View File

@ -32,10 +32,13 @@
package org.jf.dexlib2.dexbacked.raw;
import com.google.common.base.Joiner;
import com.google.common.collect.ImmutableMap;
import org.jf.dexlib2.AccessFlags;
import org.jf.dexlib2.util.AnnotatedBytes;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import java.util.Map;
public class ClassDefItem {
public static final int ITEM_SIZE = 32;
@ -102,4 +105,27 @@ public class ClassDefItem {
}
};
}
@Nullable
public static Map<Integer, String> getClassDataTypeMap(@Nonnull RawDexFile dexFile) {
MapItem classDefSection = dexFile.getMapItemForSection(ItemType.CLASS_DEF_ITEM);
if (classDefSection != null) {
int startOffset = classDefSection.getOffset();
ImmutableMap.Builder<Integer, String> builder = ImmutableMap.builder();
for (int i=0; i<classDefSection.getItemCount(); i++) {
int itemOffset = startOffset + i*ITEM_SIZE;
int classTypeIndex = dexFile.readSmallUint(itemOffset + CLASS_OFFSET);
String classType = dexFile.getType(classTypeIndex);
int classDataOffset = dexFile.readSmallUint(itemOffset + CLASS_DATA_OFFSET);
if (classDataOffset != 0) {
builder.put(classDataOffset, classType);
}
}
return builder.build();
}
return null;
}
}

View File

@ -38,6 +38,7 @@ import org.jf.dexlib2.dexbacked.util.FixedSizeList;
import org.jf.dexlib2.util.AnnotatedBytes;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import java.io.IOException;
import java.io.Writer;
import java.util.List;
@ -60,6 +61,16 @@ public class RawDexFile extends DexBackedDexFile.Impl {
return headerItem.getMapOffset();
}
@Nullable
public MapItem getMapItemForSection(int itemType) {
for (MapItem mapItem: getMapItems()) {
if (mapItem.getType() == itemType) {
return mapItem;
}
}
return null;
}
public List<MapItem> getMapItems() {
final int mapOffset = getMapOffset();
final int mapSize = readSmallUint(mapOffset);
@ -79,12 +90,14 @@ public class RawDexFile extends DexBackedDexFile.Impl {
private static final Map<Integer, SectionAnnotator> annotators;
static {
annotators = ImmutableMap.of(
ItemType.TYPE_LIST, TypeListItem.getAnnotator(),
ItemType.ANNOTATION_SET_REF_LIST, AnnotationSetRefList.getAnnotator(),
ItemType.MAP_LIST, MapItem.getAnnotator(),
ItemType.ANNOTATION_SET_ITEM, AnnotationSetItem.getAnnotator(),
ItemType.ANNOTATION_ITEM, AnnotationItem.getAnnotator());
ImmutableMap.Builder<Integer, SectionAnnotator> builder = ImmutableMap.builder();
builder.put(ItemType.TYPE_LIST, TypeListItem.getAnnotator());
builder.put(ItemType.ANNOTATION_SET_REF_LIST, AnnotationSetRefList.getAnnotator());
builder.put(ItemType.MAP_LIST, MapItem.getAnnotator());
builder.put(ItemType.ANNOTATION_SET_ITEM, AnnotationSetItem.getAnnotator());
builder.put(ItemType.ANNOTATION_ITEM, AnnotationItem.getAnnotator());
builder.put(ItemType.CLASS_DATA_ITEM, ClassDataItem.getAnnotator());
annotators = builder.build();
}
public void dumpTo(Writer out, int width) throws IOException {

View File

@ -60,7 +60,7 @@ public abstract class SectionAnnotator {
for (int i=0; i<itemCount; i++) {
out.skipTo(AlignmentUtils.alignOffset(out.getCursor(), itemAlignment));
String itemIdentity = getItemIdentity(dexFile, out.getCursor(), i);
String itemIdentity = getItemIdentity(dexFile, i, out.getCursor());
if (itemIdentity != null) {
out.annotate(0, "[%d] %s: %s", i, itemName, itemIdentity);
} else {