diff options
author | Jonathan Wakely <jwakely@redhat.com> | 2020-06-17 19:48:05 +0100 |
---|---|---|
committer | Jonathan Wakely <jwakely@redhat.com> | 2020-06-17 20:02:53 +0100 |
commit | d2384b7b24f8557b66f6958a05ea99ff4307e75c (patch) | |
tree | 87421899129a396ec7cdf476312dc30ae7cd7f22 /gcc/c-family | |
parent | 4e49b994de060d4a6c9318d0ed52ef038153426e (diff) | |
download | gcc-d2384b7b24f8557b66f6958a05ea99ff4307e75c.zip gcc-d2384b7b24f8557b66f6958a05ea99ff4307e75c.tar.gz gcc-d2384b7b24f8557b66f6958a05ea99ff4307e75c.tar.bz2 |
c-family: check qualifiers of arguments to __atomic built-ins (PR 95378)
Currently the __atomic_{load,store,exchange,compare_exchange} built-ins
will happily store values through pointers to const, or use pointers to
volatile as the input and output arguments. This patch ensures that any
pointer that will be written through does not point to a const object,
and only the pointer to the atomic variable can be volatile.
This differs slightly from Clang, which allows the third argument to
__atomic_exchange (the one that is used to return the old value) to be
volatile if and only if the first argument is volatile. That doesn't
seem useful.
For C++ emit errors, but for C use pedwarns that are controlled by
-Wincompatible-pointer-types.
gcc/c-family/ChangeLog:
* c-common.c (get_atomic_generic_size): Check cv-qualifiers in
pointer arguments.
gcc/testsuite/ChangeLog:
* c-c++-common/pr95378.c: New test.
Diffstat (limited to 'gcc/c-family')
-rw-r--r-- | gcc/c-family/c-common.c | 41 |
1 files changed, 41 insertions, 0 deletions
diff --git a/gcc/c-family/c-common.c b/gcc/c-family/c-common.c index b1379fa..b73ad2e 100644 --- a/gcc/c-family/c-common.c +++ b/gcc/c-family/c-common.c @@ -6903,6 +6903,7 @@ get_atomic_generic_size (location_t loc, tree function, { unsigned int n_param; unsigned int n_model; + unsigned int outputs = 0; // bitset of output parameters unsigned int x; int size_0; tree type_0; @@ -6913,15 +6914,22 @@ get_atomic_generic_size (location_t loc, tree function, case BUILT_IN_ATOMIC_EXCHANGE: n_param = 4; n_model = 1; + outputs = 5; break; case BUILT_IN_ATOMIC_LOAD: + n_param = 3; + n_model = 1; + outputs = 2; + break; case BUILT_IN_ATOMIC_STORE: n_param = 3; n_model = 1; + outputs = 1; break; case BUILT_IN_ATOMIC_COMPARE_EXCHANGE: n_param = 6; n_model = 2; + outputs = 3; break; default: gcc_unreachable (); @@ -7010,6 +7018,39 @@ get_atomic_generic_size (location_t loc, tree function, function); return 0; } + + { + auto_diagnostic_group d; + int quals = TYPE_QUALS (TREE_TYPE (type)); + /* Must not write to an argument of a const-qualified type. */ + if (outputs & (1 << x) && quals & TYPE_QUAL_CONST) + { + if (c_dialect_cxx ()) + { + error_at (loc, "argument %d of %qE must not be a pointer to " + "a %<const%> type", x + 1, function); + return 0; + } + else + pedwarn (loc, OPT_Wincompatible_pointer_types, "argument %d " + "of %qE discards %<const%> qualifier", x + 1, + function); + } + /* Only the first argument is allowed to be volatile. */ + if (x > 0 && quals & TYPE_QUAL_VOLATILE) + { + if (c_dialect_cxx ()) + { + error_at (loc, "argument %d of %qE must not be a pointer to " + "a %<volatile%> type", x + 1, function); + return 0; + } + else + pedwarn (loc, OPT_Wincompatible_pointer_types, "argument %d " + "of %qE discards %<volatile%> qualifier", x + 1, + function); + } + } } /* Check memory model parameters for validity. */ |