Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion libobs/build/libobs.core+.h.c
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
4 changes: 4 additions & 0 deletions libobs/build/libobs.ported-object-id+.h.c
Original file line number Diff line number Diff line change
@@ -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"
5 changes: 5 additions & 0 deletions libobs/build/ported-source-sets/object-id.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"LIBOBS_PORTED_SOURCES": [
"build/libobs.ported-object-id+.h.c"
]
}
1 change: 1 addition & 0 deletions libobs/cmake/rust-exports.d/obs-core-object_id.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
obs_obj_get_id
33 changes: 33 additions & 0 deletions libobs/obs-object-id-fields.c.h
Original file line number Diff line number Diff line change
@@ -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;
}
1 change: 1 addition & 0 deletions rust/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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. |
Expand Down
3 changes: 3 additions & 0 deletions rust/obs-build/tests/native.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Original file line number Diff line number Diff line change
@@ -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\""));
}
92 changes: 92 additions & 0 deletions rust/obs-c-oracle/oracle/object_id.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
/* Original C, real private layouts, and the exact production field adapters. */
#include "libobs/build/libobs.pch.h"
#include <stdlib.h>
#include <string.h>

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;
}
1 change: 1 addition & 0 deletions rust/obs-core/src/ffi/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
40 changes: 40 additions & 0 deletions rust/obs-core/src/ffi/object_id.rs
Original file line number Diff line number Diff line change
@@ -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())
}
1 change: 1 addition & 0 deletions rust/obs-core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
23 changes: 23 additions & 0 deletions rust/obs-core/src/object_id.rs
Original file line number Diff line number Diff line change
@@ -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<T>(kind: Option<i32>, read: impl FnOnce(Kind) -> T) -> Option<T> {
let kind = match kind? {
1 => Kind::Source,
2 => Kind::Output,
3 => Kind::Encoder,
4 => Kind::Service,
_ => return None,
};
Some(read(kind))
}
2 changes: 2 additions & 0 deletions rust/obs-core/tests/obs_core/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
61 changes: 61 additions & 0 deletions rust/obs-core/tests/obs_core/object_id.rs
Original file line number Diff line number Diff line change
@@ -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);
}
Loading
Loading