Coverage Report

Created: 2024-10-16 07:58

/rust/registry/src/index.crates.io-6f17d22bba15001f/rkyv-0.7.44/src/rc/validation.rs
Line
Count
Source (jump to first uncovered line)
1
//! Validation implementations for shared pointers.
2
3
use super::{ArchivedRc, ArchivedRcWeak, ArchivedRcWeakTag, ArchivedRcWeakVariantSome};
4
use crate::{
5
    validation::{ArchiveContext, LayoutRaw, SharedContext},
6
    ArchivePointee, RelPtr,
7
};
8
use bytecheck::{CheckBytes, Error};
9
use core::{any::TypeId, convert::Infallible, fmt, ptr};
10
use ptr_meta::Pointee;
11
12
/// Errors that can occur while checking archived shared pointers.
13
#[derive(Debug)]
14
pub enum SharedPointerError<T, R, C> {
15
    /// An error occurred while checking the bytes of a shared value
16
    PointerCheckBytesError(T),
17
    /// An error occurred while checking the bytes of a shared reference
18
    ValueCheckBytesError(R),
19
    /// A context error occurred
20
    ContextError(C),
21
}
22
23
impl<T, R, C> fmt::Display for SharedPointerError<T, R, C>
24
where
25
    T: fmt::Display,
26
    R: fmt::Display,
27
    C: fmt::Display,
28
{
29
0
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
30
0
        match self {
31
0
            SharedPointerError::PointerCheckBytesError(e) => e.fmt(f),
32
0
            SharedPointerError::ValueCheckBytesError(e) => e.fmt(f),
33
0
            SharedPointerError::ContextError(e) => e.fmt(f),
34
        }
35
0
    }
Unexecuted instantiation: <rkyv::rc::validation::SharedPointerError<core::convert::Infallible, bytecheck::StructCheckError, rkyv::validation::validators::DefaultValidatorError> as core::fmt::Display>::fmt
Unexecuted instantiation: <rkyv::rc::validation::SharedPointerError<_, _, _> as core::fmt::Display>::fmt
Unexecuted instantiation: <rkyv::rc::validation::SharedPointerError<core::convert::Infallible, bytecheck::StructCheckError, rkyv::validation::validators::DefaultValidatorError> as core::fmt::Display>::fmt
Unexecuted instantiation: <rkyv::rc::validation::SharedPointerError<_, _, _> as core::fmt::Display>::fmt
36
}
37
38
#[cfg(feature = "std")]
39
const _: () = {
40
    use std::error::Error;
41
42
    impl<T, R, C> Error for SharedPointerError<T, R, C>
43
    where
44
        T: Error + 'static,
45
        R: Error + 'static,
46
        C: Error + 'static,
47
    {
48
0
        fn source(&self) -> Option<&(dyn Error + 'static)> {
49
0
            match self {
50
0
                SharedPointerError::PointerCheckBytesError(e) => Some(e as &dyn Error),
51
0
                SharedPointerError::ValueCheckBytesError(e) => Some(e as &dyn Error),
52
0
                SharedPointerError::ContextError(e) => Some(e as &dyn Error),
53
            }
54
0
        }
Unexecuted instantiation: <rkyv::rc::validation::SharedPointerError<core::convert::Infallible, bytecheck::StructCheckError, rkyv::validation::validators::DefaultValidatorError> as core::error::Error>::source
Unexecuted instantiation: <rkyv::rc::validation::SharedPointerError<_, _, _> as core::error::Error>::source
Unexecuted instantiation: <rkyv::rc::validation::SharedPointerError<core::convert::Infallible, bytecheck::StructCheckError, rkyv::validation::validators::DefaultValidatorError> as core::error::Error>::source
Unexecuted instantiation: <rkyv::rc::validation::SharedPointerError<_, _, _> as core::error::Error>::source
55
    }
56
};
57
58
/// Errors that can occur while checking archived weak pointers.
59
#[derive(Debug)]
60
pub enum WeakPointerError<T, R, C> {
61
    /// The weak pointer had an invalid tag
62
    InvalidTag(u8),
63
    /// An error occurred while checking the underlying shared pointer
64
    CheckBytes(SharedPointerError<T, R, C>),
65
}
66
67
impl<T: fmt::Display, R: fmt::Display, C: fmt::Display> fmt::Display for WeakPointerError<T, R, C> {
68
0
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
69
0
        match self {
70
0
            WeakPointerError::InvalidTag(tag) => {
71
0
                write!(f, "archived weak had invalid tag: {}", tag)
72
            }
73
0
            WeakPointerError::CheckBytes(e) => e.fmt(f),
74
        }
75
0
    }
Unexecuted instantiation: <rkyv::rc::validation::WeakPointerError<_, _, _> as core::fmt::Display>::fmt
Unexecuted instantiation: <rkyv::rc::validation::WeakPointerError<_, _, _> as core::fmt::Display>::fmt
76
}
77
78
#[cfg(feature = "std")]
79
const _: () = {
80
    use std::error::Error;
81
82
    impl<T, R, C> Error for WeakPointerError<T, R, C>
83
    where
84
        T: Error + 'static,
85
        R: Error + 'static,
86
        C: Error + 'static,
87
    {
88
0
        fn source(&self) -> Option<&(dyn Error + 'static)> {
89
0
            match self {
90
0
                WeakPointerError::InvalidTag(_) => None,
91
0
                WeakPointerError::CheckBytes(e) => Some(e as &dyn Error),
92
            }
93
0
        }
Unexecuted instantiation: <rkyv::rc::validation::WeakPointerError<_, _, _> as core::error::Error>::source
Unexecuted instantiation: <rkyv::rc::validation::WeakPointerError<_, _, _> as core::error::Error>::source
94
    }
95
};
96
97
impl<T, R, C> From<Infallible> for WeakPointerError<T, R, C> {
98
0
    fn from(_: Infallible) -> Self {
99
0
        unsafe { core::hint::unreachable_unchecked() }
Unexecuted instantiation: <rkyv::rc::validation::WeakPointerError<_, _, _> as core::convert::From<core::convert::Infallible>>::from
Unexecuted instantiation: <rkyv::rc::validation::WeakPointerError<_, _, _> as core::convert::From<core::convert::Infallible>>::from
100
    }
101
}
102
103
impl<T, F, C> CheckBytes<C> for ArchivedRc<T, F>
104
where
105
    T: ArchivePointee + CheckBytes<C> + LayoutRaw + Pointee + ?Sized + 'static,
106
    C: ArchiveContext + SharedContext + ?Sized,
107
    T::ArchivedMetadata: CheckBytes<C>,
108
    C::Error: Error,
109
    F: 'static,
110
{
111
    type Error =
112
        SharedPointerError<<T::ArchivedMetadata as CheckBytes<C>>::Error, T::Error, C::Error>;
113
114
    #[inline]
115
0
    unsafe fn check_bytes<'a>(
116
0
        value: *const Self,
117
0
        context: &mut C,
118
0
    ) -> Result<&'a Self, Self::Error> {
119
0
        let rel_ptr = RelPtr::<T>::manual_check_bytes(value.cast(), context)
120
0
            .map_err(SharedPointerError::PointerCheckBytesError)?;
121
0
        let ptr = context
122
0
            .check_rel_ptr(rel_ptr)
123
0
            .map_err(SharedPointerError::ContextError)?;
124
125
0
        let type_id = TypeId::of::<Self>();
126
0
        if context
127
0
            .register_shared_ptr(ptr.cast(), type_id)
128
0
            .map_err(SharedPointerError::ContextError)?
129
        {
130
0
            context
131
0
                .bounds_check_subtree_ptr(ptr)
132
0
                .map_err(SharedPointerError::ContextError)?;
133
134
0
            let range = context
135
0
                .push_prefix_subtree(ptr)
136
0
                .map_err(SharedPointerError::ContextError)?;
137
0
            T::check_bytes(ptr, context).map_err(SharedPointerError::ValueCheckBytesError)?;
138
0
            context
139
0
                .pop_prefix_range(range)
140
0
                .map_err(SharedPointerError::ContextError)?;
141
0
        }
142
0
        Ok(&*value)
143
0
    }
Unexecuted instantiation: <rkyv::rc::ArchivedRc<wasmer_types::module::ArchivedArchivableModuleInfo, rkyv::impls::alloc::rc::ArcFlavor> as bytecheck::CheckBytes<rkyv::validation::validators::DefaultValidator>>::check_bytes
Unexecuted instantiation: <rkyv::rc::ArchivedRc<_, _> as bytecheck::CheckBytes<_>>::check_bytes
Unexecuted instantiation: <rkyv::rc::ArchivedRc<wasmer_types::module::ArchivedArchivableModuleInfo, rkyv::impls::alloc::rc::ArcFlavor> as bytecheck::CheckBytes<rkyv::validation::validators::DefaultValidator>>::check_bytes
Unexecuted instantiation: <rkyv::rc::ArchivedRc<_, _> as bytecheck::CheckBytes<_>>::check_bytes
144
}
145
146
impl ArchivedRcWeakTag {
147
    const TAG_NONE: u8 = ArchivedRcWeakTag::None as u8;
148
    const TAG_SOME: u8 = ArchivedRcWeakTag::Some as u8;
149
}
150
151
impl<T, F, C> CheckBytes<C> for ArchivedRcWeak<T, F>
152
where
153
    T: ArchivePointee + CheckBytes<C> + LayoutRaw + Pointee + ?Sized + 'static,
154
    C: ArchiveContext + SharedContext + ?Sized,
155
    T::ArchivedMetadata: CheckBytes<C>,
156
    C::Error: Error,
157
    F: 'static,
158
{
159
    type Error =
160
        WeakPointerError<<T::ArchivedMetadata as CheckBytes<C>>::Error, T::Error, C::Error>;
161
162
    #[inline]
163
0
    unsafe fn check_bytes<'a>(
164
0
        value: *const Self,
165
0
        context: &mut C,
166
0
    ) -> Result<&'a Self, Self::Error> {
167
0
        let tag = *u8::check_bytes(value.cast::<u8>(), context)?;
168
0
        match tag {
169
0
            ArchivedRcWeakTag::TAG_NONE => (),
170
            ArchivedRcWeakTag::TAG_SOME => {
171
0
                let value = value.cast::<ArchivedRcWeakVariantSome<T, F>>();
172
0
                ArchivedRc::<T, F>::check_bytes(ptr::addr_of!((*value).1), context)
173
0
                    .map_err(WeakPointerError::CheckBytes)?;
174
            }
175
0
            _ => return Err(WeakPointerError::InvalidTag(tag)),
176
        }
177
0
        Ok(&*value)
178
0
    }
Unexecuted instantiation: <rkyv::rc::ArchivedRcWeak<_, _> as bytecheck::CheckBytes<_>>::check_bytes
Unexecuted instantiation: <rkyv::rc::ArchivedRcWeak<_, _> as bytecheck::CheckBytes<_>>::check_bytes
179
}