diff --git a/libobs/build/libobs.core+.h.c b/libobs/build/libobs.core+.h.c index f6461dd683..62027bbd9d 100644 --- a/libobs/build/libobs.core+.h.c +++ b/libobs/build/libobs.core+.h.c @@ -47,7 +47,7 @@ #include "libobs/obs-module-post-load-fields.c.h" #include "libobs/obs-module-type-query-fields.c.h" #include "libobs/obs-module.c.h" -#include "libobs/obs-object-id.c.h" +#include "libobs/obs-object-id-fields.c.h" #include "libobs/obs-object-ref-fields.c.h" #include "libobs/obs-set-locale.c.h" #include "libobs/obs-set-output-source-fields.c.h" diff --git a/libobs/build/libobs.ported-object-id+.h.c b/libobs/build/libobs.ported-object-id+.h.c new file mode 100644 index 0000000000..7e8d67d5e2 --- /dev/null +++ b/libobs/build/libobs.ported-object-id+.h.c @@ -0,0 +1,4 @@ +/* C reference only: the hybrid uses obs-core::object_id. */ +#include "libobs/build/libobs.pch.h" + +#include "libobs/obs-object-id.c.h" diff --git a/libobs/build/ported-source-sets/object-id.json b/libobs/build/ported-source-sets/object-id.json new file mode 100644 index 0000000000..cb37ea9030 --- /dev/null +++ b/libobs/build/ported-source-sets/object-id.json @@ -0,0 +1,5 @@ +{ + "LIBOBS_PORTED_SOURCES": [ + "build/libobs.ported-object-id+.h.c" + ] +} diff --git a/libobs/cmake/rust-exports.d/obs-core-object_id.txt b/libobs/cmake/rust-exports.d/obs-core-object_id.txt new file mode 100644 index 0000000000..3e43b9aaa1 --- /dev/null +++ b/libobs/cmake/rust-exports.d/obs-core-object_id.txt @@ -0,0 +1 @@ +obs_obj_get_id diff --git a/libobs/obs-object-id-fields.c.h b/libobs/obs-object-id-fields.c.h new file mode 100644 index 0000000000..02c7f08298 --- /dev/null +++ b/libobs/obs-object-id-fields.c.h @@ -0,0 +1,33 @@ +#include "libobs/build/libobs.pch.h" + +/* Private field borrowing only; NULL and kind selection belong to Rust. */ +int obs_object_id_kind(const void *obj); +const char *const *obs_object_id_source_id(const void *obj); +const char *const *obs_object_id_output_id(const void *obj); +const char *const *obs_object_id_encoder_id(const void *obj); +const char *const *obs_object_id_service_id(const void *obj); + +int obs_object_id_kind(const void *obj) +{ + return (int)((const struct obs_context_data *)obj)->type; +} + +const char *const *obs_object_id_source_id(const void *obj) +{ + return &((const obs_source_t *)obj)->info.id; +} + +const char *const *obs_object_id_output_id(const void *obj) +{ + return &((const obs_output_t *)obj)->info.id; +} + +const char *const *obs_object_id_encoder_id(const void *obj) +{ + return &((const obs_encoder_t *)obj)->info.id; +} + +const char *const *obs_object_id_service_id(const void *obj) +{ + return &((const obs_service_t *)obj)->info.id; +} diff --git a/rust/README.md b/rust/README.md index 05d931d216..12ee078b46 100644 --- a/rust/README.md +++ b/rust/README.md @@ -577,6 +577,7 @@ and compares their exported symbols. `OBS_LIBOBS_BACKEND=C` is temporary rollbac | `libobs/obs-canvas-signals.c.h` | `obs-core::canvas_signals` | Private canvas event helpers swapped in hybrid libobs. Safe Rust constructs fixed 128-byte calldata and controls independent optional global/local routing, privacy gating, global-before-local order and canvas-only source dispatch. The thin bridge resolves each handler at its original dispatch point and shares mutable calldata across callbacks. Original C bodies remain in a reference-only unity group and the differential oracle. Tier 1 mirrors the five unchanged `test_canvas_signals` characterization cases; Tier 3 covers all route combinations, null/arbitrary source tokens, fixed capacity, pointer identity, callback payload mutation and local-handler replacement. No intentional differences; Tier 4 does not apply. | | `libobs/obs-canvas-video-getter.c.h` | `obs-core::canvas_video_getter` | Swapped in hybrid libobs (#3945): returns the borrowed mix video pointer or NULL without locks, references or mutation. C owns both layouts through private field addresses. Tier 1 mirrors unchanged actual-library characterization; Tier 3 checks real-header field-view layout and exact pointer/state parity. No exclusions or intentional differences. | | `libobs/obs-canvas-video-info-getter.c.h` | `obs-core::canvas_video_info_getter` | Swapped in hybrid libobs (#3950): preserves graphics short-circuit, absent mix, borrowed module pointer and output bytes. Tier 1 characterization, real-header layout, exact Tier 3 parity and unchanged actual-library Tier 2. No intentional differences. | +| `libobs/obs-object-id.c.h` | `obs-core::object_id` | NULL-safe borrowed ID query dispatches before projecting a supported concrete layout. Real private field adapters reuse the shared demand-linked support archive; unchanged C characterization, safe mirrors and original-C differential/mutation coverage preserve pointer identity and immutability. No intentional differences; Tier 4 inapplicable. | | `libobs/obs-object-queries.c.h` | `obs-core::object_queries` | Swapped in hybrid libobs (#3955): NULL-safe object type and privacy queries preserve stored bits without mutation. Private field-address adapter; unchanged actual-library Tier 2 characterization, real-header layout and exact Tier 3 parity. No exclusions or intentional differences. | | `libobs/obs-object-ref.c.h` | `obs-core::object_ref` | Common object strong acquisition/release, reusing the canvas atomic reference core. Preserves shutdown-before-NULL warning order, stored identity, expiry, callback-before-weak-release and final control deallocation for every type. Tier 1 mirrors unchanged C characterization; real-header layout, original-C differential and Tier 2 replay cover the swap. No intentional differences. | | `libobs/obs-object-validity-query.c.h` | `obs-core::object_validity_query` | Swapped in hybrid `libobs`. Public `obs_obj_invalid` preserves NULL and empty-data validity defaults through a private, NULL-guarded data-slot adapter. Tier 1 mirrors the C characterization; Tier 2 runs unchanged `test_object_validity_query` against both libraries; Tier 3 checks real-header scalar layouts, randomized context states and read-only parity. No public struct is mirrored; Tier 4 does not apply. | diff --git a/rust/obs-build/tests/native.rs b/rust/obs-build/tests/native.rs index 4c28b55f1f..68fdfad81e 100644 --- a/rust/obs-build/tests/native.rs +++ b/rust/obs-build/tests/native.rs @@ -5515,3 +5515,6 @@ mod canonical_source_update_properties_swap_is_registered; #[path = "native/registration/canonical_type_catalog_enum_swap_is_registered.rs"] mod canonical_type_catalog_enum_swap_is_registered; + +#[path = "native/registration/canonical_object_id_swap_is_registered.rs"] +mod canonical_object_id_swap_is_registered; diff --git a/rust/obs-build/tests/native/registration/canonical_object_id_swap_is_registered.rs b/rust/obs-build/tests/native/registration/canonical_object_id_swap_is_registered.rs new file mode 100644 index 0000000000..cf3479a43b --- /dev/null +++ b/rust/obs-build/tests/native/registration/canonical_object_id_swap_is_registered.rs @@ -0,0 +1,33 @@ +use super::*; + +#[test] +fn canonical_object_id_swap_is_registered() { + let root = Path::new(env!("CARGO_MANIFEST_DIR")).join("../.."); + let graph = + obs_build::validation::graph::Graph::load(&root.join("test/cmocka/build/test-graph.json")) + .unwrap(); + let characterization = &graph.targets["test_object_id"]; + assert!( + matches!(&characterization.sources[0], obs_build::validation::graph::Source::Path(p) + if p == "test/cmocka/test_object_id.c") + ); + let core = fs::read_to_string(root.join("libobs/build/libobs.core+.h.c")).unwrap(); + assert!(core.contains("\"libobs/obs-object-id-fields.c.h\"")); + assert!(!core.contains("\"libobs/obs-object-id.c.h\"")); + let unit: serde_json::Value = serde_json::from_str( + &fs::read_to_string(root.join("libobs/build/ported-source-sets/object-id.json")).unwrap(), + ) + .unwrap(); + assert_eq!( + unit["LIBOBS_PORTED_SOURCES"], + serde_json::json!(["build/libobs.ported-object-id+.h.c"]) + ); + let reference = + fs::read_to_string(root.join("libobs/build/libobs.ported-object-id+.h.c")).unwrap(); + assert!(reference.contains("\"libobs/obs-object-id.c.h\"")); + // Reuse the shared demand-linked adapter archive and its existing native + // registrations. New swaps must not invent another fixed consumer list. + let support = + fs::read_to_string(root.join("test/cmocka/video-info-query-link-support.c")).unwrap(); + assert!(support.contains("\"libobs/obs-object-id-fields.c.h\"")); +} diff --git a/rust/obs-c-oracle/oracle/object_id.c b/rust/obs-c-oracle/oracle/object_id.c new file mode 100644 index 0000000000..f12414ca54 --- /dev/null +++ b/rust/obs-c-oracle/oracle/object_id.c @@ -0,0 +1,92 @@ +/* Original C, real private layouts, and the exact production field adapters. */ +#include "libobs/build/libobs.pch.h" +#include +#include + +const char *oracle_obs_obj_get_id(void *obj); +#define obs_obj_get_id oracle_obs_obj_get_id +#include "libobs/obs-object-id.c.h" +#undef obs_obj_get_id +#include "object_id_fields.inc" + +typedef const char *(*object_id_query)(void *); +bool oracle_object_id_check(int kind, bool present, const char *id, const char *original_id, object_id_query query, + const char *safe_id); +bool oracle_object_id_check(int kind, bool present, const char *id, const char *original_id, object_id_query query, + const char *safe_id) +{ + if (OBS_OBJ_TYPE_INVALID != 0 || OBS_OBJ_TYPE_SOURCE != 1 || OBS_OBJ_TYPE_OUTPUT != 2 || + OBS_OBJ_TYPE_ENCODER != 3 || OBS_OBJ_TYPE_SERVICE != 4 || OBS_OBJ_TYPE_CANVAS != 5) + return false; + if (!present) + return !query(NULL) && !oracle_obs_obj_get_id(NULL) && !safe_id; + + size_t size = sizeof(struct obs_context_data); + switch (kind) { + case OBS_OBJ_TYPE_SOURCE: + size = sizeof(obs_source_t); + break; + case OBS_OBJ_TYPE_OUTPUT: + size = sizeof(obs_output_t); + break; + case OBS_OBJ_TYPE_ENCODER: + size = sizeof(obs_encoder_t); + break; + case OBS_OBJ_TYPE_SERVICE: + size = sizeof(obs_service_t); + break; + } + void *obj = calloc(1, size), *before = malloc(size); + if (!obj || !before) { + free(obj); + free(before); + return false; + } + struct obs_context_data *context = obj; + context->type = (enum obs_obj_type)kind; + context->name = "different display name"; + const char *const *field = NULL, *expected = NULL; + switch (kind) { + case OBS_OBJ_TYPE_SOURCE: + ((obs_source_t *)obj)->info.id = id; + field = &((obs_source_t *)obj)->info.id; + break; + case OBS_OBJ_TYPE_OUTPUT: + ((obs_output_t *)obj)->info.id = id; + field = &((obs_output_t *)obj)->info.id; + break; + case OBS_OBJ_TYPE_ENCODER: + ((obs_encoder_t *)obj)->info.id = id; + ((obs_encoder_t *)obj)->orig_info.id = original_id; + field = &((obs_encoder_t *)obj)->info.id; + break; + case OBS_OBJ_TYPE_SERVICE: + ((obs_service_t *)obj)->info.id = id; + field = &((obs_service_t *)obj)->info.id; + break; + } + memcpy(before, obj, size); + bool valid = obs_object_id_kind(obj) == kind; + switch (kind) { + case OBS_OBJ_TYPE_SOURCE: + valid = valid && obs_object_id_source_id(obj) == field; + break; + case OBS_OBJ_TYPE_OUTPUT: + valid = valid && obs_object_id_output_id(obj) == field; + break; + case OBS_OBJ_TYPE_ENCODER: + valid = valid && obs_object_id_encoder_id(obj) == field; + break; + case OBS_OBJ_TYPE_SERVICE: + valid = valid && obs_object_id_service_id(obj) == field; + break; + } + if (field) + expected = id; + for (int repeat = 0; repeat < 2; ++repeat) + valid = valid && query(obj) == expected && oracle_obs_obj_get_id(obj) == expected && + safe_id == expected && memcmp(before, obj, size) == 0; + free(before); + free(obj); + return valid; +} diff --git a/rust/obs-core/src/ffi/mod.rs b/rust/obs-core/src/ffi/mod.rs index 13c31880a9..a4cb6c50b5 100644 --- a/rust/obs-core/src/ffi/mod.rs +++ b/rust/obs-core/src/ffi/mod.rs @@ -56,6 +56,7 @@ pub mod module_type_queries; pub mod module_type_rules; pub mod monitoring_device_getter; pub mod nix_system_info; +pub mod object_id; pub mod object_queries; pub mod object_ref; pub mod object_validity_query; diff --git a/rust/obs-core/src/ffi/object_id.rs b/rust/obs-core/src/ffi/object_id.rs new file mode 100644 index 0000000000..506014778f --- /dev/null +++ b/rust/obs-core/src/ffi/object_id.rs @@ -0,0 +1,40 @@ +//! Borrow private C fields only after NULL and kind validation. +use crate::object_id::{self, Kind}; +use core::ffi::{c_char, c_int, c_void}; +use core::ptr::NonNull; + +unsafe extern "C" { + fn obs_object_id_kind(obj: *const c_void) -> c_int; + fn obs_object_id_source_id(obj: *const c_void) -> *const *const c_char; + fn obs_object_id_output_id(obj: *const c_void) -> *const *const c_char; + fn obs_object_id_encoder_id(obj: *const c_void) -> *const *const c_char; + fn obs_object_id_service_id(obj: *const c_void) -> *const *const c_char; +} + +/// Return the concrete info ID, borrowed from the object, or NULL. +/// +/// # Safety +/// A non-NULL object must start with a readable live obs_context_data. For a +/// supported kind it must have that kind's concrete layout, with the same +/// lifetime and synchronization as the original C query. The object owns the ID. +#[unsafe(no_mangle)] +pub unsafe extern "C" fn obs_obj_get_id(obj: *mut c_void) -> *const c_char { + let kind = NonNull::new(obj).map(|obj| { + // SAFETY: NULL is rejected before the real-header context projection. + unsafe { obs_object_id_kind(obj.as_ptr()) } + }); + object_id::query(kind, |kind| { + // SAFETY: the safe core calls this only for a non-NULL object with a + // supported kind. Only that concrete layout's field is projected. + unsafe { + let field = match kind { + Kind::Source => obs_object_id_source_id(obj), + Kind::Output => obs_object_id_output_id(obj), + Kind::Encoder => obs_object_id_encoder_id(obj), + Kind::Service => obs_object_id_service_id(obj), + }; + *field + } + }) + .unwrap_or(core::ptr::null()) +} diff --git a/rust/obs-core/src/lib.rs b/rust/obs-core/src/lib.rs index 538edfd218..7eabc550a7 100644 --- a/rust/obs-core/src/lib.rs +++ b/rust/obs-core/src/lib.rs @@ -165,6 +165,7 @@ pub mod module_type_queries; pub mod module_type_rules; pub mod monitoring_device_getter; pub mod nix_system_info; +pub mod object_id; pub mod object_queries; pub mod object_ref; pub mod object_validity_query; diff --git a/rust/obs-core/src/object_id.rs b/rust/obs-core/src/object_id.rs new file mode 100644 index 0000000000..2533898953 --- /dev/null +++ b/rust/obs-core/src/object_id.rs @@ -0,0 +1,23 @@ +//! Lazy borrowed-ID selection without projecting unsupported objects. + +/// Concrete object kinds whose private layouts contain an info ID. +#[derive(Clone, Copy, Debug, PartialEq, Eq)] +pub enum Kind { + Source, + Output, + Encoder, + Service, +} + +/// Read only the ID selected by a supported kind. Missing objects and all +/// other kinds leave the projection closure untouched. +pub fn query(kind: Option, read: impl FnOnce(Kind) -> T) -> Option { + let kind = match kind? { + 1 => Kind::Source, + 2 => Kind::Output, + 3 => Kind::Encoder, + 4 => Kind::Service, + _ => return None, + }; + Some(read(kind)) +} diff --git a/rust/obs-core/tests/obs_core/main.rs b/rust/obs-core/tests/obs_core/main.rs index 34fe527689..285d486655 100644 --- a/rust/obs-core/tests/obs_core/main.rs +++ b/rust/obs-core/tests/obs_core/main.rs @@ -116,6 +116,8 @@ mod nix_log_rules; mod nix_log_rules_parity; mod nix_system_info; mod nix_system_info_parity; +mod object_id; +mod object_id_parity; mod object_queries; mod object_queries_parity; mod object_ref; diff --git a/rust/obs-core/tests/obs_core/object_id.rs b/rust/obs-core/tests/obs_core/object_id.rs new file mode 100644 index 0000000000..7353b4c4c0 --- /dev/null +++ b/rust/obs-core/tests/obs_core/object_id.rs @@ -0,0 +1,61 @@ +//! Tier 1 mirrors every case in unchanged test_object_id.c. +use obs_core::object_id::{Kind, query}; + +#[test] +fn null_object() { + assert_eq!(query::<()>(None, |_| panic!("NULL projection")), None); +} + +#[test] +fn unknown_kinds() { + for kind in [0, 5, 6, i32::MAX, -1, i32::MIN] { + let before = kind; + for _ in 0..2 { + assert_eq!( + query::<()>(Some(kind), |_| panic!("unsupported projection")), + None + ); + } + assert_eq!(kind, before); + } +} + +fn stored_id(kind: i32, expected: Kind) { + for id in [Some("concrete_id"), Some(""), None] { + let object = (kind, id, "display name", "original_encoder_id"); + let before = object; + for _ in 0..2 { + let actual = query(Some(object.0), |selected| { + assert_eq!(selected, expected); + object.1 + }) + .expect("supported kind"); + match (actual, id) { + (Some(a), Some(b)) => assert!(std::ptr::eq(a, b)), + (None, None) => (), + _ => panic!("stored NULL changed"), + } + } + assert_eq!(object, before); + } +} + +#[test] +fn source_id() { + stored_id(1, Kind::Source); +} + +#[test] +fn output_id() { + stored_id(2, Kind::Output); +} + +#[test] +fn encoder_id() { + stored_id(3, Kind::Encoder); +} + +#[test] +fn service_id() { + stored_id(4, Kind::Service); +} diff --git a/rust/obs-core/tests/obs_core/object_id_parity.rs b/rust/obs-core/tests/obs_core/object_id_parity.rs new file mode 100644 index 0000000000..36b54637fb --- /dev/null +++ b/rust/obs-core/tests/obs_core/object_id_parity.rs @@ -0,0 +1,75 @@ +//! Tier 3: original C vs shim vs safe selection, including borrowed NULL IDs +//! and context-sized unsupported objects. No intentional differences. +use core::ffi::{c_char, c_int, c_void}; +use obs_core::{ffi::object_id::obs_obj_get_id, object_id}; +use proptest::prelude::*; +use proptest::test_runner::{Config, TestRunner}; + +unsafe extern "C" { + fn oracle_object_id_check( + kind: c_int, + present: bool, + id: *const c_char, + original_id: *const c_char, + query: unsafe extern "C" fn(*mut c_void) -> *const c_char, + safe_id: *const c_char, + ) -> bool; +} + +fn check(kind: i32, present: bool, bytes: Option>) { + let id = bytes + .as_ref() + .map_or(core::ptr::null(), |bytes| bytes.as_ptr().cast()); + let before = bytes.clone(); + let safe = object_id::query(present.then_some(kind), |_| id).unwrap_or(core::ptr::null()); + // SAFETY: C allocates exactly the real-header layout for supported kinds, + // and only a context otherwise. IDs remain borrowed and live throughout; + // the oracle compares pointers and all object bytes, then frees its fixture. + assert!(unsafe { + oracle_object_id_check( + kind, + present, + id, + c"original encoder ID".as_ptr(), + obs_obj_get_id, + safe, + ) + }); + assert_eq!(bytes, before); +} + +#[test] +fn object_id_matches_original_c() { + for kind in [i32::MIN, -1, 0, 1, 2, 3, 4, 5, 6, i32::MAX] { + for present in [false, true] { + for bytes in [None, Some(vec![0]), Some(b"concrete_id\0".to_vec())] { + check(kind, present, bytes); + } + } + } + TestRunner::new(Config { + cases: obs_c_oracle::parity::cases(128), + failure_persistence: None, + ..Config::default() + }) + .run( + &( + any::(), + any::(), + prop::option::of(prop::collection::vec(any::(), 0..65)), + ), + |(kind, present, bytes)| { + let bytes = bytes.map(|mut bytes| { + bytes.push(0); + bytes + }); + check(kind, present, bytes.clone()); + // Every generated ID also reaches each supported concrete layout. + for kind in 0..=6 { + check(kind, true, bytes.clone()); + } + Ok(()) + }, + ) + .unwrap(); +} diff --git a/test/cmocka/video-info-query-link-support.c b/test/cmocka/video-info-query-link-support.c index b9d1335f2f..27eb898a21 100644 --- a/test/cmocka/video-info-query-link-support.c +++ b/test/cmocka/video-info-query-link-support.c @@ -1,3 +1,4 @@ /* Link the real private-layout adapter; the fixture owns its core. */ #include "libobs/obs-video-info-query-fields.c.h" #include "libobs/obs-type-catalog-enum-fields.c.h" +#include "libobs/obs-object-id-fields.c.h"