You've already forked revanced-patcher
mirror of
https://github.com/revanced/revanced-patcher
synced 2025-09-06 16:38:50 +02:00
Compare commits
37 Commits
Author | SHA1 | Date | |
---|---|---|---|
![]() |
72783a5e74 | ||
![]() |
a379b69eeb | ||
![]() |
0a8ccba33e | ||
![]() |
519359a9eb | ||
![]() |
b615ed6aab | ||
![]() |
d718134ab2 | ||
![]() |
5e681ed381 | ||
![]() |
6e1b6479b6 | ||
![]() |
f3c9e28a62 | ||
![]() |
d5d6f85084 | ||
![]() |
b8151ebccb | ||
![]() |
5650e34432 | ||
![]() |
c893d16d52 | ||
![]() |
34f08bf206 | ||
![]() |
f02a42610b | ||
![]() |
c95e6fa92f | ||
![]() |
fd738e723b | ||
![]() |
b1d1956323 | ||
![]() |
725a8012ac | ||
![]() |
bb9a73e53b | ||
![]() |
ef2de35a74 | ||
![]() |
2a453d51a8 | ||
![]() |
43d6868d1f | ||
![]() |
cea9379b32 | ||
![]() |
a12fe7dd9e | ||
![]() |
efdd01a988 | ||
![]() |
eafe1c631f | ||
![]() |
aacf900764 | ||
![]() |
f82494e9bb | ||
![]() |
1e0ffa176e | ||
![]() |
b7eb2d2249 | ||
![]() |
b6d6a7591b | ||
![]() |
8f1c835299 | ||
![]() |
a188c16a99 | ||
![]() |
3e6804f06c | ||
![]() |
526a3d7c35 | ||
![]() |
28fc6a2ddd |
23
.github/workflows/release.yml
vendored
23
.github/workflows/release.yml
vendored
@@ -23,17 +23,20 @@ jobs:
|
||||
# https://github.com/cycjimmy/semantic-release-action#private-packages
|
||||
persist-credentials: false
|
||||
fetch-depth: 0
|
||||
- name: Setup JDK
|
||||
uses: actions/setup-java@v3
|
||||
- name: Cache
|
||||
uses: actions/cache@v3
|
||||
with:
|
||||
java-version: '17'
|
||||
distribution: 'zulu'
|
||||
cache: gradle
|
||||
- name: Setup Node.js
|
||||
uses: actions/setup-node@v3
|
||||
with:
|
||||
node-version: "18"
|
||||
cache: 'npm'
|
||||
path: |
|
||||
${{ runner.home }}/.gradle/caches
|
||||
${{ runner.home }}/.gradle/wrapper
|
||||
.gradle
|
||||
build
|
||||
node_modules
|
||||
key: ${{ runner.os }}-gradle-npm-${{ hashFiles('**/*.gradle*', '**/gradle-wrapper.properties', 'package-lock.json') }}
|
||||
- name: Build with Gradle
|
||||
env:
|
||||
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
|
||||
run: ./gradlew clean --no-daemon
|
||||
- name: Setup semantic-release
|
||||
run: npm install
|
||||
- name: Release
|
||||
|
138
CHANGELOG.md
138
CHANGELOG.md
@@ -1,3 +1,141 @@
|
||||
## [11.0.2](https://github.com/revanced/revanced-patcher/compare/v11.0.1...v11.0.2) (2023-06-27)
|
||||
|
||||
|
||||
### Bug Fixes
|
||||
|
||||
* catch exceptions from closing patches ([d5d6f85](https://github.com/revanced/revanced-patcher/commit/d5d6f85084c03ed9c776632823ca12394a716167))
|
||||
* do not load annotations as patches ([519359a](https://github.com/revanced/revanced-patcher/commit/519359a9eb0e9dfa390c5016e9fe4a7490b8ab18))
|
||||
* only close succeeded patches ([b8151eb](https://github.com/revanced/revanced-patcher/commit/b8151ebccb5b27dd9e06fa63235cf9baeef1c0ee))
|
||||
* use `versionCode` if `versionName` is unavailable ([6e1b647](https://github.com/revanced/revanced-patcher/commit/6e1b6479b677657c226693e9cc6b63f4ef2ee060))
|
||||
|
||||
|
||||
### Performance Improvements
|
||||
|
||||
* resolve fingerprints using method maps ([#185](https://github.com/revanced/revanced-patcher/issues/185)) ([d718134](https://github.com/revanced/revanced-patcher/commit/d718134ab26423e02708e01eba711737f9260ba0))
|
||||
|
||||
## [11.0.2-dev.4](https://github.com/revanced/revanced-patcher/compare/v11.0.2-dev.3...v11.0.2-dev.4) (2023-06-27)
|
||||
|
||||
|
||||
### Bug Fixes
|
||||
|
||||
* do not load annotations as patches ([519359a](https://github.com/revanced/revanced-patcher/commit/519359a9eb0e9dfa390c5016e9fe4a7490b8ab18))
|
||||
|
||||
## [11.0.2-dev.3](https://github.com/revanced/revanced-patcher/compare/v11.0.2-dev.2...v11.0.2-dev.3) (2023-06-27)
|
||||
|
||||
|
||||
### Performance Improvements
|
||||
|
||||
* resolve fingerprints using method maps ([#185](https://github.com/revanced/revanced-patcher/issues/185)) ([d718134](https://github.com/revanced/revanced-patcher/commit/d718134ab26423e02708e01eba711737f9260ba0))
|
||||
|
||||
## [11.0.2-dev.2](https://github.com/revanced/revanced-patcher/compare/v11.0.2-dev.1...v11.0.2-dev.2) (2023-06-18)
|
||||
|
||||
|
||||
### Bug Fixes
|
||||
|
||||
* use `versionCode` if `versionName` is unavailable ([6e1b647](https://github.com/revanced/revanced-patcher/commit/6e1b6479b677657c226693e9cc6b63f4ef2ee060))
|
||||
|
||||
## [11.0.2-dev.1](https://github.com/revanced/revanced-patcher/compare/v11.0.1...v11.0.2-dev.1) (2023-06-14)
|
||||
|
||||
|
||||
### Bug Fixes
|
||||
|
||||
* catch exceptions from closing patches ([d5d6f85](https://github.com/revanced/revanced-patcher/commit/d5d6f85084c03ed9c776632823ca12394a716167))
|
||||
* only close succeeded patches ([b8151eb](https://github.com/revanced/revanced-patcher/commit/b8151ebccb5b27dd9e06fa63235cf9baeef1c0ee))
|
||||
|
||||
## [11.0.1](https://github.com/revanced/revanced-patcher/compare/v11.0.0...v11.0.1) (2023-06-12)
|
||||
|
||||
|
||||
### Bug Fixes
|
||||
|
||||
* revert using `OutputStream.nullOutputStream` ([f02a426](https://github.com/revanced/revanced-patcher/commit/f02a42610b7698fc8cc6bc5183c9ccba2ed96cda))
|
||||
|
||||
## [11.0.1-dev.1](https://github.com/revanced/revanced-patcher/compare/v11.0.0...v11.0.1-dev.1) (2023-06-12)
|
||||
|
||||
|
||||
### Bug Fixes
|
||||
|
||||
* revert using `OutputStream.nullOutputStream` ([f02a426](https://github.com/revanced/revanced-patcher/commit/f02a42610b7698fc8cc6bc5183c9ccba2ed96cda))
|
||||
|
||||
# [11.0.0](https://github.com/revanced/revanced-patcher/compare/v10.0.0...v11.0.0) (2023-06-10)
|
||||
|
||||
|
||||
### Bug Fixes
|
||||
|
||||
* add imports to fix failing tests ([43d6868](https://github.com/revanced/revanced-patcher/commit/43d6868d1f59922f9812f3e4a2a890f3b331def6))
|
||||
|
||||
|
||||
* refactor!: move extension functions to their corresponding classes ([a12fe7d](https://github.com/revanced/revanced-patcher/commit/a12fe7dd9e976c38a0a82fe35e6650f58f815de4))
|
||||
* refactor!: use proper extension function names ([efdd01a](https://github.com/revanced/revanced-patcher/commit/efdd01a9886b6f06af213731824621964367b2a3))
|
||||
* fix!: implement extension functions consistently ([aacf900](https://github.com/revanced/revanced-patcher/commit/aacf9007647b1cc11bc40053625802573efda6ef))
|
||||
|
||||
|
||||
### BREAKING CHANGES
|
||||
|
||||
* This changes the import paths for extension functions.
|
||||
* This changes the names of extension functions
|
||||
* This changes the name of functions
|
||||
|
||||
# [11.0.0-dev.2](https://github.com/revanced/revanced-patcher/compare/v11.0.0-dev.1...v11.0.0-dev.2) (2023-06-09)
|
||||
|
||||
|
||||
### Bug Fixes
|
||||
|
||||
* add imports to fix failing tests ([43d6868](https://github.com/revanced/revanced-patcher/commit/43d6868d1f59922f9812f3e4a2a890f3b331def6))
|
||||
|
||||
# [11.0.0-dev.1](https://github.com/revanced/revanced-patcher/compare/v10.0.0...v11.0.0-dev.1) (2023-06-07)
|
||||
|
||||
|
||||
* refactor!: move extension functions to their corresponding classes ([a12fe7d](https://github.com/revanced/revanced-patcher/commit/a12fe7dd9e976c38a0a82fe35e6650f58f815de4))
|
||||
* refactor!: use proper extension function names ([efdd01a](https://github.com/revanced/revanced-patcher/commit/efdd01a9886b6f06af213731824621964367b2a3))
|
||||
* fix!: implement extension functions consistently ([aacf900](https://github.com/revanced/revanced-patcher/commit/aacf9007647b1cc11bc40053625802573efda6ef))
|
||||
|
||||
|
||||
### BREAKING CHANGES
|
||||
|
||||
* This changes the import paths for extension functions.
|
||||
* This changes the names of extension functions
|
||||
* This changes the name of functions
|
||||
|
||||
# [10.0.0](https://github.com/revanced/revanced-patcher/compare/v9.0.0...v10.0.0) (2023-06-07)
|
||||
|
||||
|
||||
* fix!: check for two methods parameters orders (#183) ([b6d6a75](https://github.com/revanced/revanced-patcher/commit/b6d6a7591ba1c0b48ffdef52352709564da8d9be)), closes [#183](https://github.com/revanced/revanced-patcher/issues/183)
|
||||
|
||||
|
||||
### BREAKING CHANGES
|
||||
|
||||
* This requires changes to `MethodFingerprint`
|
||||
|
||||
# [10.0.0-dev.1](https://github.com/revanced/revanced-patcher/compare/v9.0.0...v10.0.0-dev.1) (2023-06-07)
|
||||
|
||||
|
||||
* fix!: check for two methods parameters orders (#183) ([b6d6a75](https://github.com/revanced/revanced-patcher/commit/b6d6a7591ba1c0b48ffdef52352709564da8d9be)), closes [#183](https://github.com/revanced/revanced-patcher/issues/183)
|
||||
|
||||
|
||||
### BREAKING CHANGES
|
||||
|
||||
* This requires changes to `MethodFingerprint`
|
||||
|
||||
# [9.0.0](https://github.com/revanced/revanced-patcher/compare/v8.0.0...v9.0.0) (2023-05-23)
|
||||
|
||||
|
||||
* refactor!: rename parameter ([526a3d7](https://github.com/revanced/revanced-patcher/commit/526a3d7c359e2d95d26756da0f88d5ce975f5d9b))
|
||||
|
||||
|
||||
### BREAKING CHANGES
|
||||
|
||||
* This changes named parameters.
|
||||
|
||||
# [9.0.0-dev.1](https://github.com/revanced/revanced-patcher/compare/v8.0.0...v9.0.0-dev.1) (2023-05-23)
|
||||
|
||||
|
||||
* refactor!: rename parameter ([526a3d7](https://github.com/revanced/revanced-patcher/commit/526a3d7c359e2d95d26756da0f88d5ce975f5d9b))
|
||||
|
||||
|
||||
### BREAKING CHANGES
|
||||
|
||||
* This changes named parameters.
|
||||
|
||||
# [8.0.0](https://github.com/revanced/revanced-patcher/compare/v7.1.1...v8.0.0) (2023-05-13)
|
||||
|
||||
|
||||
|
@@ -1,2 +1,2 @@
|
||||
kotlin.code.style = official
|
||||
version = 8.0.0
|
||||
version = 11.0.2
|
||||
|
@@ -4,8 +4,8 @@ import app.revanced.patcher.data.Context
|
||||
import app.revanced.patcher.extensions.PatchExtensions.dependencies
|
||||
import app.revanced.patcher.extensions.PatchExtensions.patchName
|
||||
import app.revanced.patcher.extensions.PatchExtensions.requiresIntegrations
|
||||
import app.revanced.patcher.extensions.nullOutputStream
|
||||
import app.revanced.patcher.fingerprint.method.impl.MethodFingerprint.Companion.resolve
|
||||
import app.revanced.patcher.fingerprint.method.impl.MethodFingerprint
|
||||
import app.revanced.patcher.fingerprint.method.impl.MethodFingerprint.Companion.resolveUsingLookupMap
|
||||
import app.revanced.patcher.patch.*
|
||||
import app.revanced.patcher.util.VersionReader
|
||||
import brut.androlib.Androlib
|
||||
@@ -24,7 +24,9 @@ import lanchon.multidexlib2.MultiDexIO
|
||||
import org.jf.dexlib2.Opcodes
|
||||
import org.jf.dexlib2.iface.DexFile
|
||||
import org.jf.dexlib2.writer.io.MemoryDataStore
|
||||
import java.io.Closeable
|
||||
import java.io.File
|
||||
import java.io.OutputStream
|
||||
import java.nio.file.Files
|
||||
|
||||
internal val NAMER = BasicDexFileNamer()
|
||||
@@ -247,7 +249,13 @@ class Patcher(private val options: PatcherOptions) {
|
||||
XmlPullStreamDecoder(
|
||||
axmlParser, AndrolibResources().resXmlSerializer
|
||||
).decodeManifest(
|
||||
extInputFile.directory.getFileInput("AndroidManifest.xml"), nullOutputStream
|
||||
extInputFile.directory.getFileInput("AndroidManifest.xml"),
|
||||
// Older Android versions do not support OutputStream.nullOutputStream()
|
||||
object : OutputStream() {
|
||||
override fun write(b: Int) {
|
||||
// do nothing
|
||||
}
|
||||
}
|
||||
)
|
||||
}
|
||||
}
|
||||
@@ -255,7 +263,7 @@ class Patcher(private val options: PatcherOptions) {
|
||||
// read of the resourceTable which is created by reading the manifest file
|
||||
context.packageMetadata.let { metadata ->
|
||||
metadata.packageName = resourceTable.currentResPackage.name
|
||||
metadata.packageVersion = resourceTable.versionInfo.versionName
|
||||
metadata.packageVersion = resourceTable.versionInfo.versionName ?: resourceTable.versionInfo.versionCode
|
||||
metadata.metaInfo.versionInfo = resourceTable.versionInfo
|
||||
metadata.metaInfo.sdkInfo = resourceTable.sdkInfo
|
||||
}
|
||||
@@ -315,10 +323,7 @@ class Patcher(private val options: PatcherOptions) {
|
||||
context.resourceContext
|
||||
} else {
|
||||
context.bytecodeContext.also { context ->
|
||||
(patchInstance as BytecodePatch).fingerprints?.resolve(
|
||||
context,
|
||||
context.classes.classes
|
||||
)
|
||||
(patchInstance as BytecodePatch).fingerprints?.resolveUsingLookupMap(context)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -338,31 +343,52 @@ class Patcher(private val options: PatcherOptions) {
|
||||
return sequence {
|
||||
if (mergeIntegrations) context.integrations.merge(logger)
|
||||
|
||||
logger.trace("Initialize lookup maps for method MethodFingerprint resolution")
|
||||
|
||||
MethodFingerprint.initializeFingerprintResolutionLookupMaps(context.bytecodeContext)
|
||||
|
||||
// prevent from decoding the manifest twice if it is not needed
|
||||
if (resourceDecodingMode == ResourceDecodingMode.FULL) decodeResources(ResourceDecodingMode.FULL)
|
||||
|
||||
logger.trace("Executing all patches")
|
||||
logger.info("Executing patches")
|
||||
|
||||
val executedPatches = LinkedHashMap<String, ExecutedPatch>() // first is name
|
||||
|
||||
try {
|
||||
context.patches.forEach { patch ->
|
||||
val patchResult = executePatch(patch, executedPatches)
|
||||
context.patches.forEach { patch ->
|
||||
val patchResult = executePatch(patch, executedPatches)
|
||||
|
||||
val result = if (patchResult.isSuccess()) {
|
||||
Result.success(patchResult.success()!!)
|
||||
} else {
|
||||
Result.failure(patchResult.error()!!)
|
||||
}
|
||||
val result = if (patchResult.isSuccess()) {
|
||||
Result.success(patchResult.success()!!)
|
||||
} else {
|
||||
Result.failure(patchResult.error()!!)
|
||||
}
|
||||
|
||||
yield(patch.patchName to result)
|
||||
if (stopOnError && patchResult.isError()) return@sequence
|
||||
}
|
||||
} finally {
|
||||
executedPatches.values.reversed().forEach { (patch, _) ->
|
||||
patch.close()
|
||||
}
|
||||
// TODO: This prints before the patch really finishes in case it is a Closeable
|
||||
// because the Closeable is closed after all patches are executed.
|
||||
yield(patch.patchName to result)
|
||||
|
||||
if (stopOnError && patchResult.isError()) return@sequence
|
||||
}
|
||||
|
||||
executedPatches.values
|
||||
.filter(ExecutedPatch::success)
|
||||
.map(ExecutedPatch::patchInstance)
|
||||
.filterIsInstance(Closeable::class.java)
|
||||
.asReversed().forEach {
|
||||
try {
|
||||
it.close()
|
||||
} catch (exception: Exception) {
|
||||
val patchName = (it as Patch<Context>).javaClass.patchName
|
||||
|
||||
logger.error("Failed to close '$patchName': ${exception.stackTraceToString()}")
|
||||
|
||||
yield(patchName to Result.failure(exception))
|
||||
|
||||
// This is not failsafe. If a patch throws an exception while closing,
|
||||
// the other patches that depend on it may fail.
|
||||
if (stopOnError) return@sequence
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
@@ -1,83 +1,33 @@
|
||||
package app.revanced.patcher.extensions
|
||||
|
||||
import app.revanced.patcher.annotation.*
|
||||
import app.revanced.patcher.data.Context
|
||||
import app.revanced.patcher.fingerprint.method.annotation.FuzzyPatternScanMethod
|
||||
import app.revanced.patcher.fingerprint.method.impl.MethodFingerprint
|
||||
import app.revanced.patcher.patch.OptionsContainer
|
||||
import app.revanced.patcher.patch.Patch
|
||||
import app.revanced.patcher.patch.PatchOptions
|
||||
import app.revanced.patcher.patch.annotations.DependsOn
|
||||
import app.revanced.patcher.patch.annotations.RequiresIntegrations
|
||||
import kotlin.reflect.KClass
|
||||
import kotlin.reflect.KVisibility
|
||||
import kotlin.reflect.full.companionObject
|
||||
import kotlin.reflect.full.companionObjectInstance
|
||||
|
||||
/**
|
||||
* Recursively find a given annotation on a class.
|
||||
* @param targetAnnotation The annotation to find.
|
||||
* @return The annotation.
|
||||
*/
|
||||
private fun <T : Annotation> Class<*>.findAnnotationRecursively(targetAnnotation: KClass<T>) =
|
||||
this.findAnnotationRecursively(targetAnnotation.java, mutableSetOf())
|
||||
internal object AnnotationExtensions {
|
||||
/**
|
||||
* Recursively find a given annotation on a class.
|
||||
*
|
||||
* @param targetAnnotation The annotation to find.
|
||||
* @return The annotation.
|
||||
*/
|
||||
fun <T : Annotation> Class<*>.findAnnotationRecursively(targetAnnotation: KClass<T>): T? {
|
||||
fun <T : Annotation> Class<*>.findAnnotationRecursively(
|
||||
targetAnnotation: Class<T>, traversed: MutableSet<Annotation>
|
||||
): T? {
|
||||
val found = this.annotations.firstOrNull { it.annotationClass.java.name == targetAnnotation.name }
|
||||
|
||||
@Suppress("UNCHECKED_CAST") if (found != null) return found as T
|
||||
|
||||
private fun <T : Annotation> Class<*>.findAnnotationRecursively(
|
||||
targetAnnotation: Class<T>, traversed: MutableSet<Annotation>
|
||||
): T? {
|
||||
val found = this.annotations.firstOrNull { it.annotationClass.java.name == targetAnnotation.name }
|
||||
for (annotation in this.annotations) {
|
||||
if (traversed.contains(annotation)) continue
|
||||
traversed.add(annotation)
|
||||
|
||||
@Suppress("UNCHECKED_CAST") if (found != null) return found as T
|
||||
|
||||
for (annotation in this.annotations) {
|
||||
if (traversed.contains(annotation)) continue
|
||||
traversed.add(annotation)
|
||||
|
||||
return (annotation.annotationClass.java.findAnnotationRecursively(targetAnnotation, traversed)) ?: continue
|
||||
}
|
||||
|
||||
return null
|
||||
}
|
||||
|
||||
object PatchExtensions {
|
||||
val Class<out Patch<Context>>.patchName: String
|
||||
get() = findAnnotationRecursively(Name::class)?.name ?: this.simpleName
|
||||
|
||||
val Class<out Patch<Context>>.version
|
||||
get() = findAnnotationRecursively(Version::class)?.version
|
||||
|
||||
val Class<out Patch<Context>>.include
|
||||
get() = findAnnotationRecursively(app.revanced.patcher.patch.annotations.Patch::class)!!.include
|
||||
|
||||
val Class<out Patch<Context>>.description
|
||||
get() = findAnnotationRecursively(Description::class)?.description
|
||||
|
||||
val Class<out Patch<Context>>.dependencies
|
||||
get() = findAnnotationRecursively(DependsOn::class)?.dependencies
|
||||
|
||||
val Class<out Patch<Context>>.compatiblePackages
|
||||
get() = findAnnotationRecursively(Compatibility::class)?.compatiblePackages
|
||||
|
||||
internal val Class<out Patch<Context>>.requiresIntegrations
|
||||
get() = findAnnotationRecursively(RequiresIntegrations::class) != null
|
||||
|
||||
val Class<out Patch<Context>>.options: PatchOptions?
|
||||
get() = kotlin.companionObject?.let { cl ->
|
||||
if (cl.visibility != KVisibility.PUBLIC) return null
|
||||
kotlin.companionObjectInstance?.let {
|
||||
(it as? OptionsContainer)?.options
|
||||
return (annotation.annotationClass.java.findAnnotationRecursively(targetAnnotation, traversed))
|
||||
?: continue
|
||||
}
|
||||
|
||||
return null
|
||||
}
|
||||
}
|
||||
|
||||
object MethodFingerprintExtensions {
|
||||
val MethodFingerprint.name: String
|
||||
get() = this.javaClass.simpleName
|
||||
|
||||
val MethodFingerprint.fuzzyPatternScanMethod
|
||||
get() = javaClass.findAnnotationRecursively(FuzzyPatternScanMethod::class)
|
||||
|
||||
val MethodFingerprint.fuzzyScanThreshold
|
||||
get() = fuzzyPatternScanMethod?.threshold ?: 0
|
||||
return this.findAnnotationRecursively(targetAnnotation.java, mutableSetOf())
|
||||
}
|
||||
}
|
@@ -1,238 +1,33 @@
|
||||
package app.revanced.patcher.extensions
|
||||
|
||||
import app.revanced.patcher.util.proxy.mutableTypes.MutableMethod
|
||||
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.toInstructions
|
||||
import org.jf.dexlib2.AccessFlags
|
||||
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.instruction.*
|
||||
import org.jf.dexlib2.iface.Method
|
||||
import org.jf.dexlib2.iface.instruction.Instruction
|
||||
import org.jf.dexlib2.immutable.ImmutableMethod
|
||||
import org.jf.dexlib2.immutable.ImmutableMethodImplementation
|
||||
import java.io.OutputStream
|
||||
|
||||
infix fun AccessFlags.or(other: AccessFlags) = this.value or other.value
|
||||
infix fun Int.or(other: AccessFlags) = this or other.value
|
||||
|
||||
fun MutableMethodImplementation.addInstructions(index: Int, instructions: List<BuilderInstruction>) {
|
||||
for (i in instructions.lastIndex downTo 0) {
|
||||
this.addInstruction(index, instructions[i])
|
||||
}
|
||||
}
|
||||
|
||||
fun MutableMethodImplementation.addInstructions(instructions: List<BuilderInstruction>) {
|
||||
for (instruction in instructions) {
|
||||
this.addInstruction(instruction)
|
||||
}
|
||||
}
|
||||
|
||||
fun MutableMethodImplementation.replaceInstructions(index: Int, instructions: List<BuilderInstruction>) {
|
||||
for (i in instructions.lastIndex downTo 0) {
|
||||
this.replaceInstruction(index + i, instructions[i])
|
||||
}
|
||||
}
|
||||
|
||||
fun MutableMethodImplementation.removeInstructions(index: Int, count: Int) {
|
||||
for (i in count - 1 downTo 0) {
|
||||
this.removeInstruction(index + i)
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Clones the method.
|
||||
* @param registerCount This parameter allows you to change the register count of the method.
|
||||
* This may be a positive or negative number.
|
||||
* @return The **immutable** cloned method. Call [toMutable] or [cloneMutable] to get a **mutable** copy.
|
||||
*/
|
||||
internal fun Method.clone(registerCount: Int = 0): ImmutableMethod {
|
||||
val clonedImplementation = implementation?.let {
|
||||
ImmutableMethodImplementation(
|
||||
it.registerCount + registerCount,
|
||||
it.instructions,
|
||||
it.tryBlocks,
|
||||
it.debugItems,
|
||||
)
|
||||
}
|
||||
return ImmutableMethod(
|
||||
returnType, name, parameters, returnType, accessFlags, annotations, hiddenApiRestrictions, clonedImplementation
|
||||
)
|
||||
}
|
||||
|
||||
/**
|
||||
* Add a smali instruction to the method.
|
||||
* @param instruction The smali instruction to add.
|
||||
*/
|
||||
fun MutableMethod.addInstruction(instruction: String) =
|
||||
this.implementation!!.addInstruction(instruction.toInstruction(this))
|
||||
|
||||
/**
|
||||
* Add a smali instruction to the method.
|
||||
* @param index The index to insert the instruction at.
|
||||
* @param instruction The smali instruction to add.
|
||||
*/
|
||||
fun MutableMethod.addInstruction(index: Int, instruction: String) =
|
||||
this.implementation!!.addInstruction(index, instruction.toInstruction(this))
|
||||
|
||||
/**
|
||||
* Replace a smali instruction within the method.
|
||||
* @param index The index to replace the instruction at.
|
||||
* @param instruction The smali instruction to place.
|
||||
*/
|
||||
fun MutableMethod.replaceInstruction(index: Int, instruction: String) =
|
||||
this.implementation!!.replaceInstruction(index, instruction.toInstruction(this))
|
||||
|
||||
/**
|
||||
* Remove a smali instruction within the method.
|
||||
* @param index The index to delete the instruction at.
|
||||
*/
|
||||
fun MutableMethod.removeInstruction(index: Int) = this.implementation!!.removeInstruction(index)
|
||||
|
||||
/**
|
||||
* Create a label for the instruction at given index in the method's implementation.
|
||||
* Create a label for the instruction at given index.
|
||||
*
|
||||
* @param index The index to create the label for the instruction at.
|
||||
* @return The label.
|
||||
*/
|
||||
fun MutableMethod.label(index: Int) = this.implementation!!.newLabelForIndex(index)
|
||||
fun MutableMethod.newLabel(index: Int) = implementation!!.newLabelForIndex(index)
|
||||
|
||||
/**
|
||||
* Get an instruction at the given index in the method's implementation.
|
||||
* @param index The index to get the instruction at.
|
||||
* @return The instruction.
|
||||
* Perform a bitwise OR operation between two [AccessFlags].
|
||||
*
|
||||
* @param other The other [AccessFlags] to perform the operation with.
|
||||
*/
|
||||
fun MutableMethod.instruction(index: Int): BuilderInstruction = this.implementation!!.instructions[index]
|
||||
infix fun AccessFlags.or(other: AccessFlags) = value or other.value
|
||||
|
||||
/**
|
||||
* Get an instruction at the given index in the method's implementation.
|
||||
* @param index The index to get the instruction at.
|
||||
* @param T The type of instruction to return.
|
||||
* @return The instruction.
|
||||
* Perform a bitwise OR operation between an [AccessFlags] and an [Int].
|
||||
*
|
||||
* @param other The [Int] to perform the operation with.
|
||||
*/
|
||||
fun <T> MutableMethod.instruction(index: Int): T = instruction(index) as T
|
||||
infix fun Int.or(other: AccessFlags) = this or other.value
|
||||
|
||||
/**
|
||||
* Add smali instructions to the method.
|
||||
* @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.
|
||||
* Perform a bitwise OR operation between an [Int] and an [AccessFlags].
|
||||
*
|
||||
* @param other The [AccessFlags] to perform the operation with.
|
||||
*/
|
||||
|
||||
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.subList(0, compiledInstructions.size - externalLabels.size))
|
||||
|
||||
val methodInstructions = methodImplementation.instructions
|
||||
methodInstructions.subList(index, index + compiledInstructions.size - externalLabels.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.
|
||||
*/
|
||||
fun MutableMethod.addInstructions(instructions: String, labels: List<ExternalLabel> = emptyList()) =
|
||||
this.addInstructions(this.implementation!!.instructions.size, instructions, labels)
|
||||
|
||||
/**
|
||||
* Replace smali instructions within the method.
|
||||
* @param index The index to replace the instructions at.
|
||||
* @param instructions The smali instructions to place.
|
||||
*/
|
||||
fun MutableMethod.replaceInstructions(index: Int, instructions: String) =
|
||||
this.implementation!!.replaceInstructions(index, instructions.toInstructions(this))
|
||||
|
||||
/**
|
||||
* Remove smali instructions from the method.
|
||||
* @param index The index to remove the instructions at.
|
||||
* @param count The amount of instructions to remove.
|
||||
*/
|
||||
fun MutableMethod.removeInstructions(index: Int, count: Int) = 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.
|
||||
* @param registerCount This parameter allows you to change the register count of the method.
|
||||
* This may be a positive or negative number.
|
||||
* @return The **mutable** cloned method. Call [clone] to get an **immutable** copy.
|
||||
*/
|
||||
internal fun Method.cloneMutable(registerCount: Int = 0) = clone(registerCount).toMutable()
|
||||
|
||||
// FIXME: also check the order of parameters as different order equals different method overload
|
||||
internal fun parametersEqual(
|
||||
parameters1: Iterable<CharSequence>, parameters2: Iterable<CharSequence>
|
||||
): Boolean {
|
||||
return parameters1.count() == parameters2.count() && parameters1.all { parameter ->
|
||||
parameters2.any {
|
||||
it.startsWith(
|
||||
parameter
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
internal val nullOutputStream = object : OutputStream() {
|
||||
override fun write(b: Int) {}
|
||||
}
|
||||
infix fun AccessFlags.or(other: Int) = value or other
|
File diff suppressed because it is too large
Load Diff
@@ -0,0 +1,20 @@
|
||||
package app.revanced.patcher.extensions
|
||||
|
||||
import app.revanced.patcher.extensions.AnnotationExtensions.findAnnotationRecursively
|
||||
import app.revanced.patcher.fingerprint.method.annotation.FuzzyPatternScanMethod
|
||||
import app.revanced.patcher.fingerprint.method.impl.MethodFingerprint
|
||||
|
||||
object MethodFingerprintExtensions {
|
||||
|
||||
/**
|
||||
* The name of a [MethodFingerprint].
|
||||
*/
|
||||
val MethodFingerprint.name: String
|
||||
get() = this.javaClass.simpleName
|
||||
|
||||
/**
|
||||
* The [FuzzyPatternScanMethod] annotation of a [MethodFingerprint].
|
||||
*/
|
||||
val MethodFingerprint.fuzzyPatternScanMethod
|
||||
get() = javaClass.findAnnotationRecursively(FuzzyPatternScanMethod::class)
|
||||
}
|
@@ -0,0 +1,71 @@
|
||||
package app.revanced.patcher.extensions
|
||||
|
||||
import app.revanced.patcher.annotation.Compatibility
|
||||
import app.revanced.patcher.annotation.Description
|
||||
import app.revanced.patcher.annotation.Name
|
||||
import app.revanced.patcher.annotation.Version
|
||||
import app.revanced.patcher.data.Context
|
||||
import app.revanced.patcher.extensions.AnnotationExtensions.findAnnotationRecursively
|
||||
import app.revanced.patcher.patch.OptionsContainer
|
||||
import app.revanced.patcher.patch.Patch
|
||||
import app.revanced.patcher.patch.PatchOptions
|
||||
import app.revanced.patcher.patch.annotations.DependsOn
|
||||
import app.revanced.patcher.patch.annotations.RequiresIntegrations
|
||||
import kotlin.reflect.KVisibility
|
||||
import kotlin.reflect.full.companionObject
|
||||
import kotlin.reflect.full.companionObjectInstance
|
||||
|
||||
object PatchExtensions {
|
||||
/**
|
||||
* The name of a [Patch].
|
||||
*/
|
||||
val Class<out Patch<Context>>.patchName: String
|
||||
get() = findAnnotationRecursively(Name::class)?.name ?: this.simpleName
|
||||
|
||||
/**
|
||||
* The version of a [Patch].
|
||||
*/
|
||||
val Class<out Patch<Context>>.version
|
||||
get() = findAnnotationRecursively(Version::class)?.version
|
||||
|
||||
/**
|
||||
* Weather or not a [Patch] should be included.
|
||||
*/
|
||||
val Class<out Patch<Context>>.include
|
||||
get() = findAnnotationRecursively(app.revanced.patcher.patch.annotations.Patch::class)!!.include
|
||||
|
||||
/**
|
||||
* The description of a [Patch].
|
||||
*/
|
||||
val Class<out Patch<Context>>.description
|
||||
get() = findAnnotationRecursively(Description::class)?.description
|
||||
|
||||
/**
|
||||
* The dependencies of a [Patch].
|
||||
*/
|
||||
val Class<out Patch<Context>>.dependencies
|
||||
get() = findAnnotationRecursively(DependsOn::class)?.dependencies
|
||||
|
||||
/**
|
||||
* The packages a [Patch] is compatible with.
|
||||
*/
|
||||
val Class<out Patch<Context>>.compatiblePackages
|
||||
get() = findAnnotationRecursively(Compatibility::class)?.compatiblePackages
|
||||
|
||||
/**
|
||||
* Weather or not a [Patch] requires integrations.
|
||||
*/
|
||||
internal val Class<out Patch<Context>>.requiresIntegrations
|
||||
get() = findAnnotationRecursively(RequiresIntegrations::class) != null
|
||||
|
||||
/**
|
||||
* The options of a [Patch].
|
||||
*/
|
||||
val Class<out Patch<Context>>.options: PatchOptions?
|
||||
get() = kotlin.companionObject?.let { cl ->
|
||||
if (cl.visibility != KVisibility.PUBLIC) return null
|
||||
kotlin.companionObjectInstance?.let {
|
||||
(it as? OptionsContainer)?.options
|
||||
}
|
||||
}
|
||||
}
|
File diff suppressed because it is too large
Load Diff
@@ -12,7 +12,7 @@ import java.io.Closeable
|
||||
* If it implements [Closeable], it will be closed after all patches have been executed.
|
||||
* Closing will be done in reverse execution order.
|
||||
*/
|
||||
sealed interface Patch<out T : Context> : Closeable {
|
||||
sealed interface Patch<out T : Context> {
|
||||
/**
|
||||
* The main function of the [Patch] which the patcher will call.
|
||||
*
|
||||
@@ -20,13 +20,6 @@ sealed interface Patch<out T : Context> : Closeable {
|
||||
* @return The result of executing the patch.
|
||||
*/
|
||||
fun execute(context: @UnsafeVariance T): PatchResult
|
||||
|
||||
/**
|
||||
* The closing function for this patch.
|
||||
*
|
||||
* This can be treated like popping the patch from the current patch stack.
|
||||
*/
|
||||
override fun close() {}
|
||||
}
|
||||
|
||||
/**
|
||||
|
@@ -3,6 +3,7 @@
|
||||
package app.revanced.patcher.util.patch
|
||||
|
||||
import app.revanced.patcher.data.Context
|
||||
import app.revanced.patcher.extensions.AnnotationExtensions.findAnnotationRecursively
|
||||
import app.revanced.patcher.extensions.PatchExtensions.patchName
|
||||
import app.revanced.patcher.patch.Patch
|
||||
import org.jf.dexlib2.DexFileFactory
|
||||
@@ -19,7 +20,13 @@ sealed class PatchBundle(path: String) : File(path) {
|
||||
internal fun loadPatches(classLoader: ClassLoader, classNames: Iterator<String>) = buildList {
|
||||
classNames.forEach { className ->
|
||||
val clazz = classLoader.loadClass(className)
|
||||
if (!clazz.isAnnotationPresent(app.revanced.patcher.patch.annotations.Patch::class.java)) return@forEach
|
||||
|
||||
// Annotations can not Patch.
|
||||
if (clazz.isAnnotation) return@forEach
|
||||
|
||||
clazz.findAnnotationRecursively(app.revanced.patcher.patch.annotations.Patch::class)
|
||||
?: return@forEach
|
||||
|
||||
@Suppress("UNCHECKED_CAST") this.add(clazz as Class<out Patch<Context>>)
|
||||
}
|
||||
}.sortedBy { it.patchName }
|
||||
@@ -43,11 +50,9 @@ sealed class PatchBundle(path: String) : File(path) {
|
||||
),
|
||||
JarFile(this)
|
||||
.stream()
|
||||
.filter {it.name.endsWith(".class") && !it.name.contains("$")}
|
||||
.map({it -> it.realName.replace('/', '.').replace(".class", "")}).iterator()
|
||||
.filter { it.name.endsWith(".class") && !it.name.contains("$") }
|
||||
.map { it.realName.replace('/', '.').replace(".class", "") }.iterator()
|
||||
)
|
||||
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -63,8 +68,9 @@ sealed class PatchBundle(path: String) : File(path) {
|
||||
* Patches will be loaded to the provided [dexClassLoader].
|
||||
*/
|
||||
fun loadPatches() = loadPatches(dexClassLoader,
|
||||
DexFileFactory.loadDexFile(path, null).classes.asSequence().map({ classDef ->
|
||||
DexFileFactory.loadDexFile(path, null).classes.asSequence().map { classDef ->
|
||||
classDef.type.substring(1, classDef.length - 1).replace('/', '.')
|
||||
}).iterator())
|
||||
}.iterator()
|
||||
)
|
||||
}
|
||||
}
|
@@ -0,0 +1,233 @@
|
||||
package app.revanced.patcher.extensions
|
||||
|
||||
import app.revanced.patcher.extensions.InstructionExtensions.addInstruction
|
||||
import app.revanced.patcher.extensions.InstructionExtensions.addInstructions
|
||||
import app.revanced.patcher.extensions.InstructionExtensions.addInstructionsWithLabels
|
||||
import app.revanced.patcher.extensions.InstructionExtensions.getInstruction
|
||||
import app.revanced.patcher.extensions.InstructionExtensions.removeInstruction
|
||||
import app.revanced.patcher.extensions.InstructionExtensions.removeInstructions
|
||||
import app.revanced.patcher.extensions.InstructionExtensions.replaceInstruction
|
||||
import app.revanced.patcher.extensions.InstructionExtensions.replaceInstructions
|
||||
import app.revanced.patcher.util.proxy.mutableTypes.MutableMethod
|
||||
import app.revanced.patcher.util.proxy.mutableTypes.MutableMethod.Companion.toMutable
|
||||
import app.revanced.patcher.util.smali.ExternalLabel
|
||||
import org.jf.dexlib2.AccessFlags
|
||||
import org.jf.dexlib2.Opcode
|
||||
import org.jf.dexlib2.builder.BuilderOffsetInstruction
|
||||
import org.jf.dexlib2.builder.MutableMethodImplementation
|
||||
import org.jf.dexlib2.builder.instruction.BuilderInstruction21s
|
||||
import org.jf.dexlib2.iface.instruction.OneRegisterInstruction
|
||||
import org.jf.dexlib2.immutable.ImmutableMethod
|
||||
import org.junit.jupiter.api.BeforeEach
|
||||
import org.junit.jupiter.api.Test
|
||||
import kotlin.test.assertEquals
|
||||
|
||||
private object InstructionExtensionsTest {
|
||||
private lateinit var testMethod: MutableMethod
|
||||
private lateinit var testMethodImplementation: MutableMethodImplementation
|
||||
|
||||
@BeforeEach
|
||||
fun createTestMethod() = ImmutableMethod(
|
||||
"TestClass;",
|
||||
"testMethod",
|
||||
null,
|
||||
"V",
|
||||
AccessFlags.PUBLIC.value,
|
||||
null,
|
||||
null,
|
||||
MutableMethodImplementation(16).also { testMethodImplementation = it }.apply {
|
||||
repeat(10) { i -> this.addInstruction(TestInstruction(i)) }
|
||||
},
|
||||
).let { testMethod = it.toMutable() }
|
||||
|
||||
@Test
|
||||
fun addInstructionsToImplementationIndexed() = applyToImplementation {
|
||||
addInstructions(5, getTestInstructions(5..6)).also {
|
||||
assertRegisterIs(5, 5)
|
||||
assertRegisterIs(6, 6)
|
||||
|
||||
assertRegisterIs(5, 7)
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
fun addInstructionsToImplementation() = applyToImplementation {
|
||||
addInstructions(getTestInstructions(10..11)).also {
|
||||
assertRegisterIs(10, 10)
|
||||
assertRegisterIs(11, 11)
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
fun removeInstructionsFromImplementationIndexed() = applyToImplementation {
|
||||
removeInstructions(5, 5).also { assertRegisterIs(4, 4) }
|
||||
}
|
||||
|
||||
@Test
|
||||
fun removeInstructionsFromImplementation() = applyToImplementation {
|
||||
removeInstructions(0).also { assertRegisterIs(9, 9) }
|
||||
removeInstructions(1).also { assertRegisterIs(1, 0) }
|
||||
removeInstructions(2).also { assertRegisterIs(3, 0) }
|
||||
}
|
||||
|
||||
@Test
|
||||
fun replaceInstructionsInImplementationIndexed() = applyToImplementation {
|
||||
replaceInstructions(5, getTestInstructions(0..1)).also {
|
||||
assertRegisterIs(0, 5)
|
||||
assertRegisterIs(1, 6)
|
||||
assertRegisterIs(7, 7)
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
fun addInstructionToMethodIndexed() = applyToMethod {
|
||||
addInstruction(5, TestInstruction(0)).also { assertRegisterIs(0, 5) }
|
||||
}
|
||||
|
||||
@Test
|
||||
fun addInstructionToMethod() = applyToMethod {
|
||||
addInstruction(TestInstruction(0)).also { assertRegisterIs(0, 10) }
|
||||
}
|
||||
|
||||
@Test
|
||||
fun addSmaliInstructionToMethodIndexed() = applyToMethod {
|
||||
addInstruction(5, getTestSmaliInstruction(0)).also { assertRegisterIs(0, 5) }
|
||||
}
|
||||
|
||||
@Test
|
||||
fun addSmaliInstructionToMethod() = applyToMethod {
|
||||
addInstruction(getTestSmaliInstruction(0)).also { assertRegisterIs(0, 10) }
|
||||
}
|
||||
|
||||
@Test
|
||||
fun addInstructionsToMethodIndexed() = applyToMethod {
|
||||
addInstructions(5, getTestInstructions(0..1)).also {
|
||||
assertRegisterIs(0, 5)
|
||||
assertRegisterIs(1, 6)
|
||||
|
||||
assertRegisterIs(5, 7)
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
fun addInstructionsToMethod() = applyToMethod {
|
||||
addInstructions(getTestInstructions(0..1)).also {
|
||||
assertRegisterIs(0, 10)
|
||||
assertRegisterIs(1, 11)
|
||||
|
||||
assertRegisterIs(9, 9)
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
fun addSmaliInstructionsToMethodIndexed() = applyToMethod {
|
||||
addInstructionsWithLabels(5, getTestSmaliInstructions(0..1)).also {
|
||||
assertRegisterIs(0, 5)
|
||||
assertRegisterIs(1, 6)
|
||||
|
||||
assertRegisterIs(5, 7)
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
fun addSmaliInstructionsToMethod() = applyToMethod {
|
||||
addInstructions(getTestSmaliInstructions(0..1)).also {
|
||||
assertRegisterIs(0, 10)
|
||||
assertRegisterIs(1, 11)
|
||||
|
||||
assertRegisterIs(9, 9)
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
fun addSmaliInstructionsWithExternalLabelToMethodIndexed() = applyToMethod {
|
||||
val label = ExternalLabel("testLabel", getInstruction(5))
|
||||
|
||||
addInstructionsWithLabels(
|
||||
5,
|
||||
getTestSmaliInstructions(0..1).plus("\n").plus("goto :${label.name}"),
|
||||
label
|
||||
).also {
|
||||
assertRegisterIs(0, 5)
|
||||
assertRegisterIs(1, 6)
|
||||
assertRegisterIs(5, 8)
|
||||
|
||||
val gotoTarget = getInstruction<BuilderOffsetInstruction>(7)
|
||||
.target.location.instruction as OneRegisterInstruction
|
||||
|
||||
assertEquals(5, gotoTarget.registerA)
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
fun removeInstructionFromMethodIndexed() = applyToMethod {
|
||||
removeInstruction(5).also {
|
||||
assertRegisterIs(4, 4)
|
||||
assertRegisterIs(6, 5)
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
fun removeInstructionsFromMethodIndexed() = applyToMethod {
|
||||
removeInstructions(5, 5).also { assertRegisterIs(4, 4) }
|
||||
}
|
||||
|
||||
@Test
|
||||
fun removeInstructionsFromMethod() = applyToMethod {
|
||||
removeInstructions(0).also { assertRegisterIs(9, 9) }
|
||||
removeInstructions(1).also { assertRegisterIs(1, 0) }
|
||||
removeInstructions(2).also { assertRegisterIs(3, 0) }
|
||||
}
|
||||
|
||||
@Test
|
||||
fun replaceInstructionInMethodIndexed() = applyToMethod {
|
||||
replaceInstruction(5, TestInstruction(0)).also { assertRegisterIs(0, 5) }
|
||||
}
|
||||
|
||||
@Test
|
||||
fun replaceInstructionsInMethodIndexed() = applyToMethod {
|
||||
replaceInstructions(5, getTestInstructions(0..1)).also {
|
||||
assertRegisterIs(0, 5)
|
||||
assertRegisterIs(1, 6)
|
||||
assertRegisterIs(7, 7)
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
fun replaceSmaliInstructionsInMethodIndexed() = applyToMethod {
|
||||
replaceInstructions(5, getTestSmaliInstructions(0..1)).also {
|
||||
assertRegisterIs(0, 5)
|
||||
assertRegisterIs(1, 6)
|
||||
assertRegisterIs(7, 7)
|
||||
}
|
||||
}
|
||||
|
||||
// region Helper methods
|
||||
|
||||
private fun applyToImplementation(block: MutableMethodImplementation.() -> Unit) {
|
||||
testMethodImplementation.apply(block)
|
||||
}
|
||||
|
||||
private fun applyToMethod(block: MutableMethod.() -> Unit) {
|
||||
testMethod.apply(block)
|
||||
}
|
||||
|
||||
private fun MutableMethodImplementation.assertRegisterIs(register: Int, atIndex: Int) = assertEquals(
|
||||
register, getInstruction<OneRegisterInstruction>(atIndex).registerA
|
||||
)
|
||||
|
||||
private fun MutableMethod.assertRegisterIs(register: Int, atIndex: Int) =
|
||||
implementation!!.assertRegisterIs(register, atIndex)
|
||||
|
||||
private fun getTestInstructions(range: IntRange) = range.map { TestInstruction(it) }
|
||||
|
||||
private fun getTestSmaliInstruction(register: Int) = "const/16 v$register, 0"
|
||||
|
||||
private fun getTestSmaliInstructions(range: IntRange) = range.joinToString("\n") {
|
||||
getTestSmaliInstruction(it)
|
||||
}
|
||||
|
||||
// endregion
|
||||
|
||||
private class TestInstruction(register: Int) : BuilderInstruction21s(Opcode.CONST_16, register, 0)
|
||||
}
|
@@ -4,9 +4,9 @@ import app.revanced.patcher.annotation.Description
|
||||
import app.revanced.patcher.annotation.Name
|
||||
import app.revanced.patcher.annotation.Version
|
||||
import app.revanced.patcher.data.BytecodeContext
|
||||
import app.revanced.patcher.extensions.addInstructions
|
||||
import app.revanced.patcher.extensions.InstructionExtensions.addInstructionsWithLabels
|
||||
import app.revanced.patcher.extensions.or
|
||||
import app.revanced.patcher.extensions.replaceInstruction
|
||||
import app.revanced.patcher.extensions.InstructionExtensions.replaceInstruction
|
||||
import app.revanced.patcher.patch.*
|
||||
import app.revanced.patcher.patch.annotations.DependsOn
|
||||
import app.revanced.patcher.patch.annotations.Patch
|
||||
@@ -119,7 +119,7 @@ class ExampleBytecodePatch : BytecodePatch(listOf(ExampleFingerprint)) {
|
||||
// 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.
|
||||
method.addInstructions(
|
||||
method.addInstructionsWithLabels(
|
||||
startIndex + 2,
|
||||
"""
|
||||
invoke-static { }, LTestClass;->returnHello()Ljava/lang/String;
|
||||
|
@@ -1,8 +1,9 @@
|
||||
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.extensions.InstructionExtensions.addInstructions
|
||||
import app.revanced.patcher.extensions.InstructionExtensions.addInstructionsWithLabels
|
||||
import app.revanced.patcher.extensions.InstructionExtensions.getInstruction
|
||||
import app.revanced.patcher.extensions.newLabel
|
||||
import app.revanced.patcher.util.proxy.mutableTypes.MutableMethod.Companion.toMutable
|
||||
import org.jf.dexlib2.AccessFlags
|
||||
import org.jf.dexlib2.Opcode
|
||||
@@ -35,7 +36,7 @@ internal class InlineSmaliCompilerTest {
|
||||
method.addInstructions(arrayOfNulls<String>(insnAmount).also {
|
||||
Arrays.fill(it, "const/4 v0, 0x0")
|
||||
}.joinToString("\n"))
|
||||
method.addInstructions(
|
||||
method.addInstructionsWithLabels(
|
||||
targetIndex,
|
||||
"""
|
||||
:test
|
||||
@@ -44,7 +45,7 @@ internal class InlineSmaliCompilerTest {
|
||||
"""
|
||||
)
|
||||
|
||||
val insn = method.instruction<BuilderInstruction21t>(insnIndex)
|
||||
val insn = method.getInstruction<BuilderInstruction21t>(insnIndex)
|
||||
assertEquals(targetIndex, insn.target.location.index)
|
||||
}
|
||||
|
||||
@@ -61,19 +62,19 @@ internal class InlineSmaliCompilerTest {
|
||||
"""
|
||||
)
|
||||
|
||||
assertEquals(labelIndex, method.label(labelIndex).location.index)
|
||||
assertEquals(labelIndex, method.newLabel(labelIndex).location.index)
|
||||
|
||||
method.addInstructions(
|
||||
method.addInstructionsWithLabels(
|
||||
method.implementation!!.instructions.size,
|
||||
"""
|
||||
const/4 v0, 0x1
|
||||
if-eqz v0, :test
|
||||
return-void
|
||||
""", listOf(
|
||||
ExternalLabel("test",method.instruction(1))
|
||||
)
|
||||
""",
|
||||
ExternalLabel("test", method.getInstruction(1))
|
||||
)
|
||||
|
||||
val insn = method.instruction<BuilderInstruction21t>(insnIndex)
|
||||
val insn = method.getInstruction<BuilderInstruction21t>(insnIndex)
|
||||
assertTrue(insn.target.isPlaced, "Label was not placed")
|
||||
assertEquals(labelIndex, insn.target.location.index)
|
||||
}
|
||||
|
Reference in New Issue
Block a user