2012-06-06 17:26:25 +08:00
|
|
|
//===-- sanitizer_common.cc -----------------------------------------------===//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// This file is shared between AddressSanitizer and ThreadSanitizer
|
|
|
|
// run-time libraries.
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "sanitizer_common.h"
|
2016-06-17 04:06:06 +08:00
|
|
|
#include "sanitizer_allocator_interface.h"
|
2015-01-09 06:03:05 +08:00
|
|
|
#include "sanitizer_allocator_internal.h"
|
2018-05-02 05:01:53 +08:00
|
|
|
#include "sanitizer_atomic.h"
|
2013-10-04 16:55:03 +08:00
|
|
|
#include "sanitizer_flags.h"
|
2012-06-06 17:26:25 +08:00
|
|
|
#include "sanitizer_libc.h"
|
2015-01-09 06:03:05 +08:00
|
|
|
#include "sanitizer_placement_new.h"
|
2012-06-15 15:00:31 +08:00
|
|
|
|
2012-06-06 17:26:25 +08:00
|
|
|
namespace __sanitizer {
|
|
|
|
|
2013-01-31 22:11:21 +08:00
|
|
|
const char *SanitizerToolName = "SanitizerTool";
|
|
|
|
|
2015-01-20 21:21:20 +08:00
|
|
|
atomic_uint32_t current_verbosity;
|
2016-06-27 23:32:18 +08:00
|
|
|
uptr PageSizeCached;
|
2017-11-22 05:14:00 +08:00
|
|
|
u32 NumberOfCPUsCached;
|
2012-11-23 23:38:49 +08:00
|
|
|
|
2013-12-04 22:37:01 +08:00
|
|
|
// PID of the tracer task in StopTheWorld. It shares the address space with the
|
|
|
|
// main process, but has a different PID and thus requires special handling.
|
|
|
|
uptr stoptheworld_tracer_pid = 0;
|
|
|
|
// Cached pid of parent process - if the parent process dies, we want to keep
|
|
|
|
// writing to the same log file.
|
|
|
|
uptr stoptheworld_tracer_ppid = 0;
|
|
|
|
|
2015-08-13 07:55:38 +08:00
|
|
|
void NORETURN ReportMmapFailureAndDie(uptr size, const char *mem_type,
|
2015-11-21 02:42:01 +08:00
|
|
|
const char *mmap_type, error_t err,
|
|
|
|
bool raw_report) {
|
2015-08-13 07:55:38 +08:00
|
|
|
static int recursion_count;
|
2018-05-08 00:38:45 +08:00
|
|
|
if (SANITIZER_RTEMS || raw_report || recursion_count) {
|
|
|
|
// If we are on RTEMS or raw report is requested or we went into recursion,
|
|
|
|
// just die. The Report() and CHECK calls below may call mmap recursively
|
|
|
|
// and fail.
|
2015-08-13 07:55:38 +08:00
|
|
|
RawWrite("ERROR: Failed to mmap\n");
|
|
|
|
Die();
|
|
|
|
}
|
|
|
|
recursion_count++;
|
|
|
|
Report("ERROR: %s failed to "
|
2015-11-05 09:16:48 +08:00
|
|
|
"%s 0x%zx (%zd) bytes of %s (error code: %d)\n",
|
|
|
|
SanitizerToolName, mmap_type, size, size, mem_type, err);
|
2016-10-29 04:14:18 +08:00
|
|
|
#if !SANITIZER_GO
|
2015-08-13 07:55:38 +08:00
|
|
|
DumpProcessMap();
|
2015-11-13 02:50:19 +08:00
|
|
|
#endif
|
2015-08-13 07:55:38 +08:00
|
|
|
UNREACHABLE("unable to mmap");
|
|
|
|
}
|
|
|
|
|
2013-05-13 19:58:48 +08:00
|
|
|
typedef bool UptrComparisonFunction(const uptr &a, const uptr &b);
|
2016-08-27 07:58:42 +08:00
|
|
|
typedef bool U32ComparisonFunction(const u32 &a, const u32 &b);
|
2013-05-13 19:58:48 +08:00
|
|
|
|
2013-10-04 16:55:03 +08:00
|
|
|
const char *StripPathPrefix(const char *filepath,
|
|
|
|
const char *strip_path_prefix) {
|
2015-10-01 07:52:54 +08:00
|
|
|
if (!filepath) return nullptr;
|
|
|
|
if (!strip_path_prefix) return filepath;
|
2015-03-03 02:55:46 +08:00
|
|
|
const char *res = filepath;
|
|
|
|
if (const char *pos = internal_strstr(filepath, strip_path_prefix))
|
|
|
|
res = pos + internal_strlen(strip_path_prefix);
|
|
|
|
if (res[0] == '.' && res[1] == '/')
|
|
|
|
res += 2;
|
|
|
|
return res;
|
2013-10-04 16:55:03 +08:00
|
|
|
}
|
|
|
|
|
2014-11-05 03:34:29 +08:00
|
|
|
const char *StripModuleName(const char *module) {
|
2015-10-01 07:52:54 +08:00
|
|
|
if (!module)
|
|
|
|
return nullptr;
|
2015-04-02 22:48:08 +08:00
|
|
|
if (SANITIZER_WINDOWS) {
|
|
|
|
// On Windows, both slash and backslash are possible.
|
|
|
|
// Pick the one that goes last.
|
|
|
|
if (const char *bslash_pos = internal_strrchr(module, '\\'))
|
|
|
|
return StripModuleName(bslash_pos + 1);
|
|
|
|
}
|
|
|
|
if (const char *slash_pos = internal_strrchr(module, '/')) {
|
2014-11-05 03:34:29 +08:00
|
|
|
return slash_pos + 1;
|
2015-04-02 22:48:08 +08:00
|
|
|
}
|
2014-11-05 03:34:29 +08:00
|
|
|
return module;
|
|
|
|
}
|
|
|
|
|
2017-04-15 02:24:35 +08:00
|
|
|
void ReportErrorSummary(const char *error_message, const char *alt_tool_name) {
|
2013-11-14 16:56:59 +08:00
|
|
|
if (!common_flags()->print_summary)
|
|
|
|
return;
|
2014-12-03 06:20:11 +08:00
|
|
|
InternalScopedString buff(kMaxSummaryLength);
|
2017-04-15 02:24:35 +08:00
|
|
|
buff.append("SUMMARY: %s: %s",
|
|
|
|
alt_tool_name ? alt_tool_name : SanitizerToolName, error_message);
|
2013-11-02 01:02:14 +08:00
|
|
|
__sanitizer_report_error_summary(buff.data());
|
|
|
|
}
|
|
|
|
|
Reapply: [asan] On OS X, log reports to syslog and os_trace
When ASan currently detects a bug, by default it will only print out the text
of the report to stderr. This patch changes this behavior and writes the full
text of the report to syslog before we terminate the process. It also calls
os_trace (Activity Tracing available on OS X and iOS) with a message saying
that the report is available in syslog. This is useful, because this message
will be shown in the crash log.
For this to work, the patch makes sure we store the full report into
error_message_buffer unconditionally, and it also strips out ANSI escape
sequences from the report (they are used when producing colored reports).
I've initially tried to log to syslog during printing, which is done on Android
right now. The advantage is that if we crash during error reporting or the
produced error does not go through ScopedInErrorReport, we would still get a
(partial) message in the syslog. However, that solution is very problematic on
OS X. One issue is that the logging routine uses GCD, which may spawn a new
thread on its behalf. In many cases, the reporting logic locks threadRegistry,
which leads to deadlocks.
Reviewed at http://reviews.llvm.org/D13452
(In addition, add sanitizer_common_libcdep.cc to buildgo.sh to avoid
build failures on Linux.)
llvm-svn: 253688
2015-11-21 02:41:44 +08:00
|
|
|
// Removes the ANSI escape sequences from the input string (in-place).
|
|
|
|
void RemoveANSIEscapeSequencesFromString(char *str) {
|
|
|
|
if (!str)
|
|
|
|
return;
|
|
|
|
|
|
|
|
// We are going to remove the escape sequences in place.
|
|
|
|
char *s = str;
|
|
|
|
char *z = str;
|
|
|
|
while (*s != '\0') {
|
|
|
|
CHECK_GE(s, z);
|
|
|
|
// Skip over ANSI escape sequences with pointer 's'.
|
|
|
|
if (*s == '\033' && *(s + 1) == '[') {
|
|
|
|
s = internal_strchrnul(s, 'm');
|
|
|
|
if (*s == '\0') {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
s++;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
// 's' now points at a character we want to keep. Copy over the buffer
|
|
|
|
// content if the escape sequence has been perviously skipped andadvance
|
|
|
|
// both pointers.
|
|
|
|
if (s != z)
|
|
|
|
*z = *s;
|
|
|
|
|
|
|
|
// If we have not seen an escape sequence, just advance both pointers.
|
|
|
|
z++;
|
|
|
|
s++;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Null terminate the string.
|
|
|
|
*z = '\0';
|
|
|
|
}
|
|
|
|
|
2015-04-06 20:49:30 +08:00
|
|
|
void LoadedModule::set(const char *module_name, uptr base_address) {
|
|
|
|
clear();
|
2013-09-10 22:36:16 +08:00
|
|
|
full_name_ = internal_strdup(module_name);
|
|
|
|
base_address_ = base_address;
|
|
|
|
}
|
|
|
|
|
2016-12-03 05:27:14 +08:00
|
|
|
void LoadedModule::set(const char *module_name, uptr base_address,
|
2017-01-07 03:34:54 +08:00
|
|
|
ModuleArch arch, u8 uuid[kModuleUUIDSize],
|
|
|
|
bool instrumented) {
|
2016-12-03 05:27:14 +08:00
|
|
|
set(module_name, base_address);
|
|
|
|
arch_ = arch;
|
|
|
|
internal_memcpy(uuid_, uuid, sizeof(uuid_));
|
2017-01-07 03:34:54 +08:00
|
|
|
instrumented_ = instrumented;
|
2016-12-03 05:27:14 +08:00
|
|
|
}
|
|
|
|
|
2015-01-09 06:31:14 +08:00
|
|
|
void LoadedModule::clear() {
|
|
|
|
InternalFree(full_name_);
|
2017-01-07 04:57:47 +08:00
|
|
|
base_address_ = 0;
|
|
|
|
max_executable_address_ = 0;
|
2015-04-06 20:49:30 +08:00
|
|
|
full_name_ = nullptr;
|
2016-12-03 05:27:14 +08:00
|
|
|
arch_ = kModuleArchUnknown;
|
|
|
|
internal_memset(uuid_, 0, kModuleUUIDSize);
|
2017-01-07 03:34:54 +08:00
|
|
|
instrumented_ = false;
|
2015-01-09 06:31:14 +08:00
|
|
|
while (!ranges_.empty()) {
|
|
|
|
AddressRange *r = ranges_.front();
|
|
|
|
ranges_.pop_front();
|
|
|
|
InternalFree(r);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-04-18 00:34:38 +08:00
|
|
|
void LoadedModule::addAddressRange(uptr beg, uptr end, bool executable,
|
2017-07-26 02:16:58 +08:00
|
|
|
bool writable, const char *name) {
|
2015-01-09 06:03:05 +08:00
|
|
|
void *mem = InternalAlloc(sizeof(AddressRange));
|
2017-07-26 02:16:58 +08:00
|
|
|
AddressRange *r =
|
|
|
|
new(mem) AddressRange(beg, end, executable, writable, name);
|
2015-01-09 06:03:05 +08:00
|
|
|
ranges_.push_back(r);
|
2017-01-07 04:57:47 +08:00
|
|
|
if (executable && end > max_executable_address_)
|
|
|
|
max_executable_address_ = end;
|
2013-09-10 22:36:16 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
bool LoadedModule::containsAddress(uptr address) const {
|
2016-01-15 10:19:20 +08:00
|
|
|
for (const AddressRange &r : ranges()) {
|
|
|
|
if (r.beg <= address && address < r.end)
|
2013-09-10 22:36:16 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2014-04-14 22:51:01 +08:00
|
|
|
static atomic_uintptr_t g_total_mmaped;
|
|
|
|
|
|
|
|
void IncreaseTotalMmap(uptr size) {
|
|
|
|
if (!common_flags()->mmap_limit_mb) return;
|
|
|
|
uptr total_mmaped =
|
|
|
|
atomic_fetch_add(&g_total_mmaped, size, memory_order_relaxed) + size;
|
2014-12-12 02:45:24 +08:00
|
|
|
// Since for now mmap_limit_mb is not a user-facing flag, just kill
|
|
|
|
// a program. Use RAW_CHECK to avoid extra mmaps in reporting.
|
|
|
|
RAW_CHECK((total_mmaped >> 20) < common_flags()->mmap_limit_mb);
|
2014-04-14 22:51:01 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void DecreaseTotalMmap(uptr size) {
|
|
|
|
if (!common_flags()->mmap_limit_mb) return;
|
|
|
|
atomic_fetch_sub(&g_total_mmaped, size, memory_order_relaxed);
|
|
|
|
}
|
|
|
|
|
2015-02-20 06:56:47 +08:00
|
|
|
bool TemplateMatch(const char *templ, const char *str) {
|
2015-10-01 07:52:54 +08:00
|
|
|
if ((!str) || str[0] == 0)
|
2015-02-20 06:56:47 +08:00
|
|
|
return false;
|
|
|
|
bool start = false;
|
|
|
|
if (templ && templ[0] == '^') {
|
|
|
|
start = true;
|
|
|
|
templ++;
|
|
|
|
}
|
|
|
|
bool asterisk = false;
|
|
|
|
while (templ && templ[0]) {
|
|
|
|
if (templ[0] == '*') {
|
|
|
|
templ++;
|
|
|
|
start = false;
|
|
|
|
asterisk = true;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (templ[0] == '$')
|
|
|
|
return str[0] == 0 || asterisk;
|
|
|
|
if (str[0] == 0)
|
|
|
|
return false;
|
|
|
|
char *tpos = (char*)internal_strchr(templ, '*');
|
|
|
|
char *tpos1 = (char*)internal_strchr(templ, '$');
|
2015-10-01 07:52:54 +08:00
|
|
|
if ((!tpos) || (tpos1 && tpos1 < tpos))
|
2015-02-20 06:56:47 +08:00
|
|
|
tpos = tpos1;
|
2015-10-01 07:52:54 +08:00
|
|
|
if (tpos)
|
2015-02-20 06:56:47 +08:00
|
|
|
tpos[0] = 0;
|
|
|
|
const char *str0 = str;
|
|
|
|
const char *spos = internal_strstr(str, templ);
|
|
|
|
str = spos + internal_strlen(templ);
|
|
|
|
templ = tpos;
|
|
|
|
if (tpos)
|
|
|
|
tpos[0] = tpos == tpos1 ? '$' : '*';
|
2015-10-01 07:52:54 +08:00
|
|
|
if (!spos)
|
2015-02-20 06:56:47 +08:00
|
|
|
return false;
|
|
|
|
if (start && spos != str0)
|
|
|
|
return false;
|
|
|
|
start = false;
|
|
|
|
asterisk = false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2015-06-04 15:29:43 +08:00
|
|
|
static char binary_name_cache_str[kMaxPathLength];
|
2015-07-29 04:27:51 +08:00
|
|
|
static char process_name_cache_str[kMaxPathLength];
|
2015-06-04 15:29:43 +08:00
|
|
|
|
2015-07-29 04:27:51 +08:00
|
|
|
const char *GetProcessName() {
|
|
|
|
return process_name_cache_str;
|
|
|
|
}
|
|
|
|
|
2015-07-29 05:01:42 +08:00
|
|
|
static uptr ReadProcessName(/*out*/ char *buf, uptr buf_len) {
|
|
|
|
ReadLongProcessName(buf, buf_len);
|
|
|
|
char *s = const_cast<char *>(StripModuleName(buf));
|
|
|
|
uptr len = internal_strlen(s);
|
|
|
|
if (s != buf) {
|
|
|
|
internal_memmove(buf, s, len);
|
|
|
|
buf[len] = '\0';
|
|
|
|
}
|
|
|
|
return len;
|
|
|
|
}
|
|
|
|
|
2015-07-29 04:27:51 +08:00
|
|
|
void UpdateProcessName() {
|
|
|
|
ReadProcessName(process_name_cache_str, sizeof(process_name_cache_str));
|
2015-06-04 15:29:43 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Call once to make sure that binary_name_cache_str is initialized
|
|
|
|
void CacheBinaryName() {
|
2015-06-29 23:58:16 +08:00
|
|
|
if (binary_name_cache_str[0] != '\0')
|
|
|
|
return;
|
2015-06-04 15:29:43 +08:00
|
|
|
ReadBinaryName(binary_name_cache_str, sizeof(binary_name_cache_str));
|
2015-07-29 04:27:51 +08:00
|
|
|
ReadProcessName(process_name_cache_str, sizeof(process_name_cache_str));
|
2015-06-04 15:29:43 +08:00
|
|
|
}
|
|
|
|
|
2015-06-29 23:58:16 +08:00
|
|
|
uptr ReadBinaryNameCached(/*out*/char *buf, uptr buf_len) {
|
|
|
|
CacheBinaryName();
|
|
|
|
uptr name_len = internal_strlen(binary_name_cache_str);
|
|
|
|
name_len = (name_len < buf_len - 1) ? name_len : buf_len - 1;
|
|
|
|
if (buf_len == 0)
|
|
|
|
return 0;
|
|
|
|
internal_memcpy(buf, binary_name_cache_str, name_len);
|
|
|
|
buf[name_len] = '\0';
|
|
|
|
return name_len;
|
|
|
|
}
|
|
|
|
|
2016-01-18 15:55:12 +08:00
|
|
|
void PrintCmdline() {
|
|
|
|
char **argv = GetArgv();
|
|
|
|
if (!argv) return;
|
|
|
|
Printf("\nCommand: ");
|
|
|
|
for (uptr i = 0; argv[i]; ++i)
|
|
|
|
Printf("%s ", argv[i]);
|
|
|
|
Printf("\n\n");
|
|
|
|
}
|
|
|
|
|
2016-06-17 04:06:06 +08:00
|
|
|
// Malloc hooks.
|
|
|
|
static const int kMaxMallocFreeHooks = 5;
|
|
|
|
struct MallocFreeHook {
|
|
|
|
void (*malloc_hook)(const void *, uptr);
|
|
|
|
void (*free_hook)(const void *);
|
|
|
|
};
|
|
|
|
|
|
|
|
static MallocFreeHook MFHooks[kMaxMallocFreeHooks];
|
|
|
|
|
|
|
|
void RunMallocHooks(const void *ptr, uptr size) {
|
|
|
|
for (int i = 0; i < kMaxMallocFreeHooks; i++) {
|
|
|
|
auto hook = MFHooks[i].malloc_hook;
|
|
|
|
if (!hook) return;
|
|
|
|
hook(ptr, size);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void RunFreeHooks(const void *ptr) {
|
|
|
|
for (int i = 0; i < kMaxMallocFreeHooks; i++) {
|
|
|
|
auto hook = MFHooks[i].free_hook;
|
|
|
|
if (!hook) return;
|
|
|
|
hook(ptr);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static int InstallMallocFreeHooks(void (*malloc_hook)(const void *, uptr),
|
|
|
|
void (*free_hook)(const void *)) {
|
|
|
|
if (!malloc_hook || !free_hook) return 0;
|
|
|
|
for (int i = 0; i < kMaxMallocFreeHooks; i++) {
|
|
|
|
if (MFHooks[i].malloc_hook == nullptr) {
|
|
|
|
MFHooks[i].malloc_hook = malloc_hook;
|
|
|
|
MFHooks[i].free_hook = free_hook;
|
|
|
|
return i + 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2015-10-01 07:52:54 +08:00
|
|
|
} // namespace __sanitizer
|
2012-09-14 12:35:14 +08:00
|
|
|
|
2012-11-02 17:23:36 +08:00
|
|
|
using namespace __sanitizer; // NOLINT
|
|
|
|
|
|
|
|
extern "C" {
|
2017-02-01 04:23:14 +08:00
|
|
|
SANITIZER_INTERFACE_WEAK_DEF(void, __sanitizer_report_error_summary,
|
|
|
|
const char *error_summary) {
|
2013-10-23 15:45:53 +08:00
|
|
|
Printf("%s\n", error_summary);
|
2013-02-06 20:36:49 +08:00
|
|
|
}
|
2014-12-16 07:02:57 +08:00
|
|
|
|
2018-05-02 05:01:53 +08:00
|
|
|
SANITIZER_INTERFACE_ATTRIBUTE
|
|
|
|
int __sanitizer_acquire_crash_state() {
|
|
|
|
static atomic_uint8_t in_crash_state = {};
|
|
|
|
return !atomic_exchange(&in_crash_state, 1, memory_order_relaxed);
|
|
|
|
}
|
|
|
|
|
2014-12-16 07:02:57 +08:00
|
|
|
SANITIZER_INTERFACE_ATTRIBUTE
|
|
|
|
void __sanitizer_set_death_callback(void (*callback)(void)) {
|
|
|
|
SetUserDieCallback(callback);
|
|
|
|
}
|
2016-06-17 04:06:06 +08:00
|
|
|
|
|
|
|
SANITIZER_INTERFACE_ATTRIBUTE
|
|
|
|
int __sanitizer_install_malloc_and_free_hooks(void (*malloc_hook)(const void *,
|
|
|
|
uptr),
|
|
|
|
void (*free_hook)(const void *)) {
|
|
|
|
return InstallMallocFreeHooks(malloc_hook, free_hook);
|
|
|
|
}
|
2015-10-01 07:52:54 +08:00
|
|
|
} // extern "C"
|