blob: 7faaf577b35ab459002ec56510f92fa9da9d600a [file] [log] [blame]
Alexey Samsonovbc3a7e32012-06-06 06:47:26 +00001//===-- 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.
Alexey Samsonovee072902012-06-06 09:26:25 +000012// It declares common functions and classes that are used in both runtimes.
Alexey Samsonovbc3a7e32012-06-06 06:47:26 +000013// 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
Alexey Samsonovee072902012-06-06 09:26:25 +000023// Constants.
24const uptr kWordSize = __WORDSIZE / 8;
25const uptr kWordSizeInBits = 8 * kWordSize;
26const uptr kPageSizeBits = 12;
27const uptr kPageSize = 1UL << kPageSizeBits;
Alexey Samsonov40e51282012-06-15 07:29:14 +000028#ifndef _WIN32
29const uptr kMmapGranularity = kPageSize;
30#else
31const uptr kMmapGranularity = 1UL << 16;
32#endif
Alexey Samsonovee072902012-06-06 09:26:25 +000033
Alexey Samsonov4b1f1032012-06-07 07:13:46 +000034// Threads
Alexey Samsonovee072902012-06-06 09:26:25 +000035int GetPid();
Alexey Samsonov70afb912012-06-15 06:37:34 +000036uptr GetThreadSelf();
Alexey Samsonovcf4d3a02012-06-07 07:32:00 +000037void GetThreadStackTopAndBottom(bool at_initialization, uptr *stack_top,
Alexey Samsonov4b1f1032012-06-07 07:13:46 +000038 uptr *stack_bottom);
Alexey Samsonov40d5b772012-06-06 16:15:07 +000039
40// Memory management
41void *MmapOrDie(uptr size, const char *mem_type);
Alexey Samsonovee072902012-06-06 09:26:25 +000042void UnmapOrDie(void *addr, uptr size);
Alexey Samsonovc70d1082012-06-14 14:42:58 +000043void *MmapFixedNoReserve(uptr fixed_addr, uptr size);
44void *Mprotect(uptr fixed_addr, uptr size);
Alexey Samsonov40e51282012-06-15 07:29:14 +000045// Used to check if we can map shadow memory to a fixed location.
46bool MemoryRangeIsAvailable(uptr range_start, uptr range_end);
Alexey Samsonovc70d1082012-06-14 14:42:58 +000047
48// Internal allocator
Alexey Samsonov3a6ddb82012-06-07 08:52:56 +000049void *InternalAlloc(uptr size);
50void InternalFree(void *addr);
Alexey Samsonovee072902012-06-06 09:26:25 +000051
Alexey Samsonovc70d1082012-06-14 14:42:58 +000052// IO
Alexey Samsonov4b1f1032012-06-07 07:13:46 +000053void RawWrite(const char *buffer);
Alexey Samsonovd323f4e2012-06-06 13:58:39 +000054void Printf(const char *format, ...);
55int SNPrintf(char *buffer, uptr length, const char *format, ...);
56void Report(const char *format, ...);
Alexey Samsonov51ae9832012-06-06 13:11:29 +000057
Alexey Samsonovfe44fbd2012-06-07 05:38:26 +000058// Opens the file 'file_name" and reads up to 'max_len' bytes.
59// The resulting buffer is mmaped and stored in '*buff'.
60// The size of the mmaped region is stored in '*buff_size',
61// Returns the number of read bytes or 0 if file can not be opened.
62uptr ReadFileToBuffer(const char *file_name, char **buff,
63 uptr *buff_size, uptr max_len);
Alexey Samsonov0c53a382012-06-14 14:07:21 +000064const char *GetEnv(const char *name);
Alexey Samsonov58a3c582012-06-18 08:44:30 +000065const char *GetPwd();
Alexey Samsonovfe44fbd2012-06-07 05:38:26 +000066
Alexey Samsonovae1e1712012-06-15 06:08:19 +000067// Other
68void DisableCoreDumper();
69void DumpProcessMap();
Alexey Samsonov70afb912012-06-15 06:37:34 +000070void SleepForSeconds(int seconds);
Alexey Samsonov58a3c582012-06-18 08:44:30 +000071void SleepForMillis(int millis);
Alexey Samsonov70afb912012-06-15 06:37:34 +000072void NORETURN Exit(int exitcode);
73void NORETURN Abort();
74int Atexit(void (*function)(void));
Alexey Samsonove4a88982012-06-15 07:00:31 +000075void SortArray(uptr *array, uptr size);
Alexey Samsonovae1e1712012-06-15 06:08:19 +000076
Alexey Samsonove4a88982012-06-15 07:00:31 +000077// Atomics
78int AtomicInc(int *a);
79u16 AtomicExchange(u16 *a, u16 new_val);
80u8 AtomicExchange(u8 *a, u8 new_val);
81
82// Math
Alexey Samsonovee072902012-06-06 09:26:25 +000083inline bool IsPowerOfTwo(uptr x) {
84 return (x & (x - 1)) == 0;
85}
86inline uptr RoundUpTo(uptr size, uptr boundary) {
Alexey Samsonove4a88982012-06-15 07:00:31 +000087 CHECK(IsPowerOfTwo(boundary));
Alexey Samsonovee072902012-06-06 09:26:25 +000088 return (size + boundary - 1) & ~(boundary - 1);
89}
Alexey Samsonove4a88982012-06-15 07:00:31 +000090// Don't use std::min and std::max, to minimize dependency on libstdc++.
91template<class T> T Min(T a, T b) { return a < b ? a : b; }
92template<class T> T Max(T a, T b) { return a > b ? a : b; }
Alexey Samsonovbc3a7e32012-06-06 06:47:26 +000093
Alexey Samsonov156958d2012-06-15 13:09:52 +000094// Char handling
95inline bool IsSpace(int c) {
96 return (c == ' ') || (c == '\n') || (c == '\t') ||
97 (c == '\f') || (c == '\r') || (c == '\v');
98}
99inline bool IsDigit(int c) {
100 return (c >= '0') && (c <= '9');
101}
102inline int ToLower(int c) {
103 return (c >= 'A' && c <= 'Z') ? (c + 'a' - 'A') : c;
104}
105
Kostya Serebryanyc0bbfbf2012-06-06 16:33:46 +0000106#if __WORDSIZE == 64
107# define FIRST_32_SECOND_64(a, b) (b)
108#else
109# define FIRST_32_SECOND_64(a, b) (a)
110#endif
111
Alexey Samsonovbc3a7e32012-06-06 06:47:26 +0000112} // namespace __sanitizer
113
114#endif // SANITIZER_COMMON_H