]> git.lizzy.rs Git - rust.git/blob - src/test/run-pass/realloc-16687.rs
Auto merge of #61817 - eddyb:begone-gcx-attempt-2, r=oli-obk
[rust.git] / src / test / run-pass / realloc-16687.rs
1 // alloc::heap::reallocate test.
2 //
3 // Ideally this would be revised to use no_std, but for now it serves
4 // well enough to reproduce (and illustrate) the bug from #16687.
5
6 #![feature(allocator_api)]
7
8 use std::alloc::{Global, Alloc, Layout, handle_alloc_error};
9 use std::ptr::{self, NonNull};
10
11 fn main() {
12     unsafe {
13         assert!(test_triangle());
14     }
15 }
16
17 unsafe fn test_triangle() -> bool {
18     static COUNT : usize = 16;
19     let mut ascend = vec![ptr::null_mut(); COUNT];
20     let ascend = &mut *ascend;
21     static ALIGN : usize = 1;
22
23     // Checks that `ascend` forms triangle of ascending size formed
24     // from pairs of rows (where each pair of rows is equally sized),
25     // and the elements of the triangle match their row-pair index.
26     unsafe fn sanity_check(ascend: &[*mut u8]) {
27         for i in 0..COUNT / 2 {
28             let (p0, p1, size) = (ascend[2*i], ascend[2*i+1], idx_to_size(i));
29             for j in 0..size {
30                 assert_eq!(*p0.add(j), i as u8);
31                 assert_eq!(*p1.add(j), i as u8);
32             }
33         }
34     }
35
36     static PRINT : bool = false;
37
38     unsafe fn allocate(layout: Layout) -> *mut u8 {
39         if PRINT {
40             println!("allocate({:?})", layout);
41         }
42
43         let ret = Global.alloc(layout).unwrap_or_else(|_| handle_alloc_error(layout));
44
45         if PRINT {
46             println!("allocate({:?}) = {:?}", layout, ret);
47         }
48
49         ret.cast().as_ptr()
50     }
51
52     unsafe fn deallocate(ptr: *mut u8, layout: Layout) {
53         if PRINT {
54             println!("deallocate({:?}, {:?}", ptr, layout);
55         }
56
57         Global.dealloc(NonNull::new_unchecked(ptr), layout);
58     }
59
60     unsafe fn reallocate(ptr: *mut u8, old: Layout, new: Layout) -> *mut u8 {
61         if PRINT {
62             println!("reallocate({:?}, old={:?}, new={:?})", ptr, old, new);
63         }
64
65         let ret = Global.realloc(NonNull::new_unchecked(ptr), old, new.size())
66             .unwrap_or_else(|_| handle_alloc_error(
67                 Layout::from_size_align_unchecked(new.size(), old.align())
68             ));
69
70         if PRINT {
71             println!("reallocate({:?}, old={:?}, new={:?}) = {:?}",
72                      ptr, old, new, ret);
73         }
74         ret.cast().as_ptr()
75     }
76
77     fn idx_to_size(i: usize) -> usize { (i+1) * 10 }
78
79     // Allocate pairs of rows that form a triangle shape.  (Hope is
80     // that at least two rows will be allocated near each other, so
81     // that we trigger the bug (a buffer overrun) in an observable
82     // way.)
83     for i in 0..COUNT / 2 {
84         let size = idx_to_size(i);
85         ascend[2*i]   = allocate(Layout::from_size_align(size, ALIGN).unwrap());
86         ascend[2*i+1] = allocate(Layout::from_size_align(size, ALIGN).unwrap());
87     }
88
89     // Initialize each pair of rows to distinct value.
90     for i in 0..COUNT / 2 {
91         let (p0, p1, size) = (ascend[2*i], ascend[2*i+1], idx_to_size(i));
92         for j in 0..size {
93             *p0.add(j) = i as u8;
94             *p1.add(j) = i as u8;
95         }
96     }
97
98     sanity_check(&*ascend);
99     test_1(ascend); // triangle -> square
100     test_2(ascend); // square -> triangle
101     test_3(ascend); // triangle -> square
102     test_4(ascend); // square -> triangle
103
104     for i in 0..COUNT / 2 {
105         let size = idx_to_size(i);
106         deallocate(ascend[2*i], Layout::from_size_align(size, ALIGN).unwrap());
107         deallocate(ascend[2*i+1], Layout::from_size_align(size, ALIGN).unwrap());
108     }
109
110     return true;
111
112     // Test 1: turn the triangle into a square (in terms of
113     // allocation; initialized portion remains a triangle) by
114     // realloc'ing each row from top to bottom, and checking all the
115     // rows as we go.
116     unsafe fn test_1(ascend: &mut [*mut u8]) {
117         let new_size = idx_to_size(COUNT-1);
118         let new = Layout::from_size_align(new_size, ALIGN).unwrap();
119         for i in 0..COUNT / 2 {
120             let (p0, p1, old_size) = (ascend[2*i], ascend[2*i+1], idx_to_size(i));
121             assert!(old_size < new_size);
122             let old = Layout::from_size_align(old_size, ALIGN).unwrap();
123
124             ascend[2*i] = reallocate(p0, old.clone(), new.clone());
125             sanity_check(&*ascend);
126
127             ascend[2*i+1] = reallocate(p1, old.clone(), new.clone());
128             sanity_check(&*ascend);
129         }
130     }
131
132     // Test 2: turn the square back into a triangle, top to bottom.
133     unsafe fn test_2(ascend: &mut [*mut u8]) {
134         let old_size = idx_to_size(COUNT-1);
135         let old = Layout::from_size_align(old_size, ALIGN).unwrap();
136         for i in 0..COUNT / 2 {
137             let (p0, p1, new_size) = (ascend[2*i], ascend[2*i+1], idx_to_size(i));
138             assert!(new_size < old_size);
139             let new = Layout::from_size_align(new_size, ALIGN).unwrap();
140
141             ascend[2*i] = reallocate(p0, old.clone(), new.clone());
142             sanity_check(&*ascend);
143
144             ascend[2*i+1] = reallocate(p1, old.clone(), new.clone());
145             sanity_check(&*ascend);
146         }
147     }
148
149     // Test 3: turn triangle into a square, bottom to top.
150     unsafe fn test_3(ascend: &mut [*mut u8]) {
151         let new_size = idx_to_size(COUNT-1);
152         let new = Layout::from_size_align(new_size, ALIGN).unwrap();
153         for i in (0..COUNT / 2).rev() {
154             let (p0, p1, old_size) = (ascend[2*i], ascend[2*i+1], idx_to_size(i));
155             assert!(old_size < new_size);
156             let old = Layout::from_size_align(old_size, ALIGN).unwrap();
157
158             ascend[2*i+1] = reallocate(p1, old.clone(), new.clone());
159             sanity_check(&*ascend);
160
161             ascend[2*i] = reallocate(p0, old.clone(), new.clone());
162             sanity_check(&*ascend);
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         let old = Layout::from_size_align(old_size, ALIGN).unwrap();
170         for i in (0..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             let new = Layout::from_size_align(new_size, ALIGN).unwrap();
174
175             ascend[2*i+1] = reallocate(p1, old.clone(), new.clone());
176             sanity_check(&*ascend);
177
178             ascend[2*i] = reallocate(p0, old.clone(), new.clone());
179             sanity_check(&*ascend);
180         }
181     }
182 }