Skip to content

Commit 1edba23

Browse files
authored
Merge branch 'master' into self-referential-generator
2 parents 7fe24a2 + e6948fa commit 1edba23

File tree

14 files changed

+109
-98
lines changed

14 files changed

+109
-98
lines changed

rust-version

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
nightly-2018-11-20
1+
nightly-2018-11-26

src/bin/miri-rustc-tests.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
#![feature(rustc_private, extern_crate_item_prelude)]
1+
#![feature(rustc_private)]
22
extern crate miri;
33
extern crate getopts;
44
extern crate rustc;

src/bin/miri.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
#![feature(rustc_private, extern_crate_item_prelude)]
1+
#![feature(rustc_private)]
22

33
extern crate getopts;
44
extern crate miri;

src/fn_call.rs

Lines changed: 35 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,8 @@ impl<'a, 'mir, 'tcx: 'mir + 'a> EvalContextExt<'tcx, 'mir> for super::MiriEvalCo
114114
None => self.tcx.item_name(def_id).as_str(),
115115
};
116116

117+
let tcx = &{self.tcx.tcx};
118+
117119
// All these functions take raw pointers, so if we access memory directly
118120
// (as opposed to through a place), we have to remember to erase any tag
119121
// that might still hang around!
@@ -124,7 +126,7 @@ impl<'a, 'mir, 'tcx: 'mir + 'a> EvalContextExt<'tcx, 'mir> for super::MiriEvalCo
124126
if size == 0 {
125127
self.write_null(dest)?;
126128
} else {
127-
let align = self.tcx.data_layout.pointer_align;
129+
let align = self.tcx.data_layout.pointer_align.abi;
128130
let ptr = self.memory_mut().allocate(Size::from_bytes(size), align, MiriMemoryKind::C.into())?;
129131
self.write_scalar(Scalar::Ptr(ptr.with_default_tag()), dest)?;
130132
}
@@ -153,7 +155,7 @@ impl<'a, 'mir, 'tcx: 'mir + 'a> EvalContextExt<'tcx, 'mir> for super::MiriEvalCo
153155
let ptr = self.memory_mut()
154156
.allocate(
155157
Size::from_bytes(size),
156-
Align::from_bytes(align, align).unwrap(),
158+
Align::from_bytes(align).unwrap(),
157159
MiriMemoryKind::Rust.into()
158160
)?
159161
.with_default_tag();
@@ -171,11 +173,13 @@ impl<'a, 'mir, 'tcx: 'mir + 'a> EvalContextExt<'tcx, 'mir> for super::MiriEvalCo
171173
let ptr = self.memory_mut()
172174
.allocate(
173175
Size::from_bytes(size),
174-
Align::from_bytes(align, align).unwrap(),
176+
Align::from_bytes(align).unwrap(),
175177
MiriMemoryKind::Rust.into()
176178
)?
177179
.with_default_tag();
178-
self.memory_mut().write_repeat(ptr.into(), 0, Size::from_bytes(size))?;
180+
self.memory_mut()
181+
.get_mut(ptr.alloc_id)?
182+
.write_repeat(tcx, ptr, 0, Size::from_bytes(size))?;
179183
self.write_scalar(Scalar::Ptr(ptr), dest)?;
180184
}
181185
"__rust_dealloc" => {
@@ -190,7 +194,7 @@ impl<'a, 'mir, 'tcx: 'mir + 'a> EvalContextExt<'tcx, 'mir> for super::MiriEvalCo
190194
}
191195
self.memory_mut().deallocate(
192196
ptr,
193-
Some((Size::from_bytes(old_size), Align::from_bytes(align, align).unwrap())),
197+
Some((Size::from_bytes(old_size), Align::from_bytes(align).unwrap())),
194198
MiriMemoryKind::Rust.into(),
195199
)?;
196200
}
@@ -208,9 +212,9 @@ impl<'a, 'mir, 'tcx: 'mir + 'a> EvalContextExt<'tcx, 'mir> for super::MiriEvalCo
208212
let new_ptr = self.memory_mut().reallocate(
209213
ptr,
210214
Size::from_bytes(old_size),
211-
Align::from_bytes(align, align).unwrap(),
215+
Align::from_bytes(align).unwrap(),
212216
Size::from_bytes(new_size),
213-
Align::from_bytes(align, align).unwrap(),
217+
Align::from_bytes(align).unwrap(),
214218
MiriMemoryKind::Rust.into(),
215219
)?;
216220
self.write_scalar(Scalar::Ptr(new_ptr.with_default_tag()), dest)?;
@@ -239,7 +243,7 @@ impl<'a, 'mir, 'tcx: 'mir + 'a> EvalContextExt<'tcx, 'mir> for super::MiriEvalCo
239243
"dlsym" => {
240244
let _handle = self.read_scalar(args[0])?;
241245
let symbol = self.read_scalar(args[1])?.to_ptr()?;
242-
let symbol_name = self.memory().read_c_str(symbol)?;
246+
let symbol_name = self.memory().get(symbol.alloc_id)?.read_c_str(tcx, symbol)?;
243247
let err = format!("bad c unicode symbol: {:?}", symbol_name);
244248
let symbol_name = ::std::str::from_utf8(symbol_name).unwrap_or(&err);
245249
return err!(Unimplemented(format!(
@@ -346,7 +350,7 @@ impl<'a, 'mir, 'tcx: 'mir + 'a> EvalContextExt<'tcx, 'mir> for super::MiriEvalCo
346350
"getenv" => {
347351
let result = {
348352
let name_ptr = self.read_scalar(args[0])?.to_ptr()?;
349-
let name = self.memory().read_c_str(name_ptr)?;
353+
let name = self.memory().get(name_ptr.alloc_id)?.read_c_str(tcx, name_ptr)?;
350354
match self.machine.env_vars.get(name) {
351355
Some(&var) => Scalar::Ptr(var),
352356
None => Scalar::ptr_null(&*self.tcx),
@@ -360,8 +364,8 @@ impl<'a, 'mir, 'tcx: 'mir + 'a> EvalContextExt<'tcx, 'mir> for super::MiriEvalCo
360364
{
361365
let name_ptr = self.read_scalar(args[0])?.not_undef()?;
362366
if !name_ptr.is_null_ptr(self) {
363-
let name = self.memory().read_c_str(name_ptr.to_ptr()?
364-
)?.to_owned();
367+
let name_ptr = name_ptr.to_ptr()?;
368+
let name = self.memory().get(name_ptr.alloc_id)?.read_c_str(tcx, name_ptr)?.to_owned();
365369
if !name.is_empty() && !name.contains(&b'=') {
366370
success = Some(self.machine.env_vars.remove(&name));
367371
}
@@ -382,9 +386,10 @@ impl<'a, 'mir, 'tcx: 'mir + 'a> EvalContextExt<'tcx, 'mir> for super::MiriEvalCo
382386
{
383387
let name_ptr = self.read_scalar(args[0])?.not_undef()?;
384388
let value_ptr = self.read_scalar(args[1])?.to_ptr()?;
385-
let value = self.memory().read_c_str(value_ptr)?;
389+
let value = self.memory().get(value_ptr.alloc_id)?.read_c_str(tcx, value_ptr)?;
386390
if !name_ptr.is_null_ptr(self) {
387-
let name = self.memory().read_c_str(name_ptr.to_ptr()?)?;
391+
let name_ptr = name_ptr.to_ptr()?;
392+
let name = self.memory().get(name_ptr.alloc_id)?.read_c_str(tcx, name_ptr)?;
388393
if !name.is_empty() && !name.contains(&b'=') {
389394
new = Some((name.to_owned(), value.to_owned()));
390395
}
@@ -394,12 +399,15 @@ impl<'a, 'mir, 'tcx: 'mir + 'a> EvalContextExt<'tcx, 'mir> for super::MiriEvalCo
394399
// +1 for the null terminator
395400
let value_copy = self.memory_mut().allocate(
396401
Size::from_bytes((value.len() + 1) as u64),
397-
Align::from_bytes(1, 1).unwrap(),
402+
Align::from_bytes(1).unwrap(),
398403
MiriMemoryKind::Env.into(),
399404
)?.with_default_tag();
400-
self.memory_mut().write_bytes(value_copy.into(), &value)?;
401-
let trailing_zero_ptr = value_copy.offset(Size::from_bytes(value.len() as u64), self)?.into();
402-
self.memory_mut().write_bytes(trailing_zero_ptr, &[0])?;
405+
{
406+
let alloc = self.memory_mut().get_mut(value_copy.alloc_id)?;
407+
alloc.write_bytes(tcx, value_copy, &value)?;
408+
let trailing_zero_ptr = value_copy.offset(Size::from_bytes(value.len() as u64), tcx)?;
409+
alloc.write_bytes(tcx, trailing_zero_ptr, &[0])?;
410+
}
403411
if let Some(var) = self.machine.env_vars.insert(
404412
name.to_owned(),
405413
value_copy,
@@ -444,7 +452,7 @@ impl<'a, 'mir, 'tcx: 'mir + 'a> EvalContextExt<'tcx, 'mir> for super::MiriEvalCo
444452

445453
"strlen" => {
446454
let ptr = self.read_scalar(args[0])?.to_ptr()?;
447-
let n = self.memory().read_c_str(ptr)?.len();
455+
let n = self.memory().get(ptr.alloc_id)?.read_c_str(tcx, ptr)?.len();
448456
self.write_scalar(Scalar::from_uint(n as u64, dest.layout.size), dest)?;
449457
}
450458

@@ -469,10 +477,9 @@ impl<'a, 'mir, 'tcx: 'mir + 'a> EvalContextExt<'tcx, 'mir> for super::MiriEvalCo
469477
instance,
470478
promoted: None,
471479
};
472-
let const_val = self.const_eval(cid)?;
473-
let value = const_val.unwrap_bits(
474-
self.tcx.tcx,
475-
ty::ParamEnv::empty().and(self.tcx.types.i32)) as i32;
480+
let const_val = self.const_eval_raw(cid)?;
481+
let const_val = self.read_scalar(const_val.into())?;
482+
let value = const_val.to_i32()?;
476483
if value == name {
477484
result = Some(path_value);
478485
break;
@@ -508,13 +515,15 @@ impl<'a, 'mir, 'tcx: 'mir + 'a> EvalContextExt<'tcx, 'mir> for super::MiriEvalCo
508515
let key_layout = self.layout_of(key_type)?;
509516

510517
// Create key and write it into the memory where key_ptr wants it
511-
let key = self.machine.tls.create_tls_key(dtor, &*self.tcx) as u128;
518+
let key = self.machine.tls.create_tls_key(dtor, tcx) as u128;
512519
if key_layout.size.bits() < 128 && key >= (1u128 << key_layout.size.bits() as u128) {
513520
return err!(OutOfTls);
514521
}
515-
self.memory_mut().write_scalar(
522+
523+
self.memory().check_align(key_ptr.into(), key_layout.align.abi)?;
524+
self.memory_mut().get_mut(key_ptr.alloc_id)?.write_scalar(
525+
tcx,
516526
key_ptr,
517-
key_layout.align,
518527
Scalar::from_uint(key, key_layout.size).into(),
519528
key_layout.size,
520529
)?;
@@ -611,7 +620,7 @@ impl<'a, 'mir, 'tcx: 'mir + 'a> EvalContextExt<'tcx, 'mir> for super::MiriEvalCo
611620
// This just creates a key; Windows does not natively support TLS dtors.
612621

613622
// Create key and return it
614-
let key = self.machine.tls.create_tls_key(None, &*self.tcx) as u128;
623+
let key = self.machine.tls.create_tls_key(None, tcx) as u128;
615624

616625
// Figure out how large a TLS key actually is. This is c::DWORD.
617626
if dest.layout.size.bits() < 128 && key >= (1u128 << dest.layout.size.bits() as u128) {

src/helpers.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -130,9 +130,10 @@ impl<'a, 'mir, 'tcx> EvalContextExt<'tcx> for EvalContext<'a, 'mir, 'tcx, super:
130130
unsafe_cell_action: |place| {
131131
trace!("unsafe_cell_action on {:?}", place.ptr);
132132
// We need a size to go on.
133-
let (unsafe_cell_size, _) = self.size_and_align_of_mplace(place)?
133+
let unsafe_cell_size = self.size_and_align_of_mplace(place)?
134+
.map(|(size, _)| size)
134135
// for extern types, just cover what we can
135-
.unwrap_or_else(|| place.layout.size_and_align());
136+
.unwrap_or_else(|| place.layout.size);
136137
// Now handle this `UnsafeCell`, unless it is empty.
137138
if unsafe_cell_size != Size::ZERO {
138139
unsafe_cell_action(place.ptr, unsafe_cell_size)

src/intrinsic.rs

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ impl<'a, 'mir, 'tcx> EvalContextExt<'tcx> for super::MiriEvalContext<'a, 'mir, '
2828
if self.emulate_intrinsic(instance, args, dest)? {
2929
return Ok(());
3030
}
31-
31+
let tcx = &{self.tcx.tcx};
3232
let substs = instance.substs;
3333

3434
// All these intrinsics take raw pointers, so if we access memory directly
@@ -152,7 +152,7 @@ impl<'a, 'mir, 'tcx> EvalContextExt<'tcx> for super::MiriEvalContext<'a, 'mir, '
152152
let elem_layout = self.layout_of(elem_ty)?;
153153
let elem_size = elem_layout.size.bytes();
154154
let count = self.read_scalar(args[2])?.to_usize(self)?;
155-
let elem_align = elem_layout.align;
155+
let elem_align = elem_layout.align.abi;
156156
// erase tags: this is a raw ptr operation
157157
let src = self.read_scalar(args[0])?.not_undef()?;
158158
let dest = self.read_scalar(args[1])?.not_undef()?;
@@ -248,6 +248,7 @@ impl<'a, 'mir, 'tcx> EvalContextExt<'tcx> for super::MiriEvalContext<'a, 'mir, '
248248
// FIXME: We do not properly validate in case of ZSTs and when doing it in memory!
249249
// However, this only affects direct calls of the intrinsic; calls to the stable
250250
// functions wrapping them do get their validation.
251+
// FIXME: should we check that the destination pointer is aligned even for ZSTs?
251252
if !dest.layout.is_zst() { // nothing to do for ZST
252253
match dest.layout.abi {
253254
layout::Abi::Scalar(ref s) => {
@@ -263,7 +264,9 @@ impl<'a, 'mir, 'tcx> EvalContextExt<'tcx> for super::MiriEvalContext<'a, 'mir, '
263264
// Do it in memory
264265
let mplace = self.force_allocation(dest)?;
265266
assert!(mplace.meta.is_none());
266-
self.memory_mut().write_repeat(mplace.ptr, 0, dest.layout.size)?;
267+
// not a zst, must be valid pointer
268+
let ptr = mplace.ptr.to_ptr()?;
269+
self.memory_mut().get_mut(ptr.alloc_id)?.write_repeat(tcx, ptr, 0, dest.layout.size)?;
267270
}
268271
}
269272
}
@@ -272,7 +275,7 @@ impl<'a, 'mir, 'tcx> EvalContextExt<'tcx> for super::MiriEvalContext<'a, 'mir, '
272275
"pref_align_of" => {
273276
let ty = substs.type_at(0);
274277
let layout = self.layout_of(ty)?;
275-
let align = layout.align.pref();
278+
let align = layout.align.pref.bytes();
276279
let ptr_size = self.pointer_size();
277280
let align_val = Scalar::from_uint(align as u128, ptr_size);
278281
self.write_scalar(align_val, dest)?;
@@ -364,7 +367,7 @@ impl<'a, 'mir, 'tcx> EvalContextExt<'tcx> for super::MiriEvalContext<'a, 'mir, '
364367
.expect("size_of_val called on extern type");
365368
let ptr_size = self.pointer_size();
366369
self.write_scalar(
367-
Scalar::from_uint(align.abi(), ptr_size),
370+
Scalar::from_uint(align.bytes(), ptr_size),
368371
dest,
369372
)?;
370373
}
@@ -412,6 +415,7 @@ impl<'a, 'mir, 'tcx> EvalContextExt<'tcx> for super::MiriEvalContext<'a, 'mir, '
412415
// FIXME: We do not properly validate in case of ZSTs and when doing it in memory!
413416
// However, this only affects direct calls of the intrinsic; calls to the stable
414417
// functions wrapping them do get their validation.
418+
// FIXME: should we check alignment for ZSTs?
415419
if !dest.layout.is_zst() { // nothing to do for ZST
416420
match dest.layout.abi {
417421
layout::Abi::Scalar(..) => {
@@ -426,7 +430,10 @@ impl<'a, 'mir, 'tcx> EvalContextExt<'tcx> for super::MiriEvalContext<'a, 'mir, '
426430
// Do it in memory
427431
let mplace = self.force_allocation(dest)?;
428432
assert!(mplace.meta.is_none());
429-
self.memory_mut().mark_definedness(mplace.ptr.to_ptr()?, dest.layout.size, false)?;
433+
let ptr = mplace.ptr.to_ptr()?;
434+
self.memory_mut()
435+
.get_mut(ptr.alloc_id)?
436+
.mark_definedness(ptr, dest.layout.size, false)?;
430437
}
431438
}
432439
}
@@ -438,8 +445,14 @@ impl<'a, 'mir, 'tcx> EvalContextExt<'tcx> for super::MiriEvalContext<'a, 'mir, '
438445
let val_byte = self.read_scalar(args[1])?.to_u8()?;
439446
let ptr = self.read_scalar(args[0])?.not_undef()?;
440447
let count = self.read_scalar(args[2])?.to_usize(self)?;
441-
self.memory().check_align(ptr, ty_layout.align)?;
442-
self.memory_mut().write_repeat(ptr, val_byte, ty_layout.size * count)?;
448+
self.memory().check_align(ptr, ty_layout.align.abi)?;
449+
let byte_count = ty_layout.size * count;
450+
if byte_count.bytes() != 0 {
451+
let ptr = ptr.to_ptr()?;
452+
self.memory_mut()
453+
.get_mut(ptr.alloc_id)?
454+
.write_repeat(tcx, ptr, val_byte, byte_count)?;
455+
}
443456
}
444457

445458
name => return err!(Unimplemented(format!("unimplemented intrinsic: {}", name))),

src/lib.rs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
#![feature(rustc_private, extern_crate_item_prelude)]
1+
#![feature(rustc_private)]
22

33
#![cfg_attr(feature = "cargo-clippy", allow(cast_lossless))]
44

@@ -397,7 +397,7 @@ impl<'a, 'mir, 'tcx> Machine<'a, 'mir, 'tcx> for Evaluator<'tcx> {
397397

398398
// Second argument: align
399399
let arg = ecx.eval_place(&mir::Place::Local(args.next().unwrap()))?;
400-
let align = layout.align.abi();
400+
let align = layout.align.abi.bytes();
401401
ecx.write_scalar(Scalar::from_uint(align, arg.layout.size), arg)?;
402402

403403
// No more arguments
@@ -419,7 +419,7 @@ impl<'a, 'mir, 'tcx> Machine<'a, 'mir, 'tcx> for Evaluator<'tcx> {
419419
"__cxa_thread_atexit_impl" => {
420420
// This should be all-zero, pointer-sized
421421
let data = vec![0; tcx.data_layout.pointer_size.bytes() as usize];
422-
Allocation::from_bytes(&data[..], tcx.data_layout.pointer_align)
422+
Allocation::from_bytes(&data[..], tcx.data_layout.pointer_align.abi)
423423
}
424424
_ => return err!(Unimplemented(
425425
format!("can't access foreign static: {}", link_name),
@@ -458,9 +458,9 @@ impl<'a, 'mir, 'tcx> Machine<'a, 'mir, 'tcx> for Evaluator<'tcx> {
458458
place: MPlaceTy<'tcx, Borrow>,
459459
mutability: Option<hir::Mutability>,
460460
) -> EvalResult<'tcx, Scalar<Borrow>> {
461-
let (size, _) = ecx.size_and_align_of_mplace(place)?
461+
let size = ecx.size_and_align_of_mplace(place)?.map(|(size, _)| size)
462462
// for extern types, just cover what we can
463-
.unwrap_or_else(|| place.layout.size_and_align());
463+
.unwrap_or_else(|| place.layout.size);
464464
if !ecx.tcx.sess.opts.debugging_opts.mir_emit_retag ||
465465
!Self::enforce_validity(ecx) || size == Size::ZERO
466466
{
@@ -498,9 +498,9 @@ impl<'a, 'mir, 'tcx> Machine<'a, 'mir, 'tcx> for Evaluator<'tcx> {
498498
// This is deliberately NOT `deref_operand` as we do not want `tag_dereference`
499499
// to be called! That would kill the original tag if we got a raw ptr.
500500
let place = ecx.ref_to_mplace(ecx.read_immediate(ptr)?)?;
501-
let (size, _) = ecx.size_and_align_of_mplace(place)?
501+
let size = ecx.size_and_align_of_mplace(place)?.map(|(size, _)| size)
502502
// for extern types, just cover what we can
503-
.unwrap_or_else(|| place.layout.size_and_align());
503+
.unwrap_or_else(|| place.layout.size);
504504
if !ecx.tcx.sess.opts.debugging_opts.mir_emit_retag ||
505505
!ecx.machine.validate || size == Size::ZERO
506506
{

0 commit comments

Comments
 (0)