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 {
|
2017-09-12 08:44:23 +08:00
|
|
|
|
2015-02-27 10:29:25 +08:00
|
|
|
struct AddressInfo;
|
2017-09-15 06:44:03 +08:00
|
|
|
struct BufferedStackTrace;
|
2017-09-12 08:44:23 +08:00
|
|
|
struct SignalContext;
|
|
|
|
struct StackTrace;
|
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
|
|
|
|
2018-03-13 01:18:26 +08:00
|
|
|
const uptr kCacheLineSize = SANITIZER_CACHE_LINE_SIZE;
|
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.
|
2018-03-21 16:44:14 +08:00
|
|
|
// For such PC values __tsan_symbolize_external_ex() 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();
|
2017-11-21 01:41:57 +08:00
|
|
|
uptr GetMaxVirtualAddress();
|
2017-11-08 07:51:22 +08:00
|
|
|
uptr GetMaxUserVirtualAddress();
|
2012-06-07 15:13:46 +08:00
|
|
|
// Threads
|
2017-04-18 02:17:38 +08:00
|
|
|
tid_t 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);
|
2017-06-17 02:48:08 +08:00
|
|
|
// Behaves just like MmapOrDie, but tolerates out of memory condition, in that
|
|
|
|
// case returns nullptr.
|
|
|
|
void *MmapOrDieOnFatalError(uptr size, const char *mem_type);
|
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);
|
2017-06-27 06:54:10 +08:00
|
|
|
// Behaves just like MmapFixedOrDie, but tolerates out of memory condition, in
|
|
|
|
// that case returns nullptr.
|
|
|
|
void *MmapFixedOrDieOnFatalError(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.
|
2017-06-22 08:02:37 +08:00
|
|
|
// Dies on all but out of memory errors, in the latter case returns nullptr.
|
|
|
|
void *MmapAlignedOrDieOnFatalError(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
|
|
|
|
2018-03-24 15:45:24 +08:00
|
|
|
void MprotectMallocZones(void *addr, int prot);
|
|
|
|
|
2016-10-01 01:47:34 +08:00
|
|
|
// Find an available address space.
|
2017-07-13 07:29:21 +08:00
|
|
|
uptr FindAvailableMemoryRange(uptr size, uptr alignment, uptr left_padding,
|
2018-02-27 02:33:21 +08:00
|
|
|
uptr *largest_gap_found, uptr *max_occupied_addr);
|
2016-10-01 01:47:34 +08:00
|
|
|
|
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
|
|
|
|
Introduce ReservedAddressRange to sanitizer_common.
Summary:
Fixed version of https://reviews.llvm.org/D38437 (fixes Win/Fuchsia failures).
Creating a new revision, since the old one was getting a bit old/crowded.
In Fuchsia, MmapNoAccess/MmapFixedOrDie are implemented using a global
VMAR, which means that MmapNoAccess can only be called once. This works
for the sanitizer allocator but *not* for the Scudo allocator.
Hence, this changeset introduces a new ReservedAddressRange object to
serve as the new API for these calls. In this changeset, the object
still calls into the old Mmap implementations.
The next changeset two changesets will convert the sanitizer and scudo
allocators to use the new APIs, respectively. (ReservedAddressRange will
replace the SecondaryHeader in Scudo.)
Finally, a last changeset will update the Fuchsia implementation.
Reviewers: alekseyshl, cryptoad, phosek
Reviewed By: alekseyshl, cryptoad
Subscribers: kubamracek
Differential Revision: https://reviews.llvm.org/D39072
llvm-svn: 316934
2017-10-31 01:56:24 +08:00
|
|
|
class ReservedAddressRange {
|
|
|
|
public:
|
|
|
|
uptr Init(uptr size, const char *name = nullptr, uptr fixed_addr = 0);
|
2017-11-08 00:19:24 +08:00
|
|
|
uptr Map(uptr fixed_addr, uptr size);
|
|
|
|
uptr MapOrDie(uptr fixed_addr, uptr size);
|
Introduce ReservedAddressRange to sanitizer_common.
Summary:
Fixed version of https://reviews.llvm.org/D38437 (fixes Win/Fuchsia failures).
Creating a new revision, since the old one was getting a bit old/crowded.
In Fuchsia, MmapNoAccess/MmapFixedOrDie are implemented using a global
VMAR, which means that MmapNoAccess can only be called once. This works
for the sanitizer allocator but *not* for the Scudo allocator.
Hence, this changeset introduces a new ReservedAddressRange object to
serve as the new API for these calls. In this changeset, the object
still calls into the old Mmap implementations.
The next changeset two changesets will convert the sanitizer and scudo
allocators to use the new APIs, respectively. (ReservedAddressRange will
replace the SecondaryHeader in Scudo.)
Finally, a last changeset will update the Fuchsia implementation.
Reviewers: alekseyshl, cryptoad, phosek
Reviewed By: alekseyshl, cryptoad
Subscribers: kubamracek
Differential Revision: https://reviews.llvm.org/D39072
llvm-svn: 316934
2017-10-31 01:56:24 +08:00
|
|
|
void Unmap(uptr addr, uptr size);
|
|
|
|
void *base() const { return base_; }
|
|
|
|
uptr size() const { return size_; }
|
|
|
|
|
|
|
|
private:
|
|
|
|
void* base_;
|
|
|
|
uptr size_;
|
|
|
|
const char* name_;
|
2017-11-28 03:53:53 +08:00
|
|
|
uptr os_handle_;
|
Introduce ReservedAddressRange to sanitizer_common.
Summary:
Fixed version of https://reviews.llvm.org/D38437 (fixes Win/Fuchsia failures).
Creating a new revision, since the old one was getting a bit old/crowded.
In Fuchsia, MmapNoAccess/MmapFixedOrDie are implemented using a global
VMAR, which means that MmapNoAccess can only be called once. This works
for the sanitizer allocator but *not* for the Scudo allocator.
Hence, this changeset introduces a new ReservedAddressRange object to
serve as the new API for these calls. In this changeset, the object
still calls into the old Mmap implementations.
The next changeset two changesets will convert the sanitizer and scudo
allocators to use the new APIs, respectively. (ReservedAddressRange will
replace the SecondaryHeader in Scudo.)
Finally, a last changeset will update the Fuchsia implementation.
Reviewers: alekseyshl, cryptoad, phosek
Reviewed By: alekseyshl, cryptoad
Subscribers: kubamracek
Differential Revision: https://reviews.llvm.org/D39072
llvm-svn: 316934
2017-10-31 01:56:24 +08:00
|
|
|
};
|
|
|
|
|
2017-09-26 04:48:51 +08:00
|
|
|
typedef void (*fill_profile_f)(uptr start, uptr rss, bool file,
|
|
|
|
/*out*/uptr *stats, uptr stats_size);
|
|
|
|
|
|
|
|
// Parse the contents of /proc/self/smaps and generate a memory profile.
|
|
|
|
// |cb| is a tool-specific callback that fills the |stats| array containing
|
|
|
|
// |stats_size| elements.
|
|
|
|
void GetMemoryProfile(fill_profile_f cb, uptr *stats, uptr stats_size);
|
|
|
|
|
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_;
|
|
|
|
};
|
2017-11-21 09:01:32 +08:00
|
|
|
// Set the min alignment of LowLevelAllocator to at least alignment.
|
|
|
|
void SetLowLevelAllocateMinAlignment(uptr alignment);
|
2012-08-27 17:30:58 +08:00
|
|
|
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
|
2017-07-22 09:46:40 +08:00
|
|
|
void CatastrophicErrorWrite(const char *buffer, uptr length);
|
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
|
|
|
|
2017-09-23 02:32:05 +08:00
|
|
|
// Lock sanitizer error reporting and protects against nested errors.
|
|
|
|
class ScopedErrorReportLock {
|
|
|
|
public:
|
2017-09-23 06:36:21 +08:00
|
|
|
ScopedErrorReportLock();
|
2017-09-23 02:32:05 +08:00
|
|
|
~ScopedErrorReportLock();
|
2017-09-23 10:47:21 +08:00
|
|
|
|
|
|
|
static void CheckLocked();
|
2017-09-23 02:32:05 +08:00
|
|
|
};
|
|
|
|
|
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
|
|
|
|
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
|
|
|
|
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();
|
2017-01-07 04:57:47 +08:00
|
|
|
void PrintModuleMap();
|
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);
|
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);
|
2018-04-04 02:07:22 +08:00
|
|
|
void PlatformPrepareForSandboxing(__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 InitializeCoverage(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();
|
2017-12-14 00:23:54 +08:00
|
|
|
u64 MonotonicNanoTime();
|
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 *);
|
2017-05-26 07:42:33 +08:00
|
|
|
HandleSignalMode GetHandleSignalMode(int signum);
|
2014-01-31 21:10:07 +08:00
|
|
|
void InstallDeadlySignalHandlers(SignalHandlerType handler);
|
2017-09-23 06:57:48 +08:00
|
|
|
|
2017-09-13 12:46:37 +08:00
|
|
|
// Signal reporting.
|
2017-09-15 06:44:03 +08:00
|
|
|
// Each sanitizer uses slightly different implementation of stack unwinding.
|
|
|
|
typedef void (*UnwindSignalStackCallbackType)(const SignalContext &sig,
|
|
|
|
const void *callback_context,
|
|
|
|
BufferedStackTrace *stack);
|
2017-09-23 06:57:48 +08:00
|
|
|
// Print deadly signal report and die.
|
|
|
|
void HandleDeadlySignal(void *siginfo, void *context, u32 tid,
|
|
|
|
UnwindSignalStackCallbackType unwind,
|
|
|
|
const void *unwind_context);
|
|
|
|
|
|
|
|
// Part of HandleDeadlySignal, exposed for asan.
|
|
|
|
void StartReportDeadlySignal();
|
|
|
|
// Part of HandleDeadlySignal, exposed for asan.
|
2017-09-15 06:44:03 +08:00
|
|
|
void ReportDeadlySignal(const SignalContext &sig, u32 tid,
|
|
|
|
UnwindSignalStackCallbackType unwind,
|
|
|
|
const void *unwind_context);
|
2017-09-23 06:57:48 +08:00
|
|
|
|
2014-01-31 21:10:07 +08:00
|
|
|
// 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.
|
2017-04-15 02:24:35 +08:00
|
|
|
// If alt_tool_name is provided, it's used in place of SanitizerToolName.
|
|
|
|
void ReportErrorSummary(const char *error_message,
|
|
|
|
const char *alt_tool_name = nullptr);
|
2013-11-02 01:02:14 +08:00
|
|
|
// Same as above, but construct error_message as:
|
2015-02-27 10:29:25 +08:00
|
|
|
// error_type file:line[:column][ function]
|
2017-04-15 02:24:35 +08:00
|
|
|
void ReportErrorSummary(const char *error_type, const AddressInfo &info,
|
|
|
|
const char *alt_tool_name = nullptr);
|
2015-02-27 10:29:25 +08:00
|
|
|
// Same as above, but obtains AddressInfo by symbolizing top stack trace frame.
|
2017-04-15 02:24:35 +08:00
|
|
|
void ReportErrorSummary(const char *error_type, const StackTrace *trace,
|
|
|
|
const char *alt_tool_name = nullptr);
|
2013-02-06 20:36:49 +08:00
|
|
|
|
2018-03-24 15:45:24 +08:00
|
|
|
void ReportMmapWriteExec(int prot);
|
2018-03-22 05:25:07 +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
|
|
|
|
};
|
|
|
|
|
2017-01-07 04:57:47 +08:00
|
|
|
// When adding a new architecture, don't forget to also update
|
|
|
|
// script/asan_symbolize.py and sanitizer_symbolizer_libcdep.cc.
|
|
|
|
inline const char *ModuleArchToString(ModuleArch arch) {
|
|
|
|
switch (arch) {
|
|
|
|
case kModuleArchUnknown:
|
|
|
|
return "";
|
|
|
|
case kModuleArchI386:
|
|
|
|
return "i386";
|
|
|
|
case kModuleArchX86_64:
|
|
|
|
return "x86_64";
|
|
|
|
case kModuleArchX86_64H:
|
|
|
|
return "x86_64h";
|
|
|
|
case kModuleArchARMV6:
|
|
|
|
return "armv6";
|
|
|
|
case kModuleArchARMV7:
|
|
|
|
return "armv7";
|
|
|
|
case kModuleArchARMV7S:
|
|
|
|
return "armv7s";
|
|
|
|
case kModuleArchARMV7K:
|
|
|
|
return "armv7k";
|
|
|
|
case kModuleArchARM64:
|
|
|
|
return "arm64";
|
|
|
|
}
|
|
|
|
CHECK(0 && "Invalid module arch");
|
2017-01-11 09:12:53 +08:00
|
|
|
return "";
|
2017-01-07 04:57:47 +08:00
|
|
|
}
|
|
|
|
|
2016-12-03 05:27:14 +08:00
|
|
|
const uptr kModuleUUIDSize = 16;
|
2017-07-26 02:16:58 +08:00
|
|
|
const uptr kMaxSegName = 16;
|
2016-12-03 05:27:14 +08:00
|
|
|
|
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()
|
2017-01-07 03:34:54 +08:00
|
|
|
: full_name_(nullptr),
|
|
|
|
base_address_(0),
|
2017-01-07 04:57:47 +08:00
|
|
|
max_executable_address_(0),
|
2017-01-07 03:34:54 +08:00
|
|
|
arch_(kModuleArchUnknown),
|
|
|
|
instrumented_(false) {
|
2016-12-03 05:27:14 +08:00
|
|
|
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,
|
2017-01-07 03:34:54 +08:00
|
|
|
u8 uuid[kModuleUUIDSize], bool instrumented);
|
2015-01-09 06:31:14 +08:00
|
|
|
void clear();
|
2017-07-26 02:16:58 +08:00
|
|
|
void addAddressRange(uptr beg, uptr end, bool executable, bool writable,
|
|
|
|
const char *name = nullptr);
|
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_; }
|
2017-01-07 04:57:47 +08:00
|
|
|
uptr max_executable_address() const { return max_executable_address_; }
|
2016-12-03 05:27:14 +08:00
|
|
|
ModuleArch arch() const { return arch_; }
|
|
|
|
const u8 *uuid() const { return uuid_; }
|
2017-01-07 03:34:54 +08:00
|
|
|
bool instrumented() const { return instrumented_; }
|
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;
|
2017-05-19 21:34:02 +08:00
|
|
|
bool writable;
|
2017-07-26 02:16:58 +08:00
|
|
|
char name[kMaxSegName];
|
2017-04-18 00:34:38 +08:00
|
|
|
|
2017-07-26 02:16:58 +08:00
|
|
|
AddressRange(uptr beg, uptr end, bool executable, bool writable,
|
|
|
|
const char *name)
|
2017-04-18 00:34:38 +08:00
|
|
|
: next(nullptr),
|
|
|
|
beg(beg),
|
|
|
|
end(end),
|
|
|
|
executable(executable),
|
2017-07-26 02:16:58 +08:00
|
|
|
writable(writable) {
|
|
|
|
internal_strncpy(this->name, (name ? name : ""), ARRAY_SIZE(this->name));
|
|
|
|
}
|
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_;
|
2017-01-07 04:57:47 +08:00
|
|
|
uptr max_executable_address_;
|
2016-12-03 05:27:14 +08:00
|
|
|
ModuleArch arch_;
|
|
|
|
u8 uuid_[kModuleUUIDSize];
|
2017-01-07 03:34:54 +08:00
|
|
|
bool instrumented_;
|
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:
|
2017-09-30 04:55:06 +08:00
|
|
|
ListOfModules() : initialized(false) {}
|
2016-02-23 02:52:51 +08:00
|
|
|
~ListOfModules() { clear(); }
|
|
|
|
void init();
|
2017-10-03 04:22:16 +08:00
|
|
|
void fallbackInit(); // Uses fallback init if available, otherwise clears
|
2016-02-23 02:52:51 +08:00
|
|
|
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();
|
|
|
|
}
|
2017-09-30 04:55:06 +08:00
|
|
|
void clearOrInit() {
|
|
|
|
initialized ? clear() : modules_.Initialize(kInitialCapacity);
|
|
|
|
initialized = true;
|
|
|
|
}
|
2016-02-23 02:52:51 +08:00
|
|
|
|
2017-09-30 04:55:06 +08:00
|
|
|
InternalMmapVectorNoCtor<LoadedModule> modules_;
|
2016-02-23 02:52:51 +08:00
|
|
|
// We rarely have more than 16K loaded modules.
|
|
|
|
static const uptr kInitialCapacity = 1 << 14;
|
2017-09-30 04:55:06 +08:00
|
|
|
bool initialized;
|
2016-02-23 02:52:51 +08:00
|
|
|
};
|
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();
|
2017-06-24 07:38:20 +08:00
|
|
|
void SetAbortMessage(const char *);
|
2015-07-24 06:05:20 +08:00
|
|
|
#else
|
|
|
|
INLINE void AndroidLogInit() {}
|
2017-06-24 07:38:20 +08:00
|
|
|
// FIXME: MacOS implementation could use CRSetCrashLogMessage.
|
|
|
|
INLINE void SetAbortMessage(const char *) {}
|
2015-07-24 06:05:20 +08:00
|
|
|
#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 {
|
2017-09-14 02:30:06 +08:00
|
|
|
void *siginfo;
|
2015-03-03 01:36:02 +08:00
|
|
|
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
|
|
|
|
2017-09-14 02:30:16 +08:00
|
|
|
// VS2013 doesn't implement unrestricted unions, so we need a trivial default
|
|
|
|
// constructor
|
|
|
|
SignalContext() = default;
|
2017-09-14 10:48:41 +08:00
|
|
|
|
|
|
|
// Creates signal context in a platform-specific manner.
|
2017-09-14 02:30:06 +08:00
|
|
|
// SignalContext is going to keep pointers to siginfo and context without
|
|
|
|
// owning them.
|
2017-09-14 10:48:41 +08:00
|
|
|
SignalContext(void *siginfo, void *context)
|
2017-09-14 02:30:06 +08:00
|
|
|
: siginfo(siginfo),
|
|
|
|
context(context),
|
2017-09-14 10:48:41 +08:00
|
|
|
addr(GetAddress()),
|
|
|
|
is_memory_access(IsMemoryAccess()),
|
|
|
|
write_flag(GetWriteFlag()) {
|
|
|
|
InitPcSpBp();
|
|
|
|
}
|
2015-03-03 01:36:02 +08:00
|
|
|
|
2016-11-26 08:50:08 +08:00
|
|
|
static void DumpAllRegisters(void *context);
|
|
|
|
|
2017-09-14 02:30:06 +08:00
|
|
|
// Type of signal e.g. SIGSEGV or EXCEPTION_ACCESS_VIOLATION.
|
|
|
|
int GetType() const;
|
|
|
|
|
|
|
|
// String description of the signal.
|
2017-09-14 02:30:16 +08:00
|
|
|
const char *Describe() const;
|
2015-03-03 01:36:02 +08:00
|
|
|
|
2017-09-14 11:23:02 +08:00
|
|
|
// Returns true if signal is stack overflow.
|
|
|
|
bool IsStackOverflow() const;
|
|
|
|
|
2017-09-14 10:48:41 +08:00
|
|
|
private:
|
|
|
|
// Platform specific initialization.
|
|
|
|
void InitPcSpBp();
|
|
|
|
uptr GetAddress() const;
|
|
|
|
WriteFlag GetWriteFlag() const;
|
|
|
|
bool IsMemoryAccess() const;
|
|
|
|
};
|
2015-03-03 01:36:02 +08:00
|
|
|
|
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;
|
|
|
|
|
2017-03-09 18:47:38 +08:00
|
|
|
void CheckNoDeepBind(const char *filename, int flag);
|
|
|
|
|
[sanitizer] Add a function to gather random bytes
Summary:
AFAICT compiler-rt doesn't have a function that would return 'good' random
bytes to seed a PRNG. Currently, the `SizeClassAllocator64` uses addresses
returned by `mmap` to seed its PRNG, which is not ideal, and
`SizeClassAllocator32` doesn't benefit from the entropy offered by its 64-bit
counterpart address space, so right now it has nothing. This function aims at
solving this, allowing to implement good 32-bit chunk randomization. Scudo also
has a function that does this for Cookie purposes, which would go away in a
later CL once this lands.
This function will try the `getrandom` syscall if available, and fallback to
`/dev/urandom` if not.
Unfortunately, I do not have a way to implement and test a Mac and Windows
version, so those are unimplemented as of now. Note that `kRandomShuffleChunks`
is only used on Linux for now.
Reviewers: alekseyshl
Reviewed By: alekseyshl
Subscribers: zturner, rnk, llvm-commits, kubamracek
Differential Revision: https://reviews.llvm.org/D34412
llvm-svn: 305922
2017-06-21 23:56:03 +08:00
|
|
|
// Returns the requested amount of random data (up to 256 bytes) that can then
|
2017-08-14 22:53:47 +08:00
|
|
|
// be used to seed a PRNG. Defaults to blocking like the underlying syscall.
|
|
|
|
bool GetRandom(void *buffer, uptr length, bool blocking = true);
|
[sanitizer] Add a function to gather random bytes
Summary:
AFAICT compiler-rt doesn't have a function that would return 'good' random
bytes to seed a PRNG. Currently, the `SizeClassAllocator64` uses addresses
returned by `mmap` to seed its PRNG, which is not ideal, and
`SizeClassAllocator32` doesn't benefit from the entropy offered by its 64-bit
counterpart address space, so right now it has nothing. This function aims at
solving this, allowing to implement good 32-bit chunk randomization. Scudo also
has a function that does this for Cookie purposes, which would go away in a
later CL once this lands.
This function will try the `getrandom` syscall if available, and fallback to
`/dev/urandom` if not.
Unfortunately, I do not have a way to implement and test a Mac and Windows
version, so those are unimplemented as of now. Note that `kRandomShuffleChunks`
is only used on Linux for now.
Reviewers: alekseyshl
Reviewed By: alekseyshl
Subscribers: zturner, rnk, llvm-commits, kubamracek
Differential Revision: https://reviews.llvm.org/D34412
llvm-svn: 305922
2017-06-21 23:56:03 +08:00
|
|
|
|
2017-11-22 05:14:00 +08:00
|
|
|
// Returns the number of logical processors on the system.
|
|
|
|
u32 GetNumberOfCPUs();
|
|
|
|
extern u32 NumberOfCPUsCached;
|
|
|
|
INLINE u32 GetNumberOfCPUsCached() {
|
|
|
|
if (!NumberOfCPUsCached)
|
|
|
|
NumberOfCPUsCached = GetNumberOfCPUs();
|
|
|
|
return NumberOfCPUsCached;
|
|
|
|
}
|
|
|
|
|
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
|