Received: from mail-oo1-f74.google.com (mail-oo1-f74.google.com [209.85.161.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 BA8951DF75B for ; Wed, 4 Feb 2026 01:01:05 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.161.74 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1770166867; cv=none; b=kLhfK05zko2CMwaqZqxEE0Iq4wsbeBtzYFemoQGm0v6mBBRsd4VGPhO8ppKKxu/Y90FDo+kOoK9M9hPjl4AezwyGpvtZqWmMKaQ7OJU7cn3PMptk/2vjZNfr0AvBwjE9p/EiJKKifNr3XPp46b/8y03HIsLa6p1WSlHhSjzzX1M= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1770166867; c=relaxed/simple; bh=VFxQIZn4TYxXpKKCGjEzt9avNpb8gXQuzJhThGmBsVE=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=hjg9LtOK+1TVxfMgqoATkvD1IQfCMr7djMBF8E8ni4tGIyH4/rM65XSBUN5hei2OjYkhFV9u+ch56JyhEMr/1yy5kOwen2vwOP/foQZe2HTre74e+2JyCnki+7XZjJAQcMffBBBKVq1SQmjHSbLpSAtlFHBWgzVNHTO0TaYTaPI= 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=hGChZML4; arc=none smtp.client-ip=209.85.161.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="hGChZML4" Received: by mail-oo1-f74.google.com with SMTP id 006d021491bc7-663102ea953so18489508eaf.2 for ; Tue, 03 Feb 2026 17:01:05 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1770166865; x=1770771665; 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=nCv44Flx4qZkEtd63Kgnsoh9YeyaQDnrLAILkWlfP7I=; b=hGChZML4xv3/gx5L4KVhiwEznKYnxnmOoJebnBUCfVf1iSq7ZIZiOIBx3Kj0IZ2kK1 EXxG/88rsxzfcUWz45ufW5gHhm1FA0dPHi6tK/lyVkE9nm2t+U8r+Gjf29gl/7VrXAlY f7yeEqcFX3FYtX4VBe4B6hHVtyZxjqlJnyvhmHjKDYaYSQg70GSieBEj9n3v4NHFiFuM 3TPEb7tcheORT8HZE6AK0FHP+CCG2t6jgi9W8bxoCzy4NmF3CIeMRe+PBIAizS9fFkny vMw9DMaPtvHSUdxTRt8FglcoIPs0gaTMKIRX/I8/CIKwsnGWQkoVC8Mr7Ki6MG6OzHlt hPpQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1770166865; x=1770771665; 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=nCv44Flx4qZkEtd63Kgnsoh9YeyaQDnrLAILkWlfP7I=; b=R2NCcYHmnwNKnGdZ/cMYnk5qyAUxEMyl9yyn7Z2x+O/ZOitYUdTvq8J/hY2j3IwImR jkBUXO9HK40rlfUqRCJH9ipyDpbvUqCT7dnARO1Xqzw0p9JG5VlqyuVze+Z5IbFmc02R e5Ae5cdfu/MzNTbKiKlqEJKV4xmQGcPdiL6E0LNjK1qS5OPzxH4C9VFaU8KdIwMvFnKI 5eZbuS8PgIdpvcvk95LUSc5Tzi88u/mq3rulQqxWkC/bdd3f73lGGkUlipm3nsTOL9hE +LdrwZN5oXRMzlGswrHRcT18N/u44qsw0S7VS5k96CAIbn60v8Yo3Z7Igfv3CklgD9Qj 8D7Q== X-Forwarded-Encrypted: i=1; AJvYcCWHZJWtEBTYXKL+/LLWBWYGpixn42Rfit92JIIkagM5acufay96NZeGq1MbMTYwnNyOPOY=@vger.kernel.org X-Gm-Message-State: AOJu0Yy6PV80giZnFIovqc1XvqX+HmSudZ5oAxmqxUM8SdO/WsdHPI1X TLFEn36myzvdJl10GFA057hjuBgSDJkl5LamSbnWLDV8kpGJtFLGcTacapHR+VKeXUuJEs+ncNf iZ0upXLpQYg== X-Received: from jabio39.prod.google.com ([2002:a05:6638:8327:b0:5ca:f895:d02e]) (user=rananta job=prod-delivery.src-stubby-dispatcher) by 2002:a05:6820:138f:b0:663:170:3b with SMTP id 006d021491bc7-66a23043abdmr795256eaf.55.1770166864623; Tue, 03 Feb 2026 17:01:04 -0800 (PST) Date: Wed, 4 Feb 2026 01:00:51 +0000 In-Reply-To: <20260204010057.1079647-1-rananta@google.com> Precedence: bulk X-Mailing-List: kvm@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20260204010057.1079647-1-rananta@google.com> X-Mailer: git-send-email 2.53.0.rc2.204.g2597b5adb4-goog Message-ID: <20260204010057.1079647-3-rananta@google.com> Subject: [PATCH v3 2/8] vfio: selftests: Introduce snprintf_assert() From: Raghavendra Rao Ananta To: David Matlack , Alex Williamson , 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 f4ebd122d9b6..77b68c7129a6 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 13fdb4b0b10f..64a19481b734 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 5397822c3dd4..3a0bea5e2648 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 ecbb669b3765..723b56b485f6 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.rc2.204.g2597b5adb4-goog