asan_posix.cpp 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. //===-- asan_posix.cpp ----------------------------------------------------===//
  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. // This file is a part of AddressSanitizer, an address sanity checker.
  10. //
  11. // Posix-specific details.
  12. //===----------------------------------------------------------------------===//
  13. #include "sanitizer_common/sanitizer_platform.h"
  14. #if SANITIZER_POSIX
  15. #include "asan_internal.h"
  16. #include "asan_interceptors.h"
  17. #include "asan_mapping.h"
  18. #include "asan_poisoning.h"
  19. #include "asan_report.h"
  20. #include "asan_stack.h"
  21. #include "sanitizer_common/sanitizer_libc.h"
  22. #include "sanitizer_common/sanitizer_posix.h"
  23. #include "sanitizer_common/sanitizer_procmaps.h"
  24. #include <pthread.h>
  25. #include <signal.h>
  26. #include <stdlib.h>
  27. #include <sys/time.h>
  28. #include <sys/resource.h>
  29. #include <unistd.h>
  30. namespace __asan {
  31. void AsanOnDeadlySignal(int signo, void *siginfo, void *context) {
  32. StartReportDeadlySignal();
  33. SignalContext sig(siginfo, context);
  34. ReportDeadlySignal(sig);
  35. }
  36. bool PlatformUnpoisonStacks() {
  37. stack_t signal_stack;
  38. CHECK_EQ(0, sigaltstack(nullptr, &signal_stack));
  39. uptr sigalt_bottom = (uptr)signal_stack.ss_sp;
  40. uptr sigalt_top = (uptr)((char *)signal_stack.ss_sp + signal_stack.ss_size);
  41. // If we're executing on the signal alternate stack AND the Linux flag
  42. // SS_AUTODISARM was used, then we cannot get the signal alternate stack
  43. // bounds from sigaltstack -- sigaltstack's output looks just as if no
  44. // alternate stack has ever been set up.
  45. // We're always unpoisoning the signal alternate stack to support jumping
  46. // between the default stack and signal alternate stack.
  47. if (signal_stack.ss_flags != SS_DISABLE)
  48. UnpoisonStack(sigalt_bottom, sigalt_top, "sigalt");
  49. if (signal_stack.ss_flags != SS_ONSTACK)
  50. return false;
  51. // Since we're on the signal alternate stack, we cannot find the DEFAULT
  52. // stack bottom using a local variable.
  53. uptr default_bottom, tls_addr, tls_size, stack_size;
  54. GetThreadStackAndTls(/*main=*/false, &default_bottom, &stack_size, &tls_addr,
  55. &tls_size);
  56. UnpoisonStack(default_bottom, default_bottom + stack_size, "default");
  57. return true;
  58. }
  59. // ---------------------- TSD ---------------- {{{1
  60. #if SANITIZER_NETBSD && !ASAN_DYNAMIC
  61. // Thread Static Data cannot be used in early static ASan init on NetBSD.
  62. // Reuse the Asan TSD API for compatibility with existing code
  63. // with an alternative implementation.
  64. static void (*tsd_destructor)(void *tsd) = nullptr;
  65. struct tsd_key {
  66. tsd_key() : key(nullptr) {}
  67. ~tsd_key() {
  68. CHECK(tsd_destructor);
  69. if (key)
  70. (*tsd_destructor)(key);
  71. }
  72. void *key;
  73. };
  74. static thread_local struct tsd_key key;
  75. void AsanTSDInit(void (*destructor)(void *tsd)) {
  76. CHECK(!tsd_destructor);
  77. tsd_destructor = destructor;
  78. }
  79. void *AsanTSDGet() {
  80. CHECK(tsd_destructor);
  81. return key.key;
  82. }
  83. void AsanTSDSet(void *tsd) {
  84. CHECK(tsd_destructor);
  85. CHECK(tsd);
  86. CHECK(!key.key);
  87. key.key = tsd;
  88. }
  89. void PlatformTSDDtor(void *tsd) {
  90. CHECK(tsd_destructor);
  91. CHECK_EQ(key.key, tsd);
  92. key.key = nullptr;
  93. // Make sure that signal handler can not see a stale current thread pointer.
  94. atomic_signal_fence(memory_order_seq_cst);
  95. AsanThread::TSDDtor(tsd);
  96. }
  97. #else
  98. static pthread_key_t tsd_key;
  99. static bool tsd_key_inited = false;
  100. void AsanTSDInit(void (*destructor)(void *tsd)) {
  101. CHECK(!tsd_key_inited);
  102. tsd_key_inited = true;
  103. CHECK_EQ(0, pthread_key_create(&tsd_key, destructor));
  104. }
  105. void *AsanTSDGet() {
  106. CHECK(tsd_key_inited);
  107. return pthread_getspecific(tsd_key);
  108. }
  109. void AsanTSDSet(void *tsd) {
  110. CHECK(tsd_key_inited);
  111. pthread_setspecific(tsd_key, tsd);
  112. }
  113. void PlatformTSDDtor(void *tsd) {
  114. AsanThreadContext *context = (AsanThreadContext*)tsd;
  115. if (context->destructor_iterations > 1) {
  116. context->destructor_iterations--;
  117. CHECK_EQ(0, pthread_setspecific(tsd_key, tsd));
  118. return;
  119. }
  120. AsanThread::TSDDtor(tsd);
  121. }
  122. #endif
  123. } // namespace __asan
  124. #endif // SANITIZER_POSIX