feat(compress): move CDict attach reset orchestration into Rust

Move prepared-CDict attachment reset sequencing into a Rust projection with
explicit reset, window-attach, dictionary-metadata, and block-state ordering.
C retains parameter adjustment, private window linkage, and CCtx/CDict field
access behind callbacks, with reset allocation failures stopping before any
attachment mutation.

Test Plan:
- ulimit -v 41943040 && CARGO_BUILD_JOBS=1 cargo test --manifest-path rust/Cargo.toml (717 passed)
- ulimit -v 41943040 && CARGO_BUILD_JOBS=1 cargo clippy --manifest-path rust/Cargo.toml --all-targets -- -D warnings
- ulimit -v 41943040 && make -j1
- ulimit -v 41943040 && make -j1 -C tests test-fuzzer FUZZERTEST=-T3s FUZZER_FLAGS=--no-big-tests (287 cases)
- ulimit -v 41943040 && make -j1 -C tests test-zstream ZSTREAM_TESTTIME=-T2s (84 deterministic plus 137 randomized cases)
This commit is contained in:
2026-07-19 20:41:52 +02:00
parent 28becbe6ee
commit b8e70c1467
2 changed files with 332 additions and 57 deletions
+200
View File
@@ -1405,6 +1405,94 @@ pub unsafe extern "C" fn ZSTD_rust_resetCCtxByCopyingCDict(
0
}
type ResetCCtxByAttachingCDictResetFn =
unsafe extern "C" fn(*mut c_void, *const c_void, *const c_void, u64, c_int) -> usize;
type ResetCCtxByAttachingCDictAttachFn = unsafe extern "C" fn(*mut c_void, *const c_void);
type ResetCCtxByAttachingCDictStateFn = unsafe extern "C" fn(*mut c_void, *const c_void);
/// Projection for attaching a prepared CDict to a working CCtx.
///
/// Rust owns reset, attach, and metadata-copy ordering. C retains parameter
/// adjustment, private window linkage, and the CCtx/CDict field layout.
#[repr(C)]
pub struct ZSTD_rust_resetCCtxByAttachingCDictState {
callback_context: *mut c_void,
cdict: *const c_void,
params: *const c_void,
pledged_src_size: u64,
reset: Option<ResetCCtxByAttachingCDictResetFn>,
attach: Option<ResetCCtxByAttachingCDictAttachFn>,
copy_dict_state: Option<ResetCCtxByAttachingCDictStateFn>,
copy_block_state: Option<ResetCCtxByAttachingCDictStateFn>,
zbuff: c_int,
}
const _: () = {
assert!(size_of::<ResetCCtxByAttachingCDictResetFn>() == size_of::<usize>());
assert!(size_of::<ResetCCtxByAttachingCDictAttachFn>() == size_of::<usize>());
assert!(size_of::<ResetCCtxByAttachingCDictStateFn>() == size_of::<usize>());
assert!(offset_of!(ZSTD_rust_resetCCtxByAttachingCDictState, callback_context) == 0);
assert!(offset_of!(ZSTD_rust_resetCCtxByAttachingCDictState, cdict) == size_of::<usize>());
assert!(offset_of!(ZSTD_rust_resetCCtxByAttachingCDictState, params) == 2 * size_of::<usize>());
assert!(
offset_of!(ZSTD_rust_resetCCtxByAttachingCDictState, pledged_src_size)
== 3 * size_of::<usize>()
);
assert!(
offset_of!(ZSTD_rust_resetCCtxByAttachingCDictState, reset)
== 3 * size_of::<usize>() + size_of::<u64>()
);
assert!(
offset_of!(ZSTD_rust_resetCCtxByAttachingCDictState, zbuff)
== 3 * size_of::<usize>() + size_of::<u64>() + size_of::<[usize; 4]>()
);
assert!(
size_of::<ZSTD_rust_resetCCtxByAttachingCDictState>()
== (offset_of!(ZSTD_rust_resetCCtxByAttachingCDictState, zbuff) + size_of::<c_int>())
.div_ceil(size_of::<usize>())
* size_of::<usize>()
);
};
/// Run the private CDict-attachment operation through C-owned layout callbacks.
#[no_mangle]
pub unsafe extern "C" fn ZSTD_rust_resetCCtxByAttachingCDict(
state: *const ZSTD_rust_resetCCtxByAttachingCDictState,
) -> usize {
if state.is_null() {
return ERROR(ZstdErrorCode::Generic);
}
let state = unsafe { &*state };
let (Some(reset), Some(attach), Some(copy_dict_state), Some(copy_block_state)) = (
state.reset,
state.attach,
state.copy_dict_state,
state.copy_block_state,
) else {
return ERROR(ZstdErrorCode::Generic);
};
if state.callback_context.is_null() || state.cdict.is_null() || state.params.is_null() {
return ERROR(ZstdErrorCode::Generic);
}
unsafe {
let reset_error = reset(
state.callback_context,
state.cdict,
state.params,
state.pledged_src_size,
state.zbuff,
);
if ERR_isError(reset_error) {
return reset_error;
}
attach(state.callback_context, state.cdict);
copy_dict_state(state.callback_context, state.cdict);
copy_block_state(state.callback_context, state.cdict);
}
0
}
/// Scalar projections for the public CDict query helpers.
#[repr(C)]
pub struct ZSTD_rust_cdictQueryState {
@@ -2676,6 +2764,118 @@ mod tests {
assert_eq!(probe.events, ["reset"]);
}
#[derive(Default)]
struct ResetCCtxByAttachingCDictProbe {
events: Vec<&'static str>,
reset_result: usize,
cdict: *const c_void,
params: *const c_void,
pledged_src_size: u64,
zbuff: c_int,
}
unsafe fn reset_cctx_by_attaching_cdict_probe(
context: *mut c_void,
) -> &'static mut ResetCCtxByAttachingCDictProbe {
unsafe { &mut *context.cast::<ResetCCtxByAttachingCDictProbe>() }
}
unsafe extern "C" fn reset_cctx_by_attaching_cdict_reset(
context: *mut c_void,
cdict: *const c_void,
params: *const c_void,
pledged_src_size: u64,
zbuff: c_int,
) -> usize {
let probe = unsafe { reset_cctx_by_attaching_cdict_probe(context) };
probe.events.push("reset");
probe.cdict = cdict;
probe.params = params;
probe.pledged_src_size = pledged_src_size;
probe.zbuff = zbuff;
probe.reset_result
}
unsafe extern "C" fn reset_cctx_by_attaching_cdict_attach(
context: *mut c_void,
_cdict: *const c_void,
) {
unsafe { reset_cctx_by_attaching_cdict_probe(context) }
.events
.push("attach");
}
unsafe extern "C" fn reset_cctx_by_attaching_cdict_copy_dict_state(
context: *mut c_void,
_cdict: *const c_void,
) {
unsafe { reset_cctx_by_attaching_cdict_probe(context) }
.events
.push("dict");
}
unsafe extern "C" fn reset_cctx_by_attaching_cdict_copy_block_state(
context: *mut c_void,
_cdict: *const c_void,
) {
unsafe { reset_cctx_by_attaching_cdict_probe(context) }
.events
.push("block");
}
fn reset_cctx_by_attaching_cdict_test_state(
probe: &mut ResetCCtxByAttachingCDictProbe,
cdict: *const c_void,
params: *const c_void,
) -> ZSTD_rust_resetCCtxByAttachingCDictState {
ZSTD_rust_resetCCtxByAttachingCDictState {
callback_context: (probe as *mut ResetCCtxByAttachingCDictProbe).cast(),
cdict,
params,
pledged_src_size: 123,
reset: Some(reset_cctx_by_attaching_cdict_reset),
attach: Some(reset_cctx_by_attaching_cdict_attach),
copy_dict_state: Some(reset_cctx_by_attaching_cdict_copy_dict_state),
copy_block_state: Some(reset_cctx_by_attaching_cdict_copy_block_state),
zbuff: 7,
}
}
#[test]
fn reset_cctx_by_attaching_cdict_runs_callbacks_in_original_order() {
let mut probe = ResetCCtxByAttachingCDictProbe::default();
let cdict = 0x4000usize as *const c_void;
let params = 0x3000usize as *const c_void;
let state = reset_cctx_by_attaching_cdict_test_state(&mut probe, cdict, params);
let result = unsafe { ZSTD_rust_resetCCtxByAttachingCDict(&state) };
assert_eq!(result, 0);
assert_eq!(probe.events, ["reset", "attach", "dict", "block"]);
assert_eq!(probe.cdict, cdict);
assert_eq!(probe.params, params);
assert_eq!(probe.pledged_src_size, 123);
assert_eq!(probe.zbuff, 7);
}
#[test]
fn reset_cctx_by_attaching_cdict_stops_after_reset_error() {
let mut probe = ResetCCtxByAttachingCDictProbe {
reset_result: ERROR(ZstdErrorCode::MemoryAllocation),
..Default::default()
};
let state = reset_cctx_by_attaching_cdict_test_state(
&mut probe,
0x4000usize as *const c_void,
0x3000usize as *const c_void,
);
let result = unsafe { ZSTD_rust_resetCCtxByAttachingCDict(&state) };
assert_eq!(result, ERROR(ZstdErrorCode::MemoryAllocation));
assert_eq!(probe.events, ["reset"]);
}
unsafe fn cctx_policy_probe(context: *mut c_void) -> &'static mut CctxPolicyProbe {
unsafe { &mut *context.cast::<CctxPolicyProbe>() }
}