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
@@ -7,7 +7,7 @@ use kobako_runtime::filesystem::Iattr;
7
7
  use kobako_runtime::vfs::{self, OpenHow};
8
8
  use wiggle::GuestPtr;
9
9
 
10
- use super::descriptors::{Descriptor, FileHandle};
10
+ use super::descriptors::Descriptor;
11
11
  use super::errno::{Answer, Errno};
12
12
  use super::fd::to_filestat;
13
13
  use super::memory::GuestMemory;
@@ -119,9 +119,9 @@ pub(crate) fn open(
119
119
  .intersects(types::Fdflags::RSYNC | types::Fdflags::SYNC);
120
120
  // Linux's `do_sys_openat2` builds the flags, then reserves a descriptor
121
121
  // and `path_openat` allocates the file before the walk answers for the
122
- // name, so a full table or a full memory answers whatever the path names.
122
+ // name, so a full table answers whatever the path names.
123
123
  let op = vfs::build_open_flags(&how)?;
124
- let room = wasi.table().reserve_file::<FileHandle>()?;
124
+ let room = wasi.table().reserve_file()?;
125
125
  let new = wasi.descriptors().lowest_unused().ok_or(Errno::Mfile)?;
126
126
  let path = mem.read_path(path)?;
127
127
  let file = retry_eagain(|| vfs::do_file_open(wasi.cred(), &start_of(wasi, fd), &path, &op))?;
@@ -21,7 +21,6 @@ use super::memory::GuestMemory;
21
21
  use super::types::{self, Clockid, Eventrwflags, Eventtype, Subclockflags, SubscriptionU};
22
22
  use crate::invocation::Invocation;
23
23
  use crate::preview2::Pollable;
24
- use crate::trap::Interrupted;
25
24
 
26
25
  /// One subscription as the guest wrote it, and the error its event reports.
27
26
  struct Subscription {
@@ -73,11 +72,9 @@ pub(crate) fn poll_oneoff(
73
72
  .iter()
74
73
  .map(|sub| sub.to_pollable(started))
75
74
  .collect();
76
- let ready = match invocation.poll_ready(pollables) {
77
- Ok(ready) => ready,
78
- Err(err) if err.is::<Interrupted>() => return Err(Errno::Intr.into()),
79
- Err(err) => return Err(types::Error::trap(err)),
80
- };
75
+ let ready = invocation
76
+ .poll_ready(pollables)
77
+ .map_err(types::Error::trap)?;
81
78
 
82
79
  for (written, index) in ready.iter().enumerate() {
83
80
  let sub = &subscriptions[*index as usize];
@@ -276,27 +273,32 @@ mod tests {
276
273
 
277
274
  // @behavior RT-337
278
275
  #[test]
279
- fn an_interrupted_wait_answers_eintr_at_once() {
280
- let (ended, spent) = wait_ten_seconds(
276
+ fn an_interrupted_wait_ends_its_run_at_once() {
277
+ let (ended, _) = wait_ten_seconds(
281
278
  Profile::Permissive,
282
279
  Some(Duration::from_secs(20)),
283
280
  Some(|task: Task| {
281
+ let waits = crate::waits::count_waits();
284
282
  std::thread::spawn(move || {
285
- std::thread::sleep(Duration::from_millis(50));
283
+ crate::waits::wait_until("the clock wait going to sleep", || {
284
+ waits.load(std::sync::atomic::Ordering::SeqCst) == 1
285
+ });
286
286
  task.interrupt();
287
287
  });
288
288
  }),
289
289
  );
290
290
  assert!(
291
- ended.as_ref().ok() == Some(&ERRNO_INTR) && spent < Duration::from_secs(5),
291
+ ended
292
+ .as_ref()
293
+ .err()
294
+ .is_some_and(|trap| trap.is::<crate::trap::Interrupted>()),
292
295
  "a ten-second clock wait through a permissive guest's poll_oneoff, its thread \
293
- interrupted, must answer EINTR at once, got {:?} after {spent:?}",
296
+ interrupted as it sleeps, must end the run as interrupted, not wait out the clock; \
297
+ got {:?}",
294
298
  ended.map_err(|trap| trap.to_string())
295
299
  );
296
300
  }
297
301
 
298
- const ERRNO_INTR: i32 = 27;
299
-
300
302
  /// Poll `count` subscriptions at 0 in a fresh instance whose one page
301
303
  /// holds few of them, answering the errno or the trap.
302
304
  fn poll_count(count: i32) -> wasmtime::Result<i32> {
@@ -356,16 +358,18 @@ mod tests {
356
358
  // @behavior RT-073
357
359
  #[test]
358
360
  fn a_hermetic_wait_ends_without_the_host_waiting() {
359
- let (ended, spent) = wait_ten_seconds(Profile::Hermetic, None, None);
361
+ let waits = crate::waits::count_waits();
362
+ let (ended, _) = wait_ten_seconds(Profile::Hermetic, None, None);
360
363
  assert_eq!(
361
364
  ended.ok(),
362
365
  Some(0),
363
366
  "a ten-second clock wait through a hermetic guest's poll_oneoff must succeed"
364
367
  );
365
- assert!(
366
- spent < Duration::from_secs(1),
367
- "a ten-second clock wait through a hermetic guest's poll_oneoff must return at once, \
368
- not after {spent:?}"
368
+ assert_eq!(
369
+ waits.load(std::sync::atomic::Ordering::SeqCst),
370
+ 0,
371
+ "a ten-second clock wait through a hermetic guest's poll_oneoff must return without \
372
+ the host waiting"
369
373
  );
370
374
  }
371
375
 
@@ -456,16 +460,18 @@ mod tests {
456
460
  // @behavior RT-371
457
461
  #[test]
458
462
  fn a_hermetic_wait_on_two_clocks_fires_both_at_once() {
459
- let (ended, spent) = call_wait(
463
+ let waits = crate::waits::count_waits();
464
+ let (ended, _) = call_wait(
460
465
  "events_of_two_clocks",
461
466
  Profile::Hermetic,
462
467
  Some(Duration::from_secs(20)),
463
468
  None,
464
469
  );
470
+ let waited = waits.load(std::sync::atomic::Ordering::SeqCst);
465
471
  assert!(
466
- matches!(ended, Ok(2)) && spent < Duration::from_secs(1),
467
- "a hermetic poll_oneoff on a one- and a two-second clock must report both events at \
468
- once, not {ended:?} after {spent:?}"
472
+ matches!(ended, Ok(2)) && waited == 0,
473
+ "a hermetic poll_oneoff on a one- and a two-second clock must report both events \
474
+ without the host waiting, not {ended:?} after {waited} waits"
469
475
  );
470
476
  }
471
477
 
@@ -0,0 +1,125 @@
1
+ //! `random_get`, answered a page at a time into the guest's own buffer, as
2
+ //! Linux's `get_random_bytes_user` copies a page at a time: the buffer is
3
+ //! checked before any entropy is drawn, the host holds one page, and a run
4
+ //! past its deadline or interrupted stops between pages, as `getrandom`
5
+ //! looks for a pending signal at each page boundary.
6
+
7
+ use wasmtime_wasi::p2::bindings::random::random::Host as _;
8
+ use wiggle::GuestPtr;
9
+
10
+ use super::errno::Answer;
11
+ use super::memory::GuestMemory;
12
+ use super::types;
13
+ use crate::trap;
14
+ use crate::wasi::Wasi;
15
+
16
+ /// What one step draws and copies, Linux's `PAGE_SIZE`.
17
+ const PAGE: usize = 4096;
18
+
19
+ /// Fill the `len` bytes at `buf` with the entropy `wasi`'s profile reads.
20
+ pub(super) fn get(
21
+ memory: &mut GuestMemory<'_, '_>,
22
+ wasi: &mut Wasi,
23
+ buf: GuestPtr<u8>,
24
+ len: u32,
25
+ ) -> Answer {
26
+ let bytes = memory.bytes_mut(buf.as_array(len))?;
27
+ let random = wasmtime_wasi::WasiView::ctx(wasi.ctx_mut()).ctx.random();
28
+ for (index, page) in bytes.chunks_mut(PAGE).enumerate() {
29
+ if index > 0 {
30
+ if let Some(fatal) = trap::fatal_signal_trap() {
31
+ return Err(types::Error::trap(fatal));
32
+ }
33
+ }
34
+ let drawn = random
35
+ .get_random_bytes(page.len() as u64)
36
+ .map_err(types::Error::trap)?;
37
+ page.copy_from_slice(&drawn);
38
+ }
39
+ Ok(())
40
+ }
41
+
42
+ #[cfg(test)]
43
+ mod tests {
44
+ use std::time::Instant;
45
+
46
+ use wasmtime::{Module, Store};
47
+
48
+ use crate::cache::load_engine;
49
+ use crate::config::Config;
50
+ use crate::imports;
51
+ use crate::invocation::Invocation;
52
+ use crate::trap::NO_TIMEOUT_EPOCH_DELTA;
53
+ use crate::wasi::Wasi;
54
+ use kobako_runtime::profile::Profile;
55
+ use kobako_runtime::sched::run_task;
56
+
57
+ /// Two pages of entropy asked for at `at` of a one-page memory.
58
+ const READ_WAT: &str = r#"
59
+ (module
60
+ (import "wasi_snapshot_preview1" "random_get"
61
+ (func $random (param i32 i32) (result i32)))
62
+ (memory (export "memory") 1)
63
+ (func (export "read_two_pages") (param $at i32) (result i32)
64
+ (call $random (local.get $at) (i32.const 8192))))
65
+ "#;
66
+
67
+ /// Ask a permissive guest for two pages of entropy at `at`, its run's
68
+ /// deadline already passed when `past_deadline`, answering how the call
69
+ /// ended and what its memory then holds.
70
+ fn read_two_pages(at: i32, past_deadline: bool) -> (wasmtime::Result<i32>, Vec<u8>) {
71
+ let engine = load_engine().expect("shared engine must be constructible");
72
+ let config = Config::for_profile(Profile::Permissive);
73
+ let wasi = Wasi::for_invocation(&config, &[]).expect("WASI context must build");
74
+ let mut store = Store::new(engine, Invocation::new(wasi));
75
+ store.set_epoch_deadline(NO_TIMEOUT_EPOCH_DELTA);
76
+ let module = Module::new(engine, READ_WAT).expect("read module must compile");
77
+ let instance = imports::linker()
78
+ .expect("host imports must link")
79
+ .instantiate(&mut store, &module)
80
+ .expect("read module must instantiate");
81
+ let read = instance
82
+ .get_typed_func::<i32, i32>(&mut store, "read_two_pages")
83
+ .expect("read export must resolve");
84
+ let deadline = past_deadline.then(Instant::now);
85
+ store.data_mut().set_deadline(deadline);
86
+ let ended = run_task(deadline, || read.call(&mut store, at));
87
+ let memory = instance
88
+ .get_memory(&mut store, "memory")
89
+ .expect("the module exports its memory");
90
+ (ended, memory.data(&store).to_vec())
91
+ }
92
+
93
+ // The first page lies inside the memory and the second crosses its end,
94
+ // so a buffer checked a page at a time would draw into the first.
95
+ // @behavior RT-416
96
+ #[test]
97
+ fn entropy_asked_for_past_the_memory_traps_before_any_is_drawn() {
98
+ let at = 65536 - 4096 - 16;
99
+ let (ended, memory) = read_two_pages(at, false);
100
+ assert!(
101
+ ended.is_err() && memory[at as usize..].iter().all(|&byte| byte == 0),
102
+ "random_get into a buffer that ends past a permissive guest's memory must trap \
103
+ before drawing any entropy into the part inside it; got {:?}",
104
+ ended.map_err(|trap| trap.to_string())
105
+ );
106
+ }
107
+
108
+ // @behavior RT-417
109
+ #[test]
110
+ fn a_run_reading_entropy_stops_at_its_deadline_between_pages() {
111
+ let (ended, memory) = read_two_pages(0, true);
112
+ let (first, second) = memory[..8192].split_at(4096);
113
+ assert!(
114
+ ended
115
+ .as_ref()
116
+ .err()
117
+ .is_some_and(|trap| trap.is::<crate::trap::TimeoutTrap>())
118
+ && first.iter().any(|&byte| byte != 0)
119
+ && second.iter().all(|&byte| byte == 0),
120
+ "random_get of two pages by a permissive guest whose run's deadline passed must \
121
+ draw the first page and end the run as timed out before the second; got {:?}",
122
+ ended.map_err(|trap| trap.to_string())
123
+ );
124
+ }
125
+ }
@@ -0,0 +1,31 @@
1
+ //! Host links the preview1 tests stage beneath a Bind Mount.
2
+
3
+ use std::path::Path;
4
+
5
+ /// Make a host link at `at` leading to `target`, spelled as a unix path.
6
+ /// Windows makes a link to a directory apart from one to a file, so there
7
+ /// the kind follows what `target` names now, a file where nothing stands,
8
+ /// and `target` takes the host's separator.
9
+ pub(super) fn symlink(target: impl AsRef<Path>, at: impl AsRef<Path>) {
10
+ let (target, at) = (target.as_ref(), at.as_ref());
11
+ #[cfg(unix)]
12
+ let made = std::os::unix::fs::symlink(target, at);
13
+ #[cfg(windows)]
14
+ let made = {
15
+ let target = std::path::PathBuf::from(target.to_string_lossy().replace('/', "\\"));
16
+ let beside = at.parent().unwrap_or(Path::new(""));
17
+ if beside.join(&target).is_dir() {
18
+ std::os::windows::fs::symlink_dir(&target, at)
19
+ } else {
20
+ std::os::windows::fs::symlink_file(&target, at)
21
+ }
22
+ };
23
+ made.unwrap_or_else(|err| {
24
+ panic!(
25
+ "the host must make a link at {} to {}: {err}; a Windows host makes one only \
26
+ in Developer Mode or with the privilege to create symbolic links",
27
+ at.display(),
28
+ target.display()
29
+ )
30
+ });
31
+ }
@@ -1,14 +1,16 @@
1
- //! kobako's own answers to the 36 preview1 imports that read the
2
- //! descriptor table: `fd_*`, `path_*`, `sock_*`, and `poll_oneoff`.
1
+ //! kobako's own answers to the 37 preview1 imports that read the
2
+ //! descriptor table, `fd_*`, `path_*`, `sock_*`, and `poll_oneoff`, and to
3
+ //! `random_get`.
3
4
  //!
4
5
  //! wasmtime-wasi keeps its table private, so a descriptor kobako places
5
6
  //! there would stay invisible to its calls. Answering every call that reads
6
7
  //! a descriptor keeps the guest on one table, `descriptors`, where
7
- //! each outermost Mount is a preopen. The other ten imports — arguments,
8
- //! environment, clocks, process, scheduling, and randomness — stay
9
- //! wasmtime-wasi's, so `witx/` declares only these 36. wiggle generates their
10
- //! glue as it generates wasmtime-wasi's, so a value outside what the witx
11
- //! defines traps there before a call starts.
8
+ //! each outermost Mount is a preopen. wasmtime-wasi draws all the entropy
9
+ //! `random_get` asks for before it checks the buffer, so kobako answers it a
10
+ //! page at a time. The other nine imports — arguments, environment, clocks,
11
+ //! process, and scheduling — stay wasmtime-wasi's, so `witx/` declares only
12
+ //! these 37. wiggle generates their glue as it generates wasmtime-wasi's, so a
13
+ //! value outside what the witx defines traps there before a call starts.
12
14
 
13
15
  pub(crate) mod descriptors;
14
16
  mod errno;
@@ -16,6 +18,7 @@ mod fd;
16
18
  mod memory;
17
19
  mod path;
18
20
  mod poll;
21
+ mod random;
19
22
  mod sock;
20
23
 
21
24
  #[cfg(test)]
@@ -23,6 +26,8 @@ mod differential;
23
26
  #[cfg(test)]
24
27
  mod mount_view;
25
28
  #[cfg(test)]
29
+ mod testing;
30
+ #[cfg(test)]
26
31
  mod testsuite;
27
32
 
28
33
  use wasmtime::Linker;
@@ -72,7 +77,9 @@ pub(crate) fn shadow(linker: &mut Linker<Invocation>) -> wasmtime::Result<()> {
72
77
 
73
78
  impl Invocation {
74
79
  /// Run one call against the calling instance's memory, answering what it
75
- /// answers unless its host has given the invocation up by then.
80
+ /// answers unless its host has given the invocation up by then, or the
81
+ /// run is to stop as the call returns: that ends the run there, as Linux's
82
+ /// `exit_to_user_mode` ends a task before it reads what the call answered.
76
83
  fn answer<T>(
77
84
  &mut self,
78
85
  memory: &mut wiggle::GuestMemory<'_>,
@@ -80,7 +87,7 @@ impl Invocation {
80
87
  ) -> Answer<T> {
81
88
  let fuel = self.hostcall_fuel();
82
89
  let answer = call(&mut GuestMemory::new(memory, fuel), self);
83
- match self.abandonment() {
90
+ match self.abandonment().or_else(crate::trap::fatal_signal_trap) {
84
91
  Some(trap) => Err(types::Error::trap(trap)),
85
92
  None => answer,
86
93
  }
@@ -468,6 +475,17 @@ impl wasi_snapshot_preview1::WasiSnapshotPreview1 for Invocation {
468
475
  })
469
476
  }
470
477
 
478
+ fn random_get(
479
+ &mut self,
480
+ memory: &mut wiggle::GuestMemory<'_>,
481
+ buf: GuestPtr<u8>,
482
+ len: types::Size,
483
+ ) -> Answer {
484
+ self.answer(memory, |mem, inv| {
485
+ random::get(mem, inv.wasi_mut(), buf, len)
486
+ })
487
+ }
488
+
471
489
  fn sock_accept(
472
490
  &mut self,
473
491
  memory: &mut wiggle::GuestMemory<'_>,
@@ -514,3 +532,61 @@ impl wasi_snapshot_preview1::WasiSnapshotPreview1 for Invocation {
514
532
  })
515
533
  }
516
534
  }
535
+
536
+ #[cfg(test)]
537
+ mod tests {
538
+ use std::time::Instant;
539
+
540
+ use wasmtime::{Module, Store};
541
+
542
+ use crate::cache::load_engine;
543
+ use crate::config::Config;
544
+ use crate::imports;
545
+ use crate::invocation::Invocation;
546
+ use crate::trap::NO_TIMEOUT_EPOCH_DELTA;
547
+ use crate::wasi::Wasi;
548
+ use kobako_runtime::profile::Profile;
549
+ use kobako_runtime::sched::run_task;
550
+
551
+ /// Ask for no entropy, then answer 7 once the guest reads what the call
552
+ /// answered.
553
+ const CALL_WAT: &str = r#"
554
+ (module
555
+ (import "wasi_snapshot_preview1" "random_get"
556
+ (func $random (param i32 i32) (result i32)))
557
+ (memory (export "memory") 1)
558
+ (func (export "read_nothing") (result i32)
559
+ (drop (call $random (i32.const 0) (i32.const 0)))
560
+ (i32.const 7)))
561
+ "#;
562
+
563
+ // @behavior RT-426
564
+ #[test]
565
+ fn a_host_call_returning_past_its_runs_deadline_ends_the_run_there() {
566
+ let engine = load_engine().expect("shared engine must be constructible");
567
+ let config = Config::for_profile(Profile::Hermetic);
568
+ let wasi = Wasi::for_invocation(&config, &[]).expect("WASI context must build");
569
+ let mut store = Store::new(engine, Invocation::new(wasi));
570
+ store.set_epoch_deadline(NO_TIMEOUT_EPOCH_DELTA);
571
+ let module = Module::new(engine, CALL_WAT).expect("call module must compile");
572
+ let instance = imports::linker()
573
+ .expect("host imports must link")
574
+ .instantiate(&mut store, &module)
575
+ .expect("call module must instantiate");
576
+ let read = instance
577
+ .get_typed_func::<(), i32>(&mut store, "read_nothing")
578
+ .expect("call export must resolve");
579
+ let deadline = Some(Instant::now());
580
+ store.data_mut().set_deadline(deadline);
581
+ let ended = run_task(deadline, || read.call(&mut store, ()));
582
+ assert!(
583
+ ended
584
+ .as_ref()
585
+ .err()
586
+ .is_some_and(|trap| trap.is::<crate::trap::TimeoutTrap>()),
587
+ "a random_get returning once its run's deadline passed must end the run as timed \
588
+ out before the guest reads what it answered; got {:?}",
589
+ ended.map_err(|trap| trap.to_string())
590
+ );
591
+ }
592
+ }
@@ -17,8 +17,8 @@ use kobako_runtime::Errno;
17
17
  pub(crate) use kobako_runtime::vfs::File;
18
18
 
19
19
  /// How many times a call is made before a guest is answered EAGAIN, as cap-std
20
- /// makes `openat2` four times: any move in the process answers a `..` walking
21
- /// past it so, and nothing bounds how often one lands.
20
+ /// makes `openat2` four times: any move on a Filesystem answers a `..` walking
21
+ /// it so, and nothing bounds how often one lands.
22
22
  const MAX_ATTEMPTS: usize = 4;
23
23
 
24
24
  /// What `call` answers once no move landed during its walk, made again at most