org.jf.baksmali.BaksmaliOptions Java Examples

The following examples show how to use org.jf.baksmali.BaksmaliOptions. 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: SmaliCodeUtils.java    From atlas with Apache License 2.0 6 votes vote down vote up
/**
 * 生成解析成smali代码的选项
 *
 * @return
 */
private static BaksmaliOptions createBaksmaliOptions(ClassDef classDef) {
    BaksmaliOptions options = new BaksmaliOptions();
    options.deodex = false;
    options.parameterRegisters = false;
    options.localsDirective = true;
    options.sequentialLabels = true;
    options.debugInfo = false;
    options.codeOffsets = false;
    options.accessorComments = false;
    options.registerInfo = 0;// 128
    options.inlineResolver = null;
    options.apiLevel = DEFAULT_API_LEVEL;
    List<ClassDef> classDefs = Lists.newArrayList();
    classDefs.add(classDef);
    options.syntheticAccessorResolver = new SyntheticAccessorResolver(Opcodes.getDefault(),classDefs);
    return options;
}
 
Example #2
Source File: SmaliDiffUtils.java    From atlas with Apache License 2.0 6 votes vote down vote up
public static BaksmaliOptions getBuildOption(Iterable<? extends ClassDef> collection, int apiLevel) {
    BaksmaliOptions options = new BaksmaliOptions();

    options.deodex = false;
    options.parameterRegisters = false;
    options.localsDirective = true;
    options.sequentialLabels = true;
    options.debugInfo = true;
    options.codeOffsets = false;
    options.accessorComments = false;
    options.registerInfo = 0;// 128
    options.inlineResolver = null;
    options.apiLevel = apiLevel;
    if (!options.accessorComments) {
        options.syntheticAccessorResolver = new SyntheticAccessorResolver(Opcodes.getDefault(),collection);
    }

    return options;
}
 
Example #3
Source File: ApkSmaliDecoder.java    From apk-dependency-graph with Apache License 2.0 6 votes vote down vote up
private BaksmaliOptions getSmaliOptions(final DexBackedDexFile dexFile) {
    final BaksmaliOptions options = new BaksmaliOptions();

    options.deodex = false;
    options.implicitReferences = false;
    options.parameterRegisters = true;
    options.localsDirective = true;
    options.sequentialLabels = true;
    options.debugInfo = false;
    options.codeOffsets = false;
    options.accessorComments = false;
    options.registerInfo = 0;

    if (dexFile instanceof DexBackedOdexFile) {
        options.inlineResolver =
                InlineMethodResolver.createInlineMethodResolver(
                    ((DexBackedOdexFile)dexFile).getOdexVersion());
    } else {
        options.inlineResolver = null;
    }

    return options;
}
 
Example #4
Source File: PostInstructionRegisterInfoMethodItem.java    From atlas with Apache License 2.0 6 votes vote down vote up
@Override
public boolean writeTo(IndentingWriter writer) throws IOException {
    int registerInfo = registerFormatter.options.registerInfo;
    int registerCount = analyzedInstruction.getRegisterCount();
    BitSet registers = new BitSet(registerCount);

    if ((registerInfo & BaksmaliOptions.ALL) != 0) {
        registers.set(0, registerCount);
    } else {
        if ((registerInfo & BaksmaliOptions.ALLPOST) != 0) {
            registers.set(0, registerCount);
        } else if ((registerInfo & BaksmaliOptions.DEST) != 0) {
            addDestRegs(registers, registerCount);
        }
    }

    return writeRegisterInfo(writer, registers);
}
 
Example #5
Source File: CatchMethodItem.java    From atlas with Apache License 2.0 6 votes vote down vote up
public CatchMethodItem(@Nonnull BaksmaliOptions options, @Nonnull MethodDefinition.LabelCache labelCache,
                       int codeAddress, @Nullable String exceptionType, int startAddress, int endAddress,
                       int handlerAddress) {
    super(codeAddress);
    this.exceptionType = exceptionType;

    tryStartLabel = labelCache.internLabel(new LabelMethodItem(options, startAddress, "try_start_"));

    //use the address from the last covered instruction, but make the label
    //name refer to the address of the next instruction
    tryEndLabel = labelCache.internLabel(new EndTryLabelMethodItem(options, codeAddress, endAddress));

    if (exceptionType == null) {
        handlerLabel = labelCache.internLabel(new LabelMethodItem(options, handlerAddress, "catchall_"));
    } else {
        handlerLabel = labelCache.internLabel(new LabelMethodItem(options, handlerAddress, "catch_"));
    }
}
 
Example #6
Source File: SmaliDecoder.java    From ratel with Apache License 2.0 5 votes vote down vote up
private void decode() throws AndrolibException {
    try {
        final BaksmaliOptions options = new BaksmaliOptions();

        // options
        options.deodex = false;
        options.implicitReferences = false;
        options.parameterRegisters = true;
        options.localsDirective = true;
        options.sequentialLabels = true;
        options.debugInfo = mBakDeb;
        options.codeOffsets = false;
        options.accessorComments = false;
        options.registerInfo = 0;
        options.inlineResolver = null;

        // set jobs automatically
        int jobs = Runtime.getRuntime().availableProcessors();
        if (jobs > 6) {
            jobs = 6;
        }

        // create the dex
        DexBackedDexFile dexFile = DexFileFactory.loadDexEntry(mApkFile, mDexFile, true, Opcodes.forApi(mApi));

        if (dexFile.isOdexFile()) {
            throw new AndrolibException("Warning: You are disassembling an odex file without deodexing it.");
        }

        if (dexFile instanceof DexBackedOdexFile) {
            options.inlineResolver =
                    InlineMethodResolver.createInlineMethodResolver(((DexBackedOdexFile)dexFile).getOdexVersion());
        }

        Baksmali.disassembleDexFile(dexFile, mOutDir, jobs, options);
    } catch (IOException ex) {
        throw new AndrolibException(ex);
    }
}
 
Example #7
Source File: MethodDefinition.java    From atlas with Apache License 2.0 5 votes vote down vote up
public static void writeEmptyMethodTo(IndentingWriter writer, Method method,
                                      BaksmaliOptions options) throws IOException {
    writer.write(".method ");
    writeAccessFlags(writer, method.getAccessFlags());
    writer.write(method.getName());
    writer.write("(");
    ImmutableList<MethodParameter> methodParameters = ImmutableList.copyOf(method.getParameters());
    for (MethodParameter parameter : methodParameters) {
        writer.write(parameter.getType());
    }
    writer.write(")");
    writer.write(method.getReturnType());
    writer.write('\n');

    writer.indent(4);
    writeParameters(writer, method, methodParameters, options);

    String containingClass = null;
    if (options.implicitReferences) {
        containingClass = method.getDefiningClass();
    }
    //如果是修改的方法,需要添加ReplaceAnnotation
    if (DexDiffInfo.modifiedMethods.contains(method)) {
        MethodReplaceAnnotation replaceAnnotation = new MethodReplaceAnnotation(method.getDefiningClass(), method.getName());
        AnnotationFormatter.writeTo(writer, method.getAnnotations(), containingClass, replaceAnnotation);
    } else {
        AnnotationFormatter.writeTo(writer, method.getAnnotations(), containingClass);
    }
    writer.deindent(4);
    writer.write(".end method\n");
}
 
Example #8
Source File: MethodDefinition.java    From atlas with Apache License 2.0 5 votes vote down vote up
private static void writeParameters(IndentingWriter writer, Method method,
                                    List<? extends MethodParameter> parameters,
                                    BaksmaliOptions options) throws IOException {
    boolean isStatic = AccessFlags.STATIC.isSet(method.getAccessFlags());
    int registerNumber = isStatic ? 0 : 1;
    for (MethodParameter parameter : parameters) {
        String parameterType = parameter.getType();
        String parameterName = parameter.getName();
        Collection<? extends Annotation> annotations = parameter.getAnnotations();
        if (parameterName != null || annotations.size() != 0) {
            writer.write(".param p");
            writer.printSignedIntAsDec(registerNumber);

            if (parameterName != null && options.debugInfo) {
                writer.write(", ");
                ReferenceFormatter.writeStringReference(writer, parameterName);
            }
            writer.write("    # ");
            writer.write(parameterType);
            writer.write("\n");
            if (annotations.size() > 0) {
                writer.indent(4);

                String containingClass = null;
                if (options.implicitReferences) {
                    containingClass = method.getDefiningClass();
                }
                AnnotationFormatter.writeTo(writer, annotations, containingClass);
                writer.deindent(4);
                writer.write(".end param\n");
            }
        }

        registerNumber++;
        if (TypeUtils.isWideType(parameterType)) {
            registerNumber++;
        }
    }
}
 
Example #9
Source File: OffsetInstructionFormatMethodItem.java    From atlas with Apache License 2.0 5 votes vote down vote up
public OffsetInstructionFormatMethodItem(@Nonnull BaksmaliOptions options, @Nonnull MethodDefinition methodDef,
                                         int codeAddress, OffsetInstruction instruction) {
    super(methodDef, codeAddress, instruction);

    label = new LabelMethodItem(options, codeAddress + instruction.getCodeOffset(), getLabelPrefix());
    label = methodDef.getLabelCache().internLabel(label);
}
 
Example #10
Source File: InstructionMethodItem.java    From atlas with Apache License 2.0 5 votes vote down vote up
private boolean isAllowedOdex(@Nonnull Opcode opcode) {
    BaksmaliOptions options = methodDef.classDef.options;
    if (options.allowOdex) {
        return true;
    }

    if (methodDef.classDef.options.apiLevel >= 14) {
        return false;
    }

    return opcode.isVolatileFieldAccessor() || opcode == Opcode.THROW_VERIFICATION_ERROR;
}
 
Example #11
Source File: SmaliUtils.java    From atlas with Apache License 2.0 5 votes vote down vote up
/**
 * 生成解析成smali代码的选项
 * @return
 */
private static BaksmaliOptions createBaksmaliOptions() {
    BaksmaliOptions options = new BaksmaliOptions();
    options.deodex = false;
    options.parameterRegisters = false;
    options.localsDirective = true;
    options.sequentialLabels = true;
    options.debugInfo = true;
    options.codeOffsets = false;
    options.accessorComments = false;
    options.registerInfo = 0;// 128
    options.inlineResolver = null;
    options.apiLevel = DEFAULT_API_LEVEL;
    return options;
}
 
Example #12
Source File: ClassDefinition.java    From atlas with Apache License 2.0 5 votes vote down vote up
public ClassDefinition(@Nonnull BaksmaliOptions options, @Nonnull ClassDef classDef,
                       @Nonnull boolean isScan, @Nonnull boolean fullMethod) {
    this.options = options;
    this.classDef = classDef;
    this.isScan = isScan;
    this.fullMethod = fullMethod;
    fieldsSetInStaticConstructor = findFieldsSetInStaticConstructor();
}
 
Example #13
Source File: ClassDefinition.java    From atlas with Apache License 2.0 5 votes vote down vote up
public ClassDefinition(BaksmaliOptions options, ClassDef classDef) {
    this.options = options;
    this.classDef = classDef;
    this.isScan = false;
    this.fieldsSetInStaticConstructor = findFieldsSetInStaticConstructor();
    this.fullMethod = true;
}
 
Example #14
Source File: SmaliUtils.java    From jadx with Apache License 2.0 4 votes vote down vote up
private static void getSmaliCode(DexBackedClassDef classDef, StringWriter stringWriter) throws IOException {
	ClassDefinition classDefinition = new ClassDefinition(new BaksmaliOptions(), classDef);
	classDefinition.writeTo(new IndentingWriter(stringWriter));
}
 
Example #15
Source File: SmaliUtils.java    From Box with Apache License 2.0 4 votes vote down vote up
private static String getSmaliCode(DexBackedClassDef classDef) throws IOException {
	ClassDefinition classDefinition = new ClassDefinition(new BaksmaliOptions(), classDef);
	StringWriter sw = new StringWriter();
	classDefinition.writeTo(new IndentingWriter(sw));
	return sw.toString();
}
 
Example #16
Source File: ApkToolPlus.java    From ApkToolPlus with Apache License 2.0 4 votes vote down vote up
/**
 * .dex转换为smali
 *
 * @param dexFile       dex/apk文件
 * @param outDir        smali文件输出目录
 * @return  是否转换成功
 */
public static boolean dex2smali(File dexFile, File outDir){

    if (dexFile == null || !dexFile.exists()){
        LogUtils.w( "dex2smali dexFile is null or not exists : " + dexFile.getPath());
        return false;
    }

    // dex文件的处理
    BaksmaliOptions options = new BaksmaliOptions();

    // options
    options.deodex = false;
    options.implicitReferences = false;
    options.parameterRegisters = true;
    options.localsDirective = true;
    options.sequentialLabels = true;
    options.debugInfo = true;
    options.codeOffsets = false;
    options.accessorComments = false;
    options.registerInfo = 0;
    options.inlineResolver = null;

    // set jobs automatically
    int jobs = Runtime.getRuntime().availableProcessors();
    if (jobs > 6) {
        jobs = 6;
    }

    try {
        //brut/androlib/ApkDecoder.mApi default value is 15
        // create the dex
        DexBackedDexFile dexBackedDexFile = DexFileFactory.loadDexFile(dexFile, Opcodes.forApi(15));

        if (dexBackedDexFile.isOdexFile()) {
            LogUtils.w("Warning: You are disassembling an odex file without deodexing it.");
        }

        if (dexBackedDexFile instanceof DexBackedOdexFile) {
            options.inlineResolver =
                    InlineMethodResolver.createInlineMethodResolver(((DexBackedOdexFile)dexBackedDexFile).getOdexVersion());
        }

        Baksmali.disassembleDexFile(dexBackedDexFile, outDir, jobs, options);
    } catch (IOException e) {
        e.printStackTrace();
    }
    return false;
}
 
Example #17
Source File: LabelMethodItem.java    From atlas with Apache License 2.0 4 votes vote down vote up
public LabelMethodItem(@Nonnull BaksmaliOptions options, int codeAddress, @Nonnull String labelPrefix) {
    super(codeAddress);
    this.options = options;
    this.labelPrefix = labelPrefix;
}
 
Example #18
Source File: EndTryLabelMethodItem.java    From atlas with Apache License 2.0 4 votes vote down vote up
public EndTryLabelMethodItem(@Nonnull BaksmaliOptions options, int codeAddress, int endTryAddress) {
    super(options, codeAddress, "try_end_");
    this.endTryAddress = endTryAddress;
}
 
Example #19
Source File: RegisterFormatter.java    From atlas with Apache License 2.0 4 votes vote down vote up
public RegisterFormatter(@Nonnull BaksmaliOptions options, int registerCount, int parameterRegisterCount) {
    this.options = options;
    this.registerCount = registerCount;
    this.parameterRegisterCount = parameterRegisterCount;
}
 
Example #20
Source File: SmaliUtils.java    From Box with Apache License 2.0 4 votes vote down vote up
private static String getSmaliCode(DexBackedClassDef classDef) throws IOException {
	ClassDefinition classDefinition = new ClassDefinition(new BaksmaliOptions(), classDef);
	StringWriter sw = new StringWriter();
	classDefinition.writeTo(new IndentingWriter(sw));
	return sw.toString();
}