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