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.
- checksums.yaml +4 -4
- data/.githooks/linux-tests.sh +29 -0
- data/.release-please-manifest.json +1 -1
- data/CHANGELOG.md +125 -0
- data/Cargo.lock +457 -494
- data/README.md +2 -3
- data/ROADMAP.md +0 -1
- data/crates/kobako-runtime/CHANGELOG.md +90 -0
- data/crates/kobako-runtime/Cargo.toml +14 -15
- data/crates/kobako-runtime/README.md +1 -1
- data/crates/kobako-runtime/src/cred.rs +32 -20
- data/crates/kobako-runtime/src/filesystem/bind/fid.rs +300 -22
- data/crates/kobako-runtime/src/filesystem/bind/lookup.rs +56 -22
- data/crates/kobako-runtime/src/filesystem/bind/unicode.rs +109 -0
- data/crates/kobako-runtime/src/filesystem/bind/walk.rs +33 -14
- data/crates/kobako-runtime/src/filesystem/bind.rs +393 -220
- data/crates/kobako-runtime/src/filesystem/overlay/stack/copy_up.rs +2 -0
- data/crates/kobako-runtime/src/filesystem/overlay/stack.rs +9 -10
- data/crates/kobako-runtime/src/filesystem/overlay.rs +277 -46
- data/crates/kobako-runtime/src/filesystem/rdir.rs +8 -27
- data/crates/kobako-runtime/src/filesystem/testing.rs +31 -0
- data/crates/kobako-runtime/src/filesystem/tmpfs.rs +47 -13
- data/crates/kobako-runtime/src/filesystem.rs +2 -0
- data/crates/kobako-runtime/src/fork.rs +239 -16
- data/crates/kobako-runtime/src/limits.rs +17 -41
- data/crates/kobako-runtime/src/locking/seqlock.rs +29 -3
- data/crates/kobako-runtime/src/locking.rs +508 -113
- data/crates/kobako-runtime/src/memcg.rs +83 -160
- data/crates/kobako-runtime/src/mount.rs +0 -30
- data/crates/kobako-runtime/src/net/connections.rs +53 -13
- data/crates/kobako-runtime/src/net/egress.rs +44 -24
- data/crates/kobako-runtime/src/net/http.rs +888 -456
- data/crates/kobako-runtime/src/net/lookup.rs +150 -50
- data/crates/kobako-runtime/src/net/namespace.rs +97 -95
- data/crates/kobako-runtime/src/net/reactor.rs +298 -203
- data/crates/kobako-runtime/src/net/resolv/darwin.rs +8 -3
- data/crates/kobako-runtime/src/net/resolv/stub/tests.rs +50 -0
- data/crates/kobako-runtime/src/net/resolv/stub.rs +29 -23
- data/crates/kobako-runtime/src/net/resolv/windows.rs +14 -44
- data/crates/kobako-runtime/src/net/resolv.rs +39 -15
- data/crates/kobako-runtime/src/net/skbuff.rs +109 -0
- data/crates/kobako-runtime/src/net/socket.rs +51 -52
- data/crates/kobako-runtime/src/net/task.rs +223 -0
- data/crates/kobako-runtime/src/net/tcp.rs +410 -158
- data/crates/kobako-runtime/src/net/tls.rs +65 -3
- data/crates/kobako-runtime/src/net/transport.rs +72 -47
- data/crates/kobako-runtime/src/net/udp.rs +231 -129
- data/crates/kobako-runtime/src/net.rs +3 -0
- data/crates/kobako-runtime/src/sched.rs +244 -58
- data/crates/kobako-runtime/src/security.rs +27 -16
- data/crates/kobako-runtime/src/vfs/dentry.rs +80 -41
- data/crates/kobako-runtime/src/vfs/file.rs +52 -53
- data/crates/kobako-runtime/src/vfs/kern_path.rs +24 -11
- data/crates/kobako-runtime/src/vfs/namei.rs +399 -184
- data/crates/kobako-runtime/src/vfs/namespace.rs +123 -40
- data/crates/kobako-runtime/src/vfs/stat.rs +105 -16
- data/crates/kobako-runtime/src/vfs.rs +1 -0
- data/crates/kobako-runtime/src/wait.rs +75 -50
- data/crates/kobako-transport/CHANGELOG.md +7 -0
- data/crates/kobako-transport/Cargo.toml +1 -1
- data/crates/kobako-transport/README.md +1 -1
- data/crates/kobako-wasmtime/CHANGELOG.md +47 -0
- data/crates/kobako-wasmtime/Cargo.toml +27 -6
- data/crates/kobako-wasmtime/README.md +3 -3
- data/crates/kobako-wasmtime/src/abi.rs +2 -1
- data/crates/kobako-wasmtime/src/ambient.rs +11 -2
- 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 +88 -103
- data/crates/kobako-wasmtime/src/driver.rs +40 -27
- data/crates/kobako-wasmtime/src/invocation.rs +21 -7
- data/crates/kobako-wasmtime/src/lib.rs +2 -0
- data/crates/kobako-wasmtime/src/limiter.rs +78 -25
- data/crates/kobako-wasmtime/src/preview1/descriptors.rs +13 -17
- data/crates/kobako-wasmtime/src/preview1/differential/mount.rs +4 -5
- data/crates/kobako-wasmtime/src/preview1/differential.rs +1 -1
- data/crates/kobako-wasmtime/src/preview1/fd.rs +23 -19
- data/crates/kobako-wasmtime/src/preview1/mount_view.rs +130 -197
- data/crates/kobako-wasmtime/src/preview1/path.rs +3 -3
- data/crates/kobako-wasmtime/src/preview1/poll.rs +28 -22
- data/crates/kobako-wasmtime/src/preview1/random.rs +125 -0
- data/crates/kobako-wasmtime/src/preview1/testing.rs +31 -0
- data/crates/kobako-wasmtime/src/preview1.rs +85 -9
- data/crates/kobako-wasmtime/src/preview2/filesystem.rs +2 -2
- data/crates/kobako-wasmtime/src/preview2/host.rs +609 -249
- data/crates/kobako-wasmtime/src/preview2/http.rs +299 -215
- data/crates/kobako-wasmtime/src/preview2/io.rs +17 -4
- data/crates/kobako-wasmtime/src/preview2/table.rs +24 -72
- data/crates/kobako-wasmtime/src/trap.rs +68 -23
- data/crates/kobako-wasmtime/src/waits.rs +59 -0
- data/crates/kobako-wasmtime/src/wasi.rs +91 -10
- 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/escape.rs +332 -26
- data/ext/kobako/src/filesystem/grants.rs +5 -7
- data/ext/kobako/src/filesystem/ruby.rs +10 -3
- data/ext/kobako/src/filesystem.rs +47 -8
- data/ext/kobako/src/fork.rs +72 -0
- data/ext/kobako/src/gvl.rs +499 -76
- data/ext/kobako/src/handoff.rs +490 -0
- data/ext/kobako/src/lib.rs +10 -0
- data/ext/kobako/src/network.rs +5 -3
- data/ext/kobako/src/pinned_value.rs +28 -15
- data/ext/kobako/src/runtime/bridge.rs +32 -25
- data/ext/kobako/src/runtime.rs +74 -43
- data/lib/kobako/catalog/handles.rb +9 -5
- data/lib/kobako/codec/decoder.rb +1 -1
- data/lib/kobako/codec/state.rb +2 -18
- data/lib/kobako/codec/value_budget.rb +4 -4
- data/lib/kobako/codec.rb +5 -6
- data/lib/kobako/errors.rb +4 -7
- data/lib/kobako/filesystem.rb +1 -1
- data/lib/kobako/fork.rb +42 -0
- data/lib/kobako/handle.rb +1 -2
- data/lib/kobako/sandbox.rb +17 -36
- data/lib/kobako/sandbox_options.rb +7 -9
- data/lib/kobako/usage.rb +4 -2
- data/lib/kobako/version.rb +1 -1
- data/lib/kobako.rb +2 -2
- data/sig/kobako/catalog/handles.rbs +2 -0
- data/sig/kobako/codec/state.rbs +0 -4
- data/sig/kobako/codec/value_budget.rbs +1 -1
- data/sig/kobako/codec.rbs +2 -2
- data/sig/kobako/errors.rbs +0 -3
- data/sig/kobako/fork.rbs +17 -0
- data/sig/kobako/runtime.rbs +2 -2
- data/sig/kobako/sandbox.rbs +3 -12
- data/sig/kobako/sandbox_options.rbs +0 -2
- metadata +15 -4
- data/lib/kobako/pool.rb +0 -162
- data/sig/core/thread.rbs +0 -6
- data/sig/kobako/pool.rbs +0 -46
|
@@ -5,7 +5,7 @@ use std::sync::atomic::{AtomicBool, Ordering};
|
|
|
5
5
|
use std::sync::{Arc, Mutex, MutexGuard, PoisonError};
|
|
6
6
|
use std::time::Instant;
|
|
7
7
|
|
|
8
|
-
use kobako_runtime::net::{Exchange, TcpSock};
|
|
8
|
+
use kobako_runtime::net::{Exchange, RequestBody, TcpSock};
|
|
9
9
|
use kobako_runtime::profile::Profile;
|
|
10
10
|
use kobako_runtime::vfs::{File, PollMask};
|
|
11
11
|
use kobako_runtime::wait::PollTable;
|
|
@@ -65,11 +65,13 @@ impl<S> SocketFile<S> {
|
|
|
65
65
|
}
|
|
66
66
|
|
|
67
67
|
/// What an `input-stream` or an `output-stream` reads or writes: a socket's
|
|
68
|
-
/// file, whose socket holds what was written until the host takes it
|
|
68
|
+
/// file, whose socket holds what was written until the host takes it; a
|
|
69
|
+
/// request body's file, whose buffer holds it until the seam takes it; or any
|
|
69
70
|
/// other file.
|
|
70
71
|
#[derive(Clone)]
|
|
71
72
|
pub(crate) enum Stream {
|
|
72
73
|
Socket(SocketFile<TcpSock>),
|
|
74
|
+
RequestBody(Arc<RequestBody>, SharedFile),
|
|
73
75
|
File(SharedFile),
|
|
74
76
|
}
|
|
75
77
|
|
|
@@ -86,7 +88,7 @@ impl Stream {
|
|
|
86
88
|
pub(crate) fn shared_file(&self) -> SharedFile {
|
|
87
89
|
match self {
|
|
88
90
|
Stream::Socket(socket) => socket.shared_file(),
|
|
89
|
-
Stream::File(file) => file.clone(),
|
|
91
|
+
Stream::RequestBody(_, file) | Stream::File(file) => file.clone(),
|
|
90
92
|
}
|
|
91
93
|
}
|
|
92
94
|
|
|
@@ -94,7 +96,7 @@ impl Stream {
|
|
|
94
96
|
pub(crate) fn lock(&self) -> MutexGuard<'_, File> {
|
|
95
97
|
match self {
|
|
96
98
|
Stream::Socket(socket) => socket.file(),
|
|
97
|
-
Stream::File(file) => lock(file),
|
|
99
|
+
Stream::RequestBody(_, file) | Stream::File(file) => lock(file),
|
|
98
100
|
}
|
|
99
101
|
}
|
|
100
102
|
|
|
@@ -109,10 +111,20 @@ impl Stream {
|
|
|
109
111
|
self.lock().write(buf)
|
|
110
112
|
}
|
|
111
113
|
|
|
114
|
+
/// Write all of `buf`, which `room` promised, or fail with none of it
|
|
115
|
+
/// written. A file takes any write whole.
|
|
116
|
+
pub(crate) fn write_whole(&self, buf: &[u8]) -> Result<(), Errno> {
|
|
117
|
+
match self {
|
|
118
|
+
Stream::Socket(socket) => socket.sock.send_whole(buf),
|
|
119
|
+
Stream::RequestBody(..) | Stream::File(_) => self.write(buf).map(drop),
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
|
|
112
123
|
/// How many bytes a write takes now.
|
|
113
124
|
pub(crate) fn room(&self) -> usize {
|
|
114
125
|
match self {
|
|
115
126
|
Stream::Socket(socket) => socket.sock.send_room(),
|
|
127
|
+
Stream::RequestBody(body, _) => body.room(),
|
|
116
128
|
// A file takes any write, so it offers what wasmtime-wasi offers
|
|
117
129
|
// a file's output stream.
|
|
118
130
|
Stream::File(_) => MAX_READ_SIZE_ALLOC,
|
|
@@ -123,6 +135,7 @@ impl Stream {
|
|
|
123
135
|
pub(crate) fn is_drained(&self) -> bool {
|
|
124
136
|
match self {
|
|
125
137
|
Stream::Socket(socket) => socket.sock.is_drained(),
|
|
138
|
+
Stream::RequestBody(body, _) => body.is_drained(),
|
|
126
139
|
Stream::File(_) => true,
|
|
127
140
|
}
|
|
128
141
|
}
|
|
@@ -1,15 +1,11 @@
|
|
|
1
1
|
//! The resource table preview2 hands the guest its handles from, as
|
|
2
2
|
//! wasmtime's `ResourceTable` is: it numbers its own, apart from preview1's
|
|
3
3
|
//! descriptors, holds every file either encoding opens and counts each
|
|
4
|
-
//! against the open files, and
|
|
5
|
-
//! Invocation's memory, as Linux's `filp` cache is accounted to the memory
|
|
6
|
-
//! cgroup of the task that allocates it.
|
|
4
|
+
//! against the open files, and holds at most `MAX_TABLE_ENTRIES` entries.
|
|
7
5
|
|
|
8
6
|
use std::any::Any;
|
|
9
7
|
use std::collections::BTreeMap;
|
|
10
|
-
use std::sync::Arc;
|
|
11
8
|
|
|
12
|
-
use kobako_runtime::memcg::{MemoryBudget, MemoryCharge};
|
|
13
9
|
use kobako_runtime::vfs::OpenFiles;
|
|
14
10
|
use kobako_runtime::Errno;
|
|
15
11
|
|
|
@@ -28,11 +24,6 @@ pub(crate) trait Handle: Copy {
|
|
|
28
24
|
/// `ResourceTable::max_capacity` bounds a table.
|
|
29
25
|
pub(crate) const MAX_TABLE_ENTRIES: usize = 1_000_000;
|
|
30
26
|
|
|
31
|
-
/// The memory one entry holding a resource of kind `H` takes.
|
|
32
|
-
fn bytes_of<H: Handle>() -> usize {
|
|
33
|
-
size_of::<Entry>() + size_of::<H::Host>()
|
|
34
|
-
}
|
|
35
|
-
|
|
36
27
|
/// One handle's resource, and the handle it is a child of, as wasmtime's
|
|
37
28
|
/// `ResourceTable` keeps each entry's parent.
|
|
38
29
|
struct Entry {
|
|
@@ -45,18 +36,12 @@ struct Entry {
|
|
|
45
36
|
/// Whether it counts against the open files, as a host-side producer
|
|
46
37
|
/// does and a view does not.
|
|
47
38
|
counted: bool,
|
|
48
|
-
/// What the entry and its resource hold, against the Invocation's
|
|
49
|
-
/// memory, as each `struct file` is charged through `SLAB_ACCOUNT`.
|
|
50
|
-
_memory: MemoryCharge,
|
|
51
39
|
}
|
|
52
40
|
|
|
53
41
|
/// The room one file takes in the table before it opens, as Linux's
|
|
54
|
-
/// `get_unused_fd_flags` counts it
|
|
55
|
-
/// the
|
|
56
|
-
pub(crate) struct FileRoom
|
|
57
|
-
count: FileCount,
|
|
58
|
-
memory: MemoryCharge,
|
|
59
|
-
}
|
|
42
|
+
/// `get_unused_fd_flags` counts it before the walk. Dropped unused, it gives
|
|
43
|
+
/// the count back.
|
|
44
|
+
pub(crate) struct FileRoom(FileCount);
|
|
60
45
|
|
|
61
46
|
/// One file counted against the open files until the table takes it over.
|
|
62
47
|
struct FileCount(Option<OpenFiles>);
|
|
@@ -77,73 +62,49 @@ pub(crate) struct Table {
|
|
|
77
62
|
/// The number past every one handed out so far.
|
|
78
63
|
next: u32,
|
|
79
64
|
open_files: OpenFiles,
|
|
80
|
-
memory: Arc<MemoryBudget>,
|
|
81
65
|
}
|
|
82
66
|
|
|
83
67
|
impl Table {
|
|
84
|
-
/// A table holding nothing, counting its files against `open_files
|
|
85
|
-
///
|
|
86
|
-
|
|
87
|
-
pub(crate) fn new(open_files: OpenFiles, memory: Arc<MemoryBudget>) -> Self {
|
|
68
|
+
/// A table holding nothing, counting its files against `open_files`.
|
|
69
|
+
/// Numbers start at 1, as the Canonical ABI keeps 0 for no handle.
|
|
70
|
+
pub(crate) fn new(open_files: OpenFiles) -> Self {
|
|
88
71
|
Self {
|
|
89
72
|
entries: BTreeMap::new(),
|
|
90
73
|
free: Vec::new(),
|
|
91
74
|
next: 1,
|
|
92
75
|
open_files,
|
|
93
|
-
memory,
|
|
94
76
|
}
|
|
95
77
|
}
|
|
96
78
|
|
|
97
79
|
/// The room a host-side producer's file takes, as a socket's, a
|
|
98
80
|
/// lookup's, or an exchange's does: none past the open file limit, which
|
|
99
81
|
/// each WIT answers with its own code, as Linux's `socket()` answers
|
|
100
|
-
/// EMFILE
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
match self.reserve_file::<H>() {
|
|
104
|
-
Ok(room) => Ok(Some(room)),
|
|
105
|
-
Err(Errno::OutOfMemory) => Err(wasmtime::Error::msg(
|
|
106
|
-
"the guest's resources outgrow its memory",
|
|
107
|
-
)),
|
|
108
|
-
Err(_) => Ok(None),
|
|
109
|
-
}
|
|
82
|
+
/// EMFILE.
|
|
83
|
+
pub(crate) fn reserve_producer(&self) -> Option<FileRoom> {
|
|
84
|
+
self.reserve_file().ok()
|
|
110
85
|
}
|
|
111
86
|
|
|
112
|
-
/// The room a file
|
|
113
|
-
///
|
|
114
|
-
|
|
115
|
-
pub(crate) fn reserve_file<H: Handle>(&self) -> Result<FileRoom, Errno> {
|
|
116
|
-
self.reserve(bytes_of::<H>())
|
|
117
|
-
}
|
|
118
|
-
|
|
119
|
-
/// The room a file the Driver opened before the guest runs takes: counted
|
|
120
|
-
/// against the open files, and charged to none of the guest's memory, as
|
|
121
|
-
/// Linux charges a `struct file` to the task that allocates it.
|
|
122
|
-
pub(crate) fn reserve_inherited_file(&self) -> Result<FileRoom, Errno> {
|
|
123
|
-
self.reserve(0)
|
|
124
|
-
}
|
|
125
|
-
|
|
126
|
-
fn reserve(&self, bytes: usize) -> Result<FileRoom, Errno> {
|
|
87
|
+
/// The room a file takes, refused as EMFILE past the open file limit or
|
|
88
|
+
/// a full table, before the file opens.
|
|
89
|
+
pub(crate) fn reserve_file(&self) -> Result<FileRoom, Errno> {
|
|
127
90
|
if self.is_full() {
|
|
128
91
|
return Err(Errno::TooManyOpenFiles);
|
|
129
92
|
}
|
|
130
93
|
self.open_files.open()?;
|
|
131
|
-
|
|
132
|
-
let memory = MemoryCharge::new(&self.memory, bytes)?;
|
|
133
|
-
Ok(FileRoom { count, memory })
|
|
94
|
+
Ok(FileRoom(FileCount(Some(self.open_files.clone()))))
|
|
134
95
|
}
|
|
135
96
|
|
|
136
97
|
/// Hand out a handle for `resource`, a file opened into `room`.
|
|
137
98
|
pub(crate) fn install<H: Handle>(&mut self, room: FileRoom, resource: H::Host) -> H {
|
|
138
|
-
let FileRoom
|
|
99
|
+
let FileRoom(mut count) = room;
|
|
139
100
|
count.0 = None;
|
|
140
|
-
H::new(self.place(Box::new(resource), None, true
|
|
101
|
+
H::new(self.place(Box::new(resource), None, true))
|
|
141
102
|
}
|
|
142
103
|
|
|
143
104
|
/// Hand out a handle for `resource`, a view that holds no file of its
|
|
144
105
|
/// own; refused past `MAX_TABLE_ENTRIES`.
|
|
145
106
|
pub(crate) fn push<H: Handle>(&mut self, resource: H::Host) -> wasmtime::Result<H> {
|
|
146
|
-
self.insert
|
|
107
|
+
self.insert(Box::new(resource), None).map(H::new)
|
|
147
108
|
}
|
|
148
109
|
|
|
149
110
|
/// Hand out a handle for `resource` as a child of `parent`, which cannot
|
|
@@ -155,7 +116,7 @@ impl Table {
|
|
|
155
116
|
) -> wasmtime::Result<H> {
|
|
156
117
|
let parent = parent.index();
|
|
157
118
|
self.entry(parent)?;
|
|
158
|
-
let handle = self.insert
|
|
119
|
+
let handle = self.insert(Box::new(resource), Some(parent))?;
|
|
159
120
|
self.entries
|
|
160
121
|
.get_mut(&parent)
|
|
161
122
|
.expect("the parent was just found")
|
|
@@ -164,11 +125,11 @@ impl Table {
|
|
|
164
125
|
}
|
|
165
126
|
|
|
166
127
|
/// Number `resource` from the free list, else past every number handed
|
|
167
|
-
/// out; refused past `MAX_TABLE_ENTRIES
|
|
168
|
-
///
|
|
169
|
-
fn insert
|
|
128
|
+
/// out; refused past `MAX_TABLE_ENTRIES` as a trap, since no WIT
|
|
129
|
+
/// constructor has an error for it.
|
|
130
|
+
fn insert(
|
|
170
131
|
&mut self,
|
|
171
|
-
resource:
|
|
132
|
+
resource: Box<dyn Any + Send>,
|
|
172
133
|
parent: Option<u32>,
|
|
173
134
|
) -> wasmtime::Result<u32> {
|
|
174
135
|
if self.is_full() {
|
|
@@ -176,9 +137,7 @@ impl Table {
|
|
|
176
137
|
"the guest holds as many resources as a table may",
|
|
177
138
|
));
|
|
178
139
|
}
|
|
179
|
-
|
|
180
|
-
.map_err(|_| wasmtime::Error::msg("the guest's resources outgrow its memory"))?;
|
|
181
|
-
Ok(self.place(Box::new(resource), parent, false, memory))
|
|
140
|
+
Ok(self.place(resource, parent, false))
|
|
182
141
|
}
|
|
183
142
|
|
|
184
143
|
/// Whether the table holds as many entries as it may.
|
|
@@ -188,13 +147,7 @@ impl Table {
|
|
|
188
147
|
|
|
189
148
|
/// Number `resource` from the free list, else past every number handed
|
|
190
149
|
/// out.
|
|
191
|
-
fn place(
|
|
192
|
-
&mut self,
|
|
193
|
-
resource: Box<dyn Any + Send>,
|
|
194
|
-
parent: Option<u32>,
|
|
195
|
-
counted: bool,
|
|
196
|
-
memory: MemoryCharge,
|
|
197
|
-
) -> u32 {
|
|
150
|
+
fn place(&mut self, resource: Box<dyn Any + Send>, parent: Option<u32>, counted: bool) -> u32 {
|
|
198
151
|
let handle = self.free.pop().unwrap_or_else(|| {
|
|
199
152
|
self.next += 1;
|
|
200
153
|
self.next - 1
|
|
@@ -205,7 +158,6 @@ impl Table {
|
|
|
205
158
|
children: 0,
|
|
206
159
|
lends: 0,
|
|
207
160
|
counted,
|
|
208
|
-
_memory: memory,
|
|
209
161
|
};
|
|
210
162
|
self.entries.insert(handle, entry);
|
|
211
163
|
handle
|
|
@@ -13,8 +13,12 @@ use std::time::{Duration, Instant};
|
|
|
13
13
|
use wasmtime::{Store, StoreHookState, UpdateDeadline};
|
|
14
14
|
|
|
15
15
|
use crate::invocation::Invocation;
|
|
16
|
+
use crate::limiter::MemoryLimiter;
|
|
16
17
|
use kobako_runtime::error::{SetupError, Trap};
|
|
17
18
|
use kobako_runtime::memcg::MemoryLimitExceeded;
|
|
19
|
+
use kobako_runtime::sched::{self, FatalSignal};
|
|
20
|
+
use kobako_runtime::snapshot::Completion;
|
|
21
|
+
use kobako_transport::envelope::Outcome;
|
|
18
22
|
|
|
19
23
|
/// Marker error returned from the epoch-deadline callback when the
|
|
20
24
|
/// wall-clock deadline is exceeded. Downcast from the wasmtime trap
|
|
@@ -30,41 +34,59 @@ impl std::fmt::Display for TimeoutTrap {
|
|
|
30
34
|
|
|
31
35
|
impl std::error::Error for TimeoutTrap {}
|
|
32
36
|
|
|
33
|
-
/// Marker error a
|
|
34
|
-
///
|
|
35
|
-
///
|
|
37
|
+
/// Marker error a run ends with when its frontend interrupts it before the
|
|
38
|
+
/// deadline: a preview1 wait answers it EINTR, as a signal ends Linux's
|
|
39
|
+
/// `poll`; preview2, which has no EINTR, traps with it, and so does a guest
|
|
40
|
+
/// that only computes, at the next tick.
|
|
36
41
|
#[derive(Debug)]
|
|
37
42
|
pub(crate) struct Interrupted;
|
|
38
43
|
|
|
39
44
|
impl std::fmt::Display for Interrupted {
|
|
40
45
|
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
|
41
|
-
write!(f, "the guest
|
|
46
|
+
write!(f, "the guest was interrupted")
|
|
42
47
|
}
|
|
43
48
|
}
|
|
44
49
|
|
|
45
50
|
impl std::error::Error for Interrupted {}
|
|
46
51
|
|
|
47
|
-
/// Epoch delta that keeps
|
|
48
|
-
///
|
|
49
|
-
/// `u64::MAX`: wasmtime adds the delta to the engine's current
|
|
50
|
-
/// which the process-wide ticker advances for the engine's whole
|
|
52
|
+
/// Epoch delta that keeps a test's Store, which installs no deadline
|
|
53
|
+
/// callback, from ever reaching its epoch deadline. Half the epoch range
|
|
54
|
+
/// rather than `u64::MAX`: wasmtime adds the delta to the engine's current
|
|
55
|
+
/// epoch, which the process-wide ticker advances for the engine's whole
|
|
51
56
|
/// lifetime, so the full range overflows the sum (a panic under debug
|
|
52
57
|
/// overflow checks).
|
|
58
|
+
#[cfg(test)]
|
|
53
59
|
pub(crate) const NO_TIMEOUT_EPOCH_DELTA: u64 = u64::MAX / 2;
|
|
54
60
|
|
|
55
|
-
///
|
|
56
|
-
///
|
|
57
|
-
///
|
|
61
|
+
/// Each tick, the guest's run takes its pending signal and stops on a fatal
|
|
62
|
+
/// one, as a Linux task does on its way back to user space: past the
|
|
63
|
+
/// deadline it traps as a timeout, interrupted it traps as `Interrupted`.
|
|
58
64
|
pub(crate) fn epoch_deadline_callback(
|
|
59
65
|
ctx: StoreHookState<'_, Invocation>,
|
|
60
66
|
) -> wasmtime::Result<UpdateDeadline> {
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
67
|
+
if ctx
|
|
68
|
+
.data()
|
|
69
|
+
.deadline()
|
|
70
|
+
.is_some_and(|deadline| Instant::now() >= deadline)
|
|
71
|
+
{
|
|
72
|
+
return Err(wasmtime::Error::new(TimeoutTrap));
|
|
73
|
+
}
|
|
74
|
+
match fatal_signal_trap() {
|
|
75
|
+
Some(trap) => Err(trap),
|
|
76
|
+
None => Ok(UpdateDeadline::Continue(1)),
|
|
65
77
|
}
|
|
66
78
|
}
|
|
67
79
|
|
|
80
|
+
/// The trap ending the calling thread's run once it takes its pending signal
|
|
81
|
+
/// and finds a fatal one, as the guest's code meets on each tick and a long
|
|
82
|
+
/// host call meets between its steps.
|
|
83
|
+
pub(crate) fn fatal_signal_trap() -> Option<wasmtime::Error> {
|
|
84
|
+
sched::exit_to_user_mode().map(|signal| match signal {
|
|
85
|
+
FatalSignal::Deadline => wasmtime::Error::new(TimeoutTrap),
|
|
86
|
+
FatalSignal::Interrupt => wasmtime::Error::new(Interrupted),
|
|
87
|
+
})
|
|
88
|
+
}
|
|
89
|
+
|
|
68
90
|
/// The message leaves out the ABI export symbol, so the Sandbox layer can
|
|
69
91
|
/// attach the caller's verb instead. A cap trap carries its own message,
|
|
70
92
|
/// since wasmtime's backtrace framing is noise there.
|
|
@@ -72,19 +94,42 @@ pub(crate) fn to_trap(err: wasmtime::Error) -> Trap {
|
|
|
72
94
|
cap_trap_of(&err).unwrap_or_else(|| Trap::Other(other_trap_message(&err)))
|
|
73
95
|
}
|
|
74
96
|
|
|
75
|
-
/// Arm a Store's wall-clock deadline `limit` from now, or none.
|
|
76
|
-
///
|
|
77
|
-
///
|
|
97
|
+
/// Arm a Store's wall-clock deadline `limit` from now, or none. The callback
|
|
98
|
+
/// runs every tick either way, as an unbounded run still stops on a fatal
|
|
99
|
+
/// signal.
|
|
78
100
|
pub(crate) fn arm_deadline(store: &mut Store<Invocation>, limit: Option<Duration>) {
|
|
79
101
|
store.epoch_deadline_callback(epoch_deadline_callback);
|
|
80
102
|
store
|
|
81
103
|
.data_mut()
|
|
82
104
|
.set_deadline(limit.map(|limit| Instant::now() + limit));
|
|
83
|
-
store.set_epoch_deadline(
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
105
|
+
store.set_epoch_deadline(1);
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
/// The class a guest's interpreter raises once its allocator answers no
|
|
109
|
+
/// room, as mruby's and CRuby's do.
|
|
110
|
+
const OUT_OF_MEMORY: &str = "NoMemoryError";
|
|
111
|
+
|
|
112
|
+
/// `trap` ending a run, as `limiter` attributes it: once the budget refused
|
|
113
|
+
/// a growth, a run that fails for another reason than its deadline failed
|
|
114
|
+
/// for want of the room, as an allocator with none traps or a GC heap
|
|
115
|
+
/// allocation fails.
|
|
116
|
+
pub(crate) fn charged_to_budget(trap: Trap, limiter: &MemoryLimiter) -> Trap {
|
|
117
|
+
match (limiter.refused(), trap) {
|
|
118
|
+
(Some(refused), Trap::Other(_)) => Trap::MemoryLimit(refused.to_string()),
|
|
119
|
+
(_, trap) => trap,
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
/// What `outcome` completes a run as: a guest that ended raising
|
|
124
|
+
/// `NoMemoryError` after the budget refused a growth failed as the budget's,
|
|
125
|
+
/// while one that collected and went on answers its own outcome.
|
|
126
|
+
pub(crate) fn completion_of(outcome: Outcome, limiter: &MemoryLimiter) -> Completion {
|
|
127
|
+
match (&outcome, limiter.refused()) {
|
|
128
|
+
(Outcome::Panic(panic), Some(refused)) if panic.error.name == OUT_OF_MEMORY => {
|
|
129
|
+
Completion::Trap(Trap::MemoryLimit(refused.to_string()))
|
|
130
|
+
}
|
|
131
|
+
_ => Completion::Outcome(outcome),
|
|
132
|
+
}
|
|
88
133
|
}
|
|
89
134
|
|
|
90
135
|
/// The cap trap `err` carries, if a cap fired: a setup step that fails
|
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
//! How long a test waits on another thread, and how many waits its own
|
|
2
|
+
//! thread makes. A process registers one Blocking, so every test here shares
|
|
3
|
+
//! the one this module registers, and each counts only the waits of the
|
|
4
|
+
//! thread that asked.
|
|
5
|
+
|
|
6
|
+
use std::cell::RefCell;
|
|
7
|
+
use std::sync::atomic::{AtomicUsize, Ordering};
|
|
8
|
+
use std::sync::{Arc, Once};
|
|
9
|
+
use std::time::{Duration, Instant};
|
|
10
|
+
|
|
11
|
+
use kobako_runtime::sched::{register_blocking, Blocking};
|
|
12
|
+
|
|
13
|
+
thread_local! {
|
|
14
|
+
static COUNTS: RefCell<Option<Arc<AtomicUsize>>> = const { RefCell::new(None) };
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
struct CountsWaits;
|
|
18
|
+
|
|
19
|
+
impl Blocking for CountsWaits {
|
|
20
|
+
fn wait(&self, wait: &mut dyn FnMut()) {
|
|
21
|
+
COUNTS.with(|counts| {
|
|
22
|
+
if let Some(counts) = counts.borrow().as_ref() {
|
|
23
|
+
counts.fetch_add(1, Ordering::SeqCst);
|
|
24
|
+
}
|
|
25
|
+
});
|
|
26
|
+
wait();
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
/// The longest a test waits for another thread, so what never arrives fails
|
|
31
|
+
/// the test instead of hanging the suite.
|
|
32
|
+
pub(crate) const MAX_WAIT: Duration = Duration::from_secs(10);
|
|
33
|
+
|
|
34
|
+
/// Wait until `arrived`, sleeping a millisecond between looks so the thread
|
|
35
|
+
/// awaited has the CPU, and fail the test with `what` past `MAX_WAIT`.
|
|
36
|
+
pub(crate) fn wait_until(what: &str, mut arrived: impl FnMut() -> bool) {
|
|
37
|
+
let started = Instant::now();
|
|
38
|
+
while !arrived() {
|
|
39
|
+
assert!(
|
|
40
|
+
started.elapsed() < MAX_WAIT,
|
|
41
|
+
"{what} did not happen within {MAX_WAIT:?}"
|
|
42
|
+
);
|
|
43
|
+
std::thread::sleep(Duration::from_millis(1));
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
/// Counts each wait this thread makes from now on, starting at zero.
|
|
48
|
+
pub(crate) fn count_waits() -> Arc<AtomicUsize> {
|
|
49
|
+
static REGISTER: Once = Once::new();
|
|
50
|
+
REGISTER.call_once(|| {
|
|
51
|
+
assert!(
|
|
52
|
+
register_blocking(Box::new(CountsWaits)).is_ok(),
|
|
53
|
+
"another Blocking was registered first; register only this one in the crate's tests"
|
|
54
|
+
);
|
|
55
|
+
});
|
|
56
|
+
let counts = Arc::<AtomicUsize>::default();
|
|
57
|
+
COUNTS.with(|held| *held.borrow_mut() = Some(counts.clone()));
|
|
58
|
+
counts
|
|
59
|
+
}
|
|
@@ -19,7 +19,6 @@ use kobako_runtime::cred::Cred;
|
|
|
19
19
|
use kobako_runtime::filesystem::{create_pipe_files, PipeInodeInfo};
|
|
20
20
|
use kobako_runtime::grant::Grant;
|
|
21
21
|
use kobako_runtime::limits::Limits;
|
|
22
|
-
use kobako_runtime::mount::outermost_of;
|
|
23
22
|
use kobako_runtime::net::{Backends, Namespace, OpenConnections};
|
|
24
23
|
use kobako_runtime::network::EntryError;
|
|
25
24
|
use kobako_runtime::profile::Profile;
|
|
@@ -72,7 +71,7 @@ impl Wasi {
|
|
|
72
71
|
/// An invocation's context with no grants whose descriptor 0 reads
|
|
73
72
|
/// `stdin`, for comparing a stdin read with wasmtime-wasi's.
|
|
74
73
|
#[cfg(test)]
|
|
75
|
-
pub(crate) fn
|
|
74
|
+
pub(crate) fn with_stdin(config: &Config, stdin: Vec<u8>) -> Self {
|
|
76
75
|
Self::new(
|
|
77
76
|
stdin,
|
|
78
77
|
config.limits.stdout_limit,
|
|
@@ -116,8 +115,8 @@ impl Wasi {
|
|
|
116
115
|
)
|
|
117
116
|
}
|
|
118
117
|
|
|
119
|
-
/// A hermetic context presenting the
|
|
120
|
-
///
|
|
118
|
+
/// A hermetic context presenting the root of `mounts` as the preopen at
|
|
119
|
+
/// descriptor 3, at most `open_file_limit` files open, so a test
|
|
121
120
|
/// reaches preview1's calls as an invocation does.
|
|
122
121
|
#[cfg(test)]
|
|
123
122
|
pub(crate) fn with_mounts(
|
|
@@ -158,10 +157,7 @@ impl Wasi {
|
|
|
158
157
|
// keeps only what the remaining imports read. preview2's resource
|
|
159
158
|
// table holds every file, the descriptors' included, and counts each
|
|
160
159
|
// against the open files.
|
|
161
|
-
let mut table = preview2::Table::new(
|
|
162
|
-
OpenFiles::new(open_file_limit),
|
|
163
|
-
opener.memory_budget().clone(),
|
|
164
|
-
);
|
|
160
|
+
let mut table = preview2::Table::new(OpenFiles::new(open_file_limit));
|
|
165
161
|
let mounts = grants
|
|
166
162
|
.iter()
|
|
167
163
|
.filter_map(|grant| match grant {
|
|
@@ -179,7 +175,6 @@ impl Wasi {
|
|
|
179
175
|
create_pipe_files(&PipeInodeInfo::from_bytes(stdin), &opener).0,
|
|
180
176
|
create_pipe_files(&stdout, &opener).1,
|
|
181
177
|
create_pipe_files(&stderr, &opener).1,
|
|
182
|
-
&outermost_of(&mounts),
|
|
183
178
|
&mut table,
|
|
184
179
|
(&opener, &cred),
|
|
185
180
|
)
|
|
@@ -190,7 +185,7 @@ impl Wasi {
|
|
|
190
185
|
// The profile decides the ambient authority granted: the hermetic
|
|
191
186
|
// profile denies the preview1 time and entropy imports (see
|
|
192
187
|
// `ambient`), the permissive one wires the live clocks that the
|
|
193
|
-
// guest's waits read too, and
|
|
188
|
+
// guest's waits read too, and the OS's entropy on each draw. Ambient
|
|
194
189
|
// filesystem, environment, and network stay absent on both; the
|
|
195
190
|
// builder grants none unless asked. The exhaustive match makes a
|
|
196
191
|
// future profile a compile error here, not authority granted
|
|
@@ -207,6 +202,7 @@ impl Wasi {
|
|
|
207
202
|
let wall = ambient::LiveWallClock;
|
|
208
203
|
builder.monotonic_clock(monotonic);
|
|
209
204
|
builder.wall_clock(wall);
|
|
205
|
+
builder.secure_random(ambient::host_rng());
|
|
210
206
|
Clocks::Live { monotonic, wall }
|
|
211
207
|
}
|
|
212
208
|
};
|
|
@@ -448,6 +444,91 @@ mod tests {
|
|
|
448
444
|
);
|
|
449
445
|
}
|
|
450
446
|
|
|
447
|
+
// A fork copies a thread's generator along with the memory, so a child
|
|
448
|
+
// building its context from one would draw what its parent draws. The
|
|
449
|
+
// test reruns alone first: a fork while other tests hold the VFS's locks
|
|
450
|
+
// could leave the child waiting on one of them.
|
|
451
|
+
// @behavior RT-411
|
|
452
|
+
#[cfg(unix)]
|
|
453
|
+
#[test]
|
|
454
|
+
fn a_forked_childs_permissive_guest_reads_entropy_apart_from_its_parents() {
|
|
455
|
+
const ALONE: &str = "KOBAKO_TEST_ALONE";
|
|
456
|
+
if std::env::var_os(ALONE).is_none() {
|
|
457
|
+
let rerun = std::process::Command::new(std::env::current_exe().unwrap())
|
|
458
|
+
.args([
|
|
459
|
+
"--exact",
|
|
460
|
+
"wasi::tests::a_forked_childs_permissive_guest_reads_entropy_apart_from_its_parents",
|
|
461
|
+
"--test-threads=1",
|
|
462
|
+
])
|
|
463
|
+
.env(ALONE, "1")
|
|
464
|
+
.output()
|
|
465
|
+
.expect("the test binary must run again");
|
|
466
|
+
let printed = String::from_utf8_lossy(&rerun.stdout);
|
|
467
|
+
assert!(
|
|
468
|
+
rerun.status.success() && printed.contains("test result: ok. 1 passed"),
|
|
469
|
+
"a forked child's permissive guest must read entropy apart from its parent's, \
|
|
470
|
+
the rerun alone passing this one test; it printed:\n{printed}{}",
|
|
471
|
+
String::from_utf8_lossy(&rerun.stderr)
|
|
472
|
+
);
|
|
473
|
+
return;
|
|
474
|
+
}
|
|
475
|
+
let draw = || {
|
|
476
|
+
let config = Config::for_profile(Profile::Permissive);
|
|
477
|
+
let mut wasi = Wasi::for_invocation(&config, &[]).expect("WASI context must build");
|
|
478
|
+
wasmtime_wasi::p2::bindings::random::random::Host::get_random_bytes(
|
|
479
|
+
wasmtime_wasi::WasiView::ctx(wasi.ctx_mut()).ctx.random(),
|
|
480
|
+
16,
|
|
481
|
+
)
|
|
482
|
+
.expect("entropy must be drawn")
|
|
483
|
+
};
|
|
484
|
+
draw();
|
|
485
|
+
let mut ends = [0; 2];
|
|
486
|
+
// SAFETY: a pipe into two descriptors this test owns.
|
|
487
|
+
assert_eq!(
|
|
488
|
+
unsafe { libc::pipe(ends.as_mut_ptr()) },
|
|
489
|
+
0,
|
|
490
|
+
"a pipe must open"
|
|
491
|
+
);
|
|
492
|
+
// SAFETY: the child is the only test in its process; it draws,
|
|
493
|
+
// writes, and exits without returning to the harness.
|
|
494
|
+
let child = unsafe { libc::fork() };
|
|
495
|
+
if child == 0 {
|
|
496
|
+
let code = match std::panic::catch_unwind(draw) {
|
|
497
|
+
// SAFETY: writing bytes this child owns to its pipe.
|
|
498
|
+
Ok(bytes) => unsafe {
|
|
499
|
+
i32::from(libc::write(ends[1], bytes.as_ptr().cast(), bytes.len()) != 16)
|
|
500
|
+
},
|
|
501
|
+
Err(_) => 1,
|
|
502
|
+
};
|
|
503
|
+
// SAFETY: the child ends here.
|
|
504
|
+
unsafe { libc::_exit(code) };
|
|
505
|
+
}
|
|
506
|
+
let parent = draw();
|
|
507
|
+
let mut drawn = [0u8; 16];
|
|
508
|
+
let mut status = 0;
|
|
509
|
+
// SAFETY: reading into a buffer this test owns, then reaping the child.
|
|
510
|
+
let read = unsafe {
|
|
511
|
+
libc::close(ends[1]);
|
|
512
|
+
let read = libc::read(ends[0], drawn.as_mut_ptr().cast(), drawn.len());
|
|
513
|
+
libc::close(ends[0]);
|
|
514
|
+
libc::waitpid(child, &mut status, 0);
|
|
515
|
+
read
|
|
516
|
+
};
|
|
517
|
+
assert_eq!(
|
|
518
|
+
(
|
|
519
|
+
read,
|
|
520
|
+
libc::WIFEXITED(status) && libc::WEXITSTATUS(status) == 0
|
|
521
|
+
),
|
|
522
|
+
(16, true),
|
|
523
|
+
"a forked child must build a permissive context and draw its entropy"
|
|
524
|
+
);
|
|
525
|
+
assert_ne!(
|
|
526
|
+
drawn.as_slice(),
|
|
527
|
+
parent.as_slice(),
|
|
528
|
+
"a forked child's permissive guest must read entropy apart from its parent's"
|
|
529
|
+
);
|
|
530
|
+
}
|
|
531
|
+
|
|
451
532
|
// @behavior RT-067
|
|
452
533
|
#[test]
|
|
453
534
|
fn hermetic_grants_no_ambient_resource() {
|
|
@@ -392,6 +392,19 @@
|
|
|
392
392
|
|
|
393
393
|
|
|
394
394
|
|
|
395
|
+
;;; Write high-quality random data into a buffer.
|
|
396
|
+
;;; This function blocks when the implementation is unable to immediately
|
|
397
|
+
;;; provide sufficient high-quality random data.
|
|
398
|
+
;;; This function may execute slowly, so when large mounts of random data are
|
|
399
|
+
;;; required, it's advisable to use this function to seed a pseudo-random
|
|
400
|
+
;;; number generator, rather than to provide the random data directly.
|
|
401
|
+
(@interface func (export "random_get")
|
|
402
|
+
;;; The buffer to fill with random data.
|
|
403
|
+
(param $buf (@witx pointer u8))
|
|
404
|
+
(param $buf_len $size)
|
|
405
|
+
(result $error (expected (error $errno)))
|
|
406
|
+
)
|
|
407
|
+
|
|
395
408
|
|
|
396
409
|
;;; Accept a new incoming connection.
|
|
397
410
|
;;; Note: This is similar to `accept` in POSIX.
|
data/data/kobako.wasm
CHANGED
|
Binary file
|
data/ext/kobako/Cargo.toml
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
[package]
|
|
2
2
|
name = "kobako"
|
|
3
|
-
version = "0.
|
|
3
|
+
version = "0.32.0"
|
|
4
4
|
edition = "2021"
|
|
5
5
|
authors = ["Aotokitsuruya <contact@aotoki.me>"]
|
|
6
6
|
license = "Apache-2.0"
|
|
@@ -25,4 +25,8 @@ rb-sys = { version = "0.9", default-features = false }
|
|
|
25
25
|
# reaches this crate.
|
|
26
26
|
kobako-runtime = { path = "../../crates/kobako-runtime" }
|
|
27
27
|
kobako-transport = { path = "../../crates/kobako-transport" }
|
|
28
|
-
|
|
28
|
+
# A preforking server's children run the Sandbox their parent built on
|
|
29
|
+
# Threads of their own, which a Mach port cannot serve after a fork, so the
|
|
30
|
+
# guest's faults ride signals. A Ruby process keeps no Mach exception port
|
|
31
|
+
# of its own to lose.
|
|
32
|
+
kobako-wasmtime = { path = "../../crates/kobako-wasmtime", features = ["posix-signals-on-macos"] }
|