Skip to content

Commit

Permalink
Auto merge of #1487 - pnadon:miri-rename-undef-uninit, r=RalfJung
Browse files Browse the repository at this point in the history
Miri rename undef uninit

The changes made here are related to [issue #71193 on Rust](rust-lang/rust#71193), and the pull request [74664 on Rust](rust-lang/rust#74664).

1. renamed `ScalarMaybeUninit::not_undef` to `check_init`
2. renamed `Immediate::to_scalar_or_undef` to `Immediate::to_scalar_or_uninit`
  • Loading branch information
bors committed Jul 27, 2020
2 parents 91b58c9 + 6dd700f commit 345b033
Show file tree
Hide file tree
Showing 19 changed files with 99 additions and 99 deletions.
2 changes: 1 addition & 1 deletion rust-version
Original file line number Diff line number Diff line change
@@ -1 +1 @@
0e11fc8053d32c44e7152865852acc5c3c54efb3
13f9aa190957b993a268fd4a046fce76ca8814ee
2 changes: 1 addition & 1 deletion src/eval.rs
Original file line number Diff line number Diff line change
Expand Up @@ -234,7 +234,7 @@ pub fn eval_main<'tcx>(tcx: TyCtxt<'tcx>, main_id: DefId, config: MiriConfig) ->
}
}
}
let return_code = ecx.read_scalar(ret_place.into())?.not_undef()?.to_machine_isize(&ecx)?;
let return_code = ecx.read_scalar(ret_place.into())?.check_init()?.to_machine_isize(&ecx)?;
Ok(return_code)
})();

Expand Down
8 changes: 4 additions & 4 deletions src/helpers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
fn eval_libc(&mut self, name: &str) -> InterpResult<'tcx, Scalar<Tag>> {
self.eval_context_mut()
.eval_path_scalar(&["libc", name])?
.not_undef()
.check_init()
}

/// Helper function to get a `libc` constant as an `i32`.
Expand All @@ -80,7 +80,7 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
fn eval_windows(&mut self, module: &str, name: &str) -> InterpResult<'tcx, Scalar<Tag>> {
self.eval_context_mut()
.eval_path_scalar(&["std", "sys", "windows", module, name])?
.not_undef()
.check_init()
}

/// Helper function to get a `windows` constant as an `u64`.
Expand Down Expand Up @@ -407,7 +407,7 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
fn get_last_error(&self) -> InterpResult<'tcx, Scalar<Tag>> {
let this = self.eval_context_ref();
let errno_place = this.machine.last_error.unwrap();
this.read_scalar(errno_place.into())?.not_undef()
this.read_scalar(errno_place.into())?.check_init()
}

/// Sets the last OS error using a `std::io::Error`. This function tries to produce the most
Expand Down Expand Up @@ -467,7 +467,7 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
}
}
}

fn read_scalar_at_offset(
&self,
op: OpTy<'tcx, Tag>,
Expand Down
6 changes: 3 additions & 3 deletions src/operator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,11 @@ impl<'mir, 'tcx> EvalContextExt<'tcx> for super::MiriEvalContext<'mir, 'tcx> {
#[rustfmt::skip]
let eq = match (*left, *right) {
(Immediate::Scalar(left), Immediate::Scalar(right)) => {
self.ptr_eq(left.not_undef()?, right.not_undef()?)?
self.ptr_eq(left.check_init()?, right.check_init()?)?
}
(Immediate::ScalarPair(left1, left2), Immediate::ScalarPair(right1, right2)) => {
self.ptr_eq(left1.not_undef()?, right1.not_undef()?)?
&& self.ptr_eq(left2.not_undef()?, right2.not_undef()?)?
self.ptr_eq(left1.check_init()?, right1.check_init()?)?
&& self.ptr_eq(left2.check_init()?, right2.check_init()?)?
}
_ => bug!("Type system should not allow comparing Scalar with ScalarPair"),
};
Expand Down
34 changes: 17 additions & 17 deletions src/shims/env.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ impl<'tcx> EnvVars<'tcx> {
}
// Deallocate environ var list.
let environ = ecx.machine.env_vars.environ.unwrap();
let old_vars_ptr = ecx.read_scalar(environ.into())?.not_undef()?;
let old_vars_ptr = ecx.read_scalar(environ.into())?.check_init()?;
ecx.memory.deallocate(ecx.force_ptr(old_vars_ptr)?, None, MiriMemoryKind::Env.into())?;
Ok(())
}
Expand Down Expand Up @@ -104,7 +104,7 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
let target_os = &this.tcx.sess.target.target.target_os;
assert!(target_os == "linux" || target_os == "macos", "`getenv` is only available for the UNIX target family");

let name_ptr = this.read_scalar(name_op)?.not_undef()?;
let name_ptr = this.read_scalar(name_op)?.check_init()?;
let name = this.read_os_str_from_c_str(name_ptr)?;
Ok(match this.machine.env_vars.map.get(name) {
Some(var_ptr) => {
Expand All @@ -125,7 +125,7 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
let this = self.eval_context_mut();
this.assert_target_os("windows", "GetEnvironmentVariableW");

let name_ptr = this.read_scalar(name_op)?.not_undef()?;
let name_ptr = this.read_scalar(name_op)?.check_init()?;
let name = this.read_os_str_from_wide_str(name_ptr)?;
Ok(match this.machine.env_vars.map.get(&name) {
Some(var_ptr) => {
Expand All @@ -135,7 +135,7 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
let var_ptr = Scalar::from(var_ptr.offset(Size::from_bytes(name_offset_bytes), this)?);
let var = this.read_os_str_from_wide_str(var_ptr)?;

let buf_ptr = this.read_scalar(buf_op)?.not_undef()?;
let buf_ptr = this.read_scalar(buf_op)?.check_init()?;
// `buf_size` represents the size in characters.
let buf_size = u64::from(this.read_scalar(size_op)?.to_u32()?);
windows_check_buffer_size(this.write_os_str_to_wide_str(&var, buf_ptr, buf_size)?)
Expand All @@ -153,7 +153,7 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
let this = self.eval_context_mut();
this.assert_target_os("windows", "GetEnvironmentStringsW");

// Info on layout of environment blocks in Windows:
// Info on layout of environment blocks in Windows:
// https://docs.microsoft.com/en-us/windows/win32/procthread/environment-variables
let mut env_vars = std::ffi::OsString::new();
for &item in this.machine.env_vars.map.values() {
Expand All @@ -173,7 +173,7 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
let this = self.eval_context_mut();
this.assert_target_os("windows", "FreeEnvironmentStringsW");

let env_block_ptr = this.read_scalar(env_block_op)?.not_undef()?;
let env_block_ptr = this.read_scalar(env_block_op)?.check_init()?;
let result = this.memory.deallocate(this.force_ptr(env_block_ptr)?, None, MiriMemoryKind::Env.into());
// If the function succeeds, the return value is nonzero.
Ok(result.is_ok() as i32)
Expand All @@ -188,8 +188,8 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
let target_os = &this.tcx.sess.target.target.target_os;
assert!(target_os == "linux" || target_os == "macos", "`setenv` is only available for the UNIX target family");

let name_ptr = this.read_scalar(name_op)?.not_undef()?;
let value_ptr = this.read_scalar(value_op)?.not_undef()?;
let name_ptr = this.read_scalar(name_op)?.check_init()?;
let value_ptr = this.read_scalar(value_op)?.check_init()?;

let mut new = None;
if !this.is_null(name_ptr)? {
Expand Down Expand Up @@ -224,14 +224,14 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
let mut this = self.eval_context_mut();
this.assert_target_os("windows", "SetEnvironmentVariableW");

let name_ptr = this.read_scalar(name_op)?.not_undef()?;
let value_ptr = this.read_scalar(value_op)?.not_undef()?;
let name_ptr = this.read_scalar(name_op)?.check_init()?;
let value_ptr = this.read_scalar(value_op)?.check_init()?;

if this.is_null(name_ptr)? {
// ERROR CODE is not clearly explained in docs.. For now, throw UB instead.
throw_ub_format!("pointer to environment variable name is NULL");
}

let name = this.read_os_str_from_wide_str(name_ptr)?;
if name.is_empty() {
throw_unsup_format!("environment variable name is an empty string");
Expand Down Expand Up @@ -261,7 +261,7 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
let target_os = &this.tcx.sess.target.target.target_os;
assert!(target_os == "linux" || target_os == "macos", "`unsetenv` is only available for the UNIX target family");

let name_ptr = this.read_scalar(name_op)?.not_undef()?;
let name_ptr = this.read_scalar(name_op)?.check_init()?;
let mut success = None;
if !this.is_null(name_ptr)? {
let name = this.read_os_str_from_c_str(name_ptr)?.to_owned();
Expand Down Expand Up @@ -295,7 +295,7 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx

this.check_no_isolation("getcwd")?;

let buf = this.read_scalar(buf_op)?.not_undef()?;
let buf = this.read_scalar(buf_op)?.check_init()?;
let size = this.read_scalar(size_op)?.to_machine_usize(&*this.tcx)?;
// If we cannot get the current directory, we return null
match env::current_dir() {
Expand Down Expand Up @@ -323,7 +323,7 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
this.check_no_isolation("GetCurrentDirectoryW")?;

let size = u64::from(this.read_scalar(size_op)?.to_u32()?);
let buf = this.read_scalar(buf_op)?.not_undef()?;
let buf = this.read_scalar(buf_op)?.check_init()?;

// If we cannot get the current directory, we return 0
match env::current_dir() {
Expand All @@ -341,7 +341,7 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx

this.check_no_isolation("chdir")?;

let path = this.read_path_from_c_str(this.read_scalar(path_op)?.not_undef()?)?;
let path = this.read_path_from_c_str(this.read_scalar(path_op)?.check_init()?)?;

match env::set_current_dir(path) {
Ok(()) => Ok(0),
Expand All @@ -362,7 +362,7 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx

this.check_no_isolation("SetCurrentDirectoryW")?;

let path = this.read_path_from_wide_str(this.read_scalar(path_op)?.not_undef()?)?;
let path = this.read_path_from_wide_str(this.read_scalar(path_op)?.check_init()?)?;

match env::set_current_dir(path) {
Ok(()) => Ok(1),
Expand All @@ -379,7 +379,7 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
let this = self.eval_context_mut();
// Deallocate the old environ list, if any.
if let Some(environ) = this.machine.env_vars.environ {
let old_vars_ptr = this.read_scalar(environ.into())?.not_undef()?;
let old_vars_ptr = this.read_scalar(environ.into())?.check_init()?;
this.memory.deallocate(this.force_ptr(old_vars_ptr)?, None, MiriMemoryKind::Env.into())?;
} else {
// No `environ` allocated yet, let's do that.
Expand Down
20 changes: 10 additions & 10 deletions src/shims/foreign_items.rs
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,7 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
// Miri-specific extern functions
"miri_static_root" => {
let &[ptr] = check_arg_count(args)?;
let ptr = this.read_scalar(ptr)?.not_undef()?;
let ptr = this.read_scalar(ptr)?.check_init()?;
let ptr = this.force_ptr(ptr)?;
if ptr.offset != Size::ZERO {
throw_unsup_format!("pointer passed to miri_static_root must point to beginning of an allocated block");
Expand All @@ -226,12 +226,12 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
}
"free" => {
let &[ptr] = check_arg_count(args)?;
let ptr = this.read_scalar(ptr)?.not_undef()?;
let ptr = this.read_scalar(ptr)?.check_init()?;
this.free(ptr, MiriMemoryKind::C)?;
}
"realloc" => {
let &[old_ptr, new_size] = check_arg_count(args)?;
let old_ptr = this.read_scalar(old_ptr)?.not_undef()?;
let old_ptr = this.read_scalar(old_ptr)?.check_init()?;
let new_size = this.read_scalar(new_size)?.to_machine_usize(this)?;
let res = this.realloc(old_ptr, new_size, MiriMemoryKind::C)?;
this.write_scalar(res, dest)?;
Expand Down Expand Up @@ -268,7 +268,7 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
}
"__rust_dealloc" => {
let &[ptr, old_size, align] = check_arg_count(args)?;
let ptr = this.read_scalar(ptr)?.not_undef()?;
let ptr = this.read_scalar(ptr)?.check_init()?;
let old_size = this.read_scalar(old_size)?.to_machine_usize(this)?;
let align = this.read_scalar(align)?.to_machine_usize(this)?;
// No need to check old_size/align; we anyway check that they match the allocation.
Expand All @@ -281,7 +281,7 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
}
"__rust_realloc" => {
let &[ptr, old_size, align, new_size] = check_arg_count(args)?;
let ptr = this.force_ptr(this.read_scalar(ptr)?.not_undef()?)?;
let ptr = this.force_ptr(this.read_scalar(ptr)?.check_init()?)?;
let old_size = this.read_scalar(old_size)?.to_machine_usize(this)?;
let align = this.read_scalar(align)?.to_machine_usize(this)?;
let new_size = this.read_scalar(new_size)?.to_machine_usize(this)?;
Expand All @@ -301,8 +301,8 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
// C memory handling functions
"memcmp" => {
let &[left, right, n] = check_arg_count(args)?;
let left = this.read_scalar(left)?.not_undef()?;
let right = this.read_scalar(right)?.not_undef()?;
let left = this.read_scalar(left)?.check_init()?;
let right = this.read_scalar(right)?.check_init()?;
let n = Size::from_bytes(this.read_scalar(n)?.to_machine_usize(this)?);

let result = {
Expand All @@ -321,7 +321,7 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
}
"memrchr" => {
let &[ptr, val, num] = check_arg_count(args)?;
let ptr = this.read_scalar(ptr)?.not_undef()?;
let ptr = this.read_scalar(ptr)?.check_init()?;
let val = this.read_scalar(val)?.to_i32()? as u8;
let num = this.read_scalar(num)?.to_machine_usize(this)?;
if let Some(idx) = this
Expand All @@ -339,7 +339,7 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
}
"memchr" => {
let &[ptr, val, num] = check_arg_count(args)?;
let ptr = this.read_scalar(ptr)?.not_undef()?;
let ptr = this.read_scalar(ptr)?.check_init()?;
let val = this.read_scalar(val)?.to_i32()? as u8;
let num = this.read_scalar(num)?.to_machine_usize(this)?;
let idx = this
Expand All @@ -356,7 +356,7 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
}
"strlen" => {
let &[ptr] = check_arg_count(args)?;
let ptr = this.read_scalar(ptr)?.not_undef()?;
let ptr = this.read_scalar(ptr)?.check_init()?;
let n = this.memory.read_c_str(ptr)?.len();
this.write_scalar(Scalar::from_machine_usize(u64::try_from(n).unwrap(), this), dest)?;
}
Expand Down
10 changes: 5 additions & 5 deletions src/shims/intrinsics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,9 +68,9 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx

let size = elem_layout.size.checked_mul(count, this)
.ok_or_else(|| err_ub_format!("overflow computing total size of `{}`", intrinsic_name))?;
let src = this.read_scalar(src)?.not_undef()?;
let src = this.read_scalar(src)?.check_init()?;
let src = this.memory.check_ptr_access(src, size, elem_align)?;
let dest = this.read_scalar(dest)?.not_undef()?;
let dest = this.read_scalar(dest)?.check_init()?;
let dest = this.memory.check_ptr_access(dest, size, elem_align)?;

if let (Some(src), Some(dest)) = (src, dest) {
Expand Down Expand Up @@ -105,7 +105,7 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
let ty = instance.substs.type_at(0);
let ty_layout = this.layout_of(ty)?;
let val_byte = this.read_scalar(val_byte)?.to_u8()?;
let ptr = this.read_scalar(ptr)?.not_undef()?;
let ptr = this.read_scalar(ptr)?.check_init()?;
let count = this.read_scalar(count)?.to_machine_usize(this)?;
let byte_count = ty_layout.size.checked_mul(count, this)
.ok_or_else(|| err_ub_format!("overflow computing total size of `write_bytes`"))?;
Expand Down Expand Up @@ -392,7 +392,7 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx

// `binary_op` will bail if either of them is not a scalar.
let eq = this.overflowing_binary_op(mir::BinOp::Eq, old, expect_old)?.0;
let res = Immediate::ScalarPair(old.to_scalar_or_undef(), eq.into());
let res = Immediate::ScalarPair(old.to_scalar_or_uninit(), eq.into());
// Return old value.
this.write_immediate(res, dest)?;
// Update ptr depending on comparison.
Expand Down Expand Up @@ -503,7 +503,7 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
// Other
"assume" => {
let &[cond] = check_arg_count(args)?;
let cond = this.read_scalar(cond)?.not_undef()?.to_bool()?;
let cond = this.read_scalar(cond)?.check_init()?.to_bool()?;
if !cond {
throw_ub_format!("`assume` intrinsic called with `false`");
}
Expand Down
4 changes: 2 additions & 2 deletions src/shims/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,14 +67,14 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
let (dest, ret) = ret.unwrap();

let req_align = this
.force_bits(this.read_scalar(align_op)?.not_undef()?, this.pointer_size())?;
.force_bits(this.read_scalar(align_op)?.check_init()?, this.pointer_size())?;

// Stop if the alignment is not a power of two.
if !req_align.is_power_of_two() {
return this.start_panic("align_offset: align is not a power-of-two", unwind);
}

let ptr_scalar = this.read_scalar(ptr_op)?.not_undef()?;
let ptr_scalar = this.read_scalar(ptr_op)?.check_init()?;

// Default: no result.
let mut result = this.machine_usize_max();
Expand Down
8 changes: 4 additions & 4 deletions src/shims/panic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx

// Get the raw pointer stored in arg[0] (the panic payload).
let &[payload] = check_arg_count(args)?;
let payload = this.read_scalar(payload)?.not_undef()?;
let payload = this.read_scalar(payload)?.check_init()?;
assert!(
this.machine.panic_payload.is_none(),
"the panic runtime should avoid double-panics"
Expand Down Expand Up @@ -81,9 +81,9 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx

// Get all the arguments.
let &[try_fn, data, catch_fn] = check_arg_count(args)?;
let try_fn = this.read_scalar(try_fn)?.not_undef()?;
let data = this.read_scalar(data)?.not_undef()?;
let catch_fn = this.read_scalar(catch_fn)?.not_undef()?;
let try_fn = this.read_scalar(try_fn)?.check_init()?;
let data = this.read_scalar(data)?.check_init()?;
let catch_fn = this.read_scalar(catch_fn)?.check_init()?;

// Now we make a function call, and pass `data` as first and only argument.
let f_instance = this.memory.get_fn(try_fn)?.as_instance()?;
Expand Down
Loading

0 comments on commit 345b033

Please sign in to comment.