Skip to content

Commit

Permalink
Enable -flto and -fsanitize=cfi in clang
Browse files Browse the repository at this point in the history
  • Loading branch information
RossBrunton committed Nov 11, 2024
1 parent 1ba7f39 commit a210b80
Show file tree
Hide file tree
Showing 6 changed files with 43 additions and 7 deletions.
3 changes: 3 additions & 0 deletions .github/workflows/build-hw-reusable.yml
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,8 @@ jobs:
tar -xvf ${{github.workspace}}/dpcpp_compiler.tar.gz -C dpcpp_compiler
- name: Configure CMake
# CFI sanitization seems to fail on our CUDA nodes
# https://github.com/oneapi-src/unified-runtime/issues/2309
run: >
cmake
-B${{github.workspace}}/build
Expand All @@ -94,6 +96,7 @@ jobs:
-DUR_BUILD_ADAPTER_${{matrix.adapter.name}}=ON
-DUR_CONFORMANCE_TEST_LOADER=${{ matrix.adapter.other_name != '' && 'ON' || 'OFF' }}
${{ matrix.adapter.other_name != '' && format('-DUR_BUILD_ADAPTER_{0}=ON', matrix.adapter.other_name) || '' }}
${{ matrix.adapter.name == 'CUDA' && '-DCXX_HAS_CFI_SANITIZE=OFF' || '' }}
-DUR_STATIC_LOADER=${{matrix.adapter.static_Loader}}
-DUR_STATIC_ADAPTER_${{matrix.adapter.name}}=${{matrix.adapter.static_adapter}}
-DUR_DPCXX=${{github.workspace}}/dpcpp_compiler/bin/clang++
Expand Down
19 changes: 13 additions & 6 deletions cmake/helpers.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,11 @@ if(CMAKE_SYSTEM_NAME STREQUAL Linux)
check_cxx_compiler_flag("-fstack-clash-protection" CXX_HAS_FSTACK_CLASH_PROTECTION)
endif()

set(SAVED_CMAKE_REQUIRED_FLAGS ${CMAKE_REQUIRED_FLAGS})
set(CMAKE_REQUIRED_FLAGS "-flto -fvisibility=hidden")
check_cxx_compiler_flag("-fsanitize=cfi" CXX_HAS_CFI_SANITIZE)
set(CMAKE_REQUIRED_FLAGS ${SAVED_CMAKE_REQUIRED_FLAGS})

function(add_ur_target_compile_options name)
if(NOT MSVC)
target_compile_definitions(${name} PRIVATE -D_FORTIFY_SOURCE=2)
Expand All @@ -78,11 +83,10 @@ function(add_ur_target_compile_options name)
# Hardening options
-fPIC
-fstack-protector-strong
-fvisibility=hidden # Required for -fsanitize=cfi
# -fsanitize=cfi requires -flto, which breaks a lot of things
# See: https://github.com/oneapi-src/unified-runtime/issues/2120
# -flto
# $<$<CXX_COMPILER_ID:Clang,AppleClang>:-fsanitize=cfi>
-fvisibility=hidden
# cfi-icall requires called functions in shared libraries to also be built with cfi-icall, which we can't
# guarantee. -fsanitize=cfi depends on -flto
$<$<BOOL:${CXX_HAS_CFI_SANITIZE}>:-flto -fsanitize=cfi -fno-sanitize=cfi-icall>
$<$<BOOL:${CXX_HAS_FCF_PROTECTION_FULL}>:-fcf-protection=full>
$<$<BOOL:${CXX_HAS_FSTACK_CLASH_PROTECTION}>:-fstack-clash-protection>

Expand Down Expand Up @@ -119,7 +123,10 @@ endfunction()
function(add_ur_target_link_options name)
if(NOT MSVC)
if (NOT APPLE)
target_link_options(${name} PRIVATE "LINKER:-z,relro,-z,now,-z,noexecstack")
target_link_options(${name} PRIVATE
$<$<BOOL:${CXX_HAS_CFI_SANITIZE}>:-flto -fsanitize=cfi -fno-sanitize=cfi-icall>
"LINKER:-z,relro,-z,now,-z,noexecstack"
)
if (UR_DEVELOPER_MODE)
target_link_options(${name} PRIVATE -Werror -Wextra)
endif()
Expand Down
3 changes: 3 additions & 0 deletions test/conformance/enqueue/enqueue_adapter_opencl.match
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# Note: This file is only for use with cts_exe.py
# Fails when -fsanitize=cfi
{{OPT}}urEnqueueEventsWaitMultiDeviceMTTest.EnqueueWaitOnAllQueues/MultiThread
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# Note: This file is only for use with cts_exe.py
# These cause SIGILL when built with -fsanitize=cfi on Nvidia
{{OPT}}urCommandBufferKernelHandleUpdateTest.Success/*
{{OPT}}urCommandBufferKernelHandleUpdateTest.UpdateAgain/*
{{OPT}}urCommandBufferKernelHandleUpdateTest.RestoreOriginalKernel/*
{{OPT}}urCommandBufferKernelHandleUpdateTest.KernelAlternativeNotRegistered/*
{{OPT}}urCommandBufferKernelHandleUpdateTest.RegisterInvalidKernelAlternative/*
{{OPT}}urCommandBufferValidUpdateParametersTest.UpdateDimensionsWithoutUpdatingKernel/*
{{OPT}}urCommandBufferValidUpdateParametersTest.UpdateOnlyLocalWorkSize/*
{{OPT}}urCommandBufferValidUpdateParametersTest.SuccessNullptrHandle/*
{{OPT}}KernelCommandEventSyncUpdateTest.TwoWaitEvents/*
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# Note: This file is only for use with cts_exe.py
# These cause SIGILL when built with -fsanitize=cfi on AMD
{{OPT}}urCommandBufferKernelHandleUpdateTest.Success/*
{{OPT}}urCommandBufferKernelHandleUpdateTest.UpdateAgain/*
{{OPT}}urCommandBufferKernelHandleUpdateTest.RestoreOriginalKernel/*
{{OPT}}urCommandBufferKernelHandleUpdateTest.KernelAlternativeNotRegistered/*
{{OPT}}urCommandBufferKernelHandleUpdateTest.RegisterInvalidKernelAlternative/*
{{OPT}}urCommandBufferValidUpdateParametersTest.UpdateDimensionsWithoutUpdatingKernel/*
{{OPT}}urCommandBufferValidUpdateParametersTest.UpdateOnlyLocalWorkSize/*
{{OPT}}urCommandBufferValidUpdateParametersTest.SuccessNullptrHandle/*
4 changes: 3 additions & 1 deletion test/fuzz/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,9 @@ target_link_libraries(fuzztest-base
${PROJECT_NAME}::headers
${PROJECT_NAME}::common
-fsanitize=fuzzer)
target_compile_options(fuzztest-base PRIVATE -g -fsanitize=fuzzer)
# When built with -g and -flto (which is required by some hardening flags), this causes a segfault in (upstream)
# LLVM 14-15 while linking when CMAKE_BUILD_TYPE is Release
target_compile_options(fuzztest-base PRIVATE -fsanitize=fuzzer)
target_compile_definitions(fuzztest-base PRIVATE -DKERNEL_IL_PATH="${UR_CONFORMANCE_DEVICE_BINARIES_DIR}/fill/spir64.bin.0")
target_include_directories(fuzztest-base PRIVATE ${UR_CONFORMANCE_DEVICE_BINARIES_DIR})
add_dependencies(fuzztest-base generate_device_binaries)
Expand Down

0 comments on commit a210b80

Please sign in to comment.