diff --git a/.agents/skills/build-openshell-mxc-windows/SKILL.md b/.agents/skills/build-openshell-mxc-windows/SKILL.md index ee688314bd..d63cc8eb6b 100644 --- a/.agents/skills/build-openshell-mxc-windows/SKILL.md +++ b/.agents/skills/build-openshell-mxc-windows/SKILL.md @@ -103,7 +103,9 @@ The Windows build lane is implemented by these tracked files: | `tasks/windows.toml` | Mise task entry points for `windows:*` commands. | | `tasks/rust.toml`, `tasks/test.toml`, and `tasks/markdown.toml` | Windows routing for compiler-bearing checks, explicit Unix-only test skips, and Markdown dependency setup. | | `tasks/scripts/windows-msvc.ps1` | PowerShell wrapper that enters the Visual Studio developer environment and invokes Cargo. | -| `.github/workflows/windows-msvc.yml` | Opt-in PR lint and test plus advisory `windows` branch cache seeding and dependent binary builds on native x64 and ARM64 runners. | +| `.github/workflows/windows-msvc.yml` | Opt-in PR lint, test, and release builds plus advisory `windows` branch cache seeding and dependent binary builds on native x64 and ARM64 runners. | +| `tasks/scripts/windows-runtime.ps1` | Architecture and normal/delay PE import checks for static CRT/Z3 packages. | +| `.cargo/windows-msvc.toml` | Windows wrapper-only dependency profiles that keep native Z3 optimized in dev/test builds, matching the previous prebuilt solver. | | `.agents/skills/build-openshell-mxc-windows/` | This skill and companion reference material. | Use the code that is already in the repo. Do not generate a parallel Windows @@ -184,7 +186,7 @@ The lane targets a Windows host with Visual Studio Build Tools and rustup. | Visual C++ ARM64 tools | `vswhere -latest -products * -requires Microsoft.VisualStudio.Component.VC.Tools.ARM64 -property installationPath` | Required for native ARM64 check, build, and tests and for x64-to-ARM64 check/build. Tests always require a native runner. | | Visual C++ ARM64 Spectre-mitigated libraries | `vswhere -latest -products * -requires Microsoft.VisualStudio.Component.VC.Runtimes.ARM64.Spectre -property installationPath` | Required by `regorus` through `msvc_spectre_libs`; the build fails when the selected MSVC toolset lacks `lib\spectre\arm64`. | | Visual C++ Clang tools | `vswhere -latest -products * -requires Microsoft.VisualStudio.Component.VC.Llvm.Clang -property installationPath` | Provides host-native `libclang.dll` for `bindgen` and `clang-cl.exe` for ARM64 crypto dependencies such as `aws-lc-sys`. On ARM64, the wrapper uses `VC\Tools\Llvm\Arm64\bin`. | -| Visual C++ CMake tools | `vswhere -latest -products * -requires Microsoft.VisualStudio.Component.VC.CMake.Project -property installationPath` | Provides CMake and Ninja for native dependencies. The x64-to-ARM64 path adds Ninja to `PATH`; Z3 uses an architecture-specific prebuilt release. | +| Visual C++ CMake tools | `vswhere -latest -products * -requires Microsoft.VisualStudio.Component.VC.CMake.Project -property installationPath` | Provides CMake and Ninja for native dependencies, including static Z3. The wrapper adds Ninja to `PATH`. | | Windows SDK | `where.exe rc.exe` from a Developer PowerShell | Install an SDK containing target libraries and ARM64 tools. | | Rust via rustup | `rustc --version` | Add each target being validated: `x86_64-pc-windows-msvc` and/or `aarch64-pc-windows-msvc`. The wrapper also adds the selected target. | | mise | `mise --version` | Used as a task runner only. | @@ -203,10 +205,10 @@ from this skill. | `OPENSHELL_MXC_SKIP_ARM64` | `0` | Set to `1` to skip ARM64 when using `all` tasks. | | `OPENSHELL_WINDOWS_BUILD_JOBS` | `CARGO_BUILD_JOBS`, then `4` | Positive Cargo job limit used by the wrapper. | | `CARGO_TARGET_DIR` | `target` under repo root | Override Cargo output location. Use a short absolute path when x64-to-ARM64 builds approach Windows path-length limits. | -| `Z3_LIBRARY_PATH_OVERRIDE` | unset | Directory containing target-compatible system `libz3.lib` and `libz3.dll`, used instead of the prebuilt download. | +| `Z3_LIBRARY_PATH_OVERRIDE` | unset | Directory containing a target-compatible static `libz3.lib` built with `/MT`. Directories containing `libz3.dll` fall back to the pinned source build; release dependency checks reject import libraries. | | `Z3_SYS_Z3_HEADER` | unset | Full `z3.h` path required with a system Z3 library. | -| `Z3_SYS_Z3_VERSION` | `5.1.0` | Pinned official prebuilt Z3 release selected by the wrapper. | -| `READ_ONLY_GITHUB_TOKEN` | unset | Optional token for the Z3 release lookup; GitHub Actions supplies `github.token`. | +| `Z3_SRC_SOURCE_DIR` | downloaded source cache | Optional local Z3 5.1.0 source tree; the default archive is checksum-pinned. | +| `Z3_SYS_Z3_VERSION` | `5.1.0` | Pinned source version selected by the wrapper. | | `RUSTC_WRAPPER` | inherited | The wrapper resolves an available command to an absolute path. If it is unavailable, the wrapper warns and continues without compiler caching. | Legacy fork variables such as `OPENSHELL_UPSTREAM`, @@ -291,7 +293,8 @@ The GitHub Actions jobs layer architecture-specific `Swatinem/rust-cache` entries for Cargo registry and dependency target artifacts with sccache's GHA backend for cacheable Rust compiler outputs. Failed runs also save their usable dependency artifacts. Pull-request mirrors labeled `test:windows` run Clippy for the -Windows-supported workspace and e2e crates plus Rust tests. Pushes to `windows` and +Windows-supported workspace and e2e crates, Rust tests, packaging helper regressions, +and release builds with static-runtime import checks. Pushes to `windows` and manual dispatches on that branch run the same lint and test commands in a cache-seed job, followed by a dependent release-binary build job. The seed and PR jobs use the same cache namespaces, but pull-request mirrors cannot restore the @@ -304,7 +307,7 @@ jobs in the current mirror push run, or push a new mirrored commit. The binaries The ARM64 check/build steps in this x64-host contract are cross-builds. The wrapper discovers and adds host-native LLVM and Ninja to `PATH`, requires the ARM64 compiler and Spectre-mitigated libraries, lets ARM64 crypto crates select -`clang-cl`, and downloads the official prebuilt ARM64 Z3 package. +`clang-cl`, and builds the pinned Z3 source for ARM64 with static CRT linkage. On ARM64 hosts, validate the native ARM64 check, build, and test path. The wrapper rejects test targets that do not match the host architecture, so x64 @@ -390,7 +393,7 @@ MXC on Windows. Each other `compute-driver-*` feature installs its own Windows rejection stub without linking that driver crate. The default `in-tree-compute-drivers` alias enables all five features. An MXC-only build uses `--no-default-features --features compute-driver-mxc` (add `telemetry` -and `openshell-server/prebuilt-z3` as needed). +and `openshell-server/vendored-z3` as needed). | Driver | Windows build behavior | Runtime behavior | |---|---|---| @@ -423,14 +426,14 @@ architecture through Windows system configuration (not an emulated SSH shell), caches uploads by SHA256, and runs the unchanged real E2E harness. Results and host probes return to `target/windows-remote-results`, including nonzero test outcomes. The remote host needs SSH, OpenSSL, and MXC, not a compiler toolchain. -Package the matching `libz3.dll` from the target-specific Cargo cache alongside -the gateway: Windows prebuilt Z3 uses an import library, not static linkage. -The `windows:build:*` tasks now stage the pinned DLL, check PE architecture, -reject conflicting cache hashes, and verify the copied hash. Local MXC E2E -requires the adjacent DLL and reports a preflight error if it is missing. -Use `-Z3DllPath` when discovery is ambiguous. The native Visual C++ runtime -must be present on the remote host. Verify DLL architecture and the gateway -loader before E2E; do not count missing-DLL crashes as MXC enforcement failures. +The four OpenShell executables use static CRT linkage; the gateway embeds Z3. +The `windows:build:*` tasks verify architecture and reject normal or delayed +imports of Z3 and redistributable CRT DLLs. Do not package `libz3.dll` or require +the Visual C++ redistributable for these binaries. The separate MXC executor +retains its own prerequisites. The remote runner checks imports and gateway +startup before E2E; do not count loader crashes as MXC enforcement failures. +The deprecated `-Z3DllPath` argument remains accepted and ignored for existing +automation; do not restore DLL packaging to implement it. Verify the SSH host key beforehand. Never send the private SSH key or bundle the harness signing-key directories. `-SkipBuild` is an explicit stale-artifact opt-in, not evidence that artifacts match current sources. See the MXC driver @@ -515,11 +518,21 @@ Useful log files: | `test-x86_64-pc-windows-msvc-mxc-real.log` | Native x64 real-MXC integration output. | | `test-aarch64-pc-windows-msvc-mxc-real.log` | Native ARM64 real-MXC integration output. | -The first check downloads the pinned official Z3 archive for the target -architecture through `z3-sys`. GitHub Actions authenticates the lookup with its -read-only workflow token; local users can set `READ_ONLY_GITHUB_TOKEN` if an -unauthenticated lookup is rate-limited. Cargo stores the extracted library in -its target tree, so the Windows target cache reuses it. The artifact report +The first check downloads a checksum-pinned Z3 5.1.0 source archive unless +`Z3_SRC_SOURCE_DIR` supplies that version locally. Keep downloaded source under +`.cache/windows-z3-source`, outside Cargo target-cache pruning. The existing `vendored-z3` +feature builds a static library and selects the static MSVC runtime from the +Windows targets' `crt-static` setting. Keep this source pin separate from the +z3-src crate's bundled solver version. Cargo caches compiled Z3 per target; +cold builds require CMake and Ninja and take longer than prebuilt-library downloads. +The wrapper uses Ninja explicitly: implicit Visual Studio generator selection +in cmake-rs clears MSVC's Release optimization flags. Verify `/O2` in the +generated build rules when investigating solver performance. +The wrapper explicitly loads `.cargo/windows-msvc.toml` for workspace commands +so native Z3 uses Release optimization in dev/test builds. Do not loosen solver +budgets to compensate for a Debug native solver. Other Rust debug profiles and +non-Windows builds remain unchanged. +The artifact report computes SHA256 through .NET directly and does not rely on the `Get-FileHash` module being available inside the mise-launched Windows PowerShell process. diff --git a/.agents/skills/build-openshell-mxc-windows/reference.md b/.agents/skills/build-openshell-mxc-windows/reference.md index f157d87951..d3eb622526 100644 --- a/.agents/skills/build-openshell-mxc-windows/reference.md +++ b/.agents/skills/build-openshell-mxc-windows/reference.md @@ -12,7 +12,7 @@ when reading this reference on `main`. |---|---| | `tasks/windows.toml` | Mise task definitions for `windows:*`. | | `tasks/scripts/windows-msvc.ps1` | Visual Studio environment discovery, rustup target setup, Cargo invocation, logs, artifact report. | -| `.github/workflows/windows-msvc.yml` | Opt-in PR lint and test plus `windows` branch cache seeding and dependent binary builds on native x64 and ARM64 runners. | +| `.github/workflows/windows-msvc.yml` | Opt-in PR lint, test, and release builds plus `windows` branch cache seeding and dependent binary builds on native x64 and ARM64 runners. | ## Commands @@ -93,9 +93,10 @@ For ARM64, verify the Visual Studio instance contains the ARM64 MSVC tools, ARM64 Spectre-mitigated libraries, Clang tools, CMake tools, and a Windows SDK. Clang supplies host-native `libclang.dll` for `bindgen` and `clang-cl.exe` for ARM64 crypto dependencies such as `aws-lc-sys`. Native and -x64-to-ARM64 builds use the official prebuilt Z3 5.1.0 import library and -`libz3.dll` for the target architecture. Release build tasks stage that DLL -beside the four executables; copy it with them to the runtime host. +x64-to-ARM64 builds compile checksum-pinned Z3 5.1.0 source with the static CRT. +The four executables use static CRT linkage and the gateway embeds Z3. Release +builds verify architecture and reject normal/delayed CRT or Z3 DLL imports. +Do not package `libz3.dll`; the executables do not require the VC++ redistributable. An x64-to-ARM64 check/build discovers and adds host-native Ninja to `PATH`, while the crypto crates select `clang-cl`. Use a short `CARGO_TARGET_DIR` if Windows path-length limits are reached. diff --git a/.cargo/config.toml b/.cargo/config.toml index 0005fc2bd0..2c1c0c4171 100644 --- a/.cargo/config.toml +++ b/.cargo/config.toml @@ -6,3 +6,11 @@ # the header lives in /usr/include/z3/ rather than /usr/include/. The extra -I # is harmless on systems where the path doesn't exist. BINDGEN_EXTRA_CLANG_ARGS = "-I/usr/include/z3" + +# Windows packages carry their C/C++ runtime rather than requiring the Visual +# C++ redistributable. Native build scripts (cc/cmake) also consume crt-static. +[target.x86_64-pc-windows-msvc] +rustflags = ["-C", "target-feature=+crt-static"] + +[target.aarch64-pc-windows-msvc] +rustflags = ["-C", "target-feature=+crt-static"] diff --git a/.cargo/windows-msvc.toml b/.cargo/windows-msvc.toml new file mode 100644 index 0000000000..7b1c05f2fe --- /dev/null +++ b/.cargo/windows-msvc.toml @@ -0,0 +1,13 @@ +# SPDX-FileCopyrightText: Copyright (c) 2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved. +# SPDX-License-Identifier: Apache-2.0 + +# Loaded only by the Windows MSVC wrapper. Keep native Z3 optimized, as the +# previous prebuilt DLL was, without changing Rust application debug profiles. +# z3-sys forwards its OPT_LEVEL/DEBUG settings to the native CMake build. +[profile.dev.package.z3-sys] +opt-level = 3 +debug = false + +[profile.test.package.z3-sys] +opt-level = 3 +debug = false diff --git a/.github/workflows/windows-msvc.yml b/.github/workflows/windows-msvc.yml index a268629a22..c8cbc49cff 100644 --- a/.github/workflows/windows-msvc.yml +++ b/.github/workflows/windows-msvc.yml @@ -58,7 +58,7 @@ jobs: env: READ_ONLY_GITHUB_TOKEN: ${{ github.token }} SCCACHE_GHA_ENABLED: "true" - SCCACHE_GHA_VERSION: windows-msvc-${{ matrix.arch }} + SCCACHE_GHA_VERSION: windows-msvc-static-v1-${{ matrix.arch }} SCCACHE_CLIENT_SIDE: "1" SCCACHE_BASEDIRS: ${{ github.workspace }} steps: @@ -90,14 +90,22 @@ jobs: # wrapper; build steps still inherit sccache from the job. RUSTC_WRAPPER: "" with: - shared-key: windows-msvc-${{ matrix.arch }} + shared-key: windows-msvc-static-v3-${{ matrix.arch }} cache-targets: "true" cache-on-failure: "true" cache-bin: "false" + - name: Check Windows packaging helpers + run: | + powershell -NoProfile -ExecutionPolicy Bypass -File tasks/scripts/windows-runtime-tests.ps1 + if ($LASTEXITCODE -ne 0) { throw "Static runtime regressions failed." } + powershell -NoProfile -ExecutionPolicy Bypass -File tasks/scripts/test-mxc-remote-tests.ps1 + if ($LASTEXITCODE -ne 0) { throw "Remote helper regressions failed." } - name: Lint Windows-supported crates run: mise run --skip-tools windows:lint:${{ matrix.arch }} - name: Test run: mise run --skip-tools windows:test:${{ matrix.arch }} + - name: Build and verify release binaries + run: mise run --skip-tools windows:build:${{ matrix.arch }} - name: sccache stats if: always() run: sccache --show-stats @@ -124,7 +132,7 @@ jobs: env: READ_ONLY_GITHUB_TOKEN: ${{ github.token }} SCCACHE_GHA_ENABLED: "true" - SCCACHE_GHA_VERSION: windows-msvc-${{ matrix.arch }} + SCCACHE_GHA_VERSION: windows-msvc-static-v1-${{ matrix.arch }} SCCACHE_CLIENT_SIDE: "1" SCCACHE_BASEDIRS: ${{ github.workspace }} steps: @@ -150,10 +158,16 @@ jobs: env: RUSTC_WRAPPER: "" with: - shared-key: windows-msvc-${{ matrix.arch }} + shared-key: windows-msvc-static-v3-${{ matrix.arch }} cache-targets: "true" cache-on-failure: "true" cache-bin: "false" + - name: Check Windows packaging helpers + run: | + powershell -NoProfile -ExecutionPolicy Bypass -File tasks/scripts/windows-runtime-tests.ps1 + if ($LASTEXITCODE -ne 0) { throw "Static runtime regressions failed." } + powershell -NoProfile -ExecutionPolicy Bypass -File tasks/scripts/test-mxc-remote-tests.ps1 + if ($LASTEXITCODE -ne 0) { throw "Remote helper regressions failed." } - name: Lint Windows-supported crates # Keep the seed workload identical to the pull-request workload so the # windows-branch cache reflects the same lint and test commands. @@ -182,7 +196,7 @@ jobs: env: READ_ONLY_GITHUB_TOKEN: ${{ github.token }} SCCACHE_GHA_ENABLED: "true" - SCCACHE_GHA_VERSION: windows-msvc-${{ matrix.arch }} + SCCACHE_GHA_VERSION: windows-msvc-static-v1-${{ matrix.arch }} SCCACHE_CLIENT_SIDE: "1" SCCACHE_BASEDIRS: ${{ github.workspace }} steps: @@ -205,7 +219,7 @@ jobs: env: RUSTC_WRAPPER: "" with: - shared-key: windows-msvc-${{ matrix.arch }} + shared-key: windows-msvc-static-v3-${{ matrix.arch }} cache-targets: "true" cache-on-failure: "true" cache-bin: "false" diff --git a/CI.md b/CI.md index 8f039d34f9..df9f03e57d 100644 --- a/CI.md +++ b/CI.md @@ -78,6 +78,8 @@ The [Windows MSVC workflow on `windows`](https://github.com/NVIDIA/OpenShell/blo runs native x64 and ARM64 lanes. Windows PR checks are opt-in: add `test:windows`, then select **Re-run all jobs** on the current Windows MSVC run. Subsequent mirrored commits run them automatically. +These lanes run packaging helper regressions, Clippy, native tests, and release +builds that reject Z3 or redistributable CRT DLL imports on both architectures. Windows checks are not required for merging and do not run in merge queues. Pushes to `windows` and manual runs on that branch seed the lint/test caches and run dependent release binary builds. Those jobs use `continue-on-error: true`, so inspect diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index 15402ccffd..19b0231769 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -206,26 +206,33 @@ cargo build -p openshell-prover-cli --features vendored-z3 Local gateway image and E2E builds enable `vendored-z3` so their copied gateway binaries do not need a shared Z3 library in the runtime image. -For x86-64 and ARM64 Windows MSVC builds from the `windows` branch, use one of these Z3 paths: - -- Prebuilt Z3 (the default for `windows:*` tasks): `z3-sys` downloads the - pinned Z3 5.1.0 GitHub release for the target architecture on the first - build. Cargo reuses the extracted archive from its target directory. Windows - CI authenticates the GitHub API request with `READ_ONLY_GITHUB_TOKEN` and - preserves the archive in the architecture-specific Cargo target cache. For - cold local builds, you may set `READ_ONLY_GITHUB_TOKEN` to avoid anonymous - GitHub API rate limits. -- System Z3: point `Z3_LIBRARY_PATH_OVERRIDE` at the directory containing the - target-compatible MSVC Z3 library and `Z3_SYS_Z3_HEADER` at the full path to `z3.h`. - The `windows:*` tasks use this path automatically when `Z3_LIBRARY_PATH_OVERRIDE` - is set. - -`openshell-prover` itself has no `bindgen`/`libclang` dependency, so building -just this crate does not require `LIBCLANG_PATH`: - -```powershell -cargo build -p openshell-prover --target x86_64-pc-windows-msvc --features prebuilt-z3 -``` +For x86-64 and ARM64 Windows MSVC builds from the `windows` branch, the +`windows:*` tasks compile checksum-pinned Z3 5.1.0 source into a static library. +The Windows targets enable `crt-static` in `.cargo/config.toml`; the Z3 build +uses the same static MSVC runtime. CMake and a C++ compiler are required on +cold builds. Source lives under `.cache/windows-z3-source`; Cargo caches the +compiled library separately. `Z3_SRC_SOURCE_DIR` can point +to a local Z3 5.1.0 source tree instead of downloading it. +The wrapper uses Ninja to preserve MSVC optimization flags and keeps Z3 +optimized in development and test builds, matching the +previous prebuilt solver's performance. Rust application debug settings stay +unchanged; the dependency profile lives in `.cargo/windows-msvc.toml`. + +To supply your own library, set `Z3_LIBRARY_PATH_OVERRIDE` to the directory +containing a target-compatible, **static** `libz3.lib` built with `/MT`, and +`Z3_SYS_Z3_HEADER` to its `z3.h`. Import libraries for `libz3.dll` are rejected +by the release build's executable dependency check. +If an inherited override points to a directory containing `libz3.dll`, the +wrapper uses the pinned source build instead. + +Windows package builds intentionally stop linking shared Z3 installations. +Existing automation using a prebuilt override can keep that environment setting +when `libz3.dll` is in the override directory, but must provide CMake and a C++ +compiler for the source build. For offline builds, point `Z3_SRC_SOURCE_DIR` +to a local Z3 5.1.0 source tree. To retain a custom binary library, rebuild it +with `Z3_BUILD_LIBZ3_SHARED=OFF` and `CMAKE_MSVC_RUNTIME_LIBRARY=MultiThreaded`, +then update the override to that static library directory. A DLL import library +without an adjacent DLL still fails the release dependency check. ### Windows full build @@ -233,7 +240,7 @@ Run these commands in a `windows`-based checkout. To build the full set of Windows binaries, including `openshell-gateway.exe`, `openshell.exe`, `openshell-supervisor.exe`, and `openshell-windows-sandbox.exe`, use the `windows:build:x64` mise task instead of a -single-crate `cargo build`. It downloads the pinned prebuilt Z3 release by default. A +single-crate `cargo build`. It builds the pinned static Z3 library by default. A full build also compiles crates that use `bindgen` (e.g. the MXC driver on Windows), so it requires `libclang.dll`; if LLVM is not on the default search path, set `LIBCLANG_PATH` to the directory containing `libclang.dll`: @@ -244,17 +251,18 @@ mise run --skip-tools windows:build:x64 ``` For ARM64, use `windows:build:arm64` with ARM64 MSVC components and host-native -LLVM. Keep the matching `libz3.dll` staged by the build beside the executables. +LLVM. The four executables do not require `libz3.dll` or the Visual C++ +redistributable. Release builds check both normal and delayed PE imports. See [Windows validation](https://github.com/NVIDIA/OpenShell/blob/windows/TESTING.md#native-windows-validation) for native tests and MXC E2E requirements. Docker is not required for the native Windows build or MXC runtime. -To use a local x64 Z3 release instead of the prebuilt download, set +To use a local static x64 Z3 build with the static CRT, set `Z3_LIBRARY_PATH_OVERRIDE` and `Z3_SYS_Z3_HEADER` before running the task: ```powershell -$env:Z3_LIBRARY_PATH_OVERRIDE='C:\path\to\z3-5.1.0-x64-win\bin' -$env:Z3_SYS_Z3_HEADER='C:\path\to\z3-5.1.0-x64-win\include\z3.h' +$env:Z3_LIBRARY_PATH_OVERRIDE='C:\path\to\z3-5.1.0-x64-static\lib' +$env:Z3_SYS_Z3_HEADER='C:\path\to\z3-5.1.0-x64-static\include\z3.h' mise run --skip-tools windows:build:x64 ``` diff --git a/WINDOWS.md b/WINDOWS.md index cbd5be8b03..dd106fa54c 100644 --- a/WINDOWS.md +++ b/WINDOWS.md @@ -73,12 +73,15 @@ mise run --skip-tools windows:build:arm64 The tasks produce `openshell.exe`, `openshell-gateway.exe`, `openshell-supervisor.exe`, and `openshell-windows-sandbox.exe` under `target//release/`. Keep these binaries from the same build -together with the matching `libz3.dll` staged by the build task. The target +together. The target triples are `x86_64-pc-windows-msvc` and `aarch64-pc-windows-msvc`. `CARGO_TARGET_DIR` can override the output root. -The runtime host also needs the matching Visual C++ runtime. +These executables use the static CRT, and the gateway embeds Z3. They do not +require `libz3.dll` or the Visual C++ redistributable. Windows system DLLs and +the separate MXC executor remain required. -The build uses pinned prebuilt Z3 by default. Compile and test hosts do not +The build compiles checksum-pinned Z3 5.1.0 source with the static runtime. +Cold builds require CMake and Ninja and take longer; Cargo caches the compiled library. Compile and test hosts do not need a working MXC runtime; running native sandboxes does. These tasks do not install an MSI, register a Windows service, or publish a release. diff --git a/crates/openshell-driver-mxc/README.md b/crates/openshell-driver-mxc/README.md index 7a4c834af0..a02592c1cd 100644 --- a/crates/openshell-driver-mxc/README.md +++ b/crates/openshell-driver-mxc/README.md @@ -256,11 +256,10 @@ drivers need no UI-specific handling. UI policy itself does not require the ## Validation -Windows release build tasks stage the pinned, target-matching `libz3.dll` beside -the gateway and verify its architecture and copied SHA256. Conflicting cached -DLLs fail the build instead of selecting an arbitrary runtime. Local E2E requires -this adjacent DLL; rebuild for the native architecture if preflight reports it -missing. +Windows release build tasks use static CRT linkage and embed Z3 in the gateway. +They verify PE architecture and reject normal or delayed imports of Z3 and +redistributable CRT DLLs. Keep the four executables from the same build together; +no adjacent `libz3.dll` or Visual C++ redistributable is required for them. ### Remote Windows host @@ -283,12 +282,13 @@ uses the existing `windows:build:*` task, and validates the executable PE architecture before testing. `-BuildDirectory` selects a dedicated Cargo cache. `-SkipBuild` reuses existing artifacts without claiming they match current sources. `-Scenario` selects an existing harness scenario. +The deprecated `-Z3DllPath` argument remains accepted for existing automation; +it is ignored because packages embed Z3. -The package includes `libz3.dll` from the target-specific Cargo build cache. -Windows prebuilt Z3 uses a dynamic runtime DLL. Supply `-Z3DllPath` if multiple -different cached versions exist or when using a custom Z3 build. The remote -host needs the native Visual C++ runtime; the runner validates DLL architecture -and gateway startup before invoking E2E. +The package contains the four statically linked OpenShell executables plus the +test-only forwarding fixture. The remote runner verifies their PE architecture +and static Z3/CRT imports, then checks gateway startup before invoking E2E. +The separate MXC executor retains its own prerequisites. The example gateway explicitly enables caller driver config so tests can submit their workload command and directory. MXC does not implement external-resource diff --git a/crates/openshell-driver-mxc/examples/README-ocsf-audit.txt b/crates/openshell-driver-mxc/examples/README-ocsf-audit.txt index e9acdd1f9c..9822134499 100644 --- a/crates/openshell-driver-mxc/examples/README-ocsf-audit.txt +++ b/crates/openshell-driver-mxc/examples/README-ocsf-audit.txt @@ -45,11 +45,10 @@ WHAT YOU GET BACK -ShareOut '\\server\share' (off by default; results stay local otherwise). FILES IN THIS PACKAGE - openshell-gateway.exe the gateway (self-contained; needs only VC++ runtime) + openshell-gateway.exe the gateway (static CRT and embedded Z3) openshell.exe the CLI openshell-supervisor.exe host supervisor openshell-windows-sandbox.exe sandbox runtime - libz3.dll matching native Z3 runtime mxc-ocsf-audit.toml gateway/driver config (process_container, etw_audit=true, egress proxy) ocsf-audit.yaml sandbox policy (read-write grant to the share dir) run-ocsf-audit.ps1 the orchestrator you run diff --git a/crates/openshell-driver-mxc/examples/run-mxc-e2e.ps1 b/crates/openshell-driver-mxc/examples/run-mxc-e2e.ps1 index 0b5709d7d4..2c10aeafb4 100644 --- a/crates/openshell-driver-mxc/examples/run-mxc-e2e.ps1 +++ b/crates/openshell-driver-mxc/examples/run-mxc-e2e.ps1 @@ -682,9 +682,6 @@ try { if (-not (Test-Path $policyDir)) { throw "e2e-policies/ directory not found at $policyDir" } - if (-not (Test-Path -LiteralPath (Join-Path $BinaryDir 'libz3.dll') -PathType Leaf)) { - throw "Missing libz3.dll beside the gateway. Re-run windows:build for this architecture, or package the matching Z3 runtime with these binaries." - } # Capture the pristine TOML once; every scenario renders a fresh copy from this. $tomlBase = Get-Content $tomlTemplate -Raw diff --git a/crates/openshell-driver-mxc/tests/windows_demo_examples.rs b/crates/openshell-driver-mxc/tests/windows_demo_examples.rs index 59f7cefe91..6586c34230 100644 --- a/crates/openshell-driver-mxc/tests/windows_demo_examples.rs +++ b/crates/openshell-driver-mxc/tests/windows_demo_examples.rs @@ -262,77 +262,15 @@ if (Test-Path -LiteralPath $cliStateRoot) { throw "temporary state not removed" } #[test] -fn windows_build_stages_only_unambiguous_target_matching_z3() { - // Synthetic PE headers exercise packaging checks, not MXC containment. - let root = tempfile::tempdir().unwrap(); - let script = r#" -$ErrorActionPreference = 'Stop' -Set-StrictMode -Version Latest -$tokens = $null -$errors = $null -$ast = [System.Management.Automation.Language.Parser]::ParseFile( - $env:OPENSHELL_BUILD_SCRIPT, [ref]$tokens, [ref]$errors) -if ($errors.Count) { throw "build script parse errors: $($errors.Message -join '; ')" } -foreach ($name in @('Get-Sha256', 'Stage-Z3Runtime')) { - $function = $ast.Find({ param($node) - $node -is [System.Management.Automation.Language.FunctionDefinitionAst] -and $node.Name -eq $name - }, $true) - if ($null -eq $function) { throw "missing function: $name" } - Invoke-Expression $function.Extent.Text -} -# Mise's Windows PowerShell may not expose this module cmdlet. Packaging must -# retain its checksum checks without depending on it. -function Get-FileHash { throw 'Get-FileHash must not be used' } -$TargetDir = $env:OPENSHELL_TEST_ROOT -$PrebuiltZ3Version = '5.1.0' -$env:Z3_LIBRARY_PATH_OVERRIDE = $null -$release = Join-Path $TargetDir 'aarch64-pc-windows-msvc/release' -$sourceDir = Join-Path $release 'build/z3-sys-first/out/z3-5.1.0/bin' -New-Item -ItemType Directory -Path $sourceDir -Force | Out-Null -$source = Join-Path $sourceDir 'libz3.dll' -$bytes = New-Object byte[] 128 -$bytes[0] = 0x4D; $bytes[1] = 0x5A; $bytes[60] = 64 -$bytes[64] = 0x50; $bytes[65] = 0x45 -$bytes[68] = 0x64; $bytes[69] = 0xAA -[IO.File]::WriteAllBytes($source, $bytes) -Stage-Z3Runtime 'aarch64-pc-windows-msvc' -$destination = Join-Path $release 'libz3.dll' -if ((Get-Sha256 $destination) -ne (Get-Sha256 $source)) { throw 'copy mismatch' } -# A stale adjacent DLL must be overwritten from the pinned cache. -[IO.File]::WriteAllBytes($destination, (New-Object byte[] 128)) -Stage-Z3Runtime 'aarch64-pc-windows-msvc' -if ((Get-Sha256 $destination) -ne (Get-Sha256 $source)) { throw 'stale DLL retained' } -$bytes[68] = 0x64; $bytes[69] = 0x86 -[IO.File]::WriteAllBytes($source, $bytes) -$rejected = $false -try { Stage-Z3Runtime 'aarch64-pc-windows-msvc' } catch { - if ($_.Exception.Message -notmatch 'architecture') { throw } - $rejected = $true -} -if (-not $rejected) { throw 'wrong architecture accepted' } -$secondDir = Join-Path $release 'build/z3-sys-second/out/z3-5.1.0/bin' -New-Item -ItemType Directory -Path $secondDir -Force | Out-Null -$bytes[69] = 0xAA -[IO.File]::WriteAllBytes((Join-Path $secondDir 'libz3.dll'), $bytes) -$rejected = $false -try { Stage-Z3Runtime 'aarch64-pc-windows-msvc' } catch { - if ($_.Exception.Message -notmatch 'Ambiguous') { throw } - $rejected = $true -} -if (-not $rejected) { throw 'conflicting runtimes accepted' } -"#; +fn windows_build_rejects_dynamic_z3_and_crt_imports() { let output = Command::new("powershell.exe") - .args(["-NoProfile", "-Command", script]) - .env( - "OPENSHELL_BUILD_SCRIPT", - examples().join("../../../tasks/scripts/windows-msvc.ps1"), - ) - .env("OPENSHELL_TEST_ROOT", root.path()) + .args(["-NoProfile", "-ExecutionPolicy", "Bypass", "-File"]) + .arg(examples().join("../../../tasks/scripts/windows-runtime-tests.ps1")) .output() .unwrap(); assert!( output.status.success(), - "Z3 staging regression failed:\n{}\n{}", + "Static runtime regression failed:\n{}\n{}", String::from_utf8_lossy(&output.stdout), String::from_utf8_lossy(&output.stderr) ); diff --git a/docs/observability/telemetry.mdx b/docs/observability/telemetry.mdx index 1d40772d88..bb1d602265 100644 --- a/docs/observability/telemetry.mdx +++ b/docs/observability/telemetry.mdx @@ -46,8 +46,8 @@ cargo build --release -p openshell-gateway --no-default-features --features tele # Docker and VM only, with telemetry compiled out. cargo build --release -p openshell-gateway --no-default-features --features compute-driver-docker,compute-driver-vm -# Windows MXC only, with telemetry support and prebuilt Z3. -cargo build --release -p openshell-gateway --no-default-features --features telemetry,compute-driver-mxc,openshell-server/prebuilt-z3 +# Windows MXC only, with telemetry support and static Z3. +cargo build --release -p openshell-gateway --no-default-features --features telemetry,compute-driver-mxc,openshell-server/vendored-z3 ``` Regular builds keep their platform driver set through the default `in-tree-compute-drivers` compatibility feature. On Windows, `compute-driver-mxc` selects MXC and the other four features install unsupported-driver stubs. On other platforms, MXC is excluded. diff --git a/tasks/scripts/run-mxc-remote.ps1 b/tasks/scripts/run-mxc-remote.ps1 index 8ead81e5c3..37e6501655 100644 --- a/tasks/scripts/run-mxc-remote.ps1 +++ b/tasks/scripts/run-mxc-remote.ps1 @@ -3,6 +3,7 @@ # Remote half of test-mxc-remote.ps1. No source checkout or build tools required. $ErrorActionPreference = 'Stop' +. (Join-Path $PSScriptRoot 'windows-runtime.ps1') $ProgressPreference = 'SilentlyContinue' $PSNativeCommandUseErrorActionPreference = $false $repo = [IO.Path]::GetFullPath((Join-Path $PSScriptRoot '../..')) @@ -30,8 +31,8 @@ try { $native = Get-ItemPropertyValue 'HKLM:\SYSTEM\CurrentControlSet\Control\Session Manager\Environment' PROCESSOR_ARCHITECTURE $expected = if ($manifest.target -eq 'aarch64-pc-windows-msvc') { 0xaa64 } else { 0x8664 } if (($expected -eq 0xaa64 -and $native -ne 'ARM64') -or ($expected -eq 0x8664 -and $native -ne 'AMD64')) { throw 'Native host architecture changed.' } - foreach ($name in @('openshell.exe', 'openshell-gateway.exe', 'openshell-supervisor.exe', 'openshell-windows-sandbox.exe', 'mxc-forwarding-agent.exe', 'libz3.dll')) { - if ((Get-PeMachine (Join-Path $repo "bin/$name")) -ne $expected) { throw "Wrong executable architecture: $name" } + foreach ($name in @('openshell.exe', 'openshell-gateway.exe', 'openshell-supervisor.exe', 'openshell-windows-sandbox.exe', 'mxc-forwarding-agent.exe')) { + Assert-StaticWindowsRuntime (Join-Path $repo "bin/$name") $manifest.target } $wxc = $manifest.wxcExecPath # Check loader dependencies before the harness starts a gateway. @@ -50,7 +51,7 @@ try { $stderr = $process.StandardError.ReadToEndAsync() if (-not $process.WaitForExit(15000)) { $process.Kill(); throw 'Gateway loader check timed out.' } @($stdout.Result, $stderr.Result) | Set-Content (Join-Path $export 'gateway-version.txt') -Encoding UTF8 - if ($process.ExitCode -ne 0) { throw "Gateway loader check failed: exit $($process.ExitCode). Check the packaged DLLs and native Visual C++ runtime." } + if ($process.ExitCode -ne 0) { throw "Gateway loader check failed: exit $($process.ExitCode). Check the package architecture and Windows loader diagnostics." } Write-Host "Gateway loader check passed: $($stdout.Result.Trim())" } finally { $process.Dispose() } if (-not $wxc) { $wxc = (Get-Command wxc-exec.exe -ErrorAction Stop).Source } diff --git a/tasks/scripts/test-mxc-remote-tests.ps1 b/tasks/scripts/test-mxc-remote-tests.ps1 index 0971360804..dc14ecd253 100644 --- a/tasks/scripts/test-mxc-remote-tests.ps1 +++ b/tasks/scripts/test-mxc-remote-tests.ps1 @@ -9,9 +9,17 @@ foreach ($name in @('test-mxc-remote.ps1', 'run-mxc-remote.ps1')) { $errors = $null $ast = [Management.Automation.Language.Parser]::ParseFile((Join-Path $PSScriptRoot $name), [ref]$tokens, [ref]$errors) if ($errors.Count) { throw ($errors | Out-String) } + if ($name -eq 'test-mxc-remote.ps1') { + # Bind the real parameter block without running builds, SSH, or key checks. + $binding = [scriptblock]::Create($ast.ParamBlock.Extent.Text + '; $PSBoundParameters.Z3DllPath') + if ((& $binding -HostName localhost -UserName test -Z3DllPath 'legacy/libz3.dll') -ne 'legacy/libz3.dll') { + throw 'Legacy -Z3DllPath argument no longer binds.' + } + $count++ + } # Load pure functions only; do not start builds or SSH sessions. foreach ($function in $ast.FindAll({ param($node) $node -is [Management.Automation.Language.FunctionDefinitionAst] }, $true)) { - if ($function.Name -in @('Quote-PS', 'Find-Z3Runtime', 'Read-CacheHashes', 'Get-PeMachine')) { Invoke-Expression $function.Extent.Text } + if ($function.Name -in @('Quote-PS', 'Read-CacheHashes', 'Get-PeMachine')) { Invoke-Expression $function.Extent.Text } } $count++ } @@ -35,12 +43,6 @@ function Pe-Bytes([int] $Machine) { } $fixture = [IO.Path]::GetTempFileName() try { - if ((Find-Z3Runtime '' '' $fixture) -ne $fixture) { throw 'Explicit Z3 DLL path regression.' } - $count++ - $rejected = $false - try { Find-Z3Runtime ([IO.Path]::GetTempPath()) ('missing-' + [guid]::NewGuid().ToString('N')) '' | Out-Null } catch { $rejected = $true } - if (-not $rejected) { throw 'Missing Z3 DLL accepted.' } - $count++ foreach ($machine in @(0xaa64, 0x8664)) { [IO.File]::WriteAllBytes($fixture, (Pe-Bytes $machine)) if ((Get-PeMachine $fixture) -ne $machine) { throw 'PE architecture regression.' } @@ -53,27 +55,4 @@ try { $count++ # Reader cleanup must permit rewriting/deleting even after rejection. } finally { Remove-Item -LiteralPath $fixture } -$cacheFixture = Join-Path ([IO.Path]::GetTempPath()) ('openshell-z3-test-' + [guid]::NewGuid().ToString('N')) -$target = 'aarch64-pc-windows-msvc' -$first = Join-Path $cacheFixture "$target/release/build/z3-sys-first/out/z3-5.1.0/bin/libz3.dll" -$second = Join-Path $cacheFixture "$target/release/build/z3-sys-second/out/z3-5.1.0/bin/libz3.dll" -try { - New-Item -ItemType Directory -Path (Split-Path $first), (Split-Path $second) -Force | Out-Null - [IO.File]::WriteAllBytes($first, (Pe-Bytes 0xaa64)) - if ((Find-Z3Runtime $cacheFixture $target '') -ne $first) { throw 'Target-specific Z3 discovery regression.' } - $count++ - Copy-Item -LiteralPath $first -Destination $second - if (-not (Find-Z3Runtime $cacheFixture $target '')) { throw 'Identical cached Z3 runtimes rejected.' } - $count++ - [IO.File]::WriteAllBytes($second, (Pe-Bytes 0x8664)) - $rejected = $false - try { Find-Z3Runtime $cacheFixture $target '' | Out-Null } catch { $rejected = $true } - if (-not $rejected) { throw 'Ambiguous Z3 runtimes accepted.' } - $count++ -} finally { - # Only tear down the explicitly created fixture paths, not a recursive root. - foreach ($path in @($first, $second)) { if (Test-Path $path) { Remove-Item -LiteralPath $path } } - Get-ChildItem -LiteralPath $cacheFixture -Directory -Recurse | Sort-Object { $_.FullName.Length } -Descending | ForEach-Object { Remove-Item -LiteralPath $_.FullName } - Remove-Item -LiteralPath $cacheFixture -} Write-Host "$count remote-script regression checks passed. No runtime qualification claimed." diff --git a/tasks/scripts/test-mxc-remote.ps1 b/tasks/scripts/test-mxc-remote.ps1 index 6c842f6c31..c89bb96a6c 100644 --- a/tasks/scripts/test-mxc-remote.ps1 +++ b/tasks/scripts/test-mxc-remote.ps1 @@ -10,6 +10,7 @@ param( [string] $IdentityFile = (Join-Path $env:USERPROFILE '.ssh/openshell-mxc-test'), [ValidateSet('auto', 'arm64', 'x64')] [string] $Architecture = 'auto', [string] $BuildDirectory, + # Deprecated: retained so existing callers can use static packages unchanged. [string] $Z3DllPath, [string] $RemoteRoot, [string] $WxcExecPath, @@ -23,6 +24,9 @@ Set-StrictMode -Version Latest $ErrorActionPreference = 'Stop' $PSNativeCommandUseErrorActionPreference = $false $repo = (Resolve-Path (Join-Path $PSScriptRoot '../..')).Path +if ($PSBoundParameters.ContainsKey('Z3DllPath')) { + Write-Warning '-Z3DllPath is deprecated and ignored: Windows packages embed Z3.' +} if ($HostName -notmatch '^[A-Za-z0-9.-]+$' -or $UserName -notmatch '^[A-Za-z0-9_.-]+$') { throw 'Use a hostname/IP and a simple SSH username (or configure an SSH alias).' } @@ -36,21 +40,6 @@ $sshOptions = @('-i', $IdentityFile, '-o', 'BatchMode=yes', '-o', 'IdentitiesOnl $scpOptions = @('-i', $IdentityFile, '-o', 'BatchMode=yes', '-o', 'IdentitiesOnly=yes', '-o', 'StrictHostKeyChecking=yes', '-o', 'ConnectTimeout=15', '-P', "$SshPort") function Quote-PS([string] $Value) { return "'" + $Value.Replace("'", "''") + "'" } -function Find-Z3Runtime([string] $BuildRoot, [string] $RustTarget, [string] $ExplicitPath) { - if ($ExplicitPath) { - if (-not (Test-Path -LiteralPath $ExplicitPath -PathType Leaf)) { throw "Missing Z3 runtime: $ExplicitPath" } - return (Resolve-Path -LiteralPath $ExplicitPath).Path - } - $release = Join-Path $BuildRoot "$RustTarget/release" - $adjacent = Join-Path $release 'libz3.dll' - if (Test-Path -LiteralPath $adjacent -PathType Leaf) { return $adjacent } - # Windows prebuilt-z3 links an import library rather than a static library. - $candidates = @(Get-ChildItem -Path (Join-Path $release 'build/z3-sys-*/out/z3-*/bin/libz3.dll') -File -ErrorAction SilentlyContinue) - if ($candidates.Count -eq 0) { throw 'libz3.dll not found in the target build cache. Supply -Z3DllPath matching the linked Z3 build.' } - $hashes = @($candidates | ForEach-Object { (Get-FileHash -LiteralPath $_.FullName).Hash } | Select-Object -Unique) - if ($hashes.Count -ne 1) { throw 'Multiple different Z3 runtimes found. Supply -Z3DllPath matching the linked Z3 build.' } - return $candidates[0].FullName -} function Read-CacheHashes([string] $Json) { # Windows PowerShell 5.1 emits decoded JSON arrays as one pipeline object. # Explicitly enumerate so callers do not get a nested array with -contains. @@ -123,8 +112,7 @@ try { } $examples = 'crates/openshell-driver-mxc/examples' $files += @{ source = (Join-Path $BuildDirectory "$target/release/examples/mxc-forwarding-agent.exe"); path = 'bin/mxc-forwarding-agent.exe' } - $files += @{ source = (Find-Z3Runtime $BuildDirectory $target $Z3DllPath); path = 'bin/libz3.dll' } - foreach ($path in @("$examples/run-mxc-e2e.ps1", "$examples/mxc-gateway.toml", 'tasks/scripts/run-mxc-remote.ps1')) { + foreach ($path in @("$examples/run-mxc-e2e.ps1", "$examples/mxc-gateway.toml", 'tasks/scripts/run-mxc-remote.ps1', 'tasks/scripts/windows-runtime.ps1')) { $files += @{ source = (Join-Path $repo $path); path = $path } } foreach ($policy in Get-ChildItem (Join-Path $repo "$examples/e2e-policies") -File) { diff --git a/tasks/scripts/windows-msvc.ps1 b/tasks/scripts/windows-msvc.ps1 index 500f8701e1..5e79e3f2f0 100644 --- a/tasks/scripts/windows-msvc.ps1 +++ b/tasks/scripts/windows-msvc.ps1 @@ -24,6 +24,7 @@ if (-not [System.Runtime.InteropServices.RuntimeInformation]::IsOSPlatform([Syst } $RepoRoot = (Resolve-Path (Join-Path $PSScriptRoot "..\..")).Path +. (Join-Path $PSScriptRoot 'windows-runtime.ps1') if (-not $LogDir) { $LogDir = $RepoRoot } @@ -36,6 +37,7 @@ $TargetDir = $env:CARGO_TARGET_DIR if ([string]::IsNullOrWhiteSpace($TargetDir)) { $TargetDir = Join-Path $RepoRoot "target" } +$TargetDir = [IO.Path]::GetFullPath($TargetDir) $BuildJobsValue = $env:OPENSHELL_WINDOWS_BUILD_JOBS if ([string]::IsNullOrWhiteSpace($BuildJobsValue)) { @@ -53,12 +55,11 @@ $WindowsCargoMutex = [System.Threading.Mutex]::new($false, "Local\OpenShellWindo $UnsupportedDriverPackageExcludes = "--exclude openshell-driver-docker --exclude openshell-driver-kubernetes --exclude openshell-driver-kubernetes-secrets --exclude openshell-driver-podman --exclude openshell-driver-vault --exclude openshell-driver-vm --exclude openshell-sandbox --exclude openshell-vfio" $WindowsClippyPackageExcludes = $UnsupportedDriverPackageExcludes $WindowsClippyLintArgs = "-D warnings -A dead-code -A unused-imports -A clippy::unused-async" -$PrebuiltZ3WorkspaceFeatures = "--features openshell-prover/prebuilt-z3" -$PrebuiltZ3ServerFeatures = "--features openshell-server/prebuilt-z3,openshell-prover/prebuilt-z3" -$PrebuiltZ3Version = "5.1.0" -$Z3WorkspaceFeatures = $PrebuiltZ3WorkspaceFeatures -$Z3ServerFeatures = $PrebuiltZ3ServerFeatures -$Z3GatewayFeatures = "--features prebuilt-z3" +$Z3Version = '5.1.0' +$Z3SourceSha256 = 'e7ab3f8b682d5f55e813f951c7f9bb4fb9a48567ce6143d7adae23027e0e42a0' +$Z3WorkspaceFeatures = '--features openshell-prover/vendored-z3' +$Z3ServerFeatures = '--features openshell-server/vendored-z3,openshell-prover/vendored-z3' +$Z3GatewayFeatures = '--features vendored-z3' function Get-VsInstallRoots { $programFiles = @( @@ -294,7 +295,7 @@ function Configure-Arm64CrossBuild([string[]] $RustTargets) { Write-Host "==> ARM64 cross-build toolchain" Write-Host " clang-cl: $clangCl" Write-Host " ninja: $ninja" - Write-Host " Z3: MSVC cl.exe with the Visual Studio generator" + Write-Host " Z3: MSVC cl.exe with Ninja" } function Get-HostArch { @@ -361,14 +362,54 @@ function Resolve-Z3HeaderPath([string] $HeaderPath) { return (Resolve-Path $HeaderPath).Path } +function Resolve-Z3Source { + if ($env:Z3_SRC_SOURCE_DIR) { + $source = (Resolve-Path -LiteralPath $env:Z3_SRC_SOURCE_DIR).Path + } else { + # rust-cache prunes non-Cargo files under target, including VERSION.txt. + # Keep the complete source tree outside that cache's ownership. + $sourceRoot = Join-Path $RepoRoot '.cache/windows-z3-source' + $source = Join-Path $sourceRoot "z3-z3-$Z3Version" + if (-not (Test-Path -LiteralPath $source -PathType Container)) { + $staging = Join-Path $sourceRoot ("download-" + [guid]::NewGuid().ToString('N')) + New-Item -ItemType Directory -Path $staging -Force | Out-Null + try { + $archive = Join-Path $staging 'source.zip' + # Keep the existing Windows solver version. The z3-src crate's + # bundled source can advance independently of this release pin. + [Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12 + Invoke-WebRequest -UseBasicParsing -Uri "https://github.com/Z3Prover/z3/archive/refs/tags/z3-$Z3Version.zip" -OutFile $archive + if ((Get-Sha256 $archive) -ne $Z3SourceSha256) { throw 'Z3 source archive checksum mismatch.' } + # Hosted ARM64 runners can expose a PowerShell 7 archive module + # to Windows PowerShell 5.1. Use the .NET API shared by both. + Add-Type -AssemblyName System.IO.Compression.FileSystem + [IO.Compression.ZipFile]::ExtractToDirectory($archive, $staging) + New-Item -ItemType Directory -Path (Split-Path $source) -Force | Out-Null + Move-Item -LiteralPath (Join-Path $staging "z3-z3-$Z3Version") -Destination $source + } finally { Remove-Item -LiteralPath $staging -Recurse -Force } + } + } + $versionFile = Join-Path $source 'scripts/VERSION.txt' + if (-not (Test-Path -LiteralPath $versionFile) -or ([version](Get-Content -LiteralPath $versionFile -Raw).Trim()).ToString(3) -ne $Z3Version) { + throw "Z3 source must be version $Z3Version at $source." + } + return $source +} + function Configure-Z3 { + if ($env:Z3_LIBRARY_PATH_OVERRIDE -and (Test-Path -LiteralPath (Join-Path $env:Z3_LIBRARY_PATH_OVERRIDE 'libz3.dll'))) { + Write-Warning 'Ignoring the shared Z3 override; Windows packages require static Z3 and CRT linkage. Building pinned source instead.' + $env:Z3_LIBRARY_PATH_OVERRIDE = $null + $env:Z3_SYS_Z3_HEADER = $null + } if ([string]::IsNullOrWhiteSpace($env:Z3_LIBRARY_PATH_OVERRIDE)) { - Write-Host "==> Z3: prebuilt release" - $env:Z3_SYS_Z3_VERSION = $PrebuiltZ3Version - Write-Host " Z3_SYS_Z3_VERSION=$env:Z3_SYS_Z3_VERSION" + $env:Z3_SRC_SOURCE_DIR = Resolve-Z3Source + $env:Z3_SYS_Z3_VERSION = $Z3Version + $env:CMAKE_BUILD_PARALLEL_LEVEL = "$WindowsBuildJobs" + Write-Host "==> Z3: static library and static CRT from $env:Z3_SRC_SOURCE_DIR" return [pscustomobject]@{ - WorkspaceFeatures = $PrebuiltZ3WorkspaceFeatures - ServerFeatures = $PrebuiltZ3ServerFeatures + WorkspaceFeatures = $Z3WorkspaceFeatures + ServerFeatures = $Z3ServerFeatures } } @@ -389,7 +430,7 @@ function Configure-Z3 { $env:PATH = "$libDir;$env:PATH" } - Write-Host "==> Z3: system" + Write-Host "==> Z3: supplied static MSVC library (must use /MT)" Write-Host " Z3_LIBRARY_PATH_OVERRIDE=$env:Z3_LIBRARY_PATH_OVERRIDE" Write-Host " Z3_SYS_Z3_HEADER=$env:Z3_SYS_Z3_HEADER" @@ -399,6 +440,30 @@ function Configure-Z3 { } } +function Reset-WindowsCMakeGeneratorCache([string] $RustTarget) { + foreach ($profile in @('debug', 'release')) { + $buildRoot = Join-Path $TargetDir "$RustTarget/$profile/build" + if (-not (Test-Path -LiteralPath $buildRoot)) { continue } + $stale = @(Get-ChildItem -LiteralPath $buildRoot -Filter 'z3-sys-*' -Directory | Where-Object { + $cache = Join-Path $_.FullName 'out/build/CMakeCache.txt' + if (Test-Path -LiteralPath $cache) { + Select-String -LiteralPath $cache -Pattern '^CMAKE_GENERATOR:INTERNAL=(?!Ninja$)' -Quiet + } else { + # A pruned cache can retain the archive without its build rules. + Test-Path -LiteralPath (Join-Path $_.FullName 'out/lib/libz3.lib') + } + }) + if ($stale.Count) { + # Cargo must invalidate its build-script fingerprint and the linked + # archive too; deleting only CMake files can reuse an old solver. + $cargoProfile = if ($profile -eq 'debug') { 'dev' } else { 'release' } + Write-Host "==> Rebuilding cached $cargoProfile Z3 with Ninja" + & cargo clean -p z3-sys --target $RustTarget --profile $cargoProfile --target-dir $TargetDir + if ($LASTEXITCODE -ne 0) { throw 'Could not invalidate the old Z3 build cache.' } + } + } +} + function Invoke-VsCargo { param( [Parameter(Mandatory = $true)] [string] $RustTarget, @@ -406,6 +471,24 @@ function Invoke-VsCargo { [Parameter(Mandatory = $true)] [string] $LogName ) + # cmake-rs clears MSVC's configuration optimization flags when it selects + # the Visual Studio generator implicitly. Ninja preserves /O2 for Release + # Z3, including the dependency profile used by development/test builds. + $ninja = Resolve-NinjaPath + Add-PathEntry (Split-Path -Parent $ninja) + + if ($CargoArgs.StartsWith('cargo ') -and $CargoArgs -notmatch '--manifest-path') { + # The old prebuilt solver was optimized even in debug/test builds. + # An unoptimized native Z3 can exhaust otherwise unchanged solver budgets. + $config = Join-Path $RepoRoot '.cargo/windows-msvc.toml' + if ($CargoArgs.StartsWith('cargo nextest run ')) { + # Nextest starts Cargo itself; global Cargo options are not forwarded. + $CargoArgs = 'cargo nextest run --config "' + $config + '" ' + $CargoArgs.Substring(18) + } else { + $CargoArgs = 'cargo --config "' + $config + '" ' + $CargoArgs.Substring(6) + } + } + & rustup target add $RustTarget if ($LASTEXITCODE -ne 0) { throw "rustup target add $RustTarget failed" @@ -428,6 +511,10 @@ function Invoke-VsCargo { "set `"CARGO_TARGET_DIR=$TargetDir`"", "set `"CARGO_BUILD_JOBS=$WindowsBuildJobs`"", "set `"CARGO_INCREMENTAL=0`"", + "set `"CMAKE_GENERATOR=Ninja`"", + # z3-src adds MSBuild's -m unless a C++ launcher is configured. Use + # CMake's portable passthrough when no compiler cache was requested. + 'if not defined CMAKE_CXX_COMPILER_LAUNCHER (set "CMAKE_CXX_COMPILER_LAUNCHER=cmake;-E;env")', "set `"RUSTC_WRAPPER=$rustcWrapper`"" ) if ($hostArch -eq "amd64" -and $RustTarget -eq "aarch64-pc-windows-msvc") { @@ -463,6 +550,7 @@ function Invoke-VsCargo { throw "Timed out waiting for another Windows Cargo task to finish." } + Reset-WindowsCMakeGeneratorCache $RustTarget $cmdWithLog = "$cmd > `"$logPath`" 2>&1" & cmd /v:on /d /c $cmdWithLog $exitCode = $LASTEXITCODE @@ -524,39 +612,12 @@ function Invoke-Build([string] $RustTarget) { -RustTarget $RustTarget ` -CargoArgs "cargo build --release --target $RustTarget --bin openshell-gateway --bin openshell --bin openshell-supervisor --bin openshell-windows-sandbox $Z3WorkspaceFeatures" ` -LogName "build-$RustTarget-release.log" - Stage-Z3Runtime $RustTarget -} - -function Stage-Z3Runtime([string] $RustTarget) { - $release = Join-Path $TargetDir "$RustTarget/release" - if ($env:Z3_LIBRARY_PATH_OVERRIDE) { - $candidates = @(Get-Item -LiteralPath (Join-Path $env:Z3_LIBRARY_PATH_OVERRIDE 'libz3.dll') -ErrorAction SilentlyContinue) - } else { - # Only use the pinned release from this target's build cache. Never pick - # the newest arbitrary DLL, or trust a stale adjacent runtime. - $candidates = @(Get-ChildItem -Path (Join-Path $release "build/z3-sys-*/out/z3-$PrebuiltZ3Version/bin/libz3.dll") -File -ErrorAction SilentlyContinue) - } - if ($candidates.Count -eq 0) { throw "Matching libz3.dll not found for $RustTarget." } - $hashes = @($candidates | ForEach-Object { Get-Sha256 $_.FullName } | Select-Object -Unique) - if ($hashes.Count -ne 1) { throw "Ambiguous Z3 runtimes for $RustTarget; use a clean target cache or Z3_LIBRARY_PATH_OVERRIDE." } - $source = $candidates[0].FullName - $reader = [IO.BinaryReader]::new([IO.File]::OpenRead($source)) - try { - if ($reader.ReadUInt16() -ne 0x5A4D) { throw "Invalid Z3 DLL: $source" } - $reader.BaseStream.Position = 0x3C - $offset = $reader.ReadInt32() - if ($offset -lt 64 -or $offset -gt $reader.BaseStream.Length - 6) { throw 'Invalid Z3 PE offset.' } - $reader.BaseStream.Position = $offset - if ($reader.ReadUInt32() -ne 0x00004550) { throw 'Invalid Z3 PE signature.' } - $expected = if ($RustTarget -eq 'aarch64-pc-windows-msvc') { 0xAA64 } else { 0x8664 } - if ($reader.ReadUInt16() -ne $expected) { throw "Z3 DLL architecture does not match $RustTarget." } - } finally { $reader.Dispose() } - $destination = Join-Path $release 'libz3.dll' - if ([IO.Path]::GetFullPath($source) -ne [IO.Path]::GetFullPath($destination)) { - Copy-Item -LiteralPath $source -Destination $destination -Force - } - if ((Get-Sha256 $destination) -ne $hashes[0]) { throw 'Staged Z3 DLL hash mismatch.' } - Write-Host "==> Staged $destination (SHA256=$($hashes[0]))" + foreach ($binary in @('openshell-gateway.exe', 'openshell.exe', 'openshell-supervisor.exe', 'openshell-windows-sandbox.exe')) { + Assert-StaticWindowsRuntime (Join-Path $TargetDir "$RustTarget/release/$binary") $RustTarget + } + # Do not leave a previously staged DLL in a directory used for ZIP packages. + $staleDll = Join-Path $TargetDir "$RustTarget/release/libz3.dll" + if (Test-Path -LiteralPath $staleDll) { Remove-Item -LiteralPath $staleDll } } function Invoke-Test([string] $RustTarget) { @@ -661,7 +722,7 @@ if ($Action -in @("check", "lint", "build", "build-mxc-fixtures", "test", "test- $z3Features = Configure-Z3 $Z3WorkspaceFeatures = $z3Features.WorkspaceFeatures $Z3ServerFeatures = $z3Features.ServerFeatures - $Z3GatewayFeatures = if ($z3Features.WorkspaceFeatures) { "--features prebuilt-z3" } else { "" } + $Z3GatewayFeatures = if ($z3Features.WorkspaceFeatures) { "--features vendored-z3" } else { "" } $env:LIBCLANG_PATH = Resolve-LibclangPath Add-PathEntry $env:LIBCLANG_PATH Write-Host "==> LIBCLANG_PATH=$env:LIBCLANG_PATH" @@ -747,7 +808,7 @@ switch ($Action) { -LogName "test-$rustTarget-mxc-audit-examples.log" $package = Join-Path $TargetDir "mxc-ocsf-qualification-$rustTarget" New-Item -ItemType Directory -Force $package | Out-Null - foreach ($binary in @('openshell-gateway.exe', 'openshell.exe', 'openshell-supervisor.exe', 'openshell-windows-sandbox.exe', 'libz3.dll')) { + foreach ($binary in @('openshell-gateway.exe', 'openshell.exe', 'openshell-supervisor.exe', 'openshell-windows-sandbox.exe')) { Copy-Item -LiteralPath (Join-Path $TargetDir "$rustTarget/release/$binary") -Destination $package -Force } foreach ($artifact in @('run-ocsf-audit.ps1', 'mxc-ocsf-audit.toml', 'ocsf-audit.yaml')) { diff --git a/tasks/scripts/windows-runtime-tests.ps1 b/tasks/scripts/windows-runtime-tests.ps1 new file mode 100644 index 0000000000..8be01bcebf --- /dev/null +++ b/tasks/scripts/windows-runtime-tests.ps1 @@ -0,0 +1,158 @@ +# SPDX-FileCopyrightText: Copyright (c) 2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved. +# SPDX-License-Identifier: Apache-2.0 + +$ErrorActionPreference = 'Stop' +Set-StrictMode -Version Latest +. (Join-Path $PSScriptRoot 'windows-runtime.ps1') + +function New-Image([int] $Machine, [string] $Dependency, [switch] $Delay) { + $bytes = New-Object byte[] 1024 + function Set-U16([int] $Offset, [uint16] $Value) { [BitConverter]::GetBytes($Value).CopyTo($bytes, $Offset) } + function Set-U32([int] $Offset, [uint32] $Value) { [BitConverter]::GetBytes($Value).CopyTo($bytes, $Offset) } + Set-U16 0 0x5a4d + Set-U32 60 64 + Set-U32 64 0x4550 + Set-U16 68 $Machine + Set-U16 70 1 + Set-U16 84 240 + Set-U16 88 0x20b + Set-U32 196 16 + # One section maps RVAs 0x1000..0x11ff to file offsets 512..1023. + Set-U32 336 512 + Set-U32 340 0x1000 + Set-U32 344 512 + Set-U32 348 512 + if ($Dependency) { + if ($Delay) { + Set-U32 304 0x1000 + Set-U32 308 64 + Set-U32 512 1 + Set-U32 516 0x1080 + } else { + Set-U32 208 0x1000 + Set-U32 212 40 + Set-U32 524 0x1080 + } + [Text.Encoding]::ASCII.GetBytes($Dependency).CopyTo($bytes, 640) + } + return ,$bytes +} + +$fixture = [IO.Path]::GetTempFileName() +$count = 0 +try { + foreach ($entry in @(@(0xaa64, 'aarch64-pc-windows-msvc'), @(0x8664, 'x86_64-pc-windows-msvc'))) { + [IO.File]::WriteAllBytes($fixture, (New-Image $entry[0] 'KERNEL32.dll')) + Assert-StaticWindowsRuntime $fixture $entry[1] + $count++ + } + foreach ($dependency in @('libz3.dll', 'VCRUNTIME140.dll', 'VCRUNTIME140_1.dll', 'MSVCP140.dll', 'MSVCP140_ATOMIC_WAIT.dll', 'ucrtbase.dll', 'api-ms-win-crt-heap-l1-1-0.dll')) { + foreach ($delay in @($false, $true)) { + [IO.File]::WriteAllBytes($fixture, (New-Image 0xaa64 $dependency -Delay:$delay)) + $image = Get-WindowsImageDependencies $fixture + if ($image.Dependencies -notcontains $dependency) { throw "Import not detected: $dependency" } + $rejected = $false + try { Assert-StaticWindowsRuntime $fixture 'aarch64-pc-windows-msvc' } catch { + if ($_.Exception.Message -notmatch 'Dynamic Z3/CRT') { throw } + $rejected = $true + } + if (-not $rejected) { throw "Dynamic dependency accepted: $dependency" } + $count++ + } + } + [IO.File]::WriteAllBytes($fixture, (New-Image 0x8664 '')) + $rejected = $false + try { Assert-StaticWindowsRuntime $fixture 'aarch64-pc-windows-msvc' } catch { $rejected = $true } + if (-not $rejected) { throw 'Wrong PE architecture accepted.' } + $count++ + [IO.File]::WriteAllBytes($fixture, [byte[]]@(1, 2, 3, 4)) + $rejected = $false + try { Get-WindowsImageDependencies $fixture | Out-Null } catch { $rejected = $true } + if (-not $rejected) { throw 'Truncated PE image accepted.' } + $count++ +} finally { Remove-Item -LiteralPath $fixture } + +# Exercise cold source preparation without network access, including the legacy +# shared-Z3 environment used by the ARM64 CI host. +$tokens = $null; $errors = $null +$ast = [Management.Automation.Language.Parser]::ParseFile((Join-Path $PSScriptRoot 'windows-msvc.ps1'), [ref]$tokens, [ref]$errors) +if ($errors.Count) { throw ($errors | Out-String) } +foreach ($name in @('Get-Sha256', 'Resolve-Z3Source', 'Configure-Z3', 'Reset-WindowsCMakeGeneratorCache')) { + $function = $ast.Find({ param($node) $node -is [Management.Automation.Language.FunctionDefinitionAst] -and $node.Name -eq $name }, $true) + if ($null -eq $function) { throw "Missing build helper: $name" } + Invoke-Expression $function.Extent.Text +} +$sourceFixture = Join-Path ([IO.Path]::GetTempPath()) ('openshell-source-test-' + [guid]::NewGuid().ToString('N')) +try { + $scripts = Join-Path $sourceFixture 'input/z3-z3-5.1.0/scripts' + New-Item -ItemType Directory -Path $scripts -Force | Out-Null + Set-Content -LiteralPath (Join-Path $scripts 'VERSION.txt') -Value '5.1.0.0' -Encoding ASCII + Add-Type -AssemblyName System.IO.Compression.FileSystem + $sourceArchive = Join-Path $sourceFixture 'source.zip' + [IO.Compression.ZipFile]::CreateFromDirectory((Join-Path $sourceFixture 'input'), $sourceArchive) + function Invoke-WebRequest([switch] $UseBasicParsing, [string] $Uri, [string] $OutFile) { + Copy-Item -LiteralPath $sourceArchive -Destination $OutFile + } + $Z3Version = '5.1.0' + $Z3SourceSha256 = Get-Sha256 $sourceArchive + $Z3WorkspaceFeatures = '--features openshell-prover/vendored-z3' + $Z3ServerFeatures = '--features openshell-server/vendored-z3,openshell-prover/vendored-z3' + $WindowsBuildJobs = 2 + $RepoRoot = $sourceFixture + $TargetDir = Join-Path $sourceFixture 'target' + $oldBuild = Join-Path $TargetDir 'aarch64-pc-windows-msvc/release/build/z3-sys-old/out/build' + $newBuild = Join-Path $TargetDir 'aarch64-pc-windows-msvc/debug/build/z3-sys-new/out/build' + $otherTarget = Join-Path $TargetDir 'x86_64-pc-windows-msvc/release/build/z3-sys-old/out/build' + foreach ($build in @($oldBuild, $newBuild, $otherTarget)) { + New-Item -ItemType Directory -Path $build -Force | Out-Null + Set-Content -LiteralPath (Join-Path $build 'CMakeCache.txt') -Value 'CMAKE_GENERATOR:INTERNAL=Visual Studio 17 2022' + } + Set-Content -LiteralPath (Join-Path $newBuild 'CMakeCache.txt') -Value 'CMAKE_GENERATOR:INTERNAL=Ninja' + $script:cleanCalls = @() + function cargo { + $script:cleanCalls += ,@($args) + $global:LASTEXITCODE = 0 + } + Reset-WindowsCMakeGeneratorCache 'aarch64-pc-windows-msvc' + $expected = @('clean', '-p', 'z3-sys', '--target', 'aarch64-pc-windows-msvc', '--profile', 'release', '--target-dir', $TargetDir) + if ($script:cleanCalls.Count -ne 1 -or ($script:cleanCalls[0] -join '|') -ne ($expected -join '|')) { throw 'Cargo was not asked to invalidate precisely the stale solver profile and target.' } + if (-not (Test-Path -LiteralPath $newBuild) -or -not (Test-Path -LiteralPath $otherTarget)) { throw 'Unrelated build files removed.' } + $count++ + $archiveDir = Join-Path (Split-Path $oldBuild) 'lib' + New-Item -ItemType Directory -Path $archiveDir -Force | Out-Null + Set-Content -LiteralPath (Join-Path $archiveDir 'libz3.lib') -Value 'old cached archive' + Remove-Item -LiteralPath (Join-Path $oldBuild 'CMakeCache.txt') + Reset-WindowsCMakeGeneratorCache 'aarch64-pc-windows-msvc' + if ($script:cleanCalls.Count -ne 2) { throw 'Cached solver archive without build rules was reused.' } + $count++ + $env:Z3_SRC_SOURCE_DIR = $null + $env:Z3_LIBRARY_PATH_OVERRIDE = $sourceFixture + Set-Content -LiteralPath (Join-Path $sourceFixture 'libz3.dll') -Value 'legacy shared runtime' + $features = Configure-Z3 + if ($features.WorkspaceFeatures -notmatch 'vendored-z3' -or $env:Z3_LIBRARY_PATH_OVERRIDE) { throw 'Legacy shared override selected instead of static Z3.' } + $count++ + if ($env:Z3_SRC_SOURCE_DIR.StartsWith($TargetDir, [StringComparison]::OrdinalIgnoreCase)) { throw 'Source cache is inside the pruned Cargo target directory.' } + $count++ + if (-not (Test-Path -LiteralPath (Join-Path $env:Z3_SRC_SOURCE_DIR 'scripts/VERSION.txt'))) { throw 'Cold source extraction failed.' } + $count++ + $env:Z3_SRC_SOURCE_DIR = $null + $Z3SourceSha256 = '0' * 64 + # A verified cache can be reused without downloading another archive. + if (-not (Resolve-Z3Source)) { throw 'Source cache reuse failed.' } + $count++ + $RepoRoot = Join-Path $sourceFixture 'bad-checksum' + $rejected = $false + try { Resolve-Z3Source | Out-Null } catch { + if ($_.Exception.Message -notmatch 'checksum') { throw } + $rejected = $true + } + if (-not $rejected) { throw 'Unverified Z3 source archive accepted.' } + $count++ + $env:Z3_SRC_SOURCE_DIR = Split-Path $scripts + Set-Content -LiteralPath (Join-Path $scripts 'VERSION.txt') -Value '5.3.2.0' + $rejected = $false + try { Resolve-Z3Source | Out-Null } catch { $rejected = $true } + if (-not $rejected) { throw 'Wrong solver source version accepted.' } + $count++ +} finally { Remove-Item -LiteralPath $sourceFixture -Recurse -Force } +Write-Host "$count static-runtime regression checks passed. No MXC qualification claimed." diff --git a/tasks/scripts/windows-runtime.ps1 b/tasks/scripts/windows-runtime.ps1 new file mode 100644 index 0000000000..3614073acc --- /dev/null +++ b/tasks/scripts/windows-runtime.ps1 @@ -0,0 +1,90 @@ +# SPDX-FileCopyrightText: Copyright (c) 2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved. +# SPDX-License-Identifier: Apache-2.0 + +# Read PE imports without requiring dumpbin or a developer environment on the +# runtime host. Both supported Windows architectures use PE32+ images. +function Get-WindowsImageDependencies([string] $Path) { + $reader = [IO.BinaryReader]::new([IO.File]::OpenRead($Path)) + $stream = $reader.BaseStream + try { + if ($stream.Length -lt 64 -or $reader.ReadUInt16() -ne 0x5a4d) { throw "Invalid PE image: $Path" } + $stream.Position = 0x3c + $pe = $reader.ReadUInt32() + if ($pe -gt $stream.Length - 24) { throw "Invalid PE offset: $Path" } + $stream.Position = $pe + if ($reader.ReadUInt32() -ne 0x4550) { throw "Invalid PE signature: $Path" } + $machine = $reader.ReadUInt16() + $sectionCount = $reader.ReadUInt16() + $stream.Position = $pe + 20 + $optionalSize = $reader.ReadUInt16() + $optional = $pe + 24 + if ($optionalSize -lt 128 -or $optional + $optionalSize + 40 * $sectionCount -gt $stream.Length) { throw "Invalid PE headers: $Path" } + $stream.Position = $optional + if ($reader.ReadUInt16() -ne 0x20b) { throw "Expected PE32+ image: $Path" } + $stream.Position = $optional + 108 + $directoryCount = $reader.ReadUInt32() + if ($directoryCount -lt 2 -or 112 + 8 * $directoryCount -gt $optionalSize) { throw "Invalid PE directories: $Path" } + $sections = @() + for ($i = 0; $i -lt $sectionCount; $i++) { + $stream.Position = $optional + $optionalSize + 40 * $i + 8 + $sections += [pscustomobject]@{ + VirtualSize = $reader.ReadUInt32(); VirtualAddress = $reader.ReadUInt32() + RawSize = $reader.ReadUInt32(); RawOffset = $reader.ReadUInt32() + } + } + function Resolve-ImageRva([uint32] $Rva) { + foreach ($section in $sections) { + $relative = [long]$Rva - $section.VirtualAddress + if ($relative -ge 0 -and $relative -lt $section.RawSize) { + $offset = [long]$section.RawOffset + $relative + if ($offset -lt $stream.Length) { return $offset } + } + } + throw "Invalid PE RVA in $Path" + } + $dependencies = @() + foreach ($index in @(1, 13)) { + if ($directoryCount -le $index) { continue } + $stream.Position = $optional + 112 + 8 * $index + $rva = $reader.ReadUInt32() + $size = $reader.ReadUInt32() + if ($rva -eq 0) { continue } + $descriptorSize = if ($index -eq 1) { 20 } else { 32 } + $directory = Resolve-ImageRva $rva + $terminated = $false + for ($position = $directory; $position + $descriptorSize -le [Math]::Min($stream.Length, $directory + $size); $position += $descriptorSize) { + $stream.Position = $position + $fields = @(for ($j = 0; $j -lt $descriptorSize / 4; $j++) { $reader.ReadUInt32() }) + if (@($fields | Where-Object { $_ -ne 0 }).Count -eq 0) { $terminated = $true; break } + # Modern delay-import descriptors store RVAs (attribute bit 0). + if ($index -eq 13 -and ($fields[0] -band 1) -eq 0) { throw "Unsupported delay-import addressing: $Path" } + $nameRva = if ($index -eq 1) { $fields[3] } else { $fields[1] } + $stream.Position = Resolve-ImageRva $nameRva + $name = [Text.StringBuilder]::new() + do { + if ($stream.Position -ge $stream.Length -or $name.Length -gt 260) { throw "Invalid PE import name: $Path" } + $byte = $reader.ReadByte() + if ($byte) { [void]$name.Append([char]$byte) } + } while ($byte) + $dependencies += $name.ToString() + } + if (-not $terminated) { throw "Unterminated PE import directory: $Path" } + } + return [pscustomobject]@{ Machine = $machine; Dependencies = @($dependencies | Sort-Object -Unique) } + } finally { $reader.Dispose() } +} + +function Assert-StaticWindowsRuntime([string] $Path, [string] $RustTarget) { + $image = Get-WindowsImageDependencies $Path + $expected = switch ($RustTarget) { + 'aarch64-pc-windows-msvc' { 0xaa64 } + 'x86_64-pc-windows-msvc' { 0x8664 } + default { throw "Unsupported Windows target: $RustTarget" } + } + if ($image.Machine -ne $expected) { throw "PE architecture does not match ${RustTarget}: $Path" } + $dynamic = @($image.Dependencies | Where-Object { + $_ -match '^(libz3|z3|vcruntime\d+[^.]*|msvcp\d+[^.]*|msvcr\d+[^.]*|concrt\d+[^.]*|ucrtbased?|api-ms-win-crt-[^.]+)\.dll$' + }) + if ($dynamic.Count) { throw "Dynamic Z3/CRT dependency in ${Path}: $($dynamic -join ', ')" } + Write-Host "==> Static Z3/CRT and PE architecture verified: $Path" +}