Skip to content

bug: obs-c-oracle: port_comparison gif bench dereferences a NULL frame #2614

Closed

Description

@rdoupe

What happens

On master (81f7d26, after the libnsgif port #2514), the port_comparison bench test crashes:

soldr cargo test -p obs-c-oracle --locked --bench port_comparison
...
Testing encoder_size/rate_route/1024/c
Success
process didn't exit successfully: `.../port_comparison-...` (signal: 11, SIGSEGV: invalid memory reference)

The next module after encoder_size is gif. A backtrace puts the fault in port_comparison::gif::benchmarks::{closure#0} (the Rust side of the pair), called from pair() before measuring, so every bench module after it never runs (including the per-module allocation check).

Where

rust/obs-c-oracle/benches/port_comparison/gif.rs, both closures:

let mut b = ptr::null_mut();
nsgif_frame_decode(g, f, &mut b);
sum += u64::from(*b.cast::<u32>().add(1920 * 1080 - 1));

The results of nsgif_create(), nsgif_data_scan() and nsgif_frame_decode() are not checked, so when the decode fails b stays NULL and is dereferenced. Not traced yet: why the decode of the bench's 5-frame 1080p GIF fails on the Rust side under the test profile (the fixture, crate::lzw::stream(), or the decoder).

Proposed fix

Check each return in both closures (assert NSGIF_OK, and a non-null bitmap) so a failure names itself instead of crashing, then find why the decode fails and fix the fixture or the decoder. The bench test is the port_comparison check every port runs (soldr cargo test -p obs-c-oracle --locked --bench port_comparison).

Failing test

The bench test itself, on master as it stands.

Activity

  1. rdoupe commented on Oct 27, 2026

    @rdoupe
    CollaboratorAuthor

    Claiming: #2614.

    1. RED: check every libnsgif return and the bitmap in both gif bench closures, so the bench test fails naming the call instead of segfaulting.
    2. Find why the Rust-side decode of the 5-frame 1080p fixture fails under the test profile, and fix the fixture or the decoder (with a Tier 1/3 test if it's the decoder).
    3. GREEN: the port_comparison bench test passes again, including the per-module allocation check.

    Draft PR expected today.

  2. added
    ready
    on Oct 27, 2026
  3. vpacher commented on Oct 27, 2026

    @vpacher
    Member

    Analysis, since this bench is ours (#2514). It saves step 2 of the plan: the decode does not fail.

    Cause. nsgif_frame_decode returns the bitmap handle that the create callback made, here a Box<Vec<u32>> (24 bytes of Vec header), not the pixels. Both closures treat the handle as the pixel buffer and read b + (1920 * 1080 - 1) * 4, about 8 MB past a 24-byte heap object. The pixels must be read through the handle (get_buffer, or the Vec behind it).

    Evidence (mbmax, master 3f91af760):

    • Under lldb the fault is EXC_BAD_ACCESS at 0xa88412cfc in gif::benchmarks::{closure#0} at gif.rs:78. The address is not null.
    • With the returns printed, every call is NSGIF_OK and every bitmap is non-null: create 0, scan 0, 5 frames, decode 0 for frames 0 to 4.
    • It depends on the heap layout. In the bench profile (cargo bench, as obs-graphics: Port the libnsgif state machine and public API (#2510) #2514 measured) the read lands in mapped, zeroed memory, so both sides "read" 0 and match. In the test profile, or with extra allocations (the printed run passed), it lands in unmapped memory: SIGBUS on macOS, SIGSEGV on Linux. The C closure has the same defect.

    So checking the returns alone does not find it (they are all NSGIF_OK); the read must change. The fixture and the decoder are correct.

    Tested patch (read the last pixel through the handle; also assert every return and a non-null bitmap, as you planned):

    diff --git a/rust/obs-c-oracle/benches/port_comparison/gif.rs b/rust/obs-c-oracle/benches/port_comparison/gif.rs
    index f6649bc9f..9cbc83b4e 100644
    --- a/rust/obs-c-oracle/benches/port_comparison/gif.rs
    +++ b/rust/obs-c-oracle/benches/port_comparison/gif.rs
    @@ -34,6 +34,19 @@ unsafe extern "C" fn get_buffer(bitmap: *mut c_void) -> *mut u8 {
         unsafe { (*bitmap.cast::<Vec<u32>>()).as_mut_ptr().cast() }
     }
     
    +/// The last pixel of a decoded frame. `nsgif_frame_decode` returns the
    +/// bitmap handle that `create` made, not its pixels, so read them through it.
    +///
    +/// # Safety
    +///
    +/// `bitmap` is null or from `create`, live.
    +unsafe fn last_pixel(bitmap: *mut c_void) -> u32 {
    +    assert!(!bitmap.is_null(), "nsgif_frame_decode returned no bitmap");
    +    // SAFETY: from `create`, live (caller).
    +    let px = unsafe { &*bitmap.cast::<Vec<u32>>() };
    +    px[1920 * 1080 - 1]
    +}
    +
     static VT: BitmapCbVt = BitmapCbVt {
         create: Some(create),
         destroy: Some(destroy),
    @@ -69,13 +82,14 @@ pub fn benchmarks(criterion: &mut Criterion) {
                 let mut sum = 0u64;
                 // SAFETY: a live GIF over live data; local out-pointers.
                 unsafe {
    -                nsgif_create(&VT, 0, &mut g);
    +                assert_eq!(nsgif_create(&VT, 0, &mut g) as i32, 0);
                     let r = nsgif_data_scan(g, data.len(), data.as_ptr());
    +                assert_eq!(r as i32, 0);
                     nsgif_data_complete(g);
                     for f in 0..FRAMES {
                         let mut b = ptr::null_mut();
    -                    nsgif_frame_decode(g, f, &mut b);
    -                    sum += u64::from(*b.cast::<u32>().add(1920 * 1080 - 1));
    +                    assert_eq!(nsgif_frame_decode(g, f, &mut b) as i32, 0, "frame {f}");
    +                    sum += u64::from(last_pixel(b));
                     }
                     nsgif_destroy(g);
                     (r as i32, black_box(sum))
    @@ -86,13 +100,14 @@ pub fn benchmarks(criterion: &mut Criterion) {
                 let mut sum = 0u64;
                 // SAFETY: as above, through the C signatures.
                 unsafe {
    -                c::oracle_nsgif_create((&raw const VT).cast(), 0, &mut g);
    +                assert_eq!(c::oracle_nsgif_create((&raw const VT).cast(), 0, &mut g), 0);
                     let r = c::oracle_nsgif_data_scan(g, data.len(), data.as_ptr());
    +                assert_eq!(r, 0);
                     c::oracle_nsgif_data_complete(g);
                     for f in 0..FRAMES {
                         let mut b = ptr::null_mut();
    -                    c::oracle_nsgif_frame_decode(g, f, &mut b);
    -                    sum += u64::from(*b.cast::<u32>().add(1920 * 1080 - 1));
    +                    assert_eq!(c::oracle_nsgif_frame_decode(g, f, &mut b), 0, "frame {f}");
    +                    sum += u64::from(last_pixel(b));
                     }
                     c::oracle_nsgif_destroy(g);
                     (r, black_box(sum))

    Verified: cargo test -p obs-c-oracle --locked --bench port_comparison passes, 504/504 (all modules after gif run again); cargo fmt --all -- --check and cargo clippy -p obs-c-oracle --benches -- -D warnings are clean. Use it as you like; it is your claim.

  4. rdoupe commented on Oct 27, 2026

    @rdoupe
    CollaboratorAuthor

    Thanks, that matches what I found: every call returns NSGIF_OK with a bitmap, and the fault is the read through the handle. The fix is in #2616: the first commit checks every return and the bitmap (the bench still crashes, which shows the read is the bug), and the second reads the last pixel through get_buffer. The bench test runs to the end again, including the per-module allocation check. Your macOS trace and the note on why the bench profile hid it are credited in the PR.

  5. rdoupe commented on Oct 27, 2026

    @rdoupe
    CollaboratorAuthor

    Closed by the merge of #2616 (the fix to the gif bench's pixel read).

    What fixed it: the gif benchmark read the last pixel by indexing the bitmap handle that nsgif_frame_decode returns as if it were the pixel buffer. It now reads through the bench's own get_buffer callback, and every libnsgif return and the bitmap are checked in both closures. The decoders were not changed. The port_comparison bench test runs every module to the end again, including the per-module allocation check.

    What's left: nothing. Follow-up issue: none needed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

ready

Type

No type

Projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions