blob: 5be5186fd17846aacfdea2feeca7c98db65eb450 [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
23#if !defined(_WIN32)
Kostya Serebryany1e172b42011-11-30 01:07:02 +000024#include <stdint.h> // for __WORDSIZE
Alexander Potapenko6f045292012-01-27 15:15:04 +000025#else
26// There's no <stdint.h> in Visual Studio 9, so we have to define [u]int*_t.
27typedef unsigned __int8 uint8_t;
28typedef unsigned __int16 uint16_t;
29typedef unsigned __int32 uint32_t;
30typedef unsigned __int64 uint64_t;
31typedef __int8 int8_t;
32typedef __int16 int16_t;
33typedef __int32 int32_t;
34typedef __int64 int64_t;
35
36// Visual Studio does not define ssize_t.
37#ifdef _WIN64
38typedef int64_t ssize_t;
39#define __WORDSIZE 64
40#else
41typedef int32_t ssize_t;
42#define __WORDSIZE 32
43#endif
44
45#endif // _WIN32
Kostya Serebryany1e172b42011-11-30 01:07:02 +000046
Daniel Dunbar46166332011-12-02 01:32:27 +000047// If __WORDSIZE was undefined by the platform, define it in terms of the
48// compiler built-in __LP64__.
49#ifndef __WORDSIZE
50#if __LP64__
51#define __WORDSIZE 64
52#else
53#define __WORDSIZE 32
54#endif
55#endif
56
Alexander Potapenkoc8365232012-01-27 10:52:37 +000057#if !defined(__has_feature)
58#define __has_feature(x) 0
59#endif
60
Kostya Serebryany13ebae62011-12-27 21:57:12 +000061#if defined(__has_feature) && __has_feature(address_sanitizer)
Kostya Serebryany1e172b42011-11-30 01:07:02 +000062# error "The AddressSanitizer run-time should not be"
63 " instrumented by AddressSanitizer"
64#endif
65
Kostya Serebryanyc6f22232011-12-08 18:30:42 +000066// Build-time configuration options.
67
Kostya Serebryanyc6f22232011-12-08 18:30:42 +000068// If set, asan will install its own SEGV signal handler.
69#ifndef ASAN_NEEDS_SEGV
70# define ASAN_NEEDS_SEGV 1
71#endif
72
73// If set, asan will intercept C++ exception api call(s).
74#ifndef ASAN_HAS_EXCEPTIONS
75# define ASAN_HAS_EXCEPTIONS 1
76#endif
77
78// If set, asan uses the values of SHADOW_SCALE and SHADOW_OFFSET
79// provided by the instrumented objects. Otherwise constants are used.
80#ifndef ASAN_FLEXIBLE_MAPPING_AND_OFFSET
81# define ASAN_FLEXIBLE_MAPPING_AND_OFFSET 0
82#endif
83
Kostya Serebryany1e172b42011-11-30 01:07:02 +000084// All internal functions in asan reside inside the __asan namespace
85// to avoid namespace collisions with the user programs.
86// Seperate namespace also makes it simpler to distinguish the asan run-time
87// functions from the instrumented user code in a profile.
88namespace __asan {
89
90class AsanThread;
91struct AsanStackTrace;
92
Kostya Serebryany218a9b72011-11-30 18:50:23 +000093// asan_rtl.cc
Kostya Serebryany1e172b42011-11-30 01:07:02 +000094void CheckFailed(const char *cond, const char *file, int line);
95void ShowStatsAndAbort();
96
Kostya Serebryany218a9b72011-11-30 18:50:23 +000097// asan_globals.cc
Kostya Serebryany1e172b42011-11-30 01:07:02 +000098bool DescribeAddrIfGlobal(uintptr_t addr);
99
Kostya Serebryany218a9b72011-11-30 18:50:23 +0000100// asan_malloc_linux.cc / asan_malloc_mac.cc
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000101void ReplaceSystemMalloc();
102
Kostya Serebryanyde496f42011-12-28 22:58:01 +0000103void OutOfMemoryMessageAndDie(const char *mem_type, size_t size);
104
Kostya Serebryany218a9b72011-11-30 18:50:23 +0000105// asan_linux.cc / asan_mac.cc
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000106void *AsanDoesNotSupportStaticLinkage();
Kostya Serebryanyde496f42011-12-28 22:58:01 +0000107int AsanOpenReadonly(const char* filename);
Alexander Potapenko1e316d72012-01-13 12:59:48 +0000108const char *AsanGetEnv(const char *name);
Kostya Serebryanyde496f42011-12-28 22:58:01 +0000109
Kostya Serebryanya874fe52011-12-28 23:28:54 +0000110void *AsanMmapFixedNoReserve(uintptr_t fixed_addr, size_t size);
111void *AsanMmapFixedReserve(uintptr_t fixed_addr, size_t size);
112void *AsanMprotect(uintptr_t fixed_addr, size_t size);
Kostya Serebryanyde496f42011-12-28 22:58:01 +0000113void *AsanMmapSomewhereOrDie(size_t size, const char *where);
114void AsanUnmapOrDie(void *ptr, size_t size);
115
Kostya Serebryanyef14ff62012-01-06 02:12:25 +0000116void AsanDisableCoreDumper();
Kostya Serebryany9107c262012-01-06 19:11:09 +0000117void GetPcSpBp(void *context, uintptr_t *pc, uintptr_t *sp, uintptr_t *bp);
Kostya Serebryanyef14ff62012-01-06 02:12:25 +0000118
Kostya Serebryany0ecf5eb2012-01-09 23:11:26 +0000119size_t AsanRead(int fd, void *buf, size_t count);
120size_t AsanWrite(int fd, const void *buf, size_t count);
Kostya Serebryanyde496f42011-12-28 22:58:01 +0000121int AsanClose(int fd);
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000122
Kostya Serebryany4803ab92012-01-09 18:53:15 +0000123bool AsanInterceptsSignal(int signum);
Kostya Serebryanya7e760a2012-01-09 19:18:27 +0000124void InstallSignalHandlers();
Kostya Serebryany0ecf5eb2012-01-09 23:11:26 +0000125int GetPid();
Kostya Serebryanycc4e6862012-01-11 02:21:06 +0000126uintptr_t GetThreadSelf();
Kostya Serebryanydde7c332012-01-11 02:39:16 +0000127int AtomicInc(int *a);
Kostya Serebryanycc4e6862012-01-11 02:21:06 +0000128
129// Wrapper for TLS/TSD.
130void AsanTSDInit();
131void *AsanTSDGet();
132void AsanTSDSet(void *tsd);
Kostya Serebryany4803ab92012-01-09 18:53:15 +0000133
Kostya Serebryanydf499b42012-01-05 00:44:33 +0000134// Opens the file 'file_name" and reads up to 'max_len' bytes.
135// The resulting buffer is mmaped and stored in '*buff'.
136// The size of the mmaped region is stored in '*buff_size',
Kostya Serebryany0ecf5eb2012-01-09 23:11:26 +0000137// Returns the number of read bytes or 0 if file can not be opened.
138size_t ReadFileToBuffer(const char *file_name, char **buff,
139 size_t *buff_size, size_t max_len);
Kostya Serebryanydf499b42012-01-05 00:44:33 +0000140
Kostya Serebryany218a9b72011-11-30 18:50:23 +0000141// asan_printf.cc
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000142void RawWrite(const char *buffer);
143int SNPrint(char *buffer, size_t length, const char *format, ...);
144void Printf(const char *format, ...);
Kostya Serebryanydf499b42012-01-05 00:44:33 +0000145int SScanf(const char *str, const char *format, ...);
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000146void Report(const char *format, ...);
147
Kostya Serebryany2d8b3bd2011-12-02 18:42:04 +0000148// Don't use std::min and std::max, to minimize dependency on libstdc++.
149template<class T> T Min(T a, T b) { return a < b ? a : b; }
150template<class T> T Max(T a, T b) { return a > b ? a : b; }
151
Kostya Serebryany218a9b72011-11-30 18:50:23 +0000152// asan_poisoning.cc
153// Poisons the shadow memory for "size" bytes starting from "addr".
154void PoisonShadow(uintptr_t addr, size_t size, uint8_t value);
155// Poisons the shadow memory for "redzone_size" bytes starting from
156// "addr + size".
157void PoisonShadowPartialRightRedzone(uintptr_t addr,
158 uintptr_t size,
159 uintptr_t redzone_size,
160 uint8_t value);
161
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000162extern size_t FLAG_quarantine_size;
163extern int FLAG_demangle;
164extern bool FLAG_symbolize;
165extern int FLAG_v;
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000166extern size_t FLAG_redzone;
167extern int FLAG_debug;
168extern bool FLAG_poison_shadow;
169extern int FLAG_report_globals;
170extern size_t FLAG_malloc_context_size;
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000171extern bool FLAG_replace_str;
172extern bool FLAG_replace_intrin;
173extern bool FLAG_replace_cfallocator;
174extern bool FLAG_fast_unwind;
175extern bool FLAG_use_fake_stack;
176extern size_t FLAG_max_malloc_fill_size;
177extern int FLAG_exitcode;
178extern bool FLAG_allow_user_poisoning;
Kostya Serebryany4803ab92012-01-09 18:53:15 +0000179extern bool FLAG_handle_segv;
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000180
181extern int asan_inited;
182// Used to avoid infinite recursion in __asan_init().
183extern bool asan_init_is_running;
184
185enum LinkerInitialized { LINKER_INITIALIZED = 0 };
186
Kostya Serebryany0ecf5eb2012-01-09 23:11:26 +0000187void AsanDie();
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000188
189#define CHECK(cond) do { if (!(cond)) { \
190 CheckFailed(#cond, __FILE__, __LINE__); \
191}}while(0)
192
193#define RAW_CHECK_MSG(expr, msg) do { \
194 if (!(expr)) { \
195 RawWrite(msg); \
Kostya Serebryany0ecf5eb2012-01-09 23:11:26 +0000196 AsanDie(); \
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000197 } \
198} while (0)
199
200#define RAW_CHECK(expr) RAW_CHECK_MSG(expr, #expr)
201
202#define UNIMPLEMENTED() CHECK("unimplemented" && 0)
203
204#define ASAN_ARRAY_SIZE(a) (sizeof(a)/sizeof((a)[0]))
205
206const size_t kWordSize = __WORDSIZE / 8;
207const size_t kWordSizeInBits = 8 * kWordSize;
208const size_t kPageSizeBits = 12;
209const size_t kPageSize = 1UL << kPageSizeBits;
210
Alexander Potapenko6f045292012-01-27 15:15:04 +0000211#ifndef _WIN32
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000212#define GET_CALLER_PC() (uintptr_t)__builtin_return_address(0)
213#define GET_CURRENT_FRAME() (uintptr_t)__builtin_frame_address(0)
Alexander Potapenko6f045292012-01-27 15:15:04 +0000214#else
215// TODO(timurrrr): implement.
216#define GET_CALLER_PC() (uintptr_t)0
217#define GET_CURRENT_FRAME() (uintptr_t)0
218#endif
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000219
220#define GET_BP_PC_SP \
221 uintptr_t bp = GET_CURRENT_FRAME(); \
222 uintptr_t pc = GET_CALLER_PC(); \
223 uintptr_t local_stack; \
224 uintptr_t sp = (uintptr_t)&local_stack;
225
226// These magic values are written to shadow for better error reporting.
227const int kAsanHeapLeftRedzoneMagic = 0xfa;
228const int kAsanHeapRightRedzoneMagic = 0xfb;
229const int kAsanHeapFreeMagic = 0xfd;
230const int kAsanStackLeftRedzoneMagic = 0xf1;
231const int kAsanStackMidRedzoneMagic = 0xf2;
232const int kAsanStackRightRedzoneMagic = 0xf3;
233const int kAsanStackPartialRedzoneMagic = 0xf4;
234const int kAsanStackAfterReturnMagic = 0xf5;
235const int kAsanUserPoisonedMemoryMagic = 0xf7;
236const int kAsanGlobalRedzoneMagic = 0xf9;
Kostya Serebryany6b30e2c2011-12-15 17:41:30 +0000237const int kAsanInternalHeapMagic = 0xfe;
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000238
239static const uintptr_t kCurrentStackFrameMagic = 0x41B58AB3;
240static const uintptr_t kRetiredStackFrameMagic = 0x45E0360E;
241
Kostya Serebryanyde496f42011-12-28 22:58:01 +0000242// --------------------------- Bit twiddling ------- {{{1
243inline bool IsPowerOfTwo(size_t x) {
244 return (x & (x - 1)) == 0;
245}
246
247inline size_t RoundUpTo(size_t size, size_t boundary) {
248 CHECK(IsPowerOfTwo(boundary));
249 return (size + boundary - 1) & ~(boundary - 1);
250}
251
Kostya Serebryanyb89567c2011-12-02 21:02:20 +0000252// -------------------------- LowLevelAllocator ----- {{{1
253// A simple low-level memory allocator for internal use.
254class LowLevelAllocator {
255 public:
256 explicit LowLevelAllocator(LinkerInitialized) {}
257 // 'size' must be a power of two.
258 // Requires an external lock.
259 void *Allocate(size_t size);
260 private:
261 char *allocated_end_;
262 char *allocated_current_;
263};
264
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000265} // namespace __asan
266
267#endif // ASAN_INTERNAL_H