From 417f7c3f37eb8fd571db1350df433b5d719f4818 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marek=20Bia=C5=82ow=C4=85s?= Date: Fri, 3 Nov 2023 13:15:29 +0100 Subject: [PATCH] assert: use abort() instead of exit() - atexit functions should not be called - stream synchronization is optional (might lead to deadlocks) - the exit code should indicate the program was killed with SIGABRT JIRA: RTOS-668 --- include/assert.h | 4 +--- include/stdlib.h | 2 +- stdlib/abort.c | 22 ++++++++++++++++++---- 3 files changed, 20 insertions(+), 8 deletions(-) diff --git a/include/assert.h b/include/assert.h index 31b4e153..d47ec232 100644 --- a/include/assert.h +++ b/include/assert.h @@ -28,9 +28,7 @@ extern "C" { #ifndef NDEBUG #define assert(__expr) \ - ((__expr) \ - ? (void)0 \ - : ({ printf("Assertion '%s' failed in file %s:%d, function %s.\n", #__expr, __FILE__, __LINE__, __func__); exit(1);})) + ((__expr) ? (void)0 : ({ printf("Assertion '%s' failed in file %s:%d, function %s.\n", #__expr, __FILE__, __LINE__, __func__); abort(); })) #else diff --git a/include/stdlib.h b/include/stdlib.h index ae452429..4a2b09d7 100644 --- a/include/stdlib.h +++ b/include/stdlib.h @@ -122,7 +122,7 @@ extern size_t malloc_usable_size(void *ptr); /* Causes an abnormal program termination. */ -extern void abort(void); +extern void abort(void) __attribute__((__noreturn__)); /* Causes the specified function func to be called when the program terminates normally. */ diff --git a/stdlib/abort.c b/stdlib/abort.c index e525dc83..8bb3e87e 100644 --- a/stdlib/abort.c +++ b/stdlib/abort.c @@ -5,8 +5,8 @@ * * abort.c * - * Copyright 2018 Phoenix Systems - * Author: Kamil Amanowicz + * Copyright 2018,2023 Phoenix Systems + * Author: Kamil Amanowicz, Marek Bialowas * * This file is part of Phoenix-RTOS. * @@ -14,10 +14,24 @@ */ #include #include +#include void abort(void) { + /* unblock SIGABRT */ + sigset_t sigs; + sigemptyset(&sigs); + sigaddset(&sigs, SIGABRT); + sigprocmask(SIG_UNBLOCK, &sigs, NULL); + + raise(SIGABRT); + + /* should not return even for user-specified signal handler, but retry with default handler */ + signal(SIGABRT, SIG_DFL); raise(SIGABRT); - while(1) - exit(EXIT_FAILURE); + + /* should not return, finish program however we can */ + for (;;) { + _exit(127); + } }