1 // alloc::heap::reallocate test.
3 // Ideally this would be revised to use no_std, but for now it serves
4 // well enough to reproduce (and illustrate) the bug from #16687.
6 #![feature(allocator_api)]
8 use std::alloc::{Global, Alloc, Layout, handle_alloc_error};
9 use std::ptr::{self, NonNull};
13 assert!(test_triangle());
17 unsafe fn test_triangle() -> bool {
18 static COUNT : usize = 16;
19 let mut ascend = vec![ptr::null_mut(); COUNT];
20 let ascend = &mut *ascend;
21 static ALIGN : usize = 1;
23 // Checks that `ascend` forms triangle of ascending size formed
24 // from pairs of rows (where each pair of rows is equally sized),
25 // and the elements of the triangle match their row-pair index.
26 unsafe fn sanity_check(ascend: &[*mut u8]) {
27 for i in 0..COUNT / 2 {
28 let (p0, p1, size) = (ascend[2*i], ascend[2*i+1], idx_to_size(i));
30 assert_eq!(*p0.add(j), i as u8);
31 assert_eq!(*p1.add(j), i as u8);
36 static PRINT : bool = false;
38 unsafe fn allocate(layout: Layout) -> *mut u8 {
40 println!("allocate({:?})", layout);
43 let ret = Global.alloc(layout).unwrap_or_else(|_| handle_alloc_error(layout));
46 println!("allocate({:?}) = {:?}", layout, ret);
52 unsafe fn deallocate(ptr: *mut u8, layout: Layout) {
54 println!("deallocate({:?}, {:?}", ptr, layout);
57 Global.dealloc(NonNull::new_unchecked(ptr), layout);
60 unsafe fn reallocate(ptr: *mut u8, old: Layout, new: Layout) -> *mut u8 {
62 println!("reallocate({:?}, old={:?}, new={:?})", ptr, old, new);
65 let ret = Global.realloc(NonNull::new_unchecked(ptr), old, new.size())
66 .unwrap_or_else(|_| handle_alloc_error(
67 Layout::from_size_align_unchecked(new.size(), old.align())
71 println!("reallocate({:?}, old={:?}, new={:?}) = {:?}",
77 fn idx_to_size(i: usize) -> usize { (i+1) * 10 }
79 // Allocate pairs of rows that form a triangle shape. (Hope is
80 // that at least two rows will be allocated near each other, so
81 // that we trigger the bug (a buffer overrun) in an observable
83 for i in 0..COUNT / 2 {
84 let size = idx_to_size(i);
85 ascend[2*i] = allocate(Layout::from_size_align(size, ALIGN).unwrap());
86 ascend[2*i+1] = allocate(Layout::from_size_align(size, ALIGN).unwrap());
89 // Initialize each pair of rows to distinct value.
90 for i in 0..COUNT / 2 {
91 let (p0, p1, size) = (ascend[2*i], ascend[2*i+1], idx_to_size(i));
98 sanity_check(&*ascend);
99 test_1(ascend); // triangle -> square
100 test_2(ascend); // square -> triangle
101 test_3(ascend); // triangle -> square
102 test_4(ascend); // square -> triangle
104 for i in 0..COUNT / 2 {
105 let size = idx_to_size(i);
106 deallocate(ascend[2*i], Layout::from_size_align(size, ALIGN).unwrap());
107 deallocate(ascend[2*i+1], Layout::from_size_align(size, ALIGN).unwrap());
112 // Test 1: turn the triangle into a square (in terms of
113 // allocation; initialized portion remains a triangle) by
114 // realloc'ing each row from top to bottom, and checking all the
116 unsafe fn test_1(ascend: &mut [*mut u8]) {
117 let new_size = idx_to_size(COUNT-1);
118 let new = Layout::from_size_align(new_size, ALIGN).unwrap();
119 for i in 0..COUNT / 2 {
120 let (p0, p1, old_size) = (ascend[2*i], ascend[2*i+1], idx_to_size(i));
121 assert!(old_size < new_size);
122 let old = Layout::from_size_align(old_size, ALIGN).unwrap();
124 ascend[2*i] = reallocate(p0, old.clone(), new.clone());
125 sanity_check(&*ascend);
127 ascend[2*i+1] = reallocate(p1, old.clone(), new.clone());
128 sanity_check(&*ascend);
132 // Test 2: turn the square back into a triangle, top to bottom.
133 unsafe fn test_2(ascend: &mut [*mut u8]) {
134 let old_size = idx_to_size(COUNT-1);
135 let old = Layout::from_size_align(old_size, ALIGN).unwrap();
136 for i in 0..COUNT / 2 {
137 let (p0, p1, new_size) = (ascend[2*i], ascend[2*i+1], idx_to_size(i));
138 assert!(new_size < old_size);
139 let new = Layout::from_size_align(new_size, ALIGN).unwrap();
141 ascend[2*i] = reallocate(p0, old.clone(), new.clone());
142 sanity_check(&*ascend);
144 ascend[2*i+1] = reallocate(p1, old.clone(), new.clone());
145 sanity_check(&*ascend);
149 // Test 3: turn triangle into a square, bottom to top.
150 unsafe fn test_3(ascend: &mut [*mut u8]) {
151 let new_size = idx_to_size(COUNT-1);
152 let new = Layout::from_size_align(new_size, ALIGN).unwrap();
153 for i in (0..COUNT / 2).rev() {
154 let (p0, p1, old_size) = (ascend[2*i], ascend[2*i+1], idx_to_size(i));
155 assert!(old_size < new_size);
156 let old = Layout::from_size_align(old_size, ALIGN).unwrap();
158 ascend[2*i+1] = reallocate(p1, old.clone(), new.clone());
159 sanity_check(&*ascend);
161 ascend[2*i] = reallocate(p0, old.clone(), new.clone());
162 sanity_check(&*ascend);
166 // Test 4: turn the square back into a triangle, bottom to top.
167 unsafe fn test_4(ascend: &mut [*mut u8]) {
168 let old_size = idx_to_size(COUNT-1);
169 let old = Layout::from_size_align(old_size, ALIGN).unwrap();
170 for i in (0..COUNT / 2).rev() {
171 let (p0, p1, new_size) = (ascend[2*i], ascend[2*i+1], idx_to_size(i));
172 assert!(new_size < old_size);
173 let new = Layout::from_size_align(new_size, ALIGN).unwrap();
175 ascend[2*i+1] = reallocate(p1, old.clone(), new.clone());
176 sanity_check(&*ascend);
178 ascend[2*i] = reallocate(p0, old.clone(), new.clone());
179 sanity_check(&*ascend);