use of org.mapleir.ir.code.Stmt in project maple-ir by LLVM-but-worse.
the class LatestValue method findReachable.
private Set<Stmt> findReachable(Stmt from, Stmt to) {
Set<Stmt> res = new HashSet<>();
BasicBlock f = from.getBlock();
BasicBlock t = to.getBlock();
int end = f == t ? f.indexOf(to) : f.size();
for (int i = f.indexOf(from); i < end; i++) {
res.add(f.get(i));
}
if (f != t) {
for (BasicBlock r : cfg.wanderAllTrails(f, t)) {
res.addAll(r);
}
}
return res;
}
use of org.mapleir.ir.code.Stmt in project maple-ir by LLVM-but-worse.
the class DumbExceptionAnalysis method getPossibleUserThrowables.
@Override
public Set<Type> getPossibleUserThrowables(CodeUnit u) {
Set<Type> set = new HashSet<>();
if (u.isFlagSet(CodeUnit.FLAG_STMT)) {
Stmt s = (Stmt) u;
canThrowStmt(s, set);
for (Expr e : s.enumerateOnlyChildren()) {
canThrowExpr(e, set);
}
} else {
for (Expr e : ((Expr) u).enumerateWithSelf()) {
canThrowExpr(e, set);
}
}
return set;
}
use of org.mapleir.ir.code.Stmt in project maple-ir by LLVM-but-worse.
the class SensitiveCallGraphBuilder method process.
@Override
public void process(Worklist<MethodNode> worklist, MethodNode n) {
if (worklist != this.worklist) {
throw new IllegalStateException();
}
if (worklist.hasProcessed(n)) {
throw new UnsupportedOperationException(String.format("Already processed %s", n));
}
/* this is not the same as getNode */
CallGraphNode.CallReceiverNode currentReceiverNode = createNode(n, false);
ControlFlowGraph cfg = context.getIRCache().get(n);
if (cfg == null) {
return;
}
for (Stmt stmt : cfg.stmts()) {
for (Expr e : stmt.enumerateOnlyChildren()) {
if (e instanceof Invocation) {
Invocation invoke = (Invocation) e;
CallGraphNode.CallSiteNode thisCallSiteNode = callGraph.addInvocation(n, invoke);
/* link the current receiver to this call site. */
FunctionOwnershipEdge foe = new FunctionOwnershipEdge(currentReceiverNode, thisCallSiteNode);
callGraph.addEdge(currentReceiverNode, foe);
Set<MethodNode> targets = invoke.resolveTargets(context.getInvocationResolver());
for (MethodNode target : targets) {
CallGraphNode.CallReceiverNode targetReceiverNode = createNode(target, true);
/* link each target to the call site. */
SiteInvocationEdge sie = new SiteInvocationEdge(thisCallSiteNode, targetReceiverNode);
callGraph.addEdge(thisCallSiteNode, sie);
}
}
}
}
}
use of org.mapleir.ir.code.Stmt in project maple-ir by LLVM-but-worse.
the class DemoteRangesPass method process.
private void process(ApplicationClassSource app, ControlFlowGraph cfg, ExceptionAnalysis analysis) {
TarjanSCC<BasicBlock> sccComputor = new TarjanSCC<>(cfg);
for (BasicBlock b : cfg.vertices()) {
if (sccComputor.low(b) == -1) {
sccComputor.search(b);
}
}
Map<BasicBlock, List<BasicBlock>> sccs = new HashMap<>();
for (List<BasicBlock> l : sccComputor.getComponents()) {
for (BasicBlock e : l) {
if (sccs.containsKey(e)) {
throw new IllegalStateException();
} else {
sccs.put(e, l);
}
}
}
for (ExceptionRange<BasicBlock> er : cfg.getRanges()) {
/* if the handler catches */
for (BasicBlock b : er.get()) {
Set<Type> canThrow = new HashSet<>();
List<BasicBlock> comp = new ArrayList<>();
if (sccs.containsKey(b)) {
comp.addAll(sccs.get(b));
} else {
comp.add(b);
}
for (BasicBlock e : comp) {
for (Stmt stmt : e) {
canThrow.addAll(analysis.getPossibleUserThrowables(stmt));
}
}
if (!catchesAny(app, er.getTypes(), canThrow)) {
if (comp.size() > 1) {
System.out.println("promote: " + GraphUtils.toNodeArray(comp));
for (BasicBlock e : comp) {
System.out.println(ControlFlowGraph.printBlock(e));
}
System.out.println(" canThrow: " + canThrow);
System.out.println(" catching: " + er.getTypes());
System.out.println();
System.out.println();
System.out.println();
return;
}
} else {
break;
}
}
}
}
use of org.mapleir.ir.code.Stmt in project maple-ir by LLVM-but-worse.
the class FieldRSADecryptionPass method transform.
private void transform(AnalysisContext cxt) {
for (ClassNode cn : cxt.getApplication().iterate()) {
for (MethodNode m : cn.methods) {
ControlFlowGraph cfg = cxt.getIRCache().getFor(m);
for (BasicBlock b : cfg.vertices()) {
for (Stmt stmt : b) {
// String fsKey = "";
if (stmt.getOpcode() == Opcode.FIELD_STORE) {
FieldStoreStmt fs = (FieldStoreStmt) stmt;
// [enc, dec]
Number[] p = pairs.get(key(fs));
if (p != null) {
Expr e = fs.getValueExpression();
e.unlink();
ArithmeticExpr ae = new ArithmeticExpr(new ConstantExpr(p[1], ConstantExpr.computeType(p[1])), e, Operator.MUL);
fs.setValueExpression(ae);
// fsKey = key(fs);
}
}
for (Expr e : stmt.enumerateOnlyChildren()) {
if (e.getOpcode() == FIELD_LOAD) {
CodeUnit par = e.getParent();
FieldLoadExpr fl = (FieldLoadExpr) e;
// [enc, dec]
Number[] p = pairs.get(key(fl));
if (p == null) {
continue;
}
if (par.getOpcode() == ARITHMETIC) {
ArithmeticExpr ae = (ArithmeticExpr) par;
if (ae.getRight().getOpcode() == CONST_LOAD) {
ConstantExpr ce = (ConstantExpr) ae.getRight();
Number cst = (Number) ce.getConstant();
Number res = __mul(cst, p[0], p[0].getClass().equals(Long.class));
// if(!__eq(res, 1, p[0].getClass().equals(Long.class))) {
// System.out.println(cst + " -> " + res);
// System.out.println(" expr: " + fl.getRootParent());
// }
par.overwrite(new ConstantExpr(res, ConstantExpr.computeType(res)), par.indexOf(ce));
continue;
}
}
ArithmeticExpr ae = new ArithmeticExpr(new ConstantExpr(p[0], ConstantExpr.computeType(p[0])), fl.copy(), Operator.MUL);
par.overwrite(ae, par.indexOf(fl));
}
}
}
}
}
}
// for(ClassNode cn : cxt.getClassTree().getClasses().values()) {
// for(MethodNode m : cn.methods) {
// ControlFlowGraph cfg = cxt.getCFGS().getIR(m);
//
// for(BasicBlock b : cfg.vertices()) {
// for(Stmt stmt : b) {
// for(Expr e : stmt.enumerateOnlyChildren()) {
// if(e.getOpcode() == Opcode.ARITHMETIC) {
// ArithmeticExpr ae = (ArithmeticExpr) e;
// if(ae.getRight().getOpcode() == Opcode.CONST_LOAD) {
// ConstantExpr c = (ConstantExpr) ae.getRight();
// Object o = c.getConstant();
//
// if(o instanceof Long || o instanceof Integer) {
// Number n = (Number) o;
// if(__eq(n, 1, ae.getType().equals(Type.LONG_TYPE))) {
// Expr l = ae.getLeft();
// l.unlink();
//
// CodeUnit aePar = ae.getParent();
// aePar.overwrite(l, aePar.indexOf(ae));
// } else if(__eq(n, 0, ae.getType().equals(Type.LONG_TYPE))) {
// c.unlink();
//
// CodeUnit aePar = ae.getParent();
// aePar.overwrite(c, aePar.indexOf(ae));
// }
// }
// }
// }
// }
// }
// }
// }
// }
}
Aggregations