All of lore.kernel.org
 help / color / mirror / Atom feed
From: Nathan Chancellor <nathan@kernel.org>
To: Linus Torvalds <torvalds@linux-foundation.org>
Cc: Guenter Roeck <linux@roeck-us.net>,
	Linux Kernel Mailing List <linux-kernel@vger.kernel.org>,
	David Airlie <airlied@gmail.com>, Daniel Vetter <daniel@ffwll.ch>,
	dri-devel@lists.freedesktop.org,
	linux-toolchains@vger.kernel.org, llvm@lists.linux.dev
Subject: Re: Linux 6.3-rc3
Date: Mon, 20 Mar 2023 15:06:31 -0700	[thread overview]
Message-ID: <20230320220631.GA637514@dev-arch.thelio-3990X> (raw)
In-Reply-To: <CAHk-=wgyJREUR1WgfFmie5XVJnBLr1VPVbSibh1+Cq57Bh4Tag@mail.gmail.com>

On Mon, Mar 20, 2023 at 01:30:17PM -0700, Linus Torvalds wrote:
> On Mon, Mar 20, 2023 at 1:05 PM Guenter Roeck <linux@roeck-us.net> wrote:
> >
> > I have noticed that gcc doesn't always warn about uninitialized variables
> > in most architectures.
> 
> Yeah, I'm getting the feeling that when the gcc people were trying to
> make -Wmaybe-uninitialized work better (when moving it into "-Wall"),
> they ended up moving a lot of "clearly uninitialized" cases into it.
> 
> So then because we disable the "maybe" case (with
> -Wno-maybe-uninitialized) because it had too many random false
> positives, we end up not seeing the obvious cases either.

Right, this seems like a subtle difference in semantics between
-Wuninitialized between clang and GCC. My naive attempt to reduce the
problem with cvise spits out:

$ cat dev.i
void *host1x_probe___trans_tmp_1;
void host1x_unregister();
int host1x_probe_syncpt_irqhost1x_probe() {
  int err;
  if (host1x_probe___trans_tmp_1)
    return 2;
  if (err)
    host1x_unregister();
  return err;
}

$ gcc -O2 -Wall -c -o /dev/null dev.i
dev.i: In function ‘host1x_probe_syncpt_irqhost1x_probe’:
dev.i:7:6: warning: ‘err’ may be used uninitialized [-Wmaybe-uninitialized]
    7 |   if (err)
      |      ^
dev.i:4:7: note: ‘err’ was declared here
    4 |   int err;
      |       ^~~

$ clang -Wall -fsyntax-only dev.i
dev.i:7:7: warning: variable 'err' is uninitialized when used here [-Wuninitialized]
  if (err)
      ^~~
dev.i:4:10: note: initialize the variable 'err' to silence this warning
  int err;
         ^
          = 0
1 warning generated.

If I remove the first branch, both compilers show -Wuninitialized.

$ cat dev.i
void *host1x_probe___trans_tmp_1;
void host1x_unregister();
int host1x_probe_syncpt_irqhost1x_probe() {
  int err;
  if (err)
    host1x_unregister();
  return err;
}

$ gcc -O2 -Wall -c -o /dev/null dev.i
dev.i: In function ‘host1x_probe_syncpt_irqhost1x_probe’:
dev.i:5:6: warning: ‘err’ is used uninitialized [-Wuninitialized]
    5 |   if (err)
      |      ^
dev.i:4:7: note: ‘err’ was declared here
    4 |   int err;
      |       ^~~

$ clang -Wall -fsyntax-only dev.i
dev.i:5:7: warning: variable 'err' is uninitialized when used here [-Wuninitialized]
  if (err)
      ^~~
dev.i:4:10: note: initialize the variable 'err' to silence this warning
  int err;
         ^
          = 0
1 warning generated.

It seems like clang takes into account that the branch has no effect on
how uninitialized err is, although it does acknowledge there may be
control flow where err is not used uninitialized because it is not used
at all by stating "when used here". I guess GCC does not make this
distinction and places it under -Wmaybe-uninitialized. I could be
totally wrong though :)

Cheers,
Nathan

WARNING: multiple messages have this Message-ID (diff)
From: Nathan Chancellor <nathan@kernel.org>
To: Linus Torvalds <torvalds@linux-foundation.org>
Cc: llvm@lists.linux.dev,
	Linux Kernel Mailing List <linux-kernel@vger.kernel.org>,
	dri-devel@lists.freedesktop.org,
	linux-toolchains@vger.kernel.org,
	Guenter Roeck <linux@roeck-us.net>
Subject: Re: Linux 6.3-rc3
Date: Mon, 20 Mar 2023 15:06:31 -0700	[thread overview]
Message-ID: <20230320220631.GA637514@dev-arch.thelio-3990X> (raw)
In-Reply-To: <CAHk-=wgyJREUR1WgfFmie5XVJnBLr1VPVbSibh1+Cq57Bh4Tag@mail.gmail.com>

On Mon, Mar 20, 2023 at 01:30:17PM -0700, Linus Torvalds wrote:
> On Mon, Mar 20, 2023 at 1:05 PM Guenter Roeck <linux@roeck-us.net> wrote:
> >
> > I have noticed that gcc doesn't always warn about uninitialized variables
> > in most architectures.
> 
> Yeah, I'm getting the feeling that when the gcc people were trying to
> make -Wmaybe-uninitialized work better (when moving it into "-Wall"),
> they ended up moving a lot of "clearly uninitialized" cases into it.
> 
> So then because we disable the "maybe" case (with
> -Wno-maybe-uninitialized) because it had too many random false
> positives, we end up not seeing the obvious cases either.

Right, this seems like a subtle difference in semantics between
-Wuninitialized between clang and GCC. My naive attempt to reduce the
problem with cvise spits out:

$ cat dev.i
void *host1x_probe___trans_tmp_1;
void host1x_unregister();
int host1x_probe_syncpt_irqhost1x_probe() {
  int err;
  if (host1x_probe___trans_tmp_1)
    return 2;
  if (err)
    host1x_unregister();
  return err;
}

$ gcc -O2 -Wall -c -o /dev/null dev.i
dev.i: In function ‘host1x_probe_syncpt_irqhost1x_probe’:
dev.i:7:6: warning: ‘err’ may be used uninitialized [-Wmaybe-uninitialized]
    7 |   if (err)
      |      ^
dev.i:4:7: note: ‘err’ was declared here
    4 |   int err;
      |       ^~~

$ clang -Wall -fsyntax-only dev.i
dev.i:7:7: warning: variable 'err' is uninitialized when used here [-Wuninitialized]
  if (err)
      ^~~
dev.i:4:10: note: initialize the variable 'err' to silence this warning
  int err;
         ^
          = 0
1 warning generated.

If I remove the first branch, both compilers show -Wuninitialized.

$ cat dev.i
void *host1x_probe___trans_tmp_1;
void host1x_unregister();
int host1x_probe_syncpt_irqhost1x_probe() {
  int err;
  if (err)
    host1x_unregister();
  return err;
}

$ gcc -O2 -Wall -c -o /dev/null dev.i
dev.i: In function ‘host1x_probe_syncpt_irqhost1x_probe’:
dev.i:5:6: warning: ‘err’ is used uninitialized [-Wuninitialized]
    5 |   if (err)
      |      ^
dev.i:4:7: note: ‘err’ was declared here
    4 |   int err;
      |       ^~~

$ clang -Wall -fsyntax-only dev.i
dev.i:5:7: warning: variable 'err' is uninitialized when used here [-Wuninitialized]
  if (err)
      ^~~
dev.i:4:10: note: initialize the variable 'err' to silence this warning
  int err;
         ^
          = 0
1 warning generated.

It seems like clang takes into account that the branch has no effect on
how uninitialized err is, although it does acknowledge there may be
control flow where err is not used uninitialized because it is not used
at all by stating "when used here". I guess GCC does not make this
distinction and places it under -Wmaybe-uninitialized. I could be
totally wrong though :)

Cheers,
Nathan

  reply	other threads:[~2023-03-20 22:06 UTC|newest]

Thread overview: 65+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-03-19 20:50 Linux 6.3-rc3 Linus Torvalds
2023-03-20  8:21 ` Build regressions/improvements in v6.3-rc3 Geert Uytterhoeven
2023-03-21  5:38   ` Build regressions/improvements in v6.3-rc3 (drm/msm/) Randy Dunlap
2023-03-21  5:38     ` Randy Dunlap
2023-03-21  7:34     ` Geert Uytterhoeven
2023-03-21  7:34       ` Geert Uytterhoeven
2023-03-21 15:10       ` Randy Dunlap
2023-03-21 15:10         ` Randy Dunlap
2023-03-21 15:33         ` Geert Uytterhoeven
2023-03-21 15:33           ` Geert Uytterhoeven
2023-03-20 18:05 ` Linux 6.3-rc3 Nathan Chancellor
2023-03-20 18:05   ` Nathan Chancellor
2023-03-20 18:26   ` Linus Torvalds
2023-03-20 18:26     ` Linus Torvalds
2023-03-20 18:49     ` Linus Torvalds
2023-03-20 18:49       ` Linus Torvalds
2023-03-20 18:56       ` Nathan Chancellor
2023-03-20 18:56         ` Nathan Chancellor
2023-03-20 19:05         ` Linus Torvalds
2023-03-20 19:05           ` Linus Torvalds
2023-03-20 18:53     ` Nathan Chancellor
2023-03-20 18:53       ` Nathan Chancellor
2023-03-20 19:22       ` Nathan Chancellor
2023-03-20 19:22         ` Nathan Chancellor
2023-03-22 12:44       ` Kalle Valo
2023-03-22 12:44         ` Kalle Valo
2023-03-22 16:36         ` Nathan Chancellor
2023-03-22 16:36           ` Nathan Chancellor
2023-03-22 20:36           ` Nathan Chancellor
2023-03-22 20:36             ` Nathan Chancellor
2023-03-24 10:54           ` Kalle Valo
2023-03-24 10:54             ` Kalle Valo
2023-03-24 15:11             ` Nathan Chancellor
2023-03-24 15:11               ` Nathan Chancellor
2023-03-24 15:23               ` Kalle Valo
2023-03-24 15:23                 ` Kalle Valo
2023-03-28 19:07                 ` Nathan Chancellor
2023-03-28 19:07                   ` Nathan Chancellor
2023-03-29  8:39                   ` Kalle Valo
2023-03-29  8:39                     ` Kalle Valo
2023-03-22 16:40         ` Sedat Dilek
2023-03-22 16:40           ` Sedat Dilek
2023-03-22 16:55           ` Linus Torvalds
2023-03-22 16:55             ` Linus Torvalds
2023-03-22 18:17             ` Nick Desaulniers
2023-03-22 18:17               ` Nick Desaulniers
2023-03-24 17:16             ` Masahiro Yamada
2023-03-24 17:16               ` Masahiro Yamada
2023-03-27 16:12               ` Jani Nikula
2023-03-27 16:12                 ` Jani Nikula
2023-03-27 17:03                 ` Kalle Valo
2023-03-27 17:03                   ` Kalle Valo
2023-03-20 20:04     ` Guenter Roeck
2023-03-20 20:04       ` Guenter Roeck
2023-03-20 20:30       ` Linus Torvalds
2023-03-20 20:30         ` Linus Torvalds
2023-03-20 22:06         ` Nathan Chancellor [this message]
2023-03-20 22:06           ` Nathan Chancellor
2023-03-20 22:48           ` Segher Boessenkool
2023-03-20 22:48             ` Segher Boessenkool
2023-03-20 23:41           ` Linus Torvalds
2023-03-20 23:41             ` Linus Torvalds
2023-03-24  9:41   ` Daniel Vetter
2023-03-24  9:41     ` Daniel Vetter
2023-03-20 20:07 ` Guenter Roeck

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=20230320220631.GA637514@dev-arch.thelio-3990X \
    --to=nathan@kernel.org \
    --cc=airlied@gmail.com \
    --cc=daniel@ffwll.ch \
    --cc=dri-devel@lists.freedesktop.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-toolchains@vger.kernel.org \
    --cc=linux@roeck-us.net \
    --cc=llvm@lists.linux.dev \
    --cc=torvalds@linux-foundation.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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.