Skip to content

Commit b5340fd

Browse files
committed
Assert that we only update pc in leaves
With one exception in `handle_err` (which does not always fully unwind the stack), add `@assert`s everywhere we update `pc` to ensure that we are only doing so in leaves. This should help catch difficult-to-debug cases where we execute frames in the wrong order.
1 parent 2ada170 commit b5340fd

File tree

3 files changed

+8
-1
lines changed

3 files changed

+8
-1
lines changed

src/commands.jl

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@ function finish_stack!(@nospecialize(recurse), frame::Frame, rootistoplevel::Boo
7474
end
7575
end
7676
pc += 1
77+
@assert is_leaf(frame)
7778
frame.pc = pc
7879
shouldbreak(frame, pc) && return BreakpointRef(frame.framecode, pc)
7980
end
@@ -410,6 +411,7 @@ function unwind_exception(frame::Frame, @nospecialize(exc))
410411
while frame !== nothing
411412
if !isempty(frame.framedata.exception_frames)
412413
# Exception caught
414+
@assert is_leaf(frame)
413415
frame.pc = frame.framedata.exception_frames[end]
414416
frame.framedata.last_exception[] = exc
415417
return frame

src/construct.jl

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,8 +48,9 @@ end
4848
end
4949

5050
function return_from(frame::Frame)
51-
recycle(frame)
51+
oldframe = frame
5252
frame = caller(frame)
53+
recycle(oldframe)
5354
frame === nothing || (frame.callee = nothing)
5455
return frame
5556
end

src/interpret.jl

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -560,13 +560,15 @@ function step_expr!(@nospecialize(recurse), frame::Frame, @nospecialize(node), i
560560
rhs = eval_rhs(recurse, frame, node)
561561
end
562562
elseif isa(node, GotoNode)
563+
@assert is_leaf(frame)
563564
return (frame.pc = node.label)
564565
elseif isa(node, GotoIfNot)
565566
arg = @lookup(frame, node.cond)
566567
if !isa(arg, Bool)
567568
throw(TypeError(nameof(frame), "if", Bool, arg))
568569
end
569570
if !arg
571+
@assert is_leaf(frame)
570572
return (frame.pc = node.dest)
571573
end
572574
elseif isa(node, ReturnNode)
@@ -596,6 +598,7 @@ function step_expr!(@nospecialize(recurse), frame::Frame, @nospecialize(node), i
596598
lhs = SSAValue(pc)
597599
do_assignment!(frame, lhs, rhs)
598600
end
601+
@assert is_leaf(frame)
599602
return (frame.pc = pc + 1)
600603
end
601604

@@ -653,6 +656,7 @@ function handle_err(@nospecialize(recurse), frame::Frame, @nospecialize(err))
653656
end
654657
data.last_exception[] = err
655658
pc = @static VERSION >= v"1.11-" ? pop!(data.exception_frames) : data.exception_frames[end] # implicit :leave after https://github.com/JuliaLang/julia/pull/52245
659+
@assert is_leaf(frame)
656660
frame.pc = pc
657661
return pc
658662
end

0 commit comments

Comments
 (0)