Skip to content

Add Per-Thread stack guard overflow check#361

Merged
han-jiang277 merged 2 commits intovivoblueos:mainfrom
xuchang-vivo:xc/add_stack_guard
Mar 16, 2026
Merged

Add Per-Thread stack guard overflow check#361
han-jiang277 merged 2 commits intovivoblueos:mainfrom
xuchang-vivo:xc/add_stack_guard

Conversation

@xuchang-vivo
Copy link
Contributor

@xuchang-vivo xuchang-vivo commented Mar 12, 2026

This PR adds per-thread MPU stack-guard switching support for Cortex-M and completes the related validation/documentation.

    /// ------------------------------ <--- stack.top() == allocated buffer end
    /// | optional align gap (0..A)  |
    /// ------------------------------
    /// | Context for current thread |
    /// ------------------------------ <--- saved_sp (initial SP)
    /// |       stack region         |     (stack grows down)
    /// |           ...              |
    /// ------------------------------ <--- guard upper bound (if enabled)
    /// | stack guard region         |     (optional, usually no-access)
    /// ------------------------------ <--- stack.base()

What changed

  • Added per-thread guard programming in ARM v8-M MPU via update_thread_stack_guard(next).
  • Wired guard updates into the context-switch path, right before restoring the next thread context.
  • Wired guard setup into the initial scheduling path, so the first running thread gets a correct guard before entering thread mode.
  • Fixed guard alignment/range handling:
    • Guard start is aligned up to 32 bytes.
    • Guard region is constrained to stay within the target thread stack range.
    • This avoids protecting memory below stack_base.
  • Added a new unit test in kernel/src/lib.rs to verify that writing into a thread stack guard triggers MemManage.
  • Updated stack-layout comments in thread::init to clarify guard placement and fault behavior when SP is near/in guard on exception entry.

Verification

Executed and passed:

  • ninja -C out/qemu_mps3_an547.debug kernel/kernel:run_unittest
  • ninja -C out/qemu_mps3_an547.debug check_all

Relevant tests passing include:

  • test_mpu_sys_stack_guard_write_fault
  • test_mpu_thread_stack_guard_write_fault

@xuchang-vivo
Copy link
Contributor Author

build_prs

@github-actions
Copy link

@github-actions
Copy link

❌ Job failed. Failed jobs: check_format (failure), build_and_check_boards (failure), see https://github.com/vivoblueos/kernel/actions/runs/22993852808.

@xuchang-vivo
Copy link
Contributor Author

build_prs

@github-actions
Copy link

@github-actions
Copy link

✅ All jobs completed successfully, see https://github.com/vivoblueos/kernel/actions/runs/22994193994.

@han-jiang277 han-jiang277 merged commit 725cd0a into vivoblueos:main Mar 16, 2026
1 check passed
han-jiang277 pushed a commit to han-jiang277/kernel that referenced this pull request Mar 19, 2026
```
    /// ------------------------------ <--- stack.top() == allocated buffer end
    /// | optional align gap (0..A)  |
    /// ------------------------------
    /// | Context for current thread |
    /// ------------------------------ <--- saved_sp (initial SP)
    /// |       stack region         |     (stack grows down)
    /// |           ...              |
    /// ------------------------------ <--- guard upper bound (if enabled)
    /// | stack guard region         |     (optional, usually no-access)
    /// ------------------------------ <--- stack.base()
```
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.

2 participants