]> git.lizzy.rs Git - rust.git/blob - src/test/run-pass/realloc-16687.rs
Rollup merge of #21964 - semarie:openbsd-env, r=alexcrichton
[rust.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 extern crate alloc;
17
18 use alloc::heap;
19 use std::ptr;
20 use std::iter::repeat;
21
22 fn main() {
23     unsafe {
24         assert!(test_triangle());
25     }
26 }
27
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;
33
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));
40             for j in 0u..size {
41                 assert_eq!(*p0.offset(j as int), i as u8);
42                 assert_eq!(*p1.offset(j as int), i as u8);
43             }
44         }
45     }
46
47     static PRINT : bool = false;
48
49     unsafe fn allocate(size: uint, align: uint) -> *mut u8 {
50         if PRINT { println!("allocate(size={} align={})", size, align); }
51
52         let ret = heap::allocate(size, align);
53         if ret.is_null() { alloc::oom() }
54
55         if PRINT { println!("allocate(size={} align={}) ret: 0x{:010x}",
56                             size, align, ret as uint);
57         }
58
59         ret
60     }
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);
64         }
65
66         heap::deallocate(ptr, size, align);
67     }
68     unsafe fn reallocate(ptr: *mut u8, old_size: uint, size: uint, align: uint) -> *mut u8 {
69         if PRINT {
70             println!("reallocate(ptr=0x{:010x} old_size={} size={} align={})",
71                      ptr as uint, old_size, size, align);
72         }
73
74         let ret = heap::reallocate(ptr, old_size, size, align);
75         if ret.is_null() { alloc::oom() }
76
77         if PRINT {
78             println!("reallocate(ptr=0x{:010x} old_size={} size={} align={}) \
79                       ret: 0x{:010x}",
80                      ptr as uint, old_size, size, align, ret as uint);
81         }
82         ret
83     }
84
85     fn idx_to_size(i: uint) -> uint { (i+1) * 10 }
86
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
90     // way.)
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);
95     }
96
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));
100         for j in 0..size {
101             *p0.offset(j as int) = i as u8;
102             *p1.offset(j as int) = i as u8;
103         }
104     }
105
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
111
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);
116     }
117
118     return true;
119
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
123     // rows as we go.
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);
129
130             ascend[2*i] = reallocate(p0, old_size, new_size, ALIGN);
131             sanity_check(&*ascend);
132
133             ascend[2*i+1] = reallocate(p1, old_size, new_size, ALIGN);
134             sanity_check(&*ascend);
135         }
136     }
137
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);
144
145             ascend[2*i] = reallocate(p0, old_size, new_size, ALIGN);
146             sanity_check(&*ascend);
147
148             ascend[2*i+1] = reallocate(p1, old_size, new_size, ALIGN);
149             sanity_check(&*ascend);
150         }
151     }
152
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);
159
160             ascend[2*i+1] = reallocate(p1, old_size, new_size, ALIGN);
161             sanity_check(&*ascend);
162
163             ascend[2*i] = reallocate(p0, old_size, new_size, ALIGN);
164             sanity_check(&*ascend);
165         }
166     }
167
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);
174
175             ascend[2*i+1] = reallocate(p1, old_size, new_size, ALIGN);
176             sanity_check(&*ascend);
177
178             ascend[2*i] = reallocate(p0, old_size, new_size, ALIGN);
179             sanity_check(&*ascend);
180         }
181     }
182 }