Skip to main content

fxfs/
checksum.rs

1// Copyright 2022 The Fuchsia Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5use crate::errors::FxfsError;
6use anyhow::Error;
7use byteorder::{ByteOrder, LittleEndian};
8use fprint::TypeFingerprint;
9use serde::{Deserialize, Serialize};
10use static_assertions::assert_cfg;
11use zerocopy::{FromBytes as _, IntoBytes as _};
12
13/// For the foreseeable future, Fxfs will use 64-bit checksums.
14pub type Checksum = u64;
15
16/// Generates a Fletcher64 checksum of |buf| seeded by |previous|.
17///
18/// All logfile blocks are covered by a fletcher64 checksum as the last 8 bytes in a block.
19///
20/// We also use this checksum for integrity validation of potentially out-of-order writes
21/// during Journal replay.
22pub fn fletcher64(buf: &[u8], previous: Checksum) -> Checksum {
23    assert!(buf.len() % 4 == 0);
24    let mut lo = previous as u32;
25    let mut hi = (previous >> 32) as u32;
26    for chunk in buf.chunks(4) {
27        lo = lo.wrapping_add(LittleEndian::read_u32(chunk));
28        hi = hi.wrapping_add(lo);
29    }
30    (hi as u64) << 32 | lo as u64
31}
32
33/// A vector of fletcher64 checksums, one per block.
34/// These are stored as a flat array of bytes for efficient deserialization.
35pub type Checksums = ChecksumsV38;
36
37#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, TypeFingerprint)]
38#[cfg_attr(fuzz, derive(arbitrary::Arbitrary))]
39pub struct ChecksumsV38 {
40    #[serde(with = "crate::zerocopy_serialization")]
41    sums: Vec<u8>,
42}
43
44impl Checksums {
45    pub fn fletcher(checksums: Vec<Checksum>) -> Self {
46        assert_cfg!(target_endian = "little");
47        let checksums_as_u8: &[u8] = &*checksums.as_bytes();
48        Self { sums: checksums_as_u8.to_owned() }
49    }
50
51    pub fn len(&self) -> usize {
52        self.sums.len() / std::mem::size_of::<Checksum>()
53    }
54
55    pub fn maybe_as_ref(&self) -> Result<&[Checksum], Error> {
56        assert_cfg!(target_endian = "little");
57        <[Checksum]>::ref_from_bytes(&self.sums).map_err(|_| FxfsError::Inconsistent.into())
58    }
59
60    pub fn offset_by(&self, amount: usize) -> Self {
61        Checksums { sums: self.sums[amount * std::mem::size_of::<Checksum>()..].to_vec() }
62    }
63
64    pub fn shrunk(&self, len: usize) -> Self {
65        Checksums { sums: self.sums[..len * std::mem::size_of::<Checksum>()].to_vec() }
66    }
67}
68
69#[cfg(test)]
70mod tests {
71    use crate::checksum::Checksums;
72    use crate::errors::FxfsError;
73
74    #[test]
75    fn checksum_encoding_idempotent() {
76        let mut checksums = vec![0xabu64 << 56, 0x11002200u64, u64::MAX, 0];
77        checksums.reserve_exact(5);
78
79        let encoded = Checksums::fletcher(checksums.clone());
80        let decoded = encoded.maybe_as_ref().unwrap();
81
82        assert_eq!(decoded, &checksums[..]);
83    }
84
85    #[test]
86    fn deserialize_invalid_checksum() {
87        let bad = Checksums { sums: vec![0, 1, 2, 3, 4, 5, 6] };
88        let res = bad.maybe_as_ref().expect_err("deserialization should fail");
89        assert!(FxfsError::Inconsistent.matches(&res));
90    }
91}