Skip to content

Instantly share code, notes, and snippets.

@citostyle
Last active February 12, 2016 05:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save citostyle/d05f748545aa8e5fe413 to your computer and use it in GitHub Desktop.
Save citostyle/d05f748545aa8e5fe413 to your computer and use it in GitHub Desktop.
private static class ConnectionTransformer extends BodyTransformer {
@Override
protected void internalTransform(Body b, String phaseName, Map<String, String> options) {
SootMethod method = b.getMethod();
final PatchingChain<Unit> units = b.getUnits();
Unit u = null;
Iterator<Unit> iter = units.snapshotIterator();
while(iter.hasNext()) {
u = iter.next();
if(isConnection(u, InstrumentAAPackages.getConnectionStatements())) {
Date d = new Date();
System.out.println("####### connectionStatement " + d.getTime());
Local printRef = addPrintRef(b);
Local systemRef = addSystemRef(b);
Local stringMethodRef = addStringRef(b, "method");
Local stringTimestampRef = addStringRef(b, "timestamp");
Local dateRef = addDateRef(b);
Local longRef = addLongRef(b);
// insert "printRef = java.lang.System.out;"
units.insertBefore(Jimple.v().newAssignStmt(
printRef, Jimple.v().newStaticFieldRef(
Scene.v().getField("<java.lang.System: java.io.PrintStream out>").makeRef())), u);
//try with System.currentTimeMillis();
SootClass system = Scene.v().getSootClass("java.lang.System");
SootMethod currentTime = system.getMethod("long <currentTimeMillis>()");
units.insertBefore(Jimple.v().newAssignStmt(
dateRef, Jimple.v().newNewExpr(RefType.v("java.util.Date"))), u);
units.insertBefore(Jimple.v().newAssignStmt(
longRef,
Jimple.v().newStaticInvokeExpr(currentTime.makeRef())),
u);
units.insertBefore(Jimple.v().newAssignStmt(stringMethodRef,
StringConstant.v("#######http: " + method.getSignature())), u);
// insert "printRef.println(dateRef);"
SootMethod printlnCall = Scene.v().getSootClass("java.io.PrintStream").getMethod("void println(java.lang.String)");
units.insertBefore(Jimple.v().newInvokeStmt(
Jimple.v().newVirtualInvokeExpr(printRef, printlnCall.makeRef(), stringTimestampRef)), u);
//insert "dateRef".getTime()
SootMethod getTimeCall = Scene.v().getSootClass("java.util.Date").getMethod("long getTime()");
units.insertBefore(Jimple.v().newAssignStmt(
longRef,
Jimple.v().newVirtualInvokeExpr(dateRef, getTimeCall.makeRef())),
u);
// insert "printRef.println(stringRef);"
SootMethod printlnCall = Scene.v().getSootClass("java.io.PrintStream").getMethod("void println(java.lang.String)");
units.insertBefore(Jimple.v().newInvokeStmt(
Jimple.v().newVirtualInvokeExpr(printRef, printlnCall.makeRef(), stringMethodRef)), u);
//units.insertBefore(Jimple.v().newAssignStmt(longRef, LongConstant.v(10000L)), u);
SootMethod printlnCallLong = Scene.v().getSootClass("java.io.PrintStream").getMethod("void println(long)");
units.insertBefore(Jimple.v().newInvokeStmt(
Jimple.v().newVirtualInvokeExpr(printRef, printlnCallLong.makeRef(), longRef)), u);
}
//check that we did not mess up the Jimple
b.validate();
}
}
}
private static Local addTempRef(Body body, String name, String refType) {
Local tmpRef = Jimple.v().newLocal(name, RefType.v(refType));
body.getLocals().add(tmpRef);
return tmpRef;
}
private static Local addPrintRef(Body body)
{
return addTempRef(body, "lprint", "java.io.PrintStream");
}
public static Local addSystemRef(Body body) {
return addTempRef(body, "lsystem", "java.lang.System");
}
private static Local addStringRef(Body body, String label)
{
return addTempRef(body, label, "java.lang.String");
}
private static Local addDateRef(Body body)
{
return addTempRef(body, "ldate", "java.util.Date");
}
private static Local addLongRef(Body body)
{
//return addTempRef(body, "llong", "java.lang.Long");
Local tmpRef = Jimple.v().newLocal("tmpRef", LongType.v());
body.getLocals().add(tmpRef);
return tmpRef;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment