[Feature][Rust] Add Safe Rust Wrappers for Kernel IPC
Maintainers usually reply within 2 days
Nobody has claimed this yet.
Assessment
- Difficulty
- 5/5
- Estimated time
- Over a week
- Newbie friendliness
- 35/100
- Issue type
- Feature
- Clarity
- Mostly clear
- Activity status
- Quiet
- Domain
- embedded-iot, operating-systems
Research direction
Start by reviewing the Rust tooling support in PRs #11056 and #10910, then trace the mentioned C IPC entry points: rt_mutex_create, rt_mutex_take, rt_mutex_release, and rt_mutex_delete. Define the scope for Mutex and Semaphore wrappers, including RAII, encapsulated raw pointers, and Send/Sync behavior. Done means the core IPC mechanisms have a safe Rust layer that removes the need for callers to write these unsafe wrappers.
Written by the indexing model from the issue text.
Description
With the recent merge of Rust tooling support (PR #11056, #10910), the foundation for Rust in RT-Thread has been laid. However, developers are currently forced to use unsafe blocks to interact with the kernel API.
This creates a gap in safety. RT-Thread is widely used in safety-critical domains (Automotive, Medical, Industrial). Standard C usage is prone to concurrency bugs (race conditions on SMP systems) and memory leaks. While the C kernel works, exposing it directly to Rust negates the safety guarantees that make Rust valuable.
A Safe Rust Wrapper solves this by enforcing memory safety and concurrency rules at compile-time, preventing data races and use-after-free errors before the code even runs.
Preferred Solution
I propose implementing a Safe Rust layer for the core IPC mechanisms (Mutex, Semaphore).
Key Design Features:
- RAII (Resource Acquisition Is Initialization): Resources are automatically released when objects go out of scope.
- Type Safety: Raw pointers from the C kernel are encapsulated.
- Concurrency Safety: The wrappers will use Rust's
SendandSynctraits to prevent data races.
Proof of Concept (POC):
Here is a proposed design for a Safe Mutex wrapper:
use core::marker::PhantomData;
use core::ops::Deref;
// FFI: Bindings to C kernel functions
extern "C" {
fn rt_mutex_create(name: *const i8, flag: u8) -> *mut rt_mutex;
fn rt_mutex_take(mutex: *mut rt_mutex, time: i32) -> i32;
fn rt_mutex_release(mutex: *mut rt_mutex) -> i32;
fn rt_mutex_delete(mutex: *mut rt_mutex);
}
/// A Safe Wrapper for the RT-Thread Mutex
pub struct Mutex<T> {
raw: *mut rt_mutex,
_marker: PhantomData<T>,
}
impl<T> Mutex<T> {
pub fn new(name: &str, t: T) -> Self {
// ... CString conversion implementation ...
let raw = unsafe { rt_mutex_create(cname.as_ptr(), 0) };
Mutex { raw, _marker: PhantomData }
}
pub fn lock(&self) -> MutexGuard<'_, T> {
unsafe { rt_mutex_take(self.raw, -1) }; // Wait forever
MutexGuard { mutex: self, _marker: PhantomData }
}
}
impl<T> Drop for Mutex<T> {
fn drop(&mut self) {
unsafe { rt_mutex_delete(self.raw) };
}
}
pub struct MutexGuard<'a, T> {
mutex: &'a Mutex<T>,
_marker: PhantomData<T>,
}
impl<T> Drop for MutexGuard<'_, T> {
fn drop(&mut self) {
unsafe { rt_mutex_release(self.mutex.raw) };
}
}
This design ensures that:
-
- rt_mutex_delete is called automatically (no memory leaks).
-
- rt_mutex_release is called automatically (no deadlocks if thread panics).
-
- The protected data can only be accessed through the guard.
Possible Alternatives
1. **Continue using `unsafe` FFI:** Developers can continue writing their own `unsafe` wrappers, but this leads to fragmented code and potential security risks across different projects.
2. **External Crate:** The wrapper could be maintained as a separate repository, but keeping it in the main tree ensures it stays synchronized with kernel API changes and improves the "out-of-the-box" experience for RT-Thread users.
- Dominant language
- C
- Stars
- 12.4k
- Forks
- 5.5k
- Avg merge
- 3d 7h
- Merged PRs (30d)
- 79
Getting set up
Starts the project's dev container in your browser, under your own GitHub account.
- No Dockerfile or Docker Compose file
- Has a pull request template
- Read the contributing guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
More from RT-Thread/rt-thread
-
Arch: ARM/AArch64 Doc
Difficulty 1/5 Under an hour Newbie friendliness 90/100
RT-Thread/rt-thread#12082 · 1 comment ·
Maintainers usually reply within 2 days
-
bug
Difficulty 2/5 1-3 hours Newbie friendliness 67/100
RT-Thread/rt-thread#11951 · 2 comments ·
Maintainers usually reply within 2 days
-
[Bug] [libc] timer_gettime 拆秒前将总纳秒截断为 uint32_t,剩余时间查询错误May be free again @Yunlk claimed this 28 days ago, and no pull request is open. Openin progress
Difficulty 2/5 1-3 hours Newbie friendliness 68/100
RT-Thread/rt-thread#11924 · 7 comments · 1 assignee ·
Maintainers usually reply within 2 days
-
[Bug] dfs_v2 tmpfs: ftruncate(fd, 0) frees the file buffer but reports success — use-after-free read/write and double free from a legal POSIX callPossibly taken @remukisama claimed this 33 days ago. Openin progress
Difficulty 2/5 1-3 hours Newbie friendliness 78/100
RT-Thread/rt-thread#11902 · 4 comments · 1 assignee ·
Maintainers usually reply within 2 days
-
[bsp][stm32][bluepill] README「快速上手」缺少重新生成 MDK 工程这一步,按文档操作无法编译通过May be free again @moment-NEW claimed this 50 days ago, and no pull request is open. Openin progress
Difficulty 2/5 1-3 hours Newbie friendliness 75/100
RT-Thread/rt-thread#11818 · 4 comments · 1 assignee ·
Maintainers usually reply within 2 days
All issues in RT-Thread/rt-thread
Similar issues
-
Difficulty 2/5 1-3 hours Newbie friendliness 72/100
apache/nuttx-apps#4108 · 4 comments ·
Maintainers usually reply within 1 day
-
Difficulty 1/5 Under an hour Newbie friendliness 82/100
trezor/trezor-firmware#8357 ·
Maintainers usually reply within 2 days
-
ivorysql_ora: DBMS_SESSION lacks SLEEP (documented, not covered by #2763)Possibly taken A pull request linked to this issue is open or already merged. Open
Difficulty 2/5 1-3 hours Newbie friendliness 68/100
Maintainers usually reply within 1 day
-
good first issue kernel
Difficulty 2/5 1-3 hours Newbie friendliness 76/100
JackFurton/who-would-build-a-kernel-in-java#316 ·
Maintainers usually reply within 1 day
-
Difficulty 2/5 1-3 hours Newbie friendliness 66/100
PE5PVB/TEF6686_ESP32#970 ·