1 //===-- sanitizer/asan_interface.h ------------------------------*- C++ -*-===// 2 // 3 // The LLVM Compiler Infrastructure 4 // 5 // This file is distributed under the University of Illinois Open Source 6 // License. See LICENSE.TXT for details. 7 // 8 //===----------------------------------------------------------------------===// 9 // 10 // This file is a part of AddressSanitizer, an address sanity checker. 11 // 12 // This header can be included by the instrumented program to fetch 13 // data (mostly allocator statistics) from ASan runtime library. 14 //===----------------------------------------------------------------------===// 15 #ifndef SANITIZER_ASAN_INTERFACE_H 16 #define SANITIZER_ASAN_INTERFACE_H 17 18 #include <sanitizer/common_interface_defs.h> 19 20 // ----------- ATTENTION ------------- 21 // This header should NOT include any other headers from ASan runtime. 22 // All functions in this header are extern "C" and start with __asan_. 23 24 using __sanitizer::uptr; 25 26 extern "C" { 27 // This function should be called at the very beginning of the process, 28 // before any instrumented code is executed and before any call to malloc. 29 void __asan_init() SANITIZER_INTERFACE_ATTRIBUTE; 30 31 // This function should be called by the instrumented code. 32 // 'addr' is the address of a global variable called 'name' of 'size' bytes. 33 void __asan_register_global(uptr addr, uptr size, const char *name) 34 SANITIZER_INTERFACE_ATTRIBUTE; 35 36 // This structure describes an instrumented global variable. 37 struct __asan_global { 38 uptr beg; // The address of the global. 39 uptr size; // The original size of the global. 40 uptr size_with_redzone; // The size with the redzone. 41 const char *name; // Name as a C string. 42 uptr has_dynamic_init; // Non-zero if the global has dynamic initializer. 43 }; 44 45 // These two functions should be called by the instrumented code. 46 // 'globals' is an array of structures describing 'n' globals. 47 void __asan_register_globals(__asan_global *globals, uptr n) 48 SANITIZER_INTERFACE_ATTRIBUTE; 49 void __asan_unregister_globals(__asan_global *globals, uptr n) 50 SANITIZER_INTERFACE_ATTRIBUTE; 51 52 // These two functions should be called before and after dynamic initializers 53 // run, respectively. They should be called with parameters describing all 54 // dynamically initialized globals defined in the calling TU. 55 void __asan_before_dynamic_init(uptr first_addr, uptr last_addr) 56 SANITIZER_INTERFACE_ATTRIBUTE; 57 void __asan_after_dynamic_init() 58 SANITIZER_INTERFACE_ATTRIBUTE; 59 60 // These two functions are used by the instrumented code in the 61 // use-after-return mode. __asan_stack_malloc allocates size bytes of 62 // fake stack and __asan_stack_free poisons it. real_stack is a pointer to 63 // the real stack region. 64 uptr __asan_stack_malloc(uptr size, uptr real_stack) 65 SANITIZER_INTERFACE_ATTRIBUTE; 66 void __asan_stack_free(uptr ptr, uptr size, uptr real_stack) 67 SANITIZER_INTERFACE_ATTRIBUTE; 68 69 // Marks memory region [addr, addr+size) as unaddressable. 70 // This memory must be previously allocated by the user program. Accessing 71 // addresses in this region from instrumented code is forbidden until 72 // this region is unpoisoned. This function is not guaranteed to poison 73 // the whole region - it may poison only subregion of [addr, addr+size) due 74 // to ASan alignment restrictions. 75 // Method is NOT thread-safe in the sense that no two threads can 76 // (un)poison memory in the same memory region simultaneously. 77 void __asan_poison_memory_region(void const volatile *addr, uptr size) 78 SANITIZER_INTERFACE_ATTRIBUTE; 79 // Marks memory region [addr, addr+size) as addressable. 80 // This memory must be previously allocated by the user program. Accessing 81 // addresses in this region is allowed until this region is poisoned again. 82 // This function may unpoison a superregion of [addr, addr+size) due to 83 // ASan alignment restrictions. 84 // Method is NOT thread-safe in the sense that no two threads can 85 // (un)poison memory in the same memory region simultaneously. 86 void __asan_unpoison_memory_region(void const volatile *addr, uptr size) 87 SANITIZER_INTERFACE_ATTRIBUTE; 88 89 // Performs cleanup before a NoReturn function. Must be called before things 90 // like _exit and execl to avoid false positives on stack. 91 void __asan_handle_no_return() SANITIZER_INTERFACE_ATTRIBUTE; 92 93 // User code should use macro instead of functions. 94 #if __has_feature(address_sanitizer) 95 #define ASAN_POISON_MEMORY_REGION(addr, size) \ 96 __asan_poison_memory_region((addr), (size)) 97 #define ASAN_UNPOISON_MEMORY_REGION(addr, size) \ 98 __asan_unpoison_memory_region((addr), (size)) 99 #else 100 #define ASAN_POISON_MEMORY_REGION(addr, size) \ 101 ((void)(addr), (void)(size)) 102 #define ASAN_UNPOISON_MEMORY_REGION(addr, size) \ 103 ((void)(addr), (void)(size)) 104 #endif 105 106 // Returns true iff addr is poisoned (i.e. 1-byte read/write access to this 107 // address will result in error report from AddressSanitizer). 108 bool __asan_address_is_poisoned(void const volatile *addr) 109 SANITIZER_INTERFACE_ATTRIBUTE; 110 111 // This is an internal function that is called to report an error. 112 // However it is still a part of the interface because users may want to 113 // set a breakpoint on this function in a debugger. 114 void __asan_report_error(uptr pc, uptr bp, uptr sp, 115 uptr addr, bool is_write, uptr access_size) 116 SANITIZER_INTERFACE_ATTRIBUTE; 117 118 // Sets the exit code to use when reporting an error. 119 // Returns the old value. 120 int __asan_set_error_exit_code(int exit_code) 121 SANITIZER_INTERFACE_ATTRIBUTE; 122 123 // Sets the callback to be called right before death on error. 124 // Passing 0 will unset the callback. 125 void __asan_set_death_callback(void (*callback)(void)) 126 SANITIZER_INTERFACE_ATTRIBUTE; 127 128 void __asan_set_error_report_callback(void (*callback)(const char*)) 129 SANITIZER_INTERFACE_ATTRIBUTE; 130 131 // Sets the callback to be called right when ASan detects an error. 132 // This can be used to notice cases when ASan detects an error, but the 133 // program crashes before ASan report is printed. 134 // Passing 0 unsets the callback. 135 void __asan_set_on_error_callback(void (*callback)(void)) 136 SANITIZER_INTERFACE_ATTRIBUTE; 137 138 // User may register its own symbolization function. It should print 139 // the description of instruction at address "pc" to "out_buffer". 140 // Description should be at most "out_size" bytes long. 141 // User-specified function should return true if symbolization was 142 // successful. 143 typedef bool (*__asan_symbolize_callback)(const void *pc, char *out_buffer, 144 int out_size); 145 void __asan_set_symbolize_callback(__asan_symbolize_callback callback) 146 SANITIZER_INTERFACE_ATTRIBUTE; 147 148 // Returns the estimated number of bytes that will be reserved by allocator 149 // for request of "size" bytes. If ASan allocator can't allocate that much 150 // memory, returns the maximal possible allocation size, otherwise returns 151 // "size". 152 uptr __asan_get_estimated_allocated_size(uptr size) 153 SANITIZER_INTERFACE_ATTRIBUTE; 154 // Returns true if p was returned by the ASan allocator and 155 // is not yet freed. 156 bool __asan_get_ownership(const void *p) 157 SANITIZER_INTERFACE_ATTRIBUTE; 158 // Returns the number of bytes reserved for the pointer p. 159 // Requires (get_ownership(p) == true) or (p == 0). 160 uptr __asan_get_allocated_size(const void *p) 161 SANITIZER_INTERFACE_ATTRIBUTE; 162 // Number of bytes, allocated and not yet freed by the application. 163 uptr __asan_get_current_allocated_bytes() 164 SANITIZER_INTERFACE_ATTRIBUTE; 165 // Number of bytes, mmaped by asan allocator to fulfill allocation requests. 166 // Generally, for request of X bytes, allocator can reserve and add to free 167 // lists a large number of chunks of size X to use them for future requests. 168 // All these chunks count toward the heap size. Currently, allocator never 169 // releases memory to OS (instead, it just puts freed chunks to free lists). 170 uptr __asan_get_heap_size() 171 SANITIZER_INTERFACE_ATTRIBUTE; 172 // Number of bytes, mmaped by asan allocator, which can be used to fulfill 173 // allocation requests. When a user program frees memory chunk, it can first 174 // fall into quarantine and will count toward __asan_get_free_bytes() later. 175 uptr __asan_get_free_bytes() 176 SANITIZER_INTERFACE_ATTRIBUTE; 177 // Number of bytes in unmapped pages, that are released to OS. Currently, 178 // always returns 0. 179 uptr __asan_get_unmapped_bytes() 180 SANITIZER_INTERFACE_ATTRIBUTE; 181 // Prints accumulated stats to stderr. Used for debugging. 182 void __asan_print_accumulated_stats() 183 SANITIZER_INTERFACE_ATTRIBUTE; 184 185 // This function may be overriden by user to provide a string containing 186 // ASan runtime options. See asan_flags.h for details. 187 const char* __asan_default_options() 188 SANITIZER_WEAK_ATTRIBUTE SANITIZER_INTERFACE_ATTRIBUTE; 189 190 // Malloc hooks that may be overriden by user. 191 // __asan_malloc_hook(ptr, size) is called immediately after 192 // allocation of "size" bytes, which returned "ptr". 193 // __asan_free_hook(ptr) is called immediately before 194 // deallocation of "ptr". 195 // If user doesn't provide implementations of these hooks, they are no-op. 196 void __asan_malloc_hook(void *ptr, uptr size) 197 SANITIZER_WEAK_ATTRIBUTE SANITIZER_INTERFACE_ATTRIBUTE; 198 void __asan_free_hook(void *ptr) 199 SANITIZER_WEAK_ATTRIBUTE SANITIZER_INTERFACE_ATTRIBUTE; 200 } // extern "C" 201 202 #endif // SANITIZER_ASAN_INTERFACE_H 203