1 // Copyright 2016 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.
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.
11 // ignore-wasm32-bare compiled with panic=abort by default
13 #![feature(generators, generator_trait, untagged_unions)]
15 use std::cell::{Cell, RefCell};
16 use std::ops::Generator;
20 struct InjectedFailure;
23 data: RefCell<Vec<bool>>,
28 impl panic::UnwindSafe for Allocator {}
29 impl panic::RefUnwindSafe for Allocator {}
31 impl Drop for Allocator {
33 let data = self.data.borrow();
34 if data.iter().any(|d| *d) {
35 panic!("missing free: {:?}", data);
41 fn new(failing_op: usize) -> Self {
43 failing_op: failing_op,
44 cur_ops: Cell::new(0),
45 data: RefCell::new(vec![])
48 fn alloc(&self) -> Ptr {
49 self.cur_ops.set(self.cur_ops.get() + 1);
51 if self.cur_ops.get() == self.failing_op {
52 panic!(InjectedFailure);
55 let mut data = self.data.borrow_mut();
56 let addr = data.len();
62 struct Ptr<'a>(usize, &'a Allocator);
63 impl<'a> Drop for Ptr<'a> {
65 match self.1.data.borrow_mut()[self.0] {
67 panic!("double free at index {:?}", self.0)
69 ref mut d => *d = false
72 self.1.cur_ops.set(self.1.cur_ops.get()+1);
74 if self.1.cur_ops.get() == self.1.failing_op {
75 panic!(InjectedFailure);
80 fn dynamic_init(a: &Allocator, c: bool) {
87 fn dynamic_drop(a: &Allocator, c: bool) {
96 struct TwoPtrs<'a>(Ptr<'a>, Ptr<'a>);
97 fn struct_dynamic_drop(a: &Allocator, c0: bool, c1: bool, c: bool) {
101 if (c0 && i == 0) || (c1 && i == 1) {
102 x = (a.alloc(), a.alloc(), a.alloc());
103 y = TwoPtrs(a.alloc(), a.alloc());
112 fn field_assignment(a: &Allocator, c0: bool) {
113 let mut x = (TwoPtrs(a.alloc(), a.alloc()), a.alloc());
124 fn assignment2(a: &Allocator, c0: bool, c1: bool) {
125 let mut _v = a.alloc();
126 let mut _w = a.alloc();
136 fn assignment1(a: &Allocator, c0: bool) {
137 let mut _v = a.alloc();
138 let mut _w = a.alloc();
145 #[allow(unions_with_drop_fields)]
151 fn union1(a: &Allocator) {
153 let mut u = Boxy { a: a.alloc() };
159 fn array_simple(a: &Allocator) {
160 let _x = [a.alloc(), a.alloc(), a.alloc(), a.alloc()];
163 fn vec_simple(a: &Allocator) {
164 let _x = vec![a.alloc(), a.alloc(), a.alloc(), a.alloc()];
167 fn generator(a: &Allocator, run_count: usize) {
168 assert!(run_count < 4);
177 for _ in 0..run_count {
182 fn mixed_drop_and_nondrop(a: &Allocator) {
183 // check that destructor panics handle drop
184 // and non-drop blocks in the same scope correctly.
186 // Surprisingly enough, this used to not work.
193 #[allow(unreachable_code)]
194 fn vec_unreachable(a: &Allocator) {
195 let _x = vec![a.alloc(), a.alloc(), a.alloc(), return];
198 fn run_test<F>(mut f: F)
199 where F: FnMut(&Allocator)
201 let first_alloc = Allocator::new(usize::MAX);
204 for failing_op in 1..first_alloc.cur_ops.get()+1 {
205 let alloc = Allocator::new(failing_op);
207 let f = panic::AssertUnwindSafe(&mut f);
208 let result = panic::catch_unwind(move || {
212 Ok(..) => panic!("test executed {} ops but now {}",
213 first_alloc.cur_ops.get(), alloc.cur_ops.get()),
215 if e.downcast_ref::<InjectedFailure>().is_none() {
216 panic::resume_unwind(e);
223 fn run_test_nopanic<F>(mut f: F)
224 where F: FnMut(&Allocator)
226 let first_alloc = Allocator::new(usize::MAX);
231 run_test(|a| dynamic_init(a, false));
232 run_test(|a| dynamic_init(a, true));
233 run_test(|a| dynamic_drop(a, false));
234 run_test(|a| dynamic_drop(a, true));
236 run_test(|a| assignment2(a, false, false));
237 run_test(|a| assignment2(a, false, true));
238 run_test(|a| assignment2(a, true, false));
239 run_test(|a| assignment2(a, true, true));
241 run_test(|a| assignment1(a, false));
242 run_test(|a| assignment1(a, true));
244 run_test(|a| array_simple(a));
245 run_test(|a| vec_simple(a));
246 run_test(|a| vec_unreachable(a));
248 run_test(|a| struct_dynamic_drop(a, false, false, false));
249 run_test(|a| struct_dynamic_drop(a, false, false, true));
250 run_test(|a| struct_dynamic_drop(a, false, true, false));
251 run_test(|a| struct_dynamic_drop(a, false, true, true));
252 run_test(|a| struct_dynamic_drop(a, true, false, false));
253 run_test(|a| struct_dynamic_drop(a, true, false, true));
254 run_test(|a| struct_dynamic_drop(a, true, true, false));
255 run_test(|a| struct_dynamic_drop(a, true, true, true));
257 run_test(|a| field_assignment(a, false));
258 run_test(|a| field_assignment(a, true));
260 run_test(|a| generator(a, 0));
261 run_test(|a| generator(a, 1));
262 run_test(|a| generator(a, 2));
263 run_test(|a| generator(a, 3));
265 run_test(|a| mixed_drop_and_nondrop(a));
267 run_test_nopanic(|a| union1(a));