Skip to content

Commit f9584cf

Browse files
committed
Add clock_gettime shim
1 parent cc0468b commit f9584cf

File tree

2 files changed

+54
-8
lines changed

2 files changed

+54
-8
lines changed

src/shims/foreign_items.rs

Lines changed: 47 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -507,16 +507,55 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
507507
this.write_scalar(Scalar::from_int(result, dest.layout.size), dest)?;
508508
}
509509

510-
"strlen" => {
511-
let ptr = this.read_scalar(args[0])?.not_undef()?;
512-
let n = this.memory().read_c_str(ptr)?.len();
513-
this.write_scalar(Scalar::from_uint(n as u64, dest.layout.size), dest)?;
510+
"clock_gettime" => {
511+
if !this.machine.communicate {
512+
throw_unsup_format!("`clock_gettime` not available when isolation is enabled")
513+
} else {
514+
let tcx = &{ this.tcx.tcx };
515+
516+
let clk_id = this.read_scalar(args[0])?.to_i32()?;
517+
518+
if clk_id != this.eval_libc_i32("CLOCK_REALTIME")? {
519+
let einval = this.eval_libc("EINVAL")?;
520+
this.set_last_error(einval)?;
521+
this.write_scalar(Scalar::from_int(-1i32, dest.layout.size), dest)?;
522+
} else {
523+
let tp = this.force_ptr(this.read_scalar(args[1])?.not_undef()?)?;
524+
525+
let allocation = this.memory_mut().get_mut(tp.alloc_id)?;
526+
527+
let duration = std::time::SystemTime::now()
528+
.duration_since(std::time::SystemTime::UNIX_EPOCH)
529+
.unwrap_or_else(|_| bug!("Clock went backwards"));
530+
531+
allocation.write_scalar(
532+
tcx,
533+
tp,
534+
Scalar::from_u64(duration.as_secs()).into(),
535+
Size::from_bits(64),
536+
)?;
537+
allocation.write_scalar(
538+
tcx,
539+
tp.offset(Size::from_bits(64), tcx)?,
540+
Scalar::from_u64(duration.subsec_nanos() as u64).into(),
541+
Size::from_bits(64),
542+
)?;
543+
544+
this.write_scalar(Scalar::from_int(0i32, dest.layout.size), dest)?;
545+
}
546+
}
514547
}
515548

516-
// math functions
517-
"cbrtf" | "coshf" | "sinhf" | "tanf" => {
518-
// FIXME: Using host floats.
519-
let f = f32::from_bits(this.read_scalar(args[0])?.to_u32()?);
549+
"strlen" => {
550+
let ptr = this.read_scalar(args[0])?.not_undef()?;
551+
let n = this.memory().read_c_str(ptr)?.len();
552+
this.write_scalar(Scalar::from_uint(n as u64, dest.layout.size), dest)?;
553+
}
554+
555+
// math functions
556+
"cbrtf" | "coshf" | "sinhf" | "tanf" => {
557+
// FIXME: Using host floats.
558+
let f = f32::from_bits(this.read_scalar(args[0])?.to_u32()?);
520559
let f = match link_name {
521560
"cbrtf" => f.cbrt(),
522561
"coshf" => f.cosh(),

tests/run-pass/clock.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
// compile-flags: -Zmiri-disable-isolation
2+
3+
use std::time::SystemTime;
4+
5+
fn main() {
6+
let _now = SystemTime::now();
7+
}

0 commit comments

Comments
 (0)