Skip to contentSkip to conversation

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

PATCH Superseded Bin Meng 2 participants 7 messages Aug 9, 2022 – Aug 11, 2022

v2 of 4 Superseded Latest is v4 →

lore
Share & tools
lore

Bin Meng

author
#0 lore raw

PATCH v2 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.

Fix the logic in qemu_add_wait_object() to avoid adding the same HANDLE twice.

---

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

 util/main-loop.c | 43 +++++++++++++++++++++++++++++++------------
 1 file changed, 31 insertions(+), 12 deletions(-)
util/main-loop.c +31 -12
index f00a25451b..66b2ae2800 100644
@@ -363,37 +363,56 @@ 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};
 
 int qemu_add_wait_object(HANDLE handle, WaitObjectFunc *func, void *opaque)
 {
+    int i;
+    bool found = false;
     WaitObjects *w = &wait_objects;
+
     if (w->num >= MAXIMUM_WAIT_OBJECTS) {
         return -1;
     }
-    w->events[w->num] = handle;
-    w->func[w->num] = func;
-    w->opaque[w->num] = opaque;
-    w->revents[w->num] = 0;
-    w->num++;
+
+    for (i = 0; i < w->num; i++) {
+        /* if the same handle is added twice, newer overwrites older */
+        if (w->events[i] == handle) {
+            found = true;
+            break;
+        }
+    }
+
+    w->events[i] = handle;
+    w->func[i] = func;
+    w->opaque[i] = opaque;
+    w->revents[i] = 0;
+
+    if (!found) {
+        w->num++;
+    }
+
     return 0;
 }
 
 void qemu_del_wait_object(HANDLE handle, WaitObjectFunc *func, void *opaque)
 {
-    int i, found;
+    int i;
+    bool found = false;
     WaitObjects *w = &wait_objects;
 
-    found = 0;
     for (i = 0; i < w->num; i++) {
         if (w->events[i] == handle) {
-            found = 1;
+            found = true;
+        }
+        if (i == MAXIMUM_WAIT_OBJECTS - 1) {
+            break;
         }
         if (found) {
             w->events[i] = w->events[i + 1];
Signature · 1 line
2.34.1
author
#1 lore raw

PATCH v2 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.

---

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 on v2 2/2
#2 lore raw

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

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

lgtm

Quoted 36 lines

---

Changes in v2:
- change 'count' to unsigned

 util/aio-win32.c | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)
util/aio-win32.c +3 -3
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);
         }
     }
--

2.34.1

Signature · 1 line
Marc-André Lureau
#3 lore raw
In reply to Bin Meng

Hi

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

Fix the logic in qemu_add_wait_object() to avoid adding the same HANDLE twice.

Please make that a separate patch.

Quoted 8 lines

---

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

Still NACK, did you understand my argument about array bounds?

"if (found)" will access the arrays at position i+1 == MAXIMUM_WAIT_OBJECTS. We need the +1 for that logic to work without OOB access.

Quoted 83 lines
 util/main-loop.c | 43 +++++++++++++++++++++++++++++++------------
 1 file changed, 31 insertions(+), 12 deletions(-)
util/main-loop.c +31 -12
index f00a25451b..66b2ae2800 100644
@@ -363,37 +363,56 @@ 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};
 
 int qemu_add_wait_object(HANDLE handle, WaitObjectFunc *func, void
 *opaque)
 {
+    int i;
+    bool found = false;
     WaitObjects *w = &wait_objects;
+
     if (w->num >= MAXIMUM_WAIT_OBJECTS) {
         return -1;
     }
-    w->events[w->num] = handle;
-    w->func[w->num] = func;
-    w->opaque[w->num] = opaque;
-    w->revents[w->num] = 0;
-    w->num++;
+
+    for (i = 0; i < w->num; i++) {
+        /* if the same handle is added twice, newer overwrites older */
+        if (w->events[i] == handle) {
+            found = true;
+            break;
+        }
+    }
+
+    w->events[i] = handle;
+    w->func[i] = func;
+    w->opaque[i] = opaque;
+    w->revents[i] = 0;
+
+    if (!found) {
+        w->num++;
+    }
+
     return 0;
 }
 
 void qemu_del_wait_object(HANDLE handle, WaitObjectFunc *func, void
 *opaque)
 {
-    int i, found;
+    int i;
+    bool found = false;
     WaitObjects *w = &wait_objects;
 
-    found = 0;
     for (i = 0; i < w->num; i++) {
         if (w->events[i] == handle) {
-            found = 1;
+            found = true;
+        }
+        if (i == MAXIMUM_WAIT_OBJECTS - 1) {
+            break;
         }
         if (found) {
             w->events[i] = w->events[i + 1];

--
2.34.1

Signature · 1 line
Marc-André Lureau
author
#4 lore raw
Quoted 20 lines · Marc-André Lureau

Hi

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

Fix the logic in qemu_add_wait_object() to avoid adding the same HANDLE twice.

Please make that a separate patch.

Quoted 4 lines

---

Quoted 9 lines
Quoted 4 lines

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

Still NACK, did you understand my argument about array bounds?

"if (found)" will access the arrays at position i+1 == MAXIMUM_WAIT_OBJECTS. We need the +1 for that logic to work without OOB access.

The delete logic was updated in v2. If position is at MAXIMUM_WAIT_OBJECTS - 1, the loop will break.

Regards,
Bin

#5 lore raw
Quoted 29 lines · Bin Meng
Quoted 27 lines · Marc-André Lureau

Hi

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

Fix the logic in qemu_add_wait_object() to avoid adding the same HANDLE twice.

Please make that a separate patch.

Quoted 8 lines

---

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

Still NACK, did you understand my argument about array bounds?

Quoted 8 lines
Quoted 1 line

"if (found)" will access the arrays at position i+1 ==

MAXIMUM_WAIT_OBJECTS. We need the +1 for that logic to work without OOB access.

Quoted 1 line

The delete logic was updated in v2. If position is at MAXIMUM_WAIT_OBJECTS - 1, the loop will break.

Ah I missed that. That new condition looks wrong to me. Not only it is redundant with the loop condition check if w->num == MAXIMUM_WAIT_OBJECTS

But you still access the array at MAXIMUM_WAIT_OBJECTS index, which requires arrays of MAXIMUM_WAIT_OBJECTS+1 size, since it's 0-indexed..

Unless I say crap, which happens sometime :)

Signature · 1 line
Marc-André Lureau
author
#6 lore raw
Quoted 34 lines · Marc-André Lureau
Quoted 30 lines · Bin Meng
Quoted 27 lines · Marc-André Lureau

Hi

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

Fix the logic in qemu_add_wait_object() to avoid adding the same HANDLE twice.

Please make that a separate patch.

Quoted 8 lines

---

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

Still NACK, did you understand my argument about array bounds?

Quoted 8 lines
Quoted 5 lines
Quoted 2 lines

"if (found)" will access the arrays at position i+1 == MAXIMUM_WAIT_OBJECTS. We need the +1 for that logic to work without OOB access.

The delete logic was updated in v2. If position is at MAXIMUM_WAIT_OBJECTS - 1, the loop will break.

Ah I missed that. That new condition looks wrong to me. Not only it is redundant with the loop condition check if w->num == MAXIMUM_WAIT_OBJECTS

Did you mean the "w->num >= MAXIMUM_WAIT_OBJECTS" check in qemu_add_wait_object()? It's necessary because it prevents the OOB access to the array.

Quoted 1 line

But you still access the array at MAXIMUM_WAIT_OBJECTS index, which requires arrays of MAXIMUM_WAIT_OBJECTS+1 size, since it's 0-indexed..

There is no OOB access in either add or delete. Am I missing anything?

Quoted 3 lines

Unless I say crap, which happens sometime :)

Regards,
Bin