[11/11] UAPI: Check headers build for C++ [ver #2]
diff mbox series

Message ID 153622557736.14298.10350496216090814292.stgit@warthog.procyon.org.uk
State New
Headers show
Series
  • [01/11] UAPI: drm: Fix use of C++ keywords as structural members [ver #2]
Related show

Commit Message

David Howells Sept. 6, 2018, 9:19 a.m. UTC
Check that all the headers can be included from one file and built for C++,
thereby catching the use of C++ reserved words and bits of unimplemented
C++ in the UAPI headers.

Note that certain headers are excluded from the build, including:

 (1) Any header ending in "_32.h", "_64.h" or "_x32.h" as these are
     expected to be multiarch variant headers.

 (2) Endianness variant headers.

 (3) asm-generic/ headers (they're used conditionally by the asm/ headers
     and shouldn't be used directly).

 (4) netfilter_ipv*/ip*t_LOG.h headers.  They emit a warning indicating
     they're going to be removed soon.

Signed-off-by: David Howells <dhowells@redhat.com>
cc: Masahiro Yamada <yamada.masahiro@socionext.com>
cc: Michal Marek <michal.lkml@markovi.net>
cc: linux-kbuild@vger.kernel.org
---

 Makefile               |    1 
 scripts/headers-c++.sh |  124 ++++++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 125 insertions(+)
 create mode 100755 scripts/headers-c++.sh

Comments

kbuild test robot Sept. 10, 2018, 4:26 p.m. UTC | #1
Hi David,

I love your patch! Yet something to improve:

[auto build test ERROR on linus/master]
[also build test ERROR on v4.19-rc3 next-20180910]
[if your patch is applied to the wrong git tree, please drop us a note to help improve the system]

url:    https://github.com/0day-ci/linux/commits/David-Howells/UAPI-drm-Fix-use-of-C-keywords-as-structural-members-ver-2/20180907-092121
config: x86_64-kexec (attached as .config)
compiler: gcc-7 (Debian 7.3.0-1) 7.3.0
reproduce:
        # save the attached .config to linux build tree
        make ARCH=x86_64 

All errors (new ones prefixed by >>):

>> find: '../usr/include': No such file or directory
   In file included from test.cpp:1:0:
>> test.h:8:10: fatal error: linux/types.h: No such file or directory
    #include <linux/types.h>
             ^~~~~~~~~~~~~~~
   compilation terminated.
   make[1]: *** [headers_install] Error 1
   make: *** [sub-make] Error 2

---
0-DAY kernel test infrastructure                Open Source Technology Center
https://lists.01.org/pipermail/kbuild-all                   Intel Corporation
kbuild test robot Sept. 10, 2018, 5:02 p.m. UTC | #2
Hi David,

I love your patch! Yet something to improve:

[auto build test ERROR on linus/master]
[also build test ERROR on v4.19-rc3 next-20180910]
[if your patch is applied to the wrong git tree, please drop us a note to help improve the system]

url:    https://github.com/0day-ci/linux/commits/David-Howells/UAPI-drm-Fix-use-of-C-keywords-as-structural-members-ver-2/20180907-092121
config: x86_64-kexec (attached as .config)
compiler: gcc-7 (Debian 7.3.0-1) 7.3.0
reproduce:
        # save the attached .config to linux build tree
        make ARCH=x86_64 

All errors (new ones prefixed by >>):

   In file included from test.h:225:0,
                    from test.cpp:1:
>> ../usr/include/linux/patchkey.h:15:2: error: #error "patchkey.h included directly"
    #error "patchkey.h included directly"
     ^~~~~
   In file included from ../usr/include/asm/shmbuf.h:6:0,
                    from test.h:83,
                    from test.cpp:1:
>> ../usr/include/asm-generic/shmbuf.h:26:20: error: field 'shm_perm' has incomplete type 'ipc64_perm'
     struct ipc64_perm shm_perm; /* operation perms */
                       ^~~~~~~~
   ../usr/include/asm-generic/shmbuf.h:26:9: note: forward declaration of 'struct ipc64_perm'
     struct ipc64_perm shm_perm; /* operation perms */
            ^~~~~~~~~~
   In file included from ../usr/include/asm/msgbuf.h:6:0,
                    from test.h:112,
                    from test.cpp:1:
>> ../usr/include/asm-generic/msgbuf.h:25:20: error: field 'msg_perm' has incomplete type 'ipc64_perm'
     struct ipc64_perm msg_perm;
                       ^~~~~~~~
   In file included from ../usr/include/asm/shmbuf.h:6:0,
                    from test.h:83,
                    from test.cpp:1:
   ../usr/include/asm-generic/shmbuf.h:26:9: note: forward declaration of 'struct ipc64_perm'
     struct ipc64_perm shm_perm; /* operation perms */
            ^~~~~~~~~~
   In file included from test.h:868:0,
                    from test.cpp:1:
   ../usr/include/sound/skl-tplg-interface.h:146:2: error: 'u32' does not name a type; did you mean '__u32'?
     u32 set_params:2;
     ^~~
     __u32
   ../usr/include/sound/skl-tplg-interface.h:147:2: error: 'u32' does not name a type; did you mean '__u32'?
     u32 rsvd:30;
     ^~~
     __u32
   ../usr/include/sound/skl-tplg-interface.h:148:2: error: 'u32' does not name a type; did you mean '__u32'?
     u32 param_id;
     ^~~
     __u32
   ../usr/include/sound/skl-tplg-interface.h:149:2: error: 'u32' does not name a type; did you mean '__u32'?
     u32 max;
     ^~~
     __u32
   ../usr/include/sound/skl-tplg-interface.h:166:2: error: 'u16' does not name a type; did you mean '__u16'?
     u16 module_id;
     ^~~
     __u16
   ../usr/include/sound/skl-tplg-interface.h:167:2: error: 'u16' does not name a type; did you mean '__u16'?
     u16 instance_id;
     ^~~
     __u16
   ../usr/include/sound/skl-tplg-interface.h:171:2: error: 'u32' does not name a type; did you mean '__u32'?
     u32 channels;
     ^~~
     __u32
   ../usr/include/sound/skl-tplg-interface.h:172:2: error: 'u32' does not name a type; did you mean '__u32'?
     u32 freq;
     ^~~
     __u32
   ../usr/include/sound/skl-tplg-interface.h:173:2: error: 'u32' does not name a type; did you mean '__u32'?
     u32 bit_depth;
     ^~~
     __u32
   ../usr/include/sound/skl-tplg-interface.h:174:2: error: 'u32' does not name a type; did you mean '__u32'?
     u32 valid_bit_depth;
     ^~~
     __u32
   ../usr/include/sound/skl-tplg-interface.h:175:2: error: 'u32' does not name a type; did you mean '__u32'?
     u32 ch_cfg;
     ^~~
     __u32
   ../usr/include/sound/skl-tplg-interface.h:176:2: error: 'u32' does not name a type; did you mean '__u32'?
     u32 interleaving_style;
     ^~~
     __u32
   ../usr/include/sound/skl-tplg-interface.h:177:2: error: 'u32' does not name a type; did you mean '__u32'?
     u32 sample_type;
     ^~~
     __u32
   ../usr/include/sound/skl-tplg-interface.h:178:2: error: 'u32' does not name a type; did you mean '__u32'?
     u32 ch_map;
     ^~~
     __u32
   ../usr/include/sound/skl-tplg-interface.h:182:2: error: 'u32' does not name a type; did you mean '__u32'?
     u32 set_params:2;
     ^~~
     __u32
   ../usr/include/sound/skl-tplg-interface.h:183:2: error: 'u32' does not name a type; did you mean '__u32'?
     u32 rsvd:30;
     ^~~
     __u32
   ../usr/include/sound/skl-tplg-interface.h:184:2: error: 'u32' does not name a type; did you mean '__u32'?
     u32 param_id;
     ^~~
     __u32
   ../usr/include/sound/skl-tplg-interface.h:185:2: error: 'u32' does not name a type; did you mean '__u32'?
     u32 caps_size;
     ^~~
     __u32
   ../usr/include/sound/skl-tplg-interface.h:186:2: error: 'u32' does not name a type; did you mean '__u32'?
     u32 caps[HDA_SST_CFG_MAX];
     ^~~
     __u32
   ../usr/include/sound/skl-tplg-interface.h:190:2: error: 'u8' does not name a type; did you mean '__u8'?
     u8 pipe_id;
     ^~
     __u8
   ../usr/include/sound/skl-tplg-interface.h:191:2: error: 'u8' does not name a type; did you mean '__u8'?
     u8 pipe_priority;
     ^~
     __u8
   ../usr/include/sound/skl-tplg-interface.h:192:2: error: 'u16' does not name a type; did you mean '__u16'?
     u16 conn_type:4;
     ^~~
     __u16
   ../usr/include/sound/skl-tplg-interface.h:193:2: error: 'u16' does not name a type; did you mean '__u16'?
     u16 rsvd:4;
--
>> find: '../usr/include': No such file or directory
   In file included from test.cpp:1:0:
>> test.h:8:10: fatal error: linux/types.h: No such file or directory
    #include <linux/types.h>
             ^~~~~~~~~~~~~~~
   compilation terminated.
   make[1]: *** [headers_install] Error 1
   make: *** [sub-make] Error 2

---
0-DAY kernel test infrastructure                Open Source Technology Center
https://lists.01.org/pipermail/kbuild-all                   Intel Corporation
Arnd Bergmann Sept. 14, 2018, 9:10 a.m. UTC | #3
On Thu, Sep 6, 2018 at 11:21 AM David Howells <dhowells@redhat.com> wrote:

> +
> +typedef __s8                   int8_t;
> +typedef __s16                  int16_t;
> +typedef __s32                  int32_t;
> +typedef __s64                  int64_t;
> +typedef __u8                   uint8_t;
> +typedef __u16                  uint16_t;
> +typedef __u32                  uint32_t;
> +typedef __u64                  uint64_t;
> +typedef long int               intptr_t;
> +typedef unsigned long int      uintptr_t;
> +typedef unsigned short         u_short;
> +typedef unsigned int           u_int;
> +typedef unsigned long          u_long;
> +typedef char                   *caddr_t;
> +
> +typedef __kernel_clockid_t     clockid_t;
> +typedef __kernel_ino_t         ino_t;
> +typedef __kernel_pid_t         pid_t;
> +typedef __kernel_sa_family_t   sa_family_t;
> +typedef __kernel_size_t                size_t;
> +typedef __kernel_uid_t         uid_t;
> +
> +typedef unsigned long          elf_greg_t;
> +typedef elf_greg_t             elf_gregset_t[1];
> +typedef unsigned long long     elf_fpregset_t[1];
> +typedef unsigned long long     elf_fpxregset_t[1];
> +
> +#define INT_MIN ((int)0x80000000)
> +#define INT_MAX ((int)0x7fffffff)
> +
> +extern size_t strlen(const char *);
> +extern void *memset(void *, int, size_t);
> +extern void *memcpy(void *, const void *, size_t);
> +extern __u16 ntohs(__u16);
> +extern __u16 htons(__u16);
> +extern __u32 ntohl(__u32);
> +extern __u32 htonl(__u32);
> +
> +typedef uint32_t               grant_ref_t;
> +typedef uint16_t               domid_t;
> +typedef unsigned long          xen_pfn_t;
> +
> +#define MSG_FIN         0x200
> +
> +typedef int SVGA3dMSPattern;
> +typedef int SVGA3dMSQualityLevel;
> +
> +struct sockaddr
> +{
> +       sa_family_t     sa_family;
> +       char            sa_data[14];
> +};
> +#define sockaddr_storage __kernel_sockaddr_storage

I think we need to reduce that list as much as we can. In
https://patchwork.ozlabs.org/patch/968814/, Joseph Myers pointed
out  header file (linux/elfcore.h) that simply cannot be included from
user space at all, because its dependencies cannot be met without
running into conflicting type definitions, and he would like to include
that file in order to automatically check that it's compatible with
the glibc version (I pointed out a couple of architectures on which
it is in fact incompatible right now).

In the list above, I see multiple classes of bugs that could be
addressed:

- references to identifiers that are only present in kernel internal
  headers: SVGA3dMSPattern, MSG_FIN, xen_pfn_t, ...
  I think these are all simple bugs, and we should either remove
  the references, or make sure the respective dependencies are
  included in the uapi headers as well, possibly renamed with
  a __kernel_ prefix to avoid clashing with user space headers.

- references to user space types that should use the uapi
  internal types: sockaddr_storage, clockid_t, uid_t, ...
  I think these just need to get the __kernel_prefix
  consistently as we did a few years ago. Note that using
  the headers otherwise is broken anyway when the types
  in libc are different from the ones in the kernel.

- standard types (uint32_t): either include the correct user
  space headers ifndef __KERNEL__ or use the kernel types

- standard functions (memcpy(), ntohs(), ...): These should
  already be handled in the headers by including the user space.
  If we missed any, we should probably do the same thing
  there.


       Arnd

Patch
diff mbox series

diff --git a/Makefile b/Makefile
index 19948e556941..38d655cb4474 100644
--- a/Makefile
+++ b/Makefile
@@ -1186,6 +1186,7 @@  headers_install: __headers
 	  $(error Headers not exportable for the $(SRCARCH) architecture))
 	$(Q)$(MAKE) $(hdr-inst)=include/uapi dst=include
 	$(Q)$(MAKE) $(hdr-inst)=arch/$(SRCARCH)/include/uapi $(hdr-dst)
+	$(Q)$(CONFIG_SHELL) $(srctree)/scripts/headers-c++.sh check
 
 PHONY += headers_check_all
 headers_check_all: headers_install_all
diff --git a/scripts/headers-c++.sh b/scripts/headers-c++.sh
new file mode 100755
index 000000000000..7e56913629f8
--- /dev/null
+++ b/scripts/headers-c++.sh
@@ -0,0 +1,124 @@ 
+#!/bin/sh
+# SPDX-License-Identifier: GPL-2.0
+# Run headers_$1 command for all suitable architectures
+
+# Stop on error
+set -e
+
+if ! $CC -x c++ -c - -o /dev/null </dev/null 2>/dev/null
+then
+    echo "  CHECK   C++ HEADER COMPILATION [SKIPPED]"
+    exit 0
+fi
+
+echo "  CHECK   C++ HEADER COMPILATION"
+
+mkdir -p hdr-check
+cd hdr-check
+
+mkdir -p include/sys
+mkdir -p include/arpa
+mkdir -p include/xen/interface
+echo >include/endian.h
+echo >include/limits.h
+echo >include/stdint.h
+echo >include/stdlib.h
+echo >include/stdio.h
+echo >include/string.h
+echo >include/time.h
+echo >include/unistd.h
+echo >include/arpa/inet.h
+echo >include/sys/ioctl.h
+echo >include/sys/types.h
+echo >include/sys/time.h
+echo >include/sys/socket.h
+echo >include/xen/interface/xen.h
+
+cat >test.h <<EOF
+#ifdef __cplusplus
+#define NULL nullptr
+#define _Bool bool
+#else
+#define NULL ((void *)0)
+#define bool _Bool
+#endif
+#include <linux/types.h>
+#include <linux/socket.h>
+#include <linux/time.h>
+
+typedef __s8			int8_t;
+typedef __s16			int16_t;
+typedef __s32			int32_t;
+typedef __s64			int64_t;
+typedef __u8			uint8_t;
+typedef __u16			uint16_t;
+typedef __u32			uint32_t;
+typedef __u64			uint64_t;
+typedef long int		intptr_t;
+typedef unsigned long int	uintptr_t;
+typedef unsigned short		u_short;
+typedef unsigned int		u_int;
+typedef unsigned long		u_long;
+typedef char			*caddr_t;
+
+typedef __kernel_clockid_t	clockid_t;
+typedef __kernel_ino_t		ino_t;
+typedef __kernel_pid_t		pid_t;
+typedef __kernel_sa_family_t	sa_family_t;
+typedef __kernel_size_t		size_t;
+typedef __kernel_uid_t		uid_t;
+
+typedef unsigned long		elf_greg_t;
+typedef elf_greg_t		elf_gregset_t[1];
+typedef unsigned long long	elf_fpregset_t[1];
+typedef unsigned long long	elf_fpxregset_t[1];
+
+#define INT_MIN ((int)0x80000000)
+#define INT_MAX ((int)0x7fffffff)
+
+extern size_t strlen(const char *);
+extern void *memset(void *, int, size_t);
+extern void *memcpy(void *, const void *, size_t);
+extern __u16 ntohs(__u16);
+extern __u16 htons(__u16);
+extern __u32 ntohl(__u32);
+extern __u32 htonl(__u32);
+
+typedef uint32_t		grant_ref_t;
+typedef uint16_t		domid_t;
+typedef unsigned long		xen_pfn_t;
+
+#define MSG_FIN         0x200
+
+typedef int SVGA3dMSPattern;
+typedef int SVGA3dMSQualityLevel;
+
+struct sockaddr
+{
+	sa_family_t	sa_family;
+	char		sa_data[14];
+};
+#define sockaddr_storage __kernel_sockaddr_storage
+
+#define _LINUX_PATCHKEY_H_INDIRECT
+
+EOF
+
+find ../usr/include -name '*.h' |
+    grep -v 'linux/byteorder/big_endian.h' |
+    grep -v 'linux/byteorder/little_endian.h' |
+    grep -v '_\(32\|64\|x32\)[.]h$' |
+    grep -v '/asm-generic/' |
+    # ip*t_LOG.h are deprecated
+    grep -v 'linux/netfilter_ipv4/ipt_LOG[.]h' |
+    grep -v 'linux/netfilter_ipv6/ip6t_LOG[.]h' |
+    sed -e 's!../usr/include/!#include <!' -e 's!$!>!' >>test.h
+
+echo '#include "test.h"' >test.cpp
+
+$CC -x c++ -o /dev/null -c test.cpp \
+    -nostdinc \
+    -isystem ./include \
+    -isystem ../usr/include \
+    -fpermissive \
+    -D PAGE_SIZE='#PAGE_SIZE_IS_NOT_VALID_OUTSIDE_OF_KERNEL'