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.
295f757f3fSDimitry 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.
335f757f3fSDimitry Andric const char *SANITIZER_CDECL __hwasan_default_options(void);
340b57cec5SDimitry Andric 
355f757f3fSDimitry Andric void SANITIZER_CDECL __hwasan_enable_allocator_tagging(void);
365f757f3fSDimitry 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.
405f757f3fSDimitry Andric void SANITIZER_CDECL __hwasan_tag_memory(const volatile void *p,
415f757f3fSDimitry Andric                                          unsigned char tag, size_t size);
420b57cec5SDimitry Andric 
430b57cec5SDimitry Andric /// Set pointer tag. Previous tag is lost.
445f757f3fSDimitry Andric void *SANITIZER_CDECL __hwasan_tag_pointer(const volatile void *p,
455f757f3fSDimitry Andric                                            unsigned char tag);
465f757f3fSDimitry Andric 
475f757f3fSDimitry Andric /// Get tag from the pointer.
485f757f3fSDimitry Andric unsigned char SANITIZER_CDECL
495f757f3fSDimitry 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.
565f757f3fSDimitry 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.
605f757f3fSDimitry 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.
645f757f3fSDimitry 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.
685f757f3fSDimitry 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.
725f757f3fSDimitry 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.
755f757f3fSDimitry 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. */
795f757f3fSDimitry Andric intptr_t SANITIZER_CDECL __hwasan_test_shadow(const volatile void *x,
805f757f3fSDimitry Andric                                               size_t size);
810b57cec5SDimitry Andric 
82e8d8bef9SDimitry Andric /* Sets the callback function to be called during HWASan error reporting. */
835f757f3fSDimitry Andric void SANITIZER_CDECL
845f757f3fSDimitry Andric __hwasan_set_error_report_callback(void (*callback)(const char *));
85e8d8bef9SDimitry Andric 
865f757f3fSDimitry Andric int SANITIZER_CDECL __sanitizer_posix_memalign(void **memptr, size_t alignment,
875f757f3fSDimitry Andric                                                size_t size);
885f757f3fSDimitry Andric void *SANITIZER_CDECL __sanitizer_memalign(size_t alignment, size_t size);
895f757f3fSDimitry Andric void *SANITIZER_CDECL __sanitizer_aligned_alloc(size_t alignment, size_t size);
905f757f3fSDimitry Andric void *SANITIZER_CDECL __sanitizer___libc_memalign(size_t alignment,
915f757f3fSDimitry Andric                                                   size_t size);
925f757f3fSDimitry Andric void *SANITIZER_CDECL __sanitizer_valloc(size_t size);
935f757f3fSDimitry Andric void *SANITIZER_CDECL __sanitizer_pvalloc(size_t size);
945f757f3fSDimitry Andric void SANITIZER_CDECL __sanitizer_free(void *ptr);
955f757f3fSDimitry Andric void SANITIZER_CDECL __sanitizer_cfree(void *ptr);
965f757f3fSDimitry Andric size_t SANITIZER_CDECL __sanitizer_malloc_usable_size(const void *ptr);
975f757f3fSDimitry Andric struct mallinfo SANITIZER_CDECL __sanitizer_mallinfo();
985f757f3fSDimitry Andric int SANITIZER_CDECL __sanitizer_mallopt(int cmd, int value);
995f757f3fSDimitry Andric void SANITIZER_CDECL __sanitizer_malloc_stats(void);
1005f757f3fSDimitry Andric void *SANITIZER_CDECL __sanitizer_calloc(size_t nmemb, size_t size);
1015f757f3fSDimitry Andric void *SANITIZER_CDECL __sanitizer_realloc(void *ptr, size_t size);
1025f757f3fSDimitry Andric void *SANITIZER_CDECL __sanitizer_reallocarray(void *ptr, size_t nmemb,
1035f757f3fSDimitry Andric                                                size_t size);
1045f757f3fSDimitry 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