use of soot.jimple.FieldRef in project soot by Sable.
the class FieldRenamer method internalTransform.
protected void internalTransform(String phaseName, Map<String, String> options) {
if (output) {
if (rename_fields) {
out.println("Transforming Field Names and Adding Opaque Predicates...");
} else {
out.println("Adding Opaques...");
}
}
RefType boolRef = Scene.v().getRefType(booleanClassName);
BodyBuilder.retrieveAllBodies();
BodyBuilder.retrieveAllNames();
for (SootClass sc : Scene.v().getApplicationClasses()) {
String className = sc.getName();
if (className.contains(".")) {
className = className.substring(className.lastIndexOf(".") + 1, className.length());
}
oldToNewFieldNames.put(className, className);
if (rename_fields) {
if (output) {
out.println("\tClassName: " + className);
}
// rename all the fields in the class
for (SootField f : sc.getFields()) {
int weight = soot.jbco.Main.getWeight(phaseName, f.getName());
if (weight > 0) {
renameField(className, f);
}
}
}
// skip interfaces - they can only hold final fields
if (sc.isInterface()) {
continue;
}
// add one opaq predicate for true and one for false to each class
String bool = "opPred1";
Type t = Rand.getInt() % 2 == 0 ? BooleanType.v() : boolRef;
while (oldToNewFieldNames.containsKey(bool)) {
bool += "_";
}
SootField f = Scene.v().makeSootField(bool, t, Modifier.PUBLIC | Modifier.STATIC);
renameField(className, f);
opaquePreds1ByClass.put(sc, f);
sc.addField(f);
setBooleanTo(sc, f, true);
bool = "opPred2";
t = t == BooleanType.v() ? boolRef : BooleanType.v();
while (oldToNewFieldNames.containsKey(bool)) {
bool += "_";
}
f = Scene.v().makeSootField(bool, t, Modifier.PUBLIC | Modifier.STATIC);
renameField(className, f);
opaquePreds2ByClass.put(sc, f);
sc.addField(f);
if (t == boolRef) {
setBooleanTo(sc, f, false);
}
}
buildOpaquePairings();
if (!rename_fields) {
return;
}
if (output) {
out.println("\r\tUpdating field references in bytecode");
}
for (SootClass sc : Scene.v().getApplicationClasses()) {
for (SootMethod m : sc.getMethods()) {
if (!m.isConcrete()) {
continue;
}
if (!m.hasActiveBody()) {
m.retrieveActiveBody();
}
for (Unit unit : m.getActiveBody().getUnits()) {
for (ValueBox box : unit.getUseAndDefBoxes()) {
Value value = box.getValue();
if (value instanceof FieldRef) {
FieldRef fieldRef = (FieldRef) value;
SootFieldRef sootFieldRef = fieldRef.getFieldRef();
if (sootFieldRef.declaringClass().isLibraryClass()) {
continue;
}
String oldName = sootFieldRef.name();
String fullName = sootFieldRef.declaringClass().getName() + '.' + oldName;
String newName = oldToNewFieldNames.get(oldName);
if (newName == null || namesToNotRename.contains(fullName)) {
continue;
}
if (newName.equals(oldName)) {
System.out.println("Strange.. Should not find a field with the same old and new name.");
}
sootFieldRef = Scene.v().makeFieldRef(sootFieldRef.declaringClass(), newName, sootFieldRef.type(), sootFieldRef.isStatic());
fieldRef.setFieldRef(sootFieldRef);
try {
sootFieldRef.resolve();
} catch (Exception e) {
System.err.println("********ERROR Updating " + sootFieldRef.name() + " to " + newName);
System.err.println("Fields of " + sootFieldRef.declaringClass().getName() + ": " + sootFieldRef.declaringClass().getFields());
throw new RuntimeException(e);
}
}
}
}
}
}
}
Aggregations