OSDev.org
https://forum.osdev.org/

How are data of different sizes written to the stack?
https://forum.osdev.org/viewtopic.php?f=13&t=37294
Page 1 of 1

Author:  mrjbom [ Tue Sep 29, 2020 1:56 pm ]
Post subject:  How are data of different sizes written to the stack?

Hi.
Previously, I encountered this problem, at first it scared me, but the problem was that the esp for the problem was calculated as follows: esp = stack_bottom + stack_size, as a result, the first addition of data to the stack overwritten the data outside it.
To solve this problem, I changed the esp calculation formula to this: esp = stack_bottom + stack_size - X

And I had a question about what number should I substitute instead of X?

Since my kernel is designed to run in protected mode on x86 processors, that means x = 4 bytes(word size), right?
And what if they try to add 6 bytes of data to the stack at the start, then 2 bytes will get out of the stack border? Or will the first 4 bytes be added first, then 4 bytes will be subtracted from the esp and 4 bytes will be added again?

Author:  Octocontrabass [ Tue Sep 29, 2020 2:05 pm ]
Post subject:  Re: How are data of different sizes written to the stack?

mrjbom wrote:
And I had a question about what number should I substitute instead of X?

Zero. If data is being overwritten at addresses higher than your stack, you have a bug somewhere else.

mrjbom wrote:
And what if they try to add 6 bytes of data to the stack at the start, then 2 bytes will get out of the stack border? Or will the first 4 bytes be added first, then 4 bytes will be subtracted from the esp and 4 bytes will be added again?

That depends on how you're adding data to the stack. The PUSH instruction subtracts 4 from ESP first, and then writes data to the address in ESP.

Author:  mrjbom [ Tue Sep 29, 2020 2:56 pm ]
Post subject:  Re: How are data of different sizes written to the stack?

Octocontrabass wrote:
mrjbom wrote:
And I had a question about what number should I substitute instead of X?

Zero. If data is being overwritten at addresses higher than your stack, you have a bug somewhere else.

mrjbom wrote:
And what if they try to add 6 bytes of data to the stack at the start, then 2 bytes will get out of the stack border? Or will the first 4 bytes be added first, then 4 bytes will be subtracted from the esp and 4 bytes will be added again?

That depends on how you're adding data to the stack. The PUSH instruction subtracts 4 from ESP first, and then writes data to the address in ESP.


You're right. I expected that because of my esp value, push overwrites some area of memory, it turns out that pop was to blame for everything, as soon as I loaded a new stack, I restored values for General-purpose registers using pop, it increased values for esp above the top of the stack and subsequent pushes overwritten memory outside the stack.

I just shouldn't try to do pop the first time I first start a thread.
Thanks!

Page 1 of 1 All times are UTC - 6 hours
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group
http://www.phpbb.com/