xref: /llvm-project/compiler-rt/test/tsan/getline_nohang.cpp (revision deebf6b312227e028dd3258b162306b9cdb21cf7)
1 // RUN: %clangxx_tsan -O1 %s -o %t && %run %t
2 
3 // Data race randomly triggered.
4 // UNSUPPORTED: target={{.*netbsd.*}}
5 
6 // Make sure TSan doesn't deadlock on a file stream lock at program shutdown.
7 // See https://github.com/google/sanitizers/issues/454
8 
9 // https://github.com/google/sanitizers/issues/1733
10 // UNSUPPORTED: glibc-2.38
11 
12 #ifdef __FreeBSD__
13 #define _WITH_GETLINE  // to declare getline()
14 #endif
15 
16 #include <pthread.h>
17 #include <stdio.h>
18 #include <unistd.h>
19 
thread(void * unused)20 void *thread(void *unused) {
21   char *line = NULL;
22   size_t size;
23   int fd[2];
24   pipe(fd);
25   // Forge a non-standard stream to make sure it's not closed.
26   FILE *stream = fdopen(fd[0], "r");
27   while (1) {
28     volatile int res = getline(&line, &size, stream);
29     (void)res;
30   }
31   return NULL;
32 }
33 
main()34 int main() {
35   pthread_t t;
36   pthread_attr_t a;
37   pthread_attr_init(&a);
38   pthread_attr_setdetachstate(&a, PTHREAD_CREATE_DETACHED);
39   pthread_create(&t, &a, thread, NULL);
40   pthread_attr_destroy(&a);
41   fprintf(stderr, "DONE\n");
42   return 0;
43   // ThreadSanitizer used to hang here because of a deadlock on a file stream.
44 }
45 
46 // CHECK: DONE
47