kobako 0.9.2 → 0.10.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/.release-please-manifest.json +1 -1
- data/CHANGELOG.md +25 -0
- data/Cargo.lock +3 -1
- data/README.md +35 -14
- data/data/kobako.wasm +0 -0
- data/ext/kobako/Cargo.toml +11 -1
- data/ext/kobako/src/runtime/cache.rs +168 -4
- data/ext/kobako/src/runtime/dispatch.rs +2 -2
- data/ext/kobako/src/runtime/exports.rs +32 -21
- data/ext/kobako/src/runtime/instance_pre.rs +97 -0
- data/ext/kobako/src/runtime/invocation.rs +12 -69
- data/ext/kobako/src/runtime.rs +371 -384
- data/lib/kobako/catalog/handles.rb +9 -13
- data/lib/kobako/catalog/namespaces.rb +22 -3
- data/lib/kobako/catalog/snippets.rb +9 -1
- data/lib/kobako/codec/decoder.rb +5 -1
- data/lib/kobako/errors.rb +11 -1
- data/lib/kobako/namespace.rb +16 -2
- data/lib/kobako/pool.rb +182 -0
- data/lib/kobako/sandbox.rb +16 -14
- data/lib/kobako/version.rb +1 -1
- data/lib/kobako.rb +1 -0
- data/release-please-config.json +16 -1
- data/sig/kobako/catalog/handles.rbs +0 -2
- data/sig/kobako/errors.rbs +3 -0
- data/sig/kobako/namespace.rbs +2 -0
- data/sig/kobako/pool.rbs +44 -0
- data/sig/kobako/sandbox.rbs +2 -2
- metadata +4 -1
data/ext/kobako/src/runtime.rs
CHANGED
|
@@ -2,32 +2,38 @@
|
|
|
2
2
|
//
|
|
3
3
|
// The only Ruby-visible class is
|
|
4
4
|
//
|
|
5
|
-
// Kobako::Runtime — wraps
|
|
5
|
+
// Kobako::Runtime — wraps a pre-linked InstancePre + per-Runtime caps
|
|
6
6
|
//
|
|
7
7
|
// constructed via `Kobako::Runtime.from_path(path, timeout, memory_limit,
|
|
8
|
-
// stdout_limit, stderr_limit)`.
|
|
9
|
-
//
|
|
10
|
-
//
|
|
11
|
-
//
|
|
12
|
-
//
|
|
8
|
+
// stdout_limit, stderr_limit)`. Every invocation (`#eval` / `#run`)
|
|
9
|
+
// instantiates a fresh instance from the InstancePre and discards the
|
|
10
|
+
// whole Store afterwards — the docs/behavior.md B-49 per-invocation
|
|
11
|
+
// instance discipline (ABI v2). The underlying wasmtime Engine and
|
|
12
|
+
// compiled Module live in a process-scope cache (see the `cache`
|
|
13
|
+
// submodule) and never surface to Ruby (SPEC.md "Code Organization":
|
|
14
|
+
// `ext/` "exposes no Wasm engine types to the Host App or downstream
|
|
15
|
+
// gems").
|
|
13
16
|
//
|
|
14
17
|
// Module layout (per CLAUDE.md principle #2 — one responsibility per file):
|
|
15
18
|
//
|
|
16
19
|
// * `cache` — process-wide Engine + per-path Module cache and the
|
|
17
20
|
// process-singleton epoch ticker thread.
|
|
18
21
|
// * `config` — per-Runtime caps (timeout / stdout / stderr limits).
|
|
19
|
-
// * `exports` —
|
|
20
|
-
//
|
|
21
|
-
//
|
|
22
|
-
//
|
|
22
|
+
// * `exports` — per-invocation `__kobako_eval` / `_run` /
|
|
23
|
+
// `_take_outcome` / `_alloc` / `memory` handles.
|
|
24
|
+
// * `instance_pre`— host-import Linker wiring + per-path `InstancePre`
|
|
25
|
+
// cache.
|
|
26
|
+
// * `invocation` — Invocation (per-Store context), the `MemoryLimiter`
|
|
27
|
+
// memory cap, and the trap marker types
|
|
28
|
+
// (`TimeoutTrap` / `MemoryLimitTrap`).
|
|
23
29
|
// * `dispatch` — `__kobako_dispatch` host-import dispatch helpers.
|
|
24
30
|
// * `guest_mem` — Caller-based guest linear-memory alloc / write / read.
|
|
25
31
|
// * `capture` — stdout / stderr pipe sizing + clip helpers.
|
|
26
32
|
// * `trap` — wasmtime-error → `Kobako::*` trap classification.
|
|
27
33
|
//
|
|
28
|
-
// This file owns the `Kobako::Runtime` magnus class itself (the
|
|
29
|
-
//
|
|
30
|
-
//
|
|
34
|
+
// This file owns the `Kobako::Runtime` magnus class itself (the
|
|
35
|
+
// InstancePre + `Config` + the per-invocation `#eval` / `#run` run
|
|
36
|
+
// path), the Ruby error-class lazy-resolvers, the `trap_err` /
|
|
31
37
|
// `timeout_err` / `memory_limit_err` / `setup_err` constructors shared by
|
|
32
38
|
// every submodule, and the Ruby init() that registers the class.
|
|
33
39
|
|
|
@@ -38,6 +44,7 @@ mod config;
|
|
|
38
44
|
mod dispatch;
|
|
39
45
|
mod exports;
|
|
40
46
|
mod guest_mem;
|
|
47
|
+
mod instance_pre;
|
|
41
48
|
mod invocation;
|
|
42
49
|
mod trap;
|
|
43
50
|
|
|
@@ -54,23 +61,25 @@ use magnus::{gc, typed_data::DataTypeFunctions, value::Opaque, RArray, TypedData
|
|
|
54
61
|
|
|
55
62
|
use crate::snapshot::Snapshot;
|
|
56
63
|
use wasmtime::{
|
|
57
|
-
AsContextMut,
|
|
58
|
-
|
|
64
|
+
AsContextMut, InstancePre as WtInstancePre, Memory, ResourceLimiter, Store as WtStore,
|
|
65
|
+
TypedFunc,
|
|
59
66
|
};
|
|
60
|
-
use wasmtime_wasi::p1;
|
|
61
67
|
use wasmtime_wasi::p2::pipe::{MemoryInputPipe, MemoryOutputPipe};
|
|
62
68
|
use wasmtime_wasi::WasiCtxBuilder;
|
|
63
69
|
|
|
64
|
-
use self::cache::
|
|
70
|
+
use self::cache::shared_engine;
|
|
65
71
|
use self::config::Config;
|
|
66
72
|
use self::exports::Exports;
|
|
67
|
-
use self::invocation::
|
|
73
|
+
use self::invocation::Invocation;
|
|
68
74
|
|
|
69
75
|
/// The wire ABI version this host implements (docs/wire-codec.md § ABI
|
|
70
76
|
/// Version). A Guest Binary is accepted only when its
|
|
71
77
|
/// `__kobako_abi_version` export reports the same value (B-40 / E-42);
|
|
72
|
-
/// the guest-side mirror is `kobako_core::abi::ABI_VERSION`.
|
|
73
|
-
|
|
78
|
+
/// the guest-side mirror is `kobako_core::abi::ABI_VERSION`. Version 2
|
|
79
|
+
/// carries the per-invocation instance discipline (B-49): the host
|
|
80
|
+
/// drives every invocation on a fresh instance, so the guest may leave
|
|
81
|
+
/// its VM state dirty at exit.
|
|
82
|
+
const ABI_VERSION: u32 = 2;
|
|
74
83
|
|
|
75
84
|
/// Copy the bytes of `s` into a fresh `Vec<u8>`. Single safe entry to
|
|
76
85
|
/// what would otherwise be an inline `unsafe { rstring.as_slice() }
|
|
@@ -200,26 +209,34 @@ pub fn init(ruby: &Ruby, kobako: RModule) -> Result<(), MagnusError> {
|
|
|
200
209
|
#[derive(TypedData)]
|
|
201
210
|
#[magnus(class = "Kobako::Runtime", free_immediately, size, mark)]
|
|
202
211
|
pub(crate) struct Runtime {
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
//
|
|
206
|
-
//
|
|
207
|
-
//
|
|
208
|
-
|
|
212
|
+
// Pre-linked instantiation template (import wiring + type checks
|
|
213
|
+
// done once in `instance_pre::cached_instance_pre`). Every
|
|
214
|
+
// invocation instantiates a fresh instance from it and discards the
|
|
215
|
+
// whole Store afterwards — the docs/behavior.md B-49 per-invocation
|
|
216
|
+
// instance discipline.
|
|
217
|
+
instance_pre: WtInstancePre<Invocation>,
|
|
218
|
+
// Per-invocation linear-memory cap (docs/behavior.md B-01 / E-20),
|
|
219
|
+
// threaded into each fresh `Invocation`; lives apart from `Config`
|
|
220
|
+
// because the wasmtime `ResourceLimiter` callback consumes it from
|
|
221
|
+
// inside the wasm engine.
|
|
222
|
+
memory_limit: Option<usize>,
|
|
209
223
|
// Wall-clock + per-channel capture caps forwarded from the Sandbox;
|
|
210
|
-
// see `Config`.
|
|
211
|
-
// which lives on `Invocation` because the wasmtime `ResourceLimiter`
|
|
212
|
-
// callback consumes it from inside the wasm engine.
|
|
224
|
+
// see `Config`.
|
|
213
225
|
config: Config,
|
|
214
226
|
// The host-side dispatch Proc (docs/behavior.md B-12), held here only
|
|
215
|
-
// to give `DataTypeFunctions::mark` a
|
|
216
|
-
//
|
|
217
|
-
//
|
|
218
|
-
// `
|
|
219
|
-
// `
|
|
220
|
-
// one pinned Proc. `Cell` is sound under the GVL (see the `unsafe impl
|
|
227
|
+
// to give `DataTypeFunctions::mark` a read path so it can pin the
|
|
228
|
+
// Proc across GC. The copy the `__kobako_dispatch` import actually
|
|
229
|
+
// calls is bound onto each per-invocation `Invocation` by
|
|
230
|
+
// `Runtime::new_store`. Both hold the same `Copy` handle to the one
|
|
231
|
+
// pinned Proc. `Cell` is sound under the GVL (see the `unsafe impl
|
|
221
232
|
// Sync` below).
|
|
222
233
|
on_dispatch: Cell<Option<Opaque<Value>>>,
|
|
234
|
+
// docs/behavior.md B-35 usage of the most recent invocation —
|
|
235
|
+
// `(wall_time_seconds, memory_peak_bytes)` — captured by
|
|
236
|
+
// `build_snapshot` before the per-invocation Store is discarded so
|
|
237
|
+
// `#usage` reads survive the teardown. `(0.0, 0)` before the first
|
|
238
|
+
// invocation.
|
|
239
|
+
last_usage: Cell<(f64, usize)>,
|
|
223
240
|
}
|
|
224
241
|
|
|
225
242
|
impl DataTypeFunctions for Runtime {
|
|
@@ -239,20 +256,19 @@ impl DataTypeFunctions for Runtime {
|
|
|
239
256
|
}
|
|
240
257
|
}
|
|
241
258
|
|
|
242
|
-
// SAFETY: magnus requires `Send + Sync` on TypedData types. The
|
|
243
|
-
// `on_dispatch
|
|
244
|
-
//
|
|
245
|
-
//
|
|
246
|
-
//
|
|
247
|
-
//
|
|
248
|
-
// occur. `Send` stays auto-derived (`Opaque<Value>` is `Send`).
|
|
259
|
+
// SAFETY: magnus requires `Send + Sync` on TypedData types. The
|
|
260
|
+
// `on_dispatch` / `last_usage` `Cell`s make the auto-derived `Sync`
|
|
261
|
+
// unavailable, but every access to them happens under the GVL on a single
|
|
262
|
+
// thread at a time — Ruby method calls, and a GC `mark` pass that also
|
|
263
|
+
// holds the GVL. No cross-thread access to either Cell can occur. `Send`
|
|
264
|
+
// stays auto-derived.
|
|
249
265
|
unsafe impl Sync for Runtime {}
|
|
250
266
|
|
|
251
267
|
impl Runtime {
|
|
252
|
-
/// Construct
|
|
253
|
-
/// shared Engine and per-path Module
|
|
254
|
-
/// constructor for `Kobako::Runtime` — Engine and Module
|
|
255
|
-
/// visible to Ruby.
|
|
268
|
+
/// Construct a Runtime from a wasm file path, using the process-wide
|
|
269
|
+
/// shared Engine and per-path Module / InstancePre caches. The single
|
|
270
|
+
/// Ruby-facing constructor for `Kobako::Runtime` — Engine and Module
|
|
271
|
+
/// are never visible to Ruby.
|
|
256
272
|
///
|
|
257
273
|
/// `timeout_seconds` is the docs/behavior.md B-01 wall-clock cap in seconds
|
|
258
274
|
/// (`None` disables); `memory_limit` is the linear-memory cap in
|
|
@@ -285,108 +301,39 @@ impl Runtime {
|
|
|
285
301
|
}
|
|
286
302
|
};
|
|
287
303
|
|
|
288
|
-
let
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
let mut store = WtStore::new(engine, Invocation::new(memory_limit));
|
|
292
|
-
store.limiter(|state: &mut Invocation| -> &mut dyn ResourceLimiter { state.limiter_mut() });
|
|
293
|
-
store.epoch_deadline_callback(trap::epoch_deadline_callback);
|
|
294
|
-
|
|
295
|
-
let store_cell = StoreCell::new(store);
|
|
296
|
-
Self::build(
|
|
297
|
-
engine,
|
|
298
|
-
&module,
|
|
299
|
-
store_cell,
|
|
300
|
-
timeout,
|
|
301
|
-
stdout_limit_bytes,
|
|
302
|
-
stderr_limit_bytes,
|
|
303
|
-
)
|
|
304
|
-
}
|
|
305
|
-
|
|
306
|
-
/// Build an `Runtime` from an engine, module, and store cell. The
|
|
307
|
-
/// store cell is moved in and ends up owned by the returned Runtime.
|
|
308
|
-
/// Wires the WASI p1 imports plus the `__kobako_dispatch` host import.
|
|
309
|
-
fn build(
|
|
310
|
-
engine: &wasmtime::Engine,
|
|
311
|
-
module: &WtModule,
|
|
312
|
-
store_cell: StoreCell,
|
|
313
|
-
timeout: Option<Duration>,
|
|
314
|
-
stdout_limit_bytes: Option<usize>,
|
|
315
|
-
stderr_limit_bytes: Option<usize>,
|
|
316
|
-
) -> Result<Self, MagnusError> {
|
|
317
|
-
let ruby = Ruby::get().expect("Ruby thread");
|
|
318
|
-
let mut linker: Linker<Invocation> = Linker::new(engine);
|
|
319
|
-
|
|
320
|
-
// Wire the wasmtime-wasi preview1 WASI imports. Routes guest fd 1/2
|
|
321
|
-
// to the MemoryOutputPipes set up before each run via
|
|
322
|
-
// `Runtime::eval`. The closure pulls a `&mut WasiP1Ctx` out of
|
|
323
|
-
// Invocation; the panic semantics live inside `Invocation::wasi_mut`
|
|
324
|
-
// so the wiring stays honest about its precondition.
|
|
325
|
-
p1::add_to_linker_sync(&mut linker, |state: &mut Invocation| state.wasi_mut())
|
|
326
|
-
.map_err(|e| setup_err(&ruby, format!("failed to set up the WASI runtime: {}", e)))?;
|
|
327
|
-
|
|
328
|
-
// `__kobako_dispatch` host import. Signature per docs/wire-codec.md
|
|
329
|
-
// § ABI Signatures:
|
|
330
|
-
// (req_ptr: i32, req_len: i32) -> i64
|
|
331
|
-
// Decodes the Request bytes, dispatches via the Ruby-side
|
|
332
|
-
// dispatch Proc (bound per-Sandbox through `Runtime#on_dispatch=`),
|
|
333
|
-
// allocates a guest buffer through `__kobako_alloc`, writes
|
|
334
|
-
// the Response bytes there, and returns the packed
|
|
335
|
-
// `(ptr<<32)|len`. The dispatcher returns 0 on any wire-layer
|
|
336
|
-
// fault (including no Proc bound); see `dispatch::handle`.
|
|
337
|
-
linker
|
|
338
|
-
.func_wrap(
|
|
339
|
-
"env",
|
|
340
|
-
"__kobako_dispatch",
|
|
341
|
-
|mut caller: Caller<'_, Invocation>, req_ptr: i32, req_len: i32| -> i64 {
|
|
342
|
-
dispatch::handle(&mut caller, req_ptr, req_len)
|
|
343
|
-
},
|
|
344
|
-
)
|
|
345
|
-
.map_err(|e| {
|
|
346
|
-
setup_err(
|
|
347
|
-
&ruby,
|
|
348
|
-
format!("failed to set up the host callback bridge: {}", e),
|
|
349
|
-
)
|
|
350
|
-
})?;
|
|
351
|
-
|
|
352
|
-
let instance = {
|
|
353
|
-
let mut store_ref = store_cell.borrow_mut();
|
|
354
|
-
linker
|
|
355
|
-
.instantiate(store_ref.as_context_mut(), module)
|
|
356
|
-
.map_err(|e| trap::instantiate_err(&ruby, e))?
|
|
357
|
-
};
|
|
358
|
-
|
|
359
|
-
Self::validate_abi_version(&instance, &store_cell, &ruby)?;
|
|
360
|
-
|
|
361
|
-
let exports = Exports::resolve(&instance, &store_cell);
|
|
362
|
-
|
|
363
|
-
Ok(Self {
|
|
364
|
-
inner: instance,
|
|
365
|
-
store: store_cell,
|
|
366
|
-
exports,
|
|
304
|
+
let runtime = Self {
|
|
305
|
+
instance_pre: instance_pre::cached_instance_pre(Path::new(&path))?,
|
|
306
|
+
memory_limit,
|
|
367
307
|
config: Config {
|
|
368
308
|
timeout,
|
|
369
309
|
stdout_limit_bytes,
|
|
370
310
|
stderr_limit_bytes,
|
|
371
311
|
},
|
|
372
312
|
on_dispatch: Cell::new(None),
|
|
373
|
-
|
|
313
|
+
last_usage: Cell::new((0.0, 0)),
|
|
314
|
+
};
|
|
315
|
+
runtime.probe_abi_version(&ruby)?;
|
|
316
|
+
Ok(runtime)
|
|
374
317
|
}
|
|
375
318
|
|
|
376
|
-
///
|
|
377
|
-
///
|
|
319
|
+
/// Instantiate a throwaway probe instance at construction and require
|
|
320
|
+
/// the guest's `__kobako_abi_version` export to equal `ABI_VERSION`
|
|
378
321
|
/// (docs/behavior.md B-40). An absent export or a non-equal value is
|
|
379
322
|
/// E-42 — a deterministic artifact fault raised as
|
|
380
|
-
/// `Kobako::SetupError`.
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
) -> Result<(), MagnusError> {
|
|
386
|
-
let mut
|
|
387
|
-
|
|
323
|
+
/// `Kobako::SetupError`. The probe Store drops here; invocation
|
|
324
|
+
/// instances are created per `#eval` / `#run`. The frameless WASI
|
|
325
|
+
/// context keeps a third-party guest whose start section touches
|
|
326
|
+
/// WASI on the E-41 `SetupError` path instead of panicking in
|
|
327
|
+
/// `Invocation::wasi_mut`.
|
|
328
|
+
fn probe_abi_version(&self, ruby: &Ruby) -> Result<(), MagnusError> {
|
|
329
|
+
let mut store = self.new_store()?;
|
|
330
|
+
install_wasi_frames(&mut store, &self.config, &[])?;
|
|
331
|
+
let instance = self
|
|
332
|
+
.instance_pre
|
|
333
|
+
.instantiate(store.as_context_mut())
|
|
334
|
+
.map_err(|e| trap::instantiate_err(ruby, e))?;
|
|
388
335
|
let probe = instance
|
|
389
|
-
.get_typed_func::<(), u32>(
|
|
336
|
+
.get_typed_func::<(), u32>(store.as_context_mut(), "__kobako_abi_version")
|
|
390
337
|
.map_err(|_| {
|
|
391
338
|
setup_err(
|
|
392
339
|
ruby,
|
|
@@ -396,7 +343,7 @@ impl Runtime {
|
|
|
396
343
|
),
|
|
397
344
|
)
|
|
398
345
|
})?;
|
|
399
|
-
let reported = probe.call(
|
|
346
|
+
let reported = probe.call(store.as_context_mut(), ()).map_err(|e| {
|
|
400
347
|
setup_err(
|
|
401
348
|
ruby,
|
|
402
349
|
format!("failed to read the Guest Binary's ABI version: {e}"),
|
|
@@ -415,25 +362,13 @@ impl Runtime {
|
|
|
415
362
|
Ok(())
|
|
416
363
|
}
|
|
417
364
|
|
|
418
|
-
/// Register the Ruby-side dispatch `Proc`
|
|
419
|
-
/// Bound to Ruby as `Kobako::Runtime#on_dispatch=`.
|
|
420
|
-
///
|
|
421
|
-
///
|
|
422
|
-
///
|
|
365
|
+
/// Register the Ruby-side dispatch `Proc` (docs/behavior.md B-12).
|
|
366
|
+
/// Bound to Ruby as `Kobako::Runtime#on_dispatch=`. The handle is
|
|
367
|
+
/// pinned by `DataTypeFunctions::mark` and copied onto every
|
|
368
|
+
/// per-invocation `Invocation` by `Runtime::new_store`, where the
|
|
369
|
+
/// `__kobako_dispatch` import reads it through `Caller<Invocation>`.
|
|
423
370
|
pub(crate) fn set_on_dispatch(&self, proc_value: Value) -> Result<(), MagnusError> {
|
|
424
|
-
|
|
425
|
-
// Write both copies of the one Proc handle: the `on_dispatch` Cell
|
|
426
|
-
// gives `DataTypeFunctions::mark` a Store-free read path to pin the
|
|
427
|
-
// Proc across GC, and the `Invocation` copy is what the
|
|
428
|
-
// `__kobako_dispatch` import reads through `Caller<Invocation>`.
|
|
429
|
-
// `mark` cannot reach the Invocation copy itself — the Store is held
|
|
430
|
-
// `borrow_mut` for the whole guest call, exactly when GC may fire
|
|
431
|
-
// during dispatch — so the Cell is the dedicated GC-rooting anchor.
|
|
432
|
-
self.on_dispatch.set(Some(on_dispatch));
|
|
433
|
-
self.store
|
|
434
|
-
.borrow_mut()
|
|
435
|
-
.data_mut()
|
|
436
|
-
.bind_on_dispatch(on_dispatch);
|
|
371
|
+
self.on_dispatch.set(Some(Opaque::from(proc_value)));
|
|
437
372
|
Ok(())
|
|
438
373
|
}
|
|
439
374
|
|
|
@@ -457,7 +392,7 @@ impl Runtime {
|
|
|
457
392
|
) -> Result<RString, MagnusError> {
|
|
458
393
|
let ruby = Ruby::get().expect("Ruby thread");
|
|
459
394
|
let _ = self; // The Caller carries its own Store; `self` is only
|
|
460
|
-
// a marker that the method belongs to
|
|
395
|
+
// a marker that the method belongs to a Runtime.
|
|
461
396
|
|
|
462
397
|
let bytes = rstring_to_vec(args_bytes);
|
|
463
398
|
let Some(caller) = dispatch::current_caller() else {
|
|
@@ -482,13 +417,13 @@ impl Runtime {
|
|
|
482
417
|
/// Execute one guest invocation (`__kobako_eval` — one-shot source)
|
|
483
418
|
/// and return a `Snapshot` bundling every per-invocation observable.
|
|
484
419
|
///
|
|
485
|
-
///
|
|
486
|
-
///
|
|
487
|
-
///
|
|
488
|
-
///
|
|
489
|
-
///
|
|
490
|
-
///
|
|
491
|
-
///
|
|
420
|
+
/// Builds a fresh Store + instance (B-49) whose WASI context carries
|
|
421
|
+
/// the three-frame stdin protocol (`preamble`, `source`, `snippets`
|
|
422
|
+
/// — docs/wire-codec.md § Invocation channels), then invokes
|
|
423
|
+
/// `__kobako_eval`. Per-invocation caps (docs/behavior.md B-01) are
|
|
424
|
+
/// primed here: the wall-clock deadline is stamped into `Invocation`
|
|
425
|
+
/// and the epoch deadline is set to fire at the next ticker tick;
|
|
426
|
+
/// the memory-cap limiter is already wired.
|
|
492
427
|
///
|
|
493
428
|
/// On a wasmtime trap the configured-cap path raises
|
|
494
429
|
/// `Kobako::TimeoutError` / `Kobako::MemoryLimitError`; everything
|
|
@@ -502,27 +437,34 @@ impl Runtime {
|
|
|
502
437
|
snippets: RString,
|
|
503
438
|
) -> Result<Snapshot, MagnusError> {
|
|
504
439
|
let ruby = Ruby::get().expect("Ruby thread");
|
|
505
|
-
let
|
|
506
|
-
|
|
507
|
-
|
|
508
|
-
|
|
509
|
-
|
|
510
|
-
|
|
511
|
-
|
|
440
|
+
let mut store = self.new_store()?;
|
|
441
|
+
install_wasi_frames(
|
|
442
|
+
&mut store,
|
|
443
|
+
&self.config,
|
|
444
|
+
&[
|
|
445
|
+
rstring_to_vec(preamble),
|
|
446
|
+
rstring_to_vec(source),
|
|
447
|
+
rstring_to_vec(snippets),
|
|
448
|
+
],
|
|
449
|
+
)?;
|
|
450
|
+
let exports = self.instantiate(&ruby, &mut store)?;
|
|
451
|
+
let eval = require_export(&ruby, exports.eval.as_ref())?;
|
|
452
|
+
self.call_with_caps(&mut store, &exports, eval, ())
|
|
512
453
|
.map_err(|e| trap::call_err(&ruby, e))?;
|
|
513
|
-
self.build_snapshot(&ruby)
|
|
454
|
+
self.build_snapshot(&ruby, &mut store, &exports)
|
|
514
455
|
}
|
|
515
456
|
|
|
516
457
|
/// Execute one entrypoint dispatch (`__kobako_run`) and return a
|
|
517
458
|
/// `Snapshot` bundling every per-invocation observable.
|
|
518
459
|
///
|
|
519
|
-
///
|
|
520
|
-
/// (preamble + snippets; no user source
|
|
521
|
-
/// § Invocation channels), copies
|
|
522
|
-
/// memory via `__kobako_alloc`,
|
|
523
|
-
/// env_len)`. Per-invocation cap
|
|
524
|
-
/// Raises `Kobako::TrapError`
|
|
525
|
-
/// allocation fails
|
|
460
|
+
/// Builds a fresh Store + instance (B-49) whose WASI context carries
|
|
461
|
+
/// the two-frame stdin protocol (preamble + snippets; no user source
|
|
462
|
+
/// frame — docs/wire-codec.md § Invocation channels), copies
|
|
463
|
+
/// `envelope` bytes into guest linear memory via `__kobako_alloc`,
|
|
464
|
+
/// and calls `__kobako_run(env_ptr, env_len)`. Per-invocation cap
|
|
465
|
+
/// semantics match `Runtime::eval`. Raises `Kobako::TrapError`
|
|
466
|
+
/// ("alloc returned 0") when guest allocation fails
|
|
467
|
+
/// (docs/behavior.md E-31).
|
|
526
468
|
pub(crate) fn run(
|
|
527
469
|
&self,
|
|
528
470
|
preamble: RString,
|
|
@@ -530,46 +472,18 @@ impl Runtime {
|
|
|
530
472
|
envelope: RString,
|
|
531
473
|
) -> Result<Snapshot, MagnusError> {
|
|
532
474
|
let ruby = Ruby::get().expect("Ruby thread");
|
|
533
|
-
let
|
|
534
|
-
|
|
535
|
-
|
|
536
|
-
|
|
475
|
+
let mut store = self.new_store()?;
|
|
476
|
+
install_wasi_frames(
|
|
477
|
+
&mut store,
|
|
478
|
+
&self.config,
|
|
479
|
+
&[rstring_to_vec(preamble), rstring_to_vec(snippets)],
|
|
480
|
+
)?;
|
|
481
|
+
let exports = self.instantiate(&ruby, &mut store)?;
|
|
482
|
+
let run = require_export(&ruby, exports.run.as_ref())?;
|
|
483
|
+
let (env_ptr, env_len) = write_envelope(&ruby, &mut store, &exports, envelope)?;
|
|
484
|
+
self.call_with_caps(&mut store, &exports, run, (env_ptr, env_len))
|
|
537
485
|
.map_err(|e| trap::call_err(&ruby, e))?;
|
|
538
|
-
self.build_snapshot(&ruby)
|
|
539
|
-
}
|
|
540
|
-
|
|
541
|
-
/// Collect every per-invocation observable into a fresh `Snapshot`.
|
|
542
|
-
/// Called from the run-path methods after the guest export returns
|
|
543
|
-
/// successfully: drains OUTCOME_BUFFER via `__kobako_take_outcome`,
|
|
544
|
-
/// snapshots the per-channel stdout / stderr pipes (clipped to their
|
|
545
|
-
/// caps), and reads B-35 `wall_time` / `memory_peak` from Invocation.
|
|
546
|
-
fn build_snapshot(&self, ruby: &Ruby) -> Result<Snapshot, MagnusError> {
|
|
547
|
-
let return_bytes = self.fetch_outcome_bytes(ruby)?;
|
|
548
|
-
let (stdout_raw, stderr_raw, wall_time, memory_peak) = {
|
|
549
|
-
let state = self.store.borrow();
|
|
550
|
-
let data = state.data();
|
|
551
|
-
(
|
|
552
|
-
data.stdout_bytes(),
|
|
553
|
-
data.stderr_bytes(),
|
|
554
|
-
data.wall_time(),
|
|
555
|
-
data.memory_peak(),
|
|
556
|
-
)
|
|
557
|
-
};
|
|
558
|
-
let (stdout_visible, stdout_truncated) =
|
|
559
|
-
capture::clip_capture(&stdout_raw, self.config.stdout_limit_bytes);
|
|
560
|
-
let stdout_bytes = stdout_visible.to_vec();
|
|
561
|
-
let (stderr_visible, stderr_truncated) =
|
|
562
|
-
capture::clip_capture(&stderr_raw, self.config.stderr_limit_bytes);
|
|
563
|
-
let stderr_bytes = stderr_visible.to_vec();
|
|
564
|
-
Ok(Snapshot::new(
|
|
565
|
-
return_bytes,
|
|
566
|
-
stdout_bytes,
|
|
567
|
-
stdout_truncated,
|
|
568
|
-
stderr_bytes,
|
|
569
|
-
stderr_truncated,
|
|
570
|
-
wall_time,
|
|
571
|
-
memory_peak,
|
|
572
|
-
))
|
|
486
|
+
self.build_snapshot(&ruby, &mut store, &exports)
|
|
573
487
|
}
|
|
574
488
|
|
|
575
489
|
/// Return the docs/behavior.md B-35 per-last-invocation usage as a
|
|
@@ -581,27 +495,22 @@ impl Runtime {
|
|
|
581
495
|
/// * `wall_time` (Float seconds) — the wall-clock duration the
|
|
582
496
|
/// most recent invocation spent inside the guest export call.
|
|
583
497
|
/// Bracket opens in `Runtime::prime_caps` and closes in
|
|
584
|
-
/// `
|
|
585
|
-
///
|
|
586
|
-
///
|
|
587
|
-
/// `OUTCOME_BUFFER` fetch and decode, plus stdout / stderr
|
|
588
|
-
/// capture readout. `0.0` before the first invocation.
|
|
498
|
+
/// `disarm_caps`, so the value mirrors the `timeout` deadline
|
|
499
|
+
/// accounting and excludes everything that runs after the guest
|
|
500
|
+
/// export returns. `0.0` before the first invocation.
|
|
589
501
|
/// * `memory_peak` (Integer bytes) — the high-water mark of the
|
|
590
502
|
/// per-invocation `memory.grow` delta past the linear-memory
|
|
591
503
|
/// size captured at invocation entry. `0` before the first
|
|
592
504
|
/// invocation.
|
|
593
505
|
///
|
|
594
|
-
///
|
|
595
|
-
///
|
|
596
|
-
/// `store.borrow()` per readout and a single magnus binding to
|
|
597
|
-
/// extend when B-35's field list grows past two.
|
|
506
|
+
/// Reads the `last_usage` Cell `build_snapshot` populated before the
|
|
507
|
+
/// per-invocation Store was discarded.
|
|
598
508
|
pub(crate) fn usage(&self) -> Result<RArray, MagnusError> {
|
|
599
509
|
let ruby = Ruby::get().expect("Ruby thread");
|
|
600
|
-
let
|
|
601
|
-
let data = state.data();
|
|
510
|
+
let (wall_time, memory_peak) = self.last_usage.get();
|
|
602
511
|
let arr = ruby.ary_new_capa(2);
|
|
603
|
-
arr.push(
|
|
604
|
-
arr.push(
|
|
512
|
+
arr.push(wall_time)?;
|
|
513
|
+
arr.push(memory_peak)?;
|
|
605
514
|
Ok(arr)
|
|
606
515
|
}
|
|
607
516
|
|
|
@@ -609,9 +518,44 @@ impl Runtime {
|
|
|
609
518
|
// Private helpers.
|
|
610
519
|
// -----------------------------------------------------------------
|
|
611
520
|
|
|
521
|
+
/// Build the per-invocation Store: a fresh `Invocation` wired with
|
|
522
|
+
/// the memory limiter, the epoch-deadline callback, and the
|
|
523
|
+
/// registered dispatch Proc (docs/behavior.md B-12).
|
|
524
|
+
fn new_store(&self) -> Result<WtStore<Invocation>, MagnusError> {
|
|
525
|
+
let mut store = WtStore::new(shared_engine()?, Invocation::new(self.memory_limit));
|
|
526
|
+
store.limiter(|state: &mut Invocation| -> &mut dyn ResourceLimiter { state.limiter_mut() });
|
|
527
|
+
store.epoch_deadline_callback(trap::epoch_deadline_callback);
|
|
528
|
+
if let Some(on_dispatch) = self.on_dispatch.get() {
|
|
529
|
+
store.data_mut().bind_on_dispatch(on_dispatch);
|
|
530
|
+
}
|
|
531
|
+
Ok(store)
|
|
532
|
+
}
|
|
533
|
+
|
|
534
|
+
/// Instantiate the per-invocation instance from the pre-linked
|
|
535
|
+
/// template (B-49) and resolve its host-driven export handles. An
|
|
536
|
+
/// instantiation failure at invocation time is an engine fault —
|
|
537
|
+
/// `Kobako::TrapError` — unlike the construction-time probe, whose
|
|
538
|
+
/// failure is E-41 `SetupError`.
|
|
539
|
+
fn instantiate(
|
|
540
|
+
&self,
|
|
541
|
+
ruby: &Ruby,
|
|
542
|
+
store: &mut WtStore<Invocation>,
|
|
543
|
+
) -> Result<Exports, MagnusError> {
|
|
544
|
+
let instance = self
|
|
545
|
+
.instance_pre
|
|
546
|
+
.instantiate(store.as_context_mut())
|
|
547
|
+
.map_err(|e| {
|
|
548
|
+
trap_err(
|
|
549
|
+
ruby,
|
|
550
|
+
format!("failed to instantiate the Sandbox runtime: {e}"),
|
|
551
|
+
)
|
|
552
|
+
})?;
|
|
553
|
+
Ok(Exports::resolve(&instance, store.as_context_mut()))
|
|
554
|
+
}
|
|
555
|
+
|
|
612
556
|
/// Run one guest export call inside the per-invocation cap window:
|
|
613
|
-
/// `Runtime::prime_caps` before, `
|
|
614
|
-
///
|
|
557
|
+
/// `Runtime::prime_caps` before, `disarm_caps` after — the shared
|
|
558
|
+
/// bracket for both run-path exports (`__kobako_eval` /
|
|
615
559
|
/// `__kobako_run`). Disarm runs whether the call returns or traps, so
|
|
616
560
|
/// the docs/behavior.md B-35 `wall_time` bracket and the E-20 memory
|
|
617
561
|
/// cap always close — that close-on-trap guarantee is the reason this
|
|
@@ -620,6 +564,8 @@ impl Runtime {
|
|
|
620
564
|
/// through `trap::call_err` for its own error context.
|
|
621
565
|
fn call_with_caps<Params, Results>(
|
|
622
566
|
&self,
|
|
567
|
+
store: &mut WtStore<Invocation>,
|
|
568
|
+
exports: &Exports,
|
|
623
569
|
export: &TypedFunc<Params, Results>,
|
|
624
570
|
params: Params,
|
|
625
571
|
) -> Result<Results, wasmtime::Error>
|
|
@@ -627,12 +573,15 @@ impl Runtime {
|
|
|
627
573
|
Params: wasmtime::WasmParams,
|
|
628
574
|
Results: wasmtime::WasmResults,
|
|
629
575
|
{
|
|
630
|
-
self.prime_caps();
|
|
631
|
-
let result =
|
|
632
|
-
|
|
633
|
-
|
|
634
|
-
|
|
635
|
-
|
|
576
|
+
self.prime_caps(store, exports);
|
|
577
|
+
let result = export.call(store.as_context_mut(), params);
|
|
578
|
+
disarm_caps(store);
|
|
579
|
+
// Stash the B-35 usage figures on every outcome — including the
|
|
580
|
+
// trap paths, where `build_snapshot` never runs and the Store is
|
|
581
|
+
// about to be discarded with the error.
|
|
582
|
+
let data = store.data();
|
|
583
|
+
self.last_usage
|
|
584
|
+
.set((data.wall_time().as_secs_f64(), data.memory_peak()));
|
|
636
585
|
result
|
|
637
586
|
}
|
|
638
587
|
|
|
@@ -643,173 +592,211 @@ impl Runtime {
|
|
|
643
592
|
/// effectively never fires.
|
|
644
593
|
///
|
|
645
594
|
/// Also captures the current linear-memory size as the baseline
|
|
646
|
-
/// for the docs/behavior.md E-20 per-invocation memory delta cap
|
|
647
|
-
///
|
|
648
|
-
///
|
|
649
|
-
///
|
|
650
|
-
///
|
|
651
|
-
///
|
|
652
|
-
/// Also stamps the wall-clock entry instant for the
|
|
653
|
-
/// docs/behavior.md B-35 `wall_time` measurement. The bracket
|
|
654
|
-
/// closes in `Runtime::disarm_caps` so it matches the
|
|
655
|
-
/// `timeout` deadline window and excludes `OUTCOME_BUFFER`
|
|
595
|
+
/// for the docs/behavior.md E-20 per-invocation memory delta cap —
|
|
596
|
+
/// the pre-initialized image's allocation is folded into the
|
|
597
|
+
/// baseline rather than the budget — and stamps the wall-clock
|
|
598
|
+
/// entry instant for the docs/behavior.md B-35 `wall_time`
|
|
599
|
+
/// measurement. The bracket closes in `disarm_caps` so it matches
|
|
600
|
+
/// the `timeout` deadline window and excludes `OUTCOME_BUFFER`
|
|
656
601
|
/// decoding and stdout / stderr capture readout.
|
|
657
|
-
fn prime_caps(&self) {
|
|
658
|
-
let mut store_ref = self.store.borrow_mut();
|
|
602
|
+
fn prime_caps(&self, store: &mut WtStore<Invocation>, exports: &Exports) {
|
|
659
603
|
match self.config.timeout {
|
|
660
604
|
Some(timeout) => {
|
|
661
605
|
let deadline = Instant::now() + timeout;
|
|
662
|
-
|
|
663
|
-
|
|
606
|
+
store.data_mut().set_deadline(Some(deadline));
|
|
607
|
+
store.set_epoch_deadline(1);
|
|
664
608
|
}
|
|
665
609
|
None => {
|
|
666
|
-
|
|
667
|
-
|
|
610
|
+
store.data_mut().set_deadline(None);
|
|
611
|
+
store.set_epoch_deadline(u64::MAX);
|
|
668
612
|
}
|
|
669
613
|
}
|
|
670
|
-
let baseline = match
|
|
671
|
-
Some(
|
|
672
|
-
|
|
614
|
+
let baseline = match exports.memory {
|
|
615
|
+
Some(m) => m.data_size(store.as_context_mut()),
|
|
616
|
+
None => 0,
|
|
673
617
|
};
|
|
674
|
-
|
|
675
|
-
|
|
618
|
+
store.data_mut().arm_memory_cap(baseline);
|
|
619
|
+
store.data_mut().start_wall_clock();
|
|
676
620
|
}
|
|
677
621
|
|
|
678
|
-
///
|
|
679
|
-
///
|
|
680
|
-
///
|
|
681
|
-
/// the
|
|
682
|
-
///
|
|
683
|
-
///
|
|
684
|
-
fn
|
|
685
|
-
|
|
686
|
-
|
|
687
|
-
|
|
622
|
+
/// Collect every per-invocation observable into a fresh `Snapshot`.
|
|
623
|
+
/// Called from the run-path methods after the guest export returns
|
|
624
|
+
/// successfully: drains OUTCOME_BUFFER via `__kobako_take_outcome`
|
|
625
|
+
/// and snapshots the per-channel stdout / stderr pipes (clipped to
|
|
626
|
+
/// their caps). The B-35 usage figures were already stashed by
|
|
627
|
+
/// `call_with_caps`.
|
|
628
|
+
fn build_snapshot(
|
|
629
|
+
&self,
|
|
630
|
+
ruby: &Ruby,
|
|
631
|
+
store: &mut WtStore<Invocation>,
|
|
632
|
+
exports: &Exports,
|
|
633
|
+
) -> Result<Snapshot, MagnusError> {
|
|
634
|
+
let return_bytes = fetch_outcome_bytes(ruby, store, exports)?;
|
|
635
|
+
let data = store.data();
|
|
636
|
+
let (stdout_raw, stderr_raw, wall_time, memory_peak) = (
|
|
637
|
+
data.stdout_bytes(),
|
|
638
|
+
data.stderr_bytes(),
|
|
639
|
+
data.wall_time(),
|
|
640
|
+
data.memory_peak(),
|
|
641
|
+
);
|
|
642
|
+
let (stdout_visible, stdout_truncated) =
|
|
643
|
+
capture::clip_capture(&stdout_raw, self.config.stdout_limit_bytes);
|
|
644
|
+
let stdout_bytes = stdout_visible.to_vec();
|
|
645
|
+
let (stderr_visible, stderr_truncated) =
|
|
646
|
+
capture::clip_capture(&stderr_raw, self.config.stderr_limit_bytes);
|
|
647
|
+
let stderr_bytes = stderr_visible.to_vec();
|
|
648
|
+
Ok(Snapshot::new(
|
|
649
|
+
return_bytes,
|
|
650
|
+
stdout_bytes,
|
|
651
|
+
stdout_truncated,
|
|
652
|
+
stderr_bytes,
|
|
653
|
+
stderr_truncated,
|
|
654
|
+
wall_time,
|
|
655
|
+
memory_peak,
|
|
656
|
+
))
|
|
688
657
|
}
|
|
658
|
+
}
|
|
689
659
|
|
|
690
|
-
|
|
691
|
-
|
|
692
|
-
|
|
693
|
-
|
|
694
|
-
|
|
695
|
-
|
|
696
|
-
|
|
697
|
-
|
|
698
|
-
|
|
699
|
-
|
|
700
|
-
guest_mem::checked_payload_len(bytes.len()).map_err(|msg| trap_err(ruby, msg))?;
|
|
701
|
-
|
|
702
|
-
let mut store_ref = self.store.borrow_mut();
|
|
703
|
-
let alloc: TypedFunc<u32, u32> = self
|
|
704
|
-
.inner
|
|
705
|
-
.get_typed_func(store_ref.as_context_mut(), "__kobako_alloc")
|
|
706
|
-
.map_err(|_| trap_err(ruby, SANDBOX_RUNTIME_MISSING_HOOKS))?;
|
|
707
|
-
let ptr = alloc
|
|
708
|
-
.call(store_ref.as_context_mut(), bytes.len() as u32)
|
|
709
|
-
.map_err(|e| trap_err(ruby, format!("failed to allocate input buffer: {}", e)))?;
|
|
710
|
-
if ptr == 0 {
|
|
711
|
-
return Err(sandbox_err(
|
|
712
|
-
ruby,
|
|
713
|
-
"could not allocate input buffer (out of memory)",
|
|
714
|
-
));
|
|
715
|
-
}
|
|
660
|
+
/// Drop the memory cap as soon as the guest call returns so that
|
|
661
|
+
/// any post-run host bookkeeping (e.g. fetching the OUTCOME_BUFFER,
|
|
662
|
+
/// which can grow guest memory transiently) is not attributed to
|
|
663
|
+
/// the user script. Also closes the docs/behavior.md B-35
|
|
664
|
+
/// `wall_time` bracket opened by `Runtime::prime_caps`. Paired
|
|
665
|
+
/// with `Runtime::prime_caps`.
|
|
666
|
+
fn disarm_caps(store: &mut WtStore<Invocation>) {
|
|
667
|
+
store.data_mut().stop_wall_clock();
|
|
668
|
+
store.data_mut().disarm_memory_cap();
|
|
669
|
+
}
|
|
716
670
|
|
|
717
|
-
|
|
718
|
-
|
|
719
|
-
|
|
720
|
-
|
|
721
|
-
|
|
722
|
-
|
|
723
|
-
|
|
724
|
-
|
|
671
|
+
/// Return the resolved `memory` export handle, or raise
|
|
672
|
+
/// `Kobako::TrapError` when the loaded module exports no linear
|
|
673
|
+
/// memory — the "not a Kobako-shaped runtime" failure mode
|
|
674
|
+
/// (`SANDBOX_RUNTIME_NOT_KOBAKO`).
|
|
675
|
+
fn require_memory(ruby: &Ruby, exports: &Exports) -> Result<Memory, MagnusError> {
|
|
676
|
+
exports
|
|
677
|
+
.memory
|
|
678
|
+
.ok_or_else(|| trap_err(ruby, SANDBOX_RUNTIME_NOT_KOBAKO))
|
|
679
|
+
}
|
|
725
680
|
|
|
726
|
-
|
|
681
|
+
/// Allocate a `len`-byte buffer in guest linear memory via
|
|
682
|
+
/// `__kobako_alloc`, copy `envelope` into it, and return `(ptr, len)`
|
|
683
|
+
/// as `i32` values matching the `__kobako_run(env_ptr, env_len)` ABI.
|
|
684
|
+
/// Raises `Kobako::TrapError` when the allocation hook is missing or
|
|
685
|
+
/// itself traps, and `Kobako::SandboxError` when the hook runs but
|
|
686
|
+
/// cannot reserve the buffer (`__kobako_alloc` returns 0,
|
|
687
|
+
/// docs/behavior.md E-31) — an intact runtime, not an engine fault.
|
|
688
|
+
fn write_envelope(
|
|
689
|
+
ruby: &Ruby,
|
|
690
|
+
store: &mut WtStore<Invocation>,
|
|
691
|
+
exports: &Exports,
|
|
692
|
+
envelope: RString,
|
|
693
|
+
) -> Result<(i32, i32), MagnusError> {
|
|
694
|
+
let bytes = rstring_to_vec(envelope);
|
|
695
|
+
let len_i32 = guest_mem::checked_payload_len(bytes.len()).map_err(|msg| trap_err(ruby, msg))?;
|
|
696
|
+
|
|
697
|
+
let alloc = require_export(ruby, exports.alloc.as_ref())?;
|
|
698
|
+
let memory = require_memory(ruby, exports)?;
|
|
699
|
+
|
|
700
|
+
let ptr = alloc
|
|
701
|
+
.call(store.as_context_mut(), bytes.len() as u32)
|
|
702
|
+
.map_err(|e| trap_err(ruby, format!("failed to allocate input buffer: {}", e)))?;
|
|
703
|
+
if ptr == 0 {
|
|
704
|
+
return Err(sandbox_err(
|
|
705
|
+
ruby,
|
|
706
|
+
"could not allocate input buffer (out of memory)",
|
|
707
|
+
));
|
|
727
708
|
}
|
|
709
|
+
let data = memory.data_mut(store.as_context_mut());
|
|
710
|
+
let range = guest_mem::guest_buffer_range(ptr as usize, bytes.len(), data.len())
|
|
711
|
+
.map_err(|msg| trap_err(ruby, msg))?;
|
|
712
|
+
data[range].copy_from_slice(&bytes);
|
|
728
713
|
|
|
729
|
-
|
|
730
|
-
|
|
731
|
-
/// docs/wire-codec.md § Invocation channels) plus fresh stdout / stderr
|
|
732
|
-
/// pipes. Called at the top of every guest invocation: `#eval` passes
|
|
733
|
-
/// three frames (preamble, source, snippets), `#run` passes two
|
|
734
|
-
/// (preamble, snippets — the invocation envelope arrives via linear
|
|
735
|
-
/// memory instead). Each output pipe is sized at `cap + 1` so
|
|
736
|
-
/// `capture::clip_capture` can distinguish "wrote exactly cap
|
|
737
|
-
/// bytes" from "exceeded cap"; uncapped channels fall back
|
|
738
|
-
/// to `usize::MAX` and rely on `memory_limit` (docs/behavior.md E-20)
|
|
739
|
-
/// for the real ceiling. Raises `Kobako::TrapError` when any frame
|
|
740
|
-
/// exceeds the 16 MiB cap that keeps its `u32` length prefix from
|
|
741
|
-
/// wrapping.
|
|
742
|
-
fn refresh_wasi(&self, frames: &[Vec<u8>]) -> Result<(), MagnusError> {
|
|
743
|
-
let ruby = Ruby::get().expect("Ruby thread");
|
|
744
|
-
// Every frame carries the same 16 MiB cap as the `#run` envelope
|
|
745
|
-
// (`write_envelope`): the length prefix is a `u32`, so a frame past
|
|
746
|
-
// the cap would silently wrap and corrupt the stdin frame stream.
|
|
747
|
-
for frame in frames {
|
|
748
|
-
guest_mem::checked_payload_len(frame.len()).map_err(|msg| trap_err(&ruby, msg))?;
|
|
749
|
-
}
|
|
714
|
+
Ok((ptr as i32, len_i32))
|
|
715
|
+
}
|
|
750
716
|
|
|
751
|
-
|
|
752
|
-
|
|
753
|
-
|
|
754
|
-
|
|
755
|
-
|
|
756
|
-
|
|
717
|
+
/// Build the per-invocation WASI context with stdin carrying every frame
|
|
718
|
+
/// in `frames` (each prefixed by its 4-byte big-endian u32 length —
|
|
719
|
+
/// docs/wire-codec.md § Invocation channels) plus fresh stdout / stderr
|
|
720
|
+
/// pipes, and install it on the invocation's Store. `#eval` passes three
|
|
721
|
+
/// frames (preamble, source, snippets), `#run` passes two (preamble,
|
|
722
|
+
/// snippets — the invocation envelope arrives via linear memory
|
|
723
|
+
/// instead). Each output pipe is sized at `cap + 1` so
|
|
724
|
+
/// `capture::clip_capture` can distinguish "wrote exactly cap bytes"
|
|
725
|
+
/// from "exceeded cap"; uncapped channels fall back to `usize::MAX` and
|
|
726
|
+
/// rely on `memory_limit` (docs/behavior.md E-20) for the real ceiling.
|
|
727
|
+
/// Raises `Kobako::TrapError` when any frame exceeds the 16 MiB cap that
|
|
728
|
+
/// keeps its `u32` length prefix from wrapping.
|
|
729
|
+
fn install_wasi_frames(
|
|
730
|
+
store: &mut WtStore<Invocation>,
|
|
731
|
+
config: &Config,
|
|
732
|
+
frames: &[Vec<u8>],
|
|
733
|
+
) -> Result<(), MagnusError> {
|
|
734
|
+
let ruby = Ruby::get().expect("Ruby thread");
|
|
735
|
+
// Every frame carries the same 16 MiB cap as the `#run` envelope
|
|
736
|
+
// (`write_envelope`): the length prefix is a `u32`, so a frame past
|
|
737
|
+
// the cap would silently wrap and corrupt the stdin frame stream.
|
|
738
|
+
for frame in frames {
|
|
739
|
+
guest_mem::checked_payload_len(frame.len()).map_err(|msg| trap_err(&ruby, msg))?;
|
|
740
|
+
}
|
|
757
741
|
|
|
758
|
-
|
|
759
|
-
|
|
760
|
-
|
|
761
|
-
|
|
762
|
-
|
|
763
|
-
|
|
764
|
-
let mut builder = WasiCtxBuilder::new();
|
|
765
|
-
builder.stdin(stdin_pipe);
|
|
766
|
-
builder.stdout(stdout_pipe.clone());
|
|
767
|
-
builder.stderr(stderr_pipe.clone());
|
|
768
|
-
// Deny the preview1 ambient-authority imports the guest never legitimately
|
|
769
|
-
// reaches but the WASI layer would otherwise grant (see `ambient`).
|
|
770
|
-
builder.wall_clock(ambient::FrozenWallClock);
|
|
771
|
-
builder.monotonic_clock(ambient::FrozenMonotonicClock);
|
|
772
|
-
builder.secure_random(ambient::deterministic_rng());
|
|
773
|
-
let wasi = builder.build_p1();
|
|
774
|
-
|
|
775
|
-
self.store
|
|
776
|
-
.borrow_mut()
|
|
777
|
-
.data_mut()
|
|
778
|
-
.install_wasi(wasi, stdout_pipe, stderr_pipe);
|
|
779
|
-
Ok(())
|
|
742
|
+
let total: usize = frames.iter().map(|f| 4 + f.len()).sum();
|
|
743
|
+
let mut stdin_content: Vec<u8> = Vec::with_capacity(total);
|
|
744
|
+
for frame in frames {
|
|
745
|
+
stdin_content.extend_from_slice(&(frame.len() as u32).to_be_bytes());
|
|
746
|
+
stdin_content.extend_from_slice(frame);
|
|
780
747
|
}
|
|
781
748
|
|
|
782
|
-
|
|
783
|
-
|
|
784
|
-
|
|
785
|
-
|
|
786
|
-
|
|
787
|
-
|
|
788
|
-
|
|
789
|
-
|
|
790
|
-
|
|
791
|
-
|
|
792
|
-
|
|
793
|
-
|
|
794
|
-
|
|
795
|
-
|
|
796
|
-
|
|
797
|
-
|
|
798
|
-
|
|
749
|
+
let stdin_pipe = MemoryInputPipe::new(stdin_content);
|
|
750
|
+
let stdout_pipe = MemoryOutputPipe::new(capture::pipe_capacity(config.stdout_limit_bytes));
|
|
751
|
+
let stderr_pipe = MemoryOutputPipe::new(capture::pipe_capacity(config.stderr_limit_bytes));
|
|
752
|
+
|
|
753
|
+
let mut builder = WasiCtxBuilder::new();
|
|
754
|
+
builder.stdin(stdin_pipe);
|
|
755
|
+
builder.stdout(stdout_pipe.clone());
|
|
756
|
+
builder.stderr(stderr_pipe.clone());
|
|
757
|
+
// Deny the preview1 ambient-authority imports the guest never legitimately
|
|
758
|
+
// reaches but the WASI layer would otherwise grant (see `ambient`).
|
|
759
|
+
builder.wall_clock(ambient::FrozenWallClock);
|
|
760
|
+
builder.monotonic_clock(ambient::FrozenMonotonicClock);
|
|
761
|
+
builder.secure_random(ambient::deterministic_rng());
|
|
762
|
+
let wasi = builder.build_p1();
|
|
763
|
+
|
|
764
|
+
store
|
|
765
|
+
.data_mut()
|
|
766
|
+
.install_wasi(wasi, stdout_pipe, stderr_pipe);
|
|
767
|
+
Ok(())
|
|
768
|
+
}
|
|
799
769
|
|
|
800
|
-
|
|
801
|
-
|
|
802
|
-
|
|
803
|
-
|
|
804
|
-
|
|
805
|
-
|
|
806
|
-
|
|
807
|
-
|
|
808
|
-
|
|
809
|
-
|
|
810
|
-
|
|
811
|
-
|
|
770
|
+
/// Invoke `__kobako_take_outcome`, decode the packed `(ptr<<32)|len`
|
|
771
|
+
/// u64, and copy the OUTCOME_BUFFER slice out of guest memory. Raises
|
|
772
|
+
/// `Kobako::TrapError` when the export is missing, `len` exceeds the
|
|
773
|
+
/// 16 MiB single-dispatch cap, the `ptr`/`len` arithmetic overflows,
|
|
774
|
+
/// the slice falls outside live memory, or the `memory` export itself
|
|
775
|
+
/// is absent.
|
|
776
|
+
fn fetch_outcome_bytes(
|
|
777
|
+
ruby: &Ruby,
|
|
778
|
+
store: &mut WtStore<Invocation>,
|
|
779
|
+
exports: &Exports,
|
|
780
|
+
) -> Result<Vec<u8>, MagnusError> {
|
|
781
|
+
let take = require_export(ruby, exports.take_outcome.as_ref())?;
|
|
782
|
+
let mem = require_memory(ruby, exports)?;
|
|
783
|
+
|
|
784
|
+
let packed = take
|
|
785
|
+
.call(store.as_context_mut(), ())
|
|
786
|
+
.map_err(|e| trap_err(ruby, format!("failed to read the Sandbox result: {}", e)))?;
|
|
787
|
+
let (ptr, len) = guest_mem::unpack_outcome_packed(packed);
|
|
788
|
+
if len > guest_mem::MAX_DISPATCH_PAYLOAD {
|
|
789
|
+
return Err(trap_err(ruby, "result payload exceeds the 16 MiB limit"));
|
|
812
790
|
}
|
|
791
|
+
|
|
792
|
+
let data = mem.data(store.as_context_mut());
|
|
793
|
+
let range = guest_mem::guest_buffer_range(ptr, len, data.len()).map_err(|msg| {
|
|
794
|
+
trap_err(
|
|
795
|
+
ruby,
|
|
796
|
+
format!("the Sandbox result is out of bounds: {}", msg),
|
|
797
|
+
)
|
|
798
|
+
})?;
|
|
799
|
+
Ok(data[range].to_vec())
|
|
813
800
|
}
|
|
814
801
|
|
|
815
802
|
/// User-facing message for the "Sandbox runtime is missing one of the
|
|
@@ -829,7 +816,7 @@ const SANDBOX_RUNTIME_MISSING_HOOKS: &str = "Sandbox runtime is missing required
|
|
|
829
816
|
const SANDBOX_RUNTIME_NOT_KOBAKO: &str =
|
|
830
817
|
"the loaded Wasm module is not a Kobako-compatible runtime";
|
|
831
818
|
|
|
832
|
-
/// Return the
|
|
819
|
+
/// Return the resolved `TypedFunc` for an ABI export, or raise
|
|
833
820
|
/// `Kobako::TrapError` when the option is `None`. Both run-path
|
|
834
821
|
/// methods (`#eval`, `#run`) plus the `build_snapshot` readout that
|
|
835
822
|
/// drains `OUTCOME_BUFFER` share the same "missing export → Ruby
|