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