You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
GetKeyboardState (and other &mut [T] out-buffer wrappers) pass as_ptr() to a callee that writes, which miscompiles in release #5189
lpkeystate.as_ptr() goes through a shared reborrow, so the pointer has read-only provenance. GetKeyboardState writes through it, which is undefined behaviour.
What we see: in a release build (opt-level 3, x86_64-pc-windows-msvc, rustc 1.99.1), code like this always reads 0, even when GetKeyState(VK_CONTROL) reports the key as down at the same moment:
letmut state = [0u8;256];GetKeyboardState(&mut state)?;let ctrl = state[VK_CONTROL.0asusize]&0x80 != 0;
LLVM treats the zero-initialised array as unchanged by the call and folds the constant-index reads to 0. A loop over the whole array still sees the real bytes. Debug builds behave correctly.
Workaround: bind the function directly and pass as_mut_ptr():
ToUnicodeEx (pwszbuff), GetModuleFileNameW and DrawTextW look similar. We've only seen an actual miscompile with the fixed-size array, but all of these write through a pointer derived from a shared borrow. The bindings generator should use as_mut_ptr() for &mut parameters.
How we found it: in a TSF text service, a modifier-chord hotkey (Ctrl+Shift) never fired in release builds. Instrumented builds showed the array entries reading 0 while GetKeyState reported the key down. A unit test that calls SetKeyboardState and then the wrapper didn't reproduce it, because whether the reads get folded depends on inlining. I can put together a minimal repro crate if that would help.
Thanks for the detailed report. The required &mut [T] case is already fixed on master and will ship in the next release. While checking this I found that optional out-buffers (e.g. BCryptEncrypt's IV) still pass a shared pointer - I'll fix that too. In the meantime, binding the function directly as you did is the right workaround on 0.62.
Activity
kennykerr commentedon Oct 27, 2026
Thanks for the detailed report. The required
&mut [T]case is already fixed on master and will ship in the next release. While checking this I found that optional out-buffers (e.g.BCryptEncrypt's IV) still pass a shared pointer - I'll fix that too. In the meantime, binding the function directly as you did is the right workaround on 0.62.