]> git.lizzy.rs Git - rust.git/blob - src/test/run-pass/realloc-16687.rs
Rollup merge of #53110 - Xanewok:save-analysis-remap-path, r=nrc
[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(allocator_api)]
17
18 use std::alloc::{Global, Alloc, Layout, handle_alloc_error};
19 use std::ptr::{self, NonNull};
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 = Global.alloc(layout).unwrap_or_else(|_| handle_alloc_error(layout));
54
55         if PRINT {
56             println!("allocate({:?}) = {:?}", layout, ret);
57         }
58
59         ret.cast().as_ptr()
60     }
61
62     unsafe fn deallocate(ptr: *mut u8, layout: Layout) {
63         if PRINT {
64             println!("deallocate({:?}, {:?}", ptr, layout);
65         }
66
67         Global.dealloc(NonNull::new_unchecked(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 = Global.realloc(NonNull::new_unchecked(ptr), old, new.size())
76             .unwrap_or_else(|_| handle_alloc_error(
77                 Layout::from_size_align_unchecked(new.size(), old.align())
78             ));
79
80         if PRINT {
81             println!("reallocate({:?}, old={:?}, new={:?}) = {:?}",
82                      ptr, old, new, ret);
83         }
84         ret.cast().as_ptr()
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(Layout::from_size_align(size, ALIGN).unwrap());
96         ascend[2*i+1] = allocate(Layout::from_size_align(size, ALIGN).unwrap());
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], Layout::from_size_align(size, ALIGN).unwrap());
117         deallocate(ascend[2*i+1], Layout::from_size_align(size, ALIGN).unwrap());
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         let new = Layout::from_size_align(new_size, ALIGN).unwrap();
129         for i in 0..COUNT / 2 {
130             let (p0, p1, old_size) = (ascend[2*i], ascend[2*i+1], idx_to_size(i));
131             assert!(old_size < new_size);
132             let old = Layout::from_size_align(old_size, ALIGN).unwrap();
133
134             ascend[2*i] = reallocate(p0, old.clone(), new.clone());
135             sanity_check(&*ascend);
136
137             ascend[2*i+1] = reallocate(p1, old.clone(), new.clone());
138             sanity_check(&*ascend);
139         }
140     }
141
142     // Test 2: turn the square back into a triangle, top to bottom.
143     unsafe fn test_2(ascend: &mut [*mut u8]) {
144         let old_size = idx_to_size(COUNT-1);
145         let old = Layout::from_size_align(old_size, ALIGN).unwrap();
146         for i in 0..COUNT / 2 {
147             let (p0, p1, new_size) = (ascend[2*i], ascend[2*i+1], idx_to_size(i));
148             assert!(new_size < old_size);
149             let new = Layout::from_size_align(new_size, ALIGN).unwrap();
150
151             ascend[2*i] = reallocate(p0, old.clone(), new.clone());
152             sanity_check(&*ascend);
153
154             ascend[2*i+1] = reallocate(p1, old.clone(), new.clone());
155             sanity_check(&*ascend);
156         }
157     }
158
159     // Test 3: turn triangle into a square, bottom to top.
160     unsafe fn test_3(ascend: &mut [*mut u8]) {
161         let new_size = idx_to_size(COUNT-1);
162         let new = Layout::from_size_align(new_size, ALIGN).unwrap();
163         for i in (0..COUNT / 2).rev() {
164             let (p0, p1, old_size) = (ascend[2*i], ascend[2*i+1], idx_to_size(i));
165             assert!(old_size < new_size);
166             let old = Layout::from_size_align(old_size, ALIGN).unwrap();
167
168             ascend[2*i+1] = reallocate(p1, old.clone(), new.clone());
169             sanity_check(&*ascend);
170
171             ascend[2*i] = reallocate(p0, old.clone(), new.clone());
172             sanity_check(&*ascend);
173         }
174     }
175
176     // Test 4: turn the square back into a triangle, bottom to top.
177     unsafe fn test_4(ascend: &mut [*mut u8]) {
178         let old_size = idx_to_size(COUNT-1);
179         let old = Layout::from_size_align(old_size, ALIGN).unwrap();
180         for i in (0..COUNT / 2).rev() {
181             let (p0, p1, new_size) = (ascend[2*i], ascend[2*i+1], idx_to_size(i));
182             assert!(new_size < old_size);
183             let new = Layout::from_size_align(new_size, ALIGN).unwrap();
184
185             ascend[2*i+1] = reallocate(p1, old.clone(), new.clone());
186             sanity_check(&*ascend);
187
188             ascend[2*i] = reallocate(p0, old.clone(), new.clone());
189             sanity_check(&*ascend);
190         }
191     }
192 }