Andrew Geissler | 595f630 | 2022-01-24 19:11:47 +0000 | [diff] [blame] | 1 | From 8ff34dbff1eac612326b492d0b2cb93901ad7e2b Mon Sep 17 00:00:00 2001 |
| 2 | From: Jani Nurminen <jani.nurminen@windriver.com> |
| 3 | Date: Fri, 24 Sep 2021 09:56:11 +0200 |
| 4 | Subject: [PATCH] Lower init prio for extension attributes |
| 5 | MIME-Version: 1.0 |
| 6 | Content-Type: text/plain; charset=UTF-8 |
| 7 | Content-Transfer-Encoding: 8bit |
| 8 | |
| 9 | Added PROTOBUF_EXTENSION_ATTRIBUTE_INIT_PRIORITY in |
| 10 | code generation for extension attributes. |
| 11 | It has lower prio than PROTOBUF_ATTRIBUTE_INIT_PRIORITY to |
| 12 | ensure that extension attributes are initialized after |
| 13 | other attribute. |
| 14 | This is needed in some applications to avoid segmentation fault. |
| 15 | |
| 16 | Reported by Karl-Herman Nรคslund. |
| 17 | |
| 18 | Signed-off-by: Jani Nurminen <jani.nurminen@windriver.com> |
| 19 | |
| 20 | Rebase on master |
| 21 | |
| 22 | Signed-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 | |
| 29 | diff --git a/src/google/protobuf/compiler/cpp/cpp_extension.cc b/src/google/protobuf/compiler/cpp/cpp_extension.cc |
| 30 | index 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", |
| 42 | diff --git a/src/google/protobuf/port_def.inc b/src/google/protobuf/port_def.inc |
| 43 | index 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 |
| 65 | diff --git a/src/google/protobuf/port_undef.inc b/src/google/protobuf/port_undef.inc |
| 66 | index 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 | -- |
| 78 | 2.26.2 |
| 79 | |