1 // Test that values are not leaked in async functions, even in the cases where:
2 // * Dropping one of the values panics while running the future.
3 // * The future is dropped at one of its suspend points.
4 // * Dropping one of the values panics while dropping the future.
8 // ignore-wasm32-bare compiled with panic=abort by default
13 cell::{Cell, RefCell},
20 task::{Context, Poll, RawWaker, RawWakerVTable, Waker},
24 struct InjectedFailure;
31 impl<T: Unpin> Future for Defer<T> {
33 fn poll(mut self: Pin<&mut Self>, cx: &mut Context) -> Poll<Self::Output> {
35 Poll::Ready(self.value.take().unwrap())
43 /// Allocator tracks the creation and destruction of `Ptr`s.
44 /// The `failing_op`-th operation will panic.
46 data: RefCell<Vec<bool>>,
51 impl panic::UnwindSafe for Allocator {}
52 impl panic::RefUnwindSafe for Allocator {}
54 impl Drop for Allocator {
56 let data = self.data.borrow();
57 if data.iter().any(|d| *d) {
58 panic!("missing free: {:?}", data);
64 fn new(failing_op: usize) -> Self {
65 Allocator { failing_op, cur_ops: Cell::new(0), data: RefCell::new(vec![]) }
67 fn alloc(&self) -> impl Future<Output = Ptr<'_>> + '_ {
68 self.fallible_operation();
70 let mut data = self.data.borrow_mut();
72 let addr = data.len();
74 Defer { ready: false, value: Some(Ptr(addr, self)) }
76 fn fallible_operation(&self) {
77 self.cur_ops.set(self.cur_ops.get() + 1);
79 if self.cur_ops.get() == self.failing_op {
80 panic!(InjectedFailure);
85 // Type that tracks whether it was dropped and can panic when it's created or
87 struct Ptr<'a>(usize, &'a Allocator);
88 impl<'a> Drop for Ptr<'a> {
90 match self.1.data.borrow_mut()[self.0] {
91 false => panic!("double free at index {:?}", self.0),
92 ref mut d => *d = false,
95 self.1.fallible_operation();
99 async fn dynamic_init(a: Rc<Allocator>, c: bool) {
102 _x = Some(a.alloc().await);
106 async fn dynamic_drop(a: Rc<Allocator>, c: bool) {
107 let x = a.alloc().await;
115 struct TwoPtrs<'a>(Ptr<'a>, Ptr<'a>);
116 async fn struct_dynamic_drop(a: Rc<Allocator>, c0: bool, c1: bool, c: bool) {
120 if (c0 && i == 0) || (c1 && i == 1) {
121 x = (a.alloc().await, a.alloc().await, a.alloc().await);
122 y = TwoPtrs(a.alloc().await, a.alloc().await);
133 async fn field_assignment(a: Rc<Allocator>, c0: bool) {
134 let mut x = (TwoPtrs(a.alloc().await, a.alloc().await), a.alloc().await);
136 x.1 = a.alloc().await;
137 x.1 = a.alloc().await;
147 async fn assignment(a: Rc<Allocator>, c0: bool, c1: bool) {
148 let mut _v = a.alloc().await;
149 let mut _w = a.alloc().await;
155 _w = a.alloc().await;
159 async fn array_simple(a: Rc<Allocator>) {
160 let _x = [a.alloc().await, a.alloc().await, a.alloc().await, a.alloc().await];
163 async fn vec_simple(a: Rc<Allocator>) {
164 let _x = vec![a.alloc().await, a.alloc().await, a.alloc().await, a.alloc().await];
167 async fn mixed_drop_and_nondrop(a: Rc<Allocator>) {
168 // check that destructor panics handle drop
169 // and non-drop blocks in the same scope correctly.
171 // Surprisingly enough, this used to not work.
178 #[allow(unreachable_code)]
179 async fn vec_unreachable(a: Rc<Allocator>) {
180 let _x = vec![a.alloc().await, a.alloc().await, a.alloc().await, return];
183 async fn slice_pattern_one_of(a: Rc<Allocator>, i: usize) {
184 let array = [a.alloc().await, a.alloc().await, a.alloc().await, a.alloc().await];
191 let [_, a, ..] = array;
195 let [_, _, a, _] = array;
199 let [_, _, _, a] = array;
202 _ => panic!("unmatched"),
207 async fn subslice_pattern_from_end_with_drop(a: Rc<Allocator>, arg: bool, arg2: bool) {
208 let arr = [a.alloc().await, a.alloc().await, a.alloc().await, a.alloc().await, a.alloc().await];
215 let [.., _x, _] = arr;
217 let [_, _y @ ..] = arr;
222 async fn subslice_pattern_reassign(a: Rc<Allocator>) {
223 let mut ar = [a.alloc().await, a.alloc().await, a.alloc().await];
225 ar = [a.alloc().await, a.alloc().await, a.alloc().await];
226 let [_, _y @ ..] = ar;
230 fn run_test<F, G>(cx: &mut Context<'_>, ref f: F)
232 F: Fn(Rc<Allocator>) -> G,
233 G: Future<Output = ()>,
236 // Run without any panics to find which operations happen after the
237 // penultimate `poll`.
238 let first_alloc = Rc::new(Allocator::new(usize::MAX));
239 let mut fut = Box::pin(f(first_alloc.clone()));
240 let mut ops_before_last_poll = 0;
241 let mut completed = false;
243 ops_before_last_poll = first_alloc.cur_ops.get();
244 if let Poll::Ready(()) = fut.as_mut().poll(cx) {
250 // Start at `ops_before_last_poll` so that we will always be able to
251 // `poll` the expected number of times.
252 for failing_op in ops_before_last_poll..first_alloc.cur_ops.get() {
253 let alloc = Rc::new(Allocator::new(failing_op + 1));
256 let result = panic::catch_unwind(panic::AssertUnwindSafe(move || {
257 let mut fut = Box::pin(f(alloc));
259 let _ = fut.as_mut().poll(cx);
264 Ok(..) => panic!("test executed more ops on first call"),
266 if e.downcast_ref::<InjectedFailure>().is_none() {
267 panic::resume_unwind(e);
279 fn clone_waker(data: *const ()) -> RawWaker {
280 RawWaker::new(data, &RawWakerVTable::new(clone_waker, drop, drop, drop))
284 let waker = unsafe { Waker::from_raw(clone_waker(ptr::null())) };
285 let context = &mut Context::from_waker(&waker);
287 run_test(context, |a| dynamic_init(a, false));
288 run_test(context, |a| dynamic_init(a, true));
289 run_test(context, |a| dynamic_drop(a, false));
290 run_test(context, |a| dynamic_drop(a, true));
292 run_test(context, |a| assignment(a, false, false));
293 run_test(context, |a| assignment(a, false, true));
294 run_test(context, |a| assignment(a, true, false));
295 run_test(context, |a| assignment(a, true, true));
297 run_test(context, |a| array_simple(a));
298 run_test(context, |a| vec_simple(a));
299 run_test(context, |a| vec_unreachable(a));
301 run_test(context, |a| struct_dynamic_drop(a, false, false, false));
302 run_test(context, |a| struct_dynamic_drop(a, false, false, true));
303 run_test(context, |a| struct_dynamic_drop(a, false, true, false));
304 run_test(context, |a| struct_dynamic_drop(a, false, true, true));
305 run_test(context, |a| struct_dynamic_drop(a, true, false, false));
306 run_test(context, |a| struct_dynamic_drop(a, true, false, true));
307 run_test(context, |a| struct_dynamic_drop(a, true, true, false));
308 run_test(context, |a| struct_dynamic_drop(a, true, true, true));
310 run_test(context, |a| field_assignment(a, false));
311 run_test(context, |a| field_assignment(a, true));
313 run_test(context, |a| mixed_drop_and_nondrop(a));
315 run_test(context, |a| slice_pattern_one_of(a, 0));
316 run_test(context, |a| slice_pattern_one_of(a, 1));
317 run_test(context, |a| slice_pattern_one_of(a, 2));
318 run_test(context, |a| slice_pattern_one_of(a, 3));
320 run_test(context, |a| subslice_pattern_from_end_with_drop(a, true, true));
321 run_test(context, |a| subslice_pattern_from_end_with_drop(a, true, false));
322 run_test(context, |a| subslice_pattern_from_end_with_drop(a, false, true));
323 run_test(context, |a| subslice_pattern_from_end_with_drop(a, false, false));
324 run_test(context, |a| subslice_pattern_reassign(a));