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"
|
|
|
|
|
|
|
|
namespace __sanitizer {
|
|
|
|
|
2012-06-06 17:26:25 +08:00
|
|
|
// Constants.
|
|
|
|
const uptr kWordSize = __WORDSIZE / 8;
|
|
|
|
const uptr kWordSizeInBits = 8 * kWordSize;
|
|
|
|
const uptr kPageSizeBits = 12;
|
|
|
|
const uptr kPageSize = 1UL << kPageSizeBits;
|
2012-06-22 21:00:50 +08:00
|
|
|
const uptr kCacheLineSize = 64;
|
2012-06-15 15:29:14 +08:00
|
|
|
#ifndef _WIN32
|
|
|
|
const uptr kMmapGranularity = kPageSize;
|
|
|
|
#else
|
|
|
|
const uptr kMmapGranularity = 1UL << 16;
|
|
|
|
#endif
|
2012-06-06 17:26:25 +08:00
|
|
|
|
2012-06-07 15:13:46 +08:00
|
|
|
// Threads
|
2012-06-06 17:26:25 +08:00
|
|
|
int GetPid();
|
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);
|
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);
|
|
|
|
void *Mprotect(uptr fixed_addr, uptr size);
|
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-06-14 22:42:58 +08:00
|
|
|
|
|
|
|
// Internal allocator
|
2012-06-07 16:52:56 +08:00
|
|
|
void *InternalAlloc(uptr size);
|
2012-06-25 23:09:24 +08:00
|
|
|
void InternalFree(void *p);
|
|
|
|
// Given the pointer p into a valid allocated block,
|
|
|
|
// returns a pointer to the beginning of the block.
|
|
|
|
void *InternalAllocBlock(void *p);
|
2012-06-06 17:26:25 +08:00
|
|
|
|
2012-08-21 16:13:37 +08:00
|
|
|
// InternalScopedBuffer can be used instead of large stack arrays to
|
|
|
|
// keep frame size low.
|
|
|
|
template<typename T>
|
|
|
|
class InternalScopedBuffer {
|
|
|
|
public:
|
|
|
|
explicit InternalScopedBuffer(uptr cnt) {
|
|
|
|
cnt_ = cnt;
|
|
|
|
ptr_ = (T*)InternalAlloc(cnt * sizeof(T));
|
|
|
|
}
|
|
|
|
~InternalScopedBuffer() {
|
|
|
|
InternalFree(ptr_);
|
|
|
|
}
|
2012-08-22 15:25:52 +08:00
|
|
|
operator T*() { return ptr_; }
|
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&);
|
|
|
|
};
|
|
|
|
|
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-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 *));
|
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'.
|
|
|
|
// 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);
|
|
|
|
|
2012-06-14 22:07:21 +08:00
|
|
|
const char *GetEnv(const char *name);
|
2012-06-18 16:44:30 +08:00
|
|
|
const char *GetPwd();
|
2012-06-07 13:38:26 +08:00
|
|
|
|
2012-06-15 14:08:19 +08:00
|
|
|
// Other
|
|
|
|
void DisableCoreDumper();
|
|
|
|
void DumpProcessMap();
|
2012-06-15 14:37:34 +08:00
|
|
|
void SleepForSeconds(int seconds);
|
2012-06-18 16:44:30 +08:00
|
|
|
void SleepForMillis(int millis);
|
2012-06-15 14:37:34 +08:00
|
|
|
void NORETURN Exit(int exitcode);
|
|
|
|
void NORETURN Abort();
|
|
|
|
int Atexit(void (*function)(void));
|
2012-06-15 15:00:31 +08:00
|
|
|
void SortArray(uptr *array, uptr size);
|
2012-06-15 14:08:19 +08:00
|
|
|
|
2012-06-15 15:00:31 +08:00
|
|
|
// Math
|
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;
|
|
|
|
}
|
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);
|
|
|
|
}
|
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-06-07 00:33:46 +08:00
|
|
|
#if __WORDSIZE == 64
|
|
|
|
# define FIRST_32_SECOND_64(a, b) (b)
|
|
|
|
#else
|
|
|
|
# define FIRST_32_SECOND_64(a, b) (a)
|
|
|
|
#endif
|
|
|
|
|
2012-06-06 14:47:26 +08:00
|
|
|
} // namespace __sanitizer
|
|
|
|
|
|
|
|
#endif // SANITIZER_COMMON_H
|