Fix stack misalignment before calls in get_win32_restore_stack thunk. #2208
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.
Why
The stack overflow handler fails to execute properly in some cases, it can happen both in game release builds and inside some user editor code / c# scripts.
How
The thunk pushed RBP (−8) and then reserved only 32 bytes of shadow space, leaving RSP misaligned by 8 bytes. This violated the Win64 ABI requirement that RSP be 16-byte aligned at every call instruction. As a result, the calls to _resetstkoflw, mono_tls_get_jit_tls, and mono_restore_context were executed with an unaligned stack.
Increase the reservation to 40 bytes (32 shadow + 8 alignment fixup) to restore correct 16-byte alignment after the initial push and ensure all call sites are ABI-compliant.
I don't know how far back, probably forever since that line of code is probably decade old at this point, I did test this personally on 2019.4.26 and 2021.3.33 but that's it.
In fact, this issue still happens on current mono (see https://github.com/mono/mono/blob/main/mono/mini/exceptions-amd64.c#L94)
I figured i'd do the pr here because i don't use mono outside of an unity environement and it would be nice to get this fixed.