blob: 8a9b8580a102c816e01d05434fdfaa0995489c67 [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 Serebryany13ebae62011-12-27 21:57:12 +000034#if defined(__has_feature) && __has_feature(address_sanitizer)
Kostya Serebryany1e172b42011-11-30 01:07:02 +000035# error "The AddressSanitizer run-time should not be"
36 " instrumented by AddressSanitizer"
37#endif
38
Kostya Serebryanyc6f22232011-12-08 18:30:42 +000039// Build-time configuration options.
40
41// If set, sysinfo/sysinfo.h will be used to iterate over /proc/maps.
42#ifndef ASAN_USE_SYSINFO
Kostya Serebryanydf499b42012-01-05 00:44:33 +000043#ifdef __linux__
44# define ASAN_USE_SYSINFO 0
45#else
Kostya Serebryanyc6f22232011-12-08 18:30:42 +000046# define ASAN_USE_SYSINFO 1
47#endif
Kostya Serebryanydf499b42012-01-05 00:44:33 +000048#endif
Kostya Serebryanyc6f22232011-12-08 18:30:42 +000049
50// If set, asan will install its own SEGV signal handler.
51#ifndef ASAN_NEEDS_SEGV
52# define ASAN_NEEDS_SEGV 1
53#endif
54
55// If set, asan will intercept C++ exception api call(s).
56#ifndef ASAN_HAS_EXCEPTIONS
57# define ASAN_HAS_EXCEPTIONS 1
58#endif
59
60// If set, asan uses the values of SHADOW_SCALE and SHADOW_OFFSET
61// provided by the instrumented objects. Otherwise constants are used.
62#ifndef ASAN_FLEXIBLE_MAPPING_AND_OFFSET
63# define ASAN_FLEXIBLE_MAPPING_AND_OFFSET 0
64#endif
65
Kostya Serebryany1e172b42011-11-30 01:07:02 +000066// All internal functions in asan reside inside the __asan namespace
67// to avoid namespace collisions with the user programs.
68// Seperate namespace also makes it simpler to distinguish the asan run-time
69// functions from the instrumented user code in a profile.
70namespace __asan {
71
72class AsanThread;
73struct AsanStackTrace;
74
Kostya Serebryany218a9b72011-11-30 18:50:23 +000075// asan_rtl.cc
Kostya Serebryany1e172b42011-11-30 01:07:02 +000076void CheckFailed(const char *cond, const char *file, int line);
77void ShowStatsAndAbort();
78
Kostya Serebryany218a9b72011-11-30 18:50:23 +000079// asan_globals.cc
Kostya Serebryany1e172b42011-11-30 01:07:02 +000080bool DescribeAddrIfGlobal(uintptr_t addr);
81
Kostya Serebryany218a9b72011-11-30 18:50:23 +000082// asan_malloc_linux.cc / asan_malloc_mac.cc
Kostya Serebryany1e172b42011-11-30 01:07:02 +000083void ReplaceSystemMalloc();
84
Kostya Serebryanyde496f42011-12-28 22:58:01 +000085void OutOfMemoryMessageAndDie(const char *mem_type, size_t size);
86
Kostya Serebryany218a9b72011-11-30 18:50:23 +000087// asan_linux.cc / asan_mac.cc
Kostya Serebryany1e172b42011-11-30 01:07:02 +000088void *AsanDoesNotSupportStaticLinkage();
Kostya Serebryanyde496f42011-12-28 22:58:01 +000089int AsanOpenReadonly(const char* filename);
Alexander Potapenko1e316d72012-01-13 12:59:48 +000090const char *AsanGetEnv(const char *name);
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
Kostya Serebryanyef14ff62012-01-06 02:12:25 +000098void AsanDisableCoreDumper();
Kostya Serebryany9107c262012-01-06 19:11:09 +000099void GetPcSpBp(void *context, uintptr_t *pc, uintptr_t *sp, uintptr_t *bp);
Kostya Serebryanyef14ff62012-01-06 02:12:25 +0000100
Kostya Serebryany0ecf5eb2012-01-09 23:11:26 +0000101size_t AsanRead(int fd, void *buf, size_t count);
102size_t AsanWrite(int fd, const void *buf, size_t count);
Kostya Serebryanyde496f42011-12-28 22:58:01 +0000103int AsanClose(int fd);
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000104
Kostya Serebryany4803ab92012-01-09 18:53:15 +0000105bool AsanInterceptsSignal(int signum);
Kostya Serebryanya7e760a2012-01-09 19:18:27 +0000106void InstallSignalHandlers();
Kostya Serebryany0ecf5eb2012-01-09 23:11:26 +0000107int GetPid();
Kostya Serebryanycc4e6862012-01-11 02:21:06 +0000108uintptr_t GetThreadSelf();
Kostya Serebryanydde7c332012-01-11 02:39:16 +0000109int AtomicInc(int *a);
Kostya Serebryanycc4e6862012-01-11 02:21:06 +0000110
111// Wrapper for TLS/TSD.
112void AsanTSDInit();
113void *AsanTSDGet();
114void AsanTSDSet(void *tsd);
Kostya Serebryany4803ab92012-01-09 18:53:15 +0000115
Kostya Serebryanydf499b42012-01-05 00:44:33 +0000116// Opens the file 'file_name" and reads up to 'max_len' bytes.
117// The resulting buffer is mmaped and stored in '*buff'.
118// The size of the mmaped region is stored in '*buff_size',
Kostya Serebryany0ecf5eb2012-01-09 23:11:26 +0000119// Returns the number of read bytes or 0 if file can not be opened.
120size_t ReadFileToBuffer(const char *file_name, char **buff,
121 size_t *buff_size, size_t max_len);
Kostya Serebryanydf499b42012-01-05 00:44:33 +0000122
Kostya Serebryany218a9b72011-11-30 18:50:23 +0000123// asan_printf.cc
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000124void RawWrite(const char *buffer);
125int SNPrint(char *buffer, size_t length, const char *format, ...);
126void Printf(const char *format, ...);
Kostya Serebryanydf499b42012-01-05 00:44:33 +0000127int SScanf(const char *str, const char *format, ...);
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000128void Report(const char *format, ...);
129
Kostya Serebryany2d8b3bd2011-12-02 18:42:04 +0000130// Don't use std::min and std::max, to minimize dependency on libstdc++.
131template<class T> T Min(T a, T b) { return a < b ? a : b; }
132template<class T> T Max(T a, T b) { return a > b ? a : b; }
133
Kostya Serebryany218a9b72011-11-30 18:50:23 +0000134// asan_poisoning.cc
135// Poisons the shadow memory for "size" bytes starting from "addr".
136void PoisonShadow(uintptr_t addr, size_t size, uint8_t value);
137// Poisons the shadow memory for "redzone_size" bytes starting from
138// "addr + size".
139void PoisonShadowPartialRightRedzone(uintptr_t addr,
140 uintptr_t size,
141 uintptr_t redzone_size,
142 uint8_t value);
143
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000144extern size_t FLAG_quarantine_size;
145extern int FLAG_demangle;
146extern bool FLAG_symbolize;
147extern int FLAG_v;
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000148extern size_t FLAG_redzone;
149extern int FLAG_debug;
150extern bool FLAG_poison_shadow;
151extern int FLAG_report_globals;
152extern size_t FLAG_malloc_context_size;
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000153extern bool FLAG_replace_str;
154extern bool FLAG_replace_intrin;
155extern bool FLAG_replace_cfallocator;
156extern bool FLAG_fast_unwind;
157extern bool FLAG_use_fake_stack;
158extern size_t FLAG_max_malloc_fill_size;
159extern int FLAG_exitcode;
160extern bool FLAG_allow_user_poisoning;
Kostya Serebryany4803ab92012-01-09 18:53:15 +0000161extern bool FLAG_handle_segv;
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000162
163extern int asan_inited;
164// Used to avoid infinite recursion in __asan_init().
165extern bool asan_init_is_running;
166
167enum LinkerInitialized { LINKER_INITIALIZED = 0 };
168
Kostya Serebryany0ecf5eb2012-01-09 23:11:26 +0000169void AsanDie();
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000170
171#define CHECK(cond) do { if (!(cond)) { \
172 CheckFailed(#cond, __FILE__, __LINE__); \
173}}while(0)
174
175#define RAW_CHECK_MSG(expr, msg) do { \
176 if (!(expr)) { \
177 RawWrite(msg); \
Kostya Serebryany0ecf5eb2012-01-09 23:11:26 +0000178 AsanDie(); \
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000179 } \
180} while (0)
181
182#define RAW_CHECK(expr) RAW_CHECK_MSG(expr, #expr)
183
184#define UNIMPLEMENTED() CHECK("unimplemented" && 0)
185
186#define ASAN_ARRAY_SIZE(a) (sizeof(a)/sizeof((a)[0]))
187
188const size_t kWordSize = __WORDSIZE / 8;
189const size_t kWordSizeInBits = 8 * kWordSize;
190const size_t kPageSizeBits = 12;
191const size_t kPageSize = 1UL << kPageSizeBits;
192
193#define GET_CALLER_PC() (uintptr_t)__builtin_return_address(0)
194#define GET_CURRENT_FRAME() (uintptr_t)__builtin_frame_address(0)
195
196#define GET_BP_PC_SP \
197 uintptr_t bp = GET_CURRENT_FRAME(); \
198 uintptr_t pc = GET_CALLER_PC(); \
199 uintptr_t local_stack; \
200 uintptr_t sp = (uintptr_t)&local_stack;
201
202// These magic values are written to shadow for better error reporting.
203const int kAsanHeapLeftRedzoneMagic = 0xfa;
204const int kAsanHeapRightRedzoneMagic = 0xfb;
205const int kAsanHeapFreeMagic = 0xfd;
206const int kAsanStackLeftRedzoneMagic = 0xf1;
207const int kAsanStackMidRedzoneMagic = 0xf2;
208const int kAsanStackRightRedzoneMagic = 0xf3;
209const int kAsanStackPartialRedzoneMagic = 0xf4;
210const int kAsanStackAfterReturnMagic = 0xf5;
211const int kAsanUserPoisonedMemoryMagic = 0xf7;
212const int kAsanGlobalRedzoneMagic = 0xf9;
Kostya Serebryany6b30e2c2011-12-15 17:41:30 +0000213const int kAsanInternalHeapMagic = 0xfe;
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000214
215static const uintptr_t kCurrentStackFrameMagic = 0x41B58AB3;
216static const uintptr_t kRetiredStackFrameMagic = 0x45E0360E;
217
Kostya Serebryanyde496f42011-12-28 22:58:01 +0000218// --------------------------- Bit twiddling ------- {{{1
219inline bool IsPowerOfTwo(size_t x) {
220 return (x & (x - 1)) == 0;
221}
222
223inline size_t RoundUpTo(size_t size, size_t boundary) {
224 CHECK(IsPowerOfTwo(boundary));
225 return (size + boundary - 1) & ~(boundary - 1);
226}
227
Kostya Serebryanyb89567c2011-12-02 21:02:20 +0000228// -------------------------- LowLevelAllocator ----- {{{1
229// A simple low-level memory allocator for internal use.
230class LowLevelAllocator {
231 public:
232 explicit LowLevelAllocator(LinkerInitialized) {}
233 // 'size' must be a power of two.
234 // Requires an external lock.
235 void *Allocate(size_t size);
236 private:
237 char *allocated_end_;
238 char *allocated_current_;
239};
240
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000241} // namespace __asan
242
243#endif // ASAN_INTERNAL_H