Skip to contentSkip to conversation

system: Forbid alloca()

PATCH Superseded Philippe Mathieu-Daudé 9 participants 20 messages Jun 5, 2025 – Jun 10, 2025 Arm/Amlogic Meson SoC support

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

cover system: Forbid alloca()

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

Philippe Mathieu-Daudé (4):
hw/gpio/pca9552: Avoid using g_newa()
backends/tpmL Avoid using g_alloca()
tests/unit/test-char: Avoid using g_alloca()
buildsys: Prohibit alloca() use on system code

 meson.build                 | 4 ++++
 backends/tpm/tpm_emulator.c | 4 ++--
 hw/gpio/pca9552.c           | 2 +-
 tests/unit/test-char.c      | 3 +--
 4 files changed, 8 insertions(+), 5 deletions(-)
Signature · 1 line
2.49.0
#1 lore raw

PATCH 1/4 hw/gpio/pca9552: Avoid using g_newa()

We have pin_count <= PCA955X_PIN_COUNT_MAX. Having PCA955X_PIN_COUNT_MAX = 16, it is safe to explicitly allocate the char buffer on the stack, without g_newa().

---
 hw/gpio/pca9552.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
hw/gpio/pca9552.c +1 -1
index d65c0a2e90f..1e10238b2e0 100644
@@ -76,7 +76,7 @@ static void pca955x_display_pins_status(PCA955xState *s,
         return;
     }
     if (trace_event_get_state_backends(TRACE_PCA955X_GPIO_STATUS)) {
-        char *buf = g_newa(char, k->pin_count + 1);
+        char buf[PCA955X_PIN_COUNT_MAX + 1];
 
         for (i = 0; i < k->pin_count; i++) {
             if (extract32(pins_status, i, 1)) {
Signature · 1 line
2.49.0
Reviewed-by on v1 1/4
#2 lore raw

Re: [PATCH 1/4] hw/gpio/pca9552: Avoid using g_newa()

Thanks!

Glenn

Quoted 22 lines · Philippe Mathieu-Daudé

We have pin_count <= PCA955X_PIN_COUNT_MAX. Having PCA955X_PIN_COUNT_MAX = 16, it is safe to explicitly allocate the char buffer on the stack, without g_newa().

---
 hw/gpio/pca9552.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
hw/gpio/pca9552.c +1 -1
index d65c0a2e90f..1e10238b2e0 100644
@@ -76,7 +76,7 @@ static void pca955x_display_pins_status(PCA955xState *s,
         return;
     }
     if (trace_event_get_state_backends(TRACE_PCA955X_GPIO_STATUS)) {
-        char *buf = g_newa(char, k->pin_count + 1);
+        char buf[PCA955X_PIN_COUNT_MAX + 1];
 
         for (i = 0; i < k->pin_count; i++) {
             if (extract32(pins_status, i, 1)) {
#3 lore raw

PATCH 2/4 backends/tpmL Avoid using g_alloca()

In reply to Philippe Mathieu-Daudé

tpm_emulator_ctrlcmd() is not in hot path.
Use the heap instead of the stack, removing
the g_alloca() call.

---
 backends/tpm/tpm_emulator.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
backends/tpm/tpm_emulator.c +2 -2
index 43d350e895d..4a234ab2c0b 100644
@@ -129,11 +129,11 @@ static int tpm_emulator_ctrlcmd(TPMEmulator *tpm, unsigned long cmd, void *msg,
     CharBackend *dev = &tpm->ctrl_chr;
     uint32_t cmd_no = cpu_to_be32(cmd);
     ssize_t n = sizeof(uint32_t) + msg_len_in;
-    uint8_t *buf = NULL;
     ptm_res res;
 
     WITH_QEMU_LOCK_GUARD(&tpm->mutex) {
-        buf = g_alloca(n);
+        g_autofree uint8_t *buf = g_malloc(n);
+
         memcpy(buf, &cmd_no, sizeof(cmd_no));
         memcpy(buf + sizeof(cmd_no), msg, msg_len_in);
 
Signature · 1 line
2.49.0
#4 lore raw

Re: [PATCH 2/4] backends/tpmL Avoid using g_alloca()

Quoted 3 lines · Philippe Mathieu-Daudé

tpm_emulator_ctrlcmd() is not in hot path.
Use the heap instead of the stack, removing
the g_alloca() call.

Typo in subject L -> :

Regards,
BALATON Zoltan

Quoted 24 lines

---
backends/tpm/tpm_emulator.c | 4 ++--

1 file changed, 2 insertions(+), 2 deletions(-)
backends/tpm/tpm_emulator.c +2 -2
index 43d350e895d..4a234ab2c0b 100644
@@ -129,11 +129,11 @@ static int tpm_emulator_ctrlcmd(TPMEmulator *tpm, unsigned long cmd, void *msg,
    CharBackend *dev = &tpm->ctrl_chr;
    uint32_t cmd_no = cpu_to_be32(cmd);
    ssize_t n = sizeof(uint32_t) + msg_len_in;
-    uint8_t *buf = NULL;
    ptm_res res;
 
    WITH_QEMU_LOCK_GUARD(&tpm->mutex) {
-        buf = g_alloca(n);
+        g_autofree uint8_t *buf = g_malloc(n);
+
        memcpy(buf, &cmd_no, sizeof(cmd_no));
        memcpy(buf + sizeof(cmd_no), msg, msg_len_in);
 
#5 lore raw

Re: [PATCH 2/4] backends/tpmL Avoid using g_alloca()

Quoted 6 lines · BALATON Zoltan
Quoted 3 lines · Philippe Mathieu-Daudé

tpm_emulator_ctrlcmd() is not in hot path.
Use the heap instead of the stack, removing
the g_alloca() call.

Typo in subject L -> :

Oops thanks, I hurt my ring finger and have it now tied with the middle finger; typing like that is slower and makes me do a lot of typos ;)

Reviewed-by on v1 2/4
#6 lore raw

Re: [PATCH 2/4] backends/tpmL Avoid using g_alloca()

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

tpm_emulator_ctrlcmd() is not in hot path.
Use the heap instead of the stack, removing
the g_alloca() call.

---
  backends/tpm/tpm_emulator.c | 4 ++--
  1 file changed, 2 insertions(+), 2 deletions(-)
backends/tpm/tpm_emulator.c +0 -0
index 43d350e895d..4a234ab2c0b 100644
@@ -129,11 +129,11 @@ static int tpm_emulator_ctrlcmd(TPMEmulator *tpm, unsigned long cmd, void *msg,
      CharBackend *dev = &tpm->ctrl_chr;
      uint32_t cmd_no = cpu_to_be32(cmd);
      ssize_t n = sizeof(uint32_t) + msg_len_in;

backends/tpm/tpm_emulator.c static int tpm_emulator_ctrlcmd(TPMEmulator *tpm, unsigned long cmd, void *msg, · line 139

Quoted 10 lines
backends/tpm/tpm_emulator.c +2 -2
-    uint8_t *buf = NULL;
      ptm_res res;
 
      WITH_QEMU_LOCK_GUARD(&tpm->mutex) {
-        buf = g_alloca(n);
+        g_autofree uint8_t *buf = g_malloc(n);
+
          memcpy(buf, &cmd_no, sizeof(cmd_no));
          memcpy(buf + sizeof(cmd_no), msg, msg_len_in);
 

With the typo fixed:

Reviewed-by
#7 lore raw

Re: [PATCH 2/4] backends/tpmL Avoid using g_alloca()

Quoted 29 lines · Thomas Huth
Quoted 16 lines · Philippe Mathieu-Daudé

tpm_emulator_ctrlcmd() is not in hot path.
Use the heap instead of the stack, removing
the g_alloca() call.

---
  backends/tpm/tpm_emulator.c | 4 ++--

  1 file changed, 2 insertions(+), 2 deletions(-)
backends/tpm/tpm_emulator.c +0 -0
index 43d350e895d..4a234ab2c0b 100644
@@ -129,11 +129,11 @@ static int tpm_emulator_ctrlcmd(TPMEmulator 
 *tpm, unsigned long cmd, void *msg,
       CharBackend *dev = &tpm->ctrl_chr;

backends/tpm/tpm_emulator.c static int tpm_emulator_ctrlcmd(TPMEmulator · line 138

Quoted 10 lines
backends/tpm/tpm_emulator.c +2 -2
       uint32_t cmd_no = cpu_to_be32(cmd);
       ssize_t n = sizeof(uint32_t) + msg_len_in;
-    uint8_t *buf = NULL;
       ptm_res res;
       WITH_QEMU_LOCK_GUARD(&tpm->mutex) {
-        buf = g_alloca(n);
+        g_autofree uint8_t *buf = g_malloc(n);
+
           memcpy(buf, &cmd_no, sizeof(cmd_no));
           memcpy(buf + sizeof(cmd_no), msg, msg_len_in);

With the typo fixed:

Quoted 1 line
Quoted 2 lines
Reviewed-by on v1 2/4
#8 lore raw

Re: [PATCH 2/4] backends/tpmL Avoid using g_alloca()

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

tpm_emulator_ctrlcmd() is not in hot path.
Use the heap instead of the stack, removing
the g_alloca() call.

Quoted 22 lines
---
  backends/tpm/tpm_emulator.c | 4 ++--
  1 file changed, 2 insertions(+), 2 deletions(-)
backends/tpm/tpm_emulator.c +2 -2
index 43d350e895d..4a234ab2c0b 100644
@@ -129,11 +129,11 @@ static int tpm_emulator_ctrlcmd(TPMEmulator *tpm, unsigned long cmd, void *msg,
      CharBackend *dev = &tpm->ctrl_chr;
      uint32_t cmd_no = cpu_to_be32(cmd);
      ssize_t n = sizeof(uint32_t) + msg_len_in;
-    uint8_t *buf = NULL;
      ptm_res res;
 
      WITH_QEMU_LOCK_GUARD(&tpm->mutex) {
-        buf = g_alloca(n);
+        g_autofree uint8_t *buf = g_malloc(n);
+
          memcpy(buf, &cmd_no, sizeof(cmd_no));
          memcpy(buf + sizeof(cmd_no), msg, msg_len_in);
 
#9 lore raw

PATCH 3/4 tests/unit/test-char: Avoid using g_alloca()

In reply to Philippe Mathieu-Daudé

Do not use g_alloca(), simply allocate the CharBackend structure on the stack.

---
 tests/unit/test-char.c | 3 +--
 1 file changed, 1 insertion(+), 2 deletions(-)
tests/unit/test-char.c +1 -2
index 60a843b79d9..f30a39f61ff 100644
@@ -993,7 +993,7 @@ static void char_udp_test_internal(Chardev *reuse_chr, int sock)
     struct sockaddr_in other;
     SocketIdleData d = { 0, };
     Chardev *chr;
-    CharBackend *be;
+    CharBackend stack_be, *be = &stack_be;
     socklen_t alen = sizeof(other);
     int ret;
     char buf[10];
@@ -1009,7 +1009,6 @@ static void char_udp_test_internal(Chardev *reuse_chr, int sock)
         chr = qemu_chr_new("client", tmp, NULL);
         g_assert_nonnull(chr);
 
-        be = g_alloca(sizeof(CharBackend));
         qemu_chr_fe_init(be, chr, &error_abort);
     }
 
Signature · 1 line
2.49.0
#10 lore raw

Re: [PATCH 3/4] tests/unit/test-char: Avoid using g_alloca()

Quoted 22 lines · Philippe Mathieu-Daudé

Do not use g_alloca(), simply allocate the CharBackend structure on the stack.

---
  tests/unit/test-char.c | 3 +--
  1 file changed, 1 insertion(+), 2 deletions(-)
tests/unit/test-char.c +1 -1
index 60a843b79d9..f30a39f61ff 100644
@@ -993,7 +993,7 @@ static void char_udp_test_internal(Chardev *reuse_chr, int sock)
      struct sockaddr_in other;
      SocketIdleData d = { 0, };
      Chardev *chr;
-    CharBackend *be;
+    CharBackend stack_be, *be = &stack_be;
      socklen_t alen = sizeof(other);
      int ret;
      char buf[10];

tests/unit/test-char.c static void char_udp_test_internal(Chardev *reuse_chr, int sock) · line 1014

Quoted 7 lines
tests/unit/test-char.c +0 -1
@@ -1009,7 +1009,6 @@ static void char_udp_test_internal(Chardev *reuse_chr, int sock)
          chr = qemu_chr_new("client", tmp, NULL);
          g_assert_nonnull(chr);
 
-        be = g_alloca(sizeof(CharBackend));
          qemu_chr_fe_init(be, chr, &error_abort);
      }
 

Would that be more simple to declare the variable, and use &be in the function code?

#11 lore raw

Re: [PATCH 3/4] tests/unit/test-char: Avoid using g_alloca()

Quoted 24 lines · Pierrick Bouvier
Quoted 23 lines · Philippe Mathieu-Daudé

Do not use g_alloca(), simply allocate the CharBackend structure on the stack.

---
  tests/unit/test-char.c | 3 +--

  1 file changed, 1 insertion(+), 2 deletions(-)
tests/unit/test-char.c +1 -1
index 60a843b79d9..f30a39f61ff 100644
@@ -993,7 +993,7 @@ static void char_udp_test_internal(Chardev 
 *reuse_chr, int sock)
       struct sockaddr_in other;
       SocketIdleData d = { 0, };
       Chardev *chr;
-    CharBackend *be;
+    CharBackend stack_be, *be = &stack_be;
       socklen_t alen = sizeof(other);
       int ret;

      char buf[10];

tests/unit/test-char.c +0 -0
@@ -1009,7 +1009,6 @@ static void char_udp_test_internal(Chardev 
Quoted 8 lines

tests/unit/test-char.c static void char_udp_test_internal(Chardev *reuse_chr, int sock) · line 1013

Quoted 6 lines
tests/unit/test-char.c +0 -1
 *reuse_chr, int sock)
           chr = qemu_chr_new("client", tmp, NULL);
           g_assert_nonnull(chr);
-        be = g_alloca(sizeof(CharBackend));
           qemu_chr_fe_init(be, chr, &error_abort);
       }

Would that be more simple to declare the variable, and use &be in the function code?

More context (see reuse_chr ladder):

-- >8 --
@@ -991,45 +991,44 @@ static int make_udp_socket(int *port)
static void char_udp_test_internal(Chardev *reuse_chr, int sock)

 {
     struct sockaddr_in other;
     SocketIdleData d = { 0, };
     Chardev *chr;
-    CharBackend *be;
+    CharBackend stack_be, *be = &stack_be;
     socklen_t alen = sizeof(other);
     int ret;
     char buf[10];
     char *tmp = NULL;

     if (reuse_chr) {
         chr = reuse_chr;
         be = chr->be;
     } else {
         int port;
         sock = make_udp_socket(&port);
         tmp = g_strdup_printf("udp:127.0.0.1:%d", port);
         chr = qemu_chr_new("client", tmp, NULL);
         g_assert_nonnull(chr);
-        be = g_alloca(sizeof(CharBackend));
         qemu_chr_fe_init(be, chr, &error_abort);
     }

---

#12 lore raw

Re: [PATCH 3/4] tests/unit/test-char: Avoid using g_alloca()

Quoted 55 lines · Philippe Mathieu-Daudé
Quoted 25 lines · Pierrick Bouvier
Quoted 24 lines · Philippe Mathieu-Daudé

Do not use g_alloca(), simply allocate the CharBackend structure on the stack.

---
   tests/unit/test-char.c | 3 +--
   1 file changed, 1 insertion(+), 2 deletions(-)
tests/unit/test-char.c +1 -1
index 60a843b79d9..f30a39f61ff 100644
@@ -993,7 +993,7 @@ static void char_udp_test_internal(Chardev
 *reuse_chr, int sock)
       struct sockaddr_in other;
       SocketIdleData d = { 0, };
       Chardev *chr;
-    CharBackend *be;
+    CharBackend stack_be, *be = &stack_be;
       socklen_t alen = sizeof(other);
       int ret;

      char buf[10];

tests/unit/test-char.c +0 -0
@@ -1009,7 +1009,6 @@ static void char_udp_test_internal(Chardev
 *reuse_chr, int sock)
Quoted 8 lines

tests/unit/test-char.c static void char_udp_test_internal(Chardev *reuse_chr, int sock) · line 1013

Quoted 5 lines
tests/unit/test-char.c +0 -1
           chr = qemu_chr_new("client", tmp, NULL);
           g_assert_nonnull(chr);
-        be = g_alloca(sizeof(CharBackend));
           qemu_chr_fe_init(be, chr, &error_abort);
       }

Would that be more simple to declare the variable, and use &be in the function code?

More context (see reuse_chr ladder):

-- >8 --

(quoted diff) +1 -1
@@ -991,45 +991,44 @@ static int make_udp_socket(int *port)
   static void char_udp_test_internal(Chardev *reuse_chr, int sock)
   {
       struct sockaddr_in other;
       SocketIdleData d = { 0, };
       Chardev *chr;
-    CharBackend *be;
+    CharBackend stack_be, *be = &stack_be;
       socklen_t alen = sizeof(other);
       int ret;
       char buf[10];
       char *tmp = NULL;
 
       if (reuse_chr) {
           chr = reuse_chr;
           be = chr->be;
       } else {

Review static int make_udp_socket(int *port) · line 1015

Quoted 10 lines
(quoted diff) +0 -2
           int port;
           sock = make_udp_socket(&port);
           tmp = g_strdup_printf("udp:127.0.0.1:%d", port);
           chr = qemu_chr_new("client", tmp, NULL);
           g_assert_nonnull(chr);
 
-        be = g_alloca(sizeof(CharBackend));
           qemu_chr_fe_init(be, chr, &error_abort);
       }
---

Ok!

#13 lore raw

RFC PATCH 4/4 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 ef994676fe9..8c6ccb03c71 100644
@@ -774,6 +774,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.49.0
Reviewed-by on the v1 series
#14 lore raw
In reply to Philippe Mathieu-Daudé
Quoted 9 lines · Philippe Mathieu-Daudé

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

Philippe Mathieu-Daudé (4):
hw/gpio/pca9552: Avoid using g_newa()
backends/tpmL Avoid using g_alloca()
tests/unit/test-char: Avoid using g_alloca()
buildsys: Prohibit alloca() use on system code

Quoted 6 lines
  meson.build                 | 4 ++++
  backends/tpm/tpm_emulator.c | 4 ++--
  hw/gpio/pca9552.c           | 2 +-
  tests/unit/test-char.c      | 3 +--
  4 files changed, 8 insertions(+), 5 deletions(-)

Good idea!

For the series:

#15 lore raw
In reply to Philippe Mathieu-Daudé
Quoted 10 lines · Philippe Mathieu-Daudé

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

Philippe Mathieu-Daudé (4):
hw/gpio/pca9552: Avoid using g_newa()
backends/tpmL Avoid using g_alloca()
tests/unit/test-char: Avoid using g_alloca()
buildsys: Prohibit alloca() use on system code

Quoted 5 lines
 meson.build                 | 4 ++++
 backends/tpm/tpm_emulator.c | 4 ++--
 hw/gpio/pca9552.c           | 2 +-
 tests/unit/test-char.c      | 3 +--
 4 files changed, 8 insertions(+), 5 deletions(-)

There is also a use of alloca() in target/ppc/kvm.c in kvmppc_load_htab_chunk(), so I suspect that patch 4 here will break compilation on PPC hosts with KVM enabled.

thanks
-- PMM

#16 lore raw
Quoted 17 lines · Peter Maydell
Quoted 10 lines · Philippe Mathieu-Daudé

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

Philippe Mathieu-Daudé (4):
hw/gpio/pca9552: Avoid using g_newa()
backends/tpmL Avoid using g_alloca()
tests/unit/test-char: Avoid using g_alloca()
buildsys: Prohibit alloca() use on system code

Quoted 5 lines
  meson.build                 | 4 ++++
  backends/tpm/tpm_emulator.c | 4 ++--
  hw/gpio/pca9552.c           | 2 +-
  tests/unit/test-char.c      | 3 +--
  4 files changed, 8 insertions(+), 5 deletions(-)
Quoted 3 lines

There is also a use of alloca() in target/ppc/kvm.c in kvmppc_load_htab_chunk(), so I suspect that patch 4 here will break compilation on PPC hosts with KVM enabled.

Oops sorry I missed that one :/

#17 lore raw
In reply to Philippe Mathieu-Daudé
Quoted 2 lines · Philippe Mathieu-Daudé

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

Should we also mention it in style.rst:

Use of the ``malloc/free/realloc/calloc/valloc/memalign/posix_memalign`` APIs is not allowed in the QEMU codebase. Instead of these routines,

Quoted 12 lines

Philippe Mathieu-Daudé (4):
hw/gpio/pca9552: Avoid using g_newa()
backends/tpmL Avoid using g_alloca()
tests/unit/test-char: Avoid using g_alloca()
buildsys: Prohibit alloca() use on system code

 meson.build                 | 4 ++++
 backends/tpm/tpm_emulator.c | 4 ++--
 hw/gpio/pca9552.c           | 2 +-
 tests/unit/test-char.c      | 3 +--
 4 files changed, 8 insertions(+), 5 deletions(-)
Signature · 2 lines
Alex Bennée
Virtualisation Tech Lead @ Linaro
Reviewed-by on the v1 series
#18 lore raw
In reply to Philippe Mathieu-Daudé
Quoted 9 lines · Philippe Mathieu-Daudé

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

Philippe Mathieu-Daudé (4):
hw/gpio/pca9552: Avoid using g_newa()
backends/tpmL Avoid using g_alloca()
tests/unit/test-char: Avoid using g_alloca()
buildsys: Prohibit alloca() use on system code

Quoted 5 lines
 meson.build                 | 4 ++++
 backends/tpm/tpm_emulator.c | 4 ++--
 hw/gpio/pca9552.c           | 2 +-
 tests/unit/test-char.c      | 3 +--
 4 files changed, 8 insertions(+), 5 deletions(-)

Modulo the comments that have already been discussed:

#19 lore raw
In reply to Philippe Mathieu-Daudé
Quoted 7 lines · Philippe Mathieu-Daudé

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

Philippe Mathieu-Daudé (4):
hw/gpio/pca9552: Avoid using g_newa()
backends/tpmL Avoid using g_alloca()
tests/unit/test-char: Avoid using g_alloca()

Patches 1-3 queued.