Skip to content

Commit 53c72ba

Browse files
committed
fix minimal alignment for system allocation functions
1 parent ca05b0a commit 53c72ba

File tree

2 files changed

+45
-21
lines changed

2 files changed

+45
-21
lines changed

src/shims/foreign_items.rs

+14-2
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,18 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
5151
Ok(Some(this.load_mir(instance.def)?))
5252
}
5353

54+
/// Returns the minimum alignment for the target architecture.
55+
fn min_align(&self) -> Align {
56+
let this = self.eval_context_ref();
57+
// List taken from `libstd/sys_common/alloc.rs`.
58+
let min_align = match this.tcx.tcx.sess.target.target.arch.as_str() {
59+
"x86" | "arm" | "mips" | "powerpc" | "powerpc64" | "asmjs" | "wasm32" => 8,
60+
"x86_64" | "aarch64" | "mips64" | "s390x" | "sparc64" => 16,
61+
arch => bug!("Unsupported target architecture: {}", arch),
62+
};
63+
Align::from_bytes(min_align).unwrap()
64+
}
65+
5466
fn malloc(
5567
&mut self,
5668
size: u64,
@@ -61,7 +73,7 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
6173
if size == 0 {
6274
Scalar::from_int(0, this.pointer_size())
6375
} else {
64-
let align = this.tcx.data_layout.pointer_align.abi;
76+
let align = this.min_align();
6577
let ptr = this.memory_mut().allocate(Size::from_bytes(size), align, MiriMemoryKind::C.into());
6678
if zero_init {
6779
// We just allocated this, the access cannot fail
@@ -94,7 +106,7 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
94106
new_size: u64,
95107
) -> InterpResult<'tcx, Scalar<Tag>> {
96108
let this = self.eval_context_mut();
97-
let align = this.tcx.data_layout.pointer_align.abi;
109+
let align = this.min_align();
98110
if old_ptr.is_null_ptr(this) {
99111
if new_size == 0 {
100112
Ok(Scalar::from_int(0, this.pointer_size()))

tests/run-pass/heap_allocator.rs

+31-19
Original file line numberDiff line numberDiff line change
@@ -6,35 +6,47 @@ use std::alloc::{Global, Alloc, Layout, System};
66
use std::slice;
77

88
fn check_alloc<T: Alloc>(mut allocator: T) { unsafe {
9-
let layout = Layout::from_size_align(20, 4).unwrap();
10-
let a = allocator.alloc(layout).unwrap();
11-
allocator.dealloc(a, layout);
9+
for &align in &[4, 8, 16, 32] {
10+
let layout = Layout::from_size_align(20, align).unwrap();
1211

13-
let p1 = allocator.alloc_zeroed(layout).unwrap();
12+
for _ in 0..32 {
13+
let a = allocator.alloc(layout).unwrap();
14+
assert_eq!(a.as_ptr() as usize % align, 0, "pointer is incorrectly aligned");
15+
allocator.dealloc(a, layout);
16+
}
17+
18+
let p1 = allocator.alloc_zeroed(layout).unwrap();
19+
assert_eq!(p1.as_ptr() as usize % align, 0, "pointer is incorrectly aligned");
1420

15-
let p2 = allocator.realloc(p1, Layout::from_size_align(20, 4).unwrap(), 40).unwrap();
16-
let slice = slice::from_raw_parts(p2.as_ptr(), 20);
17-
assert_eq!(&slice, &[0_u8; 20]);
21+
let p2 = allocator.realloc(p1, layout, 40).unwrap();
22+
let layout = Layout::from_size_align(40, align).unwrap();
23+
assert_eq!(p2.as_ptr() as usize % align, 0, "pointer is incorrectly aligned");
24+
let slice = slice::from_raw_parts(p2.as_ptr(), 20);
25+
assert_eq!(&slice, &[0_u8; 20]);
1826

19-
// old size == new size
20-
let p3 = allocator.realloc(p2, Layout::from_size_align(40, 4).unwrap(), 40).unwrap();
21-
let slice = slice::from_raw_parts(p3.as_ptr(), 20);
22-
assert_eq!(&slice, &[0_u8; 20]);
27+
// old size == new size
28+
let p3 = allocator.realloc(p2, layout, 40).unwrap();
29+
assert_eq!(p3.as_ptr() as usize % align, 0, "pointer is incorrectly aligned");
30+
let slice = slice::from_raw_parts(p3.as_ptr(), 20);
31+
assert_eq!(&slice, &[0_u8; 20]);
2332

24-
// old size > new size
25-
let p4 = allocator.realloc(p3, Layout::from_size_align(40, 4).unwrap(), 10).unwrap();
26-
let slice = slice::from_raw_parts(p4.as_ptr(), 10);
27-
assert_eq!(&slice, &[0_u8; 10]);
33+
// old size > new size
34+
let p4 = allocator.realloc(p3, layout, 10).unwrap();
35+
let layout = Layout::from_size_align(10, align).unwrap();
36+
assert_eq!(p4.as_ptr() as usize % align, 0, "pointer is incorrectly aligned");
37+
let slice = slice::from_raw_parts(p4.as_ptr(), 10);
38+
assert_eq!(&slice, &[0_u8; 10]);
2839

29-
allocator.dealloc(p4, Layout::from_size_align(10, 4).unwrap());
40+
allocator.dealloc(p4, layout);
41+
}
3042
} }
3143

3244
fn check_overalign_requests<T: Alloc>(mut allocator: T) {
3345
let size = 8;
34-
// Greater than `size`.
35-
let align = 16;
46+
// Greater than `size`, and also greater than `MIN_ALIGN`.
47+
let align = 32;
3648

37-
let iterations = 5;
49+
let iterations = 32;
3850
unsafe {
3951
let pointers: Vec<_> = (0..iterations).map(|_| {
4052
allocator.alloc(Layout::from_size_align(size, align).unwrap()).unwrap()

0 commit comments

Comments
 (0)