-added a missing memory free line

-changed the wbfs allocations a bit
-added some security checks to the mem allocator to get around some crashes
This commit is contained in:
fix94.1 2013-09-01 16:43:53 +00:00
parent 11dc7c3aa5
commit 66a2b21cee
3 changed files with 69 additions and 63 deletions

View File

@ -17,18 +17,20 @@
#define wbfs_fatal(x) do { gprintf(x); wd_last_error = 1; } while(0)
#define wbfs_error(x) do { gprintf(x); wd_last_error = 2; } while(0)
/* Thanks to cfg-loader */
#define wbfs_malloc(x) calloc(x, 1)
#define wbfs_free(x) free(x)
static inline void *wbfs_ioalloc(size_t x)
static inline void *wbfs_malloc(size_t size)
{
void *p = memalign(32, x);
if(p)
memset(p, 0, x);
void *p = MEM2_memalign(32, size);
if(p) memset(p, 0, size);
return p;
}
#define wbfs_iofree(x) free(x)
static inline void wbfs_free(void *ptr)
{
MEM2_free(ptr);
}
#define wbfs_ioalloc(x) wbfs_malloc(x)
#define wbfs_iofree(x) wbfs_free(x)
#define wbfs_be16(x) (*((u16*)(x)))
#define wbfs_be32(x) (*((u32*)(x)))

View File

@ -92,7 +92,7 @@ void *CMEM2Alloc::allocate(unsigned int s)
j->next = i->next;
j->prev = i;
i->next = j;
if (j->next != 0)
if((((u32)j->next) & 0xf0000000) != 0)
j->next->prev = j;
}
return (void *)(i + 1);
@ -109,24 +109,24 @@ void CMEM2Alloc::release(void *p)
// If there are no other blocks following yet,
// set the remaining size to free size. - Dimok
if(i->next == 0)
if((((u32)i->next) & 0xf0000000) == 0)
i->s = m_endAddress - i - 1;
// Merge with previous block
if (i->prev != 0 && i->prev->f)
if ((((u32)i->prev) & 0xf0000000) != 0 && i->prev->f)
{
i = i->prev;
i->s += i->next->s + 1;
i->next = i->next->next;
if (i->next != 0)
if((((u32)i->next) & 0xf0000000) != 0)
i->next->prev = i;
}
// Merge with next block
if (i->next != 0 && i->next->f)
if ((((u32)i->next) & 0xf0000000) != 0 && i->next->f)
{
i->s += i->next->s + 1;
i->next = i->next->next;
if (i->next != 0)
if((((u32)i->next) & 0xf0000000) != 0)
i->next->prev = i;
}
}
@ -144,49 +144,49 @@ void *CMEM2Alloc::reallocate(void *p, unsigned int s)
i = (SBlock *)p - 1;
s = (s - 1) / sizeof (SBlock) + 1;
LockMutex lock(m_mutex);
//out of memory /* Dimok */
if (i + s + 1 >= m_endAddress)
{
LockMutex lock(m_mutex);
//out of memory /* Dimok */
if (i + s + 1 >= m_endAddress)
{
return 0;
}
// Last block
if (i->next == 0 && i + s + 1 < m_endAddress)
{
i->s = s;
return p;
}
// Size <= current size + next block
if (i->next != 0 && i->s < s && i->next->f && i->s + i->next->s + 1 >= s)
{
// Merge
i->s += i->next->s + 1;
i->next = i->next->next;
if (i->next != 0)
i->next->prev = i;
}
// Size <= current size
if (i->s >= s)
{
// Split
if (i->s > s + 1)
{
j = i + s + 1;
j->f = true;
j->s = i->s - s - 1;
i->s = s;
j->next = i->next;
j->prev = i;
i->next = j;
if (j->next != 0)
j->next->prev = j;
}
return p;
}
return 0;
}
// Last block
if (((((u32)i->next) & 0xf0000000) == 0) && i + s + 1 < m_endAddress)
{
i->s = s;
return p;
}
// Size <= current size + next block
if ((((u32)i->next) & 0xf0000000) != 0 && i->s < s && i->next->f && i->s + i->next->s + 1 >= s)
{
// Merge
i->s += i->next->s + 1;
i->next = i->next->next;
if((((u32)i->next) & 0xf0000000) != 0)
i->next->prev = i;
}
// Size <= current size
if (i->s >= s)
{
// Split
if (i->s > s + 1)
{
j = i + s + 1;
j->f = true;
j->s = i->s - s - 1;
i->s = s;
j->next = i->next;
j->prev = i;
i->next = j;
if((((u32)j->next) & 0xf0000000) != 0)
j->next->prev = j;
}
return p;
}
// Size > current size
n = allocate(s * sizeof (SBlock));
if (n == 0)
@ -208,13 +208,13 @@ unsigned int CMEM2Alloc::FreeSize()
for(i = m_first; i != 0; i = i->next)
{
if(i->f && i->next != 0)
if(i->f && (((u32)i->next) & 0xf0000000) != 0)
size += i->s;
else if(i->f && i->next == 0)
else if(i->f && (((u32)i->next) & 0xf0000000) == 0)
size += m_endAddress - i - 1;
else if(!i->f && i->next == 0)
else if(!i->f && (((u32)i->next) & 0xf0000000) == 0)
size += m_endAddress - i - i->s - 1;
}

View File

@ -279,10 +279,14 @@ static u8 GetRequestedGameIOS(dir_discHdr *hdr)
wbfs_disc_t *disc = WBFS_OpenDisc((u8*)&hdr->id, hdr->path);
if(disc != NULL)
{
u8 *titleTMD = NULL;
u32 tmd_size = wbfs_extract_file(disc, (char*)"TMD", (void**)&titleTMD);
if(titleTMD != NULL && tmd_size > 0x18B)
IOS = titleTMD[0x18B];
void *titleTMD = NULL;
u32 tmd_size = wbfs_extract_file(disc, (char*)"TMD", &titleTMD);
if(titleTMD != NULL)
{
if(tmd_size > 0x18B)
IOS = *((u8*)titleTMD + 0x18B);
MEM2_free(titleTMD);
}
WBFS_CloseDisc(disc);
}
WBFS_Close();