10b57cec5SDimitry Andric //===-- sanitizer/asan_interface.h ------------------------------*- C++ -*-===//
20b57cec5SDimitry Andric //
30b57cec5SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
40b57cec5SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
50b57cec5SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
60b57cec5SDimitry Andric //
70b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
80b57cec5SDimitry Andric //
90b57cec5SDimitry Andric // This file is a part of AddressSanitizer (ASan).
100b57cec5SDimitry Andric //
110b57cec5SDimitry Andric // Public interface header.
120b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
130b57cec5SDimitry Andric #ifndef SANITIZER_ASAN_INTERFACE_H
140b57cec5SDimitry Andric #define SANITIZER_ASAN_INTERFACE_H
150b57cec5SDimitry Andric 
160b57cec5SDimitry Andric #include <sanitizer/common_interface_defs.h>
170b57cec5SDimitry Andric 
180b57cec5SDimitry Andric #ifdef __cplusplus
190b57cec5SDimitry Andric extern "C" {
200b57cec5SDimitry Andric #endif
210b57cec5SDimitry Andric /// Marks a memory region (<c>[addr, addr+size)</c>) as unaddressable.
220b57cec5SDimitry Andric ///
230b57cec5SDimitry Andric /// This memory must be previously allocated by your program. Instrumented
240b57cec5SDimitry Andric /// code is forbidden from accessing addresses in this region until it is
250b57cec5SDimitry Andric /// unpoisoned. This function is not guaranteed to poison the entire region -
260b57cec5SDimitry Andric /// it could poison only a subregion of <c>[addr, addr+size)</c> due to ASan
270b57cec5SDimitry Andric /// alignment restrictions.
280b57cec5SDimitry Andric ///
290b57cec5SDimitry Andric /// \note This function is not thread-safe because no two threads can poison or
300b57cec5SDimitry Andric /// unpoison memory in the same memory region simultaneously.
310b57cec5SDimitry Andric ///
320b57cec5SDimitry Andric /// \param addr Start of memory region.
330b57cec5SDimitry Andric /// \param size Size of memory region.
340b57cec5SDimitry Andric void __asan_poison_memory_region(void const volatile *addr, size_t size);
350b57cec5SDimitry Andric 
360b57cec5SDimitry Andric /// Marks a memory region (<c>[addr, addr+size)</c>) as addressable.
370b57cec5SDimitry Andric ///
380b57cec5SDimitry Andric /// This memory must be previously allocated by your program. Accessing
390b57cec5SDimitry Andric /// addresses in this region is allowed until this region is poisoned again.
400b57cec5SDimitry Andric /// This function could unpoison a super-region of <c>[addr, addr+size)</c> due
410b57cec5SDimitry Andric /// to ASan alignment restrictions.
420b57cec5SDimitry Andric ///
430b57cec5SDimitry Andric /// \note This function is not thread-safe because no two threads can
440b57cec5SDimitry Andric /// poison or unpoison memory in the same memory region simultaneously.
450b57cec5SDimitry Andric ///
460b57cec5SDimitry Andric /// \param addr Start of memory region.
470b57cec5SDimitry Andric /// \param size Size of memory region.
480b57cec5SDimitry Andric void __asan_unpoison_memory_region(void const volatile *addr, size_t size);
490b57cec5SDimitry Andric 
500b57cec5SDimitry Andric // Macros provided for convenience.
510b57cec5SDimitry Andric #if __has_feature(address_sanitizer) || defined(__SANITIZE_ADDRESS__)
520b57cec5SDimitry Andric /// Marks a memory region as unaddressable.
530b57cec5SDimitry Andric ///
540b57cec5SDimitry Andric /// \note Macro provided for convenience; defined as a no-op if ASan is not
550b57cec5SDimitry Andric /// enabled.
560b57cec5SDimitry Andric ///
570b57cec5SDimitry Andric /// \param addr Start of memory region.
580b57cec5SDimitry Andric /// \param size Size of memory region.
590b57cec5SDimitry Andric #define ASAN_POISON_MEMORY_REGION(addr, size) \
600b57cec5SDimitry Andric   __asan_poison_memory_region((addr), (size))
610b57cec5SDimitry Andric 
620b57cec5SDimitry Andric /// Marks a memory region as addressable.
630b57cec5SDimitry Andric ///
640b57cec5SDimitry Andric /// \note Macro provided for convenience; defined as a no-op if ASan is not
650b57cec5SDimitry Andric /// enabled.
660b57cec5SDimitry Andric ///
670b57cec5SDimitry Andric /// \param addr Start of memory region.
680b57cec5SDimitry Andric /// \param size Size of memory region.
690b57cec5SDimitry Andric #define ASAN_UNPOISON_MEMORY_REGION(addr, size) \
700b57cec5SDimitry Andric   __asan_unpoison_memory_region((addr), (size))
710b57cec5SDimitry Andric #else
720b57cec5SDimitry Andric #define ASAN_POISON_MEMORY_REGION(addr, size) \
730b57cec5SDimitry Andric   ((void)(addr), (void)(size))
740b57cec5SDimitry Andric #define ASAN_UNPOISON_MEMORY_REGION(addr, size) \
750b57cec5SDimitry Andric   ((void)(addr), (void)(size))
760b57cec5SDimitry Andric #endif
770b57cec5SDimitry Andric 
780b57cec5SDimitry Andric /// Checks if an address is poisoned.
790b57cec5SDimitry Andric ///
800b57cec5SDimitry Andric /// Returns 1 if <c><i>addr</i></c> is poisoned (that is, 1-byte read/write
810b57cec5SDimitry Andric /// access to this address would result in an error report from ASan).
820b57cec5SDimitry Andric /// Otherwise returns 0.
830b57cec5SDimitry Andric ///
840b57cec5SDimitry Andric /// \param addr Address to check.
850b57cec5SDimitry Andric ///
860b57cec5SDimitry Andric /// \retval 1 Address is poisoned.
870b57cec5SDimitry Andric /// \retval 0 Address is not poisoned.
880b57cec5SDimitry Andric int __asan_address_is_poisoned(void const volatile *addr);
890b57cec5SDimitry Andric 
900b57cec5SDimitry Andric /// Checks if a region is poisoned.
910b57cec5SDimitry Andric ///
920b57cec5SDimitry Andric /// If at least one byte in <c>[beg, beg+size)</c> is poisoned, returns the
930b57cec5SDimitry Andric /// address of the first such byte. Otherwise returns 0.
940b57cec5SDimitry Andric ///
950b57cec5SDimitry Andric /// \param beg Start of memory region.
960b57cec5SDimitry Andric /// \param size Start of memory region.
970b57cec5SDimitry Andric /// \returns Address of first poisoned byte.
980b57cec5SDimitry Andric void *__asan_region_is_poisoned(void *beg, size_t size);
990b57cec5SDimitry Andric 
1000b57cec5SDimitry Andric /// Describes an address (useful for calling from the debugger).
1010b57cec5SDimitry Andric ///
1020b57cec5SDimitry Andric /// Prints the description of <c><i>addr</i></c>.
1030b57cec5SDimitry Andric ///
1040b57cec5SDimitry Andric /// \param addr Address to describe.
1050b57cec5SDimitry Andric void __asan_describe_address(void *addr);
1060b57cec5SDimitry Andric 
1070b57cec5SDimitry Andric /// Checks if an error has been or is being reported (useful for calling from
1080b57cec5SDimitry Andric /// the debugger to get information about an ASan error).
1090b57cec5SDimitry Andric ///
1100b57cec5SDimitry Andric /// Returns 1 if an error has been (or is being) reported. Otherwise returns 0.
1110b57cec5SDimitry Andric ///
1120b57cec5SDimitry Andric /// \returns 1 if an error has been (or is being) reported. Otherwise returns
1130b57cec5SDimitry Andric /// 0.
1140b57cec5SDimitry Andric int __asan_report_present(void);
1150b57cec5SDimitry Andric 
1160b57cec5SDimitry Andric /// Gets the PC (program counter) register value of an ASan error (useful for
1170b57cec5SDimitry Andric /// calling from the debugger).
1180b57cec5SDimitry Andric ///
1190b57cec5SDimitry Andric /// Returns PC if an error has been (or is being) reported.
1200b57cec5SDimitry Andric /// Otherwise returns 0.
1210b57cec5SDimitry Andric ///
1220b57cec5SDimitry Andric /// \returns PC value.
1230b57cec5SDimitry Andric void *__asan_get_report_pc(void);
1240b57cec5SDimitry Andric 
1250b57cec5SDimitry Andric /// Gets the BP (base pointer) register value of an ASan error (useful for
1260b57cec5SDimitry Andric /// calling from the debugger).
1270b57cec5SDimitry Andric ///
1280b57cec5SDimitry Andric /// Returns BP if an error has been (or is being) reported.
1290b57cec5SDimitry Andric /// Otherwise returns 0.
1300b57cec5SDimitry Andric ///
1310b57cec5SDimitry Andric /// \returns BP value.
1320b57cec5SDimitry Andric void *__asan_get_report_bp(void);
1330b57cec5SDimitry Andric 
1340b57cec5SDimitry Andric /// Gets the SP (stack pointer) register value of an ASan error (useful for
1350b57cec5SDimitry Andric /// calling from the debugger).
1360b57cec5SDimitry Andric ///
1370b57cec5SDimitry Andric /// If an error has been (or is being) reported, returns SP.
1380b57cec5SDimitry Andric /// Otherwise returns 0.
1390b57cec5SDimitry Andric ///
1400b57cec5SDimitry Andric /// \returns SP value.
1410b57cec5SDimitry Andric void *__asan_get_report_sp(void);
1420b57cec5SDimitry Andric 
1430b57cec5SDimitry Andric /// Gets the address of the report buffer of an ASan error (useful for calling
1440b57cec5SDimitry Andric /// from the debugger).
1450b57cec5SDimitry Andric ///
1460b57cec5SDimitry Andric /// Returns the address of the report buffer if an error has been (or is being)
1470b57cec5SDimitry Andric /// reported. Otherwise returns 0.
1480b57cec5SDimitry Andric ///
1490b57cec5SDimitry Andric /// \returns Address of report buffer.
1500b57cec5SDimitry Andric void *__asan_get_report_address(void);
1510b57cec5SDimitry Andric 
1520b57cec5SDimitry Andric /// Gets access type of an ASan error (useful for calling from the debugger).
1530b57cec5SDimitry Andric ///
1540b57cec5SDimitry Andric /// Returns access type (read or write) if an error has been (or is being)
1550b57cec5SDimitry Andric /// reported. Otherwise returns 0.
1560b57cec5SDimitry Andric ///
1570b57cec5SDimitry Andric /// \returns Access type (0 = read, 1 = write).
1580b57cec5SDimitry Andric int __asan_get_report_access_type(void);
1590b57cec5SDimitry Andric 
1600b57cec5SDimitry Andric /// Gets access size of an ASan error (useful for calling from the debugger).
1610b57cec5SDimitry Andric ///
1620b57cec5SDimitry Andric /// Returns access size if an error has been (or is being) reported. Otherwise
1630b57cec5SDimitry Andric /// returns 0.
1640b57cec5SDimitry Andric ///
1650b57cec5SDimitry Andric /// \returns Access size in bytes.
1660b57cec5SDimitry Andric size_t __asan_get_report_access_size(void);
1670b57cec5SDimitry Andric 
1680b57cec5SDimitry Andric /// Gets the bug description of an ASan error (useful for calling from a
1690b57cec5SDimitry Andric /// debugger).
1700b57cec5SDimitry Andric ///
1710b57cec5SDimitry Andric /// \returns Returns a bug description if an error has been (or is being)
1720b57cec5SDimitry Andric /// reported - for example, "heap-use-after-free". Otherwise returns an empty
1730b57cec5SDimitry Andric /// string.
1740b57cec5SDimitry Andric const char *__asan_get_report_description(void);
1750b57cec5SDimitry Andric 
1760b57cec5SDimitry Andric /// Gets information about a pointer (useful for calling from the debugger).
1770b57cec5SDimitry Andric ///
1780b57cec5SDimitry Andric /// Returns the category of the given pointer as a constant string.
1790b57cec5SDimitry Andric /// Possible return values are <c>global</c>, <c>stack</c>, <c>stack-fake</c>,
1800b57cec5SDimitry Andric /// <c>heap</c>, <c>heap-invalid</c>, <c>shadow-low</c>, <c>shadow-gap</c>,
1810b57cec5SDimitry Andric /// <c>shadow-high</c>, and <c>unknown</c>.
1820b57cec5SDimitry Andric ///
1830b57cec5SDimitry Andric /// If the return value is <c>global</c> or <c>stack</c>, tries to also return
1840b57cec5SDimitry Andric /// the variable name, address, and size. If the return value is <c>heap</c>,
1850b57cec5SDimitry Andric /// tries to return the chunk address and size. <c><i>name</i></c> should point
1860b57cec5SDimitry Andric /// to an allocated buffer of size <c><i>name_size</i></c>.
1870b57cec5SDimitry Andric ///
1880b57cec5SDimitry Andric /// \param addr Address to locate.
1890b57cec5SDimitry Andric /// \param name Buffer to store the variable's name.
1900b57cec5SDimitry Andric /// \param name_size Size in bytes of the variable's name buffer.
1910b57cec5SDimitry Andric /// \param region_address [out] Address of the region.
1920b57cec5SDimitry Andric /// \param region_size [out] Size of the region in bytes.
1930b57cec5SDimitry Andric ///
1940b57cec5SDimitry Andric /// \returns Returns the category of the given pointer as a constant string.
1950b57cec5SDimitry Andric const char *__asan_locate_address(void *addr, char *name, size_t name_size,
1960b57cec5SDimitry Andric                                   void **region_address, size_t *region_size);
1970b57cec5SDimitry Andric 
1980b57cec5SDimitry Andric /// Gets the allocation stack trace and thread ID for a heap address (useful
1990b57cec5SDimitry Andric /// for calling from the debugger).
2000b57cec5SDimitry Andric ///
2010b57cec5SDimitry Andric /// Stores up to <c><i>size</i></c> frames in <c><i>trace</i></c>. Returns
2020b57cec5SDimitry Andric /// the number of stored frames or 0 on error.
2030b57cec5SDimitry Andric ///
2040b57cec5SDimitry Andric /// \param addr A heap address.
2050b57cec5SDimitry Andric /// \param trace A buffer to store the stack trace.
2060b57cec5SDimitry Andric /// \param size Size in bytes of the trace buffer.
2070b57cec5SDimitry Andric /// \param thread_id [out] The thread ID of the address.
2080b57cec5SDimitry Andric ///
2090b57cec5SDimitry Andric /// \returns Returns the number of stored frames or 0 on error.
2100b57cec5SDimitry Andric size_t __asan_get_alloc_stack(void *addr, void **trace, size_t size,
2110b57cec5SDimitry Andric                               int *thread_id);
2120b57cec5SDimitry Andric 
2130b57cec5SDimitry Andric /// Gets the free stack trace and thread ID for a heap address (useful for
2140b57cec5SDimitry Andric /// calling from the debugger).
2150b57cec5SDimitry Andric ///
2160b57cec5SDimitry Andric /// Stores up to <c><i>size</i></c> frames in <c><i>trace</i></c>. Returns
2170b57cec5SDimitry Andric /// the number of stored frames or 0 on error.
2180b57cec5SDimitry Andric ///
2190b57cec5SDimitry Andric /// \param addr A heap address.
2200b57cec5SDimitry Andric /// \param trace A buffer to store the stack trace.
2210b57cec5SDimitry Andric /// \param size Size in bytes of the trace buffer.
2220b57cec5SDimitry Andric /// \param thread_id [out] The thread ID of the address.
2230b57cec5SDimitry Andric ///
2240b57cec5SDimitry Andric /// \returns Returns the number of stored frames or 0 on error.
2250b57cec5SDimitry Andric size_t __asan_get_free_stack(void *addr, void **trace, size_t size,
2260b57cec5SDimitry Andric                              int *thread_id);
2270b57cec5SDimitry Andric 
2280b57cec5SDimitry Andric /// Gets the current shadow memory mapping (useful for calling from the
2290b57cec5SDimitry Andric /// debugger).
2300b57cec5SDimitry Andric ///
2310b57cec5SDimitry Andric /// \param shadow_scale [out] Shadow scale value.
2320b57cec5SDimitry Andric /// \param shadow_offset [out] Offset value.
2330b57cec5SDimitry Andric void __asan_get_shadow_mapping(size_t *shadow_scale, size_t *shadow_offset);
2340b57cec5SDimitry Andric 
2350b57cec5SDimitry Andric /// This is an internal function that is called to report an error. However,
2360b57cec5SDimitry Andric /// it is still a part of the interface because you might want to set a
2370b57cec5SDimitry Andric /// breakpoint on this function in the debugger.
2380b57cec5SDimitry Andric ///
2390b57cec5SDimitry Andric /// \param pc <c><i>pc</i></c> value of the ASan error.
2400b57cec5SDimitry Andric /// \param bp <c><i>bp</i></c> value of the ASan error.
2410b57cec5SDimitry Andric /// \param sp <c><i>sp</i></c> value of the ASan error.
2420b57cec5SDimitry Andric /// \param addr Address of the ASan error.
2430b57cec5SDimitry Andric /// \param is_write True if the error is a write error; false otherwise.
2440b57cec5SDimitry Andric /// \param access_size Size of the memory access of the ASan error.
2450b57cec5SDimitry Andric void __asan_report_error(void *pc, void *bp, void *sp,
2460b57cec5SDimitry Andric                          void *addr, int is_write, size_t access_size);
2470b57cec5SDimitry Andric 
2480b57cec5SDimitry Andric // Deprecated. Call __sanitizer_set_death_callback instead.
2490b57cec5SDimitry Andric void __asan_set_death_callback(void (*callback)(void));
2500b57cec5SDimitry Andric 
2510b57cec5SDimitry Andric /// Sets the callback function to be called during ASan error reporting.
2520b57cec5SDimitry Andric ///
2530b57cec5SDimitry Andric /// The callback provides a string pointer to the report.
2540b57cec5SDimitry Andric ///
2550b57cec5SDimitry Andric /// \param callback User-provided function.
2560b57cec5SDimitry Andric void __asan_set_error_report_callback(void (*callback)(const char *));
2570b57cec5SDimitry Andric 
2580b57cec5SDimitry Andric /// User-provided callback on ASan errors.
2590b57cec5SDimitry Andric ///
2600b57cec5SDimitry Andric /// You can provide a function that would be called immediately when ASan
2610b57cec5SDimitry Andric /// detects an error. This is useful in cases when ASan detects an error but
2620b57cec5SDimitry Andric /// your program crashes before the ASan report is printed.
2630b57cec5SDimitry Andric void __asan_on_error(void);
2640b57cec5SDimitry Andric 
2650b57cec5SDimitry Andric /// Prints accumulated statistics to <c>stderr</c> (useful for calling from the
2660b57cec5SDimitry Andric /// debugger).
2670b57cec5SDimitry Andric void __asan_print_accumulated_stats(void);
2680b57cec5SDimitry Andric 
2690b57cec5SDimitry Andric /// User-provided default option settings.
2700b57cec5SDimitry Andric ///
2710b57cec5SDimitry Andric /// You can provide your own implementation of this function to return a string
2720b57cec5SDimitry Andric /// containing ASan runtime options (for example,
2730b57cec5SDimitry Andric /// <c>verbosity=1:halt_on_error=0</c>).
2740b57cec5SDimitry Andric ///
2750b57cec5SDimitry Andric /// \returns Default options string.
2760b57cec5SDimitry Andric const char* __asan_default_options(void);
2770b57cec5SDimitry Andric 
2780b57cec5SDimitry Andric // The following two functions facilitate garbage collection in presence of
2790b57cec5SDimitry Andric // ASan's fake stack.
2800b57cec5SDimitry Andric 
2810b57cec5SDimitry Andric /// Gets an opaque handler to the current thread's fake stack.
2820b57cec5SDimitry Andric ///
2830b57cec5SDimitry Andric /// Returns an opaque handler to be used by
2840b57cec5SDimitry Andric /// <c>__asan_addr_is_in_fake_stack()</c>. Returns NULL if the current thread
2850b57cec5SDimitry Andric /// does not have a fake stack.
2860b57cec5SDimitry Andric ///
2870b57cec5SDimitry Andric /// \returns An opaque handler to the fake stack or NULL.
2880b57cec5SDimitry Andric void *__asan_get_current_fake_stack(void);
2890b57cec5SDimitry Andric 
2900b57cec5SDimitry Andric /// Checks if an address belongs to a given fake stack.
2910b57cec5SDimitry Andric ///
2920b57cec5SDimitry Andric /// If <c><i>fake_stack</i></c> is non-NULL and <c><i>addr</i></c> belongs to a
2930b57cec5SDimitry Andric /// fake frame in <c><i>fake_stack</i></c>, returns the address of the real
2940b57cec5SDimitry Andric /// stack that corresponds to the fake frame and sets <c><i>beg</i></c> and
2950b57cec5SDimitry Andric /// <c><i>end</i></c> to the boundaries of this fake frame. Otherwise returns
2960b57cec5SDimitry Andric /// NULL and does not touch <c><i>beg</i></c> and <c><i>end</i></c>.
2970b57cec5SDimitry Andric ///
2980b57cec5SDimitry Andric /// If <c><i>beg</i></c> or <c><i>end</i></c> are NULL, they are not touched.
2990b57cec5SDimitry Andric ///
3000b57cec5SDimitry Andric /// \note This function can be called from a thread other than the owner of
3010b57cec5SDimitry Andric /// <c><i>fake_stack</i></c>, but the owner thread needs to be alive.
3020b57cec5SDimitry Andric ///
3030b57cec5SDimitry Andric /// \param fake_stack An opaque handler to a fake stack.
3040b57cec5SDimitry Andric /// \param addr Address to test.
3050b57cec5SDimitry Andric /// \param beg [out] Beginning of fake frame.
3060b57cec5SDimitry Andric /// \param end [out] End of fake frame.
3070b57cec5SDimitry Andric /// \returns Stack address or NULL.
3080b57cec5SDimitry Andric void *__asan_addr_is_in_fake_stack(void *fake_stack, void *addr, void **beg,
3090b57cec5SDimitry Andric                                    void **end);
3100b57cec5SDimitry Andric 
3110b57cec5SDimitry Andric /// Performs shadow memory cleanup of the current thread's stack before a
3120b57cec5SDimitry Andric /// function marked with the <c>[[noreturn]]</c> attribute is called.
3130b57cec5SDimitry Andric ///
3140b57cec5SDimitry Andric /// To avoid false positives on the stack, must be called before no-return
3150b57cec5SDimitry Andric /// functions like <c>_exit()</c> and <c>execl()</c>.
3160b57cec5SDimitry Andric void __asan_handle_no_return(void);
3170b57cec5SDimitry Andric 
3180b57cec5SDimitry Andric #ifdef __cplusplus
3190b57cec5SDimitry Andric }  // extern "C"
3200b57cec5SDimitry Andric #endif
3210b57cec5SDimitry Andric 
3220b57cec5SDimitry Andric #endif  // SANITIZER_ASAN_INTERFACE_H
323