all repos — mgba @ 86003496b0af95e897ab4ceea86e8d32efe01d75

mGBA Game Boy Advance Emulator

Handle out-of-bounds SRAM and ROM reads
Jeffrey Pfau jeffrey@endrift.com
Fri, 24 Oct 2014 01:09:46 -0700
commit

86003496b0af95e897ab4ceea86e8d32efe01d75

parent

5450bd8d59dbd475f765c87bf428a2adcaeece94

1 files changed, 16 insertions(+), 0 deletions(-)

jump to
M src/gba/gba-memory.csrc/gba/gba-memory.c

@@ -198,7 +198,9 @@ wait += waitstatesRegion[address >> BASE_OFFSET]; \

if ((address & (SIZE_CART0 - 1)) < memory->romSize) { \ LOAD_32(value, address & (SIZE_CART0 - 1), memory->rom); \ } else { \ + GBALog(gba, GBA_LOG_GAME_ERROR, "Out of bounds ROM Load32: 0x%08X", address); \ value = (address >> 1) & 0xFFFF; \ + value |= value << 16; \ } #define LOAD_SRAM \

@@ -315,6 +317,9 @@ case REGION_CART2:

wait = memory->waitstatesNonseq16[address >> BASE_OFFSET]; if ((address & (SIZE_CART0 - 1)) < memory->romSize) { LOAD_16(value, address & (SIZE_CART0 - 1), memory->rom); + } else { + GBALog(gba, GBA_LOG_GAME_ERROR, "Out of bounds ROM Load16: 0x%08X", address); + value = (address >> 1) & 0xFFFF; \ } break; case REGION_CART2_EX:

@@ -323,6 +328,9 @@ if (memory->savedata.type == SAVEDATA_EEPROM) {

value = GBASavedataReadEEPROM(&memory->savedata); } else if ((address & (SIZE_CART0 - 1)) < memory->romSize) { LOAD_16(value, address & (SIZE_CART0 - 1), memory->rom); + } else { + GBALog(gba, GBA_LOG_GAME_ERROR, "Out of bounds ROM Load16: 0x%08X", address); + value = (address >> 1) & 0xFFFF; \ } break; case REGION_CART_SRAM:

@@ -393,6 +401,9 @@ case REGION_CART2_EX:

wait = memory->waitstatesNonseq16[address >> BASE_OFFSET]; if ((address & (SIZE_CART0 - 1)) < memory->romSize) { value = ((int8_t*) memory->rom)[address & (SIZE_CART0 - 1)]; + } else { + GBALog(gba, GBA_LOG_GAME_ERROR, "Out of bounds ROM Load8: 0x%08X", address); + value = (address >> 1) & 0xFF; \ } break; case REGION_CART_SRAM:

@@ -406,6 +417,9 @@ if (memory->savedata.type == SAVEDATA_SRAM) {

value = memory->savedata.data[address & (SIZE_CART_SRAM - 1)]; } else if (memory->savedata.type == SAVEDATA_FLASH512 || memory->savedata.type == SAVEDATA_FLASH1M) { value = GBASavedataReadFlash(&memory->savedata, address); + } else { + GBALog(gba, GBA_LOG_GAME_ERROR, "Reading from non-existent SRAM: 0x%08X", address); + value = 7; } break; default:

@@ -614,6 +628,8 @@ if (memory->savedata.type == SAVEDATA_FLASH512 || memory->savedata.type == SAVEDATA_FLASH1M) {

GBASavedataWriteFlash(&memory->savedata, address, value); } else if (memory->savedata.type == SAVEDATA_SRAM) { memory->savedata.data[address & (SIZE_CART_SRAM - 1)] = value; + } else { + GBALog(gba, GBA_LOG_GAME_ERROR, "Writing to non-existent SRAM: 0x%08X", address); } wait = memory->waitstatesNonseq16[REGION_CART_SRAM]; break;