/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 <cstddef> |
27 | | |
28 | | #include "absl/base/config.h" |
29 | | #include "absl/base/macros.h" |
30 | | #include "absl/base/options.h" |
31 | | |
32 | | namespace absl { |
33 | | ABSL_NAMESPACE_BEGIN |
34 | | |
35 | | namespace base_internal { |
36 | | |
37 | | void SetAbslHardeningEnabled(bool enabled); |
38 | | |
39 | | // `HardeningAssert` performs runtime checks when Abseil Hardening is enabled, |
40 | | // even if `NDEBUG` is defined. |
41 | | // |
42 | | // When `NDEBUG` is not defined, `HardeningAssert`'s behavior is identical to |
43 | | // `ABSL_ASSERT`. |
44 | | // |
45 | | // Prefer a more specific assertion function over this more general one, |
46 | | // as assertion functions which perform the comparison themselves |
47 | | // can have the cost of the comparison attributed to them. |
48 | 0 | constexpr void HardeningAssert(bool cond) { |
49 | 0 | ABSL_ASSERT(cond); |
50 | 0 | #if (ABSL_OPTION_HARDENED == 1 || ABSL_OPTION_HARDENED == 2) && defined(NDEBUG) |
51 | 0 | if (ABSL_PREDICT_FALSE(!cond)) { |
52 | 0 | ABSL_INTERNAL_HARDENING_ABORT(); |
53 | 0 | } |
54 | 0 | #endif |
55 | 0 | } |
56 | | |
57 | | // `HardeningAssertSlow` is used to perform runtime checks which are too |
58 | | // computationally expensive to enable widely by default. |
59 | | // |
60 | | // When `NDEBUG` is not defined, `HardeningAssertSlow`'s behavior is identical |
61 | | // to `ABSL_ASSERT`. |
62 | 0 | constexpr void HardeningAssertSlow(bool cond) { |
63 | 0 | ABSL_ASSERT(cond); |
64 | 0 | #if (ABSL_OPTION_HARDENED == 1) && defined(NDEBUG) |
65 | 0 | if (ABSL_PREDICT_FALSE(!cond)) { |
66 | 0 | ABSL_INTERNAL_HARDENING_ABORT(); |
67 | 0 | } |
68 | 0 | #endif |
69 | 0 | } |
70 | | |
71 | | template <typename T1, typename T2> |
72 | 0 | constexpr void HardeningAssertGT(T1 val1, T2 val2) { |
73 | 0 | ABSL_ASSERT(val1 > val2); |
74 | 0 | #if (ABSL_OPTION_HARDENED == 1 || ABSL_OPTION_HARDENED == 2) && defined(NDEBUG) |
75 | 0 | if (!ABSL_PREDICT_TRUE(val1 > val2)) { |
76 | 0 | ABSL_INTERNAL_HARDENING_ABORT(); |
77 | 0 | } |
78 | 0 | #endif |
79 | 0 | } |
80 | | |
81 | | template <typename T1, typename T2> |
82 | 0 | constexpr void HardeningAssertGE(T1 val1, T2 val2) { |
83 | 0 | ABSL_ASSERT(val1 >= val2); |
84 | | #if (ABSL_OPTION_HARDENED == 1 || ABSL_OPTION_HARDENED == 2) && defined(NDEBUG) |
85 | | if (!ABSL_PREDICT_TRUE(val1 >= val2)) { |
86 | | ABSL_INTERNAL_HARDENING_ABORT(); |
87 | | } |
88 | | #endif |
89 | 0 | } |
90 | | |
91 | | template <typename T1, typename T2> |
92 | 1.06M | constexpr void HardeningAssertLT(T1 val1, T2 val2) { |
93 | 1.06M | ABSL_ASSERT(val1 < val2); |
94 | | #if (ABSL_OPTION_HARDENED == 1 || ABSL_OPTION_HARDENED == 2) && defined(NDEBUG) |
95 | | if (!ABSL_PREDICT_TRUE(val1 < val2)) { |
96 | | ABSL_INTERNAL_HARDENING_ABORT(); |
97 | | } |
98 | | #endif |
99 | 1.06M | } |
100 | | |
101 | | template <typename T1, typename T2> |
102 | 0 | constexpr void HardeningAssertLE(T1 val1, T2 val2) { |
103 | 0 | ABSL_ASSERT(val1 <= val2); |
104 | | #if (ABSL_OPTION_HARDENED == 1 || ABSL_OPTION_HARDENED == 2) && defined(NDEBUG) |
105 | | if (!ABSL_PREDICT_TRUE(val1 <= val2)) { |
106 | | ABSL_INTERNAL_HARDENING_ABORT(); |
107 | | } |
108 | | #endif |
109 | 0 | } |
110 | | |
111 | 0 | constexpr void HardeningAssertInBounds(size_t index, size_t size) { |
112 | 0 | HardeningAssertLT(index, size); |
113 | 0 | } |
114 | | |
115 | | template <typename T> |
116 | | constexpr void HardeningAssertNonEmpty(const T& container) { |
117 | | ABSL_ASSERT(!container.empty()); |
118 | | #if (ABSL_OPTION_HARDENED == 1 || ABSL_OPTION_HARDENED == 2) && defined(NDEBUG) |
119 | | if (ABSL_PREDICT_FALSE(container.empty())) { |
120 | | ABSL_INTERNAL_HARDENING_ABORT(); |
121 | | } |
122 | | #endif |
123 | | } |
124 | | |
125 | | template <typename T> |
126 | | constexpr void HardeningAssertNonNull(T ptr) { |
127 | | ABSL_ASSERT(ptr != nullptr); |
128 | | #if (ABSL_OPTION_HARDENED == 1 || ABSL_OPTION_HARDENED == 2) && defined(NDEBUG) |
129 | | if (ABSL_PREDICT_FALSE(ptr == nullptr)) { |
130 | | ABSL_INTERNAL_HARDENING_ABORT(); |
131 | | } |
132 | | #endif |
133 | | } |
134 | | |
135 | | class ScopedSetAbslHardeningForTesting { |
136 | | private: |
137 | | bool prev_state_; |
138 | | |
139 | | public: |
140 | 0 | explicit ScopedSetAbslHardeningForTesting([[maybe_unused]] bool enabled) { |
141 | 0 | prev_state_ = false; |
142 | 0 | SetAbslHardeningEnabled(enabled); |
143 | 0 | } |
144 | 0 | ~ScopedSetAbslHardeningForTesting() { |
145 | 0 | absl::base_internal::SetAbslHardeningEnabled(prev_state_); |
146 | 0 | } |
147 | | }; |
148 | | |
149 | | } // namespace base_internal |
150 | | |
151 | | ABSL_NAMESPACE_END |
152 | | } // namespace absl |
153 | | |
154 | | #endif // ABSL_BASE_INTERNAL_HARDENING_H_ |