xref: /freebsd-src/contrib/llvm-project/compiler-rt/lib/sanitizer_common/sanitizer_common.cpp (revision 5ffd83dbcc34f10e07f6d3e968ae6365869615f4)
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;
4068d75effSDimitry Andric   if (SANITIZER_RTEMS || raw_report || recursion_count) {
4168d75effSDimitry Andric     // If we are on RTEMS or raw report is requested or we went into recursion,
4268d75effSDimitry Andric     // just die.  The Report() and CHECK calls below may call mmap recursively
4368d75effSDimitry Andric     // and fail.
4468d75effSDimitry Andric     RawWrite("ERROR: Failed to mmap\n");
4568d75effSDimitry Andric     Die();
4668d75effSDimitry Andric   }
4768d75effSDimitry Andric   recursion_count++;
4868d75effSDimitry Andric   Report("ERROR: %s failed to "
4968d75effSDimitry Andric          "%s 0x%zx (%zd) bytes of %s (error code: %d)\n",
5068d75effSDimitry Andric          SanitizerToolName, mmap_type, size, size, mem_type, err);
5168d75effSDimitry Andric #if !SANITIZER_GO
5268d75effSDimitry Andric   DumpProcessMap();
5368d75effSDimitry Andric #endif
5468d75effSDimitry Andric   UNREACHABLE("unable to mmap");
5568d75effSDimitry Andric }
5668d75effSDimitry Andric 
5768d75effSDimitry Andric typedef bool UptrComparisonFunction(const uptr &a, const uptr &b);
5868d75effSDimitry Andric typedef bool U32ComparisonFunction(const u32 &a, const u32 &b);
5968d75effSDimitry Andric 
6068d75effSDimitry Andric const char *StripPathPrefix(const char *filepath,
6168d75effSDimitry Andric                             const char *strip_path_prefix) {
6268d75effSDimitry Andric   if (!filepath) return nullptr;
6368d75effSDimitry Andric   if (!strip_path_prefix) return filepath;
6468d75effSDimitry Andric   const char *res = filepath;
6568d75effSDimitry Andric   if (const char *pos = internal_strstr(filepath, strip_path_prefix))
6668d75effSDimitry Andric     res = pos + internal_strlen(strip_path_prefix);
6768d75effSDimitry Andric   if (res[0] == '.' && res[1] == '/')
6868d75effSDimitry Andric     res += 2;
6968d75effSDimitry Andric   return res;
7068d75effSDimitry Andric }
7168d75effSDimitry Andric 
7268d75effSDimitry Andric const char *StripModuleName(const char *module) {
7368d75effSDimitry Andric   if (!module)
7468d75effSDimitry Andric     return nullptr;
7568d75effSDimitry Andric   if (SANITIZER_WINDOWS) {
7668d75effSDimitry Andric     // On Windows, both slash and backslash are possible.
7768d75effSDimitry Andric     // Pick the one that goes last.
7868d75effSDimitry Andric     if (const char *bslash_pos = internal_strrchr(module, '\\'))
7968d75effSDimitry Andric       return StripModuleName(bslash_pos + 1);
8068d75effSDimitry Andric   }
8168d75effSDimitry Andric   if (const char *slash_pos = internal_strrchr(module, '/')) {
8268d75effSDimitry Andric     return slash_pos + 1;
8368d75effSDimitry Andric   }
8468d75effSDimitry Andric   return module;
8568d75effSDimitry Andric }
8668d75effSDimitry Andric 
8768d75effSDimitry Andric void ReportErrorSummary(const char *error_message, const char *alt_tool_name) {
8868d75effSDimitry Andric   if (!common_flags()->print_summary)
8968d75effSDimitry Andric     return;
9068d75effSDimitry Andric   InternalScopedString buff(kMaxSummaryLength);
9168d75effSDimitry Andric   buff.append("SUMMARY: %s: %s",
9268d75effSDimitry Andric               alt_tool_name ? alt_tool_name : SanitizerToolName, error_message);
9368d75effSDimitry Andric   __sanitizer_report_error_summary(buff.data());
9468d75effSDimitry Andric }
9568d75effSDimitry Andric 
9668d75effSDimitry Andric // Removes the ANSI escape sequences from the input string (in-place).
9768d75effSDimitry Andric void RemoveANSIEscapeSequencesFromString(char *str) {
9868d75effSDimitry Andric   if (!str)
9968d75effSDimitry Andric     return;
10068d75effSDimitry Andric 
10168d75effSDimitry Andric   // We are going to remove the escape sequences in place.
10268d75effSDimitry Andric   char *s = str;
10368d75effSDimitry Andric   char *z = str;
10468d75effSDimitry Andric   while (*s != '\0') {
10568d75effSDimitry Andric     CHECK_GE(s, z);
10668d75effSDimitry Andric     // Skip over ANSI escape sequences with pointer 's'.
10768d75effSDimitry Andric     if (*s == '\033' && *(s + 1) == '[') {
10868d75effSDimitry Andric       s = internal_strchrnul(s, 'm');
10968d75effSDimitry Andric       if (*s == '\0') {
11068d75effSDimitry Andric         break;
11168d75effSDimitry Andric       }
11268d75effSDimitry Andric       s++;
11368d75effSDimitry Andric       continue;
11468d75effSDimitry Andric     }
11568d75effSDimitry Andric     // 's' now points at a character we want to keep. Copy over the buffer
11668d75effSDimitry Andric     // content if the escape sequence has been perviously skipped andadvance
11768d75effSDimitry Andric     // both pointers.
11868d75effSDimitry Andric     if (s != z)
11968d75effSDimitry Andric       *z = *s;
12068d75effSDimitry Andric 
12168d75effSDimitry Andric     // If we have not seen an escape sequence, just advance both pointers.
12268d75effSDimitry Andric     z++;
12368d75effSDimitry Andric     s++;
12468d75effSDimitry Andric   }
12568d75effSDimitry Andric 
12668d75effSDimitry Andric   // Null terminate the string.
12768d75effSDimitry Andric   *z = '\0';
12868d75effSDimitry Andric }
12968d75effSDimitry Andric 
13068d75effSDimitry Andric void LoadedModule::set(const char *module_name, uptr base_address) {
13168d75effSDimitry Andric   clear();
13268d75effSDimitry Andric   full_name_ = internal_strdup(module_name);
13368d75effSDimitry Andric   base_address_ = base_address;
13468d75effSDimitry Andric }
13568d75effSDimitry Andric 
13668d75effSDimitry Andric void LoadedModule::set(const char *module_name, uptr base_address,
13768d75effSDimitry Andric                        ModuleArch arch, u8 uuid[kModuleUUIDSize],
13868d75effSDimitry Andric                        bool instrumented) {
13968d75effSDimitry Andric   set(module_name, base_address);
14068d75effSDimitry Andric   arch_ = arch;
14168d75effSDimitry Andric   internal_memcpy(uuid_, uuid, sizeof(uuid_));
14268d75effSDimitry Andric   instrumented_ = instrumented;
14368d75effSDimitry Andric }
14468d75effSDimitry Andric 
14568d75effSDimitry Andric void LoadedModule::clear() {
14668d75effSDimitry Andric   InternalFree(full_name_);
14768d75effSDimitry Andric   base_address_ = 0;
14868d75effSDimitry Andric   max_executable_address_ = 0;
14968d75effSDimitry Andric   full_name_ = nullptr;
15068d75effSDimitry Andric   arch_ = kModuleArchUnknown;
15168d75effSDimitry Andric   internal_memset(uuid_, 0, kModuleUUIDSize);
15268d75effSDimitry Andric   instrumented_ = false;
15368d75effSDimitry Andric   while (!ranges_.empty()) {
15468d75effSDimitry Andric     AddressRange *r = ranges_.front();
15568d75effSDimitry Andric     ranges_.pop_front();
15668d75effSDimitry Andric     InternalFree(r);
15768d75effSDimitry Andric   }
15868d75effSDimitry Andric }
15968d75effSDimitry Andric 
16068d75effSDimitry Andric void LoadedModule::addAddressRange(uptr beg, uptr end, bool executable,
16168d75effSDimitry Andric                                    bool writable, const char *name) {
16268d75effSDimitry Andric   void *mem = InternalAlloc(sizeof(AddressRange));
16368d75effSDimitry Andric   AddressRange *r =
16468d75effSDimitry Andric       new(mem) AddressRange(beg, end, executable, writable, name);
16568d75effSDimitry Andric   ranges_.push_back(r);
16668d75effSDimitry Andric   if (executable && end > max_executable_address_)
16768d75effSDimitry Andric     max_executable_address_ = end;
16868d75effSDimitry Andric }
16968d75effSDimitry Andric 
17068d75effSDimitry Andric bool LoadedModule::containsAddress(uptr address) const {
17168d75effSDimitry Andric   for (const AddressRange &r : ranges()) {
17268d75effSDimitry Andric     if (r.beg <= address && address < r.end)
17368d75effSDimitry Andric       return true;
17468d75effSDimitry Andric   }
17568d75effSDimitry Andric   return false;
17668d75effSDimitry Andric }
17768d75effSDimitry Andric 
17868d75effSDimitry Andric static atomic_uintptr_t g_total_mmaped;
17968d75effSDimitry Andric 
18068d75effSDimitry Andric void IncreaseTotalMmap(uptr size) {
18168d75effSDimitry Andric   if (!common_flags()->mmap_limit_mb) return;
18268d75effSDimitry Andric   uptr total_mmaped =
18368d75effSDimitry Andric       atomic_fetch_add(&g_total_mmaped, size, memory_order_relaxed) + size;
18468d75effSDimitry Andric   // Since for now mmap_limit_mb is not a user-facing flag, just kill
18568d75effSDimitry Andric   // a program. Use RAW_CHECK to avoid extra mmaps in reporting.
18668d75effSDimitry Andric   RAW_CHECK((total_mmaped >> 20) < common_flags()->mmap_limit_mb);
18768d75effSDimitry Andric }
18868d75effSDimitry Andric 
18968d75effSDimitry Andric void DecreaseTotalMmap(uptr size) {
19068d75effSDimitry Andric   if (!common_flags()->mmap_limit_mb) return;
19168d75effSDimitry Andric   atomic_fetch_sub(&g_total_mmaped, size, memory_order_relaxed);
19268d75effSDimitry Andric }
19368d75effSDimitry Andric 
19468d75effSDimitry Andric bool TemplateMatch(const char *templ, const char *str) {
19568d75effSDimitry Andric   if ((!str) || str[0] == 0)
19668d75effSDimitry Andric     return false;
19768d75effSDimitry Andric   bool start = false;
19868d75effSDimitry Andric   if (templ && templ[0] == '^') {
19968d75effSDimitry Andric     start = true;
20068d75effSDimitry Andric     templ++;
20168d75effSDimitry Andric   }
20268d75effSDimitry Andric   bool asterisk = false;
20368d75effSDimitry Andric   while (templ && templ[0]) {
20468d75effSDimitry Andric     if (templ[0] == '*') {
20568d75effSDimitry Andric       templ++;
20668d75effSDimitry Andric       start = false;
20768d75effSDimitry Andric       asterisk = true;
20868d75effSDimitry Andric       continue;
20968d75effSDimitry Andric     }
21068d75effSDimitry Andric     if (templ[0] == '$')
21168d75effSDimitry Andric       return str[0] == 0 || asterisk;
21268d75effSDimitry Andric     if (str[0] == 0)
21368d75effSDimitry Andric       return false;
21468d75effSDimitry Andric     char *tpos = (char*)internal_strchr(templ, '*');
21568d75effSDimitry Andric     char *tpos1 = (char*)internal_strchr(templ, '$');
21668d75effSDimitry Andric     if ((!tpos) || (tpos1 && tpos1 < tpos))
21768d75effSDimitry Andric       tpos = tpos1;
21868d75effSDimitry Andric     if (tpos)
21968d75effSDimitry Andric       tpos[0] = 0;
22068d75effSDimitry Andric     const char *str0 = str;
22168d75effSDimitry Andric     const char *spos = internal_strstr(str, templ);
22268d75effSDimitry Andric     str = spos + internal_strlen(templ);
22368d75effSDimitry Andric     templ = tpos;
22468d75effSDimitry Andric     if (tpos)
22568d75effSDimitry Andric       tpos[0] = tpos == tpos1 ? '$' : '*';
22668d75effSDimitry Andric     if (!spos)
22768d75effSDimitry Andric       return false;
22868d75effSDimitry Andric     if (start && spos != str0)
22968d75effSDimitry Andric       return false;
23068d75effSDimitry Andric     start = false;
23168d75effSDimitry Andric     asterisk = false;
23268d75effSDimitry Andric   }
23368d75effSDimitry Andric   return true;
23468d75effSDimitry Andric }
23568d75effSDimitry Andric 
23668d75effSDimitry Andric static char binary_name_cache_str[kMaxPathLength];
23768d75effSDimitry Andric static char process_name_cache_str[kMaxPathLength];
23868d75effSDimitry Andric 
23968d75effSDimitry Andric const char *GetProcessName() {
24068d75effSDimitry Andric   return process_name_cache_str;
24168d75effSDimitry Andric }
24268d75effSDimitry Andric 
24368d75effSDimitry Andric static uptr ReadProcessName(/*out*/ char *buf, uptr buf_len) {
24468d75effSDimitry Andric   ReadLongProcessName(buf, buf_len);
24568d75effSDimitry Andric   char *s = const_cast<char *>(StripModuleName(buf));
24668d75effSDimitry Andric   uptr len = internal_strlen(s);
24768d75effSDimitry Andric   if (s != buf) {
24868d75effSDimitry Andric     internal_memmove(buf, s, len);
24968d75effSDimitry Andric     buf[len] = '\0';
25068d75effSDimitry Andric   }
25168d75effSDimitry Andric   return len;
25268d75effSDimitry Andric }
25368d75effSDimitry Andric 
25468d75effSDimitry Andric void UpdateProcessName() {
25568d75effSDimitry Andric   ReadProcessName(process_name_cache_str, sizeof(process_name_cache_str));
25668d75effSDimitry Andric }
25768d75effSDimitry Andric 
25868d75effSDimitry Andric // Call once to make sure that binary_name_cache_str is initialized
25968d75effSDimitry Andric void CacheBinaryName() {
26068d75effSDimitry Andric   if (binary_name_cache_str[0] != '\0')
26168d75effSDimitry Andric     return;
26268d75effSDimitry Andric   ReadBinaryName(binary_name_cache_str, sizeof(binary_name_cache_str));
26368d75effSDimitry Andric   ReadProcessName(process_name_cache_str, sizeof(process_name_cache_str));
26468d75effSDimitry Andric }
26568d75effSDimitry Andric 
26668d75effSDimitry Andric uptr ReadBinaryNameCached(/*out*/char *buf, uptr buf_len) {
26768d75effSDimitry Andric   CacheBinaryName();
26868d75effSDimitry Andric   uptr name_len = internal_strlen(binary_name_cache_str);
26968d75effSDimitry Andric   name_len = (name_len < buf_len - 1) ? name_len : buf_len - 1;
27068d75effSDimitry Andric   if (buf_len == 0)
27168d75effSDimitry Andric     return 0;
27268d75effSDimitry Andric   internal_memcpy(buf, binary_name_cache_str, name_len);
27368d75effSDimitry Andric   buf[name_len] = '\0';
27468d75effSDimitry Andric   return name_len;
27568d75effSDimitry Andric }
27668d75effSDimitry Andric 
277*5ffd83dbSDimitry Andric #if !SANITIZER_GO
27868d75effSDimitry Andric void PrintCmdline() {
27968d75effSDimitry Andric   char **argv = GetArgv();
28068d75effSDimitry Andric   if (!argv) return;
28168d75effSDimitry Andric   Printf("\nCommand: ");
28268d75effSDimitry Andric   for (uptr i = 0; argv[i]; ++i)
28368d75effSDimitry Andric     Printf("%s ", argv[i]);
28468d75effSDimitry Andric   Printf("\n\n");
28568d75effSDimitry Andric }
286*5ffd83dbSDimitry Andric #endif
28768d75effSDimitry Andric 
28868d75effSDimitry Andric // Malloc hooks.
28968d75effSDimitry Andric static const int kMaxMallocFreeHooks = 5;
29068d75effSDimitry Andric struct MallocFreeHook {
29168d75effSDimitry Andric   void (*malloc_hook)(const void *, uptr);
29268d75effSDimitry Andric   void (*free_hook)(const void *);
29368d75effSDimitry Andric };
29468d75effSDimitry Andric 
29568d75effSDimitry Andric static MallocFreeHook MFHooks[kMaxMallocFreeHooks];
29668d75effSDimitry Andric 
29768d75effSDimitry Andric void RunMallocHooks(const void *ptr, uptr size) {
29868d75effSDimitry Andric   for (int i = 0; i < kMaxMallocFreeHooks; i++) {
29968d75effSDimitry Andric     auto hook = MFHooks[i].malloc_hook;
30068d75effSDimitry Andric     if (!hook) return;
30168d75effSDimitry Andric     hook(ptr, size);
30268d75effSDimitry Andric   }
30368d75effSDimitry Andric }
30468d75effSDimitry Andric 
30568d75effSDimitry Andric void RunFreeHooks(const void *ptr) {
30668d75effSDimitry Andric   for (int i = 0; i < kMaxMallocFreeHooks; i++) {
30768d75effSDimitry Andric     auto hook = MFHooks[i].free_hook;
30868d75effSDimitry Andric     if (!hook) return;
30968d75effSDimitry Andric     hook(ptr);
31068d75effSDimitry Andric   }
31168d75effSDimitry Andric }
31268d75effSDimitry Andric 
31368d75effSDimitry Andric static int InstallMallocFreeHooks(void (*malloc_hook)(const void *, uptr),
31468d75effSDimitry Andric                                   void (*free_hook)(const void *)) {
31568d75effSDimitry Andric   if (!malloc_hook || !free_hook) return 0;
31668d75effSDimitry Andric   for (int i = 0; i < kMaxMallocFreeHooks; i++) {
31768d75effSDimitry Andric     if (MFHooks[i].malloc_hook == nullptr) {
31868d75effSDimitry Andric       MFHooks[i].malloc_hook = malloc_hook;
31968d75effSDimitry Andric       MFHooks[i].free_hook = free_hook;
32068d75effSDimitry Andric       return i + 1;
32168d75effSDimitry Andric     }
32268d75effSDimitry Andric   }
32368d75effSDimitry Andric   return 0;
32468d75effSDimitry Andric }
32568d75effSDimitry Andric 
32668d75effSDimitry Andric } // namespace __sanitizer
32768d75effSDimitry Andric 
32868d75effSDimitry Andric using namespace __sanitizer;
32968d75effSDimitry Andric 
33068d75effSDimitry Andric extern "C" {
33168d75effSDimitry Andric SANITIZER_INTERFACE_WEAK_DEF(void, __sanitizer_report_error_summary,
33268d75effSDimitry Andric                              const char *error_summary) {
33368d75effSDimitry Andric   Printf("%s\n", error_summary);
33468d75effSDimitry Andric }
33568d75effSDimitry Andric 
33668d75effSDimitry Andric SANITIZER_INTERFACE_ATTRIBUTE
33768d75effSDimitry Andric int __sanitizer_acquire_crash_state() {
33868d75effSDimitry Andric   static atomic_uint8_t in_crash_state = {};
33968d75effSDimitry Andric   return !atomic_exchange(&in_crash_state, 1, memory_order_relaxed);
34068d75effSDimitry Andric }
34168d75effSDimitry Andric 
34268d75effSDimitry Andric SANITIZER_INTERFACE_ATTRIBUTE
34368d75effSDimitry Andric int __sanitizer_install_malloc_and_free_hooks(void (*malloc_hook)(const void *,
34468d75effSDimitry Andric                                                                   uptr),
34568d75effSDimitry Andric                                               void (*free_hook)(const void *)) {
34668d75effSDimitry Andric   return InstallMallocFreeHooks(malloc_hook, free_hook);
34768d75effSDimitry Andric }
34868d75effSDimitry Andric } // extern "C"
349