Search in sources :

Example 16 with AbstractCopyStmt

use of org.mapleir.ir.code.stmt.copy.AbstractCopyStmt in project maple-ir by LLVM-but-worse.

the class ConstantParameterPass method demoteDeadParamters.

private void demoteDeadParamters(IPAnalysis constAnalysis, ControlFlowGraph cfg, MethodNode n, boolean[] dead) {
    LocalsPool pool = cfg.getLocals();
    BasicBlock entry = cfg.getEntries().iterator().next();
    for (int i = 0; i < dead.length; i++) {
        if (dead[i]) {
            int localIndex = constAnalysis.getLocalIndex(n, i);
            VersionedLocal local = pool.get(localIndex, 0, false);
            AbstractCopyStmt copy = pool.defs.get(local);
            if (copy.getBlock() != entry) {
                System.err.printf("entry:%n%s%n", ControlFlowGraph.printBlock(entry));
                System.err.printf("block:%n%s%n", ControlFlowGraph.printBlock(copy.getBlock()));
                throw new IllegalStateException(String.format("See debug trace (entry vs block) in %s", n));
            }
            copy.delete();
            if (pool.uses.get(local).size() != 0) {
                throw new IllegalStateException(String.format("m: %s, l:%s, uses:%s", n, local, pool.uses.get(local)));
            }
            pool.defs.remove(local);
            pool.uses.remove(local);
        }
    }
}
Also used : VersionedLocal(org.mapleir.ir.locals.impl.VersionedLocal) LocalsPool(org.mapleir.ir.locals.LocalsPool) BasicBlock(org.mapleir.ir.cfg.BasicBlock) AbstractCopyStmt(org.mapleir.ir.code.stmt.copy.AbstractCopyStmt)

Example 17 with AbstractCopyStmt

use of org.mapleir.ir.code.stmt.copy.AbstractCopyStmt 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();
}
Also used : FieldStoreStmt(org.mapleir.ir.code.stmt.FieldStoreStmt) Stmt(org.mapleir.ir.code.Stmt) AbstractCopyStmt(org.mapleir.ir.code.stmt.copy.AbstractCopyStmt) ReturnStmt(org.mapleir.ir.code.stmt.ReturnStmt) ApplicationClassSource(org.mapleir.app.service.ApplicationClassSource) MethodNode(org.objectweb.asm.tree.MethodNode) AbstractCopyStmt(org.mapleir.ir.code.stmt.copy.AbstractCopyStmt) FieldStoreStmt(org.mapleir.ir.code.stmt.FieldStoreStmt) ClassNode(org.objectweb.asm.tree.ClassNode) TryCatchBlockNode(org.objectweb.asm.tree.TryCatchBlockNode) FieldNode(org.objectweb.asm.tree.FieldNode) BasicBlock(org.mapleir.ir.cfg.BasicBlock) InitialisedObjectExpr(org.mapleir.ir.code.expr.invoke.InitialisedObjectExpr) LocalVariableNode(org.objectweb.asm.tree.LocalVariableNode) Type(org.objectweb.asm.Type) InitialisedObjectExpr(org.mapleir.ir.code.expr.invoke.InitialisedObjectExpr) InvocationExpr(org.mapleir.ir.code.expr.invoke.InvocationExpr) Expr(org.mapleir.ir.code.Expr) ControlFlowGraph(org.mapleir.ir.cfg.ControlFlowGraph) ReturnStmt(org.mapleir.ir.code.stmt.ReturnStmt) InvocationExpr(org.mapleir.ir.code.expr.invoke.InvocationExpr)

Example 18 with AbstractCopyStmt

use of org.mapleir.ir.code.stmt.copy.AbstractCopyStmt in project maple-ir by LLVM-but-worse.

the class SSADefUseMap method buildIndex.

protected void buildIndex(BasicBlock b, Stmt stmt, int index, Set<Local> usedLocals) {
    if (stmt instanceof AbstractCopyStmt) {
        AbstractCopyStmt copy = (AbstractCopyStmt) stmt;
        defIndex.put(copy.getVariable().getLocal(), index);
        if (copy instanceof CopyPhiStmt) {
            PhiExpr phi = ((CopyPhiStmt) copy).getExpression();
            for (Entry<BasicBlock, Expr> en : phi.getArguments().entrySet()) {
                lastUseIndex.getNonNull(((VarExpr) en.getValue()).getLocal()).put(en.getKey(), en.getKey().size());
            // lastUseIndex.get(ul).put(b, -1);
            }
            return;
        }
    }
    for (Local usedLocal : usedLocals) lastUseIndex.getNonNull(usedLocal).put(b, index);
}
Also used : VarExpr(org.mapleir.ir.code.expr.VarExpr) Expr(org.mapleir.ir.code.Expr) PhiExpr(org.mapleir.ir.code.expr.PhiExpr) PhiExpr(org.mapleir.ir.code.expr.PhiExpr) BasicBlock(org.mapleir.ir.cfg.BasicBlock) AbstractCopyStmt(org.mapleir.ir.code.stmt.copy.AbstractCopyStmt) VarExpr(org.mapleir.ir.code.expr.VarExpr) Local(org.mapleir.ir.locals.Local) CopyPhiStmt(org.mapleir.ir.code.stmt.copy.CopyPhiStmt)

Example 19 with AbstractCopyStmt

use of org.mapleir.ir.code.stmt.copy.AbstractCopyStmt in project maple-ir by LLVM-but-worse.

the class DefUseVerifier method verify0.

public static void verify0(ControlFlowGraph cfg) {
    LocalsPool lp = cfg.getLocals();
    Map<Local, AbstractCopyStmt> defs = new HashMap<>();
    NullPermeableHashMap<VersionedLocal, Set<VarExpr>> uses = new NullPermeableHashMap<>(SetCreator.getInstance());
    for (BasicBlock b : cfg.vertices()) {
        for (Stmt stmt : b) {
            if (stmt.getOpcode() == Opcode.LOCAL_STORE || stmt.getOpcode() == Opcode.PHI_STORE) {
                AbstractCopyStmt copy = (AbstractCopyStmt) stmt;
                defs.put(copy.getVariable().getLocal(), copy);
            }
            for (Expr e : stmt.enumerateOnlyChildren()) {
                if (e.getOpcode() == Opcode.LOCAL_LOAD) {
                    VarExpr v = (VarExpr) e;
                    uses.getNonNull((VersionedLocal) v.getLocal()).add(v);
                }
            }
        }
    }
    {
        Set<Local> dlocals = new HashSet<>();
        dlocals.addAll(defs.keySet());
        dlocals.addAll(lp.defs.keySet());
        for (Local l : dlocals) {
            if (!defs.containsKey(l)) {
                throw new IllegalStateException("(other): def of " + l);
            }
            if (!lp.defs.containsKey(l)) {
                throw new IllegalStateException("(real): def of " + l);
            }
            AbstractCopyStmt copy1 = defs.get(l);
            AbstractCopyStmt copy2 = lp.defs.get(l);
            if (copy1 != copy2) {
                throw new IllegalStateException("dtest: " + copy1 + " :: " + copy2);
            }
        }
    }
    {
        Set<VersionedLocal> ulocals = new HashSet<>();
        ulocals.addAll(uses.keySet());
        ulocals.addAll(lp.uses.keySet());
        for (VersionedLocal l : ulocals) {
            /*if(!uses.containsKey(l)) {
					throw new IllegalStateException("(other): use of " + l);
				}
				
				if(!lp.uses.containsKey(l)) {
					throw new IllegalStateException("(real): use of " + l);
				}*/
            Set<VarExpr> uses1 = uses.get(l);
            Set<VarExpr> uses2 = lp.uses.get(l);
            if (uses1 == null) {
                if (uses2.size() != 0) {
                    throw new IllegalStateException(String.format("utest1: %s, u1:null :: u2:%d", l, uses2.size()));
                }
            } else if (uses2 == null) {
                if (uses1.size() == 0) {
                    throw new IllegalStateException(String.format("utest2: %s, u1:%d :: u2:null", l, uses1.size()));
                }
            } else {
                if (uses2.size() != uses1.size()) {
                    throw new IllegalStateException(String.format("utest3: %s, u1:%d :: u2:%d", l, uses1.size(), uses2.size()));
                }
            }
        }
    }
}
Also used : VersionedLocal(org.mapleir.ir.locals.impl.VersionedLocal) Set(java.util.Set) HashSet(java.util.HashSet) HashMap(java.util.HashMap) NullPermeableHashMap(org.mapleir.stdlib.collections.map.NullPermeableHashMap) BasicBlock(org.mapleir.ir.cfg.BasicBlock) Local(org.mapleir.ir.locals.Local) VersionedLocal(org.mapleir.ir.locals.impl.VersionedLocal) Stmt(org.mapleir.ir.code.Stmt) AbstractCopyStmt(org.mapleir.ir.code.stmt.copy.AbstractCopyStmt) LocalsPool(org.mapleir.ir.locals.LocalsPool) VarExpr(org.mapleir.ir.code.expr.VarExpr) Expr(org.mapleir.ir.code.Expr) NullPermeableHashMap(org.mapleir.stdlib.collections.map.NullPermeableHashMap) AbstractCopyStmt(org.mapleir.ir.code.stmt.copy.AbstractCopyStmt) VarExpr(org.mapleir.ir.code.expr.VarExpr)

Example 20 with AbstractCopyStmt

use of org.mapleir.ir.code.stmt.copy.AbstractCopyStmt in project maple-ir by LLVM-but-worse.

the class SSAGenPass method getValue.

private Expr getValue(Expr e) {
    if (e.getOpcode() == Opcode.LOCAL_LOAD) {
        VarExpr v = (VarExpr) e;
        AbstractCopyStmt def = pool.defs.get(v.getLocal());
        Expr val = def.getExpression();
        if (!def.isSynthetic() && def.getOpcode() != Opcode.PHI_STORE) {
            return getValue(val);
        }
    }
    return e;
}
Also used : ConstantExpr(org.mapleir.ir.code.expr.ConstantExpr) InitialisedObjectExpr(org.mapleir.ir.code.expr.invoke.InitialisedObjectExpr) InvocationExpr(org.mapleir.ir.code.expr.invoke.InvocationExpr) VarExpr(org.mapleir.ir.code.expr.VarExpr) Expr(org.mapleir.ir.code.Expr) PhiExpr(org.mapleir.ir.code.expr.PhiExpr) VarExpr(org.mapleir.ir.code.expr.VarExpr) AbstractCopyStmt(org.mapleir.ir.code.stmt.copy.AbstractCopyStmt)

Aggregations

AbstractCopyStmt (org.mapleir.ir.code.stmt.copy.AbstractCopyStmt)20 VarExpr (org.mapleir.ir.code.expr.VarExpr)17 Expr (org.mapleir.ir.code.Expr)15 VersionedLocal (org.mapleir.ir.locals.impl.VersionedLocal)12 PhiExpr (org.mapleir.ir.code.expr.PhiExpr)11 Local (org.mapleir.ir.locals.Local)11 BasicBlock (org.mapleir.ir.cfg.BasicBlock)9 Stmt (org.mapleir.ir.code.Stmt)8 ConstantExpr (org.mapleir.ir.code.expr.ConstantExpr)8 InitialisedObjectExpr (org.mapleir.ir.code.expr.invoke.InitialisedObjectExpr)7 InvocationExpr (org.mapleir.ir.code.expr.invoke.InvocationExpr)7 CopyPhiStmt (org.mapleir.ir.code.stmt.copy.CopyPhiStmt)6 CopyVarStmt (org.mapleir.ir.code.stmt.copy.CopyVarStmt)5 HashSet (java.util.HashSet)4 CodeUnit (org.mapleir.ir.code.CodeUnit)4 LocalsPool (org.mapleir.ir.locals.LocalsPool)4 Constraint (org.mapleir.ir.cfg.builder.ssaopt.Constraint)3 LatestValue (org.mapleir.ir.cfg.builder.ssaopt.LatestValue)3 ConditionalJumpStmt (org.mapleir.ir.code.stmt.ConditionalJumpStmt)3 PopStmt (org.mapleir.ir.code.stmt.PopStmt)3