kobako 0.19.0-aarch64-linux → 0.21.0-aarch64-linux
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 +127 -0
- data/README.md +96 -44
- data/ROADMAP.md +1 -1
- data/crates/kobako-runtime/CHANGELOG.md +44 -0
- data/crates/kobako-runtime/README.md +1 -1
- data/crates/kobako-transport/CHANGELOG.md +31 -0
- data/crates/kobako-transport/README.md +43 -0
- data/crates/kobako-wasmtime/CHANGELOG.md +30 -0
- data/crates/kobako-wasmtime/README.md +1 -1
- data/data/kobako.wasm +0 -0
- data/lib/kobako/3.3/kobako.so +0 -0
- data/lib/kobako/3.4/kobako.so +0 -0
- data/lib/kobako/4.0/kobako.so +0 -0
- data/lib/kobako/catalog/extensions.rb +50 -43
- data/lib/kobako/catalog/handles.rb +7 -16
- data/lib/kobako/catalog/services.rb +24 -46
- data/lib/kobako/catalog/snippets.rb +16 -33
- data/lib/kobako/codec/decoder.rb +10 -13
- data/lib/kobako/codec/encoder.rb +7 -7
- data/lib/kobako/codec/error.rb +6 -6
- data/lib/kobako/codec/ext_types.rb +12 -72
- data/lib/kobako/codec/handle_walk.rb +6 -6
- data/lib/kobako/codec/state.rb +4 -56
- data/lib/kobako/codec/utils.rb +4 -4
- data/lib/kobako/codec.rb +11 -21
- data/lib/kobako/context.rb +208 -0
- data/lib/kobako/errors.rb +59 -16
- data/lib/kobako/execution.rb +52 -0
- data/lib/kobako/extension.rb +24 -10
- data/lib/kobako/outcome.rb +71 -128
- data/lib/kobako/payload/arguments.rb +59 -0
- data/lib/kobako/payload.rb +19 -0
- data/lib/kobako/pool.rb +1 -7
- data/lib/kobako/sandbox.rb +51 -191
- data/lib/kobako/sandbox_options.rb +24 -3
- data/lib/kobako/snippet/binary.rb +3 -4
- data/lib/kobako/snippet/source.rb +5 -6
- data/lib/kobako/snippet.rb +2 -2
- data/lib/kobako/transport/call.rb +21 -0
- data/lib/kobako/transport/dispatcher.rb +94 -142
- data/lib/kobako/transport/error.rb +21 -5
- data/lib/kobako/transport/reflection.rb +82 -0
- data/lib/kobako/transport/run.rb +25 -33
- data/lib/kobako/transport/yielder.rb +27 -36
- data/lib/kobako/transport.rb +11 -8
- data/lib/kobako/unresolved.rb +17 -0
- data/lib/kobako/usage.rb +7 -7
- data/lib/kobako/version.rb +1 -1
- data/lib/kobako.rb +1 -0
- data/release-please-config.json +47 -2
- data/sig/kobako/catalog/extensions.rbs +4 -6
- data/sig/kobako/catalog/handles.rbs +0 -2
- data/sig/kobako/catalog/services.rbs +4 -4
- data/sig/kobako/catalog/snippets.rbs +4 -2
- data/sig/kobako/codec/error.rbs +4 -4
- data/sig/kobako/codec/ext_types.rbs +0 -7
- data/sig/kobako/codec/state.rbs +0 -4
- data/sig/kobako/codec.rbs +4 -5
- data/sig/kobako/context.rbs +36 -0
- data/sig/kobako/errors.rbs +27 -6
- data/sig/kobako/execution.rbs +24 -0
- data/sig/kobako/extension.rbs +4 -2
- data/sig/kobako/outcome.rbs +12 -13
- data/sig/kobako/payload/arguments.rbs +26 -0
- data/sig/kobako/payload.rbs +4 -0
- data/sig/kobako/pool.rbs +0 -2
- data/sig/kobako/runtime.rbs +44 -14
- data/sig/kobako/sandbox.rbs +5 -24
- data/sig/kobako/sandbox_options.rbs +9 -2
- data/sig/kobako/snippet/binary.rbs +1 -1
- data/sig/kobako/snippet/source.rbs +1 -1
- data/sig/kobako/transport/call.rbs +21 -0
- data/sig/kobako/transport/dispatcher.rbs +9 -11
- data/sig/kobako/transport/error.rbs +11 -0
- data/sig/kobako/transport/reflection.rbs +15 -0
- data/sig/kobako/transport/run.rbs +1 -1
- data/sig/kobako/transport/yielder.rbs +2 -2
- data/sig/kobako/transport.rbs +8 -2
- data/sig/kobako/unresolved.rbs +4 -0
- metadata +18 -12
- data/lib/kobako/fault.rb +0 -38
- data/lib/kobako/outcome/panic.rb +0 -42
- data/lib/kobako/transport/request.rb +0 -76
- data/lib/kobako/transport/response.rb +0 -83
- data/lib/kobako/transport/yield.rb +0 -94
- data/sig/kobako/fault.rbs +0 -19
- data/sig/kobako/outcome/panic.rbs +0 -34
- data/sig/kobako/transport/request.rbs +0 -29
- data/sig/kobako/transport/response.rbs +0 -33
- data/sig/kobako/transport/yield.rbs +0 -34
data/lib/kobako/sandbox.rb
CHANGED
|
@@ -2,37 +2,33 @@
|
|
|
2
2
|
|
|
3
3
|
require "forwardable"
|
|
4
4
|
|
|
5
|
-
require_relative "capture"
|
|
6
|
-
require_relative "codec"
|
|
7
5
|
require_relative "errors"
|
|
8
|
-
require_relative "
|
|
6
|
+
require_relative "unresolved"
|
|
9
7
|
require_relative "sandbox_options"
|
|
10
|
-
require_relative "usage"
|
|
11
8
|
require_relative "transport"
|
|
12
9
|
require_relative "catalog"
|
|
10
|
+
require_relative "context"
|
|
13
11
|
|
|
14
12
|
module Kobako
|
|
15
13
|
# Kobako::Sandbox — the user-facing entry point for executing guest mruby
|
|
16
14
|
# scripts inside a wasmtime-hosted Wasm module.
|
|
17
15
|
#
|
|
18
|
-
# The Sandbox owns the +Kobako::Runtime
|
|
19
|
-
# +Kobako::Catalog::
|
|
20
|
-
# +
|
|
21
|
-
#
|
|
22
|
-
#
|
|
23
|
-
#
|
|
24
|
-
#
|
|
25
|
-
#
|
|
16
|
+
# The Sandbox owns the reusable configuration — the +Kobako::Runtime+ and
|
|
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
|
|
26
24
|
# automatically.
|
|
27
25
|
#
|
|
28
|
-
#
|
|
29
|
-
#
|
|
30
|
-
#
|
|
31
|
-
#
|
|
32
|
-
#
|
|
33
|
-
#
|
|
34
|
-
# +#stdout_truncated?+ / +#stderr_truncated?+ are the only way to observe
|
|
35
|
-
# that the cap was hit.
|
|
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.
|
|
36
32
|
class Sandbox
|
|
37
33
|
extend Forwardable
|
|
38
34
|
|
|
@@ -40,39 +36,7 @@ module Kobako
|
|
|
40
36
|
|
|
41
37
|
# Per-option accessors forward to the immutable +SandboxOptions+ Value
|
|
42
38
|
# Object so the Host App still reads them off Sandbox directly.
|
|
43
|
-
def_delegators :@options, :timeout, :memory_limit, :stdout_limit, :stderr_limit, :profile
|
|
44
|
-
|
|
45
|
-
# Returns the bytes the guest wrote to stdout during the most recent
|
|
46
|
-
# invocation as a UTF-8 String, clipped at +stdout_limit+. Empty before
|
|
47
|
-
# any invocation; the byte content never contains a truncation sentinel,
|
|
48
|
-
# so use +#stdout_truncated?+ to observe overflow. Populated on every
|
|
49
|
-
# outcome — including a rescued +TrapError+, after which it holds the
|
|
50
|
-
# bytes written before the trap fired — mirroring +#usage+.
|
|
51
|
-
def stdout = @stdout_capture.bytes
|
|
52
|
-
|
|
53
|
-
# Returns the bytes the guest wrote to stderr during the most recent
|
|
54
|
-
# invocation as a UTF-8 String, clipped at +stderr_limit+. Empty before
|
|
55
|
-
# any invocation. Mirror of +#stdout+.
|
|
56
|
-
def stderr = @stderr_capture.bytes
|
|
57
|
-
|
|
58
|
-
# Returns +true+ iff stdout capture during the most recent invocation
|
|
59
|
-
# exceeded +stdout_limit+. Resets to +false+ at the start of the next
|
|
60
|
-
# invocation.
|
|
61
|
-
def stdout_truncated? = @stdout_capture.truncated?
|
|
62
|
-
|
|
63
|
-
# Returns +true+ iff stderr capture during the most recent invocation
|
|
64
|
-
# exceeded +stderr_limit+. Mirror of +#stdout_truncated?+.
|
|
65
|
-
def stderr_truncated? = @stderr_capture.truncated?
|
|
66
|
-
|
|
67
|
-
# Returns the +Kobako::Usage+ value object for the most recent
|
|
68
|
-
# invocation. Carries +wall_time+ (Float seconds the guest export call spent
|
|
69
|
-
# inside wasmtime) and +memory_peak+ (Integer bytes, high-water of
|
|
70
|
-
# the per-invocation +memory.grow+ delta past the entry-time
|
|
71
|
-
# baseline). Returns +Kobako::Usage::EMPTY+ before any invocation;
|
|
72
|
-
# populated on every outcome — including +TrapError+ — so the Host
|
|
73
|
-
# App can read it after rescuing a trap to diagnose budget
|
|
74
|
-
# consumption.
|
|
75
|
-
attr_reader :usage
|
|
39
|
+
def_delegators :@options, :timeout, :memory_limit, :stdout_limit, :stderr_limit, :profile, :gvl
|
|
76
40
|
|
|
77
41
|
# Build a fresh Sandbox.
|
|
78
42
|
#
|
|
@@ -91,13 +55,10 @@ module Kobako
|
|
|
91
55
|
def initialize(wasm_path: nil, **)
|
|
92
56
|
@wasm_path = wasm_path || Kobako::Runtime.default_path
|
|
93
57
|
@options = SandboxOptions.new(**)
|
|
94
|
-
@
|
|
95
|
-
@services = Kobako::Catalog::Services.new(handler: @handler)
|
|
58
|
+
@services = Kobako::Catalog::Services.new
|
|
96
59
|
@snippets = Catalog::Snippets.new
|
|
97
60
|
@extensions = Catalog::Extensions.new
|
|
98
61
|
@runtime = build_runtime!
|
|
99
|
-
install_dispatch_proc!
|
|
100
|
-
reset_invocation_state!
|
|
101
62
|
end
|
|
102
63
|
|
|
103
64
|
# Bind +object+ as the Service reachable at +path+ — a Symbol or
|
|
@@ -105,11 +66,17 @@ module Kobako
|
|
|
105
66
|
# (+"MyService::KV"+ or a top-level +"File"+). Returns +self+ for
|
|
106
67
|
# chaining.
|
|
107
68
|
#
|
|
69
|
+
# Called with only a +path+, it declares a fillable Service:
|
|
70
|
+
# +bind(path)+ reserves the path for +Kobako::Unresolved+, so the guest
|
|
71
|
+
# sees the constant while the host defers the object it stands for. A
|
|
72
|
+
# guest dispatch to an unfilled fillable surfaces as
|
|
73
|
+
# +Kobako::ServiceError+ when left unrescued.
|
|
74
|
+
#
|
|
108
75
|
# Raises +ArgumentError+ when a segment is malformed, when +path+
|
|
109
76
|
# collides with an existing binding (a name is a bound Service or a
|
|
110
77
|
# grouping prefix, never both), or when called after the first
|
|
111
78
|
# invocation has sealed Service registration.
|
|
112
|
-
def bind(path, object)
|
|
79
|
+
def bind(path, object = Unresolved)
|
|
113
80
|
@services.bind(path, object)
|
|
114
81
|
self
|
|
115
82
|
end
|
|
@@ -166,8 +133,8 @@ module Kobako
|
|
|
166
133
|
end
|
|
167
134
|
|
|
168
135
|
# Dispatch into a preloaded entrypoint constant. Delegates host
|
|
169
|
-
# pre-flight and
|
|
170
|
-
# +Kobako::Transport::Run#
|
|
136
|
+
# pre-flight and argument encoding to +Kobako::Transport::Run+ /
|
|
137
|
+
# +Kobako::Transport::Run#payload+: a non-Symbol/String +target+ raises
|
|
171
138
|
# +TypeError+, while a +target+ failing the constant pattern, a forged
|
|
172
139
|
# +Kobako::Handle+ in +args+ / +kwargs+, or a non-Symbol +kwargs+ key
|
|
173
140
|
# raise +ArgumentError+. The guest resolves +target+ as a top-level
|
|
@@ -175,11 +142,9 @@ module Kobako
|
|
|
175
142
|
# deserialized result. The first invocation seals the Service registry
|
|
176
143
|
# and snippet table. Runtime errors follow the same three-class
|
|
177
144
|
# taxonomy as +#eval+.
|
|
178
|
-
def run(target, *args, **kwargs)
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
@runtime.run(@services.encode, @snippets.encode, run_envelope.encode(@handler))
|
|
182
|
-
end
|
|
145
|
+
def run(target, *args, **kwargs, &block)
|
|
146
|
+
request = Transport::Run.new(entrypoint: target, args: args, kwargs: kwargs)
|
|
147
|
+
new_invocation.run(request, &block)
|
|
183
148
|
end
|
|
184
149
|
|
|
185
150
|
# Execute a guest mruby source string in a fresh +mrb_state+. +code+ is
|
|
@@ -188,12 +153,11 @@ module Kobako
|
|
|
188
153
|
#
|
|
189
154
|
# Source delivery uses the WASI stdin three-frame protocol
|
|
190
155
|
# ({docs/wire-codec.md Invocation channels}[link:../../docs/wire-codec.md]):
|
|
191
|
-
# Frame 1 carries the
|
|
192
|
-
#
|
|
193
|
-
#
|
|
194
|
-
#
|
|
195
|
-
#
|
|
196
|
-
# array, never an absent frame.
|
|
156
|
+
# Frame 1 carries the preamble (Service registry snapshot), Frame 2 the
|
|
157
|
+
# user source UTF-8 bytes, and Frame 3 the snippet table registered via
|
|
158
|
+
# +#preload+. Each frame is prefixed by a 4-byte big-endian u32 length;
|
|
159
|
+
# Frame 1 and Frame 3 are mandatory-presence — an empty registry sends a
|
|
160
|
+
# present, empty frame rather than an absent one.
|
|
197
161
|
#
|
|
198
162
|
# The first invocation seals the Service registry and snippet table;
|
|
199
163
|
# subsequent +#bind+ / +#preload+ calls raise +ArgumentError+.
|
|
@@ -203,24 +167,10 @@ module Kobako
|
|
|
203
167
|
# (including when +code+ is +nil+ or not a String, or when a preloaded
|
|
204
168
|
# snippet's replay raises); +Kobako::ServiceError+ on an unrescued
|
|
205
169
|
# Service capability failure.
|
|
206
|
-
def eval(code)
|
|
170
|
+
def eval(code, &block)
|
|
207
171
|
raise SandboxError, "code must be a String, got #{code.class}" unless code.is_a?(String)
|
|
208
172
|
|
|
209
|
-
|
|
210
|
-
@runtime.eval(@services.encode, code.b, @snippets.encode)
|
|
211
|
-
end
|
|
212
|
-
end
|
|
213
|
-
|
|
214
|
-
# Reset all per-invocation observable state to its pre-invocation
|
|
215
|
-
# sentinels — both per-channel captures and the per-last-invocation
|
|
216
|
-
# usage record. Shared by +#initialize+ (first-time setup) and
|
|
217
|
-
# +#begin_invocation!+ (between-invocation reset) so both paths agree on
|
|
218
|
-
# what "pre-invocation state" means; +Kobako::Pool+ calls it at checkout
|
|
219
|
-
# so a pooled Sandbox hands over empty output buffers.
|
|
220
|
-
def reset_invocation_state!
|
|
221
|
-
@stdout_capture = Capture::EMPTY
|
|
222
|
-
@stderr_capture = Capture::EMPTY
|
|
223
|
-
@usage = Usage::EMPTY
|
|
173
|
+
new_invocation.eval(code, &block)
|
|
224
174
|
end
|
|
225
175
|
|
|
226
176
|
private
|
|
@@ -231,119 +181,29 @@ module Kobako
|
|
|
231
181
|
# runtime that cannot honor the request never runs guest code.
|
|
232
182
|
def build_runtime!
|
|
233
183
|
runtime = Kobako::Runtime.from_path(@wasm_path, @options.timeout, @options.memory_limit,
|
|
234
|
-
@options.stdout_limit, @options.stderr_limit, @options.profile
|
|
184
|
+
@options.stdout_limit, @options.stderr_limit, @options.profile,
|
|
185
|
+
@options.gvl)
|
|
235
186
|
@options.enforce_floor!(runtime.profile)
|
|
236
187
|
runtime
|
|
237
188
|
end
|
|
238
189
|
|
|
239
|
-
#
|
|
240
|
-
#
|
|
241
|
-
#
|
|
242
|
-
#
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
# wasm ext callback can fire without further setup.
|
|
247
|
-
def install_dispatch_proc!
|
|
248
|
-
@runtime.on_dispatch = lambda do |request_bytes, guest_yielder|
|
|
249
|
-
Transport::Dispatcher.dispatch(request_bytes, @services, @handler, guest_yielder)
|
|
250
|
-
end
|
|
190
|
+
# Seal the config on the first invocation and return a fresh
|
|
191
|
+
# per-invocation +Context+ for the verb to drive. The Context owns this
|
|
192
|
+
# run's Handle table, resolved Extension backends, captures, and usage, so
|
|
193
|
+
# no per-invocation state is written back onto the shared config.
|
|
194
|
+
def new_invocation
|
|
195
|
+
begin_invocation!
|
|
196
|
+
Context.new(runtime: @runtime, services: @services, snippets: @snippets, extensions: @extensions)
|
|
251
197
|
end
|
|
252
198
|
|
|
253
|
-
# Per-invocation prologue
|
|
254
|
-
# registries on first call (idempotent — asserting Extension
|
|
255
|
-
# dependencies then)
|
|
256
|
-
#
|
|
257
|
-
# state
|
|
258
|
-
# +Catalog::Handles+ counter — before the guest runs. The
|
|
259
|
-
# +Catalog::Handles+ itself is held as +@handler+ and never exposed
|
|
260
|
-
# beyond this class — it is not part of the Host App's surface.
|
|
199
|
+
# Per-invocation prologue on the config tier: seals the Service and
|
|
200
|
+
# Extension registries on the first call (idempotent — asserting Extension
|
|
201
|
+
# dependencies then). The Service seal is the one +#bind+ / +#preload+ /
|
|
202
|
+
# +#install+ all gate on. Per-invocation provider resolution and observable
|
|
203
|
+
# state live on the +Context+, not here.
|
|
261
204
|
def begin_invocation!
|
|
262
205
|
@services.seal!
|
|
263
206
|
@extensions.seal!
|
|
264
|
-
@extensions.refresh_backends!(@services)
|
|
265
|
-
@handler.reset!
|
|
266
|
-
reset_invocation_state!
|
|
267
|
-
end
|
|
268
|
-
|
|
269
|
-
# Read the per-last-invocation +wall_time+ and +memory_peak+ from
|
|
270
|
-
# the ext and wrap them as a +Kobako::Usage+ value object. Runs in
|
|
271
|
-
# the +invoke!+ +ensure+ block so the usage record is populated on
|
|
272
|
-
# every outcome — value return, +Kobako::TrapError+ (including
|
|
273
|
-
# +TimeoutError+ / +MemoryLimitError+), +Kobako::SandboxError+,
|
|
274
|
-
# and +Kobako::ServiceError+. +Runtime#usage+ is the single source for
|
|
275
|
-
# both paths: the figures are stashed in the ext on every outcome, so
|
|
276
|
-
# the readout here also covers the trap path, where +Runtime#eval+ /
|
|
277
|
-
# +#run+ raise instead of returning outcome bytes.
|
|
278
|
-
#
|
|
279
|
-
# The ext-side contract is positional: +Runtime#usage+ yields
|
|
280
|
-
# +[wall_time, memory_peak]+ in +Kobako::Usage+ field order.
|
|
281
|
-
def read_usage!
|
|
282
|
-
wall_time, memory_peak = @runtime.usage
|
|
283
|
-
@usage = Usage.new(wall_time: wall_time, memory_peak: memory_peak)
|
|
284
|
-
end
|
|
285
|
-
|
|
286
|
-
# Pick the +TrapError+ subclass to re-raise based on +err+'s actual
|
|
287
|
-
# class. Cap-trap subclasses (+TimeoutError+ / +MemoryLimitError+)
|
|
288
|
-
# preserve their named identity; everything else collapses to the
|
|
289
|
-
# base +Kobako::TrapError+. The ext already raises the right subclass
|
|
290
|
-
# directly, so this is a pure re-attribution that lets +#invoke!+
|
|
291
|
-
# add the verb prefix without erasing +TimeoutError+ /
|
|
292
|
-
# +MemoryLimitError+.
|
|
293
|
-
def trap_class_for(err)
|
|
294
|
-
case err
|
|
295
|
-
when TimeoutError then TimeoutError
|
|
296
|
-
when MemoryLimitError then MemoryLimitError
|
|
297
|
-
else TrapError
|
|
298
|
-
end
|
|
299
|
-
end
|
|
300
|
-
|
|
301
|
-
# Read the per-last-invocation output captures from the ext and wrap
|
|
302
|
-
# them as +Kobako::Capture+ value objects. Runs in the +invoke!+
|
|
303
|
-
# +ensure+ block next to #read_usage! for the same reason: the ext
|
|
304
|
-
# stashes the captures on every outcome, so the readout also covers
|
|
305
|
-
# the trap path, where +Runtime#eval+ / +#run+ raise instead of
|
|
306
|
-
# returning outcome bytes — +#stdout+ / +#stderr+ keep the guest's
|
|
307
|
-
# partial output readable after a rescue.
|
|
308
|
-
#
|
|
309
|
-
# The ext-side contract is positional: +Runtime#captures+ yields
|
|
310
|
-
# +[stdout_bytes, stdout_truncated, stderr_bytes, stderr_truncated]+.
|
|
311
|
-
def read_captures!
|
|
312
|
-
stdout_bytes, stdout_truncated, stderr_bytes, stderr_truncated = @runtime.captures
|
|
313
|
-
@stdout_capture = Capture.new(bytes: stdout_bytes, truncated: stdout_truncated)
|
|
314
|
-
@stderr_capture = Capture.new(bytes: stderr_bytes, truncated: stderr_truncated)
|
|
315
|
-
end
|
|
316
|
-
|
|
317
|
-
# Shared prologue / epilogue + trap-class translator for both
|
|
318
|
-
# invocation verbs. +verb+ is +:eval+ or +:run+; it tags the
|
|
319
|
-
# TrapError message so the failing export is identifiable.
|
|
320
|
-
#
|
|
321
|
-
# The yielded block must return the invocation's raw outcome bytes —
|
|
322
|
-
# i.e. the value of +Runtime#eval+ / +#run+ — which the success path
|
|
323
|
-
# feeds to +Outcome.decode+. Captures and usage are populated by the
|
|
324
|
-
# +ensure+ readouts (#read_usage! / #read_captures!) on every
|
|
325
|
-
# outcome, so +#stdout+ / +#stderr+ / +#usage+ stay readable after a
|
|
326
|
-
# rescued trap.
|
|
327
|
-
# The rescue chain is the single trap-translation boundary —
|
|
328
|
-
# configured-cap paths surface as named TrapError subclasses
|
|
329
|
-
# (+TimeoutError+ / +MemoryLimitError+); everything else surfaces as
|
|
330
|
-
# the base +TrapError+.
|
|
331
|
-
def invoke!(verb)
|
|
332
|
-
begin_invocation!
|
|
333
|
-
return_bytes = yield
|
|
334
|
-
# A Capability Handle in the result is decoded as a Kobako::Handle
|
|
335
|
-
# token; restore it to the host object the guest referenced before
|
|
336
|
-
# handing the value to the Host App. @handler still holds this
|
|
337
|
-
# invocation's table — reset only happens at the next #begin_invocation!.
|
|
338
|
-
# A Handle-free result resolves to itself, so the restoration walk is
|
|
339
|
-
# skipped when the decode carried none.
|
|
340
|
-
value, carried_handle = Codec.track_handles { Outcome.decode(return_bytes) }
|
|
341
|
-
carried_handle ? Codec::HandleWalk.deep_restore(value, @handler) : value
|
|
342
|
-
rescue Kobako::TrapError => e
|
|
343
|
-
raise trap_class_for(e), "Sandbox##{verb} failed: #{e.message}"
|
|
344
|
-
ensure
|
|
345
|
-
read_usage!
|
|
346
|
-
read_captures!
|
|
347
207
|
end
|
|
348
208
|
end
|
|
349
209
|
end
|
|
@@ -6,7 +6,7 @@ module Kobako
|
|
|
6
6
|
# Kobako::SandboxOptions — immutable Value Object holding the four
|
|
7
7
|
# per-Sandbox configuration caps and the requested isolation profile.
|
|
8
8
|
# Built on the +class X < Data.define(...)+ subclass form (the
|
|
9
|
-
# Steep-friendly shape — see
|
|
9
|
+
# Steep-friendly shape — see +.rubocop.yml+ for the rationale).
|
|
10
10
|
#
|
|
11
11
|
# The +initialize+ normalises every option before delegating to Data's
|
|
12
12
|
# +super+: +timeout+ to Float seconds, +memory_limit+ / +stdout_limit+ /
|
|
@@ -21,7 +21,7 @@ module Kobako
|
|
|
21
21
|
# as-is. The options also own the ladder comparison
|
|
22
22
|
# (+#enforce_floor!+) that +Kobako::Sandbox+ delegates its
|
|
23
23
|
# construction floor check to.
|
|
24
|
-
class SandboxOptions < Data.define(:timeout, :memory_limit, :stdout_limit, :stderr_limit, :profile)
|
|
24
|
+
class SandboxOptions < Data.define(:timeout, :memory_limit, :stdout_limit, :stderr_limit, :profile, :gvl)
|
|
25
25
|
# Default wall-clock timeout for a single invocation: 60 seconds.
|
|
26
26
|
DEFAULT_TIMEOUT_SECONDS = 60.0
|
|
27
27
|
|
|
@@ -41,16 +41,28 @@ module Kobako
|
|
|
41
41
|
# +:permissive+ is the Host App's explicit trade.
|
|
42
42
|
DEFAULT_PROFILE = :hermetic
|
|
43
43
|
|
|
44
|
+
# The GVL scheduling modes: +:hold+ keeps Ruby's GVL for the whole
|
|
45
|
+
# invocation, +:release+ drops it for the guest span so distinct
|
|
46
|
+
# Sandboxes on distinct Threads run their guest code in parallel.
|
|
47
|
+
GVL_MODES = %i[hold release].freeze
|
|
48
|
+
|
|
49
|
+
# Default GVL mode: +:hold+ — holding the GVL matches single-threaded
|
|
50
|
+
# execution, so +:release+ is the Host App's explicit opt-in for
|
|
51
|
+
# host-parallel guest execution.
|
|
52
|
+
DEFAULT_GVL = :hold
|
|
53
|
+
|
|
44
54
|
def initialize(timeout: DEFAULT_TIMEOUT_SECONDS,
|
|
45
55
|
memory_limit: DEFAULT_MEMORY_LIMIT,
|
|
46
56
|
stdout_limit: DEFAULT_OUTPUT_LIMIT,
|
|
47
57
|
stderr_limit: DEFAULT_OUTPUT_LIMIT,
|
|
48
|
-
profile: DEFAULT_PROFILE
|
|
58
|
+
profile: DEFAULT_PROFILE,
|
|
59
|
+
gvl: DEFAULT_GVL)
|
|
49
60
|
timeout = normalize_timeout(timeout)
|
|
50
61
|
memory_limit = normalize_memory_limit(memory_limit)
|
|
51
62
|
stdout_limit = normalize_output_limit(stdout_limit, "stdout_limit")
|
|
52
63
|
stderr_limit = normalize_output_limit(stderr_limit, "stderr_limit")
|
|
53
64
|
profile = normalize_profile(profile)
|
|
65
|
+
gvl = normalize_gvl(gvl)
|
|
54
66
|
super
|
|
55
67
|
end
|
|
56
68
|
|
|
@@ -120,5 +132,14 @@ module Kobako
|
|
|
120
132
|
|
|
121
133
|
raise ArgumentError, "profile must be one of #{PROFILES.map(&:inspect).join(", ")}, got #{profile.inspect}"
|
|
122
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)
|
|
141
|
+
|
|
142
|
+
raise ArgumentError, "gvl must be one of #{GVL_MODES.map(&:inspect).join(", ")}, got #{gvl.inspect}"
|
|
143
|
+
end
|
|
123
144
|
end
|
|
124
145
|
end
|
|
@@ -18,10 +18,9 @@ module Kobako
|
|
|
18
18
|
# form +Binary.new(body: ...)+. Wire-form construction is the
|
|
19
19
|
# registry's responsibility.
|
|
20
20
|
class Binary < Data.define(:body)
|
|
21
|
-
#
|
|
22
|
-
#
|
|
23
|
-
|
|
24
|
-
KIND = "bytecode"
|
|
21
|
+
# Names the snippet form the guest replays this entry as. The wire's
|
|
22
|
+
# discriminant byte is assigned by the core envelope, not here.
|
|
23
|
+
KIND = :bytecode
|
|
25
24
|
end
|
|
26
25
|
end
|
|
27
26
|
end
|
|
@@ -15,13 +15,12 @@ module Kobako
|
|
|
15
15
|
# carries no mutation API. Callers (chiefly +Catalog::Snippets+)
|
|
16
16
|
# construct instances via keyword form +Source.new(name: ..., body: ...)+.
|
|
17
17
|
# Wire-form construction is the registry's responsibility: as a leaf
|
|
18
|
-
# carrier this Source stays pure and +Catalog::Snippets#
|
|
19
|
-
# its attributes off the outside rather than asking it to
|
|
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
20
|
class Source < Data.define(:name, :body)
|
|
21
|
-
#
|
|
22
|
-
#
|
|
23
|
-
|
|
24
|
-
KIND = "source"
|
|
21
|
+
# Names the snippet form the guest replays this entry as. The wire's
|
|
22
|
+
# discriminant byte is assigned by the core envelope, not here.
|
|
23
|
+
KIND = :source
|
|
25
24
|
end
|
|
26
25
|
end
|
|
27
26
|
end
|
data/lib/kobako/snippet.rb
CHANGED
|
@@ -10,8 +10,8 @@ module Kobako
|
|
|
10
10
|
# +Source+ represents a single +#preload(code:, name:)+ entry; +Binary+
|
|
11
11
|
# represents a single +#preload(binary:)+ entry. Both are plain value
|
|
12
12
|
# objects with no dependency on the +Catalog::Snippets+ registry that
|
|
13
|
-
# holds them — the registry reads their attributes externally
|
|
14
|
-
#
|
|
13
|
+
# holds them — the registry reads their attributes externally into the
|
|
14
|
+
# tuples the native side frames.
|
|
15
15
|
module Snippet
|
|
16
16
|
end
|
|
17
17
|
end
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Kobako
|
|
4
|
+
module Transport
|
|
5
|
+
# One guest-initiated dispatch, as the native side decoded it off the
|
|
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)
|
|
19
|
+
end
|
|
20
|
+
end
|
|
21
|
+
end
|