Search in sources :

Example 21 with ControlFlowGraph

use of org.mapleir.ir.cfg.ControlFlowGraph in project maple-ir by LLVM-but-worse.

the class RunCommand method call.

@Override
public Integer call() throws Exception {
    if (input == null) {
        logger.print("Fatal! Failed to find input jar!");
        return 1;
    }
    // Initialization
    logger.section("Preparing to run on " + input.getAbsolutePath());
    SingleJarDownloader<ClassNode> dl = new SingleJarDownloader<>(new JarInfo(input));
    dl.download();
    String appName = input.getName().substring(0, input.getName().length() - 4);
    ApplicationClassSource app = new ApplicationClassSource(appName, dl.getJarContents().getClassContents());
    if (output == null) {
        output = new File(appName + "-out.jar");
    }
    logger.section("Importing runtime...");
    if (runtime == null) {
        runtime = new File(System.getProperty("java.home"), "lib/rt.jar");
    }
    app.addLibraries(rt(app, runtime));
    logger.section("Initialising context.");
    IRCache irFactory = new IRCache(ControlFlowGraphBuilder::build);
    AnalysisContext cxt = new BasicAnalysisContext.BasicContextBuilder().setApplication(app).setInvocationResolver(new DefaultInvocationResolver(app)).setCache(irFactory).setApplicationContext(new SimpleApplicationContext(app)).setDataFlowAnalysis(new LiveDataFlowAnalysisImpl(irFactory)).build();
    logger.section("Expanding callgraph and generating cfgs.");
    for (ClassNode cn : cxt.getApplication().iterate()) {
        for (MethodNode m : cn.getMethods()) {
            cxt.getIRCache().getFor(m);
        }
    }
    logger.section0("...generated " + cxt.getIRCache().size() + " cfgs in %fs.%n", "Preparing to transform.");
    // do passes
    PassGroup masterGroup = new PassGroup("MasterController");
    for (IPass p : getTransformationPasses()) {
        masterGroup.add(p);
    }
    run(cxt, masterGroup);
    logger.section0("...done transforming in %fs.%n", "Preparing to transform.");
    for (Map.Entry<MethodNode, ControlFlowGraph> e : cxt.getIRCache().entrySet()) {
        MethodNode mn = e.getKey();
        ControlFlowGraph cfg = e.getValue();
        cfg.verify();
    }
    logger.section("Retranslating SSA IR to standard flavour.");
    for (Map.Entry<MethodNode, ControlFlowGraph> e : cxt.getIRCache().entrySet()) {
        MethodNode mn = e.getKey();
        // if (!mn.getName().equals("openFiles"))
        // continue;
        ControlFlowGraph cfg = e.getValue();
        // System.out.println(cfg);
        // CFGUtils.easyDumpCFG(cfg, "pre-destruct");
        cfg.verify();
        BoissinotDestructor.leaveSSA(cfg);
        // CFGUtils.easyDumpCFG(cfg, "pre-reaalloc");
        LocalsReallocator.realloc(cfg);
        // CFGUtils.easyDumpCFG(cfg, "post-reaalloc");
        // System.out.println(cfg);
        cfg.verify();
        // System.out.println("Rewriting " + mn.getName());
        (new ControlFlowGraphDumper(cfg, mn)).dump();
    // System.out.println(InsnListUtils.insnListToString(mn.instructions));
    }
    logger.section("Rewriting jar.");
    dumpJar(app, dl, masterGroup, output.getPath());
    logger.section("Finished.");
    return 0;
}
Also used : ClassNode(org.mapleir.asm.ClassNode) DefaultInvocationResolver(org.mapleir.DefaultInvocationResolver) LiveDataFlowAnalysisImpl(org.mapleir.deob.dataflow.LiveDataFlowAnalysisImpl) IRCache(org.mapleir.context.IRCache) AnalysisContext(org.mapleir.context.AnalysisContext) BasicAnalysisContext(org.mapleir.context.BasicAnalysisContext) SimpleApplicationContext(org.mapleir.app.client.SimpleApplicationContext) IPass(org.mapleir.deob.IPass) ControlFlowGraphDumper(org.mapleir.ir.codegen.ControlFlowGraphDumper) PassGroup(org.mapleir.deob.PassGroup) ApplicationClassSource(org.mapleir.app.service.ApplicationClassSource) MethodNode(org.mapleir.asm.MethodNode) JarInfo(org.topdank.byteengineer.commons.data.JarInfo) ControlFlowGraphBuilder(org.mapleir.ir.cfg.builder.ControlFlowGraphBuilder) ControlFlowGraph(org.mapleir.ir.cfg.ControlFlowGraph) SingleJarDownloader(org.topdank.byteio.in.SingleJarDownloader)

Example 22 with ControlFlowGraph

use of org.mapleir.ir.cfg.ControlFlowGraph 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(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(sie);
                }
            }
        }
    }
}
Also used : Invocation(org.mapleir.ir.code.expr.invoke.Invocation) SiteInvocationEdge(org.mapleir.deob.callgraph.CallGraphEdge.SiteInvocationEdge) Stmt(org.mapleir.ir.code.Stmt) Expr(org.mapleir.ir.code.Expr) MethodNode(org.mapleir.asm.MethodNode) ControlFlowGraph(org.mapleir.ir.cfg.ControlFlowGraph) FunctionOwnershipEdge(org.mapleir.deob.callgraph.CallGraphEdge.FunctionOwnershipEdge)

Example 23 with ControlFlowGraph

use of org.mapleir.ir.cfg.ControlFlowGraph in project maple-ir by LLVM-but-worse.

the class DataFlowDemoBoot method main.

public static void main(String[] args) throws Exception {
    sections = new LinkedList<>();
    logging = true;
    // Load input jar
    // File f = locateRevFile(135);
    File f = new File("res/jump.jar");
    section("Preparing to run on " + f.getAbsolutePath());
    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("test", ClassHelper.parseClasses(CGExample.class));
    // app.addLibraries(new InstalledcoRuntimeClassSource(app));
    File rtjar = new File("res/rt.jar");
    File androidjar = new File("res/android.jar");
    app.addLibraries(rt(app, rtjar), rt(app, androidjar));
    section("Initialising context.");
    IRCache irFactory = new IRCache(ControlFlowGraphBuilder::build);
    AnalysisContext cxt = new BasicAnalysisContext.BasicContextBuilder().setApplication(app).setInvocationResolver(new DefaultInvocationResolver(app)).setCache(irFactory).setApplicationContext(new SimpleApplicationContext(app)).setDataFlowAnalysis(new LiveDataFlowAnalysisImpl(irFactory)).build();
    section("Expanding callgraph and generating cfgs.");
    for (ClassNode cn : cxt.getApplication().iterate()) {
        // continue;
        for (MethodNode m : cn.getMethods()) {
            // if (!m.name.equals("setRccState"))
            // continue;
            cxt.getIRCache().getFor(m);
        }
    }
    section0("...generated " + cxt.getIRCache().size() + " cfgs in %fs.%n", "Preparing to transform.");
    // do passes
    PassGroup masterGroup = new PassGroup("MasterController");
    for (IPass p : getTransformationPasses()) {
        masterGroup.add(p);
    }
    run(cxt, masterGroup);
    section0("...done transforming in %fs.%n", "Preparing to transform.");
    for (Entry<MethodNode, ControlFlowGraph> e : cxt.getIRCache().entrySet()) {
        MethodNode mn = e.getKey();
        ControlFlowGraph cfg = e.getValue();
        cfg.verify();
    }
}
Also used : ClassNode(org.mapleir.asm.ClassNode) LiveDataFlowAnalysisImpl(org.mapleir.deob.dataflow.LiveDataFlowAnalysisImpl) IRCache(org.mapleir.context.IRCache) AnalysisContext(org.mapleir.context.AnalysisContext) BasicAnalysisContext(org.mapleir.context.BasicAnalysisContext) SimpleApplicationContext(org.mapleir.app.client.SimpleApplicationContext) IPass(org.mapleir.deob.IPass) PassGroup(org.mapleir.deob.PassGroup) ApplicationClassSource(org.mapleir.app.service.ApplicationClassSource) MethodNode(org.mapleir.asm.MethodNode) JarInfo(org.topdank.byteengineer.commons.data.JarInfo) ControlFlowGraphBuilder(org.mapleir.ir.cfg.builder.ControlFlowGraphBuilder) ControlFlowGraph(org.mapleir.ir.cfg.ControlFlowGraph) File(java.io.File) SingleJarDownloader(org.topdank.byteio.in.SingleJarDownloader)

Example 24 with ControlFlowGraph

use of org.mapleir.ir.cfg.ControlFlowGraph in project maple-ir by LLVM-but-worse.

the class ConcreteStaticInvocationPass method accept.

@Override
public PassResult accept(PassContext pcxt) {
    AnalysisContext cxt = pcxt.getAnalysis();
    int fixed = 0;
    InvocationResolver resolver = cxt.getInvocationResolver();
    for (ClassNode cn : cxt.getApplication().iterate()) {
        for (MethodNode mn : cn.getMethods()) {
            ControlFlowGraph cfg = cxt.getIRCache().getFor(mn);
            for (BasicBlock b : cfg.vertices()) {
                for (Stmt stmt : b) {
                    for (Expr e : stmt.enumerateOnlyChildren()) {
                        if (e.getOpcode() == Opcode.INVOKE) {
                            InvocationExpr invoke = (InvocationExpr) e;
                            if (invoke.getCallType() == InvocationExpr.CallType.STATIC) {
                                MethodNode invoked = resolver.resolveStaticCall(invoke.getOwner(), invoke.getName(), invoke.getDesc());
                                if (invoked != null) {
                                    if (!invoked.getOwner().equals(invoke.getOwner())) {
                                        invoke.setOwner(invoked.getOwner());
                                        fixed++;
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
    }
    System.out.printf("  corrected %d dodgy static calls.%n", fixed);
    return PassResult.with(pcxt, this).finished().make();
}
Also used : ClassNode(org.mapleir.asm.ClassNode) MethodNode(org.mapleir.asm.MethodNode) InvocationExpr(org.mapleir.ir.code.expr.invoke.InvocationExpr) Expr(org.mapleir.ir.code.Expr) ControlFlowGraph(org.mapleir.ir.cfg.ControlFlowGraph) InvocationResolver(org.mapleir.app.service.InvocationResolver) BasicBlock(org.mapleir.ir.cfg.BasicBlock) AnalysisContext(org.mapleir.context.AnalysisContext) InvocationExpr(org.mapleir.ir.code.expr.invoke.InvocationExpr) Stmt(org.mapleir.ir.code.Stmt)

Example 25 with ControlFlowGraph

use of org.mapleir.ir.cfg.ControlFlowGraph in project maple-ir by LLVM-but-worse.

the class DeadCodeEliminationPass method accept.

@Override
public PassResult accept(PassContext pcxt) {
    AnalysisContext cxt = pcxt.getAnalysis();
    deadBlocks = 0;
    immediateJumps = 0;
    deadLocals = 0;
    for (ClassNode cn : cxt.getApplication().iterate()) {
        for (MethodNode m : cn.getMethods()) {
            ControlFlowGraph cfg = cxt.getIRCache().getFor(m);
            /* dead blocks */
            process(cfg);
        }
    }
    System.out.printf("  removed %d dead blocks.%n", deadBlocks);
    System.out.printf("  converted %d immediate jumps.%n", immediateJumps);
    System.out.printf("  eliminated %d dead locals.%n", deadLocals);
    return PassResult.with(pcxt, this).finished(deadBlocks + immediateJumps).make();
}
Also used : ClassNode(org.mapleir.asm.ClassNode) MethodNode(org.mapleir.asm.MethodNode) ControlFlowGraph(org.mapleir.ir.cfg.ControlFlowGraph) AnalysisContext(org.mapleir.context.AnalysisContext)

Aggregations

ControlFlowGraph (org.mapleir.ir.cfg.ControlFlowGraph)30 MethodNode (org.mapleir.asm.MethodNode)17 BasicBlock (org.mapleir.ir.cfg.BasicBlock)14 Expr (org.mapleir.ir.code.Expr)14 ClassNode (org.mapleir.asm.ClassNode)13 Stmt (org.mapleir.ir.code.Stmt)13 AnalysisContext (org.mapleir.context.AnalysisContext)11 ApplicationClassSource (org.mapleir.app.service.ApplicationClassSource)9 InvocationResolver (org.mapleir.app.service.InvocationResolver)7 InvocationExpr (org.mapleir.ir.code.expr.invoke.InvocationExpr)7 FieldStoreStmt (org.mapleir.ir.code.stmt.FieldStoreStmt)7 MethodNode (org.objectweb.asm.tree.MethodNode)6 IRCache (org.mapleir.context.IRCache)5 ControlFlowGraphBuilder (org.mapleir.ir.cfg.builder.ControlFlowGraphBuilder)5 ConstantExpr (org.mapleir.ir.code.expr.ConstantExpr)5 FieldLoadExpr (org.mapleir.ir.code.expr.FieldLoadExpr)5 Type (org.objectweb.asm.Type)5 HashMap (java.util.HashMap)4 HashSet (java.util.HashSet)4 SimpleApplicationContext (org.mapleir.app.client.SimpleApplicationContext)4