Allow to disable clocks in WasiCtx (#6007)

Takes the approach described in #6004, but also creates a wrapper for the monotonic time that encapsulates the `creation_time` field as well, since they logically belong and are always used together.

This makes it easier to configure `WasiCtx` with custom clocks as well as disable them for security or determinism reasons.

Closes #6004.
This commit is contained in:
Ingvar Stepanyan
2023-03-13 23:47:04 +00:00
committed by GitHub
parent d6ce632b5b
commit 873d3b50a0
5 changed files with 92 additions and 56 deletions

View File

@@ -38,14 +38,14 @@ async fn empty_file_readable() -> Result<(), Error> {
let mut poll = Poll::new();
poll.subscribe_read(&mut *f, Userdata::from(123));
// Timeout bounds time in poll_oneoff
let monotonic = &*clocks.monotonic()?.abs_clock;
poll.subscribe_monotonic_clock(
&*clocks.monotonic,
clocks
.monotonic
.now(clocks.monotonic.resolution())
monotonic,
monotonic
.now(monotonic.resolution())
.checked_add(TIMEOUT)
.unwrap(),
clocks.monotonic.resolution(),
monotonic.resolution(),
Userdata::from(0),
);
poll_oneoff(&mut poll).await?;
@@ -81,14 +81,14 @@ async fn empty_file_writable() -> Result<(), Error> {
let mut poll = Poll::new();
poll.subscribe_write(&mut *writable_f, Userdata::from(123));
// Timeout bounds time in poll_oneoff
let monotonic = &*clocks.monotonic()?.abs_clock;
poll.subscribe_monotonic_clock(
&*clocks.monotonic,
clocks
.monotonic
.now(clocks.monotonic.resolution())
monotonic,
monotonic
.now(monotonic.resolution())
.checked_add(TIMEOUT)
.unwrap(),
clocks.monotonic.resolution(),
monotonic.resolution(),
Userdata::from(0),
);
poll_oneoff(&mut poll).await?;
@@ -110,9 +110,9 @@ async fn empty_file_writable() -> Result<(), Error> {
async fn stdio_readable() -> Result<(), Error> {
let clocks = clocks_ctx();
let deadline = clocks
.monotonic
.now(clocks.monotonic.resolution())
let monotonic = &*clocks.monotonic()?.abs_clock;
let deadline = monotonic
.now(monotonic.resolution())
.checked_add(TIMEOUT)
.unwrap();
@@ -133,10 +133,11 @@ async fn stdio_readable() -> Result<(), Error> {
poll.subscribe_write(&mut **file, Userdata::from(*ix));
}
// Timeout bounds time in poll_oneoff
let monotonic = &*clocks.monotonic()?.abs_clock;
poll.subscribe_monotonic_clock(
&*clocks.monotonic,
monotonic,
deadline,
clocks.monotonic.resolution(),
monotonic.resolution(),
Userdata::from(999),
);
poll_oneoff(&mut poll).await?;