From f758c41c5d5639732a3ed2bd4d41e94cc5ebae37 Mon Sep 17 00:00:00 2001 From: ddidderr Date: Sun, 19 Jul 2026 23:40:39 +0200 Subject: [PATCH] feat(compress): zero CDict hash table3 in Rust Move the final CDict-copy hashTable3 clearing operation into the Rust reset orchestration. The C bridge now projects the post-reset destination table and hash log while retaining only the private reset, workspace dirty/clean, and metadata operations on the C side. Preserve C's zero-log contract: hashLog3 == 0 means no table bytes are cleared and a null hashTable3 is valid. Nonzero logs are bounds-checked before Rust zeroes the table, keeping the operation between the dirty and clean transitions. 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 --- lib/compress/zstd_compress.c | 26 ++++-------- rust/src/zstd_compress_dictionary.rs | 63 +++++++++++++++++++--------- 2 files changed, 52 insertions(+), 37 deletions(-) diff --git a/lib/compress/zstd_compress.c b/lib/compress/zstd_compress.c index a8a0d9799..a825d9de0 100644 --- a/lib/compress/zstd_compress.c +++ b/lib/compress/zstd_compress.c @@ -2501,8 +2501,6 @@ typedef char ZSTD_rust_reset_cctx_using_cdict_state_layout[ typedef size_t (*ZSTD_rust_resetCCtxByCopyingCDictReset_f)( void* context, const void* cdict, const void* params, U64 pledgedSrcSize, int zbuff); -typedef void (*ZSTD_rust_resetCCtxByCopyingCDictState_f)( - void* context, const void* cdict); typedef void (*ZSTD_rust_resetCCtxByCopyingCDictMarkTables_f)(void* context); typedef struct { void* callbackContext; @@ -2511,7 +2509,6 @@ typedef struct { U64 pledgedSrcSize; ZSTD_rust_resetCCtxByCopyingCDictReset_f reset; ZSTD_rust_resetCCtxByCopyingCDictMarkTables_f markTablesDirty; - ZSTD_rust_resetCCtxByCopyingCDictState_f zeroHashTable3; ZSTD_rust_resetCCtxByCopyingCDictMarkTables_f markTablesClean; U32** destinationHashTable; const U32* sourceHashTable; @@ -2528,6 +2525,8 @@ typedef struct { const BYTE* sourceTagTable; U64* destinationHashSalt; const U64* sourceHashSalt; + U32** destinationHashTable3; + const U32* destinationHashLog3; void* destinationWindow; const void* sourceWindow; U32* destinationNextToUpdate; @@ -2555,7 +2554,7 @@ typedef char ZSTD_rust_reset_cctx_by_copying_cdict_state_layout[ && offsetof(ZSTD_rust_resetCCtxByCopyingCDictState, reset) == 3 * sizeof(void*) + sizeof(U64) && offsetof(ZSTD_rust_resetCCtxByCopyingCDictState, zbuff) - == 3 * sizeof(void*) + sizeof(U64) + 31 * sizeof(void*) + == 3 * sizeof(void*) + sizeof(U64) + 32 * sizeof(void*) && sizeof(ZSTD_rust_resetCCtxByCopyingCDictState) == ((offsetof(ZSTD_rust_resetCCtxByCopyingCDictState, zbuff) + sizeof(int) + sizeof(void*) - 1) / sizeof(void*)) @@ -4600,18 +4599,6 @@ static void ZSTD_rust_resetCCtx_byCopyingCDict_mark_tables_dirty(void* context) ZSTD_cwksp_mark_tables_dirty(&cctx->workspace); } -static void ZSTD_rust_resetCCtx_byCopyingCDict_zero_hash_table3( - void* context, const void* cdictOpaque) -{ - ZSTD_CCtx* const cctx = (ZSTD_CCtx*)context; - const ZSTD_CDict* const cdict = (const ZSTD_CDict*)cdictOpaque; - U32 const h3log = cctx->blockState.matchState.hashLog3; - assert(h3log <= 31); - assert(cdict->matchState.hashLog3 == 0); - ZSTD_memset(cctx->blockState.matchState.hashTable3, 0, - (h3log ? ((size_t)1 << h3log) : 0) * sizeof(U32)); -} - static void ZSTD_rust_resetCCtx_byCopyingCDict_mark_tables_clean(void* context) { ZSTD_CCtx* const cctx = (ZSTD_CCtx*)context; @@ -4640,10 +4627,9 @@ static size_t ZSTD_resetCCtx_byCopyingCDict(ZSTD_CCtx* cctx, state.reset = ZSTD_rust_resetCCtx_byCopyingCDict_reset; state.markTablesDirty = ZSTD_rust_resetCCtx_byCopyingCDict_mark_tables_dirty; - state.zeroHashTable3 = - ZSTD_rust_resetCCtx_byCopyingCDict_zero_hash_table3; state.markTablesClean = ZSTD_rust_resetCCtx_byCopyingCDict_mark_tables_clean; + assert(cdict->matchState.hashLog3 == 0); state.destinationHashTable = &cctx->blockState.matchState.hashTable; state.sourceHashTable = cdict->matchState.hashTable; @@ -4665,6 +4651,10 @@ static size_t ZSTD_resetCCtx_byCopyingCDict(ZSTD_CCtx* cctx, state.destinationHashSalt = &cctx->blockState.matchState.hashSalt; state.sourceHashSalt = &cdict->matchState.hashSalt; + state.destinationHashTable3 = + &cctx->blockState.matchState.hashTable3; + state.destinationHashLog3 = + &cctx->blockState.matchState.hashLog3; state.destinationWindow = &cctx->blockState.matchState.window; state.sourceWindow = &cdict->matchState.window; state.destinationNextToUpdate = &cctx->blockState.matchState.nextToUpdate; diff --git a/rust/src/zstd_compress_dictionary.rs b/rust/src/zstd_compress_dictionary.rs index eb7ba786f..7dba99e70 100644 --- a/rust/src/zstd_compress_dictionary.rs +++ b/rust/src/zstd_compress_dictionary.rs @@ -1303,7 +1303,6 @@ 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); #[repr(C)] @@ -1350,7 +1349,6 @@ pub struct ZSTD_rust_resetCCtxByCopyingCDictState { pledged_src_size: u64, reset: Option, mark_tables_dirty: Option, - zero_hash_table3: Option, mark_tables_clean: Option, destination_hash_table: *mut *mut c_uint, source_hash_table: *const c_uint, @@ -1367,6 +1365,8 @@ pub struct ZSTD_rust_resetCCtxByCopyingCDictState { source_tag_table: *const u8, destination_hash_salt: *mut u64, source_hash_salt: *const u64, + destination_hash_table3: *mut *mut c_uint, + destination_hash_log3: *const c_uint, destination_window: *mut c_void, source_window: *const c_void, destination_next_to_update: *mut c_uint, @@ -1384,7 +1384,6 @@ pub struct ZSTD_rust_resetCCtxByCopyingCDictState { const _: () = { assert!(size_of::() == size_of::()); - assert!(size_of::() == size_of::()); assert!(size_of::() == size_of::()); assert!(offset_of!(ZSTD_rust_resetCCtxByCopyingCDictState, callback_context) == 0); assert!(offset_of!(ZSTD_rust_resetCCtxByCopyingCDictState, cdict) == size_of::()); @@ -1399,7 +1398,7 @@ const _: () = { ); assert!( offset_of!(ZSTD_rust_resetCCtxByCopyingCDictState, zbuff) - == 3 * size_of::() + size_of::() + size_of::<[usize; 31]>() + == 3 * size_of::() + size_of::() + size_of::<[usize; 32]>() ); assert!( size_of::() @@ -1420,10 +1419,9 @@ pub unsafe extern "C" fn ZSTD_rust_resetCCtxByCopyingCDict( return ERROR(ZstdErrorCode::Generic); } let state = unsafe { &*state }; - let (Some(reset), Some(mark_tables_dirty), Some(zero_hash_table3), Some(mark_tables_clean)) = ( + let (Some(reset), Some(mark_tables_dirty), Some(mark_tables_clean)) = ( state.reset, state.mark_tables_dirty, - state.zero_hash_table3, state.mark_tables_clean, ) else { return ERROR(ZstdErrorCode::Generic); @@ -1443,6 +1441,8 @@ pub unsafe extern "C" fn ZSTD_rust_resetCCtxByCopyingCDict( || state.destination_tag_table.is_null() || state.destination_hash_salt.is_null() || state.source_hash_salt.is_null() + || state.destination_hash_table3.is_null() + || state.destination_hash_log3.is_null() || state.destination_window.is_null() || state.source_window.is_null() || state.destination_next_to_update.is_null() @@ -1496,6 +1496,15 @@ pub unsafe extern "C" fn ZSTD_rust_resetCCtxByCopyingCDict( } let destination_strategy = *state.destination_strategy; let destination_use_row_match_finder = *state.destination_use_row_match_finder; + let destination_hash_log3 = *state.destination_hash_log3; + if destination_hash_log3 > 31 { + return ERROR(ZstdErrorCode::Generic); + } + let hash_table3_size = if destination_hash_log3 == 0 { + 0 + } else { + 1usize << destination_hash_log3 + }; let destination_chain_table_size = if ZSTD_rust_params_allocateChainTable( destination_strategy, destination_use_row_match_finder, @@ -1509,11 +1518,13 @@ pub unsafe extern "C" fn ZSTD_rust_resetCCtxByCopyingCDict( let destination_hash_table = *state.destination_hash_table; let destination_chain_table = *state.destination_chain_table; let destination_tag_table = *state.destination_tag_table; + let destination_hash_table3 = *state.destination_hash_table3; if (destination_hash_table.is_null() || state.source_hash_table.is_null()) || (destination_chain_table_size != 0 && (destination_chain_table.is_null() || state.source_chain_table.is_null())) || (row_match_finder_used && (destination_tag_table.is_null() || state.source_tag_table.is_null())) + || (hash_table3_size != 0 && destination_hash_table3.is_null()) { return ERROR(ZstdErrorCode::Generic); } @@ -1540,7 +1551,9 @@ pub unsafe extern "C" fn ZSTD_rust_resetCCtxByCopyingCDict( ); *state.destination_hash_salt = *state.source_hash_salt; } - zero_hash_table3(state.callback_context, state.cdict); + if hash_table3_size != 0 { + ptr::write_bytes(destination_hash_table3, 0, hash_table3_size); + } mark_tables_clean(state.callback_context); ptr::copy( state.source_window.cast::(), @@ -2868,6 +2881,9 @@ mod tests { destination_tag_table_slot: *mut u8, source_hash_salt: u64, destination_hash_salt: u64, + destination_hash_table3: [c_uint; 4], + destination_hash_table3_slot: *mut c_uint, + destination_hash_log3: c_uint, source_hash_log: c_uint, source_chain_log: c_uint, source_strategy: c_int, @@ -2905,15 +2921,6 @@ mod tests { .push("dirty"); } - 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 @@ -2940,6 +2947,8 @@ mod tests { probe.destination_hash_table_slot = probe.destination_hash_table.as_mut_ptr(); probe.destination_chain_table_slot = probe.destination_chain_table.as_mut_ptr(); probe.destination_tag_table_slot = probe.destination_tag_table.as_mut_ptr(); + probe.destination_hash_table3_slot = probe.destination_hash_table3.as_mut_ptr(); + probe.destination_hash_log3 = 2; probe.source_hash_log = 2; probe.source_chain_log = 2; probe.source_strategy = 2; @@ -2954,7 +2963,6 @@ mod tests { pledged_src_size: 123, reset: Some(reset_cctx_by_copying_cdict_reset), mark_tables_dirty: Some(reset_cctx_by_copying_cdict_mark_dirty), - zero_hash_table3: Some(reset_cctx_by_copying_cdict_zero_hash_table3), mark_tables_clean: Some(reset_cctx_by_copying_cdict_mark_clean), destination_hash_table: &mut probe.destination_hash_table_slot, source_hash_table: probe.source_hash_table.as_ptr(), @@ -2971,6 +2979,8 @@ mod tests { source_tag_table: probe.source_tag_table.as_ptr(), destination_hash_salt: &mut probe.destination_hash_salt, source_hash_salt: &probe.source_hash_salt, + destination_hash_table3: &mut probe.destination_hash_table3_slot, + destination_hash_log3: &probe.destination_hash_log3, destination_window, source_window, destination_next_to_update, @@ -2992,6 +3002,7 @@ mod tests { let mut probe = ResetCCtxByCopyingCDictProbe { source_hash_table: [0x0000_0101, 0x0000_0202, 0x0000_0303, 0x0000_0404], source_chain_table: [0x0000_1101, 0x0000_2202, 0x0000_3303, 0x0000_4404], + destination_hash_table3: [9, 9, 9, 9], ..Default::default() }; let cdict = 0x4000usize as *const c_void; @@ -3050,11 +3061,12 @@ mod tests { let result = unsafe { ZSTD_rust_resetCCtxByCopyingCDict(&state) }; assert_eq!(result, 0); - assert_eq!(probe.events, ["reset", "dirty", "zero-h3", "clean"]); + assert_eq!(probe.events, ["reset", "dirty", "clean"]); assert_eq!(probe.destination_hash_table, [1, 2, 3, 4]); assert_eq!(probe.destination_chain_table, [0x11, 0x22, 0x33, 0x44]); assert_eq!(probe.destination_tag_table, [0; 4]); assert_eq!(probe.destination_hash_salt, 0); + assert_eq!(probe.destination_hash_table3, [0; 4]); assert_eq!(probe.cdict, cdict); assert_eq!(probe.params, params); assert_eq!(probe.pledged_src_size, 123); @@ -3079,6 +3091,7 @@ mod tests { probe.source_tag_table = [5, 6, 7, 8]; probe.destination_hash_table = [0; 4]; probe.destination_tag_table = [0; 4]; + probe.destination_hash_table3 = [9, 9, 9, 9]; probe.destination_hash_salt = 0; probe.source_hash_salt = 0x0123_4567_89ab_cdef; probe.source_strategy = 3; @@ -3090,10 +3103,22 @@ mod tests { let result = unsafe { ZSTD_rust_resetCCtxByCopyingCDict(&state) }; assert_eq!(result, 0); - assert_eq!(probe.events, ["reset", "dirty", "zero-h3", "clean"]); + assert_eq!(probe.events, ["reset", "dirty", "clean"]); assert_eq!(probe.destination_hash_table, probe.source_hash_table); assert_eq!(probe.destination_tag_table, probe.source_tag_table); assert_eq!(probe.destination_hash_salt, probe.source_hash_salt); + assert_eq!(probe.destination_hash_table3, [0; 4]); + + probe.events.clear(); + probe.destination_hash_table3 = [9, 9, 9, 9]; + probe.destination_hash_log3 = 0; + probe.destination_hash_table3_slot = ptr::null_mut(); + + let result = unsafe { ZSTD_rust_resetCCtxByCopyingCDict(&state) }; + + assert_eq!(result, 0); + assert_eq!(probe.events, ["reset", "dirty", "clean"]); + assert_eq!(probe.destination_hash_table3, [9; 4]); } #[test]