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.

115 lines
3.4 KiB

  1. /***
  2. *resetstk.c - Recover from Stack overflow.
  3. *
  4. * Copyright (c) 1989-2001, Microsoft Corporation. All rights reserved.
  5. *
  6. *Purpose:
  7. * Defines the _resetstkoflw() function.
  8. *
  9. *Revision History:
  10. * 12-10-99 GB Module Created
  11. * 04-17-01 PML Enable for Win9x, return success code (vs7#239962)
  12. * 06-04-01 PML Do nothing if guard page not missing, don't shrink
  13. * committed space (vs7#264306)
  14. *
  15. *******************************************************************************/
  16. #include <stdlib.h>
  17. #include <malloc.h>
  18. #include <windows.h>
  19. #define MIN_STACK_REQ_WIN9X 0x11000
  20. #define MIN_STACK_REQ_WINNT 0x2000
  21. /***
  22. * void _resetstkoflw(void) - Recovers from Stack Overflow
  23. *
  24. * Purpose:
  25. * Sets the guard page to its position before the stack overflow.
  26. *
  27. * Exit:
  28. * Returns nonzero on success, zero on failure
  29. *
  30. *******************************************************************************/
  31. int _resetstkoflw(void)
  32. {
  33. LPBYTE pStack, pGuard, pStackBase, pMaxGuard, pMinGuard;
  34. MEMORY_BASIC_INFORMATION mbi;
  35. SYSTEM_INFO si;
  36. DWORD PageSize;
  37. DWORD flNewProtect;
  38. DWORD flOldProtect;
  39. // Use _alloca() to get the current stack pointer
  40. pStack = _alloca(1);
  41. // Find the base of the stack.
  42. if (VirtualQuery(pStack, &mbi, sizeof mbi) == 0)
  43. return 0;
  44. pStackBase = mbi.AllocationBase;
  45. // Find the page just below where the stack pointer currently points.
  46. // This is the highest potential guard page.
  47. GetSystemInfo(&si);
  48. PageSize = si.dwPageSize;
  49. pMaxGuard = (LPBYTE) (((DWORD_PTR)pStack & ~(DWORD_PTR)(PageSize - 1))
  50. - PageSize);
  51. // If the potential guard page is too close to the start of the stack
  52. // region, abandon the reset effort for lack of space. Win9x has a
  53. // larger reserved stack requirement.
  54. pMinGuard = pStackBase + ((_osplatform == VER_PLATFORM_WIN32_WINDOWS)
  55. ? MIN_STACK_REQ_WIN9X
  56. : MIN_STACK_REQ_WINNT);
  57. if (pMaxGuard < pMinGuard)
  58. return 0;
  59. // On a non-Win9x system, do nothing if a guard page is already present,
  60. // else set up the guard page to the bottom of the committed range.
  61. // For Win9x, just set guard page below the current stack page.
  62. if (_osplatform != VER_PLATFORM_WIN32_WINDOWS) {
  63. // Find first block of committed memory in the stack region
  64. pGuard = pStackBase;
  65. do {
  66. if (VirtualQuery(pGuard, &mbi, sizeof mbi) == 0)
  67. return 0;
  68. pGuard = pGuard + mbi.RegionSize;
  69. } while ((mbi.State & MEM_COMMIT) == 0);
  70. pGuard = mbi.BaseAddress;
  71. // If first committed block is already marked as a guard page,
  72. // there is nothing that needs to be done, so return success.
  73. if (mbi.Protect & PAGE_GUARD)
  74. return 1;
  75. // Fail if the first committed block is above the highest potential
  76. // guard page. Should never happen.
  77. if (pMaxGuard < pGuard)
  78. return 0;
  79. VirtualAlloc(pGuard, PageSize, MEM_COMMIT, PAGE_READWRITE);
  80. }
  81. else {
  82. pGuard = pMaxGuard;
  83. }
  84. // Enable the new guard page.
  85. flNewProtect = _osplatform == VER_PLATFORM_WIN32_WINDOWS
  86. ? PAGE_NOACCESS
  87. : PAGE_READWRITE | PAGE_GUARD;
  88. return VirtualProtect(pGuard, PageSize, flNewProtect, &flOldProtect);
  89. }