From 7b92ccb24425e9eb26b2ec4ee78e033eb9ca72db Mon Sep 17 00:00:00 2001 From: Major Date: Mon, 9 Mar 2020 21:58:21 +0000 Subject: [PATCH] Add RedundantGotoTransformer Removes GOTO instructions that point to the next instruction, typically left over from stripping exceptions inserted by the obfuscator. Although modern decompilers can handle such GOTOs fine, removing them makes for a nicer IR representation. Signed-off-by: Major --- .../java/dev/openrs2/deob/Deobfuscator.kt | 2 + .../transform/RedundantGotoTransformer.kt | 53 +++++++++++++++++++ 2 files changed, 55 insertions(+) create mode 100644 deob/src/main/java/dev/openrs2/deob/transform/RedundantGotoTransformer.kt diff --git a/deob/src/main/java/dev/openrs2/deob/Deobfuscator.kt b/deob/src/main/java/dev/openrs2/deob/Deobfuscator.kt index b48eb443..0c9eb0b5 100644 --- a/deob/src/main/java/dev/openrs2/deob/Deobfuscator.kt +++ b/deob/src/main/java/dev/openrs2/deob/Deobfuscator.kt @@ -28,6 +28,7 @@ import dev.openrs2.deob.transform.OriginalNameTransformer import dev.openrs2.deob.transform.OriginalPcRestoreTransformer import dev.openrs2.deob.transform.OriginalPcSaveTransformer import dev.openrs2.deob.transform.OverrideTransformer +import dev.openrs2.deob.transform.RedundantGotoTransformer import dev.openrs2.deob.transform.RemapTransformer import dev.openrs2.deob.transform.ResetTransformer import dev.openrs2.deob.transform.StaticScramblingTransformer @@ -180,6 +181,7 @@ class Deobfuscator(private val input: Path, private val output: Path) { VisibilityTransformer(), FinalTransformer(), OverrideTransformer(), + RedundantGotoTransformer(), OriginalPcRestoreTransformer(), FernflowerExceptionTransformer() ) diff --git a/deob/src/main/java/dev/openrs2/deob/transform/RedundantGotoTransformer.kt b/deob/src/main/java/dev/openrs2/deob/transform/RedundantGotoTransformer.kt new file mode 100644 index 00000000..ad104ad7 --- /dev/null +++ b/deob/src/main/java/dev/openrs2/deob/transform/RedundantGotoTransformer.kt @@ -0,0 +1,53 @@ +package dev.openrs2.deob.transform + +import com.github.michaelbull.logging.InlineLogger +import dev.openrs2.asm.classpath.ClassPath +import dev.openrs2.asm.classpath.Library +import dev.openrs2.asm.nextReal +import dev.openrs2.asm.removeDeadCode +import dev.openrs2.asm.transform.Transformer +import org.objectweb.asm.Opcodes +import org.objectweb.asm.tree.ClassNode +import org.objectweb.asm.tree.JumpInsnNode +import org.objectweb.asm.tree.MethodNode + +class RedundantGotoTransformer : Transformer() { + private var removed = 0 + + override fun preTransform(classPath: ClassPath) { + removed = 0 + } + + override fun preTransformMethod( + classPath: ClassPath, + library: Library, + clazz: ClassNode, + method: MethodNode + ): Boolean { + method.removeDeadCode(clazz.name) + return false + } + + override fun transformCode(classPath: ClassPath, library: Library, clazz: ClassNode, method: MethodNode): Boolean { + for (instruction in method.instructions) { + if (instruction.opcode == Opcodes.GOTO) { + instruction as JumpInsnNode + + if (instruction.nextReal === instruction.label.nextReal) { + method.instructions.remove(instruction) + removed++ + } + } + } + + return false + } + + override fun postTransform(classPath: ClassPath) { + logger.info { "Removed $removed redundant GOTO instructions" } + } + + private companion object { + private val logger = InlineLogger() + } +}