[Soot-list] Exception management with Soot

91 views
Skip to first unread message

Paolo Giannoccaro

unread,
Mar 24, 2018, 11:27:43 AM3/24/18
to soot...@cs.mcgill.ca
Hi,
how I can catch in my java program exceptions thrown by Soot threads ? I tried several options, but such exceptions seem not to be catchable from my application.

Thanks 
Paolo

Jan Peter Stotz

unread,
Mar 24, 2018, 11:36:44 AM3/24/18
to Paolo Giannoccaro, soot...@cs.mcgill.ca
Hi Paolo,

what are you trying to do and what function of Soot do you execute when
he exception occurs you want to catch? Without detailed information all
we can do is guesswork.

Jan
_______________________________________________
Soot-list mailing list
Soot...@CS.McGill.CA
https://mailman.CS.McGill.CA/mailman/listinfo/soot-list

Paolo Giannoccaro

unread,
Mar 24, 2018, 12:07:31 PM3/24/18
to soot...@cs.mcgill.ca, Jan Peter Stotz
Hello Jan, thank you.

I am trying to instrument APK applications using a well know sample taken from Soot tutorials.
I implemented the following code:


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<Unit> units = b.getUnits();
    // important to use snapshotIterator here
    for (Iterator<Unit> iter = units.snapshotIterator(); iter.hasNext();) {
final Unit u = iter.next();
u.apply(new AbstractStmtSwitch() {
    public void caseInvokeStmt(InvokeStmt stmt) {
InvokeExpr invokeExpr = stmt.getInvokeExpr();
String methodSignature = invokeExpr.getMethodRef().getSignature();

String instrumentation = checkCallType(methodSignature);
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("<java.lang.System: java.io.PrintStream out>").makeRef())),
u);
// insert "tmpLong = instrumentation"
LOGGER.debug("--> INSTRUMENTATION " + instrumentation);
units.insertBefore(
Jimple.v().newAssignStmt(tmpString, StringConstant.v(instrumentation)), 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 Jimple code
b.validate();
    }
});
    }
}

    }));
    soot.Main.main(args);


It works quite well on most of public APK files, but sometimes crashes on specific scenarios. For example, if APK is multi dex, it may stop working with the following exception:

[Thread-17] ERROR heros.solver.CountingThreadPoolExecutor - Worker thread execution failed: Dex file overflow. Splitting not support for pre Lollipop Android (Api 22).
java.lang.RuntimeException: Dex file overflow. Splitting not support for pre Lollipop Android (Api 22).
at soot.toDex.MultiDexBuilder.hasOverflowed(MultiDexBuilder.java:62)
at soot.toDex.MultiDexBuilder.internClass(MultiDexBuilder.java:37)
at soot.toDex.DexPrinter.addAsClassDefItem(DexPrinter.java:586)
at soot.toDex.DexPrinter.add(DexPrinter.java:1490)
at soot.PackManager.writeClass(PackManager.java:1060)
at soot.PackManager.access$100(PackManager.java:112)
at soot.PackManager$2.run(PackManager.java:664)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
at java.lang.Thread.run(Thread.java:745)

 I already asked on a different thread regarding this specific issue, but now the question is: how I can catch this exception from my program ? It seems to be thrown by PackManager, but I am not able to catch it (I think it depends on threads nature).

Thanks
Paolo

Jan Peter Stotz

unread,
Mar 25, 2018, 6:51:44 AM3/25/18
to Paolo Giannoccaro, soot...@cs.mcgill.ca
Hi Paolo,

now I understand. There is an Exception caught by Soot and you only see
it in the logs/console output. From my perspective there is only one way
to deal with this problem:

Modify the source code of Soot so that the Exception is correctly
handled. You can modify it and create a Github pull request.

From my perspective the problem is located in soot.PackManager [1].
It uses multiple times ThreadPoolExecutor and executes Runnable within
those Executors. The disadvantage of Runnables is that the Exceptions
are lost if not handled separately.
Instead Callable<Void> implementations should be used. When submitting a
Callable you get a Future. When checking the future for the result (via
get()) a potential Exception thrown inside of the Callable is propagated
to the outside.

This is what you need.

Jan

[1]
https://github.com/Sable/soot/blob/develop/src/main/java/soot/PackManager.java

Paolo Giannoccaro

unread,
Mar 25, 2018, 12:34:01 PM3/25/18
to Jan Peter Stotz, soot...@cs.mcgill.ca
Thank you Jan,
I will try to work on PackManager class (on local first, if results are good I will create a pull request).

Paolo
Reply all
Reply to author
Forward
0 new messages