-
Notifications
You must be signed in to change notification settings - Fork 1k
Expand file tree
/
Copy pathmod.rs
More file actions
355 lines (308 loc) · 12.8 KB
/
Copy pathmod.rs
File metadata and controls
355 lines (308 loc) · 12.8 KB
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
use self::wasm_instance_env::WasmInstanceEnv;
use super::wasm_common::module_host_actor::{InitializationError, WasmModuleHostActor, WasmModuleInstance};
use super::wasm_common::{abi, ModuleCreationError};
use crate::config::WasmConfig;
use crate::energy::{EnergyQuanta, FunctionBudget};
use crate::error::NodesError;
use crate::module_host_context::ModuleCreationContext;
use crate::util::jobs::AllocatedJobCore;
use anyhow::Context;
use spacetimedb_paths::server::ServerDataDir;
use std::borrow::Cow;
use std::time::Duration;
pub(in crate::host) use wasm_instance_env::WasmMemoryBytesMetric;
use wasmtime::{self, Engine, Linker, StoreContext, StoreContextMut};
pub use wasmtime_module::{WasmtimeInstance, WasmtimeModule};
#[cfg(unix)]
mod pooling_stack_creator;
mod wasm_instance_env;
mod wasmtime_module;
pub struct WasmtimeRuntime {
engine: Engine,
linker: Box<Linker<WasmInstanceEnv>>,
config: WasmConfig,
}
const EPOCH_TICK_LENGTH: Duration = Duration::from_millis(10);
pub(crate) const EPOCH_TICKS_PER_SECOND: u64 = ticks_in_duration(Duration::from_secs(1));
pub(crate) const fn ticks_in_duration(duration: Duration) -> u64 {
duration.div_duration_f64(EPOCH_TICK_LENGTH) as u64
}
pub(crate) fn epoch_ticker(mut on_tick: impl 'static + Send + FnMut() -> Option<()>) {
tokio::spawn(async move {
let mut interval = tokio::time::interval(EPOCH_TICK_LENGTH);
loop {
interval.tick().await;
let Some(()) = on_tick() else {
return;
};
}
});
}
impl WasmtimeRuntime {
pub fn new(data_dir: Option<&ServerDataDir>, runtime_config: WasmConfig) -> Self {
let mut config = wasmtime::Config::new();
config
.cranelift_opt_level(wasmtime::OptLevel::Speed)
.consume_fuel(true)
.epoch_interruption(true)
.wasm_backtrace_details(wasmtime::WasmBacktraceDetails::Enable)
// We need async support to enable suspending execution of procedures
// when waiting for e.g. HTTP responses or the transaction lock.
// We don't enable either fuel-based or epoch-based yielding
// (see https://docs.wasmtime.dev/api/wasmtime/struct.Store.html#method.epoch_deadline_async_yield_and_update
// and https://docs.wasmtime.dev/api/wasmtime/struct.Store.html#method.fuel_async_yield_interval)
// so reducers will always execute to completion during the first `Future::poll` call,
// and procedures will only yield when performing an asynchronous operation.
// These futures are executed on a separate single-threaded executor not related to the "global" Tokio runtime,
// which is responsible only for executing WASM. See `crate::util::jobs` for this infrastructure.
.async_support(true);
#[cfg(unix)]
config
.async_stack_size(self::pooling_stack_creator::ASYNC_STACK_SIZE)
.with_host_stack(self::pooling_stack_creator::PoolingStackCreator::new());
// Offer a compile-time flag for enabling perfmap generation,
// so `perf` can display JITted symbol names.
// Ideally we would be able to configure this at runtime via a flag to `spacetime start`,
// but this is good enough for now.
#[cfg(feature = "perfmap")]
config.profiler(wasmtime::ProfilingStrategy::PerfMap);
if let Some(data_dir) = data_dir {
let mut cache_config = wasmtime::CacheConfig::new();
cache_config.with_directory(data_dir.wasmtime_cache().0);
match wasmtime::Cache::new(cache_config) {
Ok(cache) => {
config.cache(Some(cache));
}
Err(e) => {
// caching is just an optimization, so if it fails, just log and continue
tracing::warn!("failed to set up wasmtime cache: {e:#}")
}
}
}
let engine = Engine::new(&config).unwrap();
let weak_engine = engine.weak();
epoch_ticker(move || {
let engine = weak_engine.upgrade()?;
engine.increment_epoch();
Some(())
});
let mut linker = Box::new(Linker::new(&engine));
WasmtimeModule::link_imports(&mut linker).unwrap();
let config = runtime_config;
WasmtimeRuntime { engine, linker, config }
}
}
pub type Module = WasmModuleHostActor<WasmtimeModule>;
pub type ModuleInstance = WasmModuleInstance<WasmtimeInstance>;
const THREAD_NAME_DATABASE_ID_SUFFIX_LEN: usize = 8;
fn wasm_executor_thread_name(database_identity: &spacetimedb_lib::Identity) -> String {
let hex = database_identity.to_hex();
// We use the tail of the identity to avoid the common structured prefix.
let suffix = &hex.as_str()[hex.as_str().len() - THREAD_NAME_DATABASE_ID_SUFFIX_LEN..];
format!("wasm-{suffix}")
}
impl WasmtimeRuntime {
pub fn make_actor(
&self,
mcc: ModuleCreationContext,
program_bytes: &[u8],
core: AllocatedJobCore,
) -> anyhow::Result<super::module_host::ModuleWithInstance> {
let module =
wasmtime::Module::new(&self.engine, program_bytes).map_err(ModuleCreationError::WasmCompileError)?;
let func_imports = module
.imports()
.filter(|imp| matches!(imp.ty(), wasmtime::ExternType::Func(_)));
let abi = abi::determine_spacetime_abi(func_imports, |imp| imp.module())?;
abi::verify_supported(WasmtimeModule::IMPLEMENTED_ABI, abi)?;
let module = self
.linker
.instantiate_pre(&module)
.map_err(InitializationError::Instantiation)?;
let module = WasmtimeModule::new(module);
let executor_thread_name = wasm_executor_thread_name(&mcc.replica_ctx.database_identity);
let (module, init_inst) = WasmModuleHostActor::new(mcc, module)?;
Ok(super::module_host::ModuleWithInstance::Wasm {
module,
executor: core.spawn_named_async_executor(executor_thread_name),
init_inst: Box::new(init_inst),
procedure_instance_pool_size: self.config.procedure_instance_pool_size,
})
}
}
#[derive(Debug, derive_more::From)]
pub enum WasmError {
Db(NodesError),
BufferTooSmall,
Wasm(anyhow::Error),
}
#[derive(Copy, Clone)]
struct WasmtimeFuel(u64);
impl WasmtimeFuel {}
impl From<FunctionBudget> for WasmtimeFuel {
fn from(v: FunctionBudget) -> Self {
// ReducerBudget being u64 is load-bearing here - if it was u128 and v was ReducerBudget::MAX,
// truncating this result would mean that with set_store_fuel(budget.into()), get_store_fuel()
// would be wildly different than the original `budget`, and the energy usage for the reducer
// would be u64::MAX even if it did nothing. ask how I know.
WasmtimeFuel(v.get())
}
}
impl From<WasmtimeFuel> for FunctionBudget {
fn from(v: WasmtimeFuel) -> Self {
FunctionBudget::new(v.0)
}
}
impl From<WasmtimeFuel> for EnergyQuanta {
fn from(fuel: WasmtimeFuel) -> Self {
EnergyQuanta::new(u128::from(fuel.0))
}
}
pub trait WasmPointee {
type Pointer;
fn write_to(self, mem: &mut MemView, ptr: Self::Pointer) -> Result<(), MemError>;
fn read_from(mem: &mut MemView, ptr: Self::Pointer) -> Result<Self, MemError>
where
Self: Sized;
}
macro_rules! impl_pointee {
($($t:ty),*) => {
$(impl WasmPointee for $t {
type Pointer = u32;
fn write_to(self, mem: &mut MemView, ptr: Self::Pointer) -> Result<(), MemError> {
let bytes = self.to_le_bytes();
mem.deref_slice_mut(ptr, bytes.len() as u32)?.copy_from_slice(&bytes);
Ok(())
}
fn read_from(mem: &mut MemView, ptr: Self::Pointer) -> Result<Self, MemError> {
Ok(Self::from_le_bytes(*mem.deref_array(ptr)?))
}
})*
};
}
impl_pointee!(u8, u16, u32, u64);
impl_pointee!(super::wasm_common::RowIterIdx);
impl WasmPointee for spacetimedb_lib::Identity {
type Pointer = u32;
fn write_to(self, mem: &mut MemView, ptr: Self::Pointer) -> Result<(), MemError> {
let bytes = self.to_byte_array();
mem.deref_slice_mut(ptr, bytes.len() as u32)?.copy_from_slice(&bytes);
Ok(())
}
fn read_from(mem: &mut MemView, ptr: Self::Pointer) -> Result<Self, MemError> {
Ok(Self::from_byte_array(*mem.deref_array(ptr)?))
}
}
impl WasmPointee for spacetimedb_lib::ConnectionId {
type Pointer = u32;
fn write_to(self, mem: &mut MemView, ptr: Self::Pointer) -> Result<(), MemError> {
let bytes = self.as_le_byte_array();
mem.deref_slice_mut(ptr, bytes.len() as u32)?.copy_from_slice(&bytes);
Ok(())
}
fn read_from(mem: &mut MemView, ptr: Self::Pointer) -> Result<Self, MemError> {
Ok(Self::from_le_byte_array(*mem.deref_array(ptr)?))
}
}
type WasmPtr<T> = <T as WasmPointee>::Pointer;
/// Wraps access to WASM linear memory with some additional functionality.
#[derive(Clone, Copy)]
pub struct Mem {
/// The underlying WASM `memory` instance.
pub memory: wasmtime::Memory,
}
impl Mem {
/// Constructs an instance of `Mem` from an exports map.
pub fn extract(exports: &wasmtime::Instance, store: impl wasmtime::AsContextMut) -> anyhow::Result<Self> {
Ok(Self {
memory: exports.get_memory(store, "memory").context("no memory export")?,
})
}
/// Creates and returns a view into the actual memory `store`.
/// This view allows for reads and writes.
pub fn view_and_store_mut<'a, T: 'static>(
&self,
store: impl Into<StoreContextMut<'a, T>>,
) -> (&'a mut MemView, &'a mut T) {
let (mem, store_data) = self.memory.data_and_store_mut(store);
(MemView::from_slice_mut(mem), store_data)
}
fn view<'a, T: 'static>(&self, store: impl Into<StoreContext<'a, T>>) -> &'a MemView {
MemView::from_slice(self.memory.data(store))
}
}
#[repr(transparent)]
pub struct MemView([u8]);
impl MemView {
fn from_slice_mut(v: &mut [u8]) -> &mut Self {
// SAFETY: MemView is repr(transparent) over [u8]
unsafe { &mut *(v as *mut [u8] as *mut MemView) }
}
fn from_slice(v: &[u8]) -> &Self {
// SAFETY: MemView is repr(transparent) over [u8]
unsafe { &*(v as *const [u8] as *const MemView) }
}
/// Get a byte slice of wasm memory given a pointer and a length.
pub fn deref_slice(&self, offset: WasmPtr<u8>, len: u32) -> Result<&[u8], MemError> {
if offset == 0 {
return Err(MemError::Null);
}
self.0
.get(offset as usize..)
.and_then(|s| s.get(..len as usize))
.ok_or(MemError::OutOfBounds)
}
/// Get a utf8 slice of wasm memory given a pointer and a length.
fn deref_str(&self, offset: WasmPtr<u8>, len: u32) -> Result<&str, MemError> {
let b = self.deref_slice(offset, len)?;
std::str::from_utf8(b).map_err(MemError::Utf8)
}
/// Lossily get a utf8 slice of wasm memory given a pointer and a length, converting any
/// non-utf8 bytes to `U+FFFD REPLACEMENT CHARACTER`.
fn deref_str_lossy(&self, offset: WasmPtr<u8>, len: u32) -> Result<Cow<'_, str>, MemError> {
self.deref_slice(offset, len).map(String::from_utf8_lossy)
}
/// Get a mutable byte slice of wasm memory given a pointer and a length;
fn deref_slice_mut(&mut self, offset: WasmPtr<u8>, len: u32) -> Result<&mut [u8], MemError> {
if offset == 0 {
return Err(MemError::Null);
}
self.0
.get_mut(offset as usize..)
.and_then(|s| s.get_mut(..len as usize))
.ok_or(MemError::OutOfBounds)
}
/// Get a byte array of wasm memory the size of `N`.
fn deref_array<const N: usize>(&self, offset: WasmPtr<u8>) -> Result<&[u8; N], MemError> {
Ok(self.deref_slice(offset, N as u32)?.try_into().unwrap())
}
}
/// An error that can result from operations on [`MemView`].
#[derive(thiserror::Error, Debug)]
pub enum MemError {
#[error("out of bounds pointer passed to a spacetime function")]
OutOfBounds,
#[error("null pointer passed to a spacetime function")]
Null,
#[error("invalid utf8 passed to a spacetime function")]
Utf8(#[from] std::str::Utf8Error),
}
impl From<MemError> for WasmError {
fn from(err: MemError) -> Self {
WasmError::Wasm(err.into())
}
}
/// Extension trait to gracefully handle null `WasmPtr`s, e.g.
/// `mem.deref_slice(ptr, len).check_nullptr()? == Option<&[u8]>`.
trait NullableMemOp<T> {
fn check_nullptr(self) -> Result<Option<T>, MemError>;
}
impl<T> NullableMemOp<T> for Result<T, MemError> {
fn check_nullptr(self) -> Result<Option<T>, MemError> {
match self {
Ok(x) => Ok(Some(x)),
Err(MemError::Null) => Ok(None),
Err(e) => Err(e),
}
}
}