sanitizer_common.h revision 3dbeabb3446f203156ae03d957de9bdf50933ae4
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 = __WORDSIZE / 8;
25const uptr kWordSizeInBits = 8 * kWordSize;
26const uptr kPageSizeBits = 12;
27const uptr kPageSize = 1UL << kPageSizeBits;
28
29// Threads
30int GetPid();
31void GetThreadStackTopAndBottom(bool at_initialization, uptr *stack_top,
32                                uptr *stack_bottom);
33
34// Memory management
35void *MmapOrDie(uptr size, const char *mem_type);
36void UnmapOrDie(void *addr, uptr size);
37void *InternalAlloc(uptr size);
38void InternalFree(void *addr);
39
40void RawWrite(const char *buffer);
41void Printf(const char *format, ...);
42int SNPrintf(char *buffer, uptr length, const char *format, ...);
43void Report(const char *format, ...);
44
45// Opens the file 'file_name" and reads up to 'max_len' bytes.
46// The resulting buffer is mmaped and stored in '*buff'.
47// The size of the mmaped region is stored in '*buff_size',
48// Returns the number of read bytes or 0 if file can not be opened.
49uptr ReadFileToBuffer(const char *file_name, char **buff,
50                      uptr *buff_size, uptr max_len);
51const char *GetEnv(const char *name);
52
53// Bit twiddling.
54inline bool IsPowerOfTwo(uptr x) {
55  return (x & (x - 1)) == 0;
56}
57inline uptr RoundUpTo(uptr size, uptr boundary) {
58  // FIXME: Use CHECK here.
59  RAW_CHECK(IsPowerOfTwo(boundary));
60  return (size + boundary - 1) & ~(boundary - 1);
61}
62
63#if __WORDSIZE == 64
64# define FIRST_32_SECOND_64(a, b) (b)
65#else
66# define FIRST_32_SECOND_64(a, b) (a)
67#endif
68
69}  // namespace __sanitizer
70
71#endif  // SANITIZER_COMMON_H
72