@@ -41,7 +41,6 @@ function(sourcemeta_add_default_options visibility target)
4141 $<$<OR :$<COMPILE_LANGUAGE :CXX >,$<COMPILE_LANGUAGE :OBJCXX >>:-Woverloaded -virtual >
4242 $<$<OR :$<COMPILE_LANGUAGE :CXX >,$<COMPILE_LANGUAGE :OBJCXX >>:-Winvalid -offsetof >
4343 -funroll-loops
44- -fstrict-aliasing
4544 -ftree-vectorize
4645
4746 # To improve how much GCC/Clang will vectorize
@@ -51,7 +50,42 @@ 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+ -fstrict-flex-arrays=3 )
61+
62+ # Control-flow protection: requires hardware and OS support
63+ if (CMAKE_SYSTEM_PROCESSOR STREQUAL "x86_64" )
64+ # -fcf-protection uses Intel CET (Control-flow Enforcement Technology)
65+ # Requires OS kernel support, primarily available on Linux
66+ if (LINUX )
67+ target_compile_options ("${target} " ${visibility} -fcf-protection=full )
68+ endif ()
69+ elseif (CMAKE_SYSTEM_PROCESSOR STREQUAL "aarch64" OR CMAKE_SYSTEM_PROCESSOR STREQUAL "arm64" )
70+ # -mbranch-protection uses ARM BTI/PAC, requires Linux kernel 5.8+
71+ if (LINUX )
72+ target_compile_options ("${target} " ${visibility} -mbranch-protection=standard )
73+ endif ()
74+ endif ()
75+
76+ # _FORTIFY_SOURCE requires optimization (-O1 or higher), so only enable in Release builds
77+ # First undefine to avoid conflicts, then define
78+ target_compile_options ("${target} " ${visibility}
79+ $<$<CONFIG :Release >:-U_FORTIFY_SOURCE >
80+ $<$<CONFIG :RelWithDebInfo >:-U_FORTIFY_SOURCE >)
81+ target_compile_definitions ("${target} " ${visibility}
82+ $<$<CONFIG :Release >:_FORTIFY_SOURCE =3>
83+ $<$<CONFIG :RelWithDebInfo >:_FORTIFY_SOURCE =3>)
84+
85+ # _GLIBCXX_ASSERTIONS is libstdc++ (GNU) specific, not applicable to libc++ (LLVM/macOS)
86+ if (NOT APPLE AND SOURCEMETA_COMPILER_GCC)
87+ target_compile_definitions ("${target} " ${visibility} $<$<CONFIG :Debug >:_GLIBCXX_ASSERTIONS >)
88+ endif ()
5589 endif ()
5690
5791 if (SOURCEMETA_COMPILER_LLVM)
@@ -80,6 +114,11 @@ function(sourcemeta_add_default_options visibility target)
80114 -fvectorize
81115 # Enable vectorization of straight-line code for performance
82116 -fslp-vectorize )
117+
118+ # See https://best.openssf.org/Compiler-Hardening-Guides/Compiler-Options-Hardening-Guide-for-C-and-C++.html
119+ target_compile_options ("${target} " ${visibility}
120+ $<$<CONFIG :Release >:-fno -delete -null -pointer -checks -fno -strict -aliasing -ftrivial -auto -var -init =zero >
121+ $<$<CONFIG :RelWithDebInfo >:-fno -delete -null -pointer -checks -fno -strict -aliasing -ftrivial -auto -var -init =zero >)
83122 elseif (SOURCEMETA_COMPILER_GCC)
84123 target_compile_options ("${target} " ${visibility}
85124 -fno-trapping-math
@@ -88,7 +127,17 @@ function(sourcemeta_add_default_options visibility target)
88127 # GCC seems to print a lot of false-positives here
89128 -Wno-free-nonheap-object
90129 # Disables runtime type information
91- $<$<OR :$<COMPILE_LANGUAGE :CXX >,$<COMPILE_LANGUAGE :OBJCXX >>:-fno -rtti >)
130+ $<$<OR :$<COMPILE_LANGUAGE :CXX >,$<COMPILE_LANGUAGE :OBJCXX >>:-fno -rtti >
131+
132+ # See https://best.openssf.org/Compiler-Hardening-Guides/Compiler-Options-Hardening-Guide-for-C-and-C++.html
133+ -Wtrampolines
134+ -Wbidi-chars=any
135+ -fstack-clash-protection )
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 -aliasing -ftrivial -auto -var -init =zero >
140+ $<$<CONFIG :RelWithDebInfo >:-fno -delete -null -pointer -checks -fno -strict -aliasing -ftrivial -auto -var -init =zero >)
92141 endif ()
93142endfunction ()
94143
0 commit comments