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
use crate::errors::Result;
use crate::WasmtimeEngineProvider;
/// Used to build [`WasmtimeEngineProvider`](crate::WasmtimeEngineProvider) instances.
#[allow(missing_debug_implementations)]
#[derive(Default)]
pub struct WasmtimeEngineProviderBuilder<'a> {
engine: Option<wasmtime::Engine>,
module_bytes: &'a [u8],
#[cfg(feature = "cache")]
cache_enabled: bool,
#[cfg(feature = "cache")]
cache_path: Option<std::path::PathBuf>,
wasi_params: Option<wapc::WasiParams>,
epoch_deadlines: Option<crate::EpochDeadlines>,
}
#[allow(deprecated)]
impl<'a> WasmtimeEngineProviderBuilder<'a> {
/// A new WasmtimeEngineProviderBuilder instance,
/// must provide the wasm module to be loaded
#[must_use]
pub fn new(module_bytes: &'a [u8]) -> Self {
WasmtimeEngineProviderBuilder {
module_bytes,
..Default::default()
}
}
/// Provide a preinitialized [`wasmtime::Engine`]
///
/// **Warning:** when used, engine specific options like
/// [`cache`](WasmtimeEngineProviderBuilder::enable_cache) and
/// [`enable_epoch_interruptions`](WasmtimeEngineProviderBuilder::enable_epoch_interruptions)
/// must be pre-configured by the user. `WasmtimeEngineProviderBuilder` won't be
/// able to configure them at [`build`](WasmtimeEngineProviderBuilder::build) time.
#[must_use]
pub fn engine(mut self, engine: wasmtime::Engine) -> Self {
self.engine = Some(engine);
self
}
/// WASI params
#[must_use]
pub fn wasi_params(mut self, wasi: wapc::WasiParams) -> Self {
self.wasi_params = Some(wasi);
self
}
/// Enable Wasmtime cache feature
///
/// **Warning:** this has no effect when a custom [`wasmtime::Engine`] is provided via
/// the [`WasmtimeEngineProviderBuilder::engine`] helper. In that case, it's up to the
/// user to provide a [`wasmtime::Engine`] instance with the cache values properly configured.
#[cfg(feature = "cache")]
#[must_use]
pub fn enable_cache(mut self, path: Option<&std::path::Path>) -> Self {
self.cache_enabled = true;
self.cache_path = path.map(|p| p.to_path_buf());
self
}
/// Enable Wasmtime [epoch-based interruptions](wasmtime::Config::epoch_interruption) and set
/// the deadlines to be enforced
///
/// Two kind of deadlines have to be set:
///
/// * `wapc_init_deadline`: the number of ticks the waPC initialization code can take before the
/// code is interrupted. This is the code usually defined inside of the `wapc_init`/`_start`
/// functions
/// * `wapc_func_deadline`: the number of ticks any regular waPC guest function can run before
/// its terminated by the host
///
/// Both these limits are expressed using the number of ticks that are allowed before the
/// WebAssembly execution is interrupted.
/// It's up to the embedder of waPC to define how much time a single tick is granted. This could
/// be 1 second, 10 nanoseconds, or whatever the user prefers.
///
/// **Warning:** when providing an instance of `wasmtime::Engine` via the
/// `WasmtimeEngineProvider::engine` helper, ensure the `wasmtime::Engine`
/// has been created with the `epoch_interruption` feature enabled
#[must_use]
pub fn enable_epoch_interruptions(mut self, wapc_init_deadline: u64, wapc_func_deadline: u64) -> Self {
self.epoch_deadlines = Some(crate::EpochDeadlines {
wapc_init: wapc_init_deadline,
wapc_func: wapc_func_deadline,
});
self
}
/// Create a `WasmtimeEngineProvider` instance
pub fn build(&self) -> Result<WasmtimeEngineProvider> {
let mut provider = match &self.engine {
Some(e) => {
// note: we have to call `.clone()` because `e` is behind
// a shared reference and `Engine` does not implement `Copy`.
// However, cloning an `Engine` is a cheap operation because
// under the hood wasmtime does not create a new `Engine`, but
// rather creates a new reference to it.
// See https://docs.rs/wasmtime/latest/wasmtime/struct.Engine.html#engines-and-clone
WasmtimeEngineProvider::new_with_engine(self.module_bytes, e.clone(), self.wasi_params.clone())
}
None => {
let mut config = wasmtime::Config::default();
if self.epoch_deadlines.is_some() {
config.epoch_interruption(true);
}
cfg_if::cfg_if! {
if #[cfg(feature = "cache")] {
if self.cache_enabled {
config.strategy(wasmtime::Strategy::Cranelift);
if let Some(cache) = &self.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 = wasmtime::Engine::new(&config)?;
WasmtimeEngineProvider::new_with_engine(self.module_bytes, engine, self.wasi_params.clone())
}
}?;
provider.epoch_deadlines = self.epoch_deadlines;
Ok(provider)
}
}