From 21be5747729a65323deaf6ab7c93762e0fe3ea5b Mon Sep 17 00:00:00 2001 From: alrvid <126816223+alrvid@users.noreply.github.com> Date: Tue, 31 Oct 2023 13:00:03 +0100 Subject: [PATCH 1/2] Prevent aliasing bugs in the Callback code This change prevents the GCC compiler from applying optimizations that assume the code follows strict aliasing rules. In order to prevent bugs arising from undefined behavior that is tricky to find in the Callback implementation, or simply from compiler bugs in GCC. The reason for making the change now is concrete customer cases where incorrect machine code was generated in relation to Mbed Callbacks. --- cores/arduino/mbed/platform/include/platform/Callback.h | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/cores/arduino/mbed/platform/include/platform/Callback.h b/cores/arduino/mbed/platform/include/platform/Callback.h index e76a4f197..1cbf730cf 100644 --- a/cores/arduino/mbed/platform/include/platform/Callback.h +++ b/cores/arduino/mbed/platform/include/platform/Callback.h @@ -17,6 +17,11 @@ #ifndef MBED_CALLBACK_H #define MBED_CALLBACK_H +// This prevents the GCC compiler from applying optimizations that assume the code follows strict aliasing rules. +// In order to prevent bugs arising from undefined behavior that is tricky to find in the Callback implementation, +// or simply from compiler bugs in GCC. +#pragma GCC optimize("-fno-strict-aliasing") + #include #include #include From 584975a18f28d871e472fd37773863838ea0a8ca Mon Sep 17 00:00:00 2001 From: alrvid <126816223+alrvid@users.noreply.github.com> Date: Wed, 1 Nov 2023 22:58:36 +0100 Subject: [PATCH 2/2] Fix incorrect inline code generation This prevents the GCC compiler from generating incorrect inline code for the Callback constructor. --- .../mbed/platform/include/platform/Callback.h | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/cores/arduino/mbed/platform/include/platform/Callback.h b/cores/arduino/mbed/platform/include/platform/Callback.h index 1cbf730cf..a7dd4e003 100644 --- a/cores/arduino/mbed/platform/include/platform/Callback.h +++ b/cores/arduino/mbed/platform/include/platform/Callback.h @@ -17,11 +17,6 @@ #ifndef MBED_CALLBACK_H #define MBED_CALLBACK_H -// This prevents the GCC compiler from applying optimizations that assume the code follows strict aliasing rules. -// In order to prevent bugs arising from undefined behavior that is tricky to find in the Callback implementation, -// or simply from compiler bugs in GCC. -#pragma GCC optimize("-fno-strict-aliasing") - #include #include #include @@ -31,6 +26,14 @@ #include #include +#pragma GCC push_options +// This prevents the GCC compiler from applying optimizations that assume the code follows strict aliasing rules. +// In order to prevent bugs arising from undefined behavior that is tricky to find in the Callback implementation, +// or simply from compiler bugs in GCC. +#pragma GCC optimize("-fno-strict-aliasing") +// This prevents the GCC compiler from generating incorrect inline code for the Callback constructor. +#pragma GCC optimize("-fno-inline") + // Controlling switches from config: // MBED_CONF_PLATFORM_CALLBACK_NONTRIVIAL - support storing non-trivial function objects // MBED_CONF_PLATFORM_CALLBACK_COMPARABLE - support memcmp comparing stored objects (requires zero padding) @@ -41,7 +44,6 @@ #define MBED_CONF_PLATFORM_CALLBACK_NONTRIVIAL 1 #endif - namespace mbed { /** \addtogroup platform-public-api */ /** @{*/ @@ -840,4 +842,6 @@ Callback(R(*func)(const volatile T *, ArgTs...), const volatile U *arg) -> Callb } // namespace mbed +#pragma GCC pop_options + #endif