Search in sources :

Example 21 with ClassNode

use of org.mapleir.asm.ClassNode in project maple-ir by LLVM-but-worse.

the class RunCommand method dumpJar.

private void dumpJar(ApplicationClassSource app, SingleJarDownloader<ClassNode> dl, PassGroup masterGroup, String outputFile) throws IOException {
    (new CompleteResolvingJarDumper(dl.getJarContents(), app) {

        @Override
        public int dumpResource(JarOutputStream out, String name, byte[] file) throws IOException {
            // }
            if (name.equals("META-INF/MANIFEST.MF")) {
                ClassRenamerPass renamer = (ClassRenamerPass) masterGroup.getPass(e -> e.is(ClassRenamerPass.class));
                if (renamer != null) {
                    ByteArrayOutputStream baos = new ByteArrayOutputStream();
                    BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(baos));
                    BufferedReader br = new BufferedReader(new InputStreamReader(new ByteArrayInputStream(file)));
                    String line;
                    while ((line = br.readLine()) != null) {
                        String[] parts = line.split(": ", 2);
                        if (parts.length != 2) {
                            bw.write(line);
                            continue;
                        }
                        if (parts[0].equals("Main-Class")) {
                            String newMain = renamer.getRemappedName(parts[1].replace(".", "/")).replace("/", ".");
                            logger.print(String.format("%s -> %s%n", parts[1], newMain));
                            parts[1] = newMain;
                        }
                        bw.write(parts[0]);
                        bw.write(": ");
                        bw.write(parts[1]);
                        bw.write(System.lineSeparator());
                    }
                    br.close();
                    bw.close();
                    file = baos.toByteArray();
                }
            }
            return super.dumpResource(out, name, file);
        }
    }).dump(new File(outputFile));
}
Also used : PassResult(org.mapleir.deob.PassResult) LiveDataFlowAnalysisImpl(org.mapleir.deob.dataflow.LiveDataFlowAnalysisImpl) java.util(java.util) CompleteResolvingJarDumper(org.mapleir.app.service.CompleteResolvingJarDumper) IRCache(org.mapleir.context.IRCache) Callable(java.util.concurrent.Callable) BoissinotDestructor(org.mapleir.ir.algorithms.BoissinotDestructor) ControlFlowGraph(org.mapleir.ir.cfg.ControlFlowGraph) Logger(org.apache.log4j.Logger) Main(org.mapleir.Main) JarInfo(org.topdank.byteengineer.commons.data.JarInfo) AnalysisContext(org.mapleir.context.AnalysisContext) SingleJarDownloader(org.topdank.byteio.in.SingleJarDownloader) IPass(org.mapleir.deob.IPass) JarOutputStream(java.util.jar.JarOutputStream) CommandLine(picocli.CommandLine) MethodNode(org.mapleir.asm.MethodNode) PassGroup(org.mapleir.deob.PassGroup) SimpleApplicationContext(org.mapleir.app.client.SimpleApplicationContext) ControlFlowGraphBuilder(org.mapleir.ir.cfg.builder.ControlFlowGraphBuilder) PassContext(org.mapleir.deob.PassContext) ApplicationClassSource(org.mapleir.app.service.ApplicationClassSource) BasicAnalysisContext(org.mapleir.context.BasicAnalysisContext) RenamingHeuristic(org.mapleir.deob.util.RenamingHeuristic) ControlFlowGraphDumper(org.mapleir.ir.codegen.ControlFlowGraphDumper) Boot(org.mapleir.Boot) LibraryClassSource(org.mapleir.app.service.LibraryClassSource) ClassRenamerPass(org.mapleir.deob.passes.rename.ClassRenamerPass) LocalsReallocator(org.mapleir.ir.algorithms.LocalsReallocator) java.io(java.io) CliLog(org.mapleir.cli.CliLog) ClassNode(org.mapleir.asm.ClassNode) DefaultInvocationResolver(org.mapleir.DefaultInvocationResolver) ExceptionFixerPass(org.mapleir.deob.passes.fixer.ExceptionFixerPass) JarOutputStream(java.util.jar.JarOutputStream) CompleteResolvingJarDumper(org.mapleir.app.service.CompleteResolvingJarDumper) ClassRenamerPass(org.mapleir.deob.passes.rename.ClassRenamerPass)

Example 22 with ClassNode

use of org.mapleir.asm.ClassNode in project maple-ir by LLVM-but-worse.

the class Boot method dumpJar.

private static void dumpJar(ApplicationClassSource app, SingleJarDownloader<ClassNode> dl, PassGroup masterGroup, String outputFile) throws IOException {
    (new CompleteResolvingJarDumper(dl.getJarContents(), app) {

        @Override
        public int dumpResource(JarOutputStream out, String name, byte[] file) throws IOException {
            // }
            if (name.equals("META-INF/MANIFEST.MF")) {
                ClassRenamerPass renamer = (ClassRenamerPass) masterGroup.getPass(e -> e.is(ClassRenamerPass.class));
                if (renamer != null) {
                    ByteArrayOutputStream baos = new ByteArrayOutputStream();
                    BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(baos));
                    BufferedReader br = new BufferedReader(new InputStreamReader(new ByteArrayInputStream(file)));
                    String line;
                    while ((line = br.readLine()) != null) {
                        String[] parts = line.split(": ", 2);
                        if (parts.length != 2) {
                            bw.write(line);
                            continue;
                        }
                        if (parts[0].equals("Main-Class")) {
                            String newMain = renamer.getRemappedName(parts[1].replace(".", "/")).replace("/", ".");
                            LOGGER.info(String.format("%s -> %s%n", parts[1], newMain));
                            parts[1] = newMain;
                        }
                        bw.write(parts[0]);
                        bw.write(": ");
                        bw.write(parts[1]);
                        bw.write(System.lineSeparator());
                    }
                    br.close();
                    bw.close();
                    file = baos.toByteArray();
                }
            }
            return super.dumpResource(out, name, file);
        }
    }).dump(new File(outputFile));
}
Also used : PassResult(org.mapleir.deob.PassResult) LiveDataFlowAnalysisImpl(org.mapleir.deob.dataflow.LiveDataFlowAnalysisImpl) java.util(java.util) URL(java.net.URL) CompleteResolvingJarDumper(org.mapleir.app.service.CompleteResolvingJarDumper) IRCache(org.mapleir.context.IRCache) BoissinotDestructor(org.mapleir.ir.algorithms.BoissinotDestructor) ControlFlowGraph(org.mapleir.ir.cfg.ControlFlowGraph) Logger(org.apache.log4j.Logger) JarInfo(org.topdank.byteengineer.commons.data.JarInfo) AnalysisContext(org.mapleir.context.AnalysisContext) ConstantExpressionEvaluatorPass(org.mapleir.deob.passes.constparam.ConstantExpressionEvaluatorPass) SingleJarDownloader(org.topdank.byteio.in.SingleJarDownloader) IPass(org.mapleir.deob.IPass) ManagementFactory(java.lang.management.ManagementFactory) URI(java.net.URI) JarOutputStream(java.util.jar.JarOutputStream) PassGroup(org.mapleir.deob.PassGroup) MethodNode(org.mapleir.asm.MethodNode) SimpleApplicationContext(org.mapleir.app.client.SimpleApplicationContext) ControlFlowGraphBuilder(org.mapleir.ir.cfg.builder.ControlFlowGraphBuilder) PassContext(org.mapleir.deob.PassContext) ApplicationClassSource(org.mapleir.app.service.ApplicationClassSource) BasicAnalysisContext(org.mapleir.context.BasicAnalysisContext) RenamingHeuristic(org.mapleir.deob.util.RenamingHeuristic) ControlFlowGraphDumper(org.mapleir.ir.codegen.ControlFlowGraphDumper) LibraryClassSource(org.mapleir.app.service.LibraryClassSource) ClassRenamerPass(org.mapleir.deob.passes.rename.ClassRenamerPass) LocalsReallocator(org.mapleir.ir.algorithms.LocalsReallocator) java.io(java.io) Entry(java.util.Map.Entry) ClassNode(org.mapleir.asm.ClassNode) org.mapleir.deob.passes(org.mapleir.deob.passes) JarOutputStream(java.util.jar.JarOutputStream) CompleteResolvingJarDumper(org.mapleir.app.service.CompleteResolvingJarDumper) ClassRenamerPass(org.mapleir.deob.passes.rename.ClassRenamerPass)

Example 23 with ClassNode

use of org.mapleir.asm.ClassNode in project maple-ir by LLVM-but-worse.

the class Boot 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(args[0]);
    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 InstalledRuntimeClassSource(app));
    File rtjar = new File(System.getProperty("java.home"), "lib/rt.jar");
    // File androidjar = new File("res/android.jar");
    app.addLibraries(rt(app, rtjar));
    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.getName().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();
        try {
            cfg.verify();
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }
    section("Retranslating SSA IR to standard flavour.");
    for (Entry<MethodNode, ControlFlowGraph> e : cxt.getIRCache().entrySet()) {
        MethodNode mn = e.getKey();
        // if (!mn.getName().equals("openFiles"))
        // continue;
        ControlFlowGraph cfg = e.getValue();
        // CFGUtils.easyDumpCFG(cfg, "pre-destruct");
        try {
            cfg.verify();
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        BoissinotDestructor.leaveSSA(cfg);
        // CFGUtils.easyDumpCFG(cfg, "pre-reaalloc");
        LocalsReallocator.realloc(cfg);
        // System.out.println(cfg);
        try {
            cfg.verify();
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        // System.out.println("Rewriting " + mn.getName());
        (new ControlFlowGraphDumper(cfg, mn)).dump();
    // System.out.println(InsnListUtils.insnListToString(mn.instructions));
    }
    section("Rewriting jar.");
    dumpJar(app, dl, masterGroup, "out/rewritten.jar");
    section("Finished.");
}
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) 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 24 with ClassNode

use of org.mapleir.asm.ClassNode in project maple-ir by LLVM-but-worse.

the class DataFlowDemoBoot method rt.

private static LibraryClassSource rt(ApplicationClassSource app, File rtjar) throws IOException {
    section("Loading " + rtjar.getName() + " from " + rtjar.getAbsolutePath());
    SingleJarDownloader<ClassNode> dl = new SingleJarDownloader<>(new JarInfo(rtjar));
    dl.download();
    return new LibraryClassSource(app, dl.getJarContents().getClassContents());
}
Also used : ClassNode(org.mapleir.asm.ClassNode) JarInfo(org.topdank.byteengineer.commons.data.JarInfo) LibraryClassSource(org.mapleir.app.service.LibraryClassSource) SingleJarDownloader(org.topdank.byteio.in.SingleJarDownloader)

Example 25 with ClassNode

use of org.mapleir.asm.ClassNode in project maple-ir by LLVM-but-worse.

the class Boot2 method dumpJar.

private static void dumpJar(ApplicationClassSource app, SingleJarDownloader<ClassNode> dl, PassGroup masterGroup, String outputFile) throws IOException {
    (new CompleteResolvingJarDumper(dl.getJarContents(), app) {

        @Override
        public int dumpResource(JarOutputStream out, String name, byte[] file) throws IOException {
            // }
            if (name.equals("META-INF/MANIFEST.MF")) {
                ClassRenamerPass renamer = (ClassRenamerPass) masterGroup.getPass(e -> e.is(ClassRenamerPass.class));
                if (renamer != null) {
                    ByteArrayOutputStream baos = new ByteArrayOutputStream();
                    BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(baos));
                    BufferedReader br = new BufferedReader(new InputStreamReader(new ByteArrayInputStream(file)));
                    String line;
                    while ((line = br.readLine()) != null) {
                        String[] parts = line.split(": ", 2);
                        if (parts.length != 2) {
                            bw.write(line);
                            continue;
                        }
                        if (parts[0].equals("Main-Class")) {
                            String newMain = renamer.getRemappedName(parts[1].replace(".", "/")).replace("/", ".");
                            LOGGER.info(String.format("%s -> %s%n", parts[1], newMain));
                            parts[1] = newMain;
                        }
                        bw.write(parts[0]);
                        bw.write(": ");
                        bw.write(parts[1]);
                        bw.write(System.lineSeparator());
                    }
                    br.close();
                    bw.close();
                    file = baos.toByteArray();
                }
            }
            return super.dumpResource(out, name, file);
        }
    }).dump(new File(outputFile));
}
Also used : PassResult(org.mapleir.deob.PassResult) LiveDataFlowAnalysisImpl(org.mapleir.deob.dataflow.LiveDataFlowAnalysisImpl) java.util(java.util) CompleteResolvingJarDumper(org.mapleir.app.service.CompleteResolvingJarDumper) IRCache(org.mapleir.context.IRCache) BoissinotDestructor(org.mapleir.ir.algorithms.BoissinotDestructor) ControlFlowGraph(org.mapleir.ir.cfg.ControlFlowGraph) MethodRenamerPass(org.mapleir.deob.passes.rename.MethodRenamerPass) Logger(org.apache.log4j.Logger) JarInfo(org.topdank.byteengineer.commons.data.JarInfo) AnalysisContext(org.mapleir.context.AnalysisContext) FieldRenamerPass(org.mapleir.deob.passes.rename.FieldRenamerPass) SingleJarDownloader(org.topdank.byteio.in.SingleJarDownloader) IPass(org.mapleir.deob.IPass) JarOutputStream(java.util.jar.JarOutputStream) PassGroup(org.mapleir.deob.PassGroup) MethodNode(org.mapleir.asm.MethodNode) SimpleApplicationContext(org.mapleir.app.client.SimpleApplicationContext) ControlFlowGraphBuilder(org.mapleir.ir.cfg.builder.ControlFlowGraphBuilder) PassContext(org.mapleir.deob.PassContext) ApplicationClassSource(org.mapleir.app.service.ApplicationClassSource) BasicAnalysisContext(org.mapleir.context.BasicAnalysisContext) RenamingHeuristic(org.mapleir.deob.util.RenamingHeuristic) ControlFlowGraphDumper(org.mapleir.ir.codegen.ControlFlowGraphDumper) LibraryClassSource(org.mapleir.app.service.LibraryClassSource) ClassRenamerPass(org.mapleir.deob.passes.rename.ClassRenamerPass) LocalsReallocator(org.mapleir.ir.algorithms.LocalsReallocator) java.io(java.io) Entry(java.util.Map.Entry) ClassNode(org.mapleir.asm.ClassNode) JarOutputStream(java.util.jar.JarOutputStream) CompleteResolvingJarDumper(org.mapleir.app.service.CompleteResolvingJarDumper) ClassRenamerPass(org.mapleir.deob.passes.rename.ClassRenamerPass)

Aggregations

ClassNode (org.mapleir.asm.ClassNode)45 MethodNode (org.mapleir.asm.MethodNode)24 ControlFlowGraph (org.mapleir.ir.cfg.ControlFlowGraph)16 AnalysisContext (org.mapleir.context.AnalysisContext)13 ApplicationClassSource (org.mapleir.app.service.ApplicationClassSource)11 JarInfo (org.topdank.byteengineer.commons.data.JarInfo)11 SingleJarDownloader (org.topdank.byteio.in.SingleJarDownloader)11 IRCache (org.mapleir.context.IRCache)8 ControlFlowGraphBuilder (org.mapleir.ir.cfg.builder.ControlFlowGraphBuilder)8 SimpleApplicationContext (org.mapleir.app.client.SimpleApplicationContext)7 LibraryClassSource (org.mapleir.app.service.LibraryClassSource)7 BasicAnalysisContext (org.mapleir.context.BasicAnalysisContext)7 IPass (org.mapleir.deob.IPass)7 PassGroup (org.mapleir.deob.PassGroup)7 LiveDataFlowAnalysisImpl (org.mapleir.deob.dataflow.LiveDataFlowAnalysisImpl)7 ControlFlowGraphDumper (org.mapleir.ir.codegen.ControlFlowGraphDumper)7 BasicBlock (org.mapleir.ir.cfg.BasicBlock)6 JarOutputStream (java.util.jar.JarOutputStream)5 Expr (org.mapleir.ir.code.Expr)5 Stmt (org.mapleir.ir.code.Stmt)5