Fixing binary loader bugs relating to page sizes
authorJohn Hodge <[email protected]>
Thu, 15 Jul 2010 15:03:19 +0000 (23:03 +0800)
committerJohn Hodge <[email protected]>
Thu, 15 Jul 2010 15:03:19 +0000 (23:03 +0800)
Kernel/bin/elf.c
Kernel/binary.c
Kernel/modules.c

index 731d151..4bf15fa 100644 (file)
@@ -2,7 +2,7 @@
  * Acess v0.1\r
  * ELF Executable Loader Code\r
  */\r
-#define DEBUG  0\r
+#define DEBUG  1\r
 #include <acess.h>\r
 #include <binary.h>\r
 #include "elf.h"\r
@@ -35,7 +35,7 @@ tBinary *Elf_Load(int fp)
         int    iPageCount;\r
         int    count;\r
        \r
-       ENTER("ifp", fp);\r
+       ENTER("xfp", fp);\r
        \r
        // Read ELF Header\r
        VFS_Read(fp, sizeof(hdr), &hdr);\r
@@ -126,13 +126,15 @@ tBinary *Elf_Load(int fp)
                LOG("phtab[%i] = {VAddr:0x%x,Offset:0x%x,FileSize:0x%x}",\r
                        i, phtab[i].VAddr, phtab[i].Offset, phtab[i].FileSize);\r
                \r
-               if( (phtab[i].FileSize & 0xFFF) < 0x1000 - (phtab[i].VAddr & 0xFFF) )\r
-                       lastSize = phtab[i].FileSize;\r
-               else\r
+               //if( (phtab[i].FileSize & 0xFFF) < 0x1000 - (phtab[i].VAddr & 0xFFF) )\r
+               //      lastSize = phtab[i].FileSize;\r
+               //else\r
                        lastSize = (phtab[i].FileSize & 0xFFF) + (phtab[i].VAddr & 0xFFF);\r
-               lastSize &= 0xFFF;\r
+               //lastSize &= 0xFFF;\r
+               \r
+               //LOG("lastSize = 0x%x", lastSize);\r
                \r
-               LOG("lastSize = 0x%x", lastSize);\r
+               lastSize = phtab[i].FileSize;\r
                \r
                // Get Pages\r
                count = ( (phtab[i].VAddr&0xFFF) + phtab[i].FileSize + 0xFFF) >> 12;\r
@@ -152,6 +154,7 @@ tBinary *Elf_Load(int fp)
                                ret->Pages[j+k].Size = 4096;\r
                        LOG("ret->Pages[%i].Size = 0x%x", j+k, ret->Pages[j+k].Size);\r
                        ret->Pages[j+k].Flags = 0;\r
+                       lastSize -= ret->Pages[j+k].Size;\r
                }\r
                count = (phtab[i].MemSize + 0xFFF) >> 12;\r
                for(;k<count;k++)\r
index eb7094d..72e8210 100644 (file)
@@ -10,7 +10,6 @@
 // === CONSTANTS ===
 #define BIN_LOWEST     MM_USER_MIN             // 1MiB
 #define BIN_GRANUALITY 0x10000         // 64KiB
-//! \todo Move 0xBC000000 to mm_virt.h
 #define BIN_HIGHEST    (USER_LIB_MAX-BIN_GRANUALITY)           // Just below the kernel
 #define        KLIB_LOWEST     MM_MODULE_MIN
 #define KLIB_GRANUALITY        0x10000         // 32KiB
@@ -434,7 +433,7 @@ tBinary *Binary_DoLoad(char *truePath)
        LOG("NumPages: %i", pBinary->NumPages);
        
        // Read Data
-       for(i=0;i<pBinary->NumPages;i++)
+       for(i = 0; i < pBinary->NumPages; i ++)
        {
                Uint    dest;
                tPAddr  paddr;
@@ -461,12 +460,34 @@ tBinary *Binary_DoLoad(char *truePath)
                else
                {
                        VFS_Seek( fp, pBinary->Pages[i].Physical, 1 );
-                       if(pBinary->Pages[i].Size != 0x1000) {
+                       // If the address is not aligned, or the page is not full
+                       // sized, copy part of it
+                       if( (dest & 0xFFF) > 0 || pBinary->Pages[i].Size < 0x1000)
+                       {
+                               // Validate the size to prevent Kernel page faults
+                               // Clips to one page and prints a warning
+                               if( pBinary->Pages[i].Size + (dest & 0xFFF) > 0x1000) {
+                                       Log_Warning("Binary", "Loader error: Page %i (%p) of '%s' is %i bytes (> 4096)",
+                                               i, pBinary->Pages[i].Virtual, truePath,
+                                               (dest&0xFFF) + pBinary->Pages[i].Size);
+                                       pBinary->Pages[i].Size = 0x1000 - (dest & 0xFFF);
+                               }               
                                LOG("%i - 0x%llx - 0x%x bytes",
                                        i, pBinary->Pages[i].Physical, pBinary->Pages[i].Size);
-                               memset( (void*)dest, 0, 0x1000 -(dest&0xFFF) );
+                               // Zero from `dest` to the end of the page
+                               memset( (void*)dest, 0, 0x1000 - (dest&0xFFF) );
+                               // Read in the data
                                VFS_Read( fp, pBinary->Pages[i].Size, (void*)dest );
-                       } else {
+                       }
+                       // Full page
+                       else
+                       {
+                               // Check if the page is oversized
+                               if(pBinary->Pages[i].Size > 0x1000)
+                                       Log_Warning("Binary", "Loader error - Page %i (%p) of '%s' is %i bytes (> 4096)",
+                                               i, pBinary->Pages[i].Virtual, truePath,
+                                               pBinary->Pages[i].Size);
+                               // Read data
                                LOG("%i - 0x%x", i, pBinary->Pages[i].Physical);
                                VFS_Read( fp, 0x1000, (void*)dest );
                        }
index c9f3a70..9f6d3c5 100644 (file)
@@ -349,20 +349,6 @@ int Module_LoadFile(char *Path, char *ArgString)
                return 0;
        }
        
-       // Check magic number
-       if(info->Magic != MODULE_MAGIC)
-       {
-               Log_Warning("Module", "Module's magic value is invalid (0x%x != 0x%x)", info->Magic, MODULE_MAGIC);
-               return 0;
-       }
-       
-       // Check Architecture
-       if(info->Arch != MODULE_ARCH_ID)
-       {
-               Log_Warning("Module", "Module is for a different architecture");
-               return 0;
-       }
-       
        #if 1
        if( Module_int_Initialise( info, ArgString ) )
        {

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