xref: /freebsd-src/contrib/llvm-project/compiler-rt/lib/sanitizer_common/sanitizer_common.cpp (revision 81ad626541db97eb356e2c1d4a20eb2a26a766ab)
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"
14*81ad6265SDimitry Andric 
1568d75effSDimitry Andric #include "sanitizer_allocator_interface.h"
1668d75effSDimitry Andric #include "sanitizer_allocator_internal.h"
1768d75effSDimitry Andric #include "sanitizer_atomic.h"
1868d75effSDimitry Andric #include "sanitizer_flags.h"
19*81ad6265SDimitry Andric #include "sanitizer_interface_internal.h"
2068d75effSDimitry Andric #include "sanitizer_libc.h"
2168d75effSDimitry Andric #include "sanitizer_placement_new.h"
2268d75effSDimitry Andric 
2368d75effSDimitry Andric namespace __sanitizer {
2468d75effSDimitry Andric 
2568d75effSDimitry Andric const char *SanitizerToolName = "SanitizerTool";
2668d75effSDimitry Andric 
2768d75effSDimitry Andric atomic_uint32_t current_verbosity;
2868d75effSDimitry Andric uptr PageSizeCached;
2968d75effSDimitry Andric u32 NumberOfCPUsCached;
3068d75effSDimitry Andric 
3168d75effSDimitry Andric // PID of the tracer task in StopTheWorld. It shares the address space with the
3268d75effSDimitry Andric // main process, but has a different PID and thus requires special handling.
3368d75effSDimitry Andric uptr stoptheworld_tracer_pid = 0;
3468d75effSDimitry Andric // Cached pid of parent process - if the parent process dies, we want to keep
3568d75effSDimitry Andric // writing to the same log file.
3668d75effSDimitry Andric uptr stoptheworld_tracer_ppid = 0;
3768d75effSDimitry Andric 
3868d75effSDimitry Andric void NORETURN ReportMmapFailureAndDie(uptr size, const char *mem_type,
3968d75effSDimitry Andric                                       const char *mmap_type, error_t err,
4068d75effSDimitry Andric                                       bool raw_report) {
4168d75effSDimitry Andric   static int recursion_count;
42fe6060f1SDimitry Andric   if (raw_report || recursion_count) {
43fe6060f1SDimitry Andric     // If raw report is requested or we went into recursion just die.  The
44fe6060f1SDimitry Andric     // Report() and CHECK calls below may call mmap recursively and fail.
4568d75effSDimitry Andric     RawWrite("ERROR: Failed to mmap\n");
4668d75effSDimitry Andric     Die();
4768d75effSDimitry Andric   }
4868d75effSDimitry Andric   recursion_count++;
49*81ad6265SDimitry Andric   if (ErrorIsOOM(err)) {
50*81ad6265SDimitry Andric     ERROR_OOM("failed to %s 0x%zx (%zd) bytes of %s (error code: %d)\n",
51*81ad6265SDimitry Andric               mmap_type, size, size, mem_type, err);
52*81ad6265SDimitry Andric   } else {
53*81ad6265SDimitry Andric     Report(
54*81ad6265SDimitry Andric         "ERROR: %s failed to "
5568d75effSDimitry Andric         "%s 0x%zx (%zd) bytes of %s (error code: %d)\n",
5668d75effSDimitry Andric         SanitizerToolName, mmap_type, size, size, mem_type, err);
57*81ad6265SDimitry Andric   }
5868d75effSDimitry Andric #if !SANITIZER_GO
5968d75effSDimitry Andric   DumpProcessMap();
6068d75effSDimitry Andric #endif
6168d75effSDimitry Andric   UNREACHABLE("unable to mmap");
6268d75effSDimitry Andric }
6368d75effSDimitry Andric 
6468d75effSDimitry Andric typedef bool UptrComparisonFunction(const uptr &a, const uptr &b);
6568d75effSDimitry Andric typedef bool U32ComparisonFunction(const u32 &a, const u32 &b);
6668d75effSDimitry Andric 
6768d75effSDimitry Andric const char *StripPathPrefix(const char *filepath,
6868d75effSDimitry Andric                             const char *strip_path_prefix) {
6968d75effSDimitry Andric   if (!filepath) return nullptr;
7068d75effSDimitry Andric   if (!strip_path_prefix) return filepath;
7168d75effSDimitry Andric   const char *res = filepath;
7268d75effSDimitry Andric   if (const char *pos = internal_strstr(filepath, strip_path_prefix))
7368d75effSDimitry Andric     res = pos + internal_strlen(strip_path_prefix);
7468d75effSDimitry Andric   if (res[0] == '.' && res[1] == '/')
7568d75effSDimitry Andric     res += 2;
7668d75effSDimitry Andric   return res;
7768d75effSDimitry Andric }
7868d75effSDimitry Andric 
7968d75effSDimitry Andric const char *StripModuleName(const char *module) {
8068d75effSDimitry Andric   if (!module)
8168d75effSDimitry Andric     return nullptr;
8268d75effSDimitry Andric   if (SANITIZER_WINDOWS) {
8368d75effSDimitry Andric     // On Windows, both slash and backslash are possible.
8468d75effSDimitry Andric     // Pick the one that goes last.
8568d75effSDimitry Andric     if (const char *bslash_pos = internal_strrchr(module, '\\'))
8668d75effSDimitry Andric       return StripModuleName(bslash_pos + 1);
8768d75effSDimitry Andric   }
8868d75effSDimitry Andric   if (const char *slash_pos = internal_strrchr(module, '/')) {
8968d75effSDimitry Andric     return slash_pos + 1;
9068d75effSDimitry Andric   }
9168d75effSDimitry Andric   return module;
9268d75effSDimitry Andric }
9368d75effSDimitry Andric 
9468d75effSDimitry Andric void ReportErrorSummary(const char *error_message, const char *alt_tool_name) {
9568d75effSDimitry Andric   if (!common_flags()->print_summary)
9668d75effSDimitry Andric     return;
97fe6060f1SDimitry Andric   InternalScopedString buff;
9868d75effSDimitry Andric   buff.append("SUMMARY: %s: %s",
9968d75effSDimitry Andric               alt_tool_name ? alt_tool_name : SanitizerToolName, error_message);
10068d75effSDimitry Andric   __sanitizer_report_error_summary(buff.data());
10168d75effSDimitry Andric }
10268d75effSDimitry Andric 
10368d75effSDimitry Andric // Removes the ANSI escape sequences from the input string (in-place).
10468d75effSDimitry Andric void RemoveANSIEscapeSequencesFromString(char *str) {
10568d75effSDimitry Andric   if (!str)
10668d75effSDimitry Andric     return;
10768d75effSDimitry Andric 
10868d75effSDimitry Andric   // We are going to remove the escape sequences in place.
10968d75effSDimitry Andric   char *s = str;
11068d75effSDimitry Andric   char *z = str;
11168d75effSDimitry Andric   while (*s != '\0') {
11268d75effSDimitry Andric     CHECK_GE(s, z);
11368d75effSDimitry Andric     // Skip over ANSI escape sequences with pointer 's'.
11468d75effSDimitry Andric     if (*s == '\033' && *(s + 1) == '[') {
11568d75effSDimitry Andric       s = internal_strchrnul(s, 'm');
11668d75effSDimitry Andric       if (*s == '\0') {
11768d75effSDimitry Andric         break;
11868d75effSDimitry Andric       }
11968d75effSDimitry Andric       s++;
12068d75effSDimitry Andric       continue;
12168d75effSDimitry Andric     }
12268d75effSDimitry Andric     // 's' now points at a character we want to keep. Copy over the buffer
12368d75effSDimitry Andric     // content if the escape sequence has been perviously skipped andadvance
12468d75effSDimitry Andric     // both pointers.
12568d75effSDimitry Andric     if (s != z)
12668d75effSDimitry Andric       *z = *s;
12768d75effSDimitry Andric 
12868d75effSDimitry Andric     // If we have not seen an escape sequence, just advance both pointers.
12968d75effSDimitry Andric     z++;
13068d75effSDimitry Andric     s++;
13168d75effSDimitry Andric   }
13268d75effSDimitry Andric 
13368d75effSDimitry Andric   // Null terminate the string.
13468d75effSDimitry Andric   *z = '\0';
13568d75effSDimitry Andric }
13668d75effSDimitry Andric 
13768d75effSDimitry Andric void LoadedModule::set(const char *module_name, uptr base_address) {
13868d75effSDimitry Andric   clear();
13968d75effSDimitry Andric   full_name_ = internal_strdup(module_name);
14068d75effSDimitry Andric   base_address_ = base_address;
14168d75effSDimitry Andric }
14268d75effSDimitry Andric 
14368d75effSDimitry Andric void LoadedModule::set(const char *module_name, uptr base_address,
14468d75effSDimitry Andric                        ModuleArch arch, u8 uuid[kModuleUUIDSize],
14568d75effSDimitry Andric                        bool instrumented) {
14668d75effSDimitry Andric   set(module_name, base_address);
14768d75effSDimitry Andric   arch_ = arch;
14868d75effSDimitry Andric   internal_memcpy(uuid_, uuid, sizeof(uuid_));
1490eae32dcSDimitry Andric   uuid_size_ = kModuleUUIDSize;
15068d75effSDimitry Andric   instrumented_ = instrumented;
15168d75effSDimitry Andric }
15268d75effSDimitry Andric 
1530eae32dcSDimitry Andric void LoadedModule::setUuid(const char *uuid, uptr size) {
1540eae32dcSDimitry Andric   if (size > kModuleUUIDSize)
1550eae32dcSDimitry Andric     size = kModuleUUIDSize;
1560eae32dcSDimitry Andric   internal_memcpy(uuid_, uuid, size);
1570eae32dcSDimitry Andric   uuid_size_ = size;
1580eae32dcSDimitry Andric }
1590eae32dcSDimitry Andric 
16068d75effSDimitry Andric void LoadedModule::clear() {
16168d75effSDimitry Andric   InternalFree(full_name_);
16268d75effSDimitry Andric   base_address_ = 0;
163*81ad6265SDimitry Andric   max_address_ = 0;
16468d75effSDimitry Andric   full_name_ = nullptr;
16568d75effSDimitry Andric   arch_ = kModuleArchUnknown;
16668d75effSDimitry Andric   internal_memset(uuid_, 0, kModuleUUIDSize);
16768d75effSDimitry Andric   instrumented_ = false;
16868d75effSDimitry Andric   while (!ranges_.empty()) {
16968d75effSDimitry Andric     AddressRange *r = ranges_.front();
17068d75effSDimitry Andric     ranges_.pop_front();
17168d75effSDimitry Andric     InternalFree(r);
17268d75effSDimitry Andric   }
17368d75effSDimitry Andric }
17468d75effSDimitry Andric 
17568d75effSDimitry Andric void LoadedModule::addAddressRange(uptr beg, uptr end, bool executable,
17668d75effSDimitry Andric                                    bool writable, const char *name) {
17768d75effSDimitry Andric   void *mem = InternalAlloc(sizeof(AddressRange));
17868d75effSDimitry Andric   AddressRange *r =
17968d75effSDimitry Andric       new(mem) AddressRange(beg, end, executable, writable, name);
18068d75effSDimitry Andric   ranges_.push_back(r);
181*81ad6265SDimitry Andric   max_address_ = Max(max_address_, end);
18268d75effSDimitry Andric }
18368d75effSDimitry Andric 
18468d75effSDimitry Andric bool LoadedModule::containsAddress(uptr address) const {
18568d75effSDimitry Andric   for (const AddressRange &r : ranges()) {
18668d75effSDimitry Andric     if (r.beg <= address && address < r.end)
18768d75effSDimitry Andric       return true;
18868d75effSDimitry Andric   }
18968d75effSDimitry Andric   return false;
19068d75effSDimitry Andric }
19168d75effSDimitry Andric 
19268d75effSDimitry Andric static atomic_uintptr_t g_total_mmaped;
19368d75effSDimitry Andric 
19468d75effSDimitry Andric void IncreaseTotalMmap(uptr size) {
19568d75effSDimitry Andric   if (!common_flags()->mmap_limit_mb) return;
19668d75effSDimitry Andric   uptr total_mmaped =
19768d75effSDimitry Andric       atomic_fetch_add(&g_total_mmaped, size, memory_order_relaxed) + size;
19868d75effSDimitry Andric   // Since for now mmap_limit_mb is not a user-facing flag, just kill
19968d75effSDimitry Andric   // a program. Use RAW_CHECK to avoid extra mmaps in reporting.
20068d75effSDimitry Andric   RAW_CHECK((total_mmaped >> 20) < common_flags()->mmap_limit_mb);
20168d75effSDimitry Andric }
20268d75effSDimitry Andric 
20368d75effSDimitry Andric void DecreaseTotalMmap(uptr size) {
20468d75effSDimitry Andric   if (!common_flags()->mmap_limit_mb) return;
20568d75effSDimitry Andric   atomic_fetch_sub(&g_total_mmaped, size, memory_order_relaxed);
20668d75effSDimitry Andric }
20768d75effSDimitry Andric 
20868d75effSDimitry Andric bool TemplateMatch(const char *templ, const char *str) {
20968d75effSDimitry Andric   if ((!str) || str[0] == 0)
21068d75effSDimitry Andric     return false;
21168d75effSDimitry Andric   bool start = false;
21268d75effSDimitry Andric   if (templ && templ[0] == '^') {
21368d75effSDimitry Andric     start = true;
21468d75effSDimitry Andric     templ++;
21568d75effSDimitry Andric   }
21668d75effSDimitry Andric   bool asterisk = false;
21768d75effSDimitry Andric   while (templ && templ[0]) {
21868d75effSDimitry Andric     if (templ[0] == '*') {
21968d75effSDimitry Andric       templ++;
22068d75effSDimitry Andric       start = false;
22168d75effSDimitry Andric       asterisk = true;
22268d75effSDimitry Andric       continue;
22368d75effSDimitry Andric     }
22468d75effSDimitry Andric     if (templ[0] == '$')
22568d75effSDimitry Andric       return str[0] == 0 || asterisk;
22668d75effSDimitry Andric     if (str[0] == 0)
22768d75effSDimitry Andric       return false;
22868d75effSDimitry Andric     char *tpos = (char*)internal_strchr(templ, '*');
22968d75effSDimitry Andric     char *tpos1 = (char*)internal_strchr(templ, '$');
23068d75effSDimitry Andric     if ((!tpos) || (tpos1 && tpos1 < tpos))
23168d75effSDimitry Andric       tpos = tpos1;
23268d75effSDimitry Andric     if (tpos)
23368d75effSDimitry Andric       tpos[0] = 0;
23468d75effSDimitry Andric     const char *str0 = str;
23568d75effSDimitry Andric     const char *spos = internal_strstr(str, templ);
23668d75effSDimitry Andric     str = spos + internal_strlen(templ);
23768d75effSDimitry Andric     templ = tpos;
23868d75effSDimitry Andric     if (tpos)
23968d75effSDimitry Andric       tpos[0] = tpos == tpos1 ? '$' : '*';
24068d75effSDimitry Andric     if (!spos)
24168d75effSDimitry Andric       return false;
24268d75effSDimitry Andric     if (start && spos != str0)
24368d75effSDimitry Andric       return false;
24468d75effSDimitry Andric     start = false;
24568d75effSDimitry Andric     asterisk = false;
24668d75effSDimitry Andric   }
24768d75effSDimitry Andric   return true;
24868d75effSDimitry Andric }
24968d75effSDimitry Andric 
25068d75effSDimitry Andric static char binary_name_cache_str[kMaxPathLength];
25168d75effSDimitry Andric static char process_name_cache_str[kMaxPathLength];
25268d75effSDimitry Andric 
25368d75effSDimitry Andric const char *GetProcessName() {
25468d75effSDimitry Andric   return process_name_cache_str;
25568d75effSDimitry Andric }
25668d75effSDimitry Andric 
25768d75effSDimitry Andric static uptr ReadProcessName(/*out*/ char *buf, uptr buf_len) {
25868d75effSDimitry Andric   ReadLongProcessName(buf, buf_len);
25968d75effSDimitry Andric   char *s = const_cast<char *>(StripModuleName(buf));
26068d75effSDimitry Andric   uptr len = internal_strlen(s);
26168d75effSDimitry Andric   if (s != buf) {
26268d75effSDimitry Andric     internal_memmove(buf, s, len);
26368d75effSDimitry Andric     buf[len] = '\0';
26468d75effSDimitry Andric   }
26568d75effSDimitry Andric   return len;
26668d75effSDimitry Andric }
26768d75effSDimitry Andric 
26868d75effSDimitry Andric void UpdateProcessName() {
26968d75effSDimitry Andric   ReadProcessName(process_name_cache_str, sizeof(process_name_cache_str));
27068d75effSDimitry Andric }
27168d75effSDimitry Andric 
27268d75effSDimitry Andric // Call once to make sure that binary_name_cache_str is initialized
27368d75effSDimitry Andric void CacheBinaryName() {
27468d75effSDimitry Andric   if (binary_name_cache_str[0] != '\0')
27568d75effSDimitry Andric     return;
27668d75effSDimitry Andric   ReadBinaryName(binary_name_cache_str, sizeof(binary_name_cache_str));
27768d75effSDimitry Andric   ReadProcessName(process_name_cache_str, sizeof(process_name_cache_str));
27868d75effSDimitry Andric }
27968d75effSDimitry Andric 
28068d75effSDimitry Andric uptr ReadBinaryNameCached(/*out*/char *buf, uptr buf_len) {
28168d75effSDimitry Andric   CacheBinaryName();
28268d75effSDimitry Andric   uptr name_len = internal_strlen(binary_name_cache_str);
28368d75effSDimitry Andric   name_len = (name_len < buf_len - 1) ? name_len : buf_len - 1;
28468d75effSDimitry Andric   if (buf_len == 0)
28568d75effSDimitry Andric     return 0;
28668d75effSDimitry Andric   internal_memcpy(buf, binary_name_cache_str, name_len);
28768d75effSDimitry Andric   buf[name_len] = '\0';
28868d75effSDimitry Andric   return name_len;
28968d75effSDimitry Andric }
29068d75effSDimitry Andric 
291fe6060f1SDimitry Andric uptr ReadBinaryDir(/*out*/ char *buf, uptr buf_len) {
292fe6060f1SDimitry Andric   ReadBinaryNameCached(buf, buf_len);
293fe6060f1SDimitry Andric   const char *exec_name_pos = StripModuleName(buf);
294fe6060f1SDimitry Andric   uptr name_len = exec_name_pos - buf;
295fe6060f1SDimitry Andric   buf[name_len] = '\0';
296fe6060f1SDimitry Andric   return name_len;
297fe6060f1SDimitry Andric }
298fe6060f1SDimitry Andric 
2995ffd83dbSDimitry Andric #if !SANITIZER_GO
30068d75effSDimitry Andric void PrintCmdline() {
30168d75effSDimitry Andric   char **argv = GetArgv();
30268d75effSDimitry Andric   if (!argv) return;
30368d75effSDimitry Andric   Printf("\nCommand: ");
30468d75effSDimitry Andric   for (uptr i = 0; argv[i]; ++i)
30568d75effSDimitry Andric     Printf("%s ", argv[i]);
30668d75effSDimitry Andric   Printf("\n\n");
30768d75effSDimitry Andric }
3085ffd83dbSDimitry Andric #endif
30968d75effSDimitry Andric 
31068d75effSDimitry Andric // Malloc hooks.
31168d75effSDimitry Andric static const int kMaxMallocFreeHooks = 5;
31268d75effSDimitry Andric struct MallocFreeHook {
31368d75effSDimitry Andric   void (*malloc_hook)(const void *, uptr);
31468d75effSDimitry Andric   void (*free_hook)(const void *);
31568d75effSDimitry Andric };
31668d75effSDimitry Andric 
31768d75effSDimitry Andric static MallocFreeHook MFHooks[kMaxMallocFreeHooks];
31868d75effSDimitry Andric 
319*81ad6265SDimitry Andric void RunMallocHooks(void *ptr, uptr size) {
320*81ad6265SDimitry Andric   __sanitizer_malloc_hook(ptr, size);
32168d75effSDimitry Andric   for (int i = 0; i < kMaxMallocFreeHooks; i++) {
32268d75effSDimitry Andric     auto hook = MFHooks[i].malloc_hook;
323*81ad6265SDimitry Andric     if (!hook)
324*81ad6265SDimitry Andric       break;
32568d75effSDimitry Andric     hook(ptr, size);
32668d75effSDimitry Andric   }
32768d75effSDimitry Andric }
32868d75effSDimitry Andric 
329*81ad6265SDimitry Andric void RunFreeHooks(void *ptr) {
330*81ad6265SDimitry Andric   __sanitizer_free_hook(ptr);
33168d75effSDimitry Andric   for (int i = 0; i < kMaxMallocFreeHooks; i++) {
33268d75effSDimitry Andric     auto hook = MFHooks[i].free_hook;
333*81ad6265SDimitry Andric     if (!hook)
334*81ad6265SDimitry Andric       break;
33568d75effSDimitry Andric     hook(ptr);
33668d75effSDimitry Andric   }
33768d75effSDimitry Andric }
33868d75effSDimitry Andric 
33968d75effSDimitry Andric static int InstallMallocFreeHooks(void (*malloc_hook)(const void *, uptr),
34068d75effSDimitry Andric                                   void (*free_hook)(const void *)) {
34168d75effSDimitry Andric   if (!malloc_hook || !free_hook) return 0;
34268d75effSDimitry Andric   for (int i = 0; i < kMaxMallocFreeHooks; i++) {
34368d75effSDimitry Andric     if (MFHooks[i].malloc_hook == nullptr) {
34468d75effSDimitry Andric       MFHooks[i].malloc_hook = malloc_hook;
34568d75effSDimitry Andric       MFHooks[i].free_hook = free_hook;
34668d75effSDimitry Andric       return i + 1;
34768d75effSDimitry Andric     }
34868d75effSDimitry Andric   }
34968d75effSDimitry Andric   return 0;
35068d75effSDimitry Andric }
35168d75effSDimitry Andric 
352fe6060f1SDimitry Andric void internal_sleep(unsigned seconds) {
353fe6060f1SDimitry Andric   internal_usleep((u64)seconds * 1000 * 1000);
354fe6060f1SDimitry Andric }
355fe6060f1SDimitry Andric void SleepForSeconds(unsigned seconds) {
356fe6060f1SDimitry Andric   internal_usleep((u64)seconds * 1000 * 1000);
357fe6060f1SDimitry Andric }
358fe6060f1SDimitry Andric void SleepForMillis(unsigned millis) { internal_usleep((u64)millis * 1000); }
359fe6060f1SDimitry Andric 
360*81ad6265SDimitry Andric void WaitForDebugger(unsigned seconds, const char *label) {
361*81ad6265SDimitry Andric   if (seconds) {
362*81ad6265SDimitry Andric     Report("Sleeping for %u second(s) %s\n", seconds, label);
363*81ad6265SDimitry Andric     SleepForSeconds(seconds);
364*81ad6265SDimitry Andric   }
365*81ad6265SDimitry Andric }
366*81ad6265SDimitry Andric 
36768d75effSDimitry Andric } // namespace __sanitizer
36868d75effSDimitry Andric 
36968d75effSDimitry Andric using namespace __sanitizer;
37068d75effSDimitry Andric 
37168d75effSDimitry Andric extern "C" {
37268d75effSDimitry Andric SANITIZER_INTERFACE_WEAK_DEF(void, __sanitizer_report_error_summary,
37368d75effSDimitry Andric                              const char *error_summary) {
37468d75effSDimitry Andric   Printf("%s\n", error_summary);
37568d75effSDimitry Andric }
37668d75effSDimitry Andric 
37768d75effSDimitry Andric SANITIZER_INTERFACE_ATTRIBUTE
37868d75effSDimitry Andric int __sanitizer_acquire_crash_state() {
37968d75effSDimitry Andric   static atomic_uint8_t in_crash_state = {};
38068d75effSDimitry Andric   return !atomic_exchange(&in_crash_state, 1, memory_order_relaxed);
38168d75effSDimitry Andric }
38268d75effSDimitry Andric 
38368d75effSDimitry Andric SANITIZER_INTERFACE_ATTRIBUTE
38468d75effSDimitry Andric int __sanitizer_install_malloc_and_free_hooks(void (*malloc_hook)(const void *,
38568d75effSDimitry Andric                                                                   uptr),
38668d75effSDimitry Andric                                               void (*free_hook)(const void *)) {
38768d75effSDimitry Andric   return InstallMallocFreeHooks(malloc_hook, free_hook);
38868d75effSDimitry Andric }
389*81ad6265SDimitry Andric 
390*81ad6265SDimitry Andric // Provide default (no-op) implementation of malloc hooks.
391*81ad6265SDimitry Andric SANITIZER_INTERFACE_WEAK_DEF(void, __sanitizer_malloc_hook, void *ptr,
392*81ad6265SDimitry Andric                              uptr size) {
393*81ad6265SDimitry Andric   (void)ptr;
394*81ad6265SDimitry Andric   (void)size;
395*81ad6265SDimitry Andric }
396*81ad6265SDimitry Andric 
397*81ad6265SDimitry Andric SANITIZER_INTERFACE_WEAK_DEF(void, __sanitizer_free_hook, void *ptr) {
398*81ad6265SDimitry Andric   (void)ptr;
399*81ad6265SDimitry Andric }
400*81ad6265SDimitry Andric 
40168d75effSDimitry Andric } // extern "C"
402