public inbox for libc-alpha@sourceware.org
 help / color / mirror / Atom feed
From: Sergey Bugaev <bugaevc@gmail.com>
To: Jakub Jelinek <jakub@redhat.com>
Cc: libc-alpha@sourceware.org, Florian Weimer <fweimer@redhat.com>
Subject: Re: GCC miscompilation with __seg_fs
Date: Mon, 13 Mar 2023 19:13:46 +0300	[thread overview]
Message-ID: <CAN9u=Heeem3L5ybS8CPHLWubFn_=1uCsN7AffyzhP1-zmsGZRw@mail.gmail.com> (raw)
In-Reply-To: <ZA9FJwpYnneWpvWi@tucnak>

On Mon, Mar 13, 2023 at 6:45 PM Jakub Jelinek <jakub@redhat.com> wrote:
> You could also hide the fact that it is based on 0 pointer from the
> compiler...

But how would you do that? If we want the generated assembly to say
%fs:0, the compiler needs to statically const-propagate the pointer to
zero. If that's not important, this seems to do the trick:

static inline size_t
zero (void)
{
  size_t res;
  asm ("xor %0, %0" : "=r" (res));
  return res;
}

#define THREAD_SELF_HIDDEN                                     \
  (*(tcbhead_t * __seg_fs *) zero ())

void
assign_through_self_hidden (void)
{
  THREAD_SELF_HIDDEN->some_member = 42;
}
// assign_through_self_hidden:
//         xor %rax, %rax
//         movq    %fs:(%rax), %rax
//         movl    $42, 8(%rax)
//         ret

FWIW, without __seg_fs (where dereferencing a zero ptr is actual UB),
assigning through *(int *) 0 is enough for GCC to consider the rest of
the code dead, whereas an assignment through **(int **) 0 does not
trigger that, but the store is still eliminated:

void some_external_call (void);

void
deref_nullptr (void)
{
  *(int *) 0 = 42;
  some_external_call ();
}
// deref_nullptr:
//         movl    $0, 0
//         ud2

void
indirect_deref_nullptr (void)
{
  **(int **) 0 = 42;
  some_external_call ();
}
// indirect_deref_nullptr:
//         jmp     some_external_call

> Feel free to file a bug report in GCC bugzilla, but that won't improve
> anything on the already released compilers.

I don't even have a GCC bugzilla account, so please file it yourself.
But yes, this needs to be worked around in glibc in any case.

Sergey

      parent reply	other threads:[~2023-03-13 16:13 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-03-13 15:24 Sergey Bugaev
2023-03-13 15:45 ` Jakub Jelinek
2023-03-13 15:53   ` Andrew Pinski
2023-03-13 16:13   ` Sergey Bugaev [this message]

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='CAN9u=Heeem3L5ybS8CPHLWubFn_=1uCsN7AffyzhP1-zmsGZRw@mail.gmail.com' \
    --to=bugaevc@gmail.com \
    --cc=fweimer@redhat.com \
    --cc=jakub@redhat.com \
    --cc=libc-alpha@sourceware.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).