Skip to content

Commit 40571e8

Browse files
committed
fix horrible bug that never allocated memory if native-lib was off
1 parent fc99b4c commit 40571e8

File tree

1 file changed

+13
-9
lines changed

1 file changed

+13
-9
lines changed

src/tools/miri/src/machine.rs

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1244,16 +1244,20 @@ impl<'tcx> Machine<'tcx> for MiriMachine<'tcx> {
12441244
// based on their particular choice for `Provenance`, `AllocExtra`, and `Bytes`.
12451245
let kind = Self::GLOBAL_KIND
12461246
.expect("if GLOBAL_KIND is None, adjust_global_allocation must be overwritten");
1247-
// This closure is special in that it always relies on `take_prepared_alloc_bytes`
1248-
// to give it some reserved memory space.
1247+
// This closure is special in that it relies on `take_prepared_alloc_bytes` to give it some
1248+
// reserved memory space in case Miri needs to handle addresses especially for native calls.
12491249
let alloc = alloc.adjust_from_tcx(&ecx.tcx, |bytes, align| {
1250-
assert_eq!(alloc.align, align);
1251-
let kind = Self::GLOBAL_KIND.unwrap().into();
1252-
let mut alloc_bytes = ecx.take_prepared_alloc_bytes(id, kind)?;
1253-
assert_eq!(alloc_bytes.len(), bytes.len());
1254-
// SAFETY: `alloc_bytes` and `bytes` span the same number of bytes.
1255-
unsafe { alloc_bytes.as_mut_ptr().copy_from(bytes.as_ptr(), bytes.len()) };
1256-
Ok(alloc_bytes)
1250+
Ok(if ecx.machine.native_lib.is_some() {
1251+
assert_eq!(alloc.align, align);
1252+
let kind = Self::GLOBAL_KIND.unwrap().into();
1253+
let mut alloc_bytes = ecx.take_prepared_alloc_bytes(id, kind)?;
1254+
assert_eq!(alloc_bytes.len(), bytes.len());
1255+
// SAFETY: `alloc_bytes` and `bytes` span the same number of bytes.
1256+
unsafe { alloc_bytes.as_mut_ptr().copy_from(bytes.as_ptr(), bytes.len()) };
1257+
alloc_bytes
1258+
} else {
1259+
MiriAllocBytes::from_bytes(Cow::Borrowed(&*bytes), align)
1260+
})
12571261
},
12581262
|ptr| ecx.global_root_pointer(ptr),
12591263
)?;

0 commit comments

Comments
 (0)