wasi.rs

  1use std::{
  2    collections::HashMap, fs::File, future::Future, marker::PhantomData, path::Path, pin::Pin,
  3};
  4
  5use anyhow::{anyhow, Error};
  6use serde::{de::DeserializeOwned, Serialize};
  7
  8use wasi_common::{dir, file};
  9use wasmtime::IntoFunc;
 10use wasmtime::{Caller, Config, Engine, Instance, Linker, Module, Store, TypedFunc};
 11use wasmtime_wasi::{Dir, WasiCtx, WasiCtxBuilder};
 12
 13pub struct WasiResource(u32);
 14
 15pub struct WasiFn<A: Serialize, R: DeserializeOwned> {
 16    function: TypedFunc<(u32, u32), u32>,
 17    _function_type: PhantomData<fn(A) -> R>,
 18}
 19
 20impl<A: Serialize, R: DeserializeOwned> Copy for WasiFn<A, R> {}
 21
 22impl<A: Serialize, R: DeserializeOwned> Clone for WasiFn<A, R> {
 23    fn clone(&self) -> Self {
 24        Self {
 25            function: self.function,
 26            _function_type: PhantomData,
 27        }
 28    }
 29}
 30
 31// impl<A: Serialize, R: DeserializeOwned> WasiFn<A, R> {
 32//     #[inline(always)]
 33//     pub async fn call(&self, runtime: &mut Wasi, arg: A) -> Result<R, Error> {
 34//         runtime.call(self, arg).await
 35//     }
 36// }
 37
 38pub struct Wasi {
 39    engine: Engine,
 40    module: Module,
 41    store: Store<WasiCtx>,
 42    instance: Instance,
 43    alloc_buffer: TypedFunc<u32, u32>,
 44    // free_buffer: TypedFunc<(u32, u32), ()>,
 45}
 46
 47// type signature derived from:
 48// https://docs.rs/wasmtime/latest/wasmtime/struct.Linker.html#method.func_wrap2_async
 49// macro_rules! dynHostFunction {
 50//     () => {
 51//         Box<
 52//             dyn for<'a> Fn(Caller<'a, WasiCtx>, u32, u32)
 53//                 -> Box<dyn Future<Output = u32> + Send + 'a>
 54//                     + Send
 55//                     + Sync
 56//                     + 'static
 57//         >
 58//     };
 59// }
 60
 61// macro_rules! implHostFunction {
 62//     () => {
 63//         impl for<'a> Fn(Caller<'a, WasiCtx>, u32, u32)
 64//             -> Box<dyn Future<Output = u32> + Send + 'a>
 65//                 + Send
 66//                 + Sync
 67//                 + 'static
 68//     };
 69// }
 70
 71// This type signature goodness gracious
 72pub type HostFunction = Box<dyn IntoFunc<WasiCtx, (u32, u32), u32>>;
 73
 74pub struct WasiPluginBuilder {
 75    host_functions: HashMap<String, HostFunction>,
 76    wasi_ctx_builder: WasiCtxBuilder,
 77}
 78
 79impl WasiPluginBuilder {
 80    pub fn new() -> Self {
 81        WasiPluginBuilder {
 82            host_functions: HashMap::new(),
 83            wasi_ctx_builder: WasiCtxBuilder::new(),
 84        }
 85    }
 86
 87    pub fn new_with_default_ctx() -> WasiPluginBuilder {
 88        let mut this = Self::new();
 89        this.wasi_ctx_builder = this.wasi_ctx_builder.inherit_stdin().inherit_stderr();
 90        this
 91    }
 92
 93    fn wrap_host_function<A: Serialize, R: DeserializeOwned>(
 94        function: impl Fn(A) -> R + Send + Sync + 'static,
 95    ) -> HostFunction {
 96        Box::new(move |ptr, len| {
 97            function(todo!());
 98            todo!()
 99        })
100    }
101
102    pub fn host_function<A: Serialize, R: DeserializeOwned>(
103        mut self,
104        name: &str,
105        function: impl Fn(A) -> R + Send + Sync + 'static,
106    ) -> Self {
107        self.host_functions
108            .insert(name.to_string(), Self::wrap_host_function(function));
109        self
110    }
111
112    pub fn wasi_ctx(mut self, config: impl FnOnce(WasiCtxBuilder) -> WasiCtxBuilder) -> Self {
113        self.wasi_ctx_builder = config(self.wasi_ctx_builder);
114        self
115    }
116
117    pub async fn init<T: AsRef<[u8]>>(self, module: T) -> Result<Wasi, Error> {
118        let plugin = WasiPlugin {
119            module: module.as_ref().to_vec(),
120            wasi_ctx: self.wasi_ctx_builder.build(),
121            host_functions: self.host_functions,
122        };
123
124        Wasi::init(plugin).await
125    }
126}
127
128/// Represents a to-be-initialized plugin.
129/// Please use [`WasiPluginBuilder`], don't use this directly.
130pub struct WasiPlugin {
131    pub module: Vec<u8>,
132    pub wasi_ctx: WasiCtx,
133    pub host_functions: HashMap<String, HostFunction>,
134}
135
136impl Wasi {
137    pub fn dump_memory(data: &[u8]) {
138        for (i, byte) in data.iter().enumerate() {
139            if i % 32 == 0 {
140                println!();
141            }
142            if i % 4 == 0 {
143                print!("|");
144            }
145            if *byte == 0 {
146                print!("__")
147            } else {
148                print!("{:02x}", byte);
149            }
150        }
151        println!();
152    }
153}
154
155impl Wasi {
156    async fn init(plugin: WasiPlugin) -> Result<Self, Error> {
157        let mut config = Config::default();
158        config.async_support(true);
159        let engine = Engine::new(&config)?;
160        let mut linker = Linker::new(&engine);
161
162        linker
163            .func_wrap("env", "__command", |x: u32, y: u32| x + y)
164            .unwrap();
165        linker.func_wrap("env", "__hello", |x: u32| x * 2).unwrap();
166        linker.func_wrap("env", "__bye", |x: u32| x / 2).unwrap();
167
168        wasmtime_wasi::add_to_linker(&mut linker, |s| s)?;
169
170        let mut store: Store<_> = Store::new(&engine, plugin.wasi_ctx);
171        let module = Module::new(&engine, plugin.module)?;
172
173        linker.module_async(&mut store, "", &module).await?;
174        let instance = linker.instantiate_async(&mut store, &module).await?;
175
176        let alloc_buffer = instance.get_typed_func(&mut store, "__alloc_buffer")?;
177        // let free_buffer = instance.get_typed_func(&mut store, "__free_buffer")?;
178
179        Ok(Wasi {
180            engine,
181            module,
182            store,
183            instance,
184            alloc_buffer,
185            // free_buffer,
186        })
187    }
188
189    /// Attaches a file or directory the the given system path to the runtime.
190    /// Note that the resource must be freed by calling `remove_resource` afterwards.
191    pub fn attach_path<T: AsRef<Path>>(&mut self, path: T) -> Result<WasiResource, Error> {
192        // grab the WASI context
193        let ctx = self.store.data_mut();
194
195        // open the file we want, and convert it into the right type
196        // this is a footgun and a half
197        let file = File::open(&path).unwrap();
198        let dir = Dir::from_std_file(file);
199        let dir = Box::new(wasmtime_wasi::dir::Dir::from_cap_std(dir));
200
201        // grab an empty file descriptor, specify capabilities
202        let fd = ctx.table().push(Box::new(()))?;
203        let caps = dir::DirCaps::all();
204        let file_caps = file::FileCaps::all();
205
206        // insert the directory at the given fd,
207        // return a handle to the resource
208        ctx.insert_dir(fd, dir, caps, file_caps, path.as_ref().to_path_buf());
209        Ok(WasiResource(fd))
210    }
211
212    /// Returns `true` if the resource existed and was removed.
213    pub fn remove_resource(&mut self, resource: WasiResource) -> Result<(), Error> {
214        self.store
215            .data_mut()
216            .table()
217            .delete(resource.0)
218            .ok_or_else(|| anyhow!("Resource did not exist, but a valid handle was passed in"))?;
219        Ok(())
220    }
221
222    // pub fn with_resource<T>(
223    //     &mut self,
224    //     resource: WasiResource,
225    //     callback: fn(&mut Self) -> Result<T, Error>,
226    // ) -> Result<T, Error> {
227    //     let result = callback(self);
228    //     self.remove_resource(resource)?;
229    //     return result;
230    // }
231
232    // So this call function is kinda a dance, I figured it'd be a good idea to document it.
233    // the high level is we take a serde type, serialize it to a byte array,
234    // (we're doing this using bincode for now)
235    // then toss that byte array into webassembly.
236    // webassembly grabs that byte array, does some magic,
237    // and serializes the result into yet another byte array.
238    // we then grab *that* result byte array and deserialize it into a result.
239    //
240    // phew...
241    //
242    // now the problem is, webassambly doesn't support buffers.
243    // only really like i32s, that's it (yeah, it's sad. Not even unsigned!)
244    // (ok, I'm exaggerating a bit).
245    //
246    // the Wasm function that this calls must have a very specific signature:
247    //
248    // fn(pointer to byte array: i32, length of byte array: i32)
249    //     -> pointer to (
250    //            pointer to byte_array: i32,
251    //            length of byte array: i32,
252    //     ): i32
253    //
254    // This pair `(pointer to byte array, length of byte array)` is called a `Buffer`
255    // and can be found in the cargo_test plugin.
256    //
257    // so on the wasm side, we grab the two parameters to the function,
258    // stuff them into a `Buffer`,
259    // and then pray to the `unsafe` Rust gods above that a valid byte array pops out.
260    //
261    // On the flip side, when returning from a wasm function,
262    // we convert whatever serialized result we get into byte array,
263    // which we stuff into a Buffer and allocate on the heap,
264    // which pointer to we then return.
265    // Note the double indirection!
266    //
267    // So when returning from a function, we actually leak memory *twice*:
268    //
269    // 1) once when we leak the byte array
270    // 2) again when we leak the allocated `Buffer`
271    //
272    // This isn't a problem because Wasm stops executing after the function returns,
273    // so the heap is still valid for our inspection when we want to pull things out.
274
275    /// Takes an item, allocates a buffer, serializes the argument to that buffer,
276    /// and returns a (ptr, len) pair to that buffer.
277    async fn serialize_to_buffer<T: Serialize>(&mut self, item: T) -> Result<(u32, u32), Error> {
278        // serialize the argument using bincode
279        let item = bincode::serialize(&item)?;
280        let buffer_len = item.len() as u32;
281
282        // allocate a buffer and write the argument to that buffer
283        let buffer_ptr = self
284            .alloc_buffer
285            .call_async(&mut self.store, buffer_len)
286            .await?;
287        let plugin_memory = self
288            .instance
289            .get_memory(&mut self.store, "memory")
290            .ok_or_else(|| anyhow!("Could not grab slice of plugin memory"))?;
291        plugin_memory.write(&mut self.store, buffer_ptr as usize, &item)?;
292        Ok((buffer_ptr, buffer_len))
293    }
294
295    /// Takes a ptr to a (ptr, len) pair and returns the corresponding deserialized buffer
296    fn deserialize_from_buffer<R: DeserializeOwned>(&mut self, buffer: u32) -> Result<R, Error> {
297        // create a buffer to read the (ptr, length) pair into
298        // this is a total of 4 + 4 = 8 bytes.
299        let raw_buffer = &mut [0; 8];
300        let plugin_memory = self
301            .instance
302            .get_memory(&mut self.store, "memory")
303            .ok_or_else(|| anyhow!("Could not grab slice of plugin memory"))?;
304        plugin_memory.read(&mut self.store, buffer as usize, raw_buffer)?;
305
306        // use these bytes (wasm stores things little-endian)
307        // to get a pointer to the buffer and its length
308        let b = raw_buffer;
309        let buffer_ptr = u32::from_le_bytes([b[0], b[1], b[2], b[3]]) as usize;
310        let buffer_len = u32::from_le_bytes([b[4], b[5], b[6], b[7]]) as usize;
311        let buffer_end = buffer_ptr + buffer_len;
312
313        // read the buffer at this point into a byte array
314        // deserialize the byte array into the provided serde type
315        let result = &plugin_memory.data(&mut self.store)[buffer_ptr..buffer_end];
316        let result = bincode::deserialize(result)?;
317
318        // TODO: this is handled wasm-side, but I'd like to double-check
319        // // deallocate the argument buffer
320        // self.free_buffer.call(&mut self.store, arg_buffer);
321
322        Ok(result)
323    }
324
325    pub fn function<A: Serialize, R: DeserializeOwned, T: AsRef<str>>(
326        &mut self,
327        name: T,
328    ) -> Result<WasiFn<A, R>, Error> {
329        let fun_name = format!("__{}", name.as_ref());
330        let fun = self
331            .instance
332            .get_typed_func::<(u32, u32), u32, _>(&mut self.store, &fun_name)?;
333        Ok(WasiFn {
334            function: fun,
335            _function_type: PhantomData,
336        })
337    }
338
339    // TODO: dont' use as for conversions
340    pub async fn call<A: Serialize, R: DeserializeOwned>(
341        &mut self,
342        handle: &WasiFn<A, R>,
343        arg: A,
344    ) -> Result<R, Error> {
345        // dbg!(&handle.name);
346        // dbg!(serde_json::to_string(&arg)).unwrap();
347
348        // write the argument to linear memory
349        // this returns a (ptr, lentgh) pair
350        let arg_buffer = self.serialize_to_buffer(arg).await?;
351
352        // get the webassembly function we want to actually call
353        // TODO: precompute handle
354        // let fun_name = format!("__{}", handle);
355        // let fun = self
356        //     .instance
357        //     .get_typed_func::<(u32, u32), u32, _>(&mut self.store, &fun_name)?;
358        let fun = handle.function;
359
360        // call the function, passing in the buffer and its length
361        // this returns a ptr to a (ptr, lentgh) pair
362        let result_buffer = fun.call_async(&mut self.store, arg_buffer).await?;
363
364        self.deserialize_from_buffer(result_buffer)
365    }
366}