kobako 0.31.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 (133) 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 +125 -0
  5. data/Cargo.lock +457 -494
  6. data/README.md +2 -3
  7. data/ROADMAP.md +0 -1
  8. data/crates/kobako-runtime/CHANGELOG.md +90 -0
  9. data/crates/kobako-runtime/Cargo.toml +14 -15
  10. data/crates/kobako-runtime/README.md +1 -1
  11. data/crates/kobako-runtime/src/cred.rs +32 -20
  12. data/crates/kobako-runtime/src/filesystem/bind/fid.rs +300 -22
  13. data/crates/kobako-runtime/src/filesystem/bind/lookup.rs +56 -22
  14. data/crates/kobako-runtime/src/filesystem/bind/unicode.rs +109 -0
  15. data/crates/kobako-runtime/src/filesystem/bind/walk.rs +33 -14
  16. data/crates/kobako-runtime/src/filesystem/bind.rs +393 -220
  17. data/crates/kobako-runtime/src/filesystem/overlay/stack/copy_up.rs +2 -0
  18. data/crates/kobako-runtime/src/filesystem/overlay/stack.rs +9 -10
  19. data/crates/kobako-runtime/src/filesystem/overlay.rs +277 -46
  20. data/crates/kobako-runtime/src/filesystem/rdir.rs +8 -27
  21. data/crates/kobako-runtime/src/filesystem/testing.rs +31 -0
  22. data/crates/kobako-runtime/src/filesystem/tmpfs.rs +47 -13
  23. data/crates/kobako-runtime/src/filesystem.rs +2 -0
  24. data/crates/kobako-runtime/src/fork.rs +239 -16
  25. data/crates/kobako-runtime/src/limits.rs +17 -41
  26. data/crates/kobako-runtime/src/locking/seqlock.rs +29 -3
  27. data/crates/kobako-runtime/src/locking.rs +508 -113
  28. data/crates/kobako-runtime/src/memcg.rs +83 -160
  29. data/crates/kobako-runtime/src/mount.rs +0 -30
  30. data/crates/kobako-runtime/src/net/connections.rs +53 -13
  31. data/crates/kobako-runtime/src/net/egress.rs +44 -24
  32. data/crates/kobako-runtime/src/net/http.rs +888 -456
  33. data/crates/kobako-runtime/src/net/lookup.rs +150 -50
  34. data/crates/kobako-runtime/src/net/namespace.rs +97 -95
  35. data/crates/kobako-runtime/src/net/reactor.rs +298 -203
  36. data/crates/kobako-runtime/src/net/resolv/darwin.rs +8 -3
  37. data/crates/kobako-runtime/src/net/resolv/stub/tests.rs +50 -0
  38. data/crates/kobako-runtime/src/net/resolv/stub.rs +29 -23
  39. data/crates/kobako-runtime/src/net/resolv/windows.rs +14 -44
  40. data/crates/kobako-runtime/src/net/resolv.rs +39 -15
  41. data/crates/kobako-runtime/src/net/skbuff.rs +109 -0
  42. data/crates/kobako-runtime/src/net/socket.rs +51 -52
  43. data/crates/kobako-runtime/src/net/task.rs +223 -0
  44. data/crates/kobako-runtime/src/net/tcp.rs +410 -158
  45. data/crates/kobako-runtime/src/net/tls.rs +65 -3
  46. data/crates/kobako-runtime/src/net/transport.rs +72 -47
  47. data/crates/kobako-runtime/src/net/udp.rs +231 -129
  48. data/crates/kobako-runtime/src/net.rs +3 -0
  49. data/crates/kobako-runtime/src/sched.rs +244 -58
  50. data/crates/kobako-runtime/src/security.rs +27 -16
  51. data/crates/kobako-runtime/src/vfs/dentry.rs +80 -41
  52. data/crates/kobako-runtime/src/vfs/file.rs +52 -53
  53. data/crates/kobako-runtime/src/vfs/kern_path.rs +24 -11
  54. data/crates/kobako-runtime/src/vfs/namei.rs +399 -184
  55. data/crates/kobako-runtime/src/vfs/namespace.rs +123 -40
  56. data/crates/kobako-runtime/src/vfs/stat.rs +105 -16
  57. data/crates/kobako-runtime/src/vfs.rs +1 -0
  58. data/crates/kobako-runtime/src/wait.rs +75 -50
  59. data/crates/kobako-transport/CHANGELOG.md +7 -0
  60. data/crates/kobako-transport/Cargo.toml +1 -1
  61. data/crates/kobako-transport/README.md +1 -1
  62. data/crates/kobako-wasmtime/CHANGELOG.md +47 -0
  63. data/crates/kobako-wasmtime/Cargo.toml +27 -6
  64. data/crates/kobako-wasmtime/README.md +3 -3
  65. data/crates/kobako-wasmtime/src/abi.rs +2 -1
  66. data/crates/kobako-wasmtime/src/ambient.rs +11 -2
  67. data/crates/kobako-wasmtime/src/cache/unix.rs +95 -0
  68. data/crates/kobako-wasmtime/src/cache/windows.rs +359 -0
  69. data/crates/kobako-wasmtime/src/cache.rs +88 -103
  70. data/crates/kobako-wasmtime/src/driver.rs +40 -27
  71. data/crates/kobako-wasmtime/src/invocation.rs +21 -7
  72. data/crates/kobako-wasmtime/src/lib.rs +2 -0
  73. data/crates/kobako-wasmtime/src/limiter.rs +78 -25
  74. data/crates/kobako-wasmtime/src/preview1/descriptors.rs +13 -17
  75. data/crates/kobako-wasmtime/src/preview1/differential/mount.rs +4 -5
  76. data/crates/kobako-wasmtime/src/preview1/differential.rs +1 -1
  77. data/crates/kobako-wasmtime/src/preview1/fd.rs +23 -19
  78. data/crates/kobako-wasmtime/src/preview1/mount_view.rs +130 -197
  79. data/crates/kobako-wasmtime/src/preview1/path.rs +3 -3
  80. data/crates/kobako-wasmtime/src/preview1/poll.rs +28 -22
  81. data/crates/kobako-wasmtime/src/preview1/random.rs +125 -0
  82. data/crates/kobako-wasmtime/src/preview1/testing.rs +31 -0
  83. data/crates/kobako-wasmtime/src/preview1.rs +85 -9
  84. data/crates/kobako-wasmtime/src/preview2/filesystem.rs +2 -2
  85. data/crates/kobako-wasmtime/src/preview2/host.rs +609 -249
  86. data/crates/kobako-wasmtime/src/preview2/http.rs +299 -215
  87. data/crates/kobako-wasmtime/src/preview2/io.rs +17 -4
  88. data/crates/kobako-wasmtime/src/preview2/table.rs +24 -72
  89. data/crates/kobako-wasmtime/src/trap.rs +68 -23
  90. data/crates/kobako-wasmtime/src/waits.rs +59 -0
  91. data/crates/kobako-wasmtime/src/wasi.rs +91 -10
  92. data/crates/kobako-wasmtime/witx/wasi_snapshot_preview1.witx +13 -0
  93. data/data/kobako.wasm +0 -0
  94. data/ext/kobako/Cargo.toml +6 -2
  95. data/ext/kobako/src/escape.rs +332 -26
  96. data/ext/kobako/src/filesystem/grants.rs +5 -7
  97. data/ext/kobako/src/filesystem/ruby.rs +10 -3
  98. data/ext/kobako/src/filesystem.rs +47 -8
  99. data/ext/kobako/src/fork.rs +72 -0
  100. data/ext/kobako/src/gvl.rs +499 -76
  101. data/ext/kobako/src/handoff.rs +490 -0
  102. data/ext/kobako/src/lib.rs +10 -0
  103. data/ext/kobako/src/network.rs +5 -3
  104. data/ext/kobako/src/pinned_value.rs +28 -15
  105. data/ext/kobako/src/runtime/bridge.rs +32 -25
  106. data/ext/kobako/src/runtime.rs +74 -43
  107. data/lib/kobako/catalog/handles.rb +9 -5
  108. data/lib/kobako/codec/decoder.rb +1 -1
  109. data/lib/kobako/codec/state.rb +2 -18
  110. data/lib/kobako/codec/value_budget.rb +4 -4
  111. data/lib/kobako/codec.rb +5 -6
  112. data/lib/kobako/errors.rb +4 -7
  113. data/lib/kobako/filesystem.rb +1 -1
  114. data/lib/kobako/fork.rb +42 -0
  115. data/lib/kobako/handle.rb +1 -2
  116. data/lib/kobako/sandbox.rb +17 -36
  117. data/lib/kobako/sandbox_options.rb +7 -9
  118. data/lib/kobako/usage.rb +4 -2
  119. data/lib/kobako/version.rb +1 -1
  120. data/lib/kobako.rb +2 -2
  121. data/sig/kobako/catalog/handles.rbs +2 -0
  122. data/sig/kobako/codec/state.rbs +0 -4
  123. data/sig/kobako/codec/value_budget.rbs +1 -1
  124. data/sig/kobako/codec.rbs +2 -2
  125. data/sig/kobako/errors.rbs +0 -3
  126. data/sig/kobako/fork.rbs +17 -0
  127. data/sig/kobako/runtime.rbs +2 -2
  128. data/sig/kobako/sandbox.rbs +3 -12
  129. data/sig/kobako/sandbox_options.rbs +0 -2
  130. metadata +15 -4
  131. data/lib/kobako/pool.rb +0 -162
  132. data/sig/core/thread.rbs +0 -6
  133. data/sig/kobako/pool.rbs +0 -46
@@ -6,20 +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, Mutex, OnceLock, PoisonError};
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
13
  use crate::wait::Waiter;
14
+ use crate::Errno;
15
15
 
16
16
  /// How a thread waits for a lock another thread holds, or for what another
17
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 a holder
18
+ /// Ruby's threads share the GVL, gives that lock up for the wait, or an owner
19
19
  /// that needs it to finish never could.
20
20
  pub trait Blocking: Send + Sync {
21
21
  /// Run `wait`, which returns once the lock is taken or the event arrived.
22
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
+ }
23
36
  }
24
37
 
25
38
  static BLOCKING: OnceLock<Box<dyn Blocking>> = OnceLock::new();
@@ -41,8 +54,104 @@ pub(crate) fn schedule(wait: &mut dyn FnMut()) {
41
54
  }
42
55
 
43
56
  /// Who is running, as a lock records Linux's `current` as its owner.
44
- pub(crate) fn current() -> ThreadId {
45
- TASK.with(|task| task.borrow().id)
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
+
89
+ thread_local! {
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
+ }
106
+ }
107
+
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
+ }
46
155
  }
47
156
 
48
157
  /// A thread as its frontend may interrupt it from another thread, as a signal
@@ -50,21 +159,34 @@ pub(crate) fn current() -> ThreadId {
50
159
  #[derive(Clone, Default)]
51
160
  pub struct Task {
52
161
  interrupted: Arc<AtomicBool>,
162
+ signalled: Arc<AtomicBool>,
53
163
  /// The waiter the thread sleeps on, woken by an interrupt as
54
164
  /// `signal_wake_up` wakes a sleeping task.
55
165
  sleeping: Arc<Mutex<Option<Arc<Waiter>>>>,
56
166
  }
57
167
 
58
168
  impl Task {
59
- /// The calling thread.
169
+ /// The calling thread's.
60
170
  pub fn current() -> Self {
61
- TASK.with(|task| task.borrow().task.clone())
171
+ Current::get().state().task.clone()
62
172
  }
63
173
 
64
174
  /// Have every wait of the run this thread carries out stop, as a fatal
65
175
  /// signal ends a killable wait; it holds until that run ends.
66
176
  pub fn interrupt(&self) {
67
177
  self.interrupted.store(true, Ordering::SeqCst);
178
+ self.wake_up();
179
+ }
180
+
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) {
68
190
  if let Some(waiter) = self.sleeping().as_ref() {
69
191
  waiter.wake();
70
192
  }
@@ -93,43 +215,23 @@ pub(crate) fn sleep_on<T>(waiter: &Arc<Waiter>, sleep: impl FnOnce() -> T) -> T
93
215
  sleep()
94
216
  }
95
217
 
96
- /// The task this thread carries out, as Linux's `current`, and when its run is
97
- /// to stop.
98
- struct Current {
99
- id: ThreadId,
100
- task: Task,
101
- deadline: Option<Instant>,
102
- /// How many interruptible stretches the thread is inside: a frontend's
103
- /// around its run, and a run within another where a Filesystem's own call
104
- /// starts another.
105
- depth: usize,
106
- }
107
-
108
- thread_local! {
109
- static TASK: RefCell<Current> = RefCell::new(Current {
110
- id: thread::current().id(),
111
- task: Task::default(),
112
- deadline: None,
113
- depth: 0,
114
- });
115
- }
116
-
117
218
  /// Run `run` as a run that stops waiting once `deadline` passes or its thread
118
219
  /// is interrupted, as a Linux task stops a killable wait on a fatal signal. A
119
220
  /// run within another keeps the outer one's interrupt.
120
221
  pub fn run_task<T>(deadline: Option<Instant>, run: impl FnOnce() -> T) -> T {
121
222
  /// Puts back the deadline of the run around this one, however `run` ends.
122
- struct Outer(Option<Instant>);
223
+ struct Outer(Current, Option<Instant>);
123
224
 
124
225
  impl Drop for Outer {
125
226
  fn drop(&mut self) {
126
- TASK.with(|task| task.borrow_mut().deadline = self.0);
227
+ self.0.state().deadline = self.1;
127
228
  }
128
229
  }
129
230
 
130
231
  run_interruptible(|| {
131
- let _outer =
132
- Outer(TASK.with(|task| std::mem::replace(&mut task.borrow_mut().deadline, deadline)));
232
+ let current = Current::get();
233
+ let outer = std::mem::replace(&mut current.state().deadline, deadline);
234
+ let _outer = Outer(current, outer);
133
235
  run()
134
236
  })
135
237
  }
@@ -140,22 +242,22 @@ pub fn run_task<T>(deadline: Option<Instant>, run: impl FnOnce() -> T) -> T {
140
242
  /// waits of the run it was meant for and never those of the thread's next.
141
243
  pub fn run_interruptible<T>(run: impl FnOnce() -> T) -> T {
142
244
  /// Lets an interrupt go once no stretch is left, however `run` ends.
143
- struct Stretch;
245
+ struct Stretch(Current);
144
246
 
145
247
  impl Drop for Stretch {
146
248
  fn drop(&mut self) {
147
- TASK.with(|task| {
148
- let mut running = task.borrow_mut();
149
- running.depth -= 1;
150
- if running.depth == 0 {
151
- running.task.interrupted.store(false, Ordering::SeqCst);
152
- }
153
- });
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
+ }
154
255
  }
155
256
  }
156
257
 
157
- TASK.with(|task| task.borrow_mut().depth += 1);
158
- let _stretch = Stretch;
258
+ let current = Current::get();
259
+ current.state().depth += 1;
260
+ let _stretch = Stretch(current);
159
261
  run()
160
262
  }
161
263
 
@@ -172,16 +274,15 @@ pub enum FatalSignal {
172
274
  /// The fatal signal pending on the calling thread's run, its deadline read
173
275
  /// first; `None` while the run may go on waiting.
174
276
  pub fn pending_fatal_signal() -> Option<FatalSignal> {
175
- TASK.with(|task| {
176
- let running = task.borrow();
177
- if running.deadline.is_some_and(|at| Instant::now() >= at) {
178
- Some(FatalSignal::Deadline)
179
- } else if running.task.interrupted.load(Ordering::SeqCst) {
180
- Some(FatalSignal::Interrupt)
181
- } else {
182
- None
183
- }
184
- })
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
+ }
185
286
  }
186
287
 
187
288
  /// Whether the calling thread's run is to stop waiting, as Linux's
@@ -190,9 +291,49 @@ pub(crate) fn fatal_signal_pending() -> bool {
190
291
  pending_fatal_signal().is_some()
191
292
  }
192
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
+
193
334
  /// When the calling thread's run is to stop, once it has a deadline.
194
335
  pub(crate) fn deadline() -> Option<Instant> {
195
- TASK.with(|task| task.borrow().deadline)
336
+ Current::get().state().deadline
196
337
  }
197
338
 
198
339
  /// The most a wait sleeps before it looks again whether its run is to stop.
@@ -201,10 +342,10 @@ const MAX_SLEEP: Duration = Duration::from_millis(10);
201
342
  /// How long a wait sleeps before it looks again whether its run is to stop:
202
343
  /// never past its deadline.
203
344
  pub(crate) fn sleep_slice() -> Duration {
204
- TASK.with(|task| match task.borrow().deadline {
345
+ match deadline() {
205
346
  Some(at) => at.saturating_duration_since(Instant::now()).min(MAX_SLEEP),
206
347
  None => MAX_SLEEP,
207
- })
348
+ }
208
349
  }
209
350
 
210
351
  /// A process has one Blocking, so a test counts only the waits of the thread
@@ -220,7 +361,8 @@ pub(crate) mod testing {
220
361
  /// the test instead of hanging the suite.
221
362
  pub(crate) const MAX_WAIT: Duration = Duration::from_secs(10);
222
363
 
223
- /// 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`.
224
366
  pub(crate) fn wait_until(what: &str, mut arrived: impl FnMut() -> bool) {
225
367
  let started = Instant::now();
226
368
  while !arrived() {
@@ -228,8 +370,21 @@ pub(crate) mod testing {
228
370
  started.elapsed() < MAX_WAIT,
229
371
  "{what} did not happen within {MAX_WAIT:?}"
230
372
  );
231
- 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));
232
386
  }
387
+ true
233
388
  }
234
389
 
235
390
  /// What the thread `handle` runs answers, failing the test with `what` past
@@ -259,6 +414,8 @@ pub(crate) mod testing {
259
414
 
260
415
  thread_local! {
261
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) };
262
419
  }
263
420
 
264
421
  struct CountsWaits;
@@ -272,6 +429,19 @@ pub(crate) mod testing {
272
429
  });
273
430
  wait();
274
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
+ }
275
445
  }
276
446
 
277
447
  /// Counts each wait this thread makes through the registered Blocking.
@@ -281,6 +451,22 @@ pub(crate) mod testing {
281
451
  COUNTS.with(|held| *held.borrow_mut() = Some(counts.clone()));
282
452
  counts
283
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
+ }
284
470
  }
285
471
 
286
472
  #[cfg(test)]
@@ -140,7 +140,7 @@ pub fn prepare_creds(
140
140
  ) -> Arc<Cred> {
141
141
  let mut security: Vec<Arc<dyn SecurityOperations>> = vec![Arc::new(KobakoSecurity)];
142
142
  security.extend(modules.iter().cloned());
143
- Cred::within(security, MemoryBudget::new(memory_limit))
143
+ Cred::with_memory(security, MemoryBudget::new(memory_limit))
144
144
  }
145
145
 
146
146
  /// `cred` with each Mount's Policy and each network entry's connect policy
@@ -296,7 +296,7 @@ mod tests {
296
296
  .collect();
297
297
  let mount = Mount::new(
298
298
  "/data",
299
- Filesystem::new(tmpfs).expect("the instance must stand"),
299
+ Filesystem::new(tmpfs.with_nr_inodes(None)).expect("the instance must stand"),
300
300
  Access::ReadWrite,
301
301
  )
302
302
  .expect("/data is a guest path");
@@ -352,8 +352,13 @@ mod tests {
352
352
 
353
353
  /// Holds the first ask of `revalidate` about `d` once it reached the
354
354
  /// Filesystem, so a move of `d` racing it either lands first or waits for
355
- /// the answer.
356
- struct PausesRevalidate(std::sync::Mutex<Option<std::sync::mpsc::Sender<()>>>);
355
+ /// the answer, until `mover_waits` counts a wait or `moved` is set.
356
+ #[derive(Default)]
357
+ struct PausesRevalidate {
358
+ reached: std::sync::Mutex<Option<std::sync::mpsc::Sender<()>>>,
359
+ mover_waits: std::sync::OnceLock<Arc<std::sync::atomic::AtomicUsize>>,
360
+ moved: std::sync::atomic::AtomicBool,
361
+ }
357
362
 
358
363
  impl Hook for PausesRevalidate {
359
364
  fn revalidate(
@@ -362,13 +367,22 @@ mod tests {
362
367
  dir: &dyn InodeOperations,
363
368
  dentry: &Dentry,
364
369
  ) -> Result<bool, Errno> {
370
+ use std::sync::atomic::Ordering;
365
371
  let reached = match dentry.name() == "d" {
366
- true => self.0.lock().expect("the sender").take(),
372
+ true => self.reached.lock().expect("the sender").take(),
367
373
  false => None,
368
374
  };
369
375
  if let Some(reached) = reached {
370
376
  reached.send(()).expect("the test waits for the ask");
371
- std::thread::sleep(std::time::Duration::from_millis(100));
377
+ crate::sched::testing::wait_until(
378
+ "the move waiting for the call, or landing",
379
+ || {
380
+ self.mover_waits
381
+ .get()
382
+ .is_some_and(|waits| waits.load(Ordering::SeqCst) > 0)
383
+ || self.moved.load(Ordering::SeqCst)
384
+ },
385
+ );
372
386
  }
373
387
  ops.revalidate(dir, dentry)
374
388
  }
@@ -405,11 +419,11 @@ mod tests {
405
419
  fn a_directory_moved_while_a_call_starts_from_it_stays_reachable_beneath_a_policy() {
406
420
  let tmpfs: Tmpfs = [("p/d/f", "x"), ("q/keep", "x")].into_iter().collect();
407
421
  let (reached, asked) = std::sync::mpsc::channel();
408
- let hook = Arc::new(PausesRevalidate(std::sync::Mutex::new(None)));
422
+ let hook = Arc::new(PausesRevalidate::default());
409
423
  let filesystem = Filesystem::new(Wrap::new(Arc::new(tmpfs), hook.clone()))
410
424
  .expect("the instance must stand");
411
425
  let (cred, held, plain, guard) = held_beneath_a_policy(filesystem);
412
- *hook.0.lock().expect("the sender") = Some(reached);
426
+ *hook.reached.lock().expect("the sender") = Some(reached);
413
427
  let start = held.path().clone();
414
428
  let first = std::thread::scope(|scope| {
415
429
  let first = scope.spawn(|| guard.check(&start));
@@ -417,14 +431,11 @@ mod tests {
417
431
  .recv_timeout(crate::sched::testing::MAX_WAIT)
418
432
  .expect("the call must ask revalidate");
419
433
  let root = Path::root(plain.mnt());
420
- crate::vfs::filename_renameat2(
421
- &restrict_self(&prepare_creds(&[], None), []).expect("every Mount stands"),
422
- &root,
423
- "p/d",
424
- &root,
425
- "q/d",
426
- )
427
- .expect("the move must land");
434
+ let mover = restrict_self(&prepare_creds(&[], None), []).expect("every Mount stands");
435
+ let _ = hook.mover_waits.set(crate::sched::testing::count_waits());
436
+ crate::vfs::filename_renameat2(&mover, &root, "p/d", &root, "q/d")
437
+ .expect("the move must land");
438
+ hook.moved.store(true, std::sync::atomic::Ordering::SeqCst);
428
439
  crate::sched::testing::join_scoped("the call", first)
429
440
  });
430
441
  assert!(