8000 py/compile: Fix potential Py-stack overflow in try-finally with return. by dpgeorge · Pull Request #13569 · micropython/micropython · GitHub
[go: up one dir, main page]

Skip to content

py/compile: Fix potential Py-stack overflow in try-finally with return. #13569

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Conversation

dpgeorge
Copy link
Member

If a return is executed within the try block of a try-finally then the return value is stored on the top of the Python stack during the execution of the finally block. In this case the Python stack is one larger than it normally would be in the finally block.

Prior to this commit, the compiler was not taking this case into account and could have a Python stack overflow if the Python stack used by the finally block was more than that used elsewhere in the function. In such a scenario the last argument of the function would be clobbered by the top-most temporary value used in the deepest Python expression/statement.

This commit fixes that case by making sure enough Python stack is allocated to the function.

Fixes issue #13562.

If a return is executed within the try block of a try-finally then the
return value is stored on the top of the Python stack during the execution
of the finally block.  In this case the Python stack is one larger than it
normally would be in the finally block.

Prior to this commit, the compiler was not taking this case into account
and could have a Python stack overflow if the Python stack used by the
finally block was more than that used elsewhere in the function.  In such
a scenario the last argument of the function would be clobbered by the
top-most temporary value used in the deepest Python expression/statement.

This commit fixes that case by making sure enough Python stack is allocated
to the function.

Fixes issue micropython#13562.

Signed-off-by: Damien George <damien@micropython.org>
@dpgeorge dpgeorge added the py-core Relates to py/ directory in source label Jan 31, 2024
Copy link

Code size report:

   bare-arm:   +36 +0.064% 
minimal x86:   +48 +0.026% 
   unix x64:   +64 +0.008% standard
      stm32:   +48 +0.012% PYBV10
     mimxrt:   +48 +0.013% TEENSY40
        rp2:   +40 +0.012% RPI_PICO
       samd:   +48 +0.018% ADAFRUIT_ITSYBITSY_M4_EXPRESS

Copy link
codecov bot commented Jan 31, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Comparison is base (2d7fb9a) 98.36% compared to head (28b18c4) 98.36%.

Additional details and impacted files
@@           Coverage Diff           @@
##           master   #13569   +/-   ##
=======================================
  Coverage   98.36%   98.36%           
=======================================
  Files         159      159           
  Lines       21089    21093    +4     
=======================================
+ Hits        20744    20748    +4     
  Misses        345      345           

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@dpgeorge dpgeorge merged commit 28b18c4 into micropython:master Jan 31, 2024
@dpgeorge dpgeorge deleted the py-compile-fix-try-finally-stack-overflow branch January 31, 2024 03:15
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
py-core Relates to py/ directory in source
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant
0