1 //===-- asan_internal.h -----------------------------------------*- C++ -*-===//
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //===----------------------------------------------------------------------===//
9 // This file is a part of AddressSanitizer, an address sanity checker.
11 // ASan-private header which defines various general utilities.
12 //===----------------------------------------------------------------------===//
13 #ifndef ASAN_INTERNAL_H
14 #define ASAN_INTERNAL_H
16 #include "asan_flags.h"
17 #include "asan_interface_internal.h"
18 #include "sanitizer_common/sanitizer_common.h"
19 #include "sanitizer_common/sanitizer_internal_defs.h"
20 #include "sanitizer_common/sanitizer_stacktrace.h"
21 #include "sanitizer_common/sanitizer_libc.h"
23 #if __has_feature(address_sanitizer) || defined(__SANITIZE_ADDRESS__)
24 # error "The AddressSanitizer run-time should not be"
25 " instrumented by AddressSanitizer"
28 // Build-time configuration options.
30 // If set, asan will intercept C++ exception api call(s).
31 #ifndef ASAN_HAS_EXCEPTIONS
32 # define ASAN_HAS_EXCEPTIONS 1
35 // If set, values like allocator chunk size, as well as defaults for some flags
36 // will be changed towards less memory overhead.
37 #ifndef ASAN_LOW_MEMORY
38 # if SANITIZER_IOS || SANITIZER_ANDROID || SANITIZER_RTEMS
39 # define ASAN_LOW_MEMORY 1
41 # define ASAN_LOW_MEMORY 0
47 # define ASAN_DYNAMIC 1
49 # define ASAN_DYNAMIC 0
53 // All internal functions in asan reside inside the __asan namespace
54 // to avoid namespace collisions with the user programs.
55 // Separate namespace also makes it simpler to distinguish the asan run-time
56 // functions from the instrumented user code in a profile.
60 using __sanitizer::StackTrace
;
62 void AsanInitFromRtl();
65 void InitializePlatformExceptionHandlers();
66 // Returns whether an address is a valid allocated system heap block.
67 // 'addr' must point to the beginning of the block.
68 bool IsSystemHeapAddress(uptr addr
);
71 void PrintAddressSpaceLayout();
72 void NORETURN
ShowStatsAndAbort();
74 // asan_shadow_setup.cpp
75 void InitializeShadowMemory();
77 // asan_malloc_linux.cpp / asan_malloc_mac.cpp
78 void ReplaceSystemMalloc();
80 // asan_linux.cpp / asan_mac.cpp / asan_rtems.cpp / asan_win.cpp
81 uptr
FindDynamicShadowStart();
82 void *AsanDoesNotSupportStaticLinkage();
83 void AsanCheckDynamicRTPrereqs();
84 void AsanCheckIncompatibleRT();
86 // Unpoisons platform-specific stacks.
87 // Returns true if all stacks have been unpoisoned.
88 bool PlatformUnpoisonStacks();
91 // Unpoison a region containing a stack.
92 // Performs a sanity check and warns if the bounds don't look right.
93 // The warning contains the type string to identify the stack type.
94 void UnpoisonStack(uptr bottom
, uptr top
, const char *type
);
97 AsanThread
*CreateMainThread();
99 // Support function for __asan_(un)register_image_globals. Searches for the
100 // loaded image containing `needle' and then enumerates all global metadata
101 // structures declared in that image, applying `op' (e.g.,
102 // __asan_(un)register_globals) to them.
103 typedef void (*globals_op_fptr
)(__asan_global
*, uptr
);
104 void AsanApplyToGlobals(globals_op_fptr op
, const void *needle
);
106 void AsanOnDeadlySignal(int, void *siginfo
, void *context
);
108 void ReadContextStack(void *context
, uptr
*stack
, uptr
*ssize
);
109 void StopInitOrderChecking();
111 // Wrapper for TLS/TSD.
112 void AsanTSDInit(void (*destructor
)(void *tsd
));
114 void AsanTSDSet(void *tsd
);
115 void PlatformTSDDtor(void *tsd
);
117 void AppendToErrorMessageBuffer(const char *buffer
);
119 void *AsanDlSymNext(const char *sym
);
121 // Returns `true` iff most of ASan init process should be skipped due to the
122 // ASan library being loaded via `dlopen()`. Platforms may perform any
123 // `dlopen()` specific initialization inside this function.
124 bool HandleDlopenInit();
126 // Add convenient macro for interface functions that may be represented as
128 #define ASAN_MALLOC_HOOK(ptr, size) \
130 if (&__sanitizer_malloc_hook) __sanitizer_malloc_hook(ptr, size); \
131 RunMallocHooks(ptr, size); \
133 #define ASAN_FREE_HOOK(ptr) \
135 if (&__sanitizer_free_hook) __sanitizer_free_hook(ptr); \
138 #define ASAN_ON_ERROR() \
139 if (&__asan_on_error) __asan_on_error()
141 extern int asan_inited
;
142 // Used to avoid infinite recursion in __asan_init().
143 extern bool asan_init_is_running
;
144 extern void (*death_callback
)(void);
145 // These magic values are written to shadow for better error reporting.
146 const int kAsanHeapLeftRedzoneMagic
= 0xfa;
147 const int kAsanHeapFreeMagic
= 0xfd;
148 const int kAsanStackLeftRedzoneMagic
= 0xf1;
149 const int kAsanStackMidRedzoneMagic
= 0xf2;
150 const int kAsanStackRightRedzoneMagic
= 0xf3;
151 const int kAsanStackAfterReturnMagic
= 0xf5;
152 const int kAsanInitializationOrderMagic
= 0xf6;
153 const int kAsanUserPoisonedMemoryMagic
= 0xf7;
154 const int kAsanContiguousContainerOOBMagic
= 0xfc;
155 const int kAsanStackUseAfterScopeMagic
= 0xf8;
156 const int kAsanGlobalRedzoneMagic
= 0xf9;
157 const int kAsanInternalHeapMagic
= 0xfe;
158 const int kAsanArrayCookieMagic
= 0xac;
159 const int kAsanIntraObjectRedzone
= 0xbb;
160 const int kAsanAllocaLeftMagic
= 0xca;
161 const int kAsanAllocaRightMagic
= 0xcb;
162 // Used to populate the shadow gap for systems without memory
163 // protection there (i.e. Myriad).
164 const int kAsanShadowGap
= 0xcc;
166 static const uptr kCurrentStackFrameMagic
= 0x41B58AB3;
167 static const uptr kRetiredStackFrameMagic
= 0x45E0360E;
169 } // namespace __asan
171 #endif // ASAN_INTERNAL_H