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
@@ -25,18 +25,19 @@ module Kobako
25
25
  attr_reader :options
26
26
 
27
27
  def_delegators :@options, :timeout, :setup_timeout, :memory_limit, :stdout_limit, :stderr_limit,
28
- :open_file_limit, :profile, :gvl
28
+ :open_file_limit, :open_connection_limit, :connection_limit, :profile, :gvl
29
29
 
30
30
  # Build a Sandbox on the Guest Binary at +wasm_path+, the one bundled
31
31
  # with the gem by default. The other keywords are the options
32
32
  # SandboxOptions describes.
33
33
  #
34
- # Raises ArgumentError for an invalid option, and SetupError when the
35
- # runtime cannot be built or provides less isolation than +profile+ asks
36
- # for.
34
+ # Raises TypeError for an option of another class, RangeError for a cap
35
+ # past what the host counts, ArgumentError for any other invalid option,
36
+ # and SetupError when the runtime cannot be built or provides less
37
+ # isolation than +profile+ asks for.
37
38
  def initialize(wasm_path: nil, **)
38
39
  @wasm_path = wasm_path || Kobako::Runtime.default_path
39
- @options = SandboxOptions.new(**)
40
+ @options = SandboxOptions.new(**Kobako::Runtime.default_options, **)
40
41
  @services = Kobako::Catalog::Services.new
41
42
  @snippets = Catalog::Snippets.new
42
43
  @flags = Catalog::Flags.new
@@ -58,9 +59,10 @@ module Kobako
58
59
  # the constant, and Context#bind supplies the object for each invocation.
59
60
  # A call to one left unfilled fails as a ServiceError.
60
61
  #
61
- # Raises ArgumentError for a malformed path, a path under the reserved
62
- # +Kobako+ namespace, a path that collides with an existing binding, or a
63
- # call after the first invocation.
62
+ # Raises TypeError for a path that is no Symbol or String, ArgumentError
63
+ # for a malformed path, a path under the reserved
64
+ # +Kobako+ namespace, or a path that collides with an existing binding,
65
+ # and FrozenError for a call after the first invocation.
64
66
  def bind(path, object = Unresolved)
65
67
  refuse_once_invoked("bind")
66
68
  @services.bind(path, object)
@@ -71,8 +73,9 @@ module Kobako
71
73
  # backend. Any object answering +name+, +source+, +backend+ and
72
74
  # +depends_on+ works; Extension is the bundled one. Returns +self+.
73
75
  #
74
- # Raises ArgumentError for a malformed Extension, a call after the first
75
- # invocation, or, at the first invocation, a missing dependency.
76
+ # Raises TypeError for an Extension of another shape, FrozenError for a
77
+ # call after the first invocation, and, at the first invocation,
78
+ # ArgumentError for a missing dependency.
76
79
  def install(*extensions)
77
80
  refuse_once_invoked("install")
78
81
 
@@ -92,19 +95,39 @@ module Kobako
92
95
  # or a StandardError, fails that call as <tt>Errno::EACCES</tt> in the
93
96
  # guest and leaves the script running.
94
97
  #
95
- # A path may sit beneath another mount, and the guest reaches the
96
- # innermost one.
98
+ # A path may sit beneath another mount, standing where Linux's +mount+
99
+ # would place it, and a run fails where Linux would refuse it.
97
100
  #
98
101
  # Raises TypeError for a path that is no String or something other than
99
- # a Filesystem, and ArgumentError for a malformed or duplicate path, an
100
- # unknown access, or a call after the first invocation.
102
+ # a Filesystem, ArgumentError for a malformed or duplicate path or an
103
+ # unknown access, and FrozenError for a call after the first invocation.
101
104
  def mount(at, filesystem, access: :read, &policy)
102
105
  refuse_once_invoked("mount")
103
- Runtime.validate_mount(at, access, filesystem)
104
106
  @grants.register(Catalog::Mount.new(at: at, access: access, filesystem: filesystem, policy: policy))
105
107
  self
106
108
  end
107
109
 
110
+ # Give the guest the network entry +entry+ under +name+, any object
111
+ # answering any of +endpoints+, +credentials+, +resolve?+, +connect?+, and
112
+ # +request?+. A given block yields a Kobako::Network::Builder adding to
113
+ # +entry+, so the block's +self+ stays the caller's. Each entry's endpoints
114
+ # and credentials are read afresh for every invocation. Its +resolve?+ is
115
+ # handed each name the guest looks up, its +connect?+ each
116
+ # Kobako::Network::Connection, and its +request?+ each
117
+ # Kobako::Network::Request; any entry refusing refuses. Returns +self+.
118
+ #
119
+ # Raises TypeError for a name that is no Symbol or String, ArgumentError
120
+ # for an empty or duplicate name, an endpoint with an
121
+ # empty host, a port outside 1 to 65535, or an unknown protocol, and
122
+ # FrozenError for a call after the first invocation. An
123
+ # endpoint the ruleset refuses, such as a wildcard leading to +:private+,
124
+ # raises ArgumentError from the invocation that compiles it.
125
+ def network(name, entry = nil, &builder)
126
+ refuse_once_invoked("network")
127
+ @grants.register(Catalog::Network.new(name: name, entry: entry, &builder))
128
+ self
129
+ end
130
+
108
131
  # Register a snippet that every invocation runs, in the order registered,
109
132
  # before its own code. Returns +self+.
110
133
  #
@@ -114,8 +137,9 @@ module Kobako
114
137
  # Precompiled RITE bytecode, trusted as host code: the unmodified
115
138
  # compiler output of the mruby release the Guest Binary is built on.
116
139
  #
117
- # Raises ArgumentError for a malformed or duplicate snippet, or a call
118
- # after the first invocation. A snippet that fails to load fails every
140
+ # Raises TypeError for a snippet part of another class, ArgumentError for
141
+ # a malformed or duplicate snippet, and FrozenError for a call after the
142
+ # first invocation. A snippet that fails to load fails every
119
143
  # invocation with SandboxError, or BytecodeError for bytecode.
120
144
  def preload(code: nil, name: nil, binary: nil)
121
145
  refuse_once_invoked("preload")
@@ -131,8 +155,8 @@ module Kobako
131
155
  # Mark a flag advisory only when a script cannot tell it was skipped.
132
156
  # Returns +self+.
133
157
  #
134
- # Raises ArgumentError for a malformed or duplicate flag, or a call after
135
- # the first invocation.
158
+ # Raises TypeError for a name or value of another class, ArgumentError for
159
+ # a duplicate flag, and FrozenError for a call after the first invocation.
136
160
  def flag(name, value = "", advisory: false)
137
161
  refuse_once_invoked("flag")
138
162
 
@@ -144,9 +168,11 @@ module Kobako
144
168
  # and +kwargs+, and return the Execution. A given block receives the
145
169
  # invocation's Context before the guest runs.
146
170
  #
147
- # Raises TypeError when +target+ is not a Symbol or String, and
148
- # ArgumentError when it is not a constant name or the arguments carry a
149
- # Handle or a non-Symbol keyword. A failed run raises as #eval does.
171
+ # Raises TypeError when +target+ is not a Symbol or String, a keyword is
172
+ # not a Symbol, or an argument holds a Hash key or a reflective object
173
+ # that cannot cross, and ArgumentError when +target+ is not a constant
174
+ # name, the arguments carry a Handle, or an argument nests past what the
175
+ # wire carries. A failed run raises as #eval does.
150
176
  def run(target, *args, **kwargs, &block)
151
177
  request = Transport::Run.new(entrypoint: target, args: args, kwargs: kwargs)
152
178
  Codec.within_value_budget(@options.value_budget) { begin_invocation.run(request, &block) }
@@ -156,25 +182,25 @@ module Kobako
156
182
  # whose +value+ is the last expression. A given block receives the
157
183
  # invocation's Context before the guest runs.
158
184
  #
159
- # Raises TrapError when the engine stopped the guest, a deadline or
160
- # memory budget included; SandboxError when the guest failed, +code+ not
161
- # being a String or the Guest Binary not starting the run included; and
162
- # ServiceError when a Service call failed and the guest left it
185
+ # Raises TypeError when +code+ is no String, and ArgumentError when it is
186
+ # not UTF-8 text, before the guest runs; TrapError when the engine
187
+ # stopped the guest, a deadline or memory budget included; SandboxError
188
+ # when the guest failed, the Guest Binary not starting the run included;
189
+ # and ServiceError when a Service call failed and the guest left it
163
190
  # unrescued.
164
191
  def eval(code, &block)
165
- raise SandboxError, "code must be a String, got #{code.class}" unless code.is_a?(String)
192
+ raise TypeError, "code must be a String, got #{code.class}" unless code.is_a?(String)
166
193
 
167
- Codec.within_value_budget(@options.value_budget) { begin_invocation.eval(code, &block) }
194
+ source = code.dup.force_encoding(Encoding::UTF_8)
195
+ raise ArgumentError, "code must be UTF-8 text" unless source.valid_encoding?
196
+
197
+ Codec.within_value_budget(@options.value_budget) { begin_invocation.eval(source, &block) }
168
198
  end
169
199
 
170
200
  private
171
201
 
172
- # A runtime whose declared posture falls below the request never runs
173
- # guest code.
174
202
  def build_runtime
175
- runtime = Kobako::Runtime.from_path(@wasm_path, **@options.to_h)
176
- @options.enforce_floor(runtime.profile)
177
- runtime
203
+ Kobako::Runtime.from_path(@wasm_path, **@options.to_h)
178
204
  end
179
205
 
180
206
  # The one place the Preamble's parts are put together, for a compile
@@ -186,7 +212,12 @@ module Kobako
186
212
  # Registration closes at the first invocation, so every invocation ships
187
213
  # the same Services, snippets, flags, Extensions and mounts.
188
214
  def refuse_once_invoked(verb)
189
- raise ArgumentError, "cannot #{verb} after first Sandbox invocation" if @sealed
215
+ raise FrozenError, "cannot #{verb} #{@sealed}" if @sealed
216
+ end
217
+
218
+ # Close registration, saying why for the refusal that follows.
219
+ def seal(reason)
220
+ @sealed = reason
190
221
  end
191
222
 
192
223
  # Sealing before the dependency check keeps a Sandbox whose check failed
@@ -195,11 +226,37 @@ module Kobako
195
226
  # the Context it answers, so nothing an invocation does is written back
196
227
  # onto the shared config.
197
228
  def begin_invocation
198
- @sealed = true
229
+ warn_release_on_main_thread
230
+ seal("after first Sandbox invocation")
199
231
  @extensions.assert_dependencies
200
232
  @snippets.compile { |entries| @runtime.compile(preamble(entries)) }
201
233
  Context.new(runtime: @runtime, services: @services, extensions: @extensions, grants: @grants,
202
234
  preamble: preamble(@snippets.entries))
203
235
  end
236
+
237
+ # Ruby runs a signal's trap handler, and a Ractor's +require+, on the main
238
+ # Thread alone, and either raising as a run releasing the GVL takes it back
239
+ # after a Service call leaves the run where it cannot unwind. The first such
240
+ # run warns once.
241
+ def warn_release_on_main_thread
242
+ MainThreadRelease.warn_once if @options.gvl == :release && Thread.current == Thread.main
243
+ end
244
+
245
+ # The process's one warning that a run releasing the GVL ran on the main
246
+ # Thread.
247
+ module MainThreadRelease
248
+ MESSAGE = "Kobako::Sandbox with gvl: :release runs on the main Thread, where a trap handler " \
249
+ "that raises or exits, or another Ractor's require, can stop the run where it cannot " \
250
+ "unwind; run it on another Thread, or keep trap handlers to setting a flag"
251
+ @warned = false
252
+
253
+ def self.warn_once
254
+ return if @warned
255
+
256
+ @warned = true
257
+ warn(MESSAGE)
258
+ end
259
+ end
260
+ private_constant :MainThreadRelease
204
261
  end
205
262
  end
@@ -4,122 +4,57 @@ require_relative "errors"
4
4
 
5
5
  module Kobako
6
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.
7
+ # directly on the Sandbox, as in Sandbox#timeout. An option Sandbox.new
8
+ # leaves out takes the runtime's default, given below, and +nil+ switches a
9
+ # cap off.
9
10
  #
10
- # [+timeout+] Float seconds one invocation may run.
11
+ # [+timeout+] Float seconds one invocation may run; 60.
11
12
  # [+setup_timeout+] Float seconds the Guest Binary may take to be set up
12
- # the first time its path is loaded.
13
- # [+memory_limit+] Integer bytes one invocation may grow guest memory by.
13
+ # the first time its path is loaded; 10.
14
+ # [+memory_limit+] Integer bytes one invocation may grow guest memory by;
15
+ # 4 MiB.
14
16
  # [+stdout_limit+, +stderr_limit+]
15
- # Integer bytes kept from each output channel.
17
+ # Integer bytes kept from each output channel; 1 MiB.
16
18
  # [+open_file_limit+]
17
19
  # Integer descriptors one invocation may hold open, the
18
- # standard ones and each Mount's included.
19
- # [+profile+] The isolation the runtime must provide, one of PROFILES.
20
- # [+gvl+] How an invocation holds Ruby's GVL, one of GVL_MODES.
20
+ # standard ones and each outermost mount's included; 1024.
21
+ # [+open_connection_limit+]
22
+ # Integer connections one invocation holds open at once, each
23
+ # socket once it dials, HTTP request, and name lookup in
24
+ # flight one; 6.
25
+ # [+connection_limit+]
26
+ # Integer connections one invocation makes in all, each
27
+ # socket's dial and HTTP request one; 50.
28
+ # [+profile+] The isolation the runtime must provide at least:
29
+ # +:hermetic+, which denies the guest the clock and
30
+ # entropy, or the weaker +:permissive+, which leaves them
31
+ # live; +:hermetic+.
32
+ # [+gvl+] How an invocation holds Ruby's GVL: +:hold+ through
33
+ # the invocation, or +:release+ while the guest runs, so
34
+ # Sandboxes on different Threads run in parallel; +:hold+.
35
+ # [+network+] What the guest's network dials and looks names up
36
+ # through, beneath every rule its entries make: +nil+
37
+ # for the host's own, or a Kobako::Network::Mock a test
38
+ # writes; +nil+.
21
39
  #
22
- # Raises ArgumentError for a value outside these shapes, and SetupError
23
- # when the runtime provides less isolation than +profile+ asks for.
40
+ # Raises TypeError for a cap of another class, RangeError for one past
41
+ # what the host counts, and ArgumentError for another value outside these
42
+ # shapes. The runtime judges every cap, and +profile+, +gvl+, and
43
+ # +network+ as Sandbox.new builds it.
24
44
  class SandboxOptions < Data.define(:timeout, :setup_timeout, :memory_limit, :stdout_limit, :stderr_limit,
25
- :open_file_limit, :profile, :gvl)
26
- # The default +timeout+: 60 seconds.
27
- DEFAULT_TIMEOUT_SECONDS = 60.0
28
-
29
- # The default +setup_timeout+: 10 seconds.
30
- DEFAULT_SETUP_TIMEOUT_SECONDS = 10.0
31
-
32
- # The default +memory_limit+: 4 MiB of growth past the guest's starting
33
- # memory, sized for a guest whose every value takes 16 bytes.
34
- DEFAULT_MEMORY_LIMIT = 4 << 20
35
-
36
- # The default +stdout_limit+ and +stderr_limit+: 1 MiB each.
37
- DEFAULT_OUTPUT_LIMIT = 1 << 20
38
-
39
- # The default +open_file_limit+: 1024, the soft limit Linux gives a
40
- # process.
41
- DEFAULT_OPEN_FILE_LIMIT = 1024
42
-
43
- # The isolation profiles, weakest first. +:hermetic+ denies the guest the
44
- # clock and entropy; +:permissive+ leaves them live.
45
- PROFILES = %i[permissive hermetic].freeze
46
-
47
- # The default +profile+, the strictest.
48
- DEFAULT_PROFILE = :hermetic
49
-
50
- # +:hold+ keeps the GVL through the invocation; +:release+ drops it while
51
- # the guest runs, so Sandboxes on different Threads run in parallel.
52
- GVL_MODES = %i[hold release].freeze
53
-
54
- # The default +gvl+, +:hold+.
55
- DEFAULT_GVL = :hold
56
-
57
- def initialize(timeout: DEFAULT_TIMEOUT_SECONDS,
58
- setup_timeout: DEFAULT_SETUP_TIMEOUT_SECONDS,
59
- memory_limit: DEFAULT_MEMORY_LIMIT,
60
- stdout_limit: DEFAULT_OUTPUT_LIMIT,
61
- stderr_limit: DEFAULT_OUTPUT_LIMIT,
62
- open_file_limit: DEFAULT_OPEN_FILE_LIMIT,
63
- profile: DEFAULT_PROFILE,
64
- gvl: DEFAULT_GVL)
65
- timeout = normalize_timeout(timeout, "timeout")
66
- setup_timeout = normalize_timeout(setup_timeout, "setup_timeout")
67
- memory_limit = normalize_limit(memory_limit, "memory_limit")
68
- stdout_limit = normalize_limit(stdout_limit, "stdout_limit")
69
- stderr_limit = normalize_limit(stderr_limit, "stderr_limit")
70
- open_file_limit = normalize_limit(open_file_limit, "open_file_limit")
71
- profile = normalize_choice(profile, PROFILES, "profile")
72
- gvl = normalize_choice(gvl, GVL_MODES, "gvl")
73
- super
45
+ :open_file_limit, :open_connection_limit, :connection_limit, :profile, :gvl,
46
+ :network)
47
+ # Every option is required; Sandbox.new fills each one left out from
48
+ # the runtime's defaults.
49
+ def initialize(timeout:, setup_timeout:, memory_limit:, stdout_limit:, stderr_limit:, open_file_limit:,
50
+ open_connection_limit:, connection_limit:, profile:, gvl:, network:)
51
+ caps = Kobako::Runtime.normalize_caps({ timeout:, setup_timeout:, memory_limit:, stdout_limit:, stderr_limit:,
52
+ open_file_limit:, open_connection_limit:, connection_limit: })
53
+ super(**caps.merge(profile:, gvl:, network:))
74
54
  end
75
55
 
76
- # The least guest memory one value occupies, so a guest payload may hold
77
- # one value for each of these bytes of +memory_limit+.
78
- VALUE_BYTES = 16
79
- private_constant :VALUE_BYTES
80
-
81
56
  def value_budget # :nodoc:
82
- memory_limit&./(VALUE_BYTES)
83
- end
84
-
85
- # Both fallbacks fail closed: an unknown declaration ranks below every
86
- # request, and an unknown request refuses every declaration.
87
- def enforce_floor(declared) # :nodoc:
88
- return if (PROFILES.index(declared) || -1) >= (PROFILES.index(profile) || PROFILES.size)
89
-
90
- raise Kobako::SetupError, "runtime declares isolation profile #{declared.inspect}, " \
91
- "below the requested floor #{profile.inspect}"
92
- end
93
-
94
- private
95
-
96
- # A zero or negative timeout would either fire instantly or never, both
97
- # more surprising than an early ArgumentError.
98
- def normalize_timeout(timeout, name)
99
- return nil if timeout.nil?
100
- raise ArgumentError, "#{name} must be Numeric or nil, got #{timeout.class}" unless timeout.is_a?(Numeric)
101
-
102
- seconds = timeout.to_f
103
- raise ArgumentError, "#{name} must be > 0 (got #{timeout})" unless seconds.positive? && seconds.finite?
104
-
105
- seconds
106
- end
107
-
108
- def normalize_limit(limit, name)
109
- return nil if limit.nil?
110
- unless limit.is_a?(Integer) && limit.positive?
111
- raise ArgumentError, "#{name} must be a positive Integer or nil, got #{limit.inspect}"
112
- end
113
-
114
- limit
115
- end
116
-
117
- # Unlike the caps there is no +nil+ form: every choice, the weakest
118
- # included, is requested by name, as in +:permissive+.
119
- def normalize_choice(value, choices, name)
120
- return value if choices.include?(value)
121
-
122
- raise ArgumentError, "#{name} must be one of #{choices.map(&:inspect).join(", ")}, got #{value.inspect}"
57
+ Kobako::Runtime.value_budget_of(memory_limit)
123
58
  end
124
59
  end
125
60
  end
@@ -0,0 +1,64 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "../codec"
4
+ require_relative "exposure"
5
+
6
+ module Kobako
7
+ module Transport
8
+ # The walk a #run argument takes on its way to the guest: a leaf with no
9
+ # wire form becomes a Capability Handle in the invocation's table, and
10
+ # the rest crosses as it stands. An argument the walk refuses is the
11
+ # Host App's mistake, raised as +Marshal.dump+ raises it: +TypeError+
12
+ # for what has no form to cross as, +ArgumentError+ past the depth.
13
+ module ArgumentWrap # :nodoc:
14
+ # Only Hash values are wrapped: a non-representable key raises rather
15
+ # than crossing as a token the guest→host restore walk could not
16
+ # round-trip.
17
+ def self.wrap(value, handles, depth = 0)
18
+ refuse_past_nesting(depth)
19
+ case value
20
+ when ::Array then value.map { |element| wrap(element, handles, depth + 1) }
21
+ when ::Hash then wrap_hash(value, handles, depth)
22
+ else
23
+ Codec::HandleWalk.representable?(value) ? value : wrap_leaf(value, handles)
24
+ end
25
+ end
26
+
27
+ class << self
28
+ private
29
+
30
+ # A reference cycle necessarily trips the nesting bound, so an
31
+ # argument fails as a clean +ArgumentError+ rather than an unbounded
32
+ # host recursion.
33
+ def refuse_past_nesting(depth)
34
+ return unless depth > Codec::MAX_NESTING_DEPTH
35
+
36
+ raise ArgumentError,
37
+ "a #run argument nests deeper than #{Codec::MAX_NESTING_DEPTH} levels within the Run payload " \
38
+ "and cannot cross the sandbox boundary (possible reference cycle)"
39
+ end
40
+
41
+ def wrap_leaf(value, handles)
42
+ refused = Exposure.refused_class_of(value)
43
+ raise TypeError, "a #{refused} cannot cross as a Capability Handle" if refused
44
+
45
+ handles.alloc(value)
46
+ end
47
+
48
+ # A stateful object may cross the boundary as a Hash value but not as
49
+ # a key — the one deliberate asymmetry with the guest→host restore
50
+ # walk, which resolves Handle keys the guest built.
51
+ def wrap_hash(hash, handles, depth)
52
+ hash.to_h do |key, val|
53
+ unless Codec::HandleWalk.representable?(key, depth + 1)
54
+ raise TypeError,
55
+ "a Hash passed to #run has a key that cannot cross the sandbox boundary " \
56
+ "(#{key.class}); only wire-representable values may be Hash keys"
57
+ end
58
+ [key, wrap(val, handles, depth + 1)]
59
+ end
60
+ end
61
+ end
62
+ end
63
+ end
64
+ end
@@ -28,7 +28,7 @@ module Kobako
28
28
  raised = @raised
29
29
  return if raised.nil? || !error.equal?(raised)
30
30
 
31
- "#{raised.klass}: #{raised.message}"
31
+ "#{raised.class_name}: #{raised.message}"
32
32
  end
33
33
  end
34
34
  end
@@ -61,16 +61,17 @@ module Kobako
61
61
  # rescuable fault.
62
62
  rescue StandardError => e
63
63
  [:fault, *fault_of(e, failure)] # : [Symbol, String, String?]
64
- ensure
65
- yielder&.invalidate
66
64
  end
67
65
 
68
66
  class << self
69
67
  private
70
68
 
69
+ # Routing reads only the target and the method, so a dispatch that
70
+ # cannot route answers before its arguments are read.
71
71
  def run(call, resolver, handles, yielder)
72
- arguments, carried_handle = decode_arguments(call.payload)
73
72
  exposure = resolve_target(call.target, resolver, handles)
73
+ reject_unreachable(exposure, call.method_name.to_sym)
74
+ arguments, carried_handle = decode_arguments(call.payload)
74
75
  args, kwargs = resolve_call_args(arguments, handles, carried_handle)
75
76
  invoke(exposure, call.method_name, args, kwargs, yielder)
76
77
  end
@@ -111,13 +112,12 @@ module Kobako
111
112
  # unsplatted for methods that take no keywords.
112
113
  def invoke(exposure, method, args, kwargs, yielder)
113
114
  name = method.to_sym
114
- reject_unreachable(exposure, name)
115
115
  target = exposure.object
116
116
  block = yielder&.to_proc
117
117
  if kwargs.empty?
118
- target.public_send(name, *args, &block)
118
+ Reflection::PUBLIC_SEND.bind(target).call(name, *args, &block)
119
119
  else
120
- target.public_send(name, *args, **kwargs, &block)
120
+ Reflection::PUBLIC_SEND.bind(target).call(name, *args, **kwargs, &block)
121
121
  end
122
122
  end
123
123
 
@@ -14,15 +14,6 @@ module Kobako
14
14
  PREDICATE = :respond_to_guest?
15
15
  private_constant :PREDICATE
16
16
 
17
- # Kernel's own reflection, bound onto each object so a forwarder or a
18
- # BasicObject answers from its own method table rather than through
19
- # +method_missing+ / +respond_to_missing?+.
20
- CLASS_OF = Kernel.instance_method(:class)
21
- SINGLETON_CLASS_OF = Kernel.instance_method(:singleton_class)
22
- SINGLETON_METHODS = Kernel.instance_method(:singleton_methods)
23
- PRIVATE_METHODS = Kernel.instance_method(:private_methods)
24
- private_constant :CLASS_OF, :SINGLETON_CLASS_OF, :SINGLETON_METHODS, :PRIVATE_METHODS
25
-
26
17
  CALLABLE = Set.new(Reflection::CALLABLE_ALLOW).freeze
27
18
  NOTHING = Set.new.freeze
28
19
  private_constant :CALLABLE, :NOTHING
@@ -31,18 +22,38 @@ module Kobako
31
22
  # each class's own surface for a caller that makes many references at
32
23
  # once (a Handle table), so two objects of one class enumerate it once.
33
24
  def self.of(object, surfaces = {})
34
- klass = CLASS_OF.bind_call(object)
25
+ klass = Reflection::CLASS_OF.bind_call(object)
35
26
  names = narrows_itself?(object, klass) ? nil : resolve_default_names(object, klass, surfaces)
36
27
  new(object: object, names: names)
37
28
  end
38
29
 
30
+ # The class of +object+ when no reference may stand for it, as its
31
+ # reachable surface is not Service behaviour; +nil+ otherwise. A
32
+ # +Binding+ / +Method+ / +UnboundMethod+ hands the guest a callable
33
+ # proxy onto host reflection (a returned +Binding+ reaches
34
+ # +Binding#eval+); a +Class+ or +Module+ hands over its class-level API
35
+ # (+File.popen+ / +read+, +Kernel.system+), which the owner-based
36
+ # dispatch floor cannot see because a singleton-class owner matches no
37
+ # core-module list; a +Delegator+ (+SimpleDelegator+, +DelegateClass+,
38
+ # +WeakRef+, +Tempfile+) is a transparent forwarder whose public
39
+ # +method_missing+ binds and calls the private method the guest names
40
+ # (+Kernel#system+), a surface the floor reads as ordinary Service
41
+ # behaviour.
42
+ def self.refused_class_of(object)
43
+ case object
44
+ when Binding, Method, UnboundMethod, Module, Delegator
45
+ # Delegator < BasicObject exposes no static +#class+.
46
+ Reflection::CLASS_OF.bind_call(object)
47
+ end
48
+ end
49
+
39
50
  # Whether the guest may call +name+ through this reference. The
40
51
  # object's own predicate is consulted with the private surface
41
52
  # included, so the guest's +public_send+ can never reach it.
42
53
  def exposes?(name)
43
54
  return names.include?(name) if names
44
55
 
45
- object.__send__(PREDICATE, name) ? true : false
56
+ Reflection::SEND.bind_call(object, PREDICATE, name) ? true : false
46
57
  end
47
58
 
48
59
  class << self
@@ -53,8 +64,8 @@ module Kobako
53
64
  # +respond_to_missing?+ cannot answer for it.
54
65
  def narrows_itself?(object, klass)
55
66
  klass.method_defined?(PREDICATE) || klass.private_method_defined?(PREDICATE) ||
56
- PRIVATE_METHODS.bind_call(object, false).include?(PREDICATE) ||
57
- SINGLETON_METHODS.bind_call(object, false).include?(PREDICATE)
67
+ Reflection::PRIVATE_METHODS.bind_call(object, false).include?(PREDICATE) ||
68
+ Reflection::SINGLETON_METHODS.bind_call(object, false).include?(PREDICATE)
58
69
  end
59
70
 
60
71
  # A callable keeps its callable names; a class-level surface or a
@@ -76,9 +87,9 @@ module Kobako
76
87
  end
77
88
 
78
89
  def with_singleton(object, names)
79
- return names if SINGLETON_METHODS.bind_call(object, false).empty?
90
+ return names if Reflection::SINGLETON_METHODS.bind_call(object, false).empty?
80
91
 
81
- names.union(authored_names(SINGLETON_CLASS_OF.bind_call(object))).freeze
92
+ names.union(authored_names(Reflection::SINGLETON_CLASS_OF.bind_call(object))).freeze
82
93
  end
83
94
 
84
95
  # The public methods +owner+ itself defines in Ruby source.
@@ -17,6 +17,19 @@ module Kobako
17
17
  # callable exposes by default, so the floor and the Exposure agree.
18
18
  CALLABLE_ALLOW = %i[call [] yield arity lambda?].freeze
19
19
 
20
+ # Kernel's own reflection and dispatch, bound onto each object so a
21
+ # forwarder or a BasicObject answers from its own method table rather
22
+ # than through +method_missing+ / +respond_to_missing?+, for the floor,
23
+ # the Exposure, and the call alike.
24
+ CLASS_OF = Kernel.instance_method(:class)
25
+ IS_A = Kernel.instance_method(:is_a?)
26
+ SINGLETON_CLASS_OF = Kernel.instance_method(:singleton_class)
27
+ SINGLETON_METHODS = Kernel.instance_method(:singleton_methods)
28
+ PRIVATE_METHODS = Kernel.instance_method(:private_methods)
29
+ RESPOND_TO = Kernel.instance_method(:respond_to?)
30
+ PUBLIC_SEND = Kernel.instance_method(:public_send)
31
+ SEND = BasicObject.instance_method(:__send__)
32
+
20
33
  # The reason +name+ is unreachable through +exposure+, or +nil+ when
21
34
  # the dispatch may proceed. Composes the ambient-surface floor with the
22
35
  # reference's Exposure, in that order: the Exposure only narrows and
@@ -81,7 +94,7 @@ module Kobako
81
94
 
82
95
  klass = lookup_class(target)
83
96
  return public_method_refusal(target, name, klass) if klass.public_method_defined?(name)
84
- return "no public method #{name.inspect} on target" unless target.respond_to?(name)
97
+ return "no public method #{name.inspect} on target" unless RESPOND_TO.bind_call(target, name)
85
98
  return service_refusal(name) if klass.method_defined?(name) || klass.private_method_defined?(name)
86
99
 
87
100
  owner_refusal(target, name, klass)
@@ -117,9 +130,13 @@ module Kobako
117
130
  # singleton class is taken only when it already holds a public
118
131
  # method, since asking for it otherwise would create one.
119
132
  def lookup_class(target)
120
- return target.singleton_class if target.is_a?(Module) || target.singleton_methods.any?
133
+ return SINGLETON_CLASS_OF.bind_call(target) if module?(target) || SINGLETON_METHODS.bind_call(target).any?
134
+
135
+ CLASS_OF.bind_call(target)
136
+ end
121
137
 
122
- target.class
138
+ def module?(target)
139
+ IS_A.bind_call(target, Module)
123
140
  end
124
141
 
125
142
  # Whatever +Class+ inherits is surface every object shares rather
@@ -132,7 +149,7 @@ module Kobako
132
149
  def ambient_owner?(owner, target)
133
150
  owner >= Class ||
134
151
  GADGET_OWNERS.include?(owner) ||
135
- (target.is_a?(Module) && owner.singleton_class?)
152
+ (module?(target) && owner.singleton_class?)
136
153
  end
137
154
  end
138
155
  end