Compare commits

...

128 Commits

Author SHA1 Message Date
Skylot 6844a46c93 fix: disable HTML rendering in labels if not needed 2022-10-20 15:58:23 +01:00
Skylot e9e45707da chore: update dependencies 2022-10-20 14:54:31 +01:00
Skylot b9d02ff4c4 refactor: remove all LinkedList usage 2022-10-12 17:05:08 +01:00
Jan S 29b64300bc fix(gui): multi-threading issue in DebugController fixed (#1701) (PR #1702) 2022-10-11 19:21:06 +01:00
zhongqingsong 777355e86e fix(gui): update Messages_zh_CN.properties (PR #1700)
Add new text about logcat.
2022-10-10 18:57:58 +01:00
Skylot 620a177ce8 fix: restore enum class with custom code in static init (#1699) 2022-10-08 21:54:06 +01:00
Skylot 683c2dfbeb fix: improve ternary inline, resolve more enum cases (#1686) 2022-10-07 15:51:11 +01:00
Skylot 266cbcc6f4 fix(gui): migrate to fixed jdwp library fork (#1471) 2022-10-06 19:47:15 +01:00
Jan S 8a45602ae6 fix: improve logging messages for zip security errors (#750)(PR #1698)
Logging error messages on invalid file-names or path traversal attacks improved
2022-10-06 19:31:42 +01:00
Skylot 711419a797 fix: correct fix for all use places of incompatible primitives (#1688) 2022-10-03 00:11:04 +03:00
Skylot 603f3057eb chore: update dependencies 2022-10-03 00:11:01 +03:00
5idereal fa6fc1f871 fix(gui): update zh-TW translations (PR #1694) 2022-10-02 15:50:11 +01:00
Skylot 49fa320989 fix: handle possible concurrent exception in method codegen (#1685) 2022-09-29 20:28:01 +01:00
Skylot 2f301bf150 fix: don't mark constructor for inline if anonymous class inline is disabled (#1680) 2022-09-25 17:47:53 +01:00
Skylot b4892ce17f build: use fixed java version to build win artifacts 2022-09-23 21:08:16 +01:00
Skylot 151c171616 fix: handle empty block at end of else-if chain (#1674) 2022-09-23 20:40:56 +01:00
Skylot 79477a2de3 fix: don't rename bridged overridden methods (#1672) 2022-09-23 19:16:34 +01:00
dependabot[bot] 78aadda931 build(deps): bump actions/checkout from 2 to 3 (PR #1669)
Bumps [actions/checkout](https://github.com/actions/checkout) from 2 to 3.
- [Release notes](https://github.com/actions/checkout/releases)
- [Changelog](https://github.com/actions/checkout/blob/main/CHANGELOG.md)
- [Commits](https://github.com/actions/checkout/compare/v2...v3)

---
updated-dependencies:
- dependency-name: actions/checkout
  dependency-type: direct:production
  update-type: version-update:semver-major
...

Signed-off-by: dependabot[bot] <support@github.com>

Signed-off-by: dependabot[bot] <support@github.com>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
2022-09-12 18:30:35 +01:00
Jan S b50706505f fix(res): implemented parsing RES_TABLE_TYPE_LIBRARY chunks (#1663)(PR #1664)
* core: Implemented parsing RES_TABLE_TYPE_LIBRARY chunks

* skip unknown data at the end of type chunk
2022-09-10 16:58:26 +01:00
The Cobra Chicken 9114821fb1 feat(debugger): add logcat output (#1411)(PR #1666)
* Adding logcatController class and writing adb / debugger panel information to the controller.

* Finished parsing logcat binary output and writing an arraylist containing all events.

* added highlighting of logcat output based on type.  Added timestamp parsing.

* Updated code to only get new log messages.

* Added additional code for select all

* Completed Check and uncheckall options.

* Changed log highlighting to log color.  Changed from JTextArea to JTextPane. Logcat pane will now autoscroll only if it is already scrolled to the bottom.  Debugger exit will now stop logcat as well.

* Moved labels into NLS rather than using hardcoded strings.

* Implemented the ability to autoselect attached process.  Changed the formatting of logcat messages.

* Moved labels into NLS rather than using hardcoded strings.

* updating to use info getter methods rather than directly accessing variable

* Added Logcat Pause Button

* Added Clear button

* Updated clear icon

* Cleaning warnings

* cleaning

* Changed behavior to only show logcat for debugged process to start with.

* cleaning

* cleaning

* cleaning

* applying spotless

* Fixing bug with switch

* fixed formatting issue

* add missing localization strings

Co-authored-by: green9317 <38409554+green9317@users.noreply.github.com>
Co-authored-by: TheCobraChicken <jeffmlitfi@gmail.com>
Co-authored-by: Skylot <skylot@gmail.com>
2022-09-08 15:18:55 +01:00
Skylot 1195582da8 feat(gui): option for search results count per page (#1652) 2022-09-05 20:07:02 +01:00
Skylot 258987b0ff chore: update dependencies 2022-09-05 20:07:01 +01:00
Guilherme a6a734c70d fix(gui): update pt-BR translation (PR #1655) 2022-09-01 15:51:12 +01:00
Choiman1559 d6c23a2a9b fix(gui): update Korean translation (PR #1650)
* update korean
* Update Messages_ko_KR.properties
* Restore missing empty line

Co-authored-by: skylot <118523+skylot@users.noreply.github.com>
2022-08-20 17:59:37 +01:00
Skylot db028904d7 fix(gui): set legacy sort flag also for launch4j (#1628) 2022-08-20 17:37:26 +01:00
Skylot 63a571306c refactor: load resource table nodes in one change (#1648) 2022-08-19 22:15:04 +01:00
Skylot bc4db61e25 fix(gui): improve resources search (#1648) 2022-08-19 15:52:14 +01:00
Jan S c7e6e28830 fix(gui): improve log viewer dialog (#1311)(PR #1649)
* [gui]: improve log viewer dialog

* use method from UiUtils to set window icons

Co-authored-by: Skylot <skylot@gmail.com>
2022-08-18 19:55:41 +01:00
Skylot 1d7b6fdb2c fix(gui): additional checks on open search result (#1647) 2022-08-18 15:59:45 +01:00
Skylot ce5d8eeff8 fix: don't inline anonymous in self inner class (#1645) 2022-08-18 15:48:17 +01:00
Jan S 894e0e6132 fix: UnsupportedOperationException on adding a field (#1645)(PR #1646)
* fix: UnsupportedOperationException on adding a field

* changed list check and creation similar to safeAdd
2022-08-18 15:33:18 +01:00
Skylot 127f0ecf3f fix(gui): disable actions if files not loaded (#1644) 2022-08-16 21:28:57 +01:00
Skylot cf7767e702 fix(gui): handle null value in TableCellRenderer (#1642) 2022-08-16 20:48:23 +01:00
Skylot e0aedc7949 fix: improve top block search for try/catch (#1633) 2022-08-15 21:31:26 +01:00
Skylot bad78de74c perf: improve directory delete 2022-08-14 13:38:12 +01:00
Skylot 12df8a169f chore: update gradle and dependencies 2022-08-13 18:25:08 +01:00
Skylot 15c9d33339 fix(gui): handle possible classes overlap in disk cache (#1633) 2022-08-13 13:13:13 +00:00
Jiaxin Peng 7e0fafbaf1 fix(gui): fix broken FileDialog by using legacy sort (#1628)(PR #1630)
#1628 #1606 #1213 #1574 #1552
2022-08-11 13:59:46 +01:00
zhongqingsong 57b9c1dd7a fix(gui): update Messages_zh_CN.properties (PR #1627) 2022-08-11 13:29:15 +01:00
Skylot 8ba0c17259 fix: handle empty endless loop (#1611) 2022-08-10 22:07:52 +01:00
Areizen cd32151083 fix(gui): correct Frida snippet for constructor (PR #1605)
When hooking a constructor with Frida, call `$new` instead of `$init`. `$init` cannot be used to instantiate an object and is reserved for hooking.

Co-authored-by: Your Name <you@example.com>
2022-08-06 20:16:37 +01:00
Skylot 75b52d672e feat(gui): save project search history 2022-08-05 20:43:05 +01:00
Skylot 11d04508f7 feat(gui): add manual search, stop and sort actions to search dialog (#1600) 2022-08-05 20:09:33 +01:00
Skylot e641b773b5 fix(gui): improve search dialog performance 2022-08-05 14:53:48 +01:00
Skylot 6e5899c654 fix: checks for field init reorder (#1599) 2022-08-04 17:38:46 +01:00
Skylot c66ffaa7f9 feat(gui): show start page on jadx open 2022-08-03 16:44:55 +01:00
Skylot 5193c6a5d8 chore: add tool for automatically insert new i18n lines 2022-08-03 16:44:40 +01:00
Skylot e7212af547 chore: upgrade gradle wrapper to 7.5 2022-08-03 18:44:10 +03:00
FixTheBug 3ca1357af4 fix(gui): sort resources by deobfuscated name (#1595)(PR #1598)
Co-authored-by: /paul-nguyen-goldenowl <paul.nguyen.goldenowl@gmail.com>
2022-08-01 14:54:22 +01:00
Guilherme 90e95213e4 feat(gui): add Brazilian Portuguese translation (PR #1596)
* feat(translation): add pt-br
* fix: `adb_dialog` prefix deleted
2022-08-01 12:15:35 +01:00
Jan S ae2d4da585 fix(res): XML "@null" decoding (#1583)(PR #1594)
minor improvements
2022-07-31 13:50:32 +01:00
Skylot 691d5cd1e6 fix: hide unused label before exception handler in simple mode 2022-07-30 17:33:23 +01:00
Skylot 58a46c6417 fix(gui): add constructors usage into class usage (#1591) 2022-07-30 17:22:32 +01:00
Skylot d3f6160e62 feat: add option to disable finally block extraction (#1592) 2022-07-30 14:07:43 +01:00
Skylot 03e4afb12f fix: check variables before merge in finally block (#1592) 2022-07-30 13:48:53 +01:00
Hen_Ry 6802f6028e fix(gui): update german translation (PR #1554)
* german update
* Fix
* applied the latest changes as discussed

Co-authored-by: Jan Peter Stotz <jpstotz@users.noreply.github.com>
2022-07-27 17:45:36 +01:00
SiderealArt 5ca61cfe18 fix(gui): update zh-tw translation (PR #1589) 2022-07-27 11:16:11 +01:00
Julian Burner 32d55b48f2 fix(gui): replace mixed-up quotation marks with period (PR #1588) 2022-07-26 10:16:34 +01:00
Skylot ab4b6f9e54 feat: select better resource name (#1581) 2022-07-25 19:53:03 +01:00
Jan S 9100ad1220 fix(debugger): resolve NPE in adb device viewer (#1585) (PR #1586) 2022-07-25 17:44:55 +01:00
Skylot 8b4f8fb572 fix: resolve inherited method to use correct alias (#1582) 2022-07-24 19:15:52 +03:00
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
Skylot fcd58ae76f fix: remap class names for store in disk cache (#1503) 2022-05-30 18:16:05 +01:00
Skylot df380dea27 chore: update dependencies 2022-05-30 20:06:23 +03:00
CKCat 9d88592391 fix(res): ignore version in AndroidManifest.xml (#1502)(PR #1504) 2022-05-30 14:10:10 +01:00
dependabot[bot] c906c11b0f build(deps): bump github/codeql-action from 1 to 2 (PR #1500)
Bumps [github/codeql-action](https://github.com/github/codeql-action) from 1 to 2.
- [Release notes](https://github.com/github/codeql-action/releases)
- [Changelog](https://github.com/github/codeql-action/blob/main/CHANGELOG.md)
- [Commits](https://github.com/github/codeql-action/compare/v1...v2)

---
updated-dependencies:
- dependency-name: github/codeql-action
  dependency-type: direct:production
  update-type: version-update:semver-major
...

Signed-off-by: dependabot[bot] <support@github.com>

Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
2022-05-28 18:04:08 +01:00
Skylot 4fbc56cdb0 build: add unstable win-with-jre bundle 2022-05-28 17:46:48 +01:00
Skylot 98c0416b20 fix(gui): correct close and reopen for decompiler and cache 2022-05-28 16:41:37 +03:00
ZachQin fa41874e30 feat(gui): add parameters logging in Frida code snippet (#1497)(PR #1498) 2022-05-28 14:39:02 +01:00
Skylot 2aa6c99c90 fix: skip dex files with parsing errors (#1495) 2022-05-28 13:27:29 +01:00
Skylot 5f60c0f1bb build: fix google-java-format for all java versions 2022-05-27 22:51:52 +01:00
Skylot cb741db623 fix: improve usage search, refactor java nodes creation (#1489) 2022-05-27 17:56:08 +01:00
Skylot 1df217c4a0 fix: save cache dir for reuse on project save/reopen 2022-05-27 16:50:13 +01:00
Skylot 81f209ba9e fix: check if directory exists before delete (#1493) 2022-05-26 19:23:59 +03:00
zhongqingsong 34a31aa7df fix(gui): complete Chinese Translation (PR #1492)
1. Complete translation of Chinese
2. Polish up part of the translation
3. Restore all text for subsequent translation
2022-05-24 19:09:11 +01:00
Skylot 5099e02c9b fix(gui): correct merge for plugin options from command line (#1490) 2022-05-23 19:44:49 +01:00
Skylot f364b39b29 fix(gui): save full type info in metadata (#1487) 2022-05-22 16:18:38 +01:00
Skylot 4cd4746f9a fix(gui): save variable name to show in tooltip (#1487) 2022-05-22 15:34:09 +01:00
Skylot 6448f0e32b fix: use variable length encoding instead short for offsets (can overflow) (#1489) 2022-05-22 14:23:15 +01:00
Skylot e07332d49a fix(gui): resolve cast exception for variable reference (#1489) 2022-05-21 21:33:58 +01:00
Skylot bd8a44c4c9 fix(gui): correct handle of selected file in save dialog 2022-05-21 21:31:47 +01:00
272 changed files with 9413 additions and 2651 deletions
+7
View File
@@ -0,0 +1,7 @@
version: 2
updates:
# Set update schedule for GitHub Actions
- package-ecosystem: "github-actions"
directory: "/"
schedule:
interval: "weekly"
+88
View File
@@ -0,0 +1,88 @@
name: Build Artifacts
on:
push:
branches: [ master, build-test ]
jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
with:
fetch-depth: 0
- name: Set up JDK
uses: actions/setup-java@v3
with:
distribution: 'adopt'
java-version: 8
- name: Set jadx version
run: |
JADX_LAST_TAG=$(git describe --abbrev=0 --tags)
JADX_VERSION="${JADX_LAST_TAG:1}.$GITHUB_RUN_NUMBER-${GITHUB_SHA:0:8}"
echo "JADX_VERSION=$JADX_VERSION" >> $GITHUB_ENV
- uses: burrunan/gradle-cache-action@v1
name: Build with Gradle
env:
TERM: dumb
with:
arguments: clean dist copyExe
- name: Save bundle artifact
uses: actions/upload-artifact@v3
with:
name: ${{ format('jadx-{0}', env.JADX_VERSION) }}
# Waiting fix for https://github.com/actions/upload-artifact/issues/39 to upload zip file
# Upload unpacked files for now
path: build/jadx/**/*
if-no-files-found: error
retention-days: 30
- name: Save exe artifact
uses: actions/upload-artifact@v3
with:
name: ${{ format('jadx-gui-{0}-no-jre-win.exe', env.JADX_VERSION) }}
path: build/*.exe
if-no-files-found: error
retention-days: 30
build-win-bundle:
runs-on: windows-latest
steps:
- uses: actions/checkout@v3
with:
fetch-depth: 0
- name: Set up JDK
uses: oracle-actions/setup-java@v1
with:
release: 18
- name: Print Java version
shell: bash
run: java -version
- name: Set jadx version
shell: bash
run: |
JADX_LAST_TAG=$(git describe --abbrev=0 --tags)
JADX_VERSION="${JADX_LAST_TAG:1}.$GITHUB_RUN_NUMBER-${GITHUB_SHA:0:8}"
echo "JADX_VERSION=$JADX_VERSION" >> $GITHUB_ENV
- uses: gradle/gradle-build-action@v2
name: Build with Gradle
env:
TERM: dumb
with:
arguments: clean dist -PbundleJRE=true
- name: Save exe bundle artifact
uses: actions/upload-artifact@v3
with:
name: ${{ format('jadx-gui-{0}-with-jre-win', env.JADX_VERSION) }}
path: jadx-gui/build/*-with-jre-win/*
if-no-files-found: error
retention-days: 30
+28
View File
@@ -0,0 +1,28 @@
name: Build Test
on:
push:
branches: [ master, build-test ]
pull_request:
branches: [ master ]
jobs:
tests:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
with:
fetch-depth: 0
- name: Set up JDK
uses: actions/setup-java@v3
with:
distribution: 'adopt'
java-version: 8
- uses: burrunan/gradle-cache-action@v1
name: Build with Gradle
env:
TERM: dumb
with:
arguments: clean build dist copyExe --warning-mode=all
-52
View File
@@ -1,52 +0,0 @@
name: Build
on:
push:
branches: [ master ]
pull_request:
branches: [ master ]
jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
with:
fetch-depth: 0
- name: Set up JDK
uses: actions/setup-java@v1
with:
java-version: 8
- name: Set jadx version
run: |
JADX_LAST_TAG=$(git describe --abbrev=0 --tags)
JADX_VERSION="${JADX_LAST_TAG:1}.$GITHUB_RUN_NUMBER-${GITHUB_SHA:0:8}"
echo "JADX_VERSION=$JADX_VERSION" >> $GITHUB_ENV
- uses: burrunan/gradle-cache-action@v1
name: Build with Gradle
env:
TERM: dumb
TEST_INPUT_PLUGIN: dx
with:
arguments: clean build dist copyExe --warning-mode=all
- name: Save bundle artifact
if: success() && github.event_name == 'push'
uses: actions/upload-artifact@v2
with:
name: ${{ format('jadx-{0}', env.JADX_VERSION) }}
# Waiting fix for https://github.com/actions/upload-artifact/issues/39 to upload zip file
# Upload unpacked files for now
path: build/jadx/**/*
if-no-files-found: error
- name: Save exe artifact
if: success() && github.event_name == 'push'
uses: actions/upload-artifact@v2
with:
name: ${{ format('jadx-gui-{0}-no-jre-win.exe', env.JADX_VERSION) }}
path: build/*.exe
if-no-files-found: error
+3 -3
View File
@@ -25,10 +25,10 @@ jobs:
steps: steps:
- name: Checkout repository - name: Checkout repository
uses: actions/checkout@v2 uses: actions/checkout@v3
- name: Initialize CodeQL - name: Initialize CodeQL
uses: github/codeql-action/init@v1 uses: github/codeql-action/init@v2
with: with:
queries: +security-extended queries: +security-extended
languages: ${{ matrix.language }} languages: ${{ matrix.language }}
@@ -38,4 +38,4 @@ jobs:
./gradlew clean build -x checkstyleTest -x checkstyleMain -x test -x ':jadx-core:testClasses' ./gradlew clean build -x checkstyleTest -x checkstyleMain -x test -x ':jadx-core:testClasses'
- name: Perform CodeQL Analysis - name: Perform CodeQL Analysis
uses: github/codeql-action/analyze@v1 uses: github/codeql-action/analyze@v2
@@ -6,5 +6,5 @@ jobs:
name: "Validation" name: "Validation"
runs-on: ubuntu-latest runs-on: ubuntu-latest
steps: steps:
- uses: actions/checkout@v2 - uses: actions/checkout@v3
- uses: gradle/wrapper-validation-action@v1 - uses: gradle/wrapper-validation-action@v1
+25 -14
View File
@@ -3,8 +3,10 @@
## JADX ## JADX
[![Build status](https://github.com/skylot/jadx/workflows/Build/badge.svg)](https://github.com/skylot/jadx/actions?query=workflow%3ABuild) [![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/) ![GitHub contributors](https://img.shields.io/github/contributors/skylot/jadx)
[![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 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) [![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) [![License](http://img.shields.io/:license-apache-blue.svg)](http://www.apache.org/licenses/LICENSE-2.0.html)
@@ -33,8 +35,9 @@ See these features in action here: [jadx-gui features overview](https://github.c
<img src="https://user-images.githubusercontent.com/118523/142730720-839f017e-38db-423e-b53f-39f5f0a0316f.png" width="700"/> <img src="https://user-images.githubusercontent.com/118523/142730720-839f017e-38db-423e-b53f-39f5f0a0316f.png" width="700"/>
### Download ### Download
- release from [github: ![Latest release](https://img.shields.io/github/release/skylot/jadx.svg)](https://github.com/skylot/jadx/releases/latest) - release
- latest [unstable build](https://nightly.link/skylot/jadx/workflows/build/master) from [github: ![Latest release](https://img.shields.io/github/release/skylot/jadx.svg)](https://github.com/skylot/jadx/releases/latest)
- 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: After download unpack zip file go to `bin` directory and run:
- `jadx` - command line version - `jadx` - command line version
@@ -45,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). For Windows, you can download it from [oracle.com](https://www.oracle.com/java/technologies/downloads/#jdk17-windows) (select x64 Installer).
### Install ### Install
1. Arch linux 1. Arch linux ![Arch Linux package](https://img.shields.io/archlinux/v/community/any/jadx?label=)
```bash ```bash
sudo pacman -S jadx sudo pacman -S jadx
``` ```
2. macOS 2. macOS ![homebrew version](https://img.shields.io/homebrew/v/jadx?label=)
```bash ```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 ### 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) You can use jadx in your java projects, check details on [wiki page](https://github.com/skylot/jadx/wiki/Use-jadx-as-a-library)
@@ -95,6 +102,7 @@ options:
--add-debug-lines - add comments with debug line numbers if available --add-debug-lines - add comments with debug line numbers if available
--no-inline-anonymous - disable anonymous classes inline --no-inline-anonymous - disable anonymous classes inline
--no-inline-methods - disable methods inline --no-inline-methods - disable methods inline
--no-finally - don't extract finally block
--no-replace-consts - don't replace constant value with matching constant field --no-replace-consts - don't replace constant value with matching constant field
--escape-unicode - escape non latin characters in strings (with \u) --escape-unicode - escape non latin characters in strings (with \u)
--respect-bytecode-access-modifiers - don't change original access modifiers --respect-bytecode-access-modifiers - don't change original access modifiers
@@ -107,9 +115,12 @@ options:
'read-or-save' - read if found, save otherwise (don't overwrite) 'read-or-save' - read if found, save otherwise (don't overwrite)
'overwrite' - don't read, always save 'overwrite' - don't read, always save
'ignore' - don't read and don't 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-use-sourcename - use source file name as class name alias
--deobf-parse-kotlin-metadata - parse kotlin metadata to class and package names --deobf-parse-kotlin-metadata - parse kotlin metadata to class and package names
--deobf-res-name-source - better name source for resources:
'auto' - automatically select best name (default)
'resources' - use resources names
'code' - use R class fields names
--use-kotlin-methods-for-var-names - use kotlin intrinsic methods to rename variables, values: disable, apply, apply-and-hide, default: apply --use-kotlin-methods-for-var-names - use kotlin intrinsic methods to rename variables, values: disable, apply, apply-and-hide, default: apply
--rename-flags - fix options (comma-separated list of): --rename-flags - fix options (comma-separated list of):
'case' - fix case sensitivity issues (according to --fs-case-sensitive option), 'case' - fix case sensitivity issues (according to --fs-case-sensitive option),
@@ -130,11 +141,11 @@ options:
-h, --help - print this help -h, --help - print this help
Plugin options (-P<name>=<value>): Plugin options (-P<name>=<value>):
1) dex-input (Load .dex and .apk files) 1) dex-input: Load .dex and .apk files
-Pdex-input.verify-checksum - Verify dex file checksum before load, values: [yes, no], default: yes - dex-input.verify-checksum - verify dex file checksum before load, values: [yes, no], default: yes
2) java-convert (Convert .jar and .class files to dex) 2) java-convert: Convert .class, .jar and .aar files to dex
-Pjava-convert.mode - Convert mode, values: [dx, d8, both], default: both - java-convert.mode - convert mode, values: [dx, d8, both], default: both
-Pjava-convert.d8-desugar - Use desugar in d8, values: [yes, no], default: no - java-convert.d8-desugar - use desugar in d8, values: [yes, no], default: no
Examples: Examples:
jadx -d out classes.dex jadx -d out classes.dex
-33
View File
@@ -1,33 +0,0 @@
<?xml version="1.0" encoding="UTF-8"?>
<component type="desktop">
<id>com.github.skylot.jadx</id>
<metadata_license>CC0-1.0</metadata_license>
<project_license>Apache-2.0</project_license>
<name>JADX</name>
<summary>Dex to Java decompiler</summary>
<description>
<p>Command line and GUI tools for producing Java source code from Android Dex and Apk files</p>
<ul>
<li>decompile Dalvik bytecode to java classes from APK, dex, aar, aab and zip files</li>
<li>decode AndroidManifest.xml and other resources from resources.arsc</li>
<li>deobfuscator included</li>
<li>view decompiled code with highlighted syntax</li>
<li>jump to declaration</li>
<li>find usage</li>
<li>full text search</li>
<li>smali debugger</li>
</ul>
</description>
<screenshots>
<screenshot type="default">
<image>https://user-images.githubusercontent.com/118523/142730720-839f017e-38db-423e-b53f-39f5f0a0316f.png</image>
</screenshot>
</screenshots>
<content_rating type="oars-1.1" />
<launchable type="desktop-id">com.github.skylot.jadx.desktop</launchable>
<url type="homepage">https://github.com/skylot/jadx</url>
<url type="bugtracker">https://github.com/skylot/jadx/issues</url>
<releases>
<release version="1.3.4" date="2022-03-20" />
</releases>
</component>
+14 -16
View File
@@ -1,6 +1,6 @@
plugins { plugins {
id 'com.github.ben-manes.versions' version '0.42.0' id 'com.github.ben-manes.versions' version '0.43.0'
id 'com.diffplug.spotless' version '6.5.0' id 'com.diffplug.spotless' version '6.11.0'
} }
ext.jadxVersion = System.getenv('JADX_VERSION') ?: "dev" ext.jadxVersion = System.getenv('JADX_VERSION') ?: "dev"
@@ -14,7 +14,6 @@ allprojects {
version = jadxVersion version = jadxVersion
sourceCompatibility = JavaVersion.VERSION_1_8 sourceCompatibility = JavaVersion.VERSION_1_8
targetCompatibility = JavaVersion.VERSION_1_8
compileJava { compileJava {
options.encoding = "UTF-8" options.encoding = "UTF-8"
@@ -27,16 +26,16 @@ allprojects {
} }
dependencies { dependencies {
implementation 'org.slf4j:slf4j-api:1.7.36' implementation 'org.slf4j:slf4j-api:2.0.3'
compileOnly 'org.jetbrains:annotations:23.0.0' compileOnly 'org.jetbrains:annotations:23.0.0'
testImplementation 'ch.qos.logback:logback-classic:1.2.11' testImplementation 'ch.qos.logback:logback-classic:1.3.4'
testImplementation 'org.hamcrest:hamcrest-library:2.2' testImplementation 'org.hamcrest:hamcrest-library:2.2'
testImplementation 'org.mockito:mockito-core:4.5.1' testImplementation 'org.mockito:mockito-core:4.8.0'
testImplementation 'org.assertj:assertj-core:3.22.0' testImplementation 'org.assertj:assertj-core:3.23.1'
testImplementation 'org.junit.jupiter:junit-jupiter-api:5.8.2' testImplementation 'org.junit.jupiter:junit-jupiter-api:5.9.1'
testRuntimeOnly 'org.junit.jupiter:junit-jupiter-engine:5.8.2' testRuntimeOnly 'org.junit.jupiter:junit-jupiter-engine:5.9.1'
testCompileOnly 'org.jetbrains:annotations:23.0.0' testCompileOnly 'org.jetbrains:annotations:23.0.0'
} }
@@ -50,6 +49,11 @@ allprojects {
mavenLocal() mavenLocal()
mavenCentral() mavenCentral()
google() google()
// Commented out for now since we're using a local mapping-io fork atm.
// maven {
// name 'FabricMC'
// url 'https://maven.fabricmc.net/'
// }
} }
} }
@@ -64,13 +68,7 @@ spotless {
importOrderFile 'config/code-formatter/eclipse.importorder' importOrderFile 'config/code-formatter/eclipse.importorder'
eclipse().configFile 'config/code-formatter/eclipse.xml' eclipse().configFile 'config/code-formatter/eclipse.xml'
if (JavaVersion.current() < JavaVersion.VERSION_16) { removeUnusedImports()
removeUnusedImports()
} else {
// google-format on Java 16+ issue: https://github.com/diffplug/spotless/issues/834
println('Warning! Unused imports remove is disabled for Java 16+'
+ ' (use workaround from https://github.com/diffplug/spotless/tree/main/plugin-gradle#google-java-format)')
}
lineEndings(com.diffplug.spotless.LineEnding.UNIX) lineEndings(com.diffplug.spotless.LineEnding.UNIX)
encoding("UTF-8") encoding("UTF-8")
+9
View File
@@ -1,2 +1,11 @@
org.gradle.warning.mode=all org.gradle.warning.mode=all
org.gradle.parallel=true org.gradle.parallel=true
# Flags for google-java-format (optimize imports by spotless) for Java >= 16.
# Java < 9 will ignore unsupported flags (thanks to -XX:+IgnoreUnrecognizedVMOptions)
org.gradle.jvmargs=-XX:+IgnoreUnrecognizedVMOptions \
--add-exports='jdk.compiler/com.sun.tools.javac.api=ALL-UNNAMED' \
--add-exports='jdk.compiler/com.sun.tools.javac.file=ALL-UNNAMED' \
--add-exports='jdk.compiler/com.sun.tools.javac.parser=ALL-UNNAMED' \
--add-exports='jdk.compiler/com.sun.tools.javac.tree=ALL-UNNAMED' \
--add-exports='jdk.compiler/com.sun.tools.javac.util=ALL-UNNAMED'
Binary file not shown.
+2 -2
View File
@@ -1,6 +1,6 @@
distributionBase=GRADLE_USER_HOME distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists distributionPath=wrapper/dists
distributionSha256Sum=29e49b10984e585d8118b7d0bc452f944e386458df27371b49b4ac1dec4b7fda distributionSha256Sum=f6b8596b10cce501591e92f229816aa4046424f3b24d771751b06779d58c8ec4
distributionUrl=https\://services.gradle.org/distributions/gradle-7.4.2-bin.zip distributionUrl=https\://services.gradle.org/distributions/gradle-7.5.1-bin.zip
zipStoreBase=GRADLE_USER_HOME zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists zipStorePath=wrapper/dists
Vendored
+6
View File
@@ -205,6 +205,12 @@ set -- \
org.gradle.wrapper.GradleWrapperMain \ org.gradle.wrapper.GradleWrapperMain \
"$@" "$@"
# Stop when "xargs" is not available.
if ! command -v xargs >/dev/null 2>&1
then
echo "xargs is not available"
fi
# Use "xargs" to parse quoted args. # Use "xargs" to parse quoted args.
# #
# With -n1 it outputs one arg per line, with the quotes and backslashes removed. # With -n1 it outputs one arg per line, with the quotes and backslashes removed.
Vendored
+8 -6
View File
@@ -14,7 +14,7 @@
@rem limitations under the License. @rem limitations under the License.
@rem @rem
@if "%DEBUG%" == "" @echo off @if "%DEBUG%"=="" @echo off
@rem ########################################################################## @rem ##########################################################################
@rem @rem
@rem Gradle startup script for Windows @rem Gradle startup script for Windows
@@ -25,7 +25,7 @@
if "%OS%"=="Windows_NT" setlocal if "%OS%"=="Windows_NT" setlocal
set DIRNAME=%~dp0 set DIRNAME=%~dp0
if "%DIRNAME%" == "" set DIRNAME=. if "%DIRNAME%"=="" set DIRNAME=.
set APP_BASE_NAME=%~n0 set APP_BASE_NAME=%~n0
set APP_HOME=%DIRNAME% set APP_HOME=%DIRNAME%
@@ -40,7 +40,7 @@ if defined JAVA_HOME goto findJavaFromJavaHome
set JAVA_EXE=java.exe set JAVA_EXE=java.exe
%JAVA_EXE% -version >NUL 2>&1 %JAVA_EXE% -version >NUL 2>&1
if "%ERRORLEVEL%" == "0" goto execute if %ERRORLEVEL% equ 0 goto execute
echo. echo.
echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH.
@@ -75,13 +75,15 @@ set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar
:end :end
@rem End local scope for the variables with windows NT shell @rem End local scope for the variables with windows NT shell
if "%ERRORLEVEL%"=="0" goto mainEnd if %ERRORLEVEL% equ 0 goto mainEnd
:fail :fail
rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of
rem the _cmd.exe /c_ return code! rem the _cmd.exe /c_ return code!
if not "" == "%GRADLE_EXIT_CONSOLE%" exit 1 set EXIT_CODE=%ERRORLEVEL%
exit /b 1 if %EXIT_CODE% equ 0 set EXIT_CODE=1
if not ""=="%GRADLE_EXIT_CONSOLE%" exit %EXIT_CODE%
exit /b %EXIT_CODE%
:mainEnd :mainEnd
if "%OS%"=="Windows_NT" endlocal if "%OS%"=="Windows_NT" endlocal
+1 -1
View File
@@ -11,7 +11,7 @@ dependencies {
runtimeOnly(project(':jadx-plugins:jadx-smali-input')) runtimeOnly(project(':jadx-plugins:jadx-smali-input'))
implementation 'com.beust:jcommander:1.82' implementation 'com.beust:jcommander:1.82'
implementation 'ch.qos.logback:logback-classic:1.2.11' implementation 'ch.qos.logback:logback-classic:1.3.4'
} }
application { application {
@@ -8,6 +8,7 @@ import java.util.LinkedHashMap;
import java.util.List; import java.util.List;
import java.util.Locale; import java.util.Locale;
import java.util.Map; import java.util.Map;
import java.util.function.Supplier;
import org.jetbrains.annotations.Nullable; import org.jetbrains.annotations.Nullable;
@@ -22,6 +23,7 @@ import jadx.api.plugins.JadxPluginInfo;
import jadx.api.plugins.JadxPluginManager; import jadx.api.plugins.JadxPluginManager;
import jadx.api.plugins.options.JadxPluginOptions; import jadx.api.plugins.options.JadxPluginOptions;
import jadx.api.plugins.options.OptionDescription; import jadx.api.plugins.options.OptionDescription;
import jadx.core.utils.Utils;
public class JCommanderWrapper<T> { public class JCommanderWrapper<T> {
private final JCommander jc; private final JCommander jc;
@@ -50,12 +52,24 @@ public class JCommanderWrapper<T> {
if (parameter.isAssigned()) { if (parameter.isAssigned()) {
// copy assigned field value to obj // copy assigned field value to obj
Parameterized parameterized = parameter.getParameterized(); Parameterized parameterized = parameter.getParameterized();
Object val = parameterized.get(parameter.getObject()); Object providedValue = parameterized.get(parameter.getObject());
parameterized.set(obj, val); Object newValue = mergeValues(parameterized.getType(), providedValue, () -> parameterized.get(obj));
parameterized.set(obj, newValue);
} }
} }
} }
@SuppressWarnings({ "rawtypes", "unchecked" })
private static Object mergeValues(Class<?> type, Object value, Supplier<Object> prevValueProvider) {
if (type.isAssignableFrom(Map.class)) {
// merge maps instead replacing whole map
Map prevMap = (Map) prevValueProvider.get();
return Utils.mergeMaps(prevMap, (Map) value); // value map will override keys in prevMap
}
// simple override
return value;
}
public void printUsage() { public void printUsage() {
// print usage in not sorted fields order (by default its sorted by description) // print usage in not sorted fields order (by default its sorted by description)
PrintStream out = System.out; PrintStream out = System.out;
@@ -179,11 +193,11 @@ public class JCommanderWrapper<T> {
return false; return false;
} }
JadxPluginInfo pluginInfo = plugin.getPluginInfo(); JadxPluginInfo pluginInfo = plugin.getPluginInfo();
out.append("\n ").append(k).append(") "); out.append("\n ").append(k).append(") ");
out.append(pluginInfo.getPluginId()).append(" (").append(pluginInfo.getDescription()).append(") "); out.append(pluginInfo.getPluginId()).append(": ").append(pluginInfo.getDescription());
for (OptionDescription desc : descs) { for (OptionDescription desc : descs) {
StringBuilder opt = new StringBuilder(); StringBuilder opt = new StringBuilder();
opt.append(" -P").append(desc.name()); opt.append(" - ").append(desc.name());
addSpaces(opt, maxNamesLen - opt.length()); addSpaces(opt, maxNamesLen - opt.length());
opt.append("- ").append(desc.description()); opt.append("- ").append(desc.description());
if (!desc.values().isEmpty()) { if (!desc.values().isEmpty()) {
+8 -2
View File
@@ -66,8 +66,14 @@ public class JadxCLI {
private static boolean checkForErrors(JadxDecompiler jadx) { private static boolean checkForErrors(JadxDecompiler jadx) {
if (jadx.getRoot().getClasses().isEmpty()) { if (jadx.getRoot().getClasses().isEmpty()) {
LOG.error("Load failed! No classes for decompile!"); if (jadx.getArgs().isSkipResources()) {
return true; 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) { if (jadx.getErrorsCount() > 0) {
LOG.error("Load with errors! Check log for details"); LOG.error("Load with errors! Check log for details");
@@ -21,6 +21,7 @@ import jadx.api.JadxArgs.RenameEnum;
import jadx.api.JadxArgs.UseKotlinMethodsForVarNames; import jadx.api.JadxArgs.UseKotlinMethodsForVarNames;
import jadx.api.JadxDecompiler; import jadx.api.JadxDecompiler;
import jadx.api.args.DeobfuscationMapFileMode; import jadx.api.args.DeobfuscationMapFileMode;
import jadx.api.args.ResourceNameSource;
import jadx.core.utils.exceptions.JadxException; import jadx.core.utils.exceptions.JadxException;
import jadx.core.utils.files.FileUtils; import jadx.core.utils.files.FileUtils;
@@ -88,6 +89,9 @@ public class JadxCLIArgs {
@Parameter(names = { "--no-inline-methods" }, description = "disable methods inline") @Parameter(names = { "--no-inline-methods" }, description = "disable methods inline")
protected boolean inlineMethods = true; protected boolean inlineMethods = true;
@Parameter(names = "--no-finally", description = "don't extract finally block")
protected boolean extractFinally = true;
@Parameter(names = "--no-replace-consts", description = "don't replace constant value with matching constant field") @Parameter(names = "--no-replace-consts", description = "don't replace constant value with matching constant field")
protected boolean replaceConsts = true; protected boolean replaceConsts = true;
@@ -123,15 +127,22 @@ public class JadxCLIArgs {
) )
protected DeobfuscationMapFileMode deobfuscationMapFileMode = DeobfuscationMapFileMode.READ; 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") @Parameter(names = { "--deobf-use-sourcename" }, description = "use source file name as class name alias")
protected boolean deobfuscationUseSourceNameAsAlias = false; protected boolean deobfuscationUseSourceNameAsAlias = false;
@Parameter(names = { "--deobf-parse-kotlin-metadata" }, description = "parse kotlin metadata to class and package names") @Parameter(names = { "--deobf-parse-kotlin-metadata" }, description = "parse kotlin metadata to class and package names")
protected boolean deobfuscationParseKotlinMetadata = false; protected boolean deobfuscationParseKotlinMetadata = false;
@Parameter(
names = { "--deobf-res-name-source" },
description = "better name source for resources:"
+ "\n 'auto' - automatically select best name (default)"
+ "\n 'resources' - use resources names"
+ "\n 'code' - use R class fields names",
converter = ResourceNameSourceConverter.class
)
protected ResourceNameSource resourceNameSource = ResourceNameSource.AUTO;
@Parameter( @Parameter(
names = { "--use-kotlin-methods-for-var-names" }, names = { "--use-kotlin-methods-for-var-names" },
description = "use kotlin intrinsic methods to rename variables, values: disable, apply, apply-and-hide", description = "use kotlin intrinsic methods to rename variables, values: disable, apply, apply-and-hide",
@@ -259,16 +270,13 @@ public class JadxCLIArgs {
args.setReplaceConsts(replaceConsts); args.setReplaceConsts(replaceConsts);
args.setDeobfuscationOn(deobfuscationOn); args.setDeobfuscationOn(deobfuscationOn);
args.setDeobfuscationMapFile(FileUtils.toFile(deobfuscationMapFile)); args.setDeobfuscationMapFile(FileUtils.toFile(deobfuscationMapFile));
if (deobfuscationForceSave) { args.setDeobfuscationMapFileMode(deobfuscationMapFileMode);
args.setDeobfuscationMapFileMode(DeobfuscationMapFileMode.OVERWRITE);
} else {
args.setDeobfuscationMapFileMode(deobfuscationMapFileMode);
}
args.setDeobfuscationMinLength(deobfuscationMinLength); args.setDeobfuscationMinLength(deobfuscationMinLength);
args.setDeobfuscationMaxLength(deobfuscationMaxLength); args.setDeobfuscationMaxLength(deobfuscationMaxLength);
args.setUseSourceNameAsClassAlias(deobfuscationUseSourceNameAsAlias); args.setUseSourceNameAsClassAlias(deobfuscationUseSourceNameAsAlias);
args.setParseKotlinMetadata(deobfuscationParseKotlinMetadata); args.setParseKotlinMetadata(deobfuscationParseKotlinMetadata);
args.setUseKotlinMethodsForVarNames(useKotlinMethodsForVarNames); args.setUseKotlinMethodsForVarNames(useKotlinMethodsForVarNames);
args.setResourceNameSource(resourceNameSource);
args.setEscapeUnicode(escapeUnicode); args.setEscapeUnicode(escapeUnicode);
args.setRespectBytecodeAccModifiers(respectBytecodeAccessModifiers); args.setRespectBytecodeAccModifiers(respectBytecodeAccessModifiers);
args.setExportAsGradleProject(exportAsGradleProject); args.setExportAsGradleProject(exportAsGradleProject);
@@ -277,6 +285,7 @@ public class JadxCLIArgs {
args.setInsertDebugLines(addDebugLines); args.setInsertDebugLines(addDebugLines);
args.setInlineAnonymousClasses(inlineAnonymousClasses); args.setInlineAnonymousClasses(inlineAnonymousClasses);
args.setInlineMethods(inlineMethods); args.setInlineMethods(inlineMethods);
args.setExtractFinally(extractFinally);
args.setRenameFlags(renameFlags); args.setRenameFlags(renameFlags);
args.setFsCaseSensitive(fsCaseSensitive); args.setFsCaseSensitive(fsCaseSensitive);
args.setCommentsLevel(commentsLevel); args.setCommentsLevel(commentsLevel);
@@ -357,6 +366,10 @@ public class JadxCLIArgs {
return inlineMethods; return inlineMethods;
} }
public boolean isExtractFinally() {
return extractFinally;
}
public boolean isDeobfuscationOn() { public boolean isDeobfuscationOn() {
return deobfuscationOn; return deobfuscationOn;
} }
@@ -377,10 +390,6 @@ public class JadxCLIArgs {
return deobfuscationMapFileMode; return deobfuscationMapFileMode;
} }
public boolean isDeobfuscationForceSave() {
return deobfuscationForceSave;
}
public boolean isDeobfuscationUseSourceNameAsAlias() { public boolean isDeobfuscationUseSourceNameAsAlias() {
return deobfuscationUseSourceNameAsAlias; return deobfuscationUseSourceNameAsAlias;
} }
@@ -389,6 +398,10 @@ public class JadxCLIArgs {
return deobfuscationParseKotlinMetadata; return deobfuscationParseKotlinMetadata;
} }
public ResourceNameSource getResourceNameSource() {
return resourceNameSource;
}
public UseKotlinMethodsForVarNames getUseKotlinMethodsForVarNames() { public UseKotlinMethodsForVarNames getUseKotlinMethodsForVarNames() {
return useKotlinMethodsForVarNames; return useKotlinMethodsForVarNames;
} }
@@ -513,6 +526,19 @@ public class JadxCLIArgs {
} }
} }
public static class ResourceNameSourceConverter implements IStringConverter<ResourceNameSource> {
@Override
public ResourceNameSource convert(String value) {
try {
return ResourceNameSource.valueOf(value.toUpperCase());
} catch (Exception e) {
throw new IllegalArgumentException(
'\'' + value + "' is unknown, possible values are: "
+ JadxCLIArgs.enumValuesString(ResourceNameSource.values()));
}
}
}
public static class DecompilationModeConverter implements IStringConverter<DecompilationMode> { public static class DecompilationModeConverter implements IStringConverter<DecompilationMode> {
@Override @Override
public DecompilationMode convert(String value) { public DecompilationMode convert(String value) {
@@ -1,9 +1,14 @@
package jadx.cli; package jadx.cli;
import java.util.Collections;
import java.util.Map;
import org.hamcrest.Matchers;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import static jadx.core.utils.Utils.newConstStringMap;
import static org.hamcrest.MatcherAssert.assertThat; import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.is; import static org.hamcrest.Matchers.is;
@@ -47,6 +52,40 @@ public class JadxCLIArgsTest {
assertThat(override(args, "").isUseImports(), is(false)); assertThat(override(args, "").isUseImports(), is(false));
} }
@Test
public void testPluginOptionsOverride() {
// add key to empty base map
checkPluginOptionsMerge(
Collections.emptyMap(),
"-Poption=otherValue",
newConstStringMap("option", "otherValue"));
// override one key
checkPluginOptionsMerge(
newConstStringMap("option", "value"),
"-Poption=otherValue",
newConstStringMap("option", "otherValue"));
// merge different keys
checkPluginOptionsMerge(
Collections.singletonMap("option1", "value1"),
"-Poption2=otherValue2",
newConstStringMap("option1", "value1", "option2", "otherValue2"));
// merge and override
checkPluginOptionsMerge(
newConstStringMap("option1", "value1", "option2", "value2"),
"-Poption2=otherValue2",
newConstStringMap("option1", "value1", "option2", "otherValue2"));
}
private void checkPluginOptionsMerge(Map<String, String> baseMap, String providedArgs, Map<String, String> expectedMap) {
JadxCLIArgs args = new JadxCLIArgs();
args.pluginOptions = baseMap;
Map<String, String> resultMap = override(args, providedArgs).getPluginOptions();
assertThat(resultMap, Matchers.equalTo(expectedMap));
}
private JadxCLIArgs parse(String... args) { private JadxCLIArgs parse(String... args) {
return parse(new JadxCLIArgs(), args); return parse(new JadxCLIArgs(), args);
} }
@@ -44,6 +44,33 @@ public class TestInput {
decompile("multi", "samples/hello.dex", "samples/HelloWorld.smali"); 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 { private void decompile(String tmpDirName, String... inputSamples) throws URISyntaxException, IOException {
List<String> args = new ArrayList<>(); List<String> args = new ArrayList<>();
Path tempDir = FileUtils.createTempDir(tmpDirName); Path tempDir = FileUtils.createTempDir(tmpDirName);
+6 -7
View File
@@ -5,12 +5,11 @@ plugins {
dependencies { dependencies {
api(project(':jadx-plugins:jadx-plugins-api')) api(project(':jadx-plugins:jadx-plugins-api'))
implementation 'com.google.code.gson:gson:2.9.0' implementation 'com.google.code.gson:gson:2.9.1'
implementation 'com.android.tools.build:aapt2-proto:4.2.1-7147631'
constraints { // TODO: move resources decoding to separate plugin module
// Force protobuf version to prevent Java-7 issue implementation 'com.android.tools.build:aapt2-proto:7.3.1-8691043'
implementation 'com.google.protobuf:protobuf-java:3.11.4' implementation 'com.google.protobuf:protobuf-java:3.21.8' // forcing latest version
}
testImplementation 'org.apache.commons:commons-lang3:3.12.0' 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-java-input'))
testRuntimeOnly(project(':jadx-plugins:jadx-raung-input')) testRuntimeOnly(project(':jadx-plugins:jadx-raung-input'))
testImplementation 'org.eclipse.jdt:ecj:3.29.0' testImplementation 'org.eclipse.jdt:ecj:3.31.0'
testImplementation 'tools.profiler:async-profiler:1.8.3' testImplementation 'tools.profiler:async-profiler:1.8.3'
} }
@@ -12,13 +12,18 @@ import java.util.Set;
import java.util.function.Function; import java.util.function.Function;
import java.util.function.Predicate; import java.util.function.Predicate;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import jadx.api.args.DeobfuscationMapFileMode; import jadx.api.args.DeobfuscationMapFileMode;
import jadx.api.args.ResourceNameSource;
import jadx.api.data.ICodeData; import jadx.api.data.ICodeData;
import jadx.api.impl.AnnotatedCodeWriter; import jadx.api.impl.AnnotatedCodeWriter;
import jadx.api.impl.InMemoryCodeCache; import jadx.api.impl.InMemoryCodeCache;
import jadx.core.utils.files.FileUtils; import jadx.core.utils.files.FileUtils;
public class JadxArgs { public class JadxArgs {
private static final Logger LOG = LoggerFactory.getLogger(JadxArgs.class);
public static final int DEFAULT_THREADS_COUNT = Math.max(1, Runtime.getRuntime().availableProcessors() / 2); public static final int DEFAULT_THREADS_COUNT = Math.max(1, Runtime.getRuntime().availableProcessors() / 2);
@@ -68,6 +73,7 @@ public class JadxArgs {
private File deobfuscationMapFile = null; private File deobfuscationMapFile = null;
private DeobfuscationMapFileMode deobfuscationMapFileMode = DeobfuscationMapFileMode.READ; private DeobfuscationMapFileMode deobfuscationMapFileMode = DeobfuscationMapFileMode.READ;
private ResourceNameSource resourceNameSource = ResourceNameSource.AUTO;
private int deobfuscationMinLength = 0; private int deobfuscationMinLength = 0;
private int deobfuscationMaxLength = Integer.MAX_VALUE; private int deobfuscationMaxLength = Integer.MAX_VALUE;
@@ -122,6 +128,19 @@ public class JadxArgs {
setOutDirRes(new File(rootDir, DEFAULT_RES_DIR)); setOutDirRes(new File(rootDir, DEFAULT_RES_DIR));
} }
public void close() {
try {
inputFiles = null;
if (codeCache != null) {
codeCache.close();
}
} catch (Exception e) {
LOG.error("Failed to close JadxArgs", e);
} finally {
codeCache = null;
}
}
public List<File> getInputFiles() { public List<File> getInputFiles() {
return inputFiles; return inputFiles;
} }
@@ -352,6 +371,14 @@ public class JadxArgs {
this.deobfuscationMapFile = deobfuscationMapFile; this.deobfuscationMapFile = deobfuscationMapFile;
} }
public ResourceNameSource getResourceNameSource() {
return resourceNameSource;
}
public void setResourceNameSource(ResourceNameSource resourceNameSource) {
this.resourceNameSource = resourceNameSource;
}
public boolean isEscapeUnicode() { public boolean isEscapeUnicode() {
return escapeUnicode; return escapeUnicode;
} }
@@ -523,6 +550,7 @@ public class JadxArgs {
String argStr = "args:" + decompilationMode + useImports + showInconsistentCode String argStr = "args:" + decompilationMode + useImports + showInconsistentCode
+ inlineAnonymousClasses + inlineMethods + inlineAnonymousClasses + inlineMethods
+ deobfuscationOn + deobfuscationMinLength + deobfuscationMaxLength + deobfuscationOn + deobfuscationMinLength + deobfuscationMaxLength
+ resourceNameSource
+ parseKotlinMetadata + useKotlinMethodsForVarNames + parseKotlinMetadata + useKotlinMethodsForVarNames
+ insertDebugLines + extractFinally + insertDebugLines + extractFinally
+ debugInfo + useSourceNameAsClassAlias + escapeUnicode + replaceConsts + debugInfo + useSourceNameAsClassAlias + escapeUnicode + replaceConsts
@@ -547,6 +575,7 @@ public class JadxArgs {
+ ", deobfuscationOn=" + deobfuscationOn + ", deobfuscationOn=" + deobfuscationOn
+ ", deobfuscationMapFile=" + deobfuscationMapFile + ", deobfuscationMapFile=" + deobfuscationMapFile
+ ", deobfuscationMapFileMode=" + deobfuscationMapFileMode + ", deobfuscationMapFileMode=" + deobfuscationMapFileMode
+ ", resourceNameSource=" + resourceNameSource
+ ", useSourceNameAsClassAlias=" + useSourceNameAsClassAlias + ", useSourceNameAsClassAlias=" + useSourceNameAsClassAlias
+ ", parseKotlinMetadata=" + parseKotlinMetadata + ", parseKotlinMetadata=" + parseKotlinMetadata
+ ", useKotlinMethodsForVarNames=" + useKotlinMethodsForVarNames + ", useKotlinMethodsForVarNames=" + useKotlinMethodsForVarNames
@@ -37,8 +37,6 @@ import jadx.api.plugins.input.data.ILoadResult;
import jadx.api.plugins.options.JadxPluginOptions; import jadx.api.plugins.options.JadxPluginOptions;
import jadx.core.Jadx; import jadx.core.Jadx;
import jadx.core.dex.attributes.AFlag; 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.ClassNode;
import jadx.core.dex.nodes.FieldNode; import jadx.core.dex.nodes.FieldNode;
import jadx.core.dex.nodes.MethodNode; import jadx.core.dex.nodes.MethodNode;
@@ -99,7 +97,7 @@ public final class JadxDecompiler implements Closeable {
private final Map<MethodNode, JavaMethod> methodsMap = new ConcurrentHashMap<>(); private final Map<MethodNode, JavaMethod> methodsMap = new ConcurrentHashMap<>();
private final Map<FieldNode, JavaField> fieldsMap = new ConcurrentHashMap<>(); private final Map<FieldNode, JavaField> fieldsMap = new ConcurrentHashMap<>();
private final IDecompileScheduler decompileScheduler = new DecompilerScheduler(this); private final IDecompileScheduler decompileScheduler = new DecompilerScheduler();
private final List<ILoadResult> customLoads = new ArrayList<>(); private final List<ILoadResult> customLoads = new ArrayList<>();
@@ -161,8 +159,13 @@ public final class JadxDecompiler implements Closeable {
classesMap.clear(); classesMap.clear();
methodsMap.clear(); methodsMap.clear();
fieldsMap.clear(); fieldsMap.clear();
}
@Override
public void close() {
reset();
closeInputs(); closeInputs();
args.close();
} }
private void closeInputs() { private void closeInputs() {
@@ -176,11 +179,6 @@ public final class JadxDecompiler implements Closeable {
loadedInputs.clear(); loadedInputs.clear();
} }
@Override
public void close() {
reset();
}
private void loadPlugins(JadxArgs args) { private void loadPlugins(JadxArgs args) {
pluginManager.providesSuggestion("java-input", args.isUseDxInput() ? "java-convert" : "java-input"); pluginManager.providesSuggestion("java-input", args.isUseDxInput() ? "java-convert" : "java-input");
pluginManager.load(); pluginManager.load();
@@ -202,6 +200,7 @@ public final class JadxDecompiler implements Closeable {
} }
} }
@SuppressWarnings("unused")
public void registerPlugin(JadxPlugin plugin) { public void registerPlugin(JadxPlugin plugin) {
pluginManager.register(plugin); pluginManager.register(plugin);
} }
@@ -357,8 +356,9 @@ public final class JadxDecompiler implements Closeable {
tasks.add(() -> { tasks.add(() -> {
for (JavaClass cls : decompileBatch) { for (JavaClass cls : decompileBatch) {
try { try {
ICodeInfo code = cls.getCodeInfo(); ClassNode clsNode = cls.getClassNode();
SaveCode.save(outDir, cls.getClassNode(), code); ICodeInfo code = clsNode.getCode();
SaveCode.save(outDir, clsNode, code);
} catch (Exception e) { } catch (Exception e) {
LOG.error("Error saving class: {}", cls, e); LOG.error("Error saving class: {}", cls, e);
} }
@@ -467,23 +467,10 @@ public final class JadxDecompiler implements Closeable {
return protoXmlParser; return protoXmlParser;
} }
private void loadJavaClass(JavaClass javaClass) {
javaClass.getMethods().forEach(mth -> methodsMap.put(mth.getMethodNode(), mth));
javaClass.getFields().forEach(fld -> fieldsMap.put(fld.getFieldNode(), fld));
for (JavaClass innerCls : javaClass.getInnerClasses()) {
classesMap.put(innerCls.getClassNode(), innerCls);
loadJavaClass(innerCls);
}
for (JavaClass inlinedCls : javaClass.getInlinedClasses()) {
classesMap.put(inlinedCls.getClassNode(), inlinedCls);
loadJavaClass(inlinedCls);
}
}
/** /**
* Get JavaClass by ClassNode without loading and decompilation * Get JavaClass by ClassNode without loading and decompilation
*/ */
@ApiStatus.Internal
JavaClass convertClassNode(ClassNode cls) { JavaClass convertClassNode(ClassNode cls) {
return classesMap.compute(cls, (node, prevJavaCls) -> { return classesMap.compute(cls, (node, prevJavaCls) -> {
if (prevJavaCls != null && prevJavaCls.getClassNode() == cls) { if (prevJavaCls != null && prevJavaCls.getClassNode() == cls) {
@@ -497,100 +484,20 @@ public final class JadxDecompiler implements Closeable {
}); });
} }
@Nullable("For not generated classes")
@ApiStatus.Internal @ApiStatus.Internal
public JavaClass getJavaClassByNode(ClassNode cls) { JavaField convertFieldNode(FieldNode field) {
JavaClass javaClass = classesMap.get(cls); return fieldsMap.computeIfAbsent(field, fldNode -> {
if (javaClass != null && javaClass.getClassNode() == cls) { JavaClass parentCls = convertClassNode(fldNode.getParentClass());
return javaClass; return new JavaField(parentCls, fldNode);
} });
// load parent class if inner
ClassNode parentClass = cls.getTopParentClass();
if (parentClass.contains(AFlag.DONT_GENERATE)) {
return null;
}
JavaClass parentJavaClass = classesMap.get(parentClass);
if (parentJavaClass == null) {
getClasses();
parentJavaClass = classesMap.get(parentClass);
}
if (parentJavaClass != null) {
loadJavaClass(parentJavaClass);
javaClass = classesMap.get(cls);
if (javaClass != null) {
return javaClass;
}
}
// class or parent classes can be excluded from generation
if (cls.hasNotGeneratedParent()) {
return null;
}
throw new JadxRuntimeException("JavaClass not found by ClassNode: " + cls);
} }
@ApiStatus.Internal @ApiStatus.Internal
@Nullable JavaMethod convertMethodNode(MethodNode method) {
public JavaMethod getJavaMethodByNode(MethodNode mth) { return methodsMap.computeIfAbsent(method, mthNode -> {
JavaMethod javaMethod = methodsMap.get(mth); ClassNode parentCls = mthNode.getParentClass();
if (javaMethod != null && javaMethod.getMethodNode() == mth) { return new JavaMethod(convertClassNode(parentCls), mthNode);
return javaMethod; });
}
if (mth.contains(AFlag.DONT_GENERATE)) {
return null;
}
// parent class not loaded yet
ClassNode parentClass = mth.getParentClass();
ClassNode codeCls = getCodeParentClass(parentClass);
JavaClass javaClass = getJavaClassByNode(codeCls);
if (javaClass == null) {
return null;
}
loadJavaClass(javaClass);
javaMethod = methodsMap.get(mth);
if (javaMethod != null) {
return javaMethod;
}
if (parentClass.hasNotGeneratedParent()) {
return null;
}
throw new JadxRuntimeException("JavaMethod not found by MethodNode: " + mth);
}
private 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);
}
@ApiStatus.Internal
@Nullable
public JavaField getJavaFieldByNode(FieldNode fld) {
JavaField javaField = fieldsMap.get(fld);
if (javaField != null && javaField.getFieldNode() == fld) {
return javaField;
}
// parent class not loaded yet
JavaClass javaClass = getJavaClassByNode(fld.getParentClass().getTopParentClass());
if (javaClass == null) {
return null;
}
loadJavaClass(javaClass);
javaField = fieldsMap.get(fld);
if (javaField != null) {
return javaField;
}
if (fld.getParentClass().hasNotGeneratedParent()) {
return null;
}
throw new JadxRuntimeException("JavaField not found by FieldNode: " + fld);
} }
@Nullable @Nullable
@@ -598,7 +505,7 @@ public final class JadxDecompiler implements Closeable {
return getRoot().getClasses().stream() return getRoot().getClasses().stream()
.filter(cls -> cls.getClassInfo().getFullName().equals(fullName)) .filter(cls -> cls.getClassInfo().getFullName().equals(fullName))
.findFirst() .findFirst()
.map(this::getJavaClassByNode) .map(this::convertClassNode)
.orElse(null); .orElse(null);
} }
@@ -619,9 +526,9 @@ public final class JadxDecompiler implements Closeable {
.orElse(null); .orElse(null);
if (node != null) { if (node != null) {
if (node.contains(AFlag.DONT_GENERATE)) { if (node.contains(AFlag.DONT_GENERATE)) {
return getJavaClassByNode(node.getTopParentClass()); return convertClassNode(node.getTopParentClass());
} else { } else {
return getJavaClassByNode(node); return convertClassNode(node);
} }
} }
return null; return null;
@@ -632,7 +539,7 @@ public final class JadxDecompiler implements Closeable {
return getRoot().getClasses().stream() return getRoot().getClasses().stream()
.filter(cls -> cls.getClassInfo().getAliasFullName().equals(fullName)) .filter(cls -> cls.getClassInfo().getAliasFullName().equals(fullName))
.findFirst() .findFirst()
.map(this::getJavaClassByNode) .map(this::convertClassNode)
.orElse(null); .orElse(null);
} }
@@ -650,9 +557,9 @@ public final class JadxDecompiler implements Closeable {
case CLASS: case CLASS:
return convertClassNode((ClassNode) ann); return convertClassNode((ClassNode) ann);
case METHOD: case METHOD:
return getJavaMethodByNode((MethodNode) ann); return convertMethodNode((MethodNode) ann);
case FIELD: case FIELD:
return getJavaFieldByNode((FieldNode) ann); return convertFieldNode((FieldNode) ann);
case DECLARATION: case DECLARATION:
return getJavaNodeByCodeAnnotation(codeInfo, ((NodeDeclareRef) ann).getNode()); return getJavaNodeByCodeAnnotation(codeInfo, ((NodeDeclareRef) ann).getNode());
case VAR: case VAR:
@@ -670,7 +577,7 @@ public final class JadxDecompiler implements Closeable {
@Nullable @Nullable
private JavaVariable resolveVarNode(VarNode varNode) { private JavaVariable resolveVarNode(VarNode varNode) {
MethodNode mthNode = varNode.getMth(); MethodNode mthNode = varNode.getMth();
JavaMethod mth = getJavaMethodByNode(mthNode); JavaMethod mth = convertMethodNode(mthNode);
if (mth == null) { if (mth == null) {
return null; return null;
} }
@@ -683,10 +590,13 @@ public final class JadxDecompiler implements Closeable {
throw new JadxRuntimeException("Missing code info for resolve VarRef: " + varRef); throw new JadxRuntimeException("Missing code info for resolve VarRef: " + varRef);
} }
ICodeAnnotation varNodeAnn = codeInfo.getCodeMetadata().getAt(varRef.getRefPos()); ICodeAnnotation varNodeAnn = codeInfo.getCodeMetadata().getAt(varRef.getRefPos());
if (varNodeAnn == null) { if (varNodeAnn != null && varNodeAnn.getAnnType() == ICodeAnnotation.AnnType.DECLARATION) {
return null; ICodeNodeRef nodeRef = ((NodeDeclareRef) varNodeAnn).getNode();
if (nodeRef.getAnnType() == ICodeAnnotation.AnnType.VAR) {
return resolveVarNode((VarNode) nodeRef);
}
} }
return (JavaVariable) getJavaNodeByCodeAnnotation(codeInfo, varNodeAnn); return null;
} }
List<JavaNode> convertNodes(Collection<? extends ICodeNodeRef> nodesList) { List<JavaNode> convertNodes(Collection<? extends ICodeNodeRef> nodesList) {
+80 -41
View File
@@ -10,18 +10,23 @@ import java.util.Map;
import org.jetbrains.annotations.ApiStatus; import org.jetbrains.annotations.ApiStatus;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable; import org.jetbrains.annotations.Nullable;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import jadx.api.metadata.ICodeAnnotation; import jadx.api.metadata.ICodeAnnotation;
import jadx.api.metadata.ICodeNodeRef; import jadx.api.metadata.ICodeNodeRef;
import jadx.core.dex.attributes.AFlag; import jadx.core.dex.attributes.AFlag;
import jadx.core.dex.attributes.AType; import jadx.core.dex.attributes.AType;
import jadx.core.dex.attributes.nodes.AnonymousClassAttr; import jadx.core.dex.attributes.nodes.AnonymousClassAttr;
import jadx.core.dex.attributes.nodes.InlinedAttr;
import jadx.core.dex.info.AccessInfo; import jadx.core.dex.info.AccessInfo;
import jadx.core.dex.nodes.ClassNode; import jadx.core.dex.nodes.ClassNode;
import jadx.core.dex.nodes.FieldNode; import jadx.core.dex.nodes.FieldNode;
import jadx.core.dex.nodes.MethodNode; import jadx.core.dex.nodes.MethodNode;
import jadx.core.utils.ListUtils;
public final class JavaClass implements JavaNode { public final class JavaClass implements JavaNode {
private static final Logger LOG = LoggerFactory.getLogger(JavaClass.class);
private final JadxDecompiler decompiler; private final JadxDecompiler decompiler;
private final ClassNode cls; private final ClassNode cls;
@@ -53,7 +58,10 @@ public final class JavaClass implements JavaNode {
} }
public @NotNull ICodeInfo getCodeInfo() { public @NotNull ICodeInfo getCodeInfo() {
load(); ICodeInfo code = load();
if (code != null) {
return code;
}
return cls.decompile(); return cls.decompile();
} }
@@ -83,6 +91,14 @@ public final class JavaClass implements JavaNode {
return cls.getDisassembledCode(); return cls.getDisassembledCode();
} }
@Override
public boolean isOwnCodeAnnotation(ICodeAnnotation ann) {
if (ann.getAnnType() == ICodeAnnotation.AnnType.CLASS) {
return ann.equals(cls);
}
return false;
}
/** /**
* Internal API. Not Stable! * Internal API. Not Stable!
*/ */
@@ -94,20 +110,24 @@ public final class JavaClass implements JavaNode {
/** /**
* Decompile class and loads internal lists of fields, methods, etc. * Decompile class and loads internal lists of fields, methods, etc.
* Do nothing if already loaded. * Do nothing if already loaded.
* Return not null on first call only (for actual loading) *
* @return code info if decompilation was executed, null otherwise
*/ */
@Nullable private synchronized @Nullable ICodeInfo load() {
private synchronized void load() {
if (listsLoaded) { if (listsLoaded) {
return; return null;
} }
listsLoaded = true; listsLoaded = true;
JadxDecompiler rootDecompiler = getRootDecompiler();
ICodeCache codeCache = rootDecompiler.getArgs().getCodeCache(); ICodeInfo code;
if (!codeCache.contains(cls.getRawName())) { if (cls.getState().isProcessComplete()) {
cls.decompile(); // already decompiled -> class internals loaded
code = null;
} else {
code = cls.decompile();
} }
JadxDecompiler rootDecompiler = getRootDecompiler();
int inClsCount = cls.getInnerClasses().size(); int inClsCount = cls.getInnerClasses().size();
if (inClsCount != 0) { if (inClsCount != 0) {
List<JavaClass> list = new ArrayList<>(inClsCount); List<JavaClass> list = new ArrayList<>(inClsCount);
@@ -135,10 +155,9 @@ public final class JavaClass implements JavaNode {
if (fieldsCount != 0) { if (fieldsCount != 0) {
List<JavaField> flds = new ArrayList<>(fieldsCount); List<JavaField> flds = new ArrayList<>(fieldsCount);
for (FieldNode f : cls.getFields()) { for (FieldNode f : cls.getFields()) {
// if (!f.contains(AFlag.DONT_GENERATE)) { if (!f.contains(AFlag.DONT_GENERATE)) {
JavaField javaField = new JavaField(this, f); flds.add(rootDecompiler.convertFieldNode(f));
flds.add(javaField); }
// }
} }
this.fields = Collections.unmodifiableList(flds); this.fields = Collections.unmodifiableList(flds);
} }
@@ -148,16 +167,16 @@ public final class JavaClass implements JavaNode {
List<JavaMethod> mths = new ArrayList<>(methodsCount); List<JavaMethod> mths = new ArrayList<>(methodsCount);
for (MethodNode m : cls.getMethods()) { for (MethodNode m : cls.getMethods()) {
if (!m.contains(AFlag.DONT_GENERATE)) { if (!m.contains(AFlag.DONT_GENERATE)) {
JavaMethod javaMethod = new JavaMethod(this, m); mths.add(rootDecompiler.convertMethodNode(m));
mths.add(javaMethod);
} }
} }
mths.sort(Comparator.comparing(JavaMethod::getName)); mths.sort(Comparator.comparing(JavaMethod::getName));
this.methods = Collections.unmodifiableList(mths); this.methods = Collections.unmodifiableList(mths);
} }
return code;
} }
protected JadxDecompiler getRootDecompiler() { JadxDecompiler getRootDecompiler() {
if (parent != null) { if (parent != null) {
return parent.getRootDecompiler(); return parent.getRootDecompiler();
} }
@@ -188,23 +207,16 @@ public final class JavaClass implements JavaNode {
} }
public List<Integer> getUsePlacesFor(ICodeInfo codeInfo, JavaNode javaNode) { public List<Integer> getUsePlacesFor(ICodeInfo codeInfo, JavaNode javaNode) {
Map<Integer, ICodeAnnotation> map = codeInfo.getCodeMetadata().getAsMap(); if (!codeInfo.hasMetadata()) {
if (map.isEmpty() || decompiler == null) {
return Collections.emptyList(); return Collections.emptyList();
} }
JadxDecompiler rootDec = getRootDecompiler();
List<Integer> result = new ArrayList<>(); List<Integer> result = new ArrayList<>();
for (Map.Entry<Integer, ICodeAnnotation> entry : map.entrySet()) { codeInfo.getCodeMetadata().searchDown(0, (pos, ann) -> {
ICodeAnnotation ann = entry.getValue(); if (javaNode.isOwnCodeAnnotation(ann)) {
if (ann.getAnnType() == ICodeAnnotation.AnnType.DECLARATION) { result.add(pos);
// ignore declarations
continue;
} }
JavaNode annNode = rootDec.getJavaNodeByCodeAnnotation(codeInfo, ann); return null;
if (javaNode.equals(annNode)) { });
result.add(entry.getKey());
}
}
return result; return result;
} }
@@ -240,19 +252,37 @@ public final class JavaClass implements JavaNode {
return parent; return parent;
} }
@Override public JavaClass getOriginalTopParentClass() {
public JavaClass getTopParentClass() { return parent == null ? this : parent.getOriginalTopParentClass();
if (cls.contains(AType.ANONYMOUS_CLASS)) {
// moved to usage class
return getParentForAnonymousClass();
}
return parent == null ? this : parent.getTopParentClass();
} }
private JavaClass getParentForAnonymousClass() { /**
AnonymousClassAttr attr = cls.get(AType.ANONYMOUS_CLASS); * Return top parent class which contains code of this class.
ClassNode topParentClass = attr.getOuterCls().getTopParentClass(); * Code parent can be different from original parent after move or inline
return getRootDecompiler().convertClassNode(topParentClass); *
* @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() { public AccessInfo getAccessInfo() {
@@ -285,7 +315,16 @@ public final class JavaClass implements JavaNode {
if (methodNode == null) { if (methodNode == null) {
return null; return null;
} }
return new JavaMethod(this, methodNode); return getRootDecompiler().convertMethodNode(methodNode);
}
public List<JavaClass> getDependencies() {
JadxDecompiler d = getRootDecompiler();
return ListUtils.map(cls.getDependencies(), d::convertClassNode);
}
public int getTotalDepsCount() {
return cls.getTotalDepsCount();
} }
@Override @Override
@@ -4,6 +4,7 @@ import java.util.List;
import org.jetbrains.annotations.ApiStatus; import org.jetbrains.annotations.ApiStatus;
import jadx.api.metadata.ICodeAnnotation;
import jadx.core.dex.info.AccessInfo; import jadx.core.dex.info.AccessInfo;
import jadx.core.dex.instructions.args.ArgType; import jadx.core.dex.instructions.args.ArgType;
import jadx.core.dex.nodes.FieldNode; import jadx.core.dex.nodes.FieldNode;
@@ -65,6 +66,14 @@ public final class JavaField implements JavaNode {
this.field.getFieldInfo().removeAlias(); this.field.getFieldInfo().removeAlias();
} }
@Override
public boolean isOwnCodeAnnotation(ICodeAnnotation ann) {
if (ann.getAnnType() == ICodeAnnotation.AnnType.FIELD) {
return ann.equals(field);
}
return false;
}
/** /**
* Internal API. Not Stable! * Internal API. Not Stable!
*/ */
@@ -9,6 +9,7 @@ import org.jetbrains.annotations.ApiStatus;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import jadx.api.metadata.ICodeAnnotation;
import jadx.core.dex.attributes.AType; import jadx.core.dex.attributes.AType;
import jadx.core.dex.attributes.nodes.MethodOverrideAttr; import jadx.core.dex.attributes.nodes.MethodOverrideAttr;
import jadx.core.dex.info.AccessInfo; import jadx.core.dex.info.AccessInfo;
@@ -18,6 +19,7 @@ import jadx.core.utils.Utils;
public final class JavaMethod implements JavaNode { public final class JavaMethod implements JavaNode {
private static final Logger LOG = LoggerFactory.getLogger(JavaMethod.class); private static final Logger LOG = LoggerFactory.getLogger(JavaMethod.class);
private final MethodNode mth; private final MethodNode mth;
private final JavaClass parent; private final JavaClass parent;
@@ -78,7 +80,7 @@ public final class JavaMethod implements JavaNode {
JadxDecompiler decompiler = getDeclaringClass().getRootDecompiler(); JadxDecompiler decompiler = getDeclaringClass().getRootDecompiler();
return ovrdAttr.getRelatedMthNodes().stream() return ovrdAttr.getRelatedMthNodes().stream()
.map(m -> { .map(m -> {
JavaMethod javaMth = decompiler.getJavaMethodByNode(m); JavaMethod javaMth = decompiler.convertMethodNode(m);
if (javaMth == null) { if (javaMth == null) {
LOG.warn("Failed convert to java method: {}", m); LOG.warn("Failed convert to java method: {}", m);
} }
@@ -106,6 +108,14 @@ public final class JavaMethod implements JavaNode {
this.mth.getMethodInfo().removeAlias(); this.mth.getMethodInfo().removeAlias();
} }
@Override
public boolean isOwnCodeAnnotation(ICodeAnnotation ann) {
if (ann.getAnnType() == ICodeAnnotation.AnnType.METHOD) {
return ann.equals(mth);
}
return false;
}
/** /**
* Internal API. Not Stable! * Internal API. Not Stable!
*/ */
@@ -2,6 +2,8 @@ package jadx.api;
import java.util.List; import java.util.List;
import jadx.api.metadata.ICodeAnnotation;
public interface JavaNode { public interface JavaNode {
String getName(); String getName();
@@ -18,4 +20,6 @@ public interface JavaNode {
default void removeAlias() { default void removeAlias() {
} }
boolean isOwnCodeAnnotation(ICodeAnnotation ann);
} }
@@ -5,6 +5,8 @@ import java.util.List;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
import jadx.api.metadata.ICodeAnnotation;
public final class JavaPackage implements JavaNode, Comparable<JavaPackage> { public final class JavaPackage implements JavaNode, Comparable<JavaPackage> {
private final String name; private final String name;
private final List<JavaClass> classes; private final List<JavaClass> classes;
@@ -49,6 +51,11 @@ public final class JavaPackage implements JavaNode, Comparable<JavaPackage> {
return Collections.emptyList(); return Collections.emptyList();
} }
@Override
public boolean isOwnCodeAnnotation(ICodeAnnotation ann) {
return false;
}
@Override @Override
public int compareTo(@NotNull JavaPackage o) { public int compareTo(@NotNull JavaPackage o) {
return name.compareTo(o.name); return name.compareTo(o.name);
@@ -4,8 +4,12 @@ import java.util.Collections;
import java.util.List; import java.util.List;
import org.jetbrains.annotations.ApiStatus; import org.jetbrains.annotations.ApiStatus;
import org.jetbrains.annotations.Nullable;
import jadx.api.metadata.ICodeAnnotation;
import jadx.api.metadata.annotations.VarNode; import jadx.api.metadata.annotations.VarNode;
import jadx.api.metadata.annotations.VarRef;
import jadx.core.dex.instructions.args.ArgType;
public class JavaVariable implements JavaNode { public class JavaVariable implements JavaNode {
private final JavaMethod mth; private final JavaMethod mth;
@@ -29,7 +33,7 @@ public class JavaVariable implements JavaNode {
} }
@Override @Override
public String getName() { public @Nullable String getName() {
return varNode.getName(); return varNode.getName();
} }
@@ -43,6 +47,10 @@ public class JavaVariable implements JavaNode {
return varNode.getType() + " " + varNode.getName() + " (r" + varNode.getReg() + "v" + varNode.getSsa() + ")"; return varNode.getType() + " " + varNode.getName() + " (r" + varNode.getReg() + "v" + varNode.getSsa() + ")";
} }
public ArgType getType() {
return ArgType.tryToResolveClassAlias(mth.getMethodNode().root(), varNode.getType());
}
@Override @Override
public JavaClass getDeclaringClass() { public JavaClass getDeclaringClass() {
return mth.getDeclaringClass(); return mth.getDeclaringClass();
@@ -63,6 +71,15 @@ public class JavaVariable implements JavaNode {
return Collections.singletonList(mth); return Collections.singletonList(mth);
} }
@Override
public boolean isOwnCodeAnnotation(ICodeAnnotation ann) {
if (ann.getAnnType() == ICodeAnnotation.AnnType.VAR_REF) {
VarRef varRef = (VarRef) ann;
return varRef.getRefPos() == getDefPos();
}
return false;
}
@Override @Override
public int hashCode() { public int hashCode() {
return varNode.hashCode(); return varNode.hashCode();
@@ -0,0 +1,22 @@
package jadx.api.args;
/**
* Resources original name source (for deobfuscation)
*/
public enum ResourceNameSource {
/**
* Automatically select best name (default)
*/
AUTO,
/**
* Force use resources provided names
*/
RESOURCES,
/**
* Force use resources names from R class
*/
CODE,
}
@@ -9,7 +9,8 @@ public interface ICodeAnnotation {
VAR, VAR,
VAR_REF, VAR_REF,
DECLARATION, DECLARATION,
OFFSET OFFSET,
END // class or method body end
} }
AnnType getAnnType(); AnnType getAnnType();
@@ -32,6 +32,22 @@ public class NodeDeclareRef implements ICodeAnnotation {
return AnnType.DECLARATION; 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 @Override
public String toString() { public String toString() {
return "NodeDeclareRef{" + node + '}'; 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.NavigableMap;
import java.util.TreeMap; import java.util.TreeMap;
import java.util.function.BiFunction; import java.util.function.BiFunction;
import java.util.stream.Stream;
import org.jetbrains.annotations.Nullable; import org.jetbrains.annotations.Nullable;
import jadx.api.metadata.ICodeAnnotation; import jadx.api.metadata.ICodeAnnotation;
import jadx.api.metadata.ICodeAnnotation.AnnType;
import jadx.api.metadata.ICodeMetadata; import jadx.api.metadata.ICodeMetadata;
import jadx.api.metadata.ICodeNodeRef; import jadx.api.metadata.ICodeNodeRef;
import jadx.api.metadata.annotations.NodeDeclareRef; import jadx.api.metadata.annotations.NodeDeclareRef;
import jadx.core.dex.nodes.ClassNode;
import jadx.core.dex.nodes.MethodNode;
import jadx.core.utils.Utils; import jadx.core.utils.Utils;
public class CodeMetadataStorage implements ICodeMetadata { public class CodeMetadataStorage implements ICodeMetadata {
@@ -55,7 +53,7 @@ public class CodeMetadataStorage implements ICodeMetadata {
} }
@Override @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()) { for (ICodeAnnotation v : navMap.tailMap(position, true).values()) {
if (v.getAnnType() == annType) { if (v.getAnnType() == annType) {
return v; return v;
@@ -65,7 +63,7 @@ public class CodeMetadataStorage implements ICodeMetadata {
} }
@Override @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()) { for (ICodeAnnotation v : navMap.subMap(position, true, limitPos, true).values()) {
if (v.getAnnType() == annType) { if (v.getAnnType() == annType) {
return v; return v;
@@ -99,28 +97,40 @@ public class CodeMetadataStorage implements ICodeMetadata {
@Override @Override
public ICodeNodeRef getNodeAt(int position) { public ICodeNodeRef getNodeAt(int position) {
return navMap.tailMap(position, true) int nesting = 0;
.values().stream() for (ICodeAnnotation ann : navMap.tailMap(position, true).values()) {
.flatMap(CodeMetadataStorage::mapEnclosingNode) switch (ann.getAnnType()) {
.findFirst().orElse(null); 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 @Override
public ICodeNodeRef getNodeBelow(int position) { public ICodeNodeRef getNodeBelow(int position) {
return navMap.headMap(position, true).descendingMap() for (ICodeAnnotation ann : navMap.headMap(position, true).descendingMap().values()) {
.values().stream() if (ann.getAnnType() == AnnType.DECLARATION) {
.flatMap(CodeMetadataStorage::mapEnclosingNode) ICodeNodeRef node = ((NodeDeclareRef) ann).getNode();
.findFirst().orElse(null); AnnType nodeType = node.getAnnType();
} if (nodeType == AnnType.CLASS || nodeType == AnnType.METHOD) {
return node;
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);
} }
} }
return Stream.empty(); return null;
} }
@Override @Override
@@ -135,7 +145,7 @@ public class CodeMetadataStorage implements ICodeMetadata {
@Override @Override
public String toString() { public String toString() {
return "CodeMetadata{lines=" + lines return "CodeMetadata{\nlines=" + lines
+ ", annotations=\n" + Utils.listToString(navMap.entrySet(), "\n") + "\n}"; + "\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_TYPE_INFERENCE = false;
public static final boolean DEBUG_OVERLOADED_CASTS = false; public static final boolean DEBUG_OVERLOADED_CASTS = false;
public static final boolean DEBUG_EXC_HANDLERS = 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_OBJECT = "java.lang.Object";
public static final String CLASS_STRING = "java.lang.String"; public static final String CLASS_STRING = "java.lang.String";
+10 -7
View File
@@ -191,7 +191,6 @@ public class Jadx {
passes.add(new ProcessInstructionsVisitor()); passes.add(new ProcessInstructionsVisitor());
passes.add(new BlockSplitter()); passes.add(new BlockSplitter());
passes.add(new MethodVisitor(mth -> mth.add(AFlag.DISABLE_BLOCKS_LOCK)));
if (args.isRawCFGOutput()) { if (args.isRawCFGOutput()) {
passes.add(DotGraphVisitor.dumpRaw()); passes.add(DotGraphVisitor.dumpRaw());
} }
@@ -215,9 +214,6 @@ public class Jadx {
passes.add(new CodeShrinkVisitor()); passes.add(new CodeShrinkVisitor());
passes.add(new SimplifyVisitor()); passes.add(new SimplifyVisitor());
passes.add(new MethodVisitor(mth -> mth.remove(AFlag.DONT_GENERATE))); passes.add(new MethodVisitor(mth -> mth.remove(AFlag.DONT_GENERATE)));
if (args.isRawCFGOutput()) {
passes.add(DotGraphVisitor.dumpRaw());
}
if (args.isCfgOutput()) { if (args.isCfgOutput()) {
passes.add(DotGraphVisitor.dump()); passes.add(DotGraphVisitor.dump());
} }
@@ -238,9 +234,17 @@ public class Jadx {
private static String version; private static String version;
public static String getVersion() { public static String getVersion() {
if (version != null) { if (version == null) {
return version; version = searchJadxVersion();
} }
return version;
}
public static boolean isDevVersion() {
return getVersion().equals(VERSION_DEV);
}
private static String searchJadxVersion() {
try { try {
ClassLoader classLoader = Jadx.class.getClassLoader(); ClassLoader classLoader = Jadx.class.getClassLoader();
if (classLoader != null) { if (classLoader != null) {
@@ -250,7 +254,6 @@ public class Jadx {
Manifest manifest = new Manifest(is); Manifest manifest = new Manifest(is);
String ver = manifest.getMainAttributes().getValue("jadx-version"); String ver = manifest.getMainAttributes().getValue("jadx-version");
if (ver != null) { if (ver != null) {
version = ver;
return ver; return ver;
} }
} }
@@ -203,6 +203,9 @@ public class ClspGraph {
if (isNew) { if (isNew) {
addSuperTypes(parentCls, result); 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.ICodeInfo;
import jadx.api.ICodeWriter; import jadx.api.ICodeWriter;
import jadx.api.JadxArgs; import jadx.api.JadxArgs;
import jadx.api.metadata.annotations.NodeEnd;
import jadx.api.plugins.input.data.AccessFlags; import jadx.api.plugins.input.data.AccessFlags;
import jadx.api.plugins.input.data.annotations.EncodedType; import jadx.api.plugins.input.data.annotations.EncodedType;
import jadx.api.plugins.input.data.annotations.EncodedValue; import jadx.api.plugins.input.data.annotations.EncodedValue;
@@ -170,7 +171,7 @@ public class ClassGen {
ArgType sup = cls.getSuperClass(); ArgType sup = cls.getSuperClass();
if (sup != null if (sup != null
&& !sup.equals(ArgType.OBJECT) && !sup.equals(ArgType.OBJECT)
&& !cls.isEnum()) { && !cls.contains(AFlag.REMOVE_SUPER_CLASS)) {
clsCode.add("extends "); clsCode.add("extends ");
useClass(clsCode, sup); useClass(clsCode, sup);
clsCode.add(' '); clsCode.add(' ');
@@ -256,6 +257,7 @@ public class ClassGen {
addInnerClsAndMethods(clsCode); addInnerClsAndMethods(clsCode);
clsCode.decIndent(); clsCode.decIndent();
clsCode.startLine('}'); clsCode.startLine('}');
clsCode.attachAnnotation(NodeEnd.VALUE);
} }
private void addInnerClsAndMethods(ICodeWriter clsCode) { private void addInnerClsAndMethods(ICodeWriter clsCode) {
@@ -320,19 +322,25 @@ public class ClassGen {
if (inlineAttr == null || inlineAttr.notNeeded()) { if (inlineAttr == null || inlineAttr.notNeeded()) {
return false; return false;
} }
if (mth.getUseIn().isEmpty()) { try {
mth.add(AFlag.DONT_GENERATE); if (mth.getUseIn().isEmpty()) {
return true; mth.add(AFlag.DONT_GENERATE);
return true;
}
List<MethodNode> useInCompleted = mth.getUseIn().stream()
.filter(m -> m.getTopParentClass().getState().isProcessComplete())
.collect(Collectors.toList());
if (useInCompleted.isEmpty()) {
mth.add(AFlag.DONT_GENERATE);
return true;
}
mth.addDebugComment("Method not inlined, still used in: " + useInCompleted);
return false;
} catch (Exception e) {
// check failed => keep method
mth.addWarnComment("Failed to check method usage", e);
return false;
} }
List<MethodNode> useInCompleted = mth.getUseIn().stream()
.filter(m -> m.getTopParentClass().getState().isProcessComplete())
.collect(Collectors.toList());
if (useInCompleted.isEmpty()) {
mth.add(AFlag.DONT_GENERATE);
return true;
}
mth.addDebugComment("Method not inlined, still used in: " + useInCompleted);
return false;
} }
private boolean isMethodsPresents() { private boolean isMethodsPresents() {
@@ -369,6 +377,7 @@ public class ClassGen {
mthGen.addInstructions(code); mthGen.addInstructions(code);
code.decIndent(); code.decIndent();
code.startLine('}'); code.startLine('}');
code.attachAnnotation(NodeEnd.VALUE);
} }
} }
@@ -614,21 +623,23 @@ public class ClassGen {
if (useCls.equals(extClsInfo)) { if (useCls.equals(extClsInfo)) {
return shortName; return shortName;
} }
if (extClsInfo.getPackage().equals("java.lang") && extClsInfo.getParentClass() == null) {
return shortName;
}
if (isClassInnerFor(useCls, extClsInfo)) { if (isClassInnerFor(useCls, extClsInfo)) {
return shortName; return shortName;
} }
if (extClsInfo.isInner()) { if (extClsInfo.isInner()) {
return expandInnerClassName(useCls, extClsInfo); return expandInnerClassName(useCls, extClsInfo);
} }
if (searchCollision(cls.root(), useCls, extClsInfo)) { if (checkInnerCollision(cls.root(), useCls, extClsInfo)
|| checkInPackageCollision(cls.root(), useCls, extClsInfo)) {
return fullName; return fullName;
} }
if (isBothClassesInOneTopClass(useCls, extClsInfo)) { if (isBothClassesInOneTopClass(useCls, extClsInfo)) {
return shortName; 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 // don't add import if this class from same package
if (extClsInfo.getPackage().equals(useCls.getPackage()) && !extClsInfo.isInner()) { if (extClsInfo.getPackage().equals(useCls.getPackage()) && !extClsInfo.isInner()) {
return shortName; return shortName;
@@ -709,7 +720,7 @@ public class ClassGen {
return false; 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) { if (useCls == null) {
return false; return false;
} }
@@ -726,7 +737,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) { private void insertRenameInfo(ICodeWriter code, ClassNode cls) {
@@ -1,7 +1,7 @@
package jadx.core.codegen; package jadx.core.codegen;
import java.util.ArrayDeque;
import java.util.Iterator; import java.util.Iterator;
import java.util.LinkedList;
import java.util.Queue; import java.util.Queue;
import jadx.api.ICodeWriter; import jadx.api.ICodeWriter;
@@ -23,7 +23,7 @@ import jadx.core.utils.exceptions.JadxRuntimeException;
public class ConditionGen extends InsnGen { public class ConditionGen extends InsnGen {
private static class CondStack { private static class CondStack {
private final Queue<IfCondition> stack = new LinkedList<>(); private final Queue<IfCondition> stack = new ArrayDeque<>();
public Queue<IfCondition> getStack() { public Queue<IfCondition> getStack() {
return stack; return stack;
@@ -168,10 +168,11 @@ public class InsnGen {
* Variable definition without type, only var name * Variable definition without type, only var name
*/ */
private void defVar(ICodeWriter code, CodeVar codeVar) { private void defVar(ICodeWriter code, CodeVar codeVar) {
String varName = mgen.getNameGen().assignArg(codeVar);
if (code.isMetadataSupported()) { if (code.isMetadataSupported()) {
code.attachDefinition(VarNode.get(mth, codeVar)); code.attachDefinition(VarNode.get(mth, codeVar));
} }
code.add(mgen.getNameGen().assignArg(codeVar)); code.add(varName);
} }
private String lit(LiteralArg arg) { private String lit(LiteralArg arg) {
@@ -760,6 +761,7 @@ public class InsnGen {
ctor.add(AFlag.DONT_GENERATE); ctor.add(AFlag.DONT_GENERATE);
} }
} }
code.attachDefinition(cls);
code.add("new "); code.add("new ");
useClass(code, parent); useClass(code, parent);
MethodNode callMth = mth.root().resolveMethod(insn.getCallMth()); MethodNode callMth = mth.root().resolveMethod(insn.getCallMth());
@@ -806,14 +808,9 @@ public class InsnGen {
break; break;
case SUPER: case SUPER:
ClassInfo superCallCls = getClassForSuperCall(code, callMth); callSuper(code, callMth);
if (superCallCls != null) { k++; // use 'super' instead 'this' in 0 arg
useClass(code, superCallCls); code.add('.');
code.add('.');
}
// use 'super' instead 'this' in 0 arg
code.add("super").add('.');
k++;
break; break;
case STATIC: case STATIC:
@@ -827,9 +824,15 @@ public class InsnGen {
} }
if (callMthNode != null) { if (callMthNode != null) {
code.attachAnnotation(callMthNode); code.attachAnnotation(callMthNode);
code.add(callMthNode.getAlias()); }
if (insn.contains(AFlag.FORCE_RAW_NAME)) {
code.add(callMth.getName());
} else { } else {
code.add(callMth.getAlias()); if (callMthNode != null) {
code.add(callMthNode.getAlias());
} else {
code.add(callMth.getAlias());
}
} }
generateMethodArguments(code, insn, k, callMthNode); generateMethodArguments(code, insn, k, callMthNode);
} }
@@ -964,34 +967,43 @@ public class InsnGen {
code.startLine('}'); code.startLine('}');
} }
@Nullable private void callSuper(ICodeWriter code, MethodInfo callMth) {
private ClassInfo getClassForSuperCall(ICodeWriter code, MethodInfo callMth) { ClassInfo superCallCls = getClassForSuperCall(callMth);
ClassNode useCls = mth.getParentClass(); if (superCallCls == null) {
ClassInfo insnCls = useCls.getClassInfo(); // unknown class, add comment to keep that info
ClassInfo declClass = callMth.getDeclClass(); code.add("super/*").add(callMth.getDeclClass().getFullName()).add("*/");
if (insnCls.equals(declClass)) { return;
return null;
} }
ClassNode topClass = useCls.getTopParentClass(); ClassInfo curClass = mth.getParentClass().getClassInfo();
if (topClass.getClassInfo().equals(declClass)) { if (superCallCls.equals(curClass)) {
return declClass; code.add("super");
return;
} }
// search call class // use custom class
ClassNode nextParent = useCls; useClass(code, superCallCls);
do { code.add(".super");
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);
// 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, 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.JadxError;
import jadx.core.dex.attributes.nodes.JumpInfo; import jadx.core.dex.attributes.nodes.JumpInfo;
import jadx.core.dex.attributes.nodes.MethodOverrideAttr; import jadx.core.dex.attributes.nodes.MethodOverrideAttr;
import jadx.core.dex.attributes.nodes.MethodReplaceAttr;
import jadx.core.dex.info.AccessInfo; import jadx.core.dex.info.AccessInfo;
import jadx.core.dex.instructions.ConstStringNode; import jadx.core.dex.instructions.ConstStringNode;
import jadx.core.dex.instructions.IfNode; import jadx.core.dex.instructions.IfNode;
@@ -144,8 +145,9 @@ public class MethodGen {
} else { } else {
classGen.useType(code, mth.getReturnType()); classGen.useType(code, mth.getReturnType());
code.add(' '); code.add(' ');
code.attachDefinition(mth); MethodNode defMth = getMethodForDefinition();
code.add(mth.getAlias()); code.attachDefinition(defMth);
code.add(defMth.getAlias());
} }
code.add('('); code.add('(');
@@ -178,6 +180,14 @@ public class MethodGen {
return true; 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) { private void addOverrideAnnotation(ICodeWriter code, MethodNode mth) {
MethodOverrideAttr overrideAttr = mth.get(AType.METHOD_OVERRIDE); MethodOverrideAttr overrideAttr = mth.get(AType.METHOD_OVERRIDE);
if (overrideAttr == null) { if (overrideAttr == null) {
@@ -53,7 +53,9 @@ public class SimpleModeHelper {
startLabel.set(block.getId()); startLabel.set(block.getId());
} else if (predsCount == 1 && prev != null) { } else if (predsCount == 1 && prev != null) {
if (!prev.equals(preds.get(0))) { if (!prev.equals(preds.get(0))) {
startLabel.set(block.getId()); if (!block.contains(AFlag.EXC_BOTTOM_SPLITTER)) {
startLabel.set(block.getId());
}
if (prev.getSuccessors().size() == 1 && !mth.isPreExitBlocks(prev)) { if (prev.getSuccessors().size() == 1 && !mth.isPreExitBlocks(prev)) {
endGoto.set(prev.getId()); endGoto.set(prev.getId());
} }
@@ -428,7 +428,9 @@ public class Deobfuscator {
return "Enum"; return "Enum";
} }
String result = ""; String result = "";
if (cls.getAccessFlags().isAbstract()) { if (cls.getAccessFlags().isInterface()) {
result += "Interface";
} else if (cls.getAccessFlags().isAbstract()) {
result += "Abstract"; result += "Abstract";
} }
@@ -21,10 +21,13 @@ public enum AFlag {
DONT_GENERATE, // process as usual, but don't output to generated code DONT_GENERATE, // process as usual, but don't output to generated code
COMMENT_OUT, // process as usual, but comment insn in generated code COMMENT_OUT, // process as usual, but comment insn in generated code
REMOVE, // can be completely removed REMOVE, // can be completely removed
REMOVE_SUPER_CLASS, // don't add super class
HIDDEN, // instruction used inside other instruction but not listed in args HIDDEN, // instruction used inside other instruction but not listed in args
DONT_RENAME, // do not rename during deobfuscation DONT_RENAME, // do not rename during deobfuscation
FORCE_RAW_NAME, // force use of raw name instead alias
ADDED_TO_REGION, ADDED_TO_REGION,
EXC_TOP_SPLITTER, EXC_TOP_SPLITTER,
@@ -91,6 +91,19 @@ public class LoopInfo {
this.parentLoop = parentLoop; this.parentLoop = parentLoop;
} }
public boolean hasParent(LoopInfo searchLoop) {
LoopInfo parent = parentLoop;
while (true) {
if (parent == null) {
return false;
}
if (parent == searchLoop) {
return true;
}
parent = parent.getParentLoop();
}
}
@Override @Override
public String toString() { public String toString() {
return "LOOP:" + id + ": " + start + "->" + end; return "LOOP:" + id + ": " + start + "->" + end;
@@ -1,6 +1,6 @@
package jadx.core.dex.attributes.nodes; package jadx.core.dex.attributes.nodes;
import java.util.LinkedList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import jadx.api.ICodeWriter; import jadx.api.ICodeWriter;
@@ -10,7 +10,7 @@ import jadx.core.dex.instructions.PhiInsn;
public class PhiListAttr implements IJadxAttribute { public class PhiListAttr implements IJadxAttribute {
private final List<PhiInsn> list = new LinkedList<>(); private final List<PhiInsn> list = new ArrayList<>();
@Override @Override
public AType<PhiListAttr> getAttrType() { public AType<PhiListAttr> getAttrType() {
@@ -180,12 +180,12 @@ public final class ClassInfo implements Comparable<ClassInfo> {
return makeFullClsName(pkg, name, parentClass, false, true); return makeFullClsName(pkg, name, parentClass, false, true);
} }
private String makeAliasFullName() { public String makeAliasFullName() {
return makeFullClsName(getAliasPkg(), getAliasShortName(), parentClass, true, false); return makeFullClsName(getAliasPkg(), getAliasShortName(), parentClass, true, false);
} }
private String makeAliasRawFullName() { public String makeAliasRawFullName() {
return makeFullClsName(pkg, name, parentClass, true, true); return makeFullClsName(getAliasPkg(), getAliasShortName(), parentClass, true, true);
} }
public String getAliasFullPath() { public String getAliasFullPath() {
@@ -19,29 +19,59 @@ import static jadx.core.utils.Utils.lockList;
public final class BlockNode extends AttrNode implements IBlock, Comparable<BlockNode> { public final class BlockNode extends AttrNode implements IBlock, Comparable<BlockNode> {
/**
* Const ID
*/
private final int cid;
/**
* ID linked to position in blocks list (easier to use BitSet)
* TODO: rename to avoid confusion
*/
private int id; private int id;
/**
* Offset in methods bytecode
*/
private final int startOffset; private final int startOffset;
private final List<InsnNode> instructions = new ArrayList<>(2); private final List<InsnNode> instructions = new ArrayList<>(2);
private List<BlockNode> predecessors = new ArrayList<>(1); private List<BlockNode> predecessors = new ArrayList<>(1);
private List<BlockNode> successors = new ArrayList<>(1); private List<BlockNode> successors = new ArrayList<>(1);
private List<BlockNode> cleanSuccessors; private List<BlockNode> cleanSuccessors;
// all dominators /**
* All dominators, excluding self
*/
private BitSet doms = EmptyBitSet.EMPTY; private BitSet doms = EmptyBitSet.EMPTY;
// dominance frontier
/**
* Dominance frontier
*/
private BitSet domFrontier; private BitSet domFrontier;
// immediate dominator
/**
* Immediate dominator
*/
private BlockNode idom; private BlockNode idom;
// blocks on which dominates this block
/**
* Blocks on which dominates this block
*/
private List<BlockNode> dominatesOn = new ArrayList<>(3); private List<BlockNode> dominatesOn = new ArrayList<>(3);
public BlockNode(int id, int offset) { public BlockNode(int cid, int id, int offset) {
this.cid = cid;
this.id = id; this.id = id;
this.startOffset = offset; this.startOffset = offset;
} }
public void setId(int id) { public int getCId() {
return cid;
}
void setId(int id) {
this.id = id; this.id = id;
} }
@@ -170,6 +200,10 @@ public final class BlockNode extends AttrNode implements IBlock, Comparable<Bloc
return contains(AFlag.RETURN); return contains(AFlag.RETURN);
} }
public boolean isEmpty() {
return instructions.isEmpty();
}
@Override @Override
public int hashCode() { public int hashCode() {
return startOffset; return startOffset;
@@ -184,12 +218,12 @@ public final class BlockNode extends AttrNode implements IBlock, Comparable<Bloc
return false; return false;
} }
BlockNode other = (BlockNode) obj; BlockNode other = (BlockNode) obj;
return id == other.id && startOffset == other.startOffset; return cid == other.cid && startOffset == other.startOffset;
} }
@Override @Override
public int compareTo(@NotNull BlockNode o) { public int compareTo(@NotNull BlockNode o) {
return Integer.compare(id, o.id); return Integer.compare(cid, o.cid);
} }
@Override @Override
@@ -199,6 +233,6 @@ public final class BlockNode extends AttrNode implements IBlock, Comparable<Bloc
@Override @Override
public String toString() { public String toString() {
return "B:" + id + ':' + InsnUtils.formatOffset(startOffset); return "B:" + cid + ':' + InsnUtils.formatOffset(startOffset);
} }
} }
@@ -21,6 +21,7 @@ import jadx.api.ICodeCache;
import jadx.api.ICodeInfo; import jadx.api.ICodeInfo;
import jadx.api.ICodeWriter; import jadx.api.ICodeWriter;
import jadx.api.JadxArgs; import jadx.api.JadxArgs;
import jadx.api.impl.SimpleCodeInfo;
import jadx.api.plugins.input.data.IClassData; import jadx.api.plugins.input.data.IClassData;
import jadx.api.plugins.input.data.IFieldData; import jadx.api.plugins.input.data.IFieldData;
import jadx.api.plugins.input.data.IMethodData; import jadx.api.plugins.input.data.IMethodData;
@@ -378,8 +379,16 @@ public class ClassNode extends NotificationAttrNode implements ILoadable, ICodeN
return code; return code;
} }
} }
ICodeInfo codeInfo = root.getProcessClasses().generateCode(this); ICodeInfo codeInfo;
codeCache.add(clsRawName, 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);
}
return codeInfo; return codeInfo;
} }
@@ -460,6 +469,9 @@ public class ClassNode extends NotificationAttrNode implements ILoadable, ICodeN
} }
public void addField(FieldNode fld) { public void addField(FieldNode fld) {
if (fields == null || fields.isEmpty()) {
fields = new ArrayList<>(1);
}
fields.add(fld); fields.add(fld);
} }
@@ -648,6 +660,10 @@ public class ClassNode extends NotificationAttrNode implements ILoadable, ICodeN
return contains(AType.ANONYMOUS_CLASS); return contains(AType.ANONYMOUS_CLASS);
} }
public boolean isSynthetic() {
return contains(AFlag.SYNTHETIC);
}
public boolean isInner() { public boolean isInner() {
return parentClass != this; return parentClass != this;
} }
@@ -789,7 +805,9 @@ public class ClassNode extends NotificationAttrNode implements ILoadable, ICodeN
} }
public void addCodegenDep(ClassNode dep) { public void addCodegenDep(ClassNode dep) {
this.codegenDeps = ListUtils.safeAdd(this.codegenDeps, dep); if (!codegenDeps.contains(dep)) {
this.codegenDeps = ListUtils.safeAdd(this.codegenDeps, dep);
}
} }
public int getTotalDepsCount() { public int getTotalDepsCount() {
@@ -57,6 +57,10 @@ public class FieldNode extends NotificationAttrNode implements ICodeNode {
return accFlags.isStatic(); return accFlags.isStatic();
} }
public boolean isInstance() {
return !accFlags.isStatic();
}
public String getName() { public String getName() {
return fieldInfo.getName(); return fieldInfo.getName();
} }
@@ -65,6 +69,10 @@ public class FieldNode extends NotificationAttrNode implements ICodeNode {
return fieldInfo.getAlias(); return fieldInfo.getAlias();
} }
public void rename(String alias) {
fieldInfo.setAlias(alias);
}
public ArgType getType() { public ArgType getType() {
return type; return type;
} }
@@ -73,6 +81,10 @@ public class FieldNode extends NotificationAttrNode implements ICodeNode {
return parentClass; return parentClass;
} }
public ClassNode getTopParentClass() {
return parentClass.getTopParentClass();
}
public List<MethodNode> getUseIn() { public List<MethodNode> getUseIn() {
return useIn; return useIn;
} }
@@ -264,21 +264,6 @@ public class InsnNode extends LineAttrNode {
} }
} }
public boolean canReorderRecursive() {
if (!canReorder()) {
return false;
}
for (InsnArg arg : this.getArguments()) {
if (arg.isInsnWrap()) {
InsnNode wrapInsn = ((InsnWrapArg) arg).getWrapInsn();
if (!wrapInsn.canReorderRecursive()) {
return false;
}
}
}
return true;
}
public boolean containsWrappedInsn() { public boolean containsWrappedInsn() {
for (InsnArg arg : this.getArguments()) { for (InsnArg arg : this.getArguments()) {
if (arg.isInsnWrap()) { if (arg.isInsnWrap()) {
@@ -61,6 +61,7 @@ public class MethodNode extends NotificationAttrNode implements IMethodDetails,
private List<RegisterArg> argsList; private List<RegisterArg> argsList;
private InsnNode[] instructions; private InsnNode[] instructions;
private List<BlockNode> blocks; private List<BlockNode> blocks;
private int blocksMaxCId;
private BlockNode enterBlock; private BlockNode enterBlock;
private BlockNode exitBlock; private BlockNode exitBlock;
private List<SSAVar> sVars; private List<SSAVar> sVars;
@@ -316,6 +317,19 @@ public class MethodNode extends NotificationAttrNode implements IMethodDetails,
return blocks; return blocks;
} }
public void setBasicBlocks(List<BlockNode> blocks) {
this.blocks = blocks;
int i = 0;
for (BlockNode block : blocks) {
block.setId(i);
i++;
}
}
public int getNextBlockCId() {
return blocksMaxCId++;
}
public BlockNode getEnterBlock() { public BlockNode getEnterBlock() {
return enterBlock; return enterBlock;
} }
@@ -461,6 +475,10 @@ public class MethodNode extends NotificationAttrNode implements IMethodDetails,
return regsCount; return regsCount;
} }
public int getArgsStartReg() {
return argsStartReg;
}
public SSAVar makeNewSVar(@NotNull RegisterArg assignArg) { public SSAVar makeNewSVar(@NotNull RegisterArg assignArg) {
int regNum = assignArg.getRegNum(); int regNum = assignArg.getRegNum();
return makeNewSVar(regNum, getNextSVarVersion(regNum), assignArg); return makeNewSVar(regNum, getNextSVarVersion(regNum), assignArg);
@@ -42,7 +42,8 @@ import jadx.core.utils.StringUtils;
import jadx.core.utils.Utils; import jadx.core.utils.Utils;
import jadx.core.utils.android.AndroidResourcesUtils; import jadx.core.utils.android.AndroidResourcesUtils;
import jadx.core.utils.exceptions.JadxRuntimeException; 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.ResourceStorage;
import jadx.core.xmlgen.entry.ResourceEntry; import jadx.core.xmlgen.entry.ResourceEntry;
import jadx.core.xmlgen.entry.ValuesParser; import jadx.core.xmlgen.entry.ValuesParser;
@@ -115,21 +116,25 @@ public class RootNode {
} }
private void addDummyClass(IClassData classData, Exception exc) { private void addDummyClass(IClassData classData, Exception exc) {
String typeStr = classData.getType();
String name = null;
try { try {
ClassInfo clsInfo = ClassInfo.fromName(this, typeStr); String typeStr = classData.getType();
if (clsInfo != null) { String name = null;
name = clsInfo.getShortName(); try {
ClassInfo clsInfo = ClassInfo.fromName(this, typeStr);
if (clsInfo != null) {
name = clsInfo.getShortName();
}
} catch (Exception e) {
LOG.error("Failed to get name for class with type {}", typeStr, e);
} }
} catch (Exception e) { if (name == null || name.isEmpty()) {
LOG.error("Failed to get name for class with type {}", typeStr, e); name = "CLASS_" + typeStr;
}
ClassNode clsNode = ClassNode.addSyntheticClass(this, name, classData.getAccessFlags());
ErrorsCounter.error(clsNode, "Load error", exc);
} catch (Exception innerExc) {
LOG.error("Failed to load class from file: {}", classData.getInputFileName(), exc);
} }
if (name == null || name.isEmpty()) {
name = "CLASS_" + typeStr;
}
ClassNode clsNode = ClassNode.addSyntheticClass(this, name, classData.getAccessFlags());
ErrorsCounter.error(clsNode, "Load error", exc);
} }
private static void markDuplicatedClasses(List<ClassNode> classes) { private static void markDuplicatedClasses(List<ClassNode> classes) {
@@ -159,23 +164,13 @@ public class RootNode {
} }
public void loadResources(List<ResourceFile> resources) { public void loadResources(List<ResourceFile> resources) {
ResourceFile arsc = null; ResourceFile arsc = getResourceFile(resources);
for (ResourceFile rf : resources) {
if (rf.getType() == ResourceType.ARSC) {
arsc = rf;
break;
}
}
if (arsc == null) { if (arsc == null) {
LOG.debug("'.arsc' file not found"); LOG.debug("'.arsc' file not found");
return; return;
} }
try { try {
ResTableParser parser = ResourcesLoader.decodeStream(arsc, (size, is) -> { IResParser parser = ResourcesLoader.decodeStream(arsc, (size, is) -> ResDecoder.decode(this, arsc, is));
ResTableParser tableParser = new ResTableParser(this);
tableParser.decode(is);
return tableParser;
});
if (parser != null) { if (parser != null) {
processResources(parser.getResStorage()); processResources(parser.getResStorage());
updateObfuscatedFiles(parser, resources); updateObfuscatedFiles(parser, resources);
@@ -185,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) { public void processResources(ResourceStorage resStorage) {
constValues.setResourcesNames(resStorage.getResourcesNames()); constValues.setResourcesNames(resStorage.getResourcesNames());
appPackage = resStorage.getAppPackage(); appPackage = resStorage.getAppPackage();
@@ -205,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()) { if (args.isSkipResources()) {
return; return;
} }
@@ -265,6 +269,7 @@ public class RootNode {
public void runPreDecompileStage() { public void runPreDecompileStage() {
boolean debugEnabled = LOG.isDebugEnabled(); boolean debugEnabled = LOG.isDebugEnabled();
for (IDexTreeVisitor pass : preDecompilePasses) { for (IDexTreeVisitor pass : preDecompilePasses) {
Utils.checkThreadInterrupt();
long start = debugEnabled ? System.currentTimeMillis() : 0; long start = debugEnabled ? System.currentTimeMillis() : 0;
try { try {
pass.init(this); pass.init(this);
@@ -156,7 +156,7 @@ public final class IfCondition extends AttrNode {
return i; return i;
} }
if (c.getOp() == IfOp.EQ && c.getB().isFalse()) { if (c.getOp() == IfOp.EQ && c.getB().isFalse()) {
cond = not(new IfCondition(c.invert())); cond = new IfCondition(Mode.NOT, Collections.singletonList(new IfCondition(c.invert())));
} else { } else {
c.normalize(); c.normalize();
} }
@@ -49,6 +49,10 @@ public final class LoopRegion extends ConditionRegion {
return header; return header;
} }
public boolean isEndless() {
return header == null;
}
public IRegion getBody() { public IRegion getBody() {
return body; return body;
} }
@@ -14,9 +14,9 @@ import jadx.core.dex.instructions.args.ArgType;
import jadx.core.dex.instructions.args.InsnArg; import jadx.core.dex.instructions.args.InsnArg;
import jadx.core.dex.nodes.BlockNode; import jadx.core.dex.nodes.BlockNode;
import jadx.core.dex.nodes.IContainer; import jadx.core.dex.nodes.IContainer;
import jadx.core.dex.nodes.MethodNode;
import jadx.core.utils.InsnUtils; import jadx.core.utils.InsnUtils;
import jadx.core.utils.Utils; import jadx.core.utils.Utils;
import jadx.core.utils.exceptions.JadxRuntimeException;
public class ExceptionHandler { public class ExceptionHandler {
@@ -33,9 +33,14 @@ public class ExceptionHandler {
private boolean removed = false; 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; this.handlerOffset = addr;
addCatchType(type);
} }
/** /**
@@ -43,7 +48,7 @@ public class ExceptionHandler {
* *
* @param type - null for 'all' or 'Throwable' handler * @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 (type != null) {
if (catchTypes.contains(type)) { if (catchTypes.contains(type)) {
return false; return false;
@@ -51,14 +56,16 @@ public class ExceptionHandler {
return catchTypes.add(type); return catchTypes.add(type);
} }
if (!this.catchTypes.isEmpty()) { 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; return false;
} }
public void addCatchTypes(Collection<ClassInfo> types) { public void addCatchTypes(MethodNode mth, Collection<ClassInfo> types) {
for (ClassInfo type : types) { for (ClassInfo type : types) {
addCatchType(type); addCatchType(mth, type);
} }
} }
@@ -60,6 +60,10 @@ public class TryCatchBlockAttr implements IJadxAttribute {
return throwFound; return throwFound;
} }
public int getId() {
return id;
}
public List<ExceptionHandler> getHandlers() { public List<ExceptionHandler> getHandlers() {
return handlers; return handlers;
} }
@@ -133,7 +133,7 @@ public class AttachTryCatchVisitor extends AbstractVisitor {
ExcHandlerAttr excHandlerAttr = insn.get(AType.EXC_HANDLER); ExcHandlerAttr excHandlerAttr = insn.get(AType.EXC_HANDLER);
if (excHandlerAttr != null) { if (excHandlerAttr != null) {
ExceptionHandler handler = excHandlerAttr.getHandler(); 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 // exist handler updated (assume from same try block) - don't add again
return null; return null;
} }
@@ -143,7 +143,7 @@ public class AttachTryCatchVisitor extends AbstractVisitor {
} else { } else {
insn = insertNOP(insnByOffset, handlerOffset); insn = insertNOP(insnByOffset, handlerOffset);
} }
ExceptionHandler handler = new ExceptionHandler(handlerOffset, type); ExceptionHandler handler = ExceptionHandler.build(mth, handlerOffset, type);
mth.addExceptionHandler(handler); mth.addExceptionHandler(handler);
insn.addAttr(new ExcHandlerAttr(handler)); insn.addAttr(new ExcHandlerAttr(handler));
return handler; return handler;
@@ -10,6 +10,7 @@ import java.util.Objects;
import jadx.api.plugins.input.data.AccessFlags; import jadx.api.plugins.input.data.AccessFlags;
import jadx.core.Consts; import jadx.core.Consts;
import jadx.core.dex.attributes.AFlag; 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.FieldReplaceAttr;
import jadx.core.dex.attributes.nodes.MethodReplaceAttr; import jadx.core.dex.attributes.nodes.MethodReplaceAttr;
import jadx.core.dex.attributes.nodes.SkipMethodArgsAttr; import jadx.core.dex.attributes.nodes.SkipMethodArgsAttr;
@@ -225,7 +226,7 @@ public class ClassModifier extends AbstractVisitor {
} }
private static boolean removeBridgeMethod(ClassNode cls, MethodNode mth) { 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()); List<InsnNode> allInsns = BlockUtils.collectAllInsns(mth.getBasicBlocks());
if (allInsns.size() == 1) { if (allInsns.size() == 1) {
InsnNode wrappedInsn = allInsns.get(0); InsnNode wrappedInsn = allInsns.get(0);
@@ -235,12 +236,10 @@ public class ClassModifier extends AbstractVisitor {
wrappedInsn = ((InsnWrapArg) arg).getWrapInsn(); wrappedInsn = ((InsnWrapArg) arg).getWrapInsn();
} }
} }
if (checkSyntheticWrapper(mth, wrappedInsn)) { return checkSyntheticWrapper(mth, wrappedInsn);
return true;
}
} }
} }
return !isMethodUnique(cls, mth); return false;
} }
private static boolean checkSyntheticWrapper(MethodNode mth, InsnNode insn) { private static boolean checkSyntheticWrapper(MethodNode mth, InsnNode insn) {
@@ -283,6 +282,9 @@ public class ClassModifier extends AbstractVisitor {
if (!Objects.equals(wrappedMth.getAlias(), alias)) { if (!Objects.equals(wrappedMth.getAlias(), alias)) {
wrappedMth.getMethodInfo().setAlias(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; return true;
} }
@@ -299,20 +301,6 @@ public class ClassModifier extends AbstractVisitor {
return false; 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) * Remove public empty constructors (static or default)
*/ */
@@ -62,45 +62,47 @@ public class ConstInlineVisitor extends AbstractVisitor {
|| insn.getResult() == null) { || insn.getResult() == null) {
return; return;
} }
SSAVar sVar = insn.getResult().getSVar(); SSAVar sVar = insn.getResult().getSVar();
InsnArg constArg; InsnArg constArg;
Runnable onSuccess = null; Runnable onSuccess = null;
switch (insn.getType()) {
InsnType insnType = insn.getType(); case CONST:
if (insnType == InsnType.CONST || insnType == InsnType.MOVE) { case MOVE: {
constArg = insn.getArg(0); constArg = insn.getArg(0);
if (!constArg.isLiteral()) { if (!constArg.isLiteral()) {
return;
}
long lit = ((LiteralArg) constArg).getLiteral();
if (lit == 0 && forbidNullInlines(sVar)) {
// all usages forbids inlining
return;
}
break;
}
case CONST_STR: {
String s = ((ConstStringNode) insn).getString();
FieldNode f = mth.getParentClass().getConstField(s);
if (f == null) {
InsnNode copy = insn.copyWithoutResult();
constArg = InsnArg.wrapArg(copy);
} else {
InsnNode constGet = new IndexInsnNode(InsnType.SGET, f.getFieldInfo(), 0);
constArg = InsnArg.wrapArg(constGet);
constArg.setType(ArgType.STRING);
onSuccess = () -> f.addUseIn(mth);
}
break;
}
case CONST_CLASS: {
if (sVar.isUsedInPhi()) {
return;
}
constArg = InsnArg.wrapArg(insn.copyWithoutResult());
constArg.setType(ArgType.CLASS);
break;
}
default:
return; return;
}
long lit = ((LiteralArg) constArg).getLiteral();
if (lit == 0 && forbidNullInlines(sVar)) {
// all usages forbids inlining
return;
}
} else if (insnType == InsnType.CONST_STR) {
if (sVar.isUsedInPhi()) {
return;
}
String s = ((ConstStringNode) insn).getString();
FieldNode f = mth.getParentClass().getConstField(s);
if (f == null) {
InsnNode copy = insn.copyWithoutResult();
constArg = InsnArg.wrapArg(copy);
} else {
InsnNode constGet = new IndexInsnNode(InsnType.SGET, f.getFieldInfo(), 0);
constArg = InsnArg.wrapArg(constGet);
constArg.setType(ArgType.STRING);
onSuccess = () -> f.addUseIn(mth);
}
} else if (insnType == InsnType.CONST_CLASS) {
if (sVar.isUsedInPhi()) {
return;
}
constArg = InsnArg.wrapArg(insn.copyWithoutResult());
constArg.setType(ArgType.CLASS);
} else {
return;
} }
// all check passed, run replace // all check passed, run replace
@@ -100,7 +100,7 @@ public class DotGraphVisitor extends AbstractVisitor {
if (insnArr == null) { if (insnArr == null) {
return; return;
} }
BlockNode block = new BlockNode(0, 0); BlockNode block = new BlockNode(0, 0, 0);
List<InsnNode> insnList = block.getInstructions(); List<InsnNode> insnList = block.getInstructions();
for (InsnNode insn : insnArr) { for (InsnNode insn : insnArr) {
if (insn != null) { if (insn != null) {
@@ -199,7 +199,7 @@ public class DotGraphVisitor extends AbstractVisitor {
dot.add("color=red,"); dot.add("color=red,");
} }
dot.add("label=\"{"); dot.add("label=\"{");
dot.add(String.valueOf(block.getId())).add("\\:\\ "); dot.add(String.valueOf(block.getCId())).add("\\:\\ ");
dot.add(InsnUtils.formatOffset(block.getStartOffset())); dot.add(InsnUtils.formatOffset(block.getStartOffset()));
if (!attrs.isEmpty()) { if (!attrs.isEmpty()) {
dot.add('|').add(attrs); dot.add('|').add(attrs);
@@ -230,10 +230,10 @@ public class DotGraphVisitor extends AbstractVisitor {
if (PRINT_DOMINATORS) { if (PRINT_DOMINATORS) {
for (BlockNode c : block.getDominatesOn()) { for (BlockNode c : block.getDominatesOn()) {
conn.startLine(block.getId() + " -> " + c.getId() + "[color=green];"); conn.startLine(block.getCId() + " -> " + c.getCId() + "[color=green];");
} }
for (BlockNode dom : BlockUtils.bitSetToBlocks(mth, block.getDomFrontier())) { for (BlockNode dom : BlockUtils.bitSetToBlocks(mth, block.getDomFrontier())) {
conn.startLine("f_" + block.getId() + " -> f_" + dom.getId() + "[color=blue];"); conn.startLine("f_" + block.getCId() + " -> f_" + dom.getCId() + "[color=blue];");
} }
} }
} }
@@ -273,7 +273,7 @@ public class DotGraphVisitor extends AbstractVisitor {
private String makeName(IContainer c) { private String makeName(IContainer c) {
String name; String name;
if (c instanceof BlockNode) { if (c instanceof BlockNode) {
name = "Node_" + ((BlockNode) c).getId(); name = "Node_" + ((BlockNode) c).getCId();
} else if (c instanceof IBlock) { } else if (c instanceof IBlock) {
name = "Node_" + c.getClass().getSimpleName() + '_' + c.hashCode(); name = "Node_" + c.getClass().getSimpleName() + '_' + c.hashCode();
} else { } else {
@@ -18,6 +18,7 @@ import jadx.core.dex.attributes.AFlag;
import jadx.core.dex.attributes.AType; import jadx.core.dex.attributes.AType;
import jadx.core.dex.attributes.nodes.EnumClassAttr; import jadx.core.dex.attributes.nodes.EnumClassAttr;
import jadx.core.dex.attributes.nodes.EnumClassAttr.EnumField; 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.attributes.nodes.SkipMethodArgsAttr;
import jadx.core.dex.info.AccessInfo; import jadx.core.dex.info.AccessInfo;
import jadx.core.dex.info.ClassInfo; 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.IndexInsnNode;
import jadx.core.dex.instructions.InsnType; import jadx.core.dex.instructions.InsnType;
import jadx.core.dex.instructions.InvokeNode; 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.ArgType;
import jadx.core.dex.instructions.args.InsnArg; import jadx.core.dex.instructions.args.InsnArg;
import jadx.core.dex.instructions.args.InsnWrapArg; import jadx.core.dex.instructions.args.InsnWrapArg;
@@ -35,9 +37,13 @@ import jadx.core.dex.instructions.mods.ConstructorInsn;
import jadx.core.dex.nodes.BlockNode; import jadx.core.dex.nodes.BlockNode;
import jadx.core.dex.nodes.ClassNode; import jadx.core.dex.nodes.ClassNode;
import jadx.core.dex.nodes.FieldNode; import jadx.core.dex.nodes.FieldNode;
import jadx.core.dex.nodes.IContainer;
import jadx.core.dex.nodes.InsnNode; import jadx.core.dex.nodes.InsnNode;
import jadx.core.dex.nodes.MethodNode; import jadx.core.dex.nodes.MethodNode;
import jadx.core.dex.nodes.RootNode; import jadx.core.dex.nodes.RootNode;
import jadx.core.dex.regions.Region;
import jadx.core.dex.visitors.regions.CheckRegions;
import jadx.core.dex.visitors.regions.IfRegionVisitor;
import jadx.core.dex.visitors.shrink.CodeShrinkVisitor; import jadx.core.dex.visitors.shrink.CodeShrinkVisitor;
import jadx.core.utils.BlockInsnPair; import jadx.core.utils.BlockInsnPair;
import jadx.core.utils.BlockUtils; import jadx.core.utils.BlockUtils;
@@ -45,6 +51,7 @@ import jadx.core.utils.InsnRemover;
import jadx.core.utils.InsnUtils; import jadx.core.utils.InsnUtils;
import jadx.core.utils.Utils; import jadx.core.utils.Utils;
import jadx.core.utils.exceptions.JadxException; import jadx.core.utils.exceptions.JadxException;
import jadx.core.utils.exceptions.JadxRuntimeException;
import static jadx.core.utils.InsnUtils.checkInsnType; import static jadx.core.utils.InsnUtils.checkInsnType;
import static jadx.core.utils.InsnUtils.getSingleArg; import static jadx.core.utils.InsnUtils.getSingleArg;
@@ -53,12 +60,21 @@ import static jadx.core.utils.InsnUtils.getWrappedInsn;
@JadxVisitor( @JadxVisitor(
name = "EnumVisitor", name = "EnumVisitor",
desc = "Restore enum classes", desc = "Restore enum classes",
runAfter = { CodeShrinkVisitor.class, ModVisitor.class, ReSugarCode.class }, runAfter = {
runBefore = { ExtractFieldInit.class } CodeShrinkVisitor.class, // all possible instructions already inlined
ModVisitor.class,
ReSugarCode.class,
IfRegionVisitor.class, // ternary operator inlined
CheckRegions.class // regions processing finished
},
runBefore = {
ExtractFieldInit.class
}
) )
public class EnumVisitor extends AbstractVisitor { public class EnumVisitor extends AbstractVisitor {
private MethodInfo enumValueOfMth; private MethodInfo enumValueOfMth;
private MethodInfo cloneMth;
@Override @Override
public void init(RootNode root) { public void init(RootNode root) {
@@ -68,86 +84,77 @@ public class EnumVisitor extends AbstractVisitor {
"valueOf", "valueOf",
Arrays.asList(ArgType.CLASS, ArgType.STRING), Arrays.asList(ArgType.CLASS, ArgType.STRING),
ArgType.ENUM); ArgType.ENUM);
cloneMth = MethodInfo.fromDetails(root,
ClassInfo.fromType(root, ArgType.OBJECT),
"clone",
Collections.emptyList(),
ArgType.OBJECT);
} }
@Override @Override
public boolean visit(ClassNode cls) throws JadxException { public boolean visit(ClassNode cls) throws JadxException {
boolean converted; if (cls.isEnum()) {
try { boolean converted;
converted = convertToEnum(cls); try {
} catch (Exception e) { converted = convertToEnum(cls);
cls.addWarnComment("Enum visitor error", e); } catch (Exception e) {
converted = false; cls.addWarnComment("Enum visitor error", e);
} converted = false;
if (!converted) { }
AccessInfo accessFlags = cls.getAccessFlags(); if (!converted) {
if (accessFlags.isEnum()) { AccessInfo accessFlags = cls.getAccessFlags();
cls.setAccessFlags(accessFlags.remove(AccessFlags.ENUM)); if (accessFlags.isEnum()) {
cls.addWarnComment("Failed to restore enum class, 'enum' modifier removed"); cls.setAccessFlags(accessFlags.remove(AccessFlags.ENUM));
cls.addWarnComment("Failed to restore enum class, 'enum' modifier and super class removed");
}
} }
} }
return true; return true;
} }
private boolean convertToEnum(ClassNode cls) { private boolean convertToEnum(ClassNode cls) {
if (!cls.isEnum()) { ArgType superType = cls.getSuperClass();
return false; if (superType != null && superType.getObject().equals(ArgType.ENUM.getObject())) {
cls.add(AFlag.REMOVE_SUPER_CLASS);
} }
MethodNode classInitMth = cls.getClassInitMth(); MethodNode classInitMth = cls.getClassInitMth();
if (classInitMth == null) { if (classInitMth == null) {
cls.addWarnComment("Enum class init method not found"); cls.addWarnComment("Enum class init method not found");
return false; return false;
} }
if (classInitMth.getBasicBlocks().isEmpty()) { Region staticRegion = classInitMth.getRegion();
if (staticRegion == null || classInitMth.getBasicBlocks().isEmpty()) {
return false; return false;
} }
ArgType clsType = cls.getClassInfo().getType(); // collect blocks on linear part of static method (ignore branching on method end)
List<BlockNode> staticBlocks = new ArrayList<>();
// search "$VALUES" field (holds all enum values) for (IContainer subBlock : staticRegion.getSubBlocks()) {
List<FieldNode> valuesCandidates = cls.getFields().stream() if (subBlock instanceof BlockNode) {
.filter(f -> f.getAccessFlags().isStatic()) staticBlocks.add((BlockNode) subBlock);
.filter(f -> f.getType().isArray()) } else {
.filter(f -> Objects.equals(f.getType().getArrayRootElement(), clsType)) break;
.collect(Collectors.toList());
if (valuesCandidates.isEmpty()) {
return false;
}
if (valuesCandidates.size() > 1) {
valuesCandidates.removeIf(f -> !f.getAccessFlags().isSynthetic());
}
if (valuesCandidates.size() > 1) {
Optional<FieldNode> valuesOpt = valuesCandidates.stream().filter(f -> f.getName().equals("$VALUES")).findAny();
if (valuesOpt.isPresent()) {
valuesCandidates.clear();
valuesCandidates.add(valuesOpt.get());
} }
} }
if (valuesCandidates.size() != 1) { if (staticBlocks.isEmpty()) {
cls.addWarnComment("Found several \"values\" enum fields: " + valuesCandidates); cls.addWarnComment("Unexpected branching in enum static init block");
return false; return false;
} }
FieldNode valuesField = valuesCandidates.get(0); EnumData data = new EnumData(cls, classInitMth, staticBlocks);
List<InsnNode> toRemove = new ArrayList<>(); if (!searchValuesField(data)) {
// search "$VALUES" array init and collect enum fields
BlockInsnPair valuesInitPair = getValuesInitInsn(classInitMth, valuesField);
if (valuesInitPair == null) {
return false; return false;
} }
BlockNode staticBlock = valuesInitPair.getBlock();
InsnNode valuesInitInsn = valuesInitPair.getInsn();
List<EnumField> enumFields = null; List<EnumField> enumFields = null;
InsnArg arrArg = valuesInitInsn.getArg(0); InsnArg arrArg = data.valuesInitInsn.getArg(0);
if (arrArg.isInsnWrap()) { if (arrArg.isInsnWrap()) {
InsnNode wrappedInsn = ((InsnWrapArg) arrArg).getWrapInsn(); InsnNode wrappedInsn = ((InsnWrapArg) arrArg).getWrapInsn();
enumFields = extractEnumFieldsFromInsn(cls, staticBlock, wrappedInsn, toRemove); enumFields = extractEnumFieldsFromInsn(data, wrappedInsn);
} }
if (enumFields == null) { if (enumFields == null) {
cls.addWarnComment("Unknown enum class pattern. Please report as an issue!");
return false; return false;
} }
toRemove.add(valuesInitInsn); data.toRemove.add(data.valuesInitInsn);
// all checks complete, perform transform // all checks complete, perform transform
EnumClassAttr attr = new EnumClassAttr(enumFields); EnumClassAttr attr = new EnumClassAttr(enumFields);
@@ -167,59 +174,92 @@ public class EnumVisitor extends AbstractVisitor {
fieldNode.getFieldInfo().setAlias(name); fieldNode.getFieldInfo().setAlias(name);
} }
fieldNode.add(AFlag.DONT_GENERATE); fieldNode.add(AFlag.DONT_GENERATE);
processConstructorInsn(cls, enumField, classInitMth, staticBlock, toRemove); processConstructorInsn(data, enumField, classInitMth);
} }
valuesField.add(AFlag.DONT_GENERATE); data.valuesField.add(AFlag.DONT_GENERATE);
InsnRemover.removeAllAndUnbind(classInitMth, staticBlock, toRemove); InsnRemover.removeAllAndUnbind(classInitMth, data.toRemove);
if (classInitMth.countInsns() == 0) { if (classInitMth.countInsns() == 0) {
classInitMth.add(AFlag.DONT_GENERATE); classInitMth.add(AFlag.DONT_GENERATE);
} else if (!toRemove.isEmpty()) { } else if (!data.toRemove.isEmpty()) {
CodeShrinkVisitor.shrinkMethod(classInitMth); CodeShrinkVisitor.shrinkMethod(classInitMth);
} }
removeEnumMethods(cls, clsType, valuesField); removeEnumMethods(cls, data.valuesField);
return true; return true;
} }
private void processConstructorInsn(ClassNode cls, EnumField enumField, MethodNode classInitMth, /**
BlockNode staticBlock, List<InsnNode> toRemove) { * Search "$VALUES" field (holds all enum values)
ConstructorInsn co = enumField.getConstrInsn(); */
ClassInfo enumClsInfo = co.getClassType(); private boolean searchValuesField(EnumData data) {
if (!enumClsInfo.equals(cls.getClassInfo())) { ArgType clsType = data.cls.getClassInfo().getType();
ClassNode enumCls = cls.root().resolveClass(enumClsInfo); List<FieldNode> valuesCandidates = data.cls.getFields().stream()
if (enumCls != null) { .filter(f -> f.getAccessFlags().isStatic())
processEnumCls(cls, enumField, enumCls); .filter(f -> f.getType().isArray())
.filter(f -> Objects.equals(f.getType().getArrayRootElement(), clsType))
.collect(Collectors.toList());
if (valuesCandidates.isEmpty()) {
data.cls.addWarnComment("$VALUES field not found");
return false;
}
if (valuesCandidates.size() > 1) {
valuesCandidates.removeIf(f -> !f.getAccessFlags().isSynthetic());
}
if (valuesCandidates.size() > 1) {
Optional<FieldNode> valuesOpt = valuesCandidates.stream().filter(f -> f.getName().equals("$VALUES")).findAny();
if (valuesOpt.isPresent()) {
valuesCandidates.clear();
valuesCandidates.add(valuesOpt.get());
} }
} }
List<RegisterArg> regs = new ArrayList<>(); if (valuesCandidates.size() != 1) {
co.getRegisterArgs(regs); data.cls.addWarnComment("Found several \"values\" enum fields: " + valuesCandidates);
if (!regs.isEmpty()) { return false;
cls.addWarnComment("Init of enum " + enumField.getField().getName() + " can be incorrect");
} }
MethodNode ctrMth = cls.root().resolveMethod(co.getCallMth()); data.valuesField = valuesCandidates.get(0);
// search "$VALUES" array init and collect enum fields
BlockInsnPair valuesInitPair = getValuesInitInsn(data);
if (valuesInitPair == null) {
return false;
}
data.valuesInitInsn = valuesInitPair.getInsn();
return true;
}
private void processConstructorInsn(EnumData data, EnumField enumField, MethodNode classInitMth) {
ConstructorInsn co = enumField.getConstrInsn();
ClassInfo enumClsInfo = co.getClassType();
if (!enumClsInfo.equals(data.cls.getClassInfo())) {
ClassNode enumCls = data.cls.root().resolveClass(enumClsInfo);
if (enumCls != null) {
processEnumCls(data.cls, enumField, enumCls);
}
}
MethodNode ctrMth = data.cls.root().resolveMethod(co.getCallMth());
if (ctrMth != null) { if (ctrMth != null) {
markArgsForSkip(ctrMth); markArgsForSkip(ctrMth);
} }
RegisterArg coResArg = co.getResult(); RegisterArg coResArg = co.getResult();
if (coResArg == null || coResArg.getSVar().getUseList().size() <= 2) { if (coResArg == null || coResArg.getSVar().getUseList().size() <= 2) {
toRemove.add(co); data.toRemove.add(co);
} else { } else {
// constructor result used in other places -> replace constructor with enum field get (SGET) // constructor result used in other places -> replace constructor with enum field get (SGET)
IndexInsnNode enumGet = new IndexInsnNode(InsnType.SGET, enumField.getField().getFieldInfo(), 0); IndexInsnNode enumGet = new IndexInsnNode(InsnType.SGET, enumField.getField().getFieldInfo(), 0);
enumGet.setResult(coResArg.duplicate()); enumGet.setResult(coResArg.duplicate());
BlockUtils.replaceInsn(classInitMth, staticBlock, co, enumGet); BlockUtils.replaceInsn(classInitMth, co, enumGet);
} }
} }
@Nullable @Nullable
private List<EnumField> extractEnumFieldsFromInsn(ClassNode cls, BlockNode staticBlock, private List<EnumField> extractEnumFieldsFromInsn(EnumData enumData, InsnNode wrappedInsn) {
InsnNode wrappedInsn, List<InsnNode> toRemove) {
switch (wrappedInsn.getType()) { switch (wrappedInsn.getType()) {
case FILLED_NEW_ARRAY: case FILLED_NEW_ARRAY:
return extractEnumFieldsFromFilledArray(cls, wrappedInsn, staticBlock, toRemove); return extractEnumFieldsFromFilledArray(enumData, wrappedInsn);
case INVOKE: case INVOKE:
// handle redirection of values array fill (added in java 15) // handle redirection of values array fill (added in java 15)
return extractEnumFieldsFromInvoke(cls, staticBlock, (InvokeNode) wrappedInsn, toRemove); return extractEnumFieldsFromInvoke(enumData, (InvokeNode) wrappedInsn);
case NEW_ARRAY: case NEW_ARRAY:
InsnArg arg = wrappedInsn.getArg(0); InsnArg arg = wrappedInsn.getArg(0);
@@ -234,10 +274,9 @@ public class EnumVisitor extends AbstractVisitor {
} }
} }
private List<EnumField> extractEnumFieldsFromInvoke(ClassNode cls, BlockNode staticBlock, private List<EnumField> extractEnumFieldsFromInvoke(EnumData enumData, InvokeNode invokeNode) {
InvokeNode invokeNode, List<InsnNode> toRemove) {
MethodInfo callMth = invokeNode.getCallMth(); MethodInfo callMth = invokeNode.getCallMth();
MethodNode valuesMth = cls.root().resolveMethod(callMth); MethodNode valuesMth = enumData.cls.root().resolveMethod(callMth);
if (valuesMth == null || valuesMth.isVoidReturn()) { if (valuesMth == null || valuesMth.isVoidReturn()) {
return null; return null;
} }
@@ -247,16 +286,16 @@ public class EnumVisitor extends AbstractVisitor {
if (wrappedInsn == null) { if (wrappedInsn == null) {
return null; return null;
} }
List<EnumField> enumFields = extractEnumFieldsFromInsn(cls, staticBlock, wrappedInsn, toRemove); List<EnumField> enumFields = extractEnumFieldsFromInsn(enumData, wrappedInsn);
if (enumFields != null) { if (enumFields != null) {
valuesMth.add(AFlag.DONT_GENERATE); valuesMth.add(AFlag.DONT_GENERATE);
} }
return enumFields; return enumFields;
} }
private BlockInsnPair getValuesInitInsn(MethodNode classInitMth, FieldNode valuesField) { private BlockInsnPair getValuesInitInsn(EnumData data) {
FieldInfo searchField = valuesField.getFieldInfo(); FieldInfo searchField = data.valuesField.getFieldInfo();
for (BlockNode blockNode : classInitMth.getBasicBlocks()) { for (BlockNode blockNode : data.staticBlocks) {
for (InsnNode insn : blockNode.getInstructions()) { for (InsnNode insn : blockNode.getInstructions()) {
if (insn.getType() == InsnType.SPUT) { if (insn.getType() == InsnType.SPUT) {
IndexInsnNode indexInsnNode = (IndexInsnNode) insn; IndexInsnNode indexInsnNode = (IndexInsnNode) insn;
@@ -270,50 +309,49 @@ public class EnumVisitor extends AbstractVisitor {
return null; return null;
} }
private List<EnumField> extractEnumFieldsFromFilledArray(ClassNode cls, InsnNode arrFillInsn, BlockNode staticBlock, private List<EnumField> extractEnumFieldsFromFilledArray(EnumData enumData, InsnNode arrFillInsn) {
List<InsnNode> toRemove) {
List<EnumField> enumFields = new ArrayList<>(); List<EnumField> enumFields = new ArrayList<>();
for (InsnArg arg : arrFillInsn.getArguments()) { for (InsnArg arg : arrFillInsn.getArguments()) {
EnumField field = null; EnumField field = null;
if (arg.isInsnWrap()) { if (arg.isInsnWrap()) {
InsnNode wrappedInsn = ((InsnWrapArg) arg).getWrapInsn(); InsnNode wrappedInsn = ((InsnWrapArg) arg).getWrapInsn();
field = processEnumFieldByWrappedInsn(cls, wrappedInsn, staticBlock, toRemove); field = processEnumFieldByWrappedInsn(enumData, wrappedInsn);
} else if (arg.isRegister()) { } else if (arg.isRegister()) {
field = processEnumFieldByRegister(cls, (RegisterArg) arg, staticBlock, toRemove); field = processEnumFieldByRegister(enumData, (RegisterArg) arg);
} }
if (field == null) { if (field == null) {
return null; return null;
} }
enumFields.add(field); enumFields.add(field);
} }
toRemove.add(arrFillInsn); enumData.toRemove.add(arrFillInsn);
return enumFields; return enumFields;
} }
private EnumField processEnumFieldByWrappedInsn(ClassNode cls, InsnNode wrappedInsn, BlockNode staticBlock, List<InsnNode> toRemove) { private EnumField processEnumFieldByWrappedInsn(EnumData data, InsnNode wrappedInsn) {
if (wrappedInsn.getType() == InsnType.SGET) { if (wrappedInsn.getType() == InsnType.SGET) {
return processEnumFieldByField(cls, wrappedInsn, staticBlock, toRemove); return processEnumFieldByField(data, wrappedInsn);
} }
ConstructorInsn constructorInsn = castConstructorInsn(wrappedInsn); ConstructorInsn constructorInsn = castConstructorInsn(wrappedInsn);
if (constructorInsn != null) { if (constructorInsn != null) {
FieldNode enumFieldNode = createFakeField(cls, "EF" + constructorInsn.getOffset()); FieldNode enumFieldNode = createFakeField(data.cls, "EF" + constructorInsn.getOffset());
cls.addField(enumFieldNode); data.cls.addField(enumFieldNode);
return createEnumFieldByConstructor(cls, enumFieldNode, constructorInsn); return createEnumFieldByConstructor(data.cls, enumFieldNode, constructorInsn);
} }
return null; return null;
} }
@Nullable @Nullable
private EnumField processEnumFieldByField(ClassNode cls, InsnNode sgetInsn, BlockNode staticBlock, List<InsnNode> toRemove) { private EnumField processEnumFieldByField(EnumData data, InsnNode sgetInsn) {
if (sgetInsn.getType() != InsnType.SGET) { if (sgetInsn.getType() != InsnType.SGET) {
return null; return null;
} }
FieldInfo fieldInfo = (FieldInfo) ((IndexInsnNode) sgetInsn).getIndex(); FieldInfo fieldInfo = (FieldInfo) ((IndexInsnNode) sgetInsn).getIndex();
FieldNode enumFieldNode = cls.searchField(fieldInfo); FieldNode enumFieldNode = data.cls.searchField(fieldInfo);
if (enumFieldNode == null) { if (enumFieldNode == null) {
return null; return null;
} }
InsnNode sputInsn = searchFieldPutInsn(cls, staticBlock, enumFieldNode); InsnNode sputInsn = searchFieldPutInsn(data, enumFieldNode);
if (sputInsn == null) { if (sputInsn == null) {
return null; return null;
} }
@@ -324,17 +362,17 @@ public class EnumVisitor extends AbstractVisitor {
} }
RegisterArg sgetResult = sgetInsn.getResult(); RegisterArg sgetResult = sgetInsn.getResult();
if (sgetResult == null || sgetResult.getSVar().getUseCount() == 1) { if (sgetResult == null || sgetResult.getSVar().getUseCount() == 1) {
toRemove.add(sgetInsn); data.toRemove.add(sgetInsn);
} }
toRemove.add(sputInsn); data.toRemove.add(sputInsn);
return createEnumFieldByConstructor(cls, enumFieldNode, co); return createEnumFieldByConstructor(data.cls, enumFieldNode, co);
} }
@Nullable @Nullable
private EnumField processEnumFieldByRegister(ClassNode cls, RegisterArg arg, BlockNode staticBlock, List<InsnNode> toRemove) { private EnumField processEnumFieldByRegister(EnumData data, RegisterArg arg) {
InsnNode assignInsn = arg.getAssignInsn(); InsnNode assignInsn = arg.getAssignInsn();
if (assignInsn != null && assignInsn.getType() == InsnType.SGET) { if (assignInsn != null && assignInsn.getType() == InsnType.SGET) {
return processEnumFieldByField(cls, assignInsn, staticBlock, toRemove); return processEnumFieldByField(data, assignInsn);
} }
SSAVar ssaVar = arg.getSVar(); SSAVar ssaVar = arg.getSVar();
@@ -345,12 +383,12 @@ public class EnumVisitor extends AbstractVisitor {
if (constrInsn == null || constrInsn.getType() != InsnType.CONSTRUCTOR) { if (constrInsn == null || constrInsn.getType() != InsnType.CONSTRUCTOR) {
return null; return null;
} }
FieldNode enumFieldNode = searchEnumField(cls, ssaVar, toRemove); FieldNode enumFieldNode = searchEnumField(data, ssaVar);
if (enumFieldNode == null) { if (enumFieldNode == null) {
enumFieldNode = createFakeField(cls, "EF" + arg.getRegNum()); enumFieldNode = createFakeField(data.cls, "EF" + arg.getRegNum());
cls.addField(enumFieldNode); data.cls.addField(enumFieldNode);
} }
return createEnumFieldByConstructor(cls, enumFieldNode, (ConstructorInsn) constrInsn); return createEnumFieldByConstructor(data.cls, enumFieldNode, (ConstructorInsn) constrInsn);
} }
private FieldNode createFakeField(ClassNode cls, String name) { private FieldNode createFakeField(ClassNode cls, String name) {
@@ -363,20 +401,21 @@ public class EnumVisitor extends AbstractVisitor {
} }
@Nullable @Nullable
private FieldNode searchEnumField(ClassNode cls, SSAVar ssaVar, List<InsnNode> toRemove) { private FieldNode searchEnumField(EnumData data, SSAVar ssaVar) {
InsnNode sputInsn = ssaVar.getUseList().get(0).getParentInsn(); InsnNode sputInsn = ssaVar.getUseList().get(0).getParentInsn();
if (sputInsn == null || sputInsn.getType() != InsnType.SPUT) { if (sputInsn == null || sputInsn.getType() != InsnType.SPUT) {
return null; return null;
} }
FieldInfo fieldInfo = (FieldInfo) ((IndexInsnNode) sputInsn).getIndex(); FieldInfo fieldInfo = (FieldInfo) ((IndexInsnNode) sputInsn).getIndex();
FieldNode enumFieldNode = cls.searchField(fieldInfo); FieldNode enumFieldNode = data.cls.searchField(fieldInfo);
if (enumFieldNode == null) { if (enumFieldNode == null) {
return null; return null;
} }
toRemove.add(sputInsn); data.toRemove.add(sputInsn);
return enumFieldNode; return enumFieldNode;
} }
@SuppressWarnings("StatementWithEmptyBody")
private EnumField createEnumFieldByConstructor(ClassNode cls, FieldNode enumFieldNode, ConstructorInsn co) { private EnumField createEnumFieldByConstructor(ClassNode cls, FieldNode enumFieldNode, ConstructorInsn co) {
// usually constructor signature is '<init>(Ljava/lang/String;I)V'. // usually constructor signature is '<init>(Ljava/lang/String;I)V'.
// sometimes for one field enum second arg can be omitted // sometimes for one field enum second arg can be omitted
@@ -399,31 +438,38 @@ public class EnumVisitor extends AbstractVisitor {
if (ctrMth == null) { if (ctrMth == null) {
return null; return null;
} }
List<RegisterArg> regs = new ArrayList<>();
co.getRegisterArgs(regs);
if (!regs.isEmpty()) {
throw new JadxRuntimeException("Init of enum " + enumFieldNode.getName() + " uses external variables");
}
return new EnumField(enumFieldNode, co); return new EnumField(enumFieldNode, co);
} }
@Nullable @Nullable
private InsnNode searchFieldPutInsn(ClassNode cls, BlockNode staticBlock, FieldNode enumFieldNode) { private InsnNode searchFieldPutInsn(EnumData data, FieldNode enumFieldNode) {
for (InsnNode sputInsn : staticBlock.getInstructions()) { for (BlockNode block : data.staticBlocks) {
if (sputInsn != null && sputInsn.getType() == InsnType.SPUT) { for (InsnNode sputInsn : block.getInstructions()) {
FieldInfo f = (FieldInfo) ((IndexInsnNode) sputInsn).getIndex(); if (sputInsn != null && sputInsn.getType() == InsnType.SPUT) {
FieldNode fieldNode = cls.searchField(f); FieldInfo f = (FieldInfo) ((IndexInsnNode) sputInsn).getIndex();
if (Objects.equals(fieldNode, enumFieldNode)) { FieldNode fieldNode = data.cls.searchField(f);
return sputInsn; if (Objects.equals(fieldNode, enumFieldNode)) {
return sputInsn;
}
} }
} }
} }
return null; return null;
} }
private void removeEnumMethods(ClassNode cls, ArgType clsType, FieldNode valuesField) { private void removeEnumMethods(ClassNode cls, FieldNode valuesField) {
String valuesMethod = "values()" + TypeGen.signature(ArgType.array(clsType)); ArgType clsType = cls.getClassInfo().getType();
FieldInfo valuesFieldInfo = valuesField.getFieldInfo(); String valuesMethodShortId = "values()" + TypeGen.signature(ArgType.array(clsType));
MethodNode valuesMethod = null;
// remove compiler generated methods // remove compiler generated methods
for (MethodNode mth : cls.getMethods()) { for (MethodNode mth : cls.getMethods()) {
MethodInfo mi = mth.getMethodInfo(); MethodInfo mi = mth.getMethodInfo();
if (mi.isClassInit()) { if (mi.isClassInit() || mth.isNoCode()) {
continue; continue;
} }
String shortId = mi.getShortId(); String shortId = mi.getShortId();
@@ -432,12 +478,33 @@ public class EnumVisitor extends AbstractVisitor {
mth.add(AFlag.DONT_GENERATE); mth.add(AFlag.DONT_GENERATE);
} }
markArgsForSkip(mth); markArgsForSkip(mth);
} else if (shortId.equals(valuesMethod) } else if (mi.getShortId().equals(valuesMethodShortId)) {
|| usesValuesField(mth, valuesFieldInfo) if (isValuesMethod(mth, clsType)) {
|| simpleValueOfMth(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); 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) { private void markArgsForSkip(MethodNode mth) {
@@ -458,6 +525,25 @@ public class EnumVisitor extends AbstractVisitor {
return false; 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) { private boolean simpleValueOfMth(MethodNode mth, ArgType clsType) {
InsnNode returnInsn = InsnUtils.searchSingleReturnInsn(mth, insn -> insn.getArgsCount() == 1); InsnNode returnInsn = InsnUtils.searchSingleReturnInsn(mth, insn -> insn.getArgsCount() == 1);
if (returnInsn == null) { if (returnInsn == null) {
@@ -472,9 +558,41 @@ public class EnumVisitor extends AbstractVisitor {
return false; 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); 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) { private static void processEnumCls(ClassNode cls, EnumField field, ClassNode innerCls) {
@@ -524,4 +642,19 @@ public class EnumVisitor extends AbstractVisitor {
} }
return null; return null;
} }
private static class EnumData {
final ClassNode cls;
final MethodNode classInitMth;
final List<BlockNode> staticBlocks;
final List<InsnNode> toRemove = new ArrayList<>();
FieldNode valuesField;
InsnNode valuesInitInsn;
public EnumData(ClassNode cls, MethodNode classInitMth, List<BlockNode> staticBlocks) {
this.cls = cls;
this.classInitMth = classInitMth;
this.staticBlocks = staticBlocks;
}
}
} }
@@ -29,6 +29,7 @@ import jadx.core.dex.nodes.MethodNode;
import jadx.core.dex.visitors.shrink.CodeShrinkVisitor; import jadx.core.dex.visitors.shrink.CodeShrinkVisitor;
import jadx.core.utils.BlockUtils; import jadx.core.utils.BlockUtils;
import jadx.core.utils.InsnRemover; import jadx.core.utils.InsnRemover;
import jadx.core.utils.ListUtils;
import jadx.core.utils.Utils; import jadx.core.utils.Utils;
import jadx.core.utils.exceptions.JadxException; import jadx.core.utils.exceptions.JadxException;
@@ -45,20 +46,22 @@ public class ExtractFieldInit extends AbstractVisitor {
for (ClassNode inner : cls.getInnerClasses()) { for (ClassNode inner : cls.getInnerClasses()) {
visit(inner); visit(inner);
} }
moveStaticFieldsInit(cls); if (!cls.getFields().isEmpty()) {
moveCommonFieldsInit(cls); moveStaticFieldsInit(cls);
moveCommonFieldsInit(cls);
}
return false; return false;
} }
private static final class FieldInitInfo { private static final class FieldInitInfo {
final FieldNode fieldNode; final FieldNode fieldNode;
final IndexInsnNode putInsn; final IndexInsnNode putInsn;
final boolean singlePath; final boolean canMove;
public FieldInitInfo(FieldNode fieldNode, IndexInsnNode putInsn, boolean singlePath) { public FieldInitInfo(FieldNode fieldNode, IndexInsnNode putInsn, boolean canMove) {
this.fieldNode = fieldNode; this.fieldNode = fieldNode;
this.putInsn = putInsn; this.putInsn = putInsn;
this.singlePath = singlePath; this.canMove = canMove;
} }
} }
@@ -80,6 +83,9 @@ public class ExtractFieldInit extends AbstractVisitor {
|| classInitMth.getBasicBlocks() == null) { || classInitMth.getBasicBlocks() == null) {
return; return;
} }
if (ListUtils.noneMatch(cls.getFields(), FieldNode::isStatic)) {
return;
}
while (processStaticFields(cls, classInitMth)) { while (processStaticFields(cls, classInitMth)) {
// sometimes instructions moved to field init prevent from vars inline -> inline and try again // sometimes instructions moved to field init prevent from vars inline -> inline and try again
CodeShrinkVisitor.shrinkMethod(classInitMth); CodeShrinkVisitor.shrinkMethod(classInitMth);
@@ -116,15 +122,15 @@ public class ExtractFieldInit extends AbstractVisitor {
} }
private static void moveCommonFieldsInit(ClassNode cls) { private static void moveCommonFieldsInit(ClassNode cls) {
if (ListUtils.noneMatch(cls.getFields(), FieldNode::isInstance)) {
return;
}
List<MethodNode> constructors = getConstructorsList(cls); List<MethodNode> constructors = getConstructorsList(cls);
if (constructors.isEmpty()) { if (constructors.isEmpty()) {
return; return;
} }
List<ConstructorInitInfo> infoList = new ArrayList<>(constructors.size()); List<ConstructorInitInfo> infoList = new ArrayList<>(constructors.size());
for (MethodNode constructorMth : constructors) { for (MethodNode constructorMth : constructors) {
if (constructorMth.isNoCode()) {
return;
}
List<FieldInitInfo> inits = collectFieldsInit(cls, constructorMth, InsnType.IPUT); List<FieldInitInfo> inits = collectFieldsInit(cls, constructorMth, InsnType.IPUT);
filterFieldsInit(inits); filterFieldsInit(inits);
if (inits.isEmpty()) { if (inits.isEmpty()) {
@@ -168,19 +174,25 @@ public class ExtractFieldInit extends AbstractVisitor {
Set<BlockNode> singlePathBlocks = new HashSet<>(); Set<BlockNode> singlePathBlocks = new HashSet<>();
BlockUtils.visitSinglePath(mth.getEnterBlock(), singlePathBlocks::add); BlockUtils.visitSinglePath(mth.getEnterBlock(), singlePathBlocks::add);
boolean canReorder = true;
for (BlockNode block : mth.getBasicBlocks()) { for (BlockNode block : mth.getBasicBlocks()) {
for (InsnNode insn : block.getInstructions()) { for (InsnNode insn : block.getInstructions()) {
boolean fieldInsn = false;
if (insn.getType() == putType) { if (insn.getType() == putType) {
IndexInsnNode putInsn = (IndexInsnNode) insn; IndexInsnNode putInsn = (IndexInsnNode) insn;
FieldInfo field = (FieldInfo) putInsn.getIndex(); FieldInfo field = (FieldInfo) putInsn.getIndex();
if (field.getDeclClass().equals(cls.getClassInfo())) { if (field.getDeclClass().equals(cls.getClassInfo())) {
FieldNode fn = cls.searchField(field); FieldNode fn = cls.searchField(field);
if (fn != null) { if (fn != null) {
boolean singlePath = singlePathBlocks.contains(block); boolean canMove = canReorder && singlePathBlocks.contains(block);
fieldsInit.add(new FieldInitInfo(fn, putInsn, singlePath)); fieldsInit.add(new FieldInitInfo(fn, putInsn, canMove));
fieldInsn = true;
} }
} }
} }
if (!fieldInsn && canReorder && !insn.canReorder()) {
canReorder = false;
}
} }
} }
return fieldsInit; return fieldsInit;
@@ -226,14 +238,14 @@ public class ExtractFieldInit extends AbstractVisitor {
} }
private static boolean checkInsn(FieldInitInfo initInfo) { private static boolean checkInsn(FieldInitInfo initInfo) {
if (!initInfo.singlePath) { if (!initInfo.canMove) {
return false; return false;
} }
IndexInsnNode insn = initInfo.putInsn; IndexInsnNode insn = initInfo.putInsn;
InsnArg arg = insn.getArg(0); InsnArg arg = insn.getArg(0);
if (arg.isInsnWrap()) { if (arg.isInsnWrap()) {
InsnNode wrapInsn = ((InsnWrapArg) arg).getWrapInsn(); InsnNode wrapInsn = ((InsnWrapArg) arg).getWrapInsn();
if (!wrapInsn.canReorderRecursive() && insn.contains(AType.EXC_CATCH)) { if (!wrapInsn.canReorder() && insn.contains(AType.EXC_CATCH)) {
return false; return false;
} }
} else { } else {
@@ -364,7 +376,7 @@ public class ExtractFieldInit extends AbstractVisitor {
AccessInfo accFlags = mth.getAccessFlags(); AccessInfo accFlags = mth.getAccessFlags();
if (!accFlags.isStatic() && accFlags.isConstructor()) { if (!accFlags.isStatic() && accFlags.isConstructor()) {
list.add(mth); list.add(mth);
if (BlockUtils.isAllBlocksEmpty(mth.getBasicBlocks())) { if (mth.isNoCode() || BlockUtils.isAllBlocksEmpty(mth.getBasicBlocks())) {
return Collections.emptyList(); return Collections.emptyList();
} }
} }
@@ -362,8 +362,7 @@ public class ModVisitor extends AbstractVisitor {
private static void removeCheckCast(MethodNode mth, BlockNode block, int i, IndexInsnNode insn) { private static void removeCheckCast(MethodNode mth, BlockNode block, int i, IndexInsnNode insn) {
InsnArg castArg = insn.getArg(0); InsnArg castArg = insn.getArg(0);
ArgType castType = (ArgType) insn.getIndex(); ArgType castType = (ArgType) insn.getIndex();
if (!ArgType.isCastNeeded(mth.root(), castArg.getType(), castType) if (!ArgType.isCastNeeded(mth.root(), castArg.getType(), castType)) {
|| isCastDuplicate(insn)) {
RegisterArg result = insn.getResult(); RegisterArg result = insn.getResult();
result.setType(castArg.getType()); result.setType(castArg.getType());
@@ -371,10 +370,19 @@ public class ModVisitor extends AbstractVisitor {
move.setResult(result); move.setResult(result);
move.addArg(castArg); move.addArg(castArg);
replaceInsn(mth, block, i, move); 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); InsnArg arg = castInsn.getArg(0);
if (arg.isRegister()) { if (arg.isRegister()) {
SSAVar sVar = ((RegisterArg) arg).getSVar(); SSAVar sVar = ((RegisterArg) arg).getSVar();
@@ -382,11 +390,13 @@ public class ModVisitor extends AbstractVisitor {
InsnNode assignInsn = sVar.getAssign().getParentInsn(); InsnNode assignInsn = sVar.getAssign().getParentInsn();
if (assignInsn != null && assignInsn.getType() == InsnType.CHECK_CAST) { if (assignInsn != null && assignInsn.getType() == InsnType.CHECK_CAST) {
ArgType assignCastType = (ArgType) ((IndexInsnNode) assignInsn).getIndex(); ArgType assignCastType = (ArgType) ((IndexInsnNode) assignInsn).getIndex();
return assignCastType.equals(castInsn.getIndex()); if (assignCastType.equals(castInsn.getIndex())) {
return assignInsn;
}
} }
} }
} }
return false; return null;
} }
/** /**
@@ -222,6 +222,10 @@ public class ProcessAnonymous extends AbstractVisitor {
// exclude self usage // exclude self usage
return null; return null;
} }
if (ctrUseCls.getTopParentClass().equals(cls)) {
// exclude usage inside inner classes
return null;
}
for (MethodNode mth : cls.getMethods()) { for (MethodNode mth : cls.getMethods()) {
if (mth == ctr) { if (mth == ctr) {
continue; continue;
@@ -1,12 +1,12 @@
package jadx.core.dex.visitors; package jadx.core.dex.visitors;
import jadx.core.Consts;
import jadx.core.dex.attributes.AFlag; import jadx.core.dex.attributes.AFlag;
import jadx.core.dex.info.AccessInfo; import jadx.core.dex.info.AccessInfo;
import jadx.core.dex.nodes.ClassNode; import jadx.core.dex.nodes.ClassNode;
import jadx.core.dex.nodes.MethodNode; import jadx.core.dex.nodes.MethodNode;
import jadx.core.dex.nodes.RootNode; import jadx.core.dex.nodes.RootNode;
import jadx.core.dex.visitors.usage.UsageInfoVisitor; import jadx.core.dex.visitors.usage.UsageInfoVisitor;
import jadx.core.utils.ListUtils;
import jadx.core.utils.exceptions.JadxException; import jadx.core.utils.exceptions.JadxException;
@JadxVisitor( @JadxVisitor(
@@ -45,7 +45,14 @@ public class ProcessMethodsForInline extends AbstractVisitor {
} }
AccessInfo accessFlags = mth.getAccessFlags(); AccessInfo accessFlags = mth.getAccessFlags();
boolean isSynthetic = accessFlags.isSynthetic() || mth.getName().contains("$"); boolean isSynthetic = accessFlags.isSynthetic() || mth.getName().contains("$");
return isSynthetic && (accessFlags.isStatic() || mth.isConstructor()); return isSynthetic && canInlineMethod(mth, accessFlags);
}
private static boolean canInlineMethod(MethodNode mth, AccessInfo accessFlags) {
if (accessFlags.isStatic()) {
return true;
}
return mth.isConstructor() && mth.root().getArgs().isInlineAnonymousClasses();
} }
private static void fixClassDependencies(MethodNode mth) { private static void fixClassDependencies(MethodNode mth) {
@@ -54,8 +61,14 @@ public class ProcessMethodsForInline extends AbstractVisitor {
// remove possible cross dependency // remove possible cross dependency
// to force class with inline method to be processed before its usage // to force class with inline method to be processed before its usage
ClassNode useTopCls = useInMth.getTopParentClass(); ClassNode useTopCls = useInMth.getTopParentClass();
parentClass.setDependencies(ListUtils.safeRemoveAndTrim(parentClass.getDependencies(), useTopCls)); if (useTopCls != parentClass) {
useTopCls.addCodegenDep(parentClass); parentClass.removeDependency(useTopCls);
useTopCls.addCodegenDep(parentClass);
if (Consts.DEBUG_USAGE) {
parentClass.addDebugComment("Remove dependency: " + useTopCls + " to inline " + mth);
useTopCls.addDebugComment("Add dependency: " + parentClass + " to inline " + mth);
}
}
} }
} }
} }
@@ -50,7 +50,7 @@ public class BlockExceptionHandler {
return false; return false;
} }
BlockProcessor.updateCleanSuccessors(mth); BlockProcessor.updateCleanSuccessors(mth);
BlockProcessor.computeDominanceFrontier(mth); DominatorTree.computeDominanceFrontier(mth);
processCatchAttr(mth); processCatchAttr(mth);
initExcHandlers(mth); initExcHandlers(mth);
@@ -171,10 +171,6 @@ public class BlockExceptionHandler {
} }
} }
protected static void removeTmpConnections(MethodNode mth) {
mth.getBasicBlocks().forEach(BlockExceptionHandler::removeTmpConnection);
}
private static void removeTmpConnection(BlockNode block) { private static void removeTmpConnection(BlockNode block) {
TmpEdgeAttr tmpEdgeAttr = block.get(AType.TMP_EDGE); TmpEdgeAttr tmpEdgeAttr = block.get(AType.TMP_EDGE);
if (tmpEdgeAttr != null) { if (tmpEdgeAttr != null) {
@@ -402,6 +398,13 @@ public class BlockExceptionHandler {
} }
BlockNode topDom = BlockUtils.getCommonDominator(mth, blocks); BlockNode topDom = BlockUtils.getCommonDominator(mth, blocks);
if (topDom != null) { if (topDom != null) {
// dominator always return one up block if blocks already contains dominator, use successor instead
if (topDom.getSuccessors().size() == 1) {
BlockNode upBlock = topDom.getSuccessors().get(0);
if (blocks.contains(upBlock)) {
return upBlock;
}
}
return adjustTopBlock(topDom); return adjustTopBlock(topDom);
} }
throw new JadxRuntimeException("Failed to find top block for try-catch from: " + blocks); throw new JadxRuntimeException("Failed to find top block for try-catch from: " + blocks);
@@ -549,7 +552,7 @@ public class BlockExceptionHandler {
if (handler == resultHandler) { if (handler == resultHandler) {
return false; return false;
} }
resultHandler.addCatchTypes(handler.getCatchTypes()); resultHandler.addCatchTypes(mth, handler.getCatchTypes());
handler.markForRemove(); handler.markForRemove();
return true; return true;
}); });
@@ -1,11 +1,7 @@
package jadx.core.dex.visitors.blocks; package jadx.core.dex.visitors.blocks;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.BitSet;
import java.util.Collections;
import java.util.Deque;
import java.util.LinkedHashSet; import java.util.LinkedHashSet;
import java.util.LinkedList;
import java.util.List; import java.util.List;
import java.util.Set; import java.util.Set;
@@ -31,7 +27,6 @@ import jadx.core.utils.Utils;
import jadx.core.utils.exceptions.JadxRuntimeException; import jadx.core.utils.exceptions.JadxRuntimeException;
import static jadx.core.dex.visitors.blocks.BlockSplitter.connect; import static jadx.core.dex.visitors.blocks.BlockSplitter.connect;
import static jadx.core.utils.EmptyBitSet.EMPTY;
public class BlockProcessor extends AbstractVisitor { public class BlockProcessor extends AbstractVisitor {
private static final Logger LOG = LoggerFactory.getLogger(BlockProcessor.class); private static final Logger LOG = LoggerFactory.getLogger(BlockProcessor.class);
@@ -50,29 +45,23 @@ public class BlockProcessor extends AbstractVisitor {
computeDominators(mth); computeDominators(mth);
if (independentBlockTreeMod(mth)) { if (independentBlockTreeMod(mth)) {
checkForUnreachableBlocks(mth); checkForUnreachableBlocks(mth);
clearBlocksState(mth);
computeDominators(mth); computeDominators(mth);
} }
if (FixMultiEntryLoops.process(mth)) { if (FixMultiEntryLoops.process(mth)) {
clearBlocksState(mth);
computeDominators(mth); computeDominators(mth);
} }
updateCleanSuccessors(mth); updateCleanSuccessors(mth);
int i = 0; int i = 0;
while (modifyBlocksTree(mth)) { while (modifyBlocksTree(mth)) {
// revert calculations
clearBlocksState(mth);
// recalculate dominators tree
computeDominators(mth); computeDominators(mth);
if (i++ > 100) { if (i++ > 100) {
throw new JadxRuntimeException("CFG modification limit reached, blocks count: " + mth.getBasicBlocks().size()); throw new JadxRuntimeException("CFG modification limit reached, blocks count: " + mth.getBasicBlocks().size());
} }
} }
checkForUnreachableBlocks(mth); checkForUnreachableBlocks(mth);
computeDominanceFrontier(mth); DominatorTree.computeDominanceFrontier(mth);
registerLoops(mth); registerLoops(mth);
processNestedLoops(mth); processNestedLoops(mth);
@@ -209,139 +198,9 @@ public class BlockProcessor extends AbstractVisitor {
} }
private static void computeDominators(MethodNode mth) { private static void computeDominators(MethodNode mth) {
List<BlockNode> basicBlocks = mth.getBasicBlocks(); clearBlocksState(mth);
int nBlocks = basicBlocks.size(); DominatorTree.compute(mth);
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);
markLoops(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) { private static void markLoops(MethodNode mth) {
@@ -349,7 +208,7 @@ public class BlockProcessor extends AbstractVisitor {
// Every successor that dominates its predecessor is a header of a loop, // Every successor that dominates its predecessor is a header of a loop,
// block -> successor is a back edge. // block -> successor is a back edge.
block.getSuccessors().forEach(successor -> { block.getSuccessors().forEach(successor -> {
if (block.getDoms().get(successor.getId())) { if (block.getDoms().get(successor.getId()) || block == successor) {
successor.add(AFlag.LOOP_START); successor.add(AFlag.LOOP_START);
block.add(AFlag.LOOP_END); block.add(AFlag.LOOP_END);
@@ -572,7 +431,7 @@ public class BlockProcessor extends AbstractVisitor {
BlockNode loopHeader = loop.getStart(); BlockNode loopHeader = loop.getStart();
List<BlockNode> preds = loopHeader.getPredecessors(); List<BlockNode> preds = loopHeader.getPredecessors();
if (preds.size() > 2) { if (preds.size() > 2) {
List<BlockNode> blocks = new LinkedList<>(preds); List<BlockNode> blocks = new ArrayList<>(preds);
blocks.removeIf(block -> block.contains(AFlag.LOOP_END)); blocks.removeIf(block -> block.contains(AFlag.LOOP_END));
BlockNode first = blocks.remove(0); BlockNode first = blocks.remove(0);
BlockNode preHeader = BlockSplitter.insertBlockBetween(mth, first, loopHeader); BlockNode preHeader = BlockSplitter.insertBlockBetween(mth, first, loopHeader);
@@ -137,8 +137,9 @@ public class BlockSplitter extends AbstractVisitor {
} }
static BlockNode startNewBlock(MethodNode mth, int offset) { static BlockNode startNewBlock(MethodNode mth, int offset) {
BlockNode block = new BlockNode(mth.getBasicBlocks().size(), offset); List<BlockNode> blocks = mth.getBasicBlocks();
mth.getBasicBlocks().add(block); BlockNode block = new BlockNode(mth.getNextBlockCId(), blocks.size(), offset);
blocks.add(block);
return block; return block;
} }
@@ -391,7 +392,8 @@ public class BlockSplitter extends AbstractVisitor {
&& block.getSuccessors().size() <= 1 && block.getSuccessors().size() <= 1
&& !block.getPredecessors().isEmpty() && !block.getPredecessors().isEmpty()
&& !block.contains(AFlag.MTH_ENTER_BLOCK) && !block.contains(AFlag.MTH_ENTER_BLOCK)
&& !block.contains(AFlag.MTH_EXIT_BLOCK); && !block.contains(AFlag.MTH_EXIT_BLOCK)
&& !block.getSuccessors().contains(block); // no self loop
} }
static void collectSuccessors(BlockNode startBlock, BlockNode methodEnterBlock, Set<BlockNode> toRemove) { static void collectSuccessors(BlockNode startBlock, BlockNode methodEnterBlock, Set<BlockNode> toRemove) {
@@ -0,0 +1,168 @@
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);
}
private static List<BlockNode> sortBlocks(MethodNode mth) {
int blocksCount = mth.getBasicBlocks().size();
List<BlockNode> sorted = new ArrayList<>(blocksCount);
BlockUtils.dfsVisit(mth, sorted::add);
if (sorted.size() != blocksCount) {
throw new JadxRuntimeException("Found unreachable blocks");
}
mth.setBasicBlocks(sorted);
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,13 @@ import java.util.List;
import java.util.Set; import java.util.Set;
import jadx.core.dex.nodes.BlockNode; import jadx.core.dex.nodes.BlockNode;
import jadx.core.dex.nodes.InsnNode;
import jadx.core.dex.nodes.MethodNode;
import jadx.core.dex.trycatch.ExceptionHandler; import jadx.core.dex.trycatch.ExceptionHandler;
import jadx.core.utils.Utils;
public class FinallyExtractInfo { public class FinallyExtractInfo {
private final MethodNode mth;
private final ExceptionHandler finallyHandler; private final ExceptionHandler finallyHandler;
private final List<BlockNode> allHandlerBlocks; private final List<BlockNode> allHandlerBlocks;
private final List<InsnsSlice> duplicateSlices = new ArrayList<>(); private final List<InsnsSlice> duplicateSlices = new ArrayList<>();
@@ -16,12 +20,21 @@ public class FinallyExtractInfo {
private final InsnsSlice finallyInsnsSlice = new InsnsSlice(); private final InsnsSlice finallyInsnsSlice = new InsnsSlice();
private final BlockNode startBlock; private final BlockNode startBlock;
public FinallyExtractInfo(ExceptionHandler finallyHandler, BlockNode startBlock, List<BlockNode> allHandlerBlocks) { private InsnsSlice curDupSlice;
private List<InsnNode> curDupInsns;
private int curDupInsnsOffset;
public FinallyExtractInfo(MethodNode mth, ExceptionHandler finallyHandler, BlockNode startBlock, List<BlockNode> allHandlerBlocks) {
this.mth = mth;
this.finallyHandler = finallyHandler; this.finallyHandler = finallyHandler;
this.startBlock = startBlock; this.startBlock = startBlock;
this.allHandlerBlocks = allHandlerBlocks; this.allHandlerBlocks = allHandlerBlocks;
} }
public MethodNode getMth() {
return mth;
}
public ExceptionHandler getFinallyHandler() { public ExceptionHandler getFinallyHandler() {
return finallyHandler; return finallyHandler;
} }
@@ -45,4 +58,33 @@ public class FinallyExtractInfo {
public BlockNode getStartBlock() { public BlockNode getStartBlock() {
return startBlock; return startBlock;
} }
public InsnsSlice getCurDupSlice() {
return curDupSlice;
}
public void setCurDupSlice(InsnsSlice curDupSlice) {
this.curDupSlice = curDupSlice;
}
public List<InsnNode> getCurDupInsns() {
return curDupInsns;
}
public int getCurDupInsnsOffset() {
return curDupInsnsOffset;
}
public void setCurDupInsns(List<InsnNode> insns, int offset) {
this.curDupInsns = insns;
this.curDupInsnsOffset = offset;
}
@Override
public String toString() {
return "FinallyExtractInfo{"
+ "\n finally:\n " + finallyInsnsSlice
+ "\n dups:\n " + Utils.listToString(duplicateSlices, "\n ")
+ "\n}";
}
} }
@@ -9,8 +9,10 @@ import org.jetbrains.annotations.Nullable;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import jadx.core.Consts;
import jadx.core.dex.attributes.AFlag; import jadx.core.dex.attributes.AFlag;
import jadx.core.dex.attributes.AType; import jadx.core.dex.attributes.AType;
import jadx.core.dex.attributes.nodes.RegDebugInfoAttr;
import jadx.core.dex.instructions.InsnType; import jadx.core.dex.instructions.InsnType;
import jadx.core.dex.instructions.args.InsnArg; import jadx.core.dex.instructions.args.InsnArg;
import jadx.core.dex.instructions.args.RegisterArg; import jadx.core.dex.instructions.args.RegisterArg;
@@ -27,6 +29,7 @@ import jadx.core.dex.visitors.IDexTreeVisitor;
import jadx.core.dex.visitors.JadxVisitor; import jadx.core.dex.visitors.JadxVisitor;
import jadx.core.dex.visitors.ssa.SSATransform; import jadx.core.dex.visitors.ssa.SSATransform;
import jadx.core.utils.BlockUtils; import jadx.core.utils.BlockUtils;
import jadx.core.utils.InsnList;
import jadx.core.utils.ListUtils; import jadx.core.utils.ListUtils;
import jadx.core.utils.Utils; import jadx.core.utils.Utils;
@@ -38,6 +41,7 @@ import jadx.core.utils.Utils;
) )
public class MarkFinallyVisitor extends AbstractVisitor { public class MarkFinallyVisitor extends AbstractVisitor {
private static final Logger LOG = LoggerFactory.getLogger(MarkFinallyVisitor.class); private static final Logger LOG = LoggerFactory.getLogger(MarkFinallyVisitor.class);
// private static final Logger LOG = LoggerFactory.getLogger(MarkFinallyVisitor.class);
@Override @Override
public void visit(MethodNode mth) { public void visit(MethodNode mth) {
@@ -60,7 +64,7 @@ public class MarkFinallyVisitor extends AbstractVisitor {
} }
} }
} catch (Exception e) { } catch (Exception e) {
LOG.warn("Undo finally extract visitor, mth: {}", mth, e); mth.addWarnComment("Undo finally extract visitor", e);
undoFinallyVisitor(mth); undoFinallyVisitor(mth);
} }
} }
@@ -100,20 +104,23 @@ public class MarkFinallyVisitor extends AbstractVisitor {
List<BlockNode> handlerBlocks = List<BlockNode> handlerBlocks =
new ArrayList<>(BlockUtils.collectBlocksDominatedByWithExcHandlers(mth, handlerBlock, handlerBlock)); new ArrayList<>(BlockUtils.collectBlocksDominatedByWithExcHandlers(mth, handlerBlock, handlerBlock));
handlerBlocks.remove(handlerBlock); // exclude block with 'move-exception' 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)) { if (handlerBlocks.isEmpty() || BlockUtils.isAllBlocksEmpty(handlerBlocks)) {
// remove empty catch // remove empty catch
allHandler.getTryBlock().removeHandler(allHandler); allHandler.getTryBlock().removeHandler(allHandler);
return true; return true;
} }
BlockNode startBlock = Utils.getOne(handlerBlock.getCleanSuccessors()); 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(); boolean hasInnerBlocks = !tryBlock.getInnerTryBlocks().isEmpty();
List<ExceptionHandler> handlers; List<ExceptionHandler> handlers;
if (hasInnerBlocks) { if (hasInnerBlocks) {
// collect handlers from this and all inner blocks (intentionally not using recursive collect for // collect handlers from this and all inner blocks
// now) // (intentionally not using recursive collect for now)
handlers = new ArrayList<>(tryBlock.getHandlers()); handlers = new ArrayList<>(tryBlock.getHandlers());
for (TryCatchBlockAttr innerTryBlock : tryBlock.getInnerTryBlocks()) { for (TryCatchBlockAttr innerTryBlock : tryBlock.getInnerTryBlocks()) {
handlers.addAll(innerTryBlock.getHandlers()); handlers.addAll(innerTryBlock.getHandlers());
@@ -137,10 +144,12 @@ public class MarkFinallyVisitor extends AbstractVisitor {
} }
} }
} }
if (Consts.DEBUG_FINALLY) {
LOG.debug("Handlers slices:\n{}", extractInfo);
}
boolean mergeInnerTryBlocks; boolean mergeInnerTryBlocks;
int duplicatesCount = extractInfo.getDuplicateSlices().size(); int duplicatesCount = extractInfo.getDuplicateSlices().size();
boolean fullTryBlock = duplicatesCount == (handlers.size() - 1); if (duplicatesCount == (handlers.size() - 1)) {
if (fullTryBlock) {
// all collected handlers have duplicate block // all collected handlers have duplicate block
mergeInnerTryBlocks = hasInnerBlocks; mergeInnerTryBlocks = hasInnerBlocks;
} else { } else {
@@ -170,15 +179,24 @@ public class MarkFinallyVisitor extends AbstractVisitor {
if (upPath.size() < handlerBlocks.size()) { if (upPath.size() < handlerBlocks.size()) {
continue; continue;
} }
if (Consts.DEBUG_FINALLY) {
LOG.debug("Checking dup path starts: {} from {}", upPath, pred);
}
for (BlockNode block : upPath) { for (BlockNode block : upPath) {
if (searchDuplicateInsns(block, extractInfo)) { if (searchDuplicateInsns(block, extractInfo)) {
found = true; found = true;
if (Consts.DEBUG_FINALLY) {
LOG.debug("Found dup in: {} from {}", block, pred);
}
break; break;
} else { } else {
extractInfo.getFinallyInsnsSlice().resetIncomplete(); extractInfo.getFinallyInsnsSlice().resetIncomplete();
} }
} }
} }
if (Consts.DEBUG_FINALLY) {
LOG.debug("Result slices:\n{}", extractInfo);
}
if (!found) { if (!found) {
return false; return false;
} }
@@ -204,6 +222,28 @@ public class MarkFinallyVisitor extends AbstractVisitor {
return true; 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) { private static List<BlockNode> getPathStarts(MethodNode mth, BlockNode bottom, BlockNode bottomFinallyBlock) {
Stream<BlockNode> preds = bottom.getPredecessors().stream().filter(b -> b != bottomFinallyBlock); Stream<BlockNode> preds = bottom.getPredecessors().stream().filter(b -> b != bottomFinallyBlock);
if (bottom == mth.getExitBlock()) { if (bottom == mth.getExitBlock()) {
@@ -219,9 +259,8 @@ public class MarkFinallyVisitor extends AbstractVisitor {
for (InsnsSlice dupSlice : extractInfo.getDuplicateSlices()) { for (InsnsSlice dupSlice : extractInfo.getDuplicateSlices()) {
List<InsnNode> dupInsnsList = dupSlice.getInsnsList(); List<InsnNode> dupInsnsList = dupSlice.getInsnsList();
if (dupInsnsList.size() != finallyInsnsList.size()) { if (dupInsnsList.size() != finallyInsnsList.size()) {
if (LOG.isDebugEnabled()) { extractInfo.getMth().addDebugComment(
LOG.debug("Incorrect finally slice size: {}, expected: {}", dupSlice, finallySlice); "Incorrect finally slice size: " + dupSlice + ", expected: " + finallySlice);
}
return false; return false;
} }
} }
@@ -231,9 +270,8 @@ public class MarkFinallyVisitor extends AbstractVisitor {
List<InsnNode> insnsList = dupSlice.getInsnsList(); List<InsnNode> insnsList = dupSlice.getInsnsList();
InsnNode dupInsn = insnsList.get(i); InsnNode dupInsn = insnsList.get(i);
if (finallyInsn.getType() != dupInsn.getType()) { if (finallyInsn.getType() != dupInsn.getType()) {
if (LOG.isDebugEnabled()) { extractInfo.getMth().addDebugComment(
LOG.debug("Incorrect finally slice insn: {}, expected: {}", dupInsn, finallyInsn); "Incorrect finally slice insn: " + dupInsn + ", expected: " + finallyInsn);
}
return false; return false;
} }
} }
@@ -340,26 +378,32 @@ public class MarkFinallyVisitor extends AbstractVisitor {
* 'Finally' instructions can start in the middle of the first block. * 'Finally' instructions can start in the middle of the first block.
*/ */
private static InsnsSlice isStartBlock(BlockNode dupBlock, BlockNode finallyBlock, FinallyExtractInfo extractInfo) { private static InsnsSlice isStartBlock(BlockNode dupBlock, BlockNode finallyBlock, FinallyExtractInfo extractInfo) {
extractInfo.setCurDupSlice(null);
List<InsnNode> dupInsns = dupBlock.getInstructions(); List<InsnNode> dupInsns = dupBlock.getInstructions();
List<InsnNode> finallyInsns = finallyBlock.getInstructions(); List<InsnNode> finallyInsns = finallyBlock.getInstructions();
if (dupInsns.size() < finallyInsns.size()) { int dupSize = dupInsns.size();
int finSize = finallyInsns.size();
if (dupSize < finSize) {
return null; return null;
} }
int startPos = dupInsns.size() - finallyInsns.size(); int startPos;
int endPos = 0; int endPos = 0;
// fast check from end of block if (dupSize == finSize) {
if (!checkInsns(dupInsns, finallyInsns, startPos)) { if (!checkInsns(extractInfo, dupInsns, finallyInsns, 0)) {
// check from block start return null;
if (checkInsns(dupInsns, finallyInsns, 0)) { }
startPos = 0; startPos = 0;
endPos = finallyInsns.size(); } else {
} else { // dupSize > finSize
startPos = dupSize - finSize;
// fast check from end of block
if (!checkInsns(extractInfo, dupInsns, finallyInsns, startPos)) {
// search start insn // search start insn
boolean found = false; boolean found = false;
for (int i = 1; i < startPos; i++) { for (int i = 1; i < startPos; i++) {
if (checkInsns(dupInsns, finallyInsns, i)) { if (checkInsns(extractInfo, dupInsns, finallyInsns, i)) {
startPos = i; startPos = i;
endPos = finallyInsns.size() + i; endPos = finSize + i;
found = true; found = true;
break; break;
} }
@@ -373,13 +417,14 @@ public class MarkFinallyVisitor extends AbstractVisitor {
// put instructions into slices // put instructions into slices
boolean complete; boolean complete;
InsnsSlice slice = new InsnsSlice(); InsnsSlice slice = new InsnsSlice();
extractInfo.setCurDupSlice(slice);
int endIndex; int endIndex;
if (endPos != 0) { if (endPos != 0) {
endIndex = endPos + 1; endIndex = endPos + 1;
// both slices completed // both slices completed
complete = true; complete = true;
} else { } else {
endIndex = dupInsns.size(); endIndex = dupSize;
complete = false; complete = false;
} }
@@ -393,9 +438,8 @@ public class MarkFinallyVisitor extends AbstractVisitor {
if (finallySlice.isComplete()) { if (finallySlice.isComplete()) {
// compare slices // compare slices
if (finallySlice.getInsnsList().size() != slice.getInsnsList().size()) { if (finallySlice.getInsnsList().size() != slice.getInsnsList().size()) {
if (LOG.isDebugEnabled()) { extractInfo.getMth().addDebugComment(
LOG.debug("Another duplicated slice has different insns count: {}, finally: {}", slice, finallySlice); "Another duplicated slice has different insns count: " + slice + ", finally: " + finallySlice);
}
return null; return null;
} }
// TODO: add additional slices checks // TODO: add additional slices checks
@@ -413,11 +457,12 @@ public class MarkFinallyVisitor extends AbstractVisitor {
return slice; return slice;
} }
private static boolean checkInsns(List<InsnNode> remInsns, List<InsnNode> finallyInsns, int delta) { private static boolean checkInsns(FinallyExtractInfo extractInfo, List<InsnNode> dupInsns, List<InsnNode> finallyInsns, int delta) {
extractInfo.setCurDupInsns(dupInsns, delta);
for (int i = finallyInsns.size() - 1; i >= 0; i--) { for (int i = finallyInsns.size() - 1; i >= 0; i--) {
InsnNode startInsn = finallyInsns.get(i); InsnNode startInsn = finallyInsns.get(i);
InsnNode remInsn = remInsns.get(delta + i); InsnNode dupInsn = dupInsns.get(delta + i);
if (!sameInsns(remInsn, startInsn)) { if (!sameInsns(extractInfo, dupInsn, startInsn)) {
return false; return false;
} }
} }
@@ -469,8 +514,9 @@ public class MarkFinallyVisitor extends AbstractVisitor {
if (dupInsnCount < finallyInsnCount) { if (dupInsnCount < finallyInsnCount) {
return false; return false;
} }
extractInfo.setCurDupInsns(dupInsns, 0);
for (int i = 0; i < finallyInsnCount; i++) { for (int i = 0; i < finallyInsnCount; i++) {
if (!sameInsns(dupInsns.get(i), finallyInsns.get(i))) { if (!sameInsns(extractInfo, dupInsns.get(i), finallyInsns.get(i))) {
return false; return false;
} }
} }
@@ -484,26 +530,85 @@ public class MarkFinallyVisitor extends AbstractVisitor {
return true; return true;
} }
private static boolean sameInsns(InsnNode remInsn, InsnNode fInsn) { private static boolean sameInsns(FinallyExtractInfo extractInfo, InsnNode dupInsn, InsnNode fInsn) {
if (!remInsn.isSame(fInsn)) { if (!dupInsn.isSame(fInsn)) {
return false; return false;
} }
// TODO: check instance arg in ConstructorInsn // TODO: check instance arg in ConstructorInsn
// TODO: compare literals for (int i = 0; i < dupInsn.getArgsCount(); i++) {
for (int i = 0; i < remInsn.getArgsCount(); i++) { InsnArg dupArg = dupInsn.getArg(i);
InsnArg remArg = remInsn.getArg(i);
InsnArg fArg = fInsn.getArg(i); InsnArg fArg = fInsn.getArg(i);
if (remArg.isRegister() != fArg.isRegister()) { if (!isSameArgs(extractInfo, dupArg, fArg)) {
return false; return false;
} }
boolean remConst = remArg.isConst(); }
if (remConst != fArg.isConst()) { return true;
return false; }
}
if (remConst && !remArg.isSameConst(fArg)) { @SuppressWarnings("RedundantIfStatement")
private static boolean isSameArgs(FinallyExtractInfo extractInfo, InsnArg dupArg, InsnArg fArg) {
boolean isReg = dupArg.isRegister();
if (isReg != fArg.isRegister()) {
return false;
}
if (isReg) {
RegisterArg dupReg = (RegisterArg) dupArg;
RegisterArg fReg = (RegisterArg) fArg;
if (!dupReg.sameCodeVar(fReg)
&& !sameDebugInfo(dupReg, fReg)
&& assignedOutsideHandler(extractInfo, dupReg, fReg)
&& assignInsnDifferent(dupReg, fReg)) {
return false; return false;
} }
} }
boolean remConst = dupArg.isConst();
if (remConst != fArg.isConst()) {
return false;
}
if (remConst && !dupArg.isSameConst(fArg)) {
return false;
}
return true;
}
private static boolean sameDebugInfo(RegisterArg dupReg, RegisterArg fReg) {
RegDebugInfoAttr fDbgInfo = fReg.get(AType.REG_DEBUG_INFO);
RegDebugInfoAttr dupDbgInfo = dupReg.get(AType.REG_DEBUG_INFO);
if (fDbgInfo == null || dupDbgInfo == null) {
return false;
}
return dupDbgInfo.equals(fDbgInfo);
}
private static boolean assignInsnDifferent(RegisterArg dupReg, RegisterArg fReg) {
InsnNode assignInsn = fReg.getAssignInsn();
InsnNode dupAssign = dupReg.getAssignInsn();
if (assignInsn == null || dupAssign == null) {
return true;
}
if (!assignInsn.isSame(dupAssign)) {
return true;
}
if (assignInsn.isConstInsn() && dupAssign.isConstInsn()) {
return !assignInsn.isDeepEquals(dupAssign);
}
return false;
}
@SuppressWarnings("RedundantIfStatement")
private static boolean assignedOutsideHandler(FinallyExtractInfo extractInfo, RegisterArg dupReg, RegisterArg fReg) {
if (InsnList.contains(extractInfo.getFinallyInsnsSlice().getInsnsList(), fReg.getAssignInsn())) {
return false;
}
InsnNode dupAssign = dupReg.getAssignInsn();
InsnsSlice curDupSlice = extractInfo.getCurDupSlice();
if (curDupSlice != null && InsnList.contains(curDupSlice.getInsnsList(), dupAssign)) {
return false;
}
List<InsnNode> curDupInsns = extractInfo.getCurDupInsns();
if (Utils.notEmpty(curDupInsns) && InsnList.contains(curDupInsns, dupAssign, extractInfo.getCurDupInsnsOffset())) {
return false;
}
return true; return true;
} }
@@ -522,7 +627,7 @@ public class MarkFinallyVisitor extends AbstractVisitor {
DepthTraversal.visit(visitor, mth); DepthTraversal.visit(visitor, mth);
} }
} catch (Exception e) { } catch (Exception e) {
LOG.error("Undo finally extract failed, mth: {}", mth, e); mth.addError("Undo finally extract failed", e);
} }
} }
} }
@@ -8,6 +8,7 @@ import jadx.core.dex.nodes.IContainer;
import jadx.core.dex.nodes.IRegion; import jadx.core.dex.nodes.IRegion;
import jadx.core.dex.nodes.MethodNode; import jadx.core.dex.nodes.MethodNode;
import jadx.core.dex.regions.Region; import jadx.core.dex.regions.Region;
import jadx.core.dex.regions.loops.LoopRegion;
import jadx.core.dex.visitors.AbstractVisitor; import jadx.core.dex.visitors.AbstractVisitor;
public class CleanRegions extends AbstractVisitor { public class CleanRegions extends AbstractVisitor {
@@ -42,6 +43,13 @@ public class CleanRegions extends AbstractVisitor {
BlockNode block = (BlockNode) container; BlockNode block = (BlockNode) container;
return block.getInstructions().isEmpty(); return block.getInstructions().isEmpty();
} }
if (container instanceof LoopRegion) {
LoopRegion loopRegion = (LoopRegion) container;
if (loopRegion.isEndless()) {
// keep empty endless loops
return false;
}
}
if (container instanceof IRegion) { if (container instanceof IRegion) {
List<IContainer> subBlocks = ((IRegion) container).getSubBlocks(); List<IContainer> subBlocks = ((IRegion) container).getSubBlocks();
for (IContainer subBlock : subBlocks) { for (IContainer subBlock : subBlocks) {
@@ -19,6 +19,10 @@ public class DepthRegionTraversal {
traverseInternal(mth, visitor, mth.getRegion()); traverseInternal(mth, visitor, mth.getRegion());
} }
public static void traverse(MethodNode mth, IContainer container, IRegionVisitor visitor) {
traverseInternal(mth, visitor, container);
}
public static void traverseIterative(MethodNode mth, IRegionIterativeVisitor visitor) { public static void traverseIterative(MethodNode mth, IRegionIterativeVisitor visitor) {
boolean repeat; boolean repeat;
int k = 0; int k = 0;
@@ -3,8 +3,10 @@ package jadx.core.dex.visitors.regions;
import java.util.List; import java.util.List;
import jadx.core.dex.attributes.AFlag; import jadx.core.dex.attributes.AFlag;
import jadx.core.dex.instructions.InsnType;
import jadx.core.dex.nodes.IContainer; import jadx.core.dex.nodes.IContainer;
import jadx.core.dex.nodes.IRegion; import jadx.core.dex.nodes.IRegion;
import jadx.core.dex.nodes.InsnNode;
import jadx.core.dex.nodes.MethodNode; import jadx.core.dex.nodes.MethodNode;
import jadx.core.dex.regions.Region; import jadx.core.dex.regions.Region;
import jadx.core.dex.regions.conditions.IfCondition; import jadx.core.dex.regions.conditions.IfCondition;
@@ -45,7 +47,7 @@ public class IfRegionVisitor extends AbstractVisitor {
} }
} }
@SuppressWarnings("UnnecessaryReturnStatement") @SuppressWarnings({ "UnnecessaryReturnStatement", "StatementWithEmptyBody" })
private static void orderBranches(MethodNode mth, IfRegion ifRegion) { private static void orderBranches(MethodNode mth, IfRegion ifRegion) {
if (RegionUtils.isEmpty(ifRegion.getElseRegion())) { if (RegionUtils.isEmpty(ifRegion.getElseRegion())) {
return; return;
@@ -79,9 +81,15 @@ public class IfRegionVisitor extends AbstractVisitor {
return; return;
} }
} }
boolean lastRegion = ifRegion == RegionUtils.getLastRegion(mth.getRegion()); boolean lastRegion = RegionUtils.hasExitEdge(ifRegion);
if (elseSize == 1 && lastRegion && mth.isVoidReturn()) { if (elseSize == 1 && lastRegion && mth.isVoidReturn()) {
// single return at method end will be removed later InsnNode lastElseInsn = RegionUtils.getLastInsn(ifRegion.getElseRegion());
if (lastElseInsn != null && lastElseInsn.getType() == InsnType.THROW) {
// move `throw` into `then` block
invertIfRegion(ifRegion);
} else {
// single return at method end will be removed later
}
return; return;
} }
if (!lastRegion) { if (!lastRegion) {
@@ -1,6 +1,6 @@
package jadx.core.dex.visitors.regions; package jadx.core.dex.visitors.regions;
import java.util.LinkedList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import org.slf4j.Logger; import org.slf4j.Logger;
@@ -125,7 +125,7 @@ public class LoopRegionVisitor extends AbstractVisitor implements IRegionVisitor
return false; return false;
} }
// can't make loop if argument from increment instruction is assign in loop // can't make loop if argument from increment instruction is assign in loop
List<RegisterArg> args = new LinkedList<>(); List<RegisterArg> args = new ArrayList<>();
incrInsn.getRegisterArgs(args); incrInsn.getRegisterArgs(args);
for (RegisterArg iArg : args) { for (RegisterArg iArg : args) {
try { try {
@@ -268,7 +268,7 @@ public class LoopRegionVisitor extends AbstractVisitor implements IRegionVisitor
|| !checkInvoke(nextCall, "java.util.Iterator", "next()Ljava/lang/Object;")) { || !checkInvoke(nextCall, "java.util.Iterator", "next()Ljava/lang/Object;")) {
return false; return false;
} }
List<InsnNode> toSkip = new LinkedList<>(); List<InsnNode> toSkip = new ArrayList<>();
RegisterArg iterVar; RegisterArg iterVar;
if (nextCall.contains(AFlag.WRAPPED)) { if (nextCall.contains(AFlag.WRAPPED)) {
InsnArg wrapArg = BlockUtils.searchWrappedInsnParent(mth, nextCall); InsnArg wrapArg = BlockUtils.searchWrappedInsnParent(mth, nextCall);
@@ -464,7 +464,7 @@ public class RegionMaker {
BlockNode exitEnd = BlockUtils.followEmptyPath(exit); BlockNode exitEnd = BlockUtils.followEmptyPath(exit);
List<LoopInfo> loops = exitEnd.getAll(AType.LOOP); List<LoopInfo> loops = exitEnd.getAll(AType.LOOP);
for (LoopInfo loopAtEnd : loops) { for (LoopInfo loopAtEnd : loops) {
if (loopAtEnd != loop) { if (loopAtEnd != loop && loop.hasParent(loopAtEnd)) {
insertEdge = exitEdge; insertEdge = exitEdge;
confirm = true; confirm = true;
break; break;
@@ -10,6 +10,7 @@ import jadx.core.dex.instructions.PhiInsn;
import jadx.core.dex.instructions.args.InsnArg; import jadx.core.dex.instructions.args.InsnArg;
import jadx.core.dex.instructions.args.InsnWrapArg; import jadx.core.dex.instructions.args.InsnWrapArg;
import jadx.core.dex.instructions.args.RegisterArg; import jadx.core.dex.instructions.args.RegisterArg;
import jadx.core.dex.instructions.args.SSAVar;
import jadx.core.dex.instructions.mods.TernaryInsn; import jadx.core.dex.instructions.mods.TernaryInsn;
import jadx.core.dex.nodes.BlockNode; import jadx.core.dex.nodes.BlockNode;
import jadx.core.dex.nodes.IContainer; import jadx.core.dex.nodes.IContainer;
@@ -73,11 +74,7 @@ public class TernaryMod extends AbstractRegionVisitor implements IRegionIterativ
return false; return false;
} }
if (elseRegion == null) { if (elseRegion == null) {
if (mth.isConstructor()) { return processOneBranchTernary(mth, ifRegion);
// force ternary conversion to inline all code in 'super' or 'this' calls
return processOneBranchTernary(mth, ifRegion);
}
return false;
} }
BlockNode tb = getTernaryInsnBlock(thenRegion); BlockNode tb = getTernaryInsnBlock(thenRegion);
BlockNode eb = getTernaryInsnBlock(elseRegion); BlockNode eb = getTernaryInsnBlock(elseRegion);
@@ -93,21 +90,8 @@ public class TernaryMod extends AbstractRegionVisitor implements IRegionIterativ
InsnNode thenInsn = tb.getInstructions().get(0); InsnNode thenInsn = tb.getInstructions().get(0);
InsnNode elseInsn = eb.getInstructions().get(0); InsnNode elseInsn = eb.getInstructions().get(0);
if (mth.contains(AFlag.USE_LINES_HINTS) if (!verifyLineHints(mth, thenInsn, elseInsn)) {
&& thenInsn.getSourceLine() != elseInsn.getSourceLine()) { return false;
if (thenInsn.getSourceLine() != 0 && elseInsn.getSourceLine() != 0) {
// sometimes source lines incorrect
if (!checkLineStats(thenInsn, elseInsn)) {
return false;
}
} else {
// no debug info
if (containsTernary(thenInsn) || containsTernary(elseInsn)) {
// don't make nested ternary by default
// TODO: add addition checks
return false;
}
}
} }
RegisterArg thenResArg = thenInsn.getResult(); RegisterArg thenResArg = thenInsn.getResult();
@@ -184,6 +168,20 @@ public class TernaryMod extends AbstractRegionVisitor implements IRegionIterativ
return false; return false;
} }
private static boolean verifyLineHints(MethodNode mth, InsnNode thenInsn, InsnNode elseInsn) {
if (mth.contains(AFlag.USE_LINES_HINTS)
&& thenInsn.getSourceLine() != elseInsn.getSourceLine()) {
if (thenInsn.getSourceLine() != 0 && elseInsn.getSourceLine() != 0) {
// sometimes source lines incorrect
return checkLineStats(thenInsn, elseInsn);
}
// don't make nested ternary by default
// TODO: add addition checks
return !containsTernary(thenInsn) && !containsTernary(elseInsn);
}
return true;
}
private static void clearConditionBlocks(List<BlockNode> conditionBlocks, BlockNode header) { private static void clearConditionBlocks(List<BlockNode> conditionBlocks, BlockNode header) {
for (BlockNode block : conditionBlocks) { for (BlockNode block : conditionBlocks) {
if (block != header) { if (block != header) {
@@ -277,6 +275,7 @@ public class TernaryMod extends AbstractRegionVisitor implements IRegionIterativ
return false; return false;
} }
@SuppressWarnings("StatementWithEmptyBody")
private static void replaceWithTernary(MethodNode mth, IfRegion ifRegion, BlockNode block, InsnNode insn) { private static void replaceWithTernary(MethodNode mth, IfRegion ifRegion, BlockNode block, InsnNode insn) {
RegisterArg resArg = insn.getResult(); RegisterArg resArg = insn.getResult();
if (resArg.getSVar().getUseList().size() != 1) { if (resArg.getSVar().getUseList().size() != 1) {
@@ -296,17 +295,45 @@ public class TernaryMod extends AbstractRegionVisitor implements IRegionIterativ
if (otherArg == null) { if (otherArg == null) {
return; return;
} }
InsnNode elseAssign = otherArg.getAssignInsn();
if (mth.isConstructor() || (mth.getParentClass().isEnum() && mth.getMethodInfo().isClassInit())) {
// forcing ternary inline for constructors (will help in moving super call to the top) and enums
// skip code style checks
} else {
if (elseAssign != null && elseAssign.isConstInsn()) {
if (!verifyLineHints(mth, insn, elseAssign)) {
return;
}
} else {
if (insn.getResult().sameCodeVar(otherArg)) {
// don't use same variable in else branch to prevent: l = (l == 0) ? 1 : l
return;
}
}
}
// all checks passed // all checks passed
BlockNode header = ifRegion.getConditionBlocks().get(0); BlockNode header = ifRegion.getConditionBlocks().get(0);
if (!ifRegion.getParent().replaceSubBlock(ifRegion, header)) { if (!ifRegion.getParent().replaceSubBlock(ifRegion, header)) {
return; return;
} }
InsnArg elseArg;
if (elseAssign != null && elseAssign.isConstInsn()) {
// inline constant
SSAVar elseVar = elseAssign.getResult().getSVar();
if (elseVar.getUseCount() == 1 && elseVar.getOnlyOneUseInPhi() == phiInsn) {
InsnRemover.remove(mth, elseAssign);
}
elseArg = InsnArg.wrapInsnIntoArg(elseAssign);
} else {
elseArg = otherArg;
}
TernaryInsn ternInsn = new TernaryInsn(ifRegion.getCondition(), TernaryInsn ternInsn = new TernaryInsn(ifRegion.getCondition(),
phiInsn.getResult(), InsnArg.wrapInsnIntoArg(insn), otherArg); phiInsn.getResult(), InsnArg.wrapInsnIntoArg(insn), elseArg);
ternInsn.simplifyCondition();
InsnRemover.unbindResult(mth, insn); InsnRemover.unbindResult(mth, insn);
InsnList.remove(block, insn); InsnList.remove(block, insn);
InsnRemover.unbindAllArgs(mth, phiInsn); InsnRemover.unbindAllArgs(mth, phiInsn);
header.getInstructions().clear(); header.getInstructions().clear();
ternInsn.rebindArgs(); ternInsn.rebindArgs();
@@ -15,8 +15,9 @@ import jadx.core.codegen.json.JsonMappingGen;
import jadx.core.deobf.Deobfuscator; import jadx.core.deobf.Deobfuscator;
import jadx.core.deobf.NameMapper; import jadx.core.deobf.NameMapper;
import jadx.core.dex.attributes.AFlag; import jadx.core.dex.attributes.AFlag;
import jadx.core.dex.attributes.AType;
import jadx.core.dex.attributes.nodes.MethodOverrideAttr;
import jadx.core.dex.attributes.nodes.RenameReasonAttr; import jadx.core.dex.attributes.nodes.RenameReasonAttr;
import jadx.core.dex.info.AccessInfo;
import jadx.core.dex.info.ClassInfo; import jadx.core.dex.info.ClassInfo;
import jadx.core.dex.info.FieldInfo; import jadx.core.dex.info.FieldInfo;
import jadx.core.dex.nodes.ClassNode; import jadx.core.dex.nodes.ClassNode;
@@ -45,8 +46,8 @@ public class RenameVisitor extends AbstractVisitor {
deobfuscator.execute(); deobfuscator.execute();
} }
checkClasses(deobfuscator, root, args);
UserRenames.applyForNodes(root); UserRenames.applyForNodes(root);
checkClasses(deobfuscator, root, args);
if (args.isDeobfuscationOn() || !args.isJsonOutput()) { if (args.isDeobfuscationOn() || !args.isJsonOutput()) {
deobfuscator.savePresets(); deobfuscator.savePresets();
@@ -196,13 +197,8 @@ public class RenameVisitor extends AbstractVisitor {
if (args.isRenameValid()) { if (args.isRenameValid()) {
Set<String> names = new HashSet<>(methods.size()); Set<String> names = new HashSet<>(methods.size());
for (MethodNode mth : methods) { 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); String signature = mth.getMethodInfo().makeSignature(true, false);
if (!names.add(signature)) { if (!names.add(signature) && canRename(mth)) {
deobfuscator.forceRenameMethod(mth); deobfuscator.forceRenameMethod(mth);
mth.addAttr(new RenameReasonAttr("collision with other method in class")); mth.addAttr(new RenameReasonAttr("collision with other method in class"));
} }
@@ -210,6 +206,23 @@ public class RenameVisitor extends AbstractVisitor {
} }
} }
private static boolean canRename(MethodNode mth) {
if (mth.contains(AFlag.DONT_RENAME)) {
return false;
}
MethodOverrideAttr overrideAttr = mth.get(AType.METHOD_OVERRIDE);
if (overrideAttr != null) {
for (MethodNode relatedMth : overrideAttr.getRelatedMthNodes()) {
if (relatedMth != mth && mth.getParentClass().equals(relatedMth.getParentClass())) {
// ignore rename if exists related method from same class (bridge method in most cases)
// such rename will also rename current method and will not help to resolve name collision
return false;
}
}
}
return true;
}
private static void processRootPackages(Deobfuscator deobfuscator, RootNode root, List<ClassNode> classes) { private static void processRootPackages(Deobfuscator deobfuscator, RootNode root, List<ClassNode> classes) {
Set<String> rootPkgs = collectRootPkgs(classes); Set<String> rootPkgs = collectRootPkgs(classes);
root.getCacheStorage().setRootPkgs(rootPkgs); root.getCacheStorage().setRootPkgs(rootPkgs);
@@ -1,7 +1,7 @@
package jadx.core.dex.visitors.shrink; package jadx.core.dex.visitors.shrink;
import java.util.ArrayList;
import java.util.BitSet; import java.util.BitSet;
import java.util.LinkedList;
import java.util.List; import java.util.List;
import jadx.core.dex.instructions.InsnType; import jadx.core.dex.instructions.InsnType;
@@ -30,7 +30,7 @@ final class ArgsInfo {
} }
public static List<RegisterArg> getArgs(InsnNode insn) { public static List<RegisterArg> getArgs(InsnNode insn) {
List<RegisterArg> args = new LinkedList<>(); List<RegisterArg> args = new ArrayList<>();
addArgs(insn, args); addArgs(insn, args);
return args; return args;
} }
@@ -1,10 +1,10 @@
package jadx.core.dex.visitors.ssa; package jadx.core.dex.visitors.ssa;
import java.util.ArrayDeque;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.BitSet; import java.util.BitSet;
import java.util.Deque; import java.util.Deque;
import java.util.Iterator; import java.util.Iterator;
import java.util.LinkedList;
import java.util.List; import java.util.List;
import jadx.core.dex.attributes.AFlag; import jadx.core.dex.attributes.AFlag;
@@ -81,7 +81,7 @@ public class SSATransform extends AbstractVisitor {
int blocksCount = blocks.size(); int blocksCount = blocks.size();
BitSet hasPhi = new BitSet(blocksCount); BitSet hasPhi = new BitSet(blocksCount);
BitSet processed = new BitSet(blocksCount); BitSet processed = new BitSet(blocksCount);
Deque<BlockNode> workList = new LinkedList<>(); Deque<BlockNode> workList = new ArrayDeque<>();
BitSet assignBlocks = la.getAssignBlocks(regNum); BitSet assignBlocks = la.getAssignBlocks(regNum);
for (int id = assignBlocks.nextSetBit(0); id >= 0; id = assignBlocks.nextSetBit(id + 1)) { for (int id = assignBlocks.nextSetBit(0); id >= 0; id = assignBlocks.nextSetBit(id + 1)) {
@@ -136,7 +136,7 @@ public class SSATransform extends AbstractVisitor {
RenameState initState = RenameState.init(mth); RenameState initState = RenameState.init(mth);
initPhiInEnterBlock(initState); initPhiInEnterBlock(initState);
Deque<RenameState> stack = new LinkedList<>(); Deque<RenameState> stack = new ArrayDeque<>();
stack.push(initState); stack.push(initState);
while (!stack.isEmpty()) { while (!stack.isEmpty()) {
RenameState state = stack.pop(); RenameState state = stack.pop();
@@ -913,25 +913,20 @@ public final class TypeInferenceVisitor extends AbstractVisitor {
} }
boolean fixed = false; boolean fixed = false;
for (ITypeBound bound : typeInfo.getBounds()) { for (RegisterArg arg : new ArrayList<>(var.getUseList())) {
if (bound.getBound() == BoundEnum.USE if (fixBooleanUsage(mth, arg)) {
&& fixBooleanUsage(mth, bound)) {
fixed = true; fixed = true;
} }
} }
return fixed; return fixed;
} }
private boolean fixBooleanUsage(MethodNode mth, ITypeBound bound) { private boolean fixBooleanUsage(MethodNode mth, RegisterArg boundArg) {
ArgType boundType = bound.getType(); ArgType boundType = boundArg.getInitType();
if (boundType == ArgType.BOOLEAN if (boundType == ArgType.BOOLEAN
|| (boundType.isTypeKnown() && !boundType.isPrimitive())) { || (boundType.isTypeKnown() && !boundType.isPrimitive())) {
return false; return false;
} }
RegisterArg boundArg = bound.getArg();
if (boundArg == null) {
return false;
}
InsnNode insn = boundArg.getParentInsn(); InsnNode insn = boundArg.getParentInsn();
if (insn == null || insn.getType() == InsnType.IF) { if (insn == null || insn.getType() == InsnType.IF) {
return false; return false;
@@ -0,0 +1,62 @@
package jadx.core.utils;
import java.util.HashSet;
import java.util.Locale;
import java.util.Objects;
import java.util.Set;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import jadx.core.deobf.NameMapper;
import jadx.core.deobf.TldHelper;
public class BetterName {
private static final Logger LOG = LoggerFactory.getLogger(BetterName.class);
private static final boolean DEBUG = true;
public static String compareAndGet(String first, String second) {
if (Objects.equals(first, second)) {
return first;
}
int firstRating = calcRating(first);
int secondRating = calcRating(second);
boolean firstBetter = firstRating >= secondRating;
if (DEBUG) {
if (firstBetter) {
LOG.info("Better name: '{}' > '{}' ({} > {})", first, second, firstRating, secondRating);
} else {
LOG.info("Better name: '{}' > '{}' ({} > {})", second, first, secondRating, firstRating);
}
}
return firstBetter ? first : second;
}
public static int calcRating(String str) {
int rating = str.length() * 3;
rating += differentCharsCount(str) * 20;
if (NameMapper.isAllCharsPrintable(str)) {
rating += 100;
}
if (NameMapper.isValidIdentifier(str)) {
rating += 50;
}
if (TldHelper.contains(str)) {
rating += 20;
}
if (str.contains("_")) {
// rare in obfuscated names
rating += 100;
}
return rating;
}
private static int differentCharsCount(String str) {
String lower = str.toLowerCase(Locale.ROOT);
Set<Integer> chars = new HashSet<>();
StringUtils.visitCodePoints(lower, chars::add);
return chars.size();
}
}
@@ -697,7 +697,7 @@ public class BlockUtils {
} }
/** /**
* Search lowest common ancestor in dominator tree for input set. * Search the lowest common ancestor in dominator tree for input set.
*/ */
@Nullable @Nullable
public static BlockNode getCommonDominator(MethodNode mth, List<BlockNode> blocks) { public static BlockNode getCommonDominator(MethodNode mth, List<BlockNode> blocks) {
@@ -1013,6 +1013,9 @@ public class BlockUtils {
*/ */
@Nullable @Nullable
public static InsnNode getOnlyOneInsnFromMth(MethodNode mth) { public static InsnNode getOnlyOneInsnFromMth(MethodNode mth) {
if (mth.isNoCode()) {
return null;
}
InsnNode insn = null; InsnNode insn = null;
for (BlockNode block : mth.getBasicBlocks()) { for (BlockNode block : mth.getBasicBlocks()) {
List<InsnNode> blockInsns = block.getInstructions(); List<InsnNode> blockInsns = block.getInstructions();
@@ -246,4 +246,29 @@ public class DebugUtils {
Set<Object> seen = ConcurrentHashMap.newKeySet(); Set<Object> seen = ConcurrentHashMap.newKeySet();
return t -> seen.add(keyExtractor.apply(t)); 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)));
}
} }
@@ -13,9 +13,7 @@ import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import jadx.api.IDecompileScheduler; import jadx.api.IDecompileScheduler;
import jadx.api.JadxDecompiler;
import jadx.api.JavaClass; import jadx.api.JavaClass;
import jadx.core.dex.nodes.ClassNode;
import jadx.core.utils.exceptions.JadxRuntimeException; import jadx.core.utils.exceptions.JadxRuntimeException;
public class DecompilerScheduler implements IDecompileScheduler { public class DecompilerScheduler implements IDecompileScheduler {
@@ -24,18 +22,11 @@ public class DecompilerScheduler implements IDecompileScheduler {
private static final int MERGED_BATCH_SIZE = 16; private static final int MERGED_BATCH_SIZE = 16;
private static final boolean DEBUG_BATCHES = false; private static final boolean DEBUG_BATCHES = false;
private final JadxDecompiler decompiler;
public DecompilerScheduler(JadxDecompiler decompiler) {
this.decompiler = decompiler;
}
@Override @Override
public List<List<JavaClass>> buildBatches(List<JavaClass> classes) { public List<List<JavaClass>> buildBatches(List<JavaClass> classes) {
try { try {
long start = System.currentTimeMillis(); long start = System.currentTimeMillis();
List<List<ClassNode>> batches = internalBatches(Utils.collectionMap(classes, JavaClass::getClassNode)); List<List<JavaClass>> result = internalBatches(classes);
List<List<JavaClass>> result = Utils.collectionMap(batches, l -> Utils.collectionMapNoNull(l, decompiler::getJavaClassByNode));
if (LOG.isDebugEnabled()) { if (LOG.isDebugEnabled()) {
LOG.debug("Build decompilation batches in {}ms", System.currentTimeMillis() - start); LOG.debug("Build decompilation batches in {}ms", System.currentTimeMillis() - start);
} }
@@ -53,14 +44,14 @@ public class DecompilerScheduler implements IDecompileScheduler {
* Put classes with many dependencies at the end. * Put classes with many dependencies at the end.
* Build batches for dependencies of single class to avoid locking from another thread. * Build batches for dependencies of single class to avoid locking from another thread.
*/ */
public List<List<ClassNode>> internalBatches(List<ClassNode> classes) { public List<List<JavaClass>> internalBatches(List<JavaClass> classes) {
List<DepInfo> deps = sumDependencies(classes); List<DepInfo> deps = sumDependencies(classes);
Set<ClassNode> added = new HashSet<>(classes.size()); Set<JavaClass> added = new HashSet<>(classes.size());
Comparator<ClassNode> cmpDepSize = Comparator.comparingInt(ClassNode::getTotalDepsCount); Comparator<JavaClass> cmpDepSize = Comparator.comparingInt(JavaClass::getTotalDepsCount);
List<List<ClassNode>> result = new ArrayList<>(); List<List<JavaClass>> result = new ArrayList<>();
List<ClassNode> mergedBatch = new ArrayList<>(MERGED_BATCH_SIZE); List<JavaClass> mergedBatch = new ArrayList<>(MERGED_BATCH_SIZE);
for (DepInfo depInfo : deps) { for (DepInfo depInfo : deps) {
ClassNode cls = depInfo.getCls(); JavaClass cls = depInfo.getCls();
if (!added.add(cls)) { if (!added.add(cls)) {
continue; continue;
} }
@@ -73,9 +64,9 @@ public class DecompilerScheduler implements IDecompileScheduler {
mergedBatch = new ArrayList<>(MERGED_BATCH_SIZE); mergedBatch = new ArrayList<>(MERGED_BATCH_SIZE);
} }
} else { } else {
List<ClassNode> batch = new ArrayList<>(depsSize + 1); List<JavaClass> batch = new ArrayList<>(depsSize + 1);
for (ClassNode dep : cls.getDependencies()) { for (JavaClass dep : cls.getDependencies()) {
ClassNode topDep = dep.getTopParentClass(); JavaClass topDep = dep.getTopParentClass();
if (!added.contains(topDep)) { if (!added.contains(topDep)) {
batch.add(topDep); batch.add(topDep);
added.add(topDep); added.add(topDep);
@@ -95,11 +86,11 @@ public class DecompilerScheduler implements IDecompileScheduler {
return result; return result;
} }
private static List<DepInfo> sumDependencies(List<ClassNode> classes) { private static List<DepInfo> sumDependencies(List<JavaClass> classes) {
List<DepInfo> deps = new ArrayList<>(classes.size()); List<DepInfo> deps = new ArrayList<>(classes.size());
for (ClassNode cls : classes) { for (JavaClass cls : classes) {
int count = 0; int count = 0;
for (ClassNode dep : cls.getDependencies()) { for (JavaClass dep : cls.getDependencies()) {
count += 1 + dep.getTotalDepsCount(); count += 1 + dep.getTotalDepsCount();
} }
deps.add(new DepInfo(cls, count)); deps.add(new DepInfo(cls, count));
@@ -109,15 +100,15 @@ public class DecompilerScheduler implements IDecompileScheduler {
} }
private static final class DepInfo implements Comparable<DepInfo> { private static final class DepInfo implements Comparable<DepInfo> {
private final ClassNode cls; private final JavaClass cls;
private final int depsCount; private final int depsCount;
private DepInfo(ClassNode cls, int depsCount) { private DepInfo(JavaClass cls, int depsCount) {
this.cls = cls; this.cls = cls;
this.depsCount = depsCount; this.depsCount = depsCount;
} }
public ClassNode getCls() { public JavaClass getCls() {
return cls; return cls;
} }
@@ -129,7 +120,7 @@ public class DecompilerScheduler implements IDecompileScheduler {
public int compareTo(@NotNull DecompilerScheduler.DepInfo o) { public int compareTo(@NotNull DecompilerScheduler.DepInfo o) {
int deps = Integer.compare(depsCount, o.depsCount); int deps = Integer.compare(depsCount, o.depsCount);
if (deps == 0) { if (deps == 0) {
return cls.compareTo(o.cls); return cls.getClassNode().compareTo(o.cls.getClassNode());
} }
return deps; return deps;
} }
@@ -147,9 +138,9 @@ public class DecompilerScheduler implements IDecompileScheduler {
.collect(Collectors.toList()); .collect(Collectors.toList());
} }
private void dumpBatchesStats(List<ClassNode> classes, List<List<ClassNode>> result, List<DepInfo> deps) { private void dumpBatchesStats(List<JavaClass> classes, List<List<JavaClass>> result, List<DepInfo> deps) {
double avg = result.stream().mapToInt(List::size).average().orElse(-1); double avg = result.stream().mapToInt(List::size).average().orElse(-1);
int maxSingleDeps = classes.stream().mapToInt(ClassNode::getTotalDepsCount).max().orElse(-1); int maxSingleDeps = classes.stream().mapToInt(JavaClass::getTotalDepsCount).max().orElse(-1);
int maxSubDeps = deps.stream().mapToInt(DepInfo::getDepsCount).max().orElse(-1); int maxSubDeps = deps.stream().mapToInt(DepInfo::getDepsCount).max().orElse(-1);
LOG.info("Batches stats:" LOG.info("Batches stats:"
+ "\n input classes: " + classes.size() + "\n input classes: " + classes.size()
@@ -29,8 +29,12 @@ public final class InsnList implements Iterable<InsnNode> {
} }
public static int getIndex(List<InsnNode> list, InsnNode insn) { public static int getIndex(List<InsnNode> list, InsnNode insn) {
return getIndex(list, insn, 0);
}
public static int getIndex(List<InsnNode> list, InsnNode insn, int startOffset) {
int size = list.size(); int size = list.size();
for (int i = 0; i < size; i++) { for (int i = startOffset; i < size; i++) {
if (list.get(i) == insn) { if (list.get(i) == insn) {
return i; return i;
} }
@@ -38,6 +42,14 @@ public final class InsnList implements Iterable<InsnNode> {
return -1; return -1;
} }
public static boolean contains(List<InsnNode> list, InsnNode insn) {
return getIndex(list, insn, 0) != -1;
}
public static boolean contains(List<InsnNode> list, InsnNode insn, int startOffset) {
return getIndex(list, insn, startOffset) != -1;
}
public int getIndex(InsnNode insn) { public int getIndex(InsnNode insn) {
return getIndex(list, insn); return getIndex(list, insn);
} }
@@ -17,6 +17,7 @@ import jadx.core.dex.instructions.args.InsnWrapArg;
import jadx.core.dex.instructions.args.RegisterArg; import jadx.core.dex.instructions.args.RegisterArg;
import jadx.core.dex.instructions.args.SSAVar; import jadx.core.dex.instructions.args.SSAVar;
import jadx.core.dex.nodes.BlockNode; import jadx.core.dex.nodes.BlockNode;
import jadx.core.dex.nodes.IContainer;
import jadx.core.dex.nodes.InsnNode; import jadx.core.dex.nodes.InsnNode;
import jadx.core.dex.nodes.MethodNode; import jadx.core.dex.nodes.MethodNode;
import jadx.core.utils.exceptions.JadxRuntimeException; import jadx.core.utils.exceptions.JadxRuntimeException;
@@ -228,6 +229,18 @@ public class InsnRemover {
removeAll(block.getInstructions(), insns); removeAll(block.getInstructions(), insns);
} }
public static void removeAllAndUnbind(MethodNode mth, IContainer container, List<InsnNode> insns) {
unbindInsns(mth, insns);
RegionUtils.visitBlocks(mth, container, b -> removeAll(b.getInstructions(), insns));
}
public static void removeAllAndUnbind(MethodNode mth, List<InsnNode> insns) {
unbindInsns(mth, insns);
for (BlockNode block : mth.getBasicBlocks()) {
removeAll(block.getInstructions(), insns);
}
}
public static void removeAllWithoutUnbind(BlockNode block, List<InsnNode> insns) { public static void removeAllWithoutUnbind(BlockNode block, List<InsnNode> insns) {
removeAll(block.getInstructions(), insns); removeAll(block.getInstructions(), insns);
} }
@@ -1,6 +1,7 @@
package jadx.core.utils; package jadx.core.utils;
import java.util.List; import java.util.List;
import java.util.function.Function;
import java.util.function.Predicate; import java.util.function.Predicate;
import org.jetbrains.annotations.Nullable; import org.jetbrains.annotations.Nullable;
@@ -140,6 +141,37 @@ public class InsnUtils {
return null; 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 @Nullable
public static RegisterArg getRegFromInsn(List<RegisterArg> regs, InsnType insnType) { public static RegisterArg getRegFromInsn(List<RegisterArg> regs, InsnType insnType) {
for (RegisterArg reg : regs) { for (RegisterArg reg : regs) {
@@ -160,6 +160,10 @@ public class ListUtils {
return true; return true;
} }
public static <T> boolean noneMatch(Collection<T> list, Predicate<T> test) {
return !anyMatch(list, test);
}
public static <T> boolean anyMatch(Collection<T> list, Predicate<T> test) { public static <T> boolean anyMatch(Collection<T> list, Predicate<T> test) {
if (list == null || list.isEmpty()) { if (list == null || list.isEmpty()) {
return false; return false;
@@ -4,6 +4,7 @@ import java.util.ArrayList;
import java.util.Collections; import java.util.Collections;
import java.util.List; import java.util.List;
import java.util.Set; import java.util.Set;
import java.util.function.Consumer;
import org.jetbrains.annotations.Nullable; import org.jetbrains.annotations.Nullable;
@@ -22,9 +23,12 @@ import jadx.core.dex.nodes.IRegion;
import jadx.core.dex.nodes.InsnNode; import jadx.core.dex.nodes.InsnNode;
import jadx.core.dex.nodes.MethodNode; import jadx.core.dex.nodes.MethodNode;
import jadx.core.dex.regions.Region; import jadx.core.dex.regions.Region;
import jadx.core.dex.regions.loops.LoopRegion;
import jadx.core.dex.trycatch.CatchAttr; import jadx.core.dex.trycatch.CatchAttr;
import jadx.core.dex.trycatch.ExceptionHandler; import jadx.core.dex.trycatch.ExceptionHandler;
import jadx.core.dex.trycatch.TryCatchBlockAttr; import jadx.core.dex.trycatch.TryCatchBlockAttr;
import jadx.core.dex.visitors.regions.AbstractRegionVisitor;
import jadx.core.dex.visitors.regions.DepthRegionTraversal;
import jadx.core.utils.exceptions.JadxRuntimeException; import jadx.core.utils.exceptions.JadxRuntimeException;
public class RegionUtils { public class RegionUtils {
@@ -145,20 +149,6 @@ public class RegionUtils {
} }
} }
@Nullable
public static IContainer getLastRegion(@Nullable IContainer container) {
if (container == null) {
return null;
}
if (container instanceof IBlock || container instanceof IBranchRegion) {
return container;
}
if (container instanceof IRegion) {
return getLastRegion(Utils.last(((IRegion) container).getSubBlocks()));
}
throw new JadxRuntimeException(unknownContainerType(container));
}
public static boolean isExitBlock(MethodNode mth, IContainer container) { public static boolean isExitBlock(MethodNode mth, IContainer container) {
if (container instanceof BlockNode) { if (container instanceof BlockNode) {
return BlockUtils.isExitBlock(mth, (BlockNode) container); return BlockUtils.isExitBlock(mth, (BlockNode) container);
@@ -289,7 +279,11 @@ public class RegionUtils {
} }
} }
return false; return false;
} else if (container instanceof IRegion) { }
if (container instanceof LoopRegion) {
return true;
}
if (container instanceof IRegion) {
IRegion region = (IRegion) container; IRegion region = (IRegion) container;
for (IContainer block : region.getSubBlocks()) { for (IContainer block : region.getSubBlocks()) {
if (notEmpty(block)) { if (notEmpty(block)) {
@@ -297,9 +291,8 @@ public class RegionUtils {
} }
} }
return false; return false;
} else {
throw new JadxRuntimeException(unknownContainerType(container));
} }
throw new JadxRuntimeException(unknownContainerType(container));
} }
public static void getAllRegionBlocks(IContainer container, Set<IBlock> blocks) { public static void getAllRegionBlocks(IContainer container, Set<IBlock> blocks) {
@@ -483,4 +476,13 @@ public class RegionUtils {
} }
return "Unknown container type: " + container.getClass(); return "Unknown container type: " + container.getClass();
} }
public static void visitBlocks(MethodNode mth, IContainer container, Consumer<IBlock> visitor) {
DepthRegionTraversal.traverse(mth, container, new AbstractRegionVisitor() {
@Override
public void processBlock(MethodNode mth, IBlock block) {
visitor.accept(block);
}
});
}
} }
@@ -428,7 +428,7 @@ public class Utils {
} }
public static void checkThreadInterrupt() { public static void checkThreadInterrupt() {
if (Thread.interrupted()) { if (Thread.currentThread().isInterrupted()) {
throw new JadxRuntimeException("Thread interrupted"); throw new JadxRuntimeException("Thread interrupted");
} }
} }
@@ -129,7 +129,7 @@ public class AndroidResourcesUtils {
FieldNode newResField = new FieldNode(typeCls, rFieldInfo, FieldNode newResField = new FieldNode(typeCls, rFieldInfo,
AccessFlags.PUBLIC | AccessFlags.STATIC | AccessFlags.FINAL); AccessFlags.PUBLIC | AccessFlags.STATIC | AccessFlags.FINAL);
newResField.addAttr(new EncodedValue(EncodedType.ENCODED_INT, resource.getId())); newResField.addAttr(new EncodedValue(EncodedType.ENCODED_INT, resource.getId()));
typeCls.getFields().add(newResField); typeCls.addField(newResField);
if (rClsExists) { if (rClsExists) {
newResField.addInfoComment("Added by JADX"); newResField.addInfoComment("Added by JADX");
} }
@@ -10,12 +10,16 @@ import java.io.InputStream;
import java.io.OutputStream; import java.io.OutputStream;
import java.nio.charset.StandardCharsets; import java.nio.charset.StandardCharsets;
import java.nio.file.FileVisitOption; import java.nio.file.FileVisitOption;
import java.nio.file.FileVisitResult;
import java.nio.file.Files; import java.nio.file.Files;
import java.nio.file.Path; import java.nio.file.Path;
import java.nio.file.Paths; import java.nio.file.Paths;
import java.nio.file.SimpleFileVisitor;
import java.nio.file.StandardOpenOption;
import java.nio.file.attribute.BasicFileAttributes;
import java.security.MessageDigest; import java.security.MessageDigest;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Comparator; import java.util.Collections;
import java.util.List; import java.util.List;
import java.util.Objects; import java.util.Objects;
import java.util.jar.JarEntry; import java.util.jar.JarEntry;
@@ -103,31 +107,42 @@ public class FileUtils {
} }
} }
public static void deleteFileIfExists(Path filePath) throws IOException {
Files.deleteIfExists(filePath);
}
public static boolean deleteDir(File dir) { public static boolean deleteDir(File dir) {
File[] content = dir.listFiles(); deleteDir(dir.toPath());
if (content != null) { return true;
for (File file : content) {
deleteDir(file);
}
}
return dir.delete();
} }
public static void deleteDirIfExists(Path dir) { public static void deleteDirIfExists(Path dir) {
if (Files.exists(dir)) { if (Files.exists(dir)) {
deleteDir(dir); try {
deleteDir(dir);
} catch (Exception e) {
LOG.error("Failed to delete dir: " + dir.toAbsolutePath(), e);
}
} }
} }
public static void deleteDir(Path dir) { private static final SimpleFileVisitor<Path> FILE_DELETE_VISITOR = new SimpleFileVisitor<Path>() {
try (Stream<Path> pathStream = Files.walk(dir)) { @Override
pathStream.sorted(Comparator.reverseOrder()) public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
.map(Path::toFile) Files.delete(file);
.forEach(file -> { return FileVisitResult.CONTINUE;
if (!file.delete()) { }
LOG.warn("Failed to remove file: {}", file.getAbsolutePath());
} @Override
}); public FileVisitResult postVisitDirectory(Path dir, IOException exc) throws IOException {
Files.delete(dir);
return FileVisitResult.CONTINUE;
}
};
private static void deleteDir(Path dir) {
try {
Files.walkFileTree(dir, Collections.emptySet(), Integer.MAX_VALUE, FILE_DELETE_VISITOR);
} catch (Exception e) { } catch (Exception e) {
throw new JadxRuntimeException("Failed to delete directory " + dir, e); throw new JadxRuntimeException("Failed to delete directory " + dir, e);
} }
@@ -152,11 +167,11 @@ public class FileUtils {
} }
public static void deleteTempRootDir() { public static void deleteTempRootDir() {
deleteDir(TEMP_ROOT_DIR); deleteDirIfExists(TEMP_ROOT_DIR);
} }
public static void clearTempRootDir() { public static void clearTempRootDir() {
deleteDir(TEMP_ROOT_DIR); deleteDirIfExists(TEMP_ROOT_DIR);
makeDirs(TEMP_ROOT_DIR); makeDirs(TEMP_ROOT_DIR);
} }
@@ -217,6 +232,16 @@ public class FileUtils {
} }
} }
public static void writeFile(Path file, String data) throws IOException {
FileUtils.makeDirsForFile(file);
Files.write(file, data.getBytes(StandardCharsets.UTF_8),
StandardOpenOption.WRITE, StandardOpenOption.CREATE, StandardOpenOption.TRUNCATE_EXISTING);
}
public static String readFile(Path textFile) throws IOException {
return new String(Files.readAllBytes(textFile), StandardCharsets.UTF_8);
}
@NotNull @NotNull
public static File prepareFile(File file) { public static File prepareFile(File file) {
File saveFile = cutFileName(file); File saveFile = cutFileName(file);
@@ -254,6 +279,28 @@ public class FileUtils {
return new String(hexChars, StandardCharsets.UTF_8); return new String(hexChars, StandardCharsets.UTF_8);
} }
/**
* Zero padded hex string for first byte
*/
public static String byteToHex(int value) {
int v = value & 0xFF;
byte[] hexChars = new byte[] { HEX_ARRAY[v >>> 4], HEX_ARRAY[v & 0x0F] };
return new String(hexChars, StandardCharsets.US_ASCII);
}
/**
* Zero padded hex string for int value
*/
public static String intToHex(int value) {
byte[] hexChars = new byte[8];
int v = value;
for (int i = 7; i >= 0; i--) {
hexChars[i] = HEX_ARRAY[v & 0x0F];
v >>>= 4;
}
return new String(hexChars, StandardCharsets.US_ASCII);
}
public static boolean isZipFile(File file) { public static boolean isZipFile(File file) {
try (InputStream is = new FileInputStream(file)) { try (InputStream is = new FileInputStream(file)) {
byte[] headers = new byte[4]; byte[] headers = new byte[4];
@@ -89,7 +89,8 @@ public class BinaryXMLParser extends CommonBinaryParser {
is.mark(4); is.mark(4);
int v = is.readInt16(); // version int v = is.readInt16(); // version
int h = is.readInt16(); // header size int h = is.readInt16(); // header size
if (v == 0x0003 && h == 0x0008) { // Some APK Manifest.xml the version is 0
if (h == 0x0008) {
return true; return true;
} }
is.reset(); is.reset();
@@ -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();
}
@@ -4,10 +4,8 @@ import java.io.InputStream;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.HashMap; import java.util.HashMap;
import java.util.LinkedHashMap; import java.util.LinkedHashMap;
import java.util.LinkedList;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.stream.Collectors;
import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilder;
@@ -175,7 +173,7 @@ public class ManifestAttributes {
if (attr.getType() == MAttrType.ENUM) { if (attr.getType() == MAttrType.ENUM) {
return attr.getValues().get(value); return attr.getValues().get(value);
} else if (attr.getType() == MAttrType.FLAG) { } else if (attr.getType() == MAttrType.FLAG) {
List<String> flagList = new LinkedList<>(); List<String> flagList = new ArrayList<>();
List<Long> attrKeys = new ArrayList<>(attr.getValues().keySet()); List<Long> attrKeys = new ArrayList<>(attr.getValues().keySet());
attrKeys.sort((a, b) -> Long.compare(b, a)); // sort descending attrKeys.sort((a, b) -> Long.compare(b, a)); // sort descending
for (Long key : attrKeys) { for (Long key : attrKeys) {
@@ -188,7 +186,7 @@ public class ManifestAttributes {
value ^= key; value ^= key;
} }
} }
return flagList.stream().collect(Collectors.joining("|")); return String.join("|", flagList);
} }
return null; return null;
} }
@@ -28,9 +28,12 @@ public class ParserConstants {
protected static final int RES_XML_LAST_CHUNK_TYPE = 0x017f; protected static final int RES_XML_LAST_CHUNK_TYPE = 0x017f;
protected static final int RES_XML_RESOURCE_MAP_TYPE = 0x0180; protected static final int RES_XML_RESOURCE_MAP_TYPE = 0x0180;
protected static final int RES_TABLE_PACKAGE_TYPE = 0x0200; protected static final int RES_TABLE_PACKAGE_TYPE = 0x0200; // 512
protected static final int RES_TABLE_TYPE_TYPE = 0x0201; protected static final int RES_TABLE_TYPE_TYPE = 0x0201; // 513
protected static final int RES_TABLE_TYPE_SPEC_TYPE = 0x0202; protected static final int RES_TABLE_TYPE_SPEC_TYPE = 0x0202; // 514
protected static final int RES_TABLE_TYPE_LIBRARY = 0x0203; // 515
protected static final int RES_TABLE_TYPE_OVERLAY = 0x0204; // 516
protected static final int RES_TABLE_TYPE_STAGED_ALIAS = 0x0206; // 517
/** /**
* Type constants * Type constants
@@ -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;
}
}

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