Repository navigation
util: Port the platform string conversions to obs-util - #1874
Merged
Merged
Conversation
rust/obs-util/src/platform_conversions.rs holds the safe core for
libobs/util/platform-conversions.c with todo!() bodies: os_utf8_to_wcs,
os_wcs_to_utf8, os_mbs_to_wcs, os_wcs_to_mbs, their _ptr forms,
os_utf8_to_mbs_ptr and os_mbs_to_utf8_ptr. ffi/platform_conversions.rs
is the C ABI shim with the same exported signatures: it turns
(str, len) and (dst, dst_size) into slices and gives the _ptr forms a
bmalloc allocator. The conversions stay the platform's, as in C:
utf8_to_wchar / wchar_to_utf8 and the C library's mbstowcs / wcstombs,
through new platform::{utf8_to_wide, wide_to_utf8, mbs_to_wide,
wide_to_mbs} on Unix and Windows.
- Tier 1 (tests/platform/platform_conversions.rs): 1:1 port of the 18
cases of test_platform_conversions.c. The UTF-8 locale cases and the
bnum_allocs checks run in a child of the test binary. New cases: the
_ptr forms' size-plus-one buffer, and text exactly dst_size wide
characters long, which must give 0 and an empty string without a
write past dst_size.
- ENABLE_RUST_LIBOBS=ON drops util/platform-conversions.c: it moves
into libobs.ported-platform, and libobs.platform, now empty, is
removed. Exports in rust-exports.d/obs-util-platform_conversions.txt.
- Oracle: the plain-named Windows forwarders in platform_host.c go,
since the Rust shims now own those names, and oracle/platform_windows.c
calls the oracle_ conversions instead.
All 20 Tier 1 cases fail on todo!(); the ones that reach the shim abort
the child when the panic crosses the extern "C" boundary.
Refs obs-rust#262
The safe core follows platform-conversions.c: a NULL dst is a size
query that ignores dst_size; an empty dst is left alone; otherwise the
conversion gets dst_size - 1 elements and the NUL goes at its result.
The UTF-8 conversions give 0 and an empty string for text that is
invalid or does not fit, the mbs ones cut it to fit and treat
(size_t)-1 as 0. The _ptr forms allocate the size query plus one, and
os_utf8_to_mbs_ptr / os_mbs_to_utf8_ptr chain them as in C, with the
wide string in between in a Vec rather than bmalloc'd.
Intentional difference: C's os_utf8_to_wcs gives utf8_to_wchar a
capacity of dst_size, not dst_size - 1, so text exactly dst_size wide
characters long converts in full and its NUL lands at dst[dst_size].
C writes one element past the buffer; Rust doesn't. The capacity here is
dst_size - 1, as os_wcs_to_utf8 has it, so that text gives 0 and an
empty string like any other text that does not fit.
platform_file's os_fread_mbs and os_quick_write_mbs_file now call the
ported os_mbs_to_utf8_ptr / os_utf8_to_mbs_ptr instead of the private
copies slice 3 kept in platform/{unix,windows}/file.rs, which go.
Tier 1 tests::platform::platform_conversions: 20/20. obs-util lib
harness: 791/791.
Refs obs-rust#262
oracle/platform.c already compiles platform-conversions.c as oracle_*; src/platform_conversions.rs declares those functions for Rust. platform_conversions_parity.rs calls the Rust shim and the C with the same input and compares the results and every element of buffers that are two elements longer than the dst_size both are given: - os_utf8_to_wcs over arbitrary bytes and pieces of valid and invalid UTF-8 (overlong, surrogate, above U+10FFFF, 5- and 6-byte, truncated, BOM, embedded NUL), every len, a NULL dst with a random dst_size, and every dst_size from 0 to two past the length the text needs; - os_wcs_to_utf8 over wide units of every range, surrogates and negative wchar_t included, the same way; - os_mbs_to_wcs / os_wcs_to_mbs in the C locale, and in a UTF-8 locale in a child of the test binary; - the _ptr forms (results and strings), and their bnum_allocs on both allocators in a child. The intentional difference is excluded exactly: for every dst_size of 2 or more, the C must match utf8_to_wchar (the original) into dst_size units plus the NUL, the Rust the same into dst_size - 1, and the two must agree wherever those agree. They differ for text exactly dst_size long (the C returns dst_size and writes dst[dst_size]) and for longer text whose partial conversion reaches dst[dst_size - 1] after the NUL in C. The Rust writes nothing past dst_size for any input. 8/8 parity tests. Mutation check, each mutant reverted: - os_wcs_to_utf8 capacity dst_size - 2: 4 tests fail; - os_utf8_to_wcs skips dst_size 2: 5; - os_mbs_to_wcs keeps (size_t)-1 as dst_size - 1: 2; - os_wcs_to_mbs size query gives 1 for (size_t)-1: 4; - os_mbs_to_utf8_ptr drops the last unit: 3; - os_utf8_to_wcs_ptr allocates the size query without the NUL: 3; - os_wcs_to_utf8 leaves dst alone for 8 units or more: 4. Refs obs-rust#262
rust/README.md gets the platform-conversions.c row: the characterized behavior kept, and the intentional difference. C writes one element past the buffer; Rust doesn't. C's os_utf8_to_wcs gives utf8_to_wchar a capacity of dst_size rather than dst_size - 1, so text exactly dst_size wide characters long converts in full and its NUL is written at dst[dst_size]; Rust gives dst_size - 1, as os_wcs_to_utf8 does, so that text gives 0 and an empty string. The core's module docs say the same. port_comparison gets a conversions category (record.py knows it): os_utf8_to_wcs and os_wcs_to_utf8 into a buffer at 64 and 4096 bytes of mixed UTF-8, os_utf8_to_wcs_ptr at 64 (allocation and free included), and os_utf8_to_mbs_ptr on an ASCII file name in the C locale. record.py, 5 passes, --cpu 7, on a loaded machine (load average about 24): utf8_to_wcs/64 355.0 ns 126.1 ns 2.82 utf8_to_wcs/4096 20952.0 ns 7659.5 ns 2.74 wcs_to_utf8/64 216.8 ns 140.3 ns 1.55 wcs_to_utf8/4096 6911.9 ns 3657.7 ns 1.89 utf8_to_wcs_ptr/64 271.9 ns 141.6 ns 1.92 utf8_to_mbs_ptr/ascii 335.4 ns 187.9 ns 1.79 The gap is the obs-util utf8_to_wchar decoder this port calls, not the wrapper: utf8_to_wchar alone on the 4096-byte text is 11.7 us against the C's 3.9 us. Refs obs-rust#262
rdoupe
force-pushed
the
rust/port-platform-conversions
branch
from
October 23, 2026 03:35
e87958f to
d410a79
Compare
6 of 7 tasks
10 tasks done
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Refs #262 (slice 1, conversions). Unit 1 of #1846.
What and why
#1610 moved
os_utf8_to_wcs,os_wcs_to_utf8,os_mbs_to_wcs,os_wcs_to_mbs, their_ptrforms,os_utf8_to_mbs_ptrandos_mbs_to_utf8_ptrverbatim intolibobs/util/platform-conversions.cand pinned them with the 18 cases oftest_platform_conversions. This PR ports them toobs-util::platform_conversions, and hybridlibobsdropsutil/platform-conversions.c.rust/obs-util/src/platform_conversions.rs): the buffer handling of the C, step for step. Adstis a slice:Noneis the NULL-dst size query (which ignoresdst_size), an empty one is left untouched, and otherwise the conversion getsdst_size - 1elements and the NUL goes at its result. The UTF-8 conversions give 0 and an empty string for text that is invalid or does not fit; the mbs ones cut it to fit and treat(size_t)-1as 0. The_ptrforms take their allocator as a closure (the shim's isbmalloc): one allocation of the size query plus one.os_utf8_to_mbs_ptrandos_mbs_to_utf8_ptrchain them as in C.utf8_to_wchar/wchar_to_utf8(libobs's decoder off Windows,MultiByteToWideChar/WideCharToMultiByteon Windows) and the C library'smbstowcs/wcstombsin the currentLC_CTYPE, through newplatform::{utf8_to_wide, wide_to_utf8, mbs_to_wide, wide_to_mbs}on Unix and Windows.src/ffi/platform_conversions.rs): the ten functions with their exported signatures;(str, len)and(dst, dst_size)become slices, NULLstrreturns 0 and sets*pstrto NULL in the_ptrforms. Exports inrust-exports.d/obs-util-platform_conversions.txt.util/platform-conversions.cmoves intolibobs.ported-platform;libobs.platform, now empty, is removed fromsource-sets.json. The C reference still compiles it.os_fread_mbsandos_quick_write_mbs_filenow call the ported conversions instead of the private copies slice 3 kept inplatform/{unix,windows}/file.rs, which are removed.oracle/platform_host.care removed, since the Rust shims own those names now.oracle/platform_windows.ccalls theoracle_conversions, so the oracle still runs the original C all the way down. So are the two Unix forwarders util: Port dstr-libc to obs-util #1857 added foros_utf8_to_wcs_ptrandos_mbs_to_utf8_ptr: the dstr-libc shims call those plain names, which now resolve to the obs-util shims in libobs and undercargo testalike. Theirci/test_oracle_standins.pyentries are removed too.test_platform_conversionsis unchanged and passes): the mbs functions ignorelen(soos_mbs_to_utf8_ptrdoes too); a dst too small for the UTF-8 conversions gives 0 and an empty string, where the mbs ones cut the text; the off-Windows size query accepts an encoded surrogate that the conversion then refuses; the decoder's overlong and above-U+10FFFF forms.rust/README.mdrow): C writes one element past the buffer; Rust doesn't.os_utf8_to_wcsin C passesout_len + 1(dst_size) as the capacity toutf8_to_wchar, so text exactlydst_sizewide characters long converts in full anddst[dst_size] = 0is written one past the buffer.os_wcs_to_utf8passesdst_size - 1and does not have this. The Rust passesdst_size - 1on both sides, so that text gives 0 and an empty string, asos_wcs_to_utf8does for text exactlydst_sizebytes long. The same capacity lets the C's failed conversion of longer text leave one more unit of partial text after the NUL, atdst[dst_size - 1]; both return 0 and an empty string there.os_utf8_to_mbs_ptr/os_mbs_to_utf8_ptris a RustVec, where the Cbmallocs and frees it.bnum_allocsis the same after each call (checked in Tier 1 and Tier 3).The exact-size case is not added to
test_platform_conversions: the same binary runs againstlibobs-c-referenceand the C backend, where the C writes past the buffer, so a Tier 2 case for the fixed behavior would fail there. It is pinned in Tier 1 and Tier 3.RED → GREEN
util: Test the platform string conversions against a Rust stub. Tier 1 (tests/platform/platform_conversions.rs) is a 1:1 port of the 18 cmocka cases, with the UTF-8 locale cases and thebnum_allocschecks in a child of the test binary, plus two new ones: the_ptrsize-plus-one buffer, andutf8_to_wcs_exact_size_stays_in_dst. All 20 fail ontodo!().util: Port the platform string conversions to obs-util. Tier 1 20/20;soldr cargo test -p obs-util791/791 at GREEN, 815/815 at the head (with Tier 3).util: Compare the platform string conversions with the C(platform_conversions_parity.rs, 8 tests). The Rust shim and the C get the same input; results and every element of buffers two elements longer thandst_sizeare compared:os_utf8_to_wcsover arbitrary bytes and pieces of valid and invalid UTF-8 (overlong, surrogate, above U+10FFFF, 5- and 6-byte, truncated, BOM, embedded NUL), everylen, a NULL dst with a randomdst_size, and everydst_sizefrom 0 to two past the length the text needs;os_wcs_to_utf8over wide units of every range (surrogates and negativewchar_tincluded), the same way;os_mbs_to_wcs/os_wcs_to_mbsin the C locale, and in a UTF-8 locale in a child process;_ptrforms, and theirbnum_allocson both allocators in a child.dst_sizeof 2 or more, the C must match the originalutf8_to_wcharintodst_sizeunits plus the NUL, the Rust the same intodst_size - 1, and the two must agree wherever those agree. For the exact-size inputs the oracle gets a padded buffer, and the test checks that it wrote the NUL atdst[dst_size]and that the Rust wrote nothing pastdst_size. Invalid UTF-8 stops libobs's decoder before the capacity matters, but on WindowsMultiByteToWideCharturns it into U+FFFD, so there it can reach the capacity like valid text; the fixed case pins both.os_wcs_to_utf8capacitydst_size - 2: 4 parity tests;os_utf8_to_wcsskipsdst_size2: 5;os_mbs_to_wcskeeps(size_t)-1asdst_size - 1: 2;os_wcs_to_mbssize query gives 1 for(size_t)-1: 4;os_mbs_to_utf8_ptrdrops the last unit: 3;os_utf8_to_wcs_ptrallocates without room for the NUL: 3;os_wcs_to_utf8leaves dst alone for 8 units or more: 4.port_comparison: d410a79.OBS_LIBOBS_BACKEND=C, andHYBRIDwithOBS_BUILD_C_REFERENCE=ON), headless (private Xvfb, private D-Bus, fresh HOME):test_platform_conversionslibobslibobslibobs-c-referenceAfter the rebase onto #1857:
test_platform_conversions,test_dstr,test_dstr_libc,test_dstr_regressions,test_dstr_zero_len,test_platformandtest_platform_filepass on all three libraries, exports still IDENTICAL (1766), andsoldr cargo test -p obs-utilis 815/815, dstr-libc parity included. The full ctest numbers above are from before that rebase.No skips; a UTF-8 locale was installed, so the UTF-8 cases ran. In hybrid
libobs,os_utf8_to_wcsand the others are the Rust ones; the production link has nolibobs.platformorlibobs.ported-platformobject.ci/local_gate.py --lane lint(clang-format, gersemi, rustfmt, clippy-D warnings, Dylints, build groups, test-binary cap, ifdef ratchet): passed.--lane cross(Windows and macOS clippy/Dylints, and a link of every test binary forx86_64-pc-windows-msvcandaarch64-apple-darwin): passed.ci/test_build_groups.py,ci/test_ported_sources.py,ci/test_oracle_standins.py: passed.port_comparison(record.py, 5 passes,--cpu 7, Linux x86_64, GCC 16.2, rustc 1.98.1; the machine was under load, so treat these as rough):The gap is in the obs-util
utf8_to_wchardecoder that this port calls, not in the wrapper:utf8_to_wcharalone on the 4096-byte text takes 11.7 µs against the C's 3.9 µs. Speeding up that decoder is left for a follow-up.Windows/macOS: the
platform/windows/file.rsprimitives and the oracle change are compiled and linked by the cross lane but not run locally. PR CI's Windows and macOS lanes run them first.Checklist
master(or the description starts with a Stacked: note)docs/features.mdis added or updated (not applicable: no user-visible change)-W..., ratchets): built with scripting ON (PR CI builds no OBS app: rungh workflow run ci.yml --ref <branch>for the full builds) and cross-checked with clang and MSVC (not applicable: no flag changes)