Skip to content

GetKeyboardState (and other &mut [T] out-buffer wrappers) pass as_ptr() to a callee that writes, which miscompiles in release #5189

Closed

Description

@ncchinh

Crate: windows 0.62.2 (the latest on crates.io as of 2026-10-21), feature Win32_UI_Input_KeyboardAndMouse.

The generated wrapper is:

pub unsafe fn GetKeyboardState(lpkeystate: &mut [u8; 256]) -> windows_core::Result<()> {
    windows_core::link!("user32.dll" "system" fn GetKeyboardState(lpkeystate : *mut u8) -> windows_core::BOOL);
    unsafe { GetKeyboardState(core::mem::transmute(lpkeystate.as_ptr())).ok() }
}

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:

let mut state = [0u8; 256];
GetKeyboardState(&mut state)?;
let ctrl = state[VK_CONTROL.0 as usize] & 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():

windows_core::link!("user32.dll" "system" fn GetKeyboardState(lpkeystate: *mut u8) -> windows_core::BOOL);
unsafe { GetKeyboardState(state.as_mut_ptr()) }.ok()

With that, the release build reads the modifier state correctly.

Scope: the same transmute(x.as_ptr()) pattern appears in the generated wrappers for &mut [T] out-buffers in general. For example, in 0.62.2:

pub unsafe fn GetWindowTextW(hwnd: HWND, lpstring: &mut [u16]) -> i32 {
    ...
    unsafe { GetWindowTextW(hwnd, core::mem::transmute(lpstring.as_ptr()), lpstring.len().try_into().unwrap()) }
}

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.

Activity

  1. added
    windows
    on Oct 22, 2026
  2. kennykerr commented on Oct 27, 2026

    @kennykerr
    Collaborator

    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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    windows

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions