|
1 | 1 | package de.fraunhofer.iem.swan.features.code.soot; |
2 | 2 |
|
| 3 | +import de.fraunhofer.iem.swan.data.Method; |
| 4 | +import de.fraunhofer.iem.swan.io.dataset.SrmListUtils; |
| 5 | +import de.fraunhofer.iem.swan.util.SootUtils; |
| 6 | +import de.fraunhofer.iem.swan.util.Util; |
| 7 | +import org.slf4j.Logger; |
| 8 | +import org.slf4j.LoggerFactory; |
| 9 | +import soot.G; |
3 | 10 | import soot.Scene; |
| 11 | +import soot.SootClass; |
| 12 | +import soot.SootMethod; |
4 | 13 | import soot.options.Options; |
5 | 14 |
|
| 15 | +import java.io.IOException; |
| 16 | +import java.util.HashSet; |
| 17 | +import java.util.Set; |
| 18 | + |
6 | 19 | public class InitializeSoot { |
7 | | - |
8 | | - public InitializeSoot(){ |
9 | | - |
10 | | - } |
11 | | - |
12 | | - private String[] buildArgs(String path){ |
13 | | - String[] result = { |
14 | | - "-w", |
15 | | - "-no-bodies-for-excluded", |
16 | | - "-include-all", |
17 | | - "-p", |
18 | | - "cg.spark", |
19 | | - "on", |
20 | | - "-cp", |
21 | | - path, |
22 | | - "-p", |
23 | | - "jb", |
24 | | - "use-original-names:true", |
25 | | - "-f", |
26 | | - "n", |
27 | | - //do not merge variables (causes problems with PointsToSets) |
28 | | - "-p", |
29 | | - "jb.ulp", |
30 | | - "off" |
31 | | - }; |
32 | | - |
33 | | - return result; |
34 | | - } |
35 | | - |
36 | | - public void initialize(String path){ |
37 | | - String[] args = buildArgs(path); |
38 | | - |
39 | | - Options.v().set_allow_phantom_refs(true); |
40 | | - Options.v().set_prepend_classpath(true); |
41 | | - Options.v().set_output_format(Options.output_format_none); |
42 | | - Options.v().parse(args); |
43 | | - |
44 | | - Options.v().set_whole_program(true); |
45 | | - Scene.v().addBasicClass(Object.class.getName()); |
46 | | - Scene.v().loadNecessaryClasses(); |
47 | | - } |
48 | 20 |
|
| 21 | + private static boolean SOOT_INITIALIZED = false; |
| 22 | + private static final Logger logger = LoggerFactory.getLogger(SrmListUtils.class); |
| 23 | + private String classpath; |
| 24 | + |
| 25 | + public InitializeSoot(String... path) { |
| 26 | + |
| 27 | + this.classpath = Util.buildCP(path); |
| 28 | + initializeSoot(classpath); |
| 29 | + } |
| 30 | + |
| 31 | + private String[] buildArgs(String path) { |
| 32 | + String[] result = { |
| 33 | + "-w", |
| 34 | + "-no-bodies-for-excluded", |
| 35 | + "-include-all", |
| 36 | + "-p", |
| 37 | + "cg.spark", |
| 38 | + "on", |
| 39 | + "-cp", |
| 40 | + path, |
| 41 | + "-p", |
| 42 | + "jb", |
| 43 | + "use-original-names:true", |
| 44 | + "-f", |
| 45 | + "n", |
| 46 | + //do not merge variables (causes problems with PointsToSets) |
| 47 | + "-p", |
| 48 | + "jb.ulp", |
| 49 | + "off" |
| 50 | + }; |
| 51 | + |
| 52 | + return result; |
| 53 | + } |
| 54 | + |
| 55 | + public void initialize(String path) { |
| 56 | + String[] args = buildArgs(path); |
| 57 | + |
| 58 | + Options.v().set_allow_phantom_refs(true); |
| 59 | + Options.v().set_prepend_classpath(true); |
| 60 | + Options.v().set_output_format(Options.output_format_none); |
| 61 | + Options.v().parse(args); |
| 62 | + |
| 63 | + Options.v().set_whole_program(true); |
| 64 | + Scene.v().addBasicClass(Object.class.getName()); |
| 65 | + Scene.v().loadNecessaryClasses(); |
| 66 | + } |
| 67 | + |
| 68 | + |
| 69 | + public Set<Method> cleanupList(Set<Method> methods) throws IOException { |
| 70 | + |
| 71 | + Set<Method> purgedMethods = prefilterInterfaces(methods); |
| 72 | + |
| 73 | + Util.createSubclassAnnotations(purgedMethods, classpath); |
| 74 | + Util.sanityCheck(purgedMethods, new HashSet<>()); |
| 75 | + |
| 76 | + return purgedMethods; |
| 77 | + } |
| 78 | + |
| 79 | + public void runSoot(String path) { |
| 80 | + |
| 81 | + } |
| 82 | + |
| 83 | + |
| 84 | + private void initializeSoot(String cp) { |
| 85 | + if (SOOT_INITIALIZED) |
| 86 | + return; |
| 87 | + |
| 88 | + G.reset(); |
| 89 | + |
| 90 | + Options.v().set_allow_phantom_refs(true); |
| 91 | + Options.v().set_prepend_classpath(true); |
| 92 | + Options.v().set_whole_program(true); |
| 93 | + Options.v().set_include_all(true); |
| 94 | + Options.v().set_soot_classpath(cp); |
| 95 | + |
| 96 | + Scene.v().loadNecessaryClasses(); |
| 97 | + SOOT_INITIALIZED = true; |
| 98 | + } |
| 99 | + |
| 100 | + protected SootClass getSootClass(Method method) { |
| 101 | + |
| 102 | + SootClass c = Scene.v().forceResolve(method.getClassName(), SootClass.BODIES); |
| 103 | + |
| 104 | + if (c == null || c.isPhantom()) { |
| 105 | + return null; |
| 106 | + } |
| 107 | + |
| 108 | + return c; |
| 109 | + } |
| 110 | + |
| 111 | + protected SootMethod getSootMethod(Method method) { |
| 112 | + return getSootMethod(method, true); |
| 113 | + } |
| 114 | + |
| 115 | + protected SootMethod getSootMethod(Method method, boolean lookInHierarchy) { |
| 116 | + |
| 117 | + SootClass c = Scene.v().forceResolve(method.getClassName(), SootClass.BODIES); |
| 118 | + |
| 119 | + if (c == null || c.isPhantom()) { |
| 120 | + return null; |
| 121 | + } |
| 122 | + |
| 123 | + c.setApplicationClass(); |
| 124 | + if (c.isInterface()) return null; |
| 125 | + |
| 126 | + while (c != null) { |
| 127 | + // Does the current class declare the method we are looking for? |
| 128 | + if (method.getReturnType().isEmpty()) { |
| 129 | + if (c.declaresMethodByName(method.getName())) |
| 130 | + return c.getMethodByName(method.getName()); |
| 131 | + } else { |
| 132 | + //System.out.println(method.getSubSignature()); |
| 133 | + if (c.declaresMethod(method.getSubSignature())) |
| 134 | + return c.getMethod(method.getSubSignature()); |
| 135 | + } |
| 136 | + |
| 137 | + // Continue our search up the class hierarchy |
| 138 | + if (lookInHierarchy && c.hasSuperclass()) |
| 139 | + c = c.getSuperclass(); |
| 140 | + else |
| 141 | + c = null; |
| 142 | + } |
| 143 | + return null; |
| 144 | + } |
| 145 | + |
| 146 | + /** |
| 147 | + * Removes all interfaces from the given set of methods and returns the purged |
| 148 | + * set. |
| 149 | + */ |
| 150 | + private Set<Method> prefilterInterfaces(Set<Method> methods) { |
| 151 | + Set<Method> purgedMethods = new HashSet<>(); |
| 152 | + |
| 153 | + for (Method method : methods) { |
| 154 | + |
| 155 | + SootMethod sootMethod = getSootMethod(method); |
| 156 | + method.setSootMethod(sootMethod); |
| 157 | + method.setSootClass(getSootClass(method)); |
| 158 | + |
| 159 | + if (sootMethod == null) |
| 160 | + continue; |
| 161 | + |
| 162 | + if (sootMethod.isAbstract()) |
| 163 | + logger.info("Method purged from list {}", method.getSignature()); |
| 164 | + else |
| 165 | + purgedMethods.add(method); |
| 166 | + } |
| 167 | + |
| 168 | + logger.info("{} methods purged down to {}", methods.size(), purgedMethods.size()); |
| 169 | + return purgedMethods; |
| 170 | + } |
| 171 | + |
| 172 | + public Set<Method> loadMethodsFromTestLib(Set<String> testClasses) { |
| 173 | + Set<Method> methods = new HashSet<>(); |
| 174 | + |
| 175 | + for (String className : testClasses) { |
| 176 | + |
| 177 | + SootClass sc = Scene.v().forceResolve(className, SootClass.BODIES); |
| 178 | + |
| 179 | + if (sc == null || !testClasses.contains(sc.getName())) |
| 180 | + continue; |
| 181 | + |
| 182 | + if (!sc.isInterface() && !sc.isPrivate()) { |
| 183 | + for (SootMethod sm : sc.getMethods()) { |
| 184 | + |
| 185 | + if (sm.isConcrete()) { |
| 186 | + // This is done by hand here because of the cases where the |
| 187 | + // character ' is in the signature. This is not supported by the |
| 188 | + // current Soot. |
| 189 | + |
| 190 | + // TODO: Get Soot to support the character ' |
| 191 | + Method method = SootUtils.convertSootSignature(sm.getSignature()); |
| 192 | + method.setSootMethod(sm); |
| 193 | + method.setSootClass(sc); |
| 194 | + methods.add(method); |
| 195 | + } |
| 196 | + } |
| 197 | + } |
| 198 | + } |
| 199 | + return methods; |
| 200 | + } |
49 | 201 | } |
0 commit comments