blob: 63af84fc3ff16b843e35aabd8210001ce2220d1b [file] [log] [blame]
Evgeniy Stepanov5cb0ca82012-12-11 12:44:43 +00001//===-- msan_interface.h --------------------------------------------------===//
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 MemorySanitizer.
11//
12// Public interface header.
13//===----------------------------------------------------------------------===//
14#ifndef MSAN_INTERFACE_H
15#define MSAN_INTERFACE_H
16
17#include <sanitizer/common_interface_defs.h>
18
Evgeniy Stepanov5cb0ca82012-12-11 12:44:43 +000019#ifdef __cplusplus
20extern "C" {
21#endif
22
Evgeniy Stepanoveb4690e2013-01-30 09:56:11 +000023#if __has_feature(memory_sanitizer)
Evgeniy Stepanov12c46932013-01-29 14:33:29 +000024 /* Returns a string describing a stack origin.
25 Return NULL if the origin is invalid, or is not a stack origin. */
Evgeniy Stepanov250f2212013-01-30 13:12:08 +000026 const char *__msan_get_origin_descr_if_stack(uint32_t id);
Evgeniy Stepanov5cb0ca82012-12-11 12:44:43 +000027
Evgeniy Stepanov5cb0ca82012-12-11 12:44:43 +000028
Evgeniy Stepanov12c46932013-01-29 14:33:29 +000029 /* Set raw origin for the memory range. */
Evgeniy Stepanov1e7a3d72013-09-10 11:54:51 +000030 void __msan_set_origin(const volatile void *a, size_t size, uint32_t origin);
Evgeniy Stepanov5cb0ca82012-12-11 12:44:43 +000031
Evgeniy Stepanov12c46932013-01-29 14:33:29 +000032 /* Get raw origin for an address. */
Evgeniy Stepanov1e7a3d72013-09-10 11:54:51 +000033 uint32_t __msan_get_origin(const volatile void *a);
Evgeniy Stepanov5cb0ca82012-12-11 12:44:43 +000034
Evgeniy Stepanov12c46932013-01-29 14:33:29 +000035 /* Returns non-zero if tracking origins. */
Evgeniy Stepanov12c46932013-01-29 14:33:29 +000036 int __msan_get_track_origins();
Evgeniy Stepanov5cb0ca82012-12-11 12:44:43 +000037
Evgeniy Stepanov12c46932013-01-29 14:33:29 +000038 /* Returns the origin id of the latest UMR in the calling thread. */
Evgeniy Stepanov250f2212013-01-30 13:12:08 +000039 uint32_t __msan_get_umr_origin();
Evgeniy Stepanov5cb0ca82012-12-11 12:44:43 +000040
Evgeniy Stepanov12c46932013-01-29 14:33:29 +000041 /* Make memory region fully initialized (without changing its contents). */
Evgeniy Stepanov1e7a3d72013-09-10 11:54:51 +000042 void __msan_unpoison(const volatile void *a, size_t size);
Evgeniy Stepanov5cb0ca82012-12-11 12:44:43 +000043
Evgeniy Stepanov12c46932013-01-29 14:33:29 +000044 /* Make memory region fully uninitialized (without changing its contents). */
Evgeniy Stepanov1e7a3d72013-09-10 11:54:51 +000045 void __msan_poison(const volatile void *a, size_t size);
Evgeniy Stepanov5cb0ca82012-12-11 12:44:43 +000046
Evgeniy Stepanov12c46932013-01-29 14:33:29 +000047 /* Make memory region partially uninitialized (without changing its contents).
48 */
Evgeniy Stepanov1e7a3d72013-09-10 11:54:51 +000049 void __msan_partial_poison(const volatile void *data, void *shadow,
50 size_t size);
Evgeniy Stepanov5cb0ca82012-12-11 12:44:43 +000051
Evgeniy Stepanov12c46932013-01-29 14:33:29 +000052 /* Returns the offset of the first (at least partially) poisoned byte in the
53 memory range, or -1 if the whole range is good. */
Evgeniy Stepanov1e7a3d72013-09-10 11:54:51 +000054 intptr_t __msan_test_shadow(const volatile void *x, size_t size);
Evgeniy Stepanov5cb0ca82012-12-11 12:44:43 +000055
Evgeniy Stepanov12c46932013-01-29 14:33:29 +000056 /* Set exit code when error(s) were detected.
57 Value of 0 means don't change the program exit code. */
Evgeniy Stepanov12c46932013-01-29 14:33:29 +000058 void __msan_set_exit_code(int exit_code);
Evgeniy Stepanov5cb0ca82012-12-11 12:44:43 +000059
Evgeniy Stepanov12c46932013-01-29 14:33:29 +000060 /* For testing:
61 __msan_set_expect_umr(1);
62 ... some buggy code ...
63 __msan_set_expect_umr(0);
64 The last line will verify that a UMR happened. */
Evgeniy Stepanov12c46932013-01-29 14:33:29 +000065 void __msan_set_expect_umr(int expect_umr);
Evgeniy Stepanov5cb0ca82012-12-11 12:44:43 +000066
Evgeniy Stepanovbb881c72013-06-21 12:37:58 +000067 /* Change the value of keep_going flag. Non-zero value means don't terminate
68 program execution when an error is detected. This will not affect error in
69 modules that were compiled without the corresponding compiler flag. */
70 void __msan_set_keep_going(int keep_going);
71
Evgeniy Stepanov12c46932013-01-29 14:33:29 +000072 /* Print shadow and origin for the memory range to stdout in a human-readable
73 format. */
Evgeniy Stepanov1e7a3d72013-09-10 11:54:51 +000074 void __msan_print_shadow(const volatile void *x, size_t size);
Evgeniy Stepanov5cb0ca82012-12-11 12:44:43 +000075
Evgeniy Stepanov12c46932013-01-29 14:33:29 +000076 /* Print current function arguments shadow and origin to stdout in a
77 human-readable format. */
Evgeniy Stepanov12c46932013-01-29 14:33:29 +000078 void __msan_print_param_shadow();
Evgeniy Stepanov5cb0ca82012-12-11 12:44:43 +000079
Evgeniy Stepanov12c46932013-01-29 14:33:29 +000080 /* Returns true if running under a dynamic tool (DynamoRio-based). */
Evgeniy Stepanov12c46932013-01-29 14:33:29 +000081 int __msan_has_dynamic_component();
82
83 /* Tell MSan about newly allocated memory (ex.: custom allocator).
84 Memory will be marked uninitialized, with origin at the call site. */
Evgeniy Stepanov1e7a3d72013-09-10 11:54:51 +000085 void __msan_allocated_memory(const volatile void* data, size_t size);
Evgeniy Stepanov12c46932013-01-29 14:33:29 +000086
Evgeniy Stepanov5c48a8c2013-08-02 14:26:58 +000087 /* This function may be optionally provided by user and should return
88 a string containing Msan runtime options. See msan_flags.h for details. */
89 const char* __msan_default_options();
90
91
92 /***********************************/
93 /* Allocator statistics interface. */
94
95 /* Returns the estimated number of bytes that will be reserved by allocator
96 for request of "size" bytes. If Msan allocator can't allocate that much
97 memory, returns the maximal possible allocation size, otherwise returns
98 "size". */
99 size_t __msan_get_estimated_allocated_size(size_t size);
100
101 /* Returns true if p was returned by the Msan allocator and
102 is not yet freed. */
Evgeniy Stepanov1e7a3d72013-09-10 11:54:51 +0000103 int __msan_get_ownership(const volatile void *p);
Evgeniy Stepanov5c48a8c2013-08-02 14:26:58 +0000104
105 /* Returns the number of bytes reserved for the pointer p.
106 Requires (get_ownership(p) == true) or (p == 0). */
Evgeniy Stepanov1e7a3d72013-09-10 11:54:51 +0000107 size_t __msan_get_allocated_size(const volatile void *p);
Evgeniy Stepanov5c48a8c2013-08-02 14:26:58 +0000108
109 /* Number of bytes, allocated and not yet freed by the application. */
110 size_t __msan_get_current_allocated_bytes();
111
112 /* Number of bytes, mmaped by msan allocator to fulfill allocation requests.
113 Generally, for request of X bytes, allocator can reserve and add to free
114 lists a large number of chunks of size X to use them for future requests.
115 All these chunks count toward the heap size. Currently, allocator never
116 releases memory to OS (instead, it just puts freed chunks to free
117 lists). */
118 size_t __msan_get_heap_size();
119
120 /* Number of bytes, mmaped by msan allocator, which can be used to fulfill
121 allocation requests. When a user program frees memory chunk, it can first
122 fall into quarantine and will count toward __msan_get_free_bytes()
123 later. */
124 size_t __msan_get_free_bytes();
125
126 /* Number of bytes in unmapped pages, that are released to OS. Currently,
127 always returns 0. */
128 size_t __msan_get_unmapped_bytes();
129
130 /* Malloc hooks that may be optionally provided by user.
131 __msan_malloc_hook(ptr, size) is called immediately after
132 allocation of "size" bytes, which returned "ptr".
133 __msan_free_hook(ptr) is called immediately before
134 deallocation of "ptr". */
Evgeniy Stepanov1e7a3d72013-09-10 11:54:51 +0000135 void __msan_malloc_hook(const volatile void *ptr, size_t size);
136 void __msan_free_hook(const volatile void *ptr);
Evgeniy Stepanov5c48a8c2013-08-02 14:26:58 +0000137
Evgeniy Stepanov12c46932013-01-29 14:33:29 +0000138#else // __has_feature(memory_sanitizer)
139
Evgeniy Stepanov250f2212013-01-30 13:12:08 +0000140#define __msan_get_origin_descr_if_stack(id) ((const char*)0)
141#define __msan_set_origin(a, size, origin)
142#define __msan_get_origin(a) ((uint32_t)-1)
Evgeniy Stepanov12c46932013-01-29 14:33:29 +0000143#define __msan_get_track_origins() (0)
Evgeniy Stepanov250f2212013-01-30 13:12:08 +0000144#define __msan_get_umr_origin() ((uint32_t)-1)
145#define __msan_unpoison(a, size)
146#define __msan_poison(a, size)
147#define __msan_partial_poison(data, shadow, size)
148#define __msan_test_shadow(x, size) ((intptr_t)-1)
149#define __msan_set_exit_code(exit_code)
150#define __msan_set_expect_umr(expect_umr)
151#define __msan_print_shadow(x, size)
Evgeniy Stepanov12c46932013-01-29 14:33:29 +0000152#define __msan_print_param_shadow()
153#define __msan_has_dynamic_component() (0)
154#define __msan_allocated_memory(data, size)
155
156#endif // __has_feature(memory_sanitizer)
Evgeniy Stepanov887a5fe2013-01-28 13:52:49 +0000157
Evgeniy Stepanov5cb0ca82012-12-11 12:44:43 +0000158#ifdef __cplusplus
159} // extern "C"
160#endif
161
162#endif