From a5739a592ee3a3fd7a696455d5125744ec0e24e5 Mon Sep 17 00:00:00 2001 From: Khyber Sen Date: Mon, 22 Apr 2024 18:24:31 -0700 Subject: [PATCH 1/2] `struct Cdf*`: Initialize with `Default::default()` instead of `mem::zeroed()`, as previously we were `mem::zeroed()`ing a `RwLock` (UB). --- src/cdf.rs | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) diff --git a/src/cdf.rs b/src/cdf.rs index 39c3e0294..4fd3d9ba8 100644 --- a/src/cdf.rs +++ b/src/cdf.rs @@ -19,14 +19,13 @@ use crate::src::tables::dav1d_partition_type_count; use std::cmp; use std::ffi::c_int; use std::ffi::c_uint; -use std::mem; use std::sync::atomic::AtomicU32; use std::sync::Arc; use std::sync::RwLock; use std::sync::RwLockWriteGuard; use strum::EnumCount; -#[derive(Clone)] +#[derive(Clone, Default)] #[repr(C)] pub struct CdfContext { pub m: CdfModeContext, @@ -36,14 +35,14 @@ pub struct CdfContext { pub dmv: CdfMvContext, } -#[derive(Clone)] +#[derive(Clone, Default)] #[repr(C)] pub struct CdfMvContext { pub comp: [CdfMvComponent; 2], pub joint: Align8<[u16; 4]>, } -#[derive(Clone)] +#[derive(Clone, Default)] #[repr(C)] pub struct CdfMvComponent { pub classes: Align32<[u16; 16]>, @@ -56,7 +55,7 @@ pub struct CdfMvComponent { pub sign: Align4<[u16; 2]>, } -#[derive(Clone)] +#[derive(Clone, Default)] #[repr(C)] pub struct CdfCoefContext { pub eob_bin_16: Align16<[[[u16; 8]; 2]; 2]>, @@ -74,7 +73,7 @@ pub struct CdfCoefContext { pub dc_sign: Align4<[[[u16; 2]; 3]; 2]>, } -#[derive(Clone)] +#[derive(Clone, Default)] #[repr(C)] pub struct CdfModeContext { pub y_mode: Align32<[[u16; N_INTRA_PRED_MODES + 3]; 4]>, @@ -5127,7 +5126,7 @@ pub unsafe fn rav1d_cdf_thread_alloc( None }; Ok(CdfThreadContext::Cdf(Arc::new(CdfThreadContext_data { - cdf: mem::zeroed(), + cdf: Default::default(), progress, }))) } From 73336f1cf129ac11bcb6d761b263dea34cb4b0ef Mon Sep 17 00:00:00 2001 From: Khyber Sen Date: Mon, 22 Apr 2024 18:25:12 -0700 Subject: [PATCH 2/2] `struct Rav1dTaskContext`: Initialize fields with `Default::default()` except for `scratch`, which is a `union` and a WIP to make safe. --- src/internal.rs | 11 +++++------ src/refmvs.rs | 12 ++++++++++++ 2 files changed, 17 insertions(+), 6 deletions(-) diff --git a/src/internal.rs b/src/internal.rs index ecedf6ad6..009a8dcbc 100644 --- a/src/internal.rs +++ b/src/internal.rs @@ -98,7 +98,6 @@ use std::ops::Index; use std::ops::IndexMut; use std::ops::Range; use std::ops::Sub; -use std::ptr; use std::sync::atomic::AtomicBool; use std::sync::atomic::AtomicI32; use std::sync::atomic::AtomicU32; @@ -1070,22 +1069,22 @@ pub(crate) struct Rav1dTaskContext { } impl Rav1dTaskContext { - pub(crate) unsafe fn new(task_thread: Arc) -> Self { + pub(crate) fn new(task_thread: Arc) -> Self { Self { ts: 0, b: Default::default(), l: Default::default(), a: 0, - rt: mem::zeroed(), + rt: Default::default(), cf: Default::default(), al_pal: Default::default(), pal_sz_uv: Default::default(), - scratch: mem::zeroed(), - warpmv: mem::zeroed(), + scratch: unsafe { mem::zeroed() }, + warpmv: Default::default(), lf_mask: None, top_pre_cdef_toggle: 0, cur_sb_cdef_idx: 0, - tl_4x4_filter: mem::zeroed(), + tl_4x4_filter: Filter2d::Regular8Tap, // 0 frame_thread: Rav1dTaskContext_frame_thread { pass: 0 }, task_thread, } diff --git a/src/refmvs.rs b/src/refmvs.rs index a53b48b4d..11a7983b3 100644 --- a/src/refmvs.rs +++ b/src/refmvs.rs @@ -224,6 +224,7 @@ pub(crate) struct RefMvsFrame { pub n_frame_threads: u32, } +#[derive(Default)] #[repr(C)] pub struct refmvs_tile_range { pub start: c_int, @@ -246,6 +247,17 @@ pub(crate) struct refmvs_tile { pub tile_row: refmvs_tile_range, } +impl Default for refmvs_tile { + fn default() -> Self { + Self { + r: [Default::default(); 37], + rp_proj: Default::default(), + tile_col: Default::default(), + tile_row: Default::default(), + } + } +} + #[derive(Copy, Clone, Default)] #[repr(C)] pub struct refmvs_candidate {