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(heap_api, allocator_api)]
18 use std::heap::{Heap, Alloc, Layout};
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 = Heap.alloc(layout.clone()).unwrap_or_else(|e| Heap.oom(e));
56 println!("allocate({:?}) = {:?}", layout, ret);
62 unsafe fn deallocate(ptr: *mut u8, layout: Layout) {
64 println!("deallocate({:?}, {:?}", ptr, layout);
67 Heap.dealloc(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 = Heap.realloc(ptr, old.clone(), new.clone())
76 .unwrap_or_else(|e| Heap.oom(e));
79 println!("reallocate({:?}, old={:?}, new={:?}) = {:?}",
85 fn idx_to_size(i: usize) -> usize { (i+1) * 10 }
87 // Allocate pairs of rows that form a triangle shape. (Hope is
88 // that at least two rows will be allocated near each other, so
89 // that we trigger the bug (a buffer overrun) in an observable
91 for i in 0..COUNT / 2 {
92 let size = idx_to_size(i);
93 ascend[2*i] = allocate(Layout::from_size_align(size, ALIGN).unwrap());
94 ascend[2*i+1] = allocate(Layout::from_size_align(size, ALIGN).unwrap());
97 // Initialize each pair of rows to distinct value.
98 for i in 0..COUNT / 2 {
99 let (p0, p1, size) = (ascend[2*i], ascend[2*i+1], idx_to_size(i));
101 *p0.offset(j as isize) = i as u8;
102 *p1.offset(j as isize) = i as u8;
106 sanity_check(&*ascend);
107 test_1(ascend); // triangle -> square
108 test_2(ascend); // square -> triangle
109 test_3(ascend); // triangle -> square
110 test_4(ascend); // square -> triangle
112 for i in 0..COUNT / 2 {
113 let size = idx_to_size(i);
114 deallocate(ascend[2*i], Layout::from_size_align(size, ALIGN).unwrap());
115 deallocate(ascend[2*i+1], Layout::from_size_align(size, ALIGN).unwrap());
120 // Test 1: turn the triangle into a square (in terms of
121 // allocation; initialized portion remains a triangle) by
122 // realloc'ing each row from top to bottom, and checking all the
124 unsafe fn test_1(ascend: &mut [*mut u8]) {
125 let new_size = idx_to_size(COUNT-1);
126 let new = Layout::from_size_align(new_size, ALIGN).unwrap();
127 for i in 0..COUNT / 2 {
128 let (p0, p1, old_size) = (ascend[2*i], ascend[2*i+1], idx_to_size(i));
129 assert!(old_size < new_size);
130 let old = Layout::from_size_align(old_size, ALIGN).unwrap();
132 ascend[2*i] = reallocate(p0, old.clone(), new.clone());
133 sanity_check(&*ascend);
135 ascend[2*i+1] = reallocate(p1, old.clone(), new.clone());
136 sanity_check(&*ascend);
140 // Test 2: turn the square back into a triangle, top to bottom.
141 unsafe fn test_2(ascend: &mut [*mut u8]) {
142 let old_size = idx_to_size(COUNT-1);
143 let old = Layout::from_size_align(old_size, ALIGN).unwrap();
144 for i in 0..COUNT / 2 {
145 let (p0, p1, new_size) = (ascend[2*i], ascend[2*i+1], idx_to_size(i));
146 assert!(new_size < old_size);
147 let new = Layout::from_size_align(new_size, ALIGN).unwrap();
149 ascend[2*i] = reallocate(p0, old.clone(), new.clone());
150 sanity_check(&*ascend);
152 ascend[2*i+1] = reallocate(p1, old.clone(), new.clone());
153 sanity_check(&*ascend);
157 // Test 3: turn triangle into a square, bottom to top.
158 unsafe fn test_3(ascend: &mut [*mut u8]) {
159 let new_size = idx_to_size(COUNT-1);
160 let new = Layout::from_size_align(new_size, ALIGN).unwrap();
161 for i in (0..COUNT / 2).rev() {
162 let (p0, p1, old_size) = (ascend[2*i], ascend[2*i+1], idx_to_size(i));
163 assert!(old_size < new_size);
164 let old = Layout::from_size_align(old_size, ALIGN).unwrap();
166 ascend[2*i+1] = reallocate(p1, old.clone(), new.clone());
167 sanity_check(&*ascend);
169 ascend[2*i] = reallocate(p0, old.clone(), new.clone());
170 sanity_check(&*ascend);
174 // Test 4: turn the square back into a triangle, bottom to top.
175 unsafe fn test_4(ascend: &mut [*mut u8]) {
176 let old_size = idx_to_size(COUNT-1);
177 let old = Layout::from_size_align(old_size, ALIGN).unwrap();
178 for i in (0..COUNT / 2).rev() {
179 let (p0, p1, new_size) = (ascend[2*i], ascend[2*i+1], idx_to_size(i));
180 assert!(new_size < old_size);
181 let new = Layout::from_size_align(new_size, ALIGN).unwrap();
183 ascend[2*i+1] = reallocate(p1, old.clone(), new.clone());
184 sanity_check(&*ascend);
186 ascend[2*i] = reallocate(p0, old.clone(), new.clone());
187 sanity_check(&*ascend);