Repository navigation
media-playback: Own FFmpeg options - #733
Merged
zackees merged 2 commits intoOct 14, 2026
Merged
Conversation
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.
Status: draft. Opened so upstream PR CI can run it. The branch
fix/media-source-options-lifetimeis two commits above5f55dc0df49bad85def95aae9ab82b6e656d9a93. Upstreammasterat the time of writing is8af78cc7750f8308b7d1948257bced056ca0532a; the branch merges into it cleanly with no conflicts.5d4025fc67c51fab10c6ba112eb6ca8ff425b2ec—test: Check that media playback owns its FFmpeg options (RED)(tests only)e412fc8947f6cf0b9c30201fde5035d39928836c—media-playback: Own FFmpeg options (GREEN)Problem
Updating an FFmpeg source can free the settings string while the old media worker is still parsing it. The worker reads freed memory in FFmpeg's option parser, which is a heap use-after-free under ASan and can crash or read garbage options without it. No issue has been opened yet, so this draft has no issue link.
Root cause
At the base commit,
plugins/obs-ffmpeg/obs-ffmpeg-source.c:317passess->ffmpeg_optionsto media playback.ffmpeg_source_update()frees that string at:463-464, before its conditional media destruction at:491-493. Meanwhileshared/media-playback/media-playback/media.c:1008stores the pointer without copying it;:991starts the worker, which parses the pointer later at:739-742. The media destroy path joins the worker at:1050, so the source can free the string before that join completes.Fix
mp_media_init()now copies the options string (bstrdup) andmp_media_free()releases that copy, aftermp_kill_thread()has joined the worker. The source's own string can therefore be freed at any time.The full-decode (cache) path goes through the same
mp_media_init(), so its embeddedmp_media_talso owns a copy.mp_cache_decode()releases it throughmp_media_free()(both on success and at itsfail:label). For the cases where decode never runs,mp_cache_free()now callsmp_media_free(&c->m)unconditionally instead of only whenc->m.fmtis set.mp_media_free()is safe on an already-freed or zeroed struct (mp_media_init()already relies on that when it fails part-way), so there is no double free on any path.That cache change also closes a pre-existing leak on
mp_cache_init()'s failure path: whenmp_media_init2()fails before the file is opened,fmtis NULL, so the old code skippedmp_media_free()and leaked the half-initialised media (the semaphore, a copy of the path and now the options copy). It is the same one-line change that frees the new options copy there, so it is included rather than split out.mp_cache_t::ffmpeg_optionsis deliberately left alone. It is assigned inmp_cache_init()and never read anywhere (its only references are that assignment and the field declaration), so copying and freeing it would be dead code with nothing to test. The cache also never had the race: it parses synchronously insidemp_cache_init()on the caller's thread, while the caller's string is still valid.Options considered:
mp_mediaonly (implemented): fixes the asynchronous worker path with the smallest change.mp_cache_t::ffmpeg_options: rejected. The field is never read, so the copy cannot be exercised and would only add a double-free surface.ffmpeg_source_update()also replaces the settings string when it keeps the existing media object, so this requires retaining old strings or restarting playback for unrelated updates.Option A is recommended.
Evidence
RED → GREEN:
test_media_source_options_lifetime, a Linux-only cmocka test (it uses GNU ld--wrap, so it is skipped on macOS and Windows). It needs no sanitizer. The test executable compilesmedia-playbackitself and wraps three calls inside it: FFmpeg'sav_dict_parse_string(records the pointer and text the parser receives),bfree(counts frees of the pointers under watch) andpthread_join(releases a held parser only when the worker is joined). No libobs test hook or export is added. The four cases:media_worker_keeps_options_until_parse_finishes: holds the worker inside the parser, frees the caller's string the wayffmpeg_source_update()does, then destroys the media object. The parser is only released whenpthread_joinruns, so the test asserts the copy was not freed while the worker was still in the parser (this is the free-before-join ordering), that the parser was given a different pointer from the caller's buffer with the same text, that the real parse succeeds, and that the copy is freed exactly once.cache_init_failure_releases_options_copy:full_decodeon/dev/null, somp_cache_init()fails at open (fmtNULL). Asserts the owned copy, freed exactly once, and that the caller's string is never freed by the library.cache_decode_releases_options_copy:full_decodeon a small generated WAV file, so the cache initialises and decodes. Same assertions.cache_open_then_init_failure_releases_options_copy:full_decodeon a WAV with an unknown format tag, so the file opens (fmtis set) and then init fails. Same assertions; this is the case that would catch a double free of the copy inmp_cache_free().RED (tests only, on the base source), Linux GCC 14,
RelWithDebInfo,-Werror, no sanitizer,ENABLE_RUST_LIBOBS=OFF:[ PASSED ] 0 test(s), all four cases fail (3 of 3 runs). Case 1 fails with "media playback parsed the caller's options buffer" and the others with the parser pointer equal to the caller's pointer.GREEN, same build:
[ PASSED ] 4 test(s), 20 of 20 repeated runs and throughctest.Mutants run against the final test in the plain build (each killed, 3 of 3 runs):
mp_media_init()bfree(media->ffmpeg_options)inmp_media_free()mp_cache_free()to freeing only whenfmtis setmp_kill_thread()(before the join)c->ffmpeg_options) inmp_cache_free()bfree(c->m.ffmpeg_options)beforemp_media_free(&c->m)bfree(c->m.ffmpeg_options)beforeif (c->m.fmt) mp_media_free(...)full_decodeis setAn earlier ASan run of the original version of this test showed the real use-after-free (
heap-use-after-free READ of size 13 ... av_get_token ... av_dict_parse_string).Sanitizers: under GCC ASan+UBSan with
ASAN_OPTIONS=detect_leaks=1all four cases pass and the only report is one 104-byteAVPacketleak allocated inmp_media_reset()and reached frommp_cache_decode()(case 3). That leak is pre-existing and unrelated to the options string:mp_media_next_packet()does not return the packet on a read error or EOF. It is fixed byfix/media-packet-eof-leak. With that branch's one-line fix applied locally on top of this branch, the test exits 0 under ASan+LSan (5 of 5 runs). This PR should land after the EOF-leak PR, otherwise the Sanitizers workflow reports this test as failing for that unrelated leak; the dependency is also noted in the test'stests.dfragment. No LSan suppressions are added.Fork CI: not run. Our self-hosted CI is down (see our note on #699), so this relies on upstream PR CI. The test is Linux-only, so only the Linux jobs run it.
Real runs: not run. The headless test synchronizes the media worker at the parser and checks the pointer lifetime directly; a full OBS/websocket run is not done yet.
Compatibility & risk
No public function signature or option parsing behavior changes. Every media object makes one small extra allocation (the options copy), including the cache's embedded
mp_media_t, and it is released on every path (normal free, init failure, and the cache failure cases above).mp_cache_free()now callsmp_media_free()on a half-initialised cache, which also frees state that used to leak. Tested configuration: Linux GCC 14 withENABLE_RUST_LIBOBS=OFFand=ON(the test target, RED and GREEN), plus the ASan+UBSan+LSan build above. macOS and Windows do not run this test and were not tested.Status
Status: draft. Opened so upstream PR CI can run it. It should land after the EOF-leak PR (for a sanitizer-clean run).
How this was tested
test_media_source_options_lifetimefails 4 of 4 cases on the base source and passes 4 of 4 on GREEN, in a plain Linux GCC 14RelWithDebInfo -Werrorbuild with no sanitizer (ENABLE_UNIT_TESTS=ON), in bothENABLE_RUST_LIBOBS=OFFand=ON. The mutants listed under Evidence were each killed.AVPacketleak described above. With the EOF-leak fix applied locally it exits 0.clang-format22.1.3--dry-run --Werroron the changed C files andgersemi0.25.0--checkwith the repo.gersemircon the newtests.dfragment are clean.Thank you to the obs-rust maintainers and contributors for building this project in the open. In the American spirit of free expression and individual independence, and with the principle of judging contributions on their merit alone, this PR is a small piece of what independent people can build when they come together.