Skip to content

Threaded decoder: null derefs, heap overflows, wild free and deadlocks on SVC and resolution-change streams (2.7.0) #4079

Backlog

Description

@merrowd

Version: 2.7.0 (tarball sha256 5178775c8b440073e70920e97d195efff721c0e56d0a78cc82c919e95d54924d).

All findings below were reproduced on ASan+UBSan builds (gcc 13.3, no ASM) and, where noted, on a plain build. The decoder harness feeds one access unit per DecodeFrameNoDelay call with the thread count set through DECODER_OPTION_NUM_OF_THREADS, unless a finding says otherwise. Trigger files are binary .264 streams. Size and sha256 are given for each; the files can be provided on request.

1. Null pPicBuff deref on threaded DecodeFrame2 flush (Medium)

PrefetchPic reads pPicBuf->iCapacity at codec/decoder/core/src/pic_queue.cpp:188 with a NULL argument, called from DecodeCurrentAccessUnit at codec/decoder/core/src/decoder_core.cpp:2580. Any valid multi-frame stream, 2 or 3 threads, then DecodeFrame2(NULL, 0) to drain. No crafted input needed. Plain build segfaults (exit 139). 1 thread is clean.

Root cause: ConstructAccessUnit allocates the picture buffer only when the thread count is 1 (decoder_core.cpp:2389). In threaded mode the buffer is allocated on the DecodeFrameNoDelay path, but DecodeFrame2 reaches ConstructAccessUnit through the trailing else branch of WelsDecodeBs (decoder.cpp:954) unguarded. The sibling case at line 2542 does guard the NULL.

Related: open PR #4059 reports the same sink reached through a different path (calling-thread construction on a mid-buffer non-VCL NAL). This is a second path to the same NULL deref.

2. Threaded deadlock on a valid shipped stream (Medium)

Shipped res/VID_1280x720_cabac_temporal_direct.264 (2,640,094 bytes, 298 frames) never finishes at 2 or 3 threads (all threads in futex waits, ~0 CPU). 1 thread finishes in 4 s. A 265-frame prefix is clean, 270 frames hang. A PrefetchPic ERROR, pSps->iNumRefFrames:4 log line appears shortly before the stall.

Related: open issue #4027 reports the same stall class (threaded decode never returns once PrefetchPic cannot find a free buffer). This is a repro that reaches it with default settings on a shipped valid stream, no pool change needed.

3. Heap-buffer-overflow WRITE on threaded resolution change (High)

memset of pMbCorrectlyDecodedFlag at codec/decoder/core/src/decoder_core.cpp:2649 writes 240 bytes into a 95-byte heap buffer on a worker thread. Input carries two SPSs (20x12 and 1x68 MBs). Deterministic at 2 threads, 3/3 runs. Single-threaded decode of the same input is clean. Plain build hangs (heap corruption, then deadlock) instead of crashing.

Minimized trigger crash-1.min.264: 17,777 bytes, sha256 8abb4e51ffb3c757df3fac614e474d718efe14a19179da2d905f59ee87c7bdc8.

Root cause: the worker reuses a DPB picture whose per-picture arrays were sized for the old resolution while pCtx->pSps already reflects the new one.

Related: open PR #4076 reports the same sink line reached single-threaded through an in-place SPS overwrite that misses new-sequence detection. This is the threaded variant.

4. Null SPicture deref in error concealment (Medium)

DoErrorConSliceCopy reads pDstPic->iLinesize[0] at codec/decoder/core/src/error_concealment.cpp:169 with pDstPic == pCtx->pDec == NULL. Deterministic at 2 threads, 3/3 runs. Plain build segfaults. Single-threaded is clean.

Minimized trigger crash-2.min.264: 13,904 bytes, sha256 b2647451786aee0dc8641f20d3d2a115b43e34135a1d95babd40a52a7b05dae8. Three SPSs (22x18, 40x30, 22x18 MBs), another resolution-change stream.

The same line is also reached from a second trigger at 2 threads once the null pNalCur deref below is fixed: f4-null-pnalcur-2508.264, 35,825 bytes, sha256 fb42edeaffa9b0418e742e0733652824a4ed260aef20f3c0381560b183e0d9d8. CheckAndFinishLastPic calls ImplementErrorCon on the main thread context where pCtx->pDec is still NULL.

5. Partial-AU feeding permanently breaks B-frame references (Low, correctness)

Splitting any single AU across two DecodeFrame2 calls makes every subsequent frame error-concealed on stream res/VID_1280x544_cabac_temporal_direct.264 (151 frames): 0 errors unsplit vs 75-112 "Ref Picture for B-Slice is lost" errors and 100-150 error-concealed frames after one split. The decoder keeps a trailing partial NAL across calls by design (decoder.cpp:882-897), so chunked feeding is meant to be transparent. No crash, no memory safety impact.

6. Null pSps deref after a failed slice-header parse (Medium)

AllocPicBuffOnNewSeqBegin dereferences pCtx->pSps at codec/decoder/core/src/decoder_core.cpp:2334 (iMbWidth is at offset 4, so a release build reads address 0x4). Trigger f1-null-psps-2334.264: 16,768 bytes, sha256 fb89abbbcb931da029dac8fe10b85209f6dcaa12ae620c4fa354dc2ad7f80f28. AU0 valid IDR, AU1 P, AU2 a single P slice with slice_type 11 (invalid). Crashes at 1 thread and 2 threads, 3/3 runs.

Root cause: ParseSliceHeaderSyntaxs rejects slice_type > 9 before assigning pSps, but ForceClearCurrentNal only decrements the NAL count and leaves the half-parsed slot in the AU list. WelsDecodeInitAccessUnitStart then copies the NULL pSps into pCtx->pSps with no check.

Same root cause, secondary sites (same trigger): WelsMarkAsRef at manage_dec_ref.cpp:648 (worker thread, seen 1/6 runs) and BufferingReadyPicture at welsDecoderExt.cpp:1029 (seen once, flaky race, 0/15 on re-run; do not treat as deterministic).

7. Threaded deadlock on the second AU (Medium)

Trigger f2-deadlock-au1.264: 2,027 bytes, sha256 2b11024809bb89a2ec81a6df5a900cd4824d602f805fb3006f86f0d038d0e846. SPS, PPS, three IDR slices (AU0, 11x9 MBs), then one 70-byte P slice (AU1) with a frame_num gap. Hangs at 2 and 3 threads, 3/3 runs. 1 thread is clean. AU0 alone is clean at any thread count. Distinct from finding 2: this hangs on the second AU of a malformed stream, not after hundreds of valid frames.

8. Wild-pointer free at decoder teardown (High)

UninitialDqLayersContext frees a wild pointer at codec/decoder/core/src/decoder_core.cpp:1706 (via WelsFreeDynamicMemory / WelsCloseDecoder / Uninitialize). ASan reports SEGV in free on an address that was never malloced, deterministic per thread count. Trigger f3-teardown-wildfree.264: 8,329 bytes, sha256 6688aac47877a63e97484acfc9c53862a69885f8f9f1c14bd319680e7debb20b. Clean at 1 thread, crashes 3/3 at 2 and 3 threads. A 2 s sleep before Uninitialize still crashes, so it is not a live-worker race: the stored pointer is already corrupt when decode finishes.

Pinned root cause: the writing instruction is pCurDqLayer->pNoSubMbPartSizeLessThan8x8Flag[iMbXy] = true at codec/decoder/core/src/decode_slice.cpp:1812 with iMbXy=110 against a 44-entry array. The stream's sMb arrays hold 44 MBs (11x4) but an enhancement I slice (depId=1, firstMb=110, SPS 36x22 = 792 MBs) decodes against them. The 1-byte write lands in the neighbouring chunk's malloc header and flips a stored pointer byte. Same geometry-mismatch family as finding 9 below.

Related but distinct: open PR #4007 is about Uninitialize running while worker threads are still alive. That is a different mechanism.

9. Heap-buffer-overflow WRITE in threaded CAVLC P-slice decode (High)

memset (pCurDqLayer->pDec->pRefIndex[0][iMbXy], 0, 16) at codec/decoder/core/src/decode_slice.cpp:2492. ASan: WRITE of size 16, 69 bytes past a 1483-byte region, worker thread. Trigger f7-heap-overflow-prefindex-2492.264: 7,483 bytes, sha256 5a908a00249cd60d3fc150105a203959a3a16e501f5ee15f82a6fefd7128b33d. Clean at 1 thread, crashes 3/3 at 2 and 3 threads.

Pinned root cause: the MB loop in WelsDecodeAndConstructSlice (decode_slice.cpp:1631) bounds itself by the slice's own SPS total and never checks against the picture/layer allocation. Every picture is 112x208 (91 MBs) but the enhancement slice (depId=1) carries an SPS of 384x224 (336 MBs) with firstMb=320, so MBs 320..335 decode against 91-entry arrays. At 1 thread only base-layer slices decode, which is why this is threaded-only. Same family as finding 8.

10. Null pNalCur deref in threaded CAVLC P-slice decode (Medium)

WelsDecodeMbCavlcPSlice reads pNalCur->sNalHeaderExt.uiQualityId at codec/decoder/core/src/decode_slice.cpp:2508 with pNalCur == NULL. Trigger f4-null-pnalcur-2508.264: 35,825 bytes, sha256 fb42edeaffa9b0418e742e0733652824a4ed260aef20f3c0381560b183e0d9d8. Clean at 1 thread, crashes 3/3 at 2 and 3 threads.

Root cause: WelsDecodeAndConstructSlice reads the current NAL from pCtx->pNalCur, but pCtx->pNalCur is set to NULL at DecodeCurrentAccessUnit entry and is never assigned a non-NULL value anywhere in the decoder. The single-thread path passes the loop's local pNalCur directly, so only the threaded path passes NULL. The deref fires when a P-slice skip MB is decoded with bDefaultResidualPredFlag set. The B-slice variant WelsDecodeMbCavlcBSlice (decode_slice.cpp:2625) reads the same fields without a check.

11. Deadlock reachable with threads=1 (Medium)

Trigger f5-deadlock-1thread.264: 4,442 bytes, sha256 c81ba000891a09abe00a08a73137c14073e998ea2f0d297c4a5df87e24690bb5. Hangs at 1 thread, 3/3 runs (both threads in futex waits, 0 CPU), and at 2 threads. The last AU sticks after a "PrefetchPic ERROR, pSps->iNumRefFrames:4" log line. Same observable shape as finding 7, but reachable in the nominal single-threaded configuration, so it is reported separately. It may share a root cause with finding 7; that is not proven.

12. Heap-use-after-free in ReleaseBufferedReadyPictureReorder (High)

--pPic->iRefCount path reads a freed SPicBuff at codec/decoder/plus/src/welsDecoderExt.cpp:1143 (pPicBuff->iCapacity, READ of size 4, 24 bytes into a freed 51-byte region). Trigger is the finding 10 file f4-null-pnalcur-2508.264 at 1 thread, deterministic on a true pristine rebuild at -O1 and -O3 with -g. Mechanism: on a new-sequence realloc, DestroyPicBuff frees the SPicBuff struct, then ReleaseBufferedReadyPictureReorder reads it through the stale m_pPicBuff member (called with pCtx NULL from DecodeFrameNoDelay). Same thread, sequential, not a race. Caveat: an older prebuilt libdecoder.a without -g does not reproduce it at 1 thread (exits 0); the reason is undetermined, so treat the -g rebuild result as the reference.

13. Out-of-bounds pReadyEvent signal, usually self-hiding (Medium)

The trailing SET_EVENT(&pCtx->pDec->pReadyEvent[pCurDqLayer->iMbY]) in WelsDecodeAndConstructSlice indexes row 6 of a 6-entry array for any slice ending exactly at a row boundary (slice 10x6, 60 MBs, picture height 96). EventPost then locks a garbage mutex and the worker blocks forever in futex. Minimal trigger is the finding 6 file f1-null-psps-2334.264 at 2 threads (deterministic hang pre-fix). It usually self-hides: the array is a pool sub-allocation, so ASan cannot see the one-entry overrun, and the following bytes usually look like a valid mutex. It hangs only when they look like a locked mutex. Same family exists in the row-change signal and in BaseMC (down_line clamped by sequence-max height, not picture height), untested live.

14. GetColocatedMb INFINITE wait on an incomplete colocated picture (Medium)

GetColocatedMb waits forever at codec/decoder/core/src/mv_pred.cpp:327 (WAIT_EVENT(&colocPic->pReadyEvent[pCurDqLayer->iMbY], WELS_DEC_THREAD_WAIT_INFINITE)). Trigger is the finding 10 file f4-null-pnalcur-2508.264 at 3 threads. The worker decoding AU ts=4 (a B-slice) blocks on row 0 of the colocated picture (frame 2, incomplete, row 0 never signalled) because its decode already finished with an error, so nothing will ever signal that row. The main thread blocks in the ParseAccessUnit new-sequence barrier waiting for that worker to go idle. All 4 threads futex-blocked, 0 CPU. Note: on pristine 2.7.0 the UBSan null deref at finding 10 aborts the worker first (with halt_on_error=1), so the hang is seen once that deref is fixed. Bounding the wait to the 3 s timeout used by BaseMC turns the hang into the finding 4 error.

Related: open issues #4032 and #4069 cover BaseMC row waits on incomplete reference pictures. This is a different wait site (GetColocatedMb, no timeout at all).

15. Allocator assert on partial-AU feeding (Low)

~CMemoryAlign assertion m_nMemoryUsageInBytes == 0 fails at decoder destruction (codec/common/src/memory_align.cpp:60), abort. Trigger f6-leak-assert-memoryalign.264: 6,206 bytes, sha256 265db9bcc1ef37edfe3e3e7c654829b8eb055c31be06bee6f55a52a508a9758b. Caveat: reproduces only when AUs are split on any first_mb==0 VCL slice including type-20 enhancement slices (partial-AU feeding). With correct SVC-aware feeding it does not reproduce. Assert-only, compiled out in release builds, no memory-safety violation demonstrated. Upstream should confirm whether partial-AU feeding is in contract for SVC before treating it as a decoder bug.

Notes on local fix testing

Fix shapes were tested on a local copy only, never upstream. A combined set (MB-loop bound by the picture/layer allocation, pNalCur sync in DecodeCurrentAccessUnit, handshake release on the AU-rejection error path, BaseMC lost-ref hatch, pReadyEvent row guard, bounded colocated wait, NULL guard on the error-concealment call sites, stale m_pPicBuff sync) makes findings 1, 2, 6, 8, 9, 11 and 14 exit 0 at 1T/2T/3T with no regression on a valid SVC stream. Finding 10 then surfaces finding 4 instead, which the error-concealment guard covers. This is reported as test evidence only, not as a proposed patch.

Activity

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions