blob: 7f4fdbe04e6301433db7a7c2c670a4657ca612aa [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
Alexander Potapenko6f045292012-01-27 15:15:04 +000018#if !defined(_WIN32)
Kostya Serebryany669f5432012-01-31 18:13:50 +000019#include <stdint.h> // for uintptr_t
20#define ASAN_INTERFACE_FUNCTION_ATTRIBUTE __attribute__((visibility("default")))
Alexander Potapenko6f045292012-01-27 15:15:04 +000021#else
Kostya Serebryany669f5432012-01-31 18:13:50 +000022// TODO(timurrrr): find out what we need on Windows. __declspec(dllexport) ?
23#define ASAN_INTERFACE_FUNCTION_ATTRIBUTE
Alexander Potapenko6f045292012-01-27 15:15:04 +000024#endif
Kostya Serebryany1e172b42011-11-30 01:07:02 +000025#include <stdlib.h> // for size_t
26
27// This header should NOT include any other headers from ASan runtime.
28// All functions in this header are extern "C" and start with __asan_.
29
30extern "C" {
31 // This function should be called at the very beginning of the process,
32 // before any instrumented code is executed and before any call to malloc.
Kostya Serebryany669f5432012-01-31 18:13:50 +000033 void __asan_init() ASAN_INTERFACE_FUNCTION_ATTRIBUTE;
Kostya Serebryany1e172b42011-11-30 01:07:02 +000034
35 // This function should be called by the instrumented code.
36 // 'addr' is the address of a global variable called 'name' of 'size' bytes.
37 void __asan_register_global(uintptr_t addr, size_t size, const char *name)
Kostya Serebryany669f5432012-01-31 18:13:50 +000038 ASAN_INTERFACE_FUNCTION_ATTRIBUTE;
Kostya Serebryany1e172b42011-11-30 01:07:02 +000039
40 // This structure describes an instrumented global variable.
41 struct __asan_global {
42 size_t beg; // The address of the global.
43 size_t size; // The original size of the global.
44 size_t size_with_redzone; // The size with the redzone.
45 const char *name; // Name as a C string.
46 };
47
Kostya Serebryanyc4910612011-12-15 21:55:34 +000048 // These two functions should be called by the instrumented code.
49 // 'globals' is an array of structures describing 'n' globals.
Kostya Serebryany1e172b42011-11-30 01:07:02 +000050 void __asan_register_globals(__asan_global *globals, size_t n)
Kostya Serebryany669f5432012-01-31 18:13:50 +000051 ASAN_INTERFACE_FUNCTION_ATTRIBUTE;
Kostya Serebryanyc4910612011-12-15 21:55:34 +000052 void __asan_unregister_globals(__asan_global *globals, size_t n)
Kostya Serebryany669f5432012-01-31 18:13:50 +000053 ASAN_INTERFACE_FUNCTION_ATTRIBUTE;
Kostya Serebryany1e172b42011-11-30 01:07:02 +000054
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 size_t __asan_stack_malloc(size_t size, size_t real_stack)
Kostya Serebryany669f5432012-01-31 18:13:50 +000060 ASAN_INTERFACE_FUNCTION_ATTRIBUTE;
Kostya Serebryany1e172b42011-11-30 01:07:02 +000061 void __asan_stack_free(size_t ptr, size_t size, size_t real_stack)
Kostya Serebryany669f5432012-01-31 18:13:50 +000062 ASAN_INTERFACE_FUNCTION_ATTRIBUTE;
Kostya Serebryany1e172b42011-11-30 01:07:02 +000063
64 // Marks memory region [addr, addr+size) as unaddressable.
65 // This memory must be previously allocated by the user program. Accessing
66 // addresses in this region from instrumented code is forbidden until
67 // this region is unpoisoned. This function is not guaranteed to poison
68 // the whole region - it may poison only subregion of [addr, addr+size) due
69 // to ASan alignment restrictions.
70 // Method is NOT thread-safe in the sense that no two threads can
71 // (un)poison memory in the same memory region simultaneously.
72 void __asan_poison_memory_region(void const volatile *addr, size_t size);
73 // Marks memory region [addr, addr+size) as addressable.
74 // This memory must be previously allocated by the user program. Accessing
75 // addresses in this region is allowed until this region is poisoned again.
76 // This function may unpoison a superregion of [addr, addr+size) due to
77 // ASan alignment restrictions.
78 // Method is NOT thread-safe in the sense that no two threads can
79 // (un)poison memory in the same memory region simultaneously.
80 void __asan_unpoison_memory_region(void const volatile *addr, size_t size);
81
82// User code should use macro instead of functions.
Kostya Serebryany13ebae62011-12-27 21:57:12 +000083#if defined(__has_feature) && __has_feature(address_sanitizer)
Kostya Serebryany1e172b42011-11-30 01:07:02 +000084#define ASAN_POISON_MEMORY_REGION(addr, size) \
85 __asan_poison_memory_region((addr), (size))
86#define ASAN_UNPOISON_MEMORY_REGION(addr, size) \
87 __asan_unpoison_memory_region((addr), (size))
88#else
89#define ASAN_POISON_MEMORY_REGION(addr, size) \
90 ((void)(addr), (void)(size))
91#define ASAN_UNPOISON_MEMORY_REGION(addr, size) \
92 ((void)(addr), (void)(size))
93#endif
94
95 // Returns true iff addr is poisoned (i.e. 1-byte read/write access to this
96 // address will result in error report from AddressSanitizer).
97 bool __asan_address_is_poisoned(void const volatile *addr);
98
99 // This is an internal function that is called to report an error.
100 // However it is still a part of the interface because users may want to
101 // set a breakpoint on this function in a debugger.
102 void __asan_report_error(uintptr_t pc, uintptr_t bp, uintptr_t sp,
103 uintptr_t addr, bool is_write, size_t access_size)
Kostya Serebryany669f5432012-01-31 18:13:50 +0000104 ASAN_INTERFACE_FUNCTION_ATTRIBUTE;
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000105
106 // Sets the exit code to use when reporting an error.
107 // Returns the old value.
108 int __asan_set_error_exit_code(int exit_code);
109
110 // Returns the estimated number of bytes that will be reserved by allocator
111 // for request of "size" bytes. If ASan allocator can't allocate that much
112 // memory, returns the maximal possible allocation size, otherwise returns
113 // "size".
114 size_t __asan_get_estimated_allocated_size(size_t size);
Alexey Samsonovca2278d2012-01-18 15:26:55 +0000115 // Returns true if p was returned by the ASan allocator and
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000116 // is not yet freed.
117 bool __asan_get_ownership(const void *p);
118 // Returns the number of bytes reserved for the pointer p.
Alexey Samsonovca2278d2012-01-18 15:26:55 +0000119 // Requires (get_ownership(p) == true) or (p == NULL).
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000120 size_t __asan_get_allocated_size(const void *p);
121 // Number of bytes, allocated and not yet freed by the application.
122 size_t __asan_get_current_allocated_bytes();
123 // Number of bytes, mmaped by asan allocator to fulfill allocation requests.
124 // Generally, for request of X bytes, allocator can reserve and add to free
125 // lists a large number of chunks of size X to use them for future requests.
126 // All these chunks count toward the heap size. Currently, allocator never
127 // releases memory to OS (instead, it just puts freed chunks to free lists).
128 size_t __asan_get_heap_size();
129 // Number of bytes, mmaped by asan allocator, which can be used to fulfill
130 // allocation requests. When a user program frees memory chunk, it can first
131 // fall into quarantine and will count toward __asan_get_free_bytes() later.
132 size_t __asan_get_free_bytes();
133 // Number of bytes in unmapped pages, that are released to OS. Currently,
134 // always returns 0.
135 size_t __asan_get_unmapped_bytes();
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000136 // Prints accumulated stats to stderr. Used for debugging.
137 void __asan_print_accumulated_stats();
138} // namespace
139
Kostya Serebryany669f5432012-01-31 18:13:50 +0000140#undef ASAN_INTERFACE_FUNCTION_ATTRIBUTE
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000141#endif // ASAN_INTERFACE_H