Skip to contentSkip to conversation

util/oslib-win32: Fix fatal assertion in qemu_try_memalign

PATCH In review Stefan Weil 3 participants 4 messages Jun 11, 2021 – Jun 19, 2021

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 util/oslib-win32: Fix fatal assertion in qemu_try_memalign

The function is called with alignment == 0 which caused an assertion. Use the code from oslib_posix.c to fix that regression (introduced by commit ed6f53f9ca9).

---
 util/oslib-win32.c | 6 +++++-
 1 file changed, 5 insertions(+), 1 deletion(-)
util/oslib-win32.c +5 -1
index ca99356fdf..7b318ea835 100644
@@ -57,7 +57,11 @@ void *qemu_try_memalign(size_t alignment, size_t size)
     void *ptr;
 
     g_assert(size != 0);
-    g_assert(is_power_of_2(alignment));
+    if (alignment < sizeof(void *)) {
+        alignment = sizeof(void *);
+    } else {
+        g_assert(is_power_of_2(alignment));
+    }
     ptr = _aligned_malloc(size, alignment);
     trace_qemu_memalign(alignment, size, ptr);
     return ptr;
Signature · 1 line
2.30.2
Reviewed-by on v1 1/1
#1 lore raw
Quoted 4 lines · Stefan Weil

The function is called with alignment == 0 which caused an assertion. Use the code from oslib_posix.c to fix that regression (introduced by commit ed6f53f9ca9).

Oops.

Can we replace '(introduced by commit ed6f53f9ca9)' by:

Fixes ed6f53f9ca9 ("util/oslib: Assert qemu_try_memalign() alignment is

a power of 2")

Quoted 23 lines
---
 util/oslib-win32.c | 6 +++++-
 1 file changed, 5 insertions(+), 1 deletion(-)
util/oslib-win32.c +5 -1
index ca99356fdf..7b318ea835 100644
@@ -57,7 +57,11 @@ void *qemu_try_memalign(size_t alignment, size_t size)
     void *ptr;
 
     g_assert(size != 0);
-    g_assert(is_power_of_2(alignment));
+    if (alignment < sizeof(void *)) {
+        alignment = sizeof(void *);
+    } else {
+        g_assert(is_power_of_2(alignment));
+    }
     ptr = _aligned_malloc(size, alignment);
     trace_qemu_memalign(alignment, size, ptr);
     return ptr;
#2 lore raw

Am 11.06.21 um 13:06 schrieb Philippe Mathieu-Daudé:

Quoted 5 lines · Stefan Weil
Quoted 4 lines

The function is called with alignment == 0 which caused an assertion. Use the code from oslib_posix.c to fix that regression (introduced by commit ed6f53f9ca9).

Quoted 7 lines

Oops.

Can we replace '(introduced by commit ed6f53f9ca9)' by:

Fixes ed6f53f9ca9 ("util/oslib: Assert qemu_try_memalign() alignment is

a power of 2")

Sure, that's fine for me, too. I think the text can be changed without a v2 of the patch.

Stefan

#3 lore raw
In reply to Stefan Weil
Quoted 5 lines

The function is called with alignment == 0 which caused an assertion. Use the code from oslib_posix.c to fix that regression (introduced by commit ed6f53f9ca9).

I'll queue this to tcg-next, as I'm about to do a PR for that. I'll add the Fixed: tag as suggested by Phil.

r~