2 // alloc::heap::reallocate test.
4 // Ideally this would be revised to use no_std, but for now it serves
5 // well enough to reproduce (and illustrate) the bug from #16687.
7 #![feature(allocator_api)]
9 use std::alloc::{Global, Alloc, Layout, handle_alloc_error};
10 use std::ptr::{self, NonNull};
14 assert!(test_triangle());
18 unsafe fn test_triangle() -> bool {
19 static COUNT : usize = 16;
20 let mut ascend = vec![ptr::null_mut(); COUNT];
21 let ascend = &mut *ascend;
22 static ALIGN : usize = 1;
24 // Checks that `ascend` forms triangle of ascending size formed
25 // from pairs of rows (where each pair of rows is equally sized),
26 // and the elements of the triangle match their row-pair index.
27 unsafe fn sanity_check(ascend: &[*mut u8]) {
28 for i in 0..COUNT / 2 {
29 let (p0, p1, size) = (ascend[2*i], ascend[2*i+1], idx_to_size(i));
31 assert_eq!(*p0.add(j), i as u8);
32 assert_eq!(*p1.add(j), i as u8);
37 static PRINT : bool = false;
39 unsafe fn allocate(layout: Layout) -> *mut u8 {
41 println!("allocate({:?})", layout);
44 let ret = Global.alloc(layout).unwrap_or_else(|_| handle_alloc_error(layout));
47 println!("allocate({:?}) = {:?}", layout, ret);
53 unsafe fn deallocate(ptr: *mut u8, layout: Layout) {
55 println!("deallocate({:?}, {:?}", ptr, layout);
58 Global.dealloc(NonNull::new_unchecked(ptr), layout);
61 unsafe fn reallocate(ptr: *mut u8, old: Layout, new: Layout) -> *mut u8 {
63 println!("reallocate({:?}, old={:?}, new={:?})", ptr, old, new);
66 let ret = Global.realloc(NonNull::new_unchecked(ptr), old, new.size())
67 .unwrap_or_else(|_| handle_alloc_error(
68 Layout::from_size_align_unchecked(new.size(), old.align())
72 println!("reallocate({:?}, old={:?}, new={:?}) = {:?}",
78 fn idx_to_size(i: usize) -> usize { (i+1) * 10 }
80 // Allocate pairs of rows that form a triangle shape. (Hope is
81 // that at least two rows will be allocated near each other, so
82 // that we trigger the bug (a buffer overrun) in an observable
84 for i in 0..COUNT / 2 {
85 let size = idx_to_size(i);
86 ascend[2*i] = allocate(Layout::from_size_align(size, ALIGN).unwrap());
87 ascend[2*i+1] = allocate(Layout::from_size_align(size, ALIGN).unwrap());
90 // Initialize each pair of rows to distinct value.
91 for i in 0..COUNT / 2 {
92 let (p0, p1, size) = (ascend[2*i], ascend[2*i+1], idx_to_size(i));
99 sanity_check(&*ascend);
100 test_1(ascend); // triangle -> square
101 test_2(ascend); // square -> triangle
102 test_3(ascend); // triangle -> square
103 test_4(ascend); // square -> triangle
105 for i in 0..COUNT / 2 {
106 let size = idx_to_size(i);
107 deallocate(ascend[2*i], Layout::from_size_align(size, ALIGN).unwrap());
108 deallocate(ascend[2*i+1], Layout::from_size_align(size, ALIGN).unwrap());
113 // Test 1: turn the triangle into a square (in terms of
114 // allocation; initialized portion remains a triangle) by
115 // realloc'ing each row from top to bottom, and checking all the
117 unsafe fn test_1(ascend: &mut [*mut u8]) {
118 let new_size = idx_to_size(COUNT-1);
119 let new = Layout::from_size_align(new_size, ALIGN).unwrap();
120 for i in 0..COUNT / 2 {
121 let (p0, p1, old_size) = (ascend[2*i], ascend[2*i+1], idx_to_size(i));
122 assert!(old_size < new_size);
123 let old = Layout::from_size_align(old_size, ALIGN).unwrap();
125 ascend[2*i] = reallocate(p0, old.clone(), new.clone());
126 sanity_check(&*ascend);
128 ascend[2*i+1] = reallocate(p1, old.clone(), new.clone());
129 sanity_check(&*ascend);
133 // Test 2: turn the square back into a triangle, top to bottom.
134 unsafe fn test_2(ascend: &mut [*mut u8]) {
135 let old_size = idx_to_size(COUNT-1);
136 let old = Layout::from_size_align(old_size, ALIGN).unwrap();
137 for i in 0..COUNT / 2 {
138 let (p0, p1, new_size) = (ascend[2*i], ascend[2*i+1], idx_to_size(i));
139 assert!(new_size < old_size);
140 let new = Layout::from_size_align(new_size, ALIGN).unwrap();
142 ascend[2*i] = reallocate(p0, old.clone(), new.clone());
143 sanity_check(&*ascend);
145 ascend[2*i+1] = reallocate(p1, old.clone(), new.clone());
146 sanity_check(&*ascend);
150 // Test 3: turn triangle into a square, bottom to top.
151 unsafe fn test_3(ascend: &mut [*mut u8]) {
152 let new_size = idx_to_size(COUNT-1);
153 let new = Layout::from_size_align(new_size, ALIGN).unwrap();
154 for i in (0..COUNT / 2).rev() {
155 let (p0, p1, old_size) = (ascend[2*i], ascend[2*i+1], idx_to_size(i));
156 assert!(old_size < new_size);
157 let old = Layout::from_size_align(old_size, ALIGN).unwrap();
159 ascend[2*i+1] = reallocate(p1, old.clone(), new.clone());
160 sanity_check(&*ascend);
162 ascend[2*i] = reallocate(p0, old.clone(), new.clone());
163 sanity_check(&*ascend);
167 // Test 4: turn the square back into a triangle, bottom to top.
168 unsafe fn test_4(ascend: &mut [*mut u8]) {
169 let old_size = idx_to_size(COUNT-1);
170 let old = Layout::from_size_align(old_size, ALIGN).unwrap();
171 for i in (0..COUNT / 2).rev() {
172 let (p0, p1, new_size) = (ascend[2*i], ascend[2*i+1], idx_to_size(i));
173 assert!(new_size < old_size);
174 let new = Layout::from_size_align(new_size, ALIGN).unwrap();
176 ascend[2*i+1] = reallocate(p1, old.clone(), new.clone());
177 sanity_check(&*ascend);
179 ascend[2*i] = reallocate(p0, old.clone(), new.clone());
180 sanity_check(&*ascend);