[WSLC] Generate WSLC SDK FFI bindings with bindgen - #669
Conversation
|
Azure Pipelines: There may be pipelines that require an authorized user to comment /azp run to run. |
There was a problem hiding this comment.
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.rsinto 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. |
| 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, "")); | ||
| } |
|
Azure Pipelines: There may be pipelines that require an authorized user to comment /azp run to run. |
| $nupkg = Get-ChildItem $vendorDir -Filter "*.nupkg" | Sort-Object Name -Descending | Select-Object -First 1 | ||
| if (-not $nupkg) { Fail "No vendored .nupkg found in '$vendorDir'." } |
| 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" |
| $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" |
| /// 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( |
There was a problem hiding this comment.
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.
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>
📖 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
Cargo.lock, thedependency-feed-checkcheck passes (see docs/pull-requests.md)📋 Issue Type
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 GitHubActions build; it runs on merge to
main, and Microsoft reviewers with write access can trigger iton a PR with
/azp run. See docs/pull-requests.md.If the
dependency-feed-checkcheck fails on a new dependency, the crate must be added tothe feed before the PR can pass. See docs/pull-requests.md
for the steps.
Microsoft Reviewers: Open in CodeFlow