Skip to content

Commit 6c15c4b

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

File tree

2 files changed

+75
-3
lines changed

2 files changed

+75
-3
lines changed

cmake/common/compiler/options.cmake

Lines changed: 52 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,6 @@ function(sourcemeta_add_default_options visibility target)
4141
-Woverloaded-virtual
4242
-Winvalid-offsetof
4343
-funroll-loops
44-
-fstrict-aliasing
4544
-ftree-vectorize
4645

4746
# To improve how much GCC/Clang will vectorize
@@ -51,7 +50,41 @@ function(sourcemeta_add_default_options visibility target)
5150
# multiplication wraps around using twos-complement representation
5251
# See https://users.cs.utah.edu/~regehr/papers/overflow12.pdf
5352
# See https://www.postgresql.org/message-id/[email protected]
54-
-fwrapv)
53+
-fwrapv
54+
55+
# See https://best.openssf.org/Compiler-Hardening-Guides/Compiler-Options-Hardening-Guide-for-C-and-C++.html
56+
-Wformat
57+
-Wformat=2
58+
-Werror=format-security
59+
-fstack-protector-strong)
60+
61+
# Control-flow protection: requires hardware and OS support
62+
if(CMAKE_SYSTEM_PROCESSOR STREQUAL "x86_64")
63+
# -fcf-protection uses Intel CET (Control-flow Enforcement Technology)
64+
# Requires OS kernel support, primarily available on Linux
65+
if(LINUX)
66+
target_compile_options("${target}" ${visibility} -fcf-protection=full)
67+
endif()
68+
elseif(CMAKE_SYSTEM_PROCESSOR STREQUAL "aarch64" OR CMAKE_SYSTEM_PROCESSOR STREQUAL "arm64")
69+
# -mbranch-protection uses ARM BTI/PAC, requires Linux kernel 5.8+
70+
if(LINUX)
71+
target_compile_options("${target}" ${visibility} -mbranch-protection=standard)
72+
endif()
73+
endif()
74+
75+
# _FORTIFY_SOURCE requires optimization (-O1 or higher), so only enable in Release builds
76+
# First undefine to avoid conflicts, then define
77+
target_compile_options("${target}" ${visibility}
78+
$<$<CONFIG:Release>:-U_FORTIFY_SOURCE>
79+
$<$<CONFIG:RelWithDebInfo>:-U_FORTIFY_SOURCE>)
80+
target_compile_definitions("${target}" ${visibility}
81+
$<$<CONFIG:Release>:_FORTIFY_SOURCE=3>
82+
$<$<CONFIG:RelWithDebInfo>:_FORTIFY_SOURCE=3>)
83+
84+
# _GLIBCXX_ASSERTIONS is libstdc++ (GNU) specific, not applicable to libc++ (LLVM/macOS)
85+
if(NOT APPLE AND SOURCEMETA_COMPILER_GCC)
86+
target_compile_definitions("${target}" ${visibility} $<$<CONFIG:Debug>:_GLIBCXX_ASSERTIONS>)
87+
endif()
5588
endif()
5689

5790
if(SOURCEMETA_COMPILER_LLVM)
@@ -80,6 +113,11 @@ function(sourcemeta_add_default_options visibility target)
80113
-fvectorize
81114
# Enable vectorization of straight-line code for performance
82115
-fslp-vectorize)
116+
117+
# See https://best.openssf.org/Compiler-Hardening-Guides/Compiler-Options-Hardening-Guide-for-C-and-C++.html
118+
target_compile_options("${target}" ${visibility}
119+
$<$<CONFIG:Release>:-fno-delete-null-pointer-checks -fno-strict-aliasing -ftrivial-auto-var-init=zero>
120+
$<$<CONFIG:RelWithDebInfo>:-fno-delete-null-pointer-checks -fno-strict-aliasing -ftrivial-auto-var-init=zero>)
83121
elseif(SOURCEMETA_COMPILER_GCC)
84122
target_compile_options("${target}" ${visibility}
85123
-fno-trapping-math
@@ -88,7 +126,18 @@ function(sourcemeta_add_default_options visibility target)
88126
# GCC seems to print a lot of false-positives here
89127
-Wno-free-nonheap-object
90128
# Disables runtime type information
91-
-fno-rtti)
129+
-fno-rtti
130+
131+
# See https://best.openssf.org/Compiler-Hardening-Guides/Compiler-Options-Hardening-Guide-for-C-and-C++.html
132+
-Wtrampolines
133+
-Wbidi-chars=any
134+
-fstack-clash-protection
135+
-fstrict-flex-arrays=3)
136+
137+
# See https://best.openssf.org/Compiler-Hardening-Guides/Compiler-Options-Hardening-Guide-for-C-and-C++.html
138+
target_compile_options("${target}" ${visibility}
139+
$<$<CONFIG:Release>:-fno-delete-null-pointer-checks -fno-strict-overflow -fno-strict-aliasing -ftrivial-auto-var-init=zero>
140+
$<$<CONFIG:RelWithDebInfo>:-fno-delete-null-pointer-checks -fno-strict-overflow -fno-strict-aliasing -ftrivial-auto-var-init=zero>)
92141
endif()
93142
endfunction()
94143

cmake/common/targets/executable.cmake

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,5 +30,28 @@ 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+
# Position Independent Executable (PIE) for ASLR support
36+
if(SOURCEMETA_COMPILER_LLVM OR SOURCEMETA_COMPILER_GCC)
37+
target_compile_options(${TARGET_NAME} PRIVATE
38+
$<$<CONFIG:Release>:-fPIE>
39+
$<$<CONFIG:RelWithDebInfo>:-fPIE>)
40+
target_link_options(${TARGET_NAME} PRIVATE
41+
$<$<CONFIG:Release>:-pie>
42+
$<$<CONFIG:RelWithDebInfo>:-pie>)
43+
endif()
44+
45+
# Linux-specific ELF linker hardening options
46+
if(LINUX AND (SOURCEMETA_COMPILER_LLVM OR SOURCEMETA_COMPILER_GCC))
47+
target_link_options(${TARGET_NAME} PRIVATE
48+
"LINKER:-z,nodlopen"
49+
"LINKER:-z,noexecstack"
50+
"LINKER:-z,relro"
51+
"LINKER:-z,now"
52+
"LINKER:--as-needed"
53+
"LINKER:--no-copy-dt-needed-entries")
54+
endif()
55+
3356
set_target_properties("${TARGET_NAME}" PROPERTIES FOLDER "${FOLDER_NAME}")
3457
endfunction()

0 commit comments

Comments
 (0)