Skip to content

Commit 5fd6e8f

Browse files
committed
Refactor Soot helper methods
1 parent 7f4265a commit 5fd6e8f

File tree

2 files changed

+59
-86
lines changed

2 files changed

+59
-86
lines changed

swan-pipeline/src/main/java/de/fraunhofer/iem/swan/features/code/soot/InitializeSoot.java renamed to swan-pipeline/src/main/java/de/fraunhofer/iem/swan/soot/Soot.java

Lines changed: 58 additions & 85 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
1-
package de.fraunhofer.iem.swan.features.code.soot;
1+
package de.fraunhofer.iem.swan.soot;
22

33
import de.fraunhofer.iem.swan.data.Method;
4+
import de.fraunhofer.iem.swan.io.dataset.SrmList;
45
import de.fraunhofer.iem.swan.io.dataset.SrmListUtils;
5-
import de.fraunhofer.iem.swan.util.SootUtils;
66
import de.fraunhofer.iem.swan.util.Util;
77
import org.slf4j.Logger;
88
import org.slf4j.LoggerFactory;
9-
import soot.G;
109
import soot.Scene;
1110
import soot.SootClass;
1211
import soot.SootMethod;
@@ -16,88 +15,53 @@
1615
import java.util.HashSet;
1716
import java.util.Set;
1817

19-
public class InitializeSoot {
18+
public class Soot {
2019

21-
private static boolean SOOT_INITIALIZED = false;
2220
private static final Logger logger = LoggerFactory.getLogger(SrmListUtils.class);
2321
private String classpath;
2422

25-
public InitializeSoot(String... path) {
23+
public Soot(String... path) {
2624

2725
this.classpath = Util.buildCP(path);
28-
initializeSoot(classpath);
26+
configure(classpath);
2927
}
3028

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);
29+
/**
30+
* Configures Soot.
31+
*
32+
* @param classpath test and/or train source code classpath
33+
*/
34+
private void configure(String classpath) {
5735

5836
Options.v().set_allow_phantom_refs(true);
5937
Options.v().set_prepend_classpath(true);
60-
Options.v().set_output_format(Options.output_format_none);
61-
Options.v().parse(args);
62-
6338
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) {
39+
Options.v().set_include_all(true);
40+
Options.v().set_soot_classpath(classpath);
8041

42+
Scene.v().loadNecessaryClasses();
8143
}
8244

45+
/**
46+
* Purges list of methods and performs sanity check.
47+
*
48+
* @param srmList SRM list to be cleaned.
49+
*/
50+
public void cleanupList(SrmList srmList) throws IOException {
8351

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);
52+
prefilterInterfaces(srmList.getMethods());
9553

96-
Scene.v().loadNecessaryClasses();
97-
SOOT_INITIALIZED = true;
54+
Util.createSubclassAnnotations(srmList.getMethods(), classpath);
55+
Util.sanityCheck(srmList.getMethods(), new HashSet<>());
9856
}
9957

100-
protected SootClass getSootClass(Method method) {
58+
/**
59+
* Returns SootClass for method.
60+
*
61+
* @param method Method from test or train set.
62+
* @return Soot class
63+
*/
64+
protected SootClass getClass(Method method) {
10165

10266
SootClass c = Scene.v().forceResolve(method.getClassName(), SootClass.BODIES);
10367

@@ -108,11 +72,24 @@ protected SootClass getSootClass(Method method) {
10872
return c;
10973
}
11074

111-
protected SootMethod getSootMethod(Method method) {
112-
return getSootMethod(method, true);
75+
/**
76+
* Returns SootMethod for provided method.
77+
*
78+
* @param method Method from test/train data set.
79+
* @return Soot method
80+
*/
81+
protected SootMethod getMethod(Method method) {
82+
return getMethod(method, true);
11383
}
11484

115-
protected SootMethod getSootMethod(Method method, boolean lookInHierarchy) {
85+
/**
86+
* Returns SootMethod for provided method.
87+
*
88+
* @param method Method from test or train data set.
89+
* @param lookInHierarchy whether hierarchy should be searched
90+
* @return Soot method
91+
*/
92+
protected SootMethod getMethod(Method method, boolean lookInHierarchy) {
11693

11794
SootClass c = Scene.v().forceResolve(method.getClassName(), SootClass.BODIES);
11895

@@ -129,7 +106,6 @@ protected SootMethod getSootMethod(Method method, boolean lookInHierarchy) {
129106
if (c.declaresMethodByName(method.getName()))
130107
return c.getMethodByName(method.getName());
131108
} else {
132-
//System.out.println(method.getSubSignature());
133109
if (c.declaresMethod(method.getSubSignature()))
134110
return c.getMethod(method.getSubSignature());
135111
}
@@ -147,29 +123,26 @@ protected SootMethod getSootMethod(Method method, boolean lookInHierarchy) {
147123
* Removes all interfaces from the given set of methods and returns the purged
148124
* set.
149125
*/
150-
private Set<Method> prefilterInterfaces(Set<Method> methods) {
151-
Set<Method> purgedMethods = new HashSet<>();
126+
private void prefilterInterfaces(Set<Method> methods) {
127+
Set<Method> abstractMethods = new HashSet<>();
152128

153129
for (Method method : methods) {
154130

155-
SootMethod sootMethod = getSootMethod(method);
156-
method.setSootMethod(sootMethod);
157-
method.setSootClass(getSootClass(method));
158-
159-
if (sootMethod == null)
160-
continue;
131+
SootMethod sootMethod = getMethod(method);
161132

162-
if (sootMethod.isAbstract())
133+
if (sootMethod == null || sootMethod.isAbstract()) {
134+
abstractMethods.add(method);
163135
logger.info("Method purged from list {}", method.getSignature());
164-
else
165-
purgedMethods.add(method);
136+
} else {
137+
method.setSootMethod(sootMethod);
138+
method.setSootClass(getClass(method));
139+
}
166140
}
167-
168-
logger.info("{} methods purged down to {}", methods.size(), purgedMethods.size());
169-
return purgedMethods;
141+
methods.removeAll(abstractMethods);
142+
logger.info("{} abstract methods removed, {} remaining methods", abstractMethods.size(), methods.size());
170143
}
171144

172-
public Set<Method> loadMethodsFromTestLib(Set<String> testClasses) {
145+
public Set<Method> loadMethods(Set<String> testClasses) {
173146
Set<Method> methods = new HashSet<>();
174147

175148
for (String className : testClasses) {

swan-pipeline/src/main/java/de/fraunhofer/iem/swan/util/SootUtils.java renamed to swan-pipeline/src/main/java/de/fraunhofer/iem/swan/soot/SootUtils.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package de.fraunhofer.iem.swan.util;
1+
package de.fraunhofer.iem.swan.soot;
22

33
import de.fraunhofer.iem.swan.data.Method;
44
import java.util.Arrays;

0 commit comments

Comments
 (0)