[RetroPlayer] Autosave can unmap a DMA render buffer during the frame upload (SIGSEGV)
Bug report
Describe the bug
Here is a clear and concise description of what the problem is:
RetroPlayer can crash with SIGSEGV during an autosave. This happens when frames are in DMA render buffers that take the coherency upload, i.e. the UDMABuf and DMAHeap buffer objects.
CRenderBufferDMA::GetMemory() and ReleaseMemory() map and unmap the buffer object directly. The mapping isn't counted:
- A second
GetMemory()is handed the existing mapping. The debug log saysCUDMABufferObject::GetMemory - already mapped. - The first
ReleaseMemory()unmaps it for everyone.
Two threads use the same buffer at the same time:
- Render thread: it uploads every frame in
CRenderBufferDMAOpenGL::UploadFromMemory()(and the GLES equivalent). That isGetMemory(), thenglTexImage2D()from the mapping, thenReleaseMemory(). - Savestate worker: on an autosave it reads the captured frame in
CRPRenderManager::SaveVideoFrame()andSaveThumbnail(). ThenFreeVideoFrame()callsReleaseMemory().
Whichever finishes first unmaps the buffer while the other is still reading it.
Expected Behavior
Here is a clear and concise description of what was expected to happen:
An autosave doesn't disturb the game being played.
Actual Behavior
Kodi crashes. Here, the savestate worker unmapped the buffer while the render thread was uploading from it:
Thread 1 "kodi-wayland" received signal SIGSEGV, Segmentation fault.
#0 __memcpy_avx_unaligned_erms () at ../sysdeps/x86_64/multiarch/memmove-vec-unaligned-erms.S:265
#1 0x00007fffe1408023 in ?? () from /usr/lib/x86_64-linux-gnu/libgallium-26.0.8-1ubuntu0.3.so
#2 0x00007fffe1408f59 in ?? () from /usr/lib/x86_64-linux-gnu/libgallium-26.0.8-1ubuntu0.3.so
#3 0x00007fffe13dd3d6 in ?? () from /usr/lib/x86_64-linux-gnu/libgallium-26.0.8-1ubuntu0.3.so
#4 0x00007fffe13df831 in ?? () from /usr/lib/x86_64-linux-gnu/libgallium-26.0.8-1ubuntu0.3.so
#5 KODI::RETRO::CRenderBufferDMAOpenGL::UploadFromMemory() at xbmc/cores/RetroPlayer/buffers/RenderBufferDMAOpenGL.cpp:74
#6 KODI::RETRO::CRenderBufferDMA::UploadTexture() at xbmc/cores/RetroPlayer/buffers/RenderBufferDMA.cpp:135
#8 KODI::RETRO::CRPRenderManager::RenderInternal(...) at xbmc/cores/RetroPlayer/rendering/RPRenderManager.cpp
#9 KODI::RETRO::CRPRenderManager::RenderWindow(bClear=true, ...)
#10 KODI::RETRO::CGUIGameRenderManager::Render(...)
#11 KODI::RETRO::CGUIRenderHandle::Render()
#12 KODI::RETRO::CGameWindowFullScreen::Render()
...
#18 CApplication::Render()
#19 CApplication::Run()The debug log just before it shows the second mapping:
debug <general>: Saving savestate to special://home/saves/Super Mario Bros. (World).nes/20261001_093410.sav
debug <general>: Wrote savestate of 260656 bytes
debug <general>: CUDMABufferObject::GetMemory - already mapped fd=43 map=0x7b26ac016000
debug <general>: RetroPlayer[SAVE]: Frame 577: reused rewind false, core 42 us, capture/handoff 259 us, background commit 5 ms, success truePossible Fix
Count CPU access in CRenderBufferDMA under a mutex:
- Map and start the sync on the first
GetMemory(). - End the sync and unmap on the last
ReleaseMemory().
Every caller already pairs the two calls, so the mapping then lasts until both readers are done.
I tested this on the same setup:
- Before: 4 crashes in 20 sessions of 30 to 80 seconds.
- After: 20 one-minute sessions, 33 shared mappings between them, no crashes.
The change is sunlollyking/xbmc@ee28e54d79 (branch retroplayer-dma-map-refcount, on master). I can open a PR with it.
To Reproduce
Steps to reproduce the behavior:
- Use Linux with EGL (Wayland or GBM) where RetroPlayer picks UDMABuf or DMAHeap buffers. The debug log shows
CRenderBufferDMA: using BufferObject type: CUDMABufferObject. - With autosave on (the default), play a software-rendered game, e.g. Super Mario Bros. on game.libretro.fceumm.
- Keep playing. Each autosave can trigger it.
already mappedin the debug log marks an overlap. It appeared in roughly 40% of sessions.- The crash needs the unmap to land mid-read. It happened in 4 of 31 sessions here.
- All 4 crashes were in sessions where the downstream branch also drew a full-screen overlay after the game, on the render thread. That probably shifts the timing. In 11 sessions without the overlay the overlap still showed, but nothing crashed.
Debuglog
The debug log can be found here:
The relevant lines are quoted above. I haven't attached the full log because it contains private artwork URLs. I can send a trimmed log if needed.
Screenshots
Here are some links or screenshots to help explain the problem:
N/A
Additional context or screenshots (if appropriate)
Here is some additional context or explanation that might help:
I reproduced this on a downstream branch. Its RetroPlayer buffer and savestate code is identical to master at 3133810855: RenderBufferDMA.*, RenderBufferDMAOpenGL*.cpp, UDMABufferObject.cpp, DMAHeapBufferObject.cpp and the SaveVideoFrame / SaveThumbnail / GetVideoFrame / FreeVideoFrame / TryCaptureVideoFrame functions. The zero-copy path pairs the calls the same way: a frame lent to a game client stays mapped until AddFrame() releases it. So the same change covers it.
Your Environment
Used Operating system:
Android
iOS
tvOS
Linux
macOS
Windows
Windows UWP
Operating system version/name: Ubuntu, kernel 7.0.0, Wayland. GPU: Intel Arc (Meteor Lake), Mesa 26.0.8, GL 4.6.
Kodi version: master (3133810855), reproduced on a downstream branch as described above.
Source: xbmc/xbmc