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
20void *thread(void *unused) {
21 char *line = NULL;
22 size_t size;
23 int fd[2];
24 pipe(pipedes: fd);
25 // Forge a non-standard stream to make sure it's not closed.
26 FILE *stream = fdopen(fd: fd[0], modes: "r");
27 while (1) {
28 volatile int res = getline(lineptr: &line, n: &size, stream: stream);
29 (void)res;
30 }
31 return NULL;
32}
33
34int main() {
35 pthread_t t;
36 pthread_attr_t a;
37 pthread_attr_init(attr: &a);
38 pthread_attr_setdetachstate(attr: &a, PTHREAD_CREATE_DETACHED);
39 pthread_create(newthread: &t, attr: &a, start_routine: thread, NULL);
40 pthread_attr_destroy(attr: &a);
41 fprintf(stderr, format: "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

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