Harden generic-enum fixtures against layout and refcount bugs#10
Merged
Conversation
The `.empty(reason, metadata:)` builder wrote metadata at `MemoryLayout<GestureOutputEmptyReason>.stride == 1`, but the case's tuple layout aligns `GestureOutputMetadata?` (alignment 8) to offset 8 — the metadata bytes were landing in the payload's padding region. Reading the value back tripped `initializeWithCopy` during Swift Testing's discovery on iOS Simulator, crashing xctest at bootstrap. - Build the payload as a Swift tuple so the compiler computes the correct field offsets and refcount handling for every case. - Replace `UnsafeMutableRawPointer.allocate` + `load(as:)` with `UnsafeMutablePointer<GestureOutput>.allocate(capacity:)` + `move()` to transfer ownership out of the scratch slot, avoiding the extra retain that the load+deallocate pattern leaked.
- Promote the tuple-based enum builder into a shared `makeEnum<T, Payload>` in `Metadata+Enum.swift`; drop the per-file `make(tag:_:)` helpers. - Apply the same pattern to `GesturePhase`'s fixture (previously worked only for `Value = Int` by coincidence — `.blocked(value:blockedBy:)` uses `stride(Value)` as the second field's offset). - Add `.active`/`.blocked` tests for `GesturePhase<String>` and `.value`/`.finalValue` tests for `GestureOutput<String>` so a bridgeObject-carrying payload round-trips through `initializeWithCopy`.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
GestureOutputCompatibilityTestsandGesturePhaseCompatibilityTestseachbuild their cases via a
make(tag:_:)helper that (a) writes case fields athand-computed offsets and (b) extracts the value with
load(as:) + defer deallocate. Two latent bugs made iOS Simulator discovery crash at xctestbootstrap:
Offset bug.
.empty(GestureOutputEmptyReason, GestureOutputMetadata?)wrote metadata at
stride(GestureOutputEmptyReason) == 1, but the case'stuple layout aligns
GestureOutputMetadata?(alignment 8) to offset 8.The metadata bytes landed in the payload's padding region; reading the
value back tripped
initializeWithCopyduring Swift Testing's testdiscovery (
xctest at initializeWithCopy for GestureOutput/xctest at static GestureOutput.make(tag:_:)). The same failure mode islatent in
GesturePhase.blocked(value:blockedBy:)for anyValuewhosestridedoes not matchGestureNodeID's alignment — it only workedbecause the tests used
Value = Int.Refcount leak.
load(as:)bumps the returned value's retain countwhile
deallocate()skips destroying the memory — whenever the payloadcarried a bridgeObject field, the retain
initializeMemorywrote inplace was leaked. The test values used so far are POD, so it was
invisible until someone tested a
String-carryingValue.Both fixtures now share a tuple-based
make<Payload>(tag:payload:):(reason, metadata),(value, blockedBy),()for no-payload cases) so the compiler computesthe correct field offsets and alignment padding for every case.
UnsafeMutablePointer<…>.allocate(capacity:)+move(),transferring ownership out of the scratch slot instead of the leaky
load+deallocatedance.Also adds
.active/.blockedtests forGesturePhase<String>and.value/.finalValuetests forGestureOutput<String>to exercise abridgeObject-carrying payload through
initializeWithCopy— these wouldtrip the leak under the old pattern.
Test plan
xcodebuild test -only-testing:OpenGesturesTestson iPhone 17 Pro / iOS 26.2 SimulatorOPENGESTURES_COMPATIBILITY_TEST=0 xcodebuild test -only-testing:OpenGesturesCompatibilityTestson the same simulatorOPENGESTURES_COMPATIBILITY_TEST=1 OPENGESTURES_USE_LOCAL_DEPS=1 xcodebuild test -only-testing:OpenGesturesCompatibilityTestson the same simulator