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