fix: fix heap regression

This commit is contained in:
2026-02-27 23:51:46 +01:00
parent bd16b9afd1
commit ca85553eec
3 changed files with 34 additions and 2 deletions
+19
View File
@@ -61,6 +61,25 @@ namespace Zenith {
return userVa; return userVa;
} }
// Free all heap allocations for a process slot (called on process exit)
static void CleanupHeapForSlot(int slot, uint64_t pml4Phys) {
if (slot < 0 || slot >= Sched::MaxProcesses) return;
for (int i = 0; i < g_heapAllocCount[slot]; i++) {
uint64_t va = g_heapAllocs[slot][i].va;
uint64_t numPages = g_heapAllocs[slot][i].numPages;
for (uint64_t p = 0; p < numPages; p++) {
uint64_t pageVa = va + p * 0x1000;
uint64_t physAddr = Memory::VMM::Paging::GetPhysAddr(pml4Phys, pageVa);
if (physAddr != 0) {
Memory::g_pfa->Free((void*)Memory::HHDM(physAddr));
}
Memory::VMM::Paging::UnmapUserIn(pml4Phys, pageVa);
}
}
g_heapAllocCount[slot] = 0;
}
static void Sys_Free(uint64_t addr) { static void Sys_Free(uint64_t addr) {
auto* proc = Sched::GetCurrentProcessPtr(); auto* proc = Sched::GetCurrentProcessPtr();
if (proc == nullptr) return; if (proc == nullptr) return;
+15 -2
View File
@@ -39,9 +39,14 @@ namespace Zenith {
extern "C" int64_t SyscallDispatch(SyscallFrame* frame) { extern "C" int64_t SyscallDispatch(SyscallFrame* frame) {
switch (frame->syscall_nr) { switch (frame->syscall_nr) {
case SYS_EXIT: case SYS_EXIT: {
int slot = GetCurrentSlot();
auto* proc = Sched::GetCurrentProcessPtr();
if (slot >= 0 && proc)
CleanupHeapForSlot(slot, proc->pml4Phys);
Sys_Exit((int)frame->arg1); Sys_Exit((int)frame->arg1);
return 0; return 0;
}
case SYS_YIELD: case SYS_YIELD:
Sys_Yield(); Sys_Yield();
return 0; return 0;
@@ -192,8 +197,16 @@ namespace Zenith {
return (int64_t)Sys_WinResize((int)frame->arg1, (int)frame->arg2, (int)frame->arg3); return (int64_t)Sys_WinResize((int)frame->arg1, (int)frame->arg2, (int)frame->arg3);
case SYS_PROCLIST: case SYS_PROCLIST:
return (int64_t)Sys_ProcList((ProcInfo*)frame->arg1, (int)frame->arg2); return (int64_t)Sys_ProcList((ProcInfo*)frame->arg1, (int)frame->arg2);
case SYS_KILL: case SYS_KILL: {
// Free heap allocations for the target process before killing it
auto* target = Sched::GetProcessByPid((int)frame->arg1);
if (target) {
auto* slot0 = Sched::GetProcessSlot(0);
int targetSlot = (int)(target - slot0);
CleanupHeapForSlot(targetSlot, target->pml4Phys);
}
return (int64_t)Sys_Kill((int)frame->arg1); return (int64_t)Sys_Kill((int)frame->arg1);
}
case SYS_DEVLIST: case SYS_DEVLIST:
return (int64_t)Sys_DevList((DevInfo*)frame->arg1, (int)frame->arg2); return (int64_t)Sys_DevList((DevInfo*)frame->arg1, (int)frame->arg2);
case SYS_WINSETSCALE: case SYS_WINSETSCALE:
BIN
View File
Binary file not shown.