Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does stack grow upwards or downwards

Tags:

c

stack

x86

gcc

64-bit

At some places, I have read that stacks grow from a higher address to a lower address, but when I checked it out myself, I noticed that it grows from a lower to a higher address. For example, I allocated stack for a thread at address 2aba5ab06010 and at some point found out its value to be 2aba5b7050f0, which is clearly greater than the top of the stack.

But when I check the disassembly, I can see that function prologues subtract %rsp and epilogues add it, so in that sense, shouldn't be the value of %rsp less than the top of the stack. Why these contradictory results?

Note that I am using Linux on an x86 64 bit machine and the gcc compiler.

like image 519
MetallicPriest Avatar asked Jul 06 '26 18:07

MetallicPriest


1 Answers

The thread stack can grow upwards or downwards depending on the platform. A typically way to check this is let A call B function and with

void FunctionB( int* FromFunctionA )
{
   int localStackVariableB;
   //Compare &localStackVariableB and FromFunctionA addresses
}


 void FunctionA( )
{
   int localStackVariableA;
   FunctionB( &localStackVariableA) 
}

now compare the address of localStackVariableB and FromFunctionA and determine the direction. Make sure the optimization is turned off completely.

like image 101
parapura rajkumar Avatar answered Jul 09 '26 14:07

parapura rajkumar



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!