]> git.lizzy.rs Git - rust.git/commitdiff
Check whether unwinding is allowed before popping the stack frame
authorhyd-dev <yd-huang@outlook.com>
Mon, 24 May 2021 16:08:26 +0000 (00:08 +0800)
committerhyd-dev <yd-huang@outlook.com>
Mon, 24 May 2021 19:42:25 +0000 (03:42 +0800)
compiler/rustc_mir/src/interpret/eval_context.rs

index f1ba669492f057356a3e9ee70814143e196b0b6f..bb894e5ce0bd03736113aed43cf5ff693fffed8b 100644 (file)
@@ -798,26 +798,12 @@ pub(super) fn pop_stack_frame(&mut self, unwinding: bool) -> InterpResult<'tcx>
             throw_ub_format!("unwinding past the topmost frame of the stack");
         }
 
-        let frame =
-            self.stack_mut().pop().expect("tried to pop a stack frame, but there were none");
-
-        if !unwinding {
-            // Copy the return value to the caller's stack frame.
-            if let Some(ref return_place) = frame.return_place {
-                let op = self.access_local(&frame, mir::RETURN_PLACE, None)?;
-                self.copy_op_transmute(&op, return_place)?;
-                trace!("{:?}", self.dump_place(**return_place));
-            } else {
-                throw_ub!(Unreachable);
-            }
-        }
-
-        // Now where do we jump next?
+        // Where do we jump next?
 
         // Usually we want to clean up (deallocate locals), but in a few rare cases we don't.
         // In that case, we return early. We also avoid validation in that case,
         // because this is CTFE and the final value will be thoroughly validated anyway.
-        let (cleanup, next_block) = match (frame.return_to_block, unwinding) {
+        let (cleanup, next_block) = match (self.frame().return_to_block, unwinding) {
             (StackPopCleanup::Goto { ret, .. }, false) => (true, Some(ret)),
             (StackPopCleanup::Goto { unwind, .. }, true) => (
                 true,
@@ -832,6 +818,19 @@ pub(super) fn pop_stack_frame(&mut self, unwinding: bool) -> InterpResult<'tcx>
             (StackPopCleanup::None { cleanup, .. }, _) => (cleanup, None),
         };
 
+        let frame = self.stack_mut().pop().unwrap();
+
+        if !unwinding {
+            // Copy the return value to the caller's stack frame.
+            if let Some(ref return_place) = frame.return_place {
+                let op = self.access_local(&frame, mir::RETURN_PLACE, None)?;
+                self.copy_op_transmute(&op, return_place)?;
+                trace!("{:?}", self.dump_place(**return_place));
+            } else {
+                throw_ub!(Unreachable);
+            }
+        }
+
         if !cleanup {
             assert!(self.stack().is_empty(), "only the topmost frame should ever be leaked");
             assert!(next_block.is_none(), "tried to skip cleanup when we have a next block!");