Skip to content

mac-capture: Wait for SCK stop and fence callbacks before free - #125

Merged
zackees merged 2 commits into
obs-rust:masterfrom
chriscoveries:fix/93-sck-stop-uaf
Oct 11, 2026
Merged

zackees merged 2 commits into
obs-rust:masterfrom
chriscoveries:fix/93-sck-stop-uaf

Conversation

@chriscoveries

Copy link
Copy Markdown
Contributor

sck_video_capture_destroy/sck_audio_capture_destroy issued an async stopCaptureWithCompletionHandler, then released the delegate and bfreed screen_capture while ScreenCaptureKit could still deliver didOutputSampleBuffer:/didStopWithError: on the output queue — each dereferencing the raw self.sc back-pointer into freed memory.

  • wait on an os_event for the stop completion, matching the start path
  • add a delegate-owned sc_mutex: every callback holds it for its whole body and re-checks self.sc; teardown nils delegate.sc under the same lock after the stream stops, so in-flight callbacks finish on live memory and later ones exit early

Applied to both video and audio capture sources.

Fixes #93

@zackees

zackees commented Oct 10, 2026

Copy link
Copy Markdown
Member

Thanks. Before this can merge, the project needs a RED→GREEN automated test. ScreenCaptureKit itself can't run in CI, but the ordering logic can: factor the stop/fence wait and the free decision into a small plain-C (or Objective-C-free) helper driven by fake callbacks, and add a cmocka test that fails on the current code (frees before the callbacks fire) and passes with the fix, as #109 did for PipeWire. Avoid fail_msg().

@chriscoveries

Copy link
Copy Markdown
Contributor Author

Thanks @zackees, done the way you suggested. Sorry this needed a second round; the first version didn't come with its own test.

  • RED → GREEN: the stop wait and the decision to free are now a plain-C helper (plugins/mac-capture/mac-sck-stop.c). test/cmocka/test_sck_stop_wait.c drives it with fake completion events for three cases: completed, never signalled (timeout), and os_event_init failing. The RED commit fails on the old logic and GREEN passes.
  • One behaviour change worth flagging: the wait is now bounded at 2 s (SCK_STOP_TIMEOUT_MS). If ScreenCaptureKit never confirms the stop, we log it, leak only the small stop event (the completion block may still signal it later), and finish the normal teardown, including clearing the delegate's sc under sc_mutex. Before, this path could hang OBS indefinitely.
  • Paths covered: video and audio capture.

Evidence

  • CI on our runners (lint, Rust Tiers 1–3, Docker Tier 2 OFF/ON) passed at 6defb47: https://github.com/chriscoveries/obsrust-fork/actions/runs/38102160743
  • Live run on macOS 27 (arm64) with a build of this branch, driven over obs-websocket: 40 cycles of adding and removing ScreenCaptureKit sources. No crash, no hang, no stop timeouts, and a clean exit.
  • Limit: in that run only the audio-capture sources actually streamed. The display and window sources were created without a target, so they never started, and their stop path wasn't exercised live; the unit test covers it. The 2 s timeout path can't be triggered on demand, so it's covered only by the unit test.

@chriscoveries
chriscoveries marked this pull request as ready for review October 11, 2026 05:54
chriscoveries and others added 2 commits October 10, 2026 23:01
The ScreenCaptureKit destroy paths free the capture and the stop event
without waiting for stopCaptureWithCompletionHandler, so output and stop
callbacks can still run on freed memory (#93). Adds the plain-C wait and
free decision in mac-sck-stop.[ch] as a stub that never waits and always
frees, and test_sck_stop_wait, which drives it with real os_event
objects. 3 of 4 tests fail.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
sck_video_capture_destroy and sck_audio_capture_destroy now wait, with a
timeout, on an os_event for the stop completion, as the start path does.
A delegate-owned sc_mutex is held by every output and stop callback for
its whole body, and the callback re-checks self.sc. Teardown nils
delegate.sc under the same lock once the stream stops, so in-flight
callbacks finish on live memory and later ones return early. On timeout
only the stop event is leaked, because its completion can still fire,
and teardown completes.

Fixes #93

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Local-Gate: v1 tree=35006dd3da66707269135c5bbd2537f7932adced secs=102 lanes=lint:run,rust-test:run,tier2-linux:run
Ci-Attestation: {"at":1791698615,"gate":"rust/all/fmt","host":"linux-x86_64","key":"65c83ff6730b90bb3a4a89a824863f0011918f8f49514ec949471975367863e9","lane":"lint","parents":["85d86e025d94bec6d9871c10cf1b49be0444634d"],"secs":11,"stamp":"7b67d7151da9b98b65db1bc8a745ffd8","tree":"35006dd3da66707269135c5bbd2537f7932adced","v":1,"via":"run"}
Ci-Attestation: {"at":1791698615,"gate":"rust/x86_64-unknown-linux-gnu/clippy","host":"linux-x86_64","key":"65c83ff6730b90bb3a4a89a824863f0011918f8f49514ec949471975367863e9","lane":"lint","parents":["85d86e025d94bec6d9871c10cf1b49be0444634d"],"secs":11,"stamp":"e34c35a684486d59a79fb42915bc1048","tree":"35006dd3da66707269135c5bbd2537f7932adced","v":1,"via":"run"}
Ci-Attestation: {"at":1791698615,"gate":"rust/x86_64-unknown-linux-gnu/test","host":"linux-x86_64","key":"ef3b1129fe2ce339d95ce9308db0409d12cf2583e1c0b497fa38ad1ed4cabdb2","lane":"rust-test","parents":["85d86e025d94bec6d9871c10cf1b49be0444634d"],"secs":27,"stamp":"738f56b24f8838906be278603b35ef90","tree":"35006dd3da66707269135c5bbd2537f7932adced","v":1,"via":"run"}
Ci-Attestation: {"at":1791698615,"gate":"cpp/linux-x64/tier2","host":"linux-x86_64","key":"a66a6dbca36c004bf605335daa33ab914b4859a6c16b948b44daa15d2a9b0dca","lane":"tier2-linux","parents":["85d86e025d94bec6d9871c10cf1b49be0444634d"],"secs":64,"stamp":"a7b64dae2f78a93686b82a4777df945b","tree":"35006dd3da66707269135c5bbd2537f7932adced","v":1,"via":"run"}
@zackees

zackees commented Oct 11, 2026

Copy link
Copy Markdown
Member

I restructured this into RED then GREEN on current master. You're kept as the author of both commits. The new head is 4664b0b59. plugins/mac-capture/ is byte-identical to your 6defb4778.

  • RED 85d86e025 adds mac-sck-stop.[ch] as a stub that never waits and always frees, plus test_sck_stop_wait, registered in test/cmocka/tests.d/test_sck_stop_wait.cmake (the infra: Split shared registration lists into per-unit fragments #168 layout) instead of CMakeLists.txt. Tier 2 passed 64 of 65. test_sck_stop_wait failed 3 of 4 (test_wait_never_fires_times_out, test_wait_null_event_is_error, test_free_decision).
  • GREEN e0ea2ed74 adds the real helper and the video/audio destroy and callback changes (your six commits squashed). Tier 2 passed 65 of 65 with ENABLE_RUST_LIBOBS OFF and 65 of 65 with it ON. Local gate: lint, rust-test and tier2-linux all passed.
  • I dropped test_timeout_leaks_event_but_teardown_proceeds. It asserted a local variable it had just set to true, so it couldn't fail.

Limits: the test covers the wait and free decision only. The sc_mutex fence in the Objective-C callbacks can't run outside macOS, so the macOS CI build is the only check on the .m changes. Merging once CI, including macOS, is green.

@zackees
zackees force-pushed the fix/93-sck-stop-uaf branch from 6defb47 to 4664b0b Compare October 11, 2026 06:03
@zackees
zackees merged commit 9602d20 into obs-rust:master Oct 11, 2026
23 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

[crash] mac-capture SCK: delegate callbacks dereference freed screen_capture on teardown

2 participants