Skip to content

iOS: own every object a block hands across threads; no ARC in native checks - #6372

Open
shai-almog wants to merge 6 commits into
masterfrom
ios-mrc-block-ownership
Open

shai-almog wants to merge 6 commits into
masterfrom
ios-mrc-block-ownership

Conversation

@shai-almog

Copy link
Copy Markdown
Collaborator

Builds on and supersedes #6370 (its two commits are included).

The port builds with CLANG_ENABLE_OBJC_ARC = NO. Without ARC, a __block object variable retains nothing, so an autoreleased value stored from a completion handler or main-queue block is freed before the waiting thread reads it. ParparVM surfaces that crash as a NullPointerException with no message.

Fixes

  • CN1OidcBrowser.m: the callback URL and error are now copied/retained (from iOS: retain the sign-in sheet's callback URL in CN1OidcBrowser.m #6370). A late handler after the one-hour timeout no longer releases a newer flow's session; a new flow cancels any abandoned one first.
  • CN1WebAuthn.m, CN1AR.m, IOSNative.m: results passed back through __block variables are now owned:
    • IOSNative.m: openURL, WKWebView snapshot and the audio-recorder error strings.
    • CN1WebAuthn.m: the result and error-message strings.
    • CN1AR.m: the anchor identifier.
  • CN1SmartHome.m: the mode-write key was autoreleased outside a block and read later in a completion handler. It is now owned.
  • Microphone permission race (IOSNative.m): requestRecordPermission: was called inside the main-queue block, and the native returned before the answer arrived. A first prompt produced no recorder, a denial was never reported, and createMediaRecorder could wait forever. Permission is now awaited on the calling thread before the main-queue work, and a denial throws IOException. A leaked NSFileManager and settings dictionary are released.

Why QA missed it

identity-stack.yml linted these files with -fobjc-arc, the one model under which the original code is correct.

Gates

  • scripts/check-ios-mrc-block-writes.py (in source-gates.yml, with a unit test):
    • Fails an unowned store into a __block object variable inside a block that is read after the block.
    • Fails an unowned store outside a block that a later block reads.
    • Fails any script or workflow that compiles with -fobjc-arc across the board.
    • Escape hatch: // mrr: <reason> on the line.
    • It reports master's CN1OidcBrowser.m and CN1SmartHome.m.
  • All native test, lint and bench scripts now compile with -fno-objc-arc. Choosing the flag per file for "requires ARC" natives is still allowed.

Testing

  • scripts/pre-push-gates.sh --full: clean. scripts-ios is NOT RUN locally (CI only).
  • identity-stack.yml clang lint matrix (iphoneos/watchos x stubs/full) with -fno-objc-arc: clean.
  • Text-input-state, mac native font, titlebar and camera preview native tests all pass without ARC.
  • The crypto-gpu vectors also run without ARC.
  • Edited natives compile in every variant of check-ios-private-api.py.

🤖 Generated with Claude Code

claude and others added 6 commits December 1, 2026 14:34
CN1OidcBrowser.m was written for ARC, but the port compiles with
CLANG_ENABLE_OBJC_ARC = NO. ASWebAuthenticationSession's completion handler
stored [callbackURL absoluteString] -- autoreleased into the main thread's
pool -- in a __block variable and signalled the waiting thread, which then
called fromNSString on it after the pool had drained. That use-after-free
raised EXC_BAD_ACCESS, which ParparVM's signal handler surfaces as a
NullPointerException with no message, so SystemBrowser.authenticate and
OidcClient.authorize failed on device after the person approved, with
OidcException transport_error "Native sign-in sheet failed: null".

Under manual reference counting:
- the callback string is copied and the error retained on the main thread,
  and both are released by the waiting thread once it has read them;
- the session and its presentation context, held at +1 from -alloc, are
  autoreleased when the flow clears them (inside the session's own
  completion handler, so the session outlives the handler's return) instead
  of being leaked.

Found in a TestFlight build of an app that signs in with OidcClient: every
sign-in failed with that transport_error until the generated project was
patched the same way.
The iOS native-source lint compiles with -fobjc-arc, where explicit
retain/release/autorelease are errors. Guard them with
__has_feature(objc_arc), so the file is correct both in the product build
(ARC off) and under ARC.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01SZMcbmatyXu26VSMeHqWHJ
The port builds without ARC. CN1OidcBrowser.m was not the only native that
stored an autoreleased value into a __block variable from a block and read
it after the block returned:

- IOSNative.m: the audio recorders' error strings (freed by the block's own
  POOL_END before the IOException was built), openURL's string (retained
  under #ifdef CN1_USE_ARC -- backwards), the WKWebView snapshot image, and
  the watch screenshot's failure log.
- CN1AR.m: createAnchorFromHit's anchor id; both anchor methods also leaked
  the ARAnchor.
- CN1SmartHome.m: the mode-write key read in a later HomeKit completion, and
  the queued array walked after removeObjectForKey released it.
- CN1WebAuthn.m: results alive only because the delegate leaks; now copied.

How it passed: identity-stack.yml linted OIDC/AppleSignIn/WebAuthn with
-fobjc-arc, the one model under which the code is correct. It now compiles
them without ARC, as the target does.

New gate scripts/check-ios-mrc-block-writes.py (source-gates.yml, with a
unit test) reports an unowned store into a __block object variable inside a
block that is read after the block. It flags master's CN1OidcBrowser.m.

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

The audio recorders asked requestRecordPermission: from inside their
main-queue block and returned before the asynchronous answer arrived, so a
first prompt produced no recorder and a denial was never reported (the Java
side then waited forever). The permission is now awaited on the calling
thread, before the main-queue work, and a denial throws IOException.

The __block gate also reports an unowned value stored outside a block that a
later block reads (CN1SmartHome.m's shape), and fails any script or workflow
that compiles with -fobjc-arc: every test, lint and bench now builds without
ARC, as the app does.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
After the one-hour timeout Java can start a second sign-in; the first
session's handler then fired and autoreleased the second's session and
context, which its own handler released again. A handler now clears only
its own session, and a new flow cancels any abandoned one first. The ARC
flag check also catches a flag followed by a file name.

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

# Conflicts:
#	Ports/iOSPort/nativeSources/CN1OidcBrowser.m
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