168d75effSDimitry Andric //===-- sanitizer_common.cpp ----------------------------------------------===// 268d75effSDimitry Andric // 368d75effSDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 468d75effSDimitry Andric // See https://llvm.org/LICENSE.txt for license information. 568d75effSDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 668d75effSDimitry Andric // 768d75effSDimitry Andric //===----------------------------------------------------------------------===// 868d75effSDimitry Andric // 968d75effSDimitry Andric // This file is shared between AddressSanitizer and ThreadSanitizer 1068d75effSDimitry Andric // run-time libraries. 1168d75effSDimitry Andric //===----------------------------------------------------------------------===// 1268d75effSDimitry Andric 1368d75effSDimitry Andric #include "sanitizer_common.h" 1468d75effSDimitry Andric #include "sanitizer_allocator_interface.h" 1568d75effSDimitry Andric #include "sanitizer_allocator_internal.h" 1668d75effSDimitry Andric #include "sanitizer_atomic.h" 1768d75effSDimitry Andric #include "sanitizer_flags.h" 1868d75effSDimitry Andric #include "sanitizer_libc.h" 1968d75effSDimitry Andric #include "sanitizer_placement_new.h" 2068d75effSDimitry Andric 2168d75effSDimitry Andric namespace __sanitizer { 2268d75effSDimitry Andric 2368d75effSDimitry Andric const char *SanitizerToolName = "SanitizerTool"; 2468d75effSDimitry Andric 2568d75effSDimitry Andric atomic_uint32_t current_verbosity; 2668d75effSDimitry Andric uptr PageSizeCached; 2768d75effSDimitry Andric u32 NumberOfCPUsCached; 2868d75effSDimitry Andric 2968d75effSDimitry Andric // PID of the tracer task in StopTheWorld. It shares the address space with the 3068d75effSDimitry Andric // main process, but has a different PID and thus requires special handling. 3168d75effSDimitry Andric uptr stoptheworld_tracer_pid = 0; 3268d75effSDimitry Andric // Cached pid of parent process - if the parent process dies, we want to keep 3368d75effSDimitry Andric // writing to the same log file. 3468d75effSDimitry Andric uptr stoptheworld_tracer_ppid = 0; 3568d75effSDimitry Andric 3668d75effSDimitry Andric void NORETURN ReportMmapFailureAndDie(uptr size, const char *mem_type, 3768d75effSDimitry Andric const char *mmap_type, error_t err, 3868d75effSDimitry Andric bool raw_report) { 3968d75effSDimitry Andric static int recursion_count; 40fe6060f1SDimitry Andric if (raw_report || recursion_count) { 41fe6060f1SDimitry Andric // If raw report is requested or we went into recursion just die. The 42fe6060f1SDimitry Andric // Report() and CHECK calls below may call mmap recursively and fail. 4368d75effSDimitry Andric RawWrite("ERROR: Failed to mmap\n"); 4468d75effSDimitry Andric Die(); 4568d75effSDimitry Andric } 4668d75effSDimitry Andric recursion_count++; 4768d75effSDimitry Andric Report("ERROR: %s failed to " 4868d75effSDimitry Andric "%s 0x%zx (%zd) bytes of %s (error code: %d)\n", 4968d75effSDimitry Andric SanitizerToolName, mmap_type, size, size, mem_type, err); 5068d75effSDimitry Andric #if !SANITIZER_GO 5168d75effSDimitry Andric DumpProcessMap(); 5268d75effSDimitry Andric #endif 5368d75effSDimitry Andric UNREACHABLE("unable to mmap"); 5468d75effSDimitry Andric } 5568d75effSDimitry Andric 5668d75effSDimitry Andric typedef bool UptrComparisonFunction(const uptr &a, const uptr &b); 5768d75effSDimitry Andric typedef bool U32ComparisonFunction(const u32 &a, const u32 &b); 5868d75effSDimitry Andric 5968d75effSDimitry Andric const char *StripPathPrefix(const char *filepath, 6068d75effSDimitry Andric const char *strip_path_prefix) { 6168d75effSDimitry Andric if (!filepath) return nullptr; 6268d75effSDimitry Andric if (!strip_path_prefix) return filepath; 6368d75effSDimitry Andric const char *res = filepath; 6468d75effSDimitry Andric if (const char *pos = internal_strstr(filepath, strip_path_prefix)) 6568d75effSDimitry Andric res = pos + internal_strlen(strip_path_prefix); 6668d75effSDimitry Andric if (res[0] == '.' && res[1] == '/') 6768d75effSDimitry Andric res += 2; 6868d75effSDimitry Andric return res; 6968d75effSDimitry Andric } 7068d75effSDimitry Andric 7168d75effSDimitry Andric const char *StripModuleName(const char *module) { 7268d75effSDimitry Andric if (!module) 7368d75effSDimitry Andric return nullptr; 7468d75effSDimitry Andric if (SANITIZER_WINDOWS) { 7568d75effSDimitry Andric // On Windows, both slash and backslash are possible. 7668d75effSDimitry Andric // Pick the one that goes last. 7768d75effSDimitry Andric if (const char *bslash_pos = internal_strrchr(module, '\\')) 7868d75effSDimitry Andric return StripModuleName(bslash_pos + 1); 7968d75effSDimitry Andric } 8068d75effSDimitry Andric if (const char *slash_pos = internal_strrchr(module, '/')) { 8168d75effSDimitry Andric return slash_pos + 1; 8268d75effSDimitry Andric } 8368d75effSDimitry Andric return module; 8468d75effSDimitry Andric } 8568d75effSDimitry Andric 8668d75effSDimitry Andric void ReportErrorSummary(const char *error_message, const char *alt_tool_name) { 8768d75effSDimitry Andric if (!common_flags()->print_summary) 8868d75effSDimitry Andric return; 89fe6060f1SDimitry Andric InternalScopedString buff; 9068d75effSDimitry Andric buff.append("SUMMARY: %s: %s", 9168d75effSDimitry Andric alt_tool_name ? alt_tool_name : SanitizerToolName, error_message); 9268d75effSDimitry Andric __sanitizer_report_error_summary(buff.data()); 9368d75effSDimitry Andric } 9468d75effSDimitry Andric 9568d75effSDimitry Andric // Removes the ANSI escape sequences from the input string (in-place). 9668d75effSDimitry Andric void RemoveANSIEscapeSequencesFromString(char *str) { 9768d75effSDimitry Andric if (!str) 9868d75effSDimitry Andric return; 9968d75effSDimitry Andric 10068d75effSDimitry Andric // We are going to remove the escape sequences in place. 10168d75effSDimitry Andric char *s = str; 10268d75effSDimitry Andric char *z = str; 10368d75effSDimitry Andric while (*s != '\0') { 10468d75effSDimitry Andric CHECK_GE(s, z); 10568d75effSDimitry Andric // Skip over ANSI escape sequences with pointer 's'. 10668d75effSDimitry Andric if (*s == '\033' && *(s + 1) == '[') { 10768d75effSDimitry Andric s = internal_strchrnul(s, 'm'); 10868d75effSDimitry Andric if (*s == '\0') { 10968d75effSDimitry Andric break; 11068d75effSDimitry Andric } 11168d75effSDimitry Andric s++; 11268d75effSDimitry Andric continue; 11368d75effSDimitry Andric } 11468d75effSDimitry Andric // 's' now points at a character we want to keep. Copy over the buffer 11568d75effSDimitry Andric // content if the escape sequence has been perviously skipped andadvance 11668d75effSDimitry Andric // both pointers. 11768d75effSDimitry Andric if (s != z) 11868d75effSDimitry Andric *z = *s; 11968d75effSDimitry Andric 12068d75effSDimitry Andric // If we have not seen an escape sequence, just advance both pointers. 12168d75effSDimitry Andric z++; 12268d75effSDimitry Andric s++; 12368d75effSDimitry Andric } 12468d75effSDimitry Andric 12568d75effSDimitry Andric // Null terminate the string. 12668d75effSDimitry Andric *z = '\0'; 12768d75effSDimitry Andric } 12868d75effSDimitry Andric 12968d75effSDimitry Andric void LoadedModule::set(const char *module_name, uptr base_address) { 13068d75effSDimitry Andric clear(); 13168d75effSDimitry Andric full_name_ = internal_strdup(module_name); 13268d75effSDimitry Andric base_address_ = base_address; 13368d75effSDimitry Andric } 13468d75effSDimitry Andric 13568d75effSDimitry Andric void LoadedModule::set(const char *module_name, uptr base_address, 13668d75effSDimitry Andric ModuleArch arch, u8 uuid[kModuleUUIDSize], 13768d75effSDimitry Andric bool instrumented) { 13868d75effSDimitry Andric set(module_name, base_address); 13968d75effSDimitry Andric arch_ = arch; 14068d75effSDimitry Andric internal_memcpy(uuid_, uuid, sizeof(uuid_)); 141*0eae32dcSDimitry Andric uuid_size_ = kModuleUUIDSize; 14268d75effSDimitry Andric instrumented_ = instrumented; 14368d75effSDimitry Andric } 14468d75effSDimitry Andric 145*0eae32dcSDimitry Andric void LoadedModule::setUuid(const char *uuid, uptr size) { 146*0eae32dcSDimitry Andric if (size > kModuleUUIDSize) 147*0eae32dcSDimitry Andric size = kModuleUUIDSize; 148*0eae32dcSDimitry Andric internal_memcpy(uuid_, uuid, size); 149*0eae32dcSDimitry Andric uuid_size_ = size; 150*0eae32dcSDimitry Andric } 151*0eae32dcSDimitry Andric 15268d75effSDimitry Andric void LoadedModule::clear() { 15368d75effSDimitry Andric InternalFree(full_name_); 15468d75effSDimitry Andric base_address_ = 0; 15568d75effSDimitry Andric max_executable_address_ = 0; 15668d75effSDimitry Andric full_name_ = nullptr; 15768d75effSDimitry Andric arch_ = kModuleArchUnknown; 15868d75effSDimitry Andric internal_memset(uuid_, 0, kModuleUUIDSize); 15968d75effSDimitry Andric instrumented_ = false; 16068d75effSDimitry Andric while (!ranges_.empty()) { 16168d75effSDimitry Andric AddressRange *r = ranges_.front(); 16268d75effSDimitry Andric ranges_.pop_front(); 16368d75effSDimitry Andric InternalFree(r); 16468d75effSDimitry Andric } 16568d75effSDimitry Andric } 16668d75effSDimitry Andric 16768d75effSDimitry Andric void LoadedModule::addAddressRange(uptr beg, uptr end, bool executable, 16868d75effSDimitry Andric bool writable, const char *name) { 16968d75effSDimitry Andric void *mem = InternalAlloc(sizeof(AddressRange)); 17068d75effSDimitry Andric AddressRange *r = 17168d75effSDimitry Andric new(mem) AddressRange(beg, end, executable, writable, name); 17268d75effSDimitry Andric ranges_.push_back(r); 17368d75effSDimitry Andric if (executable && end > max_executable_address_) 17468d75effSDimitry Andric max_executable_address_ = end; 17568d75effSDimitry Andric } 17668d75effSDimitry Andric 17768d75effSDimitry Andric bool LoadedModule::containsAddress(uptr address) const { 17868d75effSDimitry Andric for (const AddressRange &r : ranges()) { 17968d75effSDimitry Andric if (r.beg <= address && address < r.end) 18068d75effSDimitry Andric return true; 18168d75effSDimitry Andric } 18268d75effSDimitry Andric return false; 18368d75effSDimitry Andric } 18468d75effSDimitry Andric 18568d75effSDimitry Andric static atomic_uintptr_t g_total_mmaped; 18668d75effSDimitry Andric 18768d75effSDimitry Andric void IncreaseTotalMmap(uptr size) { 18868d75effSDimitry Andric if (!common_flags()->mmap_limit_mb) return; 18968d75effSDimitry Andric uptr total_mmaped = 19068d75effSDimitry Andric atomic_fetch_add(&g_total_mmaped, size, memory_order_relaxed) + size; 19168d75effSDimitry Andric // Since for now mmap_limit_mb is not a user-facing flag, just kill 19268d75effSDimitry Andric // a program. Use RAW_CHECK to avoid extra mmaps in reporting. 19368d75effSDimitry Andric RAW_CHECK((total_mmaped >> 20) < common_flags()->mmap_limit_mb); 19468d75effSDimitry Andric } 19568d75effSDimitry Andric 19668d75effSDimitry Andric void DecreaseTotalMmap(uptr size) { 19768d75effSDimitry Andric if (!common_flags()->mmap_limit_mb) return; 19868d75effSDimitry Andric atomic_fetch_sub(&g_total_mmaped, size, memory_order_relaxed); 19968d75effSDimitry Andric } 20068d75effSDimitry Andric 20168d75effSDimitry Andric bool TemplateMatch(const char *templ, const char *str) { 20268d75effSDimitry Andric if ((!str) || str[0] == 0) 20368d75effSDimitry Andric return false; 20468d75effSDimitry Andric bool start = false; 20568d75effSDimitry Andric if (templ && templ[0] == '^') { 20668d75effSDimitry Andric start = true; 20768d75effSDimitry Andric templ++; 20868d75effSDimitry Andric } 20968d75effSDimitry Andric bool asterisk = false; 21068d75effSDimitry Andric while (templ && templ[0]) { 21168d75effSDimitry Andric if (templ[0] == '*') { 21268d75effSDimitry Andric templ++; 21368d75effSDimitry Andric start = false; 21468d75effSDimitry Andric asterisk = true; 21568d75effSDimitry Andric continue; 21668d75effSDimitry Andric } 21768d75effSDimitry Andric if (templ[0] == '$') 21868d75effSDimitry Andric return str[0] == 0 || asterisk; 21968d75effSDimitry Andric if (str[0] == 0) 22068d75effSDimitry Andric return false; 22168d75effSDimitry Andric char *tpos = (char*)internal_strchr(templ, '*'); 22268d75effSDimitry Andric char *tpos1 = (char*)internal_strchr(templ, '$'); 22368d75effSDimitry Andric if ((!tpos) || (tpos1 && tpos1 < tpos)) 22468d75effSDimitry Andric tpos = tpos1; 22568d75effSDimitry Andric if (tpos) 22668d75effSDimitry Andric tpos[0] = 0; 22768d75effSDimitry Andric const char *str0 = str; 22868d75effSDimitry Andric const char *spos = internal_strstr(str, templ); 22968d75effSDimitry Andric str = spos + internal_strlen(templ); 23068d75effSDimitry Andric templ = tpos; 23168d75effSDimitry Andric if (tpos) 23268d75effSDimitry Andric tpos[0] = tpos == tpos1 ? '$' : '*'; 23368d75effSDimitry Andric if (!spos) 23468d75effSDimitry Andric return false; 23568d75effSDimitry Andric if (start && spos != str0) 23668d75effSDimitry Andric return false; 23768d75effSDimitry Andric start = false; 23868d75effSDimitry Andric asterisk = false; 23968d75effSDimitry Andric } 24068d75effSDimitry Andric return true; 24168d75effSDimitry Andric } 24268d75effSDimitry Andric 24368d75effSDimitry Andric static char binary_name_cache_str[kMaxPathLength]; 24468d75effSDimitry Andric static char process_name_cache_str[kMaxPathLength]; 24568d75effSDimitry Andric 24668d75effSDimitry Andric const char *GetProcessName() { 24768d75effSDimitry Andric return process_name_cache_str; 24868d75effSDimitry Andric } 24968d75effSDimitry Andric 25068d75effSDimitry Andric static uptr ReadProcessName(/*out*/ char *buf, uptr buf_len) { 25168d75effSDimitry Andric ReadLongProcessName(buf, buf_len); 25268d75effSDimitry Andric char *s = const_cast<char *>(StripModuleName(buf)); 25368d75effSDimitry Andric uptr len = internal_strlen(s); 25468d75effSDimitry Andric if (s != buf) { 25568d75effSDimitry Andric internal_memmove(buf, s, len); 25668d75effSDimitry Andric buf[len] = '\0'; 25768d75effSDimitry Andric } 25868d75effSDimitry Andric return len; 25968d75effSDimitry Andric } 26068d75effSDimitry Andric 26168d75effSDimitry Andric void UpdateProcessName() { 26268d75effSDimitry Andric ReadProcessName(process_name_cache_str, sizeof(process_name_cache_str)); 26368d75effSDimitry Andric } 26468d75effSDimitry Andric 26568d75effSDimitry Andric // Call once to make sure that binary_name_cache_str is initialized 26668d75effSDimitry Andric void CacheBinaryName() { 26768d75effSDimitry Andric if (binary_name_cache_str[0] != '\0') 26868d75effSDimitry Andric return; 26968d75effSDimitry Andric ReadBinaryName(binary_name_cache_str, sizeof(binary_name_cache_str)); 27068d75effSDimitry Andric ReadProcessName(process_name_cache_str, sizeof(process_name_cache_str)); 27168d75effSDimitry Andric } 27268d75effSDimitry Andric 27368d75effSDimitry Andric uptr ReadBinaryNameCached(/*out*/char *buf, uptr buf_len) { 27468d75effSDimitry Andric CacheBinaryName(); 27568d75effSDimitry Andric uptr name_len = internal_strlen(binary_name_cache_str); 27668d75effSDimitry Andric name_len = (name_len < buf_len - 1) ? name_len : buf_len - 1; 27768d75effSDimitry Andric if (buf_len == 0) 27868d75effSDimitry Andric return 0; 27968d75effSDimitry Andric internal_memcpy(buf, binary_name_cache_str, name_len); 28068d75effSDimitry Andric buf[name_len] = '\0'; 28168d75effSDimitry Andric return name_len; 28268d75effSDimitry Andric } 28368d75effSDimitry Andric 284fe6060f1SDimitry Andric uptr ReadBinaryDir(/*out*/ char *buf, uptr buf_len) { 285fe6060f1SDimitry Andric ReadBinaryNameCached(buf, buf_len); 286fe6060f1SDimitry Andric const char *exec_name_pos = StripModuleName(buf); 287fe6060f1SDimitry Andric uptr name_len = exec_name_pos - buf; 288fe6060f1SDimitry Andric buf[name_len] = '\0'; 289fe6060f1SDimitry Andric return name_len; 290fe6060f1SDimitry Andric } 291fe6060f1SDimitry Andric 2925ffd83dbSDimitry Andric #if !SANITIZER_GO 29368d75effSDimitry Andric void PrintCmdline() { 29468d75effSDimitry Andric char **argv = GetArgv(); 29568d75effSDimitry Andric if (!argv) return; 29668d75effSDimitry Andric Printf("\nCommand: "); 29768d75effSDimitry Andric for (uptr i = 0; argv[i]; ++i) 29868d75effSDimitry Andric Printf("%s ", argv[i]); 29968d75effSDimitry Andric Printf("\n\n"); 30068d75effSDimitry Andric } 3015ffd83dbSDimitry Andric #endif 30268d75effSDimitry Andric 30368d75effSDimitry Andric // Malloc hooks. 30468d75effSDimitry Andric static const int kMaxMallocFreeHooks = 5; 30568d75effSDimitry Andric struct MallocFreeHook { 30668d75effSDimitry Andric void (*malloc_hook)(const void *, uptr); 30768d75effSDimitry Andric void (*free_hook)(const void *); 30868d75effSDimitry Andric }; 30968d75effSDimitry Andric 31068d75effSDimitry Andric static MallocFreeHook MFHooks[kMaxMallocFreeHooks]; 31168d75effSDimitry Andric 31268d75effSDimitry Andric void RunMallocHooks(const void *ptr, uptr size) { 31368d75effSDimitry Andric for (int i = 0; i < kMaxMallocFreeHooks; i++) { 31468d75effSDimitry Andric auto hook = MFHooks[i].malloc_hook; 31568d75effSDimitry Andric if (!hook) return; 31668d75effSDimitry Andric hook(ptr, size); 31768d75effSDimitry Andric } 31868d75effSDimitry Andric } 31968d75effSDimitry Andric 32068d75effSDimitry Andric void RunFreeHooks(const void *ptr) { 32168d75effSDimitry Andric for (int i = 0; i < kMaxMallocFreeHooks; i++) { 32268d75effSDimitry Andric auto hook = MFHooks[i].free_hook; 32368d75effSDimitry Andric if (!hook) return; 32468d75effSDimitry Andric hook(ptr); 32568d75effSDimitry Andric } 32668d75effSDimitry Andric } 32768d75effSDimitry Andric 32868d75effSDimitry Andric static int InstallMallocFreeHooks(void (*malloc_hook)(const void *, uptr), 32968d75effSDimitry Andric void (*free_hook)(const void *)) { 33068d75effSDimitry Andric if (!malloc_hook || !free_hook) return 0; 33168d75effSDimitry Andric for (int i = 0; i < kMaxMallocFreeHooks; i++) { 33268d75effSDimitry Andric if (MFHooks[i].malloc_hook == nullptr) { 33368d75effSDimitry Andric MFHooks[i].malloc_hook = malloc_hook; 33468d75effSDimitry Andric MFHooks[i].free_hook = free_hook; 33568d75effSDimitry Andric return i + 1; 33668d75effSDimitry Andric } 33768d75effSDimitry Andric } 33868d75effSDimitry Andric return 0; 33968d75effSDimitry Andric } 34068d75effSDimitry Andric 341fe6060f1SDimitry Andric void internal_sleep(unsigned seconds) { 342fe6060f1SDimitry Andric internal_usleep((u64)seconds * 1000 * 1000); 343fe6060f1SDimitry Andric } 344fe6060f1SDimitry Andric void SleepForSeconds(unsigned seconds) { 345fe6060f1SDimitry Andric internal_usleep((u64)seconds * 1000 * 1000); 346fe6060f1SDimitry Andric } 347fe6060f1SDimitry Andric void SleepForMillis(unsigned millis) { internal_usleep((u64)millis * 1000); } 348fe6060f1SDimitry Andric 34968d75effSDimitry Andric } // namespace __sanitizer 35068d75effSDimitry Andric 35168d75effSDimitry Andric using namespace __sanitizer; 35268d75effSDimitry Andric 35368d75effSDimitry Andric extern "C" { 35468d75effSDimitry Andric SANITIZER_INTERFACE_WEAK_DEF(void, __sanitizer_report_error_summary, 35568d75effSDimitry Andric const char *error_summary) { 35668d75effSDimitry Andric Printf("%s\n", error_summary); 35768d75effSDimitry Andric } 35868d75effSDimitry Andric 35968d75effSDimitry Andric SANITIZER_INTERFACE_ATTRIBUTE 36068d75effSDimitry Andric int __sanitizer_acquire_crash_state() { 36168d75effSDimitry Andric static atomic_uint8_t in_crash_state = {}; 36268d75effSDimitry Andric return !atomic_exchange(&in_crash_state, 1, memory_order_relaxed); 36368d75effSDimitry Andric } 36468d75effSDimitry Andric 36568d75effSDimitry Andric SANITIZER_INTERFACE_ATTRIBUTE 36668d75effSDimitry Andric int __sanitizer_install_malloc_and_free_hooks(void (*malloc_hook)(const void *, 36768d75effSDimitry Andric uptr), 36868d75effSDimitry Andric void (*free_hook)(const void *)) { 36968d75effSDimitry Andric return InstallMallocFreeHooks(malloc_hook, free_hook); 37068d75effSDimitry Andric } 37168d75effSDimitry Andric } // extern "C" 372