Skip to content

Commit 146a8ea

Browse files
committed
miri: fix alignment issues in volatile_memory tests
These tests implicitly assumed that the backing memory of a Vec<u8> is aligned to 8 bytes. This is seemingly always the case with the standard allocator used by rustc, however miri's allocator will happily align it to 1 byte boundaries, causing spurious failures under miri. This commit explicitly aligns the memory to 8 bytes. Signed-off-by: Patrick Roy <[email protected]>
1 parent 396aeb6 commit 146a8ea

File tree

1 file changed

+23
-22
lines changed

1 file changed

+23
-22
lines changed

src/volatile_memory.rs

Lines changed: 23 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -2032,11 +2032,14 @@ mod tests {
20322032

20332033
#[test]
20342034
fn test_atomic_accesses() {
2035-
let mut backing = vec![0u8; 0x1000];
2036-
let a = VolatileSlice::from(backing.as_mut_slice());
2037-
let s = a.as_volatile_slice();
2035+
let len = 0x1000;
2036+
let buf = unsafe { std::alloc::alloc_zeroed(Layout::from_size_align(len, 8).unwrap()) };
2037+
let a = unsafe { VolatileSlice::new(buf, len) };
20382038

2039-
crate::bytes::tests::check_atomic_accesses(s, 0, 0x1000);
2039+
crate::bytes::tests::check_atomic_accesses(a, 0, 0x1000);
2040+
unsafe {
2041+
std::alloc::dealloc(buf, Layout::from_size_align(len, 8).unwrap());
2042+
}
20402043
}
20412044

20422045
#[test]
@@ -2067,14 +2070,13 @@ mod tests {
20672070
let dirty_len = size_of_val(&val);
20682071
let page_size = 0x1000;
20692072

2070-
let mut buf = vec![0u8; 0x10000];
2073+
let len = 0x10000;
2074+
let buf = unsafe { std::alloc::alloc_zeroed(Layout::from_size_align(len, 8).unwrap()) };
20712075

20722076
// Invoke the `Bytes` test helper function.
20732077
{
2074-
let bitmap = AtomicBitmap::new(buf.len(), page_size);
2075-
let slice = unsafe {
2076-
VolatileSlice::with_bitmap(buf.as_mut_ptr(), buf.len(), bitmap.slice_at(0))
2077-
};
2078+
let bitmap = AtomicBitmap::new(len, page_size);
2079+
let slice = unsafe { VolatileSlice::with_bitmap(buf, len, bitmap.slice_at(0)) };
20782080

20792081
test_bytes(
20802082
&slice,
@@ -2089,24 +2091,19 @@ mod tests {
20892091

20902092
// Invoke the `VolatileMemory` test helper function.
20912093
{
2092-
let bitmap = AtomicBitmap::new(buf.len(), page_size);
2093-
let slice = unsafe {
2094-
VolatileSlice::with_bitmap(buf.as_mut_ptr(), buf.len(), bitmap.slice_at(0))
2095-
};
2094+
let bitmap = AtomicBitmap::new(len, page_size);
2095+
let slice = unsafe { VolatileSlice::with_bitmap(buf, len, bitmap.slice_at(0)) };
20962096
test_volatile_memory(&slice);
20972097
}
20982098

2099-
let bitmap = AtomicBitmap::new(buf.len(), page_size);
2100-
let slice =
2101-
unsafe { VolatileSlice::with_bitmap(buf.as_mut_ptr(), buf.len(), bitmap.slice_at(0)) };
2099+
let bitmap = AtomicBitmap::new(len, page_size);
2100+
let slice = unsafe { VolatileSlice::with_bitmap(buf, len, bitmap.slice_at(0)) };
21022101

2103-
let bitmap2 = AtomicBitmap::new(buf.len(), page_size);
2104-
let slice2 =
2105-
unsafe { VolatileSlice::with_bitmap(buf.as_mut_ptr(), buf.len(), bitmap2.slice_at(0)) };
2102+
let bitmap2 = AtomicBitmap::new(len, page_size);
2103+
let slice2 = unsafe { VolatileSlice::with_bitmap(buf, len, bitmap2.slice_at(0)) };
21062104

2107-
let bitmap3 = AtomicBitmap::new(buf.len(), page_size);
2108-
let slice3 =
2109-
unsafe { VolatileSlice::with_bitmap(buf.as_mut_ptr(), buf.len(), bitmap3.slice_at(0)) };
2105+
let bitmap3 = AtomicBitmap::new(len, page_size);
2106+
let slice3 = unsafe { VolatileSlice::with_bitmap(buf, len, bitmap3.slice_at(0)) };
21102107

21112108
assert!(range_is_clean(slice.bitmap(), 0, slice.len()));
21122109
assert!(range_is_clean(slice2.bitmap(), 0, slice2.len()));
@@ -2151,6 +2148,10 @@ mod tests {
21512148
slice3.copy_from(&buf);
21522149
assert!(range_is_dirty(slice3.bitmap(), 0, dirty_offset));
21532150
}
2151+
2152+
unsafe {
2153+
std::alloc::dealloc(buf, Layout::from_size_align(len, 8).unwrap());
2154+
}
21542155
}
21552156

21562157
#[test]

0 commit comments

Comments
 (0)