From 9672858c1932adba34e21b3764b44f44a6034687 Mon Sep 17 00:00:00 2001 From: Stephanos Ioannidis Date: Thu, 11 Aug 2022 03:06:46 +0900 Subject: [PATCH] cmake: compiler: Add flag template for disabling strict aliasing rule This commit adds a template for specifying the C/C++ compiler flag for disabling the strict aliasing rule. It also enables this flag globally because the Zephyr codebase does not strictly adhere to the aliasing rules specified by the C/C++ standards and the optimisation strategies that assume these rules may end up generating invalid code. For instance, GCC 11 and above tend to optimise more aggressively assuming the strict adherence to the standard aliasing rules and may generate invalid code, when compiling Zephyr with `-fstrict-aliasing`, that results in various run-time failures. Note that the footprint and performance ramifications of disabling the strict aliasing rule are negligible. Signed-off-by: Stephanos Ioannidis --- CMakeLists.txt | 4 ++++ cmake/compiler/compiler_flags_template.cmake | 3 +++ 2 files changed, 7 insertions(+) diff --git a/CMakeLists.txt b/CMakeLists.txt index bdf5e0c176f..a7701288975 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -141,6 +141,10 @@ if(TOOLCHAIN_USE_CUSTOM) zephyr_compile_definitions(__TOOLCHAIN_CUSTOM__) endif() +# @Intent: Set compiler specific flag for disabling strict aliasing rule +zephyr_compile_options($<$:$>) +zephyr_compile_options($<$:$>) + # @Intent: Set compiler flags to enable buffer overflow checks in libc functions # @config in CONFIG_NO_OPTIMIZATIONS optional : Optimizations may affect security zephyr_compile_definitions($ ) diff --git a/cmake/compiler/compiler_flags_template.cmake b/cmake/compiler/compiler_flags_template.cmake index b6fb8eb4d46..28b2e7a805a 100644 --- a/cmake/compiler/compiler_flags_template.cmake +++ b/cmake/compiler/compiler_flags_template.cmake @@ -65,6 +65,9 @@ set_property(TARGET compiler-cpp PROPERTY dialect_cpp2a) set_property(TARGET compiler-cpp PROPERTY dialect_cpp20) set_property(TARGET compiler-cpp PROPERTY dialect_cpp2b) +# Flag for disabling strict aliasing rule in C and C++ +set_compiler_property(PROPERTY no_strict_aliasing) + # Flag for disabling exceptions in C++ set_property(TARGET compiler-cpp PROPERTY no_exceptions)