1 // Copyright 2013-2014 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.
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.
11 // alloc::heap::reallocate test.
13 // Ideally this would be revised to use no_std, but for now it serves
14 // well enough to reproduce (and illustrate) the bug from #16687.
16 #![feature(allocator_api)]
18 use std::alloc::{Global, Alloc, Layout, handle_alloc_error};
19 use std::ptr::{self, NonNull};
23 assert!(test_triangle());
27 unsafe fn test_triangle() -> bool {
28 static COUNT : usize = 16;
29 let mut ascend = vec![ptr::null_mut(); COUNT];
30 let ascend = &mut *ascend;
31 static ALIGN : usize = 1;
33 // Checks that `ascend` forms triangle of ascending size formed
34 // from pairs of rows (where each pair of rows is equally sized),
35 // and the elements of the triangle match their row-pair index.
36 unsafe fn sanity_check(ascend: &[*mut u8]) {
37 for i in 0..COUNT / 2 {
38 let (p0, p1, size) = (ascend[2*i], ascend[2*i+1], idx_to_size(i));
40 assert_eq!(*p0.offset(j as isize), i as u8);
41 assert_eq!(*p1.offset(j as isize), i as u8);
46 static PRINT : bool = false;
48 unsafe fn allocate(layout: Layout) -> *mut u8 {
50 println!("allocate({:?})", layout);
53 let ret = Global.alloc(layout).unwrap_or_else(|_| handle_alloc_error(layout));
56 println!("allocate({:?}) = {:?}", layout, ret);
62 unsafe fn deallocate(ptr: *mut u8, layout: Layout) {
64 println!("deallocate({:?}, {:?}", ptr, layout);
67 Global.dealloc(NonNull::new_unchecked(ptr), layout);
70 unsafe fn reallocate(ptr: *mut u8, old: Layout, new: Layout) -> *mut u8 {
72 println!("reallocate({:?}, old={:?}, new={:?})", ptr, old, new);
75 let ret = Global.realloc(NonNull::new_unchecked(ptr), old, new.size())
76 .unwrap_or_else(|_| handle_alloc_error(
77 Layout::from_size_align_unchecked(new.size(), old.align())
81 println!("reallocate({:?}, old={:?}, new={:?}) = {:?}",
87 fn idx_to_size(i: usize) -> usize { (i+1) * 10 }
89 // Allocate pairs of rows that form a triangle shape. (Hope is
90 // that at least two rows will be allocated near each other, so
91 // that we trigger the bug (a buffer overrun) in an observable
93 for i in 0..COUNT / 2 {
94 let size = idx_to_size(i);
95 ascend[2*i] = allocate(Layout::from_size_align(size, ALIGN).unwrap());
96 ascend[2*i+1] = allocate(Layout::from_size_align(size, ALIGN).unwrap());
99 // Initialize each pair of rows to distinct value.
100 for i in 0..COUNT / 2 {
101 let (p0, p1, size) = (ascend[2*i], ascend[2*i+1], idx_to_size(i));
103 *p0.offset(j as isize) = i as u8;
104 *p1.offset(j as isize) = i as u8;
108 sanity_check(&*ascend);
109 test_1(ascend); // triangle -> square
110 test_2(ascend); // square -> triangle
111 test_3(ascend); // triangle -> square
112 test_4(ascend); // square -> triangle
114 for i in 0..COUNT / 2 {
115 let size = idx_to_size(i);
116 deallocate(ascend[2*i], Layout::from_size_align(size, ALIGN).unwrap());
117 deallocate(ascend[2*i+1], Layout::from_size_align(size, ALIGN).unwrap());
122 // Test 1: turn the triangle into a square (in terms of
123 // allocation; initialized portion remains a triangle) by
124 // realloc'ing each row from top to bottom, and checking all the
126 unsafe fn test_1(ascend: &mut [*mut u8]) {
127 let new_size = idx_to_size(COUNT-1);
128 let new = Layout::from_size_align(new_size, ALIGN).unwrap();
129 for i in 0..COUNT / 2 {
130 let (p0, p1, old_size) = (ascend[2*i], ascend[2*i+1], idx_to_size(i));
131 assert!(old_size < new_size);
132 let old = Layout::from_size_align(old_size, ALIGN).unwrap();
134 ascend[2*i] = reallocate(p0, old.clone(), new.clone());
135 sanity_check(&*ascend);
137 ascend[2*i+1] = reallocate(p1, old.clone(), new.clone());
138 sanity_check(&*ascend);
142 // Test 2: turn the square back into a triangle, top to bottom.
143 unsafe fn test_2(ascend: &mut [*mut u8]) {
144 let old_size = idx_to_size(COUNT-1);
145 let old = Layout::from_size_align(old_size, ALIGN).unwrap();
146 for i in 0..COUNT / 2 {
147 let (p0, p1, new_size) = (ascend[2*i], ascend[2*i+1], idx_to_size(i));
148 assert!(new_size < old_size);
149 let new = Layout::from_size_align(new_size, ALIGN).unwrap();
151 ascend[2*i] = reallocate(p0, old.clone(), new.clone());
152 sanity_check(&*ascend);
154 ascend[2*i+1] = reallocate(p1, old.clone(), new.clone());
155 sanity_check(&*ascend);
159 // Test 3: turn triangle into a square, bottom to top.
160 unsafe fn test_3(ascend: &mut [*mut u8]) {
161 let new_size = idx_to_size(COUNT-1);
162 let new = Layout::from_size_align(new_size, ALIGN).unwrap();
163 for i in (0..COUNT / 2).rev() {
164 let (p0, p1, old_size) = (ascend[2*i], ascend[2*i+1], idx_to_size(i));
165 assert!(old_size < new_size);
166 let old = Layout::from_size_align(old_size, ALIGN).unwrap();
168 ascend[2*i+1] = reallocate(p1, old.clone(), new.clone());
169 sanity_check(&*ascend);
171 ascend[2*i] = reallocate(p0, old.clone(), new.clone());
172 sanity_check(&*ascend);
176 // Test 4: turn the square back into a triangle, bottom to top.
177 unsafe fn test_4(ascend: &mut [*mut u8]) {
178 let old_size = idx_to_size(COUNT-1);
179 let old = Layout::from_size_align(old_size, ALIGN).unwrap();
180 for i in (0..COUNT / 2).rev() {
181 let (p0, p1, new_size) = (ascend[2*i], ascend[2*i+1], idx_to_size(i));
182 assert!(new_size < old_size);
183 let new = Layout::from_size_align(new_size, ALIGN).unwrap();
185 ascend[2*i+1] = reallocate(p1, old.clone(), new.clone());
186 sanity_check(&*ascend);
188 ascend[2*i] = reallocate(p0, old.clone(), new.clone());
189 sanity_check(&*ascend);