diff options
| author | Richard Qin <richard.qin.x@gmail.com> | 2026-01-03 02:13:08 +0800 |
|---|---|---|
| committer | Richard Qin <richard.qin.x@gmail.com> | 2026-01-03 02:13:08 +0800 |
| commit | 10614e04929a6d109a5e90524e446b637aaf0c13 (patch) | |
| tree | eb11aeb7b641187f207e3b572c5184cf3526b463 /src/kernel/proc/exec.cc | |
| parent | 1c8e37f63d413a559825020289178d442537449e (diff) | |
| download | lume-10614e04929a6d109a5e90524e446b637aaf0c13.tar.gz lume-10614e04929a6d109a5e90524e446b637aaf0c13.tar.xz | |
Fix: critical malloc size calculation bug
Summary:
- Fixed an integer arithmetic error in malloc() where 'nunits' was initialized to 0, causing incorrect heap sizing.
- This bug caused malloc to return pointers pointing to unmapped memory pages, leading to Store Page Fault (scause 15) at process boundaries.
- Switched memory allocator logic to use byte-based size calculation instead of K&R style units to prevent off-by-one errors.
Improvements:
- Shell now starts successfully without segfaults.
Diffstat (limited to 'src/kernel/proc/exec.cc')
| -rw-r--r-- | src/kernel/proc/exec.cc | 10 |
1 files changed, 7 insertions, 3 deletions
diff --git a/src/kernel/proc/exec.cc b/src/kernel/proc/exec.cc index d0e75ef..22e4842 100644 --- a/src/kernel/proc/exec.cc +++ b/src/kernel/proc/exec.cc @@ -129,15 +129,19 @@ namespace Exec // Prepare user stack uint64 sz = PGROUNDUP(vm_guard.sz); - uint64 sz1 = VM::uvmalloc(vm_guard.pagetable, sz, sz + 2 * PGSIZE, PTE_W | PTE_R | PTE_U); + + const int stack_data_pages = 16; + const int stack_total_pages = stack_data_pages + 1; + + uint64 sz1 = VM::uvmalloc(vm_guard.pagetable, sz, sz + stack_total_pages * PGSIZE, PTE_W | PTE_R | PTE_U); if (sz1 == 0) return -1; vm_guard.sz = sz1; - VM::uvmunmap(vm_guard.pagetable, sz1 - 2 * PGSIZE, 1, 0); // protected pagetable + VM::uvmunmap(vm_guard.pagetable, sz, 1, 0); // protected pagetable uint64 sp = sz1; - uint64 stackbase = sp - PGSIZE; + uint64 stackbase = sp - stack_data_pages * PGSIZE; uint64 ustack[32]; uint64 argc = 0; |
