1 //! Evaluating const expressions.
2 
3 use crate::prelude::*;
4 use crate::runtime::vm::{Instance, VMGcRef, ValRaw, I31};
5 use crate::store::{AutoAssertNoGc, StoreOpaque};
6 #[cfg(feature = "gc")]
7 use crate::{ArrayRef, ArrayRefPre, ArrayType, StructRef, StructRefPre, StructType, Val};
8 use smallvec::SmallVec;
9 use wasmtime_environ::{ConstExpr, ConstOp, FuncIndex, GlobalIndex};
10 #[cfg(feature = "gc")]
11 use wasmtime_environ::{VMSharedTypeIndex, WasmCompositeInnerType, WasmCompositeType, WasmSubType};
12 
13 /// An interpreter for const expressions.
14 ///
15 /// This can be reused across many const expression evaluations to reuse
16 /// allocated resources, if any.
17 #[derive(Default)]
18 pub struct ConstExprEvaluator {
19     stack: SmallVec<[ValRaw; 2]>,
20 }
21 
22 /// The context within which a particular const expression is evaluated.
23 pub struct ConstEvalContext<'a> {
24     pub(crate) instance: &'a mut Instance,
25 }
26 
27 impl<'a> ConstEvalContext<'a> {
28     /// Create a new context.
29     pub fn new(instance: &'a mut Instance) -> Self {
30         Self { instance }
31     }
32 
33     fn global_get(&mut self, store: &mut AutoAssertNoGc<'_>, index: GlobalIndex) -> Result<ValRaw> {
34         unsafe {
35             let global = self.instance.defined_or_imported_global_ptr(index).as_ref();
36             global.to_val_raw(store, self.instance.env_module().globals[index].wasm_ty)
37         }
38     }
39 
40     fn ref_func(&mut self, index: FuncIndex) -> Result<ValRaw> {
41         Ok(ValRaw::funcref(
42             self.instance.get_func_ref(index).unwrap().as_ptr().cast(),
43         ))
44     }
45 
46     #[cfg(feature = "gc")]
47     fn struct_fields_len(
48         &self,
49         store: &mut AutoAssertNoGc<'_>,
50         shared_ty: VMSharedTypeIndex,
51     ) -> usize {
52         let struct_ty = StructType::from_shared_type_index(store.engine(), shared_ty);
53         let fields = struct_ty.fields();
54         fields.len()
55     }
56 
57     /// Safety: field values must be of the correct types.
58     #[cfg(feature = "gc")]
59     unsafe fn struct_new(
60         &mut self,
61         store: &mut AutoAssertNoGc<'_>,
62         shared_ty: VMSharedTypeIndex,
63         fields: &[ValRaw],
64     ) -> Result<ValRaw> {
65         let struct_ty = StructType::from_shared_type_index(store.engine(), shared_ty);
66         let fields = fields
67             .iter()
68             .zip(struct_ty.fields())
69             .map(|(raw, ty)| {
70                 let ty = ty.element_type().unpack();
71                 Val::_from_raw(store, *raw, ty)
72             })
73             .collect::<Vec<_>>();
74 
75         let allocator = StructRefPre::_new(store, struct_ty);
76         let struct_ref = StructRef::_new(store, &allocator, &fields)?;
77         let raw = struct_ref.to_anyref()._to_raw(store)?;
78         Ok(ValRaw::anyref(raw))
79     }
80 
81     #[cfg(feature = "gc")]
82     fn struct_new_default(
83         &mut self,
84         store: &mut AutoAssertNoGc<'_>,
85         shared_ty: VMSharedTypeIndex,
86     ) -> Result<ValRaw> {
87         let module = self
88             .instance
89             .runtime_module()
90             .expect("should never be allocating a struct type defined in a dummy module");
91 
92         let borrowed = module
93             .engine()
94             .signatures()
95             .borrow(shared_ty)
96             .expect("should have a registered type for struct");
97         let WasmSubType {
98             composite_type:
99                 WasmCompositeType {
100                     shared: false,
101                     inner: WasmCompositeInnerType::Struct(struct_ty),
102                 },
103             ..
104         } = &*borrowed
105         else {
106             unreachable!("registered type should be a struct");
107         };
108 
109         let fields = struct_ty
110             .fields
111             .iter()
112             .map(|ty| match &ty.element_type {
113                 wasmtime_environ::WasmStorageType::I8 | wasmtime_environ::WasmStorageType::I16 => {
114                     ValRaw::i32(0)
115                 }
116                 wasmtime_environ::WasmStorageType::Val(v) => match v {
117                     wasmtime_environ::WasmValType::I32 => ValRaw::i32(0),
118                     wasmtime_environ::WasmValType::I64 => ValRaw::i64(0),
119                     wasmtime_environ::WasmValType::F32 => ValRaw::f32(0.0f32.to_bits()),
120                     wasmtime_environ::WasmValType::F64 => ValRaw::f64(0.0f64.to_bits()),
121                     wasmtime_environ::WasmValType::V128 => ValRaw::v128(0),
122                     wasmtime_environ::WasmValType::Ref(r) => {
123                         assert!(r.nullable);
124                         ValRaw::null()
125                     }
126                 },
127             })
128             .collect::<SmallVec<[_; 8]>>();
129 
130         unsafe { self.struct_new(store, shared_ty, &fields) }
131     }
132 }
133 
134 impl ConstExprEvaluator {
135     /// Evaluate the given const expression in the given context.
136     ///
137     /// # Unsafety
138     ///
139     /// The given const expression must be valid within the given context,
140     /// e.g. the const expression must be well-typed and the context must return
141     /// global values of the expected types. This evaluator operates directly on
142     /// untyped `ValRaw`s and does not and cannot check that its operands are of
143     /// the correct type.
144     pub unsafe fn eval(
145         &mut self,
146         store: &mut StoreOpaque,
147         context: &mut ConstEvalContext<'_>,
148         expr: &ConstExpr,
149     ) -> Result<ValRaw> {
150         log::trace!("evaluating const expr: {:?}", expr);
151 
152         self.stack.clear();
153 
154         // Ensure that we don't permanently root any GC references we allocate
155         // during const evaluation, keeping them alive for the duration of the
156         // store's lifetime.
157         #[cfg(feature = "gc")]
158         let mut store = crate::OpaqueRootScope::new(store);
159         #[cfg(not(feature = "gc"))]
160         let mut store = store;
161 
162         // We cannot allow GC during const evaluation because the stack of
163         // `ValRaw`s are not rooted. If we had a GC reference on our stack, and
164         // then performed a collection, that on-stack reference's object could
165         // be reclaimed or relocated by the collector, and then when we use the
166         // reference again we would basically get a use-after-free bug.
167         let mut store = AutoAssertNoGc::new(&mut store);
168 
169         for op in expr.ops() {
170             match op {
171                 ConstOp::I32Const(i) => self.stack.push(ValRaw::i32(*i)),
172                 ConstOp::I64Const(i) => self.stack.push(ValRaw::i64(*i)),
173                 ConstOp::F32Const(f) => self.stack.push(ValRaw::f32(*f)),
174                 ConstOp::F64Const(f) => self.stack.push(ValRaw::f64(*f)),
175                 ConstOp::V128Const(v) => self.stack.push(ValRaw::v128(*v)),
176                 ConstOp::GlobalGet(g) => self.stack.push(context.global_get(&mut store, *g)?),
177                 ConstOp::RefNull => self.stack.push(ValRaw::null()),
178                 ConstOp::RefFunc(f) => self.stack.push(context.ref_func(*f)?),
179                 ConstOp::RefI31 => {
180                     let i = self.pop()?.get_i32();
181                     let i31 = I31::wrapping_i32(i);
182                     let raw = VMGcRef::from_i31(i31).as_raw_u32();
183                     self.stack.push(ValRaw::anyref(raw));
184                 }
185                 ConstOp::I32Add => {
186                     let b = self.pop()?.get_i32();
187                     let a = self.pop()?.get_i32();
188                     self.stack.push(ValRaw::i32(a.wrapping_add(b)));
189                 }
190                 ConstOp::I32Sub => {
191                     let b = self.pop()?.get_i32();
192                     let a = self.pop()?.get_i32();
193                     self.stack.push(ValRaw::i32(a.wrapping_sub(b)));
194                 }
195                 ConstOp::I32Mul => {
196                     let b = self.pop()?.get_i32();
197                     let a = self.pop()?.get_i32();
198                     self.stack.push(ValRaw::i32(a.wrapping_mul(b)));
199                 }
200                 ConstOp::I64Add => {
201                     let b = self.pop()?.get_i64();
202                     let a = self.pop()?.get_i64();
203                     self.stack.push(ValRaw::i64(a.wrapping_add(b)));
204                 }
205                 ConstOp::I64Sub => {
206                     let b = self.pop()?.get_i64();
207                     let a = self.pop()?.get_i64();
208                     self.stack.push(ValRaw::i64(a.wrapping_sub(b)));
209                 }
210                 ConstOp::I64Mul => {
211                     let b = self.pop()?.get_i64();
212                     let a = self.pop()?.get_i64();
213                     self.stack.push(ValRaw::i64(a.wrapping_mul(b)));
214                 }
215 
216                 #[cfg(not(feature = "gc"))]
217                 ConstOp::StructNew { .. }
218                 | ConstOp::StructNewDefault { .. }
219                 | ConstOp::ArrayNew { .. }
220                 | ConstOp::ArrayNewDefault { .. }
221                 | ConstOp::ArrayNewFixed { .. } => {
222                     bail!(
223                         "const expr evaluation error: struct operations are not \
224                          supported without the `gc` feature"
225                     )
226                 }
227 
228                 #[cfg(feature = "gc")]
229                 ConstOp::StructNew { struct_type_index } => {
230                     let interned_type_index = context.instance.env_module().types
231                         [*struct_type_index]
232                         .unwrap_engine_type_index();
233                     let len = context.struct_fields_len(&mut store, interned_type_index);
234 
235                     if self.stack.len() < len {
236                         bail!(
237                             "const expr evaluation error: expected at least {len} values on the stack, found {}",
238                             self.stack.len()
239                         )
240                     }
241 
242                     let start = self.stack.len() - len;
243                     let s = context.struct_new(
244                         &mut store,
245                         interned_type_index,
246                         &self.stack[start..],
247                     )?;
248                     self.stack.truncate(start);
249                     self.stack.push(s);
250                 }
251 
252                 #[cfg(feature = "gc")]
253                 ConstOp::StructNewDefault { struct_type_index } => {
254                     let ty = context.instance.env_module().types[*struct_type_index]
255                         .unwrap_engine_type_index();
256                     self.stack.push(context.struct_new_default(&mut store, ty)?);
257                 }
258 
259                 #[cfg(feature = "gc")]
260                 ConstOp::ArrayNew { array_type_index } => {
261                     let ty = context.instance.env_module().types[*array_type_index]
262                         .unwrap_engine_type_index();
263                     let ty = ArrayType::from_shared_type_index(store.engine(), ty);
264 
265                     #[allow(clippy::cast_sign_loss)]
266                     let len = self.pop()?.get_i32() as u32;
267 
268                     let elem = Val::_from_raw(&mut store, self.pop()?, ty.element_type().unpack());
269 
270                     let pre = ArrayRefPre::_new(&mut store, ty);
271                     let array = ArrayRef::_new(&mut store, &pre, &elem, len)?;
272 
273                     self.stack
274                         .push(ValRaw::anyref(array.to_anyref()._to_raw(&mut store)?));
275                 }
276 
277                 #[cfg(feature = "gc")]
278                 ConstOp::ArrayNewDefault { array_type_index } => {
279                     let ty = context.instance.env_module().types[*array_type_index]
280                         .unwrap_engine_type_index();
281                     let ty = ArrayType::from_shared_type_index(store.engine(), ty);
282 
283                     #[allow(clippy::cast_sign_loss)]
284                     let len = self.pop()?.get_i32() as u32;
285 
286                     let elem = Val::default_for_ty(ty.element_type().unpack())
287                         .expect("type should have a default value");
288 
289                     let pre = ArrayRefPre::_new(&mut store, ty);
290                     let array = ArrayRef::_new(&mut store, &pre, &elem, len)?;
291 
292                     self.stack
293                         .push(ValRaw::anyref(array.to_anyref()._to_raw(&mut store)?));
294                 }
295 
296                 #[cfg(feature = "gc")]
297                 ConstOp::ArrayNewFixed {
298                     array_type_index,
299                     array_size,
300                 } => {
301                     let ty = context.instance.env_module().types[*array_type_index]
302                         .unwrap_engine_type_index();
303                     let ty = ArrayType::from_shared_type_index(store.engine(), ty);
304 
305                     let array_size = usize::try_from(*array_size).unwrap();
306                     if self.stack.len() < array_size {
307                         bail!(
308                             "const expr evaluation error: expected at least {array_size} values on the stack, found {}",
309                             self.stack.len()
310                         )
311                     }
312 
313                     let start = self.stack.len() - array_size;
314 
315                     let elem_ty = ty.element_type();
316                     let elem_ty = elem_ty.unpack();
317 
318                     let elems = self
319                         .stack
320                         .drain(start..)
321                         .map(|raw| Val::_from_raw(&mut store, raw, elem_ty))
322                         .collect::<SmallVec<[_; 8]>>();
323 
324                     let pre = ArrayRefPre::_new(&mut store, ty);
325                     let array = ArrayRef::_new_fixed(&mut store, &pre, &elems)?;
326 
327                     self.stack
328                         .push(ValRaw::anyref(array.to_anyref()._to_raw(&mut store)?));
329                 }
330             }
331         }
332 
333         if self.stack.len() == 1 {
334             log::trace!("const expr evaluated to {:?}", self.stack[0]);
335             Ok(self.stack[0])
336         } else {
337             bail!(
338                 "const expr evaluation error: expected 1 resulting value, found {}",
339                 self.stack.len()
340             )
341         }
342     }
343 
344     fn pop(&mut self) -> Result<ValRaw> {
345         self.stack.pop().ok_or_else(|| {
346             anyhow!(
347                 "const expr evaluation error: attempted to pop from an empty \
348                  evaluation stack"
349             )
350         })
351     }
352 }
353