Skip to content
Snippets Groups Projects
Commit a9f5c2be authored by John Hodge's avatar John Hodge
Browse files

Bug was in Heap_Realloc

- Assumed sizeof(tHeapHead) == 8
- Removed debugging info
parent 18a6f0a1
No related merge requests found
...@@ -7,7 +7,7 @@ ...@@ -7,7 +7,7 @@
#include <heap_int.h> #include <heap_int.h>
#define WARNINGS 1 #define WARNINGS 1
#define DEBUG_TRACE 1 #define DEBUG_TRACE 0
#define VERBOSE_DUMP 0 #define VERBOSE_DUMP 0
// === CONSTANTS === // === CONSTANTS ===
...@@ -340,7 +340,7 @@ void Heap_Deallocate(void *Ptr) ...@@ -340,7 +340,7 @@ void Heap_Deallocate(void *Ptr)
*/ */
void *Heap_Reallocate(const char *File, int Line, void *__ptr, size_t __size) 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; tHeapHead *nextHead;
tHeapFoot *foot; tHeapFoot *foot;
Uint newSize = (__size + sizeof(tHeapFoot)+sizeof(tHeapHead)+MIN_SIZE-1)&~(MIN_SIZE-1); Uint newSize = (__size + sizeof(tHeapFoot)+sizeof(tHeapHead)+MIN_SIZE-1)&~(MIN_SIZE-1);
...@@ -478,11 +478,18 @@ int Heap_IsHeapAddr(void *Ptr) ...@@ -478,11 +478,18 @@ int Heap_IsHeapAddr(void *Ptr)
return 1; return 1;
} }
/**
*/
void Heap_Validate(void)
{
Heap_Dump();
}
#if WARNINGS #if WARNINGS
void Heap_Dump(void) void Heap_Dump(void)
{ {
tHeapHead *head, *badHead; tHeapHead *head, *badHead;
tHeapFoot *foot; tHeapFoot *foot = NULL;
head = gHeapStart; head = gHeapStart;
while( (Uint)head < (Uint)gHeapEnd ) while( (Uint)head < (Uint)gHeapEnd )
...@@ -530,6 +537,10 @@ void Heap_Dump(void) ...@@ -530,6 +537,10 @@ void Heap_Dump(void)
head = foot->NextHead; head = foot->NextHead;
} }
// If the heap is valid, ok!
if( (tVAddr)head == (tVAddr)gHeapEnd )
return ;
// Check for a bad return // Check for a bad return
if( (tVAddr)head >= (tVAddr)gHeapEnd ) if( (tVAddr)head >= (tVAddr)gHeapEnd )
return ; return ;
...@@ -593,6 +604,8 @@ void Heap_Dump(void) ...@@ -593,6 +604,8 @@ void Heap_Dump(void)
head = foot->Head; head = foot->Head;
Log_Debug("Heap", "head=%p", head); Log_Debug("Heap", "head=%p", head);
} }
Panic("Heap_Dump - Heap is corrupted, kernel panic!");
} }
#endif #endif
......
...@@ -12,6 +12,7 @@ extern void *Heap_AllocateZero(const char *File, int Line, size_t Bytes); ...@@ -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_Reallocate(const char *File, int Line, void *Ptr, size_t Bytes);
extern void Heap_Deallocate(void *Ptr); extern void Heap_Deallocate(void *Ptr);
extern int Heap_IsHeapAddr(void *Ptr); extern int Heap_IsHeapAddr(void *Ptr);
extern void Heap_Validate(void);
#define malloc(size) Heap_Allocate(_MODULE_NAME_"/"__FILE__, __LINE__, (size)) #define malloc(size) Heap_Allocate(_MODULE_NAME_"/"__FILE__, __LINE__, (size))
#define calloc(num,size) Heap_AllocateZero(_MODULE_NAME_"/"__FILE__, __LINE__, (num)*(size)) #define calloc(num,size) Heap_AllocateZero(_MODULE_NAME_"/"__FILE__, __LINE__, (num)*(size))
......
...@@ -1117,6 +1117,8 @@ char *FAT_int_GetLFN(tVFS_Node *Node, int ID) ...@@ -1117,6 +1117,8 @@ char *FAT_int_GetLFN(tVFS_Node *Node, int ID)
tFAT_LFNCache *cache; tFAT_LFNCache *cache;
int i, firstFree; int i, firstFree;
Mutex_Acquire( &Node->Lock );
// TODO: Thread Safety (Lock things) // TODO: Thread Safety (Lock things)
cache = Node->Data; cache = Node->Data;
...@@ -1126,15 +1128,20 @@ char *FAT_int_GetLFN(tVFS_Node *Node, int ID) ...@@ -1126,15 +1128,20 @@ char *FAT_int_GetLFN(tVFS_Node *Node, int ID)
cache->NumEntries = 1; cache->NumEntries = 1;
cache->Entries[0].ID = ID; cache->Entries[0].ID = ID;
cache->Entries[0].Data[0] = '\0'; 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; return cache->Entries[0].Data;
} }
// Scan for a current entry // Scan for this entry
firstFree = -1; firstFree = -1;
for( i = 0; i < cache->NumEntries; i++ ) 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; return cache->Entries[i].Data;
}
if( cache->Entries[i].ID == -1 && firstFree == -1 ) if( cache->Entries[i].ID == -1 && firstFree == -1 )
firstFree = i; firstFree = i;
} }
...@@ -1144,9 +1151,11 @@ char *FAT_int_GetLFN(tVFS_Node *Node, int ID) ...@@ -1144,9 +1151,11 @@ char *FAT_int_GetLFN(tVFS_Node *Node, int ID)
i = sizeof(tFAT_LFNCache) + (cache->NumEntries+1)*sizeof(tFAT_LFNCacheEnt); i = sizeof(tFAT_LFNCache) + (cache->NumEntries+1)*sizeof(tFAT_LFNCacheEnt);
Node->Data = realloc( Node->Data, i ); Node->Data = realloc( Node->Data, i );
if( !Node->Data ) { 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; return NULL;
} }
//Log_Debug("FAT", "Realloc (%i)\n", i);
cache = Node->Data; cache = Node->Data;
i = cache->NumEntries; i = cache->NumEntries;
cache->NumEntries ++; cache->NumEntries ++;
...@@ -1159,7 +1168,8 @@ char *FAT_int_GetLFN(tVFS_Node *Node, int ID) ...@@ -1159,7 +1168,8 @@ char *FAT_int_GetLFN(tVFS_Node *Node, int ID)
cache->Entries[ i ].ID = ID; cache->Entries[ i ].ID = ID;
cache->Entries[ i ].Data[0] = '\0'; 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; return cache->Entries[ i ].Data;
} }
...@@ -1249,7 +1259,14 @@ char *FAT_ReadDir(tVFS_Node *Node, int ID) ...@@ -1249,7 +1259,14 @@ char *FAT_ReadDir(tVFS_Node *Node, int ID)
lfn = FAT_int_GetLFN( Node, ID + (lfnInfo->id & 0x3F) ); lfn = FAT_int_GetLFN( Node, ID + (lfnInfo->id & 0x3F) );
// Bit 6 indicates the start of an entry // 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; a = (lfnInfo->id & 0x3F) * 13;
......
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment