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

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...

Anyway, the gist of the bug is this...

printf("%p %p %p %p %p %p\n", 0, 0, 0, 0, 0, 0);

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.

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.

Thanks in advance,
Reuben Hawkins

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

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2015-01-23 21:36 Reuben Hawkins [this message]
2015-01-23 21:40 ` Andrew Pinski
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=CAD_8n+Q5Ac7AmmWaFMJe-4W+Zf1eROS85hb8WekMWuHhUsc1Cg@mail.gmail.com \
    --to=reubenhwk@gmail.com \
    --cc=overseers@gcc.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).