Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

stop using crc32 hash on aarch64 #112

Merged
merged 5 commits into from
Jun 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 11 additions & 1 deletion load-dynamic-libz-ng/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,16 @@ pub unsafe fn deflate(strm: *mut libz_ng_sys::z_stream, flush: i32) -> std::ffi:
f(strm, flush)
}

pub unsafe fn deflateEnd(strm: *mut libz_ng_sys::z_stream) -> std::ffi::c_int {
let lib = libloading::Library::new(LIBZ_NG_SO).unwrap();

type Func = unsafe extern "C" fn(strm: *mut libz_ng_sys::z_stream) -> i32;

let f: libloading::Symbol<Func> = lib.get(b"zng_deflateEnd").unwrap();

f(strm)
}

#[allow(non_camel_case_types)]
#[allow(non_snake_case)]
unsafe fn deflateInit2(
Expand Down Expand Up @@ -260,7 +270,7 @@ pub fn compress_with_flush<'a>(
};

unsafe {
let err = libz_ng_sys::deflateEnd(stream);
let err = deflateEnd(stream);
assert_eq!(libz_ng_sys::Z_OK, err);
}

Expand Down
61 changes: 56 additions & 5 deletions zlib-rs/src/deflate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3117,10 +3117,6 @@ mod test {
const LCET10: &str = include_str!("deflate/test-data/lcet10.txt");

#[test]
#[cfg_attr(
target_arch = "aarch64",
ignore = "https://github.com/memorysafety/zlib-rs/issues/91"
)]
#[cfg_attr(miri, ignore)]
fn compress_lcet10() {
fuzz_based_test(LCET10.as_bytes(), DeflateConfig::default(), &[])
Expand Down Expand Up @@ -3639,7 +3635,7 @@ mod test {
let (output_ng, err) = compress_slice_ng(&mut output_ng, input, config);
assert_eq!(err, ReturnCode::Ok);

assert_eq!(output_ng, output_rs);
assert_eq!(output_rs, output_ng);

if !expected.is_empty() {
assert_eq!(output_rs, expected);
Expand Down Expand Up @@ -4377,4 +4373,59 @@ mod test {
assert_eq!(ReturnCode::Ok, error);
assert_eq!(output_rs, uncompressed);
}

#[test]
fn small_aarch64_difference() {
let input = [
0, 0, 0, 0, 0, 43, 0, 0, 0, 0, 0, 0, 43, 0, 0, 0, 0, 0, 0, 0, 0, 48, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 49, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 29, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 19, 0, 0, 0, 0, 0,
0, 0, 64, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 55, 0, 64, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 48, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 102, 102, 102, 102, 102,
102, 102, 102, 102, 102, 102, 102, 102, 102, 102, 102, 102, 102, 102, 102, 102, 102,
102, 102, 102, 102, 102, 102, 102, 102, 112, 102, 102, 102, 102, 102, 102, 102, 102,
102, 102, 102, 102, 102, 102, 102, 102, 102, 102, 102, 102, 102, 102, 102, 102, 102, 0,
0, 0, 0, 0, 0, 49, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 19, 0, 0, 0, 0, 0, 0, 0, 64, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 55, 0, 64, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 42, 0, 0, 0,
50, 0,
];

let config = DeflateConfig {
level: -1,
method: Method::Deflated,
window_bits: 9,
mem_level: 8,
strategy: Strategy::Default,
};

let _x86_64 = [
24, 149, 99, 96, 96, 96, 96, 208, 6, 17, 112, 138, 129, 193, 128, 1, 29, 24, 50, 208,
1, 200, 146, 169, 79, 24, 74, 59, 96, 147, 52, 71, 22, 70, 246, 88, 26, 94, 80, 128,
83, 6, 162, 219, 144, 76, 183, 210, 5, 8, 67, 105, 7, 108, 146, 230, 216, 133, 145,
129, 22, 3, 3, 131, 17, 3, 0, 3, 228, 25, 128,
];

let _aarch64 = [
24, 149, 99, 96, 96, 96, 96, 208, 6, 17, 112, 138, 129, 193, 128, 1, 29, 24, 50, 208,
1, 200, 146, 169, 79, 24, 74, 59, 96, 147, 52, 71, 22, 70, 246, 88, 26, 94, 80, 128,
83, 6, 162, 219, 144, 76, 183, 210, 5, 8, 67, 105, 36, 159, 35, 128, 57, 118, 97, 100,
160, 197, 192, 192, 96, 196, 0, 0, 3, 228, 25, 128,
];

#[cfg(target_arch = "x86_64")]
fuzz_based_test(&input, config, &_x86_64);

#[cfg(target_arch = "aarch64")]
fuzz_based_test(&input, config, &_aarch64);
}
}
13 changes: 10 additions & 3 deletions zlib-rs/src/deflate/hash_calc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ pub struct StandardHashCalc;
impl HashCalc for StandardHashCalc {
const HASH_CALC_OFFSET: usize = 0;

const HASH_CALC_MASK: u32 = 32768 - 1;
const HASH_CALC_MASK: u32 = (HASH_SIZE - 1) as u32;

fn hash_calc(_: u32, val: u32) -> u32 {
const HASH_SLIDE: u32 = 16;
Expand All @@ -70,7 +70,7 @@ pub struct RollHashCalc;
impl HashCalc for RollHashCalc {
const HASH_CALC_OFFSET: usize = STD_MIN_MATCH - 1;

const HASH_CALC_MASK: u32 = 32768 - 1;
const HASH_CALC_MASK: u32 = (1 << 15) - 1;

fn hash_calc(h: u32, val: u32) -> u32 {
const HASH_SLIDE: u32 = 5;
Expand Down Expand Up @@ -121,8 +121,10 @@ impl Crc32HashCalc {
return true;
}

// zlib-ng no longer special-cases on aarch64
#[cfg(all(target_arch = "aarch64", feature = "std"))]
return std::arch::is_aarch64_feature_detected!("crc");
// return std::arch::is_aarch64_feature_detected!("crc");
return false;

#[allow(unreachable_code)]
false
Expand Down Expand Up @@ -199,4 +201,9 @@ mod tests {
assert_eq!(RollHashCalc::hash_calc(3826, 117), 122421);
assert_eq!(RollHashCalc::hash_calc(24117, 101), 771781);
}

#[test]
fn standard_hash_calc() {
assert_eq!(StandardHashCalc::hash_calc(0, 721420288), 47872);
}
}
Loading