Java Code Examples for org.jf.dexlib2.iface.Method#getImplementation()

The following examples show how to use org.jf.dexlib2.iface.Method#getImplementation() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ControlFlowGraph.java    From CFGScanDroid with GNU General Public License v2.0 6 votes vote down vote up
private static List<BasicBlockInstruction> getFlatMethod(Method method) {
	List<BasicBlockInstruction> flatMethod = new ArrayList<BasicBlockInstruction>();
	MethodImplementation impl = method.getImplementation();
	//List<? extends TryBlock<? extends ExceptionHandler>> tryBlocks = null;
	if(impl != null) {
		int address = 0;
		for(Instruction instruction: impl.getInstructions()) {
			BasicBlockInstruction bbinsn = new BasicBlockInstruction(address, instruction);
			//System.out.print("\t" + address + "\t" + instruction.getOpcode() + "\t" + bbinsn.branch);
			address += instruction.getCodeUnits();
			flatMethod.add(bbinsn);
		}
		//tryBlocks = impl.getTryBlocks();
	}

	return flatMethod;
}
 
Example 2
Source File: ImmutableMethod.java    From ZjDroid with Apache License 2.0 5 votes vote down vote up
public static ImmutableMethod of(Method method) {
    if (method instanceof ImmutableMethod) {
        return (ImmutableMethod)method;
    }
    return new ImmutableMethod(
            method.getDefiningClass(),
            method.getName(),
            method.getParameters(),
            method.getReturnType(),
            method.getAccessFlags(),
            method.getAnnotations(),
            method.getImplementation());
}
 
Example 3
Source File: ImmutableMethod.java    From zjdroid with Apache License 2.0 5 votes vote down vote up
public static ImmutableMethod of(Method method) {
    if (method instanceof ImmutableMethod) {
        return (ImmutableMethod)method;
    }
    return new ImmutableMethod(
            method.getDefiningClass(),
            method.getName(),
            method.getParameters(),
            method.getReturnType(),
            method.getAccessFlags(),
            method.getAnnotations(),
            method.getImplementation());
}
 
Example 4
Source File: ImmutableMethod.java    From HeyGirl with Apache License 2.0 5 votes vote down vote up
public static ImmutableMethod of(Method method) {
    if (method instanceof ImmutableMethod) {
        return (ImmutableMethod)method;
    }
    return new ImmutableMethod(
            method.getDefiningClass(),
            method.getName(),
            method.getParameters(),
            method.getReturnType(),
            method.getAccessFlags(),
            method.getAnnotations(),
            method.getImplementation());
}
 
Example 5
Source File: ImmutableMethod.java    From ZjDroid with Apache License 2.0 5 votes vote down vote up
public static ImmutableMethod of(Method method) {
    if (method instanceof ImmutableMethod) {
        return (ImmutableMethod)method;
    }
    return new ImmutableMethod(
            method.getDefiningClass(),
            method.getName(),
            method.getParameters(),
            method.getReturnType(),
            method.getAccessFlags(),
            method.getAnnotations(),
            method.getImplementation());
}
 
Example 6
Source File: SyntheticAccessorResolver.java    From ZjDroid with Apache License 2.0 4 votes vote down vote up
@Nullable
public AccessedMember getAccessedMember(@Nonnull MethodReference methodReference) {
    String methodDescriptor = ReferenceUtil.getMethodDescriptor(methodReference);

    AccessedMember accessedMember = resolvedAccessors.get(methodDescriptor);
    if (accessedMember != null) {
        return accessedMember;
    }

    String type = methodReference.getDefiningClass();
    ClassDef classDef = classDefMap.get(type);
    if (classDef == null) {
        return null;
    }

    Method matchedMethod = null;
    MethodImplementation matchedMethodImpl = null;
    for (Method method: classDef.getMethods()) {
        MethodImplementation methodImpl = method.getImplementation();
        if (methodImpl != null) {
            if (methodReferenceEquals(method, methodReference)) {
                matchedMethod = method;
                matchedMethodImpl = methodImpl;
                break;
            }
        }
    }

    if (matchedMethod == null) {
        return null;
    }

    //A synthetic accessor will be marked synthetic
    if (!AccessFlags.SYNTHETIC.isSet(matchedMethod.getAccessFlags())) {
        return null;
    }

    List<Instruction> instructions = ImmutableList.copyOf(matchedMethodImpl.getInstructions());

    int accessType = SyntheticAccessorFSM.test(instructions);

    if (accessType >= 0) {
        AccessedMember member =
                new AccessedMember(accessType, ((ReferenceInstruction)instructions.get(0)).getReference());
        resolvedAccessors.put(methodDescriptor, member);
        return member;
    }
    return null;
}
 
Example 7
Source File: MethodReIClassDef.java    From atlas with Apache License 2.0 4 votes vote down vote up
@Override
public Method reMethod(Method method) {
    String newType = null;
    boolean isBasic = false;
    boolean isInit = false;
    boolean changeOpcode = false;
    String methodName = method.getName();
    String returnType = method.getReturnType();
    MethodImplementation methodImplementation = method.getImplementation();
    List<? extends MethodParameter> paramters = method.getParameters();
    if (methodName.equals("<init>") || methodName.equals("<cinit>")) {
        isInit = true;
    }


    if (basicType.containsKey(returnType)) {
        newType = returnType;
        isBasic = true;
    } else {
        newType = classProcessor.classProcess(DefineUtils.getDalvikClassName(returnType)).className;
    }

    String[] argsOringn = new String[paramters.size()];
    String[] args = new String[paramters.size()];
    for (int i = 0; i < paramters.size(); i++) {
        //型参数不混淆
        if (basicType.containsKey(paramters.get(i).getType())) {
            argsOringn[i] = basicType.get(paramters.get(i).getType());
            args[i] = argsOringn[i];
            continue;
        }
        argsOringn[i] = DefineUtils.getDalvikClassName(paramters.get(i).getType());
        args[i] = classProcessor.classProcess(DefineUtils.getDalvikClassName(paramters.get(i).getType())).className;
    }
    String type = method.getReturnType();
    
    return new ImmutableMethod(reType,
            classProcessor.methodProcess(isInit ? methodName :
                    DefineUtils.getDalvikClassName(method.getDefiningClass()), methodName, isBasic ? basicType.get(returnType) : DefineUtils.getDalvikClassName(returnType), StringUtils.join(argsOringn, ",")).methodName,
            reParameters(paramters),
            isBasic ? newType:
                    DefineUtils.getDefineClassName(newType,type.startsWith("[")),
                method.getAccessFlags(),
            getAnnotation(method.getAnnotations()),
            reMethodImpl(methodImplementation));

}
 
Example 8
Source File: PatchMethodTool.java    From atlas with Apache License 2.0 4 votes vote down vote up
public static void modifyMethod(String srcDexFile, String outDexFile, boolean isAndFix) throws IOException {

        DexFile dexFile = DexFileFactory.loadDexFile(srcDexFile, Opcodes.getDefault());

        final Set<ClassDef> classes = Sets.newConcurrentHashSet();

        for (ClassDef classDef : dexFile.getClasses()) {
            Set<Method> methods = Sets.newConcurrentHashSet();
            boolean modifiedMethod = false;
            for (Method method : classDef.getMethods()) {
                    MethodImplementation implementation = method.getImplementation();
                    if (implementation != null&&(methodNeedsModification(classDef, method, isAndFix))) {
                        modifiedMethod = true;
                        methods.add(new ImmutableMethod(
                                method.getDefiningClass(),
                                method.getName(),
                                method.getParameters(),
                                method.getReturnType(),
                                method.getAccessFlags(),
                                method.getAnnotations(),
                                isAndFix ?
                                        modifyMethodAndFix(implementation, method) : modifyMethodTpatch(implementation, method)));
                    } else {
                        methods.add(method);
                    }
                }
            if (!modifiedMethod) {
                classes.add(classDef);
            } else {
                classes.add(new ImmutableClassDef(
                        classDef.getType(),
                        classDef.getAccessFlags(),
                        classDef.getSuperclass(),
                        classDef.getInterfaces(),
                        classDef.getSourceFile(),
                        classDef.getAnnotations(),
                        classDef.getFields(),
                        methods));
            }

        }

        DexFileFactory.writeDexFile(outDexFile, new DexFile() {
            @Nonnull
            @Override
            public Set<? extends ClassDef> getClasses() {
                return new AbstractSet<ClassDef>() {
                    @Nonnull
                    @Override
                    public Iterator<ClassDef> iterator() {
                        return classes.iterator();
                    }

                    @Override
                    public int size() {
                        return classes.size();
                    }
                };
            }

            @Nonnull
            @Override
            public Opcodes getOpcodes() {
                return Opcodes.getDefault();
            }
        });
    }
 
Example 9
Source File: ClassDefinition.java    From atlas with Apache License 2.0 4 votes vote down vote up
private Set<String> writeDirectMethods(IndentingWriter writer) throws IOException {
    boolean wroteHeader = false;
    Set<String> writtenMethods = new HashSet<String>();

    Iterable<? extends Method> directMethods;
    Set<? extends Method> modifieds = null;
    if (classDef instanceof DexBackedClassDef) {
        directMethods = ((DexBackedClassDef) classDef).getDirectMethods(false);
        modifieds = (Set<? extends Method>) DexDiffInfo.modifiedMethods;
    } else {
        directMethods = classDef.getDirectMethods();
    }

    MethodReplaceAnnotation replaceAnnotaion;
    for (Method method : directMethods) {


        if (!fullMethod && !DexDiffInfo.addedClasses.contains(classDef)) {
            if (!modifieds.contains(method) && !DexDiffInfo.addedMethods.contains(method)) {
                continue;
            }
        }

        if (!wroteHeader) {
            writer.write("\n\n");
            writer.write("# direct methods");
            wroteHeader = true;
        }
        writer.write('\n');

        // TODO: check for method validation errors
        String methodString = ReferenceUtil.getMethodDescriptor(method, true);

        IndentingWriter methodWriter = writer;
        if (!writtenMethods.add(methodString)) {
            writer.write("# duplicate method ignored\n");
            methodWriter = new CommentingIndentingWriter(writer);
        }

        MethodImplementation methodImpl = method.getImplementation();
        if (methodImpl == null) {
            MethodDefinition.writeEmptyMethodTo(methodWriter, method, options);
        } else {
            MethodDefinition methodDefinition = new MethodDefinition(this, method, methodImpl);
            methodDefinition.setFullMethod(fullMethod);
            methodDefinition.writeTo(methodWriter);
        }
    }
    return writtenMethods;
}
 
Example 10
Source File: ClassDefinition.java    From atlas with Apache License 2.0 4 votes vote down vote up
private void writeVirtualMethods(IndentingWriter writer, Set<String> directMethods) throws IOException {
    boolean wroteHeader = false;
    Set<String> writtenMethods = new HashSet<String>();

    Iterable<? extends Method> virtualMethods;
    Set<? extends Method> modifieds = null;
    if (classDef instanceof DexBackedClassDef) {
        virtualMethods = ((DexBackedClassDef) classDef).getVirtualMethods(false);
        modifieds = (Set<? extends Method>) DexDiffInfo.modifiedMethods;
    } else {
        virtualMethods = classDef.getVirtualMethods();
    }

    MethodReplaceAnnotation replaceAnnotaion;
    for (Method method : virtualMethods) {

        if (!fullMethod && !DexDiffInfo.addedClasses.contains(classDef)) {
            if (!modifieds.contains(method) && !DexDiffInfo.addedMethods.contains(method)) {
                continue;
            }
        }


        if (!wroteHeader) {
            writer.write("\n\n");
            writer.write("# virtual methods");
            wroteHeader = true;
        }
        writer.write('\n');

        // TODO: check for method validation errors
        String methodString = ReferenceUtil.getMethodDescriptor(method, true);

        IndentingWriter methodWriter = writer;
        if (!writtenMethods.add(methodString)) {
            writer.write("# duplicate method ignored\n");
            methodWriter = new CommentingIndentingWriter(writer);
        } else if (directMethods.contains(methodString)) {
            writer.write("# There is both a direct and virtual method with this signature.\n" +
                    "# You will need to rename one of these methods, including all references.\n");
            System.err.println(String.format("Duplicate direct+virtual method found: %s->%s",
                    classDef.getType(), methodString));
            System.err.println("You will need to rename one of these methods, including all references.");
        }

        MethodImplementation methodImpl = method.getImplementation();
        if (methodImpl == null) {
            MethodDefinition.writeEmptyMethodTo(methodWriter, method, options);
        } else {
            MethodDefinition methodDefinition = new MethodDefinition(this, method, methodImpl);
            methodDefinition.writeTo(methodWriter);
        }
    }
}
 
Example 11
Source File: SyntheticAccessorResolver.java    From zjdroid with Apache License 2.0 4 votes vote down vote up
@Nullable
public AccessedMember getAccessedMember(@Nonnull MethodReference methodReference) {
    String methodDescriptor = ReferenceUtil.getMethodDescriptor(methodReference);

    AccessedMember accessedMember = resolvedAccessors.get(methodDescriptor);
    if (accessedMember != null) {
        return accessedMember;
    }

    String type = methodReference.getDefiningClass();
    ClassDef classDef = classDefMap.get(type);
    if (classDef == null) {
        return null;
    }

    Method matchedMethod = null;
    MethodImplementation matchedMethodImpl = null;
    for (Method method: classDef.getMethods()) {
        MethodImplementation methodImpl = method.getImplementation();
        if (methodImpl != null) {
            if (methodReferenceEquals(method, methodReference)) {
                matchedMethod = method;
                matchedMethodImpl = methodImpl;
                break;
            }
        }
    }

    if (matchedMethod == null) {
        return null;
    }

    //A synthetic accessor will be marked synthetic
    if (!AccessFlags.SYNTHETIC.isSet(matchedMethod.getAccessFlags())) {
        return null;
    }

    List<Instruction> instructions = ImmutableList.copyOf(matchedMethodImpl.getInstructions());

    int accessType = SyntheticAccessorFSM.test(instructions);

    if (accessType >= 0) {
        AccessedMember member =
                new AccessedMember(accessType, ((ReferenceInstruction)instructions.get(0)).getReference());
        resolvedAccessors.put(methodDescriptor, member);
        return member;
    }
    return null;
}
 
Example 12
Source File: DexBody.java    From JAADAS with GNU General Public License v3.0 4 votes vote down vote up
/**
 * @param code the codeitem that is contained in this body
 * @param method the method that is associated with this body
 */
public DexBody(DexFile dexFile, Method method, RefType declaringClassType) {
    MethodImplementation code = method.getImplementation();
    if (code == null)
        throw new RuntimeException("error: no code for method "+ method.getName());
    this.declaringClassType = declaringClassType;
    tries = code.getTryBlocks();
    methodSignature = method.getDefiningClass() +": "+ method.getReturnType() +" "+ method.getName() +"(";
    for (MethodParameter mp: method.getParameters())
        methodSignature += mp.getType() +",";

    List<? extends CharSequence> paramTypes = method.getParameterTypes();
    if (paramTypes != null) {
        parameterTypes = new ArrayList<Type>();
        for (CharSequence type : paramTypes)
            parameterTypes.add(DexType.toSoot(type.toString()));
    } else {
    	parameterTypes = Collections.emptyList();
    }
    
    isStatic = Modifier.isStatic(method.getAccessFlags());
    numRegisters = code.getRegisterCount();
    numParameterRegisters = MethodUtil.getParameterRegisterCount(method);
    if (!isStatic)
        numParameterRegisters--;

    instructions = new ArrayList<DexlibAbstractInstruction>();
    instructionAtAddress = new HashMap<Integer, DexlibAbstractInstruction>();

    registerLocals = new Local[numRegisters];

    int address = 0;
    
    for (Instruction instruction : code.getInstructions()) {
        DexlibAbstractInstruction dexInstruction = fromInstruction(instruction, address);
        instructions.add(dexInstruction);
        instructionAtAddress.put(address, dexInstruction);
        Debug.printDbg(" put instruction '", dexInstruction ,"' at 0x", Integer.toHexString(address));
        address += instruction.getCodeUnits();
    }
    
    // Check taken from Android's dalvik/libdex/DexSwapVerify.cpp
    if (numParameterRegisters > numRegisters)
    	throw new RuntimeException("Malformed dex file: insSize (" + numParameterRegisters
    			+ ") > registersSize (" + numRegisters + ")");
    
    for (DebugItem di: code.getDebugItems()) {
        if (di instanceof ImmutableLineNumber) {
            ImmutableLineNumber ln = (ImmutableLineNumber)di;
            DexlibAbstractInstruction ins = instructionAtAddress(ln.getCodeAddress());
            if (ins == null) {
            	Debug.printDbg("Line number tag pointing to invalid offset: " + ln.getCodeAddress());
            	continue;
            }
            ins.setLineNumber(ln.getLineNumber());
            Debug.printDbg("Add line number tag " + ln.getLineNumber() + " for instruction: "
                    + instructionAtAddress(ln.getCodeAddress()));
        }
    }

    this.dexFile = dexFile;
}
 
Example 13
Source File: SyntheticAccessorResolver.java    From HeyGirl with Apache License 2.0 4 votes vote down vote up
@Nullable
public AccessedMember getAccessedMember(@Nonnull MethodReference methodReference) {
    String methodDescriptor = ReferenceUtil.getMethodDescriptor(methodReference);

    AccessedMember accessedMember = resolvedAccessors.get(methodDescriptor);
    if (accessedMember != null) {
        return accessedMember;
    }

    String type = methodReference.getDefiningClass();
    ClassDef classDef = classDefMap.get(type);
    if (classDef == null) {
        return null;
    }

    Method matchedMethod = null;
    MethodImplementation matchedMethodImpl = null;
    for (Method method: classDef.getMethods()) {
        MethodImplementation methodImpl = method.getImplementation();
        if (methodImpl != null) {
            if (methodReferenceEquals(method, methodReference)) {
                matchedMethod = method;
                matchedMethodImpl = methodImpl;
                break;
            }
        }
    }

    if (matchedMethod == null) {
        return null;
    }

    //A synthetic accessor will be marked synthetic
    if (!AccessFlags.SYNTHETIC.isSet(matchedMethod.getAccessFlags())) {
        return null;
    }

    List<Instruction> instructions = ImmutableList.copyOf(matchedMethodImpl.getInstructions());

    int accessType = SyntheticAccessorFSM.test(instructions);

    if (accessType >= 0) {
        AccessedMember member =
                new AccessedMember(accessType, ((ReferenceInstruction)instructions.get(0)).getReference());
        resolvedAccessors.put(methodDescriptor, member);
        return member;
    }
    return null;
}
 
Example 14
Source File: SyntheticAccessorResolver.java    From ZjDroid with Apache License 2.0 4 votes vote down vote up
@Nullable
public AccessedMember getAccessedMember(@Nonnull MethodReference methodReference) {
    String methodDescriptor = ReferenceUtil.getMethodDescriptor(methodReference);

    AccessedMember accessedMember = resolvedAccessors.get(methodDescriptor);
    if (accessedMember != null) {
        return accessedMember;
    }

    String type = methodReference.getDefiningClass();
    ClassDef classDef = classDefMap.get(type);
    if (classDef == null) {
        return null;
    }

    Method matchedMethod = null;
    MethodImplementation matchedMethodImpl = null;
    for (Method method: classDef.getMethods()) {
        MethodImplementation methodImpl = method.getImplementation();
        if (methodImpl != null) {
            if (methodReferenceEquals(method, methodReference)) {
                matchedMethod = method;
                matchedMethodImpl = methodImpl;
                break;
            }
        }
    }

    if (matchedMethod == null) {
        return null;
    }

    //A synthetic accessor will be marked synthetic
    if (!AccessFlags.SYNTHETIC.isSet(matchedMethod.getAccessFlags())) {
        return null;
    }

    List<Instruction> instructions = ImmutableList.copyOf(matchedMethodImpl.getInstructions());

    int accessType = SyntheticAccessorFSM.test(instructions);

    if (accessType >= 0) {
        AccessedMember member =
                new AccessedMember(accessType, ((ReferenceInstruction)instructions.get(0)).getReference());
        resolvedAccessors.put(methodDescriptor, member);
        return member;
    }
    return null;
}