asan_posix.cpp 4.5 KB

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