Bug: RPC memory reads can crash the emulator while the GPU renders
Is there an existing issue for this?
- I have searched the existing issues
Affected Build(s)
2126.0, 2126.1.1
Description of Issue
What happens
An RPC client reads game memory while the game renders into that memory, and Azahar exits. It writes no log line, and in the one crash checked, Windows recorded no crash either. It happens most when the GPU is busiest, such as at a battle's start in Pokémon games (tested with Omega Ruby), and every time when a client reads VRAM, where the framebuffers and render targets are: the attached proof of concept crashed it 10 times in 10, about 5 seconds after the game started.
Environment
- Windows 11 25H2, Vulkan renderer, asynchronous shader compilation and presentation on. The
machine has two GPUs, an Intel UHD Graphics and an NVIDIA Quadro T1000; the log does not say
which one Vulkan used (
physical_deviceis the default, 0). - The replays ran on my fork of Azahar 2126.0 (
d204921), whose changes are movie playback, save states, file-read timing and window flags, none of them in the files named below. Every line cited below is identical on master (662d412), where the numbers are master's. - The movie and save were recorded on Azahar 2126.1.1.
Why (every step checked in the source)
- The RPC server answers requests on a thread of its own (
src/core/rpc/rpc_server.cpp:16, astd::jthreadrunningHandleRequestsLoop). The code says so at line 28: "Memory read occurs asynchronously from the state of the emulator." HandleReadMemorycallsMemory().ReadBlock(...)(rpc_server.cpp:31and:35), which isReadBlockImpl<false>(src/core/memory.cpp:1088).- For a page of type
RasterizerCachedMemory,ReadBlockImplfirst callsRasterizerFlushVirtualRegion(..., FlushMode::Flush)(memory.cpp:213-217), unless itsUNSAFEtemplate parameter is true. Nothing instantiates it with true. - That reaches
RasterizerCache<T>::FlushRegion(src/video_core/rasterizer_cache/rasterizer_cache.h:1267) through the renderer'sFlushRegion. Neither renderer takes a lock on the way (vk_rasterizer.cpp:793,gl_rasterizer.cpp:831), and the cache has no lock of its own. FlushRegionwalksdirty_regions, the regions the GPU has drawn into and not yet written back, and callsDownloadSurfacefor each overlap (rasterizer_cache.h:1275-1305): a GPU readback, on the RPC thread.- Meanwhile the emulation thread runs the game's GPU commands, which fill and change that same
cache (
src/core/hle/service/gsp/gsp_gpu.cpp:682→GPU::SubmitCmdList,src/video_core/gpu.cpp:410).
So a read that overlaps a surface being rendered makes the RPC thread download it while the emulation thread is drawing into it.
A read of memory the GPU only reads from (an uploaded texture) finds no dirty region and downloads
nothing. That read still looks up dirty_regions on the wrong thread, which is a data race, but a
much smaller one.
How it was found and measured
I'm building a screen reader layer for Pokémon games on Azahar, so that visually impaired players can play them on the emulator. It reads memory over RPC for most things: reading out text, navigation aids and so on. From 2026-09-29 it also read the images of pictures on screen, to name icons that have no text (Pokémon types, status conditions). The crash turned up in regression testing, replaying a movie recorded with "Force deterministic async operations" on, from its first frame, with the layer reading as it does:
| The layer read | Replays | Crashed |
|---|---|---|
| no images | 20 | 0 |
| images of any size | 12 | 5 |
| images of 4 KB or less | 8 | 0 |
In each crash the last image read was 128 KB at 0x1F5A0000, in VRAM, at the battle's start. The
next read, which every surviving replay made 0.02 s later, never came.
The proof of concept
The attached poc/rpc_vram_reader.py is independent of the layer: a single Python file, standard
library only, that waits for a game to start, selects its process, and reads all of VRAM
(0x1F000000-0x1F600000) over the RPC in 1 KB reads, over and over. It never writes memory.
Replaying the attached movie from its first frame with the PoC running beside it:
| Build | Replays | Crashed |
|---|---|---|
| as it is | 10 | 10, each within 13 passes over VRAM, about 5 s after the game started |
| with fix A below | 10 | 0, each still answering after about 375 passes in 60 s |
The crashes came during the opening logos, so the movie is not needed to see it: any game should do. The movie is attached to make the run repeatable, as it also occasionally crashes ~70 seconds in when the ORAS capture tutorial begins.
I also reproduced it myself on Azahar 2126.1.1, with no movie, by running the PoC once a game was open. The README says how.
Attached (azahar-issue.zip): the PoC, the movie, the save it starts from, the logs of all 20
runs, and the patch I tested fix A with.
Potential fixes
A. RPC reads skip the flush
Add a public read that uses ReadBlockImpl<true>, for example
MemorySystem::UnsafeReadBlock(const Kernel::Process&, VAddr, void*, std::size_t) in
src/core/memory.h and src/core/memory.cpp, and call it from HandleReadMemory
(rpc_server.cpp:31 and :35). The patch I tested (fix-a-test.patch, 11 lines, changing only
line 35) is attached as evidence for the measurement above, not as a submission.
- For: a few lines, no locking, no change to how fast or when the RPC answers. The largest cause, a GPU readback on the wrong thread, is gone.
- Against: an RPC read of something the GPU has drawn returns the bytes emulated memory holds,
which may be older than what is on screen. A tool reading the framebuffer over RPC would see
that. It also leaves the smaller races: the page table read without synchronisation, and writes,
which still go through
WriteBlockImpl<false>and invalidate the cache from the RPC thread (memory.cpp:274). Writes could be given the same treatment, with the opposite risk: the GPU keeps a stale copy of what was written.
B. RPC requests run on the emulation thread
Queue requests as now, but answer them from System::RunLoop (src/core/core.cpp), as the GDB
stub does its packets (core.cpp:94-102, GDBStub::HandlePacket(*this)).
- For: it fixes the whole class: reads, writes, flushes and the page table all happen where the emulator expects them. The GDB stub already works this way.
- Against: a bigger change, and it moves when a request is answered. A reply waits for the next
RunLooppass. How often that runs, and whether it runs while emulation is paused, needs checking; if not, the RPC would go quiet while paused. Clients sending thousands of reads a second, as the accessibility layer does, would feel the latency unless requests are drained in batches.
C. A lock between RPC reads and the GPU's work
Take a mutex around HandleReadMemory's reads and around command-list processing or the
rasterizer cache's mutating calls.
- For: keeps the RPC asynchronous and fresh.
- Against: touches
video_core, which the RPC should not need to know about, puts a lock on the renderer's hot path, and needs care to avoid a deadlock with the Vulkan scheduler's worker thread (vk_scheduler.cpp:56). The most invasive of the three.
A is the smallest and safest to review. B is the one that closes the problem for good.
See also
- #2269 says the RPC server is barely documented, which may be part of why this hasn't been reported: I don't know how many people use it. The accessibility layer I'm building reads from the RPC server extensively, but never writes memory to the emulator.
Disclaimer
This issue was drafted with help from Claude Opus 5.5, which also wrote the PoC and the fix A test patch. I verified the crash myself by running the PoC.
Expected Behavior
Azahar does not crash when an RPC client reads framebuffer or render-target addresses
Reproduction Steps
See attached PoC and issue description
Log File
See attached zip file above
System Configuration
CPU: Intel(R) Core(TM) i7-10850H CPU @ 2.70GHz, 2712 Mhz, 6 Core(s), 12 Logical Processors GPU/Driver: Intel UHD Graphics and NVIDIA Quadro T1000, Vulkan renderer, asynchronous shader compilation and presentation on RAM: 32.0 GB OS: Windows 11 25H2
Source: azahar-emu/azahar