kobako 0.27.0 → 0.28.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 +71 -0
- data/Cargo.lock +117 -140
- data/README.md +100 -125
- data/ROADMAP.md +23 -22
- data/SECURITY.md +1 -1
- data/crates/kobako-runtime/CHANGELOG.md +31 -0
- data/crates/kobako-runtime/Cargo.toml +2 -2
- data/crates/kobako-runtime/README.md +4 -3
- data/crates/kobako-runtime/src/error.rs +8 -9
- data/crates/kobako-runtime/src/lib.rs +1 -1
- data/crates/kobako-runtime/src/profile.rs +1 -1
- data/crates/kobako-runtime/src/runtime.rs +31 -17
- data/crates/kobako-runtime/src/snapshot.rs +13 -10
- data/crates/kobako-runtime/src/yielder.rs +9 -6
- data/crates/kobako-transport/CHANGELOG.md +28 -0
- data/crates/kobako-transport/Cargo.toml +1 -1
- data/crates/kobako-transport/README.md +21 -12
- data/crates/kobako-transport/src/abi.rs +11 -14
- data/crates/kobako-transport/src/envelope/bytes.rs +18 -6
- data/crates/kobako-transport/src/envelope/compile_reply.rs +139 -0
- data/crates/kobako-transport/src/envelope/outcome.rs +17 -16
- data/crates/kobako-transport/src/envelope/preamble.rs +513 -0
- data/crates/kobako-transport/src/envelope/reply.rs +77 -53
- data/crates/kobako-transport/src/envelope/request.rs +295 -0
- data/crates/kobako-transport/src/envelope/run.rs +1 -2
- data/crates/kobako-transport/src/envelope/tag.rs +49 -0
- data/crates/kobako-transport/src/envelope/unsupported.rs +105 -0
- data/crates/kobako-transport/src/envelope.rs +19 -12
- data/crates/kobako-transport/src/lib.rs +0 -2
- data/crates/kobako-wasmtime/CHANGELOG.md +44 -0
- data/crates/kobako-wasmtime/Cargo.toml +7 -5
- data/crates/kobako-wasmtime/README.md +2 -2
- data/crates/kobako-wasmtime/src/abi.rs +5 -8
- data/crates/kobako-wasmtime/src/ambient.rs +2 -2
- data/crates/kobako-wasmtime/src/cache.rs +25 -59
- data/crates/kobako-wasmtime/src/capture.rs +4 -11
- data/crates/kobako-wasmtime/src/config.rs +11 -14
- data/crates/kobako-wasmtime/src/dispatch.rs +70 -80
- data/crates/kobako-wasmtime/src/driver.rs +133 -86
- data/crates/kobako-wasmtime/src/exports.rs +88 -34
- data/crates/kobako-wasmtime/src/guest_mem.rs +145 -138
- data/crates/kobako-wasmtime/src/instance_pre.rs +11 -22
- data/crates/kobako-wasmtime/src/invocation.rs +55 -372
- data/crates/kobako-wasmtime/src/io.rs +286 -0
- data/crates/kobako-wasmtime/src/lib.rs +3 -24
- data/crates/kobako-wasmtime/src/limiter.rs +253 -0
- data/crates/kobako-wasmtime/src/trap.rs +32 -45
- data/crates/kobako-wasmtime/tests/preamble_flags.rs +69 -0
- data/crates/kobako-wasmtime/tests/snippet_compile.rs +94 -0
- data/crates/kobako-wasmtime/tests/support/mod.rs +34 -0
- data/crates/kobako-wasmtime/tests/unsupported_call.rs +50 -0
- data/data/kobako.wasm +0 -0
- data/ext/kobako/Cargo.toml +2 -2
- data/ext/kobako/src/runtime/bridge.rs +38 -47
- data/ext/kobako/src/runtime/errors.rs +26 -32
- data/ext/kobako/src/runtime/gvl.rs +4 -12
- data/ext/kobako/src/runtime.rs +169 -212
- data/lib/kobako/capture.rb +1 -4
- data/lib/kobako/catalog/extensions.rb +21 -59
- data/lib/kobako/catalog/flags.rb +41 -0
- data/lib/kobako/catalog/handles.rb +7 -54
- data/lib/kobako/catalog/services.rb +7 -61
- data/lib/kobako/catalog/snippets.rb +42 -66
- data/lib/kobako/catalog.rb +2 -10
- data/lib/kobako/codec/decoder.rb +9 -28
- data/lib/kobako/codec/encoder.rb +3 -22
- data/lib/kobako/codec/error.rb +6 -27
- data/lib/kobako/codec/ext_types.rb +72 -94
- data/lib/kobako/codec/handle_walk.rb +65 -130
- data/lib/kobako/codec/nesting.rb +3 -8
- data/lib/kobako/codec/state.rb +4 -10
- data/lib/kobako/codec/utils.rb +6 -34
- data/lib/kobako/codec.rb +3 -22
- data/lib/kobako/constant_name.rb +8 -0
- data/lib/kobako/context.rb +51 -112
- data/lib/kobako/errors.rb +51 -127
- data/lib/kobako/execution.rb +16 -21
- data/lib/kobako/extension.rb +23 -40
- data/lib/kobako/handle.rb +7 -12
- data/lib/kobako/outcome.rb +51 -80
- data/lib/kobako/payload/arguments.rb +1 -16
- data/lib/kobako/payload.rb +4 -12
- data/lib/kobako/pool.rb +23 -53
- data/lib/kobako/runtime.rb +7 -21
- data/lib/kobako/sandbox.rb +96 -133
- data/lib/kobako/sandbox_options.rb +38 -84
- data/lib/kobako/snippet/binary.rb +1 -16
- data/lib/kobako/snippet/source.rb +1 -16
- data/lib/kobako/snippet.rb +1 -9
- data/lib/kobako/transport/call.rb +1 -14
- data/lib/kobako/transport/dispatcher.rb +112 -195
- data/lib/kobako/transport/error.rb +6 -20
- data/lib/kobako/transport/exposure.rb +5 -10
- data/lib/kobako/transport/reflection.rb +39 -50
- data/lib/kobako/transport/run.rb +17 -71
- data/lib/kobako/transport/yielder.rb +21 -73
- data/lib/kobako/transport.rb +3 -15
- data/lib/kobako/unresolved.rb +4 -11
- data/lib/kobako/usage.rb +2 -6
- data/lib/kobako/version.rb +1 -1
- data/sig/kobako/catalog/extensions.rbs +2 -2
- data/sig/kobako/catalog/flags.rbs +17 -0
- data/sig/kobako/catalog/services.rbs +0 -6
- data/sig/kobako/catalog/snippets.rbs +4 -4
- data/sig/kobako/codec/ext_types.rbs +10 -8
- data/sig/kobako/codec/handle_walk.rbs +9 -7
- data/sig/kobako/codec/utils.rbs +2 -2
- data/sig/kobako/codec.rbs +2 -2
- data/sig/kobako/constant_name.rbs +3 -0
- data/sig/kobako/context.rbs +2 -3
- data/sig/kobako/errors.rbs +6 -0
- data/sig/kobako/outcome.rbs +6 -8
- data/sig/kobako/runtime.rbs +12 -8
- data/sig/kobako/sandbox.rbs +4 -0
- data/sig/kobako/sandbox_options.rbs +2 -6
- data/sig/kobako/transport/dispatcher.rbs +14 -18
- data/sig/kobako/transport/reflection.rbs +5 -3
- data/sig/kobako/transport/run.rbs +1 -3
- data/sig/kobako/transport/yielder.rbs +3 -3
- data/sig/kobako/transport.rbs +7 -6
- metadata +16 -4
- data/crates/kobako-transport/src/envelope/invocation_frames.rs +0 -241
- data/crates/kobako-wasmtime/src/frames.rs +0 -273
- data/rust-toolchain.toml +0 -15
data/lib/kobako/sandbox.rb
CHANGED
|
@@ -10,168 +10,129 @@ require_relative "catalog"
|
|
|
10
10
|
require_relative "context"
|
|
11
11
|
|
|
12
12
|
module Kobako
|
|
13
|
-
#
|
|
14
|
-
#
|
|
15
|
-
#
|
|
16
|
-
#
|
|
17
|
-
# the +Kobako::Catalog::Services+ / +Catalog::Snippets+ / +Catalog::Extensions+
|
|
18
|
-
# registries. Each +#eval+ / +#run+ seals that config on the first call and
|
|
19
|
-
# drives one guest invocation through a fresh +Kobako::Context+ — its own
|
|
20
|
-
# Handle table, dispatch +Proc+, captures, and usage — so the reusable
|
|
21
|
-
# Sandbox holds no per-invocation state. The underlying wasmtime Engine and
|
|
22
|
-
# compiled Module are cached at process scope by the native ext and never
|
|
23
|
-
# surface to Ruby — constructing many Sandboxes amortises both costs
|
|
24
|
-
# automatically.
|
|
25
|
-
#
|
|
26
|
-
# A run's observables — the captured +#stdout+ / +#stderr+ (bounded by
|
|
27
|
-
# +stdout_limit+ / +stderr_limit+, enforced inside the WASI pipe) with their
|
|
28
|
-
# +#stdout_truncated?+ / +#stderr_truncated?+ predicates, and +#usage+ — live
|
|
29
|
-
# on the +Kobako::Execution+ each +#eval+ / +#run+ returns, or on the one its
|
|
30
|
-
# raised error carries. The Sandbox itself keeps none of them, so nothing a
|
|
31
|
-
# run observes carries into the next one.
|
|
13
|
+
# Runs untrusted mruby scripts in an isolated Wasm instance. Configure it
|
|
14
|
+
# once, binding Services and preloading snippets, then call #eval or #run as
|
|
15
|
+
# often as needed: each call returns an Execution and leaves nothing behind
|
|
16
|
+
# for the next.
|
|
32
17
|
class Sandbox
|
|
33
18
|
extend Forwardable
|
|
34
19
|
|
|
35
|
-
|
|
20
|
+
# The Guest Binary this Sandbox runs.
|
|
21
|
+
attr_reader :wasm_path
|
|
22
|
+
|
|
23
|
+
# The SandboxOptions this Sandbox was built with. Each option is also
|
|
24
|
+
# readable on the Sandbox itself, as in #timeout.
|
|
25
|
+
attr_reader :options
|
|
36
26
|
|
|
37
|
-
# Per-option accessors forward to the immutable +SandboxOptions+ Value
|
|
38
|
-
# Object so the Host App still reads them off Sandbox directly.
|
|
39
27
|
def_delegators :@options, :timeout, :memory_limit, :stdout_limit, :stderr_limit, :profile, :gvl
|
|
40
28
|
|
|
41
|
-
# Build a
|
|
29
|
+
# Build a Sandbox on the Guest Binary at +wasm_path+, the one bundled
|
|
30
|
+
# with the gem by default. The other keywords are the options
|
|
31
|
+
# SandboxOptions describes.
|
|
42
32
|
#
|
|
43
|
-
#
|
|
44
|
-
#
|
|
45
|
-
#
|
|
46
|
-
# requested isolation profile (+profile+) — is forwarded verbatim to
|
|
47
|
-
# +Kobako::SandboxOptions+, which owns the DEFAULT fallbacks and
|
|
48
|
-
# normalisation. The constructed +SandboxOptions+ is exposed as
|
|
49
|
-
# +#options+ and every option remains readable directly on Sandbox via
|
|
50
|
-
# +Forwardable+ delegation. The runtime builds the requested profile —
|
|
51
|
-
# +:hermetic+ (the default) denies the guest ambient time and entropy,
|
|
52
|
-
# +:permissive+ leaves them live — and construction refuses a runtime
|
|
53
|
-
# whose declared profile falls below the request, raising
|
|
54
|
-
# +Kobako::SetupError+ before any invocation entry point runs.
|
|
33
|
+
# Raises ArgumentError for an invalid option, and SetupError when the
|
|
34
|
+
# runtime cannot be built or provides less isolation than +profile+ asks
|
|
35
|
+
# for.
|
|
55
36
|
def initialize(wasm_path: nil, **)
|
|
56
37
|
@wasm_path = wasm_path || Kobako::Runtime.default_path
|
|
57
38
|
@options = SandboxOptions.new(**)
|
|
58
39
|
@services = Kobako::Catalog::Services.new
|
|
59
40
|
@snippets = Catalog::Snippets.new
|
|
41
|
+
@flags = Catalog::Flags.new
|
|
60
42
|
@extensions = Catalog::Extensions.new
|
|
43
|
+
@sealed = false
|
|
61
44
|
@runtime = build_runtime!
|
|
62
45
|
end
|
|
63
46
|
|
|
64
|
-
#
|
|
65
|
-
#
|
|
66
|
-
# (+"MyService::KV"+ or a top-level +"File"+). Returns +self+ for
|
|
67
|
-
# chaining.
|
|
47
|
+
# Make +object+ reachable from the guest as the constant at +path+, a
|
|
48
|
+
# Symbol or String such as <tt>"MyService::KV"</tt>. Returns +self+.
|
|
68
49
|
#
|
|
69
|
-
# The guest reaches the public methods +object+'s own class and
|
|
70
|
-
# itself define in source,
|
|
71
|
-
#
|
|
72
|
-
#
|
|
50
|
+
# The guest reaches the public methods that +object+'s own class and
|
|
51
|
+
# +object+ itself define in source, nothing inherited or mixed in. An
|
|
52
|
+
# +object+ defining a private <tt>respond_to_guest?(name)</tt> decides
|
|
53
|
+
# instead, on every call.
|
|
73
54
|
#
|
|
74
|
-
# Called with only
|
|
75
|
-
#
|
|
76
|
-
#
|
|
77
|
-
# guest dispatch to an unfilled fillable surfaces as
|
|
78
|
-
# +Kobako::ServiceError+ when left unrescued.
|
|
55
|
+
# Called with only +path+, it declares a fillable Service: the guest sees
|
|
56
|
+
# the constant, and Context#bind supplies the object for each invocation.
|
|
57
|
+
# A call to one left unfilled fails as a ServiceError.
|
|
79
58
|
#
|
|
80
|
-
# Raises
|
|
81
|
-
#
|
|
82
|
-
# grouping prefix, never both), or when called after the first
|
|
83
|
-
# invocation has sealed Service registration.
|
|
59
|
+
# Raises ArgumentError for a malformed path, a path that collides with an
|
|
60
|
+
# existing binding, or a call after the first invocation.
|
|
84
61
|
def bind(path, object = Unresolved)
|
|
62
|
+
refuse_once_invoked!("bind")
|
|
85
63
|
@services.bind(path, object)
|
|
86
64
|
self
|
|
87
65
|
end
|
|
88
66
|
|
|
89
|
-
# Install
|
|
90
|
-
#
|
|
91
|
-
#
|
|
92
|
-
# +name+ / +source+ / +backend+ / +depends_on+; +Kobako::Extension+ is
|
|
93
|
-
# the bundled value type. Returns +self+.
|
|
67
|
+
# Install Extensions, each guest source paired with an optional host
|
|
68
|
+
# backend. Any object answering +name+, +source+, +backend+ and
|
|
69
|
+
# +depends_on+ works; Extension is the bundled one. Returns +self+.
|
|
94
70
|
#
|
|
95
|
-
# Raises
|
|
96
|
-
#
|
|
97
|
-
# an unmet +depends_on+.
|
|
71
|
+
# Raises ArgumentError for a malformed Extension, a call after the first
|
|
72
|
+
# invocation, or, at the first invocation, a missing dependency.
|
|
98
73
|
def install(*extensions)
|
|
99
|
-
|
|
74
|
+
refuse_once_invoked!("install")
|
|
100
75
|
|
|
101
76
|
extensions.each { |extension| @extensions.install(extension, snippets: @snippets, services: @services) }
|
|
102
77
|
self
|
|
103
78
|
end
|
|
104
79
|
|
|
105
|
-
# Register a snippet
|
|
106
|
-
#
|
|
107
|
-
# * +preload(code: source, name: Name)+ — +source+ is mruby source
|
|
108
|
-
# as a +String+ and +Name+ matches +/\A[A-Z]\w*\z/+. Compile
|
|
109
|
-
# failures surface as +Kobako::SandboxError+ on the first
|
|
110
|
-
# invocation's replay. The +name+
|
|
111
|
-
# becomes the snippet's +(snippet:Name)+ backtrace filename and
|
|
112
|
-
# is the dedupe key that rejects a duplicate +code:+ snippet.
|
|
113
|
-
# * +preload(binary: bytes)+ — +bytes+ is precompiled RITE
|
|
114
|
-
# bytecode as a +String+. The canonical name, when present,
|
|
115
|
-
# lives in the bytecode's embedded +debug_info+ and is resolved
|
|
116
|
-
# by the guest at load time; the host treats the bytes as
|
|
117
|
-
# opaque. Structural failures surface as +Kobako::BytecodeError+
|
|
118
|
-
# on the first invocation.
|
|
80
|
+
# Register a snippet that every invocation runs, in the order registered,
|
|
81
|
+
# before its own code. Returns +self+.
|
|
119
82
|
#
|
|
120
|
-
#
|
|
121
|
-
# snippet
|
|
122
|
-
#
|
|
83
|
+
# [<tt>preload(code: source, name: Name)</tt>]
|
|
84
|
+
# mruby source, named <tt>(snippet:Name)</tt> in backtraces.
|
|
85
|
+
# [<tt>preload(binary: bytes)</tt>]
|
|
86
|
+
# Precompiled RITE bytecode, trusted as host code: the unmodified
|
|
87
|
+
# compiler output of the mruby release the Guest Binary is built on.
|
|
123
88
|
#
|
|
124
|
-
#
|
|
125
|
-
#
|
|
126
|
-
#
|
|
127
|
-
# supplied, when both forms are mixed (e.g., +code:+ and +binary:+
|
|
128
|
-
# together, or +binary:+ paired with +name:+), when +code+ / +bytes+
|
|
129
|
-
# is not a +String+, when +name+ does not match the constant
|
|
130
|
-
# pattern, when +name+ duplicates an already-registered +code:+ form
|
|
131
|
-
# snippet, or when called after the first invocation has sealed the
|
|
132
|
-
# snippet table.
|
|
89
|
+
# Raises ArgumentError for a malformed or duplicate snippet, or a call
|
|
90
|
+
# after the first invocation. A snippet that fails to load fails every
|
|
91
|
+
# invocation with SandboxError, or BytecodeError for bytecode.
|
|
133
92
|
def preload(code: nil, name: nil, binary: nil)
|
|
134
|
-
|
|
93
|
+
refuse_once_invoked!("preload")
|
|
135
94
|
|
|
136
95
|
@snippets.register(code: code, name: name, binary: binary)
|
|
137
96
|
self
|
|
138
97
|
end
|
|
139
98
|
|
|
140
|
-
#
|
|
141
|
-
#
|
|
142
|
-
#
|
|
143
|
-
#
|
|
144
|
-
#
|
|
145
|
-
#
|
|
146
|
-
#
|
|
147
|
-
#
|
|
148
|
-
#
|
|
149
|
-
|
|
99
|
+
# Set the flag +name+, a Symbol or String, to +value+ for every
|
|
100
|
+
# invocation; an empty +value+ is a plain switch. A Guest Binary that
|
|
101
|
+
# does not recognise the flag fails each invocation with
|
|
102
|
+
# UnsupportedFlagError, unless +advisory+ says the guest may skip it.
|
|
103
|
+
# Mark a flag advisory only when a script cannot tell it was skipped.
|
|
104
|
+
# Returns +self+.
|
|
105
|
+
#
|
|
106
|
+
# Raises ArgumentError for a malformed or duplicate flag, or a call after
|
|
107
|
+
# the first invocation.
|
|
108
|
+
def flag(name, value = "", advisory: false)
|
|
109
|
+
refuse_once_invoked!("flag")
|
|
110
|
+
|
|
111
|
+
@flags.register(name, value, advisory: advisory)
|
|
112
|
+
self
|
|
113
|
+
end
|
|
114
|
+
|
|
115
|
+
# Call +call+ on the preloaded top-level constant +target+ with +args+
|
|
116
|
+
# and +kwargs+, and return the Execution. A given block receives the
|
|
117
|
+
# invocation's Context before the guest runs.
|
|
118
|
+
#
|
|
119
|
+
# Raises TypeError when +target+ is not a Symbol or String, and
|
|
120
|
+
# ArgumentError when it is not a constant name or the arguments carry a
|
|
121
|
+
# Handle or a non-Symbol keyword. A failed run raises as #eval does.
|
|
150
122
|
def run(target, *args, **kwargs, &block)
|
|
151
123
|
request = Transport::Run.new(entrypoint: target, args: args, kwargs: kwargs)
|
|
152
124
|
new_invocation.run(request, &block)
|
|
153
125
|
end
|
|
154
126
|
|
|
155
|
-
#
|
|
156
|
-
#
|
|
157
|
-
#
|
|
158
|
-
#
|
|
159
|
-
# Source delivery uses the WASI stdin three-frame protocol
|
|
160
|
-
# ({docs/wire-codec.md Invocation channels}[link:../../docs/wire-codec.md]):
|
|
161
|
-
# Frame 1 carries the preamble (Service registry snapshot), Frame 2 the
|
|
162
|
-
# user source UTF-8 bytes, and Frame 3 the snippet table registered via
|
|
163
|
-
# +#preload+. Each frame is prefixed by a 4-byte big-endian u32 length;
|
|
164
|
-
# Frame 1 and Frame 3 are mandatory-presence — an empty registry sends a
|
|
165
|
-
# present, empty frame rather than an absent one.
|
|
127
|
+
# Evaluate +code+, mruby source as a String, and return the Execution,
|
|
128
|
+
# whose +value+ is the last expression. A given block receives the
|
|
129
|
+
# invocation's Context before the guest runs.
|
|
166
130
|
#
|
|
167
|
-
#
|
|
168
|
-
#
|
|
169
|
-
#
|
|
170
|
-
#
|
|
171
|
-
#
|
|
172
|
-
# (including when +code+ is +nil+ or not a String, or when a preloaded
|
|
173
|
-
# snippet's replay raises); +Kobako::ServiceError+ on an unrescued
|
|
174
|
-
# Service capability failure.
|
|
131
|
+
# Raises TrapError when the engine stopped the guest, a deadline or
|
|
132
|
+
# memory budget included; SandboxError when the guest failed, +code+ not
|
|
133
|
+
# being a String or the Guest Binary not starting the run included; and
|
|
134
|
+
# ServiceError when a Service call failed and the guest left it
|
|
135
|
+
# unrescued.
|
|
175
136
|
def eval(code, &block)
|
|
176
137
|
raise SandboxError, "code must be a String, got #{code.class}" unless code.is_a?(String)
|
|
177
138
|
|
|
@@ -180,10 +141,8 @@ module Kobako
|
|
|
180
141
|
|
|
181
142
|
private
|
|
182
143
|
|
|
183
|
-
#
|
|
184
|
-
#
|
|
185
|
-
# +SandboxOptions#enforce_floor!+ owns the ladder comparison, so a
|
|
186
|
-
# runtime that cannot honor the request never runs guest code.
|
|
144
|
+
# A runtime whose declared posture falls below the request never runs
|
|
145
|
+
# guest code.
|
|
187
146
|
def build_runtime!
|
|
188
147
|
runtime = Kobako::Runtime.from_path(@wasm_path, @options.timeout, @options.memory_limit,
|
|
189
148
|
@options.stdout_limit, @options.stderr_limit, @options.profile,
|
|
@@ -192,23 +151,27 @@ module Kobako
|
|
|
192
151
|
runtime
|
|
193
152
|
end
|
|
194
153
|
|
|
195
|
-
#
|
|
196
|
-
#
|
|
197
|
-
# run's Handle table, resolved Extension backends, captures, and usage, so
|
|
198
|
-
# no per-invocation state is written back onto the shared config.
|
|
154
|
+
# Per-invocation state lives on the Context, so nothing an invocation
|
|
155
|
+
# does is written back onto the shared config.
|
|
199
156
|
def new_invocation
|
|
200
157
|
begin_invocation!
|
|
201
|
-
Context.new(runtime: @runtime, services: @services, snippets: @snippets,
|
|
158
|
+
Context.new(runtime: @runtime, services: @services, snippets: @snippets, flags: @flags,
|
|
159
|
+
extensions: @extensions)
|
|
160
|
+
end
|
|
161
|
+
|
|
162
|
+
# Registration closes at the first invocation, so every invocation ships
|
|
163
|
+
# the same Services, snippets, flags and Extensions.
|
|
164
|
+
def refuse_once_invoked!(verb)
|
|
165
|
+
raise ArgumentError, "cannot #{verb} after first Sandbox invocation" if @sealed
|
|
202
166
|
end
|
|
203
167
|
|
|
204
|
-
#
|
|
205
|
-
#
|
|
206
|
-
#
|
|
207
|
-
# +#install+ all gate on. Per-invocation provider resolution and observable
|
|
208
|
-
# state live on the +Context+, not here.
|
|
168
|
+
# Sealing before the dependency check keeps a Sandbox whose check failed
|
|
169
|
+
# closed to further registration. The sealed snippets are compiled once,
|
|
170
|
+
# before the first invocation replays them.
|
|
209
171
|
def begin_invocation!
|
|
210
|
-
@
|
|
211
|
-
@extensions.
|
|
172
|
+
@sealed = true
|
|
173
|
+
@extensions.assert_dependencies!
|
|
174
|
+
@snippets.compile { |entries| @runtime.compile(@services.paths, entries, @flags.entries) }
|
|
212
175
|
end
|
|
213
176
|
end
|
|
214
177
|
end
|
|
@@ -3,52 +3,42 @@
|
|
|
3
3
|
require_relative "errors"
|
|
4
4
|
|
|
5
5
|
module Kobako
|
|
6
|
-
#
|
|
7
|
-
#
|
|
8
|
-
#
|
|
9
|
-
# Steep-friendly shape — see +.rubocop.yml+ for the rationale).
|
|
6
|
+
# The options a Sandbox was built with, read through Sandbox#options or
|
|
7
|
+
# directly on the Sandbox, as in Sandbox#timeout. A cap left out takes its
|
|
8
|
+
# default, and +nil+ switches it off.
|
|
10
9
|
#
|
|
11
|
-
#
|
|
12
|
-
# +
|
|
13
|
-
# +stderr_limit+
|
|
14
|
-
#
|
|
15
|
-
#
|
|
16
|
-
#
|
|
17
|
-
#
|
|
18
|
-
#
|
|
19
|
-
#
|
|
20
|
-
# is a wire-ready bundle the +Kobako::Runtime+ constructor consumes
|
|
21
|
-
# as-is. The options also own the ladder comparison
|
|
22
|
-
# (+#enforce_floor!+) that +Kobako::Sandbox+ delegates its
|
|
23
|
-
# construction floor check to.
|
|
10
|
+
# [+timeout+] Float seconds one invocation may run.
|
|
11
|
+
# [+memory_limit+] Integer bytes one invocation may grow guest memory by.
|
|
12
|
+
# [+stdout_limit+, +stderr_limit+]
|
|
13
|
+
# Integer bytes kept from each output channel.
|
|
14
|
+
# [+profile+] The isolation the runtime must provide, one of PROFILES.
|
|
15
|
+
# [+gvl+] How an invocation holds Ruby's GVL, one of GVL_MODES.
|
|
16
|
+
#
|
|
17
|
+
# Raises ArgumentError for a value outside these shapes, and SetupError
|
|
18
|
+
# when the runtime provides less isolation than +profile+ asks for.
|
|
24
19
|
class SandboxOptions < Data.define(:timeout, :memory_limit, :stdout_limit, :stderr_limit, :profile, :gvl)
|
|
25
|
-
#
|
|
20
|
+
# The default +timeout+: 60 seconds.
|
|
26
21
|
DEFAULT_TIMEOUT_SECONDS = 60.0
|
|
27
22
|
|
|
28
|
-
#
|
|
29
|
-
#
|
|
30
|
-
# watermark sit outside this budget.
|
|
23
|
+
# The default +memory_limit+: 1 MiB of growth past the guest's starting
|
|
24
|
+
# memory.
|
|
31
25
|
DEFAULT_MEMORY_LIMIT = 1 << 20
|
|
32
26
|
|
|
33
|
-
#
|
|
27
|
+
# The default +stdout_limit+ and +stderr_limit+: 1 MiB each.
|
|
34
28
|
DEFAULT_OUTPUT_LIMIT = 1 << 20
|
|
35
29
|
|
|
36
|
-
# The isolation
|
|
37
|
-
#
|
|
30
|
+
# The isolation profiles, weakest first. +:hermetic+ denies the guest the
|
|
31
|
+
# clock and entropy; +:permissive+ leaves them live.
|
|
38
32
|
PROFILES = %i[permissive hermetic].freeze
|
|
39
33
|
|
|
40
|
-
#
|
|
41
|
-
# +:permissive+ is the Host App's explicit trade.
|
|
34
|
+
# The default +profile+, the strictest.
|
|
42
35
|
DEFAULT_PROFILE = :hermetic
|
|
43
36
|
|
|
44
|
-
#
|
|
45
|
-
#
|
|
46
|
-
# Sandboxes on distinct Threads run their guest code in parallel.
|
|
37
|
+
# +:hold+ keeps the GVL through the invocation; +:release+ drops it while
|
|
38
|
+
# the guest runs, so Sandboxes on different Threads run in parallel.
|
|
47
39
|
GVL_MODES = %i[hold release].freeze
|
|
48
40
|
|
|
49
|
-
#
|
|
50
|
-
# execution, so +:release+ is the Host App's explicit opt-in for
|
|
51
|
-
# host-parallel guest execution.
|
|
41
|
+
# The default +gvl+, +:hold+.
|
|
52
42
|
DEFAULT_GVL = :hold
|
|
53
43
|
|
|
54
44
|
def initialize(timeout: DEFAULT_TIMEOUT_SECONDS,
|
|
@@ -58,22 +48,17 @@ module Kobako
|
|
|
58
48
|
profile: DEFAULT_PROFILE,
|
|
59
49
|
gvl: DEFAULT_GVL)
|
|
60
50
|
timeout = normalize_timeout(timeout)
|
|
61
|
-
memory_limit =
|
|
62
|
-
stdout_limit =
|
|
63
|
-
stderr_limit =
|
|
64
|
-
profile =
|
|
65
|
-
gvl =
|
|
51
|
+
memory_limit = normalize_byte_limit(memory_limit, "memory_limit")
|
|
52
|
+
stdout_limit = normalize_byte_limit(stdout_limit, "stdout_limit")
|
|
53
|
+
stderr_limit = normalize_byte_limit(stderr_limit, "stderr_limit")
|
|
54
|
+
profile = normalize_choice(profile, PROFILES, "profile")
|
|
55
|
+
gvl = normalize_choice(gvl, GVL_MODES, "gvl")
|
|
66
56
|
super
|
|
67
57
|
end
|
|
68
58
|
|
|
69
|
-
#
|
|
70
|
-
#
|
|
71
|
-
|
|
72
|
-
# +Kobako::SetupError+ instead of weakening the posture silently.
|
|
73
|
-
# Both fallbacks fail closed: a declaration off the PROFILES ladder
|
|
74
|
-
# ranks below every request, and a request off the ladder
|
|
75
|
-
# (unreachable past +initialize+) refuses every declaration.
|
|
76
|
-
def enforce_floor!(declared)
|
|
59
|
+
# Both fallbacks fail closed: an unknown declaration ranks below every
|
|
60
|
+
# request, and an unknown request refuses every declaration.
|
|
61
|
+
def enforce_floor!(declared) # :nodoc:
|
|
77
62
|
return if (PROFILES.index(declared) || -1) >= (PROFILES.index(profile) || PROFILES.size)
|
|
78
63
|
|
|
79
64
|
raise Kobako::SetupError, "runtime declares isolation profile #{declared.inspect}, " \
|
|
@@ -82,11 +67,8 @@ module Kobako
|
|
|
82
67
|
|
|
83
68
|
private
|
|
84
69
|
|
|
85
|
-
#
|
|
86
|
-
#
|
|
87
|
-
# rejected — a zero or negative timeout would either fire instantly
|
|
88
|
-
# or never, both of which would surprise callers more than an early
|
|
89
|
-
# +ArgumentError+.
|
|
70
|
+
# A zero or negative timeout would either fire instantly or never, both
|
|
71
|
+
# more surprising than an early ArgumentError.
|
|
90
72
|
def normalize_timeout(timeout)
|
|
91
73
|
return nil if timeout.nil?
|
|
92
74
|
raise ArgumentError, "timeout must be Numeric or nil, got #{timeout.class}" unless timeout.is_a?(Numeric)
|
|
@@ -97,24 +79,7 @@ module Kobako
|
|
|
97
79
|
seconds
|
|
98
80
|
end
|
|
99
81
|
|
|
100
|
-
|
|
101
|
-
# to mean unbounded. Must be a positive Integer when set; Float or
|
|
102
|
-
# zero/negative values are rejected.
|
|
103
|
-
def normalize_memory_limit(memory_limit)
|
|
104
|
-
return nil if memory_limit.nil?
|
|
105
|
-
unless memory_limit.is_a?(Integer) && memory_limit.positive?
|
|
106
|
-
raise ArgumentError, "memory_limit must be a positive Integer or nil, got #{memory_limit.inspect}"
|
|
107
|
-
end
|
|
108
|
-
|
|
109
|
-
memory_limit
|
|
110
|
-
end
|
|
111
|
-
|
|
112
|
-
# Coerce a per-channel output cap (+stdout_limit+ / +stderr_limit+)
|
|
113
|
-
# into the byte cap the ext expects, or +nil+ to leave the channel
|
|
114
|
-
# uncapped. Same shape as +normalize_memory_limit+: a positive Integer
|
|
115
|
-
# when set, Float / zero / negative rejected. +name+ tags the
|
|
116
|
-
# +ArgumentError+ with the offending keyword.
|
|
117
|
-
def normalize_output_limit(limit, name)
|
|
82
|
+
def normalize_byte_limit(limit, name)
|
|
118
83
|
return nil if limit.nil?
|
|
119
84
|
unless limit.is_a?(Integer) && limit.positive?
|
|
120
85
|
raise ArgumentError, "#{name} must be a positive Integer or nil, got #{limit.inspect}"
|
|
@@ -123,23 +88,12 @@ module Kobako
|
|
|
123
88
|
limit
|
|
124
89
|
end
|
|
125
90
|
|
|
126
|
-
#
|
|
127
|
-
#
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
def normalize_profile(profile)
|
|
131
|
-
return profile if PROFILES.include?(profile)
|
|
132
|
-
|
|
133
|
-
raise ArgumentError, "profile must be one of #{PROFILES.map(&:inspect).join(", ")}, got #{profile.inspect}"
|
|
134
|
-
end
|
|
135
|
-
|
|
136
|
-
# Validate +gvl+ against GVL_MODES. Like +profile+ it has no +nil+
|
|
137
|
-
# form: the scheduling mode is requested as an explicit Symbol, so
|
|
138
|
-
# anything off the set — +nil+ included — is rejected.
|
|
139
|
-
def normalize_gvl(gvl)
|
|
140
|
-
return gvl if GVL_MODES.include?(gvl)
|
|
91
|
+
# Unlike the caps there is no +nil+ form: every choice, the weakest
|
|
92
|
+
# included, is requested by name, as in +:permissive+.
|
|
93
|
+
def normalize_choice(value, choices, name)
|
|
94
|
+
return value if choices.include?(value)
|
|
141
95
|
|
|
142
|
-
raise ArgumentError, "
|
|
96
|
+
raise ArgumentError, "#{name} must be one of #{choices.map(&:inspect).join(", ")}, got #{value.inspect}"
|
|
143
97
|
end
|
|
144
98
|
end
|
|
145
99
|
end
|
|
@@ -2,22 +2,7 @@
|
|
|
2
2
|
|
|
3
3
|
module Kobako
|
|
4
4
|
module Snippet
|
|
5
|
-
|
|
6
|
-
# +#preload(binary:)+ entry held by +Kobako::Catalog::Snippets+.
|
|
7
|
-
#
|
|
8
|
-
# The +body+ is RITE bytecode (as emitted by +mrbc+) carried as an
|
|
9
|
-
# +ASCII_8BIT+ String so msgpack-ruby encodes it as +bin+ family on
|
|
10
|
-
# the wire ({docs/wire-codec.md Invocation channels}[link:../../../docs/wire-codec.md]).
|
|
11
|
-
# The host treats the bytes as opaque — the snippet's canonical
|
|
12
|
-
# name, when present, lives in the bytecode's embedded +debug_info+
|
|
13
|
-
# and is resolved by the guest at load time; structural validation
|
|
14
|
-
# is deferred to the first invocation's guest replay.
|
|
15
|
-
#
|
|
16
|
-
# The class is a +Data.define+ subclass — frozen and value-equal.
|
|
17
|
-
# Callers (chiefly +Catalog::Snippets+) construct instances via keyword
|
|
18
|
-
# form +Binary.new(body: ...)+. Wire-form construction is the
|
|
19
|
-
# registry's responsibility.
|
|
20
|
-
class Binary < Data.define(:body)
|
|
5
|
+
class Binary < Data.define(:body) # :nodoc:
|
|
21
6
|
# Names the snippet form the guest replays this entry as. The wire's
|
|
22
7
|
# discriminant byte is assigned by the core envelope, not here.
|
|
23
8
|
KIND = :bytecode
|
|
@@ -2,22 +2,7 @@
|
|
|
2
2
|
|
|
3
3
|
module Kobako
|
|
4
4
|
module Snippet
|
|
5
|
-
|
|
6
|
-
# +#preload(code:, name:)+ entry held by +Kobako::Catalog::Snippets+.
|
|
7
|
-
#
|
|
8
|
-
# +name+ is the canonical +Symbol+ identity baked into the loaded
|
|
9
|
-
# IREP's +debug_info+; backtrace frames originating in this snippet
|
|
10
|
-
# surface as +(snippet:Name):line+. +body+ is the UTF-8 mruby source
|
|
11
|
-
# detached from the caller's reference at +Catalog::Snippets#register+
|
|
12
|
-
# time so later mutation of the original String cannot bleed through.
|
|
13
|
-
#
|
|
14
|
-
# The class is a +Data.define+ subclass — frozen, value-equal, and
|
|
15
|
-
# carries no mutation API. Callers (chiefly +Catalog::Snippets+)
|
|
16
|
-
# construct instances via keyword form +Source.new(name: ..., body: ...)+.
|
|
17
|
-
# Wire-form construction is the registry's responsibility: as a leaf
|
|
18
|
-
# carrier this Source stays pure and +Catalog::Snippets#entries+ reads
|
|
19
|
-
# its attributes off the outside rather than asking it to project itself.
|
|
20
|
-
class Source < Data.define(:name, :body)
|
|
5
|
+
class Source < Data.define(:name, :body) # :nodoc:
|
|
21
6
|
# Names the snippet form the guest replays this entry as. The wire's
|
|
22
7
|
# discriminant byte is assigned by the core envelope, not here.
|
|
23
8
|
KIND = :source
|
data/lib/kobako/snippet.rb
CHANGED
|
@@ -4,14 +4,6 @@ require_relative "snippet/binary"
|
|
|
4
4
|
require_relative "snippet/source"
|
|
5
5
|
|
|
6
6
|
module Kobako
|
|
7
|
-
|
|
8
|
-
# held by +Kobako::Catalog::Snippets+.
|
|
9
|
-
#
|
|
10
|
-
# +Source+ represents a single +#preload(code:, name:)+ entry; +Binary+
|
|
11
|
-
# represents a single +#preload(binary:)+ entry. Both are plain value
|
|
12
|
-
# objects with no dependency on the +Catalog::Snippets+ registry that
|
|
13
|
-
# holds them — the registry reads their attributes externally into the
|
|
14
|
-
# tuples the native side frames.
|
|
15
|
-
module Snippet
|
|
7
|
+
module Snippet # :nodoc:
|
|
16
8
|
end
|
|
17
9
|
end
|
|
@@ -2,20 +2,7 @@
|
|
|
2
2
|
|
|
3
3
|
module Kobako
|
|
4
4
|
module Transport
|
|
5
|
-
|
|
6
|
-
# core envelope. The routing fields arrive already decoded — a String
|
|
7
|
-
# for a bound constant's path, an Integer for a Capability Handle id —
|
|
8
|
-
# so the host resolves a target without interpreting a payload byte.
|
|
9
|
-
#
|
|
10
|
-
# +payload+ stays bytes here on purpose: routing never interprets a
|
|
11
|
-
# payload byte, and the one place that does — the Dispatcher — must
|
|
12
|
-
# decode it inside the Codec bracket that records whether a Handle
|
|
13
|
-
# crossed.
|
|
14
|
-
#
|
|
15
|
-
# Built on the +class X < Data.define(...)+ subclass form so the class
|
|
16
|
-
# body is fully Steep-visible; see +.rubocop.yml+ for the
|
|
17
|
-
# rationale.
|
|
18
|
-
class Call < Data.define(:target, :method_name, :block_given, :payload)
|
|
5
|
+
class Call < Data.define(:target, :method_name, :block_given, :payload) # :nodoc:
|
|
19
6
|
end
|
|
20
7
|
end
|
|
21
8
|
end
|