blob: 68a539424d66a9a3d44561701ca2e51fb656daab [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 Serebryany16e00752012-05-31 13:42:53 +000017#include "sanitizer_common/sanitizer_libc.h"
Kostya Serebryanyb3cedf92012-05-29 12:18:18 +000018
Alexander Potapenko6f045292012-01-27 15:15:04 +000019#if !defined(__linux__) && !defined(__APPLE__) && !defined(_WIN32)
Kostya Serebryanyd6567c52011-12-01 21:40:52 +000020# error "This operating system is not supported by AddressSanitizer"
21#endif
22
Alexey Samsonovb823e3c2012-02-22 14:07:06 +000023#include <stddef.h> // for size_t, uintptr_t, etc.
Alexander Potapenko6f045292012-01-27 15:15:04 +000024
Kostya Serebryany85822082012-01-30 20:55:02 +000025#if defined(_WIN32)
Timur Iskhodzhanov8c505ef2012-05-21 14:25:36 +000026# if defined(__clang__)
27typedef int intptr_t;
28typedef unsigned int uintptr_t;
29# endif
30
Alexander Potapenko6f045292012-01-27 15:15:04 +000031// There's no <stdint.h> in Visual Studio 9, so we have to define [u]int*_t.
32typedef unsigned __int8 uint8_t;
33typedef unsigned __int16 uint16_t;
34typedef unsigned __int32 uint32_t;
35typedef unsigned __int64 uint64_t;
36typedef __int8 int8_t;
37typedef __int16 int16_t;
38typedef __int32 int32_t;
39typedef __int64 int64_t;
Timur Iskhodzhanov600972e2012-02-24 15:28:43 +000040typedef unsigned long DWORD; // NOLINT
Alexey Samsonovadf2b032012-02-03 08:37:19 +000041
Alexander Potapenko71d3b392012-02-08 14:14:18 +000042extern "C" void* _ReturnAddress(void);
43# pragma intrinsic(_ReturnAddress)
44
Alexey Samsonovadf2b032012-02-03 08:37:19 +000045# define ALIAS(x) // TODO(timurrrr): do we need this on Windows?
46# define ALIGNED(x) __declspec(align(x))
47# define NOINLINE __declspec(noinline)
Timur Iskhodzhanov23bd2bb2012-03-13 16:12:03 +000048# define NORETURN __declspec(noreturn)
Alexey Samsonovadf2b032012-02-03 08:37:19 +000049
50# define ASAN_INTERFACE_ATTRIBUTE // TODO(timurrrr): do we need this on Win?
51#else // defined(_WIN32)
Kostya Serebryany85822082012-01-30 20:55:02 +000052# include <stdint.h> // for __WORDSIZE
Alexey Samsonovadf2b032012-02-03 08:37:19 +000053
54# define ALIAS(x) __attribute__((alias(x)))
55# define ALIGNED(x) __attribute__((aligned(x)))
56# define NOINLINE __attribute__((noinline))
Alexey Samsonov95d6b332012-03-20 10:14:55 +000057# define NORETURN __attribute__((noreturn))
Alexey Samsonovadf2b032012-02-03 08:37:19 +000058
59# define ASAN_INTERFACE_ATTRIBUTE __attribute__((visibility("default")))
60#endif // defined(_WIN32)
Kostya Serebryany1e172b42011-11-30 01:07:02 +000061
Daniel Dunbar46166332011-12-02 01:32:27 +000062// If __WORDSIZE was undefined by the platform, define it in terms of the
Kostya Serebryany669f5432012-01-31 18:13:50 +000063// compiler built-ins __LP64__ and _WIN64.
Daniel Dunbar46166332011-12-02 01:32:27 +000064#ifndef __WORDSIZE
Kostya Serebryany669f5432012-01-31 18:13:50 +000065#if __LP64__ || defined(_WIN64)
Daniel Dunbar46166332011-12-02 01:32:27 +000066#define __WORDSIZE 64
67#else
68#define __WORDSIZE 32
69#endif
70#endif
71
Alexey Samsonove4092f62012-02-22 12:54:04 +000072// Limits for integral types. We have to redefine it in case we don't
73// have stdint.h (like in Visual Studio 9).
74#if __WORDSIZE == 64
75# define __INT64_C(c) c ## L
76# define __UINT64_C(c) c ## UL
77#else
78# define __INT64_C(c) c ## LL
79# define __UINT64_C(c) c ## ULL
80#endif // __WORDSIZE == 64
Alexey Samsonovbfc694d2012-02-22 16:12:46 +000081#undef INT32_MIN
82#define INT32_MIN (-2147483647-1)
83#undef INT32_MAX
84#define INT32_MAX (2147483647)
85#undef UINT32_MAX
86#define UINT32_MAX (4294967295U)
87#undef INT64_MIN
88#define INT64_MIN (-__INT64_C(9223372036854775807)-1)
89#undef INT64_MAX
90#define INT64_MAX (__INT64_C(9223372036854775807))
91#undef UINT64_MAX
92#define UINT64_MAX (__UINT64_C(18446744073709551615))
Alexey Samsonove4092f62012-02-22 12:54:04 +000093
Alexey Samsonovb823e3c2012-02-22 14:07:06 +000094#define ASAN_DEFAULT_FAILURE_EXITCODE 1
95
Timur Iskhodzhanov3e81fe42012-02-09 17:20:14 +000096#if defined(__linux__)
97# define ASAN_LINUX 1
98#else
99# define ASAN_LINUX 0
100#endif
101
102#if defined(__APPLE__)
103# define ASAN_MAC 1
104#else
105# define ASAN_MAC 0
106#endif
107
108#if defined(_WIN32)
109# define ASAN_WINDOWS 1
110#else
111# define ASAN_WINDOWS 0
112#endif
113
Alexey Samsonov34a32022012-03-26 09:07:29 +0000114#define ASAN_POSIX (ASAN_LINUX || ASAN_MAC)
115
Alexander Potapenkoc8365232012-01-27 10:52:37 +0000116#if !defined(__has_feature)
117#define __has_feature(x) 0
118#endif
119
Kostya Serebryany850a49e2012-04-06 20:36:18 +0000120#if __has_feature(address_sanitizer)
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000121# error "The AddressSanitizer run-time should not be"
122 " instrumented by AddressSanitizer"
123#endif
124
Kostya Serebryanyc6f22232011-12-08 18:30:42 +0000125// Build-time configuration options.
126
Kostya Serebryanyc6f22232011-12-08 18:30:42 +0000127// If set, asan will install its own SEGV signal handler.
128#ifndef ASAN_NEEDS_SEGV
129# define ASAN_NEEDS_SEGV 1
130#endif
131
132// If set, asan will intercept C++ exception api call(s).
133#ifndef ASAN_HAS_EXCEPTIONS
134# define ASAN_HAS_EXCEPTIONS 1
135#endif
136
137// If set, asan uses the values of SHADOW_SCALE and SHADOW_OFFSET
138// provided by the instrumented objects. Otherwise constants are used.
139#ifndef ASAN_FLEXIBLE_MAPPING_AND_OFFSET
140# define ASAN_FLEXIBLE_MAPPING_AND_OFFSET 0
141#endif
142
Evgeniy Stepanov8ae44ac2012-02-27 13:07:29 +0000143// If set, values like allocator chunk size, as well as defaults for some flags
144// will be changed towards less memory overhead.
145#ifndef ASAN_LOW_MEMORY
146# define ASAN_LOW_MEMORY 0
147#endif
148
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000149// All internal functions in asan reside inside the __asan namespace
150// to avoid namespace collisions with the user programs.
151// Seperate namespace also makes it simpler to distinguish the asan run-time
152// functions from the instrumented user code in a profile.
153namespace __asan {
154
155class AsanThread;
156struct AsanStackTrace;
157
Kostya Serebryany218a9b72011-11-30 18:50:23 +0000158// asan_rtl.cc
Timur Iskhodzhanov23bd2bb2012-03-13 16:12:03 +0000159void NORETURN CheckFailed(const char *cond, const char *file, int line);
Timur Iskhodzhanovb55c88d2012-03-13 16:29:25 +0000160void NORETURN ShowStatsAndAbort();
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000161
Kostya Serebryany218a9b72011-11-30 18:50:23 +0000162// asan_globals.cc
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000163bool DescribeAddrIfGlobal(uintptr_t addr);
164
Alexey Samsonov4d5f98d2012-04-06 08:21:08 +0000165void ReplaceOperatorsNewAndDelete();
Kostya Serebryany218a9b72011-11-30 18:50:23 +0000166// asan_malloc_linux.cc / asan_malloc_mac.cc
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000167void ReplaceSystemMalloc();
168
Kostya Serebryanyde496f42011-12-28 22:58:01 +0000169void OutOfMemoryMessageAndDie(const char *mem_type, size_t size);
170
Alexander Potapenkof73a6a32012-02-13 17:09:40 +0000171// asan_linux.cc / asan_mac.cc / asan_win.cc
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000172void *AsanDoesNotSupportStaticLinkage();
Alexander Potapenkof73a6a32012-02-13 17:09:40 +0000173bool AsanShadowRangeIsAvailable();
Kostya Serebryanyde496f42011-12-28 22:58:01 +0000174int AsanOpenReadonly(const char* filename);
Alexander Potapenko1e316d72012-01-13 12:59:48 +0000175const char *AsanGetEnv(const char *name);
Alexander Potapenko99d17eb2012-02-22 09:11:55 +0000176void AsanDumpProcessMap();
Kostya Serebryanyde496f42011-12-28 22:58:01 +0000177
Kostya Serebryanya874fe52011-12-28 23:28:54 +0000178void *AsanMmapFixedNoReserve(uintptr_t fixed_addr, size_t size);
179void *AsanMmapFixedReserve(uintptr_t fixed_addr, size_t size);
180void *AsanMprotect(uintptr_t fixed_addr, size_t size);
Kostya Serebryanyde496f42011-12-28 22:58:01 +0000181void *AsanMmapSomewhereOrDie(size_t size, const char *where);
182void AsanUnmapOrDie(void *ptr, size_t size);
183
Kostya Serebryanyef14ff62012-01-06 02:12:25 +0000184void AsanDisableCoreDumper();
Kostya Serebryany9107c262012-01-06 19:11:09 +0000185void GetPcSpBp(void *context, uintptr_t *pc, uintptr_t *sp, uintptr_t *bp);
Kostya Serebryanyef14ff62012-01-06 02:12:25 +0000186
Kostya Serebryany0ecf5eb2012-01-09 23:11:26 +0000187size_t AsanRead(int fd, void *buf, size_t count);
188size_t AsanWrite(int fd, const void *buf, size_t count);
Kostya Serebryanyde496f42011-12-28 22:58:01 +0000189int AsanClose(int fd);
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000190
Kostya Serebryany4803ab92012-01-09 18:53:15 +0000191bool AsanInterceptsSignal(int signum);
Alexander Potapenkof03d8af2012-04-05 10:54:52 +0000192void SetAlternateSignalStack();
193void UnsetAlternateSignalStack();
Kostya Serebryanya7e760a2012-01-09 19:18:27 +0000194void InstallSignalHandlers();
Kostya Serebryany0ecf5eb2012-01-09 23:11:26 +0000195int GetPid();
Kostya Serebryanycc4e6862012-01-11 02:21:06 +0000196uintptr_t GetThreadSelf();
Kostya Serebryanydde7c332012-01-11 02:39:16 +0000197int AtomicInc(int *a);
Kostya Serebryanyf1e82b82012-04-05 15:55:09 +0000198uint16_t AtomicExchange(uint16_t *a, uint16_t new_val);
Kostya Serebryanycc4e6862012-01-11 02:21:06 +0000199
200// Wrapper for TLS/TSD.
Kostya Serebryanyf58f9982012-02-07 00:27:15 +0000201void AsanTSDInit(void (*destructor)(void *tsd));
Kostya Serebryanycc4e6862012-01-11 02:21:06 +0000202void *AsanTSDGet();
203void AsanTSDSet(void *tsd);
Kostya Serebryany4803ab92012-01-09 18:53:15 +0000204
Kostya Serebryanydf499b42012-01-05 00:44:33 +0000205// Opens the file 'file_name" and reads up to 'max_len' bytes.
206// The resulting buffer is mmaped and stored in '*buff'.
207// The size of the mmaped region is stored in '*buff_size',
Kostya Serebryany0ecf5eb2012-01-09 23:11:26 +0000208// Returns the number of read bytes or 0 if file can not be opened.
209size_t ReadFileToBuffer(const char *file_name, char **buff,
210 size_t *buff_size, size_t max_len);
Kostya Serebryanydf499b42012-01-05 00:44:33 +0000211
Kostya Serebryany218a9b72011-11-30 18:50:23 +0000212// asan_printf.cc
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000213void RawWrite(const char *buffer);
Alexander Potapenkoa0935fa2012-02-08 11:45:09 +0000214int SNPrintf(char *buffer, size_t length, const char *format, ...);
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000215void Printf(const char *format, ...);
Kostya Serebryanydf499b42012-01-05 00:44:33 +0000216int SScanf(const char *str, const char *format, ...);
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000217void Report(const char *format, ...);
218
Kostya Serebryany2d8b3bd2011-12-02 18:42:04 +0000219// Don't use std::min and std::max, to minimize dependency on libstdc++.
220template<class T> T Min(T a, T b) { return a < b ? a : b; }
221template<class T> T Max(T a, T b) { return a > b ? a : b; }
222
Kostya Serebryany25c71782012-03-10 01:30:01 +0000223void SortArray(uintptr_t *array, size_t size);
224
Kostya Serebryany218a9b72011-11-30 18:50:23 +0000225// asan_poisoning.cc
226// Poisons the shadow memory for "size" bytes starting from "addr".
227void PoisonShadow(uintptr_t addr, size_t size, uint8_t value);
228// Poisons the shadow memory for "redzone_size" bytes starting from
229// "addr + size".
230void PoisonShadowPartialRightRedzone(uintptr_t addr,
231 uintptr_t size,
232 uintptr_t redzone_size,
233 uint8_t value);
234
Alexey Samsonov38dd4ed2012-03-20 10:54:40 +0000235// Platfrom-specific options.
236#ifdef __APPLE__
237bool PlatformHasDifferentMemcpyAndMemmove();
238# define PLATFORM_HAS_DIFFERENT_MEMCPY_AND_MEMMOVE \
239 (PlatformHasDifferentMemcpyAndMemmove())
240#else
241# define PLATFORM_HAS_DIFFERENT_MEMCPY_AND_MEMMOVE true
242#endif // __APPLE__
243
Alexander Potapenko62f10e72012-05-28 16:21:19 +0000244extern size_t FLAG_quarantine_size;
245extern int64_t FLAG_demangle;
246extern bool FLAG_symbolize;
247extern int64_t FLAG_v;
248extern size_t FLAG_redzone;
249extern int64_t FLAG_debug;
250extern bool FLAG_poison_shadow;
251extern int64_t FLAG_report_globals;
252extern size_t FLAG_malloc_context_size;
253extern bool FLAG_replace_str;
254extern bool FLAG_replace_intrin;
255extern bool FLAG_replace_cfallocator;
256extern bool FLAG_fast_unwind;
257extern bool FLAG_use_fake_stack;
258extern size_t FLAG_max_malloc_fill_size;
259extern int64_t FLAG_exitcode;
260extern bool FLAG_allow_user_poisoning;
261extern int64_t FLAG_sleep_before_dying;
262extern bool FLAG_handle_segv;
263extern bool FLAG_use_sigaltstack;
264extern bool FLAG_check_malloc_usable_size;
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000265
266extern int asan_inited;
267// Used to avoid infinite recursion in __asan_init().
268extern bool asan_init_is_running;
269
270enum LinkerInitialized { LINKER_INITIALIZED = 0 };
271
Timur Iskhodzhanovb55c88d2012-03-13 16:29:25 +0000272void NORETURN AsanDie();
Kostya Serebryanye1fe0fd2012-02-13 21:24:29 +0000273void SleepForSeconds(int seconds);
Timur Iskhodzhanovb55c88d2012-03-13 16:29:25 +0000274void NORETURN Exit(int exitcode);
Kostya Serebryanyf8e6fee2012-04-06 01:27:11 +0000275void NORETURN Abort();
Alexey Samsonovb823e3c2012-02-22 14:07:06 +0000276int Atexit(void (*function)(void));
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000277
278#define CHECK(cond) do { if (!(cond)) { \
279 CheckFailed(#cond, __FILE__, __LINE__); \
280}}while(0)
281
282#define RAW_CHECK_MSG(expr, msg) do { \
283 if (!(expr)) { \
284 RawWrite(msg); \
Kostya Serebryany0ecf5eb2012-01-09 23:11:26 +0000285 AsanDie(); \
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000286 } \
287} while (0)
288
289#define RAW_CHECK(expr) RAW_CHECK_MSG(expr, #expr)
290
291#define UNIMPLEMENTED() CHECK("unimplemented" && 0)
292
293#define ASAN_ARRAY_SIZE(a) (sizeof(a)/sizeof((a)[0]))
294
295const size_t kWordSize = __WORDSIZE / 8;
296const size_t kWordSizeInBits = 8 * kWordSize;
297const size_t kPageSizeBits = 12;
298const size_t kPageSize = 1UL << kPageSizeBits;
299
Timur Iskhodzhanov8c505ef2012-05-21 14:25:36 +0000300#if !defined(_WIN32) || defined(__clang__)
Kostya Serebryany1c83ae32012-02-07 18:23:54 +0000301# define GET_CALLER_PC() (uintptr_t)__builtin_return_address(0)
302# define GET_CURRENT_FRAME() (uintptr_t)__builtin_frame_address(0)
Alexander Potapenko6f045292012-01-27 15:15:04 +0000303#else
Kostya Serebryany1c83ae32012-02-07 18:23:54 +0000304# define GET_CALLER_PC() (uintptr_t)_ReturnAddress()
305// CaptureStackBackTrace doesn't need to know BP on Windows.
306// FIXME: This macro is still used when printing error reports though it's not
307// clear if the BP value is needed in the ASan reports on Windows.
308# define GET_CURRENT_FRAME() (uintptr_t)0xDEADBEEF
Timur Iskhodzhanov8c505ef2012-05-21 14:25:36 +0000309#endif
310
311#ifndef _WIN32
312const size_t kMmapGranularity = kPageSize;
313# define THREAD_CALLING_CONV
314typedef void* thread_return_t;
315#else
316const size_t kMmapGranularity = 1UL << 16;
Timur Iskhodzhanov600972e2012-02-24 15:28:43 +0000317# define THREAD_CALLING_CONV __stdcall
318typedef DWORD thread_return_t;
Timur Iskhodzhanov3e81fe42012-02-09 17:20:14 +0000319
320# ifndef ASAN_USE_EXTERNAL_SYMBOLIZER
Timur Iskhodzhanov8c505ef2012-05-21 14:25:36 +0000321# define ASAN_USE_EXTERNAL_SYMBOLIZER __asan_WinSymbolize
322bool __asan_WinSymbolize(const void *addr, char *out_buffer, int buffer_size);
Timur Iskhodzhanov3e81fe42012-02-09 17:20:14 +0000323# endif
Alexander Potapenko6f045292012-01-27 15:15:04 +0000324#endif
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000325
Timur Iskhodzhanov600972e2012-02-24 15:28:43 +0000326typedef thread_return_t (THREAD_CALLING_CONV *thread_callback_t)(void* arg);
327
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000328// These magic values are written to shadow for better error reporting.
329const int kAsanHeapLeftRedzoneMagic = 0xfa;
330const int kAsanHeapRightRedzoneMagic = 0xfb;
331const int kAsanHeapFreeMagic = 0xfd;
332const int kAsanStackLeftRedzoneMagic = 0xf1;
333const int kAsanStackMidRedzoneMagic = 0xf2;
334const int kAsanStackRightRedzoneMagic = 0xf3;
335const int kAsanStackPartialRedzoneMagic = 0xf4;
336const int kAsanStackAfterReturnMagic = 0xf5;
337const int kAsanUserPoisonedMemoryMagic = 0xf7;
338const int kAsanGlobalRedzoneMagic = 0xf9;
Kostya Serebryany6b30e2c2011-12-15 17:41:30 +0000339const int kAsanInternalHeapMagic = 0xfe;
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000340
341static const uintptr_t kCurrentStackFrameMagic = 0x41B58AB3;
342static const uintptr_t kRetiredStackFrameMagic = 0x45E0360E;
343
Kostya Serebryanyde496f42011-12-28 22:58:01 +0000344// --------------------------- Bit twiddling ------- {{{1
345inline bool IsPowerOfTwo(size_t x) {
346 return (x & (x - 1)) == 0;
347}
348
349inline size_t RoundUpTo(size_t size, size_t boundary) {
350 CHECK(IsPowerOfTwo(boundary));
351 return (size + boundary - 1) & ~(boundary - 1);
352}
353
Kostya Serebryanyb89567c2011-12-02 21:02:20 +0000354// -------------------------- LowLevelAllocator ----- {{{1
355// A simple low-level memory allocator for internal use.
356class LowLevelAllocator {
357 public:
358 explicit LowLevelAllocator(LinkerInitialized) {}
359 // 'size' must be a power of two.
360 // Requires an external lock.
361 void *Allocate(size_t size);
362 private:
363 char *allocated_end_;
364 char *allocated_current_;
365};
366
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000367} // namespace __asan
368
369#endif // ASAN_INTERNAL_H