Skip to contentSkip to conversation

fix bugs reported by Clang Static Analyzer

PATCH Applied Philippe Mathieu-Daudé 14 participants 111 messages Jul 24, 2017 – Jun 4, 2018

Subsystems (4)

v1 of 2 Applied Latest is v2 →

lore
Share & tools
lore
#0 lore raw

cover fix bugs reported by Clang Static Analyzer

Hi,

This series is the result of having fun with Clang's Static Analyzer during the week-end (see https://clang-analyzer.llvm.org/).

No need to delay the first batch for -rc0.

Patch 1 was in another series (delayed for 2.11), it would be nice to have it in 2.10.

I built a docker image based on debian/unstable to use bleeding code, the Dockerfile is provided at the end of this series as RFC for people wanted to give a try (also include 'dockershell' script and a fix in ./configure). These RFC patches (33-35) are not expected to enter 2.10.

I used the following commands:

$ make docker-image-debian-bleeding-dev
$ ./scripts/dockershell debian-bleeding-dev
$ mkdir -p build
$ cd build
$ ../configure --host-cc=clang-5.0 --cc=clang-5.0 --cxx=clang++-5.0 \
  --disable-docs --enable-debug
$ scan-build-5.0 -o testresults --keep-going -maxloop 2 -no-failure-reports \
  -analyzer-config stable-report-filename=true \
  -disable-checker alpha.clone.CloneChecker \
  -enable-checker alpha.core.CastSize \
  -enable-checker alpha.core.Conversion \
  -enable-checker alpha.core.IdenticalExpr \
  -enable-checker alpha.core.SizeofPtr \
  -disable-checker alpha.deadcode.UnreachableCode \
  -enable-checker alpha.security.ArrayBoundV2 \
  -enable-checker alpha.security.MallocOverflow \
  -enable-checker alpha.unix.cstring.BufferOverlap \
  -enable-checker alpha.unix.cstring.OutOfBounds \
  -disable-checker deadcode.DeadStores \
  -disable-checker optin.performance.Padding \
  -enable-checker optin.portability.UnixAPI \
  -disable-checker security.insecureAPI.getpw \
  -disable-checker security.insecureAPI.gets \
  -enable-checker security.insecureAPI.strcpy \
  -disable-checker unix.Vfork \
    make -k -j4

Regards,

Phil.

Philippe Mathieu-Daudé (35):
tests: add missing dependency to build QTEST_QEMU_BINARY
loader: check get_image_size() return value
thunk: check nb_fields is valid before continuing
ivshmem: fix incorrect error handling in ivshmem_recv_msg()
nbd: fix memory leak in nbd_opt_go()
qcow2: remove inconsistent check
qcow2: fix null pointer dereference
qcow2: fix null pointer dereference
ui/vnc: fix leak of SocketAddress **
net/eth: fix incorrect check of iov_to_buf() return value i2c/exynos4210: correctly check i2c_recv() return value vfio/platform: fix use of freed memory
vfio/pci: fix use of freed memory
vfio/ccw: fix incorrect malloc() size
usb: correctly handle Zero Length Packets
usb/dev-mtp: fix use of uninitialized values
usb/dev-mtp: fix use of uninitialized values
usb/dev-mtp: fix use of uninitialized values
usb/dev-mtp: fix use of uninitialized variable
arm/boot: fix undefined instruction on secondary smp cpu bootloader arm/sysbus-fdt: fix null pointer dereference
arm/vexpress: fix potential memory leak
m68k/translate: fix incorrect copy/paste
linux-user/sh4: fix incorrect memory write
linux-user: extract is_error() out of syscall.c
linux-user: use is_error() to avoid warnings and make the code clearer
syscall: fix dereference of undefined pointer
syscall: check dup/dup2/dup3() errors, return EBADFD/EINVAL if
required
syscall: fix out-of-bound memory access
syscall: fix use of uninitialized values
syscall: replace strcpy() by g_strlcpy()
timer/pxa2xx: silent warning about out-of-bound memory access
configure: clang does not support -Wexpansion-to-defined
docker: add debian 'bleeding' image with gcc7 and clang5 script to run docker image

 block/qcow2-bitmap.c                               |  4 +-
 block/qcow2-refcount.c                             |  4 +-
 configure                                          |  6 +-
 hw/arm/boot.c                                      |  2 +-
 hw/arm/sysbus-fdt.c                                | 11 ++-
 hw/arm/vexpress.c                                  |  5 +-
 hw/core/loader.c                                   |  4 +-
 hw/i2c/exynos4210_i2c.c                            |  8 +-
 hw/misc/ivshmem.c                                  |  5 +-
 hw/timer/pxa2xx_timer.c                            |  2 +-
 hw/usb/dev-mtp.c                                   | 60 +++++++++-----
 hw/usb/redirect.c                                  |  2 +-
 hw/vfio/ccw.c                                      |  2 +-
 hw/vfio/pci.c                                      | 11 ++-
 hw/vfio/platform.c                                 |  2 +-
 linux-user/elfload.c                               |  2 +-
 linux-user/flatload.c                              | 15 ++--
 linux-user/qemu.h                                  |  5 ++
 linux-user/syscall.c                               | 45 +++++++----
 nbd/client.c                                       |  8 +-
 net/eth.c                                          |  4 +-
 scripts/dockershell                                | 30 +++++++
 target/m68k/translate.c                            |  3 +-
 tests/Makefile.include                             |  2 +-
 .../docker/dockerfiles/debian-bleeding-dev.docker  | 94 ++++++++++++++++++++++
 thunk.c                                            |  5 +-
 ui/vnc.c                                           | 36 ++++-----
 27 files changed, 279 insertions(+), 98 deletions(-)
 create mode 100755 scripts/dockershell
 create mode 100644 tests/docker/dockerfiles/debian-bleeding-dev.docker
Signature · 1 line
2.13.3
#1 lore raw

PATCH 1/35 tests: add missing dependency to build QTEST_QEMU_BINARY

This allow a one liner from fresh repository clone, i.e.:

  ./configure && make -j check-qtest-aarch64
---
 tests/Makefile.include | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
tests/Makefile.include +1 -1
index 7af278db55..b55fe39d94 100644
@@ -830,7 +830,7 @@ GCOV_OPTIONS = -n $(if $(V),-f,)
 # gtester tests, possibly with verbose output
 
 .PHONY: $(patsubst %, check-qtest-%, $(QTEST_TARGETS))
-$(patsubst %, check-qtest-%, $(QTEST_TARGETS)): check-qtest-%: $(check-qtest-y)
+$(patsubst %, check-qtest-%, $(QTEST_TARGETS)): check-qtest-%: subdir-%-softmmu $(check-qtest-y)
 	$(if $(CONFIG_GCOV),@rm -f *.gcda */*.gcda */*/*.gcda */*/*/*.gcda,)
 	$(call quiet-command,QTEST_QEMU_BINARY=$*-softmmu/qemu-system-$* \
 		QTEST_QEMU_IMG=qemu-img$(EXESUF) \
Signature · 1 line
2.13.3
#2 lore raw

PATCH 2/35 loader: check get_image_size() return value

In reply to Philippe Mathieu-Daudé

since a negative value means it errored.

hw/core/loader.c:149:9: warning: Loss of sign in implicit conversion

    if (size > max_sz) {
        ^~~~
hw/core/loader.c:171:9: warning: Loss of sign in implicit conversion
    if (size > memory_region_size(mr)) {
        ^~~~
Reported-by Clang Static Analyzer
---
 hw/core/loader.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
hw/core/loader.c +2 -2
index c17ace0a2e..4bb176f284 100644
@@ -146,7 +146,7 @@ int load_image_targphys_as(const char *filename,
     int size;
 
     size = get_image_size(filename);
-    if (size > max_sz) {
+    if (size < 0 || size > max_sz) {
         return -1;
     }
     if (size > 0) {
@@ -168,7 +168,7 @@ int load_image_mr(const char *filename, MemoryRegion *mr)
 
     size = get_image_size(filename);
 
-    if (size > memory_region_size(mr)) {
+    if (size < 0 || size > memory_region_size(mr)) {
         return -1;
     }
     if (size > 0) {
Signature · 1 line
2.13.3
Reviewed-by on v1 2/35
#3 lore raw

Re: [Qemu-devel] [PATCH for 2.10 02/35] loader: check get_image_size() return value

Quoted 9 lines · Philippe Mathieu-Daudé

since a negative value means it errored.

hw/core/loader.c:149:9: warning: Loss of sign in implicit conversion

    if (size > max_sz) {
        ^~~~
hw/core/loader.c:171:9: warning: Loss of sign in implicit conversion
    if (size > memory_region_size(mr)) {
        ^~~~
Quoted 5 lines
Reported-by Clang Static Analyzer
---
 hw/core/loader.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
Signature · 3 lines
Eric Blake, Principal Software Engineer
Red Hat, Inc.           +1-919-301-3266
Virtualization:  qemu.org | libvirt.org
Reviewed-by
#4 lore raw

Re: [Qemu-devel] [PATCH for 2.10 02/35] loader: check get_image_size() return value

Quoted 10 lines · Eric Blake
Quoted 9 lines · Philippe Mathieu-Daudé

since a negative value means it errored.

hw/core/loader.c:149:9: warning: Loss of sign in implicit conversion

    if (size > max_sz) {
        ^~~~
hw/core/loader.c:171:9: warning: Loss of sign in implicit conversion
    if (size > memory_region_size(mr)) {
        ^~~~
Quoted 7 lines
Quoted 5 lines
Reported-by Clang Static Analyzer
---
 hw/core/loader.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

Thanks,
Alistair

Quoted 6 lines

--
Eric Blake, Principal Software Engineer
Red Hat, Inc. +1-919-301-3266
Virtualization: qemu.org | libvirt.org

#5 lore raw

PATCH 3/35 thunk: check nb_fields is valid before continuing

In reply to Philippe Mathieu-Daudé

thunk.c:91:32: warning: Call to 'malloc' has an allocation size of 0 bytes

        se->field_offsets[i] = malloc(nb_fields * sizeof(int));
                               ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Reported-by Clang Static Analyzer
---
 thunk.c | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)
thunk.c +4 -1
index 2dac36666d..d1c5e221f5 100644
@@ -67,7 +67,6 @@ void thunk_register_struct(int id, const char *name, const argtype *types)
     int nb_fields, offset, max_align, align, size, i, j;
 
     assert(id < max_struct_entries);
-    se = struct_entries + id;
 
     /* first we count the number of fields */
     type_ptr = types;
@@ -76,6 +75,10 @@ void thunk_register_struct(int id, const char *name, const argtype *types)
         type_ptr = thunk_type_next(type_ptr);
         nb_fields++;
     }
+    if (!nb_fields) {
+        return;
+    }
+    se = struct_entries + id;
     se->field_types = types;
     se->nb_fields = nb_fields;
     se->name = name;
Signature · 1 line
2.13.3
#6 lore raw

Re: [Qemu-devel] [PATCH for 2.10 03/35] thunk: check nb_fields is valid before continuing

Quoted 9 lines · Philippe Mathieu-Daudé

thunk.c:91:32: warning: Call to 'malloc' has an allocation size of 0 bytes

        se->field_offsets[i] = malloc(nb_fields * sizeof(int));
                               ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Reported-by Clang Static Analyzer
---
 thunk.c | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)

Better would be fixing the code to use g_new0, and the corresponding free.

Signature · 3 lines
Eric Blake, Principal Software Engineer
Red Hat, Inc.           +1-919-301-3266
Virtualization:  qemu.org | libvirt.org
#7 lore raw

Re: [Qemu-devel] [PATCH for 2.10 03/35] thunk: check nb_fields is valid before continuing

Quoted 5 lines · Eric Blake
Quoted 4 lines · Philippe Mathieu-Daudé

thunk.c:91:32: warning: Call to 'malloc' has an allocation size of 0 bytes

         se->field_offsets[i] = malloc(nb_fields * sizeof(int));
                                ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Quoted 7 lines
Quoted 5 lines
Reported-by Clang Static Analyzer
---
  thunk.c | 5 ++++-
  1 file changed, 4 insertions(+), 1 deletion(-)

Better would be fixing the code to use g_new0, and the corresponding free.

Ok, for 2.11 although (not a fix).

Also thunk* alloc'd are never free'd during process lifetime, so will keep like that (no g_free).

#8 lore raw

Re: [Qemu-devel] [PATCH for 2.10 03/35] thunk: check nb_fields is valid before continuing

In reply to Philippe Mathieu-Daudé
Quoted 23 lines · Philippe Mathieu-Daudé

thunk.c:91:32: warning: Call to 'malloc' has an allocation size of 0 bytes

        se->field_offsets[i] = malloc(nb_fields * sizeof(int));
                               ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Reported-by Clang Static Analyzer
---
 thunk.c | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)
thunk.c +0 -1
index 2dac36666d..d1c5e221f5 100644
@@ -67,7 +67,6 @@ void thunk_register_struct(int id, const char *name, const argtype *types)
     int nb_fields, offset, max_align, align, size, i, j;
 
     assert(id < max_struct_entries);
-    se = struct_entries + id;
 
     /* first we count the number of fields */
     type_ptr = types;

thunk.c void thunk_register_struct(int id, const char *name, const argtype *types) · line 80

Quoted 6 lines
thunk.c +3 -0
@@ -76,6 +75,10 @@ void thunk_register_struct(int id, const char *name, const argtype *types)
         type_ptr = thunk_type_next(type_ptr);
         nb_fields++;
     }
+    if (!nb_fields) {
+        return;
+    }

Can this ever actually happen? We only call this function for a fixed set of known-at-compile-time data (it's invoked by all the STRUCT() macro uses). It seems likely that it would be better to make this an assert() and check that none of our uses of STRUCT() cause it to fire.

Quoted 7 lines
thunk.c +1 -1
+    se = struct_entries + id;
     se->field_types = types;
     se->nb_fields = nb_fields;
     se->name = name;
--

2.13.3

thanks
-- PMM

#9 lore raw

PATCH 4/35 ivshmem: fix incorrect error handling in ivshmem_recv_msg()

In reply to Philippe Mathieu-Daudé

If qemu_chr_fe_read_all() returns -EINTR the do {} statement continues and the n accumulator used to complete reads upto sizeof(msg) is decremented by 4 (the value of EINTR on Linux).
To avoid that, use simpler if() statements and continue if EINTR occured.

hw/misc/ivshmem.c:650:14: warning: Loss of sign in implicit conversion

    } while (n < sizeof(msg));
             ^
Reported-by Clang Static Analyzer

---
get_maintainer.pl: No maintainers found!

 hw/misc/ivshmem.c | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)
hw/misc/ivshmem.c +4 -1
index a58f9ee579..47a015f072 100644
@@ -642,7 +642,10 @@ static int64_t ivshmem_recv_msg(IVShmemState *s, int *pfd, Error **errp)
     do {
         ret = qemu_chr_fe_read_all(&s->server_chr, (uint8_t *)&msg + n,
                                    sizeof(msg) - n);
-        if (ret < 0 && ret != -EINTR) {
+        if (ret < 0) {
+            if (ret == -EINTR) {
+                continue;
+            }
             error_setg_errno(errp, -ret, "read from server failed");
             return INT64_MIN;
         }
Signature · 1 line
2.13.3
Reviewed-by on v1 4/35
#10 lore raw

Re: [Qemu-devel] [PATCH for 2.10 04/35] ivshmem: fix incorrect error handling in ivshmem_recv_msg()

Quoted 9 lines · Philippe Mathieu-Daudé

If qemu_chr_fe_read_all() returns -EINTR the do {} statement continues and the n accumulator used to complete reads upto sizeof(msg) is decremented by 4 (the value of EINTR on Linux).
To avoid that, use simpler if() statements and continue if EINTR occured.

hw/misc/ivshmem.c:650:14: warning: Loss of sign in implicit conversion

    } while (n < sizeof(msg));
             ^

Let's add "Screwed up in commit 3a55fc0f, v2.6.0."

Quoted 14 lines
Reported-by Clang Static Analyzer

---
get_maintainer.pl: No maintainers found!

 hw/misc/ivshmem.c | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)
hw/misc/ivshmem.c +0 -0
index a58f9ee579..47a015f072 100644
@@ -642,7 +642,10 @@ static int64_t ivshmem_recv_msg(IVShmemState *s, int *pfd, Error **errp)
     do {

hw/misc/ivshmem.c static int64_t ivshmem_recv_msg(IVShmemState *s, int *pfd, Error **errp) · line 651

Quoted 10 lines
hw/misc/ivshmem.c +4 -1
         ret = qemu_chr_fe_read_all(&s->server_chr, (uint8_t *)&msg + n,
                                    sizeof(msg) - n);
-        if (ret < 0 && ret != -EINTR) {
+        if (ret < 0) {
+            if (ret == -EINTR) {
+                continue;
+            }
             error_setg_errno(errp, -ret, "read from server failed");
             return INT64_MIN;
         }

Paolo, you taking this through your miscellaneous queue would save me (and possibly Peter) a bit of work. Only if you have something queued already. Let me know.

#11 lore raw

Re: [Qemu-devel] [PATCH for 2.10 04/35] ivshmem: fix incorrect error handling in ivshmem_recv_msg()

Quoted 41 lines · Markus Armbruster
Quoted 9 lines · Philippe Mathieu-Daudé

If qemu_chr_fe_read_all() returns -EINTR the do {} statement continues and the n accumulator used to complete reads upto sizeof(msg) is decremented by 4 (the value of EINTR on Linux).
To avoid that, use simpler if() statements and continue if EINTR occured.

hw/misc/ivshmem.c:650:14: warning: Loss of sign in implicit conversion

    } while (n < sizeof(msg));
             ^

Let's add "Screwed up in commit 3a55fc0f, v2.6.0."

Quoted 14 lines
Reported-by Clang Static Analyzer

---
get_maintainer.pl: No maintainers found!

 hw/misc/ivshmem.c | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)
hw/misc/ivshmem.c +0 -0
index a58f9ee579..47a015f072 100644
@@ -642,7 +642,10 @@ static int64_t ivshmem_recv_msg(IVShmemState *s, int *pfd, Error **errp)
     do {

hw/misc/ivshmem.c static int64_t ivshmem_recv_msg(IVShmemState *s, int *pfd, Error **errp) · line 651

Quoted 10 lines
hw/misc/ivshmem.c +4 -1
         ret = qemu_chr_fe_read_all(&s->server_chr, (uint8_t *)&msg + n,
                                    sizeof(msg) - n);
-        if (ret < 0 && ret != -EINTR) {
+        if (ret < 0) {
+            if (ret == -EINTR) {
+                continue;
+            }
             error_setg_errno(errp, -ret, "read from server failed");
             return INT64_MIN;
         }
Quoted 3 lines

Paolo, you taking this through your miscellaneous queue would save me (and possibly Peter) a bit of work. Only if you have something queued already. Let me know.

Fair enough, I'll pick this up.

Paolo

#12 lore raw

PATCH 5/35 nbd: fix memory leak in nbd_opt_go()

In reply to Philippe Mathieu-Daudé

nbd/client.c:385:12: warning: Potential leak of memory pointed to by 'buf'

Reported-by Clang Static Analyzer
---
 nbd/client.c | 8 +++++---
 1 file changed, 5 insertions(+), 3 deletions(-)
nbd/client.c +5 -3
index 509ed5e4ba..0a17de80b5 100644
@@ -376,9 +376,11 @@ static int nbd_opt_go(QIOChannel *ioc, const char *wantname,
     if (info->request_sizes) {
         stw_be_p(buf + 4 + len + 2, NBD_INFO_BLOCK_SIZE);
     }
-    if (nbd_send_option_request(ioc, NBD_OPT_GO,
-                                4 + len + 2 + 2 * info->request_sizes, buf,
-                                errp) < 0) {
+    error = nbd_send_option_request(ioc, NBD_OPT_GO,
+                                    4 + len + 2 + 2 * info->request_sizes,
+                                    buf, errp);
+    g_free(buf);
+    if (error < 0) {
         return -1;
     }
 
Signature · 1 line
2.13.3
Reviewed-by on v1 5/35
#13 lore raw

Re: [Qemu-devel] [PATCH for 2.10 05/35] nbd: fix memory leak in nbd_opt_go()

Quoted 7 lines · Philippe Mathieu-Daudé

nbd/client.c:385:12: warning: Potential leak of memory pointed to by 'buf'

Reported-by Clang Static Analyzer
---
 nbd/client.c | 8 +++++---
 1 file changed, 5 insertions(+), 3 deletions(-)

I plan to take this through the NBD tree for 2.10, if no one else picks up the entire series first.

Signature · 3 lines
Eric Blake, Principal Software Engineer
Red Hat, Inc.           +1-919-301-3266
Virtualization:  qemu.org | libvirt.org
#14 lore raw

PATCH 6/35 qcow2: remove inconsistent check

In reply to Philippe Mathieu-Daudé

This is equivalent to assert(russian roulette) so better remove it.

block/qcow2-bitmap.c:259:29: warning: The left operand of '==' is a garbage value

        assert(bitmap_table == NULL);
               ~~~~~~~~~~~~ ^
Reported-by Clang Static Analyzer
---
 block/qcow2-bitmap.c | 1 -
 1 file changed, 1 deletion(-)
block/qcow2-bitmap.c +0 -1
index 3e8735a20d..fe72df5057 100644
@@ -254,7 +254,6 @@ static int free_bitmap_clusters(BlockDriverState *bs, Qcow2BitmapTable *tb)
 
     ret = bitmap_table_load(bs, tb, &bitmap_table);
     if (ret < 0) {
-        assert(bitmap_table == NULL);
         return ret;
     }
 
Signature · 1 line
2.13.3
#15 lore raw

Re: [Qemu-devel] [PATCH for 2.10 06/35] qcow2: remove inconsistent check

Quoted 17 lines · Philippe Mathieu-Daudé

This is equivalent to assert(russian roulette) so better remove it.

block/qcow2-bitmap.c:259:29: warning: The left operand of '==' is a garbage value

        assert(bitmap_table == NULL);
               ~~~~~~~~~~~~ ^
Reported-by Clang Static Analyzer
---
 block/qcow2-bitmap.c | 1 -
 1 file changed, 1 deletion(-)
block/qcow2-bitmap.c +0 -0
index 3e8735a20d..fe72df5057 100644

block/qcow2-bitmap.c static int free_bitmap_clusters(BlockDriverState *bs, Qcow2BitmapTable *tb) · line 257

Quoted 4 lines
block/qcow2-bitmap.c +0 -1
@@ -254,7 +254,6 @@ static int free_bitmap_clusters(BlockDriverState *bs, Qcow2BitmapTable *tb)
 
     ret = bitmap_table_load(bs, tb, &bitmap_table);
     if (ret < 0) {
-        assert(bitmap_table == NULL);

Rather, we should fix bitmap_table_load() to ensure that bitmap_table is always assigned, even on error.

Signature · 3 lines
Eric Blake, Principal Software Engineer
Red Hat, Inc.           +1-919-301-3266
Virtualization:  qemu.org | libvirt.org
#16 lore raw

Re: [Qemu-devel] [PATCH for 2.10 06/35] qcow2: remove inconsistent check

Quoted 19 lines · Eric Blake
Quoted 18 lines · Philippe Mathieu-Daudé

This is equivalent to assert(russian roulette) so better remove it.

block/qcow2-bitmap.c:259:29: warning: The left operand of '==' is a garbage value

        assert(bitmap_table == NULL);
               ~~~~~~~~~~~~ ^
Reported-by Clang Static Analyzer
---
 block/qcow2-bitmap.c | 1 -
 1 file changed, 1 deletion(-)
block/qcow2-bitmap.c +0 -0
index 3e8735a20d..fe72df5057 100644
@@ -254,7 +254,6 @@ static int free_bitmap_clusters(BlockDriverState *bs, Qcow2BitmapTable *tb)
 
Quoted 6 lines

block/qcow2-bitmap.c static int free_bitmap_clusters(BlockDriverState *bs, Qcow2BitmapTable *tb) · line 257

Quoted 3 lines
block/qcow2-bitmap.c +0 -1
     ret = bitmap_table_load(bs, tb, &bitmap_table);
     if (ret < 0) {
-        assert(bitmap_table == NULL);

Rather, we should fix bitmap_table_load() to ensure that bitmap_table is always assigned, even on error.

I think it's even better to initialize bitmap_table to NULL in free_bitmap_clusters, as all other callers do.

Paolo

#17 lore raw

PATCH 7/35 qcow2: fix null pointer dereference

In reply to Philippe Mathieu-Daudé

If find_bitmap_by_name() fails we have bm=NULL and go to the 'fail' label, then call bitmap_free(bm) which does g_free(bm->name) with bm=NULL...

Clang's scan-build-5.0 output:
block/qcow2-bitmap.c:492:12: warning: Access to field 'name' results in a dereference of a null pointer (loaded from variable 'bm')

    g_free(bm->name);
           ^~~~~~~~
Reported-by Clang Static Analyzer
---
 block/qcow2-bitmap.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
block/qcow2-bitmap.c +2 -1
index fe72df5057..2fd75781ce 100644
@@ -1259,7 +1259,7 @@ void qcow2_remove_persistent_dirty_bitmap(BlockDriverState *bs,
 
     bm = find_bitmap_by_name(bm_list, name);
     if (bm == NULL) {
-        goto fail;
+        goto fail_list;
     }
 
     QSIMPLEQ_REMOVE(bm_list, bm, Qcow2Bitmap, entry);
@@ -1274,6 +1274,7 @@ void qcow2_remove_persistent_dirty_bitmap(BlockDriverState *bs,
 
 fail:
     bitmap_free(bm);
+fail_list:
     bitmap_list_free(bm_list);
 }
 
Signature · 1 line
2.13.3
#18 lore raw

Re: [Qemu-devel] [PATCH for 2.10 07/35] qcow2: fix null pointer dereference

Quoted 5 lines · Philippe Mathieu-Daudé

If find_bitmap_by_name() fails we have bm=NULL and go to the 'fail' label, then call bitmap_free(bm) which does g_free(bm->name) with bm=NULL...

Clang's scan-build-5.0 output:
block/qcow2-bitmap.c:492:12: warning: Access to field 'name' results in a dereference of a null pointer (loaded from variable 'bm')

Quoted 8 lines
    g_free(bm->name);
           ^~~~~~~~
Reported-by Clang Static Analyzer
---
 block/qcow2-bitmap.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)

Max already has a better patch from Vladimir pending on his block queue:
https://lists.gnu.org/archive/html/qemu-devel/2017-07/msg04398.html

Signature · 3 lines
Eric Blake, Principal Software Engineer
Red Hat, Inc.           +1-919-301-3266
Virtualization:  qemu.org | libvirt.org
#19 lore raw

Re: [Qemu-devel] [PATCH for 2.10 07/35] qcow2: fix null pointer dereference

Hi Eric,

Quoted 9 lines · Eric Blake
Quoted 8 lines · Philippe Mathieu-Daudé

If find_bitmap_by_name() fails we have bm=NULL and go to the 'fail' label, then call bitmap_free(bm) which does g_free(bm->name) with bm=NULL...

Clang's scan-build-5.0 output:
block/qcow2-bitmap.c:492:12: warning: Access to field 'name' results in a dereference of a null pointer (loaded from variable 'bm')

     g_free(bm->name);
            ^~~~~~~~
Quoted 8 lines
Quoted 5 lines
Reported-by Clang Static Analyzer
---
  block/qcow2-bitmap.c | 3 ++-
  1 file changed, 2 insertions(+), 1 deletion(-)

Max already has a better patch from Vladimir pending on his block queue:
https://lists.gnu.org/archive/html/qemu-devel/2017-07/msg04398.html

Oh I missed that.
I'm not sure it is "better", it may be safer although ;)

Patch dropped.

Regards,

Phil.

#20 lore raw

PATCH 8/35 qcow2: fix null pointer dereference

In reply to Philippe Mathieu-Daudé

It seems this assert() was somehow misplaced.

block/qcow2-refcount.c:2193:42: warning: Array access (from variable 'on_disk_reftable') results in a null pointer dereference

        on_disk_reftable[refblock_index] = refblock_offset;
        ~~~~~~~~~~~~~~~~                 ^
Reported-by Clang Static Analyzer
---
 block/qcow2-refcount.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
block/qcow2-refcount.c +2 -2
index c9b0dcb4f3..168fc32e7b 100644
@@ -2189,6 +2189,8 @@ write_refblocks:
              * this will leak that range, but we can easily fix that by running
              * a leak-fixing check after this rebuild operation */
             reftable_offset = -1;
+        } else {
+            assert(on_disk_reftable);
         }
         on_disk_reftable[refblock_index] = refblock_offset;
 
@@ -2258,8 +2260,6 @@ write_refblocks:
         goto write_refblocks;
     }
 
-    assert(on_disk_reftable);
-
     for (refblock_index = 0; refblock_index < reftable_size; refblock_index++) {
         cpu_to_be64s(&on_disk_reftable[refblock_index]);
     }
Signature · 1 line
2.13.3
Reviewed-by on v1 8/35
#21 lore raw

Re: [Qemu-devel] [PATCH for 2.10 08/35] qcow2: fix null pointer dereference

Quoted 11 lines · Philippe Mathieu-Daudé

It seems this assert() was somehow misplaced.

block/qcow2-refcount.c:2193:42: warning: Array access (from variable 'on_disk_reftable') results in a null pointer dereference

        on_disk_reftable[refblock_index] = refblock_offset;
        ~~~~~~~~~~~~~~~~                 ^
Reported-by Clang Static Analyzer
---
 block/qcow2-refcount.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
Quoted 24 lines
block/qcow2-refcount.c +2 -2
index c9b0dcb4f3..168fc32e7b 100644
@@ -2189,6 +2189,8 @@ write_refblocks:
              * this will leak that range, but we can easily fix that by running
              * a leak-fixing check after this rebuild operation */
             reftable_offset = -1;
+        } else {
+            assert(on_disk_reftable);
         }
         on_disk_reftable[refblock_index] = refblock_offset;
 
@@ -2258,8 +2260,6 @@ write_refblocks:
         goto write_refblocks;
     }
 
-    assert(on_disk_reftable);
-
     for (refblock_index = 0; refblock_index < reftable_size; refblock_index++) {
         cpu_to_be64s(&on_disk_reftable[refblock_index]);
     }
Signature · 3 lines
Eric Blake, Principal Software Engineer
Red Hat, Inc.           +1-919-301-3266
Virtualization:  qemu.org | libvirt.org
#22 lore raw

PATCH 9/35 ui/vnc: fix leak of SocketAddress **

In reply to Philippe Mathieu-Daudé

Extract the (correct) cleaning code as a new function vnc_free_addresses() then use it to remove the memory leaks.

Reported-by Clang Static Analyzer
---
 ui/vnc.c | 36 ++++++++++++++++++------------------
 1 file changed, 18 insertions(+), 18 deletions(-)
ui/vnc.c +18 -18
index eb91559b6b..651cbb8606 100644
@@ -3521,6 +3521,20 @@ static int vnc_display_get_address(const char *addrstr,
     return ret;
 }
 
+static void vnc_free_addresses(SocketAddress ***retsaddr,
+                               size_t *retnsaddr)
+{
+    size_t i;
+
+    for (i = 0; i < *retnsaddr; i++) {
+        qapi_free_SocketAddress((*retsaddr)[i]);
+    }
+    g_free(*retsaddr);
+
+    *retsaddr = NULL;
+    *retnsaddr = 0;
+}
+
 static int vnc_display_get_addresses(QemuOpts *opts,
                                      bool reverse,
                                      SocketAddress ***retsaddr,
@@ -3538,7 +3552,6 @@ static int vnc_display_get_addresses(QemuOpts *opts,
     bool has_ipv6 = qemu_opt_get(opts, "ipv6");
     bool ipv4 = qemu_opt_get_bool(opts, "ipv4", false);
     bool ipv6 = qemu_opt_get_bool(opts, "ipv6", false);
-    size_t i;
     int displaynum = -1;
     int ret = -1;
 
@@ -3614,16 +3627,8 @@ static int vnc_display_get_addresses(QemuOpts *opts,
     ret = 0;
  cleanup:
     if (ret < 0) {
-        for (i = 0; i < *retnsaddr; i++) {
-            qapi_free_SocketAddress((*retsaddr)[i]);
-        }
-        g_free(*retsaddr);
-        for (i = 0; i < *retnwsaddr; i++) {
-            qapi_free_SocketAddress((*retwsaddr)[i]);
-        }
-        g_free(*retwsaddr);
-        *retsaddr = *retwsaddr = NULL;
-        *retnsaddr = *retnwsaddr = 0;
+        vnc_free_addresses(retsaddr, retnsaddr);
+        vnc_free_addresses(retwsaddr, retnwsaddr);
     }
     return ret;
 }
@@ -3772,7 +3777,6 @@ void vnc_display_open(const char *id, Error **errp)
     int acl = 0;
     int lock_key_sync = 1;
     int key_delay_ms;
-    size_t i;
 
     if (!vd) {
         error_setg(errp, "VNC display not active");
@@ -3993,12 +3997,8 @@ void vnc_display_open(const char *id, Error **errp)
     }
 
  cleanup:
-    for (i = 0; i < nsaddr; i++) {
-        qapi_free_SocketAddress(saddr[i]);
-    }
-    for (i = 0; i < nwsaddr; i++) {
-        qapi_free_SocketAddress(wsaddr[i]);
-    }
+    vnc_free_addresses(&saddr, &nsaddr);
+    vnc_free_addresses(&wsaddr, &nwsaddr);
     return;
 
 fail:
Signature · 1 line
2.13.3
Reviewed-by on v1 9/35
#23 lore raw

Re: [Qemu-devel] [PATCH for 2.10 09/35] ui/vnc: fix leak of SocketAddress **

Quoted 8 lines · Philippe Mathieu-Daudé

Extract the (correct) cleaning code as a new function vnc_free_addresses() then use it to remove the memory leaks.

Reported-by Clang Static Analyzer
---
 ui/vnc.c | 36 ++++++++++++++++++------------------
 1 file changed, 18 insertions(+), 18 deletions(-)

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

PATCH 10/35 net/eth: fix incorrect check of iov_to_buf() return value

In reply to Philippe Mathieu-Daudé

So we have sizeof(struct in6_address) != sizeof(uintptr_t) and Clang > Coverity on this, see 4555ca6816c :)

net/eth.c:426:30: warning: The code calls sizeof() on a pointer type. This can produce an unexpected result

        return bytes_read == sizeof(dst_addr);
                             ^     ~~~~~~~~~~
net/eth.c:475:34: warning: The code calls sizeof() on a pointer type. This can produce an unexpected result
            return bytes_read == sizeof(src_addr);
                                 ^     ~~~~~~~~~~
Reported-by Clang Static Analyzer
---
 net/eth.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
net/eth.c +2 -2
index 5b9ba26a56..ae5d881aae 100644
@@ -423,7 +423,7 @@ _eth_get_rss_ex_dst_addr(const struct iovec *pkt, int pkt_frags,
                                 rthdr_offset + sizeof(*ext_hdr),
                                 dst_addr, sizeof(*dst_addr));
 
-        return bytes_read == sizeof(dst_addr);
+        return bytes_read == sizeof(*dst_addr);
     }
 
     return false;
@@ -472,7 +472,7 @@ _eth_get_rss_ex_src_addr(const struct iovec *pkt, int pkt_frags,
                                     opt_offset + sizeof(opthdr),
                                     src_addr, sizeof(*src_addr));
 
-            return bytes_read == sizeof(src_addr);
+            return bytes_read == sizeof(*src_addr);
         }
 
         opt_offset += optlen;
Signature · 1 line
2.13.3
Reviewed-by on v1 10/35
#25 lore raw

Re: [Qemu-devel] [PATCH for 2.10 10/35] net/eth: fix incorrect check of iov_to_buf() return value

Quoted 33 lines · Philippe Mathieu-Daudé

So we have sizeof(struct in6_address) != sizeof(uintptr_t) and Clang > Coverity on this, see 4555ca6816c :)

net/eth.c:426:30: warning: The code calls sizeof() on a pointer type. This can produce an unexpected result

       return bytes_read == sizeof(dst_addr);
                            ^     ~~~~~~~~~~
net/eth.c:475:34: warning: The code calls sizeof() on a pointer type. This can produce an unexpected result
           return bytes_read == sizeof(src_addr);
                                ^     ~~~~~~~~~~
Reported-by Clang Static Analyzer

---
net/eth.c | 4 ++--

1 file changed, 2 insertions(+), 2 deletions(-)
net/eth.c +1 -1
index 5b9ba26a56..ae5d881aae 100644
@@ -423,7 +423,7 @@ _eth_get_rss_ex_dst_addr(const struct iovec *pkt, int pkt_frags,
                                rthdr_offset + sizeof(*ext_hdr),
                                dst_addr, sizeof(*dst_addr));
 
-        return bytes_read == sizeof(dst_addr);
+        return bytes_read == sizeof(*dst_addr);
    }
 
    return false;

net/eth.c _eth_get_rss_ex_src_addr(const struct iovec *pkt, int pkt_frags, · line 477

Quoted 7 lines
net/eth.c +1 -1
@@ -472,7 +472,7 @@ _eth_get_rss_ex_src_addr(const struct iovec *pkt, int pkt_frags,
                                    opt_offset + sizeof(opthdr),
                                    src_addr, sizeof(*src_addr));
 
-            return bytes_read == sizeof(src_addr);
+            return bytes_read == sizeof(*src_addr);
        }
 
Quoted 4 lines
net/eth.c +0 -0
        opt_offset += optlen;
Signature · 1 line
2.13.3
#26 lore raw

PATCH 11/35 i2c/exynos4210: correctly check i2c_recv() return value

In reply to Philippe Mathieu-Daudé

i2c_recv() returns -1 on error, if the I2CCON_ACK_GEN bit was not set this code was setting i2cds = -1.

i2c/exynos4210_i2c.c:117:20: warning: Loss of sign in implicit conversion

        s->i2cds = ret;
                   ^~~
Reported-by Clang Static Analyzer
---
 hw/i2c/exynos4210_i2c.c | 8 +++++---
 1 file changed, 5 insertions(+), 3 deletions(-)
hw/i2c/exynos4210_i2c.c +5 -3
index c96fa7d7be..4424dbd233 100644
@@ -111,10 +111,12 @@ static void exynos4210_i2c_data_receive(void *opaque)
     s->i2cstat &= ~I2CSTAT_LAST_BIT;
     s->scl_free = false;
     ret = i2c_recv(s->bus);
-    if (ret < 0 && (s->i2ccon & I2CCON_ACK_GEN)) {
-        s->i2cstat |= I2CSTAT_LAST_BIT;  /* Data is not acknowledged */
-    } else {
+    if (ret >= 0) {
         s->i2cds = ret;
+    } else {
+        if (s->i2ccon & I2CCON_ACK_GEN) {
+            s->i2cstat |= I2CSTAT_LAST_BIT;  /* Data is not acknowledged */
+        }
     }
     exynos4210_i2c_raise_interrupt(s);
 }
Signature · 1 line
2.13.3
#27 lore raw

Re: [Qemu-arm] [Qemu-devel] [PATCH for 2.10 11/35] i2c/exynos4210: correctly check i2c_recv() return value

Quoted 24 lines · Philippe Mathieu-Daudé

i2c_recv() returns -1 on error, if the I2CCON_ACK_GEN bit was not set this code was setting i2cds = -1.

i2c/exynos4210_i2c.c:117:20: warning: Loss of sign in implicit conversion

        s->i2cds = ret;
                   ^~~
Reported-by Clang Static Analyzer
---
 hw/i2c/exynos4210_i2c.c | 8 +++++---
 1 file changed, 5 insertions(+), 3 deletions(-)
hw/i2c/exynos4210_i2c.c +0 -3
index c96fa7d7be..4424dbd233 100644
@@ -111,10 +111,12 @@ static void exynos4210_i2c_data_receive(void *opaque)
     s->i2cstat &= ~I2CSTAT_LAST_BIT;
     s->scl_free = false;
     ret = i2c_recv(s->bus);
-    if (ret < 0 && (s->i2ccon & I2CCON_ACK_GEN)) {
-        s->i2cstat |= I2CSTAT_LAST_BIT;  /* Data is not acknowledged */
-    } else {

hw/i2c/exynos4210_i2c.c static void exynos4210_i2c_data_receive(void *opaque)

Quoted 10 lines
hw/i2c/exynos4210_i2c.c +5 -1
+    if (ret >= 0) {
         s->i2cds = ret;
+    } else {
+        if (s->i2ccon & I2CCON_ACK_GEN) {
+            s->i2cstat |= I2CSTAT_LAST_BIT;  /* Data is not acknowledged */
+        }
     }
     exynos4210_i2c_raise_interrupt(s);
 }
--

Have you checked this change against the data sheet for the device?

thanks
-- PMM

#28 lore raw

Re: [Qemu-devel] [PATCH for 2.10 11/35] i2c/exynos4210: correctly check i2c_recv() return value

Quoted 30 lines · Peter Maydell
Quoted 29 lines · Philippe Mathieu-Daudé

i2c_recv() returns -1 on error, if the I2CCON_ACK_GEN bit was not set this code was setting i2cds = -1.

i2c/exynos4210_i2c.c:117:20: warning: Loss of sign in implicit conversion

         s->i2cds = ret;
                    ^~~
Reported-by Clang Static Analyzer
---
  hw/i2c/exynos4210_i2c.c | 8 +++++---
  1 file changed, 5 insertions(+), 3 deletions(-)
hw/i2c/exynos4210_i2c.c +4 -3
index c96fa7d7be..4424dbd233 100644
@@ -111,10 +111,12 @@ static void exynos4210_i2c_data_receive(void *opaque)
      s->i2cstat &= ~I2CSTAT_LAST_BIT;
      s->scl_free = false;
      ret = i2c_recv(s->bus);
-    if (ret < 0 && (s->i2ccon & I2CCON_ACK_GEN)) {
-        s->i2cstat |= I2CSTAT_LAST_BIT;  /* Data is not acknowledged */
-    } else {
+    if (ret >= 0) {
          s->i2cds = ret;
+    } else {
+        if (s->i2ccon & I2CCON_ACK_GEN) {
+            s->i2cstat |= I2CSTAT_LAST_BIT;  /* Data is not acknowledged */
Quoted 8 lines

hw/i2c/exynos4210_i2c.c static void exynos4210_i2c_data_receive(void *opaque)

Quoted 5 lines
hw/i2c/exynos4210_i2c.c +1 -1
+        }
      }
      exynos4210_i2c_raise_interrupt(s);
  }
--

Have you checked this change against the data sheet for the device?

Here is the relevant part of the Exynos4210_UM DS:

[*] 14.3.5 Data Transfer Format
... If the I2C-bus is operating in Master mode, master transmits the address field. Each byte should be followed by an acknowledgement (ACK) bit.

[*] 14.3.6 ACK Signal Transmission
To complete a one-byte transfer operation, the receiver sends an ACK bit to the transmitter. The ACK pulse occurs at the ninth clock of the SCL line. ... The software (I2CSTAT) enables or disables ACK bit transmit function. However, the ACK pulse on the ninth clock of SCL is required to complete the one-byte data transfer operation.

[*] 14.3.9 Abort Conditions
If a slave receiver cannot acknowledge the confirmation of the slave address, it holds the level of the SDA line High. In this case, the master generates a Stop condition and cancels the transfer.
If a master receiver is involved in the aborted transfer, it signals the end of slave transmit operation by canceling the generation of an ACK after the last data byte received from the slave. The slave transmitter releases the SDA to allow a master to generate a Stop condition.

I2C-bus last-received bit status flag bit. (I2CSTAT_LAST_BIT) 0 = Last-received bit is 0 (ACK was received).
1 = Last-received bit is 1 (ACK was not received).

An I2C-bus interrupt occurs if 1) if a 1-byte transmit or receive operation is complete. In other words, ack period is finished. 2) A general call or a slave address match occurs, 3) Bus arbitration fails.

--

So the current code is not wrong and matches the datashit, crap is shifted into I2CDS and the guest has to poll I2CSTAT to check the peer ACK... Still it is weird to shift 0xff as of the current implementation, but it might have some usefulness while debugging, who knows...

I might add few comments in that file during 2.11 cycle.

Thank for the review!

Patch dropped.

Regards,

Phil.

#29 lore raw

PATCH 12/35 vfio/platform: fix use of freed memory

In reply to Philippe Mathieu-Daudé

free the data _after_ using it.

hw/vfio/platform.c:126:29: warning: Use of memory after it is freed

        qemu_set_fd_handler(*pfd, NULL, NULL, NULL);
                            ^~~~
Reported-by Clang Static Analyzer
---
 hw/vfio/platform.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
hw/vfio/platform.c +1 -1
index 7c09deda61..da84abf4fc 100644
@@ -120,11 +120,11 @@ static int vfio_set_trigger_eventfd(VFIOINTp *intp,
     *pfd = event_notifier_get_fd(intp->interrupt);
     qemu_set_fd_handler(*pfd, (IOHandler *)handler, NULL, intp);
     ret = ioctl(vbasedev->fd, VFIO_DEVICE_SET_IRQS, irq_set);
-    g_free(irq_set);
     if (ret < 0) {
         error_report("vfio: Failed to set trigger eventfd: %m");
         qemu_set_fd_handler(*pfd, NULL, NULL, NULL);
     }
+    g_free(irq_set);
     return ret;
 }
 
Signature · 1 line
2.13.3
Reviewed-by on v1 12/35
#30 lore raw

Re: [Qemu-devel] [PATCH for 2.10 12/35] vfio/platform: fix use of freed memory

Quoted 20 lines · Philippe Mathieu-Daudé

free the data _after_ using it.

hw/vfio/platform.c:126:29: warning: Use of memory after it is freed

        qemu_set_fd_handler(*pfd, NULL, NULL, NULL);
                            ^~~~
Reported-by Clang Static Analyzer
---
 hw/vfio/platform.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
hw/vfio/platform.c +0 -0
index 7c09deda61..da84abf4fc 100644
@@ -120,11 +120,11 @@ static int vfio_set_trigger_eventfd(VFIOINTp *intp,
     *pfd = event_notifier_get_fd(intp->interrupt);
     qemu_set_fd_handler(*pfd, (IOHandler *)handler, NULL, intp);

hw/vfio/platform.c static int vfio_set_trigger_eventfd(VFIOINTp *intp, · line 130

Quoted 10 lines
hw/vfio/platform.c +1 -1
     ret = ioctl(vbasedev->fd, VFIO_DEVICE_SET_IRQS, irq_set);
-    g_free(irq_set);
     if (ret < 0) {
         error_report("vfio: Failed to set trigger eventfd: %m");
         qemu_set_fd_handler(*pfd, NULL, NULL, NULL);
     }
+    g_free(irq_set);
     return ret;
 }
 
#31 lore raw

Re: [Qemu-devel] [PATCH for 2.10 12/35] vfio/platform: fix use of freed memory

In reply to Philippe Mathieu-Daudé
Quoted 19 lines · On Mon, 24 Jul 2017 15:27:28 -0300 Philippe Mathieu-Daudé <[email protected]>…

free the data _after_ using it.

hw/vfio/platform.c:126:29: warning: Use of memory after it is freed

        qemu_set_fd_handler(*pfd, NULL, NULL, NULL);
                            ^~~~
Reported-by Clang Static Analyzer
---
 hw/vfio/platform.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
hw/vfio/platform.c +0 -0
index 7c09deda61..da84abf4fc 100644
@@ -120,11 +120,11 @@ static int vfio_set_trigger_eventfd(VFIOINTp *intp,
     *pfd = event_notifier_get_fd(intp->interrupt);
     qemu_set_fd_handler(*pfd, (IOHandler *)handler, NULL, intp);

hw/vfio/platform.c static int vfio_set_trigger_eventfd(VFIOINTp *intp, · line 130

Quoted 10 lines
hw/vfio/platform.c +1 -1
     ret = ioctl(vbasedev->fd, VFIO_DEVICE_SET_IRQS, irq_set);
-    g_free(irq_set);
     if (ret < 0) {
         error_report("vfio: Failed to set trigger eventfd: %m");
         qemu_set_fd_handler(*pfd, NULL, NULL, NULL);
     }
+    g_free(irq_set);
     return ret;
 }
 

I'll snag this and 13/35 for a pull request with Paolo's R-b. Connie has already put a fix in for the other vfio related one, 14/35. Thanks!

Alex

#32 lore raw

PATCH 13/35 vfio/pci: fix use of freed memory

In reply to Philippe Mathieu-Daudé

hw/vfio/pci.c:308:29: warning: Use of memory after it is freed

        qemu_set_fd_handler(*pfd, NULL, NULL, vdev);
                            ^~~~
Reported-by Clang Static Analyzer
---
 hw/vfio/pci.c | 11 +++++++----
 1 file changed, 7 insertions(+), 4 deletions(-)
hw/vfio/pci.c +7 -4
index d4051cb951..31e1edf447 100644
@@ -257,7 +257,7 @@ static void vfio_intx_update(PCIDevice *pdev)
 static int vfio_intx_enable(VFIOPCIDevice *vdev, Error **errp)
 {
     uint8_t pin = vfio_pci_read_config(&vdev->pdev, PCI_INTERRUPT_PIN, 1);
-    int ret, argsz;
+    int ret, argsz, retval = 0;
     struct vfio_irq_set *irq_set;
     int32_t *pfd;
     Error *err = NULL;
@@ -302,12 +302,12 @@ static int vfio_intx_enable(VFIOPCIDevice *vdev, Error **errp)
     qemu_set_fd_handler(*pfd, vfio_intx_interrupt, NULL, vdev);
 
     ret = ioctl(vdev->vbasedev.fd, VFIO_DEVICE_SET_IRQS, irq_set);
-    g_free(irq_set);
     if (ret) {
         error_setg_errno(errp, -ret, "failed to setup INTx fd");
         qemu_set_fd_handler(*pfd, NULL, NULL, vdev);
         event_notifier_cleanup(&vdev->intx.interrupt);
-        return -errno;
+        retval = -errno;
+        goto cleanup;
     }
 
     vfio_intx_enable_kvm(vdev, &err);
@@ -319,7 +319,10 @@ static int vfio_intx_enable(VFIOPCIDevice *vdev, Error **errp)
 
     trace_vfio_intx_enable(vdev->vbasedev.name);
 
-    return 0;
+cleanup:
+    g_free(irq_set);
+
+    return retval;
 }
 
 static void vfio_intx_disable(VFIOPCIDevice *vdev)
Signature · 1 line
2.13.3
Reviewed-by on v1 13/35
#33 lore raw

Re: [Qemu-devel] [PATCH for 2.10 13/35] vfio/pci: fix use of freed memory

Quoted 41 lines · Philippe Mathieu-Daudé

hw/vfio/pci.c:308:29: warning: Use of memory after it is freed

        qemu_set_fd_handler(*pfd, NULL, NULL, vdev);
                            ^~~~
Reported-by Clang Static Analyzer
---
 hw/vfio/pci.c | 11 +++++++----
 1 file changed, 7 insertions(+), 4 deletions(-)
hw/vfio/pci.c +3 -3
index d4051cb951..31e1edf447 100644
@@ -257,7 +257,7 @@ static void vfio_intx_update(PCIDevice *pdev)
 static int vfio_intx_enable(VFIOPCIDevice *vdev, Error **errp)
 {
     uint8_t pin = vfio_pci_read_config(&vdev->pdev, PCI_INTERRUPT_PIN, 1);
-    int ret, argsz;
+    int ret, argsz, retval = 0;
     struct vfio_irq_set *irq_set;
     int32_t *pfd;
     Error *err = NULL;
@@ -302,12 +302,12 @@ static int vfio_intx_enable(VFIOPCIDevice *vdev, Error **errp)
     qemu_set_fd_handler(*pfd, vfio_intx_interrupt, NULL, vdev);
 
     ret = ioctl(vdev->vbasedev.fd, VFIO_DEVICE_SET_IRQS, irq_set);
-    g_free(irq_set);
     if (ret) {
         error_setg_errno(errp, -ret, "failed to setup INTx fd");
         qemu_set_fd_handler(*pfd, NULL, NULL, vdev);
         event_notifier_cleanup(&vdev->intx.interrupt);
-        return -errno;
+        retval = -errno;
+        goto cleanup;
     }
 
     vfio_intx_enable_kvm(vdev, &err);
@@ -319,7 +319,10 @@ static int vfio_intx_enable(VFIOPCIDevice *vdev, Error **errp)
 

hw/vfio/pci.c static int vfio_intx_enable(VFIOPCIDevice *vdev, Error **errp) · line 328

Quoted 10 lines
hw/vfio/pci.c +4 -1
     trace_vfio_intx_enable(vdev->vbasedev.name);
 
-    return 0;
+cleanup:
+    g_free(irq_set);
+
+    return retval;
 }
 
 static void vfio_intx_disable(VFIOPCIDevice *vdev)
#34 lore raw

PATCH 14/35 vfio/ccw: fix incorrect malloc() size

In reply to Philippe Mathieu-Daudé

Since sizeof(struct vfio_irq_info) < sizeof(struct vfio_irq_set) a heap overflow never occured. Still, let's use the correct size.

hw/vfio/ccw.c:170:16: warning: Cast a region whose size is not a multiple of the destination type size

    irq_info = g_malloc0(sizeof(*irq_set));
               ^~~~~~~~~~~~~~~~~~~~~~~~~~~
Reported-by Clang Static Analyzer
---
 hw/vfio/ccw.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
hw/vfio/ccw.c +1 -1
index 12d0262336..8d97b53e77 100644
@@ -168,7 +168,7 @@ static void vfio_ccw_register_io_notifier(VFIOCCWDevice *vcdev, Error **errp)
         return;
     }
 
-    argsz = sizeof(*irq_set);
+    argsz = sizeof(*irq_info);
     irq_info = g_malloc0(argsz);
     irq_info->index = VFIO_CCW_IO_IRQ_INDEX;
     irq_info->argsz = argsz;
Signature · 1 line
2.13.3
#35 lore raw

Re: [Qemu-devel] [PATCH for 2.10 14/35] vfio/ccw: fix incorrect malloc() size

Quoted 18 lines · On Mon, 24 Jul 2017 15:27:30 -0300 Philippe Mathieu-Daudé <[email protected]>…

Since sizeof(struct vfio_irq_info) < sizeof(struct vfio_irq_set) a heap overflow never occured. Still, let's use the correct size.

hw/vfio/ccw.c:170:16: warning: Cast a region whose size is not a multiple of the destination type size

    irq_info = g_malloc0(sizeof(*irq_set));
               ^~~~~~~~~~~~~~~~~~~~~~~~~~~
Reported-by Clang Static Analyzer
---
 hw/vfio/ccw.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
hw/vfio/ccw.c +0 -0
index 12d0262336..8d97b53e77 100644

hw/vfio/ccw.c static void vfio_ccw_register_io_notifier(VFIOCCWDevice *vcdev, Error **errp) · line 174

Quoted 8 lines
hw/vfio/ccw.c +1 -1
@@ -168,7 +168,7 @@ static void vfio_ccw_register_io_notifier(VFIOCCWDevice *vcdev, Error **errp)
         return;
     }
 
-    argsz = sizeof(*irq_set);
+    argsz = sizeof(*irq_info);
     irq_info = g_malloc0(argsz);
     irq_info->index = VFIO_CCW_IO_IRQ_INDEX;
     irq_info->argsz = argsz;

Thanks for the patch, but I already have "vfio/ccw: allocate irq info with the right size" queued in my s390-next branch (for which I plan to send a pull req today).

#36 lore raw

PATCH 15/35 usb: correctly handle Zero Length Packets

In reply to Philippe Mathieu-Daudé

USB Specification Revision 2.0, §5.5.3:
The Data stage of a control transfer from an endpoint to the host is complete when the endpoint does one of the following:
• Has transferred exactly the amount of data specified during the Setup stage
• Transfers a packet with a payload size less than wMaxPacketSize or transfers a zero-length packet"

hw/usb/redirect.c:802:9: warning: Declared variable-length array (VLA) has zero size

        uint8_t buf[size];
        ^~~~~~~~~~~ ~~~~
Reported-by Clang Static Analyzer
---
 hw/usb/redirect.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
hw/usb/redirect.c +1 -1
index 5e42730449..5b7073d2cd 100644
@@ -795,7 +795,7 @@ static void usbredir_handle_bulk_data(USBRedirDevice *dev, USBPacket *p,
            usbredirparser_peer_has_cap(dev->parser,
                                        usb_redir_cap_32bits_bulk_length));
 
-    if (ep & USB_DIR_IN) {
+    if (ep & USB_DIR_IN || size == 0) {
         usbredirparser_send_bulk_packet(dev->parser, p->id,
                                         &bulk_packet, NULL, 0);
     } else {
Signature · 1 line
2.13.3
#37 lore raw

Re: [Qemu-devel] [PATCH for 2.10 15/35] usb: correctly handle Zero Length Packets

Quoted 21 lines · Philippe Mathieu-Daudé

USB Specification Revision 2.0, §5.5.3:
The Data stage of a control transfer from an endpoint to the host is complete when the endpoint does one of the following:
• Has transferred exactly the amount of data specified during the Setup stage
• Transfers a packet with a payload size less than wMaxPacketSize or transfers a zero-length packet"

hw/usb/redirect.c:802:9: warning: Declared variable-length array (VLA) has zero size

        uint8_t buf[size];
        ^~~~~~~~~~~ ~~~~
Reported-by Clang Static Analyzer
---
 hw/usb/redirect.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
hw/usb/redirect.c +0 -0
index 5e42730449..5b7073d2cd 100644

hw/usb/redirect.c static void usbredir_handle_bulk_data(USBRedirDevice *dev, USBPacket *p, · line 801

Quoted 8 lines
hw/usb/redirect.c +1 -1
@@ -795,7 +795,7 @@ static void usbredir_handle_bulk_data(USBRedirDevice *dev, USBPacket *p,
            usbredirparser_peer_has_cap(dev->parser,
                                        usb_redir_cap_32bits_bulk_length));
 
-    if (ep & USB_DIR_IN) {
+    if (ep & USB_DIR_IN || size == 0) {
         usbredirparser_send_bulk_packet(dev->parser, p->id,
                                         &bulk_packet, NULL, 0);
     } else {

Ping?

#38 lore raw

Re: [Qemu-devel] [PATCH for 2.10 15/35] usb: correctly handle Zero Length Packets

Quoted 24 lines · Philippe Mathieu-Daudé
Quoted 23 lines · Philippe Mathieu-Daudé

USB Specification Revision 2.0, §5.5.3:
The Data stage of a control transfer from an endpoint to the host is complete when the endpoint does one of the following:
• Has transferred exactly the amount of data specified during the Setup stage
• Transfers a packet with a payload size less than wMaxPacketSize or transfers a zero-length packet"

hw/usb/redirect.c:802:9: warning: Declared variable-length array (VLA) has zero size

        uint8_t buf[size];
        ^~~~~~~~~~~ ~~~~
Reported-by Clang Static Analyzer
---
 hw/usb/redirect.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
hw/usb/redirect.c +0 -0
index 5e42730449..5b7073d2cd 100644
@@ -795,7 +795,7 @@ static void usbredir_handle_bulk_data(USBRedirDevice *dev, USBPacket *p,
            usbredirparser_peer_has_cap(dev->parser,
                                        usb_redir_cap_32bits_bulk_length));
 
Quoted 8 lines

hw/usb/redirect.c static void usbredir_handle_bulk_data(USBRedirDevice *dev, USBPacket *p, · line 801

Quoted 6 lines
hw/usb/redirect.c +1 -1
-    if (ep & USB_DIR_IN) {
+    if (ep & USB_DIR_IN || size == 0) {
         usbredirparser_send_bulk_packet(dev->parser, p->id,
                                         &bulk_packet, NULL, 0);
     } else {

Ping?

Whoops, pretty old one. Don't have this in my qemu-devel archive any more. Can you rebase + resend?

thanks,
Gerd

#39 lore raw

PATCH 16/35 usb/dev-mtp: fix use of uninitialized values

In reply to Philippe Mathieu-Daudé

hw/usb/dev-mtp.c:1212:13: warning: 2nd function call argument is an uninitialized value
o = usb_mtp_object_lookup(s, c->argv[0]); ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Reported-by Clang Static Analyzer
---
 hw/usb/dev-mtp.c | 16 ++++++++++++----
 1 file changed, 12 insertions(+), 4 deletions(-)
hw/usb/dev-mtp.c +12 -4
index 94c2e94f10..6dfece9ea9 100644
@@ -1209,7 +1209,9 @@ static void usb_mtp_command(MTPState *s, MTPControl *c)
         }
         break;
     case CMD_GET_OBJECT_INFO:
-        o = usb_mtp_object_lookup(s, c->argv[0]);
+        if (c->argc > 0) {
+            o = usb_mtp_object_lookup(s, c->argv[0]);
+        }
         if (o == NULL) {
             usb_mtp_queue_result(s, RES_INVALID_OBJECT_HANDLE,
                                  c->trans, 0, 0, 0);
@@ -1218,7 +1220,9 @@ static void usb_mtp_command(MTPState *s, MTPControl *c)
         data_in = usb_mtp_get_object_info(s, c, o);
         break;
     case CMD_GET_OBJECT:
-        o = usb_mtp_object_lookup(s, c->argv[0]);
+        if (c->argc > 0) {
+            o = usb_mtp_object_lookup(s, c->argv[0]);
+        }
         if (o == NULL) {
             usb_mtp_queue_result(s, RES_INVALID_OBJECT_HANDLE,
                                  c->trans, 0, 0, 0);
@@ -1237,7 +1241,9 @@ static void usb_mtp_command(MTPState *s, MTPControl *c)
         }
         break;
     case CMD_GET_PARTIAL_OBJECT:
-        o = usb_mtp_object_lookup(s, c->argv[0]);
+        if (c->argc > 0) {
+            o = usb_mtp_object_lookup(s, c->argv[0]);
+        }
         if (o == NULL) {
             usb_mtp_queue_result(s, RES_INVALID_OBJECT_HANDLE,
                                  c->trans, 0, 0, 0);
@@ -1281,7 +1287,9 @@ static void usb_mtp_command(MTPState *s, MTPControl *c)
         }
         break;
     case CMD_GET_OBJECT_PROP_VALUE:
-        o = usb_mtp_object_lookup(s, c->argv[0]);
+        if (c->argc > 0) {
+            o = usb_mtp_object_lookup(s, c->argv[0]);
+        }
         if (o == NULL) {
             usb_mtp_queue_result(s, RES_INVALID_OBJECT_HANDLE,
                                  c->trans, 0, 0, 0);
Signature · 1 line
2.13.3
#40 lore raw

Re: [Qemu-devel] [PATCH for 2.10 16/35] usb/dev-mtp: fix use of uninitialized values

     case CMD_GET_OBJECT_INFO:

Quoted 4 lines

-        o = usb_mtp_object_lookup(s, c->argv[0]); +        if (c->argc > 0) {
+            o = usb_mtp_object_lookup(s, c->argv[0]); +        }

How about zero-initializing c->argv instead?

cheers,
Gerd

#41 lore raw

Re: [Qemu-devel] [PATCH for 2.10 16/35] usb/dev-mtp: fix use of uninitialized values

Quoted 7 lines · Gerd Hoffmann

case CMD_GET_OBJECT_INFO:

hw/usb/dev-mtp.c static void usb_mtp_command(MTPState *s, MTPControl *c) · line 1214

Quoted 4 lines
hw/usb/dev-mtp.c +3 -1
-        o = usb_mtp_object_lookup(s, c->argv[0]);
+        if (c->argc > 0) {
+            o = usb_mtp_object_lookup(s, c->argv[0]);
+        }

How about zero-initializing c->argv instead?

I checked the MTP specs rev. 1.1 and I understand the case argc == 0 fits in "Invalid Parameter" section (F.2.30, code 0x201d).

So the correct patch is to queue a RES_INVALID_PARAMETER result.

I'll send another patch but since this require heavy testing this is probably 2.11 material now.

Regards,

Phil.

#42 lore raw

PATCH 17/35 usb/dev-mtp: fix use of uninitialized values

In reply to Philippe Mathieu-Daudé
Reported-by Clang Static Analyzer
---
 hw/usb/dev-mtp.c | 36 +++++++++++++++++++++---------------
 1 file changed, 21 insertions(+), 15 deletions(-)
hw/usb/dev-mtp.c +21 -15
index 6dfece9ea9..ad64495f05 100644
@@ -1134,7 +1134,7 @@ static void usb_mtp_command(MTPState *s, MTPControl *c)
                                  c->trans, 1, s->session, 0);
             return;
         }
-        if (c->argv[0] == 0) {
+        if (c->argc == 0 || c->argv[0] == 0) {
             usb_mtp_queue_result(s, RES_INVALID_PARAMETER,
                                  c->trans, 0, 0, 0);
             return;
@@ -1162,8 +1162,9 @@ static void usb_mtp_command(MTPState *s, MTPControl *c)
         data_in = usb_mtp_get_storage_ids(s, c);
         break;
     case CMD_GET_STORAGE_INFO:
-        if (c->argv[0] != QEMU_STORAGE_ID &&
-            c->argv[0] != 0xffffffff) {
+        if (c->argc == 0 ||
+           (c->argv[0] != QEMU_STORAGE_ID &&
+            c->argv[0] != 0xffffffff)) {
             usb_mtp_queue_result(s, RES_INVALID_STORAGE_ID,
                                  c->trans, 0, 0, 0);
             return;
@@ -1172,22 +1173,25 @@ static void usb_mtp_command(MTPState *s, MTPControl *c)
         break;
     case CMD_GET_NUM_OBJECTS:
     case CMD_GET_OBJECT_HANDLES:
-        if (c->argv[0] != QEMU_STORAGE_ID &&
-            c->argv[0] != 0xffffffff) {
+        if (c->argc == 0 ||
+           (c->argv[0] != QEMU_STORAGE_ID &&
+            c->argv[0] != 0xffffffff)) {
             usb_mtp_queue_result(s, RES_INVALID_STORAGE_ID,
                                  c->trans, 0, 0, 0);
             return;
         }
-        if (c->argv[1] != 0x00000000) {
+        if (c->argc > 1 && c->argv[1] != 0x00000000) {
             usb_mtp_queue_result(s, RES_SPEC_BY_FORMAT_UNSUPPORTED,
                                  c->trans, 0, 0, 0);
             return;
         }
-        if (c->argv[2] == 0x00000000 ||
-            c->argv[2] == 0xffffffff) {
-            o = QTAILQ_FIRST(&s->objects);
-        } else {
-            o = usb_mtp_object_lookup(s, c->argv[2]);
+        if (c->argc > 2) {
+            if (c->argv[2] == 0x00000000 ||
+                c->argv[2] == 0xffffffff) {
+                o = QTAILQ_FIRST(&s->objects);
+            } else {
+                o = usb_mtp_object_lookup(s, c->argv[2]);
+            }
         }
         if (o == NULL) {
             usb_mtp_queue_result(s, RES_INVALID_OBJECT_HANDLE,
@@ -1264,8 +1268,9 @@ static void usb_mtp_command(MTPState *s, MTPControl *c)
         res0 = data_in->length;
         break;
     case CMD_GET_OBJECT_PROPS_SUPPORTED:
-        if (c->argv[0] != FMT_UNDEFINED_OBJECT &&
-            c->argv[0] != FMT_ASSOCIATION) {
+        if (c->argc == 0 ||
+           (c->argv[0] != FMT_UNDEFINED_OBJECT &&
+            c->argv[0] != FMT_ASSOCIATION)) {
             usb_mtp_queue_result(s, RES_INVALID_OBJECT_FORMAT_CODE,
                                  c->trans, 0, 0, 0);
             return;
@@ -1273,8 +1278,9 @@ static void usb_mtp_command(MTPState *s, MTPControl *c)
         data_in = usb_mtp_get_object_props_supported(s, c);
         break;
     case CMD_GET_OBJECT_PROP_DESC:
-        if (c->argv[1] != FMT_UNDEFINED_OBJECT &&
-            c->argv[1] != FMT_ASSOCIATION) {
+        if (c->argc > 1 &&
+           (c->argv[1] != FMT_UNDEFINED_OBJECT &&
+            c->argv[1] != FMT_ASSOCIATION)) {
             usb_mtp_queue_result(s, RES_INVALID_OBJECT_FORMAT_CODE,
                                  c->trans, 0, 0, 0);
             return;
Signature · 1 line
2.13.3
#43 lore raw

Re: [Qemu-devel] [PATCH for 2.10 17/35] usb/dev-mtp: fix use of uninitialized values

Quoted 11 lines · Philippe Mathieu-Daudé
Reported-by Clang Static Analyzer
---
  hw/usb/dev-mtp.c | 36 +++++++++++++++++++++---------------
  1 file changed, 21 insertions(+), 15 deletions(-)
hw/usb/dev-mtp.c +0 -0
index 6dfece9ea9..ad64495f05 100644

hw/usb/dev-mtp.c static void usb_mtp_command(MTPState *s, MTPControl *c) · line 1139

Quoted 7 lines
hw/usb/dev-mtp.c +1 -1
@@ -1134,7 +1134,7 @@ static void usb_mtp_command(MTPState *s, MTPControl *c)
                                   c->trans, 1, s->session, 0);
              return;
          }
-        if (c->argv[0] == 0) {
+        if (c->argc == 0 || c->argv[0] == 0) {
              usb_mtp_queue_result(s, RES_INVALID_PARAMETER,
                                   c->trans, 0, 0, 0);

^ This is OK,

but part below is incorrect, after reading the MTP specs 1.1 I understood the correct code to return is RES_INVALID_PARAMETER.

Quoted 72 lines
hw/usb/dev-mtp.c +20 -14
              return;
@@ -1162,8 +1162,9 @@ static void usb_mtp_command(MTPState *s, MTPControl *c)
          data_in = usb_mtp_get_storage_ids(s, c);
          break;
      case CMD_GET_STORAGE_INFO:
-        if (c->argv[0] != QEMU_STORAGE_ID &&
-            c->argv[0] != 0xffffffff) {
+        if (c->argc == 0 ||
+           (c->argv[0] != QEMU_STORAGE_ID &&
+            c->argv[0] != 0xffffffff)) {
              usb_mtp_queue_result(s, RES_INVALID_STORAGE_ID,
                                   c->trans, 0, 0, 0);
              return;
@@ -1172,22 +1173,25 @@ static void usb_mtp_command(MTPState *s, MTPControl *c)
          break;
      case CMD_GET_NUM_OBJECTS:
      case CMD_GET_OBJECT_HANDLES:
-        if (c->argv[0] != QEMU_STORAGE_ID &&
-            c->argv[0] != 0xffffffff) {
+        if (c->argc == 0 ||
+           (c->argv[0] != QEMU_STORAGE_ID &&
+            c->argv[0] != 0xffffffff)) {
              usb_mtp_queue_result(s, RES_INVALID_STORAGE_ID,
                                   c->trans, 0, 0, 0);
              return;
          }
-        if (c->argv[1] != 0x00000000) {
+        if (c->argc > 1 && c->argv[1] != 0x00000000) {
              usb_mtp_queue_result(s, RES_SPEC_BY_FORMAT_UNSUPPORTED,
                                   c->trans, 0, 0, 0);
              return;
          }
-        if (c->argv[2] == 0x00000000 ||
-            c->argv[2] == 0xffffffff) {
-            o = QTAILQ_FIRST(&s->objects);
-        } else {
-            o = usb_mtp_object_lookup(s, c->argv[2]);
+        if (c->argc > 2) {
+            if (c->argv[2] == 0x00000000 ||
+                c->argv[2] == 0xffffffff) {
+                o = QTAILQ_FIRST(&s->objects);
+            } else {
+                o = usb_mtp_object_lookup(s, c->argv[2]);
+            }
          }
          if (o == NULL) {
              usb_mtp_queue_result(s, RES_INVALID_OBJECT_HANDLE,
@@ -1264,8 +1268,9 @@ static void usb_mtp_command(MTPState *s, MTPControl *c)
          res0 = data_in->length;
          break;
      case CMD_GET_OBJECT_PROPS_SUPPORTED:
-        if (c->argv[0] != FMT_UNDEFINED_OBJECT &&
-            c->argv[0] != FMT_ASSOCIATION) {
+        if (c->argc == 0 ||
+           (c->argv[0] != FMT_UNDEFINED_OBJECT &&
+            c->argv[0] != FMT_ASSOCIATION)) {
              usb_mtp_queue_result(s, RES_INVALID_OBJECT_FORMAT_CODE,
                                   c->trans, 0, 0, 0);
              return;
@@ -1273,8 +1278,9 @@ static void usb_mtp_command(MTPState *s, MTPControl *c)
          data_in = usb_mtp_get_object_props_supported(s, c);
          break;
      case CMD_GET_OBJECT_PROP_DESC:
-        if (c->argv[1] != FMT_UNDEFINED_OBJECT &&
-            c->argv[1] != FMT_ASSOCIATION) {
+        if (c->argc > 1 &&
+           (c->argv[1] != FMT_UNDEFINED_OBJECT &&
+            c->argv[1] != FMT_ASSOCIATION)) {
              usb_mtp_queue_result(s, RES_INVALID_OBJECT_FORMAT_CODE,
                                   c->trans, 0, 0, 0);
              return;
#44 lore raw

PATCH 18/35 usb/dev-mtp: fix use of uninitialized values

In reply to Philippe Mathieu-Daudé

hw/usb/dev-mtp.c:971:5: warning: 4th function call argument is an uninitialized value

    trace_usb_mtp_op_get_partial_object(s->dev.addr, o->handle, o->path,
                                         c->argv[1], c->argv[2]);
                                                     ^~~~~~~~~~
hw/usb/dev-mtp.c:981:12: warning: Assigned value is garbage or undefined
    offset = c->argv[1];
           ^ ~~~~~~~~~~
Reported-by Clang Static Analyzer
---
 hw/usb/dev-mtp.c | 6 +++++-
 1 file changed, 5 insertions(+), 1 deletion(-)
hw/usb/dev-mtp.c +5 -1
index ad64495f05..b4f0b5dafd 100644
@@ -965,12 +965,16 @@ static MTPData *usb_mtp_get_object(MTPState *s, MTPControl *c,
 static MTPData *usb_mtp_get_partial_object(MTPState *s, MTPControl *c,
                                            MTPObject *o)
 {
-    MTPData *d = usb_mtp_data_alloc(c);
+    MTPData *d;
     off_t offset;
 
+    if (c->argc <= 2) {
+        return NULL;
+    }
     trace_usb_mtp_op_get_partial_object(s->dev.addr, o->handle, o->path,
                                         c->argv[1], c->argv[2]);
 
+    d = usb_mtp_data_alloc(c);
     d->fd = open(o->path, O_RDONLY);
     if (d->fd == -1) {
         usb_mtp_data_free(d);
Signature · 1 line
2.13.3
#45 lore raw

PATCH 19/35 usb/dev-mtp: fix use of uninitialized variable

In reply to Philippe Mathieu-Daudé

hw/usb/dev-mtp.c:1200:15: warning: The left operand of '==' is a garbage value

        if (o == NULL) {
            ~ ^
Reported-by Clang Static Analyzer
---
 hw/usb/dev-mtp.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
hw/usb/dev-mtp.c +1 -1
index b4f0b5dafd..5615fb3634 100644
@@ -1117,7 +1117,7 @@ static MTPData *usb_mtp_get_object_prop_value(MTPState *s, MTPControl *c,
 static void usb_mtp_command(MTPState *s, MTPControl *c)
 {
     MTPData *data_in = NULL;
-    MTPObject *o;
+    MTPObject *o = NULL;
     uint32_t nres = 0, res0 = 0;
 
     /* sanity checks */
Signature · 1 line
2.13.3
#46 lore raw

PATCH 20/35 arm/boot: fix undefined instruction on secondary smp cpu bootloader

In reply to Philippe Mathieu-Daudé

In a ARM multicore system, write_secondary_boot() only initializes fixups for FIXUP_GIC_CPU_IF and FIXUP_BOOTREG, while smpboot[] also uses FIXUP_DSB. This results in write_bootloader() using uninitialized fixupcontext[FIXUP_DSB] instruction in the bootloader code...
Zero-initialize fixupcontext[] to avoid this issue.

hw/arm/boot.c:157:18: warning: Assigned value is garbage or undefined

            insn = fixupcontext[fixup];
                 ^ ~~~~~~~~~~~~~~~~~~~
Reported-by Clang Static Analyzer
---
 hw/arm/boot.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
hw/arm/boot.c +1 -1
index c2720c8046..fb21f113c2 100644
@@ -170,7 +170,7 @@ static void write_bootloader(const char *name, hwaddr addr,
 static void default_write_secondary(ARMCPU *cpu,
                                     const struct arm_boot_info *info)
 {
-    uint32_t fixupcontext[FIXUP_MAX];
+    uint32_t fixupcontext[FIXUP_MAX] = {};
 
     fixupcontext[FIXUP_GIC_CPU_IF] = info->gic_cpu_if_addr;
     fixupcontext[FIXUP_BOOTREG] = info->smp_bootreg_addr;
Signature · 1 line
2.13.3
#47 lore raw

Re: [Qemu-arm] [PATCH for 2.10 20/35] arm/boot: fix undefined instruction on secondary smp cpu bootloader

Quoted 4 lines · Philippe Mathieu-Daudé

In a ARM multicore system, write_secondary_boot() only initializes fixups for FIXUP_GIC_CPU_IF and FIXUP_BOOTREG, while smpboot[] also uses FIXUP_DSB. This results in write_bootloader() using uninitialized fixupcontext[FIXUP_DSB] instruction in the bootloader code...

Hmm? The code does:

    if (arm_feature(&cpu->env, ARM_FEATURE_V7)) {
        fixupcontext[FIXUP_DSB] = DSB_INSN;
    } else {
        fixupcontext[FIXUP_DSB] = CP15_DSB_INSN;
    }

so fixupcontext[FIXUP_DSB] is guaranteed initialized, as are FIXUP_GIC_CPU_IF and FIXUP_BOOTREG, which are the only fixups that the smpboot[] code uses.

thanks
-- PMM

#48 lore raw

Re: [Qemu-devel] [PATCH for 2.10 20/35] arm/boot: fix undefined instruction on secondary smp cpu bootloader

Quoted 14 lines · Peter Maydell
Quoted 4 lines · Philippe Mathieu-Daudé

In a ARM multicore system, write_secondary_boot() only initializes fixups for FIXUP_GIC_CPU_IF and FIXUP_BOOTREG, while smpboot[] also uses FIXUP_DSB. This results in write_bootloader() using uninitialized fixupcontext[FIXUP_DSB] instruction in the bootloader code...

Hmm? The code does:

     if (arm_feature(&cpu->env, ARM_FEATURE_V7)) {
         fixupcontext[FIXUP_DSB] = DSB_INSN;
     } else {
         fixupcontext[FIXUP_DSB] = CP15_DSB_INSN;
     }
Quoted 3 lines

so fixupcontext[FIXUP_DSB] is guaranteed initialized, as are FIXUP_GIC_CPU_IF and FIXUP_BOOTREG, which are the only fixups that the smpboot[] code uses.

Indeed :)

Sorry for the noise, I'll add few hints to the analyzer.

#49 lore raw

PATCH 21/35 arm/sysbus-fdt: fix null pointer dereference

In reply to Philippe Mathieu-Daudé

Use error_report() + exit() instead of error_setg(&error_fatal).

hw/arm/sysbus-fdt.c:322:9: warning: Array access (from variable 'node_path') results in a null pointer dereference

    if (node_path[1]) {
        ^~~~~~~~~~~~
Reported-by Clang Static Analyzer
---
 hw/arm/sysbus-fdt.c | 11 +++++------
 1 file changed, 5 insertions(+), 6 deletions(-)
hw/arm/sysbus-fdt.c +5 -6
index d68e3dcdbd..ad0cc49b19 100644
@@ -315,15 +315,14 @@ static int add_amd_xgbe_fdt_node(SysBusDevice *sbdev, void *opaque)
     node_path = qemu_fdt_node_path(host_fdt, dt_name, vdev->compat,
                                    &error_fatal);
     if (!node_path || !node_path[0]) {
-        error_setg(&error_fatal, "%s unable to retrieve node path for %s/%s",
+        error_report("%s unable to retrieve node path for %s/%s",
                    __func__, dt_name, vdev->compat);
-    }
-
-    if (node_path[1]) {
-        error_setg(&error_fatal, "%s more than one node matching %s/%s!",
+        exit(1);
+    } else if (node_path[1]) {
+        error_report("%s more than one node matching %s/%s!",
                    __func__, dt_name, vdev->compat);
+        exit(1);
     }
-
     g_free(dt_name);
 
     if (vbasedev->num_regions != 5) {
Signature · 1 line
2.13.3
#50 lore raw

Re: [Qemu-arm] [PATCH for 2.10 21/35] arm/sysbus-fdt: fix null pointer dereference

Quoted 5 lines · Philippe Mathieu-Daudé

Use error_report() + exit() instead of error_setg(&error_fatal).

hw/arm/sysbus-fdt.c:322:9: warning: Array access (from variable 'node_path') results in a null pointer dereference

    if (node_path[1]) {
        ^~~~~~~~~~~~

I don't understand what this warning is trying to say. We can't get to this point with a NULL node_path, because of the previous conditional, which is using error_setg(&error_fatal).

Quoted 19 lines
Reported-by Clang Static Analyzer
---
 hw/arm/sysbus-fdt.c | 11 +++++------
 1 file changed, 5 insertions(+), 6 deletions(-)
hw/arm/sysbus-fdt.c +1 -3
index d68e3dcdbd..ad0cc49b19 100644
@@ -315,15 +315,14 @@ static int add_amd_xgbe_fdt_node(SysBusDevice *sbdev, void *opaque)
     node_path = qemu_fdt_node_path(host_fdt, dt_name, vdev->compat,
                                    &error_fatal);
     if (!node_path || !node_path[0]) {
-        error_setg(&error_fatal, "%s unable to retrieve node path for %s/%s",
+        error_report("%s unable to retrieve node path for %s/%s",
                    __func__, dt_name, vdev->compat);
-    }
-

hw/arm/sysbus-fdt.c static int add_amd_xgbe_fdt_node(SysBusDevice *sbdev, void *opaque) · line 326

Quoted 10 lines
hw/arm/sysbus-fdt.c +4 -3
-    if (node_path[1]) {
-        error_setg(&error_fatal, "%s more than one node matching %s/%s!",
+        exit(1);
+    } else if (node_path[1]) {
+        error_report("%s more than one node matching %s/%s!",
                    __func__, dt_name, vdev->compat);
+        exit(1);
     }
-
     g_free(dt_name);

This doesn't seem like an improvement. Now the error handling in the function is an inconsistent mix of error_report()+exit() and error_setg(&error_fatal).

thanks
-- PMM

#51 lore raw

Re: [Qemu-arm] [PATCH for 2.10 21/35] arm/sysbus-fdt: fix null pointer dereference

Quoted 1 line · Peter Maydell

On 24 July 2017 at 19:27, Philippe Mathieu-Daudé <[email protected]> wrote:

...

Quoted 19 lines
Quoted 19 lines
hw/arm/sysbus-fdt.c +4 -5
index d68e3dcdbd..ad0cc49b19 100644
@@ -315,15 +315,14 @@ static int add_amd_xgbe_fdt_node(SysBusDevice *sbdev, void *opaque)
      node_path = qemu_fdt_node_path(host_fdt, dt_name, vdev->compat,
                                     &error_fatal);
      if (!node_path || !node_path[0]) {
-        error_setg(&error_fatal, "%s unable to retrieve node path for %s/%s",
+        error_report("%s unable to retrieve node path for %s/%s",
                     __func__, dt_name, vdev->compat);
-    }
-
-    if (node_path[1]) {
-        error_setg(&error_fatal, "%s more than one node matching %s/%s!",
+        exit(1);
+    } else if (node_path[1]) {
+        error_report("%s more than one node matching %s/%s!",
                     __func__, dt_name, vdev->compat);
Quoted 8 lines

hw/arm/sysbus-fdt.c static int add_amd_xgbe_fdt_node(SysBusDevice *sbdev, void *opaque) · line 326

Quoted 4 lines
hw/arm/sysbus-fdt.c +1 -1
+        exit(1);
      }
-
      g_free(dt_name);

This doesn't seem like an improvement. Now the error handling in the function is an inconsistent mix of error_report()+exit() and error_setg(&error_fatal).

I got this from "qapi/error.h":

156 * Please don't error_setg(&error_fatal, ...), use error_report() and

157 * exit(), because that's more obvious.

158 * Likewise, don't error_setg(&error_abort, ...), use assert().

Is this comment outdated?

#52 lore raw

Re: [Qemu-arm] [PATCH for 2.10 21/35] arm/sysbus-fdt: fix null pointer dereference

Quoted 39 lines · Philippe Mathieu-Daudé
Quoted 2 lines · Peter Maydell

On 24 July 2017 at 19:27, Philippe Mathieu-Daudé <[email protected]> wrote:

...

Quoted 23 lines
Quoted 23 lines
hw/arm/sysbus-fdt.c +5 -5
index d68e3dcdbd..ad0cc49b19 100644
@@ -315,15 +315,14 @@ static int add_amd_xgbe_fdt_node(SysBusDevice
 *sbdev, void *opaque)
      node_path = qemu_fdt_node_path(host_fdt, dt_name, vdev->compat,
                                     &error_fatal);
      if (!node_path || !node_path[0]) {
-        error_setg(&error_fatal, "%s unable to retrieve node path for
 %s/%s",
+        error_report("%s unable to retrieve node path for %s/%s",
                     __func__, dt_name, vdev->compat);
-    }
-
-    if (node_path[1]) {
-        error_setg(&error_fatal, "%s more than one node matching
 %s/%s!",
+        exit(1);
+    } else if (node_path[1]) {
+        error_report("%s more than one node matching %s/%s!",
                     __func__, dt_name, vdev->compat);
+        exit(1);
Quoted 8 lines
Quoted 3 lines
hw/arm/sysbus-fdt.c +0 -1
      }
-
      g_free(dt_name);

This doesn't seem like an improvement. Now the error handling in the function is an inconsistent mix of error_report()+exit() and error_setg(&error_fatal).

Quoted 7 lines

I got this from "qapi/error.h":

156 * Please don't error_setg(&error_fatal, ...), use error_report() and 157 * exit(), because that's more obvious.
158 * Likewise, don't error_setg(&error_abort, ...), use assert().

Is this comment outdated?

Probably not, I expect this code predates it. However my point about inconsistency still stands.

thanks
-- PMM

#53 lore raw

Re: [Qemu-arm] [PATCH for 2.10 21/35] arm/sysbus-fdt: fix null pointer dereference

In reply to Peter Maydell
Quoted 11 lines · Peter Maydell
Quoted 5 lines · Philippe Mathieu-Daudé

Use error_report() + exit() instead of error_setg(&error_fatal).

hw/arm/sysbus-fdt.c:322:9: warning: Array access (from variable 'node_path') results in a null pointer dereference

     if (node_path[1]) {
         ^~~~~~~~~~~~

I don't understand what this warning is trying to say. We can't get to this point with a NULL node_path, because of the previous conditional, which is using error_setg(&error_fatal).

Ok I see, Clang is unaware than error_setg(&error_fatal) is a noreturn.

Patch dropped.

Thanks,

Phil.

#54 lore raw

Re: [Qemu-devel] [PATCH for 2.10 21/35] arm/sysbus-fdt: fix null pointer dereference

Quoted 9 lines · Philippe Mathieu-Daudé
Quoted 8 lines · Peter Maydell
Quoted 6 lines · Philippe Mathieu-Daudé

Use error_report() + exit() instead of error_setg(&error_fatal).

hw/arm/sysbus-fdt.c:322:9: warning: Array access (from variable 'node_path') results in a null pointer dereference

     if (node_path[1]) {
         ^~~~~~~~~~~~
Quoted 6 lines
Quoted 4 lines

I don't understand what this warning is trying to say. We can't get to this point with a NULL node_path, because of the previous conditional, which is using error_setg(&error_fatal).

Ok I see, Clang is unaware than error_setg(&error_fatal) is a noreturn.

Indeed, and that's because error_setg(&error_fatal) is not in preferred form.

Quoted 2 lines

Patch dropped.

That's a shame. Rather, we should patch this file (and others) to avoid all the inconsistent uses of error_setg(&error_*), to comply with the error.h documentation.

Signature · 3 lines
Eric Blake, Principal Software Engineer
Red Hat, Inc.           +1-919-301-3266
Virtualization:  qemu.org | libvirt.org
#55 lore raw

Re: [Qemu-arm] [Qemu-devel] [PATCH for 2.10 21/35] arm/sysbus-fdt: fix null pointer dereference

Quoted 21 lines · Eric Blake
Quoted 9 lines · Philippe Mathieu-Daudé
Quoted 8 lines · Peter Maydell
Quoted 6 lines · Philippe Mathieu-Daudé

Use error_report() + exit() instead of error_setg(&error_fatal).

hw/arm/sysbus-fdt.c:322:9: warning: Array access (from variable 'node_path') results in a null pointer dereference

     if (node_path[1]) {
         ^~~~~~~~~~~~
Quoted 6 lines
Quoted 4 lines

I don't understand what this warning is trying to say. We can't get to this point with a NULL node_path, because of the previous conditional, which is using error_setg(&error_fatal).

Ok I see, Clang is unaware than error_setg(&error_fatal) is a noreturn.

Indeed, and that's because error_setg(&error_fatal) is not in preferred form.

Quoted 1 line
Quoted 5 lines
Quoted 1 line

Patch dropped.

That's a shame. Rather, we should patch this file (and others) to avoid all the inconsistent uses of error_setg(&error_*), to comply with the error.h documentation.

In other words, switching to the preferred spelling in the following files:
device_tree.c
hw/arm/sysbus-fdt.c
hw/block/fdc.c
hw/ppc/spapr_drc.c

is desirable, and has the added benefit of also silencing a Coverity false positive. But it should be done in terms of switching to the preferred spelling, as it touches more instances than just the one that shuts up Coverity.

Signature · 3 lines
Eric Blake, Principal Software Engineer
Red Hat, Inc.           +1-919-301-3266
Virtualization:  qemu.org | libvirt.org
#56 lore raw

Re: [Qemu-arm] [Qemu-devel] [PATCH for 2.10 21/35] arm/sysbus-fdt: fix null pointer dereference

Hi Eric,

Quoted 22 lines · Eric Blake
Quoted 21 lines · Eric Blake
Quoted 9 lines · Philippe Mathieu-Daudé
Quoted 8 lines · Peter Maydell
Quoted 6 lines · Philippe Mathieu-Daudé

Use error_report() + exit() instead of error_setg(&error_fatal).

hw/arm/sysbus-fdt.c:322:9: warning: Array access (from variable 'node_path') results in a null pointer dereference

     if (node_path[1]) {
         ^~~~~~~~~~~~
Quoted 6 lines
Quoted 4 lines

I don't understand what this warning is trying to say. We can't get to this point with a NULL node_path, because of the previous conditional, which is using error_setg(&error_fatal).

Ok I see, Clang is unaware than error_setg(&error_fatal) is a noreturn.

Indeed, and that's because error_setg(&error_fatal) is not in preferred form.

Quoted 1 line
Quoted 5 lines
Quoted 5 lines
Quoted 1 line

Patch dropped.

That's a shame. Rather, we should patch this file (and others) to avoid all the inconsistent uses of error_setg(&error_*), to comply with the error.h documentation.

I started to port/clean this up.
To avoid future inconsistencies, do you think we should/can enforce this check in checkpatch (which is stricter than human review)?
Is the "Qemu error function tests" section a good place to put this check?

Quoted 12 lines

In other words, switching to the preferred spelling in the following files:
device_tree.c
hw/arm/sysbus-fdt.c
hw/block/fdc.c
hw/ppc/spapr_drc.c

is desirable, and has the added benefit of also silencing a Coverity false positive. But it should be done in terms of switching to the preferred spelling, as it touches more instances than just the one that shuts up Coverity.

#57 lore raw

Re: [Qemu-arm] [Qemu-devel] [PATCH for 2.10 21/35] arm/sysbus-fdt: fix null pointer dereference

[adding Markus, as the error maintainer]

Quoted 8 lines · Philippe Mathieu-Daudé
Quoted 8 lines
Quoted 8 lines
Quoted 1 line

Ok I see, Clang is unaware than error_setg(&error_fatal) is a noreturn.

Indeed, and that's because error_setg(&error_fatal) is not in preferred form.

Quoted 2 lines

Patch dropped.

Quoted 7 lines
Quoted 3 lines
Quoted 3 lines

That's a shame. Rather, we should patch this file (and others) to avoid all the inconsistent uses of error_setg(&error_*), to comply with the error.h documentation.

I started to port/clean this up.
To avoid future inconsistencies, do you think we should/can enforce this check in checkpatch (which is stricter than human review)?

Sure, automating good code style is worthwhile.

Quoted 1 line

Is the "Qemu error function tests" section a good place to put this check?

Probably works. I'm not familiar enough with checkpatch as a whole to have any different suggestions off the top of my head (basically, any time I've touched that file, it's been grepping for something that is similar to what I want to be tweaking, rather than reading the whole thing).

Signature · 3 lines
Eric Blake, Principal Software Engineer
Red Hat, Inc.           +1-919-301-3266
Virtualization:  qemu.org | libvirt.org
#58 lore raw

PATCH 22/35 arm/vexpress: fix potential memory leak

In reply to Philippe Mathieu-Daudé

Reorder to only allocate if required.

hw/arm/vexpress.c:667:13: warning: Potential leak of memory pointed to by 'flashalias'

Reported-by Clang Static Analyzer
---
 hw/arm/vexpress.c | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)
hw/arm/vexpress.c +3 -2
index 528c65ddb6..76c4d84482 100644
@@ -553,8 +553,6 @@ static void vexpress_common_init(MachineState *machine)
     MemoryRegion *sysmem = get_system_memory();
     MemoryRegion *vram = g_new(MemoryRegion, 1);
     MemoryRegion *sram = g_new(MemoryRegion, 1);
-    MemoryRegion *flashalias = g_new(MemoryRegion, 1);
-    MemoryRegion *flash0mem;
     const hwaddr *map = daughterboard->motherboard_map;
     int i;
 
@@ -657,6 +655,9 @@ static void vexpress_common_init(MachineState *machine)
     }
 
     if (map[VE_NORFLASHALIAS] != -1) {
+        MemoryRegion *flashalias = g_new(MemoryRegion, 1);
+        MemoryRegion *flash0mem;
+
         /* Map flash 0 as an alias into low memory */
         flash0mem = sysbus_mmio_get_region(SYS_BUS_DEVICE(pflash0), 0);
         memory_region_init_alias(flashalias, NULL, "vexpress.flashalias",
Signature · 1 line
2.13.3
#59 lore raw

Re: [Qemu-arm] [PATCH for 2.10 22/35] arm/vexpress: fix potential memory leak

Quoted 34 lines · Philippe Mathieu-Daudé

Reorder to only allocate if required.

hw/arm/vexpress.c:667:13: warning: Potential leak of memory pointed to by 'flashalias'

Reported-by Clang Static Analyzer
---
 hw/arm/vexpress.c | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)
hw/arm/vexpress.c +3 -3
index 528c65ddb6..76c4d84482 100644
@@ -553,8 +553,6 @@ static void vexpress_common_init(MachineState *machine)
     MemoryRegion *sysmem = get_system_memory();
     MemoryRegion *vram = g_new(MemoryRegion, 1);
     MemoryRegion *sram = g_new(MemoryRegion, 1);
-    MemoryRegion *flashalias = g_new(MemoryRegion, 1);
-    MemoryRegion *flash0mem;
     const hwaddr *map = daughterboard->motherboard_map;
     int i;
 
@@ -657,6 +655,9 @@ static void vexpress_common_init(MachineState *machine)
     }
 
     if (map[VE_NORFLASHALIAS] != -1) {
+        MemoryRegion *flashalias = g_new(MemoryRegion, 1);
+        MemoryRegion *flash0mem;
+
         /* Map flash 0 as an alias into low memory */
         flash0mem = sysbus_mmio_get_region(SYS_BUS_DEVICE(pflash0), 0);
         memory_region_init_alias(flashalias, NULL, "vexpress.flashalias",
--
Quoted 1 line

2.13.3

flashalias is no different from any of the other MemoryRegion* variables we allocate here with g_new() -- they all leak, and it doesn't matter because the function runs once and we want the MemoryRegions to survive until the end of the simulation.

These days we could in theory clean this up by
switching from doing g_new() to having a set of
MemoryRegion fields in the VexpressMachineState
struct (compare hw/arm/mps2.c).

thanks
-- PMM

#60 lore raw

Re: [Qemu-arm] [PATCH for 2.10 22/35] arm/vexpress: fix potential memory leak

Quoted 43 lines · Peter Maydell
Quoted 34 lines · Philippe Mathieu-Daudé

Reorder to only allocate if required.

hw/arm/vexpress.c:667:13: warning: Potential leak of memory pointed to by 'flashalias'

Reported-by Clang Static Analyzer
---
  hw/arm/vexpress.c | 5 +++--
  1 file changed, 3 insertions(+), 2 deletions(-)
hw/arm/vexpress.c +3 -3
index 528c65ddb6..76c4d84482 100644
@@ -553,8 +553,6 @@ static void vexpress_common_init(MachineState *machine)
      MemoryRegion *sysmem = get_system_memory();
      MemoryRegion *vram = g_new(MemoryRegion, 1);
      MemoryRegion *sram = g_new(MemoryRegion, 1);
-    MemoryRegion *flashalias = g_new(MemoryRegion, 1);
-    MemoryRegion *flash0mem;
      const hwaddr *map = daughterboard->motherboard_map;
      int i;
 
@@ -657,6 +655,9 @@ static void vexpress_common_init(MachineState *machine)
      }
 
      if (map[VE_NORFLASHALIAS] != -1) {
+        MemoryRegion *flashalias = g_new(MemoryRegion, 1);
+        MemoryRegion *flash0mem;
+
          /* Map flash 0 as an alias into low memory */
          flash0mem = sysbus_mmio_get_region(SYS_BUS_DEVICE(pflash0), 0);
          memory_region_init_alias(flashalias, NULL, "vexpress.flashalias",
--
Quoted 1 line

2.13.3

flashalias is no different from any of the other MemoryRegion* variables we allocate here with g_new() -- they all leak, and it doesn't matter because the function runs once and we want the MemoryRegions to survive until the end of the simulation.

Quoted 4 lines

These days we could in theory clean this up by
switching from doing g_new() to having a set of
MemoryRegion fields in the VexpressMachineState
struct (compare hw/arm/mps2.c).

I agree with the analyzer here, why allocate something you'll never use?
However this is not the case here since in your commit 6ec1588e097 both legacy/a9 and aseries/a15 use NORFLASHALIAS to remap their flash at 0.

Anyway patch dropped.

Thanks,

Phil.

#61 lore raw

PATCH 23/35 m68k/translate: fix incorrect copy/paste

In reply to Philippe Mathieu-Daudé

db3d7945ae extended gen_cc_cond() for cond [6, 7, 9, 10] but misswrote [4, 5]

target/m68k/translate.c:1323:70: warning: identical expressions on both sides of logical operator

        if (op == CC_OP_ADDB || op == CC_OP_ADDW || op == CC_OP_ADDL ||
            op == CC_OP_ADDB || op == CC_OP_ADDW || op == CC_OP_ADDL) {
            ~~~~~~~~~~~~~~~~    ~~~~~~~~~~~~~~~~    ~~~~~~~~~~~~~~~~ ^
Reported-by Clang Static Analyzer
---
 target/m68k/translate.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
target/m68k/translate.c +2 -1
index ada2a91b64..1a2f421aab 100644
@@ -1321,7 +1321,8 @@ static void gen_cc_cond(DisasCompare *c, DisasContext *s, int cond)
     case 5: /* CS (C) */
         /* Some cases fold C into X.  */
         if (op == CC_OP_ADDB || op == CC_OP_ADDW || op == CC_OP_ADDL ||
-            op == CC_OP_ADDB || op == CC_OP_ADDW || op == CC_OP_ADDL) {
+            op == CC_OP_SUBB || op == CC_OP_SUBW || op == CC_OP_SUBL ||
+            op == CC_OP_LOGIC) {
             tcond = TCG_COND_NE;
             c->v1 = QREG_CC_X;
             goto done;
Signature · 1 line
2.13.3
#62 lore raw

Re: [Qemu-devel] [PATCH for 2.10 23/35] m68k/translate: fix incorrect copy/paste

Quoted 18 lines · Philippe Mathieu-Daudé

db3d7945ae extended gen_cc_cond() for cond [6, 7, 9, 10] but misswrote [4, 5]

target/m68k/translate.c:1323:70: warning: identical expressions on both sides of logical operator

        if (op == CC_OP_ADDB || op == CC_OP_ADDW || op == CC_OP_ADDL ||
            op == CC_OP_ADDB || op == CC_OP_ADDW || op == CC_OP_ADDL) {
            ~~~~~~~~~~~~~~~~    ~~~~~~~~~~~~~~~~    ~~~~~~~~~~~~~~~~ ^
Reported-by Clang Static Analyzer
---
 target/m68k/translate.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
target/m68k/translate.c +0 -0
index ada2a91b64..1a2f421aab 100644

target/m68k/translate.c static void gen_cc_cond(DisasCompare *c, DisasContext *s, int cond) · line 1325

Quoted 6 lines
target/m68k/translate.c +2 -1
@@ -1321,7 +1321,8 @@ static void gen_cc_cond(DisasCompare *c, DisasContext *s, int cond)
     case 5: /* CS (C) */
         /* Some cases fold C into X.  */
         if (op == CC_OP_ADDB || op == CC_OP_ADDW || op == CC_OP_ADDL ||
-            op == CC_OP_ADDB || op == CC_OP_ADDW || op == CC_OP_ADDL) {
+            op == CC_OP_SUBB || op == CC_OP_SUBW || op == CC_OP_SUBL ||
+            op == CC_OP_LOGIC) {

According to commit (db3d7945) introducing the incorrect copy/paste, I don't think we need the CC_OP_LOGIC here. Logic operations never generates X flags (whereas they can generate Z and and N).

Thanks,
Laurent

#63 lore raw

Re: [Qemu-devel] [PATCH for 2.10 23/35] m68k/translate: fix incorrect copy/paste

Quoted 16 lines · Laurent Vivier

target/m68k/translate.c static void gen_cc_cond(DisasCompare *c, DisasContext *s, int cond) · line 1325

Quoted 11 lines · Philippe Mathieu-Daudé
target/m68k/translate.c +2 -1
index ada2a91b64..1a2f421aab 100644
@@ -1321,7 +1321,8 @@ static void gen_cc_cond(DisasCompare *c, DisasContext *s, int cond)
      case 5: /* CS (C) */
          /* Some cases fold C into X.  */
          if (op == CC_OP_ADDB || op == CC_OP_ADDW || op == CC_OP_ADDL ||
-            op == CC_OP_ADDB || op == CC_OP_ADDW || op == CC_OP_ADDL) {
+            op == CC_OP_SUBB || op == CC_OP_SUBW || op == CC_OP_SUBL ||
+            op == CC_OP_LOGIC) {

According to commit (db3d7945) introducing the incorrect copy/paste, I don't think we need the CC_OP_LOGIC here. Logic operations never generates X flags (whereas they can generate Z and and N).

Indeed, look at the comment below. LOGIC is supposed to fall through to the code that produces TCG_COND_NEVER.

r~

#64 lore raw

Re: [Qemu-devel] [PATCH for 2.10 23/35] m68k/translate: fix incorrect copy/paste

Quoted 20 lines · Richard Henderson
Quoted 10 lines · Laurent Vivier
Quoted 9 lines · Philippe Mathieu-Daudé
target/m68k/translate.c +0 -0
index ada2a91b64..1a2f421aab 100644
@@ -1321,7 +1321,8 @@ static void gen_cc_cond(DisasCompare *c, 
 DisasContext *s, int cond)
      case 5: /* CS (C) */
          /* Some cases fold C into X.  */
          if (op == CC_OP_ADDB || op == CC_OP_ADDW || op == 
Quoted 8 lines

target/m68k/translate.c static void gen_cc_cond(DisasCompare *c, DisasContext *s, int cond) · line 1325

Quoted 4 lines

CC_OP_ADDL ||

target/m68k/translate.c +2 -1
-            op == CC_OP_ADDB || op == CC_OP_ADDW || op == CC_OP_ADDL) {
+            op == CC_OP_SUBB || op == CC_OP_SUBW || op == CC_OP_SUBL ||
+            op == CC_OP_LOGIC) {

According to commit (db3d7945) introducing the incorrect copy/paste, I don't think we need the CC_OP_LOGIC here. Logic operations never generates X flags (whereas they can generate Z and and N).

Quoted 1 line

Indeed, look at the comment below. LOGIC is supposed to fall through to the code that produces TCG_COND_NEVER.

Indeed I also miss-copypasted :)

So the correct fix would be:?

         if (op == CC_OP_ADDB || op == CC_OP_ADDW || op == CC_OP_ADDL ||
-            op == CC_OP_ADDB || op == CC_OP_ADDW || op == CC_OP_ADDL) {
+            op == CC_OP_SUBB || op == CC_OP_SUBW || op == CC_OP_SUBL) {
Reviewed-by
#65 lore raw

Re: [Qemu-devel] [PATCH for 2.10 23/35] m68k/translate: fix incorrect copy/paste

Quoted 31 lines · Philippe Mathieu-Daudé
Quoted 22 lines · Richard Henderson
Quoted 12 lines · Laurent Vivier
Quoted 11 lines · Philippe Mathieu-Daudé
target/m68k/translate.c +0 -0
index ada2a91b64..1a2f421aab 100644
@@ -1321,7 +1321,8 @@ static void gen_cc_cond(DisasCompare *c,
 DisasContext *s, int cond)
      case 5: /* CS (C) */
          /* Some cases fold C into X.  */
          if (op == CC_OP_ADDB || op == CC_OP_ADDW || op ==

CC_OP_ADDL ||
- op == CC_OP_ADDB || op == CC_OP_ADDW || op ==

Quoted 8 lines
Quoted 4 lines

CC_OP_ADDL) {
+ op == CC_OP_SUBB || op == CC_OP_SUBW || op ==
CC_OP_SUBL ||
+ op == CC_OP_LOGIC) {

According to commit (db3d7945) introducing the incorrect copy/paste, I don't think we need the CC_OP_LOGIC here. Logic operations never generates X flags (whereas they can generate Z and and N).

Quoted 2 lines

Indeed, look at the comment below. LOGIC is supposed to fall through to the code that produces TCG_COND_NEVER.

Indeed I also miss-copypasted :)

So the correct fix would be:?

         if (op == CC_OP_ADDB || op == CC_OP_ADDW || op == CC_OP_ADDL ||

Review

Quoted 2 lines
(quoted diff) +1 -1
-            op == CC_OP_ADDB || op == CC_OP_ADDW || op == CC_OP_ADDL) {
+            op == CC_OP_SUBB || op == CC_OP_SUBW || op == CC_OP_SUBL) {

yes

Laurent

#66 lore raw

PATCH 24/35 linux-user/sh4: fix incorrect memory write

In reply to Philippe Mathieu-Daudé

not hit since 2009! :)

linux-user/elfload.c:1102:20: warning: Out of bound memory access (access exceeds upper limit of memory block)

        (*regs[i]) = tswap32(env->gregs[i]);
        ~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
Reported-by Clang Static Analyzer
---
 linux-user/elfload.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
linux-user/elfload.c +1 -1
index 2a902f7806..79062882ba 100644
@@ -1099,7 +1099,7 @@ static inline void elf_core_copy_regs(target_elf_gregset_t *regs,
     int i;
 
     for (i = 0; i < 16; i++) {
-        (*regs[i]) = tswapreg(env->gregs[i]);
+        (*regs)[i] = tswapreg(env->gregs[i]);
     }
 
     (*regs)[TARGET_REG_PC] = tswapreg(env->pc);
Signature · 1 line
2.13.3
Reviewed-by on v1 24/35
#67 lore raw

Re: [Qemu-devel] [PATCH for 2.10 24/35] linux-user/sh4: fix incorrect memory write

Quoted 18 lines · Philippe Mathieu-Daudé

not hit since 2009! :)

linux-user/elfload.c:1102:20: warning: Out of bound memory access (access exceeds upper limit of memory block)

        (*regs[i]) = tswap32(env->gregs[i]);
        ~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
Reported-by Clang Static Analyzer
---
 linux-user/elfload.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
linux-user/elfload.c +0 -0
index 2a902f7806..79062882ba 100644

linux-user/elfload.c static inline void elf_core_copy_regs(target_elf_gregset_t *regs, · line 1105

Quoted 8 lines
linux-user/elfload.c +1 -1
@@ -1099,7 +1099,7 @@ static inline void elf_core_copy_regs(target_elf_gregset_t *regs,
     int i;
 
     for (i = 0; i < 16; i++) {
-        (*regs[i]) = tswapreg(env->gregs[i]);
+        (*regs)[i] = tswapreg(env->gregs[i]);
     }
 
     (*regs)[TARGET_REG_PC] = tswapreg(env->pc);
#68 lore raw

PATCH 25/35 linux-user: extract is_error() out of syscall.c

In reply to Philippe Mathieu-Daudé
---
 linux-user/qemu.h    | 5 +++++
 linux-user/syscall.c | 5 -----
 2 files changed, 5 insertions(+), 5 deletions(-)
linux-user/qemu.h +5 -0
index 4edd7d0c08..97b7cfdb63 100644
@@ -614,6 +614,11 @@ static inline void *lock_user_string(abi_ulong guest_addr)
 
 #include <pthread.h>
 
+static inline int is_error(abi_long ret)
+{
+    return (abi_ulong)ret >= (abi_ulong)(-4096);
+}
+
 /* Include target-specific struct and function definitions;
  * they may need access to the target-independent structures
  * above, so include them last.
linux-user/syscall.c +0 -5
index 003943b736..fcd20fa276 100644
@@ -837,11 +837,6 @@ static inline abi_long get_errno(abi_long ret)
         return ret;
 }
 
-static inline int is_error(abi_long ret)
-{
-    return (abi_ulong)ret >= (abi_ulong)(-4096);
-}
-
 const char *target_strerror(int err)
 {
     if (err == TARGET_ERESTARTSYS) {
Signature · 1 line
2.13.3
#69 lore raw

Re: [Qemu-devel] [PATCH for 2.10 25/35] linux-user: extract is_error() out of syscall.c

Quoted 29 lines · Philippe Mathieu-Daudé
---
 linux-user/qemu.h    | 5 +++++
 linux-user/syscall.c | 5 -----
 2 files changed, 5 insertions(+), 5 deletions(-)
linux-user/qemu.h +5 -0
index 4edd7d0c08..97b7cfdb63 100644
@@ -614,6 +614,11 @@ static inline void *lock_user_string(abi_ulong guest_addr)
 
 #include <pthread.h>
 
+static inline int is_error(abi_long ret)
+{
+    return (abi_ulong)ret >= (abi_ulong)(-4096);
+}
+
 /* Include target-specific struct and function definitions;
  * they may need access to the target-independent structures
  * above, so include them last.
linux-user/syscall.c +0 -0
index 003943b736..fcd20fa276 100644
@@ -837,11 +837,6 @@ static inline abi_long get_errno(abi_long ret)
         return ret;

linux-user/syscall.c static inline abi_long get_errno(abi_long ret) · line 842

Quoted 10 lines
linux-user/syscall.c +0 -5
 }
 
-static inline int is_error(abi_long ret)
-{
-    return (abi_ulong)ret >= (abi_ulong)(-4096);
-}
-
 const char *target_strerror(int err)
 {
     if (err == TARGET_ERESTARTSYS) {

I think you should fold this one into the following one.

Laurent

#70 lore raw

PATCH 26/35 linux-user: use is_error() to avoid warnings and make the code clearer

In reply to Philippe Mathieu-Daudé

linux-user/flatload.c:740:9: warning: Loss of sign in implicit conversion

    if (res > (unsigned long)-4096)
        ^~~
Reported-by Clang Static Analyzer
---
 linux-user/flatload.c | 15 +++++++++------
 1 file changed, 9 insertions(+), 6 deletions(-)
linux-user/flatload.c +9 -6
index a35a560904..10c529910f 100644
@@ -224,8 +224,9 @@ static int decompress_exec(
 		ret = bprm->file->f_op->read(bprm->file, buf, LBUFSIZE, &fpos);
 		if (ret <= 0)
 			break;
-		if (ret >= (unsigned long) -4096)
+                if (is_error(ret)) {
 			break;
+                }
 		len -= ret;
 
 		strm.next_in = buf;
@@ -283,8 +284,7 @@ calc_reloc(abi_ulong r, struct lib_info *p, int curid, int internalp)
                     "in same module (%d != %d)\n",
                     (unsigned) r, curid, id);
             goto failed;
-        } else if ( ! p[id].loaded &&
-                    load_flat_shared_library(id, p) > (unsigned long) -4096) {
+        } else if (!p[id].loaded && is_error(load_flat_shared_library(id, p))) {
             fprintf(stderr, "BINFMT_FLAT: failed to load library %d\n", id);
             goto failed;
         }
@@ -523,9 +523,10 @@ static int load_flat_file(struct linux_binprm * bprm,
                 fpos = 0;
                 result = bprm->file->f_op->read(bprm->file,
                                 (char *) textpos, text_len, &fpos);
-                if (result < (unsigned long) -4096)
+                if (!is_error(result)) {
                         result = decompress_exec(bprm, text_len, (char *) datapos,
                                          data_len + (relocs * sizeof(unsigned long)), 0);
+                }
         }
         else
 #endif
@@ -693,8 +694,9 @@ static int load_flat_shared_library(int id, struct lib_info *libs)
 
 	res = prepare_binprm(&bprm);
 
-	if (res <= (unsigned long)-4096)
+        if (!is_error(res)) {
 		res = load_flat_file(&bprm, libs, id, NULL);
+        }
 	if (bprm.file) {
 		allow_write_access(bprm.file);
 		fput(bprm.file);
@@ -737,8 +739,9 @@ int load_flt_binary(struct linux_binprm *bprm, struct image_info *info)
 
 
     res = load_flat_file(bprm, libinfo, 0, &stack_len);
-    if (res > (unsigned long)-4096)
+    if (is_error(res)) {
             return res;
+    }
 
     /* Update data segment pointers for all libraries */
     for (i=0; i<MAX_SHARED_LIBS; i++) {
Signature · 1 line
2.13.3
Reviewed-by on v1 26/35
#71 lore raw

Re: [Qemu-devel] [PATCH for 2.10 26/35] linux-user: use is_error() to avoid warnings and make the code clearer

Quoted 6 lines · Philippe Mathieu-Daudé

linux-user/flatload.c:740:9: warning: Loss of sign in implicit conversion

    if (res > (unsigned long)-4096)
        ^~~
Reported-by Clang Static Analyzer
Quoted 64 lines
---
 linux-user/flatload.c | 15 +++++++++------
 1 file changed, 9 insertions(+), 6 deletions(-)
linux-user/flatload.c +9 -6
index a35a560904..10c529910f 100644
@@ -224,8 +224,9 @@ static int decompress_exec(
 		ret = bprm->file->f_op->read(bprm->file, buf, LBUFSIZE, &fpos);
 		if (ret <= 0)
 			break;
-		if (ret >= (unsigned long) -4096)
+                if (is_error(ret)) {
 			break;
+                }
 		len -= ret;
 
 		strm.next_in = buf;
@@ -283,8 +284,7 @@ calc_reloc(abi_ulong r, struct lib_info *p, int curid, int internalp)
                     "in same module (%d != %d)\n",
                     (unsigned) r, curid, id);
             goto failed;
-        } else if ( ! p[id].loaded &&
-                    load_flat_shared_library(id, p) > (unsigned long) -4096) {
+        } else if (!p[id].loaded && is_error(load_flat_shared_library(id, p))) {
             fprintf(stderr, "BINFMT_FLAT: failed to load library %d\n", id);
             goto failed;
         }
@@ -523,9 +523,10 @@ static int load_flat_file(struct linux_binprm * bprm,
                 fpos = 0;
                 result = bprm->file->f_op->read(bprm->file,
                                 (char *) textpos, text_len, &fpos);
-                if (result < (unsigned long) -4096)
+                if (!is_error(result)) {
                         result = decompress_exec(bprm, text_len, (char *) datapos,
                                          data_len + (relocs * sizeof(unsigned long)), 0);
+                }
         }
         else
 #endif
@@ -693,8 +694,9 @@ static int load_flat_shared_library(int id, struct lib_info *libs)
 
 	res = prepare_binprm(&bprm);
 
-	if (res <= (unsigned long)-4096)
+        if (!is_error(res)) {
 		res = load_flat_file(&bprm, libs, id, NULL);
+        }
 	if (bprm.file) {
 		allow_write_access(bprm.file);
 		fput(bprm.file);
@@ -737,8 +739,9 @@ int load_flt_binary(struct linux_binprm *bprm, struct image_info *info)
 
 
     res = load_flat_file(bprm, libinfo, 0, &stack_len);
-    if (res > (unsigned long)-4096)
+    if (is_error(res)) {
             return res;
+    }
 
     /* Update data segment pointers for all libraries */
     for (i=0; i<MAX_SHARED_LIBS; i++) {
#72 lore raw

Re: [Qemu-devel] [PATCH for 2.10 26/35] linux-user: use is_error() to avoid warnings and make the code clearer

Hi Laurent,

Quoted 71 lines · Laurent Vivier
Quoted 6 lines · Philippe Mathieu-Daudé

linux-user/flatload.c:740:9: warning: Loss of sign in implicit conversion

    if (res > (unsigned long)-4096)
        ^~~
Reported-by Clang Static Analyzer
Quoted 61 lines
---
 linux-user/flatload.c | 15 +++++++++------
 1 file changed, 9 insertions(+), 6 deletions(-)
linux-user/flatload.c +9 -6
index a35a560904..10c529910f 100644
@@ -224,8 +224,9 @@ static int decompress_exec(
 		ret = bprm->file->f_op->read(bprm->file, buf, LBUFSIZE, &fpos);
 		if (ret <= 0)
 			break;
-		if (ret >= (unsigned long) -4096)
+                if (is_error(ret)) {
 			break;
+                }
 		len -= ret;
 
 		strm.next_in = buf;
@@ -283,8 +284,7 @@ calc_reloc(abi_ulong r, struct lib_info *p, int curid, int internalp)
                     "in same module (%d != %d)\n",
                     (unsigned) r, curid, id);
             goto failed;
-        } else if ( ! p[id].loaded &&
-                    load_flat_shared_library(id, p) > (unsigned long) -4096) {
+        } else if (!p[id].loaded && is_error(load_flat_shared_library(id, p))) {
             fprintf(stderr, "BINFMT_FLAT: failed to load library %d\n", id);
             goto failed;
         }
@@ -523,9 +523,10 @@ static int load_flat_file(struct linux_binprm * bprm,
                 fpos = 0;
                 result = bprm->file->f_op->read(bprm->file,
                                 (char *) textpos, text_len, &fpos);
-                if (result < (unsigned long) -4096)
+                if (!is_error(result)) {
                         result = decompress_exec(bprm, text_len, (char *) datapos,
                                          data_len + (relocs * sizeof(unsigned long)), 0);
+                }
         }
         else
 #endif
@@ -693,8 +694,9 @@ static int load_flat_shared_library(int id, struct lib_info *libs)
 
 	res = prepare_binprm(&bprm);
 
-	if (res <= (unsigned long)-4096)
+        if (!is_error(res)) {
 		res = load_flat_file(&bprm, libs, id, NULL);
+        }
 	if (bprm.file) {
 		allow_write_access(bprm.file);
 		fput(bprm.file);
@@ -737,8 +739,9 @@ int load_flt_binary(struct linux_binprm *bprm, struct image_info *info)
 
 
     res = load_flat_file(bprm, libinfo, 0, &stack_len);
-    if (res > (unsigned long)-4096)
+    if (is_error(res)) {
             return res;
+    }
 
Quoted 3 lines

linux-user/flatload.c int load_flt_binary(struct linux_binprm *bprm, struct image_info *info) · line 747

Quoted 3 lines
linux-user/flatload.c +0 -0
     /* Update data segment pointers for all libraries */
     for (i=0; i<MAX_SHARED_LIBS; i++) {

Can you take this via your linux-user tree?

Thanks,

Phil.

#73 lore raw

Re: [Qemu-devel] [PATCH for 2.10 26/35] linux-user: use is_error() to avoid warnings and make the code clearer

Le 29/05/2018 à 16:25, Philippe Mathieu-Daudé a écrit :

Quoted 1 line

Hi Laurent,

Hi Philippe,

Quoted 72 lines · Laurent Vivier
Quoted 71 lines · Philippe Mathieu-Daudé
Quoted 6 lines

linux-user/flatload.c:740:9: warning: Loss of sign in implicit conversion

    if (res > (unsigned long)-4096)
        ^~~
Reported-by Clang Static Analyzer
Quoted 61 lines
---
 linux-user/flatload.c | 15 +++++++++------
 1 file changed, 9 insertions(+), 6 deletions(-)
linux-user/flatload.c +9 -6
index a35a560904..10c529910f 100644
@@ -224,8 +224,9 @@ static int decompress_exec(
 		ret = bprm->file->f_op->read(bprm->file, buf, LBUFSIZE, &fpos);
 		if (ret <= 0)
 			break;
-		if (ret >= (unsigned long) -4096)
+                if (is_error(ret)) {
 			break;
+                }
 		len -= ret;
 
 		strm.next_in = buf;
@@ -283,8 +284,7 @@ calc_reloc(abi_ulong r, struct lib_info *p, int curid, int internalp)
                     "in same module (%d != %d)\n",
                     (unsigned) r, curid, id);
             goto failed;
-        } else if ( ! p[id].loaded &&
-                    load_flat_shared_library(id, p) > (unsigned long) -4096) {
+        } else if (!p[id].loaded && is_error(load_flat_shared_library(id, p))) {
             fprintf(stderr, "BINFMT_FLAT: failed to load library %d\n", id);
             goto failed;
         }
@@ -523,9 +523,10 @@ static int load_flat_file(struct linux_binprm * bprm,
                 fpos = 0;
                 result = bprm->file->f_op->read(bprm->file,
                                 (char *) textpos, text_len, &fpos);
-                if (result < (unsigned long) -4096)
+                if (!is_error(result)) {
                         result = decompress_exec(bprm, text_len, (char *) datapos,
                                          data_len + (relocs * sizeof(unsigned long)), 0);
+                }
         }
         else
 #endif
@@ -693,8 +694,9 @@ static int load_flat_shared_library(int id, struct lib_info *libs)
 
 	res = prepare_binprm(&bprm);
 
-	if (res <= (unsigned long)-4096)
+        if (!is_error(res)) {
 		res = load_flat_file(&bprm, libs, id, NULL);
+        }
 	if (bprm.file) {
 		allow_write_access(bprm.file);
 		fput(bprm.file);
@@ -737,8 +739,9 @@ int load_flt_binary(struct linux_binprm *bprm, struct image_info *info)
 
 
     res = load_flat_file(bprm, libinfo, 0, &stack_len);
-    if (res > (unsigned long)-4096)
+    if (is_error(res)) {
             return res;
+    }
 
Quoted 6 lines
Quoted 3 lines

linux-user/flatload.c int load_flt_binary(struct linux_binprm *bprm, struct image_info *info) · line 747

Quoted 3 lines
linux-user/flatload.c +0 -0
     /* Update data segment pointers for all libraries */
     for (i=0; i<MAX_SHARED_LIBS; i++) {

Can you take this via your linux-user tree?

Applied, thanks.

Laurent

#74 lore raw

Re: [Qemu-devel] [PATCH for 2.10 26/35] linux-user: use is_error() to avoid warnings and make the code clearer

Le 29/05/2018 à 17:19, Laurent Vivier a écrit :

Quoted 77 lines

Le 29/05/2018 à 16:25, Philippe Mathieu-Daudé a écrit :

Quoted 1 line

Hi Laurent,

Hi Philippe,

Quoted 72 lines · Laurent Vivier
Quoted 71 lines · Philippe Mathieu-Daudé
Quoted 6 lines

linux-user/flatload.c:740:9: warning: Loss of sign in implicit conversion

    if (res > (unsigned long)-4096)
        ^~~
Reported-by Clang Static Analyzer
Quoted 61 lines
---
 linux-user/flatload.c | 15 +++++++++------
 1 file changed, 9 insertions(+), 6 deletions(-)
linux-user/flatload.c +9 -6
index a35a560904..10c529910f 100644
@@ -224,8 +224,9 @@ static int decompress_exec(
 		ret = bprm->file->f_op->read(bprm->file, buf, LBUFSIZE, &fpos);
 		if (ret <= 0)
 			break;
-		if (ret >= (unsigned long) -4096)
+                if (is_error(ret)) {
 			break;
+                }
 		len -= ret;
 
 		strm.next_in = buf;
@@ -283,8 +284,7 @@ calc_reloc(abi_ulong r, struct lib_info *p, int curid, int internalp)
                     "in same module (%d != %d)\n",
                     (unsigned) r, curid, id);
             goto failed;
-        } else if ( ! p[id].loaded &&
-                    load_flat_shared_library(id, p) > (unsigned long) -4096) {
+        } else if (!p[id].loaded && is_error(load_flat_shared_library(id, p))) {
             fprintf(stderr, "BINFMT_FLAT: failed to load library %d\n", id);
             goto failed;
         }
@@ -523,9 +523,10 @@ static int load_flat_file(struct linux_binprm * bprm,
                 fpos = 0;
                 result = bprm->file->f_op->read(bprm->file,
                                 (char *) textpos, text_len, &fpos);
-                if (result < (unsigned long) -4096)
+                if (!is_error(result)) {
                         result = decompress_exec(bprm, text_len, (char *) datapos,
                                          data_len + (relocs * sizeof(unsigned long)), 0);
+                }
         }
         else
 #endif
@@ -693,8 +694,9 @@ static int load_flat_shared_library(int id, struct lib_info *libs)
 
 	res = prepare_binprm(&bprm);
 
-	if (res <= (unsigned long)-4096)
+        if (!is_error(res)) {
 		res = load_flat_file(&bprm, libs, id, NULL);
+        }
 	if (bprm.file) {
 		allow_write_access(bprm.file);
 		fput(bprm.file);
@@ -737,8 +739,9 @@ int load_flt_binary(struct linux_binprm *bprm, struct image_info *info)
 
 
     res = load_flat_file(bprm, libinfo, 0, &stack_len);
-    if (res > (unsigned long)-4096)
+    if (is_error(res)) {
             return res;
+    }
 
Quoted 8 lines
Quoted 6 lines
Quoted 3 lines

linux-user/flatload.c int load_flt_binary(struct linux_binprm *bprm, struct image_info *info) · line 747

Quoted 3 lines
linux-user/flatload.c +0 -0
     /* Update data segment pointers for all libraries */
     for (i=0; i<MAX_SHARED_LIBS; i++) {

Can you take this via your linux-user tree?

Applied, thanks.

Unapplied, it needs a rebase:

qemu/linux-user/flatload.c: In function 'load_flt_binary':
qemu/linux-user/flatload.c:742:9: error: implicit declaration of function 'is_error'; did you mean 'g_error'? [-Werror=implicit-function-declaration]

     if (is_error(res)) {
         ^~~~~~~~
         g_error

qemu/linux-user/flatload.c:742:9: error: nested extern declaration of 'is_error' [-Werror=nested-externs]

Thanks,
Laurent

#75 lore raw

Re: [Qemu-devel] [PATCH for 2.10 26/35] linux-user: use is_error() to avoid warnings and make the code clearer

Quoted 85 lines · Laurent Vivier

Le 29/05/2018 à 17:19, Laurent Vivier a écrit :

Quoted 84 lines

Le 29/05/2018 à 16:25, Philippe Mathieu-Daudé a écrit :

Quoted 1 line

Hi Laurent,

Hi Philippe,

Quoted 78 lines · Laurent Vivier
Quoted 71 lines · Philippe Mathieu-Daudé
Quoted 6 lines

linux-user/flatload.c:740:9: warning: Loss of sign in implicit conversion

    if (res > (unsigned long)-4096)
        ^~~
Reported-by Clang Static Analyzer
Quoted 61 lines
---
 linux-user/flatload.c | 15 +++++++++------
 1 file changed, 9 insertions(+), 6 deletions(-)
linux-user/flatload.c +9 -6
index a35a560904..10c529910f 100644
@@ -224,8 +224,9 @@ static int decompress_exec(
 		ret = bprm->file->f_op->read(bprm->file, buf, LBUFSIZE, &fpos);
 		if (ret <= 0)
 			break;
-		if (ret >= (unsigned long) -4096)
+                if (is_error(ret)) {
 			break;
+                }
 		len -= ret;
 
 		strm.next_in = buf;
@@ -283,8 +284,7 @@ calc_reloc(abi_ulong r, struct lib_info *p, int curid, int internalp)
                     "in same module (%d != %d)\n",
                     (unsigned) r, curid, id);
             goto failed;
-        } else if ( ! p[id].loaded &&
-                    load_flat_shared_library(id, p) > (unsigned long) -4096) {
+        } else if (!p[id].loaded && is_error(load_flat_shared_library(id, p))) {
             fprintf(stderr, "BINFMT_FLAT: failed to load library %d\n", id);
             goto failed;
         }
@@ -523,9 +523,10 @@ static int load_flat_file(struct linux_binprm * bprm,
                 fpos = 0;
                 result = bprm->file->f_op->read(bprm->file,
                                 (char *) textpos, text_len, &fpos);
-                if (result < (unsigned long) -4096)
+                if (!is_error(result)) {
                         result = decompress_exec(bprm, text_len, (char *) datapos,
                                          data_len + (relocs * sizeof(unsigned long)), 0);
+                }
         }
         else
 #endif
@@ -693,8 +694,9 @@ static int load_flat_shared_library(int id, struct lib_info *libs)
 
 	res = prepare_binprm(&bprm);
 
-	if (res <= (unsigned long)-4096)
+        if (!is_error(res)) {
 		res = load_flat_file(&bprm, libs, id, NULL);
+        }
 	if (bprm.file) {
 		allow_write_access(bprm.file);
 		fput(bprm.file);
@@ -737,8 +739,9 @@ int load_flt_binary(struct linux_binprm *bprm, struct image_info *info)
 
 
     res = load_flat_file(bprm, libinfo, 0, &stack_len);
-    if (res > (unsigned long)-4096)
+    if (is_error(res)) {
             return res;
+    }
 
Quoted 3 lines

linux-user/flatload.c int load_flt_binary(struct linux_binprm *bprm, struct image_info *info) · line 747

Quoted 3 lines
linux-user/flatload.c +0 -0
     /* Update data segment pointers for all libraries */
     for (i=0; i<MAX_SHARED_LIBS; i++) {

Can you take this via your linux-user tree?

Quoted 3 lines
Quoted 1 line

Applied, thanks.

Unapplied, it needs a rebase:

No rebase required, it just need the previous patch applied too :) But I didn't think of explicit it :/
Can you take both of them directly or do you prefer I RESEND?

Quoted 16 lines

qemu/linux-user/flatload.c: In function 'load_flt_binary':
qemu/linux-user/flatload.c:742:9: error: implicit declaration of function 'is_error'; did you mean 'g_error'? [-Werror=implicit-function-declaration]

     if (is_error(res)) {
         ^~~~~~~~
         g_error

qemu/linux-user/flatload.c:742:9: error: nested extern declaration of 'is_error' [-Werror=nested-externs]

Thanks,
Laurent

#76 lore raw

Re: [Qemu-devel] [PATCH for 2.10 26/35] linux-user: use is_error() to avoid warnings and make the code clearer

Le 04/06/2018 à 16:20, Philippe Mathieu-Daudé a écrit :

Quoted 86 lines · Laurent Vivier
Quoted 85 lines

Le 29/05/2018 à 17:19, Laurent Vivier a écrit :

Quoted 84 lines

Le 29/05/2018 à 16:25, Philippe Mathieu-Daudé a écrit :

Quoted 1 line

Hi Laurent,

Hi Philippe,

Quoted 78 lines · Laurent Vivier
Quoted 71 lines · Philippe Mathieu-Daudé
Quoted 6 lines

linux-user/flatload.c:740:9: warning: Loss of sign in implicit conversion

    if (res > (unsigned long)-4096)
        ^~~
Reported-by Clang Static Analyzer
Quoted 61 lines
---
 linux-user/flatload.c | 15 +++++++++------
 1 file changed, 9 insertions(+), 6 deletions(-)
linux-user/flatload.c +9 -6
index a35a560904..10c529910f 100644
@@ -224,8 +224,9 @@ static int decompress_exec(
 		ret = bprm->file->f_op->read(bprm->file, buf, LBUFSIZE, &fpos);
 		if (ret <= 0)
 			break;
-		if (ret >= (unsigned long) -4096)
+                if (is_error(ret)) {
 			break;
+                }
 		len -= ret;
 
 		strm.next_in = buf;
@@ -283,8 +284,7 @@ calc_reloc(abi_ulong r, struct lib_info *p, int curid, int internalp)
                     "in same module (%d != %d)\n",
                     (unsigned) r, curid, id);
             goto failed;
-        } else if ( ! p[id].loaded &&
-                    load_flat_shared_library(id, p) > (unsigned long) -4096) {
+        } else if (!p[id].loaded && is_error(load_flat_shared_library(id, p))) {
             fprintf(stderr, "BINFMT_FLAT: failed to load library %d\n", id);
             goto failed;
         }
@@ -523,9 +523,10 @@ static int load_flat_file(struct linux_binprm * bprm,
                 fpos = 0;
                 result = bprm->file->f_op->read(bprm->file,
                                 (char *) textpos, text_len, &fpos);
-                if (result < (unsigned long) -4096)
+                if (!is_error(result)) {
                         result = decompress_exec(bprm, text_len, (char *) datapos,
                                          data_len + (relocs * sizeof(unsigned long)), 0);
+                }
         }
         else
 #endif
@@ -693,8 +694,9 @@ static int load_flat_shared_library(int id, struct lib_info *libs)
 
 	res = prepare_binprm(&bprm);
 
-	if (res <= (unsigned long)-4096)
+        if (!is_error(res)) {
 		res = load_flat_file(&bprm, libs, id, NULL);
+        }
 	if (bprm.file) {
 		allow_write_access(bprm.file);
 		fput(bprm.file);
@@ -737,8 +739,9 @@ int load_flt_binary(struct linux_binprm *bprm, struct image_info *info)
 
 
     res = load_flat_file(bprm, libinfo, 0, &stack_len);
-    if (res > (unsigned long)-4096)
+    if (is_error(res)) {
             return res;
+    }
 
Quoted 3 lines

linux-user/flatload.c int load_flt_binary(struct linux_binprm *bprm, struct image_info *info) · line 747

Quoted 3 lines
linux-user/flatload.c +0 -0
     /* Update data segment pointers for all libraries */
     for (i=0; i<MAX_SHARED_LIBS; i++) {

Can you take this via your linux-user tree?

Quoted 19 lines
Quoted 3 lines
Quoted 1 line

Applied, thanks.

Unapplied, it needs a rebase:

No rebase required, it just need the previous patch applied too :) But I didn't think of explicit it :/
Can you take both of them directly or do you prefer I RESEND?

Quoted 11 lines

qemu/linux-user/flatload.c: In function 'load_flt_binary':
qemu/linux-user/flatload.c:742:9: error: implicit declaration of function 'is_error'; did you mean 'g_error'? [-Werror=implicit-function-declaration]

     if (is_error(res)) {
         ^~~~~~~~
         g_error

qemu/linux-user/flatload.c:742:9: error: nested extern declaration of 'is_error' [-Werror=nested-externs]

I have already my next pull-request in test phase without this one, so the better is to resend a series with the patches you want to be merged.

Thanks,
Laurent

#77 lore raw

PATCH 27/35 syscall: fix dereference of undefined pointer

In reply to Philippe Mathieu-Daudé

Clang's scan-build-5.0 reports:

linux-user/syscall.c:5581:9: warning: Dereference of undefined pointer value

    if (*host_rt_dev_ptr != 0) {
        ^~~~~~~~~~~~~~~~
Reported-by Clang Static Analyzer
---
 linux-user/syscall.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
linux-user/syscall.c +2 -1
index fcd20fa276..e79b5baec4 100644
@@ -5524,7 +5524,7 @@ static abi_long do_ioctl_rt(const IOCTLEntry *ie, uint8_t *buf_temp,
     int target_size;
     void *argptr;
     abi_ulong *target_rt_dev_ptr;
-    unsigned long *host_rt_dev_ptr;
+    unsigned long *host_rt_dev_ptr = NULL;
     abi_long ret;
     int i;
 
@@ -5570,6 +5570,7 @@ static abi_long do_ioctl_rt(const IOCTLEntry *ie, uint8_t *buf_temp,
     unlock_user(argptr, arg, 0);
 
     ret = get_errno(safe_ioctl(fd, ie->host_cmd, buf_temp));
+    assert(host_rt_dev_ptr);
     if (*host_rt_dev_ptr != 0) {
         unlock_user((void *)*host_rt_dev_ptr,
                     *target_rt_dev_ptr, 0);
Signature · 1 line
2.13.3
#78 lore raw

Re: [Qemu-devel] [PATCH for 2.10 27/35] syscall: fix dereference of undefined pointer

Quoted 28 lines · Philippe Mathieu-Daudé

Clang's scan-build-5.0 reports:

linux-user/syscall.c:5581:9: warning: Dereference of undefined pointer value

    if (*host_rt_dev_ptr != 0) {
        ^~~~~~~~~~~~~~~~
Reported-by Clang Static Analyzer
---
 linux-user/syscall.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
linux-user/syscall.c +1 -1
index fcd20fa276..e79b5baec4 100644
@@ -5524,7 +5524,7 @@ static abi_long do_ioctl_rt(const IOCTLEntry *ie, uint8_t *buf_temp,
     int target_size;
     void *argptr;
     abi_ulong *target_rt_dev_ptr;
-    unsigned long *host_rt_dev_ptr;
+    unsigned long *host_rt_dev_ptr = NULL;
     abi_long ret;
     int i;
 

linux-user/syscall.c static abi_long do_ioctl_rt(const IOCTLEntry *ie, uint8_t *buf_temp, · line 5576

Quoted 7 lines
linux-user/syscall.c +1 -0
@@ -5570,6 +5570,7 @@ static abi_long do_ioctl_rt(const IOCTLEntry *ie, uint8_t *buf_temp,
     unlock_user(argptr, arg, 0);
 
     ret = get_errno(safe_ioctl(fd, ie->host_cmd, buf_temp));
+    assert(host_rt_dev_ptr);
     if (*host_rt_dev_ptr != 0) {
         unlock_user((void *)*host_rt_dev_ptr,
                     *target_rt_dev_ptr, 0);

I think if you add the assert you should not have to add the "= NULL". Could you check?

Thanks,
Laurent

#79 lore raw

Re: [Qemu-devel] [PATCH for 2.10 27/35] syscall: fix dereference of undefined pointer

Quoted 30 lines · Laurent Vivier
Quoted 29 lines · Philippe Mathieu-Daudé

Clang's scan-build-5.0 reports:

linux-user/syscall.c:5581:9: warning: Dereference of undefined pointer value

     if (*host_rt_dev_ptr != 0) {
         ^~~~~~~~~~~~~~~~
Reported-by Clang Static Analyzer
---
  linux-user/syscall.c | 3 ++-
  1 file changed, 2 insertions(+), 1 deletion(-)
linux-user/syscall.c +1 -1
index fcd20fa276..e79b5baec4 100644
@@ -5524,7 +5524,7 @@ static abi_long do_ioctl_rt(const IOCTLEntry *ie, uint8_t *buf_temp,
      int target_size;
      void *argptr;
      abi_ulong *target_rt_dev_ptr;
-    unsigned long *host_rt_dev_ptr;
+    unsigned long *host_rt_dev_ptr = NULL;
      abi_long ret;
      int i;
 
@@ -5570,6 +5570,7 @@ static abi_long do_ioctl_rt(const IOCTLEntry *ie, uint8_t *buf_temp,
      unlock_user(argptr, arg, 0);
 
Quoted 9 lines

linux-user/syscall.c static abi_long do_ioctl_rt(const IOCTLEntry *ie, uint8_t *buf_temp, · line 5576

Quoted 6 lines
linux-user/syscall.c +1 -0
      ret = get_errno(safe_ioctl(fd, ie->host_cmd, buf_temp));
+    assert(host_rt_dev_ptr);
      if (*host_rt_dev_ptr != 0) {
          unlock_user((void *)*host_rt_dev_ptr,
                      *target_rt_dev_ptr, 0);

I think if you add the assert you should not have to add the "= NULL". Could you check?

Indeed!

Thanks :)

Phil.

#80 lore raw

Re: [Qemu-devel] [PATCH for 2.10 27/35] syscall: fix dereference of undefined pointer

In reply to Philippe Mathieu-Daudé
Quoted 27 lines · Philippe Mathieu-Daudé

Clang's scan-build-5.0 reports:

linux-user/syscall.c:5581:9: warning: Dereference of undefined pointer value

    if (*host_rt_dev_ptr != 0) {
        ^~~~~~~~~~~~~~~~
Reported-by Clang Static Analyzer
---
 linux-user/syscall.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
linux-user/syscall.c +1 -1
index fcd20fa276..e79b5baec4 100644
@@ -5524,7 +5524,7 @@ static abi_long do_ioctl_rt(const IOCTLEntry *ie, uint8_t *buf_temp,
     int target_size;
     void *argptr;
     abi_ulong *target_rt_dev_ptr;
-    unsigned long *host_rt_dev_ptr;
+    unsigned long *host_rt_dev_ptr = NULL;
     abi_long ret;
     int i;
 

linux-user/syscall.c static abi_long do_ioctl_rt(const IOCTLEntry *ie, uint8_t *buf_temp, · line 5573

Quoted 4 lines
linux-user/syscall.c +1 -0
@@ -5570,6 +5570,7 @@ static abi_long do_ioctl_rt(const IOCTLEntry *ie, uint8_t *buf_temp,
     unlock_user(argptr, arg, 0);
 
     ret = get_errno(safe_ioctl(fd, ie->host_cmd, buf_temp));
+    assert(host_rt_dev_ptr);

There's not much point in this assert because it doesn't tell us anything we're not about to find out immediately by dereferencing the pointer...

Quoted 5 lines
linux-user/syscall.c +0 -1
     if (*host_rt_dev_ptr != 0) {
         unlock_user((void *)*host_rt_dev_ptr,
                     *target_rt_dev_ptr, 0);
--

2.13.3

thanks
-- PMM

#81 lore raw

Re: [Qemu-devel] [PATCH for 2.10 27/35] syscall: fix dereference of undefined pointer

Quoted 30 lines · Peter Maydell
Quoted 29 lines · Philippe Mathieu-Daudé

Clang's scan-build-5.0 reports:

linux-user/syscall.c:5581:9: warning: Dereference of undefined pointer value

    if (*host_rt_dev_ptr != 0) {
        ^~~~~~~~~~~~~~~~
Reported-by Clang Static Analyzer
---
 linux-user/syscall.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
linux-user/syscall.c +1 -1
index fcd20fa276..e79b5baec4 100644
@@ -5524,7 +5524,7 @@ static abi_long do_ioctl_rt(const IOCTLEntry *ie, uint8_t *buf_temp,
     int target_size;
     void *argptr;
     abi_ulong *target_rt_dev_ptr;
-    unsigned long *host_rt_dev_ptr;
+    unsigned long *host_rt_dev_ptr = NULL;
     abi_long ret;
     int i;
 
@@ -5570,6 +5570,7 @@ static abi_long do_ioctl_rt(const IOCTLEntry *ie, uint8_t *buf_temp,
     unlock_user(argptr, arg, 0);
 
Quoted 6 lines

linux-user/syscall.c static abi_long do_ioctl_rt(const IOCTLEntry *ie, uint8_t *buf_temp, · line 5573

Quoted 2 lines
linux-user/syscall.c +1 -0
     ret = get_errno(safe_ioctl(fd, ie->host_cmd, buf_temp));
+    assert(host_rt_dev_ptr);

There's not much point in this assert because it doesn't tell us anything we're not about to find out immediately by dereferencing the pointer...

It's just to shut off the warning.

What I said in the comment of the previous version of this patch:

I think we should "assert(host_rt_dev_ptr)" here. It's a bug if host_rt_dev_ptr is not set.

The "for" loop scans the structure to find the rt_dev field, and we should always enter in the first "if", so "host_rt_dev_ptr" is always set.

Thanks,
Laurent

#82 lore raw

PATCH 28/35 syscall: check dup/dup2/dup3() errors, return EBADFD/EINVAL if required

In reply to Philippe Mathieu-Daudé

Linux dup(2) manpage:

ERRORS
EBADF
newfd is out of the allowed range for file descriptors (like negative)
EINVAL (dup3())
oldfd was equal to newfd

Reported-by Clang Static Analyzer
---
 linux-user/syscall.c | 32 +++++++++++++++++++++++---------
 1 file changed, 23 insertions(+), 9 deletions(-)
linux-user/syscall.c +23 -9
index e79b5baec4..637270a02d 100644
@@ -8336,9 +8336,13 @@ abi_long do_syscall(void *cpu_env, int num, abi_long arg1,
         break;
 #endif
     case TARGET_NR_dup:
-        ret = get_errno(dup(arg1));
-        if (ret >= 0) {
-            fd_trans_dup(arg1, ret);
+        if (arg1 < 0) {
+            ret = -TARGET_EBADFD;
+        } else {
+            ret = get_errno(dup(arg1));
+            if (ret >= 0) {
+                fd_trans_dup(arg1, ret);
+            }
         }
         break;
 #ifdef TARGET_NR_pipe
@@ -8436,17 +8440,27 @@ abi_long do_syscall(void *cpu_env, int num, abi_long arg1,
 #endif
 #ifdef TARGET_NR_dup2
     case TARGET_NR_dup2:
-        ret = get_errno(dup2(arg1, arg2));
-        if (ret >= 0) {
-            fd_trans_dup(arg1, arg2);
+        if (arg1 < 0 || arg2 < 0) {
+            ret = -TARGET_EBADFD;
+        } else {
+            ret = get_errno(dup2(arg1, arg2));
+            if (ret >= 0) {
+                fd_trans_dup(arg1, arg2);
+            }
         }
         break;
 #endif
 #if defined(CONFIG_DUP3) && defined(TARGET_NR_dup3)
     case TARGET_NR_dup3:
-        ret = get_errno(dup3(arg1, arg2, arg3));
-        if (ret >= 0) {
-            fd_trans_dup(arg1, arg2);
+        if (arg1 < 0 || arg2 < 0) {
+            ret = -TARGET_EBADFD;
+        } else if (arg1 == arg2) {
+            ret = -TARGET_EINVAL;
+        } else {
+            ret = get_errno(dup3(arg1, arg2, arg3));
+            if (ret >= 0) {
+                fd_trans_dup(arg1, arg2);
+            }
         }
         break;
 #endif
Signature · 1 line
2.13.3
#83 lore raw

Re: [Qemu-devel] [PATCH for 2.10 28/35] syscall: check dup/dup2/dup3() errors, return EBADFD/EINVAL if required

Quoted 8 lines · Philippe Mathieu-Daudé

Linux dup(2) manpage:

ERRORS
EBADF
newfd is out of the allowed range for file descriptors (like negative)
EINVAL (dup3())
oldfd was equal to newfd

Quoted 6 lines
Reported-by Clang Static Analyzer
---
 linux-user/syscall.c | 32 +++++++++++++++++++++++---------
 1 file changed, 23 insertions(+), 9 deletions(-)

I don't understand why we need that: the dup()/dup2()/dup3() functions will return the good error number.

Could you be more explicit on the reason?

Thanks,
Laurent

#84 lore raw

PATCH 29/35 syscall: fix out-of-bound memory access

In reply to Philippe Mathieu-Daudé

linux-user/syscall.c:555:25: warning: Out of bound memory access (accessed memory precedes memory block)

    target_fd_trans[fd] = trans;
    ~~~~~~~~~~~~~~~~~~~~^~~~~~~
Reported-by Clang Static Analyzer
---
 linux-user/syscall.c | 1 +
 1 file changed, 1 insertion(+)
linux-user/syscall.c +1 -0
index 637270a02d..26450d235f 100644
@@ -544,6 +544,7 @@ static void fd_trans_register(int fd, TargetFdTrans *trans)
 {
     unsigned int oldmax;
 
+    assert(fd >= 0);
     if (fd >= target_fd_max) {
         oldmax = target_fd_max;
         target_fd_max = ((fd >> 6) + 1) << 6; /* by slice of 64 entries */
Signature · 1 line
2.13.3
#85 lore raw

Re: [Qemu-devel] [PATCH for 2.10 29/35] syscall: fix out-of-bound memory access

Quoted 16 lines · Philippe Mathieu-Daudé

linux-user/syscall.c:555:25: warning: Out of bound memory access (accessed memory precedes memory block)

    target_fd_trans[fd] = trans;
    ~~~~~~~~~~~~~~~~~~~~^~~~~~~
Reported-by Clang Static Analyzer
---
 linux-user/syscall.c | 1 +
 1 file changed, 1 insertion(+)
linux-user/syscall.c +0 -0
index 637270a02d..26450d235f 100644

linux-user/syscall.c static void fd_trans_register(int fd, TargetFdTrans *trans) · line 550

Quoted 7 lines
linux-user/syscall.c +1 -0
@@ -544,6 +544,7 @@ static void fd_trans_register(int fd, TargetFdTrans *trans)
 {
     unsigned int oldmax;
 
+    assert(fd >= 0);
     if (fd >= target_fd_max) {
         oldmax = target_fd_max;
         target_fd_max = ((fd >> 6) + 1) << 6; /* by slice of 64 entries */

I think we should fix inotify_init(), inotify_init1(), eventfd() and eventfd2() that call fd_trans_register() without checking the value of fd before adding this assert...

Thanks,
Laurent

#86 lore raw

PATCH 30/35 syscall: fix use of uninitialized values

In reply to Philippe Mathieu-Daudé

linux-user/syscall.c:1627:35: warning: 1st function call argument is an uninitialized value

        target_saddr->sa_family = tswap16(addr->sa_family);
                                  ^~~~~~~~~~~~~~~~~~~~~~~~
linux-user/syscall.c:1629:25: warning: The left operand of '==' is a garbage value
    if (addr->sa_family == AF_NETLINK && len >= sizeof(struct sockaddr_nl)) {
        ~~~~~~~~~~~~~~~ ^
Reported-by Clang Static Analyzer
---
 linux-user/syscall.c | 1 +
 1 file changed, 1 insertion(+)
linux-user/syscall.c +1 -0
index 26450d235f..963b9c8f4b 100644
@@ -1618,6 +1618,7 @@ static inline abi_long host_to_target_sockaddr(abi_ulong target_addr,
     if (len == 0) {
         return 0;
     }
+    assert(addr);
 
     target_saddr = lock_user(VERIFY_WRITE, target_addr, len, 0);
     if (!target_saddr)
Signature · 1 line
2.13.3
Reviewed-by on v1 30/35
#87 lore raw

Re: [Qemu-devel] [PATCH for 2.10 30/35] syscall: fix use of uninitialized values

Quoted 19 lines · Philippe Mathieu-Daudé

linux-user/syscall.c:1627:35: warning: 1st function call argument is an uninitialized value

        target_saddr->sa_family = tswap16(addr->sa_family);
                                  ^~~~~~~~~~~~~~~~~~~~~~~~
linux-user/syscall.c:1629:25: warning: The left operand of '==' is a garbage value
    if (addr->sa_family == AF_NETLINK && len >= sizeof(struct sockaddr_nl)) {
        ~~~~~~~~~~~~~~~ ^
Reported-by Clang Static Analyzer
---
 linux-user/syscall.c | 1 +
 1 file changed, 1 insertion(+)
linux-user/syscall.c +0 -0
index 26450d235f..963b9c8f4b 100644

linux-user/syscall.c static inline abi_long host_to_target_sockaddr(abi_ulong target_addr, · line 1624

Quoted 7 lines
linux-user/syscall.c +1 -0
@@ -1618,6 +1618,7 @@ static inline abi_long host_to_target_sockaddr(abi_ulong target_addr,
     if (len == 0) {
         return 0;
     }
+    assert(addr);
 
     target_saddr = lock_user(VERIFY_WRITE, target_addr, len, 0);
     if (!target_saddr)
#88 lore raw

PATCH 31/35 syscall: replace strcpy() by g_strlcpy()

In reply to Philippe Mathieu-Daudé

linux-user/syscall.c:9860:17: warning: Call to function 'strcpy' is insecure as it does not provide bounding of the memory buffer. Replace unbounded copy functions with analogous functions that support length arguments such as 'strlcpy'. CWE-119

                strcpy (buf->machine, cpu_to_uname_machine(cpu_env));
                ^~~~~~
Reported-by Clang Static Analyzer
---
 linux-user/syscall.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
linux-user/syscall.c +2 -1
index 963b9c8f4b..847f729834 100644
@@ -9853,7 +9853,8 @@ abi_long do_syscall(void *cpu_env, int num, abi_long arg1,
             if (!is_error(ret)) {
                 /* Overwrite the native machine name with whatever is being
                    emulated. */
-                strcpy (buf->machine, cpu_to_uname_machine(cpu_env));
+                g_strlcpy(buf->machine, cpu_to_uname_machine(cpu_env),
+                          sizeof(buf->machine));
                 /* Allow the user to override the reported release.  */
                 if (qemu_uname_release && *qemu_uname_release) {
                     g_strlcpy(buf->release, qemu_uname_release,
Signature · 1 line
2.13.3
Reviewed-by on v1 31/35
#89 lore raw

Re: [Qemu-devel] [PATCH for 2.10 31/35] syscall: replace strcpy() by g_strlcpy()

Quoted 16 lines · Philippe Mathieu-Daudé

linux-user/syscall.c:9860:17: warning: Call to function 'strcpy' is insecure as it does not provide bounding of the memory buffer. Replace unbounded copy functions with analogous functions that support length arguments such as 'strlcpy'. CWE-119

                strcpy (buf->machine, cpu_to_uname_machine(cpu_env));
                ^~~~~~
Reported-by Clang Static Analyzer
---
 linux-user/syscall.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
linux-user/syscall.c +0 -0
index 963b9c8f4b..847f729834 100644

linux-user/syscall.c abi_long do_syscall(void *cpu_env, int num, abi_long arg1, · line 9860

Quoted 9 lines
linux-user/syscall.c +2 -1
@@ -9853,7 +9853,8 @@ abi_long do_syscall(void *cpu_env, int num, abi_long arg1,
             if (!is_error(ret)) {
                 /* Overwrite the native machine name with whatever is being
                    emulated. */
-                strcpy (buf->machine, cpu_to_uname_machine(cpu_env));
+                g_strlcpy(buf->machine, cpu_to_uname_machine(cpu_env),
+                          sizeof(buf->machine));
                 /* Allow the user to override the reported release.  */
                 if (qemu_uname_release && *qemu_uname_release) {
                     g_strlcpy(buf->release, qemu_uname_release,

We should not have a problem here as cpu_to_uname_machine() is "const char *" and the string is defined inside QEMU (so it should fit into machine[]).

#90 lore raw

Re: [Qemu-devel] [PATCH for 2.10 31/35] syscall: replace strcpy() by g_strlcpy()

Hi Laurent,

Quoted 31 lines · Laurent Vivier
Quoted 16 lines · Philippe Mathieu-Daudé

linux-user/syscall.c:9860:17: warning: Call to function 'strcpy' is insecure as it does not provide bounding of the memory buffer. Replace unbounded copy functions with analogous functions that support length arguments such as 'strlcpy'. CWE-119

                strcpy (buf->machine, cpu_to_uname_machine(cpu_env));
                ^~~~~~
Reported-by Clang Static Analyzer
---
 linux-user/syscall.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
linux-user/syscall.c +0 -0
index 963b9c8f4b..847f729834 100644

linux-user/syscall.c abi_long do_syscall(void *cpu_env, int num, abi_long arg1, · line 9860

Quoted 9 lines
linux-user/syscall.c +2 -1
@@ -9853,7 +9853,8 @@ abi_long do_syscall(void *cpu_env, int num, abi_long arg1,
             if (!is_error(ret)) {
                 /* Overwrite the native machine name with whatever is being
                    emulated. */
-                strcpy (buf->machine, cpu_to_uname_machine(cpu_env));
+                g_strlcpy(buf->machine, cpu_to_uname_machine(cpu_env),
+                          sizeof(buf->machine));
                 /* Allow the user to override the reported release.  */
                 if (qemu_uname_release && *qemu_uname_release) {
                     g_strlcpy(buf->release, qemu_uname_release,

We should not have a problem here as cpu_to_uname_machine() is "const char *" and the string is defined inside QEMU (so it should fit into machine[]).

Quoted 1 line

Do you mind queuing this patch in your linux-user tree?

Thanks,

Phil.

#91 lore raw

Re: [Qemu-devel] [PATCH for 2.10 31/35] syscall: replace strcpy() by g_strlcpy()

Le 29/05/2018 à 16:19, Philippe Mathieu-Daudé a écrit :

Quoted 34 lines

Hi Laurent,

Quoted 31 lines · Laurent Vivier
Quoted 16 lines · Philippe Mathieu-Daudé

linux-user/syscall.c:9860:17: warning: Call to function 'strcpy' is insecure as it does not provide bounding of the memory buffer. Replace unbounded copy functions with analogous functions that support length arguments such as 'strlcpy'. CWE-119

                strcpy (buf->machine, cpu_to_uname_machine(cpu_env));
                ^~~~~~
Reported-by Clang Static Analyzer
---
 linux-user/syscall.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
linux-user/syscall.c +0 -0
index 963b9c8f4b..847f729834 100644

linux-user/syscall.c abi_long do_syscall(void *cpu_env, int num, abi_long arg1, · line 9860

Quoted 9 lines
linux-user/syscall.c +2 -1
@@ -9853,7 +9853,8 @@ abi_long do_syscall(void *cpu_env, int num, abi_long arg1,
             if (!is_error(ret)) {
                 /* Overwrite the native machine name with whatever is being
                    emulated. */
-                strcpy (buf->machine, cpu_to_uname_machine(cpu_env));
+                g_strlcpy(buf->machine, cpu_to_uname_machine(cpu_env),
+                          sizeof(buf->machine));
                 /* Allow the user to override the reported release.  */
                 if (qemu_uname_release && *qemu_uname_release) {
                     g_strlcpy(buf->release, qemu_uname_release,

We should not have a problem here as cpu_to_uname_machine() is "const char *" and the string is defined inside QEMU (so it should fit into machine[]).

Quoted 3 lines
Quoted 1 line

Do you mind queuing this patch in your linux-user tree?

Applied, thanks

Laurent

#92 lore raw

PATCH 32/35 timer/pxa2xx: silent warning about out-of-bound memory access

In reply to Philippe Mathieu-Daudé

Unlikely to happen.

hw/timer/pxa2xx_timer.c:145:19: warning: Out of bound memory access (accessed memory precedes memory block)

        counter = counters[n];
                  ^~~~~~~~~~~
Reported-by Clang Static Analyzer
---
 hw/timer/pxa2xx_timer.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
hw/timer/pxa2xx_timer.c +1 -1
index 68ba5a70b3..d47f463636 100644
@@ -139,7 +139,7 @@ static void pxa2xx_timer_update4(void *opaque, uint64_t now_qemu, int n)
     if (s->tm4[n].control & (1 << 7))
         counter = n;
     else
-        counter = counters[n];
+        counter = counters[n & 7];
 
     if (!s->tm4[counter].freq) {
         timer_del(s->tm4[n].tm.qtimer);
Signature · 1 line
2.13.3
#93 lore raw

Re: [Qemu-arm] [Qemu-devel] [PATCH for 2.10 32/35] timer/pxa2xx: silent warning about out-of-bound memory access

Quoted 17 lines · Philippe Mathieu-Daudé

Unlikely to happen.

hw/timer/pxa2xx_timer.c:145:19: warning: Out of bound memory access (accessed memory precedes memory block)

        counter = counters[n];
                  ^~~~~~~~~~~
Reported-by Clang Static Analyzer
---
 hw/timer/pxa2xx_timer.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
hw/timer/pxa2xx_timer.c +0 -0
index 68ba5a70b3..d47f463636 100644

hw/timer/pxa2xx_timer.c static void pxa2xx_timer_update4(void *opaque, uint64_t now_qemu, int n)

Quoted 9 lines
hw/timer/pxa2xx_timer.c +1 -2
@@ -139,7 +139,7 @@ static void pxa2xx_timer_update4(void *opaque, uint64_t now_qemu, int n)
     if (s->tm4[n].control & (1 << 7))
         counter = n;
     else
-        counter = counters[n];
+        counter = counters[n & 7];
 
     if (!s->tm4[counter].freq) {
         timer_del(s->tm4[n].tm.qtimer);
--

This looks rather odd, because we use a mask to guard the counters[] array index, but we do an access into another 8-element array with n both immediately above and immediately below that.

It's not actually possible to call this function with n not between 0 and 7 -- if the static analyser can't figure that out does adding an assert at the top of the function help it out?

thanks
-- PMM

#94 lore raw

Re: [Qemu-arm] [Qemu-devel] [PATCH for 2.10 32/35] timer/pxa2xx: silent warning about out-of-bound memory access

Quoted 33 lines · Peter Maydell
Quoted 17 lines · Philippe Mathieu-Daudé

Unlikely to happen.

hw/timer/pxa2xx_timer.c:145:19: warning: Out of bound memory access (accessed memory precedes memory block)

         counter = counters[n];
                   ^~~~~~~~~~~
Reported-by Clang Static Analyzer
---
  hw/timer/pxa2xx_timer.c | 2 +-
  1 file changed, 1 insertion(+), 1 deletion(-)
hw/timer/pxa2xx_timer.c +0 -0
index 68ba5a70b3..d47f463636 100644

hw/timer/pxa2xx_timer.c static void pxa2xx_timer_update4(void *opaque, uint64_t now_qemu, int n)

Quoted 9 lines
hw/timer/pxa2xx_timer.c +1 -2
@@ -139,7 +139,7 @@ static void pxa2xx_timer_update4(void *opaque, uint64_t now_qemu, int n)
      if (s->tm4[n].control & (1 << 7))
          counter = n;
      else
-        counter = counters[n];
+        counter = counters[n & 7];
 
      if (!s->tm4[counter].freq) {
          timer_del(s->tm4[n].tm.qtimer);
--

This looks rather odd, because we use a mask to guard the counters[] array index, but we do an access into another 8-element array with n both immediately above and immediately below that.

Quoted 4 lines

It's not actually possible to call this function with n not between 0 and 7 -- if the static analyser can't figure that out does adding an assert at the top of the function help it out?

Yep, I'm keeping patches with "static analyzer hints" for 2.11 unless there is interest in having them in 2.10 (this patch now included in that 2.11 series).

Thanks,

Phil.

#95 lore raw

RFC PATCH 33/35 configure: clang does not support -Wexpansion-to-defined

In reply to Philippe Mathieu-Daudé
---
 configure | 6 +++++-
 1 file changed, 5 insertions(+), 1 deletion(-)
configure +5 -1
index 6b52e19ee3..48295aa1e6 100755
@@ -1582,8 +1582,12 @@ gcc_flags="-Wold-style-declaration -Wold-style-definition -Wtype-limits"
 gcc_flags="-Wformat-security -Wformat-y2k -Winit-self -Wignored-qualifiers $gcc_flags"
 gcc_flags="-Wno-missing-include-dirs -Wempty-body -Wnested-externs $gcc_flags"
 gcc_flags="-Wendif-labels -Wno-shift-negative-value $gcc_flags"
-gcc_flags="-Wno-initializer-overrides -Wexpansion-to-defined $gcc_flags"
+gcc_flags="-Wno-initializer-overrides $gcc_flags"
 gcc_flags="-Wno-string-plus-int $gcc_flags"
+# clang does not support -Wexpansion-to-defined
+if ! echo | $cc -dM -E - | grep __clang__ > /dev/null 2>&1 ; then
+    gcc_flags="-Wexpansion-to-defined $gcc_flags"
+fi
 # Note that we do not add -Werror to gcc_flags here, because that would
 # enable it for all configure tests. If a configure test failed due
 # to -Werror this would just silently disable some features,
Signature · 1 line
2.13.3
#96 lore raw

Re: [Qemu-devel] [RFC PATCH for 2.10 33/35] configure: clang does not support -Wexpansion-to-defined

Quoted 13 lines · Philippe Mathieu-Daudé
---
 configure | 6 +++++-
 1 file changed, 5 insertions(+), 1 deletion(-)
configure +0 -0
index 6b52e19ee3..48295aa1e6 100755
@@ -1582,8 +1582,12 @@ gcc_flags="-Wold-style-declaration -Wold-style-definition -Wtype-limits"
 gcc_flags="-Wformat-security -Wformat-y2k -Winit-self -Wignored-qualifiers $gcc_flags"
 gcc_flags="-Wno-missing-include-dirs -Wempty-body -Wnested-externs $gcc_flags"
 gcc_flags="-Wendif-labels -Wno-shift-negative-value $gcc_flags"

configure gcc_flags="-Wold-style-declaration -Wold-style-definition -Wtype-limits" · line 1593

Quoted 10 lines
configure +5 -1
-gcc_flags="-Wno-initializer-overrides -Wexpansion-to-defined $gcc_flags"
+gcc_flags="-Wno-initializer-overrides $gcc_flags"
 gcc_flags="-Wno-string-plus-int $gcc_flags"
+# clang does not support -Wexpansion-to-defined
+if ! echo | $cc -dM -E - | grep __clang__ > /dev/null 2>&1 ; then
+    gcc_flags="-Wexpansion-to-defined $gcc_flags"
+fi
 # Note that we do not add -Werror to gcc_flags here, because that would
 # enable it for all configure tests. If a configure test failed due
 # to -Werror this would just silently disable some features,

Clang 3.9 does.

Paolo

#97 lore raw

Re: [Qemu-devel] [RFC PATCH for 2.10 33/35] configure: clang does not support -Wexpansion-to-defined

In reply to Philippe Mathieu-Daudé
Quoted 10 lines · Philippe Mathieu-Daudé
---
 configure | 6 +++++-
 1 file changed, 5 insertions(+), 1 deletion(-)
configure +0 -0
index 6b52e19ee3..48295aa1e6 100755

configure gcc_flags="-Wold-style-declaration -Wold-style-definition -Wtype-limits" · line 1590

Quoted 10 lines
configure +5 -1
@@ -1582,8 +1582,12 @@ gcc_flags="-Wold-style-declaration -Wold-style-definition -Wtype-limits"
 gcc_flags="-Wformat-security -Wformat-y2k -Winit-self -Wignored-qualifiers $gcc_flags"
 gcc_flags="-Wno-missing-include-dirs -Wempty-body -Wnested-externs $gcc_flags"
 gcc_flags="-Wendif-labels -Wno-shift-negative-value $gcc_flags"
-gcc_flags="-Wno-initializer-overrides -Wexpansion-to-defined $gcc_flags"
+gcc_flags="-Wno-initializer-overrides $gcc_flags"
 gcc_flags="-Wno-string-plus-int $gcc_flags"
+# clang does not support -Wexpansion-to-defined
+if ! echo | $cc -dM -E - | grep __clang__ > /dev/null 2>&1 ; then
+    gcc_flags="-Wexpansion-to-defined $gcc_flags"
+fi

We already have a mechanism for testing that the warning flags we pass here are actually supported by the compiler (the cc_has_warning_flag() function, which we run on every flag in $gcc_flags). Why does -Wexpansion-to-defined need special casing?

thanks
-- PMM

#98 lore raw

Re: [Qemu-devel] [RFC PATCH for 2.10 33/35] configure: clang does not support -Wexpansion-to-defined

Quoted 19 lines · Peter Maydell
Quoted 18 lines · Philippe Mathieu-Daudé
---
  configure | 6 +++++-
  1 file changed, 5 insertions(+), 1 deletion(-)
configure +3 -1
index 6b52e19ee3..48295aa1e6 100755
@@ -1582,8 +1582,12 @@ gcc_flags="-Wold-style-declaration -Wold-style-definition -Wtype-limits"
  gcc_flags="-Wformat-security -Wformat-y2k -Winit-self -Wignored-qualifiers $gcc_flags"
  gcc_flags="-Wno-missing-include-dirs -Wempty-body -Wnested-externs $gcc_flags"
  gcc_flags="-Wendif-labels -Wno-shift-negative-value $gcc_flags"
-gcc_flags="-Wno-initializer-overrides -Wexpansion-to-defined $gcc_flags"
+gcc_flags="-Wno-initializer-overrides $gcc_flags"
  gcc_flags="-Wno-string-plus-int $gcc_flags"
+# clang does not support -Wexpansion-to-defined
+if ! echo | $cc -dM -E - | grep __clang__ > /dev/null 2>&1 ; then
Quoted 8 lines

configure gcc_flags="-Wold-style-declaration -Wold-style-definition -Wtype-limits" · line 1590

Quoted 2 lines
configure +2 -0
+    gcc_flags="-Wexpansion-to-defined $gcc_flags"
+fi

We already have a mechanism for testing that the warning flags we pass here are actually supported by the compiler (the cc_has_warning_flag() function, which we run on every flag in $gcc_flags). Why does -Wexpansion-to-defined need special casing?

Yeah no idea, it works with clang 3.9 but not 5.0, I didn't investigate further. I prefixed this patch 'RFC' to not consider it but provide it if someone wanted to reproduce the analysis. Maybe I should prefix it 'XXX' next time or 'NOT FOR MERGE'.

Regards,

Phil.

#99 lore raw

RFC PATCH 34/35 docker: add debian 'bleeding' image with gcc7 and clang5

In reply to Philippe Mathieu-Daudé
---
 .../docker/dockerfiles/debian-bleeding-dev.docker  | 94 ++++++++++++++++++++++
 1 file changed, 94 insertions(+)
 create mode 100644 tests/docker/dockerfiles/debian-bleeding-dev.docker
tests/docker/dockerfiles/debian-bleeding-dev.docker new file +94 -0
new file mode 100644
index 0000000000..d6ae20692c
@@ -0,0 +1,94 @@
+#
+# Docker x86_64 bleeding image
+#
+# This docker target builds on the debian unstable base image. Further
+# libraries which are not widely available are installed by hand.
+#
+FROM debian:unstable
+
+MAINTAINER Philippe Mathieu-Daudé <[email protected]>
+
+RUN echo 'Acquire::ForceIPv4 "true";' >> /etc/apt/apt.conf.d/01network
+
+# Duplicate deb line as deb-src
+RUN cat /etc/apt/sources.list | sed "s/^deb\ /deb-src /" >> /etc/apt/sources.list
+
+# Install common build utilities
+RUN apt-get update && \
+    DEBIAN_FRONTEND=noninteractive apt-get install -yy eatmydata
+RUN DEBIAN_FRONTEND=noninteractive eatmydata \
+    apt-get install -y --no-install-recommends \
+        bison \
+        build-essential \
+        ca-certificates \
+        ccache \
+        flex \
+        git \
+        less \
+        pkg-config \
+        psmisc \
+        python \
+        texinfo \
+        $(apt-get -s build-dep qemu | egrep ^Inst | fgrep '[all]' | cut -d\  -f2)
+
+RUN DEBIAN_FRONTEND=noninteractive eatmydata \
+    apt-get install -y --no-install-recommends \
+        binutils-multiarch \
+        gdb-multiarch \
+        libnewlib-dev \
+        musl-dev \
+        uclibc-source
+
+############################################################################
+# GCC 7
+
+RUN echo deb http://httpredir.debian.org/debian experimental main >> \
+    /etc/apt/sources.list.d/experimental.list && apt-get update
+RUN DEBIAN_FRONTEND=noninteractive eatmydata \
+    apt-get install -y --no-install-recommends \
+        gcc-7 \
+        gcc-7-cross-base-ports \
+        gcc-7-multilib \
+        g++-7
+
+############################################################################
+# Clang 5
+
+RUN DEBIAN_FRONTEND=noninteractive eatmydata \
+    apt-get install -y --no-install-recommends \
+        curl gnupg
+RUN curl http://apt.llvm.org/llvm-snapshot.gpg.key | apt-key add - && \
+    echo 'deb http://apt.llvm.org/unstable/ llvm-toolchain main' \
+        > /etc/apt/sources.list.d/llvm.list && \
+    apt-get update
+RUN DEBIAN_FRONTEND=noninteractive eatmydata \
+    apt-get install -y --no-install-recommends \
+        clang-5.0 \
+        lldb-5.0 \
+        llvm-5.0 && \
+    test -x /usr/bin/clang || ln -s clang-5.0 /usr/bin/clang
+
+############################################################################
+# QEMU specific starts here
+
+RUN DEBIAN_FRONTEND=noninteractive eatmydata \
+    apt-get build-dep -yy qemu
+
+RUN DEBIAN_FRONTEND=noninteractive eatmydata \
+    apt-get install -y --no-install-recommends \
+        libbz2-dev \
+        liblzo2-dev \
+        librdmacm-dev \
+        libsnappy-dev \
+        libvte-dev
+
+# virgl
+RUN DEBIAN_FRONTEND=noninteractive eatmydata \
+    apt-get install -y --no-install-recommends \
+        libegl1-mesa-dev \
+        libepoxy-dev \
+        libgbm-dev
+RUN git clone git://anongit.freedesktop.org/virglrenderer /usr/src/virglrenderer
+RUN cd /usr/src/virglrenderer && ./autogen.sh && ./configure --with-glx --disable-tests && make install
+
+ENV QEMU_CONFIGURE_OPTS --host-cc=clang-5.0 --cc=clang-5.0 --cxx=clang++-5.0
Signature · 1 line
2.13.3
#100 lore raw

Re: [Qemu-devel] [RFC PATCH for 2.10 34/35] docker: add debian 'bleeding' image with gcc7 and clang5

Quoted 12 lines · Philippe Mathieu-Daudé
---
 .../docker/dockerfiles/debian-bleeding-dev.docker  | 94 ++++++++++++++++++++++
 1 file changed, 94 insertions(+)
 create mode 100644 tests/docker/dockerfiles/debian-bleeding-dev.docker
tests/docker/dockerfiles/debian-bleeding-dev.docker new file +0 -0
new file mode 100644
index 0000000000..d6ae20692c

tests/docker/dockerfiles/debian-bleeding-dev.docker line 6

Quoted 6 lines
tests/docker/dockerfiles/debian-bleeding-dev.docker new file +6 -0
@@ -0,0 +1,94 @@
+#
+# Docker x86_64 bleeding image
+#
+# This docker target builds on the debian unstable base image. Further
+# libraries which are not widely available are installed by hand.
+#

Not for 2.10 please.

Quoted 77 lines
tests/docker/dockerfiles/debian-bleeding-dev.docker new file +77 -0
+FROM debian:unstable
+
+MAINTAINER Philippe Mathieu-Daudé <[email protected]>
+
+RUN echo 'Acquire::ForceIPv4 "true";' >> /etc/apt/apt.conf.d/01network
+
+# Duplicate deb line as deb-src
+RUN cat /etc/apt/sources.list | sed "s/^deb\ /deb-src /" >> /etc/apt/sources.list
+
+# Install common build utilities
+RUN apt-get update && \
+    DEBIAN_FRONTEND=noninteractive apt-get install -yy eatmydata
+RUN DEBIAN_FRONTEND=noninteractive eatmydata \
+    apt-get install -y --no-install-recommends \
+        bison \
+        build-essential \
+        ca-certificates \
+        ccache \
+        flex \
+        git \
+        less \
+        pkg-config \
+        psmisc \
+        python \
+        texinfo \
+        $(apt-get -s build-dep qemu | egrep ^Inst | fgrep '[all]' | cut -d\  -f2)
+
+RUN DEBIAN_FRONTEND=noninteractive eatmydata \
+    apt-get install -y --no-install-recommends \
+        binutils-multiarch \
+        gdb-multiarch \
+        libnewlib-dev \
+        musl-dev \
+        uclibc-source
+
+############################################################################
+# GCC 7
+
+RUN echo deb http://httpredir.debian.org/debian experimental main >> \
+    /etc/apt/sources.list.d/experimental.list && apt-get update
+RUN DEBIAN_FRONTEND=noninteractive eatmydata \
+    apt-get install -y --no-install-recommends \
+        gcc-7 \
+        gcc-7-cross-base-ports \
+        gcc-7-multilib \
+        g++-7
+
+############################################################################
+# Clang 5
+
+RUN DEBIAN_FRONTEND=noninteractive eatmydata \
+    apt-get install -y --no-install-recommends \
+        curl gnupg
+RUN curl http://apt.llvm.org/llvm-snapshot.gpg.key | apt-key add - && \
+    echo 'deb http://apt.llvm.org/unstable/ llvm-toolchain main' \
+        > /etc/apt/sources.list.d/llvm.list && \
+    apt-get update
+RUN DEBIAN_FRONTEND=noninteractive eatmydata \
+    apt-get install -y --no-install-recommends \
+        clang-5.0 \
+        lldb-5.0 \
+        llvm-5.0 && \
+    test -x /usr/bin/clang || ln -s clang-5.0 /usr/bin/clang
+
+############################################################################
+# QEMU specific starts here
+
+RUN DEBIAN_FRONTEND=noninteractive eatmydata \
+    apt-get build-dep -yy qemu
+
+RUN DEBIAN_FRONTEND=noninteractive eatmydata \
+    apt-get install -y --no-install-recommends \
+        libbz2-dev \
+        liblzo2-dev \
+        librdmacm-dev \
+        libsnappy-dev \
+        libvte-dev

tests/docker/dockerfiles/debian-bleeding-dev.docker

Quoted 10 lines
tests/docker/dockerfiles/debian-bleeding-dev.docker new file +9 -1
+
+# virgl
+RUN DEBIAN_FRONTEND=noninteractive eatmydata \
+    apt-get install -y --no-install-recommends \
+        libegl1-mesa-dev \
+        libepoxy-dev \
+        libgbm-dev
+RUN git clone git://anongit.freedesktop.org/virglrenderer /usr/src/virglrenderer
+RUN cd /usr/src/virglrenderer && ./autogen.sh && ./configure
--with-glx --disable-tests && make install

There are a lot of moving parts basing this in debian unstable and compiling extra bleeding edge stuff. What does this buy that the clang and toolchain builds in Travis don't already cover?

tests/docker/dockerfiles/debian-bleeding-dev.docker

Quoted 2 lines
tests/docker/dockerfiles/debian-bleeding-dev.docker new file +2 -0
+
+ENV QEMU_CONFIGURE_OPTS --host-cc=clang-5.0 --cc=clang-5.0 --cxx=clang++-5.0

--
Alex Bennée

#101 lore raw

Re: [Qemu-devel] [RFC PATCH for 2.10 34/35] docker: add debian 'bleeding' image with gcc7 and clang5

Quoted 16 lines · Alex Bennée
Quoted 11 lines · Philippe Mathieu-Daudé
---
 .../docker/dockerfiles/debian-bleeding-dev.docker  | 94 ++++++++++++++++++++++
 1 file changed, 94 insertions(+)
 create mode 100644 tests/docker/dockerfiles/debian-bleeding-dev.docker
tests/docker/dockerfiles/debian-bleeding-dev.docker new file +0 -0
new file mode 100644
index 0000000000..d6ae20692c
Quoted 7 lines

tests/docker/dockerfiles/debian-bleeding-dev.docker

Quoted 3 lines
tests/docker/dockerfiles/debian-bleeding-dev.docker new file +2 -1
+RUN git clone git://anongit.freedesktop.org/virglrenderer /usr/src/virglrenderer
+RUN cd /usr/src/virglrenderer && ./autogen.sh && ./configure
--with-glx --disable-tests && make install

There are a lot of moving parts basing this in debian unstable and compiling extra bleeding edge stuff. What does this buy that the clang and toolchain builds in Travis don't already cover?

FWIW, the clang version in Travis is somewhat old compared to the version that Peter uses during merge testing. I recently had a pull request that passed travis tests, but failed with modern clang.

Doesn't neccessarily mean we need debian bleeding edge though - a Fedora 26 image would have detected that since it has new clang.

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

Re: [Qemu-devel] [RFC PATCH for 2.10 34/35] docker: add debian 'bleeding' image with gcc7 and clang5

Quoted 25 lines · Daniel P. Berrange
Quoted 16 lines · Alex Bennée
Quoted 11 lines · Philippe Mathieu-Daudé
---
 .../docker/dockerfiles/debian-bleeding-dev.docker  | 94 ++++++++++++++++++++++
 1 file changed, 94 insertions(+)
 create mode 100644 tests/docker/dockerfiles/debian-bleeding-dev.docker
tests/docker/dockerfiles/debian-bleeding-dev.docker new file +0 -0
new file mode 100644
index 0000000000..d6ae20692c
Quoted 7 lines

tests/docker/dockerfiles/debian-bleeding-dev.docker

Quoted 3 lines
tests/docker/dockerfiles/debian-bleeding-dev.docker new file +2 -1
+RUN git clone git://anongit.freedesktop.org/virglrenderer /usr/src/virglrenderer
+RUN cd /usr/src/virglrenderer && ./autogen.sh && ./configure
--with-glx --disable-tests && make install

There are a lot of moving parts basing this in debian unstable and compiling extra bleeding edge stuff. What does this buy that the clang and toolchain builds in Travis don't already cover?

Quoted 6 lines

FWIW, the clang version in Travis is somewhat old compared to the version that Peter uses during merge testing. I recently had a pull request that passed travis tests, but failed with modern clang.

Doesn't neccessarily mean we need debian bleeding edge though - a Fedora 26 image would have detected that since it has new clang.

Yeah I think from a compiler testing point of view it would be nice to have two images, one for latest clang, one for latest gcc that are pre-set up to build with them for QEMU_CONFIGURE_OPTS. I'd rather those on a stable base distro than taking a potshot on the status of a rolling distro on any given day.

The virgl and other tip of tree installs are done once and probably don't need repeating in other trees.

--
Alex Bennée

#103 lore raw

Re: [Qemu-devel] [RFC PATCH for 2.10 34/35] docker: add debian 'bleeding' image with gcc7 and clang5

In reply to Alex Bennée
Quoted 10 lines · Alex Bennée

tests/docker/dockerfiles/debian-bleeding-dev.docker line 6

Quoted 8 lines

+++ b/tests/docker/dockerfiles/debian-bleeding-dev.docker

tests/docker/dockerfiles/debian-bleeding-dev.docker new file +6 -0
@@ -0,0 +1,94 @@
+#
+# Docker x86_64 bleeding image
+#
+# This docker target builds on the debian unstable base image. Further
+# libraries which are not widely available are installed by hand.
+#

Not for 2.10 please.

Surely not ;) Also unlikely to get merged in mainline...

[...]

Quoted 3 lines

There are a lot of moving parts basing this in debian unstable and compiling extra bleeding edge stuff. What does this buy that the clang and toolchain builds in Travis don't already cover?

Bleeding is bleeding :) This is an implicit No Warranty clause... It might trigger something useful or give you slow death.

https://wiki.debian.org/DebianUnstable
"If you are a hardcore developer or tester you should use this release."

https://www.debian.org/doc/manuals/securing-debian-howto/ch10.en.html#s10.1.3
"you should not use Debian's unstable branch for production-level systems"

#104 lore raw

Re: [Qemu-devel] [RFC PATCH for 2.10 34/35] docker: add debian 'bleeding' image with gcc7 and clang5

Quoted 19 lines · Philippe Mathieu-Daudé
Quoted 10 lines · Alex Bennée

tests/docker/dockerfiles/debian-bleeding-dev.docker line 6

Quoted 8 lines

+++ b/tests/docker/dockerfiles/debian-bleeding-dev.docker

tests/docker/dockerfiles/debian-bleeding-dev.docker new file +6 -0
@@ -0,0 +1,94 @@
+#
+# Docker x86_64 bleeding image
+#
+# This docker target builds on the debian unstable base image. Further
+# libraries which are not widely available are installed by hand.
+#

Not for 2.10 please.

Surely not ;) Also unlikely to get merged in mainline...

[...]

Quoted 3 lines

There are a lot of moving parts basing this in debian unstable and compiling extra bleeding edge stuff. What does this buy that the clang and toolchain builds in Travis don't already cover?

Quoted 8 lines

Bleeding is bleeding :) This is an implicit No Warranty clause... It might trigger something useful or give you slow death.

https://wiki.debian.org/DebianUnstable
"If you are a hardcore developer or tester you should use this release."

https://www.debian.org/doc/manuals/securing-debian-howto/ch10.en.html#s10.1.3
"you should not use Debian's unstable branch for production-level systems"

Quite but from a QEMU point of view these are being used for CI loops and proving a known state of something for users to test on. As I said in reply elsewhere I think to get the benefit of recent Clang and GCCs it would be best to base of some sort of stable base.

--
Alex Bennée

#105 lore raw

RFC PATCH 35/35 script to run docker image

In reply to Philippe Mathieu-Daudé
---
 scripts/dockershell | 30 ++++++++++++++++++++++++++++++
 1 file changed, 30 insertions(+)
 create mode 100755 scripts/dockershell
scripts/dockershell new file +30 -0
new file mode 100755
index 0000000000..f6a2276172
@@ -0,0 +1,30 @@
+#! /bin/sh
+
+# This script starts a docker shell with QEMU directory mounted
+#
+# Copyright (C) 2017 Philippe Mathieu-Daudé. GPLv2+.
+#
+# Usage:
+# ./scripts/dockershell qemu:debian-bleeding-dev
+
+if [ $# -lt 1 ]; then
+    echo
+    echo "error: missing image name"
+    echo
+    docker images qemu --no-trunc --format="- {{.Repository}}:{{.Tag}}"
+    exit 1
+fi
+
+SDIR=$(cd "$(dirname "$0")/.." && pwd -P)
+CDIR=$(pwd -P)
+
+VOLS="-v /tmp:/tmp"
+if [ "${SDIR}" = "${CDIR}" ]; then
+    VOLS="${VOLS} -v ${SDIR}:${SDIR} -w ${SDIR}"
+else
+    VOLS="${VOLS} -v ${SDIR}:${SDIR}:ro -v ${CDIR}:${CDIR} -w ${CDIR}"
+fi
+
+docker run --rm -it ${VOLS} \
+    -u $(id -u) \
+    $1
Signature · 1 line
2.13.3
#106 lore raw

Re: [Qemu-devel] [RFC PATCH for 2.10 35/35] script to run docker image

Quoted 12 lines · Philippe Mathieu-Daudé
---
 scripts/dockershell | 30 ++++++++++++++++++++++++++++++
 1 file changed, 30 insertions(+)
 create mode 100755 scripts/dockershell
scripts/dockershell new file +0 -0
new file mode 100755
index 0000000000..f6a2276172

scripts/dockershell line 8

Quoted 8 lines
scripts/dockershell new file +8 -0
@@ -0,0 +1,30 @@
+#! /bin/sh
+
+# This script starts a docker shell with QEMU directory mounted
+#
+# Copyright (C) 2017 Philippe Mathieu-Daudé. GPLv2+.
+#
+# Usage:
+# ./scripts/dockershell qemu:debian-bleeding-dev

<snip>

This isn't appropriate for 2.10 now we are feature frozen. That said I have pondered a helper script for this sort of thing before. As it stand I just rely on my shell to remember the correct invocation for me.

This is the sort of functionality we would need for maybe cross compiling tests using docker.

--
Alex Bennée

#107 lore raw

Re: [Qemu-devel] [RFC PATCH for 2.10 35/35] script to run docker image

Quoted 1 line

This isn't appropriate for 2.10 now we are feature frozen. That said I

Surely. From the cover:

"... provided at the end of this series as RFC for people wanted
to give a try (also include 'dockershell' script and a fix in ./configure). These RFC patches (33-35) are not expected to enter 2.10."

Peter also commented on the "./configure" patch, so I probably mis-titled "RFC" those patch

http://lists.nongnu.org/archive/html/qemu-devel/2017-07/msg07584.html:
"I prefixed this patch 'RFC' to not consider it but provide it if someone wanted to reproduce the analysis. Maybe I should prefix it 'XXX' next time or 'NOT FOR MERGE'."

better "NOT FOR REVIEW" or "!RFC"? :p

Quoted 5 lines

have pondered a helper script for this sort of thing before. As it stand I just rely on my shell to remember the correct invocation for me.

This is the sort of functionality we would need for maybe cross compiling tests using docker.

This script is way incomplete and far being finished.

Regards,

Phil.

#108 lore raw

Re: [Qemu-devel] [RFC PATCH for 2.10 35/35] script to run docker image

Quoted 8 lines · Philippe Mathieu-Daudé
Quoted 1 line

This isn't appropriate for 2.10 now we are feature frozen. That said I

Surely. From the cover:

"... provided at the end of this series as RFC for people wanted to give a try (also include 'dockershell' script and a fix in ./configure). These RFC patches (33-35) are not expected to enter 2.10."

My mistake - I hadn't gone over the cover letter. I would usually expect a series with [PATCH for 2.10] just to contain stuff being considered for the immediate future.

Quoted 10 lines

Peter also commented on the "./configure" patch, so I probably mis-titled "RFC" those patch

http://lists.nongnu.org/archive/html/qemu-devel/2017-07/msg07584.html:
"I prefixed this patch 'RFC' to not consider it but provide it if someone wanted to reproduce the analysis. Maybe I should prefix it 'XXX' next time or 'NOT FOR MERGE'."

better "NOT FOR REVIEW" or "!RFC"? :p

Better to post a logically separate series. It's fine to make an in-flight series a pre-requisite though (not that I think you need it here).

Quoted 11 lines
Quoted 5 lines

have pondered a helper script for this sort of thing before. As it stand I just rely on my shell to remember the correct invocation for me.

This is the sort of functionality we would need for maybe cross compiling tests using docker.

This script is way incomplete and far being finished.

Regards,

Phil.

--
Alex Bennée

#109 lore raw

Re: [Qemu-devel] [RFC PATCH for 2.10 35/35] script to run docker image

Quoted 5 lines · Alex Bennée
Quoted 4 lines · Philippe Mathieu-Daudé
Quoted 1 line

This isn't appropriate for 2.10 now we are feature frozen. That said I

Surely. From the cover:

Quoted 8 lines
Quoted 4 lines

"... provided at the end of this series as RFC for people wanted to give a try (also include 'dockershell' script and a fix in ./configure). These RFC patches (33-35) are not expected to enter 2.10."

My mistake - I hadn't gone over the cover letter. I would usually expect a series with [PATCH for 2.10] just to contain stuff being considered for the immediate future.

Ah I see, I used --subject-prefix='PATCH for 2.10' thinking "I'v to remember to mark the 3 last patches as "RFC" and when I prefixed with "RFC" I forgot to remove the "for 2.10" suffix. My bad!

#110 lore raw
In reply to Philippe Mathieu-Daudé
Quoted 6 lines · Philippe Mathieu-Daudé

Hi,

This series is the result of having fun with Clang's Static Analyzer during the week-end (see https://clang-analyzer.llvm.org/).

No need to delay the first batch for -rc0.

I had a quick look through the mostly ARM related ones, and there seem to be an awful lot of false positives :-( For getting this into 2.10 it would be helpful to trim down the series so that it has a more manageable number of patches in it and they're fixing actual bugs...

thanks
-- PMM