1/* RUN: %clang_msan -g %s -o %t
2 RUN: %clang_msan -g %s -DBUILD_SO -fPIC -o %t-so.so -shared
3 RUN: %run %t 2>&1
4
5 Regression test for a bug in msan/glibc integration,
6 see https://sourceware.org/bugzilla/show_bug.cgi?id=16291
7 and https://github.com/google/sanitizers/issues/547
8
9 XFAIL: target={{.*freebsd.*}}
10 UNSUPPORTED: target=powerpc{{.*}}
11
12 // Reports use-of-uninitialized-value, not analyzed
13 XFAIL: target={{.*netbsd.*}}
14 UNSUPPORTED: aarch64-target-arch
15
16*/
17
18#ifndef BUILD_SO
19#include <assert.h>
20#include <dlfcn.h>
21#include <stdio.h>
22#include <stdlib.h>
23#include <pthread.h>
24
25typedef long *(* get_t)();
26get_t GetTls;
27void *Thread1(void *unused) {
28 long uninitialized;
29 long *x = GetTls();
30 if (*x)
31 fprintf(stderr, format: "bar\n");
32 *x = uninitialized;
33 fprintf(stderr, format: "stack: %p dtls: %p\n", &x, x);
34 return 0;
35}
36
37void *Thread2(void *unused) {
38 long *x = GetTls();
39 fprintf(stderr, format: "stack: %p dtls: %p\n", &x, x);
40 if (*x)
41 fprintf(stderr, format: "foo\n"); // False negative here.
42 return 0;
43}
44
45int main(int argc, char *argv[]) {
46 char path[4096];
47 snprintf(s: path, maxlen: sizeof(path), format: "%s-so.so", argv[0]);
48 int i;
49
50 void *handle = dlopen(file: path, RTLD_LAZY);
51 if (!handle) fprintf(stderr, format: "%s\n", dlerror());
52 assert(handle != 0);
53 GetTls = (get_t)dlsym(handle: handle, name: "GetTls");
54 assert(dlerror() == 0);
55
56 pthread_t t;
57 pthread_create(newthread: &t, attr: 0, start_routine: Thread1, arg: 0);
58 pthread_join(th: t, thread_return: 0);
59 pthread_create(newthread: &t, attr: 0, start_routine: Thread2, arg: 0);
60 pthread_join(th: t, thread_return: 0);
61 return 0;
62}
63#else // BUILD_SO
64__thread long huge_thread_local_array[1 << 17];
65long *GetTls() {
66 return &huge_thread_local_array[0];
67}
68#endif
69

source code of compiler-rt/test/msan/dtls_test.c