Leaked source code of windows server 2003
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.

142 lines
5.5 KiB

  1. page ,132
  2. title chkstk - C stack checking routine
  3. ;***
  4. ;chkstk.asm - C stack checking routine
  5. ;
  6. ; Copyright (c) 1985-2001, Microsoft Corporation. All rights reserved.
  7. ;
  8. ;Purpose:
  9. ; Provides support for automatic stack checking in C procedures
  10. ; when stack checking is enabled.
  11. ;
  12. ;Revision History:
  13. ; 04-21-87 SKS Added conditional assembly switch for STKHQQ = 0
  14. ; 07-23-87 MAG [1] Added run-time CS:IP error processing for QC
  15. ; 08-17-87 JLS [2] Remove all references to DGROUP
  16. ; 08-25-87 JLS [3] Shift include files
  17. ; 11-13-87 SKS OS/2 Reentrant version, add thread ID check
  18. ; 11-18-87 SKS Make STKHQQ an array (oops!)
  19. ; 12-14-87 SKS add .286p to allow PUSH immediate value
  20. ; 02-19-88 SKS Change minimum bottom limit to STACKSLOP, not 0
  21. ; 06-01-88 PHG Merge DLL and normal versions
  22. ; 09-21-88 WAJ initial 386 version
  23. ; 10-18-88 JCR Chkstk was trashing bx... not good on 386
  24. ; 06-06-89 JCR 386 mthread support
  25. ; 06-20-89 JCR 386: Removed _LOAD_DGROUP code
  26. ; 04-06-90 GJF Fixed the copyright.
  27. ; 06-21-90 GJF Rewritten to probe pages
  28. ; 10-15-90 GJF Restored _end and STKHQQ.
  29. ; 03-19-91 GJF Revised to preserve all registers except eax. Note
  30. ; this is _rchkstk functionality so there is no longer
  31. ; a separate _rchkstk routine.
  32. ; 08-01-91 GJF Got rid of _end and STKHQQ, except for Cruiser
  33. ; (probably not needed for Cruiser either) [_WIN32_].
  34. ; 09-27-91 JCR Merged Stevewo' changes from NT tree
  35. ; 09-06-94 CFW Nuke Cruiser.
  36. ; 12-03-94 SKS Remove include of obsolete file msdos.inc
  37. ; 12-13-94 GJF Better version from Intel (old jmp eax method of
  38. ; returning is too expensive on P6).
  39. ; 09-11-98 GJF Fixed handling of very small frames.
  40. ; 12-10-99 GB Changed both _chkstk and _alloca_probe to procedure
  41. ;
  42. ;*******************************************************************************
  43. .xlist
  44. include cruntime.inc
  45. .list
  46. ; size of a page of memory
  47. _PAGESIZE_ equ 1000h
  48. CODESEG
  49. page
  50. ;***
  51. ;_chkstk - check stack upon procedure entry
  52. ;
  53. ;Purpose:
  54. ; Provide stack checking on procedure entry. Method is to simply probe
  55. ; each page of memory required for the stack in descending order. This
  56. ; causes the necessary pages of memory to be allocated via the guard
  57. ; page scheme, if possible. In the event of failure, the OS raises the
  58. ; _XCPT_UNABLE_TO_GROW_STACK exception.
  59. ;
  60. ; NOTE: Currently, the (EAX < _PAGESIZE_) code path falls through
  61. ; to the "lastpage" label of the (EAX >= _PAGESIZE_) code path. This
  62. ; is small; a minor speed optimization would be to special case
  63. ; this up top. This would avoid the painful save/restore of
  64. ; ecx and would shorten the code path by 4-6 instructions.
  65. ;
  66. ;Entry:
  67. ; EAX = size of local frame
  68. ;
  69. ;Exit:
  70. ; ESP = new stackframe, if successful
  71. ;
  72. ;Uses:
  73. ; EAX
  74. ;
  75. ;Exceptions:
  76. ; _XCPT_GUARD_PAGE_VIOLATION - May be raised on a page probe. NEVER TRAP
  77. ; THIS!!!! It is used by the OS to grow the
  78. ; stack on demand.
  79. ; _XCPT_UNABLE_TO_GROW_STACK - The stack cannot be grown. More precisely,
  80. ; the attempt by the OS memory manager to
  81. ; allocate another guard page in response
  82. ; to a _XCPT_GUARD_PAGE_VIOLATION has
  83. ; failed.
  84. ;
  85. ;*******************************************************************************
  86. public _alloca_probe
  87. _chkstk proc
  88. _alloca_probe = _chkstk
  89. cmp eax, _PAGESIZE_ ; more than one page?
  90. jae short probesetup ; yes, go setup probe loop
  91. ; no
  92. neg eax ; compute new stack pointer in eax
  93. add eax,esp
  94. add eax,4
  95. test dword ptr [eax],eax ; probe it
  96. xchg eax,esp
  97. mov eax,dword ptr [eax]
  98. push eax
  99. ret
  100. probesetup:
  101. push ecx ; save ecx
  102. lea ecx,[esp] + 8 ; compute new stack pointer in ecx
  103. ; correct for return address and
  104. ; saved ecx
  105. probepages:
  106. sub ecx,_PAGESIZE_ ; yes, move down a page
  107. sub eax,_PAGESIZE_ ; adjust request and...
  108. test dword ptr [ecx],eax ; ...probe it
  109. cmp eax,_PAGESIZE_ ; more than one page requested?
  110. jae short probepages ; no
  111. lastpage:
  112. sub ecx,eax ; move stack down by eax
  113. mov eax,esp ; save current tos and do a...
  114. test dword ptr [ecx],eax ; ...probe in case a page was crossed
  115. mov esp,ecx ; set the new stack pointer
  116. mov ecx,dword ptr [eax] ; recover ecx
  117. mov eax,dword ptr [eax + 4] ; recover return address
  118. push eax ; prepare return address
  119. ; ...probe in case a page was crossed
  120. ret
  121. _chkstk endp
  122. end