blob: fc92e9c0d1610845fb7610ef6a2c564bcc92b8e4 [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;
Alexander Potapenko6f045292012-01-27 15:15:04 +000033#else
Kostya Serebryany85822082012-01-30 20:55:02 +000034# include <stdint.h> // for __WORDSIZE
Alexander Potapenko6f045292012-01-27 15:15:04 +000035#endif // _WIN32
Kostya Serebryany1e172b42011-11-30 01:07:02 +000036
Daniel Dunbar46166332011-12-02 01:32:27 +000037// If __WORDSIZE was undefined by the platform, define it in terms of the
38// compiler built-in __LP64__.
39#ifndef __WORDSIZE
40#if __LP64__
41#define __WORDSIZE 64
42#else
43#define __WORDSIZE 32
44#endif
45#endif
46
Alexander Potapenkoc8365232012-01-27 10:52:37 +000047#if !defined(__has_feature)
48#define __has_feature(x) 0
49#endif
50
Kostya Serebryany13ebae62011-12-27 21:57:12 +000051#if defined(__has_feature) && __has_feature(address_sanitizer)
Kostya Serebryany1e172b42011-11-30 01:07:02 +000052# error "The AddressSanitizer run-time should not be"
53 " instrumented by AddressSanitizer"
54#endif
55
Kostya Serebryanyc6f22232011-12-08 18:30:42 +000056// Build-time configuration options.
57
Kostya Serebryanyc6f22232011-12-08 18:30:42 +000058// If set, asan will install its own SEGV signal handler.
59#ifndef ASAN_NEEDS_SEGV
60# define ASAN_NEEDS_SEGV 1
61#endif
62
63// If set, asan will intercept C++ exception api call(s).
64#ifndef ASAN_HAS_EXCEPTIONS
65# define ASAN_HAS_EXCEPTIONS 1
66#endif
67
68// If set, asan uses the values of SHADOW_SCALE and SHADOW_OFFSET
69// provided by the instrumented objects. Otherwise constants are used.
70#ifndef ASAN_FLEXIBLE_MAPPING_AND_OFFSET
71# define ASAN_FLEXIBLE_MAPPING_AND_OFFSET 0
72#endif
73
Kostya Serebryany1e172b42011-11-30 01:07:02 +000074// All internal functions in asan reside inside the __asan namespace
75// to avoid namespace collisions with the user programs.
76// Seperate namespace also makes it simpler to distinguish the asan run-time
77// functions from the instrumented user code in a profile.
78namespace __asan {
79
80class AsanThread;
81struct AsanStackTrace;
82
Kostya Serebryany218a9b72011-11-30 18:50:23 +000083// asan_rtl.cc
Kostya Serebryany1e172b42011-11-30 01:07:02 +000084void CheckFailed(const char *cond, const char *file, int line);
85void ShowStatsAndAbort();
86
Kostya Serebryany218a9b72011-11-30 18:50:23 +000087// asan_globals.cc
Kostya Serebryany1e172b42011-11-30 01:07:02 +000088bool DescribeAddrIfGlobal(uintptr_t addr);
89
Kostya Serebryany218a9b72011-11-30 18:50:23 +000090// asan_malloc_linux.cc / asan_malloc_mac.cc
Kostya Serebryany1e172b42011-11-30 01:07:02 +000091void ReplaceSystemMalloc();
92
Kostya Serebryanyde496f42011-12-28 22:58:01 +000093void OutOfMemoryMessageAndDie(const char *mem_type, size_t size);
94
Kostya Serebryany218a9b72011-11-30 18:50:23 +000095// asan_linux.cc / asan_mac.cc
Kostya Serebryany1e172b42011-11-30 01:07:02 +000096void *AsanDoesNotSupportStaticLinkage();
Kostya Serebryanyde496f42011-12-28 22:58:01 +000097int AsanOpenReadonly(const char* filename);
Alexander Potapenko1e316d72012-01-13 12:59:48 +000098const char *AsanGetEnv(const char *name);
Kostya Serebryanyde496f42011-12-28 22:58:01 +000099
Kostya Serebryanya874fe52011-12-28 23:28:54 +0000100void *AsanMmapFixedNoReserve(uintptr_t fixed_addr, size_t size);
101void *AsanMmapFixedReserve(uintptr_t fixed_addr, size_t size);
102void *AsanMprotect(uintptr_t fixed_addr, size_t size);
Kostya Serebryanyde496f42011-12-28 22:58:01 +0000103void *AsanMmapSomewhereOrDie(size_t size, const char *where);
104void AsanUnmapOrDie(void *ptr, size_t size);
105
Kostya Serebryanyef14ff62012-01-06 02:12:25 +0000106void AsanDisableCoreDumper();
Kostya Serebryany9107c262012-01-06 19:11:09 +0000107void GetPcSpBp(void *context, uintptr_t *pc, uintptr_t *sp, uintptr_t *bp);
Kostya Serebryanyef14ff62012-01-06 02:12:25 +0000108
Kostya Serebryany0ecf5eb2012-01-09 23:11:26 +0000109size_t AsanRead(int fd, void *buf, size_t count);
110size_t AsanWrite(int fd, const void *buf, size_t count);
Kostya Serebryanyde496f42011-12-28 22:58:01 +0000111int AsanClose(int fd);
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000112
Kostya Serebryany4803ab92012-01-09 18:53:15 +0000113bool AsanInterceptsSignal(int signum);
Kostya Serebryanya7e760a2012-01-09 19:18:27 +0000114void InstallSignalHandlers();
Kostya Serebryany0ecf5eb2012-01-09 23:11:26 +0000115int GetPid();
Kostya Serebryanycc4e6862012-01-11 02:21:06 +0000116uintptr_t GetThreadSelf();
Kostya Serebryanydde7c332012-01-11 02:39:16 +0000117int AtomicInc(int *a);
Kostya Serebryanycc4e6862012-01-11 02:21:06 +0000118
119// Wrapper for TLS/TSD.
120void AsanTSDInit();
121void *AsanTSDGet();
122void AsanTSDSet(void *tsd);
Kostya Serebryany4803ab92012-01-09 18:53:15 +0000123
Kostya Serebryanydf499b42012-01-05 00:44:33 +0000124// Opens the file 'file_name" and reads up to 'max_len' bytes.
125// The resulting buffer is mmaped and stored in '*buff'.
126// The size of the mmaped region is stored in '*buff_size',
Kostya Serebryany0ecf5eb2012-01-09 23:11:26 +0000127// Returns the number of read bytes or 0 if file can not be opened.
128size_t ReadFileToBuffer(const char *file_name, char **buff,
129 size_t *buff_size, size_t max_len);
Kostya Serebryanydf499b42012-01-05 00:44:33 +0000130
Kostya Serebryany218a9b72011-11-30 18:50:23 +0000131// asan_printf.cc
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000132void RawWrite(const char *buffer);
133int SNPrint(char *buffer, size_t length, const char *format, ...);
134void Printf(const char *format, ...);
Kostya Serebryanydf499b42012-01-05 00:44:33 +0000135int SScanf(const char *str, const char *format, ...);
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000136void Report(const char *format, ...);
137
Kostya Serebryany2d8b3bd2011-12-02 18:42:04 +0000138// Don't use std::min and std::max, to minimize dependency on libstdc++.
139template<class T> T Min(T a, T b) { return a < b ? a : b; }
140template<class T> T Max(T a, T b) { return a > b ? a : b; }
141
Kostya Serebryany218a9b72011-11-30 18:50:23 +0000142// asan_poisoning.cc
143// Poisons the shadow memory for "size" bytes starting from "addr".
144void PoisonShadow(uintptr_t addr, size_t size, uint8_t value);
145// Poisons the shadow memory for "redzone_size" bytes starting from
146// "addr + size".
147void PoisonShadowPartialRightRedzone(uintptr_t addr,
148 uintptr_t size,
149 uintptr_t redzone_size,
150 uint8_t value);
151
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000152extern size_t FLAG_quarantine_size;
153extern int FLAG_demangle;
154extern bool FLAG_symbolize;
155extern int FLAG_v;
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000156extern size_t FLAG_redzone;
157extern int FLAG_debug;
158extern bool FLAG_poison_shadow;
159extern int FLAG_report_globals;
160extern size_t FLAG_malloc_context_size;
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000161extern bool FLAG_replace_str;
162extern bool FLAG_replace_intrin;
163extern bool FLAG_replace_cfallocator;
164extern bool FLAG_fast_unwind;
165extern bool FLAG_use_fake_stack;
166extern size_t FLAG_max_malloc_fill_size;
167extern int FLAG_exitcode;
168extern bool FLAG_allow_user_poisoning;
Kostya Serebryany4803ab92012-01-09 18:53:15 +0000169extern bool FLAG_handle_segv;
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000170
171extern int asan_inited;
172// Used to avoid infinite recursion in __asan_init().
173extern bool asan_init_is_running;
174
175enum LinkerInitialized { LINKER_INITIALIZED = 0 };
176
Kostya Serebryany0ecf5eb2012-01-09 23:11:26 +0000177void AsanDie();
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000178
179#define CHECK(cond) do { if (!(cond)) { \
180 CheckFailed(#cond, __FILE__, __LINE__); \
181}}while(0)
182
183#define RAW_CHECK_MSG(expr, msg) do { \
184 if (!(expr)) { \
185 RawWrite(msg); \
Kostya Serebryany0ecf5eb2012-01-09 23:11:26 +0000186 AsanDie(); \
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000187 } \
188} while (0)
189
190#define RAW_CHECK(expr) RAW_CHECK_MSG(expr, #expr)
191
192#define UNIMPLEMENTED() CHECK("unimplemented" && 0)
193
194#define ASAN_ARRAY_SIZE(a) (sizeof(a)/sizeof((a)[0]))
195
196const size_t kWordSize = __WORDSIZE / 8;
197const size_t kWordSizeInBits = 8 * kWordSize;
198const size_t kPageSizeBits = 12;
199const size_t kPageSize = 1UL << kPageSizeBits;
200
Alexander Potapenko6f045292012-01-27 15:15:04 +0000201#ifndef _WIN32
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000202#define GET_CALLER_PC() (uintptr_t)__builtin_return_address(0)
203#define GET_CURRENT_FRAME() (uintptr_t)__builtin_frame_address(0)
Alexander Potapenko6f045292012-01-27 15:15:04 +0000204#else
205// TODO(timurrrr): implement.
206#define GET_CALLER_PC() (uintptr_t)0
207#define GET_CURRENT_FRAME() (uintptr_t)0
208#endif
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000209
210#define GET_BP_PC_SP \
211 uintptr_t bp = GET_CURRENT_FRAME(); \
212 uintptr_t pc = GET_CALLER_PC(); \
213 uintptr_t local_stack; \
214 uintptr_t sp = (uintptr_t)&local_stack;
215
216// These magic values are written to shadow for better error reporting.
217const int kAsanHeapLeftRedzoneMagic = 0xfa;
218const int kAsanHeapRightRedzoneMagic = 0xfb;
219const int kAsanHeapFreeMagic = 0xfd;
220const int kAsanStackLeftRedzoneMagic = 0xf1;
221const int kAsanStackMidRedzoneMagic = 0xf2;
222const int kAsanStackRightRedzoneMagic = 0xf3;
223const int kAsanStackPartialRedzoneMagic = 0xf4;
224const int kAsanStackAfterReturnMagic = 0xf5;
225const int kAsanUserPoisonedMemoryMagic = 0xf7;
226const int kAsanGlobalRedzoneMagic = 0xf9;
Kostya Serebryany6b30e2c2011-12-15 17:41:30 +0000227const int kAsanInternalHeapMagic = 0xfe;
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000228
229static const uintptr_t kCurrentStackFrameMagic = 0x41B58AB3;
230static const uintptr_t kRetiredStackFrameMagic = 0x45E0360E;
231
Kostya Serebryanyde496f42011-12-28 22:58:01 +0000232// --------------------------- Bit twiddling ------- {{{1
233inline bool IsPowerOfTwo(size_t x) {
234 return (x & (x - 1)) == 0;
235}
236
237inline size_t RoundUpTo(size_t size, size_t boundary) {
238 CHECK(IsPowerOfTwo(boundary));
239 return (size + boundary - 1) & ~(boundary - 1);
240}
241
Kostya Serebryanyb89567c2011-12-02 21:02:20 +0000242// -------------------------- LowLevelAllocator ----- {{{1
243// A simple low-level memory allocator for internal use.
244class LowLevelAllocator {
245 public:
246 explicit LowLevelAllocator(LinkerInitialized) {}
247 // 'size' must be a power of two.
248 // Requires an external lock.
249 void *Allocate(size_t size);
250 private:
251 char *allocated_end_;
252 char *allocated_current_;
253};
254
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000255} // namespace __asan
256
257#endif // ASAN_INTERNAL_H