Repository navigation
Expand file tree
/
Copy pathTryChecker.java
More file actions
226 lines (207 loc) · 10.8 KB
/
Copy pathTryChecker.java
File metadata and controls
226 lines (207 loc) · 10.8 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
package liquidjava.processor.refinement_checker;
import java.util.IdentityHashMap;
import java.util.List;
import java.util.Map;
import liquidjava.processor.context.Context;
import liquidjava.processor.context.RefinedVariable;
import liquidjava.processor.context.Variable;
import liquidjava.processor.context.VariableInstance;
import liquidjava.rj_language.Predicate;
import liquidjava.utils.GeneratedNames;
import liquidjava.utils.constants.Formats;
import spoon.reflect.code.CtCatch;
import spoon.reflect.code.CtExpression;
import spoon.reflect.code.CtInvocation;
import spoon.reflect.code.CtLocalVariable;
import spoon.reflect.code.CtResource;
import spoon.reflect.code.CtTry;
import spoon.reflect.code.CtTryWithResource;
import spoon.reflect.code.CtVariableRead;
import spoon.reflect.declaration.CtElement;
import spoon.reflect.factory.Factory;
import spoon.reflect.reference.CtExecutableReference;
import spoon.reflect.reference.CtTypeReference;
/**
* Checks try statements. The try block and its catch blocks are combined like an if-else-if chain with unknown
* conditions. An exception may leave the try block after any of its statements, so each catch block starts with every
* variable in any of the states it had before or during the try block. The finally block also runs after returns and
* uncaught exceptions, so it starts with any of the states from the try and catch blocks.
*/
class TryChecker {
private final RefinementTypeChecker rtc;
private final Context context;
private final VCChecker vcChecker;
private final Factory factory;
TryChecker(RefinementTypeChecker rtc, Context context, VCChecker vcChecker, Factory factory) {
this.rtc = rtc;
this.context = context;
this.vcChecker = vcChecker;
this.factory = factory;
}
void visitTry(CtTry tryBlock) {
// last instance of each variable before the try statement, or null if it has none
Map<Variable, VariableInstance> before = new IdentityHashMap<>();
for (RefinedVariable rv : context.getCtxVars())
if (rv instanceof Variable v)
before.put(v, v.getLastInstance().orElse(null));
List<RefinedVariable> pathBefore = vcChecker.getPathVariables();
context.startRecordingInstances();
Map<Variable, List<VariableInstance>> inTryAndCatches;
try {
visitBranches(tryBlock, 0, before, null);
} finally {
inTryAndCatches = context.stopRecordingInstances();
}
if (tryBlock.getFinalizer() != null)
visitFinally(tryBlock, before, inTryAndCatches, pathBefore);
}
/**
* Visits the try block (branch 0) and the catch blocks (branches 1..n) from branch {@code i} like an if-else-if
* chain, combining each variable after it from the branches that can complete normally
*
* @return whether some branch from {@code i} can complete normally
*/
private boolean visitBranches(CtTry tryBlock, int i, Map<Variable, VariableInstance> before,
Map<Variable, List<VariableInstance>> inTry) {
if (i == tryBlock.getCatchers().size())
return visitBranch(tryBlock, i, before, inTry);
String branch = i == 0 ? GeneratedNames.TRY_BRANCH : GeneratedNames.CATCH_BRANCH;
String pathVarName = GeneratedNames.of(branch, context.getCounter());
Predicate cond = Predicate.createVar(pathVarName);
RefinedVariable pathVar = context.addInstanceToContext(pathVarName, factory.Type().BOOLEAN_PRIMITIVE,
new Predicate(), tryBlock);
vcChecker.addPathVariable(pathVar);
context.variablesNewIfCombination();
context.variablesSetBeforeIf();
context.enterContext();
// path conditions from a branch hold neither in the next ones, which may start before it ends, nor after them
List<RefinedVariable> pathBefore = vcChecker.getPathVariables();
context.enterContext();
if (i == 0)
context.startRecordingInstances();
boolean thenCompletes;
try {
thenCompletes = visitBranch(tryBlock, i, before, inTry);
} finally {
if (i == 0)
inTry = context.stopRecordingInstances();
}
if (thenCompletes)
context.variablesSetThenIf();
context.exitContext();
vcChecker.restorePathVariables(pathBefore);
context.newRefinementToVariableInContext(pathVarName, cond.negate());
context.enterContext();
boolean elseCompletes = visitBranches(tryBlock, i + 1, before, inTry);
if (elseCompletes)
context.variablesSetElseIf();
context.exitContext();
vcChecker.restorePathVariables(pathBefore);
if (thenCompletes == elseCompletes) {
context.newRefinementToVariableInContext(pathVarName, new Predicate()); // either branch may have run
vcChecker.removePathVariable(pathVar);
} else {
context.newRefinementToVariableInContext(pathVarName, thenCompletes ? cond : cond.negate());
}
context.exitContext();
context.variablesCombineFromIf(cond);
context.variablesFinishIfCombination();
return thenCompletes || elseCompletes;
}
/** @return whether branch {@code i} can complete normally */
private boolean visitBranch(CtTry tryBlock, int i, Map<Variable, VariableInstance> before,
Map<Variable, List<VariableInstance>> inTry) {
if (i == 0) {
if (tryBlock instanceof CtTryWithResource tryWithResource)
visitResourcesAndBody(tryWithResource);
else
rtc.scan(tryBlock.getBody());
return rtc.canCompleteNormally(tryBlock.getBody());
}
CtCatch catcher = tryBlock.getCatchers().get(i - 1);
addAnyOfInstances(before, inTry, catcher);
rtc.scan(catcher);
return rtc.canCompleteNormally(catcher.getBody());
}
/**
* Visits the finally block from any of the states of the try statement, then continues from the states where the
* try statement completed normally, updated with the variables changed in the finally block
*/
private void visitFinally(CtTry tryBlock, Map<Variable, VariableInstance> before,
Map<Variable, List<VariableInstance>> inTryAndCatches, List<RefinedVariable> pathBefore) {
List<RefinedVariable> pathAfterTry = vcChecker.getPathVariables();
vcChecker.restorePathVariables(pathBefore);
context.enterContext();
addAnyOfInstances(before, inTryAndCatches, tryBlock.getFinalizer());
context.startRecordingInstances();
Map<Variable, List<VariableInstance>> inFinally;
try {
rtc.scan(tryBlock.getFinalizer());
} finally {
inFinally = context.stopRecordingInstances();
}
inFinally.keySet().retainAll(before.keySet());
Map<Variable, VariableInstance> lastInFinally = new IdentityHashMap<>();
inFinally.keySet().forEach(v -> v.getLastInstance().ifPresent(vi -> lastInFinally.put(v, vi)));
context.exitContext();
lastInFinally.forEach((v, vi) -> context.addRefinementInstanceToVariable(v.getName(), vi.getName()));
// the path conditions of the normal completion hold again, except the ones on variables changed in the finally
for (RefinedVariable rv : pathAfterTry)
if (!pathBefore.contains(rv))
vcChecker.addPathVariable(rv);
for (Variable v : inFinally.keySet()) {
vcChecker.removePathVariableThatIncludes(v.getName());
if (before.get(v) != null)
vcChecker.removePathVariableThatIncludes(before.get(v).getName());
inTryAndCatches.getOrDefault(v, List.of())
.forEach(vi -> vcChecker.removePathVariableThatIncludes(vi.getName()));
}
}
/** Gives each variable in {@code recorded} an instance that may be its instance before or any recorded one */
private void addAnyOfInstances(Map<Variable, VariableInstance> before,
Map<Variable, List<VariableInstance>> recorded, CtElement element) {
recorded.forEach((v, instances) -> {
if (!before.containsKey(v)) // declared inside the try statement
return;
String name = String.format(Formats.INSTANCE, v.getName(), context.getCounter());
Predicate anyOf = new Predicate(); // unknown if the variable had no instance before
if (before.get(v) != null) {
anyOf = before.get(v).getRenamedRefinements(name);
for (VariableInstance vi : instances)
anyOf = Predicate.createDisjunction(anyOf, vi.getRenamedRefinements(name));
}
// a recorded instance may refer to values of a block that ended (e.g. a loop body checked again), which
// are no longer known: the variable is then unknown
if (anyOf.getVariableNames().stream().anyMatch(n -> !n.equals(name) && !context.hasVariable(n)))
anyOf = new Predicate();
context.addInstanceToContext(name, v.getType(), anyOf, element);
context.addRefinementInstanceToVariable(v.getName(), name);
});
}
/** Visits the resources and the body of a try-with-resources, which ends by closing the resources */
private void visitResourcesAndBody(CtTryWithResource tryWithResource) {
// a resource is either a declaration (`try (R r = ...)`) or a reference to an existing variable (Java 9 `try
// (r)`)
List<CtResource<?>> resources = tryWithResource.getResources();
rtc.scan(resources);
rtc.scan(tryWithResource.getBody());
// the resources are closed when the body ends, in reverse order, before any catch or finally block runs
for (int i = resources.size() - 1; i >= 0; i--)
rtc.scan(createImplicitClose(resources.get(i), tryWithResource));
}
/** Builds the {@code resource.close()} that Java inserts at the end of a try-with-resources block */
private CtInvocation<?> createImplicitClose(CtResource<?> resource, CtTryWithResource tryWithResource) {
CtExpression<?> target = resource instanceof CtLocalVariable<?> variable
? factory.Code().createVariableRead(variable.getReference(), false)
: ((CtVariableRead<?>) resource).clone();
CtTypeReference<?> type = target.getType();
CtExecutableReference<?> close = type.getAllExecutables().stream()
.filter(e -> e.getSimpleName().equals("close") && e.getParameters().isEmpty()).findFirst()
.orElseGet(() -> factory.Executable().createReference(type, factory.Type().VOID_PRIMITIVE, "close"));
CtInvocation<?> invocation = factory.Code().createInvocation(target, close);
invocation.setParent(tryWithResource);
invocation.setPosition(resource.getPosition());
target.setPosition(resource.getPosition());
return invocation;
}
}