Coverage Report

Created: 2026-09-14 06:45

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