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.
Files changed (126) hide show
  1. checksums.yaml +4 -4
  2. data/.release-please-manifest.json +1 -1
  3. data/CHANGELOG.md +71 -0
  4. data/Cargo.lock +117 -140
  5. data/README.md +100 -125
  6. data/ROADMAP.md +23 -22
  7. data/SECURITY.md +1 -1
  8. data/crates/kobako-runtime/CHANGELOG.md +31 -0
  9. data/crates/kobako-runtime/Cargo.toml +2 -2
  10. data/crates/kobako-runtime/README.md +4 -3
  11. data/crates/kobako-runtime/src/error.rs +8 -9
  12. data/crates/kobako-runtime/src/lib.rs +1 -1
  13. data/crates/kobako-runtime/src/profile.rs +1 -1
  14. data/crates/kobako-runtime/src/runtime.rs +31 -17
  15. data/crates/kobako-runtime/src/snapshot.rs +13 -10
  16. data/crates/kobako-runtime/src/yielder.rs +9 -6
  17. data/crates/kobako-transport/CHANGELOG.md +28 -0
  18. data/crates/kobako-transport/Cargo.toml +1 -1
  19. data/crates/kobako-transport/README.md +21 -12
  20. data/crates/kobako-transport/src/abi.rs +11 -14
  21. data/crates/kobako-transport/src/envelope/bytes.rs +18 -6
  22. data/crates/kobako-transport/src/envelope/compile_reply.rs +139 -0
  23. data/crates/kobako-transport/src/envelope/outcome.rs +17 -16
  24. data/crates/kobako-transport/src/envelope/preamble.rs +513 -0
  25. data/crates/kobako-transport/src/envelope/reply.rs +77 -53
  26. data/crates/kobako-transport/src/envelope/request.rs +295 -0
  27. data/crates/kobako-transport/src/envelope/run.rs +1 -2
  28. data/crates/kobako-transport/src/envelope/tag.rs +49 -0
  29. data/crates/kobako-transport/src/envelope/unsupported.rs +105 -0
  30. data/crates/kobako-transport/src/envelope.rs +19 -12
  31. data/crates/kobako-transport/src/lib.rs +0 -2
  32. data/crates/kobako-wasmtime/CHANGELOG.md +44 -0
  33. data/crates/kobako-wasmtime/Cargo.toml +7 -5
  34. data/crates/kobako-wasmtime/README.md +2 -2
  35. data/crates/kobako-wasmtime/src/abi.rs +5 -8
  36. data/crates/kobako-wasmtime/src/ambient.rs +2 -2
  37. data/crates/kobako-wasmtime/src/cache.rs +25 -59
  38. data/crates/kobako-wasmtime/src/capture.rs +4 -11
  39. data/crates/kobako-wasmtime/src/config.rs +11 -14
  40. data/crates/kobako-wasmtime/src/dispatch.rs +70 -80
  41. data/crates/kobako-wasmtime/src/driver.rs +133 -86
  42. data/crates/kobako-wasmtime/src/exports.rs +88 -34
  43. data/crates/kobako-wasmtime/src/guest_mem.rs +145 -138
  44. data/crates/kobako-wasmtime/src/instance_pre.rs +11 -22
  45. data/crates/kobako-wasmtime/src/invocation.rs +55 -372
  46. data/crates/kobako-wasmtime/src/io.rs +286 -0
  47. data/crates/kobako-wasmtime/src/lib.rs +3 -24
  48. data/crates/kobako-wasmtime/src/limiter.rs +253 -0
  49. data/crates/kobako-wasmtime/src/trap.rs +32 -45
  50. data/crates/kobako-wasmtime/tests/preamble_flags.rs +69 -0
  51. data/crates/kobako-wasmtime/tests/snippet_compile.rs +94 -0
  52. data/crates/kobako-wasmtime/tests/support/mod.rs +34 -0
  53. data/crates/kobako-wasmtime/tests/unsupported_call.rs +50 -0
  54. data/data/kobako.wasm +0 -0
  55. data/ext/kobako/Cargo.toml +2 -2
  56. data/ext/kobako/src/runtime/bridge.rs +38 -47
  57. data/ext/kobako/src/runtime/errors.rs +26 -32
  58. data/ext/kobako/src/runtime/gvl.rs +4 -12
  59. data/ext/kobako/src/runtime.rs +169 -212
  60. data/lib/kobako/capture.rb +1 -4
  61. data/lib/kobako/catalog/extensions.rb +21 -59
  62. data/lib/kobako/catalog/flags.rb +41 -0
  63. data/lib/kobako/catalog/handles.rb +7 -54
  64. data/lib/kobako/catalog/services.rb +7 -61
  65. data/lib/kobako/catalog/snippets.rb +42 -66
  66. data/lib/kobako/catalog.rb +2 -10
  67. data/lib/kobako/codec/decoder.rb +9 -28
  68. data/lib/kobako/codec/encoder.rb +3 -22
  69. data/lib/kobako/codec/error.rb +6 -27
  70. data/lib/kobako/codec/ext_types.rb +72 -94
  71. data/lib/kobako/codec/handle_walk.rb +65 -130
  72. data/lib/kobako/codec/nesting.rb +3 -8
  73. data/lib/kobako/codec/state.rb +4 -10
  74. data/lib/kobako/codec/utils.rb +6 -34
  75. data/lib/kobako/codec.rb +3 -22
  76. data/lib/kobako/constant_name.rb +8 -0
  77. data/lib/kobako/context.rb +51 -112
  78. data/lib/kobako/errors.rb +51 -127
  79. data/lib/kobako/execution.rb +16 -21
  80. data/lib/kobako/extension.rb +23 -40
  81. data/lib/kobako/handle.rb +7 -12
  82. data/lib/kobako/outcome.rb +51 -80
  83. data/lib/kobako/payload/arguments.rb +1 -16
  84. data/lib/kobako/payload.rb +4 -12
  85. data/lib/kobako/pool.rb +23 -53
  86. data/lib/kobako/runtime.rb +7 -21
  87. data/lib/kobako/sandbox.rb +96 -133
  88. data/lib/kobako/sandbox_options.rb +38 -84
  89. data/lib/kobako/snippet/binary.rb +1 -16
  90. data/lib/kobako/snippet/source.rb +1 -16
  91. data/lib/kobako/snippet.rb +1 -9
  92. data/lib/kobako/transport/call.rb +1 -14
  93. data/lib/kobako/transport/dispatcher.rb +112 -195
  94. data/lib/kobako/transport/error.rb +6 -20
  95. data/lib/kobako/transport/exposure.rb +5 -10
  96. data/lib/kobako/transport/reflection.rb +39 -50
  97. data/lib/kobako/transport/run.rb +17 -71
  98. data/lib/kobako/transport/yielder.rb +21 -73
  99. data/lib/kobako/transport.rb +3 -15
  100. data/lib/kobako/unresolved.rb +4 -11
  101. data/lib/kobako/usage.rb +2 -6
  102. data/lib/kobako/version.rb +1 -1
  103. data/sig/kobako/catalog/extensions.rbs +2 -2
  104. data/sig/kobako/catalog/flags.rbs +17 -0
  105. data/sig/kobako/catalog/services.rbs +0 -6
  106. data/sig/kobako/catalog/snippets.rbs +4 -4
  107. data/sig/kobako/codec/ext_types.rbs +10 -8
  108. data/sig/kobako/codec/handle_walk.rbs +9 -7
  109. data/sig/kobako/codec/utils.rbs +2 -2
  110. data/sig/kobako/codec.rbs +2 -2
  111. data/sig/kobako/constant_name.rbs +3 -0
  112. data/sig/kobako/context.rbs +2 -3
  113. data/sig/kobako/errors.rbs +6 -0
  114. data/sig/kobako/outcome.rbs +6 -8
  115. data/sig/kobako/runtime.rbs +12 -8
  116. data/sig/kobako/sandbox.rbs +4 -0
  117. data/sig/kobako/sandbox_options.rbs +2 -6
  118. data/sig/kobako/transport/dispatcher.rbs +14 -18
  119. data/sig/kobako/transport/reflection.rbs +5 -3
  120. data/sig/kobako/transport/run.rbs +1 -3
  121. data/sig/kobako/transport/yielder.rbs +3 -3
  122. data/sig/kobako/transport.rbs +7 -6
  123. metadata +16 -4
  124. data/crates/kobako-transport/src/envelope/invocation_frames.rs +0 -241
  125. data/crates/kobako-wasmtime/src/frames.rs +0 -273
  126. data/rust-toolchain.toml +0 -15
@@ -10,168 +10,129 @@ require_relative "catalog"
10
10
  require_relative "context"
11
11
 
12
12
  module Kobako
13
- # Kobako::Sandbox — the user-facing entry point for executing guest mruby
14
- # scripts inside a wasmtime-hosted Wasm module.
15
- #
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
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
- attr_reader :wasm_path, :options
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 fresh Sandbox.
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
- # +wasm_path+ is the absolute path to the Guest Binary; defaults to the
44
- # gem-bundled +data/kobako.wasm+. Every other keyword — the four caps
45
- # (+stdout_limit+, +stderr_limit+, +timeout+, +memory_limit+) and the
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
- # Bind +object+ as the Service reachable at +path+ — a Symbol or
65
- # String of one or more +::+-separated constant-form segments
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 +object+
70
- # itself define in source, fixed at this call — nothing inherited, mixed
71
- # in, or built into the platform. An +object+ defining a private
72
- # +respond_to_guest?(name)+ decides instead, asked on every call.
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 a +path+, it declares a fillable Service:
75
- # +bind(path)+ reserves the path for +Kobako::Unresolved+, so the guest
76
- # sees the constant while the host defers the object it stands for. A
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 +ArgumentError+ when a segment is malformed, when +path+
81
- # collides with an existing binding (a name is a bound Service or a
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 one or more Extensions — each a guest idiom (+source+) paired
90
- # with an optional host +backend+, composed onto the Sandbox through
91
- # +#preload+ and +#bind+. An Extension is any object exposing
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 +ArgumentError+ for a malformed Extension, a call after the
96
- # first invocation seals registration, or — at that first invocation —
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
- raise ArgumentError, "cannot install after first Sandbox invocation" if @services.sealed?
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 on this Sandbox in one of two forms:
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
- # Subsequent invocations (+#eval+ or +#run+) replay every registered
121
- # snippet — in insertion order — against the fresh +mrb_state+
122
- # before per-invocation source or entrypoint resolution.
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
- # Returns +self+ to allow chaining.
125
- #
126
- # Raises +ArgumentError+ when neither form's keyword set is
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
- raise ArgumentError, "cannot preload after first Sandbox invocation" if @services.sealed?
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
- # Dispatch into a preloaded entrypoint constant. Delegates host
141
- # pre-flight and argument encoding to +Kobako::Transport::Run+ /
142
- # +Kobako::Transport::Run#payload+: a non-Symbol/String +target+ raises
143
- # +TypeError+, while a +target+ failing the constant pattern, a forged
144
- # +Kobako::Handle+ in +args+ / +kwargs+, or a non-Symbol +kwargs+ key
145
- # raise +ArgumentError+. The guest resolves +target+ as a top-level
146
- # constant, calls +#call+ on it with +args+ / +kwargs+, and returns the
147
- # deserialized result. The first invocation seals the Service registry
148
- # and snippet table. Runtime errors follow the same three-class
149
- # taxonomy as +#eval+.
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
- # Execute a guest mruby source string in a fresh +mrb_state+. +code+ is
156
- # the mruby source as a UTF-8 String. Returns the deserialized last
157
- # expression of the source.
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
- # The first invocation seals the Service registry and snippet table;
168
- # subsequent +#bind+ / +#preload+ calls raise +ArgumentError+.
169
- #
170
- # Raises +Kobako::TrapError+ on a Wasm trap or wire-violation fallback;
171
- # +Kobako::SandboxError+ when the guest ran to completion but failed
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
- # Construct the +Runtime+ with the requested isolation profile and
184
- # refuse one whose declared posture falls below the request —
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
- # Seal the config on the first invocation and return a fresh
196
- # per-invocation +Context+ for the verb to drive. The Context owns this
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, extensions: @extensions)
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
- # Per-invocation prologue on the config tier: seals the Service and
205
- # Extension registries on the first call (idempotent — asserting Extension
206
- # dependencies then). The Service seal is the one +#bind+ / +#preload+ /
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
- @services.seal!
211
- @extensions.seal!
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
- # Kobako::SandboxOptions — immutable Value Object holding the four
7
- # per-Sandbox configuration caps and the requested isolation profile.
8
- # Built on the +class X < Data.define(...)+ subclass form (the
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
- # The +initialize+ normalises every option before delegating to Data's
12
- # +super+: +timeout+ to Float seconds, +memory_limit+ / +stdout_limit+ /
13
- # +stderr_limit+ to positive Integer bytes. Each cap is +nil+-disablable
14
- # (an absent argument takes its DEFAULT; an explicit +nil+ leaves the
15
- # bound off), so all four behave uniformly. +profile+ is the one
16
- # non-cap: a Symbol on the PROFILES ladder naming the posture the
17
- # runtime builds, which is also the weakest the Host App accepts —
18
- # +nil+ is rejected because the weakest posture is requested as an
19
- # explicit +:permissive+. Anything that survives +SandboxOptions.new+
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
- # Default wall-clock timeout for a single invocation: 60 seconds.
20
+ # The default +timeout+: 60 seconds.
26
21
  DEFAULT_TIMEOUT_SECONDS = 60.0
27
22
 
28
- # Default cap on the per-invocation guest linear-memory delta:
29
- # 1 MiB. The mruby image's initial allocation and prior invocations'
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
- # Default per-channel capture ceiling: 1 MiB.
27
+ # The default +stdout_limit+ and +stderr_limit+: 1 MiB each.
34
28
  DEFAULT_OUTPUT_LIMIT = 1 << 20
35
29
 
36
- # The isolation ladder, weakest first — index order is rank order,
37
- # so a floor check is an index comparison.
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
- # Default isolation profile: the strictest rung — opting down to
41
- # +:permissive+ is the Host App's explicit trade.
34
+ # The default +profile+, the strictest.
42
35
  DEFAULT_PROFILE = :hermetic
43
36
 
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.
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
- # 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.
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 = normalize_memory_limit(memory_limit)
62
- stdout_limit = normalize_output_limit(stdout_limit, "stdout_limit")
63
- stderr_limit = normalize_output_limit(stderr_limit, "stderr_limit")
64
- profile = normalize_profile(profile)
65
- gvl = normalize_gvl(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
- # Enforce the requested +profile+ as the floor against +declared+ —
70
- # the posture a runtime reports having built — so a runtime that
71
- # cannot honor the request fails construction with
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
- # Coerce +timeout+ into the Float seconds the ext expects, or +nil+
86
- # to mean the cap is disabled. Any finite non-positive value is
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
- # Coerce +memory_limit+ into the byte cap the ext expects, or +nil+
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
- # Validate +profile+ against the PROFILES ladder. Unlike the caps
127
- # there is no +nil+ form: the weakest posture is requested as an
128
- # explicit +:permissive+, so anything off the ladder — +nil+
129
- # included — is rejected.
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, "gvl must be one of #{GVL_MODES.map(&:inspect).join(", ")}, got #{gvl.inspect}"
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
- # Kobako::Snippet::Binary — value object representing a single
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
- # Kobako::Snippet::Source — value object representing a single
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
@@ -4,14 +4,6 @@ require_relative "snippet/binary"
4
4
  require_relative "snippet/source"
5
5
 
6
6
  module Kobako
7
- # Kobako::Snippet — value-object family for preloaded snippet entries
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
- # 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)
5
+ class Call < Data.define(:target, :method_name, :block_given, :payload) # :nodoc:
19
6
  end
20
7
  end
21
8
  end