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) {
52 if (!common_flags()->print_summary)
54 if (stack->size == 0) {
55 ReportErrorSummary(error_type);
58 // Currently, we include the first stack frame into the report summary.
59 // Maybe sometimes we need to choose another frame (e.g. skip memcpy/etc).
60 uptr pc = StackTrace::GetPreviousInstructionPc(stack->trace[0]);
61 SymbolizedStack *frame = Symbolizer::GetOrInit()->SymbolizePC(pc);
62 ReportErrorSummary(error_type, frame->info);
67 static void (*SoftRssLimitExceededCallback)(bool exceeded);
68 void SetSoftRssLimitExceededCallback(void (*Callback)(bool exceeded)) {
69 CHECK_EQ(SoftRssLimitExceededCallback, nullptr);
70 SoftRssLimitExceededCallback = Callback;
73 #if SANITIZER_LINUX && !SANITIZER_GO
74 void BackgroundThread(void *arg) {
75 uptr hard_rss_limit_mb = common_flags()->hard_rss_limit_mb;
76 uptr soft_rss_limit_mb = common_flags()->soft_rss_limit_mb;
77 bool heap_profile = common_flags()->heap_profile;
78 uptr prev_reported_rss = 0;
79 uptr prev_reported_stack_depot_size = 0;
80 bool reached_soft_rss_limit = false;
81 uptr rss_during_last_reported_profile = 0;
84 uptr current_rss_mb = GetRSS() >> 20;
86 // If RSS has grown 10% since last time, print some information.
87 if (prev_reported_rss * 11 / 10 < current_rss_mb) {
88 Printf("%s: RSS: %zdMb\n", SanitizerToolName, current_rss_mb);
89 prev_reported_rss = current_rss_mb;
91 // If stack depot has grown 10% since last time, print it too.
92 StackDepotStats *stack_depot_stats = StackDepotGetStats();
93 if (prev_reported_stack_depot_size * 11 / 10 <
94 stack_depot_stats->allocated) {
95 Printf("%s: StackDepot: %zd ids; %zdM allocated\n",
97 stack_depot_stats->n_uniq_ids,
98 stack_depot_stats->allocated >> 20);
99 prev_reported_stack_depot_size = stack_depot_stats->allocated;
102 // Check RSS against the limit.
103 if (hard_rss_limit_mb && hard_rss_limit_mb < current_rss_mb) {
104 Report("%s: hard rss limit exhausted (%zdMb vs %zdMb)\n",
105 SanitizerToolName, hard_rss_limit_mb, current_rss_mb);
109 if (soft_rss_limit_mb) {
110 if (soft_rss_limit_mb < current_rss_mb && !reached_soft_rss_limit) {
111 reached_soft_rss_limit = true;
112 Report("%s: soft rss limit exhausted (%zdMb vs %zdMb)\n",
113 SanitizerToolName, soft_rss_limit_mb, current_rss_mb);
114 if (SoftRssLimitExceededCallback)
115 SoftRssLimitExceededCallback(true);
116 } else if (soft_rss_limit_mb >= current_rss_mb &&
117 reached_soft_rss_limit) {
118 reached_soft_rss_limit = false;
119 if (SoftRssLimitExceededCallback)
120 SoftRssLimitExceededCallback(false);
124 current_rss_mb > rss_during_last_reported_profile * 1.1) {
125 Printf("\n\nHEAP PROFILE at RSS %zdMb\n", current_rss_mb);
126 __sanitizer_print_memory_profile(90);
127 rss_during_last_reported_profile = current_rss_mb;
133 void WriteToSyslog(const char *msg) {
134 InternalScopedString msg_copy(kErrorMessageBufferSize);
135 msg_copy.append("%s", msg);
136 char *p = msg_copy.data();
139 // Print one line at a time.
140 // syslog, at least on Android, has an implicit message length limit.
142 q = internal_strchr(p, '\n');
145 WriteOneLineToSyslog(p);
151 void MaybeStartBackgroudThread() {
152 #if SANITIZER_LINUX && \
153 !SANITIZER_GO // Need to implement/test on other platforms.
154 // Start the background thread if one of the rss limits is given.
155 if (!common_flags()->hard_rss_limit_mb &&
156 !common_flags()->soft_rss_limit_mb &&
157 !common_flags()->heap_profile) return;
158 if (!&real_pthread_create) return; // Can't spawn the thread anyway.
159 internal_start_thread(BackgroundThread, nullptr);
163 } // namespace __sanitizer
166 __sanitizer_sandbox_on_notify(__sanitizer_sandbox_arguments *args) {
167 __sanitizer::PrepareForSandboxing(args);
168 if (__sanitizer::sandboxing_callback)
169 __sanitizer::sandboxing_callback();