kobako 0.29.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 (291) hide show
  1. checksums.yaml +4 -4
  2. data/.release-please-manifest.json +1 -1
  3. data/CHANGELOG.md +552 -10
  4. data/Cargo.lock +595 -139
  5. data/README.md +126 -564
  6. data/ROADMAP.md +3 -2
  7. data/crates/kobako-runtime/CHANGELOG.md +488 -1
  8. data/crates/kobako-runtime/Cargo.toml +73 -15
  9. data/crates/kobako-runtime/README.md +11 -4
  10. data/crates/kobako-runtime/src/cred.rs +124 -0
  11. data/crates/kobako-runtime/src/dispatch.rs +5 -6
  12. data/crates/kobako-runtime/src/endpoint/buffer.rs +301 -0
  13. data/crates/kobako-runtime/src/endpoint/dispatch.rs +292 -0
  14. data/crates/kobako-runtime/src/endpoint/invoke.rs +181 -0
  15. data/crates/kobako-runtime/src/endpoint.rs +267 -0
  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/fid.rs +82 -0
  20. data/crates/kobako-runtime/src/filesystem/bind/lookup.rs +274 -0
  21. data/crates/kobako-runtime/src/filesystem/bind/walk.rs +248 -0
  22. data/crates/kobako-runtime/src/filesystem/bind.rs +2857 -0
  23. data/crates/kobako-runtime/src/filesystem/overlay/file.rs +89 -0
  24. data/crates/kobako-runtime/src/filesystem/overlay/inode.rs +106 -0
  25. data/crates/kobako-runtime/src/filesystem/overlay/path.rs +263 -0
  26. data/crates/kobako-runtime/src/filesystem/overlay/stack/copy_up.rs +128 -0
  27. data/crates/kobako-runtime/src/filesystem/overlay/stack/dir.rs +238 -0
  28. data/crates/kobako-runtime/src/filesystem/overlay/stack/namei.rs +135 -0
  29. data/crates/kobako-runtime/src/filesystem/overlay/stack/readdir.rs +54 -0
  30. data/crates/kobako-runtime/src/filesystem/overlay/stack.rs +316 -739
  31. data/crates/kobako-runtime/src/filesystem/overlay.rs +2627 -1154
  32. data/crates/kobako-runtime/src/filesystem/pipe.rs +224 -0
  33. data/crates/kobako-runtime/src/filesystem/rdir.rs +84 -0
  34. data/crates/kobako-runtime/src/filesystem/tmpfs/seed.rs +32 -0
  35. data/crates/kobako-runtime/src/filesystem/tmpfs/tmpfile.rs +32 -0
  36. data/crates/kobako-runtime/src/filesystem/tmpfs/whiteout.rs +107 -0
  37. data/crates/kobako-runtime/src/filesystem/tmpfs/xattr.rs +22 -0
  38. data/crates/kobako-runtime/src/filesystem/tmpfs.rs +1997 -0
  39. data/crates/kobako-runtime/src/filesystem/wrap.rs +208 -0
  40. data/crates/kobako-runtime/src/filesystem.rs +25 -388
  41. data/crates/kobako-runtime/src/fork.rs +96 -0
  42. data/crates/kobako-runtime/src/grant.rs +3 -0
  43. data/crates/kobako-runtime/src/hashtable.rs +46 -0
  44. data/crates/kobako-runtime/src/lib.rs +27 -11
  45. data/crates/kobako-runtime/src/limits.rs +195 -0
  46. data/crates/kobako-runtime/src/locking/seqlock.rs +66 -0
  47. data/crates/kobako-runtime/src/locking.rs +228 -0
  48. data/crates/kobako-runtime/src/memcg.rs +383 -0
  49. data/crates/kobako-runtime/src/mount.rs +86 -101
  50. data/crates/kobako-runtime/src/net/backends.rs +43 -0
  51. data/crates/kobako-runtime/src/net/connections.rs +179 -0
  52. data/crates/kobako-runtime/src/net/credential.rs +125 -0
  53. data/crates/kobako-runtime/src/net/egress.rs +160 -0
  54. data/crates/kobako-runtime/src/net/family.rs +181 -0
  55. data/crates/kobako-runtime/src/net/http.rs +2733 -0
  56. data/crates/kobako-runtime/src/net/lookup.rs +285 -0
  57. data/crates/kobako-runtime/src/net/mock.rs +251 -0
  58. data/crates/kobako-runtime/src/net/namespace.rs +1366 -0
  59. data/crates/kobako-runtime/src/net/reactor.rs +531 -0
  60. data/crates/kobako-runtime/src/net/resolv/darwin.rs +275 -0
  61. data/crates/kobako-runtime/src/net/resolv/stub/tests.rs +469 -0
  62. data/crates/kobako-runtime/src/net/resolv/stub.rs +536 -0
  63. data/crates/kobako-runtime/src/net/resolv/windows.rs +248 -0
  64. data/crates/kobako-runtime/src/net/resolv.rs +227 -0
  65. data/crates/kobako-runtime/src/net/socket.rs +870 -0
  66. data/crates/kobako-runtime/src/net/tcp.rs +1588 -0
  67. data/crates/kobako-runtime/src/net/tls.rs +58 -0
  68. data/crates/kobako-runtime/src/net/transport.rs +279 -0
  69. data/crates/kobako-runtime/src/net/udp.rs +1003 -0
  70. data/crates/kobako-runtime/src/net.rs +42 -0
  71. data/crates/kobako-runtime/src/network/builder.rs +177 -0
  72. data/crates/kobako-runtime/src/network/ruleset.rs +777 -0
  73. data/crates/kobako-runtime/src/network.rs +551 -0
  74. data/crates/kobako-runtime/src/policy.rs +16 -0
  75. data/crates/kobako-runtime/src/profile.rs +37 -16
  76. data/crates/kobako-runtime/src/runtime.rs +3 -5
  77. data/crates/kobako-runtime/src/sched.rs +311 -0
  78. data/crates/kobako-runtime/src/security/entry_policy.rs +22 -0
  79. data/crates/kobako-runtime/src/security/hooks.rs +242 -0
  80. data/crates/kobako-runtime/src/security/mount_policy.rs +158 -0
  81. data/crates/kobako-runtime/src/security.rs +601 -0
  82. data/crates/kobako-runtime/src/snapshot.rs +121 -2
  83. data/crates/kobako-runtime/src/vfs/attr.rs +162 -0
  84. data/crates/kobako-runtime/src/vfs/dentry.rs +1466 -0
  85. data/crates/kobako-runtime/src/vfs/file.rs +1911 -0
  86. data/crates/kobako-runtime/src/vfs/inode.rs +425 -0
  87. data/crates/kobako-runtime/src/vfs/kern_path.rs +213 -0
  88. data/crates/kobako-runtime/src/vfs/mount.rs +60 -0
  89. data/crates/kobako-runtime/src/vfs/namei.rs +5837 -0
  90. data/crates/kobako-runtime/src/vfs/namespace.rs +162 -0
  91. data/crates/kobako-runtime/src/vfs/open_files.rs +69 -0
  92. data/crates/kobako-runtime/src/vfs/openat2.rs +116 -0
  93. data/crates/kobako-runtime/src/vfs/path.rs +64 -0
  94. data/crates/kobako-runtime/src/vfs/readdir.rs +163 -0
  95. data/crates/kobako-runtime/src/vfs/stat.rs +293 -0
  96. data/crates/kobako-runtime/src/vfs/super_block.rs +126 -0
  97. data/crates/kobako-runtime/src/vfs/utimes.rs +299 -0
  98. data/crates/kobako-runtime/src/vfs.rs +63 -0
  99. data/crates/kobako-runtime/src/wait.rs +356 -0
  100. data/crates/kobako-transport/CHANGELOG.md +39 -2
  101. data/crates/kobako-transport/Cargo.toml +3 -3
  102. data/crates/kobako-transport/README.md +8 -8
  103. data/crates/kobako-transport/src/abi.rs +78 -72
  104. data/crates/kobako-transport/src/envelope/bytes.rs +49 -30
  105. data/crates/kobako-transport/src/envelope/call.rs +37 -9
  106. data/crates/kobako-transport/src/envelope/compile_reply.rs +2 -2
  107. data/crates/kobako-transport/src/envelope/error_record.rs +24 -3
  108. data/crates/kobako-transport/src/envelope/fault.rs +2 -2
  109. data/crates/kobako-transport/src/envelope/name.rs +99 -0
  110. data/crates/kobako-transport/src/envelope/outcome.rs +11 -11
  111. data/crates/kobako-transport/src/envelope/preamble.rs +34 -31
  112. data/crates/kobako-transport/src/envelope/reply.rs +29 -29
  113. data/crates/kobako-transport/src/envelope/request.rs +2 -2
  114. data/crates/kobako-transport/src/envelope/run.rs +1 -1
  115. data/crates/kobako-transport/src/envelope/unsupported.rs +12 -12
  116. data/crates/kobako-transport/src/envelope.rs +15 -10
  117. data/crates/kobako-wasmtime/CHANGELOG.md +315 -8
  118. data/crates/kobako-wasmtime/Cargo.toml +35 -15
  119. data/crates/kobako-wasmtime/README.md +11 -6
  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 +19 -47
  123. data/crates/kobako-wasmtime/src/ambient.rs +33 -19
  124. data/crates/kobako-wasmtime/src/cache.rs +28 -20
  125. data/crates/kobako-wasmtime/src/config.rs +59 -60
  126. data/crates/kobako-wasmtime/src/driver.rs +63 -145
  127. data/crates/kobako-wasmtime/src/exports.rs +147 -32
  128. data/crates/kobako-wasmtime/src/guest.rs +44 -0
  129. data/crates/kobako-wasmtime/src/guest_binary.rs +14 -0
  130. data/crates/kobako-wasmtime/src/imports.rs +48 -18
  131. data/crates/kobako-wasmtime/src/instance_pre.rs +7 -7
  132. data/crates/kobako-wasmtime/src/invocation.rs +93 -51
  133. data/crates/kobako-wasmtime/src/lib.rs +6 -7
  134. data/crates/kobako-wasmtime/src/limiter.rs +52 -211
  135. data/crates/kobako-wasmtime/src/preview1/descriptors.rs +309 -0
  136. data/crates/kobako-wasmtime/src/preview1/differential/mount.rs +70 -57
  137. data/crates/kobako-wasmtime/src/preview1/differential.rs +137 -27
  138. data/crates/kobako-wasmtime/src/preview1/errno.rs +90 -233
  139. data/crates/kobako-wasmtime/src/preview1/fd.rs +1301 -522
  140. data/crates/kobako-wasmtime/src/preview1/memory.rs +126 -150
  141. data/crates/kobako-wasmtime/src/preview1/mount_view/wrap.rs +192 -0
  142. data/crates/kobako-wasmtime/src/preview1/mount_view.rs +1989 -708
  143. data/crates/kobako-wasmtime/src/preview1/path.rs +181 -365
  144. data/crates/kobako-wasmtime/src/preview1/poll.rs +345 -173
  145. data/crates/kobako-wasmtime/src/preview1/sock.rs +10 -25
  146. data/crates/kobako-wasmtime/src/preview1/testsuite-expected.txt +6 -0
  147. data/crates/kobako-wasmtime/src/preview1/testsuite.rs +147 -0
  148. data/crates/kobako-wasmtime/src/preview1.rs +471 -416
  149. data/crates/kobako-wasmtime/src/preview2/abi.rs +227 -0
  150. data/crates/kobako-wasmtime/src/preview2/error.rs +118 -0
  151. data/crates/kobako-wasmtime/src/preview2/filesystem.rs +122 -0
  152. data/crates/kobako-wasmtime/src/preview2/host.rs +3959 -0
  153. data/crates/kobako-wasmtime/src/preview2/http.rs +1503 -0
  154. data/crates/kobako-wasmtime/src/preview2/io.rs +315 -0
  155. data/crates/kobako-wasmtime/src/preview2/sockets.rs +379 -0
  156. data/crates/kobako-wasmtime/src/preview2/table.rs +305 -0
  157. data/crates/kobako-wasmtime/src/preview2.rs +57 -0
  158. data/crates/kobako-wasmtime/src/trap.rs +31 -16
  159. data/crates/kobako-wasmtime/src/wasi.rs +238 -93
  160. data/crates/kobako-wasmtime/wit/deps/cli.wit +233 -0
  161. data/crates/kobako-wasmtime/wit/deps/clocks.wit +162 -0
  162. data/crates/kobako-wasmtime/wit/deps/filesystem.wit +589 -0
  163. data/crates/kobako-wasmtime/wit/deps/http.wit +733 -0
  164. data/crates/kobako-wasmtime/wit/deps/io.wit +299 -0
  165. data/crates/kobako-wasmtime/wit/deps/random.wit +92 -0
  166. data/crates/kobako-wasmtime/wit/deps/sockets.wit +949 -0
  167. data/crates/kobako-wasmtime/witx/typenames.witx +750 -0
  168. data/crates/kobako-wasmtime/witx/wasi_snapshot_preview1.witx +441 -0
  169. data/data/kobako.wasm +0 -0
  170. data/ext/kobako/Cargo.toml +1 -1
  171. data/ext/kobako/src/{runtime/errors.rs → errors.rs} +35 -27
  172. data/ext/kobako/src/escape.rs +98 -0
  173. data/ext/kobako/src/{runtime/filesystem → filesystem}/grants.rs +36 -16
  174. data/ext/kobako/src/filesystem/ruby.rs +689 -0
  175. data/ext/kobako/src/filesystem.rs +217 -0
  176. data/ext/kobako/src/gvl.rs +306 -0
  177. data/ext/kobako/src/lib.rs +64 -3
  178. data/ext/kobako/src/network/mock.rs +187 -0
  179. data/ext/kobako/src/network.rs +591 -0
  180. data/ext/kobako/src/runtime/bridge.rs +31 -30
  181. data/ext/kobako/src/runtime/snapshot.rs +5 -5
  182. data/ext/kobako/src/runtime.rs +221 -138
  183. data/ext/kobako/src/wire.rs +42 -0
  184. data/lib/kobako/catalog/extensions.rb +19 -7
  185. data/lib/kobako/catalog/flags.rb +4 -6
  186. data/lib/kobako/catalog/grants.rb +5 -4
  187. data/lib/kobako/catalog/handles.rb +9 -31
  188. data/lib/kobako/catalog/mount.rb +12 -12
  189. data/lib/kobako/catalog/network.rb +35 -0
  190. data/lib/kobako/catalog/replacements.rb +28 -0
  191. data/lib/kobako/catalog/services.rb +7 -21
  192. data/lib/kobako/catalog/snippets.rb +10 -25
  193. data/lib/kobako/catalog.rb +4 -1
  194. data/lib/kobako/codec/{utils.rb → boundary.rb} +5 -3
  195. data/lib/kobako/codec/decoder.rb +12 -12
  196. data/lib/kobako/codec/ext_types.rb +5 -7
  197. data/lib/kobako/codec/handle_walk.rb +5 -47
  198. data/lib/kobako/codec/nesting.rb +7 -7
  199. data/lib/kobako/codec/value_budget.rb +134 -42
  200. data/lib/kobako/codec.rb +1 -1
  201. data/lib/kobako/context.rb +28 -15
  202. data/lib/kobako/errors.rb +15 -12
  203. data/lib/kobako/extension.rb +3 -2
  204. data/lib/kobako/filesystem.rb +53 -15
  205. data/lib/kobako/handle.rb +2 -2
  206. data/lib/kobako/names.rb +28 -0
  207. data/lib/kobako/network.rb +210 -0
  208. data/lib/kobako/outcome.rb +7 -7
  209. data/lib/kobako/payload/arguments.rb +2 -2
  210. data/lib/kobako/pool.rb +47 -25
  211. data/lib/kobako/runtime.rb +3 -2
  212. data/lib/kobako/sandbox.rb +113 -56
  213. data/lib/kobako/sandbox_options.rb +41 -107
  214. data/lib/kobako/snippet/bytecode.rb +1 -1
  215. data/lib/kobako/snippet/source.rb +1 -1
  216. data/lib/kobako/transport/argument_wrap.rb +64 -0
  217. data/lib/kobako/transport/block_failure.rb +1 -1
  218. data/lib/kobako/transport/dispatcher.rb +10 -10
  219. data/lib/kobako/transport/exposure.rb +28 -17
  220. data/lib/kobako/transport/reflection.rb +28 -11
  221. data/lib/kobako/transport/run.rb +13 -28
  222. data/lib/kobako/transport/yielder.rb +3 -10
  223. data/lib/kobako/transport.rb +1 -0
  224. data/lib/kobako/version.rb +1 -1
  225. data/lib/kobako.rb +38 -0
  226. data/release-please-config.json +16 -0
  227. data/sig/core/thread.rbs +6 -0
  228. data/sig/kobako/catalog/extensions.rbs +5 -3
  229. data/sig/kobako/catalog/grants.rbs +7 -4
  230. data/sig/kobako/catalog/handles.rbs +2 -4
  231. data/sig/kobako/catalog/mount.rbs +3 -5
  232. data/sig/kobako/catalog/network.rbs +20 -0
  233. data/sig/kobako/catalog/replacements.rbs +14 -0
  234. data/sig/kobako/catalog/services.rbs +1 -5
  235. data/sig/kobako/catalog/snippets.rbs +3 -5
  236. data/sig/kobako/catalog.rbs +1 -1
  237. data/sig/kobako/codec/boundary.rbs +9 -0
  238. data/sig/kobako/codec/decoder.rbs +1 -1
  239. data/sig/kobako/codec/handle_walk.rbs +0 -6
  240. data/sig/kobako/codec/nesting.rbs +2 -2
  241. data/sig/kobako/codec/value_budget.rbs +48 -5
  242. data/sig/kobako/codec.rbs +1 -2
  243. data/sig/kobako/context.rbs +4 -2
  244. data/sig/kobako/errors.rbs +4 -4
  245. data/sig/kobako/filesystem.rbs +38 -15
  246. data/sig/kobako/guest_yielder.rbs +1 -1
  247. data/sig/kobako/network.rbs +115 -0
  248. data/sig/kobako/outcome.rbs +1 -1
  249. data/sig/kobako/payload/arguments.rbs +1 -1
  250. data/sig/kobako/pool.rbs +8 -4
  251. data/sig/kobako/runtime.rbs +32 -3
  252. data/sig/kobako/sandbox.rbs +22 -7
  253. data/sig/kobako/sandbox_options.rbs +29 -38
  254. data/sig/kobako/transport/argument_wrap.rbs +15 -0
  255. data/sig/kobako/transport/dispatcher.rbs +2 -2
  256. data/sig/kobako/transport/error.rbs +1 -1
  257. data/sig/kobako/transport/exposure.rbs +3 -5
  258. data/sig/kobako/transport/reflection.rbs +12 -2
  259. data/sig/kobako/transport/run.rbs +3 -5
  260. data/sig/kobako/transport/yielder.rbs +0 -3
  261. data/sig/kobako/transport.rbs +2 -2
  262. data/sig/kobako.rbs +13 -0
  263. metadata +131 -34
  264. data/crates/kobako-runtime/src/filesystem/directory/spelling.rs +0 -194
  265. data/crates/kobako-runtime/src/filesystem/directory.rs +0 -1082
  266. data/crates/kobako-runtime/src/filesystem/memory.rs +0 -1100
  267. data/crates/kobako-runtime/src/filesystem/overlay/exclusive.rs +0 -88
  268. data/crates/kobako-runtime/src/filesystem/path.rs +0 -60
  269. data/crates/kobako-runtime/src/filesystem/temporary.rs +0 -167
  270. data/crates/kobako-wasmtime/src/capture.rs +0 -96
  271. data/crates/kobako-wasmtime/src/descriptors.rs +0 -167
  272. data/crates/kobako-wasmtime/src/dispatch.rs +0 -125
  273. data/crates/kobako-wasmtime/src/guest_mem.rs +0 -271
  274. data/crates/kobako-wasmtime/src/io.rs +0 -89
  275. data/crates/kobako-wasmtime/src/preview1/args.rs +0 -60
  276. data/crates/kobako-wasmtime/src/preview1/beneath.rs +0 -188
  277. data/crates/kobako-wasmtime/tests/compile_posture.rs +0 -43
  278. data/crates/kobako-wasmtime/tests/guest_file.rs +0 -1054
  279. data/crates/kobako-wasmtime/tests/preamble_flags.rs +0 -70
  280. data/crates/kobako-wasmtime/tests/snippet_compile.rs +0 -95
  281. data/crates/kobako-wasmtime/tests/support/mod.rs +0 -30
  282. data/crates/kobako-wasmtime/tests/unanswered_call.rs +0 -86
  283. data/crates/kobako-wasmtime/tests/unsupported_call.rs +0 -51
  284. data/ext/kobako/src/runtime/escape.rs +0 -53
  285. data/ext/kobako/src/runtime/filesystem/ruby.rs +0 -400
  286. data/ext/kobako/src/runtime/filesystem.rs +0 -236
  287. data/ext/kobako/src/runtime/gvl.rs +0 -112
  288. data/lib/kobako/constant_name.rb +0 -8
  289. data/sig/kobako/codec/utils.rbs +0 -9
  290. data/sig/kobako/constant_name.rbs +0 -3
  291. /data/ext/kobako/src/{runtime/pinned_value.rs → pinned_value.rs} +0 -0
@@ -1,198 +1,186 @@
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
- use super::errno::{Answer, Errno, Failure};
15
+ use kobako_runtime::vfs::PollMask;
16
+ use wiggle::GuestPtr;
17
+
18
+ use super::errno::{Answer, Errno};
19
+ use super::fd::get_file;
14
20
  use super::memory::GuestMemory;
15
- use crate::descriptors::Descriptor;
21
+ use super::types::{self, Clockid, Eventrwflags, Eventtype, Subclockflags, SubscriptionU};
16
22
  use crate::invocation::Invocation;
17
- use crate::trap::TimeoutTrap;
18
-
19
- const SUBSCRIPTION_SIZE: u32 = 48;
20
- const EVENT_SIZE: u32 = 32;
21
-
22
- const TAG_CLOCK: u8 = 0;
23
- const TAG_FD_READ: u8 = 1;
24
- const TAG_FD_WRITE: u8 = 2;
25
-
26
- const CLOCK_REALTIME: u32 = 0;
27
- const CLOCK_MONOTONIC: u32 = 1;
28
- /// The highest `clockid` variant.
29
- const CLOCK_MAX: u32 = 3;
30
- const SUBCLOCKFLAGS_ABSTIME: u16 = 1;
23
+ use crate::preview2::Pollable;
24
+ use crate::trap::Interrupted;
31
25
 
32
- /// One subscription as the guest wrote it.
26
+ /// One subscription as the guest wrote it, and the error its event reports.
33
27
  struct Subscription {
34
28
  userdata: u64,
35
29
  kind: Kind,
30
+ error: Errno,
31
+ }
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
+ }
36
44
  }
37
45
 
38
46
  enum Kind {
39
47
  /// A clock, as the nanoseconds left before it fires.
40
48
  Clock(u64),
41
- /// A descriptor to read; `hangup` when a file has nothing left past
42
- /// its position.
43
- FdRead {
44
- hangup: bool,
45
- },
49
+ FdRead,
46
50
  FdWrite,
47
51
  }
48
52
 
49
- const EVENTRWFLAGS_HANGUP: u16 = 1;
50
-
51
53
  pub(crate) fn poll_oneoff(
52
54
  invocation: &Invocation,
53
- mem: &mut GuestMemory,
54
- subs: u32,
55
- events: u32,
55
+ mem: &mut GuestMemory<'_, '_>,
56
+ subs: GuestPtr<types::Subscription>,
57
+ events: GuestPtr<types::Event>,
56
58
  count: u32,
57
- nevents: u32,
58
- ) -> Answer {
59
+ ) -> Answer<types::Size> {
59
60
  if count == 0 {
60
61
  return Err(Errno::Inval.into());
61
62
  }
62
- mem.consume(count, SUBSCRIPTION_SIZE)?;
63
- mem.consume(count, EVENT_SIZE)?;
64
- let subscriptions = (0..count)
65
- .map(|index| {
66
- let sub = GuestMemory::element(subs, index, SUBSCRIPTION_SIZE)?;
67
- read_subscription(invocation, mem, sub)
68
- })
63
+ mem.consume_array::<types::Subscription>(count)?;
64
+ mem.consume_array::<types::Event>(count)?;
65
+ let subscriptions = subs
66
+ .as_array(count)
67
+ .iter()
68
+ .map(|sub| read_subscription(invocation, mem, sub?))
69
69
  .collect::<Answer<Vec<_>>>()?;
70
70
 
71
- let clocks = subscriptions.iter().filter_map(|sub| match sub.kind {
72
- Kind::Clock(left) => Some(left),
73
- Kind::FdRead { .. } | Kind::FdWrite => None,
74
- });
75
- let waited = if clocks.clone().count() == subscriptions.len() {
76
- let soonest = clocks.min().unwrap_or(0);
77
- wait(invocation, soonest)?;
78
- soonest
79
- } else {
80
- 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)),
81
80
  };
82
81
 
83
- let mut written = 0;
84
- for sub in &subscriptions {
85
- let (ready, nbytes) = match sub.kind {
86
- Kind::Clock(left) => (left <= waited, 0),
87
- Kind::FdRead { .. } | Kind::FdWrite => (true, 1),
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),
88
87
  };
89
- if ready {
90
- let event = GuestMemory::element(events, written, EVENT_SIZE)?;
91
- write_event(mem, event, sub, nbytes)?;
92
- written += 1;
93
- }
88
+ mem.write(events.add(written as u32)?, to_event(sub, nbytes))?;
94
89
  }
95
- mem.write_u32(nevents, written)
90
+ Ok(ready.len() as u32)
96
91
  }
97
92
 
98
- fn read_subscription(invocation: &Invocation, mem: &GuestMemory, sub: u32) -> Answer<Subscription> {
99
- let userdata = mem.read_u64(sub)?;
100
- let kind = match mem.read_u8(GuestMemory::field(sub, 8)?)? {
101
- TAG_CLOCK => read_clock(invocation, mem, sub)?,
102
- TAG_FD_READ => match descriptor(invocation, mem, sub)? {
103
- Descriptor::Stdin(_) => Kind::FdRead { hangup: false },
104
- Descriptor::File(file) => Kind::FdRead {
105
- hangup: file.file.metadata()?.len <= file.position,
106
- },
107
- Descriptor::Channel(_) | Descriptor::Directory(_) => return Err(Errno::Badf.into()),
108
- },
109
- TAG_FD_WRITE => match descriptor(invocation, mem, sub)? {
110
- Descriptor::Channel(_) => Kind::FdWrite,
111
- Descriptor::File(file) if file.mount.allows_writes() => Kind::FdWrite,
112
- Descriptor::File(_) => return Err(Errno::Perm.into()),
113
- Descriptor::Stdin(_) | Descriptor::Directory(_) => return Err(Errno::Badf.into()),
114
- },
115
- _ => return Err(Errno::Inval.into()),
93
+ fn read_subscription(
94
+ invocation: &Invocation,
95
+ mem: &GuestMemory<'_, '_>,
96
+ sub: GuestPtr<types::Subscription>,
97
+ ) -> Answer<Subscription> {
98
+ let sub = mem.read(sub)?;
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),
116
109
  };
117
- Ok(Subscription { userdata, kind })
110
+ let mask = match kind {
111
+ Kind::FdRead => PollMask::IN,
112
+ _ => PollMask::OUT,
113
+ };
114
+ Ok(Subscription {
115
+ userdata: sub.userdata,
116
+ kind,
117
+ error: poll_descriptor(invocation, subscribed, mask)?,
118
+ })
118
119
  }
119
120
 
120
- fn descriptor<'a>(
121
- invocation: &'a Invocation,
122
- mem: &GuestMemory,
123
- sub: u32,
124
- ) -> Answer<&'a Descriptor> {
125
- let fd = mem.read_u32(GuestMemory::field(sub, 16)?)?;
126
- invocation
127
- .wasi()
128
- .descriptors()
129
- .get(fd)
130
- .ok_or_else(|| Errno::Badf.into())
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.
127
+ fn poll_descriptor(
128
+ invocation: &Invocation,
129
+ subscribed: types::SubscriptionFdReadwrite,
130
+ event: PollMask,
131
+ ) -> Answer<Errno> {
132
+ let Ok(file) = get_file(invocation.wasi(), subscribed.file_descriptor.into()) else {
133
+ return Ok(Errno::Badf);
134
+ };
135
+ match file.poll(None).contains(event) {
136
+ true => Ok(Errno::Success),
137
+ false => Err(Errno::Badf.into()),
138
+ }
131
139
  }
132
140
 
133
141
  /// A clock, read against the guest's own clocks. Only the realtime and
134
142
  /// monotonic clocks can be waited on, alone or among other
135
143
  /// subscriptions, as in wasmtime-wasi.
136
- fn read_clock(invocation: &Invocation, mem: &GuestMemory, sub: u32) -> Answer<Kind> {
137
- let id = mem.read_u32(GuestMemory::field(sub, 16)?)?;
138
- let timeout = mem.read_u64(GuestMemory::field(sub, 24)?)?;
139
- let flags = mem.read_u16(GuestMemory::field(sub, 40)?)?;
140
- if id > CLOCK_MAX || flags & !SUBCLOCKFLAGS_ABSTIME != 0 {
141
- return Err(Errno::Inval.into());
142
- }
143
- let absolute = flags & SUBCLOCKFLAGS_ABSTIME != 0;
144
+ fn read_clock(invocation: &Invocation, clock: &types::SubscriptionClock) -> Answer<Kind> {
145
+ let absolute = clock
146
+ .flags
147
+ .contains(Subclockflags::SUBSCRIPTION_CLOCK_ABSTIME);
144
148
  let clocks = invocation.wasi().clocks();
145
- let now = match (id, absolute) {
146
- (CLOCK_REALTIME | CLOCK_MONOTONIC, false) => 0,
147
- (CLOCK_MONOTONIC, true) => clocks.monotonic_now(),
148
- (CLOCK_REALTIME, true) => clocks.wall_now(),
149
+ let now = match (clock.id, absolute) {
150
+ (Clockid::Realtime | Clockid::Monotonic, false) => 0,
151
+ (Clockid::Monotonic, true) => clocks.monotonic_now(),
152
+ (Clockid::Realtime, true) => clocks.wall_now(),
149
153
  _ => return Err(Errno::Inval.into()),
150
154
  };
151
- Ok(Kind::Clock(timeout.saturating_sub(now)))
155
+ Ok(Kind::Clock(clock.timeout.saturating_sub(now)))
152
156
  }
153
157
 
154
- /// Wait `nanos` of the guest's time: none on frozen clocks, and on live
155
- /// ones no further than the invocation's deadline, which ends it as a
156
- /// timeout.
157
- fn wait(invocation: &Invocation, nanos: u64) -> Answer {
158
- if nanos == 0 || !invocation.wasi().clocks().advances() {
159
- return Ok(());
160
- }
161
- let wanted = Duration::from_nanos(nanos);
162
- if let Some(deadline) = invocation.deadline() {
163
- let left = deadline.saturating_duration_since(Instant::now());
164
- if wanted >= left {
165
- thread::sleep(left);
166
- return Err(Failure::Trap(wasmtime::Error::new(TimeoutTrap)));
167
- }
158
+ fn to_event(sub: &Subscription, nbytes: u64) -> types::Event {
159
+ types::Event {
160
+ userdata: sub.userdata,
161
+ error: sub.error,
162
+ type_: match sub.kind {
163
+ Kind::Clock(_) => Eventtype::Clock,
164
+ Kind::FdRead => Eventtype::FdRead,
165
+ Kind::FdWrite => Eventtype::FdWrite,
166
+ },
167
+ fd_readwrite: types::EventFdReadwrite {
168
+ nbytes,
169
+ flags: Eventrwflags::empty(),
170
+ },
168
171
  }
169
- thread::sleep(wanted);
170
- Ok(())
171
- }
172
-
173
- fn write_event(mem: &mut GuestMemory, event: u32, sub: &Subscription, nbytes: u64) -> Answer {
174
- let (kind, flags) = match sub.kind {
175
- Kind::Clock(_) => (TAG_CLOCK, 0),
176
- Kind::FdRead { hangup } => (TAG_FD_READ, if hangup { EVENTRWFLAGS_HANGUP } else { 0 }),
177
- Kind::FdWrite => (TAG_FD_WRITE, 0),
178
- };
179
- mem.write_u64(event, sub.userdata)?;
180
- mem.write_u16(GuestMemory::field(event, 8)?, 0)?;
181
- mem.write_u8(GuestMemory::field(event, 10)?, kind)?;
182
- mem.write_u64(GuestMemory::field(event, 16)?, nbytes)?;
183
- mem.write_u16(GuestMemory::field(event, 24)?, flags)
184
172
  }
185
173
 
186
174
  #[cfg(test)]
187
175
  mod tests {
188
- //! 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:
189
177
  //! at once on the frozen ones, and on the live ones no further than
190
178
  //! the deadline.
191
179
  use std::time::{Duration, Instant};
192
180
 
193
181
  use wasmtime::{Module, Store};
194
182
 
195
- use crate::cache::shared_engine;
183
+ use crate::cache::load_engine;
196
184
  use crate::config::Config;
197
185
  use crate::imports;
198
186
  use crate::invocation::Invocation;
@@ -200,39 +188,71 @@ mod tests {
200
188
  use crate::wasi::Wasi;
201
189
  use kobako_runtime::error::Trap;
202
190
  use kobako_runtime::profile::Profile;
191
+ use kobako_runtime::sched::{run_task, Task};
203
192
 
204
- /// One relative monotonic-clock subscription of ten seconds at 0,
205
- /// 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.
206
196
  const WAIT_WAT: &str = r#"
207
197
  (module
208
198
  (import "wasi_snapshot_preview1" "poll_oneoff"
209
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)))
210
202
  (memory (export "memory") 1)
211
203
  (func (export "wait_ten_seconds") (result i32)
212
204
  (i32.store8 (i32.const 8) (i32.const 0))
213
205
  (i32.store (i32.const 16) (i32.const 1))
214
206
  (i64.store (i32.const 24) (i64.const 10000000000))
215
- (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))))
216
232
  "#;
217
233
 
218
- /// Run the ten-second wait at `profile` under `deadline`, answering
219
- /// 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.
220
237
  fn wait_ten_seconds(
221
238
  profile: Profile,
222
239
  deadline: Option<Duration>,
240
+ interrupted: Option<fn(Task)>,
223
241
  ) -> (wasmtime::Result<i32>, Duration) {
224
- let engine = shared_engine().expect("shared engine must be constructible");
225
- let config = Config {
226
- timeout: None,
227
- setup_timeout: None,
228
- memory_limit: None,
229
- stdout_limit: None,
230
- stderr_limit: None,
231
- open_file_limit: None,
232
- profile,
233
- };
234
- let wasi = Wasi::for_invocation(&config, &[], &[]).expect("WASI context must build");
235
- let mut store = Store::new(engine, Invocation::new(None, wasi));
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)>,
251
+ ) -> (wasmtime::Result<i32>, Duration) {
252
+ let engine = load_engine().expect("shared engine must be constructible");
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));
236
256
  store.set_epoch_deadline(NO_TIMEOUT_EPOCH_DELTA);
237
257
  let linker = imports::linker().expect("host imports must link");
238
258
  let module = Module::new(engine, WAIT_WAT).expect("wait module must compile");
@@ -240,20 +260,103 @@ mod tests {
240
260
  .instantiate(&mut store, &module)
241
261
  .expect("wait module must instantiate");
242
262
  let wait = instance
243
- .get_typed_func::<(), i32>(&mut store, "wait_ten_seconds")
263
+ .get_typed_func::<(), i32>(&mut store, export)
244
264
  .expect("wait export must resolve");
245
265
  let started = Instant::now();
246
- store
247
- .data_mut()
248
- .set_deadline(deadline.map(|left| started + left));
249
- 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
+ });
250
274
  (ended, started.elapsed())
251
275
  }
252
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
+
253
356
  // @behavior RT-073
254
357
  #[test]
255
358
  fn a_hermetic_wait_ends_without_the_host_waiting() {
256
- let (ended, spent) = wait_ten_seconds(Profile::Hermetic, None);
359
+ let (ended, spent) = wait_ten_seconds(Profile::Hermetic, None, None);
257
360
  assert_eq!(
258
361
  ended.ok(),
259
362
  Some(0),
@@ -277,18 +380,10 @@ mod tests {
277
380
  (i32.store (i32.const 16) (local.get $id))
278
381
  (call $poll (i32.const 0) (i32.const 64) (i32.const 1) (i32.const 96))))
279
382
  "#;
280
- let engine = shared_engine().expect("shared engine must be constructible");
281
- let config = Config {
282
- timeout: None,
283
- setup_timeout: None,
284
- memory_limit: None,
285
- stdout_limit: None,
286
- stderr_limit: None,
287
- open_file_limit: None,
288
- profile: Profile::Hermetic,
289
- };
290
- let wasi = Wasi::for_invocation(&config, &[], &[]).expect("WASI context must build");
291
- let mut store = Store::new(engine, Invocation::new(None, wasi));
383
+ let engine = load_engine().expect("shared engine must be constructible");
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));
292
387
  store.set_epoch_deadline(NO_TIMEOUT_EPOCH_DELTA);
293
388
  let linker = imports::linker().expect("host imports must link");
294
389
  let module = Module::new(engine, WAIT_ON_WAT).expect("wait module must compile");
@@ -314,10 +409,87 @@ mod tests {
314
409
 
315
410
  const ERRNO_INVAL: i32 = 28;
316
411
 
412
+ /// What a lone wait on standard output answers for event `tag`, 1 to read
413
+ /// and 2 to write.
414
+ fn wait_on_stdout(tag: i32) -> i32 {
415
+ const WAIT_ON_FD_WAT: &str = r#"
416
+ (module
417
+ (import "wasi_snapshot_preview1" "poll_oneoff"
418
+ (func $poll (param i32 i32 i32 i32) (result i32)))
419
+ (memory (export "memory") 1)
420
+ (func (export "wait_on") (param $tag i32) (result i32)
421
+ (i32.store8 (i32.const 8) (local.get $tag))
422
+ (i32.store (i32.const 16) (i32.const 1))
423
+ (call $poll (i32.const 0) (i32.const 64) (i32.const 1) (i32.const 96))))
424
+ "#;
425
+ let engine = load_engine().expect("shared engine must be constructible");
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));
429
+ store.set_epoch_deadline(NO_TIMEOUT_EPOCH_DELTA);
430
+ let linker = imports::linker().expect("host imports must link");
431
+ let module = Module::new(engine, WAIT_ON_FD_WAT).expect("wait module must compile");
432
+ linker
433
+ .instantiate(&mut store, &module)
434
+ .expect("wait module must instantiate")
435
+ .get_typed_func::<i32, i32>(&mut store, "wait_on")
436
+ .expect("wait export must resolve")
437
+ .call(&mut store, tag)
438
+ .expect("the wait must not trap")
439
+ }
440
+
441
+ const ERRNO_BADF: i32 = 8;
442
+
443
+ // Nothing ever makes standard output ready to read, so without the refusal
444
+ // the wait would only end with the invocation.
445
+ // @behavior RT-285
446
+ #[test]
447
+ fn a_wait_on_what_a_descriptor_never_does_answers_ebadf() {
448
+ assert_eq!(
449
+ [1, 2].map(wait_on_stdout),
450
+ [ERRNO_BADF, 0],
451
+ "poll_oneoff on standard output must answer EBADF waiting to read and be ready to \
452
+ write"
453
+ );
454
+ }
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
+
317
488
  // @behavior RT-074
318
489
  #[test]
319
490
  fn a_permissive_wait_ends_at_the_deadline_as_a_timeout() {
320
- 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);
321
493
  let trap = ended.expect_err("a wait past the deadline must not return to the guest");
322
494
  assert!(
323
495
  matches!(to_trap(trap), Trap::Timeout(_)),