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#ifdef __FreeBSD__
9#define _WITH_GETLINE // to declare getline()
10#endif
11
12#include <pthread.h>
13#include <stdio.h>
14#include <unistd.h>
15
16void *thread(void *unused) {
17 char *line = NULL;
18 size_t size;
19 int fd[2];
20 pipe(pipedes: fd);
21 // Forge a non-standard stream to make sure it's not closed.
22 FILE *stream = fdopen(fd: fd[0], modes: "r");
23 while (1) {
24 volatile int res = getline(lineptr: &line, n: &size, stream: stream);
25 (void)res;
26 }
27 return NULL;
28}
29
30int main() {
31 pthread_t t;
32 pthread_attr_t a;
33 pthread_attr_init(attr: &a);
34 pthread_attr_setdetachstate(attr: &a, PTHREAD_CREATE_DETACHED);
35 pthread_create(newthread: &t, attr: &a, start_routine: thread, NULL);
36 pthread_attr_destroy(attr: &a);
37 fprintf(stderr, format: "DONE\n");
38 return 0;
39 // ThreadSanitizer used to hang here because of a deadlock on a file stream.
40}
41
42// CHECK: DONE
43

source code of compiler-rt/test/tsan/getline_nohang.cpp