⚠ This page is served via a proxy. Original site: https://github.com
This service does not collect credentials or authentication data.
Skip to content

Conversation

@jserv
Copy link
Contributor

@jserv jserv commented Jan 28, 2026

The CRITICAL_ENTER/LEAVE macros unconditionally enabled interrupts on leave, which corrupts interrupt state when called from ISR context (where MIE is already 0). Replace with nesting-aware save/restore: CRITICAL_ENTER saves MIE on first entry, CRITICAL_LEAVE restores it on last exit. Add underflow guard to CRITICAL_LEAVE.

This fixes mo_logger_enqueue() to use CRITICAL_ENTER instead of mo_mutex_lock so the [ISR-SAFE] contract is actually honored -- mutex blocks, which deadlocks in ISR context.

It also fixes realloc() missing CRITICAL_ENTER before fast paths that called CRITICAL_LEAVE, and add defensive CRITICAL_LEAVE before panic() in malloc().

ISR Context Safety Guide is added to hal-interrupt.md with function tables, callback patterns, and ISR-to-task communication examples.


Summary by cubic

Make critical sections ISR-safe by saving/restoring interrupt state with nesting. Prevents interrupt-state corruption and ISR deadlocks; updates logger/allocator and adds clear ISR safety guidance.

  • Bug Fixes

    • CRITICAL_ENTER/LEAVE now track nesting and restore the original MIE; added underflow guard.
    • mo_logger_enqueue uses CRITICAL_ENTER (no mutex), making logging safe in ISR context.
    • Fixed realloc/malloc critical-section pairing; added defensive CRITICAL_LEAVE before panic().
    • Added kernel state for nesting and saved MIE to support correct restore behavior.
  • New Features

    • Added ISR-safe UART helpers: isr_puts() and isr_putx() for emergency debug output.
    • Documented ISR safety across APIs with [ISR-SAFE]/[TASK-ONLY] annotations; timer management is ISR-safe and callback rules are explicit.
    • Added an ISR Context Safety Guide with function tables, callback patterns, and ISR-to-task communication examples.
    • Updated examples and comments to show ISR-safe logging from timer callbacks.

Written for commit 3474abb. Summary will update on new commits.

cubic-dev-ai[bot]

This comment was marked as resolved.

Comment on lines 307 to 326
return (void *) (old_block + 1);
}

CRITICAL_LEAVE();

void *new_buf = malloc(size);
if (new_buf) {
memcpy(new_buf, ptr, min(old_size, size));
free(ptr);
}

return new_buf;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The realloc function drops the critical section before calling malloc(size), opening a race window. For safety, realloc should typically hold the lock throughout the operation, or strictly enforce that shared pointers must be protected by external locks.

*/
volatile uint32_t critical_nesting = 0;
volatile int32_t critical_saved_mie = 0;

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I noticed that critical_nesting and critical_saved_mie are implemented as global variables. Is it intended for Single Core? If so, it would be better to store them in TCB, or at least document this limitation in the file. Otherwise, it is not thread-safe. Any future change allowing preemption or yielding within a critical section would corrupt the interrupt state.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it intended for Single Core? If so, it would be better to store them in TCB, or at least document this limitation in the file.

The comments were refined to address SMP awareness.

@sysprog21 sysprog21 deleted a comment from github-actions bot Jan 29, 2026
@jserv jserv requested a review from HeatCrab January 29, 2026 13:11
The CRITICAL_ENTER/LEAVE macros unconditionally enabled interrupts on
leave, which corrupts interrupt state when called from ISR context
(where MIE is already 0). Replace with nesting-aware save/restore:
CRITICAL_ENTER saves MIE on first entry, CRITICAL_LEAVE restores it on
last exit. Add underflow guard to CRITICAL_LEAVE.

This fixes mo_logger_enqueue() to use CRITICAL_ENTER instead of
mo_mutex_lock so the [ISR-SAFE] contract is actually honored -- mutex
blocks, which deadlocks in ISR context.

It also fixes realloc() missing CRITICAL_ENTER before fast paths that
called CRITICAL_LEAVE, and add defensive CRITICAL_LEAVE before panic()
in malloc().

ISR Context Safety Guide is added to hal-interrupt.md with function
tables, callback patterns, and ISR-to-task communication examples.
@github-actions
Copy link

Linmo CI Test Results

Overall Status: ✅ passed
Timestamp: 2026-01-29T13:18:20+00:00
Commit: a2aedcb

Toolchain Results

Toolchain Build Crash Test Functional
GNU ✅ passed ✅ passed ✅ passed
LLVM ✅ passed ⏭️ skipped ⏭️ skipped

Application Tests

App GNU LLVM
cond ✅ passed ⏭️ skipped
coop ✅ passed ⏭️ skipped
cpubench ✅ passed ⏭️ skipped
echo ✅ passed ⏭️ skipped
hello ✅ passed ⏭️ skipped
mqueues ✅ passed ⏭️ skipped
mutex ✅ passed ⏭️ skipped
pipes ✅ passed ⏭️ skipped
pipes_small ✅ passed ⏭️ skipped
pipes_struct ✅ passed ⏭️ skipped
prodcons ✅ passed ⏭️ skipped
progress ✅ passed ⏭️ skipped
rtsched ✅ passed ⏭️ skipped
semaphore ✅ passed ⏭️ skipped
suspend ✅ passed ⏭️ skipped
test64 ✅ passed ⏭️ skipped
test_libc ✅ passed ⏭️ skipped
timer ✅ passed ⏭️ skipped
timer_kill ✅ passed ⏭️ skipped
umode ⚠️ ⏭️ skipped

Functional Test Details

Test GNU LLVM
mutex:data_consistency ✅ passed ⏭️ skipped
mutex:fairness ✅ passed ⏭️ skipped
mutex:mutual_exclusion ✅ passed ⏭️ skipped
mutex:overall ✅ passed ⏭️ skipped
semaphore:overall ✅ passed ⏭️ skipped
umode:[exception]_illegal_instruction ✅ passed ⚠️
umode:pass:_sys_tid()_returned ✅ passed ⚠️
umode:pass:_sys_uptime()_returned ✅ passed ⚠️

Report generated from test-summary.toml

@jserv jserv merged commit 905d252 into main Jan 29, 2026
6 checks passed
@jserv jserv deleted the isr-safe branch January 29, 2026 13:56
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants