-
Notifications
You must be signed in to change notification settings - Fork 578
/
StreamFrameModelingVisitor.java
138 lines (118 loc) · 5.4 KB
/
StreamFrameModelingVisitor.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
/*
* FindBugs - Find bugs in Java programs
* Copyright (C) 2003,2004 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.detect;
import org.apache.bcel.Const;
import org.apache.bcel.generic.ConstantPoolGen;
import org.apache.bcel.generic.Instruction;
import org.apache.bcel.generic.InstructionHandle;
import org.apache.bcel.generic.InvokeInstruction;
import edu.umd.cs.findbugs.ba.BasicBlock;
import edu.umd.cs.findbugs.ba.DataflowAnalysisException;
import edu.umd.cs.findbugs.ba.Location;
import edu.umd.cs.findbugs.ba.ResourceValue;
import edu.umd.cs.findbugs.ba.ResourceValueFrame;
import edu.umd.cs.findbugs.ba.ResourceValueFrameModelingVisitor;
/**
* A visitor to model the effect of instructions on the status of the resource
* (in this case, Streams).
*/
public class StreamFrameModelingVisitor extends ResourceValueFrameModelingVisitor {
private final StreamResourceTracker resourceTracker;
private final Stream stream;
private Location location;
public StreamFrameModelingVisitor(ConstantPoolGen cpg, StreamResourceTracker resourceTracker, Stream stream) {
super(cpg);
this.resourceTracker = resourceTracker;
this.stream = stream;
}
@Override
public void transferInstruction(InstructionHandle handle, BasicBlock basicBlock) throws DataflowAnalysisException {
// Record what Location we are analyzing
this.location = new Location(handle, basicBlock);
final Instruction ins = handle.getInstruction();
final ResourceValueFrame frame = getFrame();
ResourceValueFrame.State status = ResourceValueFrame.State.NONEXISTENT;
boolean updated = false;
boolean created = false;
// Is a resource created, opened, or closed by this instruction?
Location creationPoint = stream.getLocation();
if (handle == creationPoint.getHandle() && basicBlock == creationPoint.getBasicBlock()) {
// Resource creation
if (stream.isOpenOnCreation()) {
status = ResourceValueFrame.State.OPEN;
stream.setOpenLocation(location);
resourceTracker.addStreamOpenLocation(location, stream);
} else {
status = ResourceValueFrame.State.CREATED;
}
updated = true;
created = true;
} else if (resourceTracker.isResourceOpen(basicBlock, handle, cpg, stream, frame)) {
// Resource opened
status = ResourceValueFrame.State.OPEN;
updated = true;
stream.setOpenLocation(location);
resourceTracker.addStreamOpenLocation(location, stream);
} else if (resourceTracker.isResourceClose(basicBlock, handle, cpg, stream, frame)) {
// Resource closed
status = ResourceValueFrame.State.CLOSED;
updated = true;
}
// Model use of instance values in frame slots
analyzeInstruction(ins);
// If needed, update frame status
if (updated) {
frame.setStatus(status);
if (created) {
frame.setValue(frame.getNumSlots() - 1, ResourceValue.instance());
}
}
}
@Override
protected boolean instanceEscapes(InvokeInstruction inv, int instanceArgNum) {
ConstantPoolGen cpg = getCPG();
String className = inv.getClassName(cpg);
// System.out.print("[Passed as arg="+instanceArgNum+" at " + inv +
// "]");
boolean escapes = (inv.getOpcode() == Const.INVOKESTATIC || instanceArgNum != 0);
String methodName = inv.getMethodName(cpg);
String methodSig = inv.getSignature(cpg);
if (inv.getOpcode() == Const.INVOKEVIRTUAL
&& ("load".equals(methodName) || "loadFromXml".equals(methodName) || "store".equals(methodName) || "save".equals(methodName))
&& "java.util.Properties".equals(className)) {
escapes = false;
}
if (inv.getOpcode() == Const.INVOKEVIRTUAL && ("load".equals(methodName) || "store".equals(methodName))
&& "java.security.KeyStore".equals(className)) {
escapes = false;
}
if (inv.getOpcode() == Const.INVOKEVIRTUAL && "getChannel".equals(methodName)
&& "()Ljava/nio/channels/FileChannel;".equals(methodSig)) {
escapes = true;
}
if (FindOpenStream.DEBUG && escapes) {
System.out.println("ESCAPE at " + location + " at call to " + className + "." + methodName + ":" + methodSig);
}
// Record the fact that this might be a stream escape
if (stream.getOpenLocation() != null) {
resourceTracker.addStreamEscape(stream, location);
}
return escapes;
}
}