Subject: Re: program built with -O2 exits on receiving SIGWINCH
To: None <netbsd-users@netbsd.org>
From: Christos Zoulas <christos@astron.com>
List: netbsd-users
Date: 02/26/2006 16:59:43
In article <6245f7a30602260010lf638160o53147fd38a7d8f3@mail.gmail.com>,
Hiroshi SAKURAI <an.olive.tree@gmail.com> wrote:
>Hi,
>
>I'm using NetBSD3.0.
>
>Following program exits when it receives SIGWINCH signal
>if built with -O2.
>
>	$ gcc -Wall -O2 -g a.c
>	$ ./a.out &
>	[1] 15605
>	$ pkill -WINCH a.out
>	[1]+ Done       ./a.out
>
>If built with -O1, it does not exit when receiving SIGWINCH.
>
>	$ gcc -Wall -O1 -g a.c
>	$ ./a.out &
>	[1] 15605
>	$ pkill -WINCH a.out
>	$
>
>Am I doing something wrong?
>Here's a output of uname and gcc -v.
>
>	$ uname -a
>	NetBSD tomato.odn.ne.jp 3.0 NetBSD 3.0 (GENERIC) #0: Fri Feb 24
>00:50:03 JST 2006 
>sakurai@tomato.odn.ne.jp:/usr/home/sakurai/src/obj/sys/arch/i386/compile/GENERIC
>i386
>
>	$ gcc -v
>	Using built-in specs.
>	Configured with:
>/home/nick/work/netbsd/src/tools/gcc/../../gnu/dist/gcc/configure
>--enable-long-long --disable-multilib --enable-threads
>--disable-symvers --build=i386-unknown-netbsdelf2.0.
>--host=i386--netbsdelf --target=i386--netbsdelf
>	Thread model: posix
>	gcc version 3.3.3 (NetBSD nb3 20040520)
>
>
>Thanks.
>
>
>=========
>
>#include <stdio.h>
>#include <signal.h>
>#include <stdlib.h>
>#include <unistd.h>
>
>int a;
>
>void func( int sig, siginfo_t *info, void *context )
>{
>	a = sig;
>}
>
>void handle_winch( int sig, siginfo_t *info, void *context )
>{
>	func(sig, 0, 0);
>}
>
>int main(void)
>{
>	struct sigaction act;
>
>	sigemptyset(&act.sa_mask);
>	act.sa_handler=SIG_IGN;
>	act.sa_flags = SA_SIGINFO;
>	act.sa_sigaction= &handle_winch;
>
>	if (sigaction(SIGWINCH, &act, 0) != 0) {
>		exit (1);
>	}
>
>	for (;;) {
>		sleep(1);
>	}
>
>	return 0;

The problem is that with -O2 the optimizer changes the call to "func"
to a jump, so the stack frame is off when the signal handler returns,
and we end up calling setcontext(0).

christos