How does the well-known “Process is terminated due

2020-03-13 06:12发布

问题:

A curious question:

How does the well-known "Process is terminated due to StackOverflowException" screen appear if the stack for the current process is full? Is it the runtime saving some registers for its graceful degradation or it's an internal trick that could possibly run another temp process displaying this screen?

P.S. Knowing a possible answer to this question could help someone to build his own "graceful degradation (assuming a very limited functionality of showing such a message)" mechanism from similar critical failure situations.

回答1:

This message is displayed by the CLR. You can see the code in the SSCLI20 distribution, clr/src/vm/eepolicy.cpp source code file:

void DisplayStackOverflowException()
{
    PrintToStdErrA("\n");

    PrintToStdErrA("Process is terminated due to StackOverflowException.\n");
}

Which in turn is called by the EEPolicy::HandleFatalStackOverflow() method. The only reason you can see it at all is because you are running a console mode app so output to stderr ends up on the console window. And you'll only see it if Windows Error Reporting (WER) hasn't itself terminated the app.

There is no option to intercept this exception, the CLR cannot continue running managed code since there is too little stack space left to run any managed code safely. The line of code after the DisplayStackOverflowException() call is:

    TerminateProcess(GetCurrentProcess(), COR_E_STACKOVERFLOW);