forked from java-deobfuscator/deobfuscator
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
37 changed files
with
2,809 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
23 changes: 23 additions & 0 deletions
23
src/main/java/com/javadeobfuscator/deobfuscator/exceptions/WrongTransformerException.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
/* | ||
* Copyright 2017 Sam Sun <[email protected]> | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package com.javadeobfuscator.deobfuscator.exceptions; | ||
|
||
public class WrongTransformerException extends RuntimeException { | ||
public WrongTransformerException(String message) { | ||
super(message); | ||
} | ||
} |
37 changes: 37 additions & 0 deletions
37
src/main/java/com/javadeobfuscator/deobfuscator/graph/GraphHelper.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
/* | ||
* Copyright 2017 Sam Sun <[email protected]> | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package com.javadeobfuscator.deobfuscator.graph; | ||
|
||
import org.objectweb.asm.tree.ClassNode; | ||
import org.objectweb.asm.tree.MethodNode; | ||
|
||
import java.util.Collection; | ||
import java.util.HashMap; | ||
import java.util.Map; | ||
import java.util.Objects; | ||
|
||
public class GraphHelper { | ||
public static Map<String, ClassNode> validateUniqueClasses(Collection<ClassNode> nodes) { | ||
Map<String, ClassNode> map = new HashMap<>(); | ||
for (ClassNode classNode : nodes) { | ||
if (map.put(classNode.name, classNode) != null) { | ||
throw new IllegalArgumentException("Duplicate class " + classNode.name); | ||
} | ||
} | ||
return map; | ||
} | ||
} |
92 changes: 92 additions & 0 deletions
92
src/main/java/com/javadeobfuscator/deobfuscator/graph/callgraph/CallGraph.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
/* | ||
* Copyright 2017 Sam Sun <[email protected]> | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package com.javadeobfuscator.deobfuscator.graph.callgraph; | ||
|
||
import com.google.common.collect.ImmutableMap; | ||
import com.google.common.collect.ImmutableSet; | ||
import org.objectweb.asm.tree.ClassNode; | ||
import org.objectweb.asm.tree.MethodNode; | ||
|
||
import java.util.Collection; | ||
import java.util.Map; | ||
import java.util.Objects; | ||
import java.util.Set; | ||
|
||
public class CallGraph { | ||
private final Map<Key, CallGraphNode> map; | ||
private final Set<CallGraphNode> entryPoints; | ||
|
||
CallGraph(Map<Key, CallGraphNode> callGraph, Set<CallGraphNode> entryPoints) { | ||
this.map = ImmutableMap.copyOf(callGraph); | ||
this.entryPoints = ImmutableSet.copyOf(entryPoints); | ||
} | ||
|
||
public final CallGraphNode get(ClassNode owner, MethodNode method) { | ||
return map.get(getKey(owner, method)); | ||
} | ||
|
||
public final CallGraphNode get(String owner, String name, String desc) { | ||
return map.get(getKey(owner, name, desc)); | ||
} | ||
|
||
public final Collection<CallGraphNode> values() { | ||
return map.values(); | ||
} | ||
|
||
/** | ||
* An entry point being any node with no incoming edges, not necessarily a Java entry point | ||
*/ | ||
public final Set<CallGraphNode> getEntryPoints() { | ||
return entryPoints; | ||
} | ||
|
||
static Key getKey(ClassNode classNode, MethodNode methodNode) { | ||
return new Key(classNode.name, methodNode.name, methodNode.desc); | ||
} | ||
|
||
static Key getKey(String owner, String name, String desc) { | ||
return new Key(owner, name, desc); | ||
} | ||
|
||
|
||
static class Key { | ||
final String owner; | ||
final String name; | ||
final String desc; | ||
|
||
public Key(String owner, String name, String desc) { | ||
this.owner = owner; | ||
this.name = name; | ||
this.desc = desc; | ||
} | ||
|
||
@Override | ||
public boolean equals(Object o) { | ||
if (this == o) return true; | ||
if (o == null || getClass() != o.getClass()) return false; | ||
Key key = (Key) o; | ||
return Objects.equals(owner, key.owner) && | ||
Objects.equals(name, key.name) && | ||
Objects.equals(desc, key.desc); | ||
} | ||
|
||
@Override | ||
public int hashCode() { | ||
return Objects.hash(owner, name, desc); | ||
} | ||
} | ||
} |
141 changes: 141 additions & 0 deletions
141
src/main/java/com/javadeobfuscator/deobfuscator/graph/callgraph/CallGraphBuilder.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,141 @@ | ||
/* | ||
* Copyright 2017 Sam Sun <[email protected]> | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package com.javadeobfuscator.deobfuscator.graph.callgraph; | ||
|
||
import com.javadeobfuscator.deobfuscator.graph.inheritancegraph.InheritanceGraph; | ||
import com.javadeobfuscator.deobfuscator.graph.inheritancegraph.InheritanceGraphNode; | ||
import com.javadeobfuscator.deobfuscator.utils.TransformerHelper; | ||
import org.objectweb.asm.tree.AbstractInsnNode; | ||
import org.objectweb.asm.tree.ClassNode; | ||
import org.objectweb.asm.tree.MethodInsnNode; | ||
import org.objectweb.asm.tree.MethodNode; | ||
|
||
import java.util.*; | ||
|
||
import static com.javadeobfuscator.deobfuscator.graph.GraphHelper.validateUniqueClasses; | ||
|
||
public class CallGraphBuilder { | ||
private List<ClassNode> nodes = new ArrayList<>(); | ||
private InheritanceGraph inheritanceGraph; | ||
|
||
private CallGraphBuilder() { | ||
} | ||
|
||
public static CallGraphBuilder newBuilder() { | ||
return new CallGraphBuilder(); | ||
} | ||
|
||
public final CallGraphBuilder withNode(ClassNode node) { | ||
nodes.add(node); | ||
return this; | ||
} | ||
|
||
public final CallGraphBuilder withNodes(Collection<ClassNode> nodes) { | ||
this.nodes.addAll(nodes); | ||
return this; | ||
} | ||
|
||
public final CallGraphBuilder withInheritanceGraph(InheritanceGraph inheritanceGraph) { | ||
this.inheritanceGraph = inheritanceGraph; | ||
return this; | ||
} | ||
|
||
public final CallGraph build() { | ||
Map<String, ClassNode> classMap = validateUniqueClasses(nodes); | ||
|
||
Map<CallGraph.Key, CallGraphNode> callGraph = new HashMap<>(); | ||
|
||
// First, compute the call graph | ||
for (ClassNode classNode : nodes) { | ||
for (MethodNode methodNode : classNode.methods) { | ||
CallGraphNode thisNode = callGraph.computeIfAbsent(CallGraph.getKey(classNode, methodNode), CallGraphNode::new); | ||
|
||
if (methodNode.instructions == null) { | ||
continue; | ||
} | ||
|
||
for (ListIterator<AbstractInsnNode> it = methodNode.instructions.iterator(); it.hasNext(); ) { | ||
AbstractInsnNode insn = it.next(); | ||
|
||
if (insn instanceof MethodInsnNode) { | ||
MethodInsnNode methodInsnNode = (MethodInsnNode) insn; | ||
CallGraph.Key otherKey = CallGraph.getKey(methodInsnNode.owner, methodInsnNode.name, methodInsnNode.desc); | ||
CallGraphNode otherNode = callGraph.computeIfAbsent(otherKey, CallGraphNode::new); | ||
|
||
thisNode.addXrefTo(otherNode); | ||
otherNode.addXrefFrom(thisNode); | ||
|
||
// Handle inheritance | ||
if (inheritanceGraph != null) { | ||
InheritanceGraphNode inheritanceGraphNode = inheritanceGraph.get(methodInsnNode.owner); | ||
if (inheritanceGraphNode != null) { | ||
for (InheritanceGraphNode child : inheritanceGraphNode.getChildren()) { | ||
ClassNode childNode = classMap.get(child.getOwner()); | ||
if (childNode == null) { | ||
continue; | ||
} | ||
MethodNode method = TransformerHelper.findMethodNode(childNode, methodInsnNode.name, methodInsnNode.desc); | ||
if (method == null) { | ||
continue; | ||
} | ||
otherKey = CallGraph.getKey(child.getOwner(), methodInsnNode.name, methodInsnNode.desc); | ||
otherNode = callGraph.computeIfAbsent(otherKey, CallGraphNode::new); | ||
|
||
thisNode.addXrefTo(otherNode); | ||
otherNode.addXrefFrom(thisNode); | ||
} | ||
|
||
for (InheritanceGraphNode parent : inheritanceGraphNode.getParents()) { | ||
ClassNode parentNode = classMap.get(parent.getOwner()); | ||
if (parentNode == null) { | ||
continue; | ||
} | ||
MethodNode method = TransformerHelper.findMethodNode(parentNode, methodInsnNode.name, methodInsnNode.desc); | ||
if (method == null) { | ||
continue; | ||
} | ||
otherKey = CallGraph.getKey(parent.getOwner(), methodInsnNode.name, methodInsnNode.desc); | ||
otherNode = callGraph.computeIfAbsent(otherKey, CallGraphNode::new); | ||
|
||
thisNode.addXrefTo(otherNode); | ||
otherNode.addXrefFrom(thisNode); | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} | ||
|
||
// Freeze it! | ||
callGraph.values().forEach(CallGraphNode::freeze); | ||
|
||
// Next, compute the entry points | ||
Set<CallGraphNode> entryPoints = new HashSet<>(); | ||
for (CallGraphNode node : callGraph.values()) { | ||
if (!classMap.containsKey(node.getOwner())) { | ||
continue; | ||
} | ||
|
||
if (node.getXrefsFrom().isEmpty()) { | ||
entryPoints.add(node); | ||
} | ||
} | ||
|
||
return new CallGraph(callGraph, entryPoints); | ||
} | ||
} |
Oops, something went wrong.