I am working on a built-in fiber / coroutine implementation - pretty standard, a separate stack is allocated for each fiber, and for switching contexts, registers are placed on the source context stack and popped from the target stack. It works well, but now I am facing a small problem:
I need SEH to work inside the fiber (this is normal if the program terminates, or strange things begin to happen when the exception is processed until the last stack stack is used). Just save / restore FS:[0] (along with FS:[4] and FS:[8] , obviously) during the context switch and initially set FS: [0] for the newly selected fibers to 0xFFFFFFFF (so that the exception handler installed after context, the switch will be the root of the chain) almost works.
To be precise, it works on all tested non-server Windows operating systems - the problem is that Windows Server 2008 and 2008 R2 have exception integrity checking (SEHOP, SEH overwrite protection function) by default, which makes RaiseException check if the source is the handler (somewhere in ntdll.dll) is the root of the chain and immediately exits the program, as if no handlers were installed otherwise.
Thus, I ran into the problem of creating the corresponding root frame on the stack in order to keep the verification code happy. Are there any (hidden?) API functions that I can call for this, or do I need to figure out what is needed to keep RtlDispatchException and friends happy and build the corresponding _EXCEPTION_REGISTRATION record myself? I cannot just reuse the Windows provided from the creating thread because it would be at the wrong address (the SEH implementation also checks if the handler address is within the boundaries given by FS:[4] and FS:[8] , and possibly also if the address order is agreed).
Oh, and I would rather not resort to the CreateFiber WinAPI family of functions.
source share