2 // ignore-wasm32-bare compiled with panic=abort by default
4 #![feature(generators, generator_trait, untagged_unions)]
6 #![allow(unused_assignments)]
7 #![allow(unused_variables)]
9 use std::cell::{Cell, RefCell};
10 use std::mem::ManuallyDrop;
11 use std::ops::Generator;
16 struct InjectedFailure;
19 data: RefCell<Vec<bool>>,
24 impl panic::UnwindSafe for Allocator {}
25 impl panic::RefUnwindSafe for Allocator {}
27 impl Drop for Allocator {
29 let data = self.data.borrow();
30 if data.iter().any(|d| *d) {
31 panic!("missing free: {:?}", data);
37 fn new(failing_op: usize) -> Self {
39 failing_op: failing_op,
40 cur_ops: Cell::new(0),
41 data: RefCell::new(vec![])
44 fn alloc(&self) -> Ptr<'_> {
45 self.cur_ops.set(self.cur_ops.get() + 1);
47 if self.cur_ops.get() == self.failing_op {
48 panic!(InjectedFailure);
51 let mut data = self.data.borrow_mut();
52 let addr = data.len();
56 // FIXME(#47949) Any use of this indicates a bug in rustc: we should never
57 // be leaking values in the cases here.
59 // Creates a `Ptr<'_>` and checks that the allocated value is leaked if the
60 // `failing_op` is in the list of exception.
61 fn alloc_leaked(&self, exceptions: Vec<usize>) -> Ptr<'_> {
62 let ptr = self.alloc();
64 if exceptions.iter().any(|operation| *operation == self.failing_op) {
65 let mut data = self.data.borrow_mut();
72 struct Ptr<'a>(usize, &'a Allocator);
73 impl<'a> Drop for Ptr<'a> {
75 match self.1.data.borrow_mut()[self.0] {
77 panic!("double free at index {:?}", self.0)
79 ref mut d => *d = false
82 self.1.cur_ops.set(self.1.cur_ops.get()+1);
84 if self.1.cur_ops.get() == self.1.failing_op {
85 panic!(InjectedFailure);
90 fn dynamic_init(a: &Allocator, c: bool) {
97 fn dynamic_drop(a: &Allocator, c: bool) {
106 struct TwoPtrs<'a>(Ptr<'a>, Ptr<'a>);
107 fn struct_dynamic_drop(a: &Allocator, c0: bool, c1: bool, c: bool) {
111 if (c0 && i == 0) || (c1 && i == 1) {
112 x = (a.alloc(), a.alloc(), a.alloc());
113 y = TwoPtrs(a.alloc(), a.alloc());
122 fn field_assignment(a: &Allocator, c0: bool) {
123 let mut x = (TwoPtrs(a.alloc(), a.alloc()), a.alloc());
134 fn assignment2(a: &Allocator, c0: bool, c1: bool) {
135 let mut _v = a.alloc();
136 let mut _w = a.alloc();
146 fn assignment1(a: &Allocator, c0: bool) {
147 let mut _v = a.alloc();
148 let mut _w = a.alloc();
160 fn union1(a: &Allocator) {
162 let mut u = Boxy { a: ManuallyDrop::new(a.alloc()) };
163 *u.b = a.alloc(); // drops first alloc
164 drop(ManuallyDrop::into_inner(u.a));
168 fn array_simple(a: &Allocator) {
169 let _x = [a.alloc(), a.alloc(), a.alloc(), a.alloc()];
172 fn vec_simple(a: &Allocator) {
173 let _x = vec![a.alloc(), a.alloc(), a.alloc(), a.alloc()];
176 fn generator(a: &Allocator, run_count: usize) {
177 assert!(run_count < 4);
186 for _ in 0..run_count {
187 Pin::new(&mut gen).resume();
191 fn mixed_drop_and_nondrop(a: &Allocator) {
192 // check that destructor panics handle drop
193 // and non-drop blocks in the same scope correctly.
195 // Surprisingly enough, this used to not work.
202 #[allow(unreachable_code)]
203 fn vec_unreachable(a: &Allocator) {
204 let _x = vec![a.alloc(), a.alloc(), a.alloc(), return];
207 fn slice_pattern_first(a: &Allocator) {
208 let[_x, ..] = [a.alloc(), a.alloc(), a.alloc()];
211 fn slice_pattern_middle(a: &Allocator) {
212 let[_, _x, _] = [a.alloc(), a.alloc(), a.alloc()];
215 fn slice_pattern_two(a: &Allocator) {
216 let[_x, _, _y, _] = [a.alloc(), a.alloc(), a.alloc(), a.alloc()];
219 fn slice_pattern_last(a: &Allocator) {
220 let[.., _y] = [a.alloc(), a.alloc(), a.alloc(), a.alloc()];
223 fn slice_pattern_one_of(a: &Allocator, i: usize) {
224 let array = [a.alloc(), a.alloc(), a.alloc(), a.alloc()];
226 0 => { let [a, ..] = array; a }
227 1 => { let [_, a, ..] = array; a }
228 2 => { let [_, _, a, _] = array; a }
229 3 => { let [_, _, _, a] = array; a }
230 _ => panic!("unmatched"),
234 fn subslice_pattern_from_end(a: &Allocator, arg: bool) {
235 let a = [a.alloc(), a.alloc(), a.alloc()];
243 fn subslice_pattern_from_end_with_drop(a: &Allocator, arg: bool, arg2: bool) {
244 let a = [a.alloc(), a.alloc(), a.alloc(), a.alloc(), a.alloc()];
257 fn slice_pattern_reassign(a: &Allocator) {
258 let mut ar = [a.alloc(), a.alloc()];
260 ar = [a.alloc(), a.alloc()];
264 fn subslice_pattern_reassign(a: &Allocator) {
265 let mut ar = [a.alloc(), a.alloc(), a.alloc()];
267 ar = [a.alloc(), a.alloc(), a.alloc()];
268 let[_, _y @ ..] = ar;
271 fn index_field_mixed_ends(a: &Allocator) {
272 let ar = [(a.alloc(), a.alloc()), (a.alloc(), a.alloc())];
273 let[(_x, _), ..] = ar;
274 let[(_, _y), _] = ar;
275 let[_, (_, _w)] = ar;
276 let[.., (_z, _)] = ar;
279 fn subslice_mixed_min_lengths(a: &Allocator, c: i32) {
280 let ar = [(a.alloc(), a.alloc()), (a.alloc(), a.alloc())];
282 0 => { let[_x, ..] = ar; }
283 1 => { let[_x, _, ..] = ar; }
284 2 => { let[_x, _] = ar; }
285 3 => { let[(_x, _), _, ..] = ar; }
286 4 => { let[.., (_x, _)] = ar; }
287 5 => { let[.., (_x, _), _] = ar; }
288 6 => { let [_y @ ..] = ar; }
289 _ => { let [_y @ .., _] = ar; }
293 fn panic_after_return(a: &Allocator) -> Ptr<'_> {
294 // Panic in the drop of `p` or `q` can leak
295 let exceptions = vec![8, 9];
301 // FIXME (#47949) We leak values when we panic in a destructor after
302 // evaluating an expression with `rustc_mir::build::Builder::into`.
303 a.alloc_leaked(exceptions)
307 fn panic_after_return_expr(a: &Allocator) -> Ptr<'_> {
308 // Panic in the drop of `p` or `q` can leak
309 let exceptions = vec![8, 9];
316 return a.alloc_leaked(exceptions);
320 fn panic_after_init(a: &Allocator) {
321 // Panic in the drop of `r` can leak
322 let exceptions = vec![8];
329 a.alloc_leaked(exceptions)
333 fn panic_after_init_temp(a: &Allocator) {
334 // Panic in the drop of `r` can leak
335 let exceptions = vec![8];
342 a.alloc_leaked(exceptions)
346 fn panic_after_init_by_loop(a: &Allocator) {
347 // Panic in the drop of `r` can leak
348 let exceptions = vec![8];
355 break a.alloc_leaked(exceptions);
359 fn run_test<F>(mut f: F)
360 where F: FnMut(&Allocator)
362 let first_alloc = Allocator::new(usize::MAX);
365 for failing_op in 1..first_alloc.cur_ops.get()+1 {
366 let alloc = Allocator::new(failing_op);
368 let f = panic::AssertUnwindSafe(&mut f);
369 let result = panic::catch_unwind(move || {
373 Ok(..) => panic!("test executed {} ops but now {}",
374 first_alloc.cur_ops.get(), alloc.cur_ops.get()),
376 if e.downcast_ref::<InjectedFailure>().is_none() {
377 panic::resume_unwind(e);
384 fn run_test_nopanic<F>(mut f: F)
385 where F: FnMut(&Allocator)
387 let first_alloc = Allocator::new(usize::MAX);
392 run_test(|a| dynamic_init(a, false));
393 run_test(|a| dynamic_init(a, true));
394 run_test(|a| dynamic_drop(a, false));
395 run_test(|a| dynamic_drop(a, true));
397 run_test(|a| assignment2(a, false, false));
398 run_test(|a| assignment2(a, false, true));
399 run_test(|a| assignment2(a, true, false));
400 run_test(|a| assignment2(a, true, true));
402 run_test(|a| assignment1(a, false));
403 run_test(|a| assignment1(a, true));
405 run_test(|a| array_simple(a));
406 run_test(|a| vec_simple(a));
407 run_test(|a| vec_unreachable(a));
409 run_test(|a| struct_dynamic_drop(a, false, false, false));
410 run_test(|a| struct_dynamic_drop(a, false, false, true));
411 run_test(|a| struct_dynamic_drop(a, false, true, false));
412 run_test(|a| struct_dynamic_drop(a, false, true, true));
413 run_test(|a| struct_dynamic_drop(a, true, false, false));
414 run_test(|a| struct_dynamic_drop(a, true, false, true));
415 run_test(|a| struct_dynamic_drop(a, true, true, false));
416 run_test(|a| struct_dynamic_drop(a, true, true, true));
418 run_test(|a| field_assignment(a, false));
419 run_test(|a| field_assignment(a, true));
421 run_test(|a| generator(a, 0));
422 run_test(|a| generator(a, 1));
423 run_test(|a| generator(a, 2));
424 run_test(|a| generator(a, 3));
426 run_test(|a| mixed_drop_and_nondrop(a));
428 run_test(|a| slice_pattern_first(a));
429 run_test(|a| slice_pattern_middle(a));
430 run_test(|a| slice_pattern_two(a));
431 run_test(|a| slice_pattern_last(a));
432 run_test(|a| slice_pattern_one_of(a, 0));
433 run_test(|a| slice_pattern_one_of(a, 1));
434 run_test(|a| slice_pattern_one_of(a, 2));
435 run_test(|a| slice_pattern_one_of(a, 3));
437 run_test(|a| subslice_pattern_from_end(a, true));
438 run_test(|a| subslice_pattern_from_end(a, false));
439 run_test(|a| subslice_pattern_from_end_with_drop(a, true, true));
440 run_test(|a| subslice_pattern_from_end_with_drop(a, true, false));
441 run_test(|a| subslice_pattern_from_end_with_drop(a, false, true));
442 run_test(|a| subslice_pattern_from_end_with_drop(a, false, false));
443 run_test(|a| slice_pattern_reassign(a));
444 run_test(|a| subslice_pattern_reassign(a));
446 run_test(|a| index_field_mixed_ends(a));
447 run_test(|a| subslice_mixed_min_lengths(a, 0));
448 run_test(|a| subslice_mixed_min_lengths(a, 1));
449 run_test(|a| subslice_mixed_min_lengths(a, 2));
450 run_test(|a| subslice_mixed_min_lengths(a, 3));
451 run_test(|a| subslice_mixed_min_lengths(a, 4));
452 run_test(|a| subslice_mixed_min_lengths(a, 5));
453 run_test(|a| subslice_mixed_min_lengths(a, 6));
454 run_test(|a| subslice_mixed_min_lengths(a, 7));
457 panic_after_return(a);
460 panic_after_return_expr(a);
462 run_test(|a| panic_after_init(a));
463 run_test(|a| panic_after_init_temp(a));
464 run_test(|a| panic_after_init_by_loop(a));
466 run_test_nopanic(|a| union1(a));