From douglas@slowhand.douglasthrift.net  Fri Aug 15 01:40:18 2008
Return-Path: <douglas@slowhand.douglasthrift.net>
Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2001:4f8:fff6::34])
	by hub.freebsd.org (Postfix) with ESMTP id 8878E1065671
	for <FreeBSD-gnats-submit@freebsd.org>; Fri, 15 Aug 2008 01:40:18 +0000 (UTC)
	(envelope-from douglas@slowhand.douglasthrift.net)
Received: from slowhand.douglasthrift.net (slowhand.douglasthrift.net [69.55.236.40])
	by mx1.freebsd.org (Postfix) with ESMTP id 425BD8FC08
	for <FreeBSD-gnats-submit@freebsd.org>; Fri, 15 Aug 2008 01:40:17 +0000 (UTC)
	(envelope-from douglas@slowhand.douglasthrift.net)
Received: from slowhand.douglasthrift.net (slowhand.douglasthrift.net [69.55.236.40])
	by slowhand.douglasthrift.net (8.14.3/8.14.3) with ESMTP id m7F1eH3a008467
	(version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-SHA bits=256 verify=NOT)
	for <FreeBSD-gnats-submit@freebsd.org>; Thu, 14 Aug 2008 18:40:17 -0700 (PDT)
	(envelope-from douglas@slowhand.douglasthrift.net)
Received: (from douglas@localhost)
	by slowhand.douglasthrift.net (8.14.3/8.14.3/Submit) id m7F1eH8p008466;
	Thu, 14 Aug 2008 18:40:17 -0700 (PDT)
	(envelope-from douglas)
Message-Id: <200808150140.m7F1eH8p008466@slowhand.douglasthrift.net>
Date: Thu, 14 Aug 2008 18:40:17 -0700 (PDT)
From: Douglas William Thrift <douglas@douglasthrift.net>
Reply-To: Douglas William Thrift <douglas@douglasthrift.net>
To: FreeBSD-gnats-submit@freebsd.org
Cc:
Subject: [amd64] __builtin_frame_address does not return 0 when reaching the top of the stack
X-Send-Pr-Version: 3.113
X-GNATS-Notify:

>Number:         126543
>Category:       amd64
>Synopsis:       [amd64] __builtin_frame_address does not return 0 when reaching the top of the stack
>Confidential:   no
>Severity:       non-critical
>Priority:       medium
>Responsible:    kib
>State:          closed
>Quarter:        
>Keywords:       
>Date-Required:  
>Class:          sw-bug
>Submitter-Id:   current-users
>Arrival-Date:   Fri Aug 15 01:50:03 UTC 2008
>Closed-Date:    Fri Aug 29 09:03:25 UTC 2008
>Last-Modified:  Fri Aug 29 09:03:25 UTC 2008
>Originator:     Douglas William Thrift
>Release:        FreeBSD 7.0-STABLE-jc2 amd64
>Organization:
>Environment:
System: FreeBSD slowhand.douglasthrift.net 7.0-STABLE-jc2 FreeBSD 7.0-STABLE-jc2 #0: Thu Jun 26 21:33:03 PDT 2008 user@jail8.johncompanies.com:/usr/obj/usr/src/sys/jail8 amd64
>Description:
On the amd64 architecture the gcc builtin function __builtin_frame_address does not return 0 when it is called for the level that should be the top of the stack. Instead it seems to continue returning addresses and then causes a bus error.

GCC documentation: http://gcc.gnu.org/onlinedocs/gcc-4.2.4/gcc/Return-Address.html

I've seen this occur on this system and also on two systems running FreeBSD 7.0-RELEASE-p3.

This causes devel/libexecinfo to be completely useless and crashy.
>How-To-Repeat:
Compile the following C program:

#include <stdio.h>

int main(int argc, char *argv[])
{
    void *address;
    int index;

    for (index = 0; address != NULL && index != 5; ++index)
    {
        switch (index)
        {
        case 0: address = __builtin_frame_address(0); break;
        case 1: address = __builtin_frame_address(1); break;
        case 2: address = __builtin_frame_address(2); break;
        case 3: address = __builtin_frame_address(3); break;
        case 4: address = __builtin_frame_address(4); break;
        }

        printf("0x%x\n", address);
    }

    return 0;
}

Run on an i386 machine its output is fine:
[douglas@backhome:~]$ ./test
0xbfbfec68
0xbfbfec9c
0x0

Run on an amd64 machine is not so fine:
[douglas@justonenight:~]$ ./test
0xffffeaf0
0xffffeb40
0xffffecef
0x4c454853
Bus error: 10 (core dumped)

The last hex value printed seems to consistently be a string from the environment variables. Judging by the two stack frame addresses printed on i386, perhaps the third address printed on amd64 is the one that should be a zero.
>Fix:
>Release-Note:
>Audit-Trail:
Responsible-Changed-From-To: freebsd-amd64->kib 
Responsible-Changed-By: kib 
Responsible-Changed-When: Thu Aug 21 18:56:29 UTC 2008 
Responsible-Changed-Why:  
Take, 

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

From: "Douglas Thrift" <douglas@douglasthrift.net>
To: "Douglas Thrift" <douglas@douglasthrift.net>,
        "Kostik Belousov" <kostikbel@gmail.com>
Cc: <bug-followup@FreeBSD.org>
Subject: Re: amd64/126543: [amd64] __builtin_frame_address does not return 0 when reaching the top of the stack
Date: Thu, 21 Aug 2008 20:25:41 -0700

 Hrm,
 
 It looks like the follow up email address should be bug-followup@FreeBSD.org 
 and not FreeBSD-gnats-followup@freebsd.org.
 _______________________________________________________________________
 Douglas William Thrift
 <douglas@douglasthrift.net>
 <http://www.douglasthrift.net/>
 
 ----- Original Message ----- 
 From: "Douglas Thrift" <douglas@douglasthrift.net>
 To: "Kostik Belousov" <kostikbel@gmail.com>
 Cc: <FreeBSD-gnats-followup@freebsd.org>
 Sent: Thursday, August 21, 2008 8:09 PM
 Subject: Re: amd64/126543: [amd64] __builtin_frame_address does not return 0 
 when reaching the top of the stack
 
 
 > Yay!
 >
 > That got my test program working.  I had to throw 
 > another -fno-omit-frame-pointer on to the build of libexecinfo to get it 
 > to work though, fun! I guess I'll submit that as a patch against the port.
 > _______________________________________________________________________
 > Douglas William Thrift
 > <douglas@douglasthrift.net>
 > <http://www.douglasthrift.net/>
 >
 > ----- Original Message ----- 
 > From: "Kostik Belousov" <kostikbel@gmail.com>
 > To: "Douglas William Thrift" <douglas@douglasthrift.net>
 > Cc: <FreeBSD-gnats-followup@freebsd.org>
 > Sent: Thursday, August 21, 2008 11:54 AM
 > Subject: Re: amd64/126543: [amd64] __builtin_frame_address does not return 
 > 0 when reaching the top of the stack
 >
 >
 >> The following patch works for me.
 >>
 >> diff --git a/lib/csu/amd64/Makefile b/lib/csu/amd64/Makefile
 >> index 4b51e6b..71ccd67 100644
 >> --- a/lib/csu/amd64/Makefile
 >> +++ b/lib/csu/amd64/Makefile
 >> @@ -7,6 +7,7 @@ OBJS= ${SRCS:N*.h:R:S/$/.o/g}
 >> OBJS+= gcrt1.o
 >> CFLAGS+= -I${.CURDIR}/../common \
 >>  -I${.CURDIR}/../../libc/include
 >> +CFLAGS+= -fno-omit-frame-pointer
 >>
 >> all: ${OBJS}
 >>
 >>
 >>
 > 
 

From: dfilter@FreeBSD.ORG (dfilter service)
To: bug-followup@FreeBSD.org
Cc:  
Subject: Re: amd64/126543: commit references a PR
Date: Fri, 22 Aug 2008 09:23:58 +0000 (UTC)

 kib         2008-08-22 09:23:39 UTC
 
   FreeBSD src repository
 
   Modified files:
     lib/csu/amd64        Makefile 
   Log:
   SVN rev 181997 on 2008-08-22 09:23:39Z by kib
   
   Add -fno-omit-frame-pointer to CFLAGS used to compile crt1.c on amd64.
   
   For gcc' __builtin_frame_address() to work, all call frames need to save
   frame pointer. In particular, this is important for the upper frame that
   should terminate the chain.
   
   No objections from:     jhb
   PR:     amd64/126543
   MFC after:      1 week
   
   Revision  Changes    Path
   1.19      +1 -0      src/lib/csu/amd64/Makefile
 _______________________________________________
 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->closed 
State-Changed-By: kib 
State-Changed-When: Fri Aug 29 09:03:07 UTC 2008 
State-Changed-Why:  
Committed to HEAD and RELENG_7. 

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