84 lines · cpp
1// RUN: rm -rf %t-dir2// RUN: mkdir %t-dir3 4// RUN: %clangxx_tsan -O1 -fno-builtin %s -DLIB -fPIC -fno-sanitize=thread -shared -o %t-dir/libignore_lib1.so5// RUN: %clangxx_tsan -O1 %s %link_libcxx_tsan -o %t-dir/executable6// RUN: echo running w/o suppressions:7// RUN: %deflake %run %t-dir/executable | FileCheck %s --check-prefix=CHECK-NOSUPP8// RUN: echo running with suppressions:9// RUN: %env_tsan_opts=suppressions='%s.supp' %run %t-dir/executable 2>&1 | FileCheck %s --check-prefix=CHECK-WITHSUPP10 11// REQUIRES: stable-runtime12// UNSUPPORTED: target=powerpc64le{{.*}}13// FIXME: This test occasionally fails on powerpc64 LE possibly starting with14// 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$' is18// 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 information22// UNSUPPORTED: target={{.*netbsd.*}}23 24#ifndef LIB25 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_32BIT36# define MAP_32BIT 037#endif38 39#ifdef __APPLE__40# define TSAN_MAP_ANON MAP_ANON41#else42# define TSAN_MAP_ANON MAP_ANONYMOUS43#endif44 45void *thr(void *arg) {46 // This thread creates lots of separate mappings in /proc/self/maps before47 // 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 57int 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 LIB74 75#include "ignore_lib_lib.h"76 77#endif // #ifdef LIB78 79// CHECK-NOSUPP: WARNING: ThreadSanitizer: data race80// CHECK-NOSUPP: OK81 82// CHECK-WITHSUPP-NOT: WARNING: ThreadSanitizer: data race83// CHECK-WITHSUPP: OK84