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,24 +1,27 @@
1
1
  //! `poll_oneoff`, the guest's wait: for a clock, or for a descriptor to be
2
2
  //! ready.
3
3
  //!
4
- //! Every descriptor kobako holds is ready at once, so only a clock ever
5
- //! waits. A wait passes on the guest's own clocks: the hermetic rung's
6
- //! never advance, so its waits end at once, and a permissive wait takes
7
- //! host time but never past the invocation's deadline — the host is the
8
- //! one waiting, where no epoch check can interrupt it.
4
+ //! Each subscription becomes the pollable preview2 waits on, and preview2's
5
+ //! poll answers which are ready, as wasmtime-wasi's preview1 builds its
6
+ //! `poll_oneoff` on preview2's pollables. Every descriptor preview1 holds is
7
+ //! a file, ready at once. A clock is due as the profile has it: at once on
8
+ //! the hermetic profile's frozen clocks, and in host time on the permissive
9
+ //! profile, never past the invocation's deadline. What only preview1 has
10
+ //! stays here: which descriptor answers what, and EINTR for a wait the
11
+ //! frontend interrupts.
9
12
 
10
- use std::thread;
11
13
  use std::time::{Duration, Instant};
12
14
 
13
15
  use kobako_runtime::vfs::PollMask;
14
- use wiggle::{GuestPtr, GuestType};
16
+ use wiggle::GuestPtr;
15
17
 
16
18
  use super::errno::{Answer, Errno};
17
- use super::fd::file;
19
+ use super::fd::get_file;
18
20
  use super::memory::GuestMemory;
19
21
  use super::types::{self, Clockid, Eventrwflags, Eventtype, Subclockflags, SubscriptionU};
20
22
  use crate::invocation::Invocation;
21
- use crate::trap::TimeoutTrap;
23
+ use crate::preview2::Pollable;
24
+ use crate::trap::Interrupted;
22
25
 
23
26
  /// One subscription as the guest wrote it, and the error its event reports.
24
27
  struct Subscription {
@@ -27,6 +30,19 @@ struct Subscription {
27
30
  error: Errno,
28
31
  }
29
32
 
33
+ impl Subscription {
34
+ /// The pollable preview2 waits on for this subscription, a clock due
35
+ /// `started` plus what it has left.
36
+ fn to_pollable(&self, started: Instant) -> Pollable {
37
+ match self.kind {
38
+ Kind::Clock(left) => Pollable::Clock {
39
+ at: started + Duration::from_nanos(left),
40
+ },
41
+ Kind::FdRead | Kind::FdWrite => Pollable::Ready,
42
+ }
43
+ }
44
+ }
45
+
30
46
  enum Kind {
31
47
  /// A clock, as the nanoseconds left before it fires.
32
48
  Clock(u64),
@@ -44,38 +60,34 @@ pub(crate) fn poll_oneoff(
44
60
  if count == 0 {
45
61
  return Err(Errno::Inval.into());
46
62
  }
47
- mem.consume(count, types::Subscription::guest_size())?;
48
- mem.consume(count, types::Event::guest_size())?;
63
+ mem.consume_array::<types::Subscription>(count)?;
64
+ mem.consume_array::<types::Event>(count)?;
49
65
  let subscriptions = subs
50
66
  .as_array(count)
51
67
  .iter()
52
68
  .map(|sub| read_subscription(invocation, mem, sub?))
53
69
  .collect::<Answer<Vec<_>>>()?;
54
70
 
55
- let clocks = subscriptions.iter().filter_map(|sub| match sub.kind {
56
- Kind::Clock(left) => Some(left),
57
- Kind::FdRead | Kind::FdWrite => None,
58
- });
59
- let waited = if clocks.clone().count() == subscriptions.len() {
60
- let soonest = clocks.min().unwrap_or(0);
61
- wait(invocation, soonest)?;
62
- soonest
63
- } else {
64
- 0
71
+ let started = Instant::now();
72
+ let pollables = subscriptions
73
+ .iter()
74
+ .map(|sub| sub.to_pollable(started))
75
+ .collect();
76
+ let ready = match invocation.poll_ready(pollables) {
77
+ Ok(ready) => ready,
78
+ Err(err) if err.is::<Interrupted>() => return Err(Errno::Intr.into()),
79
+ Err(err) => return Err(types::Error::trap(err)),
65
80
  };
66
81
 
67
- let mut written = 0;
68
- for sub in &subscriptions {
69
- let (ready, nbytes) = match sub.kind {
70
- Kind::Clock(left) => (left <= waited, 0),
71
- Kind::FdRead | Kind::FdWrite => (true, u64::from(sub.error == Errno::Success)),
82
+ for (written, index) in ready.iter().enumerate() {
83
+ let sub = &subscriptions[*index as usize];
84
+ let nbytes = match sub.kind {
85
+ Kind::Clock(_) => 0,
86
+ Kind::FdRead | Kind::FdWrite => u64::from(sub.error == Errno::Success),
72
87
  };
73
- if ready {
74
- mem.write(events.add(written)?, to_event(sub, nbytes))?;
75
- written += 1;
76
- }
88
+ mem.write(events.add(written as u32)?, to_event(sub, nbytes))?;
77
89
  }
78
- Ok(written)
90
+ Ok(ready.len() as u32)
79
91
  }
80
92
 
81
93
  fn read_subscription(
@@ -84,41 +96,43 @@ fn read_subscription(
84
96
  sub: GuestPtr<types::Subscription>,
85
97
  ) -> Answer<Subscription> {
86
98
  let sub = mem.read(sub)?;
87
- let (kind, error) = match sub.u {
88
- SubscriptionU::Clock(clock) => (read_clock(invocation, &clock)?, Errno::Success),
89
- SubscriptionU::FdRead(subscribed) => (
90
- Kind::FdRead,
91
- poll_descriptor(invocation, subscribed, PollMask::IN)?,
92
- ),
93
- SubscriptionU::FdWrite(subscribed) => (
94
- Kind::FdWrite,
95
- poll_descriptor(invocation, subscribed, PollMask::OUT)?,
96
- ),
99
+ let (kind, subscribed) = match sub.u {
100
+ SubscriptionU::Clock(clock) => {
101
+ return Ok(Subscription {
102
+ userdata: sub.userdata,
103
+ kind: read_clock(invocation, &clock)?,
104
+ error: Errno::Success,
105
+ })
106
+ }
107
+ SubscriptionU::FdRead(subscribed) => (Kind::FdRead, subscribed),
108
+ SubscriptionU::FdWrite(subscribed) => (Kind::FdWrite, subscribed),
109
+ };
110
+ let mask = match kind {
111
+ Kind::FdRead => PollMask::IN,
112
+ _ => PollMask::OUT,
97
113
  };
98
114
  Ok(Subscription {
99
115
  userdata: sub.userdata,
100
116
  kind,
101
- error,
117
+ error: poll_descriptor(invocation, subscribed, mask)?,
102
118
  })
103
119
  }
104
120
 
105
- /// Hold a subscription to what its descriptor would find ready, answering the
106
- /// error its event reports. A number no descriptor holds, or one standing only
107
- /// for its path, is reported in its event, as Linux's `do_pollfd` marks it
108
- /// `POLLNVAL` when `fdget` refuses it and the wait goes on. A
109
- /// pipe's end is never ready for what the other end does, and nothing here
110
- /// would wake a wait on it, so that is `BADF` rather than a wait that never
111
- /// ends.
121
+ /// The error a subscription's event reports for what its descriptor would
122
+ /// find ready. A number no descriptor holds, or one standing only for its
123
+ /// path, is reported in its event, as Linux's `do_pollfd` marks it
124
+ /// `POLLNVAL` when `fdget` refuses it and the wait goes on. A pipe's end is
125
+ /// never ready for what the other end does, and nothing here would wake a
126
+ /// wait on it, so that is `BADF` rather than a wait that never ends.
112
127
  fn poll_descriptor(
113
128
  invocation: &Invocation,
114
129
  subscribed: types::SubscriptionFdReadwrite,
115
130
  event: PollMask,
116
131
  ) -> Answer<Errno> {
117
- let descriptors = invocation.wasi().descriptors();
118
- let Ok(file) = file(descriptors, subscribed.file_descriptor.into()) else {
132
+ let Ok(file) = get_file(invocation.wasi(), subscribed.file_descriptor.into()) else {
119
133
  return Ok(Errno::Badf);
120
134
  };
121
- match file.poll().contains(event) {
135
+ match file.poll(None).contains(event) {
122
136
  true => Ok(Errno::Success),
123
137
  false => Err(Errno::Badf.into()),
124
138
  }
@@ -141,25 +155,6 @@ fn read_clock(invocation: &Invocation, clock: &types::SubscriptionClock) -> Answ
141
155
  Ok(Kind::Clock(clock.timeout.saturating_sub(now)))
142
156
  }
143
157
 
144
- /// Wait `nanos` of the guest's time: none on frozen clocks, and on live
145
- /// ones no further than the invocation's deadline, which ends it as a
146
- /// timeout.
147
- fn wait(invocation: &Invocation, nanos: u64) -> Answer {
148
- if nanos == 0 || !invocation.wasi().clocks().advances() {
149
- return Ok(());
150
- }
151
- let wanted = Duration::from_nanos(nanos);
152
- if let Some(deadline) = invocation.deadline() {
153
- let left = deadline.saturating_duration_since(Instant::now());
154
- if wanted >= left {
155
- thread::sleep(left);
156
- return Err(types::Error::trap(wasmtime::Error::new(TimeoutTrap)));
157
- }
158
- }
159
- thread::sleep(wanted);
160
- Ok(())
161
- }
162
-
163
158
  fn to_event(sub: &Subscription, nbytes: u64) -> types::Event {
164
159
  types::Event {
165
160
  userdata: sub.userdata,
@@ -178,7 +173,7 @@ fn to_event(sub: &Subscription, nbytes: u64) -> types::Event {
178
173
 
179
174
  #[cfg(test)]
180
175
  mod tests {
181
- //! Witness that a guest's clock wait passes on its posture's clocks:
176
+ //! Witness that a guest's clock wait passes on its profile's clocks:
182
177
  //! at once on the frozen ones, and on the live ones no further than
183
178
  //! the deadline.
184
179
  use std::time::{Duration, Instant};
@@ -193,40 +188,71 @@ mod tests {
193
188
  use crate::wasi::Wasi;
194
189
  use kobako_runtime::error::Trap;
195
190
  use kobako_runtime::profile::Profile;
191
+ use kobako_runtime::sched::{run_task, Task};
196
192
 
197
- /// One relative monotonic-clock subscription of ten seconds at 0,
198
- /// its event written at 64 and the event count at 96.
193
+ /// One clock subscription at 0, its event written at 64 and the event
194
+ /// count at 96: a relative monotonic one of ten seconds, or an absolute
195
+ /// realtime one a fifth of a second past the realtime clock it reads.
199
196
  const WAIT_WAT: &str = r#"
200
197
  (module
201
198
  (import "wasi_snapshot_preview1" "poll_oneoff"
202
199
  (func $poll (param i32 i32 i32 i32) (result i32)))
200
+ (import "wasi_snapshot_preview1" "clock_time_get"
201
+ (func $clock (param i32 i64 i32) (result i32)))
203
202
  (memory (export "memory") 1)
204
203
  (func (export "wait_ten_seconds") (result i32)
205
204
  (i32.store8 (i32.const 8) (i32.const 0))
206
205
  (i32.store (i32.const 16) (i32.const 1))
207
206
  (i64.store (i32.const 24) (i64.const 10000000000))
208
- (call $poll (i32.const 0) (i32.const 64) (i32.const 1) (i32.const 96))))
207
+ (call $poll (i32.const 0) (i32.const 64) (i32.const 1) (i32.const 96)))
208
+ (func (export "wait_the_largest_duration") (result i32)
209
+ (i32.store8 (i32.const 8) (i32.const 0))
210
+ (i32.store (i32.const 16) (i32.const 1))
211
+ (i64.store (i32.const 24) (i64.const -1))
212
+ (call $poll (i32.const 0) (i32.const 64) (i32.const 1) (i32.const 96)))
213
+ (func (export "wait_until_a_fifth_past_now") (result i32)
214
+ (drop (call $clock (i32.const 0) (i64.const 1) (i32.const 128)))
215
+ (i32.store8 (i32.const 8) (i32.const 0))
216
+ (i32.store (i32.const 16) (i32.const 0))
217
+ (i64.store (i32.const 24)
218
+ (i64.add (i64.load (i32.const 128)) (i64.const 200000000)))
219
+ (i32.store16 (i32.const 40) (i32.const 1))
220
+ (call $poll (i32.const 0) (i32.const 64) (i32.const 1) (i32.const 96)))
221
+ (func (export "poll_count") (param $count i32) (result i32)
222
+ (call $poll (i32.const 0) (i32.const 0) (local.get $count) (i32.const 96)))
223
+ (func (export "events_of_two_clocks") (result i32)
224
+ (i32.store8 (i32.const 8) (i32.const 0))
225
+ (i32.store (i32.const 16) (i32.const 1))
226
+ (i64.store (i32.const 24) (i64.const 1000000000))
227
+ (i32.store8 (i32.const 56) (i32.const 0))
228
+ (i32.store (i32.const 64) (i32.const 1))
229
+ (i64.store (i32.const 72) (i64.const 2000000000))
230
+ (drop (call $poll (i32.const 0) (i32.const 160) (i32.const 2) (i32.const 240)))
231
+ (i32.load (i32.const 240))))
209
232
  "#;
210
233
 
211
- /// Run the ten-second wait at `profile` under `deadline`, answering
212
- /// how it ended and how long the host spent on it.
234
+ /// Run the ten-second wait at `profile` under `deadline`, handing the
235
+ /// running thread's Task to `interrupted` first, answering how it ended
236
+ /// and how long the host spent on it.
213
237
  fn wait_ten_seconds(
214
238
  profile: Profile,
215
239
  deadline: Option<Duration>,
240
+ interrupted: Option<fn(Task)>,
241
+ ) -> (wasmtime::Result<i32>, Duration) {
242
+ call_wait("wait_ten_seconds", profile, deadline, interrupted)
243
+ }
244
+
245
+ /// Run the wait `export` as `wait_ten_seconds` runs its own.
246
+ fn call_wait(
247
+ export: &str,
248
+ profile: Profile,
249
+ deadline: Option<Duration>,
250
+ interrupted: Option<fn(Task)>,
216
251
  ) -> (wasmtime::Result<i32>, Duration) {
217
252
  let engine = load_engine().expect("shared engine must be constructible");
218
- let config = Config {
219
- timeout: None,
220
- setup_timeout: None,
221
- memory_limit: None,
222
- stdout_limit: None,
223
- stderr_limit: None,
224
- open_file_limit: None,
225
- profile,
226
- security: Vec::new(),
227
- };
228
- let wasi = Wasi::for_invocation(&config, Vec::new(), &[]).expect("WASI context must build");
229
- let mut store = Store::new(engine, Invocation::new(None, wasi));
253
+ let config = Config::for_profile(profile);
254
+ let wasi = Wasi::for_invocation(&config, &[]).expect("WASI context must build");
255
+ let mut store = Store::new(engine, Invocation::new(wasi));
230
256
  store.set_epoch_deadline(NO_TIMEOUT_EPOCH_DELTA);
231
257
  let linker = imports::linker().expect("host imports must link");
232
258
  let module = Module::new(engine, WAIT_WAT).expect("wait module must compile");
@@ -234,20 +260,103 @@ mod tests {
234
260
  .instantiate(&mut store, &module)
235
261
  .expect("wait module must instantiate");
236
262
  let wait = instance
237
- .get_typed_func::<(), i32>(&mut store, "wait_ten_seconds")
263
+ .get_typed_func::<(), i32>(&mut store, export)
238
264
  .expect("wait export must resolve");
239
265
  let started = Instant::now();
240
- store
241
- .data_mut()
242
- .set_deadline(deadline.map(|left| started + left));
243
- let ended = wait.call(&mut store, ());
266
+ let deadline = deadline.map(|left| started + left);
267
+ store.data_mut().set_deadline(deadline);
268
+ let ended = run_task(deadline, || {
269
+ if let Some(interrupted) = interrupted {
270
+ interrupted(Task::current());
271
+ }
272
+ wait.call(&mut store, ())
273
+ });
244
274
  (ended, started.elapsed())
245
275
  }
246
276
 
277
+ // @behavior RT-337
278
+ #[test]
279
+ fn an_interrupted_wait_answers_eintr_at_once() {
280
+ let (ended, spent) = wait_ten_seconds(
281
+ Profile::Permissive,
282
+ Some(Duration::from_secs(20)),
283
+ Some(|task: Task| {
284
+ std::thread::spawn(move || {
285
+ std::thread::sleep(Duration::from_millis(50));
286
+ task.interrupt();
287
+ });
288
+ }),
289
+ );
290
+ assert!(
291
+ ended.as_ref().ok() == Some(&ERRNO_INTR) && spent < Duration::from_secs(5),
292
+ "a ten-second clock wait through a permissive guest's poll_oneoff, its thread \
293
+ interrupted, must answer EINTR at once, got {:?} after {spent:?}",
294
+ ended.map_err(|trap| trap.to_string())
295
+ );
296
+ }
297
+
298
+ const ERRNO_INTR: i32 = 27;
299
+
300
+ /// Poll `count` subscriptions at 0 in a fresh instance whose one page
301
+ /// holds few of them, answering the errno or the trap.
302
+ fn poll_count(count: i32) -> wasmtime::Result<i32> {
303
+ let engine = load_engine().expect("shared engine must be constructible");
304
+ let config = Config::for_profile(Profile::Hermetic);
305
+ let wasi = Wasi::for_invocation(&config, &[]).expect("WASI context must build");
306
+ let mut store = Store::new(engine, Invocation::new(wasi));
307
+ store.set_epoch_deadline(NO_TIMEOUT_EPOCH_DELTA);
308
+ let linker = imports::linker().expect("host imports must link");
309
+ let module = Module::new(engine, WAIT_WAT).expect("wait module must compile");
310
+ let instance = linker
311
+ .instantiate(&mut store, &module)
312
+ .expect("wait module must instantiate");
313
+ instance
314
+ .get_typed_func::<i32, i32>(&mut store, "poll_count")
315
+ .expect("poll export must resolve")
316
+ .call(&mut store, count)
317
+ }
318
+
319
+ // The memory holds neither array, so a count the fuel admits fails only
320
+ // once the host reads past the page, and one past it before.
321
+ // @behavior RT-399
322
+ #[test]
323
+ fn a_poll_spends_its_fuel_as_the_host_holds_each_subscription() {
324
+ // 128 MiB of fuel over the 32-byte subscription and 32-byte event
325
+ // the host holds for each.
326
+ let admitted = 2_097_152;
327
+ let within = poll_count(admitted).ok();
328
+ let past = poll_count(admitted + 1).ok();
329
+ assert!(
330
+ within != Some(ERRNO_NOMEM) && past == Some(ERRNO_NOMEM),
331
+ "poll_oneoff must spend its hostcall fuel by the host's size of each subscription \
332
+ and event, as wasmtime-wasi does, answering NOMEM only past 2097152 of them, got \
333
+ {within:?} and {past:?}"
334
+ );
335
+ }
336
+
337
+ const ERRNO_NOMEM: i32 = 48;
338
+
339
+ // @behavior RT-382
340
+ #[test]
341
+ fn a_wait_on_the_largest_duration_lasts_until_the_deadline() {
342
+ let (ended, spent) = call_wait(
343
+ "wait_the_largest_duration",
344
+ Profile::Permissive,
345
+ Some(Duration::from_millis(100)),
346
+ None,
347
+ );
348
+ assert!(
349
+ ended.is_err() && spent >= Duration::from_millis(100),
350
+ "a poll_oneoff on a clock of the largest duration through a permissive guest must \
351
+ wait until the deadline ends it; got {:?} after {spent:?}",
352
+ ended.map_err(|trap| trap.to_string())
353
+ );
354
+ }
355
+
247
356
  // @behavior RT-073
248
357
  #[test]
249
358
  fn a_hermetic_wait_ends_without_the_host_waiting() {
250
- let (ended, spent) = wait_ten_seconds(Profile::Hermetic, None);
359
+ let (ended, spent) = wait_ten_seconds(Profile::Hermetic, None, None);
251
360
  assert_eq!(
252
361
  ended.ok(),
253
362
  Some(0),
@@ -272,18 +381,9 @@ mod tests {
272
381
  (call $poll (i32.const 0) (i32.const 64) (i32.const 1) (i32.const 96))))
273
382
  "#;
274
383
  let engine = load_engine().expect("shared engine must be constructible");
275
- let config = Config {
276
- timeout: None,
277
- setup_timeout: None,
278
- memory_limit: None,
279
- stdout_limit: None,
280
- stderr_limit: None,
281
- open_file_limit: None,
282
- profile: Profile::Hermetic,
283
- security: Vec::new(),
284
- };
285
- let wasi = Wasi::for_invocation(&config, Vec::new(), &[]).expect("WASI context must build");
286
- let mut store = Store::new(engine, Invocation::new(None, wasi));
384
+ let config = Config::for_profile(Profile::Hermetic);
385
+ let wasi = Wasi::for_invocation(&config, &[]).expect("WASI context must build");
386
+ let mut store = Store::new(engine, Invocation::new(wasi));
287
387
  store.set_epoch_deadline(NO_TIMEOUT_EPOCH_DELTA);
288
388
  let linker = imports::linker().expect("host imports must link");
289
389
  let module = Module::new(engine, WAIT_ON_WAT).expect("wait module must compile");
@@ -323,18 +423,9 @@ mod tests {
323
423
  (call $poll (i32.const 0) (i32.const 64) (i32.const 1) (i32.const 96))))
324
424
  "#;
325
425
  let engine = load_engine().expect("shared engine must be constructible");
326
- let config = Config {
327
- timeout: None,
328
- setup_timeout: None,
329
- memory_limit: None,
330
- stdout_limit: None,
331
- stderr_limit: None,
332
- open_file_limit: None,
333
- profile: Profile::Hermetic,
334
- security: Vec::new(),
335
- };
336
- let wasi = Wasi::for_invocation(&config, Vec::new(), &[]).expect("WASI context must build");
337
- let mut store = Store::new(engine, Invocation::new(None, wasi));
426
+ let config = Config::for_profile(Profile::Hermetic);
427
+ let wasi = Wasi::for_invocation(&config, &[]).expect("WASI context must build");
428
+ let mut store = Store::new(engine, Invocation::new(wasi));
338
429
  store.set_epoch_deadline(NO_TIMEOUT_EPOCH_DELTA);
339
430
  let linker = imports::linker().expect("host imports must link");
340
431
  let module = Module::new(engine, WAIT_ON_FD_WAT).expect("wait module must compile");
@@ -362,10 +453,43 @@ mod tests {
362
453
  );
363
454
  }
364
455
 
456
+ // @behavior RT-371
457
+ #[test]
458
+ fn a_hermetic_wait_on_two_clocks_fires_both_at_once() {
459
+ let (ended, spent) = call_wait(
460
+ "events_of_two_clocks",
461
+ Profile::Hermetic,
462
+ Some(Duration::from_secs(20)),
463
+ None,
464
+ );
465
+ assert!(
466
+ matches!(ended, Ok(2)) && spent < Duration::from_secs(1),
467
+ "a hermetic poll_oneoff on a one- and a two-second clock must report both events at \
468
+ once, not {ended:?} after {spent:?}"
469
+ );
470
+ }
471
+
472
+ // @behavior RT-370
473
+ #[test]
474
+ fn a_wait_until_a_wall_time_reads_the_clock_the_guest_reads() {
475
+ let (ended, spent) = call_wait(
476
+ "wait_until_a_fifth_past_now",
477
+ Profile::Permissive,
478
+ Some(Duration::from_secs(5)),
479
+ None,
480
+ );
481
+ assert!(
482
+ matches!(ended, Ok(0)) && spent >= Duration::from_millis(150),
483
+ "an absolute realtime wait a fifth of a second past clock_time_get's realtime must \
484
+ end about then, not as {ended:?} after {spent:?}"
485
+ );
486
+ }
487
+
365
488
  // @behavior RT-074
366
489
  #[test]
367
490
  fn a_permissive_wait_ends_at_the_deadline_as_a_timeout() {
368
- let (ended, spent) = wait_ten_seconds(Profile::Permissive, Some(Duration::from_millis(50)));
491
+ let (ended, spent) =
492
+ wait_ten_seconds(Profile::Permissive, Some(Duration::from_millis(50)), None);
369
493
  let trap = ended.expect_err("a wait past the deadline must not return to the guest");
370
494
  assert!(
371
495
  matches!(to_trap(trap), Trap::Timeout(_)),
@@ -1,13 +1,15 @@
1
- //! The 4 `sock_*` calls. The table holds no socket, so every open
2
- //! descriptor answers `NOTSOCK`, as wasmtime-wasi's preview1 does, and one
3
- //! standing only for its path `BADF`, as Linux's socket calls take theirs
4
- //! through `fdget`.
1
+ //! The 4 `sock_*` calls. preview1 reaches no socket, since a socket lives in
2
+ //! preview2 and an older WASI adapts to a newer one, so every descriptor it
3
+ //! holds is a file: a file's answers `NOTSOCK`, as wasmtime-wasi's preview1
4
+ //! and Linux's `sockfd_lookup` answer, and one standing only for its path
5
+ //! `BADF`, as Linux's socket calls take theirs through `fdget`.
5
6
 
6
7
  use super::errno::{Answer, Errno};
7
- use super::fd::file;
8
- use crate::descriptors::Descriptors;
8
+ use super::fd::get_file;
9
+ use crate::wasi::Wasi;
9
10
 
10
- pub(crate) fn socket<T>(table: &Descriptors, fd: u32) -> Answer<T> {
11
- file(table, fd)?;
11
+ /// Refuse a socket call on `fd`, which never names a socket.
12
+ pub(crate) fn refuse_socket_call<T>(wasi: &Wasi, fd: u32) -> Answer<T> {
13
+ get_file(wasi, fd)?;
12
14
  Err(Errno::Notsock.into())
13
15
  }
@@ -10,7 +10,6 @@
10
10
  use std::collections::BTreeMap;
11
11
  use std::fs;
12
12
  use std::path::{Path, PathBuf};
13
- use std::sync::Arc;
14
13
  use std::time::Duration;
15
14
 
16
15
  use kobako_runtime::filesystem::Bind;
@@ -55,21 +54,12 @@ fn copy_tree(from: &Path, to: &Path) {
55
54
  fn run(wasm: &Path, root: &Path) -> Result<i32, String> {
56
55
  let engine = load_engine().map_err(|err| err.to_string())?;
57
56
  let bind = Bind::new(root).map_err(|err| err.to_string())?;
58
- let filesystem = Filesystem::new(Arc::new(bind)).map_err(|err| format!("{err:?}"))?;
57
+ let filesystem = Filesystem::new(bind).map_err(|err| format!("{err:?}"))?;
59
58
  let mount = Mount::new("/", filesystem, Access::ReadWrite).map_err(|err| err.to_string())?;
60
- let config = Config {
61
- timeout: None,
62
- setup_timeout: None,
63
- memory_limit: None,
64
- stdout_limit: None,
65
- stderr_limit: None,
66
- open_file_limit: None,
67
- profile: Profile::Permissive,
68
- security: Vec::new(),
69
- };
70
- let wasi = Wasi::for_invocation(&config, Vec::new(), &[Grant::Mount(mount)])
71
- .map_err(|err| err.to_string())?;
72
- let mut store = Store::new(engine, Invocation::new(None, wasi));
59
+ let config = Config::for_profile(Profile::Permissive);
60
+ let wasi =
61
+ Wasi::for_invocation(&config, &[Grant::Mount(mount)]).map_err(|err| err.to_string())?;
62
+ let mut store = Store::new(engine, Invocation::new(wasi));
73
63
  arm_deadline(&mut store, Some(Duration::from_secs(30)));
74
64
  let module = Module::from_file(engine, wasm).map_err(|err| err.to_string())?;
75
65
  let instance = imports::linker()