blob: 7265a69e220bc015a08876af6d55e994d947397a [file] [log] [blame]
Kostya Serebryany1e172b42011-11-30 01:07:02 +00001//===-- 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#include <stdint.h> // for __WORDSIZE
19#include <stdlib.h> // for size_t
20
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
24extern "C" {
25 // This function should be called at the very beginning of the process,
26 // before any instrumented code is executed and before any call to malloc.
27 void __asan_init()
28 __attribute__((visibility("default")));
29
30 // This function should be called by the instrumented code.
31 // 'addr' is the address of a global variable called 'name' of 'size' bytes.
32 void __asan_register_global(uintptr_t addr, size_t size, const char *name)
33 __attribute__((visibility("default")));
34
35 // This structure describes an instrumented global variable.
36 struct __asan_global {
37 size_t beg; // The address of the global.
38 size_t size; // The original size of the global.
39 size_t size_with_redzone; // The size with the redzone.
40 const char *name; // Name as a C string.
41 };
42
43 // This function should be called by the instrumented code.
44 // gets an array of structures describing globals.
45 void __asan_register_globals(__asan_global *globals, size_t n)
46 __attribute__((visibility("default")));
47
48 // These two functions are used by the instrumented code in the
49 // use-after-return mode. __asan_stack_malloc allocates size bytes of
50 // fake stack and __asan_stack_free poisons it. real_stack is a pointer to
51 // the real stack region.
52 size_t __asan_stack_malloc(size_t size, size_t real_stack)
53 __attribute__((visibility("default")));
54 void __asan_stack_free(size_t ptr, size_t size, size_t real_stack)
55 __attribute__((visibility("default")));
56
57 // Marks memory region [addr, addr+size) as unaddressable.
58 // This memory must be previously allocated by the user program. Accessing
59 // addresses in this region from instrumented code is forbidden until
60 // this region is unpoisoned. This function is not guaranteed to poison
61 // the whole region - it may poison only subregion of [addr, addr+size) due
62 // to ASan alignment restrictions.
63 // Method is NOT thread-safe in the sense that no two threads can
64 // (un)poison memory in the same memory region simultaneously.
65 void __asan_poison_memory_region(void const volatile *addr, size_t size);
66 // Marks memory region [addr, addr+size) as addressable.
67 // This memory must be previously allocated by the user program. Accessing
68 // addresses in this region is allowed until this region is poisoned again.
69 // This function may unpoison a superregion of [addr, addr+size) due to
70 // 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_unpoison_memory_region(void const volatile *addr, size_t size);
74
75// User code should use macro instead of functions.
76#ifdef ADDRESS_SANITIZER
77#define ASAN_POISON_MEMORY_REGION(addr, size) \
78 __asan_poison_memory_region((addr), (size))
79#define ASAN_UNPOISON_MEMORY_REGION(addr, size) \
80 __asan_unpoison_memory_region((addr), (size))
81#else
82#define ASAN_POISON_MEMORY_REGION(addr, size) \
83 ((void)(addr), (void)(size))
84#define ASAN_UNPOISON_MEMORY_REGION(addr, size) \
85 ((void)(addr), (void)(size))
86#endif
87
88 // Returns true iff addr is poisoned (i.e. 1-byte read/write access to this
89 // address will result in error report from AddressSanitizer).
90 bool __asan_address_is_poisoned(void const volatile *addr);
91
92 // This is an internal function that is called to report an error.
93 // However it is still a part of the interface because users may want to
94 // set a breakpoint on this function in a debugger.
95 void __asan_report_error(uintptr_t pc, uintptr_t bp, uintptr_t sp,
96 uintptr_t addr, bool is_write, size_t access_size)
97 __attribute__((visibility("default")));
98
99 // Sets the exit code to use when reporting an error.
100 // Returns the old value.
101 int __asan_set_error_exit_code(int exit_code);
102
103 // Returns the estimated number of bytes that will be reserved by allocator
104 // for request of "size" bytes. If ASan allocator can't allocate that much
105 // memory, returns the maximal possible allocation size, otherwise returns
106 // "size".
107 size_t __asan_get_estimated_allocated_size(size_t size);
108 // Returns true if p is NULL or if p was returned by the ASan allocator and
109 // is not yet freed.
110 bool __asan_get_ownership(const void *p);
111 // Returns the number of bytes reserved for the pointer p.
112 // Requires (get_ownership(p) == true).
113 size_t __asan_get_allocated_size(const void *p);
114 // Number of bytes, allocated and not yet freed by the application.
115 size_t __asan_get_current_allocated_bytes();
116 // Number of bytes, mmaped by asan allocator to fulfill allocation requests.
117 // Generally, for request of X bytes, allocator can reserve and add to free
118 // lists a large number of chunks of size X to use them for future requests.
119 // All these chunks count toward the heap size. Currently, allocator never
120 // releases memory to OS (instead, it just puts freed chunks to free lists).
121 size_t __asan_get_heap_size();
122 // Number of bytes, mmaped by asan allocator, which can be used to fulfill
123 // allocation requests. When a user program frees memory chunk, it can first
124 // fall into quarantine and will count toward __asan_get_free_bytes() later.
125 size_t __asan_get_free_bytes();
126 // Number of bytes in unmapped pages, that are released to OS. Currently,
127 // always returns 0.
128 size_t __asan_get_unmapped_bytes();
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000129 // Prints accumulated stats to stderr. Used for debugging.
130 void __asan_print_accumulated_stats();
131} // namespace
132
133#endif // ASAN_INTERFACE_H