From 665e848576b384691f35995f43bf357fd4ea0c5e Mon Sep 17 00:00:00 2001 From: Brian Gianforcaro Date: Tue, 24 Aug 2021 20:28:51 -0700 Subject: [PATCH] CMake: Remove Prekernel incompatible options instead of overriding The pattern of having Prekernel inherit all of the build flags of the Kernel, and then disabling some flags by adding `-fno-` options to then disable those options doesn't work in all scenarios. For example the ASAN flag `-fasan-shadow-offset=` has no option to disable it once it's been passed, so in a future change where this flag is added we need to be able to disable it cleanly. The cleaner way is to just allow the Prekernel CMake logic to filter out the COMPILE_OPTIONS specified for that specific target. This allows us to remove individual options without trashing all inherited options. --- Kernel/CMakeLists.txt | 4 ++-- Kernel/Prekernel/CMakeLists.txt | 7 ++++++- 2 files changed, 8 insertions(+), 3 deletions(-) diff --git a/Kernel/CMakeLists.txt b/Kernel/CMakeLists.txt index adcc59844bf..9a50ede5971 100644 --- a/Kernel/CMakeLists.txt +++ b/Kernel/CMakeLists.txt @@ -395,7 +395,7 @@ set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -static-pie") # secure to run in production builds. Useful for coverage guided fuzzing. if (ENABLE_KERNEL_COVERAGE_COLLECTION) add_definitions(-DENABLE_KERNEL_COVERAGE_COLLECTION) - set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fsanitize-coverage=trace-pc") + add_compile_options(-fsanitize-coverage=trace-pc) set(KCOV_EXCLUDED_SOURCES # Make sure we don't instrument any code called from __sanitizer_cov_trace_pc # otherwise we'll end up with recursive calls to that function. @@ -427,7 +427,7 @@ set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fsanitize=undefined") # is not currently meant to be used, besides when developing Kernel ASAN support. # if (ENABLE_KERNEL_ADDRESS_SANITIZER) - set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fsanitize=kernel-address") + add_compile_options(-fsanitize=kernel-address) endif() add_compile_definitions(KERNEL) diff --git a/Kernel/Prekernel/CMakeLists.txt b/Kernel/Prekernel/CMakeLists.txt index a9502451704..4efcd4eef2a 100644 --- a/Kernel/Prekernel/CMakeLists.txt +++ b/Kernel/Prekernel/CMakeLists.txt @@ -34,4 +34,9 @@ add_custom_command( ) install(FILES "${CMAKE_CURRENT_BINARY_DIR}/Prekernel" DESTINATION boot) -set_source_files_properties(${SOURCES} PROPERTIES COMPILE_FLAGS "-fno-sanitize-coverage=trace-pc -fno-sanitize=kernel-address") + +# Remove options which the Prekernel environment doesn't support. +get_target_property(PREKERNEL_TARGET_OPTIONS ${PREKERNEL_TARGET} COMPILE_OPTIONS) +list(REMOVE_ITEM PREKERNEL_TARGET_OPTIONS "-fsanitize-coverage=trace-pc") +list(REMOVE_ITEM PREKERNEL_TARGET_OPTIONS "-fsanitize=kernel-address") +set_target_properties(${PREKERNEL_TARGET} PROPERTIES COMPILE_OPTIONS "${PREKERNEL_TARGET_OPTIONS}")