Yellow MoonByte order

The texture that outlived its file

Leaving a jungle area crashed the PC port. The crash site was innocent: a scrolling texture had been pointing at freed memory for five thousand frames.

Log 124 min read

Endymion was working through the jungle, climbing between the treehouses, and walked out the far end. The PC port crashed on the way out. They’d been playing with the session recorder on, as always, so I had their exact button presses from the moment they loaded their save.

Port, frame 74,049Port, frame 74,049. Vyse at one of the jungle’s huts.
Vyse at one of the jungle’s huts. Remember this frame: something gets freed here that matters five thousand frames later.

The crash that wasn’t the crash

Replaying the recording crashed at the same frame, 79,533, as the area was being torn down. The crash report pointed at a small function that looks a key up in the scene’s loaded data files, and the address it died reading was 4. In other words, it took the start of a list of records from a file’s header, found zero there, and read from just past zero.

The file it was reading was a route file, the data a scene’s scripted events run on, and its whole header was zeros. That’s not a file with no records. That’s a file something had written over.

My first guess was a stale entry: a file freed but still listed, so the lookup read whatever had moved into its memory. I had the debugger log every file the game registered and every free, all the way through the session. Every route file was taken off the list before it was freed. So that wasn’t it.

My second move was a wrong turn. I made the lookup skip a file with no records and ran the replay again. It crashed a moment later, inside the memory allocator, on a heap record smashed into garbage. Something was writing where it shouldn’t, and the lookup had only been the first thing to trip over it.

A watchpoint on the header

The question became who writes zeros over that header. A debugger watchpoint answers exactly that: it stops the program the moment a chosen piece of memory changes. I set one on the header of each route file as the game loaded it. It fired during the teardown, in the function that resets scrolling textures.

You’ve seen these: the flowing water, the shimmering surfaces. The game animates a model by sliding its texture coordinates every frame, and it keeps a “scroll slot” for each one. When an area ends, it writes the original coordinates back into the model’s data before letting the slot go.

This slot’s model wasn’t in the route file being overwritten. The strips it pointed at lay in memory that another route file had held until frame 74,049, when the game freed that file. The slot stayed alive, still pointing at the old address.

Three moments in the same stretch of memory. At frame 74,049 a route file is freed, but a scrolling texture still points into it. At 75,094 the port puts a new route file in the same place. At 79,533 the area teardown resets the scrolling texture, reads the new file's bytes as if they were the old model's, and writes over the new file's header. ONE STRETCH OF MEMORY, THREE MOMENTS FRAME 74,049 route file A (freed) · model strips scroll slot still points here FRAME 75,094 something else route file B the port reuses the space FRAME 79,533 read as strips: garbage header overwritten teardown resets the stale slot On the GameCube the freed bytes would still hold the old strips, so the reset rewrites them harmlessly. The fix: when a route file is freed, the slots pointing into it go with it.
The same stretch of memory at three moments. Drawn for this post from the debugger’s addresses.

At frame 75,094, the port’s allocator put a new route file right on top of that space. Then, at the teardown, the reset read the new file’s bytes as if they were the old model’s strips. They made no sense as strips: a count of 9,999 blocks, and an opcode the game doesn’t know, which it steps over two bytes at a time. So it marched along writing texture values every two bytes, across the new file’s header and on into the heap’s own records.

Why the GameCube never noticed

On the console, a freed block isn’t cleared. If nothing new lands there, the old model’s strips are still sitting in it, and the reset rewrites them with the values they already hold. Nothing breaks. That part is inference: I didn’t watch it happen on a console or in Dolphin, because the teardown draws nothing to compare.

I’d met this bug before. Early in the port, a scroll slot outlived a model file that a cutscene script unloaded, and the fix taught that unload path to drop the slots pointing into its file first. Route files get freed by a different path, and nobody had told that one. Now it detaches the slots too, using the same code, which now lives in one place for both.

Port, frame 79,700Port, frame 79,700. With the fix, the replay carries on past the teardown into the next area.
With the fix, the replay carries on past the teardown into the next area.

The lesson I wrote down for myself is the general one. When a fix guards one way of freeing something, find every other way the game frees the same kind of thing.