1 1.1 kamil //===-- sanitizer/asan_interface.h ------------------------------*- C++ -*-===// 2 1.1 kamil // 3 1.1 kamil // The LLVM Compiler Infrastructure 4 1.1 kamil // 5 1.1 kamil // This file is distributed under the University of Illinois Open Source 6 1.1 kamil // License. See LICENSE.TXT for details. 7 1.1 kamil // 8 1.1 kamil //===----------------------------------------------------------------------===// 9 1.1 kamil // 10 1.1 kamil // This file is a part of AddressSanitizer. 11 1.1 kamil // 12 1.1 kamil // Public interface header. 13 1.1 kamil //===----------------------------------------------------------------------===// 14 1.1 kamil #ifndef SANITIZER_ASAN_INTERFACE_H 15 1.1 kamil #define SANITIZER_ASAN_INTERFACE_H 16 1.1 kamil 17 1.1 kamil #include <sanitizer/common_interface_defs.h> 18 1.1 kamil 19 1.1 kamil #ifdef __cplusplus 20 1.1 kamil extern "C" { 21 1.1 kamil #endif 22 1.1 kamil // Marks memory region [addr, addr+size) as unaddressable. 23 1.1 kamil // This memory must be previously allocated by the user program. Accessing 24 1.1 kamil // addresses in this region from instrumented code is forbidden until 25 1.1 kamil // this region is unpoisoned. This function is not guaranteed to poison 26 1.1 kamil // the whole region - it may poison only subregion of [addr, addr+size) due 27 1.1 kamil // to ASan alignment restrictions. 28 1.1 kamil // Method is NOT thread-safe in the sense that no two threads can 29 1.1 kamil // (un)poison memory in the same memory region simultaneously. 30 1.1 kamil void __asan_poison_memory_region(void const volatile *addr, size_t size); 31 1.1 kamil // Marks memory region [addr, addr+size) as addressable. 32 1.1 kamil // This memory must be previously allocated by the user program. Accessing 33 1.1 kamil // addresses in this region is allowed until this region is poisoned again. 34 1.1 kamil // This function may unpoison a superregion of [addr, addr+size) due to 35 1.1 kamil // ASan alignment restrictions. 36 1.1 kamil // Method is NOT thread-safe in the sense that no two threads can 37 1.1 kamil // (un)poison memory in the same memory region simultaneously. 38 1.1 kamil void __asan_unpoison_memory_region(void const volatile *addr, size_t size); 39 1.1 kamil 40 1.1 kamil // User code should use macros instead of functions. 41 1.1 kamil #if __has_feature(address_sanitizer) || defined(__SANITIZE_ADDRESS__) 42 1.1 kamil #define ASAN_POISON_MEMORY_REGION(addr, size) \ 43 1.1 kamil __asan_poison_memory_region((addr), (size)) 44 1.1 kamil #define ASAN_UNPOISON_MEMORY_REGION(addr, size) \ 45 1.1 kamil __asan_unpoison_memory_region((addr), (size)) 46 1.1 kamil #else 47 1.1 kamil #define ASAN_POISON_MEMORY_REGION(addr, size) \ 48 1.1 kamil ((void)(addr), (void)(size)) 49 1.1 kamil #define ASAN_UNPOISON_MEMORY_REGION(addr, size) \ 50 1.1 kamil ((void)(addr), (void)(size)) 51 1.1 kamil #endif 52 1.1 kamil 53 1.1 kamil // Returns 1 if addr is poisoned (i.e. 1-byte read/write access to this 54 1.1 kamil // address will result in error report from AddressSanitizer). 55 1.1 kamil // Otherwise returns 0. 56 1.1 kamil int __asan_address_is_poisoned(void const volatile *addr); 57 1.1 kamil 58 1.1 kamil // If at least one byte in [beg, beg+size) is poisoned, return the address 59 1.1 kamil // of the first such byte. Otherwise return 0. 60 1.1 kamil void *__asan_region_is_poisoned(void *beg, size_t size); 61 1.1 kamil 62 1.1 kamil // Print the description of addr (useful when debugging in gdb). 63 1.1 kamil void __asan_describe_address(void *addr); 64 1.1 kamil 65 1.1 kamil // Useful for calling from a debugger to get information about an ASan error. 66 1.1 kamil // Returns 1 if an error has been (or is being) reported, otherwise returns 0. 67 1.1 kamil int __asan_report_present(void); 68 1.1 kamil 69 1.1 kamil // Useful for calling from a debugger to get information about an ASan error. 70 1.1 kamil // If an error has been (or is being) reported, the following functions return 71 1.1 kamil // the pc, bp, sp, address, access type (0 = read, 1 = write), access size and 72 1.1 kamil // bug description (e.g. "heap-use-after-free"). Otherwise they return 0. 73 1.1 kamil void *__asan_get_report_pc(void); 74 1.1 kamil void *__asan_get_report_bp(void); 75 1.1 kamil void *__asan_get_report_sp(void); 76 1.1 kamil void *__asan_get_report_address(void); 77 1.1 kamil int __asan_get_report_access_type(void); 78 1.1 kamil size_t __asan_get_report_access_size(void); 79 1.1 kamil const char *__asan_get_report_description(void); 80 1.1 kamil 81 1.1 kamil // Useful for calling from the debugger to get information about a pointer. 82 1.1 kamil // Returns the category of the given pointer as a constant string. 83 1.1 kamil // Possible return values are "global", "stack", "stack-fake", "heap", 84 1.1 kamil // "heap-invalid", "shadow-low", "shadow-gap", "shadow-high", "unknown". 85 1.1 kamil // If global or stack, tries to also return the variable name, address and 86 1.1 kamil // size. If heap, tries to return the chunk address and size. 'name' should 87 1.1 kamil // point to an allocated buffer of size 'name_size'. 88 1.1 kamil const char *__asan_locate_address(void *addr, char *name, size_t name_size, 89 1.1 kamil void **region_address, size_t *region_size); 90 1.1 kamil 91 1.1 kamil // Useful for calling from the debugger to get the allocation stack trace 92 1.1 kamil // and thread ID for a heap address. Stores up to 'size' frames into 'trace', 93 1.1 kamil // returns the number of stored frames or 0 on error. 94 1.1 kamil size_t __asan_get_alloc_stack(void *addr, void **trace, size_t size, 95 1.1 kamil int *thread_id); 96 1.1 kamil 97 1.1 kamil // Useful for calling from the debugger to get the free stack trace 98 1.1 kamil // and thread ID for a heap address. Stores up to 'size' frames into 'trace', 99 1.1 kamil // returns the number of stored frames or 0 on error. 100 1.1 kamil size_t __asan_get_free_stack(void *addr, void **trace, size_t size, 101 1.1 kamil int *thread_id); 102 1.1 kamil 103 1.1 kamil // Useful for calling from the debugger to get the current shadow memory 104 1.1 kamil // mapping. 105 1.1 kamil void __asan_get_shadow_mapping(size_t *shadow_scale, size_t *shadow_offset); 106 1.1 kamil 107 1.1 kamil // This is an internal function that is called to report an error. 108 1.1 kamil // However it is still a part of the interface because users may want to 109 1.1 kamil // set a breakpoint on this function in a debugger. 110 1.1 kamil void __asan_report_error(void *pc, void *bp, void *sp, 111 1.1 kamil void *addr, int is_write, size_t access_size); 112 1.1 kamil 113 1.1 kamil // Deprecated. Call __sanitizer_set_death_callback instead. 114 1.1 kamil void __asan_set_death_callback(void (*callback)(void)); 115 1.1 kamil 116 1.1 kamil void __asan_set_error_report_callback(void (*callback)(const char*)); 117 1.1 kamil 118 1.1 kamil // User may provide function that would be called right when ASan detects 119 1.1 kamil // an error. This can be used to notice cases when ASan detects an error, but 120 1.1 kamil // the program crashes before ASan report is printed. 121 1.1 kamil void __asan_on_error(void); 122 1.1 kamil 123 1.1 kamil // Prints accumulated stats to stderr. Used for debugging. 124 1.1 kamil void __asan_print_accumulated_stats(void); 125 1.1 kamil 126 1.1 kamil // This function may be optionally provided by user and should return 127 1.1 kamil // a string containing ASan runtime options. See asan_flags.h for details. 128 1.1 kamil const char* __asan_default_options(void); 129 1.1 kamil 130 1.1 kamil // The following 2 functions facilitate garbage collection in presence of 131 1.1 kamil // asan's fake stack. 132 1.1 kamil 133 1.1 kamil // Returns an opaque handler to be used later in __asan_addr_is_in_fake_stack. 134 1.1 kamil // Returns NULL if the current thread does not have a fake stack. 135 1.1 kamil void *__asan_get_current_fake_stack(void); 136 1.1 kamil 137 1.1 kamil // If fake_stack is non-NULL and addr belongs to a fake frame in 138 1.1 kamil // fake_stack, returns the address on real stack that corresponds to 139 1.1 kamil // the fake frame and sets beg/end to the boundaries of this fake frame. 140 1.1 kamil // Otherwise returns NULL and does not touch beg/end. 141 1.1 kamil // If beg/end are NULL, they are not touched. 142 1.1 kamil // This function may be called from a thread other than the owner of 143 1.1 kamil // fake_stack, but the owner thread need to be alive. 144 1.1 kamil void *__asan_addr_is_in_fake_stack(void *fake_stack, void *addr, void **beg, 145 1.1 kamil void **end); 146 1.1 kamil 147 1.1 kamil // Performs cleanup before a [[noreturn]] function. Must be called 148 1.1 kamil // before things like _exit and execl to avoid false positives on stack. 149 1.1 kamil void __asan_handle_no_return(void); 150 1.1 kamil 151 1.1 kamil #ifdef __cplusplus 152 1.1 kamil } // extern "C" 153 1.1 kamil #endif 154 1.1 kamil 155 1.1 kamil #endif // SANITIZER_ASAN_INTERFACE_H 156