wasi.rs

  1use std::{fs::File, os::unix::prelude::AsRawFd, path::Path};
  2
  3use anyhow::{anyhow, Error};
  4use serde::{de::DeserializeOwned, Serialize};
  5
  6use wasi_common::{dir, file};
  7use wasmtime::{Engine, Instance, Linker, Module, Store, TypedFunc};
  8use wasmtime_wasi::{Dir, WasiCtx, WasiCtxBuilder};
  9
 10pub struct WasiResource(u32);
 11
 12pub struct Wasi {
 13    engine: Engine,
 14    module: Module,
 15    store: Store<WasiCtx>,
 16    instance: Instance,
 17    alloc_buffer: TypedFunc<u32, u32>,
 18    // free_buffer: TypedFunc<(u32, u32), ()>,
 19}
 20
 21pub struct WasiPlugin {
 22    pub module: Vec<u8>,
 23    pub wasi_ctx: WasiCtx,
 24}
 25
 26impl Wasi {
 27    pub fn dump_memory(data: &[u8]) {
 28        for (i, byte) in data.iter().enumerate() {
 29            if i % 32 == 0 {
 30                println!();
 31            }
 32            if i % 4 == 0 {
 33                print!("|");
 34            }
 35            if *byte == 0 {
 36                print!("__")
 37            } else {
 38                print!("{:02x}", byte);
 39            }
 40        }
 41        println!();
 42    }
 43}
 44
 45impl Wasi {
 46    pub fn default_ctx() -> WasiCtx {
 47        WasiCtxBuilder::new()
 48            .inherit_stdout()
 49            .inherit_stderr()
 50            .build()
 51    }
 52
 53    pub fn init(plugin: WasiPlugin) -> Result<Self, Error> {
 54        let engine = Engine::default();
 55        let mut linker = Linker::new(&engine);
 56
 57        linker.func_wrap("env", "__hello", |x: u32| x * 2).unwrap();
 58        linker.func_wrap("env", "__bye", |x: u32| x / 2).unwrap();
 59
 60        wasmtime_wasi::add_to_linker(&mut linker, |s| s)?;
 61
 62        let mut store: Store<_> = Store::new(&engine, plugin.wasi_ctx);
 63        let module = Module::new(&engine, plugin.module)?;
 64
 65        linker.module(&mut store, "", &module)?;
 66        let instance = linker.instantiate(&mut store, &module)?;
 67
 68        let alloc_buffer = instance.get_typed_func(&mut store, "__alloc_buffer")?;
 69        // let free_buffer = instance.get_typed_func(&mut store, "__free_buffer")?;
 70
 71        Ok(Wasi {
 72            engine,
 73            module,
 74            store,
 75            instance,
 76            alloc_buffer,
 77            // free_buffer,
 78        })
 79    }
 80
 81    /// Attaches a file or directory the the given system path to the runtime.
 82    /// Note that the resource must be freed by calling `remove_resource` afterwards.
 83    pub fn attach_path<T: AsRef<Path>>(&mut self, path: T) -> Result<WasiResource, Error> {
 84        // grab the WASI context
 85        let ctx = self.store.data_mut();
 86
 87        // open the file we want, and convert it into the right type
 88        // this is a footgun and a half
 89        let file = File::open(&path).unwrap();
 90        let dir = Dir::from_std_file(file);
 91        let dir = Box::new(wasmtime_wasi::dir::Dir::from_cap_std(dir));
 92
 93        // grab an empty file descriptor, specify capabilities
 94        let fd = ctx.table().push(Box::new(()))?;
 95        let caps = dir::DirCaps::all();
 96        let file_caps = file::FileCaps::all();
 97
 98        // insert the directory at the given fd,
 99        // return a handle to the resource
100        ctx.insert_dir(fd, dir, caps, file_caps, path.as_ref().to_path_buf());
101        Ok(WasiResource(fd))
102    }
103
104    /// Returns `true` if the resource existed and was removed.
105    pub fn remove_resource(&mut self, resource: WasiResource) -> Result<(), Error> {
106        self.store
107            .data_mut()
108            .table()
109            .delete(resource.0)
110            .ok_or_else(|| anyhow!("Resource did not exist, but a valid handle was passed in"))?;
111        Ok(())
112    }
113
114    // pub fn with_resource<T>(
115    //     &mut self,
116    //     resource: WasiResource,
117    //     callback: fn(&mut Self) -> Result<T, Error>,
118    // ) -> Result<T, Error> {
119    //     let result = callback(self);
120    //     self.remove_resource(resource)?;
121    //     return result;
122    // }
123
124    // So this call function is kinda a dance, I figured it'd be a good idea to document it.
125    // the high level is we take a serde type, serialize it to a byte array,
126    // (we're doing this using bincode for now)
127    // then toss that byte array into webassembly.
128    // webassembly grabs that byte array, does some magic,
129    // and serializes the result into yet another byte array.
130    // we then grab *that* result byte array and deserialize it into a result.
131    //
132    // phew...
133    //
134    // now the problem is, webassambly doesn't support buffers.
135    // only really like i32s, that's it (yeah, it's sad. Not even unsigned!)
136    // (ok, I'm exaggerating a bit).
137    //
138    // the Wasm function that this calls must have a very specific signature:
139    //
140    // fn(pointer to byte array: i32, length of byte array: i32)
141    //     -> pointer to (
142    //            pointer to byte_array: i32,
143    //            length of byte array: i32,
144    //     ): i32
145    //
146    // This pair `(pointer to byte array, length of byte array)` is called a `Buffer`
147    // and can be found in the cargo_test plugin.
148    //
149    // so on the wasm side, we grab the two parameters to the function,
150    // stuff them into a `Buffer`,
151    // and then pray to the `unsafe` Rust gods above that a valid byte array pops out.
152    //
153    // On the flip side, when returning from a wasm function,
154    // we convert whatever serialized result we get into byte array,
155    // which we stuff into a Buffer and allocate on the heap,
156    // which pointer to we then return.
157    // Note the double indirection!
158    //
159    // So when returning from a function, we actually leak memory *twice*:
160    //
161    // 1) once when we leak the byte array
162    // 2) again when we leak the allocated `Buffer`
163    //
164    // This isn't a problem because Wasm stops executing after the function returns,
165    // so the heap is still valid for our inspection when we want to pull things out.
166
167    /// Takes an item, allocates a buffer, serializes the argument to that buffer,
168    /// and returns a (ptr, len) pair to that buffer.
169    fn serialize_to_buffer<T: Serialize>(&mut self, item: T) -> Result<(u32, u32), Error> {
170        // serialize the argument using bincode
171        let item = bincode::serialize(&item)?;
172        let buffer_len = item.len() as u32;
173
174        // allocate a buffer and write the argument to that buffer
175        let buffer_ptr = self.alloc_buffer.call(&mut self.store, buffer_len)?;
176        let plugin_memory = self
177            .instance
178            .get_memory(&mut self.store, "memory")
179            .ok_or_else(|| anyhow!("Could not grab slice of plugin memory"))?;
180        plugin_memory.write(&mut self.store, buffer_ptr as usize, &item)?;
181        Ok((buffer_ptr, buffer_len))
182    }
183
184    /// Takes a ptr to a (ptr, len) pair and returns the corresponding deserialized buffer
185    fn deserialize_from_buffer<R: DeserializeOwned>(&mut self, buffer: u32) -> Result<R, Error> {
186        // create a buffer to read the (ptr, length) pair into
187        // this is a total of 4 + 4 = 8 bytes.
188        let raw_buffer = &mut [0; 8];
189        let plugin_memory = self
190            .instance
191            .get_memory(&mut self.store, "memory")
192            .ok_or_else(|| anyhow!("Could not grab slice of plugin memory"))?;
193        plugin_memory.read(&mut self.store, buffer as usize, raw_buffer)?;
194
195        // use these bytes (wasm stores things little-endian)
196        // to get a pointer to the buffer and its length
197        let b = raw_buffer;
198        let buffer_ptr = u32::from_le_bytes([b[0], b[1], b[2], b[3]]) as usize;
199        let buffer_len = u32::from_le_bytes([b[4], b[5], b[6], b[7]]) as usize;
200        let buffer_end = buffer_ptr + buffer_len;
201
202        // read the buffer at this point into a byte array
203        // deserialize the byte array into the provided serde type
204        let result = &plugin_memory.data(&mut self.store)[buffer_ptr..buffer_end];
205        let result = bincode::deserialize(result)?;
206
207        // TODO: this is handled wasm-side, but I'd like to double-check
208        // // deallocate the argument buffer
209        // self.free_buffer.call(&mut self.store, arg_buffer);
210
211        Ok(result)
212    }
213
214    // TODO: dont' use as for conversions
215    pub fn call<A: Serialize, R: DeserializeOwned>(
216        &mut self,
217        handle: &str,
218        arg: A,
219    ) -> Result<R, Error> {
220        let start = std::time::Instant::now();
221        dbg!(&handle);
222        // dbg!(serde_json::to_string(&arg)).unwrap();
223
224        // write the argument to linear memory
225        // this returns a (ptr, lentgh) pair
226        let arg_buffer = self.serialize_to_buffer(arg)?;
227
228        // get the webassembly function we want to actually call
229        // TODO: precompute handle
230        let fun_name = format!("__{}", handle);
231        let fun = self
232            .instance
233            .get_typed_func::<(u32, u32), u32, _>(&mut self.store, &fun_name)?;
234
235        // call the function, passing in the buffer and its length
236        // this returns a ptr to a (ptr, lentgh) pair
237        let result_buffer = fun.call(&mut self.store, arg_buffer)?;
238
239        self.deserialize_from_buffer(result_buffer)
240    }
241}