use of soot.IntType in project soot by Sable.
the class DavaPrinter method printTo.
public void printTo(SootClass cl, PrintWriter out) {
// IterableSet packagesUsed = new IterableSet();
IterableSet importList = new IterableSet();
{
String curPackage = cl.getJavaPackageName();
if (!curPackage.equals("")) {
out.println("package " + curPackage + ";");
out.println();
}
if (cl.hasSuperclass()) {
SootClass superClass = cl.getSuperclass();
importList.add(superClass.toString());
// packagesUsed.add(superClass.getJavaPackageName());
}
Iterator<SootClass> interfaceIt = cl.getInterfaces().iterator();
while (interfaceIt.hasNext()) {
String interfacePackage = ((SootClass) interfaceIt.next()).toString();
if (!importList.contains(interfacePackage))
importList.add(interfacePackage);
// if (!packagesUsed.contains(interfacePackage))
// packagesUsed.add(interfacePackage);
}
Iterator<SootMethod> methodIt = cl.methodIterator();
while (methodIt.hasNext()) {
SootMethod dm = (SootMethod) methodIt.next();
if (dm.hasActiveBody()) {
// packagesUsed = packagesUsed.union(((DavaBody) dm.getActiveBody()).get_PackagesUsed());
importList = importList.union(((DavaBody) dm.getActiveBody()).getImportList());
}
Iterator<SootClass> eit = dm.getExceptions().iterator();
while (eit.hasNext()) {
String thrownPackage = eit.next().toString();
if (!importList.contains(thrownPackage))
importList.add(thrownPackage);
// if (!packagesUsed.contains(thrownPackage))
// packagesUsed.add(thrownPackage);
}
Iterator<Type> pit = dm.getParameterTypes().iterator();
while (pit.hasNext()) {
Type t = (Type) pit.next();
if (t instanceof RefType) {
String paramPackage = ((RefType) t).getSootClass().toString();
if (!importList.contains(paramPackage))
importList.add(paramPackage);
// if (packagesUsed.contains(paramPackage) == false)
// packagesUsed.add(paramPackage);
}
}
Type t = dm.getReturnType();
if (t instanceof RefType) {
String returnPackage = ((RefType) t).getSootClass().toString();
if (!importList.contains(returnPackage))
importList.add(returnPackage);
// if (packagesUsed.contains(returnPackage) == false)
// packagesUsed.add(returnPackage);
}
}
Iterator<SootField> fieldIt = cl.getFields().iterator();
while (fieldIt.hasNext()) {
SootField f = (SootField) fieldIt.next();
if (f.isPhantom())
continue;
Type t = f.getType();
if (t instanceof RefType) {
String fieldPackage = ((RefType) t).getSootClass().toString();
if (!importList.contains(fieldPackage))
importList.add(fieldPackage);
}
}
Iterator<String> pit = importList.iterator();
List<String> toImport = new ArrayList<String>();
while (pit.hasNext()) {
/*
* dont import any file which has currentPackage.className
* dont import any file which starts with java.lang
*/
String temp = (String) pit.next();
// System.out.println("temp is "+temp);
if (temp.indexOf("java.lang") > -1) {
// problem is that we need to import sub packages java.lang.ref
// for instance if the type is java.lang.ref.WeakReference
String tempClassName = RemoveFullyQualifiedName.getClassName(temp);
if (temp.equals("java.lang." + tempClassName)) {
// System.out.println("temp was not printed as it belongs to java.lang");
continue;
}
}
if (curPackage.length() > 0 && temp.indexOf(curPackage) > -1) {
// System.out.println("here "+temp);
continue;
}
if (cl.toString().equals(temp))
continue;
// System.out.println("printing"+);
toImport.add(temp);
}
/*
* Check that we are not importing two classes with the same last name
* If yes then remove explicit import and import the whole package
* else output explicit import statement
*/
Iterator it = toImport.iterator();
while (it.hasNext()) {
String temp = (String) it.next();
if (RemoveFullyQualifiedName.containsMultiple(toImport.iterator(), temp, null)) {
// import package add *
if (temp.lastIndexOf('.') > -1) {
temp = temp.substring(0, temp.lastIndexOf('.'));
out.println("import " + temp + ".*;");
} else
throw new DecompilationException("Cant find the DOT . for fullyqualified name");
} else {
if (temp.lastIndexOf('.') == -1) {
// dot not found this is a class belonging to this package so dont add
} else
out.println("import " + temp + ";");
}
}
boolean addNewLine = false;
addNewLine = true;
if (addNewLine)
out.println();
/*if (!packagesUsed.isEmpty())
out.println();
packagesUsed.add("java.lang");
packagesUsed.add(curPackage);
*/
Dava.v().set_CurrentPackageContext(importList);
// Dava.v().set_CurrentPackageContext(packagesUsed);
Dava.v().set_CurrentPackage(curPackage);
}
// Print class name + modifiers
{
String classPrefix = "";
classPrefix = classPrefix + " " + Modifier.toString(cl.getModifiers());
classPrefix = classPrefix.trim();
if (!cl.isInterface()) {
classPrefix = classPrefix + " class";
classPrefix = classPrefix.trim();
}
out.print(classPrefix + " " + cl.getShortJavaStyleName());
}
// Print extension
if (cl.hasSuperclass() && !(cl.getSuperclass().getName().equals("java.lang.Object"))) {
String superClassName = cl.getSuperclass().getName();
// Nomair Naeem 8th Feb 2006
// also check if the super class name is not a fully qualified
// name. in which case if the package is imported no need for
// the long name
superClassName = RemoveFullyQualifiedName.getReducedName(importList, superClassName, cl.getType());
out.print(" extends " + superClassName + "");
}
// Print interfaces
{
Iterator<SootClass> interfaceIt = cl.getInterfaces().iterator();
if (interfaceIt.hasNext()) {
if (cl.isInterface())
out.print(" extends ");
else
out.print(" implements ");
out.print("" + (interfaceIt.next()).getName() + "");
while (interfaceIt.hasNext()) out.print(", " + (interfaceIt.next()).getName() + "");
}
}
out.println();
out.println("{");
// Print fields
{
Iterator<SootField> fieldIt = cl.getFields().iterator();
if (fieldIt.hasNext()) {
while (fieldIt.hasNext()) {
SootField f = fieldIt.next();
if (f.isPhantom())
continue;
String declaration = null;
Type fieldType = f.getType();
String qualifiers = Modifier.toString(f.getModifiers()) + " ";
qualifiers += RemoveFullyQualifiedName.getReducedName(importList, fieldType.toString(), fieldType);
qualifiers = qualifiers.trim();
if (qualifiers.equals(""))
declaration = Scene.v().quotedNameOf(f.getName());
else
declaration = qualifiers + " " + Scene.v().quotedNameOf(f.getName()) + "";
if (f.isFinal() && f.isStatic()) {
if (fieldType instanceof DoubleType && f.hasTag("DoubleConstantValueTag")) {
double val = ((DoubleConstantValueTag) f.getTag("DoubleConstantValueTag")).getDoubleValue();
out.println(" " + declaration + " = " + val + ";");
} else if (fieldType instanceof FloatType && f.hasTag("FloatConstantValueTag")) {
float val = ((FloatConstantValueTag) f.getTag("FloatConstantValueTag")).getFloatValue();
out.println(" " + declaration + " = " + val + "f;");
} else if (fieldType instanceof LongType && f.hasTag("LongConstantValueTag")) {
long val = ((LongConstantValueTag) f.getTag("LongConstantValueTag")).getLongValue();
out.println(" " + declaration + " = " + val + "l;");
} else if (fieldType instanceof CharType && f.hasTag("IntegerConstantValueTag")) {
int val = ((IntegerConstantValueTag) f.getTag("IntegerConstantValueTag")).getIntValue();
out.println(" " + declaration + " = '" + ((char) val) + "';");
} else if (fieldType instanceof BooleanType && f.hasTag("IntegerConstantValueTag")) {
int val = ((IntegerConstantValueTag) f.getTag("IntegerConstantValueTag")).getIntValue();
if (val == 0)
out.println(" " + declaration + " = false;");
else
out.println(" " + declaration + " = true;");
} else if ((fieldType instanceof IntType || fieldType instanceof ByteType || fieldType instanceof ShortType) && f.hasTag("IntegerConstantValueTag")) {
int val = ((IntegerConstantValueTag) f.getTag("IntegerConstantValueTag")).getIntValue();
out.println(" " + declaration + " = " + val + ";");
} else if (f.hasTag("StringConstantValueTag")) {
String val = ((StringConstantValueTag) f.getTag("StringConstantValueTag")).getStringValue();
out.println(" " + declaration + " = \"" + val + "\";");
} else {
// System.out.println("Couldnt find type of
// field"+f.getDeclaration());
out.println(" " + declaration + ";");
}
} else // field is static final
{
out.println(" " + declaration + ";");
}
}
}
}
// Print methods
{
Iterator<SootMethod> methodIt = cl.methodIterator();
if (methodIt.hasNext()) {
if (cl.getMethodCount() != 0)
out.println();
while (methodIt.hasNext()) {
SootMethod method = (SootMethod) methodIt.next();
if (method.isPhantom())
continue;
if (!Modifier.isAbstract(method.getModifiers()) && !Modifier.isNative(method.getModifiers())) {
if (!method.hasActiveBody())
throw new RuntimeException("method " + method.getName() + " has no active body!");
else
printTo(method.getActiveBody(), out);
if (methodIt.hasNext())
out.println();
} else {
// if method is abstract then print the declaration
out.print(" ");
out.print(method.getDavaDeclaration());
out.println(";");
if (methodIt.hasNext())
out.println();
}
}
}
}
if (G.v().SootClassNeedsDavaSuperHandlerClass.contains(cl)) {
out.println("\n private static class DavaSuperHandler{");
out.println(" java.util.Vector myVector = new java.util.Vector();");
out.println("\n public Object get(int pos){");
out.println(" return myVector.elementAt(pos);");
out.println(" }");
out.println("\n public void store(Object obj){");
out.println(" myVector.add(obj);");
out.println(" }");
out.println(" }");
}
out.println("}");
}
use of soot.IntType in project soot by Sable.
the class ArithmeticTransformer method internalTransform.
protected void internalTransform(Body b, String phaseName, Map<String, String> options) {
int weight = soot.jbco.Main.getWeight(phaseName, b.getMethod().getSignature());
if (weight == 0) {
return;
}
PatchingChain<Unit> units = b.getUnits();
int localCount = 0;
Chain<Local> locals = b.getLocals();
if (output) {
out.println("*** Performing Arithmetic Transformation on " + b.getMethod().getSignature());
}
Iterator<Unit> it = units.snapshotIterator();
while (it.hasNext()) {
Unit u = it.next();
if (u instanceof AssignStmt) {
AssignStmt as = (AssignStmt) u;
Value v = as.getRightOp();
if (v instanceof MulExpr) {
total++;
MulExpr me = (MulExpr) v;
Value op1 = me.getOp1();
Value op = null, op2 = me.getOp2();
NumericConstant nc = null;
if (op1 instanceof NumericConstant) {
nc = (NumericConstant) op1;
op = op2;
} else if (op2 instanceof NumericConstant) {
nc = (NumericConstant) op2;
op = op1;
}
if (nc != null) {
if (output) {
out.println("Considering: " + as + "\r");
}
Type opType = op.getType();
int max = opType instanceof IntType ? 32 : opType instanceof LongType ? 64 : 0;
if (max != 0) {
Object[] shft_rem = checkNumericValue(nc);
if (shft_rem[0] != null && (Integer) shft_rem[0] < max && Rand.getInt(10) <= weight) {
List<Unit> unitsBuilt = new ArrayList<>();
int rand = Rand.getInt(16);
int shift = (Integer) shft_rem[0];
boolean neg = (Boolean) shft_rem[2];
if (rand % 2 == 0) {
shift += rand * max;
} else {
shift -= rand * max;
}
Expr e;
if (shft_rem[1] != null) {
// if there is an additive floating component
Local tmp2 = null, tmp1 = Jimple.v().newLocal("__tmp_shft_lcl" + localCount++, opType);
locals.add(tmp1);
// shift the integral portion
Unit newU = Jimple.v().newAssignStmt(tmp1, Jimple.v().newShlExpr(op, IntConstant.v(shift)));
unitsBuilt.add(newU);
units.insertBefore(newU, u);
// grab remainder (that not part of the 2^x)
double rem = (Double) shft_rem[1];
if (rem != 1) {
if (rem == ((int) rem) && opType instanceof IntType) {
nc = IntConstant.v((int) rem);
} else if (rem == ((long) rem) && opType instanceof LongType) {
nc = LongConstant.v((long) rem);
} else {
nc = DoubleConstant.v(rem);
}
if (nc instanceof DoubleConstant) {
tmp2 = Jimple.v().newLocal("__tmp_shft_lcl" + localCount++, DoubleType.v());
locals.add(tmp2);
newU = Jimple.v().newAssignStmt(tmp2, Jimple.v().newCastExpr(op, DoubleType.v()));
unitsBuilt.add(newU);
units.insertBefore(newU, u);
newU = Jimple.v().newAssignStmt(tmp2, Jimple.v().newMulExpr(tmp2, nc));
} else {
tmp2 = Jimple.v().newLocal("__tmp_shft_lcl" + localCount++, nc.getType());
locals.add(tmp2);
newU = Jimple.v().newAssignStmt(tmp2, Jimple.v().newMulExpr(op, nc));
}
unitsBuilt.add(newU);
units.insertBefore(newU, u);
}
if (tmp2 == null) {
e = Jimple.v().newAddExpr(tmp1, op);
} else if (tmp2.getType().getClass() != tmp1.getType().getClass()) {
Local tmp3 = Jimple.v().newLocal("__tmp_shft_lcl" + localCount++, tmp2.getType());
locals.add(tmp3);
newU = Jimple.v().newAssignStmt(tmp3, Jimple.v().newCastExpr(tmp1, tmp2.getType()));
unitsBuilt.add(newU);
units.insertBefore(newU, u);
e = Jimple.v().newAddExpr(tmp3, tmp2);
} else {
e = Jimple.v().newAddExpr(tmp1, tmp2);
}
} else {
e = Jimple.v().newShlExpr(op, IntConstant.v(shift));
}
if (e.getType().getClass() != as.getLeftOp().getType().getClass()) {
Local tmp = Jimple.v().newLocal("__tmp_shft_lcl" + localCount++, e.getType());
locals.add(tmp);
Unit newU = Jimple.v().newAssignStmt(tmp, e);
unitsBuilt.add(newU);
units.insertAfter(newU, u);
e = Jimple.v().newCastExpr(tmp, as.getLeftOp().getType());
}
as.setRightOp(e);
unitsBuilt.add(as);
if (neg) {
Unit newU = Jimple.v().newAssignStmt(as.getLeftOp(), Jimple.v().newNegExpr(as.getLeftOp()));
unitsBuilt.add(newU);
units.insertAfter(newU, u);
}
mulPerformed++;
printOutput(unitsBuilt);
}
}
}
} else if (v instanceof DivExpr) {
total++;
DivExpr de = (DivExpr) v;
Value op2 = de.getOp2();
NumericConstant nc;
if (op2 instanceof NumericConstant) {
nc = (NumericConstant) op2;
Type opType = de.getOp1().getType();
int max = opType instanceof IntType ? 32 : opType instanceof LongType ? 64 : 0;
if (max != 0) {
Object[] shft_rem = checkNumericValue(nc);
if (shft_rem[0] != null && (shft_rem[1] == null || (Double) shft_rem[1] == 0) && (Integer) shft_rem[0] < max && Rand.getInt(10) <= weight) {
List<Unit> unitsBuilt = new ArrayList<>();
int rand = Rand.getInt(16);
int shift = (Integer) shft_rem[0];
boolean neg = (Boolean) shft_rem[2];
if (Rand.getInt() % 2 == 0) {
shift += rand * max;
} else {
shift -= rand * max;
}
Expr e = Jimple.v().newShrExpr(de.getOp1(), IntConstant.v(shift));
if (e.getType().getClass() != as.getLeftOp().getType().getClass()) {
Local tmp = Jimple.v().newLocal("__tmp_shft_lcl" + localCount++, e.getType());
locals.add(tmp);
Unit newU = Jimple.v().newAssignStmt(tmp, e);
unitsBuilt.add(newU);
units.insertAfter(newU, u);
e = Jimple.v().newCastExpr(tmp, as.getLeftOp().getType());
}
as.setRightOp(e);
unitsBuilt.add(as);
if (neg) {
Unit newU = Jimple.v().newAssignStmt(as.getLeftOp(), Jimple.v().newNegExpr(as.getLeftOp()));
unitsBuilt.add(newU);
units.insertAfter(newU, u);
}
divPerformed++;
printOutput(unitsBuilt);
}
}
}
}
}
}
}
use of soot.IntType in project soot by Sable.
the class LocalNameStandardizer method internalTransform.
@Override
protected void internalTransform(Body body, String phaseName, Map<String, String> options) {
boolean onlyStackName = PhaseOptions.getBoolean(options, "only-stack-locals");
boolean sortLocals = PhaseOptions.getBoolean(options, "sort-locals");
final BooleanType booleanType = BooleanType.v();
final ByteType byteType = ByteType.v();
final ShortType shortType = ShortType.v();
final CharType charType = CharType.v();
final IntType intType = IntType.v();
final LongType longType = LongType.v();
final DoubleType doubleType = DoubleType.v();
final FloatType floatType = FloatType.v();
final ErroneousType erroneousType = ErroneousType.v();
final UnknownType unknownType = UnknownType.v();
final StmtAddressType stmtAddressType = StmtAddressType.v();
final NullType nullType = NullType.v();
// Change the names to the standard forms now.
{
int objectCount = 0;
int intCount = 0;
int longCount = 0;
int floatCount = 0;
int doubleCount = 0;
int addressCount = 0;
int errorCount = 0;
int nullCount = 0;
/* The goal of this option is to ensure that local ordering remains
* consistent between different iterations of soot. This helps to ensure
* things like stable string representations of instructions and stable
* jimple representations of a methods body when soot is used to load
* the same code in different iterations.
*
* First sorts the locals alphabetically by the string representation of
* their type. Then if there are two locals with the same type, it uses
* the only other source of structurally stable information (i.e. the
* instructions themselves) to produce an ordering for the locals
* that remains consistent between different soot instances. It achieves
* this by determining the position of a local's first occurrence in the
* instruction's list of definition statements. This position is then used
* to sort the locals with the same type in an ascending order.
*
* The only times that this may not produce a consistent ordering for the
* locals between different soot instances is if a local is never defined in
* the instructions or if the instructions themselves are changed in some way
* that effects the ordering of the locals. In the first case, if a local is
* never defined, the other jimple body phases will remove this local as it is
* unused. As such, all we have to do is rerun this LocalNameStandardizer after
* all other jimple body phases to eliminate any ambiguity introduced by these
* phases and by the removed unused locals. In the second case, if the instructions
* themselves changed then the user would have had to intentionally told soot to
* modify the instructions of the code. Otherwise, the instructions would not have
* changed because we assume the instructions to always be structurally stable
* between different instances of soot. As such, in this instance, the user should
* not be expecting soot to produce the same output as the input and thus the
* ordering of the locals does not matter.
*/
if (sortLocals) {
Chain<Local> locals = body.getLocals();
final List<ValueBox> defs = body.getDefBoxes();
ArrayList<Local> sortedLocals = new ArrayList<Local>(locals);
Collections.sort(sortedLocals, new Comparator<Local>() {
private Map<Local, Integer> firstOccuranceCache = new HashMap<Local, Integer>();
@Override
public int compare(Local arg0, Local arg1) {
int ret = arg0.getType().toString().compareTo(arg1.getType().toString());
if (ret == 0) {
ret = Integer.compare(getFirstOccurance(arg0), getFirstOccurance(arg1));
}
return ret;
}
private int getFirstOccurance(Local l) {
Integer cur = firstOccuranceCache.get(l);
if (cur != null) {
return cur;
} else {
int count = 0;
int first = -1;
for (ValueBox vb : defs) {
Value v = vb.getValue();
if (v instanceof Local && v.equals(l)) {
first = count;
break;
}
count++;
}
firstOccuranceCache.put(l, first);
return first;
}
}
});
locals.clear();
locals.addAll(sortedLocals);
}
for (Local l : body.getLocals()) {
String prefix = "";
if (l.getName().startsWith("$"))
prefix = "$";
else {
if (onlyStackName)
continue;
}
final Type type = l.getType();
if (type.equals(booleanType))
l.setName(prefix + "z" + intCount++);
else if (type.equals(byteType))
l.setName(prefix + "b" + longCount++);
else if (type.equals(shortType))
l.setName(prefix + "s" + longCount++);
else if (type.equals(charType))
l.setName(prefix + "c" + longCount++);
else if (type.equals(intType))
l.setName(prefix + "i" + longCount++);
else if (type.equals(longType))
l.setName(prefix + "l" + longCount++);
else if (type.equals(doubleType))
l.setName(prefix + "d" + doubleCount++);
else if (type.equals(floatType))
l.setName(prefix + "f" + floatCount++);
else if (type.equals(stmtAddressType))
l.setName(prefix + "a" + addressCount++);
else if (type.equals(erroneousType) || type.equals(unknownType)) {
l.setName(prefix + "e" + errorCount++);
} else if (type.equals(nullType))
l.setName(prefix + "n" + nullCount++);
else
l.setName(prefix + "r" + objectCount++);
}
}
}
use of soot.IntType in project soot by Sable.
the class TypeResolver method assign_types_1_2.
private void assign_types_1_2() throws TypeException {
for (Iterator<Local> localIt = stmtBody.getLocals().iterator(); localIt.hasNext(); ) {
final Local local = localIt.next();
TypeVariable var = typeVariable(local);
if (var == null) {
local.setType(RefType.v("java.lang.Object"));
} else if (var.depth() == 0) {
if (var.type() == null) {
TypeVariable.error("Type Error(5): Variable without type");
} else {
local.setType(var.type().type());
}
} else {
TypeVariable element = var.element();
for (int j = 1; j < var.depth(); j++) {
element = element.element();
}
if (element.type() == null) {
TypeVariable.error("Type Error(6): Array variable without base type");
} else if (element.type().type() instanceof NullType) {
local.setType(NullType.v());
} else {
Type t = element.type().type();
if (t instanceof IntType) {
local.setType(var.approx().type());
} else {
local.setType(ArrayType.v(t, var.depth()));
}
}
}
if (DEBUG) {
if ((var != null) && (var.approx() != null) && (var.approx().type() != null) && (local != null) && (local.getType() != null) && !local.getType().equals(var.approx().type())) {
logger.debug("local: " + local + ", type: " + local.getType() + ", approx: " + var.approx().type());
}
}
}
}
use of soot.IntType in project soot by Sable.
the class StmtVisitor method buildArrayFillInsn.
private Insn buildArrayFillInsn(ArrayRef destRef, List<Value> values) {
Local array = (Local) destRef.getBase();
Register arrayReg = regAlloc.asLocal(array);
// Convert the list of values into a list of numbers
int elementSize = 0;
List<Number> numbers = new ArrayList<Number>(values.size());
for (Value val : values) {
if (val instanceof IntConstant) {
elementSize = Math.max(elementSize, 4);
numbers.add(((IntConstant) val).value);
} else if (val instanceof LongConstant) {
elementSize = Math.max(elementSize, 8);
numbers.add(((LongConstant) val).value);
} else if (val instanceof FloatConstant) {
elementSize = Math.max(elementSize, 4);
numbers.add(((FloatConstant) val).value);
} else if (val instanceof DoubleConstant) {
elementSize = Math.max(elementSize, 8);
numbers.add(((DoubleConstant) val).value);
} else
return null;
}
// For some local types, we know the size upfront
if (destRef.getType() instanceof BooleanType)
elementSize = 1;
else if (destRef.getType() instanceof ByteType)
elementSize = 1;
else if (destRef.getType() instanceof CharType)
elementSize = 2;
else if (destRef.getType() instanceof ShortType)
elementSize = 2;
else if (destRef.getType() instanceof IntType)
elementSize = 4;
else if (destRef.getType() instanceof FloatType)
elementSize = 4;
else if (destRef.getType() instanceof LongType)
elementSize = 8;
else if (destRef.getType() instanceof DoubleType)
elementSize = 8;
ArrayDataPayload payload = new ArrayDataPayload(elementSize, numbers);
payloads.add(payload);
Insn31t insn = new Insn31t(Opcode.FILL_ARRAY_DATA, arrayReg);
insn.setPayload(payload);
return insn;
}
Aggregations