private Map<Unit, Body> getStmtsWithConstants() { Map<Unit, Body> retMap = new LinkedHashMap<Unit, Body>(); for (SootClass sc : Scene.v().getClasses()) { for (SootMethod sm : sc.getMethods()) { if (!sm.hasActiveBody()) continue; Body methodBody = sm.retrieveActiveBody(); for (Unit u : methodBody.getUnits()) { if (u instanceof ReturnStmt) { if (((ReturnStmt) u).getOp() instanceof Constant) { retMap.put(u, methodBody); } } else if (((Stmt) u).containsInvokeExpr()) { InvokeExpr ie = ((Stmt) u).getInvokeExpr(); for (Value arg : ie.getArgs()) { if (arg instanceof StringConstant || arg instanceof NumericConstant) { retMap.put(u, methodBody); break; } } } } } } return retMap; }
public void outExprOrRefValueBox(ValueBox vb){ //System.out.println("here"+vb); Value v = vb.getValue(); if(! (v instanceof BinopExpr )){ return; } BinopExpr binop = (BinopExpr)v; if(DEBUG) System.out.println("calling getResult"); NumericConstant constant = getResult(binop); if(constant ==null) return; if(DEBUG) System.out.println("Changin"+vb+" to...."+constant); vb.setValue(constant); }
public void jimplify (DexBody body) { if(!(instruction instanceof Instruction31t)) throw new IllegalArgumentException("Expected Instruction31t but got: "+instruction.getClass()); Instruction31t fillArrayInstr = (Instruction31t)instruction; int destRegister = fillArrayInstr.getRegisterA(); int offset = fillArrayInstr.getCodeOffset(); int targetAddress = codeAddress + offset; Instruction referenceTable = body.instructionAtAddress(targetAddress).instruction; if(!(referenceTable instanceof ArrayPayload)) { throw new RuntimeException("Address " + targetAddress + "refers to an invalid PseudoInstruction."); } ArrayPayload arrayTable = (ArrayPayload)referenceTable; // NopStmt nopStmtBeginning = Jimple.v().newNopStmt(); // body.add(nopStmtBeginning); Local arrayReference = body.getRegisterLocal(destRegister); List<Number> elements = arrayTable.getArrayElements(); int numElements = elements.size(); Stmt firstAssign = null; for (int i = 0; i < numElements; i++) { ArrayRef arrayRef = Jimple.v().newArrayRef(arrayReference, IntConstant.v(i)); NumericConstant element = getArrayElement(elements.get(i),body,destRegister); if (element == null) //array was not defined -> element type can not be found (obfuscated bytecode?) break; AssignStmt assign = Jimple.v().newAssignStmt(arrayRef, element); addTags(assign); body.add(assign); if (i == 0) { firstAssign = assign; } } if (firstAssign == null) { // if numElements == 0. Is it possible? firstAssign = Jimple.v().newNopStmt(); body.add (firstAssign); } // NopStmt nopStmtEnd = Jimple.v().newNopStmt(); // body.add(nopStmtEnd); // defineBlock(nopStmtBeginning, nopStmtEnd); setUnit (firstAssign); }
public NumericConstant getResult(BinopExpr binop){ if(DEBUG) System.out.println("Binop expr"+binop); Value leftOp = binop.getOp1(); Value rightOp = binop.getOp2(); int op = 0; if(binop instanceof AddExpr){ op=1; } else if(binop instanceof SubExpr || binop instanceof DCmpExpr || binop instanceof DCmpgExpr || binop instanceof DCmplExpr){ op=2; } else if(binop instanceof MulExpr){ op=3; } if(op == 0){ if(DEBUG){ System.out.println("not add sub or mult"); System.out.println(binop.getClass().getName()); } return null; } NumericConstant constant = null; if(leftOp instanceof LongConstant && rightOp instanceof LongConstant){ if(DEBUG) System.out.println("long constants!!"); if(op ==1) constant = ((LongConstant)leftOp).add((LongConstant)rightOp); else if(op ==2) constant = ((LongConstant)leftOp).subtract((LongConstant)rightOp); else if (op ==3) constant = ((LongConstant)leftOp).multiply((LongConstant)rightOp); } else if(leftOp instanceof DoubleConstant && rightOp instanceof DoubleConstant){ if(DEBUG) System.out.println("double constants!!"); if(op ==1) constant = ((DoubleConstant)leftOp).add((DoubleConstant)rightOp); else if(op ==2) constant = ((DoubleConstant)leftOp).subtract((DoubleConstant)rightOp); else if (op ==3) constant = ((DoubleConstant)leftOp).multiply((DoubleConstant)rightOp); } else if(leftOp instanceof FloatConstant && rightOp instanceof FloatConstant){ if(DEBUG) System.out.println("Float constants!!"); if(op ==1) constant = ((FloatConstant)leftOp).add((FloatConstant)rightOp); else if(op ==2) constant = ((FloatConstant)leftOp).subtract((FloatConstant)rightOp); else if (op ==3) constant = ((FloatConstant)leftOp).multiply((FloatConstant)rightOp); } else if(leftOp instanceof IntConstant && rightOp instanceof IntConstant){ if(DEBUG) System.out.println("Integer constants!!"); if(op ==1) constant = ((IntConstant)leftOp).add((IntConstant)rightOp); else if(op ==2) constant = ((IntConstant)leftOp).subtract((IntConstant)rightOp); else if (op ==3) constant = ((IntConstant)leftOp).multiply((IntConstant)rightOp); } return constant; }
private Sign signOf(Value value, Map<Local, Sign> dfv) { if (value instanceof Local) { // if the value is a local variable, then look-up the data-flow map Local local = (Local) value; if (dfv.containsKey(local)) { return dfv.get(local); } else { return Sign.TOP; } } else if (value instanceof NumericConstant) { // If the value is a constant, we can get a definite sign NumericConstant num = (NumericConstant) value; NumericConstant zero = IntConstant.v(0); NumericConstant one = IntConstant.v(1); if (num.lessThan(zero).equals(one)) { return NEGATIVE; } else if (num.greaterThan(zero).equals(one)) { return POSITIVE; } else { return ZERO; } } else if (value instanceof BinopExpr) { BinopExpr expr = (BinopExpr) value; Value op1 = expr.getOp1(); Value op2 = expr.getOp2(); Sign sign1 = signOf(op1, dfv); Sign sign2 = signOf(op2, dfv); if (value instanceof AddExpr) { // Handle arithmetic plus return sign1.plus(sign2); } else if (value instanceof MulExpr) { // Handle arithmetic multiplication return sign1.mult(sign2); } else { // We do not handle other types of binary expressions return BOTTOM; } } else if (value instanceof UnopExpr) { if (value instanceof AbstractNegExpr) { // Handle unary minus Value op = ((AbstractNegExpr) value).getOp(); Sign sign = signOf(op, dfv); return sign.negate(); } else { // We do not handle other types of binary expressions return BOTTOM; } } else { // We do not handle other types of compound expressions return BOTTOM; } }