kobako 0.30.0 → 0.31.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (266) hide show
  1. checksums.yaml +4 -4
  2. data/.release-please-manifest.json +1 -1
  3. data/CHANGELOG.md +335 -0
  4. data/Cargo.lock +469 -11
  5. data/README.md +43 -3
  6. data/ROADMAP.md +1 -1
  7. data/crates/kobako-runtime/CHANGELOG.md +219 -0
  8. data/crates/kobako-runtime/Cargo.toml +51 -3
  9. data/crates/kobako-runtime/README.md +7 -4
  10. data/crates/kobako-runtime/src/cred.rs +124 -0
  11. data/crates/kobako-runtime/src/dispatch.rs +4 -5
  12. data/crates/kobako-runtime/src/endpoint/buffer.rs +153 -44
  13. data/crates/kobako-runtime/src/endpoint/dispatch.rs +80 -41
  14. data/crates/kobako-runtime/src/endpoint/invoke.rs +23 -36
  15. data/crates/kobako-runtime/src/endpoint.rs +137 -36
  16. data/crates/kobako-runtime/src/errno.rs +391 -0
  17. data/crates/kobako-runtime/src/error.rs +31 -10
  18. data/crates/kobako-runtime/src/filesystem/anon_inodes.rs +52 -0
  19. data/crates/kobako-runtime/src/filesystem/bind/lookup.rs +52 -14
  20. data/crates/kobako-runtime/src/filesystem/bind/walk.rs +19 -20
  21. data/crates/kobako-runtime/src/filesystem/bind.rs +437 -245
  22. data/crates/kobako-runtime/src/filesystem/overlay/file.rs +6 -6
  23. data/crates/kobako-runtime/src/filesystem/overlay/path.rs +43 -65
  24. data/crates/kobako-runtime/src/filesystem/overlay/stack/copy_up.rs +9 -16
  25. data/crates/kobako-runtime/src/filesystem/overlay/stack/dir.rs +47 -72
  26. data/crates/kobako-runtime/src/filesystem/overlay/stack/namei.rs +12 -12
  27. data/crates/kobako-runtime/src/filesystem/overlay/stack/readdir.rs +4 -7
  28. data/crates/kobako-runtime/src/filesystem/overlay/stack.rs +121 -37
  29. data/crates/kobako-runtime/src/filesystem/overlay.rs +302 -343
  30. data/crates/kobako-runtime/src/filesystem/pipe.rs +12 -9
  31. data/crates/kobako-runtime/src/filesystem/rdir.rs +15 -32
  32. data/crates/kobako-runtime/src/filesystem/tmpfs/seed.rs +32 -0
  33. data/crates/kobako-runtime/src/filesystem/tmpfs/tmpfile.rs +3 -3
  34. data/crates/kobako-runtime/src/filesystem/tmpfs/whiteout.rs +57 -35
  35. data/crates/kobako-runtime/src/filesystem/tmpfs/xattr.rs +2 -2
  36. data/crates/kobako-runtime/src/filesystem/tmpfs.rs +172 -160
  37. data/crates/kobako-runtime/src/filesystem/wrap.rs +36 -52
  38. data/crates/kobako-runtime/src/filesystem.rs +6 -3
  39. data/crates/kobako-runtime/src/fork.rs +96 -0
  40. data/crates/kobako-runtime/src/grant.rs +3 -0
  41. data/crates/kobako-runtime/src/hashtable.rs +4 -2
  42. data/crates/kobako-runtime/src/lib.rs +18 -7
  43. data/crates/kobako-runtime/src/limits.rs +195 -0
  44. data/crates/kobako-runtime/src/locking.rs +10 -0
  45. data/crates/kobako-runtime/src/memcg.rs +383 -0
  46. data/crates/kobako-runtime/src/mount.rs +67 -9
  47. data/crates/kobako-runtime/src/net/backends.rs +43 -0
  48. data/crates/kobako-runtime/src/net/connections.rs +179 -0
  49. data/crates/kobako-runtime/src/net/credential.rs +125 -0
  50. data/crates/kobako-runtime/src/net/egress.rs +160 -0
  51. data/crates/kobako-runtime/src/net/family.rs +181 -0
  52. data/crates/kobako-runtime/src/net/http.rs +2733 -0
  53. data/crates/kobako-runtime/src/net/lookup.rs +285 -0
  54. data/crates/kobako-runtime/src/net/mock.rs +251 -0
  55. data/crates/kobako-runtime/src/net/namespace.rs +1366 -0
  56. data/crates/kobako-runtime/src/net/reactor.rs +531 -0
  57. data/crates/kobako-runtime/src/net/resolv/darwin.rs +275 -0
  58. data/crates/kobako-runtime/src/net/resolv/stub/tests.rs +469 -0
  59. data/crates/kobako-runtime/src/net/resolv/stub.rs +536 -0
  60. data/crates/kobako-runtime/src/net/resolv/windows.rs +248 -0
  61. data/crates/kobako-runtime/src/net/resolv.rs +227 -0
  62. data/crates/kobako-runtime/src/net/socket.rs +870 -0
  63. data/crates/kobako-runtime/src/net/tcp.rs +1588 -0
  64. data/crates/kobako-runtime/src/net/tls.rs +58 -0
  65. data/crates/kobako-runtime/src/net/transport.rs +279 -0
  66. data/crates/kobako-runtime/src/net/udp.rs +1003 -0
  67. data/crates/kobako-runtime/src/net.rs +42 -0
  68. data/crates/kobako-runtime/src/network/builder.rs +177 -0
  69. data/crates/kobako-runtime/src/network/ruleset.rs +777 -0
  70. data/crates/kobako-runtime/src/network.rs +551 -0
  71. data/crates/kobako-runtime/src/policy.rs +16 -0
  72. data/crates/kobako-runtime/src/profile.rs +29 -144
  73. data/crates/kobako-runtime/src/runtime.rs +3 -5
  74. data/crates/kobako-runtime/src/sched.rs +130 -29
  75. data/crates/kobako-runtime/src/security/entry_policy.rs +22 -0
  76. data/crates/kobako-runtime/src/{vfs/cred.rs → security/hooks.rs} +52 -144
  77. data/crates/kobako-runtime/src/security/mount_policy.rs +158 -0
  78. data/crates/kobako-runtime/src/security.rs +101 -196
  79. data/crates/kobako-runtime/src/snapshot.rs +121 -2
  80. data/crates/kobako-runtime/src/vfs/attr.rs +5 -5
  81. data/crates/kobako-runtime/src/vfs/dentry.rs +143 -89
  82. data/crates/kobako-runtime/src/vfs/file.rs +199 -203
  83. data/crates/kobako-runtime/src/vfs/inode.rs +41 -45
  84. data/crates/kobako-runtime/src/vfs/kern_path.rs +38 -42
  85. data/crates/kobako-runtime/src/vfs/mount.rs +10 -9
  86. data/crates/kobako-runtime/src/vfs/namei.rs +829 -700
  87. data/crates/kobako-runtime/src/vfs/namespace.rs +162 -0
  88. data/crates/kobako-runtime/src/vfs/open_files.rs +69 -0
  89. data/crates/kobako-runtime/src/vfs/openat2.rs +3 -3
  90. data/crates/kobako-runtime/src/vfs/path.rs +6 -6
  91. data/crates/kobako-runtime/src/vfs/readdir.rs +6 -6
  92. data/crates/kobako-runtime/src/vfs/stat.rs +159 -23
  93. data/crates/kobako-runtime/src/vfs/super_block.rs +18 -14
  94. data/crates/kobako-runtime/src/vfs/utimes.rs +37 -27
  95. data/crates/kobako-runtime/src/vfs.rs +9 -6
  96. data/crates/kobako-runtime/src/wait.rs +356 -0
  97. data/crates/kobako-transport/CHANGELOG.md +31 -0
  98. data/crates/kobako-transport/Cargo.toml +1 -1
  99. data/crates/kobako-transport/README.md +3 -4
  100. data/crates/kobako-transport/src/abi.rs +78 -72
  101. data/crates/kobako-transport/src/envelope/bytes.rs +17 -6
  102. data/crates/kobako-transport/src/envelope/call.rs +32 -4
  103. data/crates/kobako-transport/src/envelope/error_record.rs +21 -0
  104. data/crates/kobako-transport/src/envelope/name.rs +99 -0
  105. data/crates/kobako-transport/src/envelope/outcome.rs +9 -9
  106. data/crates/kobako-transport/src/envelope/preamble.rs +24 -21
  107. data/crates/kobako-transport/src/envelope/reply.rs +27 -27
  108. data/crates/kobako-transport/src/envelope/unsupported.rs +10 -10
  109. data/crates/kobako-transport/src/envelope.rs +13 -8
  110. data/crates/kobako-wasmtime/CHANGELOG.md +161 -0
  111. data/crates/kobako-wasmtime/Cargo.toml +26 -7
  112. data/crates/kobako-wasmtime/README.md +3 -2
  113. data/crates/kobako-wasmtime/build/preview2.rs +1339 -0
  114. data/crates/kobako-wasmtime/build.rs +16 -0
  115. data/crates/kobako-wasmtime/src/abi.rs +17 -37
  116. data/crates/kobako-wasmtime/src/ambient.rs +33 -18
  117. data/crates/kobako-wasmtime/src/cache.rs +23 -15
  118. data/crates/kobako-wasmtime/src/config.rs +52 -68
  119. data/crates/kobako-wasmtime/src/driver.rs +48 -63
  120. data/crates/kobako-wasmtime/src/exports.rs +143 -17
  121. data/crates/kobako-wasmtime/src/guest.rs +12 -6
  122. data/crates/kobako-wasmtime/src/guest_binary.rs +14 -0
  123. data/crates/kobako-wasmtime/src/imports.rs +42 -18
  124. data/crates/kobako-wasmtime/src/instance_pre.rs +4 -4
  125. data/crates/kobako-wasmtime/src/invocation.rs +74 -12
  126. data/crates/kobako-wasmtime/src/lib.rs +5 -3
  127. data/crates/kobako-wasmtime/src/limiter.rs +70 -5
  128. data/crates/kobako-wasmtime/src/preview1/descriptors.rs +309 -0
  129. data/crates/kobako-wasmtime/src/preview1/differential/mount.rs +5 -15
  130. data/crates/kobako-wasmtime/src/preview1/differential.rs +27 -31
  131. data/crates/kobako-wasmtime/src/preview1/errno.rs +55 -66
  132. data/crates/kobako-wasmtime/src/preview1/fd.rs +327 -296
  133. data/crates/kobako-wasmtime/src/preview1/memory.rs +21 -12
  134. data/crates/kobako-wasmtime/src/preview1/mount_view/wrap.rs +34 -53
  135. data/crates/kobako-wasmtime/src/preview1/mount_view.rs +231 -217
  136. data/crates/kobako-wasmtime/src/preview1/path.rs +56 -121
  137. data/crates/kobako-wasmtime/src/preview1/poll.rs +244 -120
  138. data/crates/kobako-wasmtime/src/preview1/sock.rs +10 -8
  139. data/crates/kobako-wasmtime/src/preview1/testsuite.rs +5 -15
  140. data/crates/kobako-wasmtime/src/preview1.rs +46 -42
  141. data/crates/kobako-wasmtime/src/preview2/abi.rs +227 -0
  142. data/crates/kobako-wasmtime/src/preview2/error.rs +118 -0
  143. data/crates/kobako-wasmtime/src/preview2/filesystem.rs +122 -0
  144. data/crates/kobako-wasmtime/src/preview2/host.rs +3959 -0
  145. data/crates/kobako-wasmtime/src/preview2/http.rs +1503 -0
  146. data/crates/kobako-wasmtime/src/preview2/io.rs +315 -0
  147. data/crates/kobako-wasmtime/src/preview2/sockets.rs +379 -0
  148. data/crates/kobako-wasmtime/src/preview2/table.rs +305 -0
  149. data/crates/kobako-wasmtime/src/preview2.rs +57 -0
  150. data/crates/kobako-wasmtime/src/trap.rs +24 -9
  151. data/crates/kobako-wasmtime/src/wasi.rs +210 -81
  152. data/crates/kobako-wasmtime/wit/deps/cli.wit +233 -0
  153. data/crates/kobako-wasmtime/wit/deps/clocks.wit +162 -0
  154. data/crates/kobako-wasmtime/wit/deps/filesystem.wit +589 -0
  155. data/crates/kobako-wasmtime/wit/deps/http.wit +733 -0
  156. data/crates/kobako-wasmtime/wit/deps/io.wit +299 -0
  157. data/crates/kobako-wasmtime/wit/deps/random.wit +92 -0
  158. data/crates/kobako-wasmtime/wit/deps/sockets.wit +949 -0
  159. data/data/kobako.wasm +0 -0
  160. data/ext/kobako/Cargo.toml +1 -1
  161. data/ext/kobako/src/{runtime/errors.rs → errors.rs} +34 -26
  162. data/ext/kobako/src/{runtime/escape.rs → escape.rs} +42 -11
  163. data/ext/kobako/src/{runtime/filesystem → filesystem}/grants.rs +30 -26
  164. data/ext/kobako/src/{runtime/filesystem → filesystem}/ruby.rs +149 -168
  165. data/ext/kobako/src/{runtime/filesystem.rs → filesystem.rs} +24 -15
  166. data/ext/kobako/src/gvl.rs +306 -0
  167. data/ext/kobako/src/lib.rs +64 -3
  168. data/ext/kobako/src/network/mock.rs +187 -0
  169. data/ext/kobako/src/network.rs +591 -0
  170. data/ext/kobako/src/runtime/bridge.rs +30 -29
  171. data/ext/kobako/src/runtime/snapshot.rs +4 -4
  172. data/ext/kobako/src/runtime.rs +221 -143
  173. data/ext/kobako/src/wire.rs +42 -0
  174. data/lib/kobako/catalog/extensions.rb +14 -2
  175. data/lib/kobako/catalog/flags.rb +3 -5
  176. data/lib/kobako/catalog/grants.rb +5 -4
  177. data/lib/kobako/catalog/handles.rb +5 -27
  178. data/lib/kobako/catalog/mount.rb +11 -3
  179. data/lib/kobako/catalog/network.rb +35 -0
  180. data/lib/kobako/catalog/replacements.rb +28 -0
  181. data/lib/kobako/catalog/services.rb +5 -19
  182. data/lib/kobako/catalog/snippets.rb +3 -18
  183. data/lib/kobako/catalog.rb +3 -0
  184. data/lib/kobako/codec/{utils.rb → boundary.rb} +4 -2
  185. data/lib/kobako/codec/decoder.rb +4 -4
  186. data/lib/kobako/codec/ext_types.rb +3 -3
  187. data/lib/kobako/codec/handle_walk.rb +3 -43
  188. data/lib/kobako/codec/value_budget.rb +132 -40
  189. data/lib/kobako/codec.rb +1 -1
  190. data/lib/kobako/context.rb +24 -11
  191. data/lib/kobako/errors.rb +15 -12
  192. data/lib/kobako/extension.rb +3 -2
  193. data/lib/kobako/filesystem.rb +9 -2
  194. data/lib/kobako/handle.rb +2 -2
  195. data/lib/kobako/names.rb +28 -0
  196. data/lib/kobako/network.rb +210 -0
  197. data/lib/kobako/outcome.rb +7 -7
  198. data/lib/kobako/pool.rb +45 -23
  199. data/lib/kobako/runtime.rb +3 -2
  200. data/lib/kobako/sandbox.rb +92 -35
  201. data/lib/kobako/sandbox_options.rb +41 -106
  202. data/lib/kobako/transport/argument_wrap.rb +64 -0
  203. data/lib/kobako/transport/block_failure.rb +1 -1
  204. data/lib/kobako/transport/dispatcher.rb +6 -6
  205. data/lib/kobako/transport/exposure.rb +26 -15
  206. data/lib/kobako/transport/reflection.rb +21 -4
  207. data/lib/kobako/transport/run.rb +8 -23
  208. data/lib/kobako/transport/yielder.rb +2 -9
  209. data/lib/kobako/transport.rb +1 -0
  210. data/lib/kobako/version.rb +1 -1
  211. data/lib/kobako.rb +6 -2
  212. data/release-please-config.json +15 -0
  213. data/sig/core/thread.rbs +6 -0
  214. data/sig/kobako/catalog/extensions.rbs +2 -0
  215. data/sig/kobako/catalog/grants.rbs +7 -4
  216. data/sig/kobako/catalog/handles.rbs +0 -2
  217. data/sig/kobako/catalog/mount.rbs +3 -1
  218. data/sig/kobako/catalog/network.rbs +20 -0
  219. data/sig/kobako/catalog/replacements.rbs +14 -0
  220. data/sig/kobako/catalog/services.rbs +0 -4
  221. data/sig/kobako/catalog/snippets.rbs +0 -2
  222. data/sig/kobako/codec/{utils.rbs → boundary.rbs} +2 -2
  223. data/sig/kobako/codec/handle_walk.rbs +0 -6
  224. data/sig/kobako/codec/value_budget.rbs +47 -4
  225. data/sig/kobako/context.rbs +3 -1
  226. data/sig/kobako/errors.rbs +4 -4
  227. data/sig/kobako/filesystem.rbs +21 -3
  228. data/sig/kobako/network.rbs +115 -0
  229. data/sig/kobako/outcome.rbs +1 -1
  230. data/sig/kobako/pool.rbs +7 -3
  231. data/sig/kobako/runtime.rbs +32 -3
  232. data/sig/kobako/sandbox.rbs +19 -2
  233. data/sig/kobako/sandbox_options.rbs +29 -38
  234. data/sig/kobako/transport/argument_wrap.rbs +15 -0
  235. data/sig/kobako/transport/error.rbs +1 -1
  236. data/sig/kobako/transport/exposure.rbs +2 -4
  237. data/sig/kobako/transport/reflection.rbs +10 -0
  238. data/sig/kobako/transport/run.rbs +0 -2
  239. data/sig/kobako/transport/yielder.rbs +0 -3
  240. data/sig/kobako/transport.rbs +1 -1
  241. data/sig/kobako.rbs +13 -0
  242. metadata +84 -35
  243. data/crates/kobako-runtime/src/endpoint/capture.rs +0 -115
  244. data/crates/kobako-runtime/src/endpoint/memory_cap.rs +0 -238
  245. data/crates/kobako-runtime/src/filesystem/path.rs +0 -21
  246. data/crates/kobako-runtime/src/vfs/errno.rs +0 -153
  247. data/crates/kobako-runtime/tests/linux_baseline/calls.rs +0 -171
  248. data/crates/kobako-runtime/tests/linux_baseline/concurrent.rs +0 -276
  249. data/crates/kobako-runtime/tests/linux_baseline/kobako.rs +0 -323
  250. data/crates/kobako-runtime/tests/linux_baseline/linux-baseline-expected.txt +0 -16
  251. data/crates/kobako-runtime/tests/linux_baseline/linux.rs +0 -449
  252. data/crates/kobako-runtime/tests/linux_baseline/main.rs +0 -611
  253. data/crates/kobako-runtime/tests/linux_baseline/permissions.rs +0 -236
  254. data/crates/kobako-wasmtime/src/descriptors.rs +0 -200
  255. data/crates/kobako-wasmtime/tests/integration/compile_posture.rs +0 -43
  256. data/crates/kobako-wasmtime/tests/integration/guest_file.rs +0 -1085
  257. data/crates/kobako-wasmtime/tests/integration/main.rs +0 -29
  258. data/crates/kobako-wasmtime/tests/integration/preamble_flags.rs +0 -70
  259. data/crates/kobako-wasmtime/tests/integration/snippet_compile.rs +0 -95
  260. data/crates/kobako-wasmtime/tests/integration/support/mod.rs +0 -30
  261. data/crates/kobako-wasmtime/tests/integration/unanswered_call.rs +0 -86
  262. data/crates/kobako-wasmtime/tests/integration/unsupported_call.rs +0 -51
  263. data/ext/kobako/src/runtime/gvl.rs +0 -147
  264. data/lib/kobako/constant_name.rb +0 -8
  265. data/sig/kobako/constant_name.rbs +0 -3
  266. /data/ext/kobako/src/{runtime/pinned_value.rs → pinned_value.rs} +0 -0
@@ -2,12 +2,14 @@
2
2
  //!
3
3
  //! The run path produces these instead of constructing host-language
4
4
  //! exceptions directly; each frontend's boundary is the single place that
5
- //! maps them onto its own error classes (the Ruby ext does so in its error
6
- //! mapper). Keeping the types frontend-free lets any engine
7
- //! implementation produce them unchanged.
5
+ //! maps them onto its own error classes. Keeping the types frontend-free
6
+ //! lets any engine implementation produce them unchanged.
8
7
 
9
8
  use std::fmt;
10
9
 
10
+ use crate::mount::UnplacedMountError;
11
+ use crate::network::EntryError;
12
+
11
13
  /// A guest invocation that faulted in the wasm engine, or a host-detected
12
14
  /// runtime corruption during invocation, classified into the host-facing
13
15
  /// kinds a frontend surfaces distinctly: the wall-clock cap (`Timeout`),
@@ -27,7 +29,7 @@ pub enum Trap {
27
29
 
28
30
  /// A failure that yields no invocation outcome. The discriminant records
29
31
  /// the runtime's state so a frontend can attribute the failure:
30
- /// `ModuleNotBuilt` (the guest artifact is absent), `Dead` (the runtime
32
+ /// `GuestBinaryNotFound` (the guest artifact is absent), `Dead` (the runtime
31
33
  /// could not be constructed), and `Intact` (the runtime is live but a
32
34
  /// host-side pre-call step failed before the engine ran).
33
35
  ///
@@ -36,15 +38,17 @@ pub enum Trap {
36
38
  #[derive(Debug, Clone, PartialEq, Eq)]
37
39
  #[non_exhaustive]
38
40
  pub enum SetupError {
39
- ModuleNotBuilt(String),
41
+ GuestBinaryNotFound(String),
40
42
  Dead(String),
41
43
  Intact(String),
42
44
  }
43
45
 
44
46
  /// A failure that prevented the invocation from starting — a pre-call
45
- /// engine fault (`Trap`) or a host-side setup fault (`Setup`) — unified so
46
- /// the run mechanics can propagate both with `?`; a frontend destructures
47
- /// it back into the two. Faults after the guest export starts
47
+ /// engine fault (`Trap`), a host-side setup fault (`Setup`), or a network
48
+ /// entry whose endpoints the ruleset refused (`Entry`), which each frontend
49
+ /// answers as the Host App's argument error — unified so
50
+ /// the run mechanics can propagate each with `?`; a frontend destructures
51
+ /// it back into the three. Faults after the guest export starts
48
52
  /// ride in `Completion::Trap` instead, so captures and usage survive them.
49
53
  ///
50
54
  /// Named for where it comes from — the `Err` of `Runtime::invoke` — so a
@@ -57,6 +61,21 @@ pub enum SetupError {
57
61
  pub enum InvokeError {
58
62
  Trap(Trap),
59
63
  Setup(SetupError),
64
+ Entry(EntryError),
65
+ }
66
+
67
+ impl From<EntryError> for InvokeError {
68
+ fn from(err: EntryError) -> Self {
69
+ InvokeError::Entry(err)
70
+ }
71
+ }
72
+
73
+ /// A Mount Linux's `mount` refuses fails the run before the guest starts,
74
+ /// with the runtime still live, as a container fails to start.
75
+ impl From<UnplacedMountError> for InvokeError {
76
+ fn from(err: UnplacedMountError) -> Self {
77
+ InvokeError::Setup(SetupError::Intact(err.to_string()))
78
+ }
60
79
  }
61
80
 
62
81
  impl From<Trap> for InvokeError {
@@ -76,6 +95,7 @@ impl fmt::Display for InvokeError {
76
95
  match self {
77
96
  InvokeError::Trap(trap) => trap.fmt(f),
78
97
  InvokeError::Setup(err) => err.fmt(f),
98
+ InvokeError::Entry(err) => err.fmt(f),
79
99
  }
80
100
  }
81
101
  }
@@ -89,8 +109,9 @@ impl fmt::Display for Trap {
89
109
 
90
110
  impl fmt::Display for SetupError {
91
111
  fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
92
- let (SetupError::ModuleNotBuilt(msg) | SetupError::Dead(msg) | SetupError::Intact(msg)) =
93
- self;
112
+ let (SetupError::GuestBinaryNotFound(msg)
113
+ | SetupError::Dead(msg)
114
+ | SetupError::Intact(msg)) = self;
94
115
  f.write_str(msg)
95
116
  }
96
117
  }
@@ -0,0 +1,52 @@
1
+ //! Files for what the runtime keeps that is no file of its own, as Linux's
2
+ //! anon_inodefs makes one for an eventfd or a timerfd: every such file stands
3
+ //! on one inode no path reaches, and only its operations tell them apart, so
4
+ //! any ABI a Wasm Driver encodes reads, writes, and polls them as it does any
5
+ //! other open file.
6
+
7
+ use std::sync::{Arc, LazyLock};
8
+
9
+ use crate::cred::Cred;
10
+ use crate::vfs::{
11
+ alloc_file_pseudo, Dentry, File, FileOperations, FileSystemType, FileType, Inode,
12
+ InodeOperations, Kstat, OpenHow, SuperBlock, VfsMount,
13
+ };
14
+ use crate::Errno;
15
+
16
+ /// The one Filesystem every anonymous file stands on, as Linux's
17
+ /// `anon_inode_mnt`.
18
+ static ANON_INODE_MNT: LazyLock<Arc<VfsMount>> = LazyLock::new(|| {
19
+ let sb = SuperBlock::new(AnonInodeFs).expect("anon_inodefs fills in nothing that can fail");
20
+ VfsMount::kern_mount(sb)
21
+ });
22
+
23
+ /// The one inode every anonymous file shares, as Linux's `anon_inode_inode`.
24
+ static ANON_INODE_INODE: LazyLock<Arc<Inode>> =
25
+ LazyLock::new(|| Inode::new(&Kstat::new(FileType::Unknown, 0), Arc::new(AnonInodeFs)));
26
+
27
+ /// An anonymous inode reports no kind and no length, as `alloc_anon_inode`
28
+ /// sets no file type in its mode.
29
+ struct AnonInodeFs;
30
+
31
+ impl FileSystemType for AnonInodeFs {
32
+ fn fill_super(self: Arc<Self>, sb: &mut SuperBlock) -> Result<(), Errno> {
33
+ sb.d_make_root(Inode::new(&Kstat::new(FileType::Dir, 0), self));
34
+ Ok(())
35
+ }
36
+ }
37
+
38
+ impl InodeOperations for AnonInodeFs {
39
+ fn getattr(&self, _dentry: &Dentry) -> Result<Kstat, Errno> {
40
+ Ok(Kstat::new(FileType::Unknown, 0))
41
+ }
42
+ }
43
+
44
+ /// A file `ops` answers for, opened as `how` asks and as `cred`, on the one
45
+ /// anonymous inode, as Linux's `anon_inode_getfile` makes one.
46
+ pub(crate) fn anon_inode_getfile(
47
+ ops: Box<dyn FileOperations>,
48
+ how: &OpenHow,
49
+ cred: &Arc<Cred>,
50
+ ) -> File {
51
+ alloc_file_pseudo(ANON_INODE_INODE.clone(), &ANON_INODE_MNT, how, ops, cred)
52
+ }
@@ -12,14 +12,17 @@ use std::io;
12
12
 
13
13
  use cap_std::fs::Dir;
14
14
 
15
- use super::{to_error, FilesystemError};
15
+ use super::{to_error, Errno};
16
16
 
17
17
  /// The name `parent` stores for the entry `name` reaches, a link reached
18
18
  /// as itself; `name` as given when nothing is there.
19
- pub(super) fn lookup(parent: &Dir, name: &str) -> Result<String, FilesystemError> {
19
+ pub(super) fn lookup(parent: &Dir, name: &str) -> Result<String, Errno> {
20
20
  #[cfg(target_os = "macos")]
21
21
  {
22
- macos::lookup(parent, name).map_err(to_error)
22
+ macos::lookup(parent, name)
23
+ .map_err(to_error)?
24
+ .into_string()
25
+ .map_err(|_| Errno::IllegalSequence)
23
26
  }
24
27
  #[cfg(not(target_os = "macos"))]
25
28
  {
@@ -35,7 +38,7 @@ pub(super) fn lookup(parent: &Dir, name: &str) -> Result<String, FilesystemError
35
38
  /// else the entry sharing its inode. A spelling the listing lacks and no
36
39
  /// inode ties to a listed name is refused as `EACCES`.
37
40
  #[cfg(not(target_os = "macos"))]
38
- fn lookup_by_listing(parent: &Dir, name: &str) -> Result<String, FilesystemError> {
41
+ fn lookup_by_listing(parent: &Dir, name: &str) -> Result<String, Errno> {
39
42
  let found = match parent.symlink_metadata(name) {
40
43
  Ok(found) => found,
41
44
  Err(err) if err.kind() == io::ErrorKind::NotFound => return Ok(name.to_string()),
@@ -57,16 +60,16 @@ fn lookup_by_listing(parent: &Dir, name: &str) -> Result<String, FilesystemError
57
60
  fn stored_by_inode(
58
61
  names: Vec<cap_std::fs::DirEntry>,
59
62
  found: &cap_std::fs::Metadata,
60
- ) -> Result<String, FilesystemError> {
63
+ ) -> Result<String, Errno> {
61
64
  use cap_std::fs::MetadataExt;
62
65
  use std::os::unix::fs::DirEntryExt;
63
66
  names
64
67
  .into_iter()
65
68
  .find(|entry| entry.ino() == found.ino())
66
- .ok_or(FilesystemError::PermissionDenied)?
69
+ .ok_or(Errno::PermissionDenied)?
67
70
  .file_name()
68
71
  .into_string()
69
- .map_err(|_| to_error(io::ErrorKind::InvalidData.into()))
72
+ .map_err(|_| Errno::IllegalSequence)
70
73
  }
71
74
 
72
75
  /// Without an inode nothing ties a spelling to a listed name.
@@ -74,15 +77,16 @@ fn stored_by_inode(
74
77
  fn stored_by_inode(
75
78
  _names: Vec<cap_std::fs::DirEntry>,
76
79
  _found: &cap_std::fs::Metadata,
77
- ) -> Result<String, FilesystemError> {
78
- Err(FilesystemError::PermissionDenied)
80
+ ) -> Result<String, Errno> {
81
+ Err(Errno::PermissionDenied)
79
82
  }
80
83
 
81
84
  #[cfg(target_os = "macos")]
82
85
  mod macos {
83
- use std::ffi::CString;
86
+ use std::ffi::{CString, OsString};
84
87
  use std::io;
85
88
  use std::os::fd::AsRawFd;
89
+ use std::os::unix::ffi::OsStringExt;
86
90
 
87
91
  use cap_std::fs::Dir;
88
92
 
@@ -95,7 +99,9 @@ mod macos {
95
99
  data: [u8; 1024],
96
100
  }
97
101
 
98
- pub(super) fn lookup(parent: &Dir, name: &str) -> io::Result<String> {
102
+ /// The name the volume stores for `name`, as its bytes; `name` itself
103
+ /// where nothing stands.
104
+ pub(super) fn lookup(parent: &Dir, name: &str) -> io::Result<OsString> {
99
105
  let path = CString::new(name).map_err(|_| io::Error::from(io::ErrorKind::InvalidInput))?;
100
106
  let mut request = libc::attrlist {
101
107
  bitmapcount: libc::ATTR_BIT_MAP_COUNT,
@@ -129,7 +135,7 @@ mod macos {
129
135
  if answered != 0 {
130
136
  let err = io::Error::last_os_error();
131
137
  return match err.kind() {
132
- io::ErrorKind::NotFound => Ok(name.to_string()),
138
+ io::ErrorKind::NotFound => Ok(OsString::from(name)),
133
139
  _ => Err(err),
134
140
  };
135
141
  }
@@ -143,7 +149,7 @@ mod macos {
143
149
  .zip(len)
144
150
  .and_then(|(start, len)| reply.data.get(start..start.checked_add(len)?))
145
151
  .ok_or(io::ErrorKind::InvalidData)?;
146
- String::from_utf8(stored_name.to_vec()).map_err(|_| io::ErrorKind::InvalidData.into())
152
+ Ok(OsString::from_vec(stored_name.to_vec()))
147
153
  }
148
154
  }
149
155
 
@@ -181,9 +187,41 @@ mod linux {
181
187
  mod tests {
182
188
  use cap_std::ambient_authority;
183
189
 
184
- use super::super::lookup_by_listing;
190
+ use super::super::{lookup_by_listing, stored_by_inode, Errno};
185
191
  use super::{fstatfs, matches_exactly, Dir, EXACT};
186
192
 
193
+ // A host spelling found by its inode answers as a listing of the
194
+ // same name does, so one condition gives the guest one error.
195
+ // @behavior FS-066
196
+ #[test]
197
+ fn a_stored_name_that_is_not_utf8_is_an_illegal_sequence() {
198
+ use std::os::unix::ffi::OsStrExt;
199
+
200
+ let path = std::env::temp_dir().join(format!("kobako-ilseq-{}", std::process::id()));
201
+ let _ = std::fs::remove_dir_all(&path);
202
+ std::fs::create_dir_all(&path).expect("the host directory must be created");
203
+ let stored = std::ffi::OsStr::from_bytes(b"\xff");
204
+ std::fs::write(path.join(stored), "x").expect("the host file must be written");
205
+ let dir = Dir::open_ambient_dir(&path, ambient_authority())
206
+ .expect("the host directory must open");
207
+ let found = dir
208
+ .symlink_metadata(stored)
209
+ .expect("the host file must stand");
210
+ let names = dir
211
+ .entries()
212
+ .expect("the host directory must list")
213
+ .collect::<Result<Vec<_>, _>>()
214
+ .expect("each listed name must be read");
215
+ let answered = stored_by_inode(names, &found);
216
+ let _ = std::fs::remove_dir_all(&path);
217
+ assert_eq!(
218
+ answered,
219
+ Err(Errno::IllegalSequence),
220
+ "a host name that is not UTF-8, spelled by a Bind's lookup of another spelling, \
221
+ must fail as an illegal byte sequence, as a listing of it does"
222
+ );
223
+ }
224
+
187
225
  // A Directory opens its directories as cap-std does, for a path
188
226
  // alone, and a filesystem known to match exactly still spells
189
227
  // without listing. A host on another filesystem has nothing here
@@ -6,22 +6,22 @@
6
6
 
7
7
  use cap_std::fs::Dir;
8
8
 
9
- use super::{open_dir_nofollow, FilesystemError};
9
+ use super::{open_dir_nofollow, Errno};
10
10
 
11
11
  /// The directory `names` reach beneath `dir`, a link on the way refused as
12
12
  /// a loop, walked in one call as 9p's `Twalk` walks several names. A path past
13
13
  /// what the host takes in one call is no name it refuses, so it goes name by
14
14
  /// name, where only a name too long for the host is refused.
15
- pub(super) fn walk(dir: &Dir, names: &[String]) -> Result<Dir, FilesystemError> {
15
+ pub(super) fn walk(dir: &Dir, names: &[String]) -> Result<Dir, Errno> {
16
16
  match kernel::walk(dir, names) {
17
- None | Some(Err(FilesystemError::InvalidFilename)) => by_name(dir, names),
17
+ None | Some(Err(Errno::InvalidFilename)) => by_name(dir, names),
18
18
  Some(opened) => opened,
19
19
  }
20
20
  }
21
21
 
22
22
  /// Each name opened in turn, the way every host can.
23
- fn by_name(dir: &Dir, names: &[String]) -> Result<Dir, FilesystemError> {
24
- let (first, rest) = names.split_first().ok_or(FilesystemError::InvalidInput)?;
23
+ fn by_name(dir: &Dir, names: &[String]) -> Result<Dir, Errno> {
24
+ let (first, rest) = names.split_first().ok_or(Errno::InvalidInput)?;
25
25
  rest.iter()
26
26
  .try_fold(open_dir_nofollow(dir, first)?, |dir, name| {
27
27
  open_dir_nofollow(&dir, name)
@@ -32,15 +32,15 @@ fn by_name(dir: &Dir, names: &[String]) -> Result<Dir, FilesystemError> {
32
32
  /// `..`, so a path leading out of `dir` is one a move carried out as it was
33
33
  /// walked, as `complete_walk` answers EXDEV, and nothing stands there now.
34
34
  #[cfg(any(target_os = "linux", target_os = "macos"))]
35
- fn to_error(errno: i32) -> FilesystemError {
35
+ fn to_error(errno: i32) -> Errno {
36
36
  const LOOP: i32 = rustix::io::Errno::LOOP.raw_os_error();
37
37
  #[cfg(target_os = "linux")]
38
38
  const LEADS_OUT: i32 = rustix::io::Errno::XDEV.raw_os_error();
39
39
  #[cfg(target_os = "macos")]
40
40
  const LEADS_OUT: i32 = libc::ENOTCAPABLE;
41
41
  match errno {
42
- LOOP => FilesystemError::FilesystemLoop,
43
- LEADS_OUT => FilesystemError::NotFound,
42
+ LOOP => Errno::FilesystemLoop,
43
+ LEADS_OUT => Errno::NotFound,
44
44
  _ => super::to_error(std::io::Error::from_raw_os_error(errno)),
45
45
  }
46
46
  }
@@ -52,9 +52,8 @@ mod kernel {
52
52
 
53
53
  use cap_std::fs::Dir;
54
54
  use rustix::fs::{openat2, Mode, OFlags, ResolveFlags};
55
- use rustix::io::Errno;
56
55
 
57
- use super::{to_error, FilesystemError};
56
+ use super::{to_error, Errno};
58
57
 
59
58
  /// Whether this kernel has `openat2`, which arrived in Linux 5.6.
60
59
  static SUPPORTED: AtomicBool = AtomicBool::new(true);
@@ -62,7 +61,7 @@ mod kernel {
62
61
  /// A directory holds its path alone, as cap-std's own opens do, so one a
63
62
  /// host lets a walk search but not read still opens. `names` hold no `..`,
64
63
  /// the one thing `RESOLVE_BENEATH` asks a caller to retry.
65
- pub(super) fn walk(dir: &Dir, names: &[String]) -> Option<Result<Dir, FilesystemError>> {
64
+ pub(super) fn walk(dir: &Dir, names: &[String]) -> Option<Result<Dir, Errno>> {
66
65
  if !SUPPORTED.load(Ordering::Relaxed) {
67
66
  return None;
68
67
  }
@@ -74,7 +73,7 @@ mod kernel {
74
73
  ResolveFlags::BENEATH | ResolveFlags::NO_SYMLINKS | ResolveFlags::NO_MAGICLINKS,
75
74
  ) {
76
75
  Ok(fd) => Some(Ok(Dir::from_std_file(File::from(fd)))),
77
- Err(Errno::NOSYS) => {
76
+ Err(rustix::io::Errno::NOSYS) => {
78
77
  SUPPORTED.store(false, Ordering::Relaxed);
79
78
  None
80
79
  }
@@ -93,7 +92,7 @@ mod kernel {
93
92
 
94
93
  use cap_std::fs::Dir;
95
94
 
96
- use super::{to_error, FilesystemError};
95
+ use super::{to_error, Errno};
97
96
 
98
97
  /// `O_RESOLVE_BENEATH` as `sys/fcntl.h` numbers it; the libc crate
99
98
  /// names no such flag for macOS.
@@ -115,12 +114,12 @@ mod kernel {
115
114
  })
116
115
  }
117
116
 
118
- pub(super) fn walk(dir: &Dir, names: &[String]) -> Option<Result<Dir, FilesystemError>> {
117
+ pub(super) fn walk(dir: &Dir, names: &[String]) -> Option<Result<Dir, Errno>> {
119
118
  if !is_supported() {
120
119
  return None;
121
120
  }
122
121
  let Ok(path) = CString::new(names.join("/")) else {
123
- return Some(Err(FilesystemError::InvalidInput));
122
+ return Some(Err(Errno::InvalidInput));
124
123
  };
125
124
  // SAFETY: `path` is a NUL-terminated string that outlives the call,
126
125
  // and `dir` holds its descriptor open across it.
@@ -148,9 +147,9 @@ mod kernel {
148
147
  mod kernel {
149
148
  use cap_std::fs::Dir;
150
149
 
151
- use super::FilesystemError;
150
+ use super::Errno;
152
151
 
153
- pub(super) fn walk(_dir: &Dir, _names: &[String]) -> Option<Result<Dir, FilesystemError>> {
152
+ pub(super) fn walk(_dir: &Dir, _names: &[String]) -> Option<Result<Dir, Errno>> {
154
153
  None
155
154
  }
156
155
  }
@@ -197,10 +196,10 @@ mod tests {
197
196
 
198
197
  /// What `open` answers for `path`, only whether it opened kept.
199
198
  fn answer(
200
- open: impl Fn(&Dir, &[String]) -> Result<Dir, FilesystemError>,
199
+ open: impl Fn(&Dir, &[String]) -> Result<Dir, Errno>,
201
200
  dir: &Dir,
202
201
  path: &str,
203
- ) -> Result<(), FilesystemError> {
202
+ ) -> Result<(), Errno> {
204
203
  open(dir, &names(path)).map(drop)
205
204
  }
206
205
 
@@ -232,7 +231,7 @@ mod tests {
232
231
  let tree = Tree::new("link");
233
232
  assert_eq!(
234
233
  answer(walk, &tree.dir(), "a/link/c"),
235
- Err(FilesystemError::FilesystemLoop),
234
+ Err(Errno::FilesystemLoop),
236
235
  "a/link/c opened beneath a directory, through a link, must be refused as a loop"
237
236
  );
238
237
  }