-
Notifications
You must be signed in to change notification settings - Fork 578
/
ResourceValueAnalysis.java
235 lines (201 loc) · 10.3 KB
/
ResourceValueAnalysis.java
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
/*
* Bytecode Analysis Framework
* Copyright (C) 2003-2005 University of Maryland
*
* This library 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 library 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
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
package edu.umd.cs.findbugs.ba;
import org.apache.bcel.Const;
import org.apache.bcel.generic.IFNONNULL;
import org.apache.bcel.generic.IFNULL;
import org.apache.bcel.generic.IF_ACMPEQ;
import org.apache.bcel.generic.IF_ACMPNE;
import org.apache.bcel.generic.Instruction;
import org.apache.bcel.generic.InstructionHandle;
import org.apache.bcel.generic.MethodGen;
import edu.umd.cs.findbugs.SystemProperties;
@javax.annotation.ParametersAreNonnullByDefault
public class ResourceValueAnalysis<Resource> extends FrameDataflowAnalysis<ResourceValue, ResourceValueFrame> implements
EdgeTypes {
private static final boolean DEBUG = SystemProperties.getBoolean("dataflow.debug");
private final MethodGen methodGen;
private final CFG cfg;
private final ResourceTracker<Resource> resourceTracker;
private final Resource resource;
private final ResourceValueFrameModelingVisitor visitor;
private final boolean ignoreImplicitExceptions;
public ResourceValueAnalysis(MethodGen methodGen, CFG cfg, DepthFirstSearch dfs, ResourceTracker<Resource> resourceTracker,
Resource resource) {
super(dfs);
this.methodGen = methodGen;
this.cfg = cfg;
this.resourceTracker = resourceTracker;
this.resource = resource;
this.visitor = resourceTracker.createVisitor(resource, methodGen.getConstantPool());
this.ignoreImplicitExceptions = resourceTracker.ignoreImplicitExceptions(resource);
}
@Override
public ResourceValueFrame createFact() {
ResourceValueFrame fact = new ResourceValueFrame(methodGen.getMaxLocals());
fact.setTop();
return fact;
}
@Override
public void initEntryFact(ResourceValueFrame result) {
result.setValid();
result.clearStack();
final int numSlots = result.getNumSlots();
for (int i = 0; i < numSlots; ++i) {
boolean slotContainsInstance = resourceTracker.isParamInstance(resource, i);
result.setValue(i, slotContainsInstance ? ResourceValue.instance() : ResourceValue.notInstance());
}
}
@Override
public void meetInto(ResourceValueFrame fact, Edge edge, ResourceValueFrame result) throws DataflowAnalysisException {
BasicBlock source = edge.getSource();
BasicBlock dest = edge.getTarget();
ResourceValueFrame tmpFact = null;
if (edge.isExceptionEdge()) {
// If this edge throws only implicit exceptions
// (as determined by TypeAnalysis and
// PruneInfeasibleExceptionEdges),
// and the resource tracker says to ignore implicit exceptions
// for this resource, ignore it.
if (AnalysisContext.currentAnalysisContext().getBoolProperty(AnalysisFeatures.ACCURATE_EXCEPTIONS)
&& ignoreImplicitExceptions && !edge.isFlagSet(EXPLICIT_EXCEPTIONS_FLAG)) {
return;
}
// The ResourceTracker may veto the exception edge
if (resourceTracker.ignoreExceptionEdge(edge, resource, methodGen.getConstantPool())) {
return;
}
if (fact.getStatus() == ResourceValueFrame.State.OPEN) {
// If status is OPEN, downgrade to OPEN_ON_EXCEPTION_PATH
tmpFact = modifyFrame(fact, null);
tmpFact.setStatus(ResourceValueFrame.State.OPEN_ON_EXCEPTION_PATH);
}
if (fact.isValid()) {
// Special case: if the instruction that closes the resource
// throws an exception, we consider the resource to be
// successfully
// closed anyway.
InstructionHandle exceptionThrower = source.getExceptionThrower();
BasicBlock fallThroughSuccessor = cfg.getSuccessorWithEdgeType(source, FALL_THROUGH_EDGE);
if (DEBUG && fallThroughSuccessor == null) {
System.out.println("Null fall through successor!");
}
if (fallThroughSuccessor != null
&& resourceTracker.isResourceClose(fallThroughSuccessor, exceptionThrower, methodGen.getConstantPool(),
resource, fact)) {
tmpFact = modifyFrame(fact, tmpFact);
tmpFact.setStatus(ResourceValueFrame.State.CLOSED);
if (DEBUG) {
System.out.print("(failed attempt to close)");
}
}
}
if (dest.isExceptionHandler()) {
// Clear stack, push value for exception
if (fact.isValid()) {
tmpFact = modifyFrame(fact, tmpFact);
tmpFact.clearStack();
tmpFact.pushValue(ResourceValue.notInstance());
}
}
}
// Make the resource nonexistent if it is compared against null
int edgeType = edge.getType();
if (edgeType == IFCMP_EDGE || edgeType == FALL_THROUGH_EDGE) {
InstructionHandle lastInSourceHandle = source.getLastInstruction();
if (lastInSourceHandle != null) {
Instruction lastInSource = lastInSourceHandle.getInstruction();
boolean isNullCheck = false;
boolean isNonNullCheck = false;
// This check catches null == X, null != X
if (lastInSource instanceof IF_ACMPEQ || lastInSource instanceof IF_ACMPNE) {
Location l = new Location(lastInSourceHandle, source);
InstructionHandle ih = l.getHandle();
// Get instruction that pushed topmost
InstructionHandle ihPrev = ih.getPrev();
// Get next-topmost that pushed next-topmost
InstructionHandle ihPrevPrev = ihPrev == null ? null : ihPrev.getPrev();
int prevPush = 0;
if (ihPrev != null) {
prevPush = ihPrev.getInstruction().produceStack(methodGen.getConstantPool());
}
int prevPrevPush = 0;
if (ihPrevPrev != null) {
prevPrevPush = ihPrevPrev.getInstruction().produceStack(methodGen.getConstantPool());
}
// If instructions exist and both push one word onto the
// stack and the next-topmost pushes null...
if (ihPrev != null && ihPrevPrev != null && prevPush == 1 && prevPrevPush == 1
&& ihPrevPrev.getInstruction().getOpcode() == Const.ACONST_NULL) {
// Topmost item on stack is being compared with null
// (the null itself is next-topmost on the stack)
isNullCheck = lastInSource instanceof IF_ACMPEQ;
isNonNullCheck = lastInSource instanceof IF_ACMPNE;
}
}
// This check catches X == null, X != null
else if (lastInSource instanceof IFNULL || lastInSource instanceof IFNONNULL) {
isNullCheck = lastInSource instanceof IFNULL;
isNonNullCheck = lastInSource instanceof IFNONNULL;
}
if (isNullCheck || isNonNullCheck) {
// Get the frame at the if statement
ResourceValueFrame startFrame = getStartFact(source);
if (startFrame.isValid()) {
// The source block has a valid start fact.
// That means it is safe to inspect the frame at the If
// instruction.
ResourceValueFrame frameAtIf = getFactAtLocation(new Location(lastInSourceHandle, source));
ResourceValue topValue = frameAtIf.getValue(frameAtIf.getNumSlots() - 1);
if (topValue.isInstance()) {
if ((isNullCheck && edgeType == IFCMP_EDGE) || (isNonNullCheck && edgeType == FALL_THROUGH_EDGE)) {
// System.out.println("**** making resource nonexistent on edge "+edge.getId());
tmpFact = modifyFrame(fact, tmpFact);
tmpFact.setStatus(ResourceValueFrame.State.NONEXISTENT);
}
}
}
}
}
}
if (tmpFact != null) {
fact = tmpFact;
}
mergeInto(fact, result);
}
@Override
protected void mergeInto(ResourceValueFrame frame, ResourceValueFrame result) throws DataflowAnalysisException {
// Merge slots
super.mergeInto(frame, result);
// Merge status
result.setStatus(result.getStatus().getType() > frame.getStatus().getType() ? frame.getStatus() : result.getStatus());
}
@Override
protected void mergeValues(ResourceValueFrame otherFrame, ResourceValueFrame resultFrame, int slot)
throws DataflowAnalysisException {
ResourceValue value = ResourceValue.merge(resultFrame.getValue(slot), otherFrame.getValue(slot));
resultFrame.setValue(slot, value);
}
@Override
public void transferInstruction(InstructionHandle handle, BasicBlock basicBlock, ResourceValueFrame fact)
throws DataflowAnalysisException {
visitor.setFrameAndLocation(fact, new Location(handle, basicBlock));
visitor.transferInstruction(handle, basicBlock);
}
}