tocat-plugins 0.2.0

The plugins compiled into tocat, a socat-inspired relay
Documentation
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
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
//! The wasmtime side: one engine, a compiled module per path, an instance per
//! stage.
//!
//! Compilation is the expensive part and instantiation is not, so they are
//! separated. A module is compiled once per process, on the first stage that
//! names it, and kept as an [`InstancePre`] with its imports already resolved.
//! Every stage after that is an instantiation, which is a fresh linear memory
//! and little else.
//!
//! That matters because a stage is per direction per connection: under `fork`
//! with `direction = "both"`, a hundred clients is two hundred instances of
//! the same compiled code. They share nothing mutable, which is the same
//! guarantee every other plugin gives and the reason none of this needs a lock
//! on the data path.

use std::{
    collections::HashMap,
    path::{Path, PathBuf},
    sync::{Mutex, OnceLock},
    time::Duration,
};

use tocat_api::{Boundaries, Needs, PluginError, Result};
use wasmtime::{
    Config, Engine, Instance, InstancePre, Linker, Memory, Module, Store, StoreLimits,
    StoreLimitsBuilder, TypedFunc, WasmParams, WasmResults,
};

use super::{
    NAME,
    abi::{self, ABI_VERSION, Outbox},
};

/// Per-store state. The limiter is what caps a guest's memory growth; without
/// it a guest could ask for as much as the platform allows, once per
/// connection.
pub struct HostState {
    limits: StoreLimits,
}

/// One process-wide engine, because a compiled module belongs to the engine
/// that produced it and sharing modules is the entire point of the cache.
fn engine() -> &'static Engine {
    static ENGINE: OnceLock<Engine> = OnceLock::new();

    ENGINE.get_or_init(|| {
        let mut config = Config::new();

        // Fuel is how a stage that loops forever becomes a failed path rather
        // than a hung relay: `on_bytes` runs on the copy task, and nothing
        // else on that task makes progress while a guest is inside it.
        // Metering costs a few percent and is on unconditionally, since the
        // engine is shared and the alternative is an engine per fuel setting.
        config.consume_fuel(true);

        Engine::new(&config).expect("wasmtime engine with default settings")
    })
}

type Cache = Mutex<HashMap<PathBuf, InstancePre<HostState>>>;

fn cache() -> &'static Cache {
    static CACHE: OnceLock<Cache> = OnceLock::new();
    CACHE.get_or_init(Cache::default)
}

/// Compile `path`, or hand back the compilation an earlier stage paid for.
///
/// The lock is held across compilation, so two stages naming the same new
/// module do not compile it twice. Startup builds every chain once before any
/// endpoint is opened, so this is paid there rather than on the first byte.
pub fn load(path: &Path) -> Result<InstancePre<HostState>> {
    let path = path
        .canonicalize()
        .map_err(|e| config_error(format!("{}: {e}", path.display())))?;

    let mut cache = cache()
        .lock()
        .unwrap_or_else(|poisoned| poisoned.into_inner());

    if let Some(pre) = cache.get(&path) {
        return Ok(pre.clone());
    }

    let module = Module::from_file(engine(), &path)
        .map_err(|e| config_error(format!("{}: {e}", path.display())))?;

    let pre = prepare(&module).map_err(|e| config_error(format!("{}: {e}", path.display())))?;

    cache.insert(path, pre.clone());

    Ok(pre)
}

/// [`load`] without the filesystem or the cache: same validation, same import
/// refusal, bytes instead of a path.
///
/// Only the tests want that, which is what the `cfg` says. It exists so they
/// can define guests as WAT inline rather than checking in binary fixtures,
/// and so that they still go through [`prepare`]: the test that a module
/// importing WASI is refused is testing the loader, and would test nothing if
/// it built its own `InstancePre`. Drop the `cfg` if something outside the
/// tests ever needs a module from memory.
#[cfg(test)]
pub fn compile(bytes: impl AsRef<[u8]>) -> Result<InstancePre<HostState>> {
    let module = Module::new(engine(), bytes).map_err(|e| config_error(e.to_string()))?;

    prepare(&module)
}

/// Validate a compiled module and resolve its imports, of which there are
/// none.
///
/// Saying so here turns "built against WASI" into a startup error that names
/// the import, rather than a trap on the first chunk, and makes the capability
/// boundary something the loader enforces rather than something the
/// documentation asks for.
fn prepare(module: &Module) -> Result<InstancePre<HostState>> {
    if let Some(import) = module.imports().next() {
        return Err(config_error(format!(
            "guest imports {}::{}, but tocat guests import nothing. Effects are \
             queued in the outbox and applied by the host, so a guest needs no \
             host functions and cannot be built against WASI",
            import.module(),
            import.name(),
        )));
    }

    Linker::new(engine())
        .instantiate_pre(module)
        .map_err(|e| config_error(e.to_string()))
}

/// One instantiated guest, and the exports worth looking up once.
pub struct Guest {
    store: Store<HostState>,
    memory: Memory,
    fuel: u64,

    alloc: TypedFunc<i32, i32>,
    outbox: TypedFunc<(), i32>,
    on_bytes: TypedFunc<(i32, i32), ()>,
    on_eof: Option<TypedFunc<(), ()>>,
    on_tick: Option<TypedFunc<(), ()>>,

    /// All read once, after `tocat_init`, because that is when the guest
    /// knows its options and because the host reads them once too.
    tick_interval: Option<Duration>,
    boundaries: Boundaries,
    needs: Needs,
}

impl Guest {
    /// Instantiate, check the ABI version, and hand the entry's `config` to
    /// `tocat_init` if the guest wants it.
    pub fn new(
        pre: &InstancePre<HostState>,
        memory_max: usize,
        fuel: u64,
        config: &[u8],
    ) -> Result<Self> {
        let state = HostState {
            limits: StoreLimitsBuilder::new().memory_size(memory_max).build(),
        };

        let mut store = Store::new(engine(), state);
        store.limiter(|state| &mut state.limits);
        set_fuel(&mut store, fuel)?;

        let instance = pre
            .instantiate(&mut store)
            .map_err(|e| config_error(format!("instantiating: {e}")))?;

        let memory = instance
            .get_memory(&mut store, "memory")
            .ok_or_else(|| config_error("guest exports no memory"))?;

        let version: TypedFunc<(), i32> = required(&instance, &mut store, "tocat_abi_version")?;
        let version = version
            .call(&mut store, ())
            .map_err(|e| config_error(format!("tocat_abi_version: {e}")))?;

        if version != ABI_VERSION {
            return Err(config_error(format!(
                "guest speaks ABI version {version}, this build speaks {ABI_VERSION}"
            )));
        }

        let mut guest = Self {
            memory,
            fuel,
            alloc: required(&instance, &mut store, "tocat_alloc")?,
            outbox: required(&instance, &mut store, "tocat_outbox")?,
            on_bytes: required(&instance, &mut store, "tocat_on_bytes")?,
            on_eof: optional(&instance, &mut store, "tocat_on_eof"),
            on_tick: optional(&instance, &mut store, "tocat_on_tick"),
            tick_interval: None,
            boundaries: Boundaries::Fuse,
            needs: Needs::Nothing,
            store,
        };

        // Config goes in through the same door as a chunk, since it is just
        // bytes the guest wants, and a guest with no options need not export
        // the entrypoint at all.
        if let Some(init) = optional::<(i32, i32), ()>(&instance, &mut guest.store, "tocat_init") {
            let ptr = guest.write(config)?;
            let len = config.len() as i32;

            set_fuel(&mut guest.store, fuel)?;
            init.call(&mut guest.store, (ptr, len))
                .map_err(|e| config_error(format!("tocat_init: {e}")))?;

            // A guest that rejects its options reports it the same way any
            // call does, so a bad option fails at startup carrying the guest's
            // own message rather than trapping later.
            let outbox = guest.outbox()?;
            if outbox.has(abi::FLAG_ERROR) {
                let message = abi::slice(guest.memory(), outbox.message.ptr, outbox.message.len)?;
                return Err(config_error(String::from_utf8_lossy(message).into_owned()));
            }
        }

        guest.tick_interval =
            optional::<(), i64>(&instance, &mut guest.store, "tocat_tick_interval_ns")
                .and_then(|func| func.call(&mut guest.store, ()).ok())
                .and_then(|nanos| u64::try_from(nanos).ok())
                .filter(|nanos| *nanos > 0)
                .map(Duration::from_nanos);

        // A guest that does not export it claims nothing, which is `Fuse` with
        // no requirement. One that exports it and answers with a bit this host
        // does not know was built against a later ABI: refusing beats reading
        // it as a claim of nothing, since the requirement the host cannot see
        // is exactly the one it would then fail to enforce.
        if let Some(func) = optional::<(), i32>(&instance, &mut guest.store, abi::BOUNDARIES) {
            let raw = func
                .call(&mut guest.store, ())
                .map_err(|e| config_error(format!("{}: {e}", abi::BOUNDARIES)))?;

            let (boundaries, needs) = u32::try_from(raw)
                .ok()
                .and_then(abi::unpack_boundaries)
                .ok_or_else(|| {
                    config_error(format!(
                        "{} returned {raw}, which this build does not understand: \
                         the guest was built against a later ABI",
                        abi::BOUNDARIES,
                    ))
                })?;

            guest.boundaries = boundaries;
            guest.needs = needs;
        }

        Ok(guest)
    }

    /// The period the guest asked for, or `None` if it wants no ticks. A guest
    /// that asks for one without exporting `tocat_on_tick` gets no timer,
    /// which is the reading that costs nothing.
    pub fn tick_interval(&self) -> Option<Duration> {
        self.tick_interval.filter(|_| self.on_tick.is_some())
    }

    /// What the guest claims to do to message boundaries.
    /// [`Boundaries::Fuse`] for a guest that does not say, which claims
    /// nothing and is the answer the trait defaults to.
    pub fn boundaries(&self) -> Boundaries {
        self.boundaries
    }

    /// What the guest needs of the path it was placed on.
    pub fn needs(&self) -> Needs {
        self.needs
    }

    pub fn on_bytes(&mut self, input: &[u8]) -> Result<()> {
        let ptr = self.write(input)?;
        let call = &self.on_bytes;

        set_fuel(&mut self.store, self.fuel)?;
        call.call(&mut self.store, (ptr, input.len() as i32))
            .map_err(|e| trap("tocat_on_bytes", &e))
    }

    pub fn on_eof(&mut self) -> Result<()> {
        let Some(call) = &self.on_eof else {
            return Ok(());
        };

        set_fuel(&mut self.store, self.fuel)?;
        call.call(&mut self.store, ())
            .map_err(|e| trap("tocat_on_eof", &e))
    }

    pub fn on_tick(&mut self) -> Result<()> {
        let Some(call) = &self.on_tick else {
            return Ok(());
        };

        set_fuel(&mut self.store, self.fuel)?;
        call.call(&mut self.store, ())
            .map_err(|e| trap("tocat_on_tick", &e))
    }

    /// The outbox left by the last call.
    pub fn outbox(&mut self) -> Result<Outbox> {
        let at = self
            .outbox
            .call(&mut self.store, ())
            .map_err(|e| trap("tocat_outbox", &e))?;

        Outbox::read(self.memory.data(&self.store), at as u32)
    }

    /// Guest memory, for reading the spans the outbox pointed at. Borrowed
    /// rather than copied, so the emission is built straight out of it.
    pub fn memory(&self) -> &[u8] {
        self.memory.data(&self.store)
    }

    /// Ask the guest where to put `bytes`, and put them there.
    ///
    /// `tocat_alloc` is an arena: the host never frees and a guest may hand
    /// back the same buffer every time, so this is a call, a bounds check and
    /// a copy rather than an allocation.
    ///
    /// The pointer it gives back is an address in its linear memory, not an
    /// offset into whatever it uses as an arena. That is the guest's most
    /// likely bug and the host cannot detect it: a wrong-but-valid address is
    /// still writable memory.
    fn write(&mut self, bytes: &[u8]) -> Result<i32> {
        let len = i32::try_from(bytes.len())
            .map_err(|_| PluginError::runtime(NAME, "chunk too large for a 32-bit guest"))?;

        let alloc = &self.alloc;

        set_fuel(&mut self.store, self.fuel)?;
        let ptr = alloc
            .call(&mut self.store, len)
            .map_err(|e| trap("tocat_alloc", &e))?;

        // Zero is how a guest says a chunk does not fit. Writing there anyway
        // would clobber whatever the guest keeps at the bottom of its memory
        // and then hand it a chunk it never agreed to take.
        if ptr <= 0 {
            return Err(PluginError::runtime(
                NAME,
                format!(
                    "guest refused a chunk of {len} bytes. A guest that meant to \
                 accept it may be returning an offset into its own arena \
                 rather than an address in its linear memory"
                ),
            ));
        }

        self.memory
            .write(&mut self.store, ptr as usize, bytes)
            .map_err(|e| {
                PluginError::runtime(NAME, format!("writing {len} bytes into the guest: {e}"))
            })
            .map(|()| ptr)
    }
}

fn required<P, R>(
    instance: &Instance,
    store: &mut Store<HostState>,
    export: &str,
) -> Result<TypedFunc<P, R>>
where
    P: WasmParams,
    R: WasmResults,
{
    instance
        .get_typed_func(store, export)
        .map_err(|e| config_error(format!("{export}: {e}")))
}

fn optional<P, R>(
    instance: &Instance,
    store: &mut Store<HostState>,
    export: &str,
) -> Option<TypedFunc<P, R>>
where
    P: WasmParams,
    R: WasmResults,
{
    instance.get_typed_func(store, export).ok()
}

fn set_fuel(store: &mut Store<HostState>, fuel: u64) -> Result<()> {
    // Zero means unmetered, which is opt-in and documented: it trades the
    // guarantee that a guest cannot hang the relay for a few percent of
    // throughput.
    let fuel = if fuel == 0 { u64::MAX } else { fuel };

    store
        .set_fuel(fuel)
        .map_err(|e| PluginError::runtime(NAME, format!("setting fuel: {e}")))
}

/// A trap is a failed direction, not a warning: the guest was mid-stream and
/// the bytes it was handed have gone nowhere.
fn trap(what: &str, error: &wasmtime::Error) -> PluginError {
    PluginError::runtime(NAME, format!("{what}: {error}"))
}

fn config_error(message: impl Into<String>) -> PluginError {
    PluginError::config(super::NAME, message.into())
}