firewire: add KUnit test to check layout of UAPI structures
authorTakashi Sakamoto <o-takashi@sakamocchi.jp>
Mon, 29 May 2023 23:12:39 +0000 (08:12 +0900)
committerTakashi Sakamoto <o-takashi@sakamocchi.jp>
Mon, 29 May 2023 23:12:39 +0000 (08:12 +0900)
In future commits, some new structure will be added to express new type
of event. They are exposed to user space as the part of UAPI. It is likely
to get trouble in ioctl compatibility layer for 32 bit binaries in 64 bit
host machine since the layout of structure could differ depending on
System V ABI for these architectures. Actually the subsystem already got
such trouble at v2.6.27. It is preferable to decide the layout of
structure carefully so that the layer is free from such trouble.

This commit utilizes KUnit framework to check the layout of structure for
the purpose. A test is added for the existent issue.

Cc: kunit-dev@googlegroups.com
Link: https://lore.kernel.org/r/20230529113406.986289-2-o-takashi@sakamocchi.jp
Signed-off-by: Takashi Sakamoto <o-takashi@sakamocchi.jp>
drivers/firewire/.kunitconfig [new file with mode: 0644]
drivers/firewire/Kconfig
drivers/firewire/Makefile
drivers/firewire/uapi-test.c [new file with mode: 0644]

diff --git a/drivers/firewire/.kunitconfig b/drivers/firewire/.kunitconfig
new file mode 100644 (file)
index 0000000..1599e06
--- /dev/null
@@ -0,0 +1,4 @@
+CONFIG_KUNIT=y
+CONFIG_PCI=y
+CONFIG_FIREWIRE=y
+CONFIG_FIREWIRE_KUNIT_UAPI_TEST=y
index ec00a6f..0a6596b 100644 (file)
@@ -18,6 +18,22 @@ config FIREWIRE
          To compile this driver as a module, say M here: the module will be
          called firewire-core.
 
+config FIREWIRE_KUNIT_UAPI_TEST
+       tristate "KUnit tests for layout of structure in UAPI" if !KUNIT_ALL_TESTS
+       depends on FIREWIRE && KUNIT
+       default KUNIT_ALL_TESTS
+       help
+         This builds the KUnit tests whether structures exposed to user
+         space have expected layout.
+
+         KUnit tests run during boot and output the results to the debug
+         log in TAP format (https://testanything.org/). Only useful for
+         kernel devs running KUnit test harness and are not for inclusion
+         into a production build.
+
+         For more information on KUnit and unit tests in general, refer
+         to the KUnit documentation in Documentation/dev-tools/kunit/.
+
 config FIREWIRE_OHCI
        tristate "OHCI-1394 controllers"
        depends on PCI && FIREWIRE && MMU
index e58c8c7..b24b287 100644 (file)
@@ -15,3 +15,6 @@ obj-$(CONFIG_FIREWIRE_SBP2) += firewire-sbp2.o
 obj-$(CONFIG_FIREWIRE_NET)  += firewire-net.o
 obj-$(CONFIG_FIREWIRE_NOSY) += nosy.o
 obj-$(CONFIG_PROVIDE_OHCI1394_DMA_INIT) += init_ohci1394_dma.o
+
+firewire-uapi-test-objs += uapi-test.o
+obj-$(CONFIG_FIREWIRE_KUNIT_UAPI_TEST) += firewire-uapi-test.o
diff --git a/drivers/firewire/uapi-test.c b/drivers/firewire/uapi-test.c
new file mode 100644 (file)
index 0000000..4dc633b
--- /dev/null
@@ -0,0 +1,38 @@
+// SPDX-License-Identifier: GPL-2.0-only
+//
+// uapi_test.c - An application of Kunit to check layout of structures exposed to user space for
+//              FireWire subsystem.
+//
+// Copyright (c) 2023 Takashi Sakamoto
+
+#include <kunit/test.h>
+#include <linux/firewire-cdev.h>
+
+// Known issue added at v2.6.27 kernel.
+static void structure_layout_event_response(struct kunit *test)
+{
+#if defined(CONFIG_X86_32)
+       // 4 bytes alignment for aggregate type including 8 bytes storage types.
+       KUNIT_EXPECT_EQ(test, 20, sizeof(struct fw_cdev_event_response));
+#else
+       // 8 bytes alignment for aggregate type including 8 bytes storage types.
+       KUNIT_EXPECT_EQ(test, 24, sizeof(struct fw_cdev_event_response));
+#endif
+
+       KUNIT_EXPECT_EQ(test, 0, offsetof(struct fw_cdev_event_response, closure));
+       KUNIT_EXPECT_EQ(test, 8, offsetof(struct fw_cdev_event_response, type));
+       KUNIT_EXPECT_EQ(test, 12, offsetof(struct fw_cdev_event_response, rcode));
+       KUNIT_EXPECT_EQ(test, 16, offsetof(struct fw_cdev_event_response, length));
+       KUNIT_EXPECT_EQ(test, 20, offsetof(struct fw_cdev_event_response, data));
+}
+
+static struct kunit_case structure_layout_test_cases[] = {
+       KUNIT_CASE(structure_layout_event_response),
+       {}
+};
+
+static struct kunit_suite structure_layout_test_suite = {
+       .name = "firewire-uapi-structure-layout",
+       .test_cases = structure_layout_test_cases,
+};
+kunit_test_suite(structure_layout_test_suite);