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 | |
| 53 | // Returns true iff addr is poisoned (i.e. 1-byte read/write access to this |
| 54 | // address will result in error report from AddressSanitizer). |
Alexey Samsonov | c70fa28 | 2013-01-31 13:46:14 +0000 | [diff] [blame] | 55 | bool __asan_address_is_poisoned(void const volatile *addr); |
Kostya Serebryany | 1e172b4 | 2011-11-30 01:07:02 +0000 | [diff] [blame] | 56 | |
Kostya Serebryany | 681e775 | 2012-12-28 15:28:19 +0000 | [diff] [blame] | 57 | // If at least on byte in [beg, beg+size) is poisoned, return the address |
| 58 | // of the first such byte. Otherwise return 0. |
Alexey Samsonov | c70fa28 | 2013-01-31 13:46:14 +0000 | [diff] [blame] | 59 | void *__asan_region_is_poisoned(void *beg, size_t size); |
Kostya Serebryany | 681e775 | 2012-12-28 15:28:19 +0000 | [diff] [blame] | 60 | |
Kostya Serebryany | 17a7c67 | 2012-12-29 10:18:31 +0000 | [diff] [blame] | 61 | // Print the description of addr (useful when debugging in gdb). |
Alexey Samsonov | c70fa28 | 2013-01-31 13:46:14 +0000 | [diff] [blame] | 62 | void __asan_describe_address(void *addr); |
Kostya Serebryany | 17a7c67 | 2012-12-29 10:18:31 +0000 | [diff] [blame] | 63 | |
Kostya Serebryany | 1e172b4 | 2011-11-30 01:07:02 +0000 | [diff] [blame] | 64 | // This is an internal function that is called to report an error. |
| 65 | // However it is still a part of the interface because users may want to |
| 66 | // set a breakpoint on this function in a debugger. |
Alexey Samsonov | c70fa28 | 2013-01-31 13:46:14 +0000 | [diff] [blame] | 67 | void __asan_report_error(void *pc, void *bp, void *sp, |
| 68 | void *addr, bool is_write, size_t access_size); |
Kostya Serebryany | 1e172b4 | 2011-11-30 01:07:02 +0000 | [diff] [blame] | 69 | |
| 70 | // Sets the exit code to use when reporting an error. |
| 71 | // Returns the old value. |
Alexey Samsonov | c70fa28 | 2013-01-31 13:46:14 +0000 | [diff] [blame] | 72 | int __asan_set_error_exit_code(int exit_code); |
Kostya Serebryany | e1fe0fd | 2012-02-13 21:24:29 +0000 | [diff] [blame] | 73 | |
| 74 | // Sets the callback to be called right before death on error. |
Kostya Serebryany | 3f4c387 | 2012-05-31 14:35:53 +0000 | [diff] [blame] | 75 | // Passing 0 will unset the callback. |
Alexey Samsonov | c70fa28 | 2013-01-31 13:46:14 +0000 | [diff] [blame] | 76 | void __asan_set_death_callback(void (*callback)(void)); |
Kostya Serebryany | 1e172b4 | 2011-11-30 01:07:02 +0000 | [diff] [blame] | 77 | |
Alexey Samsonov | c70fa28 | 2013-01-31 13:46:14 +0000 | [diff] [blame] | 78 | void __asan_set_error_report_callback(void (*callback)(const char*)); |
Alexander Potapenko | 3fe9135 | 2012-02-27 14:06:48 +0000 | [diff] [blame] | 79 | |
Alexey Samsonov | 8663343 | 2012-10-02 14:06:39 +0000 | [diff] [blame] | 80 | // User may provide function that would be called right when ASan detects |
| 81 | // an error. This can be used to notice cases when ASan detects an error, but |
| 82 | // the program crashes before ASan report is printed. |
Alexey Samsonov | c70fa28 | 2013-01-31 13:46:14 +0000 | [diff] [blame] | 83 | void __asan_on_error(); |
Alexey Samsonov | f657a19 | 2012-08-13 11:23:40 +0000 | [diff] [blame] | 84 | |
Alexey Samsonov | 1ca5357 | 2012-10-02 12:11:17 +0000 | [diff] [blame] | 85 | // User may provide its own implementation for symbolization function. |
| 86 | // It should print the description of instruction at address "pc" to |
| 87 | // "out_buffer". Description should be at most "out_size" bytes long. |
Alexey Samsonov | c93d3e2 | 2012-08-22 13:31:37 +0000 | [diff] [blame] | 88 | // User-specified function should return true if symbolization was |
| 89 | // successful. |
Alexey Samsonov | c70fa28 | 2013-01-31 13:46:14 +0000 | [diff] [blame] | 90 | bool __asan_symbolize(const void *pc, char *out_buffer, |
| 91 | int out_size); |
Alexey Samsonov | c93d3e2 | 2012-08-22 13:31:37 +0000 | [diff] [blame] | 92 | |
Kostya Serebryany | 1e172b4 | 2011-11-30 01:07:02 +0000 | [diff] [blame] | 93 | // Returns the estimated number of bytes that will be reserved by allocator |
| 94 | // for request of "size" bytes. If ASan allocator can't allocate that much |
| 95 | // memory, returns the maximal possible allocation size, otherwise returns |
| 96 | // "size". |
Alexey Samsonov | c70fa28 | 2013-01-31 13:46:14 +0000 | [diff] [blame] | 97 | size_t __asan_get_estimated_allocated_size(size_t size); |
Alexey Samsonov | ca2278d | 2012-01-18 15:26:55 +0000 | [diff] [blame] | 98 | // Returns true if p was returned by the ASan allocator and |
Kostya Serebryany | 1e172b4 | 2011-11-30 01:07:02 +0000 | [diff] [blame] | 99 | // is not yet freed. |
Alexey Samsonov | c70fa28 | 2013-01-31 13:46:14 +0000 | [diff] [blame] | 100 | bool __asan_get_ownership(const void *p); |
Kostya Serebryany | 1e172b4 | 2011-11-30 01:07:02 +0000 | [diff] [blame] | 101 | // Returns the number of bytes reserved for the pointer p. |
Kostya Serebryany | 3f4c387 | 2012-05-31 14:35:53 +0000 | [diff] [blame] | 102 | // Requires (get_ownership(p) == true) or (p == 0). |
Alexey Samsonov | c70fa28 | 2013-01-31 13:46:14 +0000 | [diff] [blame] | 103 | size_t __asan_get_allocated_size(const void *p); |
Kostya Serebryany | 1e172b4 | 2011-11-30 01:07:02 +0000 | [diff] [blame] | 104 | // Number of bytes, allocated and not yet freed by the application. |
Alexey Samsonov | c70fa28 | 2013-01-31 13:46:14 +0000 | [diff] [blame] | 105 | size_t __asan_get_current_allocated_bytes(); |
Kostya Serebryany | 1e172b4 | 2011-11-30 01:07:02 +0000 | [diff] [blame] | 106 | // Number of bytes, mmaped by asan allocator to fulfill allocation requests. |
| 107 | // Generally, for request of X bytes, allocator can reserve and add to free |
| 108 | // lists a large number of chunks of size X to use them for future requests. |
| 109 | // All these chunks count toward the heap size. Currently, allocator never |
| 110 | // releases memory to OS (instead, it just puts freed chunks to free lists). |
Alexey Samsonov | c70fa28 | 2013-01-31 13:46:14 +0000 | [diff] [blame] | 111 | size_t __asan_get_heap_size(); |
Kostya Serebryany | 1e172b4 | 2011-11-30 01:07:02 +0000 | [diff] [blame] | 112 | // Number of bytes, mmaped by asan allocator, which can be used to fulfill |
| 113 | // allocation requests. When a user program frees memory chunk, it can first |
| 114 | // fall into quarantine and will count toward __asan_get_free_bytes() later. |
Alexey Samsonov | c70fa28 | 2013-01-31 13:46:14 +0000 | [diff] [blame] | 115 | size_t __asan_get_free_bytes(); |
Kostya Serebryany | 1e172b4 | 2011-11-30 01:07:02 +0000 | [diff] [blame] | 116 | // Number of bytes in unmapped pages, that are released to OS. Currently, |
| 117 | // always returns 0. |
Alexey Samsonov | c70fa28 | 2013-01-31 13:46:14 +0000 | [diff] [blame] | 118 | size_t __asan_get_unmapped_bytes(); |
Kostya Serebryany | 1e172b4 | 2011-11-30 01:07:02 +0000 | [diff] [blame] | 119 | // Prints accumulated stats to stderr. Used for debugging. |
Alexey Samsonov | c70fa28 | 2013-01-31 13:46:14 +0000 | [diff] [blame] | 120 | void __asan_print_accumulated_stats(); |
Alexey Samsonov | c58b57e | 2012-08-15 07:11:14 +0000 | [diff] [blame] | 121 | |
Alexey Samsonov | 6a08d29 | 2012-12-07 22:01:28 +0000 | [diff] [blame] | 122 | // This function may be optionally provided by user and should return |
| 123 | // a string containing ASan runtime options. See asan_flags.h for details. |
Alexey Samsonov | c70fa28 | 2013-01-31 13:46:14 +0000 | [diff] [blame] | 124 | const char* __asan_default_options(); |
Alexey Samsonov | b21de9e | 2012-08-22 10:12:47 +0000 | [diff] [blame] | 125 | |
Alexey Samsonov | 6a08d29 | 2012-12-07 22:01:28 +0000 | [diff] [blame] | 126 | // Malloc hooks that may be optionally provided by user. |
Alexey Samsonov | b21de9e | 2012-08-22 10:12:47 +0000 | [diff] [blame] | 127 | // __asan_malloc_hook(ptr, size) is called immediately after |
| 128 | // allocation of "size" bytes, which returned "ptr". |
| 129 | // __asan_free_hook(ptr) is called immediately before |
| 130 | // deallocation of "ptr". |
Alexey Samsonov | c70fa28 | 2013-01-31 13:46:14 +0000 | [diff] [blame] | 131 | void __asan_malloc_hook(void *ptr, size_t size); |
| 132 | void __asan_free_hook(void *ptr); |
| 133 | #ifdef __cplusplus |
Alexey Samsonov | c58b57e | 2012-08-15 07:11:14 +0000 | [diff] [blame] | 134 | } // extern "C" |
Alexey Samsonov | c70fa28 | 2013-01-31 13:46:14 +0000 | [diff] [blame] | 135 | #endif |
Kostya Serebryany | 1e172b4 | 2011-11-30 01:07:02 +0000 | [diff] [blame] | 136 | |
Chandler Carruth | d865fec | 2012-08-29 02:27:54 +0000 | [diff] [blame] | 137 | #endif // SANITIZER_ASAN_INTERFACE_H |