Skip to content

Commit 05436a3

Browse files
committed
java: add Escaping query (P1)
1 parent ac83c4d commit 05436a3

File tree

6 files changed

+79
-0
lines changed

6 files changed

+79
-0
lines changed
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
<!DOCTYPE qhelp PUBLIC
2+
"-//Semmle//qhelp//EN"
3+
"qhelp.dtd">
4+
<qhelp>
5+
6+
7+
<overview>
8+
<p>
9+
In a thread-safe class, non-final fields should generally be private (or possibly volatile) to ensure that they cannot be accessed by other threads in an unsafe manner.
10+
</p>
11+
12+
</overview>
13+
<recommendation>
14+
15+
<p>
16+
If the field does not change, mark it as <code>final</code>. If the field is mutable, mark it as <code>private</code> and provide properly synchronized accessors.</p>
17+
18+
</recommendation>
19+
20+
<references>
21+
22+
23+
<li>
24+
Java Language Specification, chapter 17:
25+
<a href="https://docs.oracle.com/javase/specs/jls/se8/html/jls-17.html#jls-17.4">Threads and Locks</a>.
26+
</li>
27+
<li>
28+
Java concurrency package:
29+
<a href="https://docs.oracle.com/javase/8/docs/api/java/util/concurrent/package-summary.html">java.util.concurrent</a>.
30+
</li>
31+
32+
33+
</references>
34+
</qhelp>
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
/**
2+
* @name Escaping
3+
* @kind problem
4+
* @problem.severity warning
5+
* @id java/escaping
6+
*/
7+
8+
import java
9+
import semmle.code.java.ConflictingAccess
10+
11+
from Field f, ClassAnnotatedAsThreadSafe c
12+
where
13+
f = c.getAField() and
14+
not f.isFinal() and // final fields do not change
15+
not f.isPrivate() and
16+
// We believe that protected fields are also dangerous
17+
// Volatile fields cannot cause data races, but it is dubious to allow changes.
18+
// For now, we ignore volatile fields, but there are likely bugs to be caught here.
19+
not f.isVolatile()
20+
select f, "The class $@ is marked as thread-safe, but this field is potentially escaping.", c,
21+
c.getName()
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
| Escaping.java:3:7:3:7 | x | The class $@ is marked as thread-safe, but this field is potentially escaping. | Escaping.java:2:14:2:21 | Escaping | Escaping |
2+
| Escaping.java:4:14:4:14 | y | The class $@ is marked as thread-safe, but this field is potentially escaping. | Escaping.java:2:14:2:21 | Escaping | Escaping |
3+
| Escaping.java:9:18:9:18 | b | The class $@ is marked as thread-safe, but this field is potentially escaping. | Escaping.java:2:14:2:21 | Escaping | Escaping |
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
@ThreadSafe
2+
public class Escaping {
3+
int x; //$ Alert
4+
public int y = 0; //$ Alert
5+
private int z = 3;
6+
final int w = 0;
7+
public final int u = 4;
8+
private final long a = 5;
9+
protected long b = 0; //$ Alert
10+
protected final long c = 0L;
11+
volatile long d = 3;
12+
protected volatile long e = 3L;
13+
14+
public void methodLocal() {
15+
int i;
16+
}
17+
}
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
query: Likely Bugs/Concurrency/Escaping.ql
2+
postprocess: utils/test/InlineExpectationsTestQuery.ql
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
public @interface ThreadSafe {
2+
}

0 commit comments

Comments
 (0)