[<prev] [next>] [<thread-prev] [thread-next>] [day] [month] [year] [list]
Message-ID: <20200331223638.GA53668@ubuntu-m2-xlarge-x86>
Date: Tue, 31 Mar 2020 15:36:38 -0700
From: Nathan Chancellor <natechancellor@...il.com>
To: Daniel Santos <daniel.santos@...ox.com>
Cc: Vegard Nossum <vegard.nossum@...cle.com>,
Andrew Morton <akpm@...ux-foundation.org>,
linux-kernel@...r.kernel.org,
Rasmus Villemoes <linux@...musvillemoes.dk>,
Masahiro Yamada <yamada.masahiro@...ionext.com>,
Ian Abbott <abbotti@....co.uk>
Subject: Re: [PATCH] compiler.h: fix error in BUILD_BUG_ON() reporting
On Tue, Mar 31, 2020 at 05:25:38PM -0500, Daniel Santos wrote:
> On 3/31/20 6:26 AM, Vegard Nossum wrote:
> > compiletime_assert() uses __LINE__ to create a unique function name.
> > This means that if you have more than one BUILD_BUG_ON() in the same
> > source line (which can happen if they appear e.g. in a macro), then
> > the error message from the compiler might output the wrong condition.
> >
> > For this source file:
> >
> > #include <linux/build_bug.h>
> >
> > #define macro() \
> > BUILD_BUG_ON(1); \
> > BUILD_BUG_ON(0);
> >
> > void foo()
> > {
> > macro();
> > }
> >
> > gcc would output:
> >
> > ./include/linux/compiler.h:350:38: error: call to ‘__compiletime_assert_9’ declared with attribute error: BUILD_BUG_ON failed: 0
> > _compiletime_assert(condition, msg, __compiletime_assert_, __LINE__)
> >
> > However, it was not the BUILD_BUG_ON(0) that failed, so it should say 1
> > instead of 0. With this patch, we use __COUNTER__ instead of __LINE__, so
> > each BUILD_BUG_ON() gets a different function name and the correct
> > condition is printed:
> >
> > ./include/linux/compiler.h:350:38: error: call to ‘__compiletime_assert_0’ declared with attribute error: BUILD_BUG_ON failed: 1
> > _compiletime_assert(condition, msg, __compiletime_assert_, __COUNTER__)
> >
> > Cc: Daniel Santos <daniel.santos@...ox.com>
> > Cc: Rasmus Villemoes <linux@...musvillemoes.dk>
> > Cc: Masahiro Yamada <yamada.masahiro@...ionext.com>
> > Cc: Ian Abbott <abbotti@....co.uk>
> > Signed-off-by: Vegard Nossum <vegard.nossum@...cle.com>
> > ---
> > include/linux/compiler.h | 2 +-
> > 1 file changed, 1 insertion(+), 1 deletion(-)
> >
> > diff --git a/include/linux/compiler.h b/include/linux/compiler.h
> > index 5e88e7e33abec..034b0a644efcc 100644
> > --- a/include/linux/compiler.h
> > +++ b/include/linux/compiler.h
> > @@ -347,7 +347,7 @@ static inline void *offset_to_ptr(const int *off)
> > * compiler has support to do so.
> > */
> > #define compiletime_assert(condition, msg) \
> > - _compiletime_assert(condition, msg, __compiletime_assert_, __LINE__)
> > + _compiletime_assert(condition, msg, __compiletime_assert_, __COUNTER__)
> >
> > #define compiletime_assert_atomic_type(t) \
> > compiletime_assert(__native_word(t), \
>
> This will break builds using gcc 4.2 and earlier and the expectation was
> that you don't put two of them on the same line -- not helpful in macros
> where it all must be on the same line. Is gcc 4.2 still supported? If
> so, I recommend using another macro for the unique number that uses
> __COUNTER__ if available and __LINE__ otherwise. This was the decision
> for using __LINE__ when I wrote the original anyway.
>
> Also note that this construct:
>
> BUILD_BUG_ON_MSG(0, "I like chicken"); BUILD_BUG_ON_MSG(1, "I don't like
> chicken");
>
> will incorrectly claim that I like chicken. This is because of how
> __attribute__((error)) works -- gcc will use the first declaration to
> define the error message.
>
> I couple of years ago, I almost wrote a gcc extension to get rid of this
> whole mess and just __builtin_const_assert(cond, msg). Maybe I'll
> finish that this year.
>
> Daniel
No, GCC 4.6 is the minimum required version and it is highly likely that
the minimum version of GCC will be raised to 4.8 soon:
https://lore.kernel.org/lkml/20200123153341.19947-10-will@kernel.org/
https://git.kernel.org/peterz/queue/c/0e75b883b400ac4b1dafafe3cbd2e0a39b714232
Cheers,
Nathan
Powered by blists - more mailing lists