Skip to content

Review compiler options for Clang and GCC #1592

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

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
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
35 changes: 33 additions & 2 deletions cmake/common/compiler/options.cmake
Original file line number Diff line number Diff line change
@@ -51,7 +51,22 @@ function(sourcemeta_add_default_options visibility target)
# multiplication wraps around using twos-complement representation
# See https://users.cs.utah.edu/~regehr/papers/overflow12.pdf
# See https://www.postgresql.org/message-id/[email protected]
-fwrapv)
-fwrapv

# See https://best.openssf.org/Compiler-Hardening-Guides/Compiler-Options-Hardening-Guide-for-C-and-C++.html
-Wformat
-Wformat=2
-Werror=format-security
-fstack-protector-strong)

if(CMAKE_SYSTEM_PROCESSOR STREQUAL "x86_64")
target_compile_options("${target}" ${visibility} -fcf-protection=full)
elseif(CMAKE_SYSTEM_PROCESSOR STREQUAL "aarch64" OR CMAKE_SYSTEM_PROCESSOR STREQUAL "arm64")
target_compile_options("${target}" ${visibility} -mbranch-protection=standard)
endif()

target_compile_definitions("${target}" ${visibility} _FORTIFY_SOURCE=3)
target_compile_definitions("${target}" ${visibility} $<$<CONFIG:Debug>:_GLIBCXX_ASSERTIONS>)
endif()

if(SOURCEMETA_COMPILER_LLVM)
@@ -80,6 +95,11 @@ function(sourcemeta_add_default_options visibility target)
-fvectorize
# Enable vectorization of straight-line code for performance
-fslp-vectorize)

# See https://best.openssf.org/Compiler-Hardening-Guides/Compiler-Options-Hardening-Guide-for-C-and-C++.html
target_compile_options("${target}" ${visibility}
$<$<CONFIG:Release>:-fno-delete-null-pointer-checks -fno-strict-aliasing -ftrivial-auto-var-init=zero>
$<$<CONFIG:RelWithDebInfo>:-fno-delete-null-pointer-checks -fno-strict-aliasing -ftrivial-auto-var-init=zero>)
elseif(SOURCEMETA_COMPILER_GCC)
target_compile_options("${target}" ${visibility}
-fno-trapping-math
@@ -88,7 +108,18 @@ function(sourcemeta_add_default_options visibility target)
# GCC seems to print a lot of false-positives here
-Wno-free-nonheap-object
# Disables runtime type information
-fno-rtti)
-fno-rtti

# See https://best.openssf.org/Compiler-Hardening-Guides/Compiler-Options-Hardening-Guide-for-C-and-C++.html
-Wtrampolines
-Wbidi-chars=any
-fstack-clash-protection
-fstrict-flex-arrays=3)

# See https://best.openssf.org/Compiler-Hardening-Guides/Compiler-Options-Hardening-Guide-for-C-and-C++.html
target_compile_options("${target}" ${visibility}
$<$<CONFIG:Release>:-fno-delete-null-pointer-checks -fno-strict-overflow -fno-strict-aliasing -ftrivial-auto-var-init=zero>
$<$<CONFIG:RelWithDebInfo>:-fno-delete-null-pointer-checks -fno-strict-overflow -fno-strict-aliasing -ftrivial-auto-var-init=zero>)
endif()
endfunction()

21 changes: 21 additions & 0 deletions cmake/common/targets/executable.cmake
Original file line number Diff line number Diff line change
@@ -30,5 +30,26 @@ function(sourcemeta_executable)

add_executable("${TARGET_NAME}" ${SOURCEMETA_EXECUTABLE_SOURCES})
sourcemeta_add_default_options(PRIVATE ${TARGET_NAME})

# See https://best.openssf.org/Compiler-Hardening-Guides/Compiler-Options-Hardening-Guide-for-C-and-C++.html
if(SOURCEMETA_COMPILER_LLVM)
target_compile_options(${TARGET_NAME} PRIVATE
$<$<CONFIG:Release>:-fPIE>
$<$<CONFIG:RelWithDebInfo>:-fPIE>)
elseif(SOURCEMETA_COMPILER_GCC)
target_compile_options(${TARGET_NAME} PRIVATE
$<$<CONFIG:Release>:-fPIE -pie>
$<$<CONFIG:RelWithDebInfo>:-fPIE -pie>)
if(NOT APPLE)
target_link_options(${TARGET_NAME} PRIVATE
"LINKER:-z,nodlopen"
"LINKER:-z,noexecstack"
"LINKER:-z,relro"
"LINKER:-z,now"
"LINKER:--as-needed"
"LINKER:--no-copy-dt-needed-entries")
endif()
endif()

set_target_properties("${TARGET_NAME}" PROPERTIES FOLDER "${FOLDER_NAME}")
endfunction()