blob: 16d8e423033daba5b3d72030cffd5dc6ab3f0fa6 [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
23#include <unistd.h> // for _exit
24
25#ifdef ANDROID
26#include <sys/atomics.h>
27#endif
28
29#ifdef ADDRESS_SANITIZER
30# error "The AddressSanitizer run-time should not be"
31 " instrumented by AddressSanitizer"
32#endif
33
34// All internal functions in asan reside inside the __asan namespace
35// to avoid namespace collisions with the user programs.
36// Seperate namespace also makes it simpler to distinguish the asan run-time
37// functions from the instrumented user code in a profile.
38namespace __asan {
39
40class AsanThread;
41struct AsanStackTrace;
42
Kostya Serebryany218a9b72011-11-30 18:50:23 +000043// asan_rtl.cc
Kostya Serebryany1e172b42011-11-30 01:07:02 +000044void CheckFailed(const char *cond, const char *file, int line);
45void ShowStatsAndAbort();
46
Kostya Serebryany218a9b72011-11-30 18:50:23 +000047// asan_globals.cc
Kostya Serebryany1e172b42011-11-30 01:07:02 +000048bool DescribeAddrIfGlobal(uintptr_t addr);
49
Kostya Serebryany218a9b72011-11-30 18:50:23 +000050// asan_malloc_linux.cc / asan_malloc_mac.cc
Kostya Serebryany1e172b42011-11-30 01:07:02 +000051void ReplaceSystemMalloc();
52
Kostya Serebryany218a9b72011-11-30 18:50:23 +000053// asan_linux.cc / asan_mac.cc
Kostya Serebryany1e172b42011-11-30 01:07:02 +000054void *AsanDoesNotSupportStaticLinkage();
55void *asan_mmap(void *addr, size_t length, int prot, int flags,
56 int fd, uint64_t offset);
57ssize_t asan_write(int fd, const void *buf, size_t count);
58
Kostya Serebryany218a9b72011-11-30 18:50:23 +000059// asan_printf.cc
Kostya Serebryany1e172b42011-11-30 01:07:02 +000060void RawWrite(const char *buffer);
61int SNPrint(char *buffer, size_t length, const char *format, ...);
62void Printf(const char *format, ...);
63void Report(const char *format, ...);
64
Kostya Serebryany218a9b72011-11-30 18:50:23 +000065// asan_poisoning.cc
66// Poisons the shadow memory for "size" bytes starting from "addr".
67void PoisonShadow(uintptr_t addr, size_t size, uint8_t value);
68// Poisons the shadow memory for "redzone_size" bytes starting from
69// "addr + size".
70void PoisonShadowPartialRightRedzone(uintptr_t addr,
71 uintptr_t size,
72 uintptr_t redzone_size,
73 uint8_t value);
74
75
Kostya Serebryany1e172b42011-11-30 01:07:02 +000076extern size_t FLAG_quarantine_size;
77extern int FLAG_demangle;
78extern bool FLAG_symbolize;
79extern int FLAG_v;
80extern bool FLAG_mt;
81extern size_t FLAG_redzone;
82extern int FLAG_debug;
83extern bool FLAG_poison_shadow;
84extern int FLAG_report_globals;
85extern size_t FLAG_malloc_context_size;
86extern bool FLAG_stats;
87extern bool FLAG_replace_str;
88extern bool FLAG_replace_intrin;
89extern bool FLAG_replace_cfallocator;
90extern bool FLAG_fast_unwind;
91extern bool FLAG_use_fake_stack;
92extern size_t FLAG_max_malloc_fill_size;
93extern int FLAG_exitcode;
94extern bool FLAG_allow_user_poisoning;
95
96extern int asan_inited;
97// Used to avoid infinite recursion in __asan_init().
98extern bool asan_init_is_running;
99
100enum LinkerInitialized { LINKER_INITIALIZED = 0 };
101
102#ifndef ASAN_DIE
103#define ASAN_DIE _exit(FLAG_exitcode)
104#endif // ASAN_DIE
105
106#define CHECK(cond) do { if (!(cond)) { \
107 CheckFailed(#cond, __FILE__, __LINE__); \
108}}while(0)
109
110#define RAW_CHECK_MSG(expr, msg) do { \
111 if (!(expr)) { \
112 RawWrite(msg); \
113 ASAN_DIE; \
114 } \
115} while (0)
116
117#define RAW_CHECK(expr) RAW_CHECK_MSG(expr, #expr)
118
119#define UNIMPLEMENTED() CHECK("unimplemented" && 0)
120
121#define ASAN_ARRAY_SIZE(a) (sizeof(a)/sizeof((a)[0]))
122
123const size_t kWordSize = __WORDSIZE / 8;
124const size_t kWordSizeInBits = 8 * kWordSize;
125const size_t kPageSizeBits = 12;
126const size_t kPageSize = 1UL << kPageSizeBits;
127
128#define GET_CALLER_PC() (uintptr_t)__builtin_return_address(0)
129#define GET_CURRENT_FRAME() (uintptr_t)__builtin_frame_address(0)
130
131#define GET_BP_PC_SP \
132 uintptr_t bp = GET_CURRENT_FRAME(); \
133 uintptr_t pc = GET_CALLER_PC(); \
134 uintptr_t local_stack; \
135 uintptr_t sp = (uintptr_t)&local_stack;
136
137// These magic values are written to shadow for better error reporting.
138const int kAsanHeapLeftRedzoneMagic = 0xfa;
139const int kAsanHeapRightRedzoneMagic = 0xfb;
140const int kAsanHeapFreeMagic = 0xfd;
141const int kAsanStackLeftRedzoneMagic = 0xf1;
142const int kAsanStackMidRedzoneMagic = 0xf2;
143const int kAsanStackRightRedzoneMagic = 0xf3;
144const int kAsanStackPartialRedzoneMagic = 0xf4;
145const int kAsanStackAfterReturnMagic = 0xf5;
146const int kAsanUserPoisonedMemoryMagic = 0xf7;
147const int kAsanGlobalRedzoneMagic = 0xf9;
148
149static const uintptr_t kCurrentStackFrameMagic = 0x41B58AB3;
150static const uintptr_t kRetiredStackFrameMagic = 0x45E0360E;
151
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000152// -------------------------- Atomic ---------------- {{{1
153static inline int AtomicInc(int *a) {
154 if (!FLAG_mt) return ++(*a);
155#ifdef ANDROID
156 return __atomic_inc(a) + 1;
157#else
158 return __sync_add_and_fetch(a, 1);
159#endif
160}
161
162static inline int AtomicDec(int *a) {
163 if (!FLAG_mt) return --(*a);
164#ifdef ANDROID
165 return __atomic_dec(a) - 1;
166#else
167 return __sync_add_and_fetch(a, -1);
168#endif
169}
170
171} // namespace __asan
172
173#endif // ASAN_INTERNAL_H