diff --git a/ci/test_bench_libobs_stand_ins.py b/ci/test_bench_libobs_stand_ins.py index 901f47e958..bc676865bd 100644 --- a/ci/test_bench_libobs_stand_ins.py +++ b/ci/test_bench_libobs_stand_ins.py @@ -35,7 +35,9 @@ # The encoder name getter is absent too: #5253 exports it from obs-encoder; # the synthetic interleaver replaces only its private name seam. # The key-name store is absent: #5321 exports it from obs-hotkey (hidden in -# libobs, defined by the workspace). +# libobs, defined by the workspace). The output name getter is absent as +# well: #5302 exports it from obs-output, and the interleaver names its +# output through a private seam too. SYMBOLS = ( "obs_encoder_group_num_started", "obs_encoder_interleave_info", @@ -68,7 +70,6 @@ "obs_output_caption_track_fields", "obs_output_delay_fields", "obs_output_delay_signal", - "obs_output_get_name", "obs_output_initialize_encoders", "obs_output_interleave_active", "obs_output_interleave_add_caption", @@ -76,6 +77,7 @@ "obs_output_interleave_fields", "obs_output_interleave_first_audio_encoder_index", "obs_output_interleave_first_video_encoder_index", + "obs_output_interleave_output_name", ) diff --git a/libobs/build/libobs.core-output+.h.c b/libobs/build/libobs.core-output+.h.c index bfe1d17ffa..8d1c61653f 100644 --- a/libobs/build/libobs.core-output+.h.c +++ b/libobs/build/libobs.core-output+.h.c @@ -52,7 +52,7 @@ #include "libobs/obs-output-enum-types-with-protocol.c.h" #include "libobs/obs-output-flags-fields.c.h" #include "libobs/obs-output-frames-dropped-fields.c.h" -#include "libobs/obs-output-get-name.c.h" +#include "libobs/obs-output-get-name-fields.c.h" #include "libobs/obs-output-handler-getters-fields.c.h" #include "libobs/obs-output-identity-fields.c.h" #include "libobs/obs-output-interleave-name-fields.c.h" diff --git a/libobs/build/libobs.ported-output-get-name+.h.c b/libobs/build/libobs.ported-output-get-name+.h.c new file mode 100644 index 0000000000..e7a26b99a3 --- /dev/null +++ b/libobs/build/libobs.ported-output-get-name+.h.c @@ -0,0 +1,5 @@ +/* Unity group (BUILD.md): original output name getter, C reference only. */ + +#include "libobs/build/libobs.pch.h" + +#include "libobs/obs-output-get-name.c.h" diff --git a/libobs/build/ported-source-sets/output-get-name.json b/libobs/build/ported-source-sets/output-get-name.json new file mode 100644 index 0000000000..c11cad8fa6 --- /dev/null +++ b/libobs/build/ported-source-sets/output-get-name.json @@ -0,0 +1,5 @@ +{ + "LIBOBS_PORTED_SOURCES": [ + "build/libobs.ported-output-get-name+.h.c" + ] +} diff --git a/libobs/cmake/rust-exports.d/obs-output-output_get_name.txt b/libobs/cmake/rust-exports.d/obs-output-output_get_name.txt new file mode 100644 index 0000000000..c1599690cb --- /dev/null +++ b/libobs/cmake/rust-exports.d/obs-output-output_get_name.txt @@ -0,0 +1 @@ +obs_output_get_name diff --git a/libobs/obs-output-get-name-fields.c.h b/libobs/obs-output-get-name-fields.c.h new file mode 100644 index 0000000000..6e67a99145 --- /dev/null +++ b/libobs/obs-output-get-name-fields.c.h @@ -0,0 +1,8 @@ +#include "libobs/build/libobs.pch.h" + +const char *obs_output_get_name_context_name(const obs_output_t *output); + +const char *obs_output_get_name_context_name(const obs_output_t *output) +{ + return output->context.name; +} diff --git a/libobs/obs-output-interleave-name-fields.c.h b/libobs/obs-output-interleave-name-fields.c.h index fb02b8ac7e..ce70faefda 100644 --- a/libobs/obs-output-interleave-name-fields.c.h +++ b/libobs/obs-output-interleave-name-fields.c.h @@ -1,10 +1,17 @@ #include "libobs/build/libobs.pch.h" -/* A private seam lets synthetic interleaver fixtures supply their own names - * without replacing the public encoder getter or its native-layout projection. */ +/* Private seams let synthetic interleaver fixtures supply their own names + * without replacing the public output and encoder getters or their + * native-layout projections. */ const char *obs_output_interleave_encoder_name(const obs_encoder_t *encoder); +const char *obs_output_interleave_output_name(const obs_output_t *output); const char *obs_output_interleave_encoder_name(const obs_encoder_t *encoder) { return obs_encoder_get_name(encoder); } + +const char *obs_output_interleave_output_name(const obs_output_t *output) +{ + return obs_output_get_name(output); +} diff --git a/rust/README.md b/rust/README.md index f50a4b52ed..ca298fda39 100644 --- a/rust/README.md +++ b/rust/README.md @@ -342,6 +342,7 @@ The private output starting/encoder-demand queries use lazy safe Rust decisions | `libobs/obs-output-protocol-getter.c.h` | `obs-output::output_protocols` | Public borrowed protocol metadata getter swapped in hybrid libobs (#4062). Preserves service flags, NULL diagnostics, missing/empty and arbitrary-byte metadata, pointer identity and unchanged state. Real-header layout, Tier 1, differential mutation coverage and unchanged actual-library Tier 2. No intentional differences. | | `libobs/obs-output-set-last-error.c.h` | `obs-output::output_last_error` | Public error setter with a private writable-slot adapter. Preserves NULL diagnostics, replacement/clearing and empty or arbitrary-byte strings using libobs allocator ownership. Tier 1 mirrors existing C characterization; Tier 3 compares replacement sequences and untouched output state. Aliasing the old allocation is excluded as undefined in C. | | `libobs/obs-output-get-last-error.c.h` | `obs-output::output_get_last_error` | Swapped in hybrid `libobs` (group `libobs.ported-output-get-last-error` keeps the original for the C reference). After `obs_output_valid()`'s exact NULL diagnostic, safe Rust returns the output's own `last_error_message`, else the first error of the video encoder slots in ascending order, else of the audio slots, else NULL. Each field is read lazily, so nothing below a hit is read; the pointer is borrowed unchanged (empty and non-UTF-8 strings included), with no copy, cache, reference or lock. Reads go through the existing production adapters (`obs-output-last-error-fields`, `obs-output-video-encoder-getter-fields`, `obs-output-encoder-getters-fields`, `obs-encoder-last-error-fields`), no new layout crosses the ABI. Tier 1 mirrors every assertion of the unchanged `test_output_get_last_error` with read-order traces; Tier 3 compares the original and the shim on real-layout output/encoder fixtures (identity, untouched state, NULL diagnostic, no allocation), checks each adapter addresses the original's field, and catches five mutants. No intentional differences; Tier 4 does not apply. | +| `libobs/obs-output-get-name.c.h` | `obs-output::output_get_name` | Swapped in hybrid `libobs` (group `libobs.ported-output-get-name` keeps the original for the C reference). After `obs_output_valid()`'s exact NULL diagnostic, safe Rust returns the output's borrowed `context.name` pointer, read once through the private `obs-output-get-name-fields` adapter: no copy, cache, reference or lock, and NULL, empty and non-UTF-8 names unchanged. The interleaver names its output through the private `obs_output_interleave_output_name` seam, so its synthetic fixture no longer defines the public getter. Tier 1 mirrors every case of the unchanged `test_output_get_name`; Tier 3 compares the original and the shim on a real-layout output (identity, repeated and renamed reads, exact NULL log, untouched bytes), checks the adapter addresses the original's field, and catches four mutants. No intentional differences; Tier 4 does not apply. | | `libobs/obs-output-supported-codecs.c.h` | `obs-output::output_supported_codecs` | Public borrowed video/audio codec metadata getters swapped in hybrid libobs (#3958). Preserves NULL diagnostics, absent/empty and arbitrary-byte metadata, pointer identity and unchanged output state. Private field-address adapter with real-header layout checks, Tier 1, differential mutation coverage and unchanged actual-library Tier 2. No intentional differences. | | `libobs/obs-output-size-getters.c.h` | `obs-output::output_size_getters` | Width/height and indexed wrappers through opaque private accessors. Preserves NULL/non-video diagnostics before bounds checks, encoder versus video fallback, independent scaled axes and unchanged output state. Tier 1 and original-C differential coverage; unchanged Tier 2 characterization. No intentional differences; Tier 4 does not apply. | | `libobs/obs-output-type-info.c.h` | `obs-output::output_type_info` | #4678: borrowed type display name, exact flag bits and raw supported codec pointers through the shared existing output-type lookup. Real-header snapshots, Tier 1 characterization, original-C differential/mutation checks and unchanged Tier 2 replay. No intentional differences; Tier 4 inapplicable. | diff --git a/rust/obs-build/tests/native.rs b/rust/obs-build/tests/native.rs index 6b2afa3050..e691d12281 100644 --- a/rust/obs-build/tests/native.rs +++ b/rust/obs-build/tests/native.rs @@ -391,6 +391,8 @@ mod canonical_object_validity_query_is_registered; mod canonical_output_flags_is_registered; #[path = "native/registration/canonical_output_get_last_error_swap_is_registered.rs"] mod canonical_output_get_last_error_swap_is_registered; +#[path = "native/registration/canonical_output_get_name_swap_is_registered.rs"] +mod canonical_output_get_name_swap_is_registered; #[path = "native/registration/canonical_output_handler_getters_is_registered.rs"] mod canonical_output_handler_getters_is_registered; #[path = "native/registration/canonical_output_identity_getters_is_registered.rs"] diff --git a/rust/obs-build/tests/native/registration/canonical_output_get_name_swap_is_registered.rs b/rust/obs-build/tests/native/registration/canonical_output_get_name_swap_is_registered.rs new file mode 100644 index 0000000000..380de31f84 --- /dev/null +++ b/rust/obs-build/tests/native/registration/canonical_output_get_name_swap_is_registered.rs @@ -0,0 +1,43 @@ +use super::*; + +#[test] +fn canonical_output_get_name_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_output_get_name"; + 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_output_get_name.c") + ); + assert_eq!(selected.tests[name].target, name); + } + let ported = + fs::read_to_string(root.join("libobs/build/ported-source-sets/output-get-name.json")) + .unwrap(); + assert!(ported.contains("build/libobs.ported-output-get-name+.h.c")); + let original = "#include \"libobs/obs-output-get-name.c.h\""; + let group = + fs::read_to_string(root.join("libobs/build/libobs.ported-output-get-name+.h.c")).unwrap(); + assert!(group.contains(original)); + let hybrid = fs::read_to_string(root.join("libobs/build/libobs.core-output+.h.c")).unwrap(); + assert!(!hybrid.contains(original)); + // The context-name adapter and the interleaver's name seams stay in the core. + for adapter in [ + "obs-output-get-name-fields.c.h", + "obs-output-interleave-name-fields.c.h", + ] { + assert!(hybrid.contains(&format!("#include \"libobs/{adapter}\""))); + } + let support = + fs::read_to_string(root.join("test/cmocka/build/rust-static-support.json")).unwrap(); + assert!(support.contains("\"test/cmocka/output-get-name-link-support.c\"")); + let exports = + fs::read_to_string(root.join("libobs/cmake/rust-exports.d/obs-output-output_get_name.txt")) + .unwrap(); + assert_eq!(exports.lines().collect::>(), ["obs_output_get_name"]); +} diff --git a/rust/obs-c-oracle/benches/port_comparison.rs b/rust/obs-c-oracle/benches/port_comparison.rs index 5c88026b8f..804064df99 100644 --- a/rust/obs-c-oracle/benches/port_comparison.rs +++ b/rust/obs-c-oracle/benches/port_comparison.rs @@ -144,8 +144,8 @@ mod libobs_stand_ins { } #[unsafe(no_mangle)] - pub extern "C" fn obs_output_get_name() -> ! { - called("obs_output_get_name") + pub extern "C" fn obs_output_interleave_output_name() -> ! { + called("obs_output_interleave_output_name") } #[unsafe(no_mangle)] diff --git a/rust/obs-c-oracle/oracle/output_get_name.c b/rust/obs-c-oracle/oracle/output_get_name.c new file mode 100644 index 0000000000..6c225b3cfc --- /dev/null +++ b/rust/obs-c-oracle/oracle/output_get_name.c @@ -0,0 +1,98 @@ +/* Original output name getter and its production context-name adapter, over + * the real private output layout. + * + * The MSVC UCRT headers define snprintf and friends inline in every object + * that includes them, which lets a COFF link pull this member into an + * unrelated test. _NO_CRT_STDIO_INLINE keeps them declarations only, and the + * log capture below reads its arguments without formatting them. */ +#define _NO_CRT_STDIO_INLINE +#include +#include +#include +#include +#define obs_output_get_name oracle_obs_output_get_name +#include "obs-output-get-name.c.h" +#undef obs_output_get_name +#include "obs-output-get-name-fields.c.h" + +struct ogn_log { + unsigned count; + int level; + const char *format; + const char *function; + const char *parameter; +}; + +static void ogn_capture(int level, const char *format, va_list args, void *param) +{ + struct ogn_log *log = param; + log->count++; + log->level = level; + log->format = format; + log->function = va_arg(args, const char *); + log->parameter = va_arg(args, const char *); +} + +static bool ogn_exact_null_log(const struct ogn_log *log) +{ + return log->count == 1 && log->level == LOG_DEBUG && strcmp(log->format, "%s: Null '%s' parameter") == 0 && + strcmp(log->function, "obs_output_get_name") == 0 && strcmp(log->parameter, "output") == 0; +} + +typedef const char *(*output_name_getter)(const obs_output_t *output); + +/* The original runs on one capture and the candidate on another: same + * borrowed pointer, and either no log (a live output) or exactly + * obs_output_valid()'s diagnostic (NULL). */ +static bool ogn_compare(const obs_output_t *output, output_name_getter candidate) +{ + struct ogn_log original = {0}, rust = {0}; + base_set_log_handler(ogn_capture, &original); + const char *expected = oracle_obs_output_get_name(output); + base_set_log_handler(ogn_capture, &rust); + const char *actual = candidate(output); + if (actual != expected) + return false; + if (output) + return original.count == 0 && rust.count == 0; + return !actual && ogn_exact_null_log(&original) && ogn_exact_null_log(&rust); +} + +static bool ogn_copy(char *dst, size_t size, const char *src) +{ + size_t len = strlen(src); + if (len >= size) + return false; + memcpy(dst, src, len + 1); + return true; +} + +/* NULL, then a live real-layout output read twice, then the same output + * after its context-name slot changes. The output's bytes must be untouched + * by every read, and the production adapter must address the original's + * field. */ +bool oracle_output_get_name_probe(output_name_getter candidate, const char *name, const char *renamed, bool null_name); +bool oracle_output_get_name_probe(output_name_getter candidate, const char *name, const char *renamed, bool null_name) +{ + log_handler_t saved_handler; + void *saved_param; + char first[256], second[256]; + if (!ogn_copy(first, sizeof(first), name) || !ogn_copy(second, sizeof(second), renamed)) + return false; + + base_get_log_handler(&saved_handler, &saved_param); + static struct obs_output output; + memset(&output, 0, sizeof(output)); + output.context.name = null_name ? NULL : first; + static unsigned char before[sizeof(output)]; + memcpy(before, &output, sizeof(output)); + bool equal = ogn_compare(NULL, candidate) && ogn_compare(&output, candidate) && + ogn_compare(&output, candidate) && memcmp(before, &output, sizeof(output)) == 0 && + obs_output_get_name_context_name(&output) == output.context.name; + output.context.name = second; + memcpy(before, &output, sizeof(output)); + equal = equal && ogn_compare(&output, candidate) && memcmp(before, &output, sizeof(output)) == 0 && + obs_output_get_name_context_name(&output) == second; + base_set_log_handler(saved_handler, saved_param); + return equal; +} diff --git a/rust/obs-c-oracle/oracle/output_interleave.c b/rust/obs-c-oracle/oracle/output_interleave.c index 7c009b7488..3356e38a7d 100644 --- a/rust/obs-c-oracle/oracle/output_interleave.c +++ b/rust/obs-c-oracle/oracle/output_interleave.c @@ -9,12 +9,12 @@ * obs_encoder_interleave_info() and obs_encoder_group_num_started(), the * obs-output.c wrappers (obs_output_interleave_active(), the two first * encoder index helpers and obs_output_interleave_add_caption()), - * obs_encoder_packet_create_instance() and _release(), obs_output_get_name() - * and the private obs_output_interleave_encoder_name() seam. The cargo test that links this file defines - * them, recording each call, and the Rust shim calls the same ones, so both - * sides are seen through the same stubs; the packet copy and release are - * obs-encoder's real exports. pthread, blog and the darray come from the - * real headers and libraries. */ + * obs_encoder_packet_create_instance() and _release(), and the private + * obs_output_interleave_output_name() and _encoder_name() seams. The cargo + * test that links this file defines them, recording each call, and the Rust + * shim calls the same ones, so both sides are seen through the same stubs; + * the packet copy and release are obs-encoder's real exports. pthread, blog + * and the darray come from the real headers and libraries. */ /* As libobs is built for release (RelWithDebInfo defines NDEBUG): the * file's asserts and those of the darray.h inlines it calls are off. One of * them is reachable: discard_to_idx() pops the packet time of each video @@ -31,6 +31,7 @@ #define free_packets oracle_free_packets #define os_gettime_ns oracle_output_interleave_clock_ns #define obs_encoder_get_name obs_output_interleave_encoder_name +#define obs_output_get_name obs_output_interleave_output_name /* send_interleaved() names itself with __FUNCTION__, a GCC, Clang and MSVC * extension that -Wpedantic rejects, and the file converts between signed @@ -48,6 +49,7 @@ #endif #include "obs-output-interleave.c.h" #undef obs_encoder_get_name +#undef obs_output_get_name #if defined(__GNUC__) || defined(__clang__) #pragma GCC diagnostic pop #endif diff --git a/rust/obs-output/src/ffi/mod.rs b/rust/obs-output/src/ffi/mod.rs index 792235522d..3894bd4ab6 100644 --- a/rust/obs-output/src/ffi/mod.rs +++ b/rust/obs-output/src/ffi/mod.rs @@ -49,6 +49,7 @@ pub mod output_congestion; pub mod output_delay; pub mod output_flags; pub mod output_get_last_error; +pub mod output_get_name; pub mod output_handler_getters; pub mod output_identity; pub mod output_interleave; diff --git a/rust/obs-output/src/ffi/output_get_name.rs b/rust/obs-output/src/ffi/output_get_name.rs new file mode 100644 index 0000000000..efe75bf6d3 --- /dev/null +++ b/rust/obs-output/src/ffi/output_get_name.rs @@ -0,0 +1,37 @@ +//! The public output name query over the real private context-name field. +//! The NULL guard lives in the safe core; the field read sits behind a +//! private C adapter, so no output layout crosses the ABI. +use crate::output_get_name::get_name; +use core::ffi::{c_char, c_int, c_void}; + +unsafe extern "C" { + fn obs_output_get_name_context_name(output: *const c_void) -> *const c_char; + fn blog(level: c_int, format: *const c_char, ...); +} + +/// Borrow the output's current context name, or NULL for a NULL output. +/// +/// # Safety +/// A non-NULL output must be live and readable during the call. The result +/// borrows its C-owned name, as in C. +#[unsafe(no_mangle)] +pub unsafe extern "C" fn obs_output_get_name(output: *const c_void) -> *const c_char { + get_name( + (!output.is_null()).then_some(|| { + // SAFETY: get_name projects only a non-NULL, live output. + unsafe { obs_output_get_name_context_name(output) } + }), + || { + // SAFETY: obs_output_valid()'s static format and arguments. + unsafe { + blog( + 400, + c"%s: Null '%s' parameter".as_ptr(), + c"obs_output_get_name".as_ptr(), + c"output".as_ptr(), + ) + } + }, + ) + .unwrap_or(core::ptr::null()) +} diff --git a/rust/obs-output/src/ffi/output_interleave.rs b/rust/obs-output/src/ffi/output_interleave.rs index 674b562fdb..183d14845a 100644 --- a/rust/obs-output/src/ffi/output_interleave.rs +++ b/rust/obs-output/src/ffi/output_interleave.rs @@ -206,7 +206,8 @@ unsafe extern "C" { fn obs_encoder_packet_create_instance(dst: *mut encoder_packet, src: *const encoder_packet); // EXPORT. fn obs_encoder_packet_release(packet: *mut encoder_packet); - fn obs_output_get_name(output: *const c_void) -> *const c_char; + // Private C adapter; synthetic tests replace only this seam. + fn obs_output_interleave_output_name(output: *const c_void) -> *const c_char; // Private C adapter; synthetic tests replace only this seam. fn obs_output_interleave_encoder_name(encoder: *const c_void) -> *const c_char; // libobs's pthread (w32-pthreads on Windows); the mutexes are opaque here. @@ -893,7 +894,7 @@ impl InterleaveOutput for COutput { LOG_WARNING, c"obs-output '%s': Missing keyframe with pts %lld for encoder '%s' (track: %zu)" .as_ptr(), - obs_output_get_name(self.output), + obs_output_interleave_output_name(self.output), pts as c_longlong, obs_output_interleave_encoder_name(encoder as *const c_void), track, diff --git a/rust/obs-output/src/lib.rs b/rust/obs-output/src/lib.rs index c6f11c351c..78cfe7b888 100644 --- a/rust/obs-output/src/lib.rs +++ b/rust/obs-output/src/lib.rs @@ -194,6 +194,7 @@ pub mod output_caption_queue; pub mod output_congestion; pub mod output_flags; pub mod output_get_last_error; +pub mod output_get_name; pub mod output_handler_getters; pub mod output_identity; pub mod output_last_error; diff --git a/rust/obs-output/src/output_get_name.rs b/rust/obs-output/src/output_get_name.rs new file mode 100644 index 0000000000..ce0296030d --- /dev/null +++ b/rust/obs-output/src/output_get_name.rs @@ -0,0 +1,29 @@ +//! Safe core of `libobs/obs-output-get-name.c.h`: `obs_output_get_name()` +//! borrows the output's current context name. +//! +//! A missing output emits `obs_output_valid()`'s diagnostic and reads +//! nothing. A present output's name is projected exactly once and returned +//! unchanged, NULL, empty and non-UTF-8 names included: no copy, cache, +//! reference or lock. + +#![deny( + clippy::arithmetic_side_effects, + clippy::as_conversions, + clippy::indexing_slicing, + clippy::unwrap_used, + clippy::expect_used, + clippy::panic, + clippy::todo, + clippy::unimplemented, + clippy::unreachable +)] + +/// `None` is the NULL output: `log_null` runs once and nothing is read. +/// Otherwise `project` reads the live output's name once. +pub fn get_name(output: Option T>, log_null: impl FnOnce()) -> Option { + let Some(project) = output else { + log_null(); + return None; + }; + Some(project()) +} diff --git a/rust/obs-output/tests/output/interleave_parity.rs b/rust/obs-output/tests/output/interleave_parity.rs index ac17027505..38f68656b6 100644 --- a/rust/obs-output/tests/output/interleave_parity.rs +++ b/rust/obs-output/tests/output/interleave_parity.rs @@ -906,13 +906,13 @@ pub unsafe extern "C" fn obs_encoder_group_num_started(group: *mut c_void) -> u3 } } -/// `obs_output_get_name`. +/// `obs_output_interleave_output_name`. /// /// # Safety /// /// `output` is a live [`Fixture`]. #[unsafe(no_mangle)] -pub unsafe extern "C" fn obs_output_get_name(output: *const c_void) -> *const c_char { +pub unsafe extern "C" fn obs_output_interleave_output_name(output: *const c_void) -> *const c_char { // SAFETY: caller contract. unsafe { fixture(output) }.name } diff --git a/rust/obs-output/tests/output/main.rs b/rust/obs-output/tests/output/main.rs index 4304abd8bd..f3dc38d335 100644 --- a/rust/obs-output/tests/output/main.rs +++ b/rust/obs-output/tests/output/main.rs @@ -100,6 +100,8 @@ mod output_flags; mod output_flags_parity; mod output_get_last_error; mod output_get_last_error_parity; +mod output_get_name; +mod output_get_name_parity; mod output_handler_getters; mod output_handler_getters_parity; mod output_identity; diff --git a/rust/obs-output/tests/output/output_get_name.rs b/rust/obs-output/tests/output/output_get_name.rs new file mode 100644 index 0000000000..3e727a8044 --- /dev/null +++ b/rust/obs-output/tests/output/output_get_name.rs @@ -0,0 +1,60 @@ +//! Tier 1 mirrors test_output_get_name.c on the safe core, with a cell +//! standing in for the output's context-name slot. +use obs_output::output_get_name::get_name; +use std::cell::Cell; +use std::ffi::CStr; + +#[test] +fn null_output_logs_and_returns_null() { + let logs = Cell::new(0); + let name = get_name(None:: &'static CStr>, || logs.set(logs.get() + 1)); + assert_eq!(name, None); + assert_eq!(logs.get(), 1); +} + +fn name<'a>(slot: &'a Cell<&'a CStr>) -> &'a CStr { + get_name(Some(|| slot.get()), || panic!("a live output logged")).unwrap() +} + +#[test] +fn returns_the_context_name_pointer_silently() { + let slot = Cell::new(c"get name"); + let first = name(&slot); + let again = name(&slot); + assert_eq!(first.as_ptr(), slot.get().as_ptr()); + assert_eq!(again.as_ptr(), first.as_ptr()); + assert_eq!(first, c"get name"); +} + +#[test] +fn unnamed_output_returns_its_generated_name() { + let slot = Cell::new(c"__unnamed0003"); + let current = name(&slot); + assert_eq!(current.as_ptr(), slot.get().as_ptr()); + assert!(current.to_bytes().starts_with(b"__unnamed")); +} + +#[test] +fn reads_the_current_context_name() { + let slot = Cell::new(c"before"); + assert_eq!(name(&slot), c"before"); + let replacement = c"after"; + slot.set(replacement); + let current = name(&slot); + assert_eq!(current.as_ptr(), replacement.as_ptr()); + assert_eq!(current, c"after"); +} + +#[test] +fn projects_once_and_returns_even_an_absent_name() { + let calls = Cell::new(0); + let current = get_name( + Some(|| { + calls.set(calls.get() + 1); + None::<&CStr> + }), + || panic!("a live output logged"), + ); + assert_eq!(current, Some(None)); + assert_eq!(calls.get(), 1); +} diff --git a/rust/obs-output/tests/output/output_get_name_parity.rs b/rust/obs-output/tests/output/output_get_name_parity.rs new file mode 100644 index 0000000000..8aaceae7c2 --- /dev/null +++ b/rust/obs-output/tests/output/output_get_name_parity.rs @@ -0,0 +1,140 @@ +//! Tier 3: the untouched C body against the Rust shim on a real-layout +//! output, through the production context-name adapter. No intentional +//! differences. +use core::ffi::{c_char, c_int, c_void}; +use obs_output::ffi::output_get_name::obs_output_get_name; +use std::ffi::CString; + +type Getter = unsafe extern "C" fn(*const c_void) -> *const c_char; +unsafe extern "C" { + fn oracle_output_get_name_probe( + get: Getter, + name: *const c_char, + renamed: *const c_char, + null_name: bool, + ) -> bool; + fn obs_output_get_name_context_name(output: *const c_void) -> *const c_char; + fn blog(level: c_int, format: *const c_char, ...); +} + +fn probe(get: Getter, name: &[u8], renamed: &[u8], null_name: bool) -> bool { + let name = CString::new(name).unwrap(); + let renamed = CString::new(renamed).unwrap(); + // SAFETY: C copies the bounded strings and passes NULL or a live + // real-layout output to both getters. The isolated process owns the log + // handler, which the probe restores. + unsafe { oracle_output_get_name_probe(get, name.as_ptr(), renamed.as_ptr(), null_name) } +} + +#[test] +fn original_c_characterization_and_edges() { + if !super::isolated(concat!( + module_path!(), + "::original_c_characterization_and_edges" + )) { + return; + } + for name in [ + b"get name".as_slice(), + b"__unnamed0003", + b"before", + b"", + b"%s-%n", + b"\xff\x80", + ] { + for null_name in [false, true] { + assert!(probe(obs_output_get_name, name, b"after", null_name)); + } + } +} + +/// Reads the field but never logs for NULL. +unsafe extern "C" fn silent_null(output: *const c_void) -> *const c_char { + if output.is_null() { + return core::ptr::null(); + } + // SAFETY: a live probe output. + unsafe { obs_output_get_name_context_name(output) } +} + +/// Logs the NULL diagnostic with another function name. +unsafe extern "C" fn wrong_function(output: *const c_void) -> *const c_char { + if output.is_null() { + // SAFETY: static format and arguments. + unsafe { + blog( + 400, + c"%s: Null '%s' parameter".as_ptr(), + c"obs_output_get_last_error".as_ptr(), + c"output".as_ptr(), + ) + }; + return core::ptr::null(); + } + // SAFETY: a live probe output. + unsafe { obs_output_get_name_context_name(output) } +} + +/// Returns NULL for a live output. +unsafe extern "C" fn always_null(output: *const c_void) -> *const c_char { + // SAFETY: the real shim, called for its NULL diagnostic only. + let _ = unsafe { obs_output_get_name(output) }; + core::ptr::null() +} + +std::thread_local! { + static CACHED: core::cell::Cell<*const c_char> = const { core::cell::Cell::new(core::ptr::null()) }; +} + +/// Caches the first name it reads, so a later rename is missed. +unsafe extern "C" fn cached(output: *const c_void) -> *const c_char { + // SAFETY: the real shim on the probe's NULL or live output. + let name = unsafe { obs_output_get_name(output) }; + if output.is_null() { + return name; + } + CACHED.with(|c| { + if c.get().is_null() { + c.set(name); + } + c.get() + }) +} + +#[test] +fn mutants_are_caught() { + if !super::isolated(concat!(module_path!(), "::mutants_are_caught")) { + return; + } + for mutant in [silent_null as Getter, wrong_function, always_null, cached] { + CACHED.with(|c| c.set(core::ptr::null())); + assert!(!probe(mutant, b"before", b"after", false)); + } +} + +#[test] +fn randomized_original_c_parity() { + if !super::isolated(concat!(module_path!(), "::randomized_original_c_parity")) { + return; + } + use proptest::prelude::*; + use proptest::test_runner::{Config, TestRunner}; + let mut runner = TestRunner::new(Config { + cases: obs_c_oracle::parity::cases(128), + failure_persistence: None, + ..Config::default() + }); + runner + .run( + &( + proptest::collection::vec(1_u8..=255, 0..128), + proptest::collection::vec(1_u8..=255, 0..128), + any::(), + ), + |(name, renamed, null_name)| { + prop_assert!(probe(obs_output_get_name, &name, &renamed, null_name)); + Ok(()) + }, + ) + .unwrap(); +} diff --git a/test/cmocka/build/rust-static-support.json b/test/cmocka/build/rust-static-support.json index aa6139a60c..58571cc569 100644 --- a/test/cmocka/build/rust-static-support.json +++ b/test/cmocka/build/rust-static-support.json @@ -39,7 +39,8 @@ "test/cmocka/graphics-device-queries-link-support.c", "test/cmocka/output-encoder-getters-link-support.c", "test/cmocka/encoder-last-error-link-support.c", - "test/cmocka/hotkey-key-translation-link-support.c" + "test/cmocka/hotkey-key-translation-link-support.c", + "test/cmocka/output-get-name-link-support.c" ], "platform_sources": { "OS_WINDOWS": [ diff --git a/test/cmocka/output-get-name-link-support.c b/test/cmocka/output-get-name-link-support.c new file mode 100644 index 0000000000..3f23af866a --- /dev/null +++ b/test/cmocka/output-get-name-link-support.c @@ -0,0 +1,2 @@ +/* Real private adapter for standalone Rust archive consumers. */ +#include "libobs/obs-output-get-name-fields.c.h"