1 //===-- sanitizer_tls_get_addr.h --------------------------------*- C++ -*-===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // Handle the __tls_get_addr call.
11 //
12 // All this magic is specific to glibc and is required to workaround
13 // the lack of interface that would tell us about the Dynamic TLS (DTLS).
14 // https://sourceware.org/bugzilla/show_bug.cgi?id=16291
15 //
16 // The matters get worse because the glibc implementation changed between
17 // 2.18 and 2.19:
18 // https://groups.google.com/forum/#!topic/address-sanitizer/BfwYD8HMxTM
19 //
20 // Before 2.19, every DTLS chunk is allocated with __libc_memalign,
21 // which we intercept and thus know where is the DTLS.
22 // Since 2.19, DTLS chunks are allocated with __signal_safe_memalign,
23 // which is an internal function that wraps a mmap call, neither of which
24 // we can intercept. Luckily, __signal_safe_memalign has a simple parseable
25 // header which we can use.
26 //
27 //===----------------------------------------------------------------------===//
28 
29 #ifndef SANITIZER_TLS_GET_ADDR_H
30 #define SANITIZER_TLS_GET_ADDR_H
31 
32 #include "sanitizer_common.h"
33 
34 namespace __sanitizer {
35 
36 struct DTLS {
37   // Array of DTLS chunks for the current Thread.
38   // If beg == 0, the chunk is unused.
39   struct DTV {
40     uptr beg, size;
41   };
42 
43   uptr dtv_size;
44   DTV *dtv;  // dtv_size elements, allocated by MmapOrDie.
45 
46   // Auxiliary fields, don't access them outside sanitizer_tls_get_addr.cc
47   uptr last_memalign_size;
48   uptr last_memalign_ptr;
49 };
50 
51 // Returns pointer and size of a linker-allocated TLS block.
52 // Each block is returned exactly once.
53 DTLS::DTV *DTLS_on_tls_get_addr(void *arg, void *res, uptr static_tls_begin,
54                                 uptr static_tls_end);
55 void DTLS_on_libc_memalign(void *ptr, uptr size);
56 DTLS *DTLS_Get();
57 void DTLS_Destroy();  // Make sure to call this before the thread is destroyed.
58 
59 }  // namespace __sanitizer
60 
61 #endif  // SANITIZER_TLS_GET_ADDR_H
62