]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/compiler-rt/lib/sanitizer_common/sanitizer_common_libcdep.cc
Merge compiler-rt r291274.
[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 #if !SANITIZER_GO
52   if (!common_flags()->print_summary)
53     return;
54   if (stack->size == 0) {
55     ReportErrorSummary(error_type);
56     return;
57   }
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);
63   frame->ClearAll();
64 #endif
65 }
66
67 static void (*SoftRssLimitExceededCallback)(bool exceeded);
68 void SetSoftRssLimitExceededCallback(void (*Callback)(bool exceeded)) {
69   CHECK_EQ(SoftRssLimitExceededCallback, nullptr);
70   SoftRssLimitExceededCallback = Callback;
71 }
72
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;
82   while (true) {
83     SleepForMillis(100);
84     uptr current_rss_mb = GetRSS() >> 20;
85     if (Verbosity()) {
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;
90       }
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",
96                SanitizerToolName,
97                stack_depot_stats->n_uniq_ids,
98                stack_depot_stats->allocated >> 20);
99         prev_reported_stack_depot_size = stack_depot_stats->allocated;
100       }
101     }
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);
106       DumpProcessMap();
107       Die();
108     }
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);
121       }
122     }
123     if (heap_profile &&
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;
128     }
129   }
130 }
131 #endif
132
133 void WriteToSyslog(const char *msg) {
134   InternalScopedString msg_copy(kErrorMessageBufferSize);
135   msg_copy.append("%s", msg);
136   char *p = msg_copy.data();
137   char *q;
138
139   // Print one line at a time.
140   // syslog, at least on Android, has an implicit message length limit.
141   do {
142     q = internal_strchr(p, '\n');
143     if (q)
144       *q = '\0';
145     WriteOneLineToSyslog(p);
146     if (q)
147       p = q + 1;
148   } while (q);
149 }
150
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);
160 #endif
161 }
162
163 }  // namespace __sanitizer
164
165 void NOINLINE
166 __sanitizer_sandbox_on_notify(__sanitizer_sandbox_arguments *args) {
167   __sanitizer::PrepareForSandboxing(args);
168   if (__sanitizer::sandboxing_callback)
169     __sanitizer::sandboxing_callback();
170 }