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.
- checksums.yaml +4 -4
- data/.release-please-manifest.json +1 -1
- data/CHANGELOG.md +335 -0
- data/Cargo.lock +469 -11
- data/README.md +43 -3
- data/ROADMAP.md +1 -1
- data/crates/kobako-runtime/CHANGELOG.md +219 -0
- data/crates/kobako-runtime/Cargo.toml +51 -3
- data/crates/kobako-runtime/README.md +7 -4
- data/crates/kobako-runtime/src/cred.rs +124 -0
- data/crates/kobako-runtime/src/dispatch.rs +4 -5
- data/crates/kobako-runtime/src/endpoint/buffer.rs +153 -44
- data/crates/kobako-runtime/src/endpoint/dispatch.rs +80 -41
- data/crates/kobako-runtime/src/endpoint/invoke.rs +23 -36
- data/crates/kobako-runtime/src/endpoint.rs +137 -36
- data/crates/kobako-runtime/src/errno.rs +391 -0
- data/crates/kobako-runtime/src/error.rs +31 -10
- data/crates/kobako-runtime/src/filesystem/anon_inodes.rs +52 -0
- data/crates/kobako-runtime/src/filesystem/bind/lookup.rs +52 -14
- data/crates/kobako-runtime/src/filesystem/bind/walk.rs +19 -20
- data/crates/kobako-runtime/src/filesystem/bind.rs +437 -245
- data/crates/kobako-runtime/src/filesystem/overlay/file.rs +6 -6
- data/crates/kobako-runtime/src/filesystem/overlay/path.rs +43 -65
- data/crates/kobako-runtime/src/filesystem/overlay/stack/copy_up.rs +9 -16
- data/crates/kobako-runtime/src/filesystem/overlay/stack/dir.rs +47 -72
- data/crates/kobako-runtime/src/filesystem/overlay/stack/namei.rs +12 -12
- data/crates/kobako-runtime/src/filesystem/overlay/stack/readdir.rs +4 -7
- data/crates/kobako-runtime/src/filesystem/overlay/stack.rs +121 -37
- data/crates/kobako-runtime/src/filesystem/overlay.rs +302 -343
- data/crates/kobako-runtime/src/filesystem/pipe.rs +12 -9
- data/crates/kobako-runtime/src/filesystem/rdir.rs +15 -32
- data/crates/kobako-runtime/src/filesystem/tmpfs/seed.rs +32 -0
- data/crates/kobako-runtime/src/filesystem/tmpfs/tmpfile.rs +3 -3
- data/crates/kobako-runtime/src/filesystem/tmpfs/whiteout.rs +57 -35
- data/crates/kobako-runtime/src/filesystem/tmpfs/xattr.rs +2 -2
- data/crates/kobako-runtime/src/filesystem/tmpfs.rs +172 -160
- data/crates/kobako-runtime/src/filesystem/wrap.rs +36 -52
- data/crates/kobako-runtime/src/filesystem.rs +6 -3
- data/crates/kobako-runtime/src/fork.rs +96 -0
- data/crates/kobako-runtime/src/grant.rs +3 -0
- data/crates/kobako-runtime/src/hashtable.rs +4 -2
- data/crates/kobako-runtime/src/lib.rs +18 -7
- data/crates/kobako-runtime/src/limits.rs +195 -0
- data/crates/kobako-runtime/src/locking.rs +10 -0
- data/crates/kobako-runtime/src/memcg.rs +383 -0
- data/crates/kobako-runtime/src/mount.rs +67 -9
- data/crates/kobako-runtime/src/net/backends.rs +43 -0
- data/crates/kobako-runtime/src/net/connections.rs +179 -0
- data/crates/kobako-runtime/src/net/credential.rs +125 -0
- data/crates/kobako-runtime/src/net/egress.rs +160 -0
- data/crates/kobako-runtime/src/net/family.rs +181 -0
- data/crates/kobako-runtime/src/net/http.rs +2733 -0
- data/crates/kobako-runtime/src/net/lookup.rs +285 -0
- data/crates/kobako-runtime/src/net/mock.rs +251 -0
- data/crates/kobako-runtime/src/net/namespace.rs +1366 -0
- data/crates/kobako-runtime/src/net/reactor.rs +531 -0
- data/crates/kobako-runtime/src/net/resolv/darwin.rs +275 -0
- data/crates/kobako-runtime/src/net/resolv/stub/tests.rs +469 -0
- data/crates/kobako-runtime/src/net/resolv/stub.rs +536 -0
- data/crates/kobako-runtime/src/net/resolv/windows.rs +248 -0
- data/crates/kobako-runtime/src/net/resolv.rs +227 -0
- data/crates/kobako-runtime/src/net/socket.rs +870 -0
- data/crates/kobako-runtime/src/net/tcp.rs +1588 -0
- data/crates/kobako-runtime/src/net/tls.rs +58 -0
- data/crates/kobako-runtime/src/net/transport.rs +279 -0
- data/crates/kobako-runtime/src/net/udp.rs +1003 -0
- data/crates/kobako-runtime/src/net.rs +42 -0
- data/crates/kobako-runtime/src/network/builder.rs +177 -0
- data/crates/kobako-runtime/src/network/ruleset.rs +777 -0
- data/crates/kobako-runtime/src/network.rs +551 -0
- data/crates/kobako-runtime/src/policy.rs +16 -0
- data/crates/kobako-runtime/src/profile.rs +29 -144
- data/crates/kobako-runtime/src/runtime.rs +3 -5
- data/crates/kobako-runtime/src/sched.rs +130 -29
- data/crates/kobako-runtime/src/security/entry_policy.rs +22 -0
- data/crates/kobako-runtime/src/{vfs/cred.rs → security/hooks.rs} +52 -144
- data/crates/kobako-runtime/src/security/mount_policy.rs +158 -0
- data/crates/kobako-runtime/src/security.rs +101 -196
- data/crates/kobako-runtime/src/snapshot.rs +121 -2
- data/crates/kobako-runtime/src/vfs/attr.rs +5 -5
- data/crates/kobako-runtime/src/vfs/dentry.rs +143 -89
- data/crates/kobako-runtime/src/vfs/file.rs +199 -203
- data/crates/kobako-runtime/src/vfs/inode.rs +41 -45
- data/crates/kobako-runtime/src/vfs/kern_path.rs +38 -42
- data/crates/kobako-runtime/src/vfs/mount.rs +10 -9
- data/crates/kobako-runtime/src/vfs/namei.rs +829 -700
- data/crates/kobako-runtime/src/vfs/namespace.rs +162 -0
- data/crates/kobako-runtime/src/vfs/open_files.rs +69 -0
- data/crates/kobako-runtime/src/vfs/openat2.rs +3 -3
- data/crates/kobako-runtime/src/vfs/path.rs +6 -6
- data/crates/kobako-runtime/src/vfs/readdir.rs +6 -6
- data/crates/kobako-runtime/src/vfs/stat.rs +159 -23
- data/crates/kobako-runtime/src/vfs/super_block.rs +18 -14
- data/crates/kobako-runtime/src/vfs/utimes.rs +37 -27
- data/crates/kobako-runtime/src/vfs.rs +9 -6
- data/crates/kobako-runtime/src/wait.rs +356 -0
- data/crates/kobako-transport/CHANGELOG.md +31 -0
- data/crates/kobako-transport/Cargo.toml +1 -1
- data/crates/kobako-transport/README.md +3 -4
- data/crates/kobako-transport/src/abi.rs +78 -72
- data/crates/kobako-transport/src/envelope/bytes.rs +17 -6
- data/crates/kobako-transport/src/envelope/call.rs +32 -4
- data/crates/kobako-transport/src/envelope/error_record.rs +21 -0
- data/crates/kobako-transport/src/envelope/name.rs +99 -0
- data/crates/kobako-transport/src/envelope/outcome.rs +9 -9
- data/crates/kobako-transport/src/envelope/preamble.rs +24 -21
- data/crates/kobako-transport/src/envelope/reply.rs +27 -27
- data/crates/kobako-transport/src/envelope/unsupported.rs +10 -10
- data/crates/kobako-transport/src/envelope.rs +13 -8
- data/crates/kobako-wasmtime/CHANGELOG.md +161 -0
- data/crates/kobako-wasmtime/Cargo.toml +26 -7
- data/crates/kobako-wasmtime/README.md +3 -2
- data/crates/kobako-wasmtime/build/preview2.rs +1339 -0
- data/crates/kobako-wasmtime/build.rs +16 -0
- data/crates/kobako-wasmtime/src/abi.rs +17 -37
- data/crates/kobako-wasmtime/src/ambient.rs +33 -18
- data/crates/kobako-wasmtime/src/cache.rs +23 -15
- data/crates/kobako-wasmtime/src/config.rs +52 -68
- data/crates/kobako-wasmtime/src/driver.rs +48 -63
- data/crates/kobako-wasmtime/src/exports.rs +143 -17
- data/crates/kobako-wasmtime/src/guest.rs +12 -6
- data/crates/kobako-wasmtime/src/guest_binary.rs +14 -0
- data/crates/kobako-wasmtime/src/imports.rs +42 -18
- data/crates/kobako-wasmtime/src/instance_pre.rs +4 -4
- data/crates/kobako-wasmtime/src/invocation.rs +74 -12
- data/crates/kobako-wasmtime/src/lib.rs +5 -3
- data/crates/kobako-wasmtime/src/limiter.rs +70 -5
- data/crates/kobako-wasmtime/src/preview1/descriptors.rs +309 -0
- data/crates/kobako-wasmtime/src/preview1/differential/mount.rs +5 -15
- data/crates/kobako-wasmtime/src/preview1/differential.rs +27 -31
- data/crates/kobako-wasmtime/src/preview1/errno.rs +55 -66
- data/crates/kobako-wasmtime/src/preview1/fd.rs +327 -296
- data/crates/kobako-wasmtime/src/preview1/memory.rs +21 -12
- data/crates/kobako-wasmtime/src/preview1/mount_view/wrap.rs +34 -53
- data/crates/kobako-wasmtime/src/preview1/mount_view.rs +231 -217
- data/crates/kobako-wasmtime/src/preview1/path.rs +56 -121
- data/crates/kobako-wasmtime/src/preview1/poll.rs +244 -120
- data/crates/kobako-wasmtime/src/preview1/sock.rs +10 -8
- data/crates/kobako-wasmtime/src/preview1/testsuite.rs +5 -15
- data/crates/kobako-wasmtime/src/preview1.rs +46 -42
- data/crates/kobako-wasmtime/src/preview2/abi.rs +227 -0
- data/crates/kobako-wasmtime/src/preview2/error.rs +118 -0
- data/crates/kobako-wasmtime/src/preview2/filesystem.rs +122 -0
- data/crates/kobako-wasmtime/src/preview2/host.rs +3959 -0
- data/crates/kobako-wasmtime/src/preview2/http.rs +1503 -0
- data/crates/kobako-wasmtime/src/preview2/io.rs +315 -0
- data/crates/kobako-wasmtime/src/preview2/sockets.rs +379 -0
- data/crates/kobako-wasmtime/src/preview2/table.rs +305 -0
- data/crates/kobako-wasmtime/src/preview2.rs +57 -0
- data/crates/kobako-wasmtime/src/trap.rs +24 -9
- data/crates/kobako-wasmtime/src/wasi.rs +210 -81
- data/crates/kobako-wasmtime/wit/deps/cli.wit +233 -0
- data/crates/kobako-wasmtime/wit/deps/clocks.wit +162 -0
- data/crates/kobako-wasmtime/wit/deps/filesystem.wit +589 -0
- data/crates/kobako-wasmtime/wit/deps/http.wit +733 -0
- data/crates/kobako-wasmtime/wit/deps/io.wit +299 -0
- data/crates/kobako-wasmtime/wit/deps/random.wit +92 -0
- data/crates/kobako-wasmtime/wit/deps/sockets.wit +949 -0
- data/data/kobako.wasm +0 -0
- data/ext/kobako/Cargo.toml +1 -1
- data/ext/kobako/src/{runtime/errors.rs → errors.rs} +34 -26
- data/ext/kobako/src/{runtime/escape.rs → escape.rs} +42 -11
- data/ext/kobako/src/{runtime/filesystem → filesystem}/grants.rs +30 -26
- data/ext/kobako/src/{runtime/filesystem → filesystem}/ruby.rs +149 -168
- data/ext/kobako/src/{runtime/filesystem.rs → filesystem.rs} +24 -15
- data/ext/kobako/src/gvl.rs +306 -0
- data/ext/kobako/src/lib.rs +64 -3
- data/ext/kobako/src/network/mock.rs +187 -0
- data/ext/kobako/src/network.rs +591 -0
- data/ext/kobako/src/runtime/bridge.rs +30 -29
- data/ext/kobako/src/runtime/snapshot.rs +4 -4
- data/ext/kobako/src/runtime.rs +221 -143
- data/ext/kobako/src/wire.rs +42 -0
- data/lib/kobako/catalog/extensions.rb +14 -2
- data/lib/kobako/catalog/flags.rb +3 -5
- data/lib/kobako/catalog/grants.rb +5 -4
- data/lib/kobako/catalog/handles.rb +5 -27
- data/lib/kobako/catalog/mount.rb +11 -3
- data/lib/kobako/catalog/network.rb +35 -0
- data/lib/kobako/catalog/replacements.rb +28 -0
- data/lib/kobako/catalog/services.rb +5 -19
- data/lib/kobako/catalog/snippets.rb +3 -18
- data/lib/kobako/catalog.rb +3 -0
- data/lib/kobako/codec/{utils.rb → boundary.rb} +4 -2
- data/lib/kobako/codec/decoder.rb +4 -4
- data/lib/kobako/codec/ext_types.rb +3 -3
- data/lib/kobako/codec/handle_walk.rb +3 -43
- data/lib/kobako/codec/value_budget.rb +132 -40
- data/lib/kobako/codec.rb +1 -1
- data/lib/kobako/context.rb +24 -11
- data/lib/kobako/errors.rb +15 -12
- data/lib/kobako/extension.rb +3 -2
- data/lib/kobako/filesystem.rb +9 -2
- data/lib/kobako/handle.rb +2 -2
- data/lib/kobako/names.rb +28 -0
- data/lib/kobako/network.rb +210 -0
- data/lib/kobako/outcome.rb +7 -7
- data/lib/kobako/pool.rb +45 -23
- data/lib/kobako/runtime.rb +3 -2
- data/lib/kobako/sandbox.rb +92 -35
- data/lib/kobako/sandbox_options.rb +41 -106
- data/lib/kobako/transport/argument_wrap.rb +64 -0
- data/lib/kobako/transport/block_failure.rb +1 -1
- data/lib/kobako/transport/dispatcher.rb +6 -6
- data/lib/kobako/transport/exposure.rb +26 -15
- data/lib/kobako/transport/reflection.rb +21 -4
- data/lib/kobako/transport/run.rb +8 -23
- data/lib/kobako/transport/yielder.rb +2 -9
- data/lib/kobako/transport.rb +1 -0
- data/lib/kobako/version.rb +1 -1
- data/lib/kobako.rb +6 -2
- data/release-please-config.json +15 -0
- data/sig/core/thread.rbs +6 -0
- data/sig/kobako/catalog/extensions.rbs +2 -0
- data/sig/kobako/catalog/grants.rbs +7 -4
- data/sig/kobako/catalog/handles.rbs +0 -2
- data/sig/kobako/catalog/mount.rbs +3 -1
- data/sig/kobako/catalog/network.rbs +20 -0
- data/sig/kobako/catalog/replacements.rbs +14 -0
- data/sig/kobako/catalog/services.rbs +0 -4
- data/sig/kobako/catalog/snippets.rbs +0 -2
- data/sig/kobako/codec/{utils.rbs → boundary.rbs} +2 -2
- data/sig/kobako/codec/handle_walk.rbs +0 -6
- data/sig/kobako/codec/value_budget.rbs +47 -4
- data/sig/kobako/context.rbs +3 -1
- data/sig/kobako/errors.rbs +4 -4
- data/sig/kobako/filesystem.rbs +21 -3
- data/sig/kobako/network.rbs +115 -0
- data/sig/kobako/outcome.rbs +1 -1
- data/sig/kobako/pool.rbs +7 -3
- data/sig/kobako/runtime.rbs +32 -3
- data/sig/kobako/sandbox.rbs +19 -2
- data/sig/kobako/sandbox_options.rbs +29 -38
- data/sig/kobako/transport/argument_wrap.rbs +15 -0
- data/sig/kobako/transport/error.rbs +1 -1
- data/sig/kobako/transport/exposure.rbs +2 -4
- data/sig/kobako/transport/reflection.rbs +10 -0
- data/sig/kobako/transport/run.rbs +0 -2
- data/sig/kobako/transport/yielder.rbs +0 -3
- data/sig/kobako/transport.rbs +1 -1
- data/sig/kobako.rbs +13 -0
- metadata +84 -35
- data/crates/kobako-runtime/src/endpoint/capture.rs +0 -115
- data/crates/kobako-runtime/src/endpoint/memory_cap.rs +0 -238
- data/crates/kobako-runtime/src/filesystem/path.rs +0 -21
- data/crates/kobako-runtime/src/vfs/errno.rs +0 -153
- data/crates/kobako-runtime/tests/linux_baseline/calls.rs +0 -171
- data/crates/kobako-runtime/tests/linux_baseline/concurrent.rs +0 -276
- data/crates/kobako-runtime/tests/linux_baseline/kobako.rs +0 -323
- data/crates/kobako-runtime/tests/linux_baseline/linux-baseline-expected.txt +0 -16
- data/crates/kobako-runtime/tests/linux_baseline/linux.rs +0 -449
- data/crates/kobako-runtime/tests/linux_baseline/main.rs +0 -611
- data/crates/kobako-runtime/tests/linux_baseline/permissions.rs +0 -236
- data/crates/kobako-wasmtime/src/descriptors.rs +0 -200
- data/crates/kobako-wasmtime/tests/integration/compile_posture.rs +0 -43
- data/crates/kobako-wasmtime/tests/integration/guest_file.rs +0 -1085
- data/crates/kobako-wasmtime/tests/integration/main.rs +0 -29
- data/crates/kobako-wasmtime/tests/integration/preamble_flags.rs +0 -70
- data/crates/kobako-wasmtime/tests/integration/snippet_compile.rs +0 -95
- data/crates/kobako-wasmtime/tests/integration/support/mod.rs +0 -30
- data/crates/kobako-wasmtime/tests/integration/unanswered_call.rs +0 -86
- data/crates/kobako-wasmtime/tests/integration/unsupported_call.rs +0 -51
- data/ext/kobako/src/runtime/gvl.rs +0 -147
- data/lib/kobako/constant_name.rb +0 -8
- data/sig/kobako/constant_name.rbs +0 -3
- /data/ext/kobako/src/{runtime/pinned_value.rs → pinned_value.rs} +0 -0
data/lib/kobako/sandbox.rb
CHANGED
|
@@ -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
|
|
35
|
-
#
|
|
36
|
-
#
|
|
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
|
|
62
|
-
#
|
|
63
|
-
#
|
|
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
|
|
75
|
-
# invocation,
|
|
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,
|
|
96
|
-
#
|
|
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,
|
|
100
|
-
# unknown access,
|
|
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
|
|
118
|
-
#
|
|
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
|
|
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,
|
|
148
|
-
#
|
|
149
|
-
#
|
|
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
|
|
160
|
-
#
|
|
161
|
-
#
|
|
162
|
-
#
|
|
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
|
|
192
|
+
raise TypeError, "code must be a String, got #{code.class}" unless code.is_a?(String)
|
|
166
193
|
|
|
167
|
-
|
|
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
|
-
|
|
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
|
|
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
|
-
|
|
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.
|
|
8
|
-
# default, and +nil+ switches
|
|
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
|
|
19
|
-
# [+
|
|
20
|
-
#
|
|
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
|
|
23
|
-
#
|
|
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
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
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
|
|
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
|
|
@@ -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.
|
|
118
|
+
Reflection::PUBLIC_SEND.bind(target).call(name, *args, &block)
|
|
119
119
|
else
|
|
120
|
-
target.
|
|
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
|
-
|
|
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
|
|
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
|
|
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
|
-
|
|
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
|
-
(
|
|
152
|
+
(module?(target) && owner.singleton_class?)
|
|
136
153
|
end
|
|
137
154
|
end
|
|
138
155
|
end
|