diff --git a/libobs/build/libobs.core-output+.h.c b/libobs/build/libobs.core-output+.h.c index a5fef627e4..e1741a75c2 100644 --- a/libobs/build/libobs.core-output+.h.c +++ b/libobs/build/libobs.core-output+.h.c @@ -82,7 +82,7 @@ #include "libobs/obs-output-video-encoder-getter-fields.c.h" #include "libobs/obs-output.c.h" #include "libobs/obs-service-context-getter-fields.c.h" -#include "libobs/obs-service-find.c.h" +#include "libobs/obs-service-find-fields.c.h" #include "libobs/obs-service-handler-getters-fields.c.h" #include "libobs/obs-service-info-accessors-fields.c.h" #include "libobs/obs-service-state-adapter.c.h" diff --git a/libobs/build/libobs.ported-service-find+.h.c b/libobs/build/libobs.ported-service-find+.h.c new file mode 100644 index 0000000000..16dd62958f --- /dev/null +++ b/libobs/build/libobs.ported-service-find+.h.c @@ -0,0 +1,4 @@ +/* Original C service registry lookup, retained for the C reference. */ +#include "libobs/build/libobs.pch.h" + +#include "libobs/obs-service-find.c.h" diff --git a/libobs/build/ported-source-sets/service-find.json b/libobs/build/ported-source-sets/service-find.json new file mode 100644 index 0000000000..0d95f17b6c --- /dev/null +++ b/libobs/build/ported-source-sets/service-find.json @@ -0,0 +1,5 @@ +{ + "LIBOBS_PORTED_SOURCES": [ + "build/libobs.ported-service-find+.h.c" + ] +} diff --git a/libobs/cmake/rust-hidden.d/obs-output-service_find.txt b/libobs/cmake/rust-hidden.d/obs-output-service_find.txt new file mode 100644 index 0000000000..4fbf39cd46 --- /dev/null +++ b/libobs/cmake/rust-hidden.d/obs-output-service_find.txt @@ -0,0 +1 @@ +find_service diff --git a/libobs/obs-service-find-fields.c.h b/libobs/obs-service-find-fields.c.h new file mode 100644 index 0000000000..955dd44a3a --- /dev/null +++ b/libobs/obs-service-find-fields.c.h @@ -0,0 +1,19 @@ +#include "libobs/build/libobs.pch.h" + +size_t obs_service_find_count(void); +size_t obs_service_find_count(void) +{ + return obs->service_types.num; +} + +const struct obs_service_info *obs_service_find_entry(size_t index); +const struct obs_service_info *obs_service_find_entry(size_t index) +{ + return obs->service_types.array + index; +} + +const char *obs_service_find_id(const struct obs_service_info *entry); +const char *obs_service_find_id(const struct obs_service_info *entry) +{ + return entry->id; +} diff --git a/rust/README.md b/rust/README.md index 201a2a2fe5..c95c111104 100644 --- a/rust/README.md +++ b/rust/README.md @@ -359,6 +359,7 @@ The private output starting/encoder-demand queries use lazy safe Rust decisions | `libobs/obs-output-update.c.h` | `obs-output::output_update` | Swapped in hybrid `libobs` (group `libobs.ported-output-update`). `obs_output_update()` merges the incoming settings into the stored ones through `obs_data_apply()`, then calls the type's `update` with the plugin data and the stored settings when it has one; a NULL output logs and does nothing else. The live adapter `obs-output-update-fields.c.h` gives the stored settings, whether the type has an update, and the call. No lock, as in C, and nothing of the output is held across the plugin's callback. Tier 1 mirrors of the unchanged Tier 2 characterization and original-C differential coverage of the merged settings and the callback. No intentional differences; Tier 4 does not apply. | | `libobs/obs-service-weak-acquire.c.h` | `obs-output::service_weak_acquire` | Swapped in hybrid libobs (#3837): the hidden `obs_weak_service_weak_get_ref` and `obs_weak_service_target` read the unchanged C weak-control layout, using the platform C-long-width SeqCst atomic count. Tier 2 keeps the existing service lifecycle tests; Tier 3 checks both private layouts against the real C header and compares return values, target and both counts. No intended behavior differences for defined C inputs. Acquisition at `LONG_MAX` (signed overflow in C) and NULL controls (invalid in C) are outside the parity domain. Release/free are ported in #3838, module lookup is #3839, and context/state/handlers are #3840. | | `libobs/obs-service-context-getters.c.h` | `obs-output::service_context_getters` | Hidden borrowed name/settings getters (#4483), using real private C field addresses. No allocation, reference acquisition or mutation. Original C retained in its reference-only group; Tier 3 checks nullable field values, exact pointer identity, field addresses and unchanged service bytes. NULL services remain outside the original caller contract. | +| `libobs/obs-service-find.c.h` | `obs-output::service_find` | Hidden first byte-exact match in the C-owned service registry (#5273). Real private projections retain native layouts and borrowed pointer identity; lookup is lazy safe Rust, with unchanged external C characterization and original-C differential coverage. | | `libobs/obs-service-getters.c.h` | `obs-output::service` | Swapped in hybrid `libobs` (group `libobs.ported-service-getters`), #1899. The nine exported service getters (`obs_service_get_protocol`, `_get_connect_info`, `_can_try_to_connect`, `_get_preferred_output_type`, `_get_max_fps`, `_get_max_bitrate`, `_get_supported_resolutions` and the two codec lists), moved out of `obs-service.c.h`. `struct obs_service` stays C: the shims reach the plugin's `obs_service_info` table and data through the hidden `obs_service_get_info_table()` and `obs_service_get_plugin_data()` kept beside it, and mirror `struct obs_service_info` (the public plugin ABI) with its layout checked against the C. #1913 moved the settings, properties and identity functions the same way: `obs_service_get_display_name`, `obs_service_defaults` and `obs_get_service_properties` (by id, through `find_service()`), and `obs_service_get_name`, `_get_type`, `_get_id`, `_get_type_data`, `_update`, `_get_settings`, `_properties` and `_apply_encoder_settings`; the context name and settings come through the hidden `obs_service_get_context_name()` and `_get_context_settings()`, and the `obs_data_t` / `obs_properties_t` calls go to libobs. `obs_service_get_signal_handler` and `_get_proc_handler` are now ported separately (see below); neither has a public header declaration. #1920 moved the rest the same way: `obs_service_create`, `_create_private`, `_destroy` (deferred while active), `_release`, the strong and weak reference functions, `obs_service_get_module` and `_load_state`, and the output-side `obs_service_activate`, `_deactivate` and `_initialize` (hidden). `struct obs_service`, its context (`obs_context_data_*`) and its reference counts (`obs_ref_*`, `obs_weak_ref_*`) stay C behind hidden `obs_service_*()` / `obs_weak_service_*()` steps in `obs-service.c.h`; the port keeps their order, the release order (destroy, then the control's weak ref) and the log lines. The C's check-and-zero order is kept, including what `test_service_getters` marks "characterized, not endorsed": `get_max_bitrate` zeroes its out-parameters before checking the service, and the codec getters do not check for NULL. Tier 2 runs `test_service_getters`, `test_service_settings` and `test_service_lifecycle` through the hybrid libobs; for #1913 Tier 3 runs every function on both sides over the 32 tables of its four optional callbacks and type data, comparing returns, settings contents and the plugin call trace; for #1920 Tier 3 runs create, refs, activation and the deferred destroy on both sides over every table of its three optional callbacks, a failing `create()` and both `initialize()` results, comparing results and the trace of every step and plugin call, plus the module lookups over the same module lists; Tier 3 runs every getter on the Rust and the C with the same service over all 256 optional-callback tables, NULL and sentinel out-parameters, comparing returns, out-parameters and the plugin call trace. | | `libobs/obs-service-handler-getters.c.h` | `obs-output::service_handler_getters` | Hidden borrowed signal/procedure handlers swapped in hybrid libobs (#5002; parent #3840). Preserves exact NULL diagnostics, independent nullable handlers, pointer identity and unchanged service bytes. Guarded real field adapter, real-header layout, Tier 1, C-oracle differential/mutation and unchanged C characterization via declaration-only Rust-static mode. No intentional differences; Tier 4 does not apply. | | `libobs/obs-service-info-accessors.c.h` | `obs-output::service_info_accessors` | Swapped in hybrid `libobs` (#3840; group `libobs.ported-service-info-accessors` keeps the original for the C reference). The hidden `obs_service_set_info`, `obs_service_get_info_table`, `obs_service_set_plugin_data` and `obs_service_get_plugin_data` (`obs-internal.h`, listed in `rust-hidden.d`), which the service lifecycle and getters call. Rust copies the whole table by value into the service (reading the source first, so a table copied onto itself is unchanged), returns the borrowed address of the service's own copy, and stores and returns the plugin data as it is, NULL included; nothing else in the service changes and nothing is logged. A private field adapter (`obs-service-info-accessors-fields.c.h`) keeps `struct obs_service` in C; the Rust-static support member now supplies that adapter instead of the original, and the service oracle's own copies of the four names are gone. The unchanged `test_service_info_accessors` keeps testing the C source; its twin `test_service_info_accessors_rust` runs the same assertions on the Rust shims. Tier 1 mirrors every assertion; Tier 3 runs the original and the shims on byte-pattern services (characterized and random set/get sequences, self-copy included), comparing results, whole services and source tables, with real-header layout checks and mutants. No intentional differences; Tier 4 does not apply. | diff --git a/rust/obs-build/tests/native/registration/canonical_rust_static_private_steps_are_registered.rs b/rust/obs-build/tests/native/registration/canonical_rust_static_private_steps_are_registered.rs index 40c835ed82..9afedb51fb 100644 --- a/rust/obs-build/tests/native/registration/canonical_rust_static_private_steps_are_registered.rs +++ b/rust/obs-build/tests/native/registration/canonical_rust_static_private_steps_are_registered.rs @@ -8,7 +8,7 @@ static CASES: std::sync::LazyLock> = #[test] fn canonical_rust_static_private_steps_are_registered() { let graph = rust_static_entries::assert_family(FAMILY); - assert_eq!(CASES.len(), 8); + assert_eq!(CASES.len(), 9); for windows in [false, true] { for msvc in [false, true] { for available in [false, true] { @@ -19,8 +19,8 @@ fn canonical_rust_static_private_steps_are_registered() { ] .into(); let selected = graph.select(&facts).unwrap(); - assert_eq!(selected.targets.len(), if available { 8 } else { 0 }); - assert_eq!(selected.tests.len(), if available { 8 } else { 0 }); + assert_eq!(selected.targets.len(), if available { 9 } else { 0 }); + assert_eq!(selected.tests.len(), if available { 9 } else { 0 }); } } } diff --git a/rust/obs-build/tests/native/registration/canonical_video_info_query_is_registered.rs b/rust/obs-build/tests/native/registration/canonical_video_info_query_is_registered.rs index d2a45581f0..58e49c88a7 100644 --- a/rust/obs-build/tests/native/registration/canonical_video_info_query_is_registered.rs +++ b/rust/obs-build/tests/native/registration/canonical_video_info_query_is_registered.rs @@ -76,8 +76,8 @@ target_link_libraries(reference PRIVATE OBS::libobs) include("{root}/test/cmocka/tests.d/video_info_query_link_support.cmake") function(check_support) get_target_property(adapter_sources video_info_query_link_support SOURCES) - foreach(adapter IN ITEMS encoder-video-tex-active-fields encoder-video-tex-active-mix encoder-get-name - output-interleave-name source-audio-capture-callback) + foreach(adapter IN ITEMS encoder-get-name encoder-video-tex-active-fields encoder-video-tex-active-mix + output-interleave-name service-find source-audio-capture-callback) if(NOT "{root}/test/cmocka/${{adapter}}-link-support.c" IN_LIST adapter_sources) message(FATAL_ERROR "missing real private adapter source: ${{adapter}}") endif() diff --git a/rust/obs-build/tests/native/registration/rust-static/cases/private-steps/test_service_find.json b/rust/obs-build/tests/native/registration/rust-static/cases/private-steps/test_service_find.json new file mode 100644 index 0000000000..6c42ce50de --- /dev/null +++ b/rust/obs-build/tests/native/registration/rust-static/cases/private-steps/test_service_find.json @@ -0,0 +1,35 @@ +{ + "schema": 1, + "targets": { + "test_service_find_rust": { + "sources": [ + "test/cmocka/test_service_find.c", + "test/cmocka/service-find-registry-adapter.c" + ], + "libraries": [ + "libobs_rust-static", + "OBS::libobs", + "OBS::caption", + "Uthash::Uthash", + "OBS::native-test-cmocka", + { + "name": "OBS::w32-pthreads", + "requires": "OS_WINDOWS" + } + ], + "definitions": [ + "SERVICE_FIND_EXTERNAL" + ], + "requires": "HAVE_LIBOBS_RUST_STATIC" + } + }, + "tests": { + "test_service_find_rust": { + "target": "test_service_find_rust", + "command": [ + "${target}" + ], + "requires": "HAVE_LIBOBS_RUST_STATIC" + } + } +} diff --git a/rust/obs-c-oracle/oracle.d/service-find.list b/rust/obs-c-oracle/oracle.d/service-find.list new file mode 100644 index 0000000000..a25d29ab41 --- /dev/null +++ b/rust/obs-c-oracle/oracle.d/service-find.list @@ -0,0 +1 @@ +service-find.inc|libobs/obs-service-find.c.h|auto diff --git a/rust/obs-c-oracle/oracle/service_find.c b/rust/obs-c-oracle/oracle/service_find.c new file mode 100644 index 0000000000..fe482fd2ab --- /dev/null +++ b/rust/obs-c-oracle/oracle/service_find.c @@ -0,0 +1,94 @@ +/* Original lookup and real field projections, with a controlled C-owned core. */ +#include "obs-internal.h" +#include + +static struct obs_core service_find_oracle_storage; +static struct obs_core *service_find_oracle_core = &service_find_oracle_storage; +#define obs service_find_oracle_core +#define find_service oracle_find_service +const struct obs_service_info *oracle_find_service(const char *id); +#include "service-find.inc" +#undef find_service +#include "service_find_fields.inc" +#undef obs + +void oracle_service_set_types(const struct obs_service_info *types, size_t num); +void oracle_service_set_types(const struct obs_service_info *types, size_t num) +{ + /* The lookup only borrows this table; it never writes through the C field. */ + memcpy(&service_find_oracle_storage.service_types.array, &types, sizeof(types)); + service_find_oracle_storage.service_types.num = num; + service_find_oracle_storage.service_types.capacity = num; +} + +static unsigned service_find_calls; +static const char *service_find_name(void *data) +{ + (void)data; + service_find_calls++; + return "unused"; +} +static void *service_find_create(obs_data_t *settings, obs_service_t *service) +{ + (void)settings; + (void)service; + service_find_calls++; + return NULL; +} +static void service_find_destroy(void *data) +{ + (void)data; + service_find_calls++; +} + +typedef const struct obs_service_info *(*service_find_fn)(const char *); +bool oracle_service_find_check(const char *const *ids, size_t num, size_t capacity, const char *query, + service_find_fn rust_find); +bool oracle_service_find_check(const char *const *ids, size_t num, size_t capacity, const char *query, + service_find_fn rust_find) +{ + if (num > capacity || capacity > 8) + return false; + struct obs_service_info entries[8] = {0}; + struct obs_service_info before[8]; + int owners[8] = {0}; + unsigned char saved[sizeof(service_find_oracle_storage)]; + memcpy(saved, &service_find_oracle_storage, sizeof(saved)); + for (size_t i = 0; i < capacity; i++) { + entries[i].id = ids[i]; + entries[i].get_name = service_find_name; + entries[i].create = service_find_create; + entries[i].destroy = service_find_destroy; + entries[i].free_type_data = service_find_destroy; + entries[i].type_data = &owners[i]; + } + oracle_service_set_types(entries, num); + service_find_oracle_storage.service_types.capacity = capacity; + bool ok = true; + /* Repeat after replacing and moving entries: no stale cached result. */ + for (unsigned step = 0; step < 3; step++) { + memcpy(before, entries, sizeof(entries)); + unsigned char core_before[sizeof(service_find_oracle_storage)]; + memcpy(core_before, &service_find_oracle_storage, sizeof(core_before)); + service_find_calls = 0; + const struct obs_service_info *expected = oracle_find_service(query); + const struct obs_service_info *actual = rust_find(query); + ok = ok && actual == expected && service_find_calls == 0 && + memcmp(entries, before, sizeof(entries)) == 0 && + memcmp(core_before, &service_find_oracle_storage, sizeof(core_before)) == 0 && + memcmp(owners, (int[8]){0}, sizeof(owners)) == 0; + if (actual && expected) + ok = ok && actual->id == expected->id && actual->type_data == expected->type_data; + if (num) { + if (step == 0) { + struct obs_service_info swap = entries[0]; + entries[0] = entries[num - 1]; + entries[num - 1] = swap; + } else { + entries[0].id = "replaced"; + } + } + } + memcpy(&service_find_oracle_storage, saved, sizeof(saved)); + return ok; +} diff --git a/rust/obs-c-oracle/oracle/service_getters.c b/rust/obs-c-oracle/oracle/service_getters.c index 114683a59a..716349cd86 100644 --- a/rust/obs-c-oracle/oracle/service_getters.c +++ b/rust/obs-c-oracle/oracle/service_getters.c @@ -1,11 +1,11 @@ /* Test-only oracle: the original libobs/obs-service-getters.c.h, unmodified, * with its exports renamed to oracle_* so it can link next to the Rust - * shims (#1899, #1913, #1920). find_service() and the context accessors are - * test doubles that both sides call. The info-table and plugin-data - * accessors are the Rust port's (#3840), through the real field adapter in - * oracle/service_info_accessors.c, and oracle_service_new() builds the - * struct obs_service both sides read, so the parity tests drive the C and - * the Rust with the same service. */ + * shims (#1899, #1913, #1920). The context accessors are + * test doubles that both sides call; registry lookup uses its real seam. + * The info-table and plugin-data accessors are the Rust port's (#3840), + * through the real field adapter in oracle/service_info_accessors.c. + * oracle_service_new() builds the struct obs_service both sides read, + * so the parity tests drive the C and the Rust with the same service. */ #ifndef _WIN32 #define _GNU_SOURCE #endif @@ -231,25 +231,7 @@ void *oracle_service_control(const obs_service_t *service) return service->context.control; } -/* obs-service.c's find_service() over obs->service_types, here over one - * table the test sets (NULL: no types). */ -static const struct obs_service_info *oracle_types; -static size_t oracle_types_num; - -void oracle_service_set_types(const struct obs_service_info *types, size_t num); -void oracle_service_set_types(const struct obs_service_info *types, size_t num) -{ - oracle_types = types; - oracle_types_num = num; -} - -const struct obs_service_info *find_service(const char *id) -{ - for (size_t i = 0; i < oracle_types_num; i++) - if (strcmp(oracle_types[i].id, id) == 0) - return oracle_types + i; - return NULL; -} +/* Registry setup and lookup now use the real service-find C seam. */ /* The context fields #1913 reads; the plugin data and table are set by * oracle_service_new(). */ diff --git a/rust/obs-output/src/ffi/mod.rs b/rust/obs-output/src/ffi/mod.rs index 64546abaef..d8d6d9d822 100644 --- a/rust/obs-output/src/ffi/mod.rs +++ b/rust/obs-output/src/ffi/mod.rs @@ -82,6 +82,7 @@ mod output_type_lookup; pub mod output_video_checks; pub mod service; pub mod service_context_getters; +pub mod service_find; pub mod service_module_lookup; pub mod service_state; pub mod service_weak_acquire; diff --git a/rust/obs-output/src/ffi/service_find.rs b/rust/obs-output/src/ffi/service_find.rs new file mode 100644 index 0000000000..e58ca18f09 --- /dev/null +++ b/rust/obs-output/src/ffi/service_find.rs @@ -0,0 +1,30 @@ +//! Private ABI for the service registry lookup. C retains layouts and storage. +use core::ffi::{CStr, c_char, c_void}; + +unsafe extern "C" { + fn obs_service_find_count() -> usize; + fn obs_service_find_entry(index: usize) -> *const c_void; + fn obs_service_find_id(entry: *const c_void) -> *const c_char; +} + +/// Return the first byte-exact match as a borrowed pointer into the live registry. +/// +/// # Safety +/// The core must be initialized. `id` and registered IDs must be valid C strings; +/// the C-owned registry must remain live and unmodified throughout the call. +#[unsafe(no_mangle)] +pub unsafe extern "C" fn find_service(id: *const c_char) -> *const c_void { + // SAFETY: the caller preserves the original C lookup preconditions. Each + // index is below the logical count, and C projects its own native layout. + unsafe { + let entries = (0..obs_service_find_count()).filter_map(|index| { + let entry = obs_service_find_entry(index); + if entry.is_null() { + return None; + } + Some((CStr::from_ptr(obs_service_find_id(entry)).to_bytes(), entry)) + }); + crate::service_find::find(CStr::from_ptr(id).to_bytes(), entries) + .unwrap_or(core::ptr::null()) + } +} diff --git a/rust/obs-output/src/lib.rs b/rust/obs-output/src/lib.rs index 097c962dc9..8ccb734a03 100644 --- a/rust/obs-output/src/lib.rs +++ b/rust/obs-output/src/lib.rs @@ -223,6 +223,7 @@ pub mod raw_audio; pub mod raw_audio_trim; pub mod service; pub mod service_context_getters; +pub mod service_find; pub mod service_module_lookup; pub mod service_state; pub mod service_weak_acquire; diff --git a/rust/obs-output/src/service_find.rs b/rust/obs-output/src/service_find.rs new file mode 100644 index 0000000000..fae027a341 --- /dev/null +++ b/rust/obs-output/src/service_find.rs @@ -0,0 +1,8 @@ +//! Borrowed, byte-exact lookup in the C-owned service registry. + +/// Walk IDs lazily and return the first matching entry without copying it. +pub fn find<'a, T>(id: &[u8], entries: impl IntoIterator) -> Option { + entries + .into_iter() + .find_map(|(entry_id, entry)| (entry_id == id).then_some(entry)) +} diff --git a/rust/obs-output/tests/output/main.rs b/rust/obs-output/tests/output/main.rs index 6050bbafd3..7c1bd95a0b 100644 --- a/rust/obs-output/tests/output/main.rs +++ b/rust/obs-output/tests/output/main.rs @@ -157,6 +157,8 @@ mod raw_audio_trim_parity; mod service; mod service_context_getters; mod service_context_getters_parity; +mod service_find; +mod service_find_parity; mod service_lifecycle; mod service_lifecycle_parity; mod service_module_lookup; diff --git a/rust/obs-output/tests/output/service_find.rs b/rust/obs-output/tests/output/service_find.rs new file mode 100644 index 0000000000..e57d467d5e --- /dev/null +++ b/rust/obs-output/tests/output/service_find.rs @@ -0,0 +1,93 @@ +//! Tier 1 mirrors test_service_find.c::registry_lookup without FFI. +use obs_output::service_find::find; + +#[derive(Clone, Debug, PartialEq)] +struct Entry { + id: Vec, + owner: usize, +} + +fn lookup<'a>(entries: &'a [Entry], count: usize, query: &[u8]) -> Option<&'a Entry> { + find( + query, + entries[..count] + .iter() + .map(|entry| (entry.id.as_slice(), entry)), + ) +} + +#[test] +fn registry_lookup() { + for query in [b"".as_slice(), b"missing"] { + assert!(lookup(&[], 0, query).is_none()); + } + let ids: &[&[u8]] = &[ + b"svc", + b"svc.suffix", + b"SVC", + b"", + b"\xff\x80x", + b"svc", + b"last", + b"outside", + ]; + let mut entries: Vec<_> = ids + .iter() + .enumerate() + .map(|(owner, id)| Entry { + id: id.to_vec(), + owner, + }) + .collect(); + assert!(lookup(&entries, 0, b"svc").is_none()); + let queries: &[(&[u8], Option)] = &[ + (b"svc", Some(0)), + (b"sv", None), + (b"svc.suffix", Some(1)), + (b"svc.suffix.more", None), + (b"SVC", Some(2)), + (b"Svc", None), + (b"", Some(3)), + (b"\xff\x80x", Some(4)), + (b"\xff\x81x", None), + (b"last", Some(6)), + (b"outside", None), + (b"missing", None), + ]; + let before = entries.clone(); + for &(query, expected) in queries { + let actual = lookup(&entries, 7, query); + assert_eq!(actual.map(|entry| entry.owner), expected); + if let Some(index) = expected { + assert!(std::ptr::eq(actual.unwrap(), &entries[index])); + } + assert_eq!(entries, before); + } + entries.swap(0, 5); + assert!(std::ptr::eq( + lookup(&entries, 7, b"svc").unwrap(), + &entries[0] + )); + entries[0].id = b"replaced".to_vec(); + assert!(std::ptr::eq( + lookup(&entries, 7, b"svc").unwrap(), + &entries[5] + )); + assert_eq!(entries[5].id, b"svc"); + assert_eq!(entries[0].owner, 5); +} + +#[test] +fn stops_before_reading_later_entries() { + let mut reads = 0; + let entries = [b"svc".as_slice(), b"svc", b"other"]; + let found = find( + b"svc", + entries.iter().enumerate().map(|(index, id)| { + reads += 1; + (*id, index) + }), + ); + assert_eq!(found, Some(0)); + assert_eq!(reads, 1); +} diff --git a/rust/obs-output/tests/output/service_find_parity.rs b/rust/obs-output/tests/output/service_find_parity.rs new file mode 100644 index 0000000000..c4615a00ec --- /dev/null +++ b/rust/obs-output/tests/output/service_find_parity.rs @@ -0,0 +1,88 @@ +//! Tier 3: original whole C lookup versus Rust through real private projections. +//! Original initialized-core / valid-C-string domain; no intentional differences. +use core::ffi::{c_char, c_void}; +use obs_output::ffi::service_find::find_service; +use proptest::prelude::*; + +unsafe extern "C" { + fn oracle_service_find_check( + ids: *const *const c_char, + count: usize, + capacity: usize, + query: *const c_char, + rust_find: unsafe extern "C" fn(*const c_char) -> *const c_void, + ) -> bool; +} + +fn check(ids: &[Vec], count: usize, query: &[u8]) { + let ids: Vec<_> = ids + .iter() + .map(|id| std::ffi::CString::new(id.as_slice()).unwrap()) + .collect(); + let pointers: Vec<_> = ids.iter().map(|id| id.as_ptr()).collect(); + let query = std::ffi::CString::new(query).unwrap(); + // SAFETY: bounded arrays and valid C strings live through the call. The C + // fixture owns the real registry layouts; SERVICE_GLOBALS excludes other users. + assert!(unsafe { + oracle_service_find_check( + pointers.as_ptr(), + count, + ids.len(), + query.as_ptr(), + find_service, + ) + }); +} + +#[test] +fn characterized_registry_edges() { + let _lock = super::SERVICE_GLOBALS.lock().unwrap(); + let ids: Vec<_> = [ + b"svc".as_slice(), + b"svc.suffix", + b"SVC", + b"", + b"\xff\x80x", + b"svc", + b"last", + b"outside", + ] + .iter() + .map(|id| id.to_vec()) + .collect(); + for query in [b"".as_slice(), b"missing"] { + check(&[], 0, query); + check(&ids, 0, query); + } + for query in [ + b"svc".as_slice(), + b"sv", + b"svc.suffix", + b"svc.suffix.more", + b"SVC", + b"Svc", + b"", + b"\xff\x80x", + b"\xff\x81x", + b"last", + b"outside", + b"missing", + ] { + check(&ids, 7, query); + } +} + +proptest! { + #![proptest_config(ProptestConfig::with_cases(obs_c_oracle::parity::cases(128)))] + #[test] + fn arbitrary_byte_ids_match_original( + ids in prop::collection::vec(prop::collection::vec(1u8..=255, 0..24), 0..=8), + query in prop::collection::vec(1u8..=255, 0..24), + count_seed in any::(), + ) { + let _lock = super::SERVICE_GLOBALS.lock().unwrap(); + let count = count_seed % (ids.len() + 1); + check(&ids, count, &query); + for id in &ids { check(&ids, count, id); } + } +} diff --git a/test/cmocka/build/test-graph.json b/test/cmocka/build/test-graph.json index e33f14fc50..5d51088832 100644 --- a/test/cmocka/build/test-graph.json +++ b/test/cmocka/build/test-graph.json @@ -16358,6 +16358,27 @@ } ] }, + "test_service_find_rust": { + "sources": [ + "test/cmocka/test_service_find.c", + "test/cmocka/service-find-registry-adapter.c" + ], + "libraries": [ + "libobs_rust-static", + "OBS::libobs", + "OBS::caption", + "Uthash::Uthash", + "OBS::native-test-cmocka", + { + "name": "OBS::w32-pthreads", + "requires": "OS_WINDOWS" + } + ], + "definitions": [ + "SERVICE_FIND_EXTERNAL" + ], + "requires": "HAVE_LIBOBS_RUST_STATIC" + }, "test_service_getters": { "sources": [ "test/cmocka/test_service_getters.c" @@ -29096,6 +29117,13 @@ "${target}" ] }, + "test_service_find_rust": { + "target": "test_service_find_rust", + "command": [ + "${target}" + ], + "requires": "HAVE_LIBOBS_RUST_STATIC" + }, "test_service_getters": { "target": "test_service_getters", "command": [ diff --git a/test/cmocka/service-find-link-support.c b/test/cmocka/service-find-link-support.c index 856030db5d..340096d3f8 100644 --- a/test/cmocka/service-find-link-support.c +++ b/test/cmocka/service-find-link-support.c @@ -1,2 +1,2 @@ -/* The production service registry lookup called by the Rust service implementation. */ -#include "libobs/obs-service-find.c.h" +#include "libobs/build/libobs.pch.h" +#include "libobs/obs-service-find-fields.c.h" diff --git a/test/cmocka/service-find-registry-adapter.c b/test/cmocka/service-find-registry-adapter.c new file mode 100644 index 0000000000..35150fab3d --- /dev/null +++ b/test/cmocka/service-find-registry-adapter.c @@ -0,0 +1,6 @@ +/* Fixture-owned registry, using the actual production field projections. */ +#include "obs-internal.h" +extern struct obs_core *service_find_test_core; +#define obs service_find_test_core +#include "libobs/obs-service-find-fields.c.h" +#undef obs diff --git a/test/cmocka/tests.d/test_service_find_rust.cmake b/test/cmocka/tests.d/test_service_find_rust.cmake new file mode 100644 index 0000000000..d85a840326 --- /dev/null +++ b/test/cmocka/tests.d/test_service_find_rust.cmake @@ -0,0 +1 @@ +# Registered in the canonical native test graph (test/cmocka/build/test-graph.json).