Subject: swapcontext() does not work?
To: None <port-amd64@NetBSD.org>
From: Kouichirou Hiratsuka <hira@po6.nsk.ne.jp>
List: port-amd64
Date: 11/28/2004 23:22:12
Hello,

The following program (example in IEEE Std 1003.1, 2004 Edition)
dumps core on -current/amd64.  It works fine on i386.  Is this a
bug?


#include <stdio.h>
#include <ucontext.h>


static ucontext_t ctx[3];


static void
f1 (void)
{
    puts("start f1");
    swapcontext(&ctx[1], &ctx[2]);
    puts("finish f1");
}


static void
f2 (void)
{
    puts("start f2");
    swapcontext(&ctx[2], &ctx[1]);
    puts("finish f2");
}


int
main (void)
{
    char st1[8192];
    char st2[8192];


    getcontext(&ctx[1]);
    ctx[1].uc_stack.ss_sp = st1;
    ctx[1].uc_stack.ss_size = sizeof st1;
    ctx[1].uc_link = &ctx[0];
    makecontext(&ctx[1], f1, 0);


    getcontext(&ctx[2]);
    ctx[2].uc_stack.ss_sp = st2;
    ctx[2].uc_stack.ss_size = sizeof st2;
    ctx[2].uc_link = &ctx[1];
    makecontext(&ctx[2], f2, 0);


    swapcontext(&ctx[0], &ctx[2]);
    return 0;
}


[save as foo.c]
% make foo
cc -O2   -o foo foo.c 
% ./foo
start f2
start f1
zsh: 1181 segmentation fault (core dumped)  ./foo
% uname -a
NetBSD firefly.localdomain 2.99.10 NetBSD 2.99.10 (GENERIC) #3: Sun Nov 28 22:43:46 JST 2004  root@firefly.localdomain:/usr/src/sys/arch/amd64/compile/GENERIC amd64
%

best regards.
--
 Kouichirou Hiratsuka
   hira@po6.nsk.ne.jp