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
|
@@ -0,0 +1,381 @@
|
|
|
1
|
+
//! Waiting for what another thread brings, as Linux's wait queues and
|
|
2
|
+
//! `do_poll` are: a producer wakes the queue its event stands on, and a
|
|
3
|
+
//! poller that registered on that queue looks again. A wait ends once its run
|
|
4
|
+
//! is to stop, as `do_poll`'s interruptible sleep ends on a pending signal; the
|
|
5
|
+
//! only signal a run receives is the one that stops it. It waits through the
|
|
6
|
+
//! registered Blocking, as a lock's slow path does.
|
|
7
|
+
|
|
8
|
+
use std::sync::{Arc, Condvar, Mutex, MutexGuard, PoisonError};
|
|
9
|
+
use std::task::Waker;
|
|
10
|
+
use std::time::Instant;
|
|
11
|
+
|
|
12
|
+
use crate::sched;
|
|
13
|
+
use crate::Errno;
|
|
14
|
+
|
|
15
|
+
/// Where the waiters for one event stand, as Linux's `wait_queue_head_t`.
|
|
16
|
+
#[derive(Default)]
|
|
17
|
+
pub struct WaitQueueHead {
|
|
18
|
+
waiters: Mutex<Vec<Arc<Waiter>>>,
|
|
19
|
+
/// The tasks waiting here, each woken once and then gone, as an entry
|
|
20
|
+
/// `init_waitqueue_func_entry` makes with `autoremove_wake_function`.
|
|
21
|
+
tasks: Mutex<Vec<Waker>>,
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
impl WaitQueueHead {
|
|
25
|
+
/// Wake every waiter standing here, as `wake_up` does once the event
|
|
26
|
+
/// arrived.
|
|
27
|
+
pub fn wake_up(&self) {
|
|
28
|
+
for waiter in self.waiters().iter() {
|
|
29
|
+
waiter.wake();
|
|
30
|
+
}
|
|
31
|
+
let tasks = std::mem::take(&mut *self.tasks.lock().unwrap_or_else(PoisonError::into_inner));
|
|
32
|
+
for task in tasks {
|
|
33
|
+
task.wake();
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
/// Have the task `waker` stands for woken by the next wake here, once, as
|
|
38
|
+
/// `add_wait_queue` adds an entry whose wake function is the task's; a
|
|
39
|
+
/// task already waiting is not added twice.
|
|
40
|
+
pub(crate) fn add(&self, waker: &Waker) {
|
|
41
|
+
let mut tasks = self.tasks.lock().unwrap_or_else(PoisonError::into_inner);
|
|
42
|
+
if !tasks.iter().any(|task| task.will_wake(waker)) {
|
|
43
|
+
tasks.push(waker.clone());
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
fn waiters(&self) -> MutexGuard<'_, Vec<Arc<Waiter>>> {
|
|
48
|
+
self.waiters.lock().unwrap_or_else(PoisonError::into_inner)
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
/// One sleeping poller, as a `poll_wqueues` entry: a wake that arrives
|
|
53
|
+
/// before it sleeps is kept, so none is lost.
|
|
54
|
+
#[derive(Default)]
|
|
55
|
+
pub(crate) struct Waiter {
|
|
56
|
+
woken: Mutex<bool>,
|
|
57
|
+
wake: Condvar,
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
impl Waiter {
|
|
61
|
+
pub(crate) fn wake(&self) {
|
|
62
|
+
*self.woken() = true;
|
|
63
|
+
self.wake.notify_all();
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
/// Sleep until woken or `until` passes.
|
|
67
|
+
fn sleep_until(&self, until: Option<Instant>) {
|
|
68
|
+
let mut woken = self.woken();
|
|
69
|
+
while !*woken {
|
|
70
|
+
woken = match until {
|
|
71
|
+
Some(at) => {
|
|
72
|
+
let left = at.saturating_duration_since(Instant::now());
|
|
73
|
+
if left.is_zero() {
|
|
74
|
+
break;
|
|
75
|
+
}
|
|
76
|
+
self.wake
|
|
77
|
+
.wait_timeout(woken, left)
|
|
78
|
+
.unwrap_or_else(PoisonError::into_inner)
|
|
79
|
+
.0
|
|
80
|
+
}
|
|
81
|
+
None => self
|
|
82
|
+
.wake
|
|
83
|
+
.wait(woken)
|
|
84
|
+
.unwrap_or_else(PoisonError::into_inner),
|
|
85
|
+
};
|
|
86
|
+
}
|
|
87
|
+
*woken = false;
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
fn woken(&self) -> MutexGuard<'_, bool> {
|
|
91
|
+
self.woken.lock().unwrap_or_else(PoisonError::into_inner)
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
/// The queues one poll registered on, as Linux's `poll_table`; dropping it
|
|
96
|
+
/// leaves each, as `poll_freewait` does.
|
|
97
|
+
pub struct PollTable {
|
|
98
|
+
waiter: Arc<Waiter>,
|
|
99
|
+
queues: Vec<Arc<WaitQueueHead>>,
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
impl Drop for PollTable {
|
|
103
|
+
fn drop(&mut self) {
|
|
104
|
+
for queue in &self.queues {
|
|
105
|
+
queue
|
|
106
|
+
.waiters()
|
|
107
|
+
.retain(|waiter| !Arc::ptr_eq(waiter, &self.waiter));
|
|
108
|
+
}
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
/// Have the poll `table` stands for woken by `queue`, as a file's `->poll`
|
|
113
|
+
/// calls `poll_wait`; a poll that will not sleep passes no table. A table
|
|
114
|
+
/// stands on a queue once, however many of its sources name it, where Linux
|
|
115
|
+
/// adds an entry for each, so whoever wakes the queue wakes the poll once.
|
|
116
|
+
pub fn poll_wait(queue: &Arc<WaitQueueHead>, table: Option<&mut PollTable>) {
|
|
117
|
+
if let Some(table) = table {
|
|
118
|
+
if table.queues.iter().any(|stood| Arc::ptr_eq(stood, queue)) {
|
|
119
|
+
return;
|
|
120
|
+
}
|
|
121
|
+
queue.waiters().push(table.waiter.clone());
|
|
122
|
+
table.queues.push(queue.clone());
|
|
123
|
+
}
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
/// Ask `poll` until it finds something ready, `end` passes, or the run is to
|
|
127
|
+
/// stop, as Linux's `do_poll` asks each file again whenever a queue it
|
|
128
|
+
/// registered on wakes. `end` is read afresh each turn, so a wait whose
|
|
129
|
+
/// soonest due moves earlier, as an exchange's next timeout starting once the
|
|
130
|
+
/// last ends, sleeps only to the new one. The first ask registers through the
|
|
131
|
+
/// table it is handed; nothing ready by `end` answers `None`, and a run that is
|
|
132
|
+
/// to stop answers EINTR.
|
|
133
|
+
pub fn do_poll<T>(
|
|
134
|
+
end: impl Fn() -> Option<Instant>,
|
|
135
|
+
mut poll: impl FnMut(Option<&mut PollTable>) -> Option<T>,
|
|
136
|
+
) -> Result<Option<T>, Errno> {
|
|
137
|
+
let mut table = PollTable {
|
|
138
|
+
waiter: Arc::default(),
|
|
139
|
+
queues: Vec::new(),
|
|
140
|
+
};
|
|
141
|
+
if let Some(ready) = poll(Some(&mut table)) {
|
|
142
|
+
return Ok(Some(ready));
|
|
143
|
+
}
|
|
144
|
+
let mut answer = Ok(None);
|
|
145
|
+
sched::sleep_on(&table.waiter, || {
|
|
146
|
+
sched::schedule(&mut || loop {
|
|
147
|
+
sched::handle_signal();
|
|
148
|
+
if sched::fatal_signal_pending() {
|
|
149
|
+
answer = Err(Errno::Interrupted);
|
|
150
|
+
return;
|
|
151
|
+
}
|
|
152
|
+
let end = end();
|
|
153
|
+
if end.is_some_and(|end| Instant::now() >= end) {
|
|
154
|
+
return;
|
|
155
|
+
}
|
|
156
|
+
let until = match (end, sched::deadline()) {
|
|
157
|
+
(Some(end), Some(deadline)) => Some(end.min(deadline)),
|
|
158
|
+
(end, deadline) => end.or(deadline),
|
|
159
|
+
};
|
|
160
|
+
table.waiter.sleep_until(until);
|
|
161
|
+
if let Some(ready) = poll(None) {
|
|
162
|
+
answer = Ok(Some(ready));
|
|
163
|
+
return;
|
|
164
|
+
}
|
|
165
|
+
})
|
|
166
|
+
});
|
|
167
|
+
answer
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
#[cfg(test)]
|
|
171
|
+
mod tests {
|
|
172
|
+
use std::sync::atomic::{AtomicBool, Ordering};
|
|
173
|
+
use std::thread;
|
|
174
|
+
use std::time::Duration;
|
|
175
|
+
|
|
176
|
+
use super::*;
|
|
177
|
+
use crate::sched::testing::{count_waits, join, wait_until, within_max_wait, MAX_WAIT};
|
|
178
|
+
use crate::sched::{run_task, Task};
|
|
179
|
+
|
|
180
|
+
/// An end `after` from now, the same each time it is read.
|
|
181
|
+
fn end_after(after: Duration) -> impl Fn() -> Option<Instant> {
|
|
182
|
+
let end = Instant::now() + after;
|
|
183
|
+
move || Some(end)
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
// @behavior RT-335
|
|
187
|
+
#[test]
|
|
188
|
+
fn a_poll_looks_again_once_the_queue_it_registered_on_wakes() {
|
|
189
|
+
let queue = Arc::new(WaitQueueHead::default());
|
|
190
|
+
let ready = Arc::new(AtomicBool::new(false));
|
|
191
|
+
let waits = count_waits();
|
|
192
|
+
let producer = {
|
|
193
|
+
let (queue, ready, waits) = (queue.clone(), ready.clone(), waits.clone());
|
|
194
|
+
thread::spawn(move || {
|
|
195
|
+
// The poller has asked once and waits through the `Blocking`, so
|
|
196
|
+
// only the wake makes it look again before its run's deadline.
|
|
197
|
+
wait_until("the poll waiting on its queue", || {
|
|
198
|
+
!queue.waiters().is_empty() && waits.load(Ordering::SeqCst) > 0
|
|
199
|
+
});
|
|
200
|
+
ready.store(true, Ordering::SeqCst);
|
|
201
|
+
queue.wake_up();
|
|
202
|
+
})
|
|
203
|
+
};
|
|
204
|
+
let answer = within_max_wait(|| {
|
|
205
|
+
do_poll(
|
|
206
|
+
|| None,
|
|
207
|
+
|table| {
|
|
208
|
+
poll_wait(&queue, table);
|
|
209
|
+
// What the poll finds once its run's deadline passed is
|
|
210
|
+
// what the deadline woke it to, not the wake.
|
|
211
|
+
let woken = !crate::sched::fatal_signal_pending();
|
|
212
|
+
(woken && ready.load(Ordering::SeqCst)).then_some("ready")
|
|
213
|
+
},
|
|
214
|
+
)
|
|
215
|
+
});
|
|
216
|
+
join("the producer", producer);
|
|
217
|
+
assert!(
|
|
218
|
+
answer == Ok(Some("ready")) && queue.waiters().is_empty(),
|
|
219
|
+
"do_poll with no timeout over a queue another thread wakes once its event arrives \
|
|
220
|
+
must answer what the poll then finds before its run's deadline ends it with \
|
|
221
|
+
EINTR, and leave the queue, got {answer:?}"
|
|
222
|
+
);
|
|
223
|
+
}
|
|
224
|
+
|
|
225
|
+
// A guest may list one source many times over, each through a handle of
|
|
226
|
+
// its own, and the reactor wakes every waiter a queue holds on each
|
|
227
|
+
// event, so the wait must stand there once.
|
|
228
|
+
// @behavior RT-427
|
|
229
|
+
#[test]
|
|
230
|
+
fn a_poll_naming_one_queue_many_times_stands_on_it_once() {
|
|
231
|
+
let queue = Arc::new(WaitQueueHead::default());
|
|
232
|
+
let mut standing = None;
|
|
233
|
+
let answer = do_poll(end_after(Duration::from_millis(20)), |table| {
|
|
234
|
+
match table {
|
|
235
|
+
Some(table) => (0..2000).for_each(|_| poll_wait(&queue, Some(&mut *table))),
|
|
236
|
+
None => standing = Some(queue.waiters().len()),
|
|
237
|
+
}
|
|
238
|
+
None::<()>
|
|
239
|
+
});
|
|
240
|
+
assert_eq!(
|
|
241
|
+
(answer, standing, queue.waiters().len()),
|
|
242
|
+
(Ok(None), Some(1), 0),
|
|
243
|
+
"a poll whose 2000 sources all stand on one queue must stand on it once while it \
|
|
244
|
+
sleeps, and leave it as the poll ends"
|
|
245
|
+
);
|
|
246
|
+
}
|
|
247
|
+
|
|
248
|
+
/// A task's wake, counted.
|
|
249
|
+
#[derive(Default)]
|
|
250
|
+
struct CountsWakes(std::sync::atomic::AtomicUsize);
|
|
251
|
+
|
|
252
|
+
impl std::task::Wake for CountsWakes {
|
|
253
|
+
fn wake(self: Arc<Self>) {
|
|
254
|
+
self.0.fetch_add(1, Ordering::SeqCst);
|
|
255
|
+
}
|
|
256
|
+
}
|
|
257
|
+
|
|
258
|
+
#[test]
|
|
259
|
+
fn a_task_waiting_on_a_queue_is_woken_once_by_its_next_wake() {
|
|
260
|
+
let queue = WaitQueueHead::default();
|
|
261
|
+
let wakes = Arc::new(CountsWakes::default());
|
|
262
|
+
let waker = Waker::from(wakes.clone());
|
|
263
|
+
queue.add(&waker);
|
|
264
|
+
queue.add(&waker);
|
|
265
|
+
queue.wake_up();
|
|
266
|
+
queue.wake_up();
|
|
267
|
+
assert_eq!(
|
|
268
|
+
wakes.0.load(Ordering::SeqCst),
|
|
269
|
+
1,
|
|
270
|
+
"a task added to a wait queue twice must be woken once by the next wake_up and not \
|
|
271
|
+
by a later one, as an autoremoving wait entry leaves once it wakes"
|
|
272
|
+
);
|
|
273
|
+
}
|
|
274
|
+
|
|
275
|
+
// @behavior RT-335
|
|
276
|
+
#[test]
|
|
277
|
+
fn a_poll_finding_nothing_answers_none_once_its_timeout_passes() {
|
|
278
|
+
let started = Instant::now();
|
|
279
|
+
let answer = do_poll(end_after(Duration::from_millis(20)), |_| None::<()>);
|
|
280
|
+
assert!(
|
|
281
|
+
answer == Ok(None) && started.elapsed() >= Duration::from_millis(20),
|
|
282
|
+
"do_poll with a 20 ms timeout over nothing ready must answer None once it passes, \
|
|
283
|
+
got {answer:?} after {:?}",
|
|
284
|
+
started.elapsed()
|
|
285
|
+
);
|
|
286
|
+
}
|
|
287
|
+
|
|
288
|
+
// @behavior RT-395
|
|
289
|
+
#[test]
|
|
290
|
+
fn a_poll_sleeps_only_to_its_end_once_a_wake_moves_it_earlier() {
|
|
291
|
+
let queue = Arc::new(WaitQueueHead::default());
|
|
292
|
+
let moved = Arc::new(Mutex::new(None::<Instant>));
|
|
293
|
+
let far = Instant::now() + Duration::from_secs(30);
|
|
294
|
+
let waits = count_waits();
|
|
295
|
+
let waker = {
|
|
296
|
+
let (queue, moved, waits) = (queue.clone(), moved.clone(), waits.clone());
|
|
297
|
+
thread::spawn(move || {
|
|
298
|
+
wait_until("the poll waiting on its queue", || {
|
|
299
|
+
!queue.waiters().is_empty() && waits.load(Ordering::SeqCst) > 0
|
|
300
|
+
});
|
|
301
|
+
*moved.lock().unwrap() = Some(Instant::now() + Duration::from_millis(20));
|
|
302
|
+
queue.wake_up();
|
|
303
|
+
})
|
|
304
|
+
};
|
|
305
|
+
let answer = within_max_wait(|| {
|
|
306
|
+
do_poll(
|
|
307
|
+
|| Some(moved.lock().unwrap().unwrap_or(far)),
|
|
308
|
+
|table| {
|
|
309
|
+
poll_wait(&queue, table);
|
|
310
|
+
None::<()>
|
|
311
|
+
},
|
|
312
|
+
)
|
|
313
|
+
});
|
|
314
|
+
join("the waker", waker);
|
|
315
|
+
assert_eq!(
|
|
316
|
+
answer,
|
|
317
|
+
Ok(None),
|
|
318
|
+
"do_poll over nothing ready, its 30 s end moved to 20 ms after a wake, must answer \
|
|
319
|
+
None once the moved end passes, before its run's deadline ends it with EINTR"
|
|
320
|
+
);
|
|
321
|
+
}
|
|
322
|
+
|
|
323
|
+
// @behavior RT-267
|
|
324
|
+
#[test]
|
|
325
|
+
fn a_poll_ends_with_eintr_once_its_thread_is_interrupted() {
|
|
326
|
+
let polling = Arc::new(Mutex::new(None));
|
|
327
|
+
let poller = {
|
|
328
|
+
let polling = polling.clone();
|
|
329
|
+
thread::spawn(move || {
|
|
330
|
+
// Past the longest a test waits, so only the interrupt ends
|
|
331
|
+
// the poll before the test gives up on it.
|
|
332
|
+
run_task(Some(Instant::now() + MAX_WAIT * 3), || {
|
|
333
|
+
*polling.lock().unwrap() = Some((Task::current(), count_waits()));
|
|
334
|
+
do_poll(|| None, |_| None::<()>)
|
|
335
|
+
})
|
|
336
|
+
})
|
|
337
|
+
};
|
|
338
|
+
wait_until("the poll going to sleep", || {
|
|
339
|
+
polling
|
|
340
|
+
.lock()
|
|
341
|
+
.unwrap()
|
|
342
|
+
.as_ref()
|
|
343
|
+
.is_some_and(|(_, waits)| waits.load(Ordering::SeqCst) == 1)
|
|
344
|
+
});
|
|
345
|
+
polling.lock().unwrap().as_ref().unwrap().0.interrupt();
|
|
346
|
+
let answer = join("the interrupted poll ending", poller);
|
|
347
|
+
assert_eq!(
|
|
348
|
+
answer,
|
|
349
|
+
Err(Errno::Interrupted),
|
|
350
|
+
"a poll with no timeout, its thread interrupted as it sleeps, must answer EINTR \
|
|
351
|
+
before its deadline"
|
|
352
|
+
);
|
|
353
|
+
}
|
|
354
|
+
|
|
355
|
+
// @behavior RT-267
|
|
356
|
+
#[test]
|
|
357
|
+
fn a_poll_ends_with_eintr_once_its_deadline_passes() {
|
|
358
|
+
let answer = run_task(Some(Instant::now() + Duration::from_millis(20)), || {
|
|
359
|
+
do_poll(|| None, |_| None::<()>)
|
|
360
|
+
});
|
|
361
|
+
assert_eq!(
|
|
362
|
+
answer,
|
|
363
|
+
Err(Errno::Interrupted),
|
|
364
|
+
"do_poll with no timeout, past its run's deadline, must answer EINTR"
|
|
365
|
+
);
|
|
366
|
+
}
|
|
367
|
+
|
|
368
|
+
// @behavior RT-336
|
|
369
|
+
#[test]
|
|
370
|
+
fn a_poll_that_sleeps_waits_through_the_registered_blocking() {
|
|
371
|
+
let waits = count_waits();
|
|
372
|
+
let _ = do_poll(end_after(Duration::from_millis(1)), |_| None::<()>);
|
|
373
|
+
let _ = do_poll(end_after(Duration::from_secs(1)), |_| Some(()));
|
|
374
|
+
assert_eq!(
|
|
375
|
+
waits.load(Ordering::SeqCst),
|
|
376
|
+
1,
|
|
377
|
+
"do_poll must sleep through the registered Blocking only when its first ask finds \
|
|
378
|
+
nothing ready"
|
|
379
|
+
);
|
|
380
|
+
}
|
|
381
|
+
}
|
|
@@ -1,5 +1,43 @@
|
|
|
1
1
|
# Changelog
|
|
2
2
|
|
|
3
|
+
## [0.22.0](https://github.com/elct9620/kobako/compare/kobako-transport-v0.21.0...kobako-transport-v0.22.0) (2026-11-27)
|
|
4
|
+
|
|
5
|
+
|
|
6
|
+
### Miscellaneous Chores
|
|
7
|
+
|
|
8
|
+
* **kobako-transport:** Synchronize kobako crates versions
|
|
9
|
+
|
|
10
|
+
## [0.21.0](https://github.com/elct9620/kobako/compare/kobako-transport-v0.20.0...kobako-transport-v0.21.0) (2026-11-18)
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
### ⚠ BREAKING CHANGES
|
|
14
|
+
|
|
15
|
+
* **transport:** kobako_transport::envelope::Reply is CallReply.
|
|
16
|
+
* **transport:** call Driver::from_file in place of Driver::new;
|
|
17
|
+
* **transport:** a Guest Binary built against ABI 5 is refused at construction. Rebuild it against kobako-core, whose export_guest! emits the new names. kobako-transport drops ABI_VERSION as a u32, FRAME_LEN_SIZE, MAX_FRAME_LEN, pack_ptr_len, and unpack_ptr_len, and names the ABI through ABI_VERSION ("0.6.0"), GUEST_INTERFACE, HOST_INTERFACE, DOWNCALL, UPCALL, and PREAMBLE. kobako-core drops alloc; answer returns the return area's address.
|
|
18
|
+
* **transport:** kobako_transport::abi::MAX_DISPATCH_PAYLOAD is renamed to MAX_MESSAGE_LEN; its value is unchanged.
|
|
19
|
+
|
|
20
|
+
### Features
|
|
21
|
+
|
|
22
|
+
* **transport:** carry the Guest ABI in the Canonical ABI's core shape ([34b9560](https://github.com/elct9620/kobako/commit/34b956001a05e15fe29febd7098b2ceb054e1bab))
|
|
23
|
+
* **transport:** expose the Capability Handle id bound the Envelope holds ([8a48012](https://github.com/elct9620/kobako/commit/8a48012cbac62c6a4a67c0c47664ee03a8089605))
|
|
24
|
+
* **transport:** spell once the form of a name a guest interns ([95c10bc](https://github.com/elct9620/kobako/commit/95c10bcf7180fc0e5ef432a9935ce3f856c75c9f))
|
|
25
|
+
* **transport:** spell once the names a Panic carries that a Frontend reads ([d1fb291](https://github.com/elct9620/kobako/commit/d1fb291082abcc376fc17d2f53b2e7d69dbf5da2))
|
|
26
|
+
* **transport:** spell once the Service and entrypoint names a Panic carries ([fd09028](https://github.com/elct9620/kobako/commit/fd09028dee973397c66eb2029f7fd75bf6e7ee26))
|
|
27
|
+
|
|
28
|
+
|
|
29
|
+
### Bug Fixes
|
|
30
|
+
|
|
31
|
+
* **guest:** say a guest supports no block, as the glossary words an Unsupported ([3aa9a78](https://github.com/elct9620/kobako/commit/3aa9a7844780ba095fa2d197c3ab1a163a33dce6))
|
|
32
|
+
* **transport:** refuse a Call target past the wire's Handle id range ([8a9b169](https://github.com/elct9620/kobako/commit/8a9b1694f96dc0769fff3251a76c3e1cb52a4866))
|
|
33
|
+
|
|
34
|
+
|
|
35
|
+
### Code Refactoring
|
|
36
|
+
|
|
37
|
+
* **transport:** load a Driver from its file, and read an origin as a lookup that may miss ([4ec8d79](https://github.com/elct9620/kobako/commit/4ec8d792ddf14bb21cfd36437b64b2389ee6df89))
|
|
38
|
+
* **transport:** name a Call's reply CallReply beside YieldReply and CompileReply ([f34063e](https://github.com/elct9620/kobako/commit/f34063ee94edb723800b4a0670cf912aa1a445e0))
|
|
39
|
+
* **transport:** name the message cap by what it bounds ([133e1c1](https://github.com/elct9620/kobako/commit/133e1c151506a1f41d5f2d17ac2678648c6ed262))
|
|
40
|
+
|
|
3
41
|
## [0.20.0](https://github.com/elct9620/kobako/compare/kobako-transport-v0.19.0...kobako-transport-v0.20.0) (2026-11-07)
|
|
4
42
|
|
|
5
43
|
|
|
@@ -15,9 +15,8 @@ same in every assembly. This crate is both of them, and nothing else.
|
|
|
15
15
|
The envelope covers the downcall's and the upcall's requests under one
|
|
16
16
|
tag registry. It also covers a dispatch round-trip, how an invocation
|
|
17
17
|
ended, and what an invocation is handed, and re-exports every message
|
|
18
|
-
type. The ABI values are the version, the
|
|
19
|
-
|
|
20
|
-
message size cap.
|
|
18
|
+
type. The ABI values are the version, the interface and function
|
|
19
|
+
names it carries, and the message size cap.
|
|
21
20
|
|
|
22
21
|
## Opaque Payload
|
|
23
22
|
|
|
@@ -52,7 +51,7 @@ reaches the same types through `kobako-core`.
|
|
|
52
51
|
|
|
53
52
|
```toml
|
|
54
53
|
[dependencies]
|
|
55
|
-
kobako-transport = "0.
|
|
54
|
+
kobako-transport = "0.22.0" # x-release-please-version
|
|
56
55
|
```
|
|
57
56
|
|
|
58
57
|
## License
|
|
@@ -1,88 +1,94 @@
|
|
|
1
|
-
//! The ABI's fixed values — the
|
|
2
|
-
//!
|
|
1
|
+
//! The ABI's fixed values — the names and limits a host and a guest must
|
|
2
|
+
//! already agree on before either can read the other's bytes.
|
|
3
3
|
//!
|
|
4
4
|
//! The symbols that carry the ABI are declared where they are
|
|
5
5
|
//! implemented: the guest contract crate emits the exports, the driver
|
|
6
|
-
//! resolves them and provides the
|
|
7
|
-
//!
|
|
6
|
+
//! resolves them and provides the imports. What lives here is what both
|
|
7
|
+
//! sides must spell the same way.
|
|
8
8
|
|
|
9
|
-
///
|
|
10
|
-
///
|
|
11
|
-
///
|
|
12
|
-
///
|
|
13
|
-
|
|
9
|
+
/// Spell an ABI name as a literal, for the attributes that accept no
|
|
10
|
+
/// constant: a guest's `export_name`, and its `wasm_import_module` through
|
|
11
|
+
/// `(host => $then)`, which hands the host interface to `$then!`.
|
|
12
|
+
///
|
|
13
|
+
/// `wasm_import_module` takes no macro, so the host interface is spelled
|
|
14
|
+
/// once more there; a build whose spelling no longer matches
|
|
15
|
+
/// `HOST_INTERFACE` fails.
|
|
16
|
+
#[doc(hidden)]
|
|
17
|
+
#[macro_export]
|
|
18
|
+
macro_rules! abi_name {
|
|
19
|
+
(version) => {
|
|
20
|
+
"0.6.0"
|
|
21
|
+
};
|
|
22
|
+
(guest) => {
|
|
23
|
+
concat!("kobako:abi/guest@", $crate::abi_name!(version))
|
|
24
|
+
};
|
|
25
|
+
(host) => {
|
|
26
|
+
concat!("kobako:abi/host@", $crate::abi_name!(version))
|
|
27
|
+
};
|
|
28
|
+
(host => $then:ident) => {
|
|
29
|
+
$crate::abi_name!(@import $then "kobako:abi/host@0.6.0");
|
|
30
|
+
};
|
|
31
|
+
(@import $then:ident $module:literal) => {
|
|
32
|
+
$then!($module);
|
|
33
|
+
const _: () = assert!($crate::abi::is_host_interface($module));
|
|
34
|
+
};
|
|
35
|
+
(downcall) => {
|
|
36
|
+
"downcall"
|
|
37
|
+
};
|
|
38
|
+
(downcall_export) => {
|
|
39
|
+
concat!($crate::abi_name!(guest), "#", $crate::abi_name!(downcall))
|
|
40
|
+
};
|
|
41
|
+
(upcall) => {
|
|
42
|
+
"upcall"
|
|
43
|
+
};
|
|
44
|
+
(preamble) => {
|
|
45
|
+
"preamble"
|
|
46
|
+
};
|
|
47
|
+
}
|
|
14
48
|
|
|
15
|
-
///
|
|
16
|
-
///
|
|
17
|
-
|
|
49
|
+
/// The Guest ABI version, as the semver every ABI name carries. A host
|
|
50
|
+
/// accepts a Guest Binary only when its names carry this one, so a framing
|
|
51
|
+
/// or symbol change is an increment rather than a negotiation. A new request
|
|
52
|
+
/// tag needs neither, but a new answer arm is an increment.
|
|
53
|
+
pub const ABI_VERSION: &str = abi_name!(version);
|
|
18
54
|
|
|
19
|
-
///
|
|
20
|
-
|
|
21
|
-
/// wire violation the receiving side refuses before allocating for it.
|
|
22
|
-
pub const MAX_DISPATCH_PAYLOAD: usize = 16 * 1024 * 1024;
|
|
55
|
+
/// The interface a Guest Binary exports its downcall under.
|
|
56
|
+
pub const GUEST_INTERFACE: &str = abi_name!(guest);
|
|
23
57
|
|
|
24
|
-
///
|
|
25
|
-
|
|
26
|
-
/// declared length may make it allocate. It sits above
|
|
27
|
-
/// `MAX_DISPATCH_PAYLOAD` because it guards the read rather than enforcing
|
|
28
|
-
/// the contract: the host enforces the 16 MiB cap before it sends a frame.
|
|
29
|
-
pub const MAX_FRAME_LEN: usize = 64 * 1024 * 1024;
|
|
58
|
+
/// The interface a Guest Binary imports the upcall and the Preamble from.
|
|
59
|
+
pub const HOST_INTERFACE: &str = abi_name!(host);
|
|
30
60
|
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
const _: () = assert!(MAX_FRAME_LEN > MAX_DISPATCH_PAYLOAD);
|
|
61
|
+
/// The guest function a host calls with each request.
|
|
62
|
+
pub const DOWNCALL: &str = abi_name!(downcall);
|
|
34
63
|
|
|
35
|
-
///
|
|
36
|
-
///
|
|
37
|
-
|
|
38
|
-
/// ```text
|
|
39
|
-
/// 63 32 31 0
|
|
40
|
-
/// ┌──────────┬────────────┐
|
|
41
|
-
/// │ ptr │ len │
|
|
42
|
-
/// └──────────┴────────────┘
|
|
43
|
-
/// ```
|
|
44
|
-
#[inline]
|
|
45
|
-
pub fn pack_ptr_len(ptr: u32, len: u32) -> u64 {
|
|
46
|
-
((ptr as u64) << 32) | (len as u64)
|
|
47
|
-
}
|
|
64
|
+
/// The core export the downcall stands under: its interface, `#`, then the
|
|
65
|
+
/// function, as the Canonical ABI's legacy core naming spells it.
|
|
66
|
+
pub const DOWNCALL_EXPORT: &str = abi_name!(downcall_export);
|
|
48
67
|
|
|
49
|
-
///
|
|
50
|
-
|
|
51
|
-
pub fn unpack_ptr_len(packed: u64) -> (u32, u32) {
|
|
52
|
-
((packed >> 32) as u32, packed as u32)
|
|
53
|
-
}
|
|
68
|
+
/// The host function a guest calls with each Call.
|
|
69
|
+
pub const UPCALL: &str = abi_name!(upcall);
|
|
54
70
|
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
use super::*;
|
|
71
|
+
/// The host function a guest calls for the Preamble of its Invocation.
|
|
72
|
+
pub const PREAMBLE: &str = abi_name!(preamble);
|
|
58
73
|
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
"the packed u64 must carry ptr in the high half and len in the low half"
|
|
66
|
-
);
|
|
74
|
+
/// Whether `module` spells `HOST_INTERFACE`, readable in a const context.
|
|
75
|
+
#[doc(hidden)]
|
|
76
|
+
pub const fn is_host_interface(module: &str) -> bool {
|
|
77
|
+
let (module, host) = (module.as_bytes(), HOST_INTERFACE.as_bytes());
|
|
78
|
+
if module.len() != host.len() {
|
|
79
|
+
return false;
|
|
67
80
|
}
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
for &(ptr, len) in &[
|
|
73
|
-
(0u32, 0u32),
|
|
74
|
-
(0x1000, 1024),
|
|
75
|
-
(0x0001_0000, 4),
|
|
76
|
-
(0x7fff_ffff, 0xffff),
|
|
77
|
-
(1, u32::MAX),
|
|
78
|
-
(u32::MAX, 1),
|
|
79
|
-
(u32::MAX, u32::MAX),
|
|
80
|
-
] {
|
|
81
|
-
assert_eq!(
|
|
82
|
-
unpack_ptr_len(pack_ptr_len(ptr, len)),
|
|
83
|
-
(ptr, len),
|
|
84
|
-
"a ({ptr:#x}, {len:#x}) pair must survive the pack the ABI returns"
|
|
85
|
-
);
|
|
81
|
+
let mut i = 0;
|
|
82
|
+
while i < host.len() {
|
|
83
|
+
if module[i] != host[i] {
|
|
84
|
+
return false;
|
|
86
85
|
}
|
|
86
|
+
i += 1;
|
|
87
87
|
}
|
|
88
|
+
true
|
|
88
89
|
}
|
|
90
|
+
|
|
91
|
+
/// The largest message either side sends: 16 MiB, the whole Envelope
|
|
92
|
+
/// rather than the payload alone. A larger one is a wire violation the
|
|
93
|
+
/// receiving side refuses before allocating for it.
|
|
94
|
+
pub const MAX_MESSAGE_LEN: usize = 16 * 1024 * 1024;
|
|
@@ -68,16 +68,27 @@ impl<'a> Reader<'a> {
|
|
|
68
68
|
self.read_list(|reader| Ok(reader.read_bytes()?.to_vec()))
|
|
69
69
|
}
|
|
70
70
|
|
|
71
|
+
/// A `u32` count of items each at least `min_len` bytes long. A count
|
|
72
|
+
/// past what the bytes left can hold can never be satisfied, so it is
|
|
73
|
+
/// refused with `too_many` before it sizes an allocation or bounds work.
|
|
74
|
+
pub(crate) fn read_count(
|
|
75
|
+
&mut self,
|
|
76
|
+
min_len: usize,
|
|
77
|
+
too_many: DecodeError,
|
|
78
|
+
) -> Result<usize, DecodeError> {
|
|
79
|
+
let count = self.read_u32()? as usize;
|
|
80
|
+
if count > self.remaining().len() / min_len {
|
|
81
|
+
return Err(too_many);
|
|
82
|
+
}
|
|
83
|
+
Ok(count)
|
|
84
|
+
}
|
|
85
|
+
|
|
71
86
|
fn read_list<T>(
|
|
72
87
|
&mut self,
|
|
73
88
|
mut element: impl FnMut(&mut Self) -> Result<T, DecodeError>,
|
|
74
89
|
) -> Result<Vec<T>, DecodeError> {
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
// bytes left can hold is refused before it sizes the allocation.
|
|
78
|
-
if count > self.remaining().len() / size_of::<u32>() {
|
|
79
|
-
return Err(MALFORMED);
|
|
80
|
-
}
|
|
90
|
+
// Every element opens with a `u32` length.
|
|
91
|
+
let count = self.read_count(size_of::<u32>(), MALFORMED)?;
|
|
81
92
|
let mut out = Vec::with_capacity(count);
|
|
82
93
|
for _ in 0..count {
|
|
83
94
|
out.push(element(self)?);
|