Skip to content

[WSLC] Generate WSLC SDK FFI bindings with bindgen - #669

Merged
Soham Das (SohamDas2021) merged 5 commits into
mainfrom
user/sodas/wslc-bindgen
Jul 27, 2026
Merged

[WSLC] Generate WSLC SDK FFI bindings with bindgen#669
Soham Das (SohamDas2021) merged 5 commits into
mainfrom
user/sodas/wslc-bindgen

Conversation

@SohamDas2021

@SohamDas2021 Soham Das (SohamDas2021) commented Jul 22, 2026

Copy link
Copy Markdown
Contributor

📖 Description

Replaces the hand-written WSLC SDK FFI bindings with bindgen-generated bindings so any ABI drift between the SDK headers and our Rust declarations becomes a compile error instead of silent undefined behavior at runtime.

🔗 References

🔍 Validation

✅ Checklist

📋 Issue Type

  • Bug fix
  • Feature
  • Task

GitHub Actions runs the PR validation build automatically. The ADO pipeline
(MXC-PR-Build) is the Azure version of the PR pipeline, kept in parity with the GitHub
Actions build; it runs on merge to main, and Microsoft reviewers with write access can trigger it
on a PR with /azp run. See docs/pull-requests.md.

If the dependency-feed-check check fails on a new dependency, the crate must be added to
the feed before the PR can pass. See docs/pull-requests.md
for the steps.

Microsoft Reviewers: Open in CodeFlow

Copilot AI review requested due to automatic review settings July 22, 2026 18:32
@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines:
There may be pipelines that require an authorized user to comment /azp run to run.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

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 migrates the WSLC backend’s Rust FFI surface from hand-written declarations to bindgen-generated bindings, so ABI drift between the pinned WSLC SDK header (wslcsdk.h) and our Rust types/signatures is caught at compile time rather than becoming silent runtime UB.

Changes:

  • Add and commit bindgen-generated WSLC SDK bindings (wslcsdk_sys.rs) plus a PowerShell regeneration script.
  • Refactor wslc_bindings.rs into a thin façade over the generated module (DLL anti-hijack load + required-symbol validation + RAII guards).
  • Update the WSLC runner + policy mapping and add documentation/runbook for maintaining the bindings.

Reviewed changes

Copilot reviewed 9 out of 9 changed files in this pull request and generated 1 comment.

Show a summary per file
File Description
src/backends/wslc/common/src/wslcsdk_sys.rs New bindgen-generated ABI surface + dynamic-loading WslcSdk table (committed artifact).
src/backends/wslc/common/src/wslc_bindings.rs Façade over generated bindings: safe-ish load(), required-export validation, RAII guards, S_OK, helpers.
src/backends/wslc/common/src/wsl_container_runner.rs Update call sites/struct field names/enum constants and callback ABI to match bindgen output.
src/backends/wslc/common/src/policy_mapping.rs Update networking mode mapping/tests to use bindgen newtype constants.
src/backends/wslc/common/src/lib.rs Export new wslcsdk_sys module.
scripts/generate-wslc-bindings.ps1 New standalone regeneration script (bindgen CLI + libclang + MSVC/WinSDK include discovery + header extraction from .nupkg).
docs/wsl/wslc-sdk-bindings.md New runbook documenting the generated file, why it’s committed, and how to bump/regenerate safely.
docs/wsl/wsl-container-getting-started.md Link to the new bindings-maintenance documentation.
.github/copilot-instructions.md Document the bindgen-generated WSLC bindings convention and regeneration procedure.

Comment on lines +352 to 356
let mut missing = WslcComponentFlags::WSLC_COMPONENT_FLAG_NONE;
let hr = sdk.WslcGetMissingComponents(&mut missing);
if hr != S_OK {
return Err(sdk_error("WslcGetMissingComponents failed", hr, ""));
}
@SohamDas2021
Soham Das (SohamDas2021) marked this pull request as ready for review July 22, 2026 18:55
@SohamDas2021
Soham Das (SohamDas2021) requested a review from a team as a code owner July 22, 2026 18:55
@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines:
There may be pipelines that require an authorized user to comment /azp run to run.

Comment thread .github/copilot-instructions.md Outdated
Comment thread docs/wsl/wslc-sdk-bindings.md Outdated
Comment thread docs/wsl/wslc-sdk-bindings.md
Comment thread docs/wsl/wslc-sdk-bindings.md
Comment thread docs/wsl/wslc-sdk-bindings.md Outdated
Comment thread scripts/generate-wslc-bindings.ps1
Comment thread scripts/generate-wslc-bindings.ps1
Comment thread scripts/generate-wslc-bindings.ps1
Comment thread scripts/generate-wslc-bindings.ps1 Outdated
Comment thread src/backends/wslc/common/src/wslc_bindings.rs
Copilot AI review requested due to automatic review settings July 27, 2026 19:01

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Pull request overview

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

Comment thread scripts/generate-wslc-bindings.ps1 Outdated
Comment on lines +98 to +99
$nupkg = Get-ChildItem $vendorDir -Filter "*.nupkg" | Sort-Object Name -Descending | Select-Object -First 1
if (-not $nupkg) { Fail "No vendored .nupkg found in '$vendorDir'." }
Comment thread scripts/generate-wslc-bindings.ps1 Outdated
if ($cmd) { $bindgen = $cmd.Source }
if (-not $bindgen) { $bindgen = Join-Path $env:USERPROFILE ".cargo\bin\bindgen.exe" }
if (-not (Test-Path $bindgen)) {
Fail "bindgen CLI not found. Install it with: cargo install bindgen-cli"
Copilot AI review requested due to automatic review settings July 27, 2026 19:15

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Pull request overview

Copilot reviewed 10 out of 10 changed files in this pull request and generated 1 comment.

Comment on lines +113 to +116
$verLine = Select-String -Path (Join-Path $wslcCommon "build.rs") -Pattern 'WSLC_SDK_VERSION\s*:\s*&str\s*=\s*"([^"]+)"' | Select-Object -First 1
if (-not $verLine) { Fail "Could not read WSLC_SDK_VERSION from build.rs." }
$wslcVersion = $verLine.Matches[0].Groups[1].Value
$nupkgPath = Join-Path $vendorDir "Microsoft.WSL.Containers.$wslcVersion.nupkg"

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Pull request overview

Copilot reviewed 10 out of 10 changed files in this pull request and generated no new comments.

/// on drop), so the pointer remains valid for the duration of all callbacks.
/// The SDK guarantees `data` is valid for `data_size` bytes during the callback.
unsafe extern "system" fn io_callback(
unsafe extern "C" fn io_callback(

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

ah it's a bindgen thing. If you don't believe in your testing it'll cause an issue then I'm all good. For what it's worth looks like we can override what bindgen outputs via the override_abi parameter to its builder. That said I think from reading through the docs it's only useful if we expect to run things on x86 systems.

@SohamDas2021
Soham Das (SohamDas2021) merged commit 61af417 into main Jul 27, 2026
24 checks passed
@SohamDas2021
Soham Das (SohamDas2021) deleted the user/sodas/wslc-bindgen branch July 27, 2026 23:26
Carlos Alexandro Becker (caarlos0) added a commit to caarlos0/mxc that referenced this pull request Jul 27, 2026
Integrates the bindgen-generated WSLC FFI bindings (microsoft#669) with this branch's
Rust SDK work. Both sides changed `wslc_bindings.rs` and
`wsl_container_runner.rs` heavily, but with complementary intent, so the
resolution keeps both:

- `wslc_bindings.rs` takes upstream's facade over the generated
  `wslcsdk_sys`, and re-applies this branch's additions on top: the
  `ComApartment` COM guard, `is_available()` behind `platform_support()`, and
  the removal of the debug `eprintln!`s from the RAII guards' `Drop`.
- `wsl_container_runner.rs` keeps this branch's restructured lifecycle
  (`start_container` / `StartedContainer` / `IoSink` / streaming) and ports
  every SDK call site to the generated API shape: `(sdk.Fn)(..)` becomes
  `sdk.Fn(..)`, the guards take `sdk.release_*_fn()`, enum variants and struct
  fields take bindgen's C-style names, and the callbacks become `extern "C"`.
- `lib.rs` keeps both module sets.

This branch's "never unload `wslcsdk.dll`" safety property survived the change
of ownership: bindgen's dynamic-loading `WslcSdk` owns its `Library`, so the
leak moves up into `WslcSdk::shared()`, a process-wide `OnceLock` instance
handed out as `&'static`. That also resolves the review comment noting the
previous claim was wrong — the module really is loaded and symbol-checked once
now — and removes `sdk` from `StartedContainer`'s drop-order reasoning
entirely, since it is a borrow rather than an owned handle.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Copilot-Session: 98f4724a-151d-4e38-9367-52048ffcdb5d
Signed-off-by: Carlos Alexandro Becker <caarlos0@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants