Let’s say that — for some strange reason — the stack pointer, ESP, for some function gets decremented momentarily and then incremented again:
;; ... we're saving registers ...
push EAX
push EBX
push ECX
push EDX
add ESP, 4 ;; Whoops!
sub ESP, 4 ;; Ah, we're fine; we restored it... or are we?
Now, it’s perfectly possible for an interrupt to get triggered immediately before your sub instruction.
If I understand correctly, an interrupt will cause the CPU to push a few values onto the stack.
Does that mean your stack will now be corrupted? Or does the OS somehow (how?) use a different stack/memory to store the context of the program? Or does it depend on the privilege level of the CPU? (If so, how?)
This post seems to contradict the answers above, stating that it this is in fact safe: