use of org.jf.dexlib2.iface.DexFile in project smali by JesusFreke.
the class Baksmali method disassembleDexFile.
public static boolean disassembleDexFile(DexFile dexFile, File outputDir, int jobs, final BaksmaliOptions options, @Nullable List<String> classes) {
//sort the classes, so that if we're on a case-insensitive file system and need to handle classes with file
//name collisions, then we'll use the same name for each class, if the dex file goes through multiple
//baksmali/smali cycles for some reason. If a class with a colliding name is added or removed, the filenames
//may still change of course
List<? extends ClassDef> classDefs = Ordering.natural().sortedCopy(dexFile.getClasses());
final ClassFileNameHandler fileNameHandler = new ClassFileNameHandler(outputDir, ".smali");
ExecutorService executor = Executors.newFixedThreadPool(jobs);
List<Future<Boolean>> tasks = Lists.newArrayList();
Set<String> classSet = null;
if (classes != null) {
classSet = new HashSet<String>(classes);
}
for (final ClassDef classDef : classDefs) {
if (classSet != null && !classSet.contains(classDef.getType())) {
continue;
}
tasks.add(executor.submit(new Callable<Boolean>() {
@Override
public Boolean call() throws Exception {
return disassembleClass(classDef, fileNameHandler, options);
}
}));
}
boolean errorOccurred = false;
try {
for (Future<Boolean> task : tasks) {
while (true) {
try {
if (!task.get()) {
errorOccurred = true;
}
} catch (InterruptedException ex) {
continue;
} catch (ExecutionException ex) {
throw new RuntimeException(ex);
}
break;
}
}
} finally {
executor.shutdown();
}
return !errorOccurred;
}
use of org.jf.dexlib2.iface.DexFile in project smali by JesusFreke.
the class DeodexCommand method getOptions.
@Override
protected BaksmaliOptions getOptions() {
BaksmaliOptions options = super.getOptions();
options.deodex = true;
if (dexFile instanceof DexBackedOdexFile) {
if (inlineTable == null) {
options.inlineResolver = InlineMethodResolver.createInlineMethodResolver(((DexBackedOdexFile) dexFile).getOdexVersion());
} else {
File inlineTableFile = new File(inlineTable);
if (!inlineTableFile.exists()) {
System.err.println(String.format("Could not find file: %s", inlineTable));
System.exit(-1);
}
try {
options.inlineResolver = new CustomInlineMethodResolver(options.classPath, inlineTableFile);
} catch (IOException ex) {
System.err.println(String.format("Error while reading file: %s", inlineTableFile));
ex.printStackTrace(System.err);
System.exit(-1);
}
}
}
return options;
}
use of org.jf.dexlib2.iface.DexFile in project smali by JesusFreke.
the class DisassembleCommand method getOptions.
protected BaksmaliOptions getOptions() {
if (dexFile == null) {
throw new IllegalStateException("You must call loadDexFile first");
}
final BaksmaliOptions options = new BaksmaliOptions();
if (needsClassPath()) {
try {
options.classPath = analysisArguments.loadClassPathForDexFile(inputFile.getAbsoluteFile().getParentFile(), dexFile, shouldCheckPackagePrivateAccess());
} catch (Exception ex) {
System.err.println("\n\nError occurred while loading class path files. Aborting.");
ex.printStackTrace(System.err);
System.exit(-1);
}
}
if (!resourceIdFiles.isEmpty()) {
Map<String, File> resourceFiles = Maps.newHashMap();
assert (resourceIdFiles.size() % 2) == 0;
for (int i = 0; i < resourceIdFiles.size(); i += 2) {
String resourcePrefix = resourceIdFiles.get(i);
String publicXml = resourceIdFiles.get(i + 1);
File publicXmlFile = new File(publicXml);
if (!publicXmlFile.exists()) {
System.err.println(String.format("Can't find file: %s", publicXmlFile));
System.exit(-1);
}
resourceFiles.put(resourcePrefix, publicXmlFile);
}
try {
options.loadResourceIds(resourceFiles);
} catch (IOException ex) {
System.err.println("Error while loading resource files:");
ex.printStackTrace(System.err);
System.exit(-1);
} catch (SAXException ex) {
System.err.println("Error while loading resource files:");
ex.printStackTrace(System.err);
System.exit(-1);
}
}
options.parameterRegisters = parameterRegisters;
options.localsDirective = localsDirective;
options.sequentialLabels = sequentialLabels;
options.debugInfo = debugInfo;
options.codeOffsets = codeOffsets;
options.accessorComments = accessorComments;
options.implicitReferences = implicitReferences;
options.normalizeVirtualMethods = normalizeVirtualMethods;
options.registerInfo = 0;
for (String registerInfoType : registerInfoTypes) {
if (registerInfoType.equalsIgnoreCase("ALL")) {
options.registerInfo |= BaksmaliOptions.ALL;
} else if (registerInfoType.equalsIgnoreCase("ALLPRE")) {
options.registerInfo |= BaksmaliOptions.ALLPRE;
} else if (registerInfoType.equalsIgnoreCase("ALLPOST")) {
options.registerInfo |= BaksmaliOptions.ALLPOST;
} else if (registerInfoType.equalsIgnoreCase("ARGS")) {
options.registerInfo |= BaksmaliOptions.ARGS;
} else if (registerInfoType.equalsIgnoreCase("DEST")) {
options.registerInfo |= BaksmaliOptions.DEST;
} else if (registerInfoType.equalsIgnoreCase("MERGE")) {
options.registerInfo |= BaksmaliOptions.MERGE;
} else if (registerInfoType.equalsIgnoreCase("FULLMERGE")) {
options.registerInfo |= BaksmaliOptions.FULLMERGE;
} else {
System.err.println(String.format("Invalid register info type: %s", registerInfoType));
usage();
System.exit(-1);
}
if ((options.registerInfo & BaksmaliOptions.FULLMERGE) != 0) {
options.registerInfo &= ~BaksmaliOptions.MERGE;
}
}
if (accessorComments) {
options.syntheticAccessorResolver = new SyntheticAccessorResolver(dexFile.getOpcodes(), dexFile.getClasses());
}
return options;
}
use of org.jf.dexlib2.iface.DexFile in project smali by JesusFreke.
the class AnalysisTest method runTest.
public void runTest(String test, boolean registerInfo, boolean isArt) throws IOException, URISyntaxException {
String dexFilePath = String.format("%s%sclasses.dex", test, File.separatorChar);
DexFile dexFile = DexFileFactory.loadDexFile(findResource(dexFilePath), Opcodes.getDefault());
BaksmaliOptions options = new BaksmaliOptions();
if (registerInfo) {
options.registerInfo = BaksmaliOptions.ALL | BaksmaliOptions.FULLMERGE;
if (isArt) {
options.classPath = new ClassPath(new ArrayList<ClassProvider>(), true, 56);
} else {
options.classPath = new ClassPath();
}
}
options.implicitReferences = false;
for (ClassDef classDef : dexFile.getClasses()) {
StringWriter stringWriter = new StringWriter();
IndentingWriter writer = new IndentingWriter(stringWriter);
ClassDefinition classDefinition = new ClassDefinition(options, classDef);
classDefinition.writeTo(writer);
writer.close();
String className = classDef.getType();
String smaliPath = String.format("%s%s%s.smali", test, File.separatorChar, className.substring(1, className.length() - 1));
String smaliContents = readResource(smaliPath);
Assert.assertEquals(TextUtils.normalizeWhitespace(smaliContents), TextUtils.normalizeWhitespace((stringWriter.toString())));
}
}
use of org.jf.dexlib2.iface.DexFile in project smali by JesusFreke.
the class FieldGapOrderTest method testNewOrder.
@Test
public void testNewOrder() {
DexFile dexFile = getInputDexFile("FieldGapOrder", new BaksmaliOptions());
Assert.assertEquals(3, dexFile.getClasses().size());
ClassPath classPath = new ClassPath(Lists.newArrayList(new DexClassProvider(dexFile)), false, 67);
ClassProto classProto = (ClassProto) classPath.getClass("LGapOrder;");
Assert.assertEquals("s", classProto.getFieldByOffset(10).getName());
Assert.assertEquals("r1", classProto.getFieldByOffset(12).getName());
Assert.assertEquals("r2", classProto.getFieldByOffset(16).getName());
Assert.assertEquals("i", classProto.getFieldByOffset(20).getName());
Assert.assertEquals("d", classProto.getFieldByOffset(24).getName());
}
Aggregations