Skip to contentSkip to conversation

selftests: iommu: suppress -Warray-bounds in _test_cmd_get_hw_info

PATCH In review Eva Kurchatova 2 participants 4 messages Sep 8 – Sep 9

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH 1/2 selftests: iommu: suppress -Warray-bounds in _test_cmd_get_hw_info

In function '_test_cmd_get_hw_info',
inlined from 'iommufd_ioas_get_hw_info' at iommufd.c:614:3, inlined from 'wrapper_iommufd_ioas_get_hw_info' at iommufd.c:589:1:
iommufd_utils.h:683:37: warning: array subscript 'struct iommu_test_hw_info[0]' is partly outside array bounds of 'struct iommu_test_hw_info_buffer_smaller[1]' [-Warray-bounds=]

    683 |                         assert(!info->flags);
        |                                 ~~~~^~~~~~~

The test intentionally passes a smaller buffer to exercise the kernel's handling of undersized user buffers. Runtime data_len checks make the access safe, but GCC cannot prove this statically. Suppress the warning locally with a pragma.

---
 tools/testing/selftests/iommu/iommufd_utils.h | 7 +++++++
 1 file changed, 7 insertions(+)
tools/testing/selftests/iommu/iommufd_utils.h +7 -0
index b4928cbd4d9c..c4dfa05e0612 100644
@@ -866,12 +866,19 @@ static int _test_cmd_get_hw_info(int fd, __u32 device_id, __u32 data_type,
 		}
 	}
 
+	/*
+	 * When data_len is smaller than sizeof(*info), GCC warns about
+	 * out-of-bounds access.  The runtime data_len checks make this safe.
+	 */
+#pragma GCC diagnostic push
+#pragma GCC diagnostic ignored "-Warray-bounds"
 	if (info) {
 		if (data_len >= offsetofend(struct iommu_test_hw_info, test_reg))
 			assert(info->test_reg == IOMMU_HW_INFO_SELFTEST_REGVAL);
 		if (data_len >= offsetofend(struct iommu_test_hw_info, flags))
 			assert(!info->flags);
 	}
+#pragma GCC diagnostic pop
 
 	if (max_pasid)
 		*max_pasid = cmd.out_max_pasid_log2;
Signature · 1 line
2.55.0
#1 lore raw

PATCH 2/2 selftests: iommu: skip when the mock device is not there

Both iommufd tests need the mock device the iommufd module registers under CONFIG_IOMMUFD_TEST, and they open /dev/iommu in every fixture without checking, so on a kernel that does not provide it every case fails on the open.

Checking the open is not enough on its own. /dev/iommu appears as soon as the iommufd module is loaded, which says nothing about the mock
device: the release config here is CONFIG_IOMMUFD=m with CONFIG_IOMMUFD_TEST unset, so the open succeeds and the tests fail later in the ioctls that drive the mock device instead. Only the debug config sets CONFIG_IOMMUFD_TEST.

/sys/bus/iommufd_mock covers both, it exists only once the module is loaded and the kernel was built with CONFIG_IOMMUFD_TEST. Check for it once before handing over to the harness, rather than in each of the fixtures, and report a skip. test_harness_run() is what TEST_HARNESS_MAIN calls anyway, so the tests still run exactly as they did.

---
 tools/testing/selftests/iommu/iommufd.c          | 15 ++++++++++++++-
 tools/testing/selftests/iommu/iommufd_fail_nth.c | 15 ++++++++++++++-
 2 files changed, 28 insertions(+), 2 deletions(-)
tools/testing/selftests/iommu/iommufd.c +14 -1
index d44b34b05757..446306badcdc 100644
@@ -3613,4 +3613,17 @@ TEST_F(iommufd_device_pasid, pasid_attach)
 	test_cmd_mock_domain_replace(self->stdev_id, self->ioas_id);
 }
 
-TEST_HARNESS_MAIN
+static bool iommufd_mock_available(void)
+{
+	return access("/sys/bus/iommufd_mock", F_OK) == 0;
+}
+
+int main(int argc, char **argv)
+{
+	if (!iommufd_mock_available())
+		ksft_exit_skip("no iommufd mock device, the tests need "
+			       "CONFIG_IOMMUFD_TEST=y and the iommufd "
+			       "module loaded\n");
+
+	return test_harness_run(argc, argv);
+}
tools/testing/selftests/iommu/iommufd_fail_nth.c +14 -1
index 25495d8dceb3..c516e00f7316 100644
@@ -745,4 +745,17 @@ TEST_FAIL_NTH(basic_fail_nth, device)
 	return 0;
 }
 
-TEST_HARNESS_MAIN
+static bool iommufd_mock_available(void)
+{
+	return access("/sys/bus/iommufd_mock", F_OK) == 0;
+}
+
+int main(int argc, char **argv)
+{
+	if (!iommufd_mock_available())
+		ksft_exit_skip("no iommufd mock device, the tests need "
+			       "CONFIG_IOMMUFD_TEST=y and the iommufd "
+			       "module loaded\n");
+
+	return test_harness_run(argc, argv);
+}
Signature · 1 line
2.55.0
maintainer
#2 lore raw

Re: [PATCH 2/2] selftests: iommu: skip when the mock device is not there

Quoted 18 lines · Eva Kurchatova

Both iommufd tests need the mock device the iommufd module registers under CONFIG_IOMMUFD_TEST, and they open /dev/iommu in every fixture without checking, so on a kernel that does not provide it every case fails on the open.

Checking the open is not enough on its own. /dev/iommu appears as soon as the iommufd module is loaded, which says nothing about the mock
device: the release config here is CONFIG_IOMMUFD=m with CONFIG_IOMMUFD_TEST unset, so the open succeeds and the tests fail later in the ioctls that drive the mock device instead. Only the debug config sets CONFIG_IOMMUFD_TEST.

/sys/bus/iommufd_mock covers both, it exists only once the module is loaded and the kernel was built with CONFIG_IOMMUFD_TEST. Check for it once before handing over to the harness, rather than in each of the fixtures, and report a skip. test_harness_run() is what TEST_HARNESS_MAIN calls anyway, so the tests still run exactly as they did.

Quoted 5 lines
---
 tools/testing/selftests/iommu/iommufd.c          | 15 ++++++++++++++-
 tools/testing/selftests/iommu/iommufd_fail_nth.c | 15 ++++++++++++++-
 2 files changed, 28 insertions(+), 2 deletions(-)

Applied thanks

Jason

maintainer
#3 lore raw
In reply to Eva Kurchatova
Quoted 6 lines

In function '_test_cmd_get_hw_info',
inlined from 'iommufd_ioas_get_hw_info' at iommufd.c:614:3, inlined from 'wrapper_iommufd_ioas_get_hw_info' at iommufd.c:589:1:
iommufd_utils.h:683:37: warning: array subscript 'struct iommu_test_hw_info[0]' is partly outside array bounds of 'struct iommu_test_hw_info_buffer_smaller[1]' [-Warray-bounds=]

?? My tree doesn't have anything like iommu_test_hw_info_buffer_smaller ? And all these line numbers don't match mine.

How are you triggering the warning? The struct memory is properly sized to do this:

Quoted 1 line
    683 |                         assert(!info->flags);

I prefer not to have the #pragma things, I don't think there should be a reason for them here.

Signature · 1 line
Jason