public void outACastExpression(ACastExpression node) { Value val = (Value) mProductions.removeLast(); Type type = (Type) mProductions.removeLast(); mProductions.addLast(Jimple.v().newCastExpr(val, type)); }
/** * T t = (T) fromLocal; * * @param fromLocal * @param to * @param jb * @param us * @return */ private Local wideningPrimitiveConversion(Local fromLocal, Type to, JimpleBody jb, PatchingChain<Unit> us, LocalGenerator lc) { if (!(fromLocal.getType() instanceof PrimType)) { throw new IllegalArgumentException("Expected source to have primitive type"); } if (!(to instanceof PrimType)) { throw new IllegalArgumentException("Expected target to have primitive type"); } Local l2 = lc.generateLocal(to); us.add(Jimple.v().newAssignStmt(l2, Jimple.v().newCastExpr(fromLocal, to))); return l2; }
public soot.jimple.CastExpr newCastExpr(Value op1, Type t, ASTNode location) { soot.jimple.CastExpr expr = Jimple.v().newCastExpr(op1, t); createTag(expr, location); soot.tagkit.Tag op1tag = getTag(op1); if(op1tag != null) expr.getOpBox().addTag(op1tag); return expr; }
@Override public void jimplify(DexBody body) { if (!(instruction instanceof Instruction21c)) { throw new IllegalArgumentException("Expected Instruction21c but got: " + instruction.getClass()); } Instruction21c checkCastInstr = (Instruction21c) instruction; Local castValue = body.getRegisterLocal(checkCastInstr.getRegisterA()); Type checkCastType = DexType.toSoot((TypeReference) checkCastInstr.getReference()); CastExpr castExpr = Jimple.v().newCastExpr(castValue, checkCastType); // generate "x = (Type) x" // splitter will take care of the rest AssignStmt assign = Jimple.v().newAssignStmt(castValue, castExpr); setUnit(assign); addTags(assign); body.add(assign); if (IDalvikTyper.ENABLE_DVKTYPER) { DalvikTyper.v().setType(assign.getLeftOpBox(), checkCastType, false); } }
/** * T t = (T) fromLocal; * * @param fromLocal * @param to * @param jb * @param us * @return */ private Local narrowingReferenceConversion(Local fromLocal, Type to, JimpleBody jb, PatchingChain<Unit> us, LocalGenerator lc) { if (fromLocal.getType().equals(to)) { return fromLocal; } if (!(fromLocal.getType() instanceof RefType || fromLocal.getType() instanceof ArrayType)) { return fromLocal; } // throw new IllegalArgumentException("Expected source to have reference type"); if (!(to instanceof RefType || to instanceof ArrayType)) { return fromLocal; // throw new IllegalArgumentException("Expected target to have reference type"); } Local l2 = lc.generateLocal(to); us.add(Jimple.v().newAssignStmt(l2, Jimple.v().newCastExpr(fromLocal, to))); return l2; }
private static Value getConstantType(Type t) { if (t instanceof BooleanType) { return IntConstant.v(Rand.getInt(1)); } if (t instanceof IntType) { return IntConstant.v(Rand.getInt()); } if (t instanceof CharType) { return Jimple.v().newCastExpr(IntConstant.v(Rand.getInt()), CharType.v()); } if (t instanceof ByteType) { return Jimple.v().newCastExpr(IntConstant.v(Rand.getInt()), ByteType.v()); } if (t instanceof LongType) { return LongConstant.v(Rand.getLong()); } if (t instanceof FloatType) { return FloatConstant.v(Rand.getFloat()); } if (t instanceof DoubleType) { return DoubleConstant.v(Rand.getDouble()); } return Jimple.v().newCastExpr(NullConstant.v(), t); }
/** * Creates a cast at stmt of vold to the given type. * * @param useType * the new type * @param stmt * stmt * @param old * the old local * @return the new local */ protected Local createCast(Type useType, Stmt stmt, Local old) { Jimple jimple = Jimple.v(); Local vnew = jimple.newLocal("tmp", useType); vnew.setName("tmp$" + System.identityHashCode(vnew)); this.tg.set(vnew, useType); this.jb.getLocals().add(vnew); Unit u = Util.findFirstNonIdentityUnit(jb, stmt); this.jb.getUnits().insertBefore(jimple.newAssignStmt(vnew, jimple.newCastExpr(old, useType)), u); return vnew; }
private Local insertCastAfter(Local leftlocal, Type lefttype, Type righttype, Stmt stmt) { Local newlocal = Jimple.v().newLocal("tmp", righttype); stmtBody.getLocals().add(newlocal); Unit u = Util.findLastIdentityUnit(this.stmtBody, stmt); stmtBody.getUnits().insertAfter(Jimple.v().newAssignStmt(leftlocal, Jimple.v().newCastExpr(newlocal, lefttype)), u); return newlocal; }
private Local insertCast(Local oldlocal, Type type, Stmt stmt) { Local newlocal = Jimple.v().newLocal("tmp", type); stmtBody.getLocals().add(newlocal); Unit u = Util.findFirstNonIdentityUnit(this.stmtBody, stmt); stmtBody.getUnits().insertBefore(Jimple.v().newAssignStmt(newlocal, Jimple.v().newCastExpr(oldlocal, type)), u); return newlocal; }
private Local insertCast(Local oldlocal, Type type, Stmt stmt) { Local newlocal = Jimple.v().newLocal("tmp", type); stmtBody.getLocals().add(newlocal); Unit u = Util.findFirstNonIdentityUnit(stmtBody, stmt); stmtBody.getUnits().insertBefore(Jimple.v().newAssignStmt(newlocal, Jimple.v().newCastExpr(oldlocal, type)), u); return newlocal; } }
private Local insertCast(Local oldlocal, Type type, Stmt stmt) { Local newlocal = Jimple.v().newLocal("tmp", type); stmtBody.getLocals().add(newlocal); Unit u = Util.findFirstNonIdentityUnit(stmtBody, stmt); stmtBody.getUnits().insertBefore(Jimple.v().newAssignStmt(newlocal, Jimple.v().newCastExpr(oldlocal, type)), u); return newlocal; } }
private void insertCastOrBoxingCode(Local lhs, Local rhs, Chain<Unit> newUnits) { // if assigning to a primitive type then there's nothing to do if (lhs.getType() instanceof RefLikeType) { if ((rhs.getType() instanceof RefLikeType)) { // insert cast newUnits.add(Jimple.v().newAssignStmt(lhs, Jimple.v().newCastExpr(rhs, lhs.getType()))); } else { // primitive type in rhs; insert boxing code RefType boxedType = ((PrimType) rhs.getType()).boxedType(); SootMethodRef ref = Scene.v().makeMethodRef(boxedType.getSootClass(), "valueOf", Collections.<Type>singletonList(rhs.getType()), boxedType, true); newUnits.add(Jimple.v().newAssignStmt(lhs, Jimple.v().newStaticInvokeExpr(ref, rhs))); } } }
private void insertCastOrUnboxingCode(Local lhs, Local rhs, Chain<Unit> newUnits) { // if assigning to a reference type then there's nothing to do if (lhs.getType() instanceof PrimType) { if ((rhs.getType() instanceof PrimType)) { // insert cast newUnits.add(Jimple.v().newAssignStmt(lhs, Jimple.v().newCastExpr(rhs, lhs.getType()))); } else { // reference type in rhs; insert unboxing code RefType boxedType = (RefType) rhs.getType(); SootMethodRef ref = Scene.v().makeMethodRef(boxedType.getSootClass(), lhs.getType().toString() + "Value", Collections.<Type>emptyList(), lhs.getType(), false); newUnits.add(Jimple.v().newAssignStmt(lhs, Jimple.v().newVirtualInvokeExpr(rhs, ref))); } } }
@Override public void jimplify(DexBody body) { TwoRegisterInstruction i = (TwoRegisterInstruction) instruction; int dest = i.getRegisterA(); int source = i.getRegisterB(); Type targetType = getTargetType(); CastExpr cast = Jimple.v().newCastExpr(body.getRegisterLocal(source), targetType); AssignStmt assign = Jimple.v().newAssignStmt(body.getRegisterLocal(dest), cast); assign.addTag(getTag()); setUnit(assign); addTags(assign); body.add(assign); if (IDalvikTyper.ENABLE_DVKTYPER) { DalvikTyper.v().setType(assign.getLeftOpBox(), cast.getType(), false); // DalvikTyper.v().captureAssign((JAssignStmt)assign, op); } }
private Local insertCast(Value oldvalue, Type oldtype, Type type, Stmt stmt) { Local newlocal1 = Jimple.v().newLocal("tmp", oldtype); Local newlocal2 = Jimple.v().newLocal("tmp", type); stmtBody.getLocals().add(newlocal1); stmtBody.getLocals().add(newlocal2); Unit u = Util.findFirstNonIdentityUnit(this.stmtBody, stmt); stmtBody.getUnits().insertBefore(Jimple.v().newAssignStmt(newlocal1, oldvalue), u); stmtBody.getUnits().insertBefore(Jimple.v().newAssignStmt(newlocal2, Jimple.v().newCastExpr(newlocal1, type)), u); return newlocal2; } }
/** * Cast Expression Creation */ private soot.Value getCastLocal(polyglot.ast.Cast castExpr) { // if its already the right type if (castExpr.expr().type().equals(castExpr.type()) || (castExpr.type().isClass() && Util.getSootType(castExpr.type()).toString().equals("java.lang.Object"))) { return base().createAggressiveExpr(castExpr.expr(), false, false); } soot.Value val; val = base().createAggressiveExpr(castExpr.expr(), false, false); soot.Type type = Util.getSootType(castExpr.type()); soot.jimple.CastExpr cast = soot.jimple.Jimple.v().newCastExpr(val, type); Util.addLnPosTags(cast.getOpBox(), castExpr.expr().position()); soot.Local retLocal = lg.generateLocal(cast.getCastType()); soot.jimple.Stmt castAssign = soot.jimple.Jimple.v().newAssignStmt(retLocal, cast); body.getUnits().add(castAssign); Util.addLnPosTags(castAssign, castExpr.position()); return retLocal; }
newUnits.add(arrayLoad); Local castedLocal = localGen.generateLocal(boxedType); AssignStmt cast = Jimple.v().newAssignStmt(castedLocal, Jimple.v().newCastExpr(boxedLocal, boxedType)); newUnits.add(cast); VirtualInvokeExpr unboxInvokeExpr = Jimple.v().newVirtualInvokeExpr(castedLocal, ref); newUnits.add(arrayLoad); Local castedLocal = localGen.generateLocal(paramType); AssignStmt cast = Jimple.v().newAssignStmt(castedLocal, Jimple.v().newCastExpr(boxedLocal, paramType)); newUnits.add(cast); assignStmt = Jimple.v().newAssignStmt(paramLocals[paramIndex], castedLocal);
val = expr; } else if (op == CHECKCAST) { CastExpr expr = Jimple.v().newCastExpr(v1, t); vb = expr.getOpBox(); val = expr;
soot.jimple.Expr cast = soot.jimple.Jimple.v().newCastExpr(toApp, soot.IntType.v()); soot.jimple.Stmt castAssign = soot.jimple.Jimple.v().newAssignStmt(intLocal, cast); body.getUnits().add(castAssign);
CastExpr cast = Jimple.v().newCastExpr(val.stackOrValue(), totype); opr = new Operand(insn, cast); val.addBox(cast.getOpBox());