Bug was in Heap_Realloc
authorJohn Hodge <[email protected]>
Fri, 29 Oct 2010 07:47:48 +0000 (15:47 +0800)
committerJohn Hodge <[email protected]>
Fri, 29 Oct 2010 07:47:48 +0000 (15:47 +0800)
- Assumed sizeof(tHeapHead) == 8
- Removed debugging info

Kernel/heap.c
Kernel/include/heap.h
Modules/Filesystems/FAT/fat.c

index aa08100..abf5f27 100644 (file)
@@ -7,7 +7,7 @@
 #include <heap_int.h>
 
 #define WARNINGS       1
-#define        DEBUG_TRACE     1
+#define        DEBUG_TRACE     0
 #define        VERBOSE_DUMP    0
 
 // === CONSTANTS ===
@@ -340,7 +340,7 @@ void Heap_Deallocate(void *Ptr)
  */
 void *Heap_Reallocate(const char *File, int Line, void *__ptr, size_t __size)
 {
-       tHeapHead       *head = (void*)( (Uint)__ptr-8 );
+       tHeapHead       *head = (void*)( (Uint)__ptr-sizeof(tHeapHead) );
        tHeapHead       *nextHead;
        tHeapFoot       *foot;
        Uint    newSize = (__size + sizeof(tHeapFoot)+sizeof(tHeapHead)+MIN_SIZE-1)&~(MIN_SIZE-1);
@@ -478,11 +478,18 @@ int Heap_IsHeapAddr(void *Ptr)
        return 1;
 }
 
+/**
+ */
+void Heap_Validate(void)
+{
+       Heap_Dump();
+}
+
 #if WARNINGS
 void Heap_Dump(void)
 {
        tHeapHead       *head, *badHead;
-       tHeapFoot       *foot;
+       tHeapFoot       *foot = NULL;
        
        head = gHeapStart;
        while( (Uint)head < (Uint)gHeapEnd )
@@ -530,6 +537,10 @@ void Heap_Dump(void)
                head = foot->NextHead;
        }
        
+       // If the heap is valid, ok!
+       if( (tVAddr)head == (tVAddr)gHeapEnd )
+               return ;
+       
        // Check for a bad return
        if( (tVAddr)head >= (tVAddr)gHeapEnd )
                return ;
@@ -593,6 +604,8 @@ void Heap_Dump(void)
                head = foot->Head;
                Log_Debug("Heap", "head=%p", head);
        }
+       
+       Panic("Heap_Dump - Heap is corrupted, kernel panic!");
 }
 #endif
 
index 38a3f1d..28d33c0 100644 (file)
@@ -12,6 +12,7 @@ extern void   *Heap_AllocateZero(const char *File, int Line, size_t Bytes);
 extern void    *Heap_Reallocate(const char *File, int Line, void *Ptr, size_t Bytes);
 extern void    Heap_Deallocate(void *Ptr);
 extern int     Heap_IsHeapAddr(void *Ptr);
+extern void    Heap_Validate(void);
 
 #define malloc(size)   Heap_Allocate(_MODULE_NAME_"/"__FILE__, __LINE__, (size))
 #define calloc(num,size)       Heap_AllocateZero(_MODULE_NAME_"/"__FILE__, __LINE__, (num)*(size))
index 26b1961..911b26e 100644 (file)
@@ -1117,6 +1117,8 @@ char *FAT_int_GetLFN(tVFS_Node *Node, int ID)
        tFAT_LFNCache   *cache;\r
         int    i, firstFree;\r
        \r
+       Mutex_Acquire( &Node->Lock );\r
+       \r
        // TODO: Thread Safety (Lock things)\r
        cache = Node->Data;\r
        \r
@@ -1126,15 +1128,20 @@ char *FAT_int_GetLFN(tVFS_Node *Node, int ID)
                cache->NumEntries = 1;\r
                cache->Entries[0].ID = ID;\r
                cache->Entries[0].Data[0] = '\0';\r
+               Mutex_Release( &Node->Lock );\r
+               //Log_Debug("FAT", "Return = %p (new)", cache->Entries[0].Data);\r
                return cache->Entries[0].Data;\r
        }\r
        \r
-       // Scan for a current entry\r
+       // Scan for this entry\r
        firstFree = -1;\r
        for( i = 0; i < cache->NumEntries; i++ )\r
        {\r
-               if( cache->Entries[i].ID == ID )\r
+               if( cache->Entries[i].ID == ID ) {\r
+                       Mutex_Release( &Node->Lock );\r
+                       //Log_Debug("FAT", "Return = %p (match)", cache->Entries[i].Data);\r
                        return cache->Entries[i].Data;\r
+               }\r
                if( cache->Entries[i].ID == -1 && firstFree == -1 )\r
                        firstFree = i;\r
        }\r
@@ -1144,9 +1151,11 @@ char *FAT_int_GetLFN(tVFS_Node *Node, int ID)
                i = sizeof(tFAT_LFNCache) + (cache->NumEntries+1)*sizeof(tFAT_LFNCacheEnt);\r
                Node->Data = realloc( Node->Data, i );\r
                if( !Node->Data ) {\r
-                       Log_Error("FAT", "malloc() fail, unable to allocate %i for LFN cache", i);\r
+                       Log_Error("FAT", "realloc() fail, unable to allocate %i for LFN cache", i);\r
+                       Mutex_Release( &Node->Lock );\r
                        return NULL;\r
                }\r
+               //Log_Debug("FAT", "Realloc (%i)\n", i);\r
                cache = Node->Data;\r
                i = cache->NumEntries;\r
                cache->NumEntries ++;\r
@@ -1159,7 +1168,8 @@ char *FAT_int_GetLFN(tVFS_Node *Node, int ID)
        cache->Entries[ i ].ID = ID;\r
        cache->Entries[ i ].Data[0] = '\0';\r
        \r
-       //TODO: Unlock\r
+       Mutex_Release( &Node->Lock );\r
+       //Log_Debug("FAT", "Return = %p (firstFree, i = %i)", cache->Entries[i].Data, i);\r
        return cache->Entries[ i ].Data;\r
 }\r
 \r
@@ -1249,7 +1259,14 @@ char *FAT_ReadDir(tVFS_Node *Node, int ID)
                lfn = FAT_int_GetLFN( Node, ID + (lfnInfo->id & 0x3F) );\r
                \r
                // Bit 6 indicates the start of an entry\r
-               if(lfnInfo->id & 0x40)  memset(lfn, 0, 256);\r
+               if(lfnInfo->id & 0x40) {\r
+                       //Log_Debug("FAT", "lfn = %p", lfn);\r
+                       //Heap_Validate();\r
+                       //Log_Debug("FAT", "Clearing LFN");\r
+                       memset(lfn, 0, 256);\r
+                       //Heap_Validate();\r
+                       //Log_Debug("FAT", "Check Passed");\r
+               }\r
                \r
                a = (lfnInfo->id & 0x3F) * 13;\r
                \r

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