From 8d1bb5f3d9da544cf6e3e3848bfcc56327cde810 Mon Sep 17 00:00:00 2001 From: Lucaskyy Date: Thu, 24 Mar 2022 20:58:10 +0100 Subject: [PATCH] fix(MethodResolver): strip labels and line numbers so opcode patterns match --- .../patcher/resolver/MethodResolver.kt | 20 +++++++++---------- .../app/revanced/patcher/util/TestUtil.kt | 3 ++- 2 files changed, 11 insertions(+), 12 deletions(-) diff --git a/src/main/kotlin/app/revanced/patcher/resolver/MethodResolver.kt b/src/main/kotlin/app/revanced/patcher/resolver/MethodResolver.kt index c27a3fd..187d985 100644 --- a/src/main/kotlin/app/revanced/patcher/resolver/MethodResolver.kt +++ b/src/main/kotlin/app/revanced/patcher/resolver/MethodResolver.kt @@ -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, 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, private va } } -private operator fun ClassNode.component1(): ClassNode { - return this -} - -private operator fun ClassNode.component2(): List { - 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.convertObjects(): Array { return this.map { it.convertObject() }.toTypedArray() } + +private fun InsnList.stripLabels(): InsnList { + this.removeAll { it is LabelNode || it is LineNumberNode } + return this +} diff --git a/src/test/kotlin/app/revanced/patcher/util/TestUtil.kt b/src/test/kotlin/app/revanced/patcher/util/TestUtil.kt index de9c831..6d891e1 100644 --- a/src/test/kotlin/app/revanced/patcher/util/TestUtil.kt +++ b/src/test/kotlin/app/revanced/patcher/util/TestUtil.kt @@ -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 ", " } }