plugin.rs

  1use std::future::Future;
  2
  3use std::{fs::File, marker::PhantomData, path::Path};
  4
  5use anyhow::{anyhow, Error};
  6use serde::{de::DeserializeOwned, Serialize};
  7
  8use wasi_common::{dir, file};
  9use wasmtime::Memory;
 10use wasmtime::{
 11    AsContext, AsContextMut, Caller, Config, Engine, Extern, Instance, Linker, Module, Store, Trap,
 12    TypedFunc,
 13};
 14use wasmtime_wasi::{Dir, WasiCtx, WasiCtxBuilder};
 15
 16/// Represents a resource currently managed by the plugin, like a file descriptor.
 17pub struct PluginResource(u32);
 18
 19#[repr(C)]
 20struct WasiBuffer {
 21    ptr: u32,
 22    len: u32,
 23}
 24
 25impl WasiBuffer {
 26    pub fn into_u64(self) -> u64 {
 27        ((self.ptr as u64) << 32) | (self.len as u64)
 28    }
 29
 30    pub fn from_u64(packed: u64) -> Self {
 31        WasiBuffer {
 32            ptr: (packed >> 32) as u32,
 33            len: packed as u32,
 34        }
 35    }
 36}
 37
 38/// Represents a typed WebAssembly function.
 39pub struct WasiFn<A: Serialize, R: DeserializeOwned> {
 40    function: TypedFunc<u64, u64>,
 41    _function_type: PhantomData<fn(A) -> R>,
 42}
 43
 44impl<A: Serialize, R: DeserializeOwned> Copy for WasiFn<A, R> {}
 45
 46impl<A: Serialize, R: DeserializeOwned> Clone for WasiFn<A, R> {
 47    fn clone(&self) -> Self {
 48        Self {
 49            function: self.function,
 50            _function_type: PhantomData,
 51        }
 52    }
 53}
 54
 55/// This struct is used to build a new [`Plugin`], using the builder pattern.
 56/// Create a new default plugin with `PluginBuilder::new_with_default_ctx`,
 57/// and add host-side exported functions using `host_function` and `host_function_async`.
 58/// Finalize the plugin by calling [`init`].
 59pub struct PluginBuilder {
 60    wasi_ctx: WasiCtx,
 61    engine: Engine,
 62    linker: Linker<WasiCtxAlloc>,
 63}
 64
 65impl PluginBuilder {
 66    /// Create a new [`PluginBuilder`] with the given WASI context.
 67    /// Using the default context is a safe bet, see [`new_with_default_context`].
 68    pub fn new(wasi_ctx: WasiCtx) -> Result<Self, Error> {
 69        let mut config = Config::default();
 70        config.async_support(true);
 71        // config.epoch_interruption(true);
 72        let engine = Engine::new(&config)?;
 73        let linker = Linker::new(&engine);
 74
 75        Ok(PluginBuilder {
 76            // host_functions: HashMap::new(),
 77            wasi_ctx,
 78            engine,
 79            linker,
 80        })
 81    }
 82
 83    /// Create a new `PluginBuilder` that inherits the
 84    /// host processes' access to `stdout` and `stderr`.
 85    pub fn new_with_default_ctx() -> Result<Self, Error> {
 86        let wasi_ctx = WasiCtxBuilder::new()
 87            .inherit_stdout()
 88            .inherit_stderr()
 89            .build();
 90        Self::new(wasi_ctx)
 91    }
 92
 93    /// Add an `async` host function. See [`host_function`] for details.
 94    pub fn host_function_async<F, A, R, Fut>(
 95        mut self,
 96        name: &str,
 97        function: F,
 98    ) -> Result<Self, Error>
 99    where
100        F: Fn(A) -> Fut + Send + Sync + 'static,
101        Fut: Future<Output = R> + Send + 'static,
102        A: DeserializeOwned + Send + 'static,
103        R: Serialize + Send + Sync + 'static,
104    {
105        self.linker.func_wrap1_async(
106            "env",
107            &format!("__{}", name),
108            move |mut caller: Caller<'_, WasiCtxAlloc>, packed_buffer: u64| {
109                // TODO: use try block once avaliable
110                let result: Result<(WasiBuffer, Memory, _), Trap> = (|| {
111                    // grab a handle to the memory
112                    let mut plugin_memory = match caller.get_export("memory") {
113                        Some(Extern::Memory(mem)) => mem,
114                        _ => return Err(Trap::new("Could not grab slice of plugin memory"))?,
115                    };
116
117                    let buffer = WasiBuffer::from_u64(packed_buffer);
118
119                    // get the args passed from Guest
120                    let args = Plugin::buffer_to_bytes(&mut plugin_memory, &mut caller, &buffer)?;
121
122                    let args: A = Plugin::deserialize_to_type(&args)?;
123
124                    // Call the Host-side function
125                    let result = function(args);
126
127                    Ok((buffer, plugin_memory, result))
128                })();
129
130                Box::new(async move {
131                    let (buffer, mut plugin_memory, future) = result?;
132
133                    let result: R = future.await;
134                    let result: Result<Vec<u8>, Error> = Plugin::serialize_to_bytes(result)
135                        .map_err(|_| {
136                            Trap::new("Could not serialize value returned from function").into()
137                        });
138                    let result = result?;
139
140                    Plugin::buffer_to_free(caller.data().free_buffer(), &mut caller, buffer)
141                        .await?;
142
143                    let buffer = Plugin::bytes_to_buffer(
144                        caller.data().alloc_buffer(),
145                        &mut plugin_memory,
146                        &mut caller,
147                        result,
148                    )
149                    .await?;
150
151                    Ok(buffer.into_u64())
152                })
153            },
154        )?;
155        Ok(self)
156    }
157
158    /// Add a new host function to the given `PluginBuilder`.
159    /// A host function is a function defined host-side, in Rust,
160    /// that is accessible guest-side, in WebAssembly.
161    /// You can specify host-side functions to import using
162    /// the `#[input]` macro attribute:
163    /// ```ignore
164    /// #[input]
165    /// fn total(counts: Vec<f64>) -> f64;
166    /// ```
167    /// When loading a plugin, you need to provide all host functions the plugin imports:
168    /// ```ignore
169    /// let plugin = PluginBuilder::new_with_default_context()
170    ///     .host_function("total", |counts| counts.iter().fold(0.0, |tot, n| tot + n))
171    ///     // and so on...
172    /// ```
173    /// And that's a wrap!
174    pub fn host_function<A, R>(
175        mut self,
176        name: &str,
177        function: impl Fn(A) -> R + Send + Sync + 'static,
178    ) -> Result<Self, Error>
179    where
180        A: DeserializeOwned + Send,
181        R: Serialize + Send + Sync,
182    {
183        self.linker.func_wrap1_async(
184            "env",
185            &format!("__{}", name),
186            move |mut caller: Caller<'_, WasiCtxAlloc>, packed_buffer: u64| {
187                // TODO: use try block once avaliable
188                let result: Result<(WasiBuffer, Memory, Vec<u8>), Trap> = (|| {
189                    // grab a handle to the memory
190                    let mut plugin_memory = match caller.get_export("memory") {
191                        Some(Extern::Memory(mem)) => mem,
192                        _ => return Err(Trap::new("Could not grab slice of plugin memory"))?,
193                    };
194
195                    let buffer = WasiBuffer::from_u64(packed_buffer);
196
197                    // get the args passed from Guest
198                    let args = Plugin::buffer_to_type(&mut plugin_memory, &mut caller, &buffer)?;
199
200                    // Call the Host-side function
201                    let result: R = function(args);
202
203                    // Serialize the result back to guest
204                    let result = Plugin::serialize_to_bytes(result).map_err(|_| {
205                        Trap::new("Could not serialize value returned from function")
206                    })?;
207
208                    Ok((buffer, plugin_memory, result))
209                })();
210
211                Box::new(async move {
212                    let (buffer, mut plugin_memory, result) = result?;
213
214                    Plugin::buffer_to_free(caller.data().free_buffer(), &mut caller, buffer)
215                        .await?;
216
217                    let buffer = Plugin::bytes_to_buffer(
218                        caller.data().alloc_buffer(),
219                        &mut plugin_memory,
220                        &mut caller,
221                        result,
222                    )
223                    .await?;
224
225                    Ok(buffer.into_u64())
226                })
227            },
228        )?;
229        Ok(self)
230    }
231
232    /// Initializes a [`Plugin`] from a given compiled Wasm module.
233    /// Both binary (`.wasm`) and text (`.wat`) module formats are supported.
234    pub async fn init<T: AsRef<[u8]>>(self, module: T) -> Result<Plugin, Error> {
235        dbg!("initializing plugin");
236        Plugin::init(module.as_ref().to_vec(), self).await
237    }
238}
239
240#[derive(Copy, Clone)]
241struct WasiAlloc {
242    alloc_buffer: TypedFunc<u32, u32>,
243    free_buffer: TypedFunc<u64, ()>,
244}
245
246struct WasiCtxAlloc {
247    wasi_ctx: WasiCtx,
248    alloc: Option<WasiAlloc>,
249}
250
251impl WasiCtxAlloc {
252    fn alloc_buffer(&self) -> TypedFunc<u32, u32> {
253        self.alloc
254            .expect("allocator has been not initialized, cannot allocate buffer!")
255            .alloc_buffer
256    }
257
258    fn free_buffer(&self) -> TypedFunc<u64, ()> {
259        self.alloc
260            .expect("allocator has been not initialized, cannot free buffer!")
261            .free_buffer
262    }
263
264    fn init_alloc(&mut self, alloc: WasiAlloc) {
265        self.alloc = Some(alloc)
266    }
267}
268
269/// Represents a WebAssembly plugin, with access to the WebAssembly System Inferface.
270/// Build a new plugin using [`PluginBuilder`].
271pub struct Plugin {
272    engine: Engine,
273    module: Module,
274    store: Store<WasiCtxAlloc>,
275    instance: Instance,
276}
277
278impl Plugin {
279    /// Dumps the *entirety* of Wasm linear memory to `stdout`.
280    /// Don't call this unless you're debugging a memory issue!
281    pub fn dump_memory(data: &[u8]) {
282        for (i, byte) in data.iter().enumerate() {
283            if i % 32 == 0 {
284                println!();
285            }
286            if i % 4 == 0 {
287                print!("|");
288            }
289            if *byte == 0 {
290                print!("__")
291            } else {
292                print!("{:02x}", byte);
293            }
294        }
295        println!();
296    }
297}
298
299impl Plugin {
300    async fn init(module: Vec<u8>, plugin: PluginBuilder) -> Result<Self, Error> {
301        dbg!("Initializing new plugin");
302        // initialize the WebAssembly System Interface context
303        let engine = plugin.engine;
304        let mut linker = plugin.linker;
305        wasmtime_wasi::add_to_linker(&mut linker, |s| &mut s.wasi_ctx)?;
306
307        // create a store, note that we can't initialize the allocator,
308        // because we can't grab the functions until initialized.
309        let mut store: Store<WasiCtxAlloc> = Store::new(
310            &engine,
311            WasiCtxAlloc {
312                wasi_ctx: plugin.wasi_ctx,
313                alloc: None,
314            },
315        );
316        // store.epoch_deadline_async_yield_and_update(todo!());
317        let module = Module::new(&engine, module)?;
318
319        // load the provided module into the asynchronous runtime
320        linker.module_async(&mut store, "", &module).await?;
321        let instance = linker.instantiate_async(&mut store, &module).await?;
322
323        // now that the module is initialized,
324        // we can initialize the store's allocator
325        let alloc_buffer = instance.get_typed_func(&mut store, "__alloc_buffer")?;
326        let free_buffer = instance.get_typed_func(&mut store, "__free_buffer")?;
327        store.data_mut().init_alloc(WasiAlloc {
328            alloc_buffer,
329            free_buffer,
330        });
331
332        Ok(Plugin {
333            engine,
334            module,
335            store,
336            instance,
337        })
338    }
339
340    /// Attaches a file or directory the the given system path to the runtime.
341    /// Note that the resource must be freed by calling `remove_resource` afterwards.
342    pub fn attach_path<T: AsRef<Path>>(&mut self, path: T) -> Result<PluginResource, Error> {
343        // grab the WASI context
344        let ctx = self.store.data_mut();
345
346        // open the file we want, and convert it into the right type
347        // this is a footgun and a half
348        let file = File::open(&path).unwrap();
349        let dir = Dir::from_std_file(file);
350        let dir = Box::new(wasmtime_wasi::dir::Dir::from_cap_std(dir));
351
352        // grab an empty file descriptor, specify capabilities
353        let fd = ctx.wasi_ctx.table().push(Box::new(()))?;
354        let caps = dir::DirCaps::all();
355        let file_caps = file::FileCaps::all();
356
357        // insert the directory at the given fd,
358        // return a handle to the resource
359        ctx.wasi_ctx
360            .insert_dir(fd, dir, caps, file_caps, path.as_ref().to_path_buf());
361        Ok(PluginResource(fd))
362    }
363
364    /// Returns `true` if the resource existed and was removed.
365    /// Currently the only resource we support is adding scoped paths (e.g. folders and files)
366    /// to plugins using [`attach_path`].
367    pub fn remove_resource(&mut self, resource: PluginResource) -> Result<(), Error> {
368        self.store
369            .data_mut()
370            .wasi_ctx
371            .table()
372            .delete(resource.0)
373            .ok_or_else(|| anyhow!("Resource did not exist, but a valid handle was passed in"))?;
374        Ok(())
375    }
376
377    // So this call function is kinda a dance, I figured it'd be a good idea to document it.
378    // the high level is we take a serde type, serialize it to a byte array,
379    // (we're doing this using bincode for now)
380    // then toss that byte array into webassembly.
381    // webassembly grabs that byte array, does some magic,
382    // and serializes the result into yet another byte array.
383    // we then grab *that* result byte array and deserialize it into a result.
384    //
385    // phew...
386    //
387    // now the problem is, webassambly doesn't support buffers.
388    // only really like i32s, that's it (yeah, it's sad. Not even unsigned!)
389    // (ok, I'm exaggerating a bit).
390    //
391    // the Wasm function that this calls must have a very specific signature:
392    //
393    // fn(pointer to byte array: i32, length of byte array: i32)
394    //     -> pointer to (
395    //            pointer to byte_array: i32,
396    //            length of byte array: i32,
397    //     ): i32
398    //
399    // This pair `(pointer to byte array, length of byte array)` is called a `Buffer`
400    // and can be found in the cargo_test plugin.
401    //
402    // so on the wasm side, we grab the two parameters to the function,
403    // stuff them into a `Buffer`,
404    // and then pray to the `unsafe` Rust gods above that a valid byte array pops out.
405    //
406    // On the flip side, when returning from a wasm function,
407    // we convert whatever serialized result we get into byte array,
408    // which we stuff into a Buffer and allocate on the heap,
409    // which pointer to we then return.
410    // Note the double indirection!
411    //
412    // So when returning from a function, we actually leak memory *twice*:
413    //
414    // 1) once when we leak the byte array
415    // 2) again when we leak the allocated `Buffer`
416    //
417    // This isn't a problem because Wasm stops executing after the function returns,
418    // so the heap is still valid for our inspection when we want to pull things out.
419
420    /// Serializes a given type to bytes.
421    fn serialize_to_bytes<A: Serialize>(item: A) -> Result<Vec<u8>, Error> {
422        // serialize the argument using bincode
423        let bytes = bincode::serialize(&item)?;
424        Ok(bytes)
425    }
426
427    /// Deserializes a given type from bytes.
428    fn deserialize_to_type<R: DeserializeOwned>(bytes: &[u8]) -> Result<R, Error> {
429        // serialize the argument using bincode
430        let bytes = bincode::deserialize(bytes)?;
431        Ok(bytes)
432    }
433
434    // fn deserialize<R: DeserializeOwned>(
435    //     plugin_memory: &mut Memory,
436    //     mut store: impl AsContextMut<Data = WasiCtxAlloc>,
437    //     buffer: WasiBuffer,
438    // ) -> Result<R, Error> {
439    //     let buffer_start = buffer.ptr as usize;
440    //     let buffer_end = buffer_start + buffer.len as usize;
441
442    //     // read the buffer at this point into a byte array
443    //     // deserialize the byte array into the provided serde type
444    //     let item = &plugin_memory.data(store.as_context())[buffer_start..buffer_end];
445    //     let item = bincode::deserialize(bytes)?;
446    //     Ok(item)
447    // }
448
449    /// Takes an item, allocates a buffer, serializes the argument to that buffer,
450    /// and returns a (ptr, len) pair to that buffer.
451    async fn bytes_to_buffer(
452        alloc_buffer: TypedFunc<u32, u32>,
453        plugin_memory: &mut Memory,
454        mut store: impl AsContextMut<Data = WasiCtxAlloc>,
455        item: Vec<u8>,
456    ) -> Result<WasiBuffer, Error> {
457        // allocate a buffer and write the argument to that buffer
458        let len = item.len() as u32;
459        let ptr = alloc_buffer.call_async(&mut store, len).await?;
460        plugin_memory.write(&mut store, ptr as usize, &item)?;
461        Ok(WasiBuffer { ptr, len })
462    }
463
464    /// Takes a `(ptr, len)` pair and returns the corresponding deserialized buffer.
465    fn buffer_to_type<R: DeserializeOwned>(
466        plugin_memory: &Memory,
467        store: impl AsContext<Data = WasiCtxAlloc>,
468        buffer: &WasiBuffer,
469    ) -> Result<R, Error> {
470        let buffer_start = buffer.ptr as usize;
471        let buffer_end = buffer_start + buffer.len as usize;
472
473        // read the buffer at this point into a byte array
474        // deserialize the byte array into the provided serde type
475        let result = &plugin_memory.data(store.as_context())[buffer_start..buffer_end];
476        let result = bincode::deserialize(result)?;
477
478        Ok(result)
479    }
480
481    // TODO: don't allocate a new `Vec`!
482    /// Takes a `(ptr, len)` pair and returns the corresponding deserialized buffer.
483    fn buffer_to_bytes<'a>(
484        plugin_memory: &'a Memory,
485        store: impl AsContext<Data = WasiCtxAlloc> + 'a,
486        buffer: &WasiBuffer,
487    ) -> Result<Vec<u8>, Error> {
488        let buffer_start = buffer.ptr as usize;
489        let buffer_end = buffer_start + buffer.len as usize;
490
491        // read the buffer at this point into a byte array
492        // deserialize the byte array into the provided serde type
493        let result = plugin_memory.data(store.as_context())[buffer_start..buffer_end].to_vec();
494        Ok(result)
495    }
496
497    async fn buffer_to_free(
498        free_buffer: TypedFunc<u64, ()>,
499        mut store: impl AsContextMut<Data = WasiCtxAlloc>,
500        buffer: WasiBuffer,
501    ) -> Result<(), Error> {
502        // deallocate the argument buffer
503        Ok(free_buffer
504            .call_async(&mut store, buffer.into_u64())
505            .await?)
506    }
507
508    /// Retrieves the handle to a function of a given type.
509    pub fn function<A: Serialize, R: DeserializeOwned, T: AsRef<str>>(
510        &mut self,
511        name: T,
512    ) -> Result<WasiFn<A, R>, Error> {
513        let fun_name = format!("__{}", name.as_ref());
514        let fun = self
515            .instance
516            .get_typed_func::<u64, u64, _>(&mut self.store, &fun_name)?;
517        Ok(WasiFn {
518            function: fun,
519            _function_type: PhantomData,
520        })
521    }
522
523    // TODO: dont' use as for conversions
524    /// Asynchronously calls a function defined Guest-side.
525    pub async fn call<A: Serialize, R: DeserializeOwned>(
526        &mut self,
527        handle: &WasiFn<A, R>,
528        arg: A,
529    ) -> Result<R, Error> {
530        let mut plugin_memory = self
531            .instance
532            .get_memory(&mut self.store, "memory")
533            .ok_or_else(|| anyhow!("Could not grab slice of plugin memory"))?;
534
535        // write the argument to linear memory
536        // this returns a (ptr, lentgh) pair
537        let arg_buffer = Self::bytes_to_buffer(
538            self.store.data().alloc_buffer(),
539            &mut plugin_memory,
540            &mut self.store,
541            Self::serialize_to_bytes(arg)?,
542        )
543        .await?;
544
545        // call the function, passing in the buffer and its length
546        // this returns a ptr to a (ptr, lentgh) pair
547        let result_buffer = handle
548            .function
549            .call_async(&mut self.store, arg_buffer.into_u64())
550            .await?;
551
552        Self::buffer_to_type(
553            &mut plugin_memory,
554            &mut self.store,
555            &WasiBuffer::from_u64(result_buffer),
556        )
557    }
558}