• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 //===-- Convenient sanitizer macros -----------------------------*- 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 #ifndef LLVM_LIBC_SRC___SUPPORT_MACROS_SANITIZER_H
10 #define LLVM_LIBC_SRC___SUPPORT_MACROS_SANITIZER_H
11 
12 #include "src/__support/macros/config.h" //LIBC_HAS_FEATURE
13 
14 //-----------------------------------------------------------------------------
15 // Properties to check the presence or absence or sanitizers
16 //-----------------------------------------------------------------------------
17 
18 // MemorySanitizer (MSan) is a detector of uninitialized reads. It consists of
19 // a compiler instrumentation module and a run-time library. The
20 // MEMORY_SANITIZER macro is deprecated but we will continue to honor it for
21 // now.
22 #ifdef LIBC_HAVE_MEMORY_SANITIZER
23 #error "LIBC_HAVE_MEMORY_SANITIZER cannot be directly set."
24 #elif defined(MEMORY_SANITIZER) || defined(__SANITIZE_MEMORY__) ||             \
25     (LIBC_HAS_FEATURE(memory_sanitizer) && !defined(__native_client__))
26 #define LIBC_HAVE_MEMORY_SANITIZER
27 #endif
28 
29 // AddressSanitizer (ASan) is a fast memory error detector. The
30 // ADDRESS_SANITIZER macro is deprecated but we will continue to honor it for
31 // now.
32 #ifdef LIBC_HAVE_ADDRESS_SANITIZER
33 #error "LIBC_HAVE_ADDRESS_SANITIZER cannot be directly set."
34 #elif defined(ADDRESS_SANITIZER) || defined(__SANITIZE_ADDRESS__) ||           \
35     LIBC_HAS_FEATURE(address_sanitizer)
36 #define LIBC_HAVE_ADDRESS_SANITIZER
37 #endif
38 
39 // HWAddressSanitizer (HWASan) is a fast, low memory overhead error detector.
40 #ifdef LIBC_HAVE_HWADDRESS_SANITIZER
41 #error "LIBC_HAVE_HWADDRESS_SANITIZER cannot be directly set."
42 #elif LIBC_HAS_FEATURE(hwaddress_sanitizer)
43 #define LIBC_HAVE_HWADDRESS_SANITIZER
44 #endif
45 
46 //-----------------------------------------------------------------------------
47 // Functions to unpoison memory
48 //-----------------------------------------------------------------------------
49 
50 #if defined(LIBC_HAVE_MEMORY_SANITIZER)
51 // Only perform MSAN unpoison in non-constexpr context.
52 #include <sanitizer/msan_interface.h>
53 #define MSAN_UNPOISON(addr, size)                                              \
54   do {                                                                         \
55     if (!__builtin_is_constant_evaluated())                                    \
56       __msan_unpoison(addr, size);                                             \
57   } while (0)
58 #else
59 #define MSAN_UNPOISON(ptr, size)
60 #endif
61 
62 #ifdef LIBC_HAVE_ADDRESS_SANITIZER
63 #include <sanitizer/asan_interface.h>
64 #define ASAN_POISON_MEMORY_REGION(addr, size)                                  \
65   __asan_poison_memory_region((addr), (size))
66 #define ASAN_UNPOISON_MEMORY_REGION(addr, size)                                \
67   __asan_unpoison_memory_region((addr), (size))
68 #else
69 #define ASAN_POISON_MEMORY_REGION(addr, size) ((void)(addr), (void)(size))
70 #define ASAN_UNPOISON_MEMORY_REGION(addr, size) ((void)(addr), (void)(size))
71 #endif
72 
73 #endif // LLVM_LIBC_SRC___SUPPORT_MACROS_SANITIZER_H
74