feat(compress): move CCtx copy orchestration into Rust
Move the private CCtx-copy stage check, reset error handling, and callback ordering into a Rust projection while keeping C-owned context layout and table pointer arithmetic behind narrow callbacks. The table-size calculation now stays in the callback that runs after stage validation, preserving the copy-too-soon failure path instead of touching uninitialized source params. Test Plan: - ulimit -v 41943040 && CARGO_BUILD_JOBS=1 cargo test --manifest-path rust/Cargo.toml (713 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 (380 cases) - ulimit -v 41943040 && make -j1 -C tests test-zstream ZSTREAM_TESTTIME=-T2s (84 deterministic plus 109 randomized cases)
This commit is contained in:
@@ -1651,6 +1651,133 @@ 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 CopyCCtxCopyStateFn = unsafe extern "C" fn(*mut c_void, *const c_void);
|
||||
type CopyCCtxResetFn = unsafe extern "C" fn(
|
||||
*mut c_void,
|
||||
*const c_void,
|
||||
*const ZSTD_frameParameters,
|
||||
u64,
|
||||
c_int,
|
||||
) -> usize;
|
||||
type CopyCCtxMarkTablesFn = unsafe extern "C" fn(*mut c_void);
|
||||
type CopyCCtxCopyTablesFn = unsafe extern "C" fn(*mut c_void, *const c_void);
|
||||
|
||||
/// Projection for the private `ZSTD_copyCCtx_internal` operation.
|
||||
///
|
||||
/// Rust owns the stage/error branch and the order of the reset, workspace,
|
||||
/// table, dictionary, and block-state operations. C retains access to the
|
||||
/// private `ZSTD_CCtx` layout behind callbacks.
|
||||
#[repr(C)]
|
||||
pub struct ZSTD_rust_copyCCtxInternalState {
|
||||
callback_context: *mut c_void,
|
||||
src_cctx: *const c_void,
|
||||
f_params: *const ZSTD_frameParameters,
|
||||
pledged_src_size: u64,
|
||||
check_stage: Option<CopyCCtxCheckStageFn>,
|
||||
copy_custom_mem: Option<CopyCCtxCopyStateFn>,
|
||||
reset: Option<CopyCCtxResetFn>,
|
||||
mark_tables_dirty: Option<CopyCCtxMarkTablesFn>,
|
||||
copy_tables: Option<CopyCCtxCopyTablesFn>,
|
||||
mark_tables_clean: Option<CopyCCtxMarkTablesFn>,
|
||||
copy_match_state: Option<CopyCCtxCopyStateFn>,
|
||||
copy_dict_state: Option<CopyCCtxCopyStateFn>,
|
||||
copy_block_state: Option<CopyCCtxCopyStateFn>,
|
||||
zbuff: c_int,
|
||||
}
|
||||
|
||||
const _: () = {
|
||||
assert!(size_of::<CopyCCtxCheckStageFn>() == size_of::<usize>());
|
||||
assert!(size_of::<CopyCCtxCopyStateFn>() == size_of::<usize>());
|
||||
assert!(size_of::<CopyCCtxResetFn>() == size_of::<usize>());
|
||||
assert!(size_of::<CopyCCtxMarkTablesFn>() == size_of::<usize>());
|
||||
assert!(size_of::<CopyCCtxCopyTablesFn>() == size_of::<usize>());
|
||||
assert!(offset_of!(ZSTD_rust_copyCCtxInternalState, callback_context) == 0);
|
||||
assert!(offset_of!(ZSTD_rust_copyCCtxInternalState, src_cctx) == size_of::<usize>());
|
||||
assert!(offset_of!(ZSTD_rust_copyCCtxInternalState, f_params) == 2 * size_of::<usize>());
|
||||
assert!(
|
||||
offset_of!(ZSTD_rust_copyCCtxInternalState, pledged_src_size) == 3 * size_of::<usize>()
|
||||
);
|
||||
assert!(
|
||||
offset_of!(ZSTD_rust_copyCCtxInternalState, check_stage)
|
||||
== 3 * size_of::<usize>() + size_of::<u64>()
|
||||
);
|
||||
assert!(
|
||||
offset_of!(ZSTD_rust_copyCCtxInternalState, zbuff)
|
||||
== 3 * size_of::<usize>() + size_of::<u64>() + 9 * size_of::<usize>()
|
||||
);
|
||||
assert!(
|
||||
size_of::<ZSTD_rust_copyCCtxInternalState>()
|
||||
== (offset_of!(ZSTD_rust_copyCCtxInternalState, zbuff) + size_of::<c_int>())
|
||||
.div_ceil(size_of::<usize>())
|
||||
* size_of::<usize>()
|
||||
);
|
||||
};
|
||||
|
||||
/// Run the private context-copy operation through C-owned layout callbacks.
|
||||
#[no_mangle]
|
||||
pub unsafe extern "C" fn ZSTD_rust_copyCCtxInternal(
|
||||
state: *const ZSTD_rust_copyCCtxInternalState,
|
||||
) -> usize {
|
||||
if state.is_null() {
|
||||
return ERROR(ZstdErrorCode::Generic);
|
||||
}
|
||||
let state = unsafe { &*state };
|
||||
let (
|
||||
Some(check_stage),
|
||||
Some(copy_custom_mem),
|
||||
Some(reset),
|
||||
Some(mark_tables_dirty),
|
||||
Some(copy_tables),
|
||||
Some(mark_tables_clean),
|
||||
Some(copy_match_state),
|
||||
Some(copy_dict_state),
|
||||
Some(copy_block_state),
|
||||
) = (
|
||||
state.check_stage,
|
||||
state.copy_custom_mem,
|
||||
state.reset,
|
||||
state.mark_tables_dirty,
|
||||
state.copy_tables,
|
||||
state.mark_tables_clean,
|
||||
state.copy_match_state,
|
||||
state.copy_dict_state,
|
||||
state.copy_block_state,
|
||||
)
|
||||
else {
|
||||
return ERROR(ZstdErrorCode::Generic);
|
||||
};
|
||||
if state.callback_context.is_null() || state.src_cctx.is_null() || state.f_params.is_null() {
|
||||
return ERROR(ZstdErrorCode::Generic);
|
||||
}
|
||||
|
||||
let stage_error = unsafe { check_stage(state.callback_context, state.src_cctx) };
|
||||
if ERR_isError(stage_error) {
|
||||
return stage_error;
|
||||
}
|
||||
|
||||
unsafe {
|
||||
copy_custom_mem(state.callback_context, state.src_cctx);
|
||||
let reset_error = reset(
|
||||
state.callback_context,
|
||||
state.src_cctx,
|
||||
state.f_params,
|
||||
state.pledged_src_size,
|
||||
state.zbuff,
|
||||
);
|
||||
if ERR_isError(reset_error) {
|
||||
return reset_error;
|
||||
}
|
||||
mark_tables_dirty(state.callback_context);
|
||||
copy_tables(state.callback_context, state.src_cctx);
|
||||
mark_tables_clean(state.callback_context);
|
||||
copy_match_state(state.callback_context, state.src_cctx);
|
||||
copy_dict_state(state.callback_context, state.src_cctx);
|
||||
copy_block_state(state.callback_context, state.src_cctx);
|
||||
}
|
||||
0
|
||||
}
|
||||
|
||||
type CompressAdvancedInitParamsFn = unsafe extern "C" fn(*mut c_void, *const ZSTD_parameters);
|
||||
type CompressAdvancedInternalFn = unsafe extern "C" fn(
|
||||
*mut c_void,
|
||||
@@ -13094,6 +13221,159 @@ mod tests {
|
||||
assert_eq!(context.frame_params.noDictIDFlag, 1);
|
||||
}
|
||||
|
||||
#[derive(Default)]
|
||||
struct CopyCCtxInternalTestContext {
|
||||
events: Vec<&'static str>,
|
||||
stage_result: usize,
|
||||
reset_result: usize,
|
||||
frame_params: ZSTD_frameParameters,
|
||||
pledged_src_size: u64,
|
||||
zbuff: c_int,
|
||||
}
|
||||
|
||||
unsafe fn copy_cctx_internal_test_context(
|
||||
context: *mut c_void,
|
||||
) -> &'static mut CopyCCtxInternalTestContext {
|
||||
unsafe { &mut *context.cast::<CopyCCtxInternalTestContext>() }
|
||||
}
|
||||
|
||||
unsafe extern "C" fn copy_cctx_internal_test_check_stage(
|
||||
context: *mut c_void,
|
||||
_src_cctx: *const c_void,
|
||||
) -> usize {
|
||||
let context = unsafe { copy_cctx_internal_test_context(context) };
|
||||
context.events.push("check");
|
||||
context.stage_result
|
||||
}
|
||||
|
||||
unsafe extern "C" fn copy_cctx_internal_test_copy_state(
|
||||
context: *mut c_void,
|
||||
_src_cctx: *const c_void,
|
||||
) {
|
||||
let context = unsafe { copy_cctx_internal_test_context(context) };
|
||||
let event = match context.events.len() {
|
||||
1 => "custom",
|
||||
6 => "match",
|
||||
7 => "dict",
|
||||
8 => "block",
|
||||
_ => panic!("unexpected copy-state callback order"),
|
||||
};
|
||||
context.events.push(event);
|
||||
}
|
||||
|
||||
unsafe extern "C" fn copy_cctx_internal_test_reset(
|
||||
context: *mut c_void,
|
||||
_src_cctx: *const c_void,
|
||||
f_params: *const ZSTD_frameParameters,
|
||||
pledged_src_size: u64,
|
||||
zbuff: c_int,
|
||||
) -> usize {
|
||||
let context = unsafe { copy_cctx_internal_test_context(context) };
|
||||
context.events.push("reset");
|
||||
context.frame_params = unsafe { *f_params };
|
||||
context.pledged_src_size = pledged_src_size;
|
||||
context.zbuff = zbuff;
|
||||
context.reset_result
|
||||
}
|
||||
|
||||
unsafe extern "C" fn copy_cctx_internal_test_mark_tables_dirty(context: *mut c_void) {
|
||||
unsafe { copy_cctx_internal_test_context(context) }
|
||||
.events
|
||||
.push("dirty");
|
||||
}
|
||||
|
||||
unsafe extern "C" fn copy_cctx_internal_test_copy_tables(
|
||||
context: *mut c_void,
|
||||
_src_cctx: *const c_void,
|
||||
) {
|
||||
let context = unsafe { copy_cctx_internal_test_context(context) };
|
||||
context.events.push("tables");
|
||||
}
|
||||
|
||||
unsafe extern "C" fn copy_cctx_internal_test_mark_tables_clean(context: *mut c_void) {
|
||||
unsafe { copy_cctx_internal_test_context(context) }
|
||||
.events
|
||||
.push("clean");
|
||||
}
|
||||
|
||||
fn copy_cctx_internal_test_state(
|
||||
context: &mut CopyCCtxInternalTestContext,
|
||||
src_cctx: *const c_void,
|
||||
f_params: &ZSTD_frameParameters,
|
||||
) -> ZSTD_rust_copyCCtxInternalState {
|
||||
ZSTD_rust_copyCCtxInternalState {
|
||||
callback_context: (context as *mut CopyCCtxInternalTestContext).cast(),
|
||||
src_cctx,
|
||||
f_params,
|
||||
pledged_src_size: 123,
|
||||
check_stage: Some(copy_cctx_internal_test_check_stage),
|
||||
copy_custom_mem: Some(copy_cctx_internal_test_copy_state),
|
||||
reset: Some(copy_cctx_internal_test_reset),
|
||||
mark_tables_dirty: Some(copy_cctx_internal_test_mark_tables_dirty),
|
||||
copy_tables: Some(copy_cctx_internal_test_copy_tables),
|
||||
mark_tables_clean: Some(copy_cctx_internal_test_mark_tables_clean),
|
||||
copy_match_state: Some(copy_cctx_internal_test_copy_state),
|
||||
copy_dict_state: Some(copy_cctx_internal_test_copy_state),
|
||||
copy_block_state: Some(copy_cctx_internal_test_copy_state),
|
||||
zbuff: 7,
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn copy_cctx_internal_runs_private_callbacks_in_original_order() {
|
||||
let mut context = CopyCCtxInternalTestContext::default();
|
||||
let f_params = ZSTD_frameParameters {
|
||||
contentSizeFlag: 1,
|
||||
checksumFlag: 1,
|
||||
noDictIDFlag: 1,
|
||||
};
|
||||
let state =
|
||||
copy_cctx_internal_test_state(&mut context, 0x5000usize as *const c_void, &f_params);
|
||||
|
||||
let result = unsafe { ZSTD_rust_copyCCtxInternal(&state) };
|
||||
|
||||
assert_eq!(result, 0);
|
||||
assert_eq!(
|
||||
context.events,
|
||||
["check", "custom", "reset", "dirty", "tables", "clean", "match", "dict", "block"]
|
||||
);
|
||||
assert_eq!(context.frame_params, f_params);
|
||||
assert_eq!(context.pledged_src_size, 123);
|
||||
assert_eq!(context.zbuff, 7);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn copy_cctx_internal_rejects_stage_before_mutation() {
|
||||
let mut context = CopyCCtxInternalTestContext {
|
||||
stage_result: ERROR(ZstdErrorCode::StageWrong),
|
||||
..CopyCCtxInternalTestContext::default()
|
||||
};
|
||||
let f_params = ZSTD_frameParameters::default();
|
||||
let state =
|
||||
copy_cctx_internal_test_state(&mut context, 0x5000usize as *const c_void, &f_params);
|
||||
|
||||
let result = unsafe { ZSTD_rust_copyCCtxInternal(&state) };
|
||||
|
||||
assert_eq!(result, ERROR(ZstdErrorCode::StageWrong));
|
||||
assert_eq!(context.events, ["check"]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn copy_cctx_internal_propagates_reset_error_before_table_mutation() {
|
||||
let mut context = CopyCCtxInternalTestContext {
|
||||
reset_result: ERROR(ZstdErrorCode::MemoryAllocation),
|
||||
..CopyCCtxInternalTestContext::default()
|
||||
};
|
||||
let f_params = ZSTD_frameParameters::default();
|
||||
let state =
|
||||
copy_cctx_internal_test_state(&mut context, 0x5000usize as *const c_void, &f_params);
|
||||
|
||||
let result = unsafe { ZSTD_rust_copyCCtxInternal(&state) };
|
||||
|
||||
assert_eq!(result, ERROR(ZstdErrorCode::MemoryAllocation));
|
||||
assert_eq!(context.events, ["check", "custom", "reset"]);
|
||||
}
|
||||
|
||||
#[derive(Default)]
|
||||
struct CompressAdvancedTestContext {
|
||||
events: Vec<&'static str>,
|
||||
|
||||
Reference in New Issue
Block a user