system: Forbid alloca()
Subsystems (2)
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
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
@@ -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
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 thealloca()call.Reported-by Peter Maydell <[email protected]>Signed-off-by Philippe Mathieu-Daudé <[email protected]>--- target/ppc/kvm.c | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-)target/ppc/kvm.c +0
-0@@ -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
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 thealloca()call.Reported-by Peter Maydell <[email protected]>Signed-off-by Philippe Mathieu-Daudé <[email protected]>--- target/ppc/kvm.c | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-)target/ppc/kvm.c +1
-3@@ -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
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 thealloca()call.Reported-by Peter Maydell <[email protected]>Signed-off-by Philippe Mathieu-Daudé <[email protected]>--- target/ppc/kvm.c | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-)target/ppc/kvm.c +0
-0@@ -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;
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 thealloca()call.Reported-by Peter Maydell <[email protected]>Signed-off-by Philippe Mathieu-Daudé <[email protected]>--- target/ppc/kvm.c | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-)
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
@@ -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
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, forbidalloca()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' inall_languagesSignature · 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 :|
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
@@ -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
Re: [PATCH v2 3/3] docs/devel/style: Mention alloca() family API is forbidden
Quoted 4 lines · Philippe Mathieu-Daudé
Suggested-by Alex Bennée <[email protected]>Signed-off-by Philippe Mathieu-Daudé <[email protected]>---
Quoted 9 lines
docs/devel/style.rst | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)docs/devel/style.rst +0
-0@@ -446,8 +446,8 @@ Low level memory management ===========================
docs/devel/style.rst Low level memory management
Quoted 10 lines
docs/devel/style.rst +2
-3Use 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 :)
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é
Suggested-by Alex Bennée <[email protected]>Signed-off-by Philippe Mathieu-Daudé <[email protected]>---
Reviewed-by Manos Pitsidianakis <[email protected]>Quoted 18 lines
docs/devel/style.rst | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)docs/devel/style.rst +2
-2@@ -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!
Re: [PATCH v2 3/3] docs/devel/style: Mention alloca() family API is forbidden
In reply to Philippe Mathieu-DaudéQuoted 5 lines · Philippe Mathieu-Daudé
Suggested-by Alex Bennée <[email protected]>Signed-off-by Philippe Mathieu-Daudé <[email protected]>--- docs/devel/style.rst | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)
Quoted 4 lines · Philippe Mathieu-Daudé
Philippe Mathieu-Daudé (3):
target/ppc/kvm: Avoid usingalloca()docs/devel/style: Mention
alloca()family API is forbidden
Patches 1 & 3 queued, thanks.