In article <3o************@uni-berlin.de>,
Ulrich Eckhardt <do******@knuut.de> wrote:
Cong Wang wrote: I found an interesting problem,it is that how to implement a C
function which can be called once and return twice? Just like the POSIX
function fork() or the library function longjmp().
The structure of C (and for that matter all stack based languages) is so
that you can add (a new function call context or local var) on top of the
stack or remove (return to old function call context or remove local var)
from the top of it. IOW, you need to break out this schema to achieve what
setjmp/longjmp or fork do - since your process' memory space is yours to
access in any way, there is nothing that should keep you from doing it.
No.
The call stack need not be accessible through normal operations.
There are processors in which the call stack is usually held
in-processor, and only "spilled" to memory when the register list
gets full.
There are processors in which the current return address is held
in a register, not on the stack, so memory manipulation cannot
change that return address [but might be able to change a previous
return address.]
There are processors in which the call stack is seperate
from the argument stack, and the call stack is held in a different
memory segment which is marked as not being writable -- processors
designed to twart buffer-overflow attacks.
Whether "your process' memory space is yours to
access in any way" depends on the architecture and implementation.
C promises only that your declared variables and malloc/alloc'd memory
are visible, and makes no promises about what might happen in
system calls, or library functions; it also makes no promises
about how function calls or argument passing are implemented.
--
Any sufficiently old bug becomes a feature.