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
diff --git a/absl/base/macros.h b/absl/base/macros.h index b318f11..ccc86ab 100644 --- a/absl/base/macros.h +++ b/absl/base/macros.h
@@ -34,6 +34,7 @@ #include "absl/base/attributes.h" #include "absl/base/config.h" #include "absl/base/optimization.h" +#include "absl/base/options.h" #include "absl/base/port.h" // ABSL_ARRAYSIZE() @@ -120,7 +121,7 @@ // // See `ABSL_OPTION_HARDENED` in `absl/base/options.h` for more information on // hardened mode. -#if ABSL_OPTION_HARDENED == 1 && defined(NDEBUG) +#if (ABSL_OPTION_HARDENED == 1 || ABSL_OPTION_HARDENED == 2) && defined(NDEBUG) #define ABSL_HARDENING_ASSERT(expr) \ (ABSL_PREDICT_TRUE((expr)) ? static_cast<void>(0) \ : [] { ABSL_INTERNAL_HARDENING_ABORT(); }()) @@ -128,6 +129,25 @@ #define ABSL_HARDENING_ASSERT(expr) ABSL_ASSERT(expr) #endif +// ABSL_HARDENING_ASSERT_SLOW() +// +// `ABSL_HARDENING_ASSERT()` is like `ABSL_HARDENING_ASSERT()`, +// but specifically for assertions whose predicates are too slow +// to be enabled in many applications. +// +// When `NDEBUG` is not defined, `ABSL_HARDENING_ASSERT_SLOW()` is identical to +// `ABSL_ASSERT()`. +// +// See `ABSL_OPTION_HARDENED` in `absl/base/options.h` for more information on +// hardened mode. +#if ABSL_OPTION_HARDENED == 1 && defined(NDEBUG) +#define ABSL_HARDENING_ASSERT_SLOW(expr) \ + (ABSL_PREDICT_TRUE((expr)) ? static_cast<void>(0) \ + : [] { ABSL_INTERNAL_HARDENING_ABORT(); }()) +#else +#define ABSL_HARDENING_ASSERT_SLOW(expr) ABSL_ASSERT(expr) +#endif + #ifdef ABSL_HAVE_EXCEPTIONS #define ABSL_INTERNAL_TRY try #define ABSL_INTERNAL_CATCH_ANY catch (...)
diff --git a/absl/base/options.h b/absl/base/options.h index a169658..4b70446 100644 --- a/absl/base/options.h +++ b/absl/base/options.h
@@ -235,7 +235,10 @@ // // A value of 0 means that "hardened" mode is not enabled. // -// A value of 1 means that "hardened" mode is enabled. +// A value of 1 means that "hardened" mode is enabled with all checks. +// +// A value of 2 means that "hardened" mode is partially enabled, with +// only a subset of checks chosen to minimize performance impact. // // Hardened builds have additional security checks enabled when `NDEBUG` is // defined. Defining `NDEBUG` is normally used to turn `assert()` macro into a