diff --git a/.github/workflows/quality_windows_arm64.yml b/.github/workflows/quality_windows_arm64.yml new file mode 100644 index 000000000..e999ab68a --- /dev/null +++ b/.github/workflows/quality_windows_arm64.yml @@ -0,0 +1,95 @@ +name: Quality / Windows installer previews + +on: + push: + branches: [codex/windows-arm64] + workflow_dispatch: + +permissions: + contents: read + +concurrency: + group: windows-arm64-preview-${{ github.ref }} + cancel-in-progress: true + +jobs: + preview: + name: Windows ${{ matrix.architecture }} preview + strategy: + fail-fast: false + matrix: + include: + - architecture: arm64 + runner: windows-11-arm + rust: aarch64-pc-windows-msvc + - architecture: x64 + runner: windows-2022 + rust: x86_64-pc-windows-msvc + runs-on: ${{ matrix.runner }} + timeout-minutes: 90 + env: + FLUTTER_HOST_ARCH: ${{ matrix.architecture }} + CI: "true" + steps: + - name: Check out Ame + uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6.0.2 + with: + persist-credentials: false + + # The Windows SDK archive ships x64 Dart. The framework checkout bootstraps native Dart. + - name: Check out Flutter 3.50.0 + uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6.0.2 + with: + repository: flutter/flutter + ref: 3.50.0 + path: build/flutter-sdk + persist-credentials: false + + - name: Select the native toolchain + shell: pwsh + run: | + if ((git -C build/flutter-sdk rev-parse HEAD) -cne 'ab2912035e160d9cad6306df307576da6128ebdb') { + throw 'Unexpected Flutter 3.50.0 revision' + } + (Resolve-Path build/flutter-sdk/bin).Path >> $env:GITHUB_PATH + rustup set default-host ${{ matrix.rust }} + + - name: Build, launch and read the catalog through the Release bridge + shell: pwsh + run: ./tool/quality_windows_preview.ps1 -Architecture ${{ matrix.architecture }} + + - name: Upload portable preview + if: matrix.architecture == 'arm64' + uses: actions/upload-artifact@b7c566a772e6b6bfb58ed0dc250532a479d7789f # v6.0.0 + with: + name: Cedarflake-Ame-windows-arm64-preview + path: build/windows/arm64/runner/Release/ + if-no-files-found: error + retention-days: 14 + + - name: Package MSI with offline models + shell: pwsh + run: | + $arch = $env:FLUTTER_HOST_ARCH + $tag = 'v' + ((Select-String -LiteralPath pubspec.yaml -Pattern '^version: ([^+]+)\+' -Encoding utf8).Matches.Groups[1].Value) + $cache = Get-Content -LiteralPath "build/windows/$arch/CMakeCache.txt" -Encoding utf8 + $env:CMAKE_GENERATOR = ($cache | Select-String '^CMAKE_GENERATOR:INTERNAL=(.+)$').Matches.Groups[1].Value + $cmake = ($cache | Select-String '^CMAKE_COMMAND:INTERNAL=(.+)$').Matches.Groups[1].Value + $wix = ./tool/release_prepare_installer_toolchain.ps1 -OutputDirectory build/installer-toolchain + ./tool/release_package_installer_windows.ps1 @wix -Tag $tag -Architecture $arch ` + -BundlePath "build/windows/$arch/runner/Release" -UnsignedEvidencePath "build/windows/$arch/preview-evidence.json" ` + -CMakePath $cmake -OutputDirectory build/preview-installer -AcceptWixBinaryEula -MsiOnly + + - name: Install, repair and uninstall on Windows 11 + if: matrix.architecture == 'arm64' + shell: pwsh + run: ./tool/acceptance_windows_msi_preview.ps1 -PackageDirectory build/preview-installer + + - name: Upload verified installer + uses: actions/upload-artifact@b7c566a772e6b6bfb58ed0dc250532a479d7789f # v6.0.0 + with: + name: Cedarflake-Ame-windows-${{ matrix.architecture }}-msi + path: build/preview-installer/*.msi + compression-level: 0 + if-no-files-found: error + retention-days: 14 diff --git a/.github/workflows/release_package_windows.yml b/.github/workflows/release_package_windows.yml index 4b6f17311..788c944ae 100644 --- a/.github/workflows/release_package_windows.yml +++ b/.github/workflows/release_package_windows.yml @@ -211,6 +211,7 @@ jobs: id: installer_identity shell: pwsh env: + CMAKE_GENERATOR: Visual Studio 17 2022 AME_PORTABLE_ARCHIVE: ${{ steps.portable_identity.outputs.archive_path }} run: | $ErrorActionPreference = 'Stop' diff --git a/README.md b/README.md index 814a58bbb..5296d06a3 100644 --- a/README.md +++ b/README.md @@ -65,6 +65,23 @@ flutter pub get flutter run -d windows ``` +### Windows 11 installer previews + +The [preview workflow](.github/workflows/quality_windows_arm64.yml) builds unsigned **x64 and ARM64 +MSI installers**. Download `Cedarflake-Ame-windows-x64-msi` for Intel or AMD PCs, or +`Cedarflake-Ame-windows-arm64-msi` for Windows on Arm. Extract the MSI from the Actions artifact +and run it. Both include the offline models and the native journal service. If you previously +installed the x64 edition on an Arm PC, uninstall it before installing the ARM64 edition. +Your catalog, preferences and source files are retained. + +The `Cedarflake-Ame-windows-arm64-preview` artifact contains the ARM64 portable app. Extract +the whole ZIP and open `cedarflake_ame.exe`. The portable app downloads optional models on request. + +For a native build, use Visual Studio's ARM64 C++ tools and ARM64 Rust. Set +`$env:FLUTTER_HOST_ARCH = "arm64"` before first running Flutter from a fresh checkout of the +`3.50.0` Flutter tag. This bootstraps the ARM64 Dart SDK. Run the commands above using that SDK, +or use `flutter build windows --release` for the bundle in `build/windows/arm64/runner/Release`. + The interface is built with Flutter and Material 3. Rust handles the catalog, file tracking, previews, image analysis, and file operations, with SQLite storing the library. diff --git a/docs/architecture/0015-windows-release-distribution.md b/docs/architecture/0015-windows-release-distribution.md index 7c1f3df7a..ef4370af8 100644 --- a/docs/architecture/0015-windows-release-distribution.md +++ b/docs/architecture/0015-windows-release-distribution.md @@ -70,8 +70,13 @@ the executable's `FLUTTER_VERSION` resource. Startup diagnostics attach that ide Rust runtime or catalog cannot open. Reading it needs no platform request, catalog or startup wait. Package receipts use the matching manifest version, and release-note admission uses its tag. -Only Windows 11 x64 is supported and published. ARM packages are not produced or implied. A hosted -`windows-2025` Windows Server runner supplies reproducible build and release checks, but it does not +Tagged releases target Windows 11 x64. The [native preview workflow](../../.github/workflows/quality_windows_arm64.yml) +provides experimental x64 and ARM64 MSIs and the ARM64 portable application. Each native runner checks +the Release window and reads the catalog through its packaged Rust bridge. Both installers include +architecture-matched CPU and DirectML engines, the broker and offline models. The ARM64 MSI also +passes through fresh installation, repair and removal on a disposable Windows 11 client before upload. + +A hosted `windows-2025` Windows Server runner supplies reproducible x64 build and release checks, but it does not replace the separately authorized Windows 11 client acceptance evidence. Installer packaging uses `windows-2022` to retain the Visual Studio 2022 generator required by the native installer policy module. This packaging host does not change the supported client OS. @@ -90,9 +95,11 @@ missing ICU or application data, and missing Flutter assets. CMake's `InstallRequiredSystemLibraries` copies the selected MSVC toolchain's redistributable Release libraries beside the executable. Candidate and archive admission require `msvcp140.dll`, `msvcp140_1.dll`, `vcruntime140.dll` and `vcruntime140_1.dll`; Debug runtimes and system-directory copies are excluded. -The same four libraries accompany each CPU/GPU model runtime in its own directory, preserving +The same four libraries accompany each x64 CPU/GPU model runtime in its own directory, preserving the worker's restricted DLL search. Portable admission checks these copies against the application root's lengths and hashes while retaining the pinned engine manifest and exact directory contents. +The native ARM64 engines import `msvcp140.dll`, `msvcp140_1.dll` and `vcruntime140.dll`. +The ARM64 preview excludes the x64 `vcruntime140_1.dll` supplied for emulation. This follows Flutter's [Windows deployment guidance](https://docs.flutter.dev/platform-integration/windows/building) and Microsoft's [redistributable-file terms](https://learn.microsoft.com/en-us/cpp/windows/redistributing-visual-cpp-files). The Visual Studio build operator must hold the applicable distribution rights; the binaries retain diff --git a/docs/architecture/0027-windows-installer-lifecycle.md b/docs/architecture/0027-windows-installer-lifecycle.md index 8dbd7571a..2e8670d75 100644 --- a/docs/architecture/0027-windows-installer-lifecycle.md +++ b/docs/architecture/0027-windows-installer-lifecycle.md @@ -2,7 +2,7 @@ - Status: Accepted - Date: 2026-10-12 -- Last amended: 2026-11-16 +- Last amended: 2026-11-20 ## Context and decision drivers @@ -13,7 +13,7 @@ local builds, retain user data and restore the preceding installation if an upgr ## Decision -Use an x64, per-machine MSI built with WiX Toolset 7.0.0. Windows Installer owns file components, +Build one per-machine MSI per supported architecture using WiX Toolset 7.0.0. Windows Installer owns file components, product registration, service installation/configuration/control and transactional rollback. Distribute the MSI alongside the portable ZIP. A native WiX Burn executable using `WixInternalUIBootstrapperApplication` remains available for early elevation and cached maintenance. @@ -25,7 +25,12 @@ of the desktop application. Native actions validate protected destinations and e then own bounded service startup, its paired rollback stop and restoration of the preceding service's restricted configuration. MSI retains the installation transaction. -The upgrade identity is `C284E6B0-1484-4926-A94B-C3EC3840DBBD`. The release tag and application +The x64 upgrade identity is `C284E6B0-1484-4926-A94B-C3EC3840DBBD`. The ARM64 preview uses +`293D2430-983F-5117-BB52-64978D653436`. Each family owns its product codes. Both packages +use the same fixed broker service name and check the other family's Windows Installer registration +before mutation. A fresh install requires the native machine architecture. Switching from an +emulated x64 installation requires uninstalling it first, which retains the user's catalog. +Same-architecture upgrades and repairs use the existing transactional lifecycle. The release tag and application retain their full semantic version, including prerelease identifiers. MSI `ProductVersion` uses `major.minor.build`, with the positive numeric build suffix from Flutter's `pubspec.yaml` as its third component. For example, `0.1.0-alpha.1+1` produces MSI version `0.1.1`. Increment the build @@ -61,7 +66,7 @@ product registration and shortcut. It never owns source directories, catalogs, p user decisions or operation history. Removal of unknown files or recursive deletion of the installation directory is not part of uninstall. -Keep the broker beneath native `FOLDERID_ProgramFilesX64` in fixed `Cedarflake Ame/Journal Broker` +Keep the broker beneath the native process's `FOLDERID_ProgramFiles` in fixed `Cedarflake Ame/Journal Broker` directories. The application defaults to the sibling `Application` directory and may use another local protected installation directory selected through a native MSI path field and folder browser. Reject volume roots, overlap with the protected broker, redirected/reparse destinations, untrusted parent diff --git a/docs/architecture/0034-versioned-classification-contracts.md b/docs/architecture/0034-versioned-classification-contracts.md index fbe45047a..46e4511ff 100644 --- a/docs/architecture/0034-versioned-classification-contracts.md +++ b/docs/architecture/0034-versioned-classification-contracts.md @@ -872,9 +872,9 @@ storage is a separate rebuildable database beside the active catalog, outside th The Windows CMake dependency prepares the hash-pinned official runtime archive before the application target can complete. Its distribution manifest records archive and individual-file lengths and -SHA-256 digests, with a regression binding both DLL entries to the worker's compiled artifact pins. +SHA-256 digests. The worker reads its compiled-in target manifest for binary admission. Preparation streams at most the admitted archive length with a five-minute timeout, extracts only -the seven admitted files, verifies them and publishes a complete new build directory by rename. +the files named by that manifest, verifies them and publishes a complete new build directory by rename. Interrupted staging is disposable; an existing build directory must pass every artifact check before reuse. Corrupt existing output fails the build for explicit inspection rather than being silently trusted. The package cache lives under repository `build`; no runtime download occurs during app @@ -885,6 +885,11 @@ An upgrade replaces the bundle through the existing application packaging lifecy or digest changes require renewed adapter admission and feature compatibility; there is no fallback to a system-installed runtime or a different DLL found on the search path. Reverting a bundle restores its matching runtime without changing model files, catalogs or human decisions. +For installations recorded with Ame's `data/classification-runtime` layout, enablement and worker +startup resolve that directory beside the running executable. When the saved model selection also +belongs to the previous bundle and the current installation contains the same model package, use +the current installation's copy. A portable build without bundled models uses the saved model +directory. Custom model and runtime directories retain their stored paths. DLL dependency lookup is restricted to Windows system libraries and the pinned runtime directory; the current directory and arbitrary search-path entries are excluded inside the worker. diff --git a/docs/architecture/0039-gpu-classification-execution.md b/docs/architecture/0039-gpu-classification-execution.md index abb36c457..15ed38810 100644 --- a/docs/architecture/0039-gpu-classification-execution.md +++ b/docs/architecture/0039-gpu-classification-execution.md @@ -68,6 +68,15 @@ It does not install a system runtime. Microsoft.AI.DirectML's binary uses Micros license, not MIT; retain its supplied Windows ML distribution terms and notices. ONNX Runtime is MIT, ONNX schema and prost are Apache-2.0, and half is MIT/Apache-2.0 (used under Apache-2.0). +Both architectures obtain their CPU DLLs and upstream revision metadata from the official +Microsoft.ML.OnnxRuntime NuGet package. Its x64 DLLs match the original release ZIP byte for byte. +The packager preserves the x64 version and commit files using the NuGet metadata and verifies their +existing hashes. The Windows ARM64 preview uses the same engine versions. Its GPU DLLs come +from the ARM64 entries in the same two DirectML packages as x64. Each architecture's manifests +bind the worker pins and package contents. Switching architectures recognizes the previous run +and selects a new pipeline through the existing configuration transaction. The x64 screening +calibration applies only to its measured CPU pipeline. + The generated ONNX types come from ONNX 1.20.1's `onnx/onnx-ml.proto` descriptor. Regeneration is owned by `tool/bridge_generate_onnx_schema.ps1`; the generator pins the descriptor digest and prost version. Schema attribution and the Apache-2.0 license ship through the native-license registry. diff --git a/rust/src/adapters/classification_runtime/accelerator_distribution.arm64.json b/rust/src/adapters/classification_runtime/accelerator_distribution.arm64.json new file mode 100644 index 000000000..90065501d --- /dev/null +++ b/rust/src/adapters/classification_runtime/accelerator_distribution.arm64.json @@ -0,0 +1,88 @@ +{ + "schema": 1, + "platform": "windows-arm64", + "bundleDirectory": "data/classification-accelerator", + "archives": [ + { + "id": "onnxruntime", + "version": "1.24.4", + "url": "https://api.nuget.org/v3-flatcontainer/microsoft.ml.onnxruntime.directml/1.24.4/microsoft.ml.onnxruntime.directml.1.24.4.nupkg", + "length": 12458649, + "sha256": "57e9f11b73437bef7a309496135d4c1f96b1a8e9ddba60013fa27bfc1d788681", + "prefix": "" + }, + { + "id": "directml", + "version": "1.15.4", + "url": "https://api.nuget.org/v3-flatcontainer/microsoft.ai.directml/1.15.4/microsoft.ai.directml.1.15.4.nupkg", + "length": 202292617, + "sha256": "4e7cb7ddce8cf837a7a75dc029209b520ca0101470fcdf275c1f49736a3615b9", + "prefix": "" + } + ], + "files": [ + { + "archive": "onnxruntime", + "entry": "runtimes/win-arm64/native/onnxruntime.dll", + "name": "onnxruntime.dll", + "length": 17546784, + "sha256": "939fa0be435d253eadd9e394ee94ad6b6ac1878c83b64118bb4bda669eb0e28c" + }, + { + "archive": "onnxruntime", + "entry": "runtimes/win-arm64/native/onnxruntime_providers_shared.dll", + "name": "onnxruntime_providers_shared.dll", + "length": 21528, + "sha256": "3923290b6fc5dcd754b4d533463a9b8b46da0889d3991ef4f9b9afa17929e3e2" + }, + { + "archive": "onnxruntime", + "entry": "LICENSE", + "name": "LICENSE-ONNXRuntime.txt", + "length": 1094, + "sha256": "c250d6278f0b47a6439fb7592b08b58a55eb9f535aa49a1db63211c3f982b674" + }, + { + "archive": "onnxruntime", + "entry": "Privacy.md", + "name": "Privacy.md", + "length": 2490, + "sha256": "bfb7712b075f3f07e3ebee5dc2a296d1de32dbf71ad28663f73d1538cb6c3b08" + }, + { + "archive": "onnxruntime", + "entry": "ThirdPartyNotices.txt", + "name": "ThirdPartyNotices-ONNXRuntime.txt", + "length": 331175, + "sha256": "fb0af774b4d7cffc5b9d046f2aaeade2f37df2f80abf8033c95dfffcc77a8866" + }, + { + "archive": "directml", + "entry": "bin/arm64-win/DirectML.dll", + "name": "DirectML.dll", + "length": 18444832, + "sha256": "77b0db83ff903f2323f5caf538499d75af6038bbea23b7959f7d232d9a4ab9d4" + }, + { + "archive": "directml", + "entry": "LICENSE.txt", + "name": "LICENSE-DirectML.txt", + "length": 10439, + "sha256": "a05138e3a085ff60a44881eedfa58dccb03ecc1d7b1f6ae888418e8c2fec4b8d" + }, + { + "archive": "directml", + "entry": "LICENSE-CODE.txt", + "name": "LICENSE-CODE-DirectML.txt", + "length": 1093, + "sha256": "903df5512f7d02609fed0c780a9b704f5a3eeb6e4d84ebe42a29845c81899a3c" + }, + { + "archive": "directml", + "entry": "ThirdPartyNotices.txt", + "name": "ThirdPartyNotices-DirectML.txt", + "length": 4577, + "sha256": "2c95795c13ff48a58b6ed916f37901c23d964b5d9d601af422f17ad2172e7950" + } + ] +} diff --git a/rust/src/adapters/classification_runtime/artifacts.rs b/rust/src/adapters/classification_runtime/artifacts.rs index f8e7b9fdc..437baef9b 100644 --- a/rust/src/adapters/classification_runtime/artifacts.rs +++ b/rust/src/adapters/classification_runtime/artifacts.rs @@ -12,7 +12,7 @@ use windows_sys::Win32::Storage::FileSystem::{ }; use super::manifest::{self, Artifact}; -use super::{EncoderBackend, EncoderKind, RuntimeError, backend}; +use super::{EncoderBackend, EncoderKind, RuntimeError, binaries}; pub(super) struct ArtifactSet { pub runtime: PathBuf, @@ -33,21 +33,18 @@ impl ArtifactSet { backend: EncoderBackend, ) -> Result { let mut guards = pin_directory(models)?; + let binaries = binaries::native(); let (runtime, runtime_files) = match backend { EncoderBackend::Cpu => ( runtime.to_path_buf(), - vec![&manifest::RUNTIME, &manifest::PROVIDER], + vec![&binaries.cpu, &binaries.cpu_provider], ), EncoderBackend::DirectMl { device_id } if kind == EncoderKind::Image && device_id >= 0 => { ( runtime.with_file_name("classification-accelerator"), - vec![ - &backend::GPU_RUNTIME, - &backend::GPU_PROVIDER, - &backend::DIRECTML, - ], + vec![&binaries.gpu, &binaries.gpu_provider, &binaries.directml], ) } _ => return Err(RuntimeError::Configuration), @@ -69,7 +66,7 @@ impl ArtifactSet { None }; Ok(Self { - runtime: runtime.join(manifest::RUNTIME.name), + runtime: runtime.join(binaries.cpu.name), graph: models.join(graph.name), tokenizer, _guards: guards, diff --git a/rust/src/adapters/classification_runtime/backend.rs b/rust/src/adapters/classification_runtime/backend.rs index f3c2a66b3..f008335ca 100644 --- a/rust/src/adapters/classification_runtime/backend.rs +++ b/rust/src/adapters/classification_runtime/backend.rs @@ -9,20 +9,3 @@ pub enum EncoderBackend { device_id: i32, }, } - -pub(super) const GPU_RUNTIME: super::manifest::Artifact = super::manifest::Artifact { - name: "onnxruntime.dll", - length: 17_328_152, - digest: "e7eedec6a6f26dc39dc948276a75ef6d2bee3fff944d874ceed0bbd3b97bff40", -}; -#[cfg(any(windows, test))] -pub(super) const GPU_PROVIDER: super::manifest::Artifact = super::manifest::Artifact { - name: "onnxruntime_providers_shared.dll", - length: 22_040, - digest: "265c8daf29637cb259cac8be9f08f2cd45f3883f0f0e4949cbfddd5b4cbec3b6", -}; -pub(super) const DIRECTML: super::manifest::Artifact = super::manifest::Artifact { - name: "DirectML.dll", - length: 18_527_776, - digest: "9c9e6d822561c6c41b90e6994b3e8857cf1d66dbfb1e0c4c799c7c89b4e92da1", -}; diff --git a/rust/src/adapters/classification_runtime/binaries.rs b/rust/src/adapters/classification_runtime/binaries.rs new file mode 100644 index 000000000..be8ed3260 --- /dev/null +++ b/rust/src/adapters/classification_runtime/binaries.rs @@ -0,0 +1,77 @@ +use std::sync::LazyLock; + +use serde::Deserialize; + +use super::manifest::Artifact; + +pub(super) struct RuntimeBinaries { + pub cpu: Artifact, + pub cpu_provider: Artifact, + pub gpu: Artifact, + pub gpu_provider: Artifact, + pub directml: Artifact, +} + +pub(super) static X64: LazyLock = LazyLock::new(|| { + RuntimeBinaries::from_manifests( + include_str!("distribution.json"), + include_str!("accelerator_distribution.json"), + ) +}); +pub(super) static ARM64: LazyLock = LazyLock::new(|| { + RuntimeBinaries::from_manifests( + include_str!("distribution.arm64.json"), + include_str!("accelerator_distribution.arm64.json"), + ) +}); + +pub(super) fn native() -> &'static RuntimeBinaries { + if cfg!(target_arch = "aarch64") { + &ARM64 + } else { + &X64 + } +} + +#[derive(Deserialize)] +struct Distribution<'a> { + #[serde(borrow)] + files: Vec>, +} + +#[derive(Deserialize)] +struct FilePin<'a> { + name: &'a str, + length: u64, + sha256: &'a str, +} + +impl Distribution<'static> { + fn binary(&self, name: &'static str) -> Artifact { + let file = self + .files + .iter() + .find(|file| file.name == name) + .expect("the compiled distribution includes the required binary"); + Artifact { + name, + length: file.length, + digest: file.sha256, + } + } +} + +impl RuntimeBinaries { + fn from_manifests(cpu: &'static str, gpu: &'static str) -> Self { + let cpu: Distribution = serde_json::from_str(cpu).expect("valid bundled CPU distribution"); + let gpu: Distribution = + serde_json::from_str(gpu).expect("valid bundled DirectML distribution"); + Self { + cpu: cpu.binary("onnxruntime.dll"), + cpu_provider: cpu.binary("onnxruntime_providers_shared.dll"), + gpu: gpu.binary("onnxruntime.dll"), + gpu_provider: gpu.binary("onnxruntime_providers_shared.dll"), + directml: gpu.binary("DirectML.dll"), + } + } +} diff --git a/rust/src/adapters/classification_runtime/distribution.arm64.json b/rust/src/adapters/classification_runtime/distribution.arm64.json new file mode 100644 index 000000000..e8bc86319 --- /dev/null +++ b/rust/src/adapters/classification_runtime/distribution.arm64.json @@ -0,0 +1,50 @@ +{ + "schema": 1, + "version": "1.31.1", + "platform": "windows-arm64", + "bundleDirectory": "data/classification-runtime", + "archive": { + "url": "https://api.nuget.org/v3-flatcontainer/microsoft.ml.onnxruntime/1.31.1/microsoft.ml.onnxruntime.1.31.1.nupkg", + "length": 159626394, + "sha256": "6871ccbc97f3d5bdb4095a1dcec33aa78d56f6ed8de5980ef5fa1d9f2e7c679a", + "prefix": "" + }, + "files": [ + { + "name": "onnxruntime.dll", + "entry": "runtimes/win-arm64/native/onnxruntime.dll", + "length": 17507640, + "sha256": "3ec71b45c71ed057096f4914fddb379a38b9ec813794052b96fec7de81e2ea20" + }, + { + "name": "onnxruntime_providers_shared.dll", + "entry": "runtimes/win-arm64/native/onnxruntime_providers_shared.dll", + "length": 21344, + "sha256": "cf61a896b768c72cff1b3e603f46cf294151a274f2c72fd40fea8e648a08f0e4" + }, + { + "name": "LICENSE", + "entry": "LICENSE", + "length": 1094, + "sha256": "c250d6278f0b47a6439fb7592b08b58a55eb9f535aa49a1db63211c3f982b674" + }, + { + "name": "Privacy.md", + "entry": "Privacy.md", + "length": 6635, + "sha256": "8c26e1fc8581d9553e475250fe014e3d1e5ea4ae9f1cb084a068ff8601703dc9" + }, + { + "name": "ThirdPartyNotices.txt", + "entry": "ThirdPartyNotices.txt", + "length": 344457, + "sha256": "c53a76501ef60db6f865f20599f220761201ac4057683acefdab37d861b86622" + }, + { + "name": "Microsoft.ML.OnnxRuntime.nuspec", + "entry": "Microsoft.ML.OnnxRuntime.nuspec", + "length": 2233, + "sha256": "16161e452a09fae01ad6a9ad4b32cc15f81eafb6ffe2097d6634478c0cea5637" + } + ] +} diff --git a/rust/src/adapters/classification_runtime/distribution.json b/rust/src/adapters/classification_runtime/distribution.json index c4b84665d..e5867946f 100644 --- a/rust/src/adapters/classification_runtime/distribution.json +++ b/rust/src/adapters/classification_runtime/distribution.json @@ -4,21 +4,21 @@ "platform": "windows-x64", "bundleDirectory": "data/classification-runtime", "archive": { - "url": "https://github.com/microsoft/onnxruntime/releases/download/v1.31.1/onnxruntime-win-x64-1.31.1.zip", - "length": 82929944, - "sha256": "5869e26bb25314375f2d5ef1877e3ea06b1543d6c710e6e4a1ec2f7177ab4b2c", - "prefix": "onnxruntime-win-x64-1.31.1/" + "url": "https://api.nuget.org/v3-flatcontainer/microsoft.ml.onnxruntime/1.31.1/microsoft.ml.onnxruntime.1.31.1.nupkg", + "length": 159626394, + "sha256": "6871ccbc97f3d5bdb4095a1dcec33aa78d56f6ed8de5980ef5fa1d9f2e7c679a", + "prefix": "" }, "files": [ { "name": "onnxruntime.dll", - "entry": "lib/onnxruntime.dll", + "entry": "runtimes/win-x64/native/onnxruntime.dll", "length": 17253728, "sha256": "cb2fa518b18356617f6f9f460c012501da2819ec7e5c1d80bd8f5de20492db0c" }, { "name": "onnxruntime_providers_shared.dll", - "entry": "lib/onnxruntime_providers_shared.dll", + "entry": "runtimes/win-x64/native/onnxruntime_providers_shared.dll", "length": 21816, "sha256": "8cea20be64259950cda8643af0ed42c2a424400fc3bb9bb29f9a35cee6b3419f" }, @@ -42,13 +42,15 @@ }, { "name": "VERSION_NUMBER", - "entry": "VERSION_NUMBER", + "entry": "Microsoft.ML.OnnxRuntime.nuspec", + "nuspecField": "version", "length": 8, "sha256": "5c84dfd3de98477eafa692040c2a501dbd3b056778282985daefea6eecf9e715" }, { "name": "GIT_COMMIT_ID", - "entry": "GIT_COMMIT_ID", + "entry": "Microsoft.ML.OnnxRuntime.nuspec", + "nuspecField": "repositoryCommit", "length": 43, "sha256": "c0c2a3774d92c0afecc3f279b3ce3d36809e826cf19092c91fd07024cfdabf1e" } diff --git a/rust/src/adapters/classification_runtime/distribution_tests.rs b/rust/src/adapters/classification_runtime/distribution_tests.rs index c3e8552ef..481651546 100644 --- a/rust/src/adapters/classification_runtime/distribution_tests.rs +++ b/rust/src/adapters/classification_runtime/distribution_tests.rs @@ -1,63 +1,81 @@ -use super::manifest::{PROVIDER, RUNTIME}; +use std::collections::BTreeSet; + +use super::{EncoderBackend, binaries, manifest}; #[test] -fn bundled_runtime_matches_the_worker_artifact_contract() { - let distribution: serde_json::Value = - serde_json::from_str(include_str!("distribution.json")).unwrap(); - assert_eq!(distribution["schema"], 1); - assert_eq!(distribution["platform"], "windows-x64"); - assert_eq!( - distribution["bundleDirectory"], - "data/classification-runtime" - ); - let files = distribution["files"].as_array().unwrap(); - assert_eq!(files.len(), 7); - for artifact in [RUNTIME, PROVIDER] { - let matching: Vec<_> = files - .iter() - .filter(|file| file["name"] == artifact.name) - .collect(); - assert_eq!(matching.len(), 1); - assert_eq!(matching[0]["length"], artifact.length); - assert_eq!(matching[0]["sha256"], artifact.digest); - } - for notice in [ - "LICENSE", - "Privacy.md", - "ThirdPartyNotices.txt", - "VERSION_NUMBER", - "GIT_COMMIT_ID", +fn runtime_distributions_include_platform_binaries_and_redistribution_notices() { + for (platform, cpu, gpu, provenance) in [ + ( + "windows-x64", + include_str!("distribution.json"), + include_str!("accelerator_distribution.json"), + &["VERSION_NUMBER", "GIT_COMMIT_ID"][..], + ), + ( + "windows-arm64", + include_str!("distribution.arm64.json"), + include_str!("accelerator_distribution.arm64.json"), + &["Microsoft.ML.OnnxRuntime.nuspec"][..], + ), ] { - assert_eq!( - files.iter().filter(|file| file["name"] == notice).count(), - 1 - ); + let cpu_files = [ + "onnxruntime.dll", + "onnxruntime_providers_shared.dll", + "LICENSE", + "Privacy.md", + "ThirdPartyNotices.txt", + ]; + let gpu_files = [ + "onnxruntime.dll", + "onnxruntime_providers_shared.dll", + "DirectML.dll", + "LICENSE-ONNXRuntime.txt", + "Privacy.md", + "ThirdPartyNotices-ONNXRuntime.txt", + "LICENSE-DirectML.txt", + "LICENSE-CODE-DirectML.txt", + "ThirdPartyNotices-DirectML.txt", + ]; + for (json, directory, expected) in [ + ( + cpu, + "data/classification-runtime", + [cpu_files.as_slice(), provenance].concat(), + ), + (gpu, "data/classification-accelerator", gpu_files.to_vec()), + ] { + let distribution: serde_json::Value = serde_json::from_str(json).unwrap(); + assert_eq!(distribution["schema"], 1); + assert_eq!(distribution["platform"], platform); + assert_eq!(distribution["bundleDirectory"], directory); + let files = distribution["files"].as_array().unwrap(); + let names: BTreeSet<_> = files.iter().map(|f| f["name"].as_str().unwrap()).collect(); + assert_eq!(files.len(), names.len()); + assert_eq!(names, expected.into_iter().collect()); + } } } +#[cfg(windows)] #[test] -fn accelerator_bundle_contains_pinned_binaries_and_distribution_notices() { - use super::backend::{DIRECTML, GPU_PROVIDER, GPU_RUNTIME}; - let distribution: serde_json::Value = - serde_json::from_str(include_str!("accelerator_distribution.json")).unwrap(); - let files = distribution["files"].as_array().unwrap(); - assert_eq!(files.len(), 9); - for artifact in [GPU_RUNTIME, GPU_PROVIDER, DIRECTML] { - let file = files - .iter() - .find(|file| file["name"] == artifact.name) - .unwrap(); - assert_eq!(file["length"], artifact.length); - assert_eq!(file["sha256"], artifact.digest); - } - for name in [ - "LICENSE-ONNXRuntime.txt", - "Privacy.md", - "ThirdPartyNotices-ONNXRuntime.txt", - "LICENSE-DirectML.txt", - "LICENSE-CODE-DirectML.txt", - "ThirdPartyNotices-DirectML.txt", +fn switching_architectures_recognizes_prior_runs_and_separates_native_features() { + let factory = super::LocalClassificationEncoderFactory::default(); + for backend in [ + EncoderBackend::Cpu, + EncoderBackend::DirectMl { device_id: 0 }, ] { - assert_eq!(files.iter().filter(|file| file["name"] == name).count(), 1); + let x64 = manifest::pipeline(&binaries::X64, backend); + let arm64 = manifest::pipeline(&binaries::ARM64, backend); + assert!(factory.recognizes_model(x64)); + assert!(factory.recognizes_model(arm64)); + assert_eq!( + x64.image_identity().embedding_space, + arm64.image_identity().embedding_space + ); + assert_ne!( + x64.image_identity().pipeline, + arm64.image_identity().pipeline + ); + assert_ne!(x64.text_identity().pipeline, arm64.text_identity().pipeline); } } diff --git a/rust/src/adapters/classification_runtime/factory.rs b/rust/src/adapters/classification_runtime/factory.rs index 7590314f0..256cb87eb 100644 --- a/rust/src/adapters/classification_runtime/factory.rs +++ b/rust/src/adapters/classification_runtime/factory.rs @@ -13,7 +13,8 @@ use crate::ports::classification_encoder::{ }; use super::{ - EncoderBackend, EncoderKind, RuntimeError, WorkerConfiguration, WorkerSession, manifest, + EncoderBackend, EncoderKind, RuntimeError, WorkerConfiguration, WorkerSession, binaries, + manifest, }; #[derive(Clone, Default)] @@ -28,8 +29,13 @@ pub struct LocalClassificationEncoderFactory { impl LocalClassificationEncoderFactory { pub fn recognizes_model(&self, model: ModelCompatibility) -> bool { - model == manifest::compatibility() - || model == manifest::compatibility_for(EncoderBackend::DirectMl { device_id: 0 }) + [&*binaries::X64, &*binaries::ARM64] + .into_iter() + .any(|runtime| { + model == manifest::pipeline(runtime, EncoderBackend::Cpu) + || model + == manifest::pipeline(runtime, EncoderBackend::DirectMl { device_id: 0 }) + }) } pub fn with_gpu_preference(mut self, enabled: bool) -> Self { @@ -39,8 +45,8 @@ impl LocalClassificationEncoderFactory { .runtime_directory .with_file_name("classification-accelerator"); if enabled - && runtime.join(super::backend::GPU_RUNTIME.name).is_file() - && runtime.join(super::backend::DIRECTML.name).is_file() + && runtime.join(binaries::native().gpu.name).is_file() + && runtime.join(binaries::native().directml.name).is_file() && let Some(device) = super::device::preferred_gpu() { self.backend = EncoderBackend::DirectMl { diff --git a/rust/src/adapters/classification_runtime/installation.rs b/rust/src/adapters/classification_runtime/installation.rs index c13d22e0a..908ceacc8 100644 --- a/rust/src/adapters/classification_runtime/installation.rs +++ b/rust/src/adapters/classification_runtime/installation.rs @@ -15,6 +15,7 @@ use crate::domain::classification::installation::{ use super::RuntimeError; use super::artifacts::{pin_directory, verify_file_cancellable}; +use super::binaries; use super::factory::runtime_error; use super::manifest::{self, Artifact}; use download::{HttpsModelDownload, ModelDownload}; @@ -54,7 +55,7 @@ pub(crate) fn prepare( &model_storage.join(PACKAGE_DIRECTORY), runtime, &MODEL_FILES, - &[&manifest::RUNTIME, &manifest::PROVIDER], + &[&binaries::native().cpu, &binaries::native().cpu_provider], &mut HttpsModelDownload::new(), cancelled, progress, @@ -161,7 +162,7 @@ pub(crate) fn prepare_bundled( models, runtime, &MODEL_FILES, - &[&manifest::RUNTIME, &manifest::PROVIDER], + &[&binaries::native().cpu, &binaries::native().cpu_provider], cancelled, progress, ) diff --git a/rust/src/adapters/classification_runtime/manifest.rs b/rust/src/adapters/classification_runtime/manifest.rs index 348c10a22..0363df5ea 100644 --- a/rust/src/adapters/classification_runtime/manifest.rs +++ b/rust/src/adapters/classification_runtime/manifest.rs @@ -1,3 +1,4 @@ +use super::binaries::{self, RuntimeBinaries}; use crate::domain::classification::{ClassificationRevision, ModelCompatibility}; pub(super) const IMAGE_VALUES: usize = 3 * 224 * 224; @@ -25,22 +26,19 @@ pub(super) const TOKENIZER: Artifact = Artifact { length: 34_363_039, digest: "cb9140fae3ac5122c972d37adf83e1248471a38147ad76f8215c8872c6fd8322", }; -pub(super) const RUNTIME: Artifact = Artifact { - name: "onnxruntime.dll", - length: 17_253_728, - digest: "cb2fa518b18356617f6f9f460c012501da2819ec7e5c1d80bd8f5de20492db0c", -}; -pub(super) const PROVIDER: Artifact = Artifact { - name: "onnxruntime_providers_shared.dll", - length: 21_816, - digest: "8cea20be64259950cda8643af0ed42c2a424400fc3bb9bb29f9a35cee6b3419f", -}; pub(crate) fn compatibility() -> ModelCompatibility { compatibility_for(super::EncoderBackend::Cpu) } pub(crate) fn compatibility_for(backend: super::EncoderBackend) -> ModelCompatibility { + pipeline(binaries::native(), backend) +} + +pub(super) fn pipeline( + binaries: &RuntimeBinaries, + backend: super::EncoderBackend, +) -> ModelCompatibility { let space = ClassificationRevision::derive( "ame.classification.embedding-space.v1", [IMAGE.digest.as_bytes(), TEXT.digest.as_bytes()], @@ -50,7 +48,7 @@ pub(crate) fn compatibility_for(backend: super::EncoderBackend) -> ModelCompatib "ame.classification.image-pipeline.v1", [ IMAGE.digest.as_bytes(), - RUNTIME.digest.as_bytes(), + binaries.cpu.digest.as_bytes(), execution.as_slice(), b"image-0.25.10;first-frame;oriented-rgb8;triangle-whole-224;f32-nchw;mean-0.5;std-0.5;level-1;l2-f64", ], @@ -61,8 +59,8 @@ pub(crate) fn compatibility_for(backend: super::EncoderBackend) -> ModelCompatib "ame.classification.directml-image-pipeline.v1", [ cpu_image.bytes().as_slice(), - super::backend::GPU_RUNTIME.digest.as_bytes(), - super::backend::DIRECTML.digest.as_bytes(), + binaries.gpu.digest.as_bytes(), + binaries.directml.digest.as_bytes(), b"ort-2.0.0-rc.15;dml-1.24.4;fp32-from-fp16-v1;fixed-1x3x224x224;no-cpu-fallback", ], ), @@ -72,7 +70,7 @@ pub(crate) fn compatibility_for(backend: super::EncoderBackend) -> ModelCompatib [ TEXT.digest.as_bytes(), TOKENIZER.digest.as_bytes(), - RUNTIME.digest.as_bytes(), + binaries.cpu.digest.as_bytes(), execution.as_slice(), b"tokenizers-0.23.2;single-description;eos;pad-64;truncate-64;level-3;l2-f64", ], diff --git a/rust/src/adapters/classification_runtime/mod.rs b/rust/src/adapters/classification_runtime/mod.rs index 636d06af8..9dbf754aa 100644 --- a/rust/src/adapters/classification_runtime/mod.rs +++ b/rust/src/adapters/classification_runtime/mod.rs @@ -2,6 +2,7 @@ mod artifacts; mod backend; pub use backend::EncoderBackend; +mod binaries; #[cfg(windows)] mod device; #[cfg(test)] diff --git a/rust/src/application/classification_candidate/tests.rs b/rust/src/application/classification_candidate/tests.rs index b876852d7..dcd9956a8 100644 --- a/rust/src/application/classification_candidate/tests.rs +++ b/rust/src/application/classification_candidate/tests.rs @@ -128,9 +128,14 @@ fn custom_categories_are_usable_without_borrowed_calibration() { #[cfg(all(windows, feature = "classification-worker"))] #[test] -fn frozen_native_pipeline_matches_the_measured_candidate() { - assert_eq!( - model(), - crate::adapters::classification_runtime::selected_model_compatibility() - ); +fn only_the_measured_x64_cpu_pipeline_uses_the_frozen_screening_profile() { + let native = crate::adapters::classification_runtime::selected_model_compatibility(); + let setup = prepare(id(), initial_categories().unwrap(), native).unwrap(); + if cfg!(target_arch = "aarch64") { + assert_ne!(native, model()); + assert!(setup.refinement().is_none()); + } else { + assert_eq!(native, model()); + assert!(setup.refinement().is_some()); + } } diff --git a/rust/src/application/classification_configuration.rs b/rust/src/application/classification_configuration.rs index 10c2a9613..baa92bfbb 100644 --- a/rust/src/application/classification_configuration.rs +++ b/rust/src/application/classification_configuration.rs @@ -17,7 +17,7 @@ use super::catalog_session::open_catalog; use super::classification_candidate; use super::classification_desktop::installation_path; use super::classification_model_installation::production::{ - installation_repository, model_package, + application_data, installation_for_execution, installation_repository, model_package, }; use super::classification_runtime::while_retired; use super::storage::{StoragePaths, storage_paths}; @@ -81,14 +81,14 @@ fn save_with_storage( change: &ClassificationConfigurationChange, ) -> Result { if change.action == ClassificationConfigurationAction::Enable { - let installation = installation_repository() - .load(&installation_path(storage))? - .ok_or_else(|| { - ScanError::new( - "classification_installation_missing", - "Install the classification model before enabling analysis", - ) - })?; + let installation = + installation_for_execution(&installation_path(storage), &application_data()?)? + .ok_or_else(|| { + ScanError::new( + "classification_installation_missing", + "Install the classification model before enabling analysis", + ) + })?; model_package().verify(&installation)?; } let mut catalog = open_catalog(&storage.catalog_path, LibraryChangeLane::Recovery)?; diff --git a/rust/src/application/classification_desktop.rs b/rust/src/application/classification_desktop.rs index f5320cef7..5e5e7a867 100644 --- a/rust/src/application/classification_desktop.rs +++ b/rust/src/application/classification_desktop.rs @@ -14,7 +14,7 @@ use super::classification_dispatch::{ ClassificationDispatchSignals, ClassificationForegroundState, MonotonicDispatchClock, }; use super::classification_model_installation::production::{ - installation_repository, model_package, + installation_for_execution, installation_repository, model_package, }; use super::classification_runtime::{ClassificationRuntimePaths, NativeClassificationRuntime}; use super::{StoragePaths, storage_paths}; @@ -80,14 +80,16 @@ fn open_runtime( return Ok(None); } drop(catalog); - let installation = installation_repository() - .load(&installation_path(storage))? - .ok_or_else(|| { - ScanError::new( - "classification_installation_missing", - "Choose the local classification model and runtime directories", - ) - })?; + let installation = installation_for_execution( + &installation_path(storage), + &executable.with_file_name("data"), + )? + .ok_or_else(|| { + ScanError::new( + "classification_installation_missing", + "Choose the local classification model and runtime directories", + ) + })?; let worker = executable.with_file_name("cedarflake_ame_classification_worker.exe"); let signals = ClassificationDispatchSignals::new(MonotonicDispatchClock::default()); // The desktop supplies its current activity before allowing the first turn. diff --git a/rust/src/application/classification_model_installation.rs b/rust/src/application/classification_model_installation.rs index caa71cc2a..e5113f28f 100644 --- a/rust/src/application/classification_model_installation.rs +++ b/rust/src/application/classification_model_installation.rs @@ -17,9 +17,9 @@ static INSTALLING: AtomicBool = AtomicBool::new(false); pub(crate) mod operation; pub(super) mod production; -use production::{installation_repository, model_package, model_storage}; +use production::{application_data, installation_repository, model_package, model_storage}; mod recovery; -use recovery::{InstallationSource, PendingDownload, application_data}; +use recovery::{InstallationSource, PendingDownload}; struct InstallationOwnership; diff --git a/rust/src/application/classification_model_installation/production.rs b/rust/src/application/classification_model_installation/production.rs index 9e86bb81f..5116e355c 100644 --- a/rust/src/application/classification_model_installation/production.rs +++ b/rust/src/application/classification_model_installation/production.rs @@ -1,6 +1,7 @@ -use std::path::PathBuf; +use std::path::{Path, PathBuf}; use crate::domain::ScanError; +use crate::domain::classification::installation::ClassificationInstallation; use crate::ports::classification_acceleration::ClassificationAccelerationRepository; use crate::ports::classification_installation::ClassificationInstallationRepository; use crate::ports::classification_model_package::ClassificationModelPackage; @@ -16,6 +17,40 @@ pub(crate) fn model_package() -> impl ClassificationModelPackage { crate::adapters::classification_runtime::LocalClassificationModelPackage } +pub(in crate::application) fn application_data() -> Result { + std::env::current_exe() + .map_err(path_error)? + .parent() + .map(|parent| parent.join("data")) + .ok_or_else(|| path_error("Missing application directory")) +} + +pub(super) fn bundled_models(data: &Path) -> PathBuf { + data.join("classification-models") + .join(model_package().package_directory()) +} + +pub(in crate::application) fn installation_for_execution( + settings: &Path, + application_data: &Path, +) -> Result, ScanError> { + let mut installation = installation_repository().load(settings)?; + if let Some(selected) = &mut installation + && selected.runtime.ends_with("data/classification-runtime") + && let Some(previous_data) = selected.runtime.parent() + && previous_data != application_data + { + if selected.models == bundled_models(previous_data) { + let models = bundled_models(application_data); + if models.try_exists().map_err(path_error)? { + selected.models = models; + } + } + selected.runtime = application_data.join("classification-runtime"); + } + Ok(installation) +} + pub(super) fn model_storage() -> Result { #[cfg(debug_assertions)] if let Some(root) = std::env::var_os("CEDARFLAKE_AME_TEST_STORAGE_ROOT") { diff --git a/rust/src/application/classification_model_installation/recovery.rs b/rust/src/application/classification_model_installation/recovery.rs index 83d482d81..fde909906 100644 --- a/rust/src/application/classification_model_installation/recovery.rs +++ b/rust/src/application/classification_model_installation/recovery.rs @@ -7,7 +7,7 @@ use crate::ports::classification_model_package::ClassificationModelPackage; use super::super::classification_desktop::installation_path; use super::path_error; -use super::production::{installation_repository, model_package}; +use super::production::{application_data, bundled_models, installation_repository, model_package}; #[derive(Clone)] pub(super) enum InstallationSource { @@ -28,14 +28,6 @@ impl PendingDownload { } } -pub(super) fn application_data() -> Result { - std::env::current_exe() - .map_err(path_error)? - .parent() - .map(|parent| parent.join("data")) - .ok_or_else(|| path_error("Missing application directory")) -} - pub(super) fn pending_source() -> Result, ScanError> { let settings = installation_path(&super::super::storage_paths()?); if let Some(intent) = installation_repository() @@ -49,9 +41,7 @@ pub(super) fn pending_source() -> Result, ScanError> if installation_repository().load(&settings)?.is_some() { return Ok(None); } - let models = application_data()? - .join("classification-models") - .join(model_package().package_directory()); + let models = bundled_models(&application_data()?); if models.try_exists().map_err(path_error)? { return Ok(Some(InstallationSource::Bundled(models))); } diff --git a/rust/src/application/classification_model_installation/tests.rs b/rust/src/application/classification_model_installation/tests.rs index 1200520f7..fa9d794cb 100644 --- a/rust/src/application/classification_model_installation/tests.rs +++ b/rust/src/application/classification_model_installation/tests.rs @@ -1,5 +1,73 @@ use super::*; +#[test] +fn relocated_msi_uses_its_bundled_models_and_portable_keeps_the_saved_directory() { + let directory = tempfile::tempdir().unwrap(); + let settings = directory.path().join("settings.sqlite3"); + let old_data = directory.path().join("old-x64/data"); + let new_data = directory.path().join("new-arm64/data"); + let package = Path::new("classification-models").join(model_package().package_directory()); + let saved = ClassificationInstallation { + models: old_data.join(&package), + runtime: old_data.join("classification-runtime"), + }; + std::fs::create_dir_all(&saved.models).unwrap(); + installation_repository().save(&settings, &saved).unwrap(); + + let resolve = || { + production::installation_for_execution(&settings, &new_data) + .unwrap() + .unwrap() + }; + assert_eq!(resolve().models, saved.models); + std::fs::remove_dir(&saved.models).unwrap(); + let installed_models = new_data.join(package); + std::fs::create_dir_all(&installed_models).unwrap(); + let installed = resolve(); + assert_eq!(installed.models, installed_models); + assert_eq!(installed.runtime, new_data.join("classification-runtime")); + assert_eq!( + installation_repository().load(&settings).unwrap(), + Some(saved) + ); +} + +#[test] +fn moving_a_portable_build_uses_its_own_runtime_and_keeps_the_saved_model_selection() { + let directory = tempfile::tempdir().unwrap(); + let settings = directory.path().join("settings.sqlite3"); + let application_data = directory.path().join("arm64/data"); + let bundled = directory.path().join("x64/data/classification-runtime"); + let custom = directory.path().join("custom/classification-runtime"); + std::fs::create_dir_all( + application_data + .join("classification-models") + .join(model_package().package_directory()), + ) + .unwrap(); + for (saved, effective) in [ + (bundled, application_data.join("classification-runtime")), + (custom.clone(), custom), + ] { + let selected = ClassificationInstallation { + models: directory.path().join("models"), + runtime: saved, + }; + installation_repository() + .save(&settings, &selected) + .unwrap(); + let loaded = production::installation_for_execution(&settings, &application_data) + .unwrap() + .unwrap(); + assert_eq!(loaded.models, selected.models); + assert_eq!(loaded.runtime, effective); + assert_eq!( + installation_repository().load(&settings).unwrap(), + Some(selected) + ); + } +} + #[test] fn prepared_publication_preserves_prior_selection_on_cancel_stale_or_active_runtime() { let directory = tempfile::tempdir().unwrap(); diff --git a/rust/src/journal_broker/windows/service_identity.rs b/rust/src/journal_broker/windows/service_identity.rs index 4d803e283..3a2b91a12 100644 --- a/rust/src/journal_broker/windows/service_identity.rs +++ b/rust/src/journal_broker/windows/service_identity.rs @@ -35,9 +35,11 @@ use windows_sys::Win32::System::Services::{ use windows_sys::Win32::System::Threading::{ CREATE_NO_WINDOW, OpenProcess, PROCESS_QUERY_LIMITED_INFORMATION, QueryFullProcessImageNameW, }; -use windows_sys::Win32::UI::Shell::{ - FOLDERID_ProgramFilesX64, KF_FLAG_DEFAULT, SHGetKnownFolderPath, -}; +#[cfg(target_arch = "aarch64")] +use windows_sys::Win32::UI::Shell::FOLDERID_ProgramFiles as NATIVE_PROGRAM_FILES; +#[cfg(not(target_arch = "aarch64"))] +use windows_sys::Win32::UI::Shell::FOLDERID_ProgramFilesX64 as NATIVE_PROGRAM_FILES; +use windows_sys::Win32::UI::Shell::{KF_FLAG_DEFAULT, SHGetKnownFolderPath}; use super::process_token::ProcessToken; use super::service_host::SERVICE_NAME; @@ -465,7 +467,7 @@ fn expected_broker_path() -> Result { // CoTaskMem allocation is copied and freed exactly once below. let result = unsafe { SHGetKnownFolderPath( - &FOLDERID_ProgramFilesX64, + &NATIVE_PROGRAM_FILES, KF_FLAG_DEFAULT as u32, null_mut(), &mut raw, diff --git a/tool/acceptance_windows_msi_preview.ps1 b/tool/acceptance_windows_msi_preview.ps1 new file mode 100644 index 000000000..024127c5d --- /dev/null +++ b/tool/acceptance_windows_msi_preview.ps1 @@ -0,0 +1,64 @@ +[CmdletBinding()] +param([Parameter(Mandatory)][string]$PackageDirectory) + +$ErrorActionPreference = 'Stop' +if ($env:RUNNER_ENVIRONMENT -ne 'github-hosted' -or + (Get-CimInstance Win32_OperatingSystem).ProductType -ne 1) { + throw 'MSI lifecycle verification requires a disposable hosted Windows client' +} +$output = (Resolve-Path -LiteralPath $PackageDirectory).Path +$receipt = Get-Content -LiteralPath (Join-Path $output 'package.json') -Raw -Encoding utf8 | ConvertFrom-Json +$arch = [Runtime.InteropServices.RuntimeInformation]::OSArchitecture.ToString().ToLowerInvariant() +$msi = Join-Path $output "Cedarflake-Ame-$($receipt.releaseTag)-windows-$arch.msi" +if ($receipt.payload.machine -cne "windows-$arch" -or $receipt.databasePolicy -cne 'passed' -or + $receipt.sourceCommit -cne $env:GITHUB_SHA -or $receipt.dirtyWorktree -ne $false -or + (Get-FileHash -LiteralPath $msi).Hash -cne $receipt.packageSha256) { + throw 'MSI differs from the verified native preview' +} +$root = Join-Path ([Environment]::GetFolderPath('ProgramFiles')) 'Cedarflake Ame' +$app = Join-Path $root 'Application' +$broker = Join-Path $root 'Journal Broker/cedarflake_ame_journal_broker.exe' +$serviceName = 'CedarflakeAmeJournalBroker' +if ((Test-Path -LiteralPath $root) -or (Get-Service -Name $serviceName -ErrorAction SilentlyContinue)) { + throw 'Fresh install requires an unused product directory and service name' +} + +function Invoke-PreviewMsi([string]$Operation, [string]$Option) { + $log = Join-Path $output "$Operation.log" + $process = Start-Process msiexec.exe -ArgumentList @($Option, "`"$msi`"", '/qn', '/norestart', '/L*v', "`"$log`"") -PassThru + try { + if (-not $process.WaitForExit(300000)) { throw "$Operation exceeded five minutes" } + if ($process.ExitCode -ne 0) { + Select-String -LiteralPath $log -Pattern 'Return value 3|installation cannot continue' -Context 12,0 | Out-Host + throw "$Operation failed with Windows Installer exit code $($process.ExitCode)" + } + } finally { $process.Dispose() } +} + +function Assert-InstalledPreview { + $service = Get-CimInstance Win32_Service -Filter "Name='$serviceName'" + if ($service.StartName -ne 'LocalSystem' -or $service.StartMode -ne 'Manual' -or + $service.State -ne 'Running' -or $service.PathName.Trim('"') -ine $broker) { + throw 'Installed broker did not start with its expected identity and protected path' + } + foreach ($file in $receipt.payload.files) { + $path = Join-Path $app $file.relativePath + if ($file.relativePath -ceq 'cedarflake_ame_journal_broker.exe') { $path = $broker } + if ((Get-FileHash -LiteralPath $path).Hash -ine $file.sha256) { + throw "Installed file differs from the verified payload: $($file.relativePath)" + } + } +} + +Invoke-PreviewMsi 'install' '/i' +Assert-InstalledPreview +$repairFile = Join-Path $app 'data/icudtl.dat' +Remove-Item -LiteralPath $repairFile +Invoke-PreviewMsi 'repair' '/fa' +Assert-InstalledPreview +Invoke-PreviewMsi 'uninstall' '/x' +if ((Get-Service -Name $serviceName -ErrorAction SilentlyContinue) -or + (Test-Path -LiteralPath $broker) -or (Test-Path -LiteralPath (Join-Path $app 'cedarflake_ame.exe'))) { + throw 'Uninstall left the installed service or an executable behind' +} +Write-Host "$arch MSI installed, restored a missing file on repair, and removed its application and service." diff --git a/tool/quality_test_unsigned_windows_payload.ps1 b/tool/quality_test_unsigned_windows_payload.ps1 index e3326679c..adcacbb40 100644 --- a/tool/quality_test_unsigned_windows_payload.ps1 +++ b/tool/quality_test_unsigned_windows_payload.ps1 @@ -87,6 +87,8 @@ try { $wrongMachine[68] = 0x64; $wrongMachine[69] = 0xAA [System.IO.File]::WriteAllBytes($library, $wrongMachine) Assert-AmeUnsignedFixtureRejected { Get-AmeUnsignedWindowsPayloadFacts @arguments } 'Windows x64 PE' + Assert-AmeWindowsPeImage -Path $library -Architecture arm64 + Assert-AmeUnsignedFixtureRejected { Get-AmeUnsignedWindowsPayloadFacts @arguments -Architecture arm64 } 'Windows arm64 PE' $badOffset = [byte[]]$pe.Clone() $badOffset[0x3C] = 127 [System.IO.File]::WriteAllBytes($library, $badOffset) diff --git a/tool/quality_unsigned_windows_payload.ps1 b/tool/quality_unsigned_windows_payload.ps1 index 42208763e..f6c240c7c 100644 --- a/tool/quality_unsigned_windows_payload.ps1 +++ b/tool/quality_unsigned_windows_payload.ps1 @@ -2,6 +2,7 @@ $ErrorActionPreference = "Stop" Set-StrictMode -Version Latest . (Join-Path $PSScriptRoot "release_classification_runtime.ps1") . (Join-Path $PSScriptRoot "release_msvc_runtime.ps1") +. (Join-Path $PSScriptRoot "quality_windows_pe_image.ps1") function Assert-AmeUnsignedBuildPath { param([Parameter(Mandatory = $true)][string]$Path) @@ -18,42 +19,6 @@ function Assert-AmeUnsignedBuildPath { } } -function Assert-AmeUnsignedX64Image { - param([Parameter(Mandatory = $true)][string]$Path) - - $stream = [System.IO.File]::OpenRead($Path) - try { - $reader = [System.IO.BinaryReader]::new($stream) - try { - if ($stream.Length -lt 90 -or $reader.ReadUInt16() -ne 0x5A4D) { - throw "Unsigned runtime payload is not a complete PE image" - } - $stream.Position = 0x3C - $offset = $reader.ReadUInt32() - if ($offset -lt 64 -or $offset -gt $stream.Length - 26) { - throw "Unsigned runtime payload has an invalid PE header offset" - } - $stream.Position = $offset - if ($reader.ReadUInt32() -ne 0x4550 -or $reader.ReadUInt16() -ne 0x8664) { - throw "Unsigned runtime payload must be a Windows x64 PE image" - } - $stream.Position = $offset + 20 - $optionalHeaderSize = $reader.ReadUInt16() - if ($optionalHeaderSize -lt 2 -or $offset + 24 + $optionalHeaderSize -gt $stream.Length) { - throw "Unsigned runtime payload has an incomplete optional PE header" - } - $stream.Position = $offset + 24 - if ($reader.ReadUInt16() -ne 0x20B) { - throw "Unsigned runtime payload must use the PE32+ image format" - } - } finally { - $reader.Dispose() - } - } finally { - $stream.Dispose() - } -} - function Get-AmeUnsignedRustDependencyCount { param( [Parameter(Mandatory = $true)][string]$DependencyFile, @@ -106,7 +71,8 @@ function Get-AmeUnsignedWindowsPayloadFacts { [Parameter(Mandatory = $true)][string]$BuiltLibrary, [Parameter(Mandatory = $true)][string]$DependencyFile, [Parameter(Mandatory = $true)][string]$RequiredRustSource, - [Parameter(Mandatory = $true)][string]$BuiltBroker + [Parameter(Mandatory = $true)][string]$BuiltBroker, + [ValidateSet('x64', 'arm64')][string]$Architecture = 'x64' ) foreach ($path in @($BundlePath, $BuiltLibrary, $BuiltBroker)) { @@ -140,7 +106,7 @@ function Get-AmeUnsignedWindowsPayloadFacts { throw "Unsigned Windows payload contains a duplicate path" } if ($item.Extension -in @('.exe', '.dll')) { - Assert-AmeUnsignedX64Image $item.FullName + Assert-AmeWindowsPeImage -Path $item.FullName -Architecture $Architecture } $files.Add([pscustomobject]@{ relativePath = $relativePath @@ -154,7 +120,8 @@ function Get-AmeUnsignedWindowsPayloadFacts { 'cedarflake_ame_classification_worker.exe', 'rust_lib_cedarflake_ame.dll', 'flutter_windows.dll', 'data/app.so', 'data/icudtl.dat' - ) + @(Get-AmeClassificationRuntimePayloadNames) + @(Get-AmeMsvcRuntimePayloadPaths) + ) + @(Get-AmeClassificationRuntimePayloadNames -Architecture $Architecture) + + @(Get-AmeMsvcRuntimePayloadPaths -Architecture $Architecture) foreach ($name in $requiredFiles) { $requiredFile = @($files | Where-Object { $_.relativePath -ceq $name }) if ($requiredFile.Count -ne 1 -or $requiredFile[0].length -eq 0) { @@ -178,7 +145,7 @@ function Get-AmeUnsignedWindowsPayloadFacts { -DependencyFile $DependencyFile -BuiltLibrary $BuiltLibrary -RequiredSource $RequiredRustSource return [pscustomobject]@{ configuration = 'Release' - machine = 'windows-x64' + machine = "windows-$Architecture" fileCount = $files.Count totalBytes = $totalBytes rustDependencyCount = $dependencyCount diff --git a/tool/quality_windows_pe_image.ps1 b/tool/quality_windows_pe_image.ps1 new file mode 100644 index 000000000..9091a5aa6 --- /dev/null +++ b/tool/quality_windows_pe_image.ps1 @@ -0,0 +1,40 @@ +function Assert-AmeWindowsPeImage { + param( + [Parameter(Mandatory = $true)][string]$Path, + [Parameter(Mandatory = $true)][ValidateSet('x64', 'arm64')][string]$Architecture + ) + + $machine = if ($Architecture -eq 'arm64') { 0xAA64 } else { 0x8664 } + + $stream = [System.IO.File]::OpenRead($Path) + try { + $reader = [System.IO.BinaryReader]::new($stream) + try { + if ($stream.Length -lt 90 -or $reader.ReadUInt16() -ne 0x5A4D) { + throw "Unsigned runtime payload is not a complete PE image" + } + $stream.Position = 0x3C + $offset = $reader.ReadUInt32() + if ($offset -lt 64 -or $offset -gt $stream.Length - 26) { + throw "Unsigned runtime payload has an invalid PE header offset" + } + $stream.Position = $offset + if ($reader.ReadUInt32() -ne 0x4550 -or $reader.ReadUInt16() -ne $machine) { + throw "Unsigned runtime payload must be a Windows $Architecture PE image: $Path" + } + $stream.Position = $offset + 20 + $optionalHeaderSize = $reader.ReadUInt16() + if ($optionalHeaderSize -lt 2 -or $offset + 24 + $optionalHeaderSize -gt $stream.Length) { + throw "Unsigned runtime payload has an incomplete optional PE header" + } + $stream.Position = $offset + 24 + if ($reader.ReadUInt16() -ne 0x20B) { + throw "Unsigned runtime payload must use the PE32+ image format" + } + } finally { + $reader.Dispose() + } + } finally { + $stream.Dispose() + } +} diff --git a/tool/quality_windows_preview.ps1 b/tool/quality_windows_preview.ps1 new file mode 100644 index 000000000..a917812bc --- /dev/null +++ b/tool/quality_windows_preview.ps1 @@ -0,0 +1,83 @@ +[CmdletBinding()] +param([Parameter(Mandatory)][ValidateSet('x64', 'arm64')][string]$Architecture) + +$ErrorActionPreference = 'Stop' +. (Join-Path $PSScriptRoot 'quality_common.ps1') +. (Join-Path $PSScriptRoot 'quality_unsigned_windows_payload.ps1') + +if ([Runtime.InteropServices.RuntimeInformation]::OSArchitecture.ToString() -ine $Architecture -or + [Runtime.InteropServices.RuntimeInformation]::ProcessArchitecture.ToString() -ine $Architecture -or + [Environment]::OSVersion.Platform -ne [PlatformID]::Win32NT) { + throw "Run the preview check in native PowerShell on Windows $Architecture" +} +if ($env:RUNNER_ENVIRONMENT -ne 'github-hosted') { + throw 'The Release startup check requires a disposable hosted user profile' +} +$root = Get-AmeRepositoryRoot +$toolchain = Get-AmeToolchain +$target = if ($Architecture -eq 'arm64') { 'aarch64-pc-windows-msvc' } else { 'x86_64-pc-windows-msvc' } +$bundle = Join-Path $root "build/windows/$Architecture/runner/Release" +$built = Join-Path $root "build/windows/$Architecture/plugins/rust_lib_cedarflake_ame/cargokit_build/$target/release" +$broker = Join-Path $built 'cedarflake_ame_journal_broker.exe' +$lock = Enter-AmeRepositoryToolLock +Push-Location $root +try { + $commit = (& $toolchain.Git rev-parse --verify HEAD).Trim() + if ($LASTEXITCODE -ne 0 -or $commit -cne $env:GITHUB_SHA) { throw 'Preview requires the workflow commit' } + $changes = @(& $toolchain.Git status --porcelain) + if ($LASTEXITCODE -ne 0 -or $changes.Count -ne 0) { throw 'Preview requires a clean checkout' } + Invoke-AmeChecked $toolchain.Flutter @('--version') + Invoke-AmeChecked $toolchain.Flutter @('pub', 'get', '--enforce-lockfile') + Invoke-AmeChecked $toolchain.Flutter @('build', 'windows', '--release', '--no-pub') + Assert-AmeUnsignedBuildPath $broker + $bundledBroker = Join-Path $bundle 'cedarflake_ame_journal_broker.exe' + Assert-AmeUnsignedBuildPath $bundledBroker + Copy-Item -LiteralPath $broker -Destination $bundledBroker -Force + $facts = Get-AmeUnsignedWindowsPayloadFacts -Architecture $Architecture -BundlePath $bundle ` + -BuiltLibrary (Join-Path $built 'rust_lib_cedarflake_ame.dll') ` + -DependencyFile (Join-Path $built 'rust_lib_cedarflake_ame.d') ` + -RequiredRustSource (Join-Path $root 'rust/src/lib.rs') -BuiltBroker $broker + $crt = @(Get-AmeMsvcRuntimeCompanionFiles -BundlePath $bundle -Architecture $Architecture) + foreach ($accelerator in @($false, $true)) { + $manifest = Get-AmeClassificationRuntimeManifest -Architecture $Architecture -Accelerator:$accelerator + Assert-AmeClassificationRuntimeDirectory (Join-Path $bundle $manifest.bundleDirectory) $manifest $crt + } + Write-Host "Verified $($facts.fileCount) Release files for $($facts.machine)" + + # The runner shows its window after Flutter's first frame. + $app = Start-Process -FilePath (Join-Path $bundle 'cedarflake_ame.exe') -WorkingDirectory $bundle -PassThru + try { + $elapsed = [Diagnostics.Stopwatch]::StartNew() + do { + Start-Sleep -Milliseconds 250 + $app.Refresh() + if ($app.HasExited) { throw "Ame exited before showing its window (exit $($app.ExitCode))" } + if ($elapsed.Elapsed.TotalSeconds -ge 60) { throw 'Ame did not show its first window within 60 seconds' } + } until ($app.MainWindowHandle -ne 0 -and $app.MainWindowTitle -eq 'Cedarflake Ame') + Write-Host "$Architecture desktop window appeared in $($elapsed.ElapsedMilliseconds) ms" + if (-not $app.CloseMainWindow() -or -not $app.WaitForExit(30000) -or $app.ExitCode -ne 0) { + throw 'The application did not exit cleanly after closing its window' + } + } finally { + if (-not $app.HasExited) { $app.Kill(); $app.WaitForExit() } + $app.Dispose() + } + $aliasPath = Join-Path $env:RUNNER_TEMP "ame-$Architecture-release-smoke-$PID.dll" + $previousLibrary = $env:CEDARFLAKE_AME_TEST_LIBRARY_PATH + Copy-Item -LiteralPath (Join-Path $bundle 'rust_lib_cedarflake_ame.dll') -Destination $aliasPath -ErrorAction Stop + try { + $env:CEDARFLAKE_AME_TEST_LIBRARY_PATH = $aliasPath + Invoke-AmeChecked $toolchain.Flutter @('test', 'integration_test/release_bridge_smoke_test.dart', '-d', 'windows', '--no-pub') + } finally { + $env:CEDARFLAKE_AME_TEST_LIBRARY_PATH = $previousLibrary + Remove-Item -LiteralPath $aliasPath + } + $evidence = [ordered]@{ + format = 'cedarflake-ame-windows-preview-v1'; status = 'passed'; sourceCommit = $commit + dirtyWorktree = $false; windowClosed = $true; bridgeSmoke = 'passed_catalog_read'; payload = $facts + } + $evidence | ConvertTo-Json -Depth 8 | Set-Content -Encoding utf8 "build/windows/$Architecture/preview-evidence.json" +} finally { + Pop-Location + Exit-AmeRepositoryToolLock $lock +} diff --git a/tool/release_classification_runtime.ps1 b/tool/release_classification_runtime.ps1 index f227e54df..9cc5e8cf2 100644 --- a/tool/release_classification_runtime.ps1 +++ b/tool/release_classification_runtime.ps1 @@ -2,15 +2,19 @@ $ErrorActionPreference = "Stop" Set-StrictMode -Version Latest function Get-AmeClassificationRuntimeManifest { - param([switch]$Accelerator) - $name = if ($Accelerator) { "accelerator_distribution.json" } else { "distribution.json" } - $path = Join-Path (Split-Path -Parent $PSScriptRoot) "rust/src/adapters/classification_runtime/$name" - return [IO.File]::ReadAllText($path, [Text.Encoding]::UTF8) | ConvertFrom-Json + param([switch]$Accelerator, [ValidateSet('x64', 'arm64')][string]$Architecture = 'x64') + $name = if ($Accelerator) { 'accelerator_distribution' } else { 'distribution' } + if ($Architecture -eq 'arm64') { $name += '.arm64' } + $path = Join-Path (Split-Path -Parent $PSScriptRoot) "rust/src/adapters/classification_runtime/$name.json" + $manifest = [IO.File]::ReadAllText($path, [Text.Encoding]::UTF8) | ConvertFrom-Json + if ($manifest.platform -ne "windows-$Architecture") { throw "Unexpected runtime platform in $name.json" } + return $manifest } function Get-AmeClassificationRuntimePayloadNames { + param([ValidateSet('x64', 'arm64')][string]$Architecture = 'x64') foreach ($accelerator in @($false, $true)) { - $manifest = Get-AmeClassificationRuntimeManifest -Accelerator:$accelerator + $manifest = Get-AmeClassificationRuntimeManifest -Accelerator:$accelerator -Architecture $Architecture foreach ($file in $manifest.files) { "$($manifest.bundleDirectory)/$($file.name)" } @@ -65,6 +69,29 @@ function Assert-AmeClassificationRuntimeDirectory { } } +function Write-AmeRuntimeProvenance { + param($Entry, $File, [string]$Path) + + if ($Entry.Length -le 0 -or $Entry.Length -gt 16KB) { throw 'Unexpected NuGet package metadata size' } + $stream = $Entry.Open() + try { + $reader = [Xml.XmlReader]::Create($stream) + try { + $document = [Xml.XmlDocument]::new() + $document.Load($reader) + } finally { $reader.Dispose() } + } finally { $stream.Dispose() } + # Retain the exact x64 provenance files shipped by the original upstream ZIP. + $metadata = $document.package.metadata + $value = switch -CaseSensitive ($File.nuspecField) { + 'version' { "$($metadata.version)`r`n" } + 'repositoryCommit' { "$($metadata.repository.commit) `r`n" } + default { throw 'Unknown runtime provenance field' } + } + [IO.File]::WriteAllText($Path, $value, [Text.UTF8Encoding]::new($false)) + Assert-AmeClassificationRuntimeArtifact $Path $File +} + function Expand-AmeClassificationRuntimeArchive { param( [Parameter(Mandatory = $true)][string]$ArchivePath, @@ -85,10 +112,15 @@ function Expand-AmeClassificationRuntimeArchive { foreach ($file in $Manifest.files) { $entryName = $Manifest.archive.prefix + $file.entry $entries = @($archive.Entries | Where-Object { $_.FullName -ceq $entryName }) - if ($entries.Count -ne 1 -or $entries[0].Length -ne $file.length) { + $provenance = $null -ne $file.PSObject.Properties['nuspecField'] + if ($entries.Count -ne 1 -or (-not $provenance -and $entries[0].Length -ne $file.length)) { throw "Runtime archive has a missing, duplicate or wrong-sized entry: $entryName" } $path = Join-Path $Destination $file.name + if ($provenance) { + Write-AmeRuntimeProvenance $entries[0] $file $path + continue + } $inputStream = $null $outputStream = $null try { diff --git a/tool/release_installer_broker_runtime.ps1 b/tool/release_installer_broker_runtime.ps1 index 0f98f5fa8..fd300917e 100644 --- a/tool/release_installer_broker_runtime.ps1 +++ b/tool/release_installer_broker_runtime.ps1 @@ -5,16 +5,17 @@ Set-StrictMode -Version Latest function Add-AmeInstallerBrokerRuntime { param( [Parameter(Mandatory = $true)][Xml.XmlElement]$Fragment, - [Parameter(Mandatory = $true)][string]$BundlePath + [Parameter(Mandatory = $true)][string]$BundlePath, + [ValidateSet('x64', 'arm64')][string]$Architecture = 'x64' ) $directory = Add-AmeInstallerXmlElement $Fragment 'DirectoryRef' @{ Id = 'AmeBrokerFolder' } $group = Add-AmeInstallerXmlElement $Fragment 'ComponentGroup' @{ Id = 'AmeBrokerRuntimeFiles' } $brokerSid = 'S-1-5-80-2098581772-366539847-2170527201-1913264099-3006267874' - foreach ($name in (Get-AmeMsvcRuntimePayloadNames)) { + foreach ($name in (Get-AmeMsvcRuntimePayloadNames -Architecture $Architecture)) { $id = 'AmeBrokerRuntime_' + $name.Replace('.', '_') $component = Add-AmeInstallerXmlElement $directory 'Component' @{ - Id = $id; Guid = Get-AmeInstallerGuid "broker-runtime/$name"; Bitness = 'always64' + Id = $id; Guid = Get-AmeInstallerGuid "broker-runtime/$name" $Architecture; Bitness = 'always64' } $file = Add-AmeInstallerXmlElement $component 'File' @{ Id = $id + '_file'; Source = Join-Path $BundlePath $name; KeyPath = 'yes' diff --git a/tool/release_installer_identity.ps1 b/tool/release_installer_identity.ps1 new file mode 100644 index 000000000..4e9017795 --- /dev/null +++ b/tool/release_installer_identity.ps1 @@ -0,0 +1,23 @@ +$script:AmeInstallerFamilies = Get-Content -LiteralPath ( + Join-Path $PSScriptRoot '../windows/installer/product_families.json' +) -Encoding utf8 -Raw | ConvertFrom-Json + +function Get-AmeInstallerGuid { + param( + [Parameter(Mandatory = $true)][string]$Name, + [ValidateSet('x64', 'arm64')][string]$Architecture = 'x64' + ) + + $namespaceBytes = ([guid]$script:AmeInstallerFamilies.$Architecture).ToByteArray() + [Array]::Reverse($namespaceBytes, 0, 4) + [Array]::Reverse($namespaceBytes, 4, 2) + [Array]::Reverse($namespaceBytes, 6, 2) + $nameBytes = [Text.Encoding]::UTF8.GetBytes($Name) + $sha1 = [Security.Cryptography.SHA1]::Create() + try { $digest = $sha1.ComputeHash($namespaceBytes + $nameBytes) } finally { $sha1.Dispose() } + $digest[6] = ($digest[6] -band 0x0F) -bor 0x50 + $digest[8] = ($digest[8] -band 0x3F) -bor 0x80 + $hex = -join @($digest[0..15] | ForEach-Object { $_.ToString("X2") }) + return "{0}-{1}-{2}-{3}-{4}" -f $hex.Substring(0, 8), $hex.Substring(8, 4), + $hex.Substring(12, 4), $hex.Substring(16, 4), $hex.Substring(20, 12) +} diff --git a/tool/release_installer_layout.ps1 b/tool/release_installer_layout.ps1 index 9ab7023ed..a3a171eff 100644 --- a/tool/release_installer_layout.ps1 +++ b/tool/release_installer_layout.ps1 @@ -3,7 +3,8 @@ Set-StrictMode -Version Latest . (Join-Path $PSScriptRoot 'release_msvc_runtime.ps1') function Assert-AmeInstallerLayout { - param([Parameter(Mandatory = $true)]$Model, [Parameter(Mandatory = $true)]$Receipt) + param([Parameter(Mandatory = $true)]$Model, [Parameter(Mandatory = $true)]$Receipt, + [ValidateSet('x64', 'arm64')][string]$Architecture = 'x64') function Require([bool]$Condition, [string]$Reason) { if (-not $Condition) { throw "Installer policy: $Reason" } @@ -20,7 +21,7 @@ function Assert-AmeInstallerLayout { foreach ($row in $Model.Component) { $attributes = '256' if ($row.Component -cin @('AmeInstallationSettings', 'AmeStartMenu', 'AmeDesktop')) { $attributes = '260' } - Require ($row.Attributes -ceq $attributes -and $row.Condition -ceq '') 'conditional or non-x64 component' + Require ($row.Attributes -ceq $attributes -and $row.Condition -ceq '') 'conditional or non-64-bit component' $directory = $row.Directory_ $depth = 0 while ($directory -cne 'AmeProductFolder') { @@ -49,7 +50,7 @@ function Assert-AmeInstallerLayout { } $payload = @{} foreach ($file in $Receipt.payload.files) { $payload[$file.relativePath] = $file } - $brokerRuntime = @(Get-AmeMsvcRuntimePayloadNames) + $brokerRuntime = @(Get-AmeMsvcRuntimePayloadNames -Architecture $Architecture) Require ($Model.File.Count -eq $payload.Count + $brokerRuntime.Count) 'package file count differs from verified payload' $installed = [Collections.Generic.HashSet[string]]::new([StringComparer]::OrdinalIgnoreCase) foreach ($file in $Model.File) { diff --git a/tool/release_installer_payload.ps1 b/tool/release_installer_payload.ps1 index 498417dc9..62445725e 100644 --- a/tool/release_installer_payload.ps1 +++ b/tool/release_installer_payload.ps1 @@ -3,33 +3,17 @@ Set-StrictMode -Version Latest . (Join-Path $PSScriptRoot "release_classification_runtime.ps1") . (Join-Path $PSScriptRoot "release_classification_models.ps1") . (Join-Path $PSScriptRoot "release_installer_version.ps1") +. (Join-Path $PSScriptRoot "release_installer_identity.ps1") . (Join-Path $PSScriptRoot "release_installer_broker_runtime.ps1") -$script:AmeInstallerUpgradeCode = "C284E6B0-1484-4926-A94B-C3EC3840DBBD" $script:AmeInstallerNamespace = "http://wixtoolset.org/schemas/v4/wxs" $script:AmeInstallerDirectorySddl = "O:BAG:BAD:P(A;OICI;FA;;;SY)(A;OICI;FA;;;BA)(A;OICI;0x1200A9;;;BU)" $script:AmeInstallerFileSddl = "O:BAG:BAD:P(A;;FA;;;SY)(A;;FA;;;BA)(A;;0x1200A9;;;BU)" -function Get-AmeInstallerGuid { - param([Parameter(Mandatory = $true)][string]$Name) - - $namespaceBytes = ([guid]$script:AmeInstallerUpgradeCode).ToByteArray() - [Array]::Reverse($namespaceBytes, 0, 4) - [Array]::Reverse($namespaceBytes, 4, 2) - [Array]::Reverse($namespaceBytes, 6, 2) - $nameBytes = [Text.Encoding]::UTF8.GetBytes($Name) - $sha1 = [Security.Cryptography.SHA1]::Create() - try { $digest = $sha1.ComputeHash($namespaceBytes + $nameBytes) } finally { $sha1.Dispose() } - $digest[6] = ($digest[6] -band 0x0F) -bor 0x50 - $digest[8] = ($digest[8] -band 0x3F) -bor 0x80 - $hex = -join @($digest[0..15] | ForEach-Object { $_.ToString("X2") }) - return "{0}-{1}-{2}-{3}-{4}" -f $hex.Substring(0, 8), $hex.Substring(8, 4), - $hex.Substring(12, 4), $hex.Substring(16, 4), $hex.Substring(20, 12) -} - function Assert-AmeInstallerPayloadNames { param([Parameter(Mandatory = $true)][object[]]$Files, [bool]$RequireModels = $true, - [object]$ModelManifest = (Get-AmeClassificationModelsManifest)) + [object]$ModelManifest = (Get-AmeClassificationModelsManifest), + [ValidateSet('x64', 'arm64')][string]$Architecture = 'x64') if ($Files.Count -eq 0 -or $Files.Count -gt 4096) { throw "Installer payload must contain between 1 and 4096 files" @@ -54,7 +38,8 @@ function Assert-AmeInstallerPayloadNames { "cedarflake_ame.exe", "cedarflake_ame_journal_broker.exe", "cedarflake_ame_classification_worker.exe", "rust_lib_cedarflake_ame.dll", "flutter_windows.dll", "data/app.so", "data/icudtl.dat" - ) + @(Get-AmeClassificationRuntimePayloadNames) + @(Get-AmeMsvcRuntimePayloadPaths) + ) + @(Get-AmeClassificationRuntimePayloadNames -Architecture $Architecture) + + @(Get-AmeMsvcRuntimePayloadPaths -Architecture $Architecture) if ($RequireModels) { $requiredFiles += @(Get-AmeClassificationModelsPayloadNames $ModelManifest) } foreach ($required in $requiredFiles) { if (-not $names.Contains($required)) { throw "Installer payload is missing $required" } @@ -85,15 +70,16 @@ function New-AmeInstallerApplicationSource { [Parameter(Mandatory = $true)][string]$BundlePath, [Parameter(Mandatory = $true)][string]$OutputPath, [string]$ModelDirectory, - [object]$ModelManifest = (Get-AmeClassificationModelsManifest) + [object]$ModelManifest = (Get-AmeClassificationModelsManifest), + [ValidateSet('x64', 'arm64')][string]$Architecture = 'x64' ) - Assert-AmeInstallerPayloadNames -Files $Files -ModelManifest $ModelManifest + Assert-AmeInstallerPayloadNames -Files $Files -ModelManifest $ModelManifest -Architecture $Architecture $document = [Xml.XmlDocument]::new() $root = $document.CreateElement("Wix", $script:AmeInstallerNamespace) [void]$document.AppendChild($root) $fragment = Add-AmeInstallerXmlElement $root "Fragment" - Add-AmeInstallerBrokerRuntime -Fragment $fragment -BundlePath $BundlePath + Add-AmeInstallerBrokerRuntime -Fragment $fragment -BundlePath $BundlePath -Architecture $Architecture $directory = Add-AmeInstallerXmlElement $fragment "DirectoryRef" @{ Id = "AMEAPPDIR" } $group = Add-AmeInstallerXmlElement $fragment "ComponentGroup" @{ Id = "AmeApplicationFiles" } $directories = @{ "" = $directory } @@ -130,7 +116,7 @@ function New-AmeInstallerApplicationSource { if ($separator -ge 0) { $parent = $relative.Substring(0, $separator) } $id = "file_" + (Get-AmeInstallerGuid "file/$relative").Replace("-", "") $component = Add-AmeInstallerXmlElement $directories[$parent] "Component" @{ - Id = "component_$id"; Guid = Get-AmeInstallerGuid "component/$relative"; Bitness = "always64" + Id = "component_$id"; Guid = Get-AmeInstallerGuid "component/$relative" $Architecture; Bitness = "always64" } $fileId = $id if ($relative -ceq "cedarflake_ame.exe") { $fileId = "AmeApplicationExecutable" } diff --git a/tool/release_installer_policy.ps1 b/tool/release_installer_policy.ps1 index e735c98f0..fbad4515b 100644 --- a/tool/release_installer_policy.ps1 +++ b/tool/release_installer_policy.ps1 @@ -1,6 +1,7 @@ $ErrorActionPreference = "Stop" Set-StrictMode -Version Latest . (Join-Path $PSScriptRoot 'release_installer_layout.ps1') +. (Join-Path $PSScriptRoot 'release_installer_identity.ps1') . (Join-Path $PSScriptRoot 'release_installer_ui.ps1') function Assert-AmeInstallerPolicy { @@ -12,9 +13,14 @@ function Assert-AmeInstallerPolicy { $properties = @{} foreach ($row in $Model.Property) { $properties[$row.Property] = $row.Value } - Require ($Model.Template -ceq 'x64;2052') 'package must target Windows x64 with Chinese UI' - Require ($properties.ProductCode -ceq ('{' + $Receipt.productCode + '}') -and - $properties.UpgradeCode -ceq '{C284E6B0-1484-4926-A94B-C3EC3840DBBD}' -and + Require ($Receipt.payload.machine -cin @('windows-x64', 'windows-arm64')) 'unknown payload architecture' + $architecture = $Receipt.payload.machine.Substring(8) + $template = if ($architecture -eq 'arm64') { 'Arm64;2052' } else { 'x64;2052' } + Require ($Model.Template -ceq $template) 'package platform or language differs from the verified payload' + $family = $script:AmeInstallerFamilies.$architecture + Require ($Receipt.productCode -ceq (Get-AmeInstallerGuid "product/$($Receipt.productVersion)" $architecture) -and + $properties.ProductCode -ceq ('{' + $Receipt.productCode + '}') -and + $Receipt.upgradeCode -ceq $family -and $properties.UpgradeCode -ceq ('{' + $family + '}') -and $properties.ProductVersion -ceq $Receipt.productVersion -and $properties.ALLUSERS -ceq '1') 'product identity changed' Require ($properties.REBOOT -ceq 'ReallySuppress' -and $properties.MSIRESTARTMANAGERCONTROL -ceq 'Disable' -and @@ -107,6 +113,6 @@ function Assert-AmeInstallerPolicy { $newer[0].Attributes -ceq '2') 'version upgrade policy changed' Require (@($Model.LaunchCondition | Where-Object { $_.Condition -ceq 'NOT WIX_DOWNGRADE_DETECTED' }).Count -eq 1) 'downgrade rejection missing' - Assert-AmeInstallerLayout $Model $Receipt + Assert-AmeInstallerLayout $Model $Receipt $architecture Assert-AmeInstallerUi $Model } diff --git a/tool/release_installer_source.ps1 b/tool/release_installer_source.ps1 index 45f700321..a5db35fb8 100644 --- a/tool/release_installer_source.ps1 +++ b/tool/release_installer_source.ps1 @@ -7,6 +7,7 @@ function Get-AmeInstallerSource { param( [Parameter(Mandatory = $true, ParameterSetName = 'Quality')][string]$BundlePath, [Parameter(Mandatory = $true, ParameterSetName = 'Quality')][string]$UnsignedEvidencePath, + [Parameter(ParameterSetName = 'Quality')][ValidateSet('x64', 'arm64')][string]$Architecture = 'x64', [Parameter(Mandatory = $true, ParameterSetName = 'Candidate')][string]$CandidateRoot, [Parameter(Mandatory = $true, ParameterSetName = 'Candidate')][string]$Tag, [Parameter(Mandatory = $true, ParameterSetName = 'Candidate')][string]$SourceCommit, @@ -33,9 +34,12 @@ function Get-AmeInstallerSource { Assert-AmeUnsignedBuildPath $BundlePath Assert-AmeUnsignedBuildPath $UnsignedEvidencePath $evidence = Get-Content -LiteralPath $UnsignedEvidencePath -Raw -Encoding utf8 | ConvertFrom-Json - if ($evidence.format -cne 'cedarflake-ame-unsigned-quality-v1' -or $evidence.status -cne 'passed' -or - $evidence.payload.machine -cne 'windows-x64' -or $evidence.payload.configuration -cne 'Release' -or - $evidence.bridgeSmoke -cne 'passed_without_catalog_access' -or + $quality = $Architecture -eq 'x64' -and $evidence.format -ceq 'cedarflake-ame-unsigned-quality-v1' -and + $evidence.bridgeSmoke -ceq 'passed_without_catalog_access' + $preview = $evidence.format -ceq 'cedarflake-ame-windows-preview-v1' -and + $evidence.bridgeSmoke -ceq 'passed_catalog_read' -and $evidence.windowClosed -eq $true + if (-not ($quality -or $preview) -or $evidence.status -cne 'passed' -or + $evidence.payload.machine -cne "windows-$Architecture" -or $evidence.payload.configuration -cne 'Release' -or @($evidence.payload.files).Count -ne $evidence.payload.fileCount) { throw 'Installer requires complete unsigned Release payload verification' } diff --git a/tool/release_installer_toolchain.ps1 b/tool/release_installer_toolchain.ps1 index 17da900d3..9e943cf9c 100644 --- a/tool/release_installer_toolchain.ps1 +++ b/tool/release_installer_toolchain.ps1 @@ -59,7 +59,8 @@ function Get-AmeInstallerWixTool { Assert-AmeInstallerWixPackage -PackagePath $PackagePath -Directory $SdkDirectory ` -ExpectedHash $script:AmeInstallerWixPackages.Sdk.Sha256 - $executable = Join-Path $SdkDirectory "tools/net472/x64/wix.exe" + $hostArch = [Runtime.InteropServices.RuntimeInformation]::ProcessArchitecture.ToString().ToLowerInvariant() + $executable = Join-Path $SdkDirectory "tools/net472/$hostArch/wix.exe" $version = @(& $executable --version) if ($LASTEXITCODE -ne 0 -or $version.Count -ne 1 -or $version[0] -cne "7.0.0+b8977d6") { throw "WiX compiler version does not match the pinned toolchain" diff --git a/tool/release_msvc_runtime.ps1 b/tool/release_msvc_runtime.ps1 index 373ee94b3..d14618d37 100644 --- a/tool/release_msvc_runtime.ps1 +++ b/tool/release_msvc_runtime.ps1 @@ -3,27 +3,35 @@ Set-StrictMode -Version Latest . (Join-Path $PSScriptRoot 'release_classification_runtime.ps1') function Get-AmeMsvcRuntimePayloadNames { + param([ValidateSet('x64', 'arm64')][string]$Architecture = 'x64') + if ($Architecture -eq 'arm64') { return @('msvcp140.dll', 'vcruntime140.dll') } return @('msvcp140.dll', 'vcruntime140.dll', 'vcruntime140_1.dll') } function Get-AmeModelMsvcRuntimePayloadNames { - return @(Get-AmeMsvcRuntimePayloadNames) + @('msvcp140_1.dll') + param([ValidateSet('x64', 'arm64')][string]$Architecture = 'x64') + return @(Get-AmeMsvcRuntimePayloadNames -Architecture $Architecture) + @('msvcp140_1.dll') } function Get-AmeMsvcRuntimePayloadPaths { - Get-AmeModelMsvcRuntimePayloadNames + param([ValidateSet('x64', 'arm64')][string]$Architecture = 'x64') + $names = Get-AmeModelMsvcRuntimePayloadNames -Architecture $Architecture + $names foreach ($accelerator in @($false, $true)) { - $manifest = Get-AmeClassificationRuntimeManifest -Accelerator:$accelerator - foreach ($name in Get-AmeModelMsvcRuntimePayloadNames) { + $manifest = Get-AmeClassificationRuntimeManifest -Accelerator:$accelerator -Architecture $Architecture + foreach ($name in $names) { "$($manifest.bundleDirectory)/$name" } } } function Get-AmeMsvcRuntimeCompanionFiles { - param([Parameter(Mandatory = $true)][string]$BundlePath) + param( + [Parameter(Mandatory = $true)][string]$BundlePath, + [ValidateSet('x64', 'arm64')][string]$Architecture = 'x64' + ) - foreach ($name in Get-AmeModelMsvcRuntimePayloadNames) { + foreach ($name in Get-AmeModelMsvcRuntimePayloadNames -Architecture $Architecture) { $path = Join-Path $BundlePath $name Assert-AmeClassificationRuntimeLocalPath $path $file = Get-Item -LiteralPath $path diff --git a/tool/release_package_installer_windows.ps1 b/tool/release_package_installer_windows.ps1 index 9978929da..6be4d5506 100644 --- a/tool/release_package_installer_windows.ps1 +++ b/tool/release_package_installer_windows.ps1 @@ -3,6 +3,7 @@ param( [Parameter(Mandatory = $true)][string]$Tag, [Parameter(Mandatory = $true, ParameterSetName = 'Quality')][string]$BundlePath, [Parameter(Mandatory = $true, ParameterSetName = 'Quality')][string]$UnsignedEvidencePath, + [Parameter(ParameterSetName = 'Quality')][ValidateSet('x64', 'arm64')][string]$Architecture = 'x64', [Parameter(Mandatory = $true, ParameterSetName = 'Candidate')][string]$CandidateRoot, [Parameter(Mandatory = $true, ParameterSetName = 'Candidate')][string]$SourceCommit, [Parameter(ParameterSetName = 'Candidate')][ValidateSet('unsigned', 'signed')][string]$SigningMode = 'unsigned', @@ -16,6 +17,7 @@ param( [Parameter(Mandatory = $true)][string]$CMakePath, [Parameter(Mandatory = $true)][string]$OutputDirectory, [switch]$AcceptWixBinaryEula, + [switch]$MsiOnly, [string]$ModelSourceDirectory, [string]$PubspecPath, [string]$CargoManifestPath, @@ -36,7 +38,10 @@ $ErrorActionPreference = "Stop" if (-not $AcceptWixBinaryEula) { throw "Read WiX 7 OSMFEULA.txt and explicitly pass -AcceptWixBinaryEula before using its binary toolchain" } -if (-not [Environment]::Is64BitProcess) { throw "MSI packaging requires a native x64 process" } +if ([Runtime.InteropServices.RuntimeInformation]::OSArchitecture.ToString() -ine $Architecture -or + [Runtime.InteropServices.RuntimeInformation]::ProcessArchitecture.ToString() -ine $Architecture) { + throw "MSI packaging and native policy tests require a native Windows $Architecture process" +} $repository = Get-AmeRepositoryRoot if ([string]::IsNullOrWhiteSpace($PubspecPath)) { $PubspecPath = Join-Path $repository 'pubspec.yaml' } & (Join-Path $PSScriptRoot "release_validate_version.ps1") -Tag $Tag ` @@ -52,7 +57,7 @@ $sourceArguments = if ($PSCmdlet.ParameterSetName -ceq 'Candidate') { SigningMode = $SigningMode; ExpectedBrokerPublisher = $ExpectedBrokerPublisher } } else { - @{ BundlePath = $BundlePath; UnsignedEvidencePath = $UnsignedEvidencePath } + @{ BundlePath = $BundlePath; UnsignedEvidencePath = $UnsignedEvidencePath; Architecture = $Architecture } } $installerSource = Get-AmeInstallerSource @sourceArguments $bundle = $installerSource.Root @@ -64,17 +69,15 @@ if (-not $output.StartsWith($buildRoot + '\', [StringComparison]::OrdinalIgnoreC foreach ($path in @($bundle, $output, $CMakePath)) { Assert-AmeUnsignedBuildPath $path } -$msi = Join-Path $output "Cedarflake-Ame-$Tag-windows-x64.msi" +$msi = Join-Path $output "Cedarflake-Ame-$Tag-windows-$Architecture.msi" if (Test-Path -LiteralPath $msi) { throw "Installer output already exists; preserve the previous package" } -Assert-AmeInstallerPayloadNames $installerSource.Files -RequireModels $false +Assert-AmeInstallerPayloadNames $installerSource.Files -RequireModels $false -Architecture $Architecture $toolLock = Enter-AmeRepositoryToolLock $leases = [Collections.Generic.List[IDisposable]]::new() try { [void][IO.Directory]::CreateDirectory($output) $wix = Get-AmeInstallerWixTool -PackagePath $WixSdkPackagePath -SdkDirectory $WixSdkDirectory $wixUi = Get-AmeInstallerWixUi -PackagePath $WixUiPackagePath -Directory $WixUiDirectory - $wixBootstrapper = Get-AmeInstallerWixBootstrapper -PackagePath $WixBootstrapperPackagePath ` - -Directory $WixBootstrapperDirectory foreach ($entry in $installerSource.Files) { $path = Join-Path $bundle $entry.relativePath Assert-AmeUnsignedBuildPath $path @@ -84,6 +87,9 @@ try { (Get-FileHash -LiteralPath $path -Algorithm SHA256).Hash -ine $entry.sha256) { throw "Installer payload differs from its verified Release manifest" } + if ([IO.Path]::GetExtension($path) -in @('.exe', '.dll')) { + Assert-AmeWindowsPeImage -Path $path -Architecture $Architecture + } } $modelManifest = Get-AmeClassificationModelsManifest -MetadataDirectory $ModelMetadataDirectory $modelDirectory = Join-Path $buildRoot "classification-models/$($modelManifest.package)" @@ -96,22 +102,24 @@ try { Assert-AmeClassificationRuntimeArtifact $path $entry } $installerFiles = @($installerSource.Files) + $modelFiles - Assert-AmeInstallerPayloadNames $installerFiles -ModelManifest $modelManifest + Assert-AmeInstallerPayloadNames $installerFiles -ModelManifest $modelManifest -Architecture $Architecture + $platform = if ($Architecture -eq 'arm64') { 'ARM64' } else { 'x64' } $nativeBuild = Join-Path $output "native" Invoke-AmeChecked $CMakePath @("-S", (Join-Path $repository "windows/installer"), "-B", $nativeBuild, - "-G", "Visual Studio 17 2022", "-A", "x64") + "-A", $platform) Invoke-AmeChecked $CMakePath @("--build", $nativeBuild, "--config", "Release", "--parallel", "1") & (Join-Path $nativeBuild "Release/ame_installer_policy_test.exe") if ($LASTEXITCODE -ne 0) { throw "Installer native policy tests failed" } $admission = Join-Path $nativeBuild "Release/ame_installer_admission.dll" - Assert-AmeUnsignedX64Image $admission + Assert-AmeWindowsPeImage -Path $admission -Architecture $Architecture $leases.Add([IO.FileStream]::new($admission, [IO.FileMode]::Open, [IO.FileAccess]::Read, [IO.FileShare]::Read)) $generated = Join-Path $output "application_files.wxs" New-AmeInstallerApplicationSource -Files $installerFiles -BundlePath $bundle -OutputPath $generated ` - -ModelDirectory $modelDirectory -ModelManifest $modelManifest + -ModelDirectory $modelDirectory -ModelManifest $modelManifest -Architecture $Architecture $broker = @($installerSource.Files | Where-Object { $_.relativePath -ceq "cedarflake_ame_journal_broker.exe" })[0] $identity = "AMEJBID3|protocol=5|max_frame=1048576|sha256=" + ([string]$broker.sha256).ToUpperInvariant() - $productCode = Get-AmeInstallerGuid "product/$version" + $productCode = Get-AmeInstallerGuid "product/$version" $Architecture + $upgradeCode = $script:AmeInstallerFamilies.$Architecture $sources = @((Join-Path $repository "windows/installer/package.wxs"), (Join-Path $repository "windows/installer/ui.wxs"), (Join-Path $repository "windows/installer/options.wxs"), $generated) @@ -120,9 +128,10 @@ try { } # WIX1149 also warns on SID/privilege configuration; its suggested Util replacement only # supports failure actions. ADR 0027 limits native rows to types 5/6 and requires SCM readback. - $arguments = @("-acceptEula", "wix7", "build", "-arch", "x64", "-wx", "-sw1149", "-ct", "1", + $arguments = @("-acceptEula", "wix7", "build", "-arch", $Architecture, "-wx", "-sw1149", "-ct", "1", "-ext", $wixUi, "-culture", "zh-CN", "-d", "ProductVersion=$version", "-d", "ProductCode=$productCode", + "-d", "UpgradeCode=$upgradeCode", "-d", "Architecture=$platform", "-d", "BrokerPath=$(Join-Path $bundle 'cedarflake_ame_journal_broker.exe')", "-d", "BrokerIdentity=$identity", "-d", "AdmissionDll=$admission", "-d", "ApplicationIcon=$(Join-Path $repository 'windows/runner/resources/app_icon.ico')", @@ -138,11 +147,11 @@ try { applicationVersion = $applicationVersion productVersion = $version productCode = $productCode - upgradeCode = $script:AmeInstallerUpgradeCode + upgradeCode = $upgradeCode packageSha256 = (Get-FileHash -LiteralPath $msi -Algorithm SHA256).Hash admissionSha256 = (Get-FileHash -LiteralPath $admission -Algorithm SHA256).Hash brokerIdentity = $identity - payload = [ordered]@{ machine = 'windows-x64'; configuration = 'Release'; fileCount = $installerFiles.Count; files = $installerFiles } + payload = [ordered]@{ machine = "windows-$Architecture"; configuration = 'Release'; fileCount = $installerFiles.Count; files = $installerFiles } createdUtc = [DateTime]::UtcNow.ToString("o") } $model = Read-AmeInstallerDatabase -PackagePath $msi ` @@ -152,10 +161,14 @@ try { & (Join-Path $PSScriptRoot 'release_test_installer_ui.ps1') -PackagePath $msi ` -DtfAssemblyPath (Join-Path $WixSdkDirectory 'tools/net472/WixToolset.Dtf.WindowsInstaller.dll') $receipt.databasePolicy = "passed" - $setup = New-AmeInstallerBundle -Wix $wix -BootstrapperExtension $wixBootstrapper -PackagePath $msi ` - -Version $version -Tag $Tag -OutputDirectory $output -Repository $repository - $receipt.setupFile = [IO.Path]::GetFileName($setup) - $receipt.setupSha256 = (Get-FileHash -LiteralPath $setup -Algorithm SHA256).Hash + if (-not $MsiOnly -and $Architecture -eq 'x64') { + $wixBootstrapper = Get-AmeInstallerWixBootstrapper -PackagePath $WixBootstrapperPackagePath ` + -Directory $WixBootstrapperDirectory + $setup = New-AmeInstallerBundle -Wix $wix -BootstrapperExtension $wixBootstrapper -PackagePath $msi ` + -Version $version -Tag $Tag -OutputDirectory $output -Repository $repository + $receipt.setupFile = [IO.Path]::GetFileName($setup) + $receipt.setupSha256 = (Get-FileHash -LiteralPath $setup -Algorithm SHA256).Hash + } [IO.File]::WriteAllText((Join-Path $output "package.json"), ($receipt | ConvertTo-Json -Depth 8), [Text.UTF8Encoding]::new($false)) Write-Host "Windows Installer package built: $msi" } finally { diff --git a/tool/release_prepare_classification_runtime.ps1 b/tool/release_prepare_classification_runtime.ps1 index d8af1780f..4b23eee3e 100644 --- a/tool/release_prepare_classification_runtime.ps1 +++ b/tool/release_prepare_classification_runtime.ps1 @@ -3,7 +3,8 @@ param( [Parameter(Mandatory = $true)][string]$OutputDirectory, [string]$ArchivePath, [switch]$Accelerator, - [string]$DirectMLArchivePath + [string]$DirectMLArchivePath, + [ValidateSet('x64', 'arm64')][string]$Architecture = 'x64' ) $ErrorActionPreference = "Stop" @@ -15,7 +16,7 @@ trap { . (Join-Path $PSScriptRoot "release_classification_runtime.ps1") . (Join-Path $PSScriptRoot "release_download_classification_runtime.ps1") -$manifest = Get-AmeClassificationRuntimeManifest -Accelerator:$Accelerator +$manifest = Get-AmeClassificationRuntimeManifest -Accelerator:$Accelerator -Architecture $Architecture $output = [IO.Path]::GetFullPath($OutputDirectory).TrimEnd('\', '/') $build = [IO.Path]::GetFullPath((Join-Path (Split-Path -Parent $PSScriptRoot) "build")) if (-not $output.StartsWith("$build\", [StringComparison]::OrdinalIgnoreCase)) { diff --git a/tool/release_test_classification_runtime.ps1 b/tool/release_test_classification_runtime.ps1 index 13141310b..b181bca84 100644 --- a/tool/release_test_classification_runtime.ps1 +++ b/tool/release_test_classification_runtime.ps1 @@ -32,12 +32,19 @@ Add-Type -AssemblyName System.IO.Compression.FileSystem try { $archive = [IO.Compression.ZipFile]::Open($zipPath, [IO.Compression.ZipArchiveMode]::Create) try { + $metadataEntry = $archive.CreateEntry('Microsoft.ML.OnnxRuntime.nuspec') + $writer = [IO.StreamWriter]::new($metadataEntry.Open(), $utf8) + try { $writer.Write('1.2.3') } + finally { $writer.Dispose() } foreach ($file in $fixture.files) { $bytes = $utf8.GetBytes("fixed runtime fixture: $($file.name)") + if ($file.name -ceq 'VERSION_NUMBER') { $bytes = $utf8.GetBytes("1.2.3`r`n") } + if ($file.name -ceq 'GIT_COMMIT_ID') { $bytes = $utf8.GetBytes("abc123 `r`n") } $file.length = $bytes.Length $hash = [Security.Cryptography.SHA256]::Create() try { $file.sha256 = ([BitConverter]::ToString($hash.ComputeHash($bytes))).Replace('-', '').ToLowerInvariant() } finally { $hash.Dispose() } + if ($null -ne $file.PSObject.Properties['nuspecField']) { continue } $entry = $archive.CreateEntry($fixture.archive.prefix + $file.entry) $stream = $entry.Open() try { $stream.Write($bytes, 0, $bytes.Length) } finally { $stream.Dispose() } diff --git a/tool/release_test_installer_payload.ps1 b/tool/release_test_installer_payload.ps1 index bea4c0dca..85cf44590 100644 --- a/tool/release_test_installer_payload.ps1 +++ b/tool/release_test_installer_payload.ps1 @@ -33,6 +33,10 @@ foreach ($case in @( Assert-InstallerRejected { Get-AmeInstallerVersion -Tag $case[0] -ApplicationVersion $case[1] } } $firstProduct = Get-AmeInstallerGuid "product/0.1.0" +if ((Get-AmeInstallerGuid 'product/0.1.4' x64) -cne 'B0E516FC-1AA8-591B-A29B-86ABA3FBBF5F' -or + (Get-AmeInstallerGuid 'product/0.1.4' arm64) -ceq (Get-AmeInstallerGuid 'product/0.1.4' x64)) { + throw 'Installer architectures must have distinct products and retain the existing x64 upgrade identity' +} if ($firstProduct -cne (Get-AmeInstallerGuid "product/0.1.0") -or $firstProduct -ceq (Get-AmeInstallerGuid "product/0.1.1")) { throw "MSI product identity does not separate repair from upgrade" diff --git a/tool/release_test_installer_policy.ps1 b/tool/release_test_installer_policy.ps1 index ba97d9f48..2d20e355f 100644 --- a/tool/release_test_installer_policy.ps1 +++ b/tool/release_test_installer_policy.ps1 @@ -7,6 +7,11 @@ function Test-AmeInstallerPolicyRegressions { Assert-AmeInstallerPolicy $Model $Receipt $encoded = $Model | ConvertTo-Json -Depth 12 $rejections = [ordered]@{ + wrong_architecture = { + param($value) + $value.Template = if ($value.Template -ceq 'x64;2052') { 'Arm64;2052' } else { 'x64;2052' } + } + foreign_upgrade_family = { param($value) ($value.Property | Where-Object Property -CEQ 'UpgradeCode').Value = [guid]::Empty.ToString('B') } foreign_service = { param($value) $value.ServiceInstall[0].Name = 'OtherService' } broad_privileges = { param($value) ($value.MsiServiceConfig | Where-Object ConfigType -CEQ '6').Argument = 'SeManageVolumePrivilege[~]SeImpersonatePrivilege[~]SeDebugPrivilege' } missing_caller_impersonation = { param($value) ($value.MsiServiceConfig | Where-Object ConfigType -CEQ '6').Argument = 'SeManageVolumePrivilege' } diff --git a/windows/cmake/classification_runtime.cmake b/windows/cmake/classification_runtime.cmake index 519704f9e..0eb7b3672 100644 --- a/windows/cmake/classification_runtime.cmake +++ b/windows/cmake/classification_runtime.cmake @@ -1,5 +1,9 @@ -if(NOT FLUTTER_TARGET_PLATFORM STREQUAL "windows-x64") - message(FATAL_ERROR "The admitted classification runtime requires Windows x64") +if(FLUTTER_TARGET_PLATFORM STREQUAL "windows-arm64") + set(AME_RUNTIME_ARCH arm64) +elseif(FLUTTER_TARGET_PLATFORM STREQUAL "windows-x64") + set(AME_RUNTIME_ARCH x64) +else() + message(FATAL_ERROR "No classification runtime for ${FLUTTER_TARGET_PLATFORM}") endif() find_program(AME_POWERSHELL NAMES pwsh powershell) @@ -11,10 +15,10 @@ set(AME_CLASSIFICATION_ACCELERATOR_DIR "${CMAKE_BINARY_DIR}/classification-accel add_custom_target(ame_classification_runtime COMMAND "${AME_POWERSHELL}" -NoProfile -NonInteractive -ExecutionPolicy Bypass -File "${CMAKE_CURRENT_LIST_DIR}/../../tool/release_prepare_classification_runtime.ps1" - -OutputDirectory "${AME_CLASSIFICATION_RUNTIME_DIR}" + -OutputDirectory "${AME_CLASSIFICATION_RUNTIME_DIR}" -Architecture "${AME_RUNTIME_ARCH}" COMMAND "${AME_POWERSHELL}" -NoProfile -NonInteractive -ExecutionPolicy Bypass -File "${CMAKE_CURRENT_LIST_DIR}/../../tool/release_prepare_classification_runtime.ps1" - -OutputDirectory "${AME_CLASSIFICATION_ACCELERATOR_DIR}" -Accelerator + -OutputDirectory "${AME_CLASSIFICATION_ACCELERATOR_DIR}" -Architecture "${AME_RUNTIME_ARCH}" -Accelerator VERBATIM ) add_dependencies(${BINARY_NAME} ame_classification_runtime) diff --git a/windows/cmake/msvc_runtime.cmake b/windows/cmake/msvc_runtime.cmake index 1f0626fa9..3d03bd795 100644 --- a/windows/cmake/msvc_runtime.cmake +++ b/windows/cmake/msvc_runtime.cmake @@ -3,7 +3,14 @@ set(CMAKE_INSTALL_DEBUG_LIBRARIES FALSE) include(InstallRequiredSystemLibraries) set(AME_MODEL_MSVC_RUNTIME_LIBS) -foreach(runtime_name msvcp140.dll msvcp140_1.dll vcruntime140.dll vcruntime140_1.dll) +set(AME_MODEL_MSVC_RUNTIME_NAMES msvcp140.dll msvcp140_1.dll vcruntime140.dll) +if(FLUTTER_TARGET_PLATFORM STREQUAL "windows-x64") + list(APPEND AME_MODEL_MSVC_RUNTIME_NAMES vcruntime140_1.dll) +elseif(FLUTTER_TARGET_PLATFORM STREQUAL "windows-arm64") + # The ARM64 redistributable also carries the x64 exception handler for emulated clients. + list(FILTER CMAKE_INSTALL_SYSTEM_RUNTIME_LIBS EXCLUDE REGEX "/vcruntime140_1[.]dll$") +endif() +foreach(runtime_name IN LISTS AME_MODEL_MSVC_RUNTIME_NAMES) set(runtime_found FALSE) foreach(runtime_path IN LISTS CMAKE_INSTALL_SYSTEM_RUNTIME_LIBS) get_filename_component(runtime_file "${runtime_path}" NAME) diff --git a/windows/installer/CMakeLists.txt b/windows/installer/CMakeLists.txt index 551081090..1924351f3 100644 --- a/windows/installer/CMakeLists.txt +++ b/windows/installer/CMakeLists.txt @@ -1,15 +1,28 @@ -cmake_minimum_required(VERSION 3.15) +cmake_minimum_required(VERSION 3.19) project(ame_installer_admission LANGUAGES CXX) -if(NOT WIN32 OR NOT MSVC OR NOT CMAKE_SIZEOF_VOID_P EQUAL 8) - message(FATAL_ERROR "The installer requires Windows x64 MSVC") +string(TOLOWER "${CMAKE_VS_PLATFORM_NAME}" platform) +if(NOT WIN32 OR NOT MSVC OR NOT platform MATCHES "^(x64|arm64)$") + message(FATAL_ERROR "The installer requires Windows x64 or ARM64 MSVC") endif() +if(platform STREQUAL "arm64") + set(other_platform x64) +else() + set(other_platform arm64) +endif() +set(families_file "${CMAKE_CURRENT_SOURCE_DIR}/product_families.json") +set_property(DIRECTORY APPEND PROPERTY CMAKE_CONFIGURE_DEPENDS "${families_file}") +file(READ "${families_file}" families) +string(JSON upgrade_code GET "${families}" "${platform}") +string(JSON other_upgrade_code GET "${families}" "${other_platform}") add_library(ame_installer_policy STATIC protected_paths.cpp service_configuration.cpp installed_broker_identity.cpp registered_product.cpp service_start.cpp application_destination.cpp service_privileges.cpp service_rollback.cpp) target_compile_features(ame_installer_policy PUBLIC cxx_std_17) target_compile_definitions(ame_installer_policy PUBLIC UNICODE _UNICODE NOMINMAX) +target_compile_definitions(ame_installer_policy PUBLIC + AME_UPGRADE_CODE=L"{${upgrade_code}}" AME_OTHER_UPGRADE_CODE=L"{${other_upgrade_code}}") target_link_libraries(ame_installer_policy PUBLIC advapi32 shell32 ole32 bcrypt msi) add_library(ame_installer_admission SHARED admission.cpp) diff --git a/windows/installer/admission.cpp b/windows/installer/admission.cpp index c99c76e21..00013a89b 100644 --- a/windows/installer/admission.cpp +++ b/windows/installer/admission.cpp @@ -59,6 +59,18 @@ extern "C" __declspec(dllexport) UINT __stdcall AmeValidateInstallation(MSIHANDL } ValidateProtectedTree(product); const auto registered = ReadRegisteredProduct(); + if (!registered) { + USHORT process_machine = 0, native_machine = 0; +#if defined(_M_ARM64) + constexpr auto expected_machine = IMAGE_FILE_MACHINE_ARM64; +#else + constexpr auto expected_machine = IMAGE_FILE_MACHINE_AMD64; +#endif + if (!IsWow64Process2(GetCurrentProcess(), &process_machine, &native_machine) || + native_machine != expected_machine) { + throw std::runtime_error("Choose the Cedarflake Ame installer for this PC's native architecture."); + } + } ValidateApplicationDestination(TargetPath(session, L"AMEAPPDIR"), registered); const auto service = ReadBrokerServiceRegistration(); if (!registered && (service || diff --git a/windows/installer/package.wxs b/windows/installer/package.wxs index 92848625b..ceacf4423 100644 --- a/windows/installer/package.wxs +++ b/windows/installer/package.wxs @@ -2,14 +2,14 @@ - + + Message="此安装包需要 Windows 11 $(var.Architecture)。" /> diff --git a/windows/installer/policy.h b/windows/installer/policy.h index 7ffb28c6c..8941b484e 100644 --- a/windows/installer/policy.h +++ b/windows/installer/policy.h @@ -12,7 +12,7 @@ namespace ame::installer { -inline constexpr wchar_t kUpgradeCode[] = L"{C284E6B0-1484-4926-A94B-C3EC3840DBBD}"; +inline constexpr wchar_t kUpgradeCode[] = AME_UPGRADE_CODE; inline constexpr wchar_t kServiceName[] = L"CedarflakeAmeJournalBroker"; inline constexpr wchar_t kBrokerName[] = L"cedarflake_ame_journal_broker.exe"; inline constexpr wchar_t kBrokerIdentityPrefix[] = L"AMEJBID3|protocol=5|max_frame=1048576|sha256="; diff --git a/windows/installer/product_families.json b/windows/installer/product_families.json new file mode 100644 index 000000000..dc496f2c9 --- /dev/null +++ b/windows/installer/product_families.json @@ -0,0 +1,4 @@ +{ + "x64": "C284E6B0-1484-4926-A94B-C3EC3840DBBD", + "arm64": "293D2430-983F-5117-BB52-64978D653436" +} diff --git a/windows/installer/protected_paths.cpp b/windows/installer/protected_paths.cpp index 9ea733fa2..6c27df4e4 100644 --- a/windows/installer/protected_paths.cpp +++ b/windows/installer/protected_paths.cpp @@ -79,7 +79,7 @@ std::wstring ComparablePath(const std::filesystem::path& path) { std::filesystem::path ProgramFiles() { wchar_t* raw = nullptr; - const HRESULT result = SHGetKnownFolderPath(FOLDERID_ProgramFilesX64, 0, nullptr, &raw); + const HRESULT result = SHGetKnownFolderPath(FOLDERID_ProgramFiles, 0, nullptr, &raw); if (FAILED(result) || !raw) { CoTaskMemFree(raw); throw std::runtime_error("Native Program Files is unavailable"); diff --git a/windows/installer/registered_product.cpp b/windows/installer/registered_product.cpp index 143b206e5..e62e356f7 100644 --- a/windows/installer/registered_product.cpp +++ b/windows/installer/registered_product.cpp @@ -110,6 +110,14 @@ RegisteredProduct CachedProduct(const wchar_t* product) { } // namespace std::optional ReadRegisteredProduct() { + wchar_t other_product[39]{}; + const UINT other = MsiEnumRelatedProductsW(AME_OTHER_UPGRADE_CODE, 0, 0, other_product); + if (other == ERROR_SUCCESS) { + throw std::runtime_error("Uninstall the other architecture of Cedarflake Ame before switching. Your library is retained."); + } + if (other != ERROR_NO_MORE_ITEMS) { + throw std::runtime_error("Cannot check the other architecture's product registration"); + } std::optional registered; for (DWORD index = 0; index < 32; ++index) { wchar_t product[39]{};