How are data of different sizes written to the stack?

Programming, for all ages and all languages.
Post Reply
User avatar
mrjbom
Member
Member
Posts: 301
Joined: Sun Jul 21, 2019 7:34 am

How are data of different sizes written to the stack?

Post by mrjbom »

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?
Octocontrabass
Member
Member
Posts: 5512
Joined: Mon Mar 25, 2013 7:01 pm

Re: How are data of different sizes written to the stack?

Post by Octocontrabass »

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.
User avatar
mrjbom
Member
Member
Posts: 301
Joined: Sun Jul 21, 2019 7:34 am

Re: How are data of different sizes written to the stack?

Post by mrjbom »

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!
Post Reply