forked from typetools/checker-framework
-
Notifications
You must be signed in to change notification settings - Fork 0
/
AliasingVisitor.java
288 lines (270 loc) · 13.6 KB
/
AliasingVisitor.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
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
285
286
287
288
package org.checkerframework.common.aliasing;
import com.sun.source.tree.ExpressionTree;
import com.sun.source.tree.MethodInvocationTree;
import com.sun.source.tree.MethodTree;
import com.sun.source.tree.NewArrayTree;
import com.sun.source.tree.ThrowTree;
import com.sun.source.tree.Tree;
import com.sun.source.tree.Tree.Kind;
import com.sun.source.tree.VariableTree;
import com.sun.source.util.TreePath;
import java.util.List;
import javax.lang.model.element.ExecutableElement;
import javax.lang.model.element.VariableElement;
import org.checkerframework.checker.compilermsgs.qual.CompilerMessageKey;
import org.checkerframework.common.aliasing.qual.LeakedToResult;
import org.checkerframework.common.aliasing.qual.NonLeaked;
import org.checkerframework.common.aliasing.qual.Unique;
import org.checkerframework.common.basetype.BaseTypeChecker;
import org.checkerframework.common.basetype.BaseTypeVisitor;
import org.checkerframework.framework.type.AnnotatedTypeMirror;
import org.checkerframework.framework.type.AnnotatedTypeMirror.AnnotatedArrayType;
import org.checkerframework.framework.type.AnnotatedTypeMirror.AnnotatedDeclaredType;
import org.checkerframework.framework.type.AnnotatedTypeMirror.AnnotatedExecutableType;
import org.checkerframework.javacutil.TreeUtils;
/**
* This visitor ensures that every constructor whose result is annotated as {@literal @}Unique does
* not leak aliases.
*
* <p>TODO: Implement {@literal @}NonLeaked and {@literal @}LeakedToResult verifications:
*
* <p>{@literal @}NonLeaked: When a method declaration has a parameter annotated as
* {@literal @}NonLeaked, the method body must not leak a reference to that parameter.
*
* <p>{@literal @}LeakedToResult: When a method declaration has a parameter annotated as
* {@literal @}LeakedToResult, the method body must not leak a reference to that parameter, except
* at the method return statements.
*
* <p>Both of the checks above are similar to the @Unique check that is implemented in this visitor.
*/
public class AliasingVisitor extends BaseTypeVisitor<AliasingAnnotatedTypeFactory> {
public AliasingVisitor(BaseTypeChecker checker) {
super(checker);
}
/**
* Checks that if a method call is being invoked inside a constructor with result type
* {@literal @}Unique, it must not leak the "this" reference. There are 3 ways to make sure that
* this is not happening:
*
* <ol>
* <li>{@code this} is not an argument of the method call.
* <li>{@code this} is an argument of the method call, but the respective parameter is
* annotated as {@literal @}NonLeaked.
* <li>{@code this} is an argument of the method call, but the respective parameter is
* annotated as {@literal @}LeakedToResult AND the result of the method call is not being
* stored (the method call is a statement).
* </ol>
*
* The private method {@code isUniqueCheck} handles cases 2 and 3.
*/
@Override
public Void visitMethodInvocation(MethodInvocationTree node, Void p) {
// The check only needs to be done for constructors with result type
// @Unique. We also want to avoid visiting the <init> method.
if (isInUniqueConstructor()) {
if (TreeUtils.isSuperConstructorCall(node)) {
// Check if a call to super() might create an alias: that
// happens when the parent's respective constructor is not @Unique.
AnnotatedTypeMirror superResult = atypeFactory.getAnnotatedType(node);
if (!superResult.hasAnnotation(Unique.class)) {
checker.reportError(node, "unique.leaked");
}
} else {
// TODO: Currently the type of "this" doesn't always return
// the type of the constructor result, therefore we need
// this "else" block. Once constructors are implemented
// correctly we could remove that code below, since the type
// of "this" in a @Unique constructor will be @Unique.
Tree parent = getCurrentPath().getParentPath().getLeaf();
boolean parentIsStatement = parent.getKind() == Kind.EXPRESSION_STATEMENT;
ExecutableElement methodElement = TreeUtils.elementFromUse(node);
List<? extends VariableElement> params = methodElement.getParameters();
List<? extends ExpressionTree> args = node.getArguments();
assert (args.size() == params.size())
: "Number of arguments in"
+ " the method call "
+ node
+ " is different from the "
+ "number of parameters for the method declaration: "
+ methodElement.getSimpleName();
for (int i = 0; i < args.size(); i++) {
// Here we are traversing the arguments of the method call.
// For every argument we check if it is a reference to "this".
if (TreeUtils.isExplicitThisDereference(args.get(i))) {
// If it is a reference to "this", there is still hope that
// it is not being leaked (2. and 3. from the javadoc).
VariableElement param = params.get(i);
boolean hasNonLeaked =
atypeFactory.getAnnotatedType(param).hasAnnotation(NonLeaked.class);
boolean hasLeakedToResult =
atypeFactory
.getAnnotatedType(param)
.hasAnnotation(LeakedToResult.class);
isUniqueCheck(node, parentIsStatement, hasNonLeaked, hasLeakedToResult);
} else {
// Not possible to leak reference here (case 1. from the javadoc).
}
}
// Now, doing the same as above for the receiver parameter
AnnotatedExecutableType annotatedType =
atypeFactory.getAnnotatedType(methodElement);
AnnotatedDeclaredType receiverType = annotatedType.getReceiverType();
if (receiverType != null) {
boolean hasNonLeaked = receiverType.hasAnnotation(NonLeaked.class);
boolean hasLeakedToResult = receiverType.hasAnnotation(LeakedToResult.class);
isUniqueCheck(node, parentIsStatement, hasNonLeaked, hasLeakedToResult);
}
}
}
return super.visitMethodInvocation(node, p);
}
private void isUniqueCheck(
MethodInvocationTree node,
boolean parentIsStatement,
boolean hasNonLeaked,
boolean hasLeakedToResult) {
if (hasNonLeaked || (hasLeakedToResult && parentIsStatement)) {
// Not leaked according to cases 2. and 3. from the javadoc of
// visitMethodInvocation.
} else {
// May be leaked, raise warning.
checker.reportError(node, "unique.leaked");
}
}
// TODO: Merge that code in
// commonAssignmentCheck(AnnotatedTypeMirror varType, ExpressionTree
// valueExp, String errorKey, boolean isLocalVariableAssignement), because
// the method below isn't called for pseudo-assignments, but the mentioned
// one is. The issue of copy-pasting the code from this method to the other
// one is that a declaration such as: List<@Unique Object> will raise a
// unique.leaked warning, as there is a pseudo-assignment from @Unique to a
// @MaybeAliased object, if the @Unique annotation is not in the stubfile.
// TODO: Change the documentation in BaseTypeVisitor to point out that
// this isn't called for pseudo-assignments.
@Override
protected void commonAssignmentCheck(
Tree varTree, ExpressionTree valueExp, @CompilerMessageKey String errorKey) {
super.commonAssignmentCheck(varTree, valueExp, errorKey);
if (isInUniqueConstructor() && TreeUtils.isExplicitThisDereference(valueExp)) {
// If an assignment occurs inside a constructor with
// result type @Unique, it will invalidate the @Unique property
// by using the "this" reference.
checker.reportError(valueExp, "unique.leaked");
} else if (canBeLeaked(valueExp)) {
checker.reportError(valueExp, "unique.leaked");
}
}
@Override
protected void commonAssignmentCheck(
AnnotatedTypeMirror varType,
AnnotatedTypeMirror valueType,
Tree valueTree,
@CompilerMessageKey String errorKey) {
super.commonAssignmentCheck(varType, valueType, valueTree, errorKey);
// If we are visiting a pseudo-assignment, visitorLeafKind is either
// Kind.NEW_CLASS or Kind.METHOD_INVOCATION.
TreePath path = visitorState.getPath();
if (path == null) {
return;
}
Kind visitorLeafKind = path.getLeaf().getKind();
if (visitorLeafKind == Kind.NEW_CLASS || visitorLeafKind == Kind.METHOD_INVOCATION) {
// Handling pseudo-assignments
if (canBeLeaked(valueTree)) {
Kind parentKind = visitorState.getPath().getParentPath().getLeaf().getKind();
if (!varType.hasAnnotation(NonLeaked.class)
&& !(varType.hasAnnotation(LeakedToResult.class)
&& parentKind == Kind.EXPRESSION_STATEMENT)) {
checker.reportError(valueTree, "unique.leaked");
}
}
}
}
@Override
public Void visitThrow(ThrowTree node, Void p) {
// throw is also an escape mechanism. If an expression of type
// @Unique is thrown, it is not @Unique anymore.
ExpressionTree exp = node.getExpression();
if (canBeLeaked(exp)) {
checker.reportError(exp, "unique.leaked");
}
return super.visitThrow(node, p);
}
@Override
public Void visitVariable(VariableTree node, Void p) {
// Component types are not allowed to have the @Unique annotation.
AnnotatedTypeMirror varType = atypeFactory.getAnnotatedType(node);
VariableElement elt = TreeUtils.elementFromDeclaration(node);
if (elt.getKind().isField() && varType.hasExplicitAnnotation(Unique.class)) {
checker.reportError(node, "unique.location.forbidden");
} else if (node.getType().getKind() == Kind.ARRAY_TYPE) {
AnnotatedArrayType arrayType = (AnnotatedArrayType) varType;
if (arrayType.getComponentType().hasAnnotation(Unique.class)) {
checker.reportError(node, "unique.location.forbidden");
}
} else if (node.getType().getKind() == Kind.PARAMETERIZED_TYPE) {
AnnotatedDeclaredType declaredType = (AnnotatedDeclaredType) varType;
for (AnnotatedTypeMirror atm : declaredType.getTypeArguments()) {
if (atm.hasAnnotation(Unique.class)) {
checker.reportError(node, "unique.location.forbidden");
}
}
}
return super.visitVariable(node, p);
}
@Override
public Void visitNewArray(NewArrayTree node, Void p) {
List<? extends ExpressionTree> initializers = node.getInitializers();
if (initializers != null && !initializers.isEmpty()) {
for (ExpressionTree exp : initializers) {
if (canBeLeaked(exp)) {
checker.reportError(exp, "unique.leaked");
}
}
}
return super.visitNewArray(node, p);
}
@Override
protected void checkConstructorResult(
AnnotatedExecutableType constructorType, ExecutableElement constructorElement) {
// @Unique is verified, so don't check this.
if (!constructorType.getReturnType().hasAnnotation(atypeFactory.UNIQUE)) {
super.checkConstructorResult(constructorType, constructorElement);
}
}
@Override
protected void checkThisOrSuperConstructorCall(
MethodInvocationTree superCall, @CompilerMessageKey String errorKey) {
if (isInUniqueConstructor()) {
// Check if a call to super() might create an alias: that
// happens when the parent's respective constructor is not @Unique.
AnnotatedTypeMirror superResult = atypeFactory.getAnnotatedType(superCall);
if (!superResult.hasAnnotation(Unique.class)) {
checker.reportError(superCall, "unique.leaked");
}
}
}
/**
* Returns true if {@code exp} has type {@code @Unique} and is not a method invocation nor a new
* class expression.
*
* @param exp the Tree to check
*/
private boolean canBeLeaked(Tree exp) {
AnnotatedTypeMirror type = atypeFactory.getAnnotatedType(exp);
boolean isMethodInvocation = exp.getKind() == Kind.METHOD_INVOCATION;
boolean isNewClass = exp.getKind() == Kind.NEW_CLASS;
return type.hasExplicitAnnotation(Unique.class) && !isMethodInvocation && !isNewClass;
}
private boolean isInUniqueConstructor() {
MethodTree enclosingMethod = TreeUtils.enclosingMethod(getCurrentPath());
if (enclosingMethod == null) {
return false; // No enclosing method.
}
return TreeUtils.isConstructor(enclosingMethod)
&& atypeFactory
.getAnnotatedType(enclosingMethod)
.getReturnType()
.hasAnnotation(Unique.class);
}
}