Search in sources :

Example 1 with ConstantExpr

use of org.mapleir.ir.code.expr.ConstantExpr in project maple-ir by LLVM-but-worse.

the class ConditionalJumpStmt method toCode.

@Override
public void toCode(MethodVisitor visitor, ControlFlowGraph cfg) {
    Type opType = TypeUtils.resolveBinOpType(left.getType(), right.getType());
    if (TypeUtils.isObjectRef(opType)) {
        boolean isNull = right instanceof ConstantExpr && ((ConstantExpr) right).getConstant() == null;
        if (type != ComparisonType.EQ && type != ComparisonType.NE) {
            throw new IllegalArgumentException(type.toString());
        }
        left.toCode(visitor, cfg);
        if (isNull) {
            visitor.visitJumpInsn(type == ComparisonType.EQ ? Opcodes.IFNULL : Opcodes.IFNONNULL, trueSuccessor.getLabel());
        } else {
            right.toCode(visitor, cfg);
            visitor.visitJumpInsn(type == ComparisonType.EQ ? Opcodes.IF_ACMPEQ : Opcodes.IF_ACMPNE, trueSuccessor.getLabel());
        }
    } else if (opType == Type.INT_TYPE) {
        boolean canShorten = right instanceof ConstantExpr && ((ConstantExpr) right).getConstant() instanceof Number && ((Number) ((ConstantExpr) right).getConstant()).intValue() == 0;
        left.toCode(visitor, cfg);
        int[] cast = TypeUtils.getPrimitiveCastOpcodes(left.getType(), opType);
        for (int i = 0; i < cast.length; i++) {
            visitor.visitInsn(cast[i]);
        }
        if (canShorten) {
            visitor.visitJumpInsn(Opcodes.IFEQ + type.ordinal(), trueSuccessor.getLabel());
        } else {
            right.toCode(visitor, cfg);
            cast = TypeUtils.getPrimitiveCastOpcodes(right.getType(), opType);
            for (int i = 0; i < cast.length; i++) {
                visitor.visitInsn(cast[i]);
            }
            visitor.visitJumpInsn(Opcodes.IF_ICMPEQ + type.ordinal(), trueSuccessor.getLabel());
        }
    } else if (opType == Type.LONG_TYPE) {
        left.toCode(visitor, cfg);
        int[] cast = TypeUtils.getPrimitiveCastOpcodes(left.getType(), opType);
        for (int i = 0; i < cast.length; i++) {
            visitor.visitInsn(cast[i]);
        }
        right.toCode(visitor, cfg);
        cast = TypeUtils.getPrimitiveCastOpcodes(right.getType(), opType);
        for (int i = 0; i < cast.length; i++) {
            visitor.visitInsn(cast[i]);
        }
        visitor.visitInsn(Opcodes.LCMP);
        visitor.visitJumpInsn(Opcodes.IFEQ + type.ordinal(), trueSuccessor.getLabel());
    } else if (opType == Type.FLOAT_TYPE) {
        left.toCode(visitor, cfg);
        int[] cast = TypeUtils.getPrimitiveCastOpcodes(left.getType(), opType);
        for (int i = 0; i < cast.length; i++) {
            visitor.visitInsn(cast[i]);
        }
        right.toCode(visitor, cfg);
        cast = TypeUtils.getPrimitiveCastOpcodes(right.getType(), opType);
        for (int i = 0; i < cast.length; i++) {
            visitor.visitInsn(cast[i]);
        }
        visitor.visitInsn((type == ComparisonType.LT || type == ComparisonType.LE) ? Opcodes.FCMPL : Opcodes.FCMPG);
        visitor.visitJumpInsn(Opcodes.IFEQ + type.ordinal(), trueSuccessor.getLabel());
    } else if (opType == Type.DOUBLE_TYPE) {
        left.toCode(visitor, cfg);
        int[] cast = TypeUtils.getPrimitiveCastOpcodes(left.getType(), opType);
        for (int i = 0; i < cast.length; i++) {
            visitor.visitInsn(cast[i]);
        }
        right.toCode(visitor, cfg);
        cast = TypeUtils.getPrimitiveCastOpcodes(right.getType(), opType);
        for (int i = 0; i < cast.length; i++) {
            visitor.visitInsn(cast[i]);
        }
        visitor.visitInsn((type == ComparisonType.LT || type == ComparisonType.LE) ? Opcodes.DCMPL : Opcodes.DCMPG);
        visitor.visitJumpInsn(Opcodes.IFEQ + type.ordinal(), trueSuccessor.getLabel());
    } else {
        throw new IllegalArgumentException(opType.toString());
    }
}
Also used : Type(org.objectweb.asm.Type) ConstantExpr(org.mapleir.ir.code.expr.ConstantExpr)

Example 2 with ConstantExpr

use of org.mapleir.ir.code.expr.ConstantExpr in project maple-ir by LLVM-but-worse.

the class ConstantParameterPass method inlineConstant.

private void inlineConstant(ControlFlowGraph cfg, int argLocalIndex, Object o) {
    /* we don't actually demote the synthetic copy
		 * here as we would also need to change the
		 * method desc and we can't do that until
		 * later so we defer it. */
    LocalsPool pool = cfg.getLocals();
    /* create the spill variable but not the
		 * actual definition yet. */
    VersionedLocal argLocal = pool.get(argLocalIndex, 0, false);
    VersionedLocal spill = pool.makeLatestVersion(argLocal);
    AbstractCopyStmt synthParamCopy = pool.defs.get(argLocal);
    ConstantExpr rhsVal = new ConstantExpr(o, synthParamCopy.getType() == Type.BOOLEAN_TYPE ? Type.BYTE_TYPE : synthParamCopy.getType());
    /* we have to maintain local references in
		 * phis as opposed to direct constant refs,
		 * so we go through every use of the argLocal
		 * and either replace it with the constant or
		 * a reference to the spill local if it is in
		 * a phi. */
    Set<VarExpr> spillUses = new HashSet<>();
    boolean requireSpill = false;
    Iterator<VarExpr> it = pool.uses.get(argLocal).iterator();
    while (it.hasNext()) {
        VarExpr v = it.next();
        if (v.getParent() == null) {
            /* the use is in a phi, we can't
				 * remove the def. 
				 * 
				 * we also replace the old var
				 * with the new spill one so we
				 * have to add this as a use of
				 * the new spill local. */
            spillUses.add(v);
            v.setLocal(spill);
            requireSpill = true;
        } else {
            CodeUnit par = v.getParent();
            par.overwrite(rhsVal.copy(), par.indexOf(v));
        }
        /* this use is no longer associated
			 * with the old argLocal. */
        it.remove();
    }
    if (pool.uses.get(argLocal).size() != 0) {
        throw new IllegalStateException(String.format("l:%s, uses:%s", argLocal, pool.uses.get(argLocal)));
    }
    if (requireSpill) {
        /* generate the copy for the spill (v = const) */
        CopyVarStmt spillCopy = new CopyVarStmt(new VarExpr(spill, synthParamCopy.getVariable().getType()), rhsVal);
        synthParamCopy.getBlock().add(spillCopy);
        /* initialise data entries for the new spill
			 * variable. */
        pool.defs.put(spill, spillCopy);
        pool.uses.put(spill, spillUses);
    }
}
Also used : VersionedLocal(org.mapleir.ir.locals.impl.VersionedLocal) LocalsPool(org.mapleir.ir.locals.LocalsPool) CopyVarStmt(org.mapleir.ir.code.stmt.copy.CopyVarStmt) ConstantExpr(org.mapleir.ir.code.expr.ConstantExpr) AbstractCopyStmt(org.mapleir.ir.code.stmt.copy.AbstractCopyStmt) VarExpr(org.mapleir.ir.code.expr.VarExpr) HashSet(java.util.HashSet) CodeUnit(org.mapleir.ir.code.CodeUnit)

Example 3 with ConstantExpr

use of org.mapleir.ir.code.expr.ConstantExpr in project maple-ir by LLVM-but-worse.

the class FieldRSADecryptionPass method handleFss.

private void handleFss(FieldStoreStmt fss) {
    if (!isIntField(fss.getDesc())) {
        return;
    }
    List<CodeUnit> list = getInsns(fss, new HashSet<>());
    boolean other = containsOther(list, key(fss));
    for (CodeUnit u : list) {
        if (u.getOpcode() == CONST_LOAD) {
            ConstantExpr c = (ConstantExpr) u;
            Object cst = c.getConstant();
            if (cst instanceof Integer || cst instanceof Long) {
                if (large((Number) cst, cst instanceof Long)) {
                    if (other) {
                        dangerConstants.getNonNull(key(fss)).add((Number) cst);
                    } else {
                        constants.getNonNull(key(fss)).add((Number) cst);
                    }
                }
            }
        }
    }
}
Also used : BigInteger(java.math.BigInteger) ConstantExpr(org.mapleir.ir.code.expr.ConstantExpr) CodeUnit(org.mapleir.ir.code.CodeUnit)

Example 4 with ConstantExpr

use of org.mapleir.ir.code.expr.ConstantExpr in project maple-ir by LLVM-but-worse.

the class FieldRSADecryptionPass method handleFle.

private void handleFle(Stmt stmt, FieldLoadExpr fle) {
    if (!isIntField(fle.getDesc())) {
        return;
    }
    List<CodeUnit> list = getInsns(fle, new HashSet<>());
    boolean other = containsOther(list, key(fle));
    for (CodeUnit u : list) {
        if (u.getOpcode() == CONST_LOAD) {
            ConstantExpr c = (ConstantExpr) u;
            Object cst = c.getConstant();
            if (cst instanceof Integer || cst instanceof Long) {
                if (large((Number) cst, cst instanceof Long)) {
                    if (other) {
                        dangerConstants.getNonNull(key(fle)).add((Number) cst);
                    } else {
                        constants.getNonNull(key(fle)).add((Number) cst);
                    }
                }
            }
        }
    }
}
Also used : BigInteger(java.math.BigInteger) ConstantExpr(org.mapleir.ir.code.expr.ConstantExpr) CodeUnit(org.mapleir.ir.code.CodeUnit)

Example 5 with ConstantExpr

use of org.mapleir.ir.code.expr.ConstantExpr in project maple-ir by LLVM-but-worse.

the class ExpressionEvaluator method evaluatePrimitiveConditional.

public Boolean evaluatePrimitiveConditional(ConditionalJumpStmt cond, TaintableSet<ConstantExpr> leftSet, TaintableSet<ConstantExpr> rightSet) {
    Boolean val = null;
    Iterator<Pair<ConstantExpr, ConstantExpr>> it = leftSet.product(rightSet);
    while (it.hasNext()) {
        Pair<ConstantExpr, ConstantExpr> lcrc = it.next();
        ConstantExpr lc = lcrc.getKey();
        ConstantExpr rc = lcrc.getValue();
        if (TypeUtils.isPrimitive(lc.getType()) && TypeUtils.isPrimitive(rc.getType())) {
            EvaluationFunctor<Boolean> bridge = factory.branch(lc.getType(), rc.getType(), cond.getComparisonType());
            /*System.out.println("eval: " + bridge.method + " " + lc.getConstant().getClass() + " " + rc.getConstant().getClass());
				System.out.println("   actual: " + lc.getType() + ", " +  rc.getType());
				System.out.println("      " + lc.getConstant() +"  " + rc.getConstant());*/
            boolean branchVal = bridge.eval(lc.getConstant(), rc.getConstant());
            if (val != null) {
                /* inconsistent branch results */
                if (val != branchVal) {
                    return null;
                }
            } else {
                val = branchVal;
            }
        } else {
            /*System.err.println("something::");
				System.err.println("  " + cond);
				System.err.println("  leftset: " + leftSet);
				System.err.println("  rightSet: " + rightSet);|
				return;*/
            throw new UnsupportedOperationException();
        }
    }
    return val;
}
Also used : ConstantExpr(org.mapleir.ir.code.expr.ConstantExpr) Pair(javafx.util.Pair)

Aggregations

ConstantExpr (org.mapleir.ir.code.expr.ConstantExpr)14 Expr (org.mapleir.ir.code.Expr)9 VarExpr (org.mapleir.ir.code.expr.VarExpr)8 ArithmeticExpr (org.mapleir.ir.code.expr.ArithmeticExpr)7 ControlFlowGraph (org.mapleir.ir.cfg.ControlFlowGraph)4 CodeUnit (org.mapleir.ir.code.CodeUnit)4 CastExpr (org.mapleir.ir.code.expr.CastExpr)4 ComparisonExpr (org.mapleir.ir.code.expr.ComparisonExpr)4 NegationExpr (org.mapleir.ir.code.expr.NegationExpr)4 Type (org.objectweb.asm.Type)4 BigInteger (java.math.BigInteger)3 HashSet (java.util.HashSet)3 MethodNode (org.objectweb.asm.tree.MethodNode)3 Pair (javafx.util.Pair)2 BasicBlock (org.mapleir.ir.cfg.BasicBlock)2 Stmt (org.mapleir.ir.code.Stmt)2 FieldLoadExpr (org.mapleir.ir.code.expr.FieldLoadExpr)2 InitialisedObjectExpr (org.mapleir.ir.code.expr.invoke.InitialisedObjectExpr)2 InvocationExpr (org.mapleir.ir.code.expr.invoke.InvocationExpr)2 FieldStoreStmt (org.mapleir.ir.code.stmt.FieldStoreStmt)2