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: FreeBSD
10    UNSUPPORTED: powerpc
11 
12    // Reports use-of-uninitialized-value, not analyzed
13    XFAIL: netbsd
14 
15    // This is known to be broken with glibc-2.27+
16    // https://bugs.llvm.org/show_bug.cgi?id=37804
17    XFAIL: glibc-2.27
18 
19 */
20 
21 #ifndef BUILD_SO
22 #include <assert.h>
23 #include <dlfcn.h>
24 #include <stdio.h>
25 #include <stdlib.h>
26 #include <pthread.h>
27 
28 typedef long *(* get_t)();
29 get_t GetTls;
Thread1(void * unused)30 void *Thread1(void *unused) {
31   long uninitialized;
32   long *x = GetTls();
33   if (*x)
34     fprintf(stderr, "bar\n");
35   *x = uninitialized;
36   fprintf(stderr, "stack: %p dtls: %p\n", &x, x);
37   return 0;
38 }
39 
Thread2(void * unused)40 void *Thread2(void *unused) {
41   long *x = GetTls();
42   fprintf(stderr, "stack: %p dtls: %p\n", &x, x);
43   if (*x)
44     fprintf(stderr, "foo\n");   // False negative here.
45   return 0;
46 }
47 
main(int argc,char * argv[])48 int main(int argc, char *argv[]) {
49   char path[4096];
50   snprintf(path, sizeof(path), "%s-so.so", argv[0]);
51   int i;
52 
53   void *handle = dlopen(path, RTLD_LAZY);
54   if (!handle) fprintf(stderr, "%s\n", dlerror());
55   assert(handle != 0);
56   GetTls = (get_t)dlsym(handle, "GetTls");
57   assert(dlerror() == 0);
58 
59   pthread_t t;
60   pthread_create(&t, 0, Thread1, 0);
61   pthread_join(t, 0);
62   pthread_create(&t, 0, Thread2, 0);
63   pthread_join(t, 0);
64   return 0;
65 }
66 #else  // BUILD_SO
67 __thread long huge_thread_local_array[1 << 17];
GetTls()68 long *GetTls() {
69   return &huge_thread_local_array[0];
70 }
71 #endif
72