fix AudioTable struct and data to avoid UB

This commit is contained in:
Alejandro Javier Asenjo Nitti 2024-08-01 22:02:17 -03:00
parent 4b7393dc50
commit 45d6470c67
5 changed files with 147 additions and 150 deletions

View File

@ -99,7 +99,7 @@ else
# we support Microsoft extensions such as anonymous structs, which the compiler does support but warns for their usage. Surpress the warnings with -woff.
CFLAGS += -G 0 -non_shared -fullwarn -verbose -Xcpluscomm $(IINC) -nostdinc -Wab,-r4300_mul -woff 649,838,712,516
MIPS_VERSION := -mips2
WARNINGS := -fullwarn -verbose -woff 624,649,838,712,516,513,596,564,594,709
WARNINGS := -fullwarn -verbose -woff 624,649,838,712,516,513,596,564,594,709,807
endif
ifeq ($(COMPILER),ido)

View File

@ -1,7 +1,7 @@
/**
* This file is provisional, some of the structs need to be corrected, and others might be unused
* This file is only meant to be used as reference to help audio decompilation
*/
*/
#ifndef SF64_AUDIO_H
#define SF64_AUDIO_H
@ -221,11 +221,11 @@ typedef struct {
typedef struct {
/* 0x00 */ s32 order;
/* 0x04 */ s32 numPredictors;
#ifdef AVOID_UB
#ifdef AVOID_UB
/* 0x08 */ u64 book[]; // size 8 * order * numPredictors.
#else
#else
/* 0x08 */ u64 book[1]; // size 8 * order * numPredictors.
#endif
#endif
} AdpcmBook; // size >= 8, 0x8 aligned
typedef struct {
@ -507,14 +507,14 @@ typedef struct SequenceLayer {
/* 0x38 */ s16 delay;
/* 0x3A */ s16 gateDelay;
/* 0x3C */ s16 delay2;
/* 0x40 */ struct Note *note;
/* 0x40 */ struct Note* note;
/* 0x44 */ Instrument* instrument;
/* 0x48 */ TunedSample *tunedSample;
/* 0x4C */ SequenceChannel *channel;
/* 0x48 */ TunedSample* tunedSample;
/* 0x4C */ SequenceChannel* channel;
/* 0x50 */ SeqScriptState state;
/* 0x6C */ AudioListItem listItem;
/* 0x7C */ char pad7C[4];
} SequenceLayer; //size = 0x80
} SequenceLayer; // size = 0x80
typedef struct UnkStruct_800097A8 {
/* 0x00 */ s16* unk_0;
@ -834,19 +834,12 @@ typedef struct {
/* 0x00 */ s16 numEntries;
/* 0x02 */ s16 unkMediumParam;
/* 0x04 */ u32 romAddr;
/* 0x08 */ char pad[0x8];
/* 0x08 */ char pad[8];
} AudioTableBase;
typedef struct {
/* 0x00 */ s16 numEntries;
/* 0x02 */ s16 unkMediumParam;
/* 0x04 */ u32 romAddr;
/* 0x08 */ char pad[0x8];
#ifdef AVOID_UB
/* 0x00 */ AudioTableBase base;
/* 0x10 */ AudioTableEntry entries[]; // (dynamic size)
#else
/* 0x10 */ AudioTableEntry entries[1]; // (dynamic size)
#endif
} AudioTable; // size >= 0x20
typedef struct SampleDma {
@ -914,13 +907,13 @@ typedef struct {
} SoundTestTrack; // size: 0x6
typedef struct {
/* 0x00 */ f32 *xPos;
/* 0x04 */ f32 *yPos;
/* 0x08 */ f32 *zPos;
/* 0x00 */ f32* xPos;
/* 0x04 */ f32* yPos;
/* 0x08 */ f32* zPos;
/* 0x0C */ u8 token;
/* 0x10 */ f32 *freqMod;
/* 0x14 */ f32 *volMod;
/* 0x18 */ s8 *reverbAdd;
/* 0x10 */ f32* freqMod;
/* 0x14 */ f32* volMod;
/* 0x18 */ s8* reverbAdd;
/* 0x1C */ f32 distance;
/* 0x20 */ u32 priority;
/* 0x24 */ u32 sfxId;
@ -1022,12 +1015,12 @@ typedef struct {
#define SEQ_HEX0(seqId) ((seqId) >> 28 & 0xFF)
#define SEQ_HEX1(seqId) (((seqId) & (0xF << 24)) >> 24 & 0xFF)
#define SEQ_BYTE4(seqId) ((seqId) & 0xFF)
#define SEQ_BYTE3(seqId) (((seqId) & (0xFF << 8)) >>8 & 0xFF)
#define SEQ_BYTE2(seqId) (((seqId) & (0xFF << 16)) >>13 & 0xFFFF)
#define SEQ_BYTE4(seqId) ((seqId) &0xFF)
#define SEQ_BYTE3(seqId) (((seqId) & (0xFF << 8)) >> 8 & 0xFF)
#define SEQ_BYTE2(seqId) (((seqId) & (0xFF << 16)) >> 13 & 0xFFFF)
// audio_synthesis
void func_80008780(f32 *, s32, f32 *);
void func_80008780(f32*, s32, f32*);
Acmd* func_80009B64(Acmd* aList, s32* cmdCount, s16* aiBufStart, s32 aiBufLen);
// audio_effects
@ -1077,7 +1070,7 @@ void func_800128B4(void);
void func_80012964(NotePool* pool);
void func_80012AC4(NotePool* pool, s32);
void func_80012C40(Note* note);
Note *func_8001301C(SequenceLayer* layer);
Note* func_8001301C(SequenceLayer* layer);
void func_800132E8(void);
// audio_seqplayer
@ -1091,18 +1084,15 @@ void func_800168BC(void);
// audio_thread
void AudioThread_ScheduleProcessCmds(void);
u32 AudioThread_GetAsyncLoadStatus(u32 *);
u32 AudioThread_GetAsyncLoadStatus(u32*);
u8* AudioThread_GetFontsForSequence(s32 seqId, u32* outNumFonts);
bool AudioThread_ResetComplete(void);
void AudioThread_ResetAudioHeap(s32);
void AudioThread_Init(void);
extern AudioTableBase gSampleBankTableInit;
// extern AudioTableEntry gSampleBankTableInitEntries[];
extern AudioTableBase gSeqTableInit;
// extern AudioTableEntry gSeqTableInitEntries[];
extern AudioTableBase gSoundFontTableInit;
// extern AudioTableEntry gSoundFontTableInitEntries[];
extern AudioTable gSampleBankTableInit;
extern AudioTable gSeqTableInit;
extern AudioTable gSoundFontTableInit;
extern u8 gSeqFontTableInit[];
extern AudioSpec gAudioSpecs[];
@ -1150,7 +1140,7 @@ extern u8 gSeqLoadStatus[256];
extern volatile u8 gAudioResetStep;
extern u8 gAudioSpecId;
extern s32 gResetFadeoutFramesLeft;
extern u8 sAudioContextPad1000[0x1000];// 0x1000 gap
extern u8 sAudioContextPad1000[0x1000]; // 0x1000 gap
extern Note* gNotes;
// 0x4
extern SequencePlayer gSeqPlayers[4];
@ -1186,10 +1176,10 @@ extern u8 gSampleDmaReuseQueue1RdPos;
extern u8 gSampleDmaReuseQueue2RdPos;
extern u8 gSampleDmaReuseQueue1WrPos;
extern u8 gSampleDmaReuseQueue2WrPos;
extern AudioTable *gSequenceTable;
extern AudioTable* gSequenceTable;
extern AudioTable* gSoundFontTable;
extern AudioTable *gSampleBankTable;
extern u8 *gSeqFontTable;
extern AudioTable* gSampleBankTable;
extern u8* gSeqFontTable;
extern s16 gNumSequences;
extern SoundFont* gSoundFontList;
// 0x4

View File

@ -915,7 +915,7 @@ void AudioHeap_DiscardSampleCacheEntry(SampleCacheEntry* entry) {
s32 sampleBankId2;
s32 sampleBankId1;
numFonts = gSoundFontTable->numEntries;
numFonts = gSoundFontTable->base.numEntries;
for (fondId = 0; fondId < numFonts; fondId++) {
sampleBankId1 = gSoundFontList[fondId].sampleBankId1;
sampleBankId2 = gSoundFontList[fondId].sampleBankId2;
@ -974,7 +974,7 @@ SampleCacheEntry* AudioHeap_AllocPersistentSampleCacheEntry(u32 size) {
void AudioHeap_DiscardSampleCaches(void) {
s32 fontId;
s32 i;
s32 numFonts = gSoundFontTable->numEntries;
s32 numFonts = gSoundFontTable->base.numEntries;
s32 pad;
s32 sampleBankId2;
s32 sampleBankId1;

View File

@ -207,10 +207,10 @@ void AudioLoad_InitSampleDmaBuffers(s32 numNotes) {
void AudioLoad_InitTable(AudioTable* table, u8* romAddr, u16 unkMediumParam) {
s32 i;
table->unkMediumParam = unkMediumParam;
table->romAddr = romAddr;
table->base.unkMediumParam = unkMediumParam;
table->base.romAddr = romAddr;
for (i = 0; i < table->numEntries; i++) {
for (i = 0; i < table->base.numEntries; i++) {
if ((table->entries[i].size != 0) && (table->entries[i].medium == MEDIUM_CART)) {
table->entries[i].romAddr += (u32) romAddr;
}
@ -253,7 +253,8 @@ s32 AudioLoad_SyncLoadSample(Sample* sample, s32 fontId) {
return -1;
}
if (sample->medium == MEDIUM_UNK) {
AudioLoad_SyncDmaUnkMedium(sample->sampleAddr, sampleAddr, sample->size, gSampleBankTable->unkMediumParam);
AudioLoad_SyncDmaUnkMedium(sample->sampleAddr, sampleAddr, sample->size,
gSampleBankTable->base.unkMediumParam);
} else {
AudioLoad_SyncDma(sample->sampleAddr, sampleAddr, sample->size, sample->medium);
}
@ -509,7 +510,7 @@ void* AudioLoad_SyncLoad(u32 tableType, u32 id, s32* didAllocate) {
*didAllocate = true;
if (medium == MEDIUM_UNK) {
AudioLoad_SyncDmaUnkMedium(romAddr, ramAddr, size, table->unkMediumParam);
AudioLoad_SyncDmaUnkMedium(romAddr, ramAddr, size, table->base.unkMediumParam);
} else {
AudioLoad_SyncDma(romAddr, ramAddr, size, medium);
}
@ -882,11 +883,11 @@ void AudioLoad_Init(void) {
gSoundFontTable = (AudioTable*) &gSoundFontTableInit;
gSampleBankTable = (AudioTable*) &gSampleBankTableInit;
gSeqFontTable = gSeqFontTableInit;
gNumSequences = gSequenceTable->numEntries;
gNumSequences = gSequenceTable->base.numEntries;
AudioLoad_InitTable(gSequenceTable, SEGMENT_ROM_START(audio_seq), gSequenceMedium);
AudioLoad_InitTable(gSoundFontTable, SEGMENT_ROM_START(audio_bank), gSoundFontMedium);
AudioLoad_InitTable(gSampleBankTable, SEGMENT_ROM_START(audio_table), gSampleBankMedium);
numFonts = gSoundFontTable->numEntries;
numFonts = gSoundFontTable->base.numEntries;
gSoundFontList = AudioHeap_Alloc(&gInitPool, numFonts * sizeof(SoundFont));
for (i = 0; i < numFonts; i++) {
gSoundFontList[i].sampleBankId1 = (gSoundFontTable->entries[i].shortData1 >> 8) & 0xFF;
@ -1003,7 +1004,7 @@ void AudioLoad_ProcessSlowLoads(s32 resetStatus) {
} else if (slowLoad->bytesRemaining < 0x1000) {
if (slowLoad->medium == MEDIUM_UNK) {
AudioLoad_DmaSlowCopyUnkMedium(slowLoad->curDevAddr, slowLoad->curRamAddr,
slowLoad->bytesRemaining, sampleBankTable->unkMediumParam);
slowLoad->bytesRemaining, sampleBankTable->base.unkMediumParam);
} else {
AudioLoad_DmaSlowCopy(&gSlowLoads.slowLoad[i], slowLoad->bytesRemaining);
}
@ -1011,7 +1012,7 @@ void AudioLoad_ProcessSlowLoads(s32 resetStatus) {
} else {
if (slowLoad->medium == MEDIUM_UNK) {
AudioLoad_DmaSlowCopyUnkMedium(slowLoad->curDevAddr, slowLoad->curRamAddr, 0x1000,
sampleBankTable->unkMediumParam);
sampleBankTable->base.unkMediumParam);
} else {
AudioLoad_DmaSlowCopy(&gSlowLoads.slowLoad[i], 0x1000);
}
@ -1161,7 +1162,7 @@ void AudioLoad_ProcessAsyncLoad(AudioAsyncLoad* asyncLoad, s32 resetStatus) {
} else if (asyncLoad->bytesRemaining < asyncLoad->chunkSize) {
if (asyncLoad->medium == MEDIUM_UNK) {
AudioLoad_AsyncDmaUnkMedium(asyncLoad->curDevAddr, asyncLoad->curRamAddr, asyncLoad->bytesRemaining,
sampleTable->unkMediumParam);
sampleTable->base.unkMediumParam);
} else {
AudioLoad_AsyncDma(asyncLoad, asyncLoad->bytesRemaining);
}
@ -1169,7 +1170,7 @@ void AudioLoad_ProcessAsyncLoad(AudioAsyncLoad* asyncLoad, s32 resetStatus) {
} else {
if (asyncLoad->medium == MEDIUM_UNK) {
AudioLoad_AsyncDmaUnkMedium(asyncLoad->curDevAddr, asyncLoad->curRamAddr, asyncLoad->chunkSize,
sampleTable->unkMediumParam);
sampleTable->base.unkMediumParam);
} else {
AudioLoad_AsyncDma(asyncLoad, asyncLoad->chunkSize);
}
@ -1296,7 +1297,7 @@ s32 AudioLoad_RelocateFontAndPreloadSamples(s32 fontId, u32 fontDataAddr, Sample
case AUDIOLOAD_SYNC:
if (sample->medium == MEDIUM_UNK) {
AudioLoad_SyncDmaUnkMedium(sample->sampleAddr, sampleRamAddr, sample->size,
gSampleBankTable->unkMediumParam);
gSampleBankTable->base.unkMediumParam);
sample->sampleAddr = sampleRamAddr;
sample->medium = MEDIUM_RAM;
} else {

View File

@ -1,16 +1,19 @@
#include "sys.h"
#include "sf64audio_provisional.h"
AudioTableBase gSampleBankTableInit = { 4, 0, 0 };
AudioTableEntry gSampleBankTableInitEntries[4] = {
AudioTable gSampleBankTableInit = {
{ 4, 0, 0 },
{
{ 0x000000, 0x0E1E30, MEDIUM_CART, CACHEPOLICY_4 },
{ 0x0E1E30, 0x0FF9D0, MEDIUM_CART, CACHEPOLICY_4 },
{ 0x1E1800, 0x497480, MEDIUM_CART, CACHEPOLICY_4 },
{ 0x678C80, 0x0C3900, MEDIUM_CART, CACHEPOLICY_4 },
},
};
AudioTableBase gSeqTableInit = { SEQ_ID_MAX, 0, 0 };
AudioTableEntry gSeqTableInitEntries[SEQ_ID_MAX] = {
AudioTable gSeqTableInit = {
{ SEQ_ID_MAX, 0, 0 },
{
{ 0x00000, 0x3AF0, MEDIUM_CART, CACHEPOLICY_0 }, { 0x03AF0, 0x56B0, MEDIUM_CART, CACHEPOLICY_0 },
{ 0x091A0, 0x2D80, MEDIUM_CART, CACHEPOLICY_3 }, { 0x0BF20, 0x1410, MEDIUM_CART, CACHEPOLICY_3 },
{ 0x0D330, 0x1600, MEDIUM_CART, CACHEPOLICY_3 }, { 0x0E930, 0xF20, MEDIUM_CART, CACHEPOLICY_3 },
@ -44,6 +47,7 @@ AudioTableEntry gSeqTableInitEntries[SEQ_ID_MAX] = {
{ 0x35E00, 0xAB0, MEDIUM_CART, CACHEPOLICY_3 }, { 0x368B0, 0x13D0, MEDIUM_CART, CACHEPOLICY_3 },
{ 0x37C80, 0x250, MEDIUM_CART, CACHEPOLICY_3 }, { 0x37ED0, 0x6F0, MEDIUM_CART, CACHEPOLICY_3 },
{ 0x385C0, 0xFB0, MEDIUM_CART, CACHEPOLICY_3 }, { 0x39570, 0x1780, MEDIUM_CART, CACHEPOLICY_3 },
},
};
#define SOUNDFONT_ENTRY(offset, size, medium, cachePolicy, bank1, bank2, numInst, numDrums) \
{ \
@ -51,8 +55,9 @@ AudioTableEntry gSeqTableInitEntries[SEQ_ID_MAX] = {
(((numInst) &0xFF) << 8) | ((numDrums) &0xFF) \
}
AudioTableBase gSoundFontTableInit = { 33, 0, 0 };
AudioTableEntry gSoundFontTableInitEntries[33] = {
AudioTable gSoundFontTableInit = {
{ 33, 0, 0 },
{
SOUNDFONT_ENTRY(0x00000, 0x2F00, MEDIUM_CART, CACHEPOLICY_0, SAMPLES_SFX, SAMPLES_NONE, 127, 0),
SOUNDFONT_ENTRY(0x02F00, 0x0FE0, MEDIUM_CART, CACHEPOLICY_0, SAMPLES_MAP, SAMPLES_NONE, 127, 0),
SOUNDFONT_ENTRY(0x03EE0, 0x0640, MEDIUM_CART, CACHEPOLICY_0, SAMPLES_VOICE, SAMPLES_NONE, 127, 0),
@ -86,6 +91,7 @@ AudioTableEntry gSoundFontTableInitEntries[33] = {
SOUNDFONT_ENTRY(0x1C370, 0x09E0, MEDIUM_CART, CACHEPOLICY_3, SAMPLES_INST, SAMPLES_NONE, 10, 64),
SOUNDFONT_ENTRY(0x1CD50, 0x0A00, MEDIUM_CART, CACHEPOLICY_3, SAMPLES_INST, SAMPLES_NONE, 10, 64),
SOUNDFONT_ENTRY(0x1D750, 0x08D0, MEDIUM_CART, CACHEPOLICY_3, SAMPLES_INST, SAMPLES_NONE, 10, 64),
},
};
#define AS_BYTES(x) (((x) >> 8) & 0xFF), ((x) &0xFF)