Skip to content

Commit 3873dc9

Browse files
committed
Add SDL hardening flags for MSVC
Enable compiler flags which harden MSVC builds against mistakes and vulnerabilities.
1 parent a563456 commit 3873dc9

File tree

2 files changed

+24
-10
lines changed

2 files changed

+24
-10
lines changed

cmake/helpers.cmake

Lines changed: 23 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -114,18 +114,28 @@ function(add_ur_target_compile_options name)
114114
elseif(MSVC)
115115
target_compile_options(${name} PRIVATE
116116
$<$<CXX_COMPILER_ID:MSVC>:/MP> # clang-cl.exe does not support /MP
117-
/W3
118117
/MD$<$<CONFIG:Debug>:d>
119-
/GS
120-
/DWIN32_LEAN_AND_MEAN
121-
/DNOMINMAX
118+
119+
/W3
120+
/GS # Enable: Buffer security check
121+
/Gy # Enable: Function-level linking
122+
123+
$<$<CONFIG:Release>:/sdl> # Enable: Additional SDL checks
124+
$<$<CXX_COMPILER_ID:MSVC>:/Qspectre> # Enable: Mitigate Spectre variant 1 vulnerabilities
125+
126+
/wd4267 # Disable: 'var' : conversion from 'size_t' to 'type', possible loss of data
127+
/wd6244 # Disable: local declaration of 'variable' hides previous declaration
128+
/wd6246 # Disable: local declaration of 'variable' hides declaration of same name in outer scope
129+
)
130+
131+
target_compile_definitions(${name} PRIVATE
132+
WIN32_LEAN_AND_MEAN NOMINMAX # Cajole Windows.h to define fewer symbols
133+
_CRT_SECURE_NO_WARNINGS # Slience warnings about getenv
122134
)
123135

124136
if(UR_DEVELOPER_MODE)
125-
# _CRT_SECURE_NO_WARNINGS used mainly because of getenv
126-
# C4267: The compiler detected a conversion from size_t to a smaller type.
127137
target_compile_options(${name} PRIVATE
128-
/WX /GS /D_CRT_SECURE_NO_WARNINGS /wd4267
138+
/WX # Enable: Treat all warnings as errors
129139
)
130140
endif()
131141
endif()
@@ -149,9 +159,12 @@ function(add_ur_target_link_options name)
149159
endif()
150160
elseif(MSVC)
151161
target_link_options(${name} PRIVATE
152-
LINKER:/DYNAMICBASE
153-
LINKER:/HIGHENTROPYVA
154-
LINKER:/NXCOMPAT
162+
LINKER:/DYNAMICBASE # Enable: Modify header to indicate ASLR should be use
163+
LINKER:/HIGHENTROPYVA # Enable: High-entropy address space layout randomization (ASLR)
164+
$<$<CONFIG:Release>:
165+
LINKER:/NXCOMPAT # Enable: Data Execution Prevention
166+
LINKER:/LTCG # Enable: Link-time code generation
167+
>
155168
)
156169
endif()
157170
endfunction()

test/conformance/exp_command_buffer/fixtures.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
#ifndef UR_CONFORMANCE_COMMAND_BUFFER_FIXTURES_H_INCLUDED
77
#define UR_CONFORMANCE_COMMAND_BUFFER_FIXTURES_H_INCLUDED
88

9+
#include <array>
910
#include <uur/fixtures.h>
1011

1112
namespace uur {

0 commit comments

Comments
 (0)