Eric Bodden, Ph.D. Current conditions in Darmstadt: Broken Clouds, 20°C
20°C

Head of Secure Software Engineering Group at EC SPRIDE
Principal Investigator in Secure Services at CASED
  • rss
  • Home
  • Research
    • Publications
    • Presentations
    • Current research
      • Inter-procedural Data-flow Analysis of Software Product Lines
      • RefaFlex – Safer refactorings for reflective Java programs
      • Join Point Interfaces
      • Stateful Breakpoints
      • MOPBox
      • Closure Joinpoints for AspectJ
      • Proving Security Properties of Services
      • TamiFlex: a tool set for Taming Reflection
    • Past Research
      • Efficient Runtime Verification
      • Racer: Effective Race Detection Using AspectJ
      • Continuation-equivalent states (ICSE 2010)
      • Aspect-oriented programming and design
      • Visual specification languages
      • A denial-of-service attack on the Java bytecode verifier
      • Clara: Compile-time Approximation of Runtime Analyses
    • Hosting a Program Committee meeting with Skype
  • Tools
    • SPLlift – highly efficient product line analysis
    • Heros – Inter-Procedural Data-Flow Analysis
    • Behavior Compliance Control
    • Join Point Interfaces
    • TamiFlex: a tool set for Taming Reflection
    • Closure Joinpoints for AspectJ
    • Clara: Compile-time Approximation of Runtime Analyses
    • RacerAJ (for race detection)
    • An introduction to Soot 2.2.5
    • J-LO, a tool for runtime-checking temporal assertions
    • Aspect-oriented approaches targeting the .NET Framework
  • Teaching
    • Current lectures and thesis topics
    • Past lectures
      • Automated Software Engineering
      • Software-Engineering Project
      • COMP 520
      • COMP 621
  • About me
  • Photos

Instrumenting Android Apps with Soot

Eric | January 8, 2013

I am excited to let you know that we have recently committed to the development Branch of Soot support for reading and writing Dalvik bytecode with Soot. (This code will also be contained in Soot’s upcoming release.) This supports consists of two major modules. One is called Dexpler, mainly developed by a group around Alexandre Bartel, and with some enhancements by Ben Bellamy and myself as well as Frank Hartmann and Michael Markert, two students of mine. Dexpler converts Dalvik bytecode into Jimple’s three-address code. This may sound simple – after all Dalvik code is register based and Jimple uses local variables which are quite similar to logical registers. However, things get tricky with respect to typing. Jimple is typed; every local variable is of some declared type. In Dalvik, registers are untyped, and during the execution of a method the same register can hold values of quite different types. Constants in Dalvik are also untyped: when loading a double or a long into a register, Dalvik just loads an eight-byte bit-pattern into the register without telling you whether it’s a long or double. But in Jimple we need this information. Thus getting the typing of Jimple locals right is quite tricky and took us a while. On the other hand, typed locals are great, as they allow for a simpler and more precise pointer analysis, among other things.

The second component does just the opposite: it converts Jimple back into Dalvik code. This component was completed quite recently by Thomas Pilot, another one of my students. One of the main obstacles here is again the mismatch between local variables and registers: Soot needs to perform an at least somewhat clever register allocation to avoid using up too many registers. This currently works well enough to produce functional Dalvik code, however the code may sometimes not have the same structure as the original Dalvik code you read into Soot.

How to instrument

First grab the latest version of Soot, for instance our nightly build. Also check out the directory at https://github.com/Sable/android-platforms. This directory contains different versions of the Android standard library that Soot requires for resolving types of apps you analyze or instrument.
Next we implement a driver class with a main method into which we stick the following code:

//prefer Android APK files// -src-prec apk
Options.v().set_src_prec(Options.src_prec_apk);

//output as APK, too//-f J
Options.v().set_output_format(Options.output_format_dex);

// resolve the PrintStream and System soot-classes
Scene.v().addBasicClass("java.io.PrintStream",SootClass.SIGNATURES);
Scene.v().addBasicClass("java.lang.System",SootClass.SIGNATURES);

The first option instructs Soot to load Android APK files. The second one instructs Soot to produce a Dex/APK file as output. (In theory you could also convert Java into Dex or Dex into Java and so on.) The last two options tell Soot to load two classes which we will require for our instrumentation but which may otherwise not be required by the instrumented APK.

Next we add a Transform to Soot:

PackManager.v().getPack("jtp").add(new Transform("jtp.myInstrumenter", new BodyTransformer() {

	@Override
	protected void internalTransform(final Body b, String phaseName, @SuppressWarnings("rawtypes") Map options) {
		final PatchingChain units = b.getUnits();		
		//important to use snapshotIterator here
		for(Iterator iter = units.snapshotIterator(); iter.hasNext();) {
			final Unit u = iter.next();
			u.apply(new AbstractStmtSwitch() {

				public void caseInvokeStmt(InvokeStmt stmt) {
					//code here
				}

			});
		}
	}
}));

This will walk through all Units of all Bodies in the APK and on every InvokeStmt will invoke the code which I labeled with “code here”.

At this place we can now insert the following:

InvokeExpr invokeExpr = stmt.getInvokeExpr();
if(invokeExpr.getMethod().getName().equals("onDraw")) {

	Local tmpRef = addTmpRef(b);
	Local tmpString = addTmpString(b);

	  // insert "tmpRef = java.lang.System.out;" 
    units.insertBefore(Jimple.v().newAssignStmt( 
                  tmpRef, Jimple.v().newStaticFieldRef( 
                  Scene.v().getField("").makeRef())), u);

    // insert "tmpLong = 'HELLO';" 
    units.insertBefore(Jimple.v().newAssignStmt(tmpString, 
                  StringConstant.v("HELLO")), u);

    // insert "tmpRef.println(tmpString);" 
    SootMethod toCall = Scene.v().getSootClass("java.io.PrintStream").getMethod("void println(java.lang.String)");                    
    units.insertBefore(Jimple.v().newInvokeStmt(
                  Jimple.v().newVirtualInvokeExpr(tmpRef, toCall.makeRef(), tmpString)), u);

    //check that we did not mess up the Jimple
    b.validate();
}

This causes Soot to insert a System.out.println("HELLO") just before the method invocation but only if the target of this invocation is an onDraw method.

Last but not least, don’t forget to actually call Soot’s main method:

soot.Main.main(args);

And that’s it! Piece of cake, isn’t it? All you now need to do is run your driver class with the following arguments:

-android-jars path/to/android-platforms -process-dir your.apk

Here path/to/android-platforms is the path to the platform JAR files you downloaded earlier and your.apk is the path to the APK you with to instrument. The option -process-dir instructs Soot to process all classes inside this APK.
As a result you will find a new APK with the same name inside the directory ./sootOutput.

You can download the entire code of the example here: AndroidInstrument.java

If you find any bugs in those components (or other parts of Soot) please help us out by reporting them in our issue tracker.

Related Posts

  1. Using Soot with custom entry points
  2. Packs and phases in Soot
  3. First steps using Soot 2.3.0 as a command-line tool
  4. Analyzing and transforming Java and Android programs with Soot
  5. Using the Soot Eclipse plugin
Categories
Research
Tags
Soot Tutorial
Comments rss
Comments rss
Trackback
Trackback

« A recap on our research progress in 2012 SC submission deadline is approaching »

9 Responses to “Instrumenting Android Apps with Soot”

  1. Stats says:
    January 16, 2013 at 8:25 pm

    Thanks to you and the rest of the team on adding Android support in Soot.

    I need to so analyze some Android programs and was debating between WALA and Soot, now with the addition of the Dexpler, I am leaning heavily towards using Soot with this new feature.

    I have a few questions:

    - Does the Dexpler library fail in converting the Dalvik byte code into Jimple? If so details on those cases would be helpful?

    - How through is the android support, ie, are all entry points in Android supported? All the components lifecycle. Can you point to me the code where the entry points are setup in the new changes?

    If there are any holes in Android support, would be happy to help fixing them.

    Reply
    • Eric says:
      January 17, 2013 at 8:56 am

      Hi.

      I am afraid, I don’t understand your first question. Dexpler is now part of Soot, and it’s the conversion we are using. It may not be bug free but it generally works quite well. If you are referring to the version that existed at the time of writing of the Dexpler publication then yes, that still had a few bugs, which are now fixed.

      We do have code that models the Android lifecycle but we do not make that available yet, as it is still incomplete. Current support is for instrumentation and intra-procedural analysis only, which requires no lifecycle information.

      Eric

      Reply
      • Stats says:
        January 18, 2013 at 8:49 pm

        I am interested in doing data flow analysis within Android programs, and am thinking of using Soot for that. For ex. would like to know what app’s do with location information.

        Is there an example showing how data flow analysis can be done using Soot on Android APK?

        Thanks,
        –Dhinakar

        Reply
  2. Amin says:
    January 20, 2013 at 5:55 pm

    Thanks you Prof. Eric Bodden for adding a new tutorial about Instrumenting android app with soot.
    I implemented your sample code and ran it. every things works great, but right now I want to convert an android java file to .jimple file without create APK file (the reason refers to use and find the syntax of some variables and classes in this jimple file for instrumentation code). could you please tell me how can I convert a java android file to jimple with command line and which library or .jar file is necessary for that ?

    Reply
    • Eric says:
      January 21, 2013 at 8:36 am

      Hi Amin.

      That would work like for any other .java file. At this point it’s not android specific any more. If you want to analyze MyClass.java in the current directory just use:
      java soot.Main -cp . -pp MyClass

      See here for more details.

      Reply
  3. visitor says:
    May 5, 2013 at 11:37 am

    Thank you for your page Prof.Eric
    I’m a soot beginner,my problem is where should I put the “-android-jars path/to/android-platforms -process-dir your.apk” ,so I can run it

    Reply
    • Eric says:
      May 13, 2013 at 10:29 am

      On the command line!?

      Reply
  4. Yuru says:
    May 11, 2013 at 1:42 pm

    Hi Eric,

    It seems that soot-2.5.0 does NOT support dex analysis. The download link of the nightly build version is invalid.

    Would you please tell where to grab the latest version of soot ? Thanks!

    Yuru

    Reply
    • Eric says:
      May 13, 2013 at 10:29 am

      Hello.

      Our nightly build server is currently being restructured. You will need to build Soot yourself. A manual is on Github.

      Eric

      Reply

Leave a Reply

Click here to cancel reply.

Welcome

Welcome to my website. Interested in my research? Click here for details or jump directly to my publications.

Upcoming Conferences

SC 2013

SOAP 2013

ESEC/FSE 2013

PPPJ 2013

RV 2013

Photos

Categories & Feeds

  • Research
    RSS
    (176)
  • Misc
    RSS
    (99)
  • Montreal
    RSS
    (44)

Collaborations

  • Don Batory, UTA
  • Eric Tanter, Universidad de Chile
  • Friedrich Steimann, Fernuni Hagen
  • Grigore Rosu, UIUC
  • Hans Vangheluwe, McGill University/Universiteit Antwerpen
  • Jacques Klein, SnT Luxembourg
  • Klaus Havelund, NASA JPL
  • Laurie Hendren, McGill University
  • Martin Monperrus, Univ. of Lille
  • Matthew Dwyer, University of Nebraska
  • Oege de Moor, University of Oxford
  • Ondrej Lhotak, University of Waterloo
  • Patrick Lam, University of Waterloo
  • Rahul Purandare
  • Sarfraz Khurshid, UTA
  • Shahar Maoz, RWTH Aachen
  • Tian Zhao, UW Milwaukee
  • Volker Stolz, University of Oslo

Research projects

  • AspectBench Compiler (abc)
  • Clara
  • J-LO
  • Soot
  • Stratified aspects
  • TamiFlex

Service

  • AOSD 2006
  • AOSD 2007
  • AOSD 2010
  • AOSD 2011
  • AOSD 2012
  • ATPS 2013
  • ATVA 2008
  • ECOOP 2008 Doctoral Symposium
  • ECOOP 2010
  • ESEC/FSE 2011 New Ideas Track
  • ESEC/FSE 2013
  • FOAL 2010
  • FOAL 2012
  • FOAL 2013
  • ICSE 2010
  • ICSE 2013 (New Ideas)
  • IEEE Transactions on Software Engineering (TSE)
  • International Journal of Image and Graphics
  • ISSTA 2011
  • NFM 2011
  • OOPSLA 2008
  • OOPSLA 2010
  • OOPSLA 2012
  • PEPM 2008
  • PLDI 2006
  • PLDI 2008
  • RAM-SE 2011
  • RV 2007
  • RV 2009
  • RV 2010
  • RV 2011
  • SAC 2012
  • SC 2011
  • SC 2013
  • SEFM 2005
  • SEFM 2008
  • Transactions on Software Engineering and Methodology (TOSEM)
  • VMIL 2008
  • VMIL 2009

Some other people I know

  • Adrian Colyer
  • Bruno Dufour
  • Dan North
  • Daniel Klink
  • Dave Thomas
  • Dean Wampler
  • Eric Tanter
  • Friedrich Steimann
  • Joachim Kneis
  • Klaus Havelund
  • Kristin Lovejoy
  • Liz Keogh
  • Malte Clasen
  • Markus Schorn
  • Pascal Costanza
  • Patricia Jablonski
  • Philip Mayer
  • Ron Bodkin
  • Sven Wittig
  • Wiebke Berg

Some people not to confuse me with

  • Eric B. the terrorist
  • Eric Bodden the basketball player
  • Eric Bodden the chef who sunk
  • Master Sgt. Eric Bodden

Previous Posts

January 2013
M T W T F S S
« Dec   Feb »
 123456
78910111213
14151617181920
21222324252627
28293031  

Tags

Alumni AOP AOSD AspectJ Atlanta Bike Blizzard Bug finding Caro Clara COMP 621 Eclipse FSE Google ISSTA Java LinkedIn Mac McGill Microsoft Montreal NASA Photos Programming Quebec City Race detection Racer Runtime Monitoring Runtime verification RV RWTH Seattle Slides Snow storm Soot Soot Tutorial Static Analysis Strike TamiFlex TA strike Thesis tracematches Typestate Vacation Winter carnival


rss Comments rss valid xhtml 1.1 design by jide powered by Wordpress get firefox