Add helper comments for synthetic member accessors

This adds a comment before a synthetic member accessor is called, which
mentions what member in the parent is being accessed

Kudos to jasta for suggesting this feature!

git-svn-id: https://smali.googlecode.com/svn/trunk@809 55b6fa8a-2a1e-11de-a435-ffa8d773f76a
This commit is contained in:
jesusfreke@jesusfreke.com
2011-05-20 06:16:22 +00:00
parent 7ed253b78b
commit 2f376953b4
7 changed files with 342 additions and 3 deletions

View File

@ -343,6 +343,60 @@ public class ClassDataItem extends Item<ClassDataItem> {
return virtualMethods;
}
/**
* Performs a binary search for the definition of the specified direct method
* @param methodIdItem The MethodIdItem of the direct method to search for
* @return The EncodedMethod for the specified direct method, or null if not found
*/
public EncodedMethod findDirectMethodByMethodId(MethodIdItem methodIdItem) {
return findMethodByMethodIdInternal(methodIdItem.index, directMethods);
}
/**
* Performs a binary search for the definition of the specified virtual method
* @param methodIdItem The MethodIdItem of the virtual method to search for
* @return The EncodedMethod for the specified virtual method, or null if not found
*/
public EncodedMethod findVirtualMethodByMethodId(MethodIdItem methodIdItem) {
return findMethodByMethodIdInternal(methodIdItem.index, virtualMethods);
}
/**
* Performs a binary search for the definition of the specified method. It can be either direct or virtual
* @param methodIdItem The MethodIdItem of the virtual method to search for
* @return The EncodedMethod for the specified virtual method, or null if not found
*/
public EncodedMethod findMethodByMethodId(MethodIdItem methodIdItem) {
EncodedMethod encodedMethod = findMethodByMethodIdInternal(methodIdItem.index, directMethods);
if (encodedMethod != null) {
return encodedMethod;
}
return findMethodByMethodIdInternal(methodIdItem.index, virtualMethods);
}
private static EncodedMethod findMethodByMethodIdInternal(int methodIdItemIndex, EncodedMethod[] encodedMethods) {
int min = 0;
int max = encodedMethods.length;
while (min<max) {
int index = (min+max)>>1;
EncodedMethod encodedMethod = encodedMethods[index];
int encodedMethodIndex = encodedMethod.method.getIndex();
if (encodedMethodIndex == methodIdItemIndex) {
return encodedMethod;
} else if (encodedMethodIndex < methodIdItemIndex) {
min = index;
} else {
max = index;
}
}
return null;
}
public static class EncodedField implements Comparable<EncodedField> {
/**
* The <code>FieldIdItem</code> that this <code>EncodedField</code> is associated with

View File

@ -0,0 +1,56 @@
/*
* [The "BSD licence"]
* Copyright (c) 2011 Ben Gruver
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. 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.
* 3. The name of the author may not be used to endorse or promote products
* derived from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``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 AUTHOR 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.dexlib.Code.Analysis;
import org.jf.dexlib.ClassDefItem;
import org.jf.dexlib.DexFile;
import org.jf.dexlib.TypeIdItem;
import java.util.HashMap;
/**
* Keeps a simple map of classes defined in a dex file, allowing you to look them up by TypeIdItem or name
*/
public class DexFileClassMap {
private final HashMap<String, ClassDefItem> definedClasses = new HashMap<String, ClassDefItem>();
public DexFileClassMap(DexFile dexFile) {
for (ClassDefItem classDefItem: dexFile.ClassDefsSection.getItems()) {
definedClasses.put(classDefItem.getClassType().getTypeDescriptor(), classDefItem);
}
}
public ClassDefItem getClassDefByName(String typeName) {
return definedClasses.get(typeName);
}
public ClassDefItem getClassDefByType(TypeIdItem typeIdItem) {
return definedClasses.get(typeIdItem.getTypeDescriptor());
}
}

View File

@ -0,0 +1,133 @@
/*
* [The "BSD licence"]
* Copyright (c) 2011 Ben Gruver
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. 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.
* 3. The name of the author may not be used to endorse or promote products
* derived from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``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 AUTHOR 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.dexlib.Code.Analysis;
import org.jf.dexlib.*;
import org.jf.dexlib.Code.Format.Instruction22c;
import org.jf.dexlib.Code.Instruction;
import org.jf.dexlib.Code.InstructionWithReference;
import org.jf.dexlib.Util.AccessFlags;
import java.util.HashMap;
public class SyntheticAccessorResolver {
public static final int METHOD = 0;
public static final int GETTER = 1;
public static final int SETTER = 2;
private final DexFileClassMap classMap;
private final HashMap<MethodIdItem, AccessedMember> resolvedAccessors = new HashMap<MethodIdItem, AccessedMember>();
public SyntheticAccessorResolver(DexFile dexFile) {
classMap = new DexFileClassMap(dexFile);
}
public static boolean looksLikeSyntheticAccessor(MethodIdItem methodIdItem) {
return methodIdItem.getMethodName().getStringValue().startsWith("access$");
}
public AccessedMember getAccessedMember(MethodIdItem methodIdItem) {
AccessedMember accessedMember = resolvedAccessors.get(methodIdItem);
if (accessedMember != null) {
return accessedMember;
}
ClassDefItem classDefItem = classMap.getClassDefByType(methodIdItem.getContainingClass());
if (classDefItem == null) {
return null;
}
ClassDataItem.EncodedMethod encodedMethod = classDefItem.getClassData().findDirectMethodByMethodId(methodIdItem);
if (encodedMethod == null) {
return null;
}
//A synthetic accessor will be marked synthetic
if ((encodedMethod.accessFlags & AccessFlags.SYNTHETIC.getValue()) == 0) {
return null;
}
Instruction[] instructions = encodedMethod.codeItem.getInstructions();
//TODO: add support for odexed formats
switch (instructions[0].opcode.format) {
case Format35c:
case Format3rc: {
//a synthetic method access should be either 2 or 3 instructions, depending on if the method returns
//anything or not
if (instructions.length < 2 || instructions.length > 3) {
return null;
}
InstructionWithReference instruction = (InstructionWithReference)instructions[0];
MethodIdItem referencedMethodIdItem = (MethodIdItem)instruction.getReferencedItem();
accessedMember = new AccessedMember(METHOD, referencedMethodIdItem);
resolvedAccessors.put(methodIdItem, accessedMember);
return accessedMember;
}
case Format22c: {
//a synthetic field access should be exactly 2 instructions. The set/put, and then the return
if (instructions.length != 2) {
return null;
}
Instruction22c instruction = (Instruction22c)instructions[0];
FieldIdItem referencedFieldIdItem = (FieldIdItem)instruction.getReferencedItem();
if (instruction.opcode.setsRegister() || instruction.opcode.setsWideRegister()) {
accessedMember = new AccessedMember(GETTER, referencedFieldIdItem);
} else {
accessedMember = new AccessedMember(SETTER, referencedFieldIdItem);
}
resolvedAccessors.put(methodIdItem, accessedMember);
return accessedMember;
}
default:
return null;
}
}
public static class AccessedMember {
private final int accessedMemberType;
private final Item accessedMember;
public AccessedMember(int accessedMemberType, Item accessedMember) {
this.accessedMemberType = accessedMemberType;
this.accessedMember = accessedMember;
}
public int getAccessedMemberType() {
return accessedMemberType;
}
public Item getAccessedMember() {
return accessedMember;
}
}
}