1 //===-- sanitizer_common.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"
15 #include "sanitizer_allocator_interface.h"
16 #include "sanitizer_allocator_internal.h"
17 #include "sanitizer_flags.h"
18 #include "sanitizer_libc.h"
19 #include "sanitizer_placement_new.h"
20 #include "sanitizer_stacktrace_printer.h"
21 #include "sanitizer_symbolizer.h"
23 namespace __sanitizer {
25 const char *SanitizerToolName = "SanitizerTool";
27 atomic_uint32_t current_verbosity;
29 u32 NumberOfCPUsCached;
31 // PID of the tracer task in StopTheWorld. It shares the address space with the
32 // main process, but has a different PID and thus requires special handling.
33 uptr stoptheworld_tracer_pid = 0;
34 // Cached pid of parent process - if the parent process dies, we want to keep
35 // writing to the same log file.
36 uptr stoptheworld_tracer_ppid = 0;
38 void NORETURN ReportMmapFailureAndDie(uptr size, const char *mem_type,
39 const char *mmap_type, error_t err,
41 static int recursion_count;
42 if (raw_report || recursion_count) {
43 // If raw report is requested or we went into recursion, just die.
44 // The Report() and CHECK calls below may call mmap recursively and fail.
45 RawWrite("ERROR: Failed to mmap\n");
49 Report("ERROR: %s failed to "
50 "%s 0x%zx (%zd) bytes of %s (error code: %d)\n",
51 SanitizerToolName, mmap_type, size, size, mem_type, err);
55 UNREACHABLE("unable to mmap");
58 typedef bool UptrComparisonFunction(const uptr &a, const uptr &b);
59 typedef bool U32ComparisonFunction(const u32 &a, const u32 &b);
62 static inline bool CompareLess(const T &a, const T &b) {
66 void SortArray(uptr *array, uptr size) {
67 InternalSort<uptr*, UptrComparisonFunction>(&array, size, CompareLess);
70 void SortArray(u32 *array, uptr size) {
71 InternalSort<u32*, U32ComparisonFunction>(&array, size, CompareLess);
74 const char *StripPathPrefix(const char *filepath,
75 const char *strip_path_prefix) {
76 if (!filepath) return nullptr;
77 if (!strip_path_prefix) return filepath;
78 const char *res = filepath;
79 if (const char *pos = internal_strstr(filepath, strip_path_prefix))
80 res = pos + internal_strlen(strip_path_prefix);
81 if (res[0] == '.' && res[1] == '/')
86 const char *StripModuleName(const char *module) {
89 if (SANITIZER_WINDOWS) {
90 // On Windows, both slash and backslash are possible.
91 // Pick the one that goes last.
92 if (const char *bslash_pos = internal_strrchr(module, '\\'))
93 return StripModuleName(bslash_pos + 1);
95 if (const char *slash_pos = internal_strrchr(module, '/')) {
101 void ReportErrorSummary(const char *error_message, const char *alt_tool_name) {
102 if (!common_flags()->print_summary)
104 InternalScopedString buff(kMaxSummaryLength);
105 buff.append("SUMMARY: %s: %s",
106 alt_tool_name ? alt_tool_name : SanitizerToolName, error_message);
107 __sanitizer_report_error_summary(buff.data());
111 void ReportErrorSummary(const char *error_type, const AddressInfo &info,
112 const char *alt_tool_name) {
113 if (!common_flags()->print_summary) return;
114 InternalScopedString buff(kMaxSummaryLength);
115 buff.append("%s ", error_type);
116 RenderFrame(&buff, "%L %F", 0, info, common_flags()->symbolize_vs_style,
117 common_flags()->strip_path_prefix);
118 ReportErrorSummary(buff.data(), alt_tool_name);
122 // Removes the ANSI escape sequences from the input string (in-place).
123 void RemoveANSIEscapeSequencesFromString(char *str) {
127 // We are going to remove the escape sequences in place.
132 // Skip over ANSI escape sequences with pointer 's'.
133 if (*s == '\033' && *(s + 1) == '[') {
134 s = internal_strchrnul(s, 'm');
141 // 's' now points at a character we want to keep. Copy over the buffer
142 // content if the escape sequence has been perviously skipped andadvance
147 // If we have not seen an escape sequence, just advance both pointers.
152 // Null terminate the string.
156 void LoadedModule::set(const char *module_name, uptr base_address) {
158 full_name_ = internal_strdup(module_name);
159 base_address_ = base_address;
162 void LoadedModule::set(const char *module_name, uptr base_address,
163 ModuleArch arch, u8 uuid[kModuleUUIDSize],
165 set(module_name, base_address);
167 internal_memcpy(uuid_, uuid, sizeof(uuid_));
168 instrumented_ = instrumented;
171 void LoadedModule::clear() {
172 InternalFree(full_name_);
174 max_executable_address_ = 0;
175 full_name_ = nullptr;
176 arch_ = kModuleArchUnknown;
177 internal_memset(uuid_, 0, kModuleUUIDSize);
178 instrumented_ = false;
179 while (!ranges_.empty()) {
180 AddressRange *r = ranges_.front();
186 void LoadedModule::addAddressRange(uptr beg, uptr end, bool executable,
187 bool writable, const char *name) {
188 void *mem = InternalAlloc(sizeof(AddressRange));
190 new(mem) AddressRange(beg, end, executable, writable, name);
191 ranges_.push_back(r);
192 if (executable && end > max_executable_address_)
193 max_executable_address_ = end;
196 bool LoadedModule::containsAddress(uptr address) const {
197 for (const AddressRange &r : ranges()) {
198 if (r.beg <= address && address < r.end)
204 static atomic_uintptr_t g_total_mmaped;
206 void IncreaseTotalMmap(uptr size) {
207 if (!common_flags()->mmap_limit_mb) return;
209 atomic_fetch_add(&g_total_mmaped, size, memory_order_relaxed) + size;
210 // Since for now mmap_limit_mb is not a user-facing flag, just kill
211 // a program. Use RAW_CHECK to avoid extra mmaps in reporting.
212 RAW_CHECK((total_mmaped >> 20) < common_flags()->mmap_limit_mb);
215 void DecreaseTotalMmap(uptr size) {
216 if (!common_flags()->mmap_limit_mb) return;
217 atomic_fetch_sub(&g_total_mmaped, size, memory_order_relaxed);
220 bool TemplateMatch(const char *templ, const char *str) {
221 if ((!str) || str[0] == 0)
224 if (templ && templ[0] == '^') {
228 bool asterisk = false;
229 while (templ && templ[0]) {
230 if (templ[0] == '*') {
237 return str[0] == 0 || asterisk;
240 char *tpos = (char*)internal_strchr(templ, '*');
241 char *tpos1 = (char*)internal_strchr(templ, '$');
242 if ((!tpos) || (tpos1 && tpos1 < tpos))
246 const char *str0 = str;
247 const char *spos = internal_strstr(str, templ);
248 str = spos + internal_strlen(templ);
251 tpos[0] = tpos == tpos1 ? '$' : '*';
254 if (start && spos != str0)
262 static char binary_name_cache_str[kMaxPathLength];
263 static char process_name_cache_str[kMaxPathLength];
265 const char *GetProcessName() {
266 return process_name_cache_str;
269 static uptr ReadProcessName(/*out*/ char *buf, uptr buf_len) {
270 ReadLongProcessName(buf, buf_len);
271 char *s = const_cast<char *>(StripModuleName(buf));
272 uptr len = internal_strlen(s);
274 internal_memmove(buf, s, len);
280 void UpdateProcessName() {
281 ReadProcessName(process_name_cache_str, sizeof(process_name_cache_str));
284 // Call once to make sure that binary_name_cache_str is initialized
285 void CacheBinaryName() {
286 if (binary_name_cache_str[0] != '\0')
288 ReadBinaryName(binary_name_cache_str, sizeof(binary_name_cache_str));
289 ReadProcessName(process_name_cache_str, sizeof(process_name_cache_str));
292 uptr ReadBinaryNameCached(/*out*/char *buf, uptr buf_len) {
294 uptr name_len = internal_strlen(binary_name_cache_str);
295 name_len = (name_len < buf_len - 1) ? name_len : buf_len - 1;
298 internal_memcpy(buf, binary_name_cache_str, name_len);
299 buf[name_len] = '\0';
303 void PrintCmdline() {
304 char **argv = GetArgv();
306 Printf("\nCommand: ");
307 for (uptr i = 0; argv[i]; ++i)
308 Printf("%s ", argv[i]);
313 static const int kMaxMallocFreeHooks = 5;
314 struct MallocFreeHook {
315 void (*malloc_hook)(const void *, uptr);
316 void (*free_hook)(const void *);
319 static MallocFreeHook MFHooks[kMaxMallocFreeHooks];
321 void RunMallocHooks(const void *ptr, uptr size) {
322 for (int i = 0; i < kMaxMallocFreeHooks; i++) {
323 auto hook = MFHooks[i].malloc_hook;
329 void RunFreeHooks(const void *ptr) {
330 for (int i = 0; i < kMaxMallocFreeHooks; i++) {
331 auto hook = MFHooks[i].free_hook;
337 static int InstallMallocFreeHooks(void (*malloc_hook)(const void *, uptr),
338 void (*free_hook)(const void *)) {
339 if (!malloc_hook || !free_hook) return 0;
340 for (int i = 0; i < kMaxMallocFreeHooks; i++) {
341 if (MFHooks[i].malloc_hook == nullptr) {
342 MFHooks[i].malloc_hook = malloc_hook;
343 MFHooks[i].free_hook = free_hook;
350 } // namespace __sanitizer
352 using namespace __sanitizer; // NOLINT
355 SANITIZER_INTERFACE_WEAK_DEF(void, __sanitizer_report_error_summary,
356 const char *error_summary) {
357 Printf("%s\n", error_summary);
360 SANITIZER_INTERFACE_ATTRIBUTE
361 void __sanitizer_set_death_callback(void (*callback)(void)) {
362 SetUserDieCallback(callback);
365 SANITIZER_INTERFACE_ATTRIBUTE
366 int __sanitizer_install_malloc_and_free_hooks(void (*malloc_hook)(const void *,
368 void (*free_hook)(const void *)) {
369 return InstallMallocFreeHooks(malloc_hook, free_hook);