Verify mode

A handler has to do what its function does, and verify mode checks that it does, call by call, against the function's own recompiled code. The two can't both run on the game's live memory, since a side effect would happen twice, so a hook says what its function touches:

hook memcpy(void* dst out[size], const void* src in[size], u64 size) -> void* = hook_memmove pure

The hooks_verify setting ([Debug]) names the families whose pure hooks are checked. Their slots then hold verify mode's check (verify_fn in hook.c) in place of the adapter. For each call, hook_verify_call (src/nx/nn/verify.c) copies the buffers into scratch memory in the kernel region (buffers that overlap or touch share one copy, so a memmove's overlap stays one, and each copy keeps its buffer's address modulo 64, so the function takes the path it takes for that alignment), runs the function's own code with the buffer arguments pointing at the copies (and through the dispatch loop when the code leaves the function early), puts the registers back, runs the handler on the live memory and compares:

The log names each call that differs (the first 16 of each function), with its caller and arguments, and counts the calls that agree (the first, then at 1024 and each power of 4 after it). A call whose buffers don't fit in its scratch slot (4 MiB, 16 slots taken without waiting, since a guest thread can lose its core inside the function's code) runs the handler alone and is counted as skipped. The cumulative FP exception flags (FPSR) aren't compared: no handler sets them. Verify mode is slow (each call runs twice and copies its buffers), for checking a hook, never for playing.

The functions hooked are the running process's (g_host.hook): their own code and the adapters are addresses of this run, so a save state holds none of them, and a state loaded from another run finds this run's hooks in place. The scratch slots are guest memory, so a state holds them, with their base (g_hle.hook), only when the run that saved it verified: after the load of one that has none, each call is skipped and the log says why (tests/hook_install.c).

A function with other side effects (it waits, takes a lock, does I/O or reads the clock) can't run twice, so its hook isn't pure and verify mode skips it: such a hook is verified in tests, on inputs recorded from the game. tests/hook_verify.c checks verify mode itself with stand-ins for the function's own code.


Made from docs/platforms/nx/hooks.md at 69237645142c. GPL-3.0-or-later.