From a88175ffc95792b88a8724f66db6dda2b8cc32ee Mon Sep 17 00:00:00 2001 From: gegy1000 Date: Tue, 17 Jul 2018 19:14:08 +0200 Subject: ASM Based Class Translator (#1) * Initial port to ASM * Package updates * Annotation + inner class translation * Fix inner class mapping * More bytecode translation * Signature refactoring * Fix highlighting of mapped names * Fix parameter name offset * Fix anonymous class generation * Fix issues with inner class signature transformation * Fix bridged method detection * Fix compile issues * Resolve all failed tests * Apply deobfuscated name to transformed classes * Fix class signatures not being translated * Fix frame array type translation * Fix frame array type translation * Fix array translation in method calls * Fix method reference and bridge detection * Fix handling of null deobf mappings * Parameter translation in interfaces * Fix enum parameter index offset * Fix parsed local variable indexing * Fix stackoverflow on rebuilding method names * Ignore invalid decompiled variable indices * basic source jar * Output directly to file on source export * Make decompile parallel * fix incorrect super calls * Use previous save state to delete old mapping files * Fix old mappings not properly being removed * Fix old mappings not properly being removed * make isMethodProvider public (cherry picked from commit ebad6a9) * speed up Deobfuscator's getSources by using a single TranslatingTypeloader and caching the ClassLoaderTypeloader * ignore .idea project folders * move SynchronizedTypeLoader to a non-inner * fix signature remap of inners for now * index & resolve method/field references for usages view * Allow reader/writer subclasses to provide the underlying file operations * fix giving obf classes a name not removing them from the panel * buffer the ParsedJar class entry inputstream, allow use with a jarinputstream * make CachingClasspathTypeLoader public * make CachingClasspathTypeLoader public * support enum switches with obfuscated SwitchMaps --- .../java/cuchaz/enigma/mapping/MethodMapping.java | 139 ++++++++++----------- 1 file changed, 68 insertions(+), 71 deletions(-) (limited to 'src/main/java/cuchaz/enigma/mapping/MethodMapping.java') diff --git a/src/main/java/cuchaz/enigma/mapping/MethodMapping.java b/src/main/java/cuchaz/enigma/mapping/MethodMapping.java index 1524ce6..2f10144 100644 --- a/src/main/java/cuchaz/enigma/mapping/MethodMapping.java +++ b/src/main/java/cuchaz/enigma/mapping/MethodMapping.java @@ -11,50 +11,49 @@ package cuchaz.enigma.mapping; +import com.google.common.base.Preconditions; import com.google.common.collect.Maps; +import cuchaz.enigma.mapping.entry.ClassEntry; +import cuchaz.enigma.mapping.entry.MethodEntry; import cuchaz.enigma.throwables.IllegalNameException; import cuchaz.enigma.throwables.MappingConflict; import java.util.Map; -public class MethodMapping implements Comparable, MemberMapping { +public class MethodMapping implements Comparable, MemberMapping { private String obfName; private String deobfName; - private Signature obfSignature; - private Map arguments; + private MethodDescriptor obfDescriptor; + private Map localVariables; private Mappings.EntryModifier modifier; - public MethodMapping(String obfName, Signature obfSignature) { - this(obfName, obfSignature, null, Mappings.EntryModifier.UNCHANGED); + public MethodMapping(String obfName, MethodDescriptor obfDescriptor) { + this(obfName, obfDescriptor, null, Mappings.EntryModifier.UNCHANGED); } - public MethodMapping(String obfName, Signature obfSignature, String deobfName) { - this(obfName, obfSignature, deobfName, Mappings.EntryModifier.UNCHANGED); + public MethodMapping(String obfName, MethodDescriptor obfDescriptor, String deobfName) { + this(obfName, obfDescriptor, deobfName, Mappings.EntryModifier.UNCHANGED); } - public MethodMapping(String obfName, Signature obfSignature, String deobfName, Mappings.EntryModifier modifier) { - if (obfName == null) { - throw new IllegalArgumentException("obf name cannot be null!"); - } - if (obfSignature == null) { - throw new IllegalArgumentException("obf signature cannot be null!"); - } + public MethodMapping(String obfName, MethodDescriptor obfDescriptor, String deobfName, Mappings.EntryModifier modifier) { + Preconditions.checkNotNull(obfName, "Method obf name cannot be null"); + Preconditions.checkNotNull(obfDescriptor, "Method obf desc cannot be null"); this.obfName = obfName; this.deobfName = NameValidator.validateMethodName(deobfName); - this.obfSignature = obfSignature; - this.arguments = Maps.newTreeMap(); + this.obfDescriptor = obfDescriptor; + this.localVariables = Maps.newTreeMap(); this.modifier = modifier; } - public MethodMapping(MethodMapping other, ClassNameReplacer obfClassNameReplacer) { + public MethodMapping(MethodMapping other, Translator translator) { this.obfName = other.obfName; this.deobfName = other.deobfName; this.modifier = other.modifier; - this.obfSignature = new Signature(other.obfSignature, obfClassNameReplacer); - this.arguments = Maps.newTreeMap(); - for (Map.Entry entry : other.arguments.entrySet()) { - this.arguments.put(entry.getKey(), new ArgumentMapping(entry.getValue())); + this.obfDescriptor = translator.getTranslatedMethodDesc(other.obfDescriptor); + this.localVariables = Maps.newTreeMap(); + for (Map.Entry entry : other.localVariables.entrySet()) { + this.localVariables.put(entry.getKey(), new LocalVariableMapping(entry.getValue())); } } @@ -77,6 +76,9 @@ public class MethodMapping implements Comparable, MemberMapping, MemberMapping arguments() { - return this.arguments.values(); + public Iterable arguments() { + return this.localVariables.values(); } - public void addArgumentMapping(ArgumentMapping argumentMapping) throws MappingConflict { - if (this.arguments.containsKey(argumentMapping.getIndex())) { - throw new MappingConflict("argument", argumentMapping.getName(), this.arguments.get(argumentMapping.getIndex()).getName()); + public void addArgumentMapping(LocalVariableMapping localVariableMapping) throws MappingConflict { + if (this.localVariables.containsKey(localVariableMapping.getIndex())) { + throw new MappingConflict("argument", localVariableMapping.getName(), this.localVariables.get(localVariableMapping.getIndex()).getName()); } - this.arguments.put(argumentMapping.getIndex(), argumentMapping); + this.localVariables.put(localVariableMapping.getIndex(), localVariableMapping); } - public String getObfArgumentName(int index) { - ArgumentMapping argumentMapping = this.arguments.get(index); - if (argumentMapping != null) { - return argumentMapping.getName(); + public String getObfLocalVariableName(int index) { + LocalVariableMapping localVariableMapping = this.localVariables.get(index); + if (localVariableMapping != null) { + return localVariableMapping.getName(); } return null; } - public String getDeobfArgumentName(int index) { - ArgumentMapping argumentMapping = this.arguments.get(index); - if (argumentMapping != null) { - return argumentMapping.getName(); + public String getDeobfLocalVariableName(int index) { + LocalVariableMapping localVariableMapping = this.localVariables.get(index); + if (localVariableMapping != null) { + return localVariableMapping.getName(); } return null; } - public void setArgumentName(int index, String name) { - ArgumentMapping argumentMapping = this.arguments.get(index); - if (argumentMapping == null) { - argumentMapping = new ArgumentMapping(index, name); - boolean wasAdded = this.arguments.put(index, argumentMapping) == null; + public void setLocalVariableName(int index, String name) { + LocalVariableMapping localVariableMapping = this.localVariables.get(index); + if (localVariableMapping == null) { + localVariableMapping = new LocalVariableMapping(index, name); + boolean wasAdded = this.localVariables.put(index, localVariableMapping) == null; assert (wasAdded); } else { - argumentMapping.setName(name); + localVariableMapping.setName(name); } } - public void removeArgumentName(int index) { - boolean wasRemoved = this.arguments.remove(index) != null; + public void removeLocalVariableName(int index) { + boolean wasRemoved = this.localVariables.remove(index) != null; assert (wasRemoved); } @@ -146,14 +148,14 @@ public class MethodMapping implements Comparable, MemberMapping "); - buf.append(argumentMapping.getName()); + buf.append(localVariableMapping.getName()); buf.append("\n"); } return buf.toString(); @@ -161,12 +163,12 @@ public class MethodMapping implements Comparable, MemberMapping, MemberMapping - { + MethodDescriptor newDescriptor = obfDescriptor.remap(className -> { if (className.equals(oldObfClassName)) { return newObfClassName; } - return null; + return className; }); - if (!newSignature.equals(this.obfSignature)) { - this.obfSignature = newSignature; + if (!newDescriptor.equals(this.obfDescriptor)) { + this.obfDescriptor = newDescriptor; return true; } return false; } - public boolean isConstructor() { - return this.obfName.startsWith("<"); - } - @Override - public BehaviorEntry getObfEntry(ClassEntry classEntry) { - if (isConstructor()) { - return new ConstructorEntry(classEntry, this.obfSignature); - } else { - return new MethodEntry(classEntry, this.obfName, this.obfSignature); - } + public MethodEntry getObfEntry(ClassEntry classEntry) { + return new MethodEntry(classEntry, this.obfName, this.obfDescriptor); } public Mappings.EntryModifier getModifier() { @@ -210,4 +203,8 @@ public class MethodMapping implements Comparable, MemberMapping