From e5f9a42ee6e926b533d54f5cbac5ab4666cf241f Mon Sep 17 00:00:00 2001 From: Lucaskyy Date: Sat, 19 Mar 2022 22:34:41 +0100 Subject: [PATCH] Convert typed Object & Array types to Object types Commit name is a bit superfluous, but it means that types such as "Ljava/lang/String;" get converted to "Ljava/lang/Object;". This is so we don't need to know the mappings, which are random in YT's code. This is done in MethodResolver's "convertObject" and "convertObjects" for arrays. Also changed ExtraTypes.ArrayAny to Object instead of String, that was a mistake. (which caused the test to pass at first) --- .../patcher/resolver/MethodResolver.kt | 37 +++++++++++++++++-- .../net/revanced/patcher/util/ASMWriter.kt | 5 --- .../net/revanced/patcher/util/ExtraTypes.kt | 4 +- .../net/revanced/patcher/writer/ASMWriter.kt | 9 +++++ .../net/revanced/patcher/PatcherTest.kt | 10 ++--- 5 files changed, 49 insertions(+), 16 deletions(-) delete mode 100644 src/main/kotlin/net/revanced/patcher/util/ASMWriter.kt create mode 100644 src/main/kotlin/net/revanced/patcher/writer/ASMWriter.kt diff --git a/src/main/kotlin/net/revanced/patcher/resolver/MethodResolver.kt b/src/main/kotlin/net/revanced/patcher/resolver/MethodResolver.kt index 09726be..ca96b8d 100644 --- a/src/main/kotlin/net/revanced/patcher/resolver/MethodResolver.kt +++ b/src/main/kotlin/net/revanced/patcher/resolver/MethodResolver.kt @@ -4,6 +4,7 @@ import mu.KotlinLogging import net.revanced.patcher.cache.PatchData import net.revanced.patcher.cache.ScanData import net.revanced.patcher.signature.Signature +import net.revanced.patcher.util.ExtraTypes import org.objectweb.asm.Type import org.objectweb.asm.tree.ClassNode import org.objectweb.asm.tree.InsnList @@ -51,16 +52,32 @@ internal class MethodResolver(private val classList: List, private va } private fun cmp(method: MethodNode, signature: Signature): Pair { - if (signature.returns != Type.getReturnType(method.desc)) { - logger.debug { "Comparing sig ${signature.name}: invalid return type:\nexpected ${signature.returns},\ngot ${Type.getReturnType(method.desc)}" } + val returns = Type.getReturnType(method.desc).convertObject() + if (signature.returns != returns) { + logger.debug { + """ + Comparing sig ${signature.name}: invalid return type: + expected ${signature.returns}}, + got $returns + """.trimIndent() + } return false to null } + if (signature.accessors != method.access) { logger.debug { "Comparing sig ${signature.name}: invalid accessors:\nexpected ${signature.accessors},\ngot ${method.access}" } return false to null } - if (!signature.parameters.contentEquals(Type.getArgumentTypes(method.desc))) { - logger.debug { "Comparing sig ${signature.name}: invalid parameter types:\nexpected ${signature.parameters},\ngot ${Type.getArgumentTypes(method.desc)}" } + + val parameters = Type.getArgumentTypes(method.desc).convertObjects() + if (!signature.parameters.contentEquals(parameters)) { + logger.debug { + """ + Comparing sig ${signature.name}: invalid parameter types: + expected ${signature.parameters.joinToString()}}, + got ${parameters.joinToString()} + """.trimIndent() + } return false to null } @@ -96,3 +113,15 @@ private fun InsnList.scanFor(pattern: Array): ScanResult { return ScanResult(false) } + +private fun Type.convertObject(): Type { + return when (this.sort) { + Type.OBJECT -> ExtraTypes.Any + Type.ARRAY -> ExtraTypes.ArrayAny + else -> this + } +} + +private fun Array.convertObjects(): Array { + return this.map { it.convertObject() }.toTypedArray() +} diff --git a/src/main/kotlin/net/revanced/patcher/util/ASMWriter.kt b/src/main/kotlin/net/revanced/patcher/util/ASMWriter.kt deleted file mode 100644 index ea3fb89..0000000 --- a/src/main/kotlin/net/revanced/patcher/util/ASMWriter.kt +++ /dev/null @@ -1,5 +0,0 @@ -package net.revanced.patcher.util - -object ASMWriter { - -} \ No newline at end of file diff --git a/src/main/kotlin/net/revanced/patcher/util/ExtraTypes.kt b/src/main/kotlin/net/revanced/patcher/util/ExtraTypes.kt index 4113f69..6137c16 100644 --- a/src/main/kotlin/net/revanced/patcher/util/ExtraTypes.kt +++ b/src/main/kotlin/net/revanced/patcher/util/ExtraTypes.kt @@ -7,6 +7,6 @@ object ExtraTypes { * Any object type. * Should be used instead of types such as: "Ljava/lang/String;" */ - val Any = Type.getType(Object::class.java) - val ArrayAny = Type.getType(Array::class.java) + val Any: Type = Type.getType(Object::class.java) + val ArrayAny: Type = Type.getType(Array::class.java) } \ No newline at end of file diff --git a/src/main/kotlin/net/revanced/patcher/writer/ASMWriter.kt b/src/main/kotlin/net/revanced/patcher/writer/ASMWriter.kt new file mode 100644 index 0000000..77d44b8 --- /dev/null +++ b/src/main/kotlin/net/revanced/patcher/writer/ASMWriter.kt @@ -0,0 +1,9 @@ +package net.revanced.patcher.writer + +import org.objectweb.asm.tree.InsnList + +object ASMWriter { + fun InsnList.testingWow() { + TODO() + } +} \ No newline at end of file diff --git a/src/test/kotlin/net/revanced/patcher/PatcherTest.kt b/src/test/kotlin/net/revanced/patcher/PatcherTest.kt index 3f01be2..29f1e3d 100644 --- a/src/test/kotlin/net/revanced/patcher/PatcherTest.kt +++ b/src/test/kotlin/net/revanced/patcher/PatcherTest.kt @@ -4,6 +4,7 @@ import net.revanced.patcher.patch.Patch import net.revanced.patcher.patch.PatchResultSuccess import net.revanced.patcher.signature.Signature import net.revanced.patcher.util.ExtraTypes +import net.revanced.patcher.writer.ASMWriter.testingWow import org.junit.jupiter.api.Test import org.objectweb.asm.Opcodes.* import org.objectweb.asm.Type @@ -11,12 +12,10 @@ import org.objectweb.asm.Type internal class PatcherTest { private val testSigs: Array = arrayOf( Signature( - "testMethod", + "mainMethod", Type.VOID_TYPE, ACC_PUBLIC or ACC_STATIC, - arrayOf( - ExtraTypes.ArrayAny, - ), + arrayOf(ExtraTypes.ArrayAny), arrayOf( GETSTATIC, LDC, @@ -32,7 +31,8 @@ internal class PatcherTest { patcher.addPatches( Patch ("TestPatch") { - patcher.cache.methods["testMethod"] + val main = patcher.cache.methods["mainMethod"] + //main.method.instructions!!.testingWow() PatchResultSuccess() } )