feat(compress): move CCtx stage guard into Rust

Project the private ZSTD_CCtx compression-stage value into the Rust copy
bridge and reject non-init sources before any destination mutation. Keep the
C private context layout and reset/table operations behind the existing
callbacks while preserving the stage error contract.

Test Plan:
- 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 && CARGO_BUILD_JOBS=1 cargo test --manifest-path rust/Cargo.toml
- 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:59:43 +02:00
parent 9ab4982780
commit 46dd25a122
2 changed files with 23 additions and 54 deletions
+4 -16
View File
@@ -457,8 +457,6 @@ typedef char ZSTD_rust_reset_cctx_state_layout[
== 6 * sizeof(void*)
&& sizeof(ZSTD_rust_resetCCtxState) == 7 * sizeof(void*))
? 1 : -1];
typedef size_t (*ZSTD_rust_copyCCtxCheckStage_f)(
void* context, const void* srcCCtx);
typedef size_t (*ZSTD_rust_copyCCtxReset_f)(
void* context, const void* srcCCtx,
const ZSTD_frameParameters* fParams,
@@ -471,7 +469,7 @@ typedef struct {
const void* srcCCtx;
const ZSTD_frameParameters* fParams;
U64 pledgedSrcSize;
ZSTD_rust_copyCCtxCheckStage_f checkStage;
const int* sourceStage;
void* destinationCustomMem;
const void* sourceCustomMem;
ZSTD_rust_copyCCtxReset_f reset;
@@ -531,7 +529,7 @@ typedef char ZSTD_rust_copy_cctx_internal_state_layout[
== 2 * sizeof(void*)
&& offsetof(ZSTD_rust_copyCCtxInternalState, pledgedSrcSize)
== 3 * sizeof(void*)
&& offsetof(ZSTD_rust_copyCCtxInternalState, checkStage)
&& offsetof(ZSTD_rust_copyCCtxInternalState, sourceStage)
== 3 * sizeof(void*) + sizeof(U64)
&& offsetof(ZSTD_rust_copyCCtxInternalState, zbuff)
== 3 * sizeof(void*) + sizeof(U64)
@@ -4744,17 +4742,6 @@ static size_t ZSTD_resetCCtx_usingCDict(ZSTD_CCtx* cctx,
return ZSTD_rust_resetCCtxUsingCDict(&state);
}
static size_t ZSTD_rust_copyCCtx_check_stage(
void* context, const void* srcCCtx)
{
const ZSTD_CCtx* const src = (const ZSTD_CCtx*)srcCCtx;
(void)context;
RETURN_ERROR_IF(src->stage != ZSTDcs_init, stage_wrong,
"Can't copy a ctx that's not in init stage.");
DEBUGLOG(5, "ZSTD_copyCCtx_internal");
return 0;
}
static size_t ZSTD_rust_copyCCtx_reset(
void* context, const void* srcCCtx,
const ZSTD_frameParameters* fParams,
@@ -4832,11 +4819,12 @@ static size_t ZSTD_rust_copyCCtx_internal_callback(
U64 pledgedSrcSize, int zbuff)
{
ZSTD_rust_copyCCtxInternalState state;
int const sourceStage = (int)((const ZSTD_CCtx*)srcCCtx)->stage;
state.callbackContext = context;
state.srcCCtx = srcCCtx;
state.fParams = fParams;
state.pledgedSrcSize = pledgedSrcSize;
state.checkStage = ZSTD_rust_copyCCtx_check_stage;
state.sourceStage = &sourceStage;
state.destinationCustomMem =
&((ZSTD_CCtx*)context)->customMem;
state.sourceCustomMem =