Skip to content

Commit 1031858

Browse files
Abseil Teamcopybara-github
authored andcommitted
Allow slow and fast abseil hardening checks to be enabled independently.
This change adds a new macro, ABSL_HARDENING_ASSERT_SLOW, which can be used for hardening assertions which have too great a performance impact to enable widely. It also adds an additional supported value for ABSL_OPTION_HARDENED to only enable assertions which are not slow. ABSL_OPTION_HARDENED=1 will continue to enable all hardening assertions, while ABSL_OPTION_HARDENED=2 will only enable assertions not marked as SLOW. PiperOrigin-RevId: 668976425 Change-Id: I12cb209715d5b359c4093cc8c62e16794d7729b3
1 parent 8f942c0 commit 1031858

File tree

2 files changed

+25
-2
lines changed

2 files changed

+25
-2
lines changed

absl/base/macros.h

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
#include "absl/base/attributes.h"
3535
#include "absl/base/config.h"
3636
#include "absl/base/optimization.h"
37+
#include "absl/base/options.h"
3738
#include "absl/base/port.h"
3839

3940
// ABSL_ARRAYSIZE()
@@ -120,14 +121,33 @@ ABSL_NAMESPACE_END
120121
//
121122
// See `ABSL_OPTION_HARDENED` in `absl/base/options.h` for more information on
122123
// hardened mode.
123-
#if ABSL_OPTION_HARDENED == 1 && defined(NDEBUG)
124+
#if (ABSL_OPTION_HARDENED == 1 || ABSL_OPTION_HARDENED == 2) && defined(NDEBUG)
124125
#define ABSL_HARDENING_ASSERT(expr) \
125126
(ABSL_PREDICT_TRUE((expr)) ? static_cast<void>(0) \
126127
: [] { ABSL_INTERNAL_HARDENING_ABORT(); }())
127128
#else
128129
#define ABSL_HARDENING_ASSERT(expr) ABSL_ASSERT(expr)
129130
#endif
130131

132+
// ABSL_HARDENING_ASSERT_SLOW()
133+
//
134+
// `ABSL_HARDENING_ASSERT()` is like `ABSL_HARDENING_ASSERT()`,
135+
// but specifically for assertions whose predicates are too slow
136+
// to be enabled in many applications.
137+
//
138+
// When `NDEBUG` is not defined, `ABSL_HARDENING_ASSERT_SLOW()` is identical to
139+
// `ABSL_ASSERT()`.
140+
//
141+
// See `ABSL_OPTION_HARDENED` in `absl/base/options.h` for more information on
142+
// hardened mode.
143+
#if ABSL_OPTION_HARDENED == 1 && defined(NDEBUG)
144+
#define ABSL_HARDENING_ASSERT_SLOW(expr) \
145+
(ABSL_PREDICT_TRUE((expr)) ? static_cast<void>(0) \
146+
: [] { ABSL_INTERNAL_HARDENING_ABORT(); }())
147+
#else
148+
#define ABSL_HARDENING_ASSERT_SLOW(expr) ABSL_ASSERT(expr)
149+
#endif
150+
131151
#ifdef ABSL_HAVE_EXCEPTIONS
132152
#define ABSL_INTERNAL_TRY try
133153
#define ABSL_INTERNAL_CATCH_ANY catch (...)

absl/base/options.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -235,7 +235,10 @@
235235
//
236236
// A value of 0 means that "hardened" mode is not enabled.
237237
//
238-
// A value of 1 means that "hardened" mode is enabled.
238+
// A value of 1 means that "hardened" mode is enabled with all checks.
239+
//
240+
// A value of 2 means that "hardened" mode is partially enabled, with
241+
// only a subset of checks chosen to minimize performance impact.
239242
//
240243
// Hardened builds have additional security checks enabled when `NDEBUG` is
241244
// defined. Defining `NDEBUG` is normally used to turn `assert()` macro into a

0 commit comments

Comments
 (0)