Repository navigation
graphics: Make five math functions safe when dst overlaps an input - #423
Merged
zackees merged 2 commits intoOct 13, 2026
Merged
Conversation
zackees
approved these changes
Oct 13, 2026
added 2 commits
October 12, 2026 18:22
The five libobs/graphics math functions this branch repairs re-read an input after storing an output lane, so a dst overlapping an input depends on the overlap. Three commitments, all failing on master: - test/cmocka/test_graphics_alias_ordinary.c: runs bounds_move, bounds_scale, bounds_merge_point, bounds_get_center and plane_from_tri twice, once on separate storage and once on the arrangement, asserting the results are identical. 0 of 5 pass on master. - rust/obs-graphics/tests/bounds_parity.rs: the same four arrangements on the Rust shims, each comparing the aliased result with the non-aliased one on the Rust side, the C side and between the two. - rust/obs-graphics/tests/plane_parity.rs: the plane arrangement now also compares the aliased result against a call with a separate destination, which the C oracle on master does not satisfy (it returns dir·dir for the distance). The plane comparison is on dir's lanes and dist, not the struct, because struct plane has 12 bytes of tail padding.
…REEN) bounds_move, bounds_scale, bounds_merge_point and bounds_get_center read their vector operand, or b->min, again after writing dst->min, so a caller that passes a destination it also reads from gets a value that depends on the overlap. plane_from_tri reads v1 after writing dst->dir, so a v1 sharing storage with the destination yields a distance taken from the normalized direction instead of the vertex. Each fix is a snapshot of the input that the later read would otherwise re-read: the operand for the four bounds helpers, b->min for the center, and v1 for the plane. No expression, operand or order of operations changes, so non-overlapping results are bit-for-bit unchanged. The Rust port is aligned in the same commit. The bounds shims already read their inputs before writing, so they needed no change; plane_from_tri had copied the C bug on purpose - it detected `v1 == &dst->dir` and set the distance to dir·dir (obs-rust#338) - and that emulation is dropped, so the shim reads all three vertices like every other shim in the crate. The plane characterization test that pinned dir·dir ("Characterized, not endorsed") now asserts the distance of the original vertex and that the aliased call equals a call with a separate destination. GREEN: test_graphics_alias_ordinary 5 passed, 0 failed (exit 0), and the existing test_plane (17) and test_bounds (16) suites pass, in the same Release build as the RED commit's 0-passed/5-failed run. Local-Gate: v1 tree=e491b7e0aad43f87b98f9f54ca8b8692c5dbc748 secs=176 lanes=lint:run,rust-test:run,tier2-linux:run Ci-Attestation: {"at":1791854742,"gate":"rust/all/fmt","host":"linux-x86_64","key":"80aee80b302f28ff1fe2588d45fee172121d392fe232b8cd6d19ea0a88a01237","lane":"lint","parents":["6161f0666d81c3fd5bf9d8644a44039d58299a38"],"secs":39,"stamp":"c13a375d0f58e6abcbb9dd0201d059f0","tree":"e491b7e0aad43f87b98f9f54ca8b8692c5dbc748","v":1,"via":"run"} Ci-Attestation: {"at":1791854742,"gate":"rust/x86_64-unknown-linux-gnu/clippy","host":"linux-x86_64","key":"80aee80b302f28ff1fe2588d45fee172121d392fe232b8cd6d19ea0a88a01237","lane":"lint","parents":["6161f0666d81c3fd5bf9d8644a44039d58299a38"],"secs":39,"stamp":"5b1f1dbec523079acd2b0129aa059db5","tree":"e491b7e0aad43f87b98f9f54ca8b8692c5dbc748","v":1,"via":"run"} Ci-Attestation: {"at":1791854742,"gate":"rust/x86_64-unknown-linux-gnu/test","host":"linux-x86_64","key":"e0fc8277f27f5efe797856b0956c6e8cac844384103903d31f8fb9d4a48a4819","lane":"rust-test","parents":["6161f0666d81c3fd5bf9d8644a44039d58299a38"],"secs":64,"stamp":"f2fdd9558f3efd1fa3330dd7ce1fe7a9","tree":"e491b7e0aad43f87b98f9f54ca8b8692c5dbc748","v":1,"via":"run"} Ci-Attestation: {"at":1791854742,"gate":"cpp/linux-x64/tier2","host":"linux-x86_64","key":"2baf904178cd0711f9792e8a2e6792204bdf510ba3bf5ec4ce186d8a5f120758","lane":"tier2-linux","parents":["6161f0666d81c3fd5bf9d8644a44039d58299a38"],"secs":73,"stamp":"1ec76fb4a267a1895436e7a81eaeb3bc","tree":"e491b7e0aad43f87b98f9f54ca8b8692c5dbc748","v":1,"via":"run"}
Member
|
Verified locally (zccache bypassed for the Rust side):
|
zackees
force-pushed
the
fix/graphics-alias-ordinary
branch
from
October 13, 2026 01:25
392f913 to
5d5de3c
Compare
This was referenced Oct 14, 2026
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.
Part 1 of #370: the five ordinary same-type alias cases.
Refs #370
Problem
Five exported
libobs/graphicsmath functions store an output lane beforeevery input lane has been read, so a caller that passes a
dstoverlapping aninput gets a value that depends on the overlap rather than on the inputs:
bounds_movelibobs/graphics/bounds.c:24vfor the max corner after storingdst->minbounds_scalelibobs/graphics/bounds.c:34bounds_merge_pointlibobs/graphics/bounds.c:50bounds_get_centerlibobs/graphics/bounds.c:95b->minafter storingdstplane_from_trilibobs/graphics/plane.c:22v1after writing and normalizingdst->dirFor example,
bounds_move(&b, &b, &b.min)moves the max corner from thealready-moved minimum, and
plane_from_tri(&p, &p.dir, v2, v3)returns adistance taken from the normalized direction instead of the vertex. The audit
that found these measured each wrong result directly. The audit's source search
found no caller in
libobs, the frontend, thedata layer or the plugins that uses a failing arrangement - a grep, not a proof
across every possible pointer provenance - so this is latent rather than a live
defect. Upstream's own characterization tests for some ports do exercise
arrangements like these, so any whole-tree claim must exclude tests.
No header documents any restriction on the caller's pointers. The Rust port is
the guide: its graphics shims were written to read their inputs in full before
writing
dst, and theplane_from_trishim went further and deliberatelymatched the C bug (it detected
v1 == &dst->dirand set the distance todir·dir, #338) so that the port would agree with the C it was measuredagainst. That agreement is what the C fix and the shim change remove.
Root cause
Each of the five functions reads an input lane after a store to a lane the
input occupies, when the two share storage. Concretely, for
bounds_move:The first
vec3_addwritesdst->min; when the caller passedvat thataddress, the second line's read of
vobserves the value the first linewrote. The other four differ only in which lane is written first and which
input is read afterwards -
dst->minbeforevfor move/scale/merge-point,dstbeforeb->minfor the center, anddst->dirbeforev1for the plane,where the intervening
vec3_normmakes the difference larger.The copy that fixes it has to happen before the first store, which is what the
snapshot does; nothing about the computation itself is wrong.
Fix
Each fix is a snapshot of the input that a later read would otherwise re-read:
the vector operand for the three vector-taking
boundshelpers,b->minforbounds_get_center, andv1forplane_from_tri. No expression, operand ororder of operations changes, so non-overlapping results are bit-for-bit
unchanged - measured below rather than asserted.
The Rust side is aligned in the same commit. The
boundsshims already readtheir inputs before writing, so they needed no change;
plane_from_trideliberately matched the C bug (see above) and that emulation is dropped, so the
shim reads all three vertices like the other shims in the crate. The plane
characterization test that pinned
dir·diras "Characterized, not endorsed"now asserts the distance of the original vertex, and additionally that the
aliased call equals a call with a separate destination.
Alternatives considered, and why not:
bug stays reachable the moment a caller ignores the contract, and it is what
the Rust port already contradicts.
dstdoes not overlap an input: turns a wrong valueinto a crash in the same situations the wrong value appears, and still leaves
release builds computing the wrong answer.
restrictto the parameters: an overlapping caller becomes undefinedbehavior instead of a wrong value, which is worse, and it does not match the
Rust port either.
wrong results directly, and a fix this small has no reason to wait for a
caller to appear.
How this was tested
0a6cf0041is the new tests alone; commit392f91326is the fix. The RED commit fails on
masterand the GREEN commit passes,built with the same local flags: cmake Xcode,
Release(-O3 -std=c17),ENABLE_RUST_LIBOBS=OFF, no sanitizers, macOS arm64. The Rust suites wererun with
soldr cargo test -p obs-graphics --locked(debug and release).(selfhosted-ci.yml)
run lint (clang-format 22.1.3, gersemi), Rust Tiers 1–3 (unit, C-oracle
parity, proptest), and the unchanged cmocka suite with
ENABLE_RUST_LIBOBSOFF and ON (built natively on Debian 13), including export parity. Each run is
pinned to the exact commit in this PR:
https://github.com/chriscoveries/obsrust-fork/actions/runs/38308596686
GCC 14,
-Werror, ASan withASAN_OPTIONS=detect_leaks=1. The new cmockatest and the
obs-graphicsRust tests were both built and run there on theGREEN head - commands and results in the Evidence section below.
overlapping
dstand these are pure-math functions with no I/O, threading orplatform state - a real OBS run could not reach the changed path. The
correctness argument is the alias tests, and the unchanged-result argument is
the compiled differential described below.
Evidence
RED (commit
0a6cf0041, tests only, against the unfixed sources)test/cmocka/test_graphics_alias_ordinaryruns each function twice - onseparate storage, then on the arrangement the audit measured - and asserts the
results are identical:
The new Rust tests fail on the same commit:
(The four Rust bounds arrangements fail because the C oracle is unfixed; the
plane one fails because the aliased call no longer equals a separate call.)
GREEN (commit
392f91326)Rust:
soldr cargo test -p obs-graphics --lockedin debug, 31 test binaries,0 failures - including
move_vec_aliases_dst_min_matches_c,scale_vec_aliases_dst_min_matches_c,merge_point_vec_aliases_dst_min_matches_c,get_center_dst_aliases_min_matches_cand the updatedplane_from_tri_v1_alias_matches_c. Each compares the aliased result to its ownnon-aliased reference on the Rust side, on the C side, and between the two; the
plane arrangement really shares storage between the destination and
v1(
ptr::addr_of!((*dst).dir)). Release is green apart fromnorm_matches_c_oracle_finiteandnorm_matches_c_oracle_any_bits, which failon the merge base as well (a pre-existing
norm_to_polartrig paritydifference, unrelated to anything here).
Non-overlapping results are unchanged
This is the part a snapshot fix could plausibly get wrong, so it is measured
rather than argued. The method, in full: the five merge-base function bodies are
transcribed byte-for-byte from
git show 6318f4274:libobs/graphics/bounds.cand.../plane.cinto a wrapper translation unit, both sides are compiled against themerge-base headers, every value lane of every input is initialized, both
destination unions are zeroed before each call - which is what makes the
compared padding deterministic - and the results are compared with
memcmpoverthe whole result object (
struct plane's 12 tail bytes are included; they arezero on both sides). Only the non-overlapping arithmetic can differ, because both
destinations are separate storage.
wrappers.c: the five merge-base bodies, byte-for-byte fromgit show 6318f4274probe.c: drives both copies and comparesThe merge-base headers are extracted with
git showso the two sides arecompared against the same declarations:
Run from a directory that holds the two files above, with the branch's tree at
libobs/beside them. The same command withgccinstead ofclangis thex86-64 Debian run (no extra include needed there); on arm64, add the SIMDe
include,
-I<path-to-obs-deps>/include, becauselibobs/util/sse-intrin.htakes
__m128from SIMDe off x86.Results summary, AppleClang 21 (arm64-apple-darwin27) - twelve runs in all, six
shown here and six more under GCC 14 (Debian 14.2.0, x86-64) with the same
command. Each line is the last line of that run's output.
Each run is 50,000 comparisons (5 functions x 10,000 inputs); 12 runs, 600,000
comparisons, zero mismatches.
Sensitivity, with the mutation the probe was actually checked against: with
plane_from_tri's snapshot changed to copyv2instead ofv1(the one inputwhose value the distance depends on), the same build and the same mode prints,
literally:
The six-mode results above are a summary of the corresponding summary lines
(the probe prints one per run); the mutation block is the probe's literal
output, bytes as captured.
Linux sanitizers (claw, GCC 14, -Werror, ASan + LSan)
and
31test binaries with0failures for the Rust suite, all on the GREENhead. The changed C files compile warning-free; the one warning GCC reports in
the build is a pre-existing
-Wmaybe-uninitializedinlibobs/util/buffered-file-serializer.c:167, which libobs's own Linux configdowngrades (
cmake/linux/compilerconfig.cmake:66), so it does not fail-Werror.What this change deliberately does not include
The rest of the audit's table, left to the follow-ups, because each needs
something this PR does not want to carry:
calc_torque(libobs/graphics/math-extra.c): a plain snapshot of theoperand changes non-overlapping results at AppleClang
-O3 -ffp-contract=fast- a measured fraction of inputs differ in the last bit,because the store-to-reload between the multiply and the add is what keeps them
apart and a snapshot lets a compiler fuse them. A fix that preserves the bits
needs the value to round-trip memory explicitly, which is an optimization
barrier and belongs with the FP-sensitive group rather than in a correctness
PR.
quat_mul,vec3_rotate,vec3_transform3x4: fixing these while keepingnon-overlapping results identical requires the same kind of barrier, and the
barriers cost measurable time. For a latent aliasing bug with no known caller
that tradeoff needs a stated FP contract, not silence inside a correctness PR.
Deferred pending a maintainer decision on the project's FP policy - what
contraction mode the shipped builds should promise, and whether the ports and
the C should both honour it.
polar_to_norm,gs_u8x4_to_float4,matrix3_scale/mirror/mirrorv,quat_from_matrix3/matrix4: cross-type storage overlap and the srgbsame-base arrangement, which is a broader API-contract question.
get_3x3_submatrix(libobs/graphics/matrix4.c): the audit flagged it,but it is private, both callers pass distinct local arrays, and the arrangement
is unreachable from the public API. Untouched here; it should be its own small
PR with a private-helper regression if a maintainer wants it.
Compatibility & risk
existing call sites: every existing characterization test passes, the only
edit being the plane test whose subject this PR changes.
ENABLE_RUST_LIBOBS=ONthe Rust shims are what the cmocka testsexercise; both configurations behave the same for these functions.
-fno-strict-aliasingconfiguration, and their success does not create aportable arbitrary-overlap C contract.
(AppleClang 21) and Linux x86-64 (GCC 14); MSVC's code generation for these
functions has not been exercised. The changed code has no platform-specific
branches.
Status
fix/graphics-alias-ordinary, two commits on top ofmaster(
6318f4274): RED0a6cf0041, GREEN392f91326.twelve-run differential clean on AppleClang 21 and GCC 14, debug suite green,
lint clean.
https://github.com/chriscoveries/obsrust-fork/actions/runs/38328937735
(pinned to
392f913263bb208a0479443bf7a4894f240a19e9: gate and tier2success), plus https://github.com/chriscoveries/obsrust-fork/actions/runs/38308596686
for the mac job (also success; mac was skipped in the later run by
design). From that run's own log:
ENABLE_RUST_LIBOBS=OFF:100% tests passed, 0 tests failed out of 103(the new suite included,
#64: test_graphics_alias_ordinary ... Passed),100% tests passed, 0 tests failed out of 26in the timed partition,100% tests passed, 0 tests failed out of 9in the exclusive partition.== [OFF] FLAKY (failed once, retried): test_deinterlace_2x_field_order-test_deinterlace_2x_field_orderfailed its first attempt in this mode andpassed on the retry; the same executable fails the same way on unrelated
branches (run
38288281479
at
f855e274d). The run is green because of the retry, not because thefailure was ignored.
ENABLE_RUST_LIBOBS=ON:100% tests passed, 0 tests failed out of 103,26/26,9/9, and the same== [ON] FLAKY (failed once, retried): test_deinterlace_2x_field_order.== libobs exported symbols, OFF vs ON/IDENTICAL (1766 symbols).Thank you to the obs-rust maintainers and contributors for building this project in the open. In the American spirit of free expression and individual independence, and with the principle of judging contributions on their merit alone, this PR is a small piece of what independent people can build when they come together.