[<prev] [next>] [<thread-prev] [thread-next>] [day] [month] [year] [list]
Message-ID: <CAHk-=whP-9yPAWuJDwA6+rQ-9owuYZgmrMA9AqO3EGJVefe8vg@mail.gmail.com>
Date: Mon, 25 Feb 2019 14:34:47 -0800
From: Linus Torvalds <torvalds@...ux-foundation.org>
To: Hugh Dickins <hughd@...gle.com>, Jan Hubicka <hubicka@....cz>,
rguenth@....gnu.org
Cc: "Darrick J. Wong" <darrick.wong@...cle.com>,
Andrew Morton <akpm@...ux-foundation.org>,
Matej Kupljen <matej.kupljen@...il.com>,
Al Viro <viro@...iv.linux.org.uk>,
Dan Carpenter <dan.carpenter@...cle.com>,
Linux List Kernel Mailing <linux-kernel@...r.kernel.org>,
linux-fsdevel <linux-fsdevel@...r.kernel.org>,
Linux-MM <linux-mm@...ck.org>
Subject: Re: [PATCH] tmpfs: fix uninitialized return value in shmem_link
On Mon, Feb 25, 2019 at 12:34 PM Hugh Dickins <hughd@...gle.com> wrote:
>
> Seems like a gcc bug? But I don't have a decent recent gcc to hand
> to submit a proper report, hope someone else can shed light on it.
I don't have a _very_ recent gcc either, but with gcc-8.2.1 the
attached test-case gives me:
[torvalds@i7 ~]$ gcc -O2 -S -Wall test.c
with no warning, and then
[torvalds@i7 ~]$ gcc -O2 -S -Wall -DHIDE_PROBLEM test.c
test.c: In function ‘shmem_link’:
test.c:60:9: warning: ‘ret’ may be used uninitialized in this
function [-Wmaybe-uninitialized]
return ret;
^~~
*does* show the expected warning.
So it is the presence of that
if (ret) return ret;
that suppresses the warning.
What I *suspect* happens is
(a) gcc sees that there is only one assignment to "ret"
(b) in the same basic block as the assignment, there is a test
against "ret" being nonzero that goes out.
and what I think happens is that (a) causes gcc to consider that
assignment to be the defining assignment (which makes all kinds of
sense in an SSA world), and then (b) means that gcc decides that
clearly "ret" has to be zero in any case that doesn't go out due to
the if-test.
In fact, if I then look at the code generation, gcc will actually do
this (edited to be more legible):
movl (%rbx), %eax <- load inode->i_nlink
testl %eax, %eax
je .L1
...
...
call d_instantiate
xorl %eax, %eax <- explicitly zero 'ret'!
.L1:
popq %rbx
popq %rbp
popq %r12
ret
so at least with my compiler, it *effectively* zeroed ret (in %rax)
anyway, and it all just _happened_ to get the right result even though
'ret' wasn't actually initialized.
Which is why it all worked just fine. And depending on how gcc works
internally, it really may not just be a random mistake of register
allocation, but really because gcc kind of _thought_ that 'ret' was
zero-initialized due to the combination of the one single assigment
and test for zero.
So it turns out that the patch to initialize to zero doesn't do
anything, probably for the same reason that gcc didn't warn about the
missing initialization. Gcc kind of added an initialization of its own
there.
I'm not entirely sure if any gcc developer would be interested in this
as a test-case, but I guess I can try to do a bugzilla.
Adding a few gcc people who have been on previous kernel gcc bugzilla
discussions, just in case they have something to add.
The gcc bugzilla is this:
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=89501
and I tried to make it be self-explanatory, but I wrote the bugzilla
in parallel with this email, and maybe there's some missing context
either there (or here).
Linus
View attachment "test.c" of type "text/x-c-code" (1531 bytes)
Powered by blists - more mailing lists