blob: d0790a7c8b7b5d0c60fbbdda4c27ae72c3908b97 [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
23#include <unistd.h> // for _exit
24
Daniel Dunbar46166332011-12-02 01:32:27 +000025// If __WORDSIZE was undefined by the platform, define it in terms of the
26// compiler built-in __LP64__.
27#ifndef __WORDSIZE
28#if __LP64__
29#define __WORDSIZE 64
30#else
31#define __WORDSIZE 32
32#endif
33#endif
34
Kostya Serebryany1e172b42011-11-30 01:07:02 +000035#ifdef ANDROID
36#include <sys/atomics.h>
37#endif
38
Kostya Serebryany13ebae62011-12-27 21:57:12 +000039#if defined(__has_feature) && __has_feature(address_sanitizer)
Kostya Serebryany1e172b42011-11-30 01:07:02 +000040# error "The AddressSanitizer run-time should not be"
41 " instrumented by AddressSanitizer"
42#endif
43
Kostya Serebryanyc6f22232011-12-08 18:30:42 +000044// Build-time configuration options.
45
46// If set, sysinfo/sysinfo.h will be used to iterate over /proc/maps.
47#ifndef ASAN_USE_SYSINFO
48# define ASAN_USE_SYSINFO 1
49#endif
50
51// If set, asan will install its own SEGV signal handler.
52#ifndef ASAN_NEEDS_SEGV
53# define ASAN_NEEDS_SEGV 1
54#endif
55
56// If set, asan will intercept C++ exception api call(s).
57#ifndef ASAN_HAS_EXCEPTIONS
58# define ASAN_HAS_EXCEPTIONS 1
59#endif
60
61// If set, asan uses the values of SHADOW_SCALE and SHADOW_OFFSET
62// provided by the instrumented objects. Otherwise constants are used.
63#ifndef ASAN_FLEXIBLE_MAPPING_AND_OFFSET
64# define ASAN_FLEXIBLE_MAPPING_AND_OFFSET 0
65#endif
66
Kostya Serebryany1e172b42011-11-30 01:07:02 +000067// All internal functions in asan reside inside the __asan namespace
68// to avoid namespace collisions with the user programs.
69// Seperate namespace also makes it simpler to distinguish the asan run-time
70// functions from the instrumented user code in a profile.
71namespace __asan {
72
73class AsanThread;
74struct AsanStackTrace;
75
Kostya Serebryany218a9b72011-11-30 18:50:23 +000076// asan_rtl.cc
Kostya Serebryany1e172b42011-11-30 01:07:02 +000077void CheckFailed(const char *cond, const char *file, int line);
78void ShowStatsAndAbort();
79
Kostya Serebryany218a9b72011-11-30 18:50:23 +000080// asan_globals.cc
Kostya Serebryany1e172b42011-11-30 01:07:02 +000081bool DescribeAddrIfGlobal(uintptr_t addr);
82
Kostya Serebryany218a9b72011-11-30 18:50:23 +000083// asan_malloc_linux.cc / asan_malloc_mac.cc
Kostya Serebryany1e172b42011-11-30 01:07:02 +000084void ReplaceSystemMalloc();
85
Kostya Serebryanyde496f42011-12-28 22:58:01 +000086void OutOfMemoryMessageAndDie(const char *mem_type, size_t size);
87
Kostya Serebryany218a9b72011-11-30 18:50:23 +000088// asan_linux.cc / asan_mac.cc
Kostya Serebryany1e172b42011-11-30 01:07:02 +000089void *AsanDoesNotSupportStaticLinkage();
Kostya Serebryanyde496f42011-12-28 22:58:01 +000090int AsanOpenReadonly(const char* filename);
Kostya Serebryanyde496f42011-12-28 22:58:01 +000091
Kostya Serebryanya874fe52011-12-28 23:28:54 +000092void *AsanMmapFixedNoReserve(uintptr_t fixed_addr, size_t size);
93void *AsanMmapFixedReserve(uintptr_t fixed_addr, size_t size);
94void *AsanMprotect(uintptr_t fixed_addr, size_t size);
Kostya Serebryanyde496f42011-12-28 22:58:01 +000095void *AsanMmapSomewhereOrDie(size_t size, const char *where);
96void AsanUnmapOrDie(void *ptr, size_t size);
97
98ssize_t AsanRead(int fd, void *buf, size_t count);
99ssize_t AsanWrite(int fd, const void *buf, size_t count);
100int AsanClose(int fd);
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000101
Kostya Serebryany218a9b72011-11-30 18:50:23 +0000102// asan_printf.cc
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000103void RawWrite(const char *buffer);
104int SNPrint(char *buffer, size_t length, const char *format, ...);
105void Printf(const char *format, ...);
106void Report(const char *format, ...);
107
Kostya Serebryany2d8b3bd2011-12-02 18:42:04 +0000108// Don't use std::min and std::max, to minimize dependency on libstdc++.
109template<class T> T Min(T a, T b) { return a < b ? a : b; }
110template<class T> T Max(T a, T b) { return a > b ? a : b; }
111
Kostya Serebryany218a9b72011-11-30 18:50:23 +0000112// asan_poisoning.cc
113// Poisons the shadow memory for "size" bytes starting from "addr".
114void PoisonShadow(uintptr_t addr, size_t size, uint8_t value);
115// Poisons the shadow memory for "redzone_size" bytes starting from
116// "addr + size".
117void PoisonShadowPartialRightRedzone(uintptr_t addr,
118 uintptr_t size,
119 uintptr_t redzone_size,
120 uint8_t value);
121
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000122extern size_t FLAG_quarantine_size;
123extern int FLAG_demangle;
124extern bool FLAG_symbolize;
125extern int FLAG_v;
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000126extern size_t FLAG_redzone;
127extern int FLAG_debug;
128extern bool FLAG_poison_shadow;
129extern int FLAG_report_globals;
130extern size_t FLAG_malloc_context_size;
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000131extern bool FLAG_replace_str;
132extern bool FLAG_replace_intrin;
133extern bool FLAG_replace_cfallocator;
134extern bool FLAG_fast_unwind;
135extern bool FLAG_use_fake_stack;
136extern size_t FLAG_max_malloc_fill_size;
137extern int FLAG_exitcode;
138extern bool FLAG_allow_user_poisoning;
139
140extern int asan_inited;
141// Used to avoid infinite recursion in __asan_init().
142extern bool asan_init_is_running;
143
144enum LinkerInitialized { LINKER_INITIALIZED = 0 };
145
146#ifndef ASAN_DIE
147#define ASAN_DIE _exit(FLAG_exitcode)
148#endif // ASAN_DIE
149
150#define CHECK(cond) do { if (!(cond)) { \
151 CheckFailed(#cond, __FILE__, __LINE__); \
152}}while(0)
153
154#define RAW_CHECK_MSG(expr, msg) do { \
155 if (!(expr)) { \
156 RawWrite(msg); \
157 ASAN_DIE; \
158 } \
159} while (0)
160
161#define RAW_CHECK(expr) RAW_CHECK_MSG(expr, #expr)
162
163#define UNIMPLEMENTED() CHECK("unimplemented" && 0)
164
165#define ASAN_ARRAY_SIZE(a) (sizeof(a)/sizeof((a)[0]))
166
167const size_t kWordSize = __WORDSIZE / 8;
168const size_t kWordSizeInBits = 8 * kWordSize;
169const size_t kPageSizeBits = 12;
170const size_t kPageSize = 1UL << kPageSizeBits;
171
172#define GET_CALLER_PC() (uintptr_t)__builtin_return_address(0)
173#define GET_CURRENT_FRAME() (uintptr_t)__builtin_frame_address(0)
174
175#define GET_BP_PC_SP \
176 uintptr_t bp = GET_CURRENT_FRAME(); \
177 uintptr_t pc = GET_CALLER_PC(); \
178 uintptr_t local_stack; \
179 uintptr_t sp = (uintptr_t)&local_stack;
180
181// These magic values are written to shadow for better error reporting.
182const int kAsanHeapLeftRedzoneMagic = 0xfa;
183const int kAsanHeapRightRedzoneMagic = 0xfb;
184const int kAsanHeapFreeMagic = 0xfd;
185const int kAsanStackLeftRedzoneMagic = 0xf1;
186const int kAsanStackMidRedzoneMagic = 0xf2;
187const int kAsanStackRightRedzoneMagic = 0xf3;
188const int kAsanStackPartialRedzoneMagic = 0xf4;
189const int kAsanStackAfterReturnMagic = 0xf5;
190const int kAsanUserPoisonedMemoryMagic = 0xf7;
191const int kAsanGlobalRedzoneMagic = 0xf9;
Kostya Serebryany6b30e2c2011-12-15 17:41:30 +0000192const int kAsanInternalHeapMagic = 0xfe;
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000193
194static const uintptr_t kCurrentStackFrameMagic = 0x41B58AB3;
195static const uintptr_t kRetiredStackFrameMagic = 0x45E0360E;
196
Kostya Serebryanyde496f42011-12-28 22:58:01 +0000197// --------------------------- Bit twiddling ------- {{{1
198inline bool IsPowerOfTwo(size_t x) {
199 return (x & (x - 1)) == 0;
200}
201
202inline size_t RoundUpTo(size_t size, size_t boundary) {
203 CHECK(IsPowerOfTwo(boundary));
204 return (size + boundary - 1) & ~(boundary - 1);
205}
206
Kostya Serebryanyb89567c2011-12-02 21:02:20 +0000207// -------------------------- LowLevelAllocator ----- {{{1
208// A simple low-level memory allocator for internal use.
209class LowLevelAllocator {
210 public:
211 explicit LowLevelAllocator(LinkerInitialized) {}
212 // 'size' must be a power of two.
213 // Requires an external lock.
214 void *Allocate(size_t size);
215 private:
216 char *allocated_end_;
217 char *allocated_current_;
218};
219
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000220// -------------------------- Atomic ---------------- {{{1
221static inline int AtomicInc(int *a) {
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000222#ifdef ANDROID
223 return __atomic_inc(a) + 1;
224#else
225 return __sync_add_and_fetch(a, 1);
226#endif
227}
228
229static inline int AtomicDec(int *a) {
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000230#ifdef ANDROID
231 return __atomic_dec(a) - 1;
232#else
233 return __sync_add_and_fetch(a, -1);
234#endif
235}
236
237} // namespace __asan
238
239#endif // ASAN_INTERNAL_H