OSDev.org

The Place to Start for Operating System Developers
It is currently Thu Mar 28, 2024 10:16 am

All times are UTC - 6 hours




Post new topic Reply to topic  [ 3 posts ] 
Author Message
 Post subject: How are data of different sizes written to the stack?
PostPosted: Tue Sep 29, 2020 1:56 pm 
Offline
Member
Member
User avatar

Joined: Sun Jul 21, 2019 7:34 am
Posts: 293
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?


Top
 Profile  
 
 Post subject: Re: How are data of different sizes written to the stack?
PostPosted: Tue Sep 29, 2020 2:05 pm 
Offline
Member
Member

Joined: Mon Mar 25, 2013 7:01 pm
Posts: 5099
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.


Top
 Profile  
 
 Post subject: Re: How are data of different sizes written to the stack?
PostPosted: Tue Sep 29, 2020 2:56 pm 
Offline
Member
Member
User avatar

Joined: Sun Jul 21, 2019 7:34 am
Posts: 293
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!


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 3 posts ] 

All times are UTC - 6 hours


Who is online

Users browsing this forum: No registered users and 30 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group