2019-07-26 16:54:25 -05:00
|
|
|
// run-pass
|
2014-08-23 12:22:36 -05:00
|
|
|
// alloc::heap::reallocate test.
|
|
|
|
//
|
|
|
|
// Ideally this would be revised to use no_std, but for now it serves
|
|
|
|
// well enough to reproduce (and illustrate) the bug from #16687.
|
|
|
|
|
2018-07-22 21:14:42 -05:00
|
|
|
#![feature(allocator_api)]
|
2020-08-04 11:03:34 -05:00
|
|
|
#![feature(slice_ptr_get)]
|
2015-03-05 20:33:58 -06:00
|
|
|
|
2020-12-04 07:47:15 -06:00
|
|
|
use std::alloc::{handle_alloc_error, Allocator, Global, Layout};
|
2018-04-02 18:51:02 -05:00
|
|
|
use std::ptr::{self, NonNull};
|
2014-08-23 12:22:36 -05:00
|
|
|
|
|
|
|
fn main() {
|
|
|
|
unsafe {
|
|
|
|
assert!(test_triangle());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
unsafe fn test_triangle() -> bool {
|
2020-03-24 05:45:38 -05:00
|
|
|
static COUNT: usize = 16;
|
2015-07-08 15:52:55 -05:00
|
|
|
let mut ascend = vec![ptr::null_mut(); COUNT];
|
2015-02-01 20:53:25 -06:00
|
|
|
let ascend = &mut *ascend;
|
2020-03-24 05:45:38 -05:00
|
|
|
static ALIGN: usize = 1;
|
2014-08-23 12:22:36 -05:00
|
|
|
|
2014-10-25 22:10:16 -05:00
|
|
|
// Checks that `ascend` forms triangle of ascending size formed
|
2014-08-23 12:22:36 -05:00
|
|
|
// from pairs of rows (where each pair of rows is equally sized),
|
|
|
|
// and the elements of the triangle match their row-pair index.
|
|
|
|
unsafe fn sanity_check(ascend: &[*mut u8]) {
|
2015-03-03 02:42:26 -06:00
|
|
|
for i in 0..COUNT / 2 {
|
2020-03-24 05:45:38 -05:00
|
|
|
let (p0, p1, size) = (ascend[2 * i], ascend[2 * i + 1], idx_to_size(i));
|
2015-03-03 02:42:26 -06:00
|
|
|
for j in 0..size {
|
2018-08-19 21:16:22 -05:00
|
|
|
assert_eq!(*p0.add(j), i as u8);
|
|
|
|
assert_eq!(*p1.add(j), i as u8);
|
2014-08-23 12:22:36 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-24 05:45:38 -05:00
|
|
|
static PRINT: bool = false;
|
2014-08-23 12:22:36 -05:00
|
|
|
|
2017-06-03 16:54:08 -05:00
|
|
|
unsafe fn allocate(layout: Layout) -> *mut u8 {
|
|
|
|
if PRINT {
|
|
|
|
println!("allocate({:?})", layout);
|
|
|
|
}
|
2014-08-23 12:22:36 -05:00
|
|
|
|
2020-12-04 07:47:15 -06:00
|
|
|
let ptr = Global.allocate(layout).unwrap_or_else(|_| handle_alloc_error(layout));
|
2014-08-23 12:22:36 -05:00
|
|
|
|
2017-06-03 16:54:08 -05:00
|
|
|
if PRINT {
|
2020-08-04 11:03:34 -05:00
|
|
|
println!("allocate({:?}) = {:?}", layout, ptr);
|
2014-08-23 12:22:36 -05:00
|
|
|
}
|
|
|
|
|
2020-08-18 15:39:33 -05:00
|
|
|
ptr.as_mut_ptr()
|
2014-08-23 12:22:36 -05:00
|
|
|
}
|
2017-06-03 16:54:08 -05:00
|
|
|
|
|
|
|
unsafe fn deallocate(ptr: *mut u8, layout: Layout) {
|
|
|
|
if PRINT {
|
|
|
|
println!("deallocate({:?}, {:?}", ptr, layout);
|
2014-09-23 06:52:34 -05:00
|
|
|
}
|
|
|
|
|
2020-12-04 07:47:15 -06:00
|
|
|
Global.deallocate(NonNull::new_unchecked(ptr), layout);
|
2014-09-23 06:52:34 -05:00
|
|
|
}
|
2017-06-03 16:54:08 -05:00
|
|
|
|
|
|
|
unsafe fn reallocate(ptr: *mut u8, old: Layout, new: Layout) -> *mut u8 {
|
2014-08-23 12:22:36 -05:00
|
|
|
if PRINT {
|
2017-06-03 16:54:08 -05:00
|
|
|
println!("reallocate({:?}, old={:?}, new={:?})", ptr, old, new);
|
2014-08-23 12:22:36 -05:00
|
|
|
}
|
|
|
|
|
2020-03-25 15:12:12 -05:00
|
|
|
let memory = if new.size() > old.size() {
|
2020-08-18 15:39:33 -05:00
|
|
|
Global.grow(NonNull::new_unchecked(ptr), old, new)
|
2020-03-24 05:45:38 -05:00
|
|
|
} else {
|
2020-08-18 15:39:33 -05:00
|
|
|
Global.shrink(NonNull::new_unchecked(ptr), old, new)
|
2020-03-24 05:45:38 -05:00
|
|
|
};
|
|
|
|
|
2020-08-18 15:39:33 -05:00
|
|
|
let ptr = memory.unwrap_or_else(|_| handle_alloc_error(new));
|
2014-08-23 12:22:36 -05:00
|
|
|
|
|
|
|
if PRINT {
|
2020-08-04 11:03:34 -05:00
|
|
|
println!("reallocate({:?}, old={:?}, new={:?}) = {:?}", ptr, old, new, ptr);
|
2014-08-23 12:22:36 -05:00
|
|
|
}
|
2020-08-18 15:39:33 -05:00
|
|
|
ptr.as_mut_ptr()
|
2014-08-23 12:22:36 -05:00
|
|
|
}
|
|
|
|
|
2020-03-24 05:45:38 -05:00
|
|
|
fn idx_to_size(i: usize) -> usize {
|
|
|
|
(i + 1) * 10
|
|
|
|
}
|
2014-08-23 12:22:36 -05:00
|
|
|
|
|
|
|
// Allocate pairs of rows that form a triangle shape. (Hope is
|
|
|
|
// that at least two rows will be allocated near each other, so
|
|
|
|
// that we trigger the bug (a buffer overrun) in an observable
|
|
|
|
// way.)
|
2015-03-03 02:42:26 -06:00
|
|
|
for i in 0..COUNT / 2 {
|
2014-08-23 12:22:36 -05:00
|
|
|
let size = idx_to_size(i);
|
2020-03-24 05:45:38 -05:00
|
|
|
ascend[2 * i] = allocate(Layout::from_size_align(size, ALIGN).unwrap());
|
|
|
|
ascend[2 * i + 1] = allocate(Layout::from_size_align(size, ALIGN).unwrap());
|
2014-08-23 12:22:36 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// Initialize each pair of rows to distinct value.
|
2015-03-03 02:42:26 -06:00
|
|
|
for i in 0..COUNT / 2 {
|
2020-03-24 05:45:38 -05:00
|
|
|
let (p0, p1, size) = (ascend[2 * i], ascend[2 * i + 1], idx_to_size(i));
|
2015-01-26 14:46:12 -06:00
|
|
|
for j in 0..size {
|
2018-08-19 21:16:22 -05:00
|
|
|
*p0.add(j) = i as u8;
|
|
|
|
*p1.add(j) = i as u8;
|
2014-08-23 12:22:36 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-02-01 20:53:25 -06:00
|
|
|
sanity_check(&*ascend);
|
2014-09-23 06:52:34 -05:00
|
|
|
test_1(ascend); // triangle -> square
|
|
|
|
test_2(ascend); // square -> triangle
|
|
|
|
test_3(ascend); // triangle -> square
|
|
|
|
test_4(ascend); // square -> triangle
|
|
|
|
|
2015-03-03 02:42:26 -06:00
|
|
|
for i in 0..COUNT / 2 {
|
2014-09-23 06:52:34 -05:00
|
|
|
let size = idx_to_size(i);
|
2020-03-24 05:45:38 -05:00
|
|
|
deallocate(ascend[2 * i], Layout::from_size_align(size, ALIGN).unwrap());
|
|
|
|
deallocate(ascend[2 * i + 1], Layout::from_size_align(size, ALIGN).unwrap());
|
2014-09-23 06:52:34 -05:00
|
|
|
}
|
2014-08-23 12:22:36 -05:00
|
|
|
|
|
|
|
return true;
|
|
|
|
|
|
|
|
// Test 1: turn the triangle into a square (in terms of
|
|
|
|
// allocation; initialized portion remains a triangle) by
|
|
|
|
// realloc'ing each row from top to bottom, and checking all the
|
|
|
|
// rows as we go.
|
|
|
|
unsafe fn test_1(ascend: &mut [*mut u8]) {
|
2020-03-24 05:45:38 -05:00
|
|
|
let new_size = idx_to_size(COUNT - 1);
|
2017-06-03 16:54:08 -05:00
|
|
|
let new = Layout::from_size_align(new_size, ALIGN).unwrap();
|
2015-03-03 02:42:26 -06:00
|
|
|
for i in 0..COUNT / 2 {
|
2020-03-24 05:45:38 -05:00
|
|
|
let (p0, p1, old_size) = (ascend[2 * i], ascend[2 * i + 1], idx_to_size(i));
|
2014-08-23 12:22:36 -05:00
|
|
|
assert!(old_size < new_size);
|
2017-06-03 16:54:08 -05:00
|
|
|
let old = Layout::from_size_align(old_size, ALIGN).unwrap();
|
2014-08-23 12:22:36 -05:00
|
|
|
|
2020-03-24 05:45:38 -05:00
|
|
|
ascend[2 * i] = reallocate(p0, old.clone(), new.clone());
|
2015-02-01 20:53:25 -06:00
|
|
|
sanity_check(&*ascend);
|
2014-08-23 12:22:36 -05:00
|
|
|
|
2020-03-24 05:45:38 -05:00
|
|
|
ascend[2 * i + 1] = reallocate(p1, old.clone(), new.clone());
|
2015-02-01 20:53:25 -06:00
|
|
|
sanity_check(&*ascend);
|
2014-08-23 12:22:36 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Test 2: turn the square back into a triangle, top to bottom.
|
|
|
|
unsafe fn test_2(ascend: &mut [*mut u8]) {
|
2020-03-24 05:45:38 -05:00
|
|
|
let old_size = idx_to_size(COUNT - 1);
|
2017-06-03 16:54:08 -05:00
|
|
|
let old = Layout::from_size_align(old_size, ALIGN).unwrap();
|
2015-03-03 02:42:26 -06:00
|
|
|
for i in 0..COUNT / 2 {
|
2020-03-24 05:45:38 -05:00
|
|
|
let (p0, p1, new_size) = (ascend[2 * i], ascend[2 * i + 1], idx_to_size(i));
|
2014-08-23 12:22:36 -05:00
|
|
|
assert!(new_size < old_size);
|
2017-06-03 16:54:08 -05:00
|
|
|
let new = Layout::from_size_align(new_size, ALIGN).unwrap();
|
2014-08-23 12:22:36 -05:00
|
|
|
|
2020-03-24 05:45:38 -05:00
|
|
|
ascend[2 * i] = reallocate(p0, old.clone(), new.clone());
|
2015-02-01 20:53:25 -06:00
|
|
|
sanity_check(&*ascend);
|
2014-08-23 12:22:36 -05:00
|
|
|
|
2020-03-24 05:45:38 -05:00
|
|
|
ascend[2 * i + 1] = reallocate(p1, old.clone(), new.clone());
|
2015-02-01 20:53:25 -06:00
|
|
|
sanity_check(&*ascend);
|
2014-08-23 12:22:36 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Test 3: turn triangle into a square, bottom to top.
|
|
|
|
unsafe fn test_3(ascend: &mut [*mut u8]) {
|
2020-03-24 05:45:38 -05:00
|
|
|
let new_size = idx_to_size(COUNT - 1);
|
2017-06-03 16:54:08 -05:00
|
|
|
let new = Layout::from_size_align(new_size, ALIGN).unwrap();
|
2015-03-03 02:42:26 -06:00
|
|
|
for i in (0..COUNT / 2).rev() {
|
2020-03-24 05:45:38 -05:00
|
|
|
let (p0, p1, old_size) = (ascend[2 * i], ascend[2 * i + 1], idx_to_size(i));
|
2014-08-23 12:22:36 -05:00
|
|
|
assert!(old_size < new_size);
|
2017-06-03 16:54:08 -05:00
|
|
|
let old = Layout::from_size_align(old_size, ALIGN).unwrap();
|
2014-08-23 12:22:36 -05:00
|
|
|
|
2020-03-24 05:45:38 -05:00
|
|
|
ascend[2 * i + 1] = reallocate(p1, old.clone(), new.clone());
|
2015-02-01 20:53:25 -06:00
|
|
|
sanity_check(&*ascend);
|
2014-08-23 12:22:36 -05:00
|
|
|
|
2020-03-24 05:45:38 -05:00
|
|
|
ascend[2 * i] = reallocate(p0, old.clone(), new.clone());
|
2015-02-01 20:53:25 -06:00
|
|
|
sanity_check(&*ascend);
|
2014-08-23 12:22:36 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Test 4: turn the square back into a triangle, bottom to top.
|
|
|
|
unsafe fn test_4(ascend: &mut [*mut u8]) {
|
2020-03-24 05:45:38 -05:00
|
|
|
let old_size = idx_to_size(COUNT - 1);
|
2017-06-03 16:54:08 -05:00
|
|
|
let old = Layout::from_size_align(old_size, ALIGN).unwrap();
|
2015-03-03 02:42:26 -06:00
|
|
|
for i in (0..COUNT / 2).rev() {
|
2020-03-24 05:45:38 -05:00
|
|
|
let (p0, p1, new_size) = (ascend[2 * i], ascend[2 * i + 1], idx_to_size(i));
|
2014-08-23 12:22:36 -05:00
|
|
|
assert!(new_size < old_size);
|
2017-06-03 16:54:08 -05:00
|
|
|
let new = Layout::from_size_align(new_size, ALIGN).unwrap();
|
2014-08-23 12:22:36 -05:00
|
|
|
|
2020-03-24 05:45:38 -05:00
|
|
|
ascend[2 * i + 1] = reallocate(p1, old.clone(), new.clone());
|
2015-02-01 20:53:25 -06:00
|
|
|
sanity_check(&*ascend);
|
2014-08-23 12:22:36 -05:00
|
|
|
|
2020-03-24 05:45:38 -05:00
|
|
|
ascend[2 * i] = reallocate(p0, old.clone(), new.clone());
|
2015-02-01 20:53:25 -06:00
|
|
|
sanity_check(&*ascend);
|
2014-08-23 12:22:36 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|