FilmCraft: An open-source, nonlinear video editor written by scratch in pure Rust
NVDEC: reject pictures without valid bitstream data before the driver call
SilentYetib3d68fe0aeab
diff --git a/crates/platform/src/nvdec/abi_tests.rs b/crates/platform/src/nvdec/abi_tests.rsindex 5ac47d256..3acf0c197 100644--- a/crates/platform/src/nvdec/abi_tests.rs+++ b/crates/platform/src/nvdec/abi_tests.rs@@ -6,6 +6,10 @@ use super::ffi::*;use std::mem::{align_of, offset_of, size_of};#[test]fn layouts_match_the_header() {+ assert_eq!(offset_of!(CUVIDPICPARAMS, nBitstreamDataLen), 24, "CUVIDPICPARAMS.nBitstreamDataLen");+ assert_eq!(offset_of!(CUVIDPICPARAMS, pBitstreamData), 32, "CUVIDPICPARAMS.pBitstreamData");+ assert_eq!(offset_of!(CUVIDPICPARAMS, nNumSlices), 40, "CUVIDPICPARAMS.nNumSlices");+ assert_eq!(offset_of!(CUVIDPICPARAMS, pSliceDataOffsets), 48, "CUVIDPICPARAMS.pSliceDataOffsets");assert_eq!((size_of::<CUVIDEOFORMAT>(), align_of::<CUVIDEOFORMAT>()), (64, 4));assert_eq!(offset_of!(CUVIDEOFORMAT, codec), 0);assert_eq!(offset_of!(CUVIDEOFORMAT, frame_rate), 4);diff --git a/crates/platform/src/nvdec/cuvid.rs b/crates/platform/src/nvdec/cuvid.rsindex d8d3305b4..b52ff92b0 100644--- a/crates/platform/src/nvdec/cuvid.rs+++ b/crates/platform/src/nvdec/cuvid.rs@@ -97,6 +97,9 @@ fn check_caps(caps: &CUVIDDECODECAPS, coded: (u32, u32), bits: u32) -> Result<()Ok(())}+/// Slices per picture the driver's offset list is read for.+const MAX_SLICES: u32 = 8192;+struct State {api: Api,// Points to Session's separately boxed, immutable Device (stable across moves).@@ -306,13 +309,25 @@ unsafe extern "C" fn sequence(data: *mut c_void, format: *mut CUVIDEOFORMAT) ->unsafe { callback(data, |state| state.sequence(format.as_ref().ok_or("NVDEC sequence pointer is null")?)) }}unsafe extern "C" fn decode(data: *mut c_void, picture: *mut CUVIDPICPARAMS) -> i32 {- // SAFETY: the opaque picture parameters belong to cuvid for the callback's duration;- // they are only passed through to the live decoder with the context current.+ // SAFETY: the picture parameters belong to cuvid for the callback's duration: their shared+ // head is read (the slice offset list for at most `MAX_SLICES` entries, as the parser sized+ // it), and they are passed through to the live decoder with the context current.unsafe {callback(data, |state| {if state.decoder.is_null() || picture.is_null() {return Err("NVDEC decode pointer or decoder is null".into());}+ // A picture without bitstream data, or with slices past the end of it, is an error+ // here and never reaches the driver.+ let p = &*picture;+ let offsets = if p.pSliceDataOffsets.is_null() || p.nNumSlices > MAX_SLICES {+ &[][..]+ } else {+ std::slice::from_raw_parts(p.pSliceDataOffsets, p.nNumSlices as usize)+ };+ if p.pBitstreamData.is_null() || p.nBitstreamDataLen == 0 || offsets.is_empty() || offsets.iter().any(|&o| o > p.nBitstreamDataLen) {+ return Err("NVDEC parser returned a picture without valid bitstream data".into());+ }state.drain()?;status((state.api.decode)(state.decoder, picture), "cuvidDecodePicture")?;Ok(1)diff --git a/crates/platform/src/nvdec/ffi.rs b/crates/platform/src/nvdec/ffi.rsindex 814ef6fc6..b8e59ac7b 100644--- a/crates/platform/src/nvdec/ffi.rs+++ b/crates/platform/src/nvdec/ffi.rs@@ -26,7 +26,7 @@//! NVDEC declarations transcribed from NVIDIA's MIT headers (NVDEC API 13.1).//! Linux LP64 only; C `tcu_ulong` is an unsigned long, not a u32.#![allow(non_snake_case, non_camel_case_types, non_upper_case_globals, dead_code)]-use std::ffi::{c_int, c_ulong, c_void};+use std::ffi::{c_int, c_uint, c_ulong, c_void};pub const cudaVideoCodec_H264: c_int = 4;pub const cudaVideoCodec_HEVC: c_int = 8;pub const cudaVideoChromaFormat_420: c_int = 1;@@ -37,9 +37,20 @@ pub const cudaVideoCreate_PreferCUVID: c_ulong = 4;pub const CUVID_PKT_ENDOFSTREAM: c_ulong = 1;pub const CUVID_PKT_TIMESTAMP: c_ulong = 2;pub const CUVID_PKT_ENDOFPICTURE: c_ulong = 8;+/// The start of `CUVIDPICPARAMS`: the fields every codec shares and this backend checks. The rest+/// (reserved words and the codec-specific union) is only ever passed through by pointer.#[repr(C)]pub struct CUVIDPICPARAMS {- _opaque: [u8; 0],+ pub PicWidthInMbs: c_int,+ pub FrameHeightInMbs: c_int,+ pub CurrPicIdx: c_int,+ pub field_pic_flag: c_int,+ pub bottom_field_flag: c_int,+ pub second_field: c_int,+ pub nBitstreamDataLen: c_uint,+ pub pBitstreamData: *const u8,+ pub nNumSlices: c_uint,+ pub pSliceDataOffsets: *const c_uint,}#[repr(C)]#[derive(Clone, Copy)]