kobako 0.29.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 +552 -10
- data/Cargo.lock +595 -139
- data/README.md +126 -564
- data/ROADMAP.md +3 -2
- data/crates/kobako-runtime/CHANGELOG.md +488 -1
- data/crates/kobako-runtime/Cargo.toml +73 -15
- data/crates/kobako-runtime/README.md +11 -4
- data/crates/kobako-runtime/src/cred.rs +124 -0
- data/crates/kobako-runtime/src/dispatch.rs +5 -6
- data/crates/kobako-runtime/src/endpoint/buffer.rs +301 -0
- data/crates/kobako-runtime/src/endpoint/dispatch.rs +292 -0
- data/crates/kobako-runtime/src/endpoint/invoke.rs +181 -0
- data/crates/kobako-runtime/src/endpoint.rs +267 -0
- 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 +82 -0
- data/crates/kobako-runtime/src/filesystem/bind/lookup.rs +274 -0
- data/crates/kobako-runtime/src/filesystem/bind/walk.rs +248 -0
- data/crates/kobako-runtime/src/filesystem/bind.rs +2857 -0
- data/crates/kobako-runtime/src/filesystem/overlay/file.rs +89 -0
- data/crates/kobako-runtime/src/filesystem/overlay/inode.rs +106 -0
- data/crates/kobako-runtime/src/filesystem/overlay/path.rs +263 -0
- data/crates/kobako-runtime/src/filesystem/overlay/stack/copy_up.rs +128 -0
- data/crates/kobako-runtime/src/filesystem/overlay/stack/dir.rs +238 -0
- data/crates/kobako-runtime/src/filesystem/overlay/stack/namei.rs +135 -0
- data/crates/kobako-runtime/src/filesystem/overlay/stack/readdir.rs +54 -0
- data/crates/kobako-runtime/src/filesystem/overlay/stack.rs +316 -739
- data/crates/kobako-runtime/src/filesystem/overlay.rs +2627 -1154
- data/crates/kobako-runtime/src/filesystem/pipe.rs +224 -0
- data/crates/kobako-runtime/src/filesystem/rdir.rs +84 -0
- data/crates/kobako-runtime/src/filesystem/tmpfs/seed.rs +32 -0
- data/crates/kobako-runtime/src/filesystem/tmpfs/tmpfile.rs +32 -0
- data/crates/kobako-runtime/src/filesystem/tmpfs/whiteout.rs +107 -0
- data/crates/kobako-runtime/src/filesystem/tmpfs/xattr.rs +22 -0
- data/crates/kobako-runtime/src/filesystem/tmpfs.rs +1997 -0
- data/crates/kobako-runtime/src/filesystem/wrap.rs +208 -0
- data/crates/kobako-runtime/src/filesystem.rs +25 -388
- 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 +46 -0
- data/crates/kobako-runtime/src/lib.rs +27 -11
- data/crates/kobako-runtime/src/limits.rs +195 -0
- data/crates/kobako-runtime/src/locking/seqlock.rs +66 -0
- data/crates/kobako-runtime/src/locking.rs +228 -0
- data/crates/kobako-runtime/src/memcg.rs +383 -0
- data/crates/kobako-runtime/src/mount.rs +86 -101
- 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 +37 -16
- data/crates/kobako-runtime/src/runtime.rs +3 -5
- data/crates/kobako-runtime/src/sched.rs +311 -0
- data/crates/kobako-runtime/src/security/entry_policy.rs +22 -0
- data/crates/kobako-runtime/src/security/hooks.rs +242 -0
- data/crates/kobako-runtime/src/security/mount_policy.rs +158 -0
- data/crates/kobako-runtime/src/security.rs +601 -0
- data/crates/kobako-runtime/src/snapshot.rs +121 -2
- data/crates/kobako-runtime/src/vfs/attr.rs +162 -0
- data/crates/kobako-runtime/src/vfs/dentry.rs +1466 -0
- data/crates/kobako-runtime/src/vfs/file.rs +1911 -0
- data/crates/kobako-runtime/src/vfs/inode.rs +425 -0
- data/crates/kobako-runtime/src/vfs/kern_path.rs +213 -0
- data/crates/kobako-runtime/src/vfs/mount.rs +60 -0
- data/crates/kobako-runtime/src/vfs/namei.rs +5837 -0
- 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 +116 -0
- data/crates/kobako-runtime/src/vfs/path.rs +64 -0
- data/crates/kobako-runtime/src/vfs/readdir.rs +163 -0
- data/crates/kobako-runtime/src/vfs/stat.rs +293 -0
- data/crates/kobako-runtime/src/vfs/super_block.rs +126 -0
- data/crates/kobako-runtime/src/vfs/utimes.rs +299 -0
- data/crates/kobako-runtime/src/vfs.rs +63 -0
- data/crates/kobako-runtime/src/wait.rs +356 -0
- data/crates/kobako-transport/CHANGELOG.md +39 -2
- data/crates/kobako-transport/Cargo.toml +3 -3
- data/crates/kobako-transport/README.md +8 -8
- data/crates/kobako-transport/src/abi.rs +78 -72
- data/crates/kobako-transport/src/envelope/bytes.rs +49 -30
- data/crates/kobako-transport/src/envelope/call.rs +37 -9
- data/crates/kobako-transport/src/envelope/compile_reply.rs +2 -2
- data/crates/kobako-transport/src/envelope/error_record.rs +24 -3
- data/crates/kobako-transport/src/envelope/fault.rs +2 -2
- data/crates/kobako-transport/src/envelope/name.rs +99 -0
- data/crates/kobako-transport/src/envelope/outcome.rs +11 -11
- data/crates/kobako-transport/src/envelope/preamble.rs +34 -31
- data/crates/kobako-transport/src/envelope/reply.rs +29 -29
- data/crates/kobako-transport/src/envelope/request.rs +2 -2
- data/crates/kobako-transport/src/envelope/run.rs +1 -1
- data/crates/kobako-transport/src/envelope/unsupported.rs +12 -12
- data/crates/kobako-transport/src/envelope.rs +15 -10
- data/crates/kobako-wasmtime/CHANGELOG.md +315 -8
- data/crates/kobako-wasmtime/Cargo.toml +35 -15
- data/crates/kobako-wasmtime/README.md +11 -6
- data/crates/kobako-wasmtime/build/preview2.rs +1339 -0
- data/crates/kobako-wasmtime/build.rs +16 -0
- data/crates/kobako-wasmtime/src/abi.rs +19 -47
- data/crates/kobako-wasmtime/src/ambient.rs +33 -19
- data/crates/kobako-wasmtime/src/cache.rs +28 -20
- data/crates/kobako-wasmtime/src/config.rs +59 -60
- data/crates/kobako-wasmtime/src/driver.rs +63 -145
- data/crates/kobako-wasmtime/src/exports.rs +147 -32
- data/crates/kobako-wasmtime/src/guest.rs +44 -0
- data/crates/kobako-wasmtime/src/guest_binary.rs +14 -0
- data/crates/kobako-wasmtime/src/imports.rs +48 -18
- data/crates/kobako-wasmtime/src/instance_pre.rs +7 -7
- data/crates/kobako-wasmtime/src/invocation.rs +93 -51
- data/crates/kobako-wasmtime/src/lib.rs +6 -7
- data/crates/kobako-wasmtime/src/limiter.rs +52 -211
- data/crates/kobako-wasmtime/src/preview1/descriptors.rs +309 -0
- data/crates/kobako-wasmtime/src/preview1/differential/mount.rs +70 -57
- data/crates/kobako-wasmtime/src/preview1/differential.rs +137 -27
- data/crates/kobako-wasmtime/src/preview1/errno.rs +90 -233
- data/crates/kobako-wasmtime/src/preview1/fd.rs +1301 -522
- data/crates/kobako-wasmtime/src/preview1/memory.rs +126 -150
- data/crates/kobako-wasmtime/src/preview1/mount_view/wrap.rs +192 -0
- data/crates/kobako-wasmtime/src/preview1/mount_view.rs +1989 -708
- data/crates/kobako-wasmtime/src/preview1/path.rs +181 -365
- data/crates/kobako-wasmtime/src/preview1/poll.rs +345 -173
- data/crates/kobako-wasmtime/src/preview1/sock.rs +10 -25
- data/crates/kobako-wasmtime/src/preview1/testsuite-expected.txt +6 -0
- data/crates/kobako-wasmtime/src/preview1/testsuite.rs +147 -0
- data/crates/kobako-wasmtime/src/preview1.rs +471 -416
- 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 +31 -16
- data/crates/kobako-wasmtime/src/wasi.rs +238 -93
- 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/typenames.witx +750 -0
- data/crates/kobako-wasmtime/witx/wasi_snapshot_preview1.witx +441 -0
- data/data/kobako.wasm +0 -0
- data/ext/kobako/Cargo.toml +1 -1
- data/ext/kobako/src/{runtime/errors.rs → errors.rs} +35 -27
- data/ext/kobako/src/escape.rs +98 -0
- data/ext/kobako/src/{runtime/filesystem → filesystem}/grants.rs +36 -16
- data/ext/kobako/src/filesystem/ruby.rs +689 -0
- data/ext/kobako/src/filesystem.rs +217 -0
- 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 +31 -30
- data/ext/kobako/src/runtime/snapshot.rs +5 -5
- data/ext/kobako/src/runtime.rs +221 -138
- data/ext/kobako/src/wire.rs +42 -0
- data/lib/kobako/catalog/extensions.rb +19 -7
- data/lib/kobako/catalog/flags.rb +4 -6
- data/lib/kobako/catalog/grants.rb +5 -4
- data/lib/kobako/catalog/handles.rb +9 -31
- data/lib/kobako/catalog/mount.rb +12 -12
- data/lib/kobako/catalog/network.rb +35 -0
- data/lib/kobako/catalog/replacements.rb +28 -0
- data/lib/kobako/catalog/services.rb +7 -21
- data/lib/kobako/catalog/snippets.rb +10 -25
- data/lib/kobako/catalog.rb +4 -1
- data/lib/kobako/codec/{utils.rb → boundary.rb} +5 -3
- data/lib/kobako/codec/decoder.rb +12 -12
- data/lib/kobako/codec/ext_types.rb +5 -7
- data/lib/kobako/codec/handle_walk.rb +5 -47
- data/lib/kobako/codec/nesting.rb +7 -7
- data/lib/kobako/codec/value_budget.rb +134 -42
- data/lib/kobako/codec.rb +1 -1
- data/lib/kobako/context.rb +28 -15
- data/lib/kobako/errors.rb +15 -12
- data/lib/kobako/extension.rb +3 -2
- data/lib/kobako/filesystem.rb +53 -15
- 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/payload/arguments.rb +2 -2
- data/lib/kobako/pool.rb +47 -25
- data/lib/kobako/runtime.rb +3 -2
- data/lib/kobako/sandbox.rb +113 -56
- data/lib/kobako/sandbox_options.rb +41 -107
- data/lib/kobako/snippet/bytecode.rb +1 -1
- data/lib/kobako/snippet/source.rb +1 -1
- data/lib/kobako/transport/argument_wrap.rb +64 -0
- data/lib/kobako/transport/block_failure.rb +1 -1
- data/lib/kobako/transport/dispatcher.rb +10 -10
- data/lib/kobako/transport/exposure.rb +28 -17
- data/lib/kobako/transport/reflection.rb +28 -11
- data/lib/kobako/transport/run.rb +13 -28
- data/lib/kobako/transport/yielder.rb +3 -10
- data/lib/kobako/transport.rb +1 -0
- data/lib/kobako/version.rb +1 -1
- data/lib/kobako.rb +38 -0
- data/release-please-config.json +16 -0
- data/sig/core/thread.rbs +6 -0
- data/sig/kobako/catalog/extensions.rbs +5 -3
- data/sig/kobako/catalog/grants.rbs +7 -4
- data/sig/kobako/catalog/handles.rbs +2 -4
- data/sig/kobako/catalog/mount.rbs +3 -5
- data/sig/kobako/catalog/network.rbs +20 -0
- data/sig/kobako/catalog/replacements.rbs +14 -0
- data/sig/kobako/catalog/services.rbs +1 -5
- data/sig/kobako/catalog/snippets.rbs +3 -5
- data/sig/kobako/catalog.rbs +1 -1
- data/sig/kobako/codec/boundary.rbs +9 -0
- data/sig/kobako/codec/decoder.rbs +1 -1
- data/sig/kobako/codec/handle_walk.rbs +0 -6
- data/sig/kobako/codec/nesting.rbs +2 -2
- data/sig/kobako/codec/value_budget.rbs +48 -5
- data/sig/kobako/codec.rbs +1 -2
- data/sig/kobako/context.rbs +4 -2
- data/sig/kobako/errors.rbs +4 -4
- data/sig/kobako/filesystem.rbs +38 -15
- data/sig/kobako/guest_yielder.rbs +1 -1
- data/sig/kobako/network.rbs +115 -0
- data/sig/kobako/outcome.rbs +1 -1
- data/sig/kobako/payload/arguments.rbs +1 -1
- data/sig/kobako/pool.rbs +8 -4
- data/sig/kobako/runtime.rbs +32 -3
- data/sig/kobako/sandbox.rbs +22 -7
- data/sig/kobako/sandbox_options.rbs +29 -38
- data/sig/kobako/transport/argument_wrap.rbs +15 -0
- data/sig/kobako/transport/dispatcher.rbs +2 -2
- data/sig/kobako/transport/error.rbs +1 -1
- data/sig/kobako/transport/exposure.rbs +3 -5
- data/sig/kobako/transport/reflection.rbs +12 -2
- data/sig/kobako/transport/run.rbs +3 -5
- data/sig/kobako/transport/yielder.rbs +0 -3
- data/sig/kobako/transport.rbs +2 -2
- data/sig/kobako.rbs +13 -0
- metadata +131 -34
- data/crates/kobako-runtime/src/filesystem/directory/spelling.rs +0 -194
- data/crates/kobako-runtime/src/filesystem/directory.rs +0 -1082
- data/crates/kobako-runtime/src/filesystem/memory.rs +0 -1100
- data/crates/kobako-runtime/src/filesystem/overlay/exclusive.rs +0 -88
- data/crates/kobako-runtime/src/filesystem/path.rs +0 -60
- data/crates/kobako-runtime/src/filesystem/temporary.rs +0 -167
- data/crates/kobako-wasmtime/src/capture.rs +0 -96
- data/crates/kobako-wasmtime/src/descriptors.rs +0 -167
- data/crates/kobako-wasmtime/src/dispatch.rs +0 -125
- data/crates/kobako-wasmtime/src/guest_mem.rs +0 -271
- data/crates/kobako-wasmtime/src/io.rs +0 -89
- data/crates/kobako-wasmtime/src/preview1/args.rs +0 -60
- data/crates/kobako-wasmtime/src/preview1/beneath.rs +0 -188
- data/crates/kobako-wasmtime/tests/compile_posture.rs +0 -43
- data/crates/kobako-wasmtime/tests/guest_file.rs +0 -1054
- data/crates/kobako-wasmtime/tests/preamble_flags.rs +0 -70
- data/crates/kobako-wasmtime/tests/snippet_compile.rs +0 -95
- data/crates/kobako-wasmtime/tests/support/mod.rs +0 -30
- data/crates/kobako-wasmtime/tests/unanswered_call.rs +0 -86
- data/crates/kobako-wasmtime/tests/unsupported_call.rs +0 -51
- data/ext/kobako/src/runtime/escape.rs +0 -53
- data/ext/kobako/src/runtime/filesystem/ruby.rs +0 -400
- data/ext/kobako/src/runtime/filesystem.rs +0 -236
- data/ext/kobako/src/runtime/gvl.rs +0 -112
- data/lib/kobako/constant_name.rb +0 -8
- data/sig/kobako/codec/utils.rbs +0 -9
- data/sig/kobako/constant_name.rbs +0 -3
- /data/ext/kobako/src/{runtime/pinned_value.rs → pinned_value.rs} +0 -0
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
//! Byte primitives for the
|
|
1
|
+
//! Byte primitives for the Envelope: unsigned big-endian integers,
|
|
2
2
|
//! length-prefixed byte strings, counted lists, and the trailing remainder.
|
|
3
3
|
//!
|
|
4
4
|
//! `Reader` borrows rather than copies, so a decoded envelope's payload is
|
|
@@ -7,7 +7,7 @@
|
|
|
7
7
|
|
|
8
8
|
use super::DecodeError;
|
|
9
9
|
|
|
10
|
-
const MALFORMED: DecodeError = DecodeError::new("
|
|
10
|
+
const MALFORMED: DecodeError = DecodeError::new("Envelope ended before its declared length");
|
|
11
11
|
|
|
12
12
|
/// Cursor over one message. Every read either advances past a complete
|
|
13
13
|
/// field or fails, so a truncated message can never be mistaken for a
|
|
@@ -22,13 +22,13 @@ impl<'a> Reader<'a> {
|
|
|
22
22
|
Reader { bytes, pos: 0 }
|
|
23
23
|
}
|
|
24
24
|
|
|
25
|
-
pub(crate) fn
|
|
25
|
+
pub(crate) fn read_u8(&mut self) -> Result<u8, DecodeError> {
|
|
26
26
|
let byte = *self.bytes.get(self.pos).ok_or(MALFORMED)?;
|
|
27
27
|
self.pos += 1;
|
|
28
28
|
Ok(byte)
|
|
29
29
|
}
|
|
30
30
|
|
|
31
|
-
pub(crate) fn
|
|
31
|
+
pub(crate) fn read_u32(&mut self) -> Result<u32, DecodeError> {
|
|
32
32
|
let end = self.pos.checked_add(4).ok_or(MALFORMED)?;
|
|
33
33
|
let slice = self.bytes.get(self.pos..end).ok_or(MALFORMED)?;
|
|
34
34
|
self.pos = end;
|
|
@@ -37,8 +37,8 @@ impl<'a> Reader<'a> {
|
|
|
37
37
|
}
|
|
38
38
|
|
|
39
39
|
/// A `u32` length followed by that many bytes.
|
|
40
|
-
pub(crate) fn
|
|
41
|
-
let len = self.
|
|
40
|
+
pub(crate) fn read_bytes(&mut self) -> Result<&'a [u8], DecodeError> {
|
|
41
|
+
let len = self.read_u32()? as usize;
|
|
42
42
|
let end = self.pos.checked_add(len).ok_or(MALFORMED)?;
|
|
43
43
|
let slice = self.bytes.get(self.pos..end).ok_or(MALFORMED)?;
|
|
44
44
|
self.pos = end;
|
|
@@ -48,9 +48,9 @@ impl<'a> Reader<'a> {
|
|
|
48
48
|
/// A byte string carrying UTF-8. Every text field in the envelope is
|
|
49
49
|
/// validated at decode so a frontend receives `&str` without repeating
|
|
50
50
|
/// the check.
|
|
51
|
-
pub(crate) fn
|
|
52
|
-
core::str::from_utf8(self.
|
|
53
|
-
.map_err(|_| DecodeError::new("
|
|
51
|
+
pub(crate) fn read_text(&mut self) -> Result<&'a str, DecodeError> {
|
|
52
|
+
core::str::from_utf8(self.read_bytes()?)
|
|
53
|
+
.map_err(|_| DecodeError::new("Envelope text field is not valid UTF-8"))
|
|
54
54
|
}
|
|
55
55
|
|
|
56
56
|
/// A `u32` count followed by that many byte strings, each carrying
|
|
@@ -58,26 +58,37 @@ impl<'a> Reader<'a> {
|
|
|
58
58
|
///
|
|
59
59
|
/// The elements are copied rather than borrowed: a binding's path and
|
|
60
60
|
/// a panic's `available` names outlive the frame buffer they arrive in.
|
|
61
|
-
pub(crate) fn
|
|
62
|
-
self.
|
|
61
|
+
pub(crate) fn read_text_list(&mut self) -> Result<Vec<String>, DecodeError> {
|
|
62
|
+
self.read_list(|reader| Ok(reader.read_text()?.to_owned()))
|
|
63
63
|
}
|
|
64
64
|
|
|
65
65
|
/// A `u32` count followed by that many byte strings of any content,
|
|
66
|
-
/// copied for the same reason as `
|
|
67
|
-
pub(crate) fn
|
|
68
|
-
self.
|
|
66
|
+
/// copied for the same reason as `read_text_list`.
|
|
67
|
+
pub(crate) fn read_bytes_list(&mut self) -> Result<Vec<Vec<u8>>, DecodeError> {
|
|
68
|
+
self.read_list(|reader| Ok(reader.read_bytes()?.to_vec()))
|
|
69
69
|
}
|
|
70
70
|
|
|
71
|
-
|
|
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
|
+
|
|
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)?);
|
|
@@ -106,7 +117,7 @@ impl<'a> Reader<'a> {
|
|
|
106
117
|
Ok(())
|
|
107
118
|
} else {
|
|
108
119
|
Err(DecodeError::new(
|
|
109
|
-
"
|
|
120
|
+
"Envelope carries bytes past its last field",
|
|
110
121
|
))
|
|
111
122
|
}
|
|
112
123
|
}
|
|
@@ -175,15 +186,23 @@ mod tests {
|
|
|
175
186
|
let encoded = w.into_bytes();
|
|
176
187
|
|
|
177
188
|
let mut r = Reader::new(&encoded);
|
|
178
|
-
assert_eq!(r.u8(), Ok(7), "a u8 must round-trip through the writer");
|
|
179
|
-
assert_eq!(r.u32(), Ok(0xdead_beef), "a u32 must round-trip big-endian");
|
|
180
189
|
assert_eq!(
|
|
181
|
-
r.
|
|
190
|
+
r.read_u8(),
|
|
191
|
+
Ok(7),
|
|
192
|
+
"a u8 must round-trip through the writer"
|
|
193
|
+
);
|
|
194
|
+
assert_eq!(
|
|
195
|
+
r.read_u32(),
|
|
196
|
+
Ok(0xdead_beef),
|
|
197
|
+
"a u32 must round-trip big-endian"
|
|
198
|
+
);
|
|
199
|
+
assert_eq!(
|
|
200
|
+
r.read_bytes(),
|
|
182
201
|
Ok(&b"kv"[..]),
|
|
183
202
|
"a length-prefixed byte string must round-trip"
|
|
184
203
|
);
|
|
185
204
|
assert_eq!(
|
|
186
|
-
r.
|
|
205
|
+
r.read_text_list(),
|
|
187
206
|
Ok(vec!["a".to_owned(), "bc".to_owned()]),
|
|
188
207
|
"a counted list must round-trip element for element"
|
|
189
208
|
);
|
|
@@ -213,7 +232,7 @@ mod tests {
|
|
|
213
232
|
let bytes = [0, 0, 0, 8, b'h', b'i'];
|
|
214
233
|
let mut r = Reader::new(&bytes);
|
|
215
234
|
assert!(
|
|
216
|
-
r.
|
|
235
|
+
r.read_bytes().is_err(),
|
|
217
236
|
"a byte string whose length overruns the message must be rejected, not truncated"
|
|
218
237
|
);
|
|
219
238
|
}
|
|
@@ -225,7 +244,7 @@ mod tests {
|
|
|
225
244
|
let bytes = [0xff, 0xff, 0xff, 0xff];
|
|
226
245
|
let mut r = Reader::new(&bytes);
|
|
227
246
|
assert!(
|
|
228
|
-
r.
|
|
247
|
+
r.read_text_list().is_err(),
|
|
229
248
|
"a list count the message cannot satisfy must be rejected before any allocation"
|
|
230
249
|
);
|
|
231
250
|
}
|
|
@@ -238,7 +257,7 @@ mod tests {
|
|
|
238
257
|
let encoded = w.into_bytes();
|
|
239
258
|
let mut r = Reader::new(&encoded);
|
|
240
259
|
assert!(
|
|
241
|
-
r.
|
|
260
|
+
r.read_text().is_err(),
|
|
242
261
|
"a text field carrying non-UTF-8 bytes must be rejected at decode"
|
|
243
262
|
);
|
|
244
263
|
}
|
|
@@ -250,7 +269,7 @@ mod tests {
|
|
|
250
269
|
w.bytes(b"ok").remainder(b"!");
|
|
251
270
|
let encoded = w.into_bytes();
|
|
252
271
|
let mut r = Reader::new(&encoded);
|
|
253
|
-
r.
|
|
272
|
+
r.read_bytes().unwrap();
|
|
254
273
|
assert!(
|
|
255
274
|
r.finish().is_err(),
|
|
256
275
|
"bytes past an envelope's last field must fail loudly as a framing desync"
|
|
@@ -13,6 +13,10 @@ use super::DecodeError;
|
|
|
13
13
|
const KIND_PATH: u8 = 0;
|
|
14
14
|
const KIND_HANDLE: u8 = 1;
|
|
15
15
|
|
|
16
|
+
/// The most a Capability Handle id may be: the wire pins ids to the positive
|
|
17
|
+
/// i32 range, so no table issues one past it and no side takes one.
|
|
18
|
+
pub const MAX_HANDLE_ID: u32 = 0x7fff_ffff;
|
|
19
|
+
|
|
16
20
|
/// The object a Call is addressed to.
|
|
17
21
|
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
|
18
22
|
pub enum Target<'a> {
|
|
@@ -36,18 +40,26 @@ pub struct Call<'a> {
|
|
|
36
40
|
impl<'a> Call<'a> {
|
|
37
41
|
pub fn decode(bytes: &'a [u8]) -> Result<Self, DecodeError> {
|
|
38
42
|
let mut reader = Reader::new(bytes);
|
|
39
|
-
let target = match reader.
|
|
40
|
-
KIND_PATH => Target::Path(reader.
|
|
41
|
-
KIND_HANDLE => Target::Handle(reader.
|
|
43
|
+
let target = match reader.read_u8()? {
|
|
44
|
+
KIND_PATH => Target::Path(reader.read_text()?),
|
|
45
|
+
KIND_HANDLE => Target::Handle(reader.read_u32()?),
|
|
42
46
|
_ => return Err(DecodeError::new("Call kind must be 0 (path) or 1 (handle)")),
|
|
43
47
|
};
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
+
match target {
|
|
49
|
+
Target::Handle(0) => {
|
|
50
|
+
return Err(DecodeError::new(
|
|
51
|
+
"Call target Handle id 0 is the invalid sentinel",
|
|
52
|
+
))
|
|
53
|
+
}
|
|
54
|
+
Target::Handle(id) if id > MAX_HANDLE_ID => {
|
|
55
|
+
return Err(DecodeError::new(
|
|
56
|
+
"Call target Handle id must be at most 0x7fff_ffff",
|
|
57
|
+
))
|
|
58
|
+
}
|
|
59
|
+
_ => {}
|
|
48
60
|
}
|
|
49
|
-
let method = reader.
|
|
50
|
-
let block_given = match reader.
|
|
61
|
+
let method = reader.read_text()?;
|
|
62
|
+
let block_given = match reader.read_u8()? {
|
|
51
63
|
0 => false,
|
|
52
64
|
1 => true,
|
|
53
65
|
_ => return Err(DecodeError::new("Call block_given must be 0 or 1")),
|
|
@@ -205,6 +217,22 @@ mod tests {
|
|
|
205
217
|
);
|
|
206
218
|
}
|
|
207
219
|
|
|
220
|
+
// @behavior WE-091
|
|
221
|
+
#[test]
|
|
222
|
+
fn a_handle_id_past_the_wire_range_is_refused() {
|
|
223
|
+
let call_of = |id: u32| {
|
|
224
|
+
let mut w = Writer::new();
|
|
225
|
+
w.u8(KIND_HANDLE).u32(id).bytes(b"m").u8(0);
|
|
226
|
+
w.into_bytes()
|
|
227
|
+
};
|
|
228
|
+
assert!(
|
|
229
|
+
Call::decode(&call_of(0x7fff_ffff)).is_ok()
|
|
230
|
+
&& Call::decode(&call_of(0x8000_0000)).is_err(),
|
|
231
|
+
"Call::decode must take a Handle id of 0x7fff_ffff in the target position and refuse \
|
|
232
|
+
0x8000_0000"
|
|
233
|
+
);
|
|
234
|
+
}
|
|
235
|
+
|
|
208
236
|
// @behavior WE-016
|
|
209
237
|
#[test]
|
|
210
238
|
fn a_non_boolean_block_flag_is_refused() {
|
|
@@ -20,9 +20,9 @@ pub enum CompileReply {
|
|
|
20
20
|
impl CompileReply {
|
|
21
21
|
pub fn decode(bytes: &[u8]) -> Result<Self, DecodeError> {
|
|
22
22
|
let mut reader = Reader::new(bytes);
|
|
23
|
-
match reader.
|
|
23
|
+
match reader.read_u8()? {
|
|
24
24
|
tag::OK => {
|
|
25
|
-
let bytecodes = reader.
|
|
25
|
+
let bytecodes = reader.read_bytes_list()?;
|
|
26
26
|
reader.finish()?;
|
|
27
27
|
Ok(CompileReply::Ok(bytecodes))
|
|
28
28
|
}
|
|
@@ -8,6 +8,27 @@
|
|
|
8
8
|
use super::bytes::{Reader, Writer};
|
|
9
9
|
use super::DecodeError;
|
|
10
10
|
|
|
11
|
+
/// The name a wire violation carries: an exchange that did not complete.
|
|
12
|
+
pub const TRANSPORT_ERROR: &str = "Kobako::Transport::Error";
|
|
13
|
+
/// The name a guest that could not start the Sandbox carries.
|
|
14
|
+
pub const BOOT_ERROR: &str = "Kobako::BootError";
|
|
15
|
+
/// The name a failure the guest's own flow detected carries, where no script
|
|
16
|
+
/// error names one.
|
|
17
|
+
pub const SANDBOX_ERROR: &str = "Kobako::SandboxError";
|
|
18
|
+
/// The name a Snippet the guest could not load carries.
|
|
19
|
+
pub const BYTECODE_ERROR: &str = "Kobako::BytecodeError";
|
|
20
|
+
/// The name a critical Preamble flag the guest does not know carries.
|
|
21
|
+
pub const UNSUPPORTED_FLAG_ERROR: &str = "Kobako::UnsupportedFlagError";
|
|
22
|
+
/// The name a Run whose entrypoint the guest cannot resolve carries.
|
|
23
|
+
pub const UNDEFINED_ENTRYPOINT_ERROR: &str = "Kobako::UndefinedEntrypointError";
|
|
24
|
+
/// The name a Service call that failed carries, where no Fault category
|
|
25
|
+
/// narrows it.
|
|
26
|
+
pub const SERVICE_ERROR: &str = "Kobako::ServiceError";
|
|
27
|
+
/// The name a call to a Service the host does not hold carries.
|
|
28
|
+
pub const NO_SERVICE_ERROR: &str = "Kobako::NoServiceError";
|
|
29
|
+
/// The name a Service call the host refused for its arguments carries.
|
|
30
|
+
pub const SERVICE_ARGUMENT_ERROR: &str = "Kobako::ServiceArgumentError";
|
|
31
|
+
|
|
11
32
|
/// The error's name, message, and backtrace as the guest saw them. `name`
|
|
12
33
|
/// rather than `class` because a guest need not be object-oriented to have
|
|
13
34
|
/// named the error it is reporting.
|
|
@@ -21,9 +42,9 @@ pub struct ErrorRecord {
|
|
|
21
42
|
impl ErrorRecord {
|
|
22
43
|
pub(crate) fn read(reader: &mut Reader<'_>) -> Result<Self, DecodeError> {
|
|
23
44
|
Ok(ErrorRecord {
|
|
24
|
-
name: reader.
|
|
25
|
-
message: reader.
|
|
26
|
-
backtrace: reader.
|
|
45
|
+
name: reader.read_text()?.to_owned(),
|
|
46
|
+
message: reader.read_text()?.to_owned(),
|
|
47
|
+
backtrace: reader.read_text_list()?,
|
|
27
48
|
})
|
|
28
49
|
}
|
|
29
50
|
|
|
@@ -116,8 +116,8 @@ impl Fault {
|
|
|
116
116
|
}
|
|
117
117
|
|
|
118
118
|
pub(crate) fn read(reader: &mut Reader<'_>) -> Result<Self, DecodeError> {
|
|
119
|
-
let kind = FaultKind::from_tag(reader.
|
|
120
|
-
let message = reader.
|
|
119
|
+
let kind = FaultKind::from_tag(reader.read_u8()?);
|
|
120
|
+
let message = reader.read_text()?.to_owned();
|
|
121
121
|
// A Fault gains fields as the contract does, and it is the last
|
|
122
122
|
// thing a Reply carries, so whatever follows the fields this
|
|
123
123
|
// reader knows belongs to a later version of them.
|
|
@@ -0,0 +1,99 @@
|
|
|
1
|
+
//! The form every name a guest interns takes: a Snippet's name, a Run's
|
|
2
|
+
//! entrypoint, and each segment of a Binding's path. The guest reaches each
|
|
3
|
+
//! one as a Ruby constant, so every side refuses one outside that form
|
|
4
|
+
//! before it is written.
|
|
5
|
+
|
|
6
|
+
/// The module the guest keeps for itself, where no Binding may stand.
|
|
7
|
+
pub const RESERVED_NAMESPACE: &str = "Kobako";
|
|
8
|
+
|
|
9
|
+
/// The guest interns every constant name, and mruby holds a symbol of at
|
|
10
|
+
/// most this many bytes.
|
|
11
|
+
const MAX_SYMBOL_LEN: usize = 65_534;
|
|
12
|
+
|
|
13
|
+
/// Whether `name` is a Ruby constant name the guest can intern: an ASCII
|
|
14
|
+
/// capital, then ASCII letters, digits, or `_`, within mruby's symbol length.
|
|
15
|
+
pub fn is_constant_name(name: &str) -> bool {
|
|
16
|
+
let mut chars = name.chars();
|
|
17
|
+
name.len() <= MAX_SYMBOL_LEN
|
|
18
|
+
&& chars.next().is_some_and(|c| c.is_ascii_uppercase())
|
|
19
|
+
&& chars.all(|c| c.is_ascii_alphanumeric() || c == '_')
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
/// Whether `path` is constant names joined by `::`.
|
|
23
|
+
pub fn is_constant_path(path: &str) -> bool {
|
|
24
|
+
path.split("::").all(is_constant_name)
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
/// Whether `path` stands in the guest's own `RESERVED_NAMESPACE`.
|
|
28
|
+
pub fn is_reserved(path: &str) -> bool {
|
|
29
|
+
path.split("::").next() == Some(RESERVED_NAMESPACE)
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
/// Whether `path` and `bound` cannot both be bound: they name one constant,
|
|
33
|
+
/// or one stands as the other's namespace, so the guest could not tell a
|
|
34
|
+
/// Service from a grouping module.
|
|
35
|
+
pub fn collides(path: &str, bound: &str) -> bool {
|
|
36
|
+
let within = |inner: &str, outer: &str| {
|
|
37
|
+
inner
|
|
38
|
+
.strip_prefix(outer)
|
|
39
|
+
.is_some_and(|rest| rest.starts_with("::"))
|
|
40
|
+
};
|
|
41
|
+
path == bound || within(path, bound) || within(bound, path)
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
#[cfg(test)]
|
|
45
|
+
mod tests {
|
|
46
|
+
use super::*;
|
|
47
|
+
|
|
48
|
+
#[test]
|
|
49
|
+
fn a_constant_name_is_an_ascii_capital_then_word_characters_within_a_symbol() {
|
|
50
|
+
let longest = format!("A{}", "b".repeat(MAX_SYMBOL_LEN - 1));
|
|
51
|
+
assert_eq!(
|
|
52
|
+
[
|
|
53
|
+
is_constant_name(&longest),
|
|
54
|
+
is_constant_name(&format!("{longest}b")),
|
|
55
|
+
is_constant_name("Aé"),
|
|
56
|
+
is_constant_name("A\n"),
|
|
57
|
+
is_constant_name("lower"),
|
|
58
|
+
is_constant_name(""),
|
|
59
|
+
],
|
|
60
|
+
[true, false, false, false, false, false],
|
|
61
|
+
"is_constant_name must take an ASCII capital and word characters up to 65534 bytes, \
|
|
62
|
+
and refuse a longer name, a non-ASCII or newline character, a lowercase start, \
|
|
63
|
+
and an empty name"
|
|
64
|
+
);
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
#[test]
|
|
68
|
+
fn a_path_is_constant_names_joined_and_the_guests_own_namespace_is_reserved() {
|
|
69
|
+
assert_eq!(
|
|
70
|
+
[
|
|
71
|
+
is_constant_path("MyService::KV"),
|
|
72
|
+
is_constant_path("MyService::"),
|
|
73
|
+
is_constant_path("MyService:KV"),
|
|
74
|
+
is_reserved("Kobako"),
|
|
75
|
+
is_reserved("Kobako::KV"),
|
|
76
|
+
is_reserved("KobakoKV"),
|
|
77
|
+
],
|
|
78
|
+
[true, false, false, true, true, false],
|
|
79
|
+
"is_constant_path must join constant names by '::' only, and is_reserved must hold \
|
|
80
|
+
a path whose first segment is Kobako and no other"
|
|
81
|
+
);
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
#[test]
|
|
85
|
+
fn a_path_collides_with_itself_and_with_either_side_of_a_namespace() {
|
|
86
|
+
assert_eq!(
|
|
87
|
+
[
|
|
88
|
+
collides("KV", "KV"),
|
|
89
|
+
collides("KV::Store", "KV"),
|
|
90
|
+
collides("KV", "KV::Store"),
|
|
91
|
+
collides("KVStore", "KV"),
|
|
92
|
+
collides("KV", "Cache"),
|
|
93
|
+
],
|
|
94
|
+
[true, true, true, false, false],
|
|
95
|
+
"collides must hold for one path and for a path inside another's namespace, and \
|
|
96
|
+
not for a path that only shares a prefix"
|
|
97
|
+
);
|
|
98
|
+
}
|
|
99
|
+
}
|
|
@@ -11,9 +11,9 @@ use super::{tag, DecodeError, ErrorRecord, Unsupported};
|
|
|
11
11
|
///
|
|
12
12
|
/// The wire field is an open set of byte strings, but only two values
|
|
13
13
|
/// carry meaning — so both sides read and write it through this type and
|
|
14
|
-
/// cannot come to spell either one differently.
|
|
15
|
-
///
|
|
16
|
-
///
|
|
14
|
+
/// cannot come to spell either one differently. An Outcome whose value no
|
|
15
|
+
/// origin spells attributes to the sandbox rather than widening what a
|
|
16
|
+
/// Service can claim.
|
|
17
17
|
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
|
|
18
18
|
pub enum Origin {
|
|
19
19
|
/// The guest script raised, or a boot step faulted.
|
|
@@ -32,12 +32,12 @@ impl Origin {
|
|
|
32
32
|
}
|
|
33
33
|
}
|
|
34
34
|
|
|
35
|
-
/// Read a wire spelling back
|
|
36
|
-
|
|
37
|
-
pub fn from_name(name: &str) -> Self {
|
|
35
|
+
/// Read a wire spelling back, `None` for one no origin spells.
|
|
36
|
+
pub(crate) fn from_name(name: &str) -> Option<Self> {
|
|
38
37
|
match name {
|
|
39
|
-
"
|
|
40
|
-
|
|
38
|
+
"sandbox" => Some(Origin::Sandbox),
|
|
39
|
+
"service" => Some(Origin::Service),
|
|
40
|
+
_ => None,
|
|
41
41
|
}
|
|
42
42
|
}
|
|
43
43
|
}
|
|
@@ -66,12 +66,12 @@ pub struct Panic {
|
|
|
66
66
|
impl Outcome {
|
|
67
67
|
pub fn decode(bytes: &[u8]) -> Result<Self, DecodeError> {
|
|
68
68
|
let mut reader = Reader::new(bytes);
|
|
69
|
-
match reader.
|
|
69
|
+
match reader.read_u8()? {
|
|
70
70
|
tag::OK => Ok(Outcome::Ok(reader.remaining().to_vec())),
|
|
71
71
|
tag::PANIC => {
|
|
72
|
-
let origin = Origin::from_name(reader.
|
|
72
|
+
let origin = Origin::from_name(reader.read_text()?).unwrap_or(Origin::Sandbox);
|
|
73
73
|
let error = ErrorRecord::read(&mut reader)?;
|
|
74
|
-
let available = reader.
|
|
74
|
+
let available = reader.read_text_list()?;
|
|
75
75
|
reader.finish()?;
|
|
76
76
|
Ok(Outcome::Panic(Panic {
|
|
77
77
|
origin,
|
|
@@ -66,26 +66,22 @@ impl Preamble {
|
|
|
66
66
|
/// appear at most once; an absent one reads as empty.
|
|
67
67
|
pub fn decode(bytes: &[u8]) -> Result<Self, DecodeError> {
|
|
68
68
|
let mut reader = Reader::new(bytes);
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
return Err(DecodeError::new(
|
|
75
|
-
"Preamble declares more records than it holds",
|
|
76
|
-
));
|
|
77
|
-
}
|
|
69
|
+
// Each record costs at least its two leading bytes and a body length.
|
|
70
|
+
let count = reader.read_count(
|
|
71
|
+
2 + size_of::<u32>(),
|
|
72
|
+
DecodeError::new("Preamble declares more records than it holds"),
|
|
73
|
+
)?;
|
|
78
74
|
let mut bindings = None;
|
|
79
75
|
let mut snippets = None;
|
|
80
76
|
let mut flags = Vec::new();
|
|
81
77
|
for _ in 0..count {
|
|
82
|
-
let critical = match reader.
|
|
78
|
+
let critical = match reader.read_u8()? {
|
|
83
79
|
ADVISORY => false,
|
|
84
80
|
CRITICAL => true,
|
|
85
81
|
_ => return Err(DecodeError::new("Preamble criticality must be 0 or 1")),
|
|
86
82
|
};
|
|
87
|
-
let kind = reader.
|
|
88
|
-
let body = reader.
|
|
83
|
+
let kind = reader.read_u8()?;
|
|
84
|
+
let body = reader.read_bytes()?;
|
|
89
85
|
match kind {
|
|
90
86
|
tag::BINDINGS => once(&mut bindings, decode_bindings(body)?)?,
|
|
91
87
|
tag::SNIPPETS => once(&mut snippets, decode_snippets(body)?)?,
|
|
@@ -147,7 +143,7 @@ fn once<T>(slot: &mut Option<T>, value: T) -> Result<(), DecodeError> {
|
|
|
147
143
|
|
|
148
144
|
fn decode_bindings(bytes: &[u8]) -> Result<Bindings, DecodeError> {
|
|
149
145
|
let mut reader = Reader::new(bytes);
|
|
150
|
-
let paths = reader.
|
|
146
|
+
let paths = reader.read_text_list()?;
|
|
151
147
|
reader.finish()?;
|
|
152
148
|
Ok(Bindings { paths })
|
|
153
149
|
}
|
|
@@ -160,24 +156,20 @@ fn encode_bindings(bindings: &Bindings) -> Vec<u8> {
|
|
|
160
156
|
|
|
161
157
|
fn decode_snippets(bytes: &[u8]) -> Result<Snippets, DecodeError> {
|
|
162
158
|
let mut reader = Reader::new(bytes);
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
return Err(DecodeError::new(
|
|
169
|
-
"Preamble snippets declare more entries than they hold",
|
|
170
|
-
));
|
|
171
|
-
}
|
|
159
|
+
// Each entry costs at least a kind byte and a `u32` length.
|
|
160
|
+
let count = reader.read_count(
|
|
161
|
+
1 + size_of::<u32>(),
|
|
162
|
+
DecodeError::new("Preamble snippets declare more entries than they hold"),
|
|
163
|
+
)?;
|
|
172
164
|
let mut entries = Vec::with_capacity(count);
|
|
173
165
|
for _ in 0..count {
|
|
174
|
-
entries.push(match reader.
|
|
166
|
+
entries.push(match reader.read_u8()? {
|
|
175
167
|
SNIPPET_SOURCE => Snippet::Source {
|
|
176
|
-
name: reader.
|
|
177
|
-
body: reader.
|
|
168
|
+
name: reader.read_text()?.to_owned(),
|
|
169
|
+
body: reader.read_text()?.to_owned(),
|
|
178
170
|
},
|
|
179
171
|
SNIPPET_BYTECODE => Snippet::Bytecode {
|
|
180
|
-
body: reader.
|
|
172
|
+
body: reader.read_bytes()?.to_vec(),
|
|
181
173
|
},
|
|
182
174
|
_ => return Err(DecodeError::new("Preamble snippet kind must be 0 or 1")),
|
|
183
175
|
});
|
|
@@ -207,8 +199,8 @@ fn encode_snippets(snippets: &Snippets) -> Vec<u8> {
|
|
|
207
199
|
|
|
208
200
|
fn decode_flag(bytes: &[u8], critical: bool) -> Result<Flag, DecodeError> {
|
|
209
201
|
let mut reader = Reader::new(bytes);
|
|
210
|
-
let name = reader.
|
|
211
|
-
let value = reader.
|
|
202
|
+
let name = reader.read_text()?.to_owned();
|
|
203
|
+
let value = reader.read_text()?.to_owned();
|
|
212
204
|
reader.finish()?;
|
|
213
205
|
Ok(Flag {
|
|
214
206
|
name,
|
|
@@ -388,10 +380,21 @@ mod tests {
|
|
|
388
380
|
fn a_count_the_preamble_cannot_satisfy_is_refused() {
|
|
389
381
|
let records = [0xff, 0xff, 0xff, 0xff];
|
|
390
382
|
let snippets = preamble_with(&[record(1, 0x06, &[0xff, 0xff, 0xff, 0xff])]);
|
|
391
|
-
|
|
392
|
-
|
|
383
|
+
// The count's own refusal, not a later read running out of bytes,
|
|
384
|
+
// is what shows nothing was taken for it.
|
|
385
|
+
let refusal = |bytes: &[u8]| {
|
|
386
|
+
Preamble::decode(bytes)
|
|
387
|
+
.err()
|
|
388
|
+
.map(|err| err.message().to_owned())
|
|
389
|
+
};
|
|
390
|
+
assert_eq!(
|
|
391
|
+
(refusal(&records), refusal(&snippets)),
|
|
392
|
+
(
|
|
393
|
+
Some("Preamble declares more records than it holds".to_owned()),
|
|
394
|
+
Some("Preamble snippets declare more entries than they hold".to_owned())
|
|
395
|
+
),
|
|
393
396
|
"a record or snippet count larger than what follows through Preamble::decode must \
|
|
394
|
-
be refused before anything is allocated for it"
|
|
397
|
+
be refused for its count, before anything is allocated for it"
|
|
395
398
|
);
|
|
396
399
|
}
|
|
397
400
|
|