Skip to content

Commit 57748bd

Browse files
committed
Adjust error paths locking and returning
Various fixes in preparation for making sure error calls do not return. * Clear out handle_error's use of error_in_progress as a sort of spin lock; this is most likely to deadlock if ever activated, and conflicts with error's use of error_in_progress. Use a normal critical section lock. * Make error use same mbed_halt_system helper as mbed_error. * Make error's recursion check avoid print and proceed to halt, rather than returning. * Make mbed_error use error_in_progress to avoid recursion in same way as error() does. * Give mbed_halt_system its own recursion check in case of error in mbed_die - give it a simple fallback. * Make the in_progress things properly atomic, just in case.
1 parent c32984c commit 57748bd

File tree

1 file changed

+41
-40
lines changed

1 file changed

+41
-40
lines changed

platform/mbed_error.c

Lines changed: 41 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,8 @@ static void print_error_report(const mbed_error_ctx *ctx, const char *, const ch
3636
#define ERROR_REPORT(ctx, error_msg, error_filename, error_line) ((void) 0)
3737
#endif
3838

39-
static uint8_t error_in_progress = 0;
39+
static core_util_atomic_flag error_in_progress = CORE_UTIL_ATOMIC_FLAG_INIT;
40+
static core_util_atomic_flag halt_in_progress = CORE_UTIL_ATOMIC_FLAG_INIT;
4041
static int error_count = 0;
4142
static mbed_error_ctx first_error_ctx = {0};
4243
static mbed_error_ctx last_error_ctx = {0};
@@ -46,37 +47,41 @@ static mbed_error_status_t handle_error(mbed_error_status_t error_status, unsign
4647
//Helper function to halt the system
4748
static void mbed_halt_system(void)
4849
{
49-
//If not in ISR context exit, otherwise spin on WFI
50-
if (core_util_is_isr_active() || !core_util_are_interrupts_enabled()) {
50+
// Prevent recursion if halt is called again during halt attempt - try
51+
// something simple instead.
52+
if (core_util_atomic_flag_test_and_set(&halt_in_progress)) {
53+
core_util_critical_section_enter();
54+
__DSB();
5155
for (;;) {
52-
__WFI();
56+
__WFE(); // Not WFI, as don't want to wake for pending interrupts
5357
}
54-
} else {
55-
//exit eventually calls mbed_die
56-
exit(1);
5758
}
59+
60+
//If in ISR context, call mbed_die directly
61+
if (core_util_is_isr_active() || !core_util_are_interrupts_enabled()) {
62+
mbed_die();
63+
}
64+
65+
// In normal context, try orderly exit(1), which eventually calls mbed_die
66+
exit(1);
5867
}
5968

6069
WEAK void error(const char *format, ...)
6170
{
62-
63-
// Prevent recursion if error is called again
64-
if (error_in_progress) {
65-
return;
66-
}
67-
68-
//Call handle_error/print_error_report permanently setting error_in_progress flag
69-
handle_error(MBED_ERROR_UNKNOWN, 0, NULL, 0, MBED_CALLER_ADDR());
70-
ERROR_REPORT(&last_error_ctx, "Fatal Run-time error", NULL, 0);
71-
error_in_progress = 1;
71+
// Prevent recursion if error is called again during store+print attempt
72+
if (!core_util_atomic_flag_test_and_set(&error_in_progress)) {
73+
handle_error(MBED_ERROR_UNKNOWN, 0, NULL, 0, MBED_CALLER_ADDR());
74+
ERROR_REPORT(&last_error_ctx, "Fatal Run-time error", NULL, 0);
7275

7376
#ifndef NDEBUG
74-
va_list arg;
75-
va_start(arg, format);
76-
mbed_error_vprintf(format, arg);
77-
va_end(arg);
77+
va_list arg;
78+
va_start(arg, format);
79+
mbed_error_vprintf(format, arg);
80+
va_end(arg);
7881
#endif
79-
exit(1);
82+
}
83+
84+
mbed_halt_system();
8085
}
8186

8287
//Set an error status with the error handling system
@@ -91,18 +96,6 @@ static mbed_error_status_t handle_error(mbed_error_status_t error_status, unsign
9196
error_status = MBED_ERROR_INVALID_ARGUMENT;
9297
}
9398

94-
//Prevent corruption by holding out other callers
95-
//and we also need this until we remove the "error" call completely
96-
while (error_in_progress == 1);
97-
98-
//Use critsect here, as we don't want inadvertant modification of this global variable
99-
core_util_critical_section_enter();
100-
error_in_progress = 1;
101-
core_util_critical_section_exit();
102-
103-
//Increment error count
104-
error_count++;
105-
10699
//Clear the context capturing buffer
107100
memset(&current_error_ctx, 0, sizeof(mbed_error_ctx));
108101
//Capture error information
@@ -126,6 +119,12 @@ static mbed_error_status_t handle_error(mbed_error_status_t error_status, unsign
126119
current_error_ctx.error_line_number = line_number;
127120
#endif
128121

122+
//Prevent corruption by holding out other callers
123+
core_util_critical_section_enter();
124+
125+
//Increment error count
126+
error_count++;
127+
129128
//Capture the fist system error and store it
130129
if (error_count == 1) { //first error
131130
memcpy(&first_error_ctx, &current_error_ctx, sizeof(mbed_error_ctx));
@@ -144,7 +143,7 @@ static mbed_error_status_t handle_error(mbed_error_status_t error_status, unsign
144143
error_hook(&last_error_ctx);
145144
}
146145

147-
error_in_progress = 0;
146+
core_util_critical_section_exit();
148147

149148
return MBED_SUCCESS;
150149
}
@@ -179,13 +178,15 @@ mbed_error_status_t mbed_warning(mbed_error_status_t error_status, const char *e
179178
//Sets a fatal error, this function is marked WEAK to be able to override this for some tests
180179
WEAK mbed_error_status_t mbed_error(mbed_error_status_t error_status, const char *error_msg, unsigned int error_value, const char *filename, int line_number)
181180
{
182-
//set the error reported and then halt the system
183-
if (MBED_SUCCESS != handle_error(error_status, error_value, filename, line_number, MBED_CALLER_ADDR())) {
184-
return MBED_ERROR_FAILED_OPERATION;
181+
// Prevent recursion if error is called again during store+print attempt
182+
if (!core_util_atomic_flag_test_and_set(&error_in_progress)) {
183+
//set the error reported
184+
(void) handle_error(error_status, error_value, filename, line_number, MBED_CALLER_ADDR());
185+
186+
//On fatal errors print the error context/report
187+
ERROR_REPORT(&last_error_ctx, error_msg, filename, line_number);
185188
}
186189

187-
//On fatal errors print the error context/report
188-
ERROR_REPORT(&last_error_ctx, error_msg, filename, line_number);
189190
mbed_halt_system();
190191

191192
return MBED_ERROR_FAILED_OPERATION;

0 commit comments

Comments
 (0)