* config/pa/constraints.md: Adjust unused letters. Change "T"
[official-gcc.git] / libsanitizer / sanitizer_common / sanitizer_common.h
blob6b104884342301083988d20c6ae85818a46f99a2
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
17 #include "sanitizer_internal_defs.h"
19 namespace __sanitizer {
21 // Constants.
22 const uptr kWordSize = SANITIZER_WORDSIZE / 8;
23 const uptr kWordSizeInBits = 8 * kWordSize;
25 #if defined(__powerpc__) || defined(__powerpc64__)
26 const uptr kCacheLineSize = 128;
27 #else
28 const uptr kCacheLineSize = 64;
29 #endif
31 uptr GetPageSize();
32 uptr GetPageSizeCached();
33 uptr GetMmapGranularity();
34 // Threads
35 int GetPid();
36 uptr GetTid();
37 uptr GetThreadSelf();
38 void GetThreadStackTopAndBottom(bool at_initialization, uptr *stack_top,
39 uptr *stack_bottom);
41 // Memory management
42 void *MmapOrDie(uptr size, const char *mem_type);
43 void UnmapOrDie(void *addr, uptr size);
44 void *MmapFixedNoReserve(uptr fixed_addr, uptr size);
45 void *MmapFixedOrDie(uptr fixed_addr, uptr size);
46 void *Mprotect(uptr fixed_addr, uptr size);
47 // Map aligned chunk of address space; size and alignment are powers of two.
48 void *MmapAlignedOrDie(uptr size, uptr alignment, const char *mem_type);
49 // Used to check if we can map shadow memory to a fixed location.
50 bool MemoryRangeIsAvailable(uptr range_start, uptr range_end);
51 void FlushUnneededShadowMemory(uptr addr, uptr size);
53 // Internal allocator
54 void *InternalAlloc(uptr size);
55 void InternalFree(void *p);
57 // InternalScopedBuffer can be used instead of large stack arrays to
58 // keep frame size low.
59 // FIXME: use InternalAlloc instead of MmapOrDie once
60 // InternalAlloc is made libc-free.
61 template<typename T>
62 class InternalScopedBuffer {
63 public:
64 explicit InternalScopedBuffer(uptr cnt) {
65 cnt_ = cnt;
66 ptr_ = (T*)MmapOrDie(cnt * sizeof(T), "InternalScopedBuffer");
68 ~InternalScopedBuffer() {
69 UnmapOrDie(ptr_, cnt_ * sizeof(T));
71 T &operator[](uptr i) { return ptr_[i]; }
72 T *data() { return ptr_; }
73 uptr size() { return cnt_ * sizeof(T); }
75 private:
76 T *ptr_;
77 uptr cnt_;
78 // Disallow evil constructors.
79 InternalScopedBuffer(const InternalScopedBuffer&);
80 void operator=(const InternalScopedBuffer&);
83 // Simple low-level (mmap-based) allocator for internal use. Doesn't have
84 // constructor, so all instances of LowLevelAllocator should be
85 // linker initialized.
86 class LowLevelAllocator {
87 public:
88 // Requires an external lock.
89 void *Allocate(uptr size);
90 private:
91 char *allocated_end_;
92 char *allocated_current_;
94 typedef void (*LowLevelAllocateCallback)(uptr ptr, uptr size);
95 // Allows to register tool-specific callbacks for LowLevelAllocator.
96 // Passing NULL removes the callback.
97 void SetLowLevelAllocateCallback(LowLevelAllocateCallback callback);
99 // IO
100 void RawWrite(const char *buffer);
101 bool PrintsToTty();
102 void Printf(const char *format, ...);
103 void Report(const char *format, ...);
104 void SetPrintfAndReportCallback(void (*callback)(const char *));
106 // Opens the file 'file_name" and reads up to 'max_len' bytes.
107 // The resulting buffer is mmaped and stored in '*buff'.
108 // The size of the mmaped region is stored in '*buff_size',
109 // Returns the number of read bytes or 0 if file can not be opened.
110 uptr ReadFileToBuffer(const char *file_name, char **buff,
111 uptr *buff_size, uptr max_len);
112 // Maps given file to virtual memory, and returns pointer to it
113 // (or NULL if the mapping failes). Stores the size of mmaped region
114 // in '*buff_size'.
115 void *MapFileToMemory(const char *file_name, uptr *buff_size);
117 // OS
118 void DisableCoreDumper();
119 void DumpProcessMap();
120 bool FileExists(const char *filename);
121 const char *GetEnv(const char *name);
122 const char *GetPwd();
123 void ReExec();
124 bool StackSizeIsUnlimited();
125 void SetStackSizeLimitInBytes(uptr limit);
126 void PrepareForSandboxing();
128 // Other
129 void SleepForSeconds(int seconds);
130 void SleepForMillis(int millis);
131 int Atexit(void (*function)(void));
132 void SortArray(uptr *array, uptr size);
134 // Exit
135 void NORETURN Abort();
136 void NORETURN Exit(int exitcode);
137 void NORETURN Die();
138 void NORETURN SANITIZER_INTERFACE_ATTRIBUTE
139 CheckFailed(const char *file, int line, const char *cond, u64 v1, u64 v2);
141 // Set the name of the current thread to 'name', return true on succees.
142 // The name may be truncated to a system-dependent limit.
143 bool SanitizerSetThreadName(const char *name);
144 // Get the name of the current thread (no more than max_len bytes),
145 // return true on succees. name should have space for at least max_len+1 bytes.
146 bool SanitizerGetThreadName(char *name, int max_len);
148 // Specific tools may override behavior of "Die" and "CheckFailed" functions
149 // to do tool-specific job.
150 void SetDieCallback(void (*callback)(void));
151 typedef void (*CheckFailedCallbackType)(const char *, int, const char *,
152 u64, u64);
153 void SetCheckFailedCallback(CheckFailedCallbackType callback);
155 // Math
156 INLINE bool IsPowerOfTwo(uptr x) {
157 return (x & (x - 1)) == 0;
159 INLINE uptr RoundUpTo(uptr size, uptr boundary) {
160 CHECK(IsPowerOfTwo(boundary));
161 return (size + boundary - 1) & ~(boundary - 1);
163 INLINE uptr RoundDownTo(uptr x, uptr boundary) {
164 return x & ~(boundary - 1);
166 INLINE bool IsAligned(uptr a, uptr alignment) {
167 return (a & (alignment - 1)) == 0;
169 // Don't use std::min, std::max or std::swap, to minimize dependency
170 // on libstdc++.
171 template<class T> T Min(T a, T b) { return a < b ? a : b; }
172 template<class T> T Max(T a, T b) { return a > b ? a : b; }
173 template<class T> void Swap(T& a, T& b) {
174 T tmp = a;
175 a = b;
176 b = tmp;
179 // Char handling
180 INLINE bool IsSpace(int c) {
181 return (c == ' ') || (c == '\n') || (c == '\t') ||
182 (c == '\f') || (c == '\r') || (c == '\v');
184 INLINE bool IsDigit(int c) {
185 return (c >= '0') && (c <= '9');
187 INLINE int ToLower(int c) {
188 return (c >= 'A' && c <= 'Z') ? (c + 'a' - 'A') : c;
191 #if SANITIZER_WORDSIZE == 64
192 # define FIRST_32_SECOND_64(a, b) (b)
193 #else
194 # define FIRST_32_SECOND_64(a, b) (a)
195 #endif
197 } // namespace __sanitizer
199 #endif // SANITIZER_COMMON_H