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?
How are data of different sizes written to the stack?
-
- Member
- Posts: 5512
- Joined: Mon Mar 25, 2013 7:01 pm
Re: How are data of different sizes written to the stack?
Zero. If data is being overwritten at addresses higher than your stack, you have a bug somewhere else.mrjbom wrote:And I had a question about what number should I substitute instead of X?
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.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?
Re: How are data of different sizes written to the stack?
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.Octocontrabass wrote:Zero. If data is being overwritten at addresses higher than your stack, you have a bug somewhere else.mrjbom wrote:And I had a question about what number should I substitute instead of X?
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.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?
I just shouldn't try to do pop the first time I first start a thread.
Thanks!