private static void setupLauncher() { if (launcher == null) { VirtualMachineFactory vmFactory = new VirtualMachineFactory(); launcher = new Launcher(vmFactory); } } }
@Test public void runsWithoutMajorFailureWithSmaliFolder() throws IOException, UnhandledVirtualException { Launcher launcher = new Launcher(new VirtualMachineFactory()); File outFile = File.createTempFile("simplify-test", ".tmp"); launcher.run(new String[] { "src/test/resources/obfuscated-example", "-it", "WhiteNoise", "-o", outFile.getAbsolutePath() }); assertTrue(outFile.exists()); Files.delete(outFile.toPath()); }
@Test public void runsWithoutMajorFailureWithDexFile() throws IOException, UnhandledVirtualException { Launcher launcher = new Launcher(new VirtualMachineFactory()); File outFile = File.createTempFile("simplify-test", ".tmp"); launcher.run(new String[] { "src/test/resources/obfuscated-example.zip", "-it", "WhiteNoise", "-o", outFile.getAbsolutePath() }); assertTrue(outFile.exists()); Files.delete(outFile.toPath()); } }
public static void main(String[] args) throws Exception { // The SMALI_PATH directory is populated using dumpSmali.sh and contains converted code from org.cf.demosmali. // You can use a link to a DEX, APK, or directory with Smali files. VirtualMachineFactory vmFactory = new VirtualMachineFactory(); vm = vmFactory.build(SMALI_PATH); // The smali code contains System.out.println()s used for debugging, but smalivm won't execute this method // because it's not white listed because it affects state outside of the VM. Also, smalivm doesn't have a // "screen" to send output. This hooks those method calls with our own implementation to actually print stuff. MethodEmulator .addMethod("Ljava/io/PrintStream;->println(Ljava/lang/String;)V", java_io_PrintStream_println.class); // Execute demo smali's main() method vm.execute("Lorg/cf/demosmali/Main;->main([Ljava/lang/String;)V"); executePrintParameter(42); executeParameterLogicWithUnknownParameter(); executeParameterLogicWithKnownParameter(10); }
/** * Create a new {@link VirtualMachine} for testing. Since this is heavily used, it tries to avoid the main cost of creating a {@link * VirtualMachine} by reusing the same {@link ClassManager} by default. If {@code reloadClasses} is true, a new {@link ClassManager} is created * and all classes are loaded again. This is necessary if method implementations are modified. For example, Simplify optimization strategy tests * modify method implementation and in order for each test to have the true method implementations, many of those tests set {@code reloadClasses} * to {@code true}. * * @param reloadClasses if true, rebuild {@link ClassManager}, otherwise reuse existing * @return {@link VirtualMachine} for tests */ public static VirtualMachine spawnVM(boolean reloadClasses) { if ((null == classManager) || reloadClasses) { try { classManager = new ClassManagerFactory().build(TEST_CLASS_PATH, getDexBuilder()); } catch (IOException e) { throw new RuntimeException("Exception building class manager for " + TEST_CLASS_PATH, e); } } return new VirtualMachineFactory().build(classManager); }