Skip to content

Commit

Permalink
Merge pull request #3 from terminalsin/memes/production-ready
Browse files Browse the repository at this point in the history
ClassTree + Exception bug fix (Pass) + Some renaming 

Validated to be working on prod
  • Loading branch information
terminalsin authored Jan 8, 2022
2 parents 8071e66 + f43c1cc commit f871123
Show file tree
Hide file tree
Showing 42 changed files with 424 additions and 155 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
.idea/*
*.iml

# MacOS
*.DS_Store

# Maven output
*/target/*
Expand Down
8 changes: 4 additions & 4 deletions org.mapleir.app-services/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,22 @@
<parent>
<groupId>org.mapleir</groupId>
<artifactId>parent</artifactId>
<version>0.0.1-ALPHA</version>
<version>1.0.0-SNAPSHOT</version>
<relativePath>../org.mapleir.parent</relativePath>
</parent>
<groupId>org.mapleir</groupId>
<name>app-services</name>
<name>MapleIR-app-services</name>
<artifactId>app-services</artifactId>
<dependencies>
<dependency>
<groupId>org.mapleir</groupId>
<artifactId>modasm</artifactId>
<version>0.0.1-ALPHA</version>
<version>1.0.0-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>org.mapleir</groupId>
<artifactId>topdank-services</artifactId>
<version>0.0.1-ALPHA</version>
<version>1.0.0-SNAPSHOT</version>
</dependency>
</dependencies>
</project>
Original file line number Diff line number Diff line change
Expand Up @@ -71,4 +71,8 @@ public Iterable<ClassNode> iterate() {
public Iterator<ClassNode> iterator() {
return nodeMap.values().iterator();
}

public int size() {
return nodeMap.size();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -123,10 +123,16 @@ public Collection<ClassNode> getAllBranches(ClassNode cn) {
while (!queue.isEmpty()) {
ClassNode next = queue.remove();
if (results.add(next) && next != rootNode) {
queue.addAll(getAllParents(next));
queue.addAll(getAllChildren(next));
}
}
queue.add(cn);
while (!queue.isEmpty()) {
ClassNode next = queue.remove();
if (results.add(next) && next != rootNode) {
queue.addAll(getAllParents(next));
}
}
return results;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,20 +77,48 @@ public int dumpClass(JarOutputStream out, String name, ClassNode cn) throws IOEx
out.putNextEntry(entry);
ClassTree tree = source.getClassTree();

ClassWriter writer = new ClassWriter(ClassWriter.COMPUTE_FRAMES) {


for(MethodNode m : cn.getMethods()) {
if(m.node.instructions.size() > 10000) {
System.out.println("large method: " + m + " @" + m.node.instructions.size());
}
}

try {
try {
ClassWriter writer = this.buildClassWriter(tree, ClassWriter.COMPUTE_FRAMES);
cn.node.accept(writer); // must use custom writer which overrides getCommonSuperclass
out.write(writer.toByteArray());
} catch (Exception e) {
ClassWriter writer = this.buildClassWriter(tree, ClassWriter.COMPUTE_MAXS);
cn.node.accept(writer); // must use custom writer which overrides getCommonSuperclass
out.write(writer.toByteArray());
System.err.println("Failed to write " + cn.getName() + "! Writing with COMPUTE_MAXS, " +
"which may cause runtime abnormalities");
}
} catch (Exception e) {
System.err.println("Failed to write " + cn.getName() + "! Skipping class...");
}

return 1;
}

public ClassWriter buildClassWriter(ClassTree tree, int flags) {
return new ClassWriter(flags) {
// this method in ClassWriter uses the systemclassloader as
// a stream location to load the super class, however, most of
// the time the class is loaded/read and parsed by us so it
// isn't defined in the system classloader. in certain cases
// we may not even want it to be loaded/resolved and we can
// bypass this by implementing the hierarchy scanning algorithm
// with ClassNodes rather than Classes.
@Override
@Override
protected String getCommonSuperClass(String type1, String type2) {
ClassNode ccn = source.findClassNode(type1);
ClassNode dcn = source.findClassNode(type2);
if(ccn == null) {
ClassNode ccn = source.findClassNode(type1);
ClassNode dcn = source.findClassNode(type2);

if(ccn == null) {
// return "java/lang/Object";
ClassNode c;
try {
Expand All @@ -99,14 +127,14 @@ protected String getCommonSuperClass(String type1, String type2) {
return "java/lang/Object";
}
if(c == null) {
return "java/lang/Object";
}
throw new UnsupportedOperationException(c.toString());
// classTree.build(c);
// return getCommonSuperClass(type1, type2);
}
if(dcn == null) {
return "java/lang/Object";
}
throw new UnsupportedOperationException(c.toString());
// classTree.build(c);
// return getCommonSuperClass(type1, type2);
}

if(dcn == null) {

// return "java/lang/Object";
ClassNode c;
Expand All @@ -116,52 +144,37 @@ protected String getCommonSuperClass(String type1, String type2) {
return "java/lang/Object";
}
if(c == null) {
return "java/lang/Object";
}
throw new UnsupportedOperationException(c.toString());
// classTree.build(c);
// return getCommonSuperClass(type1, type2);
}

Collection<ClassNode> c = tree.getAllParents(ccn);
Collection<ClassNode> d = tree.getAllParents(dcn);

if(c.contains(dcn))
return type1;

if(d.contains(ccn))
return type2;

if(Modifier.isInterface(ccn.node.access) || Modifier.isInterface(dcn.node.access)) {
// enums as well?
return "java/lang/Object";
} else {
do {
ClassNode nccn = source.findClassNode(ccn.node.superName);
if(nccn == null)
break;
ccn = nccn;
c = tree.getAllParents(ccn);
} while(!c.contains(dcn));
return ccn.getName();
}
}
};

for(MethodNode m : cn.getMethods()) {
if(m.node.instructions.size() > 10000) {
System.out.println("large method: " + m + " @" + m.node.instructions.size());
}
}
return "java/lang/Object";
}
throw new UnsupportedOperationException(c.toString());
// classTree.build(c);
// return getCommonSuperClass(type1, type2);
}

try {
cn.node.accept(writer); // must use custom writer which overrides getCommonSuperclass
out.write(writer.toByteArray());
} catch (Exception e) {
System.err.println("Failed to write " + cn.getName());
throw e;
}
return 1;
Collection<ClassNode> c = tree.getAllParents(ccn);
Collection<ClassNode> d = tree.getAllParents(dcn);

if(c.contains(dcn))
return type1;

if(d.contains(ccn))
return type2;

if(Modifier.isInterface(ccn.node.access) || Modifier.isInterface(dcn.node.access)) {
// enums as well?
return "java/lang/Object";
} else {
do {
ClassNode nccn = source.findClassNode(ccn.node.superName);
if(nccn == null)
break;
ccn = nccn;
c = tree.getAllParents(ccn);
} while(!c.contains(dcn));
return ccn.getName();
}
}
};
}

/**
Expand Down
4 changes: 2 additions & 2 deletions org.mapleir.dot4j/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@
<parent>
<groupId>org.mapleir</groupId>
<artifactId>parent</artifactId>
<version>0.0.1-ALPHA</version>
<version>1.0.0-SNAPSHOT</version>
<relativePath>../org.mapleir.parent</relativePath>
</parent>
<artifactId>dot4j</artifactId>
<name>dot4j</name>
<name>MapleIR-dot4j</name>
</project>
10 changes: 5 additions & 5 deletions org.mapleir.flowgraph/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,29 +3,29 @@
<parent>
<groupId>org.mapleir</groupId>
<artifactId>parent</artifactId>
<version>0.0.1-ALPHA</version>
<version>1.0.0-SNAPSHOT</version>
<relativePath>../org.mapleir.parent</relativePath>
</parent>
<name>flowgraph</name>
<name>MapleIR-flowgraph</name>
<artifactId>flowgraph</artifactId>
<groupId>org.mapleir</groupId>
<dependencies>
<dependency>
<groupId>org.mapleir</groupId>
<artifactId>stdlib</artifactId>
<version>0.0.1-ALPHA</version>
<version>1.0.0-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>org.mapleir</groupId>
<artifactId>stdlib</artifactId>
<version>0.0.1-ALPHA</version>
<version>1.0.0-SNAPSHOT</version>
<type>test-jar</type>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mapleir</groupId>
<artifactId>modasm</artifactId>
<version>0.0.1-ALPHA</version>
<version>1.0.0-SNAPSHOT</version>
</dependency>
</dependencies>
</project>
6 changes: 3 additions & 3 deletions org.mapleir.ir.printer/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,19 @@
<parent>
<groupId>org.mapleir</groupId>
<artifactId>parent</artifactId>
<version>0.0.1-ALPHA</version>
<version>1.0.0-SNAPSHOT</version>
<relativePath>../org.mapleir.parent</relativePath>
</parent>

<groupId>org.mapleir</groupId>
<artifactId>ir.printer</artifactId>
<name>ir.printer</name>
<name>MapleIR-ir.printer</name>

<dependencies>
<dependency>
<groupId>org.mapleir</groupId>
<artifactId>ir</artifactId>
<version>0.0.1-ALPHA</version>
<version>1.0.0-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>org.mapleir</groupId>
Expand Down
18 changes: 12 additions & 6 deletions org.mapleir.ir/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,33 +3,39 @@
<parent>
<groupId>org.mapleir</groupId>
<artifactId>parent</artifactId>
<version>0.0.1-ALPHA</version>
<version>1.0.0-SNAPSHOT</version>
<relativePath>../org.mapleir.parent</relativePath>
</parent>

<groupId>org.mapleir</groupId>
<name>ir</name>
<name>MapleIR-ir</name>
<artifactId>ir</artifactId>
<dependencies>
<dependency>
<groupId>org.mapleir</groupId>
<artifactId>stdlib</artifactId>
<version>0.0.1-ALPHA</version>
<version>1.0.0-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>org.mapleir</groupId>
<artifactId>modasm</artifactId>
<version>0.0.1-ALPHA</version>
<version>1.0.0-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>org.mapleir</groupId>
<artifactId>flowgraph</artifactId>
<version>0.0.1-ALPHA</version>
<version>1.0.0-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>org.mapleir</groupId>
<artifactId>app-services</artifactId>
<version>0.0.1-ALPHA</version>
<version>1.0.0-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.20</version>
<scope>provided</scope>
</dependency>
</dependencies>
</project>
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@
* @see <a href="https://hal.inria.fr/inria-00349925v1/document">Revisiting
* Out-of-SSA Translation for Correctness, CodeQuality, and Efficiency</a>
*
* Ref: Boissinot's PhD thesis: https://citeseerx.ist.psu.edu/viewdoc/download?doi=10.1.1.385.8510&rep=rep1&type=pdf
* Ref: Slides: https://compilers.cs.uni-saarland.de/ssasem/talks/Alain.Darte.pdf
* @see <a href="https://citeseerx.ist.psu.edu/viewdoc/download?doi=10.1.1.385.8510&rep=rep1&type=pdf">Boissinot's PhD thesis</a>
* @see <a href="https://compilers.cs.uni-saarland.de/ssasem/talks/Alain.Darte.pdf">Slides</a>
*/
public class BoissinotDestructor {
// private boolean DO_VALUE_INTERFERENCE = true;
Expand Down Expand Up @@ -274,6 +274,7 @@ private void computeValueInterference() {
Expr e = copy.getExpression();
Local b = copy.getVariable().getLocal();

// Expression has to be a VarExpr
if (!copy.isSynthetic() && e.getOpcode() == Opcode.LOCAL_LOAD) {
LinkedHashSet<Local> vc = values.get(((VarExpr) e).getLocal());
vc.add(b);
Expand Down Expand Up @@ -900,11 +901,15 @@ public boolean equivalent(CodeUnit s) {
private class CongruenceClass extends TreeSet<Local> {
private static final long serialVersionUID = -4472334406997712498L;

CongruenceClass() {
super((o1, o2) -> {
if (o1 == o2)
return 0;
return ((defuse.defIndex.get(o1) - defuse.defIndex.get(o2))) >> 31 | 1;
protected CongruenceClass() {
super(new Comparator<Local>() {
@Override
public int compare(Local o1, Local o2) {
if (o1 == o2)
return 0;
return ((BoissinotDestructor.this.defuse.defIndex.get(o1) - BoissinotDestructor.
this.defuse.defIndex.get(o2))) >> 31 | 1;
}
});
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,9 @@ public int compare(Local o1, Local o2) {
} else if (!s1 && s2) {
return 1;
} else {
return o1.compareTo(o2);
VersionedLocal vo1 = o1 instanceof VersionedLocal ? (VersionedLocal) o1 : cfg.getLocals().get(o1.getIndex(), 0);
VersionedLocal vo2 = o2 instanceof VersionedLocal ? (VersionedLocal) o2 : cfg.getLocals().get(o2.getIndex(), 0);
return vo1.compareTo(vo2);
}
}
});
Expand Down
Loading

0 comments on commit f871123

Please sign in to comment.