feat(compress): move CDict attachment into Rust

Project the prepared CDict window extent and destination match-state fields
through an explicit ABI state so Rust owns the post-reset attachment
operation. Preserve the original empty-dictionary and already-advanced
window branches, including window-limit clearing and loaded-dictionary
referential updates, while keeping private C pointer arithmetic and layout
ownership in the C adapter.

Test Plan:
- CARGO_BUILD_JOBS=1 cargo test --lib zstd_compress_dictionary::tests::reset_cctx_by_attaching_cdict -- --nocapture
- CARGO_BUILD_JOBS=1 cargo clippy --all-targets -- -D warnings
- CARGO_BUILD_JOBS=1 cargo test
- CARGO_BUILD_JOBS=1 make -j1
- CARGO_BUILD_JOBS=1 make -j1 -C tests test-zstream ZSTREAM_TESTTIME=-T2s
- CARGO_BUILD_JOBS=1 make -j1 -C tests test-fuzzer FUZZERTEST=-T3s FUZZER_FLAGS=--no-big-tests

All commands were run serially with a 40 GiB virtual-memory cap.
This commit is contained in:
2026-07-19 23:49:43 +02:00
parent f758c41c5d
commit d8e8683353
2 changed files with 154 additions and 58 deletions
+28 -35
View File
@@ -2563,15 +2563,21 @@ typedef char ZSTD_rust_reset_cctx_by_copying_cdict_state_layout[
typedef size_t (*ZSTD_rust_resetCCtxByAttachingCDictReset_f)(
void* context, const void* cdict, const void* params,
U64 pledgedSrcSize, int zbuff);
typedef void (*ZSTD_rust_resetCCtxByAttachingCDictAttach_f)(
void* context, const void* cdict);
typedef struct {
void* callbackContext;
const void* cdict;
const void* params;
U64 pledgedSrcSize;
ZSTD_rust_resetCCtxByAttachingCDictReset_f reset;
ZSTD_rust_resetCCtxByAttachingCDictAttach_f attach;
const ZSTD_MatchState_t* sourceMatchState;
const U32* sourceCDictEnd;
const U32* sourceCDictLen;
const ZSTD_MatchState_t** destinationDictMatchState;
const BYTE** destinationWindowNextSrc;
const BYTE* const* destinationWindowBase;
U32* destinationWindowLowLimit;
U32* destinationWindowDictLimit;
U32* destinationLoadedDictEnd;
U32* destinationDictID;
const U32* sourceDictID;
size_t* destinationDictContentSize;
@@ -2593,7 +2599,7 @@ typedef char ZSTD_rust_reset_cctx_by_attaching_cdict_state_layout[
&& offsetof(ZSTD_rust_resetCCtxByAttachingCDictState, reset)
== 3 * sizeof(void*) + sizeof(U64)
&& offsetof(ZSTD_rust_resetCCtxByAttachingCDictState, zbuff)
== 3 * sizeof(void*) + sizeof(U64) + 8 * sizeof(void*)
== 3 * sizeof(void*) + sizeof(U64) + 16 * sizeof(void*)
&& sizeof(ZSTD_rust_resetCCtxByAttachingCDictState)
== ((offsetof(ZSTD_rust_resetCCtxByAttachingCDictState, zbuff)
+ sizeof(int) + sizeof(void*) - 1) / sizeof(void*))
@@ -4505,36 +4511,6 @@ static size_t ZSTD_rust_resetCCtx_byAttachingCDict_reset(
return 0;
}
static void ZSTD_rust_resetCCtx_byAttachingCDict_attach(
void* context, const void* cdictOpaque)
{
ZSTD_CCtx* const cctx = (ZSTD_CCtx*)context;
const ZSTD_CDict* const cdict = (const ZSTD_CDict*)cdictOpaque;
U32 const cdictEnd = (U32)(cdict->matchState.window.nextSrc
- cdict->matchState.window.base);
U32 const cdictLen = cdictEnd - cdict->matchState.window.dictLimit;
if (cdictLen == 0) {
DEBUGLOG(4, "skipping attaching empty dictionary");
return;
}
DEBUGLOG(4, "attaching dictionary into context");
cctx->blockState.matchState.dictMatchState = &cdict->matchState;
if (cctx->blockState.matchState.window.dictLimit < cdictEnd) {
cctx->blockState.matchState.window.nextSrc =
cctx->blockState.matchState.window.base + cdictEnd;
ZSTD_rust_windowClear(
(size_t)(cctx->blockState.matchState.window.nextSrc
- cctx->blockState.matchState.window.base),
&cctx->blockState.matchState.window.lowLimit,
&cctx->blockState.matchState.window.dictLimit);
}
/* loadedDictEnd is expressed within the active context referential. */
cctx->blockState.matchState.loadedDictEnd =
cctx->blockState.matchState.window.dictLimit;
}
static size_t
ZSTD_resetCCtx_byAttachingCDict(ZSTD_CCtx* cctx,
const ZSTD_CDict* cdict,
@@ -4542,13 +4518,30 @@ ZSTD_resetCCtx_byAttachingCDict(ZSTD_CCtx* cctx,
U64 pledgedSrcSize,
ZSTD_buffered_policy_e zbuff)
{
U32 const cdictEnd = (U32)(cdict->matchState.window.nextSrc
- cdict->matchState.window.base);
U32 const cdictLen = cdictEnd - cdict->matchState.window.dictLimit;
ZSTD_rust_resetCCtxByAttachingCDictState state;
state.callbackContext = cctx;
state.cdict = cdict;
state.params = &params;
state.pledgedSrcSize = pledgedSrcSize;
state.reset = ZSTD_rust_resetCCtx_byAttachingCDict_reset;
state.attach = ZSTD_rust_resetCCtx_byAttachingCDict_attach;
state.sourceMatchState = &cdict->matchState;
state.sourceCDictEnd = &cdictEnd;
state.sourceCDictLen = &cdictLen;
state.destinationDictMatchState =
&cctx->blockState.matchState.dictMatchState;
state.destinationWindowNextSrc =
&cctx->blockState.matchState.window.nextSrc;
state.destinationWindowBase =
&cctx->blockState.matchState.window.base;
state.destinationWindowLowLimit =
&cctx->blockState.matchState.window.lowLimit;
state.destinationWindowDictLimit =
&cctx->blockState.matchState.window.dictLimit;
state.destinationLoadedDictEnd =
&cctx->blockState.matchState.loadedDictEnd;
state.destinationDictID = &cctx->dictID;
state.sourceDictID = &cdict->dictID;
state.destinationDictContentSize = &cctx->dictContentSize;
+126 -23
View File
@@ -15,7 +15,7 @@ use crate::entropy_common::FSE_readNCount;
use crate::errors::{ERR_isError, ZstdErrorCode, ERROR};
use crate::fse_compress::FSE_buildCTable_wksp;
use crate::huf_compress::HUF_readCTable;
use crate::zstd_compress::ZSTD_rust_copyCDictTableIntoCCtx;
use crate::zstd_compress::{ZSTD_rust_copyCDictTableIntoCCtx, ZSTD_rust_windowClear};
use crate::zstd_compress_params::{
ZSTD_compressionParameters, ZSTD_frameParameters, ZSTD_parameters,
ZSTD_rust_params_allocateChainTable, ZSTD_rust_params_defaultCLevel,
@@ -1575,12 +1575,12 @@ pub unsafe extern "C" fn ZSTD_rust_resetCCtxByCopyingCDict(
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);
/// 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.
/// adjustment, private pointer-difference calculation, and the CCtx/CDict
/// field layout.
#[repr(C)]
pub struct ZSTD_rust_resetCCtxByAttachingCDictState {
callback_context: *mut c_void,
@@ -1588,7 +1588,15 @@ pub struct ZSTD_rust_resetCCtxByAttachingCDictState {
params: *const c_void,
pledged_src_size: u64,
reset: Option<ResetCCtxByAttachingCDictResetFn>,
attach: Option<ResetCCtxByAttachingCDictAttachFn>,
source_match_state: *const c_void,
source_cdict_end: *const c_uint,
source_cdict_len: *const c_uint,
destination_dict_match_state: *mut *const c_void,
destination_window_next_src: *mut *const c_void,
destination_window_base: *const *const c_void,
destination_window_low_limit: *mut c_uint,
destination_window_dict_limit: *mut c_uint,
destination_loaded_dict_end: *mut c_uint,
destination_dict_id: *mut c_uint,
source_dict_id: *const c_uint,
destination_dict_content_size: *mut usize,
@@ -1600,7 +1608,6 @@ pub struct ZSTD_rust_resetCCtxByAttachingCDictState {
const _: () = {
assert!(size_of::<ResetCCtxByAttachingCDictResetFn>() == size_of::<usize>());
assert!(size_of::<ResetCCtxByAttachingCDictAttachFn>() == 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>());
@@ -1614,7 +1621,7 @@ const _: () = {
);
assert!(
offset_of!(ZSTD_rust_resetCCtxByAttachingCDictState, zbuff)
== 3 * size_of::<usize>() + size_of::<u64>() + size_of::<[usize; 8]>()
== 3 * size_of::<usize>() + size_of::<u64>() + size_of::<[usize; 16]>()
);
assert!(
size_of::<ZSTD_rust_resetCCtxByAttachingCDictState>()
@@ -1624,8 +1631,8 @@ const _: () = {
);
};
/// Run the private CDict-attachment operation through C-owned layout callbacks
/// and copy the compressed-block state directly in Rust.
/// Run the private CDict-attachment operation through explicit C-owned field
/// projections and copy the compressed-block state directly in Rust.
#[no_mangle]
pub unsafe extern "C" fn ZSTD_rust_resetCCtxByAttachingCDict(
state: *const ZSTD_rust_resetCCtxByAttachingCDictState,
@@ -1634,12 +1641,21 @@ pub unsafe extern "C" fn ZSTD_rust_resetCCtxByAttachingCDict(
return ERROR(ZstdErrorCode::Generic);
}
let state = unsafe { &*state };
let (Some(reset), Some(attach)) = (state.reset, state.attach) else {
let Some(reset) = state.reset else {
return ERROR(ZstdErrorCode::Generic);
};
if state.callback_context.is_null()
|| state.cdict.is_null()
|| state.params.is_null()
|| state.source_match_state.is_null()
|| state.source_cdict_end.is_null()
|| state.source_cdict_len.is_null()
|| state.destination_dict_match_state.is_null()
|| state.destination_window_next_src.is_null()
|| state.destination_window_base.is_null()
|| state.destination_window_low_limit.is_null()
|| state.destination_window_dict_limit.is_null()
|| state.destination_loaded_dict_end.is_null()
|| state.destination_dict_id.is_null()
|| state.source_dict_id.is_null()
|| state.destination_dict_content_size.is_null()
@@ -1661,7 +1677,26 @@ pub unsafe extern "C" fn ZSTD_rust_resetCCtxByAttachingCDict(
if ERR_isError(reset_error) {
return reset_error;
}
attach(state.callback_context, state.cdict);
let cdict_end = *state.source_cdict_end;
if *state.source_cdict_len != 0 {
*state.destination_dict_match_state = state.source_match_state;
if *state.destination_window_dict_limit < cdict_end {
let destination_window_base = *state.destination_window_base;
if destination_window_base.is_null() {
return ERROR(ZstdErrorCode::Generic);
}
*state.destination_window_next_src = destination_window_base
.cast::<u8>()
.wrapping_add(cdict_end as usize)
.cast();
ZSTD_rust_windowClear(
cdict_end as usize,
state.destination_window_low_limit,
state.destination_window_dict_limit,
);
}
*state.destination_loaded_dict_end = *state.destination_window_dict_limit;
}
*state.destination_dict_id = *state.source_dict_id;
*state.destination_dict_content_size = *state.source_dict_content_size;
let destination_block_state = *state.destination_block_state;
@@ -3161,7 +3196,6 @@ mod tests {
assert_eq!(probe.events, ["reset"]);
}
#[derive(Default)]
struct ResetCCtxByAttachingCDictProbe {
events: Vec<&'static str>,
reset_result: usize,
@@ -3169,6 +3203,37 @@ mod tests {
params: *const c_void,
pledged_src_size: u64,
zbuff: c_int,
source_match_state: *const c_void,
source_cdict_end: c_uint,
source_cdict_len: c_uint,
destination_dict_match_state: *const c_void,
destination_window_next_src: *const c_void,
destination_window_base: *const c_void,
destination_window_low_limit: c_uint,
destination_window_dict_limit: c_uint,
destination_loaded_dict_end: c_uint,
}
impl Default for ResetCCtxByAttachingCDictProbe {
fn default() -> Self {
Self {
events: Vec::new(),
reset_result: 0,
cdict: ptr::null(),
params: ptr::null(),
pledged_src_size: 0,
zbuff: 0,
source_match_state: 0x5000usize as *const c_void,
source_cdict_end: 21,
source_cdict_len: 5,
destination_dict_match_state: ptr::null(),
destination_window_next_src: ptr::null(),
destination_window_base: 0x1000usize as *const c_void,
destination_window_low_limit: 3,
destination_window_dict_limit: 13,
destination_loaded_dict_end: 0,
}
}
}
unsafe fn reset_cctx_by_attaching_cdict_probe(
@@ -3193,15 +3258,6 @@ mod tests {
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");
}
fn reset_cctx_by_attaching_cdict_test_state(
probe: &mut ResetCCtxByAttachingCDictProbe,
cdict: *const c_void,
@@ -3219,7 +3275,15 @@ mod tests {
params,
pledged_src_size: 123,
reset: Some(reset_cctx_by_attaching_cdict_reset),
attach: Some(reset_cctx_by_attaching_cdict_attach),
source_match_state: probe.source_match_state,
source_cdict_end: &probe.source_cdict_end,
source_cdict_len: &probe.source_cdict_len,
destination_dict_match_state: &mut probe.destination_dict_match_state,
destination_window_next_src: &mut probe.destination_window_next_src,
destination_window_base: &probe.destination_window_base,
destination_window_low_limit: &mut probe.destination_window_low_limit,
destination_window_dict_limit: &mut probe.destination_window_dict_limit,
destination_loaded_dict_end: &mut probe.destination_loaded_dict_end,
destination_dict_id,
source_dict_id,
destination_dict_content_size,
@@ -3231,7 +3295,7 @@ mod tests {
}
#[test]
fn reset_cctx_by_attaching_cdict_runs_callbacks_in_original_order() {
fn reset_cctx_by_attaching_cdict_updates_window_in_rust_after_reset() {
let mut probe = ResetCCtxByAttachingCDictProbe::default();
let cdict = 0x4000usize as *const c_void;
let params = 0x3000usize as *const c_void;
@@ -3262,18 +3326,57 @@ mod tests {
let result = unsafe { ZSTD_rust_resetCCtxByAttachingCDict(&state) };
assert_eq!(result, 0);
assert_eq!(probe.events, ["reset", "attach"]);
assert_eq!(probe.events, ["reset"]);
assert_eq!(probe.cdict, cdict);
assert_eq!(probe.params, params);
assert_eq!(probe.pledged_src_size, 123);
assert_eq!(probe.zbuff, 7);
assert_eq!(destination_dict_id, source_dict_id);
assert_eq!(destination_dict_content_size, source_dict_content_size);
assert_eq!(probe.destination_dict_match_state, probe.source_match_state);
assert_eq!(
probe.destination_window_next_src,
0x1015usize as *const c_void
);
assert_eq!(probe.destination_window_low_limit, probe.source_cdict_end);
assert_eq!(probe.destination_window_dict_limit, probe.source_cdict_end);
assert_eq!(probe.destination_loaded_dict_end, probe.source_cdict_end);
assert_eq!(destination_block_state.rep, source_block_state.rep);
assert_eq!(
destination_block_state.entropy.huf.repeatMode,
source_block_state.entropy.huf.repeatMode
);
probe.events.clear();
probe.destination_window_next_src = 0x2000usize as *const c_void;
probe.destination_window_low_limit = 7;
probe.destination_window_dict_limit = 25;
probe.destination_loaded_dict_end = 0;
let result = unsafe { ZSTD_rust_resetCCtxByAttachingCDict(&state) };
assert_eq!(result, 0);
assert_eq!(probe.events, ["reset"]);
assert_eq!(probe.destination_dict_match_state, probe.source_match_state);
assert_eq!(
probe.destination_window_next_src,
0x2000usize as *const c_void
);
assert_eq!(probe.destination_window_low_limit, 7);
assert_eq!(probe.destination_window_dict_limit, 25);
assert_eq!(probe.destination_loaded_dict_end, 25);
probe.events.clear();
probe.source_cdict_len = 0;
probe.destination_dict_match_state = ptr::null();
probe.destination_loaded_dict_end = 99;
let result = unsafe { ZSTD_rust_resetCCtxByAttachingCDict(&state) };
assert_eq!(result, 0);
assert_eq!(probe.events, ["reset"]);
assert!(probe.destination_dict_match_state.is_null());
assert_eq!(probe.destination_loaded_dict_end, 99);
}
#[test]