Skip to content

Commit

Permalink
fix: do not add custom passes for fallback and simple modes (#2276)
Browse files Browse the repository at this point in the history
  • Loading branch information
skylot committed Sep 13, 2024
1 parent 889a945 commit 6038634
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 33 deletions.
88 changes: 55 additions & 33 deletions jadx-cli/src/test/java/jadx/cli/TestInput.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,17 @@
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");
Expand All @@ -45,63 +56,74 @@ public void testMultipleInput() throws Exception {
}

@Test
public void testResourceOnly() throws Exception {
decode("resourceOnly", "samples/resources-only.apk");
public void testFallbackMode() throws Exception {
Path tempDir = FileUtils.createTempDir("fallback");
List<String> args = buildArgs(tempDir, "samples/hello.dex");
args.add(0, "-f");

int result = JadxCLI.execute(args.toArray(new String[0]));
assertThat(result).isEqualTo(0);
List<Path> files = collectJavaFilesInDir(tempDir);
assertThat(files).hasSize(1);
}

private void decode(String tmpDirName, String apkSample) throws URISyntaxException, IOException {
List<String> args = new ArrayList<>();
Path tempDir = FileUtils.createTempDir(tmpDirName);
args.add("-v");
args.add("-d");
args.add(tempDir.toAbsolutePath().toString());
@Test
public void testSimpleMode() throws Exception {
Path tempDir = FileUtils.createTempDir("simple");
List<String> args = buildArgs(tempDir, "samples/hello.dex");
args.add(0, "--decompilation-mode");
args.add(1, "simple");

URL resource = getClass().getClassLoader().getResource(apkSample);
assertThat(resource).isNotNull();
String sampleFile = resource.toURI().getRawPath();
args.add(sampleFile);
int result = JadxCLI.execute(args.toArray(new String[0]));
assertThat(result).isEqualTo(0);
List<Path> files = collectJavaFilesInDir(tempDir);
assertThat(files).hasSize(1);
}

@Test
public void testResourceOnly() throws Exception {
Path tempDir = FileUtils.createTempDir("resourceOnly");
List<String> args = buildArgs(tempDir, "samples/resources-only.apk");

int result = JadxCLI.execute(args.toArray(new String[0]));
assertThat(result).isEqualTo(0);
List<Path> files = Files.find(
tempDir,
3,
(file, attr) -> file.getFileName().toString().equalsIgnoreCase("AndroidManifest.xml"))
.collect(Collectors.toList());
List<Path> files = collectFilesInDir(tempDir,
path -> path.getFileName().toString().equalsIgnoreCase("AndroidManifest.xml"));
assertThat(files).isNotEmpty();
}

private void decompile(String tmpDirName, String... inputSamples) throws URISyntaxException, IOException {
List<String> args = new ArrayList<>();
Path tempDir = FileUtils.createTempDir(tmpDirName);
args.add("-v");
args.add("-d");
args.add(tempDir.toAbsolutePath().toString());

for (String inputSample : inputSamples) {
URL resource = getClass().getClassLoader().getResource(inputSample);
assertThat(resource).isNotNull();
String sampleFile = resource.toURI().getRawPath();
args.add(sampleFile);
}
List<String> args = buildArgs(tempDir, inputSamples);

int result = JadxCLI.execute(args.toArray(new String[0]));
assertThat(result).isEqualTo(0);
List<Path> 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 (Path path : collectFilesInDir(tempDir, LOG_ALL_FILES)) {
for (String inputSample : inputSamples) {
assertThat(path.toAbsolutePath().toString()).doesNotContain(inputSample);
}
}
}

private List<String> buildArgs(Path tempDir, String... inputSamples) throws URISyntaxException {
List<String> args = new ArrayList<>();
args.add("-v");
args.add("-d");
args.add(tempDir.toAbsolutePath().toString());

for (String inputSample : inputSamples) {
URL resource = getClass().getClassLoader().getResource(inputSample);
assertThat(resource).isNotNull();
String sampleFile = resource.toURI().getRawPath();
args.add(sampleFile);
}
return args;
}

private static List<Path> collectJavaFilesInDir(Path dir) throws IOException {
PathMatcher javaMatcher = dir.getFileSystem().getPathMatcher("glob:**.java");
return collectFilesInDir(dir, javaMatcher);
Expand Down
7 changes: 7 additions & 0 deletions jadx-core/src/main/java/jadx/core/dex/nodes/RootNode.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import jadx.api.DecompilationMode;
import jadx.api.ICodeCache;
import jadx.api.ICodeWriter;
import jadx.api.JadxArgs;
Expand Down Expand Up @@ -310,6 +311,12 @@ private void initInnerClasses() {
}

public void mergePasses(Map<JadxPassType, List<JadxPass>> 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())
Expand Down

0 comments on commit 6038634

Please sign in to comment.