The Linux Kernel Mailing List
 help / color / mirror / Atom feed
* [PATCH] selftests/vfio: avoid VLAs
@ 2026-06-12 21:58 Alex Mastro
  2026-06-12 23:45 ` Vipin Sharma
  0 siblings, 1 reply; 3+ messages in thread
From: Alex Mastro @ 2026-06-12 21:58 UTC (permalink / raw)
  To: David Matlack, Alex Williamson, Shuah Khan,
	Raghavendra Rao Ananta, Vipin Sharma
  Cc: kvm, linux-kselftest, linux-kernel, Shuah Khan, Alex Mastro

Allocate VFIO ioctl requests dynamically instead of using VLAs. GCC 11.5.0
rejects initialized VLAs with:

  error: variable-sized object may not be initialized

The replaced stack u8 arrays also do not guarantee native struct alignment
for the aliased pointers.

Fixes: 19faf6fd969c ("vfio: selftests: Add a helper library for VFIO selftests")
Fixes: 20face8c75ff ("vfio: selftests: Add helper to set/override a vf_token")
Signed-off-by: Alex Mastro <amastro@fb.com>
Assisted-by: Codex:gpt-5.5-high
---
 tools/testing/selftests/vfio/lib/vfio_pci_device.c | 28 +++++++++++++---------
 1 file changed, 17 insertions(+), 11 deletions(-)

diff --git a/tools/testing/selftests/vfio/lib/vfio_pci_device.c b/tools/testing/selftests/vfio/lib/vfio_pci_device.c
index 94dc5fcecbeb..0b437435cce1 100644
--- a/tools/testing/selftests/vfio/lib/vfio_pci_device.c
+++ b/tools/testing/selftests/vfio/lib/vfio_pci_device.c
@@ -30,13 +30,12 @@
 static void vfio_pci_irq_set(struct vfio_pci_device *device,
 			     u32 index, u32 vector, u32 count, int *fds)
 {
-	u8 buf[sizeof(struct vfio_irq_set) + sizeof(int) * count];
-	struct vfio_irq_set *irq = (void *)&buf;
-	int *irq_fds = (void *)&irq->data;
+	struct vfio_irq_set *irq;
+	size_t irq_size = sizeof(*irq) + sizeof(int) * count;
 
-	memset(buf, 0, sizeof(buf));
-
-	irq->argsz = sizeof(buf);
+	irq = calloc(1, irq_size);
+	VFIO_ASSERT_NOT_NULL(irq);
+	irq->argsz = irq_size;
 	irq->flags = VFIO_IRQ_SET_ACTION_TRIGGER;
 	irq->index = index;
 	irq->start = vector;
@@ -44,12 +43,13 @@ static void vfio_pci_irq_set(struct vfio_pci_device *device,
 
 	if (count) {
 		irq->flags |= VFIO_IRQ_SET_DATA_EVENTFD;
-		memcpy(irq_fds, fds, sizeof(int) * count);
+		memcpy(irq->data, fds, sizeof(int) * count);
 	} else {
 		irq->flags |= VFIO_IRQ_SET_DATA_NONE;
 	}
 
 	ioctl_assert(device->fd, VFIO_DEVICE_SET_IRQS, irq);
+	free(irq);
 }
 
 void vfio_pci_irq_trigger(struct vfio_pci_device *device, u32 index, u32 vector)
@@ -118,15 +118,21 @@ static void vfio_pci_irq_get(struct vfio_pci_device *device, u32 index,
 static int vfio_device_feature_ioctl(int fd, u32 flags, void *data,
 				     size_t data_size)
 {
-	u8 buffer[sizeof(struct vfio_device_feature) + data_size] = {};
-	struct vfio_device_feature *feature = (void *)buffer;
+	struct vfio_device_feature *feature;
+	size_t feature_size = sizeof(*feature) + data_size;
+	int ret;
 
+	feature = calloc(1, feature_size);
+	VFIO_ASSERT_NOT_NULL(feature);
 	memcpy(feature->data, data, data_size);
 
-	feature->argsz = sizeof(buffer);
+	feature->argsz = feature_size;
 	feature->flags = flags;
 
-	return ioctl(fd, VFIO_DEVICE_FEATURE, feature);
+	ret = ioctl(fd, VFIO_DEVICE_FEATURE, feature);
+	free(feature);
+
+	return ret;
 }
 
 static void vfio_device_feature_set(int fd, u16 feature, void *data, size_t data_size)

---
base-commit: a26b499b757cfc8bbff1088bb1b844639e250893
change-id: 20260612-scratch-amastro-vfio-selftests-avoid-vlas-395eb3dcb3ab

Best regards,
--  
Alex Mastro <amastro@fb.com>


^ permalink raw reply related	[flat|nested] 3+ messages in thread

* Re: [PATCH] selftests/vfio: avoid VLAs
  2026-06-12 21:58 [PATCH] selftests/vfio: avoid VLAs Alex Mastro
@ 2026-06-12 23:45 ` Vipin Sharma
  2026-06-15 15:19   ` Alex Mastro
  0 siblings, 1 reply; 3+ messages in thread
From: Vipin Sharma @ 2026-06-12 23:45 UTC (permalink / raw)
  To: Alex Mastro
  Cc: David Matlack, Alex Williamson, Shuah Khan,
	Raghavendra Rao Ananta, kvm, linux-kselftest, linux-kernel,
	Shuah Khan

On Fri, Jun 12, 2026 at 02:58:18PM -0700, Alex Mastro wrote:
> Allocate VFIO ioctl requests dynamically instead of using VLAs. GCC 11.5.0
> rejects initialized VLAs with:
> 
>   error: variable-sized object may not be initialized
> 
> The replaced stack u8 arrays also do not guarantee native struct alignment
> for the aliased pointers.
> 
> Fixes: 19faf6fd969c ("vfio: selftests: Add a helper library for VFIO selftests")
> Fixes: 20face8c75ff ("vfio: selftests: Add helper to set/override a vf_token")
> Signed-off-by: Alex Mastro <amastro@fb.com>
> Assisted-by: Codex:gpt-5.5-high
> ---
>  tools/testing/selftests/vfio/lib/vfio_pci_device.c | 28 +++++++++++++---------
>  1 file changed, 17 insertions(+), 11 deletions(-)
> 
> diff --git a/tools/testing/selftests/vfio/lib/vfio_pci_device.c b/tools/testing/selftests/vfio/lib/vfio_pci_device.c
> index 94dc5fcecbeb..0b437435cce1 100644
> --- a/tools/testing/selftests/vfio/lib/vfio_pci_device.c
> +++ b/tools/testing/selftests/vfio/lib/vfio_pci_device.c
> @@ -30,13 +30,12 @@
>  static void vfio_pci_irq_set(struct vfio_pci_device *device,
>  			     u32 index, u32 vector, u32 count, int *fds)
>  {
> -	u8 buf[sizeof(struct vfio_irq_set) + sizeof(int) * count];
> -	struct vfio_irq_set *irq = (void *)&buf;
> -	int *irq_fds = (void *)&irq->data;
> +	struct vfio_irq_set *irq;
> +	size_t irq_size = sizeof(*irq) + sizeof(int) * count;

Nit: Can you change it to Reverse Fir Tree style? Same for the other
change.

Other than that this looks good. Thanks!

Tested-by: Vipin Sharma <vipinsh@google.com>
Reviewed-by: Vipin Sharma <vipinsh@google.com>

index 0b437435cce1..c70636c214bc 100644
--- a/tools/testing/selftests/vfio/lib/vfio_pci_device.c
+++ b/tools/testing/selftests/vfio/lib/vfio_pci_device.c
@@ -31,8 +31,9 @@ static void vfio_pci_irq_set(struct vfio_pci_device *device,
                             u32 index, u32 vector, u32 count, int *fds)
 {
        struct vfio_irq_set *irq;
-       size_t irq_size = sizeof(*irq) + sizeof(int) * count;
+       size_t irq_size;

+       irq_size = sizeof(*irq) + sizeof(int) * count;
        irq = calloc(1, irq_size);
        VFIO_ASSERT_NOT_NULL(irq);
        irq->argsz = irq_size;
@@ -119,9 +120,10 @@ static int vfio_device_feature_ioctl(int fd, u32 flags, void *data,
                                     size_t data_size)
 {
        struct vfio_device_feature *feature;
-       size_t feature_size = sizeof(*feature) + data_size;
+       size_t feature_size;
        int ret;

+       feature_size = sizeof(*feature) + data_size;
        feature = calloc(1, feature_size);
        VFIO_ASSERT_NOT_NULL(feature);
        memcpy(feature->data, data, data_size);


^ permalink raw reply related	[flat|nested] 3+ messages in thread

* Re: [PATCH] selftests/vfio: avoid VLAs
  2026-06-12 23:45 ` Vipin Sharma
@ 2026-06-15 15:19   ` Alex Mastro
  0 siblings, 0 replies; 3+ messages in thread
From: Alex Mastro @ 2026-06-15 15:19 UTC (permalink / raw)
  To: Vipin Sharma
  Cc: David Matlack, Alex Williamson, Shuah Khan,
	Raghavendra Rao Ananta, kvm, linux-kselftest, linux-kernel,
	Shuah Khan

On Fri, Jun 12, 2026 at 04:45:43PM -0700, Vipin Sharma wrote:
> Nit: Can you change it to Reverse Fir Tree style? Same for the other
> change.

Yep, no problem, thanks for reviewing!

^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2026-06-15 15:20 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-06-12 21:58 [PATCH] selftests/vfio: avoid VLAs Alex Mastro
2026-06-12 23:45 ` Vipin Sharma
2026-06-15 15:19   ` Alex Mastro

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox