plugin.rs

  1use std::future::Future;
  2use std::pin::Pin;
  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        dbg!("new plugin");
 70        let mut config = Config::default();
 71        config.async_support(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!("started initialization");
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        let module = Module::new(&engine, module)?;
317        dbg!("created store");
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        dbg!("bound funcitons");
333
334        Ok(Plugin {
335            engine,
336            module,
337            store,
338            instance,
339        })
340    }
341
342    /// Attaches a file or directory the the given system path to the runtime.
343    /// Note that the resource must be freed by calling `remove_resource` afterwards.
344    pub fn attach_path<T: AsRef<Path>>(&mut self, path: T) -> Result<PluginResource, Error> {
345        // grab the WASI context
346        let ctx = self.store.data_mut();
347
348        // open the file we want, and convert it into the right type
349        // this is a footgun and a half
350        let file = File::open(&path).unwrap();
351        let dir = Dir::from_std_file(file);
352        let dir = Box::new(wasmtime_wasi::dir::Dir::from_cap_std(dir));
353
354        // grab an empty file descriptor, specify capabilities
355        let fd = ctx.wasi_ctx.table().push(Box::new(()))?;
356        let caps = dir::DirCaps::all();
357        let file_caps = file::FileCaps::all();
358
359        // insert the directory at the given fd,
360        // return a handle to the resource
361        ctx.wasi_ctx
362            .insert_dir(fd, dir, caps, file_caps, path.as_ref().to_path_buf());
363        Ok(PluginResource(fd))
364    }
365
366    /// Returns `true` if the resource existed and was removed.
367    /// Currently the only resource we support is adding scoped paths (e.g. folders and files)
368    /// to plugins using [`attach_path`].
369    pub fn remove_resource(&mut self, resource: PluginResource) -> Result<(), Error> {
370        self.store
371            .data_mut()
372            .wasi_ctx
373            .table()
374            .delete(resource.0)
375            .ok_or_else(|| anyhow!("Resource did not exist, but a valid handle was passed in"))?;
376        Ok(())
377    }
378
379    // So this call function is kinda a dance, I figured it'd be a good idea to document it.
380    // the high level is we take a serde type, serialize it to a byte array,
381    // (we're doing this using bincode for now)
382    // then toss that byte array into webassembly.
383    // webassembly grabs that byte array, does some magic,
384    // and serializes the result into yet another byte array.
385    // we then grab *that* result byte array and deserialize it into a result.
386    //
387    // phew...
388    //
389    // now the problem is, webassambly doesn't support buffers.
390    // only really like i32s, that's it (yeah, it's sad. Not even unsigned!)
391    // (ok, I'm exaggerating a bit).
392    //
393    // the Wasm function that this calls must have a very specific signature:
394    //
395    // fn(pointer to byte array: i32, length of byte array: i32)
396    //     -> pointer to (
397    //            pointer to byte_array: i32,
398    //            length of byte array: i32,
399    //     ): i32
400    //
401    // This pair `(pointer to byte array, length of byte array)` is called a `Buffer`
402    // and can be found in the cargo_test plugin.
403    //
404    // so on the wasm side, we grab the two parameters to the function,
405    // stuff them into a `Buffer`,
406    // and then pray to the `unsafe` Rust gods above that a valid byte array pops out.
407    //
408    // On the flip side, when returning from a wasm function,
409    // we convert whatever serialized result we get into byte array,
410    // which we stuff into a Buffer and allocate on the heap,
411    // which pointer to we then return.
412    // Note the double indirection!
413    //
414    // So when returning from a function, we actually leak memory *twice*:
415    //
416    // 1) once when we leak the byte array
417    // 2) again when we leak the allocated `Buffer`
418    //
419    // This isn't a problem because Wasm stops executing after the function returns,
420    // so the heap is still valid for our inspection when we want to pull things out.
421
422    /// Serializes a given type to bytes.
423    fn serialize_to_bytes<A: Serialize>(item: A) -> Result<Vec<u8>, Error> {
424        // serialize the argument using bincode
425        let bytes = bincode::serialize(&item)?;
426        Ok(bytes)
427    }
428
429    /// Deserializes a given type from bytes.
430    fn deserialize_to_type<R: DeserializeOwned>(bytes: &[u8]) -> Result<R, Error> {
431        // serialize the argument using bincode
432        let bytes = bincode::deserialize(bytes)?;
433        Ok(bytes)
434    }
435
436    // fn deserialize<R: DeserializeOwned>(
437    //     plugin_memory: &mut Memory,
438    //     mut store: impl AsContextMut<Data = WasiCtxAlloc>,
439    //     buffer: WasiBuffer,
440    // ) -> Result<R, Error> {
441    //     let buffer_start = buffer.ptr as usize;
442    //     let buffer_end = buffer_start + buffer.len as usize;
443
444    //     // read the buffer at this point into a byte array
445    //     // deserialize the byte array into the provided serde type
446    //     let item = &plugin_memory.data(store.as_context())[buffer_start..buffer_end];
447    //     let item = bincode::deserialize(bytes)?;
448    //     Ok(item)
449    // }
450
451    /// Takes an item, allocates a buffer, serializes the argument to that buffer,
452    /// and returns a (ptr, len) pair to that buffer.
453    async fn bytes_to_buffer(
454        alloc_buffer: TypedFunc<u32, u32>,
455        plugin_memory: &mut Memory,
456        mut store: impl AsContextMut<Data = WasiCtxAlloc>,
457        item: Vec<u8>,
458    ) -> Result<WasiBuffer, Error> {
459        // allocate a buffer and write the argument to that buffer
460        let len = item.len() as u32;
461        let ptr = alloc_buffer.call_async(&mut store, len).await?;
462        plugin_memory.write(&mut store, ptr as usize, &item)?;
463        Ok(WasiBuffer { ptr, len })
464    }
465
466    /// Takes a `(ptr, len)` pair and returns the corresponding deserialized buffer.
467    fn buffer_to_type<R: DeserializeOwned>(
468        plugin_memory: &Memory,
469        store: impl AsContext<Data = WasiCtxAlloc>,
470        buffer: &WasiBuffer,
471    ) -> Result<R, Error> {
472        let buffer_start = buffer.ptr as usize;
473        let buffer_end = buffer_start + buffer.len as usize;
474
475        // read the buffer at this point into a byte array
476        // deserialize the byte array into the provided serde type
477        let result = &plugin_memory.data(store.as_context())[buffer_start..buffer_end];
478        let result = bincode::deserialize(result)?;
479
480        Ok(result)
481    }
482
483    // TODO: don't allocate a new `Vec`!
484    /// Takes a `(ptr, len)` pair and returns the corresponding deserialized buffer.
485    fn buffer_to_bytes<'a>(
486        plugin_memory: &'a Memory,
487        store: impl AsContext<Data = WasiCtxAlloc> + 'a,
488        buffer: &WasiBuffer,
489    ) -> Result<Vec<u8>, Error> {
490        let buffer_start = buffer.ptr as usize;
491        let buffer_end = buffer_start + buffer.len as usize;
492
493        // read the buffer at this point into a byte array
494        // deserialize the byte array into the provided serde type
495        let result = plugin_memory.data(store.as_context())[buffer_start..buffer_end].to_vec();
496        Ok(result)
497    }
498
499    async fn buffer_to_free(
500        free_buffer: TypedFunc<u64, ()>,
501        mut store: impl AsContextMut<Data = WasiCtxAlloc>,
502        buffer: WasiBuffer,
503    ) -> Result<(), Error> {
504        // deallocate the argument buffer
505        Ok(free_buffer
506            .call_async(&mut store, buffer.into_u64())
507            .await?)
508    }
509
510    /// Retrieves the handle to a function of a given type.
511    pub fn function<A: Serialize, R: DeserializeOwned, T: AsRef<str>>(
512        &mut self,
513        name: T,
514    ) -> Result<WasiFn<A, R>, Error> {
515        let fun_name = format!("__{}", name.as_ref());
516        let fun = self
517            .instance
518            .get_typed_func::<u64, u64, _>(&mut self.store, &fun_name)?;
519        Ok(WasiFn {
520            function: fun,
521            _function_type: PhantomData,
522        })
523    }
524
525    // TODO: dont' use as for conversions
526    /// Asynchronously calls a function defined Guest-side.
527    pub async fn call<A: Serialize, R: DeserializeOwned>(
528        &mut self,
529        handle: &WasiFn<A, R>,
530        arg: A,
531    ) -> Result<R, Error> {
532        let mut plugin_memory = self
533            .instance
534            .get_memory(&mut self.store, "memory")
535            .ok_or_else(|| anyhow!("Could not grab slice of plugin memory"))?;
536
537        // write the argument to linear memory
538        // this returns a (ptr, lentgh) pair
539        let arg_buffer = Self::bytes_to_buffer(
540            self.store.data().alloc_buffer(),
541            &mut plugin_memory,
542            &mut self.store,
543            Self::serialize_to_bytes(arg)?,
544        )
545        .await?;
546
547        // call the function, passing in the buffer and its length
548        // this returns a ptr to a (ptr, lentgh) pair
549        let result_buffer = handle
550            .function
551            .call_async(&mut self.store, arg_buffer.into_u64())
552            .await?;
553
554        Self::buffer_to_type(
555            &mut plugin_memory,
556            &mut self.store,
557            &WasiBuffer::from_u64(result_buffer),
558        )
559    }
560}