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
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
#![deny(
  clippy::expect_used,
  clippy::explicit_deref_methods,
  clippy::option_if_let_else,
  clippy::await_holding_lock,
  clippy::cloned_instead_of_copied,
  clippy::explicit_into_iter_loop,
  clippy::flat_map_option,
  clippy::fn_params_excessive_bools,
  clippy::implicit_clone,
  clippy::inefficient_to_string,
  clippy::large_types_passed_by_value,
  clippy::manual_ok_or,
  clippy::map_flatten,
  clippy::map_unwrap_or,
  clippy::must_use_candidate,
  clippy::needless_for_each,
  clippy::needless_pass_by_value,
  clippy::option_option,
  clippy::redundant_else,
  clippy::semicolon_if_nothing_returned,
  clippy::too_many_lines,
  clippy::trivially_copy_pass_by_ref,
  clippy::unnested_or_patterns,
  clippy::future_not_send,
  clippy::useless_let_if_seq,
  clippy::str_to_string,
  clippy::inherent_to_string,
  clippy::let_and_return,
  clippy::string_to_string,
  clippy::try_err,
  clippy::unused_async,
  clippy::missing_enforced_import_renames,
  clippy::nonstandard_macro_braces,
  clippy::rc_mutex,
  clippy::unwrap_or_else_default,
  clippy::manual_split_once,
  clippy::derivable_impls,
  clippy::needless_option_as_deref,
  clippy::iter_not_returning_iterator,
  clippy::same_name_method,
  clippy::manual_assert,
  clippy::non_send_fields_in_send_ty,
  clippy::equatable_if_let,
  bad_style,
  clashing_extern_declarations,
  const_err,
  dead_code,
  deprecated,
  explicit_outlives_requirements,
  improper_ctypes,
  invalid_value,
  missing_copy_implementations,
  missing_debug_implementations,
  mutable_transmutes,
  no_mangle_generic_items,
  non_shorthand_field_patterns,
  overflowing_literals,
  path_statements,
  patterns_in_fns_without_body,
  private_in_public,
  trivial_bounds,
  trivial_casts,
  trivial_numeric_casts,
  type_alias_bounds,
  unconditional_recursion,
  unreachable_pub,
  unsafe_code,
  unstable_features,
  unused,
  unused_allocation,
  unused_comparisons,
  unused_import_braces,
  unused_parens,
  unused_qualifications,
  while_true,
  missing_docs
)]
#![doc = include_str!("../README.md")]

mod callbacks;
#[cfg(feature = "wasi")]
mod wasi;

/// The crate's error module
pub mod errors;

use parking_lot::RwLock;
use wapc::{wapc_functions, ModuleState, WasiParams, WebAssemblyEngineProvider, HOST_NAMESPACE};
// export wasmtime and wasmtime_wasi, so that consumers of this crate can use
// the very same version
pub use wasmtime;
use wasmtime::{AsContextMut, Engine, Extern, ExternType, Instance, Linker, Module, Store, TypedFunc};
#[cfg(feature = "wasi")]
pub use wasmtime_wasi;
use wasmtime_wasi::WasiCtx;

// namespace needed for some language support
const WASI_UNSTABLE_NAMESPACE: &str = "wasi_unstable";
const WASI_SNAPSHOT_PREVIEW1_NAMESPACE: &str = "wasi_snapshot_preview1";

use std::sync::Arc;

#[macro_use]
extern crate log;

type Result<T> = std::result::Result<T, errors::Error>;

struct EngineInner {
  instance: Arc<RwLock<Instance>>,
  guest_call_fn: TypedFunc<(i32, i32), i32>,
  host: Arc<ModuleState>,
}

struct WapcStore {
  #[cfg(feature = "wasi")]
  wasi_ctx: WasiCtx,
}

/// A waPC engine provider that encapsulates the Wasmtime WebAssembly runtime
#[allow(missing_debug_implementations)]
pub struct WasmtimeEngineProvider {
  module: Module,
  #[cfg(feature = "wasi")]
  wasi_params: WasiParams,
  inner: Option<EngineInner>,
  store: Store<WapcStore>,
  engine: Engine,
  linker: Linker<WapcStore>,
}

impl Clone for WasmtimeEngineProvider {
  fn clone(&self) -> Self {
    let engine = self.engine.clone();
    cfg_if::cfg_if! {
      if #[cfg(feature = "wasi")] {
        let wasi_ctx = init_wasi(&self.wasi_params).unwrap();
        let store = Store::new(&engine, WapcStore { wasi_ctx });
      } else {
        let store = Store::new(&engine, WapcStore {});
      }
    };

    match &self.inner {
      Some(state) => {
        let mut new = Self {
          module: self.module.clone(),
          inner: None,
          store,
          engine,
          linker: self.linker.clone(),
          #[cfg(feature = "wasi")]
          wasi_params: self.wasi_params.clone(),
        };
        new.init(state.host.clone()).unwrap();
        new
      }
      None => Self {
        module: self.module.clone(),
        inner: None,
        store,
        engine,
        linker: self.linker.clone(),
        #[cfg(feature = "wasi")]
        wasi_params: self.wasi_params.clone(),
      },
    }
  }
}

impl WasmtimeEngineProvider {
  /// Creates a new instance of a [WasmtimeEngineProvider].
  pub fn new(buf: &[u8], wasi: Option<WasiParams>) -> Result<WasmtimeEngineProvider> {
    let engine = Engine::default();
    Self::new_with_engine(buf, engine, wasi)
  }

  #[cfg(feature = "cache")]
  /// Creates a new instance of a [WasmtimeEngineProvider] with caching enabled.
  pub fn new_with_cache(
    buf: &[u8],
    wasi: Option<WasiParams>,
    cache_path: Option<&std::path::Path>,
  ) -> Result<WasmtimeEngineProvider> {
    let mut config = wasmtime::Config::new();
    config.strategy(wasmtime::Strategy::Cranelift)?;
    if let Some(cache) = cache_path {
      config.cache_config_load(cache)?;
    } else if let Err(e) = config.cache_config_load_default() {
      warn!("Wasmtime cache configuration not found ({}). Repeated loads will speed up significantly with a cache configuration. See https://docs.wasmtime.dev/cli-cache.html for more information.",e);
    }
    let engine = Engine::new(&config)?;
    Self::new_with_engine(buf, engine, wasi)
  }

  /// Creates a new instance of a [WasmtimeEngineProvider] from a separately created [wasmtime::Engine].
  pub fn new_with_engine(buf: &[u8], engine: Engine, wasi: Option<WasiParams>) -> Result<Self> {
    let mut linker: Linker<WapcStore> = Linker::new(&engine);
    let module = Module::new(&engine, buf)?;

    cfg_if::cfg_if! {
      if #[cfg(feature = "wasi")] {
        wasmtime_wasi::add_to_linker(&mut linker, |s| &mut s.wasi_ctx).unwrap();
        let wasi_params = wasi.unwrap_or_default();
        let wasi_ctx = wasi::init_ctx(
            &wasi::compute_preopen_dirs(&wasi_params.preopened_dirs, &wasi_params.map_dirs)
                .unwrap(),
            &wasi_params.argv,
            &wasi_params.env_vars,
        )
        .unwrap();
        let store = Store::new(&engine, WapcStore { wasi_ctx });
      } else {
        let store = Store::new(&engine, WapcStore {});
      }
    };

    Ok(WasmtimeEngineProvider {
      module,
      #[cfg(feature = "wasi")]
      wasi_params,
      inner: None,
      store,
      engine,
      linker,
    })
  }
}

impl WebAssemblyEngineProvider for WasmtimeEngineProvider {
  fn init(
    &mut self,
    host: Arc<ModuleState>,
  ) -> std::result::Result<(), Box<(dyn std::error::Error + Send + Sync + 'static)>> {
    let instance = instance_from_module(&mut self.store, &self.module, &host, &self.linker)?;
    let instance_ref = Arc::new(RwLock::new(instance));
    let gc = guest_call_fn(self.store.as_context_mut(), &instance_ref)?;
    self.inner = Some(EngineInner {
      instance: instance_ref,
      guest_call_fn: gc,
      host,
    });
    self.initialize()?;
    Ok(())
  }

  fn call(
    &mut self,
    op_length: i32,
    msg_length: i32,
  ) -> std::result::Result<i32, Box<(dyn std::error::Error + Send + Sync + 'static)>> {
    let engine_inner = self.inner.as_ref().unwrap();
    let call = engine_inner
      .guest_call_fn
      .call(&mut self.store, (op_length, msg_length));

    match call {
      Ok(result) => Ok(result),
      Err(e) => {
        error!("Failure invoking guest module handler: {:?}", e);
        engine_inner.host.set_guest_error(e.to_string());
        Ok(0)
      }
    }
  }

  fn replace(
    &mut self,
    module: &[u8],
  ) -> std::result::Result<(), Box<(dyn std::error::Error + Send + Sync + 'static)>> {
    info!(
      "HOT SWAP - Replacing existing WebAssembly module with new buffer, {} bytes",
      module.len()
    );

    let new_instance = instance_from_buffer(
      &mut self.store,
      &self.engine,
      module,
      &self.inner.as_ref().unwrap().host,
      &self.linker,
    )?;
    *self.inner.as_ref().unwrap().instance.write() = new_instance;

    Ok(self.initialize()?)
  }
}

impl WasmtimeEngineProvider {
  fn initialize(&mut self) -> Result<()> {
    for starter in wapc::wapc_functions::REQUIRED_STARTS.iter() {
      if let Some(ext) = self
        .inner
        .as_ref()
        .unwrap()
        .instance
        .read()
        .get_export(&mut self.store, starter)
      {
        ext.into_func().unwrap().call(&mut self.store, &[], &mut [])?;
      }
    }
    Ok(())
  }
}

fn instance_from_buffer(
  store: &mut Store<WapcStore>,
  engine: &Engine,
  buf: &[u8],
  state: &Arc<ModuleState>,
  linker: &Linker<WapcStore>,
) -> Result<Instance> {
  let module = Module::new(engine, buf).unwrap();
  let imports = arrange_imports(&module, state, store, linker);
  Ok(wasmtime::Instance::new(store.as_context_mut(), &module, imports?.as_slice()).unwrap())
}

fn instance_from_module(
  store: &mut Store<WapcStore>,
  module: &Module,
  state: &Arc<ModuleState>,
  linker: &Linker<WapcStore>,
) -> Result<Instance> {
  let imports = arrange_imports(module, state, store, linker);
  Ok(wasmtime::Instance::new(store.as_context_mut(), module, imports?.as_slice()).unwrap())
}

#[cfg(feature = "wasi")]
fn init_wasi(params: &WasiParams) -> Result<WasiCtx> {
  wasi::init_ctx(
    &wasi::compute_preopen_dirs(&params.preopened_dirs, &params.map_dirs).unwrap(),
    &params.argv,
    &params.env_vars,
  )
  .map_err(|e| errors::Error::InitializationFailed(e))
}

/// 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
#[allow(clippy::unnecessary_wraps)]
fn arrange_imports(
  module: &Module,
  host: &Arc<ModuleState>,
  store: &mut impl AsContextMut<Data = WapcStore>,
  linker: &Linker<WapcStore>,
) -> Result<Vec<Extern>> {
  Ok(
    module
      .imports()
      .filter_map(|imp| {
        if let ExternType::Func(_) = imp.ty() {
          match imp.module() {
            HOST_NAMESPACE => Some(callback_for_import(store.as_context_mut(), imp.name(), host.clone())),
            WASI_SNAPSHOT_PREVIEW1_NAMESPACE | WASI_UNSTABLE_NAMESPACE => {
              linker.get_by_import(store.as_context_mut(), &imp)
            }
            other => panic!("import module `{}` was not found", other), //TODO: get rid of panic
          }
        } else {
          None
        }
      })
      .collect(),
  )
}

fn callback_for_import(store: impl AsContextMut, import: &str, host: Arc<ModuleState>) -> Extern {
  match import {
    wapc_functions::HOST_CONSOLE_LOG => callbacks::console_log_func(store, host).into(),
    wapc_functions::HOST_CALL => callbacks::host_call_func(store, host).into(),
    wapc_functions::GUEST_REQUEST_FN => callbacks::guest_request_func(store, host).into(),
    wapc_functions::HOST_RESPONSE_FN => callbacks::host_response_func(store, host).into(),
    wapc_functions::HOST_RESPONSE_LEN_FN => callbacks::host_response_len_func(store, host).into(),
    wapc_functions::GUEST_RESPONSE_FN => callbacks::guest_response_func(store, host).into(),
    wapc_functions::GUEST_ERROR_FN => callbacks::guest_error_func(store, host).into(),
    wapc_functions::HOST_ERROR_FN => callbacks::host_error_func(store, host).into(),
    wapc_functions::HOST_ERROR_LEN_FN => callbacks::host_error_len_func(store, host).into(),
    _ => unreachable!(),
  }
}

// Called once, then the result is cached. This returns a `Func` that corresponds
// to the `__guest_call` export
fn guest_call_fn(store: impl AsContextMut, instance: &Arc<RwLock<Instance>>) -> Result<TypedFunc<(i32, i32), i32>> {
  instance
    .read()
    .get_typed_func::<(i32, i32), i32, _>(store, wapc_functions::GUEST_CALL)
    .map_err(|_| errors::Error::GuestCallNotFound)
}