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.
- checksums.yaml +4 -4
- data/.githooks/linux-tests.sh +29 -0
- data/.release-please-manifest.json +1 -1
- data/CHANGELOG.md +460 -0
- data/Cargo.lock +868 -447
- data/README.md +43 -4
- data/ROADMAP.md +1 -2
- data/crates/kobako-runtime/CHANGELOG.md +309 -0
- data/crates/kobako-runtime/Cargo.toml +58 -11
- data/crates/kobako-runtime/README.md +7 -4
- data/crates/kobako-runtime/src/cred.rs +136 -0
- data/crates/kobako-runtime/src/dispatch.rs +4 -5
- data/crates/kobako-runtime/src/endpoint/buffer.rs +153 -44
- data/crates/kobako-runtime/src/endpoint/dispatch.rs +80 -41
- data/crates/kobako-runtime/src/endpoint/invoke.rs +23 -36
- data/crates/kobako-runtime/src/endpoint.rs +137 -36
- data/crates/kobako-runtime/src/errno.rs +391 -0
- data/crates/kobako-runtime/src/error.rs +31 -10
- data/crates/kobako-runtime/src/filesystem/anon_inodes.rs +52 -0
- data/crates/kobako-runtime/src/filesystem/bind/fid.rs +300 -22
- data/crates/kobako-runtime/src/filesystem/bind/lookup.rs +101 -29
- data/crates/kobako-runtime/src/filesystem/bind/unicode.rs +109 -0
- data/crates/kobako-runtime/src/filesystem/bind/walk.rs +52 -34
- data/crates/kobako-runtime/src/filesystem/bind.rs +812 -447
- data/crates/kobako-runtime/src/filesystem/overlay/file.rs +6 -6
- data/crates/kobako-runtime/src/filesystem/overlay/path.rs +43 -65
- data/crates/kobako-runtime/src/filesystem/overlay/stack/copy_up.rs +11 -16
- data/crates/kobako-runtime/src/filesystem/overlay/stack/dir.rs +47 -72
- data/crates/kobako-runtime/src/filesystem/overlay/stack/namei.rs +12 -12
- data/crates/kobako-runtime/src/filesystem/overlay/stack/readdir.rs +4 -7
- data/crates/kobako-runtime/src/filesystem/overlay/stack.rs +120 -37
- data/crates/kobako-runtime/src/filesystem/overlay.rs +578 -388
- data/crates/kobako-runtime/src/filesystem/pipe.rs +12 -9
- data/crates/kobako-runtime/src/filesystem/rdir.rs +14 -50
- data/crates/kobako-runtime/src/filesystem/testing.rs +31 -0
- data/crates/kobako-runtime/src/filesystem/tmpfs/seed.rs +32 -0
- data/crates/kobako-runtime/src/filesystem/tmpfs/tmpfile.rs +3 -3
- data/crates/kobako-runtime/src/filesystem/tmpfs/whiteout.rs +57 -35
- data/crates/kobako-runtime/src/filesystem/tmpfs/xattr.rs +2 -2
- data/crates/kobako-runtime/src/filesystem/tmpfs.rs +219 -173
- data/crates/kobako-runtime/src/filesystem/wrap.rs +36 -52
- data/crates/kobako-runtime/src/filesystem.rs +8 -3
- data/crates/kobako-runtime/src/fork.rs +319 -0
- data/crates/kobako-runtime/src/grant.rs +3 -0
- data/crates/kobako-runtime/src/hashtable.rs +4 -2
- data/crates/kobako-runtime/src/lib.rs +18 -7
- data/crates/kobako-runtime/src/limits.rs +171 -0
- data/crates/kobako-runtime/src/locking/seqlock.rs +29 -3
- data/crates/kobako-runtime/src/locking.rs +518 -113
- data/crates/kobako-runtime/src/memcg.rs +306 -0
- data/crates/kobako-runtime/src/mount.rs +37 -9
- data/crates/kobako-runtime/src/net/backends.rs +43 -0
- data/crates/kobako-runtime/src/net/connections.rs +219 -0
- data/crates/kobako-runtime/src/net/credential.rs +125 -0
- data/crates/kobako-runtime/src/net/egress.rs +180 -0
- data/crates/kobako-runtime/src/net/family.rs +181 -0
- data/crates/kobako-runtime/src/net/http.rs +3165 -0
- data/crates/kobako-runtime/src/net/lookup.rs +385 -0
- data/crates/kobako-runtime/src/net/mock.rs +251 -0
- data/crates/kobako-runtime/src/net/namespace.rs +1368 -0
- data/crates/kobako-runtime/src/net/reactor.rs +626 -0
- data/crates/kobako-runtime/src/net/resolv/darwin.rs +280 -0
- data/crates/kobako-runtime/src/net/resolv/stub/tests.rs +519 -0
- data/crates/kobako-runtime/src/net/resolv/stub.rs +542 -0
- data/crates/kobako-runtime/src/net/resolv/windows.rs +218 -0
- data/crates/kobako-runtime/src/net/resolv.rs +251 -0
- data/crates/kobako-runtime/src/net/skbuff.rs +109 -0
- data/crates/kobako-runtime/src/net/socket.rs +869 -0
- data/crates/kobako-runtime/src/net/task.rs +223 -0
- data/crates/kobako-runtime/src/net/tcp.rs +1840 -0
- data/crates/kobako-runtime/src/net/tls.rs +120 -0
- data/crates/kobako-runtime/src/net/transport.rs +304 -0
- data/crates/kobako-runtime/src/net/udp.rs +1105 -0
- data/crates/kobako-runtime/src/net.rs +45 -0
- data/crates/kobako-runtime/src/network/builder.rs +177 -0
- data/crates/kobako-runtime/src/network/ruleset.rs +777 -0
- data/crates/kobako-runtime/src/network.rs +551 -0
- data/crates/kobako-runtime/src/policy.rs +16 -0
- data/crates/kobako-runtime/src/profile.rs +29 -144
- data/crates/kobako-runtime/src/runtime.rs +3 -5
- data/crates/kobako-runtime/src/sched.rs +338 -51
- data/crates/kobako-runtime/src/security/entry_policy.rs +22 -0
- data/crates/kobako-runtime/src/{vfs/cred.rs → security/hooks.rs} +52 -144
- data/crates/kobako-runtime/src/security/mount_policy.rs +158 -0
- data/crates/kobako-runtime/src/security.rs +125 -209
- data/crates/kobako-runtime/src/snapshot.rs +121 -2
- data/crates/kobako-runtime/src/vfs/attr.rs +5 -5
- data/crates/kobako-runtime/src/vfs/dentry.rs +210 -117
- data/crates/kobako-runtime/src/vfs/file.rs +243 -248
- data/crates/kobako-runtime/src/vfs/inode.rs +41 -45
- data/crates/kobako-runtime/src/vfs/kern_path.rs +62 -53
- data/crates/kobako-runtime/src/vfs/mount.rs +10 -9
- data/crates/kobako-runtime/src/vfs/namei.rs +1162 -818
- data/crates/kobako-runtime/src/vfs/namespace.rs +245 -0
- data/crates/kobako-runtime/src/vfs/open_files.rs +69 -0
- data/crates/kobako-runtime/src/vfs/openat2.rs +3 -3
- data/crates/kobako-runtime/src/vfs/path.rs +6 -6
- data/crates/kobako-runtime/src/vfs/readdir.rs +6 -6
- data/crates/kobako-runtime/src/vfs/stat.rs +248 -23
- data/crates/kobako-runtime/src/vfs/super_block.rs +18 -14
- data/crates/kobako-runtime/src/vfs/utimes.rs +37 -27
- data/crates/kobako-runtime/src/vfs.rs +10 -6
- data/crates/kobako-runtime/src/wait.rs +381 -0
- data/crates/kobako-transport/CHANGELOG.md +38 -0
- data/crates/kobako-transport/Cargo.toml +1 -1
- data/crates/kobako-transport/README.md +3 -4
- data/crates/kobako-transport/src/abi.rs +78 -72
- data/crates/kobako-transport/src/envelope/bytes.rs +17 -6
- data/crates/kobako-transport/src/envelope/call.rs +32 -4
- data/crates/kobako-transport/src/envelope/error_record.rs +21 -0
- data/crates/kobako-transport/src/envelope/name.rs +99 -0
- data/crates/kobako-transport/src/envelope/outcome.rs +9 -9
- data/crates/kobako-transport/src/envelope/preamble.rs +24 -21
- data/crates/kobako-transport/src/envelope/reply.rs +27 -27
- data/crates/kobako-transport/src/envelope/unsupported.rs +10 -10
- data/crates/kobako-transport/src/envelope.rs +13 -8
- data/crates/kobako-wasmtime/CHANGELOG.md +208 -0
- data/crates/kobako-wasmtime/Cargo.toml +47 -7
- data/crates/kobako-wasmtime/README.md +4 -3
- data/crates/kobako-wasmtime/build/preview2.rs +1339 -0
- data/crates/kobako-wasmtime/build.rs +16 -0
- data/crates/kobako-wasmtime/src/abi.rs +18 -37
- data/crates/kobako-wasmtime/src/ambient.rs +43 -19
- data/crates/kobako-wasmtime/src/cache/unix.rs +95 -0
- data/crates/kobako-wasmtime/src/cache/windows.rs +359 -0
- data/crates/kobako-wasmtime/src/cache.rs +106 -113
- data/crates/kobako-wasmtime/src/config.rs +52 -68
- data/crates/kobako-wasmtime/src/driver.rs +80 -82
- data/crates/kobako-wasmtime/src/exports.rs +143 -17
- data/crates/kobako-wasmtime/src/guest.rs +12 -6
- data/crates/kobako-wasmtime/src/guest_binary.rs +14 -0
- data/crates/kobako-wasmtime/src/imports.rs +42 -18
- data/crates/kobako-wasmtime/src/instance_pre.rs +4 -4
- data/crates/kobako-wasmtime/src/invocation.rs +91 -15
- data/crates/kobako-wasmtime/src/lib.rs +7 -3
- data/crates/kobako-wasmtime/src/limiter.rs +130 -12
- data/crates/kobako-wasmtime/src/preview1/descriptors.rs +305 -0
- data/crates/kobako-wasmtime/src/preview1/differential/mount.rs +9 -20
- data/crates/kobako-wasmtime/src/preview1/differential.rs +27 -31
- data/crates/kobako-wasmtime/src/preview1/errno.rs +55 -66
- data/crates/kobako-wasmtime/src/preview1/fd.rs +347 -312
- data/crates/kobako-wasmtime/src/preview1/memory.rs +21 -12
- data/crates/kobako-wasmtime/src/preview1/mount_view/wrap.rs +34 -53
- data/crates/kobako-wasmtime/src/preview1/mount_view.rs +335 -388
- data/crates/kobako-wasmtime/src/preview1/path.rs +56 -121
- data/crates/kobako-wasmtime/src/preview1/poll.rs +255 -125
- data/crates/kobako-wasmtime/src/preview1/random.rs +125 -0
- data/crates/kobako-wasmtime/src/preview1/sock.rs +10 -8
- data/crates/kobako-wasmtime/src/preview1/testing.rs +31 -0
- data/crates/kobako-wasmtime/src/preview1/testsuite.rs +5 -15
- data/crates/kobako-wasmtime/src/preview1.rs +131 -51
- data/crates/kobako-wasmtime/src/preview2/abi.rs +227 -0
- data/crates/kobako-wasmtime/src/preview2/error.rs +118 -0
- data/crates/kobako-wasmtime/src/preview2/filesystem.rs +122 -0
- data/crates/kobako-wasmtime/src/preview2/host.rs +4319 -0
- data/crates/kobako-wasmtime/src/preview2/http.rs +1587 -0
- data/crates/kobako-wasmtime/src/preview2/io.rs +328 -0
- data/crates/kobako-wasmtime/src/preview2/sockets.rs +379 -0
- data/crates/kobako-wasmtime/src/preview2/table.rs +257 -0
- data/crates/kobako-wasmtime/src/preview2.rs +57 -0
- data/crates/kobako-wasmtime/src/trap.rs +88 -28
- data/crates/kobako-wasmtime/src/waits.rs +59 -0
- data/crates/kobako-wasmtime/src/wasi.rs +291 -81
- data/crates/kobako-wasmtime/wit/deps/cli.wit +233 -0
- data/crates/kobako-wasmtime/wit/deps/clocks.wit +162 -0
- data/crates/kobako-wasmtime/wit/deps/filesystem.wit +589 -0
- data/crates/kobako-wasmtime/wit/deps/http.wit +733 -0
- data/crates/kobako-wasmtime/wit/deps/io.wit +299 -0
- data/crates/kobako-wasmtime/wit/deps/random.wit +92 -0
- data/crates/kobako-wasmtime/wit/deps/sockets.wit +949 -0
- data/crates/kobako-wasmtime/witx/wasi_snapshot_preview1.witx +13 -0
- data/data/kobako.wasm +0 -0
- data/ext/kobako/Cargo.toml +6 -2
- data/ext/kobako/src/{runtime/errors.rs → errors.rs} +34 -26
- data/ext/kobako/src/escape.rs +404 -0
- data/ext/kobako/src/{runtime/filesystem → filesystem}/grants.rs +34 -32
- data/ext/kobako/src/{runtime/filesystem → filesystem}/ruby.rs +158 -170
- data/ext/kobako/src/{runtime/filesystem.rs → filesystem.rs} +71 -23
- data/ext/kobako/src/fork.rs +72 -0
- data/ext/kobako/src/gvl.rs +729 -0
- data/ext/kobako/src/handoff.rs +490 -0
- data/ext/kobako/src/lib.rs +74 -3
- data/ext/kobako/src/network/mock.rs +187 -0
- data/ext/kobako/src/network.rs +593 -0
- data/ext/kobako/src/pinned_value.rs +44 -0
- data/ext/kobako/src/runtime/bridge.rs +58 -50
- data/ext/kobako/src/runtime/snapshot.rs +4 -4
- data/ext/kobako/src/runtime.rs +272 -163
- data/ext/kobako/src/wire.rs +42 -0
- data/lib/kobako/catalog/extensions.rb +14 -2
- data/lib/kobako/catalog/flags.rb +3 -5
- data/lib/kobako/catalog/grants.rb +5 -4
- data/lib/kobako/catalog/handles.rb +14 -32
- data/lib/kobako/catalog/mount.rb +11 -3
- data/lib/kobako/catalog/network.rb +35 -0
- data/lib/kobako/catalog/replacements.rb +28 -0
- data/lib/kobako/catalog/services.rb +5 -19
- data/lib/kobako/catalog/snippets.rb +3 -18
- data/lib/kobako/catalog.rb +3 -0
- data/lib/kobako/codec/{utils.rb → boundary.rb} +4 -2
- data/lib/kobako/codec/decoder.rb +5 -5
- data/lib/kobako/codec/ext_types.rb +3 -3
- data/lib/kobako/codec/handle_walk.rb +3 -43
- data/lib/kobako/codec/state.rb +2 -18
- data/lib/kobako/codec/value_budget.rb +133 -41
- data/lib/kobako/codec.rb +6 -7
- data/lib/kobako/context.rb +24 -11
- data/lib/kobako/errors.rb +18 -18
- data/lib/kobako/extension.rb +3 -2
- data/lib/kobako/filesystem.rb +10 -3
- data/lib/kobako/fork.rb +42 -0
- data/lib/kobako/handle.rb +2 -3
- data/lib/kobako/names.rb +28 -0
- data/lib/kobako/network.rb +210 -0
- data/lib/kobako/outcome.rb +7 -7
- data/lib/kobako/runtime.rb +3 -2
- data/lib/kobako/sandbox.rb +73 -35
- data/lib/kobako/sandbox_options.rb +42 -109
- data/lib/kobako/transport/argument_wrap.rb +64 -0
- data/lib/kobako/transport/block_failure.rb +1 -1
- data/lib/kobako/transport/dispatcher.rb +6 -6
- data/lib/kobako/transport/exposure.rb +26 -15
- data/lib/kobako/transport/reflection.rb +21 -4
- data/lib/kobako/transport/run.rb +8 -23
- data/lib/kobako/transport/yielder.rb +2 -9
- data/lib/kobako/transport.rb +1 -0
- data/lib/kobako/usage.rb +4 -2
- data/lib/kobako/version.rb +1 -1
- data/lib/kobako.rb +8 -4
- data/release-please-config.json +15 -0
- data/sig/kobako/catalog/extensions.rbs +2 -0
- data/sig/kobako/catalog/grants.rbs +7 -4
- data/sig/kobako/catalog/handles.rbs +2 -2
- data/sig/kobako/catalog/mount.rbs +3 -1
- data/sig/kobako/catalog/network.rbs +20 -0
- data/sig/kobako/catalog/replacements.rbs +14 -0
- data/sig/kobako/catalog/services.rbs +0 -4
- data/sig/kobako/catalog/snippets.rbs +0 -2
- data/sig/kobako/codec/{utils.rbs → boundary.rbs} +2 -2
- data/sig/kobako/codec/handle_walk.rbs +0 -6
- data/sig/kobako/codec/state.rbs +0 -4
- data/sig/kobako/codec/value_budget.rbs +48 -5
- data/sig/kobako/codec.rbs +2 -2
- data/sig/kobako/context.rbs +3 -1
- data/sig/kobako/errors.rbs +4 -7
- data/sig/kobako/filesystem.rbs +21 -3
- data/sig/kobako/fork.rbs +17 -0
- data/sig/kobako/network.rbs +115 -0
- data/sig/kobako/outcome.rbs +1 -1
- data/sig/kobako/runtime.rbs +32 -3
- data/sig/kobako/sandbox.rbs +10 -2
- data/sig/kobako/sandbox_options.rbs +29 -40
- data/sig/kobako/transport/argument_wrap.rbs +15 -0
- data/sig/kobako/transport/error.rbs +1 -1
- data/sig/kobako/transport/exposure.rbs +2 -4
- data/sig/kobako/transport/reflection.rbs +10 -0
- data/sig/kobako/transport/run.rbs +0 -2
- data/sig/kobako/transport/yielder.rbs +0 -3
- data/sig/kobako/transport.rbs +1 -1
- data/sig/kobako.rbs +13 -0
- metadata +97 -37
- data/crates/kobako-runtime/src/endpoint/capture.rs +0 -115
- data/crates/kobako-runtime/src/endpoint/memory_cap.rs +0 -238
- data/crates/kobako-runtime/src/filesystem/path.rs +0 -21
- data/crates/kobako-runtime/src/vfs/errno.rs +0 -153
- data/crates/kobako-runtime/tests/linux_baseline/calls.rs +0 -171
- data/crates/kobako-runtime/tests/linux_baseline/concurrent.rs +0 -276
- data/crates/kobako-runtime/tests/linux_baseline/kobako.rs +0 -323
- data/crates/kobako-runtime/tests/linux_baseline/linux-baseline-expected.txt +0 -16
- data/crates/kobako-runtime/tests/linux_baseline/linux.rs +0 -449
- data/crates/kobako-runtime/tests/linux_baseline/main.rs +0 -611
- data/crates/kobako-runtime/tests/linux_baseline/permissions.rs +0 -236
- data/crates/kobako-wasmtime/src/descriptors.rs +0 -200
- data/crates/kobako-wasmtime/tests/integration/compile_posture.rs +0 -43
- data/crates/kobako-wasmtime/tests/integration/guest_file.rs +0 -1085
- data/crates/kobako-wasmtime/tests/integration/main.rs +0 -29
- data/crates/kobako-wasmtime/tests/integration/preamble_flags.rs +0 -70
- data/crates/kobako-wasmtime/tests/integration/snippet_compile.rs +0 -95
- data/crates/kobako-wasmtime/tests/integration/support/mod.rs +0 -30
- data/crates/kobako-wasmtime/tests/integration/unanswered_call.rs +0 -86
- data/crates/kobako-wasmtime/tests/integration/unsupported_call.rs +0 -51
- data/ext/kobako/src/runtime/escape.rs +0 -67
- data/ext/kobako/src/runtime/gvl.rs +0 -147
- data/ext/kobako/src/runtime/pinned_value.rs +0 -31
- data/lib/kobako/constant_name.rb +0 -8
- data/lib/kobako/pool.rb +0 -140
- data/sig/kobako/constant_name.rbs +0 -3
- 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
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
///
|
|
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
|
-
|
|
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
|
-
|
|
46
|
-
|
|
47
|
-
|
|
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
|
-
|
|
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
|
-
///
|
|
71
|
-
/// to
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
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
|
-
|
|
82
|
-
|
|
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
|
-
|
|
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
|
-
|
|
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
|
-
|
|
107
|
-
let
|
|
108
|
-
|
|
109
|
-
|
|
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
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
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
|
-
|
|
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`,
|
|
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::
|
|
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
|
+
}
|