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,7 +1,5 @@
|
|
|
1
1
|
# frozen_string_literal: true
|
|
2
2
|
|
|
3
|
-
require "delegate"
|
|
4
|
-
|
|
5
3
|
require_relative "../handle"
|
|
6
4
|
require_relative "../transport/exposure"
|
|
7
5
|
|
|
@@ -21,9 +19,12 @@ module Kobako
|
|
|
21
19
|
|
|
22
20
|
# The entry records the object's Exposure as it stands at mint, so a
|
|
23
21
|
# call made through this Handle is authorized against the reference
|
|
24
|
-
# the guest was given.
|
|
22
|
+
# the guest was given. The mint point asks the refusal itself, so it
|
|
23
|
+
# holds on every path a Handle is issued.
|
|
25
24
|
def alloc(object)
|
|
26
|
-
|
|
25
|
+
refused = Kobako::Transport::Exposure.refused_class_of(object)
|
|
26
|
+
raise SandboxError, "a #{refused} cannot cross as a Capability Handle" if refused
|
|
27
|
+
|
|
27
28
|
ensure_capacity
|
|
28
29
|
id = @next_id
|
|
29
30
|
@entries[id] = Kobako::Transport::Exposure.of(object, @surfaces)
|
|
@@ -48,29 +49,6 @@ module Kobako
|
|
|
48
49
|
|
|
49
50
|
private
|
|
50
51
|
|
|
51
|
-
# Refuse to mint a Capability Handle for an object whose reachable
|
|
52
|
-
# surface is not Service behaviour: a +Binding+ / +Method+ /
|
|
53
|
-
# +UnboundMethod+ hands the guest a callable proxy onto host
|
|
54
|
-
# reflection (a returned +Binding+ reaches +Binding#eval+); a +Class+
|
|
55
|
-
# or +Module+ hands over its class-level API (+File.popen+ / +read+,
|
|
56
|
-
# +Kernel.system+), which the owner-based dispatch floor cannot see
|
|
57
|
-
# because a singleton-class owner matches no core-module list; a
|
|
58
|
-
# +Delegator+ (+SimpleDelegator+, +DelegateClass+, +WeakRef+,
|
|
59
|
-
# +Tempfile+) is a transparent forwarder whose public +method_missing+
|
|
60
|
-
# binds and calls the private method the guest names (+Kernel#system+),
|
|
61
|
-
# a surface the floor reads as ordinary Service behaviour. Raising here
|
|
62
|
-
# keeps the rule at the single mint point, so it holds on both the
|
|
63
|
-
# Service-return and the +#run+ host→guest auto-wrap paths.
|
|
64
|
-
def reject_unwrappable(object)
|
|
65
|
-
case object
|
|
66
|
-
when Binding, Method, UnboundMethod, Module, Delegator
|
|
67
|
-
# Delegator < BasicObject exposes no static +#class+, so name the
|
|
68
|
-
# rejected object's class through Object's own.
|
|
69
|
-
kind = Object.instance_method(:class).bind_call(object)
|
|
70
|
-
raise SandboxError, "a #{kind} cannot cross as a Capability Handle"
|
|
71
|
-
end
|
|
72
|
-
end
|
|
73
|
-
|
|
74
52
|
def ensure_capacity
|
|
75
53
|
cap = Kobako::Handle::MAX_ID
|
|
76
54
|
return unless @next_id > cap
|
data/lib/kobako/catalog/mount.rb
CHANGED
|
@@ -3,13 +3,21 @@
|
|
|
3
3
|
module Kobako
|
|
4
4
|
module Catalog
|
|
5
5
|
class Mount < Data.define(:at, :access, :filesystem, :policy) # :nodoc:
|
|
6
|
-
# The
|
|
7
|
-
|
|
6
|
+
# The Mount at +at+, refused by the rules an invocation builds it with,
|
|
7
|
+
# so a mistake raises where it was written.
|
|
8
|
+
def initialize(at:, access:, filesystem:, policy:)
|
|
9
|
+
Kobako::Filesystem.validate_mount(at, access, filesystem)
|
|
10
|
+
super
|
|
11
|
+
end
|
|
12
|
+
|
|
13
|
+
# The kind and path a Context replaces this Mount at, so a Grant of
|
|
14
|
+
# another kind never meets it.
|
|
15
|
+
def key = [:mount, at]
|
|
8
16
|
|
|
9
17
|
# Why +other+ cannot be mounted beside this Mount, or +nil+: a path
|
|
10
18
|
# means one Mount, as Docker refuses a duplicate mount point.
|
|
11
19
|
def conflict(other)
|
|
12
|
-
"path #{at.inspect} is already mounted" if other.
|
|
20
|
+
"path #{at.inspect} is already mounted" if other.key == key
|
|
13
21
|
end
|
|
14
22
|
|
|
15
23
|
# The Mount as the native side reads it.
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Kobako
|
|
4
|
+
module Catalog
|
|
5
|
+
class Network < Data.define(:name, :entry) # :nodoc:
|
|
6
|
+
# The entry named +name+: +entry+ as it stands, or a Builder adding what
|
|
7
|
+
# the block declares to it. Raises TypeError for a name of another class,
|
|
8
|
+
# and ArgumentError for an empty name, which no Context could replace it
|
|
9
|
+
# by.
|
|
10
|
+
def initialize(name:, entry:)
|
|
11
|
+
name = Kobako.to_name(name, "network name").freeze
|
|
12
|
+
Kobako::Network.validate_name(name)
|
|
13
|
+
|
|
14
|
+
if block_given?
|
|
15
|
+
entry = Kobako::Network::Builder.new(entry)
|
|
16
|
+
yield entry
|
|
17
|
+
end
|
|
18
|
+
super
|
|
19
|
+
end
|
|
20
|
+
|
|
21
|
+
# The kind and name a Context replaces this entry by, so a Grant of
|
|
22
|
+
# another kind never meets it.
|
|
23
|
+
def key = [:network, name]
|
|
24
|
+
|
|
25
|
+
# Why +other+ cannot be given beside this entry, or +nil+: a name means
|
|
26
|
+
# one entry, as a path means one Mount.
|
|
27
|
+
def conflict(other)
|
|
28
|
+
"network entry #{name.inspect} is already given" if other.key == key
|
|
29
|
+
end
|
|
30
|
+
|
|
31
|
+
# The entry as the native side reads it.
|
|
32
|
+
def to_native = [name, entry]
|
|
33
|
+
end
|
|
34
|
+
end
|
|
35
|
+
end
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Kobako
|
|
4
|
+
module Catalog
|
|
5
|
+
# The Grants one invocation's Context replaced, each standing in for the
|
|
6
|
+
# Sandbox's Grant at its key.
|
|
7
|
+
class Replacements # :nodoc:
|
|
8
|
+
def initialize(grants)
|
|
9
|
+
@grants = grants
|
|
10
|
+
@replaced = {} # : Hash[Grants::key, Grants::grant]
|
|
11
|
+
end
|
|
12
|
+
|
|
13
|
+
# Have +grant+ stand in for the Grant at its key, raising ArgumentError
|
|
14
|
+
# with +refusal+ when the Sandbox holds none there, so a replacement
|
|
15
|
+
# never adds a Grant.
|
|
16
|
+
def replace(grant, refusal)
|
|
17
|
+
raise ArgumentError, refusal unless @grants.include?(grant)
|
|
18
|
+
|
|
19
|
+
@replaced[grant.key] = grant
|
|
20
|
+
nil
|
|
21
|
+
end
|
|
22
|
+
|
|
23
|
+
# The Sandbox's Grants with each replacement in place, as Grants#entries
|
|
24
|
+
# gives them.
|
|
25
|
+
def entries = @grants.entries(@replaced)
|
|
26
|
+
end
|
|
27
|
+
end
|
|
28
|
+
end
|
|
@@ -1,15 +1,11 @@
|
|
|
1
1
|
# frozen_string_literal: true
|
|
2
2
|
|
|
3
|
-
require_relative "../constant_name"
|
|
4
3
|
require_relative "../errors"
|
|
5
4
|
require_relative "../transport/exposure"
|
|
6
5
|
|
|
7
6
|
module Kobako
|
|
8
7
|
module Catalog
|
|
9
8
|
class Services # :nodoc:
|
|
10
|
-
# The Guest Binary keeps this namespace for constants it may add later.
|
|
11
|
-
RESERVED_NAMESPACE = "Kobako"
|
|
12
|
-
|
|
13
9
|
def initialize
|
|
14
10
|
@bindings = {} # : Hash[String, Kobako::Transport::Exposure]
|
|
15
11
|
end
|
|
@@ -17,9 +13,8 @@ module Kobako
|
|
|
17
13
|
# The binding records +object+'s Exposure as it stands now.
|
|
18
14
|
def bind(path, object)
|
|
19
15
|
path_str = validate_path(path)
|
|
20
|
-
if
|
|
21
|
-
raise ArgumentError,
|
|
22
|
-
"Service path #{path_str} is in the reserved #{RESERVED_NAMESPACE} namespace"
|
|
16
|
+
if Kobako.reserved_path?(path_str)
|
|
17
|
+
raise ArgumentError, "Service path #{path_str} is in the reserved Kobako namespace"
|
|
23
18
|
end
|
|
24
19
|
raise ArgumentError, "Service path #{path_str} conflicts with an existing binding" if collision?(path_str)
|
|
25
20
|
|
|
@@ -47,25 +42,16 @@ module Kobako
|
|
|
47
42
|
private
|
|
48
43
|
|
|
49
44
|
def validate_path(path)
|
|
50
|
-
path_str = path
|
|
51
|
-
|
|
52
|
-
return path_str if !segments.empty? && segments.all? { |seg| CONSTANT_NAME.match?(seg) }
|
|
45
|
+
path_str = Kobako.to_name(path, "bind path")
|
|
46
|
+
return path_str if Kobako.constant_path?(path_str)
|
|
53
47
|
|
|
54
48
|
raise ArgumentError,
|
|
55
49
|
"bind path must be constant-form segments joined by '::' (got #{path.inspect})"
|
|
56
50
|
end
|
|
57
51
|
|
|
58
|
-
def reserved?(path)
|
|
59
|
-
path.split("::").first == RESERVED_NAMESPACE
|
|
60
|
-
end
|
|
61
|
-
|
|
62
52
|
# Keeps a name from being both a bound Service and a grouping prefix.
|
|
63
53
|
def collision?(path)
|
|
64
|
-
@bindings.each_key.any?
|
|
65
|
-
existing == path ||
|
|
66
|
-
existing.start_with?("#{path}::") ||
|
|
67
|
-
path.start_with?("#{existing}::")
|
|
68
|
-
end
|
|
54
|
+
@bindings.each_key.any? { |bound| Kobako.paths_collide?(path, bound) }
|
|
69
55
|
end
|
|
70
56
|
end
|
|
71
57
|
end
|
|
@@ -1,6 +1,5 @@
|
|
|
1
1
|
# frozen_string_literal: true
|
|
2
2
|
|
|
3
|
-
require_relative "../constant_name"
|
|
4
3
|
require_relative "../snippet"
|
|
5
4
|
|
|
6
5
|
module Kobako
|
|
@@ -52,7 +51,7 @@ module Kobako
|
|
|
52
51
|
code, name = ensure_source_args(code, name)
|
|
53
52
|
body = Catalog.to_utf8(code, "code")
|
|
54
53
|
|
|
55
|
-
name_sym =
|
|
54
|
+
name_sym = Kobako.to_constant_name(name, "snippet name")
|
|
56
55
|
if @entries.any? { |e| e.is_a?(Snippet::Source) && e.name == name_sym }
|
|
57
56
|
raise ArgumentError, "snippet #{name_sym.inspect} already preloaded"
|
|
58
57
|
end
|
|
@@ -65,7 +64,7 @@ module Kobako
|
|
|
65
64
|
# as a type error rather than a missing keyword.
|
|
66
65
|
def ensure_source_args(code, name)
|
|
67
66
|
raise ArgumentError, "missing keyword: code: + name:, or binary:" if code.nil? && name.nil?
|
|
68
|
-
raise
|
|
67
|
+
raise TypeError, "code must be a String, got #{code.class}" unless code.is_a?(String)
|
|
69
68
|
raise ArgumentError, "missing keyword: name:" if name.nil?
|
|
70
69
|
|
|
71
70
|
[code, name]
|
|
@@ -74,7 +73,7 @@ module Kobako
|
|
|
74
73
|
# Kept as bytes whatever encoding the caller tagged them with, since
|
|
75
74
|
# the guest reads them as bytecode rather than text.
|
|
76
75
|
def register_binary(bytes)
|
|
77
|
-
raise
|
|
76
|
+
raise TypeError, "binary must be a String, got #{bytes.class}" unless bytes.is_a?(String)
|
|
78
77
|
|
|
79
78
|
@entries << Snippet::Bytecode.new(body: bytes.dup.force_encoding(Encoding::ASCII_8BIT))
|
|
80
79
|
nil
|
|
@@ -91,20 +90,6 @@ module Kobako
|
|
|
91
90
|
[Snippet::Bytecode::KIND, nil, entry.body]
|
|
92
91
|
end
|
|
93
92
|
end
|
|
94
|
-
|
|
95
|
-
def normalize_name(name)
|
|
96
|
-
unless name.is_a?(Symbol) || name.is_a?(String)
|
|
97
|
-
raise ArgumentError, "snippet name must be a Symbol or String, got #{name.class}"
|
|
98
|
-
end
|
|
99
|
-
|
|
100
|
-
name_str = name.to_s
|
|
101
|
-
unless CONSTANT_NAME.match?(name_str)
|
|
102
|
-
raise ArgumentError,
|
|
103
|
-
"snippet name must match #{CONSTANT_NAME.inspect} (got #{name.inspect})"
|
|
104
|
-
end
|
|
105
|
-
|
|
106
|
-
name_str.to_sym
|
|
107
|
-
end
|
|
108
93
|
end
|
|
109
94
|
end
|
|
110
95
|
end
|
data/lib/kobako/catalog.rb
CHANGED
|
@@ -1,12 +1,15 @@
|
|
|
1
1
|
# frozen_string_literal: true
|
|
2
2
|
|
|
3
|
+
require_relative "names"
|
|
3
4
|
require_relative "catalog/handles"
|
|
4
5
|
require_relative "catalog/services"
|
|
5
6
|
require_relative "catalog/snippets"
|
|
6
7
|
require_relative "catalog/flags"
|
|
7
8
|
require_relative "catalog/extensions"
|
|
8
9
|
require_relative "catalog/mount"
|
|
10
|
+
require_relative "catalog/network"
|
|
9
11
|
require_relative "catalog/grants"
|
|
12
|
+
require_relative "catalog/replacements"
|
|
10
13
|
|
|
11
14
|
module Kobako
|
|
12
15
|
module Catalog # :nodoc:
|
|
@@ -4,7 +4,9 @@ require_relative "error"
|
|
|
4
4
|
|
|
5
5
|
module Kobako
|
|
6
6
|
module Codec
|
|
7
|
-
|
|
7
|
+
# What the codec refuses as it reads a value off the wire, each refusal
|
|
8
|
+
# raised as a Codec error.
|
|
9
|
+
module Boundary # :nodoc:
|
|
8
10
|
def self.assert_utf8(string, label)
|
|
9
11
|
return if string.valid_encoding?
|
|
10
12
|
|
|
@@ -14,7 +16,7 @@ module Kobako
|
|
|
14
16
|
# Only for a value object built outside a Decoder.decode block, whose
|
|
15
17
|
# rescue already maps the same way; a host-layer +ArgumentError+
|
|
16
18
|
# elsewhere should propagate unchanged.
|
|
17
|
-
def self.
|
|
19
|
+
def self.within
|
|
18
20
|
yield
|
|
19
21
|
rescue ::ArgumentError => e
|
|
20
22
|
raise InvalidTypeError, e.message
|
data/lib/kobako/codec/decoder.rb
CHANGED
|
@@ -4,14 +4,14 @@ require "msgpack"
|
|
|
4
4
|
|
|
5
5
|
require_relative "error"
|
|
6
6
|
require_relative "ext_types"
|
|
7
|
-
require_relative "
|
|
7
|
+
require_relative "boundary"
|
|
8
8
|
|
|
9
9
|
module Kobako
|
|
10
10
|
module Codec
|
|
11
11
|
module Decoder # :nodoc:
|
|
12
12
|
# The block runs inside this method's rescue, so a Value Object built
|
|
13
13
|
# from the decoded payload reports a broken invariant as
|
|
14
|
-
# InvalidTypeError without its own
|
|
14
|
+
# InvalidTypeError without its own Boundary.within.
|
|
15
15
|
def self.decode(bytes)
|
|
16
16
|
ValueBudget.assert_within(bytes, State.current.value_budget)
|
|
17
17
|
value = FACTORY.load(bytes.b)
|
|
@@ -35,13 +35,13 @@ module Kobako
|
|
|
35
35
|
# returns UTF-8-tagged Strings for
|
|
36
36
|
# str family but does not validate the bytes; +bin+ family decodes
|
|
37
37
|
# to ASCII-8BIT. Walk the tree once and reject invalid UTF-8 in any
|
|
38
|
-
# str-typed leaf via
|
|
38
|
+
# str-typed leaf via Boundary.assert_utf8.
|
|
39
39
|
class << self
|
|
40
40
|
private
|
|
41
41
|
|
|
42
42
|
def validate_utf8(value)
|
|
43
43
|
case value
|
|
44
|
-
when String then
|
|
44
|
+
when String then Boundary.assert_utf8(value, "str payload") if value.encoding == Encoding::UTF_8
|
|
45
45
|
when Array then value.each { |v| validate_utf8(v) }
|
|
46
46
|
when Hash
|
|
47
47
|
value.each do |key, val|
|
|
@@ -3,7 +3,7 @@
|
|
|
3
3
|
require "msgpack"
|
|
4
4
|
|
|
5
5
|
require_relative "error"
|
|
6
|
-
require_relative "
|
|
6
|
+
require_relative "boundary"
|
|
7
7
|
require_relative "state"
|
|
8
8
|
require_relative "../handle"
|
|
9
9
|
|
|
@@ -49,7 +49,7 @@ module Kobako
|
|
|
49
49
|
# unpacker would otherwise apply to invalid bytes.
|
|
50
50
|
def unpack_symbol(payload)
|
|
51
51
|
name = payload.b.force_encoding(Encoding::UTF_8)
|
|
52
|
-
|
|
52
|
+
Boundary.assert_utf8(name, "Symbol payload")
|
|
53
53
|
name.to_sym
|
|
54
54
|
end
|
|
55
55
|
|
|
@@ -66,7 +66,7 @@ module Kobako
|
|
|
66
66
|
raise InvalidTypeError, "Handle payload must be 4 bytes, got #{bytes.bytesize}" unless bytes.bytesize == 4
|
|
67
67
|
|
|
68
68
|
id = bytes.unpack1("N") # : Integer
|
|
69
|
-
Codec::
|
|
69
|
+
Codec::Boundary.within { Kobako::Handle.restore(id) }
|
|
70
70
|
end
|
|
71
71
|
|
|
72
72
|
def register_symbol(factory)
|
|
@@ -26,22 +26,9 @@ module Kobako
|
|
|
26
26
|
primitive_type?(value) || container_representable?(value, depth)
|
|
27
27
|
end
|
|
28
28
|
|
|
29
|
-
#
|
|
30
|
-
#
|
|
31
|
-
#
|
|
32
|
-
def self.deep_wrap(value, handles, depth = 0)
|
|
33
|
-
guard_nesting(depth)
|
|
34
|
-
case value
|
|
35
|
-
when ::Array then value.map { |element| deep_wrap(element, handles, depth + 1) }
|
|
36
|
-
when ::Hash then deep_wrap_hash(value, handles, depth)
|
|
37
|
-
else
|
|
38
|
-
representable?(value) ? value : handles.alloc(value)
|
|
39
|
-
end
|
|
40
|
-
end
|
|
41
|
-
|
|
42
|
-
# The inverse of #deep_wrap. A Handle here was decoded off the wire,
|
|
43
|
-
# never forged by the guest, and +handles.fetch+ refuses an id with no
|
|
44
|
-
# live binding.
|
|
29
|
+
# Each Handle a decoded value holds, as the object it stands for. A
|
|
30
|
+
# Handle here was decoded off the wire, never forged by the guest, and
|
|
31
|
+
# +handles.fetch+ refuses an id with no live binding.
|
|
45
32
|
def self.deep_restore(value, handles)
|
|
46
33
|
case value
|
|
47
34
|
when ::Array then value.map { |element| deep_restore(element, handles) }
|
|
@@ -59,33 +46,6 @@ module Kobako
|
|
|
59
46
|
class << self
|
|
60
47
|
private
|
|
61
48
|
|
|
62
|
-
# A reference cycle necessarily trips the nesting bound, so a +#run+
|
|
63
|
-
# argument fails as a clean +Kobako::SandboxError+ rather than an
|
|
64
|
-
# unbounded host recursion.
|
|
65
|
-
def guard_nesting(depth)
|
|
66
|
-
return unless depth > MAX_NESTING_DEPTH
|
|
67
|
-
|
|
68
|
-
raise Kobako::SandboxError,
|
|
69
|
-
"a #run argument nests deeper than #{MAX_NESTING_DEPTH} levels within the Run payload and " \
|
|
70
|
-
"cannot cross the sandbox boundary (possible reference cycle)"
|
|
71
|
-
end
|
|
72
|
-
|
|
73
|
-
# A stateful object may cross the boundary as a Hash value but not as
|
|
74
|
-
# a key — the one deliberate asymmetry with the guest→host restore
|
|
75
|
-
# walk, which resolves Handle keys the guest built.
|
|
76
|
-
def deep_wrap_hash(hash, handles, depth)
|
|
77
|
-
wrapped = {} # : Hash[untyped, untyped]
|
|
78
|
-
hash.each do |key, val|
|
|
79
|
-
unless representable?(key, depth + 1)
|
|
80
|
-
raise Kobako::SandboxError,
|
|
81
|
-
"a Hash passed to #run has a key that cannot cross the sandbox boundary " \
|
|
82
|
-
"(#{key.class}); only wire-representable values may be Hash keys"
|
|
83
|
-
end
|
|
84
|
-
wrapped[key] = deep_wrap(val, handles, depth + 1)
|
|
85
|
-
end
|
|
86
|
-
wrapped
|
|
87
|
-
end
|
|
88
|
-
|
|
89
49
|
def primitive_type?(value)
|
|
90
50
|
case value
|
|
91
51
|
when ::NilClass, ::TrueClass, ::FalseClass, ::Float, ::String, ::Symbol, Kobako::Handle then true
|
|
@@ -6,8 +6,12 @@ module Kobako
|
|
|
6
6
|
module Codec
|
|
7
7
|
# Counts the values a payload holds before the decoder builds any, so a
|
|
8
8
|
# guest cannot make the host build more objects than it could hold in
|
|
9
|
-
# its own memory. Every value takes at
|
|
10
|
-
#
|
|
9
|
+
# its own memory. Every value takes at least one byte, so a payload no
|
|
10
|
+
# longer than the budget, and holding no float, needs no count.
|
|
11
|
+
#
|
|
12
|
+
# A map key holding a NaN costs one more value for each such key the
|
|
13
|
+
# map already holds, as a Ruby Hash compares it with each: every NaN
|
|
14
|
+
# hashes alike and none is +eql?+ to another.
|
|
11
15
|
#
|
|
12
16
|
# The count stops at the first byte it cannot frame and leaves that
|
|
13
17
|
# payload for the decoder to refuse in its own words.
|
|
@@ -19,7 +23,8 @@ module Kobako
|
|
|
19
23
|
0xd0 => 1, 0xd1 => 2, 0xd2 => 4, 0xd3 => 8,
|
|
20
24
|
0xd4 => 2, 0xd5 => 3, 0xd6 => 5, 0xd7 => 9, 0xd8 => 17
|
|
21
25
|
}.freeze
|
|
22
|
-
# Marker to a container's count width and the values each entry holds
|
|
26
|
+
# Marker to a container's count width and the values each entry holds:
|
|
27
|
+
# two for a map's key and value.
|
|
23
28
|
COUNTS = { 0xdc => [2, 1], 0xdd => [4, 1], 0xde => [2, 2], 0xdf => [4, 2] }.freeze
|
|
24
29
|
# Marker to a str, bin, or ext's length width and type tag width.
|
|
25
30
|
LENGTHS = {
|
|
@@ -30,57 +35,144 @@ module Kobako
|
|
|
30
35
|
UINT = { 1 => "C", 2 => "n", 4 => "N" }.freeze
|
|
31
36
|
private_constant :SCALARS, :COUNTS, :LENGTHS, :UINT
|
|
32
37
|
|
|
38
|
+
# A float frame's marker to how its body unpacks.
|
|
39
|
+
FLOAT_FORMATS = { 0xca => "g", 0xcb => "G" }.freeze
|
|
40
|
+
# A float frame's marker; a payload without one holds no NaN.
|
|
41
|
+
FLOATS = Regexp.union(FLOAT_FORMATS.keys.map(&:chr))
|
|
42
|
+
# The fewest bytes a NaN key and its value take: a float 32 frame and
|
|
43
|
+
# a one-byte value.
|
|
44
|
+
NAN_ENTRY = 1 + SCALARS.fetch(0xca) + 1
|
|
45
|
+
private_constant :FLOAT_FORMATS, :FLOATS, :NAN_ENTRY
|
|
46
|
+
|
|
33
47
|
def self.assert_within(bytes, budget)
|
|
34
|
-
return if budget.nil? || bytes
|
|
48
|
+
return if budget.nil? || within_by_size?(bytes, budget)
|
|
35
49
|
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
pos = 0
|
|
39
|
-
while pending.positive?
|
|
40
|
-
pos, children = step(bytes, pos)
|
|
41
|
-
return if pos.nil?
|
|
42
|
-
raise InvalidTypeError, "payload holds more than #{budget} values" if (remaining -= 1).negative?
|
|
50
|
+
Count.new(bytes, budget).run
|
|
51
|
+
end
|
|
43
52
|
|
|
44
|
-
|
|
45
|
-
|
|
53
|
+
# Every value takes at least one byte, so a payload no longer than the
|
|
54
|
+
# budget holds no more values than it, and what its NaN keys could
|
|
55
|
+
# add is bounded by how many of them its bytes could frame.
|
|
56
|
+
def self.within_by_size?(bytes, budget)
|
|
57
|
+
size = bytes.bytesize
|
|
58
|
+
return false if size > budget
|
|
59
|
+
return true unless bytes.match?(FLOATS)
|
|
60
|
+
|
|
61
|
+
nan_keys = size / NAN_ENTRY
|
|
62
|
+
size + (nan_keys * (nan_keys - 1) / 2) <= budget
|
|
46
63
|
end
|
|
47
64
|
|
|
48
|
-
#
|
|
49
|
-
#
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
65
|
+
# One count through +bytes+, walked without recursion so a payload
|
|
66
|
+
# nested past what the decoder allows cannot exhaust the stack first.
|
|
67
|
+
class Count
|
|
68
|
+
# A container still being read: the values it holds directly,
|
|
69
|
+
# whether it is keyed (a map), how many of them were read, how many
|
|
70
|
+
# of its keys held a NaN, and whether anything in it did.
|
|
71
|
+
OpenContainer = Struct.new(:held, :keyed, :read, :nan_keys, :holds_nan)
|
|
72
|
+
|
|
73
|
+
def initialize(bytes, budget)
|
|
74
|
+
@bytes = bytes
|
|
75
|
+
@budget = budget
|
|
76
|
+
@remaining = budget
|
|
77
|
+
@open = [] # : Array[OpenContainer]
|
|
78
|
+
@ended = false
|
|
59
79
|
end
|
|
60
|
-
end
|
|
61
80
|
|
|
62
|
-
|
|
63
|
-
|
|
81
|
+
def run
|
|
82
|
+
pos = 0
|
|
83
|
+
loop do
|
|
84
|
+
after, children, keyed = step(pos)
|
|
85
|
+
return if after.nil?
|
|
64
86
|
|
|
65
|
-
|
|
66
|
-
|
|
87
|
+
count_value(pos, children, keyed)
|
|
88
|
+
return if @ended
|
|
67
89
|
|
|
68
|
-
|
|
69
|
-
|
|
90
|
+
pos = after
|
|
91
|
+
end
|
|
92
|
+
end
|
|
70
93
|
|
|
71
|
-
|
|
72
|
-
|
|
94
|
+
private
|
|
95
|
+
|
|
96
|
+
# Count the value at +pos+, which holds +children+ directly, keyed
|
|
97
|
+
# when it is a map.
|
|
98
|
+
def count_value(pos, children, keyed)
|
|
99
|
+
spend(1)
|
|
100
|
+
return close_value(nan?(pos)) unless children.positive?
|
|
101
|
+
|
|
102
|
+
@open << OpenContainer.new(children, keyed, 0, 0, false)
|
|
103
|
+
end
|
|
73
104
|
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
105
|
+
# Count a value that ended, and each container it ends, noting once
|
|
106
|
+
# the outermost one did.
|
|
107
|
+
def close_value(holds_nan)
|
|
108
|
+
until @open.empty?
|
|
109
|
+
container = @open.last # : OpenContainer
|
|
110
|
+
charge_key(container) if holds_nan && container.keyed && container.read.even?
|
|
111
|
+
container.holds_nan ||= holds_nan
|
|
112
|
+
container.read += 1
|
|
113
|
+
return unless container.read == container.held
|
|
79
114
|
|
|
80
|
-
|
|
115
|
+
@open.pop
|
|
116
|
+
holds_nan = container.holds_nan
|
|
117
|
+
end
|
|
118
|
+
@ended = true
|
|
119
|
+
end
|
|
120
|
+
|
|
121
|
+
def charge_key(map)
|
|
122
|
+
spend(map.nan_keys)
|
|
123
|
+
map.nan_keys += 1
|
|
124
|
+
end
|
|
125
|
+
|
|
126
|
+
def spend(values)
|
|
127
|
+
@remaining -= values
|
|
128
|
+
raise InvalidTypeError, "payload holds more than #{@budget} values" if @remaining.negative?
|
|
129
|
+
end
|
|
130
|
+
|
|
131
|
+
def nan?(pos)
|
|
132
|
+
marker = @bytes.getbyte(pos)
|
|
133
|
+
format = FLOAT_FORMATS[marker] or return false
|
|
134
|
+
float = @bytes.byteslice(pos + 1, SCALARS.fetch(marker)).to_s.unpack1(format)
|
|
135
|
+
float.is_a?(Float) && float.nan?
|
|
136
|
+
end
|
|
137
|
+
|
|
138
|
+
# The position after the value at +pos+, nil for one the payload
|
|
139
|
+
# cannot frame, the values it holds directly, and whether it is a map.
|
|
140
|
+
def step(pos)
|
|
141
|
+
marker = @bytes.getbyte(pos) or return [nil, 0, false]
|
|
142
|
+
pos += 1
|
|
143
|
+
case marker
|
|
144
|
+
when 0x00..0x7f, 0xe0..0xff then [pos, 0, false]
|
|
145
|
+
when 0x80..0x8f then [pos, (marker & 0x0f) * 2, true]
|
|
146
|
+
when 0x90..0x9f then [pos, marker & 0x0f, false]
|
|
147
|
+
when 0xa0..0xbf then [pos + (marker & 0x1f), 0, false]
|
|
148
|
+
else step_framed(pos, marker)
|
|
149
|
+
end
|
|
150
|
+
end
|
|
151
|
+
|
|
152
|
+
def step_framed(pos, marker)
|
|
153
|
+
return [pos + SCALARS.fetch(marker), 0, false] if SCALARS.key?(marker)
|
|
154
|
+
|
|
155
|
+
width, per = COUNTS[marker]
|
|
156
|
+
return [pos + width, uint(pos, width) * per, per == 2] if width && per
|
|
157
|
+
|
|
158
|
+
width, tag = LENGTHS[marker]
|
|
159
|
+
return [nil, 0, false] unless width && tag
|
|
160
|
+
|
|
161
|
+
[pos + width + tag + uint(pos, width), 0, false]
|
|
162
|
+
end
|
|
163
|
+
|
|
164
|
+
# A length cut short reads as zero, so the count runs off the end and
|
|
165
|
+
# stops there.
|
|
166
|
+
def uint(pos, width)
|
|
167
|
+
slice = @bytes.byteslice(pos, width).to_s
|
|
168
|
+
return 0 if slice.bytesize < width
|
|
169
|
+
|
|
170
|
+
slice.unpack1(UINT.fetch(width)) # : Integer
|
|
171
|
+
end
|
|
81
172
|
end
|
|
173
|
+
private_constant :Count
|
|
82
174
|
|
|
83
|
-
private_class_method :
|
|
175
|
+
private_class_method :within_by_size?
|
|
84
176
|
end
|
|
85
177
|
end
|
|
86
178
|
end
|
data/lib/kobako/codec.rb
CHANGED