1 // RUN: rm -rf %t-dir
2 // RUN: mkdir %t-dir
3 
4 // RUN: %clangxx_tsan -O1 -fno-builtin %s -DLIB -fPIC -fno-sanitize=thread -shared -o %t-dir/libignore_lib1.so
5 // RUN: %clangxx_tsan -O1 %s %link_libcxx_tsan -o %t-dir/executable
6 // RUN: echo running w/o suppressions:
7 // RUN: %deflake %run %t-dir/executable | FileCheck %s --check-prefix=CHECK-NOSUPP
8 // RUN: echo running with suppressions:
9 // RUN: %env_tsan_opts=suppressions='%s.supp' %run %t-dir/executable 2>&1 | FileCheck %s --check-prefix=CHECK-WITHSUPP
10 
11 // REQUIRES: stable-runtime
12 // UNSUPPORTED: powerpc64le
13 // FIXME: This test occasionally fails on powerpc64 LE possibly starting with
14 // r279664.  Re-enable the test once the problem(s) have been fixed.
15 
16 // Previously the test episodically failed with:
17 //   ThreadSanitizer: called_from_lib suppression '/libignore_lib1.so$' is
18 //   matched against 2 libraries: '/libignore_lib1.so' and '/libignore_lib1.so'
19 // This was caused by non-atomicity of reading of /proc/self/maps.
20 
21 // ReadProcMaps() on NetBSD does not handle >=1MB of memory layout information
22 // UNSUPPORTED: netbsd
23 
24 #ifndef LIB
25 
26 #include <dlfcn.h>
27 #include <sys/mman.h>
28 #include <stdlib.h>
29 #include <stdio.h>
30 #include <errno.h>
31 #include <libgen.h>
32 #include <string>
33 #include "test.h"
34 
35 #ifndef MAP_32BIT
36 # define MAP_32BIT 0
37 #endif
38 
39 #ifdef __APPLE__
40 # define TSAN_MAP_ANON MAP_ANON
41 #else
42 # define TSAN_MAP_ANON MAP_ANONYMOUS
43 #endif
44 
thr(void * arg)45 void *thr(void *arg) {
46   // This thread creates lots of separate mappings in /proc/self/maps before
47   // the ignored library.
48   for (int i = 0; i < 10000; i++) {
49     if (i == 5000)
50       barrier_wait(&barrier);
51     mmap(0, 4096, PROT_READ, TSAN_MAP_ANON | MAP_PRIVATE | MAP_32BIT, -1 , 0);
52     mmap(0, 4096, PROT_WRITE, TSAN_MAP_ANON | MAP_PRIVATE | MAP_32BIT, -1 , 0);
53   }
54   return 0;
55 }
56 
main(int argc,char ** argv)57 int main(int argc, char **argv) {
58   barrier_init(&barrier, 2);
59   pthread_t th;
60   pthread_create(&th, 0, thr, 0);
61   barrier_wait(&barrier);
62   std::string lib = std::string(dirname(argv[0])) + "/libignore_lib1.so";
63   void *h = dlopen(lib.c_str(), RTLD_GLOBAL | RTLD_NOW);
64   if (h == 0)
65     exit(printf("failed to load the library (%d)\n", errno));
66   void (*f)() = (void(*)())dlsym(h, "libfunc");
67   if (f == 0)
68     exit(printf("failed to find the func (%d)\n", errno));
69   pthread_join(th, 0);
70   f();
71 }
72 
73 #else  // #ifdef LIB
74 
75 #include "ignore_lib_lib.h"
76 
77 #endif  // #ifdef LIB
78 
79 // CHECK-NOSUPP: WARNING: ThreadSanitizer: data race
80 // CHECK-NOSUPP: OK
81 
82 // CHECK-WITHSUPP-NOT: WARNING: ThreadSanitizer: data race
83 // CHECK-WITHSUPP: OK
84