Bug 32586 - Stack corruption in generic trampoline
Summary: Stack corruption in generic trampoline
Alias: None
Product: Runtime
Classification: Mono
Component: JIT ()
Version: unspecified
Hardware: PC Windows
: --- normal
Target Milestone: ---
Assignee: Bugzilla
Depends on:
Reported: 2015-07-29 16:49 UTC by Roland Quartin
Modified: 2015-07-29 23:04 UTC (History)
3 users (show)

Is this bug a regression?: ---
Last known good build:

test program (176 bytes, text/plain)
2015-07-29 16:50 UTC, Roland Quartin

Notice (2018-05-24): bugzilla.xamarin.com is now in read-only mode.

Please join us on Visual Studio Developer Community and in the Xamarin and Mono organizations on GitHub to continue tracking issues. Bugzilla will remain available for reference in read-only mode. We will continue to work on open Bugzilla bugs, copy them to the new locations as needed for follow-up, and add the new items under Related Links.

Our sincere thanks to everyone who has contributed on this bug tracker over the years. Thanks also for your understanding as we make these adjustments and improvements for the future.

Please create a new report on GitHub or Developer Community with your current version information, steps to reproduce, and relevant error messages or log files if you are hitting an issue that looks similar to this resolved bug and you do not yet see a matching new report.

Related Links:

Description Roland Quartin 2015-07-29 16:49:46 UTC
When running the attached test program (or anything that requires stack unwinding), Mono crashes or asserts inside mono_arch_unwind_frame because of a bogus lmf->previous_lmf value.
I've narrowed down the issue to stack corruption that happens when mono_arch_create_generic_trampoline calls mono_magic_trampoline. Right before mono_magic_trampoline is called, cur_lmf->previous_lmf is still valid, but a check at the beginning of mono_magic_trampoline shows that the value has already changed to garbage, which suggest that he compiler generated prolog for this function stomped the stack.

The issue happens in Visual Studio built AMD64 Mono (Git 2ef316dd5ceef7328c285dad7740743905f17e4d). It does not seem to happen on X86 Mono.
Comment 1 Roland Quartin 2015-07-29 16:50:10 UTC
Created attachment 12290 [details]
test program
Comment 2 Roland Quartin 2015-07-29 22:40:56 UTC
It seems that the Windows x64 ABI requires the caller to reserve a "shadow space" of at least 32 bytes on the stack for the callee, and mono_arch_create_generic_trampoline does not do that.

From https://msdn.microsoft.com/en-us/library/ms235286.aspx :
"The x64 Application Binary Interface (ABI) is a 4 register fast-call calling convention, with stack-backing for those registers
The caller is responsible for allocating space for parameters to the callee, and must always allocate sufficient space for the 4 register parameters, even if the callee doesn’t have that many parameters."
Comment 3 Zoltan Varga 2015-07-29 22:43:36 UTC
Should be fixed by 20d936551646e0ee892b418910c8a133b6262b67, could you try it out ?
Comment 4 Roland Quartin 2015-07-29 22:56:53 UTC
Thank you, it's fixed. Might need to add it to mono_arch_create_sdb_trampoline too.
Comment 5 Zoltan Varga 2015-07-29 23:04:56 UTC