blob: 224cc20360e8258ee9a4137aa35c5cf29593034b [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 Serebryanycc4e6862012-01-11 02:21:06 +0000111uintptr_t GetThreadSelf();
112
113// Wrapper for TLS/TSD.
114void AsanTSDInit();
115void *AsanTSDGet();
116void AsanTSDSet(void *tsd);
Kostya Serebryany4803ab92012-01-09 18:53:15 +0000117
Kostya Serebryanydf499b42012-01-05 00:44:33 +0000118// Opens the file 'file_name" and reads up to 'max_len' bytes.
119// The resulting buffer is mmaped and stored in '*buff'.
120// The size of the mmaped region is stored in '*buff_size',
Kostya Serebryany0ecf5eb2012-01-09 23:11:26 +0000121// Returns the number of read bytes or 0 if file can not be opened.
122size_t ReadFileToBuffer(const char *file_name, char **buff,
123 size_t *buff_size, size_t max_len);
Kostya Serebryanydf499b42012-01-05 00:44:33 +0000124
Kostya Serebryany218a9b72011-11-30 18:50:23 +0000125// asan_printf.cc
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000126void RawWrite(const char *buffer);
127int SNPrint(char *buffer, size_t length, const char *format, ...);
128void Printf(const char *format, ...);
Kostya Serebryanydf499b42012-01-05 00:44:33 +0000129int SScanf(const char *str, const char *format, ...);
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000130void Report(const char *format, ...);
131
Kostya Serebryany2d8b3bd2011-12-02 18:42:04 +0000132// Don't use std::min and std::max, to minimize dependency on libstdc++.
133template<class T> T Min(T a, T b) { return a < b ? a : b; }
134template<class T> T Max(T a, T b) { return a > b ? a : b; }
135
Kostya Serebryany218a9b72011-11-30 18:50:23 +0000136// asan_poisoning.cc
137// Poisons the shadow memory for "size" bytes starting from "addr".
138void PoisonShadow(uintptr_t addr, size_t size, uint8_t value);
139// Poisons the shadow memory for "redzone_size" bytes starting from
140// "addr + size".
141void PoisonShadowPartialRightRedzone(uintptr_t addr,
142 uintptr_t size,
143 uintptr_t redzone_size,
144 uint8_t value);
145
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000146extern size_t FLAG_quarantine_size;
147extern int FLAG_demangle;
148extern bool FLAG_symbolize;
149extern int FLAG_v;
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000150extern size_t FLAG_redzone;
151extern int FLAG_debug;
152extern bool FLAG_poison_shadow;
153extern int FLAG_report_globals;
154extern size_t FLAG_malloc_context_size;
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000155extern bool FLAG_replace_str;
156extern bool FLAG_replace_intrin;
157extern bool FLAG_replace_cfallocator;
158extern bool FLAG_fast_unwind;
159extern bool FLAG_use_fake_stack;
160extern size_t FLAG_max_malloc_fill_size;
161extern int FLAG_exitcode;
162extern bool FLAG_allow_user_poisoning;
Kostya Serebryany4803ab92012-01-09 18:53:15 +0000163extern bool FLAG_handle_segv;
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000164
165extern int asan_inited;
166// Used to avoid infinite recursion in __asan_init().
167extern bool asan_init_is_running;
168
169enum LinkerInitialized { LINKER_INITIALIZED = 0 };
170
Kostya Serebryany0ecf5eb2012-01-09 23:11:26 +0000171void AsanDie();
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000172
173#define CHECK(cond) do { if (!(cond)) { \
174 CheckFailed(#cond, __FILE__, __LINE__); \
175}}while(0)
176
177#define RAW_CHECK_MSG(expr, msg) do { \
178 if (!(expr)) { \
179 RawWrite(msg); \
Kostya Serebryany0ecf5eb2012-01-09 23:11:26 +0000180 AsanDie(); \
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000181 } \
182} while (0)
183
184#define RAW_CHECK(expr) RAW_CHECK_MSG(expr, #expr)
185
186#define UNIMPLEMENTED() CHECK("unimplemented" && 0)
187
188#define ASAN_ARRAY_SIZE(a) (sizeof(a)/sizeof((a)[0]))
189
190const size_t kWordSize = __WORDSIZE / 8;
191const size_t kWordSizeInBits = 8 * kWordSize;
192const size_t kPageSizeBits = 12;
193const size_t kPageSize = 1UL << kPageSizeBits;
194
195#define GET_CALLER_PC() (uintptr_t)__builtin_return_address(0)
196#define GET_CURRENT_FRAME() (uintptr_t)__builtin_frame_address(0)
197
198#define GET_BP_PC_SP \
199 uintptr_t bp = GET_CURRENT_FRAME(); \
200 uintptr_t pc = GET_CALLER_PC(); \
201 uintptr_t local_stack; \
202 uintptr_t sp = (uintptr_t)&local_stack;
203
204// These magic values are written to shadow for better error reporting.
205const int kAsanHeapLeftRedzoneMagic = 0xfa;
206const int kAsanHeapRightRedzoneMagic = 0xfb;
207const int kAsanHeapFreeMagic = 0xfd;
208const int kAsanStackLeftRedzoneMagic = 0xf1;
209const int kAsanStackMidRedzoneMagic = 0xf2;
210const int kAsanStackRightRedzoneMagic = 0xf3;
211const int kAsanStackPartialRedzoneMagic = 0xf4;
212const int kAsanStackAfterReturnMagic = 0xf5;
213const int kAsanUserPoisonedMemoryMagic = 0xf7;
214const int kAsanGlobalRedzoneMagic = 0xf9;
Kostya Serebryany6b30e2c2011-12-15 17:41:30 +0000215const int kAsanInternalHeapMagic = 0xfe;
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000216
217static const uintptr_t kCurrentStackFrameMagic = 0x41B58AB3;
218static const uintptr_t kRetiredStackFrameMagic = 0x45E0360E;
219
Kostya Serebryanyde496f42011-12-28 22:58:01 +0000220// --------------------------- Bit twiddling ------- {{{1
221inline bool IsPowerOfTwo(size_t x) {
222 return (x & (x - 1)) == 0;
223}
224
225inline size_t RoundUpTo(size_t size, size_t boundary) {
226 CHECK(IsPowerOfTwo(boundary));
227 return (size + boundary - 1) & ~(boundary - 1);
228}
229
Kostya Serebryanyb89567c2011-12-02 21:02:20 +0000230// -------------------------- LowLevelAllocator ----- {{{1
231// A simple low-level memory allocator for internal use.
232class LowLevelAllocator {
233 public:
234 explicit LowLevelAllocator(LinkerInitialized) {}
235 // 'size' must be a power of two.
236 // Requires an external lock.
237 void *Allocate(size_t size);
238 private:
239 char *allocated_end_;
240 char *allocated_current_;
241};
242
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000243// -------------------------- Atomic ---------------- {{{1
244static inline int AtomicInc(int *a) {
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000245#ifdef ANDROID
246 return __atomic_inc(a) + 1;
247#else
248 return __sync_add_and_fetch(a, 1);
249#endif
250}
251
252static inline int AtomicDec(int *a) {
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000253#ifdef ANDROID
254 return __atomic_dec(a) - 1;
255#else
256 return __sync_add_and_fetch(a, -1);
257#endif
258}
259
260} // namespace __asan
261
262#endif // ASAN_INTERNAL_H