2011-11-30 09:07:02 +08:00
|
|
|
//===-- asan_internal.h -----------------------------------------*- C++ -*-===//
|
|
|
|
//
|
2019-01-19 16:50:56 +08:00
|
|
|
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
|
|
|
|
// See https://llvm.org/LICENSE.txt for license information.
|
|
|
|
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
2011-11-30 09:07:02 +08:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// This file is a part of AddressSanitizer, an address sanity checker.
|
|
|
|
//
|
|
|
|
// ASan-private header which defines various general utilities.
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
#ifndef ASAN_INTERNAL_H
|
|
|
|
#define ASAN_INTERNAL_H
|
|
|
|
|
2012-07-10 15:41:27 +08:00
|
|
|
#include "asan_flags.h"
|
2013-01-31 21:46:14 +08:00
|
|
|
#include "asan_interface_internal.h"
|
2012-06-06 15:02:44 +08:00
|
|
|
#include "sanitizer_common/sanitizer_common.h"
|
2012-06-05 22:25:27 +08:00
|
|
|
#include "sanitizer_common/sanitizer_internal_defs.h"
|
2012-08-28 22:11:57 +08:00
|
|
|
#include "sanitizer_common/sanitizer_stacktrace.h"
|
2012-05-31 21:42:53 +08:00
|
|
|
#include "sanitizer_common/sanitizer_libc.h"
|
2012-05-29 20:18:18 +08:00
|
|
|
|
[asan] make asan work with 7fff8000 offset and prelink
When prelink is installed in the system, prelink-ed
libraries map between 0x003000000000 and 0x004000000000 thus occupying the shadow Gap,
so we need so split the address space even further, like this:
|| [0x10007fff8000, 0x7fffffffffff] || HighMem ||
|| [0x02008fff7000, 0x10007fff7fff] || HighShadow ||
|| [0x004000000000, 0x02008fff6fff] || ShadowGap3 ||
|| [0x003000000000, 0x003fffffffff] || MidMem ||
|| [0x00087fff8000, 0x002fffffffff] || ShadowGap2 ||
|| [0x00067fff8000, 0x00087fff7fff] || MidShadow ||
|| [0x00008fff7000, 0x00067fff7fff] || ShadowGap ||
|| [0x00007fff8000, 0x00008fff6fff] || LowShadow ||
|| [0x000000000000, 0x00007fff7fff] || LowMem ||
Do it only if necessary.
Also added a bit of profiling code to make sure that the
mapping code is efficient.
Added a lit test to simulate prelink-ed libraries.
Unfortunately, this test does not work with binutils-gold linker.
If gold is the default linker the test silently passes.
Also replaced
__has_feature(address_sanitizer)
with
__has_feature(address_sanitizer) || defined(__SANITIZE_ADDRESS__)
in two places.
Patch partially by Jakub Jelinek.
llvm-svn: 175263
2013-02-15 20:00:24 +08:00
|
|
|
#if __has_feature(address_sanitizer) || defined(__SANITIZE_ADDRESS__)
|
2011-11-30 09:07:02 +08:00
|
|
|
# error "The AddressSanitizer run-time should not be"
|
|
|
|
" instrumented by AddressSanitizer"
|
|
|
|
#endif
|
|
|
|
|
2011-12-09 02:30:42 +08:00
|
|
|
// Build-time configuration options.
|
|
|
|
|
|
|
|
// If set, asan will intercept C++ exception api call(s).
|
|
|
|
#ifndef ASAN_HAS_EXCEPTIONS
|
|
|
|
# define ASAN_HAS_EXCEPTIONS 1
|
|
|
|
#endif
|
|
|
|
|
2012-02-27 21:07:29 +08:00
|
|
|
// If set, values like allocator chunk size, as well as defaults for some flags
|
|
|
|
// will be changed towards less memory overhead.
|
|
|
|
#ifndef ASAN_LOW_MEMORY
|
2018-05-18 08:43:54 +08:00
|
|
|
# if SANITIZER_IOS || SANITIZER_ANDROID || SANITIZER_RTEMS
|
2012-09-28 18:07:53 +08:00
|
|
|
# define ASAN_LOW_MEMORY 1
|
2016-02-02 10:01:17 +08:00
|
|
|
# else
|
2012-09-28 18:07:53 +08:00
|
|
|
# define ASAN_LOW_MEMORY 0
|
|
|
|
# endif
|
2012-02-27 21:07:29 +08:00
|
|
|
#endif
|
2013-02-20 22:28:08 +08:00
|
|
|
|
2014-04-01 21:16:30 +08:00
|
|
|
#ifndef ASAN_DYNAMIC
|
2014-05-12 17:45:39 +08:00
|
|
|
# ifdef PIC
|
|
|
|
# define ASAN_DYNAMIC 1
|
|
|
|
# else
|
|
|
|
# define ASAN_DYNAMIC 0
|
|
|
|
# endif
|
2014-04-01 21:16:30 +08:00
|
|
|
#endif
|
|
|
|
|
2011-11-30 09:07:02 +08:00
|
|
|
// All internal functions in asan reside inside the __asan namespace
|
|
|
|
// to avoid namespace collisions with the user programs.
|
2014-05-15 10:22:34 +08:00
|
|
|
// Separate namespace also makes it simpler to distinguish the asan run-time
|
2011-11-30 09:07:02 +08:00
|
|
|
// functions from the instrumented user code in a profile.
|
|
|
|
namespace __asan {
|
|
|
|
|
|
|
|
class AsanThread;
|
2012-08-28 22:11:57 +08:00
|
|
|
using __sanitizer::StackTrace;
|
2011-11-30 09:07:02 +08:00
|
|
|
|
2014-01-16 20:31:50 +08:00
|
|
|
void AsanInitFromRtl();
|
|
|
|
|
2016-07-12 05:40:59 +08:00
|
|
|
// asan_win.cc
|
|
|
|
void InitializePlatformExceptionHandlers();
|
2017-02-22 00:09:38 +08:00
|
|
|
// Returns whether an address is a valid allocated system heap block.
|
|
|
|
// 'addr' must point to the beginning of the block.
|
|
|
|
bool IsSystemHeapAddress(uptr addr);
|
2016-07-12 05:40:59 +08:00
|
|
|
|
2011-12-01 02:50:23 +08:00
|
|
|
// asan_rtl.cc
|
2017-07-31 14:48:34 +08:00
|
|
|
void PrintAddressSpaceLayout();
|
2012-03-14 00:29:25 +08:00
|
|
|
void NORETURN ShowStatsAndAbort();
|
2011-11-30 09:07:02 +08:00
|
|
|
|
2017-07-31 14:48:34 +08:00
|
|
|
// asan_shadow_setup.cc
|
|
|
|
void InitializeShadowMemory();
|
|
|
|
|
2011-12-01 02:50:23 +08:00
|
|
|
// asan_malloc_linux.cc / asan_malloc_mac.cc
|
2011-11-30 09:07:02 +08:00
|
|
|
void ReplaceSystemMalloc();
|
|
|
|
|
2018-05-18 08:43:54 +08:00
|
|
|
// asan_linux.cc / asan_mac.cc / asan_rtems.cc / asan_win.cc
|
2017-07-13 07:29:21 +08:00
|
|
|
uptr FindDynamicShadowStart();
|
2011-11-30 09:07:02 +08:00
|
|
|
void *AsanDoesNotSupportStaticLinkage();
|
2014-04-01 21:16:30 +08:00
|
|
|
void AsanCheckDynamicRTPrereqs();
|
|
|
|
void AsanCheckIncompatibleRT();
|
2011-12-29 06:58:01 +08:00
|
|
|
|
2017-08-09 08:38:57 +08:00
|
|
|
// asan_thread.cc
|
|
|
|
AsanThread *CreateMainThread();
|
|
|
|
|
2016-03-29 04:28:17 +08:00
|
|
|
// Support function for __asan_(un)register_image_globals. Searches for the
|
|
|
|
// loaded image containing `needle' and then enumerates all global metadata
|
|
|
|
// structures declared in that image, applying `op' (e.g.,
|
|
|
|
// __asan_(un)register_globals) to them.
|
|
|
|
typedef void (*globals_op_fptr)(__asan_global *, uptr);
|
|
|
|
void AsanApplyToGlobals(globals_op_fptr op, const void *needle);
|
|
|
|
|
2015-08-07 01:52:54 +08:00
|
|
|
void AsanOnDeadlySignal(int, void *siginfo, void *context);
|
2012-01-06 10:12:25 +08:00
|
|
|
|
2013-01-17 23:45:28 +08:00
|
|
|
void ReadContextStack(void *context, uptr *stack, uptr *ssize);
|
2013-05-24 19:46:56 +08:00
|
|
|
void StopInitOrderChecking();
|
2012-01-11 10:21:06 +08:00
|
|
|
|
|
|
|
// Wrapper for TLS/TSD.
|
2012-02-07 08:27:15 +08:00
|
|
|
void AsanTSDInit(void (*destructor)(void *tsd));
|
2012-01-11 10:21:06 +08:00
|
|
|
void *AsanTSDGet();
|
|
|
|
void AsanTSDSet(void *tsd);
|
2013-10-14 20:01:05 +08:00
|
|
|
void PlatformTSDDtor(void *tsd);
|
2012-01-10 02:53:15 +08:00
|
|
|
|
2012-06-06 21:11:29 +08:00
|
|
|
void AppendToErrorMessageBuffer(const char *buffer);
|
2011-11-30 09:07:02 +08:00
|
|
|
|
2014-06-06 18:57:21 +08:00
|
|
|
void *AsanDlSymNext(const char *sym);
|
|
|
|
|
2015-05-30 06:31:28 +08:00
|
|
|
void ReserveShadowMemoryRange(uptr beg, uptr end, const char *name);
|
2015-01-21 10:05:31 +08:00
|
|
|
|
2018-12-01 23:45:42 +08:00
|
|
|
// Returns `true` iff most of ASan init process should be skipped due to the
|
|
|
|
// ASan library being loaded via `dlopen()`. Platforms may perform any
|
|
|
|
// `dlopen()` specific initialization inside this function.
|
|
|
|
bool HandleDlopenInit();
|
|
|
|
|
2012-12-08 06:01:28 +08:00
|
|
|
// Add convenient macro for interface functions that may be represented as
|
|
|
|
// weak hooks.
|
2016-06-17 04:06:06 +08:00
|
|
|
#define ASAN_MALLOC_HOOK(ptr, size) \
|
|
|
|
do { \
|
|
|
|
if (&__sanitizer_malloc_hook) __sanitizer_malloc_hook(ptr, size); \
|
|
|
|
RunMallocHooks(ptr, size); \
|
|
|
|
} while (false)
|
|
|
|
#define ASAN_FREE_HOOK(ptr) \
|
|
|
|
do { \
|
|
|
|
if (&__sanitizer_free_hook) __sanitizer_free_hook(ptr); \
|
|
|
|
RunFreeHooks(ptr); \
|
|
|
|
} while (false)
|
2012-12-08 06:01:28 +08:00
|
|
|
#define ASAN_ON_ERROR() \
|
|
|
|
if (&__asan_on_error) __asan_on_error()
|
|
|
|
|
2011-11-30 09:07:02 +08:00
|
|
|
extern int asan_inited;
|
|
|
|
// Used to avoid infinite recursion in __asan_init().
|
|
|
|
extern bool asan_init_is_running;
|
2012-06-06 15:02:44 +08:00
|
|
|
extern void (*death_callback)(void);
|
2011-11-30 09:07:02 +08:00
|
|
|
// These magic values are written to shadow for better error reporting.
|
|
|
|
const int kAsanHeapLeftRedzoneMagic = 0xfa;
|
|
|
|
const int kAsanHeapFreeMagic = 0xfd;
|
|
|
|
const int kAsanStackLeftRedzoneMagic = 0xf1;
|
|
|
|
const int kAsanStackMidRedzoneMagic = 0xf2;
|
|
|
|
const int kAsanStackRightRedzoneMagic = 0xf3;
|
|
|
|
const int kAsanStackAfterReturnMagic = 0xf5;
|
2012-08-21 22:10:25 +08:00
|
|
|
const int kAsanInitializationOrderMagic = 0xf6;
|
2011-11-30 09:07:02 +08:00
|
|
|
const int kAsanUserPoisonedMemoryMagic = 0xf7;
|
2013-11-19 16:40:07 +08:00
|
|
|
const int kAsanContiguousContainerOOBMagic = 0xfc;
|
2012-12-04 09:38:15 +08:00
|
|
|
const int kAsanStackUseAfterScopeMagic = 0xf8;
|
2011-11-30 09:07:02 +08:00
|
|
|
const int kAsanGlobalRedzoneMagic = 0xf9;
|
2011-12-16 01:41:30 +08:00
|
|
|
const int kAsanInternalHeapMagic = 0xfe;
|
2014-08-04 20:43:13 +08:00
|
|
|
const int kAsanArrayCookieMagic = 0xac;
|
2014-10-17 09:22:37 +08:00
|
|
|
const int kAsanIntraObjectRedzone = 0xbb;
|
2014-11-21 18:32:05 +08:00
|
|
|
const int kAsanAllocaLeftMagic = 0xca;
|
|
|
|
const int kAsanAllocaRightMagic = 0xcb;
|
2018-05-17 07:36:01 +08:00
|
|
|
// Used to populate the shadow gap for systems without memory
|
|
|
|
// protection there (i.e. Myriad).
|
|
|
|
const int kAsanShadowGap = 0xcc;
|
2011-11-30 09:07:02 +08:00
|
|
|
|
2012-05-31 22:35:53 +08:00
|
|
|
static const uptr kCurrentStackFrameMagic = 0x41B58AB3;
|
|
|
|
static const uptr kRetiredStackFrameMagic = 0x45E0360E;
|
2011-11-30 09:07:02 +08:00
|
|
|
|
|
|
|
} // namespace __asan
|
|
|
|
|
|
|
|
#endif // ASAN_INTERNAL_H
|