1//===-- 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 ASAN_INTERFACE_H
16#define ASAN_INTERFACE_H
17
18// ----------- ATTENTION -------------
19// This header should NOT include any other headers from ASan runtime.
20// All functions in this header are extern "C" and start with __asan_.
21
22#if !defined(_WIN32)
23#include <stdint.h>  // for uintptr_t
24#define ASAN_INTERFACE_FUNCTION_ATTRIBUTE __attribute__((visibility("default")))
25#else
26// TODO(timurrrr): find out what we need on Windows. __declspec(dllexport) ?
27#define ASAN_INTERFACE_FUNCTION_ATTRIBUTE
28#endif
29#include <stddef.h>  // for size_t
30
31extern "C" {
32  // This function should be called at the very beginning of the process,
33  // before any instrumented code is executed and before any call to malloc.
34  void __asan_init() ASAN_INTERFACE_FUNCTION_ATTRIBUTE;
35
36  // This function should be called by the instrumented code.
37  // 'addr' is the address of a global variable called 'name' of 'size' bytes.
38  void __asan_register_global(uintptr_t addr, size_t size, const char *name)
39      ASAN_INTERFACE_FUNCTION_ATTRIBUTE;
40
41  // This structure describes an instrumented global variable.
42  struct __asan_global {
43    size_t beg;                // The address of the global.
44    size_t size;               // The original size of the global.
45    size_t size_with_redzone;  // The size with the redzone.
46    const char *name;          // Name as a C string.
47  };
48
49  // These two functions should be called by the instrumented code.
50  // 'globals' is an array of structures describing 'n' globals.
51  void __asan_register_globals(__asan_global *globals, size_t n)
52      ASAN_INTERFACE_FUNCTION_ATTRIBUTE;
53  void __asan_unregister_globals(__asan_global *globals, size_t n)
54      ASAN_INTERFACE_FUNCTION_ATTRIBUTE;
55
56  // These two functions are used by the instrumented code in the
57  // use-after-return mode. __asan_stack_malloc allocates size bytes of
58  // fake stack and __asan_stack_free poisons it. real_stack is a pointer to
59  // the real stack region.
60  size_t __asan_stack_malloc(size_t size, size_t real_stack)
61      ASAN_INTERFACE_FUNCTION_ATTRIBUTE;
62  void __asan_stack_free(size_t ptr, size_t size, size_t real_stack)
63      ASAN_INTERFACE_FUNCTION_ATTRIBUTE;
64
65  // Marks memory region [addr, addr+size) as unaddressable.
66  // This memory must be previously allocated by the user program. Accessing
67  // addresses in this region from instrumented code is forbidden until
68  // this region is unpoisoned. This function is not guaranteed to poison
69  // the whole region - it may poison only subregion of [addr, addr+size) due
70  // to ASan alignment restrictions.
71  // Method is NOT thread-safe in the sense that no two threads can
72  // (un)poison memory in the same memory region simultaneously.
73  void __asan_poison_memory_region(void const volatile *addr, size_t size)
74      ASAN_INTERFACE_FUNCTION_ATTRIBUTE;
75  // Marks memory region [addr, addr+size) as addressable.
76  // This memory must be previously allocated by the user program. Accessing
77  // addresses in this region is allowed until this region is poisoned again.
78  // This function may unpoison a superregion of [addr, addr+size) due to
79  // ASan alignment restrictions.
80  // Method is NOT thread-safe in the sense that no two threads can
81  // (un)poison memory in the same memory region simultaneously.
82  void __asan_unpoison_memory_region(void const volatile *addr, size_t size)
83      ASAN_INTERFACE_FUNCTION_ATTRIBUTE;
84
85  // Performs cleanup before a NoReturn function. Must be called before things
86  // like _exit and execl to avoid false positives on stack.
87  void __asan_handle_no_return() ASAN_INTERFACE_FUNCTION_ATTRIBUTE;
88
89// User code should use macro instead of functions.
90#if !defined(__has_feature)
91#define __has_feature(x) 0
92#endif
93#if __has_feature(address_sanitizer)
94#define ASAN_POISON_MEMORY_REGION(addr, size) \
95  __asan_poison_memory_region((addr), (size))
96#define ASAN_UNPOISON_MEMORY_REGION(addr, size) \
97  __asan_unpoison_memory_region((addr), (size))
98#else
99#define ASAN_POISON_MEMORY_REGION(addr, size) \
100  ((void)(addr), (void)(size))
101#define ASAN_UNPOISON_MEMORY_REGION(addr, size) \
102  ((void)(addr), (void)(size))
103#endif
104
105  // Returns true iff addr is poisoned (i.e. 1-byte read/write access to this
106  // address will result in error report from AddressSanitizer).
107  bool __asan_address_is_poisoned(void const volatile *addr)
108      ASAN_INTERFACE_FUNCTION_ATTRIBUTE;
109
110  // This is an internal function that is called to report an error.
111  // However it is still a part of the interface because users may want to
112  // set a breakpoint on this function in a debugger.
113  void __asan_report_error(uintptr_t pc, uintptr_t bp, uintptr_t sp,
114                           uintptr_t addr, bool is_write, size_t access_size)
115    ASAN_INTERFACE_FUNCTION_ATTRIBUTE;
116
117  // Sets the exit code to use when reporting an error.
118  // Returns the old value.
119  int __asan_set_error_exit_code(int exit_code)
120      ASAN_INTERFACE_FUNCTION_ATTRIBUTE;
121
122  // Sets the callback to be called right before death on error.
123  // Passing NULL will unset the callback.
124  void __asan_set_death_callback(void (*callback)(void))
125      ASAN_INTERFACE_FUNCTION_ATTRIBUTE;
126
127  void __asan_set_error_report_callback(void (*callback)(const char*))
128      ASAN_INTERFACE_FUNCTION_ATTRIBUTE;
129
130  // Returns the estimated number of bytes that will be reserved by allocator
131  // for request of "size" bytes. If ASan allocator can't allocate that much
132  // memory, returns the maximal possible allocation size, otherwise returns
133  // "size".
134  size_t __asan_get_estimated_allocated_size(size_t size)
135      ASAN_INTERFACE_FUNCTION_ATTRIBUTE;
136  // Returns true if p was returned by the ASan allocator and
137  // is not yet freed.
138  bool __asan_get_ownership(const void *p)
139      ASAN_INTERFACE_FUNCTION_ATTRIBUTE;
140  // Returns the number of bytes reserved for the pointer p.
141  // Requires (get_ownership(p) == true) or (p == NULL).
142  size_t __asan_get_allocated_size(const void *p)
143      ASAN_INTERFACE_FUNCTION_ATTRIBUTE;
144  // Number of bytes, allocated and not yet freed by the application.
145  size_t __asan_get_current_allocated_bytes()
146      ASAN_INTERFACE_FUNCTION_ATTRIBUTE;
147  // Number of bytes, mmaped by asan allocator to fulfill allocation requests.
148  // Generally, for request of X bytes, allocator can reserve and add to free
149  // lists a large number of chunks of size X to use them for future requests.
150  // All these chunks count toward the heap size. Currently, allocator never
151  // releases memory to OS (instead, it just puts freed chunks to free lists).
152  size_t __asan_get_heap_size()
153      ASAN_INTERFACE_FUNCTION_ATTRIBUTE;
154  // Number of bytes, mmaped by asan allocator, which can be used to fulfill
155  // allocation requests. When a user program frees memory chunk, it can first
156  // fall into quarantine and will count toward __asan_get_free_bytes() later.
157  size_t __asan_get_free_bytes()
158      ASAN_INTERFACE_FUNCTION_ATTRIBUTE;
159  // Number of bytes in unmapped pages, that are released to OS. Currently,
160  // always returns 0.
161  size_t __asan_get_unmapped_bytes()
162      ASAN_INTERFACE_FUNCTION_ATTRIBUTE;
163  // Prints accumulated stats to stderr. Used for debugging.
164  void __asan_print_accumulated_stats()
165      ASAN_INTERFACE_FUNCTION_ATTRIBUTE;
166}  // namespace
167
168#undef ASAN_INTERFACE_FUNCTION_ATTRIBUTE
169#endif  // ASAN_INTERFACE_H
170