blob: 7a4d74472bcdf4afc3b822942664a284631a3bfb [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
Alexey Samsonov7ed1d2b2012-07-10 07:41:27 +000017#include "asan_flags.h"
Alexey Samsonovc70fa282013-01-31 13:46:14 +000018#include "asan_interface_internal.h"
Alexey Samsonov47657ce2012-06-06 07:02:44 +000019#include "sanitizer_common/sanitizer_common.h"
Alexey Samsonov94b50362012-06-05 14:25:27 +000020#include "sanitizer_common/sanitizer_internal_defs.h"
Kostya Serebryany1b5ea8f2012-08-28 14:11:57 +000021#include "sanitizer_common/sanitizer_stacktrace.h"
Kostya Serebryany16e00752012-05-31 13:42:53 +000022#include "sanitizer_common/sanitizer_libc.h"
Kostya Serebryanyb3cedf92012-05-29 12:18:18 +000023
Alexey Samsonovb823e3c2012-02-22 14:07:06 +000024#define ASAN_DEFAULT_FAILURE_EXITCODE 1
25
Kostya Serebryanye31eca92013-02-15 12:00:24 +000026#if __has_feature(address_sanitizer) || defined(__SANITIZE_ADDRESS__)
Kostya Serebryany1e172b42011-11-30 01:07:02 +000027# error "The AddressSanitizer run-time should not be"
28 " instrumented by AddressSanitizer"
29#endif
30
Kostya Serebryanyc6f22232011-12-08 18:30:42 +000031// Build-time configuration options.
32
Kostya Serebryanyc6f22232011-12-08 18:30:42 +000033// If set, asan will install its own SEGV signal handler.
34#ifndef ASAN_NEEDS_SEGV
Evgeniy Stepanov83cb7872013-03-19 13:54:41 +000035# if SANITIZER_ANDROID == 1
Evgeniy Stepanov9712af92012-09-28 10:07:53 +000036# define ASAN_NEEDS_SEGV 0
37# else
38# define ASAN_NEEDS_SEGV 1
39# endif
Kostya Serebryanyc6f22232011-12-08 18:30:42 +000040#endif
41
42// If set, asan will intercept C++ exception api call(s).
43#ifndef ASAN_HAS_EXCEPTIONS
44# define ASAN_HAS_EXCEPTIONS 1
45#endif
46
47// If set, asan uses the values of SHADOW_SCALE and SHADOW_OFFSET
48// provided by the instrumented objects. Otherwise constants are used.
49#ifndef ASAN_FLEXIBLE_MAPPING_AND_OFFSET
50# define ASAN_FLEXIBLE_MAPPING_AND_OFFSET 0
51#endif
52
Evgeniy Stepanov8ae44ac2012-02-27 13:07:29 +000053// If set, values like allocator chunk size, as well as defaults for some flags
54// will be changed towards less memory overhead.
55#ifndef ASAN_LOW_MEMORY
Kostya Serebryanye52810d2012-12-25 07:17:17 +000056#if SANITIZER_WORDSIZE == 32
Evgeniy Stepanov9712af92012-09-28 10:07:53 +000057# define ASAN_LOW_MEMORY 1
Kostya Serebryanye52810d2012-12-25 07:17:17 +000058#else
Evgeniy Stepanov9712af92012-09-28 10:07:53 +000059# define ASAN_LOW_MEMORY 0
60# endif
Evgeniy Stepanov8ae44ac2012-02-27 13:07:29 +000061#endif
62
Kostya Serebryanye1353432013-02-20 14:28:08 +000063#ifndef ASAN_USE_PREINIT_ARRAY
Evgeniy Stepanov83cb7872013-03-19 13:54:41 +000064# define ASAN_USE_PREINIT_ARRAY (SANITIZER_LINUX && !SANITIZER_ANDROID)
Kostya Serebryanye1353432013-02-20 14:28:08 +000065#endif
66
Kostya Serebryany1e172b42011-11-30 01:07:02 +000067// All internal functions in asan reside inside the __asan namespace
68// to avoid namespace collisions with the user programs.
69// Seperate namespace also makes it simpler to distinguish the asan run-time
70// functions from the instrumented user code in a profile.
71namespace __asan {
72
73class AsanThread;
Kostya Serebryany1b5ea8f2012-08-28 14:11:57 +000074using __sanitizer::StackTrace;
Kostya Serebryany1e172b42011-11-30 01:07:02 +000075
Kostya Serebryany218a9b72011-11-30 18:50:23 +000076// asan_rtl.cc
Timur Iskhodzhanovb55c88d2012-03-13 16:29:25 +000077void NORETURN ShowStatsAndAbort();
Kostya Serebryany1e172b42011-11-30 01:07:02 +000078
Alexey Samsonov4d5f98d2012-04-06 08:21:08 +000079void ReplaceOperatorsNewAndDelete();
Kostya Serebryany218a9b72011-11-30 18:50:23 +000080// asan_malloc_linux.cc / asan_malloc_mac.cc
Kostya Serebryany1e172b42011-11-30 01:07:02 +000081void ReplaceSystemMalloc();
82
Alexander Potapenkof73a6a32012-02-13 17:09:40 +000083// asan_linux.cc / asan_mac.cc / asan_win.cc
Kostya Serebryany1e172b42011-11-30 01:07:02 +000084void *AsanDoesNotSupportStaticLinkage();
Kostya Serebryanyde496f42011-12-28 22:58:01 +000085
Kostya Serebryany3f4c3872012-05-31 14:35:53 +000086void GetPcSpBp(void *context, uptr *pc, uptr *sp, uptr *bp);
Kostya Serebryanyef14ff62012-01-06 02:12:25 +000087
Alexander Potapenkoeb8c46e2012-08-24 09:22:05 +000088void MaybeReexec();
Kostya Serebryany4803ab92012-01-09 18:53:15 +000089bool AsanInterceptsSignal(int signum);
Alexander Potapenkof03d8af2012-04-05 10:54:52 +000090void SetAlternateSignalStack();
91void UnsetAlternateSignalStack();
Kostya Serebryanya7e760a2012-01-09 19:18:27 +000092void InstallSignalHandlers();
Alexey Samsonov57db4ba2013-01-17 15:45:28 +000093void ReadContextStack(void *context, uptr *stack, uptr *ssize);
Alexander Potapenko75b19eb2012-07-23 14:07:58 +000094void AsanPlatformThreadInit();
Alexey Samsonov46efcb02013-05-24 11:46:56 +000095void StopInitOrderChecking();
Kostya Serebryanycc4e6862012-01-11 02:21:06 +000096
97// Wrapper for TLS/TSD.
Kostya Serebryanyf58f9982012-02-07 00:27:15 +000098void AsanTSDInit(void (*destructor)(void *tsd));
Kostya Serebryanycc4e6862012-01-11 02:21:06 +000099void *AsanTSDGet();
100void AsanTSDSet(void *tsd);
Kostya Serebryany4803ab92012-01-09 18:53:15 +0000101
Alexey Samsonove9541012012-06-06 13:11:29 +0000102void AppendToErrorMessageBuffer(const char *buffer);
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000103
Alexey Samsonov38dd4ed2012-03-20 10:54:40 +0000104// Platfrom-specific options.
Evgeniy Stepanov30e110e2013-03-19 14:54:17 +0000105#if SANITIZER_MAC
Alexey Samsonov38dd4ed2012-03-20 10:54:40 +0000106bool PlatformHasDifferentMemcpyAndMemmove();
107# define PLATFORM_HAS_DIFFERENT_MEMCPY_AND_MEMMOVE \
108 (PlatformHasDifferentMemcpyAndMemmove())
109#else
110# define PLATFORM_HAS_DIFFERENT_MEMCPY_AND_MEMMOVE true
Alexey Samsonov649a2702013-04-03 07:29:53 +0000111#endif // SANITIZER_MAC
Alexey Samsonov38dd4ed2012-03-20 10:54:40 +0000112
Alexey Samsonov6a08d292012-12-07 22:01:28 +0000113// Add convenient macro for interface functions that may be represented as
114// weak hooks.
115#define ASAN_MALLOC_HOOK(ptr, size) \
116 if (&__asan_malloc_hook) __asan_malloc_hook(ptr, size)
117#define ASAN_FREE_HOOK(ptr) \
118 if (&__asan_free_hook) __asan_free_hook(ptr)
119#define ASAN_ON_ERROR() \
120 if (&__asan_on_error) __asan_on_error()
121
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000122extern int asan_inited;
123// Used to avoid infinite recursion in __asan_init().
124extern bool asan_init_is_running;
Alexey Samsonov47657ce2012-06-06 07:02:44 +0000125extern void (*death_callback)(void);
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000126
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000127// These magic values are written to shadow for better error reporting.
128const int kAsanHeapLeftRedzoneMagic = 0xfa;
129const int kAsanHeapRightRedzoneMagic = 0xfb;
130const int kAsanHeapFreeMagic = 0xfd;
131const int kAsanStackLeftRedzoneMagic = 0xf1;
132const int kAsanStackMidRedzoneMagic = 0xf2;
133const int kAsanStackRightRedzoneMagic = 0xf3;
134const int kAsanStackPartialRedzoneMagic = 0xf4;
135const int kAsanStackAfterReturnMagic = 0xf5;
Kostya Serebryany3945c582012-08-21 14:10:25 +0000136const int kAsanInitializationOrderMagic = 0xf6;
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000137const int kAsanUserPoisonedMemoryMagic = 0xf7;
Alexey Samsonovd4b5db82012-12-04 01:38:15 +0000138const int kAsanStackUseAfterScopeMagic = 0xf8;
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000139const int kAsanGlobalRedzoneMagic = 0xf9;
Kostya Serebryany6b30e2c2011-12-15 17:41:30 +0000140const int kAsanInternalHeapMagic = 0xfe;
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000141
Kostya Serebryany3f4c3872012-05-31 14:35:53 +0000142static const uptr kCurrentStackFrameMagic = 0x41B58AB3;
143static const uptr kRetiredStackFrameMagic = 0x45E0360E;
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000144
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000145} // namespace __asan
146
147#endif // ASAN_INTERNAL_H