public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
From: "cvs-commit at gcc dot gnu.org" <gcc-bugzilla@gcc.gnu.org>
To: gcc-bugs@gcc.gnu.org
Subject: [Bug middle-end/25521] change semantics of const volatile variables
Date: Thu, 29 Sep 2022 11:14:43 +0000	[thread overview]
Message-ID: <bug-25521-4-9nmm56NzDR@http.gcc.gnu.org/bugzilla/> (raw)
In-Reply-To: <bug-25521-4@http.gcc.gnu.org/bugzilla/>

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=25521

--- Comment #10 from CVS Commits <cvs-commit at gcc dot gnu.org> ---
The master branch has been updated by Jose E. Marchesi <jemarch@gcc.gnu.org>:

https://gcc.gnu.org/g:a0aafbc324aa90421f0ce99c6f5bbf64ed163da6

commit r13-2952-ga0aafbc324aa90421f0ce99c6f5bbf64ed163da6
Author: Jose E. Marchesi <jose.marchesi@oracle.com>
Date:   Thu Aug 4 21:16:10 2022 +0200

    place `const volatile' objects in read-only sections

    It is common for C BPF programs to use variables that are implicitly
    set by the BPF loader and run-time.  It is also necessary for these
    variables to be stored in read-only storage so the BPF verifier
    recognizes them as such.  This leads to declarations using both
    `const' and `volatile' qualifiers, like this:

      const volatile unsigned char is_allow_list = 0;

    Where `volatile' is used to avoid the compiler to optimize out the
    variable, or turn it into a constant, and `const' to make sure it is
    placed in .rodata.

    Now, it happens that:

    - GCC places `const volatile' objects in the .data section, under the
      assumption that `volatile' somehow voids the `const'.

    - LLVM places `const volatile' objects in .rodata, under the
      assumption that `volatile' is orthogonal to `const'.

    So there is a divergence, that has practical consequences: it makes
    BPF programs compiled with GCC to not work properly.

    When looking into this, I found this bugzilla:

      https://gcc.gnu.org/bugzilla/show_bug.cgi?id=25521
      "change semantics of const volatile variables"

    which was filed back in 2005, long ago.  This report was already
    asking to put `const volatile' objects in .rodata, questioning the
    current behavior.

    While discussing this in the #gcc IRC channel I was pointed out to the
    following excerpt from the C18 spec:

       6.7.3 Type qualifiers / 5 The properties associated with qualified
             types are meaningful only for expressions that are
             lval-values [note 135]

       135) The implementation may place a const object that is not
            volatile in a read-only region of storage. Moreover, the
            implementation need not allocate storage for such an object if
            its $ address is never used.

    This footnote may be interpreted as if const objects that are volatile
    shouldn't be put in read-only storage.  Even if I personally was not
    very convinced of that interpretation (see my earlier comment in BZ
    25521) I filed the following issue in the LLVM tracker in order to
    discuss the matter:

      https://github.com/llvm/llvm-project/issues/56468

    As you can see, Aaron Ballman, one of the LLVM hackers, asked the WG14
    reflectors about this.  He reported that the reflectors don't think
    footnote 135 has any normative value.

    So, not having a normative mandate on either direction, there are two
    options:

    a) To change GCC to place `const volatile' objects in .rodata instead
       of .data.

    b) To change LLVM to place `const volatile' objects in .data instead
       of .rodata.

    Considering that:

    - One target (bpf-unknown-none) breaks with the current GCC behavior.

    - No target/platform relies on the GCC behavior, that we know.

    - Changing the LLVM behavior at this point would be very severely
      traumatic for the BPF people and their users.

    I think the right thing to do at this point is a).
    Therefore this patch.

    Regtested in x86_64-linux-gnu and bpf-unknown-none.
    No regressions observed.

    gcc/ChangeLog:

            PR middle-end/25521
            * varasm.cc (categorize_decl_for_section): Place `const volatile'
            objects in read-only sections.
            (default_select_section): Likewise.

    gcc/testsuite/ChangeLog:

            PR middle-end/25521
            * lib/target-supports.exp (check_effective_target_elf): Define.
            * gcc.dg/pr25521.c: New test.

  parent reply	other threads:[~2022-09-29 11:14 UTC|newest]

Thread overview: 10+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
     [not found] <bug-25521-4@http.gcc.gnu.org/bugzilla/>
2011-12-22  1:16 ` pinskia at gcc dot gnu.org
2011-12-22  1:18 ` pinskia at gcc dot gnu.org
2022-07-10 20:06 ` jose.marchesi at oracle dot com
2022-07-10 20:19 ` jose.marchesi at oracle dot com
2022-07-11  1:53 ` jose.marchesi at oracle dot com
2022-08-03 18:21 ` jose.marchesi at oracle dot com
2022-09-29 11:14 ` cvs-commit at gcc dot gnu.org [this message]
2005-12-21 18:57 [Bug middle-end/25521] New: " drepper at redhat dot com
2005-12-21 19:17 ` [Bug middle-end/25521] " gdr at integrable-solutions dot net
2005-12-21 19:38 ` drepper at redhat dot com
2005-12-21 19:54 ` gdr at integrable-solutions dot net

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=bug-25521-4-9nmm56NzDR@http.gcc.gnu.org/bugzilla/ \
    --to=gcc-bugzilla@gcc.gnu.org \
    --cc=gcc-bugs@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).