unofficial mirror of bug-guile@gnu.org 
 help / color / mirror / Atom feed
From: Andy Wingo <wingo@pobox.com>
To: bug-autoconf@gnu.org
Cc: bug-guile <bug-guile@gnu.org>
Subject: bug in check for stack growth direction in _AC_LIBOBJ_ALLOCA
Date: Fri, 17 Jun 2011 12:21:23 +0200	[thread overview]
Message-ID: <87d3ic92sc.fsf@pobox.com> (raw)

Hello,

The following transcript indicates a problem with the stack growth
direction check, present at functions.m4:328 in autoconf 2.68:

  wingo@badger:/tmp$ cat foo.c 
  int
  find_stack_direction ()
  {
    static char *addr = 0;
    auto char dummy;
    if (addr == 0)
      {
        addr = &dummy;
        return find_stack_direction ();
      }
    else
      return (&dummy > addr) ? 1 : -1;
  }

  int
  main ()
  {
    return find_stack_direction () < 0;
  }
  wingo@badger:/tmp$ gcc -O1 -o test foo.c
  wingo@badger:/tmp$ ./test; echo $?
  1
  wingo@badger:/tmp$ gcc -O3 -o test foo.c
  wingo@badger:/tmp$ ./test; echo $?
  0
  $ gcc --version
  gcc (Debian 4.6.0-13) 4.6.1 20110611 (prerelease)
  Copyright (C) 2011 Free Software Foundation, Inc.
  This is free software; see the source for copying conditions.  There is NO
  warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

As you see, the check (on an x86-64 system) gives the correct result at
-O1 but not at -O3.

(I don't actually use this check from _AC_LIBOBJ_ALLOCA, but Guile has
the exact same test to check for stack growth direction, so it's the
same issue.  Reported by Marco Maggi.)

Looking a bit more closely at this, I find the assembly to be a bit odd
here:

    00000000004004a0 <find_stack_direction>:
      4004a0:	mov    0x2003f9(%rip),%rax        # 6008a0 <addr.1586>
      4004a7:	test   %rax,%rax
      4004aa:	je     4004c0 <find_stack_direction+0x20>
      4004ac:	lea    -0x2(%rsp),%rdx
      4004b1:	cmp    %rdx,%rax
      4004b4:	sbb    %eax,%eax
      4004b6:	and    $0x2,%eax
      4004b9:	sub    $0x1,%eax
      4004bc:	retq   
      4004bd:	nopl   (%rax)
      4004c0:	lea    -0x2(%rsp),%rax
      4004c5:	lea    -0x1(%rsp),%rdx
      4004ca:	cmp    %rdx,%rax
      4004cd:	mov    %rax,0x2003cc(%rip)        # 6008a0 <addr.1586>
      4004d4:	sbb    %eax,%eax
      4004d6:	and    $0x2,%eax
      4004d9:	sub    $0x1,%eax
      4004dc:	retq   
      4004dd:	nop
      4004de:	nop
      4004df:	nop

As you can see there is no call.  The test will always be true, thus the
second branch is always taken.  I don't know what allows GCC to do this
inlining.  Could it be a GCC bug?  Every time I think I have a GCC bug
I'm wrong, though :)

I tried changing the test to the following:

  int
  find_stack_direction (char *addr)
  {
    char dummy;
    if (addr == 0)
      return find_stack_direction (&dummy);
    else
      return (&dummy > addr) ? 1 : -1;
  }

  int
  main ()
  {
    return find_stack_direction (0) < 0;
  }

But I get the same behavior.

Andy
-- 
http://wingolog.org/



             reply	other threads:[~2011-06-17 10:21 UTC|newest]

Thread overview: 17+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2011-06-17 10:21 Andy Wingo [this message]
2011-06-18 20:25 ` bug in check for stack growth direction in _AC_LIBOBJ_ALLOCA Andrew W. Nosenko
2011-06-19 19:03   ` Andy Wingo
2011-06-20  8:25     ` Andrew W. Nosenko
2011-06-20 10:24       ` Andrew W. Nosenko
2011-06-18 21:42 ` Paul Eggert
2011-06-19 19:01   ` Andy Wingo
2011-06-20  5:35     ` Paul Eggert
2011-06-20  6:35       ` Ralf Wildenhues
2011-06-20  6:50         ` Paul Eggert
2011-06-20  8:18           ` Andrew W. Nosenko
2011-06-20  8:01       ` Andy Wingo
2011-06-20 17:54       ` Eric Blake
2011-06-20 22:19         ` Paul Eggert
2011-06-21 19:43           ` Ralf Wildenhues
2011-06-21 20:08             ` Paul Eggert
2011-06-20 17:56 ` Eric Blake

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://www.gnu.org/software/guile/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=87d3ic92sc.fsf@pobox.com \
    --to=wingo@pobox.com \
    --cc=bug-autoconf@gnu.org \
    --cc=bug-guile@gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).