home *** CD-ROM | disk | FTP | other *** search
/ Tricks of the Windows Gam…ming Gurus (2nd Edition) / Disc2.iso / vc98 / crt / src / platform / chkstk.asm < prev    next >
Assembly Source File  |  1998-06-17  |  4KB  |  100 lines

  1.         page    ,132
  2.         title   chkstk - C stack checking routine
  3. ;***
  4. ;chkstk.asm - C stack checking routine
  5. ;
  6. ;       Copyright (c) 1985-1997, 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 cruntime.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. labelP  _alloca_probe, PUBLIC
  64. labelP  _chkstk,       PUBLIC
  65.  
  66.         push    ecx                     ; save ecx
  67.         cmp     eax,_PAGESIZE_          ; more than one page requested?
  68.         lea     ecx,[esp] + 8           ;   compute new stack pointer in ecx
  69.                                         ;   correct for return address and
  70.                                         ;   saved ecx
  71.         jb      short lastpage          ; no
  72.  
  73. ;------------
  74.  
  75. probepages:
  76.         sub     ecx,_PAGESIZE_          ; yes, move down a page
  77.         sub     eax,_PAGESIZE_          ; adjust request and...
  78.  
  79.         test    dword ptr [ecx],eax     ; ...probe it
  80.  
  81.         cmp     eax,_PAGESIZE_          ; more than one page requested?
  82.         jae     short probepages        ; no
  83.  
  84. lastpage:
  85.         sub     ecx,eax                 ; move stack down by eax
  86.         mov     eax,esp                 ; save current tos and do a...
  87.  
  88.         test    dword ptr [ecx],eax     ; ...probe in case a page was crossed
  89.  
  90.         mov     esp,ecx                 ; set the new stack pointer
  91.  
  92.         mov     ecx,dword ptr [eax]     ; recover ecx
  93.         mov     eax,dword ptr [eax + 4] ; recover return address
  94.  
  95.         push    eax                     ; prepare return address
  96.                                         ; ...probe in case a page was crossed
  97.         ret
  98.  
  99.         end
  100.