-
Notifications
You must be signed in to change notification settings - Fork 390
Add clock_gettime shim #975
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
Merged
Merged
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
aa3e970
Add clock_gettime shim
pvdrz fcf04b5
Reduce size of nanoseconds
pvdrz adfa2eb
Return negative times when the current time is before the unix epoch
pvdrz 7a6df85
Get size of integers using libc
pvdrz b7863f2
Add gettimeofday shim for macOS
pvdrz 9f24c12
Add helper function to write structs
pvdrz b8ee90d
Throw error instead of panicking for unfittable bits
pvdrz 8f4d185
Move time related functions to its own module
pvdrz 87b210d
Fix sign when number of seconds is zero
pvdrz 2cbf4af
Split `write_c_ints` into less specific helper functions
pvdrz 508df22
Group libc helper functions
pvdrz 50618b5
Error on negative times
pvdrz f9c7688
Use places instead of ptrs to write packed immtys
pvdrz File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
use std::time::{Duration, SystemTime}; | ||
|
||
use rustc::ty::layout::TyLayout; | ||
|
||
use crate::stacked_borrows::Tag; | ||
use crate::*; | ||
|
||
// Returns the time elapsed between now and the unix epoch as a `Duration` and the sign of the time | ||
// interval | ||
fn get_time<'tcx>() -> InterpResult<'tcx, Duration> { | ||
SystemTime::now() | ||
.duration_since(SystemTime::UNIX_EPOCH) | ||
.map_err(|_| err_unsup_format!("Times before the Unix epoch are not supported").into()) | ||
} | ||
|
||
fn int_to_immty_checked<'tcx>( | ||
int: i128, | ||
layout: TyLayout<'tcx>, | ||
) -> InterpResult<'tcx, ImmTy<'tcx, Tag>> { | ||
// If `int` does not fit in `size` bits, we error instead of letting | ||
// `ImmTy::from_int` panic. | ||
let size = layout.size; | ||
let truncated = truncate(int as u128, size); | ||
if sign_extend(truncated, size) as i128 != int { | ||
throw_unsup_format!( | ||
"Signed value {:#x} does not fit in {} bits", | ||
int, | ||
size.bits() | ||
) | ||
} | ||
Ok(ImmTy::from_int(int, layout)) | ||
} | ||
|
||
impl<'mir, 'tcx> EvalContextExt<'mir, 'tcx> for crate::MiriEvalContext<'mir, 'tcx> {} | ||
pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx> { | ||
// Foreign function used by linux | ||
fn clock_gettime( | ||
&mut self, | ||
clk_id_op: OpTy<'tcx, Tag>, | ||
tp_op: OpTy<'tcx, Tag>, | ||
) -> InterpResult<'tcx, i32> { | ||
let this = self.eval_context_mut(); | ||
|
||
if !this.machine.communicate { | ||
throw_unsup_format!("`clock_gettime` not available when isolation is enabled") | ||
} | ||
|
||
let clk_id = this.read_scalar(clk_id_op)?.to_i32()?; | ||
if clk_id != this.eval_libc_i32("CLOCK_REALTIME")? { | ||
let einval = this.eval_libc("EINVAL")?; | ||
this.set_last_error(einval)?; | ||
return Ok(-1); | ||
} | ||
|
||
let tp = this.deref_operand(tp_op)?; | ||
|
||
let duration = get_time()?; | ||
let tv_sec = duration.as_secs() as i128; | ||
let tv_nsec = duration.subsec_nanos() as i128; | ||
pvdrz marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
let imms = [ | ||
int_to_immty_checked(tv_sec, this.libc_ty_layout("time_t")?)?, | ||
int_to_immty_checked(tv_nsec, this.libc_ty_layout("c_long")?)?, | ||
]; | ||
|
||
this.write_packed_immediates(&tp, &imms)?; | ||
|
||
Ok(0) | ||
} | ||
// Foreign function used by generic unix (in particular macOS) | ||
fn gettimeofday( | ||
&mut self, | ||
tv_op: OpTy<'tcx, Tag>, | ||
tz_op: OpTy<'tcx, Tag>, | ||
) -> InterpResult<'tcx, i32> { | ||
let this = self.eval_context_mut(); | ||
|
||
if !this.machine.communicate { | ||
throw_unsup_format!("`gettimeofday` not available when isolation is enabled") | ||
} | ||
// Using tz is obsolete and should always be null | ||
let tz = this.read_scalar(tz_op)?.not_undef()?; | ||
if !this.is_null(tz)? { | ||
let einval = this.eval_libc("EINVAL")?; | ||
this.set_last_error(einval)?; | ||
return Ok(-1); | ||
} | ||
|
||
let tv = this.deref_operand(tv_op)?; | ||
|
||
let duration = get_time()?; | ||
let tv_sec = duration.as_secs() as i128; | ||
let tv_usec = duration.subsec_micros() as i128; | ||
|
||
let imms = [ | ||
int_to_immty_checked(tv_sec, this.libc_ty_layout("time_t")?)?, | ||
int_to_immty_checked(tv_usec, this.libc_ty_layout("suseconds_t")?)?, | ||
]; | ||
|
||
this.write_packed_immediates(&tv, &imms)?; | ||
|
||
Ok(0) | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
// ignore-windows: TODO clock shims are not implemented on Windows | ||
// compile-flags: -Zmiri-disable-isolation | ||
|
||
use std::time::SystemTime; | ||
|
||
fn main() { | ||
let _now = SystemTime::now(); | ||
pvdrz marked this conversation as resolved.
Show resolved
Hide resolved
pvdrz marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.