Compare commits

...

48 Commits

Author SHA1 Message Date
Skylot 87e0e5bf16 fix: correct inline/merge with overriden bridge method (#1580) 2022-07-20 14:49:37 +01:00
Skylot e4c2d6cf6e fix(gui): use editor font for usage label 2022-07-20 14:36:51 +01:00
Skylot fb0bdb5112 fix(gui): allow to use empty name to reset rename 2022-07-20 14:35:53 +01:00
Skylot f4b3645435 fix: ignore anonymous classes in enclosing node search (#1580) 2022-07-19 19:25:17 +01:00
Skylot c27f2badf7 fix(gui): resolve payload offset for switch insns in debug smali code (#1575) 2022-07-18 18:50:48 +01:00
Skylot 1a877d6535 fix: resolve possible decompilation double execution 2022-07-16 22:29:59 +03:00
Skylot 5ada9331b6 chore: update dependencies 2022-07-14 14:33:04 +01:00
Skylot a0f4ccb7a4 fix: update deps and fix proto resource loading (AAB) (#1129) 2022-07-14 14:33:04 +01:00
Skylot 5b5524a7dd fix: handle parent of inlined/moved classes (#1578) 2022-07-14 14:40:13 +03:00
Jan S 3cc464c9c9 fix: IndexOutOfBoundsException in JumpManager (#1576) (PR #1577) 2022-07-13 17:24:20 +01:00
Skylot 51555667cf fix: add more checks before remove or rename enum methods (#1572) 2022-07-07 16:55:32 +01:00
Skylot e01ea7010f fix: save classes with code generation error into cache (#1568) 2022-07-03 19:32:41 +01:00
Skylot 77732c83c9 fix(gui): ignore/limit waiting of canceled search task (#1568) 2022-07-01 17:57:59 +01:00
Skylot a67fc83949 fix: better dominators algorithms 2022-07-01 17:26:54 +01:00
Skylot 3d920725aa fix: check synthetic methods before remove/inline (#1560) 2022-06-29 19:19:54 +01:00
Skylot 2f2fbea558 fix(gui): check user renames (#1557) 2022-06-29 16:21:49 +01:00
Skylot e7a86a2960 fix(gui): forbid rename method args in fallback mode (#1558) 2022-06-29 15:25:23 +01:00
Skylot b282d97ffe fix(gui): set current dir directly in file chooser constructor (#1553) 2022-06-28 16:57:57 +01:00
Skylot e4ca52a95f chore: update dependencies 2022-06-28 16:23:31 +01:00
Skylot d972d9ec74 fix(gui): ignore errors on code area dispose (#1545) 2022-06-28 16:20:31 +01:00
Jan S 0721a6b050 fix(gui): QuarkReport data validation added and other minor improvements (PR #1556)
* QuarkReport: data validation added and other minor improvements
* checkStyle
2022-06-25 22:24:53 +03:00
zhongqingsong 762ee6550e fix(gui):complete Chinese translation (PR #1549)
1、Complete Chinese translation
2、Previous word polish
2022-06-25 22:15:22 +03:00
Skylot 18070eb7a6 fix(gui): allow to select file on mapping export 2022-06-20 14:19:59 +01:00
Skylot 8486891728 fix(gui): resolve various minor issues 2022-06-20 13:17:50 +01:00
Skylot 4679172d4f fix(gui): correct set highlighted text in search (#1507) 2022-06-20 13:16:42 +01:00
Skylot 92a6c333d8 fix(gui): force jadx new version check by default 2022-06-20 12:55:15 +01:00
Skylot 358adbdd65 feat(gui): allow to disable jump on double click (#1540) 2022-06-19 17:19:08 +01:00
Skylot 65f7c80222 feat(gui): add reload and live reload actions (#1537) 2022-06-18 20:20:11 +01:00
Skylot d2e6bb236e fix: use wide move for long/double store/load java opcodes 2022-06-16 16:26:14 +01:00
Skylot eaeb114258 fix: check class name collisions (#1526) 2022-06-15 18:43:33 +01:00
Skylot 1533b7fe6e fix: keep types on duplicate cast remove (#1527) 2022-06-12 21:55:12 +01:00
Julian Burner a2cd8e1ead feat(gui): support export to deobfuscation mapping file formats (#1491)(PR #1505)
* Add option to export mappings as Tiny v2 file

* Comply with JADX's import order conventions

* Only use Java 8 features

* Only use Java 8 features (2)

* Export comments to mappings file

* Method args test (doesn't work)

* Make method arg mapping exports work now

* Use `getTopParentClass()` instead of `getParentClass()`

See https://github.com/skylot/jadx/pull/1505#issuecomment-1145064865

* Remove unneeded method load call

* Small code cleanup; initial (broken) support for method vars

* Fixes regarding inner classes

* Add option to export mappings as Enigma directory

* Add option to export mappings as Enigma file/directory

Temporarily move to my mapping-io fork until this PR gets merged: https://github.com/FabricMC/mapping-io/pull/19

* Fix method vars' lv-indices

* Use correct offset value for method var mappings

* Also supply lvt-index for method var mappings

* Clarify why we're using local mapping-io fork; comment out Fabric Maven for now

* Remove unnecessary `public` modifier

* Make an `if` condition less complicated

* Move mapping export code into jadx-gui (for now)

* Make mapping export async; make export menu only clickable when everything is loaded

* Fix export mappings menu field declaration position
2022-06-11 20:19:08 +01:00
Christian Jones 4edb512121 fix(cli): allow decoding resource-only APKs (#1517)(PR #1530)
* Process resource-only inputs
* Fix error, add testing
2022-06-11 15:40:39 +01:00
Skylot 702b88228c fix(gui): resolve popup menu action run (#1514, #1529) 2022-06-11 15:08:28 +01:00
Skylot 14fd88b2f8 fix(gui): add volatile and update sync for decompiler field in wrapper (#1518) 2022-06-08 21:06:57 +01:00
Skylot 20657e8bb5 doc(cli): improve plugins section formatting 2022-06-06 19:55:58 +01:00
Skylot 93d3194e3b doc: remove incorrect tokei badge 2022-06-06 19:54:15 +01:00
Skylot 39331d9120 fix: remove deprecated --deobf-rewrite-cfg (#1513) 2022-06-06 19:53:47 +01:00
Skylot b4fa6644bc doc: add more badges 2022-06-05 16:06:19 +01:00
Skylot 0b2e2ed034 fix: improve class search for super call (#1512) 2022-06-05 14:49:34 +01:00
Skylot 81231206f3 fix(gui): reset disk cache on new jadx version 2022-06-04 23:26:25 +01:00
Skylot 49d0e76272 fix: support all-catch in multi-catch (#1510) 2022-06-04 23:25:52 +01:00
CmP-lt 0809993b37 fix(gui): improve restoration of windows saved state (PR #1511)
* Fix restoration of windows saved state
* Don't skip restoration of window saved bounds when they intersect with screen bounds.
* Restore saved bounds of main window regardless of it's saved extended state (fixes divider location of main split pane being restored incorrectly when saved state of main window is maximized).
* Add handling for out-of-screen(s) window bounds
2022-06-04 17:41:00 +01:00
Skylot 0c3afcc24c fix(gui): try to prevent jadx node leaks in UI objects 2022-06-03 16:15:14 +01:00
Skylot d6c851eed4 test: fix method code extract 2022-06-02 19:33:16 +01:00
Skylot dcf4a7c4e3 fix(gui): try to resolve some causes of memory leak 2022-06-01 19:48:51 +01:00
Skylot 9ba07b986b fix(gui): reduce usage of nullable decompiler field in jadx wrapper (#1506) 2022-06-01 16:36:30 +01:00
Skylot e6b6b93cbb fix: improve blocks tree compare for finally extract (#1501) 2022-05-31 20:57:34 +01:00
139 changed files with 3064 additions and 769 deletions
+18 -13
View File
@@ -3,8 +3,10 @@
## JADX
[![Build status](https://github.com/skylot/jadx/workflows/Build/badge.svg)](https://github.com/skylot/jadx/actions?query=workflow%3ABuild)
[![Alerts from lgtm.com](https://img.shields.io/lgtm/alerts/g/skylot/jadx.svg?logo=lgtm&logoWidth=18)](https://lgtm.com/projects/g/skylot/jadx/alerts/)
[![semantic-release](https://img.shields.io/badge/%20%20%F0%9F%93%A6%F0%9F%9A%80-semantic--release-e10079.svg)](https://github.com/semantic-release/semantic-release)
![GitHub contributors](https://img.shields.io/github/contributors/skylot/jadx)
![GitHub all releases](https://img.shields.io/github/downloads/skylot/jadx/total)
![GitHub release (latest by SemVer)](https://img.shields.io/github/downloads/skylot/jadx/latest/total)
![Latest release](https://img.shields.io/github/release/skylot/jadx.svg)
[![Maven Central](https://img.shields.io/maven-central/v/io.github.skylot/jadx-core)](https://search.maven.org/search?q=g:io.github.skylot%20AND%20jadx)
[![License](http://img.shields.io/:license-apache-blue.svg)](http://www.apache.org/licenses/LICENSE-2.0.html)
@@ -35,7 +37,7 @@ See these features in action here: [jadx-gui features overview](https://github.c
### Download
- release
from [github: ![Latest release](https://img.shields.io/github/release/skylot/jadx.svg)](https://github.com/skylot/jadx/releases/latest)
- latest [unstable build](https://nightly.link/skylot/jadx/workflows/build-artifacts/master)
- latest [unstable build ![GitHub commits since tagged version (branch)](https://img.shields.io/github/commits-since/skylot/jadx/latest/master)](https://nightly.link/skylot/jadx/workflows/build-artifacts/master)
After download unpack zip file go to `bin` directory and run:
- `jadx` - command line version
@@ -46,14 +48,18 @@ On Windows run `.bat` files with double-click\
For Windows, you can download it from [oracle.com](https://www.oracle.com/java/technologies/downloads/#jdk17-windows) (select x64 Installer).
### Install
1. Arch linux
1. Arch linux ![Arch Linux package](https://img.shields.io/archlinux/v/community/any/jadx?label=)
```bash
sudo pacman -S jadx
sudo pacman -S jadx
```
2. macOS
2. macOS ![homebrew version](https://img.shields.io/homebrew/v/jadx?label=)
```bash
brew install jadx
brew install jadx
```
3. [Flathub ![Flathub](https://img.shields.io/flathub/v/com.github.skylot.jadx?label=)](https://flathub.org/apps/details/com.github.skylot.jadx)
```bash
flatpak install flathub com.github.skylot.jadx
```
### Use jadx as a library
You can use jadx in your java projects, check details on [wiki page](https://github.com/skylot/jadx/wiki/Use-jadx-as-a-library)
@@ -108,7 +114,6 @@ options:
'read-or-save' - read if found, save otherwise (don't overwrite)
'overwrite' - don't read, always save
'ignore' - don't read and don't save
--deobf-rewrite-cfg - set '--deobf-cfg-file-mode' to 'overwrite' (deprecated)
--deobf-use-sourcename - use source file name as class name alias
--deobf-parse-kotlin-metadata - parse kotlin metadata to class and package names
--use-kotlin-methods-for-var-names - use kotlin intrinsic methods to rename variables, values: disable, apply, apply-and-hide, default: apply
@@ -131,11 +136,11 @@ options:
-h, --help - print this help
Plugin options (-P<name>=<value>):
1) dex-input (Load .dex and .apk files)
-Pdex-input.verify-checksum - Verify dex file checksum before load, values: [yes, no], default: yes
2) java-convert (Convert .jar and .class files to dex)
-Pjava-convert.mode - Convert mode, values: [dx, d8, both], default: both
-Pjava-convert.d8-desugar - Use desugar in d8, values: [yes, no], default: no
1) dex-input: Load .dex and .apk files
- dex-input.verify-checksum - verify dex file checksum before load, values: [yes, no], default: yes
2) java-convert: Convert .class, .jar and .aar files to dex
- java-convert.mode - convert mode, values: [dx, d8, both], default: both
- java-convert.d8-desugar - use desugar in d8, values: [yes, no], default: no
Examples:
jadx -d out classes.dex
+8 -3
View File
@@ -1,6 +1,6 @@
plugins {
id 'com.github.ben-manes.versions' version '0.42.0'
id 'com.diffplug.spotless' version '6.6.1'
id 'com.diffplug.spotless' version '6.8.0'
}
ext.jadxVersion = System.getenv('JADX_VERSION') ?: "dev"
@@ -31,8 +31,8 @@ allprojects {
testImplementation 'ch.qos.logback:logback-classic:1.2.11'
testImplementation 'org.hamcrest:hamcrest-library:2.2'
testImplementation 'org.mockito:mockito-core:4.6.0'
testImplementation 'org.assertj:assertj-core:3.22.0'
testImplementation 'org.mockito:mockito-core:4.6.1'
testImplementation 'org.assertj:assertj-core:3.23.1'
testImplementation 'org.junit.jupiter:junit-jupiter-api:5.8.2'
testRuntimeOnly 'org.junit.jupiter:junit-jupiter-engine:5.8.2'
@@ -49,6 +49,11 @@ allprojects {
mavenLocal()
mavenCentral()
google()
// Commented out for now since we're using a local mapping-io fork atm.
// maven {
// name 'FabricMC'
// url 'https://maven.fabricmc.net/'
// }
}
}
@@ -193,11 +193,11 @@ public class JCommanderWrapper<T> {
return false;
}
JadxPluginInfo pluginInfo = plugin.getPluginInfo();
out.append("\n ").append(k).append(") ");
out.append(pluginInfo.getPluginId()).append(" (").append(pluginInfo.getDescription()).append(") ");
out.append("\n ").append(k).append(") ");
out.append(pluginInfo.getPluginId()).append(": ").append(pluginInfo.getDescription());
for (OptionDescription desc : descs) {
StringBuilder opt = new StringBuilder();
opt.append(" -P").append(desc.name());
opt.append(" - ").append(desc.name());
addSpaces(opt, maxNamesLen - opt.length());
opt.append("- ").append(desc.description());
if (!desc.values().isEmpty()) {
+8 -2
View File
@@ -66,8 +66,14 @@ public class JadxCLI {
private static boolean checkForErrors(JadxDecompiler jadx) {
if (jadx.getRoot().getClasses().isEmpty()) {
LOG.error("Load failed! No classes for decompile!");
return true;
if (jadx.getArgs().isSkipResources()) {
LOG.error("Load failed! No classes for decompile!");
return true;
}
if (!jadx.getArgs().isSkipSources()) {
LOG.warn("No classes to decompile; decoding resources only");
jadx.getArgs().setSkipSources(true);
}
}
if (jadx.getErrorsCount() > 0) {
LOG.error("Load with errors! Check log for details");
@@ -123,9 +123,6 @@ public class JadxCLIArgs {
)
protected DeobfuscationMapFileMode deobfuscationMapFileMode = DeobfuscationMapFileMode.READ;
@Parameter(names = { "--deobf-rewrite-cfg" }, description = "set '--deobf-cfg-file-mode' to 'overwrite' (deprecated)")
protected boolean deobfuscationForceSave = false;
@Parameter(names = { "--deobf-use-sourcename" }, description = "use source file name as class name alias")
protected boolean deobfuscationUseSourceNameAsAlias = false;
@@ -259,11 +256,7 @@ public class JadxCLIArgs {
args.setReplaceConsts(replaceConsts);
args.setDeobfuscationOn(deobfuscationOn);
args.setDeobfuscationMapFile(FileUtils.toFile(deobfuscationMapFile));
if (deobfuscationForceSave) {
args.setDeobfuscationMapFileMode(DeobfuscationMapFileMode.OVERWRITE);
} else {
args.setDeobfuscationMapFileMode(deobfuscationMapFileMode);
}
args.setDeobfuscationMapFileMode(deobfuscationMapFileMode);
args.setDeobfuscationMinLength(deobfuscationMinLength);
args.setDeobfuscationMaxLength(deobfuscationMaxLength);
args.setUseSourceNameAsClassAlias(deobfuscationUseSourceNameAsAlias);
@@ -377,10 +370,6 @@ public class JadxCLIArgs {
return deobfuscationMapFileMode;
}
public boolean isDeobfuscationForceSave() {
return deobfuscationForceSave;
}
public boolean isDeobfuscationUseSourceNameAsAlias() {
return deobfuscationUseSourceNameAsAlias;
}
@@ -44,6 +44,33 @@ public class TestInput {
decompile("multi", "samples/hello.dex", "samples/HelloWorld.smali");
}
@Test
public void testResourceOnly() throws Exception {
decode("resourceOnly", "samples/resources-only.apk");
}
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());
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 = Files.find(
tempDir,
3,
(file, attr) -> file.getFileName().toString().equalsIgnoreCase("AndroidManifest.xml"))
.collect(Collectors.toList());
assertThat(files.isEmpty()).isFalse();
}
private void decompile(String tmpDirName, String... inputSamples) throws URISyntaxException, IOException {
List<String> args = new ArrayList<>();
Path tempDir = FileUtils.createTempDir(tmpDirName);
+5 -6
View File
@@ -6,11 +6,10 @@ dependencies {
api(project(':jadx-plugins:jadx-plugins-api'))
implementation 'com.google.code.gson:gson:2.9.0'
implementation 'com.android.tools.build:aapt2-proto:4.2.1-7147631'
constraints {
// Force protobuf version to prevent Java-7 issue
implementation 'com.google.protobuf:protobuf-java:3.11.4'
}
// TODO: move resources decoding to separate plugin module
implementation 'com.android.tools.build:aapt2-proto:7.2.1-7984345'
implementation 'com.google.protobuf:protobuf-java:3.21.2' // forcing latest version
testImplementation 'org.apache.commons:commons-lang3:3.12.0'
@@ -20,7 +19,7 @@ dependencies {
testRuntimeOnly(project(':jadx-plugins:jadx-java-input'))
testRuntimeOnly(project(':jadx-plugins:jadx-raung-input'))
testImplementation 'org.eclipse.jdt:ecj:3.29.0'
testImplementation 'org.eclipse.jdt:ecj:3.30.0'
testImplementation 'tools.profiler:async-profiler:1.8.3'
}
@@ -128,7 +128,7 @@ public class JadxArgs {
public void close() {
try {
inputFiles.clear();
inputFiles = null;
if (codeCache != null) {
codeCache.close();
}
@@ -37,8 +37,6 @@ import jadx.api.plugins.input.data.ILoadResult;
import jadx.api.plugins.options.JadxPluginOptions;
import jadx.core.Jadx;
import jadx.core.dex.attributes.AFlag;
import jadx.core.dex.attributes.AType;
import jadx.core.dex.attributes.nodes.InlinedAttr;
import jadx.core.dex.nodes.ClassNode;
import jadx.core.dex.nodes.FieldNode;
import jadx.core.dex.nodes.MethodNode;
@@ -358,8 +356,9 @@ public final class JadxDecompiler implements Closeable {
tasks.add(() -> {
for (JavaClass cls : decompileBatch) {
try {
ICodeInfo code = cls.getCodeInfo();
SaveCode.save(outDir, cls.getClassNode(), code);
ClassNode clsNode = cls.getClassNode();
ICodeInfo code = clsNode.getCode();
SaveCode.save(outDir, clsNode, code);
} catch (Exception e) {
LOG.error("Error saving class: {}", cls, e);
}
@@ -496,25 +495,11 @@ public final class JadxDecompiler implements Closeable {
@ApiStatus.Internal
JavaMethod convertMethodNode(MethodNode method) {
return methodsMap.computeIfAbsent(method, mthNode -> {
ClassNode codeCls = getCodeParentClass(mthNode.getParentClass());
return new JavaMethod(convertClassNode(codeCls), mthNode);
ClassNode parentCls = mthNode.getParentClass();
return new JavaMethod(convertClassNode(parentCls), mthNode);
});
}
private static ClassNode getCodeParentClass(ClassNode cls) {
ClassNode codeCls;
InlinedAttr inlinedAttr = cls.get(AType.INLINED);
if (inlinedAttr != null) {
codeCls = inlinedAttr.getInlineCls().getTopParentClass();
} else {
codeCls = cls.getTopParentClass();
}
if (codeCls == cls) {
return codeCls;
}
return getCodeParentClass(codeCls);
}
@Nullable
public JavaClass searchJavaClassByOrigFullName(String fullName) {
return getRoot().getClasses().stream()
+47 -19
View File
@@ -18,6 +18,7 @@ import jadx.api.metadata.ICodeNodeRef;
import jadx.core.dex.attributes.AFlag;
import jadx.core.dex.attributes.AType;
import jadx.core.dex.attributes.nodes.AnonymousClassAttr;
import jadx.core.dex.attributes.nodes.InlinedAttr;
import jadx.core.dex.info.AccessInfo;
import jadx.core.dex.nodes.ClassNode;
import jadx.core.dex.nodes.FieldNode;
@@ -57,7 +58,10 @@ public final class JavaClass implements JavaNode {
}
public @NotNull ICodeInfo getCodeInfo() {
load();
ICodeInfo code = load();
if (code != null) {
return code;
}
return cls.decompile();
}
@@ -106,19 +110,24 @@ public final class JavaClass implements JavaNode {
/**
* Decompile class and loads internal lists of fields, methods, etc.
* Do nothing if already loaded.
*
* @return code info if decompilation was executed, null otherwise
*/
@Nullable
private synchronized void load() {
private synchronized @Nullable ICodeInfo load() {
if (listsLoaded) {
return;
return null;
}
listsLoaded = true;
JadxDecompiler rootDecompiler = getRootDecompiler();
ICodeCache codeCache = rootDecompiler.getArgs().getCodeCache();
if (!codeCache.contains(cls.getRawName())) {
cls.decompile();
ICodeInfo code;
if (cls.getState().isProcessComplete()) {
// already decompiled -> class internals loaded
code = null;
} else {
code = cls.decompile();
}
JadxDecompiler rootDecompiler = getRootDecompiler();
int inClsCount = cls.getInnerClasses().size();
if (inClsCount != 0) {
List<JavaClass> list = new ArrayList<>(inClsCount);
@@ -164,6 +173,7 @@ public final class JavaClass implements JavaNode {
mths.sort(Comparator.comparing(JavaMethod::getName));
this.methods = Collections.unmodifiableList(mths);
}
return code;
}
JadxDecompiler getRootDecompiler() {
@@ -242,19 +252,37 @@ public final class JavaClass implements JavaNode {
return parent;
}
@Override
public JavaClass getTopParentClass() {
if (cls.contains(AType.ANONYMOUS_CLASS)) {
// moved to usage class
return getParentForAnonymousClass();
}
return parent == null ? this : parent.getTopParentClass();
public JavaClass getOriginalTopParentClass() {
return parent == null ? this : parent.getOriginalTopParentClass();
}
private JavaClass getParentForAnonymousClass() {
AnonymousClassAttr attr = cls.get(AType.ANONYMOUS_CLASS);
ClassNode topParentClass = attr.getOuterCls().getTopParentClass();
return getRootDecompiler().convertClassNode(topParentClass);
/**
* Return top parent class which contains code of this class.
* Code parent can be different from original parent after move or inline
*
* @return this if already a top class
*/
@Override
public JavaClass getTopParentClass() {
JavaClass codeParent = getCodeParent();
return codeParent == null ? this : codeParent.getTopParentClass();
}
/**
* Return parent class which contains code of this class.
* Code parent can be different for original parent after move or inline
*/
public @Nullable JavaClass getCodeParent() {
AnonymousClassAttr anonymousClsAttr = cls.get(AType.ANONYMOUS_CLASS);
if (anonymousClsAttr != null) {
// moved to usage class
return getRootDecompiler().convertClassNode(anonymousClsAttr.getOuterCls());
}
InlinedAttr inlinedAttr = cls.get(AType.INLINED);
if (inlinedAttr != null) {
return getRootDecompiler().convertClassNode(inlinedAttr.getInlineCls());
}
return parent;
}
public AccessInfo getAccessInfo() {
@@ -4,6 +4,7 @@ import java.util.Collections;
import java.util.List;
import org.jetbrains.annotations.ApiStatus;
import org.jetbrains.annotations.Nullable;
import jadx.api.metadata.ICodeAnnotation;
import jadx.api.metadata.annotations.VarNode;
@@ -32,7 +33,7 @@ public class JavaVariable implements JavaNode {
}
@Override
public String getName() {
public @Nullable String getName() {
return varNode.getName();
}
@@ -9,7 +9,8 @@ public interface ICodeAnnotation {
VAR,
VAR_REF,
DECLARATION,
OFFSET
OFFSET,
END // class or method body end
}
AnnType getAnnType();
@@ -32,6 +32,22 @@ public class NodeDeclareRef implements ICodeAnnotation {
return AnnType.DECLARATION;
}
@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (!(o instanceof NodeDeclareRef)) {
return false;
}
return node.equals(((NodeDeclareRef) o).node);
}
@Override
public int hashCode() {
return node.hashCode();
}
@Override
public String toString() {
return "NodeDeclareRef{" + node + '}';
@@ -0,0 +1,21 @@
package jadx.api.metadata.annotations;
import jadx.api.metadata.ICodeAnnotation;
public class NodeEnd implements ICodeAnnotation {
public static final NodeEnd VALUE = new NodeEnd();
private NodeEnd() {
}
@Override
public AnnType getAnnType() {
return AnnType.END;
}
@Override
public String toString() {
return "END";
}
}
@@ -6,16 +6,14 @@ import java.util.Map;
import java.util.NavigableMap;
import java.util.TreeMap;
import java.util.function.BiFunction;
import java.util.stream.Stream;
import org.jetbrains.annotations.Nullable;
import jadx.api.metadata.ICodeAnnotation;
import jadx.api.metadata.ICodeAnnotation.AnnType;
import jadx.api.metadata.ICodeMetadata;
import jadx.api.metadata.ICodeNodeRef;
import jadx.api.metadata.annotations.NodeDeclareRef;
import jadx.core.dex.nodes.ClassNode;
import jadx.core.dex.nodes.MethodNode;
import jadx.core.utils.Utils;
public class CodeMetadataStorage implements ICodeMetadata {
@@ -55,7 +53,7 @@ public class CodeMetadataStorage implements ICodeMetadata {
}
@Override
public @Nullable ICodeAnnotation searchUp(int position, ICodeAnnotation.AnnType annType) {
public @Nullable ICodeAnnotation searchUp(int position, AnnType annType) {
for (ICodeAnnotation v : navMap.tailMap(position, true).values()) {
if (v.getAnnType() == annType) {
return v;
@@ -65,7 +63,7 @@ public class CodeMetadataStorage implements ICodeMetadata {
}
@Override
public @Nullable ICodeAnnotation searchUp(int position, int limitPos, ICodeAnnotation.AnnType annType) {
public @Nullable ICodeAnnotation searchUp(int position, int limitPos, AnnType annType) {
for (ICodeAnnotation v : navMap.subMap(position, true, limitPos, true).values()) {
if (v.getAnnType() == annType) {
return v;
@@ -99,28 +97,40 @@ public class CodeMetadataStorage implements ICodeMetadata {
@Override
public ICodeNodeRef getNodeAt(int position) {
return navMap.tailMap(position, true)
.values().stream()
.flatMap(CodeMetadataStorage::mapEnclosingNode)
.findFirst().orElse(null);
int nesting = 0;
for (ICodeAnnotation ann : navMap.tailMap(position, true).values()) {
switch (ann.getAnnType()) {
case END:
nesting++;
break;
case DECLARATION:
ICodeNodeRef node = ((NodeDeclareRef) ann).getNode();
AnnType nodeType = node.getAnnType();
if (nodeType == AnnType.CLASS || nodeType == AnnType.METHOD) {
if (nesting == 0) {
return node;
}
nesting--;
}
break;
}
}
return null;
}
@Override
public ICodeNodeRef getNodeBelow(int position) {
return navMap.headMap(position, true).descendingMap()
.values().stream()
.flatMap(CodeMetadataStorage::mapEnclosingNode)
.findFirst().orElse(null);
}
private static Stream<ICodeNodeRef> mapEnclosingNode(ICodeAnnotation ann) {
if (ann instanceof NodeDeclareRef) {
ICodeNodeRef node = ((NodeDeclareRef) ann).getNode();
if (node instanceof ClassNode || node instanceof MethodNode) {
return Stream.of(node);
for (ICodeAnnotation ann : navMap.headMap(position, true).descendingMap().values()) {
if (ann.getAnnType() == AnnType.DECLARATION) {
ICodeNodeRef node = ((NodeDeclareRef) ann).getNode();
AnnType nodeType = node.getAnnType();
if (nodeType == AnnType.CLASS || nodeType == AnnType.METHOD) {
return node;
}
}
}
return Stream.empty();
return null;
}
@Override
@@ -135,7 +145,7 @@ public class CodeMetadataStorage implements ICodeMetadata {
@Override
public String toString() {
return "CodeMetadata{lines=" + lines
+ ", annotations=\n" + Utils.listToString(navMap.entrySet(), "\n") + "\n}";
return "CodeMetadata{\nlines=" + lines
+ "\nannotations=\n " + Utils.listToString(navMap.descendingMap().entrySet(), "\n ") + "\n}";
}
}
@@ -7,6 +7,7 @@ public class Consts {
public static final boolean DEBUG_TYPE_INFERENCE = false;
public static final boolean DEBUG_OVERLOADED_CASTS = false;
public static final boolean DEBUG_EXC_HANDLERS = false;
public static final boolean DEBUG_FINALLY = false;
public static final String CLASS_OBJECT = "java.lang.Object";
public static final String CLASS_STRING = "java.lang.String";
+10 -3
View File
@@ -238,9 +238,17 @@ public class Jadx {
private static String version;
public static String getVersion() {
if (version != null) {
return version;
if (version == null) {
version = searchJadxVersion();
}
return version;
}
public static boolean isDevVersion() {
return getVersion().equals(VERSION_DEV);
}
private static String searchJadxVersion() {
try {
ClassLoader classLoader = Jadx.class.getClassLoader();
if (classLoader != null) {
@@ -250,7 +258,6 @@ public class Jadx {
Manifest manifest = new Manifest(is);
String ver = manifest.getMainAttributes().getValue("jadx-version");
if (ver != null) {
version = ver;
return ver;
}
}
@@ -203,6 +203,9 @@ public class ClspGraph {
if (isNew) {
addSuperTypes(parentCls, result);
}
} else {
// parent type is unknown
result.add(parentType.getObject());
}
}
}
@@ -19,6 +19,7 @@ import jadx.api.CommentsLevel;
import jadx.api.ICodeInfo;
import jadx.api.ICodeWriter;
import jadx.api.JadxArgs;
import jadx.api.metadata.annotations.NodeEnd;
import jadx.api.plugins.input.data.AccessFlags;
import jadx.api.plugins.input.data.annotations.EncodedType;
import jadx.api.plugins.input.data.annotations.EncodedValue;
@@ -256,6 +257,7 @@ public class ClassGen {
addInnerClsAndMethods(clsCode);
clsCode.decIndent();
clsCode.startLine('}');
clsCode.attachAnnotation(NodeEnd.VALUE);
}
private void addInnerClsAndMethods(ICodeWriter clsCode) {
@@ -369,6 +371,7 @@ public class ClassGen {
mthGen.addInstructions(code);
code.decIndent();
code.startLine('}');
code.attachAnnotation(NodeEnd.VALUE);
}
}
@@ -614,21 +617,23 @@ public class ClassGen {
if (useCls.equals(extClsInfo)) {
return shortName;
}
if (extClsInfo.getPackage().equals("java.lang") && extClsInfo.getParentClass() == null) {
return shortName;
}
if (isClassInnerFor(useCls, extClsInfo)) {
return shortName;
}
if (extClsInfo.isInner()) {
return expandInnerClassName(useCls, extClsInfo);
}
if (searchCollision(cls.root(), useCls, extClsInfo)) {
if (checkInnerCollision(cls.root(), useCls, extClsInfo)
|| checkInPackageCollision(cls.root(), useCls, extClsInfo)) {
return fullName;
}
if (isBothClassesInOneTopClass(useCls, extClsInfo)) {
return shortName;
}
// don't add import for top classes from 'java.lang' package (subpackages excluded)
if (extClsInfo.getPackage().equals("java.lang") && extClsInfo.getParentClass() == null) {
return shortName;
}
// don't add import if this class from same package
if (extClsInfo.getPackage().equals(useCls.getPackage()) && !extClsInfo.isInner()) {
return shortName;
@@ -709,7 +714,7 @@ public class ClassGen {
return false;
}
private static boolean searchCollision(RootNode root, ClassInfo useCls, ClassInfo searchCls) {
private static boolean checkInnerCollision(RootNode root, @Nullable ClassInfo useCls, ClassInfo searchCls) {
if (useCls == null) {
return false;
}
@@ -726,7 +731,20 @@ public class ClassGen {
}
}
}
return searchCollision(root, useCls.getParentClass(), searchCls);
return checkInnerCollision(root, useCls.getParentClass(), searchCls);
}
/**
* Check if class with same name exists in current package
*/
private static boolean checkInPackageCollision(RootNode root, ClassInfo useCls, ClassInfo searchCls) {
String currentPkg = useCls.getAliasPkg();
if (currentPkg.equals(searchCls.getAliasPkg())) {
// search class already from current package
return false;
}
String shortName = searchCls.getAliasShortName();
return root.getClsp().isClsKnown(currentPkg + '.' + shortName);
}
private void insertRenameInfo(ICodeWriter code, ClassNode cls) {
@@ -761,6 +761,7 @@ public class InsnGen {
ctor.add(AFlag.DONT_GENERATE);
}
}
code.attachDefinition(cls);
code.add("new ");
useClass(code, parent);
MethodNode callMth = mth.root().resolveMethod(insn.getCallMth());
@@ -807,14 +808,9 @@ public class InsnGen {
break;
case SUPER:
ClassInfo superCallCls = getClassForSuperCall(code, callMth);
if (superCallCls != null) {
useClass(code, superCallCls);
code.add('.');
}
// use 'super' instead 'this' in 0 arg
code.add("super").add('.');
k++;
callSuper(code, callMth);
k++; // use 'super' instead 'this' in 0 arg
code.add('.');
break;
case STATIC:
@@ -828,7 +824,9 @@ public class InsnGen {
}
if (callMthNode != null) {
code.attachAnnotation(callMthNode);
code.add(callMthNode.getAlias());
}
if (insn.contains(AFlag.FORCE_RAW_NAME)) {
code.add(callMth.getName());
} else {
code.add(callMth.getAlias());
}
@@ -965,34 +963,43 @@ public class InsnGen {
code.startLine('}');
}
@Nullable
private ClassInfo getClassForSuperCall(ICodeWriter code, MethodInfo callMth) {
ClassNode useCls = mth.getParentClass();
ClassInfo insnCls = useCls.getClassInfo();
ClassInfo declClass = callMth.getDeclClass();
if (insnCls.equals(declClass)) {
return null;
private void callSuper(ICodeWriter code, MethodInfo callMth) {
ClassInfo superCallCls = getClassForSuperCall(callMth);
if (superCallCls == null) {
// unknown class, add comment to keep that info
code.add("super/*").add(callMth.getDeclClass().getFullName()).add("*/");
return;
}
ClassNode topClass = useCls.getTopParentClass();
if (topClass.getClassInfo().equals(declClass)) {
return declClass;
ClassInfo curClass = mth.getParentClass().getClassInfo();
if (superCallCls.equals(curClass)) {
code.add("super");
return;
}
// search call class
ClassNode nextParent = useCls;
do {
ClassInfo nextClsInfo = nextParent.getClassInfo();
if (nextClsInfo.equals(declClass)
|| ArgType.isInstanceOf(mth.root(), nextClsInfo.getType(), declClass.getType())) {
if (nextParent == useCls) {
return null;
}
return nextClsInfo;
}
nextParent = nextParent.getParentClass();
} while (nextParent != null && nextParent != topClass);
// use custom class
useClass(code, superCallCls);
code.add(".super");
}
// search failed, just return parent class
return useCls.getParentClass().getClassInfo();
/**
* Search call class in super types of this
* and all parent classes (needed for inlined synthetic calls)
*/
@Nullable
private ClassInfo getClassForSuperCall(MethodInfo callMth) {
ArgType declClsType = callMth.getDeclClass().getType();
ClassNode parentNode = mth.getParentClass();
while (true) {
ClassInfo parentCls = parentNode.getClassInfo();
if (ArgType.isInstanceOf(root, parentCls.getType(), declClsType)) {
return parentCls;
}
ClassNode nextParent = parentNode.getParentClass();
if (nextParent == parentNode) {
// no parent, class not found
return null;
}
parentNode = nextParent;
}
}
void generateMethodArguments(ICodeWriter code, BaseInvokeNode insn, int startArgNum,
@@ -26,6 +26,7 @@ import jadx.core.dex.attributes.AType;
import jadx.core.dex.attributes.nodes.JadxError;
import jadx.core.dex.attributes.nodes.JumpInfo;
import jadx.core.dex.attributes.nodes.MethodOverrideAttr;
import jadx.core.dex.attributes.nodes.MethodReplaceAttr;
import jadx.core.dex.info.AccessInfo;
import jadx.core.dex.instructions.ConstStringNode;
import jadx.core.dex.instructions.IfNode;
@@ -144,8 +145,9 @@ public class MethodGen {
} else {
classGen.useType(code, mth.getReturnType());
code.add(' ');
code.attachDefinition(mth);
code.add(mth.getAlias());
MethodNode defMth = getMethodForDefinition();
code.attachDefinition(defMth);
code.add(defMth.getAlias());
}
code.add('(');
@@ -178,6 +180,14 @@ public class MethodGen {
return true;
}
private MethodNode getMethodForDefinition() {
MethodReplaceAttr replaceAttr = mth.get(AType.METHOD_REPLACE);
if (replaceAttr != null) {
return replaceAttr.getReplaceMth();
}
return mth;
}
private void addOverrideAnnotation(ICodeWriter code, MethodNode mth) {
MethodOverrideAttr overrideAttr = mth.get(AType.METHOD_OVERRIDE);
if (overrideAttr == null) {
@@ -25,6 +25,8 @@ public enum AFlag {
HIDDEN, // instruction used inside other instruction but not listed in args
DONT_RENAME, // do not rename during deobfuscation
FORCE_RAW_NAME, // force use of raw name instead alias
ADDED_TO_REGION,
EXC_TOP_SPLITTER,
@@ -180,12 +180,12 @@ public final class ClassInfo implements Comparable<ClassInfo> {
return makeFullClsName(pkg, name, parentClass, false, true);
}
private String makeAliasFullName() {
public String makeAliasFullName() {
return makeFullClsName(getAliasPkg(), getAliasShortName(), parentClass, true, false);
}
private String makeAliasRawFullName() {
return makeFullClsName(pkg, name, parentClass, true, true);
public String makeAliasRawFullName() {
return makeFullClsName(getAliasPkg(), getAliasShortName(), parentClass, true, true);
}
public String getAliasFullPath() {
@@ -170,6 +170,10 @@ public final class BlockNode extends AttrNode implements IBlock, Comparable<Bloc
return contains(AFlag.RETURN);
}
public boolean isEmpty() {
return instructions.isEmpty();
}
@Override
public int hashCode() {
return startOffset;
@@ -21,6 +21,7 @@ import jadx.api.ICodeCache;
import jadx.api.ICodeInfo;
import jadx.api.ICodeWriter;
import jadx.api.JadxArgs;
import jadx.api.impl.SimpleCodeInfo;
import jadx.api.plugins.input.data.IClassData;
import jadx.api.plugins.input.data.IFieldData;
import jadx.api.plugins.input.data.IMethodData;
@@ -378,7 +379,13 @@ public class ClassNode extends NotificationAttrNode implements ILoadable, ICodeN
return code;
}
}
ICodeInfo codeInfo = root.getProcessClasses().generateCode(this);
ICodeInfo codeInfo;
try {
codeInfo = root.getProcessClasses().generateCode(this);
} catch (Throwable e) {
addError("Code generation failed", e);
codeInfo = new SimpleCodeInfo(Utils.getStackTrace(e));
}
if (codeInfo != ICodeInfo.EMPTY) {
codeCache.add(clsRawName, codeInfo);
}
@@ -316,6 +316,10 @@ public class MethodNode extends NotificationAttrNode implements IMethodDetails,
return blocks;
}
public void setBasicBlocks(List<BlockNode> blocks) {
this.blocks = blocks;
}
public BlockNode getEnterBlock() {
return enterBlock;
}
@@ -461,6 +465,10 @@ public class MethodNode extends NotificationAttrNode implements IMethodDetails,
return regsCount;
}
public int getArgsStartReg() {
return argsStartReg;
}
public SSAVar makeNewSVar(@NotNull RegisterArg assignArg) {
int regNum = assignArg.getRegNum();
return makeNewSVar(regNum, getNextSVarVersion(regNum), assignArg);
@@ -42,7 +42,8 @@ import jadx.core.utils.StringUtils;
import jadx.core.utils.Utils;
import jadx.core.utils.android.AndroidResourcesUtils;
import jadx.core.utils.exceptions.JadxRuntimeException;
import jadx.core.xmlgen.ResTableParser;
import jadx.core.xmlgen.IResParser;
import jadx.core.xmlgen.ResDecoder;
import jadx.core.xmlgen.ResourceStorage;
import jadx.core.xmlgen.entry.ResourceEntry;
import jadx.core.xmlgen.entry.ValuesParser;
@@ -163,23 +164,13 @@ public class RootNode {
}
public void loadResources(List<ResourceFile> resources) {
ResourceFile arsc = null;
for (ResourceFile rf : resources) {
if (rf.getType() == ResourceType.ARSC) {
arsc = rf;
break;
}
}
ResourceFile arsc = getResourceFile(resources);
if (arsc == null) {
LOG.debug("'.arsc' file not found");
return;
}
try {
ResTableParser parser = ResourcesLoader.decodeStream(arsc, (size, is) -> {
ResTableParser tableParser = new ResTableParser(this);
tableParser.decode(is);
return tableParser;
});
IResParser parser = ResourcesLoader.decodeStream(arsc, (size, is) -> ResDecoder.decode(this, arsc, is));
if (parser != null) {
processResources(parser.getResStorage());
updateObfuscatedFiles(parser, resources);
@@ -189,6 +180,15 @@ public class RootNode {
}
}
private @Nullable ResourceFile getResourceFile(List<ResourceFile> resources) {
for (ResourceFile rf : resources) {
if (rf.getType() == ResourceType.ARSC) {
return rf;
}
}
return null;
}
public void processResources(ResourceStorage resStorage) {
constValues.setResourcesNames(resStorage.getResourcesNames());
appPackage = resStorage.getAppPackage();
@@ -209,7 +209,7 @@ public class RootNode {
}
}
private void updateObfuscatedFiles(ResTableParser parser, List<ResourceFile> resources) {
private void updateObfuscatedFiles(IResParser parser, List<ResourceFile> resources) {
if (args.isSkipResources()) {
return;
}
@@ -269,6 +269,7 @@ public class RootNode {
public void runPreDecompileStage() {
boolean debugEnabled = LOG.isDebugEnabled();
for (IDexTreeVisitor pass : preDecompilePasses) {
Utils.checkThreadInterrupt();
long start = debugEnabled ? System.currentTimeMillis() : 0;
try {
pass.init(this);
@@ -14,9 +14,9 @@ import jadx.core.dex.instructions.args.ArgType;
import jadx.core.dex.instructions.args.InsnArg;
import jadx.core.dex.nodes.BlockNode;
import jadx.core.dex.nodes.IContainer;
import jadx.core.dex.nodes.MethodNode;
import jadx.core.utils.InsnUtils;
import jadx.core.utils.Utils;
import jadx.core.utils.exceptions.JadxRuntimeException;
public class ExceptionHandler {
@@ -33,9 +33,14 @@ public class ExceptionHandler {
private boolean removed = false;
public ExceptionHandler(int addr, @Nullable ClassInfo type) {
public static ExceptionHandler build(MethodNode mth, int addr, @Nullable ClassInfo type) {
ExceptionHandler eh = new ExceptionHandler(addr);
eh.addCatchType(mth, type);
return eh;
}
private ExceptionHandler(int addr) {
this.handlerOffset = addr;
addCatchType(type);
}
/**
@@ -43,7 +48,7 @@ public class ExceptionHandler {
*
* @param type - null for 'all' or 'Throwable' handler
*/
public boolean addCatchType(@Nullable ClassInfo type) {
public boolean addCatchType(MethodNode mth, @Nullable ClassInfo type) {
if (type != null) {
if (catchTypes.contains(type)) {
return false;
@@ -51,14 +56,16 @@ public class ExceptionHandler {
return catchTypes.add(type);
}
if (!this.catchTypes.isEmpty()) {
throw new JadxRuntimeException("Null type added to not empty exception handler: " + this);
mth.addDebugComment("Throwable added to exception handler: '" + catchTypeStr() + "', keep only Throwable");
catchTypes.clear();
return true;
}
return false;
}
public void addCatchTypes(Collection<ClassInfo> types) {
public void addCatchTypes(MethodNode mth, Collection<ClassInfo> types) {
for (ClassInfo type : types) {
addCatchType(type);
addCatchType(mth, type);
}
}
@@ -133,7 +133,7 @@ public class AttachTryCatchVisitor extends AbstractVisitor {
ExcHandlerAttr excHandlerAttr = insn.get(AType.EXC_HANDLER);
if (excHandlerAttr != null) {
ExceptionHandler handler = excHandlerAttr.getHandler();
if (handler.addCatchType(type)) {
if (handler.addCatchType(mth, type)) {
// exist handler updated (assume from same try block) - don't add again
return null;
}
@@ -143,7 +143,7 @@ public class AttachTryCatchVisitor extends AbstractVisitor {
} else {
insn = insertNOP(insnByOffset, handlerOffset);
}
ExceptionHandler handler = new ExceptionHandler(handlerOffset, type);
ExceptionHandler handler = ExceptionHandler.build(mth, handlerOffset, type);
mth.addExceptionHandler(handler);
insn.addAttr(new ExcHandlerAttr(handler));
return handler;
@@ -10,6 +10,7 @@ import java.util.Objects;
import jadx.api.plugins.input.data.AccessFlags;
import jadx.core.Consts;
import jadx.core.dex.attributes.AFlag;
import jadx.core.dex.attributes.AType;
import jadx.core.dex.attributes.nodes.FieldReplaceAttr;
import jadx.core.dex.attributes.nodes.MethodReplaceAttr;
import jadx.core.dex.attributes.nodes.SkipMethodArgsAttr;
@@ -225,7 +226,7 @@ public class ClassModifier extends AbstractVisitor {
}
private static boolean removeBridgeMethod(ClassNode cls, MethodNode mth) {
if (cls.root().getArgs().isRenameValid()) {
if (cls.root().getArgs().isInlineMethods()) { // simple wrapper remove is same as inline
List<InsnNode> allInsns = BlockUtils.collectAllInsns(mth.getBasicBlocks());
if (allInsns.size() == 1) {
InsnNode wrappedInsn = allInsns.get(0);
@@ -235,12 +236,10 @@ public class ClassModifier extends AbstractVisitor {
wrappedInsn = ((InsnWrapArg) arg).getWrapInsn();
}
}
if (checkSyntheticWrapper(mth, wrappedInsn)) {
return true;
}
return checkSyntheticWrapper(mth, wrappedInsn);
}
}
return !isMethodUnique(cls, mth);
return false;
}
private static boolean checkSyntheticWrapper(MethodNode mth, InsnNode insn) {
@@ -283,6 +282,9 @@ public class ClassModifier extends AbstractVisitor {
if (!Objects.equals(wrappedMth.getAlias(), alias)) {
wrappedMth.getMethodInfo().setAlias(alias);
}
wrappedMth.addAttr(new MethodReplaceAttr(mth));
wrappedMth.copyAttributeFrom(mth, AType.METHOD_OVERRIDE);
wrappedMth.addDebugComment("Method merged with bridge method");
return true;
}
@@ -299,20 +301,6 @@ public class ClassModifier extends AbstractVisitor {
return false;
}
private static boolean isMethodUnique(ClassNode cls, MethodNode mth) {
MethodInfo mi = mth.getMethodInfo();
for (MethodNode otherMth : cls.getMethods()) {
if (otherMth != mth) {
MethodInfo omi = otherMth.getMethodInfo();
if (omi.getName().equals(mi.getName())
&& Objects.equals(omi.getArgumentsTypes(), mi.getArgumentsTypes())) {
return false;
}
}
}
return true;
}
/**
* Remove public empty constructors (static or default)
*/
@@ -18,6 +18,7 @@ import jadx.core.dex.attributes.AFlag;
import jadx.core.dex.attributes.AType;
import jadx.core.dex.attributes.nodes.EnumClassAttr;
import jadx.core.dex.attributes.nodes.EnumClassAttr.EnumField;
import jadx.core.dex.attributes.nodes.RenameReasonAttr;
import jadx.core.dex.attributes.nodes.SkipMethodArgsAttr;
import jadx.core.dex.info.AccessInfo;
import jadx.core.dex.info.ClassInfo;
@@ -26,6 +27,7 @@ import jadx.core.dex.info.MethodInfo;
import jadx.core.dex.instructions.IndexInsnNode;
import jadx.core.dex.instructions.InsnType;
import jadx.core.dex.instructions.InvokeNode;
import jadx.core.dex.instructions.InvokeType;
import jadx.core.dex.instructions.args.ArgType;
import jadx.core.dex.instructions.args.InsnArg;
import jadx.core.dex.instructions.args.InsnWrapArg;
@@ -59,6 +61,7 @@ import static jadx.core.utils.InsnUtils.getWrappedInsn;
public class EnumVisitor extends AbstractVisitor {
private MethodInfo enumValueOfMth;
private MethodInfo cloneMth;
@Override
public void init(RootNode root) {
@@ -68,6 +71,12 @@ public class EnumVisitor extends AbstractVisitor {
"valueOf",
Arrays.asList(ArgType.CLASS, ArgType.STRING),
ArgType.ENUM);
cloneMth = MethodInfo.fromDetails(root,
ClassInfo.fromType(root, ArgType.OBJECT),
"clone",
Collections.emptyList(),
ArgType.OBJECT);
}
@Override
@@ -377,6 +386,7 @@ public class EnumVisitor extends AbstractVisitor {
return enumFieldNode;
}
@SuppressWarnings("StatementWithEmptyBody")
private EnumField createEnumFieldByConstructor(ClassNode cls, FieldNode enumFieldNode, ConstructorInsn co) {
// usually constructor signature is '<init>(Ljava/lang/String;I)V'.
// sometimes for one field enum second arg can be omitted
@@ -417,13 +427,12 @@ public class EnumVisitor extends AbstractVisitor {
}
private void removeEnumMethods(ClassNode cls, ArgType clsType, FieldNode valuesField) {
String valuesMethod = "values()" + TypeGen.signature(ArgType.array(clsType));
FieldInfo valuesFieldInfo = valuesField.getFieldInfo();
String valuesMethodShortId = "values()" + TypeGen.signature(ArgType.array(clsType));
MethodNode valuesMethod = null;
// remove compiler generated methods
for (MethodNode mth : cls.getMethods()) {
MethodInfo mi = mth.getMethodInfo();
if (mi.isClassInit()) {
if (mi.isClassInit() || mth.isNoCode()) {
continue;
}
String shortId = mi.getShortId();
@@ -432,12 +441,33 @@ public class EnumVisitor extends AbstractVisitor {
mth.add(AFlag.DONT_GENERATE);
}
markArgsForSkip(mth);
} else if (shortId.equals(valuesMethod)
|| usesValuesField(mth, valuesFieldInfo)
|| simpleValueOfMth(mth, clsType)) {
} else if (mi.getShortId().equals(valuesMethodShortId)) {
if (isValuesMethod(mth, clsType)) {
valuesMethod = mth;
mth.add(AFlag.DONT_GENERATE);
} else {
// custom values method => rename to resolve conflict with enum method
mth.getMethodInfo().setAlias("valuesCustom");
mth.addAttr(new RenameReasonAttr(mth).append("to resolve conflict with enum method"));
}
} else if (isValuesMethod(mth, clsType)) {
if (!mth.getMethodInfo().getAlias().equals("values") && !mth.getUseIn().isEmpty()) {
// rename to use default values method
mth.getMethodInfo().setAlias("values");
mth.addAttr(new RenameReasonAttr(mth).append("to match enum method name"));
mth.add(AFlag.DONT_RENAME);
}
valuesMethod = mth;
mth.add(AFlag.DONT_GENERATE);
} else if (simpleValueOfMth(mth, clsType)) {
mth.add(AFlag.DONT_GENERATE);
}
}
FieldInfo valuesFieldInfo = valuesField.getFieldInfo();
for (MethodNode mth : cls.getMethods()) {
// fix access to 'values' field and 'values()' method
fixValuesAccess(mth, valuesFieldInfo, clsType, valuesMethod);
}
}
private void markArgsForSkip(MethodNode mth) {
@@ -458,6 +488,25 @@ public class EnumVisitor extends AbstractVisitor {
return false;
}
// TODO: support other method patterns ???
private boolean isValuesMethod(MethodNode mth, ArgType clsType) {
ArgType retType = mth.getReturnType();
if (!retType.isArray() || !retType.getArrayElement().equals(clsType)) {
return false;
}
InsnNode returnInsn = BlockUtils.getOnlyOneInsnFromMth(mth);
if (returnInsn == null || returnInsn.getType() != InsnType.RETURN || returnInsn.getArgsCount() != 1) {
return false;
}
InsnNode wrappedInsn = getWrappedInsn(getSingleArg(returnInsn));
IndexInsnNode castInsn = (IndexInsnNode) checkInsnType(wrappedInsn, InsnType.CHECK_CAST);
if (castInsn != null && Objects.equals(castInsn.getIndex(), ArgType.array(clsType))) {
InvokeNode invokeInsn = (InvokeNode) checkInsnType(getWrappedInsn(getSingleArg(castInsn)), InsnType.INVOKE);
return invokeInsn != null && invokeInsn.getCallMth().equals(cloneMth);
}
return false;
}
private boolean simpleValueOfMth(MethodNode mth, ArgType clsType) {
InsnNode returnInsn = InsnUtils.searchSingleReturnInsn(mth, insn -> insn.getArgsCount() == 1);
if (returnInsn == null) {
@@ -472,9 +521,41 @@ public class EnumVisitor extends AbstractVisitor {
return false;
}
private boolean usesValuesField(MethodNode mth, FieldInfo valuesFieldInfo) {
private void fixValuesAccess(MethodNode mth, FieldInfo valuesFieldInfo, ArgType clsType, @Nullable MethodNode valuesMethod) {
MethodInfo mi = mth.getMethodInfo();
if (mi.isConstructor() || mi.isClassInit() || mth.isNoCode() || mth == valuesMethod) {
return;
}
// search value field usage
Predicate<InsnNode> insnTest = insn -> Objects.equals(((IndexInsnNode) insn).getIndex(), valuesFieldInfo);
return InsnUtils.searchInsn(mth, InsnType.SGET, insnTest) != null;
InsnNode useInsn = InsnUtils.searchInsn(mth, InsnType.SGET, insnTest);
if (useInsn == null) {
return;
}
// replace 'values' field with 'values()' method
InsnUtils.replaceInsns(mth, insn -> {
if (insn.getType() == InsnType.SGET && insnTest.test(insn)) {
MethodInfo valueMth = valuesMethod == null
? getValueMthInfo(mth.root(), clsType)
: valuesMethod.getMethodInfo();
InvokeNode invokeNode = new InvokeNode(valueMth, InvokeType.STATIC, 0);
invokeNode.setResult(insn.getResult());
if (valuesMethod == null) {
// forcing enum method (can overlap and get renamed by custom method)
invokeNode.add(AFlag.FORCE_RAW_NAME);
}
mth.addDebugComment("Replace access to removed values field (" + valuesFieldInfo.getName() + ") with 'values()' method");
return invokeNode;
}
return null;
});
}
private MethodInfo getValueMthInfo(RootNode root, ArgType clsType) {
return MethodInfo.fromDetails(root,
ClassInfo.fromType(root, clsType),
"values",
Collections.emptyList(), ArgType.array(clsType));
}
private static void processEnumCls(ClassNode cls, EnumField field, ClassNode innerCls) {
@@ -362,8 +362,7 @@ public class ModVisitor extends AbstractVisitor {
private static void removeCheckCast(MethodNode mth, BlockNode block, int i, IndexInsnNode insn) {
InsnArg castArg = insn.getArg(0);
ArgType castType = (ArgType) insn.getIndex();
if (!ArgType.isCastNeeded(mth.root(), castArg.getType(), castType)
|| isCastDuplicate(insn)) {
if (!ArgType.isCastNeeded(mth.root(), castArg.getType(), castType)) {
RegisterArg result = insn.getResult();
result.setType(castArg.getType());
@@ -371,10 +370,19 @@ public class ModVisitor extends AbstractVisitor {
move.setResult(result);
move.addArg(castArg);
replaceInsn(mth, block, i, move);
return;
}
InsnNode prevCast = isCastDuplicate(insn);
if (prevCast != null) {
// replace previous cast with move
InsnNode move = new InsnNode(InsnType.MOVE, 1);
move.setResult(prevCast.getResult());
move.addArg(prevCast.getArg(0));
replaceInsn(mth, block, prevCast, move);
}
}
private static boolean isCastDuplicate(IndexInsnNode castInsn) {
private static @Nullable InsnNode isCastDuplicate(IndexInsnNode castInsn) {
InsnArg arg = castInsn.getArg(0);
if (arg.isRegister()) {
SSAVar sVar = ((RegisterArg) arg).getSVar();
@@ -382,11 +390,13 @@ public class ModVisitor extends AbstractVisitor {
InsnNode assignInsn = sVar.getAssign().getParentInsn();
if (assignInsn != null && assignInsn.getType() == InsnType.CHECK_CAST) {
ArgType assignCastType = (ArgType) ((IndexInsnNode) assignInsn).getIndex();
return assignCastType.equals(castInsn.getIndex());
if (assignCastType.equals(castInsn.getIndex())) {
return assignInsn;
}
}
}
}
return false;
return null;
}
/**
@@ -50,7 +50,7 @@ public class BlockExceptionHandler {
return false;
}
BlockProcessor.updateCleanSuccessors(mth);
BlockProcessor.computeDominanceFrontier(mth);
DominatorTree.computeDominanceFrontier(mth);
processCatchAttr(mth);
initExcHandlers(mth);
@@ -549,7 +549,7 @@ public class BlockExceptionHandler {
if (handler == resultHandler) {
return false;
}
resultHandler.addCatchTypes(handler.getCatchTypes());
resultHandler.addCatchTypes(mth, handler.getCatchTypes());
handler.markForRemove();
return true;
});
@@ -1,9 +1,6 @@
package jadx.core.dex.visitors.blocks;
import java.util.ArrayList;
import java.util.BitSet;
import java.util.Collections;
import java.util.Deque;
import java.util.LinkedHashSet;
import java.util.LinkedList;
import java.util.List;
@@ -31,7 +28,6 @@ import jadx.core.utils.Utils;
import jadx.core.utils.exceptions.JadxRuntimeException;
import static jadx.core.dex.visitors.blocks.BlockSplitter.connect;
import static jadx.core.utils.EmptyBitSet.EMPTY;
public class BlockProcessor extends AbstractVisitor {
private static final Logger LOG = LoggerFactory.getLogger(BlockProcessor.class);
@@ -50,29 +46,23 @@ public class BlockProcessor extends AbstractVisitor {
computeDominators(mth);
if (independentBlockTreeMod(mth)) {
checkForUnreachableBlocks(mth);
clearBlocksState(mth);
computeDominators(mth);
}
if (FixMultiEntryLoops.process(mth)) {
clearBlocksState(mth);
computeDominators(mth);
}
updateCleanSuccessors(mth);
int i = 0;
while (modifyBlocksTree(mth)) {
// revert calculations
clearBlocksState(mth);
// recalculate dominators tree
computeDominators(mth);
if (i++ > 100) {
throw new JadxRuntimeException("CFG modification limit reached, blocks count: " + mth.getBasicBlocks().size());
}
}
checkForUnreachableBlocks(mth);
computeDominanceFrontier(mth);
DominatorTree.computeDominanceFrontier(mth);
registerLoops(mth);
processNestedLoops(mth);
@@ -209,139 +199,9 @@ public class BlockProcessor extends AbstractVisitor {
}
private static void computeDominators(MethodNode mth) {
List<BlockNode> basicBlocks = mth.getBasicBlocks();
int nBlocks = basicBlocks.size();
for (int i = 0; i < nBlocks; i++) {
BlockNode block = basicBlocks.get(i);
block.setId(i);
block.setDoms(new BitSet(nBlocks));
block.getDoms().set(0, nBlocks);
}
BlockNode entryBlock = mth.getEnterBlock();
calcDominators(basicBlocks, entryBlock);
clearBlocksState(mth);
DominatorTree.compute(mth);
markLoops(mth);
// clear self dominance
basicBlocks.forEach(block -> {
block.getDoms().clear(block.getId());
if (block.getDoms().isEmpty()) {
block.setDoms(EMPTY);
}
});
calcImmediateDominators(mth, basicBlocks, entryBlock);
}
private static void calcDominators(List<BlockNode> basicBlocks, BlockNode entryBlock) {
entryBlock.getDoms().clear();
entryBlock.getDoms().set(entryBlock.getId());
BitSet domSet = new BitSet(basicBlocks.size());
boolean changed;
do {
changed = false;
for (BlockNode block : basicBlocks) {
if (block == entryBlock) {
continue;
}
BitSet d = block.getDoms();
if (!changed) {
domSet.clear();
domSet.or(d);
}
for (BlockNode pred : block.getPredecessors()) {
d.and(pred.getDoms());
}
d.set(block.getId());
if (!changed && !d.equals(domSet)) {
changed = true;
}
}
} while (changed);
}
private static void calcImmediateDominators(MethodNode mth, List<BlockNode> basicBlocks, BlockNode entryBlock) {
for (BlockNode block : basicBlocks) {
if (block == entryBlock) {
continue;
}
BlockNode idom;
List<BlockNode> preds = block.getPredecessors();
if (preds.size() == 1) {
idom = preds.get(0);
} else {
BitSet bs = new BitSet(block.getDoms().length());
bs.or(block.getDoms());
for (int i = bs.nextSetBit(0); i >= 0; i = bs.nextSetBit(i + 1)) {
BlockNode dom = basicBlocks.get(i);
bs.andNot(dom.getDoms());
}
if (bs.cardinality() != 1) {
throw new JadxRuntimeException("Can't find immediate dominator for block " + block
+ " in " + bs + " preds:" + preds);
}
idom = basicBlocks.get(bs.nextSetBit(0));
}
block.setIDom(idom);
idom.addDominatesOn(block);
}
}
static void computeDominanceFrontier(MethodNode mth) {
mth.getExitBlock().setDomFrontier(EMPTY);
List<BlockNode> domSortedBlocks = new ArrayList<>(mth.getBasicBlocks().size());
Deque<BlockNode> stack = new LinkedList<>();
stack.push(mth.getEnterBlock());
while (!stack.isEmpty()) {
BlockNode node = stack.pop();
for (BlockNode dominated : node.getDominatesOn()) {
stack.push(dominated);
}
domSortedBlocks.add(node);
}
Collections.reverse(domSortedBlocks);
for (BlockNode block : domSortedBlocks) {
try {
computeBlockDF(mth, block);
} catch (Exception e) {
throw new JadxRuntimeException("Failed compute block dominance frontier", e);
}
}
}
private static void computeBlockDF(MethodNode mth, BlockNode block) {
if (block.getDomFrontier() != null) {
return;
}
List<BlockNode> blocks = mth.getBasicBlocks();
BitSet domFrontier = null;
for (BlockNode s : block.getSuccessors()) {
if (s.getIDom() != block) {
if (domFrontier == null) {
domFrontier = new BitSet(blocks.size());
}
domFrontier.set(s.getId());
}
}
for (BlockNode c : block.getDominatesOn()) {
BitSet frontier = c.getDomFrontier();
if (frontier == null) {
throw new JadxRuntimeException("Dominance frontier not calculated for dominated block: " + c + ", from: " + block);
}
for (int p = frontier.nextSetBit(0); p >= 0; p = frontier.nextSetBit(p + 1)) {
if (blocks.get(p).getIDom() != block) {
if (domFrontier == null) {
domFrontier = new BitSet(blocks.size());
}
domFrontier.set(p);
}
}
}
if (domFrontier == null || domFrontier.isEmpty()) {
domFrontier = EMPTY;
}
block.setDomFrontier(domFrontier);
}
private static void markLoops(MethodNode mth) {
@@ -349,7 +209,7 @@ public class BlockProcessor extends AbstractVisitor {
// Every successor that dominates its predecessor is a header of a loop,
// block -> successor is a back edge.
block.getSuccessors().forEach(successor -> {
if (block.getDoms().get(successor.getId())) {
if (block.getDoms().get(successor.getId()) || block == successor) {
successor.add(AFlag.LOOP_START);
block.add(AFlag.LOOP_END);
@@ -0,0 +1,175 @@
package jadx.core.dex.visitors.blocks;
import java.util.ArrayList;
import java.util.BitSet;
import java.util.List;
import org.jetbrains.annotations.NotNull;
import jadx.core.dex.nodes.BlockNode;
import jadx.core.dex.nodes.MethodNode;
import jadx.core.utils.BlockUtils;
import jadx.core.utils.EmptyBitSet;
import jadx.core.utils.exceptions.JadxRuntimeException;
/**
* Build dominator tree based on the algorithm described in paper:
* Cooper, Keith D.; Harvey, Timothy J; Kennedy, Ken (2001).
* "A Simple, Fast Dominance Algorithm"
* http://www.hipersoft.rice.edu/grads/publications/dom14.pdf
*/
@SuppressWarnings("JavadocLinkAsPlainText")
public class DominatorTree {
public static void compute(MethodNode mth) {
List<BlockNode> sorted = sortBlocks(mth);
BlockNode[] doms = build(sorted);
apply(sorted, doms);
mth.setBasicBlocks(sorted);
}
private static List<BlockNode> sortBlocks(MethodNode mth) {
int blocksCount = mth.getBasicBlocks().size();
BitSet reachSet = new BitSet(blocksCount);
List<BlockNode> sorted = new ArrayList<>(blocksCount);
BlockUtils.dfsVisit(mth, b -> {
sorted.add(b);
reachSet.set(b.getId());
});
if (reachSet.cardinality() != blocksCount) {
throw new JadxRuntimeException("Found unreachable blocks");
}
for (int i = 0; i < blocksCount; i++) {
sorted.get(i).setId(i);
}
return sorted;
}
@NotNull
private static BlockNode[] build(List<BlockNode> sorted) {
int blocksCount = sorted.size();
BlockNode[] doms = new BlockNode[blocksCount];
doms[0] = sorted.get(0);
boolean changed = true;
while (changed) {
changed = false;
for (int blockId = 1; blockId < blocksCount; blockId++) {
BlockNode b = sorted.get(blockId);
List<BlockNode> preds = b.getPredecessors();
int pickedPred = -1;
BlockNode newIDom = null;
for (BlockNode pred : preds) {
int id = pred.getId();
if (doms[id] != null) {
newIDom = pred;
pickedPred = id;
break;
}
}
if (newIDom == null) {
throw new JadxRuntimeException("No predecessors for block: " + b);
}
for (BlockNode predBlock : preds) {
int predId = predBlock.getId();
if (predId == pickedPred) {
continue;
}
if (doms[predId] != null) {
newIDom = intersect(sorted, doms, predBlock, newIDom);
}
}
if (doms[blockId] != newIDom) {
doms[blockId] = newIDom;
changed = true;
}
}
}
return doms;
}
private static BlockNode intersect(List<BlockNode> sorted, BlockNode[] doms, BlockNode b1, BlockNode b2) {
int f1 = b1.getId();
int f2 = b2.getId();
while (f1 != f2) {
while (f1 > f2) {
f1 = doms[f1].getId();
}
while (f2 > f1) {
f2 = doms[f2].getId();
}
}
return sorted.get(f1);
}
private static void apply(List<BlockNode> sorted, BlockNode[] doms) {
BlockNode enterBlock = sorted.get(0);
enterBlock.setDoms(EmptyBitSet.EMPTY);
enterBlock.setIDom(null);
int blocksCount = sorted.size();
for (int i = 1; i < blocksCount; i++) {
BlockNode block = sorted.get(i);
BlockNode idom = doms[i];
block.setIDom(idom);
idom.addDominatesOn(block);
BitSet domBS = collectDoms(doms, idom);
domBS.clear(i);
block.setDoms(domBS);
}
}
private static BitSet collectDoms(BlockNode[] doms, BlockNode idom) {
BitSet domBS = new BitSet(doms.length);
BlockNode nextIDom = idom;
while (true) {
int id = nextIDom.getId();
if (domBS.get(id)) {
break;
}
domBS.set(id);
BitSet curDoms = nextIDom.getDoms();
if (curDoms != null) {
// use already collected set
domBS.or(curDoms);
break;
}
nextIDom = doms[id];
}
return domBS;
}
public static void computeDominanceFrontier(MethodNode mth) {
List<BlockNode> blocks = mth.getBasicBlocks();
for (BlockNode block : blocks) {
block.setDomFrontier(null);
}
int blocksCount = blocks.size();
for (BlockNode block : blocks) {
List<BlockNode> preds = block.getPredecessors();
if (preds.size() >= 2) {
BlockNode idom = block.getIDom();
for (BlockNode pred : preds) {
BlockNode runner = pred;
while (runner != idom) {
addToDF(runner, block, blocksCount);
runner = runner.getIDom();
}
}
}
}
for (BlockNode block : blocks) {
BitSet df = block.getDomFrontier();
if (df == null || df.isEmpty()) {
block.setDomFrontier(EmptyBitSet.EMPTY);
}
}
}
private static void addToDF(BlockNode block, BlockNode dfBlock, int blocksCount) {
BitSet df = block.getDomFrontier();
if (df == null) {
df = new BitSet(blocksCount);
block.setDomFrontier(df);
}
df.set(dfBlock.getId());
}
}
@@ -6,9 +6,12 @@ import java.util.List;
import java.util.Set;
import jadx.core.dex.nodes.BlockNode;
import jadx.core.dex.nodes.MethodNode;
import jadx.core.dex.trycatch.ExceptionHandler;
import jadx.core.utils.Utils;
public class FinallyExtractInfo {
private final MethodNode mth;
private final ExceptionHandler finallyHandler;
private final List<BlockNode> allHandlerBlocks;
private final List<InsnsSlice> duplicateSlices = new ArrayList<>();
@@ -16,12 +19,17 @@ public class FinallyExtractInfo {
private final InsnsSlice finallyInsnsSlice = new InsnsSlice();
private final BlockNode startBlock;
public FinallyExtractInfo(ExceptionHandler finallyHandler, BlockNode startBlock, List<BlockNode> allHandlerBlocks) {
public FinallyExtractInfo(MethodNode mth, ExceptionHandler finallyHandler, BlockNode startBlock, List<BlockNode> allHandlerBlocks) {
this.mth = mth;
this.finallyHandler = finallyHandler;
this.startBlock = startBlock;
this.allHandlerBlocks = allHandlerBlocks;
}
public MethodNode getMth() {
return mth;
}
public ExceptionHandler getFinallyHandler() {
return finallyHandler;
}
@@ -45,4 +53,12 @@ public class FinallyExtractInfo {
public BlockNode getStartBlock() {
return startBlock;
}
@Override
public String toString() {
return "FinallyExtractInfo{"
+ "\n finally:\n " + finallyInsnsSlice
+ "\n dups:\n " + Utils.listToString(duplicateSlices, "\n ")
+ "\n}";
}
}
@@ -9,6 +9,7 @@ import org.jetbrains.annotations.Nullable;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import jadx.core.Consts;
import jadx.core.dex.attributes.AFlag;
import jadx.core.dex.attributes.AType;
import jadx.core.dex.instructions.InsnType;
@@ -38,6 +39,7 @@ import jadx.core.utils.Utils;
)
public class MarkFinallyVisitor extends AbstractVisitor {
private static final Logger LOG = LoggerFactory.getLogger(MarkFinallyVisitor.class);
// private static final Logger LOG = LoggerFactory.getLogger(MarkFinallyVisitor.class);
@Override
public void visit(MethodNode mth) {
@@ -60,7 +62,7 @@ public class MarkFinallyVisitor extends AbstractVisitor {
}
}
} catch (Exception e) {
LOG.warn("Undo finally extract visitor, mth: {}", mth, e);
mth.addWarnComment("Undo finally extract visitor", e);
undoFinallyVisitor(mth);
}
}
@@ -100,20 +102,23 @@ public class MarkFinallyVisitor extends AbstractVisitor {
List<BlockNode> handlerBlocks =
new ArrayList<>(BlockUtils.collectBlocksDominatedByWithExcHandlers(mth, handlerBlock, handlerBlock));
handlerBlocks.remove(handlerBlock); // exclude block with 'move-exception'
handlerBlocks.removeIf(b -> BlockUtils.checkLastInsnType(b, InsnType.THROW));
cutPathEnds(mth, handlerBlocks);
if (handlerBlocks.isEmpty() || BlockUtils.isAllBlocksEmpty(handlerBlocks)) {
// remove empty catch
allHandler.getTryBlock().removeHandler(allHandler);
return true;
}
BlockNode startBlock = Utils.getOne(handlerBlock.getCleanSuccessors());
FinallyExtractInfo extractInfo = new FinallyExtractInfo(allHandler, startBlock, handlerBlocks);
FinallyExtractInfo extractInfo = new FinallyExtractInfo(mth, allHandler, startBlock, handlerBlocks);
if (Consts.DEBUG_FINALLY) {
LOG.debug("Finally info: handler=({}), start={}, blocks={}", allHandler, startBlock, handlerBlocks);
}
boolean hasInnerBlocks = !tryBlock.getInnerTryBlocks().isEmpty();
List<ExceptionHandler> handlers;
if (hasInnerBlocks) {
// collect handlers from this and all inner blocks (intentionally not using recursive collect for
// now)
// collect handlers from this and all inner blocks
// (intentionally not using recursive collect for now)
handlers = new ArrayList<>(tryBlock.getHandlers());
for (TryCatchBlockAttr innerTryBlock : tryBlock.getInnerTryBlocks()) {
handlers.addAll(innerTryBlock.getHandlers());
@@ -137,10 +142,12 @@ public class MarkFinallyVisitor extends AbstractVisitor {
}
}
}
if (Consts.DEBUG_FINALLY) {
LOG.debug("Handlers slices:\n{}", extractInfo);
}
boolean mergeInnerTryBlocks;
int duplicatesCount = extractInfo.getDuplicateSlices().size();
boolean fullTryBlock = duplicatesCount == (handlers.size() - 1);
if (fullTryBlock) {
if (duplicatesCount == (handlers.size() - 1)) {
// all collected handlers have duplicate block
mergeInnerTryBlocks = hasInnerBlocks;
} else {
@@ -170,15 +177,24 @@ public class MarkFinallyVisitor extends AbstractVisitor {
if (upPath.size() < handlerBlocks.size()) {
continue;
}
if (Consts.DEBUG_FINALLY) {
LOG.debug("Checking dup path starts: {} from {}", upPath, pred);
}
for (BlockNode block : upPath) {
if (searchDuplicateInsns(block, extractInfo)) {
found = true;
if (Consts.DEBUG_FINALLY) {
LOG.debug("Found dup in: {} from {}", block, pred);
}
break;
} else {
extractInfo.getFinallyInsnsSlice().resetIncomplete();
}
}
}
if (Consts.DEBUG_FINALLY) {
LOG.debug("Result slices:\n{}", extractInfo);
}
if (!found) {
return false;
}
@@ -204,6 +220,28 @@ public class MarkFinallyVisitor extends AbstractVisitor {
return true;
}
private static void cutPathEnds(MethodNode mth, List<BlockNode> handlerBlocks) {
List<BlockNode> throwBlocks = ListUtils.filter(handlerBlocks,
b -> BlockUtils.checkLastInsnType(b, InsnType.THROW));
if (throwBlocks.size() != 1) {
mth.addDebugComment("Finally have unexpected throw blocks count: " + throwBlocks.size() + ", expect 1");
return;
}
BlockNode throwBlock = throwBlocks.get(0);
handlerBlocks.remove(throwBlock);
removeEmptyUpPath(handlerBlocks, throwBlock);
}
private static void removeEmptyUpPath(List<BlockNode> handlerBlocks, BlockNode startBlock) {
for (BlockNode pred : startBlock.getPredecessors()) {
if (pred.isEmpty()) {
if (handlerBlocks.remove(pred) && !BlockUtils.isBackEdge(pred, startBlock)) {
removeEmptyUpPath(handlerBlocks, pred);
}
}
}
}
private static List<BlockNode> getPathStarts(MethodNode mth, BlockNode bottom, BlockNode bottomFinallyBlock) {
Stream<BlockNode> preds = bottom.getPredecessors().stream().filter(b -> b != bottomFinallyBlock);
if (bottom == mth.getExitBlock()) {
@@ -219,9 +257,8 @@ public class MarkFinallyVisitor extends AbstractVisitor {
for (InsnsSlice dupSlice : extractInfo.getDuplicateSlices()) {
List<InsnNode> dupInsnsList = dupSlice.getInsnsList();
if (dupInsnsList.size() != finallyInsnsList.size()) {
if (LOG.isDebugEnabled()) {
LOG.debug("Incorrect finally slice size: {}, expected: {}", dupSlice, finallySlice);
}
extractInfo.getMth().addDebugComment(
"Incorrect finally slice size: " + dupSlice + ", expected: " + finallySlice);
return false;
}
}
@@ -231,9 +268,8 @@ public class MarkFinallyVisitor extends AbstractVisitor {
List<InsnNode> insnsList = dupSlice.getInsnsList();
InsnNode dupInsn = insnsList.get(i);
if (finallyInsn.getType() != dupInsn.getType()) {
if (LOG.isDebugEnabled()) {
LOG.debug("Incorrect finally slice insn: {}, expected: {}", dupInsn, finallyInsn);
}
extractInfo.getMth().addDebugComment(
"Incorrect finally slice insn: " + dupInsn + ", expected: " + finallyInsn);
return false;
}
}
@@ -342,24 +378,29 @@ public class MarkFinallyVisitor extends AbstractVisitor {
private static InsnsSlice isStartBlock(BlockNode dupBlock, BlockNode finallyBlock, FinallyExtractInfo extractInfo) {
List<InsnNode> dupInsns = dupBlock.getInstructions();
List<InsnNode> finallyInsns = finallyBlock.getInstructions();
if (dupInsns.size() < finallyInsns.size()) {
int dupSize = dupInsns.size();
int finSize = finallyInsns.size();
if (dupSize < finSize) {
return null;
}
int startPos = dupInsns.size() - finallyInsns.size();
int startPos;
int endPos = 0;
// fast check from end of block
if (!checkInsns(dupInsns, finallyInsns, startPos)) {
// check from block start
if (checkInsns(dupInsns, finallyInsns, 0)) {
startPos = 0;
endPos = finallyInsns.size();
} else {
if (dupSize == finSize) {
if (!checkInsns(dupInsns, finallyInsns, 0)) {
return null;
}
startPos = 0;
} else {
// dupSize > finSize
startPos = dupSize - finSize;
// fast check from end of block
if (!checkInsns(dupInsns, finallyInsns, startPos)) {
// search start insn
boolean found = false;
for (int i = 1; i < startPos; i++) {
if (checkInsns(dupInsns, finallyInsns, i)) {
startPos = i;
endPos = finallyInsns.size() + i;
endPos = finSize + i;
found = true;
break;
}
@@ -379,7 +420,7 @@ public class MarkFinallyVisitor extends AbstractVisitor {
// both slices completed
complete = true;
} else {
endIndex = dupInsns.size();
endIndex = dupSize;
complete = false;
}
@@ -393,9 +434,8 @@ public class MarkFinallyVisitor extends AbstractVisitor {
if (finallySlice.isComplete()) {
// compare slices
if (finallySlice.getInsnsList().size() != slice.getInsnsList().size()) {
if (LOG.isDebugEnabled()) {
LOG.debug("Another duplicated slice has different insns count: {}, finally: {}", slice, finallySlice);
}
extractInfo.getMth().addDebugComment(
"Another duplicated slice has different insns count: " + slice + ", finally: " + finallySlice);
return null;
}
// TODO: add additional slices checks
@@ -522,7 +562,7 @@ public class MarkFinallyVisitor extends AbstractVisitor {
DepthTraversal.visit(visitor, mth);
}
} catch (Exception e) {
LOG.error("Undo finally extract failed, mth: {}", mth, e);
mth.addError("Undo finally extract failed", e);
}
}
}
@@ -14,9 +14,7 @@ import jadx.core.Consts;
import jadx.core.codegen.json.JsonMappingGen;
import jadx.core.deobf.Deobfuscator;
import jadx.core.deobf.NameMapper;
import jadx.core.dex.attributes.AFlag;
import jadx.core.dex.attributes.nodes.RenameReasonAttr;
import jadx.core.dex.info.AccessInfo;
import jadx.core.dex.info.ClassInfo;
import jadx.core.dex.info.FieldInfo;
import jadx.core.dex.nodes.ClassNode;
@@ -45,8 +43,8 @@ public class RenameVisitor extends AbstractVisitor {
deobfuscator.execute();
}
checkClasses(deobfuscator, root, args);
UserRenames.applyForNodes(root);
checkClasses(deobfuscator, root, args);
if (args.isDeobfuscationOn() || !args.isJsonOutput()) {
deobfuscator.savePresets();
@@ -196,11 +194,6 @@ public class RenameVisitor extends AbstractVisitor {
if (args.isRenameValid()) {
Set<String> names = new HashSet<>(methods.size());
for (MethodNode mth : methods) {
AccessInfo accessFlags = mth.getAccessFlags();
if (accessFlags.isBridge() || accessFlags.isSynthetic()
|| mth.contains(AFlag.DONT_GENERATE) /* this flag not set yet */) {
continue;
}
String signature = mth.getMethodInfo().makeSignature(true, false);
if (!names.add(signature)) {
deobfuscator.forceRenameMethod(mth);
@@ -1013,6 +1013,9 @@ public class BlockUtils {
*/
@Nullable
public static InsnNode getOnlyOneInsnFromMth(MethodNode mth) {
if (mth.isNoCode()) {
return null;
}
InsnNode insn = null;
for (BlockNode block : mth.getBasicBlocks()) {
List<InsnNode> blockInsns = block.getInstructions();
@@ -246,4 +246,29 @@ public class DebugUtils {
Set<Object> seen = ConcurrentHashMap.newKeySet();
return t -> seen.add(keyExtractor.apply(t));
}
private static Map<String, Long> execTimes;
public static void initExecTimes() {
execTimes = new ConcurrentHashMap<>();
}
public static void mergeExecTimeFromStart(String tag, long startTimeMillis) {
mergeExecTime(tag, System.currentTimeMillis() - startTimeMillis);
}
public static void mergeExecTime(String tag, long execTimeMillis) {
execTimes.merge(tag, execTimeMillis, Long::sum);
}
public static void printExecTimes() {
System.out.println("Exec times:");
execTimes.forEach((tag, time) -> System.out.println(" " + tag + ": " + time + "ms"));
}
public static void printExecTimesWithTotal(long totalMillis) {
System.out.println("Exec times: total " + totalMillis + "ms");
execTimes.forEach((tag, time) -> System.out.println(" " + tag + ": " + time + "ms"
+ String.format(" (%.2f%%)", time * 100. / (double) totalMillis)));
}
}
@@ -1,6 +1,7 @@
package jadx.core.utils;
import java.util.List;
import java.util.function.Function;
import java.util.function.Predicate;
import org.jetbrains.annotations.Nullable;
@@ -140,6 +141,37 @@ public class InsnUtils {
return null;
}
public static void replaceInsns(MethodNode mth, Function<InsnNode, InsnNode> replaceFunction) {
for (BlockNode block : mth.getBasicBlocks()) {
List<InsnNode> insns = block.getInstructions();
int insnsCount = insns.size();
for (int i = 0; i < insnsCount; i++) {
InsnNode insn = insns.get(i);
replaceInsnsInInsn(mth, insn, replaceFunction);
InsnNode replace = replaceFunction.apply(insn);
if (replace != null) {
BlockUtils.replaceInsn(mth, block, i, replace);
}
}
}
}
public static void replaceInsnsInInsn(MethodNode mth, InsnNode insn, Function<InsnNode, InsnNode> replaceFunction) {
int argsCount = insn.getArgsCount();
for (int i = 0; i < argsCount; i++) {
InsnArg arg = insn.getArg(i);
if (arg.isInsnWrap()) {
InsnNode wrapInsn = ((InsnWrapArg) arg).getWrapInsn();
replaceInsnsInInsn(mth, wrapInsn, replaceFunction);
InsnNode replace = replaceFunction.apply(wrapInsn);
if (replace != null) {
InsnRemover.unbindArgUsage(mth, arg);
insn.setArg(i, InsnArg.wrapInsnIntoArg(replace));
}
}
}
}
@Nullable
public static RegisterArg getRegFromInsn(List<RegisterArg> regs, InsnType insnType) {
for (RegisterArg reg : regs) {
@@ -428,7 +428,7 @@ public class Utils {
}
public static void checkThreadInterrupt() {
if (Thread.interrupted()) {
if (Thread.currentThread().isInterrupted()) {
throw new JadxRuntimeException("Thread interrupted");
}
}
@@ -0,0 +1,13 @@
package jadx.core.xmlgen;
import java.io.IOException;
import java.io.InputStream;
public interface IResParser {
void decode(InputStream inputStream) throws IOException;
ResourceStorage getResStorage();
String[] getStrings();
}
@@ -0,0 +1,31 @@
package jadx.core.xmlgen;
import java.io.IOException;
import java.io.InputStream;
import jadx.api.ResourceFile;
import jadx.api.ResourceType;
import jadx.core.dex.nodes.RootNode;
import jadx.core.utils.exceptions.JadxRuntimeException;
public class ResDecoder {
public static IResParser decode(RootNode root, ResourceFile resFile, InputStream is) throws IOException {
if (resFile.getType() != ResourceType.ARSC) {
throw new IllegalArgumentException("Unexpected resource type for decode: " + resFile.getType() + ", expect ARSC");
}
IResParser parser = null;
String fileName = resFile.getOriginalName();
if (fileName.endsWith(".arsc")) {
parser = new ResTableParser(root);
}
if (fileName.endsWith(".pb")) {
parser = new ResProtoParser(root);
}
if (parser == null) {
throw new JadxRuntimeException("Unknown type of resource file: " + fileName);
}
parser.decode(is);
return parser;
}
}
@@ -20,16 +20,16 @@ import com.android.aapt.Resources.Style;
import com.android.aapt.Resources.Styleable;
import com.android.aapt.Resources.Type;
import com.android.aapt.Resources.Value;
import com.google.protobuf.InvalidProtocolBufferException;
import jadx.api.ICodeInfo;
import jadx.core.dex.nodes.RootNode;
import jadx.core.utils.files.FileUtils;
import jadx.core.xmlgen.entry.EntryConfig;
import jadx.core.xmlgen.entry.ProtoValue;
import jadx.core.xmlgen.entry.ResourceEntry;
import jadx.core.xmlgen.entry.ValuesParser;
public class ResProtoParser {
public class ResProtoParser implements IResParser {
private final RootNode root;
private final ResourceStorage resStorage = new ResourceStorage();
@@ -38,11 +38,7 @@ public class ResProtoParser {
}
public ResContainer decodeFiles(InputStream inputStream) throws IOException {
ResourceTable table = decodeProto(inputStream);
for (Package p : table.getPackageList()) {
parse(p);
}
resStorage.finish();
decode(inputStream);
ValuesParser vp = new ValuesParser(new String[0], resStorage.getResourcesNames());
ResXmlGen resGen = new ResXmlGen(resStorage, vp);
ICodeInfo content = XmlGenUtils.makeXmlDump(root.makeCodeWriter(), resStorage);
@@ -50,6 +46,15 @@ public class ResProtoParser {
return ResContainer.resourceTable("res", xmlFiles, content);
}
@Override
public void decode(InputStream inputStream) throws IOException {
ResourceTable table = ResourceTable.parseFrom(FileUtils.streamToByteArray(inputStream));
for (Package p : table.getPackageList()) {
parse(p);
}
resStorage.finish();
}
private void parse(Package p) {
String name = p.getPackageName();
resStorage.setAppPackage(name);
@@ -241,8 +246,13 @@ public class ResProtoParser {
return "";
}
private ResourceTable decodeProto(InputStream inputStream)
throws InvalidProtocolBufferException, IOException {
return ResourceTable.parseFrom(XmlGenUtils.readData(inputStream));
@Override
public ResourceStorage getResStorage() {
return resStorage;
}
@Override
public String[] getStrings() {
return new String[0];
}
}
@@ -24,7 +24,7 @@ import jadx.core.xmlgen.entry.RawValue;
import jadx.core.xmlgen.entry.ResourceEntry;
import jadx.core.xmlgen.entry.ValuesParser;
public class ResTableParser extends CommonBinaryParser {
public class ResTableParser extends CommonBinaryParser implements IResParser {
private static final Logger LOG = LoggerFactory.getLogger(ResTableParser.class);
private static final Pattern VALID_RES_KEY_PATTERN = Pattern.compile("[\\w\\d_]+");
@@ -76,6 +76,7 @@ public class ResTableParser extends CommonBinaryParser {
this.useRawResName = useRawResNames;
}
@Override
public void decode(InputStream inputStream) throws IOException {
is = new ParserStream(inputStream);
decodeTableChunk();
@@ -93,14 +94,6 @@ public class ResTableParser extends CommonBinaryParser {
return ResContainer.resourceTable("res", xmlFiles, content);
}
public ResourceStorage getResStorage() {
return resStorage;
}
public String[] getStrings() {
return strings;
}
void decodeTableChunk() throws IOException {
is.checkInt16(RES_TABLE_TYPE, "Not a table chunk");
is.checkInt16(0x000c, "Unexpected table header size");
@@ -434,4 +427,14 @@ public class ResTableParser extends CommonBinaryParser {
is.skipToPos(start + length, "readScriptOrVariantChar");
return sb.toString();
}
@Override
public ResourceStorage getResStorage() {
return resStorage;
}
@Override
public String[] getStrings() {
return strings;
}
}
@@ -43,9 +43,6 @@ import jadx.api.args.DeobfuscationMapFileMode;
import jadx.api.metadata.ICodeMetadata;
import jadx.api.metadata.annotations.InsnCodeOffset;
import jadx.core.dex.attributes.AFlag;
import jadx.core.dex.attributes.AType;
import jadx.core.dex.attributes.IAttributeNode;
import jadx.core.dex.attributes.nodes.JadxCommentsAttr;
import jadx.core.dex.nodes.ClassNode;
import jadx.core.dex.nodes.MethodNode;
import jadx.core.dex.nodes.RootNode;
@@ -63,13 +60,11 @@ import jadx.tests.api.utils.TestUtils;
import static org.apache.commons.lang3.StringUtils.leftPad;
import static org.apache.commons.lang3.StringUtils.rightPad;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.containsString;
import static org.hamcrest.Matchers.empty;
import static org.hamcrest.Matchers.emptyArray;
import static org.hamcrest.Matchers.is;
import static org.hamcrest.Matchers.not;
import static org.hamcrest.Matchers.notNullValue;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.fail;
@@ -79,8 +74,6 @@ public abstract class IntegrationTest extends TestUtils {
private static final String TEST_DIRECTORY = "src/test/java";
private static final String TEST_DIRECTORY2 = "jadx-core/" + TEST_DIRECTORY;
private static final String OUT_DIR = "test-out-tmp";
private static final String DEFAULT_INPUT_PLUGIN = "dx";
/**
* Set 'TEST_INPUT_PLUGIN' env variable to use 'java' or 'dx' input in tests
@@ -132,7 +125,7 @@ public abstract class IntegrationTest extends TestUtils {
this.useJavaInput = null;
args = new JadxArgs();
args.setOutDir(new File(OUT_DIR));
args.setOutDir(new File("test-out-tmp"));
args.setShowInconsistentCode(true);
args.setThreadsCount(1);
args.setSkipResources(true);
@@ -156,6 +149,10 @@ public abstract class IntegrationTest extends TestUtils {
}
}
public void setOutDirSuffix(String suffix) {
args.setOutDir(new File("test-out-" + suffix + "-tmp"));
}
public String getTestName() {
return this.getClass().getSimpleName();
}
@@ -287,7 +284,7 @@ public abstract class IntegrationTest extends TestUtils {
}
protected void runChecks(List<ClassNode> clsList) {
clsList.forEach(this::checkCode);
clsList.forEach(cls -> checkCode(cls, allowWarnInCode));
compileClassNode(clsList);
clsList.forEach(this::runAutoCheck);
}
@@ -345,33 +342,6 @@ public abstract class IntegrationTest extends TestUtils {
root.processResources(resStorage);
}
protected void checkCode(ClassNode cls) {
assertFalse(hasErrors(cls), "Inconsistent cls: " + cls);
for (MethodNode mthNode : cls.getMethods()) {
if (hasErrors(mthNode)) {
fail("Method with problems: " + mthNode
+ "\n " + Utils.listToString(mthNode.getAttributesStringsList(), "\n "));
}
}
String code = cls.getCode().getCodeStr();
assertThat(code, not(containsString("inconsistent")));
assertThat(code, not(containsString("JADX ERROR")));
}
private boolean hasErrors(IAttributeNode node) {
if (node.contains(AFlag.INCONSISTENT_CODE) || node.contains(AType.JADX_ERROR)) {
return true;
}
if (!allowWarnInCode) {
JadxCommentsAttr commentsAttr = node.get(AType.JADX_COMMENTS);
if (commentsAttr != null) {
return commentsAttr.getComments().get(CommentsLevel.WARN) != null;
}
}
return false;
}
private void runAutoCheck(ClassNode cls) {
String clsName = cls.getClassInfo().getRawName().replace('/', '.');
try {
@@ -19,10 +19,15 @@ import static org.hamcrest.Matchers.notNullValue;
public abstract class SmaliTest extends IntegrationTest {
private static final String SMALI_TESTS_PROJECT = "jadx-core";
private static final String SMALI_TESTS_DIR = "src/test/smali";
private static final String SMALI_TESTS_EXT = ".smali";
private String currentProject = "jadx-core";
public void setCurrentProject(String currentProject) {
this.currentProject = currentProject;
}
@BeforeEach
public void init() {
Assumptions.assumeFalse(USE_JAVA_INPUT, "skip smali test for java input tests");
@@ -89,24 +94,24 @@ public abstract class SmaliTest extends IntegrationTest {
.collect(Collectors.toList());
}
private static File getSmaliFile(String baseName) {
private File getSmaliFile(String baseName) {
File smaliFile = new File(SMALI_TESTS_DIR, baseName + SMALI_TESTS_EXT);
if (smaliFile.exists()) {
return smaliFile;
}
File pathFromRoot = new File(SMALI_TESTS_PROJECT, smaliFile.getPath());
File pathFromRoot = new File(currentProject, smaliFile.getPath());
if (pathFromRoot.exists()) {
return pathFromRoot;
}
throw new AssertionError("Smali file not found: " + smaliFile.getPath());
}
private static File getSmaliDir(String baseName) {
private File getSmaliDir(String baseName) {
File smaliDir = new File(SMALI_TESTS_DIR, baseName);
if (smaliDir.exists()) {
return smaliDir;
}
File pathFromRoot = new File(SMALI_TESTS_PROJECT, smaliDir.getPath());
File pathFromRoot = new File(currentProject, smaliDir.getPath());
if (pathFromRoot.exists()) {
return pathFromRoot;
}
@@ -11,7 +11,6 @@ import java.nio.file.Path;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Locale;
import javax.tools.DiagnosticListener;
import javax.tools.JavaCompiler;
@@ -82,7 +81,7 @@ public class TestCompiler implements Closeable {
arguments.addAll(options.getArguments());
DiagnosticListener<? super JavaFileObject> diagnostic =
diagObj -> System.out.println("Compiler diagnostic: " + diagObj.getMessage(Locale.ROOT));
diagObj -> System.out.println("Compiler diagnostic: " + diagObj);
Writer out = new PrintWriter(System.out);
CompilationTask compilerTask = compiler.getTask(out, fileManager, diagnostic, arguments, null, jfObjects);
if (Boolean.FALSE.equals(compilerTask.call())) {
@@ -31,6 +31,10 @@ public enum TestProfile implements Consumer<IntegrationTest> {
test.useTargetJavaVersion(11);
test.useJavaInput();
}),
JAVA17("java-17", test -> {
test.useTargetJavaVersion(17);
test.useJavaInput();
}),
ECJ_DX_J8("ecj-dx-j8", test -> {
test.useEclipseCompiler();
test.useTargetJavaVersion(8);
@@ -52,8 +56,9 @@ public enum TestProfile implements Consumer<IntegrationTest> {
}
@Override
public void accept(IntegrationTest integrationTest) {
this.setup.accept(integrationTest);
public void accept(IntegrationTest test) {
this.setup.accept(test);
test.setOutDirSuffix(description);
}
public String getDescription() {
@@ -3,7 +3,21 @@ package jadx.tests.api.utils;
import org.junit.jupiter.api.extension.ExtendWith;
import jadx.NotYetImplementedExtension;
import jadx.api.CommentsLevel;
import jadx.api.ICodeWriter;
import jadx.core.dex.attributes.AFlag;
import jadx.core.dex.attributes.AType;
import jadx.core.dex.attributes.IAttributeNode;
import jadx.core.dex.attributes.nodes.JadxCommentsAttr;
import jadx.core.dex.nodes.ClassNode;
import jadx.core.dex.nodes.MethodNode;
import jadx.core.utils.Utils;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.containsString;
import static org.hamcrest.Matchers.not;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.fail;
@ExtendWith(NotYetImplementedExtension.class)
public class TestUtils {
@@ -35,4 +49,31 @@ public class TestUtils {
}
return count;
}
protected static void checkCode(ClassNode cls, boolean allowWarnInCode) {
assertFalse(hasErrors(cls, allowWarnInCode), "Inconsistent cls: " + cls);
for (MethodNode mthNode : cls.getMethods()) {
if (hasErrors(mthNode, allowWarnInCode)) {
fail("Method with problems: " + mthNode
+ "\n " + Utils.listToString(mthNode.getAttributesStringsList(), "\n "));
}
}
String code = cls.getCode().getCodeStr();
assertThat(code, not(containsString("inconsistent")));
assertThat(code, not(containsString("JADX ERROR")));
}
protected static boolean hasErrors(IAttributeNode node, boolean allowWarnInCode) {
if (node.contains(AFlag.INCONSISTENT_CODE) || node.contains(AType.JADX_ERROR)) {
return true;
}
if (!allowWarnInCode) {
JadxCommentsAttr commentsAttr = node.get(AType.JADX_COMMENTS);
if (commentsAttr != null) {
return commentsAttr.getComments().get(CommentsLevel.WARN) != null;
}
}
return false;
}
}
@@ -8,7 +8,6 @@ import org.assertj.core.api.Assertions;
import jadx.api.ICodeInfo;
import jadx.api.metadata.ICodeAnnotation;
import jadx.core.dex.nodes.ClassNode;
import jadx.core.dex.nodes.ICodeNode;
import jadx.tests.api.IntegrationTest;
import static jadx.tests.api.utils.assertj.JadxAssertions.assertThat;
@@ -58,11 +57,12 @@ public class JadxClassNodeAssertions extends AbstractObjectAssert<JadxClassNodeA
return this;
}
public void checkCodeAnnotationFor(String refStr, ICodeNode node) {
public JadxClassNodeAssertions checkCodeAnnotationFor(String refStr, ICodeAnnotation node) {
checkCodeAnnotationFor(refStr, 0, node);
return this;
}
public void checkCodeAnnotationFor(String refStr, int refOffset, ICodeNode node) {
public JadxClassNodeAssertions checkCodeAnnotationFor(String refStr, int refOffset, ICodeAnnotation node) {
ICodeInfo code = actual.getCode();
int codePos = code.getCodeStr().indexOf(refStr);
assertThat(codePos).describedAs("String '%s' not found", refStr).isNotEqualTo(-1);
@@ -70,9 +70,10 @@ public class JadxClassNodeAssertions extends AbstractObjectAssert<JadxClassNodeA
for (Map.Entry<Integer, ICodeAnnotation> entry : code.getCodeMetadata().getAsMap().entrySet()) {
if (entry.getKey() == refPos) {
Assertions.assertThat(entry.getValue()).isEqualTo(node);
return;
return this;
}
}
fail("Annotation for reference string: '%s' at position %d not found", refStr, refPos);
return this;
}
}
@@ -13,21 +13,24 @@ import jadx.api.ICodeWriter;
import jadx.api.JadxArgs;
import jadx.api.JadxDecompiler;
import jadx.api.JadxInternalAccess;
import jadx.api.metadata.ICodeAnnotation;
import jadx.api.metadata.ICodeNodeRef;
import jadx.core.dex.nodes.ClassNode;
import jadx.core.dex.nodes.MethodNode;
import jadx.core.dex.nodes.RootNode;
import jadx.core.utils.DebugChecks;
import jadx.core.utils.exceptions.JadxRuntimeException;
import jadx.tests.api.IntegrationTest;
import jadx.tests.api.utils.TestUtils;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.greaterThan;
import static org.hamcrest.Matchers.is;
public abstract class BaseExternalTest extends IntegrationTest {
public abstract class BaseExternalTest extends TestUtils {
private static final Logger LOG = LoggerFactory.getLogger(BaseExternalTest.class);
protected JadxDecompiler decompiler;
protected abstract String getSamplesDir();
protected JadxArgs prepare(String inputFile) {
@@ -55,16 +58,16 @@ public abstract class BaseExternalTest extends IntegrationTest {
}
protected JadxDecompiler decompile(JadxArgs jadxArgs, @Nullable String clsPatternStr, @Nullable String mthPatternStr) {
JadxDecompiler jadx = new JadxDecompiler(jadxArgs);
jadx.load();
decompiler = new JadxDecompiler(jadxArgs);
decompiler.load();
if (clsPatternStr == null) {
jadx.save();
decompiler.save();
} else {
processByPatterns(jadx, clsPatternStr, mthPatternStr);
processByPatterns(decompiler, clsPatternStr, mthPatternStr);
}
printErrorReport(jadx);
return jadx;
printErrorReport(decompiler);
return decompiler;
}
private void processByPatterns(JadxDecompiler jadx, String clsPattern, @Nullable String mthPattern) {
@@ -109,7 +112,7 @@ public abstract class BaseExternalTest extends IntegrationTest {
} else {
LOG.info("Code: \n{}", classNode.getCode());
}
checkCode(classNode);
checkCode(classNode, false);
return true;
}
@@ -134,7 +137,7 @@ public abstract class BaseExternalTest extends IntegrationTest {
String dashLine = "======================================================================================";
for (MethodNode mth : classNode.getMethods()) {
if (isMthMatch(mth, mthPattern)) {
String mthCode = cutMethodCode(codeInfo, code, mth);
String mthCode = cutMethodCode(codeInfo, mth);
LOG.info("Print method: {}\n{}\n{}\n{}", mth.getMethodInfo().getShortId(),
dashLine,
mthCode,
@@ -143,36 +146,33 @@ public abstract class BaseExternalTest extends IntegrationTest {
}
}
@NotNull
private String cutMethodCode(ICodeInfo codeInfo, String code, MethodNode mth) {
int defPos = mth.getDefPosition();
int startPos = getCommentStartPos(code, defPos);
ICodeNodeRef nodeBelow = codeInfo.getCodeMetadata().getNodeBelow(defPos);
int stopPos = nodeBelow == null ? code.length() : nodeBelow.getDefPosition();
int brackets = 0;
StringBuilder mthCode = new StringBuilder();
for (int i = startPos; i > 0 && i < stopPos;) {
int codePoint = code.codePointAt(i);
mthCode.appendCodePoint(codePoint);
if (i >= defPos) {
// also count brackets for detect method end
if (codePoint == '{') {
brackets++;
} else if (codePoint == '}') {
brackets--;
if (brackets <= 0) {
break;
}
}
}
i += Character.charCount(codePoint);
}
return mthCode.toString();
private String cutMethodCode(ICodeInfo codeInfo, MethodNode mth) {
int startPos = getCommentStartPos(codeInfo, mth.getDefPosition());
int stopPos = getNextNodePos(mth, codeInfo);
return codeInfo.getCodeStr().substring(startPos, stopPos);
}
protected int getCommentStartPos(String code, int pos) {
private int getNextNodePos(MethodNode mth, ICodeInfo codeInfo) {
int pos = mth.getDefPosition() + 1;
while (true) {
ICodeNodeRef nodeBelow = codeInfo.getCodeMetadata().getNodeBelow(pos);
if (nodeBelow == null) {
return codeInfo.getCodeStr().length();
}
if (nodeBelow.getAnnType() != ICodeAnnotation.AnnType.METHOD) {
return nodeBelow.getDefPosition();
}
MethodNode nodeMth = (MethodNode) nodeBelow;
if (nodeMth.getParentClass().equals(mth.getParentClass())) { // skip methods from anonymous classes
return getCommentStartPos(codeInfo, nodeMth.getDefPosition());
}
pos = nodeMth.getDefPosition() + 1;
}
}
protected int getCommentStartPos(ICodeInfo codeInfo, int pos) {
String emptyLine = ICodeWriter.NL + ICodeWriter.NL;
int emptyLinePos = code.lastIndexOf(emptyLine, pos);
int emptyLinePos = codeInfo.getCodeStr().lastIndexOf(emptyLine, pos);
return emptyLinePos == -1 ? pos : emptyLinePos + emptyLine.length();
}
@@ -1,8 +1,10 @@
package jadx.tests.integration.enums;
import java.util.Collections;
import org.junit.jupiter.api.Test;
import jadx.core.dex.nodes.ClassNode;
import jadx.api.CommentsLevel;
import jadx.tests.api.SmaliTest;
import static jadx.tests.api.utils.assertj.JadxAssertions.assertThat;
@@ -32,14 +34,31 @@ public class TestEnumObfuscated extends SmaliTest {
public synthetic int getNum() {
return this.num;
}
// custom values method
// should be kept and renamed to avoid collision to enum 'values()' method
public static int values() {
return new TestEnumObfuscated[0];
}
// usage of renamed 'values()' method, should be renamed back to 'values'
public static int valuesCount() {
return vs().length;
}
// usage of renamed '$VALUES' field, should be replaced with 'values()' method call
public static int valuesFieldUse() {
return $VLS.length;
}
}
*/
// @formatter:on
@Test
public void test() {
ClassNode cls = getClassNodeFromSmali();
assertThat(cls)
getArgs().setCommentsLevel(CommentsLevel.WARN);
getArgs().setRenameFlags(Collections.emptySet());
assertThat(getClassNodeFromSmali())
.code()
.doesNotContain("$VLS")
.doesNotContain("vo(")
@@ -56,8 +56,6 @@ public class TestGenericsMthOverride extends IntegrationTest {
assertThat(code, containsOne("public Y method(Exception x) {"));
assertThat(code, containsOne("public Object method(Object x) {"));
assertThat(code, countString(3, "@Override"));
// TODO: @Override missing for class C
// assertThat(code, countString(4, "@Override"));
assertThat(code, countString(4, "@Override"));
}
}
@@ -0,0 +1,47 @@
package jadx.tests.integration.inline;
import java.util.Collections;
import org.junit.jupiter.api.Test;
import jadx.tests.api.SmaliTest;
import static jadx.tests.api.utils.assertj.JadxAssertions.assertThat;
@SuppressWarnings("CommentedOutCode")
public class TestOverlapSyntheticMethods extends SmaliTest {
// @formatter:off
/*
public String test(int i) {
return a(i) + "|" + a(i);
}
public int a(int i) {
return i;
}
public String a(int i) {
return "i:" + i;
}
*/
// @formatter:on
@Test
public void testSmali() {
assertThat(getClassNodeFromSmali())
.code()
.containsOne("int a(int i) {")
.containsOne("String m0a(int i) {");
}
@Test
public void testSmaliNoRename() {
getArgs().setRenameFlags(Collections.emptySet());
disableCompilation();
assertThat(getClassNodeFromSmali())
.code()
.containsOne("int a(int i) {")
.containsOne("String a(int i) {")
.containsOne("return a(i) + \"|\" + a(i);");
}
}
@@ -0,0 +1,45 @@
package jadx.tests.integration.inline;
import java.util.function.Function;
import org.junit.jupiter.api.Test;
import jadx.api.metadata.ICodeAnnotation;
import jadx.api.metadata.annotations.NodeDeclareRef;
import jadx.core.dex.nodes.ClassNode;
import jadx.tests.api.SmaliTest;
import static jadx.tests.api.utils.assertj.JadxAssertions.assertThat;
public class TestOverrideBridgeMerge extends SmaliTest {
public static class TestCls implements Function<String, Integer> {
@Override
public /* bridge */ /* synthetic */ Integer apply(String str) {
return test(str);
}
public Integer test(String str) {
return str.length();
}
}
@Test
public void test() {
assertThat(getClassNode(TestCls.class))
.code()
.containsOne("Integer test(String str) {"); // not inlined
}
@Test
public void testSmali() {
ClassNode cls = getClassNodeFromSmali();
ICodeAnnotation mthDef = new NodeDeclareRef(getMethod(cls, "apply"));
assertThat(cls)
.checkCodeAnnotationFor("apply(String str) {", mthDef)
.code()
.containsOne("@Override")
.containsOne("public Integer apply(String str) {")
.doesNotContain("test(String str)");
}
}
@@ -0,0 +1,42 @@
package jadx.tests.integration.invoke;
import org.junit.jupiter.api.Test;
import jadx.tests.api.IntegrationTest;
import static jadx.tests.api.utils.assertj.JadxAssertions.assertThat;
public class TestSuperInvokeUnknown extends IntegrationTest {
public static class TestCls {
public static class BaseClass {
public int doSomething() {
return 0;
}
}
public static class NestedClass extends BaseClass {
@Override
public int doSomething() {
return super.doSomething();
}
}
}
@Test
public void test() {
disableCompilation();
noDebugInfo();
assertThat(getClassNode(TestCls.NestedClass.class)) // BaseClass unknown
.code()
.containsOne("return super.doSomething();");
}
@Test
public void testTopCls() {
noDebugInfo();
assertThat(getClassNode(TestCls.class))
.code()
.containsOne("return super.doSomething();");
}
}
@@ -0,0 +1,59 @@
package jadx.tests.integration.names;
import java.util.List;
import org.junit.jupiter.api.Test;
import jadx.core.dex.nodes.ClassNode;
import jadx.tests.api.IntegrationTest;
import static jadx.tests.api.utils.assertj.JadxAssertions.assertThat;
public class TestCollisionWithJavaLangClasses extends IntegrationTest {
public static class TestCls1 {
public static class System {
public static void main(String[] args) {
java.lang.System.out.println("Hello world");
}
}
}
@Test
public void test1() {
assertThat(getClassNode(TestCls1.class))
.code()
.containsOne("java.lang.System.out.println");
}
public static class TestCls2 {
public void doSomething() {
System.doSomething();
java.lang.System.out.println("Hello World");
}
public static class System {
public static void doSomething() {
}
}
}
@Test
public void test2() {
assertThat(getClassNode(TestCls2.class))
.code()
.containsLine(2, "System.doSomething();")
.containsOne("java.lang.System.out.println");
}
@Test
public void test3() {
List<ClassNode> classes = getClassNodes(
jadx.tests.integration.names.pkg2.System.class,
jadx.tests.integration.names.pkg2.TestCls.class);
assertThat(searchCls(classes, "TestCls"))
.code()
.containsLine(2, "System.doSomething();")
.containsOne("java.lang.System.out.println");
}
}
@@ -0,0 +1,6 @@
package jadx.tests.integration.names.pkg2;
public class System {
public static void doSomething() {
}
}
@@ -0,0 +1,8 @@
package jadx.tests.integration.names.pkg2;
public class TestCls {
public void doSomething() {
System.doSomething();
java.lang.System.out.println("Hello World");
}
}
@@ -1,6 +1,7 @@
package jadx.tests.integration.others;
import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;
import org.junit.jupiter.api.Test;
@@ -8,6 +9,7 @@ import jadx.api.JadxInternalAccess;
import jadx.api.JavaClass;
import jadx.api.JavaMethod;
import jadx.api.metadata.ICodeAnnotation;
import jadx.api.metadata.ICodeAnnotation.AnnType;
import jadx.api.metadata.ICodeMetadata;
import jadx.api.metadata.ICodeNodeRef;
import jadx.core.dex.nodes.ClassNode;
@@ -53,14 +55,25 @@ public class TestCodeMetadata extends IntegrationTest {
int callUse = callUsePlaces.get(0);
ICodeMetadata metadata = cls.getCode().getCodeMetadata();
System.out.println(metadata);
ICodeNodeRef callDef = metadata.getNodeAt(callUse);
assertThat(callDef).isSameAs(testMth);
int beforeCallDef = callDefPos - 10;
ICodeAnnotation closest = metadata.getClosestUp(beforeCallDef);
AtomicInteger endPos = new AtomicInteger();
ICodeAnnotation testEnd = metadata.searchUp(callDefPos, (pos, ann) -> {
if (ann.getAnnType() == AnnType.END) {
endPos.set(pos);
return ann;
}
return null;
});
assertThat(testEnd).isNotNull();
int testEndPos = endPos.get();
ICodeAnnotation closest = metadata.getClosestUp(testEndPos);
assertThat(closest).isInstanceOf(FieldNode.class); // field reference from 'return a.str;'
ICodeNodeRef nodeBelow = metadata.getNodeBelow(beforeCallDef);
ICodeNodeRef nodeBelow = metadata.getNodeBelow(testEndPos);
assertThat(nodeBelow).isSameAs(callMth);
}
}
@@ -0,0 +1,62 @@
package jadx.tests.integration.others;
import java.util.List;
import org.junit.jupiter.api.Test;
import jadx.api.JavaClass;
import jadx.api.JavaMethod;
import jadx.api.metadata.ICodeMetadata;
import jadx.core.dex.nodes.ClassNode;
import jadx.core.dex.nodes.MethodNode;
import jadx.tests.api.IntegrationTest;
import static jadx.api.JadxInternalAccess.convertClassNode;
import static jadx.api.JadxInternalAccess.convertMethodNode;
import static jadx.tests.api.utils.assertj.JadxAssertions.assertThat;
public class TestCodeMetadata2 extends IntegrationTest {
public static class TestCls {
@SuppressWarnings("Convert2Lambda")
public Runnable test(boolean a) {
if (a) {
return new Runnable() {
@Override
public void run() {
System.out.println("test");
}
};
}
System.out.println("another");
return empty();
}
public static Runnable empty() {
return new Runnable() {
@Override
public void run() {
// empty
}
};
}
}
@Test
public void test() {
ClassNode cls = getClassNode(TestCls.class);
assertThat(cls).code().containsOne("return empty();");
MethodNode testMth = getMethod(cls, "test");
MethodNode emptyMth = getMethod(cls, "empty");
JavaClass javaClass = convertClassNode(jadxDecompiler, cls);
JavaMethod emptyJavaMethod = convertMethodNode(jadxDecompiler, emptyMth);
List<Integer> emptyUsePlaces = javaClass.getUsePlacesFor(javaClass.getCodeInfo(), emptyJavaMethod);
assertThat(emptyUsePlaces).hasSize(1);
int callUse = emptyUsePlaces.get(0);
ICodeMetadata metadata = cls.getCode().getCodeMetadata();
assertThat(metadata.getNodeAt(callUse)).isSameAs(testMth);
}
}
@@ -26,7 +26,6 @@ public class TestMoveInline extends SmaliTest {
@Test
public void test() {
getArgs().setRawCFGOutput(true);
assertThat(getClassNodeFromSmali())
.code()
// check operations order
@@ -0,0 +1,45 @@
package jadx.tests.integration.trycatch;
import jadx.tests.api.IntegrationTest;
import jadx.tests.api.extensions.profiles.TestProfile;
import jadx.tests.api.extensions.profiles.TestWithProfiles;
import static jadx.tests.api.utils.assertj.JadxAssertions.assertThat;
public class TestTryCatchFinally14 extends IntegrationTest {
@SuppressWarnings("unused")
public static class TestCls {
private TCls t;
public void test() {
try {
if (t != null) {
t.doSomething();
}
} finally {
if (t != null) {
t.doFinally();
}
}
}
private static class TCls {
public void doSomething() {
}
public void doFinally() {
}
}
}
@TestWithProfiles({ TestProfile.DX_J8, TestProfile.D8_J11, TestProfile.JAVA8 })
public void test() {
assertThat(getClassNode(TestCls.class))
.code()
.containsOne(".doSomething();")
.containsOne("} finally {")
.containsOne(".doFinally();")
.countString(2, "!= null) {");
}
}
@@ -25,8 +25,6 @@ public class TestTryCatchMultiException extends IntegrationTest {
@Test
public void test() {
// printDisassemble();
// setFallback();
noDebugInfo();
ClassNode cls = getClassNode(TestCls.class);
String code = cls.getCode().toString();
@@ -0,0 +1,33 @@
package jadx.tests.integration.trycatch;
import org.junit.jupiter.api.Test;
import jadx.tests.api.SmaliTest;
import static jadx.tests.api.utils.assertj.JadxAssertions.assertThat;
@SuppressWarnings("CommentedOutCode")
public class TestTryCatchMultiException2 extends SmaliTest {
// @formatter:off
/*
public static boolean test() {
try {
Class<?> cls = Class.forName("c");
return ((Boolean) cls.getMethod("b", new Class[0]).invoke(cls, new Object[0])).booleanValue();
} catch (ClassNotFoundException | NoSuchMethodException | Exception | Throwable unused) {
// java compiler don't allow shadow subclasses in multi-catch
// in this case leave only Throwable
return false;
}
}
*/
// @formatter:on
@Test
public void test() {
assertThat(getClassNodeFromSmali())
.code()
.containsOne("} catch (Throwable unused) {");
}
}
@@ -0,0 +1,29 @@
package jadx.tests.integration.types;
import org.junit.jupiter.api.Test;
import jadx.tests.api.SmaliTest;
import static jadx.tests.api.utils.assertj.JadxAssertions.assertThat;
/**
* Issue 1527
*/
@SuppressWarnings("CommentedOutCode")
public class TestTypeResolver21 extends SmaliTest {
// @formatter:off
/*
public Number test(Object objectArray) {
Object[] arr = (Object[]) objectArray;
return (Number) arr[0];
}
*/
// @formatter:on
@Test
public void test() {
assertThat(getClassNodeFromSmali())
.code()
.containsOne("Object[] arr = (Object[]) objectArray;");
}
}
@@ -0,0 +1,29 @@
package jadx.tests.integration.types;
import java.io.IOException;
import java.io.InputStream;
import jadx.tests.api.IntegrationTest;
import jadx.tests.api.extensions.profiles.TestProfile;
import jadx.tests.api.extensions.profiles.TestWithProfiles;
import static jadx.tests.api.utils.assertj.JadxAssertions.assertThat;
public class TestTypeResolver22 extends IntegrationTest {
public static class TestCls {
public void test(InputStream input, long count) throws IOException {
long pos = input.skip(count);
while (pos < count) {
pos += input.skip(count - pos);
}
}
}
@TestWithProfiles({ TestProfile.JAVA8, TestProfile.DX_J8 })
public void test() {
assertThat(getClassNode(TestCls.class))
.code()
.containsOne("long pos = ");
}
}
@@ -0,0 +1,30 @@
package jadx.tests.integration.types;
import jadx.tests.api.IntegrationTest;
import jadx.tests.api.extensions.profiles.TestProfile;
import jadx.tests.api.extensions.profiles.TestWithProfiles;
import static jadx.tests.api.utils.assertj.JadxAssertions.assertThat;
public class TestTypeResolver23 extends IntegrationTest {
public static class TestCls {
public long test(int a) {
long v = 1L;
if (a == 2) {
v = 2L;
} else if (a == 3) {
v = 3L;
}
System.out.println(v);
return v;
}
}
@TestWithProfiles({ TestProfile.JAVA8, TestProfile.DX_J8 })
public void test() {
assertThat(getClassNode(TestCls.class))
.code()
.containsOne("long v");
}
}
@@ -72,6 +72,27 @@
return-object v0
.end method
.method public static values()[Lenums/TestEnumObfuscated;
.registers 1
sget-object v0, Lenums/TestEnumObfuscated;->$VLS:[Lenums/TestEnumObfuscated;
return v0
.end method
.method public static valuesCount()I
.registers 2
invoke-static {v0, p0}, Lenums/TestEnumObfuscated;->vs()[Lenums/TestEnumObfuscated;
move-result-object v0
array-length v1, v0
return v1
.end method
.method public static valuesFieldUse()I
.registers 2
sget-object v0, Lenums/TestEnumObfuscated;->$VLS:[Lenums/TestEnumObfuscated;
array-length v1, v0
return v1
.end method
.method public synthetic getNum()I
.registers 2
@@ -0,0 +1,41 @@
.class public Linline/TestOverlapSyntheticMethods;
.super Ljava/lang/Object;
.method public synthetic a(I)I
.registers 2
return p1
.end method
.method public synthetic a(I)Ljava/lang/String;
.registers 4
new-instance v0, Ljava/lang/StringBuilder;
invoke-direct {v0}, Ljava/lang/StringBuilder;-><init>()V
const-string v1, "i:"
invoke-virtual {v0, v1}, Ljava/lang/StringBuilder;->append(Ljava/lang/String;)Ljava/lang/StringBuilder;
move-result-object v0
invoke-virtual {v0, p1}, Ljava/lang/StringBuilder;->append(I)Ljava/lang/StringBuilder;
move-result-object v0
invoke-virtual {v0}, Ljava/lang/StringBuilder;->toString()Ljava/lang/String;
move-result-object v0
return-object v0
.end method
.method public test(I)Ljava/lang/String;
.registers 4
new-instance v0, Ljava/lang/StringBuilder;
invoke-direct {v0}, Ljava/lang/StringBuilder;-><init>()V
invoke-virtual {p0, p1}, Linline/TestOverlapSyntheticMethods;->a(I)I
move-result v1
invoke-virtual {v0, v1}, Ljava/lang/StringBuilder;->append(I)Ljava/lang/StringBuilder;
move-result-object v0
const-string v1, "|"
invoke-virtual {v0, v1}, Ljava/lang/StringBuilder;->append(Ljava/lang/String;)Ljava/lang/StringBuilder;
move-result-object v0
invoke-virtual {p0, p1}, Linline/TestOverlapSyntheticMethods;->a(I)Ljava/lang/String;
move-result-object v1
invoke-virtual {v0, v1}, Ljava/lang/StringBuilder;->append(Ljava/lang/String;)Ljava/lang/StringBuilder;
move-result-object v0
invoke-virtual {v0}, Ljava/lang/StringBuilder;->toString()Ljava/lang/String;
move-result-object v0
return-object v0
.end method
@@ -0,0 +1,39 @@
.class public Linline/TestOverrideBridgeMerge;
.super Ljava/lang/Object;
.implements Ljava/util/function/Function;
.annotation system Ldalvik/annotation/Signature;
value = {
"Ljava/lang/Object;",
"Ljava/util/function/Function",
"<",
"Ljava/lang/String;",
"Ljava/lang/Integer;",
">;"
}
.end annotation
.method public constructor <init>()V
.registers 1
invoke-direct {p0}, Ljava/lang/Object;-><init>()V
return-void
.end method
.method public bridge synthetic apply(Ljava/lang/Object;)Ljava/lang/Object;
.registers 3
check-cast p1, Ljava/lang/String;
invoke-virtual {p0, p1}, Linline/TestOverrideBridgeMerge;->test(Ljava/lang/String;)Ljava/lang/Integer;
move-result-object v0
return-object v0
.end method
.method public test(Ljava/lang/String;)Ljava/lang/Integer;
.registers 3
.param p1, "str" # Ljava/lang/String;
invoke-virtual {p1}, Ljava/lang/String;->length()I
move-result v0
invoke-static {v0}, Ljava/lang/Integer;->valueOf(I)Ljava/lang/Integer;
move-result-object v0
return-object v0
.end method
@@ -0,0 +1,38 @@
.class public Ltrycatch/TestTryCatchMultiException2;
.super Ljava/lang/Object;
.method public static test()Z
.registers 5
:try_start_b
const-string v0, "c"
invoke-static {v0}, Ljava/lang/Class;->forName(Ljava/lang/String;)Ljava/lang/Class;
move-result-object v1
const/4 v0, 0x0
const-string v2, "b"
new-array v3, v0, [Ljava/lang/Class;
invoke-virtual {v1, v2, v3}, Ljava/lang/Class;->getMethod(Ljava/lang/String;[Ljava/lang/Class;)Ljava/lang/reflect/Method;
move-result-object v2
new-array v3, v0, [Ljava/lang/Object;
invoke-virtual {v2, v1, v3}, Ljava/lang/reflect/Method;->invoke(Ljava/lang/Object;[Ljava/lang/Object;)Ljava/lang/Object;
move-result-object v1
check-cast v1, Ljava/lang/Boolean;
invoke-virtual {v1}, Ljava/lang/Boolean;->booleanValue()Z
move-result v1
:try_end_2f
.catch Ljava/lang/ClassNotFoundException; {:try_start_b .. :try_end_2f} :catch_30
.catch Ljava/lang/NoSuchMethodException; {:try_start_b .. :try_end_2f} :catch_30
.catch Ljava/lang/Exception; {:try_start_b .. :try_end_2f} :catch_30
.catchall {:try_start_b .. :try_end_2f} :catchall_30
return v1
:catch_30
:catchall_30
return v0
.end method
@@ -0,0 +1,23 @@
.class public Ltypes/TestTypeResolver21;
.super Ljava/lang/Object;
.source "TestTypeResolver21.java"
.method public test(Ljava/lang/Object;)Ljava/lang/Number;
.registers 4
.param p1, "objectArray" # Ljava/lang/Object;
.prologue
.line 16
check-cast p1, [Ljava/lang/Object;
.end local p1 # "objectArray":Ljava/lang/Object;
move-object v0, p1
check-cast v0, [Ljava/lang/Object;
.line 17
.local v0, "arr":[Ljava/lang/Object;
const/4 v1, 0x0
aget-object v1, v0, v1
check-cast v1, Ljava/lang/Number;
return-object v1
.end method
+9 -5
View File
@@ -7,7 +7,6 @@ plugins {
dependencies {
implementation(project(':jadx-core'))
implementation(project(":jadx-cli"))
implementation 'com.beust:jcommander:1.82'
implementation 'ch.qos.logback:logback-classic:1.2.11'
@@ -16,9 +15,9 @@ dependencies {
implementation files('libs/jfontchooser-1.0.5.jar')
implementation 'hu.kazocsaba:image-viewer:1.2.3'
implementation 'com.formdev:flatlaf:2.3'
implementation 'com.formdev:flatlaf-intellij-themes:2.3'
implementation 'com.formdev:flatlaf-extras:2.3'
implementation 'com.formdev:flatlaf:2.4'
implementation 'com.formdev:flatlaf-intellij-themes:2.4'
implementation 'com.formdev:flatlaf-extras:2.4'
implementation 'com.formdev:svgSalamander:1.1.3'
implementation 'com.google.code.gson:gson:2.9.0'
@@ -27,9 +26,14 @@ dependencies {
implementation 'io.reactivex.rxjava2:rxjava:2.2.21'
implementation "com.github.akarnokd:rxjava2-swing:0.3.7"
implementation 'com.android.tools.build:apksig:4.2.1'
implementation 'com.android.tools.build:apksig:7.2.1'
implementation 'io.github.hqktech:jdwp:1.0'
// TODO: Switch back to upstream once this PR gets merged:
// https://github.com/FabricMC/mapping-io/pull/19
// implementation 'net.fabricmc:mapping-io:0.3.0'
implementation files('libs/mapping-io-0.4.0-SNAPSHOT.jar')
testImplementation project(":jadx-core").sourceSets.test.output
}
Binary file not shown.
@@ -6,18 +6,27 @@ import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import jadx.api.ICodeInfo;
import jadx.api.JadxArgs;
import jadx.api.JadxDecompiler;
import jadx.api.JavaClass;
import jadx.api.JavaNode;
import jadx.api.JavaPackage;
import jadx.api.ResourceFile;
import jadx.api.impl.InMemoryCodeCache;
import jadx.api.metadata.ICodeNodeRef;
import jadx.api.plugins.JadxPlugin;
import jadx.api.plugins.JadxPluginManager;
import jadx.core.dex.nodes.ClassNode;
import jadx.core.dex.nodes.ProcessState;
import jadx.core.dex.nodes.RootNode;
import jadx.core.dex.visitors.rename.RenameVisitor;
import jadx.core.utils.exceptions.JadxRuntimeException;
import jadx.core.utils.files.FileUtils;
import jadx.gui.settings.JadxProject;
import jadx.gui.settings.JadxSettings;
@@ -30,11 +39,14 @@ import static jadx.core.dex.nodes.ProcessState.GENERATED_AND_UNLOADED;
import static jadx.core.dex.nodes.ProcessState.NOT_LOADED;
import static jadx.core.dex.nodes.ProcessState.PROCESS_COMPLETE;
@SuppressWarnings("ConstantConditions")
public class JadxWrapper {
private static final Logger LOG = LoggerFactory.getLogger(JadxWrapper.class);
private static final Object DECOMPILER_UPDATE_SYNC = new Object();
private final MainWindow mainWindow;
private JadxDecompiler decompiler;
private volatile @Nullable JadxDecompiler decompiler;
public JadxWrapper(MainWindow mainWindow) {
this.mainWindow = mainWindow;
@@ -43,23 +55,25 @@ public class JadxWrapper {
public void open() {
close();
try {
JadxProject project = getProject();
JadxArgs jadxArgs = getSettings().toJadxArgs();
jadxArgs.setInputFiles(FileUtils.toFiles(project.getFilePaths()));
jadxArgs.setCodeData(project.getCodeData());
synchronized (DECOMPILER_UPDATE_SYNC) {
JadxProject project = getProject();
JadxArgs jadxArgs = getSettings().toJadxArgs();
jadxArgs.setInputFiles(FileUtils.toFiles(project.getFilePaths()));
jadxArgs.setCodeData(project.getCodeData());
this.decompiler = new JadxDecompiler(jadxArgs);
this.decompiler.load();
initCodeCache();
this.decompiler = new JadxDecompiler(jadxArgs);
this.decompiler.load();
initCodeCache();
}
} catch (Exception e) {
LOG.error("Jadx init error", e);
LOG.error("Jadx decompiler wrapper init error", e);
close();
}
}
// TODO: check and move into core package
public void unloadClasses() {
for (ClassNode cls : decompiler.getRoot().getClasses()) {
for (ClassNode cls : getDecompiler().getRoot().getClasses()) {
ProcessState clsState = cls.getState();
cls.unload();
cls.setState(clsState == PROCESS_COMPLETE ? GENERATED_AND_UNLOADED : NOT_LOADED);
@@ -68,14 +82,16 @@ public class JadxWrapper {
public void close() {
try {
if (decompiler != null) {
decompiler.close();
mainWindow.getCacheObject().reset();
synchronized (DECOMPILER_UPDATE_SYNC) {
if (decompiler != null) {
decompiler.close();
decompiler = null;
}
}
} catch (Exception e) {
LOG.error("Jadx decompiler close error", e);
} finally {
decompiler = null;
mainWindow.getCacheObject().reset();
}
}
@@ -94,7 +110,7 @@ public class JadxWrapper {
}
private BufferCodeCache buildBufferedDiskCache() {
DiskCodeCache diskCache = new DiskCodeCache(decompiler.getRoot(), getProject().getCacheDir());
DiskCodeCache diskCache = new DiskCodeCache(getDecompiler().getRoot(), getProject().getCacheDir());
return new BufferCodeCache(diskCache);
}
@@ -102,14 +118,14 @@ public class JadxWrapper {
* Get the complete list of classes
*/
public List<JavaClass> getClasses() {
return decompiler.getClasses();
return getDecompiler().getClasses();
}
/**
* Get all classes that are not excluded by the excluded packages settings
*/
public List<JavaClass> getIncludedClasses() {
List<JavaClass> classList = decompiler.getClasses();
List<JavaClass> classList = getDecompiler().getClasses();
List<String> excludedPackages = getExcludedPackages();
if (excludedPackages.isEmpty()) {
return classList;
@@ -123,7 +139,7 @@ public class JadxWrapper {
* Get all classes that are not excluded by the excluded packages settings including inner classes
*/
public List<JavaClass> getIncludedClassesWithInners() {
List<JavaClass> classes = decompiler.getClassesWithInners();
List<JavaClass> classes = getDecompiler().getClassesWithInners();
List<String> excludedPackages = getExcludedPackages();
if (excludedPackages.isEmpty()) {
return classes;
@@ -145,7 +161,7 @@ public class JadxWrapper {
}
public List<List<JavaClass>> buildDecompileBatches(List<JavaClass> classes) {
return decompiler.getDecompileScheduler().buildBatches(classes);
return getDecompiler().getDecompileScheduler().buildBatches(classes);
}
// TODO: move to CLI and filter classes in JadxDecompiler
@@ -175,20 +191,61 @@ public class JadxWrapper {
getSettings().sync();
}
public List<JavaPackage> getPackages() {
return decompiler.getPackages();
public List<JadxPlugin> getAllPlugins() {
if (decompiler != null) {
return decompiler.getPluginManager().getAllPlugins();
}
JadxPluginManager pluginManager = new JadxPluginManager();
pluginManager.load();
return pluginManager.getAllPlugins();
}
public List<ResourceFile> getResources() {
return decompiler.getResources();
}
public JadxDecompiler getDecompiler() {
/**
* TODO: make method private
* Do not store JadxDecompiler in fields to not leak old instances
*/
public @NotNull JadxDecompiler getDecompiler() {
if (decompiler == null || decompiler.getRoot() == null) {
throw new JadxRuntimeException("Decompiler not yet loaded");
}
return decompiler;
}
// TODO: forbid usage of this method
public RootNode getRootNode() {
return getDecompiler().getRoot();
}
public void reInitRenameVisitor() {
new RenameVisitor().init(getRootNode());
}
public void reloadCodeData() {
getDecompiler().reloadCodeData();
}
public JavaNode getJavaNodeByRef(ICodeNodeRef nodeRef) {
return getDecompiler().getJavaNodeByRef(nodeRef);
}
public @Nullable JavaNode getEnclosingNode(ICodeInfo codeInfo, int pos) {
return getDecompiler().getEnclosingNode(codeInfo, pos);
}
public List<Runnable> getSaveTasks() {
return getDecompiler().getSaveTasks();
}
public List<JavaPackage> getPackages() {
return getDecompiler().getPackages();
}
public List<ResourceFile> getResources() {
return getDecompiler().getResources();
}
public JadxArgs getArgs() {
return decompiler.getArgs();
return getDecompiler().getArgs();
}
public JadxProject getProject() {
@@ -204,14 +261,14 @@ public class JadxWrapper {
* Full name of an outer class. Inner classes are not supported.
*/
public @Nullable JavaClass searchJavaClassByFullAlias(String fullName) {
return decompiler.getClasses().stream()
return getDecompiler().getClasses().stream()
.filter(cls -> cls.getFullName().equals(fullName))
.findFirst()
.orElse(null);
}
public @Nullable JavaClass searchJavaClassByOrigClassName(String fullName) {
return decompiler.searchJavaClassByOrigFullName(fullName);
return getDecompiler().searchJavaClassByOrigFullName(fullName);
}
/**
@@ -219,7 +276,7 @@ public class JadxWrapper {
* Full raw name of an outer class. Inner classes are not supported.
*/
public @Nullable JavaClass searchJavaClassByRawName(String rawName) {
return decompiler.getClasses().stream()
return getDecompiler().getClasses().stream()
.filter(cls -> cls.getRawName().equals(rawName))
.findFirst()
.orElse(null);
@@ -160,7 +160,7 @@ public class DbgUtils {
// TODO: parse AndroidManifest.xml instead of looking for keywords
private static String getManifestContent(MainWindow mainWindow) {
try {
ResourceFile androidManifest = mainWindow.getWrapper().getDecompiler().getResources()
ResourceFile androidManifest = mainWindow.getWrapper().getResources()
.stream()
.filter(res -> res.getType() == ResourceType.MANIFEST)
.findFirst()
@@ -46,6 +46,7 @@ import jadx.core.dex.instructions.args.RegisterArg;
import jadx.core.dex.nodes.ClassNode;
import jadx.core.dex.nodes.InsnNode;
import jadx.core.dex.nodes.MethodNode;
import jadx.core.utils.exceptions.JadxRuntimeException;
import static jadx.api.plugins.input.data.AccessFlagsScope.FIELD;
import static jadx.api.plugins.input.data.AccessFlagsScope.METHOD;
@@ -60,7 +61,9 @@ import static jadx.api.plugins.input.insns.Opcode.INVOKE_CUSTOM;
import static jadx.api.plugins.input.insns.Opcode.INVOKE_CUSTOM_RANGE;
import static jadx.api.plugins.input.insns.Opcode.INVOKE_POLYMORPHIC;
import static jadx.api.plugins.input.insns.Opcode.INVOKE_POLYMORPHIC_RANGE;
import static jadx.api.plugins.input.insns.Opcode.PACKED_SWITCH;
import static jadx.api.plugins.input.insns.Opcode.PACKED_SWITCH_PAYLOAD;
import static jadx.api.plugins.input.insns.Opcode.SPARSE_SWITCH;
import static jadx.api.plugins.input.insns.Opcode.SPARSE_SWITCH_PAYLOAD;
public class Smali {
@@ -288,6 +291,14 @@ public class Smali {
if (codeReader.getDebugInfo() != null) {
formatDbgInfo(codeReader.getDebugInfo(), line);
}
// first pass to fill payload offsets for switch instructions
codeReader.visitInstructions(insn -> {
Opcode opcode = insn.getOpcode();
if (opcode == PACKED_SWITCH || opcode == SPARSE_SWITCH) {
insn.decode();
line.addPayloadOffset(insn.getOffset(), insn.getTarget());
}
});
codeReader.visitInstructions(insn -> {
InsnNode node = decodeInsn(insn, line);
nodes.put((long) insn.getOffset(), node);
@@ -404,7 +415,6 @@ public class Smali {
line.addTip(target, String.format(FMT_S_SWITCH_TAG, target), "");
line.getLineWriter().append(", ").append(String.format(FMT_S_SWITCH, target));
}
line.addPayloadOffset(insn.getOffset(), target);
return true;
}
}
@@ -733,7 +743,7 @@ public class Smali {
ISwitchPayload payload = (ISwitchPayload) insn.getPayload();
if (payload != null) {
fmtSwitchPayload(insn, FMT_P_SWITCH_CASE, FMT_P_SWITCH_CASE_TAG, line, payload, insn.getOffset());
fmtSwitchPayload(insn, FMT_P_SWITCH_CASE, FMT_P_SWITCH_CASE_TAG, line, payload);
}
return true;
}
@@ -743,7 +753,7 @@ public class Smali {
ISwitchPayload payload = (ISwitchPayload) insn.getPayload();
if (payload != null) {
fmtSwitchPayload(insn, FMT_S_SWITCH_CASE, FMT_S_SWITCH_CASE_TAG, line, payload, insn.getOffset());
fmtSwitchPayload(insn, FMT_S_SWITCH_CASE, FMT_S_SWITCH_CASE_TAG, line, payload);
}
return true;
}
@@ -755,17 +765,19 @@ public class Smali {
return false;
}
private void fmtSwitchPayload(InsnData insn, String fmtTarget, String fmtTag, LineInfo line,
ISwitchPayload payload, int curOffset) {
private void fmtSwitchPayload(InsnData insn, String fmtTarget, String fmtTag, LineInfo line, ISwitchPayload payload) {
int lineStart = getInsnColStart();
lineStart += CODE_OFFSET_COLUMN_WIDTH + 1 + 1; // plus 1s for space and the ':'
String basicIndent = new String(new byte[lineStart]).replace("\0", " ");
String indent = SmaliWriter.INDENT_STR + basicIndent;
int[] keys = payload.getKeys();
int[] targets = payload.getTargets();
int opcodeOffset = line.payloadOffsetMap.get(curOffset);
Integer switchOffset = line.payloadOffsetMap.get(insn.getOffset());
if (switchOffset == null) {
throw new JadxRuntimeException("Unknown switch insn for payload at " + insn.getOffset());
}
for (int i = 0; i < keys.length; i++) {
int target = opcodeOffset + targets[i];
int target = switchOffset + targets[i];
line.addInsnLine(insn.getOffset(),
String.format("%scase %d: -> " + fmtTarget, indent, keys[i], target));
line.addTip(target,
@@ -3,6 +3,7 @@ package jadx.gui.jobs;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
@@ -11,6 +12,7 @@ import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicLong;
import java.util.function.Consumer;
import java.util.function.Supplier;
import java.util.stream.Collectors;
import javax.swing.SwingUtilities;
import javax.swing.SwingWorker;
@@ -19,6 +21,7 @@ import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import jadx.core.utils.exceptions.JadxRuntimeException;
import jadx.gui.settings.JadxSettings;
import jadx.gui.ui.MainWindow;
import jadx.gui.ui.panel.ProgressPanel;
import jadx.gui.utils.NLS;
@@ -33,16 +36,16 @@ import static jadx.gui.utils.UiUtils.calcProgress;
public class BackgroundExecutor {
private static final Logger LOG = LoggerFactory.getLogger(BackgroundExecutor.class);
private final MainWindow mainWindow;
private final JadxSettings settings;
private final ProgressPanel progressPane;
private ThreadPoolExecutor taskQueueExecutor;
private final Map<Long, IBackgroundTask> taskRunning = new ConcurrentHashMap<>();
private final AtomicLong idSupplier = new AtomicLong(0);
public BackgroundExecutor(MainWindow mainWindow) {
this.mainWindow = mainWindow;
this.progressPane = mainWindow.getProgressPane();
public BackgroundExecutor(JadxSettings settings, ProgressPanel progressPane) {
this.settings = Objects.requireNonNull(settings);
this.progressPane = Objects.requireNonNull(progressPane);
reset();
}
@@ -68,9 +71,16 @@ public class BackgroundExecutor {
public synchronized void cancelAll() {
try {
taskRunning.values().forEach(Cancelable::cancel);
taskQueueExecutor.shutdown();
boolean complete = taskQueueExecutor.awaitTermination(30, TimeUnit.SECONDS);
LOG.debug("Background task executor terminated with status: {}", complete ? "complete" : "interrupted");
taskQueueExecutor.shutdownNow();
boolean complete = taskQueueExecutor.awaitTermination(3, TimeUnit.SECONDS);
if (complete) {
LOG.debug("Background task executor canceled successfully");
} else {
String taskNames = taskRunning.values().stream()
.map(IBackgroundTask::getTitle)
.collect(Collectors.joining(", "));
LOG.debug("Background task executor cancel failed. Running tasks: {}", taskNames);
}
} catch (Exception e) {
LOG.error("Error terminating task executor", e);
} finally {
@@ -131,8 +141,17 @@ public class BackgroundExecutor {
try {
runJobs();
} finally {
taskComplete(id);
task.onDone(this);
try {
task.onDone(this);
// treat UI task operations as part of the task to not mix with others
UiUtils.uiRunAndWait(() -> {
task.onFinish(this);
progressPane.setVisible(false);
});
} finally {
taskComplete(id);
progressPane.changeVisibility(this, false);
}
}
return status;
}
@@ -146,7 +165,7 @@ public class BackgroundExecutor {
progressPane.changeVisibility(this, true);
}
status = TaskStatus.STARTED;
int threadsCount = mainWindow.getSettings().getThreadsCount();
int threadsCount = settings.getThreadsCount();
executor = (ThreadPoolExecutor) Executors.newFixedThreadPool(threadsCount);
for (Runnable job : jobs) {
executor.execute(job);
@@ -212,8 +231,15 @@ public class BackgroundExecutor {
// force termination
task.cancel();
executor.shutdown();
if (executor.awaitTermination(2, TimeUnit.SECONDS)) {
LOG.debug("Task cancel complete");
return;
}
LOG.debug("Forcing tasks cancel");
executor.shutdownNow();
boolean complete = executor.awaitTermination(5, TimeUnit.SECONDS);
LOG.debug("Task cancel complete: {}", complete ? "success" : "aborted");
LOG.debug("Forced task cancel status: {}",
complete ? "success" : "fail, still active: " + executor.getActiveCount());
}
private Supplier<TaskStatus> buildCancelCheck(long startTime) {
@@ -251,12 +277,6 @@ public class BackgroundExecutor {
};
}
@Override
protected void done() {
progressPane.setVisible(false);
task.onFinish(this);
}
@Override
public TaskStatus getStatus() {
return status;
@@ -6,7 +6,6 @@ import java.util.List;
import javax.swing.JOptionPane;
import jadx.api.ICodeCache;
import jadx.api.JadxDecompiler;
import jadx.gui.JadxWrapper;
import jadx.gui.ui.MainWindow;
import jadx.gui.utils.NLS;
@@ -35,9 +34,8 @@ public class ExportTask extends CancelableBackgroundTask {
@Override
public List<Runnable> scheduleJobs() {
wrapCodeCache();
JadxDecompiler decompiler = wrapper.getDecompiler();
decompiler.getArgs().setRootDir(saveDir);
List<Runnable> saveTasks = decompiler.getSaveTasks();
wrapper.getArgs().setRootDir(saveDir);
List<Runnable> saveTasks = wrapper.getSaveTasks();
this.timeLimit = DecompileTask.calcDecompileTimeLimit(saveTasks.size());
return saveTasks;
}
@@ -0,0 +1,285 @@
package jadx.gui.plugins.mappings;
import java.io.IOException;
import java.nio.file.Path;
import java.util.AbstractMap.SimpleEntry;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.atomic.AtomicInteger;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import net.fabricmc.mappingio.MappedElementKind;
import net.fabricmc.mappingio.MappingWriter;
import net.fabricmc.mappingio.format.MappingFormat;
import net.fabricmc.mappingio.tree.MemoryMappingTree;
import jadx.api.ICodeInfo;
import jadx.api.data.ICodeComment;
import jadx.api.data.ICodeRename;
import jadx.api.data.IJavaNodeRef.RefType;
import jadx.api.data.impl.JadxCodeData;
import jadx.api.data.impl.JadxCodeRef;
import jadx.api.metadata.ICodeNodeRef;
import jadx.api.metadata.annotations.InsnCodeOffset;
import jadx.api.metadata.annotations.NodeDeclareRef;
import jadx.api.metadata.annotations.VarNode;
import jadx.api.utils.CodeUtils;
import jadx.core.Consts;
import jadx.core.codegen.TypeGen;
import jadx.core.dex.info.ClassInfo;
import jadx.core.dex.info.FieldInfo;
import jadx.core.dex.info.MethodInfo;
import jadx.core.dex.nodes.ClassNode;
import jadx.core.dex.nodes.FieldNode;
import jadx.core.dex.nodes.MethodNode;
import jadx.core.dex.nodes.RootNode;
import jadx.core.utils.files.FileUtils;
public class MappingExporter {
private static final Logger LOG = LoggerFactory.getLogger(MappingExporter.class);
private final RootNode root;
public MappingExporter(RootNode rootNode) {
this.root = rootNode;
}
private List<VarNode> collectMethodArgs(MethodNode methodNode) {
ICodeInfo codeInfo = methodNode.getTopParentClass().getCode();
int mthDefPos = methodNode.getDefPosition();
int lineEndPos = CodeUtils.getLineEndForPos(codeInfo.getCodeStr(), mthDefPos);
List<VarNode> args = new ArrayList<>();
codeInfo.getCodeMetadata().searchDown(mthDefPos, (pos, ann) -> {
if (pos > lineEndPos) {
// Stop at line end
return Boolean.TRUE;
}
if (ann instanceof NodeDeclareRef) {
ICodeNodeRef declRef = ((NodeDeclareRef) ann).getNode();
if (declRef instanceof VarNode) {
VarNode varNode = (VarNode) declRef;
if (!varNode.getMth().equals(methodNode)) {
// Stop if we've gone too far and have entered a different method
return Boolean.TRUE;
}
args.add(varNode);
}
}
return null;
});
return args;
}
private List<SimpleEntry<VarNode, Integer>> collectMethodVars(MethodNode methodNode) {
ICodeInfo codeInfo = methodNode.getTopParentClass().getCode();
int mthDefPos = methodNode.getDefPosition();
int mthLineEndPos = CodeUtils.getLineEndForPos(codeInfo.getCodeStr(), mthDefPos);
List<SimpleEntry<VarNode, Integer>> vars = new ArrayList<>();
AtomicInteger lastOffset = new AtomicInteger(-1);
codeInfo.getCodeMetadata().searchDown(mthLineEndPos, (pos, ann) -> {
if (ann instanceof InsnCodeOffset) {
lastOffset.set(((InsnCodeOffset) ann).getOffset());
}
if (ann instanceof NodeDeclareRef) {
ICodeNodeRef declRef = ((NodeDeclareRef) ann).getNode();
if (declRef instanceof VarNode) {
VarNode varNode = (VarNode) declRef;
if (!varNode.getMth().equals(methodNode)) {
// Stop if we've gone too far and have entered a different method
return Boolean.TRUE;
}
if (lastOffset.get() != -1) {
vars.add(new SimpleEntry<VarNode, Integer>(varNode, lastOffset.get()));
} else {
LOG.warn("Local variable not present in bytecode, skipping: "
+ methodNode.getMethodInfo().getRawFullId() + "#" + varNode.getName());
}
lastOffset.set(-1);
}
}
return null;
});
return vars;
}
public void exportMappings(Path path, JadxCodeData codeData, MappingFormat mappingFormat) {
MemoryMappingTree mappingTree = new MemoryMappingTree();
// Map < SrcName >
Set<String> mappedClasses = new HashSet<>();
// Map < DeclClass + ShortId >
Set<String> mappedFields = new HashSet<>();
Set<String> mappedMethods = new HashSet<>();
Set<String> methodsWithMappedElements = new HashSet<>();
// Map < DeclClass + MethodShortId + CodeRef, NewName >
Map<String, String> mappedMethodArgsAndVars = new HashMap<>();
// Map < DeclClass + *ShortId + *CodeRef, Comment >
Map<String, String> comments = new HashMap<>();
// We have to do this so we know for sure which elements are *manually* renamed
for (ICodeRename codeRename : codeData.getRenames()) {
if (codeRename.getNodeRef().getType().equals(RefType.CLASS)) {
mappedClasses.add(codeRename.getNodeRef().getDeclaringClass());
} else if (codeRename.getNodeRef().getType().equals(RefType.FIELD)) {
mappedFields.add(codeRename.getNodeRef().getDeclaringClass() + codeRename.getNodeRef().getShortId());
} else if (codeRename.getNodeRef().getType().equals(RefType.METHOD)) {
if (codeRename.getCodeRef() == null) {
mappedMethods.add(codeRename.getNodeRef().getDeclaringClass() + codeRename.getNodeRef().getShortId());
} else {
methodsWithMappedElements.add(codeRename.getNodeRef().getDeclaringClass() + codeRename.getNodeRef().getShortId());
mappedMethodArgsAndVars.put(codeRename.getNodeRef().getDeclaringClass()
+ codeRename.getNodeRef().getShortId()
+ codeRename.getCodeRef(),
codeRename.getNewName());
}
}
}
for (ICodeComment codeComment : codeData.getComments()) {
comments.put(codeComment.getNodeRef().getDeclaringClass()
+ (codeComment.getNodeRef().getShortId() == null ? "" : codeComment.getNodeRef().getShortId())
+ (codeComment.getCodeRef() == null ? "" : codeComment.getCodeRef()),
codeComment.getComment());
if (codeComment.getCodeRef() != null) {
methodsWithMappedElements.add(codeComment.getNodeRef().getDeclaringClass() + codeComment.getNodeRef().getShortId());
}
}
try {
if (mappingFormat.hasSingleFile()) {
if (path.toFile().exists()) {
path.toFile().delete();
}
path.toFile().createNewFile();
} else {
FileUtils.makeDirs(path);
}
mappingTree.visitHeader();
mappingTree.visitNamespaces("official", Arrays.asList("named"));
mappingTree.visitContent();
for (ClassNode cls : root.getClasses()) {
ClassInfo classInfo = cls.getClassInfo();
String classPath = classInfo.makeRawFullName().replace('.', '/');
String rawClassName = classInfo.getRawName();
if (classInfo.hasAlias()
&& !classInfo.getAliasShortName().equals(classInfo.getShortName())
&& mappedClasses.contains(rawClassName)) {
mappingTree.visitClass(classPath);
String alias = classInfo.makeAliasRawFullName().replace('.', '/');
if (alias.startsWith(Consts.DEFAULT_PACKAGE_NAME)) {
alias = alias.substring(Consts.DEFAULT_PACKAGE_NAME.length() + 1);
}
mappingTree.visitDstName(MappedElementKind.CLASS, 0, alias);
}
if (comments.containsKey(rawClassName)) {
mappingTree.visitClass(classPath);
mappingTree.visitComment(MappedElementKind.CLASS, comments.get(rawClassName));
}
for (FieldNode fld : cls.getFields()) {
FieldInfo fieldInfo = fld.getFieldInfo();
if (fieldInfo.hasAlias() && mappedFields.contains(rawClassName + fieldInfo.getShortId())) {
visitField(mappingTree, classPath, fieldInfo.getName(), TypeGen.signature(fieldInfo.getType()));
mappingTree.visitDstName(MappedElementKind.FIELD, 0, fieldInfo.getAlias());
}
if (comments.containsKey(rawClassName + fieldInfo.getShortId())) {
visitField(mappingTree, classPath, fieldInfo.getName(), TypeGen.signature(fieldInfo.getType()));
mappingTree.visitComment(MappedElementKind.FIELD, comments.get(rawClassName + fieldInfo.getShortId()));
}
}
for (MethodNode mth : cls.getMethods()) {
MethodInfo methodInfo = mth.getMethodInfo();
String methodName = methodInfo.getName();
String methodDesc = methodInfo.getShortId().substring(methodName.length());
if (methodInfo.hasAlias() && mappedMethods.contains(rawClassName + methodInfo.getShortId())) {
visitMethod(mappingTree, classPath, methodName, methodDesc);
mappingTree.visitDstName(MappedElementKind.METHOD, 0, methodInfo.getAlias());
}
if (comments.containsKey(rawClassName + methodInfo.getShortId())) {
visitMethod(mappingTree, classPath, methodName, methodDesc);
mappingTree.visitComment(MappedElementKind.METHOD, comments.get(rawClassName + methodInfo.getShortId()));
}
if (!methodsWithMappedElements.contains(rawClassName + methodInfo.getShortId())) {
continue;
}
// Method args
int lvtIndex = mth.getAccessFlags().isStatic() ? 0 : 1;
int lastArgLvIndex = lvtIndex - 1;
List<VarNode> args = collectMethodArgs(mth);
for (VarNode arg : args) {
int lvIndex = arg.getReg() - args.get(0).getReg() + (mth.getAccessFlags().isStatic() ? 0 : 1);
String key = rawClassName + methodInfo.getShortId()
+ JadxCodeRef.forVar(arg.getReg(), arg.getSsa());
if (mappedMethodArgsAndVars.containsKey(key)) {
visitMethodArg(mappingTree, classPath, methodName, methodDesc, args.indexOf(arg), lvIndex);
mappingTree.visitDstName(MappedElementKind.METHOD_ARG, 0, mappedMethodArgsAndVars.get(key));
mappedMethodArgsAndVars.remove(key);
}
lastArgLvIndex = lvIndex;
lvtIndex++;
// Not checking for comments since method args can't have any
}
// Method vars
List<SimpleEntry<VarNode, Integer>> vars = collectMethodVars(mth);
for (SimpleEntry<VarNode, Integer> entry : vars) {
VarNode var = entry.getKey();
int offset = entry.getValue();
int lvIndex = lastArgLvIndex + var.getReg() + (mth.getAccessFlags().isStatic() ? 0 : 1);
String key = rawClassName + methodInfo.getShortId()
+ JadxCodeRef.forVar(var.getReg(), var.getSsa());
if (mappedMethodArgsAndVars.containsKey(key)) {
visitMethodVar(mappingTree, classPath, methodName, methodDesc, lvtIndex, lvIndex, offset);
mappingTree.visitDstName(MappedElementKind.METHOD_VAR, 0, mappedMethodArgsAndVars.get(key));
}
key = rawClassName + methodInfo.getShortId() + JadxCodeRef.forInsn(offset);
if (comments.containsKey(key)) {
visitMethodVar(mappingTree, classPath, methodName, methodDesc, lvtIndex, lvIndex, offset);
mappingTree.visitComment(MappedElementKind.METHOD_VAR, comments.get(key));
}
lvtIndex++;
}
}
}
MappingWriter writer = MappingWriter.create(path, mappingFormat);
mappingTree.accept(writer);
mappingTree.visitEnd();
writer.close();
} catch (IOException e) {
LOG.error("Failed to save deobfuscation map file '{}'", path.toAbsolutePath(), e);
}
}
private void visitField(MemoryMappingTree tree, String classPath, String srcName, String srcDesc) {
tree.visitClass(classPath);
tree.visitField(srcName, srcDesc);
}
private void visitMethod(MemoryMappingTree tree, String classPath, String srcName, String srcDesc) {
tree.visitClass(classPath);
tree.visitMethod(srcName, srcDesc);
}
private void visitMethodArg(MemoryMappingTree tree, String classPath, String methodSrcName, String methodSrcDesc, int argPosition,
int lvIndex) {
visitMethod(tree, classPath, methodSrcName, methodSrcDesc);
tree.visitMethodArg(argPosition, lvIndex, null);
}
private void visitMethodVar(MemoryMappingTree tree, String classPath, String methodSrcName, String methodSrcDesc, int lvtIndex,
int lvIndex, int startOpIdx) {
visitMethod(tree, classPath, methodSrcName, methodSrcDesc);
tree.visitMethodVar(lvtIndex, lvIndex, startOpIdx, null);
}
}
@@ -10,6 +10,7 @@ import jadx.core.utils.Utils;
@SuppressWarnings("MemberName")
public class QuarkReportData {
public static class Crime {
public String crime;
public String confidence;
@@ -18,6 +19,23 @@ public class QuarkReportData {
List<Method> native_api;
List<JsonElement> combination;
List<Map<String, InvokePlace>> register;
public int parseConfidence() {
return Integer.parseInt(confidence.replace("%", ""));
}
@Override
public String toString() {
final StringBuffer sb = new StringBuffer("Crime{");
sb.append("crime='").append(crime).append('\'');
sb.append(", confidence='").append(confidence).append('\'');
sb.append(", permissions=").append(permissions);
sb.append(", native_api=").append(native_api);
sb.append(", combination=").append(combination);
sb.append(", register=").append(register);
sb.append('}');
return sb.toString();
}
}
public static class Method {
@@ -46,4 +64,22 @@ public class QuarkReportData {
String threat_level;
int total_score;
List<Crime> crimes;
public void validate() {
if (crimes == null) {
throw new RuntimeException("Invalid data: \"crimes\" list missing");
}
for (Crime crime : crimes) {
if (crime.confidence == null) {
throw new RuntimeException("Confidence value missing: " + crime);
}
try {
crime.parseConfidence();
} catch (Exception e) {
throw new RuntimeException("Invalid crime entry: " + crime);
}
}
}
}
@@ -1,5 +1,6 @@
package jadx.gui.plugins.quark;
import java.io.BufferedReader;
import java.nio.file.Files;
import java.nio.file.Path;
@@ -33,12 +34,12 @@ public class QuarkReportNode extends JNode {
private static final ImageIcon ICON = UiUtils.openSvgIcon("ui/quark");
private final Path apkFile;
private final Path reportFile;
private ICodeInfo errorContent;
public QuarkReportNode(Path apkFile) {
this.apkFile = apkFile;
public QuarkReportNode(Path reportFile) {
this.reportFile = reportFile;
}
@Override
@@ -59,7 +60,11 @@ public class QuarkReportNode extends JNode {
@Override
public ContentPanel getContentPanel(TabbedPane tabbedPane) {
try {
QuarkReportData data = GSON.fromJson(Files.newBufferedReader(apkFile), QuarkReportData.class);
QuarkReportData data;
try (BufferedReader reader = Files.newBufferedReader(reportFile)) {
data = GSON.fromJson(reader, QuarkReportData.class);
}
data.validate();
return new QuarkReportPanel(tabbedPane, this, data);
} catch (Exception e) {
LOG.error("Quark report parse error", e);
@@ -70,7 +70,7 @@ public class QuarkReportPanel extends ContentPanel {
}
private void prepareData() {
data.crimes.sort(Comparator.comparingInt(c -> -Integer.parseInt(c.confidence.replace("%", ""))));
data.crimes.sort(Comparator.comparingInt(c -> -c.parseConfidence()));
}
private void initUI() {
@@ -290,7 +290,7 @@ public class QuarkReportPanel extends ContentPanel {
}
return new MethodTreeNode(javaMethod);
} catch (Exception e) {
LOG.error("Failed to parse method descriptor string", e);
LOG.error("Failed to parse method descriptor string: {}", descr, e);
return new TextTreeNode(descr);
}
}
@@ -3,6 +3,7 @@ package jadx.gui.search;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.function.Consumer;
@@ -65,6 +66,10 @@ public class SearchTask extends CancelableBackgroundTask {
}
public synchronized boolean addResult(JNode resultNode) {
if (isCanceled()) {
// ignore new results after cancel
return true;
}
this.results.accept(resultNode);
if (resultsLimit != 0 && resultsCount.incrementAndGet() >= resultsLimit) {
cancel();
@@ -76,9 +81,9 @@ public class SearchTask extends CancelableBackgroundTask {
public synchronized void waitTask() {
if (future != null) {
try {
future.get();
future.get(2, TimeUnit.SECONDS);
} catch (Exception e) {
LOG.error("Wait search task failed", e);
LOG.warn("Wait search task failed", e);
} finally {
future.cancel(true);
future = null;
@@ -7,6 +7,7 @@ import jadx.api.JavaNode;
import jadx.gui.search.ISearchMethod;
import jadx.gui.search.ISearchProvider;
import jadx.gui.search.SearchSettings;
import jadx.gui.treemodel.JClass;
import jadx.gui.treemodel.JNode;
import jadx.gui.ui.MainWindow;
import jadx.gui.utils.JNodeCache;
@@ -33,7 +34,7 @@ public abstract class BaseSearchProvider implements ISearchProvider {
return nodeCache.makeFrom(node);
}
protected JNode convert(JavaClass cls) {
protected JClass convert(JavaClass cls) {
return nodeCache.makeFrom(cls);
}
@@ -8,22 +8,25 @@ import org.slf4j.LoggerFactory;
import jadx.api.ICodeCache;
import jadx.api.ICodeWriter;
import jadx.api.JadxDecompiler;
import jadx.api.JavaClass;
import jadx.api.JavaNode;
import jadx.api.metadata.ICodeMetadata;
import jadx.api.metadata.ICodeNodeRef;
import jadx.gui.JadxWrapper;
import jadx.gui.jobs.Cancelable;
import jadx.gui.search.SearchSettings;
import jadx.gui.treemodel.CodeNode;
import jadx.gui.treemodel.JClass;
import jadx.gui.treemodel.JNode;
import jadx.gui.ui.MainWindow;
import static jadx.core.utils.Utils.getOrElse;
public final class CodeSearchProvider extends BaseSearchProvider {
private static final Logger LOG = LoggerFactory.getLogger(CodeSearchProvider.class);
private final ICodeCache codeCache;
private final JadxDecompiler decompiler;
private final JadxWrapper wrapper;
private @Nullable String code;
private int clsNum = 0;
@@ -32,7 +35,7 @@ public final class CodeSearchProvider extends BaseSearchProvider {
public CodeSearchProvider(MainWindow mw, SearchSettings searchSettings, List<JavaClass> classes) {
super(mw, searchSettings, classes);
this.codeCache = mw.getWrapper().getArgs().getCodeCache();
this.decompiler = mw.getWrapper().getDecompiler();
this.wrapper = mw.getWrapper();
}
@Override
@@ -68,21 +71,23 @@ public final class CodeSearchProvider extends BaseSearchProvider {
int end = lineEnd == -1 ? clsCode.length() : lineEnd;
String line = clsCode.substring(lineStart, end);
this.pos = end;
return new CodeNode(getEnclosingNode(javaClass, end), line.trim(), newPos);
JClass rootCls = convert(javaClass);
JNode enclosingNode = getOrElse(getEnclosingNode(javaClass, end), rootCls);
return new CodeNode(rootCls, enclosingNode, line.trim(), newPos);
}
private JNode getEnclosingNode(JavaClass javaCls, int pos) {
private @Nullable JNode getEnclosingNode(JavaClass javaCls, int pos) {
try {
ICodeMetadata metadata = javaCls.getCodeInfo().getCodeMetadata();
ICodeNodeRef nodeRef = metadata.getNodeAt(pos);
JavaNode encNode = decompiler.getJavaNodeByRef(nodeRef);
JavaNode encNode = wrapper.getJavaNodeByRef(nodeRef);
if (encNode != null) {
return convert(encNode);
}
} catch (Exception e) {
LOG.debug("Failed to resolve enclosing node", e);
}
return convert(javaCls);
return null;
}
private String getClassCode(JavaClass javaClass, ICodeCache codeCache) {
@@ -57,6 +57,18 @@ public class JadxProject {
this.mainWindow = mainWindow;
}
public @Nullable Path getWorkingDir() {
if (projectPath != null) {
return projectPath.toAbsolutePath().getParent();
}
List<Path> files = data.getFiles();
if (!files.isEmpty()) {
Path path = files.get(0);
return path.toAbsolutePath().getParent();
}
return null;
}
@Nullable
public Path getProjectPath() {
return projectPath;
@@ -166,7 +178,18 @@ public class JadxProject {
Path path = files.get(0);
return path.resolveSibling(path.getFileName() + ".cache");
}
throw new JadxRuntimeException("Can't get working dir");
throw new JadxRuntimeException("Failed to build cache dir");
}
public boolean isEnableLiveReload() {
return data.isEnableLiveReload();
}
public void setEnableLiveReload(boolean newValue) {
if (newValue != data.isEnableLiveReload()) {
data.setEnableLiveReload(newValue);
changed();
}
}
private void changed() {
@@ -45,7 +45,7 @@ public class JadxSettings extends JadxCLIArgs {
private static final Path USER_HOME = Paths.get(System.getProperty("user.home"));
private static final int RECENT_PROJECTS_COUNT = 15;
private static final int CURRENT_SETTINGS_VERSION = 17;
private static final int CURRENT_SETTINGS_VERSION = 18;
private static final Font DEFAULT_FONT = new RSyntaxTextArea().getFont();
@@ -59,7 +59,7 @@ public class JadxSettings extends JadxCLIArgs {
private Path lastOpenFilePath = USER_HOME;
private Path lastSaveFilePath = USER_HOME;
private boolean flattenPackage = false;
private boolean checkForUpdates = false;
private boolean checkForUpdates = true;
private List<Path> recentProjects = new ArrayList<>();
private String fontStr = "";
private String smaliFontStr = "";
@@ -91,6 +91,7 @@ public class JadxSettings extends JadxCLIArgs {
private String adbDialogPort = "5037";
private CodeCacheMode codeCacheMode = CodeCacheMode.DISK_WITH_CACHE;
private boolean jumpOnDoubleClick = true;
/**
* UI setting: the width of the tree showing the classes, resources, ...
@@ -219,29 +220,22 @@ public class JadxSettings extends JadxCLIArgs {
if (pos == null || pos.getBounds() == null) {
return false;
}
if (window instanceof MainWindow) {
int extendedState = getMainWindowExtendedState();
if (extendedState != JFrame.NORMAL) {
((JFrame) window).setExtendedState(extendedState);
return true;
}
}
if (!isContainedInAnyScreen(pos)) {
if (!isAccessibleInAnyScreen(pos)) {
return false;
}
window.setBounds(pos.getBounds());
if (window instanceof MainWindow) {
((JFrame) window).setExtendedState(getMainWindowExtendedState());
}
return true;
}
private static boolean isContainedInAnyScreen(WindowLocation pos) {
Rectangle bounds = pos.getBounds();
if (bounds.getX() > 0 && bounds.getY() > 0) {
for (GraphicsDevice gd : GraphicsEnvironment.getLocalGraphicsEnvironment().getScreenDevices()) {
if (gd.getDefaultConfiguration().getBounds().contains(bounds)) {
return true;
}
private static boolean isAccessibleInAnyScreen(WindowLocation pos) {
Rectangle windowBounds = pos.getBounds();
for (GraphicsDevice gd : GraphicsEnvironment.getLocalGraphicsEnvironment().getScreenDevices()) {
Rectangle screenBounds = gd.getDefaultConfiguration().getBounds();
if (screenBounds.intersects(windowBounds)) {
return true;
}
}
LOG.debug("Window saved position was ignored: {}", pos);
@@ -623,6 +617,14 @@ public class JadxSettings extends JadxCLIArgs {
this.codeCacheMode = codeCacheMode;
}
public boolean isJumpOnDoubleClick() {
return jumpOnDoubleClick;
}
public void setJumpOnDoubleClick(boolean jumpOnDoubleClick) {
this.jumpOnDoubleClick = jumpOnDoubleClick;
}
private void upgradeSettings(int fromVersion) {
LOG.debug("upgrade settings from version: {} to {}", fromVersion, CURRENT_SETTINGS_VERSION);
if (fromVersion == 0) {
@@ -702,11 +704,7 @@ public class JadxSettings extends JadxCLIArgs {
fromVersion++;
}
if (fromVersion == 15) {
if (deobfuscationForceSave) {
deobfuscationMapFileMode = DeobfuscationMapFileMode.OVERWRITE;
} else {
deobfuscationMapFileMode = DeobfuscationMapFileMode.READ;
}
deobfuscationMapFileMode = DeobfuscationMapFileMode.READ;
fromVersion++;
}
if (fromVersion == 16) {
@@ -717,6 +715,10 @@ public class JadxSettings extends JadxCLIArgs {
}
fromVersion++;
}
if (fromVersion == 17) {
checkForUpdates = true;
fromVersion++;
}
if (fromVersion != CURRENT_SETTINGS_VERSION) {
LOG.warn("Incorrect settings upgrade. Expected version: {}, got: {}", CURRENT_SETTINGS_VERSION, fromVersion);
}
@@ -65,7 +65,6 @@ import jadx.api.JadxArgs.UseKotlinMethodsForVarNames;
import jadx.api.args.DeobfuscationMapFileMode;
import jadx.api.plugins.JadxPlugin;
import jadx.api.plugins.JadxPluginInfo;
import jadx.api.plugins.JadxPluginManager;
import jadx.api.plugins.options.JadxPluginOptions;
import jadx.api.plugins.options.OptionDescription;
import jadx.gui.ui.MainWindow;
@@ -580,8 +579,7 @@ public class JadxSettingsWindow extends JDialog {
private SettingsGroup makePluginOptionsGroup() {
SettingsGroup pluginsGroup = new SettingsGroup(NLS.str("preferences.plugins"));
JadxPluginManager pluginManager = mainWindow.getWrapper().getDecompiler().getPluginManager();
for (JadxPlugin plugin : pluginManager.getAllPlugins()) {
for (JadxPlugin plugin : mainWindow.getWrapper().getAllPlugins()) {
if (!(plugin instanceof JadxPluginOptions)) {
continue;
}
@@ -628,6 +626,10 @@ public class JadxSettingsWindow extends JDialog {
mainWindow.loadSettings();
});
JCheckBox jumpOnDoubleClick = new JCheckBox();
jumpOnDoubleClick.setSelected(settings.isJumpOnDoubleClick());
jumpOnDoubleClick.addItemListener(e -> settings.setJumpOnDoubleClick(e.getStateChange() == ItemEvent.SELECTED));
JCheckBox update = new JCheckBox();
update.setSelected(settings.isCheckForUpdates());
update.addItemListener(e -> settings.setCheckForUpdates(e.getStateChange() == ItemEvent.SELECTED));
@@ -649,6 +651,7 @@ public class JadxSettingsWindow extends JDialog {
SettingsGroup group = new SettingsGroup(NLS.str("preferences.other"));
group.addRow(NLS.str("preferences.language"), languageCbx);
group.addRow(NLS.str("preferences.lineNumbersMode"), lineNumbersMode);
group.addRow(NLS.str("preferences.jumpOnDoubleClick"), jumpOnDoubleClick);
group.addRow(NLS.str("preferences.check_for_updates"), update);
group.addRow(NLS.str("preferences.cfg"), cfg);
group.addRow(NLS.str("preferences.raw_cfg"), rawCfg);
@@ -19,6 +19,7 @@ public class ProjectData {
private List<TabViewState> openTabs = Collections.emptyList();
private int activeTab = -1;
private @Nullable Path cacheDir;
private boolean enableLiveReload = false;
public List<Path> getFiles() {
return files;
@@ -94,4 +95,12 @@ public class ProjectData {
public void setCacheDir(Path cacheDir) {
this.cacheDir = cacheDir;
}
public boolean isEnableLiveReload() {
return enableLiveReload;
}
public void setEnableLiveReload(boolean enableLiveReload) {
this.enableLiveReload = enableLiveReload;
}
}
@@ -9,11 +9,13 @@ import jadx.api.JavaNode;
public class CodeNode extends JNode {
private static final long serialVersionUID = 1658650786734966545L;
private final transient JClass rootCls;
private final transient JNode jNode;
private final transient String line;
private final transient int pos;
public CodeNode(JNode jNode, String lineStr, int pos) {
public CodeNode(JClass rootCls, JNode jNode, String lineStr, int pos) {
this.rootCls = rootCls;
this.jNode = jNode;
this.line = lineStr;
this.pos = pos;
@@ -36,14 +38,7 @@ public class CodeNode extends JNode {
@Override
public JClass getRootClass() {
JClass parent = jNode.getJParent();
if (parent != null) {
return parent.getRootClass();
}
if (jNode instanceof JClass) {
return (JClass) jNode;
}
return null;
return rootCls;
}
@Override
@@ -226,6 +226,7 @@ public class JResource extends JLoadableNode {
case MANIFEST:
case XML:
case ARSC:
return SyntaxConstants.SYNTAX_STYLE_XML;
default:
@@ -249,8 +250,7 @@ public class JResource extends JLoadableNode {
"yaml", SyntaxConstants.SYNTAX_STYLE_YAML,
"properties", SyntaxConstants.SYNTAX_STYLE_PROPERTIES_FILE,
"ini", SyntaxConstants.SYNTAX_STYLE_INI,
"sql", SyntaxConstants.SYNTAX_STYLE_SQL,
"arsc", SyntaxConstants.SYNTAX_STYLE_XML);
"sql", SyntaxConstants.SYNTAX_STYLE_SQL);
private String getSyntaxByExtension(String name) {
int dot = name.lastIndexOf('.');
@@ -70,6 +70,6 @@ public class JVariable extends JNode {
@Override
public boolean canRename() {
return true;
return var.getName() != null;
}
}
@@ -16,6 +16,7 @@ import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.FocusAdapter;
import java.awt.event.FocusEvent;
import java.awt.event.InputEvent;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.MouseAdapter;
@@ -43,6 +44,7 @@ import javax.swing.Action;
import javax.swing.Box;
import javax.swing.ImageIcon;
import javax.swing.JCheckBoxMenuItem;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
@@ -77,6 +79,7 @@ import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import ch.qos.logback.classic.Level;
import net.fabricmc.mappingio.format.MappingFormat;
import jadx.api.JadxArgs;
import jadx.api.JavaNode;
@@ -93,6 +96,7 @@ import jadx.gui.jobs.DecompileTask;
import jadx.gui.jobs.ExportTask;
import jadx.gui.jobs.ProcessResult;
import jadx.gui.jobs.TaskStatus;
import jadx.gui.plugins.mappings.MappingExporter;
import jadx.gui.plugins.quark.QuarkDialog;
import jadx.gui.settings.JadxProject;
import jadx.gui.settings.JadxSettings;
@@ -132,7 +136,9 @@ import jadx.gui.utils.Link;
import jadx.gui.utils.NLS;
import jadx.gui.utils.SystemInfo;
import jadx.gui.utils.UiUtils;
import jadx.gui.utils.fileswatcher.LiveReloadWorker;
import jadx.gui.utils.logs.LogCollector;
import jadx.gui.utils.ui.ActionHandler;
import static io.reactivex.internal.functions.Functions.EMPTY_RUNNABLE;
import static javax.swing.KeyStroke.getKeyStroke;
@@ -149,6 +155,7 @@ public class MainWindow extends JFrame {
private static final ImageIcon ICON_OPEN = UiUtils.openSvgIcon("ui/openDisk");
private static final ImageIcon ICON_ADD_FILES = UiUtils.openSvgIcon("ui/addFile");
private static final ImageIcon ICON_SAVE_ALL = UiUtils.openSvgIcon("ui/menu-saveall");
private static final ImageIcon ICON_RELOAD = UiUtils.openSvgIcon("ui/refresh");
private static final ImageIcon ICON_EXPORT = UiUtils.openSvgIcon("ui/export");
private static final ImageIcon ICON_EXIT = UiUtils.openSvgIcon("ui/exit");
private static final ImageIcon ICON_SYNC = UiUtils.openSvgIcon("ui/pagination");
@@ -174,6 +181,7 @@ public class MainWindow extends JFrame {
private transient Action newProjectAction;
private transient Action saveProjectAction;
private transient JMenu exportMappingsMenu;
private JPanel mainPanel;
private JSplitPane splitPane;
@@ -192,6 +200,9 @@ public class MainWindow extends JFrame {
private JToggleButton deobfToggleBtn;
private JCheckBoxMenuItem deobfMenuItem;
private JCheckBoxMenuItem liveReloadMenuItem;
private final LiveReloadWorker liveReloadWorker;
private transient Link updateLink;
private transient ProgressPanel progressPane;
private transient Theme editorTheme;
@@ -204,18 +215,18 @@ public class MainWindow extends JFrame {
this.cacheObject = new CacheObject();
this.project = new JadxProject(this);
this.wrapper = new JadxWrapper(this);
this.liveReloadWorker = new LiveReloadWorker(this);
resetCache();
FontUtils.registerBundledFonts();
initUI();
this.backgroundExecutor = new BackgroundExecutor(settings, progressPane);
initMenuAndToolbar();
registerMouseNavigationButtons();
UiUtils.setWindowIcons(this);
loadSettings();
update();
this.backgroundExecutor = new BackgroundExecutor(this);
checkForUpdate();
}
@@ -306,6 +317,7 @@ public class MainWindow extends JFrame {
return;
}
closeAll();
exportMappingsMenu.setEnabled(false);
updateProject(new JadxProject(this));
}
@@ -349,6 +361,31 @@ public class MainWindow extends JFrame {
update();
}
private void exportMappings(MappingFormat mappingFormat) {
FileDialog fileDialog = new FileDialog(this, FileDialog.OpenMode.CUSTOM_SAVE);
fileDialog.setTitle(NLS.str("file.export_mappings_as"));
Path workingDir = project.getWorkingDir();
Path baseDir = workingDir != null ? workingDir : settings.getLastSaveFilePath();
if (mappingFormat.hasSingleFile()) {
fileDialog.setSelectedFile(baseDir.resolve("mappings." + mappingFormat.fileExt));
fileDialog.setFileExtList(Collections.singletonList(mappingFormat.fileExt));
fileDialog.setSelectionMode(JFileChooser.FILES_ONLY);
} else {
fileDialog.setCurrentDir(baseDir);
fileDialog.setSelectionMode(JFileChooser.DIRECTORIES_ONLY);
}
List<Path> paths = fileDialog.show();
if (paths.size() != 1) {
return;
}
Path savePath = paths.get(0);
LOG.info("Export mappings to: {}", savePath.toAbsolutePath());
backgroundExecutor.execute(NLS.str("progress.export_mappings"),
() -> new MappingExporter(wrapper.getDecompiler().getRoot())
.exportMappings(savePath, project.getCodeData(), mappingFormat),
s -> update());
}
void open(List<Path> paths) {
open(paths, EMPTY_RUNNABLE);
}
@@ -386,7 +423,7 @@ public class MainWindow extends JFrame {
return loadedFile.resolveSibling(fileName);
}
public void reopen() {
public synchronized void reopen() {
saveAll();
closeAll();
loadFiles(EMPTY_RUNNABLE);
@@ -408,6 +445,7 @@ public class MainWindow extends JFrame {
}
private void loadFiles(Runnable onFinish) {
exportMappingsMenu.setEnabled(false);
if (project.getFilePaths().isEmpty()) {
return;
}
@@ -419,8 +457,13 @@ public class MainWindow extends JFrame {
UiUtils.errorMessage(this, NLS.str("message.memoryLow"));
return;
}
if (status != TaskStatus.COMPLETE) {
LOG.warn("Loading task incomplete, status: {}", status);
return;
}
checkLoadedStatus();
onOpen();
exportMappingsMenu.setEnabled(true);
onFinish.run();
});
}
@@ -437,6 +480,7 @@ public class MainWindow extends JFrame {
LogCollector.getInstance().reset();
wrapper.close();
tabbedPane.closeAllTabs();
UiUtils.resetClipboardOwner();
System.gc();
}
@@ -463,6 +507,7 @@ public class MainWindow extends JFrame {
deobfToggleBtn.setSelected(settings.isDeobfuscationOn());
initTree();
update();
updateLiveReload(project.isEnableLiveReload());
BreakpointManager.init(project.getFilePaths().get(0).toAbsolutePath().getParent());
backgroundExecutor.execute(NLS.str("progress.load"),
@@ -470,6 +515,21 @@ public class MainWindow extends JFrame {
status -> runInitialBackgroundJobs());
}
public void updateLiveReload(boolean state) {
if (liveReloadWorker.isStarted() == state) {
return;
}
project.setEnableLiveReload(state);
liveReloadMenuItem.setEnabled(false);
backgroundExecutor.execute(
(state ? "Starting" : "Stopping") + " live reload",
() -> liveReloadWorker.updateState(state),
s -> {
liveReloadMenuItem.setState(state);
liveReloadMenuItem.setEnabled(true);
});
}
private void addTreeCustomNodes() {
treeRoot.replaceCustomNode(ApkSignature.getApkSignature(wrapper));
treeRoot.replaceCustomNode(new SummaryNode(this));
@@ -765,7 +825,6 @@ public class MainWindow extends JFrame {
}
private void initMenuAndToolbar() {
final boolean devVersion = (Jadx.VERSION_DEV.equals(Jadx.getVersion()));
Action openAction = new AbstractAction(NLS.str("file.open_action"), ICON_OPEN) {
@Override
public void actionPerformed(ActionEvent e) {
@@ -807,6 +866,49 @@ public class MainWindow extends JFrame {
};
saveProjectAsAction.putValue(Action.SHORT_DESCRIPTION, NLS.str("file.save_project_as"));
ActionHandler reload = new ActionHandler(ev -> UiUtils.uiRun(this::reopen));
reload.setNameAndDesc(NLS.str("file.reload"));
reload.setIcon(ICON_RELOAD);
reload.setKeyBinding(getKeyStroke(KeyEvent.VK_F5, 0));
ActionHandler liveReload = new ActionHandler(ev -> updateLiveReload(!project.isEnableLiveReload()));
liveReload.setName(NLS.str("file.live_reload"));
liveReload.setShortDescription(NLS.str("file.live_reload_desc"));
liveReload.setKeyBinding(getKeyStroke(KeyEvent.VK_F5, InputEvent.SHIFT_DOWN_MASK));
liveReloadMenuItem = new JCheckBoxMenuItem(liveReload);
liveReloadMenuItem.setState(project.isEnableLiveReload());
Action exportMappingsAsTiny2 = new AbstractAction("Tiny v2 file") {
@Override
public void actionPerformed(ActionEvent e) {
exportMappings(MappingFormat.TINY_2);
}
};
exportMappingsAsTiny2.putValue(Action.SHORT_DESCRIPTION, "Tiny v2 file");
Action exportMappingsAsEnigma = new AbstractAction("Enigma file") {
@Override
public void actionPerformed(ActionEvent e) {
exportMappings(MappingFormat.ENIGMA);
}
};
exportMappingsAsEnigma.putValue(Action.SHORT_DESCRIPTION, "Enigma file");
Action exportMappingsAsEnigmaDir = new AbstractAction("Enigma directory") {
@Override
public void actionPerformed(ActionEvent e) {
exportMappings(MappingFormat.ENIGMA_DIR);
}
};
exportMappingsAsEnigmaDir.putValue(Action.SHORT_DESCRIPTION, "Enigma directory");
exportMappingsMenu = new JMenu(NLS.str("file.export_mappings_as"));
exportMappingsMenu.add(exportMappingsAsTiny2);
exportMappingsMenu.add(exportMappingsAsEnigma);
exportMappingsMenu.add(exportMappingsAsEnigmaDir);
exportMappingsMenu.setEnabled(false);
Action saveAllAction = new AbstractAction(NLS.str("file.save_all"), ICON_SAVE_ALL) {
@Override
public void actionPerformed(ActionEvent e) {
@@ -993,6 +1095,11 @@ public class MainWindow extends JFrame {
file.add(saveProjectAction);
file.add(saveProjectAsAction);
file.addSeparator();
file.add(reload);
file.add(liveReloadMenuItem);
file.addSeparator();
file.add(exportMappingsMenu);
file.addSeparator();
file.add(saveAllAction);
file.add(exportAction);
file.addSeparator();
@@ -1027,7 +1134,7 @@ public class MainWindow extends JFrame {
JMenu help = new JMenu(NLS.str("menu.help"));
help.setMnemonic(KeyEvent.VK_H);
help.add(logAction);
if (devVersion) {
if (Jadx.isDevVersion()) {
help.add(new AbstractAction("Show sample error report") {
@Override
public void actionPerformed(ActionEvent e) {
@@ -1060,6 +1167,8 @@ public class MainWindow extends JFrame {
toolbar.add(openAction);
toolbar.add(addFilesAction);
toolbar.addSeparator();
toolbar.add(reload);
toolbar.addSeparator();
toolbar.add(saveAllAction);
toolbar.add(exportAction);
toolbar.addSeparator();
@@ -1398,10 +1507,6 @@ public class MainWindow extends JFrame {
return backgroundExecutor;
}
public ProgressPanel getProgressPane() {
return progressPane;
}
public JRoot getTreeRoot() {
return treeRoot;
}
@@ -309,6 +309,7 @@ public class TabbedPane extends JTabbedPane {
public void closeCodePanel(ContentPanel contentPanel) {
openTabs.remove(contentPanel.getNode());
remove(contentPanel);
contentPanel.dispose();
}
@Nullable
@@ -373,49 +374,56 @@ public class TabbedPane extends JTabbedPane {
jumps.reset();
curTab = null;
lastTab = null;
FocusManager.reset();
}
@Nullable
public Component getFocusedComp() {
return FocusManager.isActive() ? FocusManager.focusedComp : null;
return FocusManager.getFocusedComp();
}
private static class FocusManager implements FocusListener {
static boolean active = false;
static FocusManager listener = new FocusManager();
static Component focusedComp;
private static final FocusManager INSTANCE = new FocusManager();
private static @Nullable Component focusedComp;
static boolean isActive() {
return active;
return focusedComp != null;
}
static void reset() {
focusedComp = null;
}
static Component getFocusedComp() {
return focusedComp;
}
@Override
public void focusGained(FocusEvent e) {
active = true;
focusedComp = (Component) e.getSource();
}
@Override
public void focusLost(FocusEvent e) {
active = false;
focusedComp = null;
}
static void listen(ContentPanel pane) {
if (pane instanceof ClassCodeContentPanel) {
((ClassCodeContentPanel) pane).getCodeArea().addFocusListener(listener);
((ClassCodeContentPanel) pane).getSmaliCodeArea().addFocusListener(listener);
((ClassCodeContentPanel) pane).getCodeArea().addFocusListener(INSTANCE);
((ClassCodeContentPanel) pane).getSmaliCodeArea().addFocusListener(INSTANCE);
return;
}
if (pane instanceof AbstractCodeContentPanel) {
((AbstractCodeContentPanel) pane).getCodeArea().addFocusListener(listener);
((AbstractCodeContentPanel) pane).getCodeArea().addFocusListener(INSTANCE);
return;
}
if (pane instanceof HtmlPanel) {
((HtmlPanel) pane).getHtmlArea().addFocusListener(listener);
((HtmlPanel) pane).getHtmlArea().addFocusListener(INSTANCE);
return;
}
if (pane instanceof ImagePanel) {
pane.addFocusListener(listener);
pane.addFocusListener(INSTANCE);
return;
}
// throw new JadxRuntimeException("Add the new ContentPanel to TabbedPane.FocusManager: " + pane);
@@ -1,5 +1,6 @@
package jadx.gui.ui.codearea;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.Point;
import java.awt.Rectangle;
@@ -8,15 +9,21 @@ import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import java.util.Objects;
import javax.swing.AbstractAction;
import javax.swing.Action;
import javax.swing.JCheckBoxMenuItem;
import javax.swing.JMenuItem;
import javax.swing.JPopupMenu;
import javax.swing.JViewport;
import javax.swing.SwingUtilities;
import javax.swing.event.CaretEvent;
import javax.swing.event.CaretListener;
import javax.swing.event.PopupMenuEvent;
import javax.swing.event.PopupMenuListener;
import javax.swing.text.BadLocationException;
import javax.swing.text.Caret;
import javax.swing.text.DefaultCaret;
@@ -64,12 +71,12 @@ public abstract class AbstractCodeArea extends RSyntaxTextArea {
}
}
protected final ContentPanel contentPanel;
protected final JNode node;
protected ContentPanel contentPanel;
protected JNode node;
public AbstractCodeArea(ContentPanel contentPanel, JNode node) {
this.contentPanel = contentPanel;
this.node = node;
this.node = Objects.requireNonNull(node);
setMarkOccurrences(false);
setEditable(false);
@@ -348,4 +355,45 @@ public abstract class AbstractCodeArea extends RSyntaxTextArea {
}
return null;
}
public boolean isDisposed() {
return node == null;
}
public void dispose() {
// code area reference can still be used somewhere in UI objects,
// reset node reference to allow to GC jadx objects tree
node = null;
contentPanel = null;
// also clear internals
try {
setIgnoreRepaint(true);
setText("");
setEnabled(false);
setSyntaxEditingStyle(SYNTAX_STYLE_NONE);
setLinkGenerator(null);
for (MouseListener mouseListener : getMouseListeners()) {
removeMouseListener(mouseListener);
}
for (MouseMotionListener mouseMotionListener : getMouseMotionListeners()) {
removeMouseMotionListener(mouseMotionListener);
}
JPopupMenu popupMenu = getPopupMenu();
for (PopupMenuListener popupMenuListener : popupMenu.getPopupMenuListeners()) {
popupMenu.removePopupMenuListener(popupMenuListener);
}
for (Component component : popupMenu.getComponents()) {
if (component instanceof JMenuItem) {
Action action = ((JMenuItem) component).getAction();
if (action instanceof JNodeAction) {
((JNodeAction) action).dispose();
}
}
}
popupMenu.removeAll();
} catch (Throwable e) {
LOG.debug("Error on code area dispose", e);
}
}
}

Some files were not shown because too many files have changed in this diff Show More