Skip to content

Commit 4d7cbd4

Browse files
add detectors
1 parent a9e2b3f commit 4d7cbd4

35 files changed

Lines changed: 2013 additions & 1 deletion

java-benchmarks

Submodule java-benchmarks updated 164 files
Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
package pascal.taie.analysis.bugfinder;
2+
3+
import pascal.taie.ir.IR;
4+
import pascal.taie.language.classes.JClass;
5+
import pascal.taie.language.classes.JField;
6+
import pascal.taie.language.classes.JMethod;
7+
8+
import javax.annotation.Nonnull;
9+
10+
import java.util.Objects;
11+
12+
import static java.util.Objects.requireNonNull;
13+
14+
//TODO: refactor it with more precise context information.
15+
public class BugInstance {
16+
17+
private final String type;
18+
19+
private Severity severity;
20+
21+
private JClass jClass;
22+
23+
private JMethod jMethod;
24+
25+
26+
private int sourceLineStart = -1, sourceLineEnd = -2;
27+
// private final ArrayList<BugAnnotation> annotationList;
28+
29+
public BugInstance(@Nonnull String type, Severity severity) {
30+
this.type = type.intern();
31+
this.severity = severity;
32+
}
33+
34+
public Severity getSeverity() {
35+
return severity;
36+
}
37+
38+
public String getType() {
39+
return type;
40+
}
41+
42+
private String getString(Object o) {
43+
return o == null ? "empty" :o.toString();
44+
}
45+
46+
@Override
47+
public String toString() {
48+
String sourcelineRange = "empty";
49+
if(sourceLineStart >= 0){
50+
sourcelineRange = sourceLineStart == sourceLineEnd ? String.valueOf(sourceLineStart) :
51+
sourceLineStart + "---" + sourceLineEnd;
52+
}
53+
return String.format("Class: %s, Method: %s, LineNumber: %s. \nbug type: %s, severity: %s",
54+
getString(jClass), getString(jMethod), sourcelineRange, type, severity
55+
);
56+
}
57+
58+
@Override
59+
public boolean equals(Object o) {
60+
if(o == this) return true;
61+
if(!(o instanceof BugInstance bugInstance)) return false;
62+
63+
return type.equals(bugInstance.type) && jClass == bugInstance.jClass
64+
&& jMethod == bugInstance.jMethod && sourceLineStart == bugInstance.sourceLineStart
65+
&& sourceLineEnd == bugInstance.sourceLineEnd;
66+
}
67+
68+
@Override
69+
public int hashCode() {
70+
int result = Objects.hashCode(type);
71+
result = 31 * result + Objects.hashCode(jClass);
72+
result = 31 * result + Objects.hashCode(jMethod);
73+
result = 31 * result + sourceLineStart;
74+
result = 31 * result + sourceLineEnd;
75+
return result;
76+
}
77+
78+
public static BugInstance newBugInstance(String type, Severity severity, JMethod method, int lineNum){
79+
return new BugInstance(type, severity).setClassAndMethod(method).setSourceLine(lineNum);
80+
}
81+
82+
public BugInstance setClassAndMethod(JMethod method) {
83+
setMethod(method);
84+
setClass(method.getDeclaringClass());
85+
return this;
86+
}
87+
88+
public BugInstance setClass(JClass clazz) {
89+
jClass = clazz;
90+
return this;
91+
}
92+
93+
public BugInstance setMethod(JMethod method) {
94+
jMethod = method;
95+
return this;
96+
}
97+
98+
public BugInstance setSourceLine(int num) {
99+
sourceLineStart = sourceLineEnd = num;
100+
return this;
101+
}
102+
103+
public BugInstance setSourceLine(int start, int end) {
104+
sourceLineStart = start;
105+
sourceLineEnd = end;
106+
return this;
107+
}
108+
109+
public int getSourceLineStart() {
110+
return sourceLineStart;
111+
}
112+
113+
public int getSourceLineEnd() {
114+
return sourceLineEnd;
115+
}
116+
117+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package pascal.taie.analysis.bugfinder;
2+
3+
public enum Priorities {
4+
5+
HIGH_PRIORITY(1),
6+
NORMAL_PRIORITY(2),
7+
LOW_PRIORITY(3);
8+
9+
private final int priority;
10+
11+
Priorities(int priority) { this.priority = priority; }
12+
// public static final int LOW_PRIORITY = 3;
13+
//
14+
// public static final int NORMAL_PRIORITY = 2;
15+
//
16+
// public static final int HIGH_PRIORITY = 1;
17+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package pascal.taie.analysis.bugfinder;
2+
3+
public enum Severity {
4+
5+
BLOCKER("High", "High"),
6+
CRITICAL("High", "Low"),
7+
MAJOR("Low", "High"),
8+
MINOR("Low", "Low");
9+
10+
private final String impact;
11+
private final String likelihood;
12+
13+
Severity(String impact, String likelihood) {
14+
this.impact = impact;
15+
this.likelihood = likelihood;
16+
}
17+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package pascal.taie.analysis.bugfinder.bugreport;
2+
3+
/**
4+
* Provide context information for a BugInstance
5+
*/
6+
7+
public interface BugAnnotation {
8+
public String getDescription();
9+
}
Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
package pascal.taie.analysis.bugfinder.bugreport;
2+
3+
import pascal.taie.analysis.bugfinder.Priorities;
4+
import pascal.taie.language.classes.JClass;
5+
import pascal.taie.language.classes.JField;
6+
import pascal.taie.language.classes.JMethod;
7+
8+
import javax.annotation.Nonnull;
9+
10+
import static java.util.Objects.requireNonNull;
11+
12+
//TODO: refactor it with more precise context information.
13+
public class BugInstance {
14+
15+
private final String type;
16+
17+
private Priorities priority;
18+
// use JMethod to provide more precise context information temporarily
19+
private JClass jClass;
20+
private JMethod jMethod;
21+
private JField jField;
22+
private int sourceLineNumber;
23+
// private final ArrayList<BugAnnotation> annotationList;
24+
25+
public BugInstance(@Nonnull String type, Priorities priority){
26+
this.type = type.intern();
27+
this.priority = priority;
28+
// annotationList = new ArrayList<>(4);
29+
sourceLineNumber = -1;
30+
jClass = null;
31+
jField = null;
32+
jMethod = null;
33+
}
34+
35+
public Priorities getPriority() { return priority;}
36+
37+
public String getType() { return type;}
38+
39+
private String getString(Object o){
40+
if(o == null){
41+
return "";
42+
}
43+
return o.toString();
44+
}
45+
46+
/**
47+
* For now, get the location of this BugInstance.
48+
*/
49+
public String getDescription() {
50+
// TODO: add more detailed description on it.
51+
return getString(jClass) + " " + getString(jMethod) + " " + getString(jField);
52+
}
53+
54+
public String getDescriptionWithType(){
55+
return getType() + " in " + getDescription();
56+
}
57+
58+
public BugInstance addClassAndMethod(JMethod method){
59+
addMethod(method);
60+
addClass(method.getDeclaringClass());
61+
return this;
62+
}
63+
64+
// public BugInstance addClass(String className){
65+
// return this;
66+
// }
67+
//
68+
public BugInstance addClass(JClass clazz){
69+
jClass = clazz;
70+
return this;
71+
}
72+
73+
public BugInstance addMethod(JMethod method){
74+
jMethod = method;
75+
return this;
76+
}
77+
78+
public BugInstance addField(JField field){
79+
jField = field;
80+
return this;
81+
}
82+
83+
public BugInstance addSourceLine(int lineNumber){
84+
sourceLineNumber = lineNumber;
85+
return this;
86+
}
87+
88+
public int getPrimarySourceLineNumber(){
89+
return sourceLineNumber;
90+
}
91+
// public BugInstance add(@Nonnull BugAnnotation bugAnnotation){
92+
// requireNonNull(bugAnnotation, "missing bug annotation");
93+
//
94+
// return this;
95+
// }
96+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
//package pascal.taie.analysis.bugfinder.bugreport;
2+
//
3+
//public interface BugReporter {
4+
//
5+
// public void reportBug(BugInstance bugInstance);
6+
//
7+
//}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
//package pascal.taie.analysis.bugfinder.bugreport;
2+
//
3+
//import java.util.HashSet;
4+
//
5+
//public class PrintingBugReporter extends TextBugReporter{
6+
// private final HashSet<BugInstance> seenAlready = new HashSet<>();
7+
//
8+
// // FIXME: may not assist multithreading
9+
// public void doReportBug(BugInstance bugInstance){
10+
// if(seenAlready.add(bugInstance)){
11+
// printBug(bugInstance);
12+
// }
13+
// }
14+
//}
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
//package pascal.taie.analysis.bugfinder.bugreport;
2+
//
3+
//import pascal.taie.analysis.bugfinder.Priorities;
4+
//
5+
//import javax.annotation.Nonnull;
6+
//import java.io.OutputStreamWriter;
7+
//import java.io.PrintWriter;
8+
//import java.nio.charset.StandardCharsets;
9+
//
10+
//public abstract class TextBugReporter implements BugReporter{
11+
//
12+
// private int priorityThreshold;
13+
//
14+
// protected PrintWriter outputStream = new PrintWriter(
15+
// new OutputStreamWriter(System.out, StandardCharsets.UTF_8), true
16+
// );
17+
//
18+
// public TextBugReporter(){
19+
// this.priorityThreshold = Priorities.LOW_PRIORITY;
20+
// }
21+
//
22+
// public TextBugReporter(int priorityThreshold){
23+
// this.priorityThreshold = priorityThreshold;
24+
// }
25+
//
26+
// public void setPriorityThreshold(int priorityThreshold) { this.priorityThreshold = priorityThreshold; }
27+
//
28+
// public void setOutputStream(PrintWriter printWriter) { this.outputStream = printWriter; }
29+
//
30+
// protected void printBug(BugInstance bugInstance){
31+
// // TODO: finish priorities
32+
// switch (bugInstance.getPriority()){
33+
// case Priorities.LOW_PRIORITY:
34+
// outputStream.print("L :");
35+
// break;
36+
// case Priorities.NORMAL_PRIORITY:
37+
// outputStream.print("M ");
38+
// break;
39+
// case Priorities.HIGH_PRIORITY:
40+
// outputStream.print("H ");
41+
// break;
42+
// default:
43+
// assert false;
44+
// }
45+
// outputStream.print(bugInstance.getDescriptionWithType() + "line number: "
46+
// + bugInstance.getPrimarySourceLineNumber());
47+
// }
48+
// @Override
49+
// public void reportBug(@Nonnull BugInstance bugInstance){
50+
// if(bugInstance.getPriority() <= priorityThreshold){
51+
// doReportBug(bugInstance);
52+
// }
53+
// }
54+
//
55+
// protected abstract void doReportBug(BugInstance bugInstance);
56+
//}

0 commit comments

Comments
 (0)