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
@@ -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;
@@ -46,9 +43,9 @@ impl Driver {
46
43
  /// Load the Guest Binary at `path` and verify its ABI version. The
47
44
  /// Engine is shared by the process and the Module cached per path;
48
45
  /// neither leaves the driver.
49
- pub fn new(path: &Path, config: Config) -> Result<Self, SetupError> {
46
+ pub fn from_file(path: &Path, config: Config) -> Result<Self, SetupError> {
50
47
  Ok(Self {
51
- instance_pre: instance_pre::load_instance_pre(path, config.setup_timeout)?,
48
+ instance_pre: instance_pre::load_instance_pre(path, config.limits.setup_timeout)?,
52
49
  config,
53
50
  })
54
51
  }
@@ -57,12 +54,9 @@ impl Driver {
57
54
  /// instantiating the guest and writing its request count against the
58
55
  /// `timeout` as the export call does.
59
56
  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
- );
57
+ let mut store = WtStore::new(load_engine()?, Invocation::new(wasi));
64
58
  store.limiter(|state: &mut Invocation| -> &mut dyn ResourceLimiter { state.limiter_mut() });
65
- trap::arm_deadline(&mut store, self.config.timeout);
59
+ trap::arm_deadline(&mut store, self.config.limits.timeout);
66
60
  store.data_mut().start_wall_clock();
67
61
  Ok(store)
68
62
  }
@@ -74,7 +68,7 @@ impl Driver {
74
68
  .instance_pre
75
69
  .instantiate(store.as_context_mut())
76
70
  .map_err(|e| {
77
- trap::cap_trap(&e).unwrap_or_else(|| {
71
+ trap::cap_trap_of(&e).unwrap_or_else(|| {
78
72
  Trap::Other(format!("failed to instantiate the Sandbox runtime: {e}"))
79
73
  })
80
74
  })?;
@@ -83,40 +77,39 @@ impl Driver {
83
77
  Ok(exports)
84
78
  }
85
79
 
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>(
80
+ /// Instantiate the guest in `store`, write `request` into it, and call
81
+ /// the downcall under the invocation's caps, answering the return area
82
+ /// that names its answer. A fault before the call starts is the outer
83
+ /// `Err`, the call's own the inner one. Disarm runs whether the call returns or traps, so
84
+ /// the `wall_time` bracket and the memory budget always close.
85
+ fn downcall(
90
86
  &self,
91
87
  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);
88
+ request: &[u8],
89
+ ) -> Result<wasmtime::Result<u32>, InvokeError> {
90
+ let exports = self.instantiate(store)?;
91
+ let downcall = exports::require(exports.downcall.as_ref())?;
92
+ let (ptr, len) = endpoint::write_request(&mut guest_of(store), request)?;
93
+ self.arm_memory_budget(store, &exports);
101
94
  // A wait for a lock the call meets ends at the same deadline the guest's
102
95
  // code does, so another Sandbox holding it cannot keep this one past it.
103
96
  let deadline = store.data().deadline();
104
97
  let result = kobako_runtime::sched::run_task(deadline, || {
105
- export.call(store.as_context_mut(), params)
98
+ downcall.call(store.as_context_mut(), (ptr, len))
106
99
  });
107
100
  disarm_caps(store);
108
- result
101
+ Ok(result)
109
102
  }
110
103
 
111
104
  /// The pre-initialized image's allocation is folded into the memory
112
- /// baseline rather than the budget, so the cap arms only once the
105
+ /// baseline rather than the budget, so the budget arms only once the
113
106
  /// image is in place.
114
- fn arm_memory_cap(&self, store: &mut WtStore<Invocation>, exports: &Exports) {
107
+ fn arm_memory_budget(&self, store: &mut WtStore<Invocation>, exports: &Exports) {
115
108
  let baseline = match exports.memory {
116
109
  Some(m) => m.data_size(store.as_context_mut()),
117
110
  None => 0,
118
111
  };
119
- store.data_mut().arm_memory_cap(baseline);
112
+ store.data_mut().arm_memory_budget(baseline);
120
113
  }
121
114
 
122
115
  /// Built the same for every `completion`: captures and usage must
@@ -126,15 +119,10 @@ impl Driver {
126
119
  let mut usage = Usage::default();
127
120
  usage.wall_time = data.wall_time().as_secs_f64();
128
121
  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
122
  Snapshot::new(
135
123
  completion,
136
- clip(data.wasi().stdout_bytes(), self.config.stdout_limit),
137
- clip(data.wasi().stderr_bytes(), self.config.stderr_limit),
124
+ Capture::new(data.wasi().stdout_bytes(), self.config.limits.stdout_limit),
125
+ Capture::new(data.wasi().stderr_bytes(), self.config.limits.stderr_limit),
138
126
  usage,
139
127
  )
140
128
  }
@@ -152,20 +140,17 @@ impl ContractRuntime for Driver {
152
140
  handler: Option<Arc<dyn DispatchHandler>>,
153
141
  grants: &[Grant],
154
142
  ) -> 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)?;
143
+ let preamble =
144
+ endpoint::preamble_bytes(preamble).map_err(|msg| Trap::Other(msg.to_string()))?;
145
+ let wasi = Wasi::for_invocation(&self.config, grants)?;
158
146
  let mut store = self.new_store(wasi)?;
147
+ store.data_mut().set_preamble(preamble);
159
148
  if let Some(handler) = handler {
160
149
  store.data_mut().bind_on_dispatch(handler);
161
150
  }
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));
151
+ let called = self.downcall(&mut store, &endpoint::request_of(entry))?;
167
152
  let completion = match called {
168
- Ok(packed) => match endpoint::read_outcome(&mut guest_of(&mut store), packed) {
153
+ Ok(ret) => match endpoint::read_outcome(&mut guest_of(&mut store), ret) {
169
154
  Ok(outcome) => Completion::Outcome(outcome),
170
155
  Err(t) => Completion::Trap(t),
171
156
  },
@@ -174,34 +159,34 @@ impl ContractRuntime for Driver {
174
159
  Ok(self.build_snapshot(&store, completion))
175
160
  }
176
161
 
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.
162
+ /// Exactly the profile `Config` requested. `Hermetic` freezes ambient time
163
+ /// and entropy; both profiles wire no ambient filesystem, environment, or network,
164
+ /// and the linker adds only the kobako ABI's `upcall` and `preamble`
165
+ /// beyond that WASI surface.
180
166
  fn profile(&self) -> Profile {
181
167
  self.config.profile
182
168
  }
183
169
 
184
- /// A Mount becomes a preopen of kobako's own descriptor table, which
185
- /// answers every preview1 call beneath it.
170
+ /// An outermost Mount becomes a preopen of kobako's own descriptor table,
171
+ /// which answers every preview1 call beneath it; the network entries become one
172
+ /// network the preview2 sockets reach.
186
173
  fn supports(&self, kind: GrantKind) -> bool {
187
- matches!(kind, GrantKind::Mount)
174
+ matches!(kind, GrantKind::Mount | GrantKind::Network)
188
175
  }
189
176
 
190
177
  /// Bounded by the same caps as an invocation, on an instance of its
191
178
  /// own that no script ever runs on, so what it answers carries
192
179
  /// nothing a script could have written.
193
180
  fn compile(&self, preamble: &Preamble) -> Option<Vec<Vec<u8>>> {
194
- let wasi = Wasi::for_compile(&self.config, endpoint::stdin_content(preamble).ok()?).ok()?;
181
+ let bytes = endpoint::preamble_bytes(preamble).ok()?;
182
+ let wasi = Wasi::for_compile(&self.config).ok()?;
195
183
  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))
184
+ store.data_mut().set_preamble(bytes);
185
+ let ret = self
186
+ .downcall(&mut store, &endpoint::compile_request())
187
+ .ok()?
203
188
  .ok()?;
204
- let reply = endpoint::read_compile_reply(&mut guest_of(&mut store), packed).ok()?;
189
+ let reply = endpoint::read_compile_reply(&mut guest_of(&mut store), ret).ok()?;
205
190
  endpoint::compiled_bytecodes(preamble, reply)
206
191
  }
207
192
  }
@@ -213,5 +198,5 @@ fn guest_of(store: &mut WtStore<Invocation>) -> Guest<'_> {
213
198
 
214
199
  fn disarm_caps(store: &mut WtStore<Invocation>) {
215
200
  store.data_mut().stop_wall_clock();
216
- store.data_mut().disarm_memory_cap();
201
+ store.data_mut().disarm_memory_budget();
217
202
  }
@@ -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)?;