ucontext Broken on OS X 10.5 Leopard

Posted by rue, Mon Sep 08 22:20:00 UTC 2008

Apparently due to an issue between headers and the libc implementation on Leopard, ucontext_t are not allocated enough space which will cause bus errors or segmentation faults, typically when returning from the function to which the execution has setcontext() -ed to a context previously grabbed using getcontext() . This affects both x86 and PPC, and a bug has been filed although not addressed yet.

The workaround is to manually size the types correctly. So if you have, say, a struct like this:

#include <ucontext.h>
struct  thready {
  :
  ucontext_t  some_context;
  ucontext_t  some_other_context;
  :
};

The fixed version inserts the padding:

#include <ucontext.h>

#define HAS_UCONTEXT      // Or something

struct thready {
  :
  #if defined(__APPLE__) && defined(HAS_UCONTEXT)
    ucontext_t some_context;
    _STRUCT_MCONTEXT  __some_context_mc;
    ucontext_t some_other_context;
    _STRUCT_MCONTEXT __some_other_context_mc;
  #else
    ucontext_t some_context;
    ucontext_t some_other_context;
  #endif
};

Naturally, you can also use the conditional separately for each ucontext_t if it is easier or more logical in your case and/or refine the conditional itself.

I had a terrible time trying to search for information, and most stuff I found seemed to indicate PPC issues only. In the interest of hopefully making the search shorter for someone else, I will gratuitously list the terms I was looking for: ucontext, ucontext_t, getcontext, setcontext, makecontext, swapcontext, segfault, sigbus, bus error, stack corruption, returning from, os x, 10.5, leopard, x86, ppc.

Hopefully they will get it fixed soon!

Filed Under: mac rubinius | Tags:

Comments

  1. redpig 10.06.08 / 15PM

    define _XOPEN_SOURCE

    prior to including ucontext.h should make it add the neededSTRUCT_MCONTEXT declaration in the ucontext_t struct (with the 10.5.2/gcc-4.0.1 build 5484 that I’m using.)

    cheers!

Have your say

A name is required. You may use HTML in your comments.