[meta-oe,master] protobuf: Fix static init fiasco

Message ID 20220117032941.10436-1-zhe.he@windriver.com
State New
Headers show
Series [meta-oe,master] protobuf: Fix static init fiasco | expand

Commit Message

He Zhe Jan. 17, 2022, 3:29 a.m. UTC
The protobuf suffers from the C++ "Static Initialization Fiasco"
issue. This patches makes the extension attributes have a higher
priority than the attributes, so there's no possibility of random
initialization orders.

Signed-off-by: Jani Nurminen <jani.nurminen@windriver.com>

Upstream-Status: Pending

Signed-off-by: He Zhe <zhe.he@windriver.com>
---
 ...r-init-prio-for-extension-attributes.patch | 79 +++++++++++++++++++
 .../protobuf/protobuf_3.19.1.bb               |  1 +
 2 files changed, 80 insertions(+)
 create mode 100644 meta-oe/recipes-devtools/protobuf/protobuf/0001-Lower-init-prio-for-extension-attributes.patch

Patch

diff --git a/meta-oe/recipes-devtools/protobuf/protobuf/0001-Lower-init-prio-for-extension-attributes.patch b/meta-oe/recipes-devtools/protobuf/protobuf/0001-Lower-init-prio-for-extension-attributes.patch
new file mode 100644
index 000000000..a1200e01c
--- /dev/null
+++ b/meta-oe/recipes-devtools/protobuf/protobuf/0001-Lower-init-prio-for-extension-attributes.patch
@@ -0,0 +1,79 @@ 
+From 8ff34dbff1eac612326b492d0b2cb93901ad7e2b Mon Sep 17 00:00:00 2001
+From: Jani Nurminen <jani.nurminen@windriver.com>
+Date: Fri, 24 Sep 2021 09:56:11 +0200
+Subject: [PATCH] Lower init prio for extension attributes
+MIME-Version: 1.0
+Content-Type: text/plain; charset=UTF-8
+Content-Transfer-Encoding: 8bit
+
+Added PROTOBUF_EXTENSION_ATTRIBUTE_INIT_PRIORITY in
+code generation for extension attributes.
+It has lower prio than PROTOBUF_ATTRIBUTE_INIT_PRIORITY to
+ensure that extension attributes are initialized after
+other attribute.
+This is needed in some applications to avoid segmentation fault.
+
+Reported by Karl-Herman Näslund.
+
+Signed-off-by: Jani Nurminen <jani.nurminen@windriver.com>
+
+Rebase on master
+
+Signed-off-by: He Zhe <zhe.he@windriver.com>
+---
+ src/google/protobuf/compiler/cpp/cpp_extension.cc |  2 +-
+ src/google/protobuf/port_def.inc                  | 12 ++++++++++++
+ src/google/protobuf/port_undef.inc                |  1 +
+ 3 files changed, 14 insertions(+), 1 deletion(-)
+
+diff --git a/src/google/protobuf/compiler/cpp/cpp_extension.cc b/src/google/protobuf/compiler/cpp/cpp_extension.cc
+index 8604da5f2..984345ebe 100644
+--- a/src/google/protobuf/compiler/cpp/cpp_extension.cc
++++ b/src/google/protobuf/compiler/cpp/cpp_extension.cc
+@@ -164,7 +164,7 @@ void ExtensionGenerator::GenerateDefinition(io::Printer* printer) {
+   }
+ 
+   format(
+-      "PROTOBUF_ATTRIBUTE_INIT_PRIORITY "
++      "PROTOBUF_EXTENSION_ATTRIBUTE_INIT_PRIORITY "
+       "::$proto_ns$::internal::ExtensionIdentifier< $extendee$,\n"
+       "    ::$proto_ns$::internal::$type_traits$, $field_type$, $packed$ >\n"
+       "  $scoped_name$($constant_name$, $1$);\n",
+diff --git a/src/google/protobuf/port_def.inc b/src/google/protobuf/port_def.inc
+index 7e9119112..a5117090d 100644
+--- a/src/google/protobuf/port_def.inc
++++ b/src/google/protobuf/port_def.inc
+@@ -614,6 +614,18 @@
+ #define PROTOBUF_ATTRIBUTE_INIT_PRIORITY
+ #endif
+ 
++// Some embedded systems get a segmentation fault if extension attributes are
++// initialized with higher or equal priority as other attributes. This gives
++// extension attributes high priority, but lower than other attributes.
++#ifdef PROTOBUF_EXTENSION_ATTRIBUTE_INIT_PRIORITY
++#error PROTOBUF_EXTENSION_ATTRIBUTE_INIT_PRIORITY was previously defined
++#endif
++#if PROTOBUF_GNUC_MIN(3, 0) && (!defined(__APPLE__) || defined(__clang__)) && !((defined(sun) || defined(__sun)) && (defined(__SVR4) || defined(__svr4__)))
++#define PROTOBUF_EXTENSION_ATTRIBUTE_INIT_PRIORITY __attribute__((init_priority((103))))
++#else
++#define PROTOBUF_EXTENSION_ATTRIBUTE_INIT_PRIORITY
++#endif
++
+ #ifdef PROTOBUF_PRAGMA_INIT_SEG
+ #error PROTOBUF_PRAGMA_INIT_SEG was previously defined
+ #endif
+diff --git a/src/google/protobuf/port_undef.inc b/src/google/protobuf/port_undef.inc
+index ccc5daf56..2b28f3a31 100644
+--- a/src/google/protobuf/port_undef.inc
++++ b/src/google/protobuf/port_undef.inc
+@@ -83,6 +83,7 @@
+ #undef PROTOBUF_HAVE_ATTRIBUTE_WEAK
+ #undef PROTOBUF_ATTRIBUTE_NO_DESTROY
+ #undef PROTOBUF_ATTRIBUTE_INIT_PRIORITY
++#undef PROTOBUF_EXTENSION_ATTRIBUTE_INIT_PRIORITY
+ #undef PROTOBUF_PRAGMA_INIT_SEG
+ #undef PROTOBUF_ASAN
+ #undef PROTOBUF_MSAN
+-- 
+2.26.2
+
diff --git a/meta-oe/recipes-devtools/protobuf/protobuf_3.19.1.bb b/meta-oe/recipes-devtools/protobuf/protobuf_3.19.1.bb
index bff159590..8491dbdf5 100644
--- a/meta-oe/recipes-devtools/protobuf/protobuf_3.19.1.bb
+++ b/meta-oe/recipes-devtools/protobuf/protobuf_3.19.1.bb
@@ -18,6 +18,7 @@  SRC_URI = "git://github.com/protocolbuffers/protobuf.git;branch=3.19.x;protocol=
            file://0001-Makefile.am-include-descriptor.cc-when-building-libp.patch \
            file://0001-examples-Makefile-respect-CXX-LDFLAGS-variables-fix-.patch \
            file://0001-Fix-linking-error-with-ld-gold.patch \
+           file://0001-Lower-init-prio-for-extension-attributes.patch \
            "
 SRC_URI:append:mips:toolchain-clang = " file://0001-Fix-build-on-mips-clang.patch "
 SRC_URI:append:mipsel:toolchain-clang = " file://0001-Fix-build-on-mips-clang.patch "