llvm-project
46 строк · 1.1 Кб
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) {
21char *line = NULL;
22size_t size;
23int fd[2];
24pipe(fd);
25// Forge a non-standard stream to make sure it's not closed.
26FILE *stream = fdopen(fd[0], "r");
27while (1) {
28volatile int res = getline(&line, &size, stream);
29(void)res;
30}
31return NULL;
32}
33
34int main() {
35pthread_t t;
36pthread_attr_t a;
37pthread_attr_init(&a);
38pthread_attr_setdetachstate(&a, PTHREAD_CREATE_DETACHED);
39pthread_create(&t, &a, thread, NULL);
40pthread_attr_destroy(&a);
41fprintf(stderr, "DONE\n");
42return 0;
43// ThreadSanitizer used to hang here because of a deadlock on a file stream.
44}
45
46// CHECK: DONE
47