A HID device path is handed to CreateFileW with its NUL (the emulator now and then died in wcslen) #207

Open
github-bot wants to merge 1 commit from github-bot/gh-207 into main AGit
Owner

@⁠SPFCChris on GitHub · 2026-11-25 22:50 UTC · original
SPFCChris:fix/dualsense-path-nul → main · +18 −2 · 1 files · 1 commits · open

Problem

The emulator now and then ends with an access violation in ntdll, on any machine without a DualSense connected:

VIctory: first-chance exception 0xc0000005 at ntdll.dll+0x12d844 (read 0x2a88effb000: no access)
  #0  ntdll.dll+0x12d844        wcslen
  #1  ntdll.dll+0xb5251         RtlInitUnicodeStringEx
  #2  KERNELBASE.dll+0x1e377
  #3  KERNELBASE.dll+0x1fc67    CreateFileW
  #4  victory.exe+0x3dbdae      the thread started with a plain fn: dualsense::reader
VIctory: unhandled exception 0xc0000005 ...

(The names are from the DLLs' exports and the build's .pdb; Windows 11 build 26300.)

dualsense::reader looks at every HID interface every two seconds while no pad is open. device_paths copies each device path out of the SetupAPI buffer without its terminating NUL:

paths.push(unsafe { std::slice::from_raw_parts(path, len) }.to_vec());

and open_pad gives CreateFileW the pointer, path.as_ptr(). So the path is read on into whatever follows the copy on the heap. Mostly a zero comes soon, and the call fails or opens the device all the same. When none comes before the end of the mapped memory, wcslen faults and the process is gone. In the cases logged here the scan ran some 28 KB past the start of the string.

Three of about sixty runs of four to six minutes ended this way on this machine (Ryzen AI Max+ 395, no pad), in different builds and settings, which is how it came to notice: I first took them for the title's own crashes.

It came in with 34a0823 ("Input: read DualSense pads over HID when XInput has none").

Change

The copy keeps the NUL (len + 1). One line, plus the print in the ignored listing test, which now leaves the NUL out of what it prints.

Test

modules::dualsense::tests::device_paths_keep_their_terminator: every path the machine has ends in a NUL and holds no other. It passes with no HID interface at all.

Tested

  • cargo test -p victory-hle --lib --locked dualsense: 7 passed, 0 failed, 1 ignored.
  • In the title: the seven runs of about five minutes made since with this change have not ended this way. That is weak evidence for a fault that showed in one run of twenty; the stack and the code are the case.

Not tested

  • With a DualSense connected (none here): the path it opens is the same string, now terminated.

AI-assisted (Claude Code); no game data.

🤖 Generated with Claude Code

**@⁠SPFCChris** on GitHub · 2026-11-25 22:50 UTC · [original](https://github.com/Belobug/VIctory-Belobug-AMD/pull/207) `SPFCChris:fix/dualsense-path-nul` → `main` · +18 −2 · 1 files · 1 commits · open ## Problem The emulator now and then ends with an access violation in `ntdll`, on any machine without a DualSense connected: ``` VIctory: first-chance exception 0xc0000005 at ntdll.dll+0x12d844 (read 0x2a88effb000: no access) #0 ntdll.dll+0x12d844 wcslen #1 ntdll.dll+0xb5251 RtlInitUnicodeStringEx #2 KERNELBASE.dll+0x1e377 #3 KERNELBASE.dll+0x1fc67 CreateFileW #4 victory.exe+0x3dbdae the thread started with a plain fn: dualsense::reader VIctory: unhandled exception 0xc0000005 ... ``` (The names are from the DLLs' exports and the build's `.pdb`; Windows 11 build 26300.) `dualsense::reader` looks at every HID interface every two seconds while no pad is open. `device_paths` copies each device path out of the SetupAPI buffer **without its terminating NUL**: ```rust paths.push(unsafe { std::slice::from_raw_parts(path, len) }.to_vec()); ``` and `open_pad` gives `CreateFileW` the pointer, `path.as_ptr()`. So the path is read on into whatever follows the copy on the heap. Mostly a zero comes soon, and the call fails or opens the device all the same. When none comes before the end of the mapped memory, `wcslen` faults and the process is gone. In the cases logged here the scan ran some 28 KB past the start of the string. Three of about sixty runs of four to six minutes ended this way on this machine (Ryzen AI Max+ 395, no pad), in different builds and settings, which is how it came to notice: I first took them for the title's own crashes. It came in with `34a0823` ("Input: read DualSense pads over HID when XInput has none"). ## Change The copy keeps the NUL (`len + 1`). One line, plus the print in the ignored listing test, which now leaves the NUL out of what it prints. ## Test `modules::dualsense::tests::device_paths_keep_their_terminator`: every path the machine has ends in a NUL and holds no other. It passes with no HID interface at all. ## Tested - `cargo test -p victory-hle --lib --locked dualsense`: 7 passed, 0 failed, 1 ignored. - In the title: the seven runs of about five minutes made since with this change have not ended this way. That is weak evidence for a fault that showed in one run of twenty; the stack and the code are the case. ## Not tested - With a DualSense connected (none here): the path it opens is the same string, now terminated. AI-assisted (Claude Code); no game data. 🤖 Generated with [Claude Code](https://claude.com/claude-code)
The DualSense reader looks at every HID interface every two seconds while
no pad is open. `device_paths` copied each path without its terminator and
`open_pad` gave `CreateFileW` the pointer, so the path read went on into
whatever followed the copy on the heap. Mostly a zero came soon and the
open failed or found the device all the same; when none came before the
end of the mapped memory the process ended:

  first-chance exception 0xc0000005 at ntdll.dll+0x12d844 (read ...: no access)
  ntdll!wcslen < ntdll!RtlInitUnicodeStringEx < KERNELBASE!CreateFileW
  < the reader's thread

Three of some sixty runs of four to six minutes on a machine with no pad
ended this way (Ryzen AI Max+ 395, Windows 11 26300). The copy keeps the
NUL; a test checks every path the machine has.

Co-Authored-By: Claude Fable 5.2 <[email protected]>
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 03:37:59 +00:00
Author
Owner

@⁠adrianmjim on GitHub · 2026-11-26 19:50 UTC · original

Tested natively on Windows 11 build 26300 with an RX 7900 XT (driver 32.0.32029.2010) and Rust 1.98.1. PR head is 25643c8 and the base is 4f62c2a. No DualSense is connected. The machine has 33 HID interfaces.

  • cargo test -p victory-hle --lib --locked dualsense on the PR: 7 passed, 1 ignored.
  • The new test without the fix (only the test added to 4f62c2a): device_paths_keep_their_terminator fails. The last element of a path is 125 (}), not 0. So the test catches the bug on any machine with at least one HID interface.
  • Deterministic reproduction (a local test, not proposed for the PR): each path exactly as device_paths() returns it is copied to the end of a page whose next page is PAGE_NOACCESS. It is then passed to CreateFileW with the same arguments as open_pad.
    • On 4f62c2a: the test process ends with 0xc0000005 (STATUS_ACCESS_VIOLATION) in 3 of 3 runs.
    • With this PR: it passes in 3 of 3 runs, and 20 of the 33 paths open.
  • The full victory-hle lib run, 3 times each on base and PR: no new failures. The only failures are the 5 known xmem tests (#2, #8, #107), the same on both.

Not tested: a real DualSense, and the emulator itself.

**@⁠adrianmjim** on GitHub · 2026-11-26 19:50 UTC · [original](https://github.com/Belobug/VIctory-Belobug-AMD/pull/207#issuecomment-7202009430) Tested natively on Windows 11 build 26300 with an RX 7900 XT (driver 32.0.32029.2010) and Rust 1.98.1. PR head is 25643c8 and the base is 4f62c2a. No DualSense is connected. The machine has 33 HID interfaces. - `cargo test -p victory-hle --lib --locked dualsense` on the PR: 7 passed, 1 ignored. - The new test without the fix (only the test added to 4f62c2a): `device_paths_keep_their_terminator` fails. The last element of a path is `125` (`}`), not `0`. So the test catches the bug on any machine with at least one HID interface. - Deterministic reproduction (a local test, not proposed for the PR): each path exactly as `device_paths()` returns it is copied to the end of a page whose next page is `PAGE_NOACCESS`. It is then passed to `CreateFileW` with the same arguments as `open_pad`. - On 4f62c2a: the test process ends with `0xc0000005` (STATUS_ACCESS_VIOLATION) in 3 of 3 runs. - With this PR: it passes in 3 of 3 runs, and 20 of the 33 paths open. - The full `victory-hle` lib run, 3 times each on base and PR: no new failures. The only failures are the 5 known `xmem` tests (#2, #8, #107), the same on both. Not tested: a real DualSense, and the emulator itself.
This pull request can be merged automatically.
You are not authorized to merge this pull request.
View command line instructions

Checkout

From your project repository, check out a new branch and test the changes.
git fetch -u origin '+refs/pull/207/head:github-bot/gh-207'
git switch 'github-bot/gh-207'

Merge

Merge the changes and update on Forgejo.
git switch 'main'
git merge --no-ff 'github-bot/gh-207'
git switch 'github-bot/gh-207'
git rebase 'main'
git switch 'main'
git merge --ff-only 'github-bot/gh-207'
git switch 'github-bot/gh-207'
git rebase 'main'
git switch 'main'
git merge --no-ff 'github-bot/gh-207'
git switch 'main'
git merge --squash 'github-bot/gh-207'
git switch 'main'
git merge --ff-only 'github-bot/gh-207'
git switch 'main'
git merge 'github-bot/gh-207'
git push origin 'main'
This discussion has been locked. Commenting is limited to contributors.
No description provided.