From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail-ot1-f74.google.com (mail-ot1-f74.google.com [209.85.210.74]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 7AB983A7830 for ; Tue, 24 Feb 2026 18:25:39 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.210.74 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1771957542; cv=none; b=VkzzpxOQnZhOANSqgnRSbajjWMm54AWhZAkPNdB93mU4neRwkwjLRPUCVQGzIwjzE1x5ekKUhRgdG83ZBSjRkAN/F7C7px/adNmP+7Q4TF9RYhrks/t8veulmj4IRZJEOh7HTAB6x3/ebqIA9P3FXOT4JbWbrxbiEbl8dXlWwuU= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1771957542; c=relaxed/simple; bh=c3RxJ5NfBDnbTVGAVuqYFGnYsYR7E9Z82BqrkHaXlc0=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=mdI4DKt3g4cFPRayx1cktuF/7bVO2ZGf6p2QMJ+gb++q4Mbwwg8ozlGDdEI+IoEMxgKtOPvUB2Slsg1QUa3eeAbrRjeci5u5Hb6Vr/Gkds1wvY2rGHl1Unb+QhcpweoaJPuNxQQI0sYO0dcTalloFWP4SI5pUBaHmTuUm8XRpok= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--rananta.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=sM/VPDFS; arc=none smtp.client-ip=209.85.210.74 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--rananta.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="sM/VPDFS" Received: by mail-ot1-f74.google.com with SMTP id 46e09a7af769-7d18fa0728aso63150988a34.0 for ; Tue, 24 Feb 2026 10:25:39 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1771957538; x=1772562338; darn=vger.kernel.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=W43LUhh0LTU1//BOJUSbLb590wyjt02CHFj96YxulfY=; b=sM/VPDFS1LI5zqRMh2LUmqqBHKCnOaFjdC3i7m6GTWjXFLwKwYosYGxepJ/iJcaHF2 LPvfIOi1IktEkaDfp8E0SOPoVmimPa3TDG5g0Ucz0SLTEXs2d7bSSD+yVrw6fUgfICFm wXoy0bDKaGkrTYcdopYZP4jM8kFbqk6OkVg8KxXaY2lbX35YNhrhtCR2svKkRFgBX0vZ d1mX+jm/W+4Jj5kr9tX1OegzBV9zkiFwqF/5YY5GKW2e098qc/ZFN+VBD5945ItjIX1B fzfJxJ87RKCMpiP4o7zlFxoIbm/2loZhGCrs/rFHKNzIGIRe0BGhCyk000W2z+3Hywk9 mhOw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1771957538; x=1772562338; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=W43LUhh0LTU1//BOJUSbLb590wyjt02CHFj96YxulfY=; b=TyOF+L54lO/HpC8KRsz+bu/PkEzxf5esap7KJ3WTrfo9Gp/zeHnb1HshHdOzmAnPXN J8KREyqQOPyfrIDd89w1LMmBZK7Noa80aPtkwya032RtPx8tN80nT1WBOXnedVXXHgHN vIK7OJMYIkzFflFtwTGTUsEC8Qx8zVC/KhRT+vOrsAlLkejRqHfu1ImHTSytJ7oRnQAu bYcQgf0EjFzKLX2KdK/3d5dwbNN35gQ5RkLeHcGGjGmBfwjWS0Gu/AY0TeyQmGm1mpMm sWMoqCRHz0YRT04jahenrGw2YvxExw0SEMHZarPlHqcU4+q9FtKbxleMMiZTmWQtcHW+ gTWw== X-Forwarded-Encrypted: i=1; AJvYcCVgs3O1QtTF95UXVBrXPNetYiufbuT/WmTbThubKBM8lJciNTScsBFGJKCIKUj5HQQCYKmYKOMfhd2yEgs=@vger.kernel.org X-Gm-Message-State: AOJu0YzJfIfE7Pq2DJZDtIgf0kPz4c0e6Tv3GfdT6CwLVxhfeav72WQ0 5bhH/JO0MzCQRHzoR4R2RLHlPwzXBec0R1os1YxoP4ASwaeVWK+xv2PIISJDSTIJt6K1NDm5BxG eXJkIreHaAQ== X-Received: from jabl3.prod.google.com ([2002:a05:6638:c383:b0:5cb:752:3749]) (user=rananta job=prod-delivery.src-stubby-dispatcher) by 2002:a05:6820:4dc6:b0:662:f2ea:8f9b with SMTP id 006d021491bc7-679c428264bmr7129729eaf.20.1771957538427; Tue, 24 Feb 2026 10:25:38 -0800 (PST) Date: Tue, 24 Feb 2026 18:25:26 +0000 In-Reply-To: <20260224182532.3914470-1-rananta@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20260224182532.3914470-1-rananta@google.com> X-Mailer: git-send-email 2.53.0.414.gf7e9f6c205-goog Message-ID: <20260224182532.3914470-3-rananta@google.com> Subject: [PATCH v4 2/8] vfio: selftests: Introduce snprintf_assert() From: Raghavendra Rao Ananta To: David Matlack , Alex Williamson Cc: Vipin Sharma , Josh Hilke , kvm@vger.kernel.org, linux-kernel@vger.kernel.org, Raghavendra Rao Ananta Content-Type: text/plain; charset="UTF-8" Introduce snprintf_assert() to protect the users of snprintf() to fail if the requested operation was truncated due to buffer limits. VFIO tests and libraries, including a new sysfs library that will be introduced by an upcoming patch, rely quite heavily on snprintf()s to build PCI sysfs paths. Having a protection against this will be helpful to prevent false test failures. Signed-off-by: Raghavendra Rao Ananta --- .../vfio/lib/include/libvfio/assert.h | 5 +++++ .../selftests/vfio/lib/vfio_pci_device.c | 8 +++---- .../selftests/vfio/vfio_dma_mapping_test.c | 6 +++--- .../selftests/vfio/vfio_pci_device_test.c | 21 ++++++++++--------- 4 files changed, 23 insertions(+), 17 deletions(-) diff --git a/tools/testing/selftests/vfio/lib/include/libvfio/assert.h b/tools/testing/selftests/vfio/lib/include/libvfio/assert.h index f4ebd122d9b6f..77b68c7129a64 100644 --- a/tools/testing/selftests/vfio/lib/include/libvfio/assert.h +++ b/tools/testing/selftests/vfio/lib/include/libvfio/assert.h @@ -51,4 +51,9 @@ VFIO_ASSERT_EQ(__ret, 0, "ioctl(%s, %s, %s) returned %d\n", #_fd, #_op, #_arg, __ret); \ } while (0) +#define snprintf_assert(_s, _size, _fmt, ...) do { \ + int __ret = snprintf(_s, _size, _fmt, ##__VA_ARGS__); \ + VFIO_ASSERT_LT(__ret, _size); \ +} while (0) + #endif /* SELFTESTS_VFIO_LIB_INCLUDE_LIBVFIO_ASSERT_H */ diff --git a/tools/testing/selftests/vfio/lib/vfio_pci_device.c b/tools/testing/selftests/vfio/lib/vfio_pci_device.c index 13fdb4b0b10f3..64a19481b734f 100644 --- a/tools/testing/selftests/vfio/lib/vfio_pci_device.c +++ b/tools/testing/selftests/vfio/lib/vfio_pci_device.c @@ -188,7 +188,7 @@ static unsigned int vfio_pci_get_group_from_dev(const char *bdf) unsigned int group; int ret; - snprintf(sysfs_path, PATH_MAX, "%s/%s/iommu_group", PCI_SYSFS_PATH, bdf); + snprintf_assert(sysfs_path, PATH_MAX, "%s/%s/iommu_group", PCI_SYSFS_PATH, bdf); ret = readlink(sysfs_path, dev_iommu_group_path, sizeof(dev_iommu_group_path)); VFIO_ASSERT_NE(ret, -1, "Failed to get the IOMMU group for device: %s\n", bdf); @@ -208,7 +208,7 @@ static void vfio_pci_group_setup(struct vfio_pci_device *device, const char *bdf int group; group = vfio_pci_get_group_from_dev(bdf); - snprintf(group_path, sizeof(group_path), "/dev/vfio/%d", group); + snprintf_assert(group_path, sizeof(group_path), "/dev/vfio/%d", group); device->group_fd = open(group_path, O_RDWR); VFIO_ASSERT_GE(device->group_fd, 0, "open(%s) failed\n", group_path); @@ -279,7 +279,7 @@ const char *vfio_pci_get_cdev_path(const char *bdf) cdev_path = calloc(PATH_MAX, 1); VFIO_ASSERT_NOT_NULL(cdev_path); - snprintf(dir_path, sizeof(dir_path), "/sys/bus/pci/devices/%s/vfio-dev/", bdf); + snprintf_assert(dir_path, sizeof(dir_path), "/sys/bus/pci/devices/%s/vfio-dev/", bdf); dir = opendir(dir_path); VFIO_ASSERT_NOT_NULL(dir, "Failed to open directory %s\n", dir_path); @@ -289,7 +289,7 @@ const char *vfio_pci_get_cdev_path(const char *bdf) if (strncmp("vfio", entry->d_name, 4)) continue; - snprintf(cdev_path, PATH_MAX, "/dev/vfio/devices/%s", entry->d_name); + snprintf_assert(cdev_path, PATH_MAX, "/dev/vfio/devices/%s", entry->d_name); break; } diff --git a/tools/testing/selftests/vfio/vfio_dma_mapping_test.c b/tools/testing/selftests/vfio/vfio_dma_mapping_test.c index 5397822c3dd4b..3a0bea5e26480 100644 --- a/tools/testing/selftests/vfio/vfio_dma_mapping_test.c +++ b/tools/testing/selftests/vfio/vfio_dma_mapping_test.c @@ -45,9 +45,9 @@ static int intel_iommu_mapping_get(const char *bdf, u64 iova, FILE *file; char *rest; - snprintf(iommu_mapping_path, sizeof(iommu_mapping_path), - "/sys/kernel/debug/iommu/intel/%s/domain_translation_struct", - bdf); + snprintf_assert(iommu_mapping_path, sizeof(iommu_mapping_path), + "/sys/kernel/debug/iommu/intel/%s/domain_translation_struct", + bdf); printf("Searching for IOVA 0x%lx in %s\n", iova, iommu_mapping_path); diff --git a/tools/testing/selftests/vfio/vfio_pci_device_test.c b/tools/testing/selftests/vfio/vfio_pci_device_test.c index ecbb669b37651..723b56b485f67 100644 --- a/tools/testing/selftests/vfio/vfio_pci_device_test.c +++ b/tools/testing/selftests/vfio/vfio_pci_device_test.c @@ -39,16 +39,17 @@ FIXTURE_TEARDOWN(vfio_pci_device_test) iommu_cleanup(self->iommu); } -#define read_pci_id_from_sysfs(_file) ({ \ - char __sysfs_path[PATH_MAX]; \ - char __buf[32]; \ - int __fd; \ - \ - snprintf(__sysfs_path, PATH_MAX, "/sys/bus/pci/devices/%s/%s", device_bdf, _file); \ - ASSERT_GT((__fd = open(__sysfs_path, O_RDONLY)), 0); \ - ASSERT_GT(read(__fd, __buf, ARRAY_SIZE(__buf)), 0); \ - ASSERT_EQ(0, close(__fd)); \ - (u16)strtoul(__buf, NULL, 0); \ +#define read_pci_id_from_sysfs(_file) ({ \ + char __sysfs_path[PATH_MAX]; \ + char __buf[32]; \ + int __fd; \ + \ + snprintf_assert(__sysfs_path, PATH_MAX, "/sys/bus/pci/devices/%s/%s", \ + device_bdf, _file); \ + ASSERT_GT((__fd = open(__sysfs_path, O_RDONLY)), 0); \ + ASSERT_GT(read(__fd, __buf, ARRAY_SIZE(__buf)), 0); \ + ASSERT_EQ(0, close(__fd)); \ + (u16)strtoul(__buf, NULL, 0); \ }) TEST_F(vfio_pci_device_test, config_space_read_write) -- 2.53.0.414.gf7e9f6c205-goog