2 //===----------------------------------------------------------------------===//
4 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
5 // See https://llvm.org/LICENSE.txt for license information.
6 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
8 //===----------------------------------------------------------------------===//
10 #ifndef _LIBCPP___ASSERT
11 #define _LIBCPP___ASSERT
13 #include <__assertion_handler> // Note: this include is generated by CMake and is potentially vendor-provided.
16 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
17 # pragma GCC system_header
20 #define _LIBCPP_ASSERT(expression, message) \
21 (__builtin_expect(static_cast<bool>(expression), 1) \
23 : _LIBCPP_ASSERTION_HANDLER(__FILE__ ":" _LIBCPP_TOSTRING(__LINE__) ": assertion " _LIBCPP_TOSTRING( \
24 expression) " failed: " message "\n"))
26 // WARNING: __builtin_assume can currently inhibit optimizations. Only add assumptions with a clear
27 // optimization intent. See https://discourse.llvm.org/t/llvm-assume-blocks-optimization/71609 for a
29 #if __has_builtin(__builtin_assume)
30 # define _LIBCPP_ASSUME(expression) \
31 (_LIBCPP_DIAGNOSTIC_PUSH _LIBCPP_CLANG_DIAGNOSTIC_IGNORED("-Wassume") \
32 __builtin_assume(static_cast<bool>(expression)) _LIBCPP_DIAGNOSTIC_POP)
34 # define _LIBCPP_ASSUME(expression) ((void)0)
38 // Fast hardening mode checks.
40 #if _LIBCPP_HARDENING_MODE == _LIBCPP_HARDENING_MODE_FAST
43 # define _LIBCPP_ASSERT_VALID_INPUT_RANGE(expression, message) _LIBCPP_ASSERT(expression, message)
44 # define _LIBCPP_ASSERT_VALID_ELEMENT_ACCESS(expression, message) _LIBCPP_ASSERT(expression, message)
46 // On most modern platforms, dereferencing a null pointer does not lead to an actual memory access.
47 # define _LIBCPP_ASSERT_NON_NULL(expression, message) ((void)0)
48 // Overlapping ranges will make algorithms produce incorrect results but don't directly lead to a security
50 # define _LIBCPP_ASSERT_NON_OVERLAPPING_RANGES(expression, message) ((void)0)
51 # define _LIBCPP_ASSERT_VALID_DEALLOCATION(expression, message) ((void)0)
52 # define _LIBCPP_ASSERT_VALID_EXTERNAL_API_CALL(expression, message) ((void)0)
53 # define _LIBCPP_ASSERT_COMPATIBLE_ALLOCATOR(expression, message) ((void)0)
54 # define _LIBCPP_ASSERT_ARGUMENT_WITHIN_DOMAIN(expression, message) ((void)0)
55 # define _LIBCPP_ASSERT_PEDANTIC(expression, message) ((void)0)
56 # define _LIBCPP_ASSERT_SEMANTIC_REQUIREMENT(expression, message) ((void)0)
57 # define _LIBCPP_ASSERT_INTERNAL(expression, message) ((void)0)
58 # define _LIBCPP_ASSERT_UNCATEGORIZED(expression, message) ((void)0)
60 // Extensive hardening mode checks.
62 #elif _LIBCPP_HARDENING_MODE == _LIBCPP_HARDENING_MODE_EXTENSIVE
65 # define _LIBCPP_ASSERT_VALID_INPUT_RANGE(expression, message) _LIBCPP_ASSERT(expression, message)
66 # define _LIBCPP_ASSERT_VALID_ELEMENT_ACCESS(expression, message) _LIBCPP_ASSERT(expression, message)
67 # define _LIBCPP_ASSERT_NON_NULL(expression, message) _LIBCPP_ASSERT(expression, message)
68 # define _LIBCPP_ASSERT_NON_OVERLAPPING_RANGES(expression, message) _LIBCPP_ASSERT(expression, message)
69 # define _LIBCPP_ASSERT_VALID_DEALLOCATION(expression, message) _LIBCPP_ASSERT(expression, message)
70 # define _LIBCPP_ASSERT_VALID_EXTERNAL_API_CALL(expression, message) _LIBCPP_ASSERT(expression, message)
71 # define _LIBCPP_ASSERT_COMPATIBLE_ALLOCATOR(expression, message) _LIBCPP_ASSERT(expression, message)
72 # define _LIBCPP_ASSERT_ARGUMENT_WITHIN_DOMAIN(expression, message) _LIBCPP_ASSERT(expression, message)
73 # define _LIBCPP_ASSERT_PEDANTIC(expression, message) _LIBCPP_ASSERT(expression, message)
74 # define _LIBCPP_ASSERT_UNCATEGORIZED(expression, message) _LIBCPP_ASSERT(expression, message)
76 # define _LIBCPP_ASSERT_SEMANTIC_REQUIREMENT(expression, message) ((void)0)
77 # define _LIBCPP_ASSERT_INTERNAL(expression, message) ((void)0)
79 // Debug hardening mode checks.
81 #elif _LIBCPP_HARDENING_MODE == _LIBCPP_HARDENING_MODE_DEBUG
83 // All checks enabled.
84 # define _LIBCPP_ASSERT_VALID_INPUT_RANGE(expression, message) _LIBCPP_ASSERT(expression, message)
85 # define _LIBCPP_ASSERT_VALID_ELEMENT_ACCESS(expression, message) _LIBCPP_ASSERT(expression, message)
86 # define _LIBCPP_ASSERT_NON_NULL(expression, message) _LIBCPP_ASSERT(expression, message)
87 # define _LIBCPP_ASSERT_NON_OVERLAPPING_RANGES(expression, message) _LIBCPP_ASSERT(expression, message)
88 # define _LIBCPP_ASSERT_VALID_DEALLOCATION(expression, message) _LIBCPP_ASSERT(expression, message)
89 # define _LIBCPP_ASSERT_VALID_EXTERNAL_API_CALL(expression, message) _LIBCPP_ASSERT(expression, message)
90 # define _LIBCPP_ASSERT_COMPATIBLE_ALLOCATOR(expression, message) _LIBCPP_ASSERT(expression, message)
91 # define _LIBCPP_ASSERT_ARGUMENT_WITHIN_DOMAIN(expression, message) _LIBCPP_ASSERT(expression, message)
92 # define _LIBCPP_ASSERT_PEDANTIC(expression, message) _LIBCPP_ASSERT(expression, message)
93 # define _LIBCPP_ASSERT_SEMANTIC_REQUIREMENT(expression, message) _LIBCPP_ASSERT(expression, message)
94 # define _LIBCPP_ASSERT_INTERNAL(expression, message) _LIBCPP_ASSERT(expression, message)
95 # define _LIBCPP_ASSERT_UNCATEGORIZED(expression, message) _LIBCPP_ASSERT(expression, message)
97 // Disable all checks if hardening is not enabled.
101 // All checks disabled.
102 # define _LIBCPP_ASSERT_VALID_INPUT_RANGE(expression, message) ((void)0)
103 # define _LIBCPP_ASSERT_VALID_ELEMENT_ACCESS(expression, message) ((void)0)
104 # define _LIBCPP_ASSERT_NON_NULL(expression, message) ((void)0)
105 # define _LIBCPP_ASSERT_NON_OVERLAPPING_RANGES(expression, message) ((void)0)
106 # define _LIBCPP_ASSERT_VALID_DEALLOCATION(expression, message) ((void)0)
107 # define _LIBCPP_ASSERT_VALID_EXTERNAL_API_CALL(expression, message) ((void)0)
108 # define _LIBCPP_ASSERT_COMPATIBLE_ALLOCATOR(expression, message) ((void)0)
109 # define _LIBCPP_ASSERT_ARGUMENT_WITHIN_DOMAIN(expression, message) ((void)0)
110 # define _LIBCPP_ASSERT_PEDANTIC(expression, message) ((void)0)
111 # define _LIBCPP_ASSERT_SEMANTIC_REQUIREMENT(expression, message) ((void)0)
112 # define _LIBCPP_ASSERT_INTERNAL(expression, message) ((void)0)
113 # define _LIBCPP_ASSERT_UNCATEGORIZED(expression, message) ((void)0)
115 #endif // _LIBCPP_HARDENING_MODE == _LIBCPP_HARDENING_MODE_FAST
118 #endif // _LIBCPP___ASSERT