public inbox for gcc-patches@gcc.gnu.org
 help / color / mirror / Atom feed
From: Thomas Schwinge <thomas@codesourcery.com>
To: Martin Sebor <msebor@gmail.com>
Cc: <gcc-patches@gcc.gnu.org>,
	Christophe Lyon <christophe.lyon@linaro.org>,
	Hafiz Abid Qadeer <abidh@codesourcery.com>,
	Andrew Stubbs <ams@codesourcery.com>
Subject: Re: [PATCH libatomic/arm] avoid warning on constant addresses (PR 101379)
Date: Fri, 16 Jul 2021 19:42:19 +0200	[thread overview]
Message-ID: <87pmvii29w.fsf@euler.schwinge.homeip.net> (raw)
In-Reply-To: <816ce216-bf6a-75ca-4241-4861ec43ab27@gmail.com>

Hi Martin!

On 2021-07-09T17:11:25-0600, Martin Sebor via Gcc-patches <gcc-patches@gcc.gnu.org> wrote:
> The attached tweak avoids the new -Warray-bounds instances when
> building libatomic for arm. Christophe confirms it resolves
> the problem (thank you!)

As Abid has just reported in
<https://gcc.gnu.org/bugzilla/show_bug.cgi?id=101374#c16>, similar
problem with GCN target libgomp build:

    In function ‘gcn_thrs’,
        inlined from ‘gomp_thread’ at [...]/source-gcc/libgomp/libgomp.h:803:10,
        inlined from ‘GOMP_barrier’ at [...]/source-gcc/libgomp/barrier.c:34:29:
    [...]/source-gcc/libgomp/libgomp.h:792:10: error: array subscript 0 is outside array bounds of ‘__lds struct gomp_thread * __lds[0]’ [-Werror=array-bounds]
      792 |   return *thrs;
          |          ^~~~~

    gcc/config/gcn/gcn.h:  c_register_addr_space ("__lds", ADDR_SPACE_LDS);                   \

    libgomp/libgomp.h-static inline struct gomp_thread *gcn_thrs (void)
    libgomp/libgomp.h-{
    libgomp/libgomp.h-  /* The value is at the bottom of LDS.  */
    libgomp/libgomp.h:  struct gomp_thread * __lds *thrs = (struct gomp_thread * __lds *)4;
    libgomp/libgomp.h-  return *thrs;
    libgomp/libgomp.h-}

..., plus a few more.  Work-around:

       struct gomp_thread * __lds *thrs = (struct gomp_thread * __lds *)4;
    +# pragma GCC diagnostic push
    +# pragma GCC diagnostic ignored "-Warray-bounds"
       return *thrs;
    +# pragma GCC diagnostic pop

..., but it's a bit tedious to add that in all that the other places,
too.  (So I'll consider some GCN-specific '-Wno-array-bounds' if we don't
get to resolve this otherwise, soon.)

> As we have discussed, the main goal of this class of warnings
> is to detect accesses at addresses derived from null pointers
> (e.g., to struct members or array elements at a nonzero offset).

(ACK, and thanks for that work!)

> Diagnosing accesses at hardcoded addresses is incidental because
> at the stage they are detected the two are not distinguishable
> from each another.
>
> I'm planning (hoping) to implement detection of invalid pointer
> arithmetic involving null for GCC 12, so this patch is a stopgap
> solution to unblock the arm libatomic build without compromising
> the warning.  Once the new detection is in place these workarounds
> can be removed or replaced with something more appropriate (e.g.,
> declaring the objects at the hardwired addresses with an attribute
> like AVR's address or io; that would enable bounds checking at
> those addresses as well).

Of course, we may simply re-work the libgomp/GCN code -- but don't we
first need to answer the question whether the current code is actually
"bad"?  Aren't we going to get a lot of similar reports from
kernel/embedded/other low-level software developers, once this is out in
the wild?  I mean:

> PR bootstrap/101379 - libatomic arm build failure after r12-2132 due to -Warray-bounds on a constant address
>
> libatomic/ChangeLog:
>       * /config/linux/arm/host-config.h (__kernel_helper_version): New
>       function.  Adjust shadow macro.
>
> diff --git a/libatomic/config/linux/arm/host-config.h b/libatomic/config/linux/arm/host-config.h
> index 1520f237d73..777d08a2b85 100644
> --- a/libatomic/config/linux/arm/host-config.h
> +++ b/libatomic/config/linux/arm/host-config.h
> @@ -39,8 +39,14 @@ typedef void (__kernel_dmb_t) (void);
>  #define __kernel_dmb (*(__kernel_dmb_t *) 0xffff0fa0)
>
>  /* Kernel helper page version number.  */
> -#define __kernel_helper_version (*(unsigned int *)0xffff0ffc)

Are such (not un-common) '#define's actually "bad", and anyhow ought to
be replaced by something like the following?

> +static inline unsigned*
> +__kernel_helper_version ()
> +{
> +  unsigned *volatile addr = (unsigned int *)0xffff0ffc;
> +  return addr;
> +}
>
> +#define __kernel_helper_version (*__kernel_helper_version())

(No 'volatile' in the original code, by the way.)


Grüße
 Thomas
-----------------
Siemens Electronic Design Automation GmbH; Anschrift: Arnulfstraße 201, 80634 München; Gesellschaft mit beschränkter Haftung; Geschäftsführer: Thomas Heurung, Frank Thürauf; Sitz der Gesellschaft: München; Registergericht München, HRB 106955

  parent reply	other threads:[~2021-07-16 17:42 UTC|newest]

Thread overview: 15+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-07-09 23:11 Martin Sebor
2021-07-15  8:33 ` Christophe Lyon
2021-07-16 17:42 ` Thomas Schwinge [this message]
2021-07-16 21:11   ` Martin Sebor
2021-07-19  8:49     ` Thomas Schwinge
2021-07-17 22:28   ` Andrew Stubbs
2021-07-19  8:46     ` [gcn] Work-around libgomp 'error: array subscript 0 is outside array bounds of ‘__lds struct gomp_thread * __lds[0]’ [-Werror=array-bounds]' (was: [PATCH libatomic/arm] avoid warning on constant addresses (PR 101379)) Thomas Schwinge
2021-07-19  8:56       ` Jakub Jelinek
2021-07-19 11:10       ` Andrew Stubbs
2021-07-20  7:23       ` [gcn] Work-around libgomp 'error: array subscript 0 is outside array bounds of ‘__lds struct gomp_thread * __lds[0]’ [-Werror=array-bounds]' Thomas Schwinge
2021-07-20  8:40         ` '#pragma GCC diagnostic' (mis-)use in 'statement' of 'if' (was: [gcn] Work-around libgomp 'error: array subscript 0 is outside array bounds of ‘__lds struct gomp_thread * __lds[0]’ [-Werror=array-bounds]') Thomas Schwinge
2021-07-20 19:47           ` '#pragma GCC diagnostic' (mis-)use in 'statement' of 'if' Martin Sebor
2021-07-20 20:16             ` Jakub Jelinek
2021-07-21 16:41 ` [PATCH libatomic/arm] avoid warning on constant addresses (PR 101379) Kyrylo Tkachov
2021-07-21 16:54   ` Martin Sebor

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=87pmvii29w.fsf@euler.schwinge.homeip.net \
    --to=thomas@codesourcery.com \
    --cc=abidh@codesourcery.com \
    --cc=ams@codesourcery.com \
    --cc=christophe.lyon@linaro.org \
    --cc=gcc-patches@gcc.gnu.org \
    --cc=msebor@gmail.com \
    /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).