Skip to content

Commit

Permalink
Merge branch 'master' into enterprise
Browse files Browse the repository at this point in the history
  • Loading branch information
terminalsin committed May 21, 2022
2 parents 24bd659 + 4e5a488 commit 82d32d1
Show file tree
Hide file tree
Showing 61 changed files with 874 additions and 278 deletions.
1 change: 1 addition & 0 deletions docs/bug-naturalisation-pass.drawio.drawio
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<mxfile host="app.diagrams.net" modified="2022-05-21T13:18:04.278Z" agent="5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/101.0.4951.64 Safari/537.36" etag="Oi3irG9dJ9Qzta5kf0T0" version="18.0.1" type="device"><diagram id="IGXKT6YkwEFYN9pUYwX3" name="Page-1">7VxbX9o8GP80XOqvRw6XoHXzHYob25yXgUboa2lYGibu0y+h6SFJS6u2Bd28wOZpmob/c36eaMc8W20/YLBeXiEX+h1Dc7cd87xjGAOrSz8Z4Ski6D2DUxbYczktJUy935ATNU7deC4MhYkEIZ94a5E4R0EA50SgAYzRozjtHvniW9dgARXCdA58lXrruWQZUftGL6V/hN5iGb9Z7w6iOysQT+bfJFwCFz1mSKbTMc8wQiS6Wm3PoM/Ai3GJnrsouJtsDMOAVHmgO5wNb39+/v7p5OrzZPP7vzXYjk/MaJVfwN/wLzxG9MuHfMvkKcaB7n7NLuebGf01elx6BE7XYM5oj5T1lLYkK5+OdHo5Q5vAhe54lhDA/GGBGXWyIb4XQE53AX6Y0GU8wgREO9VskWjsqGwm3yjEBG4LEdATXKlAQrSCBD/RKfwBMxYqLow9PnxMGavH3FpmmNrnNMBlaZGsnMJNLzjiz0C/r8AMXSp9fIgwWaIFCoDvpNQRjqBluNBROmeM0JqD+j8k5ImrEtgQJPIGbj3yI3N9t4PY5qPzLV95N3iKBwH9uj+yg7vsIH1oN2JPmcnwBmKPwgUxnzPf4F+7/Uf8D5fJIEKDQbCfwdQEALyApEyuVUHA0AfE+yWun8dW/ugN8uibEwHSe6IA6QNbXC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uQRYVruxdtdQtDCOGVjUxYJtwGHq/tyw4++jOC5LCIK8xERW2jqJZZZZx8i6aWzVk3AnCYw6WG939HStKEy5gvRru3TCjCXzYaZCGm3r7ddIX2Iu5H5icrgqYy9aLZvaB7UXNTeLj9VeCCyXqmw1NZAN6bC8ae5vIMvz4wJ+YUVB2ztfbDirT0u1RlM2kg3XGm1dMZLvunPdk4PFQ4cltnqe7m9ob3Zl3T90f9POaTMr/c0bFJKTAJANpnCGLDn6S/uc9iDf6GXZl5iydsJ71ZCp/jst+nNYhc5Uhi2MfgMIhTrYUQzNzO2fiMAmwX69zrvqeZCiCKudEr3cB0rU97kl+p6yULXAoLa8QbXIt8Mv15fXH6K4/qPzxaG/vn5kn9+H429OVBfRbi/HY1YvGd8O76aMxm58cabOVzZ9EpVS6J60q+Enthpb45JNHI6mk/G3r874jg6+TZ2xM52eJq8YTyY3u9XOJlcOm+5cn7MZR2pdqhW62TDeRMcwLy5YwliPberr4oGlRMCz9SDLbNE26WpD8mwJgkW2nL2v46PXVFwrOdQnFZbNvD/tMpLzLe0gp1bS9ln1Nlq5NRrsoj/qaqmnKp/gHbzQYMsLmS03VW21qXqE6tWT1Su3b1OXetFh+i8dIqDTf4xhOn8A</diagram></mxfile>
Original file line number Diff line number Diff line change
Expand Up @@ -57,10 +57,6 @@ public void addLibraries(LibraryClassSource... libs) {

libraries.sort(Comparator.comparingInt(LibraryClassSource::getPriority));
Collections.reverse(libraries);

for (LibraryClassSource library : libraries) {
System.out.println("LIBRARY --> " + library.parent.getName() + " [priority: " + library.getPriority() + "]");
}
}

public ClassNode findClassNode(String name) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ protected ClassNode findClass(String name) {

private ClassNode requestClass0(String name, String from) {
try {
return source.findClassNode(name);
return findClass(name);
} catch(RuntimeException e) {
throw new RuntimeException("request from " + from, e);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import org.objectweb.asm.ClassWriter;
import org.mapleir.asm.ClassNode;
import org.mapleir.asm.MethodNode;
import org.topdank.byteengineer.commons.data.JarClassData;
import org.topdank.byteengineer.commons.data.JarContents;
import org.topdank.byteengineer.commons.data.JarResource;
import org.topdank.byteio.out.JarDumper;
Expand All @@ -25,14 +26,14 @@
*/
public class CompleteResolvingJarDumper implements JarDumper {

private final JarContents<?> contents;
private final JarContents contents;
private final ApplicationClassSource source;
/**
* Creates a new JarDumper.
*
* @param contents Contents of jar.
*/
public CompleteResolvingJarDumper(JarContents<ClassNode> contents, ApplicationClassSource source) {
public CompleteResolvingJarDumper(JarContents contents, ApplicationClassSource source) {
this.contents = contents;
this.source = source;
}
Expand All @@ -50,8 +51,8 @@ public void dump(File file) throws IOException {
JarOutputStream jos = new JarOutputStream(new FileOutputStream(file));
int classesDumped = 0;
int resourcesDumped = 0;
for (ClassNode cn : contents.getClassContents()) {
classesDumped += dumpClass(jos, cn.getName(), cn);
for (JarClassData cn : contents.getClassContents()) {
classesDumped += dumpClass(jos, cn);
}
for (JarResource res : contents.getResourceContents()) {
resourcesDumped += dumpResource(jos, res.getName(), res.getData());
Expand All @@ -66,13 +67,13 @@ public void dump(File file) throws IOException {
* Writes the {@link ClassNode} to the Jar.
*
* @param out The {@link JarOutputStream}.
* @param cn The ClassNode.
* @param name The entry name.
* @param classData The {@link JarClassData}
* @throws IOException If there is a write error.
* @return The amount of things dumped, 1 or if you're not dumping it 0.
*/
@Override
public int dumpClass(JarOutputStream out, String name, ClassNode cn) throws IOException {
public int dumpClass(JarOutputStream out, JarClassData classData) throws IOException {
ClassNode cn = classData.getClassNode();
JarEntry entry = new JarEntry(cn.getName() + ".class");
out.putNextEntry(entry);
ClassTree tree = source.getClassTree();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,14 @@
import org.mapleir.ir.cfg.builder.ssa.BlockBuilder;
import org.mapleir.ir.cfg.builder.ssa.CfgBuilder;
import org.mapleir.ir.cfg.builder.ssa.expr.*;
import org.mapleir.ir.cfg.builder.ssa.expr.invoke.StaticInvocationExprBuilder;
import org.mapleir.ir.cfg.builder.ssa.stmt.*;
import org.mapleir.ir.cfg.builder.ssa.stmt.copy.CopyPhiStmtBuilder;
import org.mapleir.ir.cfg.builder.ssa.stmt.copy.CopyVarStmtBuilder;
import org.mapleir.ir.code.Expr;
import org.mapleir.ir.code.expr.*;
import org.mapleir.ir.code.expr.invoke.InvocationExpr;
import org.mapleir.ir.code.expr.invoke.StaticInvocationExpr;
import org.mapleir.ir.code.stmt.*;
import org.mapleir.ir.code.stmt.copy.CopyPhiStmt;
import org.mapleir.ir.code.stmt.copy.CopyVarStmt;
Expand Down Expand Up @@ -304,6 +307,63 @@ public ConstantExpr build() {
};
}

@Override
public StaticInvocationExprBuilder static_invoke_expr() {
return new StaticInvocationExprBuilder() {
private InvocationExpr.CallType callType = InvocationExpr.CallType.STATIC;
private Expr[] args;
private String owner;
private String name;
private String desc;

@Override
public StaticInvocationExprBuilder callType(InvocationExpr.CallType callType) {
this.callType = callType;
return this;
}

@Override
public StaticInvocationExprBuilder args(Expr[] args) {
this.args = args;
return this;
}

@Override
public StaticInvocationExprBuilder owner(String owner) {
this.owner = owner;
return this;
}

@Override
public StaticInvocationExprBuilder name(String name) {
this.name = name;
return this;
}

@Override
public StaticInvocationExprBuilder desc(String desc) {
this.desc = desc;
return this;
}

@Override
public StaticInvocationExpr build() {
assert owner != null : "Owner name cannot be null";
assert name != null : "Name cannot be null";
assert desc != null : "Description cannot be null";


return new StaticInvocationExpr(
callType,
args,
owner,
name,
desc
);
}
};
}

@Override
public FieldLoadExprBuilder field_load_expr() {
return new FieldLoadExprBuilder() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import org.mapleir.ir.cfg.builder.ssa.BlockBuilder;
import org.mapleir.ir.cfg.builder.ssa.CfgBuilder;
import org.mapleir.ir.cfg.builder.ssa.expr.*;
import org.mapleir.ir.cfg.builder.ssa.expr.invoke.StaticInvocationExprBuilder;
import org.mapleir.ir.cfg.builder.ssa.stmt.*;
import org.mapleir.ir.cfg.builder.ssa.stmt.copy.CopyPhiStmtBuilder;
import org.mapleir.ir.cfg.builder.ssa.stmt.copy.CopyVarStmtBuilder;
Expand Down Expand Up @@ -43,6 +44,8 @@ public interface SSAFactory {

VarExprBuilder var_expr();

StaticInvocationExprBuilder static_invoke_expr();

CopyPhiStmtBuilder copy_phi_stmt();

CopyVarStmtBuilder copy_var_stmt();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1249,7 +1249,13 @@ protected void _call(int op, String owner, String name, String desc) {
callExpr = new VirtualInvocationExpr(VirtualInvocationExpr.resolveCallType(op), args, owner, name, desc);
break;
case Opcodes.INVOKESTATIC:
callExpr = new StaticInvocationExpr(args, owner, name, desc);
callExpr = builder.factory
.static_invoke_expr()
.args(args)
.owner(owner)
.name(name)
.desc(desc)
.build();
break;
default:
throw new IllegalArgumentException("invalid call opcode " + op);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,19 @@ class MergePair {
continue;
}
Set<FlowEdge<BasicBlock>> inSuccs = in.cfg.getSuccessors(e -> !(e instanceof TryCatchEdge), in);
/*
* In a nutshell, to be able to properly merge two blocks, we're looking for this
* exact scenario:
*
* B.. B..
* \ / <-- No matters. We don't really care
* BX1 (in)
* | <-- CRITICAL! Must only be ONE edge which is a successor and
* v only one reverse edge for the successor
* BX2 (b)
* / \ <-- We don't really care from here on out
* B.. B.;
*/
if(inSuccs.size() != 1 || builder.graph.getReverseEdges(b).size() != 1) {
continue;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package org.mapleir.ir.cfg.builder.ssa.expr.invoke;

import org.mapleir.app.factory.Builder;
import org.mapleir.ir.code.Expr;
import org.mapleir.ir.code.expr.invoke.InvocationExpr;
import org.mapleir.ir.code.expr.invoke.StaticInvocationExpr;

public interface StaticInvocationExprBuilder extends Builder<StaticInvocationExpr> {
StaticInvocationExprBuilder callType(InvocationExpr.CallType callType);
StaticInvocationExprBuilder args(Expr[] args);
StaticInvocationExprBuilder owner(String owner);
StaticInvocationExprBuilder name(String name);
StaticInvocationExprBuilder desc(String desc);
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@

import org.mapleir.app.service.InvocationResolver;
import org.mapleir.ir.code.Expr;
import org.mapleir.ir.code.Opcode;
import org.mapleir.stdlib.collections.CollectionUtils;
import org.objectweb.asm.ClassWriter;
import org.objectweb.asm.MethodVisitor;
import org.objectweb.asm.Opcodes;
import org.mapleir.asm.MethodNode;
Expand All @@ -15,6 +17,10 @@ public StaticInvocationExpr(Expr[] args, String owner, String name, String desc)
super(CallType.STATIC, args, owner, name, desc);
}

public StaticInvocationExpr(CallType callType, Expr[] args, String owner, String name, String desc) {
super(callType, args, owner, name, desc);
}

@Override
public StaticInvocationExpr copy() {
return new StaticInvocationExpr(copyArgs(), getOwner(), getName(), getDesc());
Expand All @@ -32,7 +38,13 @@ public boolean isDynamic() {

@Override
protected void generateCallCode(MethodVisitor visitor) {
visitor.visitMethodInsn(Opcodes.INVOKESTATIC, getOwner(), getName(), getDesc(), getCallType() == CallType.INTERFACE);
visitor.visitMethodInsn(
Opcodes.INVOKESTATIC,
getOwner(),
getName(),
getDesc(),
getCallType() == CallType.INTERFACE
);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import org.mapleir.ir.codegen.BytecodeFrontend;
import org.mapleir.stdlib.util.TabbedStringWriter;
import org.objectweb.asm.MethodVisitor;
import org.objectweb.asm.Opcodes;

public class NopStmt extends Stmt {
public NopStmt() {
Expand All @@ -23,6 +24,7 @@ public void toString(TabbedStringWriter printer) {

@Override
public void toCode(MethodVisitor visitor, BytecodeFrontend assembler) {
visitor.visitInsn(Opcodes.NOP);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import org.mapleir.ir.codegen.ControlFlowGraphDumper;
import org.mapleir.asm.ClassNode;
import org.mapleir.asm.MethodNode;
import org.topdank.byteengineer.commons.data.JarClassData;
import org.topdank.byteengineer.commons.data.JarInfo;
import org.topdank.byteio.in.SingleJarDownloader;

Expand All @@ -34,6 +35,7 @@
import java.util.*;
import java.util.Map.Entry;
import java.util.jar.JarOutputStream;
import java.util.stream.Collectors;

public class Boot {

Expand Down Expand Up @@ -63,7 +65,10 @@ public static void main(String[] args) throws Exception {
SingleJarDownloader<ClassNode> dl = new SingleJarDownloader<>(new JarInfo(f));
dl.download();
String appName = f.getName().substring(0, f.getName().length() - 4);
ApplicationClassSource app = new ApplicationClassSource(appName, dl.getJarContents().getClassContents());
ApplicationClassSource app = new ApplicationClassSource(
appName,
dl.getJarContents().getClassContents().stream().map(JarClassData::getClassNode).collect(Collectors.toList())
);
//
// ApplicationClassSource app = new ApplicationClassSource("test", ClassHelper.parseClasses(CGExample.class));
// app.addLibraries(new InstalledRuntimeClassSource(app));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,15 @@
import org.mapleir.ir.codegen.ControlFlowGraphDumper;
import org.mapleir.asm.ClassNode;
import org.mapleir.asm.MethodNode;
import org.topdank.byteengineer.commons.data.JarClassData;
import org.topdank.byteengineer.commons.data.JarInfo;
import org.topdank.byteio.in.SingleJarDownloader;

import java.io.*;
import java.util.*;
import java.util.Map.Entry;
import java.util.jar.JarOutputStream;
import java.util.stream.Collectors;

public class Boot2 {

Expand Down Expand Up @@ -58,7 +60,10 @@ public static void main(String[] args) throws Exception {
SingleJarDownloader<ClassNode> dl = new SingleJarDownloader<>(new JarInfo(f));
dl.download();
String appName = f.getName().substring(0, f.getName().length() - 4);
ApplicationClassSource app = new ApplicationClassSource(appName, dl.getJarContents().getClassContents());
ApplicationClassSource app = new ApplicationClassSource(
appName,
dl.getJarContents().getClassContents().stream().map(JarClassData::getClassNode).collect(Collectors.toList())
);
//
// ApplicationClassSource app = new ApplicationClassSource("test", ClassHelper.parseClasses(CGExample.class));
// app.addLibraries(new InstalledRuntimeClassSource(app));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,15 @@
import org.mapleir.stdlib.util.JavaDescUse;
import org.mapleir.asm.ClassNode;
import org.mapleir.asm.MethodNode;
import org.topdank.byteengineer.commons.data.JarClassData;
import org.topdank.byteengineer.commons.data.JarInfo;
import org.topdank.byteio.in.SingleJarDownloader;

import java.io.File;
import java.io.IOException;
import java.util.*;
import java.util.Map.Entry;
import java.util.stream.Collectors;

public class DataFlowDemoBoot {

Expand Down Expand Up @@ -58,7 +60,10 @@ public static void main(String[] args) throws Exception {
SingleJarDownloader<ClassNode> dl = new SingleJarDownloader<>(new JarInfo(f));
dl.download();
String appName = f.getName().substring(0, f.getName().length() - 4);
ApplicationClassSource app = new ApplicationClassSource(appName, dl.getJarContents().getClassContents());
ApplicationClassSource app = new ApplicationClassSource(
appName,
dl.getJarContents().getClassContents().stream().map(JarClassData::getClassNode).collect(Collectors.toList())
);
//
// ApplicationClassSource app = new ApplicationClassSource("test", ClassHelper.parseClasses(CGExample.class));
// app.addLibraries(new InstalledcoRuntimeClassSource(app));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@
import java.util.List;
import java.util.Map;

import org.objectweb.asm.ClassReader;
import org.objectweb.asm.ClassWriter;
import org.objectweb.asm.*;
import org.objectweb.asm.commons.JSRInlinerAdapter;

/**
* @author Bibl (don't ban me pls)
Expand Down Expand Up @@ -41,7 +41,13 @@ public static ClassNode create(byte[] bytes) {
public static ClassNode create(byte[] bytes, int flags) {
ClassReader reader = new ClassReader(bytes);
org.objectweb.asm.tree.ClassNode node = new org.objectweb.asm.tree.ClassNode();
reader.accept(node, flags);
ClassVisitor visitor = new ClassVisitor(Opcodes.ASM9, node) {
@Override public MethodVisitor visitMethod(int access, String name, String desc, String signature, String[] exceptions) {
return new JSRInlinerAdapter(super.visitMethod(access, name, desc, signature, exceptions), access, name, desc, signature, exceptions);
}
};

reader.accept(visitor, flags);
return new ClassNode(node);
}

Expand Down
Loading

0 comments on commit 82d32d1

Please sign in to comment.