kobako 0.30.0 → 0.31.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (266) hide show
  1. checksums.yaml +4 -4
  2. data/.release-please-manifest.json +1 -1
  3. data/CHANGELOG.md +335 -0
  4. data/Cargo.lock +469 -11
  5. data/README.md +43 -3
  6. data/ROADMAP.md +1 -1
  7. data/crates/kobako-runtime/CHANGELOG.md +219 -0
  8. data/crates/kobako-runtime/Cargo.toml +51 -3
  9. data/crates/kobako-runtime/README.md +7 -4
  10. data/crates/kobako-runtime/src/cred.rs +124 -0
  11. data/crates/kobako-runtime/src/dispatch.rs +4 -5
  12. data/crates/kobako-runtime/src/endpoint/buffer.rs +153 -44
  13. data/crates/kobako-runtime/src/endpoint/dispatch.rs +80 -41
  14. data/crates/kobako-runtime/src/endpoint/invoke.rs +23 -36
  15. data/crates/kobako-runtime/src/endpoint.rs +137 -36
  16. data/crates/kobako-runtime/src/errno.rs +391 -0
  17. data/crates/kobako-runtime/src/error.rs +31 -10
  18. data/crates/kobako-runtime/src/filesystem/anon_inodes.rs +52 -0
  19. data/crates/kobako-runtime/src/filesystem/bind/lookup.rs +52 -14
  20. data/crates/kobako-runtime/src/filesystem/bind/walk.rs +19 -20
  21. data/crates/kobako-runtime/src/filesystem/bind.rs +437 -245
  22. data/crates/kobako-runtime/src/filesystem/overlay/file.rs +6 -6
  23. data/crates/kobako-runtime/src/filesystem/overlay/path.rs +43 -65
  24. data/crates/kobako-runtime/src/filesystem/overlay/stack/copy_up.rs +9 -16
  25. data/crates/kobako-runtime/src/filesystem/overlay/stack/dir.rs +47 -72
  26. data/crates/kobako-runtime/src/filesystem/overlay/stack/namei.rs +12 -12
  27. data/crates/kobako-runtime/src/filesystem/overlay/stack/readdir.rs +4 -7
  28. data/crates/kobako-runtime/src/filesystem/overlay/stack.rs +121 -37
  29. data/crates/kobako-runtime/src/filesystem/overlay.rs +302 -343
  30. data/crates/kobako-runtime/src/filesystem/pipe.rs +12 -9
  31. data/crates/kobako-runtime/src/filesystem/rdir.rs +15 -32
  32. data/crates/kobako-runtime/src/filesystem/tmpfs/seed.rs +32 -0
  33. data/crates/kobako-runtime/src/filesystem/tmpfs/tmpfile.rs +3 -3
  34. data/crates/kobako-runtime/src/filesystem/tmpfs/whiteout.rs +57 -35
  35. data/crates/kobako-runtime/src/filesystem/tmpfs/xattr.rs +2 -2
  36. data/crates/kobako-runtime/src/filesystem/tmpfs.rs +172 -160
  37. data/crates/kobako-runtime/src/filesystem/wrap.rs +36 -52
  38. data/crates/kobako-runtime/src/filesystem.rs +6 -3
  39. data/crates/kobako-runtime/src/fork.rs +96 -0
  40. data/crates/kobako-runtime/src/grant.rs +3 -0
  41. data/crates/kobako-runtime/src/hashtable.rs +4 -2
  42. data/crates/kobako-runtime/src/lib.rs +18 -7
  43. data/crates/kobako-runtime/src/limits.rs +195 -0
  44. data/crates/kobako-runtime/src/locking.rs +10 -0
  45. data/crates/kobako-runtime/src/memcg.rs +383 -0
  46. data/crates/kobako-runtime/src/mount.rs +67 -9
  47. data/crates/kobako-runtime/src/net/backends.rs +43 -0
  48. data/crates/kobako-runtime/src/net/connections.rs +179 -0
  49. data/crates/kobako-runtime/src/net/credential.rs +125 -0
  50. data/crates/kobako-runtime/src/net/egress.rs +160 -0
  51. data/crates/kobako-runtime/src/net/family.rs +181 -0
  52. data/crates/kobako-runtime/src/net/http.rs +2733 -0
  53. data/crates/kobako-runtime/src/net/lookup.rs +285 -0
  54. data/crates/kobako-runtime/src/net/mock.rs +251 -0
  55. data/crates/kobako-runtime/src/net/namespace.rs +1366 -0
  56. data/crates/kobako-runtime/src/net/reactor.rs +531 -0
  57. data/crates/kobako-runtime/src/net/resolv/darwin.rs +275 -0
  58. data/crates/kobako-runtime/src/net/resolv/stub/tests.rs +469 -0
  59. data/crates/kobako-runtime/src/net/resolv/stub.rs +536 -0
  60. data/crates/kobako-runtime/src/net/resolv/windows.rs +248 -0
  61. data/crates/kobako-runtime/src/net/resolv.rs +227 -0
  62. data/crates/kobako-runtime/src/net/socket.rs +870 -0
  63. data/crates/kobako-runtime/src/net/tcp.rs +1588 -0
  64. data/crates/kobako-runtime/src/net/tls.rs +58 -0
  65. data/crates/kobako-runtime/src/net/transport.rs +279 -0
  66. data/crates/kobako-runtime/src/net/udp.rs +1003 -0
  67. data/crates/kobako-runtime/src/net.rs +42 -0
  68. data/crates/kobako-runtime/src/network/builder.rs +177 -0
  69. data/crates/kobako-runtime/src/network/ruleset.rs +777 -0
  70. data/crates/kobako-runtime/src/network.rs +551 -0
  71. data/crates/kobako-runtime/src/policy.rs +16 -0
  72. data/crates/kobako-runtime/src/profile.rs +29 -144
  73. data/crates/kobako-runtime/src/runtime.rs +3 -5
  74. data/crates/kobako-runtime/src/sched.rs +130 -29
  75. data/crates/kobako-runtime/src/security/entry_policy.rs +22 -0
  76. data/crates/kobako-runtime/src/{vfs/cred.rs → security/hooks.rs} +52 -144
  77. data/crates/kobako-runtime/src/security/mount_policy.rs +158 -0
  78. data/crates/kobako-runtime/src/security.rs +101 -196
  79. data/crates/kobako-runtime/src/snapshot.rs +121 -2
  80. data/crates/kobako-runtime/src/vfs/attr.rs +5 -5
  81. data/crates/kobako-runtime/src/vfs/dentry.rs +143 -89
  82. data/crates/kobako-runtime/src/vfs/file.rs +199 -203
  83. data/crates/kobako-runtime/src/vfs/inode.rs +41 -45
  84. data/crates/kobako-runtime/src/vfs/kern_path.rs +38 -42
  85. data/crates/kobako-runtime/src/vfs/mount.rs +10 -9
  86. data/crates/kobako-runtime/src/vfs/namei.rs +829 -700
  87. data/crates/kobako-runtime/src/vfs/namespace.rs +162 -0
  88. data/crates/kobako-runtime/src/vfs/open_files.rs +69 -0
  89. data/crates/kobako-runtime/src/vfs/openat2.rs +3 -3
  90. data/crates/kobako-runtime/src/vfs/path.rs +6 -6
  91. data/crates/kobako-runtime/src/vfs/readdir.rs +6 -6
  92. data/crates/kobako-runtime/src/vfs/stat.rs +159 -23
  93. data/crates/kobako-runtime/src/vfs/super_block.rs +18 -14
  94. data/crates/kobako-runtime/src/vfs/utimes.rs +37 -27
  95. data/crates/kobako-runtime/src/vfs.rs +9 -6
  96. data/crates/kobako-runtime/src/wait.rs +356 -0
  97. data/crates/kobako-transport/CHANGELOG.md +31 -0
  98. data/crates/kobako-transport/Cargo.toml +1 -1
  99. data/crates/kobako-transport/README.md +3 -4
  100. data/crates/kobako-transport/src/abi.rs +78 -72
  101. data/crates/kobako-transport/src/envelope/bytes.rs +17 -6
  102. data/crates/kobako-transport/src/envelope/call.rs +32 -4
  103. data/crates/kobako-transport/src/envelope/error_record.rs +21 -0
  104. data/crates/kobako-transport/src/envelope/name.rs +99 -0
  105. data/crates/kobako-transport/src/envelope/outcome.rs +9 -9
  106. data/crates/kobako-transport/src/envelope/preamble.rs +24 -21
  107. data/crates/kobako-transport/src/envelope/reply.rs +27 -27
  108. data/crates/kobako-transport/src/envelope/unsupported.rs +10 -10
  109. data/crates/kobako-transport/src/envelope.rs +13 -8
  110. data/crates/kobako-wasmtime/CHANGELOG.md +161 -0
  111. data/crates/kobako-wasmtime/Cargo.toml +26 -7
  112. data/crates/kobako-wasmtime/README.md +3 -2
  113. data/crates/kobako-wasmtime/build/preview2.rs +1339 -0
  114. data/crates/kobako-wasmtime/build.rs +16 -0
  115. data/crates/kobako-wasmtime/src/abi.rs +17 -37
  116. data/crates/kobako-wasmtime/src/ambient.rs +33 -18
  117. data/crates/kobako-wasmtime/src/cache.rs +23 -15
  118. data/crates/kobako-wasmtime/src/config.rs +52 -68
  119. data/crates/kobako-wasmtime/src/driver.rs +48 -63
  120. data/crates/kobako-wasmtime/src/exports.rs +143 -17
  121. data/crates/kobako-wasmtime/src/guest.rs +12 -6
  122. data/crates/kobako-wasmtime/src/guest_binary.rs +14 -0
  123. data/crates/kobako-wasmtime/src/imports.rs +42 -18
  124. data/crates/kobako-wasmtime/src/instance_pre.rs +4 -4
  125. data/crates/kobako-wasmtime/src/invocation.rs +74 -12
  126. data/crates/kobako-wasmtime/src/lib.rs +5 -3
  127. data/crates/kobako-wasmtime/src/limiter.rs +70 -5
  128. data/crates/kobako-wasmtime/src/preview1/descriptors.rs +309 -0
  129. data/crates/kobako-wasmtime/src/preview1/differential/mount.rs +5 -15
  130. data/crates/kobako-wasmtime/src/preview1/differential.rs +27 -31
  131. data/crates/kobako-wasmtime/src/preview1/errno.rs +55 -66
  132. data/crates/kobako-wasmtime/src/preview1/fd.rs +327 -296
  133. data/crates/kobako-wasmtime/src/preview1/memory.rs +21 -12
  134. data/crates/kobako-wasmtime/src/preview1/mount_view/wrap.rs +34 -53
  135. data/crates/kobako-wasmtime/src/preview1/mount_view.rs +231 -217
  136. data/crates/kobako-wasmtime/src/preview1/path.rs +56 -121
  137. data/crates/kobako-wasmtime/src/preview1/poll.rs +244 -120
  138. data/crates/kobako-wasmtime/src/preview1/sock.rs +10 -8
  139. data/crates/kobako-wasmtime/src/preview1/testsuite.rs +5 -15
  140. data/crates/kobako-wasmtime/src/preview1.rs +46 -42
  141. data/crates/kobako-wasmtime/src/preview2/abi.rs +227 -0
  142. data/crates/kobako-wasmtime/src/preview2/error.rs +118 -0
  143. data/crates/kobako-wasmtime/src/preview2/filesystem.rs +122 -0
  144. data/crates/kobako-wasmtime/src/preview2/host.rs +3959 -0
  145. data/crates/kobako-wasmtime/src/preview2/http.rs +1503 -0
  146. data/crates/kobako-wasmtime/src/preview2/io.rs +315 -0
  147. data/crates/kobako-wasmtime/src/preview2/sockets.rs +379 -0
  148. data/crates/kobako-wasmtime/src/preview2/table.rs +305 -0
  149. data/crates/kobako-wasmtime/src/preview2.rs +57 -0
  150. data/crates/kobako-wasmtime/src/trap.rs +24 -9
  151. data/crates/kobako-wasmtime/src/wasi.rs +210 -81
  152. data/crates/kobako-wasmtime/wit/deps/cli.wit +233 -0
  153. data/crates/kobako-wasmtime/wit/deps/clocks.wit +162 -0
  154. data/crates/kobako-wasmtime/wit/deps/filesystem.wit +589 -0
  155. data/crates/kobako-wasmtime/wit/deps/http.wit +733 -0
  156. data/crates/kobako-wasmtime/wit/deps/io.wit +299 -0
  157. data/crates/kobako-wasmtime/wit/deps/random.wit +92 -0
  158. data/crates/kobako-wasmtime/wit/deps/sockets.wit +949 -0
  159. data/data/kobako.wasm +0 -0
  160. data/ext/kobako/Cargo.toml +1 -1
  161. data/ext/kobako/src/{runtime/errors.rs → errors.rs} +34 -26
  162. data/ext/kobako/src/{runtime/escape.rs → escape.rs} +42 -11
  163. data/ext/kobako/src/{runtime/filesystem → filesystem}/grants.rs +30 -26
  164. data/ext/kobako/src/{runtime/filesystem → filesystem}/ruby.rs +149 -168
  165. data/ext/kobako/src/{runtime/filesystem.rs → filesystem.rs} +24 -15
  166. data/ext/kobako/src/gvl.rs +306 -0
  167. data/ext/kobako/src/lib.rs +64 -3
  168. data/ext/kobako/src/network/mock.rs +187 -0
  169. data/ext/kobako/src/network.rs +591 -0
  170. data/ext/kobako/src/runtime/bridge.rs +30 -29
  171. data/ext/kobako/src/runtime/snapshot.rs +4 -4
  172. data/ext/kobako/src/runtime.rs +221 -143
  173. data/ext/kobako/src/wire.rs +42 -0
  174. data/lib/kobako/catalog/extensions.rb +14 -2
  175. data/lib/kobako/catalog/flags.rb +3 -5
  176. data/lib/kobako/catalog/grants.rb +5 -4
  177. data/lib/kobako/catalog/handles.rb +5 -27
  178. data/lib/kobako/catalog/mount.rb +11 -3
  179. data/lib/kobako/catalog/network.rb +35 -0
  180. data/lib/kobako/catalog/replacements.rb +28 -0
  181. data/lib/kobako/catalog/services.rb +5 -19
  182. data/lib/kobako/catalog/snippets.rb +3 -18
  183. data/lib/kobako/catalog.rb +3 -0
  184. data/lib/kobako/codec/{utils.rb → boundary.rb} +4 -2
  185. data/lib/kobako/codec/decoder.rb +4 -4
  186. data/lib/kobako/codec/ext_types.rb +3 -3
  187. data/lib/kobako/codec/handle_walk.rb +3 -43
  188. data/lib/kobako/codec/value_budget.rb +132 -40
  189. data/lib/kobako/codec.rb +1 -1
  190. data/lib/kobako/context.rb +24 -11
  191. data/lib/kobako/errors.rb +15 -12
  192. data/lib/kobako/extension.rb +3 -2
  193. data/lib/kobako/filesystem.rb +9 -2
  194. data/lib/kobako/handle.rb +2 -2
  195. data/lib/kobako/names.rb +28 -0
  196. data/lib/kobako/network.rb +210 -0
  197. data/lib/kobako/outcome.rb +7 -7
  198. data/lib/kobako/pool.rb +45 -23
  199. data/lib/kobako/runtime.rb +3 -2
  200. data/lib/kobako/sandbox.rb +92 -35
  201. data/lib/kobako/sandbox_options.rb +41 -106
  202. data/lib/kobako/transport/argument_wrap.rb +64 -0
  203. data/lib/kobako/transport/block_failure.rb +1 -1
  204. data/lib/kobako/transport/dispatcher.rb +6 -6
  205. data/lib/kobako/transport/exposure.rb +26 -15
  206. data/lib/kobako/transport/reflection.rb +21 -4
  207. data/lib/kobako/transport/run.rb +8 -23
  208. data/lib/kobako/transport/yielder.rb +2 -9
  209. data/lib/kobako/transport.rb +1 -0
  210. data/lib/kobako/version.rb +1 -1
  211. data/lib/kobako.rb +6 -2
  212. data/release-please-config.json +15 -0
  213. data/sig/core/thread.rbs +6 -0
  214. data/sig/kobako/catalog/extensions.rbs +2 -0
  215. data/sig/kobako/catalog/grants.rbs +7 -4
  216. data/sig/kobako/catalog/handles.rbs +0 -2
  217. data/sig/kobako/catalog/mount.rbs +3 -1
  218. data/sig/kobako/catalog/network.rbs +20 -0
  219. data/sig/kobako/catalog/replacements.rbs +14 -0
  220. data/sig/kobako/catalog/services.rbs +0 -4
  221. data/sig/kobako/catalog/snippets.rbs +0 -2
  222. data/sig/kobako/codec/{utils.rbs → boundary.rbs} +2 -2
  223. data/sig/kobako/codec/handle_walk.rbs +0 -6
  224. data/sig/kobako/codec/value_budget.rbs +47 -4
  225. data/sig/kobako/context.rbs +3 -1
  226. data/sig/kobako/errors.rbs +4 -4
  227. data/sig/kobako/filesystem.rbs +21 -3
  228. data/sig/kobako/network.rbs +115 -0
  229. data/sig/kobako/outcome.rbs +1 -1
  230. data/sig/kobako/pool.rbs +7 -3
  231. data/sig/kobako/runtime.rbs +32 -3
  232. data/sig/kobako/sandbox.rbs +19 -2
  233. data/sig/kobako/sandbox_options.rbs +29 -38
  234. data/sig/kobako/transport/argument_wrap.rbs +15 -0
  235. data/sig/kobako/transport/error.rbs +1 -1
  236. data/sig/kobako/transport/exposure.rbs +2 -4
  237. data/sig/kobako/transport/reflection.rbs +10 -0
  238. data/sig/kobako/transport/run.rbs +0 -2
  239. data/sig/kobako/transport/yielder.rbs +0 -3
  240. data/sig/kobako/transport.rbs +1 -1
  241. data/sig/kobako.rbs +13 -0
  242. metadata +84 -35
  243. data/crates/kobako-runtime/src/endpoint/capture.rs +0 -115
  244. data/crates/kobako-runtime/src/endpoint/memory_cap.rs +0 -238
  245. data/crates/kobako-runtime/src/filesystem/path.rs +0 -21
  246. data/crates/kobako-runtime/src/vfs/errno.rs +0 -153
  247. data/crates/kobako-runtime/tests/linux_baseline/calls.rs +0 -171
  248. data/crates/kobako-runtime/tests/linux_baseline/concurrent.rs +0 -276
  249. data/crates/kobako-runtime/tests/linux_baseline/kobako.rs +0 -323
  250. data/crates/kobako-runtime/tests/linux_baseline/linux-baseline-expected.txt +0 -16
  251. data/crates/kobako-runtime/tests/linux_baseline/linux.rs +0 -449
  252. data/crates/kobako-runtime/tests/linux_baseline/main.rs +0 -611
  253. data/crates/kobako-runtime/tests/linux_baseline/permissions.rs +0 -236
  254. data/crates/kobako-wasmtime/src/descriptors.rs +0 -200
  255. data/crates/kobako-wasmtime/tests/integration/compile_posture.rs +0 -43
  256. data/crates/kobako-wasmtime/tests/integration/guest_file.rs +0 -1085
  257. data/crates/kobako-wasmtime/tests/integration/main.rs +0 -29
  258. data/crates/kobako-wasmtime/tests/integration/preamble_flags.rs +0 -70
  259. data/crates/kobako-wasmtime/tests/integration/snippet_compile.rs +0 -95
  260. data/crates/kobako-wasmtime/tests/integration/support/mod.rs +0 -30
  261. data/crates/kobako-wasmtime/tests/integration/unanswered_call.rs +0 -86
  262. data/crates/kobako-wasmtime/tests/integration/unsupported_call.rs +0 -51
  263. data/ext/kobako/src/runtime/gvl.rs +0 -147
  264. data/lib/kobako/constant_name.rb +0 -8
  265. data/sig/kobako/constant_name.rbs +0 -3
  266. /data/ext/kobako/src/{runtime/pinned_value.rs → pinned_value.rs} +0 -0
@@ -1,7 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require "delegate"
4
-
5
3
  require_relative "../handle"
6
4
  require_relative "../transport/exposure"
7
5
 
@@ -21,9 +19,12 @@ module Kobako
21
19
 
22
20
  # The entry records the object's Exposure as it stands at mint, so a
23
21
  # call made through this Handle is authorized against the reference
24
- # the guest was given.
22
+ # the guest was given. The mint point asks the refusal itself, so it
23
+ # holds on every path a Handle is issued.
25
24
  def alloc(object)
26
- reject_unwrappable(object)
25
+ refused = Kobako::Transport::Exposure.refused_class_of(object)
26
+ raise SandboxError, "a #{refused} cannot cross as a Capability Handle" if refused
27
+
27
28
  ensure_capacity
28
29
  id = @next_id
29
30
  @entries[id] = Kobako::Transport::Exposure.of(object, @surfaces)
@@ -48,29 +49,6 @@ module Kobako
48
49
 
49
50
  private
50
51
 
51
- # Refuse to mint a Capability Handle for an object whose reachable
52
- # surface is not Service behaviour: a +Binding+ / +Method+ /
53
- # +UnboundMethod+ hands the guest a callable proxy onto host
54
- # reflection (a returned +Binding+ reaches +Binding#eval+); a +Class+
55
- # or +Module+ hands over its class-level API (+File.popen+ / +read+,
56
- # +Kernel.system+), which the owner-based dispatch floor cannot see
57
- # because a singleton-class owner matches no core-module list; a
58
- # +Delegator+ (+SimpleDelegator+, +DelegateClass+, +WeakRef+,
59
- # +Tempfile+) is a transparent forwarder whose public +method_missing+
60
- # binds and calls the private method the guest names (+Kernel#system+),
61
- # a surface the floor reads as ordinary Service behaviour. Raising here
62
- # keeps the rule at the single mint point, so it holds on both the
63
- # Service-return and the +#run+ host→guest auto-wrap paths.
64
- def reject_unwrappable(object)
65
- case object
66
- when Binding, Method, UnboundMethod, Module, Delegator
67
- # Delegator < BasicObject exposes no static +#class+, so name the
68
- # rejected object's class through Object's own.
69
- kind = Object.instance_method(:class).bind_call(object)
70
- raise SandboxError, "a #{kind} cannot cross as a Capability Handle"
71
- end
72
- end
73
-
74
52
  def ensure_capacity
75
53
  cap = Kobako::Handle::MAX_ID
76
54
  return unless @next_id > cap
@@ -3,13 +3,21 @@
3
3
  module Kobako
4
4
  module Catalog
5
5
  class Mount < Data.define(:at, :access, :filesystem, :policy) # :nodoc:
6
- # The path a Context replaces this Mount at.
7
- def key = at
6
+ # The Mount at +at+, refused by the rules an invocation builds it with,
7
+ # so a mistake raises where it was written.
8
+ def initialize(at:, access:, filesystem:, policy:)
9
+ Kobako::Filesystem.validate_mount(at, access, filesystem)
10
+ super
11
+ end
12
+
13
+ # The kind and path a Context replaces this Mount at, so a Grant of
14
+ # another kind never meets it.
15
+ def key = [:mount, at]
8
16
 
9
17
  # Why +other+ cannot be mounted beside this Mount, or +nil+: a path
10
18
  # means one Mount, as Docker refuses a duplicate mount point.
11
19
  def conflict(other)
12
- "path #{at.inspect} is already mounted" if other.at == at
20
+ "path #{at.inspect} is already mounted" if other.key == key
13
21
  end
14
22
 
15
23
  # The Mount as the native side reads it.
@@ -0,0 +1,35 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Kobako
4
+ module Catalog
5
+ class Network < Data.define(:name, :entry) # :nodoc:
6
+ # The entry named +name+: +entry+ as it stands, or a Builder adding what
7
+ # the block declares to it. Raises TypeError for a name of another class,
8
+ # and ArgumentError for an empty name, which no Context could replace it
9
+ # by.
10
+ def initialize(name:, entry:)
11
+ name = Kobako.to_name(name, "network name").freeze
12
+ Kobako::Network.validate_name(name)
13
+
14
+ if block_given?
15
+ entry = Kobako::Network::Builder.new(entry)
16
+ yield entry
17
+ end
18
+ super
19
+ end
20
+
21
+ # The kind and name a Context replaces this entry by, so a Grant of
22
+ # another kind never meets it.
23
+ def key = [:network, name]
24
+
25
+ # Why +other+ cannot be given beside this entry, or +nil+: a name means
26
+ # one entry, as a path means one Mount.
27
+ def conflict(other)
28
+ "network entry #{name.inspect} is already given" if other.key == key
29
+ end
30
+
31
+ # The entry as the native side reads it.
32
+ def to_native = [name, entry]
33
+ end
34
+ end
35
+ end
@@ -0,0 +1,28 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Kobako
4
+ module Catalog
5
+ # The Grants one invocation's Context replaced, each standing in for the
6
+ # Sandbox's Grant at its key.
7
+ class Replacements # :nodoc:
8
+ def initialize(grants)
9
+ @grants = grants
10
+ @replaced = {} # : Hash[Grants::key, Grants::grant]
11
+ end
12
+
13
+ # Have +grant+ stand in for the Grant at its key, raising ArgumentError
14
+ # with +refusal+ when the Sandbox holds none there, so a replacement
15
+ # never adds a Grant.
16
+ def replace(grant, refusal)
17
+ raise ArgumentError, refusal unless @grants.include?(grant)
18
+
19
+ @replaced[grant.key] = grant
20
+ nil
21
+ end
22
+
23
+ # The Sandbox's Grants with each replacement in place, as Grants#entries
24
+ # gives them.
25
+ def entries = @grants.entries(@replaced)
26
+ end
27
+ end
28
+ end
@@ -1,15 +1,11 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require_relative "../constant_name"
4
3
  require_relative "../errors"
5
4
  require_relative "../transport/exposure"
6
5
 
7
6
  module Kobako
8
7
  module Catalog
9
8
  class Services # :nodoc:
10
- # The Guest Binary keeps this namespace for constants it may add later.
11
- RESERVED_NAMESPACE = "Kobako"
12
-
13
9
  def initialize
14
10
  @bindings = {} # : Hash[String, Kobako::Transport::Exposure]
15
11
  end
@@ -17,9 +13,8 @@ module Kobako
17
13
  # The binding records +object+'s Exposure as it stands now.
18
14
  def bind(path, object)
19
15
  path_str = validate_path(path)
20
- if reserved?(path_str)
21
- raise ArgumentError,
22
- "Service path #{path_str} is in the reserved #{RESERVED_NAMESPACE} namespace"
16
+ if Kobako.reserved_path?(path_str)
17
+ raise ArgumentError, "Service path #{path_str} is in the reserved Kobako namespace"
23
18
  end
24
19
  raise ArgumentError, "Service path #{path_str} conflicts with an existing binding" if collision?(path_str)
25
20
 
@@ -47,25 +42,16 @@ module Kobako
47
42
  private
48
43
 
49
44
  def validate_path(path)
50
- path_str = path.to_s
51
- segments = path_str.split("::", -1)
52
- return path_str if !segments.empty? && segments.all? { |seg| CONSTANT_NAME.match?(seg) }
45
+ path_str = Kobako.to_name(path, "bind path")
46
+ return path_str if Kobako.constant_path?(path_str)
53
47
 
54
48
  raise ArgumentError,
55
49
  "bind path must be constant-form segments joined by '::' (got #{path.inspect})"
56
50
  end
57
51
 
58
- def reserved?(path)
59
- path.split("::").first == RESERVED_NAMESPACE
60
- end
61
-
62
52
  # Keeps a name from being both a bound Service and a grouping prefix.
63
53
  def collision?(path)
64
- @bindings.each_key.any? do |existing|
65
- existing == path ||
66
- existing.start_with?("#{path}::") ||
67
- path.start_with?("#{existing}::")
68
- end
54
+ @bindings.each_key.any? { |bound| Kobako.paths_collide?(path, bound) }
69
55
  end
70
56
  end
71
57
  end
@@ -1,6 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require_relative "../constant_name"
4
3
  require_relative "../snippet"
5
4
 
6
5
  module Kobako
@@ -52,7 +51,7 @@ module Kobako
52
51
  code, name = ensure_source_args(code, name)
53
52
  body = Catalog.to_utf8(code, "code")
54
53
 
55
- name_sym = normalize_name(name)
54
+ name_sym = Kobako.to_constant_name(name, "snippet name")
56
55
  if @entries.any? { |e| e.is_a?(Snippet::Source) && e.name == name_sym }
57
56
  raise ArgumentError, "snippet #{name_sym.inspect} already preloaded"
58
57
  end
@@ -65,7 +64,7 @@ module Kobako
65
64
  # as a type error rather than a missing keyword.
66
65
  def ensure_source_args(code, name)
67
66
  raise ArgumentError, "missing keyword: code: + name:, or binary:" if code.nil? && name.nil?
68
- raise ArgumentError, "code must be a String, got #{code.class}" unless code.is_a?(String)
67
+ raise TypeError, "code must be a String, got #{code.class}" unless code.is_a?(String)
69
68
  raise ArgumentError, "missing keyword: name:" if name.nil?
70
69
 
71
70
  [code, name]
@@ -74,7 +73,7 @@ module Kobako
74
73
  # Kept as bytes whatever encoding the caller tagged them with, since
75
74
  # the guest reads them as bytecode rather than text.
76
75
  def register_binary(bytes)
77
- raise ArgumentError, "binary must be a String, got #{bytes.class}" unless bytes.is_a?(String)
76
+ raise TypeError, "binary must be a String, got #{bytes.class}" unless bytes.is_a?(String)
78
77
 
79
78
  @entries << Snippet::Bytecode.new(body: bytes.dup.force_encoding(Encoding::ASCII_8BIT))
80
79
  nil
@@ -91,20 +90,6 @@ module Kobako
91
90
  [Snippet::Bytecode::KIND, nil, entry.body]
92
91
  end
93
92
  end
94
-
95
- def normalize_name(name)
96
- unless name.is_a?(Symbol) || name.is_a?(String)
97
- raise ArgumentError, "snippet name must be a Symbol or String, got #{name.class}"
98
- end
99
-
100
- name_str = name.to_s
101
- unless CONSTANT_NAME.match?(name_str)
102
- raise ArgumentError,
103
- "snippet name must match #{CONSTANT_NAME.inspect} (got #{name.inspect})"
104
- end
105
-
106
- name_str.to_sym
107
- end
108
93
  end
109
94
  end
110
95
  end
@@ -1,12 +1,15 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ require_relative "names"
3
4
  require_relative "catalog/handles"
4
5
  require_relative "catalog/services"
5
6
  require_relative "catalog/snippets"
6
7
  require_relative "catalog/flags"
7
8
  require_relative "catalog/extensions"
8
9
  require_relative "catalog/mount"
10
+ require_relative "catalog/network"
9
11
  require_relative "catalog/grants"
12
+ require_relative "catalog/replacements"
10
13
 
11
14
  module Kobako
12
15
  module Catalog # :nodoc:
@@ -4,7 +4,9 @@ require_relative "error"
4
4
 
5
5
  module Kobako
6
6
  module Codec
7
- module Utils # :nodoc:
7
+ # What the codec refuses as it reads a value off the wire, each refusal
8
+ # raised as a Codec error.
9
+ module Boundary # :nodoc:
8
10
  def self.assert_utf8(string, label)
9
11
  return if string.valid_encoding?
10
12
 
@@ -14,7 +16,7 @@ module Kobako
14
16
  # Only for a value object built outside a Decoder.decode block, whose
15
17
  # rescue already maps the same way; a host-layer +ArgumentError+
16
18
  # elsewhere should propagate unchanged.
17
- def self.with_boundary
19
+ def self.within
18
20
  yield
19
21
  rescue ::ArgumentError => e
20
22
  raise InvalidTypeError, e.message
@@ -4,14 +4,14 @@ require "msgpack"
4
4
 
5
5
  require_relative "error"
6
6
  require_relative "ext_types"
7
- require_relative "utils"
7
+ require_relative "boundary"
8
8
 
9
9
  module Kobako
10
10
  module Codec
11
11
  module Decoder # :nodoc:
12
12
  # The block runs inside this method's rescue, so a Value Object built
13
13
  # from the decoded payload reports a broken invariant as
14
- # InvalidTypeError without its own Utils.with_boundary.
14
+ # InvalidTypeError without its own Boundary.within.
15
15
  def self.decode(bytes)
16
16
  ValueBudget.assert_within(bytes, State.current.value_budget)
17
17
  value = FACTORY.load(bytes.b)
@@ -35,13 +35,13 @@ module Kobako
35
35
  # returns UTF-8-tagged Strings for
36
36
  # str family but does not validate the bytes; +bin+ family decodes
37
37
  # to ASCII-8BIT. Walk the tree once and reject invalid UTF-8 in any
38
- # str-typed leaf via Utils.assert_utf8.
38
+ # str-typed leaf via Boundary.assert_utf8.
39
39
  class << self
40
40
  private
41
41
 
42
42
  def validate_utf8(value)
43
43
  case value
44
- when String then Utils.assert_utf8(value, "str payload") if value.encoding == Encoding::UTF_8
44
+ when String then Boundary.assert_utf8(value, "str payload") if value.encoding == Encoding::UTF_8
45
45
  when Array then value.each { |v| validate_utf8(v) }
46
46
  when Hash
47
47
  value.each do |key, val|
@@ -3,7 +3,7 @@
3
3
  require "msgpack"
4
4
 
5
5
  require_relative "error"
6
- require_relative "utils"
6
+ require_relative "boundary"
7
7
  require_relative "state"
8
8
  require_relative "../handle"
9
9
 
@@ -49,7 +49,7 @@ module Kobako
49
49
  # unpacker would otherwise apply to invalid bytes.
50
50
  def unpack_symbol(payload)
51
51
  name = payload.b.force_encoding(Encoding::UTF_8)
52
- Utils.assert_utf8(name, "Symbol payload")
52
+ Boundary.assert_utf8(name, "Symbol payload")
53
53
  name.to_sym
54
54
  end
55
55
 
@@ -66,7 +66,7 @@ module Kobako
66
66
  raise InvalidTypeError, "Handle payload must be 4 bytes, got #{bytes.bytesize}" unless bytes.bytesize == 4
67
67
 
68
68
  id = bytes.unpack1("N") # : Integer
69
- Codec::Utils.with_boundary { Kobako::Handle.restore(id) }
69
+ Codec::Boundary.within { Kobako::Handle.restore(id) }
70
70
  end
71
71
 
72
72
  def register_symbol(factory)
@@ -26,22 +26,9 @@ module Kobako
26
26
  primitive_type?(value) || container_representable?(value, depth)
27
27
  end
28
28
 
29
- # Only Hash values are wrapped: a non-representable key raises rather
30
- # than crossing as a token the guest→host restore walk could not
31
- # round-trip.
32
- def self.deep_wrap(value, handles, depth = 0)
33
- guard_nesting(depth)
34
- case value
35
- when ::Array then value.map { |element| deep_wrap(element, handles, depth + 1) }
36
- when ::Hash then deep_wrap_hash(value, handles, depth)
37
- else
38
- representable?(value) ? value : handles.alloc(value)
39
- end
40
- end
41
-
42
- # The inverse of #deep_wrap. A Handle here was decoded off the wire,
43
- # never forged by the guest, and +handles.fetch+ refuses an id with no
44
- # live binding.
29
+ # Each Handle a decoded value holds, as the object it stands for. A
30
+ # Handle here was decoded off the wire, never forged by the guest, and
31
+ # +handles.fetch+ refuses an id with no live binding.
45
32
  def self.deep_restore(value, handles)
46
33
  case value
47
34
  when ::Array then value.map { |element| deep_restore(element, handles) }
@@ -59,33 +46,6 @@ module Kobako
59
46
  class << self
60
47
  private
61
48
 
62
- # A reference cycle necessarily trips the nesting bound, so a +#run+
63
- # argument fails as a clean +Kobako::SandboxError+ rather than an
64
- # unbounded host recursion.
65
- def guard_nesting(depth)
66
- return unless depth > MAX_NESTING_DEPTH
67
-
68
- raise Kobako::SandboxError,
69
- "a #run argument nests deeper than #{MAX_NESTING_DEPTH} levels within the Run payload and " \
70
- "cannot cross the sandbox boundary (possible reference cycle)"
71
- end
72
-
73
- # A stateful object may cross the boundary as a Hash value but not as
74
- # a key — the one deliberate asymmetry with the guest→host restore
75
- # walk, which resolves Handle keys the guest built.
76
- def deep_wrap_hash(hash, handles, depth)
77
- wrapped = {} # : Hash[untyped, untyped]
78
- hash.each do |key, val|
79
- unless representable?(key, depth + 1)
80
- raise Kobako::SandboxError,
81
- "a Hash passed to #run has a key that cannot cross the sandbox boundary " \
82
- "(#{key.class}); only wire-representable values may be Hash keys"
83
- end
84
- wrapped[key] = deep_wrap(val, handles, depth + 1)
85
- end
86
- wrapped
87
- end
88
-
89
49
  def primitive_type?(value)
90
50
  case value
91
51
  when ::NilClass, ::TrueClass, ::FalseClass, ::Float, ::String, ::Symbol, Kobako::Handle then true
@@ -6,8 +6,12 @@ module Kobako
6
6
  module Codec
7
7
  # Counts the values a payload holds before the decoder builds any, so a
8
8
  # guest cannot make the host build more objects than it could hold in
9
- # its own memory. Every value takes at
10
- # least one byte, so a payload no longer than the budget needs no count.
9
+ # its own memory. Every value takes at least one byte, so a payload no
10
+ # longer than the budget, and holding no float, needs no count.
11
+ #
12
+ # A map key holding a NaN costs one more value for each such key the
13
+ # map already holds, as a Ruby Hash compares it with each: every NaN
14
+ # hashes alike and none is +eql?+ to another.
11
15
  #
12
16
  # The count stops at the first byte it cannot frame and leaves that
13
17
  # payload for the decoder to refuse in its own words.
@@ -19,7 +23,8 @@ module Kobako
19
23
  0xd0 => 1, 0xd1 => 2, 0xd2 => 4, 0xd3 => 8,
20
24
  0xd4 => 2, 0xd5 => 3, 0xd6 => 5, 0xd7 => 9, 0xd8 => 17
21
25
  }.freeze
22
- # Marker to a container's count width and the values each entry holds.
26
+ # Marker to a container's count width and the values each entry holds:
27
+ # two for a map's key and value.
23
28
  COUNTS = { 0xdc => [2, 1], 0xdd => [4, 1], 0xde => [2, 2], 0xdf => [4, 2] }.freeze
24
29
  # Marker to a str, bin, or ext's length width and type tag width.
25
30
  LENGTHS = {
@@ -30,57 +35,144 @@ module Kobako
30
35
  UINT = { 1 => "C", 2 => "n", 4 => "N" }.freeze
31
36
  private_constant :SCALARS, :COUNTS, :LENGTHS, :UINT
32
37
 
38
+ # A float frame's marker to how its body unpacks.
39
+ FLOAT_FORMATS = { 0xca => "g", 0xcb => "G" }.freeze
40
+ # A float frame's marker; a payload without one holds no NaN.
41
+ FLOATS = Regexp.union(FLOAT_FORMATS.keys.map(&:chr))
42
+ # The fewest bytes a NaN key and its value take: a float 32 frame and
43
+ # a one-byte value.
44
+ NAN_ENTRY = 1 + SCALARS.fetch(0xca) + 1
45
+ private_constant :FLOAT_FORMATS, :FLOATS, :NAN_ENTRY
46
+
33
47
  def self.assert_within(bytes, budget)
34
- return if budget.nil? || bytes.bytesize <= budget
48
+ return if budget.nil? || within_by_size?(bytes, budget)
35
49
 
36
- remaining = budget
37
- pending = 1
38
- pos = 0
39
- while pending.positive?
40
- pos, children = step(bytes, pos)
41
- return if pos.nil?
42
- raise InvalidTypeError, "payload holds more than #{budget} values" if (remaining -= 1).negative?
50
+ Count.new(bytes, budget).run
51
+ end
43
52
 
44
- pending += children - 1
45
- end
53
+ # Every value takes at least one byte, so a payload no longer than the
54
+ # budget holds no more values than it, and what its NaN keys could
55
+ # add is bounded by how many of them its bytes could frame.
56
+ def self.within_by_size?(bytes, budget)
57
+ size = bytes.bytesize
58
+ return false if size > budget
59
+ return true unless bytes.match?(FLOATS)
60
+
61
+ nan_keys = size / NAN_ENTRY
62
+ size + (nan_keys * (nan_keys - 1) / 2) <= budget
46
63
  end
47
64
 
48
- # The position after the value at +pos+, nil for one the payload
49
- # cannot frame, and the values it holds directly.
50
- def self.step(bytes, pos)
51
- marker = bytes.getbyte(pos) or return [nil, 0]
52
- pos += 1
53
- case marker
54
- when 0x00..0x7f, 0xe0..0xff then [pos, 0]
55
- when 0x80..0x8f then [pos, (marker & 0x0f) * 2]
56
- when 0x90..0x9f then [pos, marker & 0x0f]
57
- when 0xa0..0xbf then [pos + (marker & 0x1f), 0]
58
- else step_framed(bytes, pos, marker)
65
+ # One count through +bytes+, walked without recursion so a payload
66
+ # nested past what the decoder allows cannot exhaust the stack first.
67
+ class Count
68
+ # A container still being read: the values it holds directly,
69
+ # whether it is keyed (a map), how many of them were read, how many
70
+ # of its keys held a NaN, and whether anything in it did.
71
+ OpenContainer = Struct.new(:held, :keyed, :read, :nan_keys, :holds_nan)
72
+
73
+ def initialize(bytes, budget)
74
+ @bytes = bytes
75
+ @budget = budget
76
+ @remaining = budget
77
+ @open = [] # : Array[OpenContainer]
78
+ @ended = false
59
79
  end
60
- end
61
80
 
62
- def self.step_framed(bytes, pos, marker)
63
- return [pos + SCALARS.fetch(marker), 0] if SCALARS.key?(marker)
81
+ def run
82
+ pos = 0
83
+ loop do
84
+ after, children, keyed = step(pos)
85
+ return if after.nil?
64
86
 
65
- width, per = COUNTS[marker]
66
- return [pos + width, uint(bytes, pos, width) * per] if width && per
87
+ count_value(pos, children, keyed)
88
+ return if @ended
67
89
 
68
- width, tag = LENGTHS[marker]
69
- return [nil, 0] unless width && tag
90
+ pos = after
91
+ end
92
+ end
70
93
 
71
- [pos + width + tag + uint(bytes, pos, width), 0]
72
- end
94
+ private
95
+
96
+ # Count the value at +pos+, which holds +children+ directly, keyed
97
+ # when it is a map.
98
+ def count_value(pos, children, keyed)
99
+ spend(1)
100
+ return close_value(nan?(pos)) unless children.positive?
101
+
102
+ @open << OpenContainer.new(children, keyed, 0, 0, false)
103
+ end
73
104
 
74
- # A length cut short reads as zero, so the count runs off the end and
75
- # stops there.
76
- def self.uint(bytes, pos, width)
77
- slice = bytes.byteslice(pos, width).to_s
78
- return 0 if slice.bytesize < width
105
+ # Count a value that ended, and each container it ends, noting once
106
+ # the outermost one did.
107
+ def close_value(holds_nan)
108
+ until @open.empty?
109
+ container = @open.last # : OpenContainer
110
+ charge_key(container) if holds_nan && container.keyed && container.read.even?
111
+ container.holds_nan ||= holds_nan
112
+ container.read += 1
113
+ return unless container.read == container.held
79
114
 
80
- slice.unpack1(UINT.fetch(width)) # : Integer
115
+ @open.pop
116
+ holds_nan = container.holds_nan
117
+ end
118
+ @ended = true
119
+ end
120
+
121
+ def charge_key(map)
122
+ spend(map.nan_keys)
123
+ map.nan_keys += 1
124
+ end
125
+
126
+ def spend(values)
127
+ @remaining -= values
128
+ raise InvalidTypeError, "payload holds more than #{@budget} values" if @remaining.negative?
129
+ end
130
+
131
+ def nan?(pos)
132
+ marker = @bytes.getbyte(pos)
133
+ format = FLOAT_FORMATS[marker] or return false
134
+ float = @bytes.byteslice(pos + 1, SCALARS.fetch(marker)).to_s.unpack1(format)
135
+ float.is_a?(Float) && float.nan?
136
+ end
137
+
138
+ # The position after the value at +pos+, nil for one the payload
139
+ # cannot frame, the values it holds directly, and whether it is a map.
140
+ def step(pos)
141
+ marker = @bytes.getbyte(pos) or return [nil, 0, false]
142
+ pos += 1
143
+ case marker
144
+ when 0x00..0x7f, 0xe0..0xff then [pos, 0, false]
145
+ when 0x80..0x8f then [pos, (marker & 0x0f) * 2, true]
146
+ when 0x90..0x9f then [pos, marker & 0x0f, false]
147
+ when 0xa0..0xbf then [pos + (marker & 0x1f), 0, false]
148
+ else step_framed(pos, marker)
149
+ end
150
+ end
151
+
152
+ def step_framed(pos, marker)
153
+ return [pos + SCALARS.fetch(marker), 0, false] if SCALARS.key?(marker)
154
+
155
+ width, per = COUNTS[marker]
156
+ return [pos + width, uint(pos, width) * per, per == 2] if width && per
157
+
158
+ width, tag = LENGTHS[marker]
159
+ return [nil, 0, false] unless width && tag
160
+
161
+ [pos + width + tag + uint(pos, width), 0, false]
162
+ end
163
+
164
+ # A length cut short reads as zero, so the count runs off the end and
165
+ # stops there.
166
+ def uint(pos, width)
167
+ slice = @bytes.byteslice(pos, width).to_s
168
+ return 0 if slice.bytesize < width
169
+
170
+ slice.unpack1(UINT.fetch(width)) # : Integer
171
+ end
81
172
  end
173
+ private_constant :Count
82
174
 
83
- private_class_method :step, :step_framed, :uint
175
+ private_class_method :within_by_size?
84
176
  end
85
177
  end
86
178
  end
data/lib/kobako/codec.rb CHANGED
@@ -1,7 +1,7 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  require_relative "codec/error"
4
- require_relative "codec/utils"
4
+ require_relative "codec/boundary"
5
5
  require_relative "codec/handle_walk"
6
6
  require_relative "codec/nesting"
7
7
  require_relative "codec/state"