Skip to content

Commit 8f4d185

Browse files
committed
Move time related functions to its own module
1 parent b8ee90d commit 8f4d185

File tree

4 files changed

+81
-58
lines changed

4 files changed

+81
-58
lines changed

src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ pub use crate::shims::{EvalContextExt as ShimsEvalContextExt};
3232
pub use crate::shims::foreign_items::EvalContextExt as ForeignItemsEvalContextExt;
3333
pub use crate::shims::intrinsics::EvalContextExt as IntrinsicsEvalContextExt;
3434
pub use crate::shims::tls::{EvalContextExt as TlsEvalContextExt, TlsData};
35+
pub use crate::shims::time::{EvalContextExt as TimeEvalContextExt};
3536
pub use crate::shims::dlsym::{Dlsym, EvalContextExt as DlsymEvalContextExt};
3637
pub use crate::shims::env::{EnvVars, EvalContextExt as EnvEvalContextExt};
3738
pub use crate::shims::fs::{FileHandler, EvalContextExt as FileEvalContextExt};

src/shims/foreign_items.rs

Lines changed: 4 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -508,67 +508,13 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
508508
}
509509

510510
"clock_gettime" => {
511-
if !this.machine.communicate {
512-
throw_unsup_format!("`clock_gettime` not available when isolation is enabled")
513-
} else {
514-
let clk_id = this.read_scalar(args[0])?.to_i32()?;
515-
516-
if clk_id != this.eval_libc_i32("CLOCK_REALTIME")? {
517-
let einval = this.eval_libc("EINVAL")?;
518-
this.set_last_error(einval)?;
519-
this.write_scalar(Scalar::from_int(-1i32, dest.layout.size), dest)?;
520-
} else {
521-
let tp = this.force_ptr(this.read_scalar(args[1])?.not_undef()?)?;
522-
523-
let mut sign = 1;
524-
525-
let duration = std::time::SystemTime::now()
526-
.duration_since(std::time::SystemTime::UNIX_EPOCH)
527-
.unwrap_or_else(|e| {
528-
sign = -1;
529-
e.duration()
530-
});
531-
532-
let tv_sec = sign * (duration.as_secs() as i128);
533-
let tv_nsec = duration.subsec_nanos() as i128;
534-
535-
this.write_c_ints(&tp, &[tv_sec, tv_nsec], &["time_t", "c_long"])?;
536-
537-
this.write_scalar(Scalar::from_int(0i32, dest.layout.size), dest)?;
538-
}
539-
}
511+
let result = this.clock_gettime(args[0], args[1])?;
512+
this.write_scalar(Scalar::from_int(result, dest.layout.size), dest)?;
540513
}
541514

542515
"gettimeofday" => {
543-
if !this.machine.communicate {
544-
throw_unsup_format!("`gettimeofday` not available when isolation is enabled")
545-
} else {
546-
let tz = this.read_scalar(args[1])?.not_undef()?;
547-
// Using tz is obsolete and should always be null
548-
if !this.is_null(tz)? {
549-
let einval = this.eval_libc("EINVAL")?;
550-
this.set_last_error(einval)?;
551-
this.write_scalar(Scalar::from_int(-1i32, dest.layout.size), dest)?;
552-
} else {
553-
let tv = this.force_ptr(this.read_scalar(args[0])?.not_undef()?)?;
554-
555-
let mut sign = 1;
556-
557-
let duration = std::time::SystemTime::now()
558-
.duration_since(std::time::SystemTime::UNIX_EPOCH)
559-
.unwrap_or_else(|e| {
560-
sign = -1;
561-
e.duration()
562-
});
563-
564-
let tv_sec = sign * (duration.as_secs() as i128);
565-
let tv_usec = duration.subsec_micros() as i128;
566-
567-
this.write_c_ints(&tv, &[tv_sec, tv_usec], &["time_t", "suseconds_t"])?;
568-
569-
this.write_scalar(Scalar::from_int(0i32, dest.layout.size), dest)?;
570-
}
571-
}
516+
let result = this.gettimeofday(args[0], args[1])?;
517+
this.write_scalar(Scalar::from_int(result, dest.layout.size), dest)?;
572518
}
573519

574520
"strlen" => {

src/shims/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ pub mod foreign_items;
44
pub mod intrinsics;
55
pub mod tls;
66
pub mod fs;
7+
pub mod time;
78

89
use rustc::{mir, ty};
910

src/shims/time.rs

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
use crate::stacked_borrows::Tag;
2+
use crate::*;
3+
4+
use std::time::{Duration, SystemTime};
5+
6+
fn get_time() -> (Duration, i128) {
7+
let mut sign = 1;
8+
let duration = SystemTime::now()
9+
.duration_since(SystemTime::UNIX_EPOCH)
10+
.unwrap_or_else(|e| {
11+
sign = -1;
12+
e.duration()
13+
});
14+
(duration, sign)
15+
}
16+
17+
impl<'mir, 'tcx> EvalContextExt<'mir, 'tcx> for crate::MiriEvalContext<'mir, 'tcx> {}
18+
pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx> {
19+
fn clock_gettime(
20+
&mut self,
21+
clk_id_op: OpTy<'tcx, Tag>,
22+
tp_op: OpTy<'tcx, Tag>,
23+
) -> InterpResult<'tcx, i32> {
24+
let this = self.eval_context_mut();
25+
26+
if !this.machine.communicate {
27+
throw_unsup_format!("`clock_gettime` not available when isolation is enabled")
28+
}
29+
30+
let clk_id = this.read_scalar(clk_id_op)?.to_i32()?;
31+
if clk_id != this.eval_libc_i32("CLOCK_REALTIME")? {
32+
let einval = this.eval_libc("EINVAL")?;
33+
this.set_last_error(einval)?;
34+
return Ok(-1);
35+
}
36+
37+
let tp = this.force_ptr(this.read_scalar(tp_op)?.not_undef()?)?;
38+
39+
let (duration, sign) = get_time();
40+
let tv_sec = sign * (duration.as_secs() as i128);
41+
let tv_nsec = duration.subsec_nanos() as i128;
42+
this.write_c_ints(&tp, &[tv_sec, tv_nsec], &["time_t", "c_long"])?;
43+
44+
Ok(0)
45+
}
46+
47+
fn gettimeofday(
48+
&mut self,
49+
tv_op: OpTy<'tcx, Tag>,
50+
tz_op: OpTy<'tcx, Tag>,
51+
) -> InterpResult<'tcx, i32> {
52+
let this = self.eval_context_mut();
53+
54+
if !this.machine.communicate {
55+
throw_unsup_format!("`gettimeofday` not available when isolation is enabled")
56+
}
57+
// Using tz is obsolete and should always be null
58+
let tz = this.read_scalar(tz_op)?.not_undef()?;
59+
if !this.is_null(tz)? {
60+
let einval = this.eval_libc("EINVAL")?;
61+
this.set_last_error(einval)?;
62+
return Ok(-1);
63+
}
64+
65+
let tv = this.force_ptr(this.read_scalar(tv_op)?.not_undef()?)?;
66+
67+
let (duration, sign) = get_time();
68+
let tv_sec = sign * (duration.as_secs() as i128);
69+
let tv_usec = duration.subsec_micros() as i128;
70+
71+
this.write_c_ints(&tv, &[tv_sec, tv_usec], &["time_t", "suseconds_t"])?;
72+
73+
Ok(0)
74+
}
75+
}

0 commit comments

Comments
 (0)