Path: blob/main/contrib/llvm-project/compiler-rt/include/sanitizer/asan_interface.h
35235 views
//===-- sanitizer/asan_interface.h ------------------------------*- C++ -*-===//1//2// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.3// See https://llvm.org/LICENSE.txt for license information.4// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception5//6//===----------------------------------------------------------------------===//7//8// This file is a part of AddressSanitizer (ASan).9//10// Public interface header.11//===----------------------------------------------------------------------===//12#ifndef SANITIZER_ASAN_INTERFACE_H13#define SANITIZER_ASAN_INTERFACE_H1415#include <sanitizer/common_interface_defs.h>1617#ifdef __cplusplus18extern "C" {19#endif20/// Marks a memory region (<c>[addr, addr+size)</c>) as unaddressable.21///22/// This memory must be previously allocated by your program. Instrumented23/// code is forbidden from accessing addresses in this region until it is24/// unpoisoned. This function is not guaranteed to poison the entire region -25/// it could poison only a subregion of <c>[addr, addr+size)</c> due to ASan26/// alignment restrictions.27///28/// \note This function is not thread-safe because no two threads can poison or29/// unpoison memory in the same memory region simultaneously.30///31/// \param addr Start of memory region.32/// \param size Size of memory region.33void SANITIZER_CDECL __asan_poison_memory_region(void const volatile *addr,34size_t size);3536/// Marks a memory region (<c>[addr, addr+size)</c>) as addressable.37///38/// This memory must be previously allocated by your program. Accessing39/// addresses in this region is allowed until this region is poisoned again.40/// This function could unpoison a super-region of <c>[addr, addr+size)</c> due41/// to ASan alignment restrictions.42///43/// \note This function is not thread-safe because no two threads can44/// poison or unpoison memory in the same memory region simultaneously.45///46/// \param addr Start of memory region.47/// \param size Size of memory region.48void SANITIZER_CDECL __asan_unpoison_memory_region(void const volatile *addr,49size_t size);5051// Macros provided for convenience.52#ifdef __has_feature53#if __has_feature(address_sanitizer)54#define ASAN_DEFINE_REGION_MACROS55#endif56#elif defined(__SANITIZE_ADDRESS__)57#define ASAN_DEFINE_REGION_MACROS58#endif5960#ifdef ASAN_DEFINE_REGION_MACROS61/// Marks a memory region as unaddressable.62///63/// \note Macro provided for convenience; defined as a no-op if ASan is not64/// enabled.65///66/// \param addr Start of memory region.67/// \param size Size of memory region.68#define ASAN_POISON_MEMORY_REGION(addr, size) \69__asan_poison_memory_region((addr), (size))7071/// Marks a memory region as addressable.72///73/// \note Macro provided for convenience; defined as a no-op if ASan is not74/// enabled.75///76/// \param addr Start of memory region.77/// \param size Size of memory region.78#define ASAN_UNPOISON_MEMORY_REGION(addr, size) \79__asan_unpoison_memory_region((addr), (size))80#else81#define ASAN_POISON_MEMORY_REGION(addr, size) ((void)(addr), (void)(size))82#define ASAN_UNPOISON_MEMORY_REGION(addr, size) ((void)(addr), (void)(size))83#endif84#undef ASAN_DEFINE_REGION_MACROS8586/// Checks if an address is poisoned.87///88/// Returns 1 if <c><i>addr</i></c> is poisoned (that is, 1-byte read/write89/// access to this address would result in an error report from ASan).90/// Otherwise returns 0.91///92/// \param addr Address to check.93///94/// \retval 1 Address is poisoned.95/// \retval 0 Address is not poisoned.96int SANITIZER_CDECL __asan_address_is_poisoned(void const volatile *addr);9798/// Checks if a region is poisoned.99///100/// If at least one byte in <c>[beg, beg+size)</c> is poisoned, returns the101/// address of the first such byte. Otherwise returns 0.102///103/// \param beg Start of memory region.104/// \param size Start of memory region.105/// \returns Address of first poisoned byte.106void *SANITIZER_CDECL __asan_region_is_poisoned(void *beg, size_t size);107108/// Describes an address (useful for calling from the debugger).109///110/// Prints the description of <c><i>addr</i></c>.111///112/// \param addr Address to describe.113void SANITIZER_CDECL __asan_describe_address(void *addr);114115/// Checks if an error has been or is being reported (useful for calling from116/// the debugger to get information about an ASan error).117///118/// Returns 1 if an error has been (or is being) reported. Otherwise returns 0.119///120/// \returns 1 if an error has been (or is being) reported. Otherwise returns121/// 0.122int SANITIZER_CDECL __asan_report_present(void);123124/// Gets the PC (program counter) register value of an ASan error (useful for125/// calling from the debugger).126///127/// Returns PC if an error has been (or is being) reported.128/// Otherwise returns 0.129///130/// \returns PC value.131void *SANITIZER_CDECL __asan_get_report_pc(void);132133/// Gets the BP (base pointer) register value of an ASan error (useful for134/// calling from the debugger).135///136/// Returns BP if an error has been (or is being) reported.137/// Otherwise returns 0.138///139/// \returns BP value.140void *SANITIZER_CDECL __asan_get_report_bp(void);141142/// Gets the SP (stack pointer) register value of an ASan error (useful for143/// calling from the debugger).144///145/// If an error has been (or is being) reported, returns SP.146/// Otherwise returns 0.147///148/// \returns SP value.149void *SANITIZER_CDECL __asan_get_report_sp(void);150151/// Gets the address of the report buffer of an ASan error (useful for calling152/// from the debugger).153///154/// Returns the address of the report buffer if an error has been (or is being)155/// reported. Otherwise returns 0.156///157/// \returns Address of report buffer.158void *SANITIZER_CDECL __asan_get_report_address(void);159160/// Gets access type of an ASan error (useful for calling from the debugger).161///162/// Returns access type (read or write) if an error has been (or is being)163/// reported. Otherwise returns 0.164///165/// \returns Access type (0 = read, 1 = write).166int SANITIZER_CDECL __asan_get_report_access_type(void);167168/// Gets access size of an ASan error (useful for calling from the debugger).169///170/// Returns access size if an error has been (or is being) reported. Otherwise171/// returns 0.172///173/// \returns Access size in bytes.174size_t SANITIZER_CDECL __asan_get_report_access_size(void);175176/// Gets the bug description of an ASan error (useful for calling from a177/// debugger).178///179/// \returns Returns a bug description if an error has been (or is being)180/// reported - for example, "heap-use-after-free". Otherwise returns an empty181/// string.182const char *SANITIZER_CDECL __asan_get_report_description(void);183184/// Gets information about a pointer (useful for calling from the debugger).185///186/// Returns the category of the given pointer as a constant string.187/// Possible return values are <c>global</c>, <c>stack</c>, <c>stack-fake</c>,188/// <c>heap</c>, <c>heap-invalid</c>, <c>shadow-low</c>, <c>shadow-gap</c>,189/// <c>shadow-high</c>, and <c>unknown</c>.190///191/// If the return value is <c>global</c> or <c>stack</c>, tries to also return192/// the variable name, address, and size. If the return value is <c>heap</c>,193/// tries to return the chunk address and size. <c><i>name</i></c> should point194/// to an allocated buffer of size <c><i>name_size</i></c>.195///196/// \param addr Address to locate.197/// \param name Buffer to store the variable's name.198/// \param name_size Size in bytes of the variable's name buffer.199/// \param[out] region_address Address of the region.200/// \param[out] region_size Size of the region in bytes.201///202/// \returns Returns the category of the given pointer as a constant string.203const char *SANITIZER_CDECL __asan_locate_address(void *addr, char *name,204size_t name_size,205void **region_address,206size_t *region_size);207208/// Gets the allocation stack trace and thread ID for a heap address (useful209/// for calling from the debugger).210///211/// Stores up to <c><i>size</i></c> frames in <c><i>trace</i></c>. Returns212/// the number of stored frames or 0 on error.213///214/// \param addr A heap address.215/// \param trace A buffer to store the stack trace.216/// \param size Size in bytes of the trace buffer.217/// \param[out] thread_id The thread ID of the address.218///219/// \returns Returns the number of stored frames or 0 on error.220size_t SANITIZER_CDECL __asan_get_alloc_stack(void *addr, void **trace,221size_t size, int *thread_id);222223/// Gets the free stack trace and thread ID for a heap address (useful for224/// calling from the debugger).225///226/// Stores up to <c><i>size</i></c> frames in <c><i>trace</i></c>. Returns227/// the number of stored frames or 0 on error.228///229/// \param addr A heap address.230/// \param trace A buffer to store the stack trace.231/// \param size Size in bytes of the trace buffer.232/// \param[out] thread_id The thread ID of the address.233///234/// \returns Returns the number of stored frames or 0 on error.235size_t SANITIZER_CDECL __asan_get_free_stack(void *addr, void **trace,236size_t size, int *thread_id);237238/// Gets the current shadow memory mapping (useful for calling from the239/// debugger).240///241/// \param[out] shadow_scale Shadow scale value.242/// \param[out] shadow_offset Offset value.243void SANITIZER_CDECL __asan_get_shadow_mapping(size_t *shadow_scale,244size_t *shadow_offset);245246/// This is an internal function that is called to report an error. However,247/// it is still a part of the interface because you might want to set a248/// breakpoint on this function in the debugger.249///250/// \param pc <c><i>pc</i></c> value of the ASan error.251/// \param bp <c><i>bp</i></c> value of the ASan error.252/// \param sp <c><i>sp</i></c> value of the ASan error.253/// \param addr Address of the ASan error.254/// \param is_write True if the error is a write error; false otherwise.255/// \param access_size Size of the memory access of the ASan error.256void SANITIZER_CDECL __asan_report_error(void *pc, void *bp, void *sp,257void *addr, int is_write,258size_t access_size);259260// Deprecated. Call __sanitizer_set_death_callback instead.261void SANITIZER_CDECL __asan_set_death_callback(void (*callback)(void));262263/// Sets the callback function to be called during ASan error reporting.264///265/// The callback provides a string pointer to the report.266///267/// \param callback User-provided function.268void SANITIZER_CDECL269__asan_set_error_report_callback(void (*callback)(const char *));270271/// User-provided callback on ASan errors.272///273/// You can provide a function that would be called immediately when ASan274/// detects an error. This is useful in cases when ASan detects an error but275/// your program crashes before the ASan report is printed.276void SANITIZER_CDECL __asan_on_error(void);277278/// Prints accumulated statistics to <c>stderr</c> (useful for calling from the279/// debugger).280void SANITIZER_CDECL __asan_print_accumulated_stats(void);281282/// User-provided default option settings.283///284/// You can provide your own implementation of this function to return a string285/// containing ASan runtime options (for example,286/// <c>verbosity=1:halt_on_error=0</c>).287///288/// \returns Default options string.289const char *SANITIZER_CDECL __asan_default_options(void);290291// The following two functions facilitate garbage collection in presence of292// ASan's fake stack.293294/// Gets an opaque handler to the current thread's fake stack.295///296/// Returns an opaque handler to be used by297/// <c>__asan_addr_is_in_fake_stack()</c>. Returns NULL if the current thread298/// does not have a fake stack.299///300/// \returns An opaque handler to the fake stack or NULL.301void *SANITIZER_CDECL __asan_get_current_fake_stack(void);302303/// Checks if an address belongs to a given fake stack.304///305/// If <c><i>fake_stack</i></c> is non-NULL and <c><i>addr</i></c> belongs to a306/// fake frame in <c><i>fake_stack</i></c>, returns the address of the real307/// stack that corresponds to the fake frame and sets <c><i>beg</i></c> and308/// <c><i>end</i></c> to the boundaries of this fake frame. Otherwise returns309/// NULL and does not touch <c><i>beg</i></c> and <c><i>end</i></c>.310///311/// If <c><i>beg</i></c> or <c><i>end</i></c> are NULL, they are not touched.312///313/// \note This function can be called from a thread other than the owner of314/// <c><i>fake_stack</i></c>, but the owner thread needs to be alive.315///316/// \param fake_stack An opaque handler to a fake stack.317/// \param addr Address to test.318/// \param[out] beg Beginning of fake frame.319/// \param[out] end End of fake frame.320/// \returns Stack address or NULL.321void *SANITIZER_CDECL __asan_addr_is_in_fake_stack(void *fake_stack, void *addr,322void **beg, void **end);323324/// Performs shadow memory cleanup of the current thread's stack before a325/// function marked with the <c>[[noreturn]]</c> attribute is called.326///327/// To avoid false positives on the stack, must be called before no-return328/// functions like <c>_exit()</c> and <c>execl()</c>.329void SANITIZER_CDECL __asan_handle_no_return(void);330331/// Update allocation stack trace for the given allocation to the current stack332/// trace. Returns 1 if successful, 0 if not.333int SANITIZER_CDECL __asan_update_allocation_context(void *addr);334335#ifdef __cplusplus336} // extern "C"337#endif338339#endif // SANITIZER_ASAN_INTERFACE_H340341342