2012-06-06 14:47:26 +08:00
|
|
|
//===-- sanitizer_common.h --------------------------------------*- C++ -*-===//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
2015-02-19 23:15:33 +08:00
|
|
|
// This file is shared between run-time libraries of sanitizers.
|
|
|
|
//
|
2012-06-06 17:26:25 +08:00
|
|
|
// It declares common functions and classes that are used in both runtimes.
|
2012-06-06 14:47:26 +08:00
|
|
|
// Implementation of some functions are provided in sanitizer_common, while
|
|
|
|
// others must be defined by run-time library itself.
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
#ifndef SANITIZER_COMMON_H
|
|
|
|
#define SANITIZER_COMMON_H
|
|
|
|
|
2015-01-09 06:03:05 +08:00
|
|
|
#include "sanitizer_flags.h"
|
2015-02-19 23:15:33 +08:00
|
|
|
#include "sanitizer_interface_internal.h"
|
2012-06-06 14:47:26 +08:00
|
|
|
#include "sanitizer_internal_defs.h"
|
2013-02-26 21:30:27 +08:00
|
|
|
#include "sanitizer_libc.h"
|
2015-01-09 06:03:05 +08:00
|
|
|
#include "sanitizer_list.h"
|
2013-04-05 15:30:29 +08:00
|
|
|
#include "sanitizer_mutex.h"
|
2012-06-06 14:47:26 +08:00
|
|
|
|
2016-04-02 01:09:08 +08:00
|
|
|
#if defined(_MSC_VER) && !defined(__clang__)
|
2015-07-02 10:06:59 +08:00
|
|
|
extern "C" void _ReadWriteBarrier();
|
|
|
|
#pragma intrinsic(_ReadWriteBarrier)
|
|
|
|
#endif
|
|
|
|
|
2012-06-06 14:47:26 +08:00
|
|
|
namespace __sanitizer {
|
2013-02-06 20:36:49 +08:00
|
|
|
struct StackTrace;
|
2015-02-27 10:29:25 +08:00
|
|
|
struct AddressInfo;
|
2012-06-06 14:47:26 +08:00
|
|
|
|
2012-06-06 17:26:25 +08:00
|
|
|
// Constants.
|
2012-11-21 20:38:58 +08:00
|
|
|
const uptr kWordSize = SANITIZER_WORDSIZE / 8;
|
2012-06-06 17:26:25 +08:00
|
|
|
const uptr kWordSizeInBits = 8 * kWordSize;
|
2012-11-24 13:03:11 +08:00
|
|
|
|
2014-05-30 16:52:03 +08:00
|
|
|
#if defined(__powerpc__) || defined(__powerpc64__)
|
|
|
|
const uptr kCacheLineSize = 128;
|
|
|
|
#else
|
|
|
|
const uptr kCacheLineSize = 64;
|
|
|
|
#endif
|
2012-06-06 17:26:25 +08:00
|
|
|
|
2014-11-26 09:48:39 +08:00
|
|
|
const uptr kMaxPathLength = 4096;
|
2013-06-11 16:13:36 +08:00
|
|
|
|
2014-02-04 00:42:29 +08:00
|
|
|
const uptr kMaxThreadStackSize = 1 << 30; // 1Gb
|
|
|
|
|
2015-11-21 02:42:01 +08:00
|
|
|
static const uptr kErrorMessageBufferSize = 1 << 16;
|
|
|
|
|
2015-06-24 21:04:12 +08:00
|
|
|
// Denotes fake PC values that come from JIT/JAVA/etc.
|
|
|
|
// For such PC values __tsan_symbolize_external() will be called.
|
2015-06-24 21:27:56 +08:00
|
|
|
const u64 kExternalPCBit = 1ULL << 60;
|
2015-06-24 21:04:12 +08:00
|
|
|
|
2013-01-31 22:11:21 +08:00
|
|
|
extern const char *SanitizerToolName; // Can be changed by the tool.
|
|
|
|
|
2015-01-20 21:21:20 +08:00
|
|
|
extern atomic_uint32_t current_verbosity;
|
|
|
|
INLINE void SetVerbosity(int verbosity) {
|
|
|
|
atomic_store(¤t_verbosity, verbosity, memory_order_relaxed);
|
|
|
|
}
|
|
|
|
INLINE int Verbosity() {
|
|
|
|
return atomic_load(¤t_verbosity, memory_order_relaxed);
|
|
|
|
}
|
|
|
|
|
2012-11-23 23:38:49 +08:00
|
|
|
uptr GetPageSize();
|
2016-06-27 23:32:18 +08:00
|
|
|
extern uptr PageSizeCached;
|
|
|
|
INLINE uptr GetPageSizeCached() {
|
|
|
|
if (!PageSizeCached)
|
|
|
|
PageSizeCached = GetPageSize();
|
|
|
|
return PageSizeCached;
|
|
|
|
}
|
2012-11-23 23:38:49 +08:00
|
|
|
uptr GetMmapGranularity();
|
2013-07-16 17:47:39 +08:00
|
|
|
uptr GetMaxVirtualAddress();
|
2012-06-07 15:13:46 +08:00
|
|
|
// Threads
|
2012-10-02 20:58:14 +08:00
|
|
|
uptr GetTid();
|
2012-06-15 14:37:34 +08:00
|
|
|
uptr GetThreadSelf();
|
2012-06-07 15:32:00 +08:00
|
|
|
void GetThreadStackTopAndBottom(bool at_initialization, uptr *stack_top,
|
2012-06-07 15:13:46 +08:00
|
|
|
uptr *stack_bottom);
|
2013-05-07 22:41:43 +08:00
|
|
|
void GetThreadStackAndTls(bool main, uptr *stk_addr, uptr *stk_size,
|
|
|
|
uptr *tls_addr, uptr *tls_size);
|
2012-06-07 00:15:07 +08:00
|
|
|
|
|
|
|
// Memory management
|
2015-11-21 02:42:01 +08:00
|
|
|
void *MmapOrDie(uptr size, const char *mem_type, bool raw_report = false);
|
|
|
|
INLINE void *MmapOrDieQuietly(uptr size, const char *mem_type) {
|
|
|
|
return MmapOrDie(size, mem_type, /*raw_report*/ true);
|
|
|
|
}
|
2012-06-06 17:26:25 +08:00
|
|
|
void UnmapOrDie(void *addr, uptr size);
|
2015-05-30 06:31:28 +08:00
|
|
|
void *MmapFixedNoReserve(uptr fixed_addr, uptr size,
|
|
|
|
const char *name = nullptr);
|
2013-12-13 23:03:49 +08:00
|
|
|
void *MmapNoReserveOrDie(uptr size, const char *mem_type);
|
2012-12-13 13:36:00 +08:00
|
|
|
void *MmapFixedOrDie(uptr fixed_addr, uptr size);
|
2016-04-23 07:46:53 +08:00
|
|
|
void *MmapFixedNoAccess(uptr fixed_addr, uptr size, const char *name = nullptr);
|
2016-04-23 08:05:24 +08:00
|
|
|
void *MmapNoAccess(uptr size);
|
2012-12-06 14:10:31 +08:00
|
|
|
// Map aligned chunk of address space; size and alignment are powers of two.
|
|
|
|
void *MmapAlignedOrDie(uptr size, uptr alignment, const char *mem_type);
|
2016-04-23 07:46:53 +08:00
|
|
|
// Disallow access to a memory range. Use MmapFixedNoAccess to allocate an
|
2015-04-10 23:02:19 +08:00
|
|
|
// unaccessible memory.
|
|
|
|
bool MprotectNoAccess(uptr addr, uptr size);
|
2016-01-27 04:53:09 +08:00
|
|
|
bool MprotectReadOnly(uptr addr, uptr size);
|
2015-04-10 23:02:19 +08:00
|
|
|
|
2016-10-01 01:47:34 +08:00
|
|
|
// Find an available address space.
|
|
|
|
uptr FindAvailableMemoryRange(uptr size, uptr alignment, uptr left_padding);
|
|
|
|
|
2012-06-15 15:29:14 +08:00
|
|
|
// Used to check if we can map shadow memory to a fixed location.
|
|
|
|
bool MemoryRangeIsAvailable(uptr range_start, uptr range_end);
|
2016-12-01 04:41:59 +08:00
|
|
|
// Releases memory pages entirely within the [beg, end] address range. Noop if
|
|
|
|
// the provided range does not contain at least one entire page.
|
|
|
|
void ReleaseMemoryPagesToOS(uptr beg, uptr end);
|
2014-04-14 22:51:01 +08:00
|
|
|
void IncreaseTotalMmap(uptr size);
|
|
|
|
void DecreaseTotalMmap(uptr size);
|
2014-12-09 09:22:59 +08:00
|
|
|
uptr GetRSS();
|
2015-01-21 10:05:31 +08:00
|
|
|
void NoHugePagesInRegion(uptr addr, uptr length);
|
2015-02-03 18:15:15 +08:00
|
|
|
void DontDumpShadowMemory(uptr addr, uptr length);
|
2015-09-11 21:55:00 +08:00
|
|
|
// Check if the built VMA size matches the runtime one.
|
|
|
|
void CheckVMASize();
|
2016-06-17 04:06:06 +08:00
|
|
|
void RunMallocHooks(const void *ptr, uptr size);
|
|
|
|
void RunFreeHooks(const void *ptr);
|
2012-06-14 22:42:58 +08:00
|
|
|
|
2012-08-21 16:13:37 +08:00
|
|
|
// InternalScopedBuffer can be used instead of large stack arrays to
|
|
|
|
// keep frame size low.
|
2012-08-29 16:40:36 +08:00
|
|
|
// FIXME: use InternalAlloc instead of MmapOrDie once
|
|
|
|
// InternalAlloc is made libc-free.
|
2016-09-28 06:52:34 +08:00
|
|
|
template <typename T>
|
2012-08-21 16:13:37 +08:00
|
|
|
class InternalScopedBuffer {
|
|
|
|
public:
|
|
|
|
explicit InternalScopedBuffer(uptr cnt) {
|
|
|
|
cnt_ = cnt;
|
2016-09-28 06:52:34 +08:00
|
|
|
ptr_ = (T *)MmapOrDie(cnt * sizeof(T), "InternalScopedBuffer");
|
2012-08-21 16:13:37 +08:00
|
|
|
}
|
2016-09-28 06:52:34 +08:00
|
|
|
~InternalScopedBuffer() { UnmapOrDie(ptr_, cnt_ * sizeof(T)); }
|
2012-08-21 16:13:37 +08:00
|
|
|
T &operator[](uptr i) { return ptr_[i]; }
|
|
|
|
T *data() { return ptr_; }
|
|
|
|
uptr size() { return cnt_ * sizeof(T); }
|
|
|
|
|
|
|
|
private:
|
|
|
|
T *ptr_;
|
|
|
|
uptr cnt_;
|
2016-09-28 06:52:34 +08:00
|
|
|
// Disallow copies and moves.
|
|
|
|
InternalScopedBuffer(const InternalScopedBuffer &) = delete;
|
|
|
|
InternalScopedBuffer &operator=(const InternalScopedBuffer &) = delete;
|
|
|
|
InternalScopedBuffer(InternalScopedBuffer &&) = delete;
|
|
|
|
InternalScopedBuffer &operator=(InternalScopedBuffer &&) = delete;
|
2012-08-21 16:13:37 +08:00
|
|
|
};
|
|
|
|
|
2013-11-14 17:41:24 +08:00
|
|
|
class InternalScopedString : public InternalScopedBuffer<char> {
|
|
|
|
public:
|
|
|
|
explicit InternalScopedString(uptr max_length)
|
|
|
|
: InternalScopedBuffer<char>(max_length), length_(0) {
|
|
|
|
(*this)[0] = '\0';
|
|
|
|
}
|
|
|
|
uptr length() { return length_; }
|
|
|
|
void clear() {
|
|
|
|
(*this)[0] = '\0';
|
|
|
|
length_ = 0;
|
|
|
|
}
|
|
|
|
void append(const char *format, ...);
|
|
|
|
|
|
|
|
private:
|
|
|
|
uptr length_;
|
|
|
|
};
|
|
|
|
|
2012-08-27 22:51:36 +08:00
|
|
|
// Simple low-level (mmap-based) allocator for internal use. Doesn't have
|
|
|
|
// constructor, so all instances of LowLevelAllocator should be
|
|
|
|
// linker initialized.
|
2012-08-27 17:30:58 +08:00
|
|
|
class LowLevelAllocator {
|
|
|
|
public:
|
2012-08-27 22:51:36 +08:00
|
|
|
// Requires an external lock.
|
2012-08-27 17:30:58 +08:00
|
|
|
void *Allocate(uptr size);
|
|
|
|
private:
|
|
|
|
char *allocated_end_;
|
|
|
|
char *allocated_current_;
|
|
|
|
};
|
|
|
|
typedef void (*LowLevelAllocateCallback)(uptr ptr, uptr size);
|
|
|
|
// Allows to register tool-specific callbacks for LowLevelAllocator.
|
|
|
|
// Passing NULL removes the callback.
|
|
|
|
void SetLowLevelAllocateCallback(LowLevelAllocateCallback callback);
|
|
|
|
|
2012-06-14 22:42:58 +08:00
|
|
|
// IO
|
2012-06-07 15:13:46 +08:00
|
|
|
void RawWrite(const char *buffer);
|
2014-02-26 17:06:59 +08:00
|
|
|
bool ColorizeReports();
|
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
|
|
|
void RemoveANSIEscapeSequencesFromString(char *buffer);
|
2012-06-06 21:58:39 +08:00
|
|
|
void Printf(const char *format, ...);
|
|
|
|
void Report(const char *format, ...);
|
2012-08-28 19:34:40 +08:00
|
|
|
void SetPrintfAndReportCallback(void (*callback)(const char *));
|
2013-12-05 20:04:51 +08:00
|
|
|
#define VReport(level, ...) \
|
|
|
|
do { \
|
2015-01-20 21:21:20 +08:00
|
|
|
if ((uptr)Verbosity() >= (level)) Report(__VA_ARGS__); \
|
2013-12-05 20:04:51 +08:00
|
|
|
} while (0)
|
|
|
|
#define VPrintf(level, ...) \
|
|
|
|
do { \
|
2015-01-20 21:21:20 +08:00
|
|
|
if ((uptr)Verbosity() >= (level)) Printf(__VA_ARGS__); \
|
2013-12-05 20:04:51 +08:00
|
|
|
} while (0)
|
2013-11-14 17:41:24 +08:00
|
|
|
|
2013-04-05 15:30:29 +08:00
|
|
|
// Can be used to prevent mixing error reports from different sanitizers.
|
|
|
|
extern StaticSpinMutex CommonSanitizerReportMutex;
|
2014-12-12 02:30:25 +08:00
|
|
|
|
|
|
|
struct ReportFile {
|
|
|
|
void Write(const char *buffer, uptr length);
|
2015-04-09 01:42:57 +08:00
|
|
|
bool SupportsColors();
|
2014-12-12 02:30:25 +08:00
|
|
|
void SetReportPath(const char *path);
|
|
|
|
|
|
|
|
// Don't use fields directly. They are only declared public to allow
|
|
|
|
// aggregate initialization.
|
|
|
|
|
|
|
|
// Protects fields below.
|
|
|
|
StaticSpinMutex *mu;
|
|
|
|
// Opened file descriptor. Defaults to stderr. It may be equal to
|
|
|
|
// kInvalidFd, in which case new file will be opened when necessary.
|
|
|
|
fd_t fd;
|
|
|
|
// Path prefix of report file, set via __sanitizer_set_report_path.
|
|
|
|
char path_prefix[kMaxPathLength];
|
|
|
|
// Full path to report, obtained as <path_prefix>.PID
|
|
|
|
char full_path[kMaxPathLength];
|
|
|
|
// PID of the process that opened fd. If a fork() occurs,
|
|
|
|
// the PID of child will be different from fd_pid.
|
|
|
|
uptr fd_pid;
|
|
|
|
|
|
|
|
private:
|
|
|
|
void ReopenIfNecessary();
|
|
|
|
};
|
|
|
|
extern ReportFile report_file;
|
|
|
|
|
2013-12-04 22:37:01 +08:00
|
|
|
extern uptr stoptheworld_tracer_pid;
|
|
|
|
extern uptr stoptheworld_tracer_ppid;
|
2012-06-06 21:11:29 +08:00
|
|
|
|
2015-03-23 18:10:46 +08:00
|
|
|
enum FileAccessMode {
|
|
|
|
RdOnly,
|
|
|
|
WrOnly,
|
|
|
|
RdWr
|
|
|
|
};
|
|
|
|
|
2015-04-09 00:03:22 +08:00
|
|
|
// Returns kInvalidFd on error.
|
|
|
|
fd_t OpenFile(const char *filename, FileAccessMode mode,
|
|
|
|
error_t *errno_p = nullptr);
|
2015-04-09 20:37:05 +08:00
|
|
|
void CloseFile(fd_t);
|
2015-04-09 22:11:25 +08:00
|
|
|
|
|
|
|
// Return true on success, false on error.
|
2015-04-09 21:38:14 +08:00
|
|
|
bool ReadFromFile(fd_t fd, void *buff, uptr buff_size,
|
|
|
|
uptr *bytes_read = nullptr, error_t *error_p = nullptr);
|
2015-04-09 22:11:25 +08:00
|
|
|
bool WriteToFile(fd_t fd, const void *buff, uptr buff_size,
|
|
|
|
uptr *bytes_written = nullptr, error_t *error_p = nullptr);
|
|
|
|
|
2015-04-09 22:45:17 +08:00
|
|
|
bool RenameFile(const char *oldpath, const char *newpath,
|
|
|
|
error_t *error_p = nullptr);
|
|
|
|
|
2015-08-04 03:51:18 +08:00
|
|
|
// Scoped file handle closer.
|
|
|
|
struct FileCloser {
|
|
|
|
explicit FileCloser(fd_t fd) : fd(fd) {}
|
|
|
|
~FileCloser() { CloseFile(fd); }
|
|
|
|
fd_t fd;
|
|
|
|
};
|
|
|
|
|
2015-04-09 01:42:57 +08:00
|
|
|
bool SupportsColoredOutput(fd_t fd);
|
|
|
|
|
2012-06-07 13:38:26 +08:00
|
|
|
// Opens the file 'file_name" and reads up to 'max_len' bytes.
|
|
|
|
// The resulting buffer is mmaped and stored in '*buff'.
|
2015-07-18 07:50:08 +08:00
|
|
|
// The size of the mmaped region is stored in '*buff_size'.
|
|
|
|
// The total number of read bytes is stored in '*read_len'.
|
|
|
|
// Returns true if file was successfully opened and read.
|
|
|
|
bool ReadFileToBuffer(const char *file_name, char **buff, uptr *buff_size,
|
|
|
|
uptr *read_len, uptr max_len = 1 << 26,
|
|
|
|
error_t *errno_p = nullptr);
|
2012-07-03 16:24:14 +08:00
|
|
|
// Maps given file to virtual memory, and returns pointer to it
|
2015-04-09 00:03:22 +08:00
|
|
|
// (or NULL if mapping fails). Stores the size of mmaped region
|
2012-07-03 16:24:14 +08:00
|
|
|
// in '*buff_size'.
|
|
|
|
void *MapFileToMemory(const char *file_name, uptr *buff_size);
|
2015-07-31 19:29:25 +08:00
|
|
|
void *MapWritableFileToMemory(void *addr, uptr size, fd_t fd, OFF_T offset);
|
2012-07-03 16:24:14 +08:00
|
|
|
|
2014-09-18 01:56:15 +08:00
|
|
|
bool IsAccessibleMemoryRange(uptr beg, uptr size);
|
|
|
|
|
2013-10-04 16:55:03 +08:00
|
|
|
// Error report formatting.
|
|
|
|
const char *StripPathPrefix(const char *filepath,
|
|
|
|
const char *strip_file_prefix);
|
2014-11-05 03:34:29 +08:00
|
|
|
// Strip the directories from the module name.
|
|
|
|
const char *StripModuleName(const char *module);
|
2013-10-04 16:55:03 +08:00
|
|
|
|
2012-09-17 17:12:39 +08:00
|
|
|
// OS
|
2015-06-04 15:29:43 +08:00
|
|
|
uptr ReadBinaryName(/*out*/char *buf, uptr buf_len);
|
2015-06-29 23:58:16 +08:00
|
|
|
uptr ReadBinaryNameCached(/*out*/char *buf, uptr buf_len);
|
2015-07-29 05:01:42 +08:00
|
|
|
uptr ReadLongProcessName(/*out*/ char *buf, uptr buf_len);
|
2015-07-29 04:27:51 +08:00
|
|
|
const char *GetProcessName();
|
|
|
|
void UpdateProcessName();
|
2015-06-04 15:29:43 +08:00
|
|
|
void CacheBinaryName();
|
2014-08-13 06:31:19 +08:00
|
|
|
void DisableCoreDumperIfNecessary();
|
2012-09-17 17:12:39 +08:00
|
|
|
void DumpProcessMap();
|
2012-11-09 22:45:30 +08:00
|
|
|
bool FileExists(const char *filename);
|
2012-06-14 22:07:21 +08:00
|
|
|
const char *GetEnv(const char *name);
|
2015-01-31 09:27:18 +08:00
|
|
|
bool SetEnv(const char *name, const char *value);
|
2012-06-18 16:44:30 +08:00
|
|
|
const char *GetPwd();
|
2013-09-03 21:20:48 +08:00
|
|
|
char *FindPathToBinary(const char *name);
|
2015-02-27 11:12:19 +08:00
|
|
|
bool IsPathSeparator(const char c);
|
|
|
|
bool IsAbsolutePath(const char *path);
|
2016-01-27 04:10:01 +08:00
|
|
|
// Starts a subprocess and returs its pid.
|
|
|
|
// If *_fd parameters are not kInvalidFd their corresponding input/output
|
|
|
|
// streams will be redirect to the file. The files will always be closed
|
|
|
|
// in parent process even in case of an error.
|
|
|
|
// The child process will close all fds after STDERR_FILENO
|
|
|
|
// before passing control to a program.
|
|
|
|
pid_t StartSubprocess(const char *filename, const char *const argv[],
|
|
|
|
fd_t stdin_fd = kInvalidFd, fd_t stdout_fd = kInvalidFd,
|
|
|
|
fd_t stderr_fd = kInvalidFd);
|
|
|
|
// Checks if specified process is still running
|
|
|
|
bool IsProcessRunning(pid_t pid);
|
2016-01-28 07:51:36 +08:00
|
|
|
// Waits for the process to finish and returns its exit code.
|
|
|
|
// Returns -1 in case of an error.
|
|
|
|
int WaitForProcess(pid_t pid);
|
2015-02-27 11:12:19 +08:00
|
|
|
|
2013-02-18 15:17:12 +08:00
|
|
|
u32 GetUid();
|
2012-09-17 17:12:39 +08:00
|
|
|
void ReExec();
|
2016-01-18 15:55:12 +08:00
|
|
|
char **GetArgv();
|
|
|
|
void PrintCmdline();
|
2012-09-17 17:12:39 +08:00
|
|
|
bool StackSizeIsUnlimited();
|
2016-05-28 08:25:16 +08:00
|
|
|
uptr GetStackSizeLimitInBytes();
|
2012-09-17 17:12:39 +08:00
|
|
|
void SetStackSizeLimitInBytes(uptr limit);
|
2014-08-13 06:31:19 +08:00
|
|
|
bool AddressSpaceIsUnlimited();
|
|
|
|
void SetAddressSpaceUnlimited();
|
2014-02-24 16:53:26 +08:00
|
|
|
void AdjustStackSize(void *attr);
|
2014-05-19 20:53:03 +08:00
|
|
|
void PrepareForSandboxing(__sanitizer_sandbox_arguments *args);
|
|
|
|
void CovPrepareForSandboxing(__sanitizer_sandbox_arguments *args);
|
2014-04-24 21:09:17 +08:00
|
|
|
void SetSandboxingCallback(void (*f)());
|
2012-06-07 13:38:26 +08:00
|
|
|
|
2014-12-26 20:32:32 +08:00
|
|
|
void CoverageUpdateMapping();
|
2014-06-04 20:13:54 +08:00
|
|
|
void CovBeforeFork();
|
|
|
|
void CovAfterFork(int child_pid);
|
2014-05-27 20:37:52 +08:00
|
|
|
|
2014-12-26 20:32:32 +08:00
|
|
|
void InitializeCoverage(bool enabled, const char *coverage_dir);
|
|
|
|
void ReInitializeCoverage(bool enabled, const char *coverage_dir);
|
|
|
|
|
2013-03-13 16:19:53 +08:00
|
|
|
void InitTlsSize();
|
|
|
|
uptr GetTlsSize();
|
|
|
|
|
2012-06-15 14:08:19 +08:00
|
|
|
// Other
|
2012-06-15 14:37:34 +08:00
|
|
|
void SleepForSeconds(int seconds);
|
2012-06-18 16:44:30 +08:00
|
|
|
void SleepForMillis(int millis);
|
2013-03-21 14:24:31 +08:00
|
|
|
u64 NanoTime();
|
2012-06-15 14:37:34 +08:00
|
|
|
int Atexit(void (*function)(void));
|
2012-06-15 15:00:31 +08:00
|
|
|
void SortArray(uptr *array, uptr size);
|
2016-08-27 07:58:42 +08:00
|
|
|
void SortArray(u32 *array, uptr size);
|
2015-02-20 06:56:47 +08:00
|
|
|
bool TemplateMatch(const char *templ, const char *str);
|
2012-06-15 14:08:19 +08:00
|
|
|
|
2012-09-11 17:44:48 +08:00
|
|
|
// Exit
|
|
|
|
void NORETURN Abort();
|
|
|
|
void NORETURN Die();
|
2013-08-13 19:42:45 +08:00
|
|
|
void NORETURN
|
2012-09-11 17:44:48 +08:00
|
|
|
CheckFailed(const char *file, int line, const char *cond, u64 v1, u64 v2);
|
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 = false);
|
2012-09-11 17:44:48 +08:00
|
|
|
|
2012-12-07 19:27:24 +08:00
|
|
|
// Set the name of the current thread to 'name', return true on succees.
|
|
|
|
// The name may be truncated to a system-dependent limit.
|
|
|
|
bool SanitizerSetThreadName(const char *name);
|
|
|
|
// Get the name of the current thread (no more than max_len bytes),
|
|
|
|
// return true on succees. name should have space for at least max_len+1 bytes.
|
|
|
|
bool SanitizerGetThreadName(char *name, int max_len);
|
|
|
|
|
2012-09-11 17:44:48 +08:00
|
|
|
// Specific tools may override behavior of "Die" and "CheckFailed" functions
|
|
|
|
// to do tool-specific job.
|
2013-08-26 21:20:31 +08:00
|
|
|
typedef void (*DieCallbackType)(void);
|
2015-08-25 06:21:44 +08:00
|
|
|
|
|
|
|
// It's possible to add several callbacks that would be run when "Die" is
|
|
|
|
// called. The callbacks will be run in the opposite order. The tools are
|
|
|
|
// strongly recommended to setup all callbacks during initialization, when there
|
|
|
|
// is only a single thread.
|
|
|
|
bool AddDieCallback(DieCallbackType callback);
|
|
|
|
bool RemoveDieCallback(DieCallbackType callback);
|
|
|
|
|
|
|
|
void SetUserDieCallback(DieCallbackType callback);
|
|
|
|
|
2012-09-11 17:44:48 +08:00
|
|
|
typedef void (*CheckFailedCallbackType)(const char *, int, const char *,
|
|
|
|
u64, u64);
|
|
|
|
void SetCheckFailedCallback(CheckFailedCallbackType callback);
|
|
|
|
|
2015-01-07 07:53:32 +08:00
|
|
|
// Callback will be called if soft_rss_limit_mb is given and the limit is
|
|
|
|
// exceeded (exceeded==true) or if rss went down below the limit
|
|
|
|
// (exceeded==false).
|
|
|
|
// The callback should be registered once at the tool init time.
|
|
|
|
void SetSoftRssLimitExceededCallback(void (*Callback)(bool exceeded));
|
|
|
|
|
2014-01-28 19:12:29 +08:00
|
|
|
// Functions related to signal handling.
|
2014-01-31 21:10:07 +08:00
|
|
|
typedef void (*SignalHandlerType)(int, void *, void *);
|
2016-02-02 10:01:17 +08:00
|
|
|
bool IsHandledDeadlySignal(int signum);
|
2014-01-31 21:10:07 +08:00
|
|
|
void InstallDeadlySignalHandlers(SignalHandlerType handler);
|
|
|
|
// Alternative signal stack (POSIX-only).
|
2014-01-28 19:12:29 +08:00
|
|
|
void SetAlternateSignalStack();
|
|
|
|
void UnsetAlternateSignalStack();
|
|
|
|
|
2013-11-02 01:02:14 +08:00
|
|
|
// We don't want a summary too long.
|
|
|
|
const int kMaxSummaryLength = 1024;
|
|
|
|
// Construct a one-line string:
|
|
|
|
// SUMMARY: SanitizerToolName: error_message
|
|
|
|
// and pass it to __sanitizer_report_error_summary.
|
|
|
|
void ReportErrorSummary(const char *error_message);
|
|
|
|
// Same as above, but construct error_message as:
|
2015-02-27 10:29:25 +08:00
|
|
|
// error_type file:line[:column][ function]
|
|
|
|
void ReportErrorSummary(const char *error_type, const AddressInfo &info);
|
|
|
|
// Same as above, but obtains AddressInfo by symbolizing top stack trace frame.
|
2016-09-13 01:10:44 +08:00
|
|
|
void ReportErrorSummary(const char *error_type, const StackTrace *trace);
|
2013-02-06 20:36:49 +08:00
|
|
|
|
2012-06-15 15:00:31 +08:00
|
|
|
// Math
|
2013-06-10 18:02:02 +08:00
|
|
|
#if SANITIZER_WINDOWS && !defined(__clang__) && !defined(__GNUC__)
|
2013-02-08 19:45:04 +08:00
|
|
|
extern "C" {
|
|
|
|
unsigned char _BitScanForward(unsigned long *index, unsigned long mask); // NOLINT
|
|
|
|
unsigned char _BitScanReverse(unsigned long *index, unsigned long mask); // NOLINT
|
|
|
|
#if defined(_WIN64)
|
|
|
|
unsigned char _BitScanForward64(unsigned long *index, unsigned __int64 mask); // NOLINT
|
|
|
|
unsigned char _BitScanReverse64(unsigned long *index, unsigned __int64 mask); // NOLINT
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
INLINE uptr MostSignificantSetBitIndex(uptr x) {
|
2013-02-26 20:59:06 +08:00
|
|
|
CHECK_NE(x, 0U);
|
2013-02-08 19:45:04 +08:00
|
|
|
unsigned long up; // NOLINT
|
2013-06-10 18:02:02 +08:00
|
|
|
#if !SANITIZER_WINDOWS || defined(__clang__) || defined(__GNUC__)
|
2015-07-13 08:26:03 +08:00
|
|
|
# ifdef _WIN64
|
|
|
|
up = SANITIZER_WORDSIZE - 1 - __builtin_clzll(x);
|
|
|
|
# else
|
2013-02-08 19:45:04 +08:00
|
|
|
up = SANITIZER_WORDSIZE - 1 - __builtin_clzl(x);
|
2015-07-13 08:26:03 +08:00
|
|
|
# endif
|
2013-02-08 19:45:04 +08:00
|
|
|
#elif defined(_WIN64)
|
|
|
|
_BitScanReverse64(&up, x);
|
|
|
|
#else
|
|
|
|
_BitScanReverse(&up, x);
|
|
|
|
#endif
|
|
|
|
return up;
|
|
|
|
}
|
|
|
|
|
2014-02-13 23:59:00 +08:00
|
|
|
INLINE uptr LeastSignificantSetBitIndex(uptr x) {
|
|
|
|
CHECK_NE(x, 0U);
|
|
|
|
unsigned long up; // NOLINT
|
|
|
|
#if !SANITIZER_WINDOWS || defined(__clang__) || defined(__GNUC__)
|
2015-07-13 08:26:03 +08:00
|
|
|
# ifdef _WIN64
|
|
|
|
up = __builtin_ctzll(x);
|
|
|
|
# else
|
2014-02-13 23:59:00 +08:00
|
|
|
up = __builtin_ctzl(x);
|
2015-07-13 08:26:03 +08:00
|
|
|
# endif
|
2014-02-13 23:59:00 +08:00
|
|
|
#elif defined(_WIN64)
|
|
|
|
_BitScanForward64(&up, x);
|
|
|
|
#else
|
|
|
|
_BitScanForward(&up, x);
|
|
|
|
#endif
|
|
|
|
return up;
|
|
|
|
}
|
|
|
|
|
2012-07-06 00:18:28 +08:00
|
|
|
INLINE bool IsPowerOfTwo(uptr x) {
|
2012-06-06 17:26:25 +08:00
|
|
|
return (x & (x - 1)) == 0;
|
|
|
|
}
|
2013-02-08 19:45:04 +08:00
|
|
|
|
|
|
|
INLINE uptr RoundUpToPowerOfTwo(uptr size) {
|
|
|
|
CHECK(size);
|
|
|
|
if (IsPowerOfTwo(size)) return size;
|
|
|
|
|
|
|
|
uptr up = MostSignificantSetBitIndex(size);
|
2016-09-13 04:39:13 +08:00
|
|
|
CHECK_LT(size, (1ULL << (up + 1)));
|
|
|
|
CHECK_GT(size, (1ULL << up));
|
2015-07-02 09:44:34 +08:00
|
|
|
return 1ULL << (up + 1);
|
2013-02-08 19:45:04 +08:00
|
|
|
}
|
|
|
|
|
2012-07-06 00:18:28 +08:00
|
|
|
INLINE uptr RoundUpTo(uptr size, uptr boundary) {
|
2015-11-21 02:42:01 +08:00
|
|
|
RAW_CHECK(IsPowerOfTwo(boundary));
|
2012-06-06 17:26:25 +08:00
|
|
|
return (size + boundary - 1) & ~(boundary - 1);
|
|
|
|
}
|
2013-02-08 19:45:04 +08:00
|
|
|
|
2012-12-14 20:15:09 +08:00
|
|
|
INLINE uptr RoundDownTo(uptr x, uptr boundary) {
|
|
|
|
return x & ~(boundary - 1);
|
|
|
|
}
|
2013-02-08 19:45:04 +08:00
|
|
|
|
2012-12-11 22:41:31 +08:00
|
|
|
INLINE bool IsAligned(uptr a, uptr alignment) {
|
|
|
|
return (a & (alignment - 1)) == 0;
|
|
|
|
}
|
2013-02-08 19:45:04 +08:00
|
|
|
|
|
|
|
INLINE uptr Log2(uptr x) {
|
|
|
|
CHECK(IsPowerOfTwo(x));
|
2015-07-13 08:26:03 +08:00
|
|
|
return LeastSignificantSetBitIndex(x);
|
2013-02-08 19:45:04 +08:00
|
|
|
}
|
|
|
|
|
2012-07-16 19:27:17 +08:00
|
|
|
// Don't use std::min, std::max or std::swap, to minimize dependency
|
|
|
|
// on libstdc++.
|
2012-06-15 15:00:31 +08:00
|
|
|
template<class T> T Min(T a, T b) { return a < b ? a : b; }
|
|
|
|
template<class T> T Max(T a, T b) { return a > b ? a : b; }
|
2012-07-16 19:27:17 +08:00
|
|
|
template<class T> void Swap(T& a, T& b) {
|
|
|
|
T tmp = a;
|
|
|
|
a = b;
|
|
|
|
b = tmp;
|
|
|
|
}
|
2012-06-06 14:47:26 +08:00
|
|
|
|
2012-06-15 21:09:52 +08:00
|
|
|
// Char handling
|
2012-07-06 00:18:28 +08:00
|
|
|
INLINE bool IsSpace(int c) {
|
2012-06-15 21:09:52 +08:00
|
|
|
return (c == ' ') || (c == '\n') || (c == '\t') ||
|
|
|
|
(c == '\f') || (c == '\r') || (c == '\v');
|
|
|
|
}
|
2012-07-06 00:18:28 +08:00
|
|
|
INLINE bool IsDigit(int c) {
|
2012-06-15 21:09:52 +08:00
|
|
|
return (c >= '0') && (c <= '9');
|
|
|
|
}
|
2012-07-06 00:18:28 +08:00
|
|
|
INLINE int ToLower(int c) {
|
2012-06-15 21:09:52 +08:00
|
|
|
return (c >= 'A' && c <= 'Z') ? (c + 'a' - 'A') : c;
|
|
|
|
}
|
|
|
|
|
2013-02-26 21:30:27 +08:00
|
|
|
// A low-level vector based on mmap. May incur a significant memory overhead for
|
|
|
|
// small vectors.
|
|
|
|
// WARNING: The current implementation supports only POD types.
|
|
|
|
template<typename T>
|
2014-12-31 07:16:12 +08:00
|
|
|
class InternalMmapVectorNoCtor {
|
2013-02-26 21:30:27 +08:00
|
|
|
public:
|
2014-12-31 07:16:12 +08:00
|
|
|
void Initialize(uptr initial_capacity) {
|
2013-12-03 17:21:08 +08:00
|
|
|
capacity_ = Max(initial_capacity, (uptr)1);
|
2013-02-26 21:30:27 +08:00
|
|
|
size_ = 0;
|
2014-12-31 07:16:12 +08:00
|
|
|
data_ = (T *)MmapOrDie(capacity_ * sizeof(T), "InternalMmapVectorNoCtor");
|
2013-02-26 21:30:27 +08:00
|
|
|
}
|
2014-12-31 07:16:12 +08:00
|
|
|
void Destroy() {
|
2013-02-26 21:30:27 +08:00
|
|
|
UnmapOrDie(data_, capacity_ * sizeof(T));
|
|
|
|
}
|
|
|
|
T &operator[](uptr i) {
|
2013-03-29 16:03:01 +08:00
|
|
|
CHECK_LT(i, size_);
|
2013-03-28 23:37:11 +08:00
|
|
|
return data_[i];
|
|
|
|
}
|
|
|
|
const T &operator[](uptr i) const {
|
2013-03-29 16:03:01 +08:00
|
|
|
CHECK_LT(i, size_);
|
2013-02-26 21:30:27 +08:00
|
|
|
return data_[i];
|
|
|
|
}
|
|
|
|
void push_back(const T &element) {
|
|
|
|
CHECK_LE(size_, capacity_);
|
|
|
|
if (size_ == capacity_) {
|
|
|
|
uptr new_capacity = RoundUpToPowerOfTwo(size_ + 1);
|
|
|
|
Resize(new_capacity);
|
|
|
|
}
|
2016-02-26 02:12:30 +08:00
|
|
|
internal_memcpy(&data_[size_++], &element, sizeof(T));
|
2013-02-26 21:30:27 +08:00
|
|
|
}
|
2013-03-05 19:58:25 +08:00
|
|
|
T &back() {
|
|
|
|
CHECK_GT(size_, 0);
|
|
|
|
return data_[size_ - 1];
|
|
|
|
}
|
|
|
|
void pop_back() {
|
|
|
|
CHECK_GT(size_, 0);
|
|
|
|
size_--;
|
|
|
|
}
|
2013-03-28 23:37:11 +08:00
|
|
|
uptr size() const {
|
2013-02-26 21:30:27 +08:00
|
|
|
return size_;
|
|
|
|
}
|
2013-04-01 21:55:34 +08:00
|
|
|
const T *data() const {
|
|
|
|
return data_;
|
|
|
|
}
|
2015-03-18 08:23:44 +08:00
|
|
|
T *data() {
|
|
|
|
return data_;
|
|
|
|
}
|
2013-04-01 21:55:34 +08:00
|
|
|
uptr capacity() const {
|
|
|
|
return capacity_;
|
|
|
|
}
|
2016-12-13 07:45:38 +08:00
|
|
|
void resize(uptr new_size) {
|
|
|
|
Resize(new_size);
|
|
|
|
if (new_size > size_) {
|
|
|
|
internal_memset(&data_[size_], 0, sizeof(T) * (new_size - size_));
|
|
|
|
}
|
|
|
|
size_ = new_size;
|
|
|
|
}
|
2013-02-26 21:30:27 +08:00
|
|
|
|
2013-11-15 15:18:15 +08:00
|
|
|
void clear() { size_ = 0; }
|
2015-03-05 07:41:55 +08:00
|
|
|
bool empty() const { return size() == 0; }
|
2013-11-15 15:18:15 +08:00
|
|
|
|
2016-01-16 08:31:29 +08:00
|
|
|
const T *begin() const {
|
|
|
|
return data();
|
|
|
|
}
|
|
|
|
T *begin() {
|
|
|
|
return data();
|
|
|
|
}
|
|
|
|
const T *end() const {
|
|
|
|
return data() + size();
|
|
|
|
}
|
|
|
|
T *end() {
|
|
|
|
return data() + size();
|
|
|
|
}
|
|
|
|
|
2013-02-26 21:30:27 +08:00
|
|
|
private:
|
|
|
|
void Resize(uptr new_capacity) {
|
|
|
|
CHECK_GT(new_capacity, 0);
|
|
|
|
CHECK_LE(size_, new_capacity);
|
|
|
|
T *new_data = (T *)MmapOrDie(new_capacity * sizeof(T),
|
2013-06-14 17:59:40 +08:00
|
|
|
"InternalMmapVector");
|
2013-02-26 21:30:27 +08:00
|
|
|
internal_memcpy(new_data, data_, size_ * sizeof(T));
|
|
|
|
T *old_data = data_;
|
|
|
|
data_ = new_data;
|
|
|
|
UnmapOrDie(old_data, capacity_ * sizeof(T));
|
|
|
|
capacity_ = new_capacity;
|
|
|
|
}
|
|
|
|
|
|
|
|
T *data_;
|
|
|
|
uptr capacity_;
|
|
|
|
uptr size_;
|
|
|
|
};
|
2013-05-13 19:58:48 +08:00
|
|
|
|
2014-12-31 07:16:12 +08:00
|
|
|
template<typename T>
|
|
|
|
class InternalMmapVector : public InternalMmapVectorNoCtor<T> {
|
|
|
|
public:
|
|
|
|
explicit InternalMmapVector(uptr initial_capacity) {
|
|
|
|
InternalMmapVectorNoCtor<T>::Initialize(initial_capacity);
|
|
|
|
}
|
|
|
|
~InternalMmapVector() { InternalMmapVectorNoCtor<T>::Destroy(); }
|
|
|
|
// Disallow evil constructors.
|
|
|
|
InternalMmapVector(const InternalMmapVector&);
|
|
|
|
void operator=(const InternalMmapVector&);
|
|
|
|
};
|
|
|
|
|
2013-06-14 17:59:40 +08:00
|
|
|
// HeapSort for arrays and InternalMmapVector.
|
2013-05-13 19:58:48 +08:00
|
|
|
template<class Container, class Compare>
|
|
|
|
void InternalSort(Container *v, uptr size, Compare comp) {
|
|
|
|
if (size < 2)
|
|
|
|
return;
|
|
|
|
// Stage 1: insert elements to the heap.
|
|
|
|
for (uptr i = 1; i < size; i++) {
|
|
|
|
uptr j, p;
|
|
|
|
for (j = i; j > 0; j = p) {
|
|
|
|
p = (j - 1) / 2;
|
|
|
|
if (comp((*v)[p], (*v)[j]))
|
|
|
|
Swap((*v)[j], (*v)[p]);
|
|
|
|
else
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// Stage 2: swap largest element with the last one,
|
|
|
|
// and sink the new top.
|
|
|
|
for (uptr i = size - 1; i > 0; i--) {
|
|
|
|
Swap((*v)[0], (*v)[i]);
|
|
|
|
uptr j, max_ind;
|
|
|
|
for (j = 0; j < i; j = max_ind) {
|
|
|
|
uptr left = 2 * j + 1;
|
|
|
|
uptr right = 2 * j + 2;
|
|
|
|
max_ind = j;
|
|
|
|
if (left < i && comp((*v)[max_ind], (*v)[left]))
|
|
|
|
max_ind = left;
|
|
|
|
if (right < i && comp((*v)[max_ind], (*v)[right]))
|
|
|
|
max_ind = right;
|
|
|
|
if (max_ind != j)
|
|
|
|
Swap((*v)[j], (*v)[max_ind]);
|
|
|
|
else
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-11-16 12:03:27 +08:00
|
|
|
// Works like std::lower_bound: finds the first element that is not less
|
|
|
|
// than the val.
|
2016-11-19 04:48:52 +08:00
|
|
|
template <class Container, class Value, class Compare>
|
|
|
|
uptr InternalLowerBound(const Container &v, uptr first, uptr last,
|
|
|
|
const Value &val, Compare comp) {
|
2016-11-16 12:03:27 +08:00
|
|
|
while (last > first) {
|
2013-08-26 21:24:43 +08:00
|
|
|
uptr mid = (first + last) / 2;
|
|
|
|
if (comp(v[mid], val))
|
|
|
|
first = mid + 1;
|
|
|
|
else
|
2016-11-16 12:03:27 +08:00
|
|
|
last = mid;
|
2013-08-26 21:24:43 +08:00
|
|
|
}
|
2016-11-16 12:03:27 +08:00
|
|
|
return first;
|
2013-08-26 21:24:43 +08:00
|
|
|
}
|
|
|
|
|
2016-12-03 05:27:14 +08:00
|
|
|
enum ModuleArch {
|
|
|
|
kModuleArchUnknown,
|
|
|
|
kModuleArchI386,
|
|
|
|
kModuleArchX86_64,
|
|
|
|
kModuleArchX86_64H,
|
|
|
|
kModuleArchARMV6,
|
|
|
|
kModuleArchARMV7,
|
|
|
|
kModuleArchARMV7S,
|
|
|
|
kModuleArchARMV7K,
|
|
|
|
kModuleArchARM64
|
|
|
|
};
|
|
|
|
|
|
|
|
const uptr kModuleUUIDSize = 16;
|
|
|
|
|
2013-09-10 22:36:16 +08:00
|
|
|
// Represents a binary loaded into virtual memory (e.g. this can be an
|
|
|
|
// executable or a shared object).
|
|
|
|
class LoadedModule {
|
|
|
|
public:
|
2016-12-03 05:27:14 +08:00
|
|
|
LoadedModule()
|
|
|
|
: full_name_(nullptr), base_address_(0), arch_(kModuleArchUnknown) {
|
|
|
|
internal_memset(uuid_, 0, kModuleUUIDSize);
|
|
|
|
ranges_.clear();
|
|
|
|
}
|
2015-04-06 20:49:30 +08:00
|
|
|
void set(const char *module_name, uptr base_address);
|
2016-12-03 05:27:14 +08:00
|
|
|
void set(const char *module_name, uptr base_address, ModuleArch arch,
|
|
|
|
u8 uuid[kModuleUUIDSize]);
|
2015-01-09 06:31:14 +08:00
|
|
|
void clear();
|
2014-06-11 18:11:51 +08:00
|
|
|
void addAddressRange(uptr beg, uptr end, bool executable);
|
2013-09-10 22:36:16 +08:00
|
|
|
bool containsAddress(uptr address) const;
|
|
|
|
|
|
|
|
const char *full_name() const { return full_name_; }
|
|
|
|
uptr base_address() const { return base_address_; }
|
2016-12-03 05:27:14 +08:00
|
|
|
ModuleArch arch() const { return arch_; }
|
|
|
|
const u8 *uuid() const { return uuid_; }
|
2013-09-10 22:36:16 +08:00
|
|
|
|
|
|
|
struct AddressRange {
|
2015-01-09 06:03:05 +08:00
|
|
|
AddressRange *next;
|
2013-09-10 22:36:16 +08:00
|
|
|
uptr beg;
|
|
|
|
uptr end;
|
2015-01-09 06:03:05 +08:00
|
|
|
bool executable;
|
|
|
|
|
|
|
|
AddressRange(uptr beg, uptr end, bool executable)
|
|
|
|
: next(nullptr), beg(beg), end(end), executable(executable) {}
|
2013-09-10 22:36:16 +08:00
|
|
|
};
|
2015-01-09 06:03:05 +08:00
|
|
|
|
2016-01-15 10:19:20 +08:00
|
|
|
const IntrusiveList<AddressRange> &ranges() const { return ranges_; }
|
2015-01-09 06:03:05 +08:00
|
|
|
|
|
|
|
private:
|
2015-01-09 06:31:14 +08:00
|
|
|
char *full_name_; // Owned.
|
2013-09-10 22:36:16 +08:00
|
|
|
uptr base_address_;
|
2016-12-03 05:27:14 +08:00
|
|
|
ModuleArch arch_;
|
|
|
|
u8 uuid_[kModuleUUIDSize];
|
2015-01-09 06:03:05 +08:00
|
|
|
IntrusiveList<AddressRange> ranges_;
|
2013-09-10 22:36:16 +08:00
|
|
|
};
|
|
|
|
|
2016-02-23 02:52:51 +08:00
|
|
|
// List of LoadedModules. OS-dependent implementation is responsible for
|
|
|
|
// filling this information.
|
|
|
|
class ListOfModules {
|
|
|
|
public:
|
|
|
|
ListOfModules() : modules_(kInitialCapacity) {}
|
|
|
|
~ListOfModules() { clear(); }
|
|
|
|
void init();
|
|
|
|
const LoadedModule *begin() const { return modules_.begin(); }
|
|
|
|
LoadedModule *begin() { return modules_.begin(); }
|
|
|
|
const LoadedModule *end() const { return modules_.end(); }
|
|
|
|
LoadedModule *end() { return modules_.end(); }
|
|
|
|
uptr size() const { return modules_.size(); }
|
|
|
|
const LoadedModule &operator[](uptr i) const {
|
|
|
|
CHECK_LT(i, modules_.size());
|
|
|
|
return modules_[i];
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
void clear() {
|
|
|
|
for (auto &module : modules_) module.clear();
|
|
|
|
modules_.clear();
|
|
|
|
}
|
|
|
|
|
|
|
|
InternalMmapVector<LoadedModule> modules_;
|
|
|
|
// We rarely have more than 16K loaded modules.
|
|
|
|
static const uptr kInitialCapacity = 1 << 14;
|
|
|
|
};
|
2013-09-10 22:36:16 +08:00
|
|
|
|
2013-10-14 22:04:50 +08:00
|
|
|
// Callback type for iterating over a set of memory ranges.
|
|
|
|
typedef void (*RangeIteratorCallback)(uptr begin, uptr end, void *arg);
|
2013-12-13 21:13:46 +08:00
|
|
|
|
2015-06-30 04:28:55 +08:00
|
|
|
enum AndroidApiLevel {
|
|
|
|
ANDROID_NOT_ANDROID = 0,
|
|
|
|
ANDROID_KITKAT = 19,
|
|
|
|
ANDROID_LOLLIPOP_MR1 = 22,
|
|
|
|
ANDROID_POST_LOLLIPOP = 23
|
|
|
|
};
|
|
|
|
|
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
|
|
|
void WriteToSyslog(const char *buffer);
|
|
|
|
|
|
|
|
#if SANITIZER_MAC
|
2015-11-21 02:42:01 +08:00
|
|
|
void LogFullErrorReport(const char *buffer);
|
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
|
|
|
#else
|
2015-11-21 02:42:01 +08:00
|
|
|
INLINE void LogFullErrorReport(const char *buffer) {}
|
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
|
|
|
#endif
|
|
|
|
|
|
|
|
#if SANITIZER_LINUX || SANITIZER_MAC
|
|
|
|
void WriteOneLineToSyslog(const char *s);
|
2016-01-07 07:15:01 +08:00
|
|
|
void LogMessageOnPrintf(const char *str);
|
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
|
|
|
#else
|
|
|
|
INLINE void WriteOneLineToSyslog(const char *s) {}
|
2016-01-07 07:15:01 +08:00
|
|
|
INLINE void LogMessageOnPrintf(const char *str) {}
|
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
|
|
|
#endif
|
|
|
|
|
2015-07-24 06:05:20 +08:00
|
|
|
#if SANITIZER_LINUX
|
2014-09-15 19:37:40 +08:00
|
|
|
// Initialize Android logging. Any writes before this are silently lost.
|
|
|
|
void AndroidLogInit();
|
2015-07-24 06:05:20 +08:00
|
|
|
#else
|
|
|
|
INLINE void AndroidLogInit() {}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#if SANITIZER_ANDROID
|
2014-02-11 21:38:57 +08:00
|
|
|
void SanitizerInitializeUnwinder();
|
2015-06-30 04:28:55 +08:00
|
|
|
AndroidApiLevel AndroidGetApiLevel();
|
2014-01-23 19:34:41 +08:00
|
|
|
#else
|
|
|
|
INLINE void AndroidLogWrite(const char *buffer_unused) {}
|
2014-02-11 21:38:57 +08:00
|
|
|
INLINE void SanitizerInitializeUnwinder() {}
|
2015-06-30 04:28:55 +08:00
|
|
|
INLINE AndroidApiLevel AndroidGetApiLevel() { return ANDROID_NOT_ANDROID; }
|
2014-01-23 19:34:41 +08:00
|
|
|
#endif
|
2014-11-22 05:25:09 +08:00
|
|
|
|
2015-06-30 04:28:55 +08:00
|
|
|
INLINE uptr GetPthreadDestructorIterations() {
|
|
|
|
#if SANITIZER_ANDROID
|
|
|
|
return (AndroidGetApiLevel() == ANDROID_LOLLIPOP_MR1) ? 8 : 4;
|
|
|
|
#elif SANITIZER_POSIX
|
|
|
|
return 4;
|
|
|
|
#else
|
|
|
|
// Unused on Windows.
|
|
|
|
return 0;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2014-12-17 03:13:01 +08:00
|
|
|
void *internal_start_thread(void(*func)(void*), void *arg);
|
|
|
|
void internal_join_thread(void *th);
|
|
|
|
void MaybeStartBackgroudThread();
|
|
|
|
|
2014-11-22 05:25:09 +08:00
|
|
|
// Make the compiler think that something is going on there.
|
|
|
|
// Use this inside a loop that looks like memset/memcpy/etc to prevent the
|
|
|
|
// compiler from recognising it and turning it into an actual call to
|
|
|
|
// memset/memcpy/etc.
|
|
|
|
static inline void SanitizerBreakOptimization(void *arg) {
|
2016-04-02 01:09:08 +08:00
|
|
|
#if defined(_MSC_VER) && !defined(__clang__)
|
2015-07-02 09:44:34 +08:00
|
|
|
_ReadWriteBarrier();
|
2014-11-22 05:25:09 +08:00
|
|
|
#else
|
|
|
|
__asm__ __volatile__("" : : "r" (arg) : "memory");
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2015-03-03 01:36:02 +08:00
|
|
|
struct SignalContext {
|
|
|
|
void *context;
|
|
|
|
uptr addr;
|
|
|
|
uptr pc;
|
|
|
|
uptr sp;
|
|
|
|
uptr bp;
|
2016-02-04 10:02:09 +08:00
|
|
|
bool is_memory_access;
|
2016-02-09 06:50:25 +08:00
|
|
|
|
|
|
|
enum WriteFlag { UNKNOWN, READ, WRITE } write_flag;
|
2015-03-03 01:36:02 +08:00
|
|
|
|
2016-02-04 10:02:09 +08:00
|
|
|
SignalContext(void *context, uptr addr, uptr pc, uptr sp, uptr bp,
|
2016-02-09 06:50:25 +08:00
|
|
|
bool is_memory_access, WriteFlag write_flag)
|
|
|
|
: context(context),
|
|
|
|
addr(addr),
|
|
|
|
pc(pc),
|
|
|
|
sp(sp),
|
|
|
|
bp(bp),
|
|
|
|
is_memory_access(is_memory_access),
|
|
|
|
write_flag(write_flag) {}
|
2015-03-03 01:36:02 +08:00
|
|
|
|
2016-11-26 08:50:08 +08:00
|
|
|
static void DumpAllRegisters(void *context);
|
|
|
|
|
2015-03-03 01:36:02 +08:00
|
|
|
// Creates signal context in a platform-specific manner.
|
|
|
|
static SignalContext Create(void *siginfo, void *context);
|
2016-02-09 06:50:25 +08:00
|
|
|
|
|
|
|
// Returns true if the "context" indicates a memory write.
|
|
|
|
static WriteFlag GetWriteFlag(void *context);
|
2015-03-03 01:36:02 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
void GetPcSpBp(void *context, uptr *pc, uptr *sp, uptr *bp);
|
|
|
|
|
2015-12-03 18:39:43 +08:00
|
|
|
void MaybeReexec();
|
|
|
|
|
2016-01-27 04:10:01 +08:00
|
|
|
template <typename Fn>
|
|
|
|
class RunOnDestruction {
|
|
|
|
public:
|
|
|
|
explicit RunOnDestruction(Fn fn) : fn_(fn) {}
|
|
|
|
~RunOnDestruction() { fn_(); }
|
|
|
|
|
|
|
|
private:
|
|
|
|
Fn fn_;
|
|
|
|
};
|
|
|
|
|
|
|
|
// A simple scope guard. Usage:
|
|
|
|
// auto cleanup = at_scope_exit([]{ do_cleanup; });
|
|
|
|
template <typename Fn>
|
|
|
|
RunOnDestruction<Fn> at_scope_exit(Fn fn) {
|
|
|
|
return RunOnDestruction<Fn>(fn);
|
|
|
|
}
|
|
|
|
|
[sanitizer] [SystemZ] Abort if the kernel might be vulnerable to CVE-2016-2143.
In short, CVE-2016-2143 will crash the machine if a process uses both >4TB
virtual addresses and fork(). ASan, TSan, and MSan will, by necessity, map
a sizable chunk of virtual address space, which is much larger than 4TB.
Even worse, sanitizers will always use fork() for llvm-symbolizer when a bug
is detected. Disable all three by aborting on process initialization if
the running kernel version is not known to contain a fix.
Unfortunately, there's no reliable way to detect the fix without crashing
the kernel. So, we rely on whitelisting - I've included a list of upstream
kernel versions that will work. In case someone uses a distribution kernel
or applied the fix themselves, an override switch is also included.
Differential Revision: http://reviews.llvm.org/D19576
llvm-svn: 267747
2016-04-28 01:42:00 +08:00
|
|
|
// Linux on 64-bit s390 had a nasty bug that crashes the whole machine
|
|
|
|
// if a process uses virtual memory over 4TB (as many sanitizers like
|
|
|
|
// to do). This function will abort the process if running on a kernel
|
|
|
|
// that looks vulnerable.
|
|
|
|
#if SANITIZER_LINUX && SANITIZER_S390_64
|
|
|
|
void AvoidCVE_2016_2143();
|
|
|
|
#else
|
|
|
|
INLINE void AvoidCVE_2016_2143() {}
|
|
|
|
#endif
|
|
|
|
|
2016-09-16 05:02:18 +08:00
|
|
|
struct StackDepotStats {
|
|
|
|
uptr n_uniq_ids;
|
|
|
|
uptr allocated;
|
|
|
|
};
|
|
|
|
|
2016-11-29 08:22:50 +08:00
|
|
|
// The default value for allocator_release_to_os_interval_ms common flag to
|
|
|
|
// indicate that sanitizer allocator should not attempt to release memory to OS.
|
|
|
|
const s32 kReleaseToOSIntervalNever = -1;
|
|
|
|
|
2012-06-06 14:47:26 +08:00
|
|
|
} // namespace __sanitizer
|
|
|
|
|
2013-10-24 14:23:39 +08:00
|
|
|
inline void *operator new(__sanitizer::operator_new_size_type size,
|
|
|
|
__sanitizer::LowLevelAllocator &alloc) {
|
|
|
|
return alloc.Allocate(size);
|
|
|
|
}
|
|
|
|
|
2012-06-06 14:47:26 +08:00
|
|
|
#endif // SANITIZER_COMMON_H
|