From e5e272c426a28f2f9e69ed2232469575c7409af1 Mon Sep 17 00:00:00 2001 From: John Hodge Date: Sat, 12 Feb 2011 10:25:00 +0800 Subject: [PATCH] Cleaned up places where MM_Allocate was used without checks --- Kernel/arch/x86/include/mm_virt.h | 2 +- Kernel/arch/x86/mm_phys.c | 6 +++++- Kernel/arch/x86/proc.c | 13 +++++++++++-- Kernel/heap.c | 10 ++++++++-- Kernel/syscalls.c | 4 +++- Kernel/threads.c | 13 ++++++++++++- Kernel/vfs/handle.c | 16 ++++++++++++++-- Usermode/Applications/bomb_src/main.c | 2 +- 8 files changed, 55 insertions(+), 11 deletions(-) diff --git a/Kernel/arch/x86/include/mm_virt.h b/Kernel/arch/x86/include/mm_virt.h index 5e4712f6..84b088c6 100644 --- a/Kernel/arch/x86/include/mm_virt.h +++ b/Kernel/arch/x86/include/mm_virt.h @@ -27,7 +27,7 @@ // === FUNCTIONS === extern void MM_FinishVirtualInit(void); extern void MM_SetCR3(Uint CR3); -extern tPAddr MM_Allocate(tVAddr VAddr); +extern tPAddr MM_Allocate(tVAddr VAddr) __attribute__ ((warn_unused_result)); extern void MM_Deallocate(tVAddr VAddr); extern int MM_Map(tVAddr VAddr, tPAddr PAddr); extern tPAddr MM_Clone(void); diff --git a/Kernel/arch/x86/mm_phys.c b/Kernel/arch/x86/mm_phys.c index ef289acd..faef2b3e 100644 --- a/Kernel/arch/x86/mm_phys.c +++ b/Kernel/arch/x86/mm_phys.c @@ -107,7 +107,11 @@ void MM_Install(tMBoot_Info *MBoot) //LOG("Reference Pages %i", (giPageCount*4+0xFFF)>>12); for(num = 0; num < (giPageCount*4+0xFFF)>>12; num++) { - MM_Allocate( REFERENCE_BASE + (num<<12) ); + if( !MM_Allocate( REFERENCE_BASE + (num<<12) ) ) + { + Panic("Oh, ****, no space for the reference pages, that's bad"); + for(;;); + } } //LOG("Filling"); diff --git a/Kernel/arch/x86/proc.c b/Kernel/arch/x86/proc.c index 4812609a..55b9b170 100644 --- a/Kernel/arch/x86/proc.c +++ b/Kernel/arch/x86/proc.c @@ -368,7 +368,10 @@ void ArchThreads_Init(void) #endif // Create Per-Process Data Block - MM_Allocate(MM_PPD_CFG); + if( !MM_Allocate(MM_PPD_CFG) ) + { + Panic("OOM - No space for initiali Per-Process Config"); + } // Change Stacks Proc_ChangeStack(); @@ -707,7 +710,13 @@ Uint Proc_MakeUserStack(void) // Allocate Stack - Allocate incrementally to clean up MM_Dump output for( i = 0; i < USER_STACK_SZ/0x1000; i++ ) - MM_Allocate( base + (i<<12) ); + { + if( !MM_Allocate( base + (i<<12) ) ) + { + Warning("OOM: Proc_MakeUserStack"); + return 0; + } + } return base + USER_STACK_SZ; } diff --git a/Kernel/heap.c b/Kernel/heap.c index 34dd5f2c..8a7adec4 100644 --- a/Kernel/heap.c +++ b/Kernel/heap.c @@ -69,8 +69,14 @@ void *Heap_Extend(int Bytes) } // Heap expands in pages - for(i=0;i<(Bytes+0xFFF)>>12;i++) - MM_Allocate( (tVAddr)gHeapEnd+(i<<12) ); + for( i = 0; i < (Bytes+0xFFF) >> 12; i ++ ) + { + if( !MM_Allocate( (tVAddr)gHeapEnd+(i<<12) ) ) + { + Warning("OOM - Heap_Extend"); + return NULL; + } + } // Increas heap end gHeapEnd += i << 12; diff --git a/Kernel/syscalls.c b/Kernel/syscalls.c index 6e8d60e4..95ad2e5f 100644 --- a/Kernel/syscalls.c +++ b/Kernel/syscalls.c @@ -311,8 +311,10 @@ void SyscallHandler(tSyscallRegs *Regs) // -- Debug //#if DEBUG_BUILD case SYS_DEBUG: - Log((char*)Regs->Arg1, + LogF("Log: [%i] ", Threads_GetTID()); + LogF((char*)Regs->Arg1, Regs->Arg2, Regs->Arg3, Regs->Arg4, Regs->Arg5, Regs->Arg6); + LogF("\n"); break; //#endif diff --git a/Kernel/threads.c b/Kernel/threads.c index 5d15c929..13914a85 100644 --- a/Kernel/threads.c +++ b/Kernel/threads.c @@ -465,6 +465,7 @@ void Threads_Exit(int TID, int Status) void Threads_Kill(tThread *Thread, int Status) { tMsg *msg; + int isCurThread = Thread == Proc_GetCurThread(); // TODO: Kill all children #if 1 @@ -529,6 +530,7 @@ void Threads_Kill(tThread *Thread, int Status) ); } break; + // Kill it while it sleeps! case THREAD_STAT_SLEEPING: if( !Threads_int_DelFromQueue( &gSleepingThreads, Thread ) ) { @@ -538,6 +540,15 @@ void Threads_Kill(tThread *Thread, int Status) ); } break; + + // Brains!... You cannot kill + case THREAD_STAT_ZOMBIE: + Log_Warning("Threads", "Threads_Kill - Thread %p(%i,%s) is undead, you cannot kill it", + Thread, Thread->TID, Thread->ThreadName); + SHORTREL( &glThreadListLock ); + SHORTREL( &Thread->IsLocked ); + return ; + default: Log_Warning("Threads", "Threads_Kill - BUG Un-checked status (%i)", Thread->Status); @@ -565,7 +576,7 @@ void Threads_Kill(tThread *Thread, int Status) SHORTREL( &Thread->IsLocked ); // TODO: We may not actually be released... // And, reschedule - if(Status != -1) { + if(isCurThread) { for( ;; ) HALT(); } diff --git a/Kernel/vfs/handle.c b/Kernel/vfs/handle.c index 4a8d4786..21a98e05 100644 --- a/Kernel/vfs/handle.c +++ b/Kernel/vfs/handle.c @@ -60,7 +60,13 @@ int VFS_AllocHandle(int bIsUser, tVFS_Node *Node, int Mode) Uint addr, size; size = CFGINT(CFG_VFS_MAXFILES) * sizeof(tVFS_Handle); for(addr = 0; addr < size; addr += 0x1000) - MM_Allocate( (Uint)gaUserHandles + addr ); + { + if( !MM_Allocate( (Uint)gaUserHandles + addr ) ) + { + Warning("OOM - VFS_AllocHandle"); + Threads_Exit(0, 0xFF); // Terminate user + } + } memset( gaUserHandles, 0, size ); } // Get a handle @@ -81,7 +87,13 @@ int VFS_AllocHandle(int bIsUser, tVFS_Node *Node, int Mode) Uint addr, size; size = MAX_KERNEL_FILES * sizeof(tVFS_Handle); for(addr = 0; addr < size; addr += 0x1000) - MM_Allocate( (Uint)gaKernelHandles + addr ); + { + if( !MM_Allocate( (Uint)gaKernelHandles + addr ) ) + { + Panic("OOM - VFS_AllocHandle"); + Threads_Exit(0, 0xFF); // Terminate application (get some space back) + } + } memset( gaKernelHandles, 0, size ); } // Get a handle diff --git a/Usermode/Applications/bomb_src/main.c b/Usermode/Applications/bomb_src/main.c index 944d6c32..af5d094a 100644 --- a/Usermode/Applications/bomb_src/main.c +++ b/Usermode/Applications/bomb_src/main.c @@ -57,6 +57,7 @@ int main(int argc, char *argv[]) return 0; } tid = clone(0, stack+stackSize-stackOffset); + //_SysDebug("tid = %i", tid); if( tid == 0 ) { // Sleep forever (TODO: Fix up the stack so it can nuke) @@ -66,7 +67,6 @@ int main(int argc, char *argv[]) printf("Clone failed\n"); return 0; } - printf("stack = %p, tid = %i\n", stack, tid); } } -- 2.20.1