Add missing include in compiler.h
cover Add missing include in compiler.h
A compilation unit that only includes linux/string.h runs into an undefined BUILD_BUG_ON_ZERO(). Fixing that.
Philipp Reisner (1):
compiler.h: Add missing include statement for build_bug.h
include/linux/compiler.h | 1 + 1 file changed, 1 insertion(+)
Signature · 1 line
2.47.0
PATCH 1/1 compiler.h: Add missing include statement for build_bug.h
compiler.h defines __must_be_array() and __must_be_cstr() and both expand to BUILD_BUG_ON_ZERO(). build_bug.h defines BUILD_BUG_ON_ZERO(). So far compiler.h lacks to include build_bug.h.
Fix compiler.h by including build_bug.h. With that compiler.h and build_bug.h depend on each other.
--- include/linux/compiler.h | 1 + 1 file changed, 1 insertion(+)
include/linux/compiler.h +1 -0
@@ -3,6 +3,7 @@#define __LINUX_COMPILER_H#include <linux/compiler_types.h> +#include <linux/build_bug.h> /* for BUILD_BUG_ON_ZERO() */ #ifndef __ASSEMBLY__
Signature · 1 line
2.47.0
Re: [PATCH 1/1] compiler.h: Add missing include statement for build_bug.h
Quoted 9 lines · Philipp Reisner
compiler.hdefines__must_be_array()and__must_be_cstr()and both expand toBUILD_BUG_ON_ZERO().build_bug.hdefinesBUILD_BUG_ON_ZERO(). So farcompiler.hlacks to includebuild_bug.h.Fix
compiler.hby includingbuild_bug.h. With thatcompiler.handbuild_bug.hdepend on each other.Signed-off-by Philipp Reisner <[email protected]>---
What actually breaks? This commit is six years old. It's weird that we're only seeing build breakage now. Or did you just notice this while reviewing the code?
regards,
dan carpenter
Quoted 18 lines
include/linux/compiler.h | 1 + 1 file changed, 1 insertion(+)include/linux/compiler.h +1
-0@@ -3,6 +3,7 @@#define __LINUX_COMPILER_H#include <linux/compiler_types.h> +#include <linux/build_bug.h> /* for BUILD_BUG_ON_ZERO() */ #ifndef __ASSEMBLY__Signature · 1 line
2.47.0
Re: [PATCH 1/1] compiler.h: Add missing include statement for build_bug.h
Quoted 9 lines · Dan Carpenter
Quoted 7 lines · Philipp Reisner
compiler.hdefines__must_be_array()and__must_be_cstr()and both expand toBUILD_BUG_ON_ZERO().build_bug.hdefinesBUILD_BUG_ON_ZERO(). So farcompiler.hlacks to includebuild_bug.h.Fix
compiler.hby includingbuild_bug.h. With thatcompiler.handbuild_bug.hdepend on each other.
Quoted 9 lines
Quoted 2 lines
Signed-off-by Philipp Reisner <[email protected]>---
Fixes ec0bbef66f86 ("Compiler Attributes: homogenize __must_be_array")What actually breaks? This commit is six years old. It's weird that we're only seeing build breakage now. Or did you just notice this while reviewing the code?
I am working on a compilation unit that includes linux/string.h. Compiling it breaks when using strscp(). That is since commit commit 559048d156ff3391c4b793779a824c9193e20442
Author: Kees Cook <[email protected]> Date: Mon Aug 5 14:43:44 2024 -0700
Of course, my trivial workaround is including build_bug.h in my source; it is just not the proper way to fix this.
best regards,
Phil
Re: [PATCH 1/1] compiler.h: Add missing include statement for build_bug.h
Quoted 20 lines · Philipp Reisner
Quoted 9 lines · Dan Carpenter
Quoted 7 lines · Philipp Reisner
compiler.hdefines__must_be_array()and__must_be_cstr()and both expand toBUILD_BUG_ON_ZERO().build_bug.hdefinesBUILD_BUG_ON_ZERO(). So farcompiler.hlacks to includebuild_bug.h.Fix
compiler.hby includingbuild_bug.h. With thatcompiler.handbuild_bug.hdepend on each other.Quoted 9 lines
Quoted 2 lines
Signed-off-by Philipp Reisner <[email protected]>---
Fixes ec0bbef66f86 ("Compiler Attributes: homogenize __must_be_array")What actually breaks? This commit is six years old. It's weird that we're only seeing build breakage now. Or did you just notice this while reviewing the code?
Quoted 9 lines
I am working on a compilation unit that includes
linux/string.h. Compiling it breaks when usingstrscp(). That is since commit commit559048d156ff3391c4b793779a824c9193e20442Author: Kees Cook <[email protected]> Date: Mon Aug 5 14:43:44 2024 -0700Of course, my trivial workaround is including
build_bug.hin my source; it is just not the proper way to fix this.
Ah, okay. Thanks. I thought it might have broken in tree code.
regards,
dan carpenter
Re: [PATCH 1/1] compiler.h: Add missing include statement for build_bug.h
In reply to Philipp ReisnerQuoted 4 lines · Philipp Reisner
compiler.hdefines__must_be_array()and__must_be_cstr()and both expand toBUILD_BUG_ON_ZERO().build_bug.hdefinesBUILD_BUG_ON_ZERO(). So farcompiler.hlacks to includebuild_bug.h.
The bug is real, but..
Quoted 2 lines
Fix
compiler.hby includingbuild_bug.h. With thatcompiler.handbuild_bug.hdepend on each other.
I hate how compiler.h would include build_bug.h, which - on the very first line - then in turn includes compiler.h.
Does it *work*? Yes. The standard include guards stop the thing from recursing, and both headers only do create pre-processor defines, so the dependencies aren't ordered, but it's really really ugly to have these kinds of circular includes.
I think a better fix would be to not use BUILD_BUG_ON_ZERO() at all, but just use _Static_assert() directly here, to make <linux/compiler.h> be more self-sufficient.
The gcc docs say that __builtin_types_compatible_p() and __builtin_has_attribute() both return an integer constant expression, so that should be fine (the advantage of BUILD_BUG_ON_ZERO() is that it works in some contexts that aren't necessarily technically integer constant expressions - as long as they just evaluate to a constant).
We historically used to avoid _Static_assert(), but that was for historical reasons (ie it's one of those things that didn't exist back in the day..)
Hmm?
Linus
Re: [PATCH 1/1] compiler.h: Add missing include statement for build_bug.h
Quoted 4 lines · Linus Torvalds
I think a better fix would be to not use
BUILD_BUG_ON_ZERO()at all, but just use_Static_assert()directly here, to make <linux/compiler.h> be more self-sufficient.
Damn. We can't do that, because we typically use this in contexts that require us to return zero (because the assertion part of an expression), and then that whole expression needs to be a constant integer expression.
And because _Static_assert() isn't an expression, we'd need to wrap it with a statement expression or something.
And *hat* we can't do in arbitrary contexts.
Grr. I absolutely detest how bad the standard tools are. It's kind of sad how we need to build our own hacky BUILD_BUG_ON() to do this.
There's probably some trick I'm missing, but yeah, it looks like we need our BUILD_BUG_ON_ZERO() thing with that crazy bitfield hack.
Linus
Re: [PATCH 1/1] compiler.h: Add missing include statement for build_bug.h
Quoted 3 lines · Linus Torvalds
There's probably some trick I'm missing, but yeah, it looks like we need our
BUILD_BUG_ON_ZERO()thing with that crazy bitfield hack.
.. and right after sending that, I figured out the trick.
You can use 'static_assert()' inside a type definition.
So the way to turn it into an expression is to just use the same 'sizeof(empty struct)' trick that we use for BUILD_BUG_ON_ZERO:
#define Static_assert(a,msg) \
sizeof(struct{_Static_assert(a,msg);})works as a way to make _Static_assert() be an expression. What a horrid hack.
I don't know if this is worth it, but it does at least have the advantage of having a message, so that the error case can explain itself rather than get that odd
"negative width in bit-field"
error message.
I dunno.
Linus
PATCH 1/1 compiler.h: Fix undefined BUILD_BUG_ON_ZERO()
<linux/compiler.h> defines __must_be_array() and __must_be_cstr() and both expand to BUILD_BUG_ON_ZERO(), but <linux/build_bug.h> defines BUILD_BUG_ON_ZERO(). Including <linux/build_bug.h> in <linux/compiler.h> would create a cyclic dependency as <linux/build_bug.h> already includes <linux/compiler.h>.
Fix that by defining __BUILD_BUG_ON_ZERO_MSG() in <linux/compiler.h> and using that for __must_be_array() and __must_be_cstr().
--- include/linux/compiler.h | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-)
include/linux/compiler.h +9 -2
@@ -239,11 +239,18 @@ static inline void *offset_to_ptr(const int *off)#endif /* __ASSEMBLY__ */ +#ifdef __CHECKER__ +#define __BUILD_BUG_ON_ZERO_MSG (e, msg) (0) +#else /* __CHECKER__ */ +#define __BUILD_BUG_ON_ZERO_MSG (e, msg) ((int)sizeof(struct {_Static_assert(!(e), msg);})) +#endif /* __CHECKER__ */ +/* &a[0] degrades to a pointer: a different type from an array */ -#define __must_be_array (a) BUILD_BUG_ON_ZERO(__same_type((a), &(a)[0])) +#define __must_be_array (a) __BUILD_BUG_ON_ZERO_MSG(__same_type((a), &(a)[0]), "must be array")/* Require C Strings (i.e. NUL-terminated) lack the "nonstring" attribute. */ -#define __must_be_cstr (p) BUILD_BUG_ON_ZERO(__annotated(p, nonstring)) +#define __must_be_cstr (p) \ + __BUILD_BUG_ON_ZERO_MSG(__annotated(p, nonstring), "must be cstr (NUL-terminated)")/* * This returns a constant expression while determining if an argument is
Signature · 1 line
2.47.0
Re: [PATCH] compiler.h: Fix undefined BUILD_BUG_ON_ZERO()
Quoted 10 lines · Philipp Reisner
<
linux/compiler.h> defines__must_be_array()and__must_be_cstr()and both expand toBUILD_BUG_ON_ZERO(), but <linux/build_bug.h> definesBUILD_BUG_ON_ZERO(). Including <linux/build_bug.h> in <linux/compiler.h> would create a cyclic dependency as <linux/build_bug.h> already includes <linux/compiler.h>.Fix that by defining
__BUILD_BUG_ON_ZERO_MSG()in <linux/compiler.h> and using that for__must_be_array()and__must_be_cstr().Signed-off-by Philipp Reisner <[email protected]>
Thanks for finding a simple way to make this work sanely. :)
Linus, do you want a PR for this, or will you apply it directly?
Thanks!
-Kees
Quoted 32 lines
--- include/linux/compiler.h | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-)include/linux/compiler.h +9
-2@@ -239,11 +239,18 @@ static inline void *offset_to_ptr(const int *off)#endif /* __ASSEMBLY__ */ +#ifdef __CHECKER__ +#define __BUILD_BUG_ON_ZERO_MSG (e, msg) (0) +#else /* __CHECKER__ */ +#define __BUILD_BUG_ON_ZERO_MSG (e, msg) ((int)sizeof(struct {_Static_assert(!(e), msg);})) +#endif /* __CHECKER__ */ +/* &a[0] degrades to a pointer: a different type from an array */ -#define __must_be_array (a) BUILD_BUG_ON_ZERO(__same_type((a), &(a)[0])) +#define __must_be_array (a) __BUILD_BUG_ON_ZERO_MSG(__same_type((a), &(a)[0]), "must be array")/* Require C Strings (i.e. NUL-terminated) lack the "nonstring" attribute. */ -#define __must_be_cstr (p) BUILD_BUG_ON_ZERO(__annotated(p, nonstring)) +#define __must_be_cstr (p) \ + __BUILD_BUG_ON_ZERO_MSG(__annotated(p, nonstring), "must be cstr (NUL-terminated)")/* * This returns a constant expression while determining if an argument is Signature · 1 line
2.47.0
Signature · 1 line
Kees Cook
Re: [PATCH] compiler.h: Fix undefined BUILD_BUG_ON_ZERO()
Quoted 2 lines · Kees Cook
Linus, do you want a PR for this, or will you apply it directly?
Well, I'm certainly not applying it this late for 6.12 - who knows what compiler issues it can trigger - and for the merge window I will have forgotten it.
So put it in your tree and have it go through linux-next to see that it's ok. It *looks* fine to me, but...
Linus
Re: [PATCH] compiler.h: Fix undefined BUILD_BUG_ON_ZERO()
In reply to Philipp ReisnerQuoted 3 lines · Philipp Reisner
Fix that by defining
__BUILD_BUG_ON_ZERO_MSG()in <linux/compiler.h> and using that for__must_be_array()and__must_be_cstr().
Ack, that cast to 'int' seems good too, to make sure the __BUILD_BUG_ON_ZERO_MSG() test doesn't unintentionally change the type of the expression it is in.
I do wonder if we actually need that "#ifdef __CHECKER__"? I think sparse is perfectly fine with a _Static_assert(). Or does the checking cause some other issues?
Linus
Kees Cook
AppliedRe: [PATCH] compiler.h: Fix undefined BUILD_BUG_ON_ZERO()
In reply to Philipp ReisnerQuoted 10 lines · Philipp Reisner
<
linux/compiler.h> defines__must_be_array()and__must_be_cstr()and both expand toBUILD_BUG_ON_ZERO(), but <linux/build_bug.h> definesBUILD_BUG_ON_ZERO(). Including <linux/build_bug.h> in <linux/compiler.h> would create a cyclic dependency as <linux/build_bug.h> already includes <linux/compiler.h>.Fix that by defining
__BUILD_BUG_ON_ZERO_MSG()in <linux/compiler.h> and using that for__must_be_array()and__must_be_cstr().[...]
Applied to for-next/hardening, thanks!
[1/1] compiler.h: Fix undefined BUILD_BUG_ON_ZERO()
https://git.kernel.org/kees/c/d7a516c6eeae
Take care,
Signature · 1 line
Kees Cook