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