Skip to content

Migrate winapi usage to windows-sys/windows#3004

Merged
moor-coding merged 14 commits intomicrosoft:mainfrom
moor-coding:winapi-migration
Mar 17, 2026
Merged

Migrate winapi usage to windows-sys/windows#3004
moor-coding merged 14 commits intomicrosoft:mainfrom
moor-coding:winapi-migration

Conversation

@moor-coding
Copy link
Copy Markdown
Contributor

Replace remaining winapi imports and dependency blocks.
Update Windows feature sets and workspace dependencies.

@moor-coding moor-coding self-assigned this Mar 16, 2026
@github-actions github-actions Bot added the unsafe Related to unsafe code label Mar 16, 2026
@github-actions
Copy link
Copy Markdown

⚠️ Unsafe Code Detected

This PR modifies files containing unsafe Rust code. Extra scrutiny is required during review.

For more on why we check whole files, instead of just diffs, check out the Rustonomicon

…windows

- Replace remaining winapi imports and dependency blocks.
- Update Windows feature sets and workspace dependencies.
@moor-coding
Copy link
Copy Markdown
Contributor Author

@microsoft-github-policy-service agree company="Microsoft"

@moor-coding moor-coding marked this pull request as ready for review March 16, 2026 15:30
@moor-coding moor-coding requested review from a team as code owners March 16, 2026 15:30
Copilot AI review requested due to automatic review settings March 16, 2026 15:30
@moor-coding
Copy link
Copy Markdown
Contributor Author

Resolves #1061

@smalis-msft
Copy link
Copy Markdown
Contributor

I don't think this fully resolves #1061, as there are still uses of ntapi in the repo, and uses of winapi & com in the internal repo. But this is definitely a big step!

Comment thread support/pal/pal_async/src/windows/socket.rs Outdated
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR continues the repository-wide Windows API migration by replacing remaining direct winapi usages with windows-sys (and updating crate feature selections / workspace dependency blocks accordingly).

Changes:

  • Replace winapi imports with windows-sys equivalents across pal, pal_async, and vmswitch.
  • Update per-crate Windows feature sets in Cargo.toml files and remove winapi from workspace dependencies.
  • Adjust a few call sites/constants (e.g., STATUS_SUCCESS, OVERLAPPED) to match the new bindings.

Reviewed changes

Copilot reviewed 24 out of 25 changed files in this pull request and generated 5 comments.

Show a summary per file
File Description
vm/devices/virtio/virtiofs/src/section.rs Updates NT constant/type imports away from ntapi::winapi::* paths.
vm/devices/net/vmswitch/src/vmsif.rs Switches OVERLAPPED import to windows-sys.
vm/devices/net/vmswitch/src/dio.rs Migrates file I/O and status constants to windows-sys.
vm/devices/net/vmswitch/Cargo.toml Replaces winapi dependency block with windows-sys features.
support/pal/src/windows/tp.rs Migrates threadpool API bindings to windows-sys.
support/pal/src/windows/security.rs Migrates Windows security API imports to windows-sys.
support/pal/src/windows/process.rs Migrates process creation/console/handle imports to windows-sys.
support/pal/src/windows/pipe.rs Migrates pipe/IOCTL imports to windows-sys (but currently mixes NT types).
support/pal/src/windows/job.rs Migrates job object APIs to windows-sys.
support/pal/src/windows/fs.rs Migrates file query helpers to windows-sys (but currently mixes NT types).
support/pal/src/windows/alpc.rs Migrates STATUS_TIMEOUT / NTSTATUS/HANDLE imports to windows-sys.
support/pal/src/windows/affinity.rs Migrates GetCurrentProcessorNumber to windows-sys.
support/pal/src/windows/afd.rs Migrates AFD driver glue imports to windows-sys (but currently mixes NT types).
support/pal/src/windows.rs Replaces broad winapi module imports with windows-sys aliases (but currently mixes NT types).
support/pal/pal_async/src/windows/tp.rs Migrates threadpool types/constants to windows-sys.
support/pal/pal_async/src/windows/socket.rs Migrates NTSTATUS constants and introduces a local nt_success helper.
support/pal/pal_async/src/windows/pipe.rs Migrates pipe constants and I/O imports to windows-sys.
support/pal/pal_async/src/windows/overlapped.rs Migrates overlapped I/O imports to windows-sys.
support/pal/pal_async/src/windows/local.rs Migrates APC/wait/cancel APIs to windows-sys.
support/pal/pal_async/src/windows/iocp.rs Migrates IOCP flags to windows-sys.
support/pal/pal_async/src/executor_tests.rs Migrates Windows error/flag constants in tests to windows-sys.
support/pal/pal_async/Cargo.toml Removes winapi dependency and expands windows-sys feature set.
support/pal/Cargo.toml Removes winapi dependency and expands windows-sys feature set (incl. WDK features).
Cargo.toml Removes winapi from workspace dependency list.
Cargo.lock Reflects dependency graph changes after removing direct winapi deps.

Comment thread support/pal/src/windows/pipe.rs Outdated
Comment thread support/pal/src/windows/afd.rs Outdated
Comment thread support/pal/src/windows.rs Outdated
Comment thread support/pal/src/windows/fs.rs Outdated
Comment thread support/pal/src/windows/pipe.rs
- remove ntapi from workspace and crate manifests and regenerate lockfile

- migrate PAL Windows modules (core, AFD, ALPC, FS, pipe, process, security, threadpool) to windows-sys/windows types and APIs

- add local extern bindings for NT APIs not currently surfaced by windows-sys metadata (for ALPC, named pipe create, section query, wait completion packets)

- update pal_async and vmswitch Windows callsites/imports for windows-sys type and constant locations

- migrate virtiofs section handling and mesh ALPC access constants off ntapi

- replace ad-hoc NTSTATUS success check with windows-result in socket readiness path
Comment thread support/mesh/mesh_remote/Cargo.toml Outdated
Comment thread support/mesh/mesh_remote/src/alpc_node.rs Outdated
Comment thread support/pal/src/windows/afd.rs Outdated
Comment thread support/pal/src/windows/alpc.rs Outdated
Comment thread support/pal/src/windows/pipe.rs Outdated
Comment thread support/pal/src/windows/security.rs Outdated
Comment thread support/pal/src/windows.rs Outdated
Comment thread vm/devices/virtio/virtiofs/src/section.rs Outdated
Comment thread Cargo.toml
@smalis-msft
Copy link
Copy Markdown
Contributor

I'm surprised that so many things from ntapi aren't provided by windows/windows-sys. As previously discussed, let's just go back to using ntapi for them then.

- apply review follow-ups in PAL/mesh windows code:
  - remove remaining rename-style/type-alias patterns
  - replace hardcoded ALPC directory access literal with crate constants
- add required windows-sys feature/dependency wiring updates in pal and mesh_remote, with lockfile refresh
- align NT/Win32 callsites with current windows-sys signatures by removing unnecessary mutable references across afd, fs, pipe, process, and tp paths
- fix PAL windows fs test for current FILE_STAT_LX_INFORMATION FileId layout
- resolve style/lint issues in windows/alpc and windows.rs (naming and pointer constness cleanup)
- update virtiofs section code for lint compliance (constant naming and NtCreateSection argument passing)
- replace temporary local externs with existing ntapi/winapi calls
- re-add ntapi/winapi dependency wiring needed for these paths
- add TODOs to migrate back once windows/windows-sys expose the APIs
Copilot AI review requested due to automatic review settings March 16, 2026 19:55
@will-j-wright
Copy link
Copy Markdown
Contributor

I'm surprised that so many things from ntapi aren't provided by windows/windows-sys. As previously discussed, let's just go back to using ntapi for them then.

What kind of things are missing? I did this migration in a couple of other crates, and I found that the only things missing were ZSTs, like struct UINT8(u8) type things that don't really have any reason to exist.

@will-j-wright
Copy link
Copy Markdown
Contributor

Nevermind, I just took a look at the code. I think all the "missing" functions from windows_sys are undocumented. It might be worth asking Kenny why they don't exist in the crate

- Replace ref-to-raw-pointer cast in windows pipe code with std::ptr::from_mut to satisfy clippy::ref-as-ptr.\n- Replace expect-based section size conversion with checked conversion returning InvalidInput for oversized guest-provided values.
Copilot AI review requested due to automatic review settings March 16, 2026 22:08
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Migrates Windows platform bindings across multiple crates from winapi to windows-sys (and windows-result where needed), updating dependency feature sets accordingly. This reduces reliance on winapi while keeping ntapi as a fallback for NT APIs not yet exposed in the windows-sys/windows surface.

Changes:

  • Replace winapi imports with windows-sys in virtio-fs section handling, PAL Windows wrappers, vmswitch DirectIO NIC, and PAL async Windows backends.
  • Update crate Cargo.toml files to add windows-sys feature sets and remove winapi dependency blocks where possible.
  • Adjust a few call sites/types to match new bindings (e.g., status constants, handle casts, threadpool pointer types, executor tests).

Reviewed changes

Copilot reviewed 26 out of 27 changed files in this pull request and generated 3 comments.

Show a summary per file
File Description
vm/devices/virtio/virtiofs/src/section.rs Switch NtCreateSection/NtOpenSection to windows-sys, keep ntapi for NtQuerySection fallback.
vm/devices/virtio/virtiofs/Cargo.toml Add windows-sys dependency + required feature set on Windows.
vm/devices/net/vmswitch/src/vmsif.rs Replace OVERLAPPED import with windows-sys equivalent.
vm/devices/net/vmswitch/src/dio.rs Replace winapi I/O APIs/constants with windows-sys.
vm/devices/net/vmswitch/Cargo.toml Add windows-sys feature set and remove the winapi dependency block.
support/pal/src/windows/tp.rs Migrate threadpool wrappers to windows-sys types/APIs.
support/pal/src/windows/security.rs Migrate security APIs/types to windows-sys, update raw-dylib signature types.
support/pal/src/windows/process.rs Migrate process creation constants/APIs to windows-sys and replace NtCurrentProcess.
support/pal/src/windows/pipe.rs Migrate named pipe + FSCTL plumbing to windows-sys/WDK, keep ntapi fallback for NtCreateNamedPipeFile.
support/pal/src/windows/job.rs Migrate job object APIs/types to windows-sys.
support/pal/src/windows/fs.rs Migrate file/NT query + FindFirstFile usage to windows-sys and update test.
support/pal/src/windows/alpc.rs Adjust ALPC module linting and add casts to match remaining ntapi surfaces.
support/pal/src/windows/affinity.rs Replace GetCurrentProcessorNumber import with windows-sys.
support/pal/src/windows/afd.rs Migrate AFD open path to windows-sys WDK + IO functions.
support/pal/src/windows.rs Broad PAL Windows migration to windows-sys (handles, NT helpers, IOCP, overlapped).
support/pal/pal_async/src/windows/tp.rs Migrate threadpool callback signatures + constants to windows-sys.
support/pal/pal_async/src/windows/socket.rs Replace NT_SUCCESS macro usage with windows-result helper and migrate Win32 APIs.
support/pal/pal_async/src/windows/pipe.rs Migrate pipe async I/O constants/APIs to windows-sys.
support/pal/pal_async/src/windows/overlapped.rs Migrate overlapped file I/O imports to windows-sys.
support/pal/pal_async/src/windows/local.rs Migrate async local driver Win32 APIs/constants to windows-sys.
support/pal/pal_async/src/windows/iocp.rs Migrate IOCP backend constants to windows-sys.
support/pal/pal_async/src/executor_tests.rs Update Windows test constants/imports to windows-sys.
support/pal/pal_async/Cargo.toml Remove winapi dependency block; add windows-sys feature set + windows-result.
support/pal/Cargo.toml Expand windows-sys feature set for PAL Windows implementation.
support/mesh/mesh_remote/src/alpc_node.rs Replace ntapi directory access constant with windows-sys-composed equivalent.
support/mesh/mesh_remote/Cargo.toml Remove ntapi dependency; add windows-sys with required features.
Cargo.lock Update dependency graph: bump ntapi, add windows-sys/windows-result where newly used.

Comment thread vm/devices/net/vmswitch/src/dio.rs Outdated
Comment thread vm/devices/net/vmswitch/src/dio.rs Outdated
Comment thread support/pal/src/windows.rs
- Add a test-only CmdExe command wrapper and use it for env-wrapper tests on Windows.
- Use platform-specific helper commands for env lookup/failure checks.
- Keep existing ShCmd behavior on non-Windows platforms.
- Resolves aarch64-windows flowey_core shell test timeout failures in CI.
Comment thread support/pal/src/windows/alpc.rs Outdated
Comment thread support/pal/src/windows/alpc.rs Outdated
Comment thread support/pal/src/windows.rs Outdated
Comment thread support/pal/src/windows.rs Outdated
Copilot AI review requested due to automatic review settings March 17, 2026 15:31
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR continues the repository-wide migration away from winapi toward windows-sys / windows, updating Windows API imports and crate feature sets across device code and the PAL abstraction layers.

Changes:

  • Replace multiple winapi imports/usages with windows-sys equivalents across pal, pal_async, vmswitch, and virtiofs.
  • Update per-crate windows-sys feature lists (and add windows-result where needed) to match the new API surface.
  • Remove now-unneeded Windows-only dependency blocks (e.g., winapi / ntapi) from some crates, and adjust tests/utilities for Windows compatibility.

Reviewed changes

Copilot reviewed 27 out of 28 changed files in this pull request and generated 2 comments.

Show a summary per file
File Description
vm/devices/virtio/virtiofs/src/section.rs Switch section-object creation/open constants + syscalls to windows-sys (keep ntapi for NtQuerySection fallback).
vm/devices/virtio/virtiofs/Cargo.toml Add windows-sys WDK/Win32 feature set needed by section.rs.
vm/devices/net/vmswitch/src/vmsif.rs Replace winapi OVERLAPPED import with windows-sys equivalent.
vm/devices/net/vmswitch/src/dio.rs Replace winapi file/threading APIs with windows-sys equivalents for overlapped IO.
vm/devices/net/vmswitch/Cargo.toml Replace winapi dependency block with windows-sys feature set.
support/pal/src/windows/tp.rs Migrate threadpool wrappers from winapi pointer types to windows-sys PTP_* handles and functions.
support/pal/src/windows/security.rs Migrate security descriptor / SID helpers to windows-sys security APIs/constants and adjust a private dylib FFI signature.
support/pal/src/windows/process.rs Replace winapi process/console APIs with windows-sys and use GetCurrentProcess instead of NtCurrentProcess.
support/pal/src/windows/pipe.rs Migrate most named-pipe APIs/constants to windows-sys (keep ntapi for NtCreateNamedPipeFile fallback).
support/pal/src/windows/job.rs Replace job object APIs/structs/constants with windows-sys.
support/pal/src/windows/fs.rs Replace winapi findfile + NT query plumbing with windows-sys APIs and update the unit test accordingly.
support/pal/src/windows/alpc.rs Minor cleanup + more explicit casting for ALPC NT-object-attribute pointers while keeping ntapi fallback.
support/pal/src/windows/affinity.rs Replace GetCurrentProcessorNumber import with windows-sys.
support/pal/src/windows/afd.rs Migrate AFD open path + IO types/constants to windows-sys.
support/pal/src/windows.rs Large consolidation of Windows bindings to windows-sys + adjust IOCP helpers and NT heap helpers.
support/pal/pal_async/src/windows/tp.rs Migrate threadpool callback signatures/types/constants to windows-sys.
support/pal/pal_async/src/windows/socket.rs Replace NT_SUCCESS macro usage with windows-result success check and migrate to windows-sys NTSTATUS constants.
support/pal/pal_async/src/windows/pipe.rs Migrate pipe IO constants and APIs to windows-sys.
support/pal/pal_async/src/windows/overlapped.rs Migrate overlapped IO imports to windows-sys.
support/pal/pal_async/src/windows/local.rs Migrate local-driver Win32 APIs/constants to windows-sys.
support/pal/pal_async/src/windows/iocp.rs Migrate FILE_SKIP_* constants to windows-sys.
support/pal/pal_async/src/executor_tests.rs Update Windows overlapped-file test constants to windows-sys.
support/pal/pal_async/Cargo.toml Remove winapi dependency block; add windows-result + expand windows-sys features.
support/pal/Cargo.toml Expand windows-sys feature set to cover migrated API usage.
support/mesh/mesh_remote/src/alpc_node.rs Remove ntapi directory access constant usage by defining access mask from windows-sys constants.
support/mesh/mesh_remote/Cargo.toml Remove ntapi dependency and add windows-sys features needed by alpc_node.
flowey/flowey_core/src/shell.rs Add a Windows-only test wrapper option (cmd /C) and update env/exit-status wrapper tests to work on Windows.
Cargo.lock Dependency graph updates (e.g., ntapi bump, windows-result addition, new windows-sys edges).

Comment thread vm/devices/net/vmswitch/src/dio.rs Outdated
Comment thread vm/devices/net/vmswitch/src/dio.rs Outdated
smalis-msft
smalis-msft previously approved these changes Mar 17, 2026
Copilot AI review requested due to automatic review settings March 17, 2026 17:51
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Migrates remaining Windows API bindings from winapi to windows-sys (plus a few ntapi fallbacks where windows-sys is missing coverage), and updates Windows-specific feature sets/dependencies across the workspace.

Changes:

  • Replace winapi imports/usages with windows-sys across PAL, virtiofs, vmswitch, and pal_async Windows codepaths.
  • Add/adjust windows-sys feature flags in affected crates’ Cargo.toml, and remove winapi dependency blocks where no longer needed.
  • Extend Flowey test command wrapping to support Windows (cmd /C) while keeping env-related wrapper tests cross-platform.

Reviewed changes

Copilot reviewed 27 out of 28 changed files in this pull request and generated 6 comments.

Show a summary per file
File Description
vm/devices/virtio/virtiofs/src/section.rs Switch section APIs/constants to windows-sys (with ntapi fallback for NtQuerySection).
vm/devices/virtio/virtiofs/Cargo.toml Add windows-sys WDK/Win32 features for section-related APIs.
vm/devices/net/vmswitch/src/vmsif.rs Replace winapi OVERLAPPED with windows-sys equivalent.
vm/devices/net/vmswitch/src/dio.rs Migrate file IO + status constants from winapi to windows-sys.
vm/devices/net/vmswitch/Cargo.toml Replace winapi dependency block with windows-sys features.
support/pal/src/windows/tp.rs Replace threadpool types/APIs with windows-sys and adjust handle wrappers.
support/pal/src/windows/security.rs Migrate SDDL/security APIs/constants to windows-sys and update FFI signatures.
support/pal/src/windows/process.rs Migrate process/console APIs to windows-sys and update current-process retrieval.
support/pal/src/windows/pipe.rs Move named pipe + NT file APIs/constants to windows-sys, keep ntapi for NtCreateNamedPipeFile.
support/pal/src/windows/job.rs Replace job object APIs/structs/constants with windows-sys.
support/pal/src/windows/fs.rs Move NT/FindFirstFile helpers to windows-sys and adjust test assertion for new struct layout.
support/pal/src/windows/alpc.rs Minor adjustments/casts around NT ALPC calls; still uses some winapi types/constants.
support/pal/src/windows/affinity.rs Replace processor-number call with windows-sys.
support/pal/src/windows/afd.rs Migrate AFD open + IOCTL support types/APIs to windows-sys.
support/pal/src/windows.rs Large-scale migration of core PAL Windows bindings to windows-sys + some WDK imports; adjust helper signatures and casts.
support/pal/pal_async/src/windows/tp.rs Replace threadpool callback types/flags with windows-sys.
support/pal/pal_async/src/windows/socket.rs Replace NTSTATUS helpers and add windows-result for status->HRESULT checks.
support/pal/pal_async/src/windows/pipe.rs Migrate pipe IO constants/APIs to windows-sys.
support/pal/pal_async/src/windows/overlapped.rs Migrate overlapped IO APIs/types to windows-sys.
support/pal/pal_async/src/windows/local.rs Migrate APC/IO cancellation/wait APIs/constants to windows-sys.
support/pal/pal_async/src/windows/iocp.rs Migrate WindowsProgramming flags to windows-sys.
support/pal/pal_async/src/executor_tests.rs Update Windows overlapped-file test constants/errors to windows-sys.
support/pal/pal_async/Cargo.toml Remove winapi, add windows-sys feature set plus windows-result.
support/pal/Cargo.toml Expand windows-sys feature set to cover newly used Win32/WDK APIs.
support/mesh/mesh_remote/src/alpc_node.rs Replace DIRECTORY_ALL_ACCESS from ntapi with locally composed mask using windows-sys constants.
support/mesh/mesh_remote/Cargo.toml Replace Windows ntapi dependency with windows-sys features.
flowey/flowey_core/src/shell.rs Add Windows cmd /C wrapper path for tests and make wrapper tests cross-platform.
Comments suppressed due to low confidence (1)

support/pal/src/windows/alpc.rs:1

  • The PR title/description suggests migrating remaining winapi usage to windows-sys, but this module still imports winapi (STATUS_TIMEOUT, HANDLE, NTSTATUS). If the intent is full migration, consider switching these to windows_sys::Win32::Foundation::{STATUS_TIMEOUT, HANDLE, NTSTATUS} (or documenting why winapi remains required here).
// Copyright (c) Microsoft Corporation.

) -> BOOL;
caps: *mut SECURITY_CAPABILITIES,
new_token: *mut HANDLE,
) -> windows_sys::core::BOOL;
Comment on lines +335 to +338
let cmd_body = cmd_str
.strip_prefix("cmd /C ")
.unwrap_or(&cmd_str)
.trim_matches('"');
Comment on lines +377 to 385
pub unsafe fn set_file_completion_notification_modes(handle: RawHandle, flags: u32) -> Result<()> {
let flags = u8::try_from(flags).map_err(|_| {
Error::new(
io::ErrorKind::InvalidInput,
"file completion flags out of range",
)
})?;
// SAFETY: caller guarantees contract.
if unsafe { SetFileCompletionNotificationModes(handle, flags) } == 0 {
Comment on lines +33 to +37
"Wdk_Storage_FileSystem",
"Wdk_System_Memory",
"Wdk_System_SystemServices",
"Win32_Foundation",
"Win32_System_Memory",
Comment on lines +32 to +34
"Wdk_System_SystemServices",
"Win32_Storage_FileSystem",
]}
Comment on lines 167 to 180
// SAFETY: calling the API according to the NT API
unsafe {
*large_int_size.QuadPart_mut() = size.try_into().expect("size fits in an i64");
let mut handle = null_mut();
chk_status(NtCreateSection(
&mut handle,
access,
obj_attr.as_ptr(),
&mut large_int_size,
&maximum_size,
PAGE_READWRITE,
SEC_COMMIT,
null_mut(),
))?;
Ok(Self(OwnedHandle::from_raw_handle(handle)))
Ok(Self(OwnedHandle::from_raw_handle(handle.cast())))
}
@github-actions
Copy link
Copy Markdown

@moor-coding moor-coding merged commit 3bbdedc into microsoft:main Mar 17, 2026
84 of 86 checks passed
@moor-coding moor-coding deleted the winapi-migration branch March 17, 2026 20:01
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

unsafe Related to unsafe code

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants