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
@@ -6,18 +6,33 @@
6
6
  //! thread past that one's own end.
7
7
 
8
8
  use std::cell::RefCell;
9
- use std::sync::atomic::{AtomicBool, Ordering};
10
- use std::sync::{Arc, OnceLock};
11
- use std::thread::{self, ThreadId};
9
+ use std::sync::atomic::{AtomicBool, AtomicU64, Ordering};
10
+ use std::sync::{Arc, Mutex, MutexGuard, OnceLock, PoisonError};
12
11
  use std::time::{Duration, Instant};
13
12
 
14
- /// How a thread waits for a lock another thread holds. A frontend whose
15
- /// threads share a lock of their own, as Ruby's threads share the GVL,
16
- /// gives that lock up for the wait, or a holder that needs it to finish
17
- /// never could.
13
+ use crate::wait::Waiter;
14
+ use crate::Errno;
15
+
16
+ /// How a thread waits for a lock another thread holds, or for what another
17
+ /// thread brings. A frontend whose threads share a lock of their own, as
18
+ /// Ruby's threads share the GVL, gives that lock up for the wait, or an owner
19
+ /// that needs it to finish never could.
18
20
  pub trait Blocking: Send + Sync {
19
- /// Run `wait`, which returns once the lock is taken.
21
+ /// Run `wait`, which returns once the lock is taken or the event arrived.
20
22
  fn wait(&self, wait: &mut dyn FnMut());
23
+
24
+ /// Take the signal a waiting thread was woken for, interrupting its task
25
+ /// when the signal is to end the run, as Linux handles a pending signal on
26
+ /// the way back to user space. A wait the signal leaves alone goes on, as
27
+ /// Linux restarts a call a handled signal woke.
28
+ fn handle_signal(&self) {}
29
+
30
+ /// The task the calling thread carries out, where the frontend tracks it
31
+ /// apart from the thread, as a Ruby Fiber's run is its own whatever
32
+ /// thread the Fiber runs on. `None` leaves the thread its own task.
33
+ fn current(&self) -> Option<Current> {
34
+ None
35
+ }
21
36
  }
22
37
 
23
38
  static BLOCKING: OnceLock<Box<dyn Blocking>> = OnceLock::new();
@@ -38,13 +53,105 @@ pub(crate) fn schedule(wait: &mut dyn FnMut()) {
38
53
  }
39
54
  }
40
55
 
56
+ /// Who is running, as a lock records Linux's `current` as its owner.
57
+ pub(crate) fn current() -> TaskId {
58
+ Current::get().state().id
59
+ }
60
+
61
+ /// A task as a lock knows its owner, the same on every thread carrying the
62
+ /// task out, as Linux records a task and never the CPU it ran on.
63
+ #[derive(Clone, Copy, Debug, PartialEq, Eq)]
64
+ pub struct TaskId(u64);
65
+
66
+ impl TaskId {
67
+ fn new() -> Self {
68
+ static NEXT: AtomicU64 = AtomicU64::new(0);
69
+ Self(NEXT.fetch_add(1, Ordering::Relaxed))
70
+ }
71
+ }
72
+
73
+ /// The task a thread carries out, as Linux's `current`, and when its run is
74
+ /// to stop. Every thread carrying it shares one, so a run handed from thread to
75
+ /// thread stays one task.
76
+ #[derive(Clone)]
77
+ pub struct Current(Arc<Mutex<State>>);
78
+
79
+ struct State {
80
+ id: TaskId,
81
+ task: Task,
82
+ deadline: Option<Instant>,
83
+ /// How many interruptible stretches the task is inside: a frontend's
84
+ /// around its run, and a run within another where a Filesystem's own call
85
+ /// starts another.
86
+ depth: usize,
87
+ }
88
+
41
89
  thread_local! {
42
- static CURRENT: ThreadId = thread::current().id();
90
+ // What the thread carries out in another's place, while it does.
91
+ static CARRIED: RefCell<Option<Current>> = const { RefCell::new(None) };
92
+ // The thread's own task, carried whenever it carries out no other.
93
+ static OWN: Current = Current::default();
94
+ }
95
+
96
+ impl Default for Current {
97
+ /// A task nobody carries out yet.
98
+ fn default() -> Self {
99
+ Self(Arc::new(Mutex::new(State {
100
+ id: TaskId::new(),
101
+ task: Task::default(),
102
+ deadline: None,
103
+ depth: 0,
104
+ })))
105
+ }
43
106
  }
44
107
 
45
- /// Who is running, as Linux's `current`.
46
- pub(crate) fn current() -> ThreadId {
47
- CURRENT.with(|current| *current)
108
+ impl Current {
109
+ /// A task nobody carries out yet that every lock knows as `owner`, as a
110
+ /// run its frontend starts while `owner` cannot go on until it ends: it
111
+ /// holds what `owner` holds and is refused what `owner` would wait on
112
+ /// itself, while its deadline and interrupts stay its own.
113
+ pub fn with_owner(owner: &Self) -> Self {
114
+ Self(Arc::new(Mutex::new(State {
115
+ id: owner.state().id,
116
+ task: Task::default(),
117
+ deadline: None,
118
+ depth: 0,
119
+ })))
120
+ }
121
+
122
+ /// What the calling thread carries out: the task it carries in another's
123
+ /// place, the one its frontend tracks, or its own.
124
+ pub fn get() -> Self {
125
+ CARRIED
126
+ .with(|carried| carried.borrow().clone())
127
+ .or_else(|| BLOCKING.get().and_then(|blocking| blocking.current()))
128
+ .unwrap_or_else(|| OWN.with(Clone::clone))
129
+ }
130
+
131
+ /// Carry this task out on the calling thread while `f` runs, as a thread
132
+ /// taking a run over from another, putting back what it carried before.
133
+ pub fn carry<T>(&self, f: impl FnOnce() -> T) -> T {
134
+ /// Puts back what the thread carried, however `f` ends.
135
+ struct Before(Option<Current>);
136
+
137
+ impl Drop for Before {
138
+ fn drop(&mut self) {
139
+ CARRIED.with(|carried| *carried.borrow_mut() = self.0.take());
140
+ }
141
+ }
142
+
143
+ let _before = Before(CARRIED.with(|carried| carried.replace(Some(self.clone()))));
144
+ f()
145
+ }
146
+
147
+ /// What a frontend interrupts or signals this task through.
148
+ pub fn task(&self) -> Task {
149
+ self.state().task.clone()
150
+ }
151
+
152
+ fn state(&self) -> MutexGuard<'_, State> {
153
+ self.0.lock().unwrap_or_else(PoisonError::into_inner)
154
+ }
48
155
  }
49
156
 
50
157
  /// A thread as its frontend may interrupt it from another thread, as a signal
@@ -52,73 +159,181 @@ pub(crate) fn current() -> ThreadId {
52
159
  #[derive(Clone, Default)]
53
160
  pub struct Task {
54
161
  interrupted: Arc<AtomicBool>,
162
+ signalled: Arc<AtomicBool>,
163
+ /// The waiter the thread sleeps on, woken by an interrupt as
164
+ /// `signal_wake_up` wakes a sleeping task.
165
+ sleeping: Arc<Mutex<Option<Arc<Waiter>>>>,
55
166
  }
56
167
 
57
168
  impl Task {
58
- /// The calling thread.
169
+ /// The calling thread's.
59
170
  pub fn current() -> Self {
60
- TASK.with(|task| task.borrow().task.clone())
171
+ Current::get().state().task.clone()
61
172
  }
62
173
 
63
174
  /// Have every wait of the run this thread carries out stop, as a fatal
64
175
  /// signal ends a killable wait; it holds until that run ends.
65
176
  pub fn interrupt(&self) {
66
177
  self.interrupted.store(true, Ordering::SeqCst);
178
+ self.wake_up();
67
179
  }
68
- }
69
180
 
70
- /// The task this thread carries out, as Linux's `current`, and when its run is
71
- /// to stop.
72
- #[derive(Default)]
73
- struct Current {
74
- task: Task,
75
- deadline: Option<Instant>,
76
- /// How many runs the thread is inside, one within another where a
77
- /// Filesystem's own call starts another.
78
- depth: usize,
181
+ /// Have the wait the thread's run sleeps in hand a signal that may not be
182
+ /// fatal to the registered `Blocking`, as `signal_wake_up` wakes a task
183
+ /// for its pending signal; it holds until handled or that run ends.
184
+ pub fn signal(&self) {
185
+ self.signalled.store(true, Ordering::SeqCst);
186
+ self.wake_up();
187
+ }
188
+
189
+ fn wake_up(&self) {
190
+ if let Some(waiter) = self.sleeping().as_ref() {
191
+ waiter.wake();
192
+ }
193
+ }
194
+
195
+ fn sleeping(&self) -> std::sync::MutexGuard<'_, Option<Arc<Waiter>>> {
196
+ self.sleeping.lock().unwrap_or_else(PoisonError::into_inner)
197
+ }
79
198
  }
80
199
 
81
- thread_local! {
82
- static TASK: RefCell<Current> = RefCell::default();
200
+ /// Have an interrupt of the calling thread wake `waiter` while `sleep` runs,
201
+ /// as a task sleeping in `TASK_KILLABLE` is woken by a fatal signal.
202
+ pub(crate) fn sleep_on<T>(waiter: &Arc<Waiter>, sleep: impl FnOnce() -> T) -> T {
203
+ /// Takes the waiter back however `sleep` ends.
204
+ struct Awake(Task);
205
+
206
+ impl Drop for Awake {
207
+ fn drop(&mut self) {
208
+ *self.0.sleeping() = None;
209
+ }
210
+ }
211
+
212
+ let task = Task::current();
213
+ *task.sleeping() = Some(waiter.clone());
214
+ let _awake = Awake(task);
215
+ sleep()
83
216
  }
84
217
 
85
218
  /// Run `run` as a run that stops waiting once `deadline` passes or its thread
86
219
  /// is interrupted, as a Linux task stops a killable wait on a fatal signal. A
87
220
  /// run within another keeps the outer one's interrupt.
88
221
  pub fn run_task<T>(deadline: Option<Instant>, run: impl FnOnce() -> T) -> T {
89
- /// Puts back the deadline of the run around this one, however `run` ends,
90
- /// and lets an interrupt go once no run is left.
91
- struct Outer(Option<Instant>);
222
+ /// Puts back the deadline of the run around this one, however `run` ends.
223
+ struct Outer(Current, Option<Instant>);
92
224
 
93
225
  impl Drop for Outer {
94
226
  fn drop(&mut self) {
95
- TASK.with(|task| {
96
- let mut running = task.borrow_mut();
97
- running.deadline = self.0;
98
- running.depth -= 1;
99
- if running.depth == 0 {
100
- running.task.interrupted.store(false, Ordering::SeqCst);
101
- }
102
- });
227
+ self.0.state().deadline = self.1;
103
228
  }
104
229
  }
105
230
 
106
- let _outer = Outer(TASK.with(|task| {
107
- let mut running = task.borrow_mut();
108
- running.depth += 1;
109
- std::mem::replace(&mut running.deadline, deadline)
110
- }));
231
+ run_interruptible(|| {
232
+ let current = Current::get();
233
+ let outer = std::mem::replace(&mut current.state().deadline, deadline);
234
+ let _outer = Outer(current, outer);
235
+ run()
236
+ })
237
+ }
238
+
239
+ /// Run `run` as a stretch its frontend may interrupt, as a frontend brackets
240
+ /// all it does for one call around the run itself. An interrupt landing
241
+ /// anywhere in it lasts until the outermost stretch ends, so it stops the
242
+ /// waits of the run it was meant for and never those of the thread's next.
243
+ pub fn run_interruptible<T>(run: impl FnOnce() -> T) -> T {
244
+ /// Lets an interrupt go once no stretch is left, however `run` ends.
245
+ struct Stretch(Current);
246
+
247
+ impl Drop for Stretch {
248
+ fn drop(&mut self) {
249
+ let mut running = self.0.state();
250
+ running.depth -= 1;
251
+ if running.depth == 0 {
252
+ running.task.interrupted.store(false, Ordering::SeqCst);
253
+ running.task.signalled.store(false, Ordering::SeqCst);
254
+ }
255
+ }
256
+ }
257
+
258
+ let current = Current::get();
259
+ current.state().depth += 1;
260
+ let _stretch = Stretch(current);
111
261
  run()
112
262
  }
113
263
 
264
+ /// Why a run is to stop, as the fatal signal pending on a Linux task says
265
+ /// what ended it.
266
+ #[derive(Clone, Copy, Debug, PartialEq, Eq)]
267
+ pub enum FatalSignal {
268
+ /// The run passed its deadline.
269
+ Deadline,
270
+ /// The run's frontend interrupted it.
271
+ Interrupt,
272
+ }
273
+
274
+ /// The fatal signal pending on the calling thread's run, its deadline read
275
+ /// first; `None` while the run may go on waiting.
276
+ pub fn pending_fatal_signal() -> Option<FatalSignal> {
277
+ let current = Current::get();
278
+ let running = current.state();
279
+ if running.deadline.is_some_and(|at| Instant::now() >= at) {
280
+ Some(FatalSignal::Deadline)
281
+ } else if running.task.interrupted.load(Ordering::SeqCst) {
282
+ Some(FatalSignal::Interrupt)
283
+ } else {
284
+ None
285
+ }
286
+ }
287
+
114
288
  /// Whether the calling thread's run is to stop waiting, as Linux's
115
289
  /// `fatal_signal_pending(current)` answers.
116
290
  pub(crate) fn fatal_signal_pending() -> bool {
117
- TASK.with(|task| {
118
- let running = task.borrow();
119
- running.task.interrupted.load(Ordering::SeqCst)
120
- || running.deadline.is_some_and(|at| Instant::now() >= at)
121
- })
291
+ pending_fatal_signal().is_some()
292
+ }
293
+
294
+ /// Whether a signal waits for the calling thread to handle it, as Linux's
295
+ /// `signal_pending(current)` answers.
296
+ pub(crate) fn signal_pending() -> bool {
297
+ Task::current().signalled.load(Ordering::SeqCst)
298
+ }
299
+
300
+ /// Hand the signal pending on the calling thread to the registered
301
+ /// `Blocking`, as a woken wait returns to have it handled; the wait then looks
302
+ /// at `fatal_signal_pending` again. It must hold no lock that never waits
303
+ /// through the `Blocking`, as the frontend may take its own to handle it.
304
+ pub(crate) fn handle_signal() {
305
+ let signalled = Task::current().signalled.swap(false, Ordering::SeqCst);
306
+ if let (true, Some(blocking)) = (signalled, BLOCKING.get()) {
307
+ blocking.handle_signal();
308
+ }
309
+ }
310
+
311
+ /// Handle the signal pending on the calling thread's run, then answer the
312
+ /// fatal signal pending on it, as Linux's `exit_to_user_mode` does each time a
313
+ /// task goes back to running its own code: a guest that only computes meets no
314
+ /// wait to take a signal, so its engine asks here as it ticks.
315
+ pub fn exit_to_user_mode() -> Option<FatalSignal> {
316
+ if signal_pending() {
317
+ handle_signal();
318
+ }
319
+ pending_fatal_signal()
320
+ }
321
+
322
+ /// Answer EINTR where the calling thread's run is to stop, its pending signal
323
+ /// handled first, as a long loop of Linux's looks at `fatal_signal_pending`
324
+ /// between its steps. A loop beneath a sleeping lock calls it each step, so a
325
+ /// deadline or an interrupt ends the operation holding the lock; it must hold
326
+ /// no lock that never waits through the `Blocking`, as `handle_signal` asks.
327
+ pub fn stop_if_fatal() -> Result<(), Errno> {
328
+ match exit_to_user_mode() {
329
+ Some(_) => Err(Errno::Interrupted),
330
+ None => Ok(()),
331
+ }
332
+ }
333
+
334
+ /// When the calling thread's run is to stop, once it has a deadline.
335
+ pub(crate) fn deadline() -> Option<Instant> {
336
+ Current::get().state().deadline
122
337
  }
123
338
 
124
339
  /// The most a wait sleeps before it looks again whether its run is to stop.
@@ -127,10 +342,10 @@ const MAX_SLEEP: Duration = Duration::from_millis(10);
127
342
  /// How long a wait sleeps before it looks again whether its run is to stop:
128
343
  /// never past its deadline.
129
344
  pub(crate) fn sleep_slice() -> Duration {
130
- TASK.with(|task| match task.borrow().deadline {
345
+ match deadline() {
131
346
  Some(at) => at.saturating_duration_since(Instant::now()).min(MAX_SLEEP),
132
347
  None => MAX_SLEEP,
133
- })
348
+ }
134
349
  }
135
350
 
136
351
  /// A process has one Blocking, so a test counts only the waits of the thread
@@ -146,7 +361,8 @@ pub(crate) mod testing {
146
361
  /// the test instead of hanging the suite.
147
362
  pub(crate) const MAX_WAIT: Duration = Duration::from_secs(10);
148
363
 
149
- /// Wait until `arrived`, failing the test with `what` past `MAX_WAIT`.
364
+ /// Wait until `arrived`, sleeping a millisecond between looks so the thread
365
+ /// awaited has the CPU, and fail the test with `what` past `MAX_WAIT`.
150
366
  pub(crate) fn wait_until(what: &str, mut arrived: impl FnMut() -> bool) {
151
367
  let started = Instant::now();
152
368
  while !arrived() {
@@ -154,8 +370,21 @@ pub(crate) mod testing {
154
370
  started.elapsed() < MAX_WAIT,
155
371
  "{what} did not happen within {MAX_WAIT:?}"
156
372
  );
157
- std::thread::yield_now();
373
+ std::thread::sleep(Duration::from_millis(1));
374
+ }
375
+ }
376
+
377
+ /// Whether `holds` comes true within `MAX_WAIT`, looking as `wait_until`
378
+ /// does, for a promise the test's own assertion states.
379
+ pub(crate) fn holds_within_max_wait(mut holds: impl FnMut() -> bool) -> bool {
380
+ let started = Instant::now();
381
+ while !holds() {
382
+ if started.elapsed() >= MAX_WAIT {
383
+ return false;
384
+ }
385
+ std::thread::sleep(Duration::from_millis(1));
158
386
  }
387
+ true
159
388
  }
160
389
 
161
390
  /// What the thread `handle` runs answers, failing the test with `what` past
@@ -185,6 +414,8 @@ pub(crate) mod testing {
185
414
 
186
415
  thread_local! {
187
416
  static COUNTS: RefCell<Option<Arc<AtomicUsize>>> = const { RefCell::new(None) };
417
+ static SIGNALS: RefCell<Option<Arc<AtomicUsize>>> = const { RefCell::new(None) };
418
+ static ON_SIGNAL: RefCell<Option<Box<dyn Fn()>>> = const { RefCell::new(None) };
188
419
  }
189
420
 
190
421
  struct CountsWaits;
@@ -198,6 +429,19 @@ pub(crate) mod testing {
198
429
  });
199
430
  wait();
200
431
  }
432
+
433
+ fn handle_signal(&self) {
434
+ SIGNALS.with(|signals| {
435
+ if let Some(signals) = signals.borrow().as_ref() {
436
+ signals.fetch_add(1, Ordering::SeqCst);
437
+ }
438
+ });
439
+ ON_SIGNAL.with(|stop| {
440
+ if let Some(stop) = stop.borrow().as_ref() {
441
+ stop();
442
+ }
443
+ });
444
+ }
201
445
  }
202
446
 
203
447
  /// Counts each wait this thread makes through the registered Blocking.
@@ -207,4 +451,47 @@ pub(crate) mod testing {
207
451
  COUNTS.with(|held| *held.borrow_mut() = Some(counts.clone()));
208
452
  counts
209
453
  }
454
+
455
+ /// Counts each signal the registered `Blocking` handles for this thread,
456
+ /// leaving every wait to go on.
457
+ pub(crate) fn count_signals() -> Arc<AtomicUsize> {
458
+ BLOCKING.get_or_init(|| Box::new(CountsWaits));
459
+ let signals = Arc::<AtomicUsize>::default();
460
+ SIGNALS.with(|held| *held.borrow_mut() = Some(signals.clone()));
461
+ signals
462
+ }
463
+
464
+ /// Runs `stop` each time the registered `Blocking` handles a signal for this
465
+ /// thread, so a test holds a waiter where it handles one.
466
+ pub(crate) fn on_signal(stop: impl Fn() + 'static) {
467
+ BLOCKING.get_or_init(|| Box::new(CountsWaits));
468
+ ON_SIGNAL.with(|held| *held.borrow_mut() = Some(Box::new(stop)));
469
+ }
470
+ }
471
+
472
+ #[cfg(test)]
473
+ mod tests {
474
+ use super::*;
475
+
476
+ // @behavior RT-403
477
+ #[test]
478
+ fn an_interrupt_around_a_run_stops_that_run_alone() {
479
+ let (during_setup, after_teardown) = run_interruptible(|| {
480
+ Task::current().interrupt();
481
+ let during_setup = run_task(None, pending_fatal_signal);
482
+ Task::current().interrupt();
483
+ (during_setup, pending_fatal_signal())
484
+ });
485
+ let next = run_task(None, pending_fatal_signal);
486
+ assert_eq!(
487
+ (during_setup, after_teardown, next),
488
+ (
489
+ Some(FatalSignal::Interrupt),
490
+ Some(FatalSignal::Interrupt),
491
+ None
492
+ ),
493
+ "an interrupt while a run is set up must stop that run's waits, and \
494
+ one while it is torn down must not stop the thread's next run"
495
+ );
496
+ }
210
497
  }
@@ -0,0 +1,22 @@
1
+ //! A network entry's connect policy as a security module, as Landlock's
2
+ //! `net.c` answers the network rights of its domain.
3
+
4
+ use std::sync::Arc;
5
+
6
+ use super::SecurityOperations;
7
+ use crate::network::Connection;
8
+ use crate::policy::Policy;
9
+ use crate::Errno;
10
+
11
+ /// An entry's connect policy, judging what the guest named, as one more LSM
12
+ /// answers `security_socket_connect`.
13
+ pub(super) struct EntryPolicy(pub(super) Arc<dyn for<'a> Policy<Connection<'a>>>);
14
+
15
+ impl SecurityOperations for EntryPolicy {
16
+ fn socket_connect(&self, connection: &Connection<'_>) -> Result<(), Errno> {
17
+ match self.0.allows(connection) {
18
+ true => Ok(()),
19
+ false => Err(Errno::PermissionDenied),
20
+ }
21
+ }
22
+ }