metal: allow disabling binary pipeline archives #519

Open
fruvs wants to merge 1 commit from upstream-pr/519 into main
fruvs commented 2026-11-27 23:52:18 +00:00 (Migrated from github.com)

Imported from LakshanSivakumar/Native-Victory-macOS. Originally posted by fruvs on 2026-11-27T23:52:18Z.


Problem

On macOS 27.2.1 (Apple M5 Pro), Metal sometimes terminates the process while opening or populating an MTLBinaryArchive when the executable is run from an app bundle in a restricted launch. I observed an Objective-C exception in recordBinaryArchiveUsage: during a reopen, and a null dereference in MTLCompiler createBinaryArchiveAsRecompileTarget:… while adding the first compute pipeline to a fresh archive. The latter exits inside Metal before an error can reach the caller.

Change

Add an opt-out with VICTORY_NO_PIPELINE_ARCHIVE=1. This uses the existing direct pipeline compilation paths, while the translated shader and Metal library caches stay available. Default archive behavior stays enabled.

Minimal reproducer

In a signed macOS app bundle, run this using a writable cache URL. Use the normal bundled launch as a control and the same executable under a sandbox profile that denies network, external writes and other executable launches, with HOME, CFFIXED_USER_HOME and TMPDIR pointing inside the writable directory.

import AppKit
import Foundation
import Metal

_ = NSApplication.shared.setActivationPolicy(.regular)
let device = MTLCreateSystemDefaultDevice()!
let library = try device.makeLibrary(
    source: """
    #include <metal_stdlib>
    using namespace metal;
    kernel void increment(device uint *value [[buffer(0)]]) { value[0] += 1; }
    """,
    options: nil
)
let descriptor = MTLComputePipelineDescriptor()
descriptor.computeFunction = library.makeFunction(name: "increment")!
let archive = try device.makeBinaryArchive(descriptor: MTLBinaryArchiveDescriptor())
_ = try device.makeComputePipelineState(descriptor: descriptor, options: [], reflection: nil)
try archive.addComputePipelineFunctions(descriptor: descriptor)
try archive.serialize(to: URL(fileURLWithPath: CommandLine.arguments[1]))

The synthetic program reproduced the fresh-archive null dereference on the first restricted launch. Its unrestricted fresh and warm controls passed, and later restricted launches also passed. The exact macOS trigger is still unresolved.

Verification

  • Four focused victory-metal archive tests passed on native ARM64 with the default archive path.
  • A windowed ARM64 launch with the opt-out passed both previous startup failure points and ran through the animated opening credits without creating a binary archive.
<!-- greyfinch-sync:LakshanSivakumar/Native-Victory-macOS:issue:5534633995 --> Imported from [LakshanSivakumar/Native-Victory-macOS](https://github.com/LakshanSivakumar/Native-Victory-macOS/pull/519). Originally posted by [fruvs](https://github.com/fruvs) on 2026-11-27T23:52:18Z. --- ## Problem On macOS 27.2.1 (Apple M5 Pro), Metal sometimes terminates the process while opening or populating an `MTLBinaryArchive` when the executable is run from an app bundle in a restricted launch. I observed an Objective-C exception in `recordBinaryArchiveUsage:` during a reopen, and a null dereference in `MTLCompiler createBinaryArchiveAsRecompileTarget:…` while adding the first compute pipeline to a fresh archive. The latter exits inside Metal before an error can reach the caller. ## Change Add an opt-out with `VICTORY_NO_PIPELINE_ARCHIVE=1`. This uses the existing direct pipeline compilation paths, while the translated shader and Metal library caches stay available. Default archive behavior stays enabled. ## Minimal reproducer In a signed macOS app bundle, run this using a writable cache URL. Use the normal bundled launch as a control and the same executable under a sandbox profile that denies network, external writes and other executable launches, with `HOME`, `CFFIXED_USER_HOME` and `TMPDIR` pointing inside the writable directory. ```swift import AppKit import Foundation import Metal _ = NSApplication.shared.setActivationPolicy(.regular) let device = MTLCreateSystemDefaultDevice()! let library = try device.makeLibrary( source: """ #include <metal_stdlib> using namespace metal; kernel void increment(device uint *value [[buffer(0)]]) { value[0] += 1; } """, options: nil ) let descriptor = MTLComputePipelineDescriptor() descriptor.computeFunction = library.makeFunction(name: "increment")! let archive = try device.makeBinaryArchive(descriptor: MTLBinaryArchiveDescriptor()) _ = try device.makeComputePipelineState(descriptor: descriptor, options: [], reflection: nil) try archive.addComputePipelineFunctions(descriptor: descriptor) try archive.serialize(to: URL(fileURLWithPath: CommandLine.arguments[1])) ``` The synthetic program reproduced the fresh-archive null dereference on the first restricted launch. Its unrestricted fresh and warm controls passed, and later restricted launches also passed. The exact macOS trigger is still unresolved. ## Verification - Four focused `victory-metal` archive tests passed on native ARM64 with the default archive path. - A windowed ARM64 launch with the opt-out passed both previous startup failure points and ran through the animated opening credits without creating a binary archive.
AnastasisKon commented 2026-11-28 17:05:50 +00:00 (Migrated from github.com)

Imported from LakshanSivakumar/Native-Victory-macOS. Originally posted by AnastasisKon on 2026-11-28T17:05:50Z.

Review: APPROVED


Read the diff: a 4-line early return in Archive::open for VICTORY_NO_PIPELINE_ARCHIVE=1, plus a documented paragraph in docs/native-host.md (the switch follows the repo's VICTORY_* convention and the docs' cache section already documents the neighbouring VICTORY_NO_SHADER_CACHE). Ran cargo test -p victory-metal --lib on the head: 42 passed, 0 failed. The opt-out only skips the binary archive; the shader translation and Metal library caches stay on, as the body says. Approving.

<!-- greyfinch-sync:LakshanSivakumar/Native-Victory-macOS:review:6099185978 --> Imported from [LakshanSivakumar/Native-Victory-macOS](https://github.com/LakshanSivakumar/Native-Victory-macOS/pull/519#pullrequestreview-6099185978). Originally posted by [AnastasisKon](https://github.com/AnastasisKon) on 2026-11-28T17:05:50Z. **Review:** APPROVED --- Read the diff: a 4-line early return in `Archive::open` for `VICTORY_NO_PIPELINE_ARCHIVE=1`, plus a documented paragraph in `docs/native-host.md` (the switch follows the repo's `VICTORY_*` convention and the docs' cache section already documents the neighbouring `VICTORY_NO_SHADER_CACHE`). Ran `cargo test -p victory-metal --lib` on the head: 42 passed, 0 failed. The opt-out only skips the binary archive; the shader translation and Metal library caches stay on, as the body says. Approving.
This pull request cannot be merged automatically due to conflicts.
Merge manually to resolve the conflicts.
View command line instructions

Checkout

From your project repository, check out a new branch and test the changes.
git fetch -u origin 'upstream-pr/519:upstream-pr/519'
git switch 'upstream-pr/519'

Merge

Merge the changes and update on Forgejo.

Warning: The "Autodetect manual merge" setting is not enabled for this repository, you will have to mark this pull request as manually merged afterwards.

git switch 'main'
git merge --no-ff 'upstream-pr/519'
git switch 'upstream-pr/519'
git rebase 'main'
git switch 'main'
git merge --ff-only 'upstream-pr/519'
git switch 'upstream-pr/519'
git rebase 'main'
git switch 'main'
git merge --no-ff 'upstream-pr/519'
git switch 'main'
git merge --squash 'upstream-pr/519'
git switch 'main'
git merge --ff-only 'upstream-pr/519'
git switch 'main'
git merge 'upstream-pr/519'
git push origin 'main'
Sign in to join this conversation.
No reviewers
No labels
No milestone
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Reference
LakshanSivakumar/Native-Victory-macOS!519
No description provided.