util/main-loop: Fix maximum number of wait objects for win32
PATCH v4 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 v4:
- make the out of bounds access protection explicit
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 | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-)
util/main-loop.c +5 -5
@@ -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,7 +395,7 @@ void qemu_del_wait_object(HANDLE handle, WaitObjectFunc *func, void *opaque)if (w->events[i] == handle) { found =1 ; } -if (found) { +if (found && i < (MAXIMUM_WAIT_OBJECTS -1 )) { w->events[i] = w->events[i +1 ]; w->func[i] = w->func[i +1 ]; w->opaque[i] = w->opaque[i +1 ];
Signature · 1 line
2.34.1
PATCH v4 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.
---
(no changes since v3)
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
@@ -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
@@ -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
Re: [PATCH v4 2/3] util/main-loop: Avoid adding the same HANDLE twice
Quoted 9 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 toWaitForMultipleObjects()API.Signed-off-by Bin Meng <[email protected]>---
Quoted 8 lines
(no changes since v3)
Changes in v3:
- new patch: avoid adding the same HANDLE twiceinclude/qemu/main-loop.h | 2 ++ util/main-loop.c | 10 ++++++++++ 2 files changed, 12 insertions(+)
PATCH v4 3/3 util/aio-win32: Correct the event array size in aio_poll()
In reply to Bin MengFrom: 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
@@ -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
Re: [PATCH v4 3/3] util/aio-win32: Correct the event array size in aio_poll()
Quoted 12 lines · Bin Meng
From: Bin Meng <[email protected]>
WaitForMultipleObjects()can only wait forMAXIMUM_WAIT_OBJECTSobject handles. Correct the event array size inaio_poll()and add aassert()to ensure it does not cause out of bound access.Signed-off-by Bin Meng <[email protected]>Reviewed-by Stefan Weil <[email protected]>Reviewed-by Marc-André Lureau <[email protected]>Reviewed-by Daniel P. Berrangé <[email protected]>---
Quoted 7 lines
(no changes since v2)
Changes in v2:
- change 'count' to unsignedutil/aio-win32.c | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-)
Quoted 15 lines · Bin Meng
From: Bin Meng <[email protected]>
The maximum number of wait objects for win32 should be
MAXIMUM_WAIT_OBJECTS, notMAXIMUM_WAIT_OBJECTS+ 1.Signed-off-by Bin Meng <[email protected]>---
Changes in v4:
- make the out of bounds access protection explicitChanges in v3:
- move the check of adding the same HANDLE twice to a separete patch
Quoted 7 lines
Changes in v2:
- fix the logic inqemu_add_wait_object()to avoid adding the same HANDLE twiceutil/main-loop.c | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-)
Ping?
Hi Daniel,
Quoted 21 lines · Bin Meng
Quoted 19 lines · Bin Meng
From: Bin Meng <[email protected]>
The maximum number of wait objects for win32 should be
MAXIMUM_WAIT_OBJECTS, notMAXIMUM_WAIT_OBJECTS+ 1.Signed-off-by Bin Meng <[email protected]>---
Changes in v4:
- make the out of bounds access protection explicitChanges in v3:
- move the check of adding the same HANDLE twice to a separete patchChanges in v2:
- fix the logic inqemu_add_wait_object()to avoid adding the same HANDLE twice
Quoted 5 lines
Quoted 3 lines
util/main-loop.c | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-)Ping?
Would you queue this series? Thanks!
Regards,
Bin
Quoted 24 lines · Bin Meng
Hi Daniel,
Quoted 21 lines · Bin Meng
Quoted 19 lines · Bin Meng
From: Bin Meng <[email protected]>
The maximum number of wait objects for win32 should be
MAXIMUM_WAIT_OBJECTS, notMAXIMUM_WAIT_OBJECTS+ 1.Signed-off-by Bin Meng <[email protected]>---
Changes in v4:
- make the out of bounds access protection explicitChanges in v3:
- move the check of adding the same HANDLE twice to a separete patchChanges in v2:
- fix the logic inqemu_add_wait_object()to avoid adding the same HANDLE twice
Quoted 7 lines
Quoted 5 lines
Quoted 3 lines
util/main-loop.c | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-)Ping?
Would you queue this series? Thanks!
The main loop is not my area as maintainer - it would normally be Paolo IIRC.
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 :|
Quoted 30 lines · Daniel P. Berrangé
Quoted 28 lines · Bin Meng
Hi Daniel,
Quoted 25 lines · Bin Meng
Quoted 22 lines · Bin Meng
From: Bin Meng <[email protected]>
The maximum number of wait objects for win32 should be
MAXIMUM_WAIT_OBJECTS, notMAXIMUM_WAIT_OBJECTS+ 1.Signed-off-by Bin Meng <[email protected]>---
Changes in v4:
- make the out of bounds access protection explicitChanges in v3:
- move the check of adding the same HANDLE twice to a separete patchChanges in v2:
- fix the logic inqemu_add_wait_object()to avoid adding the same HANDLE twiceutil/main-loop.c | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-)
Quoted 7 lines
Quoted 3 lines
Quoted 1 line
Ping?
Would you queue this series? Thanks!
The main loop is not my area as maintainer - it would normally be Paolo IIRC.
Thanks, but Paolo has been silent since day 1 ...
Regards,
Bin
Quoted 41 lines · Bin Meng
From: Bin Meng <[email protected]>
The maximum number of wait objects for win32 should be
MAXIMUM_WAIT_OBJECTS, notMAXIMUM_WAIT_OBJECTS+ 1.Signed-off-by Bin Meng <[email protected]>---
Changes in v4:
- make the out of bounds access protection explicitChanges in v3:
- move the check of adding the same HANDLE twice to a separete patchChanges in v2:
- fix the logic inqemu_add_wait_object()to avoid adding the same HANDLE twiceutil/main-loop.c | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-)util/main-loop.c +4
-4@@ -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 398
Quoted 5 lines
util/main-loop.c +1
-1@@ -395,7 +395,7 @@ void qemu_del_wait_object(HANDLE handle, WaitObjectFunc *func, void *opaque)if (w->events[i] == handle) { found =1 ; } -if (found) { +if (found && i < (MAXIMUM_WAIT_OBJECTS -1 )) {
Matter of style, I find this form easier to review (same logic than what follows):
if (found && i + 1 < MAXIMUM_WAIT_OBJECTS) {util/main-loop.c void qemu_del_wait_object(HANDLE handle, WaitObjectFunc *func, void *opaque) · line 401
Quoted 3 lines
util/main-loop.c +0
-0w->events[i] = w->events[i +1 ]; w->func[i] = w->func[i +1 ]; w->opaque[i] = w->opaque[i +1 ];