sanitizer_tls_get_addr.h 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  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. #ifndef SANITIZER_TLS_GET_ADDR_H
  28. #define SANITIZER_TLS_GET_ADDR_H
  29. #include "sanitizer_atomic.h"
  30. #include "sanitizer_common.h"
  31. namespace __sanitizer {
  32. struct DTLS {
  33. // Array of DTLS chunks for the current Thread.
  34. // If beg == 0, the chunk is unused.
  35. struct DTV {
  36. uptr beg, size;
  37. };
  38. struct DTVBlock {
  39. atomic_uintptr_t next;
  40. DTV dtvs[(4096UL - sizeof(next)) / sizeof(DTLS::DTV)];
  41. };
  42. static_assert(sizeof(DTVBlock) <= 4096UL, "Unexpected block size");
  43. atomic_uintptr_t dtv_block;
  44. // Auxiliary fields, don't access them outside sanitizer_tls_get_addr.cpp
  45. uptr last_memalign_size;
  46. uptr last_memalign_ptr;
  47. };
  48. template <typename Fn>
  49. void ForEachDVT(DTLS *dtls, const Fn &fn) {
  50. DTLS::DTVBlock *block =
  51. (DTLS::DTVBlock *)atomic_load(&dtls->dtv_block, memory_order_acquire);
  52. while (block) {
  53. int id = 0;
  54. for (auto &d : block->dtvs) fn(d, id++);
  55. block = (DTLS::DTVBlock *)atomic_load(&block->next, memory_order_acquire);
  56. }
  57. }
  58. // Returns pointer and size of a linker-allocated TLS block.
  59. // Each block is returned exactly once.
  60. DTLS::DTV *DTLS_on_tls_get_addr(void *arg, void *res, uptr static_tls_begin,
  61. uptr static_tls_end);
  62. void DTLS_on_libc_memalign(void *ptr, uptr size);
  63. DTLS *DTLS_Get();
  64. void DTLS_Destroy(); // Make sure to call this before the thread is destroyed.
  65. // Returns true if DTLS of suspended thread is in destruction process.
  66. bool DTLSInDestruction(DTLS *dtls);
  67. } // namespace __sanitizer
  68. #endif // SANITIZER_TLS_GET_ADDR_H