]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/compiler-rt/lib/sanitizer_common/sanitizer_common_libcdep.cc
Merge ^/head r318560 through r318657.
[FreeBSD/FreeBSD.git] / contrib / compiler-rt / lib / sanitizer_common / sanitizer_common_libcdep.cc
1 //===-- sanitizer_common_libcdep.cc ---------------------------------------===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This file is shared between AddressSanitizer and ThreadSanitizer
11 // run-time libraries.
12 //===----------------------------------------------------------------------===//
13
14 #include "sanitizer_common.h"
15
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"
21
22 #if SANITIZER_POSIX
23 #include "sanitizer_posix.h"
24 #endif
25
26 namespace __sanitizer {
27
28 bool ReportFile::SupportsColors() {
29   SpinMutexLock l(mu);
30   ReopenIfNecessary();
31   return SupportsColoredOutput(fd);
32 }
33
34 bool ColorizeReports() {
35   // FIXME: Add proper Windows support to AnsiColorDecorator and re-enable color
36   // printing on Windows.
37   if (SANITIZER_WINDOWS)
38     return false;
39
40   const char *flag = common_flags()->color;
41   return internal_strcmp(flag, "always") == 0 ||
42          (internal_strcmp(flag, "auto") == 0 && report_file.SupportsColors());
43 }
44
45 static void (*sandboxing_callback)();
46 void SetSandboxingCallback(void (*f)()) {
47   sandboxing_callback = f;
48 }
49
50 void ReportErrorSummary(const char *error_type, const StackTrace *stack,
51                         const char *alt_tool_name) {
52 #if !SANITIZER_GO
53   if (!common_flags()->print_summary)
54     return;
55   if (stack->size == 0) {
56     ReportErrorSummary(error_type);
57     return;
58   }
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);
64   frame->ClearAll();
65 #endif
66 }
67
68 static void (*SoftRssLimitExceededCallback)(bool exceeded);
69 void SetSoftRssLimitExceededCallback(void (*Callback)(bool exceeded)) {
70   CHECK_EQ(SoftRssLimitExceededCallback, nullptr);
71   SoftRssLimitExceededCallback = Callback;
72 }
73
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;
83   while (true) {
84     SleepForMillis(100);
85     uptr current_rss_mb = GetRSS() >> 20;
86     if (Verbosity()) {
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;
91       }
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",
97                SanitizerToolName,
98                stack_depot_stats->n_uniq_ids,
99                stack_depot_stats->allocated >> 20);
100         prev_reported_stack_depot_size = stack_depot_stats->allocated;
101       }
102     }
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);
107       DumpProcessMap();
108       Die();
109     }
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);
122       }
123     }
124     if (heap_profile &&
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;
129     }
130   }
131 }
132 #endif
133
134 void WriteToSyslog(const char *msg) {
135   InternalScopedString msg_copy(kErrorMessageBufferSize);
136   msg_copy.append("%s", msg);
137   char *p = msg_copy.data();
138   char *q;
139
140   // Print one line at a time.
141   // syslog, at least on Android, has an implicit message length limit.
142   do {
143     q = internal_strchr(p, '\n');
144     if (q)
145       *q = '\0';
146     WriteOneLineToSyslog(p);
147     if (q)
148       p = q + 1;
149   } while (q);
150 }
151
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);
161 #endif
162 }
163
164 }  // namespace __sanitizer
165
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();
171 }