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"
|
|
|
|
#include "sanitizer_libc.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";
|
2013-03-15 22:37:21 +08:00
|
|
|
uptr SanitizerVerbosity = 0;
|
2013-01-31 22:11:21 +08:00
|
|
|
|
2012-11-23 23:38:49 +08:00
|
|
|
uptr GetPageSizeCached() {
|
|
|
|
static uptr PageSize;
|
|
|
|
if (!PageSize)
|
|
|
|
PageSize = GetPageSize();
|
|
|
|
return PageSize;
|
|
|
|
}
|
|
|
|
|
2013-01-19 00:44:27 +08:00
|
|
|
static bool log_to_file = false; // Set to true by __sanitizer_set_report_path
|
|
|
|
|
|
|
|
// By default, dump to stderr. If |log_to_file| is true and |report_fd_pid|
|
|
|
|
// isn't equal to the current PID, try to obtain file descriptor by opening
|
|
|
|
// file "report_path_prefix.<PID>".
|
2013-05-18 00:17:19 +08:00
|
|
|
fd_t report_fd = kStderrFd;
|
2013-01-19 00:44:27 +08:00
|
|
|
static char report_path_prefix[4096]; // Set via __sanitizer_set_report_path.
|
|
|
|
// PID of process that opened |report_fd|. If a fork() occurs, the PID of the
|
|
|
|
// child thread will be different from |report_fd_pid|.
|
|
|
|
static int report_fd_pid = 0;
|
2012-09-14 12:35:14 +08:00
|
|
|
|
2012-09-11 17:44:48 +08:00
|
|
|
static void (*DieCallback)(void);
|
|
|
|
void SetDieCallback(void (*callback)(void)) {
|
|
|
|
DieCallback = callback;
|
|
|
|
}
|
|
|
|
|
|
|
|
void NORETURN Die() {
|
|
|
|
if (DieCallback) {
|
|
|
|
DieCallback();
|
|
|
|
}
|
2013-02-20 21:54:32 +08:00
|
|
|
internal__exit(1);
|
2012-09-11 17:44:48 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static CheckFailedCallbackType CheckFailedCallback;
|
|
|
|
void SetCheckFailedCallback(CheckFailedCallbackType callback) {
|
|
|
|
CheckFailedCallback = callback;
|
|
|
|
}
|
|
|
|
|
|
|
|
void NORETURN CheckFailed(const char *file, int line, const char *cond,
|
|
|
|
u64 v1, u64 v2) {
|
|
|
|
if (CheckFailedCallback) {
|
|
|
|
CheckFailedCallback(file, line, cond, v1, v2);
|
|
|
|
}
|
2013-01-11 23:07:49 +08:00
|
|
|
Report("Sanitizer CHECK failed: %s:%d %s (%lld, %lld)\n", file, line, cond,
|
|
|
|
v1, v2);
|
2012-09-11 17:44:48 +08:00
|
|
|
Die();
|
|
|
|
}
|
|
|
|
|
2013-05-18 00:17:19 +08:00
|
|
|
void MaybeOpenReportFile() {
|
2013-01-19 00:44:27 +08:00
|
|
|
if (!log_to_file || (report_fd_pid == GetPid())) return;
|
2013-01-20 21:22:06 +08:00
|
|
|
InternalScopedBuffer<char> report_path_full(4096);
|
|
|
|
internal_snprintf(report_path_full.data(), report_path_full.size(),
|
2013-01-19 00:44:27 +08:00
|
|
|
"%s.%d", report_path_prefix, GetPid());
|
2013-05-08 22:43:49 +08:00
|
|
|
uptr openrv = OpenFile(report_path_full.data(), true);
|
|
|
|
if (internal_iserror(openrv)) {
|
2012-11-02 23:18:34 +08:00
|
|
|
report_fd = kStderrFd;
|
2013-01-19 00:44:27 +08:00
|
|
|
log_to_file = false;
|
2013-01-20 21:22:06 +08:00
|
|
|
Report("ERROR: Can't open file: %s\n", report_path_full.data());
|
2012-11-02 23:18:34 +08:00
|
|
|
Die();
|
|
|
|
}
|
2013-01-19 00:44:27 +08:00
|
|
|
if (report_fd != kInvalidFd) {
|
|
|
|
// We're in the child. Close the parent's log.
|
|
|
|
internal_close(report_fd);
|
|
|
|
}
|
2013-05-08 22:43:49 +08:00
|
|
|
report_fd = openrv;
|
2013-01-19 00:44:27 +08:00
|
|
|
report_fd_pid = GetPid();
|
2012-11-02 23:18:34 +08:00
|
|
|
}
|
|
|
|
|
2012-06-06 17:26:25 +08:00
|
|
|
void RawWrite(const char *buffer) {
|
|
|
|
static const char *kRawWriteError = "RawWrite can't output requested buffer!";
|
|
|
|
uptr length = (uptr)internal_strlen(buffer);
|
2012-11-02 23:18:34 +08:00
|
|
|
MaybeOpenReportFile();
|
2012-09-14 12:35:14 +08:00
|
|
|
if (length != internal_write(report_fd, buffer, length)) {
|
|
|
|
internal_write(report_fd, kRawWriteError, internal_strlen(kRawWriteError));
|
2012-06-06 17:26:25 +08:00
|
|
|
Die();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-06-07 13:38:26 +08:00
|
|
|
uptr ReadFileToBuffer(const char *file_name, char **buff,
|
|
|
|
uptr *buff_size, uptr max_len) {
|
2012-11-23 23:38:49 +08:00
|
|
|
uptr PageSize = GetPageSizeCached();
|
|
|
|
uptr kMinFileLen = PageSize;
|
2012-06-07 13:38:26 +08:00
|
|
|
uptr read_len = 0;
|
|
|
|
*buff = 0;
|
|
|
|
*buff_size = 0;
|
|
|
|
// The files we usually open are not seekable, so try different buffer sizes.
|
|
|
|
for (uptr size = kMinFileLen; size <= max_len; size *= 2) {
|
2013-05-08 22:43:49 +08:00
|
|
|
uptr openrv = OpenFile(file_name, /*write*/ false);
|
|
|
|
if (internal_iserror(openrv)) return 0;
|
|
|
|
fd_t fd = openrv;
|
2012-06-07 13:38:26 +08:00
|
|
|
UnmapOrDie(*buff, *buff_size);
|
|
|
|
*buff = (char*)MmapOrDie(size, __FUNCTION__);
|
|
|
|
*buff_size = size;
|
|
|
|
// Read up to one page at a time.
|
|
|
|
read_len = 0;
|
|
|
|
bool reached_eof = false;
|
2012-11-23 23:38:49 +08:00
|
|
|
while (read_len + PageSize <= size) {
|
|
|
|
uptr just_read = internal_read(fd, *buff + read_len, PageSize);
|
2012-06-07 13:38:26 +08:00
|
|
|
if (just_read == 0) {
|
|
|
|
reached_eof = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
read_len += just_read;
|
|
|
|
}
|
|
|
|
internal_close(fd);
|
|
|
|
if (reached_eof) // We've read the whole file.
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
return read_len;
|
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2012-12-06 14:10:31 +08:00
|
|
|
// We want to map a chunk of address space aligned to 'alignment'.
|
|
|
|
// We do it by maping a bit more and then unmaping redundant pieces.
|
|
|
|
// We probably can do it with fewer syscalls in some OS-dependent way.
|
|
|
|
void *MmapAlignedOrDie(uptr size, uptr alignment, const char *mem_type) {
|
2012-12-06 15:43:17 +08:00
|
|
|
// uptr PageSize = GetPageSizeCached();
|
2012-12-06 14:10:31 +08:00
|
|
|
CHECK(IsPowerOfTwo(size));
|
|
|
|
CHECK(IsPowerOfTwo(alignment));
|
|
|
|
uptr map_size = size + alignment;
|
|
|
|
uptr map_res = (uptr)MmapOrDie(map_size, mem_type);
|
|
|
|
uptr map_end = map_res + map_size;
|
|
|
|
uptr res = map_res;
|
|
|
|
if (res & (alignment - 1)) // Not aligned.
|
2012-12-06 23:42:54 +08:00
|
|
|
res = (map_res + alignment) & ~(alignment - 1);
|
2012-12-06 14:10:31 +08:00
|
|
|
uptr end = res + size;
|
|
|
|
if (res != map_res)
|
|
|
|
UnmapOrDie((void*)map_res, res - map_res);
|
|
|
|
if (end != map_end)
|
|
|
|
UnmapOrDie((void*)end, map_end - end);
|
|
|
|
return (void*)res;
|
|
|
|
}
|
|
|
|
|
2013-02-06 20:36:49 +08:00
|
|
|
void ReportErrorSummary(const char *error_type, const char *file,
|
|
|
|
int line, const char *function) {
|
|
|
|
const int kMaxSize = 1024; // We don't want a summary too long.
|
|
|
|
InternalScopedBuffer<char> buff(kMaxSize);
|
2013-02-06 22:24:00 +08:00
|
|
|
internal_snprintf(buff.data(), kMaxSize, "%s: %s %s:%d %s",
|
2013-02-06 20:36:49 +08:00
|
|
|
SanitizerToolName, error_type,
|
2013-02-07 16:04:56 +08:00
|
|
|
file ? file : "??", line, function ? function : "??");
|
2013-02-06 20:36:49 +08:00
|
|
|
__sanitizer_report_error_summary(buff.data());
|
|
|
|
}
|
|
|
|
|
2012-06-06 17:26:25 +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) {
|
|
|
|
if (!path) return;
|
|
|
|
uptr len = internal_strlen(path);
|
2013-01-19 00:44:27 +08:00
|
|
|
if (len > sizeof(report_path_prefix) - 100) {
|
2012-09-14 12:35:14 +08:00
|
|
|
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();
|
|
|
|
}
|
2013-01-19 00:44:27 +08:00
|
|
|
internal_strncpy(report_path_prefix, path, sizeof(report_path_prefix));
|
|
|
|
report_path_prefix[len] = '\0';
|
2012-11-02 17:38:47 +08:00
|
|
|
report_fd = kInvalidFd;
|
2013-01-19 00:44:27 +08:00
|
|
|
log_to_file = true;
|
2012-09-14 12:35:14 +08:00
|
|
|
}
|
2012-11-02 17:23:36 +08:00
|
|
|
|
|
|
|
void __sanitizer_set_report_fd(int fd) {
|
2012-11-02 17:38:47 +08:00
|
|
|
if (report_fd != kStdoutFd &&
|
|
|
|
report_fd != kStderrFd &&
|
|
|
|
report_fd != kInvalidFd)
|
|
|
|
internal_close(report_fd);
|
|
|
|
report_fd = fd;
|
2012-11-02 17:23:36 +08:00
|
|
|
}
|
2012-12-10 21:10:40 +08:00
|
|
|
|
|
|
|
void NOINLINE __sanitizer_sandbox_on_notify(void *reserved) {
|
|
|
|
(void)reserved;
|
|
|
|
PrepareForSandboxing();
|
|
|
|
}
|
2013-02-06 20:36:49 +08:00
|
|
|
|
|
|
|
void __sanitizer_report_error_summary(const char *error_summary) {
|
|
|
|
Printf("SUMMARY: %s\n", error_summary);
|
|
|
|
}
|
2012-11-02 17:23:36 +08:00
|
|
|
} // extern "C"
|