[Sanitizer] extend internal libc with stat/fstat/lstat functions
[blocksruntime.git] / lib / sanitizer_common / sanitizer_common.h
blobb39bd74e89540ba0f751e7dabc683aed1e9b16b5
1 //===-- sanitizer_common.h --------------------------------------*- C++ -*-===//
2 //
3 // The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This file is shared between AddressSanitizer and ThreadSanitizer
11 // run-time libraries.
12 // It declares common functions and classes that are used in both runtimes.
13 // Implementation of some functions are provided in sanitizer_common, while
14 // others must be defined by run-time library itself.
15 //===----------------------------------------------------------------------===//
16 #ifndef SANITIZER_COMMON_H
17 #define SANITIZER_COMMON_H
19 #include "sanitizer_internal_defs.h"
21 namespace __sanitizer {
23 // Constants.
24 const uptr kWordSize = SANITIZER_WORDSIZE / 8;
25 const uptr kWordSizeInBits = 8 * kWordSize;
27 #if defined(__powerpc__) || defined(__powerpc64__)
28 const uptr kCacheLineSize = 128;
29 #else
30 const uptr kCacheLineSize = 64;
31 #endif
33 extern const char *SanitizerToolName; // Can be changed by the tool.
35 uptr GetPageSize();
36 uptr GetPageSizeCached();
37 uptr GetMmapGranularity();
38 // Threads
39 int GetPid();
40 uptr GetTid();
41 uptr GetThreadSelf();
42 void GetThreadStackTopAndBottom(bool at_initialization, uptr *stack_top,
43 uptr *stack_bottom);
45 // Memory management
46 void *MmapOrDie(uptr size, const char *mem_type);
47 void UnmapOrDie(void *addr, uptr size);
48 void *MmapFixedNoReserve(uptr fixed_addr, uptr size);
49 void *MmapFixedOrDie(uptr fixed_addr, uptr size);
50 void *Mprotect(uptr fixed_addr, uptr size);
51 // Map aligned chunk of address space; size and alignment are powers of two.
52 void *MmapAlignedOrDie(uptr size, uptr alignment, const char *mem_type);
53 // Used to check if we can map shadow memory to a fixed location.
54 bool MemoryRangeIsAvailable(uptr range_start, uptr range_end);
55 void FlushUnneededShadowMemory(uptr addr, uptr size);
57 // Internal allocator
58 void *InternalAlloc(uptr size);
59 void InternalFree(void *p);
61 // InternalScopedBuffer can be used instead of large stack arrays to
62 // keep frame size low.
63 // FIXME: use InternalAlloc instead of MmapOrDie once
64 // InternalAlloc is made libc-free.
65 template<typename T>
66 class InternalScopedBuffer {
67 public:
68 explicit InternalScopedBuffer(uptr cnt) {
69 cnt_ = cnt;
70 ptr_ = (T*)MmapOrDie(cnt * sizeof(T), "InternalScopedBuffer");
72 ~InternalScopedBuffer() {
73 UnmapOrDie(ptr_, cnt_ * sizeof(T));
75 T &operator[](uptr i) { return ptr_[i]; }
76 T *data() { return ptr_; }
77 uptr size() { return cnt_ * sizeof(T); }
79 private:
80 T *ptr_;
81 uptr cnt_;
82 // Disallow evil constructors.
83 InternalScopedBuffer(const InternalScopedBuffer&);
84 void operator=(const InternalScopedBuffer&);
87 // Simple low-level (mmap-based) allocator for internal use. Doesn't have
88 // constructor, so all instances of LowLevelAllocator should be
89 // linker initialized.
90 class LowLevelAllocator {
91 public:
92 // Requires an external lock.
93 void *Allocate(uptr size);
94 private:
95 char *allocated_end_;
96 char *allocated_current_;
98 typedef void (*LowLevelAllocateCallback)(uptr ptr, uptr size);
99 // Allows to register tool-specific callbacks for LowLevelAllocator.
100 // Passing NULL removes the callback.
101 void SetLowLevelAllocateCallback(LowLevelAllocateCallback callback);
103 // IO
104 void RawWrite(const char *buffer);
105 bool PrintsToTty();
106 void Printf(const char *format, ...);
107 void Report(const char *format, ...);
108 void SetPrintfAndReportCallback(void (*callback)(const char *));
110 fd_t OpenFile(const char *filename, bool write);
111 // Opens the file 'file_name" and reads up to 'max_len' bytes.
112 // The resulting buffer is mmaped and stored in '*buff'.
113 // The size of the mmaped region is stored in '*buff_size',
114 // Returns the number of read bytes or 0 if file can not be opened.
115 uptr ReadFileToBuffer(const char *file_name, char **buff,
116 uptr *buff_size, uptr max_len);
117 // Maps given file to virtual memory, and returns pointer to it
118 // (or NULL if the mapping failes). Stores the size of mmaped region
119 // in '*buff_size'.
120 void *MapFileToMemory(const char *file_name, uptr *buff_size);
122 // OS
123 void DisableCoreDumper();
124 void DumpProcessMap();
125 bool FileExists(const char *filename);
126 const char *GetEnv(const char *name);
127 const char *GetPwd();
128 void ReExec();
129 bool StackSizeIsUnlimited();
130 void SetStackSizeLimitInBytes(uptr limit);
131 void PrepareForSandboxing();
133 // Other
134 void SleepForSeconds(int seconds);
135 void SleepForMillis(int millis);
136 int Atexit(void (*function)(void));
137 void SortArray(uptr *array, uptr size);
139 // Exit
140 void NORETURN Abort();
141 void NORETURN Exit(int exitcode);
142 void NORETURN Die();
143 void NORETURN SANITIZER_INTERFACE_ATTRIBUTE
144 CheckFailed(const char *file, int line, const char *cond, u64 v1, u64 v2);
146 // Set the name of the current thread to 'name', return true on succees.
147 // The name may be truncated to a system-dependent limit.
148 bool SanitizerSetThreadName(const char *name);
149 // Get the name of the current thread (no more than max_len bytes),
150 // return true on succees. name should have space for at least max_len+1 bytes.
151 bool SanitizerGetThreadName(char *name, int max_len);
153 // Specific tools may override behavior of "Die" and "CheckFailed" functions
154 // to do tool-specific job.
155 void SetDieCallback(void (*callback)(void));
156 typedef void (*CheckFailedCallbackType)(const char *, int, const char *,
157 u64, u64);
158 void SetCheckFailedCallback(CheckFailedCallbackType callback);
160 // Math
161 INLINE bool IsPowerOfTwo(uptr x) {
162 return (x & (x - 1)) == 0;
164 INLINE uptr RoundUpTo(uptr size, uptr boundary) {
165 CHECK(IsPowerOfTwo(boundary));
166 return (size + boundary - 1) & ~(boundary - 1);
168 INLINE uptr RoundDownTo(uptr x, uptr boundary) {
169 return x & ~(boundary - 1);
171 INLINE bool IsAligned(uptr a, uptr alignment) {
172 return (a & (alignment - 1)) == 0;
174 // Don't use std::min, std::max or std::swap, to minimize dependency
175 // on libstdc++.
176 template<class T> T Min(T a, T b) { return a < b ? a : b; }
177 template<class T> T Max(T a, T b) { return a > b ? a : b; }
178 template<class T> void Swap(T& a, T& b) {
179 T tmp = a;
180 a = b;
181 b = tmp;
184 // Char handling
185 INLINE bool IsSpace(int c) {
186 return (c == ' ') || (c == '\n') || (c == '\t') ||
187 (c == '\f') || (c == '\r') || (c == '\v');
189 INLINE bool IsDigit(int c) {
190 return (c >= '0') && (c <= '9');
192 INLINE int ToLower(int c) {
193 return (c >= 'A' && c <= 'Z') ? (c + 'a' - 'A') : c;
196 #if SANITIZER_WORDSIZE == 64
197 # define FIRST_32_SECOND_64(a, b) (b)
198 #else
199 # define FIRST_32_SECOND_64(a, b) (a)
200 #endif
202 } // namespace __sanitizer
204 #endif // SANITIZER_COMMON_H