blob: e6027b75f95aada5ab7c62949b710f1ae809b4d0 [file] [log] [blame]
Kostya Serebryany1e172b42011-11-30 01:07:02 +00001//===-- asan_internal.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 a part of AddressSanitizer, an address sanity checker.
11//
12// ASan-private header which defines various general utilities.
13//===----------------------------------------------------------------------===//
14#ifndef ASAN_INTERNAL_H
15#define ASAN_INTERNAL_H
16
Kostya Serebryanyd6567c52011-12-01 21:40:52 +000017#if !defined(__linux__) && !defined(__APPLE__)
18# error "This operating system is not supported by AddressSanitizer"
19#endif
20
Kostya Serebryany1e172b42011-11-30 01:07:02 +000021#include <stdint.h> // for __WORDSIZE
22#include <stdlib.h> // for size_t
Kostya Serebryany1e172b42011-11-30 01:07:02 +000023
Daniel Dunbar46166332011-12-02 01:32:27 +000024// If __WORDSIZE was undefined by the platform, define it in terms of the
25// compiler built-in __LP64__.
26#ifndef __WORDSIZE
27#if __LP64__
28#define __WORDSIZE 64
29#else
30#define __WORDSIZE 32
31#endif
32#endif
33
Kostya Serebryany1e172b42011-11-30 01:07:02 +000034#ifdef ANDROID
35#include <sys/atomics.h>
36#endif
37
Kostya Serebryany13ebae62011-12-27 21:57:12 +000038#if defined(__has_feature) && __has_feature(address_sanitizer)
Kostya Serebryany1e172b42011-11-30 01:07:02 +000039# error "The AddressSanitizer run-time should not be"
40 " instrumented by AddressSanitizer"
41#endif
42
Kostya Serebryanyc6f22232011-12-08 18:30:42 +000043// Build-time configuration options.
44
45// If set, sysinfo/sysinfo.h will be used to iterate over /proc/maps.
46#ifndef ASAN_USE_SYSINFO
Kostya Serebryanydf499b42012-01-05 00:44:33 +000047#ifdef __linux__
48# define ASAN_USE_SYSINFO 0
49#else
Kostya Serebryanyc6f22232011-12-08 18:30:42 +000050# define ASAN_USE_SYSINFO 1
51#endif
Kostya Serebryanydf499b42012-01-05 00:44:33 +000052#endif
Kostya Serebryanyc6f22232011-12-08 18:30:42 +000053
54// If set, asan will install its own SEGV signal handler.
55#ifndef ASAN_NEEDS_SEGV
56# define ASAN_NEEDS_SEGV 1
57#endif
58
59// If set, asan will intercept C++ exception api call(s).
60#ifndef ASAN_HAS_EXCEPTIONS
61# define ASAN_HAS_EXCEPTIONS 1
62#endif
63
64// If set, asan uses the values of SHADOW_SCALE and SHADOW_OFFSET
65// provided by the instrumented objects. Otherwise constants are used.
66#ifndef ASAN_FLEXIBLE_MAPPING_AND_OFFSET
67# define ASAN_FLEXIBLE_MAPPING_AND_OFFSET 0
68#endif
69
Kostya Serebryany1e172b42011-11-30 01:07:02 +000070// All internal functions in asan reside inside the __asan namespace
71// to avoid namespace collisions with the user programs.
72// Seperate namespace also makes it simpler to distinguish the asan run-time
73// functions from the instrumented user code in a profile.
74namespace __asan {
75
76class AsanThread;
77struct AsanStackTrace;
78
Kostya Serebryany218a9b72011-11-30 18:50:23 +000079// asan_rtl.cc
Kostya Serebryany1e172b42011-11-30 01:07:02 +000080void CheckFailed(const char *cond, const char *file, int line);
81void ShowStatsAndAbort();
82
Kostya Serebryany218a9b72011-11-30 18:50:23 +000083// asan_globals.cc
Kostya Serebryany1e172b42011-11-30 01:07:02 +000084bool DescribeAddrIfGlobal(uintptr_t addr);
85
Kostya Serebryany218a9b72011-11-30 18:50:23 +000086// asan_malloc_linux.cc / asan_malloc_mac.cc
Kostya Serebryany1e172b42011-11-30 01:07:02 +000087void ReplaceSystemMalloc();
88
Kostya Serebryanyde496f42011-12-28 22:58:01 +000089void OutOfMemoryMessageAndDie(const char *mem_type, size_t size);
90
Kostya Serebryany218a9b72011-11-30 18:50:23 +000091// asan_linux.cc / asan_mac.cc
Kostya Serebryany1e172b42011-11-30 01:07:02 +000092void *AsanDoesNotSupportStaticLinkage();
Kostya Serebryanyde496f42011-12-28 22:58:01 +000093int AsanOpenReadonly(const char* filename);
Kostya Serebryanyde496f42011-12-28 22:58:01 +000094
Kostya Serebryanya874fe52011-12-28 23:28:54 +000095void *AsanMmapFixedNoReserve(uintptr_t fixed_addr, size_t size);
96void *AsanMmapFixedReserve(uintptr_t fixed_addr, size_t size);
97void *AsanMprotect(uintptr_t fixed_addr, size_t size);
Kostya Serebryanyde496f42011-12-28 22:58:01 +000098void *AsanMmapSomewhereOrDie(size_t size, const char *where);
99void AsanUnmapOrDie(void *ptr, size_t size);
100
Kostya Serebryanyef14ff62012-01-06 02:12:25 +0000101void AsanDisableCoreDumper();
Kostya Serebryany9107c262012-01-06 19:11:09 +0000102void GetPcSpBp(void *context, uintptr_t *pc, uintptr_t *sp, uintptr_t *bp);
Kostya Serebryanyef14ff62012-01-06 02:12:25 +0000103
Kostya Serebryany0ecf5eb2012-01-09 23:11:26 +0000104size_t AsanRead(int fd, void *buf, size_t count);
105size_t AsanWrite(int fd, const void *buf, size_t count);
Kostya Serebryanyde496f42011-12-28 22:58:01 +0000106int AsanClose(int fd);
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000107
Kostya Serebryany4803ab92012-01-09 18:53:15 +0000108bool AsanInterceptsSignal(int signum);
Kostya Serebryanya7e760a2012-01-09 19:18:27 +0000109void InstallSignalHandlers();
Kostya Serebryany0ecf5eb2012-01-09 23:11:26 +0000110int GetPid();
Kostya Serebryany4803ab92012-01-09 18:53:15 +0000111
Kostya Serebryanydf499b42012-01-05 00:44:33 +0000112// Opens the file 'file_name" and reads up to 'max_len' bytes.
113// The resulting buffer is mmaped and stored in '*buff'.
114// The size of the mmaped region is stored in '*buff_size',
Kostya Serebryany0ecf5eb2012-01-09 23:11:26 +0000115// Returns the number of read bytes or 0 if file can not be opened.
116size_t ReadFileToBuffer(const char *file_name, char **buff,
117 size_t *buff_size, size_t max_len);
Kostya Serebryanydf499b42012-01-05 00:44:33 +0000118
Kostya Serebryany218a9b72011-11-30 18:50:23 +0000119// asan_printf.cc
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000120void RawWrite(const char *buffer);
121int SNPrint(char *buffer, size_t length, const char *format, ...);
122void Printf(const char *format, ...);
Kostya Serebryanydf499b42012-01-05 00:44:33 +0000123int SScanf(const char *str, const char *format, ...);
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000124void Report(const char *format, ...);
125
Kostya Serebryany2d8b3bd2011-12-02 18:42:04 +0000126// Don't use std::min and std::max, to minimize dependency on libstdc++.
127template<class T> T Min(T a, T b) { return a < b ? a : b; }
128template<class T> T Max(T a, T b) { return a > b ? a : b; }
129
Kostya Serebryany218a9b72011-11-30 18:50:23 +0000130// asan_poisoning.cc
131// Poisons the shadow memory for "size" bytes starting from "addr".
132void PoisonShadow(uintptr_t addr, size_t size, uint8_t value);
133// Poisons the shadow memory for "redzone_size" bytes starting from
134// "addr + size".
135void PoisonShadowPartialRightRedzone(uintptr_t addr,
136 uintptr_t size,
137 uintptr_t redzone_size,
138 uint8_t value);
139
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000140extern size_t FLAG_quarantine_size;
141extern int FLAG_demangle;
142extern bool FLAG_symbolize;
143extern int FLAG_v;
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000144extern size_t FLAG_redzone;
145extern int FLAG_debug;
146extern bool FLAG_poison_shadow;
147extern int FLAG_report_globals;
148extern size_t FLAG_malloc_context_size;
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000149extern bool FLAG_replace_str;
150extern bool FLAG_replace_intrin;
151extern bool FLAG_replace_cfallocator;
152extern bool FLAG_fast_unwind;
153extern bool FLAG_use_fake_stack;
154extern size_t FLAG_max_malloc_fill_size;
155extern int FLAG_exitcode;
156extern bool FLAG_allow_user_poisoning;
Kostya Serebryany4803ab92012-01-09 18:53:15 +0000157extern bool FLAG_handle_segv;
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000158
159extern int asan_inited;
160// Used to avoid infinite recursion in __asan_init().
161extern bool asan_init_is_running;
162
163enum LinkerInitialized { LINKER_INITIALIZED = 0 };
164
Kostya Serebryany0ecf5eb2012-01-09 23:11:26 +0000165void AsanDie();
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000166
167#define CHECK(cond) do { if (!(cond)) { \
168 CheckFailed(#cond, __FILE__, __LINE__); \
169}}while(0)
170
171#define RAW_CHECK_MSG(expr, msg) do { \
172 if (!(expr)) { \
173 RawWrite(msg); \
Kostya Serebryany0ecf5eb2012-01-09 23:11:26 +0000174 AsanDie(); \
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000175 } \
176} while (0)
177
178#define RAW_CHECK(expr) RAW_CHECK_MSG(expr, #expr)
179
180#define UNIMPLEMENTED() CHECK("unimplemented" && 0)
181
182#define ASAN_ARRAY_SIZE(a) (sizeof(a)/sizeof((a)[0]))
183
184const size_t kWordSize = __WORDSIZE / 8;
185const size_t kWordSizeInBits = 8 * kWordSize;
186const size_t kPageSizeBits = 12;
187const size_t kPageSize = 1UL << kPageSizeBits;
188
189#define GET_CALLER_PC() (uintptr_t)__builtin_return_address(0)
190#define GET_CURRENT_FRAME() (uintptr_t)__builtin_frame_address(0)
191
192#define GET_BP_PC_SP \
193 uintptr_t bp = GET_CURRENT_FRAME(); \
194 uintptr_t pc = GET_CALLER_PC(); \
195 uintptr_t local_stack; \
196 uintptr_t sp = (uintptr_t)&local_stack;
197
198// These magic values are written to shadow for better error reporting.
199const int kAsanHeapLeftRedzoneMagic = 0xfa;
200const int kAsanHeapRightRedzoneMagic = 0xfb;
201const int kAsanHeapFreeMagic = 0xfd;
202const int kAsanStackLeftRedzoneMagic = 0xf1;
203const int kAsanStackMidRedzoneMagic = 0xf2;
204const int kAsanStackRightRedzoneMagic = 0xf3;
205const int kAsanStackPartialRedzoneMagic = 0xf4;
206const int kAsanStackAfterReturnMagic = 0xf5;
207const int kAsanUserPoisonedMemoryMagic = 0xf7;
208const int kAsanGlobalRedzoneMagic = 0xf9;
Kostya Serebryany6b30e2c2011-12-15 17:41:30 +0000209const int kAsanInternalHeapMagic = 0xfe;
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000210
211static const uintptr_t kCurrentStackFrameMagic = 0x41B58AB3;
212static const uintptr_t kRetiredStackFrameMagic = 0x45E0360E;
213
Kostya Serebryanyde496f42011-12-28 22:58:01 +0000214// --------------------------- Bit twiddling ------- {{{1
215inline bool IsPowerOfTwo(size_t x) {
216 return (x & (x - 1)) == 0;
217}
218
219inline size_t RoundUpTo(size_t size, size_t boundary) {
220 CHECK(IsPowerOfTwo(boundary));
221 return (size + boundary - 1) & ~(boundary - 1);
222}
223
Kostya Serebryanyb89567c2011-12-02 21:02:20 +0000224// -------------------------- LowLevelAllocator ----- {{{1
225// A simple low-level memory allocator for internal use.
226class LowLevelAllocator {
227 public:
228 explicit LowLevelAllocator(LinkerInitialized) {}
229 // 'size' must be a power of two.
230 // Requires an external lock.
231 void *Allocate(size_t size);
232 private:
233 char *allocated_end_;
234 char *allocated_current_;
235};
236
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000237// -------------------------- Atomic ---------------- {{{1
238static inline int AtomicInc(int *a) {
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000239#ifdef ANDROID
240 return __atomic_inc(a) + 1;
241#else
242 return __sync_add_and_fetch(a, 1);
243#endif
244}
245
246static inline int AtomicDec(int *a) {
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000247#ifdef ANDROID
248 return __atomic_dec(a) - 1;
249#else
250 return __sync_add_and_fetch(a, -1);
251#endif
252}
253
254} // namespace __asan
255
256#endif // ASAN_INTERNAL_H