Skip to content

Commit cf85fbf

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

File tree

3 files changed

+68
-10
lines changed

3 files changed

+68
-10
lines changed

cmake/FindPCRE2.cmake

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,11 @@ if(NOT PCRE2_FOUND)
103103
target_compile_options(sljit PRIVATE -Wno-conditional-uninitialized)
104104
endif()
105105

106+
if(SOURCEMETA_COMPILER_GCC)
107+
target_compile_options(sljit PRIVATE -Wno-stringop-overflow)
108+
target_compile_options(sljit PRIVATE -fstrict-flex-arrays=0)
109+
endif()
110+
106111
if(SOURCEMETA_COMPILER_MSVC)
107112
target_compile_options(sljit PRIVATE /sdl-)
108113
target_compile_options(sljit PRIVATE /wd4701)
@@ -140,6 +145,11 @@ if(NOT PCRE2_FOUND)
140145
target_compile_options(pcre2 PRIVATE -Wno-type-limits)
141146
endif()
142147

148+
if(SOURCEMETA_COMPILER_GCC)
149+
target_compile_options(pcre2 PRIVATE -Wno-stringop-overflow)
150+
target_compile_options(pcre2 PRIVATE -fstrict-flex-arrays=0)
151+
endif()
152+
143153
if(SOURCEMETA_COMPILER_MSVC)
144154
target_compile_options(pcre2 PRIVATE /sdl-)
145155
target_compile_options(pcre2 PRIVATE /wd4127)

cmake/common/compiler/options.cmake

Lines changed: 55 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,9 @@ function(sourcemeta_add_default_options visibility target)
77
$<$<OR:$<COMPILE_LANGUAGE:C>,$<COMPILE_LANGUAGE:CXX>>:/W4>
88
$<$<OR:$<COMPILE_LANGUAGE:C>,$<COMPILE_LANGUAGE:CXX>>:/WL>
99
$<$<OR:$<COMPILE_LANGUAGE:C>,$<COMPILE_LANGUAGE:CXX>>:/MP>
10-
$<$<OR:$<COMPILE_LANGUAGE:C>,$<COMPILE_LANGUAGE:CXX>>:/sdl>)
10+
$<$<OR:$<COMPILE_LANGUAGE:C>,$<COMPILE_LANGUAGE:CXX>>:/sdl>
11+
# See https://learn.microsoft.com/en-us/cpp/build/reference/guard-enable-control-flow-guard
12+
$<$<OR:$<COMPILE_LANGUAGE:C>,$<COMPILE_LANGUAGE:CXX>>:/guard:cf>)
1113
elseif(SOURCEMETA_COMPILER_LLVM OR SOURCEMETA_COMPILER_GCC)
1214
target_compile_options("${target}" ${visibility}
1315
-Wall
@@ -41,7 +43,6 @@ function(sourcemeta_add_default_options visibility target)
4143
$<$<OR:$<COMPILE_LANGUAGE:CXX>,$<COMPILE_LANGUAGE:OBJCXX>>:-Woverloaded-virtual>
4244
$<$<OR:$<COMPILE_LANGUAGE:CXX>,$<COMPILE_LANGUAGE:OBJCXX>>:-Winvalid-offsetof>
4345
-funroll-loops
44-
-fstrict-aliasing
4546
-ftree-vectorize
4647

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

5793
if(SOURCEMETA_COMPILER_LLVM)
@@ -80,6 +116,11 @@ function(sourcemeta_add_default_options visibility target)
80116
-fvectorize
81117
# Enable vectorization of straight-line code for performance
82118
-fslp-vectorize)
119+
120+
# See https://best.openssf.org/Compiler-Hardening-Guides/Compiler-Options-Hardening-Guide-for-C-and-C++.html
121+
target_compile_options("${target}" ${visibility}
122+
$<$<CONFIG:Release>:-fno-delete-null-pointer-checks;-fno-strict-aliasing;-ftrivial-auto-var-init=zero>
123+
$<$<CONFIG:RelWithDebInfo>:-fno-delete-null-pointer-checks;-fno-strict-aliasing;-ftrivial-auto-var-init=zero>)
83124
elseif(SOURCEMETA_COMPILER_GCC)
84125
target_compile_options("${target}" ${visibility}
85126
-fno-trapping-math
@@ -88,7 +129,17 @@ function(sourcemeta_add_default_options visibility target)
88129
# GCC seems to print a lot of false-positives here
89130
-Wno-free-nonheap-object
90131
# Disables runtime type information
91-
$<$<OR:$<COMPILE_LANGUAGE:CXX>,$<COMPILE_LANGUAGE:OBJCXX>>:-fno-rtti>)
132+
$<$<OR:$<COMPILE_LANGUAGE:CXX>,$<COMPILE_LANGUAGE:OBJCXX>>:-fno-rtti>
133+
134+
# See https://best.openssf.org/Compiler-Hardening-Guides/Compiler-Options-Hardening-Guide-for-C-and-C++.html
135+
-Wtrampolines
136+
-Wbidi-chars=any
137+
-fstack-clash-protection)
138+
139+
# See https://best.openssf.org/Compiler-Hardening-Guides/Compiler-Options-Hardening-Guide-for-C-and-C++.html
140+
target_compile_options("${target}" ${visibility}
141+
$<$<CONFIG:Release>:-fno-delete-null-pointer-checks -fno-strict-aliasing -ftrivial-auto-var-init=zero>
142+
$<$<CONFIG:RelWithDebInfo>:-fno-delete-null-pointer-checks -fno-strict-aliasing -ftrivial-auto-var-init=zero>)
92143
endif()
93144
endfunction()
94145

cmake/common/targets/executable.cmake

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -32,12 +32,9 @@ function(sourcemeta_executable)
3232
sourcemeta_add_default_options(PRIVATE ${TARGET_NAME})
3333

3434
# 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
35+
# PIE linker flags for ASLR support. The compile-time -fPIC is already
36+
# enabled globally via CMAKE_POSITION_INDEPENDENT_CODE in defaults.cmake.
3637
if(SOURCEMETA_COMPILER_LLVM OR SOURCEMETA_COMPILER_GCC)
37-
target_compile_options(${TARGET_NAME} PRIVATE
38-
$<$<CONFIG:Release>:-fPIE>
39-
$<$<CONFIG:RelWithDebInfo>:-fPIE>
40-
$<$<CONFIG:MinSizeRel>:-fPIE>)
4138
target_link_options(${TARGET_NAME} PRIVATE
4239
$<$<CONFIG:Release>:-pie>
4340
$<$<CONFIG:RelWithDebInfo>:-pie>
@@ -46,8 +43,8 @@ function(sourcemeta_executable)
4643

4744
# See https://learn.microsoft.com/en-us/cpp/build/reference/guard-enable-control-flow-guard
4845
# See https://learn.microsoft.com/en-us/cpp/build/reference/cetcompat
46+
# The /guard:cf compile flag is in sourcemeta_add_default_options()
4947
if(SOURCEMETA_COMPILER_MSVC)
50-
target_compile_options(${TARGET_NAME} PRIVATE /guard:cf)
5148
target_link_options(${TARGET_NAME} PRIVATE /guard:cf /CETCOMPAT)
5249
endif()
5350

0 commit comments

Comments
 (0)