Skip to content

Commit 9e77e5b

Browse files
committed
java: add test with deeper paths
also format test files
1 parent f183a72 commit 9e77e5b

File tree

3 files changed

+88
-26
lines changed

3 files changed

+88
-26
lines changed

java/ql/test/query-tests/ThreadSafe/ThreadSafe.expected

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
| examples/C.java:26:9:26:14 | this.y | This field access (publicly accessible via $@) is not protected by any monitor, but the class is annotated as @ThreadSafe. | examples/C.java:26:9:26:14 | this.y | this expression |
1010
| examples/C.java:30:13:30:13 | y | This field access (publicly accessible via $@) is not protected by any monitor, but the class is annotated as @ThreadSafe. | examples/C.java:30:13:30:13 | y | this expression |
1111
| examples/C.java:33:9:33:9 | y | This field access (publicly accessible via $@) is not protected by any monitor, but the class is annotated as @ThreadSafe. | examples/C.java:33:9:33:9 | y | this expression |
12+
| examples/DeepPaths.java:8:17:8:17 | y | This field is not properly synchronized in that no single monitor covers all accesses, but the class $@ is annotated as @ThreadSafe. | examples/DeepPaths.java:7:14:7:22 | DeepPaths | DeepPaths |
1213
| examples/FaultyTurnstileExample.java:18:5:18:9 | count | This field access (publicly accessible via $@) is not protected by any monitor, but the class is annotated as @ThreadSafe. | examples/FaultyTurnstileExample.java:18:5:18:9 | count | this expression |
1314
| examples/FaultyTurnstileExample.java:26:15:26:19 | count | This field is not properly synchronized in that no single monitor covers all accesses, but the class $@ is annotated as @ThreadSafe. | examples/FaultyTurnstileExample.java:23:7:23:29 | FaultyTurnstileExample2 | FaultyTurnstileExample2 |
1415
| examples/FlawedSemaphore.java:15:14:15:18 | state | This field access (publicly accessible via $@) is not protected by any monitor, but the class is annotated as @ThreadSafe. | examples/FlawedSemaphore.java:15:14:15:18 | state | this expression |
@@ -34,7 +35,7 @@
3435
| examples/LockExample.java:124:5:124:21 | notRelatedToOther | This field access (publicly accessible via $@) is not protected by any monitor, but the class is annotated as @ThreadSafe. | examples/LockExample.java:124:5:124:21 | notRelatedToOther | this expression |
3536
| examples/LockExample.java:145:5:145:21 | notRelatedToOther | This field access (publicly accessible via $@) is not protected by any monitor, but the class is annotated as @ThreadSafe. | examples/LockExample.java:145:5:145:21 | notRelatedToOther | this expression |
3637
| examples/LockExample.java:153:5:153:21 | notRelatedToOther | This field access (publicly accessible via $@) is not protected by any monitor, but the class is annotated as @ThreadSafe. | examples/LockExample.java:153:5:153:21 | notRelatedToOther | this expression |
37-
| examples/ManyLocks.java:8:15:8:15 | y | This field is not properly synchronized in that no single monitor covers all accesses, but the class $@ is annotated as @ThreadSafe. | examples/ManyLocks.java:7:14:7:22 | ManyLocks | ManyLocks |
38+
| examples/ManyLocks.java:8:17:8:17 | y | This field is not properly synchronized in that no single monitor covers all accesses, but the class $@ is annotated as @ThreadSafe. | examples/ManyLocks.java:7:14:7:22 | ManyLocks | ManyLocks |
3839
| examples/SyncLstExample.java:45:5:45:7 | lst | This field access (publicly accessible via $@) is not protected by any monitor, but the class is annotated as @ThreadSafe. | examples/SyncLstExample.java:45:5:45:7 | lst | this expression |
3940
| examples/SyncStackExample.java:37:5:37:7 | stc | This field access (publicly accessible via $@) is not protected by any monitor, but the class is annotated as @ThreadSafe. | examples/SyncStackExample.java:37:5:37:7 | stc | this expression |
4041
| examples/SynchronizedAndLock.java:10:17:10:22 | length | This field is not properly synchronized in that no single monitor covers all accesses, but the class $@ is annotated as @ThreadSafe. | examples/SynchronizedAndLock.java:7:14:7:32 | SynchronizedAndLock | SynchronizedAndLock |
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
package examples;
2+
3+
import java.util.concurrent.locks.Lock;
4+
import java.util.concurrent.locks.ReentrantLock;
5+
6+
@ThreadSafe
7+
public class DeepPaths {
8+
private int y; // $ Alert
9+
10+
private final Lock lock1 = new ReentrantLock();
11+
private final Lock lock2 = new ReentrantLock();
12+
private final Lock lock3 = new ReentrantLock();
13+
14+
public void layer1Locked() {
15+
lock1.lock();
16+
this.layer2Locked();
17+
lock1.unlock();
18+
}
19+
20+
private void layer2Locked() {
21+
lock2.lock();
22+
this.layer3Unlocked();
23+
lock2.unlock();
24+
}
25+
26+
private void layer3Locked() {
27+
lock3.lock();
28+
y++;
29+
lock3.unlock();
30+
}
31+
32+
public void layer1Skip() {
33+
lock2.lock();
34+
this.layer3Locked();
35+
lock2.unlock();
36+
}
37+
38+
public void layer1Indirect() {
39+
this.layer2();
40+
}
41+
42+
private void layer2() {
43+
this.layer2Locked();
44+
}
45+
46+
public void layer1Unlocked() {
47+
this.layer2Unlocked();
48+
}
49+
50+
private void layer2Unlocked() {
51+
this.layer3();
52+
}
53+
54+
private void layer3() {
55+
this.layer3Locked();
56+
}
57+
58+
private void layer3Unlocked() {
59+
y++;
60+
}
61+
}

java/ql/test/query-tests/ThreadSafe/examples/ManyLocks.java

Lines changed: 25 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -5,33 +5,33 @@
55

66
@ThreadSafe
77
public class ManyLocks {
8-
private int y; // $ Alert
8+
private int y; // $ Alert
99

10-
private final Lock lock1 = new ReentrantLock();
11-
private final Lock lock2 = new ReentrantLock();
12-
private final Lock lock3 = new ReentrantLock();
10+
private final Lock lock1 = new ReentrantLock();
11+
private final Lock lock2 = new ReentrantLock();
12+
private final Lock lock3 = new ReentrantLock();
1313

14-
public void inc() {
15-
lock1.lock();
16-
lock2.lock();
17-
y++;
18-
lock2.unlock();
19-
lock1.unlock();
20-
}
14+
public void inc() {
15+
lock1.lock();
16+
lock2.lock();
17+
y++;
18+
lock2.unlock();
19+
lock1.unlock();
20+
}
2121

22-
public void dec() {
23-
lock2.lock();
24-
lock3.lock();
25-
y--;
26-
lock3.unlock();
27-
lock2.unlock();
28-
}
22+
public void dec() {
23+
lock2.lock();
24+
lock3.lock();
25+
y--;
26+
lock3.unlock();
27+
lock2.unlock();
28+
}
2929

30-
public void reset() {
31-
lock1.lock();
32-
lock3.lock();
33-
y = 0;
34-
lock3.unlock();
35-
lock1.unlock();
36-
}
30+
public void reset() {
31+
lock1.lock();
32+
lock3.lock();
33+
y = 0;
34+
lock3.unlock();
35+
lock1.unlock();
36+
}
3737
}

0 commit comments

Comments
 (0)