rattery 0.2.0

rattery: a sandboxed terminal host that runs ratatui apps delivered over HTTP, as a CLI and as a library
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
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
//! The `Store` data: WASI contexts, limits, the terminal, extension state,
//! and the host implementation of `rattery:tui/terminal` and
//! `rattery:tui/websocket`.
//!
//! Synchronous WIT functions land in the `Host` traits and take `&mut self`.
//! The `async func`s (`next-event`, websocket `connect` and `receive`) land in
//! the `HostWithStore` traits: they run concurrently with the guest and reach
//! the store through an `Accessor` only when they need it.

use std::any::{Any, TypeId};
use std::collections::HashMap;
use std::sync::Arc;

use wasmtime::ResourceLimiter;
use wasmtime::component::{Accessor, HasSelf, Resource, ResourceTable};
use wasmtime_wasi::{WasiCtx, WasiCtxView, WasiView};
use wasmtime_wasi_http::{WasiHttpCtx, WasiHttpCtxView, WasiHttpView};

use crate::bindings::terminal::{self, CellUpdate, ClearType, Event, Position, Size, WindowSize};
use crate::bindings::websocket;
use crate::http::{CookieJar, OriginHooks, OriginPolicy, RequestPolicy};
use crate::terminal::{Interrupt, Interrupter, PhaseHook, TerminalHost};
use crate::websocket::WsSocket;
use crate::{Limits, Phase};

/// What survives a run of the store.
pub struct RunParts {
    pub term: TerminalHost,
    pub ext: HashMap<TypeId, Box<dyn Any + Send>>,
    pub memory_peak: usize,
    /// Reaps finished connection tasks continuously; `close` + `wait` it.
    pub websocket_tasks: tokio_util::task::TaskTracker,
}

/// The CPU budget, counted in epoch ticks while the guest executes.
pub struct CpuBudget {
    pub ticks: u64,
    pub budget_ticks: Option<u64>,
}

/// Bounds memory and tables *in aggregate* across every memory and table
/// the instance has, unlike wasmtime's `StoreLimits`, whose limits apply to
/// each memory separately.
pub struct AggregateLimiter {
    memory_limit: usize,
    memory_used: usize,
    table_limit: usize,
    table_used: usize,
    memories: usize,
    tables: usize,
    instances: usize,
    peak: usize,
}

impl AggregateLimiter {
    pub fn new(limits: &Limits) -> Self {
        Self {
            memory_limit: limits.memory_bytes,
            memory_used: 0,
            table_limit: limits.table_elements,
            table_used: 0,
            memories: limits.memories,
            tables: limits.tables,
            instances: limits.instances,
            peak: 0,
        }
    }

    /// The most linear memory in use at once across all memories.
    pub fn memory_peak(&self) -> usize {
        self.peak
    }
}

impl ResourceLimiter for AggregateLimiter {
    fn memory_growing(
        &mut self,
        current: usize,
        desired: usize,
        _maximum: Option<usize>,
    ) -> wasmtime::Result<bool> {
        let others = self.memory_used.saturating_sub(current);
        if others.saturating_add(desired) > self.memory_limit {
            return Err(wasmtime::Error::msg(format!(
                "memory limit of {} bytes exceeded (growing to {desired} bytes with {others} bytes in other memories)",
                self.memory_limit
            )));
        }
        self.memory_used = others + desired;
        self.peak = self.peak.max(self.memory_used);
        Ok(true)
    }

    fn table_growing(
        &mut self,
        current: usize,
        desired: usize,
        _maximum: Option<usize>,
    ) -> wasmtime::Result<bool> {
        let others = self.table_used.saturating_sub(current);
        if others.saturating_add(desired) > self.table_limit {
            return Err(wasmtime::Error::msg(format!(
                "table limit of {} elements exceeded",
                self.table_limit
            )));
        }
        self.table_used = others + desired;
        Ok(true)
    }

    fn instances(&self) -> usize {
        self.instances
    }

    fn tables(&self) -> usize {
        self.tables
    }

    fn memories(&self) -> usize {
        self.memories
    }
}

/// The data behind the wasmtime `Store`. Host extensions registered with
/// [`App::extension`](crate::App::extension) receive a
/// `Linker<HostState>` and can keep their own state in it through
/// [`HostState::ext`] and [`HostState::ext_mut`].
pub struct HostState {
    table: ResourceTable,
    wasi: WasiCtx,
    http: WasiHttpCtx,
    hooks: OriginHooks,
    policy: OriginPolicy,
    cookies: Option<CookieJar>,
    request_policy: Option<Arc<dyn RequestPolicy>>,
    term: TerminalHost,
    limits: Limits,
    limiter: AggregateLimiter,
    cpu: CpuBudget,
    interrupter: Arc<Interrupter>,
    on_phase: Option<PhaseHook>,
    /// Websocket slots; a permit lives in each socket resource.
    websocket_slots: Arc<tokio::sync::Semaphore>,
    /// Connection tasks. A tracker forgets tasks as they finish, so an app
    /// that connects and drops sockets forever costs nothing here.
    websocket_tasks: tokio_util::task::TaskTracker,
    ext: HashMap<TypeId, Box<dyn Any + Send>>,
}

pub struct HostStateConfig {
    pub wasi: WasiCtx,
    pub policy: OriginPolicy,
    pub cookies: Option<CookieJar>,
    pub request_policy: Option<Arc<dyn RequestPolicy>>,
    pub term: TerminalHost,
    pub limits: Limits,
    pub interrupter: Arc<Interrupter>,
    pub on_phase: Option<PhaseHook>,
    pub ext: HashMap<TypeId, Box<dyn Any + Send>>,
}

impl HostState {
    pub fn new(config: HostStateConfig) -> Self {
        let HostStateConfig {
            wasi,
            policy,
            cookies,
            request_policy,
            term,
            limits,
            interrupter,
            on_phase,
            ext,
        } = config;
        let limiter = AggregateLimiter::new(&limits);
        let websocket_slots = Arc::new(tokio::sync::Semaphore::new(limits.websockets));
        let mut table = ResourceTable::new();
        table.set_max_capacity(limits.resources);
        let budget_ticks = limits
            .cpu_time
            .map(|d| (d.as_nanos() / crate::runner::EPOCH_TICK.as_nanos()).max(1) as u64);
        Self {
            table,
            wasi,
            http: WasiHttpCtx::new(),
            hooks: OriginHooks::new(
                policy.clone(),
                cookies.clone(),
                request_policy.clone(),
                &limits,
                on_phase.clone(),
            ),
            policy,
            cookies,
            request_policy,
            term,
            limits,
            limiter,
            cpu: CpuBudget {
                ticks: 0,
                budget_ticks,
            },
            interrupter,
            on_phase,
            websocket_slots,
            websocket_tasks: tokio_util::task::TaskTracker::new(),
            ext,
        }
    }

    pub fn into_terminal(self) -> TerminalHost {
        self.term
    }

    /// The terminal, the extension state (carried across a reload), the peak
    /// memory the app used, and the websocket tasks still to be awaited.
    pub(crate) fn into_parts(self) -> RunParts {
        let websocket_tasks = self.websocket_tasks;
        RunParts {
            term: self.term,
            ext: self.ext,
            memory_peak: self.limiter.memory_peak(),
            websocket_tasks,
        }
    }

    pub(crate) fn limiter(&mut self) -> &mut AggregateLimiter {
        &mut self.limiter
    }

    /// State registered with [`App::state`](crate::App::state).
    pub fn ext<T: Any + Send>(&self) -> Option<&T> {
        self.ext
            .get(&TypeId::of::<T>())
            .and_then(|b| b.downcast_ref())
    }

    /// Mutable access to state registered with [`App::state`](crate::App::state).
    pub fn ext_mut<T: Any + Send>(&mut self) -> Option<&mut T> {
        self.ext
            .get_mut(&TypeId::of::<T>())
            .and_then(|b| b.downcast_mut())
    }

    /// The resource table, for host extensions that own resources.
    pub fn table(&mut self) -> &mut ResourceTable {
        &mut self.table
    }

    /// One epoch tick elapsed while the guest was executing. Returns the
    /// reason to stop, if any.
    pub(crate) fn tick(&mut self) -> Option<Interrupt> {
        if self.interrupter.is_fired() {
            // Keep the reason visible to the runner.
            let reason = self.interrupter.take_reason().unwrap_or(Interrupt::Kill);
            self.interrupter.fire(reason.clone());
            return Some(reason);
        }
        self.cpu.ticks += 1;
        if let Some(budget) = self.cpu.budget_ticks
            && self.cpu.ticks > budget
        {
            let reason = Interrupt::Limit(format!(
                "CPU budget of {:?} exhausted",
                self.limits.cpu_time.unwrap_or_default()
            ));
            self.interrupter.fire(reason.clone());
            return Some(reason);
        }
        None
    }
}

impl WasiView for HostState {
    fn ctx(&mut self) -> WasiCtxView<'_> {
        WasiCtxView {
            ctx: &mut self.wasi,
            table: &mut self.table,
        }
    }
}

impl WasiHttpView for HostState {
    fn http(&mut self) -> WasiHttpCtxView<'_> {
        WasiHttpCtxView {
            ctx: &mut self.http,
            table: &mut self.table,
            hooks: &mut self.hooks,
        }
    }
}

impl terminal::Host for HostState {
    async fn draw(&mut self, updates: Vec<CellUpdate>) -> wasmtime::Result<()> {
        if updates.len() > self.limits.frame_cells {
            let reason = Interrupt::Limit(format!(
                "a frame carried {} cells, over the limit of {}",
                updates.len(),
                self.limits.frame_cells
            ));
            self.interrupter.fire(reason.clone());
            return Err(wasmtime::Error::msg(format!("{reason:?}")));
        }
        self.term.draw(&updates)?;
        Ok(())
    }

    async fn append_lines(&mut self, n: u16) -> wasmtime::Result<()> {
        self.term.append_lines(n)?;
        Ok(())
    }

    async fn hide_cursor(&mut self) -> wasmtime::Result<()> {
        self.term.hide_cursor()?;
        Ok(())
    }

    async fn show_cursor(&mut self) -> wasmtime::Result<()> {
        self.term.show_cursor()?;
        Ok(())
    }

    async fn get_cursor_position(&mut self) -> wasmtime::Result<Position> {
        Ok(self.term.cursor_position()?)
    }

    async fn set_cursor_position(&mut self, pos: Position) -> wasmtime::Result<()> {
        self.term.set_cursor_position(pos)?;
        Ok(())
    }

    async fn clear(&mut self, kind: ClearType) -> wasmtime::Result<()> {
        self.term.clear(kind)?;
        Ok(())
    }

    async fn get_size(&mut self) -> wasmtime::Result<Size> {
        Ok(self.term.size()?)
    }

    async fn get_window_size(&mut self) -> wasmtime::Result<WindowSize> {
        Ok(self.term.window_size()?)
    }

    async fn flush(&mut self) -> wasmtime::Result<()> {
        self.term.flush()?;
        Ok(())
    }

    async fn read_events(&mut self) -> wasmtime::Result<Vec<Event>> {
        Ok(self.term.drain_events())
    }

    async fn origin(&mut self) -> wasmtime::Result<Option<String>> {
        Ok(self.term.origin().map(str::to_owned))
    }

    async fn location(&mut self) -> wasmtime::Result<Option<String>> {
        Ok(self.term.location().map(str::to_owned))
    }

    async fn set_title(&mut self, title: String) -> wasmtime::Result<()> {
        self.term.set_title(&title)?;
        Ok(())
    }

    async fn ready(&mut self) -> wasmtime::Result<()> {
        self.term.app_ready();
        Ok(())
    }
}

impl<U> terminal::HostWithStore<U> for HasSelf<HostState> {
    async fn next_event(store: &Accessor<U, Self>) -> wasmtime::Result<Event> {
        let queue = store.with(|mut view| view.get().term.queue());
        let event = queue.next().await;
        store.with(|mut view| view.get().term.note_event());
        Ok(event)
    }
}

impl websocket::Host for HostState {}

impl websocket::HostSocket for HostState {
    async fn close(&mut self, this: Resource<WsSocket>) -> wasmtime::Result<()> {
        self.table.get(&this)?.close();
        Ok(())
    }

    async fn drop(&mut self, this: Resource<WsSocket>) -> wasmtime::Result<()> {
        // The slot permit and the task's abort handle go with the resource.
        self.table.delete(this)?;
        Ok(())
    }
}

impl<U> websocket::HostSocketWithStore<U> for HasSelf<HostState> {
    async fn connect(
        store: &Accessor<U, Self>,
        url: String,
    ) -> wasmtime::Result<Result<Resource<WsSocket>, websocket::Error>> {
        // A slot is a semaphore permit held by the socket resource: it is
        // released whether the attempt is cancelled, fails, cannot be stored,
        // or the resource is dropped normally.
        let (slot, policy, cookies, request_policy, on_phase, limits, tasks) =
            store.with(|mut view| {
                let state = view.get();
                (
                    state.websocket_slots.clone().try_acquire_owned().ok(),
                    state.policy.clone(),
                    state.cookies.clone(),
                    state.request_policy.clone(),
                    state.on_phase.clone(),
                    state.limits.clone(),
                    state.websocket_tasks.clone(),
                )
            });
        let Some(slot) = slot else {
            if let Some(hook) = &on_phase {
                hook(Phase::RequestDenied {
                    url: url.clone(),
                    reason: format!("websocket limit of {} reached", limits.websockets),
                });
            }
            return Ok(Err(websocket::Error::Denied));
        };
        let connected = WsSocket::connect(
            &url,
            &policy,
            cookies.as_ref(),
            request_policy.as_ref(),
            on_phase.as_ref(),
            &limits,
            slot,
            &tasks,
        )
        .await;
        match connected {
            Ok(socket) => Ok(Ok(store.with(|mut view| view.get().table.push(socket))?)),
            Err(err) => Ok(Err(err)),
        }
    }

    async fn send(
        store: &Accessor<U, Self>,
        this: Resource<WsSocket>,
        message: websocket::Message,
    ) -> wasmtime::Result<Result<(), websocket::Error>> {
        let shared = store.with(|mut view| {
            let socket = view.get().table.get(&this)?;
            Ok::<_, wasmtime::Error>(socket.check_size(&message).map(|()| socket.shared()))
        })?;
        match shared {
            Ok(shared) => Ok(shared.send(message).await),
            Err(err) => Ok(Err(err)),
        }
    }

    async fn receive(
        store: &Accessor<U, Self>,
        this: Resource<WsSocket>,
    ) -> wasmtime::Result<Result<websocket::Message, websocket::Error>> {
        let shared = store.with(|mut view| view.get().table.get(&this).map(WsSocket::shared))?;
        Ok(shared.next_message().await)
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn memory_limit_is_aggregate_across_memories() {
        let limits = Limits {
            memory_bytes: 100,
            ..Limits::default()
        };
        let mut limiter = AggregateLimiter::new(&limits);
        assert!(limiter.memory_growing(0, 60, None).unwrap());
        // A second memory: 60 in use elsewhere, 50 more would exceed 100.
        assert!(limiter.memory_growing(0, 50, None).is_err());
        assert!(limiter.memory_growing(0, 40, None).unwrap());
        // The first memory growing from 60 to 70: 40 + 70 > 100.
        assert!(limiter.memory_growing(60, 70, None).is_err());
        assert_eq!(limiter.memory_peak(), 100);
    }
}