**Save state loaded after a restart leaves the FC1004 writing through stale pointers** Disclosure: this was found, fixed and tested by an AI model (Claude Opus 5.5, Anthropic) working with me; I reviewed the code, the tests and this text. Thank you for Nuked-MD. **What happens.** Save with F5, quit, start Nuked-MD again and load with F7. `load_state` reads `ym` back with one `load_blob(&ym, sizeof(ym), f)`, and that includes six pointers `FC1004_Init` aims at fields of the chip itself: - `arb.ext_vaddress_out`, `arb.ext_data_out` - `ioc.ext_vdata_out`, `ioc.ext_zdata_out`, `ioc.ext_vaddress_out` - `tmss.ext_data_out` The file holds the addresses of the process that wrote it. With a PIE build and ASLR enabled, `ym` is relocated between launches, so these pointers refer to addresses in the previous process. (The CMake build here is PIE by the compiler's default.) F5 then F7 in the same run is fine, since the addresses have not moved. **What we saw** on Linux x86-64 (gcc 15.2, SDL 2.32.10, Sonic The Hedgehog), with the CMake-built executable and one F5 or F7 key event injected through an `LD_PRELOAD` wrapper on `SDL_PollEvent`, since this machine has no display: | | load in a new process | | --- | --- | | 594ca2a | segmentation fault, 2 of 2 | | 594ca2a with the change below | loads and keeps running, 2 of 2 | A second check built the same sources as a library, so a run could stop at an exact cycle count. Run A launched fresh, saved with `save_state` (what F5 does) and kept going. Run B launched fresh in a new process, loaded A's file (what F7 does) and ran on. Both stopped at the same cycle count, about 10 frames after the save, and wrote their whole state with `save_state`, with the six pointers set to zero since they differ between processes by design. With the change, B's file matched A's byte for byte (2 of 2); without it, B crashed before the stop point (2 of 2). With ASLR off (`setarch -R`), unchanged 594ca2a matched too. We did not test Windows. **A possible fix**: move the six assignments from `FC1004_Init` into `FC1004_InitPointers`, which `FC1004_Init` calls, and call it in `load_state` right after `ym` is read. Calling `FC1004_Init` there instead would also run `ARB_Init` and reset the arbiter's delay chains that were just loaded. The save-state format is unchanged. Existing save files remain readable; the six internal pointers are reconstructed after loading. Those six are the only pointers in the saved state as far as we can see. ```diff --- a/fc1004.c +++ b/fc1004.c @@ -26,7 +26,7 @@ #include "fc1004.h" -void FC1004_Init(fc1004_t *chip) +void FC1004_InitPointers(fc1004_t *chip) { chip->arb.ext_vaddress_out = &chip->o_vaddress; chip->arb.ext_data_out = &chip->o_vdata; @@ -34,6 +34,11 @@ void FC1004_Init(fc1004_t *chip) chip->ioc.ext_zdata_out = &chip->o_zdata; chip->ioc.ext_vaddress_out = &chip->o_vaddress; chip->tmss.ext_data_out = &chip->o_vdata; +} + +void FC1004_Init(fc1004_t *chip) +{ + FC1004_InitPointers(chip); ARB_Init(&chip->arb); } --- a/fc1004.h +++ b/fc1004.h @@ -144,6 +144,7 @@ typedef struct #pragma pack(pop) +void FC1004_InitPointers(fc1004_t *chip); void FC1004_Init(fc1004_t *chip); void FC1004_Destroy(fc1004_t* chip); void FC1004_Clock(fc1004_t *chip, int mclk, uint64_t cycles); --- a/savestate.c +++ b/savestate.c @@ -133,6 +133,8 @@ int load_state(const char* filename) if (load_blob(&ym, sizeof(ym), f)) return -1; + // The chip's internal pointers in the file are the writer's addresses: aim them again + FC1004_InitPointers(&ym); if (load_blob(&tmss_rom, sizeof(tmss_rom), f)) return -1; ``` Happy to open a PR if that is easier, or for you to do it your own way.