Skip to content

Commit 95eab0d

Browse files
authored
[sanitizer] Test for __sanitizer_get_dtls_size (llvm#108349)
Test for llvm#108348 which is almost NFC. However, the test may fail on some platforms, and a few iterations could be needed to filter out unsupported platforms.
1 parent de0fdcb commit 95eab0d

File tree

1 file changed

+52
-0
lines changed

1 file changed

+52
-0
lines changed
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
// RUN: %clang -g %s -o %t
2+
// RUN: %clang -g %s -DBUILD_SO -fPIC -o %t-so.so -shared
3+
// RUN: %run %t 2>&1 | FileCheck %s
4+
5+
// REQUIRES: glibc
6+
7+
// `__tls_get_addr` is somehow not invoked.
8+
// XFAIL: i386-linux
9+
10+
// These don't intercept __tls_get_addr.
11+
// XFAIL: lsan,hwasan,ubsan
12+
13+
#ifndef BUILD_SO
14+
# include <assert.h>
15+
# include <dlfcn.h>
16+
# include <pthread.h>
17+
# include <stdio.h>
18+
# include <stdlib.h>
19+
20+
// CHECK-COUNT-2: __sanitizer_get_dtls_size:
21+
size_t __sanitizer_get_dtls_size(const void *ptr) {
22+
fprintf(stderr, "__sanitizer_get_dtls_size: %p\n", ptr);
23+
return 0;
24+
}
25+
26+
typedef long *(*get_t)();
27+
get_t GetTls;
28+
void *Thread(void *unused) { return GetTls(); }
29+
30+
int main(int argc, char *argv[]) {
31+
char path[4096];
32+
snprintf(path, sizeof(path), "%s-so.so", argv[0]);
33+
int i;
34+
35+
void *handle = dlopen(path, RTLD_LAZY);
36+
if (!handle)
37+
fprintf(stderr, "%s\n", dlerror());
38+
assert(handle != 0);
39+
GetTls = (get_t)dlsym(handle, "GetTls");
40+
assert(dlerror() == 0);
41+
42+
pthread_t t;
43+
pthread_create(&t, 0, Thread, 0);
44+
pthread_join(t, 0);
45+
pthread_create(&t, 0, Thread, 0);
46+
pthread_join(t, 0);
47+
return 0;
48+
}
49+
#else // BUILD_SO
50+
__thread long huge_thread_local_array[1 << 17];
51+
long *GetTls() { return &huge_thread_local_array[0]; }
52+
#endif

0 commit comments

Comments
 (0)