Skip to content

Commit

Permalink
assert: use abort() instead of exit()
Browse files Browse the repository at this point in the history
- 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
  • Loading branch information
nalajcie committed Nov 3, 2023
1 parent 894855f commit 417f7c3
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 8 deletions.
4 changes: 1 addition & 3 deletions include/assert.h
Original file line number Diff line number Diff line change
Expand Up @@ -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


Expand Down
2 changes: 1 addition & 1 deletion include/stdlib.h
Original file line number Diff line number Diff line change
Expand Up @@ -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. */
Expand Down
22 changes: 18 additions & 4 deletions stdlib/abort.c
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,33 @@
*
* 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.
*
* %LICENSE%
*/
#include <stdlib.h>
#include <signal.h>
#include <unistd.h>

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);
}
}

0 comments on commit 417f7c3

Please sign in to comment.