Search in sources :

Example 16 with MethodNode

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

the class CleanBoot method main.

public static void main(String[] args) throws Exception {
    ClassNode cn = ClassHelper.create(new FileInputStream(new File("MemeIn.class")));
    IRCache irFactory = new IRCache();
    for (MethodNode mn : cn.getMethods()) {
        // if (!mn.getName().equals("merge"))
        // continue;
        // if (mn.getName().equals("merge"))
        // System.out.println(InsnListUtils.insnListToString(mn.node.instructions));
        ControlFlowGraph cfg = irFactory.getNonNull(mn);
        // if (mn.getName().equals("merge"))
        // System.out.println(cfg);
        // if (mn.getName().equals("merge"))
        // CFGUtils.easyDumpCFG(cfg, "pre-destruct");
        cfg.verify();
        BoissinotDestructor.leaveSSA(cfg);
        // if (mn.getName().equals("merge"))
        // CFGUtils.easyDumpCFG(cfg, "pre-reaalloc");
        LocalsReallocator.realloc(cfg);
        // if (mn.getName().equals("merge"))
        // 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.node.instructions));
    }
    new FileOutputStream(new File("Meme.class")).write(ClassHelper.toByteArray(cn, ClassWriter.COMPUTE_FRAMES));
}
Also used : ClassNode(org.mapleir.asm.ClassNode) ControlFlowGraphDumper(org.mapleir.ir.codegen.ControlFlowGraphDumper) MethodNode(org.mapleir.asm.MethodNode) ControlFlowGraph(org.mapleir.ir.cfg.ControlFlowGraph) FileOutputStream(java.io.FileOutputStream) IRCache(org.mapleir.context.IRCache) File(java.io.File) FileInputStream(java.io.FileInputStream)

Example 17 with MethodNode

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

the class CompilationDemo method main.

public static void main(String[] args) throws IOException {
    // File f = new File("res/Bad.jar");
    // SingleJarDownloader<ClassNode> dl = new SingleJarDownloader<>(new JarInfo(f));
    // dl.download();
    // for (ClassNode cn : dl.getJarContents().getClassContents().namedMap().values()) {
    String className = "HelloWorld";
    JavaClassCompiler compiler = new JavaClassCompiler();
    byte[] bytes = compiler.compile(className, "public class " + className + " { public static void main(String[] args) { System.out.println(\"Hello world\"); } }");
    if (bytes == null) {
        System.out.println("Compilation failed!");
    } else {
        ASMFactory cnFactory = new DefaultASMFactory();
        ClassNode cn = cnFactory.create(bytes, className);
        for (MethodNode mn : cn.getMethods()) {
            System.out.println(mn.getJavaDesc());
            ControlFlowGraphBuilder builder = new ControlFlowGraphBuilder(mn, false);
            ControlFlowGraph cfg = builder.buildImpl();
            System.out.println(cfg);
            BoissinotDestructor.leaveSSA(cfg);
            LocalsReallocator.realloc(cfg);
            System.out.println(cfg);
        }
    }
}
Also used : ClassNode(org.mapleir.asm.ClassNode) MethodNode(org.mapleir.asm.MethodNode) ControlFlowGraphBuilder(org.mapleir.ir.cfg.builder.ControlFlowGraphBuilder) ControlFlowGraph(org.mapleir.ir.cfg.ControlFlowGraph) DefaultASMFactory(org.topdank.byteengineer.commons.asm.DefaultASMFactory) JavaClassCompiler(org.mapleir.stdlib.util.JavaClassCompiler) DefaultASMFactory(org.topdank.byteengineer.commons.asm.DefaultASMFactory) ASMFactory(org.topdank.byteengineer.commons.asm.ASMFactory)

Example 18 with MethodNode

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

the class DefaultInvocationResolver method resolve.

public MethodNode resolve(ClassNode receiver, String name, String desc, boolean strict) {
    /*if(strict && receiver.isAbstract()) {
			throw new UnsupportedOperationException(String.format("Tried to call method on abstract receiver: %s.%s %s", receiver, name, desc));
		}*/
    Selector selector = new Selector(name, desc);
    if (!hasVisited(receiver)) {
        throw new UnsupportedOperationException(String.format("No table for %s", receiver));
    } else {
        Map<Selector, MethodNode> cvtable = concreteVTables.get(receiver);
        Map<Selector, MethodNode> avtable = abstractVTables.get(receiver);
        MethodNode cm = cvtable.get(selector);
        MethodNode am = avtable.get(selector);
        if (cm == null && am == null) {
            if (strict) {
                throw new NoSuchMethodError(receiver.getName() + "." + name + desc);
            }
        } else if (cm != null) {
            return cm;
        } else if (am != null) {
            if (strict) {
                throw new AbstractMethodError(receiver.getName() + "." + name + desc);
            } else {
                return am;
            }
        } else {
            if (strict) {
                throw new IllegalStateException(String.format("Multiple target sites %s and %s", cm, am));
            }
        }
    }
    return null;
}
Also used : MethodNode(org.mapleir.asm.MethodNode)

Example 19 with MethodNode

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

the class DefaultInvocationResolver method resolveVirtualInitCall.

@Override
public MethodNode resolveVirtualInitCall(String owner, String desc) {
    ClassNode cn = app.findClassNode(owner);
    if (!checkNullClass(cn, owner)) {
        return null;
    }
    MethodNode mn = resolve(cn, "<init>", desc, true);
    if (mn == null) {
        return null;
    } else if (!mn.owner.getName().equals(owner)) {
        throw new UnsupportedOperationException(mn.toString());
    } else {
        return mn;
    }
}
Also used : ClassNode(org.mapleir.asm.ClassNode) MethodNode(org.mapleir.asm.MethodNode)

Example 20 with MethodNode

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

the class HierarchyMethods method findMethods.

/**
 * Finds methods in cn matching name and desc.
 * @param cn ClassNode
 * @param name name of method
 * @param desc method descriptor
 * @param returnTypes One of ANY_TYPE, CONGRUENT_TYPES, EXACT_TYPES, or LOOSELY_RELATED_TYPES
 * @param allowedMask Mask of allowed attributes for modifiers; bit 1 = allowed, 0 = not allowed
 * @param requiredMask Mask of required attributes for modifiers; bit 1 = allowed, 0 = not allowed
 * @return Set of methods matching specifications
 */
private Set<MethodNode> findMethods(ClassNode cn, String name, String desc, int returnTypes, int allowedMask, int requiredMask) {
    allowedMask |= requiredMask;
    Set<MethodNode> findM = new HashSet<>();
    Type[] expectedParams = Type.getArgumentTypes(desc);
    for (MethodNode m : cn.getMethods()) {
        // no bits unset in m.access that are in requiredMask
        if (((m.node.access ^ allowedMask) & m.node.access) == 0 && ((m.node.access ^ requiredMask) & requiredMask) == 0) {
            if (!Modifier.isStatic(allowedMask) && Modifier.isStatic(m.node.access))
                throw new IllegalStateException("B0i");
            if (!Modifier.isAbstract(allowedMask) && Modifier.isAbstract(m.node.access))
                throw new IllegalStateException("B0i");
            if (!isBridge(allowedMask) && isBridge(m.node.access))
                throw new IllegalStateException("B0i");
            if (Modifier.isStatic(requiredMask) && !Modifier.isStatic(m.node.access))
                throw new IllegalStateException("B0i");
            if (!m.getName().equals(name) || !Arrays.equals(expectedParams, Type.getArgumentTypes(m.getDesc()))) {
                continue;
            }
            switch(returnTypes) {
                case ANY_TYPES:
                    break;
                case CONGRUENT_TYPES:
                    if (!areTypesCongruent(Type.getReturnType(desc), Type.getReturnType(m.getDesc()))) {
                        continue;
                    }
                    break;
                case EXACT_TYPES:
                    if (!desc.equals(m.getDesc())) {
                        continue;
                    }
                    break;
                case LOOSELY_RELATED_TYPES:
                    if (!areTypesLooselyRelated(Type.getReturnType(desc), Type.getReturnType(m.getDesc()))) {
                        continue;
                    }
                    break;
            }
            // sanity check
            if (returnTypes == EXACT_TYPES && !isBridge(allowedMask) && !findM.isEmpty()) {
                System.err.println("==findM==");
                debugCong(desc, findM.iterator().next().getDesc());
                System.err.println("==m==");
                debugCong(desc, m.getDesc());
                {
                    byte[] bs = ClassHelper.toByteArray(cn);
                    try {
                        FileOutputStream fos = new FileOutputStream(new File("out/broken.class"));
                        fos.write(bs);
                        fos.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
                throw new IllegalStateException(String.format("%s contains %s(br=%b) and %s(br=%b)", cn.getName(), findM, isBridge(findM.iterator().next().node.access), m, isBridge(m.node.access)));
            }
            findM.add(m);
        }
    }
    return findM;
}
Also used : Type(org.objectweb.asm.Type) MethodNode(org.mapleir.asm.MethodNode) FileOutputStream(java.io.FileOutputStream) IOException(java.io.IOException) File(java.io.File) HashSet(java.util.HashSet)

Aggregations

MethodNode (org.mapleir.asm.MethodNode)36 ClassNode (org.mapleir.asm.ClassNode)21 ControlFlowGraph (org.mapleir.ir.cfg.ControlFlowGraph)17 AnalysisContext (org.mapleir.context.AnalysisContext)13 ApplicationClassSource (org.mapleir.app.service.ApplicationClassSource)8 Expr (org.mapleir.ir.code.Expr)8 Stmt (org.mapleir.ir.code.Stmt)7 BasicBlock (org.mapleir.ir.cfg.BasicBlock)6 Type (org.objectweb.asm.Type)6 InvocationResolver (org.mapleir.app.service.InvocationResolver)5 IRCache (org.mapleir.context.IRCache)5 ControlFlowGraphBuilder (org.mapleir.ir.cfg.builder.ControlFlowGraphBuilder)5 InsnList (org.objectweb.asm.tree.InsnList)5 InsnNode (org.objectweb.asm.tree.InsnNode)5 JumpInsnNode (org.objectweb.asm.tree.JumpInsnNode)5 VarInsnNode (org.objectweb.asm.tree.VarInsnNode)5 HashSet (java.util.HashSet)4 SimpleApplicationContext (org.mapleir.app.client.SimpleApplicationContext)4 BasicAnalysisContext (org.mapleir.context.BasicAnalysisContext)4 IPass (org.mapleir.deob.IPass)4