Advertisement
Guest User

Error

a guest
Oct 4th, 2021
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.38 KB | None | 0 0
  1. page ,132
  2. title chkstk - C stack checking routine
  3. ;***
  4. ;chkstk.asm - C stack checking routine
  5. ;
  6. ; Copyright (c) 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. ;*******************************************************************************
  13.  
  14. .xlist
  15. include vcruntime.inc
  16. .list
  17.  
  18. ; size of a page of memory
  19.  
  20. _PAGESIZE_ equ 1000h
  21.  
  22.  
  23. CODESEG
  24.  
  25. page
  26. ;***
  27. ;_chkstk - check stack upon procedure entry
  28. ;
  29. ;Purpose:
  30. ; Provide stack checking on procedure entry. Method is to simply probe
  31. ; each page of memory required for the stack in descending order. This
  32. ; causes the necessary pages of memory to be allocated via the guard
  33. ; page scheme, if possible. In the event of failure, the OS raises the
  34. ; _XCPT_UNABLE_TO_GROW_STACK exception.
  35. ;
  36. ; NOTE: Currently, the (EAX < _PAGESIZE_) code path falls through
  37. ; to the "lastpage" label of the (EAX >= _PAGESIZE_) code path. This
  38. ; is small; a minor speed optimization would be to special case
  39. ; this up top. This would avoid the painful save/restore of
  40. ; ecx and would shorten the code path by 4-6 instructions.
  41. ;
  42. ;Entry:
  43. ; EAX = size of local frame
  44. ;
  45. ;Exit:
  46. ; ESP = new stackframe, if successful
  47. ;
  48. ;Uses:
  49. ; EAX
  50. ;
  51. ;Exceptions:
  52. ; _XCPT_GUARD_PAGE_VIOLATION - May be raised on a page probe. NEVER TRAP
  53. ; THIS!!!! It is used by the OS to grow the
  54. ; stack on demand.
  55. ; _XCPT_UNABLE_TO_GROW_STACK - The stack cannot be grown. More precisely,
  56. ; the attempt by the OS memory manager to
  57. ; allocate another guard page in response
  58. ; to a _XCPT_GUARD_PAGE_VIOLATION has
  59. ; failed.
  60. ;
  61. ;*******************************************************************************
  62.  
  63. public _alloca_probe
  64.  
  65. _chkstk proc
  66.  
  67. _alloca_probe = _chkstk
  68.  
  69. push ecx
  70.  
  71. ; Calculate new TOS.
  72.  
  73. lea ecx, [esp] + 8 - 4 ; TOS before entering function + size for ret value
  74. sub ecx, eax ; new TOS
  75.  
  76. ; Handle allocation size that results in wraparound.
  77. ; Wraparound will result in StackOverflow exception.
  78.  
  79. sbb eax, eax ; 0 if CF==0, ~0 if CF==1
  80. not eax ; ~0 if TOS did not wrapped around, 0 otherwise
  81. and ecx, eax ; set to 0 if wraparound
  82.  
  83. mov eax, esp ; current TOS
  84. and eax, not ( _PAGESIZE_ - 1) ; Round down to current page boundary
  85.  
  86. cs10:
  87. cmp ecx, eax ; Is new TOS
  88. bnd jb short cs20 ; in probed page?
  89. mov eax, ecx ; yes.
  90. pop ecx
  91. xchg esp, eax ; update esp
  92. mov eax, dword ptr [eax] ; get return address
  93. mov dword ptr [esp], eax ; and put it at new TOS
  94. bnd ret
  95.  
  96. ; Find next lower page and probe
  97. cs20:
  98. sub eax, _PAGESIZE_ ; decrease by PAGESIZE
  99. test dword ptr [eax],eax ; probe page.
  100. jmp short cs10
  101.  
  102. _chkstk endp
  103.  
  104. end
  105.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement