Skip to content

Commit dc7c516

Browse files
committed
firewire: add KUnit test to check layout of UAPI structures
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>
1 parent 44c026a commit dc7c516

4 files changed

Lines changed: 61 additions & 0 deletions

File tree

drivers/firewire/.kunitconfig

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
CONFIG_KUNIT=y
2+
CONFIG_PCI=y
3+
CONFIG_FIREWIRE=y
4+
CONFIG_FIREWIRE_KUNIT_UAPI_TEST=y

drivers/firewire/Kconfig

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,22 @@ config FIREWIRE
1818
To compile this driver as a module, say M here: the module will be
1919
called firewire-core.
2020

21+
config FIREWIRE_KUNIT_UAPI_TEST
22+
tristate "KUnit tests for layout of structure in UAPI" if !KUNIT_ALL_TESTS
23+
depends on FIREWIRE && KUNIT
24+
default KUNIT_ALL_TESTS
25+
help
26+
This builds the KUnit tests whether structures exposed to user
27+
space have expected layout.
28+
29+
KUnit tests run during boot and output the results to the debug
30+
log in TAP format (https://testanything.org/). Only useful for
31+
kernel devs running KUnit test harness and are not for inclusion
32+
into a production build.
33+
34+
For more information on KUnit and unit tests in general, refer
35+
to the KUnit documentation in Documentation/dev-tools/kunit/.
36+
2137
config FIREWIRE_OHCI
2238
tristate "OHCI-1394 controllers"
2339
depends on PCI && FIREWIRE && MMU

drivers/firewire/Makefile

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,3 +15,6 @@ obj-$(CONFIG_FIREWIRE_SBP2) += firewire-sbp2.o
1515
obj-$(CONFIG_FIREWIRE_NET) += firewire-net.o
1616
obj-$(CONFIG_FIREWIRE_NOSY) += nosy.o
1717
obj-$(CONFIG_PROVIDE_OHCI1394_DMA_INIT) += init_ohci1394_dma.o
18+
19+
firewire-uapi-test-objs += uapi-test.o
20+
obj-$(CONFIG_FIREWIRE_KUNIT_UAPI_TEST) += firewire-uapi-test.o

drivers/firewire/uapi-test.c

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
// SPDX-License-Identifier: GPL-2.0-only
2+
//
3+
// uapi_test.c - An application of Kunit to check layout of structures exposed to user space for
4+
// FireWire subsystem.
5+
//
6+
// Copyright (c) 2023 Takashi Sakamoto
7+
8+
#include <kunit/test.h>
9+
#include <linux/firewire-cdev.h>
10+
11+
// Known issue added at v2.6.27 kernel.
12+
static void structure_layout_event_response(struct kunit *test)
13+
{
14+
#if defined(CONFIG_X86_32)
15+
// 4 bytes alignment for aggregate type including 8 bytes storage types.
16+
KUNIT_EXPECT_EQ(test, 20, sizeof(struct fw_cdev_event_response));
17+
#else
18+
// 8 bytes alignment for aggregate type including 8 bytes storage types.
19+
KUNIT_EXPECT_EQ(test, 24, sizeof(struct fw_cdev_event_response));
20+
#endif
21+
22+
KUNIT_EXPECT_EQ(test, 0, offsetof(struct fw_cdev_event_response, closure));
23+
KUNIT_EXPECT_EQ(test, 8, offsetof(struct fw_cdev_event_response, type));
24+
KUNIT_EXPECT_EQ(test, 12, offsetof(struct fw_cdev_event_response, rcode));
25+
KUNIT_EXPECT_EQ(test, 16, offsetof(struct fw_cdev_event_response, length));
26+
KUNIT_EXPECT_EQ(test, 20, offsetof(struct fw_cdev_event_response, data));
27+
}
28+
29+
static struct kunit_case structure_layout_test_cases[] = {
30+
KUNIT_CASE(structure_layout_event_response),
31+
{}
32+
};
33+
34+
static struct kunit_suite structure_layout_test_suite = {
35+
.name = "firewire-uapi-structure-layout",
36+
.test_cases = structure_layout_test_cases,
37+
};
38+
kunit_test_suite(structure_layout_test_suite);

0 commit comments

Comments
 (0)