1*0b57cec5SDimitry Andric //===-- sanitizer/asan_interface.h ------------------------------*- C++ -*-===// 2*0b57cec5SDimitry Andric // 3*0b57cec5SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4*0b57cec5SDimitry Andric // See https://llvm.org/LICENSE.txt for license information. 5*0b57cec5SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6*0b57cec5SDimitry Andric // 7*0b57cec5SDimitry Andric //===----------------------------------------------------------------------===// 8*0b57cec5SDimitry Andric // 9*0b57cec5SDimitry Andric // This file is a part of AddressSanitizer (ASan). 10*0b57cec5SDimitry Andric // 11*0b57cec5SDimitry Andric // Public interface header. 12*0b57cec5SDimitry Andric //===----------------------------------------------------------------------===// 13*0b57cec5SDimitry Andric #ifndef SANITIZER_ASAN_INTERFACE_H 14*0b57cec5SDimitry Andric #define SANITIZER_ASAN_INTERFACE_H 15*0b57cec5SDimitry Andric 16*0b57cec5SDimitry Andric #include <sanitizer/common_interface_defs.h> 17*0b57cec5SDimitry Andric 18*0b57cec5SDimitry Andric #ifdef __cplusplus 19*0b57cec5SDimitry Andric extern "C" { 20*0b57cec5SDimitry Andric #endif 21*0b57cec5SDimitry Andric /// Marks a memory region (<c>[addr, addr+size)</c>) as unaddressable. 22*0b57cec5SDimitry Andric /// 23*0b57cec5SDimitry Andric /// This memory must be previously allocated by your program. Instrumented 24*0b57cec5SDimitry Andric /// code is forbidden from accessing addresses in this region until it is 25*0b57cec5SDimitry Andric /// unpoisoned. This function is not guaranteed to poison the entire region - 26*0b57cec5SDimitry Andric /// it could poison only a subregion of <c>[addr, addr+size)</c> due to ASan 27*0b57cec5SDimitry Andric /// alignment restrictions. 28*0b57cec5SDimitry Andric /// 29*0b57cec5SDimitry Andric /// \note This function is not thread-safe because no two threads can poison or 30*0b57cec5SDimitry Andric /// unpoison memory in the same memory region simultaneously. 31*0b57cec5SDimitry Andric /// 32*0b57cec5SDimitry Andric /// \param addr Start of memory region. 33*0b57cec5SDimitry Andric /// \param size Size of memory region. 34*0b57cec5SDimitry Andric void __asan_poison_memory_region(void const volatile *addr, size_t size); 35*0b57cec5SDimitry Andric 36*0b57cec5SDimitry Andric /// Marks a memory region (<c>[addr, addr+size)</c>) as addressable. 37*0b57cec5SDimitry Andric /// 38*0b57cec5SDimitry Andric /// This memory must be previously allocated by your program. Accessing 39*0b57cec5SDimitry Andric /// addresses in this region is allowed until this region is poisoned again. 40*0b57cec5SDimitry Andric /// This function could unpoison a super-region of <c>[addr, addr+size)</c> due 41*0b57cec5SDimitry Andric /// to ASan alignment restrictions. 42*0b57cec5SDimitry Andric /// 43*0b57cec5SDimitry Andric /// \note This function is not thread-safe because no two threads can 44*0b57cec5SDimitry Andric /// poison or unpoison memory in the same memory region simultaneously. 45*0b57cec5SDimitry Andric /// 46*0b57cec5SDimitry Andric /// \param addr Start of memory region. 47*0b57cec5SDimitry Andric /// \param size Size of memory region. 48*0b57cec5SDimitry Andric void __asan_unpoison_memory_region(void const volatile *addr, size_t size); 49*0b57cec5SDimitry Andric 50*0b57cec5SDimitry Andric // Macros provided for convenience. 51*0b57cec5SDimitry Andric #if __has_feature(address_sanitizer) || defined(__SANITIZE_ADDRESS__) 52*0b57cec5SDimitry Andric /// Marks a memory region as unaddressable. 53*0b57cec5SDimitry Andric /// 54*0b57cec5SDimitry Andric /// \note Macro provided for convenience; defined as a no-op if ASan is not 55*0b57cec5SDimitry Andric /// enabled. 56*0b57cec5SDimitry Andric /// 57*0b57cec5SDimitry Andric /// \param addr Start of memory region. 58*0b57cec5SDimitry Andric /// \param size Size of memory region. 59*0b57cec5SDimitry Andric #define ASAN_POISON_MEMORY_REGION(addr, size) \ 60*0b57cec5SDimitry Andric __asan_poison_memory_region((addr), (size)) 61*0b57cec5SDimitry Andric 62*0b57cec5SDimitry Andric /// Marks a memory region as addressable. 63*0b57cec5SDimitry Andric /// 64*0b57cec5SDimitry Andric /// \note Macro provided for convenience; defined as a no-op if ASan is not 65*0b57cec5SDimitry Andric /// enabled. 66*0b57cec5SDimitry Andric /// 67*0b57cec5SDimitry Andric /// \param addr Start of memory region. 68*0b57cec5SDimitry Andric /// \param size Size of memory region. 69*0b57cec5SDimitry Andric #define ASAN_UNPOISON_MEMORY_REGION(addr, size) \ 70*0b57cec5SDimitry Andric __asan_unpoison_memory_region((addr), (size)) 71*0b57cec5SDimitry Andric #else 72*0b57cec5SDimitry Andric #define ASAN_POISON_MEMORY_REGION(addr, size) \ 73*0b57cec5SDimitry Andric ((void)(addr), (void)(size)) 74*0b57cec5SDimitry Andric #define ASAN_UNPOISON_MEMORY_REGION(addr, size) \ 75*0b57cec5SDimitry Andric ((void)(addr), (void)(size)) 76*0b57cec5SDimitry Andric #endif 77*0b57cec5SDimitry Andric 78*0b57cec5SDimitry Andric /// Checks if an address is poisoned. 79*0b57cec5SDimitry Andric /// 80*0b57cec5SDimitry Andric /// Returns 1 if <c><i>addr</i></c> is poisoned (that is, 1-byte read/write 81*0b57cec5SDimitry Andric /// access to this address would result in an error report from ASan). 82*0b57cec5SDimitry Andric /// Otherwise returns 0. 83*0b57cec5SDimitry Andric /// 84*0b57cec5SDimitry Andric /// \param addr Address to check. 85*0b57cec5SDimitry Andric /// 86*0b57cec5SDimitry Andric /// \retval 1 Address is poisoned. 87*0b57cec5SDimitry Andric /// \retval 0 Address is not poisoned. 88*0b57cec5SDimitry Andric int __asan_address_is_poisoned(void const volatile *addr); 89*0b57cec5SDimitry Andric 90*0b57cec5SDimitry Andric /// Checks if a region is poisoned. 91*0b57cec5SDimitry Andric /// 92*0b57cec5SDimitry Andric /// If at least one byte in <c>[beg, beg+size)</c> is poisoned, returns the 93*0b57cec5SDimitry Andric /// address of the first such byte. Otherwise returns 0. 94*0b57cec5SDimitry Andric /// 95*0b57cec5SDimitry Andric /// \param beg Start of memory region. 96*0b57cec5SDimitry Andric /// \param size Start of memory region. 97*0b57cec5SDimitry Andric /// \returns Address of first poisoned byte. 98*0b57cec5SDimitry Andric void *__asan_region_is_poisoned(void *beg, size_t size); 99*0b57cec5SDimitry Andric 100*0b57cec5SDimitry Andric /// Describes an address (useful for calling from the debugger). 101*0b57cec5SDimitry Andric /// 102*0b57cec5SDimitry Andric /// Prints the description of <c><i>addr</i></c>. 103*0b57cec5SDimitry Andric /// 104*0b57cec5SDimitry Andric /// \param addr Address to describe. 105*0b57cec5SDimitry Andric void __asan_describe_address(void *addr); 106*0b57cec5SDimitry Andric 107*0b57cec5SDimitry Andric /// Checks if an error has been or is being reported (useful for calling from 108*0b57cec5SDimitry Andric /// the debugger to get information about an ASan error). 109*0b57cec5SDimitry Andric /// 110*0b57cec5SDimitry Andric /// Returns 1 if an error has been (or is being) reported. Otherwise returns 0. 111*0b57cec5SDimitry Andric /// 112*0b57cec5SDimitry Andric /// \returns 1 if an error has been (or is being) reported. Otherwise returns 113*0b57cec5SDimitry Andric /// 0. 114*0b57cec5SDimitry Andric int __asan_report_present(void); 115*0b57cec5SDimitry Andric 116*0b57cec5SDimitry Andric /// Gets the PC (program counter) register value of an ASan error (useful for 117*0b57cec5SDimitry Andric /// calling from the debugger). 118*0b57cec5SDimitry Andric /// 119*0b57cec5SDimitry Andric /// Returns PC if an error has been (or is being) reported. 120*0b57cec5SDimitry Andric /// Otherwise returns 0. 121*0b57cec5SDimitry Andric /// 122*0b57cec5SDimitry Andric /// \returns PC value. 123*0b57cec5SDimitry Andric void *__asan_get_report_pc(void); 124*0b57cec5SDimitry Andric 125*0b57cec5SDimitry Andric /// Gets the BP (base pointer) register value of an ASan error (useful for 126*0b57cec5SDimitry Andric /// calling from the debugger). 127*0b57cec5SDimitry Andric /// 128*0b57cec5SDimitry Andric /// Returns BP if an error has been (or is being) reported. 129*0b57cec5SDimitry Andric /// Otherwise returns 0. 130*0b57cec5SDimitry Andric /// 131*0b57cec5SDimitry Andric /// \returns BP value. 132*0b57cec5SDimitry Andric void *__asan_get_report_bp(void); 133*0b57cec5SDimitry Andric 134*0b57cec5SDimitry Andric /// Gets the SP (stack pointer) register value of an ASan error (useful for 135*0b57cec5SDimitry Andric /// calling from the debugger). 136*0b57cec5SDimitry Andric /// 137*0b57cec5SDimitry Andric /// If an error has been (or is being) reported, returns SP. 138*0b57cec5SDimitry Andric /// Otherwise returns 0. 139*0b57cec5SDimitry Andric /// 140*0b57cec5SDimitry Andric /// \returns SP value. 141*0b57cec5SDimitry Andric void *__asan_get_report_sp(void); 142*0b57cec5SDimitry Andric 143*0b57cec5SDimitry Andric /// Gets the address of the report buffer of an ASan error (useful for calling 144*0b57cec5SDimitry Andric /// from the debugger). 145*0b57cec5SDimitry Andric /// 146*0b57cec5SDimitry Andric /// Returns the address of the report buffer if an error has been (or is being) 147*0b57cec5SDimitry Andric /// reported. Otherwise returns 0. 148*0b57cec5SDimitry Andric /// 149*0b57cec5SDimitry Andric /// \returns Address of report buffer. 150*0b57cec5SDimitry Andric void *__asan_get_report_address(void); 151*0b57cec5SDimitry Andric 152*0b57cec5SDimitry Andric /// Gets access type of an ASan error (useful for calling from the debugger). 153*0b57cec5SDimitry Andric /// 154*0b57cec5SDimitry Andric /// Returns access type (read or write) if an error has been (or is being) 155*0b57cec5SDimitry Andric /// reported. Otherwise returns 0. 156*0b57cec5SDimitry Andric /// 157*0b57cec5SDimitry Andric /// \returns Access type (0 = read, 1 = write). 158*0b57cec5SDimitry Andric int __asan_get_report_access_type(void); 159*0b57cec5SDimitry Andric 160*0b57cec5SDimitry Andric /// Gets access size of an ASan error (useful for calling from the debugger). 161*0b57cec5SDimitry Andric /// 162*0b57cec5SDimitry Andric /// Returns access size if an error has been (or is being) reported. Otherwise 163*0b57cec5SDimitry Andric /// returns 0. 164*0b57cec5SDimitry Andric /// 165*0b57cec5SDimitry Andric /// \returns Access size in bytes. 166*0b57cec5SDimitry Andric size_t __asan_get_report_access_size(void); 167*0b57cec5SDimitry Andric 168*0b57cec5SDimitry Andric /// Gets the bug description of an ASan error (useful for calling from a 169*0b57cec5SDimitry Andric /// debugger). 170*0b57cec5SDimitry Andric /// 171*0b57cec5SDimitry Andric /// \returns Returns a bug description if an error has been (or is being) 172*0b57cec5SDimitry Andric /// reported - for example, "heap-use-after-free". Otherwise returns an empty 173*0b57cec5SDimitry Andric /// string. 174*0b57cec5SDimitry Andric const char *__asan_get_report_description(void); 175*0b57cec5SDimitry Andric 176*0b57cec5SDimitry Andric /// Gets information about a pointer (useful for calling from the debugger). 177*0b57cec5SDimitry Andric /// 178*0b57cec5SDimitry Andric /// Returns the category of the given pointer as a constant string. 179*0b57cec5SDimitry Andric /// Possible return values are <c>global</c>, <c>stack</c>, <c>stack-fake</c>, 180*0b57cec5SDimitry Andric /// <c>heap</c>, <c>heap-invalid</c>, <c>shadow-low</c>, <c>shadow-gap</c>, 181*0b57cec5SDimitry Andric /// <c>shadow-high</c>, and <c>unknown</c>. 182*0b57cec5SDimitry Andric /// 183*0b57cec5SDimitry Andric /// If the return value is <c>global</c> or <c>stack</c>, tries to also return 184*0b57cec5SDimitry Andric /// the variable name, address, and size. If the return value is <c>heap</c>, 185*0b57cec5SDimitry Andric /// tries to return the chunk address and size. <c><i>name</i></c> should point 186*0b57cec5SDimitry Andric /// to an allocated buffer of size <c><i>name_size</i></c>. 187*0b57cec5SDimitry Andric /// 188*0b57cec5SDimitry Andric /// \param addr Address to locate. 189*0b57cec5SDimitry Andric /// \param name Buffer to store the variable's name. 190*0b57cec5SDimitry Andric /// \param name_size Size in bytes of the variable's name buffer. 191*0b57cec5SDimitry Andric /// \param region_address [out] Address of the region. 192*0b57cec5SDimitry Andric /// \param region_size [out] Size of the region in bytes. 193*0b57cec5SDimitry Andric /// 194*0b57cec5SDimitry Andric /// \returns Returns the category of the given pointer as a constant string. 195*0b57cec5SDimitry Andric const char *__asan_locate_address(void *addr, char *name, size_t name_size, 196*0b57cec5SDimitry Andric void **region_address, size_t *region_size); 197*0b57cec5SDimitry Andric 198*0b57cec5SDimitry Andric /// Gets the allocation stack trace and thread ID for a heap address (useful 199*0b57cec5SDimitry Andric /// for calling from the debugger). 200*0b57cec5SDimitry Andric /// 201*0b57cec5SDimitry Andric /// Stores up to <c><i>size</i></c> frames in <c><i>trace</i></c>. Returns 202*0b57cec5SDimitry Andric /// the number of stored frames or 0 on error. 203*0b57cec5SDimitry Andric /// 204*0b57cec5SDimitry Andric /// \param addr A heap address. 205*0b57cec5SDimitry Andric /// \param trace A buffer to store the stack trace. 206*0b57cec5SDimitry Andric /// \param size Size in bytes of the trace buffer. 207*0b57cec5SDimitry Andric /// \param thread_id [out] The thread ID of the address. 208*0b57cec5SDimitry Andric /// 209*0b57cec5SDimitry Andric /// \returns Returns the number of stored frames or 0 on error. 210*0b57cec5SDimitry Andric size_t __asan_get_alloc_stack(void *addr, void **trace, size_t size, 211*0b57cec5SDimitry Andric int *thread_id); 212*0b57cec5SDimitry Andric 213*0b57cec5SDimitry Andric /// Gets the free stack trace and thread ID for a heap address (useful for 214*0b57cec5SDimitry Andric /// calling from the debugger). 215*0b57cec5SDimitry Andric /// 216*0b57cec5SDimitry Andric /// Stores up to <c><i>size</i></c> frames in <c><i>trace</i></c>. Returns 217*0b57cec5SDimitry Andric /// the number of stored frames or 0 on error. 218*0b57cec5SDimitry Andric /// 219*0b57cec5SDimitry Andric /// \param addr A heap address. 220*0b57cec5SDimitry Andric /// \param trace A buffer to store the stack trace. 221*0b57cec5SDimitry Andric /// \param size Size in bytes of the trace buffer. 222*0b57cec5SDimitry Andric /// \param thread_id [out] The thread ID of the address. 223*0b57cec5SDimitry Andric /// 224*0b57cec5SDimitry Andric /// \returns Returns the number of stored frames or 0 on error. 225*0b57cec5SDimitry Andric size_t __asan_get_free_stack(void *addr, void **trace, size_t size, 226*0b57cec5SDimitry Andric int *thread_id); 227*0b57cec5SDimitry Andric 228*0b57cec5SDimitry Andric /// Gets the current shadow memory mapping (useful for calling from the 229*0b57cec5SDimitry Andric /// debugger). 230*0b57cec5SDimitry Andric /// 231*0b57cec5SDimitry Andric /// \param shadow_scale [out] Shadow scale value. 232*0b57cec5SDimitry Andric /// \param shadow_offset [out] Offset value. 233*0b57cec5SDimitry Andric void __asan_get_shadow_mapping(size_t *shadow_scale, size_t *shadow_offset); 234*0b57cec5SDimitry Andric 235*0b57cec5SDimitry Andric /// This is an internal function that is called to report an error. However, 236*0b57cec5SDimitry Andric /// it is still a part of the interface because you might want to set a 237*0b57cec5SDimitry Andric /// breakpoint on this function in the debugger. 238*0b57cec5SDimitry Andric /// 239*0b57cec5SDimitry Andric /// \param pc <c><i>pc</i></c> value of the ASan error. 240*0b57cec5SDimitry Andric /// \param bp <c><i>bp</i></c> value of the ASan error. 241*0b57cec5SDimitry Andric /// \param sp <c><i>sp</i></c> value of the ASan error. 242*0b57cec5SDimitry Andric /// \param addr Address of the ASan error. 243*0b57cec5SDimitry Andric /// \param is_write True if the error is a write error; false otherwise. 244*0b57cec5SDimitry Andric /// \param access_size Size of the memory access of the ASan error. 245*0b57cec5SDimitry Andric void __asan_report_error(void *pc, void *bp, void *sp, 246*0b57cec5SDimitry Andric void *addr, int is_write, size_t access_size); 247*0b57cec5SDimitry Andric 248*0b57cec5SDimitry Andric // Deprecated. Call __sanitizer_set_death_callback instead. 249*0b57cec5SDimitry Andric void __asan_set_death_callback(void (*callback)(void)); 250*0b57cec5SDimitry Andric 251*0b57cec5SDimitry Andric /// Sets the callback function to be called during ASan error reporting. 252*0b57cec5SDimitry Andric /// 253*0b57cec5SDimitry Andric /// The callback provides a string pointer to the report. 254*0b57cec5SDimitry Andric /// 255*0b57cec5SDimitry Andric /// \param callback User-provided function. 256*0b57cec5SDimitry Andric void __asan_set_error_report_callback(void (*callback)(const char *)); 257*0b57cec5SDimitry Andric 258*0b57cec5SDimitry Andric /// User-provided callback on ASan errors. 259*0b57cec5SDimitry Andric /// 260*0b57cec5SDimitry Andric /// You can provide a function that would be called immediately when ASan 261*0b57cec5SDimitry Andric /// detects an error. This is useful in cases when ASan detects an error but 262*0b57cec5SDimitry Andric /// your program crashes before the ASan report is printed. 263*0b57cec5SDimitry Andric void __asan_on_error(void); 264*0b57cec5SDimitry Andric 265*0b57cec5SDimitry Andric /// Prints accumulated statistics to <c>stderr</c> (useful for calling from the 266*0b57cec5SDimitry Andric /// debugger). 267*0b57cec5SDimitry Andric void __asan_print_accumulated_stats(void); 268*0b57cec5SDimitry Andric 269*0b57cec5SDimitry Andric /// User-provided default option settings. 270*0b57cec5SDimitry Andric /// 271*0b57cec5SDimitry Andric /// You can provide your own implementation of this function to return a string 272*0b57cec5SDimitry Andric /// containing ASan runtime options (for example, 273*0b57cec5SDimitry Andric /// <c>verbosity=1:halt_on_error=0</c>). 274*0b57cec5SDimitry Andric /// 275*0b57cec5SDimitry Andric /// \returns Default options string. 276*0b57cec5SDimitry Andric const char* __asan_default_options(void); 277*0b57cec5SDimitry Andric 278*0b57cec5SDimitry Andric // The following two functions facilitate garbage collection in presence of 279*0b57cec5SDimitry Andric // ASan's fake stack. 280*0b57cec5SDimitry Andric 281*0b57cec5SDimitry Andric /// Gets an opaque handler to the current thread's fake stack. 282*0b57cec5SDimitry Andric /// 283*0b57cec5SDimitry Andric /// Returns an opaque handler to be used by 284*0b57cec5SDimitry Andric /// <c>__asan_addr_is_in_fake_stack()</c>. Returns NULL if the current thread 285*0b57cec5SDimitry Andric /// does not have a fake stack. 286*0b57cec5SDimitry Andric /// 287*0b57cec5SDimitry Andric /// \returns An opaque handler to the fake stack or NULL. 288*0b57cec5SDimitry Andric void *__asan_get_current_fake_stack(void); 289*0b57cec5SDimitry Andric 290*0b57cec5SDimitry Andric /// Checks if an address belongs to a given fake stack. 291*0b57cec5SDimitry Andric /// 292*0b57cec5SDimitry Andric /// If <c><i>fake_stack</i></c> is non-NULL and <c><i>addr</i></c> belongs to a 293*0b57cec5SDimitry Andric /// fake frame in <c><i>fake_stack</i></c>, returns the address of the real 294*0b57cec5SDimitry Andric /// stack that corresponds to the fake frame and sets <c><i>beg</i></c> and 295*0b57cec5SDimitry Andric /// <c><i>end</i></c> to the boundaries of this fake frame. Otherwise returns 296*0b57cec5SDimitry Andric /// NULL and does not touch <c><i>beg</i></c> and <c><i>end</i></c>. 297*0b57cec5SDimitry Andric /// 298*0b57cec5SDimitry Andric /// If <c><i>beg</i></c> or <c><i>end</i></c> are NULL, they are not touched. 299*0b57cec5SDimitry Andric /// 300*0b57cec5SDimitry Andric /// \note This function can be called from a thread other than the owner of 301*0b57cec5SDimitry Andric /// <c><i>fake_stack</i></c>, but the owner thread needs to be alive. 302*0b57cec5SDimitry Andric /// 303*0b57cec5SDimitry Andric /// \param fake_stack An opaque handler to a fake stack. 304*0b57cec5SDimitry Andric /// \param addr Address to test. 305*0b57cec5SDimitry Andric /// \param beg [out] Beginning of fake frame. 306*0b57cec5SDimitry Andric /// \param end [out] End of fake frame. 307*0b57cec5SDimitry Andric /// \returns Stack address or NULL. 308*0b57cec5SDimitry Andric void *__asan_addr_is_in_fake_stack(void *fake_stack, void *addr, void **beg, 309*0b57cec5SDimitry Andric void **end); 310*0b57cec5SDimitry Andric 311*0b57cec5SDimitry Andric /// Performs shadow memory cleanup of the current thread's stack before a 312*0b57cec5SDimitry Andric /// function marked with the <c>[[noreturn]]</c> attribute is called. 313*0b57cec5SDimitry Andric /// 314*0b57cec5SDimitry Andric /// To avoid false positives on the stack, must be called before no-return 315*0b57cec5SDimitry Andric /// functions like <c>_exit()</c> and <c>execl()</c>. 316*0b57cec5SDimitry Andric void __asan_handle_no_return(void); 317*0b57cec5SDimitry Andric 318*0b57cec5SDimitry Andric #ifdef __cplusplus 319*0b57cec5SDimitry Andric } // extern "C" 320*0b57cec5SDimitry Andric #endif 321*0b57cec5SDimitry Andric 322*0b57cec5SDimitry Andric #endif // SANITIZER_ASAN_INTERFACE_H 323