1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
//! Validation implementations for HashMap and HashSet.

use super::{ArchivedBucket, ArchivedHashMap, ArchivedHashSet, Group};
use crate::{
    offset_of,
    validation::{ArchiveContext, ArchiveMemoryError},
    RelPtr,
};
use bytecheck::{CheckBytes, Unreachable};
use core::{fmt, hash::Hash};
use std::error::Error;

/// Errors that can occur while checking an archived bucket.
#[derive(Debug)]
pub enum ArchivedBucketError<K, V> {
    /// An error occurred while checking the bytes of a key
    KeyCheckBytes(K),
    /// An error occurred while checking the bytes of a value
    ValueCheckBytes(V),
}

impl<K: fmt::Display, V: fmt::Display> fmt::Display for ArchivedBucketError<K, V> {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            ArchivedBucketError::KeyCheckBytes(e) => write!(f, "key check error: {}", e),
            ArchivedBucketError::ValueCheckBytes(e) => write!(f, "value check error: {}", e),
        }
    }
}

impl<K: fmt::Debug + fmt::Display, V: fmt::Debug + fmt::Display> Error
    for ArchivedBucketError<K, V>
{
}

impl<K: CheckBytes<ArchiveContext>, V: CheckBytes<ArchiveContext>> CheckBytes<ArchiveContext>
    for ArchivedBucket<K, V>
{
    type Error = ArchivedBucketError<K::Error, V::Error>;

    unsafe fn check_bytes<'a>(
        bytes: *const u8,
        context: &mut ArchiveContext,
    ) -> Result<&'a Self, Self::Error> {
        K::check_bytes(bytes.add(offset_of!(ArchivedBucket<K, V>, key)), context)
            .map_err(ArchivedBucketError::KeyCheckBytes)?;
        V::check_bytes(bytes.add(offset_of!(ArchivedBucket<K, V>, value)), context)
            .map_err(ArchivedBucketError::ValueCheckBytes)?;
        Ok(&*bytes.cast())
    }
}

/// Errors that can occur while checking an [`ArchivedHashMap`].
#[derive(Debug)]
pub enum ArchivedHashMapError<K, V> {
    /// The number of items the hashmap claims to have doesn't match the actual
    /// number of items as indicated by the control bytes.
    InvalidItemCount {
        /// The number of items the hashmap claims to have
        expected_items: usize,
        /// The actual number of items in the hashmap
        actual_items: usize,
    },
    /// A memory error occurred
    MemoryError(ArchiveMemoryError),
    /// An error occured while checking the bytes of a bucket
    BucketError(ArchivedBucketError<K, V>),
    /// A key is placed in the wrong bucket
    IncorrectKeyHash {
        /// The index of the key when iterating
        index: usize,
    },
}

impl<K: fmt::Display, V: fmt::Display> fmt::Display for ArchivedHashMapError<K, V> {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            ArchivedHashMapError::InvalidItemCount {
                expected_items,
                actual_items,
            } => write!(
                f,
                "invalid item count: expected {} items, found {} items",
                expected_items, actual_items
            ),
            ArchivedHashMapError::MemoryError(e) => write!(f, "hash map memory error: {}", e),
            ArchivedHashMapError::BucketError(e) => write!(f, "hash map bucket error: {}", e),
            ArchivedHashMapError::IncorrectKeyHash { index } => {
                write!(f, "incorrect key hash: at index {}", index)
            }
        }
    }
}

impl<K: fmt::Debug + fmt::Display, V: fmt::Debug + fmt::Display> Error
    for ArchivedHashMapError<K, V>
{
}

impl<K, V> From<Unreachable> for ArchivedHashMapError<K, V> {
    fn from(_: Unreachable) -> Self {
        unreachable!();
    }
}

impl<K, V> From<ArchiveMemoryError> for ArchivedHashMapError<K, V> {
    fn from(e: ArchiveMemoryError) -> Self {
        Self::MemoryError(e)
    }
}

impl<K, V> From<ArchivedBucketError<K, V>> for ArchivedHashMapError<K, V> {
    fn from(e: ArchivedBucketError<K, V>) -> Self {
        Self::BucketError(e)
    }
}

impl<K: CheckBytes<ArchiveContext> + Eq + Hash, V: CheckBytes<ArchiveContext>>
    CheckBytes<ArchiveContext> for ArchivedHashMap<K, V>
{
    type Error = ArchivedHashMapError<K::Error, V::Error>;

    unsafe fn check_bytes<'a>(
        bytes: *const u8,
        context: &mut ArchiveContext,
    ) -> Result<&'a Self, Self::Error> {
        let bucket_mask = *u32::check_bytes(
            bytes.add(offset_of!(ArchivedHashMap<K, V>, bucket_mask)),
            context,
        )?;
        let buckets = bucket_mask as usize + 1;

        let ctrl_ptr =
            RelPtr::check_bytes(bytes.add(offset_of!(ArchivedHashMap<K, V>, ctrl)), context)?;
        let ctrl = context.claim_bytes(
            (ctrl_ptr as *const RelPtr).cast(),
            ctrl_ptr.offset(),
            buckets + Group::WIDTH,
            Group::WIDTH,
        )?;

        let data_ptr =
            RelPtr::check_bytes(bytes.add(offset_of!(ArchivedHashMap<K, V>, data)), context)?;
        let data = context.claim::<ArchivedBucket<K, V>>(
            (data_ptr as *const RelPtr).cast(),
            data_ptr.offset(),
            buckets,
        )?;

        let items = *u32::check_bytes(bytes.add(offset_of!(ArchivedHashMap<K, V>, items)), context)?
            as usize;

        let mut actual_items = 0;

        // Iterate through control bytes and check full buckets
        let mut current_group = Group::load_aligned(ctrl).match_full();
        let mut current_data = data.cast::<ArchivedBucket<K, V>>();
        let mut next_ctrl = ctrl.add(Group::WIDTH);
        let end = ctrl.add(buckets);

        loop {
            let next = loop {
                if let Some(index) = current_group.lowest_set_bit() {
                    current_group = current_group.remove_lowest_bit();
                    break Some(current_data.add(index));
                }

                if next_ctrl >= end {
                    break None;
                }

                current_group = Group::load_aligned(next_ctrl).match_full();
                current_data = current_data.add(Group::WIDTH);
                next_ctrl = next_ctrl.add(Group::WIDTH);
            };

            if let Some(bucket) = next {
                actual_items += 1;
                ArchivedBucket::<K, V>::check_bytes(bucket.cast(), context)?;
            } else {
                break;
            }
        }

        if items != actual_items {
            return Err(ArchivedHashMapError::InvalidItemCount {
                expected_items: items,
                actual_items,
            });
        }

        // At this point, everything checks out and we just need to make sure
        // that the keys point to their own position. That's something we can do
        // by just iterating through the hash map.
        let hash_map = &*bytes.cast::<ArchivedHashMap<K, V>>();
        for (index, key) in hash_map.keys().enumerate() {
            if !hash_map.contains_key(key) {
                return Err(ArchivedHashMapError::IncorrectKeyHash { index });
            }
        }

        Ok(hash_map)
    }
}

impl<K: CheckBytes<ArchiveContext> + Eq + Hash> CheckBytes<ArchiveContext> for ArchivedHashSet<K> {
    type Error = ArchivedHashMapError<K::Error, <() as CheckBytes<ArchiveContext>>::Error>;

    unsafe fn check_bytes<'a>(
        bytes: *const u8,
        context: &mut ArchiveContext,
    ) -> Result<&'a Self, Self::Error> {
        ArchivedHashMap::<K, ()>::check_bytes(bytes, context)?;
        Ok(&*bytes.cast())
    }
}