copyallocator

command
v0.6.19 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 28, 2026 License: MIT Imports: 13 Imported by: ? modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Command copyallocator is a failing regression gate for two applegen defects that together make the MPS fallback copy allocator unusable from Go.

It is expected to FAIL on the tree it was written against (release-v0.7.0, 2026-08-16). It fails by crashing a child process with SIGSEGV inside Metal Performance Shaders. Do not "fix" this example by weakening it: it is the acceptance test for the generator fix, and an example that passes today would prove nothing.

What the API is

MPSImage.framework/Headers/MPSImageKernel.h:109 declares

typedef id<MTLTexture> __nonnull NS_RETURNS_RETAINED (^MPSCopyAllocator)(
    MPSKernel * __nonnull filter,
    id<MTLCommandBuffer> __nonnull commandBuffer,
    id<MTLTexture> __nonnull sourceTexture);

- (BOOL) encodeToCommandBuffer:(id<MTLCommandBuffer>)commandBuffer
                inPlaceTexture:(__strong id<MTLTexture> * __nonnull)texture
         fallbackCopyAllocator:(MPSCopyAllocator __nullable)copyAllocator;

The kernel first tries to filter the texture in place. When that is not possible it calls the allocator, which must RETURN a fresh texture at +1, filters into it, releases the old texture and writes the new one back through the inPlaceTexture pointer. Both directions matter: the block returns a value, and the texture argument is a pointer to the caller's variable, not the texture itself.

Defect 1: the block's return value was folded into its name

metalperformanceshaders/blocks.gen.go:158 emits

type MTLTextureMPSKernelMTLCommandBufferMTLTextureHandler = func(*MPSKernel, metal.MTLCommandBuffer, metal.MTLTexture)

The leading "MTLTexture" in the generated name is the block's RETURN type, which the generator consumed as part of the identifier and then dropped. Worse, blocks.gen.go:171 binds the block's first PARAMETER to a variable called `result`, so the return slot is not merely missing, it is misattributed. The Go handler has no way to hand a texture back.

Observed consequence: the handler runs with fully valid arguments and then SIGSEGVs in objc_msgSend the instant it returns, because MPS messages whatever garbage the trampoline left in x0 as the returned texture.

A correct fix must give the handler an `objc.ID` (or metal.MTLTexture) return AND honour NS_RETURNS_RETAINED, handing the texture back at +1.

Defect 2: the __strong id* out-parameter was passed by value

metalperformanceshaders/mps_unary_image_kernel.gen.go:228 emits

func (u MPSUnaryImageKernel) EncodeToCommandBufferInPlaceTextureFallbackCopyAllocator(
    commandBuffer metal.MTLCommandBuffer,
    texture metal.MTLTexture,             // <-- Apple's type is id<MTLTexture> *
    copyAllocator MTLTextureMPSKernelMTLCommandBufferMTLTextureHandler) bool

so the texture object is passed where MPS expects the ADDRESS of a texture variable. MPS dereferences the object pointer as an `id *` and writes through it. Observed: a deterministic SIGSEGV inside the encode call itself, before the allocator is ever entered. Proven by A/B — a hand-written objc.Send passing unsafe.Pointer(&texID) runs cleanly 5/5 and MPS rewrites the variable as documented.

Fixing only one of the two still crashes. Defect 2 kills the call before the allocator runs; defect 1 kills it on the way out of the allocator.

Why this program has two processes

The failure is a SIGSEGV raised on a cgo/ObjC stack. Go's recover() cannot catch it — the process dies. So the assertions run in a child process and the parent scores it. The parent requires a positive marker line from the child: an empty log, a missing binary (exit 127) or a child that dies before printing scores as FAIL, never as success.

What the example asserts

The five things a working copy allocator must do, and where each stands against the SHIPPED generated API:

  1. the allocator is invoked — expressible (marker line)
  2. its three arguments are valid objects — expressible (each is messaged with -description and -class, not merely nil-checked)
  3. the texture the allocator returns is the one MPS filters into, i.e. the caller's texture variable is REPLACED by it — INEXPRESSIBLE: the handler cannot return (defect 1) and the caller passes no variable (defect 2)
  4. encode returns true and the output holds the filtered image — INEXPRESSIBLE in the allocator path for the same reason: the filtered result lands in a texture this program can never be handed
  5. NS_RETURNS_RETAINED refcount correctness — NOT CHECKED, and deliberately not faked. -retainCount is unreliable under ARC/taggedptr and MPS may retain the texture again internally; there is no sound in-process check. The right proof is a leaks/heap run against a fixed binding, out of scope here.

Assertions 3 and 4 are written out as the code one WOULD write, in fixed.go, behind the `mpsfixedcopyallocator` build tag so that this package still builds today. They are not silently dropped. Once the generator emits the corrected signatures, that file compiles and

go run -tags mpsfixedcopyallocator ./examples/metalperformanceshaders/copyallocator

runs all four checkable assertions. Note that the fix necessarily changes the shipped signature, so this file's call site will stop compiling at that point — that compile error is the last step of the gate, and the maintainer landing the fix should delete the today-path below and promote fixed.go.

Usage:

copyallocator          # parent: runs the child and scores it
copyallocator -child   # the crashing case; not meant to be run directly

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL