Skip to content

[SYCL][CMAKE] Fix _FORTIFY_SOURCE=3 #19268

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 23 additions & 8 deletions llvm/cmake/modules/AddSecurityFlags.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -164,24 +164,39 @@ macro(append_common_extra_security_flags)
endif()
endif()

if(LLVM_ON_UNIX)
# Fortify Source (strongly recommended):
# Fortify Source (strongly recommended):
if (NOT WIN32)
# Strictly speaking, _FORTIFY_SOURCE is a glibc feature and not a compiler
# feature. However, we experienced some issues (warnings about redefined macro
# which are problematic under -Werror) when setting it to value '3' with older
# gcc versions. Hence the check.
# Value '3' became supported in glibc somewhere around gcc 12, so that is
# what we are looking for.
if (is_gcc AND CMAKE_CXX_COMPILER_VERSION VERSION_LESS 12)
set(FORTIFY_SOURCE "-D_FORTIFY_SOURCE=2")
else()
# Assuming that the problem is not reproducible with other compilers
set(FORTIFY_SOURCE "-D_FORTIFY_SOURCE=3")
endif()

if(CMAKE_BUILD_TYPE STREQUAL "Debug")
message(WARNING "-D_FORTIFY_SOURCE=3 can only be used with optimization.")
message(WARNING "-D_FORTIFY_SOURCE=3 is not supported.")
message(WARNING "${FORTIFY_SOURCE} can only be used with optimization.")
message(WARNING "${FORTIFY_SOURCE} is not supported.")
else()
# Sanitizers do not work with checked memory functions, such as
# __memset_chk. We do not build release packages with sanitizers, so just
# avoid -D_FORTIFY_SOURCE=3 under LLVM_USE_SANITIZER.
# avoid -D_FORTIFY_SOURCE=N under LLVM_USE_SANITIZER.
if(NOT LLVM_USE_SANITIZER)
message(STATUS "Building with -D_FORTIFY_SOURCE=3")
add_definitions(-D_FORTIFY_SOURCE=3)
message(STATUS "Building with ${FORTIFY_SOURCE}")
add_definitions(${FORTIFY_SOURCE})
else()
message(
WARNING "-D_FORTIFY_SOURCE=3 dropped due to LLVM_USE_SANITIZER.")
WARNING "${FORTIFY_SOURCE} dropped due to LLVM_USE_SANITIZER.")
endif()
endif()
endif()

if(LLVM_ON_UNIX)
if(LLVM_ENABLE_ASSERTIONS)
add_definitions(-D_GLIBCXX_ASSERTIONS)
endif()
Expand Down
8 changes: 7 additions & 1 deletion unified-runtime/cmake/helpers.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,13 @@ endif()

function(add_ur_target_compile_options name)
if(NOT MSVC)
target_compile_definitions(${name} PRIVATE -D_FORTIFY_SOURCE=2)
if (NOT LLVM_ENABLE_PROJECTS)
# If UR is built as part of LLVM (i.e. as part of SYCL), then
# _FORTIFY_SOURCE will be set globally in advance to a potentially
# different value. To avoid redefinition errors, only set the
# macro for a "standalone" build.
target_compile_definitions(${name} PRIVATE -D_FORTIFY_SOURCE=2)
endif()
target_compile_options(${name} PRIVATE
# Warning options
-Wall
Expand Down