refactor(decompress): move default window policy to Rust

Route the configured default maximum window size through the Rust policy layer while keeping the build-time C configuration value and decoder layout in C. The explicit projection preserves overridden builds exactly and gives the scalar policy a focused null-input contract and tests.

Test Plan:

- git diff --check -- lib/decompress/zstd_decompress.c rust/src/zstd_decompress.rs

- capped serial cargo check, clippy, nightly fmt, C syntax checks, and focused default-window tests (passed in the worker)
This commit is contained in:
2026-07-21 09:47:29 +02:00
parent 2eb56f3434
commit 9c7bf99a61
2 changed files with 79 additions and 3 deletions
+63
View File
@@ -310,6 +310,22 @@ const _: () = {
assert!(size_of::<ZSTD_rustLegacySupportProjection>() == size_of::<c_uint>());
};
#[repr(C)]
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
pub struct ZSTD_rustDefaultMaxWindowProjection {
pub configured_max_window_size: usize,
}
const _: () = {
assert!(
offset_of!(
ZSTD_rustDefaultMaxWindowProjection,
configured_max_window_size
) == 0
);
assert!(size_of::<ZSTD_rustDefaultMaxWindowProjection>() == size_of::<usize>());
};
#[repr(C)]
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
pub struct ZSTD_rustNoForwardProgressProjection {
@@ -678,6 +694,21 @@ unsafe fn configured_legacy_support() -> u32 {
}
}
/// Preserve the active C build's default maximum window size.
///
/// C supplies the compile-time value because it may be overridden by the
/// build. Rust owns this scalar policy boundary without clamping or replacing
/// the configured value, so custom decoder limits keep their existing meaning.
#[no_mangle]
pub unsafe extern "C" fn ZSTD_rust_default_max_window_policy(
projection: *const ZSTD_rustDefaultMaxWindowProjection,
) -> usize {
let Some(projection) = (unsafe { projection.as_ref() }) else {
return 0;
};
projection.configured_max_window_size
}
/// Preserve the active C build's no-forward-progress threshold.
///
/// C supplies the compile-time value because it may be overridden by the
@@ -708,6 +739,38 @@ pub unsafe extern "C" fn ZSTD_rust_heapmode_policy(
c_int::from(projection.configured_mode >= 1)
}
#[cfg(test)]
mod default_max_window_policy_tests {
use super::*;
#[test]
fn preserves_configured_default_window_sizes() {
for configured_max_window_size in [
0,
1,
1usize << ZSTD_WINDOWLOG_ABSOLUTEMIN,
(1usize << ZSTD_WINDOWLOG_LIMIT_DEFAULT) + 1,
usize::MAX,
] {
let projection = ZSTD_rustDefaultMaxWindowProjection {
configured_max_window_size,
};
assert_eq!(
unsafe { ZSTD_rust_default_max_window_policy(&projection) },
configured_max_window_size
);
}
}
#[test]
fn rejects_missing_default_window_configuration() {
assert_eq!(
unsafe { ZSTD_rust_default_max_window_policy(ptr::null()) },
0
);
}
}
#[cfg(test)]
mod no_forward_progress_policy_tests {
use super::*;