system: Forbid alloca()
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
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
@@ -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
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. HavingPCA955X_PIN_COUNT_MAX= 16, it is safe to explicitly allocate the char buffer on the stack, withoutg_newa().Signed-off-by Philippe Mathieu-Daudé <[email protected]>--- hw/gpio/pca9552.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)hw/gpio/pca9552.c +1
-1@@ -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 )) {
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
@@ -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
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
theg_alloca()call.
Typo in subject L -> :
Regards,
BALATON Zoltan
Quoted 24 lines
Signed-off-by Philippe Mathieu-Daudé <[email protected]>---
backends/tpm/tpm_emulator.c| 4 ++--1 file changed, 2 insertions(+), 2 deletions(-)backends/tpm/tpm_emulator.c +2
-2@@ -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);
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
theg_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 ;)
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
theg_alloca()call.Signed-off-by Philippe Mathieu-Daudé <[email protected]>--- backends/tpm/tpm_emulator.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)backends/tpm/tpm_emulator.c +0
-0@@ -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:
Stefan Berger
Reviewed-byRe: [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
theg_alloca()call.Signed-off-by Philippe Mathieu-Daudé <[email protected]>---
backends/tpm/tpm_emulator.c| 4 ++--1 file changed, 2 insertions(+), 2 deletions(-)backends/tpm/tpm_emulator.c +0
-0@@ -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
-2uint32_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
Reviewed-by Thomas Huth <[email protected]>
Quoted 2 lines
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
theg_alloca()call.Signed-off-by Philippe Mathieu-Daudé <[email protected]>
Quoted 22 lines
--- backends/tpm/tpm_emulator.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)backends/tpm/tpm_emulator.c +2
-2@@ -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);
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
@@ -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
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.Signed-off-by Philippe Mathieu-Daudé <[email protected]>--- tests/unit/test-char.c | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-)tests/unit/test-char.c +1
-1@@ -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?
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.Signed-off-by Philippe Mathieu-Daudé <[email protected]>---
tests/unit/test-char.c| 3 +--1 file changed, 1 insertion(+), 2 deletions(-)tests/unit/test-char.c +1
-1@@ -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);
}---
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.Signed-off-by Philippe Mathieu-Daudé <[email protected]>--- tests/unit/test-char.c | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-)tests/unit/test-char.c +1
-1@@ -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
-1chr = 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_chrladder):-- >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
-2int 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!
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
@@ -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
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 usingg_newa()
backends/tpmL Avoid usingg_alloca()
tests/unit/test-char: Avoid usingg_alloca()
buildsys: Prohibitalloca()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:
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 usingg_newa()
backends/tpmL Avoid usingg_alloca()
tests/unit/test-char: Avoid usingg_alloca()
buildsys: Prohibitalloca()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
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 usingg_newa()
backends/tpmL Avoid usingg_alloca()
tests/unit/test-char: Avoid usingg_alloca()
buildsys: Prohibitalloca()use on system codeQuoted 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()intarget/ppc/kvm.cinkvmppc_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 :/
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 usingg_newa()
backends/tpmL Avoid usingg_alloca()
tests/unit/test-char: Avoid usingg_alloca()
buildsys: Prohibitalloca()use on system codemeson.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
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 usingg_newa()
backends/tpmL Avoid usingg_alloca()
tests/unit/test-char: Avoid usingg_alloca()
buildsys: Prohibitalloca()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:
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 usingg_newa()
backends/tpmL Avoid usingg_alloca()
tests/unit/test-char: Avoid usingg_alloca()
Patches 1-3 queued.