Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it safe to use the stack below esp?

Tags:

assembly

intel

I have a simple assembly function called from a c program and I have to use a instruction (FIDIV) that needs a memory operand.

Is it safe to move the value to [esp - 2] and use it in the next instruction or is it never safe to use the stack that way?

I know there are many workarounds, and I really don't need this anymore, so now it's just curiosity.

like image 502
901 Avatar asked Dec 22 '22 11:12

901


2 Answers

Using an offset like that will definately expose the data to corruption any time any action on the thread needs to touch the stack again. This can occur during interrupts, APCs, context switches, exceptions, etc. What you'll want to do instead is to actually reserve space on the stack and save a pointer to it.

sub esp, 4        ; Allways move it 4 byte increments. x64 may need 8 bytes
mov eax, esp      ; EAX points to your 4 byte buffer
add esp, 4        ; Restore allocation.

Of course if you only need a few bytes, the push instruction is much faster

push eax
mov  eax, esp     ; EAX points to a buffer properly alligned to system 
like image 169
Paul Alexander Avatar answered Jan 04 '23 22:01

Paul Alexander


It's not safe - that part of the stack may be used for context switches, interrupts and possibly other things that your thread has little or no knowlege of or control over.

like image 42
Michael Burr Avatar answered Jan 04 '23 22:01

Michael Burr