From bedbaf39789c61494f5d91d98c4f50696ab7ce1d Mon Sep 17 00:00:00 2001 From: Vicki Pfau Date: Mon, 26 Oct 2020 21:25:49 -0700 Subject: [PATCH] GBA Serialize: Ensure program counter is aligned when loading --- CHANGES | 1 + src/gba/serialize.c | 5 +++++ 2 files changed, 6 insertions(+) diff --git a/CHANGES b/CHANGES index f6597ab52..becbef98d 100644 --- a/CHANGES +++ b/CHANGES @@ -71,6 +71,7 @@ Other fixes: - GBA: Disable more checks when loading GS save with checks disabled (fixes mgba.io/i/1851) - GBA: Fix endianness issues in renderer proxy - GBA Core: Fix memory leak when loading symbols + - GBA Serialize: Ensure program counter is aligned when loading - Qt: Force OpenGL paint engine creation thread (fixes mgba.io/i/1642) - Qt: Fix static compilation in MinGW (fixes mgba.io/i/1769) - Qt: Fix a race condition in the frame inspector diff --git a/src/gba/serialize.c b/src/gba/serialize.c index 0e3c43f8c..464cef460 100644 --- a/src/gba/serialize.c +++ b/src/gba/serialize.c @@ -154,6 +154,11 @@ bool GBADeserialize(struct GBA* gba, const struct GBASerializedState* state) { LOAD_32(gba->cpu->bankedSPSRs[i], i * sizeof(gba->cpu->bankedSPSRs[0]), state->cpu.bankedSPSRs); } gba->cpu->privilegeMode = gba->cpu->cpsr.priv; + uint32_t pcMask = (gba->cpu->executionMode == MODE_THUMB ? WORD_SIZE_THUMB : WORD_SIZE_ARM) - 1; + if (gba->cpu->gprs[ARM_PC] & pcMask) { + mLOG(GBA_STATE, WARN, "Savestate has unaligned PC and is probably corrupted"); + gba->cpu->gprs[ARM_PC] &= ~pcMask; + } gba->cpu->memory.setActiveRegion(gba->cpu, gba->cpu->gprs[ARM_PC]); if (state->biosPrefetch) { LOAD_32(gba->memory.biosPrefetch, 0, &state->biosPrefetch);