Skip to content

Fix/simulator injection and extension - #10

Merged
dautovri merged 3 commits into
dautovri:mainfrom
hflexgrig:fix/simulator-injection-and-extension
Oct 20, 2026
Merged

dautovri merged 3 commits into
dautovri:mainfrom
hflexgrig:fix/simulator-injection-and-extension

Conversation

@hflexgrig

Copy link
Copy Markdown
Contributor

Summary

Fixes for the simulator injection library (#8) and the camera extension, found while using SimulatorCamera with a .NET MAUI app that captures through CommunityToolkit.Maui.Camera (6.1.0) on the Xcode 27 / iOS 27 Simulator. Without these changes the injected camera was found, but the app crashed as soon as the camera page opened, and captured photos came out with the wrong shape. With them, preview, photo capture and the app's crop step all work in the Simulator.

The library had three crashes. Its -init swizzles returned objects with the wrong ownership, AVFoundation's -dealloc ran on fake objects that were never initialised, and adding a second output crashed. The photo issue was that capturePhoto returned the raw landscape frame, while a real iPhone returns a 4:3 portrait photo when the app captures in portrait. Apps that crop by proportion (ours crops to an on-screen frame) then got a thin strip.

Changes

Simulator injection library (SimulatorInjection/SimCamInject.m)

  • -init swizzles (AVCaptureDeviceInput -initWithDevice:error:, AVCaptureVideoPreviewLayer -initWithSession: / -initWithSessionWithNoConnection:) now use init-family ownership (ns_consumed self, ns_returns_retained). As plain ARC C functions they returned +0, so callers over-released the fake input and crashed in objc_autoreleasePoolPop; the layer inits leaked.
  • The fake classes (SCDevice, SCDeviceInput, SCConnection, SCQRObject, SCPhoto) get a -dealloc that skips AVFoundation's teardown and goes straight to NSObject's, which still releases their ivars and associated objects. -[AVCaptureConnection dealloc] crashed on the never-initialised fake connection, e.g. when a preview layer was released.
  • SCConnection returns the fake device from -sourceDevice, and -canAddOutput: answers directly for sessions on the fake camera. The stock check walks existing outputs' connections and crashed when a second output was added.
  • Photos are a centre 4:3 crop of the frame: portrait when the app's preview layer is portrait, falling back to the connection's rotation angle when there is no layer. Both the JPEG and pixelBuffer use the crop. Frames are upright as the preview shows them, so the previewed area sits inside the photo, as on a device.

Build

  • The "Bundle SimulatorInjection" phase declares its inputs and output instead of always running. Before, incremental builds rewrote libSimCamInject.dylib inside the already-signed app without re-signing it, so codesign --verify failed with "a sealed resource is missing or invalid".
  • The extension's PRODUCT_NAME is now $(PRODUCT_BUNDLE_IDENTIFIER): sysextd requires the extension bundle's file name to equal its bundle identifier, and activation failed with "Extension not found in App bundle" otherwise. scripts/build-release.sh, the README and docs/ARCHITECTURE.md now use the new name, com.dautov.SimulatorCamera.Extension.systemextension.

Camera extension / CoreMediaIO

  • CMIOSinkClient passes a no-op queue-altered callback to CMIOStreamCopyBufferQueue. With a nil callback CoreMediaIO returns noErr but no queue, which surfaced as "CMIOStreamCopyBufferQueue failed (OSStatus 0)".
  • CameraLink requests camera access before attaching. Without TCC access the sandboxed app sees an empty CMIO device list, so the virtual camera looked "not registered".
  • CameraLink re-resolves device and stream IDs on every attach. They change when the extension restarts or is replaced, and stale IDs failed with '!str'.
  • The sink stream's producer check: when CoreMediaIO reports the client's signing ID as nil or "unknown", the extension reads it from the kernel for that pid. It accepts it only for a valid, non-ad-hoc signature from the extension's own team.

Testing

Environment: macOS 27.0.1 on Apple M1 Pro, Xcode 27, iOS 27 Simulator (iPhone 18 Pro Max, DeviceHub). Consuming app: .NET MAUI (net10.0-ios) using CommunityToolkit.Maui.Camera 6.1.0 CameraView (default camera provider, AVCapturePhotoOutput capture). These changes were what made the camera work end to end in the Simulator for this app. Before them it either found no camera or crashed when the camera page opened.

For local signing I built with my own team and bundle IDs. Those changes are not part of this PR.

  • xcodegen generate && xcodebuild -scheme SimulatorCamera build succeeds (signed with my own team; this branch also builds with CODE_SIGNING_ALLOWED=NO, and xcodegen generate reproduces the committed project.pbxproj exactly)
  • Extension activates (systemextensionsctl list shows it [activated enabled])
  • simcamctl ping / simcamctl status report a running stream: ping reports the extension reachable, and status reports sink open: yes with frames received and none rejected. It shows stream running: no, because no macOS app had the virtual camera open at the time.
  • Verified frames reach an app on an Xcode 16+ iOS Simulator (Xcode 27 / iOS 27, through the injected library: live preview, photo capture, and the MAUI app's own crop of the captured photo)

Also tested with small Objective-C test programs run in the Simulator with the library injected:

  • Three rounds of session setup and teardown: alloc/initWithDevice: and deviceInputWithDevice:, a photo output and a preview layer each round. This crashed before the fix and runs cleanly after.
  • Photo and video output: 4 photos and 80+ video frames across two sessions, then teardown.
  • Photo size: 540×720 with a portrait preview layer, 540×720 at rotation 90 with no layer, 960×720 at rotation 0 (720p source frames).

XPC contract impact

  • No change to Shared/XPCContract.swift (that file no longer exists; the control contract in Shared/CameraContract.swift is unchanged too)
  • Changed — app, extension, and simcamctl all updated together

Checklist

  • Added/updated entry in CHANGELOG.md under [Unreleased]
  • No private APIs introduced. Two to flag:
    • The signing-ID fallback calls csops (exported by libsystem_kernel but not in the public headers). SecCodeCheckValidity re-reads the client's executable from disk, which the extension sandbox denies. A file-read exception entitlement made the extension crash at sandbox init.
    • SCConnection overrides the private -[AVCaptureConnection sourceDevice], like the existing private -mediaType override. This is in the Simulator-only injection library.
  • Consuming iOS apps still need zero imports and zero project changes (the library is still loaded with DYLD_INSERT_LIBRARIES. One note: the VS Code .NET MAUI debugger can't pass environment variables to the app, so in our MAUI app we dlopen the dylib at startup in Debug Simulator builds instead. That's an app-side choice, not something this PR requires.)

hflexgrig and others added 3 commits October 17, 2026 23:20
- Give the -init swizzles init-family ownership (consume self, return +1).
  As plain ARC functions they returned +0, so callers of
  -[AVCaptureDeviceInput initWithDevice:error:] over-released the fake
  input and crashed in objc_autoreleasePoolPop.
- Skip AVFoundation's -dealloc for the fake objects, which were never
  initialised; -[AVCaptureConnection dealloc] crashed on them.
- Report the fake device as the fake connection's sourceDevice and answer
  -canAddOutput: directly for fake sessions; the stock check crashed when
  a second output was added.
- Return 4:3 photos cropped from the frame centre, portrait when the
  preview is portrait, like a real iPhone. Raw landscape frames broke
  apps that crop by proportion in portrait.
- Declare the bundling phase's inputs and outputs so Xcode re-signs the
  app after the dylib changes; incremental builds left an invalid seal.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
- Name the extension bundle after its bundle identifier, which sysextd
  requires; update build-release.sh and the README to match.
- Pass a no-op queue-altered callback to CMIOStreamCopyBufferQueue. With
  a nil proc CoreMediaIO returns noErr but no queue.
- Request camera access before attaching: without it the sandboxed app
  sees an empty CMIO device list and the virtual camera looks
  unregistered.
- Re-resolve device and stream IDs on every attach; they change when the
  extension restarts and stale IDs fail with '!str'.
- When CoreMediaIO reports the sink producer's signing ID as nil or
  "unknown", read it from the kernel (csops), accepting only a valid,
  non-ad-hoc signature from the extension's own team.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…elog

The extension bundle is now named after its bundle identifier, so
build-release.sh looks for com.dautov.SimulatorCamera.Extension.systemextension,
and the README and architecture diagrams show that name. Add the fixes from
this branch to the [Unreleased] changelog.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@dautovri
dautovri merged commit a3b9b2b into dautovri:main Oct 20, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants