public ExecutionContext spawnRootContext(VirtualMethod method) { return spawnRootContext(method, null, 0); }
public ExecutionContext spawnRootContext(String methodSignature) { String[] parts = methodSignature.split("->"); String className = parts[0]; String methodDescriptor = parts[1]; return spawnRootContext(className, methodDescriptor); }
public ExecutionGraph execute(VirtualMethod method) throws VirtualMachineException { ExecutionContext context = spawnRootContext(method); return execute(method, context, null, null); }
public ExecutionContext spawnRootContext(String className, String methodDescriptor) { VirtualMethod method = getClassManager().getVirtualClass(className).getMethod(methodDescriptor); if (method == null) { throw new RuntimeException("Method signature not found: " + className + "->" + methodDescriptor); } return spawnRootContext(method); }
private static void executePrintParameter(int parameterValue) throws VirtualMachineException { String className = "Lorg/cf/demosmali/Main;"; String methodDescriptor = "printParameter(I)V"; // Execute method with context which contains the argument values. ExecutionContext context = vm.spawnRootContext(className, methodDescriptor); MethodState mState = context.getMethodState(); mState.assignParameter(3, parameterValue, "I"); vm.execute(className, methodDescriptor, context); }
private static void executeParameterLogicWithKnownParameter(int parameterValue) throws VirtualMachineException { String methodSignature = "Lorg/cf/demosmali/Main;->parameterLogic(I)I"; ExecutionContext context = vm.spawnRootContext(methodSignature); MethodState mState = context.getMethodState(); // This method has 4 locals (r0, r1, r2, r3). Since it's virtual (not static) the first parameter (p0 or r4) // is the 'this' reference, or contains a reference to the object. // First method parameter starts at r5, or parameter start + 1. mState.assignParameter(mState.getParameterStart() + 1, parameterValue, "I"); ExecutionGraph graph = vm.execute(methodSignature, context); HeapItem item = graph.getTerminatingRegisterConsensus(MethodState.ReturnRegister); System.out.println("With context, returns " + parameterValue + ": " + item); }
private ExecutionContext buildLocalCalleeContext(ExecutionContext callerContext, VirtualMethod method) { ExecutionContext calleeContext = vm.spawnRootContext(method, callerContext, getAddress()); MethodState callerMethodState = callerContext.getMethodState(); MethodState calleeMethodState = calleeContext.getMethodState(); assignCalleeMethodArguments(callerMethodState, calleeMethodState); // VirtualClass state merging is handled by the VM. return calleeContext; }
ExecutionContext context = manipulator.getVM().spawnRootContext(manipulator.getMethod()); HeapItem item = context.getMethodState().peekRegister(registerA); preCastType = item.getType();
} else { assert METHOD_ROOT_ADDRESS == newLocation.getCodeAddress(); newContext = vm.spawnRootContext(method); newNode.setContext(newContext);
private static ExecutionContext buildContext(VirtualMachine vm) { VirtualClass dummyClass = vm.getClassManager().getVirtualClass(DUMMY_CLASS_NAME); VirtualMethod inertStatic = dummyClass.getMethod("dummyMethod()V"); return vm.spawnRootContext(inertStatic); }
private static ExecutionContextMethod test(VirtualMachine vm, ExecutionContext context, String fieldName, String callingMethodSignature, boolean setAccessible) throws Exception { // Setup caller context ExecutionContext callerContext = vm.spawnRootContext(callingMethodSignature); Field callerContextField = ExecutionContext.class.getDeclaredField("callerContext"); callerContextField.setAccessible(true); callerContextField.set(context, callerContext); // Setup actual field to use as parameter ClassLoader classLoader = vm.getClassLoader(); Class<?> klazz = classLoader.loadClass(DUMMY_CLASS_NAME_BINARY); Constructor<?> ctor = klazz.getDeclaredConstructor(); ctor.setAccessible(true); Object instance = ctor.newInstance(); Field field = instance.getClass().getDeclaredField(fieldName); field.setAccessible(setAccessible); // Setup method state and arguments HeapItem fieldItem = new HeapItem(field, "Ljava/lang/reflect/Field;"); HeapItem instanceItem = new HeapItem(instance, DUMMY_CLASS_NAME); MethodState methodState = context.getMethodState(); methodState.pokeRegister(0, fieldItem); methodState.pokeRegister(1, instanceItem); ExecutionContextMethod fieldGet = new java_lang_reflect_Field_get(); fieldGet.execute(vm, null, context); return fieldGet; }
ExecutionContext initContext = vm.spawnRootContext(method); ClassState cState = initContext.peekClassState(virtualClass); initContext.initializeClass(cState, SideEffect.Level.NONE);
private static ExecutionContext buildInitializedContext(VirtualMachine vm, String methodSignature, VMState state) { VirtualMethod method = vm.getClassManager().getMethod(methodSignature); ExecutionContext context = vm.spawnRootContext(method); int registerCount = context.getMethodState().getRegisterCount(); setupMethodState(context, state.getRegisters(), registerCount); setupClassStates(context, vm, state.getFields()); return context; }