1
mirror of https://github.com/revanced/revanced-patcher synced 2025-09-13 18:30:49 +02:00

Compare commits

...

52 Commits

Author SHA1 Message Date
semantic-release-bot
817b8db019 chore(release): 2.5.2 [skip ci]
## [2.5.2](https://github.com/revanced/revanced-patcher/compare/v2.5.1...v2.5.2) (2022-07-24)
2022-07-24 16:30:57 +00:00
Sculas
a321b8971b build: update Apktool to 2.7.0 2022-07-24 18:29:25 +02:00
semantic-release-bot
783b2de9db chore(release): 2.5.1 [skip ci]
## [2.5.1](https://github.com/revanced/revanced-patcher/compare/v2.5.0...v2.5.1) (2022-07-17)

### Bug Fixes

* close stream when closing `DomFileEditor` ([77604d4](77604d4078))
2022-07-17 23:30:04 +00:00
oSumAtrIX
77604d4078 fix: close stream when closing DomFileEditor 2022-07-18 01:28:16 +02:00
oSumAtrIX
21b5404180 docs: change size of headings [skip ci] 2022-07-12 01:12:45 +02:00
semantic-release-bot
9ac6d5c7da chore(release): 2.5.0 [skip ci]
# [2.5.0](https://github.com/revanced/revanced-patcher/compare/v2.4.0...v2.5.0) (2022-07-11)

### Bug Fixes

* missing additional items [skip ci] ([0ebab8b](0ebab8bf59))

### Features

* feature request issue template ([1b39278](1b39278b24))
* issue templates [skip ci] ([112bc99](112bc998f4))
2022-07-11 18:21:14 +00:00
oSumAtrIX
1b39278b24 feat: feature request issue template 2022-07-11 20:19:53 +02:00
oSumAtrIX
0ebab8bf59 fix: missing additional items [skip ci] 2022-07-10 00:34:55 +02:00
oSumAtrIX
112bc998f4 feat: issue templates [skip ci] 2022-07-10 00:32:05 +02:00
semantic-release-bot
12c96bf818 chore(release): 2.4.0 [skip ci]
# [2.4.0](https://github.com/revanced/revanced-patcher/compare/v2.3.1...v2.4.0) (2022-07-09)

### Features

* Improve Smali Compiler ([6bfe571](6bfe5716c3))
2022-07-09 13:04:21 +00:00
oSumAtrIX
91298a8790 Merge pull request #52 from revanced/feat/smali-branching
feat: improve Smali compiler
2022-07-09 15:03:01 +02:00
oSumAtrIX
f2a7cff41c style: fix casing of the first letter in comments 2022-07-09 06:27:49 +02:00
oSumAtrIX
dd941233ca refactor: improve the addInstructions extension method further more 2022-07-09 06:26:05 +02:00
semantic-release-bot
fc06dd1c29 chore(release): 2.3.1 [skip ci]
## [2.3.1](https://github.com/revanced/revanced-patcher/compare/v2.3.0...v2.3.1) (2022-07-07)

### Bug Fixes

* handle null properly ([#64](https://github.com/revanced/revanced-patcher/issues/64)) ([482af78](482af78f2b))
2022-07-07 06:35:28 +00:00
bogadana
482af78f2b fix: handle null properly (#64) 2022-07-07 08:33:40 +02:00
semantic-release-bot
89a27dfbe6 chore(release): 2.3.0 [skip ci]
# [2.3.0](https://github.com/revanced/revanced-patcher/compare/v2.2.2...v2.3.0) (2022-07-05)

### Features

* nullability for `BytecodePatch` constructor ([#59](https://github.com/revanced/revanced-patcher/issues/59)) ([4ea030d](4ea030d0a0))
2022-07-05 14:48:22 +00:00
bogadana
4ea030d0a0 feat: nullability for BytecodePatch constructor (#59) 2022-07-05 16:46:54 +02:00
semantic-release-bot
4cc2fa17f5 chore(release): 2.2.2 [skip ci]
## [2.2.2](https://github.com/revanced/revanced-patcher/compare/v2.2.1...v2.2.2) (2022-07-04)

### Bug Fixes

* `MethodWalker` not accounting for all reference instructions ([48068cb](48068cb3d7))
2022-07-04 19:36:34 +00:00
oSumAtrIX
48068cb3d7 fix: MethodWalker not accounting for all reference instructions 2022-07-03 22:51:04 +02:00
semantic-release-bot
d107c7245c chore(release): 2.2.1 [skip ci]
## [2.2.1](https://github.com/revanced/revanced-patcher/compare/v2.2.0...v2.2.1) (2022-07-03)

### Bug Fixes

* more useful error message ([4b2e323](4b2e3230ec))
2022-07-03 14:46:19 +00:00
oSumAtrIX
4b2e3230ec fix: more useful error message 2022-07-03 15:37:48 +02:00
semantic-release-bot
fb5b82da4e chore(release): 2.2.0 [skip ci]
# [2.2.0](https://github.com/revanced/revanced-patcher/compare/v2.1.2...v2.2.0) (2022-07-02)

### Bug Fixes

* DomFileEditor opening in- and output streams on the same file ([83187c9](83187c9edd))

### Features

* remove deprecated functions ([ada5a03](ada5a033de))
* streams overload for `XmlFileHolder` ([6f72c4c](6f72c4c4c0))
2022-07-02 22:59:27 +00:00
oSumAtrIX
5970e32aa5 Merge pull request #57 from revanced/dev
Merge `dev` to `main`
2022-07-03 00:30:37 +02:00
semantic-release-bot
0f00d33f4e chore(release): 2.2.0-dev.3 [skip ci]
# [2.2.0-dev.3](https://github.com/revanced/revanced-patcher/compare/v2.2.0-dev.2...v2.2.0-dev.3) (2022-07-02)

### Bug Fixes

* DomFileEditor opening in- and output streams on the same file ([83187c9](83187c9edd))
2022-07-02 22:21:19 +00:00
oSumAtrIX
83187c9edd fix: DomFileEditor opening in- and output streams on the same file 2022-07-03 00:19:39 +02:00
semantic-release-bot
79d70cff4b chore(release): 2.2.0-dev.2 [skip ci]
# [2.2.0-dev.2](https://github.com/revanced/revanced-patcher/compare/v2.2.0-dev.1...v2.2.0-dev.2) (2022-07-02)

### Features

* streams overload for `XmlFileHolder` ([6f72c4c](6f72c4c4c0))
2022-07-02 15:46:16 +00:00
oSumAtrIX
6f72c4c4c0 feat: streams overload for XmlFileHolder 2022-07-02 17:44:08 +02:00
oSumAtrIX
c8eedac4d9 refactor: members of ResourceData 2022-07-02 16:22:19 +02:00
semantic-release-bot
60a8278ae8 chore(release): 2.2.0-dev.1 [skip ci]
# [2.2.0-dev.1](https://github.com/revanced/revanced-patcher/compare/v2.1.2...v2.2.0-dev.1) (2022-07-02)

### Features

* remove deprecated functions ([ada5a03](ada5a033de))
2022-07-02 02:23:32 +00:00
oSumAtrIX
ada5a033de feat: remove deprecated functions 2022-07-02 04:21:20 +02:00
semantic-release-bot
109b8a296d chore(release): 2.1.2 [skip ci]
## [2.1.2](https://github.com/revanced/revanced-patcher/compare/v2.1.1...v2.1.2) (2022-06-29)

### Bug Fixes

* invert fingerprint resolution condition of `customFingerprint` ([e2faf4c](e2faf4ca9b))
2022-06-29 23:37:37 +00:00
oSumAtrIX
e2faf4ca9b fix: invert fingerprint resolution condition of customFingerprint 2022-06-30 01:36:25 +02:00
oSumAtrIX
2134182a0e refactor: declare data parameter internal 2022-06-29 19:55:23 +02:00
semantic-release-bot
d8b5b8bb7c chore(release): 2.1.1 [skip ci]
## [2.1.1](https://github.com/revanced/revanced-patcher/compare/v2.1.0...v2.1.1) (2022-06-28)
2022-06-28 17:47:39 +00:00
Sculas
49970b5926 build: update Apktool to re-enable 9patch decoder 2022-06-28 19:45:45 +02:00
semantic-release-bot
c1fbd8cf8c chore(release): 2.1.0 [skip ci]
# [2.1.0](https://github.com/revanced/revanced-patcher/compare/v2.0.4...v2.1.0) (2022-06-28)

### Features

* log failed patches due to failed dependencies ([a467fbb](a467fbb704))
2022-06-28 01:27:05 +00:00
oSumAtrIX
a467fbb704 feat: log failed patches due to failed dependencies 2022-06-28 03:25:49 +02:00
semantic-release-bot
5a4bd7a76e chore(release): 2.0.4 [skip ci]
## [2.0.4](https://github.com/revanced/revanced-patcher/compare/v2.0.3...v2.0.4) (2022-06-27)
2022-06-27 22:38:36 +00:00
Sculas
e5ca86fac6 build: update Apktool to disable 9patch decoder
for real for real, this time
2022-06-28 00:36:50 +02:00
semantic-release-bot
68d9e9f02c chore(release): 2.0.3 [skip ci]
## [2.0.3](https://github.com/revanced/revanced-patcher/compare/v2.0.2...v2.0.3) (2022-06-27)
2022-06-27 22:22:19 +00:00
Sculas
494a9a09ac build: update Apktool to disable 9patch decoder
for real, this time
2022-06-28 00:20:54 +02:00
semantic-release-bot
06a88839de chore(release): 2.0.2 [skip ci]
## [2.0.2](https://github.com/revanced/revanced-patcher/compare/v2.0.1...v2.0.2) (2022-06-27)
2022-06-27 22:05:56 +00:00
Sculas
4c41b955df ci: trigger release on build commits 2022-06-28 00:04:39 +02:00
Sculas
614e555f4c build: update Apktool to disable 9patch decoder 2022-06-28 00:01:38 +02:00
Lucaskyy
6bfe5716c3 feat: Improve Smali Compiler
- Branching support has been added. See InlineSmaliCompilerTest.kt for an example.
- Some other improvements have been made too.
2022-06-27 21:11:59 +02:00
semantic-release-bot
d6ed06a327 chore(release): 2.0.1 [skip ci]
## [2.0.1](https://github.com/revanced/revanced-patcher/compare/v2.0.0...v2.0.1) (2022-06-26)

### Bug Fixes

* use `Exception` instead of `MethodNotFoundException` ([2fc4ec4](2fc4ec4021))
2022-06-26 16:03:10 +00:00
oSumAtrIX
2fc4ec4021 fix: use Exception instead of MethodNotFoundException 2022-06-26 18:01:54 +02:00
Lucaskyy
f565c4f6a7 refactor: improve ResourceData.kt
Old methods have been marked as deprecated, and will be removed in the future.

- ResourceData.kt was made an Iterable<File>, and the forEach method was removed in favor of Kotlin's forEach function. (no modifications required)
- The resolve method was deprecated in favor of a new operator getter function, which can be either called using get(path) or data[path]. This keeps backwards compatibility with the old get method.
- The getXmlEditor method was deprecated in favor of the new xmlEditor variable, which is a XmlFileHolder which has an operator getter which acts like an array. This is syntactically better.
2022-06-26 17:14:38 +02:00
Lucaskyy
35749454ab refactor: remove deprecated methods in ResourceData.kt 2022-06-26 16:53:25 +02:00
Lucaskyy
2b492e7a5e refactor: cleanup code 2022-06-26 16:32:22 +02:00
Lucaskyy
081a5a6849 refactor: fmt ExampleBytecodePatch.kt 2022-06-26 16:21:55 +02:00
Lucaskyy
852ae7d8d1 refactor: move BytecodeData#proxy into class 2022-06-26 16:20:45 +02:00
22 changed files with 502 additions and 449 deletions

24
.github/ISSUE_TEMPLATE/bug-report.md vendored Normal file
View File

@@ -0,0 +1,24 @@
---
name: Bug report
about: Create a bug report on the patcher. Do not submit suggestions for patches here.
title: 'problem: some problem'
labels: bug
assignees: oSumAtrIX, Sculas
---
## 🐞 Issue
<!-- Describe your issue in detail here -->
## ⚙ Reproduce
<!-- Include your environment and steps to reproduce the issue as detailed as possible -->
## 🛠 Solution
<!-- If applicable, add a possible solution -->
## ⚠ Additional context
<!-- Add any other context about the problem here -->

View File

@@ -0,0 +1,24 @@
---
name: Feature request
about: Suggest a change for the patcher. Do not submit suggestions for patches here.
title: 'feat: some feature'
labels: feature-request
assignees: ''
---
## 🐞 Issue
<!-- Explain here, what the current problem is and why it leads you to request a feature change -->
## ❗ Solution
<!-- Explain how your current issue can be solved -->
## ❓ Motivation
<!-- Explain why your feature should be considered -->
## ⚠ Additional context
<!-- Add any other context or screenshots about the feature request here -->

View File

@@ -7,7 +7,11 @@
} }
], ],
"plugins": [ "plugins": [
"@semantic-release/commit-analyzer", ["@semantic-release/commit-analyzer", {
"releaseRules": [
{"type": "build", "release": "patch"}
]
}],
"@semantic-release/release-notes-generator", "@semantic-release/release-notes-generator",
"@semantic-release/changelog", "@semantic-release/changelog",
"gradle-semantic-release-plugin", "gradle-semantic-release-plugin",

View File

@@ -1,3 +1,123 @@
## [2.5.2](https://github.com/revanced/revanced-patcher/compare/v2.5.1...v2.5.2) (2022-07-24)
## [2.5.1](https://github.com/revanced/revanced-patcher/compare/v2.5.0...v2.5.1) (2022-07-17)
### Bug Fixes
* close stream when closing `DomFileEditor` ([77604d4](https://github.com/revanced/revanced-patcher/commit/77604d40785847b775155c0e75b663a3c7336aa3))
# [2.5.0](https://github.com/revanced/revanced-patcher/compare/v2.4.0...v2.5.0) (2022-07-11)
### Bug Fixes
* missing additional items [skip ci] ([0ebab8b](https://github.com/revanced/revanced-patcher/commit/0ebab8bf598d993df6e340651205cba48f1ef725))
### Features
* feature request issue template ([1b39278](https://github.com/revanced/revanced-patcher/commit/1b39278b24ba2f964d93bd8ad2e28472ee036d90))
* issue templates [skip ci] ([112bc99](https://github.com/revanced/revanced-patcher/commit/112bc998f4761a647cb9eab7454e35264fa96fd9))
# [2.4.0](https://github.com/revanced/revanced-patcher/compare/v2.3.1...v2.4.0) (2022-07-09)
### Features
* Improve Smali Compiler ([6bfe571](https://github.com/revanced/revanced-patcher/commit/6bfe5716c38181bbe9476b5c6ad29526edb4e022))
## [2.3.1](https://github.com/revanced/revanced-patcher/compare/v2.3.0...v2.3.1) (2022-07-07)
### Bug Fixes
* handle null properly ([#64](https://github.com/revanced/revanced-patcher/issues/64)) ([482af78](https://github.com/revanced/revanced-patcher/commit/482af78f2ba23b8003fc9961df5fde54d7295d5c))
# [2.3.0](https://github.com/revanced/revanced-patcher/compare/v2.2.2...v2.3.0) (2022-07-05)
### Features
* nullability for `BytecodePatch` constructor ([#59](https://github.com/revanced/revanced-patcher/issues/59)) ([4ea030d](https://github.com/revanced/revanced-patcher/commit/4ea030d0a03f736bbecbd491317ba2167b18fe94))
## [2.2.2](https://github.com/revanced/revanced-patcher/compare/v2.2.1...v2.2.2) (2022-07-04)
### Bug Fixes
* `MethodWalker` not accounting for all reference instructions ([48068cb](https://github.com/revanced/revanced-patcher/commit/48068cb3d79e283ff1cad9f3f78dc1d0fcd14f83))
## [2.2.1](https://github.com/revanced/revanced-patcher/compare/v2.2.0...v2.2.1) (2022-07-03)
### Bug Fixes
* more useful error message ([4b2e323](https://github.com/revanced/revanced-patcher/commit/4b2e3230ec74fa3a57ae86067e5cb7cecbe45013))
# [2.2.0](https://github.com/revanced/revanced-patcher/compare/v2.1.2...v2.2.0) (2022-07-02)
### Bug Fixes
* DomFileEditor opening in- and output streams on the same file ([83187c9](https://github.com/revanced/revanced-patcher/commit/83187c9edd7b088bc18960c5eb9a2042ca536b5f))
### Features
* remove deprecated functions ([ada5a03](https://github.com/revanced/revanced-patcher/commit/ada5a033de3cf94e7255ec2d522520f86431f001))
* streams overload for `XmlFileHolder` ([6f72c4c](https://github.com/revanced/revanced-patcher/commit/6f72c4c4c051e48c8d03d2a7b2cfc1c53028ed86))
# [2.2.0-dev.3](https://github.com/revanced/revanced-patcher/compare/v2.2.0-dev.2...v2.2.0-dev.3) (2022-07-02)
### Bug Fixes
* DomFileEditor opening in- and output streams on the same file ([83187c9](https://github.com/revanced/revanced-patcher/commit/83187c9edd7b088bc18960c5eb9a2042ca536b5f))
# [2.2.0-dev.2](https://github.com/revanced/revanced-patcher/compare/v2.2.0-dev.1...v2.2.0-dev.2) (2022-07-02)
### Features
* streams overload for `XmlFileHolder` ([6f72c4c](https://github.com/revanced/revanced-patcher/commit/6f72c4c4c051e48c8d03d2a7b2cfc1c53028ed86))
# [2.2.0-dev.1](https://github.com/revanced/revanced-patcher/compare/v2.1.2...v2.2.0-dev.1) (2022-07-02)
### Features
* remove deprecated functions ([ada5a03](https://github.com/revanced/revanced-patcher/commit/ada5a033de3cf94e7255ec2d522520f86431f001))
## [2.1.2](https://github.com/revanced/revanced-patcher/compare/v2.1.1...v2.1.2) (2022-06-29)
### Bug Fixes
* invert fingerprint resolution condition of `customFingerprint` ([e2faf4c](https://github.com/revanced/revanced-patcher/commit/e2faf4ca9b6de23300b20ab471ee9dc365b04339))
## [2.1.1](https://github.com/revanced/revanced-patcher/compare/v2.1.0...v2.1.1) (2022-06-28)
# [2.1.0](https://github.com/revanced/revanced-patcher/compare/v2.0.4...v2.1.0) (2022-06-28)
### Features
* log failed patches due to failed dependencies ([a467fbb](https://github.com/revanced/revanced-patcher/commit/a467fbb704eebe812cdec14025398dab2af43959))
## [2.0.4](https://github.com/revanced/revanced-patcher/compare/v2.0.3...v2.0.4) (2022-06-27)
## [2.0.3](https://github.com/revanced/revanced-patcher/compare/v2.0.2...v2.0.3) (2022-06-27)
## [2.0.2](https://github.com/revanced/revanced-patcher/compare/v2.0.1...v2.0.2) (2022-06-27)
## [2.0.1](https://github.com/revanced/revanced-patcher/compare/v2.0.0...v2.0.1) (2022-06-26)
### Bug Fixes
* use `Exception` instead of `MethodNotFoundException` ([2fc4ec4](https://github.com/revanced/revanced-patcher/commit/2fc4ec40217a917ea6106ddc87be332f725aa13c))
# [2.0.0](https://github.com/revanced/revanced-patcher/compare/v1.11.0...v2.0.0) (2022-06-26) # [2.0.0](https://github.com/revanced/revanced-patcher/compare/v1.11.0...v2.0.0) (2022-06-26)

View File

@@ -21,11 +21,10 @@ repositories {
} }
dependencies { dependencies {
implementation("app.revanced:multidexlib2:2.5.2.r2")
implementation("xpp3:xpp3:1.1.4c") implementation("xpp3:xpp3:1.1.4c")
implementation("org.smali:smali:2.5.2") implementation("org.smali:smali:2.5.2")
implementation("org.apktool:apktool-lib:2.6.5-SNAPSHOT") implementation("app.revanced:multidexlib2:2.5.2.r2")
implementation("org.apktool:apktool-lib:2.7.0-SNAPSHOT")
testImplementation(kotlin("test")) testImplementation(kotlin("test"))
} }

View File

@@ -1,2 +1,2 @@
kotlin.code.style = official kotlin.code.style = official
version = 2.0.0 version = 2.5.2

View File

@@ -226,7 +226,7 @@ class Patcher(private val options: PatcherOptions) {
return PatcherResult( return PatcherResult(
dexFiles.map { dexFiles.map {
app.revanced.patcher.util.dex.DexFile(it.key, it.value.readAt(0)) app.revanced.patcher.util.dex.DexFile(it.key, it.value.readAt(0))
}, metaInfo.doNotCompress.toList(), resourceFile }, metaInfo.doNotCompress?.toList(), resourceFile
) )
} }
@@ -241,31 +241,35 @@ class Patcher(private val options: PatcherOptions) {
/** /**
* Apply a [patch] and its dependencies recursively. * Apply a [patch] and its dependencies recursively.
* @param patch The [patch] to apply. * @param patch The [patch] to apply.
* @param appliedPatches A list of [patch] names, to prevent applying [patch]es twice. * @param appliedPatches A map of [patch]es paired to a boolean indicating their success, to prevent infinite recursion.
* @return The result of executing the [patch]. * @return The result of executing the [patch].
*/ */
private fun applyPatch( private fun applyPatch(
patch: Class<out Patch<Data>>, appliedPatches: MutableList<String> patch: Class<out Patch<Data>>,
appliedPatches: MutableMap<String, Boolean>
): PatchResult { ): PatchResult {
val patchName = patch.patchName val patchName = patch.patchName
// if the patch has already applied silently skip it // if the patch has already applied silently skip it
if (appliedPatches.contains(patchName)) { if (appliedPatches.contains(patchName)) {
logger.trace("Skipping patch $patchName because it has already been applied") if (!appliedPatches[patchName]!!)
return PatchResultError("'$patchName' did not succeed previously")
logger.trace("Skipping '$patchName' because it has already been applied")
return PatchResultSuccess() return PatchResultSuccess()
} }
appliedPatches.add(patchName)
// recursively apply all dependency patches // recursively apply all dependency patches
patch.dependencies?.forEach { patch.dependencies?.forEach {
val patchDependency = it.java val patchDependency = it.java
val result = applyPatch(patchDependency, appliedPatches) val result = applyPatch(patchDependency, appliedPatches)
if (result.isSuccess()) return@forEach if (result.isSuccess()) return@forEach
val errorMessage = result.error()!!.message val errorMessage = result.error()!!.cause
return PatchResultError("$patchName depends on ${patchDependency.patchName} but the following error was raised: $errorMessage") return PatchResultError("'$patchName' depends on '${patchDependency.patchName}' but the following error was raised: $errorMessage")
} }
val patchInstance = patch.getDeclaredConstructor().newInstance() val patchInstance = patch.getDeclaredConstructor().newInstance()
@@ -273,7 +277,7 @@ class Patcher(private val options: PatcherOptions) {
// if the current patch is a resource patch but resource patching is disabled, return an error // if the current patch is a resource patch but resource patching is disabled, return an error
val isResourcePatch = patchInstance is ResourcePatch val isResourcePatch = patchInstance is ResourcePatch
if (!options.patchResources && isResourcePatch) { if (!options.patchResources && isResourcePatch) {
return PatchResultError("$patchName is a resource patch, but resource patching is disabled.") return PatchResultError("'$patchName' is a resource patch, but resource patching is disabled")
} }
// TODO: find a solution for this // TODO: find a solution for this
@@ -281,15 +285,18 @@ class Patcher(private val options: PatcherOptions) {
data.resourceData data.resourceData
} else { } else {
val bytecodeData = data.bytecodeData val bytecodeData = data.bytecodeData
(patchInstance as BytecodePatch).fingerprints.resolve(bytecodeData, bytecodeData.classes.internalClasses) (patchInstance as BytecodePatch).fingerprints?.resolve(bytecodeData, bytecodeData.classes.internalClasses)
bytecodeData bytecodeData
} }
logger.trace("Executing patch $patchName of type: ${if (isResourcePatch) "resource" else "bytecode"}") logger.trace("Executing '$patchName' of type: ${if (isResourcePatch) "resource" else "bytecode"}")
return try { return try {
patchInstance.execute(data) val result = patchInstance.execute(data)
appliedPatches[patchName] = result.isSuccess()
result
} catch (e: Exception) { } catch (e: Exception) {
appliedPatches[patchName] = false
PatchResultError(e) PatchResultError(e)
} }
} }
@@ -301,7 +308,7 @@ class Patcher(private val options: PatcherOptions) {
*/ */
fun applyPatches(stopOnError: Boolean = false) = sequence { fun applyPatches(stopOnError: Boolean = false) = sequence {
logger.trace("Applying all patches") logger.trace("Applying all patches")
val appliedPatches = mutableListOf<String>() val appliedPatches = mutableMapOf<String, Boolean>() // first is success, second is name
for (patch in data.patches) { for (patch in data.patches) {
val patchResult = applyPatch(patch, appliedPatches) val patchResult = applyPatch(patch, appliedPatches)

View File

@@ -13,9 +13,7 @@ data class PatcherData(
internal val resourceCacheDirectory: String, internal val resourceCacheDirectory: String,
val packageMetadata: PackageMetadata val packageMetadata: PackageMetadata
) { ) {
internal val patches = mutableListOf<Class<out Patch<Data>>>() internal val patches = mutableListOf<Class<out Patch<Data>>>()
internal val bytecodeData = BytecodeData(internalClasses) internal val bytecodeData = BytecodeData(internalClasses)
internal val resourceData = ResourceData(File(resourceCacheDirectory)) internal val resourceData = ResourceData(File(resourceCacheDirectory))
} }

View File

@@ -28,6 +28,15 @@ class BytecodeData(
classes.proxies.firstOrNull { predicate(it.immutableClass) } ?: classes.proxies.firstOrNull { predicate(it.immutableClass) } ?:
// else resolve the class to a proxy and return it, if the predicate is matching a class // else resolve the class to a proxy and return it, if the predicate is matching a class
classes.find(predicate)?.let { proxy(it) } classes.find(predicate)?.let { proxy(it) }
fun proxy(classDef: ClassDef): app.revanced.patcher.util.proxy.ClassProxy {
var proxy = this.classes.proxies.find { it.immutableClass.type == classDef.type }
if (proxy == null) {
proxy = app.revanced.patcher.util.proxy.ClassProxy(classDef)
this.classes.add(proxy)
}
return proxy
}
} }
internal class MethodNotFoundException(s: String) : Exception(s) internal class MethodNotFoundException(s: String) : Exception(s)
@@ -58,12 +67,3 @@ internal inline fun <T> Iterable<T>.findIndexed(predicate: (T) -> Boolean): Pair
} }
return null return null
} }
fun BytecodeData.proxy(classDef: ClassDef): app.revanced.patcher.util.proxy.ClassProxy {
var proxy = this.classes.proxies.find { it.immutableClass.type == classDef.type }
if (proxy == null) {
proxy = app.revanced.patcher.util.proxy.ClassProxy(classDef)
this.classes.add(proxy)
}
return proxy
}

View File

@@ -4,43 +4,46 @@ import app.revanced.patcher.data.Data
import org.w3c.dom.Document import org.w3c.dom.Document
import java.io.Closeable import java.io.Closeable
import java.io.File import java.io.File
import java.io.InputStream
import java.io.OutputStream
import javax.xml.parsers.DocumentBuilderFactory import javax.xml.parsers.DocumentBuilderFactory
import javax.xml.transform.TransformerFactory import javax.xml.transform.TransformerFactory
import javax.xml.transform.dom.DOMSource import javax.xml.transform.dom.DOMSource
import javax.xml.transform.stream.StreamResult import javax.xml.transform.stream.StreamResult
class ResourceData(private val resourceCacheDirectory: File) : Data { class ResourceData(private val resourceCacheDirectory: File) : Data, Iterable<File> {
private fun resolve(path: String) = resourceCacheDirectory.resolve(path) val xmlEditor = XmlFileHolder()
fun forEach(action: (File) -> Unit) = resourceCacheDirectory.walkTopDown().forEach(action) operator fun get(path: String) = resourceCacheDirectory.resolve(path)
fun get(path: String) = resolve(path)
fun replace(path: String, oldValue: String, newValue: String, oldValueIsRegex: Boolean = false) { override fun iterator() = resourceCacheDirectory.walkTopDown().iterator()
// TODO: buffer this somehow
val content = resolve(path).readText()
if (oldValueIsRegex) { inner class XmlFileHolder {
content.replace(Regex(oldValue), newValue) operator fun get(inputStream: InputStream, outputStream: OutputStream) =
return DomFileEditor(inputStream, lazyOf(outputStream))
operator fun get(path: String) = DomFileEditor(this@ResourceData[path])
} }
}
fun getXmlEditor(path: String) = DomFileEditor(resolve(path))
} }
class DomFileEditor internal constructor(private val domFile: File) : Closeable { class DomFileEditor internal constructor(
val file: Document private val inputStream: InputStream,
private val outputStream: Lazy<OutputStream>,
) : Closeable {
val file: Document = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(inputStream)
.also(Document::normalize)
init { // lazily open an output stream
val factory = DocumentBuilderFactory.newInstance() // this is required because when constructing a DomFileEditor the output stream is created along with the input stream, which is not allowed
// the workaround is to lazily create the output stream. This way it would be used after the input stream is closed, which happens in the constructor
constructor(file: File) : this(file.inputStream(), lazy { file.outputStream() })
val builder = factory.newDocumentBuilder() override fun close() {
val result = StreamResult(outputStream.value)
TransformerFactory.newInstance().newTransformer().transform(DOMSource(file), result)
// this will expectedly throw inputStream.close()
file = builder.parse(domFile) outputStream.value.close()
file.normalize()
} }
override fun close() = TransformerFactory.newInstance().newTransformer()
.transform(DOMSource(file), StreamResult(domFile.outputStream()))
} }

View File

@@ -2,12 +2,17 @@ package app.revanced.patcher.extensions
import app.revanced.patcher.util.proxy.mutableTypes.MutableMethod import app.revanced.patcher.util.proxy.mutableTypes.MutableMethod
import app.revanced.patcher.util.proxy.mutableTypes.MutableMethod.Companion.toMutable import app.revanced.patcher.util.proxy.mutableTypes.MutableMethod.Companion.toMutable
import app.revanced.patcher.util.smali.ExternalLabel
import app.revanced.patcher.util.smali.toInstruction import app.revanced.patcher.util.smali.toInstruction
import app.revanced.patcher.util.smali.toInstructions import app.revanced.patcher.util.smali.toInstructions
import org.jf.dexlib2.AccessFlags import org.jf.dexlib2.AccessFlags
import org.jf.dexlib2.builder.BuilderInstruction import org.jf.dexlib2.builder.BuilderInstruction
import org.jf.dexlib2.builder.BuilderOffsetInstruction
import org.jf.dexlib2.builder.Label
import org.jf.dexlib2.builder.MutableMethodImplementation import org.jf.dexlib2.builder.MutableMethodImplementation
import org.jf.dexlib2.builder.instruction.*
import org.jf.dexlib2.iface.Method import org.jf.dexlib2.iface.Method
import org.jf.dexlib2.iface.instruction.Instruction
import org.jf.dexlib2.iface.reference.MethodReference import org.jf.dexlib2.iface.reference.MethodReference
import org.jf.dexlib2.immutable.ImmutableMethod import org.jf.dexlib2.immutable.ImmutableMethod
import org.jf.dexlib2.immutable.ImmutableMethodImplementation import org.jf.dexlib2.immutable.ImmutableMethodImplementation
@@ -23,6 +28,12 @@ fun MutableMethodImplementation.addInstructions(index: Int, instructions: List<B
} }
} }
fun MutableMethodImplementation.addInstructions(instructions: List<BuilderInstruction>) {
for (instruction in instructions) {
this.addInstruction(instruction)
}
}
fun MutableMethodImplementation.replaceInstructions(index: Int, instructions: List<BuilderInstruction>) { fun MutableMethodImplementation.replaceInstructions(index: Int, instructions: List<BuilderInstruction>) {
for (i in instructions.lastIndex downTo 0) { for (i in instructions.lastIndex downTo 0) {
this.replaceInstruction(index + i, instructions[i]) this.replaceInstruction(index + i, instructions[i])
@@ -40,11 +51,11 @@ fun MutableMethodImplementation.removeInstructions(index: Int, count: Int) {
* @param otherMethod The method to compare against. * @param otherMethod The method to compare against.
* @return True if the methods match given the conditions. * @return True if the methods match given the conditions.
*/ */
fun Method.softCompareTo( fun Method.softCompareTo(otherMethod: MethodReference): Boolean {
otherMethod: MethodReference if (MethodUtil.isConstructor(this) && !parametersEqual(
): Boolean { this.parameterTypes, otherMethod.parameterTypes
if (MethodUtil.isConstructor(this) && !parametersEqual(this.parameterTypes, otherMethod.parameterTypes)) )
return false ) return false
return this.name == otherMethod.name return this.name == otherMethod.name
} }
@@ -54,9 +65,7 @@ fun Method.softCompareTo(
* This may be a positive or negative number. * This may be a positive or negative number.
* @return The **immutable** cloned method. Call [toMutable] or [cloneMutable] to get a **mutable** copy. * @return The **immutable** cloned method. Call [toMutable] or [cloneMutable] to get a **mutable** copy.
*/ */
internal fun Method.clone( internal fun Method.clone(registerCount: Int = 0): ImmutableMethod {
registerCount: Int = 0,
): ImmutableMethod {
val clonedImplementation = implementation?.let { val clonedImplementation = implementation?.let {
ImmutableMethodImplementation( ImmutableMethodImplementation(
it.registerCount + registerCount, it.registerCount + registerCount,
@@ -66,14 +75,7 @@ internal fun Method.clone(
) )
} }
return ImmutableMethod( return ImmutableMethod(
returnType, returnType, name, parameters, returnType, accessFlags, annotations, hiddenApiRestrictions, clonedImplementation
name,
parameters,
returnType,
accessFlags,
annotations,
hiddenApiRestrictions,
clonedImplementation
) )
} }
@@ -104,16 +106,89 @@ fun MutableMethod.replaceInstruction(index: Int, instruction: String) =
* Remove a smali instruction within the method. * Remove a smali instruction within the method.
* @param index The index to delete the instruction at. * @param index The index to delete the instruction at.
*/ */
fun MutableMethod.removeInstruction(index: Int) = fun MutableMethod.removeInstruction(index: Int) = this.implementation!!.removeInstruction(index)
this.implementation!!.removeInstruction(index)
/**
* Create a label for the instruction at given index in the method's implementation.
* @param index The index to create the label for the instruction at.
* @return The label.
*/
fun MutableMethod.label(index: Int) = this.implementation!!.newLabelForIndex(index)
/**
* Get the instruction at given index in the method's implementation.
* @param index The index to get the instruction at.
* @return The instruction.
*/
fun MutableMethod.instruction(index: Int): BuilderInstruction = this.implementation!!.instructions[index]
/** /**
* Add smali instructions to the method. * Add smali instructions to the method.
* @param index The index to insert the instructions at. * @param index The index to insert the instructions at.
* @param smali The smali instructions to add.
* @param externalLabels A list of [ExternalLabel] representing a list of labels for instructions which are not in the method to compile.
*/
fun MutableMethod.addInstructions(index: Int, smali: String, externalLabels: List<ExternalLabel> = emptyList()) {
// Create reference dummy instructions for the instructions.
val nopedSmali = StringBuilder(smali).also { builder ->
externalLabels.forEach { (name, _) ->
builder.append("\n:$name\nnop")
}
}.toString()
// Compile the instructions with the dummy labels
val compiledInstructions = nopedSmali.toInstructions(this)
// Add the compiled list of instructions to the method.
val methodImplementation = this.implementation!!
methodImplementation.addInstructions(index, compiledInstructions)
val methodInstructions = methodImplementation.instructions
methodInstructions.subList(index, index + compiledInstructions.size)
.forEachIndexed { compiledInstructionIndex, compiledInstruction ->
// If the compiled instruction is not an offset instruction, skip it.
if (compiledInstruction !is BuilderOffsetInstruction) return@forEachIndexed
/**
* Creates a new label for the instruction and replaces it with the label of the [compiledInstruction] at [compiledInstructionIndex].
*/
fun Instruction.makeNewLabel() {
// Create the final label.
val label = methodImplementation.newLabelForIndex(methodInstructions.indexOf(this))
// Create the final instruction with the new label.
val newInstruction = replaceOffset(
compiledInstruction, label
)
// Replace the instruction pointing to the dummy label with the new instruction pointing to the real instruction.
methodImplementation.replaceInstruction(index + compiledInstructionIndex, newInstruction)
}
// If the compiled instruction targets its own instruction,
// which means it points to some of its own, simply an offset has to be applied.
val labelIndex = compiledInstruction.target.location.index
if (labelIndex < compiledInstructions.size - externalLabels.size) {
// Get the targets index (insertion index + the index of the dummy instruction).
methodInstructions[index + labelIndex].makeNewLabel()
return@forEachIndexed
}
// Since the compiled instruction points to a dummy instruction,
// we can find the real instruction which it was created for by calculation.
// Get the index of the instruction in the externalLabels list which the dummy instruction was created for.
// this line works because we created the dummy instructions in the same order as the externalLabels list.
val (_, instruction) = externalLabels[(compiledInstructions.size - 1) - labelIndex]
instruction.makeNewLabel()
}
}
/**
* Add smali instructions to the end of the method.
* @param instructions The smali instructions to add. * @param instructions The smali instructions to add.
*/ */
fun MutableMethod.addInstructions(index: Int, instructions: String) = fun MutableMethod.addInstructions(instructions: String, labels: List<ExternalLabel> = emptyList()) =
this.implementation!!.addInstructions(index, instructions.toInstructions(this)) this.addInstructions(this.implementation!!.instructions.size, instructions, labels)
/** /**
* Replace smali instructions within the method. * Replace smali instructions within the method.
@@ -128,8 +203,21 @@ fun MutableMethod.replaceInstructions(index: Int, instructions: String) =
* @param index The index to remove the instructions at. * @param index The index to remove the instructions at.
* @param count The amount of instructions to remove. * @param count The amount of instructions to remove.
*/ */
fun MutableMethod.removeInstructions(index: Int, count: Int) = fun MutableMethod.removeInstructions(index: Int, count: Int) = this.implementation!!.removeInstructions(index, count)
this.implementation!!.removeInstructions(index, count)
private fun replaceOffset(
i: BuilderOffsetInstruction, label: Label
): BuilderOffsetInstruction {
return when (i) {
is BuilderInstruction10t -> BuilderInstruction10t(i.opcode, label)
is BuilderInstruction20t -> BuilderInstruction20t(i.opcode, label)
is BuilderInstruction21t -> BuilderInstruction21t(i.opcode, i.registerA, label)
is BuilderInstruction22t -> BuilderInstruction22t(i.opcode, i.registerA, i.registerB, label)
is BuilderInstruction30t -> BuilderInstruction30t(i.opcode, label)
is BuilderInstruction31t -> BuilderInstruction31t(i.opcode, i.registerA, label)
else -> throw IllegalStateException("A non-offset instruction was given, this should never happen!")
}
}
/** /**
* Clones the method. * Clones the method.
@@ -137,14 +225,11 @@ fun MutableMethod.removeInstructions(index: Int, count: Int) =
* This may be a positive or negative number. * This may be a positive or negative number.
* @return The **mutable** cloned method. Call [clone] to get an **immutable** copy. * @return The **mutable** cloned method. Call [clone] to get an **immutable** copy.
*/ */
internal fun Method.cloneMutable( internal fun Method.cloneMutable(registerCount: Int = 0) = clone(registerCount).toMutable()
registerCount: Int = 0,
) = clone(registerCount).toMutable()
// FIXME: also check the order of parameters as different order equals different method overload // FIXME: also check the order of parameters as different order equals different method overload
internal fun parametersEqual( internal fun parametersEqual(
parameters1: Iterable<CharSequence>, parameters1: Iterable<CharSequence>, parameters2: Iterable<CharSequence>
parameters2: Iterable<CharSequence>
): Boolean { ): Boolean {
return parameters1.count() == parameters2.count() && parameters1.all { parameter -> return parameters1.count() == parameters2.count() && parameters1.all { parameter ->
parameters2.any { parameters2.any {
@@ -155,10 +240,9 @@ internal fun parametersEqual(
} }
} }
internal val nullOutputStream: OutputStream = internal val nullOutputStream = object : OutputStream() {
object : OutputStream() {
override fun write(b: Int) {} override fun write(b: Int) {}
} }
/** /**
* Should be used to parse a list of parameters represented by their first letter, * Should be used to parse a list of parameters represented by their first letter,

View File

@@ -2,7 +2,6 @@ package app.revanced.patcher.fingerprint.method.impl
import app.revanced.patcher.data.impl.BytecodeData import app.revanced.patcher.data.impl.BytecodeData
import app.revanced.patcher.data.impl.MethodNotFoundException import app.revanced.patcher.data.impl.MethodNotFoundException
import app.revanced.patcher.data.impl.proxy
import app.revanced.patcher.extensions.MethodFingerprintExtensions.name import app.revanced.patcher.extensions.MethodFingerprintExtensions.name
import app.revanced.patcher.extensions.softCompareTo import app.revanced.patcher.extensions.softCompareTo
import app.revanced.patcher.fingerprint.Fingerprint import app.revanced.patcher.fingerprint.Fingerprint
@@ -35,7 +34,7 @@ abstract class MethodFingerprint(
* @throws MethodNotFoundException If the resolution of the [Method] has not happened. * @throws MethodNotFoundException If the resolution of the [Method] has not happened.
*/ */
var result: MethodFingerprintResult? = null var result: MethodFingerprintResult? = null
get() = field ?: throw MethodNotFoundException("${this.name} has not been resolved yet.") get() = field ?: throw Exception("${this.name} has not been resolved yet.")
} }
/** /**
@@ -49,7 +48,7 @@ data class MethodFingerprintResult(
val method: Method, val method: Method,
val classDef: ClassDef, val classDef: ClassDef,
val patternScanResult: PatternScanResult?, val patternScanResult: PatternScanResult?,
val data: BytecodeData internal val data: BytecodeData
) { ) {
/** /**
* Returns a mutable clone of [classDef] * Returns a mutable clone of [classDef]

View File

@@ -68,7 +68,7 @@ object MethodFingerprintUtils {
) )
) return false ) return false
if (methodFingerprint.customFingerprint != null && methodFingerprint.customFingerprint!!(context)) if (methodFingerprint.customFingerprint != null && !methodFingerprint.customFingerprint!!(context))
return false return false
if (methodFingerprint.strings != null) { if (methodFingerprint.strings != null) {

View File

@@ -9,5 +9,5 @@ import app.revanced.patcher.patch.Patch
* @param fingerprints A list of [MethodFingerprint] this patch relies on. * @param fingerprints A list of [MethodFingerprint] this patch relies on.
*/ */
abstract class BytecodePatch( abstract class BytecodePatch(
internal val fingerprints: Iterable<MethodFingerprint> internal val fingerprints: Iterable<MethodFingerprint>? = null
) : Patch<BytecodeData>() ) : Patch<BytecodeData>()

View File

@@ -4,11 +4,9 @@ import app.revanced.patcher.data.impl.BytecodeData
import app.revanced.patcher.data.impl.MethodNotFoundException import app.revanced.patcher.data.impl.MethodNotFoundException
import app.revanced.patcher.extensions.softCompareTo import app.revanced.patcher.extensions.softCompareTo
import app.revanced.patcher.util.proxy.mutableTypes.MutableMethod import app.revanced.patcher.util.proxy.mutableTypes.MutableMethod
import org.jf.dexlib2.Format
import org.jf.dexlib2.iface.Method import org.jf.dexlib2.iface.Method
import org.jf.dexlib2.iface.instruction.formats.Instruction35c import org.jf.dexlib2.iface.instruction.ReferenceInstruction
import org.jf.dexlib2.iface.reference.MethodReference import org.jf.dexlib2.iface.reference.MethodReference
import org.jf.dexlib2.util.Preconditions
/** /**
* Find a method from another method via instruction offsets. * Find a method from another method via instruction offsets.
@@ -37,9 +35,7 @@ class MethodWalker internal constructor(
currentMethod.implementation?.instructions?.let { instructions -> currentMethod.implementation?.instructions?.let { instructions ->
val instruction = instructions.elementAt(offset) val instruction = instructions.elementAt(offset)
Preconditions.checkFormat(instruction.opcode, Format.Format35c) val newMethod = (instruction as ReferenceInstruction).reference as MethodReference
val newMethod = (instruction as Instruction35c).reference as MethodReference
val proxy = bytecodeData.findClass(newMethod.definingClass)!! val proxy = bytecodeData.findClass(newMethod.definingClass)!!
val methods = if (walkMutable) proxy.resolve().methods else proxy.immutableClass.methods val methods = if (walkMutable) proxy.resolve().methods else proxy.immutableClass.methods

View File

@@ -0,0 +1,10 @@
package app.revanced.patcher.util.smali
import org.jf.dexlib2.iface.instruction.Instruction
/**
* A class that represents a label for an instruction.
* @param name The label name.
* @param instruction The instruction that this label is for.
*/
data class ExternalLabel(internal val name: String, internal val instruction: Instruction)

View File

@@ -1,12 +1,12 @@
package app.revanced.patcher.util.smali package app.revanced.patcher.util.smali
import app.revanced.patcher.util.proxy.mutableTypes.MutableMethod
import org.antlr.runtime.CommonTokenStream import org.antlr.runtime.CommonTokenStream
import org.antlr.runtime.TokenSource import org.antlr.runtime.TokenSource
import org.antlr.runtime.tree.CommonTreeNodeStream import org.antlr.runtime.tree.CommonTreeNodeStream
import org.jf.dexlib2.AccessFlags import org.jf.dexlib2.AccessFlags
import org.jf.dexlib2.Opcodes import org.jf.dexlib2.Opcodes
import org.jf.dexlib2.builder.BuilderInstruction import org.jf.dexlib2.builder.BuilderInstruction
import org.jf.dexlib2.iface.Method
import org.jf.dexlib2.writer.builder.DexBuilder import org.jf.dexlib2.writer.builder.DexBuilder
import org.jf.smali.LexerErrorInterface import org.jf.smali.LexerErrorInterface
import org.jf.smali.smaliFlexLexer import org.jf.smali.smaliFlexLexer
@@ -27,18 +27,19 @@ class InlineSmaliCompiler {
companion object { companion object {
/** /**
* Compiles a string of Smali code to a list of instructions. * Compiles a string of Smali code to a list of instructions.
* p0, p1 etc. will only work correctly if the parameters and registers are passed. * Special registers (such as p0, p1) will only work correctly
* Do not cross the boundaries of the control flow (if-nez insn, etc), * if the parameters and registers of the method are passed.
* as that will result in exceptions since the labels cannot be calculated.
* Do not create dummy labels to fix the issue, since the code addresses will
* be messed up and results in broken Dalvik bytecode.
* FIXME: Fix the above issue. When this is fixed, add the proper conversions in [InstructionConverter].
*/ */
fun compile( fun compile(
instructions: String, parameters: String, registers: Int, forStaticMethod: Boolean instructions: String, parameters: String, registers: Int, forStaticMethod: Boolean
): List<BuilderInstruction> { ): List<BuilderInstruction> {
val input = val input = METHOD_TEMPLATE.format(
METHOD_TEMPLATE.format(if (forStaticMethod) "static" else "", parameters, registers, instructions) if (forStaticMethod) {
"static"
} else {
""
}, parameters, registers, instructions
)
val reader = InputStreamReader(input.byteInputStream()) val reader = InputStreamReader(input.byteInputStream())
val lexer: LexerErrorInterface = smaliFlexLexer(reader, 15) val lexer: LexerErrorInterface = smaliFlexLexer(reader, 15)
val tokens = CommonTokenStream(lexer as TokenSource) val tokens = CommonTokenStream(lexer as TokenSource)
@@ -54,25 +55,30 @@ class InlineSmaliCompiler {
val dexGen = smaliTreeWalker(treeStream) val dexGen = smaliTreeWalker(treeStream)
dexGen.setDexBuilder(DexBuilder(Opcodes.getDefault())) dexGen.setDexBuilder(DexBuilder(Opcodes.getDefault()))
val classDef = dexGen.smali_file() val classDef = dexGen.smali_file()
return classDef.methods.first().implementation!!.instructions.map { it.toBuilderInstruction() } return classDef.methods.first().implementation!!.instructions.map { it as BuilderInstruction }
} }
} }
} }
/** /**
* Compile lines of Smali code to a list of instructions. * Compile lines of Smali code to a list of instructions.
* @param templateMethod The method to compile the instructions against. *
* Note: Adding compiled instructions to an existing method with
* offset instructions WITHOUT specifying a parent method will not work.
* @param method The method to compile the instructions against.
* @returns A list of instructions. * @returns A list of instructions.
*/ */
fun String.toInstructions(templateMethod: Method? = null) = InlineSmaliCompiler.compile(this, fun String.toInstructions(method: MutableMethod? = null): List<BuilderInstruction> {
templateMethod?.parameters?.joinToString("") { it } ?: "", return InlineSmaliCompiler.compile(this,
templateMethod?.implementation?.registerCount ?: 1, method?.parameters?.joinToString("") { it } ?: "",
templateMethod?.let { AccessFlags.STATIC.isSet(it.accessFlags) } ?: true method?.implementation?.registerCount ?: 1,
) method?.let { AccessFlags.STATIC.isSet(it.accessFlags) } ?: true
)
}
/** /**
* Compile a line of Smali code to an instruction. * Compile a line of Smali code to an instruction.
* @param templateMethod The method to compile the instructions against. * @param templateMethod The method to compile the instructions against.
* @return The instruction. * @return The instruction.
*/ */
fun String.toInstruction(templateMethod: Method? = null) = this.toInstructions(templateMethod).first() fun String.toInstruction(templateMethod: MutableMethod? = null) = this.toInstructions(templateMethod).first()

View File

@@ -1,46 +0,0 @@
package app.revanced.patcher.usage
import org.junit.jupiter.api.Test
internal class PatcherTest {
@Test
fun testPatcher() {
return // FIXME: create a proper resource to pass this test
/**
val patcher = Patcher(
File(PatcherTest::class.java.getResource("/example.apk")!!.toURI()),
"exampleCacheDirectory",
patchResources = true
)
patcher.addPatches(listOf(ExampleBytecodePatch(), ExampleResourcePatch()))
for (signature in patcher.resolveSignatures()) {
if (!signature.resolved) {
throw Exception("Signature ${signature.metadata.name} was not resolved!")
}
val patternScanMethod = signature.metadata.patternScanMethod
if (patternScanMethod is PatternScanMethod.Fuzzy) {
val warnings = patternScanMethod.warnings
if (warnings != null) {
println("Signature ${signature.metadata.name} had ${warnings.size} warnings!")
for (warning in warnings) {
println(warning.toString())
}
} else {
println("Signature ${signature.metadata.name} used the fuzzy resolver, but the warnings list is null!")
}
}
}
for ((metadata, result) in patcher.applyPatches()) {
if (result.isFailure) {
throw Exception("Patch ${metadata.shortName} failed", result.exceptionOrNull()!!)
} else {
println("Patch ${metadata.shortName} applied successfully!")
}
}
val out = patcher.save()
assertTrue(out.isNotEmpty(), "Expected the output of Patcher#save() to not be empty.")
*/
}
}

View File

@@ -6,6 +6,7 @@ import app.revanced.patcher.annotation.Version
import app.revanced.patcher.data.impl.BytecodeData import app.revanced.patcher.data.impl.BytecodeData
import app.revanced.patcher.extensions.addInstructions import app.revanced.patcher.extensions.addInstructions
import app.revanced.patcher.extensions.or import app.revanced.patcher.extensions.or
import app.revanced.patcher.extensions.replaceInstruction
import app.revanced.patcher.patch.PatchResult import app.revanced.patcher.patch.PatchResult
import app.revanced.patcher.patch.PatchResultSuccess import app.revanced.patcher.patch.PatchResultSuccess
import app.revanced.patcher.patch.annotations.Patch import app.revanced.patcher.patch.annotations.Patch
@@ -14,7 +15,6 @@ import app.revanced.patcher.usage.bytecode.fingerprints.ExampleFingerprint
import app.revanced.patcher.usage.resource.annotation.ExampleResourceCompatibility import app.revanced.patcher.usage.resource.annotation.ExampleResourceCompatibility
import app.revanced.patcher.util.proxy.mutableTypes.MutableField.Companion.toMutable import app.revanced.patcher.util.proxy.mutableTypes.MutableField.Companion.toMutable
import app.revanced.patcher.util.proxy.mutableTypes.MutableMethod.Companion.toMutable import app.revanced.patcher.util.proxy.mutableTypes.MutableMethod.Companion.toMutable
import app.revanced.patcher.util.smali.toInstruction
import com.google.common.collect.ImmutableList import com.google.common.collect.ImmutableList
import org.jf.dexlib2.AccessFlags import org.jf.dexlib2.AccessFlags
import org.jf.dexlib2.Format import org.jf.dexlib2.Format
@@ -36,12 +36,7 @@ import org.jf.dexlib2.util.Preconditions
@Description("Example demonstration of a bytecode patch.") @Description("Example demonstration of a bytecode patch.")
@ExampleResourceCompatibility @ExampleResourceCompatibility
@Version("0.0.1") @Version("0.0.1")
class ExampleBytecodePatch : BytecodePatch( class ExampleBytecodePatch : BytecodePatch(listOf(ExampleFingerprint)) {
listOf(
ExampleFingerprint
)
) {
// This function will be executed by the patcher. // This function will be executed by the patcher.
// You can treat it as a constructor // You can treat it as a constructor
override fun execute(data: BytecodeData): PatchResult { override fun execute(data: BytecodeData): PatchResult {
@@ -112,22 +107,21 @@ class ExampleBytecodePatch : BytecodePatch(
) )
// store the fields initial value into the first virtual register // store the fields initial value into the first virtual register
implementation.replaceInstruction( method.replaceInstruction(0, "sget-object v0, LTestClass;->dummyField:Ljava/io/PrintStream;")
0,
"sget-object v0, LTestClass;->dummyField:Ljava/io/PrintStream;".toInstruction()
)
// Now let's create a new call to our method and print the return value! // Now let's create a new call to our method and print the return value!
// You can also use the smali compiler to create instructions. // You can also use the smali compiler to create instructions.
// For this sake of example I reuse the TestClass field dummyField inside the virtual register 0. // For this sake of example I reuse the TestClass field dummyField inside the virtual register 0.
// //
// Control flow instructions are not supported as of now. // Control flow instructions are not supported as of now.
val instructions = """ method.addInstructions(
startIndex + 2,
"""
invoke-static { }, LTestClass;->returnHello()Ljava/lang/String; invoke-static { }, LTestClass;->returnHello()Ljava/lang/String;
move-result-object v1 move-result-object v1
invoke-virtual { v0, v1 }, Ljava/io/PrintStream;->println(Ljava/lang/String;)V invoke-virtual { v0, v1 }, Ljava/io/PrintStream;->println(Ljava/lang/String;)V
""" """
method.addInstructions(startIndex + 2, instructions) )
// Finally, tell the patcher that this patch was a success. // Finally, tell the patcher that this patch was a success.
// You can also return PatchResultError with a message. // You can also return PatchResultError with a message.

View File

@@ -18,8 +18,8 @@ import org.w3c.dom.Element
@Version("0.0.1") @Version("0.0.1")
class ExampleResourcePatch : ResourcePatch() { class ExampleResourcePatch : ResourcePatch() {
override fun execute(data: ResourceData): PatchResult { override fun execute(data: ResourceData): PatchResult {
data.getXmlEditor("AndroidManifest.xml").use { domFileEditor -> data.xmlEditor["AndroidManifest.xml"].use { editor ->
val element = domFileEditor // regular DomFileEditor val element = editor // regular DomFileEditor
.file .file
.getElementsByTagName("application") .getElementsByTagName("application")
.item(0) as Element .item(0) as Element
@@ -30,18 +30,6 @@ class ExampleResourcePatch : ResourcePatch() {
) )
} }
// iterate through all available resources
data.forEach {
if (it.extension.lowercase() != "xml") return@forEach
data.replace(
it.path,
"\\ddip", // regex supported
"0dip",
true
)
}
return PatchResultSuccess() return PatchResultSuccess()
} }
} }

View File

@@ -0,0 +1,104 @@
package app.revanced.patcher.util.smali
import app.revanced.patcher.extensions.addInstructions
import app.revanced.patcher.extensions.instruction
import app.revanced.patcher.extensions.label
import app.revanced.patcher.util.proxy.mutableTypes.MutableMethod.Companion.toMutable
import org.jf.dexlib2.AccessFlags
import org.jf.dexlib2.Opcode
import org.jf.dexlib2.builder.BuilderInstruction
import org.jf.dexlib2.builder.MutableMethodImplementation
import org.jf.dexlib2.builder.instruction.BuilderInstruction21c
import org.jf.dexlib2.builder.instruction.BuilderInstruction21t
import org.jf.dexlib2.immutable.ImmutableMethod
import org.jf.dexlib2.immutable.reference.ImmutableStringReference
import java.util.*
import kotlin.test.Test
import kotlin.test.assertEquals
import kotlin.test.assertTrue
internal class InlineSmaliCompilerTest {
@Test
fun `compiler should output valid instruction`() {
val want = BuilderInstruction21c(Opcode.CONST_STRING, 0, ImmutableStringReference("Test")) as BuilderInstruction
val have = "const-string v0, \"Test\"".toInstruction()
instructionEquals(want, have)
}
@Test
fun `compiler should support branching with own branches`() {
val method = createMethod()
val insnAmount = 8
val insnIndex = insnAmount - 2
val targetIndex = insnIndex - 1
method.addInstructions(arrayOfNulls<String>(insnAmount).also {
Arrays.fill(it, "const/4 v0, 0x0")
}.joinToString("\n"))
method.addInstructions(
targetIndex,
"""
:test
const/4 v0, 0x1
if-eqz v0, :test
"""
)
val insn = method.instruction(insnIndex) as BuilderInstruction21t
assertEquals(targetIndex, insn.target.location.index)
}
@Test
fun `compiler should support branching to outside branches`() {
val method = createMethod()
val insnIndex = 3
val labelIndex = 1
method.addInstructions(
"""
const/4 v0, 0x1
const/4 v0, 0x0
"""
)
assertEquals(labelIndex, method.label(labelIndex).location.index)
method.addInstructions(
"""
const/4 v0, 0x1
if-eqz v0, :test
return-void
""", listOf(
ExternalLabel("test",method.instruction(1))
)
)
val insn = method.instruction(insnIndex) as BuilderInstruction21t
assertTrue(insn.target.isPlaced, "Label was not placed")
assertEquals(labelIndex, insn.target.location.index)
}
companion object {
private fun createMethod(
name: String = "dummy",
returnType: String = "V",
accessFlags: Int = AccessFlags.STATIC.value,
registerCount: Int = 1,
) = ImmutableMethod(
"Ldummy;",
name,
emptyList(), // parameters
returnType,
accessFlags,
emptySet(),
emptySet(),
MutableMethodImplementation(registerCount)
).toMutable()
private fun instructionEquals(want: BuilderInstruction, have: BuilderInstruction) {
assertEquals(want.opcode, have.opcode)
assertEquals(want.format, have.format)
assertEquals(want.codeUnits, have.codeUnits)
}
}
}