1 #![cfg(arc_try_new)]
2 
3 use wasmtime::{Config, Engine, Memory, MemoryType, Result, Store};
4 use wasmtime_fuzzing::oom::OomTest;
5 
6 #[test]
memory_new() -> Result<()>7 fn memory_new() -> Result<()> {
8     let mut config = Config::new();
9     config.enable_compiler(false);
10     config.concurrency_support(false);
11     let engine = Engine::new(&config)?;
12 
13     OomTest::new()
14         // `IndexMap::reserve` will try to allocate double space, but if that
15         // fails, will attempt to allocate the minimal space necessary.
16         .allow_alloc_after_oom(true)
17         .test(|| {
18             let mut store = Store::try_new(&engine, ())?;
19             let _memory = Memory::new(&mut store, MemoryType::new(1, None))?;
20             Ok(())
21         })
22 }
23 
24 #[test]
memory_grow() -> Result<()>25 fn memory_grow() -> Result<()> {
26     let mut config = Config::new();
27     config.enable_compiler(false);
28     config.concurrency_support(false);
29     let engine = Engine::new(&config)?;
30 
31     OomTest::new().allow_alloc_after_oom(true).test(|| {
32         let mut store = Store::try_new(&engine, ())?;
33         let memory = Memory::new(&mut store, MemoryType::new(1, None))?;
34         let _old_size = memory.grow(&mut store, 1)?;
35         Ok(())
36     })
37 }
38 
39 #[test]
memory_ty() -> Result<()>40 fn memory_ty() -> Result<()> {
41     let mut config = Config::new();
42     config.enable_compiler(false);
43     config.concurrency_support(false);
44     let engine = Engine::new(&config)?;
45 
46     OomTest::new().allow_alloc_after_oom(true).test(|| {
47         let mut store = Store::try_new(&engine, ())?;
48         let memory = Memory::new(&mut store, MemoryType::new(1, None))?;
49         let _ty = memory.ty(&store);
50         Ok(())
51     })
52 }
53 
54 #[test]
memory_size() -> Result<()>55 fn memory_size() -> Result<()> {
56     let mut config = Config::new();
57     config.enable_compiler(false);
58     config.concurrency_support(false);
59     let engine = Engine::new(&config)?;
60 
61     OomTest::new().allow_alloc_after_oom(true).test(|| {
62         let mut store = Store::try_new(&engine, ())?;
63         let memory = Memory::new(&mut store, MemoryType::new(1, None))?;
64         assert_eq!(memory.size(&store), 1);
65         Ok(())
66     })
67 }
68 
69 #[test]
memory_data_size() -> Result<()>70 fn memory_data_size() -> Result<()> {
71     let mut config = Config::new();
72     config.enable_compiler(false);
73     config.concurrency_support(false);
74     let engine = Engine::new(&config)?;
75 
76     OomTest::new().allow_alloc_after_oom(true).test(|| {
77         let mut store = Store::try_new(&engine, ())?;
78         let memory = Memory::new(&mut store, MemoryType::new(1, None))?;
79         assert_eq!(memory.data_size(&store), 65536);
80         Ok(())
81     })
82 }
83 
84 #[test]
memory_read_write() -> Result<()>85 fn memory_read_write() -> Result<()> {
86     let mut config = Config::new();
87     config.enable_compiler(false);
88     config.concurrency_support(false);
89     let engine = Engine::new(&config)?;
90 
91     OomTest::new().allow_alloc_after_oom(true).test(|| {
92         let mut store = Store::try_new(&engine, ())?;
93         let memory = Memory::new(&mut store, MemoryType::new(1, None))?;
94         memory.write(&mut store, 0, &[1, 2, 3, 4])?;
95         let mut buf = [0u8; 4];
96         memory.read(&store, 0, &mut buf)?;
97         assert_eq!(buf, [1, 2, 3, 4]);
98         Ok(())
99     })
100 }
101 
102 #[test]
memory_data() -> Result<()>103 fn memory_data() -> Result<()> {
104     let mut config = Config::new();
105     config.enable_compiler(false);
106     config.concurrency_support(false);
107     let engine = Engine::new(&config)?;
108 
109     OomTest::new().allow_alloc_after_oom(true).test(|| {
110         let mut store = Store::try_new(&engine, ())?;
111         let memory = Memory::new(&mut store, MemoryType::new(1, None))?;
112         let data = memory.data(&store);
113         assert_eq!(data.len(), 65536);
114         Ok(())
115     })
116 }
117 
118 #[test]
memory_data_mut() -> Result<()>119 fn memory_data_mut() -> Result<()> {
120     let mut config = Config::new();
121     config.enable_compiler(false);
122     config.concurrency_support(false);
123     let engine = Engine::new(&config)?;
124 
125     OomTest::new().allow_alloc_after_oom(true).test(|| {
126         let mut store = Store::try_new(&engine, ())?;
127         let memory = Memory::new(&mut store, MemoryType::new(1, None))?;
128         let data = memory.data_mut(&mut store);
129         data[0] = 42;
130         assert_eq!(data[0], 42);
131         Ok(())
132     })
133 }
134