Skip to content

Commit 21d8992

Browse files
committed
alloc_system: don’t assume MIN_ALIGN for small sizes, fix #45955
The GNU C library (glibc) is documented to always allocate with an alignment of at least 8 or 16 bytes, on 32-bit or 64-bit platforms: https://www.gnu.org/software/libc/manual/html_node/Aligned-Memory-Blocks.html This matches our use of `MIN_ALIGN` before this commit. However, even when libc is glibc, the program might be linked with another allocator that redefines the `malloc` symbol and friends. (The `alloc_jemalloc` crate does, in some cases.) So `alloc_system` doesn’t know which allocator it calls, and needs to be conservative in assumptions it makes. The C standard says: https://port70.net/%7Ensz/c/c11/n1570.html#7.22.3 > The pointer returned if the allocation succeeds is suitably aligned > so that it may be assigned to a pointer to any type of object > with a fundamental alignment requirement https://port70.net/~nsz/c/c11/n1570.html#6.2.8p2 > A fundamental alignment is represented by an alignment less than > or equal to the greatest alignment supported by the implementation > in all contexts, which is equal to `_Alignof (max_align_t)`. `_Alignof (max_align_t)` depends on the ABI and doesn’t seem to have a clear definition, but it seems to match our `MIN_ALIGN` in practice. However, the size of objects is rounded up to the next multiple of their alignment (since that size is also the stride used in arrays). Conversely, the alignment of a non-zero-size object is at most its size. So for example it seems ot be legal for `malloc(8)` to return a pointer that’s only 8-bytes-aligned, even if `_Alignof (max_align_t)` is 16.
1 parent 41e03c3 commit 21d8992

File tree

3 files changed

+47
-3
lines changed

3 files changed

+47
-3
lines changed

src/liballoc/tests/heap.rs

+40
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
// Copyright 2017 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
use alloc_system::System;
12+
use std::heap::{Heap, Alloc, Layout};
13+
14+
/// https://github.com/rust-lang/rust/issues/45955
15+
///
16+
/// Note that `#[global_allocator]` is not used,
17+
/// so `liballoc_jemalloc` is linked (on some platforms).
18+
#[test]
19+
fn alloc_system_overaligned_request() {
20+
check_overalign_requests(System)
21+
}
22+
23+
fn check_overalign_requests<T: Alloc>(mut allocator: T) {
24+
let size = 8;
25+
let align = 16; // greater than size
26+
let iterations = 100;
27+
unsafe {
28+
let pointers: Vec<_> = (0..iterations).map(|_| {
29+
allocator.alloc(Layout::from_size_align(size, align).unwrap()).unwrap()
30+
}).collect();
31+
for &ptr in &pointers {
32+
assert_eq!((ptr as usize) % align, 0, "Got a pointer less aligned than requested")
33+
}
34+
35+
// Clean up
36+
for &ptr in &pointers {
37+
allocator.dealloc(ptr, Layout::from_size_align(size, align).unwrap())
38+
}
39+
}
40+
}

src/liballoc/tests/lib.rs

+4
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010

1111
#![deny(warnings)]
1212

13+
#![feature(allocator_api)]
14+
#![feature(alloc_system)]
1315
#![feature(attr_literals)]
1416
#![feature(box_syntax)]
1517
#![feature(inclusive_range_syntax)]
@@ -29,6 +31,7 @@
2931
#![feature(unboxed_closures)]
3032
#![feature(unicode)]
3133

34+
extern crate alloc_system;
3235
extern crate std_unicode;
3336
extern crate rand;
3437

@@ -39,6 +42,7 @@ mod binary_heap;
3942
mod btree;
4043
mod cow_str;
4144
mod fmt;
45+
mod heap;
4246
mod linked_list;
4347
mod slice;
4448
mod str;

src/liballoc_system/lib.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@ mod platform {
132132
unsafe impl<'a> Alloc for &'a System {
133133
#[inline]
134134
unsafe fn alloc(&mut self, layout: Layout) -> Result<*mut u8, AllocErr> {
135-
let ptr = if layout.align() <= MIN_ALIGN {
135+
let ptr = if layout.align() <= MIN_ALIGN && layout.align() <= layout.size() {
136136
libc::malloc(layout.size()) as *mut u8
137137
} else {
138138
aligned_malloc(&layout)
@@ -148,7 +148,7 @@ mod platform {
148148
unsafe fn alloc_zeroed(&mut self, layout: Layout)
149149
-> Result<*mut u8, AllocErr>
150150
{
151-
if layout.align() <= MIN_ALIGN {
151+
if layout.align() <= MIN_ALIGN && layout.align() <= layout.size() {
152152
let ptr = libc::calloc(layout.size(), 1) as *mut u8;
153153
if !ptr.is_null() {
154154
Ok(ptr)
@@ -180,7 +180,7 @@ mod platform {
180180
})
181181
}
182182

183-
if new_layout.align() <= MIN_ALIGN {
183+
if new_layout.align() <= MIN_ALIGN && new_layout.align() <= new_layout.size(){
184184
let ptr = libc::realloc(ptr as *mut libc::c_void, new_layout.size());
185185
if !ptr.is_null() {
186186
Ok(ptr as *mut u8)

0 commit comments

Comments
 (0)