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