forked from torvalds/linux
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Improve BUILD_BUG_ON error message with clang #1173
Labels
feature-request
Not a bug per-se
[FIXED][LINUX] 5.15
This bug was fixed in Linux 5.15
[FIXED][LLVM] 14
This bug was fixed in LLVM 14.x
Reported upstream
This bug was filed on LLVM’s issue tracker, Phabricator, or the kernel mailing list.
Comments
|
cc @arndb |
|
See also: https://llvm.org/pr16428 |
|
I also have this diff to send to LKML: diff --git a/include/linux/compiler-gcc.h b/include/linux/compiler-gcc.h
index cb9217fc60af..4ee87cbe7208 100644
--- a/include/linux/compiler-gcc.h
+++ b/include/linux/compiler-gcc.h
@@ -44,7 +44,6 @@
#define __compiletime_object_size(obj) __builtin_object_size(obj, 0)
#define __compiletime_warning(message) __attribute__((__warning__(message)))
-#define __compiletime_error(message) __attribute__((__error__(message)))
#if defined(LATENT_ENTROPY_PLUGIN) && !defined(__CHECKER__)
#define __latent_entropy __attribute__((latent_entropy))
diff --git a/include/linux/compiler_attributes.h b/include/linux/compiler_attributes.h
index 2487be0e7199..fcdcb711d715 100644
--- a/include/linux/compiler_attributes.h
+++ b/include/linux/compiler_attributes.h
@@ -137,6 +137,12 @@
# define __designated_init
#endif
+#if __has_attribute(__error__)
+# define __compiletime_error(msg) __attribute__((__error__(msg)))
+#else
+# define __compiletime_error(msg)
+#endif
+
/*
* Optional: not supported by clang
*
diff --git a/include/linux/compiler_types.h b/include/linux/compiler_types.h
index e4ea86fc584d..cee45de0f61b 100644
--- a/include/linux/compiler_types.h
+++ b/include/linux/compiler_types.h
@@ -297,9 +297,6 @@ struct ftrace_likely_data {
#ifndef __compiletime_warning
# define __compiletime_warning(message)
#endif
-#ifndef __compiletime_error
-# define __compiletime_error(message)
-#endif
#ifdef __OPTIMIZE__
# define __compiletime_assert(condition, msg, prefix, suffix) \ |
fengguang
pushed a commit
to 0day-ci/linux
that referenced
this issue
Jul 20, 2021
I'm working on adding support for __attribute__((__error__(""))) and
__attribute__((__warning__(""))) to Clang. To make use of these in
__compiletime_error and __compiletime_warning (as used by BUILD_BUG and
friends) for newer clang and detect/fallback for older versions of
clang, move these to compiler_attributes.h and guard them with
__has_attribute preprocessor guards.
Link: https://reviews.llvm.org/D106030
Link: https://bugs.llvm.org/show_bug.cgi?id=16428
Link: ClangBuiltLinux#1173
Signed-off-by: Nick Desaulniers <ndesaulniers@google.com>
fengguang
pushed a commit
to 0day-ci/linux
that referenced
this issue
Aug 2, 2021
I'm working on adding support for __attribute__((__error__(""))) and
__attribute__((__warning__(""))) to Clang. To make use of these in
__compiletime_error and __compiletime_warning (as used by BUILD_BUG and
friends) for newer clang and detect/fallback for older versions of
clang, move these to compiler_attributes.h and guard them with
__has_attribute preprocessor guards.
Link: https://reviews.llvm.org/D106030
Link: https://bugs.llvm.org/show_bug.cgi?id=16428
Link: ClangBuiltLinux#1173
Signed-off-by: Nick Desaulniers <ndesaulniers@google.com>
nickdesaulniers
added a commit
to llvm/llvm-project
that referenced
this issue
Aug 25, 2021
Add support for the GNU C style __attribute__((error(""))) and
__attribute__((warning(""))). These attributes are meant to be put on
declarations of functions whom should not be called.
They are frequently used to provide compile time diagnostics similar to
_Static_assert, but which may rely on non-ICE conditions (ie. relying on
compiler optimizations). This is also similar to diagnose_if function
attribute, but can diagnose after optimizations have been run.
While users may instead simply call undefined functions in such cases to
get a linkage failure from the linker, these provide a much more
ergonomic and actionable diagnostic to users and do so at compile time
rather than at link time. Users instead may be able use inline asm .err
directives.
These are used throughout the Linux kernel in its implementation of
BUILD_BUG and BUILD_BUG_ON macros. These macros generally cannot be
converted to use _Static_assert because many of the parameters are not
ICEs. The Linux kernel still needs to be modified to make use of these
when building with Clang; I have a patch that does so I will send once
this feature is landed.
To do so, we create a new IR level Function attribute, "dontcall" (both
error and warning boil down to one IR Fn Attr). Then, similar to calls
to inline asm, we attach a !srcloc Metadata node to call sites of such
attributed callees.
The backend diagnoses these during instruction selection, while we still
know that a call is a call (vs say a JMP that's a tail call) in an arch
agnostic manner.
The frontend then reconstructs the SourceLocation from that Metadata,
and determines whether to emit an error or warning based on the callee's
attribute.
Link: https://bugs.llvm.org/show_bug.cgi?id=16428
Link: ClangBuiltLinux/linux#1173
Reviewed By: aaron.ballman
Differential Revision: https://reviews.llvm.org/D106030
|
fixed in llvm-14:
Let's wait to close this out though until @ojeda picks up the kernel patch (with suggestion to s/clang-13/clang-14/) then that hits mainline. |
ojeda
pushed a commit
to ojeda/linux
that referenced
this issue
Sep 9, 2021
Clang 14 will add support for __attribute__((__error__(""))) and
__attribute__((__warning__(""))). To make use of these in
__compiletime_error and __compiletime_warning (as used by BUILD_BUG and
friends) for newer clang and detect/fallback for older versions of
clang, move these to compiler_attributes.h and guard them with
__has_attribute preprocessor guards.
Link: https://reviews.llvm.org/D106030
Link: https://bugs.llvm.org/show_bug.cgi?id=16428
Link: ClangBuiltLinux#1173
Signed-off-by: Nick Desaulniers <ndesaulniers@google.com>
Reviewed-by: Nathan Chancellor <nathan@kernel.org>
Reviewed-by: Kees Cook <keescook@chromium.org>
[Reworded, landed in Clang 14]
Signed-off-by: Miguel Ojeda <ojeda@kernel.org>
|
b83a908 |
|
Awesome! I can confirm this works as expected for the FORTIFY_SOURCE warnings: |
3 tasks
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Labels
feature-request
Not a bug per-se
[FIXED][LINUX] 5.15
This bug was fixed in Linux 5.15
[FIXED][LLVM] 14
This bug was fixed in LLVM 14.x
Reported upstream
This bug was filed on LLVM’s issue tracker, Phabricator, or the kernel mailing list.
pcc commentedOct 9, 2020
With clang
BUILD_BUG_ONfailures only appear at link time due to a lack of support for__attribute__((error)). The error messages look like this:and it can be hard to trace them back to the caller.
I had the idea of putting the error message in the symbol name so that it survives until link time. Here's a quick proof of concept:
With that and an artificial
BUILD_BUG_ONfailure injected into the kernel:the error message looks like this:
Not perfect but at least you can see the error message. It does require
LLVM_IAS=1because GNU as doesn't like these weird symbol names. I don't know if the kernel folks will go for this though.The text was updated successfully, but these errors were encountered: