|
1 |
| -// Test for potential deadlock in LeakSanitizer+AddressSanitizer. |
2 |
| -// REQUIRES: leak-detection |
3 |
| -// |
4 |
| -// RUN: %clangxx_asan -O0 %s -o %t |
5 |
| -// RUN: %env_asan_opts=detect_leaks=1 not %run %t 2>&1 | FileCheck %s |
6 |
| - |
7 |
| -/* |
8 |
| - * Purpose: Verify deadlock prevention between ASan error reporting and LSan leak checking. |
9 |
| - * |
10 |
| - * Test Design: |
11 |
| - * 1. Creates contention scenario between: |
12 |
| - * - ASan's error reporting (requires lock B -> lock A ordering) |
13 |
| - * - LSan's leak check (requires lock A -> lock B ordering) |
14 |
| - * 2. Thread timing: |
15 |
| - * - Main thread: Holds 'in' mutex -> Triggers LSan check (lock A then B) |
16 |
| - * - Worker thread: Triggers ASan OOB error (lock B then A via symbolization) |
17 |
| - * |
18 |
| - * Deadlock Condition (if unfixed): |
19 |
| - * Circular lock dependency forms when: |
20 |
| - * [Main Thread] LSan: lock A -> requests lock B |
21 |
| - * [Worker Thread] ASan: lock B -> requests lock A |
22 |
| - * |
23 |
| - * Success Criteria: |
24 |
| - * With proper lock ordering enforcement, watchdog should NOT trigger - test exits normally. |
25 |
| - * If deadlock occurs, watchdog terminates via _exit(1) after 10s timeout. |
26 |
| - */ |
27 |
| - |
28 |
| -#include <mutex> |
29 |
| -#include <sanitizer/lsan_interface.h> |
30 |
| -#include <stdio.h> |
31 |
| -#include <thread> |
32 |
| -#include <unistd.h> |
33 |
| - |
34 |
| -void Watchdog() { |
35 |
| - // Safety mechanism: Turn infinite deadlock into finite test failure |
36 |
| - usleep(10000000); |
37 |
| - // CHECK-NOT: Timeout! Deadlock detected. |
38 |
| - puts("Timeout! Deadlock detected."); |
39 |
| - fflush(stdout); |
40 |
| - _exit(1); |
41 |
| -} |
42 |
| - |
43 |
| -int main(int argc, char **argv) { |
44 |
| - int arr[1] = {0}; |
45 |
| - std::mutex in; |
46 |
| - in.lock(); |
47 |
| - |
48 |
| - std::thread w(Watchdog); |
49 |
| - w.detach(); |
50 |
| - |
51 |
| - std::thread t([&]() { |
52 |
| - in.unlock(); |
53 |
| - /* |
54 |
| - * Provoke ASan error: ASan's error reporting acquires: |
55 |
| - * 1. ASan's thread registry lock (B) during the reporting |
56 |
| - * 2. dl_iterate_phdr lock (A) during symbolization |
57 |
| - */ |
58 |
| - // CHECK: SUMMARY: AddressSanitizer: stack-buffer-overflow |
59 |
| - arr[argc] = 1; // Deliberate OOB access |
60 |
| - }); |
61 |
| - |
62 |
| - in.lock(); |
63 |
| - /* |
64 |
| - * Critical section: LSan's check acquires: |
65 |
| - * 1. dl_iterate_phdr lock (A) |
66 |
| - * 2. ASan's thread registry lock (B) |
67 |
| - * before Stop The World. |
68 |
| - */ |
69 |
| - __lsan_do_leak_check(); |
70 |
| - t.join(); |
71 |
| - return 0; |
72 |
| -} |
| 1 | +// Test for potential deadlock in LeakSanitizer+AddressSanitizer. |
| 2 | +// REQUIRES: leak-detection |
| 3 | +// |
| 4 | +// RUN: %clangxx_asan -O0 %s -o %t |
| 5 | +// RUN: %env_asan_opts=detect_leaks=1 not %run %t 2>&1 | FileCheck %s |
| 6 | + |
| 7 | +/* |
| 8 | + * Purpose: Verify deadlock prevention between ASan error reporting and LSan leak checking. |
| 9 | + * |
| 10 | + * Test Design: |
| 11 | + * 1. Creates contention scenario between: |
| 12 | + * - ASan's error reporting (requires lock B -> lock A ordering) |
| 13 | + * - LSan's leak check (requires lock A -> lock B ordering) |
| 14 | + * 2. Thread timing: |
| 15 | + * - Main thread: Holds 'in' mutex -> Triggers LSan check (lock A then B) |
| 16 | + * - Worker thread: Triggers ASan OOB error (lock B then A via symbolization) |
| 17 | + * |
| 18 | + * Deadlock Condition (if unfixed): |
| 19 | + * Circular lock dependency forms when: |
| 20 | + * [Main Thread] LSan: lock A -> requests lock B |
| 21 | + * [Worker Thread] ASan: lock B -> requests lock A |
| 22 | + * |
| 23 | + * Success Criteria: |
| 24 | + * With proper lock ordering enforcement, watchdog should NOT trigger - test exits normally. |
| 25 | + * If deadlock occurs, watchdog terminates via _exit(1) after 10s timeout. |
| 26 | + */ |
| 27 | + |
| 28 | +#include <mutex> |
| 29 | +#include <sanitizer/lsan_interface.h> |
| 30 | +#include <stdio.h> |
| 31 | +#include <thread> |
| 32 | +#include <unistd.h> |
| 33 | + |
| 34 | +void Watchdog() { |
| 35 | + // Safety mechanism: Turn infinite deadlock into finite test failure |
| 36 | + usleep(10000000); |
| 37 | + // CHECK-NOT: Timeout! Deadlock detected. |
| 38 | + puts("Timeout! Deadlock detected."); |
| 39 | + fflush(stdout); |
| 40 | + _exit(1); |
| 41 | +} |
| 42 | + |
| 43 | +int main(int argc, char **argv) { |
| 44 | + int arr[1] = {0}; |
| 45 | + std::mutex in; |
| 46 | + in.lock(); |
| 47 | + |
| 48 | + std::thread w(Watchdog); |
| 49 | + w.detach(); |
| 50 | + |
| 51 | + std::thread t([&]() { |
| 52 | + in.unlock(); |
| 53 | + /* |
| 54 | + * Provoke ASan error: ASan's error reporting acquires: |
| 55 | + * 1. ASan's thread registry lock (B) during the reporting |
| 56 | + * 2. dl_iterate_phdr lock (A) during symbolization |
| 57 | + */ |
| 58 | + // CHECK: SUMMARY: AddressSanitizer: stack-buffer-overflow |
| 59 | + arr[argc] = 1; // Deliberate OOB access |
| 60 | + }); |
| 61 | + |
| 62 | + in.lock(); |
| 63 | + /* |
| 64 | + * Critical section: LSan's check acquires: |
| 65 | + * 1. dl_iterate_phdr lock (A) |
| 66 | + * 2. ASan's thread registry lock (B) |
| 67 | + * before Stop The World. |
| 68 | + */ |
| 69 | + __lsan_do_leak_check(); |
| 70 | + t.join(); |
| 71 | + return 0; |
| 72 | +} |
0 commit comments