Skip to content

hal: Bulletproof kernel operations on user memory#664

Draft
etiaro wants to merge 3 commits into
masterfrom
etiaro/threads-excjmp
Draft

hal: Bulletproof kernel operations on user memory#664
etiaro wants to merge 3 commits into
masterfrom
etiaro/threads-excjmp

Conversation

@etiaro

@etiaro etiaro commented Jun 25, 2025

Copy link
Copy Markdown
Contributor

Description

Inspired by BSD/openQNX exception handling routines, this draft introduces a mechanism with semantics similar to try - catch and based on idea similar to setjmp/longjmp by introducing

  • hal_createexcjmp that prepares "try-catch" context to be saved in TCB
  • threads_setexcjmp/threads_getexcjmp which manages access to dedicated thread_t struct field

Introduced a few basic function and preprocessor macro for easy and safe use of this exception handling mechanism.

On NOMMU targets, thanks to fixed configuration of accessible memory, simple access checks are good enough. That being said protection (R/W/E) checks are included in this PR to ensure no illegal accesses via kernel.

Motivation and Context

There are some events in kernel code that require operating on user's memory., which is vulnerable to TOCTOU class races.
There's no good way to ensure such memory operations won't trigger a processor exception apart from interrupting all threads that could access that memory fragment.

These mechanisms can also be used in the future to gently handle user stackoverflows and faulty signal trampolines that currently can become a cascade of kernel exceptions.

Types of changes

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to change)
  • Chore (refactoring, style fixes, git/CI config, submodule management, no code logic changes)

How Has This Been Tested?

  • Already covered by automatic testing.
  • New test added: (add PR link here).
  • Tested by hand on: (list targets here).

Checklist:

  • My change requires a change to the documentation.
  • I have updated the documentation accordingly.
  • I have added tests to cover my changes.
  • All new and existing linter checks and tests passed.
  • My changes generate no new compilation warnings for any of the targets.

Special treatment

  • This PR needs additional PRs to work (list the PRs, preferably in merge-order).
  • I will merge this PR by myself when appropriate.

@etiaro
etiaro requested a review from agkaminski June 25, 2025 09:02

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Summary of Changes

Hello @etiaro, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

This pull request introduces a robust exception handling mechanism within the kernel, inspired by try-catch blocks and setjmp/longjmp semantics. This mechanism is designed to safely manage operations that interact with user memory, preventing processor exceptions and improving system stability. A key application demonstrated is enhanced stack overflow handling on MMU-enabled devices.

Highlights

  • Kernel Exception Handling Framework: Implemented a setjmp/longjmp-like mechanism (hal_setexcjmp, hal_excjmp) at the hardware abstraction layer (HAL) for ARMv7-A and IA32 architectures. This allows kernel code to gracefully recover from processor exceptions (e.g., page faults) that occur during operations on user memory, providing a 'try-catch' semantic.
  • Thread-Specific Exception Context: Introduced a new excjmpctx field to the thread_t structure and added threads_setexcjmp/threads_getexcjmp functions. This enables the storage and retrieval of a thread-local exception jump context, ensuring that exception handling is managed on a per-thread basis.
  • Safer Signal Handling: Modified hal_cpuPushSignal to utilize the new exception handling framework. This change protects the operations involved in setting up signal contexts against potential memory faults, returning an -EFAULT error if a fault occurs, which then leads to the termination of the affected process in threads_setupUserReturn.
  • Internal Code Refactoring: Replaced all internal calls to the static _proc_current function with the public proc_current function across the proc/threads.c file, streamlining access to the current thread's context.
Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point in your pull request via creating an issue comment (i.e. comment on the pull request page) using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in issue comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist is currently in preview and may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments to provide feedback.

You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension.

Footnotes

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Code Review

The code changes introduce a mechanism for handling kernel unsafe operations, similar to try-catch, using setjmp/longjmp. It includes functions for setting and getting exception jump contexts, and integrates this mechanism into signal handling. The changes span multiple files, modifying assembly code, header files, and C code to implement this new exception handling feature.

Comment thread hal/armv7a/_interrupts.S Outdated
Comment thread hal/armv7a/_interrupts.S Outdated
Comment thread proc/threads.c Outdated
Comment thread hal/armv7a/_interrupts.S Outdated
Comment thread hal/armv7a/cpu.c Outdated
Comment thread proc/threads.c Outdated
Comment thread proc/threads.c Outdated
Comment thread proc/threads.c Outdated
Comment thread proc/threads.c Outdated
@etiaro
etiaro force-pushed the etiaro/threads-excjmp branch 2 times, most recently from 3c8b5ff to 1472edb Compare June 26, 2025 09:13
@etiaro
etiaro requested a review from lukileczo June 26, 2025 09:22
@etiaro etiaro changed the title threads: Kernel unsafe operations handling threads: Kernel unsafe user memory operations handling Sep 17, 2025
@etiaro etiaro changed the title threads: Kernel unsafe user memory operations handling threads: Kernel handling of unsafe operations on user memory Sep 17, 2025
@etiaro
etiaro force-pushed the etiaro/threads-excjmp branch from 1472edb to a1498e6 Compare October 20, 2025 16:25
@etiaro
etiaro force-pushed the etiaro/threads-excjmp branch 2 times, most recently from 10c64c7 to 331849c Compare April 13, 2026 10:06
@etiaro etiaro changed the title threads: Kernel handling of unsafe operations on user memory threads: Bulletproof kernel operations on user memory Apr 13, 2026
@etiaro
etiaro force-pushed the etiaro/threads-excjmp branch from 331849c to 8bd2ac3 Compare April 27, 2026 19:43
@etiaro etiaro changed the title threads: Bulletproof kernel operations on user memory hal: Bulletproof kernel operations on user memory Apr 28, 2026
@phoenix-rtos phoenix-rtos deleted a comment from github-actions Bot Apr 28, 2026
@etiaro
etiaro force-pushed the etiaro/threads-excjmp branch from 8bd2ac3 to 6cabf93 Compare July 20, 2026 10:02
Comment thread lib/usermem.h
Introduce usermem module with functions for safe operations on user
memory.

TASK: RTOS-1141
@etiaro
etiaro force-pushed the etiaro/threads-excjmp branch from 6cabf93 to c01d05e Compare July 20, 2026 10:49
@github-actions

github-actions Bot commented Jul 20, 2026

Copy link
Copy Markdown

Unit Test Results

11 307 tests  ±0   10 600 ✅ +1   54m 37s ⏱️ + 2m 10s
   690 suites ±0      707 💤 ±0 
     1 files   ±0        0 ❌  - 1 

Results for commit 31f1c4c. ± Comparison against base commit fb914a8.

♻️ This comment has been updated with latest results.

etiaro added 2 commits July 20, 2026 13:23
Introduce checks for read/write access to memory pointed to in syscall
arguments.

TASK: RTOS-1141
TBD: implement in all syscalls
@etiaro
etiaro force-pushed the etiaro/threads-excjmp branch from c01d05e to 31f1c4c Compare July 20, 2026 14:51
@etiaro
etiaro requested review from Darchiv and removed request for lukileczo July 20, 2026 14:52
@Darchiv
Darchiv requested review from oI0ck and ziemleszcz July 21, 2026 10:18
@etiaro
etiaro requested a review from agkaminski July 22, 2026 11:24
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