From 38f88a971b6afce7c10e021dfcfeab3f008e54f8 Mon Sep 17 00:00:00 2001 From: Chris Alef <476570+calef@users.noreply.github.com> Date: Sun, 22 Nov 2026 22:31:01 +0000 Subject: [PATCH] docs(rsext4): state the safety argument at each CRC32C unsafe use Each call into a hardware CRC32C helper now names the CPU feature it needs and the register it was detected from. The aarch64 feature read says why `mrs` of an EL1 ID register is valid at EL0, and the aarch64 loop has one unsafe block per loop and per tail step, each with its alignment and bounds argument, instead of one block over the whole body. Each SAFETY comment sits inside its block, as in the x86_64 helper. The instructions executed are unchanged. --- fs/rsext4/src/crc32c/arm64.rs | 66 +++++++++++++++++++++++++--------- fs/rsext4/src/crc32c/crc32c.rs | 21 +++++++++-- 2 files changed, 67 insertions(+), 20 deletions(-) diff --git a/fs/rsext4/src/crc32c/arm64.rs b/fs/rsext4/src/crc32c/arm64.rs index 7f2c87826..bdfc74aee 100644 --- a/fs/rsext4/src/crc32c/arm64.rs +++ b/fs/rsext4/src/crc32c/arm64.rs @@ -49,6 +49,13 @@ pub fn has_hardware_crc32() -> bool { { let mut reg_val: u64; unsafe { + // SAFETY: `mrs` only copies the read-only ID register into a + // general-purpose register and touches no memory. At EL1 the read + // is architectural. At EL0 it traps, and the instruction is only + // valid there because the kernel emulates the ID registers for + // user space (Linux does so when it sets `HWCAP_CPUID`); a host + // that does not emulate them delivers the trap as an illegal + // instruction. // mrs: Move from System Register to general purpose register asm!("mrs {}, ID_AA64ISAR0_EL1", out(reg) reg_val); } @@ -76,44 +83,69 @@ pub fn has_hardware_crc32() -> bool { pub unsafe fn crc32c_hardware(mut crc: u32, data: &[u8]) -> u32 { // This helper only performs the raw CRC update step. Callers remain // responsible for any ext4-specific init/finalize inversion. - unsafe { - let mut p = data.as_ptr(); - let mut len = data.len(); + let mut p = data.as_ptr(); + let mut len = data.len(); - // 1. Consume the unaligned prefix so the hot loop can use 64-bit loads. - while len > 0 && !(p as usize).is_multiple_of(8) { + // Every read below is in bounds because `len` counts the bytes left + // between `p` and the end of `data`, and each step reads at most `len` + // bytes before advancing `p` and lowering `len` by the same amount. + + // 1. Consume the unaligned prefix so the hot loop can use 64-bit loads. + while len > 0 && !(p as usize).is_multiple_of(8) { + unsafe { + // SAFETY: `len > 0`, so `p` points at a byte of `data`, and + // advancing it by one keeps it within `data` or one past its end. crc = __crc32cb(crc, *p); p = p.add(1); - len -= 1; } + len -= 1; + } - // 2. Main loop: process aligned 64-bit chunks. - while len >= 8 { + // 2. Main loop: process aligned 64-bit chunks. + while len >= 8 { + unsafe { + // SAFETY: the prefix loop left `p` 8-byte aligned and this loop + // advances it in steps of 8, so the read is aligned; `len >= 8` + // keeps all eight bytes in `data`, and `p` ends within `data` or + // one past its end. let val = *(p as *const u64); crc = __crc32cd(crc, val); p = p.add(8); - len -= 8; } + len -= 8; + } - // 3. Finish the tail with progressively smaller chunk sizes. - if len >= 4 { + // 3. Finish the tail with progressively smaller chunk sizes. + if len >= 4 { + unsafe { + // SAFETY: `p` is 8-byte aligned here, so also 4-byte aligned; + // `len >= 4` keeps the four bytes in `data`, and `p` ends within + // `data` or one past its end. let val = *(p as *const u32); crc = __crc32cw(crc, val); p = p.add(4); - len -= 4; } + len -= 4; + } - if len >= 2 { + if len >= 2 { + unsafe { + // SAFETY: `p` advanced by 0 or 4 from an 8-byte boundary, so it + // is 2-byte aligned; `len >= 2` keeps the two bytes in `data`, and + // `p` ends within `data` or one past its end. let val = *(p as *const u16); crc = __crc32ch(crc, val); p = p.add(2); - len -= 2; } + len -= 2; + } - if len > 0 { + if len > 0 { + unsafe { + // SAFETY: `len > 0`, so `p` points at the last byte of `data`. crc = __crc32cb(crc, *p); } - - crc } + + crc } diff --git a/fs/rsext4/src/crc32c/crc32c.rs b/fs/rsext4/src/crc32c/crc32c.rs index c7e9419c5..e72c6fa10 100644 --- a/fs/rsext4/src/crc32c/crc32c.rs +++ b/fs/rsext4/src/crc32c/crc32c.rs @@ -94,7 +94,12 @@ pub fn crc32c_append(crc: u32, data: &[u8]) -> u32 { #[cfg(target_arch = "aarch64")] { if is_hardware_crc32_supported() { - return unsafe { crc32c_hardware(crc, data) }; + return unsafe { + // SAFETY: `crc32c_hardware` needs the ARMv8 CRC32 extension, + // and `is_hardware_crc32_supported` has just read it as present + // from `ID_AA64ISAR0_EL1[19:16]` (see `crc32c::arm64`). + crc32c_hardware(crc, data) + }; } } @@ -104,7 +109,12 @@ pub fn crc32c_append(crc: u32, data: &[u8]) -> u32 { #[cfg(target_arch = "x86_64")] { if is_hardware_crc32_supported() { - return unsafe { crc32c_hardware(crc, data) }; + return unsafe { + // SAFETY: `crc32c_hardware` needs SSE4.2, and + // `is_hardware_crc32_supported` has just read it as present + // from CPUID leaf 1 ECX bit 20 (see `crc32c::x86_64`). + crc32c_hardware(crc, data) + }; } } @@ -219,7 +229,12 @@ mod tests { for len in 0..=257 { let data = &storage[offset..offset + len]; assert_eq!( - unsafe { crate::crc32c::x86_64::crc32c_hardware(seed, data) }, + unsafe { + // SAFETY: the test returned early above unless + // `is_hardware_crc32_supported` read SSE4.2 as + // present from CPUID leaf 1 ECX bit 20. + crate::crc32c::x86_64::crc32c_hardware(seed, data) + }, crc32c_update(seed, data), "offset={offset} length={len} seed={seed:#x}" ); -- 2.53.0