Re: getcontext() -> Bus error?
Re: getcontext() -> Bus error?
- Subject: Re: getcontext() -> Bus error?
- From: Terry Lambert <email@hidden>
- Date: Thu, 14 Feb 2008 10:58:30 -0800
When you call getcontext/makecontext/setcontext, you are hacking the
contents of your context. This includes register and other state,
including the stack register. This makes sense, since these functions
are typically used to implement user space threads schedulers.
Then you go and depend on the contents of a value which is either
stack relative, or which has been loaded into a volatile register, and
expect the code to "do the right thing", even though you are saying
something like "load a bunch of stack relative data off an offset of
-256 off the stack" (or whatever), or you are saying "load a bunch of
data relative the the address contained in register such-and-such I
just overwrote". Either of these can now refer to totally different
data, but the compiler didn't know about that as a barrier, so it uses
the stale contents not knowing the contents of what it is using was
changed out from under it.
Either way you look at it, this stuff is going to break under those
conditions.
Minimally, you should be using a chunk of heap or global data, the
address of which is contained in a variable that has been declared
"volatile" so that (a) it will be passed to the functions in a
volatile register, and (b) after the function returns, it doesn't try
to pop data from the stack back into the register, particularly
because your new stack doesn't have the saved data on it anyway, and
even if it did, the data it wants to pop back only exists on the old
stack, which you are no longer referencing.
I think on other systems you are simply lucking out due to internal
implementation, or your compiler is doing different optimizations by
default.
Using functions like these correctly will generally take a pretty
fundamental understanding of the machine architecture, the assembly
code that will be emitted by your compiler, and the relevant standards
governing the functions behaviour.
-- Terry
On Feb 14, 2008, at 10:17 AM, Edd Dawson <email@hidden> wrote:
Terry Lambert wrote:
This declaration is on the main program stack:
ucontext_t ctx;
it's not allocated on the heap (e.g. via malloc), and it's not
global.
Is that bad? Does the storage type of the ucontext_t matter?
This has worked for me on other systems so I'm interested to know
about any darwin-specific (or general) coding faux-pas in this area.
Thanks,
Edd
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Darwin-dev mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden