asan_posix.cc 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. //===-- asan_posix.cc -----------------------------------------------------===//
  2. //
  3. // This file is distributed under the University of Illinois Open Source
  4. // License. See LICENSE.TXT for details.
  5. //
  6. //===----------------------------------------------------------------------===//
  7. //
  8. // This file is a part of AddressSanitizer, an address sanity checker.
  9. //
  10. // Posix-specific details.
  11. //===----------------------------------------------------------------------===//
  12. #include "sanitizer_common/sanitizer_platform.h"
  13. #if SANITIZER_POSIX
  14. #include "asan_internal.h"
  15. #include "asan_interceptors.h"
  16. #include "asan_mapping.h"
  17. #include "asan_report.h"
  18. #include "asan_stack.h"
  19. #include "sanitizer_common/sanitizer_libc.h"
  20. #include "sanitizer_common/sanitizer_procmaps.h"
  21. #include <pthread.h>
  22. #include <signal.h>
  23. #include <stdlib.h>
  24. #include <sys/time.h>
  25. #include <sys/resource.h>
  26. #include <unistd.h>
  27. namespace __asan {
  28. void AsanOnSIGSEGV(int, void *siginfo, void *context) {
  29. ScopedDeadlySignal signal_scope(GetCurrentThread());
  30. uptr addr = (uptr)((siginfo_t*)siginfo)->si_addr;
  31. int code = (int)((siginfo_t*)siginfo)->si_code;
  32. // Write the first message using the bullet-proof write.
  33. if (13 != internal_write(2, "ASAN:SIGSEGV\n", 13)) Die();
  34. uptr pc, sp, bp;
  35. GetPcSpBp(context, &pc, &sp, &bp);
  36. // Access at a reasonable offset above SP, or slightly below it (to account
  37. // for x86_64 or PowerPC redzone, ARM push of multiple registers, etc) is
  38. // probably a stack overflow.
  39. // We also check si_code to filter out SEGV caused by something else other
  40. // then hitting the guard page or unmapped memory, like, for example,
  41. // unaligned memory access.
  42. if (addr + 512 > sp && addr < sp + 0xFFFF &&
  43. (code == si_SEGV_MAPERR || code == si_SEGV_ACCERR))
  44. ReportStackOverflow(pc, sp, bp, context, addr);
  45. else
  46. ReportSIGSEGV("SEGV", pc, sp, bp, context, addr);
  47. }
  48. // ---------------------- TSD ---------------- {{{1
  49. static pthread_key_t tsd_key;
  50. static bool tsd_key_inited = false;
  51. void AsanTSDInit(void (*destructor)(void *tsd)) {
  52. CHECK(!tsd_key_inited);
  53. tsd_key_inited = true;
  54. CHECK_EQ(0, pthread_key_create(&tsd_key, destructor));
  55. }
  56. void *AsanTSDGet() {
  57. CHECK(tsd_key_inited);
  58. return pthread_getspecific(tsd_key);
  59. }
  60. void AsanTSDSet(void *tsd) {
  61. CHECK(tsd_key_inited);
  62. pthread_setspecific(tsd_key, tsd);
  63. }
  64. void PlatformTSDDtor(void *tsd) {
  65. AsanThreadContext *context = (AsanThreadContext*)tsd;
  66. if (context->destructor_iterations > 1) {
  67. context->destructor_iterations--;
  68. CHECK_EQ(0, pthread_setspecific(tsd_key, tsd));
  69. return;
  70. }
  71. AsanThread::TSDDtor(tsd);
  72. }
  73. } // namespace __asan
  74. #endif // SANITIZER_POSIX