Skip to content

Commit 1339fec

Browse files
committed
Review compiler options for Clang and GCC
Signed-off-by: Juan Cruz Viotti <jv@jviotti.com>
1 parent 639d617 commit 1339fec

File tree

2 files changed

+48
-2
lines changed

2 files changed

+48
-2
lines changed

cmake/common/compiler/options.cmake

+33-2
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,23 @@ function(sourcemeta_add_default_options visibility target)
5151
# multiplication wraps around using twos-complement representation
5252
# See https://users.cs.utah.edu/~regehr/papers/overflow12.pdf
5353
# See https://www.postgresql.org/message-id/1689.1134422394@sss.pgh.pa.us
54-
-fwrapv)
54+
-fwrapv
55+
56+
# See https://best.openssf.org/Compiler-Hardening-Guides/Compiler-Options-Hardening-Guide-for-C-and-C++.html
57+
-Wformat
58+
-Wformat=2
59+
-Werror=format-security
60+
-fstrict-flex-arrays=3
61+
-fstack-protector-strong)
62+
63+
if(CMAKE_SYSTEM_PROCESSOR STREQUAL "x86_64")
64+
target_compile_options("${target}" ${visibility} -fcf-protection=full)
65+
elseif(CMAKE_SYSTEM_PROCESSOR STREQUAL "aarch64" OR CMAKE_SYSTEM_PROCESSOR STREQUAL "arm64")
66+
target_compile_options("${target}" ${visibility} -mbranch-protection=standard)
67+
endif()
68+
69+
target_compile_definitions("${target}" ${visibility} _FORTIFY_SOURCE=3)
70+
target_compile_definitions("${target}" ${visibility} $<$<CONFIG:Debug>:_GLIBCXX_ASSERTIONS>)
5571
endif()
5672

5773
if(SOURCEMETA_COMPILER_LLVM)
@@ -80,6 +96,11 @@ function(sourcemeta_add_default_options visibility target)
8096
-fvectorize
8197
# Enable vectorization of straight-line code for performance
8298
-fslp-vectorize)
99+
100+
# See https://best.openssf.org/Compiler-Hardening-Guides/Compiler-Options-Hardening-Guide-for-C-and-C++.html
101+
target_compile_options("${target}" ${visibility}
102+
$<$<CONFIG:Release>:-fno-delete-null-pointer-checks -fno-strict-aliasing -ftrivial-auto-var-init=zero>
103+
$<$<CONFIG:RelWithDebInfo>:-fno-delete-null-pointer-checks -fno-strict-aliasing -ftrivial-auto-var-init=zero>)
83104
elseif(SOURCEMETA_COMPILER_GCC)
84105
target_compile_options("${target}" ${visibility}
85106
-fno-trapping-math
@@ -88,7 +109,17 @@ function(sourcemeta_add_default_options visibility target)
88109
# GCC seems to print a lot of false-positives here
89110
-Wno-free-nonheap-object
90111
# Disables runtime type information
91-
-fno-rtti)
112+
-fno-rtti
113+
114+
# See https://best.openssf.org/Compiler-Hardening-Guides/Compiler-Options-Hardening-Guide-for-C-and-C++.html
115+
-Wtrampolines
116+
-Wbidi-chars=any
117+
-fstack-clash-protection)
118+
119+
# See https://best.openssf.org/Compiler-Hardening-Guides/Compiler-Options-Hardening-Guide-for-C-and-C++.html
120+
target_compile_options("${target}" ${visibility}
121+
$<$<CONFIG:Release>:-fno-delete-null-pointer-checks -fno-strict-overflow -fno-strict-aliasing -ftrivial-auto-var-init=zero>
122+
$<$<CONFIG:RelWithDebInfo>:-fno-delete-null-pointer-checks -fno-strict-overflow -fno-strict-aliasing -ftrivial-auto-var-init=zero>)
92123
endif()
93124
endfunction()
94125

cmake/common/targets/executable.cmake

+15
Original file line numberDiff line numberDiff line change
@@ -30,5 +30,20 @@ function(sourcemeta_executable)
3030

3131
add_executable("${TARGET_NAME}" ${SOURCEMETA_EXECUTABLE_SOURCES})
3232
sourcemeta_add_default_options(PRIVATE ${TARGET_NAME})
33+
34+
# See https://best.openssf.org/Compiler-Hardening-Guides/Compiler-Options-Hardening-Guide-for-C-and-C++.html
35+
if(SOURCEMETA_COMPILER_LLVM OR SOURCEMETA_COMPILER_GCC)
36+
target_compile_options(${TARGET_NAME} PRIVATE
37+
$<$<CONFIG:Release>:-fPIE -pie>
38+
$<$<CONFIG:RelWithDebInfo>:-fPIE -pie>)
39+
target_link_options(${TARGET_NAME} PRIVATE
40+
-Wl,-z,nodlopen
41+
-Wl,-z,noexecstack
42+
-Wl,-z,relro
43+
-Wl,-z,now
44+
-Wl,--as-needed
45+
-Wl,--no-copy-dt-needed-entries)
46+
endif()
47+
3348
set_target_properties("${TARGET_NAME}" PROPERTIES FOLDER "${FOLDER_NAME}")
3449
endfunction()

0 commit comments

Comments
 (0)