kobako 0.30.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 +460 -0
- data/Cargo.lock +868 -447
- data/README.md +43 -4
- data/ROADMAP.md +1 -2
- data/crates/kobako-runtime/CHANGELOG.md +309 -0
- data/crates/kobako-runtime/Cargo.toml +58 -11
- data/crates/kobako-runtime/README.md +7 -4
- data/crates/kobako-runtime/src/cred.rs +136 -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/fid.rs +300 -22
- data/crates/kobako-runtime/src/filesystem/bind/lookup.rs +101 -29
- data/crates/kobako-runtime/src/filesystem/bind/unicode.rs +109 -0
- data/crates/kobako-runtime/src/filesystem/bind/walk.rs +52 -34
- data/crates/kobako-runtime/src/filesystem/bind.rs +812 -447
- 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 +11 -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 +120 -37
- data/crates/kobako-runtime/src/filesystem/overlay.rs +578 -388
- data/crates/kobako-runtime/src/filesystem/pipe.rs +12 -9
- data/crates/kobako-runtime/src/filesystem/rdir.rs +14 -50
- data/crates/kobako-runtime/src/filesystem/testing.rs +31 -0
- 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 +219 -173
- data/crates/kobako-runtime/src/filesystem/wrap.rs +36 -52
- data/crates/kobako-runtime/src/filesystem.rs +8 -3
- data/crates/kobako-runtime/src/fork.rs +319 -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 +171 -0
- data/crates/kobako-runtime/src/locking/seqlock.rs +29 -3
- data/crates/kobako-runtime/src/locking.rs +518 -113
- data/crates/kobako-runtime/src/memcg.rs +306 -0
- data/crates/kobako-runtime/src/mount.rs +37 -9
- data/crates/kobako-runtime/src/net/backends.rs +43 -0
- data/crates/kobako-runtime/src/net/connections.rs +219 -0
- data/crates/kobako-runtime/src/net/credential.rs +125 -0
- data/crates/kobako-runtime/src/net/egress.rs +180 -0
- data/crates/kobako-runtime/src/net/family.rs +181 -0
- data/crates/kobako-runtime/src/net/http.rs +3165 -0
- data/crates/kobako-runtime/src/net/lookup.rs +385 -0
- data/crates/kobako-runtime/src/net/mock.rs +251 -0
- data/crates/kobako-runtime/src/net/namespace.rs +1368 -0
- data/crates/kobako-runtime/src/net/reactor.rs +626 -0
- data/crates/kobako-runtime/src/net/resolv/darwin.rs +280 -0
- data/crates/kobako-runtime/src/net/resolv/stub/tests.rs +519 -0
- data/crates/kobako-runtime/src/net/resolv/stub.rs +542 -0
- data/crates/kobako-runtime/src/net/resolv/windows.rs +218 -0
- data/crates/kobako-runtime/src/net/resolv.rs +251 -0
- data/crates/kobako-runtime/src/net/skbuff.rs +109 -0
- data/crates/kobako-runtime/src/net/socket.rs +869 -0
- data/crates/kobako-runtime/src/net/task.rs +223 -0
- data/crates/kobako-runtime/src/net/tcp.rs +1840 -0
- data/crates/kobako-runtime/src/net/tls.rs +120 -0
- data/crates/kobako-runtime/src/net/transport.rs +304 -0
- data/crates/kobako-runtime/src/net/udp.rs +1105 -0
- data/crates/kobako-runtime/src/net.rs +45 -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 +338 -51
- 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 +125 -209
- 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 +210 -117
- data/crates/kobako-runtime/src/vfs/file.rs +243 -248
- data/crates/kobako-runtime/src/vfs/inode.rs +41 -45
- data/crates/kobako-runtime/src/vfs/kern_path.rs +62 -53
- data/crates/kobako-runtime/src/vfs/mount.rs +10 -9
- data/crates/kobako-runtime/src/vfs/namei.rs +1162 -818
- data/crates/kobako-runtime/src/vfs/namespace.rs +245 -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 +248 -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 +10 -6
- data/crates/kobako-runtime/src/wait.rs +381 -0
- data/crates/kobako-transport/CHANGELOG.md +38 -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 +208 -0
- data/crates/kobako-wasmtime/Cargo.toml +47 -7
- data/crates/kobako-wasmtime/README.md +4 -3
- data/crates/kobako-wasmtime/build/preview2.rs +1339 -0
- data/crates/kobako-wasmtime/build.rs +16 -0
- data/crates/kobako-wasmtime/src/abi.rs +18 -37
- data/crates/kobako-wasmtime/src/ambient.rs +43 -19
- 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 +106 -113
- data/crates/kobako-wasmtime/src/config.rs +52 -68
- data/crates/kobako-wasmtime/src/driver.rs +80 -82
- 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 +91 -15
- data/crates/kobako-wasmtime/src/lib.rs +7 -3
- data/crates/kobako-wasmtime/src/limiter.rs +130 -12
- data/crates/kobako-wasmtime/src/preview1/descriptors.rs +305 -0
- data/crates/kobako-wasmtime/src/preview1/differential/mount.rs +9 -20
- 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 +347 -312
- 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 +335 -388
- data/crates/kobako-wasmtime/src/preview1/path.rs +56 -121
- data/crates/kobako-wasmtime/src/preview1/poll.rs +255 -125
- data/crates/kobako-wasmtime/src/preview1/random.rs +125 -0
- data/crates/kobako-wasmtime/src/preview1/sock.rs +10 -8
- data/crates/kobako-wasmtime/src/preview1/testing.rs +31 -0
- data/crates/kobako-wasmtime/src/preview1/testsuite.rs +5 -15
- data/crates/kobako-wasmtime/src/preview1.rs +131 -51
- 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 +4319 -0
- data/crates/kobako-wasmtime/src/preview2/http.rs +1587 -0
- data/crates/kobako-wasmtime/src/preview2/io.rs +328 -0
- data/crates/kobako-wasmtime/src/preview2/sockets.rs +379 -0
- data/crates/kobako-wasmtime/src/preview2/table.rs +257 -0
- data/crates/kobako-wasmtime/src/preview2.rs +57 -0
- data/crates/kobako-wasmtime/src/trap.rs +88 -28
- data/crates/kobako-wasmtime/src/waits.rs +59 -0
- data/crates/kobako-wasmtime/src/wasi.rs +291 -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/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/{runtime/errors.rs → errors.rs} +34 -26
- data/ext/kobako/src/escape.rs +404 -0
- data/ext/kobako/src/{runtime/filesystem → filesystem}/grants.rs +34 -32
- data/ext/kobako/src/{runtime/filesystem → filesystem}/ruby.rs +158 -170
- data/ext/kobako/src/{runtime/filesystem.rs → filesystem.rs} +71 -23
- data/ext/kobako/src/fork.rs +72 -0
- data/ext/kobako/src/gvl.rs +729 -0
- data/ext/kobako/src/handoff.rs +490 -0
- data/ext/kobako/src/lib.rs +74 -3
- data/ext/kobako/src/network/mock.rs +187 -0
- data/ext/kobako/src/network.rs +593 -0
- data/ext/kobako/src/pinned_value.rs +44 -0
- data/ext/kobako/src/runtime/bridge.rs +58 -50
- data/ext/kobako/src/runtime/snapshot.rs +4 -4
- data/ext/kobako/src/runtime.rs +272 -163
- 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 +14 -32
- 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 +5 -5
- data/lib/kobako/codec/ext_types.rb +3 -3
- data/lib/kobako/codec/handle_walk.rb +3 -43
- data/lib/kobako/codec/state.rb +2 -18
- data/lib/kobako/codec/value_budget.rb +133 -41
- data/lib/kobako/codec.rb +6 -7
- data/lib/kobako/context.rb +24 -11
- data/lib/kobako/errors.rb +18 -18
- data/lib/kobako/extension.rb +3 -2
- data/lib/kobako/filesystem.rb +10 -3
- data/lib/kobako/fork.rb +42 -0
- data/lib/kobako/handle.rb +2 -3
- data/lib/kobako/names.rb +28 -0
- data/lib/kobako/network.rb +210 -0
- data/lib/kobako/outcome.rb +7 -7
- data/lib/kobako/runtime.rb +3 -2
- data/lib/kobako/sandbox.rb +73 -35
- data/lib/kobako/sandbox_options.rb +42 -109
- 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/usage.rb +4 -2
- data/lib/kobako/version.rb +1 -1
- data/lib/kobako.rb +8 -4
- data/release-please-config.json +15 -0
- data/sig/kobako/catalog/extensions.rbs +2 -0
- data/sig/kobako/catalog/grants.rbs +7 -4
- data/sig/kobako/catalog/handles.rbs +2 -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/state.rbs +0 -4
- data/sig/kobako/codec/value_budget.rbs +48 -5
- data/sig/kobako/codec.rbs +2 -2
- data/sig/kobako/context.rbs +3 -1
- data/sig/kobako/errors.rbs +4 -7
- data/sig/kobako/filesystem.rbs +21 -3
- data/sig/kobako/fork.rbs +17 -0
- data/sig/kobako/network.rbs +115 -0
- data/sig/kobako/outcome.rbs +1 -1
- data/sig/kobako/runtime.rbs +32 -3
- data/sig/kobako/sandbox.rbs +10 -2
- data/sig/kobako/sandbox_options.rbs +29 -40
- 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 +97 -37
- 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/escape.rs +0 -67
- data/ext/kobako/src/runtime/gvl.rs +0 -147
- data/ext/kobako/src/runtime/pinned_value.rs +0 -31
- data/lib/kobako/constant_name.rb +0 -8
- data/lib/kobako/pool.rb +0 -140
- data/sig/kobako/constant_name.rbs +0 -3
- data/sig/kobako/pool.rbs +0 -42
|
@@ -3,12 +3,14 @@
|
|
|
3
3
|
//!
|
|
4
4
|
//! Owned as the data of each per-invocation `wasmtime::Store`
|
|
5
5
|
//! and threaded through every host import —
|
|
6
|
-
//! the `
|
|
6
|
+
//! the `upcall` dispatcher reads the bound dispatch handler, the `preamble`
|
|
7
|
+
//! import the Invocation's Preamble,
|
|
7
8
|
//! and the WASI imports read the invocation's `Wasi` context, which the
|
|
8
9
|
//! Store is created with.
|
|
9
10
|
//!
|
|
10
11
|
//! The slot also carries the per-invocation wall-clock deadline and the
|
|
11
|
-
//!
|
|
12
|
+
//! `MemoryLimiter` charging the guest's growth to the memory budget its
|
|
13
|
+
//! Cred carries, both read from the wasmtime
|
|
12
14
|
//! `epoch_deadline_callback` / `ResourceLimiter` callbacks installed in
|
|
13
15
|
//! `Driver::new_store`.
|
|
14
16
|
|
|
@@ -16,7 +18,10 @@ use std::sync::Arc;
|
|
|
16
18
|
use std::time::{Duration, Instant};
|
|
17
19
|
|
|
18
20
|
use kobako_runtime::dispatch::DispatchHandler;
|
|
19
|
-
use kobako_runtime::endpoint::{Calls,
|
|
21
|
+
use kobako_runtime::endpoint::{Calls, ABANDONED};
|
|
22
|
+
use kobako_runtime::profile::Profile;
|
|
23
|
+
|
|
24
|
+
use wasmtime::{Engine, Store};
|
|
20
25
|
|
|
21
26
|
use crate::exports::Exports;
|
|
22
27
|
use crate::limiter::MemoryLimiter;
|
|
@@ -39,20 +44,48 @@ pub(crate) struct Invocation {
|
|
|
39
44
|
wall_time: Duration,
|
|
40
45
|
exports: Option<Exports>,
|
|
41
46
|
hostcall_fuel: usize,
|
|
47
|
+
preamble: Arc<[u8]>,
|
|
42
48
|
}
|
|
43
49
|
|
|
44
50
|
impl Invocation {
|
|
45
|
-
|
|
51
|
+
/// The guest's growth is charged to the budget `wasi`'s Cred carries, which
|
|
52
|
+
/// counts it against the run's `memory_limit`.
|
|
53
|
+
pub(crate) fn new(wasi: Wasi) -> Self {
|
|
54
|
+
let limiter = MemoryLimiter::new(wasi.cred().memory_budget().clone());
|
|
46
55
|
Self {
|
|
47
56
|
wasi,
|
|
48
57
|
calls: Calls::default(),
|
|
49
58
|
deadline: None,
|
|
50
|
-
limiter
|
|
59
|
+
limiter,
|
|
51
60
|
wall_entry: None,
|
|
52
61
|
wall_time: Duration::ZERO,
|
|
53
62
|
exports: None,
|
|
54
63
|
hostcall_fuel: 0,
|
|
64
|
+
preamble: Arc::from([]),
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
/// The Store the invocation's guest runs in. Each host call may have the
|
|
69
|
+
/// host copy at most the run's `memory_limit` out of the guest, or
|
|
70
|
+
/// wasmtime's default with no limit, as a one-call lift is overhead the
|
|
71
|
+
/// limit sizes.
|
|
72
|
+
pub(crate) fn into_store(self, engine: &Engine) -> wasmtime::Result<Store<Self>> {
|
|
73
|
+
let fuel = self.limiter.budget().limit();
|
|
74
|
+
let mut store = Store::try_new(engine, self)?;
|
|
75
|
+
if let Some(fuel) = fuel {
|
|
76
|
+
store.set_hostcall_fuel(fuel);
|
|
55
77
|
}
|
|
78
|
+
Ok(store)
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
/// The Preamble bytes the `preamble` import hands the guest; empty until
|
|
82
|
+
/// an entry sets them.
|
|
83
|
+
pub(crate) fn preamble(&self) -> Arc<[u8]> {
|
|
84
|
+
Arc::clone(&self.preamble)
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
pub(crate) fn set_preamble(&mut self, preamble: Vec<u8>) {
|
|
88
|
+
self.preamble = Arc::from(preamble);
|
|
56
89
|
}
|
|
57
90
|
|
|
58
91
|
/// How much of the guest's memory the call under way may have the host
|
|
@@ -107,25 +140,29 @@ impl Invocation {
|
|
|
107
140
|
self.deadline
|
|
108
141
|
}
|
|
109
142
|
|
|
143
|
+
pub(crate) fn profile(&self) -> Profile {
|
|
144
|
+
self.wasi.profile()
|
|
145
|
+
}
|
|
146
|
+
|
|
110
147
|
pub(crate) fn limiter_mut(&mut self) -> &mut MemoryLimiter {
|
|
111
148
|
&mut self.limiter
|
|
112
149
|
}
|
|
113
150
|
|
|
114
|
-
///
|
|
115
|
-
///
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
self.limiter.0.activate(baseline);
|
|
151
|
+
/// Growth past the `declared` bytes the Guest Binary's memories and
|
|
152
|
+
/// tables start with is charged from now on.
|
|
153
|
+
pub(crate) fn arm_memory_budget(&mut self, declared: usize) {
|
|
154
|
+
self.limiter.budget().arm(declared);
|
|
119
155
|
}
|
|
120
156
|
|
|
121
157
|
/// Disarmed once the export returns, so post-run host bookkeeping such
|
|
122
158
|
/// as reading the answer back is not charged to the script.
|
|
123
|
-
pub(crate) fn
|
|
124
|
-
self.limiter.
|
|
159
|
+
pub(crate) fn disarm_memory_budget(&mut self) {
|
|
160
|
+
self.limiter.budget().disarm();
|
|
125
161
|
}
|
|
126
162
|
|
|
127
|
-
/// Stamped
|
|
128
|
-
/// the `timeout`
|
|
163
|
+
/// Stamped as the Store opens, so instantiating the guest and writing its
|
|
164
|
+
/// request count as the `timeout` counts them, and post-run host
|
|
165
|
+
/// bookkeeping stays out.
|
|
129
166
|
pub(crate) fn start_wall_clock(&mut self) {
|
|
130
167
|
self.wall_entry = Some(Instant::now());
|
|
131
168
|
}
|
|
@@ -141,6 +178,45 @@ impl Invocation {
|
|
|
141
178
|
}
|
|
142
179
|
|
|
143
180
|
pub(crate) fn memory_peak(&self) -> usize {
|
|
144
|
-
self.limiter.
|
|
181
|
+
self.limiter.budget().peak()
|
|
182
|
+
}
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
#[cfg(test)]
|
|
186
|
+
impl Invocation {
|
|
187
|
+
/// An invocation under `config` whose one network entry holds
|
|
188
|
+
/// `endpoints`.
|
|
189
|
+
pub(crate) fn with_network(
|
|
190
|
+
config: &crate::config::Config,
|
|
191
|
+
endpoints: impl IntoIterator<Item = kobako_runtime::network::Endpoint>,
|
|
192
|
+
) -> Self {
|
|
193
|
+
use kobako_runtime::grant::Grant;
|
|
194
|
+
use kobako_runtime::network::{Builder, Network};
|
|
195
|
+
|
|
196
|
+
let mut entry = Builder::default();
|
|
197
|
+
for endpoint in endpoints {
|
|
198
|
+
entry.endpoint(endpoint);
|
|
199
|
+
}
|
|
200
|
+
let grant = Grant::Network(Network::new("local", Arc::new(entry)).unwrap());
|
|
201
|
+
let wasi = Wasi::for_invocation(config, &[grant]).expect("WASI context must build");
|
|
202
|
+
Self::new(wasi)
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
/// A hermetic invocation whose one network entry holds `endpoints`, or
|
|
206
|
+
/// with no network for `None`, under the hermetic config `adjust` moves.
|
|
207
|
+
pub(crate) fn hermetic(
|
|
208
|
+
endpoints: Option<Vec<kobako_runtime::network::Endpoint>>,
|
|
209
|
+
adjust: impl FnOnce(&mut crate::config::Config),
|
|
210
|
+
) -> Self {
|
|
211
|
+
let mut config =
|
|
212
|
+
crate::config::Config::for_profile(kobako_runtime::profile::Profile::Hermetic);
|
|
213
|
+
adjust(&mut config);
|
|
214
|
+
match endpoints {
|
|
215
|
+
Some(endpoints) => Self::with_network(&config, endpoints),
|
|
216
|
+
None => {
|
|
217
|
+
let wasi = Wasi::for_invocation(&config, &[]).expect("WASI context must build");
|
|
218
|
+
Self::new(wasi)
|
|
219
|
+
}
|
|
220
|
+
}
|
|
145
221
|
}
|
|
146
222
|
}
|
|
@@ -5,8 +5,8 @@
|
|
|
5
5
|
//! every invocation instantiates a fresh instance from a pre-linked
|
|
6
6
|
//! template and discards the whole Store afterwards — the
|
|
7
7
|
//! per-invocation instance discipline. Everything engine-bound
|
|
8
|
-
//! lives behind the contract surface, so a frontend
|
|
9
|
-
//!
|
|
8
|
+
//! lives behind the contract surface, so a frontend sees no wasmtime
|
|
9
|
+
//! type.
|
|
10
10
|
//!
|
|
11
11
|
//! Each module holds one responsibility and opens with its own doc.
|
|
12
12
|
|
|
@@ -14,16 +14,20 @@ mod abi;
|
|
|
14
14
|
mod ambient;
|
|
15
15
|
mod cache;
|
|
16
16
|
mod config;
|
|
17
|
-
mod descriptors;
|
|
18
17
|
mod driver;
|
|
19
18
|
mod exports;
|
|
20
19
|
mod guest;
|
|
20
|
+
#[cfg(test)]
|
|
21
|
+
mod guest_binary;
|
|
21
22
|
mod imports;
|
|
22
23
|
mod instance_pre;
|
|
23
24
|
mod invocation;
|
|
24
25
|
mod limiter;
|
|
25
26
|
mod preview1;
|
|
27
|
+
mod preview2;
|
|
26
28
|
mod trap;
|
|
29
|
+
#[cfg(test)]
|
|
30
|
+
mod waits;
|
|
27
31
|
mod wasi;
|
|
28
32
|
|
|
29
33
|
pub use config::Config;
|
|
@@ -1,29 +1,147 @@
|
|
|
1
|
-
//! Hands wasmtime's growth hook to the runtime's per-invocation memory
|
|
1
|
+
//! Hands wasmtime's growth hook to the runtime's per-invocation memory budget.
|
|
2
2
|
|
|
3
|
-
use
|
|
4
|
-
use wasmtime::ResourceLimiter;
|
|
3
|
+
use std::sync::Arc;
|
|
5
4
|
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
5
|
+
use kobako_runtime::memcg::{GrowthCharge, MemoryBudget, MemoryLimitExceeded};
|
|
6
|
+
use wasmtime::{Module, ResourceLimiter};
|
|
7
|
+
|
|
8
|
+
/// What one wasm page of a memory takes; the Engine enables no custom page
|
|
9
|
+
/// size.
|
|
10
|
+
const PAGE: usize = 64 * 1024;
|
|
11
|
+
|
|
12
|
+
/// What one table entry takes, a pointer in wasmtime.
|
|
13
|
+
const TABLE_ENTRY: usize = size_of::<usize>();
|
|
14
|
+
|
|
15
|
+
/// The bytes the memories and tables `module` defines start with, which
|
|
16
|
+
/// wasmtime reports as their count and the largest initial size, so a module
|
|
17
|
+
/// whose sizes differ is granted the largest for each.
|
|
18
|
+
pub(crate) fn declared(module: &Module) -> usize {
|
|
19
|
+
let required = module.resources_required();
|
|
20
|
+
let memory = to_usize(required.max_initial_memory_size).saturating_mul(PAGE);
|
|
21
|
+
let table = to_usize(required.max_initial_table_size).saturating_mul(TABLE_ENTRY);
|
|
22
|
+
(required.num_memories as usize)
|
|
23
|
+
.saturating_mul(memory)
|
|
24
|
+
.saturating_add((required.num_tables as usize).saturating_mul(table))
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
fn to_usize(size: Option<u64>) -> usize {
|
|
28
|
+
size.map_or(0, |size| usize::try_from(size).unwrap_or(usize::MAX))
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
/// wasmtime asks this as a memory, a table, or the GC heap is created or
|
|
32
|
+
/// grows; a growth past the budget fails and is kept as the refusal that a
|
|
33
|
+
/// run failing after it is attributed to. A growth it allowed may still
|
|
34
|
+
/// fail, past the memory's maximum or for want of host memory, and its
|
|
35
|
+
/// charge is then given back.
|
|
36
|
+
pub(crate) struct MemoryLimiter {
|
|
37
|
+
budget: Arc<MemoryBudget>,
|
|
38
|
+
pending: Option<GrowthCharge>,
|
|
39
|
+
refused: Option<MemoryLimitExceeded>,
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
impl MemoryLimiter {
|
|
43
|
+
pub(crate) fn new(budget: Arc<MemoryBudget>) -> Self {
|
|
44
|
+
Self {
|
|
45
|
+
budget,
|
|
46
|
+
pending: None,
|
|
47
|
+
refused: None,
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
/// The budget it charges.
|
|
52
|
+
pub(crate) fn budget(&self) -> &MemoryBudget {
|
|
53
|
+
&self.budget
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
/// The last growth the budget refused. The guest only sees the growth
|
|
57
|
+
/// fail, so this is what tells a run that then fails apart as the
|
|
58
|
+
/// budget's.
|
|
59
|
+
pub(crate) fn refused(&self) -> Option<&MemoryLimitExceeded> {
|
|
60
|
+
self.refused.as_ref()
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
/// A growth the budget refuses fails, as `memory.grow` answers -1, rather
|
|
64
|
+
/// than ending the run, so the guest can collect what it no longer holds
|
|
65
|
+
/// and try again, as mruby does once its allocator answers none.
|
|
66
|
+
fn charge(&mut self, bytes: usize) -> wasmtime::Result<bool> {
|
|
67
|
+
match self.budget.charge_growth(bytes) {
|
|
68
|
+
Ok(charge) => {
|
|
69
|
+
self.pending = Some(charge);
|
|
70
|
+
Ok(true)
|
|
71
|
+
}
|
|
72
|
+
Err(refusal) => {
|
|
73
|
+
self.refused = Some(refusal);
|
|
74
|
+
Ok(false)
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
fn give_back(&mut self) {
|
|
80
|
+
if let Some(charge) = self.pending.take() {
|
|
81
|
+
self.budget.cancel_growth(charge);
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
}
|
|
9
85
|
|
|
10
86
|
impl ResourceLimiter for MemoryLimiter {
|
|
11
87
|
fn memory_growing(
|
|
12
88
|
&mut self,
|
|
13
|
-
|
|
89
|
+
current: usize,
|
|
14
90
|
desired: usize,
|
|
15
91
|
_maximum: Option<usize>,
|
|
16
92
|
) -> wasmtime::Result<bool> {
|
|
17
|
-
self.
|
|
18
|
-
|
|
93
|
+
self.charge(desired.saturating_sub(current))
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
fn memory_grow_failed(&mut self, _error: wasmtime::Error) -> wasmtime::Result<()> {
|
|
97
|
+
self.give_back();
|
|
98
|
+
Ok(())
|
|
19
99
|
}
|
|
20
100
|
|
|
21
101
|
fn table_growing(
|
|
22
102
|
&mut self,
|
|
23
|
-
|
|
24
|
-
|
|
103
|
+
current: usize,
|
|
104
|
+
desired: usize,
|
|
25
105
|
_maximum: Option<usize>,
|
|
26
106
|
) -> wasmtime::Result<bool> {
|
|
27
|
-
|
|
107
|
+
self.charge(desired.saturating_sub(current).saturating_mul(TABLE_ENTRY))
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
fn table_grow_failed(&mut self, _error: wasmtime::Error) -> wasmtime::Result<()> {
|
|
111
|
+
self.give_back();
|
|
112
|
+
Ok(())
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
#[cfg(test)]
|
|
117
|
+
mod tests {
|
|
118
|
+
use wasmtime::{Memory, MemoryType, Store};
|
|
119
|
+
|
|
120
|
+
use super::*;
|
|
121
|
+
|
|
122
|
+
const PAGE: usize = 64 * 1024;
|
|
123
|
+
|
|
124
|
+
// wasmtime asks the limiter before it checks the memory's own maximum, so
|
|
125
|
+
// only a growth it fails after the budget allowed it shows the charge
|
|
126
|
+
// given back.
|
|
127
|
+
// @behavior S-211
|
|
128
|
+
#[test]
|
|
129
|
+
fn a_growth_the_engine_fails_to_make_costs_the_budget_nothing() {
|
|
130
|
+
let budget = MemoryBudget::new(Some(3 * PAGE));
|
|
131
|
+
budget.arm(PAGE);
|
|
132
|
+
let engine = crate::cache::load_engine().expect("the shared Engine must be built");
|
|
133
|
+
let mut store = Store::new(engine, MemoryLimiter::new(budget.clone()));
|
|
134
|
+
store.limiter(|limiter| limiter);
|
|
135
|
+
let memory = Memory::new(&mut store, MemoryType::new(1, Some(2)))
|
|
136
|
+
.expect("a memory of one page within its maximum must be made");
|
|
137
|
+
let failed = memory.grow(&mut store, 2).is_err();
|
|
138
|
+
let peak = budget.peak();
|
|
139
|
+
let regrown = budget.charge_growth(2 * PAGE).is_ok();
|
|
140
|
+
assert_eq!(
|
|
141
|
+
(failed, peak, regrown),
|
|
142
|
+
(true, 2 * PAGE, true),
|
|
143
|
+
"a growth past a memory's maximum that the budget allowed must fail, give the \
|
|
144
|
+
budget back for the next growth, and stay in the peak reported"
|
|
145
|
+
);
|
|
28
146
|
}
|
|
29
147
|
}
|
|
@@ -0,0 +1,305 @@
|
|
|
1
|
+
//! The guest's descriptor table: what each preview1 file descriptor
|
|
2
|
+
//! stands for within one invocation.
|
|
3
|
+
//!
|
|
4
|
+
//! kobako answers every preview1 call that reads this table, so the guest
|
|
5
|
+
//! sees one table and no call falls through to a second one that
|
|
6
|
+
//! disagrees. Numbering follows Linux's `alloc_fd`: the lowest number the
|
|
7
|
+
//! table does not hold is taken. Each number stands over a handle in
|
|
8
|
+
//! preview2's resource table, which holds the file and counts it, as
|
|
9
|
+
//! wasmtime-wasi's preview1 numbers apart from the `ResourceTable` it shares
|
|
10
|
+
//! with preview2. The standard streams stand over preview2 streams, as
|
|
11
|
+
//! `wasi:cli` hands them out, each over its end of a pipe and lent to its
|
|
12
|
+
//! descriptor, so the guest drops none while the descriptor stands.
|
|
13
|
+
|
|
14
|
+
use std::collections::BTreeMap;
|
|
15
|
+
use std::ops::{Deref, DerefMut};
|
|
16
|
+
use std::sync::{Arc, MutexGuard};
|
|
17
|
+
|
|
18
|
+
use kobako_runtime::cred::Cred;
|
|
19
|
+
use kobako_runtime::vfs::File;
|
|
20
|
+
use kobako_runtime::Errno;
|
|
21
|
+
|
|
22
|
+
pub(crate) use crate::preview2::bindings::wasi_filesystem_types_0_2_12::Descriptor as FileHandle;
|
|
23
|
+
use crate::preview2::bindings::wasi_io_streams_0_2_12::{
|
|
24
|
+
InputStream as InputHandle, OutputStream as OutputHandle,
|
|
25
|
+
};
|
|
26
|
+
use crate::preview2::io::{InputStream, OutputStream, Stream};
|
|
27
|
+
use crate::preview2::table::Handle;
|
|
28
|
+
use crate::preview2::Table;
|
|
29
|
+
|
|
30
|
+
/// The preview2 resource one descriptor stands over, by its handle.
|
|
31
|
+
#[derive(Clone, Copy)]
|
|
32
|
+
pub(crate) enum Resource {
|
|
33
|
+
File(FileHandle),
|
|
34
|
+
/// stdin, read as `wasi:cli`'s `get-stdin` hands it.
|
|
35
|
+
Input(InputHandle),
|
|
36
|
+
/// stdout or stderr, written as `wasi:cli`'s `get-stdout` hands it.
|
|
37
|
+
Output(OutputHandle),
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
impl Resource {
|
|
41
|
+
/// The stream a standard stream's descriptor stands over, if it is one.
|
|
42
|
+
fn stream(self, table: &Table) -> Option<&Stream> {
|
|
43
|
+
match self {
|
|
44
|
+
Resource::File(_) => None,
|
|
45
|
+
Resource::Input(handle) => table.get(handle).ok().map(|input| &input.stream),
|
|
46
|
+
Resource::Output(handle) => table.get(handle).ok().map(|output| &output.stream),
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
/// What one descriptor stands for, as Linux's fd table holds a `struct file`.
|
|
52
|
+
pub(crate) struct Descriptor {
|
|
53
|
+
pub(crate) resource: Resource,
|
|
54
|
+
/// The guest path a preopen names; the guest's root has one, `/`.
|
|
55
|
+
pub(crate) preopen_path: Option<String>,
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
impl Descriptor {
|
|
59
|
+
/// The descriptor of the file under `file`, naming no preopen.
|
|
60
|
+
pub(crate) fn for_file(file: FileHandle) -> Self {
|
|
61
|
+
Self {
|
|
62
|
+
resource: Resource::File(file),
|
|
63
|
+
preopen_path: None,
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
fn for_stream(resource: Resource) -> Self {
|
|
68
|
+
Self {
|
|
69
|
+
resource,
|
|
70
|
+
preopen_path: None,
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
pub(crate) struct Descriptors {
|
|
76
|
+
used: BTreeMap<u32, Descriptor>,
|
|
77
|
+
/// No number below this is free, as Linux's `next_fd` hints `alloc_fd`.
|
|
78
|
+
next_fd: u32,
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
impl Descriptors {
|
|
82
|
+
/// The table every invocation starts from: stdin, then the two
|
|
83
|
+
/// Channels, then the guest's root as the one preopen `/`, at descriptor
|
|
84
|
+
/// 3, where `guest` holds one. Each counts against the open files in
|
|
85
|
+
/// `table`, as a process's `RLIMIT_NOFILE` counts every one it holds. The
|
|
86
|
+
/// root opens as `opener`, the Cred the Driver prepared, the topmost mount
|
|
87
|
+
/// stacked on it for `guest`, as Linux's `topmost_overmount` answers.
|
|
88
|
+
pub(crate) fn new(
|
|
89
|
+
stdin: File,
|
|
90
|
+
stdout: File,
|
|
91
|
+
stderr: File,
|
|
92
|
+
table: &mut Table,
|
|
93
|
+
(opener, guest): (&Arc<Cred>, &Arc<Cred>),
|
|
94
|
+
) -> Result<Self, Errno> {
|
|
95
|
+
let mut used = BTreeMap::new();
|
|
96
|
+
let input = InputStream::new(Stream::from(stdin));
|
|
97
|
+
let input = lend_inherited(table, input)?;
|
|
98
|
+
used.insert(0, Descriptor::for_stream(Resource::Input(input)));
|
|
99
|
+
for (fd, file) in [(1, stdout), (2, stderr)] {
|
|
100
|
+
let output = lend_inherited(table, OutputStream::new(Stream::from(file)))?;
|
|
101
|
+
used.insert(fd, Descriptor::for_stream(Resource::Output(output)));
|
|
102
|
+
}
|
|
103
|
+
if let Some(root) = guest.root() {
|
|
104
|
+
let root = File::root(&guest.topmost_overmount(&root), opener)?;
|
|
105
|
+
used.insert(
|
|
106
|
+
3,
|
|
107
|
+
Descriptor {
|
|
108
|
+
resource: Resource::File(install_inherited(table, root)?),
|
|
109
|
+
preopen_path: Some("/".to_string()),
|
|
110
|
+
},
|
|
111
|
+
);
|
|
112
|
+
}
|
|
113
|
+
Ok(Self {
|
|
114
|
+
next_fd: used.len() as u32,
|
|
115
|
+
used,
|
|
116
|
+
})
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
pub(crate) fn get(&self, fd: u32) -> Option<&Descriptor> {
|
|
120
|
+
self.used.get(&fd)
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
/// Take `fd` out of the table, freeing its number for reuse.
|
|
124
|
+
pub(crate) fn remove(&mut self, fd: u32) -> Option<Descriptor> {
|
|
125
|
+
let descriptor = self.used.remove(&fd)?;
|
|
126
|
+
self.next_fd = self.next_fd.min(fd);
|
|
127
|
+
Some(descriptor)
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
/// Place `descriptor` at `fd`, a number the caller has just freed or
|
|
131
|
+
/// `lowest_unused` found, as Linux's `fd_install` does.
|
|
132
|
+
pub(crate) fn place(&mut self, fd: u32, descriptor: Descriptor) {
|
|
133
|
+
self.used.insert(fd, descriptor);
|
|
134
|
+
if fd == self.next_fd {
|
|
135
|
+
self.next_fd = self.lowest_unused().unwrap_or(u32::MAX);
|
|
136
|
+
}
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
/// The number the next descriptor takes, the lowest one the table does
|
|
140
|
+
/// not hold, as Linux's `get_unused_fd_flags` finds it.
|
|
141
|
+
pub(crate) fn lowest_unused(&self) -> Option<u32> {
|
|
142
|
+
(self.next_fd..=u32::MAX).find(|fd| !self.used.contains_key(fd))
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
/// The file `fd` stands for, one that stands only for its path included:
|
|
146
|
+
/// the one `table` holds, or the pipe's end a standard stream stands
|
|
147
|
+
/// over, which answers each call as Linux's pipe does.
|
|
148
|
+
pub(crate) fn file<'a>(&self, fd: u32, table: &'a Table) -> Option<FileRef<'a>> {
|
|
149
|
+
let resource = self.get(fd)?.resource;
|
|
150
|
+
Some(match resource {
|
|
151
|
+
Resource::File(handle) => FileRef::Table(table.get(handle).ok()?),
|
|
152
|
+
stream => FileRef::Stream(stream.stream(table)?.lock()),
|
|
153
|
+
})
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
/// The file `fd` stands for, to change.
|
|
157
|
+
pub(crate) fn file_mut<'a>(&self, fd: u32, table: &'a mut Table) -> Option<FileRefMut<'a>> {
|
|
158
|
+
let resource = self.get(fd)?.resource;
|
|
159
|
+
Some(match resource {
|
|
160
|
+
Resource::File(handle) => FileRefMut::Table(table.get_mut(handle).ok()?),
|
|
161
|
+
stream => FileRefMut::Stream(stream.stream(table)?.lock()),
|
|
162
|
+
})
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
/// Take `fd` out of the table, and what it stood over out of `table`:
|
|
166
|
+
/// the file to close, or none for a standard stream, whose lend ends
|
|
167
|
+
/// while preview2's handle keeps the stream.
|
|
168
|
+
pub(crate) fn take(&mut self, fd: u32, table: &mut Table) -> Option<Option<File>> {
|
|
169
|
+
Some(match self.remove(fd)?.resource {
|
|
170
|
+
Resource::File(handle) => table.delete(handle).ok(),
|
|
171
|
+
Resource::Input(handle) => {
|
|
172
|
+
table.end_lend(handle);
|
|
173
|
+
None
|
|
174
|
+
}
|
|
175
|
+
Resource::Output(handle) => {
|
|
176
|
+
table.end_lend(handle);
|
|
177
|
+
None
|
|
178
|
+
}
|
|
179
|
+
})
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
/// Open `file` under the lowest free descriptor; `None` when the
|
|
183
|
+
/// Invocation holds as many files as it may.
|
|
184
|
+
#[cfg(test)]
|
|
185
|
+
pub(crate) fn push_file(&mut self, file: File, table: &mut Table) -> Option<u32> {
|
|
186
|
+
let room = table.reserve_file().ok()?;
|
|
187
|
+
let fd = self.lowest_unused()?;
|
|
188
|
+
let handle = table.install(room, file);
|
|
189
|
+
self.place(fd, Descriptor::for_file(handle));
|
|
190
|
+
Some(fd)
|
|
191
|
+
}
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
/// The handle `resource`, opened before the guest runs, takes in `table`;
|
|
195
|
+
/// refused past the open file limit.
|
|
196
|
+
fn install_inherited<H: Handle>(table: &mut Table, resource: H::Host) -> Result<H, Errno> {
|
|
197
|
+
let room = table.reserve_file()?;
|
|
198
|
+
Ok(table.install(room, resource))
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
/// The handle a standard stream takes in `table`, lent to the descriptor
|
|
202
|
+
/// standing over it.
|
|
203
|
+
fn lend_inherited<H: Handle>(table: &mut Table, stream: H::Host) -> Result<H, Errno> {
|
|
204
|
+
let handle = install_inherited(table, stream)?;
|
|
205
|
+
table.lend(handle);
|
|
206
|
+
Ok(handle)
|
|
207
|
+
}
|
|
208
|
+
|
|
209
|
+
/// A file a descriptor stands for, as `Descriptors::file` finds it.
|
|
210
|
+
pub(crate) enum FileRef<'a> {
|
|
211
|
+
Table(&'a File),
|
|
212
|
+
/// The pipe's end behind a standard stream, which its stream shares.
|
|
213
|
+
Stream(MutexGuard<'a, File>),
|
|
214
|
+
}
|
|
215
|
+
|
|
216
|
+
impl Deref for FileRef<'_> {
|
|
217
|
+
type Target = File;
|
|
218
|
+
|
|
219
|
+
fn deref(&self) -> &File {
|
|
220
|
+
match self {
|
|
221
|
+
FileRef::Table(file) => file,
|
|
222
|
+
FileRef::Stream(file) => file,
|
|
223
|
+
}
|
|
224
|
+
}
|
|
225
|
+
}
|
|
226
|
+
|
|
227
|
+
/// A file a descriptor stands for, to change, as `Descriptors::file_mut`
|
|
228
|
+
/// finds it.
|
|
229
|
+
pub(crate) enum FileRefMut<'a> {
|
|
230
|
+
Table(&'a mut File),
|
|
231
|
+
Stream(MutexGuard<'a, File>),
|
|
232
|
+
}
|
|
233
|
+
|
|
234
|
+
impl Deref for FileRefMut<'_> {
|
|
235
|
+
type Target = File;
|
|
236
|
+
|
|
237
|
+
fn deref(&self) -> &File {
|
|
238
|
+
match self {
|
|
239
|
+
FileRefMut::Table(file) => file,
|
|
240
|
+
FileRefMut::Stream(file) => file,
|
|
241
|
+
}
|
|
242
|
+
}
|
|
243
|
+
}
|
|
244
|
+
|
|
245
|
+
impl DerefMut for FileRefMut<'_> {
|
|
246
|
+
fn deref_mut(&mut self) -> &mut File {
|
|
247
|
+
match self {
|
|
248
|
+
FileRefMut::Table(file) => file,
|
|
249
|
+
FileRefMut::Stream(file) => file,
|
|
250
|
+
}
|
|
251
|
+
}
|
|
252
|
+
}
|
|
253
|
+
|
|
254
|
+
#[cfg(test)]
|
|
255
|
+
mod tests {
|
|
256
|
+
use super::*;
|
|
257
|
+
use kobako_runtime::filesystem::{create_pipe_files, PipeInodeInfo};
|
|
258
|
+
use kobako_runtime::security::prepare_creds;
|
|
259
|
+
use kobako_runtime::vfs::OpenFiles;
|
|
260
|
+
|
|
261
|
+
fn pipe_end() -> File {
|
|
262
|
+
create_pipe_files(&PipeInodeInfo::new(1), &prepare_creds(&[], None)).1
|
|
263
|
+
}
|
|
264
|
+
|
|
265
|
+
// @behavior RT-189
|
|
266
|
+
#[test]
|
|
267
|
+
fn a_new_descriptor_takes_the_lowest_closed_number() {
|
|
268
|
+
let mut table = Table::new(OpenFiles::new(None));
|
|
269
|
+
let mut descriptors = Descriptors::new(
|
|
270
|
+
pipe_end(),
|
|
271
|
+
pipe_end(),
|
|
272
|
+
pipe_end(),
|
|
273
|
+
&mut table,
|
|
274
|
+
(&prepare_creds(&[], None), &prepare_creds(&[], None)),
|
|
275
|
+
)
|
|
276
|
+
.unwrap_or_else(|_| panic!("a table holding no Mount must make"));
|
|
277
|
+
let mut open = |descriptors: &mut Descriptors| {
|
|
278
|
+
let fd = descriptors
|
|
279
|
+
.lowest_unused()
|
|
280
|
+
.expect("a table with no limit numbers every descriptor");
|
|
281
|
+
let file =
|
|
282
|
+
install_inherited(&mut table, pipe_end()).expect("a table with no limit takes it");
|
|
283
|
+
descriptors.place(fd, Descriptor::for_file(file));
|
|
284
|
+
fd
|
|
285
|
+
};
|
|
286
|
+
let numbers = [
|
|
287
|
+
open(&mut descriptors),
|
|
288
|
+
open(&mut descriptors),
|
|
289
|
+
open(&mut descriptors),
|
|
290
|
+
];
|
|
291
|
+
assert_eq!(numbers, [3, 4, 5], "descriptors must number from 3");
|
|
292
|
+
descriptors.remove(3);
|
|
293
|
+
descriptors.remove(4);
|
|
294
|
+
assert_eq!(
|
|
295
|
+
(
|
|
296
|
+
open(&mut descriptors),
|
|
297
|
+
open(&mut descriptors),
|
|
298
|
+
open(&mut descriptors),
|
|
299
|
+
),
|
|
300
|
+
(3, 4, 6),
|
|
301
|
+
"path_open's descriptors must take the lowest closed number first, as Linux's alloc_fd \
|
|
302
|
+
does, and then the one past the highest"
|
|
303
|
+
);
|
|
304
|
+
}
|
|
305
|
+
}
|