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