sanitizer_posix_libcdep.cc 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. //===-- sanitizer_posix_libcdep.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 shared between AddressSanitizer and ThreadSanitizer
  9. // run-time libraries and implements libc-dependent POSIX-specific functions
  10. // from sanitizer_libc.h.
  11. //===----------------------------------------------------------------------===//
  12. #include "sanitizer_platform.h"
  13. #if SANITIZER_POSIX
  14. #include "sanitizer_common.h"
  15. #include "sanitizer_flags.h"
  16. #include "sanitizer_platform_limits_posix.h"
  17. #include "sanitizer_stacktrace.h"
  18. #include <errno.h>
  19. #include <pthread.h>
  20. #include <signal.h>
  21. #include <stdlib.h>
  22. #include <sys/mman.h>
  23. #include <sys/resource.h>
  24. #include <sys/time.h>
  25. #include <sys/types.h>
  26. #include <unistd.h>
  27. namespace __sanitizer {
  28. u32 GetUid() {
  29. return getuid();
  30. }
  31. uptr GetThreadSelf() {
  32. return (uptr)pthread_self();
  33. }
  34. void FlushUnneededShadowMemory(uptr addr, uptr size) {
  35. madvise((void*)addr, size, MADV_DONTNEED);
  36. }
  37. static rlim_t getlim(int res) {
  38. rlimit rlim;
  39. CHECK_EQ(0, getrlimit(res, &rlim));
  40. return rlim.rlim_cur;
  41. }
  42. static void setlim(int res, rlim_t lim) {
  43. // The following magic is to prevent clang from replacing it with memset.
  44. volatile struct rlimit rlim;
  45. rlim.rlim_cur = lim;
  46. rlim.rlim_max = lim;
  47. if (setrlimit(res, (struct rlimit*)&rlim)) {
  48. Report("ERROR: %s setrlimit() failed %d\n", SanitizerToolName, errno);
  49. Die();
  50. }
  51. }
  52. void DisableCoreDumperIfNecessary() {
  53. if (common_flags()->disable_coredump) {
  54. setlim(RLIMIT_CORE, 0);
  55. }
  56. }
  57. bool StackSizeIsUnlimited() {
  58. rlim_t stack_size = getlim(RLIMIT_STACK);
  59. return (stack_size == RLIM_INFINITY);
  60. }
  61. void SetStackSizeLimitInBytes(uptr limit) {
  62. setlim(RLIMIT_STACK, (rlim_t)limit);
  63. CHECK(!StackSizeIsUnlimited());
  64. }
  65. bool AddressSpaceIsUnlimited() {
  66. rlim_t as_size = getlim(RLIMIT_AS);
  67. return (as_size == RLIM_INFINITY);
  68. }
  69. void SetAddressSpaceUnlimited() {
  70. setlim(RLIMIT_AS, RLIM_INFINITY);
  71. CHECK(AddressSpaceIsUnlimited());
  72. }
  73. void SleepForSeconds(int seconds) {
  74. sleep(seconds);
  75. }
  76. void SleepForMillis(int millis) {
  77. usleep(millis * 1000);
  78. }
  79. void Abort() {
  80. abort();
  81. }
  82. int Atexit(void (*function)(void)) {
  83. #ifndef SANITIZER_GO
  84. return atexit(function);
  85. #else
  86. return 0;
  87. #endif
  88. }
  89. int internal_isatty(fd_t fd) {
  90. return isatty(fd);
  91. }
  92. #ifndef SANITIZER_GO
  93. // TODO(glider): different tools may require different altstack size.
  94. static const uptr kAltStackSize = SIGSTKSZ * 4; // SIGSTKSZ is not enough.
  95. void SetAlternateSignalStack() {
  96. stack_t altstack, oldstack;
  97. CHECK_EQ(0, sigaltstack(0, &oldstack));
  98. // If the alternate stack is already in place, do nothing.
  99. // Android always sets an alternate stack, but it's too small for us.
  100. if (!SANITIZER_ANDROID && !(oldstack.ss_flags & SS_DISABLE)) return;
  101. // TODO(glider): the mapped stack should have the MAP_STACK flag in the
  102. // future. It is not required by man 2 sigaltstack now (they're using
  103. // malloc()).
  104. void* base = MmapOrDie(kAltStackSize, __func__);
  105. altstack.ss_sp = (char*) base;
  106. altstack.ss_flags = 0;
  107. altstack.ss_size = kAltStackSize;
  108. CHECK_EQ(0, sigaltstack(&altstack, 0));
  109. }
  110. void UnsetAlternateSignalStack() {
  111. stack_t altstack, oldstack;
  112. altstack.ss_sp = 0;
  113. altstack.ss_flags = SS_DISABLE;
  114. altstack.ss_size = kAltStackSize; // Some sane value required on Darwin.
  115. CHECK_EQ(0, sigaltstack(&altstack, &oldstack));
  116. UnmapOrDie(oldstack.ss_sp, oldstack.ss_size);
  117. }
  118. typedef void (*sa_sigaction_t)(int, siginfo_t *, void *);
  119. static void MaybeInstallSigaction(int signum,
  120. SignalHandlerType handler) {
  121. if (!IsDeadlySignal(signum))
  122. return;
  123. struct sigaction sigact;
  124. internal_memset(&sigact, 0, sizeof(sigact));
  125. sigact.sa_sigaction = (sa_sigaction_t)handler;
  126. // Do not block the signal from being received in that signal's handler.
  127. // Clients are responsible for handling this correctly.
  128. sigact.sa_flags = SA_SIGINFO | SA_NODEFER;
  129. if (common_flags()->use_sigaltstack) sigact.sa_flags |= SA_ONSTACK;
  130. CHECK_EQ(0, internal_sigaction(signum, &sigact, 0));
  131. VReport(1, "Installed the sigaction for signal %d\n", signum);
  132. }
  133. void InstallDeadlySignalHandlers(SignalHandlerType handler) {
  134. // Set the alternate signal stack for the main thread.
  135. // This will cause SetAlternateSignalStack to be called twice, but the stack
  136. // will be actually set only once.
  137. if (common_flags()->use_sigaltstack) SetAlternateSignalStack();
  138. MaybeInstallSigaction(SIGSEGV, handler);
  139. MaybeInstallSigaction(SIGBUS, handler);
  140. }
  141. #endif // SANITIZER_GO
  142. bool IsAccessibleMemoryRange(uptr beg, uptr size) {
  143. uptr page_size = GetPageSizeCached();
  144. // Checking too large memory ranges is slow.
  145. CHECK_LT(size, page_size * 10);
  146. int sock_pair[2];
  147. if (pipe(sock_pair))
  148. return false;
  149. uptr bytes_written =
  150. internal_write(sock_pair[1], reinterpret_cast<void *>(beg), size);
  151. int write_errno;
  152. bool result;
  153. if (internal_iserror(bytes_written, &write_errno)) {
  154. CHECK_EQ(EFAULT, write_errno);
  155. result = false;
  156. } else {
  157. result = (bytes_written == size);
  158. }
  159. internal_close(sock_pair[0]);
  160. internal_close(sock_pair[1]);
  161. return result;
  162. }
  163. } // namespace __sanitizer
  164. #endif // SANITIZER_POSIX