blob: 9b4c880a0bf6a6e299a9c547e4084109a730f90f [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 Samsonovfe44fbd2012-06-07 05:38:26 +000065
Alexey Samsonovae1e1712012-06-15 06:08:19 +000066// Other
67void DisableCoreDumper();
68void DumpProcessMap();
Alexey Samsonov70afb912012-06-15 06:37:34 +000069void SleepForSeconds(int seconds);
70void NORETURN Exit(int exitcode);
71void NORETURN Abort();
72int Atexit(void (*function)(void));
Alexey Samsonove4a88982012-06-15 07:00:31 +000073void SortArray(uptr *array, uptr size);
Alexey Samsonovae1e1712012-06-15 06:08:19 +000074
Alexey Samsonove4a88982012-06-15 07:00:31 +000075// Atomics
76int AtomicInc(int *a);
77u16 AtomicExchange(u16 *a, u16 new_val);
78u8 AtomicExchange(u8 *a, u8 new_val);
79
80// Math
Alexey Samsonovee072902012-06-06 09:26:25 +000081inline bool IsPowerOfTwo(uptr x) {
82 return (x & (x - 1)) == 0;
83}
84inline uptr RoundUpTo(uptr size, uptr boundary) {
Alexey Samsonove4a88982012-06-15 07:00:31 +000085 CHECK(IsPowerOfTwo(boundary));
Alexey Samsonovee072902012-06-06 09:26:25 +000086 return (size + boundary - 1) & ~(boundary - 1);
87}
Alexey Samsonove4a88982012-06-15 07:00:31 +000088// Don't use std::min and std::max, to minimize dependency on libstdc++.
89template<class T> T Min(T a, T b) { return a < b ? a : b; }
90template<class T> T Max(T a, T b) { return a > b ? a : b; }
Alexey Samsonovbc3a7e32012-06-06 06:47:26 +000091
Alexey Samsonov156958d2012-06-15 13:09:52 +000092// Char handling
93inline bool IsSpace(int c) {
94 return (c == ' ') || (c == '\n') || (c == '\t') ||
95 (c == '\f') || (c == '\r') || (c == '\v');
96}
97inline bool IsDigit(int c) {
98 return (c >= '0') && (c <= '9');
99}
100inline int ToLower(int c) {
101 return (c >= 'A' && c <= 'Z') ? (c + 'a' - 'A') : c;
102}
103
Kostya Serebryanyc0bbfbf2012-06-06 16:33:46 +0000104#if __WORDSIZE == 64
105# define FIRST_32_SECOND_64(a, b) (b)
106#else
107# define FIRST_32_SECOND_64(a, b) (a)
108#endif
109
Alexey Samsonovbc3a7e32012-06-06 06:47:26 +0000110} // namespace __sanitizer
111
112#endif // SANITIZER_COMMON_H