Skip to contentSkip to conversation

stddef: make __struct_group() UAPI C++-friendly

PATCH Applied Alexander Lobakin 3 participants 3 messages Dec 19, 2024 – Dec 20, 2024

lore
Share & tools
lore
#0 lore raw

PATCH v2 1/1 stddef: make __struct_group() UAPI C++-friendly

For the most part of the C++ history, it couldn't have type declarations inside anonymous unions for different reasons. At the same time, __struct_group() relies on the latters, so when the @TAG argument is not empty, C++ code doesn't want to build (even under extern "C"):

../linux/include/uapi/linux/pkt_cls.h:25:24: error:
'struct tc_u32_sel::<unnamed union>::tc_u32_sel_hdr,' invalid; an anonymous union may only have public non-static data members [-fpermissive]

The safest way to fix this without trying to switch standards (which is impossible in UAPI anyway) etc., is to disable tag declaration for that language. This won't break anything since for now it's not buildable at all.
Use a separate definition for __struct_group() when __cplusplus is defined to mitigate the error, including the version from tools/.

---
From v1[0]:
* wrap @TAG into __struct_group_tag() (Kees).

[0] https://lore.kernel.org/linux-hardening/[email protected]

---
 include/uapi/linux/stddef.h       | 13 ++++++++++---
 tools/include/uapi/linux/stddef.h | 15 +++++++++++----
 2 files changed, 21 insertions(+), 7 deletions(-)
include/uapi/linux/stddef.h +10 -3
index 58154117d9b0..a6fce46aeb37 100644
@@ -8,6 +8,13 @@
 #define __always_inline inline
 #endif
 
+/* Not all C++ standards support type declarations inside an anonymous union */
+#ifndef __cplusplus
+#define __struct_group_tag(TAG)		TAG
+#else
+#define __struct_group_tag(TAG)
+#endif
+
 /**
  * __struct_group() - Create a mirrored named and anonyomous struct
  *
@@ -20,13 +27,13 @@
  * and size: one anonymous and one named. The former's members can be used
  * normally without sub-struct naming, and the latter can be used to
  * reason about the start, end, and size of the group of struct members.
- * The named struct can also be explicitly tagged for layer reuse, as well
- * as both having struct attributes appended.
+ * The named struct can also be explicitly tagged for layer reuse (C only),
+ * as well as both having struct attributes appended.
  */
 #define __struct_group(TAG, NAME, ATTRS, MEMBERS...) \
 	union { \
 		struct { MEMBERS } ATTRS; \
-		struct TAG { MEMBERS } ATTRS NAME; \
+		struct __struct_group_tag(TAG) { MEMBERS } ATTRS NAME; \
 	} ATTRS
 
 #ifdef __cplusplus
tools/include/uapi/linux/stddef.h +11 -4
index bb6ea517efb5..c53cde425406 100644
@@ -8,6 +8,13 @@
 #define __always_inline __inline__
 #endif
 
+/* Not all C++ standards support type declarations inside an anonymous union */
+#ifndef __cplusplus
+#define __struct_group_tag(TAG)		TAG
+#else
+#define __struct_group_tag(TAG)
+#endif
+
 /**
  * __struct_group() - Create a mirrored named and anonyomous struct
  *
@@ -20,14 +27,14 @@
  * and size: one anonymous and one named. The former's members can be used
  * normally without sub-struct naming, and the latter can be used to
  * reason about the start, end, and size of the group of struct members.
- * The named struct can also be explicitly tagged for layer reuse, as well
- * as both having struct attributes appended.
+ * The named struct can also be explicitly tagged for layer reuse (C only),
+ * as well as both having struct attributes appended.
  */
 #define __struct_group(TAG, NAME, ATTRS, MEMBERS...) \
 	union { \
 		struct { MEMBERS } ATTRS; \
-		struct TAG { MEMBERS } ATTRS NAME; \
-	}
+		struct __struct_group_tag(TAG) { MEMBERS } ATTRS NAME; \
+	} ATTRS
 
 /**
  * __DECLARE_FLEX_ARRAY() - Declare a flexible array usable in a union
Signature · 1 line
2.47.1
Reviewed-by on v2 1/1
#1 lore raw
Quoted 18 lines · Alexander Lobakin

For the most part of the C++ history, it couldn't have type declarations inside anonymous unions for different reasons. At the same time, __struct_group() relies on the latters, so when the @TAG argument is not empty, C++ code doesn't want to build (even under extern "C"):

../linux/include/uapi/linux/pkt_cls.h:25:24: error:
'struct tc_u32_sel::<unnamed union>::tc_u32_sel_hdr,' invalid; an anonymous union may only have public non-static data members [-fpermissive]

The safest way to fix this without trying to switch standards (which is impossible in UAPI anyway) etc., is to disable tag declaration for that language. This won't break anything since for now it's not buildable at all.
Use a separate definition for __struct_group() when __cplusplus is defined to mitigate the error, including the version from tools/.

Quoted 5 lines

Thanks!
-Gustavo

Quoted 82 lines

---
From v1[0]:
* wrap @TAG into __struct_group_tag() (Kees).

[0] https://lore.kernel.org/linux-hardening/[email protected]

---
  include/uapi/linux/stddef.h       | 13 ++++++++++---
  tools/include/uapi/linux/stddef.h | 15 +++++++++++----
  2 files changed, 21 insertions(+), 7 deletions(-)
include/uapi/linux/stddef.h +10 -3
index 58154117d9b0..a6fce46aeb37 100644
@@ -8,6 +8,13 @@
  #define __always_inline inline
  #endif
 
+/* Not all C++ standards support type declarations inside an anonymous union */
+#ifndef __cplusplus
+#define __struct_group_tag(TAG)		TAG
+#else
+#define __struct_group_tag(TAG)
+#endif
+
  /**
   * __struct_group() - Create a mirrored named and anonyomous struct
   *
@@ -20,13 +27,13 @@
   * and size: one anonymous and one named. The former's members can be used
   * normally without sub-struct naming, and the latter can be used to
   * reason about the start, end, and size of the group of struct members.
- * The named struct can also be explicitly tagged for layer reuse, as well
- * as both having struct attributes appended.
+ * The named struct can also be explicitly tagged for layer reuse (C only),
+ * as well as both having struct attributes appended.
   */
  #define __struct_group(TAG, NAME, ATTRS, MEMBERS...) \
  	union { \
  		struct { MEMBERS } ATTRS; \
-		struct TAG { MEMBERS } ATTRS NAME; \
+		struct __struct_group_tag(TAG) { MEMBERS } ATTRS NAME; \
  	} ATTRS
 
  #ifdef __cplusplus
tools/include/uapi/linux/stddef.h +11 -4
index bb6ea517efb5..c53cde425406 100644
@@ -8,6 +8,13 @@
  #define __always_inline __inline__
  #endif
 
+/* Not all C++ standards support type declarations inside an anonymous union */
+#ifndef __cplusplus
+#define __struct_group_tag(TAG)		TAG
+#else
+#define __struct_group_tag(TAG)
+#endif
+
  /**
   * __struct_group() - Create a mirrored named and anonyomous struct
   *
@@ -20,14 +27,14 @@
   * and size: one anonymous and one named. The former's members can be used
   * normally without sub-struct naming, and the latter can be used to
   * reason about the start, end, and size of the group of struct members.
- * The named struct can also be explicitly tagged for layer reuse, as well
- * as both having struct attributes appended.
+ * The named struct can also be explicitly tagged for layer reuse (C only),
+ * as well as both having struct attributes appended.
   */
  #define __struct_group(TAG, NAME, ATTRS, MEMBERS...) \
  	union { \
  		struct { MEMBERS } ATTRS; \
-		struct TAG { MEMBERS } ATTRS NAME; \
-	}
+		struct __struct_group_tag(TAG) { MEMBERS } ATTRS NAME; \
+	} ATTRS
 
  /**
   * __DECLARE_FLEX_ARRAY() - Declare a flexible array usable in a union
Applied
#2 lore raw
In reply to Alexander Lobakin
Quoted 6 lines · Alexander Lobakin

For the most part of the C++ history, it couldn't have type declarations inside anonymous unions for different reasons. At the same time, __struct_group() relies on the latters, so when the @TAG argument is not empty, C++ code doesn't want to build (even under extern "C"):

Quoted 6 lines

../linux/include/uapi/linux/pkt_cls.h:25:24: error:
'struct tc_u32_sel::<unnamed union>::tc_u32_sel_hdr,' invalid; an anonymous union may only have public non-static data members [-fpermissive]

[...]

Applied to for-next/hardening, thanks!

[1/1] stddef: make __struct_group() UAPI C++-friendly

      https://git.kernel.org/kees/c/724c6ce38bba

Take care,

Signature · 1 line
Kees Cook