From 8dd5292cb2aa3df394ffdc2ee91fd0ce5323a3fa Mon Sep 17 00:00:00 2001 From: Graham Date: Sat, 27 Jun 2020 21:12:18 +0100 Subject: [PATCH] Remap argument names Signed-off-by: Graham --- .../java/dev/openrs2/asm/ClassNodeRemapper.kt | 14 +++++++++++++ .../openrs2/asm/classpath/ExtendedRemapper.kt | 10 +++++++++ deob/src/main/java/dev/openrs2/deob/ArgRef.kt | 5 +++++ .../deob/remap/ArgumentMappingGenerator.kt | 21 +++++++++++++++++++ .../dev/openrs2/deob/remap/TypedRemapper.kt | 15 +++++++++++++ 5 files changed, 65 insertions(+) create mode 100644 deob/src/main/java/dev/openrs2/deob/ArgRef.kt create mode 100644 deob/src/main/java/dev/openrs2/deob/remap/ArgumentMappingGenerator.kt diff --git a/asm/src/main/java/dev/openrs2/asm/ClassNodeRemapper.kt b/asm/src/main/java/dev/openrs2/asm/ClassNodeRemapper.kt index 37da6973..b40eeadb 100644 --- a/asm/src/main/java/dev/openrs2/asm/ClassNodeRemapper.kt +++ b/asm/src/main/java/dev/openrs2/asm/ClassNodeRemapper.kt @@ -1,6 +1,7 @@ package dev.openrs2.asm import dev.openrs2.asm.classpath.ExtendedRemapper +import org.objectweb.asm.Type import org.objectweb.asm.commons.Remapper import org.objectweb.asm.tree.AbstractInsnNode import org.objectweb.asm.tree.ClassNode @@ -13,6 +14,7 @@ import org.objectweb.asm.tree.LdcInsnNode import org.objectweb.asm.tree.MethodInsnNode import org.objectweb.asm.tree.MethodNode import org.objectweb.asm.tree.MultiANewArrayInsnNode +import org.objectweb.asm.tree.ParameterNode import org.objectweb.asm.tree.TypeInsnNode fun ClassNode.remap(remapper: ExtendedRemapper) { @@ -57,6 +59,14 @@ fun FieldNode.remap(remapper: ExtendedRemapper, owner: String) { } fun MethodNode.remap(remapper: ExtendedRemapper, owner: String) { + if (parameters == null) { + parameters = List(Type.getArgumentTypes(desc).size) { ParameterNode(null, 0) } + } + + for ((index, parameter) in parameters.withIndex()) { + parameter.remap(remapper, owner, name, desc, index) + } + name = remapper.mapMethodName(owner, name, desc) desc = remapper.mapMethodDesc(desc) signature = remapper.mapSignature(signature, false) @@ -75,6 +85,10 @@ fun MethodNode.remap(remapper: ExtendedRemapper, owner: String) { } } +fun ParameterNode.remap(remapper: ExtendedRemapper, owner: String, methodName: String, desc: String, index: Int) { + name = remapper.mapArgumentName(owner, methodName, desc, index, name) +} + fun AbstractInsnNode.remap(remapper: ExtendedRemapper) { when (this) { is FrameNode -> throw UnsupportedOperationException("SKIP_FRAMES and COMPUTE_FRAMES must be used") diff --git a/asm/src/main/java/dev/openrs2/asm/classpath/ExtendedRemapper.kt b/asm/src/main/java/dev/openrs2/asm/classpath/ExtendedRemapper.kt index 3e917977..3cfbc2db 100644 --- a/asm/src/main/java/dev/openrs2/asm/classpath/ExtendedRemapper.kt +++ b/asm/src/main/java/dev/openrs2/asm/classpath/ExtendedRemapper.kt @@ -15,4 +15,14 @@ abstract class ExtendedRemapper : Remapper() { open fun mapMethodOwner(owner: String, name: String, descriptor: String): String { return mapType(owner) } + + open fun mapArgumentName( + owner: String, + name: String, + descriptor: String, + index: Int, + argumentName: String? + ): String? { + return argumentName + } } diff --git a/deob/src/main/java/dev/openrs2/deob/ArgRef.kt b/deob/src/main/java/dev/openrs2/deob/ArgRef.kt new file mode 100644 index 00000000..555d5ee7 --- /dev/null +++ b/deob/src/main/java/dev/openrs2/deob/ArgRef.kt @@ -0,0 +1,5 @@ +package dev.openrs2.deob + +import dev.openrs2.asm.MemberRef + +data class ArgRef(val method: MemberRef, val index: Int) diff --git a/deob/src/main/java/dev/openrs2/deob/remap/ArgumentMappingGenerator.kt b/deob/src/main/java/dev/openrs2/deob/remap/ArgumentMappingGenerator.kt new file mode 100644 index 00000000..25465b58 --- /dev/null +++ b/deob/src/main/java/dev/openrs2/deob/remap/ArgumentMappingGenerator.kt @@ -0,0 +1,21 @@ +package dev.openrs2.deob.remap + +import dev.openrs2.deob.ArgRef +import dev.openrs2.deob.util.map.NameMap + +class ArgumentMappingGenerator( + private val nameMap: NameMap +) { + fun generate(): Map { + val argumentNames = mutableMapOf() + + for ((methodRef, method) in nameMap.methods) { + for ((index, name) in method.arguments) { + val argument = ArgRef(methodRef, index) + argumentNames[argument] = name + } + } + + return argumentNames + } +} diff --git a/deob/src/main/java/dev/openrs2/deob/remap/TypedRemapper.kt b/deob/src/main/java/dev/openrs2/deob/remap/TypedRemapper.kt index 3b2a55ff..ffde8315 100644 --- a/deob/src/main/java/dev/openrs2/deob/remap/TypedRemapper.kt +++ b/deob/src/main/java/dev/openrs2/deob/remap/TypedRemapper.kt @@ -5,6 +5,7 @@ import dev.openrs2.asm.MemberRef import dev.openrs2.asm.classpath.ClassPath import dev.openrs2.asm.classpath.ExtendedRemapper import dev.openrs2.asm.filter.UnionMemberFilter +import dev.openrs2.deob.ArgRef import dev.openrs2.deob.Profile import dev.openrs2.deob.filter.BrowserControlFilter import dev.openrs2.deob.util.map.NameMap @@ -17,6 +18,7 @@ class TypedRemapper private constructor( private val classes: Map, private val fields: Map, String>, private val methods: Map, String>, + private val argumentNames: Map, private val staticFields: Map, StaticField>, private val staticMethods: Map, String> ) : ExtendedRemapper() { @@ -54,6 +56,17 @@ class TypedRemapper private constructor( return staticMethods.getOrDefault(partition, mapType(owner)) } + override fun mapArgumentName( + owner: String, + name: String, + descriptor: String, + index: Int, + argumentName: String? + ): String? { + val argument = ArgRef(MemberRef(owner, name, descriptor), index) + return argumentNames[argument] ?: argumentName + } + companion object { private val logger = InlineLogger() @@ -79,6 +92,7 @@ class TypedRemapper private constructor( nameMap, inheritedMethodSets ).generate() + val argumentNames = ArgumentMappingGenerator(nameMap).generate() verifyMapping(classes, profile.maxObfuscatedNameLen) verifyMemberMapping(fields, profile.maxObfuscatedNameLen) @@ -110,6 +124,7 @@ class TypedRemapper private constructor( classes, fields, methods, + argumentNames, staticFields, staticMethods )