fix(MethodResolver): fix cd57a8c9a0db7e3ae5ad0bca202e5955930319ab

This commit is contained in:
Lucaskyy 2022-03-24 23:29:32 +01:00 committed by oSumAtrIX
parent f3d8b917de
commit cbd8df2df0
No known key found for this signature in database
GPG Key ID: A9B3094ACDB604B4
2 changed files with 22 additions and 11 deletions

View File

@ -1,15 +1,13 @@
package app.revanced.patcher.resolver package app.revanced.patcher.resolver
import mu.KotlinLogging
import app.revanced.patcher.cache.MethodMap import app.revanced.patcher.cache.MethodMap
import app.revanced.patcher.cache.PatchData import app.revanced.patcher.cache.PatchData
import app.revanced.patcher.cache.PatternScanData import app.revanced.patcher.cache.PatternScanData
import app.revanced.patcher.signature.Signature import app.revanced.patcher.signature.Signature
import app.revanced.patcher.util.ExtraTypes import app.revanced.patcher.util.ExtraTypes
import mu.KotlinLogging
import org.objectweb.asm.Type import org.objectweb.asm.Type
import org.objectweb.asm.tree.* import org.objectweb.asm.tree.*
import kotlin.reflect.KType
import kotlin.reflect.typeOf
private val logger = KotlinLogging.logger("MethodResolver") private val logger = KotlinLogging.logger("MethodResolver")
@ -133,8 +131,8 @@ private fun InsnList.scanFor(pattern: IntArray): ScanResult {
val n = this[i + occurrence] val n = this[i + occurrence]
if ( if (
!n.anyOf( !n.anyOf(
typeOf<LabelNode>(), LabelNode::class.java,
typeOf<LineNumberNode>() LineNumberNode::class.java
) && ) &&
n.opcode != pattern[occurrence] n.opcode != pattern[occurrence]
) break ) break
@ -160,6 +158,5 @@ private fun Array<Type>.convertObjects(): Array<Type> {
return this.map { it.convertObject() }.toTypedArray() return this.map { it.convertObject() }.toTypedArray()
} }
private fun AbstractInsnNode.anyOf(vararg types: KType): Boolean { private fun AbstractInsnNode.anyOf(vararg types: Class<*>): Boolean =
return types.any { it.javaClass.isAssignableFrom(this.javaClass) } types.any { this@anyOf.javaClass == it }
}

View File

@ -39,8 +39,10 @@ internal class PatcherTest {
ACC_PUBLIC or ACC_STATIC, ACC_PUBLIC or ACC_STATIC,
arrayOf(ExtraTypes.ArrayAny), arrayOf(ExtraTypes.ArrayAny),
intArrayOf( intArrayOf(
GETSTATIC,
LDC, LDC,
INVOKEVIRTUAL INVOKEVIRTUAL,
RETURN
) )
) )
) )
@ -66,7 +68,19 @@ internal class PatcherTest {
// Get the start index of our opcode pattern. // Get the start index of our opcode pattern.
// This will be the index of the LDC instruction. // This will be the index of the LDC instruction.
val startIndex = mainMethod.scanData.startIndex val startIndex = mainMethod.scanData.startIndex
TestUtil.assertNodeEqual(LdcInsnNode("Hello, world!"), instructions[startIndex]!!)
// Ignore this, just testing if the method resolver works :)
TestUtil.assertNodeEqual(
FieldInsnNode(
GETSTATIC,
Type.getInternalName(System::class.java),
"out",
// for whatever reason, it adds an "L" and ";" to the node string
"L${Type.getInternalName(PrintStream::class.java)};"
),
instructions[startIndex]!!
)
// Create a new LDC node and replace the LDC instruction. // Create a new LDC node and replace the LDC instruction.
val stringNode = LdcInsnNode("Hello, ReVanced! Editing bytecode.") val stringNode = LdcInsnNode("Hello, ReVanced! Editing bytecode.")
instructions.setAt(startIndex, stringNode) instructions.setAt(startIndex, stringNode)
@ -82,7 +96,7 @@ internal class PatcherTest {
GETSTATIC, GETSTATIC,
Type.getInternalName(System::class.java), // "java/lang/System" Type.getInternalName(System::class.java), // "java/lang/System"
"out", "out",
"L" + Type.getInternalName(PrintStream::class.java) // "Ljava/io/PrintStream" Type.getInternalName(PrintStream::class.java) // "java/io/PrintStream"
), ),
LdcInsnNode("Hello, ReVanced! Adding bytecode."), LdcInsnNode("Hello, ReVanced! Adding bytecode."),
MethodInsnNode( MethodInsnNode(