soot.jimple.internal.JNopStmt Java Examples

The following examples show how to use soot.jimple.internal.JNopStmt. 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: AndroidEntryPointCreator.java    From JAADAS with GNU General Public License v3.0 5 votes vote down vote up
/**
 * Generates the lifecycle for an Android content provider class
 * @param entryPoints The list of methods to consider in this class
 * @param currentClass The class for which to build the content provider
 * lifecycle
 * @param endClassStmt The statement to which to jump after completing
 * the lifecycle
 * @param classLocal The local referencing an instance of the current class
 */
private void generateContentProviderLifecycle
		(Set<String> entryPoints,
		SootClass currentClass,
		JNopStmt endClassStmt,
		Local classLocal) {
	// As we don't know the order in which the different Android lifecycles
	// run, we allow for each single one of them to be skipped
	createIfStmt(endClassStmt);

	// ContentProvider.onCreate() runs before everything else, even before
	// Application.onCreate(). We must thus handle it elsewhere.
	// Stmt onCreateStmt = searchAndBuildMethod(AndroidEntryPointConstants.CONTENTPROVIDER_ONCREATE, currentClass, entryPoints, classLocal);
	
	// see: http://developer.android.com/reference/android/content/ContentProvider.html
	//methods
	JNopStmt startWhileStmt = new JNopStmt();
	JNopStmt endWhileStmt = new JNopStmt();
	body.getUnits().add(startWhileStmt);
	createIfStmt(endWhileStmt);
	
	boolean hasAdditionalMethods = false;
	if (modelAdditionalMethods) {
		for (SootMethod currentMethod : currentClass.getMethods())
			if (entryPoints.contains(currentMethod.toString()))
				hasAdditionalMethods |= createPlainMethodCall(classLocal, currentMethod);
	}
	addCallbackMethods(currentClass);
	body.getUnits().add(endWhileStmt);
	if (hasAdditionalMethods)
		createIfStmt(startWhileStmt);
	// createIfStmt(onCreateStmt);
}
 
Example #2
Source File: AndroidEntryPointCreator.java    From JAADAS with GNU General Public License v3.0 5 votes vote down vote up
/**
 * Generates the lifecycle for an Android broadcast receiver class
 * @param entryPoints The list of methods to consider in this class
 * @param currentClass The class for which to build the broadcast receiver
 * lifecycle
 * @param endClassStmt The statement to which to jump after completing
 * the lifecycle
 * @param classLocal The local referencing an instance of the current class
 */
private void generateBroadcastReceiverLifecycle
		(Set<String> entryPoints,
		SootClass currentClass,
		JNopStmt endClassStmt,
		Local classLocal) {
	// As we don't know the order in which the different Android lifecycles
	// run, we allow for each single one of them to be skipped
	createIfStmt(endClassStmt);

	Stmt onReceiveStmt = searchAndBuildMethod(AndroidEntryPointConstants.BROADCAST_ONRECEIVE, currentClass, entryPoints, classLocal);
	//methods
	JNopStmt startWhileStmt = new JNopStmt();
	JNopStmt endWhileStmt = new JNopStmt();
	body.getUnits().add(startWhileStmt);
	createIfStmt(endWhileStmt);
	
	boolean hasAdditionalMethods = false;
	if (modelAdditionalMethods) {
		for (SootMethod currentMethod : currentClass.getMethods())
			if (entryPoints.contains(currentMethod.toString()))
				hasAdditionalMethods |= createPlainMethodCall(classLocal, currentMethod);
	}
	addCallbackMethods(currentClass);
	body.getUnits().add(endWhileStmt);
	if (hasAdditionalMethods)
		createIfStmt(startWhileStmt);
	createIfStmt(onReceiveStmt);
}
 
Example #3
Source File: AndroidEntryPointCreator.java    From JAADAS with GNU General Public License v3.0 5 votes vote down vote up
private boolean createPlainMethodCall(Local classLocal, SootMethod currentMethod) {
	// Do not create calls to lifecycle methods which we handle explicitly
	if(AndroidEntryPointConstants.getServiceLifecycleMethods().contains(currentMethod.getSubSignature()))
		return false;
	
	JNopStmt beforeStmt = new JNopStmt();
	JNopStmt thenStmt = new JNopStmt();
	body.getUnits().add(beforeStmt);
	createIfStmt(thenStmt);
	buildMethodCall(currentMethod, body, classLocal, generator);
	
	body.getUnits().add(thenStmt);
	createIfStmt(beforeStmt);
	return true;
}
 
Example #4
Source File: CustomEntryPointCreator.java    From steady with Apache License 2.0 4 votes vote down vote up
private SootMethod generateMethodImplementation(SootMethod methodToImplement,
                                                final SootClass generatedDummyClass) {
    SootMethod generatedMethod = new SootMethod(methodToImplement.getName(), methodToImplement.getParameterTypes(), methodToImplement.getReturnType());
    Body body = Jimple.v().newBody();
    body.setMethod(generatedMethod);
    generatedMethod.setActiveBody(body);

    // add locals for Parameter
    // Add a parameter reference to the body
    LocalGenerator lg = new LocalGenerator(body);

    //create a local for the this reference
    if (!methodToImplement.isStatic()) {
        Local thisLocal = lg.generateLocal(generatedDummyClass.getType());
        body.getUnits().addFirst(Jimple.v().newIdentityStmt(thisLocal, Jimple.v().newThisRef(generatedDummyClass.getType())));
    }

    int i = 0;
    for (Type type : generatedMethod.getParameterTypes()) {
        Local paramLocal = lg.generateLocal(type);
        body.getUnits().add(Jimple.v().newIdentityStmt(paramLocal,
                Jimple.v().newParameterRef(type, i)));
        i++;
    }

    JNopStmt startStmt = new JNopStmt();
    JNopStmt endStmt = new JNopStmt();

    body.getUnits().add(startStmt);


    //check if return type is void (check first, since next call includes void)
    if (methodToImplement.getReturnType() instanceof VoidType) {
        body.getUnits().add(Jimple.v().newReturnVoidStmt());
    }
    // if sootClass is simpleClass
    else if (isSimpleType(methodToImplement.getReturnType().toString())) {
        Local varLocal = lg.generateLocal(getSimpleTypeFromType(methodToImplement.getReturnType()));

        AssignStmt aStmt = Jimple.v().newAssignStmt(varLocal, getSimpleDefaultValue(methodToImplement.getReturnType()));
        body.getUnits().add(aStmt);
        body.getUnits().add(Jimple.v().newReturnStmt(varLocal));
    } else {
        body.getUnits().add(Jimple.v().newReturnStmt(NullConstant.v()));

    }

    //remove the abstract Modifier from the new implemented method
    generatedMethod.setModifiers(methodToImplement.getModifiers() ^ Modifier.ABSTRACT);

    return generatedMethod;
}
 
Example #5
Source File: AndroidEntryPointCreator.java    From JAADAS with GNU General Public License v3.0 4 votes vote down vote up
/**
 * Adds calls to the callback methods defined in the application class
 * @param applicationClass The class in which the user-defined application
 * is implemented
 * @param applicationLocal The local containing the instance of the
 * user-defined application
 */
private void addApplicationCallbackMethods() {
	if (!this.callbackFunctions.containsKey(applicationClass.getName()))
		return;
	
	// Do not try to generate calls to methods in non-concrete classes
	if (applicationClass.isAbstract())
		return;
	if (applicationClass.isPhantom()) {
		System.err.println("Skipping possible application callbacks in "
				+ "phantom class " + applicationClass);
		return;
	}

	for (String methodSig : this.callbackFunctions.get(applicationClass.getName())) {
		SootMethodAndClass methodAndClass = SootMethodRepresentationParser.v().parseSootMethodString(methodSig);
	
		// We do not consider lifecycle methods which are directly inserted
		// at their respective positions
		if (AndroidEntryPointConstants.getApplicationLifecycleMethods().contains
				(methodAndClass.getSubSignature()))
			continue;
				
		SootMethod method = findMethod(Scene.v().getSootClass(methodAndClass.getClassName()),
				methodAndClass.getSubSignature());
		// If we found no implementation or if the implementation we found
		// is in a system class, we skip it. Note that null methods may
		// happen since all callback interfaces for application callbacks
		// are registered under the name of the application class.
		if (method == null)
			continue;
		if (method.getDeclaringClass().getName().startsWith("android.")
				|| method.getDeclaringClass().getName().startsWith("java."))
			continue;
		
		// Get the local instance of the target class
		Local local = this.localVarsForClasses.get(methodAndClass.getClassName());
		if (local == null) {
			System.err.println("Could not create call to application callback "
					+ method.getSignature() + ". Local was null.");
			continue;
		}

		// Add a conditional call to the method
		JNopStmt thenStmt = new JNopStmt();
		createIfStmt(thenStmt);
		buildMethodCall(method, body, local, generator);	
		body.getUnits().add(thenStmt);
	}
}
 
Example #6
Source File: DefaultEntryPointCreator.java    From JAADAS with GNU General Public License v3.0 4 votes vote down vote up
@Override
protected SootMethod createDummyMainInternal(SootMethod mainMethod) {
	Map<String, Set<String>> classMap =
			SootMethodRepresentationParser.v().parseClassNames(methodsToCall, false);
	
	// create new class:
	Body body = mainMethod.getActiveBody();
		LocalGenerator generator = new LocalGenerator(body);
	HashMap<String, Local> localVarsForClasses = new HashMap<String, Local>();
	
	// create constructors:
	for(String className : classMap.keySet()){
		SootClass createdClass = Scene.v().forceResolve(className, SootClass.BODIES);
		createdClass.setApplicationClass();
		
		Local localVal = generateClassConstructor(createdClass, body);
		if (localVal == null) {
			logger.warn("Cannot generate constructor for class: {}", createdClass);
			continue;
		}
		localVarsForClasses.put(className, localVal);
	}
	
	// add entrypoint calls
	int conditionCounter = 0;
	JNopStmt startStmt = new JNopStmt();
	JNopStmt endStmt = new JNopStmt();
	Value intCounter = generator.generateLocal(IntType.v());
	body.getUnits().add(startStmt);
	for (Entry<String, Set<String>> entry : classMap.entrySet()){
		Local classLocal = localVarsForClasses.get(entry.getKey());
		for (String method : entry.getValue()){
			SootMethodAndClass methodAndClass =
					SootMethodRepresentationParser.v().parseSootMethodString(method);
			SootMethod currentMethod = findMethod(Scene.v().getSootClass(methodAndClass.getClassName()),
					methodAndClass.getSubSignature());
			if (currentMethod == null) {
				logger.warn("Entry point not found: {}", method);
				continue;
			}
			
			JEqExpr cond = new JEqExpr(intCounter, IntConstant.v(conditionCounter));
			conditionCounter++;
			JNopStmt thenStmt = new JNopStmt();
			JIfStmt ifStmt = new JIfStmt(cond, thenStmt);
			body.getUnits().add(ifStmt);
			buildMethodCall(currentMethod, body, classLocal, generator);
			body.getUnits().add(thenStmt);
		}
	}
	body.getUnits().add(endStmt);
	JGotoStmt gotoStart = new JGotoStmt(startStmt);
	body.getUnits().add(gotoStart);
	
	body.getUnits().add(Jimple.v().newReturnVoidStmt());
	NopEliminator.v().transform(body);
	eliminateSelfLoops(body);
	return mainMethod;
}
 
Example #7
Source File: BoomerangPretransformer.java    From SPDS with Eclipse Public License 2.0 2 votes vote down vote up
/**
 * The first statement of a method must be a nop statement, because the call-flow functions do only map parameters
 * to arguments. If the first statement of a method would be an assign statement, the analysis misses data-flows.
 */
private void addNopStmtToMethods(Body b) {
    b.getUnits().insertBefore(new JNopStmt(), b.getUnits().getFirst());
}