@@ -63,6 +63,12 @@ if(CMAKE_SYSTEM_NAME STREQUAL Linux)
6363 check_cxx_compiler_flag("-fstack-clash-protection" CXX_HAS_FSTACK_CLASH_PROTECTION)
6464endif ()
6565
66+ if (UR_USE_CFI AND UR_USE_ASAN)
67+ message (WARNING "Both UR_USE_CFI and UR_USE_ASAN are ON. "
68+ "Due to build errors, this is unsupported; CFI checks will be disabled" )
69+ set (UR_USE_CFI OFF )
70+ endif ()
71+
6672if (UR_USE_CFI)
6773 set (SAVED_CMAKE_REQUIRED_FLAGS ${CMAKE_REQUIRED_FLAGS} )
6874 set (CMAKE_REQUIRED_FLAGS "-flto -fvisibility=hidden" )
@@ -73,6 +79,13 @@ else()
7379 set (CXX_HAS_CFI_SANITIZE OFF )
7480endif ()
7581
82+ set (CFI_FLAGS "" )
83+ if (CFI_HAS_CFI_SANITIZE)
84+ # cfi-icall requires called functions in shared libraries to also be built with cfi-icall, which we can't
85+ # guarantee. -fsanitize=cfi depends on -flto
86+ set (CFI_FLAGS "-flto -fsanitize=cfi -fno-sanitize=cfi-icall -fsanitize-ignorelist=${CMAKE_SOURCE_DIR} /sanitizer-ignorelist.txt" )
87+ endif ()
88+
7689function (add_ur_target_compile_options name )
7790 if (NOT MSVC )
7891 target_compile_definitions (${name} PRIVATE -D_FORTIFY_SOURCE=2)
@@ -89,9 +102,8 @@ function(add_ur_target_compile_options name)
89102 -fPIC
90103 -fstack-protector-strong
91104 -fvisibility=hidden
92- # cfi-icall requires called functions in shared libraries to also be built with cfi-icall, which we can't
93- # guarantee. -fsanitize=cfi depends on -flto
94- $<$<BOOL :${CXX_HAS_CFI_SANITIZE} >:-flto -fsanitize=cfi -fno-sanitize=cfi-icall>
105+
106+ ${CFI_FLAGS}
95107 $<$<BOOL :${CXX_HAS_FCF_PROTECTION_FULL} >:-fcf-protection=full>
96108 $<$<BOOL :${CXX_HAS_FSTACK_CLASH_PROTECTION} >:-fstack-clash-protection>
97109
@@ -108,18 +120,28 @@ function(add_ur_target_compile_options name)
108120 elseif (MSVC )
109121 target_compile_options (${name} PRIVATE
110122 $<$<CXX_COMPILER_ID:MSVC >:/MP> # clang-cl.exe does not support /MP
111- /W3
112123 /MD $<$<CONFIG:Debug>:d>
113- /GS
114- /DWIN32_LEAN_AND_MEAN
115- /DNOMINMAX
124+
125+ /W3
126+ /GS # Enable: Buffer security check
127+ /Gy # Enable: Function-level linking
128+
129+ $<$<CONFIG:Release>:/sdl> # Enable: Additional SDL checks
130+ $<$<CXX_COMPILER_ID:MSVC >:/Qspectre> # Enable: Mitigate Spectre variant 1 vulnerabilities
131+
132+ /wd4267 # Disable: 'var' : conversion from 'size_t' to 'type', possible loss of data
133+ /wd6244 # Disable: local declaration of 'variable' hides previous declaration
134+ /wd6246 # Disable: local declaration of 'variable' hides declaration of same name in outer scope
135+ )
136+
137+ target_compile_definitions (${name} PRIVATE
138+ WIN32_LEAN_AND_MEAN NOMINMAX # Cajole Windows.h to define fewer symbols
139+ _CRT_SECURE_NO_WARNINGS # Slience warnings about getenv
116140 )
117141
118142 if (UR_DEVELOPER_MODE)
119- # _CRT_SECURE_NO_WARNINGS used mainly because of getenv
120- # C4267: The compiler detected a conversion from size_t to a smaller type.
121143 target_compile_options (${name} PRIVATE
122- /WX /GS /D_CRT_SECURE_NO_WARNINGS /wd4267
144+ /WX # Enable: Treat all warnings as errors
123145 )
124146 endif ()
125147 endif ()
@@ -129,7 +151,7 @@ function(add_ur_target_link_options name)
129151 if (NOT MSVC )
130152 if (NOT APPLE )
131153 target_link_options (${name} PRIVATE
132- $<$< BOOL : ${CXX_HAS_CFI_SANITIZE} >:-flto -fsanitize=cfi -fno-sanitize=cfi-icall>
154+ ${CFI_FLAGS}
133155 "LINKER:-z,relro,-z,now,-z,noexecstack"
134156 )
135157 if (UR_DEVELOPER_MODE)
@@ -143,9 +165,12 @@ function(add_ur_target_link_options name)
143165 endif ()
144166 elseif (MSVC )
145167 target_link_options (${name} PRIVATE
146- LINKER:/DYNAMICBASE
147- LINKER:/HIGHENTROPYVA
148- LINKER:/NXCOMPAT
168+ LINKER:/DYNAMICBASE # Enable: Modify header to indicate ASLR should be use
169+ LINKER:/HIGHENTROPYVA # Enable: High-entropy address space layout randomization (ASLR)
170+ $<$<CONFIG:Release>:
171+ LINKER:/NXCOMPAT # Enable: Data Execution Prevention
172+ LINKER:/LTCG # Enable: Link-time code generation
173+ >
149174 )
150175 endif ()
151176endfunction ()
0 commit comments