kobako 0.30.0 → 0.31.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (266) hide show
  1. checksums.yaml +4 -4
  2. data/.release-please-manifest.json +1 -1
  3. data/CHANGELOG.md +335 -0
  4. data/Cargo.lock +469 -11
  5. data/README.md +43 -3
  6. data/ROADMAP.md +1 -1
  7. data/crates/kobako-runtime/CHANGELOG.md +219 -0
  8. data/crates/kobako-runtime/Cargo.toml +51 -3
  9. data/crates/kobako-runtime/README.md +7 -4
  10. data/crates/kobako-runtime/src/cred.rs +124 -0
  11. data/crates/kobako-runtime/src/dispatch.rs +4 -5
  12. data/crates/kobako-runtime/src/endpoint/buffer.rs +153 -44
  13. data/crates/kobako-runtime/src/endpoint/dispatch.rs +80 -41
  14. data/crates/kobako-runtime/src/endpoint/invoke.rs +23 -36
  15. data/crates/kobako-runtime/src/endpoint.rs +137 -36
  16. data/crates/kobako-runtime/src/errno.rs +391 -0
  17. data/crates/kobako-runtime/src/error.rs +31 -10
  18. data/crates/kobako-runtime/src/filesystem/anon_inodes.rs +52 -0
  19. data/crates/kobako-runtime/src/filesystem/bind/lookup.rs +52 -14
  20. data/crates/kobako-runtime/src/filesystem/bind/walk.rs +19 -20
  21. data/crates/kobako-runtime/src/filesystem/bind.rs +437 -245
  22. data/crates/kobako-runtime/src/filesystem/overlay/file.rs +6 -6
  23. data/crates/kobako-runtime/src/filesystem/overlay/path.rs +43 -65
  24. data/crates/kobako-runtime/src/filesystem/overlay/stack/copy_up.rs +9 -16
  25. data/crates/kobako-runtime/src/filesystem/overlay/stack/dir.rs +47 -72
  26. data/crates/kobako-runtime/src/filesystem/overlay/stack/namei.rs +12 -12
  27. data/crates/kobako-runtime/src/filesystem/overlay/stack/readdir.rs +4 -7
  28. data/crates/kobako-runtime/src/filesystem/overlay/stack.rs +121 -37
  29. data/crates/kobako-runtime/src/filesystem/overlay.rs +302 -343
  30. data/crates/kobako-runtime/src/filesystem/pipe.rs +12 -9
  31. data/crates/kobako-runtime/src/filesystem/rdir.rs +15 -32
  32. data/crates/kobako-runtime/src/filesystem/tmpfs/seed.rs +32 -0
  33. data/crates/kobako-runtime/src/filesystem/tmpfs/tmpfile.rs +3 -3
  34. data/crates/kobako-runtime/src/filesystem/tmpfs/whiteout.rs +57 -35
  35. data/crates/kobako-runtime/src/filesystem/tmpfs/xattr.rs +2 -2
  36. data/crates/kobako-runtime/src/filesystem/tmpfs.rs +172 -160
  37. data/crates/kobako-runtime/src/filesystem/wrap.rs +36 -52
  38. data/crates/kobako-runtime/src/filesystem.rs +6 -3
  39. data/crates/kobako-runtime/src/fork.rs +96 -0
  40. data/crates/kobako-runtime/src/grant.rs +3 -0
  41. data/crates/kobako-runtime/src/hashtable.rs +4 -2
  42. data/crates/kobako-runtime/src/lib.rs +18 -7
  43. data/crates/kobako-runtime/src/limits.rs +195 -0
  44. data/crates/kobako-runtime/src/locking.rs +10 -0
  45. data/crates/kobako-runtime/src/memcg.rs +383 -0
  46. data/crates/kobako-runtime/src/mount.rs +67 -9
  47. data/crates/kobako-runtime/src/net/backends.rs +43 -0
  48. data/crates/kobako-runtime/src/net/connections.rs +179 -0
  49. data/crates/kobako-runtime/src/net/credential.rs +125 -0
  50. data/crates/kobako-runtime/src/net/egress.rs +160 -0
  51. data/crates/kobako-runtime/src/net/family.rs +181 -0
  52. data/crates/kobako-runtime/src/net/http.rs +2733 -0
  53. data/crates/kobako-runtime/src/net/lookup.rs +285 -0
  54. data/crates/kobako-runtime/src/net/mock.rs +251 -0
  55. data/crates/kobako-runtime/src/net/namespace.rs +1366 -0
  56. data/crates/kobako-runtime/src/net/reactor.rs +531 -0
  57. data/crates/kobako-runtime/src/net/resolv/darwin.rs +275 -0
  58. data/crates/kobako-runtime/src/net/resolv/stub/tests.rs +469 -0
  59. data/crates/kobako-runtime/src/net/resolv/stub.rs +536 -0
  60. data/crates/kobako-runtime/src/net/resolv/windows.rs +248 -0
  61. data/crates/kobako-runtime/src/net/resolv.rs +227 -0
  62. data/crates/kobako-runtime/src/net/socket.rs +870 -0
  63. data/crates/kobako-runtime/src/net/tcp.rs +1588 -0
  64. data/crates/kobako-runtime/src/net/tls.rs +58 -0
  65. data/crates/kobako-runtime/src/net/transport.rs +279 -0
  66. data/crates/kobako-runtime/src/net/udp.rs +1003 -0
  67. data/crates/kobako-runtime/src/net.rs +42 -0
  68. data/crates/kobako-runtime/src/network/builder.rs +177 -0
  69. data/crates/kobako-runtime/src/network/ruleset.rs +777 -0
  70. data/crates/kobako-runtime/src/network.rs +551 -0
  71. data/crates/kobako-runtime/src/policy.rs +16 -0
  72. data/crates/kobako-runtime/src/profile.rs +29 -144
  73. data/crates/kobako-runtime/src/runtime.rs +3 -5
  74. data/crates/kobako-runtime/src/sched.rs +130 -29
  75. data/crates/kobako-runtime/src/security/entry_policy.rs +22 -0
  76. data/crates/kobako-runtime/src/{vfs/cred.rs → security/hooks.rs} +52 -144
  77. data/crates/kobako-runtime/src/security/mount_policy.rs +158 -0
  78. data/crates/kobako-runtime/src/security.rs +101 -196
  79. data/crates/kobako-runtime/src/snapshot.rs +121 -2
  80. data/crates/kobako-runtime/src/vfs/attr.rs +5 -5
  81. data/crates/kobako-runtime/src/vfs/dentry.rs +143 -89
  82. data/crates/kobako-runtime/src/vfs/file.rs +199 -203
  83. data/crates/kobako-runtime/src/vfs/inode.rs +41 -45
  84. data/crates/kobako-runtime/src/vfs/kern_path.rs +38 -42
  85. data/crates/kobako-runtime/src/vfs/mount.rs +10 -9
  86. data/crates/kobako-runtime/src/vfs/namei.rs +829 -700
  87. data/crates/kobako-runtime/src/vfs/namespace.rs +162 -0
  88. data/crates/kobako-runtime/src/vfs/open_files.rs +69 -0
  89. data/crates/kobako-runtime/src/vfs/openat2.rs +3 -3
  90. data/crates/kobako-runtime/src/vfs/path.rs +6 -6
  91. data/crates/kobako-runtime/src/vfs/readdir.rs +6 -6
  92. data/crates/kobako-runtime/src/vfs/stat.rs +159 -23
  93. data/crates/kobako-runtime/src/vfs/super_block.rs +18 -14
  94. data/crates/kobako-runtime/src/vfs/utimes.rs +37 -27
  95. data/crates/kobako-runtime/src/vfs.rs +9 -6
  96. data/crates/kobako-runtime/src/wait.rs +356 -0
  97. data/crates/kobako-transport/CHANGELOG.md +31 -0
  98. data/crates/kobako-transport/Cargo.toml +1 -1
  99. data/crates/kobako-transport/README.md +3 -4
  100. data/crates/kobako-transport/src/abi.rs +78 -72
  101. data/crates/kobako-transport/src/envelope/bytes.rs +17 -6
  102. data/crates/kobako-transport/src/envelope/call.rs +32 -4
  103. data/crates/kobako-transport/src/envelope/error_record.rs +21 -0
  104. data/crates/kobako-transport/src/envelope/name.rs +99 -0
  105. data/crates/kobako-transport/src/envelope/outcome.rs +9 -9
  106. data/crates/kobako-transport/src/envelope/preamble.rs +24 -21
  107. data/crates/kobako-transport/src/envelope/reply.rs +27 -27
  108. data/crates/kobako-transport/src/envelope/unsupported.rs +10 -10
  109. data/crates/kobako-transport/src/envelope.rs +13 -8
  110. data/crates/kobako-wasmtime/CHANGELOG.md +161 -0
  111. data/crates/kobako-wasmtime/Cargo.toml +26 -7
  112. data/crates/kobako-wasmtime/README.md +3 -2
  113. data/crates/kobako-wasmtime/build/preview2.rs +1339 -0
  114. data/crates/kobako-wasmtime/build.rs +16 -0
  115. data/crates/kobako-wasmtime/src/abi.rs +17 -37
  116. data/crates/kobako-wasmtime/src/ambient.rs +33 -18
  117. data/crates/kobako-wasmtime/src/cache.rs +23 -15
  118. data/crates/kobako-wasmtime/src/config.rs +52 -68
  119. data/crates/kobako-wasmtime/src/driver.rs +48 -63
  120. data/crates/kobako-wasmtime/src/exports.rs +143 -17
  121. data/crates/kobako-wasmtime/src/guest.rs +12 -6
  122. data/crates/kobako-wasmtime/src/guest_binary.rs +14 -0
  123. data/crates/kobako-wasmtime/src/imports.rs +42 -18
  124. data/crates/kobako-wasmtime/src/instance_pre.rs +4 -4
  125. data/crates/kobako-wasmtime/src/invocation.rs +74 -12
  126. data/crates/kobako-wasmtime/src/lib.rs +5 -3
  127. data/crates/kobako-wasmtime/src/limiter.rs +70 -5
  128. data/crates/kobako-wasmtime/src/preview1/descriptors.rs +309 -0
  129. data/crates/kobako-wasmtime/src/preview1/differential/mount.rs +5 -15
  130. data/crates/kobako-wasmtime/src/preview1/differential.rs +27 -31
  131. data/crates/kobako-wasmtime/src/preview1/errno.rs +55 -66
  132. data/crates/kobako-wasmtime/src/preview1/fd.rs +327 -296
  133. data/crates/kobako-wasmtime/src/preview1/memory.rs +21 -12
  134. data/crates/kobako-wasmtime/src/preview1/mount_view/wrap.rs +34 -53
  135. data/crates/kobako-wasmtime/src/preview1/mount_view.rs +231 -217
  136. data/crates/kobako-wasmtime/src/preview1/path.rs +56 -121
  137. data/crates/kobako-wasmtime/src/preview1/poll.rs +244 -120
  138. data/crates/kobako-wasmtime/src/preview1/sock.rs +10 -8
  139. data/crates/kobako-wasmtime/src/preview1/testsuite.rs +5 -15
  140. data/crates/kobako-wasmtime/src/preview1.rs +46 -42
  141. data/crates/kobako-wasmtime/src/preview2/abi.rs +227 -0
  142. data/crates/kobako-wasmtime/src/preview2/error.rs +118 -0
  143. data/crates/kobako-wasmtime/src/preview2/filesystem.rs +122 -0
  144. data/crates/kobako-wasmtime/src/preview2/host.rs +3959 -0
  145. data/crates/kobako-wasmtime/src/preview2/http.rs +1503 -0
  146. data/crates/kobako-wasmtime/src/preview2/io.rs +315 -0
  147. data/crates/kobako-wasmtime/src/preview2/sockets.rs +379 -0
  148. data/crates/kobako-wasmtime/src/preview2/table.rs +305 -0
  149. data/crates/kobako-wasmtime/src/preview2.rs +57 -0
  150. data/crates/kobako-wasmtime/src/trap.rs +24 -9
  151. data/crates/kobako-wasmtime/src/wasi.rs +210 -81
  152. data/crates/kobako-wasmtime/wit/deps/cli.wit +233 -0
  153. data/crates/kobako-wasmtime/wit/deps/clocks.wit +162 -0
  154. data/crates/kobako-wasmtime/wit/deps/filesystem.wit +589 -0
  155. data/crates/kobako-wasmtime/wit/deps/http.wit +733 -0
  156. data/crates/kobako-wasmtime/wit/deps/io.wit +299 -0
  157. data/crates/kobako-wasmtime/wit/deps/random.wit +92 -0
  158. data/crates/kobako-wasmtime/wit/deps/sockets.wit +949 -0
  159. data/data/kobako.wasm +0 -0
  160. data/ext/kobako/Cargo.toml +1 -1
  161. data/ext/kobako/src/{runtime/errors.rs → errors.rs} +34 -26
  162. data/ext/kobako/src/{runtime/escape.rs → escape.rs} +42 -11
  163. data/ext/kobako/src/{runtime/filesystem → filesystem}/grants.rs +30 -26
  164. data/ext/kobako/src/{runtime/filesystem → filesystem}/ruby.rs +149 -168
  165. data/ext/kobako/src/{runtime/filesystem.rs → filesystem.rs} +24 -15
  166. data/ext/kobako/src/gvl.rs +306 -0
  167. data/ext/kobako/src/lib.rs +64 -3
  168. data/ext/kobako/src/network/mock.rs +187 -0
  169. data/ext/kobako/src/network.rs +591 -0
  170. data/ext/kobako/src/runtime/bridge.rs +30 -29
  171. data/ext/kobako/src/runtime/snapshot.rs +4 -4
  172. data/ext/kobako/src/runtime.rs +221 -143
  173. data/ext/kobako/src/wire.rs +42 -0
  174. data/lib/kobako/catalog/extensions.rb +14 -2
  175. data/lib/kobako/catalog/flags.rb +3 -5
  176. data/lib/kobako/catalog/grants.rb +5 -4
  177. data/lib/kobako/catalog/handles.rb +5 -27
  178. data/lib/kobako/catalog/mount.rb +11 -3
  179. data/lib/kobako/catalog/network.rb +35 -0
  180. data/lib/kobako/catalog/replacements.rb +28 -0
  181. data/lib/kobako/catalog/services.rb +5 -19
  182. data/lib/kobako/catalog/snippets.rb +3 -18
  183. data/lib/kobako/catalog.rb +3 -0
  184. data/lib/kobako/codec/{utils.rb → boundary.rb} +4 -2
  185. data/lib/kobako/codec/decoder.rb +4 -4
  186. data/lib/kobako/codec/ext_types.rb +3 -3
  187. data/lib/kobako/codec/handle_walk.rb +3 -43
  188. data/lib/kobako/codec/value_budget.rb +132 -40
  189. data/lib/kobako/codec.rb +1 -1
  190. data/lib/kobako/context.rb +24 -11
  191. data/lib/kobako/errors.rb +15 -12
  192. data/lib/kobako/extension.rb +3 -2
  193. data/lib/kobako/filesystem.rb +9 -2
  194. data/lib/kobako/handle.rb +2 -2
  195. data/lib/kobako/names.rb +28 -0
  196. data/lib/kobako/network.rb +210 -0
  197. data/lib/kobako/outcome.rb +7 -7
  198. data/lib/kobako/pool.rb +45 -23
  199. data/lib/kobako/runtime.rb +3 -2
  200. data/lib/kobako/sandbox.rb +92 -35
  201. data/lib/kobako/sandbox_options.rb +41 -106
  202. data/lib/kobako/transport/argument_wrap.rb +64 -0
  203. data/lib/kobako/transport/block_failure.rb +1 -1
  204. data/lib/kobako/transport/dispatcher.rb +6 -6
  205. data/lib/kobako/transport/exposure.rb +26 -15
  206. data/lib/kobako/transport/reflection.rb +21 -4
  207. data/lib/kobako/transport/run.rb +8 -23
  208. data/lib/kobako/transport/yielder.rb +2 -9
  209. data/lib/kobako/transport.rb +1 -0
  210. data/lib/kobako/version.rb +1 -1
  211. data/lib/kobako.rb +6 -2
  212. data/release-please-config.json +15 -0
  213. data/sig/core/thread.rbs +6 -0
  214. data/sig/kobako/catalog/extensions.rbs +2 -0
  215. data/sig/kobako/catalog/grants.rbs +7 -4
  216. data/sig/kobako/catalog/handles.rbs +0 -2
  217. data/sig/kobako/catalog/mount.rbs +3 -1
  218. data/sig/kobako/catalog/network.rbs +20 -0
  219. data/sig/kobako/catalog/replacements.rbs +14 -0
  220. data/sig/kobako/catalog/services.rbs +0 -4
  221. data/sig/kobako/catalog/snippets.rbs +0 -2
  222. data/sig/kobako/codec/{utils.rbs → boundary.rbs} +2 -2
  223. data/sig/kobako/codec/handle_walk.rbs +0 -6
  224. data/sig/kobako/codec/value_budget.rbs +47 -4
  225. data/sig/kobako/context.rbs +3 -1
  226. data/sig/kobako/errors.rbs +4 -4
  227. data/sig/kobako/filesystem.rbs +21 -3
  228. data/sig/kobako/network.rbs +115 -0
  229. data/sig/kobako/outcome.rbs +1 -1
  230. data/sig/kobako/pool.rbs +7 -3
  231. data/sig/kobako/runtime.rbs +32 -3
  232. data/sig/kobako/sandbox.rbs +19 -2
  233. data/sig/kobako/sandbox_options.rbs +29 -38
  234. data/sig/kobako/transport/argument_wrap.rbs +15 -0
  235. data/sig/kobako/transport/error.rbs +1 -1
  236. data/sig/kobako/transport/exposure.rbs +2 -4
  237. data/sig/kobako/transport/reflection.rbs +10 -0
  238. data/sig/kobako/transport/run.rbs +0 -2
  239. data/sig/kobako/transport/yielder.rbs +0 -3
  240. data/sig/kobako/transport.rbs +1 -1
  241. data/sig/kobako.rbs +13 -0
  242. metadata +84 -35
  243. data/crates/kobako-runtime/src/endpoint/capture.rs +0 -115
  244. data/crates/kobako-runtime/src/endpoint/memory_cap.rs +0 -238
  245. data/crates/kobako-runtime/src/filesystem/path.rs +0 -21
  246. data/crates/kobako-runtime/src/vfs/errno.rs +0 -153
  247. data/crates/kobako-runtime/tests/linux_baseline/calls.rs +0 -171
  248. data/crates/kobako-runtime/tests/linux_baseline/concurrent.rs +0 -276
  249. data/crates/kobako-runtime/tests/linux_baseline/kobako.rs +0 -323
  250. data/crates/kobako-runtime/tests/linux_baseline/linux-baseline-expected.txt +0 -16
  251. data/crates/kobako-runtime/tests/linux_baseline/linux.rs +0 -449
  252. data/crates/kobako-runtime/tests/linux_baseline/main.rs +0 -611
  253. data/crates/kobako-runtime/tests/linux_baseline/permissions.rs +0 -236
  254. data/crates/kobako-wasmtime/src/descriptors.rs +0 -200
  255. data/crates/kobako-wasmtime/tests/integration/compile_posture.rs +0 -43
  256. data/crates/kobako-wasmtime/tests/integration/guest_file.rs +0 -1085
  257. data/crates/kobako-wasmtime/tests/integration/main.rs +0 -29
  258. data/crates/kobako-wasmtime/tests/integration/preamble_flags.rs +0 -70
  259. data/crates/kobako-wasmtime/tests/integration/snippet_compile.rs +0 -95
  260. data/crates/kobako-wasmtime/tests/integration/support/mod.rs +0 -30
  261. data/crates/kobako-wasmtime/tests/integration/unanswered_call.rs +0 -86
  262. data/crates/kobako-wasmtime/tests/integration/unsupported_call.rs +0 -51
  263. data/ext/kobako/src/runtime/gvl.rs +0 -147
  264. data/lib/kobako/constant_name.rb +0 -8
  265. data/sig/kobako/constant_name.rbs +0 -3
  266. /data/ext/kobako/src/{runtime/pinned_value.rs → pinned_value.rs} +0 -0
@@ -1,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
  }
@@ -1,27 +1,21 @@
1
- //! Per-invocation I/O between the host and the guest: it frames what the guest
2
- //! is handed, writes the entry's downcall into a freshly allocated guest buffer,
3
- //! and reads the Outcome the downcall answered with.
1
+ //! Per-invocation I/O between the host and the guest: it encodes the Preamble
2
+ //! the guest takes, writes the entry's downcall into room the guest gives, and
3
+ //! reads the Outcome the downcall answered with.
4
4
 
5
- use kobako_transport::abi::FRAME_LEN_SIZE;
6
5
  use kobako_transport::envelope::{CompileReply, Downcall, Outcome, Preamble, Run, Snippet};
7
6
 
8
- use super::buffer::{
9
- alloc_and_write, checked_payload_len, read_buffer, unpack_guest_buffer, ReadError, WriteError,
10
- };
7
+ use super::buffer::{alloc_and_write, checked_payload_len, lift_list, ReadError, WriteError};
11
8
  use super::{GuestInstance, SANDBOX_RUNTIME_NOT_KOBAKO};
12
- use crate::error::{InvokeError, SetupError, Trap};
9
+ use crate::error::{InvokeError, Trap};
13
10
  use crate::runtime::Entry;
14
11
 
15
- /// What the guest reads from stdin: the Preamble behind its length prefix. It
16
- /// carries the same 16 MiB cap as a downcall request: past it, the prefix
17
- /// would silently wrap.
18
- pub fn stdin_content(preamble: &Preamble) -> Result<Vec<u8>, &'static str> {
19
- let preamble = preamble.encode();
20
- checked_payload_len(preamble.len())?;
21
- let mut content = Vec::with_capacity(FRAME_LEN_SIZE + preamble.len());
22
- content.extend_from_slice(&(preamble.len() as u32).to_be_bytes());
23
- content.extend_from_slice(&preamble);
24
- Ok(content)
12
+ /// The bytes the host hands the guest through its `preamble` import, held to
13
+ /// the 16 MiB cap every message carries, so an Invocation whose Preamble is
14
+ /// past it fails before anything runs.
15
+ pub fn preamble_bytes(preamble: &Preamble) -> Result<Vec<u8>, &'static str> {
16
+ let bytes = preamble.encode();
17
+ checked_payload_len(bytes.len())?;
18
+ Ok(bytes)
25
19
  }
26
20
 
27
21
  /// The downcall `entry` is.
@@ -44,22 +38,18 @@ pub fn compile_request() -> Vec<u8> {
44
38
  Downcall::Compile.encode()
45
39
  }
46
40
 
47
- /// Write a request into a guest buffer, answering where. A missing or trapping
48
- /// allocator is an engine fault, a `Trap`; one that runs but returns 0 leaves
49
- /// the runtime intact, so it is a `SetupError`.
41
+ /// Write a request into room the guest gives, answering where. A missing
42
+ /// allocator, or one that traps for want of room, is a `Trap`.
50
43
  pub fn write_request<G: GuestInstance>(
51
44
  guest: &mut G,
52
45
  envelope: &[u8],
53
46
  ) -> Result<(i32, i32), InvokeError> {
54
47
  let trap = |message: String| -> InvokeError { Trap::Other(message).into() };
55
48
  let ptr = alloc_and_write(guest, envelope).map_err(|err| match err {
56
- WriteError::OutOfMemory => {
57
- SetupError::Intact("could not allocate input buffer (out of memory)".to_string()).into()
58
- }
59
49
  WriteError::TooLarge => trap("payload exceeds the 16 MiB limit".to_string()),
60
50
  WriteError::Incompatible(message) => trap(message.to_string()),
61
- WriteError::Trapped(e) => G::cap_trap(&e)
62
- .unwrap_or_else(|| Trap::Other(format!("failed to allocate input buffer: {e}")))
51
+ WriteError::Trapped(e) => G::cap_trap_of(&e)
52
+ .unwrap_or_else(|| Trap::Other(format!("could not allocate input buffer: {e}")))
63
53
  .into(),
64
54
  WriteError::OutOfBounds => {
65
55
  trap("the input buffer lies outside the Sandbox's memory".to_string())
@@ -72,8 +62,8 @@ pub fn write_request<G: GuestInstance>(
72
62
  /// A guest that answered without an Outcome the envelope can frame — an
73
63
  /// empty answer included — has nothing left to attribute, so it is a
74
64
  /// `Trap` like every other way of failing to read the result.
75
- pub fn read_outcome<G: GuestInstance>(guest: &mut G, packed: u64) -> Result<Outcome, Trap> {
76
- let bytes = read_answer(guest, packed)?;
65
+ pub fn read_outcome<G: GuestInstance>(guest: &mut G, ret: u32) -> Result<Outcome, Trap> {
66
+ let bytes = read_answer(guest, ret)?;
77
67
  Outcome::decode(&bytes).map_err(|_| {
78
68
  Trap::Other(
79
69
  if bytes.is_empty() {
@@ -88,21 +78,18 @@ pub fn read_outcome<G: GuestInstance>(guest: &mut G, packed: u64) -> Result<Outc
88
78
 
89
79
  /// Held to the same 16 MiB bound as an Outcome; an answer that does not
90
80
  /// frame is a `Trap` the caller treats like any other failed compile.
91
- pub fn read_compile_reply<G: GuestInstance>(
92
- guest: &mut G,
93
- packed: u64,
94
- ) -> Result<CompileReply, Trap> {
95
- let bytes = read_answer(guest, packed)?;
81
+ pub fn read_compile_reply<G: GuestInstance>(guest: &mut G, ret: u32) -> Result<CompileReply, Trap> {
82
+ let bytes = read_answer(guest, ret)?;
96
83
  CompileReply::decode(&bytes)
97
84
  .map_err(|_| Trap::Other("Sandbox produced an unrecognised compile answer".to_string()))
98
85
  }
99
86
 
100
- fn read_answer<G: GuestInstance>(guest: &mut G, packed: u64) -> Result<Vec<u8>, Trap> {
87
+ /// The answer the return area at `ret` names.
88
+ fn read_answer<G: GuestInstance>(guest: &mut G, ret: u32) -> Result<Vec<u8>, Trap> {
101
89
  let memory = guest
102
90
  .memory()
103
91
  .ok_or_else(|| Trap::Other(SANDBOX_RUNTIME_NOT_KOBAKO.to_string()))?;
104
- let (ptr, len) = unpack_guest_buffer(packed);
105
- read_buffer(memory, ptr, len).map_err(|err| {
92
+ lift_list(memory, ret).map_err(|err| {
106
93
  Trap::Other(
107
94
  match err {
108
95
  ReadError::TooLarge => "result payload exceeds the 16 MiB limit",