use of org.mapleir.ir.code.expr.invoke.InitialisedObjectExpr in project maple-ir by LLVM-but-worse.
the class SSAGenPass method aggregateInitialisers.
private void aggregateInitialisers() {
for (BasicBlock b : builder.graph.vertices()) {
for (Stmt stmt : new ArrayList<>(b)) {
if (stmt.getOpcode() == Opcode.POP) {
PopStmt pop = (PopStmt) stmt;
Expr expr = pop.getExpression();
if (expr.getOpcode() == Opcode.INVOKE) {
InvocationExpr invoke = (InvocationExpr) expr;
if (invoke.getCallType() == InvocationExpr.CallType.SPECIAL && invoke.getName().equals("<init>")) {
Expr inst = invoke.getPhysicalReceiver();
if (inst.getOpcode() == Opcode.LOCAL_LOAD) {
VarExpr var = (VarExpr) inst;
VersionedLocal local = (VersionedLocal) var.getLocal();
AbstractCopyStmt def = pool.defs.get(local);
Expr rhs = def.getExpression();
if (rhs.getOpcode() == Opcode.ALLOC_OBJ) {
// replace pop(x.<init>()) with x := new Klass();
// remove x := new Klass;
// here we are assuming that the new object
// can't be used until it is initialised.
Expr[] args = invoke.getParameterExprs();
// we want to reuse the exprs, so free it first.
pop.deleteAt(0);
Expr[] newArgs = Arrays.copyOf(args, args.length);
for (int i = args.length - 1; i >= 0; i--) {
args[i].unlink();
}
// remove the old def
def.delete();
int index = b.indexOf(pop);
// add a copy statement before the pop (x = newExpr)
InitialisedObjectExpr newExpr = new InitialisedObjectExpr(invoke.getOwner(), invoke.getDesc(), newArgs);
CopyVarStmt newCvs = new CopyVarStmt(var, newExpr);
pool.defs.put(local, newCvs);
pool.uses.get(local).remove(var);
b.add(index, newCvs);
// remove the pop statement
b.remove(pop);
// update the latestval constraints
LatestValue lval = latest.get(local);
if (lval.hasConstraints()) {
/* need to check this out (shouldn't happen) */
System.out.println("Constraints:");
for (Constraint c : lval.getConstraints()) {
System.out.println(" " + c);
}
throw new IllegalStateException(lval.toString());
} else {
lval.makeConstraints(newExpr);
}
}
} else if (inst.getOpcode() == Opcode.ALLOC_OBJ) {
// replace pop(new Klass.<init>(args)) with pop(new Klass(args))
// UninitialisedObjectExpr obj = (UninitialisedObjectExpr) inst;
Expr[] args = invoke.getParameterExprs();
// we want to reuse the exprs, so free it first.
invoke.unlink();
for (Expr e : args) {
e.unlink();
}
Expr[] newArgs = Arrays.copyOf(args, args.length);
InitialisedObjectExpr newExpr = new InitialisedObjectExpr(invoke.getOwner(), invoke.getDesc(), newArgs);
// replace pop contents
// no changes to defs or uses
pop.setExpression(newExpr);
} else {
System.err.println(b);
System.err.println("Stmt: " + stmt.getDisplayName() + ". " + stmt);
System.err.println("Inst: " + inst);
System.err.println(builder.graph);
throw new RuntimeException("interesting1 " + inst.getClass());
}
}
}
}
}
}
}
use of org.mapleir.ir.code.expr.invoke.InitialisedObjectExpr in project maple-ir by LLVM-but-worse.
the class ConstantParameterPass method patchCall.
private void patchCall(String newDesc, Expr call, boolean[] dead) {
if (call.getOpcode() == Opcode.INIT_OBJ) {
InitialisedObjectExpr init = (InitialisedObjectExpr) call;
CodeUnit parent = init.getParent();
Expr[] newArgs = buildArgs(init.getArgumentExprs(), false, dead);
InitialisedObjectExpr init2 = new InitialisedObjectExpr(init.getOwner(), newDesc, newArgs);
parent.overwrite(init2, parent.indexOf(init));
} else if (call.getOpcode() == Opcode.INVOKE) {
InvocationExpr invoke = (InvocationExpr) call;
CodeUnit parent = invoke.getParent();
Expr[] newArgs = buildArgs(invoke.getArgumentExprs(), invoke.getCallType() != InvocationExpr.CallType.STATIC, dead);
InvocationExpr invoke2 = new InvocationExpr(invoke.getCallType(), newArgs, invoke.getOwner(), invoke.getName(), newDesc);
parent.overwrite(invoke2, parent.indexOf(invoke));
} else {
throw new UnsupportedOperationException(call.toString());
}
}
use of org.mapleir.ir.code.expr.invoke.InitialisedObjectExpr in project maple-ir by LLVM-but-worse.
the class ClassRenamerPass method accept.
/*private String getClassName(String name) {
int i = name.lastIndexOf('/');
if(i == -1) {
return name;
} else {
return name.substring(i + 1, name.length());
}
}*/
@Override
public int accept(AnalysisContext cxt, IPass prev, List<IPass> completed) {
ApplicationClassSource source = cxt.getApplication();
Collection<ClassNode> classes = CollectionUtils.collate(source.iterator());
// int min = RenamingUtil.computeMinimum(classes.size());
int n = RenamingUtil.numeric("aaa");
int step = 27;
for (ClassNode cn : classes) {
String className = RenamingUtil.getClassName(cn.name);
if (!heuristic.shouldRename(className, cn.access)) {
System.out.println("Heuristic bypass " + cn.name);
}
String newName = heuristic.shouldRename(className, cn.access) ? RenamingUtil.createName(n) : className;
String s = RenamingUtil.getPackage(cn.name) + newName;
n += step;
remapping.put(cn.name, s);
// System.out.println(cn.name + " -> " + s);
cn.name = s;
}
for (ClassNode cn : classes) {
cn.superName = remapping.getOrDefault(cn.superName, cn.superName);
{
List<String> ifaces = new ArrayList<>();
for (int i = 0; i < cn.interfaces.size(); i++) {
String s = cn.interfaces.get(i);
ifaces.add(remapping.getOrDefault(s, s));
}
cn.interfaces = ifaces;
}
unsupported(cn.signature);
// unsupported(cn.sourceFile);
// unsupported(cn.sourceDebug);
cn.outerClass = remapping.getOrDefault(cn.outerClass, cn.outerClass);
// unsupported(cn.outerMethod);
// unsupported(cn.outerMethodDesc);
unsupported(cn.visibleAnnotations);
unsupported(cn.invisibleAnnotations);
unsupported(cn.visibleTypeAnnotations);
unsupported(cn.invisibleTypeAnnotations);
unsupported(cn.attrs);
unsupported(cn.innerClasses);
for (FieldNode f : cn.fields) {
unsupported(cn.signature);
{
Type type = Type.getType(f.desc);
String newType = resolveType(type, remapping);
if (newType != null) {
f.desc = newType;
}
}
unsupported(f.visibleAnnotations);
unsupported(f.invisibleAnnotations);
unsupported(f.visibleTypeAnnotations);
unsupported(f.invisibleTypeAnnotations);
unsupported(f.attrs);
}
for (MethodNode m : cn.methods) {
m.desc = resolveMethod(m.desc, remapping);
unsupported(m.signature);
{
List<String> exceptions = new ArrayList<>();
for (int i = 0; i < m.exceptions.size(); i++) {
String s = m.exceptions.get(i);
exceptions.add(remapping.getOrDefault(s, s));
}
m.exceptions = exceptions;
}
unsupported(m.parameters);
unsupported(m.visibleAnnotations);
unsupported(m.invisibleAnnotations);
unsupported(m.visibleTypeAnnotations);
unsupported(m.invisibleTypeAnnotations);
unsupported(m.attrs);
unsupported(m.annotationDefault);
unsupported(m.visibleParameterAnnotations);
unsupported(m.invisibleParameterAnnotations);
for (TryCatchBlockNode tcbn : m.tryCatchBlocks) {
tcbn.type = remapping.getOrDefault(tcbn.type, tcbn.type);
}
ControlFlowGraph cfg = cxt.getIRCache().getFor(m);
for (ExceptionRange<BasicBlock> er : cfg.getRanges()) {
Set<Type> newTypeSet = new HashSet<>();
for (Type t : er.getTypes()) {
// FIXME:
String s = t.getInternalName();
if (remapping.containsKey(s)) {
newTypeSet.add(Type.getType("L" + remapping.get(s) + ";"));
} else {
newTypeSet.add(t);
}
}
er.setTypes(newTypeSet);
}
if (m.localVariables != null) {
m.localVariables.clear();
for (LocalVariableNode lvn : m.localVariables) {
String newDesc = resolveType(Type.getType(lvn.desc), remapping);
if (newDesc != null) {
lvn.desc = newDesc;
}
unsupported(lvn.signature);
}
}
unsupported(m.visibleLocalVariableAnnotations);
unsupported(m.invisibleLocalVariableAnnotations);
for (BasicBlock b : cfg.vertices()) {
for (Stmt stmt : b) {
if (stmt.getOpcode() == Opcode.FIELD_STORE) {
FieldStoreStmt fs = (FieldStoreStmt) stmt;
String owner = fs.getOwner();
fs.setOwner(remapping.getOrDefault(owner, owner));
{
Type type = Type.getType(fs.getDesc());
String newType = resolveType(type, remapping);
if (newType != null) {
fs.setDesc(newType);
}
}
} else if (stmt.getOpcode() == Opcode.RETURN) {
ReturnStmt ret = (ReturnStmt) stmt;
String newType = resolveType(ret.getType(), remapping);
if (newType != null) {
ret.setType(Type.getType(newType));
}
} else if (stmt instanceof AbstractCopyStmt) {
AbstractCopyStmt copy = (AbstractCopyStmt) stmt;
VarExpr v = copy.getVariable();
String newType = resolveType(v.getType(), remapping);
if (newType != null) {
v.setType(Type.getType(newType));
}
}
for (Expr e : stmt.enumerateOnlyChildren()) {
if (e.getOpcode() == Opcode.CAST) {
CastExpr cast = (CastExpr) e;
String newType = resolveType(cast.getType(), remapping);
if (newType != null) {
cast.setType(Type.getType(newType));
}
} else if (e.getOpcode() == Opcode.CATCH) {
CaughtExceptionExpr caught = (CaughtExceptionExpr) e;
String newType = resolveType(caught.getType(), remapping);
if (newType != null) {
caught.setType(Type.getType(newType));
}
} else if (e.getOpcode() == Opcode.DYNAMIC_INVOKE) {
throw new UnsupportedOperationException();
} else if (e.getOpcode() == Opcode.INVOKE) {
InvocationExpr invoke = (InvocationExpr) e;
invoke.setOwner(remapping.getOrDefault(invoke.getOwner(), invoke.getOwner()));
invoke.setDesc(resolveMethod(invoke.getDesc(), remapping));
} else if (e.getOpcode() == Opcode.FIELD_LOAD) {
FieldLoadExpr fl = (FieldLoadExpr) e;
fl.setOwner(remapping.getOrDefault(fl.getOwner(), fl.getOwner()));
String newType = resolveType(fl.getType(), remapping);
if (newType != null) {
fl.setDesc(newType);
}
} else if (e.getOpcode() == Opcode.INIT_OBJ) {
InitialisedObjectExpr init = (InitialisedObjectExpr) e;
init.setOwner(remapping.getOrDefault(init.getOwner(), init.getOwner()));
init.setDesc(resolveMethod(init.getDesc(), remapping));
} else if (e.getOpcode() == Opcode.INSTANCEOF) {
InstanceofExpr inst = (InstanceofExpr) e;
String newType = resolveType(inst.getCheckType(), remapping);
if (newType != null) {
inst.setCheckType(Type.getType(newType));
}
} else if (e.getOpcode() == Opcode.NEW_ARRAY) {
NewArrayExpr na = (NewArrayExpr) e;
String newType = resolveType(na.getType(), remapping);
if (newType != null) {
na.setType(Type.getType(newType));
}
} else if (e.getOpcode() == Opcode.ALLOC_OBJ) {
AllocObjectExpr uninit = (AllocObjectExpr) e;
String newType = resolveType(uninit.getType(), remapping);
if (newType != null) {
uninit.setType(Type.getType(newType));
}
} else if (e.getOpcode() == Opcode.LOCAL_LOAD) {
VarExpr v = (VarExpr) e;
String newType = resolveType(v.getType(), remapping);
if (newType != null) {
v.setType(Type.getType(newType));
}
} else if (e.getOpcode() == Opcode.CONST_LOAD) {
ConstantExpr c = (ConstantExpr) e;
Object cst = c.getConstant();
if (cst instanceof Type) {
Type t = (Type) cst;
if (t.getSort() == Type.OBJECT) {
String newType = resolveType(t, remapping);
if (newType != null) {
c.setConstant(Type.getType(newType));
}
} else {
throw new UnsupportedOperationException(String.format("Unsupported ctype %s (%d)", t, t.getSort()));
}
}
}
}
}
}
}
}
source.rebuildTable();
return classes.size();
}
Aggregations