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, alloc, oom)]
22 use std
::iter
::repeat
;
26 assert
!(test_triangle());
30 unsafe fn test_triangle() -> bool
{
31 static COUNT
: usize = 16;
32 let mut ascend
= repeat(ptr
::null_mut()).take(COUNT
).collect
::<Vec
<_
>>();
33 let ascend
= &mut *ascend
;
34 static ALIGN
: usize = 1;
36 // Checks that `ascend` forms triangle of ascending size formed
37 // from pairs of rows (where each pair of rows is equally sized),
38 // and the elements of the triangle match their row-pair index.
39 unsafe fn sanity_check(ascend
: &[*mut u8]) {
40 for i
in 0..COUNT
/ 2 {
41 let (p0
, p1
, size
) = (ascend
[2*i
], ascend
[2*i
+1], idx_to_size(i
));
43 assert_eq
!(*p0
.offset(j
as isize), i
as u8);
44 assert_eq
!(*p1
.offset(j
as isize), i
as u8);
49 static PRINT
: bool
= false;
51 unsafe fn allocate(size
: usize, align
: usize) -> *mut u8 {
52 if PRINT { println!("allocate(size={} align
={}
)", size, align); }
54 let ret = heap::allocate(size, align);
55 if ret.is_null() { alloc::oom() }
57 if PRINT { println!("allocate(size={} align={}) ret: 0x{:010x}",
58 size
, align
, ret
as usize);
63 unsafe fn deallocate(ptr
: *mut u8, size
: usize, align
: usize) {
64 if PRINT { println!("deallocate(ptr=0x{:010x} size
={} align
={}
)",
65 ptr as usize, size, align);
68 heap::deallocate(ptr, size, align);
70 unsafe fn reallocate(ptr: *mut u8, old_size: usize, size: usize, align: usize) -> *mut u8 {
72 println!("reallocate(ptr
=0x{:010x} old_size
={} size
={} align
={}
)",
73 ptr as usize, old_size, size, align);
76 let ret = heap::reallocate(ptr, old_size, size, align);
77 if ret.is_null() { alloc::oom() }
80 println!("reallocate(ptr
=0x{:010x} old_size
={} size
={} align
={}
) \
82 ptr as usize, old_size, size, align, ret as usize);
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(size, ALIGN);
96 ascend[2*i+1] = allocate(size, ALIGN);
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], size, ALIGN);
117 deallocate(ascend[2*i+1], size, ALIGN);
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 for i in 0..COUNT / 2 {
129 let (p0, p1, old_size) = (ascend[2*i], ascend[2*i+1], idx_to_size(i));
130 assert!(old_size < new_size);
132 ascend[2*i] = reallocate(p0, old_size, new_size, ALIGN);
133 sanity_check(&*ascend);
135 ascend[2*i+1] = reallocate(p1, old_size, new_size, ALIGN);
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 for i in 0..COUNT / 2 {
144 let (p0, p1, new_size) = (ascend[2*i], ascend[2*i+1], idx_to_size(i));
145 assert!(new_size < old_size);
147 ascend[2*i] = reallocate(p0, old_size, new_size, ALIGN);
148 sanity_check(&*ascend);
150 ascend[2*i+1] = reallocate(p1, old_size, new_size, ALIGN);
151 sanity_check(&*ascend);
155 // Test 3: turn triangle into a square, bottom to top.
156 unsafe fn test_3(ascend: &mut [*mut u8]) {
157 let new_size = idx_to_size(COUNT-1);
158 for i in (0..COUNT / 2).rev() {
159 let (p0, p1, old_size) = (ascend[2*i], ascend[2*i+1], idx_to_size(i));
160 assert!(old_size < new_size);
162 ascend[2*i+1] = reallocate(p1, old_size, new_size, ALIGN);
163 sanity_check(&*ascend);
165 ascend[2*i] = reallocate(p0, old_size, new_size, ALIGN);
166 sanity_check(&*ascend);
170 // Test 4: turn the square back into a triangle, bottom to top.
171 unsafe fn test_4(ascend: &mut [*mut u8]) {
172 let old_size = idx_to_size(COUNT-1);
173 for i in (0..COUNT / 2).rev() {
174 let (p0, p1, new_size) = (ascend[2*i], ascend[2*i+1], idx_to_size(i));
175 assert!(new_size < old_size);
177 ascend[2*i+1] = reallocate(p1, old_size, new_size, ALIGN);
178 sanity_check(&*ascend);
180 ascend[2*i] = reallocate(p0, old_size, new_size, ALIGN);
181 sanity_check(&*ascend);