Skip to contentSkip to conversation

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

PATCH Superseded Bin Meng 3 participants 8 messages Aug 5, 2022 – Aug 9, 2022

v1 of 4 Superseded Latest is v4 →

lore
Share & tools
lore

Bin Meng

author
#0 lore raw

PATCH 1/2 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.

---

 util/main-loop.c | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)
util/main-loop.c +4 -4
index f00a25451b..f15d8e7d12 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};
Signature · 1 line
2.34.1
author
#1 lore raw

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

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.

---

 util/aio-win32.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
util/aio-win32.c +2 -1
index 44003d645e..8cf5779567 100644
@@ -326,7 +326,7 @@ 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;
     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 Stefan Weil on v1 2/2
#2 lore raw

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

Am 05.08.22 um 16:56 schrieb Bin Meng:

Quoted 26 lines

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.

---

  util/aio-win32.c | 3 ++-
  1 file changed, 2 insertions(+), 1 deletion(-)
util/aio-win32.c +1 -1
index 44003d645e..8cf5779567 100644
@@ -326,7 +326,7 @@ 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;
      int timeout;

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

Quoted 4 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);

Would using g_assert for new code be better? Currently the rest of that file (and most QEMU code) uses assert.

count could also be changed from int to unsigned (which matches better to the unsigned DWORD).

author
#3 lore raw

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

Quoted 29 lines · Stefan Weil

Am 05.08.22 um 16:56 schrieb Bin Meng:

Quoted 26 lines

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.

---

  util/aio-win32.c | 3 ++-
  1 file changed, 2 insertions(+), 1 deletion(-)
util/aio-win32.c +1 -1
index 44003d645e..8cf5779567 100644
@@ -326,7 +326,7 @@ 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;
      int timeout;
@@ -369,6 +369,7 @@ bool aio_poll(AioContext *ctx, bool blocking)
Quoted 8 lines

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

Quoted 4 lines
util/aio-win32.c +1 -0
      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);

Would using g_assert for new code be better? Currently the rest of that file (and most QEMU code) uses assert.

Yeah, I noticed that but didn't do that because I feel it's better to be consistent, at least in this single file.

Changing to g_assert() could be a future patch, if necessary.

Quoted 4 lines

count could also be changed from int to unsigned (which matches better to the unsigned DWORD).

changed in v2.

Quoted 1 line

Thanks!

Regards,
Bin

Reviewed-by Philippe Mathieu-Daudé
#5 lore raw
Quoted 7 lines · Bin Meng
Quoted 6 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.

Quoted 8 lines
Quoted 6 lines

---

 util/main-loop.c | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)

Ping?

NB: qemu_del_wait_object() seems dubious in case the same handle is added more than once with qemu_add_wait_object().

#6 lore raw
In reply to Bin Meng

Hi

Quoted 7 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.

Nack,

if wait_objects.num reaches MAXIMUM_WAIT_OBJECTS,

then qemu_del_wait_object() will iterate up to it, and then the branch "if (found)" will access the arrays at position i+1 == MAXIMUM_WAIT_OBJECTS.

Note that the add functions should probably learn to avoid adding the same HANDLE twice, otherwise del is a bit broken.

---

Quoted 29 lines
 util/main-loop.c | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)
util/main-loop.c +4 -4
index f00a25451b..f15d8e7d12 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};
--
2.34.1

Signature · 1 line
Marc-André Lureau
author
#7 lore raw
Quoted 13 lines · Marc-André Lureau

Hi

Quoted 7 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.

Quoted 8 lines

Nack,

if wait_objects.num reaches MAXIMUM_WAIT_OBJECTS,

then qemu_del_wait_object() will iterate up to it, and then the branch "if (found)" will access the arrays at position i+1 == MAXIMUM_WAIT_OBJECTS.

Note that the add functions should probably learn to avoid adding the same HANDLE twice, otherwise del is a bit broken.

Thanks for the review. Will fix in v2.

Regards,
Bin