From: John Hodge Date: Fri, 29 Oct 2010 07:47:48 +0000 (+0800) Subject: Bug was in Heap_Realloc X-Git-Tag: rel0.06~6 X-Git-Url: https://git.ucc.asn.au/?a=commitdiff_plain;h=a9f5c2be879ab67080122aaabe83a7ae897126bb;p=tpg%2Facess2.git Bug was in Heap_Realloc - Assumed sizeof(tHeapHead) == 8 - Removed debugging info --- diff --git a/Kernel/heap.c b/Kernel/heap.c index aa081008..abf5f27e 100644 --- a/Kernel/heap.c +++ b/Kernel/heap.c @@ -7,7 +7,7 @@ #include #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 diff --git a/Kernel/include/heap.h b/Kernel/include/heap.h index 38a3f1d0..28d33c0d 100644 --- a/Kernel/include/heap.h +++ b/Kernel/include/heap.h @@ -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)) diff --git a/Modules/Filesystems/FAT/fat.c b/Modules/Filesystems/FAT/fat.c index 26b19613..911b26e4 100644 --- a/Modules/Filesystems/FAT/fat.c +++ b/Modules/Filesystems/FAT/fat.c @@ -1117,6 +1117,8 @@ char *FAT_int_GetLFN(tVFS_Node *Node, int ID) tFAT_LFNCache *cache; int i, firstFree; + Mutex_Acquire( &Node->Lock ); + // TODO: Thread Safety (Lock things) cache = Node->Data; @@ -1126,15 +1128,20 @@ char *FAT_int_GetLFN(tVFS_Node *Node, int ID) cache->NumEntries = 1; cache->Entries[0].ID = ID; cache->Entries[0].Data[0] = '\0'; + Mutex_Release( &Node->Lock ); + //Log_Debug("FAT", "Return = %p (new)", cache->Entries[0].Data); return cache->Entries[0].Data; } - // Scan for a current entry + // Scan for this entry firstFree = -1; for( i = 0; i < cache->NumEntries; i++ ) { - if( cache->Entries[i].ID == ID ) + if( cache->Entries[i].ID == ID ) { + Mutex_Release( &Node->Lock ); + //Log_Debug("FAT", "Return = %p (match)", cache->Entries[i].Data); return cache->Entries[i].Data; + } if( cache->Entries[i].ID == -1 && firstFree == -1 ) firstFree = i; } @@ -1144,9 +1151,11 @@ char *FAT_int_GetLFN(tVFS_Node *Node, int ID) i = sizeof(tFAT_LFNCache) + (cache->NumEntries+1)*sizeof(tFAT_LFNCacheEnt); Node->Data = realloc( Node->Data, i ); if( !Node->Data ) { - Log_Error("FAT", "malloc() fail, unable to allocate %i for LFN cache", i); + Log_Error("FAT", "realloc() fail, unable to allocate %i for LFN cache", i); + Mutex_Release( &Node->Lock ); return NULL; } + //Log_Debug("FAT", "Realloc (%i)\n", i); cache = Node->Data; i = cache->NumEntries; cache->NumEntries ++; @@ -1159,7 +1168,8 @@ char *FAT_int_GetLFN(tVFS_Node *Node, int ID) cache->Entries[ i ].ID = ID; cache->Entries[ i ].Data[0] = '\0'; - //TODO: Unlock + Mutex_Release( &Node->Lock ); + //Log_Debug("FAT", "Return = %p (firstFree, i = %i)", cache->Entries[i].Data, i); return cache->Entries[ i ].Data; } @@ -1249,7 +1259,14 @@ char *FAT_ReadDir(tVFS_Node *Node, int ID) lfn = FAT_int_GetLFN( Node, ID + (lfnInfo->id & 0x3F) ); // Bit 6 indicates the start of an entry - if(lfnInfo->id & 0x40) memset(lfn, 0, 256); + if(lfnInfo->id & 0x40) { + //Log_Debug("FAT", "lfn = %p", lfn); + //Heap_Validate(); + //Log_Debug("FAT", "Clearing LFN"); + memset(lfn, 0, 256); + //Heap_Validate(); + //Log_Debug("FAT", "Check Passed"); + } a = (lfnInfo->id & 0x3F) * 13;