• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 //===-- 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 
19 #ifdef __cplusplus
20 extern "C" {
21 #endif
22 
23 #if __has_feature(memory_sanitizer)
24   /* Returns a string describing a stack origin.
25      Return NULL if the origin is invalid, or is not a stack origin. */
26   const char *__msan_get_origin_descr_if_stack(uint32_t id);
27 
28 
29   /* Set raw origin for the memory range. */
30   void __msan_set_origin(const void *a, size_t size, uint32_t origin);
31 
32   /* Get raw origin for an address. */
33   uint32_t __msan_get_origin(const void *a);
34 
35   /* Returns non-zero if tracking origins. */
36   int __msan_get_track_origins();
37 
38   /* Returns the origin id of the latest UMR in the calling thread. */
39   uint32_t __msan_get_umr_origin();
40 
41   /* Make memory region fully initialized (without changing its contents). */
42   void __msan_unpoison(const void *a, size_t size);
43 
44   /* Make memory region fully uninitialized (without changing its contents). */
45   void __msan_poison(const void *a, size_t size);
46 
47   /* Make memory region partially uninitialized (without changing its contents).
48    */
49   void __msan_partial_poison(const void* data, void* shadow, size_t size);
50 
51   /* Returns the offset of the first (at least partially) poisoned byte in the
52      memory range, or -1 if the whole range is good. */
53   intptr_t __msan_test_shadow(const void *x, size_t size);
54 
55   /* Set exit code when error(s) were detected.
56      Value of 0 means don't change the program exit code. */
57   void __msan_set_exit_code(int exit_code);
58 
59   /* For testing:
60      __msan_set_expect_umr(1);
61      ... some buggy code ...
62      __msan_set_expect_umr(0);
63      The last line will verify that a UMR happened. */
64   void __msan_set_expect_umr(int expect_umr);
65 
66   /* Change the value of keep_going flag. Non-zero value means don't terminate
67      program execution when an error is detected. This will not affect error in
68      modules that were compiled without the corresponding compiler flag. */
69   void __msan_set_keep_going(int keep_going);
70 
71   /* Print shadow and origin for the memory range to stdout in a human-readable
72      format. */
73   void __msan_print_shadow(const void *x, size_t size);
74 
75   /* Print current function arguments shadow and origin to stdout in a
76      human-readable format. */
77   void __msan_print_param_shadow();
78 
79   /* Returns true if running under a dynamic tool (DynamoRio-based). */
80   int  __msan_has_dynamic_component();
81 
82   /* Tell MSan about newly allocated memory (ex.: custom allocator).
83      Memory will be marked uninitialized, with origin at the call site. */
84   void __msan_allocated_memory(const void* data, size_t size);
85 
86   /* This function may be optionally provided by user and should return
87      a string containing Msan runtime options. See msan_flags.h for details. */
88   const char* __msan_default_options();
89 
90 
91   /***********************************/
92   /* Allocator statistics interface. */
93 
94   /* Returns the estimated number of bytes that will be reserved by allocator
95      for request of "size" bytes. If Msan allocator can't allocate that much
96      memory, returns the maximal possible allocation size, otherwise returns
97      "size". */
98   size_t __msan_get_estimated_allocated_size(size_t size);
99 
100   /* Returns true if p was returned by the Msan allocator and
101      is not yet freed. */
102   bool __msan_get_ownership(const void *p);
103 
104   /* Returns the number of bytes reserved for the pointer p.
105      Requires (get_ownership(p) == true) or (p == 0). */
106   size_t __msan_get_allocated_size(const void *p);
107 
108   /* Number of bytes, allocated and not yet freed by the application. */
109   size_t __msan_get_current_allocated_bytes();
110 
111   /* Number of bytes, mmaped by msan allocator to fulfill allocation requests.
112      Generally, for request of X bytes, allocator can reserve and add to free
113      lists a large number of chunks of size X to use them for future requests.
114      All these chunks count toward the heap size. Currently, allocator never
115      releases memory to OS (instead, it just puts freed chunks to free
116      lists). */
117   size_t __msan_get_heap_size();
118 
119   /* Number of bytes, mmaped by msan allocator, which can be used to fulfill
120      allocation requests. When a user program frees memory chunk, it can first
121      fall into quarantine and will count toward __msan_get_free_bytes()
122      later. */
123   size_t __msan_get_free_bytes();
124 
125   /* Number of bytes in unmapped pages, that are released to OS. Currently,
126      always returns 0. */
127   size_t __msan_get_unmapped_bytes();
128 
129   /* Malloc hooks that may be optionally provided by user.
130      __msan_malloc_hook(ptr, size) is called immediately after
131        allocation of "size" bytes, which returned "ptr".
132      __msan_free_hook(ptr) is called immediately before
133        deallocation of "ptr". */
134   void __msan_malloc_hook(void *ptr, size_t size);
135   void __msan_free_hook(void *ptr);
136 
137 #else  // __has_feature(memory_sanitizer)
138 
139 #define __msan_get_origin_descr_if_stack(id) ((const char*)0)
140 #define __msan_set_origin(a, size, origin)
141 #define __msan_get_origin(a) ((uint32_t)-1)
142 #define __msan_get_track_origins() (0)
143 #define __msan_get_umr_origin() ((uint32_t)-1)
144 #define __msan_unpoison(a, size)
145 #define __msan_poison(a, size)
146 #define __msan_partial_poison(data, shadow, size)
147 #define __msan_test_shadow(x, size) ((intptr_t)-1)
148 #define __msan_set_exit_code(exit_code)
149 #define __msan_set_expect_umr(expect_umr)
150 #define __msan_print_shadow(x, size)
151 #define __msan_print_param_shadow()
152 #define __msan_has_dynamic_component() (0)
153 #define __msan_allocated_memory(data, size)
154 
155 #endif   // __has_feature(memory_sanitizer)
156 
157 #ifdef __cplusplus
158 }  // extern "C"
159 #endif
160 
161 #endif
162