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 | // FIXME: Hangs for unknown reasons on all platforms. We can re-enable it when |
8 | // its either deterministic, or we solve the deadlock between asan and lsan. |
9 | // UNSUPPORTED: true |
10 | |
11 | /* |
12 | * Purpose: Verify deadlock prevention between ASan error reporting and LSan leak checking. |
13 | * |
14 | * Test Design: |
15 | * 1. Creates contention scenario between: |
16 | * - ASan's error reporting (requires lock B -> lock A ordering) |
17 | * - LSan's leak check (requires lock A -> lock B ordering) |
18 | * 2. Thread timing: |
19 | * - Main thread: Holds 'in' mutex -> Triggers LSan check (lock A then B) |
20 | * - Worker thread: Triggers ASan OOB error (lock B then A via symbolization) |
21 | * |
22 | * Deadlock Condition (if unfixed): |
23 | * Circular lock dependency forms when: |
24 | * [Main Thread] LSan: lock A -> requests lock B |
25 | * [Worker Thread] ASan: lock B -> requests lock A |
26 | * |
27 | * Success Criteria: |
28 | * With proper lock ordering enforcement, watchdog should NOT trigger - test exits with Asan report. |
29 | */ |
30 | |
31 | #include <mutex> |
32 | #include <sanitizer/lsan_interface.h> |
33 | #include <stdio.h> |
34 | #include <thread> |
35 | #include <unistd.h> |
36 | |
37 | void Watchdog() { |
38 | // Safety mechanism: Turn infinite deadlock into finite test failure |
39 | sleep(seconds: 60); |
40 | // Unexpected. "not" in RUN will fail if we reached here. |
41 | _exit(status: 0); |
42 | } |
43 | |
44 | int main(int argc, char **argv) { |
45 | int arr[1] = {0}; |
46 | std::mutex in; |
47 | in.lock(); |
48 | |
49 | std::thread w(Watchdog); |
50 | w.detach(); |
51 | |
52 | std::thread t([&]() { |
53 | in.unlock(); |
54 | /* |
55 | * Provoke ASan error: ASan's error reporting acquires: |
56 | * 1. ASan's thread registry lock (B) during the reporting |
57 | * 2. dl_iterate_phdr lock (A) during symbolization |
58 | */ |
59 | // CHECK: SUMMARY: AddressSanitizer: stack-buffer-overflow |
60 | arr[argc] = 1; // Deliberate OOB access |
61 | }); |
62 | |
63 | in.lock(); |
64 | /* |
65 | * Critical section: LSan's check acquires: |
66 | * 1. dl_iterate_phdr lock (A) |
67 | * 2. ASan's thread registry lock (B) |
68 | * before Stop The World. |
69 | */ |
70 | __lsan_do_leak_check(); |
71 | t.join(); |
72 | return 0; |
73 | } |
74 | |