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 acending 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={:u} align={:u})", size, align); }
51 let ret = heap::allocate(size, align);
53 if PRINT { println!("allocate(size={:u} align={:u}) ret: 0x{:010x}",
54 size, align, ret as uint);
59 unsafe fn deallocate(ptr: *mut u8, size: uint, align: uint) {
60 if PRINT { println!("deallocate(ptr=0x{:010x} size={:u} align={:u})",
61 ptr as uint, size, align);
64 heap::deallocate(ptr, size, align);
66 unsafe fn reallocate(ptr: *mut u8, size: uint, align: uint,
67 old_size: uint) -> *mut u8 {
69 println!("reallocate(ptr=0x{:010x} size={:u} align={:u} old_size={:u})",
70 ptr as uint, size, align, old_size);
73 let ret = heap::reallocate(ptr, size, align, old_size);
76 println!("reallocate(ptr=0x{:010x} size={:u} align={:u} old_size={:u}) \
78 ptr as uint, size, align, old_size, ret as uint);
83 fn idx_to_size(i: uint) -> uint { (i+1) * 10 }
85 // Allocate pairs of rows that form a triangle shape. (Hope is
86 // that at least two rows will be allocated near each other, so
87 // that we trigger the bug (a buffer overrun) in an observable
89 for i in range(0u, COUNT / 2) {
90 let size = idx_to_size(i);
91 ascend[2*i] = allocate(size, ALIGN);
92 ascend[2*i+1] = allocate(size, ALIGN);
95 // Initialize each pair of rows to distinct value.
96 for i in range(0u, COUNT / 2) {
97 let (p0, p1, size) = (ascend[2*i], ascend[2*i+1], idx_to_size(i));
98 for j in range(0, size) {
99 *p0.offset(j as int) = i as u8;
100 *p1.offset(j as int) = i as u8;
104 sanity_check(ascend.as_slice());
105 test_1(ascend); // triangle -> square
106 test_2(ascend); // square -> triangle
107 test_3(ascend); // triangle -> square
108 test_4(ascend); // square -> triangle
110 for i in range(0u, COUNT / 2) {
111 let size = idx_to_size(i);
112 deallocate(ascend[2*i], size, ALIGN);
113 deallocate(ascend[2*i+1], size, ALIGN);
118 // Test 1: turn the triangle into a square (in terms of
119 // allocation; initialized portion remains a triangle) by
120 // realloc'ing each row from top to bottom, and checking all the
122 unsafe fn test_1(ascend: &mut [*mut u8]) {
123 let new_size = idx_to_size(COUNT-1);
124 for i in range(0u, COUNT / 2) {
125 let (p0, p1, old_size) = (ascend[2*i], ascend[2*i+1], idx_to_size(i));
126 assert!(old_size < new_size);
128 ascend[2*i] = reallocate(p0, new_size, ALIGN, old_size);
129 sanity_check(ascend.as_slice());
131 ascend[2*i+1] = reallocate(p1, new_size, ALIGN, old_size);
132 sanity_check(ascend.as_slice());
136 // Test 2: turn the square back into a triangle, top to bottom.
137 unsafe fn test_2(ascend: &mut [*mut u8]) {
138 let old_size = idx_to_size(COUNT-1);
139 for i in range(0u, COUNT / 2) {
140 let (p0, p1, new_size) = (ascend[2*i], ascend[2*i+1], idx_to_size(i));
141 assert!(new_size < old_size);
143 ascend[2*i] = reallocate(p0, new_size, ALIGN, old_size);
144 sanity_check(ascend.as_slice());
146 ascend[2*i+1] = reallocate(p1, new_size, ALIGN, old_size);
147 sanity_check(ascend.as_slice());
151 // Test 3: turn triangle into a square, bottom to top.
152 unsafe fn test_3(ascend: &mut [*mut u8]) {
153 let new_size = idx_to_size(COUNT-1);
154 for i in range(0u, 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);
158 ascend[2*i+1] = reallocate(p1, new_size, ALIGN, old_size);
159 sanity_check(ascend.as_slice());
161 ascend[2*i] = reallocate(p0, new_size, ALIGN, old_size);
162 sanity_check(ascend.as_slice());
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 for i in range(0u, COUNT / 2).rev() {
170 let (p0, p1, new_size) = (ascend[2*i], ascend[2*i+1], idx_to_size(i));
171 assert!(new_size < old_size);
173 ascend[2*i+1] = reallocate(p1, new_size, ALIGN, old_size);
174 sanity_check(ascend.as_slice());
176 ascend[2*i] = reallocate(p0, new_size, ALIGN, old_size);
177 sanity_check(ascend.as_slice());