Skip to content

Commit d6fc9fc

Browse files
committed
kbuild: compile-test exported headers to ensure they are self-contained
Multiple people have suggested compile-testing UAPI headers to ensure they can be really included from user-space. "make headers_check" is obviously not enough to catch bugs, and we often leak unresolved references to user-space. Use the new header-test-y syntax to implement it. Please note exported headers are compile-tested with a completely different set of compiler flags. The header search path is set to $(objtree)/usr/include since exported headers should not include unexported ones. We use -std=gnu89 for the kernel space since the kernel code highly depends on GNU extensions. On the other hand, UAPI headers should be written in more standardized C, so they are compiled with -std=c90. This will emit errors if C++ style comments, the keyword 'inline', etc. are used. Please use C style comments (/* ... */), '__inline__', etc. in UAPI headers. There is additional compiler requirement to enable this test because many of UAPI headers include <stdlib.h>, <sys/ioctl.h>, <sys/time.h>, etc. directly or indirectly. You cannot use kernel.org pre-built toolchains [1] since they lack <stdlib.h>. I reused CONFIG_CC_CAN_LINK to check the system header availability. The intention is slightly different, but a compiler that can link userspace programs provide system headers. For now, a lot of headers need to be excluded because they cannot be compiled standalone, but this is a good start point. [1] https://mirrors.edge.kernel.org/pub/tools/crosstool/index.html Signed-off-by: Masahiro Yamada <yamada.masahiro@socionext.com> Reviewed-by: Sam Ravnborg <sam@ravnborg.org>
1 parent 1a927fd commit d6fc9fc

6 files changed

Lines changed: 148 additions & 2 deletions

File tree

‎Makefile‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1378,7 +1378,7 @@ CLEAN_DIRS += $(MODVERDIR) include/ksym
13781378
CLEAN_FILES += modules.builtin.modinfo
13791379

13801380
# Directories & files removed with 'make mrproper'
1381-
MRPROPER_DIRS += include/config usr/include include/generated \
1381+
MRPROPER_DIRS += include/config include/generated \
13821382
arch/$(SRCARCH)/include/generated .tmp_objdiff
13831383
MRPROPER_FILES += .config .config.old .version \
13841384
Module.symvers tags TAGS cscope* GPATH GTAGS GRTAGS GSYMS \

‎init/Kconfig‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,16 @@ config HEADER_TEST
108108
If you are a developer or tester and want to ensure the requested
109109
headers are self-contained, say Y here. Otherwise, choose N.
110110

111+
config UAPI_HEADER_TEST
112+
bool "Compile test UAPI headers"
113+
depends on HEADER_TEST && HEADERS_INSTALL && CC_CAN_LINK
114+
help
115+
Compile test headers exported to user-space to ensure they are
116+
self-contained, i.e. compilable as standalone units.
117+
118+
If you are a developer or tester and want to ensure the exported
119+
headers are self-contained, say Y here. Otherwise, choose N.
120+
111121
config LOCALVERSION
112122
string "Local version - append to kernel release"
113123
help

‎usr/.gitignore‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,3 @@ initramfs_data.cpio.gz
77
initramfs_data.cpio.bz2
88
initramfs_data.cpio.lzma
99
initramfs_list
10-
include

‎usr/Makefile‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,3 +56,5 @@ $(deps_initramfs): klibcdirs
5656
$(obj)/$(datafile_y): $(obj)/gen_init_cpio $(deps_initramfs) klibcdirs
5757
$(Q)$(initramfs) -l $(ramfs-input) > $(obj)/$(datafile_d_y)
5858
$(call if_changed,initfs)
59+
60+
subdir-$(CONFIG_UAPI_HEADER_TEST) += include

‎usr/include/.gitignore‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
*
2+
!.gitignore
3+
!Makefile

‎usr/include/Makefile‎

Lines changed: 132 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,132 @@
1+
# SPDX-License-Identifier: GPL-2.0-only
2+
3+
# Unlike the kernel space, exported headers are written in standard C.
4+
# - Forbid C++ style comments
5+
# - Use '__inline__', '__asm__' instead of 'inline', 'asm'
6+
#
7+
# -std=c90 (equivalent to -ansi) catches the violation of those.
8+
# We cannot go as far as adding -Wpedantic since it emits too many warnings.
9+
UAPI_CFLAGS := -std=c90 -Wall -Werror=implicit-function-declaration
10+
11+
override c_flags = $(UAPI_CFLAGS) -Wp,-MD,$(depfile) -I$(objtree)/usr/include
12+
13+
# The following are excluded for now because they fail to build.
14+
#
15+
# Do not add a new header to the blacklist without legitimate reason.
16+
# Please consider to fix the header first.
17+
#
18+
# Sorted alphabetically.
19+
header-test- += asm/ipcbuf.h
20+
header-test- += asm/msgbuf.h
21+
header-test- += asm/sembuf.h
22+
header-test- += asm/shmbuf.h
23+
header-test- += asm/signal.h
24+
header-test- += asm/ucontext.h
25+
header-test- += drm/vmwgfx_drm.h
26+
header-test- += linux/am437x-vpfe.h
27+
header-test- += linux/android/binder.h
28+
header-test- += linux/android/binderfs.h
29+
header-test-$(CONFIG_CPU_BIG_ENDIAN) += linux/byteorder/big_endian.h
30+
header-test-$(CONFIG_CPU_LITTLE_ENDIAN) += linux/byteorder/little_endian.h
31+
header-test- += linux/coda.h
32+
header-test- += linux/coda_psdev.h
33+
header-test- += linux/dvb/audio.h
34+
header-test- += linux/dvb/osd.h
35+
header-test- += linux/elfcore.h
36+
header-test- += linux/errqueue.h
37+
header-test- += linux/fsmap.h
38+
header-test- += linux/hdlc/ioctl.h
39+
header-test- += linux/ivtv.h
40+
header-test- += linux/jffs2.h
41+
header-test- += linux/kexec.h
42+
header-test- += linux/matroxfb.h
43+
header-test- += linux/netfilter_bridge/ebtables.h
44+
header-test- += linux/netfilter_ipv4/ipt_LOG.h
45+
header-test- += linux/netfilter_ipv6/ip6t_LOG.h
46+
header-test- += linux/nfc.h
47+
header-test- += linux/nilfs2_ondisk.h
48+
header-test- += linux/omap3isp.h
49+
header-test- += linux/omapfb.h
50+
header-test- += linux/patchkey.h
51+
header-test- += linux/phonet.h
52+
header-test- += linux/reiserfs_xattr.h
53+
header-test- += linux/scc.h
54+
header-test- += linux/sctp.h
55+
header-test- += linux/signal.h
56+
header-test- += linux/sysctl.h
57+
header-test- += linux/usb/audio.h
58+
header-test- += linux/v4l2-mediabus.h
59+
header-test- += linux/v4l2-subdev.h
60+
header-test- += linux/videodev2.h
61+
header-test- += linux/vm_sockets.h
62+
header-test- += misc/ocxl.h
63+
header-test- += mtd/mtd-abi.h
64+
header-test- += mtd/mtd-user.h
65+
header-test- += scsi/scsi_bsg_fc.h
66+
header-test- += scsi/scsi_netlink.h
67+
header-test- += scsi/scsi_netlink_fc.h
68+
header-test- += sound/asequencer.h
69+
header-test- += sound/asoc.h
70+
header-test- += sound/asound.h
71+
header-test- += sound/compress_offload.h
72+
header-test- += sound/emu10k1.h
73+
header-test- += sound/sfnt_info.h
74+
header-test- += sound/sof/eq.h
75+
header-test- += sound/sof/fw.h
76+
header-test- += sound/sof/header.h
77+
header-test- += sound/sof/manifest.h
78+
header-test- += sound/sof/trace.h
79+
header-test- += xen/evtchn.h
80+
header-test- += xen/gntdev.h
81+
header-test- += xen/privcmd.h
82+
83+
# More headers are broken in some architectures
84+
85+
ifeq ($(SRCARCH),arc)
86+
header-test- += linux/bpf_perf_event.h
87+
endif
88+
89+
ifeq ($(SRCARCH),ia64)
90+
header-test- += asm/setup.h
91+
header-test- += asm/sigcontext.h
92+
header-test- += asm/perfmon.h
93+
header-test- += asm/perfmon_default_smpl.h
94+
header-test- += linux/if_bonding.h
95+
endif
96+
97+
ifeq ($(SRCARCH),mips)
98+
header-test- += asm/stat.h
99+
endif
100+
101+
ifeq ($(SRCARCH),powerpc)
102+
header-test- += asm/stat.h
103+
header-test- += linux/bpf_perf_event.h
104+
endif
105+
106+
ifeq ($(SRCARCH),riscv)
107+
header-test- += linux/bpf_perf_event.h
108+
endif
109+
110+
ifeq ($(SRCARCH),s390)
111+
header-test- += asm/runtime_instr.h
112+
header-test- += asm/zcrypt.h
113+
endif
114+
115+
ifeq ($(SRCARCH),sparc)
116+
header-test- += asm/stat.h
117+
header-test- += asm/uctx.h
118+
header-test- += asm/fbio.h
119+
header-test- += asm/openpromio.h
120+
endif
121+
122+
# asm-generic/*.h is used by asm/*.h, and should not be included directly
123+
header-test- += asm-generic/%
124+
125+
# The rest are compile-tested
126+
header-test-y += $(filter-out $(header-test-), \
127+
$(patsubst $(obj)/%,%, $(wildcard \
128+
$(addprefix $(obj)/, *.h */*.h */*/*.h */*/*/*.h))))
129+
130+
# For GNU Make <= 4.2.1, $(wildcard $(obj)/*/) matches to not only directories
131+
# but also regular files. Use $(filter %/, ...) just in case.
132+
clean-dirs += $(patsubst $(obj)/%/,%,$(filter %/, $(wildcard $(obj)/*/)))

0 commit comments

Comments
 (0)