Repository navigation
Fix/simulator injection and extension - #10
Merged
dautovri merged 3 commits intoOct 20, 2026
Merged
Conversation
- 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>
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
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
-initswizzles returned objects with the wrong ownership, AVFoundation's-deallocran on fake objects that were never initialised, and adding a second output crashed. The photo issue was thatcapturePhotoreturned 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)-initswizzles (AVCaptureDeviceInput -initWithDevice:error:,AVCaptureVideoPreviewLayer -initWithSession:/-initWithSessionWithNoConnection:) now use init-family ownership (ns_consumedself,ns_returns_retained). As plain ARC C functions they returned +0, so callers over-released the fake input and crashed inobjc_autoreleasePoolPop; the layer inits leaked.SCDevice,SCDeviceInput,SCConnection,SCQRObject,SCPhoto) get a-deallocthat skips AVFoundation's teardown and goes straight toNSObject'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.SCConnectionreturns 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.pixelBufferuse the crop. Frames are upright as the preview shows them, so the previewed area sits inside the photo, as on a device.Build
libSimCamInject.dylibinside the already-signed app without re-signing it, socodesign --verifyfailed with "a sealed resource is missing or invalid".PRODUCT_NAMEis 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 anddocs/ARCHITECTURE.mdnow use the new name,com.dautov.SimulatorCamera.Extension.systemextension.Camera extension / CoreMediaIO
CMIOSinkClientpasses a no-op queue-altered callback toCMIOStreamCopyBufferQueue. With a nil callback CoreMediaIO returnsnoErrbut no queue, which surfaced as "CMIOStreamCopyBufferQueue failed (OSStatus 0)".CameraLinkrequests camera access before attaching. Without TCC access the sandboxed app sees an empty CMIO device list, so the virtual camera looked "not registered".CameraLinkre-resolves device and stream IDs on every attach. They change when the extension restarts or is replaced, and stale IDs failed with'!str'.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,AVCapturePhotoOutputcapture). 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 buildsucceeds (signed with my own team; this branch also builds withCODE_SIGNING_ALLOWED=NO, andxcodegen generatereproduces the committedproject.pbxprojexactly)systemextensionsctl listshows it[activated enabled])simcamctl ping/simcamctl statusreport a running stream:pingreports the extension reachable, andstatusreportssink open: yeswith frames received and none rejected. It showsstream running: no, because no macOS app had the virtual camera open at the time.Also tested with small Objective-C test programs run in the Simulator with the library injected:
alloc/initWithDevice:anddeviceInputWithDevice:, a photo output and a preview layer each round. This crashed before the fix and runs cleanly after.XPC contract impact
Shared/XPCContract.swift(that file no longer exists; the control contract inShared/CameraContract.swiftis unchanged too)simcamctlall updated togetherChecklist
CHANGELOG.mdunder[Unreleased]csops(exported by libsystem_kernel but not in the public headers).SecCodeCheckValidityre-reads the client's executable from disk, which the extension sandbox denies. A file-read exception entitlement made the extension crash at sandbox init.SCConnectionoverrides the private-[AVCaptureConnection sourceDevice], like the existing private-mediaTypeoverride. This is in the Simulator-only injection library.DYLD_INSERT_LIBRARIES. One note: the VS Code .NET MAUI debugger can't pass environment variables to the app, so in our MAUI app wedlopenthe dylib at startup in Debug Simulator builds instead. That's an app-side choice, not something this PR requires.)