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.
20 use std::iter::repeat;
24 assert!(test_triangle());
28 unsafe fn test_triangle() -> bool {
29 static COUNT : uint = 16;
30 let mut ascend = repeat(ptr::null_mut()).take(COUNT).collect::<Vec<_>>();
31 let ascend = &mut *ascend;
32 static ALIGN : uint = 1;
34 // Checks that `ascend` forms triangle of ascending size formed
35 // from pairs of rows (where each pair of rows is equally sized),
36 // and the elements of the triangle match their row-pair index.
37 unsafe fn sanity_check(ascend: &[*mut u8]) {
38 for i in 0u..COUNT / 2 {
39 let (p0, p1, size) = (ascend[2*i], ascend[2*i+1], idx_to_size(i));
41 assert_eq!(*p0.offset(j as int), i as u8);
42 assert_eq!(*p1.offset(j as int), i as u8);
47 static PRINT : bool = false;
49 unsafe fn allocate(size: uint, align: uint) -> *mut u8 {
50 if PRINT { println!("allocate(size={} align={})", size, align); }
52 let ret = heap::allocate(size, align);
53 if ret.is_null() { alloc::oom() }
55 if PRINT { println!("allocate(size={} align={}) ret: 0x{:010x}",
56 size, align, ret as uint);
61 unsafe fn deallocate(ptr: *mut u8, size: uint, align: uint) {
62 if PRINT { println!("deallocate(ptr=0x{:010x} size={} align={})",
63 ptr as uint, size, align);
66 heap::deallocate(ptr, size, align);
68 unsafe fn reallocate(ptr: *mut u8, old_size: uint, size: uint, align: uint) -> *mut u8 {
70 println!("reallocate(ptr=0x{:010x} old_size={} size={} align={})",
71 ptr as uint, old_size, size, align);
74 let ret = heap::reallocate(ptr, old_size, size, align);
75 if ret.is_null() { alloc::oom() }
78 println!("reallocate(ptr=0x{:010x} old_size={} size={} align={}) \
80 ptr as uint, old_size, size, align, ret as uint);
85 fn idx_to_size(i: uint) -> uint { (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 0u..COUNT / 2 {
92 let size = idx_to_size(i);
93 ascend[2*i] = allocate(size, ALIGN);
94 ascend[2*i+1] = allocate(size, ALIGN);
97 // Initialize each pair of rows to distinct value.
98 for i in 0u..COUNT / 2 {
99 let (p0, p1, size) = (ascend[2*i], ascend[2*i+1], idx_to_size(i));
101 *p0.offset(j as int) = i as u8;
102 *p1.offset(j as int) = 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 0u..COUNT / 2 {
113 let size = idx_to_size(i);
114 deallocate(ascend[2*i], size, ALIGN);
115 deallocate(ascend[2*i+1], size, ALIGN);
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 for i in 0u..COUNT / 2 {
127 let (p0, p1, old_size) = (ascend[2*i], ascend[2*i+1], idx_to_size(i));
128 assert!(old_size < new_size);
130 ascend[2*i] = reallocate(p0, old_size, new_size, ALIGN);
131 sanity_check(&*ascend);
133 ascend[2*i+1] = reallocate(p1, old_size, new_size, ALIGN);
134 sanity_check(&*ascend);
138 // Test 2: turn the square back into a triangle, top to bottom.
139 unsafe fn test_2(ascend: &mut [*mut u8]) {
140 let old_size = idx_to_size(COUNT-1);
141 for i in 0u..COUNT / 2 {
142 let (p0, p1, new_size) = (ascend[2*i], ascend[2*i+1], idx_to_size(i));
143 assert!(new_size < old_size);
145 ascend[2*i] = reallocate(p0, old_size, new_size, ALIGN);
146 sanity_check(&*ascend);
148 ascend[2*i+1] = reallocate(p1, old_size, new_size, ALIGN);
149 sanity_check(&*ascend);
153 // Test 3: turn triangle into a square, bottom to top.
154 unsafe fn test_3(ascend: &mut [*mut u8]) {
155 let new_size = idx_to_size(COUNT-1);
156 for i in (0u..COUNT / 2).rev() {
157 let (p0, p1, old_size) = (ascend[2*i], ascend[2*i+1], idx_to_size(i));
158 assert!(old_size < new_size);
160 ascend[2*i+1] = reallocate(p1, old_size, new_size, ALIGN);
161 sanity_check(&*ascend);
163 ascend[2*i] = reallocate(p0, old_size, new_size, ALIGN);
164 sanity_check(&*ascend);
168 // Test 4: turn the square back into a triangle, bottom to top.
169 unsafe fn test_4(ascend: &mut [*mut u8]) {
170 let old_size = idx_to_size(COUNT-1);
171 for i in (0u..COUNT / 2).rev() {
172 let (p0, p1, new_size) = (ascend[2*i], ascend[2*i+1], idx_to_size(i));
173 assert!(new_size < old_size);
175 ascend[2*i+1] = reallocate(p1, old_size, new_size, ALIGN);
176 sanity_check(&*ascend);
178 ascend[2*i] = reallocate(p0, old_size, new_size, ALIGN);
179 sanity_check(&*ascend);