xref: /freebsd/contrib/llvm-project/compiler-rt/include/sanitizer/hwasan_interface.h (revision 5f757f3ff9144b609b3c433dfd370cc6bdc191ad)
106c3fb27SDimitry Andric //===-- sanitizer/hwasan_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 HWAddressSanitizer.
100b57cec5SDimitry Andric //
110b57cec5SDimitry Andric // Public interface header.
120b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
130b57cec5SDimitry Andric #ifndef SANITIZER_HWASAN_INTERFACE_H
140b57cec5SDimitry Andric #define SANITIZER_HWASAN_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 // Libc hook for program startup in statically linked executables.
220b57cec5SDimitry Andric // Initializes enough of the runtime to run instrumented code. This function
230b57cec5SDimitry Andric // should only be called in statically linked executables because it modifies
240b57cec5SDimitry Andric // the GOT, which won't work in regular binaries because RELRO will already
250b57cec5SDimitry Andric // have been applied by the time the function is called. This also means that
260b57cec5SDimitry Andric // the function should be called before libc applies RELRO.
270b57cec5SDimitry Andric // Does not call libc unless there is an error.
280b57cec5SDimitry Andric // Can be called multiple times.
29*5f757f3fSDimitry Andric void SANITIZER_CDECL __hwasan_init_static(void);
300b57cec5SDimitry Andric 
310b57cec5SDimitry Andric // This function may be optionally provided by user and should return
320b57cec5SDimitry Andric // a string containing HWASan runtime options. See asan_flags.h for details.
33*5f757f3fSDimitry Andric const char *SANITIZER_CDECL __hwasan_default_options(void);
340b57cec5SDimitry Andric 
35*5f757f3fSDimitry Andric void SANITIZER_CDECL __hwasan_enable_allocator_tagging(void);
36*5f757f3fSDimitry Andric void SANITIZER_CDECL __hwasan_disable_allocator_tagging(void);
370b57cec5SDimitry Andric 
380b57cec5SDimitry Andric // Mark region of memory with the given tag. Both address and size need to be
390b57cec5SDimitry Andric // 16-byte aligned.
40*5f757f3fSDimitry Andric void SANITIZER_CDECL __hwasan_tag_memory(const volatile void *p,
41*5f757f3fSDimitry Andric                                          unsigned char tag, size_t size);
420b57cec5SDimitry Andric 
430b57cec5SDimitry Andric /// Set pointer tag. Previous tag is lost.
44*5f757f3fSDimitry Andric void *SANITIZER_CDECL __hwasan_tag_pointer(const volatile void *p,
45*5f757f3fSDimitry Andric                                            unsigned char tag);
46*5f757f3fSDimitry Andric 
47*5f757f3fSDimitry Andric /// Get tag from the pointer.
48*5f757f3fSDimitry Andric unsigned char SANITIZER_CDECL
49*5f757f3fSDimitry Andric __hwasan_get_tag_from_pointer(const volatile void *p);
500b57cec5SDimitry Andric 
510b57cec5SDimitry Andric // Set memory tag from the current SP address to the given address to zero.
520b57cec5SDimitry Andric // This is meant to annotate longjmp and other non-local jumps.
530b57cec5SDimitry Andric // This function needs to know the (almost) exact destination frame address;
540b57cec5SDimitry Andric // clearing shadow for the entire thread stack like __asan_handle_no_return
550b57cec5SDimitry Andric // does would cause false reports.
56*5f757f3fSDimitry Andric void SANITIZER_CDECL __hwasan_handle_longjmp(const void *sp_dst);
570b57cec5SDimitry Andric 
580b57cec5SDimitry Andric // Set memory tag for the part of the current thread stack below sp_dst to
590b57cec5SDimitry Andric // zero. Call this in vfork() before returning in the parent process.
60*5f757f3fSDimitry Andric void SANITIZER_CDECL __hwasan_handle_vfork(const void *sp_dst);
610b57cec5SDimitry Andric 
620b57cec5SDimitry Andric // Libc hook for thread creation. Should be called in the child thread before
630b57cec5SDimitry Andric // any instrumented code.
64*5f757f3fSDimitry Andric void SANITIZER_CDECL __hwasan_thread_enter();
650b57cec5SDimitry Andric 
660b57cec5SDimitry Andric // Libc hook for thread destruction. No instrumented code should run after
670b57cec5SDimitry Andric // this call.
68*5f757f3fSDimitry Andric void SANITIZER_CDECL __hwasan_thread_exit();
690b57cec5SDimitry Andric 
700b57cec5SDimitry Andric // Print shadow and origin for the memory range to stderr in a human-readable
710b57cec5SDimitry Andric // format.
72*5f757f3fSDimitry Andric void SANITIZER_CDECL __hwasan_print_shadow(const volatile void *x, size_t size);
730b57cec5SDimitry Andric 
740b57cec5SDimitry Andric // Print one-line report about the memory usage of the current process.
75*5f757f3fSDimitry Andric void SANITIZER_CDECL __hwasan_print_memory_usage();
760b57cec5SDimitry Andric 
770b57cec5SDimitry Andric /* Returns the offset of the first byte in the memory range that can not be
780b57cec5SDimitry Andric  * accessed through the pointer in x, or -1 if the whole range is good. */
79*5f757f3fSDimitry Andric intptr_t SANITIZER_CDECL __hwasan_test_shadow(const volatile void *x,
80*5f757f3fSDimitry Andric                                               size_t size);
810b57cec5SDimitry Andric 
82e8d8bef9SDimitry Andric /* Sets the callback function to be called during HWASan error reporting. */
83*5f757f3fSDimitry Andric void SANITIZER_CDECL
84*5f757f3fSDimitry Andric __hwasan_set_error_report_callback(void (*callback)(const char *));
85e8d8bef9SDimitry Andric 
86*5f757f3fSDimitry Andric int SANITIZER_CDECL __sanitizer_posix_memalign(void **memptr, size_t alignment,
87*5f757f3fSDimitry Andric                                                size_t size);
88*5f757f3fSDimitry Andric void *SANITIZER_CDECL __sanitizer_memalign(size_t alignment, size_t size);
89*5f757f3fSDimitry Andric void *SANITIZER_CDECL __sanitizer_aligned_alloc(size_t alignment, size_t size);
90*5f757f3fSDimitry Andric void *SANITIZER_CDECL __sanitizer___libc_memalign(size_t alignment,
91*5f757f3fSDimitry Andric                                                   size_t size);
92*5f757f3fSDimitry Andric void *SANITIZER_CDECL __sanitizer_valloc(size_t size);
93*5f757f3fSDimitry Andric void *SANITIZER_CDECL __sanitizer_pvalloc(size_t size);
94*5f757f3fSDimitry Andric void SANITIZER_CDECL __sanitizer_free(void *ptr);
95*5f757f3fSDimitry Andric void SANITIZER_CDECL __sanitizer_cfree(void *ptr);
96*5f757f3fSDimitry Andric size_t SANITIZER_CDECL __sanitizer_malloc_usable_size(const void *ptr);
97*5f757f3fSDimitry Andric struct mallinfo SANITIZER_CDECL __sanitizer_mallinfo();
98*5f757f3fSDimitry Andric int SANITIZER_CDECL __sanitizer_mallopt(int cmd, int value);
99*5f757f3fSDimitry Andric void SANITIZER_CDECL __sanitizer_malloc_stats(void);
100*5f757f3fSDimitry Andric void *SANITIZER_CDECL __sanitizer_calloc(size_t nmemb, size_t size);
101*5f757f3fSDimitry Andric void *SANITIZER_CDECL __sanitizer_realloc(void *ptr, size_t size);
102*5f757f3fSDimitry Andric void *SANITIZER_CDECL __sanitizer_reallocarray(void *ptr, size_t nmemb,
103*5f757f3fSDimitry Andric                                                size_t size);
104*5f757f3fSDimitry Andric void *SANITIZER_CDECL __sanitizer_malloc(size_t size);
1050b57cec5SDimitry Andric #ifdef __cplusplus
1060b57cec5SDimitry Andric } // extern "C"
1070b57cec5SDimitry Andric #endif
1080b57cec5SDimitry Andric 
1090b57cec5SDimitry Andric #endif // SANITIZER_HWASAN_INTERFACE_H
110