wasm/validation/modules/
indices.rs1use crate::{
2 core::{
3 decoding::reader::WasmDecoder,
4 structure::modules::indices::{
5 DataIdx, ElemIdx, FuncIdx, GlobalIdx, Idx, IdxVec, LocalIdx, MemIdx, TableIdx, TypeIdx,
6 },
7 },
8 FuncType, ValType, ValidationError,
9};
10
11impl TypeIdx {
12 pub fn validate(
17 index: u32,
18 c_types: &IdxVec<TypeIdx, FuncType>,
19 ) -> Result<Self, ValidationError> {
20 c_types
21 .validate_index(index)
22 .ok_or(ValidationError::InvalidTypeIdx(index))
23 }
24
25 pub fn decode_and_validate(
28 wasm: &mut WasmDecoder,
29 c_types: &IdxVec<TypeIdx, FuncType>,
30 ) -> Result<Self, ValidationError> {
31 let index = wasm.decode_var_u32()?;
32 Self::validate(index, c_types)
33 }
34}
35
36impl FuncIdx {
37 pub fn validate<T>(index: u32, c_funcs: &IdxVec<FuncIdx, T>) -> Result<Self, ValidationError> {
42 c_funcs
43 .validate_index(index)
44 .ok_or(ValidationError::InvalidFuncIdx(index))
45 }
46
47 pub fn decode_and_validate<T>(
50 wasm: &mut WasmDecoder,
51 c_funcs: &IdxVec<FuncIdx, T>,
52 ) -> Result<Self, ValidationError> {
53 let index = wasm.decode_var_u32()?;
54 Self::validate(index, c_funcs)
55 }
56}
57
58impl TableIdx {
59 pub fn validate<T>(
64 index: u32,
65 c_tables: &IdxVec<TableIdx, T>,
66 ) -> Result<Self, ValidationError> {
67 c_tables
68 .validate_index(index)
69 .ok_or(ValidationError::InvalidTableIdx(index))
70 }
71
72 pub fn decode_and_validate<T>(
75 wasm: &mut WasmDecoder,
76 c_tables: &IdxVec<TableIdx, T>,
77 ) -> Result<Self, ValidationError> {
78 let index = wasm.decode_var_u32()?;
79 Self::validate(index, c_tables)
80 }
81}
82
83impl MemIdx {
84 pub fn validate<T>(index: u32, c_mems: &IdxVec<MemIdx, T>) -> Result<Self, ValidationError> {
89 c_mems
90 .validate_index(index)
91 .ok_or(ValidationError::InvalidMemIdx(index))
92 }
93
94 pub fn decode_and_validate<T>(
97 wasm: &mut WasmDecoder,
98 c_mems: &IdxVec<MemIdx, T>,
99 ) -> Result<Self, ValidationError> {
100 let index = wasm.decode_var_u32()?;
101 Self::validate(index, c_mems)
102 }
103}
104
105impl GlobalIdx {
106 pub fn validate<T>(
111 index: u32,
112 c_globals: &IdxVec<GlobalIdx, T>,
113 ) -> Result<Self, ValidationError> {
114 c_globals
115 .validate_index(index)
116 .ok_or(ValidationError::InvalidGlobalIdx(index))
117 }
118
119 pub fn decode_and_validate<T>(
122 wasm: &mut WasmDecoder,
123 c_globals: &IdxVec<GlobalIdx, T>,
124 ) -> Result<Self, ValidationError> {
125 let index = wasm.decode_var_u32()?;
126 Self::validate(index, c_globals)
127 }
128}
129
130impl ElemIdx {
131 pub fn validate<T>(index: u32, c_elems: &IdxVec<ElemIdx, T>) -> Result<Self, ValidationError> {
136 c_elems
137 .validate_index(index)
138 .ok_or(ValidationError::InvalidElemIdx(index))
139 }
140
141 pub fn decode_and_validate<T>(
144 wasm: &mut WasmDecoder,
145 c_elems: &IdxVec<ElemIdx, T>,
146 ) -> Result<Self, ValidationError> {
147 let index = wasm.decode_var_u32()?;
148 Self::validate(index, c_elems)
149 }
150}
151
152impl DataIdx {
153 pub fn validate(index: u32, data_count: u32) -> Result<Self, ValidationError> {
158 (index < data_count)
159 .then_some(<Self as Idx>::new(index))
160 .ok_or(ValidationError::InvalidDataIdx(index))
161 }
162
163 pub fn decode_and_validate(
166 wasm: &mut WasmDecoder,
167 data_count: u32,
168 ) -> Result<Self, ValidationError> {
169 let index = wasm.decode_var_u32()?;
170 Self::validate(index, data_count)
171 }
172}
173
174impl LocalIdx {
175 pub fn decode_and_validate(
178 wasm: &mut WasmDecoder,
179 locals_of_current_function: &[ValType],
180 ) -> Result<Self, ValidationError> {
181 let index = wasm.decode_var_u32()?;
182 let index_as_usize = usize::try_from(index).expect("architecture to be at least 32 bits");
183
184 match locals_of_current_function.get(index_as_usize) {
185 Some(_local) => Ok(Self(index)),
186 None => Err(ValidationError::InvalidLocalIdx(index)),
187 }
188 }
189}