public inbox for overseers@sourceware.org
 help / color / mirror / Atom feed
From: Andrew Pinski <pinskia@gcc.gnu.org>
To: Reuben Hawkins <reubenhwk@gmail.com>
Cc: "overseers@gcc.gnu.org" <overseers@gcc.gnu.org>
Subject: Re: gcc x64 linux code generation (passing pointer var-args) bug
Date: Fri, 23 Jan 2015 21:40:00 -0000	[thread overview]
Message-ID: <CA+=Sn1=DM0imtQ25qZ3iWhvvABXProto9ZPjdaX5KP4uU3ZGkw@mail.gmail.com> (raw)
In-Reply-To: <CAD_8n+Q5Ac7AmmWaFMJe-4W+Zf1eROS85hb8WekMWuHhUsc1Cg@mail.gmail.com>

On Fri, Jan 23, 2015 at 1:36 PM, Reuben Hawkins <reubenhwk@gmail.com> wrote:
> Hi Overseers,
>
> I ran into an issue with all versions of gcc which support x64 which
> *could* be considered a bug.  At the very least, it's a pitfall.  I'm
> not really sure to whom I should bring this problem to.  Bugzilla?
> Mailing list?  Not sure...


It is not a bug because ...

>
> Anyway, the gist of the bug is this...
>
> printf("%p %p %p %p %p %p\n", 0, 0, 0, 0, 0, 0);

You are passing 32bit values where 64bit values are expected.  If you
used -Wformat, it would have warned you about this issue.

>
> The first 5 zero ints are copied into the esi, edx, ecx, r8d and r9d,
> (as the linux x64 calling convention mandates) with the movl
> instruction.  The movl instruction will zero out the upper 32-bits of
> those registers.  The last zero int, however is copied to (%rsp) with
> movl, which does *not* zero out the upper 32 bits because (%rsp) is
> not a register, so the last 0 is not promoted to a 64-bit zero, but
> the rest of the zeros are.  If I were to add another zero, that zero
> would be copied to 8(%rsp), so the upper 32-bits of (%rsp) are skipped
> and whatever garbage happens to be there is passed to the called
> function.
>
> printf("%p %p %p %p %p %p\n", 0, 0, 0, 0, 0, (void*)0);
>
> ...works because the (void*) causes gcc to emit a movq instruction.
>
> I'm wondering if there's a possibility to change this unexpected
> behavior in gcc such that it always uses movq on stack args.


it is not unexpected because the ABI says something different from
what you are trying to work with.

>
> I realize all the zeros are technically wrong, they should be either
> NULL or (void*) casts, but it's a huge pain that '0' works for the
> first 6 args, then doesn't on the 7th when the args start going on the
> stack.


So this is undefined in C and will never work on most other targets too.

Thanks,
Andrew

>
> Thanks in advance,
> Reuben Hawkins

  reply	other threads:[~2015-01-23 21:40 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2015-01-23 21:36 Reuben Hawkins
2015-01-23 21:40 ` Andrew Pinski [this message]
2015-01-23 22:11   ` Reuben Hawkins
2015-01-24 22:28 ` Ian Lance Taylor
2015-01-24 22:52   ` Joseph Myers

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

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

  git send-email \
    --in-reply-to='CA+=Sn1=DM0imtQ25qZ3iWhvvABXProto9ZPjdaX5KP4uU3ZGkw@mail.gmail.com' \
    --to=pinskia@gcc.gnu.org \
    --cc=overseers@gcc.gnu.org \
    --cc=reubenhwk@gmail.com \
    /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).