Loading save state sometimes causes crash on linux x64
Sometimes, loading a save state will cause ppsspp to crash. It doesn't happen that frequently, but spamming load 20-30 times will generally trigger it. (Reproduced on ppsspp 1.4.1 and also latest git source)
I did a bit of digging, and the cause seems to be that in the Jit constructor, it allocates a region for the .code pointer with AllocateExecutableMemory. This provides a hint to mmap aimed at allocating these in close regions of memory, but it looks like in the cases where it can't allocate the region exactly at the hint pointer (presumably where we overlap with some other mapped region), it can actually differ by quite a bit.
Eg. when debugging, an allocation with map_hint=0x40cc0000 returned an address way up in high memory (0x7ff8e08a9000). This seems to be the cause of the crash, as in OpArg::WriteRest in x86emitter.cpp, it's triggering the error assert about being out of range for RIP addressing. The crash itself happens later, with a useless call stack (I assume when running the JITted code), but presumably is triggered by the wrong offset that gets provided here.
The mmap manpage does suggest that "addr is taken to be a suggestion of a process address near which the mapping", but in practice it looks like it can differ quite a bit if it can't get the exact region. A brief look at the linux source seems to indicate that (on x86_64 at least), addr is only used if it can map that exact region. As such, it doesn't look like that "near the mapping" means much.
Yeah, this is a bug. The JIT should be fixed to fall back to slower methods of access if it can't get low memory.
And who knows, this might even affect Android.
I thought we were scanning and validating the mapped pointer? Sounds like we're just not validating it properly to ensure it is in range of RIP displacements.
Falling back to being slow randomly 1 out of 20-30 times doesn't sound great if we can avoid it. I don't like the idea of silently kicking it into slow gear sometimes.
-[Unknown]
Well, losing RIP doesn't mean "slow", it means very, very slightly slower. If we can't get low RAM for some reason it's better to not use RIP than to crash... Though it is kind of odd that it would happen.
Doing this will be a bit of a pain though. We have a lot of things like
STMXCSR(M(&mips_->temp));
which all need to add checking for RIP compatibility and worked around if not. Also things like ANDPS(tempxregs[i], M(&oneOneOneOne));
At least for the first case we do have a pointer to mips_ in a register already....
Well, what I mean is that x64 has so much usable address space.
https://github.com/hrydgard/ppsspp/blob/4b46bb4ecf92460b89c1d067efd7840fd453a138/Common/MemoryUtil.cpp#L203
We're probably just rolling in too wide a playground. But really, I think we should do what we do on 32 bit, and try several allocations until we get a good one with an address we like.
Otherwise we need to add a bunch of helpers or code to all those places. And it'd mean using a temp register, right? That seems like a great way to have a bug only 3-5% of the times you start PPSSPP...
-[Unknown]
I think this may be my bug when I was playing a game today on Arch community/ppsspp 1.4.2-1. I can confirm this one crashes for me too by repeatedly loading the save state until it crashes... as a test.
My bug is that when I use Rewind snapshot frequency (mem hog) feature to undo my failures, it can crash sometimes. I counted 6 crashes when playing. I think I'm almost done with the game too.
If you need logs, please let me know how I can get them to you. Thanks.
EDIT: Beating a game took me more than 30 crashes when playing with rewinding.
I went ahead and implemented the solution mentioned above, although have not yet verified that it fully fixes the problem - it should, though. #9834, will merge soon.
You'll be happy to know that I ran a simple loop command just now... repeatedly loading the save state every 2s until the counter reached 100. Not a single crash. Verified? Thank you for the fix. :-)
Great @lasers , thanks for testing!