1 use anyhow::Result;
2 use std::sync::atomic::{AtomicUsize, Ordering::SeqCst};
3 use wasmtime::*;
4 
5 #[test]
6 fn host_always_has_some_stack() -> Result<()> {
7     static HITS: AtomicUsize = AtomicUsize::new(0);
8     // assume hosts always have at least 128k of stack
9     const HOST_STACK: usize = 128 * 1024;
10 
11     let mut store = Store::<()>::default();
12 
13     // Create a module that's infinitely recursive, but calls the host on each
14     // level of wasm stack to always test how much host stack we have left.
15     let module = Module::new(
16         store.engine(),
17         r#"
18             (module
19                 (import "" "" (func $host))
20                 (func $recursive (export "foo")
21                     call $host
22                     call $recursive)
23             )
24         "#,
25     )?;
26     let func = Func::wrap(&mut store, test_host_stack);
27     let instance = Instance::new(&mut store, &module, &[func.into()])?;
28     let foo = instance.get_typed_func::<(), ()>(&mut store, "foo")?;
29 
30     // Make sure that our function traps and the trap says that the call stack
31     // has been exhausted.
32     let trap = foo.call(&mut store, ()).unwrap_err().downcast::<Trap>()?;
33     assert_eq!(trap, Trap::StackOverflow);
34 
35     // Additionally, however, and this is the crucial test, make sure that the
36     // host function actually completed. If HITS is 1 then we entered but didn't
37     // exit meaning we segfaulted while executing the host, yet still tried to
38     // recover from it with longjmp.
39     assert_eq!(HITS.load(SeqCst), 0);
40 
41     return Ok(());
42 
43     fn test_host_stack() {
44         HITS.fetch_add(1, SeqCst);
45         assert!(consume_some_stack(0, HOST_STACK) > 0);
46         HITS.fetch_sub(1, SeqCst);
47     }
48 
49     #[inline(never)]
50     fn consume_some_stack(ptr: usize, stack: usize) -> usize {
51         if stack == 0 {
52             return ptr;
53         }
54         let mut space = [0u8; 1024];
55         consume_some_stack(space.as_mut_ptr() as usize, stack.saturating_sub(1024))
56     }
57 }
58 
59 #[test]
60 fn big_stack_works_ok() -> Result<()> {
61     const N: usize = 10000;
62 
63     // Build a module with a function that uses a very large amount of stack space,
64     // modeled here by calling an i64-returning-function many times followed by
65     // adding them all into one i64.
66     //
67     // This should exercise the ability to consume multi-page stacks and
68     // only touch a few internals of it at a time.
69     let mut s = String::new();
70     s.push_str("(module\n");
71     s.push_str("(func (export \"\") (result i64)\n");
72     s.push_str("i64.const 0\n");
73     for _ in 0..N {
74         s.push_str("call $get\n");
75     }
76     for _ in 0..N {
77         s.push_str("i64.add\n");
78     }
79     s.push_str(")\n");
80     s.push_str("(func $get (result i64) i64.const 0)\n");
81     s.push_str(")\n");
82 
83     let mut store = Store::<()>::default();
84     let module = Module::new(store.engine(), &s)?;
85     let instance = Instance::new(&mut store, &module, &[])?;
86     let func = instance.get_typed_func::<(), i64>(&mut store, "")?;
87     assert_eq!(func.call(&mut store, ())?, 0);
88     Ok(())
89 }
90