metal: allow disabling binary pipeline archives #523

Open
github-bot wants to merge 1 commit from github-bot/gh-519 into main AGit
Owner

@⁠fruvs on GitHub · 2026-11-27 23:52 UTC · original
fruvs:codex/metal-pipeline-archive-opt-out → main · +9 −0 · 2 files · 1 commits · draft

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.
**@⁠fruvs** on GitHub · 2026-11-27 23:52 UTC · [original](https://github.com/LakshanSivakumar/Native-Victory-macOS/pull/519) `fruvs:codex/metal-pipeline-archive-opt-out` → `main` · +9 −0 · 2 files · 1 commits · draft ## 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.
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-27 23:54:55 +00:00
Author
Owner

@⁠AnastasisKon on GitHub · 2026-11-28 17:05 UTC · original · approved these changes

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.

**@⁠AnastasisKon** on GitHub · 2026-11-28 17:05 UTC · [original](https://github.com/LakshanSivakumar/Native-Victory-macOS/pull/519#pullrequestreview-6099185978) · **approved these changes** 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 can be merged automatically.
This branch is out-of-date with the base branch
You are not authorized to merge this pull request.
View command line instructions

Checkout

From your project repository, check out a new branch and test the changes.
git fetch -u origin '+refs/pull/523/head:github-bot/gh-519'
git switch 'github-bot/gh-519'

Merge

Merge the changes and update on Forgejo.
git switch 'main'
git merge --no-ff 'github-bot/gh-519'
git switch 'github-bot/gh-519'
git rebase 'main'
git switch 'main'
git merge --ff-only 'github-bot/gh-519'
git switch 'github-bot/gh-519'
git rebase 'main'
git switch 'main'
git merge --no-ff 'github-bot/gh-519'
git switch 'main'
git merge --squash 'github-bot/gh-519'
git switch 'main'
git merge --ff-only 'github-bot/gh-519'
git switch 'main'
git merge 'github-bot/gh-519'
git push origin 'main'
This discussion has been locked. Commenting is limited to contributors.
No description provided.