fix(MethodResolver): strip labels nodes so opcode patterns match

this commit is also a fix for 8d1bb5f3d9 because it corrupted the stack by completely removing the nodes
This commit is contained in:
Lucaskyy 2022-03-24 22:48:34 +01:00 committed by oSumAtrIX
parent 81d0cf20f3
commit 82c530650f
No known key found for this signature in database
GPG key ID: A9B3094ACDB604B4

View file

@ -8,6 +8,8 @@ import app.revanced.patcher.signature.Signature
import app.revanced.patcher.util.ExtraTypes import app.revanced.patcher.util.ExtraTypes
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")
@ -108,7 +110,7 @@ internal class MethodResolver(private val classList: List<ClassNode>, private va
} }
signature.opcodes?.let { _ -> signature.opcodes?.let { _ ->
val result = method.instructions.stripLabels().scanFor(signature.opcodes) val result = method.instructions.scanFor(signature.opcodes)
if (!result.found) { if (!result.found) {
logger.trace { "Comparing sig ${signature.name}: invalid opcode pattern" } logger.trace { "Comparing sig ${signature.name}: invalid opcode pattern" }
return@cmp false to null return@cmp false to null
@ -128,7 +130,14 @@ private fun InsnList.scanFor(pattern: IntArray): ScanResult {
for (i in 0 until this.size()) { for (i in 0 until this.size()) {
var occurrence = 0 var occurrence = 0
while (i + occurrence < this.size()) { while (i + occurrence < this.size()) {
if (this[i + occurrence].opcode != pattern[occurrence]) break val n = this[i + occurrence]
if (
!n.anyOf(
typeOf<LabelNode>(),
typeOf<LineNumberNode>()
) &&
n.opcode != pattern[occurrence]
) break
if (++occurrence >= pattern.size) { if (++occurrence >= pattern.size) {
val current = i + occurrence val current = i + occurrence
return ScanResult(true, current - pattern.size, current) return ScanResult(true, current - pattern.size, current)
@ -151,7 +160,6 @@ private fun Array<Type>.convertObjects(): Array<Type> {
return this.map { it.convertObject() }.toTypedArray() return this.map { it.convertObject() }.toTypedArray()
} }
private fun InsnList.stripLabels(): InsnList { private fun AbstractInsnNode.anyOf(vararg types: KType): Boolean {
this.removeAll { it is LabelNode || it is LineNumberNode } return types.any { it.javaClass.isAssignableFrom(this.javaClass) }
return this
} }