Pop for loop values from stack prior to returning #2110
Merged
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
What I did
Fix an issue causing a bad jumpdest when returning from inside a for loop.
During a for loop the stack contains the iterator value and a callback pointer to the start of the loop. When a loop is completed, or exitted via
break
, each of these values is popped off. Areturn
statement inside a for loop was missing these pops, leaving unexpected values on the stack after returning.This is only problematic when the return in a for loop happens at least 2 internal calls deep, because the callback pointer for each internal function is also placed on the stack. As such, the attempt to return from the next internal function would instead use the for loop iterator value as it's destination - resulting in a bad jumpdest.
How I did it
To fix this issue, I've added a new LLL macro
exit_repeater
which adds 2POP
instructions for each active for loop prior to the return jump. This macro is added whenever a return statement is encountered within a for loop.How to verify it
Run the tests. I've added several cases to verify that the behavior is now working as expected.
Description for the changelog
Cute Animal Picture