Subject: lib/30585: Sigcontext misfilled in pthread mode.
To: None <lib-bug-people@netbsd.org, gnats-admin@netbsd.org,>
From: None <guilhem.lavaux@free.fr>
List: netbsd-bugs
Date: 06/23/2005 15:25:00
>Number:         30585
>Category:       lib
>Synopsis:       Sigcontext misfilled in pthread mode.
>Confidential:   no
>Severity:       serious
>Priority:       medium
>Responsible:    lib-bug-people
>State:          open
>Class:          sw-bug
>Submitter-Id:   net
>Arrival-Date:   Thu Jun 23 15:25:00 +0000 2005
>Originator:     Guilhem Lavaux
>Release:        Netbsd 2.0.2
>Organization:
>Environment:
NetBSD rochben 2.0.2 NetBSD 2.0.2 (GENERIC.MP) #0: Wed Mar 23 01:32:33 UTC 2005 jmc@faith.netbsd.org:/home/builds/ab/netbsd-2-0-2-RELEASE/sparc/200503220140Z-obj/home/builds/ab/netbsd-2-0-2-RELEASE/src/sys/arch/sparc/compile/GENERIC.MP sparc
>Description:
Kaffe needs to have access to the stack pointer and the instruction pointer at the place where a SIGSEGV/SIGBUS/SIGFPE occurs. For that we use the sigcontext structure which is given in parameter of the signal handler. However this structure seems to be misfilled in pthread mode.

>How-To-Repeat:
#include <signal.h>
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <pthread.h>

void segvHandler(int sig, int code, struct sigcontext *sc)
{
  fprintf(stderr, "caught SEGV (sp=%p, pc=%p)\n", sc->sc_sp, sc->sc_pc);
  exit(0);
}

void *specialThread(void *arg)
{
  int *i = (int *)arg;

  *i = 1;
}

int main(void)
{
  pthread_t th;
  struct sigaction newact, oldact;

  newact.sa_handler = segvHandler;
  sigemptyset(&newact.sa_mask);

  newact.sa_flags = SA_SIGINFO | SA_RESTART;
  sigaction(SIGSEGV, &newact, NULL);

  pthread_create(&th, NULL, specialThread, NULL);

  sleep(10);

  return 0;
}

You'll see that it prints:
caught SEGV (sp=0x0, pc=0x0)

on netbsd 2.0.2 / sparc

This has not been tested on other processors.

>Fix: