feat(compress): move CCtx custom memory copy into Rust

Replace the remaining CCtx-copy custom-memory callback with explicit source and
destination pointers. Rust now copies the ABI-checked ZSTD_customMem payload in
its original position before reset, while C retains private context access and
the surrounding workspace and table operations.

Test Plan:
- ulimit -v 41943040 cargo fmt --manifest-path rust/Cargo.toml -- --check
- ulimit -v 41943040 CARGO_BUILD_JOBS=1 cargo clippy --manifest-path rust/Cargo.toml --all-targets -- -D warnings
- ulimit -v 41943040 CARGO_BUILD_JOBS=1 cargo test --manifest-path rust/Cargo.toml
- ulimit -v 41943040 make -j1
- ulimit -v 41943040 make -j1 -C tests test-zstream ZSTREAM_TESTTIME=-T2s
- ulimit -v 41943040 make -j1 -C tests test-fuzzer FUZZERTEST=-T3s FUZZER_FLAGS=--no-big-tests
This commit is contained in:
2026-07-19 22:53:15 +02:00
parent 7eb70baec1
commit 9ab4982780
2 changed files with 41 additions and 33 deletions
+7 -13
View File
@@ -459,8 +459,6 @@ typedef char ZSTD_rust_reset_cctx_state_layout[
? 1 : -1]; ? 1 : -1];
typedef size_t (*ZSTD_rust_copyCCtxCheckStage_f)( typedef size_t (*ZSTD_rust_copyCCtxCheckStage_f)(
void* context, const void* srcCCtx); void* context, const void* srcCCtx);
typedef void (*ZSTD_rust_copyCCtxCopyState_f)(
void* context, const void* srcCCtx);
typedef size_t (*ZSTD_rust_copyCCtxReset_f)( typedef size_t (*ZSTD_rust_copyCCtxReset_f)(
void* context, const void* srcCCtx, void* context, const void* srcCCtx,
const ZSTD_frameParameters* fParams, const ZSTD_frameParameters* fParams,
@@ -474,7 +472,8 @@ typedef struct {
const ZSTD_frameParameters* fParams; const ZSTD_frameParameters* fParams;
U64 pledgedSrcSize; U64 pledgedSrcSize;
ZSTD_rust_copyCCtxCheckStage_f checkStage; ZSTD_rust_copyCCtxCheckStage_f checkStage;
ZSTD_rust_copyCCtxCopyState_f copyCustomMem; void* destinationCustomMem;
const void* sourceCustomMem;
ZSTD_rust_copyCCtxReset_f reset; ZSTD_rust_copyCCtxReset_f reset;
ZSTD_rust_copyCCtxMarkTables_f markTablesDirty; ZSTD_rust_copyCCtxMarkTables_f markTablesDirty;
ZSTD_rust_copyCCtxCopyTables_f copyTables; ZSTD_rust_copyCCtxCopyTables_f copyTables;
@@ -536,7 +535,7 @@ typedef char ZSTD_rust_copy_cctx_internal_state_layout[
== 3 * sizeof(void*) + sizeof(U64) == 3 * sizeof(void*) + sizeof(U64)
&& offsetof(ZSTD_rust_copyCCtxInternalState, zbuff) && offsetof(ZSTD_rust_copyCCtxInternalState, zbuff)
== 3 * sizeof(void*) + sizeof(U64) == 3 * sizeof(void*) + sizeof(U64)
+ 18 * sizeof(void*) + 19 * sizeof(void*)
&& sizeof(ZSTD_rust_copyCCtxInternalState) && sizeof(ZSTD_rust_copyCCtxInternalState)
== ((offsetof(ZSTD_rust_copyCCtxInternalState, zbuff) == ((offsetof(ZSTD_rust_copyCCtxInternalState, zbuff)
+ sizeof(int) + sizeof(void*) - 1) / sizeof(void*)) + sizeof(int) + sizeof(void*) - 1) / sizeof(void*))
@@ -4756,14 +4755,6 @@ static size_t ZSTD_rust_copyCCtx_check_stage(
return 0; return 0;
} }
static void ZSTD_rust_copyCCtx_copy_custom_mem(
void* context, const void* srcCCtx)
{
ZSTD_CCtx* const dst = (ZSTD_CCtx*)context;
const ZSTD_CCtx* const src = (const ZSTD_CCtx*)srcCCtx;
ZSTD_memcpy(&dst->customMem, &src->customMem, sizeof(ZSTD_customMem));
}
static size_t ZSTD_rust_copyCCtx_reset( static size_t ZSTD_rust_copyCCtx_reset(
void* context, const void* srcCCtx, void* context, const void* srcCCtx,
const ZSTD_frameParameters* fParams, const ZSTD_frameParameters* fParams,
@@ -4846,7 +4837,10 @@ static size_t ZSTD_rust_copyCCtx_internal_callback(
state.fParams = fParams; state.fParams = fParams;
state.pledgedSrcSize = pledgedSrcSize; state.pledgedSrcSize = pledgedSrcSize;
state.checkStage = ZSTD_rust_copyCCtx_check_stage; state.checkStage = ZSTD_rust_copyCCtx_check_stage;
state.copyCustomMem = ZSTD_rust_copyCCtx_copy_custom_mem; state.destinationCustomMem =
&((ZSTD_CCtx*)context)->customMem;
state.sourceCustomMem =
&((const ZSTD_CCtx*)srcCCtx)->customMem;
state.reset = ZSTD_rust_copyCCtx_reset; state.reset = ZSTD_rust_copyCCtx_reset;
state.markTablesDirty = ZSTD_rust_copyCCtx_mark_tables_dirty; state.markTablesDirty = ZSTD_rust_copyCCtx_mark_tables_dirty;
state.copyTables = ZSTD_rust_copyCCtx_copy_tables; state.copyTables = ZSTD_rust_copyCCtx_copy_tables;
+34 -20
View File
@@ -32,7 +32,7 @@ use crate::zstd_compress_params::{
ZSTD_RUST_CPM_NO_ATTACH_DICT, ZSTD_RUST_PS_AUTO, ZSTD_RUST_PS_DISABLE, ZSTD_RUST_PS_ENABLE, ZSTD_RUST_CPM_NO_ATTACH_DICT, ZSTD_RUST_PS_AUTO, ZSTD_RUST_PS_DISABLE, ZSTD_RUST_PS_ENABLE,
}; };
use crate::zstd_compress_params_api::{ use crate::zstd_compress_params_api::{
ZSTD_CCtxParams_setParameter, ZSTD_CCtx_params, ZSTD_rust_isUpdateAuthorized, ZSTD_CCtxParams_setParameter, ZSTD_CCtx_params, ZSTD_customMem, ZSTD_rust_isUpdateAuthorized,
}; };
use crate::zstd_compress_sequences::SeqDef; use crate::zstd_compress_sequences::SeqDef;
use crate::zstd_compress_stats::{ use crate::zstd_compress_stats::{
@@ -1673,7 +1673,6 @@ pub unsafe extern "C" fn ZSTD_rust_copyCCtx(state: *const ZSTD_rust_copyCCtxStat
} }
type CopyCCtxCheckStageFn = unsafe extern "C" fn(*mut c_void, *const c_void) -> usize; type CopyCCtxCheckStageFn = unsafe extern "C" fn(*mut c_void, *const c_void) -> usize;
type CopyCCtxCopyStateFn = unsafe extern "C" fn(*mut c_void, *const c_void);
type CopyCCtxResetFn = unsafe extern "C" fn( type CopyCCtxResetFn = unsafe extern "C" fn(
*mut c_void, *mut c_void,
*const c_void, *const c_void,
@@ -1715,6 +1714,10 @@ const _: () = {
); );
}; };
const _: () = {
assert!(size_of::<ZSTD_customMem>() == 3 * size_of::<usize>());
};
/// Projection for the private `ZSTD_copyCCtx_internal` operation. /// Projection for the private `ZSTD_copyCCtx_internal` operation.
/// ///
/// Rust owns the stage/error branch and the order of the reset, workspace, /// Rust owns the stage/error branch and the order of the reset, workspace,
@@ -1727,7 +1730,8 @@ pub struct ZSTD_rust_copyCCtxInternalState {
f_params: *const ZSTD_frameParameters, f_params: *const ZSTD_frameParameters,
pledged_src_size: u64, pledged_src_size: u64,
check_stage: Option<CopyCCtxCheckStageFn>, check_stage: Option<CopyCCtxCheckStageFn>,
copy_custom_mem: Option<CopyCCtxCopyStateFn>, destination_custom_mem: *mut c_void,
source_custom_mem: *const c_void,
reset: Option<CopyCCtxResetFn>, reset: Option<CopyCCtxResetFn>,
mark_tables_dirty: Option<CopyCCtxMarkTablesFn>, mark_tables_dirty: Option<CopyCCtxMarkTablesFn>,
copy_tables: Option<CopyCCtxCopyTablesFn>, copy_tables: Option<CopyCCtxCopyTablesFn>,
@@ -1749,7 +1753,6 @@ pub struct ZSTD_rust_copyCCtxInternalState {
const _: () = { const _: () = {
assert!(size_of::<CopyCCtxCheckStageFn>() == size_of::<usize>()); assert!(size_of::<CopyCCtxCheckStageFn>() == size_of::<usize>());
assert!(size_of::<CopyCCtxCopyStateFn>() == size_of::<usize>());
assert!(size_of::<CopyCCtxResetFn>() == size_of::<usize>()); assert!(size_of::<CopyCCtxResetFn>() == size_of::<usize>());
assert!(size_of::<CopyCCtxMarkTablesFn>() == size_of::<usize>()); assert!(size_of::<CopyCCtxMarkTablesFn>() == size_of::<usize>());
assert!(size_of::<CopyCCtxCopyTablesFn>() == size_of::<usize>()); assert!(size_of::<CopyCCtxCopyTablesFn>() == size_of::<usize>());
@@ -1765,7 +1768,7 @@ const _: () = {
); );
assert!( assert!(
offset_of!(ZSTD_rust_copyCCtxInternalState, zbuff) offset_of!(ZSTD_rust_copyCCtxInternalState, zbuff)
== 3 * size_of::<usize>() + size_of::<u64>() + 18 * size_of::<usize>() == 3 * size_of::<usize>() + size_of::<u64>() + 19 * size_of::<usize>()
); );
assert!( assert!(
size_of::<ZSTD_rust_copyCCtxInternalState>() size_of::<ZSTD_rust_copyCCtxInternalState>()
@@ -1787,14 +1790,12 @@ pub unsafe extern "C" fn ZSTD_rust_copyCCtxInternal(
let state = unsafe { &*state }; let state = unsafe { &*state };
let ( let (
Some(check_stage), Some(check_stage),
Some(copy_custom_mem),
Some(reset), Some(reset),
Some(mark_tables_dirty), Some(mark_tables_dirty),
Some(copy_tables), Some(copy_tables),
Some(mark_tables_clean), Some(mark_tables_clean),
) = ( ) = (
state.check_stage, state.check_stage,
state.copy_custom_mem,
state.reset, state.reset,
state.mark_tables_dirty, state.mark_tables_dirty,
state.copy_tables, state.copy_tables,
@@ -1806,6 +1807,8 @@ pub unsafe extern "C" fn ZSTD_rust_copyCCtxInternal(
if state.callback_context.is_null() if state.callback_context.is_null()
|| state.src_cctx.is_null() || state.src_cctx.is_null()
|| state.f_params.is_null() || state.f_params.is_null()
|| state.destination_custom_mem.is_null()
|| state.source_custom_mem.is_null()
|| state.destination_window.is_null() || state.destination_window.is_null()
|| state.source_window.is_null() || state.source_window.is_null()
|| state.destination_next_to_update.is_null() || state.destination_next_to_update.is_null()
@@ -1828,7 +1831,11 @@ pub unsafe extern "C" fn ZSTD_rust_copyCCtxInternal(
} }
unsafe { unsafe {
copy_custom_mem(state.callback_context, state.src_cctx); ptr::copy(
state.source_custom_mem.cast::<ZSTD_customMem>(),
state.destination_custom_mem.cast::<ZSTD_customMem>(),
1,
);
let reset_error = reset( let reset_error = reset(
state.callback_context, state.callback_context,
state.src_cctx, state.src_cctx,
@@ -13915,15 +13922,6 @@ mod tests {
context.stage_result context.stage_result
} }
unsafe extern "C" fn copy_cctx_internal_test_copy_state(
context: *mut c_void,
_src_cctx: *const c_void,
) {
unsafe { copy_cctx_internal_test_context(context) }
.events
.push("custom");
}
unsafe extern "C" fn copy_cctx_internal_test_reset( unsafe extern "C" fn copy_cctx_internal_test_reset(
context: *mut c_void, context: *mut c_void,
_src_cctx: *const c_void, _src_cctx: *const c_void,
@@ -13963,6 +13961,8 @@ mod tests {
context: &mut CopyCCtxInternalTestContext, context: &mut CopyCCtxInternalTestContext,
src_cctx: *const c_void, src_cctx: *const c_void,
f_params: &ZSTD_frameParameters, f_params: &ZSTD_frameParameters,
destination_custom_mem: *mut c_void,
source_custom_mem: *const c_void,
destination_window: *mut c_void, destination_window: *mut c_void,
source_window: *const c_void, source_window: *const c_void,
destination_next_to_update: *mut c_uint, destination_next_to_update: *mut c_uint,
@@ -13982,7 +13982,8 @@ mod tests {
f_params, f_params,
pledged_src_size: 123, pledged_src_size: 123,
check_stage: Some(copy_cctx_internal_test_check_stage), check_stage: Some(copy_cctx_internal_test_check_stage),
copy_custom_mem: Some(copy_cctx_internal_test_copy_state), destination_custom_mem,
source_custom_mem,
reset: Some(copy_cctx_internal_test_reset), reset: Some(copy_cctx_internal_test_reset),
mark_tables_dirty: Some(copy_cctx_internal_test_mark_tables_dirty), mark_tables_dirty: Some(copy_cctx_internal_test_mark_tables_dirty),
copy_tables: Some(copy_cctx_internal_test_copy_tables), copy_tables: Some(copy_cctx_internal_test_copy_tables),
@@ -14011,6 +14012,8 @@ mod tests {
checksumFlag: 1, checksumFlag: 1,
noDictIDFlag: 1, noDictIDFlag: 1,
}; };
let source_custom_mem = [0x11usize, 0x22, 0x33];
let mut destination_custom_mem = [0usize; 3];
let mut source_block_state = let mut source_block_state =
unsafe { MaybeUninit::<ZSTD_compressedBlockState_t>::zeroed().assume_init() }; unsafe { MaybeUninit::<ZSTD_compressedBlockState_t>::zeroed().assume_init() };
source_block_state.rep = [15, 16, 17]; source_block_state.rep = [15, 16, 17];
@@ -14048,6 +14051,8 @@ mod tests {
&mut context, &mut context,
0x5000usize as *const c_void, 0x5000usize as *const c_void,
&f_params, &f_params,
(&mut destination_custom_mem as *mut [usize; 3]).cast(),
(&source_custom_mem as *const [usize; 3]).cast(),
(&mut destination_window as *mut ZSTD_rust_copyWindowState).cast(), (&mut destination_window as *mut ZSTD_rust_copyWindowState).cast(),
(&source_window as *const ZSTD_rust_copyWindowState).cast(), (&source_window as *const ZSTD_rust_copyWindowState).cast(),
&mut destination_next_to_update, &mut destination_next_to_update,
@@ -14067,11 +14072,12 @@ mod tests {
assert_eq!(result, 0); assert_eq!(result, 0);
assert_eq!( assert_eq!(
context.events, context.events,
["check", "custom", "reset", "dirty", "tables", "clean"] ["check", "reset", "dirty", "tables", "clean"]
); );
assert_eq!(context.frame_params, f_params); assert_eq!(context.frame_params, f_params);
assert_eq!(context.pledged_src_size, 123); assert_eq!(context.pledged_src_size, 123);
assert_eq!(context.zbuff, 7); assert_eq!(context.zbuff, 7);
assert_eq!(destination_custom_mem, source_custom_mem);
assert_eq!(destination_window, source_window); assert_eq!(destination_window, source_window);
assert_eq!(destination_next_to_update, source_next_to_update); assert_eq!(destination_next_to_update, source_next_to_update);
assert_eq!(destination_loaded_dict_end, source_loaded_dict_end); assert_eq!(destination_loaded_dict_end, source_loaded_dict_end);
@@ -14095,6 +14101,8 @@ mod tests {
..CopyCCtxInternalTestContext::default() ..CopyCCtxInternalTestContext::default()
}; };
let f_params = ZSTD_frameParameters::default(); let f_params = ZSTD_frameParameters::default();
let source_custom_mem = [0usize; 3];
let mut destination_custom_mem = [0usize; 3];
let mut destination_block_state = let mut destination_block_state =
unsafe { MaybeUninit::<ZSTD_compressedBlockState_t>::zeroed().assume_init() }; unsafe { MaybeUninit::<ZSTD_compressedBlockState_t>::zeroed().assume_init() };
let mut destination_block_state_slot = let mut destination_block_state_slot =
@@ -14109,6 +14117,8 @@ mod tests {
&mut context, &mut context,
0x5000usize as *const c_void, 0x5000usize as *const c_void,
&f_params, &f_params,
(&mut destination_custom_mem as *mut [usize; 3]).cast(),
(&source_custom_mem as *const [usize; 3]).cast(),
ptr::dangling_mut(), ptr::dangling_mut(),
ptr::dangling(), ptr::dangling(),
ptr::dangling_mut(), ptr::dangling_mut(),
@@ -14136,6 +14146,8 @@ mod tests {
..CopyCCtxInternalTestContext::default() ..CopyCCtxInternalTestContext::default()
}; };
let f_params = ZSTD_frameParameters::default(); let f_params = ZSTD_frameParameters::default();
let source_custom_mem = [0usize; 3];
let mut destination_custom_mem = [0usize; 3];
let mut destination_block_state = let mut destination_block_state =
unsafe { MaybeUninit::<ZSTD_compressedBlockState_t>::zeroed().assume_init() }; unsafe { MaybeUninit::<ZSTD_compressedBlockState_t>::zeroed().assume_init() };
let mut destination_block_state_slot = let mut destination_block_state_slot =
@@ -14150,6 +14162,8 @@ mod tests {
&mut context, &mut context,
0x5000usize as *const c_void, 0x5000usize as *const c_void,
&f_params, &f_params,
(&mut destination_custom_mem as *mut [usize; 3]).cast(),
(&source_custom_mem as *const [usize; 3]).cast(),
ptr::dangling_mut(), ptr::dangling_mut(),
ptr::dangling(), ptr::dangling(),
ptr::dangling_mut(), ptr::dangling_mut(),
@@ -14167,7 +14181,7 @@ mod tests {
let result = unsafe { ZSTD_rust_copyCCtxInternal(&state) }; let result = unsafe { ZSTD_rust_copyCCtxInternal(&state) };
assert_eq!(result, ERROR(ZstdErrorCode::MemoryAllocation)); assert_eq!(result, ERROR(ZstdErrorCode::MemoryAllocation));
assert_eq!(context.events, ["check", "custom", "reset"]); assert_eq!(context.events, ["check", "reset"]);
} }
#[derive(Default)] #[derive(Default)]