1 //===-- sanitizer_common_libcdep.cc ---------------------------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file is shared between AddressSanitizer and ThreadSanitizer
11 // run-time libraries.
12 //===----------------------------------------------------------------------===//
14 #include "sanitizer_common.h"
16 #include "sanitizer_allocator_interface.h"
17 #include "sanitizer_flags.h"
18 #include "sanitizer_stackdepot.h"
19 #include "sanitizer_stacktrace.h"
20 #include "sanitizer_symbolizer.h"
23 #include "sanitizer_posix.h"
26 namespace __sanitizer {
28 bool ReportFile::SupportsColors() {
31 return SupportsColoredOutput(fd);
34 bool ColorizeReports() {
35 // FIXME: Add proper Windows support to AnsiColorDecorator and re-enable color
36 // printing on Windows.
37 if (SANITIZER_WINDOWS)
40 const char *flag = common_flags()->color;
41 return internal_strcmp(flag, "always") == 0 ||
42 (internal_strcmp(flag, "auto") == 0 && report_file.SupportsColors());
45 static void (*sandboxing_callback)();
46 void SetSandboxingCallback(void (*f)()) {
47 sandboxing_callback = f;
50 void ReportErrorSummary(const char *error_type, const StackTrace *stack,
51 const char *alt_tool_name) {
53 if (!common_flags()->print_summary)
55 if (stack->size == 0) {
56 ReportErrorSummary(error_type);
59 // Currently, we include the first stack frame into the report summary.
60 // Maybe sometimes we need to choose another frame (e.g. skip memcpy/etc).
61 uptr pc = StackTrace::GetPreviousInstructionPc(stack->trace[0]);
62 SymbolizedStack *frame = Symbolizer::GetOrInit()->SymbolizePC(pc);
63 ReportErrorSummary(error_type, frame->info, alt_tool_name);
68 static void (*SoftRssLimitExceededCallback)(bool exceeded);
69 void SetSoftRssLimitExceededCallback(void (*Callback)(bool exceeded)) {
70 CHECK_EQ(SoftRssLimitExceededCallback, nullptr);
71 SoftRssLimitExceededCallback = Callback;
74 #if SANITIZER_LINUX && !SANITIZER_GO
75 void BackgroundThread(void *arg) {
76 uptr hard_rss_limit_mb = common_flags()->hard_rss_limit_mb;
77 uptr soft_rss_limit_mb = common_flags()->soft_rss_limit_mb;
78 bool heap_profile = common_flags()->heap_profile;
79 uptr prev_reported_rss = 0;
80 uptr prev_reported_stack_depot_size = 0;
81 bool reached_soft_rss_limit = false;
82 uptr rss_during_last_reported_profile = 0;
85 uptr current_rss_mb = GetRSS() >> 20;
87 // If RSS has grown 10% since last time, print some information.
88 if (prev_reported_rss * 11 / 10 < current_rss_mb) {
89 Printf("%s: RSS: %zdMb\n", SanitizerToolName, current_rss_mb);
90 prev_reported_rss = current_rss_mb;
92 // If stack depot has grown 10% since last time, print it too.
93 StackDepotStats *stack_depot_stats = StackDepotGetStats();
94 if (prev_reported_stack_depot_size * 11 / 10 <
95 stack_depot_stats->allocated) {
96 Printf("%s: StackDepot: %zd ids; %zdM allocated\n",
98 stack_depot_stats->n_uniq_ids,
99 stack_depot_stats->allocated >> 20);
100 prev_reported_stack_depot_size = stack_depot_stats->allocated;
103 // Check RSS against the limit.
104 if (hard_rss_limit_mb && hard_rss_limit_mb < current_rss_mb) {
105 Report("%s: hard rss limit exhausted (%zdMb vs %zdMb)\n",
106 SanitizerToolName, hard_rss_limit_mb, current_rss_mb);
110 if (soft_rss_limit_mb) {
111 if (soft_rss_limit_mb < current_rss_mb && !reached_soft_rss_limit) {
112 reached_soft_rss_limit = true;
113 Report("%s: soft rss limit exhausted (%zdMb vs %zdMb)\n",
114 SanitizerToolName, soft_rss_limit_mb, current_rss_mb);
115 if (SoftRssLimitExceededCallback)
116 SoftRssLimitExceededCallback(true);
117 } else if (soft_rss_limit_mb >= current_rss_mb &&
118 reached_soft_rss_limit) {
119 reached_soft_rss_limit = false;
120 if (SoftRssLimitExceededCallback)
121 SoftRssLimitExceededCallback(false);
125 current_rss_mb > rss_during_last_reported_profile * 1.1) {
126 Printf("\n\nHEAP PROFILE at RSS %zdMb\n", current_rss_mb);
127 __sanitizer_print_memory_profile(90, 20);
128 rss_during_last_reported_profile = current_rss_mb;
134 void WriteToSyslog(const char *msg) {
135 InternalScopedString msg_copy(kErrorMessageBufferSize);
136 msg_copy.append("%s", msg);
137 char *p = msg_copy.data();
140 // Print one line at a time.
141 // syslog, at least on Android, has an implicit message length limit.
143 q = internal_strchr(p, '\n');
146 WriteOneLineToSyslog(p);
152 void MaybeStartBackgroudThread() {
153 #if SANITIZER_LINUX && \
154 !SANITIZER_GO // Need to implement/test on other platforms.
155 // Start the background thread if one of the rss limits is given.
156 if (!common_flags()->hard_rss_limit_mb &&
157 !common_flags()->soft_rss_limit_mb &&
158 !common_flags()->heap_profile) return;
159 if (!&real_pthread_create) return; // Can't spawn the thread anyway.
160 internal_start_thread(BackgroundThread, nullptr);
164 } // namespace __sanitizer
166 SANITIZER_INTERFACE_WEAK_DEF(void, __sanitizer_sandbox_on_notify,
167 __sanitizer_sandbox_arguments *args) {
168 __sanitizer::PrepareForSandboxing(args);
169 if (__sanitizer::sandboxing_callback)
170 __sanitizer::sandboxing_callback();