Skip to content

Commit 345b033

Browse files
committed
Auto merge of #1487 - pnadon:miri-rename-undef-uninit, r=RalfJung
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`
2 parents 91b58c9 + 6dd700f commit 345b033

19 files changed

+99
-99
lines changed

rust-version

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
0e11fc8053d32c44e7152865852acc5c3c54efb3
1+
13f9aa190957b993a268fd4a046fce76ca8814ee

src/eval.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -234,7 +234,7 @@ pub fn eval_main<'tcx>(tcx: TyCtxt<'tcx>, main_id: DefId, config: MiriConfig) ->
234234
}
235235
}
236236
}
237-
let return_code = ecx.read_scalar(ret_place.into())?.not_undef()?.to_machine_isize(&ecx)?;
237+
let return_code = ecx.read_scalar(ret_place.into())?.check_init()?.to_machine_isize(&ecx)?;
238238
Ok(return_code)
239239
})();
240240

src/helpers.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
6767
fn eval_libc(&mut self, name: &str) -> InterpResult<'tcx, Scalar<Tag>> {
6868
self.eval_context_mut()
6969
.eval_path_scalar(&["libc", name])?
70-
.not_undef()
70+
.check_init()
7171
}
7272

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

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

413413
/// Sets the last OS error using a `std::io::Error`. This function tries to produce the most
@@ -467,7 +467,7 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
467467
}
468468
}
469469
}
470-
470+
471471
fn read_scalar_at_offset(
472472
&self,
473473
op: OpTy<'tcx, Tag>,

src/operator.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,11 +32,11 @@ impl<'mir, 'tcx> EvalContextExt<'tcx> for super::MiriEvalContext<'mir, 'tcx> {
3232
#[rustfmt::skip]
3333
let eq = match (*left, *right) {
3434
(Immediate::Scalar(left), Immediate::Scalar(right)) => {
35-
self.ptr_eq(left.not_undef()?, right.not_undef()?)?
35+
self.ptr_eq(left.check_init()?, right.check_init()?)?
3636
}
3737
(Immediate::ScalarPair(left1, left2), Immediate::ScalarPair(right1, right2)) => {
38-
self.ptr_eq(left1.not_undef()?, right1.not_undef()?)?
39-
&& self.ptr_eq(left2.not_undef()?, right2.not_undef()?)?
38+
self.ptr_eq(left1.check_init()?, right1.check_init()?)?
39+
&& self.ptr_eq(left2.check_init()?, right2.check_init()?)?
4040
}
4141
_ => bug!("Type system should not allow comparing Scalar with ScalarPair"),
4242
};

src/shims/env.rs

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ impl<'tcx> EnvVars<'tcx> {
6969
}
7070
// Deallocate environ var list.
7171
let environ = ecx.machine.env_vars.environ.unwrap();
72-
let old_vars_ptr = ecx.read_scalar(environ.into())?.not_undef()?;
72+
let old_vars_ptr = ecx.read_scalar(environ.into())?.check_init()?;
7373
ecx.memory.deallocate(ecx.force_ptr(old_vars_ptr)?, None, MiriMemoryKind::Env.into())?;
7474
Ok(())
7575
}
@@ -104,7 +104,7 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
104104
let target_os = &this.tcx.sess.target.target.target_os;
105105
assert!(target_os == "linux" || target_os == "macos", "`getenv` is only available for the UNIX target family");
106106

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

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

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

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

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

191-
let name_ptr = this.read_scalar(name_op)?.not_undef()?;
192-
let value_ptr = this.read_scalar(value_op)?.not_undef()?;
191+
let name_ptr = this.read_scalar(name_op)?.check_init()?;
192+
let value_ptr = this.read_scalar(value_op)?.check_init()?;
193193

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

227-
let name_ptr = this.read_scalar(name_op)?.not_undef()?;
228-
let value_ptr = this.read_scalar(value_op)?.not_undef()?;
227+
let name_ptr = this.read_scalar(name_op)?.check_init()?;
228+
let value_ptr = this.read_scalar(value_op)?.check_init()?;
229229

230230
if this.is_null(name_ptr)? {
231231
// ERROR CODE is not clearly explained in docs.. For now, throw UB instead.
232232
throw_ub_format!("pointer to environment variable name is NULL");
233233
}
234-
234+
235235
let name = this.read_os_str_from_wide_str(name_ptr)?;
236236
if name.is_empty() {
237237
throw_unsup_format!("environment variable name is an empty string");
@@ -261,7 +261,7 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
261261
let target_os = &this.tcx.sess.target.target.target_os;
262262
assert!(target_os == "linux" || target_os == "macos", "`unsetenv` is only available for the UNIX target family");
263263

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

296296
this.check_no_isolation("getcwd")?;
297297

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

325325
let size = u64::from(this.read_scalar(size_op)?.to_u32()?);
326-
let buf = this.read_scalar(buf_op)?.not_undef()?;
326+
let buf = this.read_scalar(buf_op)?.check_init()?;
327327

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

342342
this.check_no_isolation("chdir")?;
343343

344-
let path = this.read_path_from_c_str(this.read_scalar(path_op)?.not_undef()?)?;
344+
let path = this.read_path_from_c_str(this.read_scalar(path_op)?.check_init()?)?;
345345

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

363363
this.check_no_isolation("SetCurrentDirectoryW")?;
364364

365-
let path = this.read_path_from_wide_str(this.read_scalar(path_op)?.not_undef()?)?;
365+
let path = this.read_path_from_wide_str(this.read_scalar(path_op)?.check_init()?)?;
366366

367367
match env::set_current_dir(path) {
368368
Ok(()) => Ok(1),
@@ -379,7 +379,7 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
379379
let this = self.eval_context_mut();
380380
// Deallocate the old environ list, if any.
381381
if let Some(environ) = this.machine.env_vars.environ {
382-
let old_vars_ptr = this.read_scalar(environ.into())?.not_undef()?;
382+
let old_vars_ptr = this.read_scalar(environ.into())?.check_init()?;
383383
this.memory.deallocate(this.force_ptr(old_vars_ptr)?, None, MiriMemoryKind::Env.into())?;
384384
} else {
385385
// No `environ` allocated yet, let's do that.

src/shims/foreign_items.rs

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -200,7 +200,7 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
200200
// Miri-specific extern functions
201201
"miri_static_root" => {
202202
let &[ptr] = check_arg_count(args)?;
203-
let ptr = this.read_scalar(ptr)?.not_undef()?;
203+
let ptr = this.read_scalar(ptr)?.check_init()?;
204204
let ptr = this.force_ptr(ptr)?;
205205
if ptr.offset != Size::ZERO {
206206
throw_unsup_format!("pointer passed to miri_static_root must point to beginning of an allocated block");
@@ -226,12 +226,12 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
226226
}
227227
"free" => {
228228
let &[ptr] = check_arg_count(args)?;
229-
let ptr = this.read_scalar(ptr)?.not_undef()?;
229+
let ptr = this.read_scalar(ptr)?.check_init()?;
230230
this.free(ptr, MiriMemoryKind::C)?;
231231
}
232232
"realloc" => {
233233
let &[old_ptr, new_size] = check_arg_count(args)?;
234-
let old_ptr = this.read_scalar(old_ptr)?.not_undef()?;
234+
let old_ptr = this.read_scalar(old_ptr)?.check_init()?;
235235
let new_size = this.read_scalar(new_size)?.to_machine_usize(this)?;
236236
let res = this.realloc(old_ptr, new_size, MiriMemoryKind::C)?;
237237
this.write_scalar(res, dest)?;
@@ -268,7 +268,7 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
268268
}
269269
"__rust_dealloc" => {
270270
let &[ptr, old_size, align] = check_arg_count(args)?;
271-
let ptr = this.read_scalar(ptr)?.not_undef()?;
271+
let ptr = this.read_scalar(ptr)?.check_init()?;
272272
let old_size = this.read_scalar(old_size)?.to_machine_usize(this)?;
273273
let align = this.read_scalar(align)?.to_machine_usize(this)?;
274274
// No need to check old_size/align; we anyway check that they match the allocation.
@@ -281,7 +281,7 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
281281
}
282282
"__rust_realloc" => {
283283
let &[ptr, old_size, align, new_size] = check_arg_count(args)?;
284-
let ptr = this.force_ptr(this.read_scalar(ptr)?.not_undef()?)?;
284+
let ptr = this.force_ptr(this.read_scalar(ptr)?.check_init()?)?;
285285
let old_size = this.read_scalar(old_size)?.to_machine_usize(this)?;
286286
let align = this.read_scalar(align)?.to_machine_usize(this)?;
287287
let new_size = this.read_scalar(new_size)?.to_machine_usize(this)?;
@@ -301,8 +301,8 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
301301
// C memory handling functions
302302
"memcmp" => {
303303
let &[left, right, n] = check_arg_count(args)?;
304-
let left = this.read_scalar(left)?.not_undef()?;
305-
let right = this.read_scalar(right)?.not_undef()?;
304+
let left = this.read_scalar(left)?.check_init()?;
305+
let right = this.read_scalar(right)?.check_init()?;
306306
let n = Size::from_bytes(this.read_scalar(n)?.to_machine_usize(this)?);
307307

308308
let result = {
@@ -321,7 +321,7 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
321321
}
322322
"memrchr" => {
323323
let &[ptr, val, num] = check_arg_count(args)?;
324-
let ptr = this.read_scalar(ptr)?.not_undef()?;
324+
let ptr = this.read_scalar(ptr)?.check_init()?;
325325
let val = this.read_scalar(val)?.to_i32()? as u8;
326326
let num = this.read_scalar(num)?.to_machine_usize(this)?;
327327
if let Some(idx) = this
@@ -339,7 +339,7 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
339339
}
340340
"memchr" => {
341341
let &[ptr, val, num] = check_arg_count(args)?;
342-
let ptr = this.read_scalar(ptr)?.not_undef()?;
342+
let ptr = this.read_scalar(ptr)?.check_init()?;
343343
let val = this.read_scalar(val)?.to_i32()? as u8;
344344
let num = this.read_scalar(num)?.to_machine_usize(this)?;
345345
let idx = this
@@ -356,7 +356,7 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
356356
}
357357
"strlen" => {
358358
let &[ptr] = check_arg_count(args)?;
359-
let ptr = this.read_scalar(ptr)?.not_undef()?;
359+
let ptr = this.read_scalar(ptr)?.check_init()?;
360360
let n = this.memory.read_c_str(ptr)?.len();
361361
this.write_scalar(Scalar::from_machine_usize(u64::try_from(n).unwrap(), this), dest)?;
362362
}

src/shims/intrinsics.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -68,9 +68,9 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
6868

6969
let size = elem_layout.size.checked_mul(count, this)
7070
.ok_or_else(|| err_ub_format!("overflow computing total size of `{}`", intrinsic_name))?;
71-
let src = this.read_scalar(src)?.not_undef()?;
71+
let src = this.read_scalar(src)?.check_init()?;
7272
let src = this.memory.check_ptr_access(src, size, elem_align)?;
73-
let dest = this.read_scalar(dest)?.not_undef()?;
73+
let dest = this.read_scalar(dest)?.check_init()?;
7474
let dest = this.memory.check_ptr_access(dest, size, elem_align)?;
7575

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

393393
// `binary_op` will bail if either of them is not a scalar.
394394
let eq = this.overflowing_binary_op(mir::BinOp::Eq, old, expect_old)?.0;
395-
let res = Immediate::ScalarPair(old.to_scalar_or_undef(), eq.into());
395+
let res = Immediate::ScalarPair(old.to_scalar_or_uninit(), eq.into());
396396
// Return old value.
397397
this.write_immediate(res, dest)?;
398398
// Update ptr depending on comparison.
@@ -503,7 +503,7 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
503503
// Other
504504
"assume" => {
505505
let &[cond] = check_arg_count(args)?;
506-
let cond = this.read_scalar(cond)?.not_undef()?.to_bool()?;
506+
let cond = this.read_scalar(cond)?.check_init()?.to_bool()?;
507507
if !cond {
508508
throw_ub_format!("`assume` intrinsic called with `false`");
509509
}

src/shims/mod.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,14 +67,14 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
6767
let (dest, ret) = ret.unwrap();
6868

6969
let req_align = this
70-
.force_bits(this.read_scalar(align_op)?.not_undef()?, this.pointer_size())?;
70+
.force_bits(this.read_scalar(align_op)?.check_init()?, this.pointer_size())?;
7171

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

77-
let ptr_scalar = this.read_scalar(ptr_op)?.not_undef()?;
77+
let ptr_scalar = this.read_scalar(ptr_op)?.check_init()?;
7878

7979
// Default: no result.
8080
let mut result = this.machine_usize_max();

src/shims/panic.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
4747

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

8282
// Get all the arguments.
8383
let &[try_fn, data, catch_fn] = check_arg_count(args)?;
84-
let try_fn = this.read_scalar(try_fn)?.not_undef()?;
85-
let data = this.read_scalar(data)?.not_undef()?;
86-
let catch_fn = this.read_scalar(catch_fn)?.not_undef()?;
84+
let try_fn = this.read_scalar(try_fn)?.check_init()?;
85+
let data = this.read_scalar(data)?.check_init()?;
86+
let catch_fn = this.read_scalar(catch_fn)?.check_init()?;
8787

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

0 commit comments

Comments
 (0)