kobako 0.30.0 → 0.31.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/.release-please-manifest.json +1 -1
- data/CHANGELOG.md +335 -0
- data/Cargo.lock +469 -11
- data/README.md +43 -3
- data/ROADMAP.md +1 -1
- data/crates/kobako-runtime/CHANGELOG.md +219 -0
- data/crates/kobako-runtime/Cargo.toml +51 -3
- data/crates/kobako-runtime/README.md +7 -4
- data/crates/kobako-runtime/src/cred.rs +124 -0
- data/crates/kobako-runtime/src/dispatch.rs +4 -5
- data/crates/kobako-runtime/src/endpoint/buffer.rs +153 -44
- data/crates/kobako-runtime/src/endpoint/dispatch.rs +80 -41
- data/crates/kobako-runtime/src/endpoint/invoke.rs +23 -36
- data/crates/kobako-runtime/src/endpoint.rs +137 -36
- data/crates/kobako-runtime/src/errno.rs +391 -0
- data/crates/kobako-runtime/src/error.rs +31 -10
- data/crates/kobako-runtime/src/filesystem/anon_inodes.rs +52 -0
- data/crates/kobako-runtime/src/filesystem/bind/lookup.rs +52 -14
- data/crates/kobako-runtime/src/filesystem/bind/walk.rs +19 -20
- data/crates/kobako-runtime/src/filesystem/bind.rs +437 -245
- data/crates/kobako-runtime/src/filesystem/overlay/file.rs +6 -6
- data/crates/kobako-runtime/src/filesystem/overlay/path.rs +43 -65
- data/crates/kobako-runtime/src/filesystem/overlay/stack/copy_up.rs +9 -16
- data/crates/kobako-runtime/src/filesystem/overlay/stack/dir.rs +47 -72
- data/crates/kobako-runtime/src/filesystem/overlay/stack/namei.rs +12 -12
- data/crates/kobako-runtime/src/filesystem/overlay/stack/readdir.rs +4 -7
- data/crates/kobako-runtime/src/filesystem/overlay/stack.rs +121 -37
- data/crates/kobako-runtime/src/filesystem/overlay.rs +302 -343
- data/crates/kobako-runtime/src/filesystem/pipe.rs +12 -9
- data/crates/kobako-runtime/src/filesystem/rdir.rs +15 -32
- data/crates/kobako-runtime/src/filesystem/tmpfs/seed.rs +32 -0
- data/crates/kobako-runtime/src/filesystem/tmpfs/tmpfile.rs +3 -3
- data/crates/kobako-runtime/src/filesystem/tmpfs/whiteout.rs +57 -35
- data/crates/kobako-runtime/src/filesystem/tmpfs/xattr.rs +2 -2
- data/crates/kobako-runtime/src/filesystem/tmpfs.rs +172 -160
- data/crates/kobako-runtime/src/filesystem/wrap.rs +36 -52
- data/crates/kobako-runtime/src/filesystem.rs +6 -3
- data/crates/kobako-runtime/src/fork.rs +96 -0
- data/crates/kobako-runtime/src/grant.rs +3 -0
- data/crates/kobako-runtime/src/hashtable.rs +4 -2
- data/crates/kobako-runtime/src/lib.rs +18 -7
- data/crates/kobako-runtime/src/limits.rs +195 -0
- data/crates/kobako-runtime/src/locking.rs +10 -0
- data/crates/kobako-runtime/src/memcg.rs +383 -0
- data/crates/kobako-runtime/src/mount.rs +67 -9
- data/crates/kobako-runtime/src/net/backends.rs +43 -0
- data/crates/kobako-runtime/src/net/connections.rs +179 -0
- data/crates/kobako-runtime/src/net/credential.rs +125 -0
- data/crates/kobako-runtime/src/net/egress.rs +160 -0
- data/crates/kobako-runtime/src/net/family.rs +181 -0
- data/crates/kobako-runtime/src/net/http.rs +2733 -0
- data/crates/kobako-runtime/src/net/lookup.rs +285 -0
- data/crates/kobako-runtime/src/net/mock.rs +251 -0
- data/crates/kobako-runtime/src/net/namespace.rs +1366 -0
- data/crates/kobako-runtime/src/net/reactor.rs +531 -0
- data/crates/kobako-runtime/src/net/resolv/darwin.rs +275 -0
- data/crates/kobako-runtime/src/net/resolv/stub/tests.rs +469 -0
- data/crates/kobako-runtime/src/net/resolv/stub.rs +536 -0
- data/crates/kobako-runtime/src/net/resolv/windows.rs +248 -0
- data/crates/kobako-runtime/src/net/resolv.rs +227 -0
- data/crates/kobako-runtime/src/net/socket.rs +870 -0
- data/crates/kobako-runtime/src/net/tcp.rs +1588 -0
- data/crates/kobako-runtime/src/net/tls.rs +58 -0
- data/crates/kobako-runtime/src/net/transport.rs +279 -0
- data/crates/kobako-runtime/src/net/udp.rs +1003 -0
- data/crates/kobako-runtime/src/net.rs +42 -0
- data/crates/kobako-runtime/src/network/builder.rs +177 -0
- data/crates/kobako-runtime/src/network/ruleset.rs +777 -0
- data/crates/kobako-runtime/src/network.rs +551 -0
- data/crates/kobako-runtime/src/policy.rs +16 -0
- data/crates/kobako-runtime/src/profile.rs +29 -144
- data/crates/kobako-runtime/src/runtime.rs +3 -5
- data/crates/kobako-runtime/src/sched.rs +130 -29
- data/crates/kobako-runtime/src/security/entry_policy.rs +22 -0
- data/crates/kobako-runtime/src/{vfs/cred.rs → security/hooks.rs} +52 -144
- data/crates/kobako-runtime/src/security/mount_policy.rs +158 -0
- data/crates/kobako-runtime/src/security.rs +101 -196
- data/crates/kobako-runtime/src/snapshot.rs +121 -2
- data/crates/kobako-runtime/src/vfs/attr.rs +5 -5
- data/crates/kobako-runtime/src/vfs/dentry.rs +143 -89
- data/crates/kobako-runtime/src/vfs/file.rs +199 -203
- data/crates/kobako-runtime/src/vfs/inode.rs +41 -45
- data/crates/kobako-runtime/src/vfs/kern_path.rs +38 -42
- data/crates/kobako-runtime/src/vfs/mount.rs +10 -9
- data/crates/kobako-runtime/src/vfs/namei.rs +829 -700
- data/crates/kobako-runtime/src/vfs/namespace.rs +162 -0
- data/crates/kobako-runtime/src/vfs/open_files.rs +69 -0
- data/crates/kobako-runtime/src/vfs/openat2.rs +3 -3
- data/crates/kobako-runtime/src/vfs/path.rs +6 -6
- data/crates/kobako-runtime/src/vfs/readdir.rs +6 -6
- data/crates/kobako-runtime/src/vfs/stat.rs +159 -23
- data/crates/kobako-runtime/src/vfs/super_block.rs +18 -14
- data/crates/kobako-runtime/src/vfs/utimes.rs +37 -27
- data/crates/kobako-runtime/src/vfs.rs +9 -6
- data/crates/kobako-runtime/src/wait.rs +356 -0
- data/crates/kobako-transport/CHANGELOG.md +31 -0
- data/crates/kobako-transport/Cargo.toml +1 -1
- data/crates/kobako-transport/README.md +3 -4
- data/crates/kobako-transport/src/abi.rs +78 -72
- data/crates/kobako-transport/src/envelope/bytes.rs +17 -6
- data/crates/kobako-transport/src/envelope/call.rs +32 -4
- data/crates/kobako-transport/src/envelope/error_record.rs +21 -0
- data/crates/kobako-transport/src/envelope/name.rs +99 -0
- data/crates/kobako-transport/src/envelope/outcome.rs +9 -9
- data/crates/kobako-transport/src/envelope/preamble.rs +24 -21
- data/crates/kobako-transport/src/envelope/reply.rs +27 -27
- data/crates/kobako-transport/src/envelope/unsupported.rs +10 -10
- data/crates/kobako-transport/src/envelope.rs +13 -8
- data/crates/kobako-wasmtime/CHANGELOG.md +161 -0
- data/crates/kobako-wasmtime/Cargo.toml +26 -7
- data/crates/kobako-wasmtime/README.md +3 -2
- data/crates/kobako-wasmtime/build/preview2.rs +1339 -0
- data/crates/kobako-wasmtime/build.rs +16 -0
- data/crates/kobako-wasmtime/src/abi.rs +17 -37
- data/crates/kobako-wasmtime/src/ambient.rs +33 -18
- data/crates/kobako-wasmtime/src/cache.rs +23 -15
- data/crates/kobako-wasmtime/src/config.rs +52 -68
- data/crates/kobako-wasmtime/src/driver.rs +48 -63
- data/crates/kobako-wasmtime/src/exports.rs +143 -17
- data/crates/kobako-wasmtime/src/guest.rs +12 -6
- data/crates/kobako-wasmtime/src/guest_binary.rs +14 -0
- data/crates/kobako-wasmtime/src/imports.rs +42 -18
- data/crates/kobako-wasmtime/src/instance_pre.rs +4 -4
- data/crates/kobako-wasmtime/src/invocation.rs +74 -12
- data/crates/kobako-wasmtime/src/lib.rs +5 -3
- data/crates/kobako-wasmtime/src/limiter.rs +70 -5
- data/crates/kobako-wasmtime/src/preview1/descriptors.rs +309 -0
- data/crates/kobako-wasmtime/src/preview1/differential/mount.rs +5 -15
- data/crates/kobako-wasmtime/src/preview1/differential.rs +27 -31
- data/crates/kobako-wasmtime/src/preview1/errno.rs +55 -66
- data/crates/kobako-wasmtime/src/preview1/fd.rs +327 -296
- data/crates/kobako-wasmtime/src/preview1/memory.rs +21 -12
- data/crates/kobako-wasmtime/src/preview1/mount_view/wrap.rs +34 -53
- data/crates/kobako-wasmtime/src/preview1/mount_view.rs +231 -217
- data/crates/kobako-wasmtime/src/preview1/path.rs +56 -121
- data/crates/kobako-wasmtime/src/preview1/poll.rs +244 -120
- data/crates/kobako-wasmtime/src/preview1/sock.rs +10 -8
- data/crates/kobako-wasmtime/src/preview1/testsuite.rs +5 -15
- data/crates/kobako-wasmtime/src/preview1.rs +46 -42
- data/crates/kobako-wasmtime/src/preview2/abi.rs +227 -0
- data/crates/kobako-wasmtime/src/preview2/error.rs +118 -0
- data/crates/kobako-wasmtime/src/preview2/filesystem.rs +122 -0
- data/crates/kobako-wasmtime/src/preview2/host.rs +3959 -0
- data/crates/kobako-wasmtime/src/preview2/http.rs +1503 -0
- data/crates/kobako-wasmtime/src/preview2/io.rs +315 -0
- data/crates/kobako-wasmtime/src/preview2/sockets.rs +379 -0
- data/crates/kobako-wasmtime/src/preview2/table.rs +305 -0
- data/crates/kobako-wasmtime/src/preview2.rs +57 -0
- data/crates/kobako-wasmtime/src/trap.rs +24 -9
- data/crates/kobako-wasmtime/src/wasi.rs +210 -81
- data/crates/kobako-wasmtime/wit/deps/cli.wit +233 -0
- data/crates/kobako-wasmtime/wit/deps/clocks.wit +162 -0
- data/crates/kobako-wasmtime/wit/deps/filesystem.wit +589 -0
- data/crates/kobako-wasmtime/wit/deps/http.wit +733 -0
- data/crates/kobako-wasmtime/wit/deps/io.wit +299 -0
- data/crates/kobako-wasmtime/wit/deps/random.wit +92 -0
- data/crates/kobako-wasmtime/wit/deps/sockets.wit +949 -0
- data/data/kobako.wasm +0 -0
- data/ext/kobako/Cargo.toml +1 -1
- data/ext/kobako/src/{runtime/errors.rs → errors.rs} +34 -26
- data/ext/kobako/src/{runtime/escape.rs → escape.rs} +42 -11
- data/ext/kobako/src/{runtime/filesystem → filesystem}/grants.rs +30 -26
- data/ext/kobako/src/{runtime/filesystem → filesystem}/ruby.rs +149 -168
- data/ext/kobako/src/{runtime/filesystem.rs → filesystem.rs} +24 -15
- data/ext/kobako/src/gvl.rs +306 -0
- data/ext/kobako/src/lib.rs +64 -3
- data/ext/kobako/src/network/mock.rs +187 -0
- data/ext/kobako/src/network.rs +591 -0
- data/ext/kobako/src/runtime/bridge.rs +30 -29
- data/ext/kobako/src/runtime/snapshot.rs +4 -4
- data/ext/kobako/src/runtime.rs +221 -143
- data/ext/kobako/src/wire.rs +42 -0
- data/lib/kobako/catalog/extensions.rb +14 -2
- data/lib/kobako/catalog/flags.rb +3 -5
- data/lib/kobako/catalog/grants.rb +5 -4
- data/lib/kobako/catalog/handles.rb +5 -27
- data/lib/kobako/catalog/mount.rb +11 -3
- data/lib/kobako/catalog/network.rb +35 -0
- data/lib/kobako/catalog/replacements.rb +28 -0
- data/lib/kobako/catalog/services.rb +5 -19
- data/lib/kobako/catalog/snippets.rb +3 -18
- data/lib/kobako/catalog.rb +3 -0
- data/lib/kobako/codec/{utils.rb → boundary.rb} +4 -2
- data/lib/kobako/codec/decoder.rb +4 -4
- data/lib/kobako/codec/ext_types.rb +3 -3
- data/lib/kobako/codec/handle_walk.rb +3 -43
- data/lib/kobako/codec/value_budget.rb +132 -40
- data/lib/kobako/codec.rb +1 -1
- data/lib/kobako/context.rb +24 -11
- data/lib/kobako/errors.rb +15 -12
- data/lib/kobako/extension.rb +3 -2
- data/lib/kobako/filesystem.rb +9 -2
- data/lib/kobako/handle.rb +2 -2
- data/lib/kobako/names.rb +28 -0
- data/lib/kobako/network.rb +210 -0
- data/lib/kobako/outcome.rb +7 -7
- data/lib/kobako/pool.rb +45 -23
- data/lib/kobako/runtime.rb +3 -2
- data/lib/kobako/sandbox.rb +92 -35
- data/lib/kobako/sandbox_options.rb +41 -106
- data/lib/kobako/transport/argument_wrap.rb +64 -0
- data/lib/kobako/transport/block_failure.rb +1 -1
- data/lib/kobako/transport/dispatcher.rb +6 -6
- data/lib/kobako/transport/exposure.rb +26 -15
- data/lib/kobako/transport/reflection.rb +21 -4
- data/lib/kobako/transport/run.rb +8 -23
- data/lib/kobako/transport/yielder.rb +2 -9
- data/lib/kobako/transport.rb +1 -0
- data/lib/kobako/version.rb +1 -1
- data/lib/kobako.rb +6 -2
- data/release-please-config.json +15 -0
- data/sig/core/thread.rbs +6 -0
- data/sig/kobako/catalog/extensions.rbs +2 -0
- data/sig/kobako/catalog/grants.rbs +7 -4
- data/sig/kobako/catalog/handles.rbs +0 -2
- data/sig/kobako/catalog/mount.rbs +3 -1
- data/sig/kobako/catalog/network.rbs +20 -0
- data/sig/kobako/catalog/replacements.rbs +14 -0
- data/sig/kobako/catalog/services.rbs +0 -4
- data/sig/kobako/catalog/snippets.rbs +0 -2
- data/sig/kobako/codec/{utils.rbs → boundary.rbs} +2 -2
- data/sig/kobako/codec/handle_walk.rbs +0 -6
- data/sig/kobako/codec/value_budget.rbs +47 -4
- data/sig/kobako/context.rbs +3 -1
- data/sig/kobako/errors.rbs +4 -4
- data/sig/kobako/filesystem.rbs +21 -3
- data/sig/kobako/network.rbs +115 -0
- data/sig/kobako/outcome.rbs +1 -1
- data/sig/kobako/pool.rbs +7 -3
- data/sig/kobako/runtime.rbs +32 -3
- data/sig/kobako/sandbox.rbs +19 -2
- data/sig/kobako/sandbox_options.rbs +29 -38
- data/sig/kobako/transport/argument_wrap.rbs +15 -0
- data/sig/kobako/transport/error.rbs +1 -1
- data/sig/kobako/transport/exposure.rbs +2 -4
- data/sig/kobako/transport/reflection.rbs +10 -0
- data/sig/kobako/transport/run.rbs +0 -2
- data/sig/kobako/transport/yielder.rbs +0 -3
- data/sig/kobako/transport.rbs +1 -1
- data/sig/kobako.rbs +13 -0
- metadata +84 -35
- data/crates/kobako-runtime/src/endpoint/capture.rs +0 -115
- data/crates/kobako-runtime/src/endpoint/memory_cap.rs +0 -238
- data/crates/kobako-runtime/src/filesystem/path.rs +0 -21
- data/crates/kobako-runtime/src/vfs/errno.rs +0 -153
- data/crates/kobako-runtime/tests/linux_baseline/calls.rs +0 -171
- data/crates/kobako-runtime/tests/linux_baseline/concurrent.rs +0 -276
- data/crates/kobako-runtime/tests/linux_baseline/kobako.rs +0 -323
- data/crates/kobako-runtime/tests/linux_baseline/linux-baseline-expected.txt +0 -16
- data/crates/kobako-runtime/tests/linux_baseline/linux.rs +0 -449
- data/crates/kobako-runtime/tests/linux_baseline/main.rs +0 -611
- data/crates/kobako-runtime/tests/linux_baseline/permissions.rs +0 -236
- data/crates/kobako-wasmtime/src/descriptors.rs +0 -200
- data/crates/kobako-wasmtime/tests/integration/compile_posture.rs +0 -43
- data/crates/kobako-wasmtime/tests/integration/guest_file.rs +0 -1085
- data/crates/kobako-wasmtime/tests/integration/main.rs +0 -29
- data/crates/kobako-wasmtime/tests/integration/preamble_flags.rs +0 -70
- data/crates/kobako-wasmtime/tests/integration/snippet_compile.rs +0 -95
- data/crates/kobako-wasmtime/tests/integration/support/mod.rs +0 -30
- data/crates/kobako-wasmtime/tests/integration/unanswered_call.rs +0 -86
- data/crates/kobako-wasmtime/tests/integration/unsupported_call.rs +0 -51
- data/ext/kobako/src/runtime/gvl.rs +0 -147
- data/lib/kobako/constant_name.rb +0 -8
- data/sig/kobako/constant_name.rbs +0 -3
- /data/ext/kobako/src/{runtime/pinned_value.rs → pinned_value.rs} +0 -0
|
@@ -1,18 +1,26 @@
|
|
|
1
|
-
//! Guest linear-memory I/O for the ABI's
|
|
1
|
+
//! Guest linear-memory I/O for the ABI's list handoffs, whether the
|
|
2
2
|
//! invocation body or an upcall drives one.
|
|
3
3
|
//!
|
|
4
|
-
//!
|
|
5
|
-
//!
|
|
6
|
-
//!
|
|
7
|
-
//!
|
|
4
|
+
//! Every list the ABI carries is named by a return area: two little-endian
|
|
5
|
+
//! u32, its place then its length, 4-byte aligned, as the Canonical ABI lays
|
|
6
|
+
//! a `list<u8>` out. The shared steps answer an error kind, and each caller
|
|
7
|
+
//! words it, because depth decides what the same problem means: before the
|
|
8
|
+
//! export call it is a `Trap`, inside a callback it is a wire failure the
|
|
9
|
+
//! guest receives.
|
|
8
10
|
|
|
9
|
-
use kobako_transport::abi::
|
|
11
|
+
use kobako_transport::abi::MAX_MESSAGE_LEN;
|
|
10
12
|
|
|
11
13
|
use super::{GuestInstance, RUNTIME_INCOMPATIBLE, SANDBOX_RUNTIME_NOT_KOBAKO};
|
|
12
14
|
|
|
13
15
|
// The size-limit messages here and in the endpoint name the cap as 16 MiB, so
|
|
14
16
|
// moving the cap fails the build until they are reworded with it.
|
|
15
|
-
const _: () = assert!(
|
|
17
|
+
const _: () = assert!(MAX_MESSAGE_LEN == 16 << 20);
|
|
18
|
+
|
|
19
|
+
/// The bytes of a return area: a u32 place, then a u32 length.
|
|
20
|
+
const RETURN_AREA_SIZE: usize = 8;
|
|
21
|
+
|
|
22
|
+
/// The alignment a return area keeps, its u32's own.
|
|
23
|
+
const RETURN_AREA_ALIGN: usize = 4;
|
|
16
24
|
|
|
17
25
|
/// Why a host write into guest memory failed.
|
|
18
26
|
pub(super) enum WriteError<F> {
|
|
@@ -21,8 +29,6 @@ pub(super) enum WriteError<F> {
|
|
|
21
29
|
/// message for it.
|
|
22
30
|
Incompatible(&'static str),
|
|
23
31
|
Trapped(F),
|
|
24
|
-
/// The allocator ran and answered 0.
|
|
25
|
-
OutOfMemory,
|
|
26
32
|
OutOfBounds,
|
|
27
33
|
}
|
|
28
34
|
|
|
@@ -32,8 +38,8 @@ pub(super) enum ReadError {
|
|
|
32
38
|
OutOfBounds,
|
|
33
39
|
}
|
|
34
40
|
|
|
35
|
-
///
|
|
36
|
-
/// address.
|
|
41
|
+
/// Take room from the guest's `cabi_realloc` and copy `bytes` into it,
|
|
42
|
+
/// answering the room's address.
|
|
37
43
|
pub(super) fn alloc_and_write<G: GuestInstance>(
|
|
38
44
|
guest: &mut G,
|
|
39
45
|
bytes: &[u8],
|
|
@@ -42,13 +48,11 @@ pub(super) fn alloc_and_write<G: GuestInstance>(
|
|
|
42
48
|
if guest.memory().is_none() {
|
|
43
49
|
return Err(WriteError::Incompatible(SANDBOX_RUNTIME_NOT_KOBAKO));
|
|
44
50
|
}
|
|
51
|
+
// Fresh room for a `list<u8>`, which the Canonical ABI aligns to 1.
|
|
45
52
|
let ptr = guest
|
|
46
|
-
.
|
|
53
|
+
.realloc(0, 0, 1, len as u32)
|
|
47
54
|
.ok_or(WriteError::Incompatible(RUNTIME_INCOMPATIBLE))?
|
|
48
55
|
.map_err(WriteError::Trapped)?;
|
|
49
|
-
if ptr == 0 {
|
|
50
|
-
return Err(WriteError::OutOfMemory);
|
|
51
|
-
}
|
|
52
56
|
let memory = guest
|
|
53
57
|
.memory()
|
|
54
58
|
.ok_or(WriteError::Incompatible(SANDBOX_RUNTIME_NOT_KOBAKO))?;
|
|
@@ -58,10 +62,43 @@ pub(super) fn alloc_and_write<G: GuestInstance>(
|
|
|
58
62
|
Ok(ptr)
|
|
59
63
|
}
|
|
60
64
|
|
|
65
|
+
/// Write `bytes` into the guest and name them in the return area at `ret`.
|
|
66
|
+
/// A return area the guest cannot hold is refused before the guest gives
|
|
67
|
+
/// room for the answer; memory only grows, so it still holds after.
|
|
68
|
+
pub(super) fn lower_list<G: GuestInstance>(
|
|
69
|
+
guest: &mut G,
|
|
70
|
+
bytes: &[u8],
|
|
71
|
+
ret: u32,
|
|
72
|
+
) -> Result<(), WriteError<G::Fault>> {
|
|
73
|
+
let mem_size = guest
|
|
74
|
+
.memory()
|
|
75
|
+
.ok_or(WriteError::Incompatible(SANDBOX_RUNTIME_NOT_KOBAKO))?
|
|
76
|
+
.len();
|
|
77
|
+
let area = return_area_range(ret as usize, mem_size).map_err(|_| WriteError::OutOfBounds)?;
|
|
78
|
+
let ptr = alloc_and_write(guest, bytes)?;
|
|
79
|
+
let memory = guest
|
|
80
|
+
.memory()
|
|
81
|
+
.ok_or(WriteError::Incompatible(SANDBOX_RUNTIME_NOT_KOBAKO))?;
|
|
82
|
+
// The write has already held the length to the payload cap.
|
|
83
|
+
memory[area.start..area.start + 4].copy_from_slice(&ptr.to_le_bytes());
|
|
84
|
+
memory[area.start + 4..area.end].copy_from_slice(&(bytes.len() as u32).to_le_bytes());
|
|
85
|
+
Ok(())
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
/// Copy out the list the return area at `ret` names; a length past the
|
|
89
|
+
/// 16 MiB cap is refused before the list is touched.
|
|
90
|
+
pub(super) fn lift_list(memory: &[u8], ret: u32) -> Result<Vec<u8>, ReadError> {
|
|
91
|
+
let area = return_area_range(ret as usize, memory.len()).map_err(|_| ReadError::OutOfBounds)?;
|
|
92
|
+
let word =
|
|
93
|
+
|at: usize| u32::from_le_bytes(memory[at..at + 4].try_into().expect("a u32's width"));
|
|
94
|
+
let (ptr, len) = (word(area.start), word(area.start + 4));
|
|
95
|
+
read_buffer(memory, ptr as usize, len as usize)
|
|
96
|
+
}
|
|
97
|
+
|
|
61
98
|
/// Copy out the buffer the guest names; a length past the 16 MiB cap is
|
|
62
99
|
/// refused before memory is touched.
|
|
63
100
|
pub(super) fn read_buffer(data: &[u8], ptr: usize, len: usize) -> Result<Vec<u8>, ReadError> {
|
|
64
|
-
if len >
|
|
101
|
+
if len > MAX_MESSAGE_LEN {
|
|
65
102
|
return Err(ReadError::TooLarge);
|
|
66
103
|
}
|
|
67
104
|
let range = guest_buffer_range(ptr, len, data.len()).map_err(|_| ReadError::OutOfBounds)?;
|
|
@@ -71,7 +108,7 @@ pub(super) fn read_buffer(data: &[u8], ptr: usize, len: usize) -> Result<Vec<u8>
|
|
|
71
108
|
/// Every host write boundary routes its length through here, so the
|
|
72
109
|
/// wire-violation reason is uniform.
|
|
73
110
|
pub(super) fn checked_payload_len(len: usize) -> Result<i32, &'static str> {
|
|
74
|
-
if len >
|
|
111
|
+
if len > MAX_MESSAGE_LEN {
|
|
75
112
|
return Err("payload exceeds the 16 MiB limit");
|
|
76
113
|
}
|
|
77
114
|
// The cap above sits below `i32::MAX`, so this conversion cannot wrap.
|
|
@@ -91,29 +128,61 @@ fn guest_buffer_range(
|
|
|
91
128
|
Ok(ptr..end)
|
|
92
129
|
}
|
|
93
130
|
|
|
94
|
-
/// The
|
|
95
|
-
///
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
131
|
+
/// The bytes of guest memory the return area at `ret` covers, refused off
|
|
132
|
+
/// its alignment or reaching past memory.
|
|
133
|
+
fn return_area_range(ret: usize, mem_size: usize) -> Result<core::ops::Range<usize>, &'static str> {
|
|
134
|
+
if !ret.is_multiple_of(RETURN_AREA_ALIGN) {
|
|
135
|
+
return Err("the return area is not aligned");
|
|
136
|
+
}
|
|
137
|
+
guest_buffer_range(ret, RETURN_AREA_SIZE, mem_size)
|
|
99
138
|
}
|
|
100
139
|
|
|
101
140
|
#[cfg(test)]
|
|
102
141
|
mod tests {
|
|
103
|
-
use super::{
|
|
104
|
-
|
|
105
|
-
|
|
142
|
+
use super::{checked_payload_len, guest_buffer_range, lift_list, lower_list, MAX_MESSAGE_LEN};
|
|
143
|
+
use crate::endpoint::{Calls, GuestInstance};
|
|
144
|
+
use crate::error::Trap;
|
|
145
|
+
|
|
146
|
+
/// 64 bytes of guest memory whose allocator counts the room it gave.
|
|
147
|
+
#[derive(Default)]
|
|
148
|
+
struct Counted {
|
|
149
|
+
memory: Vec<u8>,
|
|
150
|
+
given: u32,
|
|
151
|
+
calls: Calls<String>,
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
impl GuestInstance for Counted {
|
|
155
|
+
type Fault = String;
|
|
156
|
+
|
|
157
|
+
fn calls(&mut self) -> &mut Calls<String> {
|
|
158
|
+
&mut self.calls
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
fn memory(&mut self) -> Option<&mut [u8]> {
|
|
162
|
+
Some(&mut self.memory)
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
fn realloc(&mut self, _: u32, _: u32, _: u32, _: u32) -> Option<Result<u32, String>> {
|
|
166
|
+
self.given += 1;
|
|
167
|
+
Some(Ok(32))
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
fn downcall(&mut self, _ptr: i32, _len: i32) -> Option<Result<u32, String>> {
|
|
171
|
+
None
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
fn cap_trap_of(_fault: &String) -> Option<Trap> {
|
|
175
|
+
None
|
|
176
|
+
}
|
|
177
|
+
}
|
|
106
178
|
|
|
107
179
|
// @behavior WE-059
|
|
108
180
|
#[test]
|
|
109
181
|
fn a_transfer_of_nothing_or_at_the_cap_is_admitted() {
|
|
110
182
|
assert_eq!(
|
|
111
|
-
(
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
),
|
|
115
|
-
(Ok(0), Ok(MAX_DISPATCH_PAYLOAD as i32)),
|
|
116
|
-
"0 bytes and MAX_DISPATCH_PAYLOAD bytes through checked_payload_len must be admitted"
|
|
183
|
+
(checked_payload_len(0), checked_payload_len(MAX_MESSAGE_LEN)),
|
|
184
|
+
(Ok(0), Ok(MAX_MESSAGE_LEN as i32)),
|
|
185
|
+
"0 bytes and MAX_MESSAGE_LEN bytes through checked_payload_len must be admitted"
|
|
117
186
|
);
|
|
118
187
|
}
|
|
119
188
|
|
|
@@ -121,9 +190,9 @@ mod tests {
|
|
|
121
190
|
#[test]
|
|
122
191
|
fn a_transfer_past_the_cap_is_refused() {
|
|
123
192
|
assert!(
|
|
124
|
-
checked_payload_len(
|
|
193
|
+
checked_payload_len(MAX_MESSAGE_LEN + 1).is_err()
|
|
125
194
|
&& checked_payload_len(usize::MAX).is_err(),
|
|
126
|
-
"one byte past
|
|
195
|
+
"one byte past MAX_MESSAGE_LEN and usize::MAX through checked_payload_len must be \
|
|
127
196
|
refused"
|
|
128
197
|
);
|
|
129
198
|
}
|
|
@@ -169,24 +238,64 @@ mod tests {
|
|
|
169
238
|
);
|
|
170
239
|
}
|
|
171
240
|
|
|
172
|
-
|
|
241
|
+
/// 64 bytes of memory holding `answer` at 32, named by a return area at 8.
|
|
242
|
+
fn memory_naming(answer: &[u8]) -> Vec<u8> {
|
|
243
|
+
let mut memory = vec![0; 64];
|
|
244
|
+
memory[8..12].copy_from_slice(&32u32.to_le_bytes());
|
|
245
|
+
memory[12..16].copy_from_slice(&(answer.len() as u32).to_le_bytes());
|
|
246
|
+
memory[32..32 + answer.len()].copy_from_slice(answer);
|
|
247
|
+
memory
|
|
248
|
+
}
|
|
249
|
+
|
|
250
|
+
// @behavior WE-092
|
|
173
251
|
#[test]
|
|
174
|
-
fn
|
|
252
|
+
fn a_return_area_names_its_answers_place_then_its_length() {
|
|
253
|
+
let memory = memory_naming(b"answer");
|
|
254
|
+
|
|
175
255
|
assert_eq!(
|
|
176
|
-
|
|
177
|
-
(
|
|
178
|
-
"
|
|
179
|
-
length 0x1122_3344"
|
|
256
|
+
lift_list(&memory, 8).ok(),
|
|
257
|
+
Some(b"answer".to_vec()),
|
|
258
|
+
"a return area holding 32 then 6, read by the host, must answer the 6 bytes at 32"
|
|
180
259
|
);
|
|
181
260
|
}
|
|
182
261
|
|
|
183
|
-
// @behavior WE-
|
|
262
|
+
// @behavior WE-093
|
|
184
263
|
#[test]
|
|
185
|
-
fn
|
|
264
|
+
fn a_return_area_off_its_alignment_or_past_memory_is_refused() {
|
|
265
|
+
let mut memory = memory_naming(b"answer");
|
|
266
|
+
// At 1, off alignment, the same place and length the area at 8 holds.
|
|
267
|
+
memory.copy_within(8..16, 1);
|
|
268
|
+
|
|
269
|
+
assert!(
|
|
270
|
+
[1, 60, u32::MAX - 3]
|
|
271
|
+
.iter()
|
|
272
|
+
.all(|&ret| lift_list(&memory, ret).is_err()),
|
|
273
|
+
"a return area at 1 (unaligned, naming a list inside memory), 60 (past 64 bytes), or \
|
|
274
|
+
u32::MAX - 3, read by the host, must be refused"
|
|
275
|
+
);
|
|
276
|
+
}
|
|
277
|
+
|
|
278
|
+
// @behavior WE-093
|
|
279
|
+
#[test]
|
|
280
|
+
fn an_answer_for_a_return_area_the_guest_cannot_hold_is_never_copied() {
|
|
281
|
+
let mut guest = Counted {
|
|
282
|
+
memory: vec![0; 64],
|
|
283
|
+
..Counted::default()
|
|
284
|
+
};
|
|
285
|
+
let refused = [1, 60, u32::MAX - 3]
|
|
286
|
+
.iter()
|
|
287
|
+
.all(|&ret| lower_list(&mut guest, b"answer", ret).is_err());
|
|
288
|
+
|
|
186
289
|
assert_eq!(
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
290
|
+
(
|
|
291
|
+
refused,
|
|
292
|
+
guest.given,
|
|
293
|
+
guest.memory.iter().all(|&byte| byte == 0)
|
|
294
|
+
),
|
|
295
|
+
(true, 0, true),
|
|
296
|
+
"an answer written for a return area at 1 (unaligned), 60 (past 64 bytes), or \
|
|
297
|
+
u32::MAX - 3 must be refused before the guest's allocator gives it room or any byte \
|
|
298
|
+
lands in guest memory"
|
|
190
299
|
);
|
|
191
300
|
}
|
|
192
301
|
}
|
|
@@ -1,7 +1,8 @@
|
|
|
1
|
-
//! Host side of the `
|
|
2
|
-
//! the bound `DispatchHandler` and write its Reply back into guest
|
|
3
|
-
//!
|
|
4
|
-
//!
|
|
1
|
+
//! Host side of the `upcall` and `preamble` imports: hand the guest's Call
|
|
2
|
+
//! to the bound `DispatchHandler` and write its Reply back into guest
|
|
3
|
+
//! memory, or write the Invocation's Preamble there. A request under a tag
|
|
4
|
+
//! the host does not serve is answered as unsupported, so a guest asking
|
|
5
|
+
//! for a later operation can fall back.
|
|
5
6
|
//!
|
|
6
7
|
//! A failure lands on whoever is answerable for it, and always says why.
|
|
7
8
|
//! A trap the guest raises during a callback into it ends the invocation
|
|
@@ -12,13 +13,12 @@
|
|
|
12
13
|
|
|
13
14
|
use std::sync::Arc;
|
|
14
15
|
|
|
15
|
-
use kobako_transport::abi::pack_ptr_len;
|
|
16
16
|
use kobako_transport::envelope::{
|
|
17
|
-
Downcall, Fault, FaultKind,
|
|
17
|
+
CallReply, Downcall, Fault, FaultKind, Unsupported, Upcall, YieldReply,
|
|
18
18
|
};
|
|
19
19
|
|
|
20
20
|
use super::buffer::{
|
|
21
|
-
alloc_and_write, checked_payload_len,
|
|
21
|
+
alloc_and_write, checked_payload_len, lift_list, lower_list, read_buffer, ReadError, WriteError,
|
|
22
22
|
};
|
|
23
23
|
use super::{GuestInstance, RUNTIME_INCOMPATIBLE, SANDBOX_RUNTIME_NOT_KOBAKO};
|
|
24
24
|
use crate::dispatch::DispatchHandler;
|
|
@@ -62,8 +62,9 @@ impl<F> Calls<F> {
|
|
|
62
62
|
}
|
|
63
63
|
}
|
|
64
64
|
|
|
65
|
-
/// How
|
|
66
|
-
|
|
65
|
+
/// How a host import, the upcall or the Preamble's, ends an invocation
|
|
66
|
+
/// instead of answering.
|
|
67
|
+
pub enum ImportEnd<F> {
|
|
67
68
|
/// The guest trapped during a callback into it.
|
|
68
69
|
Fault(F),
|
|
69
70
|
/// The host gave the invocation up.
|
|
@@ -92,32 +93,55 @@ impl<G: GuestInstance> Yielder for FrameYielder<'_, G> {
|
|
|
92
93
|
}
|
|
93
94
|
}
|
|
94
95
|
|
|
95
|
-
/// Answer one `
|
|
96
|
-
///
|
|
96
|
+
/// Answer one `upcall` with the Reply, named in the return area at `ret`,
|
|
97
|
+
/// or what ends the invocation instead.
|
|
97
98
|
pub fn handle<G: GuestInstance>(
|
|
98
99
|
guest: &mut G,
|
|
99
100
|
req_ptr: i32,
|
|
100
101
|
req_len: i32,
|
|
101
|
-
|
|
102
|
+
ret: i32,
|
|
103
|
+
) -> Result<(), ImportEnd<G::Fault>> {
|
|
102
104
|
let reply = reply_to(guest, req_ptr, req_len)
|
|
103
|
-
.unwrap_or_else(|reason|
|
|
104
|
-
|
|
105
|
+
.unwrap_or_else(|reason| CallReply::Fault(Fault::new(FaultKind::Internal, reason)));
|
|
106
|
+
answer_import(guest, &fit_in_message(reply).encode(), ret)
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
/// Answer one `preamble` call with `preamble`, the Invocation's Preamble
|
|
110
|
+
/// bytes, named in the return area at `ret`. Each call writes the same
|
|
111
|
+
/// bytes afresh, so the guest may ask as often as it likes.
|
|
112
|
+
pub fn hand_preamble<G: GuestInstance>(
|
|
113
|
+
guest: &mut G,
|
|
114
|
+
preamble: &[u8],
|
|
115
|
+
ret: i32,
|
|
116
|
+
) -> Result<(), ImportEnd<G::Fault>> {
|
|
117
|
+
answer_import(guest, preamble, ret)
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
/// Name `answer` in the return area at `ret`, or end the invocation: a trap
|
|
121
|
+
/// a callback raised comes first, then a host that gave the invocation up,
|
|
122
|
+
/// then an answer that could not be written.
|
|
123
|
+
fn answer_import<G: GuestInstance>(
|
|
124
|
+
guest: &mut G,
|
|
125
|
+
answer: &[u8],
|
|
126
|
+
ret: i32,
|
|
127
|
+
) -> Result<(), ImportEnd<G::Fault>> {
|
|
128
|
+
let written = lower_from_callback(guest, answer, ret as u32);
|
|
105
129
|
if let Some(trap) = guest.calls().reentry_trap.take() {
|
|
106
|
-
return Err(
|
|
130
|
+
return Err(ImportEnd::Fault(trap));
|
|
107
131
|
}
|
|
108
132
|
if guest.calls().abandoned() {
|
|
109
|
-
return Err(
|
|
133
|
+
return Err(ImportEnd::Abandoned);
|
|
110
134
|
}
|
|
111
|
-
written.map_err(
|
|
135
|
+
written.map_err(ImportEnd::Unwritable)
|
|
112
136
|
}
|
|
113
137
|
|
|
114
138
|
/// An answer past the message cap is the Service's to change, as any other
|
|
115
139
|
/// answer the host cannot write is.
|
|
116
|
-
fn fit_in_message(reply:
|
|
140
|
+
fn fit_in_message(reply: CallReply) -> CallReply {
|
|
117
141
|
let bytes = reply.encode().len();
|
|
118
142
|
match checked_payload_len(bytes) {
|
|
119
143
|
Ok(_) => reply,
|
|
120
|
-
Err(limit) =>
|
|
144
|
+
Err(limit) => CallReply::Fault(Fault::new(
|
|
121
145
|
FaultKind::Runtime,
|
|
122
146
|
format!("Sandbox could not write the Service's answer: {limit}"),
|
|
123
147
|
)),
|
|
@@ -128,7 +152,7 @@ fn reply_to<G: GuestInstance>(
|
|
|
128
152
|
guest: &mut G,
|
|
129
153
|
req_ptr: i32,
|
|
130
154
|
req_len: i32,
|
|
131
|
-
) -> Result<
|
|
155
|
+
) -> Result<CallReply, &'static str> {
|
|
132
156
|
let req_bytes = read_request(guest, req_ptr, req_len)?;
|
|
133
157
|
// The endpoint decodes the Envelope, here and in `FrameYielder`, so
|
|
134
158
|
// the frontend never sees a frame; the payload inside it stays bytes
|
|
@@ -136,7 +160,7 @@ fn reply_to<G: GuestInstance>(
|
|
|
136
160
|
let call = match Upcall::decode(&req_bytes) {
|
|
137
161
|
Ok(Upcall::Call(call)) => call,
|
|
138
162
|
Ok(_) => {
|
|
139
|
-
return Ok(
|
|
163
|
+
return Ok(CallReply::Unsupported(Unsupported::new(
|
|
140
164
|
"the host does not serve this request",
|
|
141
165
|
)))
|
|
142
166
|
}
|
|
@@ -174,12 +198,6 @@ fn reply_to<G: GuestInstance>(
|
|
|
174
198
|
)
|
|
175
199
|
}
|
|
176
200
|
|
|
177
|
-
fn write_reply<G: GuestInstance>(guest: &mut G, bytes: &[u8]) -> Result<i64, &'static str> {
|
|
178
|
-
let ptr = write_from_callback(guest, bytes)?;
|
|
179
|
-
// The write has already held the length to the payload cap.
|
|
180
|
-
Ok(pack_ptr_len(ptr, bytes.len() as u32) as i64)
|
|
181
|
-
}
|
|
182
|
-
|
|
183
201
|
/// The request the guest hands the upcall import. Its `(ptr, len)` are
|
|
184
202
|
/// guest addresses, so they are read unsigned.
|
|
185
203
|
fn read_request<G: GuestInstance>(
|
|
@@ -205,13 +223,35 @@ fn keep_trap<G: GuestInstance>(
|
|
|
205
223
|
reason
|
|
206
224
|
}
|
|
207
225
|
|
|
208
|
-
/// Write `bytes` into the guest from inside a callback. A
|
|
209
|
-
/// already trapped is not called again.
|
|
226
|
+
/// Write `bytes` into the guest from inside a callback, answering where. A
|
|
227
|
+
/// guest that has already trapped is not called again.
|
|
210
228
|
fn write_from_callback<G: GuestInstance>(guest: &mut G, bytes: &[u8]) -> Result<u32, &'static str> {
|
|
211
|
-
|
|
212
|
-
|
|
229
|
+
refuse_after_trap(guest)?;
|
|
230
|
+
alloc_and_write(guest, bytes).map_err(|err| keep_write_failure(guest, err))
|
|
231
|
+
}
|
|
232
|
+
|
|
233
|
+
/// Write `bytes` into the guest from inside a callback and name them in the
|
|
234
|
+
/// return area at `ret`, as a host import answers a list.
|
|
235
|
+
fn lower_from_callback<G: GuestInstance>(
|
|
236
|
+
guest: &mut G,
|
|
237
|
+
bytes: &[u8],
|
|
238
|
+
ret: u32,
|
|
239
|
+
) -> Result<(), &'static str> {
|
|
240
|
+
refuse_after_trap(guest)?;
|
|
241
|
+
lower_list(guest, bytes, ret).map_err(|err| keep_write_failure(guest, err))
|
|
242
|
+
}
|
|
243
|
+
|
|
244
|
+
fn refuse_after_trap<G: GuestInstance>(guest: &mut G) -> Result<(), &'static str> {
|
|
245
|
+
match guest.calls().reentry_trap {
|
|
246
|
+
Some(_) => Err("the Sandbox already trapped during this invocation"),
|
|
247
|
+
None => Ok(()),
|
|
213
248
|
}
|
|
214
|
-
|
|
249
|
+
}
|
|
250
|
+
|
|
251
|
+
/// Keep a trap a failed write raised, as `keep_trap` does, and answer why
|
|
252
|
+
/// the write failed.
|
|
253
|
+
fn keep_write_failure<G: GuestInstance>(guest: &mut G, err: WriteError<G::Fault>) -> &'static str {
|
|
254
|
+
match err {
|
|
215
255
|
WriteError::TooLarge => "payload exceeds the 16 MiB limit",
|
|
216
256
|
WriteError::Incompatible(message) => message,
|
|
217
257
|
WriteError::Trapped(trap) => keep_trap(
|
|
@@ -219,9 +259,8 @@ fn write_from_callback<G: GuestInstance>(guest: &mut G, bytes: &[u8]) -> Result<
|
|
|
219
259
|
trap,
|
|
220
260
|
"the Sandbox trapped while allocating memory for the request",
|
|
221
261
|
),
|
|
222
|
-
WriteError::OutOfMemory => "the Sandbox ran out of memory while preparing the request",
|
|
223
262
|
WriteError::OutOfBounds => "could not write the request into the Sandbox's memory",
|
|
224
|
-
}
|
|
263
|
+
}
|
|
225
264
|
}
|
|
226
265
|
|
|
227
266
|
fn drive_yield<G: GuestInstance>(
|
|
@@ -237,17 +276,17 @@ fn drive_yield<G: GuestInstance>(
|
|
|
237
276
|
let len_i32 = checked_payload_len(request.len())?;
|
|
238
277
|
let req_ptr = write_from_callback(guest, &request)? as i32;
|
|
239
278
|
|
|
240
|
-
let
|
|
279
|
+
let ret = guest
|
|
241
280
|
.downcall(req_ptr, len_i32)
|
|
242
281
|
.ok_or(RUNTIME_INCOMPATIBLE)?
|
|
243
282
|
.map_err(|trap| keep_trap(guest, trap, "the Sandbox trapped while invoking a block"))?;
|
|
244
|
-
let (reply_ptr, reply_len) = unpack_guest_buffer(packed);
|
|
245
|
-
if reply_len == 0 {
|
|
246
|
-
return Err("the Sandbox returned an empty block result");
|
|
247
|
-
}
|
|
248
283
|
let memory = guest.memory().ok_or(SANDBOX_RUNTIME_NOT_KOBAKO)?;
|
|
249
|
-
|
|
284
|
+
let reply = lift_list(memory, ret).map_err(|err| match err {
|
|
250
285
|
ReadError::TooLarge => "block result payload exceeds the 16 MiB limit",
|
|
251
286
|
ReadError::OutOfBounds => "the Sandbox returned an out-of-bounds block result",
|
|
252
|
-
})
|
|
287
|
+
})?;
|
|
288
|
+
if reply.is_empty() {
|
|
289
|
+
return Err("the Sandbox returned an empty block result");
|
|
290
|
+
}
|
|
291
|
+
Ok(reply)
|
|
253
292
|
}
|
|
@@ -1,27 +1,21 @@
|
|
|
1
|
-
//! Per-invocation I/O between the host and the guest: it
|
|
2
|
-
//!
|
|
3
|
-
//!
|
|
1
|
+
//! Per-invocation I/O between the host and the guest: it encodes the Preamble
|
|
2
|
+
//! the guest takes, writes the entry's downcall into room the guest gives, and
|
|
3
|
+
//! reads the Outcome the downcall answered with.
|
|
4
4
|
|
|
5
|
-
use kobako_transport::abi::FRAME_LEN_SIZE;
|
|
6
5
|
use kobako_transport::envelope::{CompileReply, Downcall, Outcome, Preamble, Run, Snippet};
|
|
7
6
|
|
|
8
|
-
use super::buffer::{
|
|
9
|
-
alloc_and_write, checked_payload_len, read_buffer, unpack_guest_buffer, ReadError, WriteError,
|
|
10
|
-
};
|
|
7
|
+
use super::buffer::{alloc_and_write, checked_payload_len, lift_list, ReadError, WriteError};
|
|
11
8
|
use super::{GuestInstance, SANDBOX_RUNTIME_NOT_KOBAKO};
|
|
12
|
-
use crate::error::{InvokeError,
|
|
9
|
+
use crate::error::{InvokeError, Trap};
|
|
13
10
|
use crate::runtime::Entry;
|
|
14
11
|
|
|
15
|
-
///
|
|
16
|
-
///
|
|
17
|
-
///
|
|
18
|
-
pub fn
|
|
19
|
-
let
|
|
20
|
-
checked_payload_len(
|
|
21
|
-
|
|
22
|
-
content.extend_from_slice(&(preamble.len() as u32).to_be_bytes());
|
|
23
|
-
content.extend_from_slice(&preamble);
|
|
24
|
-
Ok(content)
|
|
12
|
+
/// The bytes the host hands the guest through its `preamble` import, held to
|
|
13
|
+
/// the 16 MiB cap every message carries, so an Invocation whose Preamble is
|
|
14
|
+
/// past it fails before anything runs.
|
|
15
|
+
pub fn preamble_bytes(preamble: &Preamble) -> Result<Vec<u8>, &'static str> {
|
|
16
|
+
let bytes = preamble.encode();
|
|
17
|
+
checked_payload_len(bytes.len())?;
|
|
18
|
+
Ok(bytes)
|
|
25
19
|
}
|
|
26
20
|
|
|
27
21
|
/// The downcall `entry` is.
|
|
@@ -44,22 +38,18 @@ pub fn compile_request() -> Vec<u8> {
|
|
|
44
38
|
Downcall::Compile.encode()
|
|
45
39
|
}
|
|
46
40
|
|
|
47
|
-
/// Write a request into
|
|
48
|
-
/// allocator
|
|
49
|
-
/// the runtime intact, so it is a `SetupError`.
|
|
41
|
+
/// Write a request into room the guest gives, answering where. A missing
|
|
42
|
+
/// allocator, or one that traps for want of room, is a `Trap`.
|
|
50
43
|
pub fn write_request<G: GuestInstance>(
|
|
51
44
|
guest: &mut G,
|
|
52
45
|
envelope: &[u8],
|
|
53
46
|
) -> Result<(i32, i32), InvokeError> {
|
|
54
47
|
let trap = |message: String| -> InvokeError { Trap::Other(message).into() };
|
|
55
48
|
let ptr = alloc_and_write(guest, envelope).map_err(|err| match err {
|
|
56
|
-
WriteError::OutOfMemory => {
|
|
57
|
-
SetupError::Intact("could not allocate input buffer (out of memory)".to_string()).into()
|
|
58
|
-
}
|
|
59
49
|
WriteError::TooLarge => trap("payload exceeds the 16 MiB limit".to_string()),
|
|
60
50
|
WriteError::Incompatible(message) => trap(message.to_string()),
|
|
61
|
-
WriteError::Trapped(e) => G::
|
|
62
|
-
.unwrap_or_else(|| Trap::Other(format!("
|
|
51
|
+
WriteError::Trapped(e) => G::cap_trap_of(&e)
|
|
52
|
+
.unwrap_or_else(|| Trap::Other(format!("could not allocate input buffer: {e}")))
|
|
63
53
|
.into(),
|
|
64
54
|
WriteError::OutOfBounds => {
|
|
65
55
|
trap("the input buffer lies outside the Sandbox's memory".to_string())
|
|
@@ -72,8 +62,8 @@ pub fn write_request<G: GuestInstance>(
|
|
|
72
62
|
/// A guest that answered without an Outcome the envelope can frame — an
|
|
73
63
|
/// empty answer included — has nothing left to attribute, so it is a
|
|
74
64
|
/// `Trap` like every other way of failing to read the result.
|
|
75
|
-
pub fn read_outcome<G: GuestInstance>(guest: &mut G,
|
|
76
|
-
let bytes = read_answer(guest,
|
|
65
|
+
pub fn read_outcome<G: GuestInstance>(guest: &mut G, ret: u32) -> Result<Outcome, Trap> {
|
|
66
|
+
let bytes = read_answer(guest, ret)?;
|
|
77
67
|
Outcome::decode(&bytes).map_err(|_| {
|
|
78
68
|
Trap::Other(
|
|
79
69
|
if bytes.is_empty() {
|
|
@@ -88,21 +78,18 @@ pub fn read_outcome<G: GuestInstance>(guest: &mut G, packed: u64) -> Result<Outc
|
|
|
88
78
|
|
|
89
79
|
/// Held to the same 16 MiB bound as an Outcome; an answer that does not
|
|
90
80
|
/// frame is a `Trap` the caller treats like any other failed compile.
|
|
91
|
-
pub fn read_compile_reply<G: GuestInstance>(
|
|
92
|
-
|
|
93
|
-
packed: u64,
|
|
94
|
-
) -> Result<CompileReply, Trap> {
|
|
95
|
-
let bytes = read_answer(guest, packed)?;
|
|
81
|
+
pub fn read_compile_reply<G: GuestInstance>(guest: &mut G, ret: u32) -> Result<CompileReply, Trap> {
|
|
82
|
+
let bytes = read_answer(guest, ret)?;
|
|
96
83
|
CompileReply::decode(&bytes)
|
|
97
84
|
.map_err(|_| Trap::Other("Sandbox produced an unrecognised compile answer".to_string()))
|
|
98
85
|
}
|
|
99
86
|
|
|
100
|
-
|
|
87
|
+
/// The answer the return area at `ret` names.
|
|
88
|
+
fn read_answer<G: GuestInstance>(guest: &mut G, ret: u32) -> Result<Vec<u8>, Trap> {
|
|
101
89
|
let memory = guest
|
|
102
90
|
.memory()
|
|
103
91
|
.ok_or_else(|| Trap::Other(SANDBOX_RUNTIME_NOT_KOBAKO.to_string()))?;
|
|
104
|
-
|
|
105
|
-
read_buffer(memory, ptr, len).map_err(|err| {
|
|
92
|
+
lift_list(memory, ret).map_err(|err| {
|
|
106
93
|
Trap::Other(
|
|
107
94
|
match err {
|
|
108
95
|
ReadError::TooLarge => "result payload exceeds the 16 MiB limit",
|