sanitizer_common_libcdep.cpp 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. //===-- sanitizer_common_libcdep.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 shared between AddressSanitizer and ThreadSanitizer
  10. // run-time libraries.
  11. //===----------------------------------------------------------------------===//
  12. #include "sanitizer_allocator_interface.h"
  13. #include "sanitizer_common.h"
  14. #include "sanitizer_flags.h"
  15. #include "sanitizer_procmaps.h"
  16. namespace __sanitizer {
  17. static void (*SoftRssLimitExceededCallback)(bool exceeded);
  18. void SetSoftRssLimitExceededCallback(void (*Callback)(bool exceeded)) {
  19. CHECK_EQ(SoftRssLimitExceededCallback, nullptr);
  20. SoftRssLimitExceededCallback = Callback;
  21. }
  22. #if (SANITIZER_LINUX || SANITIZER_NETBSD) && !SANITIZER_GO
  23. // Weak default implementation for when sanitizer_stackdepot is not linked in.
  24. SANITIZER_WEAK_ATTRIBUTE StackDepotStats StackDepotGetStats() { return {}; }
  25. void *BackgroundThread(void *arg) {
  26. const uptr hard_rss_limit_mb = common_flags()->hard_rss_limit_mb;
  27. const uptr soft_rss_limit_mb = common_flags()->soft_rss_limit_mb;
  28. const bool heap_profile = common_flags()->heap_profile;
  29. uptr prev_reported_rss = 0;
  30. uptr prev_reported_stack_depot_size = 0;
  31. bool reached_soft_rss_limit = false;
  32. uptr rss_during_last_reported_profile = 0;
  33. while (true) {
  34. SleepForMillis(100);
  35. const uptr current_rss_mb = GetRSS() >> 20;
  36. if (Verbosity()) {
  37. // If RSS has grown 10% since last time, print some information.
  38. if (prev_reported_rss * 11 / 10 < current_rss_mb) {
  39. Printf("%s: RSS: %zdMb\n", SanitizerToolName, current_rss_mb);
  40. prev_reported_rss = current_rss_mb;
  41. }
  42. // If stack depot has grown 10% since last time, print it too.
  43. StackDepotStats stack_depot_stats = StackDepotGetStats();
  44. if (prev_reported_stack_depot_size * 11 / 10 <
  45. stack_depot_stats.allocated) {
  46. Printf("%s: StackDepot: %zd ids; %zdM allocated\n", SanitizerToolName,
  47. stack_depot_stats.n_uniq_ids, stack_depot_stats.allocated >> 20);
  48. prev_reported_stack_depot_size = stack_depot_stats.allocated;
  49. }
  50. }
  51. // Check RSS against the limit.
  52. if (hard_rss_limit_mb && hard_rss_limit_mb < current_rss_mb) {
  53. Report("%s: hard rss limit exhausted (%zdMb vs %zdMb)\n",
  54. SanitizerToolName, hard_rss_limit_mb, current_rss_mb);
  55. DumpProcessMap();
  56. Die();
  57. }
  58. if (soft_rss_limit_mb) {
  59. if (soft_rss_limit_mb < current_rss_mb && !reached_soft_rss_limit) {
  60. reached_soft_rss_limit = true;
  61. Report("%s: soft rss limit exhausted (%zdMb vs %zdMb)\n",
  62. SanitizerToolName, soft_rss_limit_mb, current_rss_mb);
  63. if (SoftRssLimitExceededCallback)
  64. SoftRssLimitExceededCallback(true);
  65. } else if (soft_rss_limit_mb >= current_rss_mb &&
  66. reached_soft_rss_limit) {
  67. reached_soft_rss_limit = false;
  68. if (SoftRssLimitExceededCallback)
  69. SoftRssLimitExceededCallback(false);
  70. }
  71. }
  72. if (heap_profile &&
  73. current_rss_mb > rss_during_last_reported_profile * 1.1) {
  74. Printf("\n\nHEAP PROFILE at RSS %zdMb\n", current_rss_mb);
  75. __sanitizer_print_memory_profile(90, 20);
  76. rss_during_last_reported_profile = current_rss_mb;
  77. }
  78. }
  79. }
  80. #endif
  81. void WriteToSyslog(const char *msg) {
  82. InternalScopedString msg_copy;
  83. msg_copy.append("%s", msg);
  84. const char *p = msg_copy.data();
  85. // Print one line at a time.
  86. // syslog, at least on Android, has an implicit message length limit.
  87. while (char* q = internal_strchr(p, '\n')) {
  88. *q = '\0';
  89. WriteOneLineToSyslog(p);
  90. p = q + 1;
  91. }
  92. // Print remaining characters, if there are any.
  93. // Note that this will add an extra newline at the end.
  94. // FIXME: buffer extra output. This would need a thread-local buffer, which
  95. // on Android requires plugging into the tools (ex. ASan's) Thread class.
  96. if (*p)
  97. WriteOneLineToSyslog(p);
  98. }
  99. void MaybeStartBackgroudThread() {
  100. #if (SANITIZER_LINUX || SANITIZER_NETBSD) && \
  101. !SANITIZER_GO // Need to implement/test on other platforms.
  102. // Start the background thread if one of the rss limits is given.
  103. if (!common_flags()->hard_rss_limit_mb &&
  104. !common_flags()->soft_rss_limit_mb &&
  105. !common_flags()->heap_profile) return;
  106. if (!&real_pthread_create) return; // Can't spawn the thread anyway.
  107. internal_start_thread(BackgroundThread, nullptr);
  108. #endif
  109. }
  110. static void (*sandboxing_callback)();
  111. void SetSandboxingCallback(void (*f)()) {
  112. sandboxing_callback = f;
  113. }
  114. uptr ReservedAddressRange::InitAligned(uptr size, uptr align,
  115. const char *name) {
  116. CHECK(IsPowerOfTwo(align));
  117. if (align <= GetPageSizeCached())
  118. return Init(size, name);
  119. uptr start = Init(size + align, name);
  120. start += align - (start & (align - 1));
  121. return start;
  122. }
  123. #if !SANITIZER_FUCHSIA
  124. // Reserve memory range [beg, end].
  125. // We need to use inclusive range because end+1 may not be representable.
  126. void ReserveShadowMemoryRange(uptr beg, uptr end, const char *name,
  127. bool madvise_shadow) {
  128. CHECK_EQ((beg % GetMmapGranularity()), 0);
  129. CHECK_EQ(((end + 1) % GetMmapGranularity()), 0);
  130. uptr size = end - beg + 1;
  131. DecreaseTotalMmap(size); // Don't count the shadow against mmap_limit_mb.
  132. if (madvise_shadow ? !MmapFixedSuperNoReserve(beg, size, name)
  133. : !MmapFixedNoReserve(beg, size, name)) {
  134. Report(
  135. "ReserveShadowMemoryRange failed while trying to map 0x%zx bytes. "
  136. "Perhaps you're using ulimit -v\n",
  137. size);
  138. Abort();
  139. }
  140. if (madvise_shadow && common_flags()->use_madv_dontdump)
  141. DontDumpShadowMemory(beg, size);
  142. }
  143. void ProtectGap(uptr addr, uptr size, uptr zero_base_shadow_start,
  144. uptr zero_base_max_shadow_start) {
  145. if (!size)
  146. return;
  147. void *res = MmapFixedNoAccess(addr, size, "shadow gap");
  148. if (addr == (uptr)res)
  149. return;
  150. // A few pages at the start of the address space can not be protected.
  151. // But we really want to protect as much as possible, to prevent this memory
  152. // being returned as a result of a non-FIXED mmap().
  153. if (addr == zero_base_shadow_start) {
  154. uptr step = GetMmapGranularity();
  155. while (size > step && addr < zero_base_max_shadow_start) {
  156. addr += step;
  157. size -= step;
  158. void *res = MmapFixedNoAccess(addr, size, "shadow gap");
  159. if (addr == (uptr)res)
  160. return;
  161. }
  162. }
  163. Report(
  164. "ERROR: Failed to protect the shadow gap. "
  165. "%s cannot proceed correctly. ABORTING.\n",
  166. SanitizerToolName);
  167. DumpProcessMap();
  168. Die();
  169. }
  170. #endif // !SANITIZER_FUCHSIA
  171. } // namespace __sanitizer
  172. SANITIZER_INTERFACE_WEAK_DEF(void, __sanitizer_sandbox_on_notify,
  173. __sanitizer_sandbox_arguments *args) {
  174. __sanitizer::PlatformPrepareForSandboxing(args);
  175. if (__sanitizer::sandboxing_callback)
  176. __sanitizer::sandboxing_callback();
  177. }