Skip to content

Commit

Permalink
Add helper function to write structs
Browse files Browse the repository at this point in the history
  • Loading branch information
pvdrz committed Oct 8, 2019
1 parent 622d84f commit e00db1b
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 49 deletions.
35 changes: 34 additions & 1 deletion src/helpers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use rustc::hir::def_id::{DefId, CRATE_DEF_INDEX};
use rustc::mir;
use rustc::ty::{
self,
layout::{self, Align, Size},
layout::{self, Align, Size, LayoutOf},
};

use rand::RngCore;
Expand Down Expand Up @@ -295,4 +295,37 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
}
}
}

fn write_c_ints(
&mut self,
ptr: &Pointer<Tag>,
bits: &[i128],
ty_names: &[&str],
) -> InterpResult<'tcx> {
let this = self.eval_context_mut();

let tcx = &{ this.tcx.tcx };

let mut sizes = Vec::new();

for name in ty_names {
let ty = this.resolve_path(&["libc", name])?.ty(*tcx);
sizes.push(this.layout_of(ty)?.size);
}

let allocation = this.memory_mut().get_mut(ptr.alloc_id)?;
let mut offset = Size::from_bytes(0);

for (value, size) in bits.iter().zip(sizes) {
allocation.write_scalar(
tcx,
ptr.offset(offset, tcx)?,
Scalar::from_int(*value, size).into(),
size,
)?;
offset += size;
}

Ok(())
}
}
56 changes: 8 additions & 48 deletions src/shims/foreign_items.rs
Original file line number Diff line number Diff line change
Expand Up @@ -511,8 +511,6 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
if !this.machine.communicate {
throw_unsup_format!("`clock_gettime` not available when isolation is enabled")
} else {
let tcx = &{ this.tcx.tcx };

let clk_id = this.read_scalar(args[0])?.to_i32()?;

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

let long = this.resolve_path(&["libc", "c_long"])?.ty(*tcx);
let time_t = this.resolve_path(&["libc", "time_t"])?.ty(*tcx);

let tv_sec_size = this.layout_of(time_t)?.size;
let tv_nsec_size = this.layout_of(long)?.size;

let allocation = this.memory_mut().get_mut(tp.alloc_id)?;

let mut sign = 1;

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

allocation.write_scalar(
tcx,
tp,
Scalar::from_int(sign * (duration.as_secs() as i64), tv_sec_size)
.into(),
tv_sec_size,
)?;

allocation.write_scalar(
tcx,
tp.offset(tv_sec_size, tcx)?,
Scalar::from_int(duration.subsec_nanos() as i64, tv_nsec_size).into(),
tv_nsec_size,
)?;
let tv_sec = sign * (duration.as_secs() as i128);
let tv_nsec = duration.subsec_nanos() as i128;

this.write_c_ints(&tp, &[tv_sec, tv_nsec], &["time_t", "c_long"])?;

this.write_scalar(Scalar::from_int(0i32, dest.layout.size), dest)?;
}
Expand All @@ -563,8 +543,6 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
if !this.machine.communicate {
throw_unsup_format!("`gettimeofday` not available when isolation is enabled")
} else {
let tcx = &{ this.tcx.tcx };

let tz = this.read_scalar(args[1])?.not_undef()?;
// Using tz is obsolete and should always be null
if !this.is_null(tz)? {
Expand All @@ -574,14 +552,6 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
} else {
let tv = this.force_ptr(this.read_scalar(args[0])?.not_undef()?)?;

let time_t = this.resolve_path(&["libc", "time_t"])?.ty(*tcx);
let suseconds_t = this.resolve_path(&["libc", "suseconds_t"])?.ty(*tcx);

let tv_sec_size = this.layout_of(time_t)?.size;
let tv_usec_size = this.layout_of(suseconds_t)?.size;

let allocation = this.memory_mut().get_mut(tv.alloc_id)?;

let mut sign = 1;

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

allocation.write_scalar(
tcx,
tv,
Scalar::from_int(sign * (duration.as_secs() as i64), tv_sec_size)
.into(),
tv_sec_size,
)?;

allocation.write_scalar(
tcx,
tv.offset(tv_sec_size, tcx)?,
Scalar::from_int(duration.subsec_micros() as i64, tv_usec_size).into(),
tv_usec_size,
)?;
let tv_sec = sign * (duration.as_secs() as i128);
let tv_usec = duration.subsec_micros() as i128;

this.write_c_ints(&tv, &[tv_sec, tv_usec], &["time_t", "suseconds_t"])?;

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

0 comments on commit e00db1b

Please sign in to comment.