Search code examples
windowsstack64-bitcalling-conventionabi

Does the Windows ABI allow me to change the stack pointer?


I know that the windows ABI have some restrictions about code generation for procedure's prologs & epilog, but I was wondering if it's fine by the OS to allocate a large heap storage and point the stack pointer to this location (and restore the RSP before the function returns)? Basically, from what I understand windows threads have a hard limit of 4GB and I wonder if it's OK to increase the stack limit that way or if there's another way to do so?

I have read the information that MSDN has about the x64 stack usage here but I could not find any information about assigning new value to the stack register


Solution

  • 3 or 4 important things must be done for any self-allocated new stack (The CreateFiber() API probably does all this itself):

    1. Update the pointers that describe max and min stack pointer values. These are in the NT_TIB structure (see winnt.h) pointed to by FS: in 32 bit and GS: in 64 bit.
    2. Make sure the new stack has the expected guard page, invalid end pages etc. just like the real one.
    3. Make sure the linked list of SEH frames are within the new stack and make the top-most SEH frame contain code to extend the SEH handler search and unwind to the previous stack.
    4. Not sure if it is necessary to somehow inform the NT kernel about user mode stack limits too, but I see no such mechanism in the Nebbett book.