@@ -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 ()
93144endfunction ()
94145
0 commit comments