From d78cfde1f567b32c1cc52d51dd1fc90e333b9781 Mon Sep 17 00:00:00 2001 From: Zach Vorhies Date: Thu, 19 Nov 2026 18:22:36 -0800 Subject: [PATCH 1/2] test: characterize canvas mix clearing before extraction (Refs #5324) Ci-Attestation: {"at":1795143766,"gate":"data/all/compatibility","host":"linux-x86_64","key":"","lane":"client-checks","parents":["869cc329a4d9053ba424088d01b05b6bb695bcad"],"secs":283,"stamp":"22c595787778803b4d58f48b85e114a2","tree":"f18b8f2ef90de3ca18f571de56a3b032747ab1cd","v":1,"via":"run"} Ci-Attestation: {"at":1795143766,"gate":"data/all/flatpak-manifest","host":"linux-x86_64","key":"","lane":"client-checks","parents":["869cc329a4d9053ba424088d01b05b6bb695bcad"],"secs":283,"stamp":"ed64e2d405c2d0ed5e8a283d86a39a39","tree":"f18b8f2ef90de3ca18f571de56a3b032747ab1cd","v":1,"via":"run"} Ci-Attestation: {"at":1795143766,"gate":"data/all/qt-xml","host":"linux-x86_64","key":"","lane":"client-checks","parents":["869cc329a4d9053ba424088d01b05b6bb695bcad"],"secs":283,"stamp":"1ccbf5d8a5f7b14de2fc5ba7d60e8ead","tree":"f18b8f2ef90de3ca18f571de56a3b032747ab1cd","v":1,"via":"run"} Ci-Attestation: {"at":1795143766,"gate":"data/all/services-schema","host":"linux-x86_64","key":"","lane":"client-checks","parents":["869cc329a4d9053ba424088d01b05b6bb695bcad"],"secs":283,"stamp":"0405a1449e9c402664c8d1c74c6c1db6","tree":"f18b8f2ef90de3ca18f571de56a3b032747ab1cd","v":1,"via":"run"} Ci-Attestation: {"at":1795143475,"gate":"general/all/client-lint","host":"linux-x86_64","key":"cbe559d1105a462b2fab1f11cec8a86699b4ba70b8e0cd90c963503904cf54e7","lane":"client-lint","parents":["869cc329a4d9053ba424088d01b05b6bb695bcad"],"secs":42,"stamp":"92b968317b3e18ee9f1c29cfe351691a","tree":"f18b8f2ef90de3ca18f571de56a3b032747ab1cd","v":1,"via":"run"} Ci-Attestation: {"at":1795143766,"gate":"python/all/self-test","host":"linux-x86_64","key":"","lane":"client-checks","parents":["869cc329a4d9053ba424088d01b05b6bb695bcad"],"secs":283,"stamp":"d5920762ddc5aa2583351b42544ec12e","tree":"f18b8f2ef90de3ca18f571de56a3b032747ab1cd","v":1,"via":"run"} Ci-Attestation: {"at":1795143477,"gate":"rust/aarch64-apple-darwin/client-clippy","host":"linux-x86_64","key":"cbe559d1105a462b2fab1f11cec8a86699b4ba70b8e0cd90c963503904cf54e7","lane":"client-lint","parents":["869cc329a4d9053ba424088d01b05b6bb695bcad"],"secs":42,"stamp":"d1598c996cccc9c2c5b93234e6094b38","tree":"f18b8f2ef90de3ca18f571de56a3b032747ab1cd","v":1,"via":"run"} Ci-Attestation: {"at":1795143477,"gate":"rust/aarch64-apple-darwin/client-dylint","host":"linux-x86_64","key":"cbe559d1105a462b2fab1f11cec8a86699b4ba70b8e0cd90c963503904cf54e7","lane":"client-lint","parents":["869cc329a4d9053ba424088d01b05b6bb695bcad"],"secs":42,"stamp":"3580a2ff038c665d2b34fdc46526e6a8","tree":"f18b8f2ef90de3ca18f571de56a3b032747ab1cd","v":1,"via":"run"} Ci-Attestation: {"at":1795143476,"gate":"rust/aarch64-pc-windows-msvc/client-clippy","host":"linux-x86_64","key":"cbe559d1105a462b2fab1f11cec8a86699b4ba70b8e0cd90c963503904cf54e7","lane":"client-lint","parents":["869cc329a4d9053ba424088d01b05b6bb695bcad"],"secs":42,"stamp":"61560bcc3aa463c256222a47d076ce1c","tree":"f18b8f2ef90de3ca18f571de56a3b032747ab1cd","v":1,"via":"run"} Ci-Attestation: {"at":1795143476,"gate":"rust/aarch64-pc-windows-msvc/client-dylint","host":"linux-x86_64","key":"cbe559d1105a462b2fab1f11cec8a86699b4ba70b8e0cd90c963503904cf54e7","lane":"client-lint","parents":["869cc329a4d9053ba424088d01b05b6bb695bcad"],"secs":42,"stamp":"32355ab2c62bddf1eee52b43caad7af4","tree":"f18b8f2ef90de3ca18f571de56a3b032747ab1cd","v":1,"via":"run"} Ci-Attestation: {"at":1795143475,"gate":"rust/aarch64-unknown-linux-gnu/client-clippy","host":"linux-x86_64","key":"cbe559d1105a462b2fab1f11cec8a86699b4ba70b8e0cd90c963503904cf54e7","lane":"client-lint","parents":["869cc329a4d9053ba424088d01b05b6bb695bcad"],"secs":42,"stamp":"63432d23eeb52fe77ca4cbeef678266a","tree":"f18b8f2ef90de3ca18f571de56a3b032747ab1cd","v":1,"via":"run"} Ci-Attestation: {"at":1795143476,"gate":"rust/aarch64-unknown-linux-gnu/client-dylint","host":"linux-x86_64","key":"cbe559d1105a462b2fab1f11cec8a86699b4ba70b8e0cd90c963503904cf54e7","lane":"client-lint","parents":["869cc329a4d9053ba424088d01b05b6bb695bcad"],"secs":42,"stamp":"f622febbabfa79bd96fb0608dbad23c3","tree":"f18b8f2ef90de3ca18f571de56a3b032747ab1cd","v":1,"via":"run"} Ci-Attestation: {"at":1795143476,"gate":"rust/x86_64-apple-darwin/client-clippy","host":"linux-x86_64","key":"cbe559d1105a462b2fab1f11cec8a86699b4ba70b8e0cd90c963503904cf54e7","lane":"client-lint","parents":["869cc329a4d9053ba424088d01b05b6bb695bcad"],"secs":42,"stamp":"7063049e21ba685d582d95eeeefb3ea8","tree":"f18b8f2ef90de3ca18f571de56a3b032747ab1cd","v":1,"via":"run"} Ci-Attestation: {"at":1795143477,"gate":"rust/x86_64-apple-darwin/client-dylint","host":"linux-x86_64","key":"cbe559d1105a462b2fab1f11cec8a86699b4ba70b8e0cd90c963503904cf54e7","lane":"client-lint","parents":["869cc329a4d9053ba424088d01b05b6bb695bcad"],"secs":42,"stamp":"30a0bf70b2c441df7123fe4f189cecd1","tree":"f18b8f2ef90de3ca18f571de56a3b032747ab1cd","v":1,"via":"run"} Ci-Attestation: {"at":1795143476,"gate":"rust/x86_64-pc-windows-msvc/client-clippy","host":"linux-x86_64","key":"cbe559d1105a462b2fab1f11cec8a86699b4ba70b8e0cd90c963503904cf54e7","lane":"client-lint","parents":["869cc329a4d9053ba424088d01b05b6bb695bcad"],"secs":42,"stamp":"e159a8db23bc91d0c9c149f84ed927b5","tree":"f18b8f2ef90de3ca18f571de56a3b032747ab1cd","v":1,"via":"run"} Ci-Attestation: {"at":1795143476,"gate":"rust/x86_64-pc-windows-msvc/client-dylint","host":"linux-x86_64","key":"cbe559d1105a462b2fab1f11cec8a86699b4ba70b8e0cd90c963503904cf54e7","lane":"client-lint","parents":["869cc329a4d9053ba424088d01b05b6bb695bcad"],"secs":42,"stamp":"305c7c717721de3431b2c064bc336ce7","tree":"f18b8f2ef90de3ca18f571de56a3b032747ab1cd","v":1,"via":"run"} Ci-Attestation: {"at":1795143475,"gate":"rust/x86_64-unknown-linux-gnu/client-clippy","host":"linux-x86_64","key":"cbe559d1105a462b2fab1f11cec8a86699b4ba70b8e0cd90c963503904cf54e7","lane":"client-lint","parents":["869cc329a4d9053ba424088d01b05b6bb695bcad"],"secs":42,"stamp":"806df248daeeea9904025b11e00a10fd","tree":"f18b8f2ef90de3ca18f571de56a3b032747ab1cd","v":1,"via":"run"} Ci-Attestation: {"at":1795143475,"gate":"rust/x86_64-unknown-linux-gnu/client-dylint","host":"linux-x86_64","key":"cbe559d1105a462b2fab1f11cec8a86699b4ba70b8e0cd90c963503904cf54e7","lane":"client-lint","parents":["869cc329a4d9053ba424088d01b05b6bb695bcad"],"secs":42,"stamp":"9773ca5ee0f001abc673385da9caaced","tree":"f18b8f2ef90de3ca18f571de56a3b032747ab1cd","v":1,"via":"run"} --- test/cmocka/build/test-graph.json | 34 +++ .../cmocka/canvas-clear-mix-null-host-linux.c | 32 +++ test/cmocka/test_canvas_clear_mix.c | 251 ++++++++++++++++++ .../tests.d/test_canvas_clear_mix.cmake | 1 + 4 files changed, 318 insertions(+) create mode 100644 test/cmocka/canvas-clear-mix-null-host-linux.c create mode 100644 test/cmocka/test_canvas_clear_mix.c create mode 100644 test/cmocka/tests.d/test_canvas_clear_mix.cmake diff --git a/test/cmocka/build/test-graph.json b/test/cmocka/build/test-graph.json index 7321935fd5..9938690001 100644 --- a/test/cmocka/build/test-graph.json +++ b/test/cmocka/build/test-graph.json @@ -1062,6 +1062,34 @@ } ] }, + "test_canvas_clear_mix": { + "sources": [ + "test/cmocka/test_canvas_clear_mix.c", + { + "path": "test/cmocka/canvas-clear-mix-null-host-linux.c", + "requires": "OS_LINUX" + } + ], + "libraries": [ + "OBS::libobs", + "OBS::caption", + "Uthash::Uthash", + "OBS::native-test-cmocka", + { + "name": "OBS::w32-pthreads", + "requires": "OS_WINDOWS" + } + ], + "header_libraries": [ + "SIMDe::SIMDe" + ], + "compile_options": [ + { + "name": "-DCANVAS_CLEAR_MIX_NULL_PROBE", + "requires": "OS_LINUX" + } + ] + }, "test_canvas_destroy_scene_refs": { "sources": [ "test/cmocka/test_canvas_destroy_scene_refs.c" @@ -22680,6 +22708,12 @@ "${target}" ] }, + "test_canvas_clear_mix": { + "target": "test_canvas_clear_mix", + "command": [ + "${target}" + ] + }, "test_canvas_destroy_scene_refs": { "target": "test_canvas_destroy_scene_refs", "command": [ diff --git a/test/cmocka/canvas-clear-mix-null-host-linux.c b/test/cmocka/canvas-clear-mix-null-host-linux.c new file mode 100644 index 0000000000..ca53684000 --- /dev/null +++ b/test/cmocka/canvas-clear-mix-null-host-linux.c @@ -0,0 +1,32 @@ +/* Isolate the existing NULL-canvas fault before any threads are started. */ +#include +#include +#include +#include +#include +#include +#include +#include +#include + +void canvas_clear_mix_null_probe(void (*clear)(obs_canvas_t *)); + +void canvas_clear_mix_null_probe(void (*clear)(obs_canvas_t *)) +{ + pid_t child = fork(); + assert_true(child >= 0); + if (child == 0) { + const struct rlimit no_core = {0, 0}; + if (setrlimit(RLIMIT_CORE, &no_core) != 0) + _exit(2); + signal(SIGSEGV, SIG_DFL); + signal(SIGBUS, SIG_DFL); + /* Characterized, not endorsed: NULL is outside the C valid domain. */ + clear(NULL); + _exit(0); + } + int status = 0; + assert_int_equal(waitpid(child, &status, 0), child); + assert_true(WIFSIGNALED(status)); + assert_true(WTERMSIG(status) == SIGSEGV || WTERMSIG(status) == SIGBUS); +} diff --git a/test/cmocka/test_canvas_clear_mix.c b/test/cmocka/test_canvas_clear_mix.c new file mode 100644 index 0000000000..8e48d4131c --- /dev/null +++ b/test/cmocka/test_canvas_clear_mix.c @@ -0,0 +1,251 @@ +/* C-source characterization of the private clear-mix seam. The recording + * teardown boundary checks ordering and frees empty mixes; it does not model + * GPU/video teardown. No OBS startup, graphics device or video thread. + * External mode is reserved for the later swap's unchanged assertions. */ +#include +#include +#include +#include +#include +#include +#include "libobs/build/libobs.pch.h" + +static struct obs_core core; +static struct obs_core *current_core; +static struct obs_canvas canvas; +static struct obs_weak_canvas control; +static struct obs_source source; +static struct obs_weak_source source_control; +static struct obs_core_video_mix *expected_mix; +static size_t expected_count; +static unsigned locks, unlocks, frees; +static bool locked, retain_mix; + +static int record_lock(pthread_mutex_t *mutex) +{ + assert_ptr_equal(mutex, &core.video.mixes_mutex); + assert_false(locked); + assert_ptr_equal(canvas.mix, expected_mix); + int result = pthread_mutex_lock(mutex); + assert_int_equal(result, 0); + locked = true; + locks++; + return result; +} + +static int record_unlock(pthread_mutex_t *mutex) +{ + assert_true(locked); + assert_ptr_equal(mutex, &core.video.mixes_mutex); + /* The owner pointer is cleared only after the list lock is released. */ + assert_ptr_equal(canvas.mix, expected_mix); + int result = pthread_mutex_unlock(mutex); + assert_int_equal(result, 0); + locked = false; + unlocks++; + return result; +} + +static void record_free(struct obs_core_video_mix *mix) +{ + assert_ptr_equal(mix, expected_mix); + assert_true(locked); + assert_int_equal(pthread_mutex_trylock(&core.video.mixes_mutex), EBUSY); + assert_int_equal(core.video.mixes.num, expected_count); + assert_ptr_equal(canvas.mix, mix); + assert_null(mix->video); + frees++; + if (!retain_mix) + bfree(mix); +} + +#ifndef CANVAS_CLEAR_MIX_EXTERNAL +#define obs current_core +#define obs_free_video_mix record_free +#pragma push_macro("pthread_mutex_lock") +#pragma push_macro("pthread_mutex_unlock") +#undef pthread_mutex_lock +#undef pthread_mutex_unlock +#define pthread_mutex_lock record_lock +#define pthread_mutex_unlock record_unlock +#include "libobs/obs-canvas-clear-mix.c.h" +#pragma pop_macro("pthread_mutex_unlock") +#pragma pop_macro("pthread_mutex_lock") +#undef obs_free_video_mix +#undef obs +#endif + +static int setup(void **unused) +{ + (void)unused; + memset(&core, 0, sizeof(core)); + memset(&canvas, 0, sizeof(canvas)); + memset(&control, 0, sizeof(control)); + memset(&source, 0, sizeof(source)); + memset(&source_control, 0, sizeof(source_control)); + control.canvas = &canvas; + control.ref.refs = 17; + control.ref.weak_refs = 23; + canvas.context.control = (struct obs_weak_object *)&control; + source_control.source = &source; + source_control.ref.refs = 31; + source_control.ref.weak_refs = 47; + source.context.control = (struct obs_weak_object *)&source_control; + canvas.sources = &source; + canvas.view.channels[0] = &source; + canvas.flags = UINT32_MAX; + current_core = &core; + expected_mix = NULL; + expected_count = 0; + locks = unlocks = frees = 0; + locked = retain_mix = false; + assert_int_equal(pthread_mutex_init(&core.video.mixes_mutex, NULL), 0); + return 0; +} + +static void check_clear(void) +{ + struct obs_canvas before = canvas; + struct obs_weak_canvas refs_before = control; + struct obs_weak_source source_refs_before = source_control; + struct obs_source source_before = source; + obs_canvas_clear_mix(&canvas); + before.mix = NULL; + assert_memory_equal(&canvas, &before, sizeof(canvas)); + assert_memory_equal(&control, &refs_before, sizeof(control)); + assert_memory_equal(&source_control, &source_refs_before, sizeof(source_control)); + assert_memory_equal(&source, &source_before, sizeof(source)); + assert_false(locked); + assert_int_equal(pthread_mutex_trylock(&core.video.mixes_mutex), 0); + assert_int_equal(pthread_mutex_unlock(&core.video.mixes_mutex), 0); +} + +static int teardown(void **unused) +{ + (void)unused; + da_free(core.video.mixes); + assert_int_equal(pthread_mutex_destroy(&core.video.mixes_mutex), 0); + return 0; +} + +static void no_mix(void **unused) +{ + (void)unused; + /* The early return does not even read the core or acquire its mutex. */ + current_core = NULL; + check_clear(); + current_core = &core; + assert_int_equal(pthread_mutex_lock(&core.video.mixes_mutex), 0); + obs_canvas_clear_mix(&canvas); + assert_int_equal(pthread_mutex_unlock(&core.video.mixes_mutex), 0); + assert_int_equal(locks + unlocks + frees, 0); +} + +static void list_case(size_t count, size_t position) +{ + struct obs_core_video_mix *mixes[4]; + for (size_t i = 0; i < count; i++) { + mixes[i] = bzalloc(sizeof(*mixes[i])); + assert_non_null(mixes[i]); + da_push_back(core.video.mixes, &mixes[i]); + } + canvas.mix = expected_mix = mixes[position]; + expected_count = count - 1; + long allocations = bnum_allocs(); + check_clear(); + assert_int_equal(bnum_allocs(), allocations - 1); + assert_int_equal(locks, 1); + assert_int_equal(unlocks, 1); + assert_int_equal(frees, 1); + assert_int_equal(core.video.mixes.num, count - 1); + size_t index = 0; + for (size_t i = 0; i < count; i++) { + if (i == position) + continue; + assert_ptr_equal(core.video.mixes.array[index++], mixes[i]); + bfree(mixes[i]); + } + /* A second clear neither locks nor frees again. */ + check_clear(); + assert_int_equal(locks, 1); + assert_int_equal(frees, 1); + da_free(core.video.mixes); +} + +static void one_mix(void **unused) +{ + (void)unused; + list_case(1, 0); +} + +static void several_mixes(void **unused) +{ + (void)unused; + for (size_t position = 0; position < 4; position++) { + locks = unlocks = frees = 0; + list_case(4, position); + } +} + +static void unregistered_mix(void **unused) +{ + (void)unused; + struct obs_core_video_mix *other = bzalloc(sizeof(*other)); + struct obs_core_video_mix *orphan = bzalloc(sizeof(*orphan)); + da_push_back(core.video.mixes, &other); + canvas.mix = expected_mix = orphan; + long allocations = bnum_allocs(); + /* Characterized, not endorsed: an unregistered mix is detached, not freed. */ + check_clear(); + assert_int_equal(bnum_allocs(), allocations); + assert_int_equal(core.video.mixes.num, 1); + assert_ptr_equal(core.video.mixes.array[0], other); + assert_int_equal(locks, 1); + assert_int_equal(unlocks, 1); + assert_int_equal(frees, 0); + bfree(orphan); + bfree(other); +} + +static void first_match_only(void **unused) +{ + (void)unused; + struct obs_core_video_mix *mix = bzalloc(sizeof(*mix)); + da_push_back(core.video.mixes, &mix); + da_push_back(core.video.mixes, &mix); + canvas.mix = expected_mix = mix; + expected_count = 1; + /* Keep the allocation alive to inspect the duplicate pointer safely. + * Characterized, not endorsed: the production list must not contain duplicates. */ + retain_mix = true; + check_clear(); + assert_int_equal(frees, 1); + assert_int_equal(core.video.mixes.num, 1); + assert_ptr_equal(core.video.mixes.array[0], mix); + bfree(mix); +} + +#ifdef CANVAS_CLEAR_MIX_NULL_PROBE +extern void canvas_clear_mix_null_probe(void (*clear)(obs_canvas_t *)); + +static void null_canvas(void **unused) +{ + (void)unused; + canvas_clear_mix_null_probe(obs_canvas_clear_mix); +} +#endif + +int main(void) +{ + const struct CMUnitTest tests[] = { +#ifdef CANVAS_CLEAR_MIX_NULL_PROBE + cmocka_unit_test(null_canvas), +#endif + cmocka_unit_test_setup_teardown(no_mix, setup, teardown), + cmocka_unit_test_setup_teardown(one_mix, setup, teardown), + cmocka_unit_test_setup_teardown(several_mixes, setup, teardown), + cmocka_unit_test_setup_teardown(unregistered_mix, setup, teardown), + cmocka_unit_test_setup_teardown(first_match_only, setup, teardown), + }; + return cmocka_run_group_tests(tests, NULL, NULL); +} diff --git a/test/cmocka/tests.d/test_canvas_clear_mix.cmake b/test/cmocka/tests.d/test_canvas_clear_mix.cmake new file mode 100644 index 0000000000..6f6c250d83 --- /dev/null +++ b/test/cmocka/tests.d/test_canvas_clear_mix.cmake @@ -0,0 +1 @@ +# Registered once in the canonical native graph: build/test-graph.json. From 529d4fe42822f2c9377e1708e28eac59846f4878 Mon Sep 17 00:00:00 2001 From: Zach Vorhies Date: Thu, 19 Nov 2026 19:08:46 -0800 Subject: [PATCH 2/2] libobs: extract canvas mix clearing verbatim (Refs #5324) Ci-Attestation: {"at":1795146597,"gate":"data/all/compatibility","host":"linux-x86_64","key":"","lane":"client-checks","parents":["d78cfde1f567b32c1cc52d51dd1fc90e333b9781"],"secs":559,"stamp":"66fc44df2def2ec9b5014efcd4dacf88","tree":"ab35871bf3b5c8715c61745538d5324e6872cf40","v":1,"via":"run"} Ci-Attestation: {"at":1795146597,"gate":"data/all/flatpak-manifest","host":"linux-x86_64","key":"","lane":"client-checks","parents":["d78cfde1f567b32c1cc52d51dd1fc90e333b9781"],"secs":559,"stamp":"3063c245efce6f6a9f991081436ee44a","tree":"ab35871bf3b5c8715c61745538d5324e6872cf40","v":1,"via":"run"} Ci-Attestation: {"at":1795146597,"gate":"data/all/qt-xml","host":"linux-x86_64","key":"","lane":"client-checks","parents":["d78cfde1f567b32c1cc52d51dd1fc90e333b9781"],"secs":559,"stamp":"da760456955818b97f30e8d2be31e0ff","tree":"ab35871bf3b5c8715c61745538d5324e6872cf40","v":1,"via":"run"} Ci-Attestation: {"at":1795146597,"gate":"data/all/services-schema","host":"linux-x86_64","key":"","lane":"client-checks","parents":["d78cfde1f567b32c1cc52d51dd1fc90e333b9781"],"secs":559,"stamp":"b4da15c2040b6b1d74a2c1867fb99359","tree":"ab35871bf3b5c8715c61745538d5324e6872cf40","v":1,"via":"run"} Ci-Attestation: {"at":1795146034,"gate":"general/all/client-lint","host":"linux-x86_64","key":"f01ba8e83abab56028239dae769649d2dfa3cc559a5133d44c615cb5afb5d526","lane":"client-lint","parents":["d78cfde1f567b32c1cc52d51dd1fc90e333b9781"],"secs":36,"stamp":"feae2f55fb6666d66b03fa613aa7fd2b","tree":"ab35871bf3b5c8715c61745538d5324e6872cf40","v":1,"via":"run"} Ci-Attestation: {"at":1795146597,"gate":"python/all/self-test","host":"linux-x86_64","key":"","lane":"client-checks","parents":["d78cfde1f567b32c1cc52d51dd1fc90e333b9781"],"secs":559,"stamp":"6fead04a18f07763138a2ca912b04010","tree":"ab35871bf3b5c8715c61745538d5324e6872cf40","v":1,"via":"run"} Ci-Attestation: {"at":1795146036,"gate":"rust/aarch64-apple-darwin/client-clippy","host":"linux-x86_64","key":"f01ba8e83abab56028239dae769649d2dfa3cc559a5133d44c615cb5afb5d526","lane":"client-lint","parents":["d78cfde1f567b32c1cc52d51dd1fc90e333b9781"],"secs":36,"stamp":"7b0f3a020d5a7bf8ab5fcc30f3f36e6a","tree":"ab35871bf3b5c8715c61745538d5324e6872cf40","v":1,"via":"run"} Ci-Attestation: {"at":1795146036,"gate":"rust/aarch64-apple-darwin/client-dylint","host":"linux-x86_64","key":"f01ba8e83abab56028239dae769649d2dfa3cc559a5133d44c615cb5afb5d526","lane":"client-lint","parents":["d78cfde1f567b32c1cc52d51dd1fc90e333b9781"],"secs":36,"stamp":"5e2b0aea40ecd145ba76d92a2de9c797","tree":"ab35871bf3b5c8715c61745538d5324e6872cf40","v":1,"via":"run"} Ci-Attestation: {"at":1795146035,"gate":"rust/aarch64-pc-windows-msvc/client-clippy","host":"linux-x86_64","key":"f01ba8e83abab56028239dae769649d2dfa3cc559a5133d44c615cb5afb5d526","lane":"client-lint","parents":["d78cfde1f567b32c1cc52d51dd1fc90e333b9781"],"secs":36,"stamp":"935e539e296ad6fec5856083c54901ae","tree":"ab35871bf3b5c8715c61745538d5324e6872cf40","v":1,"via":"run"} Ci-Attestation: {"at":1795146035,"gate":"rust/aarch64-pc-windows-msvc/client-dylint","host":"linux-x86_64","key":"f01ba8e83abab56028239dae769649d2dfa3cc559a5133d44c615cb5afb5d526","lane":"client-lint","parents":["d78cfde1f567b32c1cc52d51dd1fc90e333b9781"],"secs":36,"stamp":"0a05f6fc242c811305f43e7f2c47b512","tree":"ab35871bf3b5c8715c61745538d5324e6872cf40","v":1,"via":"run"} Ci-Attestation: {"at":1795146034,"gate":"rust/aarch64-unknown-linux-gnu/client-clippy","host":"linux-x86_64","key":"f01ba8e83abab56028239dae769649d2dfa3cc559a5133d44c615cb5afb5d526","lane":"client-lint","parents":["d78cfde1f567b32c1cc52d51dd1fc90e333b9781"],"secs":36,"stamp":"cb4b82959c7c208f6542fff0c6e0bdb1","tree":"ab35871bf3b5c8715c61745538d5324e6872cf40","v":1,"via":"run"} Ci-Attestation: {"at":1795146035,"gate":"rust/aarch64-unknown-linux-gnu/client-dylint","host":"linux-x86_64","key":"f01ba8e83abab56028239dae769649d2dfa3cc559a5133d44c615cb5afb5d526","lane":"client-lint","parents":["d78cfde1f567b32c1cc52d51dd1fc90e333b9781"],"secs":36,"stamp":"a269465355e4ba14e8c9735932aaba11","tree":"ab35871bf3b5c8715c61745538d5324e6872cf40","v":1,"via":"run"} Ci-Attestation: {"at":1795146035,"gate":"rust/x86_64-apple-darwin/client-clippy","host":"linux-x86_64","key":"f01ba8e83abab56028239dae769649d2dfa3cc559a5133d44c615cb5afb5d526","lane":"client-lint","parents":["d78cfde1f567b32c1cc52d51dd1fc90e333b9781"],"secs":36,"stamp":"c7d2ec5c88ca7054ebb79373c66fa214","tree":"ab35871bf3b5c8715c61745538d5324e6872cf40","v":1,"via":"run"} Ci-Attestation: {"at":1795146035,"gate":"rust/x86_64-apple-darwin/client-dylint","host":"linux-x86_64","key":"f01ba8e83abab56028239dae769649d2dfa3cc559a5133d44c615cb5afb5d526","lane":"client-lint","parents":["d78cfde1f567b32c1cc52d51dd1fc90e333b9781"],"secs":36,"stamp":"34cab53cbe5c26411bdfbb3c3453e41d","tree":"ab35871bf3b5c8715c61745538d5324e6872cf40","v":1,"via":"run"} Ci-Attestation: {"at":1795146035,"gate":"rust/x86_64-pc-windows-msvc/client-clippy","host":"linux-x86_64","key":"f01ba8e83abab56028239dae769649d2dfa3cc559a5133d44c615cb5afb5d526","lane":"client-lint","parents":["d78cfde1f567b32c1cc52d51dd1fc90e333b9781"],"secs":36,"stamp":"b3cb808d5030d62853ad2230970210e7","tree":"ab35871bf3b5c8715c61745538d5324e6872cf40","v":1,"via":"run"} Ci-Attestation: {"at":1795146035,"gate":"rust/x86_64-pc-windows-msvc/client-dylint","host":"linux-x86_64","key":"f01ba8e83abab56028239dae769649d2dfa3cc559a5133d44c615cb5afb5d526","lane":"client-lint","parents":["d78cfde1f567b32c1cc52d51dd1fc90e333b9781"],"secs":36,"stamp":"51e1660461bcaf31240b7e8638b879dc","tree":"ab35871bf3b5c8715c61745538d5324e6872cf40","v":1,"via":"run"} Ci-Attestation: {"at":1795146034,"gate":"rust/x86_64-unknown-linux-gnu/client-clippy","host":"linux-x86_64","key":"f01ba8e83abab56028239dae769649d2dfa3cc559a5133d44c615cb5afb5d526","lane":"client-lint","parents":["d78cfde1f567b32c1cc52d51dd1fc90e333b9781"],"secs":36,"stamp":"3cbe2fe8569e07c447f7d1032bf5a4a5","tree":"ab35871bf3b5c8715c61745538d5324e6872cf40","v":1,"via":"run"} Ci-Attestation: {"at":1795146034,"gate":"rust/x86_64-unknown-linux-gnu/client-dylint","host":"linux-x86_64","key":"f01ba8e83abab56028239dae769649d2dfa3cc559a5133d44c615cb5afb5d526","lane":"client-lint","parents":["d78cfde1f567b32c1cc52d51dd1fc90e333b9781"],"secs":36,"stamp":"b8bdafc3c2356075f72a6e36a034d273","tree":"ab35871bf3b5c8715c61745538d5324e6872cf40","v":1,"via":"run"} --- libobs/build/libobs.core-source+.h.c | 1 + libobs/obs-canvas-clear-mix.c.h | 38 ++++++++++++++++++++++++++++ libobs/obs-canvas.c.h | 20 --------------- test/cmocka/test_canvas_clear_mix.c | 31 ++++++++++++++++++----- 4 files changed, 64 insertions(+), 26 deletions(-) create mode 100644 libobs/obs-canvas-clear-mix.c.h diff --git a/libobs/build/libobs.core-source+.h.c b/libobs/build/libobs.core-source+.h.c index d5b941beb5..3036116360 100644 --- a/libobs/build/libobs.core-source+.h.c +++ b/libobs/build/libobs.core-source+.h.c @@ -3,6 +3,7 @@ #include "libobs/build/libobs.pch.h" #include "libobs/obs-canvas-channel-fields.c.h" +#include "libobs/obs-canvas-clear-mix.c.h" #include "libobs/obs-canvas-flags-fields.c.h" #include "libobs/obs-canvas-public-constructors.c.h" #include "libobs/obs-canvas-readonly-fields.c.h" diff --git a/libobs/obs-canvas-clear-mix.c.h b/libobs/obs-canvas-clear-mix.c.h new file mode 100644 index 0000000000..fbefde2d97 --- /dev/null +++ b/libobs/obs-canvas-clear-mix.c.h @@ -0,0 +1,38 @@ +/****************************************************************************** + Copyright (C) 2025 by Dennis Sädtler + + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 2 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see . +******************************************************************************/ + +#include "libobs/build/libobs.pch.h" + +/* Free canvas mix (if any) */ +void obs_canvas_clear_mix(obs_canvas_t *canvas) +{ + if (!canvas->mix) + return; + + pthread_mutex_lock(&obs->video.mixes_mutex); + for (size_t i = 0; i < obs->video.mixes.num; i++) { + struct obs_core_video_mix *mix = obs->video.mixes.array[i]; + if (mix == canvas->mix) { + da_erase(obs->video.mixes, i); + obs_free_video_mix(mix); + break; + } + } + pthread_mutex_unlock(&obs->video.mixes_mutex); + + canvas->mix = NULL; +} diff --git a/libobs/obs-canvas.c.h b/libobs/obs-canvas.c.h index 2b8a7d9851..f87b52ac52 100644 --- a/libobs/obs-canvas.c.h +++ b/libobs/obs-canvas.c.h @@ -160,26 +160,6 @@ void obs_canvas_destroy(obs_canvas_t *canvas) /*** Internal API ***/ -/* Free canvas mix (if any) */ -void obs_canvas_clear_mix(obs_canvas_t *canvas) -{ - if (!canvas->mix) - return; - - pthread_mutex_lock(&obs->video.mixes_mutex); - for (size_t i = 0; i < obs->video.mixes.num; i++) { - struct obs_core_video_mix *mix = obs->video.mixes.array[i]; - if (mix == canvas->mix) { - da_erase(obs->video.mixes, i); - obs_free_video_mix(mix); - break; - } - } - pthread_mutex_unlock(&obs->video.mixes_mutex); - - canvas->mix = NULL; -} - /* Clear mixes attached to canvases */ void obs_free_canvas_mixes(void) { diff --git a/test/cmocka/test_canvas_clear_mix.c b/test/cmocka/test_canvas_clear_mix.c index 8e48d4131c..b617b6c75d 100644 --- a/test/cmocka/test_canvas_clear_mix.c +++ b/test/cmocka/test_canvas_clear_mix.c @@ -17,6 +17,7 @@ static struct obs_weak_canvas control; static struct obs_source source; static struct obs_weak_source source_control; static struct obs_core_video_mix *expected_mix; +static unsigned char expected_mix_bytes[sizeof(expected_mix)]; static size_t expected_count; static unsigned locks, unlocks, frees; static bool locked, retain_mix; @@ -37,8 +38,9 @@ static int record_unlock(pthread_mutex_t *mutex) { assert_true(locked); assert_ptr_equal(mutex, &core.video.mixes_mutex); - /* The owner pointer is cleared only after the list lock is released. */ - assert_ptr_equal(canvas.mix, expected_mix); + /* The owner pointer is cleared only after unlocking. Compare its bytes: + * the pointed-to allocation may already have been freed. */ + assert_memory_equal(&canvas.mix, expected_mix_bytes, sizeof(canvas.mix)); int result = pthread_mutex_unlock(mutex); assert_int_equal(result, 0); locked = false; @@ -105,10 +107,15 @@ static int setup(void **unused) static void check_clear(void) { - struct obs_canvas before = canvas; - struct obs_weak_canvas refs_before = control; - struct obs_weak_source source_refs_before = source_control; - struct obs_source source_before = source; + struct obs_canvas before; + struct obs_weak_canvas refs_before; + struct obs_weak_source source_refs_before; + struct obs_source source_before; + memcpy(&before, &canvas, sizeof(before)); + memcpy(&refs_before, &control, sizeof(refs_before)); + memcpy(&source_refs_before, &source_control, sizeof(source_refs_before)); + memcpy(&source_before, &source, sizeof(source_before)); + memcpy(expected_mix_bytes, &canvas.mix, sizeof(canvas.mix)); obs_canvas_clear_mix(&canvas); before.mix = NULL; assert_memory_equal(&canvas, &before, sizeof(canvas)); @@ -135,10 +142,22 @@ static void no_mix(void **unused) current_core = NULL; check_clear(); current_core = &core; + struct obs_core_video_mix *other = bzalloc(sizeof(*other)); + da_push_back(core.video.mixes, &other); + long allocations = bnum_allocs(); + check_clear(); + assert_int_equal(core.video.mixes.num, 1); + assert_ptr_equal(core.video.mixes.array[0], other); + assert_int_equal(bnum_allocs(), allocations); assert_int_equal(pthread_mutex_lock(&core.video.mixes_mutex), 0); + /* Make an unexpected recursive lock fail in the recorder before it blocks. */ + locked = true; obs_canvas_clear_mix(&canvas); + assert_true(locked); + locked = false; assert_int_equal(pthread_mutex_unlock(&core.video.mixes_mutex), 0); assert_int_equal(locks + unlocks + frees, 0); + bfree(other); } static void list_case(size_t count, size_t position)