From nobody@FreeBSD.org  Wed Nov 28 08:56:10 2007
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 BC06D16A41B
	for <freebsd-gnats-submit@FreeBSD.org>; Wed, 28 Nov 2007 08:56:10 +0000 (UTC)
	(envelope-from nobody@FreeBSD.org)
Received: from www.freebsd.org (www.freebsd.org [IPv6:2001:4f8:fff6::21])
	by mx1.freebsd.org (Postfix) with ESMTP id AB0B613C468
	for <freebsd-gnats-submit@FreeBSD.org>; Wed, 28 Nov 2007 08:56:10 +0000 (UTC)
	(envelope-from nobody@FreeBSD.org)
Received: from www.freebsd.org (localhost [127.0.0.1])
	by www.freebsd.org (8.14.2/8.14.2) with ESMTP id lAS8u21W048066
	for <freebsd-gnats-submit@FreeBSD.org>; Wed, 28 Nov 2007 08:56:02 GMT
	(envelope-from nobody@www.freebsd.org)
Received: (from nobody@localhost)
	by www.freebsd.org (8.14.2/8.14.1/Submit) id lAS8u2hO048064;
	Wed, 28 Nov 2007 08:56:02 GMT
	(envelope-from nobody)
Message-Id: <200711280856.lAS8u2hO048064@www.freebsd.org>
Date: Wed, 28 Nov 2007 08:56:02 GMT
From: carl shapiro <carl.shapiro@gmail.com>
To: freebsd-gnats-submit@FreeBSD.org
Subject: freebsd 7 delivers unanticipated signal for page faults to freebsd <= 6 binaries
X-Send-Pr-Version: www-3.1
X-GNATS-Notify:

>Number:         118304
>Category:       kern
>Synopsis:       freebsd 7 delivers unanticipated signal for page faults to freebsd <= 6 binaries
>Confidential:   no
>Severity:       serious
>Priority:       high
>Responsible:    kib
>State:          closed
>Quarter:        
>Keywords:       
>Date-Required:  
>Class:          sw-bug
>Submitter-Id:   current-users
>Arrival-Date:   Wed Nov 28 09:00:03 UTC 2007
>Closed-Date:    Mon Mar 05 12:00:06 UTC 2012
>Last-Modified:  Mon Mar 05 12:00:06 UTC 2012
>Originator:     carl shapiro
>Release:        7.0-BETA3
>Organization:
self
>Environment:
FreeBSD freebsd7 7.0-BETA3 FreeBSD 7.0-BETA3 #0: Fri Nov 16 22:20:33 UTC 2007     root@logan.cse.buffalo.edu:/usr/obj/usr/src/sys/GENERIC  i386

>Description:
Prior to FreeBSD 7 a SIGBUS is delivered if a page's protection is
violated.  FreeBSD 7 changed this to SIGSEGV.  Older executables which
expect SIGBUS when a page's protection is violated now unexpectedly
receive a SIGSEGV.  This breaks applications which attempt to handle
page protection binaries.  Garbage collectors that implement write
barriers through page protection (boehm gc, cmucl, etc.) are one such
application. 



>How-To-Repeat:
Start on a FreeBSD 6 system.

freebsd6 $ cc -o mmap6 mmap.c
freebsd6 $ ./mmap6
__FreeBSD_version=601000
signum=10,info->si_code=12,context=0xbfbfe880

Transfer the mmap6 binary to a FreeBSD 7 system to resume the test case.

freebsd7 $ ./mmap6
__FreeBSD_version=601000
signum=11,info->si_code=2,context=0xbfbfe9d0
freebsd7 $ cc -o mmap7 mmap.c
freebsd7 $ ./mmap7
__FreeBSD_version=700055
signum=11,info->si_code=2,context=0xbfbfe9e0

Note the different signal and different subcode.

Here is the test program.

/* mmap.c */

#include <sys/mman.h>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <osreldate.h>

void handler(int signum, siginfo_t *info, void *context) {
  fprintf(stderr, "__FreeBSD_version=%d\n", __FreeBSD_version);
  fprintf(stderr, "signum=%d,info->si_code=%d,context=%p\n", signum, info->si_code, context);
  exit(1);
}

int main(int argc, char *argv[]) {
  struct sigaction sa;
  size_t len = 0x1000;
  char *p = mmap(0, len, PROT_NONE, MAP_ANON, -1, 0);
  if (p == MAP_FAILED) {
    perror("mmap");
    exit(1);
  }
  sa.sa_sigaction = handler;
  sa.sa_flags = SA_SIGINFO;
  sigaction(SIGBUS, &sa, NULL);
  sigaction(SIGSEGV, &sa, NULL);
  *p = 0xff;
  if (munmap(p, len) == -1) {
    perror("munmap");
    exit(1);
  }
  return 0;
}

>Fix:


>Release-Note:
>Audit-Trail:
Responsible-Changed-From-To: freebsd-bugs->kib-bugs 
Responsible-Changed-By: kib 
Responsible-Changed-When: Sun Dec 2 14:22:19 UTC 2007 
Responsible-Changed-Why:  
I am working on it. 

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

From: dfilter@FreeBSD.ORG (dfilter service)
To: bug-followup@FreeBSD.org
Cc:  
Subject: Re: kern/118304: commit references a PR
Date: Tue,  4 Dec 2007 12:33:08 +0000 (UTC)

 kib         2007-12-04 12:33:03 UTC
 
   FreeBSD src repository
 
   Modified files:
     sys/i386/i386        trap.c 
     sys/amd64/amd64      trap.c 
   Log:
   Fix the ABI change of the signal delivered on the access to the page
   with insufficient protection mode.
   
   For the i386 and amd64, create the tunable, machdep.prot_fault_translation,
   with the following behaviour:
           0 = autodetect the signal to be delivered on KERN_PROTECTION_FAILURE
               from vm_fault based on the ELF OSABI note:
                   no note or __FreeBSD_version < 700004 - SIGBUS/BUS_PAGE_FAULT
                   note, and __FreeBSD_version >= 700004 - SIGSEGV/SEGV_ACCERR
           1 = always SIGBUS/BUS_PAGE_FAULT
           2 = always SIGSEGV/SEGV_ACCERR
   
   This would do mostly automatic correction of ABI breakage, with the exception
   of the untaged binaries for 7-CURRENT/RELENG_7 before the note is fixed. For
   them, sysctl would allow to run the binary with manual settings.
   
   Discussed with: portmgr (kris)
   PR:             kern/118304
   MFC after:      3 days
   
   Revision  Changes    Path
   1.323     +29 -2     src/sys/amd64/amd64/trap.c
   1.310     +29 -2     src/sys/i386/i386/trap.c
 _______________________________________________
 cvs-all@freebsd.org mailing list
 http://lists.freebsd.org/mailman/listinfo/cvs-all
 To unsubscribe, send any mail to "cvs-all-unsubscribe@freebsd.org"
 
State-Changed-From-To: open->patched 
State-Changed-By: kib 
State-Changed-When: Wed Dec 12 13:39:11 UTC 2007 
State-Changed-Why:  
Patch committed for some time. 

http://www.freebsd.org/cgi/query-pr.cgi?pr=118304 
Responsible-Changed-From-To: kib-bugs->kib 
Responsible-Changed-By: linimon 
Responsible-Changed-When: Mon Dec 17 04:25:45 UTC 2007 
Responsible-Changed-Why:  
Fix assignment. 

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

From: Kris Kennaway <kris@FreeBSD.org>
To: carl shapiro <carl.shapiro@gmail.com>
Cc: freebsd-gnats-submit@FreeBSD.org
Subject: Re: misc/118304: freebsd 7 delivers unanticipated signal for page
 faults to freebsd <= 6 binaries
Date: Sun, 17 Feb 2008 14:13:47 +0100

 carl shapiro wrote:
 
 > Prior to FreeBSD 7 a SIGBUS is delivered if a page's protection is violated.  FreeBSD 7 changed this to SIGSEGV.  Older executables which expect SIGBUS when a page's protection is violated now unexpectedly receive a SIGSEGV.  This breaks applications which attempt to handle page protection binaries.  Garbage collectors that implement write barriers through page protection (boehm gc, cmucl, etc.) are one such application. 
 
 Yes, this is expected behaviour (and I believe it is documented in the 
 release notes).  The change was made at some point in the 7.0 
 development cycle, and brings us in line with other OSes.  Unfortunately 
 this change was not entirely well thought out with respect to the impact 
 on third party code, but by the time this was realised by the wider 
 developer community it was too late to back it out because the 7.0 
 release cycle was well under way and an unknown number of vendors had 
 already made the switch to recognizing SIGSEGV instead of SIGBUS.
 
 By default FreeBSD 7.0 will send a SIGSEGV to new binaries, and SIGBUS 
 to older binaries running under compatibility on a 7.0 kernel.  Are you 
 saying that this is not working?
 
 The solution is for application developers to expect SIGSEGV when 
 compiling for FreeBSD >= 7.0 systems, and SIGBUS on < 7.0 systems.
 
 If you really need to force delivery of SIGBUS to newly compiled 
 binaries on 7.0, use the machdep.prot_fault_translation=1 sysctl.
 
 Kris
 

From: Kris Kennaway <kris@FreeBSD.org>
To: Kostik Belousov <kostikbel@gmail.com>
Cc: freebsd-gnats-submit@freebsd.org
Subject: Re: misc/118304: freebsd 7 delivers unanticipated signal for page
 faults to freebsd <= 6 binaries
Date: Sun, 17 Feb 2008 17:26:14 +0100

 Kostik Belousov wrote:
 > I believe that the issue is closed for now if we restrict it to the
 > question, formulated in the PR only. Carl, if you have an objection,
 > I would like to hear it.
 > 
 > In the wider sense, the issue is not finished, and this is what stopped
 > me from closing the PR. The behaviour of the OS there
 > - is still not compliant with standard;
 > - is different on i386/amd64 and all other arches.
 > 
 > I need to return to this in the near future, and this is why I keep the PR
 > open.
 
 OK, I missed that you had grabbed this PR.  Thanks for tracking it.
 
 Kris

From: Kostik Belousov <kostikbel@gmail.com>
To: Kris Kennaway <kris@freebsd.org>
Cc: freebsd-gnats-submit@freebsd.org
Subject: Re: misc/118304: freebsd 7 delivers unanticipated signal for page faults to freebsd <= 6 binaries
Date: Sun, 17 Feb 2008 18:06:20 +0200

 --0Qq4aj5wYbbRsDJ0
 Content-Type: text/plain; charset=us-ascii
 Content-Disposition: inline
 
 I believe that the issue is closed for now if we restrict it to the
 question, formulated in the PR only. Carl, if you have an objection,
 I would like to hear it.
 
 In the wider sense, the issue is not finished, and this is what stopped
 me from closing the PR. The behaviour of the OS there
 - is still not compliant with standard;
 - is different on i386/amd64 and all other arches.
 
 I need to return to this in the near future, and this is why I keep the PR
 open.
 
 --0Qq4aj5wYbbRsDJ0
 Content-Type: application/pgp-signature
 Content-Disposition: inline
 
 -----BEGIN PGP SIGNATURE-----
 Version: GnuPG v1.4.8 (FreeBSD)
 
 iEYEARECAAYFAke4W3sACgkQC3+MBN1Mb4gIzwCfYSpAwsrtNnoInyIjiOU4cokj
 q4UAoKbPzqowJsHABsk6iR3y+ab1QL71
 =AdKi
 -----END PGP SIGNATURE-----
 
 --0Qq4aj5wYbbRsDJ0--

From: "Carl Shapiro" <carl.shapiro@gmail.com>
To: "Kris Kennaway" <kris@freebsd.org>
Cc: freebsd-gnats-submit@freebsd.org
Subject: Re: misc/118304: freebsd 7 delivers unanticipated signal for page faults to freebsd <= 6 binaries
Date: Mon, 18 Feb 2008 16:21:47 -0800

 On Feb 17, 2008 5:13 AM, Kris Kennaway <kris@freebsd.org> wrote:
 
 Kris,
 
 > Yes, this is expected behaviour (and I believe it is documented in the
 > release notes).  The change was made at some point in the 7.0
 > development cycle, and brings us in line with other OSes.  Unfortunately
 > this change was not entirely well thought out with respect to the impact
 > on third party code, but by the time this was realised by the wider
 > developer community it was too late to back it out because the 7.0
 > release cycle was well under way and an unknown number of vendors had
 > already made the switch to recognizing SIGSEGV instead of SIGBUS.
 
 Yes, I am aware of this behavior.  I brought this matter to the
 attention of the freebsd-current list and was responded to with patch
 to 7.0.  In fact, I think you participated in the discussion.
 
 What is more serious is that the FreeBSD 7.0 behavior is still wrong.
 The FreeBSD 6.x behavior was inverted with respect to the standard:
 SIGBUS is delivered in cases where a SIGSEGV is expected and SIGSEGV
 is delivered when SIGBUS is expected.  The change David Xu made for
 7.0 delivers SIGSEGV in all cases which is not conforming.  Worse
 still, it created a third case application developers who rely on
 handling page faults to consider: the =< 6.x case, the 7.x case and
 the (hopefully forthcoming) standards compliant case.
 
 Please see the message from Bakul Shah in the discussion thread that I
 started.  It contains a more detailed summary of the incorrect
 behavior that is new as of 7.0.
 
 http://www.freebsd.org/cgi/getmsg.cgi?fetch=1306902+0+/usr/local/www/db/text/2007/freebsd-current/20071202.freebsd-current

From: Gavin Atkinson <gavin@FreeBSD.org>
To: kib@FreeBSD.org
Cc: bug-followup@FreeBSD.org
Subject: Re: kern/118304: freebsd 7 delivers unanticipated signal for page
	faults to freebsd <= 6 binaries
Date: Fri, 06 Jun 2008 13:55:09 +0100

 Hi Kostik,
 
 I believe this PR can be closed, as it was only an issue on
 HEAD/RELENG_7, and has been fixed on both of them by the commit
 referenced in the PR.  Do you agree?
 
 Thanks,
 
 Gavin

From: Gavin Atkinson <gavin@FreeBSD.org>
Date: Fri, 06 Jun 2008 18:19:20 +0100

 kib@ responded in private that he is keeping this patch open as a
 reminder that there is further work to be done on this, as FreeBSD is
 still not SUSv3-conformant in this area.


State-Changed-From-To: patched->closed 
State-Changed-By: kib 
State-Changed-When: Mon Mar 5 11:59:08 UTC 2012 
State-Changed-Why:  
The time prove that I will not work on the linux compat with this change, 
so close the PR. 

http://www.freebsd.org/cgi/query-pr.cgi?pr=118304 
>Unformatted:
