-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAnderson.java
More file actions
185 lines (162 loc) · 5.15 KB
/
Anderson.java
File metadata and controls
185 lines (162 loc) · 5.15 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
package pta;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.HashSet;
class AssignConstraint {
APointer from, to;
AssignConstraint(APointer from, APointer to) {
this.from = from;
this.to = to;
}
@Override
public int hashCode() {
return (from.hashString() + "?" + to.hashString()).hashCode();
}
@Override
public boolean equals(Object o) {
if (this == o)
return true;
if (o == null || getClass() != o.getClass())
return false;
AssignConstraint that = (AssignConstraint) o;
return from.equals(that.from) && to.equals(that.to);
}
}
class NewConstraint {
APointer to;
APointer allocId;
NewConstraint(APointer allocId, APointer to) {
this.allocId = allocId;
this.to = to;
}
@Override
public int hashCode() {
return (allocId.hashString() + "?" + to.hashString()).hashCode();
}
@Override
public boolean equals(Object o) {
if (this == o)
return true;
if (o == null || getClass() != o.getClass())
return false;
NewConstraint that = (NewConstraint) o;
return allocId.equals(that.allocId) && to.equals(that.to);
}
}
public class Anderson {
public static boolean useContext = true;
private ArrayList<AssignConstraint> assignConstraintList = new ArrayList<AssignConstraint>();
private ArrayList<NewConstraint> newConstraintList = new ArrayList<NewConstraint>();
Map<APointer, HashSet<APointer>> pts = new HashMap<APointer, HashSet<APointer>>();
Map<String, HashSet<String>> cons = new HashMap<String, HashSet<String>>();
public boolean inPts(APointer thi) {
return pts.containsKey(thi);
}
public boolean mergePts(APointer from, APointer to) {
return pts.get(to).addAll(pts.get(from));
}
public boolean updateAssign(APointer from, APointer to) {
// System.out.println("from: " + from + ", to:" + to +
// ", fromSetDe: " + pts.get(from.deField()) +
// ", toSetDe:" + pts.get(to.deField())); // debug
if (!inPts(from.deField())) {
pts.put(from.deField(), new HashSet<APointer>());
return false;
}
if (!inPts(to.deField())) {
pts.put(to.deField(), new HashSet<APointer>());
}
if (to.field == null && from.field == null) {
return mergePts(from, to);
} else if (to.field != null) {
boolean flag = false;
for (APointer l : pts.get(to.deField())) {
APointer p = new APointer(l.id, to.field, l.indexlv, "heap");
// System.out.println("from: " + from + ", p: " + p + ", fromSet: " +
// pts.get(from) + ", pSet:" + pts.get(p)); // debug
if (!inPts(p))
pts.put(p, new HashSet<APointer>());
flag |= mergePts(from, p);
}
return flag;
} else if (from.field != null) {
boolean flag = false;
HashSet<APointer> tmpHS = (HashSet<APointer>) pts.get(from.deField()).clone();
for (APointer r : tmpHS) {
APointer q = new APointer((int) r.id, from.field, r.indexlv, "heap");
// System.out.println("q: " + q + ", to: " + to + ", qSet: " + pts.get(q) + ",
// toSet:" + pts.get(to)); // debug
if (!inPts(q))
pts.put(q, new HashSet<APointer>());
flag |= mergePts(q, to);
}
return flag;
}
System.out.println(BashColor.ANSI_RED +
"Anderson::updateAssign Invalid Constraint, From = " + from +
", To = " + to + BashColor.ANSI_RESET);
return false;
}
void addAssignConstraint(APointer from, APointer to) {
// if(from.name.contains("test.FieldSensitivity")||from.name.contains("benchmark.objects.A")
// ||from.name.contains("benchmark.objects.B")) {
// System.out.println(BashColor.ANSI_RED+
// "Fvar: "+from.name +BashColor.ANSI_RESET);
// System.out.println(BashColor.ANSI_GREEN+
// "Fcontext: "+from.context+BashColor.ANSI_RESET);
// }
// if(to.name.contains("test.FieldSensitivity")||to.name.contains("benchmark.objects.A")
// ||to.name.contains("benchmark.objects.B")) {
// System.out.println(BashColor.ANSI_RED+
// "Tvar: "+to.name +BashColor.ANSI_RESET);
// System.out.println(BashColor.ANSI_GREEN+
// "Tcontext: "+to.context+BashColor.ANSI_RESET);
// }
assignConstraintList.add(new AssignConstraint(from, to));
}
void addNewConstraint(APointer alloc, APointer to) {
newConstraintList.add(new NewConstraint(alloc, to));
}
void run() {
System.out.println(BashColor.ANSI_YELLOW +
"Number of new constraints: " + newConstraintList.size() +
BashColor.ANSI_RESET);
System.out.println(BashColor.ANSI_YELLOW +
"Number of assign constraints: " + assignConstraintList.size() +
BashColor.ANSI_RESET);
for (NewConstraint nc : newConstraintList) {
if (!pts.containsKey(nc.to)) {
pts.put(nc.to, new HashSet<APointer>());
}
pts.get(nc.to).add(nc.allocId);
}
for (boolean flag = true; flag;) {
flag = false;
for (AssignConstraint ac : assignConstraintList) {
flag |= updateAssign(ac.from, ac.to);
}
}
}
HashSet<APointer> getPointsToSet(APointer str) {
return pts.get(str);
}
boolean isEmptyCons(String s) {
if (!cons.containsKey(s))
return true;
return cons.get(s).isEmpty();
}
void addCons(String s, String c) {
if (!useContext)
c = "qwq";
if (!cons.containsKey(s)) {
cons.put(s, new HashSet<String>());
}
cons.get(s).add(c);
}
HashSet<String> getCons(String s) {
return cons.get(s);
}
}