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"
|
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"
|
2015-03-18 07:46:06 +08:00
|
|
|
#include "sanitizer_stacktrace_printer.h"
|
2015-02-27 10:29:25 +08:00
|
|
|
#include "sanitizer_symbolizer.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;
|
2012-11-23 23:38:49 +08:00
|
|
|
|
2014-12-12 02:30:25 +08:00
|
|
|
StaticSpinMutex report_file_mu;
|
|
|
|
ReportFile report_file = {&report_file_mu, kStderrFd, "", "", 0};
|
2013-01-19 00:44:27 +08:00
|
|
|
|
2014-12-12 02:30:25 +08:00
|
|
|
void RawWrite(const char *buffer) {
|
|
|
|
report_file.Write(buffer, internal_strlen(buffer));
|
|
|
|
}
|
2013-09-05 11:19:57 +08:00
|
|
|
|
2014-12-12 02:30:25 +08:00
|
|
|
void ReportFile::ReopenIfNecessary() {
|
|
|
|
mu->CheckLocked();
|
|
|
|
if (fd == kStdoutFd || fd == kStderrFd) return;
|
|
|
|
|
|
|
|
uptr pid = internal_getpid();
|
|
|
|
// If in tracer, use the parent's file.
|
|
|
|
if (pid == stoptheworld_tracer_pid)
|
|
|
|
pid = stoptheworld_tracer_ppid;
|
|
|
|
if (fd != kInvalidFd) {
|
|
|
|
// If the report file is already opened by the current process,
|
|
|
|
// do nothing. Otherwise the report file was opened by the parent
|
|
|
|
// process, close it now.
|
|
|
|
if (fd_pid == pid)
|
|
|
|
return;
|
|
|
|
else
|
2015-04-09 20:37:05 +08:00
|
|
|
CloseFile(fd);
|
2014-12-12 02:30:25 +08:00
|
|
|
}
|
2013-09-05 11:19:57 +08:00
|
|
|
|
2015-07-29 04:27:51 +08:00
|
|
|
const char *exe_name = GetProcessName();
|
2015-06-05 14:08:23 +08:00
|
|
|
if (common_flags()->log_exe_name && exe_name) {
|
|
|
|
internal_snprintf(full_path, kMaxPathLength, "%s.%s.%zu", path_prefix,
|
|
|
|
exe_name, pid);
|
|
|
|
} else {
|
|
|
|
internal_snprintf(full_path, kMaxPathLength, "%s.%zu", path_prefix, pid);
|
|
|
|
}
|
2015-04-09 00:03:22 +08:00
|
|
|
fd = OpenFile(full_path, WrOnly);
|
|
|
|
if (fd == kInvalidFd) {
|
2014-12-12 02:30:25 +08:00
|
|
|
const char *ErrorMsgPrefix = "ERROR: Can't open file: ";
|
2015-04-09 22:11:25 +08:00
|
|
|
WriteToFile(kStderrFd, ErrorMsgPrefix, internal_strlen(ErrorMsgPrefix));
|
|
|
|
WriteToFile(kStderrFd, full_path, internal_strlen(full_path));
|
2014-12-12 02:30:25 +08:00
|
|
|
Die();
|
|
|
|
}
|
|
|
|
fd_pid = pid;
|
|
|
|
}
|
|
|
|
|
|
|
|
void ReportFile::SetReportPath(const char *path) {
|
|
|
|
if (!path)
|
|
|
|
return;
|
|
|
|
uptr len = internal_strlen(path);
|
|
|
|
if (len > sizeof(path_prefix) - 100) {
|
|
|
|
Report("ERROR: Path is too long: %c%c%c%c%c%c%c%c...\n",
|
|
|
|
path[0], path[1], path[2], path[3],
|
|
|
|
path[4], path[5], path[6], path[7]);
|
|
|
|
Die();
|
|
|
|
}
|
|
|
|
|
|
|
|
SpinMutexLock l(mu);
|
|
|
|
if (fd != kStdoutFd && fd != kStderrFd && fd != kInvalidFd)
|
2015-04-09 20:37:05 +08:00
|
|
|
CloseFile(fd);
|
2014-12-12 02:30:25 +08:00
|
|
|
fd = kInvalidFd;
|
|
|
|
if (internal_strcmp(path, "stdout") == 0) {
|
|
|
|
fd = kStdoutFd;
|
|
|
|
} else if (internal_strcmp(path, "stderr") == 0) {
|
|
|
|
fd = kStderrFd;
|
|
|
|
} else {
|
|
|
|
internal_snprintf(path_prefix, kMaxPathLength, "%s", path);
|
|
|
|
}
|
|
|
|
}
|
2012-09-14 12:35:14 +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;
|
2015-11-21 02:42:01 +08:00
|
|
|
if (raw_report || recursion_count) {
|
|
|
|
// If raw report is requested or we went into recursion, just die.
|
2015-08-13 07:55:38 +08:00
|
|
|
// The Report() and CHECK calls below may call mmap recursively and fail.
|
|
|
|
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);
|
2015-11-13 02:50:19 +08:00
|
|
|
#ifndef 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");
|
|
|
|
}
|
|
|
|
|
2015-07-18 07:50:08 +08:00
|
|
|
bool ReadFileToBuffer(const char *file_name, char **buff, uptr *buff_size,
|
|
|
|
uptr *read_len, uptr max_len, error_t *errno_p) {
|
2012-11-23 23:38:49 +08:00
|
|
|
uptr PageSize = GetPageSizeCached();
|
|
|
|
uptr kMinFileLen = PageSize;
|
2015-07-18 07:50:08 +08:00
|
|
|
*buff = nullptr;
|
2012-06-07 13:38:26 +08:00
|
|
|
*buff_size = 0;
|
2015-07-18 07:50:08 +08:00
|
|
|
*read_len = 0;
|
2012-06-07 13:38:26 +08:00
|
|
|
// The files we usually open are not seekable, so try different buffer sizes.
|
|
|
|
for (uptr size = kMinFileLen; size <= max_len; size *= 2) {
|
2015-04-09 00:03:22 +08:00
|
|
|
fd_t fd = OpenFile(file_name, RdOnly, errno_p);
|
2015-07-18 07:50:08 +08:00
|
|
|
if (fd == kInvalidFd) return false;
|
2012-06-07 13:38:26 +08:00
|
|
|
UnmapOrDie(*buff, *buff_size);
|
2014-02-27 04:33:22 +08:00
|
|
|
*buff = (char*)MmapOrDie(size, __func__);
|
2012-06-07 13:38:26 +08:00
|
|
|
*buff_size = size;
|
2015-07-18 07:50:08 +08:00
|
|
|
*read_len = 0;
|
2012-06-07 13:38:26 +08:00
|
|
|
// Read up to one page at a time.
|
|
|
|
bool reached_eof = false;
|
2015-07-18 07:50:08 +08:00
|
|
|
while (*read_len + PageSize <= size) {
|
2015-04-09 21:38:14 +08:00
|
|
|
uptr just_read;
|
2015-07-18 07:50:08 +08:00
|
|
|
if (!ReadFromFile(fd, *buff + *read_len, PageSize, &just_read, errno_p)) {
|
2015-01-16 23:25:16 +08:00
|
|
|
UnmapOrDie(*buff, *buff_size);
|
2015-07-18 07:50:08 +08:00
|
|
|
return false;
|
2015-01-16 23:25:16 +08:00
|
|
|
}
|
2012-06-07 13:38:26 +08:00
|
|
|
if (just_read == 0) {
|
|
|
|
reached_eof = true;
|
|
|
|
break;
|
|
|
|
}
|
2015-07-18 07:50:08 +08:00
|
|
|
*read_len += just_read;
|
2012-06-07 13:38:26 +08:00
|
|
|
}
|
2015-04-09 20:37:05 +08:00
|
|
|
CloseFile(fd);
|
2012-06-07 13:38:26 +08:00
|
|
|
if (reached_eof) // We've read the whole file.
|
|
|
|
break;
|
|
|
|
}
|
2015-07-18 07:50:08 +08:00
|
|
|
return true;
|
2012-06-07 13:38:26 +08:00
|
|
|
}
|
|
|
|
|
2013-05-13 19:58:48 +08:00
|
|
|
typedef bool UptrComparisonFunction(const uptr &a, const uptr &b);
|
|
|
|
|
|
|
|
template<class T>
|
|
|
|
static inline bool CompareLess(const T &a, const T &b) {
|
|
|
|
return a < b;
|
|
|
|
}
|
|
|
|
|
2012-06-15 15:00:31 +08:00
|
|
|
void SortArray(uptr *array, uptr size) {
|
2013-05-13 19:58:48 +08:00
|
|
|
InternalSort<uptr*, UptrComparisonFunction>(&array, size, CompareLess);
|
2012-06-15 15:00:31 +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;
|
|
|
|
}
|
|
|
|
|
2013-11-02 01:02:14 +08:00
|
|
|
void ReportErrorSummary(const char *error_message) {
|
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);
|
|
|
|
buff.append("SUMMARY: %s: %s", SanitizerToolName, error_message);
|
2013-11-02 01:02:14 +08:00
|
|
|
__sanitizer_report_error_summary(buff.data());
|
|
|
|
}
|
|
|
|
|
2015-03-18 07:46:06 +08:00
|
|
|
#ifndef SANITIZER_GO
|
2015-02-27 10:29:25 +08:00
|
|
|
void ReportErrorSummary(const char *error_type, const AddressInfo &info) {
|
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);
|
2015-03-18 07:46:06 +08:00
|
|
|
buff.append("%s ", error_type);
|
2015-06-04 09:20:06 +08:00
|
|
|
RenderFrame(&buff, "%L %F", 0, info, common_flags()->symbolize_vs_style,
|
|
|
|
common_flags()->strip_path_prefix);
|
2013-11-02 01:02:14 +08:00
|
|
|
ReportErrorSummary(buff.data());
|
|
|
|
}
|
2015-03-18 07:46:06 +08:00
|
|
|
#endif
|
2013-11-02 01:02:14 +08:00
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2015-01-09 06:31:14 +08:00
|
|
|
void LoadedModule::clear() {
|
|
|
|
InternalFree(full_name_);
|
2015-04-06 20:49:30 +08:00
|
|
|
full_name_ = nullptr;
|
2015-01-09 06:31:14 +08:00
|
|
|
while (!ranges_.empty()) {
|
|
|
|
AddressRange *r = ranges_.front();
|
|
|
|
ranges_.pop_front();
|
|
|
|
InternalFree(r);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-06-11 18:11:51 +08:00
|
|
|
void LoadedModule::addAddressRange(uptr beg, uptr end, bool executable) {
|
2015-01-09 06:03:05 +08:00
|
|
|
void *mem = InternalAlloc(sizeof(AddressRange));
|
|
|
|
AddressRange *r = new(mem) AddressRange(beg, end, executable);
|
|
|
|
ranges_.push_back(r);
|
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-08-11 07:40:27 +08:00
|
|
|
static const char kPathSeparator = SANITIZER_WINDOWS ? ';' : ':';
|
|
|
|
|
|
|
|
char *FindPathToBinary(const char *name) {
|
2016-01-27 04:10:01 +08:00
|
|
|
if (FileExists(name)) {
|
|
|
|
return internal_strdup(name);
|
|
|
|
}
|
|
|
|
|
2015-08-11 07:40:27 +08:00
|
|
|
const char *path = GetEnv("PATH");
|
|
|
|
if (!path)
|
2015-10-01 07:52:54 +08:00
|
|
|
return nullptr;
|
2015-08-11 07:40:27 +08:00
|
|
|
uptr name_len = internal_strlen(name);
|
|
|
|
InternalScopedBuffer<char> buffer(kMaxPathLength);
|
|
|
|
const char *beg = path;
|
|
|
|
while (true) {
|
|
|
|
const char *end = internal_strchrnul(beg, kPathSeparator);
|
|
|
|
uptr prefix_len = end - beg;
|
|
|
|
if (prefix_len + name_len + 2 <= kMaxPathLength) {
|
|
|
|
internal_memcpy(buffer.data(), beg, prefix_len);
|
|
|
|
buffer[prefix_len] = '/';
|
|
|
|
internal_memcpy(&buffer[prefix_len + 1], name, name_len);
|
|
|
|
buffer[prefix_len + 1 + name_len] = '\0';
|
|
|
|
if (FileExists(buffer.data()))
|
|
|
|
return internal_strdup(buffer.data());
|
|
|
|
}
|
|
|
|
if (*end == '\0') break;
|
|
|
|
beg = end + 1;
|
|
|
|
}
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
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" {
|
2012-09-14 12:35:14 +08:00
|
|
|
void __sanitizer_set_report_path(const char *path) {
|
2014-12-12 02:30:25 +08:00
|
|
|
report_file.SetReportPath(path);
|
2012-11-02 17:23:36 +08:00
|
|
|
}
|
2012-12-10 21:10:40 +08:00
|
|
|
|
2016-05-28 05:23:05 +08:00
|
|
|
void __sanitizer_set_report_fd(void *fd) {
|
2016-05-28 05:35:58 +08:00
|
|
|
report_file.fd = (fd_t)reinterpret_cast<uptr>(fd);
|
2016-05-28 05:23:05 +08:00
|
|
|
report_file.fd_pid = internal_getpid();
|
|
|
|
}
|
|
|
|
|
2013-02-06 20:36:49 +08:00
|
|
|
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
|
|
|
|
|
|
|
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"
|