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.
23 assert!(test_triangle());
27 unsafe fn test_triangle() -> bool {
28 static COUNT : uint = 16;
29 let mut ascend = Vec::from_elem(COUNT, ptr::null_mut());
30 let ascend = ascend.as_mut_slice();
31 static ALIGN : uint = 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 range(0u, COUNT / 2) {
38 let (p0, p1, size) = (ascend[2*i], ascend[2*i+1], idx_to_size(i));
39 for j in range(0u, size) {
40 assert_eq!(*p0.offset(j as int), i as u8);
41 assert_eq!(*p1.offset(j as int), i as u8);
46 static PRINT : bool = false;
48 unsafe fn allocate(size: uint, align: uint) -> *mut u8 {
49 if PRINT { println!("allocate(size={} align={})", size, align); }
51 let ret = heap::allocate(size, align);
52 if ret.is_null() { alloc::oom() }
54 if PRINT { println!("allocate(size={} align={}) ret: 0x{:010x}",
55 size, align, ret as uint);
60 unsafe fn deallocate(ptr: *mut u8, size: uint, align: uint) {
61 if PRINT { println!("deallocate(ptr=0x{:010x} size={} align={})",
62 ptr as uint, size, align);
65 heap::deallocate(ptr, size, align);
67 unsafe fn reallocate(ptr: *mut u8, old_size: uint, size: uint, align: uint) -> *mut u8 {
69 println!("reallocate(ptr=0x{:010x} old_size={} size={} align={})",
70 ptr as uint, old_size, size, align);
73 let ret = heap::reallocate(ptr, old_size, size, align);
74 if ret.is_null() { alloc::oom() }
77 println!("reallocate(ptr=0x{:010x} old_size={} size={} align={}) \
79 ptr as uint, old_size, size, align, ret as uint);
84 fn idx_to_size(i: uint) -> uint { (i+1) * 10 }
86 // Allocate pairs of rows that form a triangle shape. (Hope is
87 // that at least two rows will be allocated near each other, so
88 // that we trigger the bug (a buffer overrun) in an observable
90 for i in range(0u, COUNT / 2) {
91 let size = idx_to_size(i);
92 ascend[2*i] = allocate(size, ALIGN);
93 ascend[2*i+1] = allocate(size, ALIGN);
96 // Initialize each pair of rows to distinct value.
97 for i in range(0u, COUNT / 2) {
98 let (p0, p1, size) = (ascend[2*i], ascend[2*i+1], idx_to_size(i));
99 for j in range(0, size) {
100 *p0.offset(j as int) = i as u8;
101 *p1.offset(j as int) = i as u8;
105 sanity_check(ascend.as_slice());
106 test_1(ascend); // triangle -> square
107 test_2(ascend); // square -> triangle
108 test_3(ascend); // triangle -> square
109 test_4(ascend); // square -> triangle
111 for i in range(0u, COUNT / 2) {
112 let size = idx_to_size(i);
113 deallocate(ascend[2*i], size, ALIGN);
114 deallocate(ascend[2*i+1], size, ALIGN);
119 // Test 1: turn the triangle into a square (in terms of
120 // allocation; initialized portion remains a triangle) by
121 // realloc'ing each row from top to bottom, and checking all the
123 unsafe fn test_1(ascend: &mut [*mut u8]) {
124 let new_size = idx_to_size(COUNT-1);
125 for i in range(0u, COUNT / 2) {
126 let (p0, p1, old_size) = (ascend[2*i], ascend[2*i+1], idx_to_size(i));
127 assert!(old_size < new_size);
129 ascend[2*i] = reallocate(p0, old_size, new_size, ALIGN);
130 sanity_check(ascend.as_slice());
132 ascend[2*i+1] = reallocate(p1, old_size, new_size, ALIGN);
133 sanity_check(ascend.as_slice());
137 // Test 2: turn the square back into a triangle, top to bottom.
138 unsafe fn test_2(ascend: &mut [*mut u8]) {
139 let old_size = idx_to_size(COUNT-1);
140 for i in range(0u, COUNT / 2) {
141 let (p0, p1, new_size) = (ascend[2*i], ascend[2*i+1], idx_to_size(i));
142 assert!(new_size < old_size);
144 ascend[2*i] = reallocate(p0, old_size, new_size, ALIGN);
145 sanity_check(ascend.as_slice());
147 ascend[2*i+1] = reallocate(p1, old_size, new_size, ALIGN);
148 sanity_check(ascend.as_slice());
152 // Test 3: turn triangle into a square, bottom to top.
153 unsafe fn test_3(ascend: &mut [*mut u8]) {
154 let new_size = idx_to_size(COUNT-1);
155 for i in range(0u, COUNT / 2).rev() {
156 let (p0, p1, old_size) = (ascend[2*i], ascend[2*i+1], idx_to_size(i));
157 assert!(old_size < new_size);
159 ascend[2*i+1] = reallocate(p1, old_size, new_size, ALIGN);
160 sanity_check(ascend.as_slice());
162 ascend[2*i] = reallocate(p0, old_size, new_size, ALIGN);
163 sanity_check(ascend.as_slice());
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 for i in range(0u, 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);
174 ascend[2*i+1] = reallocate(p1, old_size, new_size, ALIGN);
175 sanity_check(ascend.as_slice());
177 ascend[2*i] = reallocate(p0, old_size, new_size, ALIGN);
178 sanity_check(ascend.as_slice());