Source code of Windows XP (NT5)
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

75 lines
3.0 KiB

  1. /*++
  2. Copyright (c) 2000 Microsoft Corporation
  3. Module Name:
  4. ShimStack.h
  5. Abstract:
  6. Macros for giving APIs a temporary stack.
  7. Notes:
  8. None
  9. History:
  10. 02/26/2000 linstev Created
  11. --*/
  12. #ifndef _SHIMSTACK_H_
  13. #define _SHIMSTACK_H_
  14. #ifdef _X86_
  15. CRITICAL_SECTION g_csStack;
  16. LPVOID g_pStack;
  17. DWORD g_dwStackSize;
  18. DWORD g_dwStackCopy;
  19. // Macro to initialize memory and critical section for stack protection macros
  20. // TotalSize : the total size for the temporary stack (in DWORDS)
  21. // CopySize : the size of the current stack to copy (in DWORDS)
  22. #define INIT_STACK(TotalSize, CopySize) \
  23. InitializeCriticalSection(&g_csStack); \
  24. g_pStack = VirtualAlloc(NULL, TotalSize * 4, MEM_COMMIT, PAGE_READWRITE); \
  25. g_dwStackSize = TotalSize; \
  26. g_dwStackCopy = CopySize;
  27. // Macro to free the temporary stack and the critical section
  28. #define FREE_STACK() \
  29. VirtualFree(g_pStack, 0, MEM_RELEASE); \
  30. DeleteCriticalSection(&g_csStack);
  31. // Get a new stack by copying the old to a buffer.
  32. #define NEW_STACK() \
  33. EnterCriticalSection(&g_csStack); \
  34. DWORD dwTempESP; \
  35. DWORD dwDiff = (g_dwStackSize - g_dwStackCopy) * 4; \
  36. __asm { mov dwTempESP,esp } \
  37. __asm { push ecx } \
  38. __asm { push esi } \
  39. __asm { push edi } \
  40. __asm { mov esi,dwTempESP } \
  41. __asm { mov edi,g_pStack } \
  42. __asm { add edi,dwDiff } \
  43. __asm { mov ecx,g_dwStackCopy } \
  44. __asm { cld } \
  45. __asm { rep movsd } \
  46. __asm { pop esi } \
  47. __asm { pop edi } \
  48. __asm { pop ecx } \
  49. __asm { mov esp,g_pStack } \
  50. __asm { add esp,dwDiff }
  51. // Revert to the old stack
  52. #define OLD_STACK() \
  53. __asm { mov esp,dwTempESP } \
  54. LeaveCriticalSection(&g_csStack);
  55. #endif // _X86_
  56. #endif // _SHIMSTACK_H_