From 603863403f600f96a6119345354f4cd214ae3ed9 Mon Sep 17 00:00:00 2001 From: Skylot <118523+skylot@users.noreply.github.com> Date: Fri, 13 Sep 2024 21:01:19 +0100 Subject: [PATCH] fix: do not add custom passes for fallback and simple modes (#2276) --- .../src/test/java/jadx/cli/TestInput.java | 96 ++++++++++++------- .../java/jadx/core/dex/nodes/RootNode.java | 7 ++ 2 files changed, 66 insertions(+), 37 deletions(-) diff --git a/jadx-cli/src/test/java/jadx/cli/TestInput.java b/jadx-cli/src/test/java/jadx/cli/TestInput.java index c4d1a4789..2e5e035b5 100644 --- a/jadx-cli/src/test/java/jadx/cli/TestInput.java +++ b/jadx-cli/src/test/java/jadx/cli/TestInput.java @@ -24,6 +24,17 @@ import static org.assertj.core.api.Assertions.assertThat; public class TestInput { private static final Logger LOG = LoggerFactory.getLogger(TestInput.class); + private static final PathMatcher LOG_ALL_FILES = path -> { + LOG.debug("File in result dir: {}", path); + return true; + }; + + @Test + public void testHelp() { + int result = JadxCLI.execute(new String[] { "--help" }); + assertThat(result).isEqualTo(0); + } + @Test public void testDexInput() throws Exception { decompile("dex", "samples/hello.dex"); @@ -45,35 +56,61 @@ public class TestInput { } @Test - public void testResourceOnly() throws Exception { - decode("resourceOnly", "samples/resources-only.apk"); - } - - private void decode(String tmpDirName, String apkSample) throws URISyntaxException, IOException { - List args = new ArrayList<>(); - Path tempDir = FileUtils.createTempDir(tmpDirName); - args.add("-v"); - args.add("-d"); - args.add(tempDir.toAbsolutePath().toString()); - - URL resource = getClass().getClassLoader().getResource(apkSample); - assertThat(resource).isNotNull(); - String sampleFile = resource.toURI().getRawPath(); - args.add(sampleFile); + public void testFallbackMode() throws Exception { + Path tempDir = FileUtils.createTempDir("fallback"); + List args = buildArgs(tempDir, "samples/hello.dex"); + args.add(0, "-f"); int result = JadxCLI.execute(args.toArray(new String[0])); assertThat(result).isEqualTo(0); - List files = Files.find( - tempDir, - 3, - (file, attr) -> file.getFileName().toString().equalsIgnoreCase("AndroidManifest.xml")) - .collect(Collectors.toList()); + List files = collectJavaFilesInDir(tempDir); + assertThat(files).hasSize(1); + } + + @Test + public void testSimpleMode() throws Exception { + Path tempDir = FileUtils.createTempDir("simple"); + List args = buildArgs(tempDir, "samples/hello.dex"); + args.add(0, "--decompilation-mode"); + args.add(1, "simple"); + + int result = JadxCLI.execute(args.toArray(new String[0])); + assertThat(result).isEqualTo(0); + List files = collectJavaFilesInDir(tempDir); + assertThat(files).hasSize(1); + } + + @Test + public void testResourceOnly() throws Exception { + Path tempDir = FileUtils.createTempDir("resourceOnly"); + List args = buildArgs(tempDir, "samples/resources-only.apk"); + + int result = JadxCLI.execute(args.toArray(new String[0])); + assertThat(result).isEqualTo(0); + List files = collectFilesInDir(tempDir, + path -> path.getFileName().toString().equalsIgnoreCase("AndroidManifest.xml")); assertThat(files).isNotEmpty(); } private void decompile(String tmpDirName, String... inputSamples) throws URISyntaxException, IOException { - List args = new ArrayList<>(); Path tempDir = FileUtils.createTempDir(tmpDirName); + List args = buildArgs(tempDir, inputSamples); + + int result = JadxCLI.execute(args.toArray(new String[0])); + assertThat(result).isEqualTo(0); + List resultJavaFiles = collectJavaFilesInDir(tempDir); + assertThat(resultJavaFiles).isNotEmpty(); + + // do not copy input files as resources + for (Path path : collectFilesInDir(tempDir, LOG_ALL_FILES)) { + for (String inputSample : inputSamples) { + assertThat(path.toAbsolutePath().toString()).doesNotContain(inputSample); + } + } + } + + private List buildArgs(Path tempDir, String... inputSamples) throws URISyntaxException { + List args = new ArrayList<>(); args.add("-v"); args.add("-d"); args.add(tempDir.toAbsolutePath().toString()); @@ -84,22 +121,7 @@ public class TestInput { String sampleFile = resource.toURI().getRawPath(); args.add(sampleFile); } - - int result = JadxCLI.execute(args.toArray(new String[0])); - assertThat(result).isEqualTo(0); - List resultJavaFiles = collectJavaFilesInDir(tempDir); - assertThat(resultJavaFiles).isNotEmpty(); - - // do not copy input files as resources - PathMatcher logAllFiles = path -> { - LOG.debug("File in result dir: {}", path); - return true; - }; - for (Path path : collectFilesInDir(tempDir, logAllFiles)) { - for (String inputSample : inputSamples) { - assertThat(path.toAbsolutePath().toString()).doesNotContain(inputSample); - } - } + return args; } private static List collectJavaFilesInDir(Path dir) throws IOException { diff --git a/jadx-core/src/main/java/jadx/core/dex/nodes/RootNode.java b/jadx-core/src/main/java/jadx/core/dex/nodes/RootNode.java index a468f00ad..b371ae443 100644 --- a/jadx-core/src/main/java/jadx/core/dex/nodes/RootNode.java +++ b/jadx-core/src/main/java/jadx/core/dex/nodes/RootNode.java @@ -13,6 +13,7 @@ import org.jetbrains.annotations.Nullable; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import jadx.api.DecompilationMode; import jadx.api.ICodeCache; import jadx.api.ICodeWriter; import jadx.api.JadxArgs; @@ -310,6 +311,12 @@ public class RootNode { } public void mergePasses(Map> customPasses) { + DecompilationMode mode = args.getDecompilationMode(); + if (mode == DecompilationMode.FALLBACK || mode == DecompilationMode.SIMPLE) { + // for predefined modes ignore custom (and plugin) passes + return; + } + new PassMerge(preDecompilePasses) .merge(customPasses.get(JadxPreparePass.TYPE), p -> new PreparePassWrapper((JadxPreparePass) p)); new PassMerge(processClasses.getPasses())