Skip to content

Commit

Permalink
Tests: detect task leaks (#5528)
Browse files Browse the repository at this point in the history
  • Loading branch information
mrgriffin authored Oct 22, 2024
1 parent d11fd21 commit 1421ed1
Show file tree
Hide file tree
Showing 6 changed files with 3,350 additions and 13 deletions.
1 change: 1 addition & 0 deletions include/sound.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,5 +45,6 @@ void SE12PanpotControl(s8 pan);
bool8 IsSEPlaying(void);
bool8 IsBGMPlaying(void);
bool8 IsSpecialSEPlaying(void);
void Task_DuckBGMForPokemonCry(u8 taskId);

#endif // GUARD_SOUND_H
13 changes: 13 additions & 0 deletions src/battle_main.c
Original file line number Diff line number Diff line change
Expand Up @@ -1780,7 +1780,20 @@ void CB2_QuitRecordedBattle(void)
m4aMPlayStop(&gMPlayInfo_SE1);
m4aMPlayStop(&gMPlayInfo_SE2);
if (gTestRunnerEnabled)
{
// Clean up potentially-leaking tasks.
// I think these leak when the battle ends soon after a
// battler is fainted.
u8 taskId;
taskId = FindTaskIdByFunc(Task_PlayerController_RestoreBgmAfterCry);
if (taskId != TASK_NONE)
DestroyTask(taskId);
taskId = FindTaskIdByFunc(Task_DuckBGMForPokemonCry);
if (taskId != TASK_NONE)
DestroyTask(taskId);

TestRunner_Battle_AfterLastTurn();
}
FreeRestoreBattleData();
FreeAllWindowBuffers();
SetMainCallback2(gMain.savedCallback);
Expand Down
3 changes: 1 addition & 2 deletions src/sound.c
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ extern struct ToneData gCryTable_Reverse[];

static void Task_Fanfare(u8 taskId);
static void CreateFanfareTask(void);
static void Task_DuckBGMForPokemonCry(u8 taskId);
static void RestoreBGMVolumeAfterPokemonCry(void);

static const struct Fanfare sFanfares[] = {
Expand Down Expand Up @@ -513,7 +512,7 @@ bool8 IsCryPlaying(void)
return FALSE;
}

static void Task_DuckBGMForPokemonCry(u8 taskId)
void Task_DuckBGMForPokemonCry(u8 taskId)
{
if (gPokemonCryBGMDuckingCounter)
{
Expand Down
18 changes: 17 additions & 1 deletion test/test_runner.c
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include "main.h"
#include "malloc.h"
#include "random.h"
#include "task.h"
#include "constants/characters.h"
#include "test_runner.h"
#include "test/test.h"
Expand Down Expand Up @@ -190,6 +191,7 @@ void CB2_TestRunner(void)
else
gTestRunnerState.timeoutSeconds = UINT_MAX;
InitHeap(gHeap, HEAP_SIZE);
ResetTasks();
EnableInterrupts(INTR_FLAG_TIMER2);
REG_TM2CNT_L = UINT16_MAX - (274 * 60); // Approx. 1 second.
REG_TM2CNT_H = TIMER_ENABLE | TIMER_INTR_ENABLE | TIMER_1024CLK;
Expand Down Expand Up @@ -243,6 +245,7 @@ void CB2_TestRunner(void)
if (gTestRunnerState.result == TEST_RESULT_PASS
&& !gTestRunnerState.expectLeaks)
{
int i;
const struct MemBlock *head = HeapHead();
const struct MemBlock *block = head;
do
Expand All @@ -251,7 +254,7 @@ void CB2_TestRunner(void)
|| !(EWRAM_START <= (uintptr_t)block->next && (uintptr_t)block->next < EWRAM_END)
|| (block->next <= block && block->next != head))
{
Test_MgbaPrintf("gHeap corrupted block at 0x%p", block);
Test_MgbaPrintf("gHeap corrupted block at %p", block);
gTestRunnerState.result = TEST_RESULT_ERROR;
break;
}
Expand All @@ -268,6 +271,15 @@ void CB2_TestRunner(void)
block = block->next;
}
while (block != head);

for (i = 0; i < NUM_TASKS; i++)
{
if (gTasks[i].isActive)
{
Test_MgbaPrintf("%p: task not freed", gTasks[i].func);
gTestRunnerState.result = TEST_RESULT_FAIL;
}
}
}

if (gTestRunnerState.test->runner == &gAssumptionsRunner)
Expand Down Expand Up @@ -585,6 +597,9 @@ static s32 MgbaVPrintf_(const char *fmt, va_list va)
p = va_arg(va, unsigned);
{
s32 n;
i = MgbaPutchar_(i, '<');
i = MgbaPutchar_(i, '0');
i = MgbaPutchar_(i, 'x');
for (n = 0; n < 7; n++)
{
unsigned nybble = (p >> (24 - (4*n))) & 0xF;
Expand All @@ -593,6 +608,7 @@ static s32 MgbaVPrintf_(const char *fmt, va_list va)
else
i = MgbaPutchar_(i, 'a' + nybble - 10);
}
i = MgbaPutchar_(i, '>');
}
break;
case 'q':
Expand Down
Loading

0 comments on commit 1421ed1

Please sign in to comment.