Coverage Report

Created: 2024-10-16 07:58

/src/wasmer/lib/types/src/module_hash.rs
Line
Count
Source (jump to first uncovered line)
1
use std::fmt::{self, Display, Formatter};
2
3
use rkyv::{Archive, CheckBytes, Deserialize as RkyvDeserialize, Serialize as RkyvSerialize};
4
#[cfg(feature = "enable-serde")]
5
use serde::{Deserialize, Serialize};
6
use sha2::Digest;
7
8
/// Hashing algorithm to be used for the module info
9
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
10
pub enum HashAlgorithm {
11
    /// Sha256
12
    Sha256,
13
    /// XXHash
14
    XXHash,
15
}
16
17
/// The hash of a WebAssembly module.
18
#[derive(
19
    Debug,
20
    Copy,
21
    Clone,
22
    PartialEq,
23
    Eq,
24
    Hash,
25
    PartialOrd,
26
    Ord,
27
0
    RkyvSerialize,
Unexecuted instantiation: <wasmer_types::module_hash::ModuleHash as rkyv::Serialize<rkyv::ser::serializers::CompositeSerializer<rkyv::ser::serializers::alloc::AlignedSerializer<rkyv::util::aligned_vec::AlignedVec>, rkyv::ser::serializers::core::FallbackScratch<rkyv::ser::serializers::alloc::HeapScratch<4096>, rkyv::ser::serializers::alloc::AllocScratch>, rkyv::ser::serializers::alloc::SharedSerializeMap>>>::serialize
Unexecuted instantiation: <wasmer_types::module_hash::ModuleHash as rkyv::Serialize<rkyv::ser::serializers::CompositeSerializer<rkyv::ser::serializers::alloc::AlignedSerializer<rkyv::util::aligned_vec::AlignedVec>, rkyv::ser::serializers::core::FallbackScratch<rkyv::ser::serializers::alloc::HeapScratch<4096>, rkyv::ser::serializers::alloc::AllocScratch>, rkyv::ser::serializers::alloc::SharedSerializeMap>>>::serialize
28
0
    RkyvDeserialize,
Unexecuted instantiation: <wasmer_types::module_hash::ArchivedModuleHash as rkyv::Deserialize<wasmer_types::module_hash::ModuleHash, rkyv::de::deserializers::alloc::SharedDeserializeMap>>::deserialize
Unexecuted instantiation: <wasmer_types::module_hash::ArchivedModuleHash as rkyv::Deserialize<wasmer_types::module_hash::ModuleHash, rkyv::de::deserializers::alloc::SharedDeserializeMap>>::deserialize
Unexecuted instantiation: <wasmer_types::module_hash::ArchivedModuleHash as rkyv::Deserialize<wasmer_types::module_hash::ModuleHash, rkyv::de::deserializers::alloc::SharedDeserializeMap>>::deserialize
Unexecuted instantiation: <wasmer_types::module_hash::ArchivedModuleHash as rkyv::Deserialize<wasmer_types::module_hash::ModuleHash, rkyv::de::deserializers::alloc::SharedDeserializeMap>>::deserialize
29
0
    Archive,
Unexecuted instantiation: <wasmer_types::module_hash::ModuleHash as rkyv::Archive>::resolve
Unexecuted instantiation: <wasmer_types::module_hash::ModuleHash as rkyv::Archive>::resolve
30
)]
31
0
#[archive_attr(derive(CheckBytes, Debug))]
Unexecuted instantiation: <wasmer_types::module_hash::ArchivedModuleHash as bytecheck::CheckBytes<rkyv::validation::validators::DefaultValidator>>::check_bytes::{closure#0}
Unexecuted instantiation: <wasmer_types::module_hash::ArchivedModuleHash as bytecheck::CheckBytes<rkyv::validation::validators::DefaultValidator>>::check_bytes::{closure#1}
Unexecuted instantiation: <wasmer_types::module_hash::ArchivedModuleHash as bytecheck::CheckBytes<rkyv::validation::validators::DefaultValidator>>::check_bytes::{closure#0}
Unexecuted instantiation: <wasmer_types::module_hash::ArchivedModuleHash as bytecheck::CheckBytes<rkyv::validation::validators::DefaultValidator>>::check_bytes::{closure#1}
32
#[cfg_attr(feature = "enable-serde", derive(Serialize, Deserialize))]
33
pub enum ModuleHash {
34
    /// xxhash
35
    XXHash([u8; 8]),
36
37
    /// sha256
38
    Sha256([u8; 32]),
39
}
40
41
#[cfg(feature = "artifact-size")]
42
impl loupe::MemoryUsage for ModuleHash {
43
    fn size_of_val(&self, _tracker: &mut dyn loupe::MemoryUsageTracker) -> usize {
44
        match self {
45
            ModuleHash::XXHash(_) => 8 * 8,
46
            ModuleHash::Sha256(_) => 8 * 32,
47
        }
48
    }
49
}
50
51
impl ModuleHash {
52
    /// Create a new [`ModuleHash`] from the raw xxhash hash.
53
0
    pub fn xxhash_from_bytes(key: [u8; 8]) -> Self {
54
0
        Self::XXHash(key)
55
0
    }
Unexecuted instantiation: <wasmer_types::module_hash::ModuleHash>::xxhash_from_bytes
Unexecuted instantiation: <wasmer_types::module_hash::ModuleHash>::xxhash_from_bytes
56
57
    /// Create a new [`ModuleHash`] from the raw sha256 hash.
58
0
    pub fn sha256_from_bytes(key: [u8; 32]) -> Self {
59
0
        Self::Sha256(key)
60
0
    }
Unexecuted instantiation: <wasmer_types::module_hash::ModuleHash>::sha256_from_bytes
Unexecuted instantiation: <wasmer_types::module_hash::ModuleHash>::sha256_from_bytes
61
62
    /// Parse a XXHash hash from a hex-encoded string.
63
0
    pub fn xxhash_parse_hex(hex_str: &str) -> Result<Self, hex::FromHexError> {
64
0
        let mut hash = [0_u8; 8];
65
0
        hex::decode_to_slice(hex_str, &mut hash)?;
66
0
        Ok(Self::xxhash_from_bytes(hash))
67
0
    }
Unexecuted instantiation: <wasmer_types::module_hash::ModuleHash>::xxhash_parse_hex
Unexecuted instantiation: <wasmer_types::module_hash::ModuleHash>::xxhash_parse_hex
68
69
    /// Parse a Sha256 hash from a hex-encoded string.
70
0
    pub fn sha256_parse_hex(hex_str: &str) -> Result<Self, hex::FromHexError> {
71
0
        let mut hash = [0_u8; 32];
72
0
        hex::decode_to_slice(hex_str, &mut hash)?;
73
0
        Ok(Self::sha256_from_bytes(hash))
74
0
    }
Unexecuted instantiation: <wasmer_types::module_hash::ModuleHash>::sha256_parse_hex
Unexecuted instantiation: <wasmer_types::module_hash::ModuleHash>::sha256_parse_hex
75
76
    /// Generate a new [`ModuleCache`] based on the XXHash hash of some bytes.
77
0
    pub fn xxhash(wasm: impl AsRef<[u8]>) -> Self {
78
0
        let wasm = wasm.as_ref();
79
0
80
0
        let hash = xxhash_rust::xxh64::xxh64(wasm, 0);
81
0
82
0
        Self::XXHash(hash.to_ne_bytes())
83
0
    }
Unexecuted instantiation: <wasmer_types::module_hash::ModuleHash>::xxhash::<&[u8]>
Unexecuted instantiation: <wasmer_types::module_hash::ModuleHash>::xxhash::<_>
Unexecuted instantiation: <wasmer_types::module_hash::ModuleHash>::xxhash::<&[u8]>
Unexecuted instantiation: <wasmer_types::module_hash::ModuleHash>::xxhash::<_>
84
85
    /// Generate a new [`ModuleCache`] based on the Sha256 hash of some bytes.
86
0
    pub fn sha256(wasm: impl AsRef<[u8]>) -> Self {
87
0
        let wasm = wasm.as_ref();
88
0
89
0
        let hash: [u8; 32] = sha2::Sha256::digest(wasm).into();
90
0
91
0
        Self::Sha256(hash)
92
0
    }
Unexecuted instantiation: <wasmer_types::module_hash::ModuleHash>::sha256::<&[u8]>
Unexecuted instantiation: <wasmer_types::module_hash::ModuleHash>::sha256::<_>
Unexecuted instantiation: <wasmer_types::module_hash::ModuleHash>::sha256::<&[u8]>
Unexecuted instantiation: <wasmer_types::module_hash::ModuleHash>::sha256::<_>
93
94
    /// Get the raw hash.
95
0
    pub fn as_bytes(&self) -> &[u8] {
96
0
        match self {
97
0
            Self::XXHash(bytes) => bytes.as_slice(),
98
0
            Self::Sha256(bytes) => bytes.as_slice(),
99
        }
100
0
    }
Unexecuted instantiation: <wasmer_types::module_hash::ModuleHash>::as_bytes
Unexecuted instantiation: <wasmer_types::module_hash::ModuleHash>::as_bytes
101
}
102
103
impl Display for ModuleHash {
104
0
    fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
105
0
        fn format<const N: usize>(f: &mut Formatter<'_>, bytes: &[u8; N]) -> fmt::Result {
106
0
            for byte in bytes {
107
0
                write!(f, "{byte:02X}")?;
108
0
            }
109
0
110
0
            Ok(())
111
0
        }
Unexecuted instantiation: <wasmer_types::module_hash::ModuleHash as core::fmt::Display>::fmt::format::<32>
Unexecuted instantiation: <wasmer_types::module_hash::ModuleHash as core::fmt::Display>::fmt::format::<8>
Unexecuted instantiation: <wasmer_types::module_hash::ModuleHash as core::fmt::Display>::fmt::format::<32>
Unexecuted instantiation: <wasmer_types::module_hash::ModuleHash as core::fmt::Display>::fmt::format::<8>
112
0
113
0
        match self {
114
0
            Self::XXHash(bytes) => format(f, bytes)?,
115
0
            Self::Sha256(bytes) => format(f, bytes)?,
116
        }
117
118
0
        Ok(())
119
0
    }
Unexecuted instantiation: <wasmer_types::module_hash::ModuleHash as core::fmt::Display>::fmt
Unexecuted instantiation: <wasmer_types::module_hash::ModuleHash as core::fmt::Display>::fmt
120
}