]> git.ipfire.org Git - thirdparty/gcc.git/blame - libsanitizer/sanitizer_common/sanitizer_common.h
[libsanitizer] merge from upstream r168514
[thirdparty/gcc.git] / libsanitizer / sanitizer_common / sanitizer_common.h
CommitLineData
f35db108
WM
1//===-- sanitizer_common.h --------------------------------------*- C++ -*-===//
2//
3// This file is distributed under the University of Illinois Open Source
4// License. See LICENSE.TXT for details.
5//
6//===----------------------------------------------------------------------===//
7//
8// This file is shared between AddressSanitizer and ThreadSanitizer
9// run-time libraries.
10// It declares common functions and classes that are used in both runtimes.
11// Implementation of some functions are provided in sanitizer_common, while
12// others must be defined by run-time library itself.
13//===----------------------------------------------------------------------===//
14#ifndef SANITIZER_COMMON_H
15#define SANITIZER_COMMON_H
16
17#include "sanitizer_internal_defs.h"
18
19namespace __sanitizer {
20
21// Constants.
e297eb60 22const uptr kWordSize = SANITIZER_WORDSIZE / 8;
f35db108 23const uptr kWordSizeInBits = 8 * kWordSize;
e297eb60
KS
24#if defined(__powerpc__) || defined(__powerpc64__)
25// Current PPC64 kernels use 64K pages sizes, but they can be
26// configured with 4K or even other sizes.
27// We may want to use getpagesize() or sysconf(_SC_PAGESIZE) here rather than
28// hardcoding the values, but today these values need to be compile-time
29// constants.
30const uptr kPageSize = 1UL << 16;
31const uptr kCacheLineSize = 128;
32const uptr kMmapGranularity = kPageSize;
33#elif !defined(_WIN32)
34const uptr kPageSize = 1UL << 12;
f35db108 35const uptr kCacheLineSize = 64;
f35db108
WM
36const uptr kMmapGranularity = kPageSize;
37#else
e297eb60
KS
38const uptr kPageSize = 1UL << 12;
39const uptr kCacheLineSize = 64;
f35db108
WM
40const uptr kMmapGranularity = 1UL << 16;
41#endif
42
43// Threads
44int GetPid();
45uptr GetTid();
46uptr GetThreadSelf();
47void GetThreadStackTopAndBottom(bool at_initialization, uptr *stack_top,
48 uptr *stack_bottom);
49
50// Memory management
51void *MmapOrDie(uptr size, const char *mem_type);
52void UnmapOrDie(void *addr, uptr size);
53void *MmapFixedNoReserve(uptr fixed_addr, uptr size);
54void *Mprotect(uptr fixed_addr, uptr size);
55// Used to check if we can map shadow memory to a fixed location.
56bool MemoryRangeIsAvailable(uptr range_start, uptr range_end);
57
58// Internal allocator
59void *InternalAlloc(uptr size);
60void InternalFree(void *p);
61// Given the pointer p into a valid allocated block,
62// returns a pointer to the beginning of the block.
63void *InternalAllocBlock(void *p);
64
65// InternalScopedBuffer can be used instead of large stack arrays to
66// keep frame size low.
67// FIXME: use InternalAlloc instead of MmapOrDie once
68// InternalAlloc is made libc-free.
69template<typename T>
70class InternalScopedBuffer {
71 public:
72 explicit InternalScopedBuffer(uptr cnt) {
73 cnt_ = cnt;
74 ptr_ = (T*)MmapOrDie(cnt * sizeof(T), "InternalScopedBuffer");
75 }
76 ~InternalScopedBuffer() {
77 UnmapOrDie(ptr_, cnt_ * sizeof(T));
78 }
79 T &operator[](uptr i) { return ptr_[i]; }
80 T *data() { return ptr_; }
81 uptr size() { return cnt_ * sizeof(T); }
82
83 private:
84 T *ptr_;
85 uptr cnt_;
86 // Disallow evil constructors.
87 InternalScopedBuffer(const InternalScopedBuffer&);
88 void operator=(const InternalScopedBuffer&);
89};
90
91// Simple low-level (mmap-based) allocator for internal use. Doesn't have
92// constructor, so all instances of LowLevelAllocator should be
93// linker initialized.
94class LowLevelAllocator {
95 public:
96 // Requires an external lock.
97 void *Allocate(uptr size);
98 private:
99 char *allocated_end_;
100 char *allocated_current_;
101};
102typedef void (*LowLevelAllocateCallback)(uptr ptr, uptr size);
103// Allows to register tool-specific callbacks for LowLevelAllocator.
104// Passing NULL removes the callback.
105void SetLowLevelAllocateCallback(LowLevelAllocateCallback callback);
106
107// IO
108void RawWrite(const char *buffer);
e297eb60 109bool PrintsToTty();
f35db108
WM
110void Printf(const char *format, ...);
111void Report(const char *format, ...);
112void SetPrintfAndReportCallback(void (*callback)(const char *));
113
114// Opens the file 'file_name" and reads up to 'max_len' bytes.
115// The resulting buffer is mmaped and stored in '*buff'.
116// The size of the mmaped region is stored in '*buff_size',
117// Returns the number of read bytes or 0 if file can not be opened.
118uptr ReadFileToBuffer(const char *file_name, char **buff,
119 uptr *buff_size, uptr max_len);
120// Maps given file to virtual memory, and returns pointer to it
121// (or NULL if the mapping failes). Stores the size of mmaped region
122// in '*buff_size'.
123void *MapFileToMemory(const char *file_name, uptr *buff_size);
124
125// OS
126void DisableCoreDumper();
127void DumpProcessMap();
e297eb60 128bool FileExists(const char *filename);
f35db108
WM
129const char *GetEnv(const char *name);
130const char *GetPwd();
131void ReExec();
132bool StackSizeIsUnlimited();
133void SetStackSizeLimitInBytes(uptr limit);
134
135// Other
136void SleepForSeconds(int seconds);
137void SleepForMillis(int millis);
138int Atexit(void (*function)(void));
139void SortArray(uptr *array, uptr size);
140
141// Exit
142void NORETURN Abort();
143void NORETURN Exit(int exitcode);
144void NORETURN Die();
145void NORETURN SANITIZER_INTERFACE_ATTRIBUTE
146CheckFailed(const char *file, int line, const char *cond, u64 v1, u64 v2);
147
148// Specific tools may override behavior of "Die" and "CheckFailed" functions
149// to do tool-specific job.
150void SetDieCallback(void (*callback)(void));
151typedef void (*CheckFailedCallbackType)(const char *, int, const char *,
152 u64, u64);
153void SetCheckFailedCallback(CheckFailedCallbackType callback);
154
155// Math
156INLINE bool IsPowerOfTwo(uptr x) {
157 return (x & (x - 1)) == 0;
158}
159INLINE uptr RoundUpTo(uptr size, uptr boundary) {
160 CHECK(IsPowerOfTwo(boundary));
161 return (size + boundary - 1) & ~(boundary - 1);
162}
163// Don't use std::min, std::max or std::swap, to minimize dependency
164// on libstdc++.
165template<class T> T Min(T a, T b) { return a < b ? a : b; }
166template<class T> T Max(T a, T b) { return a > b ? a : b; }
167template<class T> void Swap(T& a, T& b) {
168 T tmp = a;
169 a = b;
170 b = tmp;
171}
172
173// Char handling
174INLINE bool IsSpace(int c) {
175 return (c == ' ') || (c == '\n') || (c == '\t') ||
176 (c == '\f') || (c == '\r') || (c == '\v');
177}
178INLINE bool IsDigit(int c) {
179 return (c >= '0') && (c <= '9');
180}
181INLINE int ToLower(int c) {
182 return (c >= 'A' && c <= 'Z') ? (c + 'a' - 'A') : c;
183}
184
e297eb60 185#if SANITIZER_WORDSIZE == 64
f35db108
WM
186# define FIRST_32_SECOND_64(a, b) (b)
187#else
188# define FIRST_32_SECOND_64(a, b) (a)
189#endif
190
191} // namespace __sanitizer
192
193#endif // SANITIZER_COMMON_H