public inbox for gcc-patches@gcc.gnu.org
 help / color / mirror / Atom feed
From: Martin Sebor <msebor@gmail.com>
To: "Martin Liška" <mliska@suse.cz>, gcc-patches <gcc-patches@gcc.gnu.org>
Cc: Jonathan Wakely <jwakely@redhat.com>
Subject: Re: [PATCH] enable -Winvalid-memory-order for C++ [PR99612]
Date: Wed, 5 Jan 2022 13:34:06 -0700	[thread overview]
Message-ID: <4ecfa20c-d06b-00fc-d72a-c06bb14aabf8@gmail.com> (raw)
In-Reply-To: <ed6f3af3-f329-6740-49b0-3bf31b0572a1@suse.cz>

On 1/5/22 1:45 AM, Martin Liška wrote:
> On 12/8/21 17:49, Martin Sebor via Gcc-patches wrote:
>> Even with -Wno-system-headers enabled, the -Winvalid-memory-order
>> code tries to make sure calls to atomic functions with invalid
>> memory orders are diagnosed even though the C atomic functions
>> are defined as macros in the <stdatomic.h> system header.
>> The warning triggers at all optimization levels, including -O0.
>>
>> Independently, the core diagnostic enhancements implemented earlier
>> this year (the warning group control) enable warnings for functions
>> defined in system headers that are inlined into user code.  This
>> was done for similar reason as above: because it's desirable to
>> diagnose invalid calls made from user code to system functions
>> (e.g., buffer overflows, invalid or mismatched deallocations,
>> etc.)
>>
>> However, the C macro solution interferes with the code diagnostic
>> changes and prevents the invalid memory model warnings from being
>> issued for the same problems in C++.  In addition, because C++
>> atomics are ordinary (inline) functions that call the underlying
>> __atomic_xxx built-ins, the invalid memory orders can only be
>> detected with both inlining and constant propagation enabled.
>>
>> The attached patch removes these limitations and enables
>> -Winvalid-memory-order to trigger even for C++ std::atomic,
>> (almost) just like it does in C, at all optimization levels
>> including -O0.
>>
>> To make that possible I had to move -Winvalid-memory-order from
>> builtins.c to a GIMPLE pass where it can use context-sensitive
>> range info at -O0, instead of relying on constant propagation
>> (only available at -O1 and above).  Although the same approach
>> could be used to emit better object code for C++ atomics at -O0
>> (i.e., use the right memory order instead of dropping to seq_cst),
>> this patch doesn't do that.)
>>
>> In addition to enabling the warning I've also enhanced it to
>> include the memory models involved in the diagnosed call (both
>> the problem ones and the viable alternatives).
>>
>> Tested on x86_64-linux.
>>
>> Jonathan, I CC you for two reasons: a) because this solution
>> is based on your (as well as my own) preference for handling
>> C++ system headers, and because of our last week's discussion
>> of the false positives in std::string resulting from the same
>> choice there.
>>
>> I don't anticipate this change to lead to the same fallout
>> because it's unlikely for GCC to synthesize invalid memory
>> orders out of thin air; and b) because the current solution
>> can only detect the problems in calls to atomic functions at
>> -O0 that are declared with attribute always_inline.  This
>> includes member functions defined in the enclosing atomic
>> class but not namespace-scope functions.  To make
>> the detection possible those would also have to be
>> always_inline.  If that's a change you'd like to see I can
>> look into making it happen.
>>
>> Martin
> 
> Hello.
> 
> I think the patch caused:
> 
> gcc/gimple-ssa-warn-access.cc:2844:30: warning: quoted ‘%s’ directive in 
> format; use ‘%qs’ instead [-Wformat-diag]
> 
> Can you please take a look?

I've fixed it in r12-6271.

Thanks
Martin

  reply	other threads:[~2022-01-05 20:34 UTC|newest]

Thread overview: 11+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-12-08 16:49 Martin Sebor
2021-12-08 17:14 ` Jonathan Wakely
2021-12-08 18:12   ` Martin Sebor
2021-12-15 15:30 ` PING " Martin Sebor
2021-12-23 23:20 ` Jeff Law
2022-01-05  8:45 ` Martin Liška
2022-01-05 20:34   ` Martin Sebor [this message]
2022-01-27 23:47 ` Andrew Pinski
2022-01-27 23:48   ` Andrew Pinski
2022-01-28  0:59   ` Martin Sebor
2022-01-28 11:05     ` Jonathan Wakely

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=4ecfa20c-d06b-00fc-d72a-c06bb14aabf8@gmail.com \
    --to=msebor@gmail.com \
    --cc=gcc-patches@gcc.gnu.org \
    --cc=jwakely@redhat.com \
    --cc=mliska@suse.cz \
    /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).