1 //! This module generates test cases for the Wasmtime component model function APIs,
2 //! e.g. `wasmtime::component::func::Func` and `TypedFunc`.
3 //!
4 //! Each case includes a list of arbitrary interface types to use as parameters, plus another one to use as a
5 //! result, and a component which exports a function and imports a function.  The exported function forwards its
6 //! parameters to the imported one and forwards the result back to the caller.  This serves to exercise Wasmtime's
7 //! lifting and lowering code and verify the values remain intact during both processes.
8 
9 use crate::block_on;
10 use crate::generators::{self, CompilerStrategy, InstanceAllocationStrategy};
11 use crate::oracles::log_wasm;
12 use arbitrary::{Arbitrary, Unstructured};
13 use std::any::Any;
14 use std::fmt::Debug;
15 use std::ops::ControlFlow;
16 use wasmtime::component::{
17     self, Accessor, Component, ComponentNamedList, Lift, Linker, Lower, Val,
18 };
19 use wasmtime::{AsContextMut, Enabled, Engine, Result, Store, StoreContextMut};
20 use wasmtime_test_util::component_fuzz::{
21     Declarations, EXPORT_FUNCTION, IMPORT_FUNCTION, MAX_TYPE_DEPTH, TestCase, Type,
22 };
23 
24 /// Minimum length of an arbitrary list value generated for a test case
25 const MIN_LIST_LENGTH: u32 = 0;
26 
27 /// Maximum length of an arbitrary list value generated for a test case
28 const MAX_LIST_LENGTH: u32 = 10;
29 
30 /// Generate an arbitrary instance of the specified type.
31 fn arbitrary_val(ty: &component::Type, input: &mut Unstructured) -> arbitrary::Result<Val> {
32     use component::Type;
33 
34     Ok(match ty {
35         Type::Bool => Val::Bool(input.arbitrary()?),
36         Type::S8 => Val::S8(input.arbitrary()?),
37         Type::U8 => Val::U8(input.arbitrary()?),
38         Type::S16 => Val::S16(input.arbitrary()?),
39         Type::U16 => Val::U16(input.arbitrary()?),
40         Type::S32 => Val::S32(input.arbitrary()?),
41         Type::U32 => Val::U32(input.arbitrary()?),
42         Type::S64 => Val::S64(input.arbitrary()?),
43         Type::U64 => Val::U64(input.arbitrary()?),
44         Type::Float32 => Val::Float32(input.arbitrary()?),
45         Type::Float64 => Val::Float64(input.arbitrary()?),
46         Type::Char => Val::Char(input.arbitrary()?),
47         Type::String => Val::String(input.arbitrary()?),
48         Type::List(list) => {
49             let mut values = Vec::new();
50             input.arbitrary_loop(Some(MIN_LIST_LENGTH), Some(MAX_LIST_LENGTH), |input| {
51                 values.push(arbitrary_val(&list.ty(), input)?);
52 
53                 Ok(ControlFlow::Continue(()))
54             })?;
55 
56             Val::List(values)
57         }
58         Type::Record(record) => Val::Record(
59             record
60                 .fields()
61                 .map(|field| Ok((field.name.to_string(), arbitrary_val(&field.ty, input)?)))
62                 .collect::<arbitrary::Result<_>>()?,
63         ),
64         Type::Tuple(tuple) => Val::Tuple(
65             tuple
66                 .types()
67                 .map(|ty| arbitrary_val(&ty, input))
68                 .collect::<arbitrary::Result<_>>()?,
69         ),
70         Type::Variant(variant) => {
71             let cases = variant.cases().collect::<Vec<_>>();
72             let case = input.choose(&cases)?;
73             let payload = match &case.ty {
74                 Some(ty) => Some(Box::new(arbitrary_val(ty, input)?)),
75                 None => None,
76             };
77             Val::Variant(case.name.to_string(), payload)
78         }
79         Type::Enum(en) => {
80             let names = en.names().collect::<Vec<_>>();
81             let name = input.choose(&names)?;
82             Val::Enum(name.to_string())
83         }
84         Type::Option(option) => {
85             let discriminant = input.int_in_range(0..=1)?;
86             Val::Option(match discriminant {
87                 0 => None,
88                 1 => Some(Box::new(arbitrary_val(&option.ty(), input)?)),
89                 _ => unreachable!(),
90             })
91         }
92         Type::Result(result) => {
93             let discriminant = input.int_in_range(0..=1)?;
94             Val::Result(match discriminant {
95                 0 => Ok(match result.ok() {
96                     Some(ty) => Some(Box::new(arbitrary_val(&ty, input)?)),
97                     None => None,
98                 }),
99                 1 => Err(match result.err() {
100                     Some(ty) => Some(Box::new(arbitrary_val(&ty, input)?)),
101                     None => None,
102                 }),
103                 _ => unreachable!(),
104             })
105         }
106         Type::Flags(flags) => Val::Flags(
107             flags
108                 .names()
109                 .filter_map(|name| {
110                     input
111                         .arbitrary()
112                         .map(|p| if p { Some(name.to_string()) } else { None })
113                         .transpose()
114                 })
115                 .collect::<arbitrary::Result<_>>()?,
116         ),
117 
118         // Resources, futures, streams, and error contexts aren't fuzzed at this time.
119         Type::Own(_) | Type::Borrow(_) | Type::Future(_) | Type::Stream(_) | Type::ErrorContext => {
120             unreachable!()
121         }
122     })
123 }
124 
125 fn store<T>(input: &mut Unstructured<'_>, val: T) -> arbitrary::Result<Store<T>> {
126     crate::init_fuzzing();
127 
128     let mut config = input.arbitrary::<generators::Config>()?;
129     config.enable_async(input)?;
130     config.module_config.config.multi_value_enabled = true;
131     config.module_config.config.reference_types_enabled = true;
132     config.module_config.config.max_memories = 2;
133     config.module_config.component_model_async = true;
134     config.module_config.component_model_async_stackful = true;
135     if config.wasmtime.compiler_strategy == CompilerStrategy::Winch {
136         config.wasmtime.compiler_strategy = CompilerStrategy::CraneliftNative;
137     }
138 
139     fn set_min<T>(a: &mut T, min: T)
140     where
141         T: Ord + Copy,
142     {
143         if *a < min {
144             *a = min;
145         }
146     }
147 
148     fn set_opt_min<T>(a: &mut Option<T>, min: T)
149     where
150         T: Ord + Copy,
151     {
152         if let Some(a) = a {
153             set_min(a, min);
154         }
155     }
156 
157     if let InstanceAllocationStrategy::Pooling(p) = &mut config.wasmtime.strategy {
158         set_min(&mut p.total_component_instances, 5);
159         set_min(&mut p.total_core_instances, 5);
160         set_min(&mut p.total_memories, 2);
161         set_min(&mut p.total_stacks, 4);
162         set_min(&mut p.max_memories_per_component, 2);
163         set_min(&mut p.max_memories_per_module, 2);
164         set_min(&mut p.component_instance_size, 64 << 10);
165         p.memory_protection_keys = Enabled::No;
166         p.max_memory_size = 10 << 20; // 10 MiB
167     }
168     set_opt_min(
169         &mut config.wasmtime.memory_config.memory_reservation,
170         10 << 20,
171     );
172 
173     let engine = Engine::new(
174         config
175             .to_wasmtime()
176             .debug_adapter_modules(input.arbitrary()?),
177     )
178     .unwrap();
179     let mut store = Store::new(&engine, val);
180     config.configure_store_epoch_and_fuel(&mut store);
181     Ok(store)
182 }
183 
184 /// Generate zero or more sets of arbitrary argument and result values and execute the test using those
185 /// values, asserting that they flow from host-to-guest and guest-to-host unchanged.
186 pub fn static_api_test<'a, P, R>(
187     input: &mut Unstructured<'a>,
188     declarations: &Declarations,
189 ) -> arbitrary::Result<()>
190 where
191     P: ComponentNamedList
192         + Lift
193         + Lower
194         + Clone
195         + PartialEq
196         + Debug
197         + Arbitrary<'a>
198         + Send
199         + Sync
200         + 'static,
201     R: ComponentNamedList
202         + Lift
203         + Lower
204         + Clone
205         + PartialEq
206         + Debug
207         + Arbitrary<'a>
208         + Send
209         + Sync
210         + 'static,
211 {
212     crate::init_fuzzing();
213 
214     let mut store = store::<Box<dyn Any + Send>>(input, Box::new(()))?;
215     let engine = store.engine();
216     let wat = declarations.make_component();
217     let wat = wat.as_bytes();
218     crate::oracles::log_wasm(wat);
219     let component = Component::new(&engine, wat).unwrap();
220     let mut linker = Linker::new(&engine);
221 
222     fn host_function<P, R>(
223         cx: StoreContextMut<'_, Box<dyn Any + Send>>,
224         params: P,
225     ) -> anyhow::Result<R>
226     where
227         P: Debug + PartialEq + 'static,
228         R: Debug + Clone + 'static,
229     {
230         log::trace!("received parameters {params:?}");
231         let data: &(P, R) = cx.data().downcast_ref().unwrap();
232         let (expected_params, result) = data;
233         assert_eq!(params, *expected_params);
234         log::trace!("returning result {result:?}");
235         Ok(result.clone())
236     }
237 
238     if declarations.options.host_async {
239         linker
240             .root()
241             .func_wrap_concurrent(IMPORT_FUNCTION, |a, params| {
242                 Box::pin(async move {
243                     a.with(|mut cx| host_function::<P, R>(cx.as_context_mut(), params))
244                 })
245             })
246             .unwrap();
247     } else {
248         linker
249             .root()
250             .func_wrap(IMPORT_FUNCTION, |cx, params| {
251                 host_function::<P, R>(cx, params)
252             })
253             .unwrap();
254     }
255 
256     block_on(async {
257         let instance = linker
258             .instantiate_async(&mut store, &component)
259             .await
260             .unwrap();
261         let func = instance
262             .get_typed_func::<P, R>(&mut store, EXPORT_FUNCTION)
263             .unwrap();
264 
265         while input.arbitrary()? {
266             let params = input.arbitrary::<P>()?;
267             let result = input.arbitrary::<R>()?;
268             *store.data_mut() = Box::new((params.clone(), result.clone()));
269             log::trace!("passing in parameters {params:?}");
270             let actual = if declarations.options.guest_caller_async {
271                 store
272                     .run_concurrent(async |a| func.call_concurrent(a, params).await.unwrap().0)
273                     .await
274                     .unwrap()
275             } else {
276                 let result = func.call_async(&mut store, params).await.unwrap();
277                 func.post_return_async(&mut store).await.unwrap();
278                 result
279             };
280             log::trace!("got result {actual:?}");
281             assert_eq!(actual, result);
282         }
283 
284         Ok(())
285     })
286 }
287 
288 /// Generate and execute a `crate::generators::component_types::TestCase` using the specified `input` to create
289 /// arbitrary types and values.
290 pub fn dynamic_component_api_target(input: &mut arbitrary::Unstructured) -> arbitrary::Result<()> {
291     crate::init_fuzzing();
292 
293     let mut types = Vec::new();
294     let mut type_fuel = 500;
295 
296     for _ in 0..5 {
297         types.push(Type::generate(input, MAX_TYPE_DEPTH, &mut type_fuel)?);
298     }
299 
300     let case = TestCase::generate(&types, input)?;
301 
302     let mut store = store(input, (Vec::new(), None))?;
303     let engine = store.engine();
304     let wat = case.declarations().make_component();
305     let wat = wat.as_bytes();
306     log_wasm(wat);
307     let component = Component::new(&engine, wat).unwrap();
308     let mut linker = Linker::new(&engine);
309 
310     fn host_function(
311         mut cx: StoreContextMut<'_, (Vec<Val>, Option<Vec<Val>>)>,
312         params: &[Val],
313         results: &mut [Val],
314     ) -> Result<()> {
315         log::trace!("received params {params:?}");
316         let (expected_args, expected_results) = cx.data_mut();
317         assert_eq!(params.len(), expected_args.len());
318         for (expected, actual) in expected_args.iter().zip(params) {
319             assert_eq!(expected, actual);
320         }
321         results.clone_from_slice(&expected_results.take().unwrap());
322         log::trace!("returning results {results:?}");
323         Ok(())
324     }
325 
326     if case.options.host_async {
327         linker
328             .root()
329             .func_new_concurrent(IMPORT_FUNCTION, {
330                 move |cx: &Accessor<_, _>, _, params: &[Val], results: &mut [Val]| {
331                     Box::pin(async move {
332                         cx.with(|mut store| host_function(store.as_context_mut(), params, results))
333                     })
334                 }
335             })
336             .unwrap();
337     } else {
338         linker
339             .root()
340             .func_new(IMPORT_FUNCTION, {
341                 move |cx, _, params, results| host_function(cx, params, results)
342             })
343             .unwrap();
344     }
345 
346     block_on(async {
347         let instance = linker
348             .instantiate_async(&mut store, &component)
349             .await
350             .unwrap();
351         let func = instance.get_func(&mut store, EXPORT_FUNCTION).unwrap();
352         let ty = func.ty(&store);
353 
354         while input.arbitrary()? {
355             let params = ty
356                 .params()
357                 .map(|(_, ty)| arbitrary_val(&ty, input))
358                 .collect::<arbitrary::Result<Vec<_>>>()?;
359             let results = ty
360                 .results()
361                 .map(|ty| arbitrary_val(&ty, input))
362                 .collect::<arbitrary::Result<Vec<_>>>()?;
363 
364             *store.data_mut() = (params.clone(), Some(results.clone()));
365 
366             log::trace!("passing params {params:?}");
367             let mut actual = vec![Val::Bool(false); results.len()];
368             if case.options.guest_caller_async {
369                 store
370                     .run_concurrent(async |a| {
371                         func.call_concurrent(a, &params, &mut actual).await.unwrap();
372                     })
373                     .await
374                     .unwrap();
375             } else {
376                 func.call_async(&mut store, &params, &mut actual)
377                     .await
378                     .unwrap();
379                 func.post_return_async(&mut store).await.unwrap();
380             }
381             log::trace!("received results {actual:?}");
382             assert_eq!(actual, results);
383         }
384         Ok(())
385     })
386 }
387 
388 #[cfg(test)]
389 mod tests {
390     use super::*;
391     use crate::test::test_n_times;
392     use wasmtime_test_util::component_fuzz::{TestCase, Type};
393 
394     #[test]
395     fn dynamic_component_api_smoke_test() {
396         test_n_times(50, |(), u| super::dynamic_component_api_target(u));
397     }
398 
399     #[test]
400     fn static_api_smoke_test() {
401         test_n_times(10, |(), u| {
402             let mut case = TestCase::generate(&[], u)?;
403             case.params = vec![&Type::S32, &Type::Bool, &Type::String];
404             case.result = Some(&Type::String);
405 
406             let declarations = case.declarations();
407             static_api_test::<(i32, bool, String), (String,)>(u, &declarations)
408         });
409     }
410 }
411