File: tls_get_addr.c

package info (click to toggle)
llvm-toolchain-20 1%3A20.1.6-1~exp1
  • links: PTS, VCS
  • area: main
  • in suites: experimental
  • size: 2,111,304 kB
  • sloc: cpp: 7,438,677; ansic: 1,393,822; asm: 1,012,926; python: 241,650; f90: 86,635; objc: 75,479; lisp: 42,144; pascal: 17,286; sh: 10,027; ml: 5,082; perl: 4,730; awk: 3,523; makefile: 3,349; javascript: 2,251; xml: 892; fortran: 672
file content (56 lines) | stat: -rw-r--r-- 1,378 bytes parent folder | download | duplicates (9)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
// RUN: %clang -g %s -o %t
// RUN: %clang -g %s -DBUILD_SO -fPIC -o %t-so.so -shared
// RUN: %run %t 2>&1 | FileCheck %s

// REQUIRES: glibc

// `__tls_get_addr` is somehow not invoked.
// XFAIL: i386-linux

// These don't intercept __tls_get_addr.
// XFAIL: lsan,hwasan,ubsan

// FIXME: Fails for unknown reasons.
// UNSUPPORTED: powerpc64le-target-arch

#ifndef BUILD_SO
#  include <assert.h>
#  include <dlfcn.h>
#  include <pthread.h>
#  include <stdio.h>
#  include <stdlib.h>

// CHECK-COUNT-2: __sanitizer_get_dtls_size:
size_t __sanitizer_get_dtls_size(const void *ptr)
    __attribute__((disable_sanitizer_instrumentation)) {
  fprintf(stderr, "__sanitizer_get_dtls_size: %p\n", ptr);
  return 0;
}

typedef long *(*get_t)();
get_t GetTls;
void *Thread(void *unused) { return GetTls(); }

int main(int argc, char *argv[]) {
  char path[4096];
  snprintf(path, sizeof(path), "%s-so.so", argv[0]);
  int i;

  void *handle = dlopen(path, RTLD_LAZY);
  if (!handle)
    fprintf(stderr, "%s\n", dlerror());
  assert(handle != 0);
  GetTls = (get_t)dlsym(handle, "GetTls");
  assert(dlerror() == 0);

  pthread_t t;
  pthread_create(&t, 0, Thread, 0);
  pthread_join(t, 0);
  pthread_create(&t, 0, Thread, 0);
  pthread_join(t, 0);
  return 0;
}
#else // BUILD_SO
__thread long huge_thread_local_array[1 << 17];
long *GetTls() { return &huge_thread_local_array[0]; }
#endif