feat(compress): move CDict copy reset orchestration into Rust
Move the prepared-CDict-to-CCtx reset sequence into a Rust projection with explicit reset, table, hashTable3, dictionary, and block-state ordering. C retains private layouts, table-tag removal, row-matchfinder metadata, and all pointer arithmetic behind callbacks, while reset allocation errors now stop the sequence before table mutation. Test Plan: - ulimit -v 41943040 && CARGO_BUILD_JOBS=1 cargo test --manifest-path rust/Cargo.toml (715 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 (305 cases) - ulimit -v 41943040 && make -j1 -C tests test-zstream ZSTREAM_TESTTIME=-T2s (84 deterministic plus 248 randomized cases)
This commit is contained in:
@@ -1294,6 +1294,117 @@ pub unsafe extern "C" fn ZSTD_rust_resetCCtxUsingCDict(
|
||||
}
|
||||
}
|
||||
|
||||
type ResetCCtxByCopyingCDictResetFn =
|
||||
unsafe extern "C" fn(*mut c_void, *const c_void, *const c_void, u64, c_int) -> usize;
|
||||
type ResetCCtxByCopyingCDictStateFn = unsafe extern "C" fn(*mut c_void, *const c_void);
|
||||
type ResetCCtxByCopyingCDictMarkTablesFn = unsafe extern "C" fn(*mut c_void);
|
||||
|
||||
/// Projection for copying a prepared CDict into a working CCtx.
|
||||
///
|
||||
/// Rust owns the reset/copy ordering and stops on reset allocation failure.
|
||||
/// C retains the private CCtx/CDict layout, table-size policy, and pointer
|
||||
/// arithmetic behind callbacks.
|
||||
#[repr(C)]
|
||||
pub struct ZSTD_rust_resetCCtxByCopyingCDictState {
|
||||
callback_context: *mut c_void,
|
||||
cdict: *const c_void,
|
||||
params: *const c_void,
|
||||
pledged_src_size: u64,
|
||||
reset: Option<ResetCCtxByCopyingCDictResetFn>,
|
||||
mark_tables_dirty: Option<ResetCCtxByCopyingCDictMarkTablesFn>,
|
||||
copy_tables: Option<ResetCCtxByCopyingCDictStateFn>,
|
||||
zero_hash_table3: Option<ResetCCtxByCopyingCDictStateFn>,
|
||||
mark_tables_clean: Option<ResetCCtxByCopyingCDictMarkTablesFn>,
|
||||
copy_match_state: Option<ResetCCtxByCopyingCDictStateFn>,
|
||||
copy_dict_state: Option<ResetCCtxByCopyingCDictStateFn>,
|
||||
copy_block_state: Option<ResetCCtxByCopyingCDictStateFn>,
|
||||
zbuff: c_int,
|
||||
}
|
||||
|
||||
const _: () = {
|
||||
assert!(size_of::<ResetCCtxByCopyingCDictResetFn>() == size_of::<usize>());
|
||||
assert!(size_of::<ResetCCtxByCopyingCDictStateFn>() == size_of::<usize>());
|
||||
assert!(size_of::<ResetCCtxByCopyingCDictMarkTablesFn>() == size_of::<usize>());
|
||||
assert!(offset_of!(ZSTD_rust_resetCCtxByCopyingCDictState, callback_context) == 0);
|
||||
assert!(offset_of!(ZSTD_rust_resetCCtxByCopyingCDictState, cdict) == size_of::<usize>());
|
||||
assert!(offset_of!(ZSTD_rust_resetCCtxByCopyingCDictState, params) == 2 * size_of::<usize>());
|
||||
assert!(
|
||||
offset_of!(ZSTD_rust_resetCCtxByCopyingCDictState, pledged_src_size)
|
||||
== 3 * size_of::<usize>()
|
||||
);
|
||||
assert!(
|
||||
offset_of!(ZSTD_rust_resetCCtxByCopyingCDictState, reset)
|
||||
== 3 * size_of::<usize>() + size_of::<u64>()
|
||||
);
|
||||
assert!(
|
||||
offset_of!(ZSTD_rust_resetCCtxByCopyingCDictState, zbuff)
|
||||
== 3 * size_of::<usize>() + size_of::<u64>() + size_of::<[usize; 8]>()
|
||||
);
|
||||
assert!(
|
||||
size_of::<ZSTD_rust_resetCCtxByCopyingCDictState>()
|
||||
== (offset_of!(ZSTD_rust_resetCCtxByCopyingCDictState, zbuff) + size_of::<c_int>())
|
||||
.div_ceil(size_of::<usize>())
|
||||
* size_of::<usize>()
|
||||
);
|
||||
};
|
||||
|
||||
/// Run the private CDict-copy operation through C-owned layout callbacks.
|
||||
#[no_mangle]
|
||||
pub unsafe extern "C" fn ZSTD_rust_resetCCtxByCopyingCDict(
|
||||
state: *const ZSTD_rust_resetCCtxByCopyingCDictState,
|
||||
) -> usize {
|
||||
if state.is_null() {
|
||||
return ERROR(ZstdErrorCode::Generic);
|
||||
}
|
||||
let state = unsafe { &*state };
|
||||
let (
|
||||
Some(reset),
|
||||
Some(mark_tables_dirty),
|
||||
Some(copy_tables),
|
||||
Some(zero_hash_table3),
|
||||
Some(mark_tables_clean),
|
||||
Some(copy_match_state),
|
||||
Some(copy_dict_state),
|
||||
Some(copy_block_state),
|
||||
) = (
|
||||
state.reset,
|
||||
state.mark_tables_dirty,
|
||||
state.copy_tables,
|
||||
state.zero_hash_table3,
|
||||
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.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;
|
||||
}
|
||||
mark_tables_dirty(state.callback_context);
|
||||
copy_tables(state.callback_context, state.cdict);
|
||||
zero_hash_table3(state.callback_context, state.cdict);
|
||||
mark_tables_clean(state.callback_context);
|
||||
copy_match_state(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 {
|
||||
@@ -2416,6 +2527,155 @@ mod tests {
|
||||
assert_eq!(probe.events, ["attach"]);
|
||||
}
|
||||
|
||||
#[derive(Default)]
|
||||
struct ResetCCtxByCopyingCDictProbe {
|
||||
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_copying_cdict_probe(
|
||||
context: *mut c_void,
|
||||
) -> &'static mut ResetCCtxByCopyingCDictProbe {
|
||||
unsafe { &mut *context.cast::<ResetCCtxByCopyingCDictProbe>() }
|
||||
}
|
||||
|
||||
unsafe extern "C" fn reset_cctx_by_copying_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_copying_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_copying_cdict_mark_dirty(context: *mut c_void) {
|
||||
unsafe { reset_cctx_by_copying_cdict_probe(context) }
|
||||
.events
|
||||
.push("dirty");
|
||||
}
|
||||
|
||||
unsafe extern "C" fn reset_cctx_by_copying_cdict_copy_tables(
|
||||
context: *mut c_void,
|
||||
_cdict: *const c_void,
|
||||
) {
|
||||
unsafe { reset_cctx_by_copying_cdict_probe(context) }
|
||||
.events
|
||||
.push("tables");
|
||||
}
|
||||
|
||||
unsafe extern "C" fn reset_cctx_by_copying_cdict_zero_hash_table3(
|
||||
context: *mut c_void,
|
||||
_cdict: *const c_void,
|
||||
) {
|
||||
unsafe { reset_cctx_by_copying_cdict_probe(context) }
|
||||
.events
|
||||
.push("zero-h3");
|
||||
}
|
||||
|
||||
unsafe extern "C" fn reset_cctx_by_copying_cdict_mark_clean(context: *mut c_void) {
|
||||
unsafe { reset_cctx_by_copying_cdict_probe(context) }
|
||||
.events
|
||||
.push("clean");
|
||||
}
|
||||
|
||||
unsafe extern "C" fn reset_cctx_by_copying_cdict_copy_match_state(
|
||||
context: *mut c_void,
|
||||
_cdict: *const c_void,
|
||||
) {
|
||||
unsafe { reset_cctx_by_copying_cdict_probe(context) }
|
||||
.events
|
||||
.push("match");
|
||||
}
|
||||
|
||||
unsafe extern "C" fn reset_cctx_by_copying_cdict_copy_dict_state(
|
||||
context: *mut c_void,
|
||||
_cdict: *const c_void,
|
||||
) {
|
||||
unsafe { reset_cctx_by_copying_cdict_probe(context) }
|
||||
.events
|
||||
.push("dict");
|
||||
}
|
||||
|
||||
unsafe extern "C" fn reset_cctx_by_copying_cdict_copy_block_state(
|
||||
context: *mut c_void,
|
||||
_cdict: *const c_void,
|
||||
) {
|
||||
unsafe { reset_cctx_by_copying_cdict_probe(context) }
|
||||
.events
|
||||
.push("block");
|
||||
}
|
||||
|
||||
fn reset_cctx_by_copying_cdict_test_state(
|
||||
probe: &mut ResetCCtxByCopyingCDictProbe,
|
||||
cdict: *const c_void,
|
||||
params: *const c_void,
|
||||
) -> ZSTD_rust_resetCCtxByCopyingCDictState {
|
||||
ZSTD_rust_resetCCtxByCopyingCDictState {
|
||||
callback_context: (probe as *mut ResetCCtxByCopyingCDictProbe).cast(),
|
||||
cdict,
|
||||
params,
|
||||
pledged_src_size: 123,
|
||||
reset: Some(reset_cctx_by_copying_cdict_reset),
|
||||
mark_tables_dirty: Some(reset_cctx_by_copying_cdict_mark_dirty),
|
||||
copy_tables: Some(reset_cctx_by_copying_cdict_copy_tables),
|
||||
zero_hash_table3: Some(reset_cctx_by_copying_cdict_zero_hash_table3),
|
||||
mark_tables_clean: Some(reset_cctx_by_copying_cdict_mark_clean),
|
||||
copy_match_state: Some(reset_cctx_by_copying_cdict_copy_match_state),
|
||||
copy_dict_state: Some(reset_cctx_by_copying_cdict_copy_dict_state),
|
||||
copy_block_state: Some(reset_cctx_by_copying_cdict_copy_block_state),
|
||||
zbuff: 7,
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn reset_cctx_by_copying_cdict_runs_callbacks_in_original_order() {
|
||||
let mut probe = ResetCCtxByCopyingCDictProbe::default();
|
||||
let cdict = 0x4000usize as *const c_void;
|
||||
let params = 0x3000usize as *const c_void;
|
||||
let state = reset_cctx_by_copying_cdict_test_state(&mut probe, cdict, params);
|
||||
|
||||
let result = unsafe { ZSTD_rust_resetCCtxByCopyingCDict(&state) };
|
||||
|
||||
assert_eq!(result, 0);
|
||||
assert_eq!(
|
||||
probe.events,
|
||||
["reset", "dirty", "tables", "zero-h3", "clean", "match", "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_copying_cdict_stops_after_reset_error() {
|
||||
let mut probe = ResetCCtxByCopyingCDictProbe {
|
||||
reset_result: ERROR(ZstdErrorCode::MemoryAllocation),
|
||||
..Default::default()
|
||||
};
|
||||
let state = reset_cctx_by_copying_cdict_test_state(
|
||||
&mut probe,
|
||||
0x4000usize as *const c_void,
|
||||
0x3000usize as *const c_void,
|
||||
);
|
||||
|
||||
let result = unsafe { ZSTD_rust_resetCCtxByCopyingCDict(&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>() }
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user