The thread stack is created by Windows. It uses so-called protective pages for detection. A feature that is typically available for user mode code, as described in this MSDN library article . The basic idea is that the last two pages of the stack (2 x 4096 = 8192 bytes) are reserved, and any access to the processor causes a page error, which turned into a SEH exception, STATUS_GUARD_PAGE_VIOLATION.
This is intercepted by the kernel in the case of pages that belong to the thread stack. It changes the security attributes of the first of these two pages, thereby giving the thread some sort of emergency stack space to solve the problem, and then repeatedly raising the STATUS_STACK_OVERFLOW exception.
This exception, in turn, is caught by the CLR. At this point, about 3 kilobytes of stack space is left. This is not enough to run the Just-in-time compiler (JITter) to compile code that could handle an exception in your program, JITTER requires much more space than this. Thus, the CLR can do nothing but flagrant interruption of the stream. And according to the policy of .NET 2.0, which also completes the process.
Please note that this is not a problem in Java, it has a bytecode interpreter, so there is a guarantee that the user's executable code can work. Or in an unmanaged program written in languages such as C, C ++ or Delphi, the code is generated at build time. However, it is still a very difficult problem to deal with, an emergency place in the stack is blown out, so there is no scenario in which safe code execution in a thread is safe. The likelihood that the program may continue to work correctly with a thread interrupted in a completely random place and in a rather damaged state is unlikely.
If at all there was any effort when considering the issue of an event in another thread or about removing the limit in winapi (the number of protective pages is not configurable), then this is either a very well-kept secret or simply was not considered useful. I suspect that the latter, I do not know, is a fact.
Hans Passant Mar 17 '14 at 23:38 2014-03-17 23:38
source share