Skip to content

Commit e8c5bac

Browse files
committed
Check the heaps for corruption
1 parent 330fae1 commit e8c5bac

File tree

12 files changed

+386
-5
lines changed

12 files changed

+386
-5
lines changed

ttyd-tools/rel/include/commonfunctions.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ void recheckJumpAndHammerLevels();
3939
uint32_t getCurrentPitFloor();
4040
void clearGSWFsRange(uint32_t lowerBound, uint32_t upperBound);
4141
ttyd::evtmgr::EvtWork *getCurrentEventWork();
42+
bool checkIfPointerIsValid(void *ptr);
4243
void *getLastPointerFromPath(void *address, int32_t *offset, uint32_t offsetAmount);;
4344

4445
}

ttyd-tools/rel/include/draw.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,7 @@ void drawPalaceSkipDetails();
114114
void drawActionCommandsTiming();
115115
void drawSettingsCurrentWork();
116116

117+
void drawHeapArrayErrors();
117118
void drawTitleScreenInfo();
118119
void drawFileSelectScreenInfo();
119120

ttyd-tools/rel/include/gc/os.h

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
#pragma once
2+
3+
#include <cstdint>
4+
5+
namespace gc::os {
6+
7+
struct ChunkInfo
8+
{
9+
ChunkInfo *prev;
10+
ChunkInfo *next;
11+
uint32_t size;
12+
} __attribute__((__packed__));
13+
14+
struct HeapInfo
15+
{
16+
uint32_t capacity;
17+
ChunkInfo *firstFree;
18+
ChunkInfo *firstUsed;
19+
} __attribute__((__packed__));
20+
21+
extern "C" {
22+
23+
extern HeapInfo *OSAlloc_HeapArray;
24+
extern int32_t OSAlloc_NumHeaps;
25+
26+
}
27+
28+
}

ttyd-tools/rel/include/global.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -603,6 +603,12 @@ struct SettingsStruct
603603
MemoryWatchStruct MemoryWatchSettings[30];
604604
};
605605

606+
struct CheckHeaps
607+
{
608+
const char **StandardHeapArray;
609+
const char **SmartHeapArray;
610+
};
611+
606612
extern Menus Menu[26];
607613
extern Cheats Cheat[20];
608614
extern bool Displays[9];
@@ -626,6 +632,7 @@ extern TrickDisplay PalaceSkip;
626632
extern OnScreenTimerDisplay OnScreenTimer;
627633
extern DisplayActionCommandTiming DisplayActionCommands;
628634
extern MemoryCardStruct MenuSettings;
635+
extern CheckHeaps CheckHeap;
629636

630637
extern const char *VersionNumber;
631638
extern uint8_t CheatsOrder[];
@@ -703,6 +710,7 @@ extern uint32_t BattleAddressesStart;
703710
extern uint32_t PiantaParlorAddressesStart;
704711
extern uint32_t seqMainAddress;
705712
extern uint32_t WorkAreaAddress;
713+
extern uint32_t SmartWorkAddress;
706714
extern uint32_t FieldItemsAddressesStart;
707715

708716
extern uint32_t GlobalWorkPointer;

ttyd-tools/rel/include/ttyd.eu.lst

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1508,6 +1508,20 @@
15081508
// w_atan2.c
15091509
80276394:atan2
15101510

1511+
// os.a
1512+
// OSAlloc.c
1513+
// text
1514+
// 8029A238:DLInsert
1515+
8029A2E4:OSAllocFromHeap
1516+
8029A3E0:OSFreeToHeap
1517+
8029A45C:OSSetCurrentHeap
1518+
8029A46C:OSInitAlloc
1519+
8029A4DC:OSCreateHeap
1520+
8029A548:OSDestroyHeap
1521+
// data
1522+
8042B9E0:OSAlloc_HeapArray
1523+
8042B9E4:OSAlloc_NumHeaps
1524+
15111525
// OSCache.c
15121526
// 8029A84C:DCEnable
15131527
// 8029A860:DCInvalidateRange

ttyd-tools/rel/include/ttyd.jp.lst

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1500,6 +1500,20 @@
15001500
// w_atan2.c
15011501
8026C588:atan2
15021502

1503+
// os.a
1504+
// OSAlloc.c
1505+
// text
1506+
// 802903E8:DLInsert
1507+
80290494:OSAllocFromHeap
1508+
80290590:OSFreeToHeap
1509+
8029060C:OSSetCurrentHeap
1510+
8029061C:OSInitAlloc
1511+
8029068C:OSCreateHeap
1512+
802906F8:OSDestroyHeap
1513+
// data
1514+
80418EA0:OSAlloc_HeapArray
1515+
80418EA4:OSAlloc_NumHeaps
1516+
15031517
// OSCache.c
15041518
// 802909FC:DCEnable
15051519
// 80290A10:DCInvalidateRange

ttyd-tools/rel/include/ttyd.us.lst

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1506,6 +1506,20 @@
15061506
// w_atan2.c
15071507
802725A0:atan2
15081508

1509+
// os.a
1510+
// OSAlloc.c
1511+
// text
1512+
// 80296400:DLInsert
1513+
802964AC:OSAllocFromHeap
1514+
802965A8:OSFreeToHeap
1515+
80296624:OSSetCurrentHeap
1516+
80296634:OSInitAlloc
1517+
802966A4:OSCreateHeap
1518+
80296710:OSDestroyHeap
1519+
// data
1520+
8041F070:OSAlloc_HeapArray
1521+
8041F074:OSAlloc_NumHeaps
1522+
15091523
// OSCache.c
15101524
// 80296A14:DCEnable
15111525
// 80296A28:DCInvalidateRange

ttyd-tools/rel/include/ttyd/memory.h

Lines changed: 38 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,41 @@
44

55
namespace ttyd::memory {
66

7+
struct SmartAllocationData
8+
{
9+
void *pMemory;
10+
uint32_t usedSize;
11+
// FileEntryData *pFileInfo;
12+
void *pFileInfo;
13+
uint16_t bUsed;
14+
uint8_t group;
15+
uint8_t pad_f;
16+
uint32_t unusedSize;
17+
SmartAllocationData *pNext;
18+
SmartAllocationData *pPrev;
19+
} __attribute__((__packed__)); // size 0x1C
20+
21+
struct SmartWork
22+
{
23+
void *pArena;
24+
SmartAllocationData allocations[2048];
25+
uint32_t unallocatedArenaStartSize;
26+
SmartAllocationData *pFirstUsed;
27+
SmartAllocationData *pLastUsed;
28+
SmartAllocationData *pFirstFree;
29+
SmartAllocationData *pLastFree;
30+
uint32_t lastFrameFreeCount;
31+
} __attribute__((__packed__)); // size 0xE01C
32+
33+
enum SmartAllocationGroup
34+
{
35+
kNone = 0,
36+
kArea,
37+
kBattle,
38+
kSingleFrame,
39+
kDoubleFrame,
40+
};
41+
742
extern "C" {
843

944
// memInit
@@ -15,9 +50,9 @@ void __memFree(uint32_t heap, void *ptr);
1550
// _mapFree
1651
// smartInit
1752
// smartReInit
18-
// smartAutoFree
19-
// smartFree
20-
// smartAlloc
53+
void smartAutoFree(uint32_t group);
54+
void smartFree(SmartAllocationData *pAlloc);
55+
SmartAllocationData *smartAlloc(uint32_t size, uint32_t group);
2156
// smartGarbage
2257
// smartTexObj
2358

ttyd-tools/rel/source/commonfunctions.cpp

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -278,14 +278,20 @@ ttyd::evtmgr::EvtWork *getCurrentEventWork()
278278
return ttyd::evtmgr::evtGetWork();
279279
}
280280

281+
bool checkIfPointerIsValid(void *ptr)
282+
{
283+
uint32_t ptrRaw = reinterpret_cast<uint32_t>(ptr);
284+
return ((ptrRaw >= 0x80000000) && (ptrRaw < 0x81800000));
285+
}
286+
281287
void *getLastPointerFromPath(void *address, int32_t *offset, uint32_t offsetAmount)
282288
{
283289
uint32_t tempAddress = reinterpret_cast<uint32_t>(address);
284290
if (offsetAmount == 0)
285291
{
286292
return address;
287293
}
288-
else if ((tempAddress >= 0x80000000) && (tempAddress < 0x81800000))
294+
else if (checkIfPointerIsValid(address))
289295
{
290296
tempAddress = *reinterpret_cast<uint32_t *>(tempAddress);
291297
}
@@ -297,7 +303,7 @@ void *getLastPointerFromPath(void *address, int32_t *offset, uint32_t offsetAmou
297303
uint32_t Counter = 0;
298304
for (uint32_t i = 0; i < (offsetAmount - 1); i++)
299305
{
300-
if ((tempAddress >= 0x80000000) && (tempAddress < 0x81800000))
306+
if (checkIfPointerIsValid(reinterpret_cast<void *>(tempAddress)))
301307
{
302308
tempAddress = *reinterpret_cast<uint32_t *>(tempAddress + offset[i]);
303309
}

ttyd-tools/rel/source/draw.cpp

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3727,6 +3727,43 @@ void drawSettingsCurrentWork()
37273727
Timer = tempTimer - 1;
37283728
}
37293729

3730+
void drawHeapArrayErrors()
3731+
{
3732+
uint32_t TextColor = 0xFFFFFFFF;
3733+
uint8_t Alpha = 0xFF;
3734+
int32_t PosX = -232;
3735+
int32_t PosY = 120;
3736+
float Scale = 0.6;
3737+
3738+
// Draw the standard heap text
3739+
if (CheckHeap.StandardHeapArray)
3740+
{
3741+
const char **tempHeapArray = CheckHeap.StandardHeapArray;
3742+
uint32_t Counter = 0;
3743+
3744+
while (tempHeapArray[Counter])
3745+
{
3746+
drawText(tempHeapArray[Counter], PosX, PosY, Alpha, TextColor, Scale);
3747+
PosY -= 20;
3748+
Counter++;
3749+
}
3750+
}
3751+
3752+
// Draw the smart heap text
3753+
if (CheckHeap.SmartHeapArray)
3754+
{
3755+
const char **tempHeapArray = CheckHeap.SmartHeapArray;
3756+
uint32_t Counter = 0;
3757+
3758+
while (tempHeapArray[Counter])
3759+
{
3760+
drawText(tempHeapArray[Counter], PosX, PosY, Alpha, TextColor, Scale);
3761+
PosY -= 20;
3762+
Counter++;
3763+
}
3764+
}
3765+
}
3766+
37303767
void drawTitleScreenInfo()
37313768
{
37323769
// Draw the window for the text

0 commit comments

Comments
 (0)