]> git.lizzy.rs Git - rust.git/blob - src/test/run-pass/realloc-16687.rs
doc: remove incomplete sentence
[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
21 fn main() {
22     unsafe {
23         assert!(test_triangle());
24     }
25 }
26
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;
32
33     // Checks that `ascend` forms triangle of ascending 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);
42             }
43         }
44     }
45
46     static PRINT : bool = false;
47
48     unsafe fn allocate(size: uint, align: uint) -> *mut u8 {
49         if PRINT { println!("allocate(size={} align={})", size, align); }
50
51         let ret = heap::allocate(size, align);
52         if ret.is_null() { alloc::oom() }
53
54         if PRINT { println!("allocate(size={} align={}) ret: 0x{:010x}",
55                             size, align, ret as uint);
56         }
57
58         ret
59     }
60     unsafe fn deallocate(ptr: *mut u8, size: uint, align: uint) {
61         if PRINT { println!("deallocate(ptr=0x{:010x} size={} align={})",
62                             ptr as uint, size, align);
63         }
64
65         heap::deallocate(ptr, size, align);
66     }
67     unsafe fn reallocate(ptr: *mut u8, old_size: uint, size: uint, align: uint) -> *mut u8 {
68         if PRINT {
69             println!("reallocate(ptr=0x{:010x} old_size={} size={} align={})",
70                      ptr as uint, old_size, size, align);
71         }
72
73         let ret = heap::reallocate(ptr, old_size, size, align);
74         if ret.is_null() { alloc::oom() }
75
76         if PRINT {
77             println!("reallocate(ptr=0x{:010x} old_size={} size={} align={}) \
78                       ret: 0x{:010x}",
79                      ptr as uint, old_size, size, align, ret as uint);
80         }
81         ret
82     }
83
84     fn idx_to_size(i: uint) -> uint { (i+1) * 10 }
85
86     // Allocate pairs of rows that form a triangle shape.  (Hope is
87     // that at least two rows will be allocated near each other, so
88     // that we trigger the bug (a buffer overrun) in an observable
89     // way.)
90     for i in range(0u, COUNT / 2) {
91         let size = idx_to_size(i);
92         ascend[2*i]   = allocate(size, ALIGN);
93         ascend[2*i+1] = allocate(size, ALIGN);
94     }
95
96     // Initialize each pair of rows to distinct value.
97     for i in range(0u, COUNT / 2) {
98         let (p0, p1, size) = (ascend[2*i], ascend[2*i+1], idx_to_size(i));
99         for j in range(0, size) {
100             *p0.offset(j as int) = i as u8;
101             *p1.offset(j as int) = i as u8;
102         }
103     }
104
105     sanity_check(ascend.as_slice());
106     test_1(ascend); // triangle -> square
107     test_2(ascend); // square -> triangle
108     test_3(ascend); // triangle -> square
109     test_4(ascend); // square -> triangle
110
111     for i in range(0u, COUNT / 2) {
112         let size = idx_to_size(i);
113         deallocate(ascend[2*i], size, ALIGN);
114         deallocate(ascend[2*i+1], size, ALIGN);
115     }
116
117     return true;
118
119     // Test 1: turn the triangle into a square (in terms of
120     // allocation; initialized portion remains a triangle) by
121     // realloc'ing each row from top to bottom, and checking all the
122     // rows as we go.
123     unsafe fn test_1(ascend: &mut [*mut u8]) {
124         let new_size = idx_to_size(COUNT-1);
125         for i in range(0u, COUNT / 2) {
126             let (p0, p1, old_size) = (ascend[2*i], ascend[2*i+1], idx_to_size(i));
127             assert!(old_size < new_size);
128
129             ascend[2*i] = reallocate(p0, old_size, new_size, ALIGN);
130             sanity_check(ascend.as_slice());
131
132             ascend[2*i+1] = reallocate(p1, old_size, new_size, ALIGN);
133             sanity_check(ascend.as_slice());
134         }
135     }
136
137     // Test 2: turn the square back into a triangle, top to bottom.
138     unsafe fn test_2(ascend: &mut [*mut u8]) {
139         let old_size = idx_to_size(COUNT-1);
140         for i in range(0u, COUNT / 2) {
141             let (p0, p1, new_size) = (ascend[2*i], ascend[2*i+1], idx_to_size(i));
142             assert!(new_size < old_size);
143
144             ascend[2*i] = reallocate(p0, old_size, new_size, ALIGN);
145             sanity_check(ascend.as_slice());
146
147             ascend[2*i+1] = reallocate(p1, old_size, new_size, ALIGN);
148             sanity_check(ascend.as_slice());
149         }
150     }
151
152     // Test 3: turn triangle into a square, bottom to top.
153     unsafe fn test_3(ascend: &mut [*mut u8]) {
154         let new_size = idx_to_size(COUNT-1);
155         for i in range(0u, COUNT / 2).rev() {
156             let (p0, p1, old_size) = (ascend[2*i], ascend[2*i+1], idx_to_size(i));
157             assert!(old_size < new_size);
158
159             ascend[2*i+1] = reallocate(p1, old_size, new_size, ALIGN);
160             sanity_check(ascend.as_slice());
161
162             ascend[2*i] = reallocate(p0, old_size, new_size, ALIGN);
163             sanity_check(ascend.as_slice());
164         }
165     }
166
167     // Test 4: turn the square back into a triangle, bottom to top.
168     unsafe fn test_4(ascend: &mut [*mut u8]) {
169         let old_size = idx_to_size(COUNT-1);
170         for i in range(0u, COUNT / 2).rev() {
171             let (p0, p1, new_size) = (ascend[2*i], ascend[2*i+1], idx_to_size(i));
172             assert!(new_size < old_size);
173
174             ascend[2*i+1] = reallocate(p1, old_size, new_size, ALIGN);
175             sanity_check(ascend.as_slice());
176
177             ascend[2*i] = reallocate(p0, old_size, new_size, ALIGN);
178             sanity_check(ascend.as_slice());
179         }
180     }
181 }