1 #![cfg(arc_try_new)]
2 
3 use wasmtime::{Config, Engine, Func, Instance, Linker, Module, Result, Store};
4 use wasmtime_fuzzing::oom::OomTest;
5 
6 #[test]
call_exported_func() -> Result<()>7 fn call_exported_func() -> Result<()> {
8     let module_bytes = {
9         let mut config = Config::new();
10         config.concurrency_support(false);
11         let engine = Engine::new(&config)?;
12         let module = Module::new(
13             &engine,
14             r#"
15                 (module
16                     (func (export "add") (param i32 i32) (result i32)
17                         (i32.add (local.get 0) (local.get 1))
18                     )
19                 )
20             "#,
21         )?;
22         module.serialize()?
23     };
24 
25     let mut config = Config::new();
26     config.enable_compiler(false);
27     config.concurrency_support(false);
28     let engine = Engine::new(&config)?;
29 
30     let module = unsafe { Module::deserialize(&engine, &module_bytes)? };
31     let linker = Linker::<()>::new(&engine);
32     let instance_pre = linker.instantiate_pre(&module)?;
33 
34     OomTest::new().test(|| {
35         let mut store = Store::try_new(&engine, ())?;
36         let instance = instance_pre.instantiate(&mut store)?;
37         let add = instance.get_typed_func::<(i32, i32), i32>(&mut store, "add")?;
38         let result = add.call(&mut store, (1, 2))?;
39         assert_eq!(result, 3);
40         Ok(())
41     })
42 }
43 
44 #[test]
instance_new() -> Result<()>45 fn instance_new() -> Result<()> {
46     let module_bytes = {
47         let mut config = Config::new();
48         config.concurrency_support(false);
49         let engine = Engine::new(&config)?;
50         Module::new(
51             &engine,
52             "(module (import \"\" \"f\" (func)) (func (export \"g\") (call 0)))",
53         )?
54         .serialize()?
55     };
56     let mut config = Config::new();
57     config.enable_compiler(false);
58     config.concurrency_support(false);
59     let engine = Engine::new(&config)?;
60     let module = unsafe { Module::deserialize(&engine, &module_bytes)? };
61 
62     OomTest::new().test(|| {
63         let mut store = Store::try_new(&engine, ())?;
64         let func = Func::try_wrap(&mut store, || {})?;
65         let _instance = Instance::new(&mut store, &module, &[func.into()])?;
66         Ok(())
67     })
68 }
69 
70 #[test]
instance_get_export() -> Result<()>71 fn instance_get_export() -> Result<()> {
72     let module_bytes = {
73         let mut config = Config::new();
74         config.concurrency_support(false);
75         let engine = Engine::new(&config)?;
76         Module::new(&engine, "(module (func (export \"f\")))")?.serialize()?
77     };
78     let mut config = Config::new();
79     config.enable_compiler(false);
80     config.concurrency_support(false);
81     let engine = Engine::new(&config)?;
82     let module = unsafe { Module::deserialize(&engine, &module_bytes)? };
83     let linker = Linker::<()>::new(&engine);
84     let instance_pre = linker.instantiate_pre(&module)?;
85 
86     OomTest::new().test(|| {
87         let mut store = Store::try_new(&engine, ())?;
88         let instance = instance_pre.instantiate(&mut store)?;
89         let _export = instance.get_export(&mut store, "f");
90         Ok(())
91     })
92 }
93 
94 #[test]
instance_exports() -> Result<()>95 fn instance_exports() -> Result<()> {
96     let module_bytes = {
97         let mut config = Config::new();
98         config.concurrency_support(false);
99         let engine = Engine::new(&config)?;
100         Module::new(
101             &engine,
102             "(module (func (export \"f\")) (memory (export \"m\") 1) (global (export \"g\") i32 (i32.const 0)))",
103         )?
104         .serialize()?
105     };
106     let mut config = Config::new();
107     config.enable_compiler(false);
108     config.concurrency_support(false);
109     let engine = Engine::new(&config)?;
110     let module = unsafe { Module::deserialize(&engine, &module_bytes)? };
111     let linker = Linker::<()>::new(&engine);
112     let instance_pre = linker.instantiate_pre(&module)?;
113 
114     OomTest::new().test(|| {
115         let mut store = Store::try_new(&engine, ())?;
116         let instance = instance_pre.instantiate(&mut store)?;
117         let count = instance.exports(&mut store).count();
118         assert_eq!(count, 3);
119         Ok(())
120     })
121 }
122 
123 #[test]
instance_get_func() -> Result<()>124 fn instance_get_func() -> Result<()> {
125     let module_bytes = {
126         let mut config = Config::new();
127         config.concurrency_support(false);
128         let engine = Engine::new(&config)?;
129         Module::new(
130             &engine,
131             "(module (func (export \"f\") (param i32) (result i32) (local.get 0)))",
132         )?
133         .serialize()?
134     };
135     let mut config = Config::new();
136     config.enable_compiler(false);
137     config.concurrency_support(false);
138     let engine = Engine::new(&config)?;
139     let module = unsafe { Module::deserialize(&engine, &module_bytes)? };
140     let linker = Linker::<()>::new(&engine);
141     let instance_pre = linker.instantiate_pre(&module)?;
142 
143     OomTest::new().test(|| {
144         let mut store = Store::try_new(&engine, ())?;
145         let instance = instance_pre.instantiate(&mut store)?;
146         let f = instance.get_func(&mut store, "f");
147         assert!(f.is_some());
148         Ok(())
149     })
150 }
151 
152 #[test]
instance_get_typed_func() -> Result<()>153 fn instance_get_typed_func() -> Result<()> {
154     let module_bytes = {
155         let mut config = Config::new();
156         config.concurrency_support(false);
157         let engine = Engine::new(&config)?;
158         Module::new(
159             &engine,
160             "(module (func (export \"f\") (param i32) (result i32) (local.get 0)))",
161         )?
162         .serialize()?
163     };
164     let mut config = Config::new();
165     config.enable_compiler(false);
166     config.concurrency_support(false);
167     let engine = Engine::new(&config)?;
168     let module = unsafe { Module::deserialize(&engine, &module_bytes)? };
169     let linker = Linker::<()>::new(&engine);
170     let instance_pre = linker.instantiate_pre(&module)?;
171 
172     OomTest::new().test(|| {
173         let mut store = Store::try_new(&engine, ())?;
174         let instance = instance_pre.instantiate(&mut store)?;
175         let _f = instance.get_typed_func::<i32, i32>(&mut store, "f")?;
176         Ok(())
177     })
178 }
179 
180 #[test]
instance_get_table() -> Result<()>181 fn instance_get_table() -> Result<()> {
182     let module_bytes = {
183         let mut config = Config::new();
184         config.concurrency_support(false);
185         let engine = Engine::new(&config)?;
186         Module::new(&engine, "(module (table (export \"t\") 1 funcref))")?.serialize()?
187     };
188     let mut config = Config::new();
189     config.enable_compiler(false);
190     config.concurrency_support(false);
191     let engine = Engine::new(&config)?;
192     let module = unsafe { Module::deserialize(&engine, &module_bytes)? };
193     let linker = Linker::<()>::new(&engine);
194     let instance_pre = linker.instantiate_pre(&module)?;
195 
196     OomTest::new().test(|| {
197         let mut store = Store::try_new(&engine, ())?;
198         let instance = instance_pre.instantiate(&mut store)?;
199         let t = instance.get_table(&mut store, "t");
200         assert!(t.is_some());
201         Ok(())
202     })
203 }
204 
205 #[test]
instance_get_memory() -> Result<()>206 fn instance_get_memory() -> Result<()> {
207     let module_bytes = {
208         let mut config = Config::new();
209         config.concurrency_support(false);
210         let engine = Engine::new(&config)?;
211         Module::new(&engine, "(module (memory (export \"m\") 1))")?.serialize()?
212     };
213     let mut config = Config::new();
214     config.enable_compiler(false);
215     config.concurrency_support(false);
216     let engine = Engine::new(&config)?;
217     let module = unsafe { Module::deserialize(&engine, &module_bytes)? };
218     let linker = Linker::<()>::new(&engine);
219     let instance_pre = linker.instantiate_pre(&module)?;
220 
221     OomTest::new().test(|| {
222         let mut store = Store::try_new(&engine, ())?;
223         let instance = instance_pre.instantiate(&mut store)?;
224         let m = instance.get_memory(&mut store, "m");
225         assert!(m.is_some());
226         Ok(())
227     })
228 }
229 
230 #[test]
instance_get_global() -> Result<()>231 fn instance_get_global() -> Result<()> {
232     let module_bytes = {
233         let mut config = Config::new();
234         config.concurrency_support(false);
235         let engine = Engine::new(&config)?;
236         Module::new(
237             &engine,
238             "(module (global (export \"g\") i32 (i32.const 42)))",
239         )?
240         .serialize()?
241     };
242     let mut config = Config::new();
243     config.enable_compiler(false);
244     config.concurrency_support(false);
245     let engine = Engine::new(&config)?;
246     let module = unsafe { Module::deserialize(&engine, &module_bytes)? };
247     let linker = Linker::<()>::new(&engine);
248     let instance_pre = linker.instantiate_pre(&module)?;
249 
250     OomTest::new().test(|| {
251         let mut store = Store::try_new(&engine, ())?;
252         let instance = instance_pre.instantiate(&mut store)?;
253         let g = instance.get_global(&mut store, "g");
254         assert!(g.is_some());
255         Ok(())
256     })
257 }
258