os_query_stackBottom() how does it work?

mclysenk at mtu.edu mclysenk at mtu.edu
Mon May 8 08:07:23 PDT 2006


In article <e3njs0$l4i$1 at digitaldaemon.com>, Lionello Lunesu says...
>
>mclysenk at mtu.edu wrote:
>> In article <e3mf16$1g83$1 at digitaldaemon.com>, Chad J says...
>>> /**********************************************
>>>  * Determine "bottom" of stack (actually the top on Win32 systems).
>>>  */
>>>
>>> void *os_query_stackBottom()
>>> {
>>>     asm
>>>     {
>>> 	naked			;
>>> 	mov	EAX,FS:4	;
>>> 	ret			;
>>>     }
>>> }
>>>
>> 
>> That snippet of code is pulling out the bottom of the stack from the Thread
>> Execution Block (TEB).  The TEB is stored in process' memory and is also
>> mirrored in the FS segment.  FS:0 is the top of the structured exception handler
>> chain, FS:4 is the bottom of the stack and FS:8 is the top of the stack.
>
>
>Isn't the bottom of the stack the same as ESP? If so, couldn't the code 
>be replaced by something like
>
>void* bla(){ int x; return &x; }
>
>or using alloca or something?
>
>I suppose it's the GC that needs the stack bottom/top, but why would it 
>be interested in anything before ESP?
>
>L.

I think there's some confusion with the notation that the GC uses.  Internally
the stack bottom is the higher address while the stack top is the lower address
or the stack pointer.  This is a bit confusing at first, but it makes sense
given that the stack grows downwards.  In order for the GC to mark the entire
stack used by the program, it needs the stack pointer for the start of the range
and the stack bottom for the end.

-Mik





More information about the Digitalmars-d mailing list