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
@@ -4,15 +4,12 @@
4
4
  //! A `Driver` is the engine half of a kobako host — the pre-linked
5
5
  //! `InstancePre` plus the per-Driver caps — and implements the contract
6
6
  //! `Runtime` trait over it. It is free of any frontend type; a frontend
7
- //! shell (the Ruby ext's `Kobako::Runtime`) only shuttles its
8
- //! host-language values across the contract boundary.
7
+ //! only shuttles its host-language values across the contract boundary.
9
8
 
10
9
  use std::path::Path;
11
10
  use std::sync::Arc;
12
11
 
13
- use wasmtime::{
14
- AsContextMut, InstancePre as WtInstancePre, ResourceLimiter, Store as WtStore, TypedFunc,
15
- };
12
+ use wasmtime::{AsContextMut, InstancePre as WtInstancePre, ResourceLimiter, Store as WtStore};
16
13
 
17
14
  use crate::cache::load_engine;
18
15
  use crate::config::Config;
@@ -20,7 +17,7 @@ use crate::exports::{self, Exports};
20
17
  use crate::guest::Guest;
21
18
  use crate::invocation::Invocation;
22
19
  use crate::wasi::Wasi;
23
- use crate::{instance_pre, trap};
20
+ use crate::{instance_pre, limiter, trap};
24
21
  use kobako_runtime::dispatch::DispatchHandler;
25
22
  use kobako_runtime::endpoint;
26
23
  use kobako_runtime::error::{InvokeError, SetupError, Trap};
@@ -30,6 +27,9 @@ use kobako_runtime::runtime::{Entry, Runtime as ContractRuntime};
30
27
  use kobako_runtime::snapshot::{Capture, Completion, Snapshot, Usage};
31
28
  use kobako_transport::envelope::Preamble;
32
29
 
30
+ /// The GC heap a Store makes before its memory budget opens.
31
+ const GC_HEAP_PAGE: u64 = 64 * 1024;
32
+
33
33
  /// The wasmtime execution unit behind one sandbox runtime.
34
34
  pub struct Driver {
35
35
  // Pre-linked instantiation template (import wiring, type checks,
@@ -46,23 +46,32 @@ impl Driver {
46
46
  /// Load the Guest Binary at `path` and verify its ABI version. The
47
47
  /// Engine is shared by the process and the Module cached per path;
48
48
  /// neither leaves the driver.
49
- pub fn new(path: &Path, config: Config) -> Result<Self, SetupError> {
49
+ pub fn from_file(path: &Path, config: Config) -> Result<Self, SetupError> {
50
50
  Ok(Self {
51
- instance_pre: instance_pre::load_instance_pre(path, config.setup_timeout)?,
51
+ instance_pre: instance_pre::load_instance_pre(path, config.limits.setup_timeout)?,
52
52
  config,
53
53
  })
54
54
  }
55
55
 
56
- /// The deadline and the `wall_time` bracket open with the Store, so
57
- /// instantiating the guest and writing its request count against the
58
- /// `timeout` as the export call does.
59
- fn new_store(&self, wasi: Wasi) -> Result<WtStore<Invocation>, SetupError> {
60
- let mut store = WtStore::new(
61
- load_engine()?,
62
- Invocation::new(self.config.memory_limit, wasi),
63
- );
56
+ /// The deadline, the `wall_time` bracket, and the memory budget open with
57
+ /// the Store, so instantiating the guest and writing its request count
58
+ /// against the caps as the export call does. The GC heap's first page is
59
+ /// made before the budget opens, a fixed cost like what the Guest Binary
60
+ /// declares, so a run that filled its budget can still raise and rescue.
61
+ /// A host with no thread or memory for it stops the run before the guest
62
+ /// starts, as instantiating does.
63
+ fn new_store(&self, wasi: Wasi) -> Result<WtStore<Invocation>, Trap> {
64
+ let engine = load_engine().map_err(|err| Trap::Other(err.to_string()))?;
65
+ let mut store = Invocation::new(wasi)
66
+ .into_store(engine)
67
+ .map_err(|err| Trap::Other(format!("failed to create the Sandbox's store: {err}")))?;
64
68
  store.limiter(|state: &mut Invocation| -> &mut dyn ResourceLimiter { state.limiter_mut() });
65
- trap::arm_deadline(&mut store, self.config.timeout);
69
+ store
70
+ .gc_heap_grow(GC_HEAP_PAGE)
71
+ .map_err(|err| Trap::Other(format!("failed to create the Sandbox's GC heap: {err}")))?;
72
+ let declared = limiter::declared(self.instance_pre.module());
73
+ store.data_mut().arm_memory_budget(declared);
74
+ trap::arm_deadline(&mut store, self.config.limits.timeout);
66
75
  store.data_mut().start_wall_clock();
67
76
  Ok(store)
68
77
  }
@@ -74,49 +83,44 @@ impl Driver {
74
83
  .instance_pre
75
84
  .instantiate(store.as_context_mut())
76
85
  .map_err(|e| {
77
- trap::cap_trap(&e).unwrap_or_else(|| {
86
+ let failed = trap::cap_trap_of(&e).unwrap_or_else(|| {
78
87
  Trap::Other(format!("failed to instantiate the Sandbox runtime: {e}"))
79
- })
88
+ });
89
+ trap::charged_to_budget(failed, store.data_mut().limiter_mut())
80
90
  })?;
81
91
  let exports = Exports::resolve(&instance, store.as_context_mut());
82
92
  store.data_mut().set_exports(exports.clone());
83
93
  Ok(exports)
84
94
  }
85
95
 
86
- /// Disarm runs whether the call returns or traps, so the `wall_time`
87
- /// bracket and the memory cap always close; that guarantee is why the
88
- /// bracket lives in one place rather than at each call site.
89
- fn call_with_caps<Params, Results>(
96
+ /// Instantiate the guest in `store`, write `request` into it, and call
97
+ /// the downcall under the invocation's caps, answering the return area
98
+ /// that names its answer. A fault before the call starts is the outer
99
+ /// `Err`, the call's own the inner one. Disarm runs whether the call returns or traps, so
100
+ /// the `wall_time` bracket and the memory budget always close.
101
+ fn downcall(
90
102
  &self,
91
103
  store: &mut WtStore<Invocation>,
92
- exports: &Exports,
93
- export: &TypedFunc<Params, Results>,
94
- params: Params,
95
- ) -> Result<Results, wasmtime::Error>
96
- where
97
- Params: wasmtime::WasmParams,
98
- Results: wasmtime::WasmResults,
99
- {
100
- self.arm_memory_cap(store, exports);
104
+ request: &[u8],
105
+ ) -> Result<wasmtime::Result<u32>, InvokeError> {
106
+ let exports = self.instantiate(store)?;
107
+ let downcall = exports::require(exports.downcall.as_ref())?;
108
+ let (ptr, len) =
109
+ endpoint::write_request(&mut guest_of(store), request).map_err(|err| match err {
110
+ InvokeError::Trap(failed) => InvokeError::Trap(trap::charged_to_budget(
111
+ failed,
112
+ store.data_mut().limiter_mut(),
113
+ )),
114
+ other => other,
115
+ })?;
101
116
  // A wait for a lock the call meets ends at the same deadline the guest's
102
117
  // code does, so another Sandbox holding it cannot keep this one past it.
103
118
  let deadline = store.data().deadline();
104
119
  let result = kobako_runtime::sched::run_task(deadline, || {
105
- export.call(store.as_context_mut(), params)
120
+ downcall.call(store.as_context_mut(), (ptr, len))
106
121
  });
107
122
  disarm_caps(store);
108
- result
109
- }
110
-
111
- /// The pre-initialized image's allocation is folded into the memory
112
- /// baseline rather than the budget, so the cap arms only once the
113
- /// image is in place.
114
- fn arm_memory_cap(&self, store: &mut WtStore<Invocation>, exports: &Exports) {
115
- let baseline = match exports.memory {
116
- Some(m) => m.data_size(store.as_context_mut()),
117
- None => 0,
118
- };
119
- store.data_mut().arm_memory_cap(baseline);
123
+ Ok(result)
120
124
  }
121
125
 
122
126
  /// Built the same for every `completion`: captures and usage must
@@ -126,15 +130,10 @@ impl Driver {
126
130
  let mut usage = Usage::default();
127
131
  usage.wall_time = data.wall_time().as_secs_f64();
128
132
  usage.memory_peak = data.memory_peak();
129
- let clip = |bytes, limit| {
130
- let mut capture = Capture::default();
131
- (capture.bytes, capture.truncated) = endpoint::clip_capture(bytes, limit);
132
- capture
133
- };
134
133
  Snapshot::new(
135
134
  completion,
136
- clip(data.wasi().stdout_bytes(), self.config.stdout_limit),
137
- clip(data.wasi().stderr_bytes(), self.config.stderr_limit),
135
+ Capture::new(data.wasi().stdout_bytes(), self.config.limits.stdout_limit),
136
+ Capture::new(data.wasi().stderr_bytes(), self.config.limits.stderr_limit),
138
137
  usage,
139
138
  )
140
139
  }
@@ -152,56 +151,55 @@ impl ContractRuntime for Driver {
152
151
  handler: Option<Arc<dyn DispatchHandler>>,
153
152
  grants: &[Grant],
154
153
  ) -> Result<Snapshot, InvokeError> {
155
- let stdin =
156
- endpoint::stdin_content(preamble).map_err(|msg| Trap::Other(msg.to_string()))?;
157
- let wasi = Wasi::for_invocation(&self.config, stdin, grants)?;
154
+ let preamble =
155
+ endpoint::preamble_bytes(preamble).map_err(|msg| Trap::Other(msg.to_string()))?;
156
+ let wasi = Wasi::for_invocation(&self.config, grants)?;
158
157
  let mut store = self.new_store(wasi)?;
158
+ store.data_mut().set_preamble(preamble);
159
159
  if let Some(handler) = handler {
160
160
  store.data_mut().bind_on_dispatch(handler);
161
161
  }
162
- let exports = self.instantiate(&mut store)?;
163
- let downcall = exports::require(exports.downcall.as_ref())?;
164
- let request = endpoint::request_of(entry);
165
- let (ptr, len) = endpoint::write_request(&mut guest_of(&mut store), &request)?;
166
- let called = self.call_with_caps(&mut store, &exports, downcall, (ptr, len));
167
- let completion = match called {
168
- Ok(packed) => match endpoint::read_outcome(&mut guest_of(&mut store), packed) {
169
- Ok(outcome) => Completion::Outcome(outcome),
170
- Err(t) => Completion::Trap(t),
171
- },
172
- Err(e) => Completion::Trap(trap::to_trap(e)),
162
+ let called = self.downcall(&mut store, &endpoint::request_of(entry))?;
163
+ let read = match called {
164
+ Ok(ret) => endpoint::read_outcome(&mut guest_of(&mut store), ret),
165
+ Err(e) => Err(trap::to_trap(e)),
166
+ };
167
+ let limiter = store.data_mut().limiter_mut();
168
+ let completion = match read {
169
+ Ok(outcome) => trap::completion_of(outcome, limiter),
170
+ Err(failed) => Completion::Trap(trap::charged_to_budget(failed, limiter)),
173
171
  };
174
172
  Ok(self.build_snapshot(&store, completion))
175
173
  }
176
174
 
177
- /// Exactly the rung `Config` requested. `Hermetic` freezes ambient time
178
- /// and entropy; both rungs wire no ambient filesystem, environment, or network,
179
- /// and the linker adds only `__kobako_upcall` beyond that WASI surface.
175
+ /// Exactly the profile `Config` requested. `Hermetic` freezes ambient time
176
+ /// and entropy; both profiles wire no ambient filesystem, environment, or network,
177
+ /// and the linker adds only the kobako ABI's `upcall` and `preamble`
178
+ /// beyond that WASI surface.
180
179
  fn profile(&self) -> Profile {
181
180
  self.config.profile
182
181
  }
183
182
 
184
- /// A Mount becomes a preopen of kobako's own descriptor table, which
185
- /// answers every preview1 call beneath it.
183
+ /// An outermost Mount becomes a preopen of kobako's own descriptor table,
184
+ /// which answers every preview1 call beneath it; the network entries become one
185
+ /// network the preview2 sockets reach.
186
186
  fn supports(&self, kind: GrantKind) -> bool {
187
- matches!(kind, GrantKind::Mount)
187
+ matches!(kind, GrantKind::Mount | GrantKind::Network)
188
188
  }
189
189
 
190
190
  /// Bounded by the same caps as an invocation, on an instance of its
191
191
  /// own that no script ever runs on, so what it answers carries
192
192
  /// nothing a script could have written.
193
193
  fn compile(&self, preamble: &Preamble) -> Option<Vec<Vec<u8>>> {
194
- let wasi = Wasi::for_compile(&self.config, endpoint::stdin_content(preamble).ok()?).ok()?;
194
+ let bytes = endpoint::preamble_bytes(preamble).ok()?;
195
+ let wasi = Wasi::for_compile(&self.config).ok()?;
195
196
  let mut store = self.new_store(wasi).ok()?;
196
- let exports = self.instantiate(&mut store).ok()?;
197
- let downcall = exports::require(exports.downcall.as_ref()).ok()?;
198
- let (ptr, len) =
199
- endpoint::write_request(&mut guest_of(&mut store), &endpoint::compile_request())
200
- .ok()?;
201
- let packed = self
202
- .call_with_caps(&mut store, &exports, downcall, (ptr, len))
197
+ store.data_mut().set_preamble(bytes);
198
+ let ret = self
199
+ .downcall(&mut store, &endpoint::compile_request())
200
+ .ok()?
203
201
  .ok()?;
204
- let reply = endpoint::read_compile_reply(&mut guest_of(&mut store), packed).ok()?;
202
+ let reply = endpoint::read_compile_reply(&mut guest_of(&mut store), ret).ok()?;
205
203
  endpoint::compiled_bytecodes(preamble, reply)
206
204
  }
207
205
  }
@@ -213,5 +211,5 @@ fn guest_of(store: &mut WtStore<Invocation>) -> Guest<'_> {
213
211
 
214
212
  fn disarm_caps(store: &mut WtStore<Invocation>) {
215
213
  store.data_mut().stop_wall_clock();
216
- store.data_mut().disarm_memory_cap();
214
+ store.data_mut().disarm_memory_budget();
217
215
  }
@@ -12,6 +12,7 @@ use wasmtime::{AsContextMut, Instance as WtInstance, Memory, TypedFunc};
12
12
 
13
13
  use kobako_runtime::endpoint::RUNTIME_INCOMPATIBLE;
14
14
  use kobako_runtime::error::Trap;
15
+ use kobako_transport::abi::DOWNCALL_EXPORT;
15
16
 
16
17
  /// The resolved host-driven export handles. Each is `Option` because test
17
18
  /// fixtures (a minimal "ping" module) need not provide them; real
@@ -24,8 +25,8 @@ use kobako_runtime::error::Trap;
24
25
  /// own data for the host imports to reach.
25
26
  #[derive(Clone)]
26
27
  pub(crate) struct Exports {
27
- pub(crate) downcall: Option<TypedFunc<(i32, i32), u64>>,
28
- pub(crate) alloc: Option<TypedFunc<u32, u32>>,
28
+ pub(crate) downcall: Option<TypedFunc<(i32, i32), u32>>,
29
+ pub(crate) realloc: Option<TypedFunc<(u32, u32, u32, u32), u32>>,
29
30
  pub(crate) memory: Option<Memory>,
30
31
  }
31
32
 
@@ -35,10 +36,10 @@ impl Exports {
35
36
  pub(crate) fn resolve(instance: &WtInstance, mut ctx: impl AsContextMut) -> Self {
36
37
  Self {
37
38
  downcall: instance
38
- .get_typed_func::<(i32, i32), u64>(&mut ctx, "__kobako_downcall")
39
+ .get_typed_func::<(i32, i32), u32>(&mut ctx, DOWNCALL_EXPORT)
39
40
  .ok(),
40
- alloc: instance
41
- .get_typed_func::<u32, u32>(&mut ctx, "__kobako_alloc")
41
+ realloc: instance
42
+ .get_typed_func::<(u32, u32, u32, u32), u32>(&mut ctx, "cabi_realloc")
42
43
  .ok(),
43
44
  memory: instance.get_memory(&mut ctx, "memory"),
44
45
  }
@@ -64,27 +65,27 @@ mod tests {
64
65
  //! CI (which always builds it) and a silent skip locally.
65
66
  use std::path::Path;
66
67
 
67
- use wasmtime::ExternType;
68
+ use kobako_runtime::profile::Profile;
69
+ use kobako_transport::abi::DOWNCALL_EXPORT;
70
+ use wasmtime::{ExternType, Store};
68
71
 
69
- use crate::cache::load_module;
72
+ use crate::cache::{load_engine, load_module};
73
+ use crate::config::Config;
74
+ use crate::guest_binary;
75
+ use crate::imports;
76
+ use crate::invocation::Invocation;
77
+ use crate::trap::NO_TIMEOUT_EPOCH_DELTA;
78
+ use crate::wasi::Wasi;
70
79
 
71
80
  const WASM: &str = concat!(env!("CARGO_MANIFEST_DIR"), "/../../data/kobako.wasm");
72
81
 
73
- const ABI_EXPORTS: [&str; 3] = [
74
- "__kobako_abi_version",
75
- "__kobako_alloc",
76
- "__kobako_downcall",
77
- ];
82
+ const ABI_EXPORTS: [&str; 2] = ["cabi_realloc", DOWNCALL_EXPORT];
78
83
 
79
84
  // @behavior RT-068
80
85
  #[test]
81
86
  fn the_guest_binary_exports_the_abi_functions_and_nothing_else() {
82
87
  let path = Path::new(WASM);
83
- if !path.exists() {
84
- assert!(
85
- std::env::var_os("CI").is_none(),
86
- "data/kobako.wasm missing under CI — run `bundle exec rake wasm:build`"
87
- );
88
+ if !guest_binary::built(path.exists(), "data/kobako.wasm") {
88
89
  return;
89
90
  }
90
91
  let module = load_module(path).expect("the Guest Binary must load");
@@ -101,4 +102,129 @@ mod tests {
101
102
  "data/kobako.wasm read through its export section must export exactly the ABI's functions"
102
103
  );
103
104
  }
105
+
106
+ // @behavior RT-375
107
+ #[test]
108
+ fn the_guest_binary_hands_an_empty_list_no_room_and_traps_with_none_left() {
109
+ let path = Path::new(WASM);
110
+ if !guest_binary::built(path.exists(), "data/kobako.wasm") {
111
+ return;
112
+ }
113
+ let module = load_module(path).expect("the Guest Binary must load");
114
+ let config = Config::for_profile(Profile::Hermetic);
115
+ let wasi = Wasi::for_invocation(&config, &[]).expect("WASI context must build");
116
+ let mut store = Store::new(
117
+ load_engine().expect("shared engine must be constructible"),
118
+ Invocation::new(wasi),
119
+ );
120
+ store.set_epoch_deadline(NO_TIMEOUT_EPOCH_DELTA);
121
+ let instance = imports::linker()
122
+ .expect("host imports must link")
123
+ .instantiate(&mut store, &module)
124
+ .expect("the Guest Binary must instantiate");
125
+ let realloc = instance
126
+ .get_typed_func::<(i32, i32, i32, i32), i32>(&mut store, "cabi_realloc")
127
+ .expect("the Guest Binary exports cabi_realloc");
128
+ let empty = [
129
+ realloc.call(&mut store, (0, 0, 4, 0)).ok(),
130
+ realloc.call(&mut store, (0, 0, 4, 0)).ok(),
131
+ ];
132
+ let past = realloc.call(&mut store, (0, 0, 1, i32::MAX)).is_err();
133
+ assert_eq!(
134
+ (empty, past),
135
+ ([Some(4), Some(4)], true),
136
+ "cabi_realloc asked twice for an empty list on 4 must answer 4 and take no room, and \
137
+ asked for more room than the Guest Binary has must trap rather than answer 0"
138
+ );
139
+ }
140
+
141
+ /// Where a function's calls can lead: the imports it reaches, and whether
142
+ /// it reaches a call through the function table, which could be any.
143
+ #[derive(Debug, Default, PartialEq)]
144
+ struct Reach {
145
+ imports: Vec<String>,
146
+ indirect: bool,
147
+ }
148
+
149
+ /// What the export `name` of the module in `bytes` can reach through its
150
+ /// direct calls.
151
+ fn reach_of(bytes: &[u8], name: &str) -> Reach {
152
+ use wasmparser::{ExternalKind, Operator, Parser, Payload, TypeRef};
153
+
154
+ let (mut imports, mut start) = (Vec::new(), None);
155
+ let mut bodies: Vec<(Vec<u32>, bool)> = Vec::new();
156
+ for payload in Parser::new(0).parse_all(bytes) {
157
+ match payload.expect("the Guest Binary parses") {
158
+ Payload::ImportSection(section) => {
159
+ for import in section.into_imports() {
160
+ let import = import.expect("an import parses");
161
+ if matches!(import.ty, TypeRef::Func(_)) {
162
+ imports.push(format!("{}.{}", import.module, import.name));
163
+ }
164
+ }
165
+ }
166
+ Payload::ExportSection(section) => {
167
+ for export in section {
168
+ let export = export.expect("an export parses");
169
+ if export.name == name && export.kind == ExternalKind::Func {
170
+ start = Some(export.index);
171
+ }
172
+ }
173
+ }
174
+ Payload::CodeSectionEntry(body) => {
175
+ let (mut calls, mut indirect) = (Vec::new(), false);
176
+ let mut reader = body.get_operators_reader().expect("a body parses");
177
+ while !reader.eof() {
178
+ match reader.read().expect("an operator parses") {
179
+ Operator::Call { function_index }
180
+ | Operator::ReturnCall { function_index } => calls.push(function_index),
181
+ Operator::CallIndirect { .. }
182
+ | Operator::ReturnCallIndirect { .. }
183
+ | Operator::CallRef { .. }
184
+ | Operator::ReturnCallRef { .. } => indirect = true,
185
+ _ => {}
186
+ }
187
+ }
188
+ bodies.push((calls, indirect));
189
+ }
190
+ _ => {}
191
+ }
192
+ }
193
+
194
+ let mut reach = Reach::default();
195
+ let mut seen = std::collections::HashSet::new();
196
+ let mut pending = vec![start.unwrap_or_else(|| panic!("the module exports {name}"))];
197
+ while let Some(index) = pending.pop() {
198
+ if !seen.insert(index) {
199
+ continue;
200
+ }
201
+ match imports.get(index as usize) {
202
+ Some(import) => reach.imports.push(import.clone()),
203
+ None => {
204
+ let (calls, indirect) = &bodies[index as usize - imports.len()];
205
+ reach.indirect |= indirect;
206
+ pending.extend(calls);
207
+ }
208
+ }
209
+ }
210
+ reach.imports.sort_unstable();
211
+ reach
212
+ }
213
+
214
+ // @behavior RT-397
215
+ #[test]
216
+ fn the_guest_binarys_allocator_calls_into_no_host_import() {
217
+ let path = Path::new(WASM);
218
+ if !guest_binary::built(path.exists(), "data/kobako.wasm") {
219
+ return;
220
+ }
221
+ let bytes = std::fs::read(path).expect("the Guest Binary must read");
222
+
223
+ assert_eq!(
224
+ reach_of(&bytes, "cabi_realloc"),
225
+ Reach::default(),
226
+ "cabi_realloc in data/kobako.wasm, followed through its calls, must reach no host \
227
+ import and no call through the function table"
228
+ );
229
+ }
104
230
  }
@@ -22,17 +22,23 @@ impl GuestInstance for Guest<'_> {
22
22
  Some(memory.data_mut(&mut self.0))
23
23
  }
24
24
 
25
- fn alloc(&mut self, len: u32) -> Option<wasmtime::Result<u32>> {
26
- let alloc = self.0.data().exports()?.alloc?;
27
- Some(alloc.call(&mut self.0, len))
25
+ fn realloc(
26
+ &mut self,
27
+ old_ptr: u32,
28
+ old_len: u32,
29
+ align: u32,
30
+ new_len: u32,
31
+ ) -> Option<wasmtime::Result<u32>> {
32
+ let realloc = self.0.data().exports()?.realloc?;
33
+ Some(realloc.call(&mut self.0, (old_ptr, old_len, align, new_len)))
28
34
  }
29
35
 
30
- fn downcall(&mut self, ptr: i32, len: i32) -> Option<wasmtime::Result<u64>> {
36
+ fn downcall(&mut self, ptr: i32, len: i32) -> Option<wasmtime::Result<u32>> {
31
37
  let downcall = self.0.data().exports()?.downcall?;
32
38
  Some(downcall.call(&mut self.0, (ptr, len)))
33
39
  }
34
40
 
35
- fn cap_trap(fault: &wasmtime::Error) -> Option<Trap> {
36
- trap::cap_trap(fault)
41
+ fn cap_trap_of(fault: &wasmtime::Error) -> Option<Trap> {
42
+ trap::cap_trap_of(fault)
37
43
  }
38
44
  }
@@ -0,0 +1,14 @@
1
+ //! Whether a test reading the built Guest Binaries may go on. The unit tests
2
+ //! here and the integration tests of this crate and the SDK each compile
3
+ //! apart, so each reads this one file.
4
+
5
+ /// Whether a test reading the Guest Binaries at `what` may go on, `present`
6
+ /// saying they were built: one missing skips the test locally and fails it
7
+ /// under CI, which always builds them.
8
+ pub(crate) fn built(present: bool, what: &str) -> bool {
9
+ assert!(
10
+ present || std::env::var_os("CI").is_none(),
11
+ "{what} missing under CI — run `bundle exec rake wasm:build`"
12
+ );
13
+ present
14
+ }
@@ -1,9 +1,10 @@
1
- //! The host imports a Guest Binary resolves: the WASI preview1 set and
2
- //! the wire ABI's `__kobako_upcall`.
1
+ //! The host imports a Guest Binary resolves: the WASI preview1 set, the
2
+ //! WASI 0.2 functions a core module links, and the kobako ABI's `upcall`
3
+ //! and `preamble`.
3
4
  //!
4
- //! Both closures read all their state from the `Invocation` inside the
5
- //! calling Store, never from a Runtime, so the wiring is the same for
6
- //! every Sandbox and `crate::instance_pre` can cache what it resolves.
5
+ //! Every import reads its state from the `Invocation` inside the calling
6
+ //! Store, never from a Runtime, so the wiring is the same for every Sandbox
7
+ //! and `crate::instance_pre` can cache what it resolves.
7
8
 
8
9
  use wasmtime::{AsContextMut, Caller, Linker};
9
10
  use wasmtime_wasi::p1;
@@ -12,8 +13,10 @@ use crate::cache::load_engine;
12
13
  use crate::guest::Guest;
13
14
  use crate::invocation::Invocation;
14
15
  use crate::preview1;
15
- use kobako_runtime::endpoint::{self, UpcallEnd, ABANDONED};
16
+ use crate::preview2;
17
+ use kobako_runtime::endpoint::{self, ImportEnd, ABANDONED};
16
18
  use kobako_runtime::error::SetupError;
19
+ use kobako_transport::abi::{HOST_INTERFACE, PREAMBLE, UPCALL};
17
20
 
18
21
  pub(crate) fn linker() -> Result<Linker<Invocation>, SetupError> {
19
22
  let mut linker: Linker<Invocation> = Linker::new(load_engine()?);
@@ -26,27 +29,48 @@ pub(crate) fn linker() -> Result<Linker<Invocation>, SetupError> {
26
29
  state.wasi_mut().ctx_mut()
27
30
  })
28
31
  .and_then(|_| preview1::shadow(&mut linker))
32
+ .and_then(|_| preview2::add_to_linker(&mut linker))
29
33
  .map_err(|e| SetupError::Dead(format!("failed to set up the WASI runtime: {e}")))?;
30
34
 
31
- // `__kobako_upcall`, `(ptr: i32, len: i32) -> i64` in the ABI;
32
- // `endpoint::handle` owns what it answers.
35
+ // `upcall`, `(ptr: i32, len: i32, ret: i32)` in the ABI, and `preamble`,
36
+ // `(ret: i32)`; the endpoint owns what each answers.
37
+ let bridge = |e: wasmtime::Error| {
38
+ SetupError::Dead(format!("failed to set up the host callback bridge: {e}"))
39
+ };
33
40
  linker
34
41
  .func_wrap(
35
- "env",
36
- "__kobako_upcall",
42
+ HOST_INTERFACE,
43
+ UPCALL,
37
44
  |mut caller: Caller<'_, Invocation>,
38
45
  req_ptr: i32,
39
- req_len: i32|
40
- -> wasmtime::Result<i64> {
46
+ req_len: i32,
47
+ ret: i32|
48
+ -> wasmtime::Result<()> {
41
49
  let mut guest = Guest(caller.as_context_mut());
42
- endpoint::handle(&mut guest, req_ptr, req_len).map_err(|end| match end {
43
- UpcallEnd::Fault(trap) => trap,
44
- UpcallEnd::Abandoned => wasmtime::Error::msg(ABANDONED),
45
- UpcallEnd::Unwritable(reason) => wasmtime::Error::msg(reason),
46
- })
50
+ endpoint::handle(&mut guest, req_ptr, req_len, ret).map_err(to_import_error)
47
51
  },
48
52
  )
49
- .map_err(|e| SetupError::Dead(format!("failed to set up the host callback bridge: {e}")))?;
53
+ .map_err(bridge)?;
54
+ linker
55
+ .func_wrap(
56
+ HOST_INTERFACE,
57
+ PREAMBLE,
58
+ |mut caller: Caller<'_, Invocation>, ret: i32| -> wasmtime::Result<()> {
59
+ let preamble = caller.data().preamble();
60
+ let mut guest = Guest(caller.as_context_mut());
61
+ endpoint::hand_preamble(&mut guest, &preamble, ret).map_err(to_import_error)
62
+ },
63
+ )
64
+ .map_err(bridge)?;
50
65
 
51
66
  Ok(linker)
52
67
  }
68
+
69
+ /// The trap a host import ends the invocation with instead of answering.
70
+ fn to_import_error(end: ImportEnd<wasmtime::Error>) -> wasmtime::Error {
71
+ match end {
72
+ ImportEnd::Fault(trap) => trap,
73
+ ImportEnd::Abandoned => wasmtime::Error::msg(ABANDONED),
74
+ ImportEnd::Unwritable(reason) => wasmtime::Error::msg(reason),
75
+ }
76
+ }
@@ -1,10 +1,10 @@
1
1
  //! Per-path cache of pre-instantiated wasmtime artifacts.
2
2
  //!
3
3
  //! The host imports (`crate::imports`) and their type-check against the
4
- //! compiled Module are identical for every `Kobako::Runtime` on the same
5
- //! Guest Binary. Caching the resolved `InstancePre` per path — together
4
+ //! compiled Module are identical for every `Driver` on the same Guest
5
+ //! Binary. Caching the resolved `InstancePre` per path — together
6
6
  //! with the ABI version check it guards (`crate::abi`) — takes both off
7
- //! the `Driver::new` hot path.
7
+ //! the `Driver::from_file` hot path.
8
8
  //!
9
9
  //! Concurrency: the same as `crate::cache` — the Mutex covers lookup and
10
10
  //! insert only, and a concurrent miss builds an equivalent `InstancePre`.
@@ -45,7 +45,7 @@ pub(crate) fn load_instance_pre(
45
45
  let linker = imports::linker()?;
46
46
  let pre = linker
47
47
  .instantiate_pre(&module)
48
- .map_err(trap::instantiate_err)?;
48
+ .map_err(trap::to_setup_error)?;
49
49
  // Verify before the insert, so a rejected artifact is never
50
50
  // remembered as a usable template.
51
51
  abi::verify(&pre, setup_timeout)?;