From nobody@FreeBSD.org  Tue Nov  1 07:45:05 2011
Return-Path: <nobody@FreeBSD.org>
Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2001:4f8:fff6::34])
	by hub.freebsd.org (Postfix) with ESMTP id BE06C106566B
	for <freebsd-gnats-submit@FreeBSD.org>; Tue,  1 Nov 2011 07:45:05 +0000 (UTC)
	(envelope-from nobody@FreeBSD.org)
Received: from red.freebsd.org (red.freebsd.org [IPv6:2001:4f8:fff6::22])
	by mx1.freebsd.org (Postfix) with ESMTP id A502A8FC0C
	for <freebsd-gnats-submit@FreeBSD.org>; Tue,  1 Nov 2011 07:45:05 +0000 (UTC)
Received: from red.freebsd.org (localhost [127.0.0.1])
	by red.freebsd.org (8.14.4/8.14.4) with ESMTP id pA17j5qC047971
	for <freebsd-gnats-submit@FreeBSD.org>; Tue, 1 Nov 2011 07:45:05 GMT
	(envelope-from nobody@red.freebsd.org)
Received: (from nobody@localhost)
	by red.freebsd.org (8.14.4/8.14.4/Submit) id pA17j5K2047970;
	Tue, 1 Nov 2011 07:45:05 GMT
	(envelope-from nobody)
Message-Id: <201111010745.pA17j5K2047970@red.freebsd.org>
Date: Tue, 1 Nov 2011 07:45:05 GMT
From: yamayan <yamayan@kbh.biglobe.ne.jp>
To: freebsd-gnats-submit@FreeBSD.org
Subject: longjmp and siglongjmp, stack pointer is not aligned
X-Send-Pr-Version: www-3.1
X-GNATS-Notify:

>Number:         162214
>Category:       amd64
>Synopsis:       longjmp and siglongjmp, stack pointer is not aligned
>Confidential:   no
>Severity:       serious
>Priority:       high
>Responsible:    kib
>State:          closed
>Quarter:        
>Keywords:       
>Date-Required:  
>Class:          sw-bug
>Submitter-Id:   current-users
>Arrival-Date:   Tue Nov 01 07:50:11 UTC 2011
>Closed-Date:    Fri Dec 30 18:30:27 UTC 2011
>Last-Modified:  Fri Dec 30 18:40:13 UTC 2011
>Originator:     yamayan
>Release:        10.0-CURRENT
>Organization:
>Environment:
FreeBSD yamayan 10.0-CURRENT FreeBSD 10.0-CURRENT #1 r226954M: Mon Oct 31 10:10:55 JST 2011     root@yamayan:/usr/obj/usr/src/sys/YAMAYAN  amd64

>Description:
/usr/src/lib/libc/amd64/gen/
At longjmp(setjmp.S) and siglongjmp(sigsetjmp.S),
stack pointer is not 16-byte alignment,
so to call sigprocmask makes BusError.

in /usr/src/lib/libthr/thread/thr_sig.c,
compile with clang -march=native @ corei7,
movaps is used at sigprocmask.

It is not problem.
unaligned stack pointer is problem.
>How-To-Repeat:
build and install lib/libthr with clang -march=corei7
(-march=native @ corei7 CPU)

for example, build /usr/ports/lang/perl5.14.
miniperl stops with BusError.

check corefile.
>Fix:
patch my patch.
Adjust stack pointer alignment.

Patch attached with submission follows:

Index: setjmp.S
===================================================================
--- setjmp.S	(revision 226979)
+++ setjmp.S	(working copy)
@@ -81,7 +81,9 @@
 	movq	$3,%rdi			/* SIG_SETMASK     */
 	leaq	72(%rdx),%rsi		/* (sigset_t*)set  */
 	movq	$0,%rdx			/* (sigset_t*)oset */
+	subq	$0x8,%rsp	/* adjust alignment */
 	call	PIC_PLT(CNAME(_sigprocmask))
+	addq	$0x8,%rsp
 	popq	%rsi
 	popq	%rdi			/* jmpbuf */
 	movq	%rdi,%rdx
Index: sigsetjmp.S
===================================================================
--- sigsetjmp.S	(revision 226979)
+++ sigsetjmp.S	(working copy)
@@ -90,7 +90,9 @@
 	movq	$3,%rdi			/* SIG_SETMASK     */
 	leaq	72(%rdx),%rsi		/* (sigset_t*)set  */
 	movq	$0,%rdx			/* (sigset_t*)oset */
+	subq	$0x8,%rsp	/* adjust alignment */
 	call	PIC_PLT(CNAME(_sigprocmask))
+	addq	$0x8,%rsp
 	popq	%rsi
 	popq	%rdi			/* jmpbuf */
 2:	movq	%rdi,%rdx


>Release-Note:
>Audit-Trail:
Responsible-Changed-From-To: freebsd-amd64->kib 
Responsible-Changed-By: kib 
Responsible-Changed-When: Tue Nov 1 11:48:32 UTC 2011 
Responsible-Changed-Why:  
Grab. 

http://www.freebsd.org/cgi/query-pr.cgi?pr=162214 

From: Kostik Belousov <kostikbel@gmail.com>
To: yamayan <yamayan@kbh.biglobe.ne.jp>
Cc: freebsd-gnats-submit@freebsd.org
Subject: Re: amd64/162214: longjmp and siglongjmp, stack pointer is not aligned
Date: Tue, 1 Nov 2011 13:47:55 +0200

 You are right.
 
 The longjmp variants have the stack accidentally aligned, so I added
 the comments there.
 
 In fact, new gcc on i386 requires 16-byte alignment too, so we should
 try to preserve the alignment, if the function was called with the
 aligned stack. But there, stack is again accidentally aligned for non-pic
 case, it seems.
 
 Could you, please, look at the updated patch ?
 
 diff --git a/lib/libc/amd64/gen/setjmp.S b/lib/libc/amd64/gen/setjmp.S
 index 1409f4c..47772be 100644
 --- a/lib/libc/amd64/gen/setjmp.S
 +++ b/lib/libc/amd64/gen/setjmp.S
 @@ -54,6 +54,7 @@ ENTRY(setjmp)
  	movq	$1,%rdi			/* SIG_BLOCK       */
  	movq	$0,%rsi			/* (sigset_t*)set  */
  	leaq	72(%rcx),%rdx		/* 9,10; (sigset_t*)oset */
 +	/* stack is 16-byte aligned */
  	call	PIC_PLT(CNAME(_sigprocmask))
  	popq	%rdi
  	movq	%rdi,%rcx
 @@ -81,7 +82,9 @@ ENTRY(__longjmp)
  	movq	$3,%rdi			/* SIG_SETMASK     */
  	leaq	72(%rdx),%rsi		/* (sigset_t*)set  */
  	movq	$0,%rdx			/* (sigset_t*)oset */
 +	subq	$0x8,%rsp		/* make the stack 16-byte aligned */
  	call	PIC_PLT(CNAME(_sigprocmask))
 +	addq	$0x8,%rsp
  	popq	%rsi
  	popq	%rdi			/* jmpbuf */
  	movq	%rdi,%rdx
 diff --git a/lib/libc/amd64/gen/sigsetjmp.S b/lib/libc/amd64/gen/sigsetjmp.S
 index 438d440..706b1a0 100644
 --- a/lib/libc/amd64/gen/sigsetjmp.S
 +++ b/lib/libc/amd64/gen/sigsetjmp.S
 @@ -62,6 +62,7 @@ ENTRY(sigsetjmp)
  	movq	$1,%rdi			/* SIG_BLOCK       */
  	movq	$0,%rsi			/* (sigset_t*)set  */
  	leaq	72(%rcx),%rdx		/* 9,10 (sigset_t*)oset */
 +	/* stack is 16-byte aligned */
  	call	PIC_PLT(CNAME(_sigprocmask))
  	popq	%rdi
  2:	movq	%rdi,%rcx
 @@ -90,7 +91,9 @@ ENTRY(__siglongjmp)
  	movq	$3,%rdi			/* SIG_SETMASK     */
  	leaq	72(%rdx),%rsi		/* (sigset_t*)set  */
  	movq	$0,%rdx			/* (sigset_t*)oset */
 +	subq	$0x8,%rsp		/* align the stack */
  	call	PIC_PLT(CNAME(_sigprocmask))
 +	addq	$0x8,%rsp
  	popq	%rsi
  	popq	%rdi			/* jmpbuf */
  2:	movq	%rdi,%rdx
 diff --git a/lib/libc/i386/gen/setjmp.S b/lib/libc/i386/gen/setjmp.S
 index 5d0ddc4..91c8a39 100644
 --- a/lib/libc/i386/gen/setjmp.S
 +++ b/lib/libc/i386/gen/setjmp.S
 @@ -51,12 +51,19 @@ __FBSDID("$FreeBSD$");
  ENTRY(setjmp)
  	movl	4(%esp),%ecx
  	PIC_PROLOGUE
 +#ifdef PIC
 +	subl	$12,%esp		/* make the stack is 16-byte aligned */
 +#endif
  	leal	28(%ecx), %eax
  	pushl	%eax			/* (sigset_t*)oset */
  	pushl	$0			/* (sigset_t*)set  */
  	pushl	$1			/* SIG_BLOCK       */
  	call	PIC_PLT(CNAME(_sigprocmask))
 +#ifdef PIC
 +	addl	$24,%esp
 +#else
  	addl	$12,%esp
 +#endif
  	PIC_EPILOGUE
  	movl	4(%esp),%ecx
  	movl	0(%esp),%edx
 @@ -76,12 +83,19 @@ END(setjmp)
  ENTRY(__longjmp)
  	movl	4(%esp),%edx
  	PIC_PROLOGUE
 +#ifdef PIC
 +	subl	$12,%esp		/* make the stack is 16-byte aligned */
 +#endif
  	pushl	$0			/* (sigset_t*)oset */
  	leal	28(%edx), %eax
  	pushl	%eax			/* (sigset_t*)set  */
  	pushl	$3			/* SIG_SETMASK     */
  	call	PIC_PLT(CNAME(_sigprocmask))
 +#ifdef PIC
 +	addl	$24,%esp
 +#else
  	addl	$12,%esp
 +#endif
  	PIC_EPILOGUE
  	movl	4(%esp),%edx
  	movl	8(%esp),%eax
 diff --git a/lib/libc/i386/gen/sigsetjmp.S b/lib/libc/i386/gen/sigsetjmp.S
 index 6487745..2c8f01e 100644
 --- a/lib/libc/i386/gen/sigsetjmp.S
 +++ b/lib/libc/i386/gen/sigsetjmp.S
 @@ -60,12 +60,19 @@ ENTRY(sigsetjmp)
  	testl	%eax,%eax
  	jz	2f
  	PIC_PROLOGUE
 +#ifdef PIC
 +	subl	$12,%esp		/* make the stack is 16-byte aligned */
 +#endif
  	leal	28(%ecx), %eax
  	pushl	%eax			/* (sigset_t*)oset */
  	pushl	$0			/* (sigset_t*)set  */
  	pushl	$1			/* SIG_BLOCK       */
  	call	PIC_PLT(CNAME(_sigprocmask))
 +#ifdef PIC
 +	addl	$24,%esp
 +#else
  	addl	$12,%esp
 +#endif
  	PIC_EPILOGUE
  	movl	4(%esp),%ecx
  2:	movl	0(%esp),%edx
 @@ -87,12 +94,20 @@ ENTRY(__siglongjmp)
  	cmpl	$0,44(%edx)
  	jz	2f
  	PIC_PROLOGUE
 +#ifdef PIC
 +	subl	$12,%esp		/* make the stack is 16-byte aligned */
 +#endif
  	pushl	$0			/* (sigset_t*)oset */
  	leal	28(%edx), %eax
  	pushl	%eax			/* (sigset_t*)set  */
  	pushl	$3			/* SIG_SETMASK     */
  	call	PIC_PLT(CNAME(_sigprocmask))
  	addl	$12,%esp
 +#ifdef PIC
 +	addl	$24,%esp
 +#else
 +	addl	$12,%esp
 +#endif
  	PIC_EPILOGUE
  	movl	4(%esp),%edx
  2:	movl	8(%esp),%eax

From: dfilter@FreeBSD.ORG (dfilter service)
To: bug-followup@FreeBSD.org
Cc:  
Subject: Re: amd64/162214: commit references a PR
Date: Wed,  2 Nov 2011 18:09:19 +0000 (UTC)

 Author: kib
 Date: Wed Nov  2 18:06:22 2011
 New Revision: 227023
 URL: http://svn.freebsd.org/changeset/base/227023
 
 Log:
   Make sure that stack is 16-byte aligned before calling a function,
   as it is required by amd64 ABI. Add a comment for the places were
   the stack is accidentally properly aligned already.
   
   PR:	amd64/162214
   Submitted by:	yamayan <yamayan kbh biglobe ne jp>
   MFC after:	1 week
 
 Modified:
   head/lib/libc/amd64/gen/setjmp.S
   head/lib/libc/amd64/gen/sigsetjmp.S
 
 Modified: head/lib/libc/amd64/gen/setjmp.S
 ==============================================================================
 --- head/lib/libc/amd64/gen/setjmp.S	Wed Nov  2 17:40:21 2011	(r227022)
 +++ head/lib/libc/amd64/gen/setjmp.S	Wed Nov  2 18:06:22 2011	(r227023)
 @@ -54,6 +54,7 @@ ENTRY(setjmp)
  	movq	$1,%rdi			/* SIG_BLOCK       */
  	movq	$0,%rsi			/* (sigset_t*)set  */
  	leaq	72(%rcx),%rdx		/* 9,10; (sigset_t*)oset */
 +	/* stack is 16-byte aligned */
  	call	PIC_PLT(CNAME(_sigprocmask))
  	popq	%rdi
  	movq	%rdi,%rcx
 @@ -81,7 +82,9 @@ ENTRY(__longjmp)
  	movq	$3,%rdi			/* SIG_SETMASK     */
  	leaq	72(%rdx),%rsi		/* (sigset_t*)set  */
  	movq	$0,%rdx			/* (sigset_t*)oset */
 +	subq	$0x8,%rsp		/* make the stack 16-byte aligned */
  	call	PIC_PLT(CNAME(_sigprocmask))
 +	addq	$0x8,%rsp
  	popq	%rsi
  	popq	%rdi			/* jmpbuf */
  	movq	%rdi,%rdx
 
 Modified: head/lib/libc/amd64/gen/sigsetjmp.S
 ==============================================================================
 --- head/lib/libc/amd64/gen/sigsetjmp.S	Wed Nov  2 17:40:21 2011	(r227022)
 +++ head/lib/libc/amd64/gen/sigsetjmp.S	Wed Nov  2 18:06:22 2011	(r227023)
 @@ -62,6 +62,7 @@ ENTRY(sigsetjmp)
  	movq	$1,%rdi			/* SIG_BLOCK       */
  	movq	$0,%rsi			/* (sigset_t*)set  */
  	leaq	72(%rcx),%rdx		/* 9,10 (sigset_t*)oset */
 +	/* stack is 16-byte aligned */
  	call	PIC_PLT(CNAME(_sigprocmask))
  	popq	%rdi
  2:	movq	%rdi,%rcx
 @@ -90,7 +91,9 @@ ENTRY(__siglongjmp)
  	movq	$3,%rdi			/* SIG_SETMASK     */
  	leaq	72(%rdx),%rsi		/* (sigset_t*)set  */
  	movq	$0,%rdx			/* (sigset_t*)oset */
 +	subq	$0x8,%rsp		/* make the stack 16-byte aligned */
  	call	PIC_PLT(CNAME(_sigprocmask))
 +	addq	$0x8,%rsp
  	popq	%rsi
  	popq	%rdi			/* jmpbuf */
  2:	movq	%rdi,%rdx
 _______________________________________________
 svn-src-all@freebsd.org mailing list
 http://lists.freebsd.org/mailman/listinfo/svn-src-all
 To unsubscribe, send any mail to "svn-src-all-unsubscribe@freebsd.org"
 

From: dfilter@FreeBSD.ORG (dfilter service)
To: bug-followup@FreeBSD.org
Cc:  
Subject: Re: amd64/162214: commit references a PR
Date: Fri, 30 Dec 2011 18:18:15 +0000 (UTC)

 Author: kib
 Date: Fri Dec 30 18:18:06 2011
 New Revision: 229008
 URL: http://svn.freebsd.org/changeset/base/229008
 
 Log:
   MFC r227023:
   Make sure that stack is 16-byte aligned before calling a function,
   as it is required by amd64 ABI. Add a comment for the places were
   the stack is accidentally properly aligned already.
   
   PR:	amd64/162214
 
 Modified:
   stable/9/lib/libc/amd64/gen/setjmp.S
   stable/9/lib/libc/amd64/gen/sigsetjmp.S
 Directory Properties:
   stable/9/lib/libc/   (props changed)
 
 Modified: stable/9/lib/libc/amd64/gen/setjmp.S
 ==============================================================================
 --- stable/9/lib/libc/amd64/gen/setjmp.S	Fri Dec 30 18:16:15 2011	(r229007)
 +++ stable/9/lib/libc/amd64/gen/setjmp.S	Fri Dec 30 18:18:06 2011	(r229008)
 @@ -54,6 +54,7 @@ ENTRY(setjmp)
  	movq	$1,%rdi			/* SIG_BLOCK       */
  	movq	$0,%rsi			/* (sigset_t*)set  */
  	leaq	72(%rcx),%rdx		/* 9,10; (sigset_t*)oset */
 +	/* stack is 16-byte aligned */
  	call	PIC_PLT(CNAME(_sigprocmask))
  	popq	%rdi
  	movq	%rdi,%rcx
 @@ -81,7 +82,9 @@ ENTRY(__longjmp)
  	movq	$3,%rdi			/* SIG_SETMASK     */
  	leaq	72(%rdx),%rsi		/* (sigset_t*)set  */
  	movq	$0,%rdx			/* (sigset_t*)oset */
 +	subq	$0x8,%rsp		/* make the stack 16-byte aligned */
  	call	PIC_PLT(CNAME(_sigprocmask))
 +	addq	$0x8,%rsp
  	popq	%rsi
  	popq	%rdi			/* jmpbuf */
  	movq	%rdi,%rdx
 
 Modified: stable/9/lib/libc/amd64/gen/sigsetjmp.S
 ==============================================================================
 --- stable/9/lib/libc/amd64/gen/sigsetjmp.S	Fri Dec 30 18:16:15 2011	(r229007)
 +++ stable/9/lib/libc/amd64/gen/sigsetjmp.S	Fri Dec 30 18:18:06 2011	(r229008)
 @@ -62,6 +62,7 @@ ENTRY(sigsetjmp)
  	movq	$1,%rdi			/* SIG_BLOCK       */
  	movq	$0,%rsi			/* (sigset_t*)set  */
  	leaq	72(%rcx),%rdx		/* 9,10 (sigset_t*)oset */
 +	/* stack is 16-byte aligned */
  	call	PIC_PLT(CNAME(_sigprocmask))
  	popq	%rdi
  2:	movq	%rdi,%rcx
 @@ -90,7 +91,9 @@ ENTRY(__siglongjmp)
  	movq	$3,%rdi			/* SIG_SETMASK     */
  	leaq	72(%rdx),%rsi		/* (sigset_t*)set  */
  	movq	$0,%rdx			/* (sigset_t*)oset */
 +	subq	$0x8,%rsp		/* make the stack 16-byte aligned */
  	call	PIC_PLT(CNAME(_sigprocmask))
 +	addq	$0x8,%rsp
  	popq	%rsi
  	popq	%rdi			/* jmpbuf */
  2:	movq	%rdi,%rdx
 _______________________________________________
 svn-src-all@freebsd.org mailing list
 http://lists.freebsd.org/mailman/listinfo/svn-src-all
 To unsubscribe, send any mail to "svn-src-all-unsubscribe@freebsd.org"
 
State-Changed-From-To: open->closed 
State-Changed-By: kib 
State-Changed-When: Fri Dec 30 18:30:09 UTC 2011 
State-Changed-Why:  
Merged to 8 and 9. 

http://www.freebsd.org/cgi/query-pr.cgi?pr=162214 

From: dfilter@FreeBSD.ORG (dfilter service)
To: bug-followup@FreeBSD.org
Cc:  
Subject: Re: amd64/162214: commit references a PR
Date: Fri, 30 Dec 2011 18:22:45 +0000 (UTC)

 Author: kib
 Date: Fri Dec 30 18:22:34 2011
 New Revision: 229010
 URL: http://svn.freebsd.org/changeset/base/229010
 
 Log:
   MFC r227023:
   Make sure that stack is 16-byte aligned before calling a function,
   as it is required by amd64 ABI. Add a comment for the places were
   the stack is accidentally properly aligned already.
   
   PR:	amd64/162214
 
 Modified:
   stable/8/lib/libc/amd64/gen/setjmp.S
   stable/8/lib/libc/amd64/gen/sigsetjmp.S
 Directory Properties:
   stable/8/lib/libc/   (props changed)
 
 Modified: stable/8/lib/libc/amd64/gen/setjmp.S
 ==============================================================================
 --- stable/8/lib/libc/amd64/gen/setjmp.S	Fri Dec 30 18:20:44 2011	(r229009)
 +++ stable/8/lib/libc/amd64/gen/setjmp.S	Fri Dec 30 18:22:34 2011	(r229010)
 @@ -54,6 +54,7 @@ ENTRY(setjmp)
  	movq	$1,%rdi			/* SIG_BLOCK       */
  	movq	$0,%rsi			/* (sigset_t*)set  */
  	leaq	72(%rcx),%rdx		/* 9,10; (sigset_t*)oset */
 +	/* stack is 16-byte aligned */
  	call	PIC_PLT(CNAME(_sigprocmask))
  	popq	%rdi
  	movq	%rdi,%rcx
 @@ -81,7 +82,9 @@ ENTRY(__longjmp)
  	movq	$3,%rdi			/* SIG_SETMASK     */
  	leaq	72(%rdx),%rsi		/* (sigset_t*)set  */
  	movq	$0,%rdx			/* (sigset_t*)oset */
 +	subq	$0x8,%rsp		/* make the stack 16-byte aligned */
  	call	PIC_PLT(CNAME(_sigprocmask))
 +	addq	$0x8,%rsp
  	popq	%rsi
  	popq	%rdi			/* jmpbuf */
  	movq	%rdi,%rdx
 
 Modified: stable/8/lib/libc/amd64/gen/sigsetjmp.S
 ==============================================================================
 --- stable/8/lib/libc/amd64/gen/sigsetjmp.S	Fri Dec 30 18:20:44 2011	(r229009)
 +++ stable/8/lib/libc/amd64/gen/sigsetjmp.S	Fri Dec 30 18:22:34 2011	(r229010)
 @@ -62,6 +62,7 @@ ENTRY(sigsetjmp)
  	movq	$1,%rdi			/* SIG_BLOCK       */
  	movq	$0,%rsi			/* (sigset_t*)set  */
  	leaq	72(%rcx),%rdx		/* 9,10 (sigset_t*)oset */
 +	/* stack is 16-byte aligned */
  	call	PIC_PLT(CNAME(_sigprocmask))
  	popq	%rdi
  2:	movq	%rdi,%rcx
 @@ -90,7 +91,9 @@ ENTRY(__siglongjmp)
  	movq	$3,%rdi			/* SIG_SETMASK     */
  	leaq	72(%rdx),%rsi		/* (sigset_t*)set  */
  	movq	$0,%rdx			/* (sigset_t*)oset */
 +	subq	$0x8,%rsp		/* make the stack 16-byte aligned */
  	call	PIC_PLT(CNAME(_sigprocmask))
 +	addq	$0x8,%rsp
  	popq	%rsi
  	popq	%rdi			/* jmpbuf */
  2:	movq	%rdi,%rdx
 _______________________________________________
 svn-src-all@freebsd.org mailing list
 http://lists.freebsd.org/mailman/listinfo/svn-src-all
 To unsubscribe, send any mail to "svn-src-all-unsubscribe@freebsd.org"
 
>Unformatted:
