Java Code Examples for soot.jimple.JimpleBody#validate()

The following examples show how to use soot.jimple.JimpleBody#validate() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: AbstractTestingFramework.java    From SPDS with Eclipse Public License 2.0 6 votes vote down vote up
private String getTargetClass() {
    SootClass sootClass = new SootClass("dummyClass");
    Type paramType = ArrayType.v(RefType.v("java.lang.String"), 1);
    SootMethod mainMethod = new SootMethod("main", Collections.singletonList(paramType), VoidType.v(),
            Modifier.PUBLIC | Modifier.STATIC);
    sootClass.addMethod(mainMethod);
    JimpleBody body = Jimple.v().newBody(mainMethod);
    mainMethod.setActiveBody(body);
    RefType testCaseType = RefType.v(getTestCaseClassName());
    Local loc = Jimple.v().newLocal("l0", paramType);
    body.getLocals().add(loc);
    body.getUnits().add(Jimple.v().newIdentityStmt(loc, Jimple.v().newParameterRef(paramType, 0)));
    Local allocatedTestObj = Jimple.v().newLocal("dummyObj", testCaseType);
    body.getLocals().add(allocatedTestObj);
    body.getUnits().add(Jimple.v().newAssignStmt(allocatedTestObj, Jimple.v().newNewExpr(testCaseType)));
    body.getUnits().add(
            Jimple.v().newInvokeStmt(Jimple.v().newVirtualInvokeExpr(allocatedTestObj, sootTestMethod.makeRef())));
    body.getUnits().add(Jimple.v().newReturnVoidStmt());

    Scene.v().addClass(sootClass);
    body.validate();
    return sootClass.toString();
}
 
Example 2
Source File: ReflectiveCallsInliner.java    From JAADAS with GNU General Public License v3.0 4 votes vote down vote up
private void addCaching(Kind kind) {
	
	SootClass c;
	String methodName;
	switch(kind) {
	case ClassNewInstance:
		c = Scene.v().getSootClass("java.lang.Class");
		methodName = "knownClassNewInstance";
		break;
	case ConstructorNewInstance: 
		c = Scene.v().getSootClass("java.lang.reflect.Constructor");
		methodName = "knownConstructorNewInstance";
		break;
	case MethodInvoke: 
		c = Scene.v().getSootClass("java.lang.reflect.Method");
		methodName = "knownMethodInvoke";
		break;
	case ClassForName:
		//Cannot implement caching in this case because we can add no field to the String argument
		return;
	default:
		throw new IllegalStateException("unknown kind: "+kind);
	}
	
	SootClass reflCallsClass = Scene.v().getSootClass("soot.rtlib.tamiflex.ReflectiveCalls");
	
	SootMethod m = reflCallsClass.getMethodByName(methodName);
	JimpleBody body = (JimpleBody) m.retrieveActiveBody();
	LocalGenerator localGen = new LocalGenerator(body);
	Unit firstStmt = body.getFirstNonIdentityStmt();
	firstStmt = body.getUnits().getPredOf(firstStmt);
	
	Stmt jumpTarget = Jimple.v().newNopStmt();
	
	Chain<Unit> newUnits = new HashChain<Unit>();
	
	//alreadyCheckedLocal = m.alreadyChecked
	InstanceFieldRef fieldRef = Jimple.v().newInstanceFieldRef(body.getParameterLocal(m.getParameterCount()-1), Scene.v().makeFieldRef(c, ALREADY_CHECKED_FIELDNAME, BooleanType.v(), false));
	Local alreadyCheckedLocal = localGen.generateLocal(BooleanType.v());
	newUnits.add(Jimple.v().newAssignStmt(alreadyCheckedLocal, fieldRef));
	
	//if(!alreadyChecked) goto jumpTarget
	newUnits.add(Jimple.v().newIfStmt(Jimple.v().newEqExpr(alreadyCheckedLocal, IntConstant.v(0)), jumpTarget));
	
	//return
	newUnits.add(Jimple.v().newReturnVoidStmt());
	
	//jumpTarget: nop		
	newUnits.add(jumpTarget);
	
	//m.alreadyChecked = true
	InstanceFieldRef fieldRef2 = Jimple.v().newInstanceFieldRef(body.getParameterLocal(m.getParameterCount()-1), Scene.v().makeFieldRef(c, ALREADY_CHECKED_FIELDNAME, BooleanType.v(), false));
	newUnits.add(Jimple.v().newAssignStmt(fieldRef2, IntConstant.v(1)));
	
	body.getUnits().insertAfter(newUnits, firstStmt);
	
	if(Options.v().validate()) body.validate();
}
 
Example 3
Source File: Mocker.java    From DroidRA with GNU Lesser General Public License v2.1 4 votes vote down vote up
public static SootMethod mockSootMethod(String clsName, String methodSubSignature, boolean isStatic)
{
	SootClass sc = mockSootClass(clsName);
	
	SootMethod sm = null;

	try
	{
		sm = sc.getMethod(methodSubSignature);
	}
	catch (Exception ex)
	{
		sm = null;
	}

	if (null == sm)
	{
		int m = Modifier.PUBLIC;
		if (isStatic)
		{
			m = m | Modifier.STATIC;
		}
		
		List<Type> paramTypes = new ArrayList<Type>();
		paramTypes.add(ArrayType.v(RefType.v("java.lang.Object"), 1));
		
		String[] strs = methodSubSignature.split(" ");
		String methodName = strs[1].trim().substring(0, strs[1].trim().indexOf("("));
		
		if (null == methodName || methodName.isEmpty())
		{
			return null;
		}
		
		sm = new SootMethod(methodName, paramTypes, RefType.v("java.lang.Object"), m);
		sc.addMethod(sm);
		
		//Add body of sm
		JimpleBody b = Jimple.v().newBody(sm);
        sm.setActiveBody(b);
        //LocalGenerator lg = new LocalGenerator(b);
		{
			b.insertIdentityStmts();
			
			
			
			//Local rtLoc = lg.generateLocal(RefType.v("java.lang.Object"));
			
			//Local param0 = lg.generateLocal(ArrayType.v(RefType.v("java.lang.Object"), 1));
			//Unit param0U = Jimple.v().newIdentityStmt(rtLoc, Jimple.v().newParameterRef(ArrayType.v(RefType.v("java.lang.Object"), 1), 0));
			
			
			//Unit rtLocAssignU = Jimple.v().newAssignStmt(rtLoc, param0);
			
			Unit returnU = Jimple.v().newReturnStmt(b.getParameterLocal(0));
			
			//b.getUnits().add(param0U);
			b.getUnits().add(returnU);
		}
		
		System.out.println("validation:" + b);
		b.validate();
	}	
	
	return sm;
}
 
Example 4
Source File: DummyMainGenerator.java    From DroidRA with GNU Lesser General Public License v2.1 4 votes vote down vote up
public SootMethod generateMain(Set<String> components)
{
	SootMethod mainMethod = new SootMethod(DUMMY_METHOD_NAME, 
			Arrays.asList(new Type[] {ArrayType.v(RefType.v("java.lang.String"), 1)}), 
   			VoidType.v(), 
   			Modifier.PUBLIC | Modifier.STATIC);
   	JimpleBody body = Jimple.v().newBody(mainMethod);
   	mainMethod.setActiveBody(body);
   	
   	SootClass sootClass = new SootClass(DUMMY_CLASS_NAME);
   	sootClass.setSuperclass(Scene.v().getSootClass("java.lang.Object"));
   	sootClass.setPhantom(false);
   	sootClass.setApplicationClass();
   	sootClass.setInScene(true);
   	
   	sootClass.addMethod(mainMethod);
	
   	LocalGenerator generator = new LocalGenerator(body);
	
   	body.insertIdentityStmts();
   	
	for (String str : components)
	{
		SootClass sc = Scene.v().getSootClass(str);
		if (sc.isPhantom())
		{
			continue;
		}
		
		SootMethod method = ICCDummyMainCreator.v().generateDummyMainMethod(str);
		instrumentDummyMainMethod(method);
		
		SootClass cls = method.getDeclaringClass();
		SootMethod sootMethod = cls.getMethod("<init>", new ArrayList<Type>());
		
		if (null == sootMethod)
		{
			throw new RuntimeException("No default constructor for comp " + cls.getName());
		}
		
		Local al = generator.generateLocal(cls.getType());
		Unit newU = (Unit) Jimple.v().newAssignStmt(al, Jimple.v().newNewExpr(cls.getType()));
		
		Unit initU = (Unit) Jimple.v().newInvokeStmt(
				Jimple.v().newSpecialInvokeExpr(al, sootMethod.makeRef()));
		
		Unit callU = (Unit) Jimple.v().newInvokeStmt(
				Jimple.v().newSpecialInvokeExpr(al, method.makeRef()));
		
		body.getUnits().add(newU);
		body.getUnits().add(initU);
		body.getUnits().add(callU);
	}
	
	body.getUnits().add(Jimple.v().newReturnVoidStmt());
	
	if (fullMethodCover)
	{
		mainMethod = appendNonComponents(mainMethod);
	}
	
	System.out.println(body);
	
	body.validate();
	
	return mainMethod;
}