/src/wasmtime/crates/wast/src/spectest.rs
Line | Count | Source |
1 | | use wasmtime::*; |
2 | | |
3 | | /// Configuration of how spectest primitives work. |
4 | | pub struct SpectestConfig { |
5 | | /// Whether or not to have a `shared_memory` definition. |
6 | | pub use_shared_memory: bool, |
7 | | /// Whether or not spectest functions that print things actually print things. |
8 | | pub suppress_prints: bool, |
9 | | } |
10 | | |
11 | | /// Return an instance implementing the "spectest" interface used in the |
12 | | /// spec testsuite. |
13 | 3.35k | pub fn link_spectest<T>( |
14 | 3.35k | linker: &mut Linker<T>, |
15 | 3.35k | store: &mut Store<T>, |
16 | 3.35k | config: &SpectestConfig, |
17 | 3.35k | ) -> Result<()> { |
18 | 3.35k | let suppress = config.suppress_prints; |
19 | 3.35k | linker.func_wrap("spectest", "print", || {})?; |
20 | 3.35k | linker.func_wrap("spectest", "print_i32", move |val: i32| { |
21 | 59 | if !suppress { |
22 | 0 | println!("{val}: i32") |
23 | 59 | } |
24 | 59 | })?; |
25 | 3.35k | linker.func_wrap("spectest", "print_i64", move |val: i64| { |
26 | 12 | if !suppress { |
27 | 0 | println!("{val}: i64") |
28 | 12 | } |
29 | 12 | })?; |
30 | 3.35k | linker.func_wrap("spectest", "print_f32", move |val: f32| { |
31 | 7 | if !suppress { |
32 | 0 | println!("{val}: f32") |
33 | 7 | } |
34 | 7 | })?; |
35 | 3.35k | linker.func_wrap("spectest", "print_f64", move |val: f64| { |
36 | 21 | if !suppress { |
37 | 0 | println!("{val}: f64") |
38 | 21 | } |
39 | 21 | })?; |
40 | 3.35k | linker.func_wrap("spectest", "print_i32_f32", move |i: i32, f: f32| { |
41 | 11 | if !suppress { |
42 | 0 | println!("{i}: i32"); |
43 | 0 | println!("{f}: f32"); |
44 | 11 | } |
45 | 11 | })?; |
46 | 3.35k | linker.func_wrap("spectest", "print_f64_f64", move |f1: f64, f2: f64| { |
47 | 7 | if !suppress { |
48 | 0 | println!("{f1}: f64"); |
49 | 0 | println!("{f2}: f64"); |
50 | 7 | } |
51 | 7 | })?; |
52 | | |
53 | 3.35k | let ty = GlobalType::new(ValType::I32, Mutability::Const); |
54 | 3.35k | let g = Global::new(&mut *store, ty, Val::I32(666))?; |
55 | 3.35k | linker.define(&mut *store, "spectest", "global_i32", g)?; |
56 | | |
57 | 3.35k | let ty = GlobalType::new(ValType::I64, Mutability::Const); |
58 | 3.35k | let g = Global::new(&mut *store, ty, Val::I64(666))?; |
59 | 3.35k | linker.define(&mut *store, "spectest", "global_i64", g)?; |
60 | | |
61 | 3.35k | let ty = GlobalType::new(ValType::F32, Mutability::Const); |
62 | 3.35k | let g = Global::new(&mut *store, ty, Val::F32(0x4426_a666))?; |
63 | 3.35k | linker.define(&mut *store, "spectest", "global_f32", g)?; |
64 | | |
65 | 3.35k | let ty = GlobalType::new(ValType::F64, Mutability::Const); |
66 | 3.35k | let g = Global::new(&mut *store, ty, Val::F64(0x4084_d4cc_cccc_cccd))?; |
67 | 3.35k | linker.define(&mut *store, "spectest", "global_f64", g)?; |
68 | | |
69 | 3.35k | let ty = TableType::new(RefType::FUNCREF, 10, Some(20)); |
70 | 3.35k | let table = Table::new(&mut *store, ty, Ref::Func(None))?; |
71 | 3.35k | linker.define(&mut *store, "spectest", "table", table)?; |
72 | | |
73 | 3.35k | let ty = TableType::new64(RefType::FUNCREF, 10, Some(20)); |
74 | 3.35k | let table = Table::new(&mut *store, ty, Ref::Func(None))?; |
75 | 3.35k | linker.define(&mut *store, "spectest", "table64", table)?; |
76 | | |
77 | 3.35k | let ty = MemoryType::new(1, Some(2)); |
78 | 3.35k | let memory = Memory::new(&mut *store, ty)?; |
79 | 3.35k | linker.define(&mut *store, "spectest", "memory", memory)?; |
80 | | |
81 | 3.35k | if config.use_shared_memory { |
82 | 3.35k | let ty = MemoryType::shared(1, 1); |
83 | 3.35k | let memory = SharedMemory::new(store.engine(), ty)?; |
84 | 3.35k | linker.define(&mut *store, "spectest", "shared_memory", memory)?; |
85 | 0 | } |
86 | | |
87 | 3.35k | Ok(()) |
88 | 3.35k | } |
89 | | |
90 | | #[cfg(feature = "component-model")] |
91 | 3.35k | pub fn link_component_spectest<T>(linker: &mut component::Linker<T>) -> Result<()> { |
92 | | use std::sync::Arc; |
93 | | use std::sync::atomic::{AtomicU32, Ordering::SeqCst}; |
94 | | use wasmtime::component::{Resource, ResourceType}; |
95 | | |
96 | 3.35k | let engine = linker.engine().clone(); |
97 | 3.35k | linker |
98 | 3.35k | .root() |
99 | 3.35k | .func_wrap_concurrent("host-echo-u32", |_, (v,): (u32,)| { |
100 | 0 | Box::pin(async move { Ok((v,)) }) |
101 | 0 | })?; |
102 | 3.35k | linker |
103 | 3.35k | .root() |
104 | 3.35k | .func_wrap("host-return-two", |_, _: ()| Ok((2u32,)))?; |
105 | 3.35k | let mut i = linker.instance("host")?; |
106 | 3.35k | i.func_wrap("return-three", |_, _: ()| Ok((3u32,)))?; |
107 | 3.35k | i.instance("nested")? |
108 | 3.35k | .func_wrap("return-four", |_, _: ()| Ok((4u32,)))?; |
109 | | |
110 | 3.35k | if !cfg!(miri) { |
111 | 3.35k | let module = Module::new( |
112 | 3.35k | &engine, |
113 | | r#" |
114 | | (module |
115 | | (global (export "g") i32 i32.const 100) |
116 | | (func (export "f") (result i32) i32.const 101) |
117 | | ) |
118 | | "#, |
119 | 0 | )?; |
120 | 3.35k | i.module("simple-module", &module)?; |
121 | 0 | } |
122 | | |
123 | | struct Resource1; |
124 | | struct Resource2; |
125 | | |
126 | | #[derive(Default)] |
127 | | struct ResourceState { |
128 | | drops: AtomicU32, |
129 | | last_drop: AtomicU32, |
130 | | } |
131 | | |
132 | 3.35k | let state = Arc::new(ResourceState::default()); |
133 | | |
134 | 3.35k | i.resource("resource1", ResourceType::host::<Resource1>(), { |
135 | 3.35k | let state = state.clone(); |
136 | 67 | move |_, rep| { |
137 | 67 | state.drops.fetch_add(1, SeqCst); |
138 | 67 | state.last_drop.store(rep, SeqCst); |
139 | | |
140 | 67 | Ok(()) |
141 | 67 | } |
142 | 0 | })?; |
143 | 3.35k | i.resource( |
144 | 3.35k | "resource2", |
145 | 3.35k | ResourceType::host::<Resource2>(), |
146 | 0 | |_, _| Ok(()), |
147 | 0 | )?; |
148 | | // Currently the embedder API requires redefining the resource destructor |
149 | | // here despite this being the same type as before, and fixing that is left |
150 | | // for a future refactoring. |
151 | 3.35k | i.resource( |
152 | 3.35k | "resource1-again", |
153 | 3.35k | ResourceType::host::<Resource1>(), |
154 | 0 | |_, _| { |
155 | 0 | panic!("shouldn't be destroyed"); |
156 | | }, |
157 | 0 | )?; |
158 | | |
159 | 3.35k | i.func_wrap("[constructor]resource1", |_cx, (rep,): (u32,)| { |
160 | 130 | Ok((Resource::<Resource1>::new_own(rep),)) |
161 | 130 | })?; |
162 | 3.35k | i.func_wrap( |
163 | 3.35k | "[static]resource1.assert", |
164 | 21 | |_cx, (resource, rep): (Resource<Resource1>, u32)| { |
165 | 21 | assert_eq!(resource.rep(), rep); |
166 | 21 | Ok(()) |
167 | 21 | }, |
168 | 0 | )?; |
169 | 3.35k | i.func_wrap("[static]resource1.last-drop", { |
170 | 3.35k | let state = state.clone(); |
171 | 14 | move |_, (): ()| Ok((state.last_drop.load(SeqCst),)) |
172 | 0 | })?; |
173 | 3.35k | i.func_wrap("[static]resource1.drops", { |
174 | 3.35k | let state = state.clone(); |
175 | 28 | move |_, (): ()| Ok((state.drops.load(SeqCst),)) |
176 | 0 | })?; |
177 | 3.35k | i.func_wrap( |
178 | 3.35k | "[method]resource1.simple", |
179 | 42 | |_cx, (resource, rep): (Resource<Resource1>, u32)| { |
180 | 42 | assert!(!resource.owned()); |
181 | 42 | assert_eq!(resource.rep(), rep); |
182 | 42 | Ok(()) |
183 | 42 | }, |
184 | 0 | )?; |
185 | | |
186 | 3.35k | i.func_wrap( |
187 | 3.35k | "[method]resource1.take-borrow", |
188 | 28 | |_, (a, b): (Resource<Resource1>, Resource<Resource1>)| { |
189 | 28 | assert!(!a.owned()); |
190 | 28 | assert!(!b.owned()); |
191 | 28 | Ok(()) |
192 | 28 | }, |
193 | 0 | )?; |
194 | 3.35k | i.func_wrap( |
195 | 3.35k | "[method]resource1.take-own", |
196 | 21 | |_cx, (a, b): (Resource<Resource1>, Resource<Resource1>)| { |
197 | 21 | assert!(!a.owned()); |
198 | 21 | assert!(b.owned()); |
199 | 21 | Ok(()) |
200 | 21 | }, |
201 | 0 | )?; |
202 | 3.35k | i.func_wrap_concurrent("never-return", |_, _: ()| { |
203 | 3 | Box::pin(async move { std::future::pending::<Result<()>>().await }) |
204 | 3 | })?; |
205 | 3.35k | i.func_wrap_concurrent("return-two-slowly", |_, _: ()| { |
206 | 37 | Box::pin(async move { |
207 | 37 | tokio::task::yield_now().await; |
208 | 37 | Ok((2,)) |
209 | 37 | }) |
210 | 37 | })?; |
211 | 3.35k | i.func_wrap_concurrent("echo-slowly", |_, (a,): (u32,)| { |
212 | 12 | Box::pin(async move { |
213 | 12 | tokio::task::yield_now().await; |
214 | 12 | Ok((a,)) |
215 | 12 | }) |
216 | 12 | })?; |
217 | 3.35k | i.func_wrap_concurrent( |
218 | 3.35k | "[method]resource1.never-return", |
219 | 4 | |_, (_,): (Resource<Resource1>,)| { |
220 | 4 | Box::pin(async move { std::future::pending::<Result<()>>().await }) |
221 | 4 | }, |
222 | 0 | )?; |
223 | 3.35k | i.func_wrap("return-hi", |_cx, (): ()| Ok(("hi".to_string(),)))?; |
224 | 3.35k | Ok(()) |
225 | 3.35k | } |