1 | // Test interaction of Asan recovery mode with asynch signals. |
2 | // |
3 | // RUN: %clang_asan -fsanitize-recover=address -pthread %s -o %t |
4 | // |
5 | // RUN: %env_asan_opts=halt_on_error=false:suppress_equal_pcs=false %run %t 100 >%t.log 2>&1 || true |
6 | // Collision will almost always get triggered but we still need to check the unlikely case: |
7 | // RUN: FileCheck --check-prefix=CHECK-COLLISION %s <%t.log || FileCheck --check-prefix=CHECK-NO-COLLISION %s <%t.log |
8 | |
9 | #define _SVID_SOURCE 1 // SA_NODEFER |
10 | |
11 | #include <stdio.h> |
12 | #include <stdlib.h> |
13 | #include <string.h> |
14 | #include <pthread.h> |
15 | #include <time.h> |
16 | #include <signal.h> |
17 | |
18 | #include <sanitizer/asan_interface.h> |
19 | |
20 | void random_delay(unsigned *seed) { |
21 | *seed = 1664525 * *seed + 1013904223; |
22 | struct timespec delay = { .tv_sec: 0, .tv_nsec: (*seed % 1000) * 1000 }; |
23 | nanosleep(requested_time: &delay, remaining: 0); |
24 | } |
25 | |
26 | volatile char bad[2] = {1, }; |
27 | |
28 | void error() { |
29 | // CHECK-COLLISION: AddressSanitizer: nested bug in the same thread, aborting |
30 | // CHECK-NO-COLLISION: AddressSanitizer: use-after-poison |
31 | volatile int idx = 0; |
32 | bad[idx] = 0; |
33 | } |
34 | |
35 | #define CHECK_CALL(e, msg) do { \ |
36 | if (0 != (e)) { \ |
37 | fprintf(stderr, "Failed to " msg "\n"); \ |
38 | exit(1); \ |
39 | } \ |
40 | } while (0) |
41 | |
42 | size_t niter = 10; |
43 | pthread_t sender_tid, receiver_tid; |
44 | |
45 | pthread_mutex_t keep_alive_mu = PTHREAD_MUTEX_INITIALIZER; |
46 | |
47 | void *sender(void *arg) { |
48 | unsigned seed = 0; |
49 | for (size_t i = 0; i < niter; ++i) { |
50 | random_delay(seed: &seed); |
51 | CHECK_CALL(pthread_kill(receiver_tid, SIGUSR1), "send signal" ); |
52 | } |
53 | return 0; |
54 | } |
55 | |
56 | void handler(int sig) { |
57 | // Expect error collisions here |
58 | error(); |
59 | } |
60 | |
61 | void *receiver(void *arg) { |
62 | unsigned seed = 1; |
63 | for (size_t i = 0; i < niter; ++i) { |
64 | random_delay(seed: &seed); |
65 | // And here |
66 | error(); |
67 | } |
68 | // Parent will release this when it's ok to terminate |
69 | CHECK_CALL(pthread_mutex_lock(&keep_alive_mu), "unlock mutex" ); |
70 | return 0; |
71 | } |
72 | |
73 | int main(int argc, char **argv) { |
74 | if (argc != 2) { |
75 | fprintf(stderr, format: "Syntax: %s niter\n" , argv[0]); |
76 | exit(status: 1); |
77 | } |
78 | |
79 | niter = (size_t)strtoul(nptr: argv[1], endptr: 0, base: 0); |
80 | |
81 | struct sigaction sa; |
82 | memset(s: &sa, c: 0, n: sizeof(sa)); |
83 | sa.sa_handler = handler; |
84 | sa.sa_flags = SA_NODEFER; // Enable nested handlers to add more stress |
85 | CHECK_CALL(sigaction(SIGUSR1, &sa, 0), "set sighandler" ); |
86 | |
87 | __asan_poison_memory_region(addr: &bad, size: sizeof(bad)); |
88 | |
89 | CHECK_CALL(pthread_mutex_lock(&keep_alive_mu), "lock mutex" ); |
90 | CHECK_CALL(pthread_create(&receiver_tid, 0, receiver, 0), "start thread" ); |
91 | CHECK_CALL(pthread_create(&sender_tid, 0, sender, 0), "start thread" ); |
92 | CHECK_CALL(pthread_join(sender_tid, 0), "join thread" ); |
93 | // Now allow receiver to die |
94 | CHECK_CALL(pthread_mutex_unlock(&keep_alive_mu), "unlock mutex" ); |
95 | CHECK_CALL(pthread_join(receiver_tid, 0), "join thread" ); |
96 | |
97 | // CHECK-NO-COLLISION: All threads terminated |
98 | printf(format: "All threads terminated\n" ); |
99 | |
100 | return 0; |
101 | } |
102 | |