/rust/registry/src/index.crates.io-6f17d22bba15001f/rkyv-0.7.44/src/validation/validators/mod.rs
Line | Count | Source (jump to first uncovered line) |
1 | | //! Validators that can check archived types. |
2 | | |
3 | | mod archive; |
4 | | mod shared; |
5 | | mod util; |
6 | | |
7 | | use crate::{ |
8 | | validation::{ |
9 | | check_archived_root_with_context, check_archived_value_with_context, ArchiveContext, |
10 | | CheckTypeError, SharedContext, |
11 | | }, |
12 | | Archive, Fallible, |
13 | | }; |
14 | | pub use archive::*; |
15 | | use bytecheck::CheckBytes; |
16 | | use core::{ |
17 | | alloc::{Layout, LayoutError}, |
18 | | any::TypeId, |
19 | | fmt, |
20 | | }; |
21 | | pub use shared::*; |
22 | | pub use util::*; |
23 | | |
24 | | /// The default validator error. |
25 | | #[derive(Debug)] |
26 | | pub enum DefaultValidatorError { |
27 | | /// An archive validator error occurred. |
28 | | ArchiveError(ArchiveError), |
29 | | /// A shared validator error occurred. |
30 | | SharedError(SharedError), |
31 | | } |
32 | | |
33 | | impl fmt::Display for DefaultValidatorError { |
34 | 0 | fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { |
35 | 0 | match self { |
36 | 0 | Self::ArchiveError(e) => write!(f, "{}", e), |
37 | 0 | Self::SharedError(e) => write!(f, "{}", e), |
38 | | } |
39 | 0 | } Unexecuted instantiation: <rkyv::validation::validators::DefaultValidatorError as core::fmt::Display>::fmt Unexecuted instantiation: <rkyv::validation::validators::DefaultValidatorError as core::fmt::Display>::fmt |
40 | | } |
41 | | |
42 | | #[cfg(feature = "std")] |
43 | | const _: () = { |
44 | | use std::error::Error; |
45 | | |
46 | | impl Error for DefaultValidatorError { |
47 | 0 | fn source(&self) -> Option<&(dyn Error + 'static)> { |
48 | 0 | match self { |
49 | 0 | Self::ArchiveError(e) => Some(e as &dyn Error), |
50 | 0 | Self::SharedError(e) => Some(e as &dyn Error), |
51 | | } |
52 | 0 | } Unexecuted instantiation: <rkyv::validation::validators::DefaultValidatorError as core::error::Error>::source Unexecuted instantiation: <rkyv::validation::validators::DefaultValidatorError as core::error::Error>::source Unexecuted instantiation: <rkyv::validation::validators::DefaultValidatorError as core::error::Error>::source Unexecuted instantiation: <rkyv::validation::validators::DefaultValidatorError as core::error::Error>::source |
53 | | } |
54 | | }; |
55 | | |
56 | | /// The default validator. |
57 | | #[derive(Debug)] |
58 | | pub struct DefaultValidator<'a> { |
59 | | archive: ArchiveValidator<'a>, |
60 | | shared: SharedValidator, |
61 | | } |
62 | | |
63 | | impl<'a> DefaultValidator<'a> { |
64 | | /// Creates a new validator from a byte range. |
65 | | #[inline] |
66 | 0 | pub fn new(bytes: &'a [u8]) -> Self { |
67 | 0 | Self { |
68 | 0 | archive: ArchiveValidator::new(bytes), |
69 | 0 | shared: SharedValidator::new(), |
70 | 0 | } |
71 | 0 | } Unexecuted instantiation: <rkyv::validation::validators::DefaultValidator>::new Unexecuted instantiation: <rkyv::validation::validators::DefaultValidator>::new Unexecuted instantiation: <rkyv::validation::validators::DefaultValidator>::new Unexecuted instantiation: <rkyv::validation::validators::DefaultValidator>::new |
72 | | |
73 | | /// Create a new validator from a byte range with specific capacity. |
74 | | #[inline] |
75 | 0 | pub fn with_capacity(bytes: &'a [u8], capacity: usize) -> Self { |
76 | 0 | Self { |
77 | 0 | archive: ArchiveValidator::new(bytes), |
78 | 0 | shared: SharedValidator::with_capacity(capacity), |
79 | 0 | } |
80 | 0 | } Unexecuted instantiation: <rkyv::validation::validators::DefaultValidator>::with_capacity Unexecuted instantiation: <rkyv::validation::validators::DefaultValidator>::with_capacity |
81 | | } |
82 | | |
83 | | impl<'a> Fallible for DefaultValidator<'a> { |
84 | | type Error = DefaultValidatorError; |
85 | | } |
86 | | |
87 | | impl<'a> ArchiveContext for DefaultValidator<'a> { |
88 | | type PrefixRange = <ArchiveValidator<'a> as ArchiveContext>::PrefixRange; |
89 | | type SuffixRange = <ArchiveValidator<'a> as ArchiveContext>::SuffixRange; |
90 | | |
91 | | #[inline] |
92 | 0 | unsafe fn bounds_check_ptr( |
93 | 0 | &mut self, |
94 | 0 | base: *const u8, |
95 | 0 | offset: isize, |
96 | 0 | ) -> Result<*const u8, Self::Error> { |
97 | 0 | self.archive |
98 | 0 | .bounds_check_ptr(base, offset) |
99 | 0 | .map_err(DefaultValidatorError::ArchiveError) |
100 | 0 | } Unexecuted instantiation: <rkyv::validation::validators::DefaultValidator as rkyv::validation::ArchiveContext>::bounds_check_ptr Unexecuted instantiation: <rkyv::validation::validators::DefaultValidator as rkyv::validation::ArchiveContext>::bounds_check_ptr Unexecuted instantiation: <rkyv::validation::validators::DefaultValidator as rkyv::validation::ArchiveContext>::bounds_check_ptr Unexecuted instantiation: <rkyv::validation::validators::DefaultValidator as rkyv::validation::ArchiveContext>::bounds_check_ptr |
101 | | |
102 | | #[inline] |
103 | 0 | unsafe fn bounds_check_layout( |
104 | 0 | &mut self, |
105 | 0 | data_address: *const u8, |
106 | 0 | layout: &Layout, |
107 | 0 | ) -> Result<(), Self::Error> { |
108 | 0 | self.archive |
109 | 0 | .bounds_check_layout(data_address, layout) |
110 | 0 | .map_err(DefaultValidatorError::ArchiveError) |
111 | 0 | } Unexecuted instantiation: <rkyv::validation::validators::DefaultValidator as rkyv::validation::ArchiveContext>::bounds_check_layout Unexecuted instantiation: <rkyv::validation::validators::DefaultValidator as rkyv::validation::ArchiveContext>::bounds_check_layout Unexecuted instantiation: <rkyv::validation::validators::DefaultValidator as rkyv::validation::ArchiveContext>::bounds_check_layout Unexecuted instantiation: <rkyv::validation::validators::DefaultValidator as rkyv::validation::ArchiveContext>::bounds_check_layout |
112 | | |
113 | | #[inline] |
114 | 0 | unsafe fn bounds_check_subtree_ptr_layout( |
115 | 0 | &mut self, |
116 | 0 | data_address: *const u8, |
117 | 0 | layout: &Layout, |
118 | 0 | ) -> Result<(), Self::Error> { |
119 | 0 | self.archive |
120 | 0 | .bounds_check_subtree_ptr_layout(data_address, layout) |
121 | 0 | .map_err(DefaultValidatorError::ArchiveError) |
122 | 0 | } Unexecuted instantiation: <rkyv::validation::validators::DefaultValidator as rkyv::validation::ArchiveContext>::bounds_check_subtree_ptr_layout Unexecuted instantiation: <rkyv::validation::validators::DefaultValidator as rkyv::validation::ArchiveContext>::bounds_check_subtree_ptr_layout Unexecuted instantiation: <rkyv::validation::validators::DefaultValidator as rkyv::validation::ArchiveContext>::bounds_check_subtree_ptr_layout Unexecuted instantiation: <rkyv::validation::validators::DefaultValidator as rkyv::validation::ArchiveContext>::bounds_check_subtree_ptr_layout |
123 | | |
124 | | #[inline] |
125 | 0 | unsafe fn push_prefix_subtree_range( |
126 | 0 | &mut self, |
127 | 0 | root: *const u8, |
128 | 0 | end: *const u8, |
129 | 0 | ) -> Result<PrefixRange, Self::Error> { |
130 | 0 | self.archive |
131 | 0 | .push_prefix_subtree_range(root, end) |
132 | 0 | .map_err(DefaultValidatorError::ArchiveError) |
133 | 0 | } Unexecuted instantiation: <rkyv::validation::validators::DefaultValidator as rkyv::validation::ArchiveContext>::push_prefix_subtree_range Unexecuted instantiation: <rkyv::validation::validators::DefaultValidator as rkyv::validation::ArchiveContext>::push_prefix_subtree_range Unexecuted instantiation: <rkyv::validation::validators::DefaultValidator as rkyv::validation::ArchiveContext>::push_prefix_subtree_range Unexecuted instantiation: <rkyv::validation::validators::DefaultValidator as rkyv::validation::ArchiveContext>::push_prefix_subtree_range |
134 | | |
135 | | #[inline] |
136 | 0 | fn pop_prefix_range(&mut self, range: PrefixRange) -> Result<(), Self::Error> { |
137 | 0 | self.archive |
138 | 0 | .pop_prefix_range(range) |
139 | 0 | .map_err(DefaultValidatorError::ArchiveError) |
140 | 0 | } Unexecuted instantiation: <rkyv::validation::validators::DefaultValidator as rkyv::validation::ArchiveContext>::pop_prefix_range Unexecuted instantiation: <rkyv::validation::validators::DefaultValidator as rkyv::validation::ArchiveContext>::pop_prefix_range Unexecuted instantiation: <rkyv::validation::validators::DefaultValidator as rkyv::validation::ArchiveContext>::pop_prefix_range Unexecuted instantiation: <rkyv::validation::validators::DefaultValidator as rkyv::validation::ArchiveContext>::pop_prefix_range |
141 | | |
142 | | #[inline] |
143 | 0 | unsafe fn push_suffix_subtree_range( |
144 | 0 | &mut self, |
145 | 0 | start: *const u8, |
146 | 0 | root: *const u8, |
147 | 0 | ) -> Result<SuffixRange, Self::Error> { |
148 | 0 | self.archive |
149 | 0 | .push_suffix_subtree_range(start, root) |
150 | 0 | .map_err(DefaultValidatorError::ArchiveError) |
151 | 0 | } Unexecuted instantiation: <rkyv::validation::validators::DefaultValidator as rkyv::validation::ArchiveContext>::push_suffix_subtree_range Unexecuted instantiation: <rkyv::validation::validators::DefaultValidator as rkyv::validation::ArchiveContext>::push_suffix_subtree_range Unexecuted instantiation: <rkyv::validation::validators::DefaultValidator as rkyv::validation::ArchiveContext>::push_suffix_subtree_range Unexecuted instantiation: <rkyv::validation::validators::DefaultValidator as rkyv::validation::ArchiveContext>::push_suffix_subtree_range |
152 | | |
153 | | #[inline] |
154 | 0 | fn pop_suffix_range(&mut self, range: SuffixRange) -> Result<(), Self::Error> { |
155 | 0 | self.archive |
156 | 0 | .pop_suffix_range(range) |
157 | 0 | .map_err(DefaultValidatorError::ArchiveError) |
158 | 0 | } Unexecuted instantiation: <rkyv::validation::validators::DefaultValidator as rkyv::validation::ArchiveContext>::pop_suffix_range Unexecuted instantiation: <rkyv::validation::validators::DefaultValidator as rkyv::validation::ArchiveContext>::pop_suffix_range Unexecuted instantiation: <rkyv::validation::validators::DefaultValidator as rkyv::validation::ArchiveContext>::pop_suffix_range Unexecuted instantiation: <rkyv::validation::validators::DefaultValidator as rkyv::validation::ArchiveContext>::pop_suffix_range |
159 | | |
160 | | #[inline] |
161 | 0 | fn finish(&mut self) -> Result<(), Self::Error> { |
162 | 0 | self.archive |
163 | 0 | .finish() |
164 | 0 | .map_err(DefaultValidatorError::ArchiveError) |
165 | 0 | } Unexecuted instantiation: <rkyv::validation::validators::DefaultValidator as rkyv::validation::ArchiveContext>::finish Unexecuted instantiation: <rkyv::validation::validators::DefaultValidator as rkyv::validation::ArchiveContext>::finish Unexecuted instantiation: <rkyv::validation::validators::DefaultValidator as rkyv::validation::ArchiveContext>::finish Unexecuted instantiation: <rkyv::validation::validators::DefaultValidator as rkyv::validation::ArchiveContext>::finish |
166 | | |
167 | | #[inline] |
168 | 0 | fn wrap_layout_error(error: LayoutError) -> Self::Error { |
169 | 0 | DefaultValidatorError::ArchiveError(ArchiveValidator::wrap_layout_error(error)) |
170 | 0 | } Unexecuted instantiation: <rkyv::validation::validators::DefaultValidator as rkyv::validation::ArchiveContext>::wrap_layout_error Unexecuted instantiation: <rkyv::validation::validators::DefaultValidator as rkyv::validation::ArchiveContext>::wrap_layout_error Unexecuted instantiation: <rkyv::validation::validators::DefaultValidator as rkyv::validation::ArchiveContext>::wrap_layout_error Unexecuted instantiation: <rkyv::validation::validators::DefaultValidator as rkyv::validation::ArchiveContext>::wrap_layout_error |
171 | | } |
172 | | |
173 | | impl<'a> SharedContext for DefaultValidator<'a> { |
174 | | #[inline] |
175 | 0 | fn register_shared_ptr( |
176 | 0 | &mut self, |
177 | 0 | ptr: *const u8, |
178 | 0 | type_id: TypeId, |
179 | 0 | ) -> Result<bool, Self::Error> { |
180 | 0 | self.shared |
181 | 0 | .register_shared_ptr(ptr, type_id) |
182 | 0 | .map_err(DefaultValidatorError::SharedError) |
183 | 0 | } Unexecuted instantiation: <rkyv::validation::validators::DefaultValidator as rkyv::validation::SharedContext>::register_shared_ptr Unexecuted instantiation: <rkyv::validation::validators::DefaultValidator as rkyv::validation::SharedContext>::register_shared_ptr Unexecuted instantiation: <rkyv::validation::validators::DefaultValidator as rkyv::validation::SharedContext>::register_shared_ptr Unexecuted instantiation: <rkyv::validation::validators::DefaultValidator as rkyv::validation::SharedContext>::register_shared_ptr |
184 | | } |
185 | | |
186 | | /// Checks the given archive at the given position for an archived version of the given type. |
187 | | /// |
188 | | /// This is a safe alternative to [`archived_value`](crate::archived_value) for types that implement |
189 | | /// `CheckBytes`. |
190 | | /// |
191 | | /// # Examples |
192 | | /// ``` |
193 | | /// use rkyv::{ |
194 | | /// check_archived_value, |
195 | | /// ser::{Serializer, serializers::AlignedSerializer}, |
196 | | /// AlignedVec, |
197 | | /// Archive, |
198 | | /// Serialize, |
199 | | /// }; |
200 | | /// use bytecheck::CheckBytes; |
201 | | /// |
202 | | /// #[derive(Archive, Serialize)] |
203 | | /// #[archive_attr(derive(CheckBytes))] |
204 | | /// struct Example { |
205 | | /// name: String, |
206 | | /// value: i32, |
207 | | /// } |
208 | | /// |
209 | | /// let value = Example { |
210 | | /// name: "pi".to_string(), |
211 | | /// value: 31415926, |
212 | | /// }; |
213 | | /// |
214 | | /// let mut serializer = AlignedSerializer::new(AlignedVec::new()); |
215 | | /// let pos = serializer.serialize_value(&value) |
216 | | /// .expect("failed to archive test"); |
217 | | /// let buf = serializer.into_inner(); |
218 | | /// let archived = check_archived_value::<Example>(buf.as_ref(), pos).unwrap(); |
219 | | /// ``` |
220 | | #[inline] |
221 | 0 | pub fn check_archived_value<'a, T: Archive>( |
222 | 0 | bytes: &'a [u8], |
223 | 0 | pos: usize, |
224 | 0 | ) -> Result<&T::Archived, CheckTypeError<T::Archived, DefaultValidator<'a>>> |
225 | 0 | where |
226 | 0 | T::Archived: CheckBytes<DefaultValidator<'a>>, |
227 | 0 | { |
228 | 0 | let mut validator = DefaultValidator::new(bytes); |
229 | 0 | check_archived_value_with_context::<T, DefaultValidator>(bytes, pos, &mut validator) |
230 | 0 | } Unexecuted instantiation: rkyv::validation::validators::check_archived_value::<wasmer_types::serialize::SerializableModule> Unexecuted instantiation: rkyv::validation::validators::check_archived_value::<wasmer_types::compilation::symbols::ModuleMetadata> Unexecuted instantiation: rkyv::validation::validators::check_archived_value::<_> Unexecuted instantiation: rkyv::validation::validators::check_archived_value::<wasmer_types::serialize::SerializableModule> Unexecuted instantiation: rkyv::validation::validators::check_archived_value::<wasmer_types::compilation::symbols::ModuleMetadata> Unexecuted instantiation: rkyv::validation::validators::check_archived_value::<_> |
231 | | |
232 | | /// Checks the given archive at the given position for an archived version of the given type. |
233 | | /// |
234 | | /// This is a safe alternative to [`archived_value`](crate::archived_value) for types that implement |
235 | | /// `CheckBytes`. |
236 | | /// |
237 | | /// See [`check_archived_value`] for more details. |
238 | | #[inline] |
239 | 0 | pub fn check_archived_root<'a, T: Archive>( |
240 | 0 | bytes: &'a [u8], |
241 | 0 | ) -> Result<&'a T::Archived, CheckTypeError<T::Archived, DefaultValidator<'a>>> |
242 | 0 | where |
243 | 0 | T::Archived: CheckBytes<DefaultValidator<'a>>, |
244 | 0 | { |
245 | 0 | let mut validator = DefaultValidator::new(bytes); |
246 | 0 | check_archived_root_with_context::<T, DefaultValidator>(bytes, &mut validator) |
247 | 0 | } Unexecuted instantiation: rkyv::validation::validators::check_archived_root::<_> Unexecuted instantiation: rkyv::validation::validators::check_archived_root::<_> |