Skip to contentSkip to conversation

util/main-loop: Fix maximum number of wait objects for win32

PATCH Superseded Bin Meng 4 participants 16 messages Aug 24, 2022 – Oct 19, 2022

v3 of 4 Superseded Latest is v4 →

lore
Share & tools
lore

Bin Meng

author
#0 lore raw

PATCH v3 1/3 util/main-loop: Fix maximum number of wait objects for win32

From: Bin Meng <[email protected]>

The maximum number of wait objects for win32 should be MAXIMUM_WAIT_OBJECTS, not MAXIMUM_WAIT_OBJECTS + 1.

---

Changes in v3:
- move the check of adding the same HANDLE twice to a separete patch

Changes in v2:
- fix the logic in qemu_add_wait_object() to avoid adding the same HANDLE twice

 util/main-loop.c | 11 +++++++----
 1 file changed, 7 insertions(+), 4 deletions(-)
util/main-loop.c +7 -4
index f00a25451b..cb018dc33c 100644
@@ -363,10 +363,10 @@ void qemu_del_polling_cb(PollingFunc *func, void *opaque)
 /* Wait objects support */
 typedef struct WaitObjects {
     int num;
-    int revents[MAXIMUM_WAIT_OBJECTS + 1];
-    HANDLE events[MAXIMUM_WAIT_OBJECTS + 1];
-    WaitObjectFunc *func[MAXIMUM_WAIT_OBJECTS + 1];
-    void *opaque[MAXIMUM_WAIT_OBJECTS + 1];
+    int revents[MAXIMUM_WAIT_OBJECTS];
+    HANDLE events[MAXIMUM_WAIT_OBJECTS];
+    WaitObjectFunc *func[MAXIMUM_WAIT_OBJECTS];
+    void *opaque[MAXIMUM_WAIT_OBJECTS];
 } WaitObjects;
 
 static WaitObjects wait_objects = {0};
@@ -395,6 +395,9 @@ void qemu_del_wait_object(HANDLE handle, WaitObjectFunc *func, void *opaque)
         if (w->events[i] == handle) {
             found = 1;
         }
+        if (i == MAXIMUM_WAIT_OBJECTS - 1) {
+            break;
+        }
         if (found) {
             w->events[i] = w->events[i + 1];
             w->func[i] = w->func[i + 1];
Signature · 1 line
2.34.1
author
#1 lore raw

PATCH v3 2/3 util/main-loop: Avoid adding the same HANDLE twice

From: Bin Meng <[email protected]>

Fix the logic in qemu_add_wait_object() to avoid adding the same HANDLE twice, as the behavior is undefined when passing an array that contains same HANDLEs to WaitForMultipleObjects() API.

---

Changes in v3:
- new patch: avoid adding the same HANDLE twice

 include/qemu/main-loop.h |  2 ++
 util/main-loop.c         | 10 ++++++++++
 2 files changed, 12 insertions(+)
include/qemu/main-loop.h +2 -0
index c50d1b7e3a..db8d380550 100644
@@ -157,6 +157,8 @@ typedef void WaitObjectFunc(void *opaque);
  * in the main loop's calls to WaitForMultipleObjects.  When the handle
  * is in a signaled state, QEMU will call @func.
  *
+ * If the same HANDLE is added twice, this function returns -1.
+ *
  * @handle: The Windows handle to be observed.
  * @func: A function to be called when @handle is in a signaled state.
  * @opaque: A pointer-size value that is passed to @func.
util/main-loop.c +10 -0
index cb018dc33c..dae33a8daf 100644
@@ -373,10 +373,20 @@ static WaitObjects wait_objects = {0};
 
 int qemu_add_wait_object(HANDLE handle, WaitObjectFunc *func, void *opaque)
 {
+    int i;
     WaitObjects *w = &wait_objects;
+
     if (w->num >= MAXIMUM_WAIT_OBJECTS) {
         return -1;
     }
+
+    for (i = 0; i < w->num; i++) {
+        /* check if the same handle is added twice */
+        if (w->events[i] == handle) {
+            return -1;
+        }
+    }
+
     w->events[w->num] = handle;
     w->func[w->num] = func;
     w->opaque[w->num] = opaque;
Signature · 1 line
2.34.1
Reviewed-by Philippe Mathieu-Daudé on v3 2/3
#2 lore raw

Re: [PATCH v3 2/3] util/main-loop: Avoid adding the same HANDLE twice

Quoted 7 lines · Bin Meng

From: Bin Meng <[email protected]>

Fix the logic in qemu_add_wait_object() to avoid adding the same HANDLE twice, as the behavior is undefined when passing an array that contains same HANDLEs to WaitForMultipleObjects() API.

Quoted 8 lines

---

Changes in v3:
- new patch: avoid adding the same HANDLE twice

  include/qemu/main-loop.h |  2 ++
  util/main-loop.c         | 10 ++++++++++
  2 files changed, 12 insertions(+)
#3 lore raw

Re: [PATCH v3 2/3] util/main-loop: Avoid adding the same HANDLE twice

In reply to Bin Meng
Quoted 5 lines · Bin Meng

From: Bin Meng <[email protected]>

Fix the logic in qemu_add_wait_object() to avoid adding the same HANDLE twice, as the behavior is undefined when passing an array that contains same HANDLEs to WaitForMultipleObjects() API.

Have you encountered this problem in the real world, or is this just a flaw you spotted through code inspection ?

Essentially I'm wondering if there's any known caller that is making this mistake of adding it twice ?

Quoted 53 lines

---

Changes in v3:
- new patch: avoid adding the same HANDLE twice

 include/qemu/main-loop.h |  2 ++
 util/main-loop.c         | 10 ++++++++++
 2 files changed, 12 insertions(+)
include/qemu/main-loop.h +2 -0
index c50d1b7e3a..db8d380550 100644
@@ -157,6 +157,8 @@ typedef void WaitObjectFunc(void *opaque);
  * in the main loop's calls to WaitForMultipleObjects.  When the handle
  * is in a signaled state, QEMU will call @func.
  *
+ * If the same HANDLE is added twice, this function returns -1.
+ *
  * @handle: The Windows handle to be observed.
  * @func: A function to be called when @handle is in a signaled state.
  * @opaque: A pointer-size value that is passed to @func.
util/main-loop.c +10 -0
index cb018dc33c..dae33a8daf 100644
@@ -373,10 +373,20 @@ static WaitObjects wait_objects = {0};
 
 int qemu_add_wait_object(HANDLE handle, WaitObjectFunc *func, void *opaque)
 {
+    int i;
     WaitObjects *w = &wait_objects;
+
     if (w->num >= MAXIMUM_WAIT_OBJECTS) {
         return -1;
     }
+
+    for (i = 0; i < w->num; i++) {
+        /* check if the same handle is added twice */
+        if (w->events[i] == handle) {
+            return -1;
+        }
+    }
+
     w->events[w->num] = handle;
     w->func[w->num] = func;
     w->opaque[w->num] = opaque;
Signature · 1 line
2.34.1

With regards,
Daniel

Signature · 3 lines
|: https://berrange.com      -o-    https://www.flickr.com/photos/dberrange :|
|: https://libvirt.org         -o-            https://fstop138.berrange.com :|
|: https://entangle-photo.org    -o-    https://www.instagram.com/dberrange :|
author
#4 lore raw

Re: [PATCH v3 2/3] util/main-loop: Avoid adding the same HANDLE twice

Hi Daniel,

Quoted 10 lines · Daniel P. Berrangé
Quoted 5 lines · Bin Meng

From: Bin Meng <[email protected]>

Fix the logic in qemu_add_wait_object() to avoid adding the same HANDLE twice, as the behavior is undefined when passing an array that contains same HANDLEs to WaitForMultipleObjects() API.

Have you encountered this problem in the real world, or is this just a flaw you spotted through code inspection ?

No. This was noticed as part of debugging [1] and code inspection was done for all possible suspicious places.

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

Quoted 3 lines

Essentially I'm wondering if there's any known caller that is making this mistake of adding it twice ?

No known caller at this call chain. But there is another in the QIO socket channel APIs that may add the same handle twice, fortunately that scenario is handled properly in the GLib internally.

Quoted 5 lines
Quoted 5 lines

---

Quoted 7 lines
Quoted 7 lines

Changes in v3:
- new patch: avoid adding the same HANDLE twice

 include/qemu/main-loop.h |  2 ++
 util/main-loop.c         | 10 ++++++++++
 2 files changed, 12 insertions(+)

Regards,
Bin

author
#5 lore raw

PATCH v3 3/3 util/aio-win32: Correct the event array size in aio_poll()

In reply to Bin Meng

From: Bin Meng <[email protected]>

WaitForMultipleObjects() can only wait for MAXIMUM_WAIT_OBJECTS object handles. Correct the event array size in aio_poll() and add a assert() to ensure it does not cause out of bound access.

---

(no changes since v2)

Changes in v2:
- change 'count' to unsigned

 util/aio-win32.c | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)
util/aio-win32.c +3 -2
index 44003d645e..80cfe012ad 100644
@@ -326,9 +326,9 @@ void aio_dispatch(AioContext *ctx)
 bool aio_poll(AioContext *ctx, bool blocking)
 {
     AioHandler *node;
-    HANDLE events[MAXIMUM_WAIT_OBJECTS + 1];
+    HANDLE events[MAXIMUM_WAIT_OBJECTS];
     bool progress, have_select_revents, first;
-    int count;
+    unsigned count;
     int timeout;
 
     /*
@@ -369,6 +369,7 @@ bool aio_poll(AioContext *ctx, bool blocking)
     QLIST_FOREACH_RCU(node, &ctx->aio_handlers, node) {
         if (!node->deleted && node->io_notify
             && aio_node_check(ctx, node->is_external)) {
+            assert(count < MAXIMUM_WAIT_OBJECTS);
             events[count++] = event_notifier_get_handle(node->e);
         }
     }
Signature · 1 line
2.34.1
Reviewed-by Philippe Mathieu-Daudé on v3 3/3
#6 lore raw

Re: [PATCH v3 3/3] util/aio-win32: Correct the event array size in aio_poll()

Quoted 11 lines · Bin Meng

From: Bin Meng <[email protected]>

WaitForMultipleObjects() can only wait for MAXIMUM_WAIT_OBJECTS object handles. Correct the event array size in aio_poll() and add a assert() to ensure it does not cause out of bound access.

---

Quoted 7 lines

(no changes since v2)

Changes in v2:
- change 'count' to unsigned

  util/aio-win32.c | 5 +++--
  1 file changed, 3 insertions(+), 2 deletions(-)
Reviewed-by on v3 3/3
#7 lore raw

Re: [PATCH v3 3/3] util/aio-win32: Correct the event array size in aio_poll()

In reply to Bin Meng
Quoted 24 lines · Bin Meng

From: Bin Meng <[email protected]>

WaitForMultipleObjects() can only wait for MAXIMUM_WAIT_OBJECTS object handles. Correct the event array size in aio_poll() and add a assert() to ensure it does not cause out of bound access.

---

(no changes since v2)

Changes in v2:
- change 'count' to unsigned

 util/aio-win32.c | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)
util/aio-win32.c +0 -0
index 44003d645e..80cfe012ad 100644

util/aio-win32.c void aio_dispatch(AioContext *ctx) · line 329

Quoted 5 lines
util/aio-win32.c +1 -1
@@ -326,9 +326,9 @@ void aio_dispatch(AioContext *ctx)
 bool aio_poll(AioContext *ctx, bool blocking)
 {
     AioHandler *node;
-    HANDLE events[MAXIMUM_WAIT_OBJECTS + 1];
+    HANDLE events[MAXIMUM_WAIT_OBJECTS];

Interestingly, the orignial + 1 was entirely pointless, since the aio_poll impl has no bounds checking at all, until your new assert.

Quoted 7 lines
util/aio-win32.c +1 -1
     bool progress, have_select_revents, first;
-    int count;
+    unsigned count;
     int timeout;
 
     /*

util/aio-win32.c bool aio_poll(AioContext *ctx, bool blocking) · line 375

Quoted 7 lines
util/aio-win32.c +1 -0
@@ -369,6 +369,7 @@ bool aio_poll(AioContext *ctx, bool blocking)
     QLIST_FOREACH_RCU(node, &ctx->aio_handlers, node) {
         if (!node->deleted && node->io_notify
             && aio_node_check(ctx, node->is_external)) {
+            assert(count < MAXIMUM_WAIT_OBJECTS);
             events[count++] = event_notifier_get_handle(node->e);
         }
     }

With regards,
Daniel

Signature · 3 lines
|: https://berrange.com      -o-    https://www.flickr.com/photos/dberrange :|
|: https://libvirt.org         -o-            https://fstop138.berrange.com :|
|: https://entangle-photo.org    -o-    https://www.instagram.com/dberrange :|
author
#8 lore raw
In reply to Bin Meng
Quoted 39 lines · Bin Meng

From: Bin Meng <[email protected]>

The maximum number of wait objects for win32 should be MAXIMUM_WAIT_OBJECTS, not MAXIMUM_WAIT_OBJECTS + 1.

---

Changes in v3:
- move the check of adding the same HANDLE twice to a separete patch

Changes in v2:
- fix the logic in qemu_add_wait_object() to avoid adding the same HANDLE twice

 util/main-loop.c | 11 +++++++----
 1 file changed, 7 insertions(+), 4 deletions(-)
util/main-loop.c +4 -4
index f00a25451b..cb018dc33c 100644
@@ -363,10 +363,10 @@ void qemu_del_polling_cb(PollingFunc *func, void *opaque)
 /* Wait objects support */
 typedef struct WaitObjects {
     int num;
-    int revents[MAXIMUM_WAIT_OBJECTS + 1];
-    HANDLE events[MAXIMUM_WAIT_OBJECTS + 1];
-    WaitObjectFunc *func[MAXIMUM_WAIT_OBJECTS + 1];
-    void *opaque[MAXIMUM_WAIT_OBJECTS + 1];
+    int revents[MAXIMUM_WAIT_OBJECTS];
+    HANDLE events[MAXIMUM_WAIT_OBJECTS];
+    WaitObjectFunc *func[MAXIMUM_WAIT_OBJECTS];
+    void *opaque[MAXIMUM_WAIT_OBJECTS];
 } WaitObjects;
 
 static WaitObjects wait_objects = {0};

util/main-loop.c void qemu_del_wait_object(HANDLE handle, WaitObjectFunc *func, void *opaque)

Quoted 10 lines
util/main-loop.c +3 -1
@@ -395,6 +395,9 @@ void qemu_del_wait_object(HANDLE handle, WaitObjectFunc *func, void *opaque)
         if (w->events[i] == handle) {
             found = 1;
         }
+        if (i == MAXIMUM_WAIT_OBJECTS - 1) {
+            break;
+        }
         if (found) {
             w->events[i] = w->events[i + 1];
             w->func[i] = w->func[i + 1];
--

Ping for this series?

author
#9 lore raw
Quoted 39 lines · Bin Meng
Quoted 37 lines · Bin Meng

From: Bin Meng <[email protected]>

The maximum number of wait objects for win32 should be MAXIMUM_WAIT_OBJECTS, not MAXIMUM_WAIT_OBJECTS + 1.

---

Changes in v3:
- move the check of adding the same HANDLE twice to a separete patch

Changes in v2:
- fix the logic in qemu_add_wait_object() to avoid adding the same HANDLE twice

 util/main-loop.c | 11 +++++++----
 1 file changed, 7 insertions(+), 4 deletions(-)
util/main-loop.c +4 -4
index f00a25451b..cb018dc33c 100644
@@ -363,10 +363,10 @@ void qemu_del_polling_cb(PollingFunc *func, void *opaque)
 /* Wait objects support */
 typedef struct WaitObjects {
     int num;
-    int revents[MAXIMUM_WAIT_OBJECTS + 1];
-    HANDLE events[MAXIMUM_WAIT_OBJECTS + 1];
-    WaitObjectFunc *func[MAXIMUM_WAIT_OBJECTS + 1];
-    void *opaque[MAXIMUM_WAIT_OBJECTS + 1];
+    int revents[MAXIMUM_WAIT_OBJECTS];
+    HANDLE events[MAXIMUM_WAIT_OBJECTS];
+    WaitObjectFunc *func[MAXIMUM_WAIT_OBJECTS];
+    void *opaque[MAXIMUM_WAIT_OBJECTS];
 } WaitObjects;
 
Quoted 14 lines

util/main-loop.c void qemu_del_wait_object(HANDLE handle, WaitObjectFunc *func, void *opaque)

Quoted 12 lines
util/main-loop.c +3 -1
 static WaitObjects wait_objects = {0};
@@ -395,6 +395,9 @@ void qemu_del_wait_object(HANDLE handle, WaitObjectFunc *func, void *opaque)
         if (w->events[i] == handle) {
             found = 1;
         }
+        if (i == MAXIMUM_WAIT_OBJECTS - 1) {
+            break;
+        }
         if (found) {
             w->events[i] = w->events[i + 1];
             w->func[i] = w->func[i + 1];
--

Ping for this series?

Ping?

#10 lore raw
In reply to Bin Meng

Hi

Quoted 39 lines · Bin Meng

From: Bin Meng <[email protected]>

The maximum number of wait objects for win32 should be MAXIMUM_WAIT_OBJECTS, not MAXIMUM_WAIT_OBJECTS + 1.

---

Changes in v3:
- move the check of adding the same HANDLE twice to a separete patch

Changes in v2:
- fix the logic in qemu_add_wait_object() to avoid adding the same HANDLE twice

 util/main-loop.c | 11 +++++++----
 1 file changed, 7 insertions(+), 4 deletions(-)
util/main-loop.c +4 -4
index f00a25451b..cb018dc33c 100644
@@ -363,10 +363,10 @@ void qemu_del_polling_cb(PollingFunc *func, void
 *opaque)
 /* Wait objects support */
 typedef struct WaitObjects {
     int num;
-    int revents[MAXIMUM_WAIT_OBJECTS + 1];
-    HANDLE events[MAXIMUM_WAIT_OBJECTS + 1];
-    WaitObjectFunc *func[MAXIMUM_WAIT_OBJECTS + 1];
-    void *opaque[MAXIMUM_WAIT_OBJECTS + 1];
+    int revents[MAXIMUM_WAIT_OBJECTS];
+    HANDLE events[MAXIMUM_WAIT_OBJECTS];
+    WaitObjectFunc *func[MAXIMUM_WAIT_OBJECTS];
+    void *opaque[MAXIMUM_WAIT_OBJECTS];
 } WaitObjects;
 

static WaitObjects wait_objects = {0};

util/main-loop.c void qemu_del_wait_object(HANDLE handle, · line 402

Quoted 8 lines
util/main-loop.c +3 -0
@@ -395,6 +395,9 @@ void qemu_del_wait_object(HANDLE handle,
 WaitObjectFunc *func, void *opaque)
         if (w->events[i] == handle) {
             found = 1;
         }
+        if (i == MAXIMUM_WAIT_OBJECTS - 1) {
+            break;
+        }
 

hmm

Quoted 4 lines
util/main-loop.c +0 -0
         if (found) {
             w->events[i] = w->events[i + 1];
             w->func[i] = w->func[i + 1];

The way deletion works is by moving the i+1 element (which is always zeroed for i == MAXIMUM_WAIT_OBJECTS) to i.

After your patch, for i == MAXIMUM_WAIT_OBJECTS, we no longer clear the last value, and instead rely simply on updated w->num:

    if (found) {
        w->num--;
    }

So your patch looks ok to me, but I prefer the current code.

Paolo, what do you say?

author
#11 lore raw

Hi Paolo,

Quoted 63 lines · Marc-André Lureau

Hi

Quoted 39 lines · Bin Meng

From: Bin Meng <[email protected]>

The maximum number of wait objects for win32 should be MAXIMUM_WAIT_OBJECTS, not MAXIMUM_WAIT_OBJECTS + 1.

---

Changes in v3:
- move the check of adding the same HANDLE twice to a separete patch

Changes in v2:
- fix the logic in qemu_add_wait_object() to avoid adding the same HANDLE twice

 util/main-loop.c | 11 +++++++----
 1 file changed, 7 insertions(+), 4 deletions(-)
util/main-loop.c +4 -4
index f00a25451b..cb018dc33c 100644
@@ -363,10 +363,10 @@ void qemu_del_polling_cb(PollingFunc *func, void *opaque)
 /* Wait objects support */
 typedef struct WaitObjects {
     int num;
-    int revents[MAXIMUM_WAIT_OBJECTS + 1];
-    HANDLE events[MAXIMUM_WAIT_OBJECTS + 1];
-    WaitObjectFunc *func[MAXIMUM_WAIT_OBJECTS + 1];
-    void *opaque[MAXIMUM_WAIT_OBJECTS + 1];
+    int revents[MAXIMUM_WAIT_OBJECTS];
+    HANDLE events[MAXIMUM_WAIT_OBJECTS];
+    WaitObjectFunc *func[MAXIMUM_WAIT_OBJECTS];
+    void *opaque[MAXIMUM_WAIT_OBJECTS];
 } WaitObjects;
 
 static WaitObjects wait_objects = {0};

util/main-loop.c void qemu_del_wait_object(HANDLE handle, WaitObjectFunc *func, void *opaque) · line 400

Quoted 6 lines
util/main-loop.c +3 -0
@@ -395,6 +395,9 @@ void qemu_del_wait_object(HANDLE handle, WaitObjectFunc *func, void *opaque)
         if (w->events[i] == handle) {
             found = 1;
         }
+        if (i == MAXIMUM_WAIT_OBJECTS - 1) {
+            break;
+        }

hmm

util/main-loop.c void qemu_del_wait_object(HANDLE handle, WaitObjectFunc *func, void *opaque) · line 403

Quoted 4 lines
util/main-loop.c +0 -0
         if (found) {
             w->events[i] = w->events[i + 1];
             w->func[i] = w->func[i + 1];

The way deletion works is by moving the i+1 element (which is always zeroed for i == MAXIMUM_WAIT_OBJECTS) to i.

After your patch, for i == MAXIMUM_WAIT_OBJECTS, we no longer clear the last value, and instead rely simply on updated w->num:

Quoted 7 lines
    if (found) {
        w->num--;
    }

So your patch looks ok to me, but I prefer the current code.

Paolo, what do you say?

Ping?

Regards,
Bin

author
#12 lore raw

Hi Paolo,

Quoted 72 lines · Bin Meng

Hi Paolo,

Quoted 67 lines · Marc-André Lureau

Hi

Quoted 39 lines · Bin Meng

From: Bin Meng <[email protected]>

The maximum number of wait objects for win32 should be MAXIMUM_WAIT_OBJECTS, not MAXIMUM_WAIT_OBJECTS + 1.

---

Changes in v3:
- move the check of adding the same HANDLE twice to a separete patch

Changes in v2:
- fix the logic in qemu_add_wait_object() to avoid adding the same HANDLE twice

 util/main-loop.c | 11 +++++++----
 1 file changed, 7 insertions(+), 4 deletions(-)
util/main-loop.c +4 -4
index f00a25451b..cb018dc33c 100644
@@ -363,10 +363,10 @@ void qemu_del_polling_cb(PollingFunc *func, void *opaque)
 /* Wait objects support */
 typedef struct WaitObjects {
     int num;
-    int revents[MAXIMUM_WAIT_OBJECTS + 1];
-    HANDLE events[MAXIMUM_WAIT_OBJECTS + 1];
-    WaitObjectFunc *func[MAXIMUM_WAIT_OBJECTS + 1];
-    void *opaque[MAXIMUM_WAIT_OBJECTS + 1];
+    int revents[MAXIMUM_WAIT_OBJECTS];
+    HANDLE events[MAXIMUM_WAIT_OBJECTS];
+    WaitObjectFunc *func[MAXIMUM_WAIT_OBJECTS];
+    void *opaque[MAXIMUM_WAIT_OBJECTS];
 } WaitObjects;
 
 static WaitObjects wait_objects = {0};

util/main-loop.c void qemu_del_wait_object(HANDLE handle, WaitObjectFunc *func, void *opaque) · line 400

Quoted 6 lines
util/main-loop.c +3 -0
@@ -395,6 +395,9 @@ void qemu_del_wait_object(HANDLE handle, WaitObjectFunc *func, void *opaque)
         if (w->events[i] == handle) {
             found = 1;
         }
+        if (i == MAXIMUM_WAIT_OBJECTS - 1) {
+            break;
+        }

hmm

util/main-loop.c void qemu_del_wait_object(HANDLE handle, WaitObjectFunc *func, void *opaque) · line 403

Quoted 4 lines
util/main-loop.c +0 -0
         if (found) {
             w->events[i] = w->events[i + 1];
             w->func[i] = w->func[i + 1];

The way deletion works is by moving the i+1 element (which is always zeroed for i == MAXIMUM_WAIT_OBJECTS) to i.

After your patch, for i == MAXIMUM_WAIT_OBJECTS, we no longer clear the last value, and instead rely simply on updated w->num:

    if (found) {
        w->num--;
    }
Quoted 6 lines
Quoted 3 lines

So your patch looks ok to me, but I prefer the current code.

Paolo, what do you say?

Ping?

Ping?

Regards,
Bin

author
#13 lore raw

+more people

Quoted 76 lines · Bin Meng

Hi Paolo,

Quoted 72 lines · Bin Meng

Hi Paolo,

Quoted 67 lines · Marc-André Lureau

Hi

Quoted 39 lines · Bin Meng

From: Bin Meng <[email protected]>

The maximum number of wait objects for win32 should be MAXIMUM_WAIT_OBJECTS, not MAXIMUM_WAIT_OBJECTS + 1.

---

Changes in v3:
- move the check of adding the same HANDLE twice to a separete patch

Changes in v2:
- fix the logic in qemu_add_wait_object() to avoid adding the same HANDLE twice

 util/main-loop.c | 11 +++++++----
 1 file changed, 7 insertions(+), 4 deletions(-)
util/main-loop.c +4 -4
index f00a25451b..cb018dc33c 100644
@@ -363,10 +363,10 @@ void qemu_del_polling_cb(PollingFunc *func, void *opaque)
 /* Wait objects support */
 typedef struct WaitObjects {
     int num;
-    int revents[MAXIMUM_WAIT_OBJECTS + 1];
-    HANDLE events[MAXIMUM_WAIT_OBJECTS + 1];
-    WaitObjectFunc *func[MAXIMUM_WAIT_OBJECTS + 1];
-    void *opaque[MAXIMUM_WAIT_OBJECTS + 1];
+    int revents[MAXIMUM_WAIT_OBJECTS];
+    HANDLE events[MAXIMUM_WAIT_OBJECTS];
+    WaitObjectFunc *func[MAXIMUM_WAIT_OBJECTS];
+    void *opaque[MAXIMUM_WAIT_OBJECTS];
 } WaitObjects;
 
 static WaitObjects wait_objects = {0};

util/main-loop.c void qemu_del_wait_object(HANDLE handle, WaitObjectFunc *func, void *opaque) · line 400

Quoted 6 lines
util/main-loop.c +3 -0
@@ -395,6 +395,9 @@ void qemu_del_wait_object(HANDLE handle, WaitObjectFunc *func, void *opaque)
         if (w->events[i] == handle) {
             found = 1;
         }
+        if (i == MAXIMUM_WAIT_OBJECTS - 1) {
+            break;
+        }

hmm

util/main-loop.c void qemu_del_wait_object(HANDLE handle, WaitObjectFunc *func, void *opaque) · line 403

Quoted 4 lines
util/main-loop.c +0 -0
         if (found) {
             w->events[i] = w->events[i + 1];
             w->func[i] = w->func[i + 1];

The way deletion works is by moving the i+1 element (which is always zeroed for i == MAXIMUM_WAIT_OBJECTS) to i.

After your patch, for i == MAXIMUM_WAIT_OBJECTS, we no longer clear the last value, and instead rely simply on updated w->num:

    if (found) {
        w->num--;
    }
Quoted 9 lines
Quoted 6 lines
Quoted 3 lines

So your patch looks ok to me, but I prefer the current code.

Paolo, what do you say?

Ping?

Ping?

Could this series be merged? Thanks,

Regards,
Bin

author
#14 lore raw

+Daniel,

Quoted 84 lines · Bin Meng

+more people

Quoted 80 lines · Bin Meng

Hi Paolo,

Quoted 76 lines · Bin Meng

Hi Paolo,

Quoted 70 lines · Marc-André Lureau

Hi

Quoted 39 lines · Bin Meng

From: Bin Meng <[email protected]>

The maximum number of wait objects for win32 should be MAXIMUM_WAIT_OBJECTS, not MAXIMUM_WAIT_OBJECTS + 1.

---

Changes in v3:
- move the check of adding the same HANDLE twice to a separete patch

Changes in v2:
- fix the logic in qemu_add_wait_object() to avoid adding the same HANDLE twice

 util/main-loop.c | 11 +++++++----
 1 file changed, 7 insertions(+), 4 deletions(-)
util/main-loop.c +4 -4
index f00a25451b..cb018dc33c 100644
@@ -363,10 +363,10 @@ void qemu_del_polling_cb(PollingFunc *func, void *opaque)
 /* Wait objects support */
 typedef struct WaitObjects {
     int num;
-    int revents[MAXIMUM_WAIT_OBJECTS + 1];
-    HANDLE events[MAXIMUM_WAIT_OBJECTS + 1];
-    WaitObjectFunc *func[MAXIMUM_WAIT_OBJECTS + 1];
-    void *opaque[MAXIMUM_WAIT_OBJECTS + 1];
+    int revents[MAXIMUM_WAIT_OBJECTS];
+    HANDLE events[MAXIMUM_WAIT_OBJECTS];
+    WaitObjectFunc *func[MAXIMUM_WAIT_OBJECTS];
+    void *opaque[MAXIMUM_WAIT_OBJECTS];
 } WaitObjects;
 
 static WaitObjects wait_objects = {0};

util/main-loop.c void qemu_del_wait_object(HANDLE handle, WaitObjectFunc *func, void *opaque) · line 400

Quoted 6 lines
util/main-loop.c +3 -0
@@ -395,6 +395,9 @@ void qemu_del_wait_object(HANDLE handle, WaitObjectFunc *func, void *opaque)
         if (w->events[i] == handle) {
             found = 1;
         }
+        if (i == MAXIMUM_WAIT_OBJECTS - 1) {
+            break;
+        }

hmm

util/main-loop.c void qemu_del_wait_object(HANDLE handle, WaitObjectFunc *func, void *opaque) · line 403

Quoted 4 lines
util/main-loop.c +0 -0
         if (found) {
             w->events[i] = w->events[i + 1];
             w->func[i] = w->func[i + 1];

The way deletion works is by moving the i+1 element (which is always zeroed for i == MAXIMUM_WAIT_OBJECTS) to i.

After your patch, for i == MAXIMUM_WAIT_OBJECTS, we no longer clear the last value, and instead rely simply on updated w->num:

    if (found) {
        w->num--;
    }

So your patch looks ok to me, but I prefer the current code.

Paolo, what do you say?

Quoted 8 lines
Quoted 5 lines
Quoted 2 lines

Ping?

Ping?

Could this series be merged? Thanks,

Since Polo keeps silent, Daniel would you help queue this series? Thanks!

Regards,
Bin

#15 lore raw
In reply to Bin Meng
Quoted 38 lines · Bin Meng

From: Bin Meng <[email protected]>

The maximum number of wait objects for win32 should be MAXIMUM_WAIT_OBJECTS, not MAXIMUM_WAIT_OBJECTS + 1.

---

Changes in v3:
- move the check of adding the same HANDLE twice to a separete patch

Changes in v2:
- fix the logic in qemu_add_wait_object() to avoid adding the same HANDLE twice

 util/main-loop.c | 11 +++++++----
 1 file changed, 7 insertions(+), 4 deletions(-)
util/main-loop.c +4 -4
index f00a25451b..cb018dc33c 100644
@@ -363,10 +363,10 @@ void qemu_del_polling_cb(PollingFunc *func, void *opaque)
 /* Wait objects support */
 typedef struct WaitObjects {
     int num;
-    int revents[MAXIMUM_WAIT_OBJECTS + 1];
-    HANDLE events[MAXIMUM_WAIT_OBJECTS + 1];
-    WaitObjectFunc *func[MAXIMUM_WAIT_OBJECTS + 1];
-    void *opaque[MAXIMUM_WAIT_OBJECTS + 1];
+    int revents[MAXIMUM_WAIT_OBJECTS];
+    HANDLE events[MAXIMUM_WAIT_OBJECTS];
+    WaitObjectFunc *func[MAXIMUM_WAIT_OBJECTS];
+    void *opaque[MAXIMUM_WAIT_OBJECTS];
 } WaitObjects;
 
 static WaitObjects wait_objects = {0};

util/main-loop.c void qemu_del_wait_object(HANDLE handle, WaitObjectFunc *func, void *opaque) · line 400

Quoted 6 lines
util/main-loop.c +3 -0
@@ -395,6 +395,9 @@ void qemu_del_wait_object(HANDLE handle, WaitObjectFunc *func, void *opaque)
         if (w->events[i] == handle) {
             found = 1;
         }
+        if (i == MAXIMUM_WAIT_OBJECTS - 1) {
+            break;
+        }

Took me a while to realize this was protecting the body of the next if from out of bounds access. Can we redo this to make it explicit:

util/main-loop.c void qemu_del_wait_object(HANDLE handle, WaitObjectFunc *func, void *opaque) · line 401

Quoted 1 line
util/main-loop.c +0 -0
         if (found) {
   if (found &&
       i < (MAXIMUM_WAIT_OBJECTS - 1)) {

util/main-loop.c void qemu_del_wait_object(HANDLE handle, WaitObjectFunc *func, void *opaque) · line 403

Quoted 2 lines
util/main-loop.c +0 -0
             w->events[i] = w->events[i + 1];
             w->func[i] = w->func[i + 1];

With regards,
Daniel

Signature · 3 lines
|: https://berrange.com      -o-    https://www.flickr.com/photos/dberrange :|
|: https://libvirt.org         -o-            https://fstop138.berrange.com :|
|: https://entangle-photo.org    -o-    https://www.instagram.com/dberrange :|