@@ -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 ()
93142endfunction ()
94143
0 commit comments