From 9b9bdeae63345ac73aec359015f303bf25ec7e3f Mon Sep 17 00:00:00 2001 From: Graham Date: Sat, 10 Aug 2019 20:00:19 +0100 Subject: [PATCH] Add multi-pass Transformer support --- .../openrs2/asm/transform/Transformer.java | 43 +++++++++++-------- .../deob/transform/BitShiftTransformer.java | 4 +- .../deob/transform/CanvasTransformer.java | 8 ++-- .../transform/ClassForNameTransformer.java | 4 +- .../deob/transform/CounterTransformer.java | 4 +- .../ExceptionTracingTransformer.java | 4 +- .../deob/transform/FieldOrderTransformer.java | 3 +- .../transform/OpaquePredicateTransformer.java | 4 +- .../transform/OriginalNameTransformer.java | 14 ++++-- 9 files changed, 56 insertions(+), 32 deletions(-) diff --git a/asm/src/main/java/dev/openrs2/asm/transform/Transformer.java b/asm/src/main/java/dev/openrs2/asm/transform/Transformer.java index 1dc99b9c..a3cbdb2e 100644 --- a/asm/src/main/java/dev/openrs2/asm/transform/Transformer.java +++ b/asm/src/main/java/dev/openrs2/asm/transform/Transformer.java @@ -11,23 +11,28 @@ public abstract class Transformer { public void transform(ClassPath classPath) throws AnalyzerException { preTransform(classPath); - for (var library : classPath.getLibraries()) { - for (var clazz : library) { - transformClass(clazz); + boolean changed; + do { + changed = false; - for (var field : clazz.fields) { - transformField(clazz, field); - } + for (var library : classPath.getLibraries()) { + for (var clazz : library) { + changed |= transformClass(clazz); + + for (var field : clazz.fields) { + changed |= transformField(clazz, field); + } - for (var method : clazz.methods) { - transformMethod(clazz, method); + for (var method : clazz.methods) { + changed |= transformMethod(clazz, method); - if ((method.access & (Opcodes.ACC_NATIVE | Opcodes.ACC_ABSTRACT)) == 0) { - transformCode(clazz, method); + if ((method.access & (Opcodes.ACC_NATIVE | Opcodes.ACC_ABSTRACT)) == 0) { + changed |= transformCode(clazz, method); + } } } } - } + } while (changed); postTransform(classPath); } @@ -36,20 +41,20 @@ public abstract class Transformer { /* empty */ } - public void transformClass(ClassNode clazz) throws AnalyzerException { - /* empty */ + public boolean transformClass(ClassNode clazz) throws AnalyzerException { + return false; } - public void transformField(ClassNode clazz, FieldNode field) throws AnalyzerException { - /* empty */ + public boolean transformField(ClassNode clazz, FieldNode field) throws AnalyzerException { + return false; } - public void transformMethod(ClassNode clazz, MethodNode method) throws AnalyzerException { - /* empty */ + public boolean transformMethod(ClassNode clazz, MethodNode method) throws AnalyzerException { + return false; } - public void transformCode(ClassNode clazz, MethodNode method) throws AnalyzerException { - /* empty */ + public boolean transformCode(ClassNode clazz, MethodNode method) throws AnalyzerException { + return false; } public void postTransform(ClassPath classPath) throws AnalyzerException { diff --git a/deob/src/main/java/dev/openrs2/deob/transform/BitShiftTransformer.java b/deob/src/main/java/dev/openrs2/deob/transform/BitShiftTransformer.java index b96bfe91..1bdb0b1d 100644 --- a/deob/src/main/java/dev/openrs2/deob/transform/BitShiftTransformer.java +++ b/deob/src/main/java/dev/openrs2/deob/transform/BitShiftTransformer.java @@ -26,7 +26,7 @@ public final class BitShiftTransformer extends Transformer { } @Override - public void transformCode(ClassNode clazz, MethodNode method) { + public boolean transformCode(ClassNode clazz, MethodNode method) { CONST_SHIFT_MATCHER.match(method).forEach(match -> { var push = match.get(0); var bits = InsnNodeUtils.getIntConstant(push); @@ -39,6 +39,8 @@ public final class BitShiftTransformer extends Transformer { simplified++; } }); + + return false; } @Override diff --git a/deob/src/main/java/dev/openrs2/deob/transform/CanvasTransformer.java b/deob/src/main/java/dev/openrs2/deob/transform/CanvasTransformer.java index 9bc73d3e..9defb292 100644 --- a/deob/src/main/java/dev/openrs2/deob/transform/CanvasTransformer.java +++ b/deob/src/main/java/dev/openrs2/deob/transform/CanvasTransformer.java @@ -6,15 +6,17 @@ import org.objectweb.asm.tree.ClassNode; public final class CanvasTransformer extends Transformer { @Override - public void transformClass(ClassNode clazz) { + public boolean transformClass(ClassNode clazz) { if (!"java/awt/Canvas".equals(clazz.superName)) { - return; + return false; } if ((clazz.access & Opcodes.ACC_FINAL) == 0) { - return; + return false; } clazz.interfaces.remove("java/awt/event/FocusListener"); + + return false; } } diff --git a/deob/src/main/java/dev/openrs2/deob/transform/ClassForNameTransformer.java b/deob/src/main/java/dev/openrs2/deob/transform/ClassForNameTransformer.java index 43a591f9..b8208d46 100644 --- a/deob/src/main/java/dev/openrs2/deob/transform/ClassForNameTransformer.java +++ b/deob/src/main/java/dev/openrs2/deob/transform/ClassForNameTransformer.java @@ -33,7 +33,7 @@ public final class ClassForNameTransformer extends Transformer { } @Override - public void transformCode(ClassNode clazz, MethodNode method) { + public boolean transformCode(ClassNode clazz, MethodNode method) { INVOKE_MATCHER.match(method).filter(ClassForNameTransformer::isClassForName).forEach(match -> { var ldc = (LdcInsnNode) match.get(0); var name = remapper.map((String) ldc.cst); @@ -41,5 +41,7 @@ public final class ClassForNameTransformer extends Transformer { ldc.cst = name; } }); + + return false; } } diff --git a/deob/src/main/java/dev/openrs2/deob/transform/CounterTransformer.java b/deob/src/main/java/dev/openrs2/deob/transform/CounterTransformer.java index 4e1ccc20..0c873cac 100644 --- a/deob/src/main/java/dev/openrs2/deob/transform/CounterTransformer.java +++ b/deob/src/main/java/dev/openrs2/deob/transform/CounterTransformer.java @@ -94,7 +94,7 @@ public final class CounterTransformer extends Transformer { } @Override - public void transformCode(ClassNode clazz, MethodNode method) { + public boolean transformCode(ClassNode clazz, MethodNode method) { RESET_PATTERN.match(method).forEach(match -> { var putstatic = (FieldInsnNode) match.get(1); if (counters.contains(new MemberRef(putstatic.owner, putstatic.name, putstatic.desc))) { @@ -111,6 +111,8 @@ public final class CounterTransformer extends Transformer { match.forEach(method.instructions::remove); } }); + + return false; } @Override diff --git a/deob/src/main/java/dev/openrs2/deob/transform/ExceptionTracingTransformer.java b/deob/src/main/java/dev/openrs2/deob/transform/ExceptionTracingTransformer.java index 0224a076..bb92f81b 100644 --- a/deob/src/main/java/dev/openrs2/deob/transform/ExceptionTracingTransformer.java +++ b/deob/src/main/java/dev/openrs2/deob/transform/ExceptionTracingTransformer.java @@ -22,7 +22,7 @@ public final class ExceptionTracingTransformer extends Transformer { } @Override - public void transformCode(ClassNode clazz, MethodNode method) { + public boolean transformCode(ClassNode clazz, MethodNode method) { CATCH_MATCHER.match(method).forEach(match -> { var foundTryCatch = method.tryCatchBlocks.removeIf(tryCatch -> { if (!"java/lang/RuntimeException".equals(tryCatch.type)) { @@ -36,6 +36,8 @@ public final class ExceptionTracingTransformer extends Transformer { tracingTryCatches++; } }); + + return false; } @Override diff --git a/deob/src/main/java/dev/openrs2/deob/transform/FieldOrderTransformer.java b/deob/src/main/java/dev/openrs2/deob/transform/FieldOrderTransformer.java index 68cbc1d5..66e80be1 100644 --- a/deob/src/main/java/dev/openrs2/deob/transform/FieldOrderTransformer.java +++ b/deob/src/main/java/dev/openrs2/deob/transform/FieldOrderTransformer.java @@ -45,8 +45,9 @@ public final class FieldOrderTransformer extends Transformer { } @Override - public void transformClass(ClassNode clazz) { + public boolean transformClass(ClassNode clazz) { sortFields(clazz, CONSTRUCTOR, Opcodes.PUTFIELD); sortFields(clazz, STATIC_CONSTRUCTOR, Opcodes.PUTSTATIC); + return false; } } diff --git a/deob/src/main/java/dev/openrs2/deob/transform/OpaquePredicateTransformer.java b/deob/src/main/java/dev/openrs2/deob/transform/OpaquePredicateTransformer.java index 8bb1a72a..b4af7920 100644 --- a/deob/src/main/java/dev/openrs2/deob/transform/OpaquePredicateTransformer.java +++ b/deob/src/main/java/dev/openrs2/deob/transform/OpaquePredicateTransformer.java @@ -91,7 +91,7 @@ public final class OpaquePredicateTransformer extends Transformer { } @Override - public void transformCode(ClassNode clazz, MethodNode method) { + public boolean transformCode(ClassNode clazz, MethodNode method) { /* find and fix opaque predicates */ OPAQUE_PREDICATE_MATCHER.match(method).filter(match -> isOpaquePredicate(method, match)).forEach(match -> { var branch = (JumpInsnNode) match.get(1); @@ -113,6 +113,8 @@ public final class OpaquePredicateTransformer extends Transformer { match.forEach(method.instructions::remove); stores++; }); + + return false; } @Override diff --git a/deob/src/main/java/dev/openrs2/deob/transform/OriginalNameTransformer.java b/deob/src/main/java/dev/openrs2/deob/transform/OriginalNameTransformer.java index 3dbb213d..d6e3bf5d 100644 --- a/deob/src/main/java/dev/openrs2/deob/transform/OriginalNameTransformer.java +++ b/deob/src/main/java/dev/openrs2/deob/transform/OriginalNameTransformer.java @@ -37,26 +37,30 @@ public final class OriginalNameTransformer extends Transformer { } @Override - public void transformClass(ClassNode clazz) { + public boolean transformClass(ClassNode clazz) { if (clazz.invisibleAnnotations == null) { clazz.invisibleAnnotations = new ArrayList<>(); } clazz.invisibleAnnotations.add(createOriginalClassAnnotation(clazz.name)); + + return false; } @Override - public void transformField(ClassNode clazz, FieldNode field) { + public boolean transformField(ClassNode clazz, FieldNode field) { if (field.invisibleAnnotations == null) { field.invisibleAnnotations = new ArrayList<>(); } field.invisibleAnnotations.add(createOriginalMemberAnnotation(clazz.name, field.name, field.desc)); + + return false; } @SuppressWarnings("unchecked") @Override - public void transformMethod(ClassNode clazz, MethodNode method) { + public boolean transformMethod(ClassNode clazz, MethodNode method) { if (method.name.equals("") || method.name.equals("")) { - return; + return false; } if (method.invisibleAnnotations == null) { @@ -75,5 +79,7 @@ public final class OriginalNameTransformer extends Transformer { } annotations.add(createOriginalArgAnnotation(i)); } + + return false; } }