Search in sources :

Example 11 with Method

use of org.jf.dexlib2.iface.Method in project android-classyshark by google.

the class RootBuilder method fillFromDex.

private void fillFromDex(File file, ClassNode rootNode) {
    try {
        DexFile dxFile = DexlibLoader.loadDexFile(file);
        Set<? extends ClassDef> classSet = dxFile.getClasses();
        for (ClassDef o : classSet) {
            int methodCount = 0;
            for (Method method : o.getMethods()) {
                methodCount++;
            }
            String translatedClassName = o.getType().replaceAll("\\/", "\\.").substring(1, o.getType().length() - 1);
            ClassInfo classInfo = new ClassInfo(translatedClassName, methodCount);
            rootNode.add(classInfo);
        }
    } catch (Exception ex) {
        System.err.println("Error parsing Dexfile: " + file.getName() + ": " + ex.getMessage());
        ex.printStackTrace(System.err);
    }
}
Also used : ClassDef(org.jf.dexlib2.iface.ClassDef) Method(org.jf.dexlib2.iface.Method) DexFile(org.jf.dexlib2.iface.DexFile) IOException(java.io.IOException)

Example 12 with Method

use of org.jf.dexlib2.iface.Method in project smali by JesusFreke.

the class DexUtil method verifyOdexHeader.

/**
     * Reads in the odex header from the given input stream and verifies that it is valid and a supported version
     *
     * The inputStream must support mark(), and will be reset to initial position upon exiting the method
     *
     * @param inputStream An input stream that is positioned at an odex header
     * @throws NotAnOdexFile If the file is not an odex file
     * @throws UnsupportedFile If the odex header is valid, but is an unsupported version
     */
public static void verifyOdexHeader(@Nonnull InputStream inputStream) throws IOException {
    if (!inputStream.markSupported()) {
        throw new IllegalArgumentException("InputStream must support mark");
    }
    inputStream.mark(8);
    byte[] partialHeader = new byte[8];
    try {
        ByteStreams.readFully(inputStream, partialHeader);
    } catch (EOFException ex) {
        throw new NotAnOdexFile("File is too short");
    } finally {
        inputStream.reset();
    }
    verifyOdexHeader(partialHeader, 0);
}
Also used : NotAnOdexFile(org.jf.dexlib2.dexbacked.DexBackedOdexFile.NotAnOdexFile) EOFException(java.io.EOFException)

Example 13 with Method

use of org.jf.dexlib2.iface.Method in project smali by JesusFreke.

the class DexUtil method verifyDexHeader.

/**
     * Reads in the dex header from the given input stream and verifies that it is valid and a supported version
     *
     * The inputStream must support mark(), and will be reset to initial position upon exiting the method
     *
     * @param inputStream An input stream that is positioned at a dex header
     * @throws NotADexFile If the file is not a dex file
     * @throws InvalidFile If the header appears to be a dex file, but is not valid for some reason
     * @throws UnsupportedFile If the dex header is valid, but uses unsupported functionality
     */
public static void verifyDexHeader(@Nonnull InputStream inputStream) throws IOException {
    if (!inputStream.markSupported()) {
        throw new IllegalArgumentException("InputStream must support mark");
    }
    inputStream.mark(44);
    byte[] partialHeader = new byte[44];
    try {
        ByteStreams.readFully(inputStream, partialHeader);
    } catch (EOFException ex) {
        throw new NotADexFile("File is too short");
    } finally {
        inputStream.reset();
    }
    verifyDexHeader(partialHeader, 0);
}
Also used : NotADexFile(org.jf.dexlib2.dexbacked.DexBackedDexFile.NotADexFile) EOFException(java.io.EOFException)

Example 14 with Method

use of org.jf.dexlib2.iface.Method in project smali by JesusFreke.

the class SyntheticAccessorResolver method getAccessedMember.

@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;
}
Also used : MethodImplementation(org.jf.dexlib2.iface.MethodImplementation) ClassDef(org.jf.dexlib2.iface.ClassDef) Method(org.jf.dexlib2.iface.Method) Instruction(org.jf.dexlib2.iface.instruction.Instruction) ReferenceInstruction(org.jf.dexlib2.iface.instruction.ReferenceInstruction) Nullable(javax.annotation.Nullable)

Example 15 with Method

use of org.jf.dexlib2.iface.Method in project smali by JesusFreke.

the class DexWriter method writeDebugAndCodeItems.

private void writeDebugAndCodeItems(@Nonnull DexDataWriter offsetWriter, @Nonnull DeferredOutputStream temp) throws IOException {
    ByteArrayOutputStream ehBuf = new ByteArrayOutputStream();
    debugSectionOffset = offsetWriter.getPosition();
    DebugWriter<StringKey, TypeKey> debugWriter = new DebugWriter<StringKey, TypeKey>(stringSection, typeSection, offsetWriter);
    DexDataWriter codeWriter = new DexDataWriter(temp, 0);
    List<CodeItemOffset<MethodKey>> codeOffsets = Lists.newArrayList();
    for (ClassKey classKey : classSection.getSortedClasses()) {
        Collection<? extends MethodKey> directMethods = classSection.getSortedDirectMethods(classKey);
        Collection<? extends MethodKey> virtualMethods = classSection.getSortedVirtualMethods(classKey);
        Iterable<MethodKey> methods = Iterables.concat(directMethods, virtualMethods);
        for (MethodKey methodKey : methods) {
            List<? extends TryBlock<? extends ExceptionHandler>> tryBlocks = classSection.getTryBlocks(methodKey);
            Iterable<? extends Instruction> instructions = classSection.getInstructions(methodKey);
            Iterable<? extends DebugItem> debugItems = classSection.getDebugItems(methodKey);
            if (instructions != null && stringSection.hasJumboIndexes()) {
                boolean needsFix = false;
                for (Instruction instruction : instructions) {
                    if (instruction.getOpcode() == Opcode.CONST_STRING) {
                        if (stringSection.getItemIndex((StringRef) ((ReferenceInstruction) instruction).getReference()) >= 65536) {
                            needsFix = true;
                            break;
                        }
                    }
                }
                if (needsFix) {
                    MutableMethodImplementation mutableMethodImplementation = classSection.makeMutableMethodImplementation(methodKey);
                    fixInstructions(mutableMethodImplementation);
                    instructions = mutableMethodImplementation.getInstructions();
                    tryBlocks = mutableMethodImplementation.getTryBlocks();
                    debugItems = mutableMethodImplementation.getDebugItems();
                }
            }
            int debugItemOffset = writeDebugItem(offsetWriter, debugWriter, classSection.getParameterNames(methodKey), debugItems);
            int codeItemOffset;
            try {
                codeItemOffset = writeCodeItem(codeWriter, ehBuf, methodKey, tryBlocks, instructions, debugItemOffset);
            } catch (RuntimeException ex) {
                throw new ExceptionWithContext(ex, "Exception occurred while writing code_item for method %s", methodSection.getMethodReference(methodKey));
            }
            if (codeItemOffset != -1) {
                codeOffsets.add(new CodeItemOffset<MethodKey>(methodKey, codeItemOffset));
            }
        }
    }
    offsetWriter.align();
    codeSectionOffset = offsetWriter.getPosition();
    codeWriter.close();
    temp.writeTo(offsetWriter);
    temp.close();
    for (CodeItemOffset<MethodKey> codeOffset : codeOffsets) {
        classSection.setCodeItemOffset(codeOffset.method, codeSectionOffset + codeOffset.codeOffset);
    }
}
Also used : ByteArrayOutputStream(java.io.ByteArrayOutputStream) OneRegisterInstruction(org.jf.dexlib2.iface.instruction.OneRegisterInstruction) Instruction(org.jf.dexlib2.iface.instruction.Instruction) ReferenceInstruction(org.jf.dexlib2.iface.instruction.ReferenceInstruction) ExceptionWithContext(org.jf.util.ExceptionWithContext) MutableMethodImplementation(org.jf.dexlib2.builder.MutableMethodImplementation)

Aggregations

ClassDef (org.jf.dexlib2.iface.ClassDef)21 Method (org.jf.dexlib2.iface.Method)21 MethodImplementation (org.jf.dexlib2.iface.MethodImplementation)19 Test (org.junit.Test)16 Instruction (org.jf.dexlib2.iface.instruction.Instruction)15 DexFile (org.jf.dexlib2.iface.DexFile)13 ImmutableMethod (org.jf.dexlib2.immutable.ImmutableMethod)13 ImmutableClassDef (org.jf.dexlib2.immutable.ImmutableClassDef)11 ImmutableDexFile (org.jf.dexlib2.immutable.ImmutableDexFile)10 MethodImplementationBuilder (org.jf.dexlib2.builder.MethodImplementationBuilder)7 MutableMethodImplementation (org.jf.dexlib2.builder.MutableMethodImplementation)7 BuilderInstruction10x (org.jf.dexlib2.builder.instruction.BuilderInstruction10x)7 DexBackedClassDef (org.jf.dexlib2.dexbacked.DexBackedClassDef)7 ImmutableMethodParameter (org.jf.dexlib2.immutable.ImmutableMethodParameter)7 Nonnull (javax.annotation.Nonnull)6 Opcode (org.jf.dexlib2.Opcode)6 BuilderInstruction21t (org.jf.dexlib2.builder.instruction.BuilderInstruction21t)6 BuilderInstruction22c (org.jf.dexlib2.builder.instruction.BuilderInstruction22c)6 ReferenceInstruction (org.jf.dexlib2.iface.instruction.ReferenceInstruction)6 MethodReference (org.jf.dexlib2.iface.reference.MethodReference)6