Skip to contentSkip to conversation

Add QEMU_WARN_UNUSED_RESULT function attribute

PATCH In review Manos Pitsidianakis 5 participants 15 messages Nov 10, 2023

lore
Share & tools
lore
#0 lore raw

cover Add QEMU_WARN_UNUSED_RESULT function attribute

Functions that return a value to indicate success or failure can be decorated with the warn_unused_result attribute. GCC will stop compilation if a caller does not check the return value after calling such a function. This was not possible to spot statically before, but Coverity detects this kind of bug. I was prompted by

https://lore.kernel.org/qemu-devel/CAFEAcA_TS-B0gc-DUYT6BaKnm8Uauhsx3rW2dmVNUgTToVjSJg@mail.gmail.com/

to prevent this from happening in the future.

This patch series depends on <[email protected]>

https://lore.kernel.org/qemu-devel/[email protected]/

Manos Pitsidianakis (2):
Add QEMU_WARN_UNUSED_RESULT attribute Add warn_unused_result attr to AUD_register_card

 audio/audio.h           |  2 +-
 hw/arm/omap2.c          |  8 +++++++-
 hw/input/tsc210x.c      |  8 +++++++-
 include/qemu/compiler.h | 14 ++++++++++++++
 4 files changed, 29 insertions(+), 3 deletions(-)

base-commit: ad6ef0a42e314a8c6ac6c96d5f6e607a1e5644b5
prerequisite-patch-id: 484ec9f7f6109c10d4be0484fe8e3c2550c415f4

Signature · 1 line
2.39.2
#1 lore raw

PATCH 1/2 Add QEMU_WARN_UNUSED_RESULT attribute

This commit adds QEMU_WARN_UNUSED_RESULT, a macro for the gcc function attribute warn_unused_result. The utility of this attribute is to ensure functions that return values that need to be inspected are not ignored by the caller.

---
 include/qemu/compiler.h | 14 ++++++++++++++
 1 file changed, 14 insertions(+)
include/qemu/compiler.h +14 -0
index c797f0d457..7ddbf1f1cf 100644
@@ -212,6 +212,20 @@
 # define QEMU_USED
 #endif
 
+/*
+ * From GCC documentation:
+ *
+ *   The warn_unused_result attribute causes a warning to be emitted if a
+ *   caller of the function with this attribute does not use its return value.
+ *   This is useful for functions where not checking the result is either a
+ *   security problem or always a bug, such as realloc.
+ */
+#if __has_attribute(warn_unused_result)
+# define QEMU_WARN_UNUSED_RESULT __attribute__((warn_unused_result))
+#else
+# define QEMU_WARN_UNUSED_RESULT
+#endif
+
 /*
  * Ugly CPP trick that is like "defined FOO", but also works in C
  * code.  Useful to replace #ifdef with "if" statements; assumes
Signature · 1 line
2.39.2
#2 lore raw

Re: [PATCH 1/2] Add QEMU_WARN_UNUSED_RESULT attribute

Quoted 21 lines · Manos Pitsidianakis

This commit adds QEMU_WARN_UNUSED_RESULT, a macro for the gcc function attribute warn_unused_result. The utility of this attribute is to ensure functions that return values that need to be inspected are not ignored by the caller.

---
 include/qemu/compiler.h | 14 ++++++++++++++
 1 file changed, 14 insertions(+)
include/qemu/compiler.h +3 -0
index c797f0d457..7ddbf1f1cf 100644
@@ -212,6 +212,20 @@
 # define QEMU_USED
 #endif
 
+/*
+ * From GCC documentation:
+ *

include/qemu/compiler.h line 227

Quoted 10 lines
include/qemu/compiler.h +10 -0
+ *   The warn_unused_result attribute causes a warning to be emitted if a
+ *   caller of the function with this attribute does not use its return value.
+ *   This is useful for functions where not checking the result is either a
+ *   security problem or always a bug, such as realloc.
+ */
+#if __has_attribute(warn_unused_result)
+# define QEMU_WARN_UNUSED_RESULT __attribute__((warn_unused_result))
+#else
+# define QEMU_WARN_UNUSED_RESULT
+#endif

GLib already provides us G_GNUC_WARN_UNUSED_RESULT so don't add this.

With 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 :|
#3 lore raw

Re: [PATCH 1/2] Add QEMU_WARN_UNUSED_RESULT attribute

In reply to Manos Pitsidianakis
Quoted 13 lines · Manos Pitsidianakis

This commit adds QEMU_WARN_UNUSED_RESULT, a macro for the gcc function attribute warn_unused_result. The utility of this attribute is to ensure functions that return values that need to be inspected are not ignored by the caller.

---
  include/qemu/compiler.h | 14 ++++++++++++++
  1 file changed, 14 insertions(+)
include/qemu/compiler.h +3 -0
+/*
+ * From GCC documentation:
+ *

include/qemu/compiler.h line 227

Quoted 10 lines
include/qemu/compiler.h +10 -0
+ *   The warn_unused_result attribute causes a warning to be emitted if a
+ *   caller of the function with this attribute does not use its return value.
+ *   This is useful for functions where not checking the result is either a
+ *   security problem or always a bug, such as realloc.
+ */
+#if __has_attribute(warn_unused_result)
+# define QEMU_WARN_UNUSED_RESULT __attribute__((warn_unused_result))
+#else
+# define QEMU_WARN_UNUSED_RESULT
+#endif

FWIW I sometimes use:

+#if __has_attribute(nonnull)
+# define QEMU_NONNULL(indexes...) __attribute__((nonnull((indexes)))) +#else
+# define QEMU_NONNULL(indexes...)
+#endif

when doing tree-wide refactors, then remove the attribute because prototypes become really ugly.

#4 lore raw

PATCH 2/2 Add warn_unused_result attr to AUD_register_card

In reply to Manos Pitsidianakis

Ignoring the return value by accident is easy to miss as a bug. Such a bug was spotted by Coverity CID 1523899. Now, future instances of this type of bug will produce a warning when using GCC.

---
 audio/audio.h      | 2 +-
 hw/arm/omap2.c     | 8 +++++++-
 hw/input/tsc210x.c | 8 +++++++-
 3 files changed, 15 insertions(+), 3 deletions(-)
audio/audio.h +1 -1
index fcc22307be..b78c75962e 100644
@@ -94,7 +94,7 @@ typedef struct QEMUAudioTimeStamp {
 void AUD_vlog (const char *cap, const char *fmt, va_list ap) G_GNUC_PRINTF(2, 0);
 void AUD_log (const char *cap, const char *fmt, ...) G_GNUC_PRINTF(2, 3);
 
-bool AUD_register_card (const char *name, QEMUSoundCard *card, Error **errp);
+bool AUD_register_card (const char *name, QEMUSoundCard *card, Error **errp) QEMU_WARN_UNUSED_RESULT;
 void AUD_remove_card (QEMUSoundCard *card);
 CaptureVoiceOut *AUD_add_capture(
     AudioState *s,
hw/arm/omap2.c +7 -1
index f170728e7e..59fc061120 100644
@@ -614,7 +614,13 @@ static struct omap_eac_s *omap_eac_init(struct omap_target_agent_s *ta,
         s->codec.card.name = g_strdup(current_machine->audiodev);
         s->codec.card.state = audio_state_by_name(s->codec.card.name, &error_fatal);
     }
-    AUD_register_card("OMAP EAC", &s->codec.card, &error_fatal);
+    /*
+     * We pass error_fatal so on error QEMU will exit(). But we check the
+     * return value to make the warn_unused_result compiler warning go away.
+     */
+    if (!AUD_register_card("OMAP EAC", &s->codec.card, &error_fatal)) {
+        exit(1);
+    }
 
     memory_region_init_io(&s->iomem, NULL, &omap_eac_ops, s, "omap.eac",
                           omap_l4_region_size(ta, 0));
hw/input/tsc210x.c +7 -1
index 950506fb38..003c664b56 100644
@@ -1102,7 +1102,13 @@ static void tsc210x_init(TSC210xState *s,
         s->card.name = g_strdup(current_machine->audiodev);
         s->card.state = audio_state_by_name(s->card.name, &error_fatal);
     }
-    AUD_register_card(s->name, &s->card, &error_fatal);
+    /*
+     * We pass error_fatal so on error QEMU will exit(). But we check the
+     * return value to make the warn_unused_result compiler warning go away.
+     */
+    if (!AUD_register_card(s->name, &s->card, &error_fatal)) {
+        return;
+    }
 
     qemu_register_reset((void *) tsc210x_reset, s);
     vmstate_register(NULL, 0, vmsd, s);
Signature · 1 line
2.39.2
#5 lore raw

Re: [PATCH 2/2] Add warn_unused_result attr to AUD_register_card

Quoted 31 lines · Manos Pitsidianakis

Ignoring the return value by accident is easy to miss as a bug. Such a bug was spotted by Coverity CID 1523899. Now, future instances of this type of bug will produce a warning when using GCC.

---
 audio/audio.h      | 2 +-
 hw/arm/omap2.c     | 8 +++++++-
 hw/input/tsc210x.c | 8 +++++++-
 3 files changed, 15 insertions(+), 3 deletions(-)
audio/audio.h +1 -1
index fcc22307be..b78c75962e 100644
@@ -94,7 +94,7 @@ typedef struct QEMUAudioTimeStamp {
 void AUD_vlog (const char *cap, const char *fmt, va_list ap) G_GNUC_PRINTF(2, 0);
 void AUD_log (const char *cap, const char *fmt, ...) G_GNUC_PRINTF(2, 3);
 
-bool AUD_register_card (const char *name, QEMUSoundCard *card, Error **errp);
+bool AUD_register_card (const char *name, QEMUSoundCard *card, Error **errp) QEMU_WARN_UNUSED_RESULT;
 void AUD_remove_card (QEMUSoundCard *card);
 CaptureVoiceOut *AUD_add_capture(
     AudioState *s,
hw/arm/omap2.c +0 -0
index f170728e7e..59fc061120 100644
@@ -614,7 +614,13 @@ static struct omap_eac_s *omap_eac_init(struct omap_target_agent_s *ta,
         s->codec.card.name = g_strdup(current_machine->audiodev);

hw/arm/omap2.c static struct omap_eac_s *omap_eac_init(struct omap_target_agent_s *ta, · line 623

Quoted 10 lines
hw/arm/omap2.c +7 -1
         s->codec.card.state = audio_state_by_name(s->codec.card.name, &error_fatal);
     }
-    AUD_register_card("OMAP EAC", &s->codec.card, &error_fatal);
+    /*
+     * We pass error_fatal so on error QEMU will exit(). But we check the
+     * return value to make the warn_unused_result compiler warning go away.
+     */
+    if (!AUD_register_card("OMAP EAC", &s->codec.card, &error_fatal)) {
+        exit(1);
+    }

This kind of thing is why Coverity's unused-result warning has a lot of false positives. We shouldn't introduce extra code like this to work around the fact that the tooling doesn't understand our error-handling convention (i.e. error_fatal, and the way that some functions report errors both via the return value and also via the Error** argument).

thanks
-- PMM

#6 lore raw

Re: [PATCH 2/2] Add warn_unused_result attr to AUD_register_card

Quoted 6 lines · Peter Maydell

This kind of thing is why Coverity's unused-result warning has a lot of false positives. We shouldn't introduce extra code like this to work around the fact that the tooling doesn't understand our error-handling convention (i.e. error_fatal, and the way that some functions report errors both via the return value and also via the Error** argument).

I respect that :). But I personally believe that clinging to C's inadequacies, instead of preventing bugs statically just because it adds some lines of code, is misguided. Proper code should strive to make bugs impossible in the first place. At least that is my perspective and I would like there to be constructive discussions about different approaches in the mailing list. Perhaps something good might come out of it!

#7 lore raw

Re: [PATCH 2/2] Add warn_unused_result attr to AUD_register_card

Quoted 9 lines · Manos Pitsidianakis
Quoted 6 lines · Peter Maydell

This kind of thing is why Coverity's unused-result warning has a lot of false positives. We shouldn't introduce extra code like this to work around the fact that the tooling doesn't understand our error-handling convention (i.e. error_fatal, and the way that some functions report errors both via the return value and also via the Error** argument).

Quoted 4 lines

I respect that :). But I personally believe that clinging to C's inadequacies, instead of preventing bugs statically just because it adds some lines of code, is misguided. Proper code should strive to make bugs impossible in the first place.

I generally agree. The problem here really is that we've ended up with this odd API convention that reports errors in two ways. In an ideal world we'd tidy up our APIs to report errors exactly in one way (presumably via the Error).

-- PMM

#8 lore raw

Re: [PATCH 2/2] Add warn_unused_result attr to AUD_register_card

Quoted 16 lines · Peter Maydell
Quoted 9 lines · Manos Pitsidianakis
Quoted 6 lines · Peter Maydell

This kind of thing is why Coverity's unused-result warning has a lot of false positives. We shouldn't introduce extra code like this to work around the fact that the tooling doesn't understand our error-handling convention (i.e. error_fatal, and the way that some functions report errors both via the return value and also via the Error** argument).

Quoted 4 lines

I respect that :). But I personally believe that clinging to C's inadequacies, instead of preventing bugs statically just because it adds some lines of code, is misguided. Proper code should strive to make bugs impossible in the first place.

Quoted 4 lines

I generally agree. The problem here really is that we've ended up with this odd API convention that reports errors in two ways. In an ideal world we'd tidy up our APIs to report errors exactly in one way (presumably via the Error).

The compelling thing about our slightly odd &error_fatal/error_abort approach is that we can directly get stack traces showing exactly where the error happened.

If we just propagate the error up the stack to finally exit/abort, the origin context is essentially lost, and when it does abort, we don't get a snapshot of all threads at the time the error hit, as we've moved on in time.

With 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 :|
#9 lore raw

Re: [PATCH 2/2] Add warn_unused_result attr to AUD_register_card

Quoted 22 lines · Daniel P. Berrangé
Quoted 16 lines · Peter Maydell
Quoted 9 lines · Manos Pitsidianakis
Quoted 6 lines · Peter Maydell

This kind of thing is why Coverity's unused-result warning has a lot of false positives. We shouldn't introduce extra code like this to work around the fact that the tooling doesn't understand our error-handling convention (i.e. error_fatal, and the way that some functions report errors both via the return value and also via the Error** argument).

Quoted 4 lines

I respect that :). But I personally believe that clinging to C's inadequacies, instead of preventing bugs statically just because it adds some lines of code, is misguided. Proper code should strive to make bugs impossible in the first place.

Quoted 4 lines

I generally agree. The problem here really is that we've ended up with this odd API convention that reports errors in two ways. In an ideal world we'd tidy up our APIs to report errors exactly in one way (presumably via the Error).

Quoted 8 lines

The compelling thing about our slightly odd &error_fatal/error_abort approach is that we can directly get stack traces showing exactly where the error happened.

If we just propagate the error up the stack to finally exit/abort, the origin context is essentially lost, and when it does abort, we don't get a snapshot of all threads at the time the error hit, as we've moved on in time.

FYI: It is possible to get a stack trace using gnu libunwind which is portable, dependency-free and runs on many targets: https://github.com/libunwind/libunwind#libunwind

Or llvm's libunwind which is also of great quality: https://bcain-llvm.readthedocs.io/projects/libunwind/en/latest/

They are also compatible with co-routines and JIT code (At least, the GNU one is, not sure about llvm).

#10 lore raw

Re: [PATCH 2/2] Add warn_unused_result attr to AUD_register_card

In reply to Manos Pitsidianakis
Quoted 8 lines · Manos Pitsidianakis
Quoted 6 lines · Peter Maydell

This kind of thing is why Coverity's unused-result warning has a lot of false positives. We shouldn't introduce extra code like this to work around the fact that the tooling doesn't understand our error-handling convention (i.e. error_fatal, and the way that some functions report errors both via the return value and also via the Error** argument).

Quoted 6 lines

I respect that :). But I personally believe that clinging to C's inadequacies, instead of preventing bugs statically just because it adds some lines of code, is misguided. Proper code should strive to make bugs impossible in the first place. At least that is my perspective and I would like there to be constructive discussions about different approaches in the mailing list. Perhaps something good might come out of it!

Your approach to the problem:

  if (!AUD_register_card("OMAP EAC", &s->codec.card, &error_fatal)) {
    exit(1);
  }

is adding dead-code because the exit(1) will never be reachable. So while it lets you squelch the unused result warning, it is verbose and misleading to anyone who sees it.

Perhaps a more viable option is to pull in gnulib's ignore_value macro

  #define ignore_value(x) \
    (__extension__ ({ __typeof__ (x) __x = (x); (void) __x; }))

and then we would have just this:

ignore_value(AUD_register_card("OMAP EAC", &s->codec.card, &error_fatal));

With 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 :|
#11 lore raw

Re: [PATCH 2/2] Add warn_unused_result attr to AUD_register_card

Quoted 10 lines · Daniel P. Berrangé

Your approach to the problem:

 if (!AUD_register_card("OMAP EAC", &s->codec.card, &error_fatal)) {
   exit(1);

}

is adding dead-code because the exit(1) will never be reachable. So while it lets you squelch the unused result warning, it is verbose and misleading to anyone who sees it.

Quoted 8 lines

Perhaps a more viable option is to pull in gnulib's ignore_value macro

 #define ignore_value(x) \
   (__extension__ ({ __typeof__ (x) __x = (x); (void) __x; }))

and then we would have just this:

ignore_value(AUD_register_card("OMAP EAC", &s->codec.card, &error_fatal));

Good suggestion, thanks!

And to be fair, I did put a comment directly above that line to explain the unreachable code path. :)

+ /*

+     * We pass error_fatal so on error QEMU will exit(). But we check the
+     * return value to make the warn_unused_result compiler warning go away.
+     */
#12 lore raw

Re: [PATCH 2/2] Add warn_unused_result attr to AUD_register_card

In reply to Daniel P. Berrangé
Quoted 26 lines · Daniel P. Berrangé
Quoted 8 lines · Manos Pitsidianakis
Quoted 6 lines · Peter Maydell

This kind of thing is why Coverity's unused-result warning has a lot of false positives. We shouldn't introduce extra code like this to work around the fact that the tooling doesn't understand our error-handling convention (i.e. error_fatal, and the way that some functions report errors both via the return value and also via the Error** argument).

Quoted 6 lines

I respect that :). But I personally believe that clinging to C's inadequacies, instead of preventing bugs statically just because it adds some lines of code, is misguided. Proper code should strive to make bugs impossible in the first place. At least that is my perspective and I would like there to be constructive discussions about different approaches in the mailing list. Perhaps something good might come out of it!

Your approach to the problem:

 if (!AUD_register_card("OMAP EAC", &s->codec.card, &error_fatal)) {
   exit(1);

}

is adding dead-code because the exit(1) will never be reachable. So while it lets you squelch the unused result warning, it is verbose and misleading to anyone who sees it.

Quoted 8 lines

Perhaps a more viable option is to pull in gnulib's ignore_value macro

 #define ignore_value(x) \
   (__extension__ ({ __typeof__ (x) __x = (x); (void) __x; }))

and then we would have just this:

ignore_value(AUD_register_card("OMAP EAC", &s->codec.card, &error_fatal));

I wonder if just casting to (void) without assigning to a value could avoid the warning? In that case you would not need a macro just

(void)AUD_register_card("OMAP EAC", &s->codec.card, &error_fatal);

Not sure it's clearer than a macro which states what it does but the definition is a bit cryptic while this cast is simpler but may also puzzle somebody not familiar with it.

Regards,
BALATON Zoltan

#13 lore raw

Re: [PATCH 2/2] Add warn_unused_result attr to AUD_register_card

Quoted 32 lines · BALATON Zoltan
Quoted 31 lines · Daniel P. Berrangé
Quoted 8 lines · Manos Pitsidianakis
Quoted 6 lines · Peter Maydell

This kind of thing is why Coverity's unused-result warning has a lot of false positives. We shouldn't introduce extra code like this to work around the fact that the tooling doesn't understand our error-handling convention (i.e. error_fatal, and the way that some functions report errors both via the return value and also via the Error** argument).

Quoted 6 lines

I respect that :). But I personally believe that clinging to C's inadequacies, instead of preventing bugs statically just because it adds some lines of code, is misguided. Proper code should strive to make bugs impossible in the first place. At least that is my perspective and I would like there to be constructive discussions about different approaches in the mailing list. Perhaps something good might come out of it!

Your approach to the problem:

 if (!AUD_register_card("OMAP EAC", &s->codec.card, &error_fatal)) {
   exit(1);

}

is adding dead-code because the exit(1) will never be reachable. So while it lets you squelch the unused result warning, it is verbose and misleading to anyone who sees it.

Perhaps a more viable option is to pull in gnulib's ignore_value macro

 #define ignore_value(x) \
   (__extension__ ({ __typeof__ (x) __x = (x); (void) __x; }))
Quoted 8 lines
Quoted 3 lines

and then we would have just this:

ignore_value(AUD_register_card("OMAP EAC", &s->codec.card, &error_fatal));

I wonder if just casting to (void) without assigning to a value could avoid the warning? In that case you would not need a macro just

(void)AUD_register_card("OMAP EAC", &s->codec.card, &error_fatal);

Casting to void is not sufficient, which is why I suggested the ignore_value macro, which does enough to fool the compiler into thinking the return value is used.

With 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 :|
#14 lore raw

Re: [PATCH 2/2] Add warn_unused_result attr to AUD_register_card

In reply to Daniel P. Berrangé
Quoted 16 lines · Daniel P. Berrangé
Quoted 8 lines · Manos Pitsidianakis
Quoted 6 lines · Peter Maydell

This kind of thing is why Coverity's unused-result warning has a lot of false positives. We shouldn't introduce extra code like this to work around the fact that the tooling doesn't understand our error-handling convention (i.e. error_fatal, and the way that some functions report errors both via the return value and also via the Error** argument).

Quoted 6 lines

I respect that :). But I personally believe that clinging to C's inadequacies, instead of preventing bugs statically just because it adds some lines of code, is misguided. Proper code should strive to make bugs impossible in the first place. At least that is my perspective and I would like there to be constructive discussions about different approaches in the mailing list. Perhaps something good might come out of it!

Quoted 4 lines

Your approach to the problem:

   if (!AUD_register_card("OMAP EAC", &s->codec.card, &error_fatal)) {
     exit(1);

Rather:

       g_assert_not_reached();
Quoted 5 lines
   }

is adding dead-code because the exit(1) will never be reachable. So while it lets you squelch the unused result warning, it is verbose and misleading to anyone who sees it.