forked from soot-oss/soot
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathAbstractSootFieldRef.java
More file actions
284 lines (253 loc) · 8.47 KB
/
AbstractSootFieldRef.java
File metadata and controls
284 lines (253 loc) · 8.47 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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
package soot;
/*-
* #%L
* Soot - a J*va Optimization Framework
* %%
* Copyright (C) 2004 Ondrej Lhotak
* %%
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as
* published by the Free Software Foundation, either version 2.1 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Lesser Public License for more details.
*
* You should have received a copy of the GNU General Lesser Public
* License along with this program. If not, see
* <http://www.gnu.org/licenses/lgpl-2.1.html>.
* #L%
*/
import java.util.ArrayDeque;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import soot.options.Options;
/**
* Representation of a reference to a field as it appears in a class file. Note that the field directly referred to may not
* actually exist; the actual target of the reference is determined according to the resolution procedure in the Java Virtual
* Machine Specification, 2nd ed, section 5.4.3.2.
*/
public class AbstractSootFieldRef implements SootFieldRef {
private static final Logger logger = LoggerFactory.getLogger(AbstractSootFieldRef.class);
public AbstractSootFieldRef(SootClass declaringClass, String name, Type type, boolean isStatic) {
this.declaringClass = declaringClass;
this.name = name;
this.type = type;
this.isStatic = isStatic;
if (declaringClass == null) {
throw new RuntimeException("Attempt to create SootFieldRef with null class");
}
if (name == null) {
throw new RuntimeException("Attempt to create SootFieldRef with null name");
}
if (type == null) {
throw new RuntimeException("Attempt to create SootFieldRef with null type");
}
}
private final SootClass declaringClass;
private final String name;
private final Type type;
private final boolean isStatic;
@Override
public SootClass declaringClass() {
return declaringClass;
}
@Override
public String name() {
return name;
}
@Override
public Type type() {
return type;
}
@Override
public boolean isStatic() {
return isStatic;
}
@Override
public String getSignature() {
return SootField.getSignature(declaringClass, name, type);
}
public class FieldResolutionFailedException extends ResolutionFailedException {
/**
*
*/
private static final long serialVersionUID = -4657113720516199499L;
public FieldResolutionFailedException() {
super("Class " + declaringClass + " doesn't have field " + name + " : " + type
+ "; failed to resolve in superclasses and interfaces");
}
@Override
public String toString() {
StringBuffer ret = new StringBuffer();
ret.append(super.toString());
resolve(ret);
return ret.toString();
}
}
@Override
public SootField resolve() {
return resolve(null);
}
private SootField checkStatic(SootField ret) {
if ((Options.v().wrong_staticness() == Options.wrong_staticness_fail
|| Options.v().wrong_staticness() == Options.wrong_staticness_fixstrict) && ret.isStatic() != isStatic()
&& !ret.isPhantom()) {
throw new ResolutionFailedException("Resolved " + this + " to " + ret + " which has wrong static-ness");
}
return ret;
}
private SootField resolve(StringBuffer trace) {
SootClass cl = declaringClass;
while (true) {
if (trace != null) {
trace.append("Looking in " + cl + " which has fields " + cl.getFields() + "\n");
}
// Check whether we have the field in the current class
SootField clField = cl.getFieldUnsafe(name, type);
if (clField != null) {
return checkStatic(clField);
}
// If we have a phantom class, we directly construct a phantom field
// in it and don't care about superclasses.
else if (Scene.v().allowsPhantomRefs() && cl.isPhantom()) {
synchronized (cl) {
// Check that no other thread has created the field in the
// meantime
clField = cl.getFieldUnsafe(name, type);
if (clField != null) {
return checkStatic(clField);
}
// Make sure that we don't have a conflicting field
SootField existingField = cl.getFieldByNameUnsafe(name);
if (existingField != null) {
return handleFieldTypeMismatch(clField);
}
// Create the phantom field
SootField f = Scene.v().makeSootField(name, type, isStatic() ? Modifier.STATIC : 0);
f.setPhantom(true);
cl.addField(f);
return f;
}
} else {
// Since this class is not phantom, we look at its interfaces
ArrayDeque<SootClass> queue = new ArrayDeque<SootClass>();
queue.addAll(cl.getInterfaces());
while (true) {
SootClass iface = queue.poll();
if (iface == null) {
break;
}
if (trace != null) {
trace.append("Looking in " + iface + " which has fields " + iface.getFields() + "\n");
}
SootField ifaceField = iface.getFieldUnsafe(name, type);
if (ifaceField != null) {
return checkStatic(ifaceField);
}
queue.addAll(iface.getInterfaces());
}
// If we have not found a suitable field in the current class,
// try the superclass
if (cl.hasSuperclass()) {
cl = cl.getSuperclass();
} else {
break;
}
}
}
// If we allow phantom refs, we construct phantom fields
if (Options.v().allow_phantom_refs()) {
SootField sf = Scene.v().makeSootField(name, type, isStatic ? Modifier.STATIC : 0);
sf.setPhantom(true);
synchronized (declaringClass) {
// Be careful: Another thread may have already created this
// field in the meantime, so better check twice.
SootField clField = declaringClass.getFieldByNameUnsafe(name);
if (clField != null) {
if (clField.getType().equals(type)) {
return checkStatic(clField);
} else {
return handleFieldTypeMismatch(clField);
}
} else {
// Add the new phantom field
declaringClass.addField(sf);
return sf;
}
}
}
if (trace == null) {
FieldResolutionFailedException e = new FieldResolutionFailedException();
if (Options.v().ignore_resolution_errors()) {
logger.debug("" + e.getMessage());
} else {
throw e;
}
}
return null;
}
protected SootField handleFieldTypeMismatch(SootField clField) {
switch (Options.v().field_type_mismatches()) {
case Options.field_type_mismatches_fail:
throw new ConflictingFieldRefException(clField, type);
case Options.field_type_mismatches_ignore:
return checkStatic(clField);
case Options.field_type_mismatches_null:
return null;
}
throw new RuntimeException(
String.format("Unsupported option for handling field type mismatches: %d", Options.v().field_type_mismatches()));
}
@Override
public String toString() {
return getSignature();
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((declaringClass == null) ? 0 : declaringClass.hashCode());
result = prime * result + (isStatic ? 1231 : 1237);
result = prime * result + ((name == null) ? 0 : name.hashCode());
result = prime * result + ((type == null) ? 0 : type.hashCode());
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj) {
return true;
}
if ((obj == null) || (getClass() != obj.getClass())) {
return false;
}
AbstractSootFieldRef other = (AbstractSootFieldRef) obj;
if (declaringClass == null) {
if (other.declaringClass != null) {
return false;
}
} else if (!declaringClass.equals(other.declaringClass)) {
return false;
}
if (isStatic != other.isStatic) {
return false;
}
if (name == null) {
if (other.name != null) {
return false;
}
} else if (!name.equals(other.name)) {
return false;
}
if (type == null) {
if (other.type != null) {
return false;
}
} else if (!type.equals(other.type)) {
return false;
}
return true;
}
}