kobako 0.31.0 → 0.32.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/.githooks/linux-tests.sh +29 -0
- data/.release-please-manifest.json +1 -1
- data/CHANGELOG.md +125 -0
- data/Cargo.lock +457 -494
- data/README.md +2 -3
- data/ROADMAP.md +0 -1
- data/crates/kobako-runtime/CHANGELOG.md +90 -0
- data/crates/kobako-runtime/Cargo.toml +14 -15
- data/crates/kobako-runtime/README.md +1 -1
- data/crates/kobako-runtime/src/cred.rs +32 -20
- data/crates/kobako-runtime/src/filesystem/bind/fid.rs +300 -22
- data/crates/kobako-runtime/src/filesystem/bind/lookup.rs +56 -22
- data/crates/kobako-runtime/src/filesystem/bind/unicode.rs +109 -0
- data/crates/kobako-runtime/src/filesystem/bind/walk.rs +33 -14
- data/crates/kobako-runtime/src/filesystem/bind.rs +393 -220
- data/crates/kobako-runtime/src/filesystem/overlay/stack/copy_up.rs +2 -0
- data/crates/kobako-runtime/src/filesystem/overlay/stack.rs +9 -10
- data/crates/kobako-runtime/src/filesystem/overlay.rs +277 -46
- data/crates/kobako-runtime/src/filesystem/rdir.rs +8 -27
- data/crates/kobako-runtime/src/filesystem/testing.rs +31 -0
- data/crates/kobako-runtime/src/filesystem/tmpfs.rs +47 -13
- data/crates/kobako-runtime/src/filesystem.rs +2 -0
- data/crates/kobako-runtime/src/fork.rs +239 -16
- data/crates/kobako-runtime/src/limits.rs +17 -41
- data/crates/kobako-runtime/src/locking/seqlock.rs +29 -3
- data/crates/kobako-runtime/src/locking.rs +508 -113
- data/crates/kobako-runtime/src/memcg.rs +83 -160
- data/crates/kobako-runtime/src/mount.rs +0 -30
- data/crates/kobako-runtime/src/net/connections.rs +53 -13
- data/crates/kobako-runtime/src/net/egress.rs +44 -24
- data/crates/kobako-runtime/src/net/http.rs +888 -456
- data/crates/kobako-runtime/src/net/lookup.rs +150 -50
- data/crates/kobako-runtime/src/net/namespace.rs +97 -95
- data/crates/kobako-runtime/src/net/reactor.rs +298 -203
- data/crates/kobako-runtime/src/net/resolv/darwin.rs +8 -3
- data/crates/kobako-runtime/src/net/resolv/stub/tests.rs +50 -0
- data/crates/kobako-runtime/src/net/resolv/stub.rs +29 -23
- data/crates/kobako-runtime/src/net/resolv/windows.rs +14 -44
- data/crates/kobako-runtime/src/net/resolv.rs +39 -15
- data/crates/kobako-runtime/src/net/skbuff.rs +109 -0
- data/crates/kobako-runtime/src/net/socket.rs +51 -52
- data/crates/kobako-runtime/src/net/task.rs +223 -0
- data/crates/kobako-runtime/src/net/tcp.rs +410 -158
- data/crates/kobako-runtime/src/net/tls.rs +65 -3
- data/crates/kobako-runtime/src/net/transport.rs +72 -47
- data/crates/kobako-runtime/src/net/udp.rs +231 -129
- data/crates/kobako-runtime/src/net.rs +3 -0
- data/crates/kobako-runtime/src/sched.rs +244 -58
- data/crates/kobako-runtime/src/security.rs +27 -16
- data/crates/kobako-runtime/src/vfs/dentry.rs +80 -41
- data/crates/kobako-runtime/src/vfs/file.rs +52 -53
- data/crates/kobako-runtime/src/vfs/kern_path.rs +24 -11
- data/crates/kobako-runtime/src/vfs/namei.rs +399 -184
- data/crates/kobako-runtime/src/vfs/namespace.rs +123 -40
- data/crates/kobako-runtime/src/vfs/stat.rs +105 -16
- data/crates/kobako-runtime/src/vfs.rs +1 -0
- data/crates/kobako-runtime/src/wait.rs +75 -50
- data/crates/kobako-transport/CHANGELOG.md +7 -0
- data/crates/kobako-transport/Cargo.toml +1 -1
- data/crates/kobako-transport/README.md +1 -1
- data/crates/kobako-wasmtime/CHANGELOG.md +47 -0
- data/crates/kobako-wasmtime/Cargo.toml +27 -6
- data/crates/kobako-wasmtime/README.md +3 -3
- data/crates/kobako-wasmtime/src/abi.rs +2 -1
- data/crates/kobako-wasmtime/src/ambient.rs +11 -2
- data/crates/kobako-wasmtime/src/cache/unix.rs +95 -0
- data/crates/kobako-wasmtime/src/cache/windows.rs +359 -0
- data/crates/kobako-wasmtime/src/cache.rs +88 -103
- data/crates/kobako-wasmtime/src/driver.rs +40 -27
- data/crates/kobako-wasmtime/src/invocation.rs +21 -7
- data/crates/kobako-wasmtime/src/lib.rs +2 -0
- data/crates/kobako-wasmtime/src/limiter.rs +78 -25
- data/crates/kobako-wasmtime/src/preview1/descriptors.rs +13 -17
- data/crates/kobako-wasmtime/src/preview1/differential/mount.rs +4 -5
- data/crates/kobako-wasmtime/src/preview1/differential.rs +1 -1
- data/crates/kobako-wasmtime/src/preview1/fd.rs +23 -19
- data/crates/kobako-wasmtime/src/preview1/mount_view.rs +130 -197
- data/crates/kobako-wasmtime/src/preview1/path.rs +3 -3
- data/crates/kobako-wasmtime/src/preview1/poll.rs +28 -22
- data/crates/kobako-wasmtime/src/preview1/random.rs +125 -0
- data/crates/kobako-wasmtime/src/preview1/testing.rs +31 -0
- data/crates/kobako-wasmtime/src/preview1.rs +85 -9
- data/crates/kobako-wasmtime/src/preview2/filesystem.rs +2 -2
- data/crates/kobako-wasmtime/src/preview2/host.rs +609 -249
- data/crates/kobako-wasmtime/src/preview2/http.rs +299 -215
- data/crates/kobako-wasmtime/src/preview2/io.rs +17 -4
- data/crates/kobako-wasmtime/src/preview2/table.rs +24 -72
- data/crates/kobako-wasmtime/src/trap.rs +68 -23
- data/crates/kobako-wasmtime/src/waits.rs +59 -0
- data/crates/kobako-wasmtime/src/wasi.rs +91 -10
- data/crates/kobako-wasmtime/witx/wasi_snapshot_preview1.witx +13 -0
- data/data/kobako.wasm +0 -0
- data/ext/kobako/Cargo.toml +6 -2
- data/ext/kobako/src/escape.rs +332 -26
- data/ext/kobako/src/filesystem/grants.rs +5 -7
- data/ext/kobako/src/filesystem/ruby.rs +10 -3
- data/ext/kobako/src/filesystem.rs +47 -8
- data/ext/kobako/src/fork.rs +72 -0
- data/ext/kobako/src/gvl.rs +499 -76
- data/ext/kobako/src/handoff.rs +490 -0
- data/ext/kobako/src/lib.rs +10 -0
- data/ext/kobako/src/network.rs +5 -3
- data/ext/kobako/src/pinned_value.rs +28 -15
- data/ext/kobako/src/runtime/bridge.rs +32 -25
- data/ext/kobako/src/runtime.rs +74 -43
- data/lib/kobako/catalog/handles.rb +9 -5
- data/lib/kobako/codec/decoder.rb +1 -1
- data/lib/kobako/codec/state.rb +2 -18
- data/lib/kobako/codec/value_budget.rb +4 -4
- data/lib/kobako/codec.rb +5 -6
- data/lib/kobako/errors.rb +4 -7
- data/lib/kobako/filesystem.rb +1 -1
- data/lib/kobako/fork.rb +42 -0
- data/lib/kobako/handle.rb +1 -2
- data/lib/kobako/sandbox.rb +17 -36
- data/lib/kobako/sandbox_options.rb +7 -9
- data/lib/kobako/usage.rb +4 -2
- data/lib/kobako/version.rb +1 -1
- data/lib/kobako.rb +2 -2
- data/sig/kobako/catalog/handles.rbs +2 -0
- data/sig/kobako/codec/state.rbs +0 -4
- data/sig/kobako/codec/value_budget.rbs +1 -1
- data/sig/kobako/codec.rbs +2 -2
- data/sig/kobako/errors.rbs +0 -3
- data/sig/kobako/fork.rbs +17 -0
- data/sig/kobako/runtime.rbs +2 -2
- data/sig/kobako/sandbox.rbs +3 -12
- data/sig/kobako/sandbox_options.rbs +0 -2
- metadata +15 -4
- data/lib/kobako/pool.rb +0 -162
- data/sig/core/thread.rbs +0 -6
- data/sig/kobako/pool.rbs +0 -46
|
@@ -9,7 +9,6 @@ use core::cell::{Cell, RefCell};
|
|
|
9
9
|
use core::ptr::NonNull;
|
|
10
10
|
use std::sync::Arc;
|
|
11
11
|
|
|
12
|
-
use magnus::rb_sys::AsRawValue;
|
|
13
12
|
use magnus::value::{Opaque, ReprValue};
|
|
14
13
|
use magnus::{
|
|
15
14
|
method, prelude::*, Error as MagnusError, RModule, RString, Ruby, Symbol, TypedData, Value,
|
|
@@ -93,9 +92,9 @@ impl OpenFrames {
|
|
|
93
92
|
/// belongs to does, before `call` reaches a frame pointer no longer on the
|
|
94
93
|
/// stack.
|
|
95
94
|
///
|
|
96
|
-
/// The guest's Store is entered
|
|
97
|
-
/// invocation, and only
|
|
98
|
-
///
|
|
95
|
+
/// The guest's Store is re-entered for the fiber that drives the
|
|
96
|
+
/// invocation, and only for it, on the guest's own thread where its frames
|
|
97
|
+
/// stand: the open dispatch frames are that fiber's. A
|
|
99
98
|
/// block run from any other fiber, including one on another thread, is
|
|
100
99
|
/// refused before it reaches the guest.
|
|
101
100
|
///
|
|
@@ -107,7 +106,10 @@ struct GuestYielder {
|
|
|
107
106
|
frame: u32,
|
|
108
107
|
open: Arc<OpenFrames>,
|
|
109
108
|
live: Cell<bool>,
|
|
110
|
-
fiber:
|
|
109
|
+
fiber: crate::escape::FiberId,
|
|
110
|
+
/// The line to the guest whose frames the block re-enters, whose own
|
|
111
|
+
/// thread it runs on.
|
|
112
|
+
guest: Option<Arc<crate::handoff::Line>>,
|
|
111
113
|
}
|
|
112
114
|
|
|
113
115
|
// SAFETY: magnus requires `Send + Sync` on wrapped types. `call` runs only
|
|
@@ -118,12 +120,13 @@ unsafe impl Send for GuestYielder {}
|
|
|
118
120
|
unsafe impl Sync for GuestYielder {}
|
|
119
121
|
|
|
120
122
|
impl GuestYielder {
|
|
121
|
-
fn new(
|
|
123
|
+
fn new(frame: u32, open: Arc<OpenFrames>) -> Self {
|
|
122
124
|
Self {
|
|
123
125
|
frame,
|
|
124
126
|
open,
|
|
125
127
|
live: Cell::new(true),
|
|
126
|
-
fiber:
|
|
128
|
+
fiber: crate::escape::fiber_id(),
|
|
129
|
+
guest: crate::escape::current_run().and_then(|run| run.innermost_line()),
|
|
127
130
|
}
|
|
128
131
|
}
|
|
129
132
|
|
|
@@ -144,7 +147,7 @@ impl GuestYielder {
|
|
|
144
147
|
"guest block invoked after host dispatch frame returned",
|
|
145
148
|
));
|
|
146
149
|
};
|
|
147
|
-
if
|
|
150
|
+
if crate::escape::fiber_id() != self.fiber {
|
|
148
151
|
return Err(MagnusError::new(
|
|
149
152
|
ruby.exception_thread_error(),
|
|
150
153
|
"guest block called from a thread or fiber other than its invocation's",
|
|
@@ -156,9 +159,13 @@ impl GuestYielder {
|
|
|
156
159
|
// Only the innermost frame's Ruby code runs, so no other borrow of
|
|
157
160
|
// that Yielder is live; the borrow ends with this method.
|
|
158
161
|
let yielder: &mut dyn Yielder = unsafe { ptr.as_mut() };
|
|
159
|
-
let
|
|
160
|
-
.
|
|
161
|
-
.
|
|
162
|
+
let guest = self
|
|
163
|
+
.guest
|
|
164
|
+
.as_ref()
|
|
165
|
+
.expect("a guest block is made inside a run");
|
|
166
|
+
let reply =
|
|
167
|
+
crate::gvl::on_guest_stack(guest, || yielder.yield_to_block(self.frame, &bytes))
|
|
168
|
+
.map_err(|t| crate::errors::from_trap(&ruby, t))?;
|
|
162
169
|
Ok(match reply {
|
|
163
170
|
YieldReply::Ok(body) => (ruby.to_symbol(ARM_OK), ruby.str_from_slice(&body), None),
|
|
164
171
|
YieldReply::Break => (ruby.to_symbol(ARM_BREAK), ruby.str_from_slice(&[]), None),
|
|
@@ -201,17 +208,15 @@ impl RubyDispatchHandler {
|
|
|
201
208
|
impl DispatchHandler for RubyDispatchHandler {
|
|
202
209
|
/// Ruby decodes only the payload, through the codec it already owns,
|
|
203
210
|
/// which keeps a large payload's strings shared with the buffer rather
|
|
204
|
-
/// than copied. The Proc folds every `StandardError`
|
|
205
|
-
/// fault arm, so
|
|
206
|
-
/// `None
|
|
211
|
+
/// than copied. The Proc folds every `StandardError` a Service raises into
|
|
212
|
+
/// the Reply's fault arm, so whatever escapes it is held for the host and
|
|
213
|
+
/// answered as `None`, ending the invocation.
|
|
207
214
|
fn dispatch(&self, call: Call<'_>, yielder: &mut dyn Yielder) -> Option<CallReply> {
|
|
208
|
-
// The guest
|
|
209
|
-
//
|
|
210
|
-
//
|
|
215
|
+
// The guest's thread hands the whole callback to the Fiber that called
|
|
216
|
+
// the invocation (#eval or #run), which holds the GVL, before any Ruby
|
|
217
|
+
// VALUE is touched; `None` once that caller is gone.
|
|
211
218
|
crate::gvl::reenter(|| {
|
|
212
|
-
//
|
|
213
|
-
// the active Sandbox invocation (#eval or #run), so `Ruby::get()`
|
|
214
|
-
// is available here once the GVL is held.
|
|
219
|
+
// `Ruby::get()` is available here, on the run's caller.
|
|
215
220
|
// Panicking with `expect` localises the violation rather than
|
|
216
221
|
// letting a nonsense error propagate.
|
|
217
222
|
let ruby = Ruby::get().expect("Ruby handle unavailable in the upcall");
|
|
@@ -228,7 +233,7 @@ impl DispatchHandler for RubyDispatchHandler {
|
|
|
228
233
|
// invalidate it the instant the Proc returns, so a guest block that
|
|
229
234
|
// escapes the dispatch frame never re-enters the guest.
|
|
230
235
|
let guest_yielder =
|
|
231
|
-
ruby.obj_wrap(GuestYielder::new(
|
|
236
|
+
ruby.obj_wrap(GuestYielder::new(yielder.frame(), self.open.clone()));
|
|
232
237
|
let answer: Result<(Symbol, RString, Option<RString>), magnus::Error> =
|
|
233
238
|
self.open.lend(yielder, || {
|
|
234
239
|
proc_value.funcall(
|
|
@@ -243,13 +248,14 @@ impl DispatchHandler for RubyDispatchHandler {
|
|
|
243
248
|
)
|
|
244
249
|
});
|
|
245
250
|
guest_yielder.invalidate();
|
|
246
|
-
// The Proc
|
|
247
|
-
//
|
|
248
|
-
//
|
|
251
|
+
// The Proc folds every StandardError a Service raises into a
|
|
252
|
+
// fault, so what escapes it was never the Service's: an interrupt
|
|
253
|
+
// landing before the Proc's own rescue, or the host's own
|
|
254
|
+
// exception. Either is held for the host.
|
|
249
255
|
let (arm, body, kind) = match answer {
|
|
250
256
|
Ok(answer) => answer,
|
|
251
257
|
Err(error) => {
|
|
252
|
-
|
|
258
|
+
crate::escape::keep(error);
|
|
253
259
|
return None;
|
|
254
260
|
}
|
|
255
261
|
};
|
|
@@ -270,6 +276,7 @@ impl DispatchHandler for RubyDispatchHandler {
|
|
|
270
276
|
_ => None,
|
|
271
277
|
}
|
|
272
278
|
})
|
|
279
|
+
.flatten()
|
|
273
280
|
}
|
|
274
281
|
|
|
275
282
|
fn abandoned(&self) -> bool {
|
data/ext/kobako/src/runtime.rs
CHANGED
|
@@ -27,6 +27,7 @@ use magnus::{
|
|
|
27
27
|
};
|
|
28
28
|
|
|
29
29
|
use std::path::Path;
|
|
30
|
+
use std::sync::atomic::{AtomicUsize, Ordering};
|
|
30
31
|
use std::sync::Arc;
|
|
31
32
|
use std::time::Duration;
|
|
32
33
|
|
|
@@ -101,11 +102,11 @@ pub(crate) fn register(ruby: &Ruby, kobako: RModule) -> Result<(), MagnusError>
|
|
|
101
102
|
runtime.define_singleton_method("from_path", function!(Runtime::from_path, 2))?;
|
|
102
103
|
runtime.define_singleton_method("default_options", function!(Runtime::default_options, 0))?;
|
|
103
104
|
runtime.define_singleton_method("normalize_caps", function!(normalize_caps, 1))?;
|
|
104
|
-
runtime.define_singleton_method("value_budget_of", function!(value_budget_of, 1))?;
|
|
105
105
|
runtime.define_method("eval", method!(Runtime::eval, 3))?;
|
|
106
106
|
runtime.define_method("run", method!(Runtime::run, 4))?;
|
|
107
107
|
runtime.define_method("profile", method!(Runtime::profile, 0))?;
|
|
108
108
|
runtime.define_method("compile", method!(Runtime::compile, 1))?;
|
|
109
|
+
runtime.define_method("stat", method!(Runtime::stat, 0))?;
|
|
109
110
|
// The guest re-enters for a block yield through a frame-scoped
|
|
110
111
|
// `Kobako::GuestYielder` the dispatcher hands the Proc, not a method on
|
|
111
112
|
// Runtime.
|
|
@@ -131,14 +132,6 @@ fn normalize_caps(caps: RHash) -> Result<RHash, MagnusError> {
|
|
|
131
132
|
caps_of(&ruby, &to_limits(&ruby, caps)?)
|
|
132
133
|
}
|
|
133
134
|
|
|
134
|
-
/// The most values a guest payload may make the host build under
|
|
135
|
-
/// `memory_limit`, as the runtime's caps count them.
|
|
136
|
-
fn value_budget_of(memory_limit: Option<usize>) -> Option<usize> {
|
|
137
|
-
let mut limits = Limits::default();
|
|
138
|
-
limits.memory_limit = memory_limit;
|
|
139
|
-
limits.value_budget()
|
|
140
|
-
}
|
|
141
|
-
|
|
142
135
|
/// The caps in `caps`, keyed as `Kobako::SandboxOptions` names them, each read
|
|
143
136
|
/// as its kind: a deadline as seconds, a positive finite Numeric, and every
|
|
144
137
|
/// other cap as a positive Integer, `nil` asking for none. One outside its
|
|
@@ -241,6 +234,32 @@ fn to_count(ruby: &Ruby, name: &str, value: Value) -> Result<Option<usize>, Magn
|
|
|
241
234
|
}
|
|
242
235
|
}
|
|
243
236
|
|
|
237
|
+
/// What an invocation runs, owned so the guest's thread holds it.
|
|
238
|
+
enum Verb {
|
|
239
|
+
Eval(Vec<u8>),
|
|
240
|
+
Run(String, Vec<u8>),
|
|
241
|
+
}
|
|
242
|
+
|
|
243
|
+
impl Verb {
|
|
244
|
+
fn name(&self) -> &'static str {
|
|
245
|
+
match self {
|
|
246
|
+
Self::Eval(_) => "eval",
|
|
247
|
+
Self::Run(..) => "run",
|
|
248
|
+
}
|
|
249
|
+
}
|
|
250
|
+
|
|
251
|
+
/// Hand `invoke` the Entry this verb names.
|
|
252
|
+
fn invoke<T>(self, invoke: impl FnOnce(Entry<'_>) -> T) -> T {
|
|
253
|
+
match self {
|
|
254
|
+
Self::Eval(source) => invoke(Entry::Eval { source: &source }),
|
|
255
|
+
Self::Run(entrypoint, payload) => invoke(Entry::Run {
|
|
256
|
+
entrypoint: &entrypoint,
|
|
257
|
+
payload,
|
|
258
|
+
}),
|
|
259
|
+
}
|
|
260
|
+
}
|
|
261
|
+
}
|
|
262
|
+
|
|
244
263
|
#[derive(TypedData)]
|
|
245
264
|
#[magnus(class = "Kobako::Runtime", free_immediately, size)]
|
|
246
265
|
struct Runtime {
|
|
@@ -249,13 +268,17 @@ struct Runtime {
|
|
|
249
268
|
// per-invocation state — each `#eval` / `#run` takes its dispatch
|
|
250
269
|
// handler as an argument and returns its whole result as a `Snapshot`
|
|
251
270
|
// — so the contract's `Send + Sync` carries the type with no interior
|
|
252
|
-
// mutability to guard.
|
|
253
|
-
|
|
271
|
+
// mutability to guard. A run's guest holds it too, as a guest outlives a
|
|
272
|
+
// caller that never came back for it.
|
|
273
|
+
runtime: Arc<dyn ContractRuntime + Send + Sync>,
|
|
254
274
|
// Whether each invocation releases Ruby's GVL for its guest span
|
|
255
275
|
// (`gvl: :release`) or holds it throughout (`gvl: :hold`). Fixed at
|
|
256
276
|
// construction; a `bool` carries no interior mutability, so the type
|
|
257
277
|
// stays `Send + Sync`.
|
|
258
278
|
release_gvl: bool,
|
|
279
|
+
// How many of its runs wait now, on a lock or an event, as each run's
|
|
280
|
+
// guest counts while it waits. An atomic, so the type stays `Send + Sync`.
|
|
281
|
+
waiting: Arc<AtomicUsize>,
|
|
259
282
|
}
|
|
260
283
|
|
|
261
284
|
impl DataTypeFunctions for Runtime {}
|
|
@@ -323,8 +346,9 @@ impl Runtime {
|
|
|
323
346
|
let driver = Driver::from_file(Path::new(&path), config)
|
|
324
347
|
.map_err(|e| errors::from_setup(&ruby, e))?;
|
|
325
348
|
Ok(Self {
|
|
326
|
-
runtime:
|
|
349
|
+
runtime: Arc::new(driver),
|
|
327
350
|
release_gvl,
|
|
351
|
+
waiting: Arc::default(),
|
|
328
352
|
})
|
|
329
353
|
}
|
|
330
354
|
|
|
@@ -347,8 +371,7 @@ impl Runtime {
|
|
|
347
371
|
setup: RArray,
|
|
348
372
|
source: RString,
|
|
349
373
|
) -> Result<Snapshot, MagnusError> {
|
|
350
|
-
|
|
351
|
-
self.invoke(dispatch, setup, Entry::Eval { source: &source })
|
|
374
|
+
self.invoke(dispatch, setup, Verb::Eval(to_bytes(source)))
|
|
352
375
|
}
|
|
353
376
|
|
|
354
377
|
fn run(
|
|
@@ -358,45 +381,50 @@ impl Runtime {
|
|
|
358
381
|
entrypoint: String,
|
|
359
382
|
payload: RString,
|
|
360
383
|
) -> Result<Snapshot, MagnusError> {
|
|
361
|
-
self.invoke(
|
|
362
|
-
dispatch,
|
|
363
|
-
setup,
|
|
364
|
-
Entry::Run {
|
|
365
|
-
entrypoint: &entrypoint,
|
|
366
|
-
payload: to_bytes(payload),
|
|
367
|
-
},
|
|
368
|
-
)
|
|
384
|
+
self.invoke(dispatch, setup, Verb::Run(entrypoint, to_bytes(payload)))
|
|
369
385
|
}
|
|
370
386
|
|
|
371
387
|
/// One owner for the wiring both verbs share, so a Preamble, a Grant,
|
|
372
388
|
/// or a handler change cannot drift between them.
|
|
373
|
-
fn invoke(
|
|
374
|
-
&self,
|
|
375
|
-
dispatch: Value,
|
|
376
|
-
setup: RArray,
|
|
377
|
-
entry: Entry<'_>,
|
|
378
|
-
) -> Result<Snapshot, MagnusError> {
|
|
389
|
+
fn invoke(&self, dispatch: Value, setup: RArray, verb: Verb) -> Result<Snapshot, MagnusError> {
|
|
379
390
|
let ruby = Ruby::get().expect("Ruby thread");
|
|
380
|
-
let
|
|
381
|
-
Entry::Eval { .. } => "eval",
|
|
382
|
-
Entry::Run { .. } => "run",
|
|
383
|
-
};
|
|
391
|
+
let name = verb.name();
|
|
384
392
|
let handler = build_handler(dispatch);
|
|
385
393
|
let preamble = to_preamble(&ruby, setup)?;
|
|
386
|
-
let
|
|
387
|
-
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
|
|
391
|
-
|
|
392
|
-
|
|
393
|
-
|
|
394
|
-
|
|
394
|
+
let runtime = self.runtime.clone();
|
|
395
|
+
gvl::run(&ruby, self.release_gvl, &self.waiting, || {
|
|
396
|
+
let (mut grants, mut pins) = filesystem::to_grants(setup.entry(3)?)?;
|
|
397
|
+
let (networks, network_pins) = network::to_grants(&ruby, setup.entry(4)?)?;
|
|
398
|
+
grants.extend(networks);
|
|
399
|
+
pins.extend(network_pins);
|
|
400
|
+
// The closure touches no Ruby VALUE (the runtime is magnus-free, and
|
|
401
|
+
// a guest→host dispatch, or a mount's block, comes back to this
|
|
402
|
+
// Fiber), and owns all it reads, as the guest's thread runs it. It
|
|
403
|
+
// lets the pins go once the Store, which reaches them, is gone.
|
|
404
|
+
Ok(move || {
|
|
405
|
+
let snapshot =
|
|
406
|
+
verb.invoke(|entry| runtime.invoke(entry, &preamble, handler, &grants));
|
|
407
|
+
drop(grants);
|
|
408
|
+
drop(pins);
|
|
409
|
+
snapshot
|
|
410
|
+
})
|
|
395
411
|
})?
|
|
396
|
-
.map(|snapshot| Snapshot::new(snapshot,
|
|
412
|
+
.map(|snapshot| Snapshot::new(snapshot, name))
|
|
397
413
|
.map_err(|e| errors::from_invoke(&ruby, e))
|
|
398
414
|
}
|
|
399
415
|
|
|
416
|
+
/// What this Runtime's runs are doing now, as `GC.stat` answers the
|
|
417
|
+
/// collector's: how many wait, on a lock or an event.
|
|
418
|
+
fn stat(&self) -> Result<RHash, MagnusError> {
|
|
419
|
+
let ruby = Ruby::get().expect("Ruby thread");
|
|
420
|
+
let stat = ruby.hash_new();
|
|
421
|
+
stat.aset(
|
|
422
|
+
ruby.to_symbol("runs_waiting"),
|
|
423
|
+
self.waiting.load(Ordering::SeqCst),
|
|
424
|
+
)?;
|
|
425
|
+
Ok(stat)
|
|
426
|
+
}
|
|
427
|
+
|
|
400
428
|
/// The bytecode of the source snippets, one binary String each in
|
|
401
429
|
/// order, or `nil` when the guest compiled nothing; no failure raises,
|
|
402
430
|
/// since the Sandbox keeps the source either way. Takes the Preamble an
|
|
@@ -406,7 +434,10 @@ impl Runtime {
|
|
|
406
434
|
fn compile(&self, preamble: RArray) -> Result<Option<RArray>, MagnusError> {
|
|
407
435
|
let ruby = Ruby::get().expect("Ruby thread");
|
|
408
436
|
let preamble = to_preamble(&ruby, preamble)?;
|
|
409
|
-
let
|
|
437
|
+
let runtime = self.runtime.clone();
|
|
438
|
+
let bytecodes = gvl::run(&ruby, self.release_gvl, &self.waiting, || {
|
|
439
|
+
Ok(move || runtime.compile(&preamble))
|
|
440
|
+
})?;
|
|
410
441
|
Ok(bytecodes.map(|bytecodes| {
|
|
411
442
|
ruby.ary_from_iter(
|
|
412
443
|
bytecodes
|
|
@@ -8,8 +8,13 @@ module Kobako
|
|
|
8
8
|
# One invocation's Handle table, mapping opaque ids to host objects. Each
|
|
9
9
|
# invocation mints its own, so a Handle resolves only where it was issued.
|
|
10
10
|
class Handles # :nodoc:
|
|
11
|
-
#
|
|
12
|
-
#
|
|
11
|
+
# The most Handles one invocation issues, as wasmtime's ResourceTable
|
|
12
|
+
# holds at most 1,000,000 of what a host hands its guest: the table is
|
|
13
|
+
# kobako's own, so it stays bounded whatever a Service returns.
|
|
14
|
+
MAX_HANDLES = 1_000_000
|
|
15
|
+
|
|
16
|
+
# +next_id+ lets tests start near MAX_HANDLES to reach exhaustion
|
|
17
|
+
# without a million allocations.
|
|
13
18
|
def initialize(next_id: 1)
|
|
14
19
|
@entries = {} # : Hash[Integer, Kobako::Transport::Exposure]
|
|
15
20
|
surfaces = {} # : Hash[Module, Set[Symbol]]
|
|
@@ -50,12 +55,11 @@ module Kobako
|
|
|
50
55
|
private
|
|
51
56
|
|
|
52
57
|
def ensure_capacity
|
|
53
|
-
|
|
54
|
-
return unless @next_id > cap
|
|
58
|
+
return unless @next_id > MAX_HANDLES
|
|
55
59
|
|
|
56
60
|
raise HandleExhaustedError,
|
|
57
61
|
"Out of handle allocations: too many host objects were referenced " \
|
|
58
|
-
"in a single invocation (limit #{
|
|
62
|
+
"in a single invocation (limit #{MAX_HANDLES})"
|
|
59
63
|
end
|
|
60
64
|
|
|
61
65
|
def require_bound(id)
|
data/lib/kobako/codec/decoder.rb
CHANGED
|
@@ -13,7 +13,7 @@ module Kobako
|
|
|
13
13
|
# from the decoded payload reports a broken invariant as
|
|
14
14
|
# InvalidTypeError without its own Boundary.within.
|
|
15
15
|
def self.decode(bytes)
|
|
16
|
-
ValueBudget.assert_within(bytes,
|
|
16
|
+
ValueBudget.assert_within(bytes, MAX_PAYLOAD_VALUES)
|
|
17
17
|
value = FACTORY.load(bytes.b)
|
|
18
18
|
validate_utf8(value)
|
|
19
19
|
block_given? ? yield(value) : value
|
data/lib/kobako/codec/state.rb
CHANGED
|
@@ -2,9 +2,8 @@
|
|
|
2
2
|
|
|
3
3
|
module Kobako
|
|
4
4
|
module Codec
|
|
5
|
-
# Whether a Capability Handle crossed the decode in flight,
|
|
6
|
-
#
|
|
7
|
-
# variables stay sound.
|
|
5
|
+
# Whether a Capability Handle crossed the decode in flight, kept per
|
|
6
|
+
# thread so plain instance variables stay sound.
|
|
8
7
|
class State # :nodoc:
|
|
9
8
|
# Thread-local slot holding the calling thread's State.
|
|
10
9
|
STATE_KEY = :__kobako_codec_state__
|
|
@@ -19,21 +18,6 @@ module Kobako
|
|
|
19
18
|
|
|
20
19
|
def initialize
|
|
21
20
|
@carried_handle = false
|
|
22
|
-
@value_budget = nil
|
|
23
|
-
end
|
|
24
|
-
|
|
25
|
-
# The most values a decode on this thread may build, or nil for no
|
|
26
|
-
# budget.
|
|
27
|
-
attr_reader :value_budget
|
|
28
|
-
|
|
29
|
-
# A nested invocation on the same thread restores the outer one's
|
|
30
|
-
# budget when it returns.
|
|
31
|
-
def within_value_budget(budget)
|
|
32
|
-
outer = @value_budget
|
|
33
|
-
@value_budget = budget
|
|
34
|
-
yield
|
|
35
|
-
ensure
|
|
36
|
-
@value_budget = outer
|
|
37
21
|
end
|
|
38
22
|
|
|
39
23
|
# ExtTypes#unpack_handle is the sole chokepoint every Handle passes
|
|
@@ -5,9 +5,9 @@ require_relative "error"
|
|
|
5
5
|
module Kobako
|
|
6
6
|
module Codec
|
|
7
7
|
# Counts the values a payload holds before the decoder builds any, so a
|
|
8
|
-
#
|
|
9
|
-
#
|
|
10
|
-
#
|
|
8
|
+
# short payload cannot make the host build a large value. Every value
|
|
9
|
+
# takes at least one byte, so a payload no longer than the budget, and
|
|
10
|
+
# holding no float, needs no count.
|
|
11
11
|
#
|
|
12
12
|
# A map key holding a NaN costs one more value for each such key the
|
|
13
13
|
# map already holds, as a Ruby Hash compares it with each: every NaN
|
|
@@ -45,7 +45,7 @@ module Kobako
|
|
|
45
45
|
private_constant :FLOAT_FORMATS, :FLOATS, :NAN_ENTRY
|
|
46
46
|
|
|
47
47
|
def self.assert_within(bytes, budget)
|
|
48
|
-
return if
|
|
48
|
+
return if within_by_size?(bytes, budget)
|
|
49
49
|
|
|
50
50
|
Count.new(bytes, budget).run
|
|
51
51
|
end
|
data/lib/kobako/codec.rb
CHANGED
|
@@ -19,16 +19,15 @@ module Kobako
|
|
|
19
19
|
# packer.
|
|
20
20
|
MAX_NESTING_DEPTH = 128
|
|
21
21
|
|
|
22
|
+
# The most values one guest payload may make the host build, whatever
|
|
23
|
+
# the memory cap, shared with the Rust +kobako_codec+ so both hosts
|
|
24
|
+
# refuse the same payload.
|
|
25
|
+
MAX_PAYLOAD_VALUES = 262_144
|
|
26
|
+
|
|
22
27
|
# Answers the block's result with whether the decode carried a
|
|
23
28
|
# Capability Handle, so a caller can skip an all-identity restore walk.
|
|
24
29
|
def self.track_handles(&block)
|
|
25
30
|
State.current.track_handles(&block)
|
|
26
31
|
end
|
|
27
|
-
|
|
28
|
-
# Holds every decode the block runs on this thread to +budget+ values,
|
|
29
|
-
# or to none when +budget+ is nil.
|
|
30
|
-
def self.within_value_budget(budget, &block)
|
|
31
|
-
State.current.within_value_budget(budget, &block)
|
|
32
|
-
end
|
|
33
32
|
end
|
|
34
33
|
end
|
data/lib/kobako/errors.rb
CHANGED
|
@@ -3,7 +3,7 @@
|
|
|
3
3
|
module Kobako
|
|
4
4
|
# Every Sandbox#eval or Sandbox#run that reaches the guest returns, or
|
|
5
5
|
# raises exactly one of TrapError, SandboxError, or ServiceError. SetupError
|
|
6
|
-
#
|
|
6
|
+
# arises outside any invocation. A mistake in a call
|
|
7
7
|
# raises the TypeError, ArgumentError, RangeError, or FrozenError a CRuby
|
|
8
8
|
# core method raises for it.
|
|
9
9
|
|
|
@@ -20,7 +20,8 @@ module Kobako
|
|
|
20
20
|
end
|
|
21
21
|
|
|
22
22
|
# The engine stopped the guest: a trap, a cap reached, or an outcome that
|
|
23
|
-
# could not be read.
|
|
23
|
+
# could not be read. Or the host could not start it, out of threads or
|
|
24
|
+
# address space. The next invocation on the same Sandbox runs as usual.
|
|
24
25
|
# +execution+ is the failed run's Execution, so a rescue reads its captures
|
|
25
26
|
# and usage as a successful caller would; it is +nil+ when the failure came
|
|
26
27
|
# before the guest ran.
|
|
@@ -95,7 +96,7 @@ module Kobako
|
|
|
95
96
|
# block never ran. Left unrescued, the Service call fails.
|
|
96
97
|
class UnsupportedError < Error; end
|
|
97
98
|
|
|
98
|
-
# One invocation issued more
|
|
99
|
+
# One invocation issued more than the 1,000,000 Handles it may hold.
|
|
99
100
|
class HandleExhaustedError < SandboxError; end
|
|
100
101
|
|
|
101
102
|
# A <tt>preload(binary:)</tt> snippet failed to load. Bytecode that loads
|
|
@@ -118,8 +119,4 @@ module Kobako
|
|
|
118
119
|
@available = available
|
|
119
120
|
end
|
|
120
121
|
end
|
|
121
|
-
|
|
122
|
-
# Pool#with waited past +checkout_timeout+ with every slot held. Retrying
|
|
123
|
-
# succeeds once a holder returns its Sandbox.
|
|
124
|
-
class PoolTimeoutError < Error; end
|
|
125
122
|
end
|
data/lib/kobako/filesystem.rb
CHANGED
|
@@ -7,7 +7,7 @@ module Kobako
|
|
|
7
7
|
#
|
|
8
8
|
# sandbox.mount("/data", Kobako::Filesystem::Tmpfs.new("config.yml" => yaml))
|
|
9
9
|
#
|
|
10
|
-
# [<tt>Tmpfs.new(files = {}, size:
|
|
10
|
+
# [<tt>Tmpfs.new(files = {}, size: 4 MiB, nr_inodes:)</tt>]
|
|
11
11
|
# Files held in memory for as long as the object lives, seeded from a
|
|
12
12
|
# Hash of path to contents. Every invocation shares them; one that
|
|
13
13
|
# should start empty mounts a new Tmpfs through Context#mount.
|
data/lib/kobako/fork.rb
ADDED
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Kobako
|
|
4
|
+
# The check a fork through Ruby passes, defined by the ext: +prepare+ raises
|
|
5
|
+
# RuntimeError for a fork that cannot go on, as while a run is in progress,
|
|
6
|
+
# and otherwise holds new runs back, and +release+ lets them in again. This
|
|
7
|
+
# file hooks it into each fork Ruby makes.
|
|
8
|
+
module Fork # :nodoc:
|
|
9
|
+
# Prepended to Process's singleton class. +_fork+ is what Kernel#fork,
|
|
10
|
+
# Process.fork, and <tt>IO.popen("-")</tt> call, and Process.daemon
|
|
11
|
+
# forks without it. Only the main Ractor runs a Sandbox, so another
|
|
12
|
+
# Ractor's fork has nothing to check.
|
|
13
|
+
module Hook
|
|
14
|
+
def _fork
|
|
15
|
+
Fork.around { super }
|
|
16
|
+
end
|
|
17
|
+
|
|
18
|
+
def daemon(...)
|
|
19
|
+
Fork.around { super }
|
|
20
|
+
end
|
|
21
|
+
end
|
|
22
|
+
|
|
23
|
+
# Run +fork+ checked. A forked child holds no run back, so its +release+
|
|
24
|
+
# finds nothing to let in.
|
|
25
|
+
def self.around
|
|
26
|
+
return yield unless Ractor.current == Ractor.main
|
|
27
|
+
|
|
28
|
+
prepare
|
|
29
|
+
begin
|
|
30
|
+
yield
|
|
31
|
+
ensure
|
|
32
|
+
release
|
|
33
|
+
end
|
|
34
|
+
end
|
|
35
|
+
|
|
36
|
+
# Hook every fork from now on, once. A Sandbox installs it as it is
|
|
37
|
+
# built, so the hook stands outside the ones loaded before.
|
|
38
|
+
def self.install
|
|
39
|
+
Process.singleton_class.prepend(Hook) unless Process.singleton_class <= Hook
|
|
40
|
+
end
|
|
41
|
+
end
|
|
42
|
+
end
|
data/lib/kobako/handle.rb
CHANGED
|
@@ -6,8 +6,7 @@ module Kobako
|
|
|
6
6
|
class Handle < Data.define(:id)
|
|
7
7
|
# The smallest id; 0 never names a Handle.
|
|
8
8
|
MIN_ID = 1
|
|
9
|
-
# The largest id, the bound the wire's Envelope holds every side to
|
|
10
|
-
# invocation needing more raises HandleExhaustedError.
|
|
9
|
+
# The largest id, the bound the wire's Envelope holds every side to.
|
|
11
10
|
MAX_ID = Kobako.max_handle_id
|
|
12
11
|
|
|
13
12
|
def initialize(id:) # :nodoc:
|
data/lib/kobako/sandbox.rb
CHANGED
|
@@ -45,6 +45,7 @@ module Kobako
|
|
|
45
45
|
@grants = Catalog::Grants.new
|
|
46
46
|
@sealed = false
|
|
47
47
|
@runtime = build_runtime
|
|
48
|
+
Fork.install
|
|
48
49
|
end
|
|
49
50
|
|
|
50
51
|
# Make +object+ reachable from the guest as the constant at +path+, a
|
|
@@ -175,7 +176,7 @@ module Kobako
|
|
|
175
176
|
# wire carries. A failed run raises as #eval does.
|
|
176
177
|
def run(target, *args, **kwargs, &block)
|
|
177
178
|
request = Transport::Run.new(entrypoint: target, args: args, kwargs: kwargs)
|
|
178
|
-
|
|
179
|
+
begin_invocation.run(request, &block)
|
|
179
180
|
end
|
|
180
181
|
|
|
181
182
|
# Evaluate +code+, mruby source as a String, and return the Execution,
|
|
@@ -184,7 +185,8 @@ module Kobako
|
|
|
184
185
|
#
|
|
185
186
|
# Raises TypeError when +code+ is no String, and ArgumentError when it is
|
|
186
187
|
# not UTF-8 text, before the guest runs; TrapError when the engine
|
|
187
|
-
# stopped the guest, a deadline or memory budget included
|
|
188
|
+
# stopped the guest, a deadline or memory budget included, or the host
|
|
189
|
+
# could not start it; SandboxError
|
|
188
190
|
# when the guest failed, the Guest Binary not starting the run included;
|
|
189
191
|
# and ServiceError when a Service call failed and the guest left it
|
|
190
192
|
# unrescued.
|
|
@@ -194,7 +196,17 @@ module Kobako
|
|
|
194
196
|
source = code.dup.force_encoding(Encoding::UTF_8)
|
|
195
197
|
raise ArgumentError, "code must be UTF-8 text" unless source.valid_encoding?
|
|
196
198
|
|
|
197
|
-
|
|
199
|
+
begin_invocation.eval(source, &block)
|
|
200
|
+
end
|
|
201
|
+
|
|
202
|
+
# What this Sandbox's runs are doing now, as a Hash, or the one value
|
|
203
|
+
# +key+ names, as GC.stat answers: +:runs_waiting+ counts the runs waiting
|
|
204
|
+
# on a lock or an event. Raises ArgumentError for a key it does not hold.
|
|
205
|
+
def stat(key = nil) # :nodoc:
|
|
206
|
+
stat = @runtime.stat
|
|
207
|
+
return stat if key.nil?
|
|
208
|
+
|
|
209
|
+
stat.fetch(key) { raise ArgumentError, "unknown key: #{key}" }
|
|
198
210
|
end
|
|
199
211
|
|
|
200
212
|
private
|
|
@@ -212,12 +224,7 @@ module Kobako
|
|
|
212
224
|
# Registration closes at the first invocation, so every invocation ships
|
|
213
225
|
# the same Services, snippets, flags, Extensions and mounts.
|
|
214
226
|
def refuse_once_invoked(verb)
|
|
215
|
-
raise FrozenError, "cannot #{verb}
|
|
216
|
-
end
|
|
217
|
-
|
|
218
|
-
# Close registration, saying why for the refusal that follows.
|
|
219
|
-
def seal(reason)
|
|
220
|
-
@sealed = reason
|
|
227
|
+
raise FrozenError, "cannot #{verb} after first Sandbox invocation" if @sealed
|
|
221
228
|
end
|
|
222
229
|
|
|
223
230
|
# Sealing before the dependency check keeps a Sandbox whose check failed
|
|
@@ -226,37 +233,11 @@ module Kobako
|
|
|
226
233
|
# the Context it answers, so nothing an invocation does is written back
|
|
227
234
|
# onto the shared config.
|
|
228
235
|
def begin_invocation
|
|
229
|
-
|
|
230
|
-
seal("after first Sandbox invocation")
|
|
236
|
+
@sealed = true
|
|
231
237
|
@extensions.assert_dependencies
|
|
232
238
|
@snippets.compile { |entries| @runtime.compile(preamble(entries)) }
|
|
233
239
|
Context.new(runtime: @runtime, services: @services, extensions: @extensions, grants: @grants,
|
|
234
240
|
preamble: preamble(@snippets.entries))
|
|
235
241
|
end
|
|
236
|
-
|
|
237
|
-
# Ruby runs a signal's trap handler, and a Ractor's +require+, on the main
|
|
238
|
-
# Thread alone, and either raising as a run releasing the GVL takes it back
|
|
239
|
-
# after a Service call leaves the run where it cannot unwind. The first such
|
|
240
|
-
# run warns once.
|
|
241
|
-
def warn_release_on_main_thread
|
|
242
|
-
MainThreadRelease.warn_once if @options.gvl == :release && Thread.current == Thread.main
|
|
243
|
-
end
|
|
244
|
-
|
|
245
|
-
# The process's one warning that a run releasing the GVL ran on the main
|
|
246
|
-
# Thread.
|
|
247
|
-
module MainThreadRelease
|
|
248
|
-
MESSAGE = "Kobako::Sandbox with gvl: :release runs on the main Thread, where a trap handler " \
|
|
249
|
-
"that raises or exits, or another Ractor's require, can stop the run where it cannot " \
|
|
250
|
-
"unwind; run it on another Thread, or keep trap handlers to setting a flag"
|
|
251
|
-
@warned = false
|
|
252
|
-
|
|
253
|
-
def self.warn_once
|
|
254
|
-
return if @warned
|
|
255
|
-
|
|
256
|
-
@warned = true
|
|
257
|
-
warn(MESSAGE)
|
|
258
|
-
end
|
|
259
|
-
end
|
|
260
|
-
private_constant :MainThreadRelease
|
|
261
242
|
end
|
|
262
243
|
end
|