/src/abseil-cpp/absl/base/internal/hardening.h
Line | Count | Source |
1 | | // |
2 | | // Copyright 2026 The Abseil Authors. |
3 | | // |
4 | | // Licensed under the Apache License, Version 2.0 (the "License"); |
5 | | // you may not use this file except in compliance with the License. |
6 | | // You may obtain a copy of the License at |
7 | | // |
8 | | // https://www.apache.org/licenses/LICENSE-2.0 |
9 | | // |
10 | | // Unless required by applicable law or agreed to in writing, software |
11 | | // distributed under the License is distributed on an "AS IS" BASIS, |
12 | | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
13 | | // See the License for the specific language governing permissions and |
14 | | // limitations under the License. |
15 | | // |
16 | | // ----------------------------------------------------------------------------- |
17 | | // File: hardening.h |
18 | | // ----------------------------------------------------------------------------- |
19 | | // |
20 | | // This header file defines macros and functions for performing Abseil |
21 | | // hardening checks and aborts. |
22 | | |
23 | | #ifndef ABSL_BASE_INTERNAL_HARDENING_H_ |
24 | | #define ABSL_BASE_INTERNAL_HARDENING_H_ |
25 | | |
26 | | #include "absl/base/attributes.h" |
27 | | #include "absl/base/config.h" |
28 | | #include "absl/base/macros.h" |
29 | | #include "absl/base/options.h" |
30 | | |
31 | | #ifdef ABSL_INTERNAL_ATTRIBUTE_NO_MERGE |
32 | | #error ABSL_INTERNAL_ATTRIBUTE_NO_MERGE cannot be directly set |
33 | | #elif ABSL_HAVE_CPP_ATTRIBUTE(clang::nomerge) |
34 | | #define ABSL_INTERNAL_ATTRIBUTE_NO_MERGE [[clang::nomerge]] |
35 | | #else |
36 | | #define ABSL_INTERNAL_ATTRIBUTE_NO_MERGE |
37 | | #endif |
38 | | |
39 | | namespace absl { |
40 | | ABSL_NAMESPACE_BEGIN |
41 | | |
42 | | namespace base_internal { |
43 | | |
44 | | [[noreturn]] ABSL_ATTRIBUTE_NOINLINE void HardeningAbort(); |
45 | | |
46 | | // `HardeningAssert` performs runtime checks when Abseil Hardening is enabled, |
47 | | // even if `NDEBUG` is defined. |
48 | | // |
49 | | // When `NDEBUG` is not defined, `HardeningAssert`'s behavior is identical to |
50 | | // `ABSL_ASSERT`. |
51 | | // |
52 | | // Prefer a more specific assertion function over this more general one, |
53 | | // as assertion functions which perform the comparison themselves |
54 | | // can have the cost of the comparison attributed to them. |
55 | 0 | constexpr void HardeningAssert(bool cond) { |
56 | 0 | ABSL_ASSERT(cond); |
57 | 0 | #if (ABSL_OPTION_HARDENED == 1 || ABSL_OPTION_HARDENED == 2) && defined(NDEBUG) |
58 | 0 | if (ABSL_PREDICT_FALSE(!cond)) { |
59 | 0 | ABSL_INTERNAL_ATTRIBUTE_NO_MERGE HardeningAbort(); |
60 | 0 | } |
61 | 0 | #endif |
62 | 0 | } |
63 | | |
64 | | // `HardeningAssertSlow` is used to perform runtime checks which are too |
65 | | // computationally expensive to enable widely by default. |
66 | | // |
67 | | // When `NDEBUG` is not defined, `HardeningAssertSlow`'s behavior is identical |
68 | | // to `ABSL_ASSERT`. |
69 | 0 | constexpr void HardeningAssertSlow(bool cond) { |
70 | 0 | ABSL_ASSERT(cond); |
71 | 0 | #if (ABSL_OPTION_HARDENED == 1) && defined(NDEBUG) |
72 | 0 | if (ABSL_PREDICT_FALSE(!cond)) { |
73 | 0 | ABSL_INTERNAL_ATTRIBUTE_NO_MERGE HardeningAbort(); |
74 | 0 | } |
75 | 0 | #endif |
76 | 0 | } |
77 | | |
78 | | template <typename T> |
79 | 0 | constexpr void HardeningAssertGT(T val1, T val2) { |
80 | 0 | ABSL_ASSERT(val1 > val2); |
81 | 0 | #if (ABSL_OPTION_HARDENED == 1 || ABSL_OPTION_HARDENED == 2) && defined(NDEBUG) |
82 | 0 | if (!ABSL_PREDICT_TRUE(val1 > val2)) { |
83 | 0 | ABSL_INTERNAL_ATTRIBUTE_NO_MERGE HardeningAbort(); |
84 | 0 | } |
85 | 0 | #endif |
86 | 0 | } |
87 | | |
88 | | template <typename T> |
89 | 0 | constexpr void HardeningAssertGE(T val1, T val2) { |
90 | 0 | ABSL_ASSERT(val1 >= val2); |
91 | | #if (ABSL_OPTION_HARDENED == 1 || ABSL_OPTION_HARDENED == 2) && defined(NDEBUG) |
92 | | if (!ABSL_PREDICT_TRUE(val1 >= val2)) { |
93 | | ABSL_INTERNAL_ATTRIBUTE_NO_MERGE HardeningAbort(); |
94 | | } |
95 | | #endif |
96 | 0 | } |
97 | | |
98 | | template <typename T> |
99 | 996k | constexpr void HardeningAssertLT(T val1, T val2) { |
100 | 996k | ABSL_ASSERT(val1 < val2); |
101 | | #if (ABSL_OPTION_HARDENED == 1 || ABSL_OPTION_HARDENED == 2) && defined(NDEBUG) |
102 | | if (!ABSL_PREDICT_TRUE(val1 < val2)) { |
103 | | ABSL_INTERNAL_ATTRIBUTE_NO_MERGE HardeningAbort(); |
104 | | } |
105 | | #endif |
106 | 996k | } |
107 | | |
108 | | template <typename T> |
109 | 0 | constexpr void HardeningAssertLE(T val1, T val2) { |
110 | 0 | ABSL_ASSERT(val1 <= val2); |
111 | | #if (ABSL_OPTION_HARDENED == 1 || ABSL_OPTION_HARDENED == 2) && defined(NDEBUG) |
112 | | if (!ABSL_PREDICT_TRUE(val1 <= val2)) { |
113 | | ABSL_INTERNAL_ATTRIBUTE_NO_MERGE HardeningAbort(); |
114 | | } |
115 | | #endif |
116 | 0 | } |
117 | | |
118 | 0 | constexpr void HardeningAssertInBounds(size_t index, size_t size) { |
119 | 0 | HardeningAssertLT(index, size); |
120 | 0 | } |
121 | | |
122 | | template <typename T> |
123 | | constexpr void HardeningAssertNonEmpty(const T& container) { |
124 | | ABSL_ASSERT(!container.empty()); |
125 | | #if (ABSL_OPTION_HARDENED == 1 || ABSL_OPTION_HARDENED == 2) && defined(NDEBUG) |
126 | | if (ABSL_PREDICT_FALSE(container.empty())) { |
127 | | ABSL_INTERNAL_ATTRIBUTE_NO_MERGE HardeningAbort(); |
128 | | } |
129 | | #endif |
130 | | } |
131 | | |
132 | | template <typename T> |
133 | | constexpr void HardeningAssertNonNull(T ptr) { |
134 | | ABSL_ASSERT(ptr != nullptr); |
135 | | #if (ABSL_OPTION_HARDENED == 1 || ABSL_OPTION_HARDENED == 2) && defined(NDEBUG) |
136 | | if (ABSL_PREDICT_FALSE(ptr == nullptr)) { |
137 | | ABSL_INTERNAL_ATTRIBUTE_NO_MERGE HardeningAbort(); |
138 | | } |
139 | | #endif |
140 | | } |
141 | | |
142 | | } // namespace base_internal |
143 | | |
144 | | ABSL_NAMESPACE_END |
145 | | } // namespace absl |
146 | | |
147 | | #undef ABSL_INTERNAL_ATTRIBUTE_NO_MERGE |
148 | | |
149 | | #endif // ABSL_BASE_INTERNAL_HARDENING_H_ |