Fix a serious bug in PSEH that could lead to stack corruption and was causing CSRSS to crash, when large amounts of text were output on the console.
Background: PSEH used __builtin_alloca to allocate an SEH registration frame on the stack on demand, ie only for the first try level. But there are some nasty things with __builtin_alloca: First it DOES NOT - as one might think - free the allocated memory, once the allocation "goes out of scope", like with local variables, but only on function exit. Therefore it cannot normally be used inside a loop. The trick that PSEH used to "fix" this problem, was to save the stack pointer and reset it back at the end. This is quite problematic, since the rest of the code might assume that the stack pointer is still where it was left off after the allocation. The other thing is that __builtin_alloca() can allocate the memory whenever it likes to. It can allocate everything on function entry or not allocate anything at all, when other stack variables that went out of scope have left enough space to be reused. In csrss it now happened that the allocation was done before the stack pointer was saved, so the memory could not be freed by restoring the old stack pointer value. That lead to slowly eating up the stack since the code was inside a loop.
The allocation is now replaced with a variable sized array. The variable stays in scope until the _SEH2_END and will be automaticall cleaned up by the compiler. This also makes saving and restoring the stack pointer obsolete.
Reliability++

svn path=/trunk/; revision=56442
This commit is contained in:
Timo Kreuzer 2012-04-28 20:00:09 +00:00
parent b621664add
commit 7de47192fe

View file

@ -192,7 +192,6 @@ extern void __cdecl _SEH2Return(void);
if(_SEHTopTryLevel) \
{ \
_SEH2LeaveFrame(); \
__asm__ __volatile__("mov %0, %%esp" : : "g" (_SEHStackPointer)); \
}
#define __SEH_END_SCOPE_CHAIN \
@ -219,16 +218,14 @@ extern void __cdecl _SEH2Return(void);
static const int _SEH2ScopeKind = 0; \
volatile _SEH2TryLevel_t _SEHTryLevel; \
volatile _SEH2HandleTryLevel_t _SEHHandleTryLevel; \
void * _SEHStackPointer; \
_SEH2Frame_t _SEH2Frame[_SEHTopTryLevel ? 1 : 0]; \
volatile _SEH2TryLevel_t * _SEH2TryLevelP; \
_SEH2Frame_t * const _SEH2FrameP = _SEHTopTryLevel ? \
({ __asm__ __volatile__("mov %%esp, %0" : "=g" (_SEHStackPointer)); __builtin_alloca(sizeof(_SEH2Frame_t)); }) : \
_SEHCurFrameP; \
_SEH2Frame : _SEHCurFrameP; \
\
(void)_SEH2ScopeKind; \
(void)_SEHTryLevel; \
(void)_SEHHandleTryLevel; \
(void)_SEHStackPointer; \
(void)_SEH2FrameP; \
(void)_SEH2TryLevelP; \
\