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,227 @@
|
|
|
1
|
+
//! What the generated bindings read and write in the guest's memory, as the
|
|
2
|
+
//! Canonical ABI lays a value out: little-endian scalars at their offset, and
|
|
3
|
+
//! lists the guest's `cabi_realloc` allocates.
|
|
4
|
+
|
|
5
|
+
use wasmtime::{AsContextMut, Caller, Extern, Memory};
|
|
6
|
+
|
|
7
|
+
/// A scalar the Canonical ABI stores little-endian at its natural size.
|
|
8
|
+
pub(crate) trait Scalar: Copy {
|
|
9
|
+
const SIZE: usize;
|
|
10
|
+
fn from_le(bytes: &[u8]) -> Self;
|
|
11
|
+
fn to_le(self, bytes: &mut [u8]);
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
macro_rules! scalar {
|
|
15
|
+
($($ty:ty),*) => {$(
|
|
16
|
+
impl Scalar for $ty {
|
|
17
|
+
const SIZE: usize = std::mem::size_of::<$ty>();
|
|
18
|
+
fn from_le(bytes: &[u8]) -> Self {
|
|
19
|
+
<$ty>::from_le_bytes(bytes.try_into().expect("a scalar's width"))
|
|
20
|
+
}
|
|
21
|
+
fn to_le(self, bytes: &mut [u8]) {
|
|
22
|
+
bytes.copy_from_slice(&self.to_le_bytes());
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
)*};
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
scalar!(u8, i8, u16, i16, u32, i32, u64, i64);
|
|
29
|
+
|
|
30
|
+
fn memory_of<T>(caller: &mut Caller<'_, T>) -> wasmtime::Result<Memory> {
|
|
31
|
+
match caller.get_export("memory") {
|
|
32
|
+
Some(Extern::Memory(memory)) => Ok(memory),
|
|
33
|
+
_ => Err(wasmtime::Error::msg("the guest exports no memory")),
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
/// The guest address `offset` bytes past `at`, a 32-bit pointer as the
|
|
38
|
+
/// Canonical ABI reads one; widened so the sum never wraps into memory the
|
|
39
|
+
/// guest did not name.
|
|
40
|
+
fn to_address(at: i32, offset: usize) -> usize {
|
|
41
|
+
at as u32 as usize + offset
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
fn read<T>(caller: &mut Caller<'_, T>, at: usize, buf: &mut [u8]) -> wasmtime::Result<()> {
|
|
45
|
+
let memory = memory_of(caller)?;
|
|
46
|
+
memory
|
|
47
|
+
.read(caller.as_context_mut(), at, buf)
|
|
48
|
+
.map_err(|_| wasmtime::Error::msg("a value the guest named lies outside its memory"))
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
fn write<T>(caller: &mut Caller<'_, T>, at: usize, bytes: &[u8]) -> wasmtime::Result<()> {
|
|
52
|
+
let memory = memory_of(caller)?;
|
|
53
|
+
memory
|
|
54
|
+
.write(caller.as_context_mut(), at, bytes)
|
|
55
|
+
.map_err(|_| wasmtime::Error::msg("an answer's place lies outside the guest's memory"))
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
/// The return area of `size` bytes the guest names at `at` for an answer,
|
|
59
|
+
/// once it lies wholly inside the guest's memory and on the answer's `align`.
|
|
60
|
+
/// The Canonical ABI's `lower_flat_values` checks both once the call returns
|
|
61
|
+
/// and before any part of the answer is stored or allocated.
|
|
62
|
+
pub(crate) fn ensure_return_area(
|
|
63
|
+
caller: &mut Caller<'_, impl Sized>,
|
|
64
|
+
at: i32,
|
|
65
|
+
size: usize,
|
|
66
|
+
align: usize,
|
|
67
|
+
) -> wasmtime::Result<()> {
|
|
68
|
+
let memory = memory_of(caller)?;
|
|
69
|
+
let inside = to_address(at, 0)
|
|
70
|
+
.checked_add(size)
|
|
71
|
+
.is_some_and(|end| end <= memory.data_size(caller.as_context_mut()));
|
|
72
|
+
match (inside, to_address(at, 0).is_multiple_of(align)) {
|
|
73
|
+
(true, true) => Ok(()),
|
|
74
|
+
(false, _) => Err(wasmtime::Error::msg(
|
|
75
|
+
"the return area the guest named lies outside its memory",
|
|
76
|
+
)),
|
|
77
|
+
(true, false) => Err(wasmtime::Error::msg(
|
|
78
|
+
"the return area the guest named is not aligned",
|
|
79
|
+
)),
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
pub(crate) fn load<S: Scalar>(
|
|
84
|
+
caller: &mut Caller<'_, impl Sized>,
|
|
85
|
+
at: i32,
|
|
86
|
+
offset: usize,
|
|
87
|
+
) -> wasmtime::Result<S> {
|
|
88
|
+
let mut bytes = [0; 8];
|
|
89
|
+
read(caller, to_address(at, offset), &mut bytes[..S::SIZE])?;
|
|
90
|
+
Ok(S::from_le(&bytes[..S::SIZE]))
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
pub(crate) fn store<S: Scalar>(
|
|
94
|
+
caller: &mut Caller<'_, impl Sized>,
|
|
95
|
+
at: i32,
|
|
96
|
+
offset: usize,
|
|
97
|
+
value: S,
|
|
98
|
+
) -> wasmtime::Result<()> {
|
|
99
|
+
let mut bytes = [0; 8];
|
|
100
|
+
value.to_le(&mut bytes[..S::SIZE]);
|
|
101
|
+
write(caller, to_address(at, offset), &bytes[..S::SIZE])
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
/// The length of the list of `len` elements of `size` bytes the guest names
|
|
105
|
+
/// at `at`, once it lies wholly inside the guest's memory and on its `align`,
|
|
106
|
+
/// as wasmtime's `WasmList::new` checks before anything is read; a list
|
|
107
|
+
/// outside traps, so no length the guest names sizes a host allocation first.
|
|
108
|
+
pub(crate) fn checked_list_len(
|
|
109
|
+
caller: &mut Caller<'_, impl Sized>,
|
|
110
|
+
at: i32,
|
|
111
|
+
len: i32,
|
|
112
|
+
size: usize,
|
|
113
|
+
align: usize,
|
|
114
|
+
) -> wasmtime::Result<usize> {
|
|
115
|
+
let len = len as u32 as usize;
|
|
116
|
+
let end = len
|
|
117
|
+
.checked_mul(size)
|
|
118
|
+
.and_then(|bytes| to_address(at, 0).checked_add(bytes));
|
|
119
|
+
let memory = memory_of(caller)?;
|
|
120
|
+
match end {
|
|
121
|
+
Some(end) if end <= memory.data_size(caller.as_context_mut()) => {}
|
|
122
|
+
_ => {
|
|
123
|
+
return Err(wasmtime::Error::msg(
|
|
124
|
+
"a list the guest named lies outside its memory",
|
|
125
|
+
))
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
if !to_address(at, 0).is_multiple_of(align) {
|
|
129
|
+
return Err(wasmtime::Error::msg(
|
|
130
|
+
"a list the guest named is not aligned",
|
|
131
|
+
));
|
|
132
|
+
}
|
|
133
|
+
Ok(len)
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
/// What one call may have the host copy out of the guest's memory, the
|
|
137
|
+
/// hostcall fuel wasmtime hands each host call, as preview1's calls spend it.
|
|
138
|
+
/// Each list a call lifts spends its bytes first, so lists naming one region
|
|
139
|
+
/// of memory over and over cannot multiply what the host holds.
|
|
140
|
+
pub(crate) struct Fuel(usize);
|
|
141
|
+
|
|
142
|
+
impl Fuel {
|
|
143
|
+
pub(crate) fn new(caller: &mut Caller<'_, impl Sized>) -> Self {
|
|
144
|
+
Self(caller.as_context_mut().hostcall_fuel())
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
/// Spend `len` elements of `size` bytes, or trap once the call's fuel is
|
|
148
|
+
/// gone, as wasmtime's `consume_fuel_array`.
|
|
149
|
+
pub(crate) fn spend(&mut self, len: usize, size: usize) -> wasmtime::Result<()> {
|
|
150
|
+
self.0 = len
|
|
151
|
+
.checked_mul(size)
|
|
152
|
+
.and_then(|bytes| self.0.checked_sub(bytes))
|
|
153
|
+
.ok_or_else(|| {
|
|
154
|
+
wasmtime::Error::msg("the guest had a call copy more than its hostcall fuel allows")
|
|
155
|
+
})?;
|
|
156
|
+
Ok(())
|
|
157
|
+
}
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
pub(crate) fn load_list<S: Scalar>(
|
|
161
|
+
caller: &mut Caller<'_, impl Sized>,
|
|
162
|
+
fuel: &mut Fuel,
|
|
163
|
+
at: i32,
|
|
164
|
+
len: i32,
|
|
165
|
+
) -> wasmtime::Result<Vec<S>> {
|
|
166
|
+
let len = checked_list_len(caller, at, len, S::SIZE, S::SIZE)?;
|
|
167
|
+
fuel.spend(len, S::SIZE)?;
|
|
168
|
+
let mut bytes = vec![0; len * S::SIZE];
|
|
169
|
+
read(caller, to_address(at, 0), &mut bytes)?;
|
|
170
|
+
Ok((0..len)
|
|
171
|
+
.map(|i| S::from_le(&bytes[i * S::SIZE..][..S::SIZE]))
|
|
172
|
+
.collect())
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
/// The string at `at`, `len` bytes of UTF-8 as the Canonical ABI encodes one;
|
|
176
|
+
/// other bytes trap, as the ABI's lift does.
|
|
177
|
+
pub(crate) fn load_string(
|
|
178
|
+
caller: &mut Caller<'_, impl Sized>,
|
|
179
|
+
fuel: &mut Fuel,
|
|
180
|
+
at: i32,
|
|
181
|
+
len: i32,
|
|
182
|
+
) -> wasmtime::Result<String> {
|
|
183
|
+
let len = checked_list_len(caller, at, len, 1, 1)?;
|
|
184
|
+
fuel.spend(len, 1)?;
|
|
185
|
+
let mut bytes = vec![0; len];
|
|
186
|
+
read(caller, to_address(at, 0), &mut bytes)?;
|
|
187
|
+
String::from_utf8(bytes).map_err(|_| invalid("string"))
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
/// Room for `size` bytes on `align` in the guest's memory, from its
|
|
191
|
+
/// `cabi_realloc`; room outside its memory or off its alignment traps, as
|
|
192
|
+
/// wasmtime checks `realloc`'s return.
|
|
193
|
+
pub(crate) fn realloc(
|
|
194
|
+
caller: &mut Caller<'_, impl Sized>,
|
|
195
|
+
size: i32,
|
|
196
|
+
align: usize,
|
|
197
|
+
) -> wasmtime::Result<i32> {
|
|
198
|
+
let realloc = caller
|
|
199
|
+
.get_export("cabi_realloc")
|
|
200
|
+
.and_then(Extern::into_func)
|
|
201
|
+
.ok_or_else(|| wasmtime::Error::msg("the guest exports no cabi_realloc"))?
|
|
202
|
+
.typed::<(i32, i32, i32, i32), i32>(caller.as_context_mut())?;
|
|
203
|
+
let at = realloc.call(caller.as_context_mut(), (0, 0, align as i32, size))?;
|
|
204
|
+
checked_list_len(caller, at, size, 1, align)?;
|
|
205
|
+
Ok(at)
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
pub(crate) fn store_list<S: Scalar>(
|
|
209
|
+
caller: &mut Caller<'_, impl Sized>,
|
|
210
|
+
list: &[S],
|
|
211
|
+
align: usize,
|
|
212
|
+
) -> wasmtime::Result<(i32, i32)> {
|
|
213
|
+
let mut bytes = vec![0; list.len() * S::SIZE];
|
|
214
|
+
for (i, value) in list.iter().enumerate() {
|
|
215
|
+
value.to_le(&mut bytes[i * S::SIZE..][..S::SIZE]);
|
|
216
|
+
}
|
|
217
|
+
let len = i32::try_from(list.len())
|
|
218
|
+
.map_err(|_| wasmtime::Error::msg("a list too long for the guest"))?;
|
|
219
|
+
let at = realloc(caller, bytes.len() as i32, align)?;
|
|
220
|
+
write(caller, to_address(at, 0), &bytes)?;
|
|
221
|
+
Ok((at, len))
|
|
222
|
+
}
|
|
223
|
+
|
|
224
|
+
/// The trap a value the guest encoded wrongly ends the run with.
|
|
225
|
+
pub(crate) fn invalid(what: &str) -> wasmtime::Error {
|
|
226
|
+
wasmtime::Error::msg(format!("the guest passed an invalid {what}"))
|
|
227
|
+
}
|
|
@@ -0,0 +1,118 @@
|
|
|
1
|
+
//! The host side of each WIT error a preview2 import answers, as wasmtime's
|
|
2
|
+
//! `trappable_error_type` gives one: a host method answers the guest's error or
|
|
3
|
+
//! traps through one `?`, and the generated glue turns it back into the WIT
|
|
4
|
+
//! error.
|
|
5
|
+
|
|
6
|
+
use kobako_runtime::Errno;
|
|
7
|
+
|
|
8
|
+
use super::bindings::wasi_http_types_0_2_12 as http;
|
|
9
|
+
use super::bindings::wasi_sockets_network_0_2_12 as network;
|
|
10
|
+
use super::sockets::to_error_code;
|
|
11
|
+
|
|
12
|
+
/// The error `E` the guest reads, or a trap ending the run, as wasmtime-wasi's
|
|
13
|
+
/// `TrappableError`.
|
|
14
|
+
#[derive(Debug)]
|
|
15
|
+
pub(crate) enum TrappableError<E> {
|
|
16
|
+
Error(E),
|
|
17
|
+
Trap(wasmtime::Error),
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
impl<E> TrappableError<E> {
|
|
21
|
+
/// The error the guest reads, or the trap.
|
|
22
|
+
pub(crate) fn downcast(self) -> wasmtime::Result<E> {
|
|
23
|
+
match self {
|
|
24
|
+
Self::Error(err) => Ok(err),
|
|
25
|
+
Self::Trap(trap) => Err(trap),
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
impl<E> From<wasmtime::Error> for TrappableError<E> {
|
|
31
|
+
fn from(trap: wasmtime::Error) -> Self {
|
|
32
|
+
Self::Trap(trap)
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
pub(crate) type SocketError = TrappableError<network::ErrorCode>;
|
|
37
|
+
pub(crate) type HttpError = TrappableError<http::ErrorCode>;
|
|
38
|
+
pub(crate) type HeaderError = TrappableError<http::HeaderError>;
|
|
39
|
+
|
|
40
|
+
impl From<network::ErrorCode> for SocketError {
|
|
41
|
+
fn from(code: network::ErrorCode) -> Self {
|
|
42
|
+
Self::Error(code)
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
/// A socket's failure, by the code its POSIX name maps to, as wasmtime-wasi's
|
|
47
|
+
/// `SocketError` takes an `io::Error`.
|
|
48
|
+
impl From<Errno> for SocketError {
|
|
49
|
+
fn from(err: Errno) -> Self {
|
|
50
|
+
Self::Error(to_error_code(&err))
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
impl From<http::ErrorCode> for HttpError {
|
|
55
|
+
fn from(code: http::ErrorCode) -> Self {
|
|
56
|
+
Self::Error(code)
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
impl From<http::HeaderError> for HeaderError {
|
|
61
|
+
fn from(err: http::HeaderError) -> Self {
|
|
62
|
+
Self::Error(err)
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
/// A stream's failure, as wasmtime-wasi-io's `StreamError`: the end, an error
|
|
67
|
+
/// the guest may read, or a trap.
|
|
68
|
+
#[derive(Debug)]
|
|
69
|
+
pub(crate) enum StreamError {
|
|
70
|
+
Closed,
|
|
71
|
+
LastOperationFailed(Errno),
|
|
72
|
+
Trap(wasmtime::Error),
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
impl From<wasmtime::Error> for StreamError {
|
|
76
|
+
fn from(trap: wasmtime::Error) -> Self {
|
|
77
|
+
Self::Trap(trap)
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
/// A broken pipe is the stream's end; anything else is an error the guest may
|
|
82
|
+
/// read.
|
|
83
|
+
impl From<Errno> for StreamError {
|
|
84
|
+
fn from(err: Errno) -> Self {
|
|
85
|
+
match err {
|
|
86
|
+
Errno::BrokenPipe => Self::Closed,
|
|
87
|
+
err => Self::LastOperationFailed(err),
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
/// A host method's answer split as the generated glue splits it: the trap
|
|
93
|
+
/// apart from the result the guest reads.
|
|
94
|
+
#[cfg(test)]
|
|
95
|
+
pub(crate) trait IntoAnswer<T, E> {
|
|
96
|
+
fn into_answer(self) -> wasmtime::Result<Result<T, E>>;
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
#[cfg(test)]
|
|
100
|
+
impl<T, E> IntoAnswer<T, E> for Result<T, TrappableError<E>> {
|
|
101
|
+
fn into_answer(self) -> wasmtime::Result<Result<T, E>> {
|
|
102
|
+
match self {
|
|
103
|
+
Ok(answer) => Ok(Ok(answer)),
|
|
104
|
+
Err(TrappableError::Error(err)) => Ok(Err(err)),
|
|
105
|
+
Err(TrappableError::Trap(trap)) => Err(trap),
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
#[cfg(test)]
|
|
111
|
+
impl<T> IntoAnswer<T, StreamError> for Result<T, StreamError> {
|
|
112
|
+
fn into_answer(self) -> wasmtime::Result<Result<T, StreamError>> {
|
|
113
|
+
match self {
|
|
114
|
+
Err(StreamError::Trap(trap)) => Err(trap),
|
|
115
|
+
answer => Ok(answer),
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
}
|
|
@@ -0,0 +1,122 @@
|
|
|
1
|
+
//! The `wasi:filesystem` host: what every encoding judges once a call walks
|
|
2
|
+
//! from a descriptor. preview1 adapts to it today, and a guest built for
|
|
3
|
+
//! preview2 inherits it. A call on an open file asks the Open File itself.
|
|
4
|
+
//! Setting its times is the exception: Linux's `do_utimes_fd` makes it
|
|
5
|
+
//! through the file's path, so the guard judges that path first.
|
|
6
|
+
|
|
7
|
+
use std::ops::Deref;
|
|
8
|
+
use std::sync::Arc;
|
|
9
|
+
|
|
10
|
+
use kobako_runtime::cred::Cred;
|
|
11
|
+
use kobako_runtime::filesystem::Iattr;
|
|
12
|
+
use kobako_runtime::security::WalkGuard;
|
|
13
|
+
use kobako_runtime::vfs;
|
|
14
|
+
use kobako_runtime::Errno;
|
|
15
|
+
|
|
16
|
+
/// What a `descriptor` stands for: the Open File every encoding shares.
|
|
17
|
+
pub(crate) use kobako_runtime::vfs::File;
|
|
18
|
+
|
|
19
|
+
/// How many times a call is made before a guest is answered EAGAIN, as cap-std
|
|
20
|
+
/// makes `openat2` four times: any move on a Filesystem answers a `..` walking
|
|
21
|
+
/// it so, and nothing bounds how often one lands.
|
|
22
|
+
const MAX_ATTEMPTS: usize = 4;
|
|
23
|
+
|
|
24
|
+
/// What `call` answers once no move landed during its walk, made again at most
|
|
25
|
+
/// `MAX_ATTEMPTS` times in all, as cap-std retries `openat2` on EAGAIN.
|
|
26
|
+
pub(crate) fn retry_eagain<T>(mut call: impl FnMut() -> Result<T, Errno>) -> Result<T, Errno> {
|
|
27
|
+
let mut answer = call();
|
|
28
|
+
for _ in 1..MAX_ATTEMPTS {
|
|
29
|
+
if !matches!(answer, Err(Errno::WouldBlock)) {
|
|
30
|
+
break;
|
|
31
|
+
}
|
|
32
|
+
answer = call();
|
|
33
|
+
}
|
|
34
|
+
answer
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
/// Where the descriptor `file` finds stands, for a path to be walked from,
|
|
38
|
+
/// once `guard` answered for a directory there. The descriptor is found when
|
|
39
|
+
/// the VFS has taken the name, as Linux's `path_init` takes it. What is no
|
|
40
|
+
/// directory the walk itself refuses, and a move the guard meets is one a
|
|
41
|
+
/// walk meets, so `retry_eagain` makes the call again.
|
|
42
|
+
pub(crate) fn start_at<'a, F: Deref<Target = File>>(
|
|
43
|
+
guard: &'a WalkGuard,
|
|
44
|
+
file: impl Fn() -> Result<F, Errno> + 'a,
|
|
45
|
+
) -> impl vfs::Start + 'a {
|
|
46
|
+
move || {
|
|
47
|
+
let file = file()?;
|
|
48
|
+
if file.is_directory() {
|
|
49
|
+
guard.check(file.path())?;
|
|
50
|
+
}
|
|
51
|
+
Ok(file.path().clone())
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
/// Set `file`'s times, once `guard` answered for where it stands.
|
|
56
|
+
pub(crate) fn set_times(
|
|
57
|
+
cred: &Arc<Cred>,
|
|
58
|
+
guard: &WalkGuard,
|
|
59
|
+
file: &File,
|
|
60
|
+
times: &Iattr,
|
|
61
|
+
) -> Result<(), Errno> {
|
|
62
|
+
retry_eagain(|| {
|
|
63
|
+
guard.check(file.path())?;
|
|
64
|
+
vfs::do_utimes_fd(cred, file.path(), times)
|
|
65
|
+
})
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
/// Make a link at `path` from `start` to `target`. A link to an absolute
|
|
69
|
+
/// path is refused once the start is found, as cap-std refuses one beneath
|
|
70
|
+
/// wasmtime's filesystem host, so no program outside the guest follows it
|
|
71
|
+
/// out of the Mount.
|
|
72
|
+
pub(crate) fn symlink(
|
|
73
|
+
cred: &Arc<Cred>,
|
|
74
|
+
target: &str,
|
|
75
|
+
start: &impl vfs::Start,
|
|
76
|
+
path: &str,
|
|
77
|
+
) -> Result<(), Errno> {
|
|
78
|
+
let start = || -> Result<vfs::Path, Errno> {
|
|
79
|
+
let start = start.path_init()?;
|
|
80
|
+
match target.starts_with('/') {
|
|
81
|
+
true => Err(Errno::NotPermitted),
|
|
82
|
+
false => Ok(start),
|
|
83
|
+
}
|
|
84
|
+
};
|
|
85
|
+
retry_eagain(|| vfs::filename_symlinkat(cred, target, &start, path))
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
#[cfg(test)]
|
|
89
|
+
mod tests {
|
|
90
|
+
use super::*;
|
|
91
|
+
|
|
92
|
+
/// What `retry_eagain` answers when the first `moves` calls each had a move
|
|
93
|
+
/// land in them, and how many calls it made.
|
|
94
|
+
fn retry_after_moves(moves: usize) -> (Result<(), Errno>, usize) {
|
|
95
|
+
let mut made = 0;
|
|
96
|
+
let answer = retry_eagain(|| {
|
|
97
|
+
made += 1;
|
|
98
|
+
match made <= moves {
|
|
99
|
+
true => Err(Errno::WouldBlock),
|
|
100
|
+
false => Ok(()),
|
|
101
|
+
}
|
|
102
|
+
});
|
|
103
|
+
(answer, made)
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
// A move lands during a walk only between two of its steps, which no guest
|
|
107
|
+
// call can time, so the retry itself witnesses it.
|
|
108
|
+
// @behavior RT-256
|
|
109
|
+
#[test]
|
|
110
|
+
fn a_path_call_a_move_landed_in_is_made_again_four_times_at_most() {
|
|
111
|
+
assert_eq!(
|
|
112
|
+
[
|
|
113
|
+
retry_after_moves(0),
|
|
114
|
+
retry_after_moves(3),
|
|
115
|
+
retry_after_moves(4)
|
|
116
|
+
],
|
|
117
|
+
[(Ok(()), 1), (Ok(()), 4), (Err(Errno::WouldBlock), 4),],
|
|
118
|
+
"a path_* call answered EAGAIN must be made again until it answers otherwise, four \
|
|
119
|
+
times in all at most, as cap-std makes openat2"
|
|
120
|
+
);
|
|
121
|
+
}
|
|
122
|
+
}
|