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