fix(MethodResolver): strip labels and line numbers so opcode patterns match

This commit is contained in:
Lucaskyy 2022-03-24 20:58:10 +01:00
parent c8a017a4c0
commit 8d1bb5f3d9
No known key found for this signature in database
GPG key ID: 1530BFF96D1EEB89
2 changed files with 11 additions and 12 deletions

View file

@ -7,9 +7,7 @@ import app.revanced.patcher.cache.PatternScanData
import app.revanced.patcher.signature.Signature
import app.revanced.patcher.util.ExtraTypes
import org.objectweb.asm.Type
import org.objectweb.asm.tree.ClassNode
import org.objectweb.asm.tree.InsnList
import org.objectweb.asm.tree.MethodNode
import org.objectweb.asm.tree.*
private val logger = KotlinLogging.logger("MethodResolver")
@ -110,7 +108,7 @@ internal class MethodResolver(private val classList: List<ClassNode>, private va
}
signature.opcodes?.let { _ ->
val result = method.instructions.scanFor(signature.opcodes)
val result = method.instructions.stripLabels().scanFor(signature.opcodes)
if (!result.found) {
logger.debug { "Comparing sig ${signature.name}: invalid opcode pattern" }
return@cmp false to null
@ -123,13 +121,8 @@ internal class MethodResolver(private val classList: List<ClassNode>, private va
}
}
private operator fun ClassNode.component1(): ClassNode {
return this
}
private operator fun ClassNode.component2(): List<MethodNode> {
return this.methods
}
private operator fun ClassNode.component1() = this
private operator fun ClassNode.component2() = this.methods
private fun InsnList.scanFor(pattern: IntArray): ScanResult {
for (i in 0 until this.size()) {
@ -157,3 +150,8 @@ private fun Type.convertObject(): Type {
private fun Array<Type>.convertObjects(): Array<Type> {
return this.map { it.convertObject() }.toTypedArray()
}
private fun InsnList.stripLabels(): InsnList {
this.removeAll { it is LabelNode || it is LineNumberNode }
return this
}

View file

@ -38,7 +38,8 @@ private class NodeStringBuilder {
}
override fun toString(): String {
if (sb.isEmpty()) return ""
val s = sb.toString()
return s.substring(0 until s.length - 2) // remove the last ", "
return s.substring(0 .. (s.length - 2).coerceAtLeast(0)) // remove the last ", "
}
}