feat(compress): move CCtx reset tail orchestration into Rust

Move the post-workspace CCtx reset order into Rust: initialize the applied
context state, reset the previous compressed-block state, reset match state,
and publish the remaining storage in sequence with error propagation between
steps. Keep private CCtx and workspace mutations in C-owned callbacks and
preserve the C/Rust state layout assertions.

Add focused tests for successful ordering and match-state/storage failure
boundaries.

Test Plan:
- `ulimit -v 41943040 && CARGO_BUILD_JOBS=1 cargo test --manifest-path rust/Cargo.toml`
- `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-zstream ZSTREAM_TESTTIME=-T2s`
- `ulimit -v 41943040 && make -j1 -C tests test-fuzzer FUZZERTEST=-T3s FUZZER_FLAGS=--no-big-tests`
- `ulimit -v 41943040 && cargo fmt --manifest-path rust/Cargo.toml -- --check`
- `git diff --check`
This commit is contained in:
2026-07-19 21:41:02 +02:00
parent f882006e7a
commit 0be6d23512
2 changed files with 279 additions and 29 deletions
+178
View File
@@ -6720,6 +6720,7 @@ const _: () = {
type ResetCCtxWorkspaceCreate = unsafe extern "C" fn(*mut c_void, usize) -> usize;
type ResetCCtxWorkspaceReserveObject = unsafe extern "C" fn(*mut c_void, usize) -> *mut c_void;
type ResetCCtxTailCallback = unsafe extern "C" fn(*mut c_void) -> usize;
/// C-owned workspace operations used by the CCtx reset policy.
///
@@ -6800,6 +6801,34 @@ const _: () = {
);
};
/// C-owned operations used by the post-workspace CCtx reset tail.
///
/// Rust owns the operation order and error propagation. C retains the
/// private context initialization, compressed-block reset, match-state reset,
/// and storage publication operations.
#[repr(C)]
pub struct ZSTD_rust_resetCCtxTailState {
callbackContext: *mut c_void,
initialize: Option<ResetCCtxStorageCallback>,
resetCompressedBlockState: Option<ResetCCtxStorageCallback>,
resetMatchState: Option<ResetCCtxTailCallback>,
resetStorage: Option<ResetCCtxTailCallback>,
}
const _: () = {
assert!(offset_of!(ZSTD_rust_resetCCtxTailState, callbackContext) == 0);
assert!(offset_of!(ZSTD_rust_resetCCtxTailState, initialize) == size_of::<usize>());
assert!(
offset_of!(ZSTD_rust_resetCCtxTailState, resetCompressedBlockState)
== 2 * size_of::<usize>()
);
assert!(offset_of!(ZSTD_rust_resetCCtxTailState, resetMatchState) == 3 * size_of::<usize>());
assert!(
size_of::<ZSTD_rust_resetCCtxTailState>()
== offset_of!(ZSTD_rust_resetCCtxTailState, resetStorage) + size_of::<usize>()
);
};
/// Reserve and publish the private CCtx storage that follows match-state reset.
#[no_mangle]
pub unsafe extern "C" fn ZSTD_rust_resetCCtxStorage(
@@ -7082,6 +7111,46 @@ pub unsafe extern "C" fn ZSTD_rust_resetCCtxWorkspace(
0
}
/// Own the callback order for the post-workspace CCtx reset tail.
#[no_mangle]
pub unsafe extern "C" fn ZSTD_rust_resetCCtxTail(
state: *const ZSTD_rust_resetCCtxTailState,
) -> usize {
if state.is_null() {
return ERROR(ZstdErrorCode::Generic);
}
let state = unsafe { &*state };
if state.callbackContext.is_null() {
return ERROR(ZstdErrorCode::Generic);
}
let Some(initialize) = state.initialize else {
return ERROR(ZstdErrorCode::Generic);
};
let Some(reset_compressed_block_state) = state.resetCompressedBlockState else {
return ERROR(ZstdErrorCode::Generic);
};
let Some(reset_match_state) = state.resetMatchState else {
return ERROR(ZstdErrorCode::Generic);
};
let Some(reset_storage) = state.resetStorage else {
return ERROR(ZstdErrorCode::Generic);
};
unsafe {
initialize(state.callbackContext);
reset_compressed_block_state(state.callbackContext);
let result = reset_match_state(state.callbackContext);
if ERR_isError(result) {
return result;
}
let result = reset_storage(state.callbackContext);
if ERR_isError(result) {
return result;
}
}
0
}
#[inline]
fn max_estimate_cctx_size(
estimate0: usize,
@@ -16653,6 +16722,115 @@ mod tests {
assert!(context.events.is_empty());
}
#[derive(Default)]
struct ResetCCtxTailTestContext {
events: Vec<&'static str>,
match_result: usize,
storage_result: usize,
}
unsafe fn reset_cctx_tail_test_context(
context: *mut c_void,
) -> &'static mut ResetCCtxTailTestContext {
unsafe { &mut *context.cast::<ResetCCtxTailTestContext>() }
}
unsafe extern "C" fn reset_cctx_tail_test_initialize(context: *mut c_void) {
let context = unsafe { reset_cctx_tail_test_context(context) };
context.events.push("initialize");
}
unsafe extern "C" fn reset_cctx_tail_test_reset_compressed_block_state(context: *mut c_void) {
let context = unsafe { reset_cctx_tail_test_context(context) };
context.events.push("reset-compressed-block-state");
}
unsafe extern "C" fn reset_cctx_tail_test_reset_match_state(context: *mut c_void) -> usize {
let context = unsafe { reset_cctx_tail_test_context(context) };
context.events.push("reset-match-state");
context.match_result
}
unsafe extern "C" fn reset_cctx_tail_test_reset_storage(context: *mut c_void) -> usize {
let context = unsafe { reset_cctx_tail_test_context(context) };
context.events.push("reset-storage");
context.storage_result
}
fn reset_cctx_tail_test_state(
context: &mut ResetCCtxTailTestContext,
) -> ZSTD_rust_resetCCtxTailState {
ZSTD_rust_resetCCtxTailState {
callbackContext: (context as *mut ResetCCtxTailTestContext).cast(),
initialize: Some(reset_cctx_tail_test_initialize),
resetCompressedBlockState: Some(reset_cctx_tail_test_reset_compressed_block_state),
resetMatchState: Some(reset_cctx_tail_test_reset_match_state),
resetStorage: Some(reset_cctx_tail_test_reset_storage),
}
}
#[test]
fn cctx_reset_tail_preserves_post_workspace_callback_order() {
let mut context = ResetCCtxTailTestContext::default();
let state = reset_cctx_tail_test_state(&mut context);
assert_eq!(unsafe { ZSTD_rust_resetCCtxTail(&state) }, 0);
assert_eq!(
context.events,
[
"initialize",
"reset-compressed-block-state",
"reset-match-state",
"reset-storage",
]
);
}
#[test]
fn cctx_reset_tail_stops_before_storage_after_match_state_error() {
let mut context = ResetCCtxTailTestContext {
match_result: ERROR(ZstdErrorCode::StageWrong),
..ResetCCtxTailTestContext::default()
};
let state = reset_cctx_tail_test_state(&mut context);
assert_eq!(
unsafe { ZSTD_rust_resetCCtxTail(&state) },
ERROR(ZstdErrorCode::StageWrong)
);
assert_eq!(
context.events,
[
"initialize",
"reset-compressed-block-state",
"reset-match-state"
]
);
}
#[test]
fn cctx_reset_tail_propagates_storage_error_after_match_state() {
let mut context = ResetCCtxTailTestContext {
storage_result: ERROR(ZstdErrorCode::MemoryAllocation),
..ResetCCtxTailTestContext::default()
};
let state = reset_cctx_tail_test_state(&mut context);
assert_eq!(
unsafe { ZSTD_rust_resetCCtxTail(&state) },
ERROR(ZstdErrorCode::MemoryAllocation)
);
assert_eq!(
context.events,
[
"initialize",
"reset-compressed-block-state",
"reset-match-state",
"reset-storage",
]
);
}
#[test]
fn estimate_cctx_workspace_size_keeps_static_and_buffer_components_separate() {
let sizing = cctx_workspace_test_sizing();