Skip to content

Commit 2dcfabf

Browse files
committed
dont remap if method overrides library method, include java runtime in vineflower
1 parent 975a781 commit 2dcfabf

File tree

19 files changed

+129
-59
lines changed

19 files changed

+129
-59
lines changed

deobfuscator-api/src/main/java/uwu/narumi/deobfuscator/api/asm/FieldRef.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,4 +17,9 @@ public static FieldRef of(ClassNode classNode, FieldNode fieldNode) {
1717
public static FieldRef of(FieldInsnNode fieldInsn) {
1818
return new FieldRef(fieldInsn.owner, fieldInsn.name, fieldInsn.desc);
1919
}
20+
21+
@Override
22+
public String toString() {
23+
return owner + "." + name + desc;
24+
}
2025
}

deobfuscator-api/src/main/java/uwu/narumi/deobfuscator/api/asm/MethodRef.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,4 +17,9 @@ public static MethodRef of(ClassNode classNode, MethodNode methodNode) {
1717
public static MethodRef of(MethodInsnNode methodInsn) {
1818
return new MethodRef(methodInsn.owner, methodInsn.name, methodInsn.desc);
1919
}
20+
21+
@Override
22+
public String toString() {
23+
return owner + "." + name + desc;
24+
}
2025
}

deobfuscator-api/src/main/java/uwu/narumi/deobfuscator/api/inheritance/InheritanceGraph.java

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import uwu.narumi.deobfuscator.api.classpath.ClassProvider;
1010
import uwu.narumi.deobfuscator.api.classpath.CombinedClassProvider;
1111
import uwu.narumi.deobfuscator.api.classpath.JvmClassProvider;
12+
import uwu.narumi.deobfuscator.api.context.Context;
1213

1314
import java.util.*;
1415
import java.util.concurrent.ConcurrentHashMap;
@@ -33,12 +34,14 @@ public class InheritanceGraph {
3334
private final Set<String> stubs = ConcurrentHashMap.newKeySet();
3435
private final Function<String, InheritanceVertex> vertexProvider = createVertexProvider();
3536
private final ClassProvider classProvider;
37+
private final ClassProvider librariesClassProvider;
3638

3739
/**
3840
* Create an inheritance graph.
3941
*/
40-
public InheritanceGraph(@NotNull ClassProvider classProvider) {
41-
this.classProvider = new CombinedClassProvider(classProvider, JvmClassProvider.INSTANCE);
42+
public InheritanceGraph(@NotNull Context context) {
43+
this.librariesClassProvider = new CombinedClassProvider(context.getLibraries(), JvmClassProvider.INSTANCE);
44+
this.classProvider = new CombinedClassProvider(context, this.librariesClassProvider);
4245

4346
// Populate downwards (parent --> child) lookup
4447
refreshChildLookup();
@@ -310,13 +313,13 @@ private Function<String, InheritanceVertex> createVertexProvider() {
310313
//ResourcePathNode resourcePath = result.getPathOfType(WorkspaceResource.class);
311314
//boolean isPrimary = resourcePath != null && resourcePath.isPrimary();
312315
//ClassInfo info = result.getValue();
313-
return new InheritanceVertex(result, this::getVertex, this::getDirectChildren);
316+
return new InheritanceVertex(result, this::getVertex, this::getDirectChildren, this.librariesClassProvider.getClass(name) == null);
314317
};
315318
}
316319

317320
private static class InheritanceStubVertex extends InheritanceVertex {
318321
private InheritanceStubVertex() {
319-
super(new ClassNode(), in -> null, in -> null);
322+
super(new ClassNode(), in -> null, in -> null, false);
320323
}
321324

322325
@Override

deobfuscator-api/src/main/java/uwu/narumi/deobfuscator/api/inheritance/InheritanceVertex.java

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
public class InheritanceVertex {
2121
private final Function<String, InheritanceVertex> lookup;
2222
private final Function<String, Collection<String>> childrenLookup;
23-
//private final boolean isPrimary;
23+
private final boolean isPrimary;
2424
private volatile Set<InheritanceVertex> parents;
2525
private volatile Set<InheritanceVertex> children;
2626
private ClassNode value;
@@ -35,11 +35,12 @@ public class InheritanceVertex {
3535
*/
3636
public InheritanceVertex(@NotNull ClassNode value,
3737
@NotNull Function<String, InheritanceVertex> lookup,
38-
@NotNull Function<String, Collection<String>> childrenLookup) {
38+
@NotNull Function<String, Collection<String>> childrenLookup,
39+
boolean isPrimary) {
3940
this.value = value;
4041
this.lookup = lookup;
4142
this.childrenLookup = childrenLookup;
42-
//this.isPrimary = isPrimary;
43+
this.isPrimary = isPrimary;
4344
}
4445

4546
/**
@@ -138,12 +139,10 @@ public boolean hasMethodInSelfOrChildren(@NotNull String name, @NotNull String d
138139

139140
/**
140141
* @return {@code true} if the class represented by this vertex is a library class.
141-
* This means a class that does not belong to the primary {@link WorkspaceResource}
142-
* of a {@link Workspace}.
143142
*/
144-
/*public boolean isLibraryVertex() {
143+
public boolean isLibraryVertex() {
145144
return !isPrimary;
146-
}*/
145+
}
147146

148147
/**
149148
* @return {@code true} when the current vertex represents {@link Object}.
@@ -177,7 +176,7 @@ public boolean isModule() {
177176
* @return {@code true} if method is an extension of an outside class's methods and thus should not be renamed.
178177
* {@code false} if the method is safe to rename.
179178
*/
180-
/*public boolean isLibraryMethod(@NotNull String name, @NotNull String desc) {
179+
public boolean isLibraryMethod(@NotNull String name, @NotNull String desc) {
181180
// Check against this definition
182181
if (!isPrimary && hasMethod(name, desc))
183182
return true;
@@ -190,7 +189,7 @@ public boolean isModule() {
190189

191190
// No library definition found, so its safe to rename.
192191
return false;
193-
}*/
192+
}
194193

195194
/**
196195
* @param vertex

deobfuscator-impl/src/main/java/uwu/narumi/deobfuscator/Deobfuscator.java

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -187,9 +187,7 @@ private void saveToJar() {
187187
* @param saver a consumer that accepts a path and data to save
188188
*/
189189
private void save(BiConsumer<String, byte[]> saver) {
190-
InheritanceGraph inheritanceGraph = new InheritanceGraph(
191-
new CombinedClassProvider(this.context, this.context.getLibraries())
192-
);
190+
InheritanceGraph inheritanceGraph = new InheritanceGraph(this.context);
193191

194192
// Save classes
195193
context.getClassesMap().forEach((ignored, classWrapper) -> {

deobfuscator-impl/src/test/java/uwu/narumi/deobfuscator/base/TestDeobfuscationBase.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,7 @@ private void assertOutput(@Nullable IContextSource contextSource, @Nullable Path
155155

156156
Decompiler.Builder decompilerBuilder = Decompiler.builder()
157157
.option(IFernflowerPreferences.INDENT_STRING, " ")
158+
.option(IFernflowerPreferences.INCLUDE_JAVA_RUNTIME, true)
158159
.output(assertingResultSaver); // Assert output
159160

160161
// Add sources

deobfuscator-transformers/src/main/java/uwu/narumi/deobfuscator/core/other/impl/universal/RemapperTransformer.java

Lines changed: 36 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,14 @@
66
import uwu.narumi.deobfuscator.api.asm.FieldRef;
77
import uwu.narumi.deobfuscator.api.asm.MethodRef;
88
import uwu.narumi.deobfuscator.api.asm.remapper.NamesRemapper;
9-
import uwu.narumi.deobfuscator.api.classpath.CombinedClassProvider;
109
import uwu.narumi.deobfuscator.api.context.DeobfuscatorOptions;
1110
import uwu.narumi.deobfuscator.api.inheritance.InheritanceGraph;
1211
import uwu.narumi.deobfuscator.api.inheritance.InheritanceVertex;
1312
import uwu.narumi.deobfuscator.api.transformer.Transformer;
1413

14+
import java.io.IOException;
15+
import java.nio.file.Files;
16+
import java.nio.file.Path;
1517
import java.util.ArrayList;
1618
import java.util.Comparator;
1719
import java.util.List;
@@ -20,7 +22,7 @@
2022
import java.util.function.Predicate;
2123

2224
/**
23-
* Remaps class, method and field names. Useful to remap scrambled names to something more readable.
25+
* Remaps class, method, and field names. Useful to remap scrambled names to something more readable.
2426
* <p>
2527
* WARNING: If class overrides a method from a library's class and the library is not loaded {@link DeobfuscatorOptions#libraries()}
2628
* then the method will be remapped and will no longer override the library method. You must load the library to prevent this.
@@ -44,9 +46,7 @@ public RemapperTransformer(Predicate<String> classPredicate, Predicate<String> m
4446
protected void transform() throws Exception {
4547
NamesRemapper remapper = new NamesRemapper();
4648

47-
InheritanceGraph inheritanceGraph = new InheritanceGraph(
48-
new CombinedClassProvider(context(), context().getLibraries())
49-
);
49+
InheritanceGraph inheritanceGraph = new InheritanceGraph(context());
5050

5151
AtomicInteger classCounter = new AtomicInteger(0);
5252
AtomicInteger methodCounter = new AtomicInteger(0);
@@ -63,8 +63,9 @@ protected void transform() throws Exception {
6363
remapper.classMappings.put(classWrapper.name(), "class_" + classCounter.getAndIncrement());
6464
}
6565

66+
InheritanceVertex vertex = inheritanceGraph.getVertex(classWrapper.name());
6667
// Parents and children combined
67-
Set<InheritanceVertex> directVertices = inheritanceGraph.getVertex(classWrapper.name()).getAllDirectVertices();
68+
Set<InheritanceVertex> directVertices = vertex.getAllDirectVertices();
6869

6970
// Methods
7071
classWrapper.methods().forEach(methodNode -> {
@@ -78,14 +79,19 @@ protected void transform() throws Exception {
7879
// Test
7980
if (!this.methodPredicate.test(methodNode.name)) return;
8081

82+
if (vertex.isLibraryMethod(methodNode.name, methodNode.desc)) {
83+
// It is a library method, don't remap
84+
return;
85+
}
86+
8187
String newName = "method_" + methodCounter.getAndIncrement();
8288

8389
// Map current method
8490
remapper.methodMappings.put(methodRef, newName);
8591

8692
// Map the same method in inheritance graph
87-
for (InheritanceVertex vertex : directVertices) {
88-
remapper.methodMappings.put(MethodRef.of(vertex.getValue(), methodNode), newName);
93+
for (InheritanceVertex directVertex : directVertices) {
94+
remapper.methodMappings.put(MethodRef.of(directVertex.getValue(), methodNode), newName);
8995
}
9096
});
9197

@@ -103,6 +109,8 @@ protected void transform() throws Exception {
103109
});
104110
});
105111

112+
//saveMappings(remapper);
113+
106114
// Remap
107115
new ArrayList<>(context().classes()).forEach(classWrapper -> {
108116
ClassNode newNode = new ClassNode();
@@ -122,4 +130,24 @@ protected void transform() throws Exception {
122130
markChange();
123131
});
124132
}
133+
134+
private void saveMappings(NamesRemapper remapper) throws IOException {
135+
StringBuilder mappings = new StringBuilder();
136+
mappings.append("Class mappings:\n");
137+
for (var entry : remapper.classMappings.entrySet()) {
138+
mappings.append(entry.getKey()).append(" -> ").append(entry.getValue()).append("\n");
139+
}
140+
mappings.append("\n");
141+
mappings.append("Method mappings\n");
142+
for (var entry : remapper.methodMappings.entrySet()) {
143+
mappings.append(entry.getKey()).append(" -> ").append(entry.getValue()).append("\n");
144+
}
145+
mappings.append("\n");
146+
mappings.append("Field mappings\n");
147+
for (var entry : remapper.fieldMappings.entrySet()) {
148+
mappings.append(entry.getKey()).append(" -> ").append(entry.getValue()).append("\n");
149+
}
150+
151+
Files.writeString(Path.of("mappings.txt"), mappings.toString());
152+
}
125153
}

testData/results/custom-classes/Pop2Sample.dec

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import javax.swing.JPanel;
99
import javax.swing.colorchooser.AbstractColorChooserPanel;
1010

1111
class Ld implements Runnable {
12+
@Override
1213
public void run() {
1314
AbstractColorChooserPanel[] var5;
1415
JColorChooser var7;
1 Byte
Binary file not shown.

testData/results/custom-classes/zkm/sample2/a/a/a/a/a4.dec

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ public class a4 {
4141
}
4242

4343
private static List b(Predicate var0) {
44-
return (List)a(new Object[0]).stream().filter(var0).collect(Collectors.toList());
44+
return a(new Object[0]).stream().filter(var0).collect(Collectors.toList());
4545
}
4646

4747
public static Set a() {
@@ -52,10 +52,14 @@ public class a4 {
5252
long var10000 = 31437394211371L ^ var0;
5353
return var2 == null
5454
? Collections.emptyList()
55-
: (List)e.computeIfAbsent(
55+
: e.computeIfAbsent(
5656
var2,
5757
var1 -> Collections.unmodifiableList(
58-
b(new Object[]{(Predicate)var1x -> var2.equals(var1x.getLanguage()) && !var1x.getCountry().isEmpty() && var1x.getVariant().isEmpty()})
58+
b(
59+
new Object[]{
60+
(Predicate<Locale>)var1x -> var2.equals(var1x.getLanguage()) && !var1x.getCountry().isEmpty() && var1x.getVariant().isEmpty()
61+
}
62+
)
5963
)
6064
);
6165
}
@@ -91,9 +95,11 @@ public class a4 {
9195
long var10000 = 31437394211371L ^ var1;
9296
return var0 == null
9397
? Collections.emptyList()
94-
: (List)d.computeIfAbsent(
98+
: d.computeIfAbsent(
9599
var0,
96-
var1x -> Collections.unmodifiableList(b(new Object[]{(Predicate)var1xx -> var0.equals(var1xx.getCountry()) && var1xx.getVariant().isEmpty()}))
100+
var1x -> Collections.unmodifiableList(
101+
b(new Object[]{(Predicate<Locale>)var1xx -> var0.equals(var1xx.getCountry()) && var1xx.getVariant().isEmpty()})
102+
)
97103
);
98104
}
99105

@@ -403,8 +409,8 @@ public class a4 {
403409
k = new Integer[6];
404410
c = (char)b<"f">(16481, 4871103691199342599L);
405411
a = (char)b<"f">(4103, 7748647110630619232L);
406-
d = new ConcurrentHashMap();
407-
e = new ConcurrentHashMap();
412+
d = new ConcurrentHashMap<>();
413+
e = new ConcurrentHashMap<>();
408414
return;
409415
}
410416
break;
@@ -522,7 +528,7 @@ public class a4 {
522528
long var5 = (Long)var3[1];
523529
String var7 = a(var4, var5);
524530
MethodHandle var8 = MethodHandles.constant(String.class, var7);
525-
var1.setTarget(MethodHandles.dropArguments(var8, 0, new Class[]{int.class, long.class}));
531+
var1.setTarget(MethodHandles.dropArguments(var8, 0, int.class, long.class));
526532
return var7;
527533
}
528534

@@ -532,7 +538,7 @@ public class a4 {
532538
try {
533539
var3.setTarget(
534540
MethodHandles.explicitCastArguments(
535-
MethodHandles.insertArguments("a".asCollector(Object[].class, var2.parameterCount()), 0, new Object[]{var0, var3, var1}), var2
541+
MethodHandles.insertArguments("a".asCollector(Object[].class, var2.parameterCount()), 0, var0, var3, var1), var2
536542
)
537543
);
538544
return var3;
@@ -596,7 +602,7 @@ public class a4 {
596602
long var5 = (Long)var3[1];
597603
int var7 = b(var4, var5);
598604
MethodHandle var8 = MethodHandles.constant(int.class, var7);
599-
var1.setTarget(MethodHandles.dropArguments(var8, 0, new Class[]{int.class, long.class}));
605+
var1.setTarget(MethodHandles.dropArguments(var8, 0, int.class, long.class));
600606
return var7;
601607
}
602608

@@ -606,7 +612,7 @@ public class a4 {
606612
try {
607613
var3.setTarget(
608614
MethodHandles.explicitCastArguments(
609-
MethodHandles.insertArguments("b".asCollector(Object[].class, var2.parameterCount()), 0, new Object[]{var0, var3, var1}), var2
615+
MethodHandles.insertArguments("b".asCollector(Object[].class, var2.parameterCount()), 0, var0, var3, var1), var2
610616
)
611617
);
612618
return var3;

0 commit comments

Comments
 (0)