1 #![cfg(arc_try_new)]
2 
3 use wasmtime::{Config, Engine, Func, FuncType, Module, Result, Store};
4 use wasmtime_fuzzing::oom::OomTest;
5 
6 #[test]
caller_get_export() -> Result<()>7 fn caller_get_export() -> Result<()> {
8     let module_bytes = {
9         let mut config = Config::new();
10         config.concurrency_support(false);
11         let engine = Engine::new(&config)?;
12         Module::new(
13             &engine,
14             r#"(module
15                 (import "" "f" (func))
16                 (memory (export "m") 1)
17                 (func (export "run") (call 0))
18             )"#,
19         )?
20         .serialize()?
21     };
22     let mut config = Config::new();
23     config.enable_compiler(false);
24     config.concurrency_support(false);
25     let engine = Engine::new(&config)?;
26     let module = unsafe { Module::deserialize(&engine, &module_bytes)? };
27 
28     OomTest::new().test(|| {
29         let mut store = Store::try_new(&engine, ())?;
30         let host_func = Func::try_new(
31             &mut store,
32             FuncType::try_new(&engine, [], [])?,
33             |mut caller, _params, _results| {
34                 let mem = caller.get_export("m");
35                 assert!(mem.is_some());
36                 Ok(())
37             },
38         )?;
39         let instance = wasmtime::Instance::new(&mut store, &module, &[host_func.into()])?;
40         let run = instance.get_typed_func::<(), ()>(&mut store, "run")?;
41         run.call(&mut store, ())?;
42         Ok(())
43     })
44 }
45 
46 #[test]
caller_data() -> Result<()>47 fn caller_data() -> Result<()> {
48     let module_bytes = {
49         let mut config = Config::new();
50         config.concurrency_support(false);
51         let engine = Engine::new(&config)?;
52         Module::new(
53             &engine,
54             r#"(module
55                 (import "" "f" (func))
56                 (func (export "run") (call 0))
57             )"#,
58         )?
59         .serialize()?
60     };
61     let mut config = Config::new();
62     config.enable_compiler(false);
63     config.concurrency_support(false);
64     let engine = Engine::new(&config)?;
65     let module = unsafe { Module::deserialize(&engine, &module_bytes)? };
66 
67     OomTest::new().test(|| {
68         let mut store = Store::try_new(&engine, 42u32)?;
69         let host_func = Func::try_new(
70             &mut store,
71             FuncType::try_new(&engine, [], [])?,
72             |caller, _params, _results| {
73                 assert_eq!(*caller.data(), 42u32);
74                 Ok(())
75             },
76         )?;
77         let instance = wasmtime::Instance::new(&mut store, &module, &[host_func.into()])?;
78         let run = instance.get_typed_func::<(), ()>(&mut store, "run")?;
79         run.call(&mut store, ())?;
80         Ok(())
81     })
82 }
83 
84 #[test]
caller_engine() -> Result<()>85 fn caller_engine() -> Result<()> {
86     let module_bytes = {
87         let mut config = Config::new();
88         config.concurrency_support(false);
89         let engine = Engine::new(&config)?;
90         Module::new(
91             &engine,
92             r#"(module
93                 (import "" "f" (func))
94                 (func (export "run") (call 0))
95             )"#,
96         )?
97         .serialize()?
98     };
99     let mut config = Config::new();
100     config.enable_compiler(false);
101     config.concurrency_support(false);
102     let engine = Engine::new(&config)?;
103     let module = unsafe { Module::deserialize(&engine, &module_bytes)? };
104 
105     OomTest::new().test(|| {
106         let mut store = Store::try_new(&engine, ())?;
107         let host_func = Func::try_new(
108             &mut store,
109             FuncType::try_new(&engine, [], [])?,
110             |caller, _params, _results| {
111                 let _engine = caller.engine();
112                 Ok(())
113             },
114         )?;
115         let instance = wasmtime::Instance::new(&mut store, &module, &[host_func.into()])?;
116         let run = instance.get_typed_func::<(), ()>(&mut store, "run")?;
117         run.call(&mut store, ())?;
118         Ok(())
119     })
120 }
121