Search in sources :

Example 16 with CmpgExpr

use of soot.jimple.CmpgExpr in project soot by Sable.

the class ConstraintChecker method caseAssignStmt.

@Override
public void caseAssignStmt(AssignStmt stmt) {
    final ClassHierarchy classHierarchy = ClassHierarchy.v();
    final Value l = stmt.getLeftOp();
    final Value r = stmt.getRightOp();
    TypeNode left = null;
    TypeNode right = null;
    if (l instanceof ArrayRef) {
        ArrayRef ref = (ArrayRef) l;
        Type baset = ((Local) ref.getBase()).getType();
        if (baset instanceof ArrayType) {
            ArrayType base = (ArrayType) baset;
            Value index = ref.getIndex();
            if ((base.numDimensions == 1) && (base.baseType instanceof IntegerType)) {
                left = classHierarchy.typeNode(base.baseType);
            }
            if (index instanceof Local) {
                if (!classHierarchy.typeNode(((Local) index).getType()).hasAncestor_1(classHierarchy.INT)) {
                    if (fix) {
                        ref.setIndex(insertCast((Local) index, IntType.v(), stmt));
                    } else {
                        error("Type Error(5)");
                    }
                }
            }
        }
    } else if (l instanceof Local) {
        Type ty = ((Local) l).getType();
        if (ty instanceof IntegerType) {
            left = classHierarchy.typeNode(ty);
        }
    } else if (l instanceof InstanceFieldRef) {
        Type ty = ((InstanceFieldRef) l).getFieldRef().type();
        if (ty instanceof IntegerType) {
            left = classHierarchy.typeNode(ty);
        }
    } else if (l instanceof StaticFieldRef) {
        Type ty = ((StaticFieldRef) l).getFieldRef().type();
        if (ty instanceof IntegerType) {
            left = classHierarchy.typeNode(ty);
        }
    } else {
        throw new RuntimeException("Unhandled assignment left hand side type: " + l.getClass());
    }
    if (r instanceof ArrayRef) {
        ArrayRef ref = (ArrayRef) r;
        Type baset = ((Local) ref.getBase()).getType();
        if (!(baset instanceof NullType)) {
            ArrayType base = (ArrayType) baset;
            Value index = ref.getIndex();
            if ((base.numDimensions == 1) && (base.baseType instanceof IntegerType)) {
                right = classHierarchy.typeNode(base.baseType);
            }
            if (index instanceof Local) {
                if (!classHierarchy.typeNode(((Local) index).getType()).hasAncestor_1(classHierarchy.INT)) {
                    if (fix) {
                        ref.setIndex(insertCast((Local) index, IntType.v(), stmt));
                    } else {
                        error("Type Error(6)");
                    }
                }
            }
        }
    } else if (r instanceof DoubleConstant) {
    } else if (r instanceof FloatConstant) {
    } else if (r instanceof IntConstant) {
        int value = ((IntConstant) r).value;
        if (value < -32768) {
            right = classHierarchy.INT;
        } else if (value < -128) {
            right = classHierarchy.SHORT;
        } else if (value < 0) {
            right = classHierarchy.BYTE;
        } else if (value < 2) {
            right = classHierarchy.R0_1;
        } else if (value < 128) {
            right = classHierarchy.R0_127;
        } else if (value < 32768) {
            right = classHierarchy.R0_32767;
        } else if (value < 65536) {
            right = classHierarchy.CHAR;
        } else {
            right = classHierarchy.INT;
        }
    } else if (r instanceof LongConstant) {
    } else if (r instanceof NullConstant) {
    } else if (r instanceof StringConstant) {
    } else if (r instanceof ClassConstant) {
    } else if (r instanceof BinopExpr) {
        // ******** BINOP EXPR ********
        BinopExpr be = (BinopExpr) r;
        Value lv = be.getOp1();
        Value rv = be.getOp2();
        TypeNode lop = null;
        TypeNode rop = null;
        // ******** LEFT ********
        if (lv instanceof Local) {
            if (((Local) lv).getType() instanceof IntegerType) {
                lop = classHierarchy.typeNode(((Local) lv).getType());
            }
        } else if (lv instanceof DoubleConstant) {
        } else if (lv instanceof FloatConstant) {
        } else if (lv instanceof IntConstant) {
            int value = ((IntConstant) lv).value;
            if (value < -32768) {
                lop = classHierarchy.INT;
            } else if (value < -128) {
                lop = classHierarchy.SHORT;
            } else if (value < 0) {
                lop = classHierarchy.BYTE;
            } else if (value < 2) {
                lop = classHierarchy.R0_1;
            } else if (value < 128) {
                lop = classHierarchy.R0_127;
            } else if (value < 32768) {
                lop = classHierarchy.R0_32767;
            } else if (value < 65536) {
                lop = classHierarchy.CHAR;
            } else {
                lop = classHierarchy.INT;
            }
        } else if (lv instanceof LongConstant) {
        } else if (lv instanceof NullConstant) {
        } else if (lv instanceof StringConstant) {
        } else if (lv instanceof ClassConstant) {
        } else {
            throw new RuntimeException("Unhandled binary expression left operand type: " + lv.getClass());
        }
        // ******** RIGHT ********
        if (rv instanceof Local) {
            if (((Local) rv).getType() instanceof IntegerType) {
                rop = classHierarchy.typeNode(((Local) rv).getType());
            }
        } else if (rv instanceof DoubleConstant) {
        } else if (rv instanceof FloatConstant) {
        } else if (rv instanceof IntConstant) {
            int value = ((IntConstant) rv).value;
            if (value < -32768) {
                rop = classHierarchy.INT;
            } else if (value < -128) {
                rop = classHierarchy.SHORT;
            } else if (value < 0) {
                rop = classHierarchy.BYTE;
            } else if (value < 2) {
                rop = classHierarchy.R0_1;
            } else if (value < 128) {
                rop = classHierarchy.R0_127;
            } else if (value < 32768) {
                rop = classHierarchy.R0_32767;
            } else if (value < 65536) {
                rop = classHierarchy.CHAR;
            } else {
                rop = classHierarchy.INT;
            }
        } else if (rv instanceof LongConstant) {
        } else if (rv instanceof NullConstant) {
        } else if (rv instanceof StringConstant) {
        } else if (rv instanceof ClassConstant) {
        } else {
            throw new RuntimeException("Unhandled binary expression right operand type: " + rv.getClass());
        }
        if ((be instanceof AddExpr) || (be instanceof SubExpr) || (be instanceof MulExpr) || (be instanceof DivExpr) || (be instanceof RemExpr)) {
            if (lop != null && rop != null) {
                if (!lop.hasAncestor_1(classHierarchy.INT)) {
                    if (fix) {
                        be.setOp1(insertCast(be.getOp1(), getTypeForCast(lop), IntType.v(), stmt));
                    } else {
                        error("Type Error(7)");
                    }
                }
                if (!rop.hasAncestor_1(classHierarchy.INT)) {
                    if (fix) {
                        be.setOp2(insertCast(be.getOp2(), getTypeForCast(rop), IntType.v(), stmt));
                    } else {
                        error("Type Error(8)");
                    }
                }
            }
            right = classHierarchy.INT;
        } else if ((be instanceof AndExpr) || (be instanceof OrExpr) || (be instanceof XorExpr)) {
            if (lop != null && rop != null) {
                TypeNode lca = lop.lca_1(rop);
                if (lca == classHierarchy.TOP) {
                    if (fix) {
                        if (!lop.hasAncestor_1(classHierarchy.INT)) {
                            be.setOp1(insertCast(be.getOp1(), getTypeForCast(lop), getTypeForCast(rop), stmt));
                            lca = rop;
                        }
                        if (!rop.hasAncestor_1(classHierarchy.INT)) {
                            be.setOp2(insertCast(be.getOp2(), getTypeForCast(rop), getTypeForCast(lop), stmt));
                            lca = lop;
                        }
                    } else {
                        error("Type Error(11)");
                    }
                }
                right = lca;
            }
        } else if (be instanceof ShlExpr) {
            if (lop != null) {
                if (!lop.hasAncestor_1(classHierarchy.INT)) {
                    if (fix) {
                        be.setOp1(insertCast(be.getOp1(), getTypeForCast(lop), IntType.v(), stmt));
                    } else {
                        error("Type Error(9)");
                    }
                }
            }
            if (!rop.hasAncestor_1(classHierarchy.INT)) {
                if (fix) {
                    be.setOp2(insertCast(be.getOp2(), getTypeForCast(rop), IntType.v(), stmt));
                } else {
                    error("Type Error(10)");
                }
            }
            right = (lop == null) ? null : classHierarchy.INT;
        } else if ((be instanceof ShrExpr) || (be instanceof UshrExpr)) {
            if (lop != null) {
                if (!lop.hasAncestor_1(classHierarchy.INT)) {
                    if (fix) {
                        be.setOp1(insertCast(be.getOp1(), getTypeForCast(lop), ByteType.v(), stmt));
                        lop = classHierarchy.BYTE;
                    } else {
                        error("Type Error(9)");
                    }
                }
            }
            if (!rop.hasAncestor_1(classHierarchy.INT)) {
                if (fix) {
                    be.setOp2(insertCast(be.getOp2(), getTypeForCast(rop), IntType.v(), stmt));
                } else {
                    error("Type Error(10)");
                }
            }
            right = lop;
        } else if ((be instanceof CmpExpr) || (be instanceof CmpgExpr) || (be instanceof CmplExpr)) {
            right = classHierarchy.BYTE;
        } else if ((be instanceof EqExpr) || (be instanceof GeExpr) || (be instanceof GtExpr) || (be instanceof LeExpr) || (be instanceof LtExpr) || (be instanceof NeExpr)) {
            if (rop != null) {
                TypeNode lca = lop.lca_1(rop);
                if (lca == classHierarchy.TOP) {
                    if (fix) {
                        if (!lop.hasAncestor_1(classHierarchy.INT)) {
                            be.setOp1(insertCast(be.getOp1(), getTypeForCast(lop), getTypeForCast(rop), stmt));
                        }
                        if (!rop.hasAncestor_1(classHierarchy.INT)) {
                            be.setOp2(insertCast(be.getOp2(), getTypeForCast(rop), getTypeForCast(lop), stmt));
                        }
                    } else {
                        error("Type Error(11)");
                    }
                }
            }
            right = classHierarchy.BOOLEAN;
        } else {
            throw new RuntimeException("Unhandled binary expression type: " + be.getClass());
        }
    } else if (r instanceof CastExpr) {
        Type ty = ((CastExpr) r).getCastType();
        if (ty instanceof IntegerType) {
            right = classHierarchy.typeNode(ty);
        }
    } else if (r instanceof InstanceOfExpr) {
        right = classHierarchy.BOOLEAN;
    } else if (r instanceof InvokeExpr) {
        InvokeExpr ie = (InvokeExpr) r;
        handleInvokeExpr(ie, stmt);
        Type retTy = ie.getMethodRef().getReturnType();
        if (retTy instanceof IntegerType) {
            right = classHierarchy.typeNode(retTy);
        }
    } else if (r instanceof NewArrayExpr) {
        NewArrayExpr nae = (NewArrayExpr) r;
        Value size = nae.getSize();
        if (size instanceof Local) {
            if (!classHierarchy.typeNode(((Local) size).getType()).hasAncestor_1(classHierarchy.INT)) {
                if (fix) {
                    nae.setSize(insertCast((Local) size, IntType.v(), stmt));
                } else {
                    error("Type Error(12)");
                }
            }
        }
    } else if (r instanceof NewExpr) {
    } else if (r instanceof NewMultiArrayExpr) {
        NewMultiArrayExpr nmae = (NewMultiArrayExpr) r;
        for (int i = 0; i < nmae.getSizeCount(); i++) {
            Value size = nmae.getSize(i);
            if (size instanceof Local) {
                if (!classHierarchy.typeNode(((Local) size).getType()).hasAncestor_1(classHierarchy.INT)) {
                    if (fix) {
                        nmae.setSize(i, insertCast((Local) size, IntType.v(), stmt));
                    } else {
                        error("Type Error(13)");
                    }
                }
            }
        }
    } else if (r instanceof LengthExpr) {
        right = classHierarchy.INT;
    } else if (r instanceof NegExpr) {
        NegExpr ne = (NegExpr) r;
        Value op = ne.getOp();
        if (op instanceof Local) {
            Local local = (Local) op;
            if (local.getType() instanceof IntegerType) {
                TypeNode ltype = classHierarchy.typeNode(local.getType());
                if (!ltype.hasAncestor_1(classHierarchy.INT)) {
                    if (fix) {
                        ne.setOp(insertCast(local, IntType.v(), stmt));
                        ltype = classHierarchy.BYTE;
                    } else {
                        error("Type Error(14)");
                    }
                }
                right = (ltype == classHierarchy.CHAR) ? classHierarchy.INT : ltype;
            }
        } else if (op instanceof DoubleConstant) {
        } else if (op instanceof FloatConstant) {
        } else if (op instanceof IntConstant) {
            right = classHierarchy.INT;
        } else if (op instanceof LongConstant) {
        } else {
            throw new RuntimeException("Unhandled neg expression operand type: " + op.getClass());
        }
    } else if (r instanceof Local) {
        Type ty = ((Local) r).getType();
        if (ty instanceof IntegerType) {
            right = classHierarchy.typeNode(ty);
        }
    } else if (r instanceof InstanceFieldRef) {
        Type ty = ((InstanceFieldRef) r).getFieldRef().type();
        if (ty instanceof IntegerType) {
            right = classHierarchy.typeNode(ty);
        }
    } else if (r instanceof StaticFieldRef) {
        Type ty = ((StaticFieldRef) r).getFieldRef().type();
        if (ty instanceof IntegerType) {
            right = classHierarchy.typeNode(ty);
        }
    } else {
        throw new RuntimeException("Unhandled assignment right hand side type: " + r.getClass());
    }
    if (left != null && right != null) {
        if (!right.hasAncestor_1(left)) {
            if (fix) {
                stmt.setRightOp(insertCast(stmt.getRightOp(), getTypeForCast(right), getTypeForCast(left), stmt));
            } else {
                error("Type Error(15)");
            }
        }
    }
}
Also used : AndExpr(soot.jimple.AndExpr) GtExpr(soot.jimple.GtExpr) LtExpr(soot.jimple.LtExpr) ArrayType(soot.ArrayType) DynamicInvokeExpr(soot.jimple.DynamicInvokeExpr) InvokeExpr(soot.jimple.InvokeExpr) CastExpr(soot.jimple.CastExpr) ShlExpr(soot.jimple.ShlExpr) XorExpr(soot.jimple.XorExpr) NeExpr(soot.jimple.NeExpr) Local(soot.Local) AddExpr(soot.jimple.AddExpr) InstanceOfExpr(soot.jimple.InstanceOfExpr) OrExpr(soot.jimple.OrExpr) StaticFieldRef(soot.jimple.StaticFieldRef) NewArrayExpr(soot.jimple.NewArrayExpr) RemExpr(soot.jimple.RemExpr) ShrExpr(soot.jimple.ShrExpr) CmpExpr(soot.jimple.CmpExpr) Value(soot.Value) NewExpr(soot.jimple.NewExpr) NullType(soot.NullType) StringConstant(soot.jimple.StringConstant) CmplExpr(soot.jimple.CmplExpr) ClassConstant(soot.jimple.ClassConstant) BinopExpr(soot.jimple.BinopExpr) MulExpr(soot.jimple.MulExpr) DoubleConstant(soot.jimple.DoubleConstant) NewMultiArrayExpr(soot.jimple.NewMultiArrayExpr) FloatConstant(soot.jimple.FloatConstant) NegExpr(soot.jimple.NegExpr) GeExpr(soot.jimple.GeExpr) UshrExpr(soot.jimple.UshrExpr) LeExpr(soot.jimple.LeExpr) ArrayRef(soot.jimple.ArrayRef) InstanceFieldRef(soot.jimple.InstanceFieldRef) IntConstant(soot.jimple.IntConstant) LongConstant(soot.jimple.LongConstant) LengthExpr(soot.jimple.LengthExpr) SubExpr(soot.jimple.SubExpr) NullConstant(soot.jimple.NullConstant) IntegerType(soot.IntegerType) ShortType(soot.ShortType) BooleanType(soot.BooleanType) ByteType(soot.ByteType) Type(soot.Type) IntType(soot.IntType) NullType(soot.NullType) ArrayType(soot.ArrayType) IntegerType(soot.IntegerType) DivExpr(soot.jimple.DivExpr) EqExpr(soot.jimple.EqExpr) CmpgExpr(soot.jimple.CmpgExpr)

Example 17 with CmpgExpr

use of soot.jimple.CmpgExpr in project soot by Sable.

the class Evaluator method getConstantValueOf.

// isValueConstantValued
/**
 * Returns the constant value of <code>op</code> if it is easy to find the constant value; else returns <code>null</code>.
 */
public static Value getConstantValueOf(Value op) {
    if (!isValueConstantValued(op)) {
        return null;
    }
    if (op instanceof Constant) {
        return op;
    } else if (op instanceof UnopExpr) {
        Value c = getConstantValueOf(((UnopExpr) op).getOp());
        if (op instanceof NegExpr) {
            return ((NumericConstant) c).negate();
        }
    } else if (op instanceof BinopExpr) {
        final BinopExpr binExpr = (BinopExpr) op;
        final Value c1 = getConstantValueOf(binExpr.getOp1());
        final Value c2 = getConstantValueOf(binExpr.getOp2());
        if (op instanceof AddExpr) {
            return ((NumericConstant) c1).add((NumericConstant) c2);
        } else if (op instanceof SubExpr) {
            return ((NumericConstant) c1).subtract((NumericConstant) c2);
        } else if (op instanceof MulExpr) {
            return ((NumericConstant) c1).multiply((NumericConstant) c2);
        } else if (op instanceof DivExpr) {
            return ((NumericConstant) c1).divide((NumericConstant) c2);
        } else if (op instanceof RemExpr) {
            return ((NumericConstant) c1).remainder((NumericConstant) c2);
        } else if (op instanceof EqExpr || op instanceof NeExpr) {
            if (c1 instanceof NumericConstant) {
                if (!(c2 instanceof NumericConstant)) {
                    return IntConstant.v(0);
                } else if (op instanceof EqExpr) {
                    return ((NumericConstant) c1).equalEqual((NumericConstant) c2);
                } else if (op instanceof NeExpr) {
                    return ((NumericConstant) c1).notEqual((NumericConstant) c2);
                }
            } else if (c1 instanceof StringConstant || c1 instanceof NullConstant || c1 instanceof ClassConstant) {
                boolean equality = c1.equals(c2);
                boolean truth = (op instanceof EqExpr) ? equality : !equality;
                return IntConstant.v(truth ? 1 : 0);
            }
            throw new RuntimeException("constant neither numeric nor string");
        } else if (op instanceof GtExpr) {
            return ((NumericConstant) c1).greaterThan((NumericConstant) c2);
        } else if (op instanceof GeExpr) {
            return ((NumericConstant) c1).greaterThanOrEqual((NumericConstant) c2);
        } else if (op instanceof LtExpr) {
            return ((NumericConstant) c1).lessThan((NumericConstant) c2);
        } else if (op instanceof LeExpr) {
            return ((NumericConstant) c1).lessThanOrEqual((NumericConstant) c2);
        } else if (op instanceof AndExpr) {
            return ((ArithmeticConstant) c1).and((ArithmeticConstant) c2);
        } else if (op instanceof OrExpr) {
            return ((ArithmeticConstant) c1).or((ArithmeticConstant) c2);
        } else if (op instanceof XorExpr) {
            return ((ArithmeticConstant) c1).xor((ArithmeticConstant) c2);
        } else if (op instanceof ShlExpr) {
            return ((ArithmeticConstant) c1).shiftLeft((ArithmeticConstant) c2);
        } else if (op instanceof ShrExpr) {
            return ((ArithmeticConstant) c1).shiftRight((ArithmeticConstant) c2);
        } else if (op instanceof UshrExpr) {
            return ((ArithmeticConstant) c1).unsignedShiftRight((ArithmeticConstant) c2);
        } else if (op instanceof CmpExpr) {
            if ((c1 instanceof LongConstant) && (c2 instanceof LongConstant)) {
                return ((LongConstant) c1).cmp((LongConstant) c2);
            } else {
                throw new IllegalArgumentException("CmpExpr: LongConstant(s) expected");
            }
        } else if ((op instanceof CmpgExpr) || (op instanceof CmplExpr)) {
            if ((c1 instanceof RealConstant) && (c2 instanceof RealConstant)) {
                if (op instanceof CmpgExpr) {
                    return ((RealConstant) c1).cmpg((RealConstant) c2);
                } else if (op instanceof CmplExpr) {
                    return ((RealConstant) c1).cmpl((RealConstant) c2);
                }
            } else {
                throw new IllegalArgumentException("CmpExpr: RealConstant(s) expected");
            }
        } else {
            throw new RuntimeException("unknown binop: " + op);
        }
    }
    throw new RuntimeException("couldn't getConstantValueOf of: " + op);
}
Also used : MulExpr(soot.jimple.MulExpr) AndExpr(soot.jimple.AndExpr) RealConstant(soot.jimple.RealConstant) ClassConstant(soot.jimple.ClassConstant) NullConstant(soot.jimple.NullConstant) ArithmeticConstant(soot.jimple.ArithmeticConstant) Constant(soot.jimple.Constant) LongConstant(soot.jimple.LongConstant) IntConstant(soot.jimple.IntConstant) NumericConstant(soot.jimple.NumericConstant) StringConstant(soot.jimple.StringConstant) UnopExpr(soot.jimple.UnopExpr) NegExpr(soot.jimple.NegExpr) GtExpr(soot.jimple.GtExpr) LtExpr(soot.jimple.LtExpr) GeExpr(soot.jimple.GeExpr) UshrExpr(soot.jimple.UshrExpr) LeExpr(soot.jimple.LeExpr) RealConstant(soot.jimple.RealConstant) ShlExpr(soot.jimple.ShlExpr) LongConstant(soot.jimple.LongConstant) XorExpr(soot.jimple.XorExpr) NeExpr(soot.jimple.NeExpr) SubExpr(soot.jimple.SubExpr) NullConstant(soot.jimple.NullConstant) AddExpr(soot.jimple.AddExpr) ArithmeticConstant(soot.jimple.ArithmeticConstant) OrExpr(soot.jimple.OrExpr) DivExpr(soot.jimple.DivExpr) NumericConstant(soot.jimple.NumericConstant) RemExpr(soot.jimple.RemExpr) EqExpr(soot.jimple.EqExpr) ShrExpr(soot.jimple.ShrExpr) CmpExpr(soot.jimple.CmpExpr) CmpgExpr(soot.jimple.CmpgExpr) Value(soot.Value) StringConstant(soot.jimple.StringConstant) CmplExpr(soot.jimple.CmplExpr) ClassConstant(soot.jimple.ClassConstant) BinopExpr(soot.jimple.BinopExpr)

Example 18 with CmpgExpr

use of soot.jimple.CmpgExpr in project soot by Sable.

the class DavaBody method javafy_binop_expr.

private void javafy_binop_expr(ValueBox vb) {
    BinopExpr boe = (BinopExpr) vb.getValue();
    ValueBox leftOpBox = boe.getOp1Box(), rightOpBox = boe.getOp2Box();
    Value leftOp = leftOpBox.getValue(), rightOp = rightOpBox.getValue();
    if (rightOp instanceof IntConstant) {
        if (!(leftOp instanceof IntConstant)) {
            javafy(leftOpBox);
            leftOp = leftOpBox.getValue();
            if (boe instanceof ConditionExpr) {
                rightOpBox.setValue(DIntConstant.v(((IntConstant) rightOp).value, leftOp.getType()));
            } else {
                rightOpBox.setValue(DIntConstant.v(((IntConstant) rightOp).value, null));
            }
        }
    } else if (leftOp instanceof IntConstant) {
        javafy(rightOpBox);
        rightOp = rightOpBox.getValue();
        if (boe instanceof ConditionExpr) {
            leftOpBox.setValue(DIntConstant.v(((IntConstant) leftOp).value, rightOp.getType()));
        } else {
            leftOpBox.setValue(DIntConstant.v(((IntConstant) leftOp).value, null));
        }
    } else {
        javafy(rightOpBox);
        rightOp = rightOpBox.getValue();
        javafy(leftOpBox);
        leftOp = leftOpBox.getValue();
    }
    if (boe instanceof CmpExpr) {
        vb.setValue(new DCmpExpr(leftOp, rightOp));
    } else if (boe instanceof CmplExpr) {
        vb.setValue(new DCmplExpr(leftOp, rightOp));
    } else if (boe instanceof CmpgExpr) {
        vb.setValue(new DCmpgExpr(leftOp, rightOp));
    }
}
Also used : DCmpgExpr(soot.dava.internal.javaRep.DCmpgExpr) ValueBox(soot.ValueBox) DCmpExpr(soot.dava.internal.javaRep.DCmpExpr) CmpExpr(soot.jimple.CmpExpr) ConditionExpr(soot.jimple.ConditionExpr) DCmpgExpr(soot.dava.internal.javaRep.DCmpgExpr) CmpgExpr(soot.jimple.CmpgExpr) Value(soot.Value) IntConstant(soot.jimple.IntConstant) DIntConstant(soot.dava.internal.javaRep.DIntConstant) DCmpExpr(soot.dava.internal.javaRep.DCmpExpr) DCmplExpr(soot.dava.internal.javaRep.DCmplExpr) DCmplExpr(soot.dava.internal.javaRep.DCmplExpr) CmplExpr(soot.jimple.CmplExpr) BinopExpr(soot.jimple.BinopExpr)

Example 19 with CmpgExpr

use of soot.jimple.CmpgExpr in project soot by soot-oss.

the class DavaBody method javafy_binop_expr.

private void javafy_binop_expr(ValueBox vb) {
    BinopExpr boe = (BinopExpr) vb.getValue();
    ValueBox leftOpBox = boe.getOp1Box(), rightOpBox = boe.getOp2Box();
    Value leftOp = leftOpBox.getValue(), rightOp = rightOpBox.getValue();
    if (rightOp instanceof IntConstant) {
        if (!(leftOp instanceof IntConstant)) {
            javafy(leftOpBox);
            leftOp = leftOpBox.getValue();
            if (boe instanceof ConditionExpr) {
                rightOpBox.setValue(DIntConstant.v(((IntConstant) rightOp).value, leftOp.getType()));
            } else {
                rightOpBox.setValue(DIntConstant.v(((IntConstant) rightOp).value, null));
            }
        }
    } else if (leftOp instanceof IntConstant) {
        javafy(rightOpBox);
        rightOp = rightOpBox.getValue();
        if (boe instanceof ConditionExpr) {
            leftOpBox.setValue(DIntConstant.v(((IntConstant) leftOp).value, rightOp.getType()));
        } else {
            leftOpBox.setValue(DIntConstant.v(((IntConstant) leftOp).value, null));
        }
    } else {
        javafy(rightOpBox);
        rightOp = rightOpBox.getValue();
        javafy(leftOpBox);
        leftOp = leftOpBox.getValue();
    }
    if (boe instanceof CmpExpr) {
        vb.setValue(new DCmpExpr(leftOp, rightOp));
    } else if (boe instanceof CmplExpr) {
        vb.setValue(new DCmplExpr(leftOp, rightOp));
    } else if (boe instanceof CmpgExpr) {
        vb.setValue(new DCmpgExpr(leftOp, rightOp));
    }
}
Also used : DCmpgExpr(soot.dava.internal.javaRep.DCmpgExpr) ValueBox(soot.ValueBox) DCmpExpr(soot.dava.internal.javaRep.DCmpExpr) CmpExpr(soot.jimple.CmpExpr) ConditionExpr(soot.jimple.ConditionExpr) DCmpgExpr(soot.dava.internal.javaRep.DCmpgExpr) CmpgExpr(soot.jimple.CmpgExpr) Value(soot.Value) IntConstant(soot.jimple.IntConstant) DIntConstant(soot.dava.internal.javaRep.DIntConstant) DCmpExpr(soot.dava.internal.javaRep.DCmpExpr) DCmplExpr(soot.dava.internal.javaRep.DCmplExpr) DCmplExpr(soot.dava.internal.javaRep.DCmplExpr) CmplExpr(soot.jimple.CmplExpr) BinopExpr(soot.jimple.BinopExpr)

Example 20 with CmpgExpr

use of soot.jimple.CmpgExpr in project soot by soot-oss.

the class ConstraintChecker method caseAssignStmt.

@Override
public void caseAssignStmt(AssignStmt stmt) {
    final Value l = stmt.getLeftOp();
    final Value r = stmt.getRightOp();
    TypeNode left = null;
    if (l instanceof ArrayRef) {
        ArrayRef ref = (ArrayRef) l;
        TypeNode base = hierarchy.typeNode(((Local) ref.getBase()).getType());
        if (!base.isArray()) {
            error("Type Error(16)");
        }
        left = base.element();
        Value index = ref.getIndex();
        if (index instanceof Local) {
            if (!hierarchy.typeNode(((Local) index).getType()).hasAncestorOrSelf(hierarchy.typeNode(IntType.v()))) {
                error("Type Error(17)");
            }
        }
    } else if (l instanceof Local) {
        try {
            left = hierarchy.typeNode(((Local) l).getType());
        } catch (InternalTypingException e) {
            logger.debug("untyped local: " + l);
            throw e;
        }
    } else if (l instanceof InstanceFieldRef) {
        InstanceFieldRef ref = (InstanceFieldRef) l;
        Local base = (Local) ref.getBase();
        RefType classTy = ref.getField().getDeclaringClass().getType();
        if (!hierarchy.typeNode(base.getType()).hasAncestorOrSelf(hierarchy.typeNode(classTy))) {
            if (fix) {
                ref.setBase(insertCast(base, classTy, stmt));
            } else {
                error("Type Error(18)");
            }
        }
        left = hierarchy.typeNode(ref.getField().getType());
    } else if (l instanceof StaticFieldRef) {
        StaticFieldRef ref = (StaticFieldRef) l;
        left = hierarchy.typeNode(ref.getField().getType());
    } else {
        throw new RuntimeException("Unhandled assignment left hand side type: " + l.getClass());
    }
    if (r instanceof ArrayRef) {
        ArrayRef ref = (ArrayRef) r;
        Local base = (Local) ref.getBase();
        TypeNode baseTy = hierarchy.typeNode((base).getType());
        if (!baseTy.isArray()) {
            error("Type Error(19): " + baseTy + " is not an array type");
        }
        if (baseTy == hierarchy.NULL) {
            return;
        }
        if (!left.hasDescendantOrSelf(baseTy.element())) {
            if (fix) {
                Type lefttype = left.type();
                if (lefttype instanceof ArrayType) {
                    ArrayType atype = (ArrayType) lefttype;
                    ref.setBase(insertCast(base, ArrayType.v(atype.baseType, atype.numDimensions + 1), stmt));
                } else {
                    ref.setBase(insertCast(base, ArrayType.v(lefttype, 1), stmt));
                }
            } else {
                error("Type Error(20)");
            }
        }
        Value index = ref.getIndex();
        if (index instanceof Local) {
            if (!hierarchy.typeNode(((Local) index).getType()).hasAncestorOrSelf(hierarchy.typeNode(IntType.v()))) {
                error("Type Error(21)");
            }
        }
    } else if (r instanceof DoubleConstant) {
        if (!left.hasDescendantOrSelf(hierarchy.typeNode(DoubleType.v()))) {
            error("Type Error(22)");
        }
    } else if (r instanceof FloatConstant) {
        if (!left.hasDescendantOrSelf(hierarchy.typeNode(FloatType.v()))) {
            error("Type Error(45)");
        }
    } else if (r instanceof IntConstant) {
        if (!left.hasDescendantOrSelf(hierarchy.typeNode(IntType.v()))) {
            error("Type Error(23)");
        }
    } else if (r instanceof LongConstant) {
        if (!left.hasDescendantOrSelf(hierarchy.typeNode(LongType.v()))) {
            error("Type Error(24)");
        }
    } else if (r instanceof NullConstant) {
        if (!left.hasDescendantOrSelf(hierarchy.typeNode(NullType.v()))) {
            error("Type Error(25)");
        }
    } else if (r instanceof StringConstant) {
        if (!left.hasDescendantOrSelf(hierarchy.typeNode(RefType.v("java.lang.String")))) {
            error("Type Error(26)");
        }
    } else if (r instanceof ClassConstant) {
        if (!left.hasDescendantOrSelf(hierarchy.typeNode(RefType.v("java.lang.Class")))) {
            error("Type Error(27)");
        }
    } else if (r instanceof BinopExpr) {
        // ******** BINOP EXPR ********
        final BinopExpr be = (BinopExpr) r;
        final Value lv = be.getOp1();
        final Value rv = be.getOp2();
        TypeNode lop;
        TypeNode rop;
        // ******** LEFT ********
        if (lv instanceof Local) {
            lop = hierarchy.typeNode(((Local) lv).getType());
        } else if (lv instanceof DoubleConstant) {
            lop = hierarchy.typeNode(DoubleType.v());
        } else if (lv instanceof FloatConstant) {
            lop = hierarchy.typeNode(FloatType.v());
        } else if (lv instanceof IntConstant) {
            lop = hierarchy.typeNode(IntType.v());
        } else if (lv instanceof LongConstant) {
            lop = hierarchy.typeNode(LongType.v());
        } else if (lv instanceof NullConstant) {
            lop = hierarchy.typeNode(NullType.v());
        } else if (lv instanceof StringConstant) {
            lop = hierarchy.typeNode(RefType.v("java.lang.String"));
        } else if (lv instanceof ClassConstant) {
            lop = hierarchy.typeNode(RefType.v("java.lang.Class"));
        } else {
            throw new RuntimeException("Unhandled binary expression left operand type: " + lv.getClass());
        }
        // ******** RIGHT ********
        if (rv instanceof Local) {
            rop = hierarchy.typeNode(((Local) rv).getType());
        } else if (rv instanceof DoubleConstant) {
            rop = hierarchy.typeNode(DoubleType.v());
        } else if (rv instanceof FloatConstant) {
            rop = hierarchy.typeNode(FloatType.v());
        } else if (rv instanceof IntConstant) {
            rop = hierarchy.typeNode(IntType.v());
        } else if (rv instanceof LongConstant) {
            rop = hierarchy.typeNode(LongType.v());
        } else if (rv instanceof NullConstant) {
            rop = hierarchy.typeNode(NullType.v());
        } else if (rv instanceof StringConstant) {
            rop = hierarchy.typeNode(RefType.v("java.lang.String"));
        } else if (rv instanceof ClassConstant) {
            rop = hierarchy.typeNode(RefType.v("java.lang.Class"));
        } else {
            throw new RuntimeException("Unhandled binary expression right operand type: " + rv.getClass());
        }
        if ((be instanceof AddExpr) || (be instanceof SubExpr) || (be instanceof MulExpr) || (be instanceof DivExpr) || (be instanceof RemExpr) || (be instanceof AndExpr) || (be instanceof OrExpr) || (be instanceof XorExpr)) {
            if (!(left.hasDescendantOrSelf(lop) && left.hasDescendantOrSelf(rop))) {
                error("Type Error(27)");
            }
        } else if ((be instanceof ShlExpr) || (be instanceof ShrExpr) || (be instanceof UshrExpr)) {
            if (!(left.hasDescendantOrSelf(lop) && hierarchy.typeNode(IntType.v()).hasAncestorOrSelf(rop))) {
                error("Type Error(28)");
            }
        } else if ((be instanceof CmpExpr) || (be instanceof CmpgExpr) || (be instanceof CmplExpr) || (be instanceof EqExpr) || (be instanceof GeExpr) || (be instanceof GtExpr) || (be instanceof LeExpr) || (be instanceof LtExpr) || (be instanceof NeExpr)) {
            try {
                lop.lca(rop);
            } catch (TypeException e) {
                error(e.getMessage());
            }
            if (!left.hasDescendantOrSelf(hierarchy.typeNode(IntType.v()))) {
                error("Type Error(29)");
            }
        } else {
            throw new RuntimeException("Unhandled binary expression type: " + be.getClass());
        }
    } else if (r instanceof CastExpr) {
        CastExpr ce = (CastExpr) r;
        TypeNode castTy = hierarchy.typeNode(ce.getCastType());
        Value op = ce.getOp();
        if (op instanceof Local) {
            TypeNode opTy = hierarchy.typeNode(((Local) op).getType());
            try {
                // we must be careful not to reject primitive type casts (e.g. int to long)
                if (castTy.isClassOrInterface() || opTy.isClassOrInterface()) {
                    castTy.lca(opTy);
                }
            } catch (TypeException e) {
                logger.debug(r + "[" + opTy + "<->" + castTy + "]");
                error(e.getMessage());
            }
        }
        if (!left.hasDescendantOrSelf(castTy)) {
            error("Type Error(30)");
        }
    } else if (r instanceof InstanceOfExpr) {
        InstanceOfExpr ioe = (InstanceOfExpr) r;
        TypeNode type = hierarchy.typeNode(ioe.getCheckType());
        TypeNode op = hierarchy.typeNode(ioe.getOp().getType());
        try {
            op.lca(type);
        } catch (TypeException e) {
            logger.debug(r + "[" + op + "<->" + type + "]");
            error(e.getMessage());
        }
        if (!left.hasDescendantOrSelf(hierarchy.typeNode(IntType.v()))) {
            error("Type Error(31)");
        }
    } else if (r instanceof InvokeExpr) {
        InvokeExpr ie = (InvokeExpr) r;
        handleInvokeExpr(ie, stmt);
        if (!left.hasDescendantOrSelf(hierarchy.typeNode(ie.getMethodRef().getReturnType()))) {
            error("Type Error(32)");
        }
    } else if (r instanceof NewArrayExpr) {
        NewArrayExpr nae = (NewArrayExpr) r;
        Type baseType = nae.getBaseType();
        TypeNode right;
        if (baseType instanceof ArrayType) {
            right = hierarchy.typeNode(ArrayType.v(((ArrayType) baseType).baseType, ((ArrayType) baseType).numDimensions + 1));
        } else {
            right = hierarchy.typeNode(ArrayType.v(baseType, 1));
        }
        if (!left.hasDescendantOrSelf(right)) {
            error("Type Error(33)");
        }
        Value size = nae.getSize();
        if (size instanceof Local) {
            TypeNode var = hierarchy.typeNode(((Local) size).getType());
            if (!var.hasAncestorOrSelf(hierarchy.typeNode(IntType.v()))) {
                error("Type Error(34)");
            }
        }
    } else if (r instanceof NewExpr) {
        NewExpr ne = (NewExpr) r;
        if (!left.hasDescendantOrSelf(hierarchy.typeNode(ne.getBaseType()))) {
            error("Type Error(35)");
        }
    } else if (r instanceof NewMultiArrayExpr) {
        NewMultiArrayExpr nmae = (NewMultiArrayExpr) r;
        if (!left.hasDescendantOrSelf(hierarchy.typeNode(nmae.getBaseType()))) {
            error("Type Error(36)");
        }
        for (int i = 0; i < nmae.getSizeCount(); i++) {
            Value size = nmae.getSize(i);
            if (size instanceof Local) {
                TypeNode var = hierarchy.typeNode(((Local) size).getType());
                if (!var.hasAncestorOrSelf(hierarchy.typeNode(IntType.v()))) {
                    error("Type Error(37)");
                }
            }
        }
    } else if (r instanceof LengthExpr) {
        LengthExpr le = (LengthExpr) r;
        if (!left.hasDescendantOrSelf(hierarchy.typeNode(IntType.v()))) {
            error("Type Error(38)");
        }
        Value op = le.getOp();
        if (op instanceof Local) {
            if (!hierarchy.typeNode(((Local) op).getType()).isArray()) {
                error("Type Error(39)");
            }
        }
    } else if (r instanceof NegExpr) {
        NegExpr ne = (NegExpr) r;
        TypeNode right;
        Value op = ne.getOp();
        if (op instanceof Local) {
            right = hierarchy.typeNode(((Local) op).getType());
        } else if (op instanceof DoubleConstant) {
            right = hierarchy.typeNode(DoubleType.v());
        } else if (op instanceof FloatConstant) {
            right = hierarchy.typeNode(FloatType.v());
        } else if (op instanceof IntConstant) {
            right = hierarchy.typeNode(IntType.v());
        } else if (op instanceof LongConstant) {
            right = hierarchy.typeNode(LongType.v());
        } else {
            throw new RuntimeException("Unhandled neg expression operand type: " + op.getClass());
        }
        if (!left.hasDescendantOrSelf(right)) {
            error("Type Error(40)");
        }
    } else if (r instanceof Local) {
        Local loc = (Local) r;
        if (!left.hasDescendantOrSelf(hierarchy.typeNode(loc.getType()))) {
            if (fix) {
                stmt.setRightOp(insertCast(loc, left.type(), stmt));
            } else {
                error("Type Error(41)");
            }
        }
    } else if (r instanceof InstanceFieldRef) {
        InstanceFieldRef ref = (InstanceFieldRef) r;
        Local base = (Local) ref.getBase();
        RefType classTy = ref.getField().getDeclaringClass().getType();
        if (!hierarchy.typeNode(base.getType()).hasAncestorOrSelf(hierarchy.typeNode(classTy))) {
            if (fix) {
                ref.setBase(insertCast(base, classTy, stmt));
            } else {
                error("Type Error(42)");
            }
        }
        if (!left.hasDescendantOrSelf(hierarchy.typeNode(ref.getField().getType()))) {
            error("Type Error(43)");
        }
    } else if (r instanceof StaticFieldRef) {
        StaticFieldRef ref = (StaticFieldRef) r;
        if (!left.hasDescendantOrSelf(hierarchy.typeNode(ref.getField().getType()))) {
            error("Type Error(44)");
        }
    } else {
        throw new RuntimeException("Unhandled assignment right hand side type: " + r.getClass());
    }
}
Also used : AndExpr(soot.jimple.AndExpr) GtExpr(soot.jimple.GtExpr) LtExpr(soot.jimple.LtExpr) RefType(soot.RefType) ArrayType(soot.ArrayType) InterfaceInvokeExpr(soot.jimple.InterfaceInvokeExpr) DynamicInvokeExpr(soot.jimple.DynamicInvokeExpr) VirtualInvokeExpr(soot.jimple.VirtualInvokeExpr) InvokeExpr(soot.jimple.InvokeExpr) SpecialInvokeExpr(soot.jimple.SpecialInvokeExpr) StaticInvokeExpr(soot.jimple.StaticInvokeExpr) CastExpr(soot.jimple.CastExpr) ShlExpr(soot.jimple.ShlExpr) XorExpr(soot.jimple.XorExpr) NeExpr(soot.jimple.NeExpr) Local(soot.Local) AddExpr(soot.jimple.AddExpr) InstanceOfExpr(soot.jimple.InstanceOfExpr) OrExpr(soot.jimple.OrExpr) StaticFieldRef(soot.jimple.StaticFieldRef) NewArrayExpr(soot.jimple.NewArrayExpr) RemExpr(soot.jimple.RemExpr) ShrExpr(soot.jimple.ShrExpr) CmpExpr(soot.jimple.CmpExpr) Value(soot.Value) NewExpr(soot.jimple.NewExpr) StringConstant(soot.jimple.StringConstant) CmplExpr(soot.jimple.CmplExpr) ClassConstant(soot.jimple.ClassConstant) BinopExpr(soot.jimple.BinopExpr) MulExpr(soot.jimple.MulExpr) DoubleConstant(soot.jimple.DoubleConstant) NewMultiArrayExpr(soot.jimple.NewMultiArrayExpr) FloatConstant(soot.jimple.FloatConstant) NegExpr(soot.jimple.NegExpr) GeExpr(soot.jimple.GeExpr) UshrExpr(soot.jimple.UshrExpr) LeExpr(soot.jimple.LeExpr) ArrayRef(soot.jimple.ArrayRef) InstanceFieldRef(soot.jimple.InstanceFieldRef) IntConstant(soot.jimple.IntConstant) LongConstant(soot.jimple.LongConstant) LengthExpr(soot.jimple.LengthExpr) SubExpr(soot.jimple.SubExpr) NullConstant(soot.jimple.NullConstant) RefType(soot.RefType) Type(soot.Type) DoubleType(soot.DoubleType) FloatType(soot.FloatType) IntType(soot.IntType) LongType(soot.LongType) NullType(soot.NullType) ArrayType(soot.ArrayType) DivExpr(soot.jimple.DivExpr) EqExpr(soot.jimple.EqExpr) CmpgExpr(soot.jimple.CmpgExpr)

Aggregations

CmpExpr (soot.jimple.CmpExpr)24 CmpgExpr (soot.jimple.CmpgExpr)24 CmplExpr (soot.jimple.CmplExpr)24 AddExpr (soot.jimple.AddExpr)22 AndExpr (soot.jimple.AndExpr)22 DivExpr (soot.jimple.DivExpr)22 EqExpr (soot.jimple.EqExpr)22 GeExpr (soot.jimple.GeExpr)22 GtExpr (soot.jimple.GtExpr)22 LeExpr (soot.jimple.LeExpr)22 LtExpr (soot.jimple.LtExpr)22 MulExpr (soot.jimple.MulExpr)22 NeExpr (soot.jimple.NeExpr)22 OrExpr (soot.jimple.OrExpr)22 RemExpr (soot.jimple.RemExpr)22 ShlExpr (soot.jimple.ShlExpr)22 ShrExpr (soot.jimple.ShrExpr)22 SubExpr (soot.jimple.SubExpr)22 UshrExpr (soot.jimple.UshrExpr)22 XorExpr (soot.jimple.XorExpr)22