Skip to content
jgf edited this page May 18, 2012 · 2 revisions

This short example shows how the DexAnalysis interface may be used for program analysis.

We assume that the android jar files can be found in the directory "./data/". Those are:

data/core.jar:data/ext.jar:data/framework.jar:data/android.policy.jar:data/services.jar
  • Baksmali: The example .dex file we use is "./data/JLex.dex".
  • Smali: The example .smali assembler sources should be located in "./data/smali-src/"

All graphs (CFG, DOM, CDG) are dumped as .dot files in the directories "./out/baksmali/" and "./out/smali/". If these output directories do not exist, they are created.

public class CallBakSmaliAnalysis {

	public static void main(final String[] args) throws DexAnalysisException, FileNotFoundException {
		System.out.println("========= BAKSMALI =========");
		final DexProgram bakSmaliProg = testBakSmali();
		DexAnalysisUtil.printProgram(bakSmaliProg);
		System.out.print("Dumping graphs... ");
		DexAnalysisUtil.dumpGraphsTo(bakSmaliProg, "./out/baksmali/");
		System.out.println("done.");
	}

	private static DexProgram testBakSmali() throws DexAnalysisException {
		final BakSmaliAnalysis.BakSmaliConfig conf = new BakSmaliAnalysis.BakSmaliConfig();
		System.out.println(conf);
		final BakSmaliAnalysis analysis = new BakSmaliAnalysis(conf);
		final DexProgram prog = analysis.analyze("data/JLex.dex");
		
		return prog;
	}

}

public class CallSmaliAnalysis {

	public static void main(final String[] args) throws DexAnalysisException, FileNotFoundException {
		System.out.println("========= SMALI ============");
		final DexProgram smaliProg = testSmali();
		DexAnalysisUtil.printProgram(smaliProg);
		System.out.print("Dumping graphs... ");
		DexAnalysisUtil.dumpGraphsTo(smaliProg, "./out/smali/");
		System.out.println("done.");
	}

	private static DexProgram testSmali() throws DexAnalysisException {
		final SmaliAnalysis.SmaliConfig conf = new SmaliAnalysis.SmaliConfig();
		System.out.println(conf);
		final SmaliAnalysis analysis = new SmaliAnalysis(conf);
		final SmaliAnalysis.SmaliInput input = new SmaliAnalysis.SmaliInput();
		input.addFile("./data/smali-src");
		final DexProgram prog = analysis.analyze(input);
		
		return prog;
	}

}

public class DexAnalysisUtil {

	private DexAnalysisUtil() {}

	public static void printProgram(final DexProgram prog) {
		System.out.println(prog);
	
		for (final DexClass cls : prog.getClasses()) {
			System.out.println(cls);
			
			for (final DexMethod method : cls.getMethods()) {
				System.out.print(method);
				
				System.out.print(" - computing graphs");
				method.getControlFlowGraph(false);
				System.out.print(".");
				method.getControlFlowGraph(true);
				System.out.print(".");
				method.getDominanceFrontiers(false);
				System.out.print(".");
				method.getDominanceFrontiers(true);
				System.out.print(".");
				method.getDominators(false);
				System.out.print(".");
				method.getDominators(true);
				System.out.print(".");
				method.getDominationTree(false);
				System.out.print(".");
				method.getDominationTree(true);
				System.out.print(".");
				method.getControlDependenceGraph(false);
				System.out.print(".");
				method.getControlDependenceGraph(true);
				System.out.println("done.");
			}
		}
	}

	public static void dumpGraphsTo(final DexProgram dexProg, final String toDir) throws FileNotFoundException {
		final GraphDumper gDumpNoExc = new GraphDumper(toDir, "no-exc-", true, true, true, false);
		dexProg.dumpGraphs(gDumpNoExc);
		final GraphDumper gDumpWithExc = new GraphDumper(toDir, "with-exc-", true, true, true, true);
		dexProg.dumpGraphs(gDumpWithExc);
	}

}

Clone this wiki locally