From 1bcf2a898d96a1c7bfdfe065d38e814718bcd829 Mon Sep 17 00:00:00 2001 From: Ryan Doupe Date: Thu, 19 Nov 2026 20:52:11 -0500 Subject: [PATCH 1/2] test: mirror hotkey-combination background-id characterization (RED, Refs #5288) Tier 1 mirrors every assertion of the unchanged test_hotkey_combination_background_id.c against the safe query. Tier 3 runs the extracted original and the shim on one recording fixture through the new real-named lock, callback-slot, unlock and dstr_free adapters, with six mutants. background_query_parity drops its same-named fake getter and drives the real getter on that fixture instead. The safe query is a stub that returns None: soldr cargo test -p obs-data --test obs-tests-core -- hotkey::combination_background_id hotkey::background_query test result: FAILED. 4 passed; 6 failed Co-Authored-By: Claude Opus 5.5 --- libobs/build/libobs.core+.h.c | 1 + ...otkey-combination-background-id-fields.c.h | 33 +++ .../oracle/hotkey_combination_background_id.c | 230 +++++++++++++++++ .../src/combination_background_id.rs | 31 +++ .../src/ffi/combination_background_id.rs | 67 +++++ rust/obs-hotkey/src/ffi/mod.rs | 1 + rust/obs-hotkey/src/lib.rs | 1 + .../tests/hotkey/background_query_parity.rs | 75 ++++-- .../tests/hotkey/combination_background_id.rs | 231 +++++++++++++++++ .../combination_background_id_parity.rs | 239 ++++++++++++++++++ rust/obs-hotkey/tests/hotkey/main.rs | 2 + 11 files changed, 884 insertions(+), 27 deletions(-) create mode 100644 libobs/obs-hotkey-combination-background-id-fields.c.h create mode 100644 rust/obs-c-oracle/oracle/hotkey_combination_background_id.c create mode 100644 rust/obs-hotkey/src/combination_background_id.rs create mode 100644 rust/obs-hotkey/src/ffi/combination_background_id.rs create mode 100644 rust/obs-hotkey/tests/hotkey/combination_background_id.rs create mode 100644 rust/obs-hotkey/tests/hotkey/combination_background_id_parity.rs diff --git a/libobs/build/libobs.core+.h.c b/libobs/build/libobs.core+.h.c index 4489fe769a..2c61839753 100644 --- a/libobs/build/libobs.core+.h.c +++ b/libobs/build/libobs.core+.h.c @@ -25,6 +25,7 @@ #include "libobs/obs-hotkey-background-press-fields.c.h" #include "libobs/obs-hotkey-background-state.c.h" #include "libobs/obs-hotkey-background-trigger-fields.c.h" +#include "libobs/obs-hotkey-combination-background-id-fields.c.h" #include "libobs/obs-hotkey-combination-background-id.c.h" #include "libobs/obs-hotkey-enum-fields.c.h" #include "libobs/obs-hotkey-identity-fields.c.h" diff --git a/libobs/obs-hotkey-combination-background-id-fields.c.h b/libobs/obs-hotkey-combination-background-id-fields.c.h new file mode 100644 index 0000000000..f465d4d51d --- /dev/null +++ b/libobs/obs-hotkey-combination-background-id-fields.c.h @@ -0,0 +1,33 @@ +#include "libobs/build/libobs.pch.h" + +extern bool obs_hotkey_seam_lock(void); +extern void obs_hotkey_seam_unlock(void); + +/* Private adapters for the Rust combination background-id query. The callback + * table, its mutex and dstr ownership stay in C. */ +typedef bool (*obs_hotkey_combination_background_id_func)(obs_key_combination_t combo, struct dstr *id); + +bool obs_hotkey_combination_background_id_lock(void); +bool obs_hotkey_combination_background_id_lock(void) +{ + return obs_hotkey_seam_lock(); +} + +/* Called only with the hotkey mutex held. */ +const obs_hotkey_combination_background_id_func *obs_hotkey_combination_background_id_fields(void); +const obs_hotkey_combination_background_id_func *obs_hotkey_combination_background_id_fields(void) +{ + return &obs->hotkeys.background.shortcut_id; +} + +void obs_hotkey_combination_background_id_unlock(void); +void obs_hotkey_combination_background_id_unlock(void) +{ + obs_hotkey_seam_unlock(); +} + +void obs_hotkey_combination_background_id_free(struct dstr *id); +void obs_hotkey_combination_background_id_free(struct dstr *id) +{ + dstr_free(id); +} diff --git a/rust/obs-c-oracle/oracle/hotkey_combination_background_id.c b/rust/obs-c-oracle/oracle/hotkey_combination_background_id.c new file mode 100644 index 0000000000..9ecc45d66b --- /dev/null +++ b/rust/obs-c-oracle/oracle/hotkey_combination_background_id.c @@ -0,0 +1,230 @@ +/* Original combination background-id wrapper and its real-header adapters on + * one recording fixture. The probe runs the original and a candidate getter + * through the same lock, callback-field, unlock and dstr_free boundary and + * reports every difference instead of aborting, so mutants are observable. */ +#include "libobs/build/libobs.pch.h" +#include +#include + +typedef char *(*combo_id_getter)(obs_key_combination_t combo); + +enum combo_id_output { NO_OUTPUT, EMPTY_OUTPUT, TEXT_OUTPUT, PARTIAL_OUTPUT, FAILED_EMPTY, REENTER, OUTPUT_KINDS }; + +static _Thread_local struct obs_core combo_id_core; +static _Thread_local bool combo_id_lock_result, combo_id_held, combo_id_with_callback; +static _Thread_local enum combo_id_output combo_id_kind; +static _Thread_local obs_key_combination_t combo_id_expected; +static _Thread_local combo_id_getter combo_id_current; +static _Thread_local char *combo_id_allocation; +static _Thread_local char combo_id_events[64]; +static _Thread_local size_t combo_id_event_count; + +static void combo_id_event(char value) +{ + if (combo_id_event_count + 1 < sizeof(combo_id_events)) { + combo_id_events[combo_id_event_count++] = value; + combo_id_events[combo_id_event_count] = '\0'; + } +} + +/* 'X' marks a lock-discipline violation. */ +static void combo_id_require(bool held) +{ + if (combo_id_held != held) + combo_id_event('X'); +} + +static bool combo_id_wrong_callback(obs_key_combination_t combo, struct dstr *id) +{ + (void)combo; + (void)id; + /* The field after unlock: a wrapper must invoke its saved callback. */ + combo_id_event('W'); + return false; +} + +static bool combo_id_callback(obs_key_combination_t combo, struct dstr *id) +{ + combo_id_require(false); + combo_id_event('C'); + if (combo.key != combo_id_expected.key || combo.modifiers != combo_id_expected.modifiers) + combo_id_event('K'); + if (id->array || id->len || id->capacity) + combo_id_event('Z'); + if (combo_id_kind == REENTER) { + /* Reenter the getter under test after release, with no callback. */ + combo_id_with_callback = false; + if (combo_id_current(combo)) + combo_id_event('N'); + } else if (combo_id_kind != NO_OUTPUT && combo_id_kind != FAILED_EMPTY) { + dstr_copy(id, combo_id_kind == EMPTY_OUTPUT ? "" : "background-id"); + /* An empty successful result may still own an allocated empty string. */ + if (!id->array) + id->array = bzalloc(1); + combo_id_allocation = id->array; + } + return combo_id_kind != PARTIAL_OUTPUT && combo_id_kind != FAILED_EMPTY; +} + +bool oracle_combo_id_lock(void); +bool oracle_combo_id_lock(void) +{ + combo_id_require(false); + combo_id_event('L'); + combo_id_held = combo_id_lock_result; + /* The current callback is visible only while the lock is held. */ + combo_id_core.hotkeys.background.shortcut_id = combo_id_with_callback ? combo_id_callback : NULL; + return combo_id_lock_result; +} + +void oracle_combo_id_unlock(void); +void oracle_combo_id_unlock(void) +{ + combo_id_require(true); + combo_id_event('U'); + combo_id_held = false; + combo_id_core.hotkeys.background.shortcut_id = combo_id_wrong_callback; +} + +static struct obs_core *combo_id_read_core(void) +{ + combo_id_require(true); + combo_id_event('R'); + return &combo_id_core; +} + +static void combo_id_free(struct dstr *id) +{ + combo_id_require(false); + combo_id_event('F'); + if (id->array != combo_id_allocation) + combo_id_event('P'); + dstr_free(id); + if (id->array || id->len || id->capacity) + combo_id_event('P'); +} + +static inline bool lock(void) +{ + return oracle_combo_id_lock(); +} + +static inline void unlock(void) +{ + oracle_combo_id_unlock(); +} + +char *oracle_obs_hotkey_combination_get_background_id(obs_key_combination_t combo); + +#define obs combo_id_read_core() +#define dstr_free combo_id_free +#define obs_hotkey_seam_lock oracle_combo_id_lock +#define obs_hotkey_seam_unlock oracle_combo_id_unlock +#define obs_hotkey_combination_get_background_id oracle_obs_hotkey_combination_get_background_id +#include "libobs/obs-hotkey-combination-background-id.c.h" +#undef obs_hotkey_combination_get_background_id +#include "hotkey_combination_background_id_fields.inc" +#undef obs_hotkey_seam_unlock +#undef obs_hotkey_seam_lock +#undef dstr_free +#undef obs + +struct combo_id_result { + char events[64]; + bool null_result, transferred; + char text[16]; +}; + +static void combo_id_arm(combo_id_getter getter, unsigned bits, obs_key_combination_t combo) +{ + memset(&combo_id_core, 0, sizeof(combo_id_core)); + memset(combo_id_events, 0, sizeof(combo_id_events)); + combo_id_event_count = 0; + combo_id_lock_result = (bits & 1) != 0; + combo_id_with_callback = (bits & 2) != 0; + combo_id_kind = (enum combo_id_output)((bits >> 2) % OUTPUT_KINDS); + combo_id_held = false; + combo_id_expected = combo; + combo_id_current = getter; + combo_id_allocation = NULL; + combo_id_core.hotkeys.background.shortcut_id = combo_id_wrong_callback; +} + +/* Arm the fixture for callers that reach the getter indirectly. */ +void oracle_hotkey_combination_background_id_arm(unsigned bits, uint32_t modifiers, int key, combo_id_getter getter); +void oracle_hotkey_combination_background_id_arm(unsigned bits, uint32_t modifiers, int key, combo_id_getter getter) +{ + obs_key_combination_t combo = {modifiers, (obs_key_t)key}; + combo_id_arm(getter, bits, combo); +} + +void oracle_hotkey_combination_background_id_events(char *out, size_t size); +void oracle_hotkey_combination_background_id_events(char *out, size_t size) +{ + strncpy(out, combo_id_events, size - 1); + out[size - 1] = '\0'; +} + +static void combo_id_run(combo_id_getter getter, unsigned bits, obs_key_combination_t combo, + struct combo_id_result *out) +{ + combo_id_arm(getter, bits, combo); + char *result = getter(combo); + combo_id_require(false); + memset(out, 0, sizeof(*out)); + memcpy(out->events, combo_id_events, sizeof(out->events)); + out->null_result = !result; + out->transferred = result && result == combo_id_allocation; + if (result) + strncpy(out->text, result, sizeof(out->text) - 1); + bfree(result); + /* A mutant may return with the fixture still locked. */ + combo_id_held = false; +} + +/* Bits of the result: 1 trace (order, discipline, callback input or partial + * free), 2 returned pointer or text. Allocation balance follows from the + * trace: every non-transferred allocation is freed exactly once (F, P). */ +unsigned oracle_hotkey_combination_background_id_probe(unsigned bits, uint32_t modifiers, int key, + combo_id_getter getter); +unsigned oracle_hotkey_combination_background_id_probe(unsigned bits, uint32_t modifiers, int key, + combo_id_getter getter) +{ + obs_key_combination_t combo = {modifiers, (obs_key_t)key}; + struct combo_id_result original, candidate; + combo_id_run(oracle_obs_hotkey_combination_get_background_id, bits, combo, &original); + combo_id_run(getter, bits, combo, &candidate); + unsigned diff = 0; + if (strcmp(original.events, candidate.events) != 0 || strpbrk(candidate.events, "XWKZPN")) + diff |= 1; + if (original.null_result != candidate.null_result || original.transferred != candidate.transferred || + strcmp(original.text, candidate.text) != 0) + diff |= 2; + return diff; +} + +/* One getter's trace, for exact expectations in Rust. */ +void oracle_hotkey_combination_background_id_trace(unsigned bits, combo_id_getter getter, char *out, size_t size); +void oracle_hotkey_combination_background_id_trace(unsigned bits, combo_id_getter getter, char *out, size_t size) +{ + struct combo_id_result run; + obs_key_combination_t combo = {INTERACT_CONTROL_KEY, OBS_KEY_A}; + combo_id_run(getter ? getter : oracle_obs_hotkey_combination_get_background_id, bits, combo, &run); + strncpy(out, run.events, size - 1); + out[size - 1] = '\0'; +} + +void oracle_hotkey_combination_background_id_layout(size_t *out); +void oracle_hotkey_combination_background_id_layout(size_t *out) +{ + out[0] = sizeof(struct dstr); + out[1] = _Alignof(struct dstr); + out[2] = offsetof(struct dstr, array); + out[3] = offsetof(struct dstr, len); + out[4] = offsetof(struct dstr, capacity); + out[5] = sizeof(obs_key_combination_t); + out[6] = _Alignof(obs_key_combination_t); + out[7] = offsetof(obs_key_combination_t, modifiers); + out[8] = offsetof(obs_key_combination_t, key); + out[9] = sizeof(combo_id_core.hotkeys.background.shortcut_id); +} diff --git a/rust/obs-hotkey/src/combination_background_id.rs b/rust/obs-hotkey/src/combination_background_id.rs new file mode 100644 index 0000000000..9c508c3ee4 --- /dev/null +++ b/rust/obs-hotkey/src/combination_background_id.rs @@ -0,0 +1,31 @@ +//! The background identifier of one key combination, from the backend's +//! callback. The callback table and its mutex stay with the caller. + +/// Access to the C-owned callback slot and the identifier string. +pub trait CombinationBackgroundId { + /// The saved callback. + type Callback: Copy; + /// The identifier being built, empty when created. + type Id; + /// What the caller receives on success. + type Owned; + fn lock(&mut self) -> bool; + /// The current callback; read only while the lock is held. + fn callback(&mut self) -> Option; + fn unlock(&mut self); + /// A new, empty identifier. + fn empty(&mut self) -> Self::Id; + /// Invoke the saved callback once, after the lock is released. + fn invoke(&mut self, callback: Self::Callback, id: &mut Self::Id) -> bool; + /// Release a partial identifier after a missing callback or a failure. + fn free(&mut self, id: Self::Id); + /// Hand a successful identifier to the caller unchanged. + fn transfer(&mut self, id: Self::Id) -> Self::Owned; +} + +/// Capture the callback under the lock and call it with the lock released. +/// A failed lock returns `None` with no further access. +pub fn query(backend: &mut B) -> Option { + let _ = backend; + None +} diff --git a/rust/obs-hotkey/src/ffi/combination_background_id.rs b/rust/obs-hotkey/src/ffi/combination_background_id.rs new file mode 100644 index 0000000000..889c6cbbb2 --- /dev/null +++ b/rust/obs-hotkey/src/ffi/combination_background_id.rs @@ -0,0 +1,67 @@ +//! The public combination background-id query over C-owned state: the +//! existing hotkey mutex, the backend's callback slot and libobs's dstr. +use super::nix_hotkey_state::obs_key_combination; +use core::ffi::c_char; +use obs_util::ffi::dstr::dstr; + +/// `bool (*shortcut_id)(obs_key_combination_t, struct dstr *)` in `obs-internal.h`. +pub type ShortcutId = unsafe extern "C" fn(obs_key_combination, *mut dstr) -> bool; + +unsafe extern "C" { + fn obs_hotkey_combination_background_id_lock() -> bool; + fn obs_hotkey_combination_background_id_fields() -> *const Option; + fn obs_hotkey_combination_background_id_unlock(); + fn obs_hotkey_combination_background_id_free(id: *mut dstr); +} + +struct Background(obs_key_combination); +impl crate::combination_background_id::CombinationBackgroundId for Background { + type Callback = ShortcutId; + type Id = dstr; + type Owned = *mut c_char; + fn lock(&mut self) -> bool { + // SAFETY: the adapter checks core presence before acquiring its mutex. + unsafe { obs_hotkey_combination_background_id_lock() } + } + fn callback(&mut self) -> Option { + // SAFETY: the safe query reads the slot only after lock succeeded; the + // core and its callback table are live under the existing mutex. + unsafe { *obs_hotkey_combination_background_id_fields() } + } + fn unlock(&mut self) { + // SAFETY: the safe query releases exactly the successfully acquired lock. + unsafe { obs_hotkey_combination_background_id_unlock() }; + } + fn empty(&mut self) -> dstr { + dstr { + array: core::ptr::null_mut(), + len: 0, + capacity: 0, + } + } + fn invoke(&mut self, callback: ShortcutId, id: &mut dstr) -> bool { + // SAFETY: the backend registered this callback for exactly this + // signature; it receives the combination by value and an empty dstr + // it may fill with libobs's allocator. No lock is held. + unsafe { callback(self.0, id) } + } + fn free(&mut self, mut id: dstr) { + // SAFETY: id is empty or was filled by the callback with libobs's allocator. + unsafe { obs_hotkey_combination_background_id_free(&mut id) }; + } + fn transfer(&mut self, id: dstr) -> *mut c_char { + id.array + } +} + +/// Return the backend's bfree-owned identifier for `combo`, or NULL. +/// +/// # Safety +/// As for the C API: a registered callback must accept `combo` and fill the +/// dstr only with libobs's allocator. +#[unsafe(no_mangle)] +pub unsafe extern "C" fn obs_hotkey_combination_get_background_id( + combo: obs_key_combination, +) -> *mut c_char { + crate::combination_background_id::query(&mut Background(combo)).unwrap_or(core::ptr::null_mut()) +} diff --git a/rust/obs-hotkey/src/ffi/mod.rs b/rust/obs-hotkey/src/ffi/mod.rs index cb1479e650..cc581f56f7 100644 --- a/rust/obs-hotkey/src/ffi/mod.rs +++ b/rust/obs-hotkey/src/ffi/mod.rs @@ -11,6 +11,7 @@ pub mod binding_getters; pub mod binding_press; pub mod bindings; pub mod cocoa_keys; +pub mod combination_background_id; pub mod enumeration; pub mod hotkey_name_map; pub mod identity_getters; diff --git a/rust/obs-hotkey/src/lib.rs b/rust/obs-hotkey/src/lib.rs index 7639b6d8d1..a74ef967e0 100644 --- a/rust/obs-hotkey/src/lib.rs +++ b/rust/obs-hotkey/src/lib.rs @@ -46,6 +46,7 @@ pub mod binding_getters; pub mod binding_press; pub mod bindings; pub mod cocoa_keys; +pub mod combination_background_id; pub mod enumeration; pub mod identity_getters; pub mod identity_setters; diff --git a/rust/obs-hotkey/tests/hotkey/background_query_parity.rs b/rust/obs-hotkey/tests/hotkey/background_query_parity.rs index 43bf210cf8..82d6494972 100644 --- a/rust/obs-hotkey/tests/hotkey/background_query_parity.rs +++ b/rust/obs-hotkey/tests/hotkey/background_query_parity.rs @@ -1,26 +1,31 @@ //! Exact C/Rust query parity. No exclusions or intentional differences. -use core::ffi::{c_char, c_int, c_void}; +//! +//! The identifier lookup is the real Rust getter on the shared recording +//! fixture of `combination_background_id_parity`, never a same-named fake: +//! the getter is defined in this crate, so a second definition would collide. +use core::ffi::{CStr, c_char, c_int, c_void}; use core::mem::{align_of, offset_of, size_of}; use obs_hotkey::background_query::NATIVE; use obs_hotkey::ffi::{ - background_query::obs_hotkey_combination_is_background, nix_hotkey_state::obs_key_combination, + background_query::obs_hotkey_combination_is_background, + combination_background_id::obs_hotkey_combination_get_background_id, + nix_hotkey_state::obs_key_combination, }; use obs_util::ffi::bmem::bnum_allocs; -use obs_util::ffi::darray::{bfree, bmalloc}; +use obs_util::ffi::darray::bfree; use proptest::prelude::*; use std::cell::RefCell; #[derive(Default)] struct World { state: i32, - present: bool, queries: Vec>, releases: usize, } thread_local! { static WORLD: RefCell = RefCell::new(World::default()); } -// The production shim resolves its two unported dependencies to these local -// test hooks. They are per-thread; allocation-accounting parity re-execs first. +// The production shim resolves its unported status dependency to this local +// test hook. It is per-thread; allocation-accounting parity re-execs first. #[unsafe(no_mangle)] unsafe extern "C" fn obs_hotkeys_get_background_state(flags: *mut u32) -> c_int { assert!(flags.is_null()); @@ -30,22 +35,6 @@ unsafe extern "C" fn obs_hotkeys_get_background_state(flags: *mut u32) -> c_int }) } -#[unsafe(no_mangle)] -unsafe extern "C" fn obs_hotkey_combination_get_background_id( - combo: obs_key_combination, -) -> *mut c_char { - WORLD.with_borrow_mut(|world| { - world.queries.push(Some(combo)); - if world.present { - // SAFETY: a one-byte owned allocation is enough: the query only - // tests pointer presence and returns it to bfree without reading it. - unsafe { bmalloc(1).cast() } - } else { - core::ptr::null_mut() - } - }) -} - unsafe extern "C" fn query_state(_: *mut c_void, flags: *mut u32) -> c_int { // SAFETY: the original query passes a null optional output. unsafe { obs_hotkeys_get_background_state(flags) } @@ -59,7 +48,15 @@ unsafe extern "C" fn release(_: *mut c_void, id: *mut c_void) { // SAFETY: the original query returns exactly the owned pointer, or null. unsafe { bfree(id) }; } +type Getter = unsafe extern "C" fn(obs_key_combination) -> *mut c_char; unsafe extern "C" { + fn oracle_hotkey_combination_background_id_arm( + bits: u32, + modifiers: u32, + key: c_int, + getter: Getter, + ); + fn oracle_hotkey_combination_background_id_events(out: *mut c_char, size: usize); fn oracle_hotkey_background_query_layout(out: *mut usize); fn oracle_hotkey_background_query( combo: obs_key_combination, @@ -70,12 +67,32 @@ unsafe extern "C" { ) -> bool; } -fn reset(state: i32, present: bool) { +fn reset(state: i32, present: bool, combo: obs_key_combination) { WORLD.set(World { state, - present, ..World::default() }); + // Lock succeeds and a callback is registered; it fills the identifier + // ("background-id") or succeeds with none. + let bits = 3 | if present { 2 << 2 } else { 0 }; + // SAFETY: arms this thread's fixture; the getter only reenters on request. + unsafe { + oracle_hotkey_combination_background_id_arm( + bits, + combo.modifiers, + combo.key, + obs_hotkey_combination_get_background_id, + ); + } +} + +fn lookups() -> String { + let mut out = [0 as c_char; 64]; + // SAFETY: the oracle writes a terminated trace of at most 63 bytes. + unsafe { + oracle_hotkey_combination_background_id_events(out.as_mut_ptr(), out.len()); + CStr::from_ptr(out.as_ptr()).to_str().unwrap().to_owned() + } } #[test] @@ -100,29 +117,33 @@ fn compare(state: i32, modifiers: u32, key: i32, present: bool) { let combo = obs_key_combination { modifiers, key }; // SAFETY: accounting is read only inside the isolated test process. let before = unsafe { bnum_allocs() }; - reset(state, present); + reset(state, present, combo); // SAFETY: combo contains a valid key; our hooks provide all C dependencies. let rust = unsafe { obs_hotkey_combination_is_background(combo) }; let rust_queries = WORLD.with_borrow(|world| world.queries.clone()); + let rust_lookups = lookups(); // SAFETY: the returned identifier must have been released by the shim. assert_eq!( unsafe { bnum_allocs() }, before, "Rust leaked the identifier" ); - reset(state, present); + reset(state, present, combo); // SAFETY: all callbacks are supplied here and use only the per-thread world. let c = unsafe { oracle_hotkey_background_query(combo, core::ptr::null_mut(), query_state, query_id, release) }; assert_eq!(rust, c); + assert_eq!(lookups(), rust_lookups); + // The fixture marks a combination that differs from this one with 'K'. + assert_eq!(rust_lookups, if state == NATIVE { "" } else { "LRUC" }); WORLD.with_borrow(|world| { assert_eq!(world.queries, rust_queries); assert_eq!(world.releases, usize::from(state != NATIVE)); }); // SAFETY: the oracle callback returns the identifier to the same allocator. assert_eq!(unsafe { bnum_allocs() }, before, "C leaked the identifier"); - assert_eq!(rust_queries.len(), if state == NATIVE { 1 } else { 2 }); + assert_eq!(rust_queries.len(), 1); } #[test] diff --git a/rust/obs-hotkey/tests/hotkey/combination_background_id.rs b/rust/obs-hotkey/tests/hotkey/combination_background_id.rs new file mode 100644 index 0000000000..fba86bbef5 --- /dev/null +++ b/rust/obs-hotkey/tests/hotkey/combination_background_id.rs @@ -0,0 +1,231 @@ +//! Tier 1: every assertion of the unchanged +//! test_hotkey_combination_background_id.c against the safe query. The +//! backend records lock, field read, unlock, call and free, and swaps the +//! callback slot at unlock so a read after unlock is caught. +use obs_hotkey::combination_background_id::{CombinationBackgroundId, query}; + +#[derive(Clone, Copy, PartialEq, Eq, Debug)] +enum Output { + None, + Empty, + Text, + Partial, + FailedEmpty, + Reenter, +} + +#[derive(Clone, Copy, PartialEq, Eq, Debug)] +enum Slot { + Empty, + Callback, + Wrong, +} + +#[derive(Clone, Copy, PartialEq, Eq, Debug)] +struct Combo { + modifiers: u32, + key: i32, +} + +struct Backend { + lock_result: bool, + locked: bool, + with_callback: bool, + slot: Slot, + combo: Combo, + output: Output, + events: String, + locks: u32, + unlocks: u32, + reads: u32, + calls: u32, + frees: u32, + allocation: Option<*const u8>, +} + +impl CombinationBackgroundId for Backend { + type Callback = Slot; + type Id = Option; + type Owned = Option; + fn lock(&mut self) -> bool { + assert!(!self.locked); + self.locks += 1; + self.events.push('L'); + self.locked = self.lock_result; + // The current callback becomes available only when locking succeeds. + self.slot = if self.with_callback { + Slot::Callback + } else { + Slot::Empty + }; + self.lock_result + } + fn callback(&mut self) -> Option { + assert!(self.locked); + self.reads += 1; + self.events.push('R'); + (self.slot != Slot::Empty).then_some(self.slot) + } + fn unlock(&mut self) { + assert!(self.locked); + assert_eq!(self.reads, self.locks); + self.unlocks += 1; + self.events.push('U'); + self.locked = false; + // The wrapper must invoke its saved callback, not read this after unlock. + self.slot = Slot::Wrong; + } + fn empty(&mut self) -> Option { + None + } + fn invoke(&mut self, callback: Slot, id: &mut Option) -> bool { + assert_eq!(callback, Slot::Callback, "wrong callback invoked"); + assert!(!self.locked); + assert_eq!(self.unlocks, 1); + assert_eq!(self.calls, 0); + self.calls += 1; + self.events.push('C'); + assert!(id.is_none()); + match self.output { + Output::Reenter => { + // Reenter the query after release, with no nested callback. + self.with_callback = false; + assert!(query(self).is_none()); + } + Output::None | Output::FailedEmpty => {} + output => { + let text = if output == Output::Empty { + String::with_capacity(1) + } else { + "background-id".to_owned() + }; + self.allocation = Some(text.as_ptr()); + *id = Some(text); + } + } + !matches!(self.output, Output::Partial | Output::FailedEmpty) + } + fn free(&mut self, id: Option) { + assert!(!self.locked); + self.frees += 1; + self.events.push('F'); + assert_eq!(id.as_ref().map(|id| id.as_ptr()), self.allocation); + } + fn transfer(&mut self, id: Option) -> Option { + id + } +} + +fn run(success: bool, with_callback: bool, output: Output, combo: Combo) { + let mut backend = Backend { + lock_result: success, + locked: false, + with_callback, + slot: Slot::Wrong, + combo, + output, + events: String::new(), + locks: 0, + unlocks: 0, + reads: 0, + calls: 0, + frees: 0, + allocation: None, + }; + let result = query(&mut backend); + let invoked = success && with_callback; + let nested = invoked && output == Output::Reenter; + let b = &backend; + assert!(!b.locked); + assert_eq!(b.combo, combo); + assert_eq!(b.locks, if nested { 2 } else { 1 }); + assert_eq!( + b.unlocks, + match (success, nested) { + (false, _) => 0, + (true, true) => 2, + (true, false) => 1, + } + ); + assert_eq!(b.reads, b.unlocks); + assert_eq!(b.calls, u32::from(invoked)); + let freed = success + && (!with_callback || matches!(output, Output::Partial | Output::FailedEmpty) || nested); + assert_eq!(b.frees, u32::from(freed)); + assert_eq!( + b.events, + expected_events(success, with_callback, output), + "{success} {with_callback} {output:?}" + ); + check_result(result, invoked, output, b.allocation); +} + +fn expected_events(success: bool, with_callback: bool, output: Output) -> &'static str { + if !success { + "L" + } else if !with_callback { + "LRUF" + } else if output == Output::Reenter { + "LRUCLRUF" + } else if matches!(output, Output::Partial | Output::FailedEmpty) { + "LRUCF" + } else { + "LRUC" + } +} + +fn check_result( + result: Option>, + invoked: bool, + output: Output, + allocation: Option<*const u8>, +) { + if invoked && matches!(output, Output::Empty | Output::Text) { + // Exact ownership transfer: the callback's allocation, not a copy. + let id = result.flatten().expect("successful identifier"); + assert_eq!(Some(id.as_ptr()), allocation); + assert_eq!( + id, + if output == Output::Empty { + "" + } else { + "background-id" + } + ); + } else if invoked && matches!(output, Output::None | Output::Reenter) { + // Characterized, not endorsed: callback success may return NULL. + assert_eq!(result, Some(None)); + } else { + assert_eq!(result, None); + } +} + +#[test] +fn lock_and_callback_contract() { + let combinations = [ + Combo { + modifiers: 0, + key: 0, + }, + // INTERACT_CONTROL_KEY | INTERACT_SHIFT_KEY with OBS_KEY_A. + Combo { + modifiers: (1 << 2) | (1 << 1), + key: 1, + }, + Combo { + modifiers: u32::MAX, + key: -1, + }, + ]; + for combo in combinations { + run(false, false, Output::None, combo); + run(false, true, Output::Partial, combo); + run(true, false, Output::None, combo); + run(true, true, Output::None, combo); + run(true, true, Output::Empty, combo); + run(true, true, Output::Text, combo); + run(true, true, Output::Partial, combo); + run(true, true, Output::FailedEmpty, combo); + run(true, true, Output::Reenter, combo); + } +} diff --git a/rust/obs-hotkey/tests/hotkey/combination_background_id_parity.rs b/rust/obs-hotkey/tests/hotkey/combination_background_id_parity.rs new file mode 100644 index 0000000000..c2842893ff --- /dev/null +++ b/rust/obs-hotkey/tests/hotkey/combination_background_id_parity.rs @@ -0,0 +1,239 @@ +//! Tier 3: the original extracted C wrapper versus the shim on one recording +//! fixture, through the same real-named lock, callback-slot, unlock and +//! dstr_free adapters. Compares the full trace, the returned pointer and text +//! and allocation balance. No intentional differences. Mutants show the probe +//! catches each broken contract. +use core::ffi::{CStr, c_char, c_int}; +use core::mem::{align_of, offset_of, size_of}; +use obs_hotkey::ffi::combination_background_id::{ + ShortcutId, obs_hotkey_combination_get_background_id, +}; +use obs_hotkey::ffi::nix_hotkey_state::obs_key_combination; +use obs_util::ffi::dstr::dstr; +use proptest::prelude::*; + +type Getter = unsafe extern "C" fn(obs_key_combination) -> *mut c_char; +unsafe extern "C" { + fn oracle_hotkey_combination_background_id_probe( + bits: u32, + modifiers: u32, + key: c_int, + getter: Getter, + ) -> u32; + fn oracle_hotkey_combination_background_id_trace( + bits: u32, + getter: Option, + out: *mut c_char, + size: usize, + ); + fn oracle_hotkey_combination_background_id_layout(out: *mut usize); + fn obs_hotkey_combination_background_id_lock() -> bool; + fn obs_hotkey_combination_background_id_fields() -> *const Option; + fn obs_hotkey_combination_background_id_unlock(); + fn obs_hotkey_combination_background_id_free(id: *mut dstr); +} + +/// Bits: 1 lock succeeds, 2 a callback is registered, then the output kind +/// (none, empty, text, partial, failed empty, reenter) from bit 2. +const KINDS: u32 = 6; + +fn probe(bits: u32, modifiers: u32, key: c_int, getter: Getter) -> u32 { + // SAFETY: the oracle owns the whole fixture and passes the getter only a + // combination; every adapter the getter reaches records into it. + unsafe { oracle_hotkey_combination_background_id_probe(bits, modifiers, key, getter) } +} + +fn check(bits: u32, modifiers: u32, key: c_int) { + assert_eq!( + probe( + bits, + modifiers, + key, + obs_hotkey_combination_get_background_id + ), + 0, + "bits={bits:#x} modifiers={modifiers:#x} key={key}" + ); +} + +fn trace(bits: u32, getter: Option) -> String { + let mut out = [0 as c_char; 64]; + // SAFETY: the oracle writes a terminated trace of at most 63 bytes. + unsafe { + oracle_hotkey_combination_background_id_trace(bits, getter, out.as_mut_ptr(), out.len()); + CStr::from_ptr(out.as_ptr()).to_str().unwrap().to_owned() + } +} + +#[test] +fn real_header_layout() { + let mut layout = [0; 10]; + // SAFETY: the oracle writes ten size_t measurements from the real headers. + unsafe { oracle_hotkey_combination_background_id_layout(layout.as_mut_ptr()) }; + assert_eq!( + layout, + [ + size_of::(), + align_of::(), + offset_of!(dstr, array), + offset_of!(dstr, len), + offset_of!(dstr, capacity), + size_of::(), + align_of::(), + offset_of!(obs_key_combination, modifiers), + offset_of!(obs_key_combination, key), + size_of::>(), + ] + ); +} + +#[test] +fn original_traces_match_the_characterization() { + // The C characterization's expected sequences, from the original itself. + let kind = |k: u32| k << 2; + for (bits, expected) in [ + (0, "L"), + (2 | kind(3), "L"), + (1, "LRUF"), + (3, "LRUC"), + (3 | kind(1), "LRUC"), + (3 | kind(2), "LRUC"), + (3 | kind(3), "LRUCF"), + (3 | kind(4), "LRUCF"), + (3 | kind(5), "LRUCLRUF"), + ] { + assert_eq!(trace(bits, None), expected, "bits={bits:#x}"); + assert_eq!( + trace(bits, Some(obs_hotkey_combination_get_background_id)), + expected, + "bits={bits:#x}" + ); + } +} + +#[test] +fn characterized_edges() { + let combinations = [(0, 0), ((1 << 2) | (1 << 1), 1), (u32::MAX, -1)]; + for bits in 0..4 * KINDS { + for (modifiers, key) in combinations { + check(bits, modifiers, key); + } + } +} + +proptest! { + #![proptest_config(ProptestConfig { + cases: obs_c_oracle::parity::cases(256), failure_persistence: None, + ..ProptestConfig::default() + })] + #[test] + fn get_background_id_matches_c(bits in 0..4 * KINDS, modifiers in any::(), key in any::()) { + check(bits, modifiers, key); + } +} + +fn empty() -> dstr { + dstr { + array: core::ptr::null_mut(), + len: 0, + capacity: 0, + } +} + +unsafe extern "C" fn calls_while_locked(combo: obs_key_combination) -> *mut c_char { + // SAFETY: fixture adapters; the call deliberately happens before unlock. + unsafe { + if !obs_hotkey_combination_background_id_lock() { + return core::ptr::null_mut(); + } + let mut id = empty(); + let callback = *obs_hotkey_combination_background_id_fields(); + let ok = callback.is_some_and(|callback| callback(combo, &mut id)); + obs_hotkey_combination_background_id_unlock(); + if ok { + return id.array; + } + obs_hotkey_combination_background_id_free(&mut id); + core::ptr::null_mut() + } +} + +unsafe extern "C" fn reads_after_unlock(combo: obs_key_combination) -> *mut c_char { + // SAFETY: fixture adapters; the slot is deliberately re-read after unlock. + unsafe { + if !obs_hotkey_combination_background_id_lock() { + return core::ptr::null_mut(); + } + let _ = *obs_hotkey_combination_background_id_fields(); + obs_hotkey_combination_background_id_unlock(); + let mut id = empty(); + let callback = *obs_hotkey_combination_background_id_fields(); + if callback.is_some_and(|callback| callback(combo, &mut id)) { + return id.array; + } + obs_hotkey_combination_background_id_free(&mut id); + core::ptr::null_mut() + } +} + +unsafe extern "C" fn leaks_partial(combo: obs_key_combination) -> *mut c_char { + // SAFETY: fixture adapters; a failed call deliberately skips the free. + unsafe { + if !obs_hotkey_combination_background_id_lock() { + return core::ptr::null_mut(); + } + let callback = *obs_hotkey_combination_background_id_fields(); + obs_hotkey_combination_background_id_unlock(); + let mut id = empty(); + if callback.is_some_and(|callback| callback(combo, &mut id)) { + id.array + } else { + core::ptr::null_mut() + } + } +} + +unsafe extern "C" fn drops_result(combo: obs_key_combination) -> *mut c_char { + // SAFETY: as the shim, but the identifier is deliberately freed. + let id = unsafe { obs_hotkey_combination_get_background_id(combo) }; + // SAFETY: the shim returns a bfree-owned identifier or NULL. + unsafe { obs_util::ffi::darray::bfree(id.cast()) }; + core::ptr::null_mut() +} + +unsafe extern "C" fn reads_on_failed_lock(combo: obs_key_combination) -> *mut c_char { + // SAFETY: fixture adapters; the slot is deliberately read without the lock. + unsafe { + if !obs_hotkey_combination_background_id_lock() { + let _ = *obs_hotkey_combination_background_id_fields(); + return core::ptr::null_mut(); + } + obs_hotkey_combination_background_id_unlock(); + obs_hotkey_combination_get_background_id(combo) + } +} + +unsafe extern "C" fn wrong_combo(combo: obs_key_combination) -> *mut c_char { + let combo = obs_key_combination { + modifiers: combo.modifiers ^ 1, + key: combo.key, + }; + // SAFETY: as the shim, with a deliberately altered combination. + unsafe { obs_hotkey_combination_get_background_id(combo) } +} + +#[test] +fn mutants_are_caught() { + let text = 3 | (2 << 2); + let partial = 3 | (3 << 2); + let failed_lock = 2 | (2 << 2); + assert_eq!(probe(text, 0, 1, calls_while_locked) & 1, 1); + assert_eq!(probe(text, 0, 1, reads_after_unlock) & 1, 1); + assert_eq!(probe(partial, 0, 1, leaks_partial) & 1, 1); + assert_eq!(probe(text, 0, 1, drops_result) & 2, 2); + assert_eq!(probe(failed_lock, 0, 1, reads_on_failed_lock) & 1, 1); + assert_eq!(probe(text, 0, 1, wrong_combo) & 1, 1); + for bits in [text, partial, failed_lock] { + check(bits, 0, 1); + } +} diff --git a/rust/obs-hotkey/tests/hotkey/main.rs b/rust/obs-hotkey/tests/hotkey/main.rs index 0b2e5a8986..db57cadbae 100644 --- a/rust/obs-hotkey/tests/hotkey/main.rs +++ b/rust/obs-hotkey/tests/hotkey/main.rs @@ -22,6 +22,8 @@ mod bindings; mod bindings_parity; mod cocoa_keys; mod cocoa_keys_parity; +mod combination_background_id; +mod combination_background_id_parity; mod enumeration; mod enumeration_parity; mod hotkey_name_map; From 4e1699e9c4332cd6b9e4824800a240164a202dcb Mon Sep 17 00:00:00 2001 From: Ryan Doupe Date: Thu, 19 Nov 2026 20:52:19 -0500 Subject: [PATCH 2/2] obs-hotkey: swap hotkey-combination background-id query to safe Rust (Refs #5288) obs_hotkey_combination_get_background_id now comes from obs-hotkey in the hybrid libobs. Safe Rust takes the existing hotkey mutex, captures the backend's shortcut_id callback, releases the lock and only then calls the saved callback once with the combination by value. A failed lock returns NULL with no field read; a missing callback or a false result frees the partial dstr and returns NULL; a true result transfers id.array unchanged. The mutex, callback slot and dstr_free stay in C behind private adapters. The original wrapper moves to libobs.ported-hotkey-combination-background-id, which gives it its static lock pair through the existing seam. soldr cargo test -p obs-data --test obs-tests-core -- hotkey::combination_background_id hotkey::background_query test result: ok. 10 passed; 0 failed Co-Authored-By: Claude Opus 5.5 Ci-Attestation: {"at":1795141142,"gate":"data/all/compatibility","host":"linux-x86_64","key":"","lane":"client-checks","parents":["1bcf2a898d96a1c7bfdfe065d38e814718bcd829"],"secs":94,"stamp":"dae2b56ee79163274b0b3f9c597495bf","tree":"dba1e45a032bf9008dd396ae7ad361ca60dc3f4d","v":1,"via":"run"} Ci-Attestation: {"at":1795141142,"gate":"data/all/flatpak-manifest","host":"linux-x86_64","key":"","lane":"client-checks","parents":["1bcf2a898d96a1c7bfdfe065d38e814718bcd829"],"secs":94,"stamp":"64dadec6784a44b6b04a04b11175fd85","tree":"dba1e45a032bf9008dd396ae7ad361ca60dc3f4d","v":1,"via":"run"} Ci-Attestation: {"at":1795141142,"gate":"data/all/qt-xml","host":"linux-x86_64","key":"","lane":"client-checks","parents":["1bcf2a898d96a1c7bfdfe065d38e814718bcd829"],"secs":94,"stamp":"63eeff4d6503b5e0bf71081f3a916378","tree":"dba1e45a032bf9008dd396ae7ad361ca60dc3f4d","v":1,"via":"run"} Ci-Attestation: {"at":1795141142,"gate":"data/all/services-schema","host":"linux-x86_64","key":"","lane":"client-checks","parents":["1bcf2a898d96a1c7bfdfe065d38e814718bcd829"],"secs":94,"stamp":"c553f39cc407a25f59f79cfded9542f1","tree":"dba1e45a032bf9008dd396ae7ad361ca60dc3f4d","v":1,"via":"run"} Ci-Attestation: {"at":1795141047,"gate":"general/all/client-lint","host":"linux-x86_64","key":"8237acc3dd2eeb6c241b8077be33f3e8eaf869884058d8f0c801ef7659da4329","lane":"client-lint","parents":["1bcf2a898d96a1c7bfdfe065d38e814718bcd829"],"secs":63,"stamp":"1fa72d8b2329b0c0b960fb2a4d389559","tree":"dba1e45a032bf9008dd396ae7ad361ca60dc3f4d","v":1,"via":"run"} Ci-Attestation: {"at":1795141142,"gate":"python/all/self-test","host":"linux-x86_64","key":"","lane":"client-checks","parents":["1bcf2a898d96a1c7bfdfe065d38e814718bcd829"],"secs":94,"stamp":"b95bfa8d10cb18f27474db7d9f9fbdd6","tree":"dba1e45a032bf9008dd396ae7ad361ca60dc3f4d","v":1,"via":"run"} Ci-Attestation: {"at":1795141047,"gate":"rust/aarch64-apple-darwin/client-clippy","host":"linux-x86_64","key":"8237acc3dd2eeb6c241b8077be33f3e8eaf869884058d8f0c801ef7659da4329","lane":"client-lint","parents":["1bcf2a898d96a1c7bfdfe065d38e814718bcd829"],"secs":63,"stamp":"3c29b023b1aba9c71d4e0d240f4c4c1c","tree":"dba1e45a032bf9008dd396ae7ad361ca60dc3f4d","v":1,"via":"run"} Ci-Attestation: {"at":1795141047,"gate":"rust/aarch64-apple-darwin/client-dylint","host":"linux-x86_64","key":"8237acc3dd2eeb6c241b8077be33f3e8eaf869884058d8f0c801ef7659da4329","lane":"client-lint","parents":["1bcf2a898d96a1c7bfdfe065d38e814718bcd829"],"secs":63,"stamp":"68a93310b322d57de61f713576074ed6","tree":"dba1e45a032bf9008dd396ae7ad361ca60dc3f4d","v":1,"via":"run"} Ci-Attestation: {"at":1795141047,"gate":"rust/aarch64-pc-windows-msvc/client-clippy","host":"linux-x86_64","key":"8237acc3dd2eeb6c241b8077be33f3e8eaf869884058d8f0c801ef7659da4329","lane":"client-lint","parents":["1bcf2a898d96a1c7bfdfe065d38e814718bcd829"],"secs":63,"stamp":"4f4833965661898ec04cee56eda48f90","tree":"dba1e45a032bf9008dd396ae7ad361ca60dc3f4d","v":1,"via":"run"} Ci-Attestation: {"at":1795141047,"gate":"rust/aarch64-pc-windows-msvc/client-dylint","host":"linux-x86_64","key":"8237acc3dd2eeb6c241b8077be33f3e8eaf869884058d8f0c801ef7659da4329","lane":"client-lint","parents":["1bcf2a898d96a1c7bfdfe065d38e814718bcd829"],"secs":63,"stamp":"f1f22e63a4ab31a5588cc91a64694afd","tree":"dba1e45a032bf9008dd396ae7ad361ca60dc3f4d","v":1,"via":"run"} Ci-Attestation: {"at":1795141047,"gate":"rust/aarch64-unknown-linux-gnu/client-clippy","host":"linux-x86_64","key":"8237acc3dd2eeb6c241b8077be33f3e8eaf869884058d8f0c801ef7659da4329","lane":"client-lint","parents":["1bcf2a898d96a1c7bfdfe065d38e814718bcd829"],"secs":63,"stamp":"923ab93b6ee188ec99c42df526f4e6ad","tree":"dba1e45a032bf9008dd396ae7ad361ca60dc3f4d","v":1,"via":"run"} Ci-Attestation: {"at":1795141047,"gate":"rust/aarch64-unknown-linux-gnu/client-dylint","host":"linux-x86_64","key":"8237acc3dd2eeb6c241b8077be33f3e8eaf869884058d8f0c801ef7659da4329","lane":"client-lint","parents":["1bcf2a898d96a1c7bfdfe065d38e814718bcd829"],"secs":63,"stamp":"460214df84df78bd764d87b4708876c7","tree":"dba1e45a032bf9008dd396ae7ad361ca60dc3f4d","v":1,"via":"run"} Ci-Attestation: {"at":1795141047,"gate":"rust/x86_64-apple-darwin/client-clippy","host":"linux-x86_64","key":"8237acc3dd2eeb6c241b8077be33f3e8eaf869884058d8f0c801ef7659da4329","lane":"client-lint","parents":["1bcf2a898d96a1c7bfdfe065d38e814718bcd829"],"secs":63,"stamp":"ae4245f056e3477a35e8ca9de2e728f6","tree":"dba1e45a032bf9008dd396ae7ad361ca60dc3f4d","v":1,"via":"run"} Ci-Attestation: {"at":1795141047,"gate":"rust/x86_64-apple-darwin/client-dylint","host":"linux-x86_64","key":"8237acc3dd2eeb6c241b8077be33f3e8eaf869884058d8f0c801ef7659da4329","lane":"client-lint","parents":["1bcf2a898d96a1c7bfdfe065d38e814718bcd829"],"secs":63,"stamp":"15552ab23b2a9cabf588eaf83ff11702","tree":"dba1e45a032bf9008dd396ae7ad361ca60dc3f4d","v":1,"via":"run"} Ci-Attestation: {"at":1795141047,"gate":"rust/x86_64-pc-windows-msvc/client-clippy","host":"linux-x86_64","key":"8237acc3dd2eeb6c241b8077be33f3e8eaf869884058d8f0c801ef7659da4329","lane":"client-lint","parents":["1bcf2a898d96a1c7bfdfe065d38e814718bcd829"],"secs":63,"stamp":"5b7d41956b8148dd31022c9284f24005","tree":"dba1e45a032bf9008dd396ae7ad361ca60dc3f4d","v":1,"via":"run"} Ci-Attestation: {"at":1795141047,"gate":"rust/x86_64-pc-windows-msvc/client-dylint","host":"linux-x86_64","key":"8237acc3dd2eeb6c241b8077be33f3e8eaf869884058d8f0c801ef7659da4329","lane":"client-lint","parents":["1bcf2a898d96a1c7bfdfe065d38e814718bcd829"],"secs":63,"stamp":"d5cff465fef4180d3508352bf042e701","tree":"dba1e45a032bf9008dd396ae7ad361ca60dc3f4d","v":1,"via":"run"} Ci-Attestation: {"at":1795141047,"gate":"rust/x86_64-unknown-linux-gnu/client-clippy","host":"linux-x86_64","key":"8237acc3dd2eeb6c241b8077be33f3e8eaf869884058d8f0c801ef7659da4329","lane":"client-lint","parents":["1bcf2a898d96a1c7bfdfe065d38e814718bcd829"],"secs":63,"stamp":"45496753a706a23275e544311d4afbfc","tree":"dba1e45a032bf9008dd396ae7ad361ca60dc3f4d","v":1,"via":"run"} Ci-Attestation: {"at":1795141047,"gate":"rust/x86_64-unknown-linux-gnu/client-dylint","host":"linux-x86_64","key":"8237acc3dd2eeb6c241b8077be33f3e8eaf869884058d8f0c801ef7659da4329","lane":"client-lint","parents":["1bcf2a898d96a1c7bfdfe065d38e814718bcd829"],"secs":63,"stamp":"d8452b7d0f552c98d074c51d561c234b","tree":"dba1e45a032bf9008dd396ae7ad361ca60dc3f4d","v":1,"via":"run"} Ci-Attestation: {"at":1795141366,"gate":"rust/x86_64-unknown-linux-gnu/client-test","host":"linux-x86_64","key":"b2087d1d1f640d75467108e499b87897e52d9b8d8227fb8a6f2907a94f11b2af","lane":"client-test-x86_64-unknown-linux-gnu","parents":["1bcf2a898d96a1c7bfdfe065d38e814718bcd829"],"secs":224,"stamp":"0969529f982fd5c9af60ff84a18ee586","tree":"dba1e45a032bf9008dd396ae7ad361ca60dc3f4d","v":1,"via":"run"} Client-Checks: {"checks":{"base":"869cc329a4d9053ba424088d01b05b6bb695bcad","coverage":"all-tracked-applicable-files","diff":["ci/test_hotkey_register_extraction.py","libobs/build/libobs.core+.h.c","libobs/build/libobs.ported-hotkey-combination-background-id+.h.c","libobs/build/ported-source-sets/hotkey-combination-background-id.json","libobs/cmake/rust-exports.d/obs-hotkey-combination_background_id.txt","libobs/obs-hotkey-combination-background-id-fields.c.h","libobs/obs-hotkey-combination-background-id-lock.c.h","libobs/obs-hotkey-combination-background-id.c.h","libobs/obs-hotkey.c.h","libobs/obs-hotkeys-background-status-changed.c.h","rust/README.md","rust/obs-build/tests/native.rs","rust/obs-build/tests/native/registration/canonical_hotkey_combination_background_id_swap_is_registered.rs","rust/obs-build/tests/native/registration/headless_characterizations_expected.json","rust/obs-build/tests/native/registration/portal_output_expected.json","rust/obs-build/tests/native/registration/rust-static/cases/hotkey-control/test_hotkey_background_enum.json","rust/obs-build/tests/native/registration/rust-static/cases/hotkey-control/test_hotkey_background_press.json","rust/obs-build/tests/native/registration/rust-static/cases/hotkey-control/test_hotkey_background_trigger.json","rust/obs-build/tests/native/registration/rust-static/cases/hotkey-control/test_hotkey_binding_press_steps.json","rust/obs-build/tests/native/registration/rust-static/cases/hotkey-control/test_hotkey_getters_characterization.json","rust/obs-build/tests/native/registration/rust-static/cases/hotkey-control/test_hotkey_load.json","rust/obs-build/tests/native/registration/rust-static/cases/hotkey-control/test_hotkey_pair_steps.json","rust/obs-build/tests/native/registration/rust-static/cases/hotkey-control/test_hotkey_press_steps.json","rust/obs-build/tests/native/registration/rust-static/cases/hotkey-control/test_hotkey_unregister_entry.json","rust/obs-build/tests/native/registration/rust-static/cases/hotkey-control/test_portal_shortcuts.json","rust/obs-build/tests/native/registration/rust-static/cases/hotkey-control/test_wayland_hotkeys.json","rust/obs-c-oracle/oracle/hotkey_combination_background_id.c","rust/obs-hotkey/src/combination_background_id.rs","rust/obs-hotkey/src/ffi/combination_background_id.rs","rust/obs-hotkey/src/ffi/mod.rs","rust/obs-hotkey/src/lib.rs","rust/obs-hotkey/tests/hotkey/background_query_parity.rs","rust/obs-hotkey/tests/hotkey/combination_background_id.rs","rust/obs-hotkey/tests/hotkey/combination_background_id_parity.rs","rust/obs-hotkey/tests/hotkey/main.rs","test/cmocka/build/test-graph.json","test/cmocka/test_hotkeys_background_status_changed.c","test/cmocka/tests.d/test_hotkeys_background_status_changed.cmake"],"head":"987b89c46ed3aaeab67f31cf67cf5eb924acd716","tools":{"orchestrator-python":"3.13.16 (main, Oct 14 2026, 01:04:01) [Clang 23.1.3 ]","schema-dependencies":"3.13.16 (main, Oct 14 2026, 01:04:01) [Clang 23.1.3 ]\n{'jsonschema': '4.26.0', 'json_source_map': '1.0.5'}","uv":"uv 0.13.5 (x86_64-unknown-linux-gnu)","xmllint":"xmllint: using libxml version 21504-GITv2.15.4\n compiled with: Threads Tree Output Push Reader Patterns Writer SAXv1 DTDValid HTML C14N Catalog XPath XPointer XInclude Iconv ICU ISO8859X Regexps Automata RelaxNG Schemas Schematron Modules Debug Zlib"},"tracked-digest":"9e9e205e8e89c2f6ff36b305ea2f3965453462c9ae322234adc4a6cb87791544","tree":"dba1e45a032bf9008dd396ae7ad361ca60dc3f4d"}} --- libobs/build/libobs.core+.h.c | 1 - ...rted-hotkey-combination-background-id+.h.c | 6 +++ .../hotkey-combination-background-id.json | 5 +++ .../obs-hotkey-combination_background_id.txt | 1 + ...-hotkey-combination-background-id-lock.c.h | 16 +++++++ .../obs-hotkey-combination-background-id.c.h | 3 +- rust/README.md | 1 + rust/obs-build/tests/native.rs | 2 + ...nation_background_id_swap_is_registered.rs | 42 +++++++++++++++++++ .../src/combination_background_id.rs | 15 ++++++- 10 files changed, 88 insertions(+), 4 deletions(-) create mode 100644 libobs/build/libobs.ported-hotkey-combination-background-id+.h.c create mode 100644 libobs/build/ported-source-sets/hotkey-combination-background-id.json create mode 100644 libobs/cmake/rust-exports.d/obs-hotkey-combination_background_id.txt create mode 100644 libobs/obs-hotkey-combination-background-id-lock.c.h create mode 100644 rust/obs-build/tests/native/registration/canonical_hotkey_combination_background_id_swap_is_registered.rs diff --git a/libobs/build/libobs.core+.h.c b/libobs/build/libobs.core+.h.c index 2c61839753..8224ce55a1 100644 --- a/libobs/build/libobs.core+.h.c +++ b/libobs/build/libobs.core+.h.c @@ -26,7 +26,6 @@ #include "libobs/obs-hotkey-background-state.c.h" #include "libobs/obs-hotkey-background-trigger-fields.c.h" #include "libobs/obs-hotkey-combination-background-id-fields.c.h" -#include "libobs/obs-hotkey-combination-background-id.c.h" #include "libobs/obs-hotkey-enum-fields.c.h" #include "libobs/obs-hotkey-identity-fields.c.h" #include "libobs/obs-hotkey-identity-setter-fields.c.h" diff --git a/libobs/build/libobs.ported-hotkey-combination-background-id+.h.c b/libobs/build/libobs.ported-hotkey-combination-background-id+.h.c new file mode 100644 index 0000000000..83e5eb1624 --- /dev/null +++ b/libobs/build/libobs.ported-hotkey-combination-background-id+.h.c @@ -0,0 +1,6 @@ +/* Unity group (BUILD.md): original combination background-id query for the C reference. */ + +#include "libobs/build/libobs.pch.h" + +#include "libobs/obs-hotkey-combination-background-id-lock.c.h" +#include "libobs/obs-hotkey-combination-background-id.c.h" diff --git a/libobs/build/ported-source-sets/hotkey-combination-background-id.json b/libobs/build/ported-source-sets/hotkey-combination-background-id.json new file mode 100644 index 0000000000..6571b4b867 --- /dev/null +++ b/libobs/build/ported-source-sets/hotkey-combination-background-id.json @@ -0,0 +1,5 @@ +{ + "LIBOBS_PORTED_SOURCES": [ + "build/libobs.ported-hotkey-combination-background-id+.h.c" + ] +} diff --git a/libobs/cmake/rust-exports.d/obs-hotkey-combination_background_id.txt b/libobs/cmake/rust-exports.d/obs-hotkey-combination_background_id.txt new file mode 100644 index 0000000000..8fdf58b36e --- /dev/null +++ b/libobs/cmake/rust-exports.d/obs-hotkey-combination_background_id.txt @@ -0,0 +1 @@ +obs_hotkey_combination_get_background_id diff --git a/libobs/obs-hotkey-combination-background-id-lock.c.h b/libobs/obs-hotkey-combination-background-id-lock.c.h new file mode 100644 index 0000000000..c708a9fa53 --- /dev/null +++ b/libobs/obs-hotkey-combination-background-id-lock.c.h @@ -0,0 +1,16 @@ +#include "libobs/build/libobs.pch.h" + +extern bool obs_hotkey_seam_lock(void); +extern void obs_hotkey_seam_unlock(void); + +/* The extracted wrapper calls the static lock pair of obs-hotkey.c.h. In the + * C reference's own group it reaches the same mutex through the seam. */ +static inline bool lock(void) +{ + return obs_hotkey_seam_lock(); +} + +static inline void unlock(void) +{ + obs_hotkey_seam_unlock(); +} diff --git a/libobs/obs-hotkey-combination-background-id.c.h b/libobs/obs-hotkey-combination-background-id.c.h index 190dfcb049..d424b6c28e 100644 --- a/libobs/obs-hotkey-combination-background-id.c.h +++ b/libobs/obs-hotkey-combination-background-id.c.h @@ -1,6 +1,7 @@ #include "libobs/build/libobs.pch.h" -/* Defined in obs-hotkey.c.h, later in the same unity group. */ +/* Defined in obs-hotkey.c.h later in a hotkey unity group, or before this + * file by obs-hotkey-combination-background-id-lock.c.h in the C reference. */ static inline bool lock(void); static inline void unlock(void); diff --git a/rust/README.md b/rust/README.md index a2c5b07f13..bb640ce228 100644 --- a/rust/README.md +++ b/rust/README.md @@ -128,6 +128,7 @@ The private output starting/encoder-demand queries use lazy safe Rust decisions | `libobs/obs-hotkey-unregister-entry.c.h` | `obs-hotkey::unregister_entry` | Public single/pair unregistration dispatch through the existing lock/unlock seams, with exact size_t forwarding and no-core silence. The existing single remover and real C pair helper retain notification, binding/weak-owner release and member/hash/free order. Frozen Tier 2 recorder and existing API tests run unchanged; Tier 3 compares original-C seam traces with bounded edge/random calls. No intentional differences; no layouts cross the new boundary. | | `libobs/obs-hotkey-background-query.c.h` | `obs-hotkey::background_query` | Swapped in hybrid libobs (#3969). The public combination query preserves the native short circuit, identifier lookup and release. Actual-library native fallback characterization, real-header combination layout, callback-based differential parity and isolated allocation accounting. No exclusions or intentional differences. | | `libobs/obs-hotkey-background-trigger.c.h` | `obs-hotkey::background_trigger` | Owned background trigger retrieval under the existing hotkey mutex. Preserves exact combo/wake_data, callback-before-unlock, rejected-output cleanup and exact allocation transfer, including allocated empty and successful NULL results. Unchanged C characterization, safe-core assertions, real-header layouts and differential ownership/dispatch parity. No intentional differences. | +| `libobs/obs-hotkey-combination-background-id.c.h` | `obs-hotkey::combination_background_id` | Swapped in hybrid `libobs` (group `libobs.ported-hotkey-combination-background-id` keeps the original for the C reference). Safe Rust takes the existing hotkey mutex, captures the backend's `shortcut_id` callback, releases the lock and only then calls the saved callback once with the combination by value. A failed lock returns NULL with no field read; a missing callback or a false result frees the partial dstr and returns NULL; a true result transfers `id.array` unchanged, NULL or an allocated empty string included. The mutex, the callback slot and `dstr_free` stay in C behind private adapters (`obs-hotkey-combination-background-id-fields.c.h`), so the caller's bfree always matches the callback's allocator. Tier 1 mirrors every assertion of the unchanged `test_hotkey_combination_background_id`; Tier 3 runs the original and the shim on one recording fixture (trace, returned pointer and text) with real-header `dstr`/combination layout checks and mutants for a call under the lock, a read after unlock, a leaked partial id, a dropped result, a read after a failed lock and an altered combination. `background_query_parity` now drives this real getter instead of a same-named fake. No intentional differences; no network protocol (Tier 4 inapplicable). | | `libobs/obs-hotkey-binding-getters.c.h` | `obs-hotkey::binding_getters` | Three public binding getters: exact stored combination, independent binding ID and pointer identity. No intentional differences. Null stored hotkeys are preserved; null binding inputs remain outside the C contract. Full real-header binding/combination layouts and differential parity, with actual-library C characterization. | | `libobs/obs-service-module-lookup.c.h` | `obs-output::service_module_lookup` | #3839: enabled/disabled list selection, next link, service count/id and load state. C supplies both global roots without selection; Rust reads the unchanged private module layout, checked field by field against `obs-internal.h`. No intended differences on defined C inputs; NULL modules and out-of-range id indexes are excluded as C UB. | | `libobs/util/bitstream.c` | `obs-util::bitstream` | C ABI keeps the `uint8_t pos` wrap past byte 255; the safe API does not wrap | diff --git a/rust/obs-build/tests/native.rs b/rust/obs-build/tests/native.rs index a6a8cab241..1f2bd74cfb 100644 --- a/rust/obs-build/tests/native.rs +++ b/rust/obs-build/tests/native.rs @@ -353,6 +353,8 @@ mod canonical_hotkey_background_query_is_registered; mod canonical_hotkey_background_trigger_swap_is_registered; #[path = "native/registration/canonical_hotkey_backtab_is_registered.rs"] mod canonical_hotkey_backtab_is_registered; +#[path = "native/registration/canonical_hotkey_combination_background_id_swap_is_registered.rs"] +mod canonical_hotkey_combination_background_id_swap_is_registered; #[path = "native/registration/canonical_hotkey_identity_getters_is_registered.rs"] mod canonical_hotkey_identity_getters_is_registered; #[path = "native/registration/canonical_hotkey_mnemonic_is_registered.rs"] diff --git a/rust/obs-build/tests/native/registration/canonical_hotkey_combination_background_id_swap_is_registered.rs b/rust/obs-build/tests/native/registration/canonical_hotkey_combination_background_id_swap_is_registered.rs new file mode 100644 index 0000000000..57057732d4 --- /dev/null +++ b/rust/obs-build/tests/native/registration/canonical_hotkey_combination_background_id_swap_is_registered.rs @@ -0,0 +1,42 @@ +use super::*; + +#[test] +fn canonical_hotkey_combination_background_id_swap_is_registered() { + use obs_build::validation::graph::{Graph, Source}; + let root = Path::new(env!("CARGO_MANIFEST_DIR")).join("../.."); + let mut graph = Graph::load(&root.join("test/cmocka/build/test-graph.json")).unwrap(); + let name = "test_hotkey_combination_background_id"; + graph.targets.retain(|key, _| key == name); + graph.tests.retain(|key, _| key == name); + for os in ["OS_LINUX", "OS_MACOS", "OS_WINDOWS"] { + let selected = graph.select(&[(os.into(), true)].into()).unwrap(); + assert!( + matches!(selected.targets[name].sources.as_slice(), [Source::Path(path)] + if path == "test/cmocka/test_hotkey_combination_background_id.c") + ); + assert_eq!(selected.tests[name].target, name); + } + let ported = fs::read_to_string( + root.join("libobs/build/ported-source-sets/hotkey-combination-background-id.json"), + ) + .unwrap(); + assert!(ported.contains("build/libobs.ported-hotkey-combination-background-id+.h.c")); + let group = fs::read_to_string( + root.join("libobs/build/libobs.ported-hotkey-combination-background-id+.h.c"), + ) + .unwrap(); + // The C reference gives the original its static lock pair through the seam. + let lock = "#include \"libobs/obs-hotkey-combination-background-id-lock.c.h\""; + let original = "#include \"libobs/obs-hotkey-combination-background-id.c.h\""; + assert!(group.find(lock).unwrap() < group.find(original).unwrap()); + let hybrid = fs::read_to_string(root.join("libobs/build/libobs.core+.h.c")).unwrap(); + assert!(!hybrid.contains(original)); + // The C-owned mutex, callback slot and dstr_free stay in the production group. + assert!(hybrid.contains("#include \"libobs/obs-hotkey.c.h\"")); + assert!(hybrid.contains("#include \"libobs/obs-hotkey-combination-background-id-fields.c.h\"")); + let exports = fs::read_to_string( + root.join("libobs/cmake/rust-exports.d/obs-hotkey-combination_background_id.txt"), + ) + .unwrap(); + assert_eq!(exports.trim(), "obs_hotkey_combination_get_background_id"); +} diff --git a/rust/obs-hotkey/src/combination_background_id.rs b/rust/obs-hotkey/src/combination_background_id.rs index 9c508c3ee4..dd8fac190a 100644 --- a/rust/obs-hotkey/src/combination_background_id.rs +++ b/rust/obs-hotkey/src/combination_background_id.rs @@ -26,6 +26,17 @@ pub trait CombinationBackgroundId { /// Capture the callback under the lock and call it with the lock released. /// A failed lock returns `None` with no further access. pub fn query(backend: &mut B) -> Option { - let _ = backend; - None + if !backend.lock() { + return None; + } + let mut id = backend.empty(); + let callback = backend.callback(); + backend.unlock(); + match callback { + Some(callback) if backend.invoke(callback, &mut id) => Some(backend.transfer(id)), + _ => { + backend.free(id); + None + } + } }