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