From f93984a743cb8c3a8870513675807867d72c727f Mon Sep 17 00:00:00 2001 From: Lucwousin Date: Wed, 30 Oct 2019 10:30:37 +0100 Subject: [PATCH] Deob changes for injector and general usability --- .../main/java/net/runelite/asm/Annotated.java | 17 +++++ .../main/java/net/runelite/asm/ClassFile.java | 8 +-- .../java/net/runelite/asm/ClassGroup.java | 18 ++++- .../src/main/java/net/runelite/asm/Field.java | 9 +-- .../java/net/runelite/asm/Interfaces.java | 10 ++- .../main/java/net/runelite/asm/Method.java | 6 +- .../src/main/java/net/runelite/asm/Named.java | 6 ++ .../runelite/asm/attributes/Annotations.java | 27 +++++--- .../net/runelite/asm/attributes/Code.java | 19 +++--- .../asm/attributes/annotation/Annotation.java | 61 +++++++++++------ .../attributes/annotation/ArrayElement.java | 42 ++++++++++++ .../asm/attributes/annotation/Element.java | 57 +++++++++++----- .../attributes/annotation/SimpleElement.java | 15 ++++ .../asm/attributes/code/Instructions.java | 26 ++++++- ...tor.java => AnnotationElementVisitor.java} | 59 ++++++++++------ .../asm/visitors/ClassAnnotationVisitor.java | 68 ------------------- .../asm/visitors/ClassFieldVisitor.java | 24 +++---- .../asm/visitors/ClassFileVisitor.java | 9 ++- .../runelite/asm/visitors/CodeVisitor.java | 14 ++-- .../asm/visitors/FieldAnnotationVisitor.java | 68 ------------------- .../net/runelite/deob/DeobAnnotations.java | 2 +- .../runelite/deob/deobfuscators/Renamer.java | 1 + .../constparam/ConstantParameter.java | 10 ++- .../mapping/AnnotationMapper.java | 15 ++-- .../deob/updater/AnnotationAdder.java | 23 +++---- .../deob/updater/AnnotationCopier.java | 10 ++- .../deob/updater/AnnotationRenamer.java | 4 -- .../java/net/runelite/deob/util/JarUtil.java | 37 ++++++++++ .../java/net/runelite/osb/HookImporter.java | 5 +- 29 files changed, 365 insertions(+), 305 deletions(-) create mode 100644 deobfuscator/src/main/java/net/runelite/asm/Annotated.java create mode 100644 deobfuscator/src/main/java/net/runelite/asm/Named.java create mode 100644 deobfuscator/src/main/java/net/runelite/asm/attributes/annotation/ArrayElement.java create mode 100644 deobfuscator/src/main/java/net/runelite/asm/attributes/annotation/SimpleElement.java rename deobfuscator/src/main/java/net/runelite/asm/visitors/{MethodAnnotationVisitor.java => AnnotationElementVisitor.java} (62%) delete mode 100644 deobfuscator/src/main/java/net/runelite/asm/visitors/ClassAnnotationVisitor.java delete mode 100644 deobfuscator/src/main/java/net/runelite/asm/visitors/FieldAnnotationVisitor.java diff --git a/deobfuscator/src/main/java/net/runelite/asm/Annotated.java b/deobfuscator/src/main/java/net/runelite/asm/Annotated.java new file mode 100644 index 0000000000..4645fa23a2 --- /dev/null +++ b/deobfuscator/src/main/java/net/runelite/asm/Annotated.java @@ -0,0 +1,17 @@ +package net.runelite.asm; + +import java.util.Iterator; +import net.runelite.asm.attributes.Annotations; +import net.runelite.asm.attributes.annotation.Annotation; +import org.jetbrains.annotations.NotNull; + +public interface Annotated extends Iterable +{ + Annotations getAnnotations(); + + @NotNull + default Iterator iterator() + { + return getAnnotations().iterator(); + } +} diff --git a/deobfuscator/src/main/java/net/runelite/asm/ClassFile.java b/deobfuscator/src/main/java/net/runelite/asm/ClassFile.java index a5f3a6ecb6..fd2ba5a40a 100644 --- a/deobfuscator/src/main/java/net/runelite/asm/ClassFile.java +++ b/deobfuscator/src/main/java/net/runelite/asm/ClassFile.java @@ -31,13 +31,12 @@ import net.runelite.asm.attributes.annotation.Annotation; import net.runelite.asm.pool.Class; import net.runelite.asm.signature.Signature; import static net.runelite.deob.DeobAnnotations.*; -import org.objectweb.asm.AnnotationVisitor; import org.objectweb.asm.ClassVisitor; import org.objectweb.asm.FieldVisitor; import org.objectweb.asm.MethodVisitor; import org.objectweb.asm.Opcodes; -public class ClassFile +public class ClassFile implements Annotated, Named { private ClassGroup group; @@ -100,10 +99,9 @@ public class ClassFile visitor.visit(version, access, name.getName(), null, super_class.getName(), ints); visitor.visitSource(source, null); - for (Annotation annotation : annotations.getAnnotations()) + for (Annotation annotation : annotations) { - AnnotationVisitor av = visitor.visitAnnotation(annotation.getType().toString(), true); - annotation.accept(av); + annotation.accept(visitor.visitAnnotation(annotation.getType().toString(), true)); } for (Field field : fields) diff --git a/deobfuscator/src/main/java/net/runelite/asm/ClassGroup.java b/deobfuscator/src/main/java/net/runelite/asm/ClassGroup.java index 33ab161acc..fabde88742 100644 --- a/deobfuscator/src/main/java/net/runelite/asm/ClassGroup.java +++ b/deobfuscator/src/main/java/net/runelite/asm/ClassGroup.java @@ -27,13 +27,16 @@ package net.runelite.asm; import java.util.ArrayList; import java.util.Collections; import java.util.HashMap; +import java.util.Iterator; import java.util.List; import java.util.Map; +import java.util.function.Consumer; import net.runelite.asm.attributes.Code; import net.runelite.asm.signature.Signature; import static net.runelite.deob.DeobAnnotations.*; +import org.jetbrains.annotations.NotNull; -public class ClassGroup +public class ClassGroup implements Iterable { private final List classes = new ArrayList<>(); // to keep order private final Map classMap = new HashMap<>(); @@ -156,4 +159,17 @@ public class ClassGroup return findClass(name); } + + @NotNull + @Override + public Iterator iterator() + { + return this.classes.iterator(); + } + + @Override + public void forEach(Consumer action) + { + this.classes.forEach(action); + } } diff --git a/deobfuscator/src/main/java/net/runelite/asm/Field.java b/deobfuscator/src/main/java/net/runelite/asm/Field.java index 39590b021c..1a9fd7a776 100644 --- a/deobfuscator/src/main/java/net/runelite/asm/Field.java +++ b/deobfuscator/src/main/java/net/runelite/asm/Field.java @@ -27,15 +27,13 @@ package net.runelite.asm; import net.runelite.asm.attributes.Annotations; import net.runelite.asm.attributes.annotation.Annotation; import net.runelite.deob.DeobAnnotations; -import org.objectweb.asm.AnnotationVisitor; import org.objectweb.asm.FieldVisitor; import org.objectweb.asm.Opcodes; - import static org.objectweb.asm.Opcodes.ACC_PRIVATE; import static org.objectweb.asm.Opcodes.ACC_PROTECTED; import static org.objectweb.asm.Opcodes.ACC_PUBLIC; -public class Field +public class Field implements Annotated, Named { public static final int ACCESS_MODIFIERS = ACC_PUBLIC | ACC_PRIVATE | ACC_PROTECTED; @@ -53,15 +51,14 @@ public class Field this.name = name; this.type = type; - annotations = new Annotations(); + this.annotations = new Annotations(); } public void accept(FieldVisitor visitor) { for (Annotation annotation : annotations.getAnnotations()) { - AnnotationVisitor av = visitor.visitAnnotation(annotation.getType().toString(), true); - annotation.accept(av); + annotation.accept(visitor.visitAnnotation(annotation.getType().toString(), true)); } visitor.visitEnd(); diff --git a/deobfuscator/src/main/java/net/runelite/asm/Interfaces.java b/deobfuscator/src/main/java/net/runelite/asm/Interfaces.java index f5994ad1d3..9bc15d5100 100644 --- a/deobfuscator/src/main/java/net/runelite/asm/Interfaces.java +++ b/deobfuscator/src/main/java/net/runelite/asm/Interfaces.java @@ -25,12 +25,14 @@ package net.runelite.asm; import java.util.ArrayList; +import java.util.Iterator; import java.util.List; import java.util.stream.Collectors; import net.runelite.asm.pool.Class; import net.runelite.deob.DeobAnnotations; +import org.jetbrains.annotations.NotNull; -public class Interfaces +public class Interfaces implements Iterable { private final ClassFile classFile; @@ -107,4 +109,10 @@ public class Interfaces return names; } + + @NotNull + public Iterator iterator() + { + return this.interfaces.iterator(); + } } diff --git a/deobfuscator/src/main/java/net/runelite/asm/Method.java b/deobfuscator/src/main/java/net/runelite/asm/Method.java index ad7394c7be..b13e98aa3f 100644 --- a/deobfuscator/src/main/java/net/runelite/asm/Method.java +++ b/deobfuscator/src/main/java/net/runelite/asm/Method.java @@ -36,7 +36,6 @@ import net.runelite.asm.attributes.code.Parameter; import net.runelite.asm.attributes.code.instruction.types.LVTInstruction; import net.runelite.asm.signature.Signature; import net.runelite.deob.DeobAnnotations; -import org.objectweb.asm.AnnotationVisitor; import org.objectweb.asm.Label; import org.objectweb.asm.MethodVisitor; import static org.objectweb.asm.Opcodes.ACC_FINAL; @@ -47,7 +46,7 @@ import static org.objectweb.asm.Opcodes.ACC_PUBLIC; import static org.objectweb.asm.Opcodes.ACC_STATIC; import static org.objectweb.asm.Opcodes.ACC_SYNCHRONIZED; -public class Method +public class Method implements Annotated, Named { public static final int ACCESS_MODIFIERS = ACC_PUBLIC | ACC_PRIVATE | ACC_PROTECTED; @@ -92,8 +91,7 @@ public class Method for (Annotation annotation : annotations.getAnnotations()) { - AnnotationVisitor av = visitor.visitAnnotation(annotation.getType().toString(), true); - annotation.accept(av); + annotation.accept(visitor.visitAnnotation(annotation.getType().toString(), true)); } if (code != null) diff --git a/deobfuscator/src/main/java/net/runelite/asm/Named.java b/deobfuscator/src/main/java/net/runelite/asm/Named.java new file mode 100644 index 0000000000..ec2bc0b6a9 --- /dev/null +++ b/deobfuscator/src/main/java/net/runelite/asm/Named.java @@ -0,0 +1,6 @@ +package net.runelite.asm; + +public interface Named +{ + String getName(); +} diff --git a/deobfuscator/src/main/java/net/runelite/asm/attributes/Annotations.java b/deobfuscator/src/main/java/net/runelite/asm/attributes/Annotations.java index b14d761c28..7c1a6ba535 100644 --- a/deobfuscator/src/main/java/net/runelite/asm/attributes/Annotations.java +++ b/deobfuscator/src/main/java/net/runelite/asm/attributes/Annotations.java @@ -26,13 +26,16 @@ package net.runelite.asm.attributes; import java.util.ArrayList; +import java.util.Iterator; import java.util.List; import net.runelite.asm.Type; import net.runelite.asm.attributes.annotation.Annotation; import net.runelite.asm.attributes.annotation.Element; +import net.runelite.asm.attributes.annotation.SimpleElement; +import org.jetbrains.annotations.NotNull; -public class Annotations +public class Annotations implements Iterable { private final List annotations = new ArrayList<>(); @@ -40,7 +43,7 @@ public class Annotations { return annotations; } - + public void addAnnotation(Annotation annotation) { annotations.add(annotation); @@ -55,7 +58,7 @@ public class Annotations { annotations.clear(); } - + public Annotation find(Type type) { for (Annotation a : annotations) @@ -68,18 +71,22 @@ public class Annotations { return annotations.size(); } - + public Annotation addAnnotation(Type type, String name, Object value) { - Annotation annotation = new Annotation(this); - annotation.setType(type); + Annotation annotation = new Annotation(type); addAnnotation(annotation); - - Element element = new Element(annotation); - element.setName(name); - element.setValue(value); + + Element element = new SimpleElement(name, value); annotation.addElement(element); return annotation; } + + @NotNull + @Override + public Iterator iterator() + { + return this.annotations.iterator(); + } } diff --git a/deobfuscator/src/main/java/net/runelite/asm/attributes/Code.java b/deobfuscator/src/main/java/net/runelite/asm/attributes/Code.java index b20c183fea..58c759844a 100644 --- a/deobfuscator/src/main/java/net/runelite/asm/attributes/Code.java +++ b/deobfuscator/src/main/java/net/runelite/asm/attributes/Code.java @@ -41,11 +41,11 @@ public class Code private int maxStack; private Instructions instructions; private final Exceptions exceptions; - + public Code(Method method) { this.method = method; - + exceptions = new Exceptions(this); instructions = new Instructions(this); } @@ -59,12 +59,12 @@ public class Code { return maxStack; } - + public void setMaxStack(int maxStack) { this.maxStack = maxStack; } - + private int getMaxLocalsFromSig() { Method m = getMethod(); @@ -77,12 +77,11 @@ public class Code /** * calculates the size of the lvt required for this method - * @return */ public int getMaxLocals() { int max = -1; - + for (Instruction ins : instructions.getInstructions()) { if (ins instanceof LVTInstruction) @@ -96,19 +95,19 @@ public class Code } } } - + int fromSig = getMaxLocalsFromSig(); if (fromSig > max) max = fromSig; - + return max; } - + public Exceptions getExceptions() { return exceptions; } - + public Instructions getInstructions() { return instructions; diff --git a/deobfuscator/src/main/java/net/runelite/asm/attributes/annotation/Annotation.java b/deobfuscator/src/main/java/net/runelite/asm/attributes/annotation/Annotation.java index 7c727eb788..6f3a459eb3 100644 --- a/deobfuscator/src/main/java/net/runelite/asm/attributes/annotation/Annotation.java +++ b/deobfuscator/src/main/java/net/runelite/asm/attributes/annotation/Annotation.java @@ -26,30 +26,26 @@ package net.runelite.asm.attributes.annotation; import java.util.ArrayList; +import java.util.Iterator; import java.util.List; - import net.runelite.asm.Type; -import net.runelite.asm.attributes.Annotations; +import org.jetbrains.annotations.NotNull; import org.objectweb.asm.AnnotationVisitor; -public class Annotation +public class Annotation extends Element> implements Iterable { - private final Annotations annotations; - private Type type; - private final List elements = new ArrayList<>(); + private final Type type; - public Annotation(Annotations annotations) + public Annotation(Type type) { - this.annotations = annotations; + this.value = new ArrayList<>(); + this.type = type; } - public Annotations getAnnotations() - { - return annotations; - } - - public void setType(Type type) + public Annotation(String name, Type type) { + this.value = new ArrayList<>(); + this.name = name; this.type = type; } @@ -60,23 +56,44 @@ public class Annotation public List getElements() { - return elements; + return value; } - + public Element getElement() { - return elements.get(0); + return value.get(0); } - + public void addElement(Element element) { - elements.add(element); + value.add(element); } - + + @Override + public final void setValue(List value) + { + throw new UnsupportedOperationException(); + } + public void accept(AnnotationVisitor visitor) { - for (Element element : elements) - visitor.visit(element.getName(), element.getValue()); + if (visitor == null) + { + return; + } + + for (Element element : this) + { + accept(visitor, element.name, element.value); + } + visitor.visitEnd(); } + + @NotNull + @Override + public Iterator iterator() + { + return this.value.iterator(); + } } diff --git a/deobfuscator/src/main/java/net/runelite/asm/attributes/annotation/ArrayElement.java b/deobfuscator/src/main/java/net/runelite/asm/attributes/annotation/ArrayElement.java new file mode 100644 index 0000000000..b7e2b7ef84 --- /dev/null +++ b/deobfuscator/src/main/java/net/runelite/asm/attributes/annotation/ArrayElement.java @@ -0,0 +1,42 @@ +package net.runelite.asm.attributes.annotation; + +import java.util.ArrayList; +import java.util.Iterator; +import java.util.List; +import java.util.stream.Stream; +import org.jetbrains.annotations.NotNull; + +public class ArrayElement extends Element implements Iterable +{ + public ArrayElement(String name) + { + this.name = name; + this.value = new ArrayList<>(); + } + + @SuppressWarnings("unchecked") + public void addValue(Object value) + { + this.value.add(value); + } + + @Override + public final void setValue(List value) + { + throw new UnsupportedOperationException(); + } + + @NotNull + @Override + @SuppressWarnings("unchecked") + public Iterator iterator() + { + return this.value.iterator(); + } + + @SuppressWarnings("unchecked") + public Stream stream() + { + return this.value.stream(); + } +} diff --git a/deobfuscator/src/main/java/net/runelite/asm/attributes/annotation/Element.java b/deobfuscator/src/main/java/net/runelite/asm/attributes/annotation/Element.java index 18fdfa33f4..10b59b5b27 100644 --- a/deobfuscator/src/main/java/net/runelite/asm/attributes/annotation/Element.java +++ b/deobfuscator/src/main/java/net/runelite/asm/attributes/annotation/Element.java @@ -25,21 +25,14 @@ package net.runelite.asm.attributes.annotation; -public class Element -{ - private final Annotation annotation; - private String name; - private Object value; - - public Element(Annotation annotation) - { - this.annotation = annotation; - } +import java.util.List; +import org.objectweb.asm.AnnotationVisitor; - public Annotation getAnnotation() - { - return annotation; - } +public abstract class Element +{ + String name = "value"; + + T value; public String getName() { @@ -51,18 +44,48 @@ public class Element this.name = name; } - public Object getValue() + public T getValue() { return value; } - public void setValue(Object value) + public void setValue(T value) { this.value = value; } - + public String getString() { return value.toString(); } + + public static void accept(AnnotationVisitor visitor, final String name, final Object value) + { + if (visitor == null) + { + return; + } + + if (value instanceof Annotation) + { + Annotation annotation = (Annotation) value; + annotation.accept(visitor.visitAnnotation(name, annotation.getType().toString())); + } + else if (value instanceof List) + { + AnnotationVisitor arr = visitor.visitArray(name); + List arrayValue = (List) value; + + for (Object o : arrayValue) + { + accept(arr, null, o); + } + + arr.visitEnd(); + } + else + { + visitor.visit(name, value); + } + } } diff --git a/deobfuscator/src/main/java/net/runelite/asm/attributes/annotation/SimpleElement.java b/deobfuscator/src/main/java/net/runelite/asm/attributes/annotation/SimpleElement.java new file mode 100644 index 0000000000..52e25e295c --- /dev/null +++ b/deobfuscator/src/main/java/net/runelite/asm/attributes/annotation/SimpleElement.java @@ -0,0 +1,15 @@ +package net.runelite.asm.attributes.annotation; + +public class SimpleElement extends Element +{ + public SimpleElement(Object value) + { + this.value = value; + } + + public SimpleElement(String name, Object value) + { + this.name = name; + this.value = value; + } +} diff --git a/deobfuscator/src/main/java/net/runelite/asm/attributes/code/Instructions.java b/deobfuscator/src/main/java/net/runelite/asm/attributes/code/Instructions.java index a528950d0a..212c168998 100644 --- a/deobfuscator/src/main/java/net/runelite/asm/attributes/code/Instructions.java +++ b/deobfuscator/src/main/java/net/runelite/asm/attributes/code/Instructions.java @@ -26,11 +26,14 @@ package net.runelite.asm.attributes.code; import java.util.ArrayList; import java.util.HashMap; +import java.util.Iterator; import java.util.List; +import java.util.ListIterator; import java.util.Map; import net.runelite.asm.attributes.Code; +import org.jetbrains.annotations.NotNull; -public class Instructions +public class Instructions implements Iterable { private final Code code; private final List instructions = new ArrayList<>(); @@ -186,4 +189,25 @@ public class Instructions return i; } + + public int size() + { + return this.instructions.size(); + } + + @NotNull + public Iterator iterator() + { + return this.instructions.iterator(); + } + + public ListIterator listIterator() + { + return this.instructions.listIterator(); + } + + public ListIterator listIterator(int i) + { + return this.instructions.listIterator(i); + } } diff --git a/deobfuscator/src/main/java/net/runelite/asm/visitors/MethodAnnotationVisitor.java b/deobfuscator/src/main/java/net/runelite/asm/visitors/AnnotationElementVisitor.java similarity index 62% rename from deobfuscator/src/main/java/net/runelite/asm/visitors/MethodAnnotationVisitor.java rename to deobfuscator/src/main/java/net/runelite/asm/visitors/AnnotationElementVisitor.java index bcce61aedd..7bd5811813 100644 --- a/deobfuscator/src/main/java/net/runelite/asm/visitors/MethodAnnotationVisitor.java +++ b/deobfuscator/src/main/java/net/runelite/asm/visitors/AnnotationElementVisitor.java @@ -25,44 +25,59 @@ package net.runelite.asm.visitors; -import net.runelite.asm.Method; import net.runelite.asm.Type; import net.runelite.asm.attributes.annotation.Annotation; -import net.runelite.asm.attributes.annotation.Element; +import net.runelite.asm.attributes.annotation.ArrayElement; +import net.runelite.asm.attributes.annotation.SimpleElement; import org.objectweb.asm.AnnotationVisitor; import org.objectweb.asm.Opcodes; -public class MethodAnnotationVisitor extends AnnotationVisitor +public class AnnotationElementVisitor extends AnnotationVisitor { - private final Method method; - private final Type type; private final Annotation annotation; - - public MethodAnnotationVisitor(Method method, Type type) + + AnnotationElementVisitor(Annotation annotation) { super(Opcodes.ASM5); - - this.method = method; - this.type = type; - - annotation = new Annotation(method.getAnnotations()); - annotation.setType(type); + + this.annotation = annotation; } - + @Override public void visit(String name, Object value) { - Element element = new Element(annotation); - - element.setName(name); - element.setValue(value); - + SimpleElement element = new SimpleElement(name, value); annotation.addElement(element); } - + @Override - public void visitEnd() + public AnnotationVisitor visitArray(String name) { - method.getAnnotations().addAnnotation(annotation); + ArrayElement element = new ArrayElement(name); + this.annotation.addElement(element); + return new AnnotationVisitor(Opcodes.ASM5) + { + @Override + public void visit(String name, Object value) + { + element.addValue(value); + } + + @Override + public AnnotationVisitor visitAnnotation(String name, String descriptor) + { + Annotation annotation = new Annotation(name, new Type(descriptor)); + element.addValue(annotation); + return new AnnotationElementVisitor(annotation); + } + }; + } + + @Override + public AnnotationVisitor visitAnnotation(String name, String descriptor) + { + Annotation annotation = new Annotation(name, new Type(descriptor)); + this.annotation.addElement(annotation); + return new AnnotationElementVisitor(annotation); } } diff --git a/deobfuscator/src/main/java/net/runelite/asm/visitors/ClassAnnotationVisitor.java b/deobfuscator/src/main/java/net/runelite/asm/visitors/ClassAnnotationVisitor.java deleted file mode 100644 index f9f62fc82a..0000000000 --- a/deobfuscator/src/main/java/net/runelite/asm/visitors/ClassAnnotationVisitor.java +++ /dev/null @@ -1,68 +0,0 @@ -/* - * Copyright (c) 2016-2017, Adam - * All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions are met: - * - * 1. Redistributions of source code must retain the above copyright notice, this - * list of conditions and the following disclaimer. - * 2. Redistributions in binary form must reproduce the above copyright notice, - * this list of conditions and the following disclaimer in the documentation - * and/or other materials provided with the distribution. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND - * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED - * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE - * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR - * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES - * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; - * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND - * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT - * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS - * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - */ - -package net.runelite.asm.visitors; - -import net.runelite.asm.ClassFile; -import net.runelite.asm.Type; -import net.runelite.asm.attributes.annotation.Annotation; -import net.runelite.asm.attributes.annotation.Element; -import org.objectweb.asm.AnnotationVisitor; -import org.objectweb.asm.Opcodes; - -public class ClassAnnotationVisitor extends AnnotationVisitor -{ - private final ClassFile classFile; - private final Type type; - private final Annotation annotation; - - public ClassAnnotationVisitor(ClassFile classFile, Type type) - { - super(Opcodes.ASM5); - - this.classFile = classFile; - this.type = type; - - annotation = new Annotation(classFile.getAnnotations()); - annotation.setType(type); - } - - @Override - public void visit(String name, Object value) - { - Element element = new Element(annotation); - - element.setName(name); - element.setValue(value); - - annotation.addElement(element); - } - - @Override - public void visitEnd() - { - classFile.getAnnotations().addAnnotation(annotation); - } -} diff --git a/deobfuscator/src/main/java/net/runelite/asm/visitors/ClassFieldVisitor.java b/deobfuscator/src/main/java/net/runelite/asm/visitors/ClassFieldVisitor.java index 37478d26e3..c9012b162c 100644 --- a/deobfuscator/src/main/java/net/runelite/asm/visitors/ClassFieldVisitor.java +++ b/deobfuscator/src/main/java/net/runelite/asm/visitors/ClassFieldVisitor.java @@ -22,12 +22,12 @@ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ - package net.runelite.asm.visitors; import net.runelite.asm.ClassFile; import net.runelite.asm.Field; import net.runelite.asm.Type; +import net.runelite.asm.attributes.annotation.Annotation; import org.objectweb.asm.AnnotationVisitor; import org.objectweb.asm.Attribute; import org.objectweb.asm.FieldVisitor; @@ -35,25 +35,25 @@ import org.objectweb.asm.Opcodes; public class ClassFieldVisitor extends FieldVisitor { - private final ClassFile classFile; private final Field field; - public ClassFieldVisitor(ClassFile cf, int access, String name, Type desc, Object value) + ClassFieldVisitor(ClassFile cf, int access, String name, Type desc, Object value) { super(Opcodes.ASM5); - this.classFile = cf; + this.field = new Field(cf, name, desc); + this.field.setAccessFlags(access); + this.field.setValue(value); - field = new Field(cf, name, desc); - field.setAccessFlags(access); - field.setValue(value); + cf.addField(field); } @Override public AnnotationVisitor visitAnnotation(String desc, boolean visible) { - Type type = new Type(desc); - return new FieldAnnotationVisitor(field, type); + Annotation element = new Annotation(new Type(desc)); + this.field.getAnnotations().addAnnotation(element); + return new AnnotationElementVisitor(element); } @Override @@ -61,10 +61,4 @@ public class ClassFieldVisitor extends FieldVisitor { System.out.println(attr); } - - @Override - public void visitEnd() - { - classFile.addField(field); - } } diff --git a/deobfuscator/src/main/java/net/runelite/asm/visitors/ClassFileVisitor.java b/deobfuscator/src/main/java/net/runelite/asm/visitors/ClassFileVisitor.java index abaf4e919d..d96ec550b1 100644 --- a/deobfuscator/src/main/java/net/runelite/asm/visitors/ClassFileVisitor.java +++ b/deobfuscator/src/main/java/net/runelite/asm/visitors/ClassFileVisitor.java @@ -27,6 +27,7 @@ package net.runelite.asm.visitors; import net.runelite.asm.ClassFile; import net.runelite.asm.Type; +import net.runelite.asm.attributes.annotation.Annotation; import net.runelite.asm.signature.Signature; import org.objectweb.asm.AnnotationVisitor; import org.objectweb.asm.ClassVisitor; @@ -55,7 +56,7 @@ public class ClassFileVisitor extends ClassVisitor classFile.setSuperName(superName); classFile.setVersion(version); classFile.setAccess(access); - + for (String inter : interfaces) classFile.getInterfaces().addInterface(new net.runelite.asm.pool.Class(inter)); } @@ -69,8 +70,10 @@ public class ClassFileVisitor extends ClassVisitor @Override public AnnotationVisitor visitAnnotation(String desc, boolean visible) { - Type type = new Type(desc); - return new ClassAnnotationVisitor(classFile, type); + Annotation annotation = new Annotation(new Type(desc)); + classFile.getAnnotations().addAnnotation(annotation); + + return new AnnotationElementVisitor(annotation); } @Override diff --git a/deobfuscator/src/main/java/net/runelite/asm/visitors/CodeVisitor.java b/deobfuscator/src/main/java/net/runelite/asm/visitors/CodeVisitor.java index d01873b83b..7746cd7a7a 100644 --- a/deobfuscator/src/main/java/net/runelite/asm/visitors/CodeVisitor.java +++ b/deobfuscator/src/main/java/net/runelite/asm/visitors/CodeVisitor.java @@ -32,6 +32,7 @@ import net.runelite.asm.ClassFile; import net.runelite.asm.Method; import net.runelite.asm.Type; import net.runelite.asm.attributes.Code; +import net.runelite.asm.attributes.annotation.Annotation; import net.runelite.asm.attributes.code.Exceptions; import net.runelite.asm.attributes.code.Instruction; import net.runelite.asm.attributes.code.InstructionType; @@ -72,18 +73,14 @@ import static org.objectweb.asm.Opcodes.ICONST_5; import static org.objectweb.asm.Opcodes.ICONST_M1; import static org.objectweb.asm.Opcodes.LCONST_0; import static org.objectweb.asm.Opcodes.LCONST_1; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; public class CodeVisitor extends MethodVisitor { - private static final Logger logger = LoggerFactory.getLogger(CodeVisitor.class); - private final ClassFile classFile; private final Method method; private Code code; - public CodeVisitor(ClassFile classFile, int access, String name, Signature signature, String[] sexceptions) + CodeVisitor(ClassFile classFile, int access, String name, Signature signature, String[] sexceptions) { super(Opcodes.ASM5); @@ -111,8 +108,9 @@ public class CodeVisitor extends MethodVisitor @Override public AnnotationVisitor visitAnnotation(String desc, boolean visible) { - Type type = new Type(desc); - return new MethodAnnotationVisitor(method, type); + Annotation element = new Annotation(new Type(desc)); + this.method.getAnnotations().addAnnotation(element); + return new AnnotationElementVisitor(element); } @Override @@ -333,7 +331,7 @@ public class CodeVisitor extends MethodVisitor if (cst instanceof org.objectweb.asm.Type) { org.objectweb.asm.Type t = (org.objectweb.asm.Type) cst; - entry = new net.runelite.asm.pool.Class((String) t.getClassName()); + entry = new net.runelite.asm.pool.Class(t.getClassName()); } LDC ldc = new LDC(code.getInstructions(), entry); diff --git a/deobfuscator/src/main/java/net/runelite/asm/visitors/FieldAnnotationVisitor.java b/deobfuscator/src/main/java/net/runelite/asm/visitors/FieldAnnotationVisitor.java deleted file mode 100644 index 962328f272..0000000000 --- a/deobfuscator/src/main/java/net/runelite/asm/visitors/FieldAnnotationVisitor.java +++ /dev/null @@ -1,68 +0,0 @@ -/* - * Copyright (c) 2016-2017, Adam - * All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions are met: - * - * 1. Redistributions of source code must retain the above copyright notice, this - * list of conditions and the following disclaimer. - * 2. Redistributions in binary form must reproduce the above copyright notice, - * this list of conditions and the following disclaimer in the documentation - * and/or other materials provided with the distribution. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND - * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED - * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE - * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR - * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES - * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; - * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND - * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT - * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS - * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - */ - -package net.runelite.asm.visitors; - -import net.runelite.asm.Field; -import net.runelite.asm.Type; -import net.runelite.asm.attributes.annotation.Annotation; -import net.runelite.asm.attributes.annotation.Element; -import org.objectweb.asm.AnnotationVisitor; -import org.objectweb.asm.Opcodes; - -public class FieldAnnotationVisitor extends AnnotationVisitor -{ - private final Field field; - private final Type type; - private final Annotation annotation; - - public FieldAnnotationVisitor(Field field, Type type) - { - super(Opcodes.ASM5); - - this.field = field; - this.type = type; - - annotation = new Annotation(field.getAnnotations()); - annotation.setType(type); - } - - @Override - public void visit(String name, Object value) - { - Element element = new Element(annotation); - - element.setName(name); - element.setValue(value); - - annotation.addElement(element); - } - - @Override - public void visitEnd() - { - field.getAnnotations().addAnnotation(annotation); - } -} diff --git a/deobfuscator/src/main/java/net/runelite/deob/DeobAnnotations.java b/deobfuscator/src/main/java/net/runelite/deob/DeobAnnotations.java index 2881148caa..6294574e74 100644 --- a/deobfuscator/src/main/java/net/runelite/deob/DeobAnnotations.java +++ b/deobfuscator/src/main/java/net/runelite/deob/DeobAnnotations.java @@ -95,7 +95,7 @@ public class DeobAnnotations { return null; } - + return (Number) an.getElement().getValue(); } diff --git a/deobfuscator/src/main/java/net/runelite/deob/deobfuscators/Renamer.java b/deobfuscator/src/main/java/net/runelite/deob/deobfuscators/Renamer.java index 418802ef5a..588b840918 100644 --- a/deobfuscator/src/main/java/net/runelite/deob/deobfuscators/Renamer.java +++ b/deobfuscator/src/main/java/net/runelite/deob/deobfuscators/Renamer.java @@ -193,6 +193,7 @@ public class Renamer implements Deobfuscator } @Override + @SuppressWarnings("unchecked") public void run(ClassGroup group) { group.buildClassGraph(); diff --git a/deobfuscator/src/main/java/net/runelite/deob/deobfuscators/constparam/ConstantParameter.java b/deobfuscator/src/main/java/net/runelite/deob/deobfuscators/constparam/ConstantParameter.java index 0970a9b3e1..b2a0c369b3 100644 --- a/deobfuscator/src/main/java/net/runelite/deob/deobfuscators/constparam/ConstantParameter.java +++ b/deobfuscator/src/main/java/net/runelite/deob/deobfuscators/constparam/ConstantParameter.java @@ -37,6 +37,7 @@ import net.runelite.asm.Method; import net.runelite.asm.attributes.Annotations; import net.runelite.asm.attributes.annotation.Annotation; import net.runelite.asm.attributes.annotation.Element; +import net.runelite.asm.attributes.annotation.SimpleElement; import net.runelite.asm.attributes.code.Instruction; import net.runelite.asm.attributes.code.InstructionType; import net.runelite.asm.attributes.code.Instructions; @@ -113,7 +114,6 @@ public class ConstantParameter implements Deobfuscator List pops = invokeCtx.getPops(); - outer: // object is popped first, then param 1, 2, 3, etc. double and long take two slots. for (int lvtOffset = offset, parameterIndex = 0; parameterIndex < method.getDescriptor().size(); @@ -451,9 +451,7 @@ public class ConstantParameter implements Deobfuscator } // Add garbage value - Element element = new Element(obfuscatedSignature); - element.setName("garbageValue"); - element.setValue(value.toString()); + Element element = new SimpleElement("garbageValue", value.toString()); obfuscatedSignature.addElement(element); } } @@ -464,12 +462,12 @@ public class ConstantParameter implements Deobfuscator public void run(ClassGroup group) { Execution execution = new Execution(group); - execution.addExecutionVisitor(i -> findParameters(i)); + execution.addExecutionVisitor(this::findParameters); execution.populateInitialMethods(); execution.run(); execution = new Execution(group); - execution.addMethodContextVisitor(mc -> findDeadParameters(mc)); + execution.addMethodContextVisitor(this::findDeadParameters); execution.populateInitialMethods(); execution.run(); diff --git a/deobfuscator/src/main/java/net/runelite/deob/deobfuscators/mapping/AnnotationMapper.java b/deobfuscator/src/main/java/net/runelite/deob/deobfuscators/mapping/AnnotationMapper.java index 87e01ac4ae..bd60ec165e 100644 --- a/deobfuscator/src/main/java/net/runelite/deob/deobfuscators/mapping/AnnotationMapper.java +++ b/deobfuscator/src/main/java/net/runelite/deob/deobfuscators/mapping/AnnotationMapper.java @@ -32,6 +32,7 @@ import net.runelite.asm.Method; import net.runelite.asm.attributes.Annotations; import net.runelite.asm.attributes.annotation.Annotation; import net.runelite.asm.attributes.annotation.Element; +import net.runelite.asm.attributes.annotation.SimpleElement; import net.runelite.deob.DeobAnnotations; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -39,7 +40,7 @@ import org.slf4j.LoggerFactory; public class AnnotationMapper { private static final Logger logger = LoggerFactory.getLogger(AnnotationMapper.class); - + private final ClassGroup source, target; private final ParallelExecutorMapping mapping; @@ -119,20 +120,17 @@ public class AnnotationMapper if (from.getAnnotations() == null) return count; - + for (Annotation a : from.getAnnotations()) { if (isCopyable(a)) { - Annotation annotation = new Annotation(to); - annotation.setType(a.getType()); + Annotation annotation = new Annotation(a.getType()); to.addAnnotation(annotation); for (Element e : a.getElements()) { - Element element = new Element(annotation); - element.setName(e.getName()); - element.setValue(e.getValue()); + Element element = new SimpleElement(e.getName(), e.getValue()); annotation.addElement(element); } @@ -155,7 +153,6 @@ public class AnnotationMapper private boolean isCopyable(Annotation a) { return a.getType().equals(DeobAnnotations.EXPORT) - || a.getType().equals(DeobAnnotations.IMPLEMENTS) - || a.getType().equals(DeobAnnotations.HOOK); + || a.getType().equals(DeobAnnotations.IMPLEMENTS); } } diff --git a/deobfuscator/src/main/java/net/runelite/deob/updater/AnnotationAdder.java b/deobfuscator/src/main/java/net/runelite/deob/updater/AnnotationAdder.java index a01628d863..d2c3d62034 100644 --- a/deobfuscator/src/main/java/net/runelite/deob/updater/AnnotationAdder.java +++ b/deobfuscator/src/main/java/net/runelite/deob/updater/AnnotationAdder.java @@ -8,6 +8,7 @@ import net.runelite.asm.Method; import net.runelite.asm.attributes.Annotations; import net.runelite.asm.attributes.annotation.Annotation; import net.runelite.asm.attributes.annotation.Element; +import net.runelite.asm.attributes.annotation.SimpleElement; import net.runelite.deob.Deob; import net.runelite.deob.DeobAnnotations; import org.slf4j.Logger; @@ -23,6 +24,7 @@ public class AnnotationAdder private final ClassGroup group; private final Logger log = LoggerFactory.getLogger(AnnotationAdder.class); + @SuppressWarnings("unchecked") public void run() { int impl = 0; @@ -50,12 +52,9 @@ public class AnnotationAdder { Annotations an = c.getAnnotations(); - Annotation implAn = new Annotation(an); - implAn.setType(DeobAnnotations.IMPLEMENTS); + Annotation implAn = new Annotation(DeobAnnotations.IMPLEMENTS); - Element value = new Element(implAn); - value.setValue(c.getClassName()); - value.setName("value"); + Element value = new SimpleElement(c.getClassName()); implAn.addElement(value); an.addAnnotation(implAn); @@ -81,12 +80,9 @@ public class AnnotationAdder Annotation a = an.find(DeobAnnotations.EXPORT); if (a == null) { - a = new Annotation(an); - a.setType(DeobAnnotations.EXPORT); + a = new Annotation(DeobAnnotations.EXPORT); - Element value = new Element(a); - value.setValue(fieldName); - value.setName("value"); + Element value = new SimpleElement(fieldName); a.addElement(value); an.addAnnotation(a); @@ -114,12 +110,9 @@ public class AnnotationAdder Annotation a = an.find(DeobAnnotations.EXPORT); if (a == null) { - a = new Annotation(an); - a.setType(DeobAnnotations.EXPORT); + a = new Annotation(DeobAnnotations.EXPORT); - Element value = new Element(a); - value.setValue(methodName); - value.setName("value"); + Element value = new SimpleElement(methodName); a.addElement(value); an.addAnnotation(a); diff --git a/deobfuscator/src/main/java/net/runelite/deob/updater/AnnotationCopier.java b/deobfuscator/src/main/java/net/runelite/deob/updater/AnnotationCopier.java index 0c594cfe19..5d495b9029 100644 --- a/deobfuscator/src/main/java/net/runelite/deob/updater/AnnotationCopier.java +++ b/deobfuscator/src/main/java/net/runelite/deob/updater/AnnotationCopier.java @@ -34,6 +34,7 @@ import net.runelite.asm.Type; import net.runelite.asm.attributes.Annotations; import net.runelite.asm.attributes.annotation.Annotation; import net.runelite.asm.attributes.annotation.Element; +import net.runelite.asm.attributes.annotation.SimpleElement; public class AnnotationCopier { @@ -97,15 +98,12 @@ public class AnnotationCopier { if (!isType(a.getType())) continue; - - Annotation a2 = new Annotation(an2); - a2.setType(a.getType()); + + Annotation a2 = new Annotation(a.getType()); for (Element element : a.getElements()) { - Element element2 = new Element(a2); - element2.setName(element.getName()); - element2.setValue(element.getValue()); + Element element2 = new SimpleElement(element.getName(), element.getValue()); a2.addElement(element2); } diff --git a/deobfuscator/src/main/java/net/runelite/deob/updater/AnnotationRenamer.java b/deobfuscator/src/main/java/net/runelite/deob/updater/AnnotationRenamer.java index 7d0f2f03d2..da920899bc 100644 --- a/deobfuscator/src/main/java/net/runelite/deob/updater/AnnotationRenamer.java +++ b/deobfuscator/src/main/java/net/runelite/deob/updater/AnnotationRenamer.java @@ -34,13 +34,9 @@ import net.runelite.asm.attributes.annotation.Annotation; import net.runelite.deob.DeobAnnotations; import net.runelite.deob.deobfuscators.Renamer; import net.runelite.deob.util.NameMappings; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; public class AnnotationRenamer { - private static final Logger logger = LoggerFactory.getLogger(AnnotationRenamer.class); - private ClassGroup group; public AnnotationRenamer(ClassGroup group) diff --git a/deobfuscator/src/main/java/net/runelite/deob/util/JarUtil.java b/deobfuscator/src/main/java/net/runelite/deob/util/JarUtil.java index 1b0435d3d0..67364468c0 100644 --- a/deobfuscator/src/main/java/net/runelite/deob/util/JarUtil.java +++ b/deobfuscator/src/main/java/net/runelite/deob/util/JarUtil.java @@ -25,9 +25,11 @@ package net.runelite.deob.util; import java.io.File; +import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; +import java.util.Collection; import java.util.Enumeration; import java.util.jar.JarEntry; import java.util.jar.JarFile; @@ -79,6 +81,41 @@ public class JarUtil return group; } + public static ClassFile loadClass(byte[] bytes) + { + ClassReader reader = new ClassReader(bytes); + ClassFileVisitor cv = new ClassFileVisitor(); + reader.accept(cv, ClassReader.SKIP_FRAMES); + return cv.getClassFile(); + } + + public static ClassGroup loadClasses(Collection files) throws IOException + { + final ClassGroup group = new ClassGroup(); + + for (File file : files) + { + if (!file.getName().endsWith(".class")) + { + continue; + } + + try (InputStream is = new FileInputStream(file)) + { + ClassReader reader = new ClassReader(is); + ClassFileVisitor cv = new ClassFileVisitor(); + + reader.accept(cv, ClassReader.SKIP_FRAMES); + + group.addClass(cv.getClassFile()); + } + } + + group.initialize(); + + return group; + } + public static void saveJar(ClassGroup group, File jarfile) throws IOException { try (JarOutputStream jout = new JarOutputStream(new FileOutputStream(jarfile), new Manifest())) diff --git a/deobfuscator/src/test/java/net/runelite/osb/HookImporter.java b/deobfuscator/src/test/java/net/runelite/osb/HookImporter.java index bf8e2bed2b..dbb17bfe55 100644 --- a/deobfuscator/src/test/java/net/runelite/osb/HookImporter.java +++ b/deobfuscator/src/test/java/net/runelite/osb/HookImporter.java @@ -276,8 +276,7 @@ public class HookImporter { for (Element e : a.getElements()) { - String str = (String) e.getValue(); - return str; + return (String) e.getValue(); } } } @@ -288,7 +287,7 @@ public class HookImporter private Signature getObfuscatedMethodSignature(Method method) { String sig = getAnnotation(method.getAnnotations(), OBFUSCATED_SIGNATURE); - if (sig.isEmpty() == false) + if (!sig.isEmpty()) { return toObSignature(new Signature(sig)); // if it is annoted, use that }