xref: /llvm-project/compiler-rt/test/asan/TestCases/Posix/concurrent_overflow.cpp (revision 673dc3d4a0b0fbb3b9b34ae2ecbfa522627fe582)
1*673dc3d4SNico Weber // RUN: %clangxx_asan -O0 -w %s -o %t && not %run %t 2>&1 | FileCheck %s
2*673dc3d4SNico Weber 
3*673dc3d4SNico Weber // Checks that concurrent reports will not trigger false "nested bug" reports.
4*673dc3d4SNico Weber // Regression test for https://github.com/google/sanitizers/issues/858
5*673dc3d4SNico Weber 
6*673dc3d4SNico Weber #include <pthread.h>
7*673dc3d4SNico Weber #include <stdlib.h>
8*673dc3d4SNico Weber #include <unistd.h>
9*673dc3d4SNico Weber 
start_routine(void * arg)10*673dc3d4SNico Weber static void *start_routine(void *arg) {
11*673dc3d4SNico Weber   volatile int *counter = (volatile int *)arg;
12*673dc3d4SNico Weber   char buf[8];
13*673dc3d4SNico Weber   __atomic_sub_fetch(counter, 1, __ATOMIC_SEQ_CST);
14*673dc3d4SNico Weber   while (*counter)
15*673dc3d4SNico Weber     ;
16*673dc3d4SNico Weber   buf[0] = buf[9];
17*673dc3d4SNico Weber   return 0;
18*673dc3d4SNico Weber }
19*673dc3d4SNico Weber 
main(void)20*673dc3d4SNico Weber int main(void) {
21*673dc3d4SNico Weber   const int n_threads = 8;
22*673dc3d4SNico Weber   int i, counter = n_threads;
23*673dc3d4SNico Weber   pthread_t thread[n_threads];
24*673dc3d4SNico Weber 
25*673dc3d4SNico Weber   for (i = 0; i < n_threads; ++i)
26*673dc3d4SNico Weber     pthread_create(&thread[i], NULL, &start_routine, (void *)&counter);
27*673dc3d4SNico Weber   for (i = 0; i < n_threads; ++i)
28*673dc3d4SNico Weber     pthread_join(thread[i], NULL);
29*673dc3d4SNico Weber   return 0;
30*673dc3d4SNico Weber }
31*673dc3d4SNico Weber 
32*673dc3d4SNico Weber // CHECK-NOT: nested bug
33*673dc3d4SNico Weber // CHECK: ERROR: AddressSanitizer: stack-buffer-overflow on address
34*673dc3d4SNico Weber // CHECK: SUMMARY: AddressSanitizer: stack-buffer-overflow
35