Skip to content

Commit

Permalink
fix(MethodResolver): strip labels and line numbers so opcode patterns…
Browse files Browse the repository at this point in the history
… match
  • Loading branch information
Sculas authored and oSumAtrIX committed Jun 5, 2022
1 parent b197956 commit 699c730
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 12 deletions.
20 changes: 9 additions & 11 deletions src/main/kotlin/app/revanced/patcher/resolver/MethodResolver.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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")

Expand Down Expand Up @@ -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
Expand All @@ -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()) {
Expand Down Expand Up @@ -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
}
3 changes: 2 additions & 1 deletion src/test/kotlin/app/revanced/patcher/util/TestUtil.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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 ", "
}
}

0 comments on commit 699c730

Please sign in to comment.