[Boost-users] [coroutine] Bus error on Mac OS X 10.5

15 views
Skip to first unread message

Nat Goodspeed

unread,
Mar 27, 2009, 10:20:56 PM3/27/09
to boost...@lists.boost.org
I've downloaded the not-yet-Boost coroutine library [1] from the Boost
Vault [2]. On a couple of Linux platforms I was able to build and run
example/banana.cpp -- though I had to patch noreturn.hpp for gcc 3.3 [3].

On an Intel "fall 2008" MacBook Pro running OS X 10.5.6, with Apple gcc
4.0.1, I had to comment out the MAP_ANONYMOUS mmap() flag [4] -- I don't
think that flag is available in Darwin. (Obviously that patch should
test for MAP_ANONYMOUS rather than simply suppressing it.)

Having done that, the 'banana' example program built -- but I get a bus
error trying to run it.

I did find an exchange on the Apple mailing list [5] in which Kevin Van
Vechten recommends #defining _XOPEN_SOURCE before #include <ucontext.h>:

>> Why the bus error? What am I doing wrong?

> This is a known issue where getcontext(3) is writing past the end of
> the ucontext_t struct when _XOPEN_SOURCE is not defined
> (rdar://problem/5578699 ). As a workaround, define _XOPEN_SOURCE
> before including ucontext.h.

Unfortunately, adding such a #define [6] does not resolve my bus error.

The article [7] in which I found ref [5] proposes a classic-C program
[8] to illustrate use of getcontext() and swapcontext(). On my Mac, that
program produces a seg fault with or without #define _XOPEN_SOURCE. So
my apologies: this may very well not be a Boost issue, or even a
pre-Boost issue.

I'm very excited to try the coroutine library, but crashing on Macs is a
show-stopper for us. Has anyone else succeeded in running it on a Mac?

[1] http://www.crystalclearsoftware.com/soc/coroutine/index.html
[2]
http://www.boostpro.com/vault/index.php?action=downloadfile&filename=boost-coroutine.tar.gz&directory=Concurrent%20Programming&
[3] attached boost-coroutine.gcc33.patch
[4] attached boost-coroutine.osx-mmap.patch
[5] http://lists.apple.com/archives/darwin-dev/2008/Jan/msg00232.html
[6] attached boost-coroutine.osx-context.patch
[7] http://hjiang.net/archives/220
[8] attached swapcontext_test.c

boost-coroutine.gcc33.patch
boost-coroutine.osx-mmap.patch
boost-coroutine.osx-context.patch
swapcontext_test.c

tom fogal

unread,
Mar 29, 2009, 2:55:43 AM3/29/09
to boost...@lists.boost.org
Nat Goodspeed <n...@lindenlab.com> writes:
> On an Intel "fall 2008" MacBook Pro running OS X 10.5.6, with Apple gcc
> 4.0.1, I had to comment out the MAP_ANONYMOUS mmap() flag [4] -- I don't
> think that flag is available in Darwin. (Obviously that patch should
> test for MAP_ANONYMOUS rather than simply suppressing it.)

Use MAP_ANON on Apple. It's been a while, but IIRC Apple doesn't
support/export the latest POSIX spec.

-tom
_______________________________________________
Boost-users mailing list
Boost...@lists.boost.org
http://lists.boost.org/mailman/listinfo.cgi/boost-users

Nat Goodspeed

unread,
Apr 8, 2009, 12:26:02 AM4/8/09
to boost...@lists.boost.org
Nat Goodspeed wrote:

> the 'banana' example program built -- but I get a bus
> error trying to run it.
>

> I did find an exchange on the Apple mailing list [1] in which Kevin Van
> Vechten recommends #defining _XOPEN_SOURCE before #include <ucontext.h>.
>
> Unfortunately, adding such a #define does not resolve my bus error.

banana.cpp now runs on my Mac! For closure, here's what I found.

There were two crash issues. One is indeed fixed by #defining
_XOPEN_SOURCE -- /if/ your #define precedes the first #include of
sys/_structs.h! This was a little tricky to resolve, since many system
headers end up pulling that in.

This only matters in boost/coroutine/detail/context_posix.hpp, so I kept
the #define there -- but test whether the system file has already been
#included. If so, you get an #error to the effect that you must #include
coroutine headers before anything that #includes <ucontext.h>.

Then I had to move some internal #includes that were forcing that #error
even when coroutine.hpp is first.

Moving boost/coroutine/detail/context_posix.hpp before most other
#includes revealed that it uses BOOST_ASSERT without #including
<boost/assert.hpp>. I've added that #include.

The other crash issue is that 8192 is way too small a default stack. On
this platform, SIGSTKSZ is 131072, or 16x the hard-coded
default_stack_size. I changed the definition of
ucontext_context_impl::default_stack_size to SIGSTKSZ.

I changed boost/coroutine/detail/posix_utility.hpp to reference MAP_ANON
rather than MAP_ANONYMOUS in the __APPLE__ case -- thanks Tom!

All these changes are in a patch file [2].

> [1] http://lists.apple.com/archives/darwin-dev/2008/Jan/msg00232.html
> [2] attached boost-coroutine.patch

boost-coroutine.patch

Nat Goodspeed

unread,
Apr 8, 2009, 1:31:36 AM4/8/09
to boost...@lists.boost.org
Nat Goodspeed wrote:

> banana.cpp now runs on my Mac! For closure, here's what I found.

I also got it to run on Debian sarge (linux32), Debian etch (linux64),
Mac OS X 10.5 and Windows XP Pro. My internal #include reordering turned
up another couple of dependencies on BOOST_ASSERT without the
corresponding #include; I've added those to the attached cumulative
patch file.

This cumulative patch file also incorporates the gcc 3.3 workaround
mentioned in an earlier mail, but omitted from the previous
boost-coroutine.patch.

Finally -- the Windows Fiber API isn't visible in <windows.h> unless you
tweak _WIN32_WINNT to a magic number. This too is included in the
attached patch file.

boost-coroutine2.patch

Nat Goodspeed

unread,
Apr 8, 2009, 11:47:38 PM4/8/09
to boost...@lists.boost.org
Nat Goodspeed wrote:

> banana.cpp now runs on my Mac!

Mine -- but not all target Macs, sigh! We have to support OS X 10.4 as
well. To my distress, 10.4 does not have swapcontext() et al.

The #else clause in boost/coroutine/detail/context_posix.hpp contains
the following comment (along with an #error):

/**
* This is just a temporary placeholder. Context swapping can
* be implemented on all most posix systems lacking *context using the
* sigaltstack+longjmp trick.
* Eventually it will be implemented, but for now just throw an error.
* Most posix systems are at least SuSv2 compliant anyway.
*/

After a couple hours of searching, I have not yet turned up either an
article or an example of the "sigaltstack + longjmp trick." Can anyone
steer me to a reference implementation? I can't just hack something
together on my own: my boss wants me to forget about coroutines in C++,
and that would be a shame indeed. If I could adapt an implementation
that already works, though, I might still pull this off.

Many thanks!

Zeljko Vrba

unread,
Apr 9, 2009, 1:39:19 AM4/9/09
to boost...@lists.boost.org
On Wed, Apr 08, 2009 at 11:47:38PM -0400, Nat Goodspeed wrote:
>
> After a couple hours of searching, I have not yet turned up either an
> article or an example of the "sigaltstack + longjmp trick." Can anyone
> steer me to a reference implementation? I can't just hack something
>
Yes, look up GNU Pth: http://www.gnu.org/software/pth/

There you will find also a link to the "Portable Multithreading" paper
which describes the trick.

You probably do not need to do very much, since GNU Pth already *does*
provide *context() equivalents, though with a slightly different interface.

Nat Goodspeed

unread,
Apr 15, 2009, 9:28:25 PM4/15/09
to boost...@lists.boost.org
Zeljko Vrba wrote:

> On Wed, Apr 08, 2009 at 11:47:38PM -0400, Nat Goodspeed wrote:

>> I have not yet turned up either an
>> article or an example of the "sigaltstack + longjmp trick." Can anyone
>> steer me to a reference implementation?
>>

> Yes, look up GNU Pth: http://www.gnu.org/software/pth/
>

> You probably do not need to do very much, since GNU Pth already *does*
> provide *context() equivalents, though with a slightly different interface.

Many thanks! I've hacked in a Pth-based implementation, using those
*context() workalikes, which appears to work on Mac OS X 10.4.

You're probably about to ask me to post the hack, since it may be
generally useful. ;-) I will, as soon as the dust settles a bit; I'm
pulling all this together against an internal deadline.

Reply all
Reply to author
Forward
0 new messages