Skip to content

Commit aa53a35

Browse files
committed
Hardening flags
UR_DEVELOPER_MODE has also been changed to only set Werror. The previous other flags set by this variable either have no effect (-fno-omit-frame-pointer) or are now always enabled (-fstack-protector-strong). Some smaller warnings have also been fixed.
1 parent 0112320 commit aa53a35

File tree

9 files changed

+120
-16
lines changed

9 files changed

+120
-16
lines changed

.github/workflows/cmake.yml

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ jobs:
4949
- name: Install apt packages
5050
run: |
5151
sudo apt-get update
52-
sudo apt-get install -y ${{matrix.compiler.c}}
52+
sudo apt-get install -y ${{matrix.compiler.c}} devscripts
5353
5454
- name: Install libhwloc
5555
run: .github/scripts/install_hwloc.sh
@@ -82,6 +82,7 @@ jobs:
8282
8383
- name: Configure CMake
8484
if: matrix.os == 'ubuntu-22.04'
85+
# WEXTRA: https://github.com/oneapi-src/unified-runtime/issues/2109
8586
run: >
8687
cmake
8788
-B${{github.workspace}}/build
@@ -91,13 +92,16 @@ jobs:
9192
-DCMAKE_BUILD_TYPE=${{matrix.build_type}}
9293
-DUR_BUILD_TESTS=ON
9394
-DUR_FORMAT_CPP_STYLE=OFF
95+
-DUR_DEVELOPER_MODE=ON
9496
-DUR_DPCXX=${{github.workspace}}/dpcpp_compiler/bin/clang++
9597
${{matrix.libbacktrace}}
9698
${{matrix.pool_tracking}}
9799
${{matrix.latency_tracking}}
98100
99101
- name: Configure CMake
100102
if: matrix.os == 'ubuntu-20.04'
103+
# WEXTRA: https://github.com/oneapi-src/unified-runtime/issues/2109
104+
# Note: Disable Werror, since 20.04 raises different ones than 22.04
101105
run: >
102106
cmake
103107
-B${{github.workspace}}/build
@@ -107,13 +111,19 @@ jobs:
107111
-DCMAKE_BUILD_TYPE=${{matrix.build_type}}
108112
-DUR_BUILD_TESTS=ON
109113
-DUR_FORMAT_CPP_STYLE=OFF
114+
-DUR_DEVELOPER_MODE=OFF
110115
${{matrix.libbacktrace}}
111116
${{matrix.pool_tracking}}
112117
${{matrix.latency_tracking}}
113118
114119
- name: Build
115120
run: cmake --build ${{github.workspace}}/build -j $(nproc)
116121

122+
- name: Verify hardening flags have been set
123+
run: cmake --build ${{github.workspace}}/build --target verify-hardening
124+
# https://github.com/oneapi-src/unified-runtime/issues/2120
125+
if: ${{ matrix.compiler.cxx != 'clang++' && matrix.os != 'ubuntu-20.04' }}
126+
117127
- name: Test
118128
working-directory: ${{github.workspace}}/build
119129
run: ctest -C ${{matrix.build_type}} --output-on-failure -L "umf|loader|validation|tracing|unit|urtrace"

CMakeLists.txt

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ option(UR_BUILD_EXAMPLES "Build example applications." ON)
3434
option(UR_BUILD_TESTS "Build unit tests." ON)
3535
option(UR_BUILD_TOOLS "build ur tools" ON)
3636
option(UR_FORMAT_CPP_STYLE "format code style of C++ sources" OFF)
37-
option(UR_DEVELOPER_MODE "enable developer checks, treats warnings as errors" OFF)
37+
option(UR_DEVELOPER_MODE "treats warnings as errors" OFF)
3838
option(UR_ENABLE_FAST_SPEC_MODE "enable fast specification generation mode" OFF)
3939
option(UR_USE_ASAN "enable AddressSanitizer" OFF)
4040
option(UR_USE_UBSAN "enable UndefinedBehaviorSanitizer" OFF)
@@ -161,6 +161,12 @@ if(UR_ENABLE_TRACING)
161161
LIBRARY_OUTPUT_DIRECTORY ${CMAKE_LIBRARY_OUTPUT_DIRECTORY}
162162
)
163163

164+
if (NOT MSVC)
165+
# Hardening flags cause issues on Windows
166+
add_ur_target_compile_options(xptifw)
167+
add_ur_target_link_options(xptifw)
168+
endif()
169+
164170
if (UR_STATIC_LOADER)
165171
install(TARGETS xpti xptifw
166172
EXPORT ${PROJECT_NAME}-targets
@@ -269,6 +275,13 @@ add_custom_target(verify-licenses
269275
COMMENT "Verify all files contain a license."
270276
)
271277

278+
# Add hardening check
279+
add_custom_target(verify-hardening
280+
COMMAND "${PROJECT_SOURCE_DIR}/scripts/check-hardening.sh"
281+
${CMAKE_BINARY_DIR}
282+
COMMENT "Check hardening settings on built binaries and libraries"
283+
)
284+
272285
# Add code formatter target
273286
add_custom_target(cppformat)
274287
# ... and all source files to the formatter

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ List of options provided by CMake:
122122
| UR_BUILD_TESTS | Build the tests | ON/OFF | ON |
123123
| UR_BUILD_TOOLS | Build tools | ON/OFF | ON |
124124
| UR_FORMAT_CPP_STYLE | Format code style | ON/OFF | OFF |
125-
| UR_DEVELOPER_MODE | Treat warnings as errors and enables additional checks | ON/OFF | OFF |
125+
| UR_DEVELOPER_MODE | Treat warnings as errors | ON/OFF | OFF |
126126
| UR_ENABLE_FAST_SPEC_MODE | Enable fast specification generation mode | ON/OFF | OFF |
127127
| UR_USE_ASAN | Enable AddressSanitizer | ON/OFF | OFF |
128128
| UR_USE_TSAN | Enable ThreadSanitizer | ON/OFF | OFF |

cmake/helpers.cmake

Lines changed: 32 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -60,26 +60,40 @@ endmacro()
6060

6161
function(add_ur_target_compile_options name)
6262
if(NOT MSVC)
63+
target_compile_definitions(${name} PRIVATE -D_FORTIFY_SOURCE=2)
6364
target_compile_options(${name} PRIVATE
64-
-fPIC
65+
# Warning options
6566
-Wall
6667
-Wpedantic
6768
-Wempty-body
69+
-Wformat
70+
-Wformat-security
6871
-Wunused-parameter
72+
73+
# Hardening options
74+
-fPIC
75+
-fstack-protector-strong
76+
-fvisibility=hidden # Required for -fsanitize=cfi
77+
# -fsanitize=cfi requires -flto, which breaks a lot of things
78+
# See: https://github.com/oneapi-src/unified-runtime/issues/2120
79+
# -flto
80+
# $<$<CXX_COMPILER_ID:Clang,AppleClang>:-fsanitize=cfi>
81+
# -fcf-protection not supported in GCC < 8
82+
$<$<OR:$<NOT:$<CXX_COMPILER_ID:GNU>>,$<VERSION_GREATER_EQUAL:$<CXX_COMPILER_VERSION>,8>>:-fcf-protection=full>
83+
# -fstack-clash-protection is not supported in apple clang or GCC < 8
84+
$<$<AND:$<CXX_COMPILER_ID:GNU>,$<VERSION_GREATER_EQUAL:$<CXX_COMPILER_VERSION>,8>>:-fstack-clash-protection>
85+
$<$<CXX_COMPILER_ID:Clang>:-fstack-clash-protection>
86+
87+
# Colored output
6988
$<$<CXX_COMPILER_ID:GNU>:-fdiagnostics-color=always>
7089
$<$<CXX_COMPILER_ID:Clang,AppleClang>:-fcolor-diagnostics>
7190
)
91+
if (UR_DEVELOPER_MODE)
92+
target_compile_options(${name} PRIVATE -Werror)
93+
endif()
7294
if (CMAKE_BUILD_TYPE STREQUAL "Release")
73-
target_compile_definitions(${name} PRIVATE -D_FORTIFY_SOURCE=2)
7495
target_compile_options(${name} PRIVATE -fvisibility=hidden)
7596
endif()
76-
if(UR_DEVELOPER_MODE)
77-
target_compile_options(${name} PRIVATE
78-
-Werror
79-
-fno-omit-frame-pointer
80-
-fstack-protector-strong
81-
)
82-
endif()
8397
elseif(MSVC)
8498
target_compile_options(${name} PRIVATE
8599
$<$<CXX_COMPILER_ID:MSVC>:/MP> # clang-cl.exe does not support /MP
@@ -103,7 +117,15 @@ endfunction()
103117
function(add_ur_target_link_options name)
104118
if(NOT MSVC)
105119
if (NOT APPLE)
106-
target_link_options(${name} PRIVATE "LINKER:-z,relro,-z,now")
120+
target_link_options(${name} PRIVATE "LINKER:-z,relro,-z,now,-z,noexecstack")
121+
if (UR_DEVELOPER_MODE)
122+
target_link_options(${name} PRIVATE -Werror)
123+
endif()
124+
if (CMAKE_BUILD_TYPE STREQUAL "Release")
125+
target_link_options(${name} PRIVATE
126+
$<$<CXX_COMPILER_ID:GNU>:-pie>
127+
)
128+
endif()
107129
endif()
108130
elseif(MSVC)
109131
target_link_options(${name} PRIVATE

scripts/check-hardening.sh

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
#!/bin/sh
2+
if [ -z $1 ]; then
3+
echo "Usage: $0 builddir" >&2;
4+
exit;
5+
fi
6+
7+
which hardening-check >> /dev/null;
8+
if [ $? != "0" ]; then
9+
echo "hardening-check not found - on Ubuntu it is from the 'devscripts' package." >&2;
10+
exit;
11+
fi
12+
13+
RET=0;
14+
15+
for file in $1/bin/*; do
16+
case "$file" in
17+
*/urtrace)
18+
# This is a python script
19+
true;;
20+
*)
21+
hardening-check -q --nocfprotection --nofortify $file;;
22+
esac
23+
RET=$(($RET + $?))
24+
done;
25+
26+
for file in $1/lib/*.so; do
27+
case "$file" in
28+
*/libOpenCL*)
29+
# This is not built as part of UR
30+
true;;
31+
*/libzeCallMap.so | */libur_mock_headers.so)
32+
# Only used in testing, and are too simple for many of the hardening flags to have an effect.
33+
true;;
34+
*)
35+
hardening-check -q --nocfprotection --nofortify $file;;
36+
esac
37+
RET=$(($RET + $?))
38+
done;
39+
40+
if [ $RET != "0" ]; then
41+
exit 1;
42+
fi

source/adapters/level_zero/CMakeLists.txt

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,21 @@ if(UR_BUILD_ADAPTER_L0)
6666
)
6767
endif()
6868

69+
# Ensure UR flags are propagated to level zero
70+
# Note: UR compile options cause issues under MSVC
71+
if(NOT MSVC)
72+
foreach(TARGET IN ITEMS ze_loader ze_validation_layer ze_tracing_layer ze_null)
73+
if (TARGET TARGET)
74+
add_ur_target_compile_options(${TARGET})
75+
add_ur_target_link_options(${TARGET})
76+
target_compile_options(${TARGET} PRIVATE
77+
$<$<CXX_COMPILER_ID:GNU,Clang,Intel,IntelLLVM>:-Wno-error -Wno-unused-parameter>
78+
$<$<CXX_COMPILER_ID:MSVC>:/WX- /UUNICODE>
79+
)
80+
endif()
81+
endforeach()
82+
endif()
83+
6984
if(NOT WIN32)
7085
target_sources(ur_adapter_level_zero
7186
PRIVATE

source/loader/layers/tracing/ur_tracing_layer.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ struct XptiContextManager {
3737
static std::shared_ptr<XptiContextManager> xptiContextManagerGet() {
3838
static auto contextManager = std::make_shared<XptiContextManager>();
3939
return contextManager;
40-
};
40+
}
4141
static thread_local xpti_td *activeEvent;
4242

4343
///////////////////////////////////////////////////////////////////////////////

test/adapters/level_zero/zeCallMap.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,5 @@
99
// Map used by L0 adapter to count the number of calls to each L0 function
1010
// Lifetime is managed by the adapter, this variable is defined here
1111
// only so that we can read it from the tests.
12-
std::map<std::string, int> *ZeCallCount = nullptr;
12+
__attribute__((visibility("default"))) std::map<std::string, int> *ZeCallCount =
13+
nullptr;

test/conformance/exp_command_buffer/fixtures.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,8 @@ struct urUpdatableCommandBufferExpTest : uur::urQueueTest {
126126

127127
// Create a command-buffer with update enabled.
128128
ur_exp_command_buffer_desc_t desc{
129-
UR_STRUCTURE_TYPE_EXP_COMMAND_BUFFER_DESC, nullptr, true};
129+
UR_STRUCTURE_TYPE_EXP_COMMAND_BUFFER_DESC, nullptr, true, false,
130+
false};
130131

131132
ASSERT_SUCCESS(urCommandBufferCreateExp(context, device, &desc,
132133
&updatable_cmd_buf_handle));

0 commit comments

Comments
 (0)