]> git.lizzy.rs Git - rust.git/blob - src/test/run-pass/realloc-16687.rs
rustc: Implement the #[global_allocator] attribute
[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 #![feature(heap_api, allocator_api)]
17
18 use std::heap::{Heap, Alloc, Layout};
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 : usize = 16;
29     let mut ascend = vec![ptr::null_mut(); COUNT];
30     let ascend = &mut *ascend;
31     static ALIGN : usize = 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 0..COUNT / 2 {
38             let (p0, p1, size) = (ascend[2*i], ascend[2*i+1], idx_to_size(i));
39             for j in 0..size {
40                 assert_eq!(*p0.offset(j as isize), i as u8);
41                 assert_eq!(*p1.offset(j as isize), i as u8);
42             }
43         }
44     }
45
46     static PRINT : bool = false;
47
48     unsafe fn allocate(layout: Layout) -> *mut u8 {
49         if PRINT {
50             println!("allocate({:?})", layout);
51         }
52
53         let ret = Heap.alloc(layout.clone()).unwrap_or_else(|e| Heap.oom(e));
54
55         if PRINT {
56             println!("allocate({:?}) = {:?}", layout, ret);
57         }
58
59         ret
60     }
61
62     unsafe fn deallocate(ptr: *mut u8, layout: Layout) {
63         if PRINT {
64             println!("deallocate({:?}, {:?}", ptr, layout);
65         }
66
67         Heap.dealloc(ptr, layout);
68     }
69
70     unsafe fn reallocate(ptr: *mut u8, old: Layout, new: Layout) -> *mut u8 {
71         if PRINT {
72             println!("reallocate({:?}, old={:?}, new={:?})", ptr, old, new);
73         }
74
75         let ret = Heap.realloc(ptr, old.clone(), new.clone())
76             .unwrap_or_else(|e| Heap.oom(e));
77
78         if PRINT {
79             println!("reallocate({:?}, old={:?}, new={:?}) = {:?}",
80                      ptr, old, new, ret);
81         }
82         ret
83     }
84
85     fn idx_to_size(i: usize) -> usize { (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 0..COUNT / 2 {
92         let size = idx_to_size(i);
93         ascend[2*i]   = allocate(Layout::from_size_align(size, ALIGN).unwrap());
94         ascend[2*i+1] = allocate(Layout::from_size_align(size, ALIGN).unwrap());
95     }
96
97     // Initialize each pair of rows to distinct value.
98     for i in 0..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 isize) = i as u8;
102             *p1.offset(j as isize) = 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 0..COUNT / 2 {
113         let size = idx_to_size(i);
114         deallocate(ascend[2*i], Layout::from_size_align(size, ALIGN).unwrap());
115         deallocate(ascend[2*i+1], Layout::from_size_align(size, ALIGN).unwrap());
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         let new = Layout::from_size_align(new_size, ALIGN).unwrap();
127         for i in 0..COUNT / 2 {
128             let (p0, p1, old_size) = (ascend[2*i], ascend[2*i+1], idx_to_size(i));
129             assert!(old_size < new_size);
130             let old = Layout::from_size_align(old_size, ALIGN).unwrap();
131
132             ascend[2*i] = reallocate(p0, old.clone(), new.clone());
133             sanity_check(&*ascend);
134
135             ascend[2*i+1] = reallocate(p1, old.clone(), new.clone());
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         let old = Layout::from_size_align(old_size, ALIGN).unwrap();
144         for i in 0..COUNT / 2 {
145             let (p0, p1, new_size) = (ascend[2*i], ascend[2*i+1], idx_to_size(i));
146             assert!(new_size < old_size);
147             let new = Layout::from_size_align(new_size, ALIGN).unwrap();
148
149             ascend[2*i] = reallocate(p0, old.clone(), new.clone());
150             sanity_check(&*ascend);
151
152             ascend[2*i+1] = reallocate(p1, old.clone(), new.clone());
153             sanity_check(&*ascend);
154         }
155     }
156
157     // Test 3: turn triangle into a square, bottom to top.
158     unsafe fn test_3(ascend: &mut [*mut u8]) {
159         let new_size = idx_to_size(COUNT-1);
160         let new = Layout::from_size_align(new_size, ALIGN).unwrap();
161         for i in (0..COUNT / 2).rev() {
162             let (p0, p1, old_size) = (ascend[2*i], ascend[2*i+1], idx_to_size(i));
163             assert!(old_size < new_size);
164             let old = Layout::from_size_align(old_size, ALIGN).unwrap();
165
166             ascend[2*i+1] = reallocate(p1, old.clone(), new.clone());
167             sanity_check(&*ascend);
168
169             ascend[2*i] = reallocate(p0, old.clone(), new.clone());
170             sanity_check(&*ascend);
171         }
172     }
173
174     // Test 4: turn the square back into a triangle, bottom to top.
175     unsafe fn test_4(ascend: &mut [*mut u8]) {
176         let old_size = idx_to_size(COUNT-1);
177         let old = Layout::from_size_align(old_size, ALIGN).unwrap();
178         for i in (0..COUNT / 2).rev() {
179             let (p0, p1, new_size) = (ascend[2*i], ascend[2*i+1], idx_to_size(i));
180             assert!(new_size < old_size);
181             let new = Layout::from_size_align(new_size, ALIGN).unwrap();
182
183             ascend[2*i+1] = reallocate(p1, old.clone(), new.clone());
184             sanity_check(&*ascend);
185
186             ascend[2*i] = reallocate(p0, old.clone(), new.clone());
187             sanity_check(&*ascend);
188         }
189     }
190 }