blob: 553d506620baece1376d5668a3ef8cdcdb23f2cc [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.
Evgeniy Stepanovc85f91f2012-02-14 13:46:06 +000072 void __asan_poison_memory_region(void const volatile *addr, size_t size)
73 ASAN_INTERFACE_FUNCTION_ATTRIBUTE;
Kostya Serebryany1e172b42011-11-30 01:07:02 +000074 // Marks memory region [addr, addr+size) as addressable.
75 // This memory must be previously allocated by the user program. Accessing
76 // addresses in this region is allowed until this region is poisoned again.
77 // This function may unpoison a superregion of [addr, addr+size) due to
78 // ASan alignment restrictions.
79 // Method is NOT thread-safe in the sense that no two threads can
80 // (un)poison memory in the same memory region simultaneously.
Evgeniy Stepanovc85f91f2012-02-14 13:46:06 +000081 void __asan_unpoison_memory_region(void const volatile *addr, size_t size)
82 ASAN_INTERFACE_FUNCTION_ATTRIBUTE;
Kostya Serebryany1e172b42011-11-30 01:07:02 +000083
Kostya Serebryanyf54b1f92012-02-08 21:33:27 +000084 // Performs cleanup before a NoReturn function. Must be called before things
85 // like _exit and execl to avoid false positives on stack.
Kostya Serebryanye1fe0fd2012-02-13 21:24:29 +000086 void __asan_handle_no_return() ASAN_INTERFACE_FUNCTION_ATTRIBUTE;
Kostya Serebryanyf54b1f92012-02-08 21:33:27 +000087
Kostya Serebryany1e172b42011-11-30 01:07:02 +000088// User code should use macro instead of functions.
Kostya Serebryany13ebae62011-12-27 21:57:12 +000089#if defined(__has_feature) && __has_feature(address_sanitizer)
Kostya Serebryany1e172b42011-11-30 01:07:02 +000090#define ASAN_POISON_MEMORY_REGION(addr, size) \
91 __asan_poison_memory_region((addr), (size))
92#define ASAN_UNPOISON_MEMORY_REGION(addr, size) \
93 __asan_unpoison_memory_region((addr), (size))
94#else
95#define ASAN_POISON_MEMORY_REGION(addr, size) \
96 ((void)(addr), (void)(size))
97#define ASAN_UNPOISON_MEMORY_REGION(addr, size) \
98 ((void)(addr), (void)(size))
99#endif
100
101 // Returns true iff addr is poisoned (i.e. 1-byte read/write access to this
102 // address will result in error report from AddressSanitizer).
Evgeniy Stepanovc85f91f2012-02-14 13:46:06 +0000103 bool __asan_address_is_poisoned(void const volatile *addr)
104 ASAN_INTERFACE_FUNCTION_ATTRIBUTE;
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000105
106 // This is an internal function that is called to report an error.
107 // However it is still a part of the interface because users may want to
108 // set a breakpoint on this function in a debugger.
109 void __asan_report_error(uintptr_t pc, uintptr_t bp, uintptr_t sp,
110 uintptr_t addr, bool is_write, size_t access_size)
Kostya Serebryany669f5432012-01-31 18:13:50 +0000111 ASAN_INTERFACE_FUNCTION_ATTRIBUTE;
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000112
113 // Sets the exit code to use when reporting an error.
114 // Returns the old value.
Kostya Serebryanye1fe0fd2012-02-13 21:24:29 +0000115 int __asan_set_error_exit_code(int exit_code)
116 ASAN_INTERFACE_FUNCTION_ATTRIBUTE;
117
118 // Sets the callback to be called right before death on error.
119 // Passing NULL will unset the callback.
120 void __asan_set_death_callback(void (*callback)(void))
121 ASAN_INTERFACE_FUNCTION_ATTRIBUTE;
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000122
123 // Returns the estimated number of bytes that will be reserved by allocator
124 // for request of "size" bytes. If ASan allocator can't allocate that much
125 // memory, returns the maximal possible allocation size, otherwise returns
126 // "size".
Evgeniy Stepanovc85f91f2012-02-14 13:46:06 +0000127 size_t __asan_get_estimated_allocated_size(size_t size)
128 ASAN_INTERFACE_FUNCTION_ATTRIBUTE;
Alexey Samsonovca2278d2012-01-18 15:26:55 +0000129 // Returns true if p was returned by the ASan allocator and
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000130 // is not yet freed.
Evgeniy Stepanovc85f91f2012-02-14 13:46:06 +0000131 bool __asan_get_ownership(const void *p)
132 ASAN_INTERFACE_FUNCTION_ATTRIBUTE;
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000133 // Returns the number of bytes reserved for the pointer p.
Alexey Samsonovca2278d2012-01-18 15:26:55 +0000134 // Requires (get_ownership(p) == true) or (p == NULL).
Evgeniy Stepanovc85f91f2012-02-14 13:46:06 +0000135 size_t __asan_get_allocated_size(const void *p)
136 ASAN_INTERFACE_FUNCTION_ATTRIBUTE;
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000137 // Number of bytes, allocated and not yet freed by the application.
Evgeniy Stepanovc85f91f2012-02-14 13:46:06 +0000138 size_t __asan_get_current_allocated_bytes()
139 ASAN_INTERFACE_FUNCTION_ATTRIBUTE;
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000140 // Number of bytes, mmaped by asan allocator to fulfill allocation requests.
141 // Generally, for request of X bytes, allocator can reserve and add to free
142 // lists a large number of chunks of size X to use them for future requests.
143 // All these chunks count toward the heap size. Currently, allocator never
144 // releases memory to OS (instead, it just puts freed chunks to free lists).
Evgeniy Stepanovc85f91f2012-02-14 13:46:06 +0000145 size_t __asan_get_heap_size()
146 ASAN_INTERFACE_FUNCTION_ATTRIBUTE;
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000147 // Number of bytes, mmaped by asan allocator, which can be used to fulfill
148 // allocation requests. When a user program frees memory chunk, it can first
149 // fall into quarantine and will count toward __asan_get_free_bytes() later.
Evgeniy Stepanovc85f91f2012-02-14 13:46:06 +0000150 size_t __asan_get_free_bytes()
151 ASAN_INTERFACE_FUNCTION_ATTRIBUTE;
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000152 // Number of bytes in unmapped pages, that are released to OS. Currently,
153 // always returns 0.
Evgeniy Stepanovc85f91f2012-02-14 13:46:06 +0000154 size_t __asan_get_unmapped_bytes()
155 ASAN_INTERFACE_FUNCTION_ATTRIBUTE;
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000156 // Prints accumulated stats to stderr. Used for debugging.
Evgeniy Stepanovc85f91f2012-02-14 13:46:06 +0000157 void __asan_print_accumulated_stats()
158 ASAN_INTERFACE_FUNCTION_ATTRIBUTE;
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000159} // namespace
160
Kostya Serebryany669f5432012-01-31 18:13:50 +0000161#undef ASAN_INTERFACE_FUNCTION_ATTRIBUTE
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000162#endif // ASAN_INTERFACE_H