add QEMU_WARN_NONNULL_ARGS() macro
cover add QEMU_WARN_NONNULL_ARGS() macro
Some old PoC series I remember after reading
http://lists.nongnu.org/archive/html/qemu-devel/2018-01/msg03545.html
I had few more changes but then I found the code was harder to read so I didn't continue further.
Only 2 patches are included as example.
This might still be useful in few cases, so I'm still sending as RFC to have different thoughts.
This macro is, however, helpful to the Clang static analizer (reducing false positive).
BTW another useful macro for the static analizer I used is:
#define QEMU_FALLTHROUGH __attribute__((fallthrough))
It replaces the /* fall through */ comment, i.e.:
switch (rap) {
case BCR_SWS:
if (!(CSR_STOP(s) || CSR_SPND(s)))
return;
val &= ~0x0300;
QEMU_FALLTHROUGH;
case BCR_LNKST:
case BCR_LED1:
case BCR_LED2:
case BCR_LED3:
case BCR_MC:
case BCR_FDC:
case BCR_BSBC:
case BCR_EECAS:
case BCR_PLAT:
s->bcr[rap] = val;
break;Regards,
Phil.
Philippe Mathieu-Daudé (3):
compiler: add QEMU_WARN_NONNULL_ARGS()
virtio: let virtio_add/clear_feature() use QEMU_WARN_NONNULL_ARGS()
utils: let qemu_find_file() use QEMU_WARN_NONNULL_ARGS()
include/hw/virtio/virtio.h | 2 ++ include/qemu-common.h | 2 +- include/qemu/compiler.h | 2 ++ 3 files changed, 5 insertions(+), 1 deletion(-)
Signature · 1 line
2.15.1
RFC PATCH 1/3 compiler: add QEMU_WARN_NONNULL_ARGS()
--- include/qemu/compiler.h | 2 ++ 1 file changed, 2 insertions(+)
include/qemu/compiler.h +2 -0
@@ -26,6 +26,8 @@#define QEMU_WARN_UNUSED_RESULT __attribute__((warn_unused_result)) +#define QEMU_WARN_NONNULL_ARGS(args...) __attribute__((nonnull(args))) +#define QEMU_SENTINEL __attribute__((sentinel))#if QEMU_GNUC_PREREQ (4 ,3 )
Signature · 1 line
2.15.1
Re: [Qemu-devel] [RFC PATCH 1/3] compiler: add QEMU_WARN_NONNULL_ARGS()
Quoted 10 lines · Philippe Mathieu-Daudé
Signed-off-by Philippe Mathieu-Daudé <[email protected]>--- include/qemu/compiler.h | 2 ++ 1 file changed, 2 insertions(+)include/qemu/compiler.h +0
-0
include/qemu/compiler.h line 29
Quoted 4 lines
include/qemu/compiler.h +1
-0@@ -26,6 +26,8 @@#define QEMU_WARN_UNUSED_RESULT __attribute__((warn_unused_result)) +#define QEMU_WARN_NONNULL_ARGS(args...) __attribute__((nonnull(args)))
If we take this, it should come with a warning attached to it, because it has really nasty behaviour with GCC. Consider code like:
void foo(void *bar) __attribute__((nonnull(1)));
...
void foo(void *bar) { if (!bar) return; }
GCC may or may not warn you about passing NULL for the 'bar' parameter, but it will none the less assume nothing passes NULL, and thus remove the 'if (!bar)' conditional during optimization. IOW, adding nonnull annotations can actually make your code less robust :-(
After having a number of crashes in libvirt caused by gcc optimizing out checks for NULL, we now only define nonnull when running under static analysis (coverity) and not when compiling normally.
The 2 functions you've added nonnull attrs to look safe enough, but people might unwittingly use this elsewhere in QEMU in future not realizing the side-effect it has.
Regards,
Daniel
Signature · 3 lines
|: https://berrange.com -o- https://www.flickr.com/photos/dberrange :| |: https://libvirt.org -o- https://fstop138.berrange.com :| |: https://entangle-photo.org -o- https://www.instagram.com/dberrange :|
Re: [Qemu-devel] [RFC PATCH 1/3] compiler: add QEMU_WARN_NONNULL_ARGS()
Quoted 23 lines · Daniel P. Berrange
Quoted 10 lines · Philippe Mathieu-Daudé
Signed-off-by Philippe Mathieu-Daudé <[email protected]>--- include/qemu/compiler.h | 2 ++ 1 file changed, 2 insertions(+)include/qemu/compiler.h +0
-0include/qemu/compiler.h line 29
Quoted 4 lines
include/qemu/compiler.h +1
-0@@ -26,6 +26,8 @@#define QEMU_WARN_UNUSED_RESULT __attribute__((warn_unused_result)) +#define QEMU_WARN_NONNULL_ARGS(args...) __attribute__((nonnull(args)))If we take this, it should come with a warning attached to it, because it has really nasty behaviour with GCC. Consider code like:
void foo(void *bar) __attribute__((nonnull(1)));...
Quoted 7 lines
void
foo(void *bar){ if (!bar) return; }GCC may or may not warn you about passing NULL for the 'bar' parameter, but it will none the less assume nothing passes NULL, and thus remove the 'if (!bar)' conditional during optimization. IOW, adding nonnull annotations can actually make your code less robust :-(
TIL!
Quoted 6 lines
After having a number of crashes in libvirt caused by gcc optimizing out checks for NULL, we now only define nonnull when running under static analysis (coverity) and not when compiling normally.
Why do you use __attribute__(()) ? Isn't this enough:
#if defined __clang_analyzer__ || defined __COVERITY__ #define QEMU_STATIC_ANALYSIS 1
+#define QEMU_WARN_NONNULL_ARGS(args...) __attribute__((nonnull(args))) +#else
+#define QEMU_WARN_NONNULL_ARGS(args...)
#endif
Quoted 7 lines
The 2 functions you've added nonnull attrs to look safe enough, but people might unwittingly use this elsewhere in QEMU in future not realizing the side-effect it has.
Regards,
Daniel
Re: [Qemu-devel] [RFC PATCH 1/3] compiler: add QEMU_WARN_NONNULL_ARGS()
Quoted 34 lines · Philippe Mathieu-Daudé
Quoted 23 lines · Daniel P. Berrange
Quoted 10 lines · Philippe Mathieu-Daudé
Signed-off-by Philippe Mathieu-Daudé <[email protected]>--- include/qemu/compiler.h | 2 ++ 1 file changed, 2 insertions(+)include/qemu/compiler.h +0
-0include/qemu/compiler.h line 29
Quoted 4 lines
include/qemu/compiler.h +1
-0@@ -26,6 +26,8 @@#define QEMU_WARN_UNUSED_RESULT __attribute__((warn_unused_result)) +#define QEMU_WARN_NONNULL_ARGS(args...) __attribute__((nonnull(args)))If we take this, it should come with a warning attached to it, because it has really nasty behaviour with GCC. Consider code like:
void foo(void *bar) __attribute__((nonnull(1)));...
Quoted 7 lines
void
foo(void *bar){ if (!bar) return; }GCC may or may not warn you about passing NULL for the 'bar' parameter, but it will none the less assume nothing passes NULL, and thus remove the 'if (!bar)' conditional during optimization. IOW, adding nonnull annotations can actually make your code less robust :-(
TIL!
Quoted 8 lines
Quoted 6 lines
After having a number of crashes in libvirt caused by gcc optimizing out checks for NULL, we now only define nonnull when running under static analysis (coverity) and not when compiling normally.
Why do you use __attribute__(()) ? Isn't this enough:
No idea offhand - Eric wrote this so perhaps he had a reason for that else branch style.
Quoted 11 lines
#if defined
__clang_analyzer__|| defined__COVERITY__#defineQEMU_STATIC_ANALYSIS1(quoted diff) +3
-0+#define QEMU_WARN_NONNULL_ARGS(args...) __attribute__((nonnull(args))) +#else +#define QEMU_WARN_NONNULL_ARGS(args...)#endif
Quoted 3 lines
The 2 functions you've added nonnull attrs to look safe enough, but people might unwittingly use this elsewhere in QEMU in future not realizing the side-effect it has.
Regards,
Daniel
Signature · 3 lines
|: https://berrange.com -o- https://www.flickr.com/photos/dberrange :| |: https://libvirt.org -o- https://fstop138.berrange.com :| |: https://entangle-photo.org -o- https://www.instagram.com/dberrange :|
Re: [Qemu-devel] [RFC PATCH 1/3] compiler: add QEMU_WARN_NONNULL_ARGS()
Quoted 6 lines · Daniel P. Berrange
Quoted 6 lines
Quoted 6 lines
GCC may or may not warn you about passing NULL for the 'bar' parameter, but it will none the less assume nothing passes NULL, and thus remove the 'if (!bar)' conditional during optimization. IOW, adding nonnull annotations can actually make your code less robust :-(
The gcc bug mentioned in the libvirt code says that newer gcc may be smarter than when we added the libvirt workaround:
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=17308
But I haven't played with it in a long time (the sour experience with misoptimized code with no warning has made me wary of re-trying).
Quoted 7 lines
Quoted 2 lines
Why do you use __attribute__(()) ? Isn't this enough:
No idea offhand - Eric wrote this so perhaps he had a reason for that else branch style.
Quoted 1 line
Quoted 6 lines
Quoted 6 lines
#if defined
__clang_analyzer__|| defined__COVERITY__#defineQEMU_STATIC_ANALYSIS1(quoted diff) +3
-0+#define QEMU_WARN_NONNULL_ARGS(args...) __attribute__((nonnull(args))) +#else +#define QEMU_WARN_NONNULL_ARGS(args...)#endif
My reasoning for using the empty attribute was to at least ensure that the gcc compilation agrees that the annotation is syntactically valid (nothing worse than sticking an __attribute__ code in a place that gcc doesn't like, but only for static checker builds, so you don't learn about it right away). Defining the macro to nothing, rather than an empty attribute, makes it harder for the common-case compilation to detect placement problems.
Signature · 3 lines
Eric Blake, Principal Software Engineer Red Hat, Inc. +1-919-301-3266 Virtualization: qemu.org | libvirt.org
Re: [Qemu-devel] [RFC PATCH 1/3] compiler: add QEMU_WARN_NONNULL_ARGS()
Quoted 9 lines · Eric Blake
Quoted 6 lines · Daniel P. Berrange
Quoted 6 lines
Quoted 6 lines
GCC may or may not warn you about passing NULL for the 'bar' parameter, but it will none the less assume nothing passes NULL, and thus remove the 'if (!bar)' conditional during optimization. IOW, adding nonnull annotations can actually make your code less robust :-(
Quoted 6 lines
The gcc bug mentioned in the libvirt code says that newer gcc may be smarter than when we added the libvirt workaround:
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=17308But I haven't played with it in a long time (the sour experience with misoptimized code with no warning has made me wary of re-trying).
I have been there...
Quoted 7 lines
Quoted 7 lines
Quoted 2 lines
Why do you use __attribute__(()) ? Isn't this enough:
No idea offhand - Eric wrote this so perhaps he had a reason for that else branch style.
Quoted 1 line
Quoted 14 lines
Quoted 6 lines
Quoted 6 lines
#if defined
__clang_analyzer__|| defined__COVERITY__#defineQEMU_STATIC_ANALYSIS1(quoted diff) +3
-0+#define QEMU_WARN_NONNULL_ARGS(args...) __attribute__((nonnull(args))) +#else +#define QEMU_WARN_NONNULL_ARGS(args...)#endif
My reasoning for using the empty attribute was to at least ensure that the gcc compilation agrees that the annotation is syntactically valid (nothing worse than sticking an
__attribute__code in a place that gcc doesn't like, but only for static checker builds, so you don't learn about it right away). Defining the macro to nothing, rather than an empty attribute, makes it harder for the common-case compilation to detect placement problems.
I wouldn't have thought of that, thanks!
Phil.
RFC PATCH 2/3 virtio: let virtio_add/clear_feature() use QEMU_WARN_NONNULL_ARGS()
In reply to Philippe Mathieu-Daudé--- include/hw/virtio/virtio.h | 2 ++ 1 file changed, 2 insertions(+)
include/hw/virtio/virtio.h +2 -0
@@ -292,12 +292,14 @@ void virtio_queue_aio_set_host_notifier_handler(VirtQueue *vq, AioContext *ctx,VirtQueue *virtio_vector_first_queue (VirtIODevice *vdev,uint16_t vector);VirtQueue *virtio_vector_next_queue (VirtQueue *vq); +QEMU_WARN_NONNULL_ARGS(1)static inline void virtio_add_feature (uint64_t *features,unsigned int fbit) {assert (fbit <64 ); *features |= (1ULL << fbit); } +QEMU_WARN_NONNULL_ARGS(1)static inline void virtio_clear_feature (uint64_t *features,unsigned int fbit) {assert (fbit <64 );
Signature · 1 line
2.15.1
RFC PATCH 3/3 utils: let qemu_find_file() use QEMU_WARN_NONNULL_ARGS()
In reply to Philippe Mathieu-Daudé--- include/qemu-common.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
include/qemu-common.h +1 -1
@@ -134,7 +134,7 @@ const char *qemu_get_vm_name(void);#define QEMU_FILE_TYPE_BIOS 0#define QEMU_FILE_TYPE_KEYMAP 1 -char *qemu_find_file (int type,const char *name); +char *qemu_find_file (int type,const char *name)QEMU_WARN_NONNULL_ARGS (2 );/* OS specific functions */ void os_setup_early_signal_handling (void );
Signature · 1 line
2.15.1
Quoted 10 lines · Philippe Mathieu-Daudé
Some old PoC series I remember after reading
http://lists.nongnu.org/archive/html/qemu-devel/2018-01/msg03545.htmlI had few more changes but then I found the code was harder to read so I didn't continue further.
Only 2 patches are included as example.This might still be useful in few cases, so I'm still sending as RFC to have different thoughts.
Quoted 6 lines
This macro is, however, helpful to the Clang static analizer (reducing false positive).
BTW another useful macro for the static analizer I used is:
#define QEMU_FALLTHROUGH __attribute__((fallthrough))
and:
#define QEMU_STATIC_ANALYSIS_ASSERT(expression) assert(expression)
i.e.:
linux-user/syscall.c:5581:9: warning: Dereference of undefined pointer value
if (*host_rt_dev_ptr != 0) {
^~~~~~~~~~~~~~~~This can safely be silenced using:
unlock_user(argptr, arg, 0);
+ QEMU_STATIC_ANALYSIS_ASSERT(host_rt_dev_ptr);
ret = get_errno(safe_ioctl(fd, ie->host_cmd, buf_temp));
if (*host_rt_dev_ptr != 0) {
...Or:
target/mips/msa_helper.c:1008:23: warning: The result of the '<<' expression is undefined
int64_t r_bit = 1 << (DF_BITS(df) - 2);
~~^~~~~~~~~~~~~~~~~~~~Using:
static inline int64_t msa_mulr_q_df(uint32_t df, int64_t arg1, int64_t arg2)
{
int64_t q_min = DF_MIN_INT(df);
int64_t q_max = DF_MAX_INT(df);- int64_t r_bit = 1 << (DF_BITS(df) - 2);
+ int64_t r_bit;
+ QEMU_STATIC_ANALYSIS_ASSERT(DF_BITS(df) < 32);
+ r_bit = 1 << (DF_BITS(df) - 2);
Similar:
target/arm/helper.c:4283:25: warning: The result of the '>>' expression is undefined
len = cto32(bas >> basstart);
~~~~^~~~~~~~~~~Using:
basstart = ctz32(bas);
+QEMU_STATIC_ANALYSIS_ASSERT(basstart <= 8); /* bas is at most 8-bit */
len = cto32(bas >> basstart);
Another:
monitor.c:1481:26: warning: Access to field 'name' results in a dereference of a null pointer (loaded from variable 'mr')
addr, mr->name, ptr);
^~~~~~~~Using:
if (local_err) {
error_report_err(local_err);
return;
+ } else {
+ QEMU_STATIC_ANALYSIS_ASSERT(ptr != NULL);
}
physaddr = vtop(ptr, &local_err);
if (local_err) {
error_report_err(local_err);
} else {
monitor_printf(mon, "Host physical address for 0x%" HWADDR_PRIx
" (%s) is 0x%" PRIx64 "\n",
addr, mr->name, (uint64_t) physaddr);etc..
Too many false positive leads to unused report,
but personally I still prefer readable code.
Quoted 35 lines
It replaces the /* fall through */ comment, i.e.:
switch (rap) { case BCR_SWS: if (!(CSR_STOP(s) || CSR_SPND(s))) return; val &= ~0x0300; QEMU_FALLTHROUGH; case BCR_LNKST: case BCR_LED1: case BCR_LED2: case BCR_LED3: case BCR_MC: case BCR_FDC: case BCR_BSBC: case BCR_EECAS: case BCR_PLAT: s->bcr[rap] = val; break;Regards,
Phil.
Philippe Mathieu-Daudé (3):
compiler: addQEMU_WARN_NONNULL_ARGS()
virtio: let virtio_add/clear_feature() useQEMU_WARN_NONNULL_ARGS()
utils: letqemu_find_file()useQEMU_WARN_NONNULL_ARGS()include/hw/virtio/virtio.h | 2 ++ include/qemu-common.h | 2 +- include/qemu/compiler.h | 2 ++ 3 files changed, 5 insertions(+), 1 deletion(-)
Quoted 5 lines · Philippe Mathieu-Daudé
BTW another useful macro for the static analizer I used is:
#define QEMU_FALLTHROUGH __attribute__((fallthrough))It replaces the /* fall through */ comment, i.e.:
That's unfortunate. Does it help if you use the actual lint spelling of /* FALLTHRU */?
r~
Quoted 9 lines · Richard Henderson
Quoted 5 lines · Philippe Mathieu-Daudé
BTW another useful macro for the static analizer I used is:
#define QEMU_FALLTHROUGH __attribute__((fallthrough))It replaces the /* fall through */ comment, i.e.:
That's unfortunate. Does it help if you use the actual lint spelling of /* FALLTHRU */?
New gcc has this:
'-Wimplicit-fallthrough=N'
Warn when a switch case falls through. For example:
...
Since there are occasions where a switch case fall through is desirable, GCC provides an attribute, '__attribute__ ((fallthrough))', that is to be used along with a null statement to suppress this warning that would normally occur:
switch (cond)
{
case 1:
bar (0);
__attribute__ ((fallthrough));
default:
...
}
C++17 provides a standard way to suppress the '-Wimplicit-fallthrough' warning using '[[fallthrough]];' instead of the GNU attribute. In C++11 or C++14 users can use '[[gnu::fallthrough]];', which is a GNU extension. Instead of the these attributes, it is also possible to add a fallthrough comment to silence the warning. The whole body of the C or C++ style comment should match the given regular expressions listed below.
The option argument N specifies what kind of comments are accepted:
* '-Wimplicit-fallthrough=0' disables the warning altogether.
* '-Wimplicit-fallthrough=1' matches '.*' regular expression, any comment is used as fallthrough comment.
* '-Wimplicit-fallthrough=2' case insensitively matches '.*falls?[ \t-]*thr(ough|u).*' regular expression.
* '-Wimplicit-fallthrough=3' case sensitively matches one of the following regular expressions:
* '-fallthrough'
* '@fallthrough@'
* 'lint -fallthrough[ \t]*'
* '[ \t.!]*(ELSE,? |INTENTIONAL(LY)? )? FALL(S | |-)?THR(OUGH|U)[ \t.!]*(-[^\n\r]*)?'
* '[ \t.!]*(Else,? |Intentional(ly)? )? Fall((s | |-)[Tt]|t)hr(ough|u)[ \t.!]*(-[^\n\r]*)?'
* '[ \t.!]*([Ee]lse,? |[Ii]ntentional(ly)? )? fall(s | |-)?thr(ough|u)[ \t.!]*(-[^\n\r]*)?'
* '-Wimplicit-fallthrough=4' case sensitively matches one of the following regular expressions:
* '-fallthrough'
* '@fallthrough@'
* 'lint -fallthrough[ \t]*'
* '[ \t]*FALLTHR(OUGH|U)[ \t]*'
* '-Wimplicit-fallthrough=5' doesn't recognize any comments as fallthrough comments, only attributes disable the warning.
The comment needs to be followed after optional whitespace and other comments by 'case' or 'default' keywords or by a user label that precedes some 'case' or 'default' label.
switch (cond)
{
case 1:
bar (0);
/* FALLTHRU */
default:
...
}
The '-Wimplicit-fallthrough=3' warning is enabled by '-Wextra'.
No thanks to level 5, these days, you HAVE to use a macro that expands to the attribute and/or C17 spelling, rather than relying on the lint spelling, if you cannot control what level a user will request; although with level 3, the lint spelling of a comment still works. I'm not sure which static analyzers recognize which other spellings; which is why a macro that consistently expands to the same string known to work, rather than 20 different ad hoc comments (many of which work, but auditing that all of them work is a bigger task), may be worthwhile.
Signature · 3 lines
Eric Blake, Principal Software Engineer Red Hat, Inc. +1-919-301-3266 Virtualization: qemu.org | libvirt.org