From da781e90c11e6b7ac645f0ce046f909d3d3118f5 Mon Sep 17 00:00:00 2001 From: Andreas Kling Date: Thu, 6 Oct 2022 13:35:46 +0200 Subject: [PATCH] AK: Print VERIFY() error messages in release builds Until now, VERIFY() failures would just cause a __builtin_trap() in release builds, which made them a bit too harsh. This commit adds an out-of-line helper function that prints the error before trapping. --- AK/Assertions.cpp | 20 ++++++++++++++++++++ AK/Assertions.h | 9 ++++++--- 2 files changed, 26 insertions(+), 3 deletions(-) create mode 100644 AK/Assertions.cpp diff --git a/AK/Assertions.cpp b/AK/Assertions.cpp new file mode 100644 index 0000000000..d8ac028345 --- /dev/null +++ b/AK/Assertions.cpp @@ -0,0 +1,20 @@ +/* + * Copyright (c) 2018-2020, Andreas Kling + * + * SPDX-License-Identifier: BSD-2-Clause + */ + +#include +#include + +#if !defined(KERNEL) && defined(NDEBUG) +extern "C" { + +void ak_verification_failed(char const* message) +{ + dbgln("VERIFICATION FAILED: {}", message); + __builtin_trap(); +} +} + +#endif diff --git a/AK/Assertions.h b/AK/Assertions.h index 648d9b9d49..ab628c14a6 100644 --- a/AK/Assertions.h +++ b/AK/Assertions.h @@ -13,9 +13,12 @@ # ifndef NDEBUG # define VERIFY assert # else -# define VERIFY(expr) \ - (__builtin_expect(!(expr), 0) \ - ? __builtin_trap() \ +# define __stringify_helper(x) # x +# define __stringify(x) __stringify_helper(x) +extern "C" __attribute__((noreturn)) void ak_verification_failed(char const*); +# define VERIFY(expr) \ + (__builtin_expect(!(expr), 0) \ + ? ak_verification_failed(#expr "\n" __FILE__ ":" __stringify(__LINE__)) \ : (void)0) # endif # define VERIFY_NOT_REACHED() VERIFY(false) /* NOLINT(cert-dcl03-c,misc-static-assert) No, this can't be static_assert, it's a runtime check */