Skip to content

Commit bbef7a0

Browse files
committed
Add helper function to write structs
1 parent fd246e7 commit bbef7a0

File tree

2 files changed

+42
-49
lines changed

2 files changed

+42
-49
lines changed

src/helpers.rs

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use rustc::hir::def_id::{DefId, CRATE_DEF_INDEX};
44
use rustc::mir;
55
use rustc::ty::{
66
self,
7-
layout::{self, Align, Size},
7+
layout::{self, Align, Size, LayoutOf},
88
};
99

1010
use rand::RngCore;
@@ -295,4 +295,37 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
295295
}
296296
}
297297
}
298+
299+
fn write_c_ints(
300+
&mut self,
301+
ptr: &Pointer<Tag>,
302+
bits: &[i128],
303+
ty_names: &[&str],
304+
) -> InterpResult<'tcx> {
305+
let this = self.eval_context_mut();
306+
307+
let tcx = &{ this.tcx.tcx };
308+
309+
let mut sizes = Vec::new();
310+
311+
for name in ty_names {
312+
let ty = this.resolve_path(&["libc", name])?.ty(*tcx);
313+
sizes.push(this.layout_of(ty)?.size);
314+
}
315+
316+
let allocation = this.memory_mut().get_mut(ptr.alloc_id)?;
317+
let mut offset = Size::from_bytes(0);
318+
319+
for (value, size) in bits.iter().zip(sizes) {
320+
allocation.write_scalar(
321+
tcx,
322+
ptr.offset(offset, tcx)?,
323+
Scalar::from_int(*value, size).into(),
324+
size,
325+
)?;
326+
offset += size;
327+
}
328+
329+
Ok(())
330+
}
298331
}

src/shims/foreign_items.rs

Lines changed: 8 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -511,8 +511,6 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
511511
if !this.machine.communicate {
512512
throw_unsup_format!("`clock_gettime` not available when isolation is enabled")
513513
} else {
514-
let tcx = &{ this.tcx.tcx };
515-
516514
let clk_id = this.read_scalar(args[0])?.to_i32()?;
517515

518516
if clk_id != this.eval_libc_i32("CLOCK_REALTIME")? {
@@ -522,14 +520,6 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
522520
} else {
523521
let tp = this.force_ptr(this.read_scalar(args[1])?.not_undef()?)?;
524522

525-
let long = this.resolve_path(&["libc", "c_long"])?.ty(*tcx);
526-
let time_t = this.resolve_path(&["libc", "time_t"])?.ty(*tcx);
527-
528-
let tv_sec_size = this.layout_of(time_t)?.size;
529-
let tv_nsec_size = this.layout_of(long)?.size;
530-
531-
let allocation = this.memory_mut().get_mut(tp.alloc_id)?;
532-
533523
let mut sign = 1;
534524

535525
let duration = std::time::SystemTime::now()
@@ -539,20 +529,10 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
539529
e.duration()
540530
});
541531

542-
allocation.write_scalar(
543-
tcx,
544-
tp,
545-
Scalar::from_int(sign * (duration.as_secs() as i64), tv_sec_size)
546-
.into(),
547-
tv_sec_size,
548-
)?;
549-
550-
allocation.write_scalar(
551-
tcx,
552-
tp.offset(tv_sec_size, tcx)?,
553-
Scalar::from_int(duration.subsec_nanos() as i64, tv_nsec_size).into(),
554-
tv_nsec_size,
555-
)?;
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"])?;
556536

557537
this.write_scalar(Scalar::from_int(0i32, dest.layout.size), dest)?;
558538
}
@@ -563,8 +543,6 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
563543
if !this.machine.communicate {
564544
throw_unsup_format!("`gettimeofday` not available when isolation is enabled")
565545
} else {
566-
let tcx = &{ this.tcx.tcx };
567-
568546
let tz = this.read_scalar(args[1])?.not_undef()?;
569547
// Using tz is obsolete and should always be null
570548
if !this.is_null(tz)? {
@@ -574,14 +552,6 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
574552
} else {
575553
let tv = this.force_ptr(this.read_scalar(args[0])?.not_undef()?)?;
576554

577-
let time_t = this.resolve_path(&["libc", "time_t"])?.ty(*tcx);
578-
let suseconds_t = this.resolve_path(&["libc", "suseconds_t"])?.ty(*tcx);
579-
580-
let tv_sec_size = this.layout_of(time_t)?.size;
581-
let tv_usec_size = this.layout_of(suseconds_t)?.size;
582-
583-
let allocation = this.memory_mut().get_mut(tv.alloc_id)?;
584-
585555
let mut sign = 1;
586556

587557
let duration = std::time::SystemTime::now()
@@ -591,20 +561,10 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
591561
e.duration()
592562
});
593563

594-
allocation.write_scalar(
595-
tcx,
596-
tv,
597-
Scalar::from_int(sign * (duration.as_secs() as i64), tv_sec_size)
598-
.into(),
599-
tv_sec_size,
600-
)?;
601-
602-
allocation.write_scalar(
603-
tcx,
604-
tv.offset(tv_sec_size, tcx)?,
605-
Scalar::from_int(duration.subsec_micros() as i64, tv_usec_size).into(),
606-
tv_usec_size,
607-
)?;
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"])?;
608568

609569
this.write_scalar(Scalar::from_int(0i32, dest.layout.size), dest)?;
610570
}

0 commit comments

Comments
 (0)