Run DCE after a LoopFlatten test to reduce spurious output [nfc]
[llvm-project.git] / compiler-rt / include / sanitizer / common_interface_defs.h
blob0bbade454244a0c769e1c7decf216fb25eb3173b
1 //===-- sanitizer/common_interface_defs.h -----------------------*- C++ -*-===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // Common part of the public sanitizer interface.
10 //===----------------------------------------------------------------------===//
12 #ifndef SANITIZER_COMMON_INTERFACE_DEFS_H
13 #define SANITIZER_COMMON_INTERFACE_DEFS_H
15 #include <stddef.h>
16 #include <stdint.h>
18 // GCC does not understand __has_feature.
19 #if !defined(__has_feature)
20 #define __has_feature(x) 0
21 #endif
23 // Windows allows a user to set their default calling convention, but we always
24 // use __cdecl
25 #ifdef _WIN32
26 #define SANITIZER_CDECL __cdecl
27 #else
28 #define SANITIZER_CDECL
29 #endif
31 #ifdef __cplusplus
32 extern "C" {
33 #endif
34 // Arguments for __sanitizer_sandbox_on_notify() below.
35 typedef struct {
36 // Enable sandbox support in sanitizer coverage.
37 int coverage_sandboxed;
38 // File descriptor to write coverage data to. If -1 is passed, a file will
39 // be pre-opened by __sanitizer_sandbox_on_notify(). This field has no
40 // effect if coverage_sandboxed == 0.
41 intptr_t coverage_fd;
42 // If non-zero, split the coverage data into well-formed blocks. This is
43 // useful when coverage_fd is a socket descriptor. Each block will contain
44 // a header, allowing data from multiple processes to be sent over the same
45 // socket.
46 unsigned int coverage_max_block_size;
47 } __sanitizer_sandbox_arguments;
49 // Tell the tools to write their reports to "path.<pid>" instead of stderr.
50 void SANITIZER_CDECL __sanitizer_set_report_path(const char *path);
51 // Tell the tools to write their reports to the provided file descriptor
52 // (casted to void *).
53 void SANITIZER_CDECL __sanitizer_set_report_fd(void *fd);
54 // Get the current full report file path, if a path was specified by
55 // an earlier call to __sanitizer_set_report_path. Returns null otherwise.
56 const char *SANITIZER_CDECL __sanitizer_get_report_path();
58 // Notify the tools that the sandbox is going to be turned on. The reserved
59 // parameter will be used in the future to hold a structure with functions
60 // that the tools may call to bypass the sandbox.
61 void SANITIZER_CDECL
62 __sanitizer_sandbox_on_notify(__sanitizer_sandbox_arguments *args);
64 // This function is called by the tool when it has just finished reporting
65 // an error. 'error_summary' is a one-line string that summarizes
66 // the error message. This function can be overridden by the client.
67 void SANITIZER_CDECL
68 __sanitizer_report_error_summary(const char *error_summary);
70 // Some of the sanitizers (for example ASan/TSan) could miss bugs that happen
71 // in unaligned loads/stores. To find such bugs reliably, you need to replace
72 // plain unaligned loads/stores with these calls.
74 /// Loads a 16-bit unaligned value.
76 /// \param p Pointer to unaligned memory.
77 ///
78 /// \returns Loaded value.
79 uint16_t SANITIZER_CDECL __sanitizer_unaligned_load16(const void *p);
81 /// Loads a 32-bit unaligned value.
82 ///
83 /// \param p Pointer to unaligned memory.
84 ///
85 /// \returns Loaded value.
86 uint32_t SANITIZER_CDECL __sanitizer_unaligned_load32(const void *p);
88 /// Loads a 64-bit unaligned value.
89 ///
90 /// \param p Pointer to unaligned memory.
91 ///
92 /// \returns Loaded value.
93 uint64_t SANITIZER_CDECL __sanitizer_unaligned_load64(const void *p);
95 /// Stores a 16-bit unaligned value.
96 ///
97 /// \param p Pointer to unaligned memory.
98 /// \param x 16-bit value to store.
99 void SANITIZER_CDECL __sanitizer_unaligned_store16(void *p, uint16_t x);
101 /// Stores a 32-bit unaligned value.
103 /// \param p Pointer to unaligned memory.
104 /// \param x 32-bit value to store.
105 void SANITIZER_CDECL __sanitizer_unaligned_store32(void *p, uint32_t x);
107 /// Stores a 64-bit unaligned value.
109 /// \param p Pointer to unaligned memory.
110 /// \param x 64-bit value to store.
111 void SANITIZER_CDECL __sanitizer_unaligned_store64(void *p, uint64_t x);
113 // Returns 1 on the first call, then returns 0 thereafter. Called by the tool
114 // to ensure only one report is printed when multiple errors occur
115 // simultaneously.
116 int SANITIZER_CDECL __sanitizer_acquire_crash_state();
118 /// Annotates the current state of a contiguous container, such as
119 /// <c>std::vector</c>, <c>std::string</c>, or similar.
121 /// A contiguous container is a container that keeps all of its elements
122 /// in a contiguous region of memory. The container owns the region of memory
123 /// <c>[beg, end)</c>; the memory <c>[beg, mid)</c> is used to store the
124 /// current elements, and the memory <c>[mid, end)</c> is reserved for future
125 /// elements (<c>beg <= mid <= end</c>). For example, in
126 /// <c>std::vector<> v</c>:
128 /// \code
129 /// beg = &v[0];
130 /// end = beg + v.capacity() * sizeof(v[0]);
131 /// mid = beg + v.size() * sizeof(v[0]);
132 /// \endcode
134 /// This annotation tells the Sanitizer tool about the current state of the
135 /// container so that the tool can report errors when memory from
136 /// <c>[mid, end)</c> is accessed. Insert this annotation into methods like
137 /// <c>push_back()</c> or <c>pop_back()</c>. Supply the old and new values of
138 /// <c>mid</c>(<c><i>old_mid</i></c> and <c><i>new_mid</i></c>). In the initial
139 /// state <c>mid == end</c>, so that should be the final state when the
140 /// container is destroyed or when the container reallocates the storage.
142 /// For ASan, <c><i>beg</i></c> no longer needs to be 8-aligned,
143 /// first and last granule may be shared with other objects
144 /// and therefore the function can be used for any allocator.
146 /// The following example shows how to use the function:
148 /// \code
149 /// int32_t x[3]; // 12 bytes
150 /// char *beg = (char*)&x[0];
151 /// char *end = beg + 12;
152 /// __sanitizer_annotate_contiguous_container(beg, end, beg, end);
153 /// \endcode
155 /// \note Use this function with caution and do not use for anything other
156 /// than vector-like classes.
157 /// \note Unaligned <c><i>beg</i></c> or <c><i>end</i></c> may miss bugs in
158 /// these granules.
160 /// \param beg Beginning of memory region.
161 /// \param end End of memory region.
162 /// \param old_mid Old middle of memory region.
163 /// \param new_mid New middle of memory region.
164 void SANITIZER_CDECL __sanitizer_annotate_contiguous_container(
165 const void *beg, const void *end, const void *old_mid, const void *new_mid);
167 /// Similar to <c>__sanitizer_annotate_contiguous_container</c>.
169 /// Annotates the current state of a contiguous container memory,
170 /// such as <c>std::deque</c>'s single chunk, when the boundries are moved.
172 /// A contiguous chunk is a chunk that keeps all of its elements
173 /// in a contiguous region of memory. The container owns the region of memory
174 /// <c>[storage_beg, storage_end)</c>; the memory <c>[container_beg,
175 /// container_end)</c> is used to store the current elements, and the memory
176 /// <c>[storage_beg, container_beg), [container_end, storage_end)</c> is
177 /// reserved for future elements (<c>storage_beg <= container_beg <=
178 /// container_end <= storage_end</c>). For example, in <c> std::deque </c>:
179 /// - chunk with a frist deques element will have container_beg equal to address
180 /// of the first element.
181 /// - in every next chunk with elements, true is <c> container_beg ==
182 /// storage_beg </c>.
184 /// Argument requirements:
185 /// During unpoisoning memory of empty container (before first element is
186 /// added):
187 /// - old_container_beg_p == old_container_end_p
188 /// During poisoning after last element was removed:
189 /// - new_container_beg_p == new_container_end_p
190 /// \param storage_beg Beginning of memory region.
191 /// \param storage_end End of memory region.
192 /// \param old_container_beg Old beginning of used region.
193 /// \param old_container_end End of used region.
194 /// \param new_container_beg New beginning of used region.
195 /// \param new_container_end New end of used region.
196 void SANITIZER_CDECL __sanitizer_annotate_double_ended_contiguous_container(
197 const void *storage_beg, const void *storage_end,
198 const void *old_container_beg, const void *old_container_end,
199 const void *new_container_beg, const void *new_container_end);
201 /// Returns true if the contiguous container <c>[beg, end)</c> is properly
202 /// poisoned.
204 /// Proper poisoning could occur, for example, with
205 /// <c>__sanitizer_annotate_contiguous_container</c>), that is, if
206 /// <c>[beg, mid)</c> is addressable and <c>[mid, end)</c> is unaddressable.
207 /// Full verification requires O (<c>end - beg</c>) time; this function tries
208 /// to avoid such complexity by touching only parts of the container around
209 /// <c><i>beg</i></c>, <c><i>mid</i></c>, and <c><i>end</i></c>.
211 /// \param beg Beginning of memory region.
212 /// \param mid Middle of memory region.
213 /// \param end Old end of memory region.
215 /// \returns True if the contiguous container <c>[beg, end)</c> is properly
216 /// poisoned.
217 int SANITIZER_CDECL __sanitizer_verify_contiguous_container(const void *beg,
218 const void *mid,
219 const void *end);
221 /// Returns true if the double ended contiguous
222 /// container <c>[storage_beg, storage_end)</c> is properly poisoned.
224 /// Proper poisoning could occur, for example, with
225 /// <c>__sanitizer_annotate_double_ended_contiguous_container</c>), that is, if
226 /// <c>[storage_beg, container_beg)</c> is not addressable, <c>[container_beg,
227 /// container_end)</c> is addressable and <c>[container_end, end)</c> is
228 /// unaddressable. Full verification requires O (<c>storage_end -
229 /// storage_beg</c>) time; this function tries to avoid such complexity by
230 /// touching only parts of the container around <c><i>storage_beg</i></c>,
231 /// <c><i>container_beg</i></c>, <c><i>container_end</i></c>, and
232 /// <c><i>storage_end</i></c>.
234 /// \param storage_beg Beginning of memory region.
235 /// \param container_beg Beginning of used region.
236 /// \param container_end End of used region.
237 /// \param storage_end End of memory region.
239 /// \returns True if the double-ended contiguous container <c>[storage_beg,
240 /// container_beg, container_end, end)</c> is properly poisoned - only
241 /// [container_beg; container_end) is addressable.
242 int SANITIZER_CDECL __sanitizer_verify_double_ended_contiguous_container(
243 const void *storage_beg, const void *container_beg,
244 const void *container_end, const void *storage_end);
246 /// Similar to <c>__sanitizer_verify_contiguous_container()</c> but also
247 /// returns the address of the first improperly poisoned byte.
249 /// Returns NULL if the area is poisoned properly.
251 /// \param beg Beginning of memory region.
252 /// \param mid Middle of memory region.
253 /// \param end Old end of memory region.
255 /// \returns The bad address or NULL.
256 const void *SANITIZER_CDECL __sanitizer_contiguous_container_find_bad_address(
257 const void *beg, const void *mid, const void *end);
259 /// returns the address of the first improperly poisoned byte.
261 /// Returns NULL if the area is poisoned properly.
263 /// \param storage_beg Beginning of memory region.
264 /// \param container_beg Beginning of used region.
265 /// \param container_end End of used region.
266 /// \param storage_end End of memory region.
268 /// \returns The bad address or NULL.
269 const void *SANITIZER_CDECL
270 __sanitizer_double_ended_contiguous_container_find_bad_address(
271 const void *storage_beg, const void *container_beg,
272 const void *container_end, const void *storage_end);
274 /// Prints the stack trace leading to this call (useful for calling from the
275 /// debugger).
276 void SANITIZER_CDECL __sanitizer_print_stack_trace(void);
278 // Symbolizes the supplied 'pc' using the format string 'fmt'.
279 // Outputs at most 'out_buf_size' bytes into 'out_buf'.
280 // If 'out_buf' is not empty then output is zero or more non empty C strings
281 // followed by single empty C string. Multiple strings can be returned if PC
282 // corresponds to inlined function. Inlined frames are printed in the order
283 // from "most-inlined" to the "least-inlined", so the last frame should be the
284 // not inlined function.
285 // Inlined frames can be removed with 'symbolize_inline_frames=0'.
286 // The format syntax is described in
287 // lib/sanitizer_common/sanitizer_stacktrace_printer.h.
288 void SANITIZER_CDECL __sanitizer_symbolize_pc(void *pc, const char *fmt,
289 char *out_buf,
290 size_t out_buf_size);
291 // Same as __sanitizer_symbolize_pc, but for data section (i.e. globals).
292 void SANITIZER_CDECL __sanitizer_symbolize_global(void *data_ptr,
293 const char *fmt,
294 char *out_buf,
295 size_t out_buf_size);
296 // Determine the return address.
297 #if !defined(_MSC_VER) || defined(__clang__)
298 #define __sanitizer_return_address() \
299 __builtin_extract_return_addr(__builtin_return_address(0))
300 #else
301 extern "C" void *SANITIZER_CDECL _ReturnAddress(void);
302 #pragma intrinsic(_ReturnAddress)
303 #define __sanitizer_return_address() _ReturnAddress()
304 #endif
306 /// Sets the callback to be called immediately before death on error.
308 /// Passing 0 will unset the callback.
310 /// \param callback User-provided callback.
311 void SANITIZER_CDECL __sanitizer_set_death_callback(void (*callback)(void));
313 // Interceptor hooks.
314 // Whenever a libc function interceptor is called, it checks if the
315 // corresponding weak hook is defined, and calls it if it is indeed defined.
316 // The primary use-case is data-flow-guided fuzzing, where the fuzzer needs
317 // to know what is being passed to libc functions (for example memcmp).
318 // FIXME: implement more hooks.
320 /// Interceptor hook for <c>memcmp()</c>.
322 /// \param called_pc PC (program counter) address of the original call.
323 /// \param s1 Pointer to block of memory.
324 /// \param s2 Pointer to block of memory.
325 /// \param n Number of bytes to compare.
326 /// \param result Value returned by the intercepted function.
327 void SANITIZER_CDECL __sanitizer_weak_hook_memcmp(void *called_pc,
328 const void *s1,
329 const void *s2, size_t n,
330 int result);
332 /// Interceptor hook for <c>strncmp()</c>.
334 /// \param called_pc PC (program counter) address of the original call.
335 /// \param s1 Pointer to block of memory.
336 /// \param s2 Pointer to block of memory.
337 /// \param n Number of bytes to compare.
338 /// \param result Value returned by the intercepted function.
339 void SANITIZER_CDECL __sanitizer_weak_hook_strncmp(void *called_pc,
340 const char *s1,
341 const char *s2, size_t n,
342 int result);
344 /// Interceptor hook for <c>strncasecmp()</c>.
346 /// \param called_pc PC (program counter) address of the original call.
347 /// \param s1 Pointer to block of memory.
348 /// \param s2 Pointer to block of memory.
349 /// \param n Number of bytes to compare.
350 /// \param result Value returned by the intercepted function.
351 void SANITIZER_CDECL __sanitizer_weak_hook_strncasecmp(void *called_pc,
352 const char *s1,
353 const char *s2, size_t n,
354 int result);
356 /// Interceptor hook for <c>strcmp()</c>.
358 /// \param called_pc PC (program counter) address of the original call.
359 /// \param s1 Pointer to block of memory.
360 /// \param s2 Pointer to block of memory.
361 /// \param result Value returned by the intercepted function.
362 void SANITIZER_CDECL __sanitizer_weak_hook_strcmp(void *called_pc,
363 const char *s1,
364 const char *s2, int result);
366 /// Interceptor hook for <c>strcasecmp()</c>.
368 /// \param called_pc PC (program counter) address of the original call.
369 /// \param s1 Pointer to block of memory.
370 /// \param s2 Pointer to block of memory.
371 /// \param result Value returned by the intercepted function.
372 void SANITIZER_CDECL __sanitizer_weak_hook_strcasecmp(void *called_pc,
373 const char *s1,
374 const char *s2,
375 int result);
377 /// Interceptor hook for <c>strstr()</c>.
379 /// \param called_pc PC (program counter) address of the original call.
380 /// \param s1 Pointer to block of memory.
381 /// \param s2 Pointer to block of memory.
382 /// \param result Value returned by the intercepted function.
383 void SANITIZER_CDECL __sanitizer_weak_hook_strstr(void *called_pc,
384 const char *s1,
385 const char *s2, char *result);
387 void SANITIZER_CDECL __sanitizer_weak_hook_strcasestr(void *called_pc,
388 const char *s1,
389 const char *s2,
390 char *result);
392 void SANITIZER_CDECL __sanitizer_weak_hook_memmem(void *called_pc,
393 const void *s1, size_t len1,
394 const void *s2, size_t len2,
395 void *result);
397 // Prints stack traces for all live heap allocations ordered by total
398 // allocation size until top_percent of total live heap is shown. top_percent
399 // should be between 1 and 100. At most max_number_of_contexts contexts
400 // (stack traces) are printed.
401 // Experimental feature currently available only with ASan on Linux/x86_64.
402 void SANITIZER_CDECL __sanitizer_print_memory_profile(
403 size_t top_percent, size_t max_number_of_contexts);
405 /// Notify ASan that a fiber switch has started (required only if implementing
406 /// your own fiber library).
408 /// Before switching to a different stack, you must call
409 /// <c>__sanitizer_start_switch_fiber()</c> with a pointer to the bottom of the
410 /// destination stack and with its size. When code starts running on the new
411 /// stack, it must call <c>__sanitizer_finish_switch_fiber()</c> to finalize
412 /// the switch. The <c>__sanitizer_start_switch_fiber()</c> function takes a
413 /// <c>void**</c> pointer argument to store the current fake stack if there is
414 /// one (it is necessary when the runtime option
415 /// <c>detect_stack_use_after_return</c> is enabled).
417 /// When restoring a stack, this <c>void**</c> pointer must be given to the
418 /// <c>__sanitizer_finish_switch_fiber()</c> function. In most cases, this
419 /// pointer can be stored on the stack immediately before switching. When
420 /// leaving a fiber definitely, NULL must be passed as the first argument to
421 /// the <c>__sanitizer_start_switch_fiber()</c> function so that the fake stack
422 /// is destroyed. If your program does not need stack use-after-return
423 /// detection, you can always pass NULL to these two functions.
425 /// \note The fake stack mechanism is disabled during fiber switch, so if a
426 /// signal callback runs during the switch, it will not benefit from stack
427 /// use-after-return detection.
429 /// \param[out] fake_stack_save Fake stack save location.
430 /// \param bottom Bottom address of stack.
431 /// \param size Size of stack in bytes.
432 void SANITIZER_CDECL __sanitizer_start_switch_fiber(void **fake_stack_save,
433 const void *bottom,
434 size_t size);
436 /// Notify ASan that a fiber switch has completed (required only if
437 /// implementing your own fiber library).
439 /// When code starts running on the new stack, it must call
440 /// <c>__sanitizer_finish_switch_fiber()</c> to finalize
441 /// the switch. For usage details, see the description of
442 /// <c>__sanitizer_start_switch_fiber()</c>.
444 /// \param fake_stack_save Fake stack save location.
445 /// \param[out] bottom_old Bottom address of old stack.
446 /// \param[out] size_old Size of old stack in bytes.
447 void SANITIZER_CDECL __sanitizer_finish_switch_fiber(void *fake_stack_save,
448 const void **bottom_old,
449 size_t *size_old);
451 // Get full module name and calculate pc offset within it.
452 // Returns 1 if pc belongs to some module, 0 if module was not found.
453 int SANITIZER_CDECL __sanitizer_get_module_and_offset_for_pc(
454 void *pc, char *module_path, size_t module_path_len, void **pc_offset);
456 #ifdef __cplusplus
457 } // extern "C"
458 #endif
460 #endif // SANITIZER_COMMON_INTERFACE_DEFS_H