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