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.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// This file is shared between AddressSanitizer and ThreadSanitizer
|
|
|
|
// run-time libraries.
|
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
|
|
|
|
|
|
|
|
#include "sanitizer_internal_defs.h"
|
2013-02-26 21:30:27 +08:00
|
|
|
#include "sanitizer_libc.h"
|
2013-04-05 15:30:29 +08:00
|
|
|
#include "sanitizer_mutex.h"
|
2012-06-06 14:47:26 +08:00
|
|
|
|
|
|
|
namespace __sanitizer {
|
2013-02-06 20:36:49 +08:00
|
|
|
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
|
|
|
|
2012-11-20 15:00:42 +08:00
|
|
|
#if defined(__powerpc__) || defined(__powerpc64__)
|
|
|
|
const uptr kCacheLineSize = 128;
|
2012-06-15 15:29:14 +08:00
|
|
|
#else
|
2012-11-20 15:00:42 +08:00
|
|
|
const uptr kCacheLineSize = 64;
|
2012-06-15 15:29:14 +08:00
|
|
|
#endif
|
2012-06-06 17:26:25 +08:00
|
|
|
|
2013-06-11 16:13:36 +08:00
|
|
|
const uptr kMaxPathLength = 512;
|
|
|
|
|
2013-01-31 22:11:21 +08:00
|
|
|
extern const char *SanitizerToolName; // Can be changed by the tool.
|
|
|
|
|
2012-11-23 23:38:49 +08:00
|
|
|
uptr GetPageSize();
|
|
|
|
uptr GetPageSizeCached();
|
|
|
|
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
|
|
|
|
void *MmapOrDie(uptr size, const char *mem_type);
|
2012-06-06 17:26:25 +08:00
|
|
|
void UnmapOrDie(void *addr, uptr size);
|
2012-06-14 22:42:58 +08:00
|
|
|
void *MmapFixedNoReserve(uptr fixed_addr, uptr size);
|
2012-12-13 13:36:00 +08:00
|
|
|
void *MmapFixedOrDie(uptr fixed_addr, uptr size);
|
2012-06-14 22:42:58 +08:00
|
|
|
void *Mprotect(uptr fixed_addr, 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);
|
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);
|
2012-12-27 15:37:24 +08:00
|
|
|
void FlushUnneededShadowMemory(uptr addr, uptr size);
|
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.
|
2012-08-21 16:13:37 +08:00
|
|
|
template<typename T>
|
|
|
|
class InternalScopedBuffer {
|
|
|
|
public:
|
|
|
|
explicit InternalScopedBuffer(uptr cnt) {
|
|
|
|
cnt_ = cnt;
|
2012-08-29 16:40:36 +08:00
|
|
|
ptr_ = (T*)MmapOrDie(cnt * sizeof(T), "InternalScopedBuffer");
|
2012-08-21 16:13:37 +08:00
|
|
|
}
|
|
|
|
~InternalScopedBuffer() {
|
2012-08-29 16:40:36 +08:00
|
|
|
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_;
|
|
|
|
// Disallow evil constructors.
|
|
|
|
InternalScopedBuffer(const InternalScopedBuffer&);
|
|
|
|
void operator=(const InternalScopedBuffer&);
|
|
|
|
};
|
|
|
|
|
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);
|
2012-11-02 23:18:34 +08:00
|
|
|
bool PrintsToTty();
|
2013-09-03 21:31:03 +08:00
|
|
|
// Caching version of PrintsToTty(). Not thread-safe.
|
|
|
|
bool PrintsToTtyCached();
|
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-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;
|
2013-05-18 00:17:19 +08:00
|
|
|
void MaybeOpenReportFile();
|
|
|
|
extern fd_t report_fd;
|
2013-09-05 11:19:57 +08:00
|
|
|
extern bool log_to_file;
|
|
|
|
extern char report_path_prefix[4096];
|
|
|
|
extern uptr report_fd_pid;
|
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
|
|
|
|
2013-05-08 22:43:49 +08:00
|
|
|
uptr OpenFile(const char *filename, bool write);
|
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'.
|
|
|
|
// The size of the mmaped region is stored in '*buff_size',
|
|
|
|
// Returns the number of read bytes or 0 if file can not be opened.
|
|
|
|
uptr ReadFileToBuffer(const char *file_name, char **buff,
|
|
|
|
uptr *buff_size, uptr max_len);
|
2012-07-03 16:24:14 +08:00
|
|
|
// Maps given file to virtual memory, and returns pointer to it
|
|
|
|
// (or NULL if the mapping failes). Stores the size of mmaped region
|
|
|
|
// in '*buff_size'.
|
|
|
|
void *MapFileToMemory(const char *file_name, uptr *buff_size);
|
|
|
|
|
2013-10-04 16:55:03 +08:00
|
|
|
// Error report formatting.
|
|
|
|
const char *StripPathPrefix(const char *filepath,
|
|
|
|
const char *strip_file_prefix);
|
2013-11-14 17:41:24 +08:00
|
|
|
void PrintSourceLocation(InternalScopedString *buffer, const char *file,
|
|
|
|
int line, int column);
|
|
|
|
void PrintModuleAndOffset(InternalScopedString *buffer,
|
|
|
|
const char *module, uptr offset);
|
2013-10-04 16:55:03 +08:00
|
|
|
|
2012-09-17 17:12:39 +08:00
|
|
|
// OS
|
|
|
|
void DisableCoreDumper();
|
|
|
|
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);
|
2013-04-23 20:49:12 +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);
|
2013-02-18 15:17:12 +08:00
|
|
|
u32 GetUid();
|
2012-09-17 17:12:39 +08:00
|
|
|
void ReExec();
|
|
|
|
bool StackSizeIsUnlimited();
|
|
|
|
void SetStackSizeLimitInBytes(uptr limit);
|
2012-12-10 21:10:40 +08:00
|
|
|
void PrepareForSandboxing();
|
2012-06-07 13:38:26 +08:00
|
|
|
|
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);
|
2013-11-15 15:18:15 +08:00
|
|
|
// Strip the directories from the module name, return a new string allocated
|
|
|
|
// with internal_strdup.
|
|
|
|
char *StripModuleName(const char *module);
|
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);
|
|
|
|
|
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);
|
|
|
|
void SetDieCallback(DieCallbackType);
|
|
|
|
DieCallbackType GetDieCallback();
|
2012-09-11 17:44:48 +08:00
|
|
|
typedef void (*CheckFailedCallbackType)(const char *, int, const char *,
|
|
|
|
u64, u64);
|
|
|
|
void SetCheckFailedCallback(CheckFailedCallbackType callback);
|
|
|
|
|
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:
|
|
|
|
// error_type: file:line function
|
2013-02-06 20:36:49 +08:00
|
|
|
void ReportErrorSummary(const char *error_type, const char *file,
|
|
|
|
int line, const char *function);
|
2013-11-02 01:02:14 +08:00
|
|
|
void ReportErrorSummary(const char *error_type, 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__)
|
2013-02-08 19:45:04 +08:00
|
|
|
up = SANITIZER_WORDSIZE - 1 - __builtin_clzl(x);
|
|
|
|
#elif defined(_WIN64)
|
|
|
|
_BitScanReverse64(&up, x);
|
|
|
|
#else
|
|
|
|
_BitScanReverse(&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);
|
|
|
|
CHECK(size < (1ULL << (up + 1)));
|
|
|
|
CHECK(size > (1ULL << up));
|
|
|
|
return 1UL << (up + 1);
|
|
|
|
}
|
|
|
|
|
2012-07-06 00:18:28 +08:00
|
|
|
INLINE uptr RoundUpTo(uptr size, uptr boundary) {
|
2012-06-15 15:00:31 +08:00
|
|
|
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));
|
2013-06-10 18:02:02 +08:00
|
|
|
#if !SANITIZER_WINDOWS || defined(__clang__) || defined(__GNUC__)
|
2013-02-08 19:45:04 +08:00
|
|
|
return __builtin_ctzl(x);
|
|
|
|
#elif defined(_WIN64)
|
|
|
|
unsigned long ret; // NOLINT
|
|
|
|
_BitScanForward64(&ret, x);
|
|
|
|
return ret;
|
|
|
|
#else
|
|
|
|
unsigned long ret; // NOLINT
|
|
|
|
_BitScanForward(&ret, x);
|
|
|
|
return ret;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2012-11-21 20:38:58 +08:00
|
|
|
#if SANITIZER_WORDSIZE == 64
|
2012-06-07 00:33:46 +08:00
|
|
|
# define FIRST_32_SECOND_64(a, b) (b)
|
|
|
|
#else
|
|
|
|
# define FIRST_32_SECOND_64(a, b) (a)
|
|
|
|
#endif
|
|
|
|
|
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>
|
2013-06-14 17:59:40 +08:00
|
|
|
class InternalMmapVector {
|
2013-02-26 21:30:27 +08:00
|
|
|
public:
|
2013-06-14 17:59:40 +08:00
|
|
|
explicit InternalMmapVector(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;
|
2013-06-14 17:59:40 +08:00
|
|
|
data_ = (T *)MmapOrDie(capacity_ * sizeof(T), "InternalMmapVector");
|
2013-02-26 21:30:27 +08:00
|
|
|
}
|
2013-06-14 17:59:40 +08:00
|
|
|
~InternalMmapVector() {
|
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);
|
|
|
|
}
|
|
|
|
data_[size_++] = element;
|
|
|
|
}
|
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_;
|
|
|
|
}
|
|
|
|
uptr capacity() const {
|
|
|
|
return capacity_;
|
|
|
|
}
|
2013-02-26 21:30:27 +08:00
|
|
|
|
2013-11-15 15:18:15 +08:00
|
|
|
void clear() { size_ = 0; }
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
// Disallow evil constructors.
|
2013-06-14 17:59:40 +08:00
|
|
|
InternalMmapVector(const InternalMmapVector&);
|
|
|
|
void operator=(const InternalMmapVector&);
|
2013-02-26 21:30:27 +08:00
|
|
|
|
|
|
|
T *data_;
|
|
|
|
uptr capacity_;
|
|
|
|
uptr size_;
|
|
|
|
};
|
2013-05-13 19:58:48 +08:00
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-08-26 21:24:43 +08:00
|
|
|
template<class Container, class Value, class Compare>
|
|
|
|
uptr InternalBinarySearch(const Container &v, uptr first, uptr last,
|
|
|
|
const Value &val, Compare comp) {
|
|
|
|
uptr not_found = last + 1;
|
|
|
|
while (last >= first) {
|
|
|
|
uptr mid = (first + last) / 2;
|
|
|
|
if (comp(v[mid], val))
|
|
|
|
first = mid + 1;
|
|
|
|
else if (comp(val, v[mid]))
|
|
|
|
last = mid - 1;
|
|
|
|
else
|
|
|
|
return mid;
|
|
|
|
}
|
|
|
|
return not_found;
|
|
|
|
}
|
|
|
|
|
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:
|
|
|
|
LoadedModule(const char *module_name, uptr base_address);
|
|
|
|
void addAddressRange(uptr beg, uptr end);
|
|
|
|
bool containsAddress(uptr address) const;
|
|
|
|
|
|
|
|
const char *full_name() const { return full_name_; }
|
|
|
|
uptr base_address() const { return base_address_; }
|
|
|
|
|
|
|
|
private:
|
|
|
|
struct AddressRange {
|
|
|
|
uptr beg;
|
|
|
|
uptr end;
|
|
|
|
};
|
|
|
|
char *full_name_;
|
|
|
|
uptr base_address_;
|
|
|
|
static const uptr kMaxNumberOfAddressRanges = 6;
|
|
|
|
AddressRange ranges_[kMaxNumberOfAddressRanges];
|
|
|
|
uptr n_ranges_;
|
|
|
|
};
|
|
|
|
|
|
|
|
// OS-dependent function that fills array with descriptions of at most
|
|
|
|
// "max_modules" currently loaded modules. Returns the number of
|
|
|
|
// initialized modules. If filter is nonzero, ignores modules for which
|
|
|
|
// filter(full_name) is false.
|
|
|
|
typedef bool (*string_predicate_t)(const char *);
|
|
|
|
uptr GetListOfModules(LoadedModule *modules, uptr max_modules,
|
|
|
|
string_predicate_t filter);
|
|
|
|
|
2013-10-11 22:19:14 +08:00
|
|
|
#if SANITIZER_POSIX
|
|
|
|
const uptr kPthreadDestructorIterations = 4;
|
|
|
|
#else
|
|
|
|
// Unused on Windows.
|
|
|
|
const uptr kPthreadDestructorIterations = 0;
|
|
|
|
#endif
|
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);
|
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
|