blob: a1200e01c1418e4e873929ad22d1dde7f490f3b8 [file] [log] [blame]
Andrew Geissler595f6302022-01-24 19:11:47 +00001From 8ff34dbff1eac612326b492d0b2cb93901ad7e2b Mon Sep 17 00:00:00 2001
2From: Jani Nurminen <jani.nurminen@windriver.com>
3Date: Fri, 24 Sep 2021 09:56:11 +0200
4Subject: [PATCH] Lower init prio for extension attributes
5MIME-Version: 1.0
6Content-Type: text/plain; charset=UTF-8
7Content-Transfer-Encoding: 8bit
8
9Added PROTOBUF_EXTENSION_ATTRIBUTE_INIT_PRIORITY in
10code generation for extension attributes.
11It has lower prio than PROTOBUF_ATTRIBUTE_INIT_PRIORITY to
12ensure that extension attributes are initialized after
13other attribute.
14This is needed in some applications to avoid segmentation fault.
15
16Reported by Karl-Herman Nรคslund.
17
18Signed-off-by: Jani Nurminen <jani.nurminen@windriver.com>
19
20Rebase on master
21
22Signed-off-by: He Zhe <zhe.he@windriver.com>
23---
24 src/google/protobuf/compiler/cpp/cpp_extension.cc | 2 +-
25 src/google/protobuf/port_def.inc | 12 ++++++++++++
26 src/google/protobuf/port_undef.inc | 1 +
27 3 files changed, 14 insertions(+), 1 deletion(-)
28
29diff --git a/src/google/protobuf/compiler/cpp/cpp_extension.cc b/src/google/protobuf/compiler/cpp/cpp_extension.cc
30index 8604da5f2..984345ebe 100644
31--- a/src/google/protobuf/compiler/cpp/cpp_extension.cc
32+++ b/src/google/protobuf/compiler/cpp/cpp_extension.cc
33@@ -164,7 +164,7 @@ void ExtensionGenerator::GenerateDefinition(io::Printer* printer) {
34 }
35
36 format(
37- "PROTOBUF_ATTRIBUTE_INIT_PRIORITY "
38+ "PROTOBUF_EXTENSION_ATTRIBUTE_INIT_PRIORITY "
39 "::$proto_ns$::internal::ExtensionIdentifier< $extendee$,\n"
40 " ::$proto_ns$::internal::$type_traits$, $field_type$, $packed$ >\n"
41 " $scoped_name$($constant_name$, $1$);\n",
42diff --git a/src/google/protobuf/port_def.inc b/src/google/protobuf/port_def.inc
43index 7e9119112..a5117090d 100644
44--- a/src/google/protobuf/port_def.inc
45+++ b/src/google/protobuf/port_def.inc
46@@ -614,6 +614,18 @@
47 #define PROTOBUF_ATTRIBUTE_INIT_PRIORITY
48 #endif
49
50+// Some embedded systems get a segmentation fault if extension attributes are
51+// initialized with higher or equal priority as other attributes. This gives
52+// extension attributes high priority, but lower than other attributes.
53+#ifdef PROTOBUF_EXTENSION_ATTRIBUTE_INIT_PRIORITY
54+#error PROTOBUF_EXTENSION_ATTRIBUTE_INIT_PRIORITY was previously defined
55+#endif
56+#if PROTOBUF_GNUC_MIN(3, 0) && (!defined(__APPLE__) || defined(__clang__)) && !((defined(sun) || defined(__sun)) && (defined(__SVR4) || defined(__svr4__)))
57+#define PROTOBUF_EXTENSION_ATTRIBUTE_INIT_PRIORITY __attribute__((init_priority((103))))
58+#else
59+#define PROTOBUF_EXTENSION_ATTRIBUTE_INIT_PRIORITY
60+#endif
61+
62 #ifdef PROTOBUF_PRAGMA_INIT_SEG
63 #error PROTOBUF_PRAGMA_INIT_SEG was previously defined
64 #endif
65diff --git a/src/google/protobuf/port_undef.inc b/src/google/protobuf/port_undef.inc
66index ccc5daf56..2b28f3a31 100644
67--- a/src/google/protobuf/port_undef.inc
68+++ b/src/google/protobuf/port_undef.inc
69@@ -83,6 +83,7 @@
70 #undef PROTOBUF_HAVE_ATTRIBUTE_WEAK
71 #undef PROTOBUF_ATTRIBUTE_NO_DESTROY
72 #undef PROTOBUF_ATTRIBUTE_INIT_PRIORITY
73+#undef PROTOBUF_EXTENSION_ATTRIBUTE_INIT_PRIORITY
74 #undef PROTOBUF_PRAGMA_INIT_SEG
75 #undef PROTOBUF_ASAN
76 #undef PROTOBUF_MSAN
77--
782.26.2
79