Skip to content

libobs: Keep only the public canvas flags, and free a canvas whose context fails - #2151

Merged
zackees merged 2 commits into
obs-rust:masterfrom
rdoupe:fix/canvas-flags
Oct 24, 2026
Merged

zackees merged 2 commits into
obs-rust:masterfrom
rdoupe:fix/canvas-flags

Conversation

@rdoupe

@rdoupe rdoupe commented Oct 24, 2026

Copy link
Copy Markdown
Collaborator

Branch: fix/canvas-flags (base origin/master af31b65)

Fixes #2141

What and why

Two small bugs in canvas creation and load (libobs/obs-canvas.c.h). Upstream has the same code.

  • Internal and unknown flag bits. obs_canvas_create(), obs_canvas_create_private() and obs_load_canvas() cleared only MAIN from the flags they were given. A plugin, a script or a scene collection (edited by hand or written by another tool) could set the internal REMOVED bit (1 << 31). The canvas then reported obs_canvas_removed() from the start, and obs_canvas_remove() returned early without its canvas_remove/remove signals, so the frontend and other holders never let it go. Unknown bits were kept and written back by obs_save_canvas(). All three entry points now keep only ACTIVATE | MIX_AUDIO | SCENE_REF | EPHEMERAL, through one constant, PUBLIC_CANVAS_FLAGS, next to REMOVED. The main canvas still gets MAIN | PROGRAM from obs_create_main_canvas(), which does not go through the mask.
  • Leak on context init failure. obs_canvas_create_internal() returned NULL without freeing the canvas when obs_context_data_init() failed; the two later failure paths already freed it. It now calls bfree(canvas). obs_context_data_init() already frees the context itself on failure (obs_context_data_free()), so only the canvas is freed.

No caller in the tree passes anything beyond the public flags: the frontend creates canvases only through obs_frontend_add_canvas() (flags from the calling plugin) and loads them from what obs_save_canvas() wrote, which never includes REMOVED (a removed canvas is not saved). obs-websocket and the in-tree plugins only read flags. No Rust port reads canvas flags. Every valid combination is unchanged, so no existing scene collection or plugin behaves differently.

RED → GREEN

  • RED commit: e7467c6 test: Cover canvas flags from callers and scene collections, and a failed context init. New test_canvas_flags (Linux, real libobs, public API only; skips without a display). It is a new suite rather than a case in test_context_names (names only) or test_canvas_destroy_scene_refs (one race), because it covers all three entry points, saving, removal and a forced allocation failure.
    • Loads (public and private) and creates (obs_canvas_create(), obs_canvas_create_private()) canvases whose flags include REMOVED, 1 << 5, 1 << 30 and MAIN, plus a load of -1. Each checks obs_canvas_get_flags(), obs_canvas_removed(), the flags obs_save_canvas() writes, and that obs_canvas_remove() fires the canvas's remove signal (and obs canvas_remove for a public canvas), so an owner that releases on that signal, as the frontend does, lets it go and it leaves the canvas list (obs_get_canvas_by_uuid()/_by_name(), weak ref).
    • All 16 combinations of the public flags (the PROGRAM, PREVIEW and DEVICE presets among them) through all four paths are unchanged, MAIN alone is still dropped, and the main canvas keeps MAIN | PROGRAM and cannot be removed.
    • The leak: the test exports its own pthread_mutex_init() (-Wl,--export-dynamic-symbol, link_options in test-graph.json) that fails on demand for the test thread, and counts bmalloc allocations around the failed create, load and private create.
    • On RED: 6 of 7 fail on the C libobs and on libobs-c-reference (the five bad-flag cases, e.g. 3221225514 != 10 for REMOVED | 1<<30 | 1<<5 | MAIN | ACTIVATE | SCENE_REF; and the leak case, 1 != 0 allocations). On the hybrid libobs 5 of 7 fail: the leak case passes there, because the Rust signal and proc handlers never fail, so obs_context_data_init() cannot fail and the create fails later at the canvas's sources mutex, a path that always freed the canvas. test_valid_flags_unchanged passes on RED everywhere, as it should.
    • LeakSanitizer (LD_PRELOAD=liblsan.so, run on 38747e880 / 414774a2d before the final rebase) on RED, C libobs and libobs-c-reference: a direct leak from bzalloc in obs_canvas_create_internal (obs-canvas.c.h:139), called from test_context_init_failure_frees_the_canvas. On GREEN: no leaks reported on any of the three.
  • GREEN: cceb98f (rebased on af31b65). test_canvas_flags 7/7 on the C libobs, the hybrid libobs and libobs-c-reference. Every existing cmocka test that mentions canvases (40 others) passes on all three (37 run directly against each library; test_multitrack_ladder, test_slideshow_size and test_ws_scene_count through ctest on the C and hybrid lanes). Exports: 1766 on all three libraries, identical sets. --test-diff origin/master: 3362 run, 3362 passed, 4 skipped; clang-format clean.
  • Before the final rebase (RED 38747e880, GREEN 414774a2d on 6bae297; same diff): the same RED and GREEN results, plus 10 of 10 repeat runs of test_canvas_flags on each library, and full ctest -j4 (private Xvfb -noreset, dbus-run-session, fresh HOME, ulimit -c 0, TMPDIR unset): C lane 441/441, hybrid lane 454/454, no ctest skips; inside cmocka only open_crashes_without_obs_core and test_hex_float_values_ignore_comma_locale skip, on both lanes. One run of test_group_hide_transition_audio failed on the hybrid lane under machine load (known flake Flaky: real-time audio cmocka tests lose audio under machine load (test_vlc_playlist_gap, test_group_hide_transition_audio) #838) and passed on its rerun and in both full ctests. uv run ci/local_gate.py --lane lint and --lane cross pass; --test-diff origin/master 3290/3290, 4 skipped.
  • Real app: no UI, protocol or logging change; not run.

Checklist

  • Base is master
  • I claimed the issue, or no one else has an open PR for it
  • Characterization test (if the unit had none), then RED, then GREEN commits
  • User-visible change: its line in docs/features.md is added or updated
  • (Optional) Local gate passed and the pushed head is stamped; otherwise PR CI runs the full suite
  • Warning or flag changes (-W..., ratchets): none
  • Intentional differences from C listed in the parity test header and rust/README.md (ports): not a port

…iled context init

obs_canvas_create(), obs_canvas_create_private() and obs_load_canvas()
keep the internal REMOVED bit and unknown bits from their caller. Such a
canvas reports obs_canvas_removed() from the start, obs_canvas_remove()
returns early without its signal, and unknown bits are saved back.

test_canvas_flags loads and creates canvases with REMOVED, unknown bits
and MAIN set, and checks their flags, their saved flags, and that
obs_canvas_remove() signals the removal so the owner lets the canvas go
and it leaves the canvas list. Every combination of the public flags,
the presets among them, stays unchanged, and the main canvas keeps
MAIN | PROGRAM.

It also interposes pthread_mutex_init() for its own thread to make
obs_context_data_init() fail, and counts bmalloc allocations around the
failed create: obs_canvas_create_internal() leaks the canvas there. The
hybrid libobs's Rust signal and proc handlers cannot fail, so there the
create fails later, on a path that already frees the canvas.

Refs obs-rust#2141
…ntext fails

obs_canvas_create(), obs_canvas_create_private() and obs_load_canvas()
cleared only MAIN from the flags they were given. A plugin, a script or a
scene collection could set the internal REMOVED bit, which made the canvas
count as removed from the start, so obs_canvas_remove() returned without
signalling its removal; unknown bits were kept and saved back.

All three now keep only ACTIVATE, MIX_AUDIO, SCENE_REF and EPHEMERAL
(PUBLIC_CANVAS_FLAGS, next to REMOVED). The main canvas keeps
MAIN | PROGRAM, and every valid combination is unchanged.

obs_canvas_create_internal() also returned without freeing the canvas
when obs_context_data_init() failed; it frees it now.
obs_context_data_init() frees the context itself on failure, so only the
canvas is freed.

Fixes obs-rust#2141
@rdoupe
rdoupe requested a review from zackees as a code owner October 24, 2026 13:20
@zackees
zackees added this pull request to the merge queue Oct 24, 2026
Merged via the queue into obs-rust:master with commit fe75932 Oct 24, 2026
35 checks passed
@rdoupe rdoupe mentioned this pull request Oct 27, 2026
5 of 7 tasks
rdoupe added a commit that referenced this pull request Oct 27, 2026
Behaviour-neutral extraction before the Rust port. The flag rules and
setter checks of obs-canvas.c.h move to libobs/obs-canvas-rules.c.h,
as seventeen functions on plain values declared in the new
libobs/obs-canvas-rules.h without EXPORT (hidden, as
-fvisibility=hidden keeps them):

- _main_flags() and _public_flags(): MAIN | PROGRAM for
  obs_create_main_canvas(), and the public mask of obs_canvas_create(),
  obs_canvas_create_private() and obs_load_canvas(), which still casts
  the saved int64 to uint32_t itself;
- _main_view() and _mix_audio(): the view type and the mix's
  mix_audio, in obs_canvas_create_internal() and
  obs_canvas_reset_video_internal();
- _public(): a canvas that is not private is named, unnamed, renamed
  through the hash table and signalled on obs->signals
  (canvas_dosignal(), obs_canvas_create_internal(),
  obs_canvas_destroy(), obs_canvas_set_name());
- _holds_scenes(): SCENE_REF, in obs_canvas_destroy(),
  obs_canvas_insert_source() and obs_canvas_remove_source();
- _saves(): neither EPHEMERAL nor REMOVED;
- _size_set() and _format_set(): obs_canvas_has_valid_video_info();
- _resets(), _global_signals(), _channel_ok(), _name_empty(),
  _renames(), _removable(), _mark_removed() and _removed(): the checks
  of obs_canvas_reset_video(), obs_canvas_rename_source(),
  obs_canvas_set_channel(), obs_canvas_set_name(), obs_canvas_remove()
  and obs_canvas_removed().

Split per read, as #2153 did. canvas->flags is read without a lock and
obs_canvas_remove() writes it from any thread, so each check gets the
value of its own read, at its point. Every check behind a || or && is
still asked only where the C reached it: obs_source_is_scene() only on
a SCENE_REF canvas, obs_video_active() only for a canvas that may
reset, strcmp() only for a canvas that may be renamed, and each video
info field only after the one before it was set. The rest stays C at
its point: the allocations and frees, the context data calls, the
signal handler setup, the mutexes, obs_view_init() and _free(), the
mix and its list under mixes_mutex, HASH_CLEAR and the walk that takes
next before each release (the #2038 and #2049 order), every reference
and weak reference, the pointer checks (source, previous source,
canvas, mix, the "already in a canvas" step), the group walk, the
calldata, every signal, obs_source_activate() and _deactivate(),
strcmp() and the logs. The flag values, REMOVED, the public mask,
VIDEO_FORMAT_NONE and MAX_CHANNELS are copied into the header, so it
needs only stdbool.h and stdint.h; obs-canvas.c.h checks them with
_Static_assert. REMOVED and PUBLIC_CANVAS_FLAGS leave obs-canvas.c.h.

Checked in a scratch harness (not committed). It compiles
origin/master's obs-canvas.c.h verbatim, and this branch's with
obs-canvas-rules.c.h, over a stand-in libobs.pch.h whose functions log
every call in order with its arguments: the context data calls (init
may fail), signal_handler_add_array() (may fail), the mutex init (may
fail), every lock and unlock, obs_view_init() and _free(), the mix
create (may fail) and free, the mix list, HASH_CLEAR, every signal
with its calldata, the strong and weak references (a weak get may
fail), obs_source_get_ref() (may return NULL), _release(), _is_scene(),
_is_group(), _activate(), _deactivate(), obs_video_active(), the scene
walks, the obs_data reads and writes, and blog(). A channel_change
handler may replace the source in the calldata. A concurrent writer may
flip any flag bit (MAIN, ACTIVATE, MIX_AUDIO, SCENE_REF, EPHEMERAL,
REMOVED, an unknown bit), the private flag or a video info field of
any live canvas at any call. Each run makes up to sixteen random calls
of the whole canvas API (every constructor with random flags, names and
video info; obs_load_canvas() with saved flags of 0, -1, INT64_MIN,
INT64_MAX, above 32 bits and random; save, the video info check, both
resets, insert, remove and rename of scene, group and plain sources,
set_channel at 0 to 3, 63, 64, 65 and UINT32_MAX with the same, another
or no source, set_name with NULL, "", "Main", the same and other
names, remove, the getters, scene create, remove and move, refs taken
and released, the mix clears and render), then releases everything
and removes the sources left alive, as obs_free_data() does. The C++
build also logs every read and write of the flags, the private flag
and the video info fields.

6,600,000 runs: 1,000,000 each with GCC and Clang at -O0 and -O2 as C
(67,558,889 logged lines each side), 1,000,000 each with G++ at -O0
and -O2 as C++ with the logged reads (95,351,694 each side), and
200,000 each under ASan and UBSan with no recovery (GCC -O0 and Clang
-O2 as C, G++ -O1 as C++). The two logs are identical in every
configuration. gcov over 100,000 runs takes every branch of the moved
decisions both ways, but the two no caller reaches: canvas_dosignal()
with no core signal name, and obs_canvas_remove() failing to take a
reference to a canvas its caller holds.

34 deliberately broken moves were planted, and the C++ build fails all
34: each rule changed (each operand dropped or swapped, a bound off by
one, an inverted result, the REMOVED write dropped), the destroy walk
reading the flags once before it, obs_video_active() asked before the
flags and the flags read without video info, obs_source_is_scene()
asked before the flags, a video info field read early, strcmp() before
the MAIN check, the flags read before the canvas's own signal in
set_channel(), remove_source() and rename_source(), the remove check
after the reference, the private flag read once in set_name(), the
create's mix_audio read from the canvas instead of its argument, and
a second read of the flags in save. The C builds, which see reads only
through the writer, fail 29 of them.

git blame on the moved lines shows upstream commits (3d57b1f
introducing canvases, 66f4792, 1ecea56, 0fd1261) and the fork's
fe75932 (the public mask, #2151); they move as they are.

The new file joins libobs.core-source with libobs.pch.h, which
includes the new header; source-sets.json lists it. The exported
symbol sets are unchanged: 1766 on the C backend, the hybrid and
libobs-c-reference, the same set on all three, with the seventeen
functions local.

Refs #2544.
rdoupe added a commit that referenced this pull request Oct 27, 2026
Behaviour-neutral extraction before the Rust port. The flag rules and
setter checks of obs-canvas.c.h move to libobs/obs-canvas-rules.c.h,
as seventeen functions on plain values declared in the new
libobs/obs-canvas-rules.h without EXPORT (hidden, as
-fvisibility=hidden keeps them):

- _main_flags() and _public_flags(): MAIN | PROGRAM for
  obs_create_main_canvas(), and the public mask of obs_canvas_create(),
  obs_canvas_create_private() and obs_load_canvas(), which still casts
  the saved int64 to uint32_t itself;
- _main_view() and _mix_audio(): the view type and the mix's
  mix_audio, in obs_canvas_create_internal() and
  obs_canvas_reset_video_internal();
- _public(): a canvas that is not private is named, unnamed, renamed
  through the hash table and signalled on obs->signals
  (canvas_dosignal(), obs_canvas_create_internal(),
  obs_canvas_destroy(), obs_canvas_set_name());
- _holds_scenes(): SCENE_REF, in obs_canvas_destroy(),
  obs_canvas_insert_source() and obs_canvas_remove_source();
- _saves(): neither EPHEMERAL nor REMOVED;
- _size_set() and _format_set(): obs_canvas_has_valid_video_info();
- _resets(), _global_signals(), _channel_ok(), _name_empty(),
  _renames(), _removable(), _mark_removed() and _removed(): the checks
  of obs_canvas_reset_video(), obs_canvas_rename_source(),
  obs_canvas_set_channel(), obs_canvas_set_name(), obs_canvas_remove()
  and obs_canvas_removed().

Split per read, as #2153 did. canvas->flags is read without a lock and
obs_canvas_remove() writes it from any thread, so each check gets the
value of its own read, at its point. Every check behind a || or && is
still asked only where the C reached it: obs_source_is_scene() only on
a SCENE_REF canvas, obs_video_active() only for a canvas that may
reset, strcmp() only for a canvas that may be renamed, and each video
info field only after the one before it was set. The rest stays C at
its point: the allocations and frees, the context data calls, the
signal handler setup, the mutexes, obs_view_init() and _free(), the
mix and its list under mixes_mutex, HASH_CLEAR and the walk that takes
next before each release (the #2038 and #2049 order), every reference
and weak reference, the pointer checks (source, previous source,
canvas, mix, the "already in a canvas" step), the group walk, the
calldata, every signal, obs_source_activate() and _deactivate(),
strcmp() and the logs. The flag values, REMOVED, the public mask,
VIDEO_FORMAT_NONE and MAX_CHANNELS are copied into the header, so it
needs only stdbool.h and stdint.h; obs-canvas.c.h checks them with
_Static_assert. REMOVED and PUBLIC_CANVAS_FLAGS leave obs-canvas.c.h.

Checked in a scratch harness (not committed). It compiles
origin/master's obs-canvas.c.h verbatim, and this branch's with
obs-canvas-rules.c.h, over a stand-in libobs.pch.h whose functions log
every call in order with its arguments: the context data calls (init
may fail), signal_handler_add_array() (may fail), the mutex init (may
fail), every lock and unlock, obs_view_init() and _free(), the mix
create (may fail) and free, the mix list, HASH_CLEAR, every signal
with its calldata, the strong and weak references (a weak get may
fail), obs_source_get_ref() (may return NULL), _release(), _is_scene(),
_is_group(), _activate(), _deactivate(), obs_video_active(), the scene
walks, the obs_data reads and writes, and blog(). A channel_change
handler may replace the source in the calldata. A concurrent writer may
flip any flag bit (MAIN, ACTIVATE, MIX_AUDIO, SCENE_REF, EPHEMERAL,
REMOVED, an unknown bit), the private flag or a video info field of
any live canvas at any call. Each run makes up to sixteen random calls
of the whole canvas API (every constructor with random flags, names and
video info; obs_load_canvas() with saved flags of 0, -1, INT64_MIN,
INT64_MAX, above 32 bits and random; save, the video info check, both
resets, insert, remove and rename of scene, group and plain sources,
set_channel at 0 to 3, 63, 64, 65 and UINT32_MAX with the same, another
or no source, set_name with NULL, "", "Main", the same and other
names, remove, the getters, scene create, remove and move, refs taken
and released, the mix clears and render), then releases everything
and removes the sources left alive, as obs_free_data() does. The C++
build also logs every read and write of the flags, the private flag
and the video info fields.

6,600,000 runs: 1,000,000 each with GCC and Clang at -O0 and -O2 as C
(67,558,889 logged lines each side), 1,000,000 each with G++ at -O0
and -O2 as C++ with the logged reads (95,351,694 each side), and
200,000 each under ASan and UBSan with no recovery (GCC -O0 and Clang
-O2 as C, G++ -O1 as C++). The two logs are identical in every
configuration. gcov over 100,000 runs takes every branch of the moved
decisions both ways, but the two no caller reaches: canvas_dosignal()
with no core signal name, and obs_canvas_remove() failing to take a
reference to a canvas its caller holds.

34 deliberately broken moves were planted, and the C++ build fails all
34: each rule changed (each operand dropped or swapped, a bound off by
one, an inverted result, the REMOVED write dropped), the destroy walk
reading the flags once before it, obs_video_active() asked before the
flags and the flags read without video info, obs_source_is_scene()
asked before the flags, a video info field read early, strcmp() before
the MAIN check, the flags read before the canvas's own signal in
set_channel(), remove_source() and rename_source(), the remove check
after the reference, the private flag read once in set_name(), the
create's mix_audio read from the canvas instead of its argument, and
a second read of the flags in save. The C builds, which see reads only
through the writer, fail 29 of them.

git blame on the moved lines shows upstream commits (3d57b1f
introducing canvases, 66f4792, 1ecea56, 0fd1261) and the fork's
fe75932 (the public mask, #2151); they move as they are.

The new file joins libobs.core-source with libobs.pch.h, which
includes the new header; source-sets.json lists it. The exported
symbol sets are unchanged: 1766 on the C backend, the hybrid and
libobs-c-reference, the same set on all three, with the seventeen
functions local.

Refs #2544.
MCPVOT pushed a commit to MCPVOT/obs-studio that referenced this pull request Oct 30, 2026
* libobs: Move the canvas flag rules

Behaviour-neutral extraction before the Rust port. The flag rules and
setter checks of obs-canvas.c.h move to libobs/obs-canvas-rules.c.h,
as seventeen functions on plain values declared in the new
libobs/obs-canvas-rules.h without EXPORT (hidden, as
-fvisibility=hidden keeps them):

- _main_flags() and _public_flags(): MAIN | PROGRAM for
  obs_create_main_canvas(), and the public mask of obs_canvas_create(),
  obs_canvas_create_private() and obs_load_canvas(), which still casts
  the saved int64 to uint32_t itself;
- _main_view() and _mix_audio(): the view type and the mix's
  mix_audio, in obs_canvas_create_internal() and
  obs_canvas_reset_video_internal();
- _public(): a canvas that is not private is named, unnamed, renamed
  through the hash table and signalled on obs->signals
  (canvas_dosignal(), obs_canvas_create_internal(),
  obs_canvas_destroy(), obs_canvas_set_name());
- _holds_scenes(): SCENE_REF, in obs_canvas_destroy(),
  obs_canvas_insert_source() and obs_canvas_remove_source();
- _saves(): neither EPHEMERAL nor REMOVED;
- _size_set() and _format_set(): obs_canvas_has_valid_video_info();
- _resets(), _global_signals(), _channel_ok(), _name_empty(),
  _renames(), _removable(), _mark_removed() and _removed(): the checks
  of obs_canvas_reset_video(), obs_canvas_rename_source(),
  obs_canvas_set_channel(), obs_canvas_set_name(), obs_canvas_remove()
  and obs_canvas_removed().

Split per read, as obs-rust#2153 did. canvas->flags is read without a lock and
obs_canvas_remove() writes it from any thread, so each check gets the
value of its own read, at its point. Every check behind a || or && is
still asked only where the C reached it: obs_source_is_scene() only on
a SCENE_REF canvas, obs_video_active() only for a canvas that may
reset, strcmp() only for a canvas that may be renamed, and each video
info field only after the one before it was set. The rest stays C at
its point: the allocations and frees, the context data calls, the
signal handler setup, the mutexes, obs_view_init() and _free(), the
mix and its list under mixes_mutex, HASH_CLEAR and the walk that takes
next before each release (the obs-rust#2038 and obs-rust#2049 order), every reference
and weak reference, the pointer checks (source, previous source,
canvas, mix, the "already in a canvas" step), the group walk, the
calldata, every signal, obs_source_activate() and _deactivate(),
strcmp() and the logs. The flag values, REMOVED, the public mask,
VIDEO_FORMAT_NONE and MAX_CHANNELS are copied into the header, so it
needs only stdbool.h and stdint.h; obs-canvas.c.h checks them with
_Static_assert. REMOVED and PUBLIC_CANVAS_FLAGS leave obs-canvas.c.h.

Checked in a scratch harness (not committed). It compiles
origin/master's obs-canvas.c.h verbatim, and this branch's with
obs-canvas-rules.c.h, over a stand-in libobs.pch.h whose functions log
every call in order with its arguments: the context data calls (init
may fail), signal_handler_add_array() (may fail), the mutex init (may
fail), every lock and unlock, obs_view_init() and _free(), the mix
create (may fail) and free, the mix list, HASH_CLEAR, every signal
with its calldata, the strong and weak references (a weak get may
fail), obs_source_get_ref() (may return NULL), _release(), _is_scene(),
_is_group(), _activate(), _deactivate(), obs_video_active(), the scene
walks, the obs_data reads and writes, and blog(). A channel_change
handler may replace the source in the calldata. A concurrent writer may
flip any flag bit (MAIN, ACTIVATE, MIX_AUDIO, SCENE_REF, EPHEMERAL,
REMOVED, an unknown bit), the private flag or a video info field of
any live canvas at any call. Each run makes up to sixteen random calls
of the whole canvas API (every constructor with random flags, names and
video info; obs_load_canvas() with saved flags of 0, -1, INT64_MIN,
INT64_MAX, above 32 bits and random; save, the video info check, both
resets, insert, remove and rename of scene, group and plain sources,
set_channel at 0 to 3, 63, 64, 65 and UINT32_MAX with the same, another
or no source, set_name with NULL, "", "Main", the same and other
names, remove, the getters, scene create, remove and move, refs taken
and released, the mix clears and render), then releases everything
and removes the sources left alive, as obs_free_data() does. The C++
build also logs every read and write of the flags, the private flag
and the video info fields.

6,600,000 runs: 1,000,000 each with GCC and Clang at -O0 and -O2 as C
(67,558,889 logged lines each side), 1,000,000 each with G++ at -O0
and -O2 as C++ with the logged reads (95,351,694 each side), and
200,000 each under ASan and UBSan with no recovery (GCC -O0 and Clang
-O2 as C, G++ -O1 as C++). The two logs are identical in every
configuration. gcov over 100,000 runs takes every branch of the moved
decisions both ways, but the two no caller reaches: canvas_dosignal()
with no core signal name, and obs_canvas_remove() failing to take a
reference to a canvas its caller holds.

34 deliberately broken moves were planted, and the C++ build fails all
34: each rule changed (each operand dropped or swapped, a bound off by
one, an inverted result, the REMOVED write dropped), the destroy walk
reading the flags once before it, obs_video_active() asked before the
flags and the flags read without video info, obs_source_is_scene()
asked before the flags, a video info field read early, strcmp() before
the MAIN check, the flags read before the canvas's own signal in
set_channel(), remove_source() and rename_source(), the remove check
after the reference, the private flag read once in set_name(), the
create's mix_audio read from the canvas instead of its argument, and
a second read of the flags in save. The C builds, which see reads only
through the writer, fail 29 of them.

git blame on the moved lines shows upstream commits (3d57b1f
introducing canvases, 66f4792, 1ecea56, 0fd1261) and the fork's
fe75932 (the public mask, obs-rust#2151); they move as they are.

The new file joins libobs.core-source with libobs.pch.h, which
includes the new header; source-sets.json lists it. The exported
symbol sets are unchanged: 1766 on the C backend, the hybrid and
libobs-c-reference, the same set on all three, with the seventeen
functions local.

Refs obs-rust#2544.

* test: Characterize the canvas flag rules

test_canvas_rules_steps compiles libobs/obs-canvas-rules.c.h (ASan and
UBSan) and calls only its functions through obs-canvas-rules.h. 11
cases:

- the header's copies of the flags, REMOVED, the public mask,
  VIDEO_FORMAT_NONE and MAX_CHANNELS against obs.h;
- the main canvas's flags (MAIN | PROGRAM), and the public mask over
  every single bit and every combination of the five public flags,
  MAIN, REMOVED and an unknown bit;
- obs_load_canvas()'s cast of the saved int64 before the mask, at 0,
  the named flags, 0x80000000, 0xffffffff, above 32 bits, -1, -2, -32
  and the ends of int64;
- every flag rule (view type, mix_audio, scene references, save,
  reset, global signals, rename, remove, removed, the REMOVED write)
  over those 128 combinations and over every single bit;
- the main, program, preview and device canvases, and a caller asking
  for MAIN;
- the public canvas rule, the video info checks (sizes 0, 1, 1920 and
  UINT32_MAX; formats NONE, I420, NV12, P010 and outside the enum),
  the channel bound (0 to 1023, 63, 64, 65, 2^31 and UINT32_MAX), the
  empty name (NULL, "", " ", "a", "Main", "\0b", 0xff) and removing
  twice.

Characterized, not endorsed:
- the saved int64 is truncated before the mask, so bits above 31 are
  dropped silently and a negative value keeps the public flags of its
  low word (-1 loads as every public flag);
- a video format outside the enum counts as set;
- a removed canvas can still be reset and renamed.

It is in the native test graph, in name order, with the header as a
watch input. No assert_in_range or fail_msg, so it builds with cmocka
1.x and 2.x, and no two enum types are compared. Passes 11/11 on the C
backend and the hybrid. test_canvas_flags, test_scene_private_canvas,
test_canvas_destroy_scene_refs, test_shutdown_canvas_weak_ref,
test_scene_canvas_characterization, test_context_names and test_view
cover the callers through the real libobs, and pass on both.

Refs obs-rust#2544.

* obs-core: Test the canvas_rules port

RED for the port of libobs/obs-canvas-rules.c.h to a new canvas_rules
module of rust/obs-core, next to canvas_draw. The safe core is a stub
(todo!()): main_flags(), public_flags(), main_view(), mix_audio(),
is_public() (public is the C name; is_public reads better in Rust),
holds_scenes(), saves(), size_set(), format_set(), resets(),
global_signals(), channel_ok(), name_empty(), renames(), removable(),
mark_removed() and removed(), with the flag constants (MAIN, ACTIVATE,
MIX_AUDIO, SCENE_REF, EPHEMERAL, PROGRAM, PREVIEW, DEVICE, REMOVED,
PUBLIC), FORMAT_NONE and MAX_CHANNELS. name_empty() takes the name's
first byte, None for NULL, so the core reads no pointer.

The plan sketched wider steps (reset(has_ovi, flags, video_active),
rename(empty, flags, same), scene_ref(flags, is_scene)). Each would
take a value the C reads or calls only behind a || or &&, so each check
is its own function, asked at its own point, as obs-rust#2153 did; the pointer
checks, obs_source_is_scene(), obs_video_active() and strcmp() stay C.

tests/obs_core/canvas_rules.rs ports test_canvas_rules_steps 1:1: its
11 cases under the same names (steps_*), in the same order, with the
same values and expectations, the characterized-not-endorsed cases
included. Three new proptests (edge_*, 4096 cases each) check every
rule over any u32 flags, the load over any i64 (truncated as the C's
cast, never keeping MAIN or REMOVED), and the size, format, channel,
name byte and private rules over any value, against models written
from the C's expressions.

soldr cargo test -p obs-data --test obs-tests-core --
canvas_rules:: passes 1 of 14 (the constants) and fails 13 with "not
yet implemented".

Refs obs-rust#2544.

* obs-core: Port the canvas flag rules

Ports libobs/obs-canvas-rules.c.h, the canvas flag rules and setter
checks of obs-canvas.c, to the canvas_rules module of rust/obs-core.
obs-canvas.c.h stays C around it: every read of the canvas's flags,
private flag and video info fields at its point, the allocations and
frees, the context data calls, the signal handler setup, the mutexes,
the view, the mix and its list, HASH_CLEAR and the destroy walk (the
obs-rust#2038 and obs-rust#2049 order), every reference, the pointer checks, the group
walk, obs_source_is_scene() and _is_group(), obs_video_active(),
strcmp(), the calldata, every signal, the activation calls and the
logs. The hybrid libobs drops the C file and links the Rust shims in
its place.

Safe core (src/canvas_rules.rs), seventeen functions:
- main_flags(): MAIN | PROGRAM; public_flags(): flags & PUBLIC;
- main_view(): ACTIVATE; mix_audio(): MIX_AUDIO; is_public():
  !is_private; holds_scenes(): SCENE_REF;
- saves(): neither EPHEMERAL nor REMOVED; size_set(): not 0;
  format_set(): not FORMAT_NONE (a format outside the enum is set);
- resets() and renames(): not MAIN; global_signals(): MAIN;
  channel_ok(): below MAX_CHANNELS;
- name_empty(): None or a first byte of 0;
- removable(): neither REMOVED nor MAIN; mark_removed(): | REMOVED;
  removed(): REMOVED.
The module denies clippy's arithmetic, indexing, as, unwrap, expect,
panic, todo, unimplemented and unreachable lints, keeps no state and
takes no lock.

Shims (src/ffi/canvas_rules.rs): the seventeen C signatures by value;
the flags as u32, the format as int. The one pointer is the name of
obs_canvas_set_name(): the shim reads its first byte only when it is
not NULL, as the C's !name || !*name. No struct is mirrored. No
intentional difference from the C.

C side: the file leaves libobs.core-source for its own group,
libobs.ported-canvas-rules, registered in its own fragment,
libobs/build/ported-source-sets/canvas-rules.json
(LIBOBS_PORTED_SOURCES), so it edits no shared ported list. Its PCH is
only obs-canvas-rules.h; libobs.pch.h keeps the header for
obs-canvas.c.
The functions are not EXPORT, so the shims are hidden: rust-hidden.txt,
the local: list of rust-exports.map and rust-unexports-macos.txt.
libobs-rust already links the crate's ffi. The exported symbol sets
are unchanged and equal: 1766 on the C backend, the hybrid and
libobs-c-reference, the same set as origin/master; the seventeen
functions are local in all three, and the hybrid's come from the Rust
archive (the group's object is built only for libobs-c-reference).
ci/test_ported_sources.py checks the group on every host.

test_canvas_rules_steps still compiles the C file into itself,
unchanged, and passes 11/11. It has no _rust variant: linking it
against libobs_rust-static fails with 401 undefined references (108
symbols), because the shims' archive member (an obs_core codegen unit)
also holds the other obs-core ports, as for test_canvas_draw_steps;
its tests.d file says so. The Tier 3 parity tests compare the shims
with the C instead.

soldr cargo test -p obs-data --test obs-tests-core --
canvas_rules:: passes 14 of 14.

No user-visible change, so no docs/features.md line.

Refs obs-rust#2544.

* obs-core: Check canvas_rules against the C

rust/obs-c-oracle/oracle/canvas_rules.c compiles the unchanged
libobs/obs-canvas-rules.c.h with its seventeen functions renamed
oracle_*, after obs.h, plus the header's copies, enum
obs_canvas_flags, VIDEO_FORMAT_NONE, MAX_CHANNELS and the type sizes.
tests/obs_core/canvas_rules_parity.rs (7 tests) runs the shim, the C
and the safe core on every input and compares the results exactly: the
C's bool as the byte it returns, the flags as the uint32_t.

- the 23 values and sizes against the C headers, and the main flags;
- exhaustive: every flag rule for every low 16-bit flags word under
  16 high words (bits 16, 17, 30 and 31), every single bit and every
  combination of the known flags, REMOVED and an unknown bit; the load
  for 786,432 saved int64 values (four high words, three top halves,
  every low half) and the ends; the size and channel for 0 to 70,000,
  every 65,521st u32 and the ends; the format for -70,000 to 70,000
  and the ends; the name for NULL and every first byte, through a C
  string;
- each caller's decisions in their order (the three constructors, the
  save, the reset, the channel bound and global signal, the name
  refusals and the public rename, the remove and REMOVED write, the
  video info chain), one copy on each side, for every combination of
  the known flags, REMOVED, an unknown bit, the private flag, six
  channels, four names, three video infos and five formats;
- proptests (20,000 cases each): every decision over any value, and
  the callers over all of it at once.

No input reaches a division or a 128-bit multiply on either side, so
nothing is skipped on MSVC x64. No intentional differences.

The parity module passed 3 runs in a row with fresh seeds.
test_canvas_rules_steps passes 11/11 under ASan and UBSan. The
committed regression files hold the seeds proptest saved while mutants
failed; all pass on the port.

Mutation check (the mutants are not committed): 36 mutants, 24 in the
core and 12 in the shims, each run against Tier 1 (14 tests) and Tier
3 (7). 34 fail. The two that survive are equivalent: the resets shim
calling renames() (both are "not MAIN"), and the name shim passing
Some(0) for NULL (both are empty). Tier 1 does not reach the shims;
Tier 3 kills every other shim mutant. Each mutant and the tests that
fail on it:

 1. core, main_flags: no SCENE_REF
    T1: steps_canvas_kinds, steps_constructor_flags
    T3: callers_every_input, proptest_callers,
        values_match_the_c_headers
 2. core, public_flags: keeps MAIN
    T1: edge_flag_rules_any_flags, edge_load_any_int64,
        steps_canvas_kinds, steps_constructor_flags,
        steps_load_flags_from_int64
    T3: callers_every_input, every_flags_word, every_load,
        proptest_callers, proptest_decisions
 3. core, public_flags: keeps REMOVED
    T1: edge_flag_rules_any_flags, edge_load_any_int64,
        steps_constructor_flags, steps_load_flags_from_int64,
        steps_remove_twice
    T3: callers_every_input, every_flags_word, every_load,
        proptest_callers, proptest_decisions
 4. core, public_flags: drops EPHEMERAL
    T1: edge_flag_rules_any_flags, edge_load_any_int64,
        steps_canvas_kinds, steps_constructor_flags,
        steps_load_flags_from_int64
    T3: callers_every_input, every_flags_word, every_load,
        proptest_callers, proptest_decisions
 5. core, main_view: MIX_AUDIO
    T1: edge_flag_rules_any_flags, steps_canvas_kinds,
        steps_flag_rules_every_bit, steps_flag_rules_every_combination
    T3: callers_every_input, every_flags_word, proptest_callers,
        proptest_decisions
 6. core, mix_audio: ACTIVATE
    T1: as 5; T3: as 5
 7. core, is_public: not negated
    T1: edge_values_any_input, steps_public_rule
    T3: callers_every_input, every_value, proptest_callers,
        proptest_decisions
 8. core, holds_scenes: EPHEMERAL
    T1: edge_flag_rules_any_flags, steps_canvas_kinds,
        steps_flag_rules_every_bit,
        steps_flag_rules_every_combination, steps_remove_twice
    T3: every_flags_word, proptest_decisions
 9. core, saves: ignores REMOVED
    T1: edge_flag_rules_any_flags, steps_flag_rules_every_bit,
        steps_flag_rules_every_combination, steps_remove_twice
    T3: callers_every_input, every_flags_word, proptest_callers,
        proptest_decisions
10. core, saves: ignores EPHEMERAL
    T1: as 5; T3: as 5
11. core, size_set: > 1
    T1: steps_video_info_checks
    T3: every_value
12. core, format_set: > 0
    T1: edge_values_any_input, steps_video_info_checks
    T3: callers_every_input, every_value, proptest_callers,
        proptest_decisions
13. core, resets: always
    T1: as 5; T3: as 5
14. core, global_signals: negated
    T1: edge_flag_rules_any_flags, edge_load_any_int64,
        steps_canvas_kinds, steps_flag_rules_every_bit,
        steps_flag_rules_every_combination
    T3: callers_every_input, every_flags_word, proptest_callers,
        proptest_decisions
15. core, channel_ok: <=
    T1: steps_channel_bound
    T3: callers_every_input, every_value, proptest_callers
16. core, name_empty: NULL only
    T1: edge_values_any_input, steps_name_checks
    T3: callers_every_input, every_value, proptest_callers,
        proptest_decisions
17. core, name_empty: NULL is a name
    T1: as 16; T3: as 16
18. core, renames: always
    T1: as 5; T3: as 5
19. core, removable: ignores REMOVED
    T1: as 9; T3: as 9
20. core, removable: ignores MAIN
    T1: as 5; T3: as 5
21. core, mark_removed: no write
    T1: as 9; T3: as 9
22. core, removed: bit 30
    T1: as 9; T3: as 9
23. core, REMOVED: 1 << 30
    T1: edge_flag_rules_any_flags, steps_constants,
        steps_flag_rules_every_bit
    T3: callers_every_input, every_flags_word, proptest_callers,
        proptest_decisions, values_match_the_c_headers
24. core, MAX_CHANNELS: 63
    T1: steps_channel_bound, steps_constants
    T3: callers_every_input, every_value, proptest_callers,
        values_match_the_c_headers
25. shim, public_flags: main_flags
    T3: callers_every_input, every_flags_word, every_load,
        proptest_callers, proptest_decisions
26. shim, main_view: mix_audio
    T3: callers_every_input, every_flags_word, proptest_callers,
        proptest_decisions
27. shim, public: negated
    T3: callers_every_input, every_value, proptest_callers,
        proptest_decisions
28. shim, saves: removable
    T3: as 26
29. shim, resets: renames: SURVIVES (equivalent)
30. shim, global_signals: removed
    T3: as 26
31. shim, format_set: format & 0x7fffffff
    T3: every_value
32. shim, channel_ok: low byte only
    T3: every_value, proptest_callers, proptest_decisions
33. shim, name_empty: reads the byte, answers Some(1)
    T3: callers_every_input, every_value, proptest_callers,
        proptest_decisions
34. shim, name_empty: NULL as Some(0): SURVIVES (equivalent)
35. shim, mark_removed: public_flags
    T3: as 26
36. shim, removed: removable
    T3: as 26

Refs obs-rust#2544.

* docs: Record the canvas_rules port

- rust/README.md: the obs-core crate row names
  libobs/obs-canvas-rules.c.h, and a row for the file says what is
  swapped in the hybrid, what obs-canvas.c.h keeps and why each read
  asks its own function, the copied values, the characterized cases,
  why there is no _rust variant, and what Tier 3 covers;
- rust/obs-core: the crate description and the lib.rs docs list the
  module;
- docs/rust-port/naming.md: obs-canvas-rules.c.h, extracted from
  obs-canvas.c.h, maps to rust/obs-core;
- rust/obs-c-oracle/benches/port_comparison/canvas_rules.rs:
  canvas_rules/create and canvas_rules/setters, the constructors' and
  the setters' decisions in their order over 1024 calls on each side,
  folded into a checksum both must match. It allocates nothing, so the
  per-module allocation check holds. The bench's build.rs finds it in
  the directory, so no shared list changes.

port_comparison (x86_64 Linux, GCC, not pinned, load average about
3; criterion's median per 1024 calls): create 7.8 us Rust, 5.4 us C;
setters 16.7 us Rust, 11.4 us C, about 2 to 5 ns more a decision
through the exported shims. These run when a canvas is created, saved,
reset, renamed or removed and when a scene or channel changes, never
per frame.

No docs/features.md line: the port changes no behaviour.

Fixes obs-rust#2544.

* test: Characterize the canvas rules' callers

test_canvas_rules_steps pins the seventeen rules on their own, and the
move was checked against the callers only in a scratch harness. This
pins what the callers in obs-canvas.c do with the rules, through the
real libobs and its public API, so it runs unchanged against the C
backend, the hybrid and libobs-c-reference. 9 cases:

- constructor_flags: the main canvas is MAIN | PROGRAM; create and
  create_private drop MAIN, REMOVED and unknown bits; obs_load_canvas()
  keeps the public flags of the saved value's low word;
- save_rules: the saved flags and private flag, a private canvas
  reloaded, and no save for an ephemeral or a removed canvas;
- reset_rules: no video info, the main canvas, and active video (a raw
  video callback on the main mix) are refused, with no video_reset
  signal; otherwise the canvas gets a mix and signals once;
- set_name_rules: NULL, "", the same name and the main canvas are
  refused with no signal; a public canvas is renamed in the name table
  and signals canvas_rename globally; a private one signals only its
  own rename and stays out of the name table;
- remove_rules: one remove signal each, none for a second remove or
  for the main canvas, and the REMOVED bit in the flags;
- scene_refs: a scene on the main canvas (SCENE_REF) outlives its
  creator's reference until it is removed; a plain source on it gets
  no canvas reference; a scene on a canvas without SCENE_REF dies with
  its last reference; a SCENE_REF canvas releases its scenes when it is
  destroyed;
- group_walk: removing a group removes its items from every scene of
  its canvas; a removed plain source leaves the canvas but its items
  stay in the scenes;
- rename_source_signals: a scene rename on the main canvas signals
  source_rename globally, one on another canvas only on that canvas;
- channel_replace: a channel_change handler that replaces the source.

Characterized, not endorsed: the saved int64 is truncated before the
mask; a removed canvas can still be renamed; the handler's replacement
is stored and activated without a reference of its own, so clearing
the channel releases one more reference on it than the canvas took,
and the reference taken on the source passed in is never released.

It needs a display and OpenGL (Linux, the graphics suite, under
xvfb-run like test_main_mix_before_video), and is in the test graph in
name order. No assert_in_range or fail_msg, and no two enum types are
compared. Passes 9/9 three runs in a row on the C backend and the
hybrid, and 9/9 against libobs-c-reference, each under a private Xvfb.

Refs obs-rust#2544.
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.

libobs: Canvases accept the internal REMOVED flag and unknown bits; leak when context init fails

2 participants