blob: 041fa3dce803be895a325f1601891bc98bffad92 [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
Alexander Potapenko6f045292012-01-27 15:15:04 +000017#if !defined(__linux__) && !defined(__APPLE__) && !defined(_WIN32)
Kostya Serebryanyd6567c52011-12-01 21:40:52 +000018# error "This operating system is not supported by AddressSanitizer"
19#endif
20
Alexander Potapenko6f045292012-01-27 15:15:04 +000021#include <stdlib.h> // for size_t, uintptr_t, etc.
22
Kostya Serebryany85822082012-01-30 20:55:02 +000023#if defined(_WIN32)
Alexander Potapenko6f045292012-01-27 15:15:04 +000024// There's no <stdint.h> in Visual Studio 9, so we have to define [u]int*_t.
25typedef unsigned __int8 uint8_t;
26typedef unsigned __int16 uint16_t;
27typedef unsigned __int32 uint32_t;
28typedef unsigned __int64 uint64_t;
29typedef __int8 int8_t;
30typedef __int16 int16_t;
31typedef __int32 int32_t;
32typedef __int64 int64_t;
Alexey Samsonovadf2b032012-02-03 08:37:19 +000033
34# define ALIAS(x) // TODO(timurrrr): do we need this on Windows?
35# define ALIGNED(x) __declspec(align(x))
36# define NOINLINE __declspec(noinline)
37
38# define ASAN_INTERFACE_ATTRIBUTE // TODO(timurrrr): do we need this on Win?
39#else // defined(_WIN32)
Kostya Serebryany85822082012-01-30 20:55:02 +000040# include <stdint.h> // for __WORDSIZE
Alexey Samsonovadf2b032012-02-03 08:37:19 +000041
42# define ALIAS(x) __attribute__((alias(x)))
43# define ALIGNED(x) __attribute__((aligned(x)))
44# define NOINLINE __attribute__((noinline))
45
46# define ASAN_INTERFACE_ATTRIBUTE __attribute__((visibility("default")))
47#endif // defined(_WIN32)
Kostya Serebryany1e172b42011-11-30 01:07:02 +000048
Daniel Dunbar46166332011-12-02 01:32:27 +000049// If __WORDSIZE was undefined by the platform, define it in terms of the
Kostya Serebryany669f5432012-01-31 18:13:50 +000050// compiler built-ins __LP64__ and _WIN64.
Daniel Dunbar46166332011-12-02 01:32:27 +000051#ifndef __WORDSIZE
Kostya Serebryany669f5432012-01-31 18:13:50 +000052#if __LP64__ || defined(_WIN64)
Daniel Dunbar46166332011-12-02 01:32:27 +000053#define __WORDSIZE 64
54#else
55#define __WORDSIZE 32
56#endif
57#endif
58
Alexander Potapenkoc8365232012-01-27 10:52:37 +000059#if !defined(__has_feature)
60#define __has_feature(x) 0
61#endif
62
Kostya Serebryany13ebae62011-12-27 21:57:12 +000063#if defined(__has_feature) && __has_feature(address_sanitizer)
Kostya Serebryany1e172b42011-11-30 01:07:02 +000064# error "The AddressSanitizer run-time should not be"
65 " instrumented by AddressSanitizer"
66#endif
67
Kostya Serebryanyc6f22232011-12-08 18:30:42 +000068// Build-time configuration options.
69
Kostya Serebryanyc6f22232011-12-08 18:30:42 +000070// If set, asan will install its own SEGV signal handler.
71#ifndef ASAN_NEEDS_SEGV
72# define ASAN_NEEDS_SEGV 1
73#endif
74
75// If set, asan will intercept C++ exception api call(s).
76#ifndef ASAN_HAS_EXCEPTIONS
77# define ASAN_HAS_EXCEPTIONS 1
78#endif
79
80// If set, asan uses the values of SHADOW_SCALE and SHADOW_OFFSET
81// provided by the instrumented objects. Otherwise constants are used.
82#ifndef ASAN_FLEXIBLE_MAPPING_AND_OFFSET
83# define ASAN_FLEXIBLE_MAPPING_AND_OFFSET 0
84#endif
85
Kostya Serebryany1e172b42011-11-30 01:07:02 +000086// All internal functions in asan reside inside the __asan namespace
87// to avoid namespace collisions with the user programs.
88// Seperate namespace also makes it simpler to distinguish the asan run-time
89// functions from the instrumented user code in a profile.
90namespace __asan {
91
92class AsanThread;
93struct AsanStackTrace;
94
Kostya Serebryany218a9b72011-11-30 18:50:23 +000095// asan_rtl.cc
Kostya Serebryany1e172b42011-11-30 01:07:02 +000096void CheckFailed(const char *cond, const char *file, int line);
97void ShowStatsAndAbort();
98
Kostya Serebryany218a9b72011-11-30 18:50:23 +000099// asan_globals.cc
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000100bool DescribeAddrIfGlobal(uintptr_t addr);
101
Kostya Serebryany218a9b72011-11-30 18:50:23 +0000102// asan_malloc_linux.cc / asan_malloc_mac.cc
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000103void ReplaceSystemMalloc();
104
Kostya Serebryanyde496f42011-12-28 22:58:01 +0000105void OutOfMemoryMessageAndDie(const char *mem_type, size_t size);
106
Kostya Serebryany218a9b72011-11-30 18:50:23 +0000107// asan_linux.cc / asan_mac.cc
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000108void *AsanDoesNotSupportStaticLinkage();
Kostya Serebryanyde496f42011-12-28 22:58:01 +0000109int AsanOpenReadonly(const char* filename);
Alexander Potapenko1e316d72012-01-13 12:59:48 +0000110const char *AsanGetEnv(const char *name);
Kostya Serebryanyde496f42011-12-28 22:58:01 +0000111
Kostya Serebryanya874fe52011-12-28 23:28:54 +0000112void *AsanMmapFixedNoReserve(uintptr_t fixed_addr, size_t size);
113void *AsanMmapFixedReserve(uintptr_t fixed_addr, size_t size);
114void *AsanMprotect(uintptr_t fixed_addr, size_t size);
Kostya Serebryanyde496f42011-12-28 22:58:01 +0000115void *AsanMmapSomewhereOrDie(size_t size, const char *where);
116void AsanUnmapOrDie(void *ptr, size_t size);
117
Kostya Serebryanyef14ff62012-01-06 02:12:25 +0000118void AsanDisableCoreDumper();
Kostya Serebryany9107c262012-01-06 19:11:09 +0000119void GetPcSpBp(void *context, uintptr_t *pc, uintptr_t *sp, uintptr_t *bp);
Kostya Serebryanyef14ff62012-01-06 02:12:25 +0000120
Kostya Serebryany0ecf5eb2012-01-09 23:11:26 +0000121size_t AsanRead(int fd, void *buf, size_t count);
122size_t AsanWrite(int fd, const void *buf, size_t count);
Kostya Serebryanyde496f42011-12-28 22:58:01 +0000123int AsanClose(int fd);
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000124
Kostya Serebryany4803ab92012-01-09 18:53:15 +0000125bool AsanInterceptsSignal(int signum);
Kostya Serebryanya7e760a2012-01-09 19:18:27 +0000126void InstallSignalHandlers();
Kostya Serebryany0ecf5eb2012-01-09 23:11:26 +0000127int GetPid();
Kostya Serebryanycc4e6862012-01-11 02:21:06 +0000128uintptr_t GetThreadSelf();
Kostya Serebryanydde7c332012-01-11 02:39:16 +0000129int AtomicInc(int *a);
Kostya Serebryanycc4e6862012-01-11 02:21:06 +0000130
131// Wrapper for TLS/TSD.
Kostya Serebryanyf58f9982012-02-07 00:27:15 +0000132void AsanTSDInit(void (*destructor)(void *tsd));
Kostya Serebryanycc4e6862012-01-11 02:21:06 +0000133void *AsanTSDGet();
134void AsanTSDSet(void *tsd);
Kostya Serebryany4803ab92012-01-09 18:53:15 +0000135
Kostya Serebryanydf499b42012-01-05 00:44:33 +0000136// Opens the file 'file_name" and reads up to 'max_len' bytes.
137// The resulting buffer is mmaped and stored in '*buff'.
138// The size of the mmaped region is stored in '*buff_size',
Kostya Serebryany0ecf5eb2012-01-09 23:11:26 +0000139// Returns the number of read bytes or 0 if file can not be opened.
140size_t ReadFileToBuffer(const char *file_name, char **buff,
141 size_t *buff_size, size_t max_len);
Kostya Serebryanydf499b42012-01-05 00:44:33 +0000142
Kostya Serebryany218a9b72011-11-30 18:50:23 +0000143// asan_printf.cc
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000144void RawWrite(const char *buffer);
145int SNPrint(char *buffer, size_t length, const char *format, ...);
146void Printf(const char *format, ...);
Kostya Serebryanydf499b42012-01-05 00:44:33 +0000147int SScanf(const char *str, const char *format, ...);
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000148void Report(const char *format, ...);
149
Kostya Serebryany2d8b3bd2011-12-02 18:42:04 +0000150// Don't use std::min and std::max, to minimize dependency on libstdc++.
151template<class T> T Min(T a, T b) { return a < b ? a : b; }
152template<class T> T Max(T a, T b) { return a > b ? a : b; }
153
Kostya Serebryany218a9b72011-11-30 18:50:23 +0000154// asan_poisoning.cc
155// Poisons the shadow memory for "size" bytes starting from "addr".
156void PoisonShadow(uintptr_t addr, size_t size, uint8_t value);
157// Poisons the shadow memory for "redzone_size" bytes starting from
158// "addr + size".
159void PoisonShadowPartialRightRedzone(uintptr_t addr,
160 uintptr_t size,
161 uintptr_t redzone_size,
162 uint8_t value);
163
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000164extern size_t FLAG_quarantine_size;
165extern int FLAG_demangle;
166extern bool FLAG_symbolize;
167extern int FLAG_v;
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000168extern size_t FLAG_redzone;
169extern int FLAG_debug;
170extern bool FLAG_poison_shadow;
171extern int FLAG_report_globals;
172extern size_t FLAG_malloc_context_size;
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000173extern bool FLAG_replace_str;
174extern bool FLAG_replace_intrin;
175extern bool FLAG_replace_cfallocator;
176extern bool FLAG_fast_unwind;
177extern bool FLAG_use_fake_stack;
178extern size_t FLAG_max_malloc_fill_size;
179extern int FLAG_exitcode;
180extern bool FLAG_allow_user_poisoning;
Kostya Serebryanycb00d132012-01-31 00:52:18 +0000181extern int FLAG_sleep_before_dying;
Kostya Serebryany4803ab92012-01-09 18:53:15 +0000182extern bool FLAG_handle_segv;
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000183
184extern int asan_inited;
185// Used to avoid infinite recursion in __asan_init().
186extern bool asan_init_is_running;
187
188enum LinkerInitialized { LINKER_INITIALIZED = 0 };
189
Kostya Serebryany0ecf5eb2012-01-09 23:11:26 +0000190void AsanDie();
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000191
192#define CHECK(cond) do { if (!(cond)) { \
193 CheckFailed(#cond, __FILE__, __LINE__); \
194}}while(0)
195
196#define RAW_CHECK_MSG(expr, msg) do { \
197 if (!(expr)) { \
198 RawWrite(msg); \
Kostya Serebryany0ecf5eb2012-01-09 23:11:26 +0000199 AsanDie(); \
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000200 } \
201} while (0)
202
203#define RAW_CHECK(expr) RAW_CHECK_MSG(expr, #expr)
204
205#define UNIMPLEMENTED() CHECK("unimplemented" && 0)
206
207#define ASAN_ARRAY_SIZE(a) (sizeof(a)/sizeof((a)[0]))
208
209const size_t kWordSize = __WORDSIZE / 8;
210const size_t kWordSizeInBits = 8 * kWordSize;
211const size_t kPageSizeBits = 12;
212const size_t kPageSize = 1UL << kPageSizeBits;
213
Alexander Potapenko6f045292012-01-27 15:15:04 +0000214#ifndef _WIN32
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000215#define GET_CALLER_PC() (uintptr_t)__builtin_return_address(0)
216#define GET_CURRENT_FRAME() (uintptr_t)__builtin_frame_address(0)
Alexander Potapenko6f045292012-01-27 15:15:04 +0000217#else
218// TODO(timurrrr): implement.
219#define GET_CALLER_PC() (uintptr_t)0
220#define GET_CURRENT_FRAME() (uintptr_t)0
221#endif
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000222
223#define GET_BP_PC_SP \
224 uintptr_t bp = GET_CURRENT_FRAME(); \
225 uintptr_t pc = GET_CALLER_PC(); \
226 uintptr_t local_stack; \
227 uintptr_t sp = (uintptr_t)&local_stack;
228
229// These magic values are written to shadow for better error reporting.
230const int kAsanHeapLeftRedzoneMagic = 0xfa;
231const int kAsanHeapRightRedzoneMagic = 0xfb;
232const int kAsanHeapFreeMagic = 0xfd;
233const int kAsanStackLeftRedzoneMagic = 0xf1;
234const int kAsanStackMidRedzoneMagic = 0xf2;
235const int kAsanStackRightRedzoneMagic = 0xf3;
236const int kAsanStackPartialRedzoneMagic = 0xf4;
237const int kAsanStackAfterReturnMagic = 0xf5;
238const int kAsanUserPoisonedMemoryMagic = 0xf7;
239const int kAsanGlobalRedzoneMagic = 0xf9;
Kostya Serebryany6b30e2c2011-12-15 17:41:30 +0000240const int kAsanInternalHeapMagic = 0xfe;
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000241
242static const uintptr_t kCurrentStackFrameMagic = 0x41B58AB3;
243static const uintptr_t kRetiredStackFrameMagic = 0x45E0360E;
244
Kostya Serebryanyde496f42011-12-28 22:58:01 +0000245// --------------------------- Bit twiddling ------- {{{1
246inline bool IsPowerOfTwo(size_t x) {
247 return (x & (x - 1)) == 0;
248}
249
250inline size_t RoundUpTo(size_t size, size_t boundary) {
251 CHECK(IsPowerOfTwo(boundary));
252 return (size + boundary - 1) & ~(boundary - 1);
253}
254
Kostya Serebryanyb89567c2011-12-02 21:02:20 +0000255// -------------------------- LowLevelAllocator ----- {{{1
256// A simple low-level memory allocator for internal use.
257class LowLevelAllocator {
258 public:
259 explicit LowLevelAllocator(LinkerInitialized) {}
260 // 'size' must be a power of two.
261 // Requires an external lock.
262 void *Allocate(size_t size);
263 private:
264 char *allocated_end_;
265 char *allocated_current_;
266};
267
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000268} // namespace __asan
269
270#endif // ASAN_INTERNAL_H