From 9e77e5b0468055aca55417569d3ab8831a1c103d Mon Sep 17 00:00:00 2001 From: yoff Date: Tue, 21 Oct 2025 14:02:36 +0200 Subject: [PATCH] java: add test with deeper paths also format test files --- .../ThreadSafe/ThreadSafe.expected | 3 +- .../ThreadSafe/examples/DeepPaths.java | 61 +++++++++++++++++++ .../ThreadSafe/examples/ManyLocks.java | 50 +++++++-------- 3 files changed, 88 insertions(+), 26 deletions(-) create mode 100644 java/ql/test/query-tests/ThreadSafe/examples/DeepPaths.java diff --git a/java/ql/test/query-tests/ThreadSafe/ThreadSafe.expected b/java/ql/test/query-tests/ThreadSafe/ThreadSafe.expected index a4da07d7ac6..3d73caaffe5 100644 --- a/java/ql/test/query-tests/ThreadSafe/ThreadSafe.expected +++ b/java/ql/test/query-tests/ThreadSafe/ThreadSafe.expected @@ -9,6 +9,7 @@ | 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 | | 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 | | 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 | +| 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 | | 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 | | 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 | | 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 @@ | 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 | | 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 | | 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 | -| 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 | +| 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 | | 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 | | 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 | | 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 | diff --git a/java/ql/test/query-tests/ThreadSafe/examples/DeepPaths.java b/java/ql/test/query-tests/ThreadSafe/examples/DeepPaths.java new file mode 100644 index 00000000000..095087a5018 --- /dev/null +++ b/java/ql/test/query-tests/ThreadSafe/examples/DeepPaths.java @@ -0,0 +1,61 @@ +package examples; + +import java.util.concurrent.locks.Lock; +import java.util.concurrent.locks.ReentrantLock; + +@ThreadSafe +public class DeepPaths { + private int y; // $ Alert + + private final Lock lock1 = new ReentrantLock(); + private final Lock lock2 = new ReentrantLock(); + private final Lock lock3 = new ReentrantLock(); + + public void layer1Locked() { + lock1.lock(); + this.layer2Locked(); + lock1.unlock(); + } + + private void layer2Locked() { + lock2.lock(); + this.layer3Unlocked(); + lock2.unlock(); + } + + private void layer3Locked() { + lock3.lock(); + y++; + lock3.unlock(); + } + + public void layer1Skip() { + lock2.lock(); + this.layer3Locked(); + lock2.unlock(); + } + + public void layer1Indirect() { + this.layer2(); + } + + private void layer2() { + this.layer2Locked(); + } + + public void layer1Unlocked() { + this.layer2Unlocked(); + } + + private void layer2Unlocked() { + this.layer3(); + } + + private void layer3() { + this.layer3Locked(); + } + + private void layer3Unlocked() { + y++; + } +} diff --git a/java/ql/test/query-tests/ThreadSafe/examples/ManyLocks.java b/java/ql/test/query-tests/ThreadSafe/examples/ManyLocks.java index 93557ecdafe..a7e19b3424b 100644 --- a/java/ql/test/query-tests/ThreadSafe/examples/ManyLocks.java +++ b/java/ql/test/query-tests/ThreadSafe/examples/ManyLocks.java @@ -5,33 +5,33 @@ import java.util.concurrent.locks.ReentrantLock; @ThreadSafe public class ManyLocks { - private int y; // $ Alert + private int y; // $ Alert - private final Lock lock1 = new ReentrantLock(); - private final Lock lock2 = new ReentrantLock(); - private final Lock lock3 = new ReentrantLock(); + private final Lock lock1 = new ReentrantLock(); + private final Lock lock2 = new ReentrantLock(); + private final Lock lock3 = new ReentrantLock(); - public void inc() { - lock1.lock(); - lock2.lock(); - y++; - lock2.unlock(); - lock1.unlock(); - } + public void inc() { + lock1.lock(); + lock2.lock(); + y++; + lock2.unlock(); + lock1.unlock(); + } - public void dec() { - lock2.lock(); - lock3.lock(); - y--; - lock3.unlock(); - lock2.unlock(); - } + public void dec() { + lock2.lock(); + lock3.lock(); + y--; + lock3.unlock(); + lock2.unlock(); + } - public void reset() { - lock1.lock(); - lock3.lock(); - y = 0; - lock3.unlock(); - lock1.unlock(); - } + public void reset() { + lock1.lock(); + lock3.lock(); + y = 0; + lock3.unlock(); + lock1.unlock(); + } } \ No newline at end of file