fix: harden kernel — eliminate UB, proper process cleanup
Critical: kernel_ref() UB eliminated
- kernel_ref() returned &'static mut, creating aliasing mutable
references across trap handler + boot code (13+ call sites)
- Replaced with kernel_ptr() -> *mut Kernel, each use site does
explicit unsafe { &mut *kernel_ptr() }
- KernelCell: removed get_mut(), added as_ptr() -> *mut T
- UB is gone: raw pointer access makes the single-core invariant
explicit at every use site
Important: process cleanup on exit/fault
- New destroy_process() method consolidates cleanup:
- Removes all threads from scheduler and endpoints
- Clears pending messages and results
- Destroys process and frees ProcessMemory
- handle_exit and handle_fault both use destroy_process()
- handle_exit_thread uses destroy_process when last thread exits
IPC hardening:
- Added invariant comment on pending_messages[sender].take()
(sender in wait queue must have a pending message)
handle_release_pages:
- Now uses translate_va to convert VA→PA (was treating VA as PA)
This commit is contained in:
@@ -14,6 +14,7 @@ impl<P: Platform> Kernel<P> {
|
||||
Err(_) => return,
|
||||
};
|
||||
|
||||
// Try to deliver fault to a fault handler endpoint (if configured)
|
||||
if let Ok(process) = self.processes.get(pid) {
|
||||
if let Some(handler_slot) = process.fault_handler() {
|
||||
if let Ok(cap) = process.capabilities().get(handler_slot) {
|
||||
@@ -46,21 +47,6 @@ impl<P: Platform> Kernel<P> {
|
||||
}
|
||||
}
|
||||
|
||||
let thread_ids: [Option<types::id::ThreadId>; 4] = {
|
||||
match self.processes.get(pid) {
|
||||
Ok(p) => {
|
||||
let mut ids = [None; 4];
|
||||
for (i, t) in p.thread_ids().enumerate() {
|
||||
if i < 4 { ids[i] = Some(t); }
|
||||
}
|
||||
ids
|
||||
}
|
||||
Err(_) => return,
|
||||
}
|
||||
};
|
||||
|
||||
self.cleanup_threads(&thread_ids);
|
||||
let _ = self.processes.destroy(pid);
|
||||
self.process_memory[pid.raw() as usize] = None;
|
||||
self.destroy_process(pid);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user