Skip to contentSkip to conversation

virtio: ARRAY_SIZE fixups

PATCH Superseded Michael S. Tsirkin 3 participants 8 messages Jan 18, 2017 – Jan 19, 2017

v1 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

cover virtio: ARRAY_SIZE fixups

Turns out virtio kept using ARRAY_SIZE on fields which stopped being arrays, this was noticed by a coverity scan. Fix this up, and fix up the ARRAY_SIZE macro so that this bug does not reappear in any other place.

Michael S. Tsirkin (4):
virtio: fix up max size checks
compiler: drop ; after BUILD_BUG_ON
compiler: expression version of QEMU_BUILD_BUG_ON
ARRAY_SIZE: check that argument is an array

 include/qemu/compiler.h |  4 +++-
 include/qemu/osdep.h    |  8 +++++++-
 hw/virtio/virtio.c      | 27 +++++++++++----------------
 3 files changed, 21 insertions(+), 18 deletions(-)
Signature · 1 line
MST
#1 lore raw

PATCH 1/4 virtio: fix up max size checks

Coverity reports that ARRAY_SIZE(elem->out_sg) (and all the others too) is wrong because elem->out_sg is a pointer.

However, the check is not in the right place and the max_size argument of virtqueue_map_iovec can be removed. The check on in_num/out_num should be moved to qemu_get_virtqueue_element instead, before the call to virtqueue_alloc_element.

Fixes 3724650db07057333879484c8bc7d900b5c1bf8e ("virtio: introduce virtqueue_alloc_element")
---
 hw/virtio/virtio.c | 27 +++++++++++----------------
 1 file changed, 11 insertions(+), 16 deletions(-)
hw/virtio/virtio.c +11 -16
index 34065c7..c29127a 100644
@@ -599,23 +599,11 @@ static void virtqueue_undo_map_desc(unsigned int out_num, unsigned int in_num,
 
 static void virtqueue_map_iovec(VirtIODevice *vdev, struct iovec *sg,
                                 hwaddr *addr, unsigned int *num_sg,
-                                unsigned int max_size, int is_write)
+                                int is_write)
 {
     unsigned int i;
     hwaddr len;
 
-    /* Note: this function MUST validate input, some callers
-     * are passing in num_sg values received over the network.
-     */
-    /* TODO: teach all callers that this can fail, and return failure instead
-     * of asserting here.
-     * When we do, we might be able to re-enable NDEBUG below.
-     */
-#ifdef NDEBUG
-#error building with NDEBUG is not supported
-#endif
-    assert(*num_sg <= max_size);
-
     for (i = 0; i < *num_sg; i++) {
         len = sg[i].iov_len;
         sg[i].iov_base = dma_memory_map(vdev->dma_as,
@@ -636,11 +624,8 @@ static void virtqueue_map_iovec(VirtIODevice *vdev, struct iovec *sg,
 void virtqueue_map(VirtIODevice *vdev, VirtQueueElement *elem)
 {
     virtqueue_map_iovec(vdev, elem->in_sg, elem->in_addr, &elem->in_num,
-                        MIN(ARRAY_SIZE(elem->in_sg), ARRAY_SIZE(elem->in_addr)),
                         1);
     virtqueue_map_iovec(vdev, elem->out_sg, elem->out_addr, &elem->out_num,
-                        MIN(ARRAY_SIZE(elem->out_sg),
-                        ARRAY_SIZE(elem->out_addr)),
                         0);
 }
 
@@ -840,6 +825,16 @@ void *qemu_get_virtqueue_element(VirtIODevice *vdev, QEMUFile *f, size_t sz)
 
     qemu_get_buffer(f, (uint8_t *)&data, sizeof(VirtQueueElementOld));
 
+    /* TODO: teach all callers that this can fail, and return failure instead
+     * of asserting here.
+     * When we do, we might be able to re-enable NDEBUG below.
+     */
+#ifdef NDEBUG
+#error building with NDEBUG is not supported
+#endif
+    assert(ARRAY_SIZE(data.in_addr) >= data.in_num);
+    assert(ARRAY_SIZE(data.out_addr) >= data.out_num);
+
     elem = virtqueue_alloc_element(sz, data.out_num, data.in_num);
     elem->index = data.index;
 
Signature · 1 line
MST
#2 lore raw

PATCH 2/4 compiler: drop ; after BUILD_BUG_ON

In reply to Michael S. Tsirkin

All users include the trailing ;, let's require that so that uses such as if (a) QEMU_BUILD_BUG_ON(); do not produce unexpected results.

Not a huge problem for QEMU since our style requires the use of {} but seems cleaner nevertheless.

---
 include/qemu/compiler.h | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
include/qemu/compiler.h +1 -1
index 157698b..1a9eeb9 100644
@@ -86,7 +86,7 @@
 #define type_check(t1,t2) ((t1*)0 - (t2*)0)
 
 #define QEMU_BUILD_BUG_ON(x) \
-    typedef char glue(qemu_build_bug_on__,__LINE__)[(x)?-1:1] __attribute__((unused));
+    typedef char glue(qemu_build_bug_on__,__LINE__)[(x)?-1:1] __attribute__((unused))
 
 #if defined __GNUC__
 # if !QEMU_GNUC_PREREQ(4, 4)
Signature · 1 line
MST
#3 lore raw

PATCH 3/4 compiler: expression version of QEMU_BUILD_BUG_ON

In reply to Michael S. Tsirkin

QEMU_BUILD_BUG_ON uses a typedef in order to be safe to use outside functions, but sometimes it's useful to have a version that can be used within an expression. Following what Linux does, introduce QEMU_BUILD_BUG_ON_ZERO that return zero after checking condition at build time.

---
 include/qemu/compiler.h | 2 ++
 1 file changed, 2 insertions(+)
include/qemu/compiler.h +2 -0
index 1a9eeb9..d2b05dd 100644
@@ -88,6 +88,8 @@
 #define QEMU_BUILD_BUG_ON(x) \
     typedef char glue(qemu_build_bug_on__,__LINE__)[(x)?-1:1] __attribute__((unused))
 
+#define QEMU_BUILD_BUG_ON_ZERO(x) (sizeof(int[(x)?-1:1]) - sizeof(int))
+
 #if defined __GNUC__
 # if !QEMU_GNUC_PREREQ(4, 4)
    /* gcc versions before 4.4.x don't support gnu_printf, so use printf. */
Signature · 1 line
MST
Reviewed-by on v1 3/4
#4 lore raw

Re: [Qemu-devel] [PATCH 3/4] compiler: expression version of QEMU_BUILD_BUG_ON

Quoted 16 lines · * Michael S. Tsirkin ([email protected]) wrote:

QEMU_BUILD_BUG_ON uses a typedef in order to be safe to use outside functions, but sometimes it's useful to have a version that can be used within an expression. Following what Linux does, introduce QEMU_BUILD_BUG_ON_ZERO that return zero after checking condition at build time.

---
 include/qemu/compiler.h | 2 ++
 1 file changed, 2 insertions(+)
include/qemu/compiler.h +0 -0
index 1a9eeb9..d2b05dd 100644

include/qemu/compiler.h line 92

Quoted 5 lines
include/qemu/compiler.h +2 -0
@@ -88,6 +88,8 @@
 #define QEMU_BUILD_BUG_ON(x) \
     typedef char glue(qemu_build_bug_on__,__LINE__)[(x)?-1:1] __attribute__((unused))
 
+#define QEMU_BUILD_BUG_ON_ZERO(x) (sizeof(int[(x)?-1:1]) - sizeof(int))
+

Yes, pretty much the same as:
https://lists.gnu.org/archive/html/qemu-devel/2016-10/msg07090.html

I'd used -sizeof(int[1]) in the right hand size to match the left, but
I think it should always be the same.

Quoted 7 lines
include/qemu/compiler.h +0 -0
 #if defined __GNUC__
 # if !QEMU_GNUC_PREREQ(4, 4)
    /* gcc versions before 4.4.x don't support gnu_printf, so use printf. */
Signature · 1 line
MST

--
Dr. David Alan Gilbert / [email protected] / Manchester, UK

#5 lore raw

PATCH 4/4 ARRAY_SIZE: check that argument is an array

In reply to Michael S. Tsirkin

It's a familiar pattern: some code uses ARRAY_SIZE, then refactoring changes the argument from an array to a pointer to a dynamically allocated buffer. Code keeps compiling but any ARRAY_SIZE calls now return the size of the pointer divided by element size.

Let's add build time checks to ARRAY_SIZE before we allow more of these in the code-base.

---
 include/qemu/osdep.h | 8 +++++++-
 1 file changed, 7 insertions(+), 1 deletion(-)
include/qemu/osdep.h +7 -1
index 689f253..24bfda0 100644
@@ -199,7 +199,13 @@ extern int daemon(int, int);
 #endif
 
 #ifndef ARRAY_SIZE
-#define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0]))
+/*
+ * &(x)[0] is always a pointer - if it's same type as x then the argument is a
+ * pointer, not an array as expected.
+ */
+#define ARRAY_SIZE(x) ((sizeof(x) / sizeof((x)[0])) + QEMU_BUILD_BUG_ON_ZERO( \
+                        __builtin_types_compatible_p(typeof(x), \
+                                                     typeof(&(x)[0]))))
 #endif
 
 int qemu_daemon(int nochdir, int noclose);
Signature · 1 line
MST
#6 lore raw
In reply to Michael S. Tsirkin
Your series seems to have some coding style problems. See output below for more information:

Hi,

Your series seems to have some coding style problems. See output below for more information:

Message-id: [email protected]
Subject: [Qemu-devel] [PATCH 0/4] virtio: ARRAY_SIZE fixups
Type: series

=== TEST SCRIPT BEGIN ===
#!/bin/bash

BASE=base
n=1
total=$(git log --oneline $BASE.. | wc -l)
failed=0

# Useful git options
git config --local diff.renamelimit 0
git config --local diff.renames True

commits="$(git log --format=%H --reverse $BASE..)" for c in $commits; do

    echo "Checking PATCH $n/$total: $(git log -n 1 --format=%s $c)..."
    if ! git show $c --format=email | ./scripts/checkpatch.pl --mailback -; then
        failed=1
        echo
    fi
    n=$((n+1))

done

exit $failed
=== TEST SCRIPT END ===

Updating 3c8cf5a9c21ff8782164d1def7f44bd888713384 From https://github.com/patchew-project/qemu
* [new tag] patchew/[email protected] -> patchew/[email protected]
Switched to a new branch 'test'
38b031b ARRAY_SIZE: check that argument is an array 19ef003 compiler: expression version of QEMU_BUILD_BUG_ON 71870e6 compiler: drop ; after BUILD_BUG_ON c652d79 virtio: fix up max size checks

=== OUTPUT BEGIN ===
Checking PATCH 1/4: virtio: fix up max size checks... Checking PATCH 2/4: compiler: drop ; after BUILD_BUG_ON...
WARNING: line over 80 characters
#25: FILE: include/qemu/compiler.h:89:
+ typedef char glue(qemu_build_bug_on__,__LINE__)[(x)?-1:1] __attribute__((unused))

ERROR: space required after that ',' (ctx:VxV)
#25: FILE: include/qemu/compiler.h:89:
+ typedef char glue(qemu_build_bug_on__,__LINE__)[(x)?-1:1] __attribute__((unused))

                                          ^

ERROR: spaces required around that '?' (ctx:VxO)
#25: FILE: include/qemu/compiler.h:89:
+ typedef char glue(qemu_build_bug_on__,__LINE__)[(x)?-1:1] __attribute__((unused))

                                                        ^

ERROR: space required before that '-' (ctx:OxV)
#25: FILE: include/qemu/compiler.h:89:
+ typedef char glue(qemu_build_bug_on__,__LINE__)[(x)?-1:1] __attribute__((unused))

                                                         ^

ERROR: spaces required around that ':' (ctx:VxV)
#25: FILE: include/qemu/compiler.h:89:
+ typedef char glue(qemu_build_bug_on__,__LINE__)[(x)?-1:1] __attribute__((unused))

                                                           ^

total: 4 errors, 1 warnings, 8 lines checked

Your patch has style problems, please review. If any of these errors are false positives report them to the maintainer, see CHECKPATCH in MAINTAINERS.

Checking PATCH 3/4: compiler: expression version of QEMU_BUILD_BUG_ON...
ERROR: spaces required around that '?' (ctx:VxO)
#23: FILE: include/qemu/compiler.h:91:
+#define QEMU_BUILD_BUG_ON_ZERO(x) (sizeof(int[(x)?-1:1]) - sizeof(int))

                                                  ^

ERROR: space required before that '-' (ctx:OxV)
#23: FILE: include/qemu/compiler.h:91:
+#define QEMU_BUILD_BUG_ON_ZERO(x) (sizeof(int[(x)?-1:1]) - sizeof(int))

                                                   ^

ERROR: spaces required around that ':' (ctx:VxV)
#23: FILE: include/qemu/compiler.h:91:
+#define QEMU_BUILD_BUG_ON_ZERO(x) (sizeof(int[(x)?-1:1]) - sizeof(int))

                                                     ^

total: 3 errors, 0 warnings, 8 lines checked

Your patch has style problems, please review. If any of these errors are false positives report them to the maintainer, see CHECKPATCH in MAINTAINERS.

Checking PATCH 4/4: ARRAY_SIZE: check that argument is an array... === OUTPUT END ===

Test command exited with code: 1

---
Email generated automatically by Patchew [http://patchew.org/].
Please send your feedback to [email protected]

#7 lore raw
Quoted 4 lines · On Wed, Jan 18, 2017 at 12:40:50PM -0800, [email protected] wrote:

Hi,

Your series seems to have some coding style problems. See output below for more information:

Not sure we really do care as this matches existing code, but I'll fix it up.

Quoted 102 lines

Message-id: [email protected]
Subject: [Qemu-devel] [PATCH 0/4] virtio: ARRAY_SIZE fixups
Type: series

=== TEST SCRIPT BEGIN ===
#!/bin/bash

BASE=base
n=1
total=$(git log --oneline $BASE.. | wc -l)
failed=0

# Useful git options
git config --local diff.renamelimit 0
git config --local diff.renames True

commits="$(git log --format=%H --reverse $BASE..)" for c in $commits; do

    echo "Checking PATCH $n/$total: $(git log -n 1 --format=%s $c)..."
    if ! git show $c --format=email | ./scripts/checkpatch.pl --mailback -; then
        failed=1
        echo
    fi
    n=$((n+1))

done

exit $failed
=== TEST SCRIPT END ===

Updating 3c8cf5a9c21ff8782164d1def7f44bd888713384 From https://github.com/patchew-project/qemu
* [new tag] patchew/[email protected] -> patchew/[email protected]
Switched to a new branch 'test'
38b031b ARRAY_SIZE: check that argument is an array 19ef003 compiler: expression version of QEMU_BUILD_BUG_ON 71870e6 compiler: drop ; after BUILD_BUG_ON c652d79 virtio: fix up max size checks

=== OUTPUT BEGIN ===
Checking PATCH 1/4: virtio: fix up max size checks... Checking PATCH 2/4: compiler: drop ; after BUILD_BUG_ON...
WARNING: line over 80 characters
#25: FILE: include/qemu/compiler.h:89:
+ typedef char glue(qemu_build_bug_on__,__LINE__)[(x)?-1:1] __attribute__((unused))

ERROR: space required after that ',' (ctx:VxV)
#25: FILE: include/qemu/compiler.h:89:
+ typedef char glue(qemu_build_bug_on__,__LINE__)[(x)?-1:1] __attribute__((unused))

                                          ^

ERROR: spaces required around that '?' (ctx:VxO)
#25: FILE: include/qemu/compiler.h:89:
+ typedef char glue(qemu_build_bug_on__,__LINE__)[(x)?-1:1] __attribute__((unused))

                                                        ^

ERROR: space required before that '-' (ctx:OxV)
#25: FILE: include/qemu/compiler.h:89:
+ typedef char glue(qemu_build_bug_on__,__LINE__)[(x)?-1:1] __attribute__((unused))

                                                         ^

ERROR: spaces required around that ':' (ctx:VxV)
#25: FILE: include/qemu/compiler.h:89:
+ typedef char glue(qemu_build_bug_on__,__LINE__)[(x)?-1:1] __attribute__((unused))

                                                           ^

total: 4 errors, 1 warnings, 8 lines checked

Your patch has style problems, please review. If any of these errors are false positives report them to the maintainer, see CHECKPATCH in MAINTAINERS.

Checking PATCH 3/4: compiler: expression version of QEMU_BUILD_BUG_ON...
ERROR: spaces required around that '?' (ctx:VxO)
#23: FILE: include/qemu/compiler.h:91:
+#define QEMU_BUILD_BUG_ON_ZERO(x) (sizeof(int[(x)?-1:1]) - sizeof(int))

                                                  ^

ERROR: space required before that '-' (ctx:OxV)
#23: FILE: include/qemu/compiler.h:91:
+#define QEMU_BUILD_BUG_ON_ZERO(x) (sizeof(int[(x)?-1:1]) - sizeof(int))

                                                   ^

ERROR: spaces required around that ':' (ctx:VxV)
#23: FILE: include/qemu/compiler.h:91:
+#define QEMU_BUILD_BUG_ON_ZERO(x) (sizeof(int[(x)?-1:1]) - sizeof(int))

                                                     ^

total: 3 errors, 0 warnings, 8 lines checked

Your patch has style problems, please review. If any of these errors are false positives report them to the maintainer, see CHECKPATCH in MAINTAINERS.

Checking PATCH 4/4: ARRAY_SIZE: check that argument is an array... === OUTPUT END ===

Test command exited with code: 1

---
Email generated automatically by Patchew [http://patchew.org/].
Please send your feedback to [email protected]