Skip to content

Commit 7457ae9

Browse files
Add ASM and files for generating original and sliced bytecode
1 parent 07d7ab3 commit 7457ae9

4 files changed

Lines changed: 139 additions & 0 deletions

File tree

.idea/modules/SIMVA-SoS.iml

Lines changed: 6 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/new main/Slicing/SIMVA-SoS.Slicing.iml

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,5 +8,59 @@
88
</content>
99
<orderEntry type="inheritedJdk" />
1010
<orderEntry type="sourceFolder" forTests="false" />
11+
<orderEntry type="module-library">
12+
<library>
13+
<CLASSES>
14+
<root url="jar://$USER_HOME$/workspace/ASMtest1/asm-6.0/lib/asm-6.0.jar!/" />
15+
</CLASSES>
16+
<JAVADOC />
17+
<SOURCES />
18+
</library>
19+
</orderEntry>
20+
<orderEntry type="module-library">
21+
<library>
22+
<CLASSES>
23+
<root url="jar://$USER_HOME$/workspace/ASMtest1/asm-6.0/lib/asm-analysis-6.0.jar!/" />
24+
</CLASSES>
25+
<JAVADOC />
26+
<SOURCES />
27+
</library>
28+
</orderEntry>
29+
<orderEntry type="module-library">
30+
<library>
31+
<CLASSES>
32+
<root url="jar://$USER_HOME$/workspace/ASMtest1/asm-6.0/lib/asm-commons-6.0.jar!/" />
33+
</CLASSES>
34+
<JAVADOC />
35+
<SOURCES />
36+
</library>
37+
</orderEntry>
38+
<orderEntry type="module-library">
39+
<library>
40+
<CLASSES>
41+
<root url="jar://$USER_HOME$/workspace/ASMtest1/asm-6.0/lib/asm-tree-6.0.jar!/" />
42+
</CLASSES>
43+
<JAVADOC />
44+
<SOURCES />
45+
</library>
46+
</orderEntry>
47+
<orderEntry type="module-library">
48+
<library>
49+
<CLASSES>
50+
<root url="jar://$USER_HOME$/workspace/ASMtest1/asm-6.0/lib/asm-util-6.0.jar!/" />
51+
</CLASSES>
52+
<JAVADOC />
53+
<SOURCES />
54+
</library>
55+
</orderEntry>
56+
<orderEntry type="module-library">
57+
<library>
58+
<CLASSES>
59+
<root url="jar://$USER_HOME$/workspace/ASMtest1/asm-6.0/lib/asm-xml-6.0.jar!/" />
60+
</CLASSES>
61+
<JAVADOC />
62+
<SOURCES />
63+
</library>
64+
</orderEntry>
1165
</component>
1266
</module>
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import java.io.File;
2+
import java.io.IOException;
3+
import java.io.PrintWriter;
4+
import org.objectweb.asm.ClassReader;
5+
import org.objectweb.asm.util.ASMifier;
6+
import org.objectweb.asm.util.TraceClassVisitor;
7+
8+
public class OriginalBytecodeGen {
9+
public void genOriginalBytecode (String[] args) throws IOException {
10+
for (String arg: args) {
11+
new ClassReader(OriginalBytecodeGen.class.getResourceAsStream((arg+".class")))
12+
.accept(new TraceClassVisitor(null, new ASMifier(), new PrintWriter(new File((arg+".txt")))), 0);
13+
}
14+
}
15+
}
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
import java.io.*;
2+
import java.util.HashMap;
3+
import java.util.HashSet;
4+
5+
/**
6+
* @author Jiyoung Song
7+
* This class generates .java files that getnerate sliced bytecode
8+
*/
9+
public class SlicedBytecodeGen {
10+
11+
/**
12+
* @param slicedFile - the name of sliced file generated from hammacher's javaslicer
13+
* @return slicedLinesInFile - hashmap that contains sliced line numbers for every class and method
14+
* @throws IOException
15+
*/
16+
public HashMap<String, HashSet<Integer>> getSlicedLinesPerFile (String slicedFile) throws IOException{
17+
HashMap<String, HashSet<Integer>> slicedLinesInFile = new HashMap<String, HashSet<Integer>>();
18+
BufferedReader bufferedReader = new BufferedReader(new FileReader(new File((slicedFile+".txt"))));
19+
String line = "";
20+
while ((line = bufferedReader.readLine()) != null) {
21+
String[] classNameAndLineNum = line.split(":");
22+
String[] lineNumAndInst = classNameAndLineNum[1].split(" ");
23+
if (slicedLinesInFile.containsKey(classNameAndLineNum[0])) {
24+
HashSet<Integer> lineNumbers = slicedLinesInFile.get(classNameAndLineNum[0]);
25+
lineNumbers.add(Integer.parseInt(lineNumAndInst[0]));
26+
} else {
27+
HashSet<Integer> lineNumbers = new HashSet<Integer>();
28+
lineNumbers.add(Integer.parseUnsignedInt(lineNumAndInst[0]));
29+
slicedLinesInFile.put(classNameAndLineNum[0],lineNumbers);
30+
}
31+
}
32+
return slicedLinesInFile;
33+
}
34+
35+
/**
36+
*
37+
* @param slicedFile
38+
* @param subjectFiles - the name list of files that is target of slicing
39+
* @throws IOException
40+
*/
41+
public void genSlicedClassFileGenerator (String slicedFile, String[] subjectFiles) throws IOException {
42+
for(String subjectFile: subjectFiles) {
43+
PrintWriter printWriter = new PrintWriter(new File((subjectFile+".java")));
44+
printWriter.println("import java.io.FileOutputStream;");
45+
BufferedReader bufferedReader = new BufferedReader(new FileReader(new File((subjectFile+".txt"))));
46+
boolean isSliced = true;
47+
String line = "";
48+
while ((line = bufferedReader.readLine()) != null) {
49+
if (line.contains("public static byte[] dump () throws Exception {")) {
50+
printWriter.println("public static void main(final String args[]) throws Exception {");
51+
printWriter.println("FileOutputStream fos = new FileOutputStream(\"SlicingExample.class\");");
52+
printWriter.println("fos.write(dump());");
53+
printWriter.println("fos.close();");
54+
printWriter.println("}");
55+
isSliced = true;
56+
} else if (line.contains("mv.visitLineNumber")) {
57+
58+
}
59+
if (isSliced)
60+
printWriter.println(line);
61+
}
62+
}
63+
}
64+
}

0 commit comments

Comments
 (0)