Skip to contentSkip to conversation

system: Forbid alloca()

PATCH In review Philippe Mathieu-Daudé 6 participants 13 messages Sep 1, 2025 – Sep 2, 2025

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

v2 cover system: Forbid alloca()

Eradicate alloca() uses on system code, then enable -Walloca to prevent new ones to creep back in.

Since v1:
- Convert KVM/PPC (Peter)
- Update doc (Alex)

Philippe Mathieu-Daudé (3):
target/ppc/kvm: Avoid using alloca()
buildsys: Prohibit alloca() use on system code docs/devel/style: Mention alloca() family API is forbidden

 docs/devel/style.rst | 4 ++--
 meson.build          | 4 ++++
 target/ppc/kvm.c     | 3 +--
 3 files changed, 7 insertions(+), 4 deletions(-)
Signature · 1 line
2.51.0
#1 lore raw

PATCH v2 1/3 target/ppc/kvm: Avoid using alloca()

kvmppc_load_htab_chunk() is used for migration, thus is not a hot path. Use the heap instead of the stack, removing the alloca() call.

---
 target/ppc/kvm.c | 3 +--
 1 file changed, 1 insertion(+), 2 deletions(-)
target/ppc/kvm.c +1 -2
index d145774b09a..937b9ee986d 100644
@@ -2760,11 +2760,10 @@ int kvmppc_save_htab(QEMUFile *f, int fd, size_t bufsize, int64_t max_ns)
 int kvmppc_load_htab_chunk(QEMUFile *f, int fd, uint32_t index,
                            uint16_t n_valid, uint16_t n_invalid, Error **errp)
 {
-    struct kvm_get_htab_header *buf;
     size_t chunksize = sizeof(*buf) + n_valid * HASH_PTE_SIZE_64;
+    g_autofree struct kvm_get_htab_header *buf = g_malloc(chunksize);
     ssize_t rc;
 
-    buf = alloca(chunksize);
     buf->index = index;
     buf->n_valid = n_valid;
     buf->n_invalid = n_invalid;
Signature · 1 line
2.51.0
Reviewed-by on v2 1/3
#2 lore raw

Re: [PATCH v2 1/3] target/ppc/kvm: Avoid using alloca()

Quoted 18 lines · Philippe Mathieu-Daudé

kvmppc_load_htab_chunk() is used for migration, thus is not a hot path. Use the heap instead of the stack, removing the alloca() call.

---
 target/ppc/kvm.c | 3 +--
 1 file changed, 1 insertion(+), 2 deletions(-)
target/ppc/kvm.c +0 -0
index d145774b09a..937b9ee986d 100644
@@ -2760,11 +2760,10 @@ int kvmppc_save_htab(QEMUFile *f, int fd, size_t bufsize, int64_t max_ns)
 int kvmppc_load_htab_chunk(QEMUFile *f, int fd, uint32_t index,
                            uint16_t n_valid, uint16_t n_invalid, Error **errp)

target/ppc/kvm.c int kvmppc_save_htab(QEMUFile *f, int fd, size_t bufsize, int64_t max_ns) · line 2769

Quoted 10 lines
target/ppc/kvm.c +1 -2
 {
-    struct kvm_get_htab_header *buf;
     size_t chunksize = sizeof(*buf) + n_valid * HASH_PTE_SIZE_64;
+    g_autofree struct kvm_get_htab_header *buf = g_malloc(chunksize);
     ssize_t rc;
 
-    buf = alloca(chunksize);
     buf->index = index;
     buf->n_valid = n_valid;
     buf->n_invalid = n_invalid;

thanks
-- PMM

Reviewed-by on v2 1/3
#3 lore raw

Re: [PATCH v2 1/3] target/ppc/kvm: Avoid using alloca()

In reply to Philippe Mathieu-Daudé
Quoted 29 lines · Philippe Mathieu-Daudé

kvmppc_load_htab_chunk() is used for migration, thus is not a hot path. Use the heap instead of the stack, removing the alloca() call.

---
 target/ppc/kvm.c | 3 +--
 1 file changed, 1 insertion(+), 2 deletions(-)
target/ppc/kvm.c +1 -3
index d145774b09a..937b9ee986d 100644
@@ -2760,11 +2760,10 @@ int kvmppc_save_htab(QEMUFile *f, int fd, size_t bufsize, int64_t max_ns)
 int kvmppc_load_htab_chunk(QEMUFile *f, int fd, uint32_t index,
                            uint16_t n_valid, uint16_t n_invalid, Error **errp)
 {
-    struct kvm_get_htab_header *buf;
     size_t chunksize = sizeof(*buf) + n_valid * HASH_PTE_SIZE_64;
+    g_autofree struct kvm_get_htab_header *buf = g_malloc(chunksize);
     ssize_t rc;
 
-    buf = alloca(chunksize);
     buf->index = index;
     buf->n_valid = n_valid;
     buf->n_invalid = n_invalid;
--
Quoted 3 lines

2.51.0

Reviewed-by on v2 1/3
#4 lore raw

Re: [PATCH v2 1/3] target/ppc/kvm: Avoid using alloca()

In reply to Philippe Mathieu-Daudé
Quoted 17 lines · Philippe Mathieu-Daudé

kvmppc_load_htab_chunk() is used for migration, thus is not a hot path. Use the heap instead of the stack, removing the alloca() call.

---
  target/ppc/kvm.c | 3 +--
  1 file changed, 1 insertion(+), 2 deletions(-)
target/ppc/kvm.c +0 -0
index d145774b09a..937b9ee986d 100644
@@ -2760,11 +2760,10 @@ int kvmppc_save_htab(QEMUFile *f, int fd, size_t bufsize, int64_t max_ns)
  int kvmppc_load_htab_chunk(QEMUFile *f, int fd, uint32_t index,
                             uint16_t n_valid, uint16_t n_invalid, Error **errp)

target/ppc/kvm.c int kvmppc_save_htab(QEMUFile *f, int fd, size_t bufsize, int64_t max_ns) · line 2769

Quoted 10 lines
target/ppc/kvm.c +1 -2
  {
-    struct kvm_get_htab_header *buf;
      size_t chunksize = sizeof(*buf) + n_valid * HASH_PTE_SIZE_64;
+    g_autofree struct kvm_get_htab_header *buf = g_malloc(chunksize);
      ssize_t rc;
 
-    buf = alloca(chunksize);
      buf->index = index;
      buf->n_valid = n_valid;
      buf->n_invalid = n_invalid;
Reviewed-by on v2 1/3
#5 lore raw

Re: [PATCH v2 1/3] target/ppc/kvm: Avoid using alloca()

In reply to Philippe Mathieu-Daudé
Quoted 9 lines · Philippe Mathieu-Daudé

kvmppc_load_htab_chunk() is used for migration, thus is not a hot path. Use the heap instead of the stack, removing the alloca() call.

---
 target/ppc/kvm.c | 3 +--
 1 file changed, 1 insertion(+), 2 deletions(-)
#6 lore raw

PATCH v2 2/3 buildsys: Prohibit alloca() use on system code

In reply to Philippe Mathieu-Daudé

Similarly to commit 64c1a544352 ("meson: Enable -Wvla") with variable length arrays, forbid alloca() uses on system code.

There are few uses on ancient linux-user code, do not bother there.

---
 meson.build | 4 ++++
 1 file changed, 4 insertions(+)
meson.build +4 -0
index 0d42de61ae6..3d1dc2e9e26 100644
@@ -775,6 +775,10 @@ if host_os != 'darwin'
   endif
 endif
 
+if have_system
+  warn_flags += ['-Walloca']
+endif
+
 # Set up C++ compiler flags
 qemu_cxxflags = []
 if 'cpp' in all_languages
Signature · 1 line
2.51.0
#7 lore raw

Re: [PATCH v2 2/3] buildsys: Prohibit alloca() use on system code

Quoted 5 lines · Philippe Mathieu-Daudé

Similarly to commit 64c1a544352 ("meson: Enable -Wvla") with variable length arrays, forbid alloca() uses on system code.

There are few uses on ancient linux-user code, do not bother there.

This says you're not turning on -Walloca for linux-user, but....

meson.build if host_os != 'darwin' · line 780

Quoted 3 lines
meson.build +3 -0
+if have_system
+  warn_flags += ['-Walloca']
+endif

...surely this still turns on -Walloca for linux-user, if the build has enabled multiple targets covering both system & user mode. ie a default qemu build ?

Quoted 7 lines

+
# Set up C++ compiler flags
qemu_cxxflags = []
if 'cpp' in all_languages

Signature · 1 line
2.51.0

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 :|
#8 lore raw

PATCH v2 3/3 docs/devel/style: Mention alloca() family API is forbidden

In reply to Philippe Mathieu-Daudé
---
 docs/devel/style.rst | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
docs/devel/style.rst +2 -2
index d025933808e..941fe14bfd4 100644
@@ -446,8 +446,8 @@ Low level memory management
 ===========================
 
 Use of the ``malloc/free/realloc/calloc/valloc/memalign/posix_memalign``
-APIs is not allowed in the QEMU codebase. Instead of these routines,
-use the GLib memory allocation routines
+or ``alloca/g_alloca/g_newa/g_newa0`` APIs is not allowed in the QEMU codebase.
+Instead of these routines, use the GLib memory allocation routines
 ``g_malloc/g_malloc0/g_new/g_new0/g_realloc/g_free``
 or QEMU's ``qemu_memalign/qemu_blockalign/qemu_vfree`` APIs.
 
Signature · 1 line
2.51.0
Reviewed-by on v2 3/3
#9 lore raw

Re: [PATCH v2 3/3] docs/devel/style: Mention alloca() family API is forbidden

Quoted 4 lines · Philippe Mathieu-Daudé

---

Quoted 9 lines
 docs/devel/style.rst | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
docs/devel/style.rst +0 -0
index d025933808e..941fe14bfd4 100644
@@ -446,8 +446,8 @@ Low level memory management
 ===========================

docs/devel/style.rst Low level memory management

Quoted 10 lines
docs/devel/style.rst +2 -3
 
 Use of the ``malloc/free/realloc/calloc/valloc/memalign/posix_memalign``
-APIs is not allowed in the QEMU codebase. Instead of these routines,
-use the GLib memory allocation routines
+or ``alloca/g_alloca/g_newa/g_newa0`` APIs is not allowed in the QEMU codebase.
+Instead of these routines, use the GLib memory allocation routines
 ``g_malloc/g_malloc0/g_new/g_new0/g_realloc/g_free``
 or QEMU's ``qemu_memalign/qemu_blockalign/qemu_vfree`` APIs.
 
--

If you wanna dust off your perl, you could also add this to checkpatch.pl :)

#10 lore raw

Re: [PATCH v2 3/3] docs/devel/style: Mention alloca() family API is forbidden

Quoted 26 lines · Manos Pitsidianakis
Quoted 4 lines · Philippe Mathieu-Daudé

---

Quoted 18 lines
  docs/devel/style.rst | 4 ++--
  1 file changed, 2 insertions(+), 2 deletions(-)
docs/devel/style.rst +2 -2
index d025933808e..941fe14bfd4 100644
@@ -446,8 +446,8 @@ Low level memory management
  ===========================
 
  Use of the ``malloc/free/realloc/calloc/valloc/memalign/posix_memalign``
-APIs is not allowed in the QEMU codebase. Instead of these routines,
-use the GLib memory allocation routines
+or ``alloca/g_alloca/g_newa/g_newa0`` APIs is not allowed in the QEMU codebase.
+Instead of these routines, use the GLib memory allocation routines
  ``g_malloc/g_malloc0/g_new/g_new0/g_realloc/g_free``
  or QEMU's ``qemu_memalign/qemu_blockalign/qemu_vfree`` APIs.
 
Quoted 3 lines
Quoted 1 line

--

If you wanna dust off your perl, you could also add this to checkpatch.pl :)

We expect contributors to test their patches before posting :P (normally it shouldn't build due to -Walloca in the previous patch)

Thanks!