From: Jan Janssen Date: Wed, 11 Aug 2021 12:59:46 +0000 (+0200) Subject: sd-boot: Add assert implementation X-Git-Tag: v250-rc1~835^2~5 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=7a7267bf55cac0a90f6a8a83841c56fdebb1e69c;p=thirdparty%2Fsystemd.git sd-boot: Add assert implementation There is a ASSERT() macro from gnu-efi, but that does not show any output to ConOut. Having to do some additional setup just to get some debug output is tedious and outright difficult on real hardware. --- diff --git a/src/boot/efi/assert.c b/src/boot/efi/assert.c new file mode 100644 index 00000000000..350b2b01677 --- /dev/null +++ b/src/boot/efi/assert.c @@ -0,0 +1,15 @@ +/* SPDX-License-Identifier: LGPL-2.1-or-later */ + +#ifndef NDEBUG + +#include +#include +#include "util.h" + +void efi_assert(const char *expr, const char *file, unsigned line, const char *function) { + log_error_stall(L"systemd-boot assertion '%a' failed at %a:%u, function %a(). Halting.", expr, file, line, function); + for (;;) + uefi_call_wrapper(BS->Stall, 1, 60 * 1000 * 1000); +} + +#endif diff --git a/src/boot/efi/meson.build b/src/boot/efi/meson.build index 0a96a72d6ec..c96a16426a4 100644 --- a/src/boot/efi/meson.build +++ b/src/boot/efi/meson.build @@ -17,6 +17,7 @@ efi_headers = files(''' '''.split()) common_sources = ''' + assert.c disk.c graphics.c measure.c @@ -219,12 +220,16 @@ if have_gnu_efi compile_args += ['-Werror'] endif if get_option('buildtype') == 'debug' - compile_args += ['-ggdb', '-O0'] + compile_args += ['-ggdb', '-O0', '-DEFI_DEBUG'] elif get_option('buildtype') == 'debugoptimized' - compile_args += ['-ggdb', '-Og'] + compile_args += ['-ggdb', '-Og', '-DEFI_DEBUG'] else compile_args += ['-O2'] endif + if get_option('b_ndebug') == 'true' or ( + get_option('b_ndebug') == 'if-release' and ['plain', 'release'].contains(get_option('buildtype'))) + compile_args += ['-DNDEBUG'] + endif efi_ldflags = ['-T', efi_lds, '-shared', diff --git a/src/fundamental/macro-fundamental.h b/src/fundamental/macro-fundamental.h index 91b24a18264..461d37fa9e1 100644 --- a/src/fundamental/macro-fundamental.h +++ b/src/fundamental/macro-fundamental.h @@ -50,7 +50,14 @@ #define CONCATENATE(x, y) XCONCATENATE(x, y) #ifdef SD_BOOT -#define assert(expr) do {} while (false) + #ifdef NDEBUG + #define assert(expr) + #define assert_not_reached() __builtin_unreachable() + #else + void efi_assert(const char *expr, const char *file, unsigned line, const char *function) _noreturn_; + #define assert(expr) ({ _likely_(expr) ? VOID_0 : efi_assert(#expr, __FILE__, __LINE__, __PRETTY_FUNCTION__); }) + #define assert_not_reached() efi_assert("Code should not be reached", __FILE__, __LINE__, __PRETTY_FUNCTION__) + #endif #endif #if defined(static_assert)