Skip to content

libobs: Extract the hotkey binding press count and its seam (#1836, PR 1) - #3032

Merged
zackees merged 3 commits into
masterfrom
port/1836-hotkey-binding-press
Oct 30, 2026
Merged

zackees merged 3 commits into
masterfrom
port/1836-hotkey-binding-press

Conversation

@zackees

@zackees zackees commented Oct 30, 2026

Copy link
Copy Markdown
Member

PR 1 of 2 for #1836 (Refs, not Fixes): the behavior-neutral half — characterization, extraction and the seam. The Rust port of the file is PR 2.

RED then GREEN

Three commits, each behavior-neutral on the C side:

  1. test: Characterize the hotkey binding press count — new test_hotkey_binding_press_steps. It compiles obs-hotkey.c into itself like test_hotkey_press_steps, registers a hotkey through the public API, and calls obs_hotkey_binding_press() / _release() with the binding's flag and the hotkey's count set by hand. It pins the flag, the count and the call after every event (2 flag states × 6 counts −2..3 × press or release, each in the three routing states), the arguments of each call, that both fields are written before the call and nothing after it, two bindings of one hotkey, and remove_bindings() releasing only the pressed bindings it removes. Characterized, not endorsed: neither function checks the flag, so a press of a pressed binding counts again and a release at count 0 goes negative (libobs never reaches either).
  2. libobs: Move the hotkey binding press count into its own file — the four functions move verbatim into libobs/obs-hotkey-binding-press.c.h, in the libobs.core unity group just before obs-hotkey.c.h, with libobs.pch.h. remove_bindings() calls the wrapper it already declared in obs-internal.h, so the static forward declaration goes. No group, PCH or source-sets.json change; no platform conditional moves.
  3. libobs: Reach the binding press count through a hidden accessor — RED was the fields_and_dispatch case (it did not build: neither function existed), GREEN is obs_hotkey_binding_count_fields() (pointers to the binding's flag, its hotkey's count, and the hotkey) and obs_hotkey_dispatch() (the rerouting choice and the call, so the callback ABI and the frontend's router stay C). Both are declared in obs-internal.h without EXPORT; the pointers alias the fields, so every write happens at the same point with the same lock held.

Tiers

  • Tier 1/3: not in this PR — no Rust yet. They land with the port in PR 2, against the moved file.
  • Tier 2: rust/tools/linux-validate/run.py (hybrid libobs + libobs-c-reference): 629 tests, and the 20 hotkey / Wayland / portal tests pass, including test_hotkey_press_state, test_hotkey_pair_latch, test_hotkey_press_steps, test_hotkey_load, test_wayland_hotkeys and test_portal_shortcuts. The unchanged C tests are untouched.
  • Tier 4: does not apply (no protocol change).
  • ./lint: clean except ci/test_check_changes.py, which fails in this sandbox with git: command not found inside its own probe (environment, not this change).

docs/features.md: no line — behavior is unchanged.

Test cost

New tests: test_hotkey_binding_press_steps, 5 cases, 0.05 s total, no own threads, no allocation in the unit under test. It joins the test/cmocka cmocka binary set (native-core), so no new Rust test executable.

Reviewer notes

  • The counts are small on purpose: no polling or key state is scripted here, test_hotkey_press_state already covers the poll and inject paths on the unchanged C.
  • PR 2 follows: obs_hotkey::binding_press, libobs.ported-hotkey-binding-press group with its own PCH / source-sets.json entry / export fragment, Tier 1 over these expectations and Tier 3 against the moved C.

The binding press count (press_released_binding() and
release_pressed_binding() in obs-hotkey.c, next to be ported to
rust/obs-hotkey as binding_press) turns binding events into hotkey events:
the hotkey's callback runs on the first press and the last release only.

test_hotkey_binding_press_steps compiles obs-hotkey.c into itself like
test_hotkey_press_steps, registers a hotkey through the public API, and calls
obs_hotkey_binding_press() and obs_hotkey_binding_release() with the
binding's pressed flag and its hotkey's pressed count set by hand. It pins
what the real libobs cannot reach or cannot show:

- the flag, the count and the call after every event: 2 flag states x 6
  counts (-2..3) x press or release, each in the three routing states;
- the arguments of each call, and that the flag and the count are written
  before it and nothing after it;
- two bindings of one hotkey (the count is per hotkey, not per binding);
- remove_bindings() releasing only the pressed bindings it removes, so
  unregistering a hotkey held through two bindings calls it once.

Characterized, not endorsed: neither function checks the flag, so a press of
a pressed binding counts again and a release at count 0 makes it negative.
libobs presses only a released binding and releases only a pressed one, so
this is unreachable today.

Refs #1836
Behavior-neutral extraction before the Rust port: press_released_binding(),
release_pressed_binding() and their two wrappers obs_hotkey_binding_press()
and obs_hotkey_binding_release() move verbatim from libobs/obs-hotkey.c.h
into libobs/obs-hotkey-binding-press.c.h. remove_bindings(), which stays in
obs-hotkey.c.h, now calls the wrapper it already declared in obs-internal.h,
so the static forward declaration goes away.

The new file joins libobs.core just before obs-hotkey.c.h, with
libobs.pch.h. No group, PCH or source-sets.json change, no platform
conditional moves, and the exported symbols are unchanged. The two wrappers
are declared in obs-internal.h without EXPORT, so -fvisibility=hidden keeps
them local.

The tests that compile obs-hotkey.c.h on their own (test_hotkey_load,
test_hotkey_getters_characterization, test_hotkey_pair_steps,
test_hotkey_press_steps, test_wayland_hotkeys and test_portal_shortcuts)
now compile the new file next to it.

Tier 2 Linux, targeted: test_hotkey_binding_press_steps 4/4 and
test_wayland_hotkeys pass in the hybrid build.

Refs #1836
The hotkey binding press count, libobs/obs-hotkey-binding-press.c.h, writes
the binding's pressed flag and its hotkey's pressed count, and calls the
hotkey. All three are private, and the Rust port should not mirror either
struct, so the unit now reaches them through two hidden functions in
obs-hotkey.c.h:

- obs_hotkey_binding_count_fields(binding): pointers to the binding's pressed
  flag and its hotkey's pressed count, plus the hotkey;
- obs_hotkey_dispatch(hotkey, pressed): the hotkey's own callback, or the
  router when callback rerouting is on (nothing when it is on and no router
  is set), so the callback ABI and the frontend's router stay C.

The pointers alias the fields themselves, so each is still written at the
same point, with the same lock held, and nothing is written after the call.
Both are declared in obs-internal.h without EXPORT, so the exported symbols
are unchanged.

The new fields_and_dispatch case in test_hotkey_binding_press_steps pins
them: the accessor points at the binding's own flag and its hotkey's count,
a value written through it is the one the unit writes, and the dispatch
makes the rerouting choice and nothing else.

Tier 2 Linux, targeted: test_hotkey_binding_press_steps 5/5, and the other
19 hotkey, Wayland and portal tests, pass in the hybrid build.

Refs #1836
@zackees
zackees marked this pull request as ready for review October 30, 2026 09:32
@zackees
zackees added this pull request to the merge queue Oct 30, 2026
@zackees

zackees commented Oct 30, 2026

Copy link
Copy Markdown
Member Author

Handed off to the coordinator.

  • PR CI green: Format, Fast Gate, Rust Tier 2 Linux (build + tests ON/OFF, both shard parts), Sanitize/asan, Validate Data. CI Gate ✅.
  • Queued to merge (squash).
  • Local ./lint is clean except ci/test_check_changes.py, which fails in this sandbox because its own probe cannot find git on PATH; nothing in this PR touches ci/.
  • The local-gate stamp is not attached for the same reason (the lint lane contains that failing check); PR CI is the merge gate here.

PR 2 (the Rust port of libobs/obs-hotkey-binding-press.c.h to obs_hotkey::binding_press, with Tier 1 and Tier 3) follows once this lands, since it needs the moved file on master.

@zackees

zackees commented Oct 30, 2026

Copy link
Copy Markdown
Member Author

Coordinator: this PR turned master red on macOS. Build 🧱 / Rust Tier 2 (macos-26) fails in master push runs 41035468706 (93b5e64, this merge) and 41038256023 (the next one), so it is not a flake:

test_hotkey_binding_press_steps.c:317: error: Failure!   (assert_int_equal(num_calls, 2) after obs_hotkey_unregister)
[  FAILED  ] remove_bindings_releases

PR CI never ran macOS Tier 2 for this path, and Linux passes. My best guess, not verified on a Mac: on macOS the test's libobs has a real hotkey platform context, so the hotkey thread polls key state. It sees that D and E are not physically down and releases the hand-pressed bindings on its own, before obs_hotkey_unregister runs. remove_bindings() then finds nothing pressed and never calls. Headless Linux has no platform context, so the thread does nothing there. If that is right, the test needs the hotkey thread out of the picture (no platform context, or a stopped thread) rather than a change to libobs.

Please fix it forward, or revert while you look. Every master push stays red on macOS Tier 2 until then.

@zackees

zackees commented Oct 30, 2026

Copy link
Copy Markdown
Member Author

Correction to my comment above: the hotkey-thread guess was wrong. #3045 finds the real cause. unregister_hotkey() frees the hotkey before remove_bindings() releases its bindings, and the release reads binding->hotkey->pressed from freed memory. This PR's characterization test exposed an existing use-after-free; it did not create one. Linux passes only because the freed block still holds 2. The fix is #3045 (#3044); no revert needed.

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.

1 participant