Path: blob/main/contrib/llvm-project/compiler-rt/include/sanitizer/common_interface_defs.h
35235 views
//===-- sanitizer/common_interface_defs.h -----------------------*- C++ -*-===//1//2// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.3// See https://llvm.org/LICENSE.txt for license information.4// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception5//6//===----------------------------------------------------------------------===//7//8// Common part of the public sanitizer interface.9//===----------------------------------------------------------------------===//1011#ifndef SANITIZER_COMMON_INTERFACE_DEFS_H12#define SANITIZER_COMMON_INTERFACE_DEFS_H1314#include <stddef.h>15#include <stdint.h>1617// Windows allows a user to set their default calling convention, but we always18// use __cdecl19#ifdef _WIN3220#define SANITIZER_CDECL __cdecl21#else22#define SANITIZER_CDECL23#endif2425#ifdef __cplusplus26extern "C" {27#endif28// Arguments for __sanitizer_sandbox_on_notify() below.29typedef struct {30// Enable sandbox support in sanitizer coverage.31int coverage_sandboxed;32// File descriptor to write coverage data to. If -1 is passed, a file will33// be pre-opened by __sanitizer_sandbox_on_notify(). This field has no34// effect if coverage_sandboxed == 0.35intptr_t coverage_fd;36// If non-zero, split the coverage data into well-formed blocks. This is37// useful when coverage_fd is a socket descriptor. Each block will contain38// a header, allowing data from multiple processes to be sent over the same39// socket.40unsigned int coverage_max_block_size;41} __sanitizer_sandbox_arguments;4243// Tell the tools to write their reports to "path.<pid>" instead of stderr.44void SANITIZER_CDECL __sanitizer_set_report_path(const char *path);45// Tell the tools to write their reports to the provided file descriptor46// (casted to void *).47void SANITIZER_CDECL __sanitizer_set_report_fd(void *fd);48// Get the current full report file path, if a path was specified by49// an earlier call to __sanitizer_set_report_path. Returns null otherwise.50const char *SANITIZER_CDECL __sanitizer_get_report_path();5152// Notify the tools that the sandbox is going to be turned on. The reserved53// parameter will be used in the future to hold a structure with functions54// that the tools may call to bypass the sandbox.55void SANITIZER_CDECL56__sanitizer_sandbox_on_notify(__sanitizer_sandbox_arguments *args);5758// This function is called by the tool when it has just finished reporting59// an error. 'error_summary' is a one-line string that summarizes60// the error message. This function can be overridden by the client.61void SANITIZER_CDECL62__sanitizer_report_error_summary(const char *error_summary);6364// Some of the sanitizers (for example ASan/TSan) could miss bugs that happen65// in unaligned loads/stores. To find such bugs reliably, you need to replace66// plain unaligned loads/stores with these calls.6768/// Loads a 16-bit unaligned value.69//70/// \param p Pointer to unaligned memory.71///72/// \returns Loaded value.73uint16_t SANITIZER_CDECL __sanitizer_unaligned_load16(const void *p);7475/// Loads a 32-bit unaligned value.76///77/// \param p Pointer to unaligned memory.78///79/// \returns Loaded value.80uint32_t SANITIZER_CDECL __sanitizer_unaligned_load32(const void *p);8182/// Loads a 64-bit unaligned value.83///84/// \param p Pointer to unaligned memory.85///86/// \returns Loaded value.87uint64_t SANITIZER_CDECL __sanitizer_unaligned_load64(const void *p);8889/// Stores a 16-bit unaligned value.90///91/// \param p Pointer to unaligned memory.92/// \param x 16-bit value to store.93void SANITIZER_CDECL __sanitizer_unaligned_store16(void *p, uint16_t x);9495/// Stores a 32-bit unaligned value.96///97/// \param p Pointer to unaligned memory.98/// \param x 32-bit value to store.99void SANITIZER_CDECL __sanitizer_unaligned_store32(void *p, uint32_t x);100101/// Stores a 64-bit unaligned value.102///103/// \param p Pointer to unaligned memory.104/// \param x 64-bit value to store.105void SANITIZER_CDECL __sanitizer_unaligned_store64(void *p, uint64_t x);106107// Returns 1 on the first call, then returns 0 thereafter. Called by the tool108// to ensure only one report is printed when multiple errors occur109// simultaneously.110int SANITIZER_CDECL __sanitizer_acquire_crash_state();111112/// Annotates the current state of a contiguous container, such as113/// <c>std::vector</c>, <c>std::string</c>, or similar.114///115/// A contiguous container is a container that keeps all of its elements116/// in a contiguous region of memory. The container owns the region of memory117/// <c>[beg, end)</c>; the memory <c>[beg, mid)</c> is used to store the118/// current elements, and the memory <c>[mid, end)</c> is reserved for future119/// elements (<c>beg <= mid <= end</c>). For example, in120/// <c>std::vector<> v</c>:121///122/// \code123/// beg = &v[0];124/// end = beg + v.capacity() * sizeof(v[0]);125/// mid = beg + v.size() * sizeof(v[0]);126/// \endcode127///128/// This annotation tells the Sanitizer tool about the current state of the129/// container so that the tool can report errors when memory from130/// <c>[mid, end)</c> is accessed. Insert this annotation into methods like131/// <c>push_back()</c> or <c>pop_back()</c>. Supply the old and new values of132/// <c>mid</c>(<c><i>old_mid</i></c> and <c><i>new_mid</i></c>). In the initial133/// state <c>mid == end</c>, so that should be the final state when the134/// container is destroyed or when the container reallocates the storage.135///136/// For ASan, <c><i>beg</i></c> no longer needs to be 8-aligned,137/// first and last granule may be shared with other objects138/// and therefore the function can be used for any allocator.139///140/// The following example shows how to use the function:141///142/// \code143/// int32_t x[3]; // 12 bytes144/// char *beg = (char*)&x[0];145/// char *end = beg + 12;146/// __sanitizer_annotate_contiguous_container(beg, end, beg, end);147/// \endcode148///149/// \note Use this function with caution and do not use for anything other150/// than vector-like classes.151/// \note Unaligned <c><i>beg</i></c> or <c><i>end</i></c> may miss bugs in152/// these granules.153///154/// \param beg Beginning of memory region.155/// \param end End of memory region.156/// \param old_mid Old middle of memory region.157/// \param new_mid New middle of memory region.158void SANITIZER_CDECL __sanitizer_annotate_contiguous_container(159const void *beg, const void *end, const void *old_mid, const void *new_mid);160161/// Similar to <c>__sanitizer_annotate_contiguous_container</c>.162///163/// Annotates the current state of a contiguous container memory,164/// such as <c>std::deque</c>'s single chunk, when the boundries are moved.165///166/// A contiguous chunk is a chunk that keeps all of its elements167/// in a contiguous region of memory. The container owns the region of memory168/// <c>[storage_beg, storage_end)</c>; the memory <c>[container_beg,169/// container_end)</c> is used to store the current elements, and the memory170/// <c>[storage_beg, container_beg), [container_end, storage_end)</c> is171/// reserved for future elements (<c>storage_beg <= container_beg <=172/// container_end <= storage_end</c>). For example, in <c> std::deque </c>:173/// - chunk with a frist deques element will have container_beg equal to address174/// of the first element.175/// - in every next chunk with elements, true is <c> container_beg ==176/// storage_beg </c>.177///178/// Argument requirements:179/// During unpoisoning memory of empty container (before first element is180/// added):181/// - old_container_beg_p == old_container_end_p182/// During poisoning after last element was removed:183/// - new_container_beg_p == new_container_end_p184/// \param storage_beg Beginning of memory region.185/// \param storage_end End of memory region.186/// \param old_container_beg Old beginning of used region.187/// \param old_container_end End of used region.188/// \param new_container_beg New beginning of used region.189/// \param new_container_end New end of used region.190void SANITIZER_CDECL __sanitizer_annotate_double_ended_contiguous_container(191const void *storage_beg, const void *storage_end,192const void *old_container_beg, const void *old_container_end,193const void *new_container_beg, const void *new_container_end);194195/// Returns true if the contiguous container <c>[beg, end)</c> is properly196/// poisoned.197///198/// Proper poisoning could occur, for example, with199/// <c>__sanitizer_annotate_contiguous_container</c>), that is, if200/// <c>[beg, mid)</c> is addressable and <c>[mid, end)</c> is unaddressable.201/// Full verification requires O (<c>end - beg</c>) time; this function tries202/// to avoid such complexity by touching only parts of the container around203/// <c><i>beg</i></c>, <c><i>mid</i></c>, and <c><i>end</i></c>.204///205/// \param beg Beginning of memory region.206/// \param mid Middle of memory region.207/// \param end Old end of memory region.208///209/// \returns True if the contiguous container <c>[beg, end)</c> is properly210/// poisoned.211int SANITIZER_CDECL __sanitizer_verify_contiguous_container(const void *beg,212const void *mid,213const void *end);214215/// Returns true if the double ended contiguous216/// container <c>[storage_beg, storage_end)</c> is properly poisoned.217///218/// Proper poisoning could occur, for example, with219/// <c>__sanitizer_annotate_double_ended_contiguous_container</c>), that is, if220/// <c>[storage_beg, container_beg)</c> is not addressable, <c>[container_beg,221/// container_end)</c> is addressable and <c>[container_end, end)</c> is222/// unaddressable. Full verification requires O (<c>storage_end -223/// storage_beg</c>) time; this function tries to avoid such complexity by224/// touching only parts of the container around <c><i>storage_beg</i></c>,225/// <c><i>container_beg</i></c>, <c><i>container_end</i></c>, and226/// <c><i>storage_end</i></c>.227///228/// \param storage_beg Beginning of memory region.229/// \param container_beg Beginning of used region.230/// \param container_end End of used region.231/// \param storage_end End of memory region.232///233/// \returns True if the double-ended contiguous container <c>[storage_beg,234/// container_beg, container_end, end)</c> is properly poisoned - only235/// [container_beg; container_end) is addressable.236int SANITIZER_CDECL __sanitizer_verify_double_ended_contiguous_container(237const void *storage_beg, const void *container_beg,238const void *container_end, const void *storage_end);239240/// Similar to <c>__sanitizer_verify_contiguous_container()</c> but also241/// returns the address of the first improperly poisoned byte.242///243/// Returns NULL if the area is poisoned properly.244///245/// \param beg Beginning of memory region.246/// \param mid Middle of memory region.247/// \param end Old end of memory region.248///249/// \returns The bad address or NULL.250const void *SANITIZER_CDECL __sanitizer_contiguous_container_find_bad_address(251const void *beg, const void *mid, const void *end);252253/// returns the address of the first improperly poisoned byte.254///255/// Returns NULL if the area is poisoned properly.256///257/// \param storage_beg Beginning of memory region.258/// \param container_beg Beginning of used region.259/// \param container_end End of used region.260/// \param storage_end End of memory region.261///262/// \returns The bad address or NULL.263const void *SANITIZER_CDECL264__sanitizer_double_ended_contiguous_container_find_bad_address(265const void *storage_beg, const void *container_beg,266const void *container_end, const void *storage_end);267268/// Prints the stack trace leading to this call (useful for calling from the269/// debugger).270void SANITIZER_CDECL __sanitizer_print_stack_trace(void);271272// Symbolizes the supplied 'pc' using the format string 'fmt'.273// Outputs at most 'out_buf_size' bytes into 'out_buf'.274// If 'out_buf' is not empty then output is zero or more non empty C strings275// followed by single empty C string. Multiple strings can be returned if PC276// corresponds to inlined function. Inlined frames are printed in the order277// from "most-inlined" to the "least-inlined", so the last frame should be the278// not inlined function.279// Inlined frames can be removed with 'symbolize_inline_frames=0'.280// The format syntax is described in281// lib/sanitizer_common/sanitizer_stacktrace_printer.h.282void SANITIZER_CDECL __sanitizer_symbolize_pc(void *pc, const char *fmt,283char *out_buf,284size_t out_buf_size);285// Same as __sanitizer_symbolize_pc, but for data section (i.e. globals).286void SANITIZER_CDECL __sanitizer_symbolize_global(void *data_ptr,287const char *fmt,288char *out_buf,289size_t out_buf_size);290// Determine the return address.291#if !defined(_MSC_VER) || defined(__clang__)292#define __sanitizer_return_address() \293__builtin_extract_return_addr(__builtin_return_address(0))294#else295void *_ReturnAddress(void);296#pragma intrinsic(_ReturnAddress)297#define __sanitizer_return_address() _ReturnAddress()298#endif299300/// Sets the callback to be called immediately before death on error.301///302/// Passing 0 will unset the callback.303///304/// \param callback User-provided callback.305void SANITIZER_CDECL __sanitizer_set_death_callback(void (*callback)(void));306307// Interceptor hooks.308// Whenever a libc function interceptor is called, it checks if the309// corresponding weak hook is defined, and calls it if it is indeed defined.310// The primary use-case is data-flow-guided fuzzing, where the fuzzer needs311// to know what is being passed to libc functions (for example memcmp).312// FIXME: implement more hooks.313314/// Interceptor hook for <c>memcmp()</c>.315///316/// \param called_pc PC (program counter) address of the original call.317/// \param s1 Pointer to block of memory.318/// \param s2 Pointer to block of memory.319/// \param n Number of bytes to compare.320/// \param result Value returned by the intercepted function.321void SANITIZER_CDECL __sanitizer_weak_hook_memcmp(void *called_pc,322const void *s1,323const void *s2, size_t n,324int result);325326/// Interceptor hook for <c>strncmp()</c>.327///328/// \param called_pc PC (program counter) address of the original call.329/// \param s1 Pointer to block of memory.330/// \param s2 Pointer to block of memory.331/// \param n Number of bytes to compare.332/// \param result Value returned by the intercepted function.333void SANITIZER_CDECL __sanitizer_weak_hook_strncmp(void *called_pc,334const char *s1,335const char *s2, size_t n,336int result);337338/// Interceptor hook for <c>strncasecmp()</c>.339///340/// \param called_pc PC (program counter) address of the original call.341/// \param s1 Pointer to block of memory.342/// \param s2 Pointer to block of memory.343/// \param n Number of bytes to compare.344/// \param result Value returned by the intercepted function.345void SANITIZER_CDECL __sanitizer_weak_hook_strncasecmp(void *called_pc,346const char *s1,347const char *s2, size_t n,348int result);349350/// Interceptor hook for <c>strcmp()</c>.351///352/// \param called_pc PC (program counter) address of the original call.353/// \param s1 Pointer to block of memory.354/// \param s2 Pointer to block of memory.355/// \param result Value returned by the intercepted function.356void SANITIZER_CDECL __sanitizer_weak_hook_strcmp(void *called_pc,357const char *s1,358const char *s2, int result);359360/// Interceptor hook for <c>strcasecmp()</c>.361///362/// \param called_pc PC (program counter) address of the original call.363/// \param s1 Pointer to block of memory.364/// \param s2 Pointer to block of memory.365/// \param result Value returned by the intercepted function.366void SANITIZER_CDECL __sanitizer_weak_hook_strcasecmp(void *called_pc,367const char *s1,368const char *s2,369int result);370371/// Interceptor hook for <c>strstr()</c>.372///373/// \param called_pc PC (program counter) address of the original call.374/// \param s1 Pointer to block of memory.375/// \param s2 Pointer to block of memory.376/// \param result Value returned by the intercepted function.377void SANITIZER_CDECL __sanitizer_weak_hook_strstr(void *called_pc,378const char *s1,379const char *s2, char *result);380381void SANITIZER_CDECL __sanitizer_weak_hook_strcasestr(void *called_pc,382const char *s1,383const char *s2,384char *result);385386void SANITIZER_CDECL __sanitizer_weak_hook_memmem(void *called_pc,387const void *s1, size_t len1,388const void *s2, size_t len2,389void *result);390391// Prints stack traces for all live heap allocations ordered by total392// allocation size until top_percent of total live heap is shown. top_percent393// should be between 1 and 100. At most max_number_of_contexts contexts394// (stack traces) are printed.395// Experimental feature currently available only with ASan on Linux/x86_64.396void SANITIZER_CDECL __sanitizer_print_memory_profile(397size_t top_percent, size_t max_number_of_contexts);398399/// Notify ASan that a fiber switch has started (required only if implementing400/// your own fiber library).401///402/// Before switching to a different stack, you must call403/// <c>__sanitizer_start_switch_fiber()</c> with a pointer to the bottom of the404/// destination stack and with its size. When code starts running on the new405/// stack, it must call <c>__sanitizer_finish_switch_fiber()</c> to finalize406/// the switch. The <c>__sanitizer_start_switch_fiber()</c> function takes a407/// <c>void**</c> pointer argument to store the current fake stack if there is408/// one (it is necessary when the runtime option409/// <c>detect_stack_use_after_return</c> is enabled).410///411/// When restoring a stack, this <c>void**</c> pointer must be given to the412/// <c>__sanitizer_finish_switch_fiber()</c> function. In most cases, this413/// pointer can be stored on the stack immediately before switching. When414/// leaving a fiber definitely, NULL must be passed as the first argument to415/// the <c>__sanitizer_start_switch_fiber()</c> function so that the fake stack416/// is destroyed. If your program does not need stack use-after-return417/// detection, you can always pass NULL to these two functions.418///419/// \note The fake stack mechanism is disabled during fiber switch, so if a420/// signal callback runs during the switch, it will not benefit from stack421/// use-after-return detection.422///423/// \param[out] fake_stack_save Fake stack save location.424/// \param bottom Bottom address of stack.425/// \param size Size of stack in bytes.426void SANITIZER_CDECL __sanitizer_start_switch_fiber(void **fake_stack_save,427const void *bottom,428size_t size);429430/// Notify ASan that a fiber switch has completed (required only if431/// implementing your own fiber library).432///433/// When code starts running on the new stack, it must call434/// <c>__sanitizer_finish_switch_fiber()</c> to finalize435/// the switch. For usage details, see the description of436/// <c>__sanitizer_start_switch_fiber()</c>.437///438/// \param fake_stack_save Fake stack save location.439/// \param[out] bottom_old Bottom address of old stack.440/// \param[out] size_old Size of old stack in bytes.441void SANITIZER_CDECL __sanitizer_finish_switch_fiber(void *fake_stack_save,442const void **bottom_old,443size_t *size_old);444445// Get full module name and calculate pc offset within it.446// Returns 1 if pc belongs to some module, 0 if module was not found.447int SANITIZER_CDECL __sanitizer_get_module_and_offset_for_pc(448void *pc, char *module_path, size_t module_path_len, void **pc_offset);449450#ifdef __cplusplus451} // extern "C"452#endif453454#endif // SANITIZER_COMMON_INTERFACE_DEFS_H455456457