feat(compress): move static CDict gating into Rust
Move public ZSTD_initStaticCDict alignment, null, workspace-size, and callback dispatch into Rust. Keep C static workspace construction and dictionary initialization behind a private callback. Test Plan: - ulimit -v 41943040; CARGO_BUILD_JOBS=1 cargo test --manifest-path rust/Cargo.toml --release init_static_cdict -- --nocapture - ulimit -v 41943040; CARGO_BUILD_JOBS=1 cargo test --manifest-path rust/Cargo.toml --release - ulimit -v 41943040; CARGO_BUILD_JOBS=1 cargo clippy --manifest-path rust/Cargo.toml --release --all-targets -- -D warnings - ulimit -v 41943040; make -B -C programs -j1 zstd - ulimit -v 41943040; make -C tests -j1 test-zstream ZSTREAM_TESTTIME=-T1s
This commit is contained in:
@@ -42,6 +42,7 @@ const ZSTD_MAGIC_DICTIONARY: u32 = 0xEC30_A437;
|
||||
const ZSTD_DCT_AUTO: c_int = 0;
|
||||
const ZSTD_DCT_RAW_CONTENT: c_int = 1;
|
||||
const ZSTD_DCT_FULL_DICT: c_int = 2;
|
||||
const ZSTD_STATIC_WORKSPACE_ALIGNMENT: usize = 8;
|
||||
|
||||
/// C keeps match-state/content insertion private because it depends on the
|
||||
/// configuration-sensitive `ZSTD_MatchState_t`, `ldmState_t`, workspace, and
|
||||
@@ -479,6 +480,50 @@ pub unsafe extern "C" fn ZSTD_rust_freeCDict(state: *const ZSTD_rust_freeCDictSt
|
||||
0
|
||||
}
|
||||
|
||||
type InitStaticCDictFn = unsafe extern "C" fn(*mut c_void) -> *mut c_void;
|
||||
|
||||
/// Explicit projection for the public `ZSTD_initStaticCDict` wrapper.
|
||||
///
|
||||
/// Rust owns workspace pointer/alignment validation, minimum-size validation,
|
||||
/// and callback ordering. C retains the private static-workspace layout and
|
||||
/// dictionary initialization behind one callback.
|
||||
#[repr(C)]
|
||||
pub struct ZSTD_rust_initStaticCDictState {
|
||||
callback_context: *mut c_void,
|
||||
workspace: *mut c_void,
|
||||
workspace_size: usize,
|
||||
needed_size: usize,
|
||||
init: InitStaticCDictFn,
|
||||
}
|
||||
|
||||
const _: () = {
|
||||
assert!(offset_of!(ZSTD_rust_initStaticCDictState, callback_context) == 0);
|
||||
assert!(offset_of!(ZSTD_rust_initStaticCDictState, workspace) == size_of::<usize>());
|
||||
assert!(offset_of!(ZSTD_rust_initStaticCDictState, workspace_size) == 2 * size_of::<usize>());
|
||||
assert!(offset_of!(ZSTD_rust_initStaticCDictState, needed_size) == 3 * size_of::<usize>());
|
||||
assert!(offset_of!(ZSTD_rust_initStaticCDictState, init) == 4 * size_of::<usize>());
|
||||
assert!(size_of::<ZSTD_rust_initStaticCDictState>() == size_of::<[usize; 5]>());
|
||||
};
|
||||
|
||||
/// Validate static CDict workspace ownership before entering the C leaf.
|
||||
#[no_mangle]
|
||||
pub unsafe extern "C" fn ZSTD_rust_initStaticCDict(
|
||||
state: *const ZSTD_rust_initStaticCDictState,
|
||||
) -> *mut c_void {
|
||||
if state.is_null() {
|
||||
return ptr::null_mut();
|
||||
}
|
||||
let state = unsafe { &*state };
|
||||
if state.callback_context.is_null()
|
||||
|| state.workspace.is_null()
|
||||
|| (state.workspace as usize) & (ZSTD_STATIC_WORKSPACE_ALIGNMENT - 1) != 0
|
||||
|| state.workspace_size < state.needed_size
|
||||
{
|
||||
return ptr::null_mut();
|
||||
}
|
||||
unsafe { (state.init)(state.callback_context) }
|
||||
}
|
||||
|
||||
type CompressBeginUsingDictBeginFn =
|
||||
unsafe extern "C" fn(*mut c_void, *const c_void, usize, *const c_void, u64) -> usize;
|
||||
|
||||
@@ -2939,6 +2984,103 @@ mod tests {
|
||||
assert_eq!(result, 0);
|
||||
}
|
||||
|
||||
#[derive(Default)]
|
||||
struct InitStaticCDictProbe {
|
||||
events: Vec<&'static str>,
|
||||
result: *mut c_void,
|
||||
}
|
||||
|
||||
unsafe extern "C" fn init_static_cdict_test_init(context: *mut c_void) -> *mut c_void {
|
||||
let probe = unsafe { &mut *context.cast::<InitStaticCDictProbe>() };
|
||||
probe.events.push("init");
|
||||
probe.result
|
||||
}
|
||||
|
||||
fn init_static_cdict_test_state(
|
||||
probe: &mut InitStaticCDictProbe,
|
||||
workspace: *mut c_void,
|
||||
workspace_size: usize,
|
||||
needed_size: usize,
|
||||
) -> ZSTD_rust_initStaticCDictState {
|
||||
ZSTD_rust_initStaticCDictState {
|
||||
callback_context: (probe as *mut InitStaticCDictProbe).cast(),
|
||||
workspace,
|
||||
workspace_size,
|
||||
needed_size,
|
||||
init: init_static_cdict_test_init,
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn init_static_cdict_rejects_an_unaligned_workspace_before_callback() {
|
||||
let mut probe = InitStaticCDictProbe {
|
||||
result: ptr::dangling_mut(),
|
||||
..Default::default()
|
||||
};
|
||||
let mut workspace = [0usize; 2];
|
||||
let unaligned_workspace = workspace.as_mut_ptr().cast::<u8>().wrapping_add(1).cast();
|
||||
let state = init_static_cdict_test_state(&mut probe, unaligned_workspace, usize::MAX, 0);
|
||||
|
||||
let result = unsafe { ZSTD_rust_initStaticCDict(&state) };
|
||||
|
||||
assert!(result.is_null());
|
||||
assert!(probe.events.is_empty());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn init_static_cdict_rejects_insufficient_workspace_before_callback() {
|
||||
let mut probe = InitStaticCDictProbe {
|
||||
result: ptr::dangling_mut(),
|
||||
..Default::default()
|
||||
};
|
||||
let mut workspace = [0usize; 1];
|
||||
let state = init_static_cdict_test_state(
|
||||
&mut probe,
|
||||
workspace.as_mut_ptr().cast(),
|
||||
size_of_val(&workspace),
|
||||
size_of_val(&workspace) + 1,
|
||||
);
|
||||
|
||||
let result = unsafe { ZSTD_rust_initStaticCDict(&state) };
|
||||
|
||||
assert!(result.is_null());
|
||||
assert!(probe.events.is_empty());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn init_static_cdict_calls_the_initializer_after_workspace_validation() {
|
||||
let mut probe = InitStaticCDictProbe {
|
||||
result: ptr::dangling_mut(),
|
||||
..Default::default()
|
||||
};
|
||||
let mut workspace = [0usize; 1];
|
||||
let state = init_static_cdict_test_state(
|
||||
&mut probe,
|
||||
workspace.as_mut_ptr().cast(),
|
||||
size_of_val(&workspace),
|
||||
size_of_val(&workspace),
|
||||
);
|
||||
|
||||
let result = unsafe { ZSTD_rust_initStaticCDict(&state) };
|
||||
|
||||
assert_eq!(result, probe.result);
|
||||
assert_eq!(probe.events, ["init"]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn init_static_cdict_rejects_a_null_workspace() {
|
||||
let mut probe = InitStaticCDictProbe {
|
||||
result: ptr::dangling_mut(),
|
||||
..Default::default()
|
||||
};
|
||||
let state = init_static_cdict_test_state(&mut probe, ptr::null_mut(), usize::MAX, 0);
|
||||
|
||||
let result = unsafe { ZSTD_rust_initStaticCDict(&state) };
|
||||
|
||||
assert!(result.is_null());
|
||||
assert!(probe.events.is_empty());
|
||||
}
|
||||
|
||||
#[derive(Default)]
|
||||
struct CompressBeginUsingDictProbe {
|
||||
events: Vec<&'static str>,
|
||||
|
||||
Reference in New Issue
Block a user