blob: d678172bf776bc0c702c967374c385eca7f1de3a [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
Alexey Samsonovb823e3c2012-02-22 14:07:06 +000018// ----------- 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
Alexander Potapenko6f045292012-01-27 15:15:04 +000022#if !defined(_WIN32)
Kostya Serebryany669f5432012-01-31 18:13:50 +000023#include <stdint.h> // for uintptr_t
24#define ASAN_INTERFACE_FUNCTION_ATTRIBUTE __attribute__((visibility("default")))
Alexander Potapenko62f10e72012-05-28 16:21:19 +000025#define ASAN_WEAK_ATTRIBUTE __attribute__((weak));
Alexander Potapenko6f045292012-01-27 15:15:04 +000026#else
Kostya Serebryany669f5432012-01-31 18:13:50 +000027// TODO(timurrrr): find out what we need on Windows. __declspec(dllexport) ?
28#define ASAN_INTERFACE_FUNCTION_ATTRIBUTE
Alexander Potapenko62f10e72012-05-28 16:21:19 +000029#define ASAN_WEAK_ATTRIBUTE
Alexander Potapenko6f045292012-01-27 15:15:04 +000030#endif
Alexey Samsonovb823e3c2012-02-22 14:07:06 +000031#include <stddef.h> // for size_t
Kostya Serebryany1e172b42011-11-30 01:07:02 +000032
33extern "C" {
34 // This function should be called at the very beginning of the process,
35 // before any instrumented code is executed and before any call to malloc.
Kostya Serebryany669f5432012-01-31 18:13:50 +000036 void __asan_init() ASAN_INTERFACE_FUNCTION_ATTRIBUTE;
Kostya Serebryany1e172b42011-11-30 01:07:02 +000037
38 // This function should be called by the instrumented code.
39 // 'addr' is the address of a global variable called 'name' of 'size' bytes.
40 void __asan_register_global(uintptr_t addr, size_t size, const char *name)
Kostya Serebryany669f5432012-01-31 18:13:50 +000041 ASAN_INTERFACE_FUNCTION_ATTRIBUTE;
Kostya Serebryany1e172b42011-11-30 01:07:02 +000042
43 // This structure describes an instrumented global variable.
44 struct __asan_global {
45 size_t beg; // The address of the global.
46 size_t size; // The original size of the global.
47 size_t size_with_redzone; // The size with the redzone.
48 const char *name; // Name as a C string.
49 };
50
Kostya Serebryanyc4910612011-12-15 21:55:34 +000051 // These two functions should be called by the instrumented code.
52 // 'globals' is an array of structures describing 'n' globals.
Kostya Serebryany1e172b42011-11-30 01:07:02 +000053 void __asan_register_globals(__asan_global *globals, size_t n)
Kostya Serebryany669f5432012-01-31 18:13:50 +000054 ASAN_INTERFACE_FUNCTION_ATTRIBUTE;
Kostya Serebryanyc4910612011-12-15 21:55:34 +000055 void __asan_unregister_globals(__asan_global *globals, size_t n)
Kostya Serebryany669f5432012-01-31 18:13:50 +000056 ASAN_INTERFACE_FUNCTION_ATTRIBUTE;
Kostya Serebryany1e172b42011-11-30 01:07:02 +000057
58 // These two functions are used by the instrumented code in the
59 // use-after-return mode. __asan_stack_malloc allocates size bytes of
60 // fake stack and __asan_stack_free poisons it. real_stack is a pointer to
61 // the real stack region.
62 size_t __asan_stack_malloc(size_t size, size_t real_stack)
Kostya Serebryany669f5432012-01-31 18:13:50 +000063 ASAN_INTERFACE_FUNCTION_ATTRIBUTE;
Kostya Serebryany1e172b42011-11-30 01:07:02 +000064 void __asan_stack_free(size_t ptr, size_t size, size_t real_stack)
Kostya Serebryany669f5432012-01-31 18:13:50 +000065 ASAN_INTERFACE_FUNCTION_ATTRIBUTE;
Kostya Serebryany1e172b42011-11-30 01:07:02 +000066
67 // Marks memory region [addr, addr+size) as unaddressable.
68 // This memory must be previously allocated by the user program. Accessing
69 // addresses in this region from instrumented code is forbidden until
70 // this region is unpoisoned. This function is not guaranteed to poison
71 // the whole region - it may poison only subregion of [addr, addr+size) due
72 // to 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.
Evgeniy Stepanovc85f91f2012-02-14 13:46:06 +000075 void __asan_poison_memory_region(void const volatile *addr, size_t size)
76 ASAN_INTERFACE_FUNCTION_ATTRIBUTE;
Kostya Serebryany1e172b42011-11-30 01:07:02 +000077 // Marks memory region [addr, addr+size) as addressable.
78 // This memory must be previously allocated by the user program. Accessing
79 // addresses in this region is allowed until this region is poisoned again.
80 // This function may unpoison a superregion of [addr, addr+size) due to
81 // ASan alignment restrictions.
82 // Method is NOT thread-safe in the sense that no two threads can
83 // (un)poison memory in the same memory region simultaneously.
Evgeniy Stepanovc85f91f2012-02-14 13:46:06 +000084 void __asan_unpoison_memory_region(void const volatile *addr, size_t size)
85 ASAN_INTERFACE_FUNCTION_ATTRIBUTE;
Kostya Serebryany1e172b42011-11-30 01:07:02 +000086
Kostya Serebryanyf54b1f92012-02-08 21:33:27 +000087 // Performs cleanup before a NoReturn function. Must be called before things
88 // like _exit and execl to avoid false positives on stack.
Kostya Serebryanye1fe0fd2012-02-13 21:24:29 +000089 void __asan_handle_no_return() ASAN_INTERFACE_FUNCTION_ATTRIBUTE;
Kostya Serebryanyf54b1f92012-02-08 21:33:27 +000090
Kostya Serebryany1e172b42011-11-30 01:07:02 +000091// User code should use macro instead of functions.
Kostya Serebryany850a49e2012-04-06 20:36:18 +000092#if !defined(__has_feature)
93#define __has_feature(x) 0
94#endif
95#if __has_feature(address_sanitizer)
Kostya Serebryany1e172b42011-11-30 01:07:02 +000096#define ASAN_POISON_MEMORY_REGION(addr, size) \
97 __asan_poison_memory_region((addr), (size))
98#define ASAN_UNPOISON_MEMORY_REGION(addr, size) \
99 __asan_unpoison_memory_region((addr), (size))
100#else
101#define ASAN_POISON_MEMORY_REGION(addr, size) \
102 ((void)(addr), (void)(size))
103#define ASAN_UNPOISON_MEMORY_REGION(addr, size) \
104 ((void)(addr), (void)(size))
105#endif
106
107 // Returns true iff addr is poisoned (i.e. 1-byte read/write access to this
108 // address will result in error report from AddressSanitizer).
Evgeniy Stepanovc85f91f2012-02-14 13:46:06 +0000109 bool __asan_address_is_poisoned(void const volatile *addr)
110 ASAN_INTERFACE_FUNCTION_ATTRIBUTE;
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000111
112 // This is an internal function that is called to report an error.
113 // However it is still a part of the interface because users may want to
114 // set a breakpoint on this function in a debugger.
115 void __asan_report_error(uintptr_t pc, uintptr_t bp, uintptr_t sp,
116 uintptr_t addr, bool is_write, size_t access_size)
Kostya Serebryany669f5432012-01-31 18:13:50 +0000117 ASAN_INTERFACE_FUNCTION_ATTRIBUTE;
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000118
119 // Sets the exit code to use when reporting an error.
120 // Returns the old value.
Kostya Serebryanye1fe0fd2012-02-13 21:24:29 +0000121 int __asan_set_error_exit_code(int exit_code)
122 ASAN_INTERFACE_FUNCTION_ATTRIBUTE;
123
124 // Sets the callback to be called right before death on error.
125 // Passing NULL will unset the callback.
126 void __asan_set_death_callback(void (*callback)(void))
127 ASAN_INTERFACE_FUNCTION_ATTRIBUTE;
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000128
Alexander Potapenko3fe91352012-02-27 14:06:48 +0000129 void __asan_set_error_report_callback(void (*callback)(const char*))
130 ASAN_INTERFACE_FUNCTION_ATTRIBUTE;
131
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000132 // Returns the estimated number of bytes that will be reserved by allocator
133 // for request of "size" bytes. If ASan allocator can't allocate that much
134 // memory, returns the maximal possible allocation size, otherwise returns
135 // "size".
Evgeniy Stepanovc85f91f2012-02-14 13:46:06 +0000136 size_t __asan_get_estimated_allocated_size(size_t size)
137 ASAN_INTERFACE_FUNCTION_ATTRIBUTE;
Alexey Samsonovca2278d2012-01-18 15:26:55 +0000138 // Returns true if p was returned by the ASan allocator and
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000139 // is not yet freed.
Evgeniy Stepanovc85f91f2012-02-14 13:46:06 +0000140 bool __asan_get_ownership(const void *p)
141 ASAN_INTERFACE_FUNCTION_ATTRIBUTE;
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000142 // Returns the number of bytes reserved for the pointer p.
Alexey Samsonovca2278d2012-01-18 15:26:55 +0000143 // Requires (get_ownership(p) == true) or (p == NULL).
Evgeniy Stepanovc85f91f2012-02-14 13:46:06 +0000144 size_t __asan_get_allocated_size(const void *p)
145 ASAN_INTERFACE_FUNCTION_ATTRIBUTE;
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000146 // Number of bytes, allocated and not yet freed by the application.
Evgeniy Stepanovc85f91f2012-02-14 13:46:06 +0000147 size_t __asan_get_current_allocated_bytes()
148 ASAN_INTERFACE_FUNCTION_ATTRIBUTE;
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000149 // Number of bytes, mmaped by asan allocator to fulfill allocation requests.
150 // Generally, for request of X bytes, allocator can reserve and add to free
151 // lists a large number of chunks of size X to use them for future requests.
152 // All these chunks count toward the heap size. Currently, allocator never
153 // releases memory to OS (instead, it just puts freed chunks to free lists).
Evgeniy Stepanovc85f91f2012-02-14 13:46:06 +0000154 size_t __asan_get_heap_size()
155 ASAN_INTERFACE_FUNCTION_ATTRIBUTE;
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000156 // Number of bytes, mmaped by asan allocator, which can be used to fulfill
157 // allocation requests. When a user program frees memory chunk, it can first
158 // fall into quarantine and will count toward __asan_get_free_bytes() later.
Evgeniy Stepanovc85f91f2012-02-14 13:46:06 +0000159 size_t __asan_get_free_bytes()
160 ASAN_INTERFACE_FUNCTION_ATTRIBUTE;
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000161 // Number of bytes in unmapped pages, that are released to OS. Currently,
162 // always returns 0.
Evgeniy Stepanovc85f91f2012-02-14 13:46:06 +0000163 size_t __asan_get_unmapped_bytes()
164 ASAN_INTERFACE_FUNCTION_ATTRIBUTE;
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000165 // Prints accumulated stats to stderr. Used for debugging.
Evgeniy Stepanovc85f91f2012-02-14 13:46:06 +0000166 void __asan_print_accumulated_stats()
167 ASAN_INTERFACE_FUNCTION_ATTRIBUTE;
Alexander Potapenko62f10e72012-05-28 16:21:19 +0000168 char *__asan_default_options
169 ASAN_WEAK_ATTRIBUTE;
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000170} // namespace
171
Kostya Serebryany669f5432012-01-31 18:13:50 +0000172#undef ASAN_INTERFACE_FUNCTION_ATTRIBUTE
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000173#endif // ASAN_INTERFACE_H