Skip to content

Commit

Permalink
fix(MethodResolver): strip labels nodes so opcode patterns match
Browse files Browse the repository at this point in the history
this commit is also a fix for 8d1bb5f because it corrupted the stack by completely removing the nodes
  • Loading branch information
Sculas authored and oSumAtrIX committed Jun 5, 2022
1 parent 81d0cf2 commit 82c5306
Showing 1 changed file with 13 additions and 5 deletions.
18 changes: 13 additions & 5 deletions src/main/kotlin/app/revanced/patcher/resolver/MethodResolver.kt
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import app.revanced.patcher.signature.Signature
import app.revanced.patcher.util.ExtraTypes
import org.objectweb.asm.Type
import org.objectweb.asm.tree.*
import kotlin.reflect.KType
import kotlin.reflect.typeOf

private val logger = KotlinLogging.logger("MethodResolver")

Expand Down Expand Up @@ -108,7 +110,7 @@ internal class MethodResolver(private val classList: List<ClassNode>, private va
}

signature.opcodes?.let { _ ->
val result = method.instructions.stripLabels().scanFor(signature.opcodes)
val result = method.instructions.scanFor(signature.opcodes)
if (!result.found) {
logger.trace { "Comparing sig ${signature.name}: invalid opcode pattern" }
return@cmp false to null
Expand All @@ -128,7 +130,14 @@ private fun InsnList.scanFor(pattern: IntArray): ScanResult {
for (i in 0 until this.size()) {
var occurrence = 0
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) {
val current = i + occurrence
return ScanResult(true, current - pattern.size, current)
Expand All @@ -151,7 +160,6 @@ 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
private fun AbstractInsnNode.anyOf(vararg types: KType): Boolean {
return types.any { it.javaClass.isAssignableFrom(this.javaClass) }
}

0 comments on commit 82c5306

Please sign in to comment.