kobako 0.30.0 → 0.32.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 (288) hide show
  1. checksums.yaml +4 -4
  2. data/.githooks/linux-tests.sh +29 -0
  3. data/.release-please-manifest.json +1 -1
  4. data/CHANGELOG.md +460 -0
  5. data/Cargo.lock +868 -447
  6. data/README.md +43 -4
  7. data/ROADMAP.md +1 -2
  8. data/crates/kobako-runtime/CHANGELOG.md +309 -0
  9. data/crates/kobako-runtime/Cargo.toml +58 -11
  10. data/crates/kobako-runtime/README.md +7 -4
  11. data/crates/kobako-runtime/src/cred.rs +136 -0
  12. data/crates/kobako-runtime/src/dispatch.rs +4 -5
  13. data/crates/kobako-runtime/src/endpoint/buffer.rs +153 -44
  14. data/crates/kobako-runtime/src/endpoint/dispatch.rs +80 -41
  15. data/crates/kobako-runtime/src/endpoint/invoke.rs +23 -36
  16. data/crates/kobako-runtime/src/endpoint.rs +137 -36
  17. data/crates/kobako-runtime/src/errno.rs +391 -0
  18. data/crates/kobako-runtime/src/error.rs +31 -10
  19. data/crates/kobako-runtime/src/filesystem/anon_inodes.rs +52 -0
  20. data/crates/kobako-runtime/src/filesystem/bind/fid.rs +300 -22
  21. data/crates/kobako-runtime/src/filesystem/bind/lookup.rs +101 -29
  22. data/crates/kobako-runtime/src/filesystem/bind/unicode.rs +109 -0
  23. data/crates/kobako-runtime/src/filesystem/bind/walk.rs +52 -34
  24. data/crates/kobako-runtime/src/filesystem/bind.rs +812 -447
  25. data/crates/kobako-runtime/src/filesystem/overlay/file.rs +6 -6
  26. data/crates/kobako-runtime/src/filesystem/overlay/path.rs +43 -65
  27. data/crates/kobako-runtime/src/filesystem/overlay/stack/copy_up.rs +11 -16
  28. data/crates/kobako-runtime/src/filesystem/overlay/stack/dir.rs +47 -72
  29. data/crates/kobako-runtime/src/filesystem/overlay/stack/namei.rs +12 -12
  30. data/crates/kobako-runtime/src/filesystem/overlay/stack/readdir.rs +4 -7
  31. data/crates/kobako-runtime/src/filesystem/overlay/stack.rs +120 -37
  32. data/crates/kobako-runtime/src/filesystem/overlay.rs +578 -388
  33. data/crates/kobako-runtime/src/filesystem/pipe.rs +12 -9
  34. data/crates/kobako-runtime/src/filesystem/rdir.rs +14 -50
  35. data/crates/kobako-runtime/src/filesystem/testing.rs +31 -0
  36. data/crates/kobako-runtime/src/filesystem/tmpfs/seed.rs +32 -0
  37. data/crates/kobako-runtime/src/filesystem/tmpfs/tmpfile.rs +3 -3
  38. data/crates/kobako-runtime/src/filesystem/tmpfs/whiteout.rs +57 -35
  39. data/crates/kobako-runtime/src/filesystem/tmpfs/xattr.rs +2 -2
  40. data/crates/kobako-runtime/src/filesystem/tmpfs.rs +219 -173
  41. data/crates/kobako-runtime/src/filesystem/wrap.rs +36 -52
  42. data/crates/kobako-runtime/src/filesystem.rs +8 -3
  43. data/crates/kobako-runtime/src/fork.rs +319 -0
  44. data/crates/kobako-runtime/src/grant.rs +3 -0
  45. data/crates/kobako-runtime/src/hashtable.rs +4 -2
  46. data/crates/kobako-runtime/src/lib.rs +18 -7
  47. data/crates/kobako-runtime/src/limits.rs +171 -0
  48. data/crates/kobako-runtime/src/locking/seqlock.rs +29 -3
  49. data/crates/kobako-runtime/src/locking.rs +518 -113
  50. data/crates/kobako-runtime/src/memcg.rs +306 -0
  51. data/crates/kobako-runtime/src/mount.rs +37 -9
  52. data/crates/kobako-runtime/src/net/backends.rs +43 -0
  53. data/crates/kobako-runtime/src/net/connections.rs +219 -0
  54. data/crates/kobako-runtime/src/net/credential.rs +125 -0
  55. data/crates/kobako-runtime/src/net/egress.rs +180 -0
  56. data/crates/kobako-runtime/src/net/family.rs +181 -0
  57. data/crates/kobako-runtime/src/net/http.rs +3165 -0
  58. data/crates/kobako-runtime/src/net/lookup.rs +385 -0
  59. data/crates/kobako-runtime/src/net/mock.rs +251 -0
  60. data/crates/kobako-runtime/src/net/namespace.rs +1368 -0
  61. data/crates/kobako-runtime/src/net/reactor.rs +626 -0
  62. data/crates/kobako-runtime/src/net/resolv/darwin.rs +280 -0
  63. data/crates/kobako-runtime/src/net/resolv/stub/tests.rs +519 -0
  64. data/crates/kobako-runtime/src/net/resolv/stub.rs +542 -0
  65. data/crates/kobako-runtime/src/net/resolv/windows.rs +218 -0
  66. data/crates/kobako-runtime/src/net/resolv.rs +251 -0
  67. data/crates/kobako-runtime/src/net/skbuff.rs +109 -0
  68. data/crates/kobako-runtime/src/net/socket.rs +869 -0
  69. data/crates/kobako-runtime/src/net/task.rs +223 -0
  70. data/crates/kobako-runtime/src/net/tcp.rs +1840 -0
  71. data/crates/kobako-runtime/src/net/tls.rs +120 -0
  72. data/crates/kobako-runtime/src/net/transport.rs +304 -0
  73. data/crates/kobako-runtime/src/net/udp.rs +1105 -0
  74. data/crates/kobako-runtime/src/net.rs +45 -0
  75. data/crates/kobako-runtime/src/network/builder.rs +177 -0
  76. data/crates/kobako-runtime/src/network/ruleset.rs +777 -0
  77. data/crates/kobako-runtime/src/network.rs +551 -0
  78. data/crates/kobako-runtime/src/policy.rs +16 -0
  79. data/crates/kobako-runtime/src/profile.rs +29 -144
  80. data/crates/kobako-runtime/src/runtime.rs +3 -5
  81. data/crates/kobako-runtime/src/sched.rs +338 -51
  82. data/crates/kobako-runtime/src/security/entry_policy.rs +22 -0
  83. data/crates/kobako-runtime/src/{vfs/cred.rs → security/hooks.rs} +52 -144
  84. data/crates/kobako-runtime/src/security/mount_policy.rs +158 -0
  85. data/crates/kobako-runtime/src/security.rs +125 -209
  86. data/crates/kobako-runtime/src/snapshot.rs +121 -2
  87. data/crates/kobako-runtime/src/vfs/attr.rs +5 -5
  88. data/crates/kobako-runtime/src/vfs/dentry.rs +210 -117
  89. data/crates/kobako-runtime/src/vfs/file.rs +243 -248
  90. data/crates/kobako-runtime/src/vfs/inode.rs +41 -45
  91. data/crates/kobako-runtime/src/vfs/kern_path.rs +62 -53
  92. data/crates/kobako-runtime/src/vfs/mount.rs +10 -9
  93. data/crates/kobako-runtime/src/vfs/namei.rs +1162 -818
  94. data/crates/kobako-runtime/src/vfs/namespace.rs +245 -0
  95. data/crates/kobako-runtime/src/vfs/open_files.rs +69 -0
  96. data/crates/kobako-runtime/src/vfs/openat2.rs +3 -3
  97. data/crates/kobako-runtime/src/vfs/path.rs +6 -6
  98. data/crates/kobako-runtime/src/vfs/readdir.rs +6 -6
  99. data/crates/kobako-runtime/src/vfs/stat.rs +248 -23
  100. data/crates/kobako-runtime/src/vfs/super_block.rs +18 -14
  101. data/crates/kobako-runtime/src/vfs/utimes.rs +37 -27
  102. data/crates/kobako-runtime/src/vfs.rs +10 -6
  103. data/crates/kobako-runtime/src/wait.rs +381 -0
  104. data/crates/kobako-transport/CHANGELOG.md +38 -0
  105. data/crates/kobako-transport/Cargo.toml +1 -1
  106. data/crates/kobako-transport/README.md +3 -4
  107. data/crates/kobako-transport/src/abi.rs +78 -72
  108. data/crates/kobako-transport/src/envelope/bytes.rs +17 -6
  109. data/crates/kobako-transport/src/envelope/call.rs +32 -4
  110. data/crates/kobako-transport/src/envelope/error_record.rs +21 -0
  111. data/crates/kobako-transport/src/envelope/name.rs +99 -0
  112. data/crates/kobako-transport/src/envelope/outcome.rs +9 -9
  113. data/crates/kobako-transport/src/envelope/preamble.rs +24 -21
  114. data/crates/kobako-transport/src/envelope/reply.rs +27 -27
  115. data/crates/kobako-transport/src/envelope/unsupported.rs +10 -10
  116. data/crates/kobako-transport/src/envelope.rs +13 -8
  117. data/crates/kobako-wasmtime/CHANGELOG.md +208 -0
  118. data/crates/kobako-wasmtime/Cargo.toml +47 -7
  119. data/crates/kobako-wasmtime/README.md +4 -3
  120. data/crates/kobako-wasmtime/build/preview2.rs +1339 -0
  121. data/crates/kobako-wasmtime/build.rs +16 -0
  122. data/crates/kobako-wasmtime/src/abi.rs +18 -37
  123. data/crates/kobako-wasmtime/src/ambient.rs +43 -19
  124. data/crates/kobako-wasmtime/src/cache/unix.rs +95 -0
  125. data/crates/kobako-wasmtime/src/cache/windows.rs +359 -0
  126. data/crates/kobako-wasmtime/src/cache.rs +106 -113
  127. data/crates/kobako-wasmtime/src/config.rs +52 -68
  128. data/crates/kobako-wasmtime/src/driver.rs +80 -82
  129. data/crates/kobako-wasmtime/src/exports.rs +143 -17
  130. data/crates/kobako-wasmtime/src/guest.rs +12 -6
  131. data/crates/kobako-wasmtime/src/guest_binary.rs +14 -0
  132. data/crates/kobako-wasmtime/src/imports.rs +42 -18
  133. data/crates/kobako-wasmtime/src/instance_pre.rs +4 -4
  134. data/crates/kobako-wasmtime/src/invocation.rs +91 -15
  135. data/crates/kobako-wasmtime/src/lib.rs +7 -3
  136. data/crates/kobako-wasmtime/src/limiter.rs +130 -12
  137. data/crates/kobako-wasmtime/src/preview1/descriptors.rs +305 -0
  138. data/crates/kobako-wasmtime/src/preview1/differential/mount.rs +9 -20
  139. data/crates/kobako-wasmtime/src/preview1/differential.rs +27 -31
  140. data/crates/kobako-wasmtime/src/preview1/errno.rs +55 -66
  141. data/crates/kobako-wasmtime/src/preview1/fd.rs +347 -312
  142. data/crates/kobako-wasmtime/src/preview1/memory.rs +21 -12
  143. data/crates/kobako-wasmtime/src/preview1/mount_view/wrap.rs +34 -53
  144. data/crates/kobako-wasmtime/src/preview1/mount_view.rs +335 -388
  145. data/crates/kobako-wasmtime/src/preview1/path.rs +56 -121
  146. data/crates/kobako-wasmtime/src/preview1/poll.rs +255 -125
  147. data/crates/kobako-wasmtime/src/preview1/random.rs +125 -0
  148. data/crates/kobako-wasmtime/src/preview1/sock.rs +10 -8
  149. data/crates/kobako-wasmtime/src/preview1/testing.rs +31 -0
  150. data/crates/kobako-wasmtime/src/preview1/testsuite.rs +5 -15
  151. data/crates/kobako-wasmtime/src/preview1.rs +131 -51
  152. data/crates/kobako-wasmtime/src/preview2/abi.rs +227 -0
  153. data/crates/kobako-wasmtime/src/preview2/error.rs +118 -0
  154. data/crates/kobako-wasmtime/src/preview2/filesystem.rs +122 -0
  155. data/crates/kobako-wasmtime/src/preview2/host.rs +4319 -0
  156. data/crates/kobako-wasmtime/src/preview2/http.rs +1587 -0
  157. data/crates/kobako-wasmtime/src/preview2/io.rs +328 -0
  158. data/crates/kobako-wasmtime/src/preview2/sockets.rs +379 -0
  159. data/crates/kobako-wasmtime/src/preview2/table.rs +257 -0
  160. data/crates/kobako-wasmtime/src/preview2.rs +57 -0
  161. data/crates/kobako-wasmtime/src/trap.rs +88 -28
  162. data/crates/kobako-wasmtime/src/waits.rs +59 -0
  163. data/crates/kobako-wasmtime/src/wasi.rs +291 -81
  164. data/crates/kobako-wasmtime/wit/deps/cli.wit +233 -0
  165. data/crates/kobako-wasmtime/wit/deps/clocks.wit +162 -0
  166. data/crates/kobako-wasmtime/wit/deps/filesystem.wit +589 -0
  167. data/crates/kobako-wasmtime/wit/deps/http.wit +733 -0
  168. data/crates/kobako-wasmtime/wit/deps/io.wit +299 -0
  169. data/crates/kobako-wasmtime/wit/deps/random.wit +92 -0
  170. data/crates/kobako-wasmtime/wit/deps/sockets.wit +949 -0
  171. data/crates/kobako-wasmtime/witx/wasi_snapshot_preview1.witx +13 -0
  172. data/data/kobako.wasm +0 -0
  173. data/ext/kobako/Cargo.toml +6 -2
  174. data/ext/kobako/src/{runtime/errors.rs → errors.rs} +34 -26
  175. data/ext/kobako/src/escape.rs +404 -0
  176. data/ext/kobako/src/{runtime/filesystem → filesystem}/grants.rs +34 -32
  177. data/ext/kobako/src/{runtime/filesystem → filesystem}/ruby.rs +158 -170
  178. data/ext/kobako/src/{runtime/filesystem.rs → filesystem.rs} +71 -23
  179. data/ext/kobako/src/fork.rs +72 -0
  180. data/ext/kobako/src/gvl.rs +729 -0
  181. data/ext/kobako/src/handoff.rs +490 -0
  182. data/ext/kobako/src/lib.rs +74 -3
  183. data/ext/kobako/src/network/mock.rs +187 -0
  184. data/ext/kobako/src/network.rs +593 -0
  185. data/ext/kobako/src/pinned_value.rs +44 -0
  186. data/ext/kobako/src/runtime/bridge.rs +58 -50
  187. data/ext/kobako/src/runtime/snapshot.rs +4 -4
  188. data/ext/kobako/src/runtime.rs +272 -163
  189. data/ext/kobako/src/wire.rs +42 -0
  190. data/lib/kobako/catalog/extensions.rb +14 -2
  191. data/lib/kobako/catalog/flags.rb +3 -5
  192. data/lib/kobako/catalog/grants.rb +5 -4
  193. data/lib/kobako/catalog/handles.rb +14 -32
  194. data/lib/kobako/catalog/mount.rb +11 -3
  195. data/lib/kobako/catalog/network.rb +35 -0
  196. data/lib/kobako/catalog/replacements.rb +28 -0
  197. data/lib/kobako/catalog/services.rb +5 -19
  198. data/lib/kobako/catalog/snippets.rb +3 -18
  199. data/lib/kobako/catalog.rb +3 -0
  200. data/lib/kobako/codec/{utils.rb → boundary.rb} +4 -2
  201. data/lib/kobako/codec/decoder.rb +5 -5
  202. data/lib/kobako/codec/ext_types.rb +3 -3
  203. data/lib/kobako/codec/handle_walk.rb +3 -43
  204. data/lib/kobako/codec/state.rb +2 -18
  205. data/lib/kobako/codec/value_budget.rb +133 -41
  206. data/lib/kobako/codec.rb +6 -7
  207. data/lib/kobako/context.rb +24 -11
  208. data/lib/kobako/errors.rb +18 -18
  209. data/lib/kobako/extension.rb +3 -2
  210. data/lib/kobako/filesystem.rb +10 -3
  211. data/lib/kobako/fork.rb +42 -0
  212. data/lib/kobako/handle.rb +2 -3
  213. data/lib/kobako/names.rb +28 -0
  214. data/lib/kobako/network.rb +210 -0
  215. data/lib/kobako/outcome.rb +7 -7
  216. data/lib/kobako/runtime.rb +3 -2
  217. data/lib/kobako/sandbox.rb +73 -35
  218. data/lib/kobako/sandbox_options.rb +42 -109
  219. data/lib/kobako/transport/argument_wrap.rb +64 -0
  220. data/lib/kobako/transport/block_failure.rb +1 -1
  221. data/lib/kobako/transport/dispatcher.rb +6 -6
  222. data/lib/kobako/transport/exposure.rb +26 -15
  223. data/lib/kobako/transport/reflection.rb +21 -4
  224. data/lib/kobako/transport/run.rb +8 -23
  225. data/lib/kobako/transport/yielder.rb +2 -9
  226. data/lib/kobako/transport.rb +1 -0
  227. data/lib/kobako/usage.rb +4 -2
  228. data/lib/kobako/version.rb +1 -1
  229. data/lib/kobako.rb +8 -4
  230. data/release-please-config.json +15 -0
  231. data/sig/kobako/catalog/extensions.rbs +2 -0
  232. data/sig/kobako/catalog/grants.rbs +7 -4
  233. data/sig/kobako/catalog/handles.rbs +2 -2
  234. data/sig/kobako/catalog/mount.rbs +3 -1
  235. data/sig/kobako/catalog/network.rbs +20 -0
  236. data/sig/kobako/catalog/replacements.rbs +14 -0
  237. data/sig/kobako/catalog/services.rbs +0 -4
  238. data/sig/kobako/catalog/snippets.rbs +0 -2
  239. data/sig/kobako/codec/{utils.rbs → boundary.rbs} +2 -2
  240. data/sig/kobako/codec/handle_walk.rbs +0 -6
  241. data/sig/kobako/codec/state.rbs +0 -4
  242. data/sig/kobako/codec/value_budget.rbs +48 -5
  243. data/sig/kobako/codec.rbs +2 -2
  244. data/sig/kobako/context.rbs +3 -1
  245. data/sig/kobako/errors.rbs +4 -7
  246. data/sig/kobako/filesystem.rbs +21 -3
  247. data/sig/kobako/fork.rbs +17 -0
  248. data/sig/kobako/network.rbs +115 -0
  249. data/sig/kobako/outcome.rbs +1 -1
  250. data/sig/kobako/runtime.rbs +32 -3
  251. data/sig/kobako/sandbox.rbs +10 -2
  252. data/sig/kobako/sandbox_options.rbs +29 -40
  253. data/sig/kobako/transport/argument_wrap.rbs +15 -0
  254. data/sig/kobako/transport/error.rbs +1 -1
  255. data/sig/kobako/transport/exposure.rbs +2 -4
  256. data/sig/kobako/transport/reflection.rbs +10 -0
  257. data/sig/kobako/transport/run.rbs +0 -2
  258. data/sig/kobako/transport/yielder.rbs +0 -3
  259. data/sig/kobako/transport.rbs +1 -1
  260. data/sig/kobako.rbs +13 -0
  261. metadata +97 -37
  262. data/crates/kobako-runtime/src/endpoint/capture.rs +0 -115
  263. data/crates/kobako-runtime/src/endpoint/memory_cap.rs +0 -238
  264. data/crates/kobako-runtime/src/filesystem/path.rs +0 -21
  265. data/crates/kobako-runtime/src/vfs/errno.rs +0 -153
  266. data/crates/kobako-runtime/tests/linux_baseline/calls.rs +0 -171
  267. data/crates/kobako-runtime/tests/linux_baseline/concurrent.rs +0 -276
  268. data/crates/kobako-runtime/tests/linux_baseline/kobako.rs +0 -323
  269. data/crates/kobako-runtime/tests/linux_baseline/linux-baseline-expected.txt +0 -16
  270. data/crates/kobako-runtime/tests/linux_baseline/linux.rs +0 -449
  271. data/crates/kobako-runtime/tests/linux_baseline/main.rs +0 -611
  272. data/crates/kobako-runtime/tests/linux_baseline/permissions.rs +0 -236
  273. data/crates/kobako-wasmtime/src/descriptors.rs +0 -200
  274. data/crates/kobako-wasmtime/tests/integration/compile_posture.rs +0 -43
  275. data/crates/kobako-wasmtime/tests/integration/guest_file.rs +0 -1085
  276. data/crates/kobako-wasmtime/tests/integration/main.rs +0 -29
  277. data/crates/kobako-wasmtime/tests/integration/preamble_flags.rs +0 -70
  278. data/crates/kobako-wasmtime/tests/integration/snippet_compile.rs +0 -95
  279. data/crates/kobako-wasmtime/tests/integration/support/mod.rs +0 -30
  280. data/crates/kobako-wasmtime/tests/integration/unanswered_call.rs +0 -86
  281. data/crates/kobako-wasmtime/tests/integration/unsupported_call.rs +0 -51
  282. data/ext/kobako/src/runtime/escape.rs +0 -67
  283. data/ext/kobako/src/runtime/gvl.rs +0 -147
  284. data/ext/kobako/src/runtime/pinned_value.rs +0 -31
  285. data/lib/kobako/constant_name.rb +0 -8
  286. data/lib/kobako/pool.rb +0 -140
  287. data/sig/kobako/constant_name.rbs +0 -3
  288. data/sig/kobako/pool.rbs +0 -42
@@ -0,0 +1,136 @@
1
+ //! Who a call is made for, as Linux's `struct cred`: the security modules
2
+ //! registered for the caller, the memory charged on its behalf, the profile it
3
+ //! sees entries through, and its mount and inode number namespaces. The hooks that ask its
4
+ //! modules live with the security framework, as `security/security.c` holds
5
+ //! them apart from `kernel/cred.c`.
6
+
7
+ use std::sync::Arc;
8
+
9
+ use crate::memcg::MemoryBudget;
10
+ use crate::profile::Profile;
11
+ use crate::security::SecurityOperations;
12
+ use crate::vfs::{InoNamespace, MntNamespace, VfsMount};
13
+
14
+ /// Who a call is made for, holding the security modules registered for it, as
15
+ /// `cred->security` holds a Landlock domain, and the memory budget the guest's
16
+ /// growth is charged to, which every Cred restricted from it shares, as a
17
+ /// task's memory cgroup does. Its profile decides what the
18
+ /// caller sees of an entry, as `cred->user_ns` decides the owner `cp_new_stat`
19
+ /// reports. Its mount namespace says which mount stands on which name, as a
20
+ /// task's `nsproxy` holds one beside its credentials, and its inode number
21
+ /// namespace which number a hermetic caller knows each entry by, as a PID
22
+ /// namespace sits there too.
23
+ #[derive(Clone)]
24
+ pub struct Cred {
25
+ security: Vec<Arc<dyn SecurityOperations>>,
26
+ memory: Arc<MemoryBudget>,
27
+ profile: Profile,
28
+ mnt_ns: Arc<MntNamespace>,
29
+ ino_ns: Arc<InoNamespace>,
30
+ }
31
+
32
+ /// One no module is registered for, which this crate's tests make calls as.
33
+ #[cfg(test)]
34
+ impl Default for Cred {
35
+ fn default() -> Self {
36
+ Self {
37
+ security: Vec::new(),
38
+ memory: MemoryBudget::new(None),
39
+ profile: Profile::Hermetic,
40
+ mnt_ns: Arc::default(),
41
+ ino_ns: Arc::default(),
42
+ }
43
+ }
44
+ }
45
+
46
+ impl Cred {
47
+ /// One with `security` registered, its memory unbounded.
48
+ pub(crate) fn new(security: Vec<Arc<dyn SecurityOperations>>) -> Arc<Self> {
49
+ Self::with_memory(security, MemoryBudget::new(None))
50
+ }
51
+
52
+ /// One with `security` registered, charging what is held on its behalf to
53
+ /// `memory`, seeing entries as the hermetic profile does until told
54
+ /// otherwise, and numbering them afresh, which every Cred restricted from
55
+ /// it shares.
56
+ pub(crate) fn with_memory(
57
+ security: Vec<Arc<dyn SecurityOperations>>,
58
+ memory: Arc<MemoryBudget>,
59
+ ) -> Arc<Self> {
60
+ Arc::new(Self {
61
+ security,
62
+ memory,
63
+ profile: Profile::Hermetic,
64
+ mnt_ns: Arc::default(),
65
+ ino_ns: Arc::default(),
66
+ })
67
+ }
68
+
69
+ /// This Cred with `more` modules registered after its own, sharing the
70
+ /// memory held on its behalf, as `landlock_restrict_self` adds a domain.
71
+ pub(crate) fn with_modules(&self, more: Vec<Arc<dyn SecurityOperations>>) -> Arc<Self> {
72
+ let mut security = self.security.clone();
73
+ security.extend(more);
74
+ Arc::new(Self {
75
+ security,
76
+ ..self.clone()
77
+ })
78
+ }
79
+
80
+ /// This Cred with `mnt_ns` as its mount namespace, as a task that unshared
81
+ /// its mounts holds a namespace of its own.
82
+ pub(crate) fn with_mnt_ns(&self, mnt_ns: Arc<MntNamespace>) -> Arc<Self> {
83
+ Arc::new(Self {
84
+ mnt_ns,
85
+ ..self.clone()
86
+ })
87
+ }
88
+
89
+ /// The mount every guest path starts from, as a task's `fs->root`: the
90
+ /// Mount at `/`, or the read-only root the runtime made beneath the others;
91
+ /// none where no Mount was made.
92
+ pub fn root(&self) -> Option<Arc<VfsMount>> {
93
+ self.mnt_ns.root().cloned()
94
+ }
95
+
96
+ /// The mount a walk from the root of `mnt` stands on: the topmost of those
97
+ /// stacked over that root, as Linux's `topmost_overmount` answers.
98
+ pub fn topmost_overmount(&self, mnt: &Arc<VfsMount>) -> Arc<VfsMount> {
99
+ self.mnt_ns.topmost_overmount(mnt)
100
+ }
101
+
102
+ /// This Cred seeing entries as `profile` does, as a task's credentials
103
+ /// carry the user namespace its stat is reported in.
104
+ pub fn with_profile(&self, profile: Profile) -> Arc<Self> {
105
+ Arc::new(Self {
106
+ profile,
107
+ ..self.clone()
108
+ })
109
+ }
110
+
111
+ /// The security modules registered for the caller, in the order they are
112
+ /// asked.
113
+ pub(crate) fn modules(&self) -> &[Arc<dyn SecurityOperations>] {
114
+ &self.security
115
+ }
116
+
117
+ /// Which mount stands on which name for the caller.
118
+ pub(crate) fn mnt_ns(&self) -> &MntNamespace {
119
+ &self.mnt_ns
120
+ }
121
+
122
+ /// The isolation profile the caller runs under.
123
+ pub fn profile(&self) -> Profile {
124
+ self.profile
125
+ }
126
+
127
+ /// The budget a Wasm Driver charges the guest's growth to.
128
+ pub fn memory_budget(&self) -> &Arc<MemoryBudget> {
129
+ &self.memory
130
+ }
131
+
132
+ /// The inode numbers a hermetic caller knows its entries by.
133
+ pub(crate) fn ino_ns(&self) -> &InoNamespace {
134
+ &self.ino_ns
135
+ }
136
+ }
@@ -3,13 +3,12 @@
3
3
  //!
4
4
  //! The runtime decodes the Envelope and hands a handler the routed
5
5
  //! `Call` — target, method, block flag, and the payload bytes it never
6
- //! reads — then expects a `Reply` back. What the handler *is* — a Ruby
6
+ //! reads — then expects a `CallReply` back. What the handler *is* — a Ruby
7
7
  //! Proc, a Rust closure — is the frontend's concern; the runtime only sees
8
- //! this trait. The Ruby ext conforms by bridging its dispatch Proc behind
9
- //! it.
8
+ //! this trait.
10
9
 
11
10
  use crate::yielder::Yielder;
12
- use kobako_transport::envelope::{Call, Reply};
11
+ use kobako_transport::envelope::{Call, CallReply};
13
12
 
14
13
  /// Host-side handler for a guest→host dispatch.
15
14
  ///
@@ -24,7 +23,7 @@ use kobako_transport::envelope::{Call, Reply};
24
23
  /// The `Call` borrows the buffer the runtime decoded, so a handler passes
25
24
  /// the payload onward without copying it first.
26
25
  pub trait DispatchHandler: Send + Sync {
27
- fn dispatch(&self, call: Call<'_>, yielder: &mut dyn Yielder) -> Option<Reply>;
26
+ fn dispatch(&self, call: Call<'_>, yielder: &mut dyn Yielder) -> Option<CallReply>;
28
27
 
29
28
  /// Whether the host gave up on this invocation, as when one of its
30
29
  /// callbacks raised something meant for the host rather than the
@@ -1,18 +1,26 @@
1
- //! Guest linear-memory I/O for the ABI's buffer handoffs, whether the
1
+ //! Guest linear-memory I/O for the ABI's list handoffs, whether the
2
2
  //! invocation body or an upcall drives one.
3
3
  //!
4
- //! The two shared steps answer an error kind, and each caller words it,
5
- //! because depth decides what the same problem means: before the export
6
- //! call it is a `Trap` or an intact runtime, inside a callback it is a
7
- //! wire failure the guest receives.
4
+ //! Every list the ABI carries is named by a return area: two little-endian
5
+ //! u32, its place then its length, 4-byte aligned, as the Canonical ABI lays
6
+ //! a `list<u8>` out. The shared steps answer an error kind, and each caller
7
+ //! words it, because depth decides what the same problem means: before the
8
+ //! export call it is a `Trap`, inside a callback it is a wire failure the
9
+ //! guest receives.
8
10
 
9
- use kobako_transport::abi::{unpack_ptr_len, MAX_DISPATCH_PAYLOAD};
11
+ use kobako_transport::abi::MAX_MESSAGE_LEN;
10
12
 
11
13
  use super::{GuestInstance, RUNTIME_INCOMPATIBLE, SANDBOX_RUNTIME_NOT_KOBAKO};
12
14
 
13
15
  // The size-limit messages here and in the endpoint name the cap as 16 MiB, so
14
16
  // moving the cap fails the build until they are reworded with it.
15
- const _: () = assert!(MAX_DISPATCH_PAYLOAD == 16 << 20);
17
+ const _: () = assert!(MAX_MESSAGE_LEN == 16 << 20);
18
+
19
+ /// The bytes of a return area: a u32 place, then a u32 length.
20
+ const RETURN_AREA_SIZE: usize = 8;
21
+
22
+ /// The alignment a return area keeps, its u32's own.
23
+ const RETURN_AREA_ALIGN: usize = 4;
16
24
 
17
25
  /// Why a host write into guest memory failed.
18
26
  pub(super) enum WriteError<F> {
@@ -21,8 +29,6 @@ pub(super) enum WriteError<F> {
21
29
  /// message for it.
22
30
  Incompatible(&'static str),
23
31
  Trapped(F),
24
- /// The allocator ran and answered 0.
25
- OutOfMemory,
26
32
  OutOfBounds,
27
33
  }
28
34
 
@@ -32,8 +38,8 @@ pub(super) enum ReadError {
32
38
  OutOfBounds,
33
39
  }
34
40
 
35
- /// Allocate a guest buffer and copy `bytes` into it, answering the buffer's
36
- /// address.
41
+ /// Take room from the guest's `cabi_realloc` and copy `bytes` into it,
42
+ /// answering the room's address.
37
43
  pub(super) fn alloc_and_write<G: GuestInstance>(
38
44
  guest: &mut G,
39
45
  bytes: &[u8],
@@ -42,13 +48,11 @@ pub(super) fn alloc_and_write<G: GuestInstance>(
42
48
  if guest.memory().is_none() {
43
49
  return Err(WriteError::Incompatible(SANDBOX_RUNTIME_NOT_KOBAKO));
44
50
  }
51
+ // Fresh room for a `list<u8>`, which the Canonical ABI aligns to 1.
45
52
  let ptr = guest
46
- .alloc(len as u32)
53
+ .realloc(0, 0, 1, len as u32)
47
54
  .ok_or(WriteError::Incompatible(RUNTIME_INCOMPATIBLE))?
48
55
  .map_err(WriteError::Trapped)?;
49
- if ptr == 0 {
50
- return Err(WriteError::OutOfMemory);
51
- }
52
56
  let memory = guest
53
57
  .memory()
54
58
  .ok_or(WriteError::Incompatible(SANDBOX_RUNTIME_NOT_KOBAKO))?;
@@ -58,10 +62,43 @@ pub(super) fn alloc_and_write<G: GuestInstance>(
58
62
  Ok(ptr)
59
63
  }
60
64
 
65
+ /// Write `bytes` into the guest and name them in the return area at `ret`.
66
+ /// A return area the guest cannot hold is refused before the guest gives
67
+ /// room for the answer; memory only grows, so it still holds after.
68
+ pub(super) fn lower_list<G: GuestInstance>(
69
+ guest: &mut G,
70
+ bytes: &[u8],
71
+ ret: u32,
72
+ ) -> Result<(), WriteError<G::Fault>> {
73
+ let mem_size = guest
74
+ .memory()
75
+ .ok_or(WriteError::Incompatible(SANDBOX_RUNTIME_NOT_KOBAKO))?
76
+ .len();
77
+ let area = return_area_range(ret as usize, mem_size).map_err(|_| WriteError::OutOfBounds)?;
78
+ let ptr = alloc_and_write(guest, bytes)?;
79
+ let memory = guest
80
+ .memory()
81
+ .ok_or(WriteError::Incompatible(SANDBOX_RUNTIME_NOT_KOBAKO))?;
82
+ // The write has already held the length to the payload cap.
83
+ memory[area.start..area.start + 4].copy_from_slice(&ptr.to_le_bytes());
84
+ memory[area.start + 4..area.end].copy_from_slice(&(bytes.len() as u32).to_le_bytes());
85
+ Ok(())
86
+ }
87
+
88
+ /// Copy out the list the return area at `ret` names; a length past the
89
+ /// 16 MiB cap is refused before the list is touched.
90
+ pub(super) fn lift_list(memory: &[u8], ret: u32) -> Result<Vec<u8>, ReadError> {
91
+ let area = return_area_range(ret as usize, memory.len()).map_err(|_| ReadError::OutOfBounds)?;
92
+ let word =
93
+ |at: usize| u32::from_le_bytes(memory[at..at + 4].try_into().expect("a u32's width"));
94
+ let (ptr, len) = (word(area.start), word(area.start + 4));
95
+ read_buffer(memory, ptr as usize, len as usize)
96
+ }
97
+
61
98
  /// Copy out the buffer the guest names; a length past the 16 MiB cap is
62
99
  /// refused before memory is touched.
63
100
  pub(super) fn read_buffer(data: &[u8], ptr: usize, len: usize) -> Result<Vec<u8>, ReadError> {
64
- if len > MAX_DISPATCH_PAYLOAD {
101
+ if len > MAX_MESSAGE_LEN {
65
102
  return Err(ReadError::TooLarge);
66
103
  }
67
104
  let range = guest_buffer_range(ptr, len, data.len()).map_err(|_| ReadError::OutOfBounds)?;
@@ -71,7 +108,7 @@ pub(super) fn read_buffer(data: &[u8], ptr: usize, len: usize) -> Result<Vec<u8>
71
108
  /// Every host write boundary routes its length through here, so the
72
109
  /// wire-violation reason is uniform.
73
110
  pub(super) fn checked_payload_len(len: usize) -> Result<i32, &'static str> {
74
- if len > MAX_DISPATCH_PAYLOAD {
111
+ if len > MAX_MESSAGE_LEN {
75
112
  return Err("payload exceeds the 16 MiB limit");
76
113
  }
77
114
  // The cap above sits below `i32::MAX`, so this conversion cannot wrap.
@@ -91,29 +128,61 @@ fn guest_buffer_range(
91
128
  Ok(ptr..end)
92
129
  }
93
130
 
94
- /// The `(ptr, len)` the guest packs for any answer it hands back, the
95
- /// outcome and a block's result alike.
96
- pub(super) fn unpack_guest_buffer(packed: u64) -> (usize, usize) {
97
- let (ptr, len) = unpack_ptr_len(packed);
98
- (ptr as usize, len as usize)
131
+ /// The bytes of guest memory the return area at `ret` covers, refused off
132
+ /// its alignment or reaching past memory.
133
+ fn return_area_range(ret: usize, mem_size: usize) -> Result<core::ops::Range<usize>, &'static str> {
134
+ if !ret.is_multiple_of(RETURN_AREA_ALIGN) {
135
+ return Err("the return area is not aligned");
136
+ }
137
+ guest_buffer_range(ret, RETURN_AREA_SIZE, mem_size)
99
138
  }
100
139
 
101
140
  #[cfg(test)]
102
141
  mod tests {
103
- use super::{
104
- checked_payload_len, guest_buffer_range, unpack_guest_buffer, MAX_DISPATCH_PAYLOAD,
105
- };
142
+ use super::{checked_payload_len, guest_buffer_range, lift_list, lower_list, MAX_MESSAGE_LEN};
143
+ use crate::endpoint::{Calls, GuestInstance};
144
+ use crate::error::Trap;
145
+
146
+ /// 64 bytes of guest memory whose allocator counts the room it gave.
147
+ #[derive(Default)]
148
+ struct Counted {
149
+ memory: Vec<u8>,
150
+ given: u32,
151
+ calls: Calls<String>,
152
+ }
153
+
154
+ impl GuestInstance for Counted {
155
+ type Fault = String;
156
+
157
+ fn calls(&mut self) -> &mut Calls<String> {
158
+ &mut self.calls
159
+ }
160
+
161
+ fn memory(&mut self) -> Option<&mut [u8]> {
162
+ Some(&mut self.memory)
163
+ }
164
+
165
+ fn realloc(&mut self, _: u32, _: u32, _: u32, _: u32) -> Option<Result<u32, String>> {
166
+ self.given += 1;
167
+ Some(Ok(32))
168
+ }
169
+
170
+ fn downcall(&mut self, _ptr: i32, _len: i32) -> Option<Result<u32, String>> {
171
+ None
172
+ }
173
+
174
+ fn cap_trap_of(_fault: &String) -> Option<Trap> {
175
+ None
176
+ }
177
+ }
106
178
 
107
179
  // @behavior WE-059
108
180
  #[test]
109
181
  fn a_transfer_of_nothing_or_at_the_cap_is_admitted() {
110
182
  assert_eq!(
111
- (
112
- checked_payload_len(0),
113
- checked_payload_len(MAX_DISPATCH_PAYLOAD)
114
- ),
115
- (Ok(0), Ok(MAX_DISPATCH_PAYLOAD as i32)),
116
- "0 bytes and MAX_DISPATCH_PAYLOAD bytes through checked_payload_len must be admitted"
183
+ (checked_payload_len(0), checked_payload_len(MAX_MESSAGE_LEN)),
184
+ (Ok(0), Ok(MAX_MESSAGE_LEN as i32)),
185
+ "0 bytes and MAX_MESSAGE_LEN bytes through checked_payload_len must be admitted"
117
186
  );
118
187
  }
119
188
 
@@ -121,9 +190,9 @@ mod tests {
121
190
  #[test]
122
191
  fn a_transfer_past_the_cap_is_refused() {
123
192
  assert!(
124
- checked_payload_len(MAX_DISPATCH_PAYLOAD + 1).is_err()
193
+ checked_payload_len(MAX_MESSAGE_LEN + 1).is_err()
125
194
  && checked_payload_len(usize::MAX).is_err(),
126
- "one byte past MAX_DISPATCH_PAYLOAD and usize::MAX through checked_payload_len must be \
195
+ "one byte past MAX_MESSAGE_LEN and usize::MAX through checked_payload_len must be \
127
196
  refused"
128
197
  );
129
198
  }
@@ -169,24 +238,64 @@ mod tests {
169
238
  );
170
239
  }
171
240
 
172
- // @behavior WE-001
241
+ /// 64 bytes of memory holding `answer` at 32, named by a return area at 8.
242
+ fn memory_naming(answer: &[u8]) -> Vec<u8> {
243
+ let mut memory = vec![0; 64];
244
+ memory[8..12].copy_from_slice(&32u32.to_le_bytes());
245
+ memory[12..16].copy_from_slice(&(answer.len() as u32).to_le_bytes());
246
+ memory[32..32 + answer.len()].copy_from_slice(answer);
247
+ memory
248
+ }
249
+
250
+ // @behavior WE-092
173
251
  #[test]
174
- fn a_packed_answer_carries_the_pointer_high_and_the_length_low() {
252
+ fn a_return_area_names_its_answers_place_then_its_length() {
253
+ let memory = memory_naming(b"answer");
254
+
175
255
  assert_eq!(
176
- unpack_guest_buffer(0xAABB_CCDD_1122_3344),
177
- (0xAABB_CCDD, 0x1122_3344),
178
- "0xAABB_CCDD_1122_3344 through unpack_guest_buffer must answer pointer 0xAABB_CCDD and \
179
- length 0x1122_3344"
256
+ lift_list(&memory, 8).ok(),
257
+ Some(b"answer".to_vec()),
258
+ "a return area holding 32 then 6, read by the host, must answer the 6 bytes at 32"
180
259
  );
181
260
  }
182
261
 
183
- // @behavior WE-065
262
+ // @behavior WE-093
184
263
  #[test]
185
- fn a_packed_answer_of_nothing_is_a_zero_pair() {
264
+ fn a_return_area_off_its_alignment_or_past_memory_is_refused() {
265
+ let mut memory = memory_naming(b"answer");
266
+ // At 1, off alignment, the same place and length the area at 8 holds.
267
+ memory.copy_within(8..16, 1);
268
+
269
+ assert!(
270
+ [1, 60, u32::MAX - 3]
271
+ .iter()
272
+ .all(|&ret| lift_list(&memory, ret).is_err()),
273
+ "a return area at 1 (unaligned, naming a list inside memory), 60 (past 64 bytes), or \
274
+ u32::MAX - 3, read by the host, must be refused"
275
+ );
276
+ }
277
+
278
+ // @behavior WE-093
279
+ #[test]
280
+ fn an_answer_for_a_return_area_the_guest_cannot_hold_is_never_copied() {
281
+ let mut guest = Counted {
282
+ memory: vec![0; 64],
283
+ ..Counted::default()
284
+ };
285
+ let refused = [1, 60, u32::MAX - 3]
286
+ .iter()
287
+ .all(|&ret| lower_list(&mut guest, b"answer", ret).is_err());
288
+
186
289
  assert_eq!(
187
- unpack_guest_buffer(0),
188
- (0, 0),
189
- "0 through unpack_guest_buffer must answer pointer 0 and length 0"
290
+ (
291
+ refused,
292
+ guest.given,
293
+ guest.memory.iter().all(|&byte| byte == 0)
294
+ ),
295
+ (true, 0, true),
296
+ "an answer written for a return area at 1 (unaligned), 60 (past 64 bytes), or \
297
+ u32::MAX - 3 must be refused before the guest's allocator gives it room or any byte \
298
+ lands in guest memory"
190
299
  );
191
300
  }
192
301
  }
@@ -1,7 +1,8 @@
1
- //! Host side of the `__kobako_upcall` import: hand the guest's Call to
2
- //! the bound `DispatchHandler` and write its Reply back into guest memory.
3
- //! A request under a tag the host does not serve is answered as
4
- //! unsupported, so a guest asking for a later operation can fall back.
1
+ //! Host side of the `upcall` and `preamble` imports: hand the guest's Call
2
+ //! to the bound `DispatchHandler` and write its Reply back into guest
3
+ //! memory, or write the Invocation's Preamble there. A request under a tag
4
+ //! the host does not serve is answered as unsupported, so a guest asking
5
+ //! for a later operation can fall back.
5
6
  //!
6
7
  //! A failure lands on whoever is answerable for it, and always says why.
7
8
  //! A trap the guest raises during a callback into it ends the invocation
@@ -12,13 +13,12 @@
12
13
 
13
14
  use std::sync::Arc;
14
15
 
15
- use kobako_transport::abi::pack_ptr_len;
16
16
  use kobako_transport::envelope::{
17
- Downcall, Fault, FaultKind, Reply, Unsupported, Upcall, YieldReply,
17
+ CallReply, Downcall, Fault, FaultKind, Unsupported, Upcall, YieldReply,
18
18
  };
19
19
 
20
20
  use super::buffer::{
21
- alloc_and_write, checked_payload_len, read_buffer, unpack_guest_buffer, ReadError, WriteError,
21
+ alloc_and_write, checked_payload_len, lift_list, lower_list, read_buffer, ReadError, WriteError,
22
22
  };
23
23
  use super::{GuestInstance, RUNTIME_INCOMPATIBLE, SANDBOX_RUNTIME_NOT_KOBAKO};
24
24
  use crate::dispatch::DispatchHandler;
@@ -62,8 +62,9 @@ impl<F> Calls<F> {
62
62
  }
63
63
  }
64
64
 
65
- /// How an upcall ends an invocation instead of answering.
66
- pub enum UpcallEnd<F> {
65
+ /// How a host import, the upcall or the Preamble's, ends an invocation
66
+ /// instead of answering.
67
+ pub enum ImportEnd<F> {
67
68
  /// The guest trapped during a callback into it.
68
69
  Fault(F),
69
70
  /// The host gave the invocation up.
@@ -92,32 +93,55 @@ impl<G: GuestInstance> Yielder for FrameYielder<'_, G> {
92
93
  }
93
94
  }
94
95
 
95
- /// Answer one `__kobako_upcall` call with the packed `(ptr<<32)|len` of
96
- /// the Reply, or what ends the invocation instead.
96
+ /// Answer one `upcall` with the Reply, named in the return area at `ret`,
97
+ /// or what ends the invocation instead.
97
98
  pub fn handle<G: GuestInstance>(
98
99
  guest: &mut G,
99
100
  req_ptr: i32,
100
101
  req_len: i32,
101
- ) -> Result<i64, UpcallEnd<G::Fault>> {
102
+ ret: i32,
103
+ ) -> Result<(), ImportEnd<G::Fault>> {
102
104
  let reply = reply_to(guest, req_ptr, req_len)
103
- .unwrap_or_else(|reason| Reply::Fault(Fault::new(FaultKind::Internal, reason)));
104
- let written = write_reply(guest, &fit_in_message(reply).encode());
105
+ .unwrap_or_else(|reason| CallReply::Fault(Fault::new(FaultKind::Internal, reason)));
106
+ answer_import(guest, &fit_in_message(reply).encode(), ret)
107
+ }
108
+
109
+ /// Answer one `preamble` call with `preamble`, the Invocation's Preamble
110
+ /// bytes, named in the return area at `ret`. Each call writes the same
111
+ /// bytes afresh, so the guest may ask as often as it likes.
112
+ pub fn hand_preamble<G: GuestInstance>(
113
+ guest: &mut G,
114
+ preamble: &[u8],
115
+ ret: i32,
116
+ ) -> Result<(), ImportEnd<G::Fault>> {
117
+ answer_import(guest, preamble, ret)
118
+ }
119
+
120
+ /// Name `answer` in the return area at `ret`, or end the invocation: a trap
121
+ /// a callback raised comes first, then a host that gave the invocation up,
122
+ /// then an answer that could not be written.
123
+ fn answer_import<G: GuestInstance>(
124
+ guest: &mut G,
125
+ answer: &[u8],
126
+ ret: i32,
127
+ ) -> Result<(), ImportEnd<G::Fault>> {
128
+ let written = lower_from_callback(guest, answer, ret as u32);
105
129
  if let Some(trap) = guest.calls().reentry_trap.take() {
106
- return Err(UpcallEnd::Fault(trap));
130
+ return Err(ImportEnd::Fault(trap));
107
131
  }
108
132
  if guest.calls().abandoned() {
109
- return Err(UpcallEnd::Abandoned);
133
+ return Err(ImportEnd::Abandoned);
110
134
  }
111
- written.map_err(UpcallEnd::Unwritable)
135
+ written.map_err(ImportEnd::Unwritable)
112
136
  }
113
137
 
114
138
  /// An answer past the message cap is the Service's to change, as any other
115
139
  /// answer the host cannot write is.
116
- fn fit_in_message(reply: Reply) -> Reply {
140
+ fn fit_in_message(reply: CallReply) -> CallReply {
117
141
  let bytes = reply.encode().len();
118
142
  match checked_payload_len(bytes) {
119
143
  Ok(_) => reply,
120
- Err(limit) => Reply::Fault(Fault::new(
144
+ Err(limit) => CallReply::Fault(Fault::new(
121
145
  FaultKind::Runtime,
122
146
  format!("Sandbox could not write the Service's answer: {limit}"),
123
147
  )),
@@ -128,7 +152,7 @@ fn reply_to<G: GuestInstance>(
128
152
  guest: &mut G,
129
153
  req_ptr: i32,
130
154
  req_len: i32,
131
- ) -> Result<Reply, &'static str> {
155
+ ) -> Result<CallReply, &'static str> {
132
156
  let req_bytes = read_request(guest, req_ptr, req_len)?;
133
157
  // The endpoint decodes the Envelope, here and in `FrameYielder`, so
134
158
  // the frontend never sees a frame; the payload inside it stays bytes
@@ -136,7 +160,7 @@ fn reply_to<G: GuestInstance>(
136
160
  let call = match Upcall::decode(&req_bytes) {
137
161
  Ok(Upcall::Call(call)) => call,
138
162
  Ok(_) => {
139
- return Ok(Reply::Unsupported(Unsupported::new(
163
+ return Ok(CallReply::Unsupported(Unsupported::new(
140
164
  "the host does not serve this request",
141
165
  )))
142
166
  }
@@ -174,12 +198,6 @@ fn reply_to<G: GuestInstance>(
174
198
  )
175
199
  }
176
200
 
177
- fn write_reply<G: GuestInstance>(guest: &mut G, bytes: &[u8]) -> Result<i64, &'static str> {
178
- let ptr = write_from_callback(guest, bytes)?;
179
- // The write has already held the length to the payload cap.
180
- Ok(pack_ptr_len(ptr, bytes.len() as u32) as i64)
181
- }
182
-
183
201
  /// The request the guest hands the upcall import. Its `(ptr, len)` are
184
202
  /// guest addresses, so they are read unsigned.
185
203
  fn read_request<G: GuestInstance>(
@@ -205,13 +223,35 @@ fn keep_trap<G: GuestInstance>(
205
223
  reason
206
224
  }
207
225
 
208
- /// Write `bytes` into the guest from inside a callback. A guest that has
209
- /// already trapped is not called again.
226
+ /// Write `bytes` into the guest from inside a callback, answering where. A
227
+ /// guest that has already trapped is not called again.
210
228
  fn write_from_callback<G: GuestInstance>(guest: &mut G, bytes: &[u8]) -> Result<u32, &'static str> {
211
- if guest.calls().reentry_trap.is_some() {
212
- return Err("the Sandbox already trapped during this invocation");
229
+ refuse_after_trap(guest)?;
230
+ alloc_and_write(guest, bytes).map_err(|err| keep_write_failure(guest, err))
231
+ }
232
+
233
+ /// Write `bytes` into the guest from inside a callback and name them in the
234
+ /// return area at `ret`, as a host import answers a list.
235
+ fn lower_from_callback<G: GuestInstance>(
236
+ guest: &mut G,
237
+ bytes: &[u8],
238
+ ret: u32,
239
+ ) -> Result<(), &'static str> {
240
+ refuse_after_trap(guest)?;
241
+ lower_list(guest, bytes, ret).map_err(|err| keep_write_failure(guest, err))
242
+ }
243
+
244
+ fn refuse_after_trap<G: GuestInstance>(guest: &mut G) -> Result<(), &'static str> {
245
+ match guest.calls().reentry_trap {
246
+ Some(_) => Err("the Sandbox already trapped during this invocation"),
247
+ None => Ok(()),
213
248
  }
214
- alloc_and_write(guest, bytes).map_err(|err| match err {
249
+ }
250
+
251
+ /// Keep a trap a failed write raised, as `keep_trap` does, and answer why
252
+ /// the write failed.
253
+ fn keep_write_failure<G: GuestInstance>(guest: &mut G, err: WriteError<G::Fault>) -> &'static str {
254
+ match err {
215
255
  WriteError::TooLarge => "payload exceeds the 16 MiB limit",
216
256
  WriteError::Incompatible(message) => message,
217
257
  WriteError::Trapped(trap) => keep_trap(
@@ -219,9 +259,8 @@ fn write_from_callback<G: GuestInstance>(guest: &mut G, bytes: &[u8]) -> Result<
219
259
  trap,
220
260
  "the Sandbox trapped while allocating memory for the request",
221
261
  ),
222
- WriteError::OutOfMemory => "the Sandbox ran out of memory while preparing the request",
223
262
  WriteError::OutOfBounds => "could not write the request into the Sandbox's memory",
224
- })
263
+ }
225
264
  }
226
265
 
227
266
  fn drive_yield<G: GuestInstance>(
@@ -237,17 +276,17 @@ fn drive_yield<G: GuestInstance>(
237
276
  let len_i32 = checked_payload_len(request.len())?;
238
277
  let req_ptr = write_from_callback(guest, &request)? as i32;
239
278
 
240
- let packed = guest
279
+ let ret = guest
241
280
  .downcall(req_ptr, len_i32)
242
281
  .ok_or(RUNTIME_INCOMPATIBLE)?
243
282
  .map_err(|trap| keep_trap(guest, trap, "the Sandbox trapped while invoking a block"))?;
244
- let (reply_ptr, reply_len) = unpack_guest_buffer(packed);
245
- if reply_len == 0 {
246
- return Err("the Sandbox returned an empty block result");
247
- }
248
283
  let memory = guest.memory().ok_or(SANDBOX_RUNTIME_NOT_KOBAKO)?;
249
- read_buffer(memory, reply_ptr, reply_len).map_err(|err| match err {
284
+ let reply = lift_list(memory, ret).map_err(|err| match err {
250
285
  ReadError::TooLarge => "block result payload exceeds the 16 MiB limit",
251
286
  ReadError::OutOfBounds => "the Sandbox returned an out-of-bounds block result",
252
- })
287
+ })?;
288
+ if reply.is_empty() {
289
+ return Err("the Sandbox returned an empty block result");
290
+ }
291
+ Ok(reply)
253
292
  }