Skip to content

Conversation

@tridge
Copy link
Contributor

@tridge tridge commented Jul 5, 2023

this fixes an issue where a lua library function triggers an exception after it does a math operation which changes the floating point registers on M7 MCUs (such as STM32H7). An example is math.random() which calls math_random(), which pre-calculates a double value before checking if the arguments to the call are valid. When it then checks and finds invalid values the exception longjmp does not restore the floating point registers.

An example script which reproduces this error is:
math.random(1,0)

if you run that and then do a "scripting restart" scripting dies

This PR takes a belt and braces approach to fixing the problem:

  • it adds the FPU save/restore in the LUAI_TRY() code
  • it avoids the use of FPU registers in the thread loop by changing to O0 optimisation

either change is sufficient to fix the issue

many thanks to @bugobliterator for tracking this down and @IamPete1 for finding the issue originally

this fixes an issue where a lua library function triggers an exception
after it does a math operation which changes the floating point
registers on M7 MCUs (such as STM32H7). An example is math.random()
which calls math_random(), which pre-calculates a double value before
checking if the arguments to the call are valid. When it then checks
and finds invalid values the exception longjmp does not restore the
floating point registers.
@tridge tridge force-pushed the pr-lua-exception-FPU branch from ca8cd6d to e48f2a6 Compare July 5, 2023 23:29
Copy link
Member

@IamPete1 IamPete1 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just retested on CubeOrangePlus. Still works.

I wonder if we should fix the random method as well?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

Status: 4.4.0-beta4

Development

Successfully merging this pull request may close these issues.

3 participants