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.

120 lines
3.4 KiB

  1. TITLE "Runtime Stack Checking"
  2. ;++
  3. ;
  4. ; Copyright (c) 2000 Microsoft Corporation
  5. ;
  6. ; Module Name:
  7. ;
  8. ; chkstk.s
  9. ;
  10. ; Abstract:
  11. ;
  12. ; This module implements runtime stack checking.
  13. ;
  14. ; Author:
  15. ;
  16. ; David N. Cutler (davec) 20-Oct-2000
  17. ;
  18. ; Environment:
  19. ;
  20. ; Any mode.
  21. ;
  22. ;--
  23. include ksamd64.inc
  24. subttl "Check Stack"
  25. ;++
  26. ;
  27. ; ULONG64
  28. ; __chkstk (
  29. ; VOID
  30. ; )
  31. ;
  32. ; Routine Description:
  33. ;
  34. ; This function provides runtime stack checking for local allocations
  35. ; that are more than a page and for storage dynamically allocated with
  36. ; the alloca function. Stack checking consists of probing downward in
  37. ; the stack a page at a time. If the current stack commitment is exceeded,
  38. ; then the system will automatically attempts to expand the stack. If the
  39. ; attempt succeeds, then another page is committed. Otherwise, a stack
  40. ; overflow exception is raised. It is the responsibility of the caller to
  41. ; handle this exception.
  42. ;
  43. ; N.B. This routine is called using a non-standard calling sequence since
  44. ; it is typically called from within the prologue. The allocation size
  45. ; argument is in register rax and it must be preserved. Registers r10
  46. ; and r11 used by this function and are not preserved.
  47. ;
  48. ; The typical calling sequence from the prologue is:
  49. ;
  50. ; mov rax, allocation-size ; set requested stack frame size
  51. ; call __chkstk ; check stack page allocation
  52. ; sub rsp, rax ; allocate stack frame
  53. ;
  54. ; Arguments:
  55. ;
  56. ; None.
  57. ;
  58. ; Implicit Arguments:
  59. ;
  60. ; Allocation (rax) - Supplies the size of the allocation on the stack.
  61. ;
  62. ; Return Value:
  63. ;
  64. ; The allocation size is returned as the function value.
  65. ;
  66. ;--
  67. LEAF_ENTRY __chkstk, _TEXT$00
  68. ifdef NTOS_KERNEL_RUNTIME
  69. ;
  70. ; Kernel components should never allocate more than 512 bytes on the kernel
  71. ; stack.
  72. ;
  73. if DBG
  74. cmp rax, 512 ; check if less than 512 bytes
  75. jbe short cs05 ; if be, less than 512 bytes
  76. int 3 ; break into debugger
  77. endif
  78. cs05: ret ; return
  79. else
  80. lea r10, 8[rsp] ; compute requested stack address
  81. sub r10, rax ;
  82. ;
  83. ; If the new stack address is greater than the current stack limit, then the
  84. ; pages have already been allocated and nothing further needs to be done.
  85. ;
  86. mov r11, gs:[TeStackLimit] ; get current stack limit
  87. cmp r10, r11 ; check if stack within limits
  88. jae short cs20 ; if ae, stack within limits
  89. ;
  90. ; The new stack address is not within the currently allocated stack. Probe
  91. ; pages downward in the stack until all pages have been allocated or a stack
  92. ; overflow occurs in which case an exception will be raised.
  93. ;
  94. and r10w, not (PAGE_SIZE - 1) ; round down new stack address
  95. cs10: lea r11, (-PAGE_SIZE)[r11] ; get next lower page address
  96. mov byte ptr [r11], 0 ; probe stack address
  97. cmp r10, r11 ; check if end of probe range
  98. jne short cs10 ; if ne, not end of probe range
  99. cs20: ret ; return
  100. endif
  101. LEAF_END __chkstk, _TEXT$00
  102. end