From 66283c6f7adef3acd098c3745458a202d573f2ea Mon Sep 17 00:00:00 2001 From: Graham Date: Mon, 29 Jul 2019 21:56:37 +0100 Subject: [PATCH] Add ExceptionTracingTransformer --- .../java/dev/openrs2/deob/Deobfuscator.java | 4 +- .../ExceptionTracingTransformer.java | 45 +++++++++++++++++++ 2 files changed, 48 insertions(+), 1 deletion(-) create mode 100644 deob/src/main/java/dev/openrs2/deob/transform/ExceptionTracingTransformer.java diff --git a/deob/src/main/java/dev/openrs2/deob/Deobfuscator.java b/deob/src/main/java/dev/openrs2/deob/Deobfuscator.java index c29bfc075f..0003f2755d 100644 --- a/deob/src/main/java/dev/openrs2/deob/Deobfuscator.java +++ b/deob/src/main/java/dev/openrs2/deob/Deobfuscator.java @@ -14,6 +14,7 @@ import dev.openrs2.asm.Transformer; import dev.openrs2.deob.path.ClassPath; import dev.openrs2.deob.path.TypedRemapper; import dev.openrs2.deob.transform.ClassForNameTransformer; +import dev.openrs2.deob.transform.ExceptionTracingTransformer; import dev.openrs2.deob.transform.OpaquePredicateTransformer; import dev.openrs2.deob.transform.OriginalNameTransformer; import org.slf4j.Logger; @@ -23,7 +24,8 @@ public final class Deobfuscator { private static final Logger logger = LoggerFactory.getLogger(Deobfuscator.class); private static final List TRANSFORMERS = List.of( - new OpaquePredicateTransformer() + new OpaquePredicateTransformer(), + new ExceptionTracingTransformer() ); public static void main(String[] args) throws IOException { diff --git a/deob/src/main/java/dev/openrs2/deob/transform/ExceptionTracingTransformer.java b/deob/src/main/java/dev/openrs2/deob/transform/ExceptionTracingTransformer.java new file mode 100644 index 0000000000..5b285258de --- /dev/null +++ b/deob/src/main/java/dev/openrs2/deob/transform/ExceptionTracingTransformer.java @@ -0,0 +1,45 @@ +package dev.openrs2.deob.transform; + +import dev.openrs2.asm.InsnMatcher; +import dev.openrs2.asm.InsnNodeUtils; +import dev.openrs2.asm.Library; +import dev.openrs2.asm.Transformer; +import org.objectweb.asm.tree.ClassNode; +import org.objectweb.asm.tree.MethodNode; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +public final class ExceptionTracingTransformer extends Transformer { + private static final Logger logger = LoggerFactory.getLogger(ExceptionTracingTransformer.class); + + private static final InsnMatcher CATCH_MATCHER = InsnMatcher.compile("ASTORE ALOAD (| LDC INVOKESTATIC | NEW DUP (LDC INVOKESPECIAL | INVOKESPECIAL LDC INVOKEVIRTUAL) ((ILOAD | LLOAD | FLOAD | DLOAD | (ALOAD IFNULL LDC GOTO LDC) | BIPUSH) INVOKEVIRTUAL)* INVOKEVIRTUAL INVOKESTATIC) ATHROW"); + + private int tracingTryCatches; + + @Override + public void preTransform(Library library) { + tracingTryCatches = 0; + } + + @Override + public void transformCode(ClassNode clazz, MethodNode method) { + CATCH_MATCHER.match(method).forEach(match -> { + var foundTryCatch = method.tryCatchBlocks.removeIf(tryCatch -> { + if (!"java/lang/RuntimeException".equals(tryCatch.type)) { + return false; + } + return InsnNodeUtils.nextReal(tryCatch.handler) == match.get(0); + }); + + if (foundTryCatch) { + match.forEach(method.instructions::remove); + tracingTryCatches++; + } + }); + } + + @Override + public void postTransform(Library library) { + logger.info("Removed {} tracing try/catch blocks", tracingTryCatches); + } +}