kobako 0.30.0 → 0.32.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (288) hide show
  1. checksums.yaml +4 -4
  2. data/.githooks/linux-tests.sh +29 -0
  3. data/.release-please-manifest.json +1 -1
  4. data/CHANGELOG.md +460 -0
  5. data/Cargo.lock +868 -447
  6. data/README.md +43 -4
  7. data/ROADMAP.md +1 -2
  8. data/crates/kobako-runtime/CHANGELOG.md +309 -0
  9. data/crates/kobako-runtime/Cargo.toml +58 -11
  10. data/crates/kobako-runtime/README.md +7 -4
  11. data/crates/kobako-runtime/src/cred.rs +136 -0
  12. data/crates/kobako-runtime/src/dispatch.rs +4 -5
  13. data/crates/kobako-runtime/src/endpoint/buffer.rs +153 -44
  14. data/crates/kobako-runtime/src/endpoint/dispatch.rs +80 -41
  15. data/crates/kobako-runtime/src/endpoint/invoke.rs +23 -36
  16. data/crates/kobako-runtime/src/endpoint.rs +137 -36
  17. data/crates/kobako-runtime/src/errno.rs +391 -0
  18. data/crates/kobako-runtime/src/error.rs +31 -10
  19. data/crates/kobako-runtime/src/filesystem/anon_inodes.rs +52 -0
  20. data/crates/kobako-runtime/src/filesystem/bind/fid.rs +300 -22
  21. data/crates/kobako-runtime/src/filesystem/bind/lookup.rs +101 -29
  22. data/crates/kobako-runtime/src/filesystem/bind/unicode.rs +109 -0
  23. data/crates/kobako-runtime/src/filesystem/bind/walk.rs +52 -34
  24. data/crates/kobako-runtime/src/filesystem/bind.rs +812 -447
  25. data/crates/kobako-runtime/src/filesystem/overlay/file.rs +6 -6
  26. data/crates/kobako-runtime/src/filesystem/overlay/path.rs +43 -65
  27. data/crates/kobako-runtime/src/filesystem/overlay/stack/copy_up.rs +11 -16
  28. data/crates/kobako-runtime/src/filesystem/overlay/stack/dir.rs +47 -72
  29. data/crates/kobako-runtime/src/filesystem/overlay/stack/namei.rs +12 -12
  30. data/crates/kobako-runtime/src/filesystem/overlay/stack/readdir.rs +4 -7
  31. data/crates/kobako-runtime/src/filesystem/overlay/stack.rs +120 -37
  32. data/crates/kobako-runtime/src/filesystem/overlay.rs +578 -388
  33. data/crates/kobako-runtime/src/filesystem/pipe.rs +12 -9
  34. data/crates/kobako-runtime/src/filesystem/rdir.rs +14 -50
  35. data/crates/kobako-runtime/src/filesystem/testing.rs +31 -0
  36. data/crates/kobako-runtime/src/filesystem/tmpfs/seed.rs +32 -0
  37. data/crates/kobako-runtime/src/filesystem/tmpfs/tmpfile.rs +3 -3
  38. data/crates/kobako-runtime/src/filesystem/tmpfs/whiteout.rs +57 -35
  39. data/crates/kobako-runtime/src/filesystem/tmpfs/xattr.rs +2 -2
  40. data/crates/kobako-runtime/src/filesystem/tmpfs.rs +219 -173
  41. data/crates/kobako-runtime/src/filesystem/wrap.rs +36 -52
  42. data/crates/kobako-runtime/src/filesystem.rs +8 -3
  43. data/crates/kobako-runtime/src/fork.rs +319 -0
  44. data/crates/kobako-runtime/src/grant.rs +3 -0
  45. data/crates/kobako-runtime/src/hashtable.rs +4 -2
  46. data/crates/kobako-runtime/src/lib.rs +18 -7
  47. data/crates/kobako-runtime/src/limits.rs +171 -0
  48. data/crates/kobako-runtime/src/locking/seqlock.rs +29 -3
  49. data/crates/kobako-runtime/src/locking.rs +518 -113
  50. data/crates/kobako-runtime/src/memcg.rs +306 -0
  51. data/crates/kobako-runtime/src/mount.rs +37 -9
  52. data/crates/kobako-runtime/src/net/backends.rs +43 -0
  53. data/crates/kobako-runtime/src/net/connections.rs +219 -0
  54. data/crates/kobako-runtime/src/net/credential.rs +125 -0
  55. data/crates/kobako-runtime/src/net/egress.rs +180 -0
  56. data/crates/kobako-runtime/src/net/family.rs +181 -0
  57. data/crates/kobako-runtime/src/net/http.rs +3165 -0
  58. data/crates/kobako-runtime/src/net/lookup.rs +385 -0
  59. data/crates/kobako-runtime/src/net/mock.rs +251 -0
  60. data/crates/kobako-runtime/src/net/namespace.rs +1368 -0
  61. data/crates/kobako-runtime/src/net/reactor.rs +626 -0
  62. data/crates/kobako-runtime/src/net/resolv/darwin.rs +280 -0
  63. data/crates/kobako-runtime/src/net/resolv/stub/tests.rs +519 -0
  64. data/crates/kobako-runtime/src/net/resolv/stub.rs +542 -0
  65. data/crates/kobako-runtime/src/net/resolv/windows.rs +218 -0
  66. data/crates/kobako-runtime/src/net/resolv.rs +251 -0
  67. data/crates/kobako-runtime/src/net/skbuff.rs +109 -0
  68. data/crates/kobako-runtime/src/net/socket.rs +869 -0
  69. data/crates/kobako-runtime/src/net/task.rs +223 -0
  70. data/crates/kobako-runtime/src/net/tcp.rs +1840 -0
  71. data/crates/kobako-runtime/src/net/tls.rs +120 -0
  72. data/crates/kobako-runtime/src/net/transport.rs +304 -0
  73. data/crates/kobako-runtime/src/net/udp.rs +1105 -0
  74. data/crates/kobako-runtime/src/net.rs +45 -0
  75. data/crates/kobako-runtime/src/network/builder.rs +177 -0
  76. data/crates/kobako-runtime/src/network/ruleset.rs +777 -0
  77. data/crates/kobako-runtime/src/network.rs +551 -0
  78. data/crates/kobako-runtime/src/policy.rs +16 -0
  79. data/crates/kobako-runtime/src/profile.rs +29 -144
  80. data/crates/kobako-runtime/src/runtime.rs +3 -5
  81. data/crates/kobako-runtime/src/sched.rs +338 -51
  82. data/crates/kobako-runtime/src/security/entry_policy.rs +22 -0
  83. data/crates/kobako-runtime/src/{vfs/cred.rs → security/hooks.rs} +52 -144
  84. data/crates/kobako-runtime/src/security/mount_policy.rs +158 -0
  85. data/crates/kobako-runtime/src/security.rs +125 -209
  86. data/crates/kobako-runtime/src/snapshot.rs +121 -2
  87. data/crates/kobako-runtime/src/vfs/attr.rs +5 -5
  88. data/crates/kobako-runtime/src/vfs/dentry.rs +210 -117
  89. data/crates/kobako-runtime/src/vfs/file.rs +243 -248
  90. data/crates/kobako-runtime/src/vfs/inode.rs +41 -45
  91. data/crates/kobako-runtime/src/vfs/kern_path.rs +62 -53
  92. data/crates/kobako-runtime/src/vfs/mount.rs +10 -9
  93. data/crates/kobako-runtime/src/vfs/namei.rs +1162 -818
  94. data/crates/kobako-runtime/src/vfs/namespace.rs +245 -0
  95. data/crates/kobako-runtime/src/vfs/open_files.rs +69 -0
  96. data/crates/kobako-runtime/src/vfs/openat2.rs +3 -3
  97. data/crates/kobako-runtime/src/vfs/path.rs +6 -6
  98. data/crates/kobako-runtime/src/vfs/readdir.rs +6 -6
  99. data/crates/kobako-runtime/src/vfs/stat.rs +248 -23
  100. data/crates/kobako-runtime/src/vfs/super_block.rs +18 -14
  101. data/crates/kobako-runtime/src/vfs/utimes.rs +37 -27
  102. data/crates/kobako-runtime/src/vfs.rs +10 -6
  103. data/crates/kobako-runtime/src/wait.rs +381 -0
  104. data/crates/kobako-transport/CHANGELOG.md +38 -0
  105. data/crates/kobako-transport/Cargo.toml +1 -1
  106. data/crates/kobako-transport/README.md +3 -4
  107. data/crates/kobako-transport/src/abi.rs +78 -72
  108. data/crates/kobako-transport/src/envelope/bytes.rs +17 -6
  109. data/crates/kobako-transport/src/envelope/call.rs +32 -4
  110. data/crates/kobako-transport/src/envelope/error_record.rs +21 -0
  111. data/crates/kobako-transport/src/envelope/name.rs +99 -0
  112. data/crates/kobako-transport/src/envelope/outcome.rs +9 -9
  113. data/crates/kobako-transport/src/envelope/preamble.rs +24 -21
  114. data/crates/kobako-transport/src/envelope/reply.rs +27 -27
  115. data/crates/kobako-transport/src/envelope/unsupported.rs +10 -10
  116. data/crates/kobako-transport/src/envelope.rs +13 -8
  117. data/crates/kobako-wasmtime/CHANGELOG.md +208 -0
  118. data/crates/kobako-wasmtime/Cargo.toml +47 -7
  119. data/crates/kobako-wasmtime/README.md +4 -3
  120. data/crates/kobako-wasmtime/build/preview2.rs +1339 -0
  121. data/crates/kobako-wasmtime/build.rs +16 -0
  122. data/crates/kobako-wasmtime/src/abi.rs +18 -37
  123. data/crates/kobako-wasmtime/src/ambient.rs +43 -19
  124. data/crates/kobako-wasmtime/src/cache/unix.rs +95 -0
  125. data/crates/kobako-wasmtime/src/cache/windows.rs +359 -0
  126. data/crates/kobako-wasmtime/src/cache.rs +106 -113
  127. data/crates/kobako-wasmtime/src/config.rs +52 -68
  128. data/crates/kobako-wasmtime/src/driver.rs +80 -82
  129. data/crates/kobako-wasmtime/src/exports.rs +143 -17
  130. data/crates/kobako-wasmtime/src/guest.rs +12 -6
  131. data/crates/kobako-wasmtime/src/guest_binary.rs +14 -0
  132. data/crates/kobako-wasmtime/src/imports.rs +42 -18
  133. data/crates/kobako-wasmtime/src/instance_pre.rs +4 -4
  134. data/crates/kobako-wasmtime/src/invocation.rs +91 -15
  135. data/crates/kobako-wasmtime/src/lib.rs +7 -3
  136. data/crates/kobako-wasmtime/src/limiter.rs +130 -12
  137. data/crates/kobako-wasmtime/src/preview1/descriptors.rs +305 -0
  138. data/crates/kobako-wasmtime/src/preview1/differential/mount.rs +9 -20
  139. data/crates/kobako-wasmtime/src/preview1/differential.rs +27 -31
  140. data/crates/kobako-wasmtime/src/preview1/errno.rs +55 -66
  141. data/crates/kobako-wasmtime/src/preview1/fd.rs +347 -312
  142. data/crates/kobako-wasmtime/src/preview1/memory.rs +21 -12
  143. data/crates/kobako-wasmtime/src/preview1/mount_view/wrap.rs +34 -53
  144. data/crates/kobako-wasmtime/src/preview1/mount_view.rs +335 -388
  145. data/crates/kobako-wasmtime/src/preview1/path.rs +56 -121
  146. data/crates/kobako-wasmtime/src/preview1/poll.rs +255 -125
  147. data/crates/kobako-wasmtime/src/preview1/random.rs +125 -0
  148. data/crates/kobako-wasmtime/src/preview1/sock.rs +10 -8
  149. data/crates/kobako-wasmtime/src/preview1/testing.rs +31 -0
  150. data/crates/kobako-wasmtime/src/preview1/testsuite.rs +5 -15
  151. data/crates/kobako-wasmtime/src/preview1.rs +131 -51
  152. data/crates/kobako-wasmtime/src/preview2/abi.rs +227 -0
  153. data/crates/kobako-wasmtime/src/preview2/error.rs +118 -0
  154. data/crates/kobako-wasmtime/src/preview2/filesystem.rs +122 -0
  155. data/crates/kobako-wasmtime/src/preview2/host.rs +4319 -0
  156. data/crates/kobako-wasmtime/src/preview2/http.rs +1587 -0
  157. data/crates/kobako-wasmtime/src/preview2/io.rs +328 -0
  158. data/crates/kobako-wasmtime/src/preview2/sockets.rs +379 -0
  159. data/crates/kobako-wasmtime/src/preview2/table.rs +257 -0
  160. data/crates/kobako-wasmtime/src/preview2.rs +57 -0
  161. data/crates/kobako-wasmtime/src/trap.rs +88 -28
  162. data/crates/kobako-wasmtime/src/waits.rs +59 -0
  163. data/crates/kobako-wasmtime/src/wasi.rs +291 -81
  164. data/crates/kobako-wasmtime/wit/deps/cli.wit +233 -0
  165. data/crates/kobako-wasmtime/wit/deps/clocks.wit +162 -0
  166. data/crates/kobako-wasmtime/wit/deps/filesystem.wit +589 -0
  167. data/crates/kobako-wasmtime/wit/deps/http.wit +733 -0
  168. data/crates/kobako-wasmtime/wit/deps/io.wit +299 -0
  169. data/crates/kobako-wasmtime/wit/deps/random.wit +92 -0
  170. data/crates/kobako-wasmtime/wit/deps/sockets.wit +949 -0
  171. data/crates/kobako-wasmtime/witx/wasi_snapshot_preview1.witx +13 -0
  172. data/data/kobako.wasm +0 -0
  173. data/ext/kobako/Cargo.toml +6 -2
  174. data/ext/kobako/src/{runtime/errors.rs → errors.rs} +34 -26
  175. data/ext/kobako/src/escape.rs +404 -0
  176. data/ext/kobako/src/{runtime/filesystem → filesystem}/grants.rs +34 -32
  177. data/ext/kobako/src/{runtime/filesystem → filesystem}/ruby.rs +158 -170
  178. data/ext/kobako/src/{runtime/filesystem.rs → filesystem.rs} +71 -23
  179. data/ext/kobako/src/fork.rs +72 -0
  180. data/ext/kobako/src/gvl.rs +729 -0
  181. data/ext/kobako/src/handoff.rs +490 -0
  182. data/ext/kobako/src/lib.rs +74 -3
  183. data/ext/kobako/src/network/mock.rs +187 -0
  184. data/ext/kobako/src/network.rs +593 -0
  185. data/ext/kobako/src/pinned_value.rs +44 -0
  186. data/ext/kobako/src/runtime/bridge.rs +58 -50
  187. data/ext/kobako/src/runtime/snapshot.rs +4 -4
  188. data/ext/kobako/src/runtime.rs +272 -163
  189. data/ext/kobako/src/wire.rs +42 -0
  190. data/lib/kobako/catalog/extensions.rb +14 -2
  191. data/lib/kobako/catalog/flags.rb +3 -5
  192. data/lib/kobako/catalog/grants.rb +5 -4
  193. data/lib/kobako/catalog/handles.rb +14 -32
  194. data/lib/kobako/catalog/mount.rb +11 -3
  195. data/lib/kobako/catalog/network.rb +35 -0
  196. data/lib/kobako/catalog/replacements.rb +28 -0
  197. data/lib/kobako/catalog/services.rb +5 -19
  198. data/lib/kobako/catalog/snippets.rb +3 -18
  199. data/lib/kobako/catalog.rb +3 -0
  200. data/lib/kobako/codec/{utils.rb → boundary.rb} +4 -2
  201. data/lib/kobako/codec/decoder.rb +5 -5
  202. data/lib/kobako/codec/ext_types.rb +3 -3
  203. data/lib/kobako/codec/handle_walk.rb +3 -43
  204. data/lib/kobako/codec/state.rb +2 -18
  205. data/lib/kobako/codec/value_budget.rb +133 -41
  206. data/lib/kobako/codec.rb +6 -7
  207. data/lib/kobako/context.rb +24 -11
  208. data/lib/kobako/errors.rb +18 -18
  209. data/lib/kobako/extension.rb +3 -2
  210. data/lib/kobako/filesystem.rb +10 -3
  211. data/lib/kobako/fork.rb +42 -0
  212. data/lib/kobako/handle.rb +2 -3
  213. data/lib/kobako/names.rb +28 -0
  214. data/lib/kobako/network.rb +210 -0
  215. data/lib/kobako/outcome.rb +7 -7
  216. data/lib/kobako/runtime.rb +3 -2
  217. data/lib/kobako/sandbox.rb +73 -35
  218. data/lib/kobako/sandbox_options.rb +42 -109
  219. data/lib/kobako/transport/argument_wrap.rb +64 -0
  220. data/lib/kobako/transport/block_failure.rb +1 -1
  221. data/lib/kobako/transport/dispatcher.rb +6 -6
  222. data/lib/kobako/transport/exposure.rb +26 -15
  223. data/lib/kobako/transport/reflection.rb +21 -4
  224. data/lib/kobako/transport/run.rb +8 -23
  225. data/lib/kobako/transport/yielder.rb +2 -9
  226. data/lib/kobako/transport.rb +1 -0
  227. data/lib/kobako/usage.rb +4 -2
  228. data/lib/kobako/version.rb +1 -1
  229. data/lib/kobako.rb +8 -4
  230. data/release-please-config.json +15 -0
  231. data/sig/kobako/catalog/extensions.rbs +2 -0
  232. data/sig/kobako/catalog/grants.rbs +7 -4
  233. data/sig/kobako/catalog/handles.rbs +2 -2
  234. data/sig/kobako/catalog/mount.rbs +3 -1
  235. data/sig/kobako/catalog/network.rbs +20 -0
  236. data/sig/kobako/catalog/replacements.rbs +14 -0
  237. data/sig/kobako/catalog/services.rbs +0 -4
  238. data/sig/kobako/catalog/snippets.rbs +0 -2
  239. data/sig/kobako/codec/{utils.rbs → boundary.rbs} +2 -2
  240. data/sig/kobako/codec/handle_walk.rbs +0 -6
  241. data/sig/kobako/codec/state.rbs +0 -4
  242. data/sig/kobako/codec/value_budget.rbs +48 -5
  243. data/sig/kobako/codec.rbs +2 -2
  244. data/sig/kobako/context.rbs +3 -1
  245. data/sig/kobako/errors.rbs +4 -7
  246. data/sig/kobako/filesystem.rbs +21 -3
  247. data/sig/kobako/fork.rbs +17 -0
  248. data/sig/kobako/network.rbs +115 -0
  249. data/sig/kobako/outcome.rbs +1 -1
  250. data/sig/kobako/runtime.rbs +32 -3
  251. data/sig/kobako/sandbox.rbs +10 -2
  252. data/sig/kobako/sandbox_options.rbs +29 -40
  253. data/sig/kobako/transport/argument_wrap.rbs +15 -0
  254. data/sig/kobako/transport/error.rbs +1 -1
  255. data/sig/kobako/transport/exposure.rbs +2 -4
  256. data/sig/kobako/transport/reflection.rbs +10 -0
  257. data/sig/kobako/transport/run.rbs +0 -2
  258. data/sig/kobako/transport/yielder.rbs +0 -3
  259. data/sig/kobako/transport.rbs +1 -1
  260. data/sig/kobako.rbs +13 -0
  261. metadata +97 -37
  262. data/crates/kobako-runtime/src/endpoint/capture.rs +0 -115
  263. data/crates/kobako-runtime/src/endpoint/memory_cap.rs +0 -238
  264. data/crates/kobako-runtime/src/filesystem/path.rs +0 -21
  265. data/crates/kobako-runtime/src/vfs/errno.rs +0 -153
  266. data/crates/kobako-runtime/tests/linux_baseline/calls.rs +0 -171
  267. data/crates/kobako-runtime/tests/linux_baseline/concurrent.rs +0 -276
  268. data/crates/kobako-runtime/tests/linux_baseline/kobako.rs +0 -323
  269. data/crates/kobako-runtime/tests/linux_baseline/linux-baseline-expected.txt +0 -16
  270. data/crates/kobako-runtime/tests/linux_baseline/linux.rs +0 -449
  271. data/crates/kobako-runtime/tests/linux_baseline/main.rs +0 -611
  272. data/crates/kobako-runtime/tests/linux_baseline/permissions.rs +0 -236
  273. data/crates/kobako-wasmtime/src/descriptors.rs +0 -200
  274. data/crates/kobako-wasmtime/tests/integration/compile_posture.rs +0 -43
  275. data/crates/kobako-wasmtime/tests/integration/guest_file.rs +0 -1085
  276. data/crates/kobako-wasmtime/tests/integration/main.rs +0 -29
  277. data/crates/kobako-wasmtime/tests/integration/preamble_flags.rs +0 -70
  278. data/crates/kobako-wasmtime/tests/integration/snippet_compile.rs +0 -95
  279. data/crates/kobako-wasmtime/tests/integration/support/mod.rs +0 -30
  280. data/crates/kobako-wasmtime/tests/integration/unanswered_call.rs +0 -86
  281. data/crates/kobako-wasmtime/tests/integration/unsupported_call.rs +0 -51
  282. data/ext/kobako/src/runtime/escape.rs +0 -67
  283. data/ext/kobako/src/runtime/gvl.rs +0 -147
  284. data/ext/kobako/src/runtime/pinned_value.rs +0 -31
  285. data/lib/kobako/constant_name.rb +0 -8
  286. data/lib/kobako/pool.rb +0 -140
  287. data/sig/kobako/constant_name.rbs +0 -3
  288. data/sig/kobako/pool.rbs +0 -42
@@ -8,16 +8,17 @@
8
8
 
9
9
  use std::time::{Duration, SystemTime, UNIX_EPOCH};
10
10
 
11
- use kobako_runtime::filesystem::{FileType, FilesystemError, Iattr, Kstat, SetTime};
12
- use kobako_runtime::profile::Profile;
11
+ use kobako_runtime::cred::Cred;
12
+ use kobako_runtime::filesystem::{FileType, Iattr, Kstat, SetTime};
13
13
  use kobako_runtime::vfs::{self, verify_dirent_name, DirContext, DirEmit, File, Whence};
14
14
  use wiggle::{GuestMemory as WitxMemory, GuestPtr, GuestType};
15
15
 
16
+ use super::descriptors::{Descriptor, FileRef, FileRefMut};
16
17
  use super::errno::{Answer, Errno};
17
18
  use super::memory::GuestMemory;
18
- use super::path::retry_eagain;
19
19
  use super::types::{self, Fdflags, Filetype, Fstflags, Rights};
20
- use crate::descriptors::{Descriptor, Descriptors};
20
+ use crate::preview2::{filesystem, try_to_nanos};
21
+ use crate::wasi::Wasi;
21
22
 
22
23
  /// What a preopen allows itself: every path call, as wasmtime-wasi hands
23
24
  /// most userlands.
@@ -62,32 +63,38 @@ fn directory_rights_inheriting() -> Rights {
62
63
 
63
64
  /// What `fd` stands for, a file that stands only for its path included, as
64
65
  /// `fdget_raw` takes one.
65
- fn descriptor(table: &Descriptors, fd: u32) -> Answer<&Descriptor> {
66
- table.get(fd).ok_or_else(|| Errno::Badf.into())
66
+ fn get_descriptor(wasi: &Wasi, fd: u32) -> Answer<&Descriptor> {
67
+ wasi.descriptors().get(fd).ok_or_else(|| Errno::Badf.into())
68
+ }
69
+
70
+ /// The file `fd` stands for, one that stands only for its path included, as
71
+ /// `fdget_raw` takes one.
72
+ fn get_raw_file(wasi: &Wasi, fd: u32) -> Answer<FileRef<'_>> {
73
+ wasi.file(fd).ok_or_else(|| Errno::Badf.into())
67
74
  }
68
75
 
69
76
  /// The open file `fd` stands for, refusing one that stands only for its path,
70
77
  /// as `fdget` does.
71
- pub(crate) fn file(table: &Descriptors, fd: u32) -> Answer<&File> {
72
- match table.get(fd) {
73
- Some(descriptor) if !descriptor.file.is_path() => Ok(&descriptor.file),
78
+ pub(crate) fn get_file(wasi: &Wasi, fd: u32) -> Answer<FileRef<'_>> {
79
+ match wasi.file(fd) {
80
+ Some(file) if !file.is_path() => Ok(file),
74
81
  _ => Err(Errno::Badf.into()),
75
82
  }
76
83
  }
77
84
 
78
85
  /// The open file `fd` stands for, to change, refusing one that stands only for
79
86
  /// its path, as `fdget` does.
80
- fn file_mut(table: &mut Descriptors, fd: u32) -> Answer<&mut File> {
81
- match table.get_mut(fd) {
82
- Some(descriptor) if !descriptor.file.is_path() => Ok(&mut descriptor.file),
87
+ fn get_file_mut(wasi: &mut Wasi, fd: u32) -> Answer<FileRefMut<'_>> {
88
+ match wasi.file_mut(fd) {
89
+ Some(file) if !file.is_path() => Ok(file),
83
90
  _ => Err(Errno::Badf.into()),
84
91
  }
85
92
  }
86
93
 
87
94
  /// The open file `fd` stands for, in a call about its position. wasi-testsuite
88
95
  /// has a directory refuse one, where Linux would seek it.
89
- fn seekable_file(table: &mut Descriptors, fd: u32) -> Answer<&mut File> {
90
- match file_mut(table, fd)? {
96
+ fn get_seekable(wasi: &mut Wasi, fd: u32) -> Answer<FileRefMut<'_>> {
97
+ match get_file_mut(wasi, fd)? {
91
98
  file if file.is_directory() => Err(Errno::Badf.into()),
92
99
  file => Ok(file),
93
100
  }
@@ -95,8 +102,8 @@ fn seekable_file(table: &mut Descriptors, fd: u32) -> Answer<&mut File> {
95
102
 
96
103
  /// A call any file or directory answers, which preview1 refuses for
97
104
  /// stdio.
98
- fn refuse_stream(table: &Descriptors, fd: u32) -> Answer {
99
- match descriptor(table, fd)?.file.is_stream() {
105
+ fn refuse_stream(wasi: &Wasi, fd: u32) -> Answer {
106
+ match get_raw_file(wasi, fd)?.is_stream() {
100
107
  true => Err(Errno::Badf.into()),
101
108
  false => Ok(()),
102
109
  }
@@ -116,29 +123,30 @@ fn to_filetype(file_type: FileType) -> Filetype {
116
123
  }
117
124
  }
118
125
 
119
- /// A `filestat` for what a Filesystem reports a path names, as the guest
120
- /// holding `table` sees it, at time zero where it keeps no time.
121
- pub(crate) fn to_filestat(table: &Descriptors, stat: Kstat) -> types::Filestat {
122
- let stat = table.profile().guest_kstat(stat);
123
- types::Filestat {
126
+ /// A `filestat` for what a Filesystem reports a path names, as `cred` sees
127
+ /// it, at time zero where it keeps no time.
128
+ pub(crate) fn to_filestat(cred: &Cred, stat: Kstat) -> Answer<types::Filestat> {
129
+ let stat = vfs::cp_new_stat(cred, stat)?;
130
+ Ok(types::Filestat {
124
131
  dev: stat.dev,
125
132
  ino: stat.ino,
126
133
  filetype: to_filetype(stat.file_type),
127
134
  nlink: stat.nlink,
128
135
  size: stat.size,
129
- atim: to_timestamp(stat.atime),
130
- mtim: to_timestamp(stat.mtime),
131
- ctim: to_timestamp(stat.ctime),
132
- }
136
+ atim: to_timestamp(stat.atime)?,
137
+ mtim: to_timestamp(stat.mtime)?,
138
+ ctim: to_timestamp(stat.ctime)?,
139
+ })
133
140
  }
134
141
 
135
- /// `time` as a preview1 `timestamp`, nanoseconds since the Unix epoch;
136
- /// zero for none, and the largest timestamp for a time past it.
137
- fn to_timestamp(time: Option<SystemTime>) -> u64 {
138
- time.and_then(|time| time.duration_since(UNIX_EPOCH).ok())
139
- .map_or(0, |since| {
140
- u64::try_from(since.as_nanos()).unwrap_or(u64::MAX)
141
- })
142
+ /// `time` as a preview1 `timestamp`, nanoseconds since the Unix epoch, and
143
+ /// zero for none or one before it. One past what the `u64` holds answers
144
+ /// `OVERFLOW`, as wasmtime-wasi's conversion from a `datetime` does.
145
+ fn to_timestamp(time: Option<SystemTime>) -> Answer<u64> {
146
+ let Some(since) = time.and_then(|time| time.duration_since(UNIX_EPOCH).ok()) else {
147
+ return Ok(0);
148
+ };
149
+ try_to_nanos(since).map_err(|_| Errno::Overflow.into())
142
150
  }
143
151
 
144
152
  /// Set the times `flags` ask through `call`, answering before it looks at the
@@ -171,70 +179,75 @@ fn to_iattr(
171
179
  };
172
180
  let atime = time(atim, Fstflags::ATIM, Fstflags::ATIM_NOW)?;
173
181
  let mtime = time(mtim, Fstflags::MTIM, Fstflags::MTIM_NOW)?;
174
- Ok((atime.is_some() || mtime.is_some()).then(|| Iattr::times(atime, mtime)))
182
+ Ok((atime.is_some() || mtime.is_some()).then(|| Iattr::for_times(atime, mtime)))
175
183
  }
176
184
 
177
- pub(crate) fn advise(table: &mut Descriptors, fd: u32, offset: u64, len: u64) -> Answer {
178
- Ok(file_mut(table, fd)?.fadvise(offset, len)?)
185
+ pub(crate) fn advise(wasi: &mut Wasi, fd: u32, offset: u64, len: u64) -> Answer {
186
+ Ok(get_file_mut(wasi, fd)?.fadvise(offset, len)?)
179
187
  }
180
188
 
181
- pub(crate) fn allocate(table: &mut Descriptors, fd: u32, offset: u64, len: u64) -> Answer {
182
- Ok(file_mut(table, fd)?.fallocate(offset, len)?)
189
+ pub(crate) fn allocate(wasi: &mut Wasi, fd: u32, offset: u64, len: u64) -> Answer {
190
+ Ok(get_file_mut(wasi, fd)?.fallocate(offset, len)?)
183
191
  }
184
192
 
185
- pub(crate) fn close(table: &mut Descriptors, fd: u32) -> Answer {
186
- Ok(table.remove(fd).ok_or(Errno::Badf)?.file.close()?)
193
+ pub(crate) fn close(wasi: &mut Wasi, fd: u32) -> Answer {
194
+ match wasi.take(fd).ok_or(Errno::Badf)? {
195
+ Some(file) => Ok(file.close()?),
196
+ None => Ok(()),
197
+ }
187
198
  }
188
199
 
189
- pub(crate) fn sync(table: &mut Descriptors, fd: u32, datasync: bool) -> Answer {
190
- Ok(file_mut(table, fd)?.fsync(datasync)?)
200
+ pub(crate) fn sync(wasi: &mut Wasi, fd: u32, datasync: bool) -> Answer {
201
+ Ok(get_file_mut(wasi, fd)?.fsync(datasync)?)
191
202
  }
192
203
 
193
- pub(crate) fn fdstat_get(table: &Descriptors, fd: u32) -> Answer<types::Fdstat> {
194
- let descriptor = descriptor(table, fd)?;
195
- let file = &descriptor.file;
204
+ pub(crate) fn fdstat_get(wasi: &Wasi, fd: u32) -> Answer<types::Fdstat> {
205
+ let preopen = get_descriptor(wasi, fd)?.preopen_path.is_some();
206
+ Ok(to_fdstat(&*get_raw_file(wasi, fd)?, preopen))
207
+ }
208
+
209
+ /// What `fd_fdstat_get` reports of `file`, a preopen's root when `preopen`.
210
+ fn to_fdstat(file: &File, preopen: bool) -> types::Fdstat {
196
211
  let mut fs_flags = Fdflags::empty();
197
- fs_flags.set(Fdflags::APPEND, file.append());
198
- fs_flags.set(Fdflags::NONBLOCK, file.nonblock());
199
- fs_flags.set(Fdflags::DSYNC, file.dsync());
200
- fs_flags.set(Fdflags::SYNC, file.sync());
212
+ fs_flags.set(Fdflags::APPEND, file.is_append());
213
+ fs_flags.set(Fdflags::NONBLOCK, file.is_nonblock());
214
+ fs_flags.set(Fdflags::DSYNC, file.is_dsync());
215
+ // Linux's `O_RSYNC` is `O_SYNC`, so a file opened to sync holds both.
216
+ fs_flags.set(Fdflags::SYNC | Fdflags::RSYNC, file.is_sync());
201
217
  let (fs_rights_base, fs_rights_inheriting) = if file.is_stream() {
202
218
  let mut rights = Rights::empty();
203
- rights.set(Rights::FD_READ, file.readable());
204
- rights.set(Rights::FD_WRITE, file.writable());
219
+ rights.set(Rights::FD_READ, file.is_readable());
220
+ rights.set(Rights::FD_WRITE, file.is_writable());
205
221
  (rights, rights)
206
- } else if descriptor.preopen_path.is_some() {
222
+ } else if preopen {
207
223
  (preopen_rights(), directory_rights_inheriting())
208
- } else if file.is_directory() {
209
- let mut rights = Rights::all()
210
- - Rights::FD_WRITE
211
- - Rights::FD_SEEK
212
- - Rights::FD_FILESTAT_SET_SIZE
213
- - Rights::PATH_FILESTAT_SET_SIZE;
214
- if !file.readable() {
215
- rights -= Rights::FD_READ | Rights::FD_READDIR;
216
- }
217
- (rights, directory_rights_inheriting())
218
224
  } else {
219
225
  let mut rights = Rights::all();
220
- if !file.readable() {
226
+ if file.is_directory() {
227
+ rights -=
228
+ Rights::FD_SEEK | Rights::FD_FILESTAT_SET_SIZE | Rights::PATH_FILESTAT_SET_SIZE;
229
+ }
230
+ if !file.is_readable() {
221
231
  rights -= Rights::FD_READ | Rights::FD_READDIR;
222
232
  }
223
- if !file.writable() {
233
+ if !file.is_writable() {
224
234
  rights -= Rights::FD_WRITE;
225
235
  }
226
- (rights, rights)
236
+ match file.is_directory() {
237
+ true => (rights, directory_rights_inheriting()),
238
+ false => (rights, rights),
239
+ }
227
240
  };
228
- Ok(types::Fdstat {
229
- fs_filetype: to_filetype(file.kind()),
241
+ types::Fdstat {
242
+ fs_filetype: to_filetype(file.file_type()),
230
243
  fs_flags,
231
244
  fs_rights_base,
232
245
  fs_rights_inheriting,
233
- })
246
+ }
234
247
  }
235
248
 
236
- pub(crate) fn fdstat_set_flags(table: &mut Descriptors, fd: u32, flags: Fdflags) -> Answer {
237
- file_mut(table, fd)?.setfl(
249
+ pub(crate) fn fdstat_set_flags(wasi: &mut Wasi, fd: u32, flags: Fdflags) -> Answer {
250
+ get_file_mut(wasi, fd)?.setfl(
238
251
  flags.contains(Fdflags::APPEND),
239
252
  flags.contains(Fdflags::NONBLOCK),
240
253
  );
@@ -242,47 +255,57 @@ pub(crate) fn fdstat_set_flags(table: &mut Descriptors, fd: u32, flags: Fdflags)
242
255
  }
243
256
 
244
257
  /// Rights only narrow at open, so a later change is unsupported.
245
- pub(crate) fn fdstat_set_rights(table: &Descriptors, fd: u32) -> Answer {
246
- refuse_stream(table, fd)?;
258
+ pub(crate) fn fdstat_set_rights(wasi: &Wasi, fd: u32) -> Answer {
259
+ refuse_stream(wasi, fd)?;
247
260
  Err(Errno::Notsup.into())
248
261
  }
249
262
 
250
- pub(crate) fn filestat_get(table: &Descriptors, fd: u32) -> Answer<types::Filestat> {
251
- Ok(to_filestat(table, descriptor(table, fd)?.file.getattr()?))
263
+ pub(crate) fn filestat_get(wasi: &Wasi, fd: u32) -> Answer<types::Filestat> {
264
+ let stat = get_raw_file(wasi, fd)?.getattr()?;
265
+ to_filestat(wasi.cred(), stat)
252
266
  }
253
267
 
254
- /// A size Linux's signed `loff_t` reads as negative answers before the
255
- /// descriptor is looked for, as `ksys_ftruncate` checks it before `fdget`.
256
- pub(crate) fn filestat_set_size(table: &mut Descriptors, fd: u32, size: u64) -> Answer {
257
- if i64::try_from(size).is_err() {
258
- return Err(Errno::Inval.into());
268
+ /// Refuse a size or offset Linux's signed `loff_t` reads as negative, before
269
+ /// the descriptor is looked for, as `ksys_ftruncate`, `ksys_pread64`, and
270
+ /// `ksys_pwrite64` check it before `fdget`.
271
+ fn ensure_offset(at: u64) -> Answer {
272
+ match at > vfs::OFFSET_MAX {
273
+ true => Err(Errno::Inval.into()),
274
+ false => Ok(()),
259
275
  }
260
- Ok(file_mut(table, fd)?.truncate(size)?)
261
276
  }
262
277
 
263
- pub(crate) fn filestat_set_times(table: &Descriptors, fd: u32, times: &Iattr) -> Answer {
264
- let file = file(table, fd)?;
265
- Ok(retry_eagain(|| {
266
- table.walk_guard().check(file.path())?;
267
- vfs::do_utimes_fd(table.cred(), file.path(), times)
268
- })?)
278
+ pub(crate) fn filestat_set_size(wasi: &mut Wasi, fd: u32, size: u64) -> Answer {
279
+ ensure_offset(size)?;
280
+ Ok(get_file_mut(wasi, fd)?.truncate(size)?)
281
+ }
282
+
283
+ pub(crate) fn filestat_set_times(wasi: &Wasi, fd: u32, times: &Iattr) -> Answer {
284
+ let file = get_file(wasi, fd)?;
285
+ Ok(filesystem::set_times(
286
+ wasi.cred(),
287
+ wasi.walk_guard(),
288
+ &file,
289
+ times,
290
+ )?)
269
291
  }
270
292
 
271
293
  pub(crate) fn pread(
272
- table: &mut Descriptors,
294
+ wasi: &mut Wasi,
273
295
  mem: &mut GuestMemory<'_, '_>,
274
296
  fd: u32,
275
297
  iovs: types::IovecArray,
276
298
  offset: u64,
277
299
  ) -> Answer<types::Size> {
278
- let buf = mem.first_non_empty_iovec(iovs)?;
279
- let file = file_mut(table, fd)?;
300
+ ensure_offset(offset)?;
301
+ let buf = mem.read_first_non_empty_iovec(iovs)?;
302
+ let mut file = get_file_mut(wasi, fd)?;
280
303
  file.may_pread(offset)?;
281
304
  Ok(file.pread(mem.bytes_mut(buf)?, offset)? as u32)
282
305
  }
283
306
 
284
- pub(crate) fn prestat_get(table: &Descriptors, fd: u32) -> Answer<types::Prestat> {
285
- match &descriptor(table, fd)?.preopen_path {
307
+ pub(crate) fn prestat_get(wasi: &Wasi, fd: u32) -> Answer<types::Prestat> {
308
+ match &get_descriptor(wasi, fd)?.preopen_path {
286
309
  Some(at) => Ok(types::Prestat::Dir(types::PrestatDir {
287
310
  pr_name_len: at.len() as u32,
288
311
  })),
@@ -291,13 +314,13 @@ pub(crate) fn prestat_get(table: &Descriptors, fd: u32) -> Answer<types::Prestat
291
314
  }
292
315
 
293
316
  pub(crate) fn prestat_dir_name(
294
- table: &Descriptors,
317
+ wasi: &Wasi,
295
318
  mem: &mut GuestMemory<'_, '_>,
296
319
  fd: u32,
297
320
  path: GuestPtr<u8>,
298
321
  path_len: u32,
299
322
  ) -> Answer {
300
- match &descriptor(table, fd)?.preopen_path {
323
+ match &get_descriptor(wasi, fd)?.preopen_path {
301
324
  Some(at) => {
302
325
  let name = at.as_bytes();
303
326
  if name.len() > path_len as usize {
@@ -310,26 +333,27 @@ pub(crate) fn prestat_dir_name(
310
333
  }
311
334
 
312
335
  pub(crate) fn pwrite(
313
- table: &mut Descriptors,
336
+ wasi: &mut Wasi,
314
337
  mem: &mut GuestMemory<'_, '_>,
315
338
  fd: u32,
316
339
  ciovs: types::CiovecArray,
317
340
  offset: u64,
318
341
  ) -> Answer<types::Size> {
319
- let buf = mem.first_non_empty_ciovec(ciovs)?;
320
- let file = file_mut(table, fd)?;
342
+ ensure_offset(offset)?;
343
+ let buf = mem.read_first_non_empty_ciovec(ciovs)?;
344
+ let mut file = get_file_mut(wasi, fd)?;
321
345
  file.may_pwrite(offset)?;
322
346
  Ok(file.pwrite(mem.bytes(buf)?, offset)? as u32)
323
347
  }
324
348
 
325
349
  pub(crate) fn read(
326
- table: &mut Descriptors,
350
+ wasi: &mut Wasi,
327
351
  mem: &mut GuestMemory<'_, '_>,
328
352
  fd: u32,
329
353
  iovs: types::IovecArray,
330
354
  ) -> Answer<types::Size> {
331
- let buf = mem.first_non_empty_iovec(iovs)?;
332
- let file = file_mut(table, fd)?;
355
+ let buf = mem.read_first_non_empty_iovec(iovs)?;
356
+ let mut file = get_file_mut(wasi, fd)?;
333
357
  file.may_read()?;
334
358
  // Bounded before the file is asked, as a trap takes no bytes from a pipe.
335
359
  Ok(file.read(mem.bytes_mut(buf)?)? as u32)
@@ -357,12 +381,12 @@ fn write_dirent(
357
381
  mem: &mut GuestMemory<'_, '_>,
358
382
  buf: (GuestPtr<u8>, u32),
359
383
  used: &mut u32,
360
- profile: Profile,
384
+ cred: &Cred,
361
385
  emit: &DirEmit<'_>,
362
386
  ) -> Answer<Option<u32>> {
363
387
  let (start, capacity) = buf;
364
388
  let at = *used;
365
- let header = to_dirent_header(emit, profile.guest_ino(emit.identity))?;
389
+ let header = to_dirent_header(emit, vfs::ino_of(cred, emit.identity)?)?;
366
390
  let mut whole = true;
367
391
  for part in [&header[..], emit.name.as_bytes()] {
368
392
  let room = (capacity - *used) as usize;
@@ -392,20 +416,20 @@ fn write_d_next(
392
416
  /// A directory's entries from the `cookie`th on, cut off where the buffer
393
417
  /// ends.
394
418
  pub(crate) fn readdir(
395
- table: &mut Descriptors,
419
+ wasi: &mut Wasi,
396
420
  mem: &mut GuestMemory<'_, '_>,
397
421
  fd: u32,
398
422
  start: GuestPtr<u8>,
399
423
  capacity: u32,
400
424
  cookie: u64,
401
425
  ) -> Answer<types::Size> {
402
- let profile = table.profile();
403
- let dir = file_mut(table, fd)?;
426
+ let cred = wasi.cred().clone();
427
+ let mut dir = get_file_mut(wasi, fd)?;
404
428
  // preview1 names where a listing goes on by its cookie, as wasi-libc's
405
429
  // `seekdir` moves a directory's position before `getdents`; a listing going
406
430
  // on from where the last one stopped needs no seek.
407
431
  if cookie != dir.pos() {
408
- let cookie = i64::try_from(cookie).map_err(|_| FilesystemError::InvalidInput)?;
432
+ let cookie = i64::try_from(cookie).map_err(|_| kobako_runtime::Errno::InvalidInput)?;
409
433
  dir.llseek(cookie, Whence::Set)?;
410
434
  }
411
435
  let mut used: u32 = 0;
@@ -418,7 +442,7 @@ pub(crate) fn readdir(
418
442
  Some(at) => write_d_next(mem, start, at, emit.offset),
419
443
  None => Ok(()),
420
444
  })
421
- .and_then(|()| write_dirent(mem, (start, capacity), &mut used, profile, emit));
445
+ .and_then(|()| write_dirent(mem, (start, capacity), &mut used, &cred, emit));
422
446
  match written {
423
447
  Ok(whole) => {
424
448
  last_whole = whole;
@@ -448,46 +472,44 @@ pub(crate) fn readdir(
448
472
  /// preview1 renumbering moves `from` onto `to`, both already open, then
449
473
  /// closes what stood at `to`, ignoring how its close answers, as Linux's
450
474
  /// `do_dup2` installs the file before `filp_close` of the one it replaced.
451
- pub(crate) fn renumber(table: &mut Descriptors, from: u32, to: u32) -> Answer {
452
- if table.get(from).is_none() || table.get(to).is_none() {
475
+ pub(crate) fn renumber(wasi: &mut Wasi, from: u32, to: u32) -> Answer {
476
+ if wasi.descriptors().get(from).is_none() || wasi.descriptors().get(to).is_none() {
453
477
  return Err(Errno::Badf.into());
454
478
  }
455
479
  if from == to {
456
480
  return Ok(());
457
481
  }
458
- let moved = table.remove(from).expect("found open above");
459
- let replaced = table.remove(to).expect("found open above");
460
- table.place(to, moved);
461
- let _ = replaced.file.close();
482
+ let replaced = wasi.take(to).expect("found open above");
483
+ let descriptors = wasi.descriptors_mut();
484
+ let moved = descriptors.remove(from).expect("found open above");
485
+ descriptors.place(to, moved);
486
+ if let Some(file) = replaced {
487
+ let _ = file.close();
488
+ }
462
489
  Ok(())
463
490
  }
464
491
 
465
- pub(crate) fn seek(
466
- table: &mut Descriptors,
467
- fd: u32,
468
- offset: i64,
469
- whence: types::Whence,
470
- ) -> Answer<u64> {
492
+ pub(crate) fn seek(wasi: &mut Wasi, fd: u32, offset: i64, whence: types::Whence) -> Answer<u64> {
471
493
  let whence = match whence {
472
494
  types::Whence::Set => Whence::Set,
473
495
  types::Whence::Cur => Whence::Cur,
474
496
  types::Whence::End => Whence::End,
475
497
  };
476
- Ok(seekable_file(table, fd)?.llseek(offset, whence)?)
498
+ Ok(get_seekable(wasi, fd)?.llseek(offset, whence)?)
477
499
  }
478
500
 
479
- pub(crate) fn tell(table: &mut Descriptors, fd: u32) -> Answer<u64> {
480
- Ok(seekable_file(table, fd)?.llseek(0, Whence::Cur)?)
501
+ pub(crate) fn tell(wasi: &mut Wasi, fd: u32) -> Answer<u64> {
502
+ seek(wasi, fd, 0, types::Whence::Cur)
481
503
  }
482
504
 
483
505
  pub(crate) fn write(
484
- table: &mut Descriptors,
506
+ wasi: &mut Wasi,
485
507
  mem: &mut GuestMemory<'_, '_>,
486
508
  fd: u32,
487
509
  ciovs: types::CiovecArray,
488
510
  ) -> Answer<types::Size> {
489
- let buf = mem.first_non_empty_ciovec(ciovs)?;
490
- let file = file_mut(table, fd)?;
511
+ let buf = mem.read_first_non_empty_ciovec(ciovs)?;
512
+ let mut file = get_file_mut(wasi, fd)?;
491
513
  file.may_write()?;
492
514
  Ok(file.write(mem.bytes(buf)?)? as u32)
493
515
  }
@@ -531,24 +553,22 @@ mod tests {
531
553
 
532
554
  /// `fd_filestat_set_times` of `fd` setting both times to zero as `flags`
533
555
  /// ask, as the dispatcher answers it.
534
- fn set_times(table: &Descriptors, fd: u32, flags: Fstflags) -> Answer {
535
- utimensat(0, 0, flags, |times| filestat_set_times(table, fd, times))
556
+ fn set_times(wasi: &Wasi, fd: u32, flags: Fstflags) -> Answer {
557
+ utimensat(0, 0, flags, |times| filestat_set_times(wasi, fd, times))
536
558
  }
537
- use crate::descriptors::{Descriptor, Descriptors};
538
559
  use crate::preview1::memory::GuestMemory;
539
560
  use crate::preview1::path::{
540
561
  create_directory, filestat_set_times as path_set_times, open as path_open, readlink,
541
562
  rename, symlink, PathOpen,
542
563
  };
543
564
  use crate::preview1::types::{self, Rights, Whence};
544
- use kobako_runtime::filesystem::{create_pipe_files, PipeInodeInfo};
565
+ use crate::wasi::Wasi;
545
566
  use kobako_runtime::filesystem::{
546
- FileOperations, FileType, Filesystem, FilesystemError, InodeOperations, Kstat, Lookup,
567
+ FileOperations, FileType, Filesystem, InodeOperations, Kstat, Lookup,
547
568
  };
548
569
  use kobako_runtime::mount::{Access, Mount};
549
- use kobako_runtime::profile::Profile;
550
- use kobako_runtime::security::prepare_creds_within;
551
- use kobako_runtime::vfs::{self, Dentry, Inode, OpenHow, Path, PATH_MAX};
570
+ use kobako_runtime::security::prepare_creds;
571
+ use kobako_runtime::vfs::{self, Dentry, File, Inode, OpenHow, Path, PATH_MAX};
552
572
  use wiggle::{GuestPtr, GuestType};
553
573
 
554
574
  /// What `do_file_open` takes for an open `how` asks.
@@ -556,25 +576,15 @@ mod tests {
556
576
  vfs::build_open_flags(how).expect("the flags must mean an open")
557
577
  }
558
578
 
559
- /// A table holding the three standard streams and `mounts`, at most `limit`
560
- /// descriptors open, as a Driver makes one.
561
- fn table_of(mounts: Vec<Mount>, limit: Option<usize>) -> Result<Descriptors, FilesystemError> {
562
- let cred = prepare_creds_within(&[], None);
563
- let ends = |pipe| create_pipe_files(&pipe, &cred);
564
- Descriptors::new(
565
- ends(PipeInodeInfo::from_bytes(Vec::new())).0,
566
- ends(PipeInodeInfo::new(1)).1,
567
- ends(PipeInodeInfo::new(1)).1,
568
- mounts,
569
- limit,
570
- cred.clone(),
571
- Profile::Hermetic,
572
- )
579
+ /// A context holding the three standard streams and `mounts`, at most
580
+ /// `limit` descriptors open, as a Driver makes one.
581
+ fn wasi_of(mounts: Vec<Mount>, limit: Option<usize>) -> Wasi {
582
+ Wasi::with_mounts(mounts, limit).unwrap_or_else(|_| panic!("the context must make"))
573
583
  }
574
584
 
575
- /// A table holding only the three standard streams.
576
- fn standard_streams() -> Descriptors {
577
- table_of(Vec::new(), None).expect("a table holding no Mount must make")
585
+ /// A context holding only the three standard streams.
586
+ fn standard_streams() -> Wasi {
587
+ wasi_of(Vec::new(), None)
578
588
  }
579
589
 
580
590
  /// The code a call answered, if it answered one.
@@ -582,16 +592,39 @@ mod tests {
582
592
  answer.err().and_then(|err| err.downcast().ok())
583
593
  }
584
594
 
595
+ // @behavior RT-401
596
+ #[test]
597
+ fn a_standard_streams_descriptor_holds_its_pipe_apart_from_preview2s_handles() {
598
+ use crate::invocation::Invocation;
599
+ use crate::preview2::bindings::wasi_io_streams_0_2_12::{InputStream, OutputStream};
600
+ use crate::preview2::bindings::IoHost as _;
601
+
602
+ let config = crate::config::Config::for_profile(kobako_runtime::profile::Profile::Hermetic);
603
+ let mut guest = Invocation::new(Wasi::with_stdin(&config, b"hi".to_vec()));
604
+ let read = guest.streams_input_stream_read(InputStream(1), 16).ok();
605
+ let dropped_while_open = guest.streams_output_stream_drop(OutputStream(2)).is_ok();
606
+ let closed = errno(super::close(guest.wasi_mut(), 1));
607
+ let dropped_once_closed = guest.streams_output_stream_drop(OutputStream(2)).is_ok();
608
+ assert_eq!(
609
+ (read, dropped_while_open, closed, dropped_once_closed),
610
+ (Some(b"hi".to_vec()), false, None, true),
611
+ "stdin's bytes must read through preview2's input-stream under handle 1, as \
612
+ wasi:cli's get-stdin hands it; dropping stdout's output-stream must trap while \
613
+ descriptor 1 stands over it, as the Canonical ABI traps a drop of a lent handle, and \
614
+ succeed once fd_close closed descriptor 1"
615
+ );
616
+ }
617
+
585
618
  // @behavior RT-177
586
619
  #[test]
587
620
  fn a_standard_stream_answers_as_a_pipe_does() {
588
- let mut table = standard_streams();
621
+ let mut wasi = standard_streams();
589
622
  assert_eq!(
590
623
  [
591
- errno(sync(&mut table, 1, false)),
592
- errno(filestat_set_size(&mut table, 1, 0)),
593
- errno(tell(&mut table, 0)),
594
- errno(tell(&mut table, 1)),
624
+ errno(sync(&mut wasi, 1, false)),
625
+ errno(filestat_set_size(&mut wasi, 1, 0)),
626
+ errno(tell(&mut wasi, 0)),
627
+ errno(tell(&mut wasi, 1)),
595
628
  ],
596
629
  [
597
630
  Some(Errno::Inval),
@@ -608,8 +641,8 @@ mod tests {
608
641
  struct RefusesClose;
609
642
 
610
643
  impl FileOperations for RefusesClose {
611
- fn flush(&mut self) -> Result<(), FilesystemError> {
612
- Err(FilesystemError::PermissionDenied)
644
+ fn flush(&mut self) -> Result<(), kobako_runtime::Errno> {
645
+ Err(kobako_runtime::Errno::PermissionDenied)
613
646
  }
614
647
  }
615
648
 
@@ -630,7 +663,7 @@ mod tests {
630
663
  const HOLDS_REFUSES_CLOSE: Holds = Holds(FileType::RegFile, || Box::new(RefusesClose));
631
664
 
632
665
  impl kobako_runtime::filesystem::FileSystemType for Holds {
633
- fn fill_super(self: Arc<Self>, sb: &mut Filesystem) -> Result<(), FilesystemError> {
666
+ fn fill_super(self: Arc<Self>, sb: &mut Filesystem) -> Result<(), kobako_runtime::Errno> {
634
667
  sb.d_make_root(kobako_runtime::vfs::Inode::new(
635
668
  &kobako_runtime::Kstat::new(kobako_runtime::FileType::Dir, 0),
636
669
  self,
@@ -640,11 +673,11 @@ mod tests {
640
673
  }
641
674
 
642
675
  impl InodeOperations for Holds {
643
- fn getattr(&self, _dentry: &Dentry) -> Result<Kstat, FilesystemError> {
676
+ fn getattr(&self, _dentry: &Dentry) -> Result<Kstat, kobako_runtime::Errno> {
644
677
  Ok(Kstat::new(FileType::Dir, 0))
645
678
  }
646
679
 
647
- fn lookup(&self, _dentry: &Dentry) -> Result<Lookup, FilesystemError> {
680
+ fn lookup(&self, _dentry: &Dentry) -> Result<Lookup, kobako_runtime::Errno> {
648
681
  let found = Kstat::new(self.0, 0);
649
682
  Ok(Lookup::Found(Inode::new(
650
683
  &found,
@@ -656,37 +689,30 @@ mod tests {
656
689
  &self,
657
690
  _dentry: &Dentry,
658
691
  _options: &OpenHow,
659
- ) -> Result<Box<dyn FileOperations>, FilesystemError> {
692
+ ) -> Result<Box<dyn FileOperations>, kobako_runtime::Errno> {
660
693
  Ok((self.1)())
661
694
  }
662
695
  }
663
696
 
664
697
  /// The file `holds` holds, opened as `how` asks beneath a Mount that
665
698
  /// writes when `how` does.
666
- fn open_held(holds: Holds, how: &OpenHow) -> Descriptor {
699
+ fn open_held(holds: Holds, how: &OpenHow) -> File {
667
700
  let access = match how.write {
668
701
  true => Access::ReadWrite,
669
702
  false => Access::Read,
670
703
  };
671
704
  let mount = Mount::new(
672
- "/data",
673
- Filesystem::new(Arc::new(holds)).expect("the instance must stand"),
705
+ "/",
706
+ Filesystem::new(holds).expect("the instance must stand"),
674
707
  access,
675
708
  )
676
- .expect("/data is a guest path");
709
+ .expect("/ is a guest path");
677
710
  let start = Path::root(mount.mnt());
678
- Descriptor::for_file(
679
- vfs::do_file_open(
680
- &prepare_creds_within(&[], None),
681
- &start,
682
- "file",
683
- &flags_of(how),
684
- )
685
- .unwrap_or_else(|_| panic!("the file must open")),
686
- )
711
+ vfs::do_file_open(&prepare_creds(&[], None), &start, "file", &flags_of(how))
712
+ .unwrap_or_else(|_| panic!("the file must open"))
687
713
  }
688
714
 
689
- fn open_file() -> Descriptor {
715
+ fn open_file() -> File {
690
716
  let mut how = OpenHow::default();
691
717
  how.read = true;
692
718
  open_held(HOLDS_REFUSES_CLOSE, &how)
@@ -695,10 +721,10 @@ mod tests {
695
721
  // @behavior RT-176
696
722
  #[test]
697
723
  fn a_file_without_fsync_refuses_a_sync() {
698
- let mut table = standard_streams();
699
- let fd = table.push(open_file()).expect("a descriptor to sync");
724
+ let mut wasi = standard_streams();
725
+ let fd = wasi.push_file(open_file()).expect("a descriptor to sync");
700
726
  assert_eq!(
701
- errno(sync(&mut table, fd, false)),
727
+ errno(sync(&mut wasi, fd, false)),
702
728
  Some(Errno::Inval),
703
729
  "fd_sync on a file whose Filesystem has no fsync must answer EINVAL"
704
730
  );
@@ -707,19 +733,19 @@ mod tests {
707
733
  // @behavior RT-129
708
734
  #[test]
709
735
  fn a_file_set_not_to_block_reports_it() {
710
- let mut table = standard_streams();
711
- let fd = table.push(open_file()).expect("a descriptor to set");
712
- let flags = |table: &Descriptors| {
713
- fdstat_get(table, fd)
736
+ let mut wasi = standard_streams();
737
+ let fd = wasi.push_file(open_file()).expect("a descriptor to set");
738
+ let flags = |wasi: &Wasi| {
739
+ fdstat_get(wasi, fd)
714
740
  .unwrap_or_else(|_| panic!("the file must report its flags"))
715
741
  .fs_flags
716
742
  };
717
743
  assert!(
718
- fdstat_set_flags(&mut table, fd, Fdflags::NONBLOCK | Fdflags::SYNC).is_ok(),
744
+ fdstat_set_flags(&mut wasi, fd, Fdflags::NONBLOCK | Fdflags::SYNC).is_ok(),
719
745
  "fd_fdstat_set_flags asking not to block and to sync must succeed, as Linux's setfl"
720
746
  );
721
747
  assert_eq!(
722
- flags(&table),
748
+ flags(&wasi),
723
749
  Fdflags::NONBLOCK,
724
750
  "fd_fdstat_get after fd_fdstat_set_flags must report not blocking and, as Linux's \
725
751
  setfl ignores it, no sync"
@@ -731,21 +757,21 @@ mod tests {
731
757
  // @behavior RT-275
732
758
  #[test]
733
759
  fn a_stream_reports_what_it_was_opened_and_set_for() {
734
- let mut table = standard_streams();
760
+ let mut wasi = standard_streams();
735
761
  let mut how = OpenHow::default();
736
762
  how.read = true;
737
763
  how.write = true;
738
- let fd = table
739
- .push(open_held(
764
+ let fd = wasi
765
+ .push_file(open_held(
740
766
  Holds(FileType::RegFile, || Box::new(Stream)),
741
767
  &how,
742
768
  ))
743
769
  .expect("a descriptor to set");
744
770
  assert!(
745
- fdstat_set_flags(&mut table, fd, Fdflags::APPEND | Fdflags::NONBLOCK).is_ok(),
771
+ fdstat_set_flags(&mut wasi, fd, Fdflags::APPEND | Fdflags::NONBLOCK).is_ok(),
746
772
  "fd_fdstat_set_flags asking a stream to append and not to block must succeed"
747
773
  );
748
- let stat = fdstat_get(&table, fd).unwrap_or_else(|_| panic!("the stream must report"));
774
+ let stat = fdstat_get(&wasi, fd).unwrap_or_else(|_| panic!("the stream must report"));
749
775
  assert_eq!(
750
776
  (
751
777
  stat.fs_flags,
@@ -761,15 +787,15 @@ mod tests {
761
787
  // @behavior RT-276
762
788
  #[test]
763
789
  fn a_descriptor_reports_the_kind_its_file_is() {
764
- let mut table = standard_streams();
765
- let fd = table
766
- .push(open_held(
790
+ let mut wasi = standard_streams();
791
+ let fd = wasi
792
+ .push_file(open_held(
767
793
  Holds(FileType::Chrdev, || Box::new(RefusesClose)),
768
794
  &OpenHow::default(),
769
795
  ))
770
796
  .expect("a descriptor to ask about");
771
797
  assert_eq!(
772
- fdstat_get(&table, fd)
798
+ fdstat_get(&wasi, fd)
773
799
  .unwrap_or_else(|_| panic!("the device must report"))
774
800
  .fs_filetype,
775
801
  types::Filetype::CharacterDevice,
@@ -784,17 +810,16 @@ mod tests {
784
810
  #[test]
785
811
  fn a_directory_refuses_a_call_about_its_position() {
786
812
  let mount = Mount::new(
787
- "/data",
788
- Filesystem::new(Arc::new(HOLDS_REFUSES_CLOSE)).expect("the instance must stand"),
813
+ "/",
814
+ Filesystem::new(HOLDS_REFUSES_CLOSE).expect("the instance must stand"),
789
815
  Access::Read,
790
816
  )
791
- .expect("/data is a guest path");
792
- let mut table =
793
- table_of(vec![mount], None).unwrap_or_else(|_| panic!("the table must make"));
817
+ .expect("/ is a guest path");
818
+ let mut wasi = wasi_of(vec![mount], None);
794
819
  assert_eq!(
795
820
  (
796
- errno(tell(&mut table, 3)),
797
- errno(seek(&mut table, 3, 0, Whence::Set)),
821
+ errno(tell(&mut wasi, 3)),
822
+ errno(seek(&mut wasi, 3, 0, Whence::Set)),
798
823
  ),
799
824
  (Some(Errno::Badf), Some(Errno::Badf)),
800
825
  "fd_tell and fd_seek on a directory through preview1 must answer EBADF, as \
@@ -802,43 +827,44 @@ mod tests {
802
827
  );
803
828
  }
804
829
 
805
- // @behavior RT-251
830
+ // @behavior RT-392
806
831
  #[test]
807
832
  fn a_descriptor_reports_how_far_it_was_opened_to_sync() {
808
833
  let tmpfs: kobako_runtime::filesystem::Tmpfs = [("f", "x")].into_iter().collect();
809
834
  let mount = Mount::new(
810
- "/data",
811
- Filesystem::new(Arc::new(tmpfs)).expect("the instance must stand"),
835
+ "/",
836
+ Filesystem::new(tmpfs).expect("the instance must stand"),
812
837
  Access::ReadWrite,
813
838
  )
814
- .expect("/data is a guest path");
815
- let root = Path::root(mount.mnt());
816
- let mut table =
817
- table_of(vec![mount], None).unwrap_or_else(|_| panic!("the table must make"));
818
- let cred = table.cred().clone();
819
- let mut reported = |dsync, sync| {
820
- let mut how = OpenHow::default();
821
- how.write = true;
822
- how.dsync = dsync;
823
- how.sync = sync;
824
- let opened = vfs::do_file_open(&cred, &root, "f", &flags_of(&how))
839
+ .expect("/ is a guest path");
840
+ let mut wasi = wasi_of(vec![mount], None);
841
+ let mut bytes = b"f".to_vec();
842
+ let mut memory = wiggle::GuestMemory::Unshared(&mut bytes);
843
+ let mem = GuestMemory::new(&mut memory, 64);
844
+ let mut reported = |fdflags| {
845
+ let call = PathOpen {
846
+ dirflags: types::Lookupflags::empty(),
847
+ oflags: types::Oflags::empty(),
848
+ rights_base: Rights::FD_WRITE,
849
+ fdflags,
850
+ };
851
+ let fd = path_open(&mut wasi, &mem, 3, GuestPtr::new((0, 1)), &call)
825
852
  .unwrap_or_else(|_| panic!("the file must open"));
826
- let fd = table
827
- .push(Descriptor::for_file(opened))
828
- .unwrap_or_else(|| panic!("the table must have room"));
829
- let Ok(stat) = fdstat_get(&table, fd) else {
853
+ let Ok(stat) = fdstat_get(&wasi, fd.into()) else {
830
854
  panic!("the descriptor must report itself");
831
855
  };
832
- (
833
- stat.fs_flags.contains(Fdflags::DSYNC),
834
- stat.fs_flags.contains(Fdflags::SYNC),
835
- )
856
+ stat.fs_flags & (Fdflags::DSYNC | Fdflags::SYNC | Fdflags::RSYNC)
836
857
  };
858
+ let every = Fdflags::DSYNC | Fdflags::SYNC | Fdflags::RSYNC;
837
859
  assert_eq!(
838
- (reported(true, false), reported(false, true)),
839
- ((true, false), (true, true)),
840
- "fd_fdstat_get must report DSYNC alone for a file opened O_DSYNC, and both for one \
841
- opened O_SYNC, whose bits hold O_DSYNC's as Linux's do"
860
+ [
861
+ reported(Fdflags::DSYNC),
862
+ reported(Fdflags::SYNC),
863
+ reported(Fdflags::RSYNC)
864
+ ],
865
+ [Fdflags::DSYNC, every, every],
866
+ "fd_fdstat_get must report DSYNC alone for a file path_open opened O_DSYNC, and DSYNC, \
867
+ SYNC, and RSYNC for one opened O_SYNC or O_RSYNC, as Linux's O_RSYNC is O_SYNC"
842
868
  );
843
869
  }
844
870
 
@@ -846,8 +872,8 @@ mod tests {
846
872
  #[derive(Default)]
847
873
  struct CountsAsks(std::sync::atomic::AtomicUsize);
848
874
 
849
- impl kobako_runtime::mount::Policy for CountsAsks {
850
- fn allows(&self, _path: &str, _access: Access) -> bool {
875
+ impl kobako_runtime::policy::Policy<kobako_runtime::mount::MountRequest<'_>> for CountsAsks {
876
+ fn allows(&self, _request: &kobako_runtime::mount::MountRequest<'_>) -> bool {
851
877
  self.0.fetch_add(1, std::sync::atomic::Ordering::SeqCst);
852
878
  true
853
879
  }
@@ -862,28 +888,27 @@ mod tests {
862
888
  let policy = Arc::new(CountsAsks::default());
863
889
  let tmpfs: kobako_runtime::filesystem::Tmpfs = [("f", "x")].into_iter().collect();
864
890
  let mount = Mount::new(
865
- "/data",
866
- Filesystem::new(Arc::new(tmpfs)).expect("the instance must stand"),
891
+ "/",
892
+ Filesystem::new(tmpfs).expect("the instance must stand"),
867
893
  Access::ReadWrite,
868
894
  )
869
- .expect("/data is a guest path")
895
+ .expect("/ is a guest path")
870
896
  .with_policy(policy.clone());
871
897
  let root = Path::root(mount.mnt());
872
- let mut table =
873
- table_of(vec![mount], None).unwrap_or_else(|_| panic!("the table must make"));
874
- let cred = table.cred().clone();
898
+ let mut wasi = wasi_of(vec![mount], None);
899
+ let cred = wasi.cred().clone();
875
900
  let mut read = OpenHow::default();
876
901
  read.read = true;
877
902
  let opened = vfs::do_file_open(&cred, &root, "f", &flags_of(&read))
878
903
  .unwrap_or_else(|_| panic!("the file must open"));
879
- let fd = table
880
- .push(Descriptor::for_file(opened))
904
+ let fd = wasi
905
+ .push_file(opened)
881
906
  .unwrap_or_else(|| panic!("the table must have room"));
882
907
  vfs::filename_unlinkat(&cred, &root, "f").unwrap_or_else(|_| panic!("the name must go"));
883
908
  policy.0.store(0, Ordering::SeqCst);
884
909
  assert_eq!(
885
910
  (
886
- errno(set_times(&table, fd, Fstflags::ATIM_NOW)),
911
+ errno(set_times(&wasi, fd, Fstflags::ATIM_NOW)),
887
912
  policy.0.load(Ordering::SeqCst)
888
913
  ),
889
914
  (Some(Errno::Noent), 0),
@@ -896,17 +921,17 @@ mod tests {
896
921
  #[test]
897
922
  fn setting_a_descriptors_times_answers_as_a_paths_do() {
898
923
  let mount = Mount::new(
899
- "/data",
900
- Filesystem::new(Arc::new(HOLDS_REFUSES_CLOSE)).expect("the instance must stand"),
924
+ "/",
925
+ Filesystem::new(HOLDS_REFUSES_CLOSE).expect("the instance must stand"),
901
926
  Access::Read,
902
927
  )
903
- .expect("/data is a guest path");
904
- let table = table_of(vec![mount], None).unwrap_or_else(|_| panic!("the table must make"));
928
+ .expect("/ is a guest path");
929
+ let wasi = wasi_of(vec![mount], None);
905
930
  assert_eq!(
906
931
  (
907
- errno(set_times(&table, 3, Fstflags::MTIM)),
908
- errno(set_times(&table, 3, Fstflags::empty())),
909
- errno(set_times(&table, 3, Fstflags::MTIM | Fstflags::MTIM_NOW)),
932
+ errno(set_times(&wasi, 3, Fstflags::MTIM)),
933
+ errno(set_times(&wasi, 3, Fstflags::empty())),
934
+ errno(set_times(&wasi, 3, Fstflags::MTIM | Fstflags::MTIM_NOW)),
910
935
  ),
911
936
  (Some(Errno::Rofs), None, Some(Errno::Inval)),
912
937
  "fd_filestat_set_times beneath a read-only Mount through preview1 must answer EROFS, \
@@ -917,13 +942,15 @@ mod tests {
917
942
  // @behavior RT-185
918
943
  #[test]
919
944
  fn a_path_call_starting_from_a_file_is_not_a_directory() {
920
- let mut table = standard_streams();
921
- let fd = table.push(open_file()).expect("a descriptor to start from");
945
+ let mut wasi = standard_streams();
946
+ let fd = wasi
947
+ .push_file(open_file())
948
+ .expect("a descriptor to start from");
922
949
  let mut bytes = *b"a";
923
950
  let mut memory = wiggle::GuestMemory::Unshared(&mut bytes);
924
951
  let mem = GuestMemory::new(&mut memory, 1);
925
952
  assert_eq!(
926
- errno(create_directory(&table, &mem, fd, GuestPtr::new((0, 1)))),
953
+ errno(create_directory(&wasi, &mem, fd, GuestPtr::new((0, 1)))),
927
954
  Some(Errno::Notdir),
928
955
  "path_create_directory starting from a file through preview1 must answer ENOTDIR, as \
929
956
  Linux refuses a walk starting there"
@@ -935,8 +962,10 @@ mod tests {
935
962
  // @behavior RT-225 RT-230 RT-231 RT-232
936
963
  #[test]
937
964
  fn a_call_no_walk_can_take_answers_before_where_it_starts() {
938
- let mut table = standard_streams();
939
- let fd = table.push(open_file()).expect("a descriptor to start from");
965
+ let mut wasi = standard_streams();
966
+ let fd = wasi
967
+ .push_file(open_file())
968
+ .expect("a descriptor to start from");
940
969
  let mut bytes = vec![b'a'; PATH_MAX];
941
970
  let mut memory = wiggle::GuestMemory::Unshared(&mut bytes);
942
971
  let mem = GuestMemory::new(&mut memory, 2 * PATH_MAX);
@@ -949,11 +978,11 @@ mod tests {
949
978
  let path = |len| GuestPtr::new((0, len));
950
979
  assert_eq!(
951
980
  (
952
- errno(path_open(&mut table, &mem, fd, path(1), &call)),
953
- errno(create_directory(&table, &mem, fd, path(0))),
954
- errno(create_directory(&table, &mem, fd, path(PATH_MAX as u32))),
981
+ errno(path_open(&mut wasi, &mem, fd, path(1), &call)),
982
+ errno(create_directory(&wasi, &mem, fd, path(0))),
983
+ errno(create_directory(&wasi, &mem, fd, path(PATH_MAX as u32))),
955
984
  errno(utimensat(0, 0, Fstflags::empty(), |times| path_set_times(
956
- &table,
985
+ &wasi,
957
986
  &mem,
958
987
  fd,
959
988
  types::Lookupflags::empty(),
@@ -978,7 +1007,7 @@ mod tests {
978
1007
  // @behavior RT-257 RT-258
979
1008
  #[test]
980
1009
  fn a_call_no_walk_can_take_answers_before_the_descriptor_it_names() {
981
- let mut table = standard_streams();
1010
+ let mut wasi = standard_streams();
982
1011
  let mut bytes = vec![b'a'; PATH_MAX];
983
1012
  let mut memory = wiggle::GuestMemory::Unshared(&mut bytes);
984
1013
  let mem = GuestMemory::new(&mut memory, 2 * PATH_MAX);
@@ -993,15 +1022,15 @@ mod tests {
993
1022
  let create_dir = call(types::Oflags::CREAT | types::Oflags::DIRECTORY);
994
1023
  assert_eq!(
995
1024
  (
996
- errno(path_open(&mut table, &mem, unopened, path(1), &create_dir)),
997
- errno(create_directory(&table, &mem, unopened, path(0))),
1025
+ errno(path_open(&mut wasi, &mem, unopened, path(1), &create_dir)),
1026
+ errno(create_directory(&wasi, &mem, unopened, path(0))),
998
1027
  errno(create_directory(
999
- &table,
1028
+ &wasi,
1000
1029
  &mem,
1001
1030
  unopened,
1002
1031
  path(PATH_MAX as u32)
1003
1032
  )),
1004
- errno(create_directory(&table, &mem, unopened, path(1))),
1033
+ errno(create_directory(&wasi, &mem, unopened, path(1))),
1005
1034
  ),
1006
1035
  (
1007
1036
  Some(Errno::Inval),
@@ -1020,7 +1049,7 @@ mod tests {
1020
1049
  // @behavior RT-286
1021
1050
  #[test]
1022
1051
  fn a_call_naming_two_paths_answers_for_its_first_before_its_second() {
1023
- let table = standard_streams();
1052
+ let wasi = standard_streams();
1024
1053
  let mut bytes = vec![b'a'; PATH_MAX];
1025
1054
  let mut memory = wiggle::GuestMemory::Unshared(&mut bytes);
1026
1055
  let mem = GuestMemory::new(&mut memory, 2 * PATH_MAX);
@@ -1029,12 +1058,12 @@ mod tests {
1029
1058
  assert_eq!(
1030
1059
  (
1031
1060
  errno(rename(
1032
- &table,
1061
+ &wasi,
1033
1062
  &mem,
1034
1063
  (stdout, path(1)),
1035
1064
  (stdout, path(PATH_MAX as u32))
1036
1065
  )),
1037
- errno(rename(&table, &mem, (stdout, path(1)), (unopened, path(1)))),
1066
+ errno(rename(&wasi, &mem, (stdout, path(1)), (unopened, path(1)))),
1038
1067
  ),
1039
1068
  (Some(Errno::Notdir), Some(Errno::Notdir)),
1040
1069
  "path_rename from standard output to a path past PATH_MAX, or to a descriptor \
@@ -1045,12 +1074,12 @@ mod tests {
1045
1074
  // @behavior RT-287
1046
1075
  #[test]
1047
1076
  fn an_absolute_path_answers_before_the_descriptor_it_names() {
1048
- let table = standard_streams();
1077
+ let wasi = standard_streams();
1049
1078
  let mut bytes = b"/a".to_vec();
1050
1079
  let mut memory = wiggle::GuestMemory::Unshared(&mut bytes);
1051
1080
  let mem = GuestMemory::new(&mut memory, 16);
1052
1081
  assert_eq!(
1053
- errno(create_directory(&table, &mem, 99, GuestPtr::new((0, 2)))),
1082
+ errno(create_directory(&wasi, &mem, 99, GuestPtr::new((0, 2)))),
1054
1083
  Some(Errno::Notcapable),
1055
1084
  "path_create_directory of an absolute path from a descriptor number naming nothing \
1056
1085
  through preview1 must answer ENOTCAPABLE, as path_init looks at no descriptor"
@@ -1060,13 +1089,13 @@ mod tests {
1060
1089
  // @behavior RT-277
1061
1090
  #[test]
1062
1091
  fn a_readlink_into_no_room_is_invalid() {
1063
- let table = standard_streams();
1092
+ let wasi = standard_streams();
1064
1093
  let mut bytes = vec![0u8; 8];
1065
1094
  let mut memory = wiggle::GuestMemory::Unshared(&mut bytes);
1066
1095
  let mut mem = GuestMemory::new(&mut memory, 16);
1067
1096
  assert_eq!(
1068
1097
  errno(readlink(
1069
- &table,
1098
+ &wasi,
1070
1099
  &mut mem,
1071
1100
  99,
1072
1101
  GuestPtr::new((0, 0)),
@@ -1087,14 +1116,16 @@ mod tests {
1087
1116
  // @behavior RT-289
1088
1117
  #[test]
1089
1118
  fn a_readlink_of_an_empty_path_answers_for_its_descriptor() {
1090
- let mut table = standard_streams();
1091
- let fd = table.push(open_file()).expect("a descriptor to start from");
1119
+ let mut wasi = standard_streams();
1120
+ let fd = wasi
1121
+ .push_file(open_file())
1122
+ .expect("a descriptor to start from");
1092
1123
  let mut bytes = vec![0u8; 8];
1093
1124
  let mut memory = wiggle::GuestMemory::Unshared(&mut bytes);
1094
1125
  let mut mem = GuestMemory::new(&mut memory, 16);
1095
1126
  let mut empty_from = |start| {
1096
1127
  errno(readlink(
1097
- &table,
1128
+ &wasi,
1098
1129
  &mut mem,
1099
1130
  start,
1100
1131
  GuestPtr::new((0, 0)),
@@ -1116,16 +1147,16 @@ mod tests {
1116
1147
  // @behavior RT-271
1117
1148
  #[test]
1118
1149
  fn a_call_asking_no_time_succeeds_whatever_descriptor_it_names() {
1119
- let table = standard_streams();
1150
+ let wasi = standard_streams();
1120
1151
  let mut bytes = *b"a";
1121
1152
  let mut memory = wiggle::GuestMemory::Unshared(&mut bytes);
1122
1153
  let mem = GuestMemory::new(&mut memory, 1);
1123
1154
  let unopened = 99;
1124
1155
  assert_eq!(
1125
1156
  (
1126
- errno(set_times(&table, unopened, Fstflags::empty())),
1157
+ errno(set_times(&wasi, unopened, Fstflags::empty())),
1127
1158
  errno(utimensat(0, 0, Fstflags::empty(), |times| path_set_times(
1128
- &table,
1159
+ &wasi,
1129
1160
  &mem,
1130
1161
  unopened,
1131
1162
  types::Lookupflags::empty(),
@@ -1144,7 +1175,7 @@ mod tests {
1144
1175
  // @behavior RT-259
1145
1176
  #[test]
1146
1177
  fn a_full_table_answers_after_the_flags_and_before_the_start() {
1147
- let mut table = table_of(Vec::new(), Some(3)).expect("a table holding no Mount must make");
1178
+ let mut wasi = wasi_of(Vec::new(), Some(3));
1148
1179
  let mut bytes = *b"a";
1149
1180
  let mut memory = wiggle::GuestMemory::Unshared(&mut bytes);
1150
1181
  let mem = GuestMemory::new(&mut memory, 1);
@@ -1159,14 +1190,14 @@ mod tests {
1159
1190
  assert_eq!(
1160
1191
  (
1161
1192
  errno(path_open(
1162
- &mut table,
1193
+ &mut wasi,
1163
1194
  &mem,
1164
1195
  unopened,
1165
1196
  path,
1166
1197
  &call(types::Oflags::CREAT | types::Oflags::DIRECTORY)
1167
1198
  )),
1168
1199
  errno(path_open(
1169
- &mut table,
1200
+ &mut wasi,
1170
1201
  &mem,
1171
1202
  unopened,
1172
1203
  path,
@@ -1183,7 +1214,7 @@ mod tests {
1183
1214
  struct ListsNames(&'static str);
1184
1215
 
1185
1216
  impl kobako_runtime::filesystem::FileSystemType for ListsNames {
1186
- fn fill_super(self: Arc<Self>, sb: &mut Filesystem) -> Result<(), FilesystemError> {
1217
+ fn fill_super(self: Arc<Self>, sb: &mut Filesystem) -> Result<(), kobako_runtime::Errno> {
1187
1218
  sb.d_make_root(kobako_runtime::vfs::Inode::new(
1188
1219
  &kobako_runtime::Kstat::new(kobako_runtime::FileType::Dir, 0),
1189
1220
  self,
@@ -1193,7 +1224,7 @@ mod tests {
1193
1224
  }
1194
1225
 
1195
1226
  impl InodeOperations for ListsNames {
1196
- fn getattr(&self, _dentry: &Dentry) -> Result<Kstat, FilesystemError> {
1227
+ fn getattr(&self, _dentry: &Dentry) -> Result<Kstat, kobako_runtime::Errno> {
1197
1228
  Ok(Kstat::new(FileType::Dir, 0))
1198
1229
  }
1199
1230
 
@@ -1201,22 +1232,26 @@ mod tests {
1201
1232
  &self,
1202
1233
  _dentry: &Dentry,
1203
1234
  _options: &OpenHow,
1204
- ) -> Result<Box<dyn FileOperations>, FilesystemError> {
1235
+ ) -> Result<Box<dyn FileOperations>, kobako_runtime::Errno> {
1205
1236
  Ok(Box::new(Self(self.0)))
1206
1237
  }
1207
1238
  }
1208
1239
 
1209
1240
  impl FileOperations for ListsNames {
1241
+ fn has_iterate_shared(&self) -> bool {
1242
+ true
1243
+ }
1244
+
1210
1245
  fn iterate_shared(
1211
1246
  &mut self,
1212
1247
  dentry: &Dentry,
1213
1248
  ctx: &mut vfs::DirContext<'_>,
1214
- ) -> Result<(), FilesystemError> {
1249
+ ) -> Result<(), kobako_runtime::Errno> {
1215
1250
  let entries = vec![
1216
1251
  vfs::DirEntry::new("ok", FileType::RegFile),
1217
1252
  vfs::DirEntry::new(self.0, FileType::RegFile),
1218
1253
  ];
1219
- kobako_runtime::filesystem::Rdir::default().iterate(dentry, ctx, || Ok(entries))
1254
+ kobako_runtime::filesystem::Rdir::default().iterate_shared(dentry, ctx, || Ok(entries))
1220
1255
  }
1221
1256
  }
1222
1257
 
@@ -1247,21 +1282,20 @@ mod tests {
1247
1282
  #[test]
1248
1283
  fn each_listed_entry_names_where_the_listing_goes_on() {
1249
1284
  let mount = Mount::new(
1250
- "/data",
1251
- Filesystem::new(Arc::new(ListsNames("bb"))).expect("the instance must stand"),
1285
+ "/",
1286
+ Filesystem::new(ListsNames("bb")).expect("the instance must stand"),
1252
1287
  Access::Read,
1253
1288
  )
1254
- .expect("/data is a guest path");
1255
- let mut table =
1256
- table_of(vec![mount], None).unwrap_or_else(|_| panic!("the table must make"));
1289
+ .expect("/ is a guest path");
1290
+ let mut wasi = wasi_of(vec![mount], None);
1257
1291
  let header = types::Dirent::guest_size();
1258
1292
  let dots = 2 * header + 3;
1259
1293
  let mut bytes = vec![0u8; 256];
1260
1294
  let used = {
1261
1295
  let mut memory = wiggle::GuestMemory::Unshared(&mut bytes);
1262
1296
  let mut mem = GuestMemory::new(&mut memory, 256);
1263
- let first = readdir(&mut table, &mut mem, 3, GuestPtr::new(0), dots, 0);
1264
- let rest = readdir(&mut table, &mut mem, 3, GuestPtr::new(128), 128, 2);
1297
+ let first = readdir(&mut wasi, &mut mem, 3, GuestPtr::new(0), dots, 0);
1298
+ let rest = readdir(&mut wasi, &mut mem, 3, GuestPtr::new(128), 128, 2);
1265
1299
  (first.ok(), rest.ok())
1266
1300
  };
1267
1301
  let entry = |name: &str, d_next| (name.to_string(), d_next);
@@ -1290,20 +1324,19 @@ mod tests {
1290
1324
  fn a_listing_stops_at_a_name_no_directory_can_hold() {
1291
1325
  let listed = |bad: &'static str| {
1292
1326
  let mount = Mount::new(
1293
- "/data",
1294
- Filesystem::new(Arc::new(ListsNames(bad))).expect("the instance must stand"),
1327
+ "/",
1328
+ Filesystem::new(ListsNames(bad)).expect("the instance must stand"),
1295
1329
  Access::Read,
1296
1330
  )
1297
- .expect("/data is a guest path");
1298
- let mut table =
1299
- table_of(vec![mount], None).unwrap_or_else(|_| panic!("the table must make"));
1331
+ .expect("/ is a guest path");
1332
+ let mut wasi = wasi_of(vec![mount], None);
1300
1333
  let mut bytes = vec![0u8; 256];
1301
1334
  let (first, next) = {
1302
1335
  let mut memory = wiggle::GuestMemory::Unshared(&mut bytes);
1303
1336
  let mut mem = GuestMemory::new(&mut memory, 256);
1304
1337
  let buf = GuestPtr::new(0);
1305
- let first = readdir(&mut table, &mut mem, 3, buf, 256, 0).ok();
1306
- (first, errno(readdir(&mut table, &mut mem, 3, buf, 256, 3)))
1338
+ let first = readdir(&mut wasi, &mut mem, 3, buf, 256, 0).ok();
1339
+ (first, errno(readdir(&mut wasi, &mut mem, 3, buf, 256, 3)))
1307
1340
  };
1308
1341
  let handed_over = bytes.windows(bad.len().max(1)).any(|w| w == bad.as_bytes());
1309
1342
  (
@@ -1325,7 +1358,7 @@ mod tests {
1325
1358
  struct MakesLinks;
1326
1359
 
1327
1360
  impl kobako_runtime::filesystem::FileSystemType for MakesLinks {
1328
- fn fill_super(self: Arc<Self>, sb: &mut Filesystem) -> Result<(), FilesystemError> {
1361
+ fn fill_super(self: Arc<Self>, sb: &mut Filesystem) -> Result<(), kobako_runtime::Errno> {
1329
1362
  sb.d_make_root(kobako_runtime::vfs::Inode::new(
1330
1363
  &kobako_runtime::Kstat::new(kobako_runtime::FileType::Dir, 0),
1331
1364
  self,
@@ -1335,15 +1368,15 @@ mod tests {
1335
1368
  }
1336
1369
 
1337
1370
  impl InodeOperations for MakesLinks {
1338
- fn getattr(&self, _dentry: &Dentry) -> Result<Kstat, FilesystemError> {
1371
+ fn getattr(&self, _dentry: &Dentry) -> Result<Kstat, kobako_runtime::Errno> {
1339
1372
  Ok(Kstat::new(FileType::Dir, 0))
1340
1373
  }
1341
1374
 
1342
- fn lookup(&self, _dentry: &Dentry) -> Result<Lookup, FilesystemError> {
1375
+ fn lookup(&self, _dentry: &Dentry) -> Result<Lookup, kobako_runtime::Errno> {
1343
1376
  Ok(Lookup::Absent)
1344
1377
  }
1345
1378
 
1346
- fn symlink(&self, _dentry: &Dentry, _target: &str) -> Result<(), FilesystemError> {
1379
+ fn symlink(&self, _dentry: &Dentry, _target: &str) -> Result<(), kobako_runtime::Errno> {
1347
1380
  Ok(())
1348
1381
  }
1349
1382
  }
@@ -1354,12 +1387,12 @@ mod tests {
1354
1387
  #[test]
1355
1388
  fn a_link_to_an_absolute_path_is_not_made() {
1356
1389
  let mount = Mount::new(
1357
- "/data",
1358
- Filesystem::new(Arc::new(MakesLinks)).expect("the instance must stand"),
1390
+ "/",
1391
+ Filesystem::new(MakesLinks).expect("the instance must stand"),
1359
1392
  Access::ReadWrite,
1360
1393
  )
1361
- .expect("/data is a guest path");
1362
- let table = table_of(vec![mount], None).unwrap_or_else(|_| panic!("the table must make"));
1394
+ .expect("/ is a guest path");
1395
+ let wasi = wasi_of(vec![mount], None);
1363
1396
  let mut bytes = *b"/trelative";
1364
1397
  let mut memory = wiggle::GuestMemory::Unshared(&mut bytes);
1365
1398
  let mem = GuestMemory::new(&mut memory, 64);
@@ -1370,8 +1403,8 @@ mod tests {
1370
1403
  );
1371
1404
  assert_eq!(
1372
1405
  (
1373
- errno(symlink(&table, &mem, absolute, 3, name)),
1374
- errno(symlink(&table, &mem, relative, 3, name)),
1406
+ errno(symlink(&wasi, &mem, absolute, 3, name)),
1407
+ errno(symlink(&wasi, &mem, relative, 3, name)),
1375
1408
  ),
1376
1409
  (Some(Errno::Perm), None),
1377
1410
  "path_symlink through preview1 to an absolute target must answer EPERM, as \
@@ -1384,14 +1417,16 @@ mod tests {
1384
1417
  // @behavior RT-114
1385
1418
  #[test]
1386
1419
  fn renumbering_onto_an_open_file_replaces_it_whatever_its_close_answers() {
1387
- let mut table = standard_streams();
1388
- let from = table.push(open_file()).expect("a descriptor to move");
1389
- let to = table.push(open_file()).expect("a descriptor to move onto");
1420
+ let mut wasi = standard_streams();
1421
+ let from = wasi.push_file(open_file()).expect("a descriptor to move");
1422
+ let to = wasi
1423
+ .push_file(open_file())
1424
+ .expect("a descriptor to move onto");
1390
1425
  assert_eq!(
1391
1426
  (
1392
- errno(renumber(&mut table, from, to)),
1393
- table.get(from).is_none(),
1394
- table.get(to).is_some()
1427
+ errno(renumber(&mut wasi, from, to)),
1428
+ wasi.descriptors().get(from).is_none(),
1429
+ wasi.descriptors().get(to).is_some()
1395
1430
  ),
1396
1431
  (None, true, true),
1397
1432
  "fd_renumber through preview1 onto a file whose Filesystem refuses the close must \
@@ -1403,14 +1438,14 @@ mod tests {
1403
1438
  // @behavior RT-309
1404
1439
  #[test]
1405
1440
  fn renumbering_onto_a_number_not_open_answers_ebadf_and_keeps_the_source() {
1406
- let mut table = standard_streams();
1407
- let from = table.push(open_file()).expect("a descriptor to move");
1441
+ let mut wasi = standard_streams();
1442
+ let from = wasi.push_file(open_file()).expect("a descriptor to move");
1408
1443
  let to = from + 1;
1409
1444
  assert_eq!(
1410
1445
  (
1411
- errno(renumber(&mut table, from, to)),
1412
- table.get(from).is_some(),
1413
- table.get(to).is_none()
1446
+ errno(renumber(&mut wasi, from, to)),
1447
+ wasi.descriptors().get(from).is_some(),
1448
+ wasi.descriptors().get(to).is_none()
1414
1449
  ),
1415
1450
  (Some(Errno::Badf), true, true),
1416
1451
  "fd_renumber through preview1 onto a number nothing stands at must answer EBADF and \