/rust/registry/src/index.crates.io-6f17d22bba15001f/wasmparser-0.121.2/src/resources.rs
Line | Count | Source (jump to first uncovered line) |
1 | | /* Copyright 2019 Mozilla Foundation |
2 | | * |
3 | | * Licensed under the Apache License, Version 2.0 (the "License"); |
4 | | * you may not use this file except in compliance with the License. |
5 | | * You may obtain a copy of the License at |
6 | | * |
7 | | * http://www.apache.org/licenses/LICENSE-2.0 |
8 | | * |
9 | | * Unless required by applicable law or agreed to in writing, software |
10 | | * distributed under the License is distributed on an "AS IS" BASIS, |
11 | | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
12 | | * See the License for the specific language governing permissions and |
13 | | * limitations under the License. |
14 | | */ |
15 | | |
16 | | use crate::{ |
17 | | types::CoreTypeId, BinaryReaderError, FuncType, GlobalType, HeapType, MemoryType, RefType, |
18 | | SubType, TableType, ValType, WasmFeatures, |
19 | | }; |
20 | | |
21 | | /// Types that qualify as Wasm validation database. |
22 | | /// |
23 | | /// # Note |
24 | | /// |
25 | | /// The `wasmparser` crate provides a builtin validation framework but allows |
26 | | /// users of this crate to also feed the parsed Wasm into their own data |
27 | | /// structure while parsing and also validate at the same time without |
28 | | /// the need of an additional parsing or validation step or copying data around. |
29 | | pub trait WasmModuleResources { |
30 | | /// Returns the table at given index if any. |
31 | | /// |
32 | | /// The table element type must be canonicalized. |
33 | | fn table_at(&self, at: u32) -> Option<TableType>; |
34 | | |
35 | | /// Returns the linear memory at given index. |
36 | | fn memory_at(&self, at: u32) -> Option<MemoryType>; |
37 | | |
38 | | /// Returns the tag at given index. |
39 | | /// |
40 | | /// The tag's function type must be canonicalized. |
41 | | fn tag_at(&self, at: u32) -> Option<&FuncType>; |
42 | | |
43 | | /// Returns the global variable at given index. |
44 | | /// |
45 | | /// The global's value type must be canonicalized. |
46 | | fn global_at(&self, at: u32) -> Option<GlobalType>; |
47 | | |
48 | | /// Returns the `SubType` associated with the given type index. |
49 | | /// |
50 | | /// The sub type must be canonicalized. |
51 | | fn sub_type_at(&self, type_index: u32) -> Option<&SubType>; |
52 | | |
53 | | /// Returns the type id associated with the given function |
54 | | /// index. |
55 | | fn type_id_of_function(&self, func_idx: u32) -> Option<CoreTypeId>; |
56 | | |
57 | | /// Returns the `FuncType` associated with the given function index. |
58 | | /// |
59 | | /// The function type must be canonicalized. |
60 | | fn type_of_function(&self, func_idx: u32) -> Option<&FuncType>; |
61 | | |
62 | | /// Returns the element type at the given index. |
63 | | /// |
64 | | /// The `RefType` must be canonicalized. |
65 | | fn element_type_at(&self, at: u32) -> Option<RefType>; |
66 | | |
67 | | /// Is `a` a subtype of `b`? |
68 | | fn is_subtype(&self, a: ValType, b: ValType) -> bool; |
69 | | |
70 | | /// Check and canonicalize a value type. |
71 | | /// |
72 | | /// This will validate that `t` is valid under the `features` provided and |
73 | | /// then additionally validate the structure of `t`. For example any type |
74 | | /// references that `t` makes are validated and canonicalized. |
75 | 3.50M | fn check_value_type( |
76 | 3.50M | &self, |
77 | 3.50M | t: &mut ValType, |
78 | 3.50M | features: &WasmFeatures, |
79 | 3.50M | offset: usize, |
80 | 3.50M | ) -> Result<(), BinaryReaderError> { |
81 | 3.50M | features |
82 | 3.50M | .check_value_type(*t) |
83 | 3.50M | .map_err(|s| BinaryReaderError::new(s, offset))?; Unexecuted instantiation: <wasmparser::validator::core::ValidatorResources as wasmparser::resources::WasmModuleResources>::check_value_type::{closure#0}Unexecuted instantiation: <wasmparser::validator::core::OperatorValidatorResources as wasmparser::resources::WasmModuleResources>::check_value_type::{closure#0} |
84 | 3.50M | match t { |
85 | 0 | ValType::Ref(r) => self.check_ref_type(r, offset), |
86 | 3.50M | ValType::I32 | ValType::I64 | ValType::F32 | ValType::F64 | ValType::V128 => Ok(()), |
87 | | } |
88 | 3.50M | } <wasmparser::validator::core::ValidatorResources as wasmparser::resources::WasmModuleResources>::check_value_type Line | Count | Source | 75 | 3.50M | fn check_value_type( | 76 | 3.50M | &self, | 77 | 3.50M | t: &mut ValType, | 78 | 3.50M | features: &WasmFeatures, | 79 | 3.50M | offset: usize, | 80 | 3.50M | ) -> Result<(), BinaryReaderError> { | 81 | 3.50M | features | 82 | 3.50M | .check_value_type(*t) | 83 | 3.50M | .map_err(|s| BinaryReaderError::new(s, offset))?; | 84 | 3.50M | match t { | 85 | 0 | ValType::Ref(r) => self.check_ref_type(r, offset), | 86 | 3.50M | ValType::I32 | ValType::I64 | ValType::F32 | ValType::F64 | ValType::V128 => Ok(()), | 87 | | } | 88 | 3.50M | } |
Unexecuted instantiation: <wasmparser::validator::core::OperatorValidatorResources as wasmparser::resources::WasmModuleResources>::check_value_type |
89 | | |
90 | | /// Check and canonicalize a reference type. |
91 | 0 | fn check_ref_type( |
92 | 0 | &self, |
93 | 0 | ref_type: &mut RefType, |
94 | 0 | offset: usize, |
95 | 0 | ) -> Result<(), BinaryReaderError> { |
96 | 0 | let is_nullable = ref_type.is_nullable(); |
97 | 0 | let mut heap_ty = ref_type.heap_type(); |
98 | 0 | self.check_heap_type(&mut heap_ty, offset)?; |
99 | 0 | *ref_type = RefType::new(is_nullable, heap_ty).unwrap(); |
100 | 0 | Ok(()) |
101 | 0 | } Unexecuted instantiation: <wasmparser::validator::core::ValidatorResources as wasmparser::resources::WasmModuleResources>::check_ref_type Unexecuted instantiation: <wasmparser::validator::core::OperatorValidatorResources as wasmparser::resources::WasmModuleResources>::check_ref_type |
102 | | |
103 | | /// Checks that a `HeapType` is valid and then additionally place it in its |
104 | | /// canonical form. |
105 | | /// |
106 | | /// Similar to `check_value_type` but for heap types. |
107 | | fn check_heap_type( |
108 | | &self, |
109 | | heap_type: &mut HeapType, |
110 | | offset: usize, |
111 | | ) -> Result<(), BinaryReaderError>; |
112 | | |
113 | | /// Get the top type for the given heap type. |
114 | | fn top_type(&self, heap_type: &HeapType) -> HeapType; |
115 | | |
116 | | /// Returns the number of elements. |
117 | | fn element_count(&self) -> u32; |
118 | | |
119 | | /// Returns the number of bytes in the Wasm data section. |
120 | | fn data_count(&self) -> Option<u32>; |
121 | | |
122 | | /// Returns whether the function index is referenced in the module anywhere |
123 | | /// outside of the start/function sections. |
124 | | fn is_function_referenced(&self, idx: u32) -> bool; |
125 | | } |
126 | | |
127 | | impl<T> WasmModuleResources for &'_ T |
128 | | where |
129 | | T: ?Sized + WasmModuleResources, |
130 | | { |
131 | 0 | fn table_at(&self, at: u32) -> Option<TableType> { |
132 | 0 | T::table_at(self, at) |
133 | 0 | } |
134 | 0 | fn memory_at(&self, at: u32) -> Option<MemoryType> { |
135 | 0 | T::memory_at(self, at) |
136 | 0 | } |
137 | 0 | fn tag_at(&self, at: u32) -> Option<&FuncType> { |
138 | 0 | T::tag_at(self, at) |
139 | 0 | } |
140 | 0 | fn global_at(&self, at: u32) -> Option<GlobalType> { |
141 | 0 | T::global_at(self, at) |
142 | 0 | } |
143 | 0 | fn sub_type_at(&self, at: u32) -> Option<&SubType> { |
144 | 0 | T::sub_type_at(self, at) |
145 | 0 | } |
146 | 0 | fn type_id_of_function(&self, func_idx: u32) -> Option<CoreTypeId> { |
147 | 0 | T::type_id_of_function(self, func_idx) |
148 | 0 | } |
149 | 0 | fn type_of_function(&self, func_idx: u32) -> Option<&FuncType> { |
150 | 0 | T::type_of_function(self, func_idx) |
151 | 0 | } |
152 | 0 | fn check_heap_type(&self, t: &mut HeapType, offset: usize) -> Result<(), BinaryReaderError> { |
153 | 0 | T::check_heap_type(self, t, offset) |
154 | 0 | } |
155 | 0 | fn top_type(&self, heap_type: &HeapType) -> HeapType { |
156 | 0 | T::top_type(self, heap_type) |
157 | 0 | } |
158 | 0 | fn element_type_at(&self, at: u32) -> Option<RefType> { |
159 | 0 | T::element_type_at(self, at) |
160 | 0 | } |
161 | 0 | fn is_subtype(&self, a: ValType, b: ValType) -> bool { |
162 | 0 | T::is_subtype(self, a, b) |
163 | 0 | } |
164 | | |
165 | 0 | fn element_count(&self) -> u32 { |
166 | 0 | T::element_count(self) |
167 | 0 | } |
168 | 0 | fn data_count(&self) -> Option<u32> { |
169 | 0 | T::data_count(self) |
170 | 0 | } |
171 | 0 | fn is_function_referenced(&self, idx: u32) -> bool { |
172 | 0 | T::is_function_referenced(self, idx) |
173 | 0 | } |
174 | | } |
175 | | |
176 | | impl<T> WasmModuleResources for std::sync::Arc<T> |
177 | | where |
178 | | T: WasmModuleResources, |
179 | | { |
180 | 0 | fn table_at(&self, at: u32) -> Option<TableType> { |
181 | 0 | T::table_at(self, at) |
182 | 0 | } |
183 | | |
184 | 0 | fn memory_at(&self, at: u32) -> Option<MemoryType> { |
185 | 0 | T::memory_at(self, at) |
186 | 0 | } |
187 | | |
188 | 0 | fn tag_at(&self, at: u32) -> Option<&FuncType> { |
189 | 0 | T::tag_at(self, at) |
190 | 0 | } |
191 | | |
192 | 0 | fn global_at(&self, at: u32) -> Option<GlobalType> { |
193 | 0 | T::global_at(self, at) |
194 | 0 | } |
195 | | |
196 | 0 | fn sub_type_at(&self, type_idx: u32) -> Option<&SubType> { |
197 | 0 | T::sub_type_at(self, type_idx) |
198 | 0 | } |
199 | | |
200 | 0 | fn type_id_of_function(&self, func_idx: u32) -> Option<CoreTypeId> { |
201 | 0 | T::type_id_of_function(self, func_idx) |
202 | 0 | } |
203 | | |
204 | 0 | fn type_of_function(&self, func_idx: u32) -> Option<&FuncType> { |
205 | 0 | T::type_of_function(self, func_idx) |
206 | 0 | } |
207 | | |
208 | 0 | fn check_heap_type(&self, t: &mut HeapType, offset: usize) -> Result<(), BinaryReaderError> { |
209 | 0 | T::check_heap_type(self, t, offset) |
210 | 0 | } |
211 | | |
212 | 0 | fn top_type(&self, heap_type: &HeapType) -> HeapType { |
213 | 0 | T::top_type(self, heap_type) |
214 | 0 | } |
215 | | |
216 | 0 | fn element_type_at(&self, at: u32) -> Option<RefType> { |
217 | 0 | T::element_type_at(self, at) |
218 | 0 | } |
219 | | |
220 | 0 | fn is_subtype(&self, a: ValType, b: ValType) -> bool { |
221 | 0 | T::is_subtype(self, a, b) |
222 | 0 | } |
223 | | |
224 | 0 | fn element_count(&self) -> u32 { |
225 | 0 | T::element_count(self) |
226 | 0 | } |
227 | | |
228 | 0 | fn data_count(&self) -> Option<u32> { |
229 | 0 | T::data_count(self) |
230 | 0 | } |
231 | | |
232 | 0 | fn is_function_referenced(&self, idx: u32) -> bool { |
233 | 0 | T::is_function_referenced(self, idx) |
234 | 0 | } |
235 | | } |