Skip to content

Specialize sleep_until implementation for unix (except mac) #141829

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 20 commits into
base: master
Choose a base branch
from
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 8 additions & 11 deletions src/tools/miri/src/shims/time.rs
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please also add a test in tests/pass-dep/libc/libc-time.rs for the new shim. In particular, it seems there are two codepaths (relative and absolute?), they should both be tested.

Also, please add a call to sleep_until in tests/pass/shims/time.rs.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should be done.

  • I'm a little unsure about the libc-time.rs test as I've added a use std::time::{Duration, Instant}; while the test hardly had any imports before.
  • I've added a test for nanosleep since there was none there.
  • All tests where locally tested by sabotaging them.

Original file line number Diff line number Diff line change
Expand Up @@ -367,18 +367,15 @@ pub trait EvalContextExt<'tcx>: crate::MiriInterpCxExt<'tcx> {
clock_id: &OpTy<'tcx>,
flags: &OpTy<'tcx>,
req: &OpTy<'tcx>,
rem: &OpTy<'tcx>, // Signal handlers are not supported, so rem will never be written to.
rem: &OpTy<'tcx>,
) -> InterpResult<'tcx, Scalar> {
let this = self.eval_context_mut();

let clockid_t_size = this.libc_ty_layout("clockid_t").size;
let clock_id = this.read_scalar(clock_id_op)?.to_int(clockid_t_size)?;
let req = this.deref_pointer_as(req_op, this.libc_ty_layout("timespec"))?;
// TODO must be a better way to do this, also fix the
// if compare of the flags later
let int_size = this.libc_ty_layout("int").size;
let flags = this.read_scalar(flags)?.to_int(int_size);
let rem = this.read_pointer()?;
let clock_id = this.read_scalar(clock_id)?.to_int(clockid_t_size)?;
let req = this.deref_pointer_as(req, this.libc_ty_layout("timespec"))?;
let flags = this.read_scalar(flags)?.to_i32()?;
let _rem = this.read_pointer(rem)?; // Signal handlers are not supported, so rem will never be written to.

// The standard lib through sleep_until only needs CLOCK_MONOTONIC
if clock_id != this.eval_libc("CLOCK_MONOTONIC").to_int(clockid_t_size)? {
Expand All @@ -396,14 +393,14 @@ pub trait EvalContextExt<'tcx>: crate::MiriInterpCxExt<'tcx> {
// No flags set, the timespec should be interperted as a duration
// to sleep for
TimeoutAnchor::Relative
} else if flag == this.eval_libc("TIMER_ABSTIME").to_int(int_size) {
} else if flags == this.eval_libc("TIMER_ABSTIME").to_i32()? {
// Only flag TIMER_ABSTIME set, the timespec should be interperted as
// an absolute time.
TimeoutAnchor::Absolute
} else {
// The standard lib through sleep_until only needs TIMER_ABSTIME
// The standard lib (through `sleep_until`) only needs TIMER_ABSTIME
throw_unsup_format!(
"`clock_nanosleep` unsupported flags {flags}, only no flags or\
"`clock_nanosleep` unsupported flags {flags}, only no flags or \
TIMER_ABSTIME is supported"
);
};
Expand Down
Loading