Skip to content

Commit e7f466a

Browse files
committed
Review compiler options for Clang and GCC
Signed-off-by: Juan Cruz Viotti <[email protected]>
1 parent 9ece629 commit e7f466a

File tree

2 files changed

+54
-2
lines changed

2 files changed

+54
-2
lines changed

cmake/common/compiler/options.cmake

+33-2
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,22 @@ 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/[email protected]
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+
-fstack-protector-strong)
61+
62+
if(CMAKE_SYSTEM_PROCESSOR STREQUAL "x86_64")
63+
target_compile_options("${target}" ${visibility} -fcf-protection=full)
64+
elseif(CMAKE_SYSTEM_PROCESSOR STREQUAL "aarch64" OR CMAKE_SYSTEM_PROCESSOR STREQUAL "arm64")
65+
target_compile_options("${target}" ${visibility} -mbranch-protection=standard)
66+
endif()
67+
68+
target_compile_definitions("${target}" ${visibility} _FORTIFY_SOURCE=3)
69+
target_compile_definitions("${target}" ${visibility} $<$<CONFIG:Debug>:_GLIBCXX_ASSERTIONS>)
5570
endif()
5671

5772
if(SOURCEMETA_COMPILER_LLVM)
@@ -80,6 +95,11 @@ function(sourcemeta_add_default_options visibility target)
8095
-fvectorize
8196
# Enable vectorization of straight-line code for performance
8297
-fslp-vectorize)
98+
99+
# See https://best.openssf.org/Compiler-Hardening-Guides/Compiler-Options-Hardening-Guide-for-C-and-C++.html
100+
target_compile_options("${target}" ${visibility}
101+
$<$<CONFIG:Release>:-fno-delete-null-pointer-checks -fno-strict-aliasing -ftrivial-auto-var-init=zero>
102+
$<$<CONFIG:RelWithDebInfo>:-fno-delete-null-pointer-checks -fno-strict-aliasing -ftrivial-auto-var-init=zero>)
83103
elseif(SOURCEMETA_COMPILER_GCC)
84104
target_compile_options("${target}" ${visibility}
85105
-fno-trapping-math
@@ -88,7 +108,18 @@ function(sourcemeta_add_default_options visibility target)
88108
# GCC seems to print a lot of false-positives here
89109
-Wno-free-nonheap-object
90110
# Disables runtime type information
91-
-fno-rtti)
111+
-fno-rtti
112+
113+
# See https://best.openssf.org/Compiler-Hardening-Guides/Compiler-Options-Hardening-Guide-for-C-and-C++.html
114+
-Wtrampolines
115+
-Wbidi-chars=any
116+
-fstack-clash-protection
117+
-fstrict-flex-arrays=3)
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

+21
Original file line numberDiff line numberDiff line change
@@ -30,5 +30,26 @@ 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)
36+
target_compile_options(${TARGET_NAME} PRIVATE
37+
$<$<CONFIG:Release>:-fPIE>
38+
$<$<CONFIG:RelWithDebInfo>:-fPIE>)
39+
elseif(SOURCEMETA_COMPILER_GCC)
40+
target_compile_options(${TARGET_NAME} PRIVATE
41+
$<$<CONFIG:Release>:-fPIE -pie>
42+
$<$<CONFIG:RelWithDebInfo>:-fPIE -pie>)
43+
if(NOT APPLE)
44+
target_link_options(${TARGET_NAME} PRIVATE
45+
"LINKER:-z,nodlopen"
46+
"LINKER:-z,noexecstack"
47+
"LINKER:-z,relro"
48+
"LINKER:-z,now"
49+
"LINKER:--as-needed"
50+
"LINKER:--no-copy-dt-needed-entries")
51+
endif()
52+
endif()
53+
3354
set_target_properties("${TARGET_NAME}" PROPERTIES FOLDER "${FOLDER_NAME}")
3455
endfunction()

0 commit comments

Comments
 (0)