kobako 0.30.0 → 0.31.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 +335 -0
- data/Cargo.lock +469 -11
- data/README.md +43 -3
- data/ROADMAP.md +1 -1
- data/crates/kobako-runtime/CHANGELOG.md +219 -0
- data/crates/kobako-runtime/Cargo.toml +51 -3
- data/crates/kobako-runtime/README.md +7 -4
- data/crates/kobako-runtime/src/cred.rs +124 -0
- data/crates/kobako-runtime/src/dispatch.rs +4 -5
- data/crates/kobako-runtime/src/endpoint/buffer.rs +153 -44
- data/crates/kobako-runtime/src/endpoint/dispatch.rs +80 -41
- data/crates/kobako-runtime/src/endpoint/invoke.rs +23 -36
- data/crates/kobako-runtime/src/endpoint.rs +137 -36
- data/crates/kobako-runtime/src/errno.rs +391 -0
- data/crates/kobako-runtime/src/error.rs +31 -10
- data/crates/kobako-runtime/src/filesystem/anon_inodes.rs +52 -0
- data/crates/kobako-runtime/src/filesystem/bind/lookup.rs +52 -14
- data/crates/kobako-runtime/src/filesystem/bind/walk.rs +19 -20
- data/crates/kobako-runtime/src/filesystem/bind.rs +437 -245
- data/crates/kobako-runtime/src/filesystem/overlay/file.rs +6 -6
- data/crates/kobako-runtime/src/filesystem/overlay/path.rs +43 -65
- data/crates/kobako-runtime/src/filesystem/overlay/stack/copy_up.rs +9 -16
- data/crates/kobako-runtime/src/filesystem/overlay/stack/dir.rs +47 -72
- data/crates/kobako-runtime/src/filesystem/overlay/stack/namei.rs +12 -12
- data/crates/kobako-runtime/src/filesystem/overlay/stack/readdir.rs +4 -7
- data/crates/kobako-runtime/src/filesystem/overlay/stack.rs +121 -37
- data/crates/kobako-runtime/src/filesystem/overlay.rs +302 -343
- data/crates/kobako-runtime/src/filesystem/pipe.rs +12 -9
- data/crates/kobako-runtime/src/filesystem/rdir.rs +15 -32
- data/crates/kobako-runtime/src/filesystem/tmpfs/seed.rs +32 -0
- data/crates/kobako-runtime/src/filesystem/tmpfs/tmpfile.rs +3 -3
- data/crates/kobako-runtime/src/filesystem/tmpfs/whiteout.rs +57 -35
- data/crates/kobako-runtime/src/filesystem/tmpfs/xattr.rs +2 -2
- data/crates/kobako-runtime/src/filesystem/tmpfs.rs +172 -160
- data/crates/kobako-runtime/src/filesystem/wrap.rs +36 -52
- data/crates/kobako-runtime/src/filesystem.rs +6 -3
- data/crates/kobako-runtime/src/fork.rs +96 -0
- data/crates/kobako-runtime/src/grant.rs +3 -0
- data/crates/kobako-runtime/src/hashtable.rs +4 -2
- data/crates/kobako-runtime/src/lib.rs +18 -7
- data/crates/kobako-runtime/src/limits.rs +195 -0
- data/crates/kobako-runtime/src/locking.rs +10 -0
- data/crates/kobako-runtime/src/memcg.rs +383 -0
- data/crates/kobako-runtime/src/mount.rs +67 -9
- data/crates/kobako-runtime/src/net/backends.rs +43 -0
- data/crates/kobako-runtime/src/net/connections.rs +179 -0
- data/crates/kobako-runtime/src/net/credential.rs +125 -0
- data/crates/kobako-runtime/src/net/egress.rs +160 -0
- data/crates/kobako-runtime/src/net/family.rs +181 -0
- data/crates/kobako-runtime/src/net/http.rs +2733 -0
- data/crates/kobako-runtime/src/net/lookup.rs +285 -0
- data/crates/kobako-runtime/src/net/mock.rs +251 -0
- data/crates/kobako-runtime/src/net/namespace.rs +1366 -0
- data/crates/kobako-runtime/src/net/reactor.rs +531 -0
- data/crates/kobako-runtime/src/net/resolv/darwin.rs +275 -0
- data/crates/kobako-runtime/src/net/resolv/stub/tests.rs +469 -0
- data/crates/kobako-runtime/src/net/resolv/stub.rs +536 -0
- data/crates/kobako-runtime/src/net/resolv/windows.rs +248 -0
- data/crates/kobako-runtime/src/net/resolv.rs +227 -0
- data/crates/kobako-runtime/src/net/socket.rs +870 -0
- data/crates/kobako-runtime/src/net/tcp.rs +1588 -0
- data/crates/kobako-runtime/src/net/tls.rs +58 -0
- data/crates/kobako-runtime/src/net/transport.rs +279 -0
- data/crates/kobako-runtime/src/net/udp.rs +1003 -0
- data/crates/kobako-runtime/src/net.rs +42 -0
- data/crates/kobako-runtime/src/network/builder.rs +177 -0
- data/crates/kobako-runtime/src/network/ruleset.rs +777 -0
- data/crates/kobako-runtime/src/network.rs +551 -0
- data/crates/kobako-runtime/src/policy.rs +16 -0
- data/crates/kobako-runtime/src/profile.rs +29 -144
- data/crates/kobako-runtime/src/runtime.rs +3 -5
- data/crates/kobako-runtime/src/sched.rs +130 -29
- data/crates/kobako-runtime/src/security/entry_policy.rs +22 -0
- data/crates/kobako-runtime/src/{vfs/cred.rs → security/hooks.rs} +52 -144
- data/crates/kobako-runtime/src/security/mount_policy.rs +158 -0
- data/crates/kobako-runtime/src/security.rs +101 -196
- data/crates/kobako-runtime/src/snapshot.rs +121 -2
- data/crates/kobako-runtime/src/vfs/attr.rs +5 -5
- data/crates/kobako-runtime/src/vfs/dentry.rs +143 -89
- data/crates/kobako-runtime/src/vfs/file.rs +199 -203
- data/crates/kobako-runtime/src/vfs/inode.rs +41 -45
- data/crates/kobako-runtime/src/vfs/kern_path.rs +38 -42
- data/crates/kobako-runtime/src/vfs/mount.rs +10 -9
- data/crates/kobako-runtime/src/vfs/namei.rs +829 -700
- data/crates/kobako-runtime/src/vfs/namespace.rs +162 -0
- data/crates/kobako-runtime/src/vfs/open_files.rs +69 -0
- data/crates/kobako-runtime/src/vfs/openat2.rs +3 -3
- data/crates/kobako-runtime/src/vfs/path.rs +6 -6
- data/crates/kobako-runtime/src/vfs/readdir.rs +6 -6
- data/crates/kobako-runtime/src/vfs/stat.rs +159 -23
- data/crates/kobako-runtime/src/vfs/super_block.rs +18 -14
- data/crates/kobako-runtime/src/vfs/utimes.rs +37 -27
- data/crates/kobako-runtime/src/vfs.rs +9 -6
- data/crates/kobako-runtime/src/wait.rs +356 -0
- data/crates/kobako-transport/CHANGELOG.md +31 -0
- data/crates/kobako-transport/Cargo.toml +1 -1
- data/crates/kobako-transport/README.md +3 -4
- data/crates/kobako-transport/src/abi.rs +78 -72
- data/crates/kobako-transport/src/envelope/bytes.rs +17 -6
- data/crates/kobako-transport/src/envelope/call.rs +32 -4
- data/crates/kobako-transport/src/envelope/error_record.rs +21 -0
- data/crates/kobako-transport/src/envelope/name.rs +99 -0
- data/crates/kobako-transport/src/envelope/outcome.rs +9 -9
- data/crates/kobako-transport/src/envelope/preamble.rs +24 -21
- data/crates/kobako-transport/src/envelope/reply.rs +27 -27
- data/crates/kobako-transport/src/envelope/unsupported.rs +10 -10
- data/crates/kobako-transport/src/envelope.rs +13 -8
- data/crates/kobako-wasmtime/CHANGELOG.md +161 -0
- data/crates/kobako-wasmtime/Cargo.toml +26 -7
- data/crates/kobako-wasmtime/README.md +3 -2
- data/crates/kobako-wasmtime/build/preview2.rs +1339 -0
- data/crates/kobako-wasmtime/build.rs +16 -0
- data/crates/kobako-wasmtime/src/abi.rs +17 -37
- data/crates/kobako-wasmtime/src/ambient.rs +33 -18
- data/crates/kobako-wasmtime/src/cache.rs +23 -15
- data/crates/kobako-wasmtime/src/config.rs +52 -68
- data/crates/kobako-wasmtime/src/driver.rs +48 -63
- data/crates/kobako-wasmtime/src/exports.rs +143 -17
- data/crates/kobako-wasmtime/src/guest.rs +12 -6
- data/crates/kobako-wasmtime/src/guest_binary.rs +14 -0
- data/crates/kobako-wasmtime/src/imports.rs +42 -18
- data/crates/kobako-wasmtime/src/instance_pre.rs +4 -4
- data/crates/kobako-wasmtime/src/invocation.rs +74 -12
- data/crates/kobako-wasmtime/src/lib.rs +5 -3
- data/crates/kobako-wasmtime/src/limiter.rs +70 -5
- data/crates/kobako-wasmtime/src/preview1/descriptors.rs +309 -0
- data/crates/kobako-wasmtime/src/preview1/differential/mount.rs +5 -15
- data/crates/kobako-wasmtime/src/preview1/differential.rs +27 -31
- data/crates/kobako-wasmtime/src/preview1/errno.rs +55 -66
- data/crates/kobako-wasmtime/src/preview1/fd.rs +327 -296
- data/crates/kobako-wasmtime/src/preview1/memory.rs +21 -12
- data/crates/kobako-wasmtime/src/preview1/mount_view/wrap.rs +34 -53
- data/crates/kobako-wasmtime/src/preview1/mount_view.rs +231 -217
- data/crates/kobako-wasmtime/src/preview1/path.rs +56 -121
- data/crates/kobako-wasmtime/src/preview1/poll.rs +244 -120
- data/crates/kobako-wasmtime/src/preview1/sock.rs +10 -8
- data/crates/kobako-wasmtime/src/preview1/testsuite.rs +5 -15
- data/crates/kobako-wasmtime/src/preview1.rs +46 -42
- data/crates/kobako-wasmtime/src/preview2/abi.rs +227 -0
- data/crates/kobako-wasmtime/src/preview2/error.rs +118 -0
- data/crates/kobako-wasmtime/src/preview2/filesystem.rs +122 -0
- data/crates/kobako-wasmtime/src/preview2/host.rs +3959 -0
- data/crates/kobako-wasmtime/src/preview2/http.rs +1503 -0
- data/crates/kobako-wasmtime/src/preview2/io.rs +315 -0
- data/crates/kobako-wasmtime/src/preview2/sockets.rs +379 -0
- data/crates/kobako-wasmtime/src/preview2/table.rs +305 -0
- data/crates/kobako-wasmtime/src/preview2.rs +57 -0
- data/crates/kobako-wasmtime/src/trap.rs +24 -9
- data/crates/kobako-wasmtime/src/wasi.rs +210 -81
- data/crates/kobako-wasmtime/wit/deps/cli.wit +233 -0
- data/crates/kobako-wasmtime/wit/deps/clocks.wit +162 -0
- data/crates/kobako-wasmtime/wit/deps/filesystem.wit +589 -0
- data/crates/kobako-wasmtime/wit/deps/http.wit +733 -0
- data/crates/kobako-wasmtime/wit/deps/io.wit +299 -0
- data/crates/kobako-wasmtime/wit/deps/random.wit +92 -0
- data/crates/kobako-wasmtime/wit/deps/sockets.wit +949 -0
- data/data/kobako.wasm +0 -0
- data/ext/kobako/Cargo.toml +1 -1
- data/ext/kobako/src/{runtime/errors.rs → errors.rs} +34 -26
- data/ext/kobako/src/{runtime/escape.rs → escape.rs} +42 -11
- data/ext/kobako/src/{runtime/filesystem → filesystem}/grants.rs +30 -26
- data/ext/kobako/src/{runtime/filesystem → filesystem}/ruby.rs +149 -168
- data/ext/kobako/src/{runtime/filesystem.rs → filesystem.rs} +24 -15
- data/ext/kobako/src/gvl.rs +306 -0
- data/ext/kobako/src/lib.rs +64 -3
- data/ext/kobako/src/network/mock.rs +187 -0
- data/ext/kobako/src/network.rs +591 -0
- data/ext/kobako/src/runtime/bridge.rs +30 -29
- data/ext/kobako/src/runtime/snapshot.rs +4 -4
- data/ext/kobako/src/runtime.rs +221 -143
- data/ext/kobako/src/wire.rs +42 -0
- data/lib/kobako/catalog/extensions.rb +14 -2
- data/lib/kobako/catalog/flags.rb +3 -5
- data/lib/kobako/catalog/grants.rb +5 -4
- data/lib/kobako/catalog/handles.rb +5 -27
- data/lib/kobako/catalog/mount.rb +11 -3
- data/lib/kobako/catalog/network.rb +35 -0
- data/lib/kobako/catalog/replacements.rb +28 -0
- data/lib/kobako/catalog/services.rb +5 -19
- data/lib/kobako/catalog/snippets.rb +3 -18
- data/lib/kobako/catalog.rb +3 -0
- data/lib/kobako/codec/{utils.rb → boundary.rb} +4 -2
- data/lib/kobako/codec/decoder.rb +4 -4
- data/lib/kobako/codec/ext_types.rb +3 -3
- data/lib/kobako/codec/handle_walk.rb +3 -43
- data/lib/kobako/codec/value_budget.rb +132 -40
- data/lib/kobako/codec.rb +1 -1
- data/lib/kobako/context.rb +24 -11
- data/lib/kobako/errors.rb +15 -12
- data/lib/kobako/extension.rb +3 -2
- data/lib/kobako/filesystem.rb +9 -2
- data/lib/kobako/handle.rb +2 -2
- data/lib/kobako/names.rb +28 -0
- data/lib/kobako/network.rb +210 -0
- data/lib/kobako/outcome.rb +7 -7
- data/lib/kobako/pool.rb +45 -23
- data/lib/kobako/runtime.rb +3 -2
- data/lib/kobako/sandbox.rb +92 -35
- data/lib/kobako/sandbox_options.rb +41 -106
- data/lib/kobako/transport/argument_wrap.rb +64 -0
- data/lib/kobako/transport/block_failure.rb +1 -1
- data/lib/kobako/transport/dispatcher.rb +6 -6
- data/lib/kobako/transport/exposure.rb +26 -15
- data/lib/kobako/transport/reflection.rb +21 -4
- data/lib/kobako/transport/run.rb +8 -23
- data/lib/kobako/transport/yielder.rb +2 -9
- data/lib/kobako/transport.rb +1 -0
- data/lib/kobako/version.rb +1 -1
- data/lib/kobako.rb +6 -2
- data/release-please-config.json +15 -0
- data/sig/core/thread.rbs +6 -0
- data/sig/kobako/catalog/extensions.rbs +2 -0
- data/sig/kobako/catalog/grants.rbs +7 -4
- data/sig/kobako/catalog/handles.rbs +0 -2
- data/sig/kobako/catalog/mount.rbs +3 -1
- data/sig/kobako/catalog/network.rbs +20 -0
- data/sig/kobako/catalog/replacements.rbs +14 -0
- data/sig/kobako/catalog/services.rbs +0 -4
- data/sig/kobako/catalog/snippets.rbs +0 -2
- data/sig/kobako/codec/{utils.rbs → boundary.rbs} +2 -2
- data/sig/kobako/codec/handle_walk.rbs +0 -6
- data/sig/kobako/codec/value_budget.rbs +47 -4
- data/sig/kobako/context.rbs +3 -1
- data/sig/kobako/errors.rbs +4 -4
- data/sig/kobako/filesystem.rbs +21 -3
- data/sig/kobako/network.rbs +115 -0
- data/sig/kobako/outcome.rbs +1 -1
- data/sig/kobako/pool.rbs +7 -3
- data/sig/kobako/runtime.rbs +32 -3
- data/sig/kobako/sandbox.rbs +19 -2
- data/sig/kobako/sandbox_options.rbs +29 -38
- data/sig/kobako/transport/argument_wrap.rbs +15 -0
- data/sig/kobako/transport/error.rbs +1 -1
- data/sig/kobako/transport/exposure.rbs +2 -4
- data/sig/kobako/transport/reflection.rbs +10 -0
- data/sig/kobako/transport/run.rbs +0 -2
- data/sig/kobako/transport/yielder.rbs +0 -3
- data/sig/kobako/transport.rbs +1 -1
- data/sig/kobako.rbs +13 -0
- metadata +84 -35
- data/crates/kobako-runtime/src/endpoint/capture.rs +0 -115
- data/crates/kobako-runtime/src/endpoint/memory_cap.rs +0 -238
- data/crates/kobako-runtime/src/filesystem/path.rs +0 -21
- data/crates/kobako-runtime/src/vfs/errno.rs +0 -153
- data/crates/kobako-runtime/tests/linux_baseline/calls.rs +0 -171
- data/crates/kobako-runtime/tests/linux_baseline/concurrent.rs +0 -276
- data/crates/kobako-runtime/tests/linux_baseline/kobako.rs +0 -323
- data/crates/kobako-runtime/tests/linux_baseline/linux-baseline-expected.txt +0 -16
- data/crates/kobako-runtime/tests/linux_baseline/linux.rs +0 -449
- data/crates/kobako-runtime/tests/linux_baseline/main.rs +0 -611
- data/crates/kobako-runtime/tests/linux_baseline/permissions.rs +0 -236
- data/crates/kobako-wasmtime/src/descriptors.rs +0 -200
- data/crates/kobako-wasmtime/tests/integration/compile_posture.rs +0 -43
- data/crates/kobako-wasmtime/tests/integration/guest_file.rs +0 -1085
- data/crates/kobako-wasmtime/tests/integration/main.rs +0 -29
- data/crates/kobako-wasmtime/tests/integration/preamble_flags.rs +0 -70
- data/crates/kobako-wasmtime/tests/integration/snippet_compile.rs +0 -95
- data/crates/kobako-wasmtime/tests/integration/support/mod.rs +0 -30
- data/crates/kobako-wasmtime/tests/integration/unanswered_call.rs +0 -86
- data/crates/kobako-wasmtime/tests/integration/unsupported_call.rs +0 -51
- data/ext/kobako/src/runtime/gvl.rs +0 -147
- data/lib/kobako/constant_name.rb +0 -8
- data/sig/kobako/constant_name.rbs +0 -3
- /data/ext/kobako/src/{runtime/pinned_value.rs → pinned_value.rs} +0 -0
data/ext/kobako/src/runtime.rs
CHANGED
|
@@ -1,8 +1,9 @@
|
|
|
1
|
-
//!
|
|
1
|
+
//! The Ruby handle of the contract `Runtime` a Sandbox runs each
|
|
2
|
+
//! invocation on.
|
|
2
3
|
//!
|
|
3
4
|
//! The Ruby-visible classes are
|
|
4
5
|
//!
|
|
5
|
-
//! Kobako::Runtime —
|
|
6
|
+
//! Kobako::Runtime — holds one contract `Runtime`
|
|
6
7
|
//! Kobako::Snapshot — one invocation's completion + captures + usage
|
|
7
8
|
//!
|
|
8
9
|
//! `Kobako::Runtime` is constructed via `Kobako::Runtime.from_path(path,
|
|
@@ -11,24 +12,18 @@
|
|
|
11
12
|
//! argument, instantiates a fresh instance, and returns a `Snapshot` — the
|
|
12
13
|
//! whole per-invocation result — so the Runtime holds no per-invocation
|
|
13
14
|
//! state and one Runtime is safe to drive concurrently. The run mechanics —
|
|
14
|
-
//! engine/module caches, caps, trap classification — live in the
|
|
15
|
-
//! `
|
|
15
|
+
//! engine/module caches, caps, trap classification — live in the Wasm
|
|
16
|
+
//! Driver `from_path` builds, behind the `kobako_runtime` contract; no wasm
|
|
16
17
|
//! engine type reaches this crate or the Host App.
|
|
17
18
|
//!
|
|
18
19
|
//! Each module holds one responsibility and opens with its own doc.
|
|
19
20
|
|
|
20
21
|
mod bridge;
|
|
21
|
-
mod errors;
|
|
22
|
-
mod escape;
|
|
23
|
-
mod filesystem;
|
|
24
|
-
mod gvl;
|
|
25
|
-
mod pinned_value;
|
|
26
22
|
mod snapshot;
|
|
27
23
|
|
|
28
24
|
use magnus::{
|
|
29
|
-
function, method, prelude::*,
|
|
30
|
-
|
|
31
|
-
Value,
|
|
25
|
+
function, method, prelude::*, typed_data::DataTypeFunctions, value::Opaque,
|
|
26
|
+
Error as MagnusError, RArray, RHash, RModule, RString, Ruby, Symbol, TypedData, Value,
|
|
32
27
|
};
|
|
33
28
|
|
|
34
29
|
use std::path::Path;
|
|
@@ -36,23 +31,18 @@ use std::sync::Arc;
|
|
|
36
31
|
use std::time::Duration;
|
|
37
32
|
|
|
38
33
|
use kobako_runtime::dispatch::DispatchHandler;
|
|
34
|
+
use kobako_runtime::limits::{Cap, Limits};
|
|
35
|
+
use kobako_runtime::net::Backends;
|
|
39
36
|
use kobako_runtime::profile::Profile;
|
|
40
37
|
use kobako_runtime::runtime::{Entry, Runtime as ContractRuntime};
|
|
41
38
|
use kobako_transport::envelope::{Bindings, Flag, Preamble, Snippet, Snippets};
|
|
42
39
|
use kobako_wasmtime::{Config, Driver};
|
|
43
40
|
|
|
41
|
+
use crate::{errors, filesystem, gvl, name_of, network, symbol_name_of, to_bytes, value_by_name};
|
|
44
42
|
use snapshot::Snapshot;
|
|
45
43
|
|
|
46
|
-
/// The one place the boundary borrows an RString's bytes: the borrow does
|
|
47
|
-
/// not outlive this call, so no Ruby allocation can move the RString
|
|
48
|
-
/// between the borrow and the copy.
|
|
49
|
-
fn rstring_to_vec(s: RString) -> Vec<u8> {
|
|
50
|
-
// SAFETY: see item doc.
|
|
51
|
-
unsafe { s.as_slice() }.to_vec()
|
|
52
|
-
}
|
|
53
|
-
|
|
54
44
|
/// `kind` arrives as a Symbol so the registry never names the wire's
|
|
55
|
-
/// discriminant; an
|
|
45
|
+
/// discriminant; an unknown kind raises rather than defaulting.
|
|
56
46
|
fn to_snippets(ruby: &Ruby, entries: RArray) -> Result<Snippets, MagnusError> {
|
|
57
47
|
let mut snippets = Snippets {
|
|
58
48
|
entries: Vec::with_capacity(entries.len()),
|
|
@@ -66,7 +56,7 @@ fn to_snippets(ruby: &Ruby, entries: RArray) -> Result<Snippets, MagnusError> {
|
|
|
66
56
|
body: entry.entry(2)?,
|
|
67
57
|
},
|
|
68
58
|
"bytecode" => Snippet::Bytecode {
|
|
69
|
-
body:
|
|
59
|
+
body: to_bytes(entry.entry(2)?),
|
|
70
60
|
},
|
|
71
61
|
other => {
|
|
72
62
|
return Err(MagnusError::new(
|
|
@@ -106,27 +96,20 @@ fn to_preamble(ruby: &Ruby, parts: RArray) -> Result<Preamble, MagnusError> {
|
|
|
106
96
|
// Ruby init
|
|
107
97
|
// ---------------------------------------------------------------------------
|
|
108
98
|
|
|
109
|
-
pub fn
|
|
110
|
-
// Error hierarchy lives in `lib/kobako/errors.rb`; the ext raises
|
|
111
|
-
// directly into those classes through the constructors and mappers
|
|
112
|
-
// in `runtime/errors.rs` — no intermediate hierarchy is registered.
|
|
113
|
-
|
|
114
|
-
// This extension is the only frontend in its process, so the first
|
|
115
|
-
// registration is the one that stands.
|
|
116
|
-
let _ = kobako_runtime::sched::register_blocking(Box::new(gvl::GvlFreeWait));
|
|
117
|
-
|
|
99
|
+
pub(crate) fn register(ruby: &Ruby, kobako: RModule) -> Result<(), MagnusError> {
|
|
118
100
|
let runtime = kobako.define_class("Runtime", ruby.class_object())?;
|
|
119
101
|
runtime.define_singleton_method("from_path", function!(Runtime::from_path, 2))?;
|
|
102
|
+
runtime.define_singleton_method("default_options", function!(Runtime::default_options, 0))?;
|
|
103
|
+
runtime.define_singleton_method("normalize_caps", function!(normalize_caps, 1))?;
|
|
104
|
+
runtime.define_singleton_method("value_budget_of", function!(value_budget_of, 1))?;
|
|
120
105
|
runtime.define_method("eval", method!(Runtime::eval, 3))?;
|
|
121
106
|
runtime.define_method("run", method!(Runtime::run, 4))?;
|
|
122
107
|
runtime.define_method("profile", method!(Runtime::profile, 0))?;
|
|
123
108
|
runtime.define_method("compile", method!(Runtime::compile, 1))?;
|
|
124
|
-
runtime.define_singleton_method("validate_mount", function!(filesystem::validate_mount, 3))?;
|
|
125
109
|
// The guest re-enters for a block yield through a frame-scoped
|
|
126
110
|
// `Kobako::GuestYielder` the dispatcher hands the Proc, not a method on
|
|
127
111
|
// Runtime.
|
|
128
112
|
bridge::register(kobako)?;
|
|
129
|
-
filesystem::register(ruby, kobako)?;
|
|
130
113
|
|
|
131
114
|
// Snapshot — the per-invocation result object each entry point returns.
|
|
132
115
|
snapshot::register(ruby, kobako)?;
|
|
@@ -141,16 +124,133 @@ pub fn init(ruby: &Ruby, kobako: RModule) -> Result<(), MagnusError> {
|
|
|
141
124
|
Ok(())
|
|
142
125
|
}
|
|
143
126
|
|
|
127
|
+
/// Each cap in `caps`, read as `to_limits` reads it and keyed as
|
|
128
|
+
/// `Kobako::SandboxOptions` names it.
|
|
129
|
+
fn normalize_caps(caps: RHash) -> Result<RHash, MagnusError> {
|
|
130
|
+
let ruby = Ruby::get().expect("Ruby thread");
|
|
131
|
+
caps_of(&ruby, &to_limits(&ruby, caps)?)
|
|
132
|
+
}
|
|
133
|
+
|
|
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
|
+
/// The caps in `caps`, keyed as `Kobako::SandboxOptions` names them, each read
|
|
143
|
+
/// as its kind: a deadline as seconds, a positive finite Numeric, and every
|
|
144
|
+
/// other cap as a positive Integer, `nil` asking for none. One outside its
|
|
145
|
+
/// kind is refused as an argument error, and a zero by the runtime's own
|
|
146
|
+
/// check, which the Rust SDK asks too.
|
|
147
|
+
fn to_limits(ruby: &Ruby, caps: RHash) -> Result<Limits, MagnusError> {
|
|
148
|
+
let mut limits = Limits::default();
|
|
149
|
+
for (name, cap) in limits.caps_mut() {
|
|
150
|
+
let value = caps.fetch(ruby.to_symbol(name))?;
|
|
151
|
+
match cap {
|
|
152
|
+
Cap::Deadline(deadline) => *deadline = to_deadline(ruby, name, value)?,
|
|
153
|
+
Cap::Count(count) => *count = to_count(ruby, name, value)?,
|
|
154
|
+
}
|
|
155
|
+
}
|
|
156
|
+
limits
|
|
157
|
+
.check()
|
|
158
|
+
.map_err(|zero| MagnusError::new(ruby.exception_arg_error(), zero.to_string()))?;
|
|
159
|
+
Ok(limits)
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
/// Each cap `limits` holds, keyed as `Kobako::SandboxOptions` names it, a
|
|
163
|
+
/// deadline in seconds.
|
|
164
|
+
fn caps_of(ruby: &Ruby, limits: &Limits) -> Result<RHash, MagnusError> {
|
|
165
|
+
let caps = ruby.hash_new();
|
|
166
|
+
let mut limits = *limits;
|
|
167
|
+
for (name, cap) in limits.caps_mut() {
|
|
168
|
+
let key = ruby.to_symbol(name);
|
|
169
|
+
match cap {
|
|
170
|
+
Cap::Deadline(deadline) => caps.aset(key, deadline.map(|d| d.as_secs_f64()))?,
|
|
171
|
+
Cap::Count(count) => caps.aset(key, *count)?,
|
|
172
|
+
}
|
|
173
|
+
}
|
|
174
|
+
Ok(caps)
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
/// The deadline `value` names in seconds for `name`, none for `nil`. A
|
|
178
|
+
/// deadline that never arrives, is past what a `Duration` counts, or is not a
|
|
179
|
+
/// number, is refused: the way to ask for none is `nil`.
|
|
180
|
+
fn to_deadline(ruby: &Ruby, name: &str, value: Value) -> Result<Option<Duration>, MagnusError> {
|
|
181
|
+
if value.is_nil() {
|
|
182
|
+
return Ok(None);
|
|
183
|
+
}
|
|
184
|
+
if !value.is_kind_of(ruby.class_numeric()) {
|
|
185
|
+
return Err(MagnusError::new(
|
|
186
|
+
ruby.exception_type_error(),
|
|
187
|
+
format!(
|
|
188
|
+
"{name} must be Numeric or nil, got {}",
|
|
189
|
+
value.class().inspect()
|
|
190
|
+
),
|
|
191
|
+
));
|
|
192
|
+
}
|
|
193
|
+
let seconds: f64 = value.funcall("to_f", ())?;
|
|
194
|
+
let refusal = if seconds > 0.0 && seconds.is_finite() {
|
|
195
|
+
ruby.exception_range_error()
|
|
196
|
+
} else {
|
|
197
|
+
ruby.exception_arg_error()
|
|
198
|
+
};
|
|
199
|
+
match Duration::try_from_secs_f64(seconds) {
|
|
200
|
+
Ok(deadline) if seconds.is_finite() => Ok(Some(deadline)),
|
|
201
|
+
_ => Err(MagnusError::new(
|
|
202
|
+
refusal,
|
|
203
|
+
format!(
|
|
204
|
+
"{name} must be > 0 and at most {} seconds (got {})",
|
|
205
|
+
Duration::MAX.as_secs(),
|
|
206
|
+
value.inspect()
|
|
207
|
+
),
|
|
208
|
+
)),
|
|
209
|
+
}
|
|
210
|
+
}
|
|
211
|
+
|
|
212
|
+
/// The count `value` names for the cap `name`, none for `nil`; anything but
|
|
213
|
+
/// an Integer a `usize` holds is refused.
|
|
214
|
+
fn to_count(ruby: &Ruby, name: &str, value: Value) -> Result<Option<usize>, MagnusError> {
|
|
215
|
+
if value.is_nil() {
|
|
216
|
+
return Ok(None);
|
|
217
|
+
}
|
|
218
|
+
let Some(integer) = magnus::Integer::from_value(value) else {
|
|
219
|
+
return Err(MagnusError::new(
|
|
220
|
+
ruby.exception_type_error(),
|
|
221
|
+
format!(
|
|
222
|
+
"{name} must be an Integer or nil, got {}",
|
|
223
|
+
value.class().inspect()
|
|
224
|
+
),
|
|
225
|
+
));
|
|
226
|
+
};
|
|
227
|
+
let refusal = if value.funcall::<_, _, bool>("negative?", ())? {
|
|
228
|
+
ruby.exception_arg_error()
|
|
229
|
+
} else {
|
|
230
|
+
ruby.exception_range_error()
|
|
231
|
+
};
|
|
232
|
+
match integer.to_usize().ok() {
|
|
233
|
+
Some(count) => Ok(Some(count)),
|
|
234
|
+
None => Err(MagnusError::new(
|
|
235
|
+
refusal,
|
|
236
|
+
format!(
|
|
237
|
+
"{name} must be a positive Integer or nil, got {}",
|
|
238
|
+
value.inspect()
|
|
239
|
+
),
|
|
240
|
+
)),
|
|
241
|
+
}
|
|
242
|
+
}
|
|
243
|
+
|
|
144
244
|
#[derive(TypedData)]
|
|
145
245
|
#[magnus(class = "Kobako::Runtime", free_immediately, size)]
|
|
146
246
|
struct Runtime {
|
|
147
|
-
// The
|
|
148
|
-
//
|
|
149
|
-
//
|
|
150
|
-
//
|
|
151
|
-
//
|
|
152
|
-
//
|
|
153
|
-
|
|
247
|
+
// The runtime every invocation runs on, held behind the contract so the
|
|
248
|
+
// driver is named only where it is built. The Runtime holds no
|
|
249
|
+
// per-invocation state — each `#eval` / `#run` takes its dispatch
|
|
250
|
+
// handler as an argument and returns its whole result as a `Snapshot`
|
|
251
|
+
// — so the contract's `Send + Sync` carries the type with no interior
|
|
252
|
+
// mutability to guard.
|
|
253
|
+
runtime: Box<dyn ContractRuntime + Send + Sync>,
|
|
154
254
|
// Whether each invocation releases Ruby's GVL for its guest span
|
|
155
255
|
// (`gvl: :release`) or holds it throughout (`gvl: :hold`). Fixed at
|
|
156
256
|
// construction; a `bool` carries no interior mutability, so the type
|
|
@@ -161,99 +261,69 @@ struct Runtime {
|
|
|
161
261
|
impl DataTypeFunctions for Runtime {}
|
|
162
262
|
|
|
163
263
|
impl Runtime {
|
|
264
|
+
/// Each option `from_path` takes, keyed as `Kobako::SandboxOptions` names
|
|
265
|
+
/// it, at the runtime's default, so every frontend applies one set. A
|
|
266
|
+
/// run holds the GVL unless the Host App asks to release it.
|
|
267
|
+
fn default_options() -> Result<RHash, MagnusError> {
|
|
268
|
+
let ruby = Ruby::get().expect("Ruby thread");
|
|
269
|
+
let options = caps_of(&ruby, &Limits::default())?;
|
|
270
|
+
options.aset(
|
|
271
|
+
ruby.to_symbol("profile"),
|
|
272
|
+
to_profile_name(&ruby, Profile::default()),
|
|
273
|
+
)?;
|
|
274
|
+
options.aset(ruby.to_symbol("gvl"), ruby.to_symbol("hold"))?;
|
|
275
|
+
options.aset(ruby.to_symbol("network"), ruby.qnil())?;
|
|
276
|
+
Ok(options)
|
|
277
|
+
}
|
|
278
|
+
|
|
164
279
|
/// The only Ruby-facing constructor, so Engine and Module are never
|
|
165
|
-
/// visible to Ruby. Every option is a required keyword
|
|
166
|
-
///
|
|
280
|
+
/// visible to Ruby. Every option is a required keyword, as
|
|
281
|
+
/// `default_options` answers each.
|
|
167
282
|
fn from_path(path: String, options: RHash) -> Result<Self, MagnusError> {
|
|
168
283
|
let ruby = Ruby::get().expect("Ruby thread");
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
);
|
|
179
|
-
let options = get_kwargs::<_, Options, (), ()>(
|
|
180
|
-
options,
|
|
181
|
-
&[
|
|
182
|
-
"timeout",
|
|
183
|
-
"setup_timeout",
|
|
184
|
-
"memory_limit",
|
|
185
|
-
"stdout_limit",
|
|
186
|
-
"stderr_limit",
|
|
187
|
-
"open_file_limit",
|
|
188
|
-
"profile",
|
|
189
|
-
"gvl",
|
|
190
|
-
],
|
|
191
|
-
&[],
|
|
192
|
-
)?;
|
|
193
|
-
let (
|
|
194
|
-
timeout_seconds,
|
|
195
|
-
setup_timeout_seconds,
|
|
196
|
-
memory_limit,
|
|
197
|
-
stdout_limit,
|
|
198
|
-
stderr_limit,
|
|
199
|
-
open_file_limit,
|
|
200
|
-
profile,
|
|
201
|
-
gvl,
|
|
202
|
-
) = options.required;
|
|
203
|
-
// An invalid cap argument is a Host App programming error and
|
|
204
|
-
// raises `ArgumentError`, outside the construction-failure
|
|
205
|
-
// `SetupError` branch. `SandboxOptions` is the primary guard (it
|
|
206
|
-
// never lets a bad timeout reach here); this is defence-in-depth
|
|
207
|
-
// for direct `from_path` calls.
|
|
208
|
-
let to_duration = |seconds: Option<f64>, name: &str| match seconds {
|
|
209
|
-
None => Ok(None),
|
|
210
|
-
Some(secs) if secs.is_finite() && secs > 0.0 => Ok(Some(Duration::from_secs_f64(secs))),
|
|
211
|
-
Some(secs) => Err(MagnusError::new(
|
|
284
|
+
let limits = to_limits(&ruby, options)?;
|
|
285
|
+
let profile: Value = options.fetch(ruby.to_symbol("profile"))?;
|
|
286
|
+
let gvl: Value = options.fetch(ruby.to_symbol("gvl"))?;
|
|
287
|
+
// A value kobako does not name a profile has no profile to stand
|
|
288
|
+
// for, so it is refused rather than read as a weaker one.
|
|
289
|
+
let Some(profile) =
|
|
290
|
+
symbol_name_of(profile)?.and_then(|name| value_by_name(&PROFILE_NAMES, &name))
|
|
291
|
+
else {
|
|
292
|
+
return Err(MagnusError::new(
|
|
212
293
|
ruby.exception_arg_error(),
|
|
213
|
-
format!(
|
|
214
|
-
|
|
294
|
+
format!(
|
|
295
|
+
"profile must be :permissive or :hermetic, got {}",
|
|
296
|
+
profile.inspect()
|
|
297
|
+
),
|
|
298
|
+
));
|
|
215
299
|
};
|
|
216
|
-
|
|
217
|
-
let
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
let profile = match profile.name()?.as_ref() {
|
|
222
|
-
"hermetic" => Profile::Hermetic,
|
|
223
|
-
"permissive" => Profile::Permissive,
|
|
224
|
-
other => {
|
|
300
|
+
// A mode kobako does not name is refused as a profile is.
|
|
301
|
+
let release_gvl = match symbol_name_of(gvl)?.as_deref() {
|
|
302
|
+
Some("hold") => false,
|
|
303
|
+
Some("release") => true,
|
|
304
|
+
_ => {
|
|
225
305
|
return Err(MagnusError::new(
|
|
226
306
|
ruby.exception_arg_error(),
|
|
227
|
-
format!("
|
|
307
|
+
format!("gvl must be :hold or :release, got {}", gvl.inspect()),
|
|
228
308
|
));
|
|
229
309
|
}
|
|
230
310
|
};
|
|
231
|
-
|
|
232
|
-
//
|
|
233
|
-
|
|
234
|
-
let
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
other => {
|
|
238
|
-
return Err(MagnusError::new(
|
|
239
|
-
ruby.exception_arg_error(),
|
|
240
|
-
format!("gvl must be :hold or :release, got :{other}"),
|
|
241
|
-
));
|
|
242
|
-
}
|
|
311
|
+
|
|
312
|
+
// `nil` is the host's own network; anything else must be a Mock.
|
|
313
|
+
let mock: Value = options.fetch(ruby.to_symbol("network"))?;
|
|
314
|
+
let backends = match mock.is_nil() {
|
|
315
|
+
true => Backends::default(),
|
|
316
|
+
false => <&network::MockNet>::try_convert(mock)?.backends(),
|
|
243
317
|
};
|
|
244
318
|
|
|
245
319
|
let mut config = Config::default();
|
|
246
|
-
config.
|
|
247
|
-
config.setup_timeout = setup_timeout;
|
|
248
|
-
config.memory_limit = memory_limit;
|
|
249
|
-
config.stdout_limit = stdout_limit;
|
|
250
|
-
config.stderr_limit = stderr_limit;
|
|
251
|
-
config.open_file_limit = open_file_limit;
|
|
320
|
+
config.limits = limits;
|
|
252
321
|
config.profile = profile;
|
|
253
|
-
|
|
254
|
-
|
|
322
|
+
config.network = backends;
|
|
323
|
+
let driver = Driver::from_file(Path::new(&path), config)
|
|
324
|
+
.map_err(|e| errors::from_setup(&ruby, e))?;
|
|
255
325
|
Ok(Self {
|
|
256
|
-
driver,
|
|
326
|
+
runtime: Box::new(driver),
|
|
257
327
|
release_gvl,
|
|
258
328
|
})
|
|
259
329
|
}
|
|
@@ -263,11 +333,12 @@ impl Runtime {
|
|
|
263
333
|
// argument and returns a `Snapshot` for any completed invocation —
|
|
264
334
|
// success or trap alike. Only a could-not-start fault (a missing export
|
|
265
335
|
// or a fault before the export call) raises directly, as the class
|
|
266
|
-
// `errors::
|
|
336
|
+
// `errors::from_invoke` gives its cause, since it yields no `Snapshot`.
|
|
267
337
|
//
|
|
268
|
-
// `setup` is the invocation's `[paths, snippets, flags, mounts
|
|
269
|
-
// Preamble's three parts, then the Mounts as
|
|
270
|
-
// `filesystem::to_grants` reads them
|
|
338
|
+
// `setup` is the invocation's `[paths, snippets, flags, mounts,
|
|
339
|
+
// networks]`: the Preamble's three parts, then the Mounts as
|
|
340
|
+
// `filesystem::to_grants` reads them and the network entries as
|
|
341
|
+
// `network::to_grants` reads them.
|
|
271
342
|
// -----------------------------------------------------------------
|
|
272
343
|
|
|
273
344
|
fn eval(
|
|
@@ -276,7 +347,7 @@ impl Runtime {
|
|
|
276
347
|
setup: RArray,
|
|
277
348
|
source: RString,
|
|
278
349
|
) -> Result<Snapshot, MagnusError> {
|
|
279
|
-
let source =
|
|
350
|
+
let source = to_bytes(source);
|
|
280
351
|
self.invoke(dispatch, setup, Entry::Eval { source: &source })
|
|
281
352
|
}
|
|
282
353
|
|
|
@@ -292,7 +363,7 @@ impl Runtime {
|
|
|
292
363
|
setup,
|
|
293
364
|
Entry::Run {
|
|
294
365
|
entrypoint: &entrypoint,
|
|
295
|
-
payload:
|
|
366
|
+
payload: to_bytes(payload),
|
|
296
367
|
},
|
|
297
368
|
)
|
|
298
369
|
}
|
|
@@ -312,30 +383,30 @@ impl Runtime {
|
|
|
312
383
|
};
|
|
313
384
|
let handler = build_handler(dispatch);
|
|
314
385
|
let preamble = to_preamble(&ruby, setup)?;
|
|
315
|
-
let (grants,
|
|
386
|
+
let (mut grants, mut pins) = filesystem::to_grants(&ruby, setup.entry(3)?)?;
|
|
387
|
+
let (networks, network_pins) = network::to_grants(&ruby, setup.entry(4)?)?;
|
|
388
|
+
grants.extend(networks);
|
|
389
|
+
pins.extend(network_pins);
|
|
316
390
|
// Release the GVL around the guest span iff this Sandbox asks for it;
|
|
317
|
-
// the closure touches no Ruby VALUE (the
|
|
391
|
+
// the closure touches no Ruby VALUE (the runtime is magnus-free, and a
|
|
318
392
|
// guest→host dispatch, or a mount's block, re-acquires the GVL).
|
|
319
|
-
|
|
320
|
-
self.
|
|
321
|
-
})
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
.map(|snapshot| Snapshot::new(snapshot, verb))
|
|
325
|
-
.map_err(|e| errors::to_magnus(&ruby, e))
|
|
393
|
+
gvl::run(&ruby, self.release_gvl, || {
|
|
394
|
+
self.runtime.invoke(entry, &preamble, handler, &grants)
|
|
395
|
+
})?
|
|
396
|
+
.map(|snapshot| Snapshot::new(snapshot, verb))
|
|
397
|
+
.map_err(|e| errors::from_invoke(&ruby, e))
|
|
326
398
|
}
|
|
327
399
|
|
|
328
400
|
/// The bytecode of the source snippets, one binary String each in
|
|
329
401
|
/// order, or `nil` when the guest compiled nothing; no failure raises,
|
|
330
402
|
/// since the Sandbox keeps the source either way. Takes the Preamble an
|
|
331
|
-
/// invocation sends, `paths` included, so the
|
|
403
|
+
/// invocation sends, `paths` included, so the runtime can tell whether
|
|
332
404
|
/// the bytecode still fits it; no dispatch handler is bound, since
|
|
333
405
|
/// compiling runs no script.
|
|
334
406
|
fn compile(&self, preamble: RArray) -> Result<Option<RArray>, MagnusError> {
|
|
335
407
|
let ruby = Ruby::get().expect("Ruby thread");
|
|
336
408
|
let preamble = to_preamble(&ruby, preamble)?;
|
|
337
|
-
let bytecodes = gvl::
|
|
338
|
-
escape::resume()?;
|
|
409
|
+
let bytecodes = gvl::run(&ruby, self.release_gvl, || self.runtime.compile(&preamble))?;
|
|
339
410
|
Ok(bytecodes.map(|bytecodes| {
|
|
340
411
|
ruby.ary_from_iter(
|
|
341
412
|
bytecodes
|
|
@@ -345,19 +416,26 @@ impl Runtime {
|
|
|
345
416
|
}))
|
|
346
417
|
}
|
|
347
418
|
|
|
348
|
-
/// The
|
|
349
|
-
/// posture its `profile:` option requested.
|
|
419
|
+
/// The isolation profile the runtime declares it built.
|
|
350
420
|
fn profile(&self) -> Symbol {
|
|
351
421
|
let ruby = Ruby::get().expect("Ruby thread");
|
|
352
|
-
|
|
353
|
-
Profile::Hermetic => ruby.to_symbol("hermetic"),
|
|
354
|
-
Profile::Permissive => ruby.to_symbol("permissive"),
|
|
355
|
-
}
|
|
422
|
+
to_profile_name(&ruby, self.runtime.profile())
|
|
356
423
|
}
|
|
357
424
|
}
|
|
358
425
|
|
|
426
|
+
/// Each profile beside the name the `profile:` option takes it by.
|
|
427
|
+
const PROFILE_NAMES: [(Profile, &str); 2] = [
|
|
428
|
+
(Profile::Hermetic, "hermetic"),
|
|
429
|
+
(Profile::Permissive, "permissive"),
|
|
430
|
+
];
|
|
431
|
+
|
|
432
|
+
/// `profile` as the Symbol the `profile:` option names it by.
|
|
433
|
+
fn to_profile_name(ruby: &Ruby, profile: Profile) -> Symbol {
|
|
434
|
+
ruby.to_symbol(name_of(&PROFILE_NAMES, &profile).expect("every profile has a name"))
|
|
435
|
+
}
|
|
436
|
+
|
|
359
437
|
/// The Proc stays GC-rooted for the synchronous `#eval` / `#run` call as a
|
|
360
|
-
/// live method argument on the Ruby stack, so the
|
|
438
|
+
/// live method argument on the Ruby stack, so the runtime only borrows it
|
|
361
439
|
/// (the safety contract on `kobako_runtime::runtime::Runtime`).
|
|
362
440
|
fn build_handler(dispatch: Value) -> Option<Arc<dyn DispatchHandler>> {
|
|
363
441
|
if dispatch.is_nil() {
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
//! The Wire's rules a Ruby frontend asks before it writes a name the guest
|
|
2
|
+
//! interns or issues a Capability Handle id, so both frontends hold to the
|
|
3
|
+
//! one rule the Envelope tier keeps.
|
|
4
|
+
|
|
5
|
+
use magnus::{function, prelude::*, Error as MagnusError, RModule, RString};
|
|
6
|
+
|
|
7
|
+
use kobako_transport::envelope::name::{collides, is_constant_name, is_constant_path, is_reserved};
|
|
8
|
+
use kobako_transport::envelope::MAX_HANDLE_ID;
|
|
9
|
+
|
|
10
|
+
pub(crate) fn register(kobako: RModule) -> Result<(), MagnusError> {
|
|
11
|
+
kobako.define_singleton_method("max_handle_id", function!(|| MAX_HANDLE_ID, 0))?;
|
|
12
|
+
kobako.define_singleton_method(
|
|
13
|
+
"constant_name?",
|
|
14
|
+
function!(|name: RString| holds(name, is_constant_name), 1),
|
|
15
|
+
)?;
|
|
16
|
+
kobako.define_singleton_method(
|
|
17
|
+
"constant_path?",
|
|
18
|
+
function!(|path: RString| holds(path, is_constant_path), 1),
|
|
19
|
+
)?;
|
|
20
|
+
kobako.define_singleton_method(
|
|
21
|
+
"reserved_path?",
|
|
22
|
+
function!(|path: RString| holds(path, is_reserved), 1),
|
|
23
|
+
)?;
|
|
24
|
+
kobako.define_singleton_method("paths_collide?", function!(paths_collide, 2))?;
|
|
25
|
+
Ok(())
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
/// Whether the paths `path` and `bound` spell cannot both be bound; bytes
|
|
29
|
+
/// that spell no text are no name at all, so they collide with nothing.
|
|
30
|
+
fn paths_collide(path: RString, bound: RString) -> bool {
|
|
31
|
+
let (path, bound) = (crate::to_bytes(path), crate::to_bytes(bound));
|
|
32
|
+
match (std::str::from_utf8(&path), std::str::from_utf8(&bound)) {
|
|
33
|
+
(Ok(path), Ok(bound)) => collides(path, bound),
|
|
34
|
+
_ => false,
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
/// Whether `rule` holds for the text `s` spells; bytes that spell no text
|
|
39
|
+
/// are no name at all.
|
|
40
|
+
fn holds(s: RString, rule: fn(&str) -> bool) -> bool {
|
|
41
|
+
std::str::from_utf8(&crate::to_bytes(s)).is_ok_and(rule)
|
|
42
|
+
}
|
|
@@ -66,13 +66,25 @@ module Kobako
|
|
|
66
66
|
|
|
67
67
|
def validate(extension)
|
|
68
68
|
source = extension.source
|
|
69
|
-
raise
|
|
69
|
+
raise TypeError, "Extension #source must be a String, got #{source.class}" unless source.is_a?(String)
|
|
70
70
|
|
|
71
|
+
validate_dependencies(extension.depends_on)
|
|
71
72
|
backend = extension.backend
|
|
72
73
|
return if backend.nil?
|
|
73
74
|
return if backend.respond_to?(:path) && backend.respond_to?(:object) && backend.respond_to?(:provider)
|
|
74
75
|
|
|
75
|
-
raise
|
|
76
|
+
raise TypeError, "Extension #backend must expose #path, #object, and #provider"
|
|
77
|
+
end
|
|
78
|
+
|
|
79
|
+
# Each dependency names an Extension as its +name+ does.
|
|
80
|
+
def validate_dependencies(dependencies)
|
|
81
|
+
return if dependencies.nil?
|
|
82
|
+
unless dependencies.is_a?(Array)
|
|
83
|
+
raise TypeError,
|
|
84
|
+
"Extension #depends_on must be an Array, got #{dependencies.class}"
|
|
85
|
+
end
|
|
86
|
+
|
|
87
|
+
dependencies.each { |dependency| Kobako.to_name(dependency, "Extension #depends_on entry") }
|
|
76
88
|
end
|
|
77
89
|
|
|
78
90
|
def unmet_dependency
|
data/lib/kobako/catalog/flags.rb
CHANGED
|
@@ -13,12 +13,10 @@ module Kobako
|
|
|
13
13
|
end
|
|
14
14
|
|
|
15
15
|
def register(name, value, advisory:)
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
end
|
|
19
|
-
raise ArgumentError, "flag value must be a String, got #{value.class}" unless value.is_a?(String)
|
|
16
|
+
name = Kobako.to_name(name, "flag name")
|
|
17
|
+
raise TypeError, "flag value must be a String, got #{value.class}" unless value.is_a?(String)
|
|
20
18
|
|
|
21
|
-
key = Catalog.to_utf8(name
|
|
19
|
+
key = Catalog.to_utf8(name, "flag name")
|
|
22
20
|
raise ArgumentError, "flag #{key.inspect} already set" if @entries.key?(key)
|
|
23
21
|
|
|
24
22
|
@entries[key] = [Catalog.to_utf8(value, "flag value"), !advisory]
|
|
@@ -4,7 +4,7 @@ module Kobako
|
|
|
4
4
|
module Catalog
|
|
5
5
|
class Grants # :nodoc:
|
|
6
6
|
def initialize
|
|
7
|
-
@entries = {} # : Hash[
|
|
7
|
+
@entries = {} # : Hash[key, Mount | Network]
|
|
8
8
|
end
|
|
9
9
|
|
|
10
10
|
# Each Grant decides what it cannot stand beside.
|
|
@@ -20,10 +20,11 @@ module Kobako
|
|
|
20
20
|
@entries.key?(grant.key)
|
|
21
21
|
end
|
|
22
22
|
|
|
23
|
-
#
|
|
24
|
-
# the Grant it shares a key with.
|
|
23
|
+
# The Mounts, then the network entries, each as the native side reads
|
|
24
|
+
# it, an override standing in for the Grant it shares a key with.
|
|
25
25
|
def entries(overrides = {})
|
|
26
|
-
@entries.merge(overrides).values
|
|
26
|
+
grants = @entries.merge(overrides).values
|
|
27
|
+
[grants.grep(Mount).map(&:to_native), grants.grep(Network).map(&:to_native)]
|
|
27
28
|
end
|
|
28
29
|
end
|
|
29
30
|
end
|