-
Notifications
You must be signed in to change notification settings - Fork 4.9k
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
[NativeAOT] correctly initalize CONTEXT before failing fast #81010
Conversation
Tagging subscribers to this area: @agocke, @MichalStrehovsky, @jkotas Issue DetailsShort ExplanationBasically the Long ExplanationConsider this program: using System;
using System.Globalization;
static class Program
{
static void FillStack(byte value)
{
Span<byte> bytes = stackalloc byte[1024 * 16];
bytes.Fill(value);
}
static void Main(string[] args)
{
if (args.Length != 0)
{
byte fillValue = byte.Parse(args[0], NumberStyles.HexNumber);
AppDomain.CurrentDomain.UnhandledException += (_,_) => FillStack(fillValue);
}
throw new Exception();
}
} When run on Windows, the last thing it does is call If you pass
That's less than ideal as it does not directly point at the faulting function. If you pass
After this PR is merged, the call stack correctly points at the faulting function:
TODO before merging:
I won't bother with x86 since it not currently supported by NativeAOT.
|
I do not think we want to grow |
68e537e
to
667caac
Compare
I did not bother getting the segment registers when running on non-Windows, as the only use of the |
In such case there is no need for a |
Co-authored-by: Vladimir Sadov <[email protected]>
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM. Thanks!
FYI, this problem reproduces with .NET 7.0.2 on Windows 11 22H2. So it should be considered for servicing to the .NET 7 branch. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM, thank you!
Test failure looks like #75244. It the "coreclr Pri0 Runtime Tests Run windows x86 checked" job that failed, so presumably that's unrelated to this NativeAOT change. |
I am not able to reproduce it. What is the program that you were able to reproduce it with? |
I made a GitHub repo with the reproduction program and a script that builds and runs: https://github.com/AustinWise/dotnet-81010 Also included in the readme is the exact versions of software used. |
Thanks for great repro! |
/backport to release/7.0 |
Started backporting to release/7.0: https://github.com/dotnet/runtime/actions/runs/4127325824 |
Short Explanation
Basically the
CONTEXT
structure is not being initalized properly before callingRaiseFailFastException
.Long Explanation
Consider this program:
On NativeAOT on Windows, the last act of a process that throws an unhanded exception is to call
RaiseFailFastException
here. The instruction pointer andCONTEXT
structure passed toRaiseFailFastException
point at the function that raised the unhanded exception. If you have a debugger like Visual Studio attached after whenRaiseFailFastException
is called, you will see a call stack pointing to the faulting function.Depending on what argument you pass to the program, you get different call stacks.
If you pass
0
you get:That's less than ideal as it does not directly point at the faulting function.
If you pass
ff
you get garbage:After this PR is merged, the call stack correctly points at the faulting function:
TODO before merging:
AMD64: captureCS
,SS
, andRFLAGS
intoPAL_LIMITED_CONTEXT
when throwing and exceptionAMD64: restoreCS
,SS
, andRFLAGS
inRhpCopyContextFromExInfo
RhpCopyContextFromExInfo
, capture theCS
andSS
segment registers from the current thread and stuff them into theCONTEXT
.