]> git.proxmox.com Git - rustc.git/blob - src/test/run-pass/realloc-16687.rs
Imported Upstream version 1.2.0+dfsg1
[rustc.git] / src / test / run-pass / realloc-16687.rs
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.
4 //
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.
10
11 // alloc::heap::reallocate test.
12 //
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.
15
16 #![feature(heap_api, alloc, oom)]
17
18 extern crate alloc;
19
20 use alloc::heap;
21 use std::ptr;
22 use std::iter::repeat;
23
24 fn main() {
25 unsafe {
26 assert!(test_triangle());
27 }
28 }
29
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;
35
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));
42 for j in 0..size {
43 assert_eq!(*p0.offset(j as isize), i as u8);
44 assert_eq!(*p1.offset(j as isize), i as u8);
45 }
46 }
47 }
48
49 static PRINT : bool = false;
50
51 unsafe fn allocate(size: usize, align: usize) -> *mut u8 {
52 if PRINT { println!("allocate(size={} align={})", size, align); }
53
54 let ret = heap::allocate(size, align);
55 if ret.is_null() { alloc::oom() }
56
57 if PRINT { println!("allocate(size={} align={}) ret: 0x{:010x}",
58 size, align, ret as usize);
59 }
60
61 ret
62 }
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);
66 }
67
68 heap::deallocate(ptr, size, align);
69 }
70 unsafe fn reallocate(ptr: *mut u8, old_size: usize, size: usize, align: usize) -> *mut u8 {
71 if PRINT {
72 println!("reallocate(ptr=0x{:010x} old_size={} size={} align={})",
73 ptr as usize, old_size, size, align);
74 }
75
76 let ret = heap::reallocate(ptr, old_size, size, align);
77 if ret.is_null() { alloc::oom() }
78
79 if PRINT {
80 println!("reallocate(ptr=0x{:010x} old_size={} size={} align={}) \
81 ret: 0x{:010x}",
82 ptr as usize, old_size, size, align, ret as usize);
83 }
84 ret
85 }
86
87 fn idx_to_size(i: usize) -> usize { (i+1) * 10 }
88
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
92 // way.)
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);
97 }
98
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));
102 for j in 0..size {
103 *p0.offset(j as isize) = i as u8;
104 *p1.offset(j as isize) = i as u8;
105 }
106 }
107
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
113
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);
118 }
119
120 return true;
121
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
125 // rows as we go.
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);
131
132 ascend[2*i] = reallocate(p0, old_size, new_size, ALIGN);
133 sanity_check(&*ascend);
134
135 ascend[2*i+1] = reallocate(p1, old_size, new_size, ALIGN);
136 sanity_check(&*ascend);
137 }
138 }
139
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);
146
147 ascend[2*i] = reallocate(p0, old_size, new_size, ALIGN);
148 sanity_check(&*ascend);
149
150 ascend[2*i+1] = reallocate(p1, old_size, new_size, ALIGN);
151 sanity_check(&*ascend);
152 }
153 }
154
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);
161
162 ascend[2*i+1] = reallocate(p1, old_size, new_size, ALIGN);
163 sanity_check(&*ascend);
164
165 ascend[2*i] = reallocate(p0, old_size, new_size, ALIGN);
166 sanity_check(&*ascend);
167 }
168 }
169
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);
176
177 ascend[2*i+1] = reallocate(p1, old_size, new_size, ALIGN);
178 sanity_check(&*ascend);
179
180 ascend[2*i] = reallocate(p0, old_size, new_size, ALIGN);
181 sanity_check(&*ascend);
182 }
183 }
184 }