Cleaned up places where MM_Allocate was used without checks
authorJohn Hodge <[email protected]>
Sat, 12 Feb 2011 02:25:00 +0000 (10:25 +0800)
committerJohn Hodge <[email protected]>
Sat, 12 Feb 2011 02:25:00 +0000 (10:25 +0800)
Kernel/arch/x86/include/mm_virt.h
Kernel/arch/x86/mm_phys.c
Kernel/arch/x86/proc.c
Kernel/heap.c
Kernel/syscalls.c
Kernel/threads.c
Kernel/vfs/handle.c
Usermode/Applications/bomb_src/main.c

index 5e4712f..84b088c 100644 (file)
@@ -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);
index ef289ac..faef2b3 100644 (file)
@@ -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");
index 4812609..55b9b17 100644 (file)
@@ -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;
 }
index 34dd5f2..8a7adec 100644 (file)
@@ -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;
index 6e8d60e..95ad2e5 100644 (file)
@@ -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
        
index 5d15c92..13914a8 100644 (file)
@@ -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();
        }
index 4a8d478..21a98e0 100644 (file)
@@ -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
index 944d6c3..af5d094 100644 (file)
@@ -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);
                }
        }
 

UCC git Repository :: git.ucc.asn.au