1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
#![doc(html_logo_url = "https://avatars0.githubusercontent.com/u/54989751?s=200&v=4")]

//! # wapc
//!
//! The `wapc` crate provides a WebAssembly host runtime that conforms to an RPC mechanism
//! called **waPC**. waPC is designed specifically to prevent either side of the call from having
//! to know anything about _how_ or _when_ memory is allocated or freed. The interface may at first appear more
//! "chatty" than other protocols, but the cleanliness, ease of use, and simplified developer experience
//! is worth the few extra nanoseconds of latency.
//!
//! To use `wapc`, first you'll need a waPC-compliant WebAssembly module (referred to as the _guest_) to load
//! and interpret. You can find a number of these samples available in the GitHub repository,
//! and anything compiled with the [wascc](https://github.com/wascc) actor SDK can also be invoked
//! via waPC as it is 100% waPC compliant.
//!
//! To make function calls, first set your `host_callback` function, a function invoked by the _guest_.
//! Then execute `call` on the `WapcHost` instance.
//! # Example
//! ```
//! extern crate wapc;
//! use wapc::prelude::*;
//!
//! # fn load_file() -> Vec<u8> {
//! #    include_bytes!("../.assets/hello.wasm").to_vec()
//! # }
//! # fn load_wasi_file() -> Vec<u8> {
//! #    include_bytes!("../.assets/hello_wasi.wasm").to_vec()
//! # }
//! pub fn main() -> Result<(), Box<dyn std::error::Error>> {
//! # env_logger::init();
//!     let module_bytes = load_file();
//!     let mut host = WapcHost::new(host_callback, &module_bytes, None)?;
//!
//!     let res = host.call("wapc:sample!Hello", b"this is a test")?;
//!     assert_eq!(res, b"hello world!");
//!
//!     Ok(())
//! }
//!
//! fn host_callback(id: u64, ns: &str, op: &str, payload: &[u8]) -> Result<Vec<u8>, Box<dyn std::error::Error>> {
//!     println!("Guest {} invoked '{}:{}' with payload of {} bytes", id, ns, op, payload.len());
//!     Ok(vec![])
//! }
//! ```
//!
//! # Notes
//! waPC is _reactive_. Guest modules cannot initiate host calls without first handling a call
//! initiated by the host. waPC will not automatically invoke any start functions--that decision
//! is up to the waPC library consumer. Guest modules can synchronously make as many host calls
//! as they like, but keep in mind that if a host call takes too long or fails, it'll cause the original
//! guest call to also fail.
//!
//! In summary, keep `host_callback` functions fast and resilient, and do not spawn new threads
//! within `host_callback` unless you must (and can synchronize memory access) because waPC
//! assumes a single-threaded execution environment. The `host_callback` function intentionally
//! has no references to the WebAssembly module bytes or the running instance.

#[macro_use]
extern crate log;

mod callbacks;
pub mod errors;
pub mod prelude;

/// A result type for errors that occur within the wapc library
pub type Result<T> = std::result::Result<T, errors::Error>;

use std::fs::File;
use std::sync::atomic::{AtomicU64, Ordering};
use wasmtime::Func;
use wasmtime::Instance;

use std::cell::RefCell;

use crate::callbacks::Callback;
use crate::callbacks::ModuleState;
use std::rc::Rc;
use wasmtime::*;

macro_rules!  call {
    ($func:expr, $($p:expr),*) => {
      match $func.borrow().call(&[$($p.into()),*]) {
        Ok(result) => {
          let result: i32 = result[0].i32().unwrap();
          result
        }
        Err(e) => {
            error!("Failure invoking guest module handler: {:?}", e);
            0
        }
      }
    }
  }

static GLOBAL_MODULE_COUNT: AtomicU64 = AtomicU64::new(1);

const HOST_NAMESPACE: &str = "wapc";

// -- Functions called by guest, exported by host
const HOST_CONSOLE_LOG: &str = "__console_log";
const HOST_CALL: &str = "__host_call";
const GUEST_REQUEST_FN: &str = "__guest_request";
const HOST_RESPONSE_FN: &str = "__host_response";
const HOST_RESPONSE_LEN_FN: &str = "__host_response_len";
const GUEST_RESPONSE_FN: &str = "__guest_response";
const GUEST_ERROR_FN: &str = "__guest_error";
const HOST_ERROR_FN: &str = "__host_error";
const HOST_ERROR_LEN_FN: &str = "__host_error_len";

// -- Functions called by host, exported by guest
const GUEST_CALL: &str = "__guest_call";

type HostCallback = dyn Fn(u64, &str, &str, &[u8]) -> std::result::Result<Vec<u8>, Box<dyn std::error::Error>>
    + Sync
    + Send
    + 'static;

#[derive(Debug, Clone)]
struct Invocation {
    operation: String,
    msg: Vec<u8>,
}

impl Invocation {
    fn new(op: &str, msg: Vec<u8>) -> Invocation {
        Invocation {
            operation: op.to_string(),
            msg,
        }
    }
}

/// Stores the parameters required to create a WASI instance
#[derive(Debug)]
pub struct WasiParams {
    argv: Vec<String>,
    environment: Vec<(String, String)>,
    preopened_dirs: Vec<(String, File)>,
}

impl WasiParams {
    pub fn new(
        argv: Vec<String>,
        environment: Vec<(String, String)>,
        preopened_dirs: Vec<(String, File)>,
    ) -> Self {
        WasiParams {
            argv,
            environment,
            preopened_dirs,
        }
    }
}

/// A WebAssembly host runtime for waPC-compliant WebAssembly modules
///
/// Use an instance of this struct to provide a means of invoking procedure calls by
/// specifying an operation name and a set of bytes representing the opaque operation payload.
/// `WapcHost` makes no assumptions about the contents or format of either the payload or the
/// operation name.
pub struct WapcHost {
    state: Rc<RefCell<ModuleState>>,
    instance: Rc<RefCell<Option<Instance>>>,
    wasidata: Option<WasiParams>,
}

impl WapcHost {
    /// Creates a new instance of a waPC-compliant WebAssembly host runtime. The resulting WebAssembly
    /// module instance will _not_ be allowed to utilize WASI host functions.
    pub fn new<F>(host_callback: F, buf: &[u8], wasi: Option<WasiParams>) -> Result<Self>
    where
        F: Fn(u64, &str, &str, &[u8]) -> std::result::Result<Vec<u8>, Box<dyn std::error::Error>>
            + Sync
            + Send
            + 'static,
    {
        let id = GLOBAL_MODULE_COUNT.fetch_add(1, Ordering::SeqCst);
        let state = Rc::new(RefCell::new(ModuleState::new(id, Box::new(host_callback))));
        let instance_ref = Rc::new(RefCell::new(None));
        let instance =
            WapcHost::instance_from_buffer(buf, &wasi, instance_ref.clone(), state.clone());
        instance_ref.replace(Some(instance));
        if wasi.is_some() {
            error!("NOTE - WASI support is not yet enabled, but will be soon.");
        }
        let mh = WapcHost {
            state,
            instance: instance_ref,
            wasidata: wasi,
        };
        Ok(mh)
    }

    /// Returns a reference to the unique identifier of this module. If a parent process
    /// has instantiated multiple `WapcHost`s, then the single static host call function
    /// will be required to differentiate between modules. Use the unique ID as a differentiator
    pub fn id(&self) -> u64 {
        self.state.borrow().id
    }

    /// Invokes the `__guest_call` function within the guest module as per the waPC specification.
    /// Provide an operation name and an opaque payload of bytes and the function returns a `Result`
    /// containing either an error or an opaque reply of bytes.    
    ///
    /// It is worth noting that the _first_ time `call` is invoked, the WebAssembly module
    /// will be JIT-compiled. This can take up to a few seconds on debug .wasm files, but
    /// all subsequent calls will be "hot" and run at near-native speeds.    
    pub fn call(&mut self, op: &str, payload: &[u8]) -> Result<Vec<u8>> {
        let inv = Invocation::new(op, payload.to_vec());

        {
            let mut state = self.state.borrow_mut();
            state.guest_response = None;
            state.guest_request = Some((inv).clone());
            state.guest_error = None;
        }

        let callresult: i32  = call!(
            self.guest_call_fn()?,
            inv.operation.len() as i32,
            inv.msg.len() as i32
        );

        if callresult == 0 {
            // invocation failed
            match self.state.borrow().guest_error {
                Some(ref s) => Err(errors::new(errors::ErrorKind::GuestCallFailure(s.clone()))),
                None => Err(errors::new(errors::ErrorKind::GuestCallFailure(
                    "No error message set for call failure".to_string(),
                ))),
            }
        } else {
            // invocation succeeded
            match self.state.borrow().guest_response {
                Some(ref e) => Ok(e.clone()),
                None => match self.state.borrow().guest_error {
                    Some(ref s) => Err(errors::new(errors::ErrorKind::GuestCallFailure(s.clone()))),
                    None => Err(errors::new(errors::ErrorKind::GuestCallFailure(
                        "No error message OR response set for call success".to_string(),
                    ))),
                },
            }
        }
    }

    /// Performs a live "hot swap" of the WebAssembly module. Since execution is assumed to be
    /// single-threaded within the environment of the `WapcHost`, this will not cause any pending function
    /// calls to be lost. This will replace the currently executing WebAssembly module with the new
    /// bytes.
    ///
    /// **Note**: you will lose all JITted functions for this module, so the first `call` after a
    /// hot swap will be "cold" and take longer than regular calls. There are an enormous number of
    /// ways in which a hot swap could go horribly wrong, so please ensure you have the proper guards
    /// in place before invoking it. Libraries that build upon this one can (and likely should) implement
    /// some form of security to protect against malicious swaps.
    ///
    /// If you perform a hot swap of a WASI module, you cannot alter the parameters used to create the WASI module
    /// like the environment variables, mapped directories, pre-opened files, etc. Not abiding by this could lead
    /// to privilege escalation attacks or non-deterministic behavior after the swap.
    pub fn replace_module(&self, module: &[u8]) -> Result<()> {
        info!(
            "HOT SWAP - Replacing existing WebAssembly module with new buffer, {} bytes",
            module.len()
        );
        let state = self.state.clone();
        let new_instance =
            WapcHost::instance_from_buffer(module, &self.wasidata, self.instance.clone(), state);
        self.instance.borrow_mut().replace(new_instance);
        Ok(())
    }

    fn instance_from_buffer(
        buf: &[u8],
        _wasi: &Option<WasiParams>,
        instance_ref: Rc<RefCell<Option<Instance>>>,
        state: Rc<RefCell<ModuleState>>,
    ) -> Instance {
        let engine = Engine::default();
        let store = Store::new(&engine);
        let module = Module::new(&store, buf).unwrap();

        let imports = arrange_imports(&module, state.clone(), instance_ref.clone(), store.clone());

        wasmtime::Instance::new( &module, imports.as_slice()).unwrap()
    }

    // TODO: make this cacheable
    fn guest_call_fn(&self) -> Result<HostRef<Func>> {
        if let Some(ext) = self
            .instance
            .borrow()
            .as_ref()
            .unwrap()
            .get_export(GUEST_CALL)
        {
            Ok(HostRef::new(ext.func().unwrap().clone()))
        } else {
            Err(errors::new(errors::ErrorKind::GuestCallFailure(
                "Guest module did not export __guest_call function!".to_string(),
            )))
        }
    }
}

/// wasmtime requires that the list of callbacks be "zippable" with the list
/// of module imports. In order to ensure that both lists are in the same
/// order, we have to loop through the module imports and instantiate the
/// corresponding callback. We **cannot** rely on a predictable import order
/// in the wasm module
fn arrange_imports(
    module: &Module,
    state: Rc<RefCell<ModuleState>>,
    instance: Rc<RefCell<Option<Instance>>>,
    store: Store,
) -> Vec<Extern> {
    module
        .imports()
        .iter()
        .filter_map(|imp| {
            if let ExternType::Func(_) = imp.ty() {
                if imp.module() == HOST_NAMESPACE {
                    Some(callback_for_import(
                        imp.name(),
                        state.clone(),
                        instance.clone(),
                        store.clone(),
                    ))
                } else {
                    None
                }
            } else {
                None
            }
        })
        .collect()
}

fn callback_for_import(
    import:  &str,
    state: Rc<RefCell<ModuleState>>,
    instance_ref: Rc<RefCell<Option<Instance>>>,
    store: Store,
) -> Extern {
    match import {
        HOST_CONSOLE_LOG => {
            callbacks::ConsoleLog::as_func(state.clone(), instance_ref.clone(), store).into()
        }
        HOST_CALL => {
            callbacks::HostCall::as_func(state.clone(), instance_ref.clone(), store).into()
        }
        GUEST_REQUEST_FN => {
            callbacks::GuestRequest::as_func(state.clone(), instance_ref.clone(), store).into()
        }
        HOST_RESPONSE_FN => {
            callbacks::HostResponse::as_func(state.clone(), instance_ref.clone(), store).into()
        }
        HOST_RESPONSE_LEN_FN => {
            callbacks::HostResponseLen::as_func(state.clone(), instance_ref.clone(), store).into()
        }
        GUEST_RESPONSE_FN => {
            callbacks::GuestResponse::as_func(state.clone(), instance_ref.clone(), store).into()
        }
        GUEST_ERROR_FN => {
            callbacks::GuestError::as_func(state.clone(), instance_ref.clone(), store).into()
        }
        HOST_ERROR_FN => {
            callbacks::HostError::as_func(state.clone(), instance_ref.clone(), store).into()
        }
        HOST_ERROR_LEN_FN => {
            callbacks::HostErrorLen::as_func(state.clone(), instance_ref.clone(), store).into()
        }
        _ => unreachable!(),
    }
}