blob: 107e16ee31b942534e79aaeb072d2dc9a14ab7f9 [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 intercept C++ exception api call(s).
34#ifndef ASAN_HAS_EXCEPTIONS
35# define ASAN_HAS_EXCEPTIONS 1
36#endif
37
Evgeniy Stepanov8ae44ac2012-02-27 13:07:29 +000038// If set, values like allocator chunk size, as well as defaults for some flags
39// will be changed towards less memory overhead.
40#ifndef ASAN_LOW_MEMORY
Kostya Serebryanye52810d2012-12-25 07:17:17 +000041#if SANITIZER_WORDSIZE == 32
Evgeniy Stepanov9712af92012-09-28 10:07:53 +000042# define ASAN_LOW_MEMORY 1
Kostya Serebryanye52810d2012-12-25 07:17:17 +000043#else
Evgeniy Stepanov9712af92012-09-28 10:07:53 +000044# define ASAN_LOW_MEMORY 0
45# endif
Evgeniy Stepanov8ae44ac2012-02-27 13:07:29 +000046#endif
47
Stephen Hines2d1fdb22014-05-28 23:58:16 -070048#ifndef ASAN_DYNAMIC
49# ifdef PIC
50# define ASAN_DYNAMIC 1
51# else
52# define ASAN_DYNAMIC 0
53# endif
54#endif
55
Kostya Serebryany1e172b42011-11-30 01:07:02 +000056// All internal functions in asan reside inside the __asan namespace
57// to avoid namespace collisions with the user programs.
Stephen Hines2d1fdb22014-05-28 23:58:16 -070058// Separate namespace also makes it simpler to distinguish the asan run-time
Kostya Serebryany1e172b42011-11-30 01:07:02 +000059// functions from the instrumented user code in a profile.
60namespace __asan {
61
62class AsanThread;
Kostya Serebryany1b5ea8f2012-08-28 14:11:57 +000063using __sanitizer::StackTrace;
Kostya Serebryany1e172b42011-11-30 01:07:02 +000064
Stephen Hines2d1fdb22014-05-28 23:58:16 -070065void AsanInitFromRtl();
66
Kostya Serebryany218a9b72011-11-30 18:50:23 +000067// asan_rtl.cc
Timur Iskhodzhanovb55c88d2012-03-13 16:29:25 +000068void NORETURN ShowStatsAndAbort();
Kostya Serebryany1e172b42011-11-30 01:07:02 +000069
Kostya Serebryany218a9b72011-11-30 18:50:23 +000070// asan_malloc_linux.cc / asan_malloc_mac.cc
Kostya Serebryany1e172b42011-11-30 01:07:02 +000071void ReplaceSystemMalloc();
72
Alexander Potapenkof73a6a32012-02-13 17:09:40 +000073// asan_linux.cc / asan_mac.cc / asan_win.cc
Kostya Serebryany1e172b42011-11-30 01:07:02 +000074void *AsanDoesNotSupportStaticLinkage();
Stephen Hines2d1fdb22014-05-28 23:58:16 -070075void AsanCheckDynamicRTPrereqs();
76void AsanCheckIncompatibleRT();
Kostya Serebryanyde496f42011-12-28 22:58:01 +000077
Stephen Hines2d1fdb22014-05-28 23:58:16 -070078void AsanOnSIGSEGV(int, void *siginfo, void *context);
Kostya Serebryanyef14ff62012-01-06 02:12:25 +000079
Stephen Hines86277eb2015-03-23 12:06:32 -070080void DisableReexec();
Alexander Potapenkoeb8c46e2012-08-24 09:22:05 +000081void MaybeReexec();
Alexey Samsonov57db4ba2013-01-17 15:45:28 +000082void ReadContextStack(void *context, uptr *stack, uptr *ssize);
Alexander Potapenko75b19eb2012-07-23 14:07:58 +000083void AsanPlatformThreadInit();
Alexey Samsonov46efcb02013-05-24 11:46:56 +000084void StopInitOrderChecking();
Kostya Serebryanycc4e6862012-01-11 02:21:06 +000085
86// Wrapper for TLS/TSD.
Kostya Serebryanyf58f9982012-02-07 00:27:15 +000087void AsanTSDInit(void (*destructor)(void *tsd));
Kostya Serebryanycc4e6862012-01-11 02:21:06 +000088void *AsanTSDGet();
89void AsanTSDSet(void *tsd);
Sergey Matveeve86e35f2013-10-14 12:01:05 +000090void PlatformTSDDtor(void *tsd);
Kostya Serebryany4803ab92012-01-09 18:53:15 +000091
Alexey Samsonove9541012012-06-06 13:11:29 +000092void AppendToErrorMessageBuffer(const char *buffer);
Kostya Serebryany1e172b42011-11-30 01:07:02 +000093
Stephen Hines6a211c52014-07-21 00:49:56 -070094void *AsanDlSymNext(const char *sym);
95
Pirama Arumuga Nainarcdce50b2015-07-01 12:26:56 -070096void ReserveShadowMemoryRange(uptr beg, uptr end, const char *name);
Stephen Hines86277eb2015-03-23 12:06:32 -070097
Stephen Hines2d1fdb22014-05-28 23:58:16 -070098// Platform-specific options.
Evgeniy Stepanov30e110e2013-03-19 14:54:17 +000099#if SANITIZER_MAC
Alexey Samsonov38dd4ed2012-03-20 10:54:40 +0000100bool PlatformHasDifferentMemcpyAndMemmove();
101# define PLATFORM_HAS_DIFFERENT_MEMCPY_AND_MEMMOVE \
102 (PlatformHasDifferentMemcpyAndMemmove())
103#else
104# define PLATFORM_HAS_DIFFERENT_MEMCPY_AND_MEMMOVE true
Alexey Samsonov649a2702013-04-03 07:29:53 +0000105#endif // SANITIZER_MAC
Alexey Samsonov38dd4ed2012-03-20 10:54:40 +0000106
Alexey Samsonov6a08d292012-12-07 22:01:28 +0000107// Add convenient macro for interface functions that may be represented as
108// weak hooks.
109#define ASAN_MALLOC_HOOK(ptr, size) \
Stephen Hines6a211c52014-07-21 00:49:56 -0700110 if (&__sanitizer_malloc_hook) __sanitizer_malloc_hook(ptr, size)
Alexey Samsonov6a08d292012-12-07 22:01:28 +0000111#define ASAN_FREE_HOOK(ptr) \
Stephen Hines6a211c52014-07-21 00:49:56 -0700112 if (&__sanitizer_free_hook) __sanitizer_free_hook(ptr)
Alexey Samsonov6a08d292012-12-07 22:01:28 +0000113#define ASAN_ON_ERROR() \
114 if (&__asan_on_error) __asan_on_error()
115
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000116extern int asan_inited;
117// Used to avoid infinite recursion in __asan_init().
118extern bool asan_init_is_running;
Alexey Samsonov47657ce2012-06-06 07:02:44 +0000119extern void (*death_callback)(void);
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000120
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000121// These magic values are written to shadow for better error reporting.
122const int kAsanHeapLeftRedzoneMagic = 0xfa;
123const int kAsanHeapRightRedzoneMagic = 0xfb;
124const int kAsanHeapFreeMagic = 0xfd;
125const int kAsanStackLeftRedzoneMagic = 0xf1;
126const int kAsanStackMidRedzoneMagic = 0xf2;
127const int kAsanStackRightRedzoneMagic = 0xf3;
128const int kAsanStackPartialRedzoneMagic = 0xf4;
129const int kAsanStackAfterReturnMagic = 0xf5;
Kostya Serebryany3945c582012-08-21 14:10:25 +0000130const int kAsanInitializationOrderMagic = 0xf6;
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000131const int kAsanUserPoisonedMemoryMagic = 0xf7;
Stephen Hines2d1fdb22014-05-28 23:58:16 -0700132const int kAsanContiguousContainerOOBMagic = 0xfc;
Alexey Samsonovd4b5db82012-12-04 01:38:15 +0000133const int kAsanStackUseAfterScopeMagic = 0xf8;
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000134const int kAsanGlobalRedzoneMagic = 0xf9;
Kostya Serebryany6b30e2c2011-12-15 17:41:30 +0000135const int kAsanInternalHeapMagic = 0xfe;
Stephen Hines6d186232014-11-26 17:56:19 -0800136const int kAsanArrayCookieMagic = 0xac;
137const int kAsanIntraObjectRedzone = 0xbb;
Stephen Hines86277eb2015-03-23 12:06:32 -0700138const int kAsanAllocaLeftMagic = 0xca;
139const int kAsanAllocaRightMagic = 0xcb;
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000140
Kostya Serebryany3f4c3872012-05-31 14:35:53 +0000141static const uptr kCurrentStackFrameMagic = 0x41B58AB3;
142static const uptr kRetiredStackFrameMagic = 0x45E0360E;
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000143
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000144} // namespace __asan
145
146#endif // ASAN_INTERNAL_H