Skip to content
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

Fix NativeAOT unhandled exception stack trace #91415

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -708,14 +708,14 @@ private static void DispatchEx(scoped ref StackFrameIterator frameIter, ref ExIn
uint startIdx = MaxTryRegionIdx;
for (; isValid; isValid = frameIter.Next(&startIdx, &unwoundReversePInvoke))
{
prevControlPC = frameIter.ControlPC;
prevOriginalPC = frameIter.OriginalControlPC;

// For GC stackwalking, we'll happily walk across native code blocks, but for EH dispatch, we
// disallow dispatching exceptions across native code.
if (unwoundReversePInvoke)
break;

prevControlPC = frameIter.ControlPC;
prevOriginalPC = frameIter.OriginalControlPC;

DebugScanCallFrame(exInfo._passNumber, frameIter.ControlPC, frameIter.SP);

UpdateStackTrace(exceptionObj, exInfo._frameIter.FramePointer, (IntPtr)frameIter.OriginalControlPC, frameIter.SP, ref isFirstRethrowFrame, ref prevFramePtr, ref isFirstFrame, ref exInfo);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,7 @@ internal static unsafe void FailFast(string? message, Exception? exception, RhFa
string outputMessage;
if (exception != null)
{
prefix = "Unhandled Exception: ";
prefix = "Unhandled exception. ";
outputMessage = exception.ToString();
}
else
Expand Down
101 changes: 101 additions & 0 deletions src/tests/baseservices/exceptions/unhandled/unhandled.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Text;
using System.Text.RegularExpressions;

namespace TestUnhandledException
{
class Program
{
static int Main(string[] args)
{
if (args.Length != 0)
{
throw new Exception("Test");
}

List<string> lines = new List<string>();

Process testProcess = new Process();

testProcess.StartInfo.FileName = Environment.ProcessPath;
testProcess.StartInfo.Arguments = Environment.CommandLine + " throw";
testProcess.StartInfo.RedirectStandardError = true;
testProcess.ErrorDataReceived += (sender, line) =>
{
Console.WriteLine($"\"{line.Data}\"");
if (!string.IsNullOrEmpty(line.Data))
{
lines.Add(line.Data);
}
};

testProcess.Start();
testProcess.BeginErrorReadLine();
testProcess.WaitForExit();
testProcess.CancelErrorRead();

int expectedExitCode;
if (TestLibrary.Utilities.IsMonoRuntime)
{
expectedExitCode = 1;
}
else if (!OperatingSystem.IsWindows())
{
expectedExitCode = 128 + 6;
}
else if (TestLibrary.Utilities.IsNativeAot)
{
expectedExitCode = unchecked((int)0xC0000409);
}
else
{
expectedExitCode = unchecked((int)0xE0434352);
}

if (expectedExitCode != testProcess.ExitCode)
{
Console.WriteLine($"Wrong exit code 0x{testProcess.ExitCode:X8}");
return 101;
}

int exceptionStackFrameLine = 1;
if (TestLibrary.Utilities.IsMonoRuntime)
{
if (lines[0] != "Unhandled Exception:")
{
Console.WriteLine("Missing Unhandled exception header");
return 102;
}
if (lines[1] != "System.Exception: Test")
{
Console.WriteLine("Missing exception type and message");
return 103;
}

exceptionStackFrameLine = 2;
}
else
{
if (lines[0] != "Unhandled exception. System.Exception: Test")
{
Console.WriteLine("Missing Unhandled exception header");
return 102;
}

}

if (!lines[exceptionStackFrameLine].TrimStart().StartsWith("at TestUnhandledException.Program.Main"))
{
Console.WriteLine("Missing exception source frame");
return 103;
}

return 100;
}
}
}
12 changes: 12 additions & 0 deletions src/tests/baseservices/exceptions/unhandled/unhandled.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<Optimize>false</Optimize>
</PropertyGroup>
<ItemGroup>
<Compile Include="unhandled.cs" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="$(TestSourceDir)Common/CoreCLRTestLibrary/CoreCLRTestLibrary.csproj" />
</ItemGroup>
</Project>
3 changes: 3 additions & 0 deletions src/tests/issues.targets
Original file line number Diff line number Diff line change
Expand Up @@ -3230,6 +3230,9 @@
</ItemGroup>

<ItemGroup Condition="'$(TargetArchitecture)' == 'wasm'">
<ExcludeList Include = "$(XunitTestBinBase)/baseservices/exceptions/unhandled/unhandled/**">
<Issue>System.Diagnostics.Process is not supported</Issue>
</ExcludeList>
<ExcludeList Include = "$(XunitTestBinBase)/tracing/eventcounter/incrementingeventcounter/**">
<Issue>System.Threading.Thread.UnsafeStart not supported</Issue>
</ExcludeList>
Expand Down