vfio: selftests: Add allocation assert helpers

Add {malloc,calloc}_assert() helpers alongside the existing *_assert()
helpers.

Use them for VFIO selftest allocations that immediately assert a
non-NULL result.

Assisted-by: Codex:gpt-5.5-high
Signed-off-by: Alex Mastro <amastro@fb.com>
Reviewed-by: David Matlack <dmatlack@google.com>
Link: https://lore.kernel.org/r/20260617-scratch-amastro-vfio-selftests-avoid-vlas-v4-1-b9f52f1e2c5a@fb.com
Signed-off-by: Alex Williamson <alex@shazbot.org>
This commit is contained in:
Alex Mastro 2026-06-17 11:58:00 -07:00 committed by Alex Williamson
parent 27fffdfba3
commit bc4be8c6fd
6 changed files with 28 additions and 19 deletions

View File

@ -3,6 +3,7 @@
#define SELFTESTS_VFIO_LIB_INCLUDE_LIBVFIO_ASSERT_H
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/ioctl.h>
@ -45,6 +46,23 @@
VFIO_LOG_AND_EXIT(_fmt, ##__VA_ARGS__); \
} while (0)
#define malloc_assert(_size) ({ \
size_t __size = (_size); \
void *__ptr = malloc(__size); \
VFIO_ASSERT_NOT_NULL(__ptr, "malloc(%zu) failed", \
__size); \
__ptr; \
})
#define calloc_assert(_nmemb, _size) ({ \
size_t __nmemb = (_nmemb); \
size_t __size = (_size); \
void *__ptr = calloc(__nmemb, __size); \
VFIO_ASSERT_NOT_NULL(__ptr, "calloc(%zu, %zu) failed", \
__nmemb, __size); \
__ptr; \
})
#define ioctl_assert(_fd, _op, _arg) do { \
void *__arg = (_arg); \
int __ret = ioctl((_fd), (_op), (__arg)); \

View File

@ -286,8 +286,7 @@ static struct vfio_iommu_type1_info *vfio_iommu_get_info(int container_fd)
{
struct vfio_iommu_type1_info *info;
info = malloc(sizeof(*info));
VFIO_ASSERT_NOT_NULL(info);
info = malloc_assert(sizeof(*info));
*info = (struct vfio_iommu_type1_info) {
.argsz = sizeof(*info),
@ -324,8 +323,7 @@ static struct iommu_iova_range *vfio_iommu_iova_ranges(struct iommu *iommu,
cap_range = container_of(hdr, struct vfio_iommu_type1_info_cap_iova_range, header);
VFIO_ASSERT_GT(cap_range->nr_iovas, 0);
ranges = calloc(cap_range->nr_iovas, sizeof(*ranges));
VFIO_ASSERT_NOT_NULL(ranges);
ranges = calloc_assert(cap_range->nr_iovas, sizeof(*ranges));
for (u32 i = 0; i < cap_range->nr_iovas; i++) {
ranges[i] = (struct iommu_iova_range){
@ -357,8 +355,7 @@ static struct iommu_iova_range *iommufd_iova_ranges(struct iommu *iommu,
VFIO_ASSERT_EQ(errno, EMSGSIZE);
VFIO_ASSERT_GT(query.num_iovas, 0);
ranges = calloc(query.num_iovas, sizeof(*ranges));
VFIO_ASSERT_NOT_NULL(ranges);
ranges = calloc_assert(query.num_iovas, sizeof(*ranges));
query.allowed_iovas = (uintptr_t)ranges;
@ -424,8 +421,7 @@ struct iommu *iommu_init(const char *iommu_mode)
struct iommu *iommu;
int version;
iommu = calloc(1, sizeof(*iommu));
VFIO_ASSERT_NOT_NULL(iommu);
iommu = calloc_assert(1, sizeof(*iommu));
INIT_LIST_HEAD(&iommu->dma_regions);

View File

@ -29,8 +29,7 @@ struct iova_allocator *iova_allocator_init(struct iommu *iommu)
ranges = iommu_iova_ranges(iommu, &nranges);
VFIO_ASSERT_NOT_NULL(ranges);
allocator = malloc(sizeof(*allocator));
VFIO_ASSERT_NOT_NULL(allocator);
allocator = malloc_assert(sizeof(*allocator));
*allocator = (struct iova_allocator){
.ranges = ranges,
@ -90,4 +89,3 @@ iova_t iova_allocator_alloc(struct iova_allocator *allocator, size_t size)
allocator->range_offset = 0;
}
}

View File

@ -107,8 +107,7 @@ char *sysfs_sriov_vf_bdf_get(const char *pf_bdf, int i)
char *out_vf_bdf;
/* Fit "0000:00:00.0" */
out_vf_bdf = calloc(16, sizeof(char));
VFIO_ASSERT_NOT_NULL(out_vf_bdf);
out_vf_bdf = calloc_assert(16, sizeof(char));
snprintf_assert(path, PATH_MAX, "/sys/bus/pci/devices/%s/virtfn%d", pf_bdf, i);
readlink_base(path, "%s", out_vf_bdf);

View File

@ -343,8 +343,7 @@ const char *vfio_pci_get_cdev_path(const char *bdf)
char *cdev_path;
DIR *dir;
cdev_path = calloc(PATH_MAX, 1);
VFIO_ASSERT_NOT_NULL(cdev_path);
cdev_path = calloc_assert(PATH_MAX, 1);
snprintf_assert(dir_path, sizeof(dir_path), "/sys/bus/pci/devices/%s/vfio-dev/", bdf);
@ -425,8 +424,7 @@ struct vfio_pci_device *vfio_pci_device_alloc(const char *bdf, struct iommu *iom
{
struct vfio_pci_device *device;
device = calloc(1, sizeof(*device));
VFIO_ASSERT_NOT_NULL(device);
device = calloc_assert(1, sizeof(*device));
VFIO_ASSERT_NOT_NULL(iommu);
device->iommu = iommu;

View File

@ -45,8 +45,8 @@ FIXTURE_SETUP(vfio_pci_device_init_perf_test)
int i;
self->iommu = iommu_init(variant->iommu_mode);
self->threads = calloc(nr_devices, sizeof(self->threads[0]));
self->thread_args = calloc(nr_devices, sizeof(self->thread_args[0]));
self->threads = calloc_assert(nr_devices, sizeof(self->threads[0]));
self->thread_args = calloc_assert(nr_devices, sizeof(self->thread_args[0]));
pthread_barrier_init(&self->barrier, NULL, nr_devices);