diff --git a/libdevice/CMakeLists.txt b/libdevice/CMakeLists.txt index 564391547677f..a985c101a6f94 100644 --- a/libdevice/CMakeLists.txt +++ b/libdevice/CMakeLists.txt @@ -8,6 +8,17 @@ set(CMAKE_MODULE_PATH "${CMAKE_CURRENT_SOURCE_DIR}/cmake/modules" ) +if(EXISTS ${FETCHCONTENT_BASE_DIR}/unified-runtime-src) + set(UR_SANITIZER_INCLUDE_DIR + ${FETCHCONTENT_BASE_DIR}/unified-runtime-src/source/loader/layers/sanitizer) +elseif(EXISTS ${SYCL_UR_SOURCE_DIR}) + set(UR_SANITIZER_INCLUDE_DIR + ${SYCL_UR_SOURCE_DIR}/source/loader/layers/sanitizer) +else() + message(WARNING "Unified Runtime source directory is not provided, \ + will not build libdevice sanitizer") +endif() + # Build libdevice for SYCL. include(SYCLLibdevice) diff --git a/libdevice/cmake/modules/SYCLLibdevice.cmake b/libdevice/cmake/modules/SYCLLibdevice.cmake index d3bf7ac5c6b50..8cd5513933c0c 100644 --- a/libdevice/cmake/modules/SYCLLibdevice.cmake +++ b/libdevice/cmake/modules/SYCLLibdevice.cmake @@ -200,10 +200,10 @@ set(cmath_obj_deps device_math.h device.h sycl-compiler) set(imf_obj_deps device_imf.hpp imf_half.hpp imf_bf16.hpp imf_rounding_op.hpp imf_impl_utils.hpp device.h sycl-compiler) set(itt_obj_deps device_itt.h spirv_vars.h device.h sycl-compiler) set(bfloat16_obj_deps sycl-headers sycl-compiler) -if (NOT MSVC) +if (NOT MSVC AND UR_SANITIZER_INCLUDE_DIR) set(sanitizer_obj_deps device.h atomic.hpp spirv_vars.h - include/asan_libdevice.hpp + ${UR_SANITIZER_INCLUDE_DIR}/asan_libdevice.hpp include/sanitizer_utils.hpp include/spir_global_var.hpp sycl-compiler) @@ -268,10 +268,12 @@ if(MSVC) SRC msvc_math.cpp DEPENDENCIES ${cmath_obj_deps}) else() - add_devicelibs(libsycl-sanitizer - SRC sanitizer_utils.cpp - DEPENDENCIES ${sanitizer_obj_deps} - EXTRA_OPTS -fno-sycl-instrument-device-code) + if(UR_SANITIZER_INCLUDE_DIR) + add_devicelibs(libsycl-sanitizer + SRC sanitizer_utils.cpp + DEPENDENCIES ${sanitizer_obj_deps} + EXTRA_OPTS -fno-sycl-instrument-device-code -I${UR_SANITIZER_INCLUDE_DIR}) + endif() endif() add_devicelibs(libsycl-fallback-cassert diff --git a/libdevice/include/asan_libdevice.hpp b/libdevice/include/asan_libdevice.hpp deleted file mode 100644 index 091a0dae14b56..0000000000000 --- a/libdevice/include/asan_libdevice.hpp +++ /dev/null @@ -1,157 +0,0 @@ -//===---- asan_libdevice.hpp - Structure and declaration for sanitizer ----===// -// -// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. -// See https://llvm.org/LICENSE.txt for license information. -// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception -// -//===----------------------------------------------------------------------===// -#pragma once - -#include - -// NOTE This file should be sync with -// unified-runtime/source/loader/layers/sanitizer/device_sanitizer_report.hpp - -enum class DeviceType : uint32_t { UNKNOWN = 0, CPU, GPU_PVC, GPU_DG2 }; -enum class DeviceSanitizerErrorType : int32_t { - UNKNOWN, - OUT_OF_BOUNDS, - MISALIGNED, - USE_AFTER_FREE, - OUT_OF_SHADOW_BOUNDS, - UNKNOWN_DEVICE, - NULL_POINTER, -}; - -enum class DeviceSanitizerMemoryType : int32_t { - UNKNOWN, - USM_DEVICE, - USM_HOST, - USM_SHARED, - LOCAL, - PRIVATE, - MEM_BUFFER, - DEVICE_GLOBAL, -}; - -struct DeviceSanitizerReport { - int Flag = 0; - - char File[256 + 1] = {}; - char Func[256 + 1] = {}; - - int32_t Line = 0; - - uint64_t GID0 = 0; - uint64_t GID1 = 0; - uint64_t GID2 = 0; - - uint64_t LID0 = 0; - uint64_t LID1 = 0; - uint64_t LID2 = 0; - - uintptr_t Address = 0; - bool IsWrite = false; - uint32_t AccessSize = 0; - DeviceSanitizerMemoryType MemoryType = DeviceSanitizerMemoryType::UNKNOWN; - DeviceSanitizerErrorType ErrorType = DeviceSanitizerErrorType::UNKNOWN; - - bool IsRecover = false; -}; - -struct LocalArgsInfo { - uint64_t Size = 0; - uint64_t SizeWithRedZone = 0; -}; - -constexpr std::size_t ASAN_MAX_NUM_REPORTS = 10; - -struct LaunchInfo { - uintptr_t GlobalShadowOffset = 0; - uintptr_t GlobalShadowOffsetEnd = 0; - - uintptr_t PrivateShadowOffset = 0; - uintptr_t PrivateShadowOffsetEnd = 0; - - uintptr_t LocalShadowOffset = 0; - uintptr_t LocalShadowOffsetEnd = 0; - - LocalArgsInfo *LocalArgs = nullptr; // Ordered by ArgIndex - uint32_t NumLocalArgs = 0; - - DeviceType DeviceTy = DeviceType::UNKNOWN; - uint32_t Debug = 0; - - DeviceSanitizerReport SanitizerReport[ASAN_MAX_NUM_REPORTS]; -}; - -constexpr unsigned ASAN_SHADOW_SCALE = 4; -constexpr unsigned ASAN_SHADOW_GRANULARITY = 1ULL << ASAN_SHADOW_SCALE; - -// Based on the observation, only the last 24 bits of the address of the private -// variable have changed -constexpr std::size_t ASAN_PRIVATE_SIZE = 0xffffffULL + 1; - -// These magic values are written to shadow for better error -// reporting. -constexpr int kUsmDeviceRedzoneMagic = (char)0x81; -constexpr int kUsmHostRedzoneMagic = (char)0x82; -constexpr int kUsmSharedRedzoneMagic = (char)0x83; -constexpr int kMemBufferRedzoneMagic = (char)0x84; -constexpr int kDeviceGlobalRedzoneMagic = (char)0x85; -constexpr int kNullPointerRedzoneMagic = (char)0x86; - -constexpr int kUsmDeviceDeallocatedMagic = (char)0x91; -constexpr int kUsmHostDeallocatedMagic = (char)0x92; -constexpr int kUsmSharedDeallocatedMagic = (char)0x93; -constexpr int kMemBufferDeallocatedMagic = (char)0x93; - -constexpr int kSharedLocalRedzoneMagic = (char)0xa1; - -// Same with host ASan stack -const int kPrivateLeftRedzoneMagic = (char)0xf1; -const int kPrivateMidRedzoneMagic = (char)0xf2; -const int kPrivateRightRedzoneMagic = (char)0xf3; - -constexpr auto kSPIR_AsanDeviceGlobalCount = "__AsanDeviceGlobalCount"; -constexpr auto kSPIR_AsanDeviceGlobalMetadata = "__AsanDeviceGlobalMetadata"; - -inline const char *ToString(DeviceSanitizerMemoryType MemoryType) { - switch (MemoryType) { - case DeviceSanitizerMemoryType::USM_DEVICE: - return "Device USM"; - case DeviceSanitizerMemoryType::USM_HOST: - return "Host USM"; - case DeviceSanitizerMemoryType::USM_SHARED: - return "Shared USM"; - case DeviceSanitizerMemoryType::LOCAL: - return "Local Memory"; - case DeviceSanitizerMemoryType::PRIVATE: - return "Private Memory"; - case DeviceSanitizerMemoryType::MEM_BUFFER: - return "Memory Buffer"; - case DeviceSanitizerMemoryType::DEVICE_GLOBAL: - return "Device Global"; - default: - return "Unknown Memory"; - } -} - -inline const char *ToString(DeviceSanitizerErrorType ErrorType) { - switch (ErrorType) { - case DeviceSanitizerErrorType::OUT_OF_BOUNDS: - return "out-of-bounds-access"; - case DeviceSanitizerErrorType::MISALIGNED: - return "misaligned-access"; - case DeviceSanitizerErrorType::USE_AFTER_FREE: - return "use-after-free"; - case DeviceSanitizerErrorType::OUT_OF_SHADOW_BOUNDS: - return "out-of-shadow-bounds-access"; - case DeviceSanitizerErrorType::UNKNOWN_DEVICE: - return "unknown-device"; - case DeviceSanitizerErrorType::NULL_POINTER: - return "null-pointer-access"; - default: - return "unknown-error"; - } -} diff --git a/libdevice/sanitizer_utils.cpp b/libdevice/sanitizer_utils.cpp index bf8441d92c5ed..e5d5ac5afdc30 100644 --- a/libdevice/sanitizer_utils.cpp +++ b/libdevice/sanitizer_utils.cpp @@ -6,11 +6,11 @@ // //===----------------------------------------------------------------------===// +#include "asan_libdevice.hpp" #include "atomic.hpp" #include "device.h" #include "spirv_vars.h" -#include "include/asan_libdevice.hpp" #include "include/sanitizer_utils.hpp" using uptr = uintptr_t; @@ -381,8 +381,11 @@ void __asan_internal_report_save(DeviceSanitizerErrorType error_type) { auto &SanitizerReport = ((__SYCL_GLOBAL__ LaunchInfo *)__AsanLaunchInfo) ->SanitizerReport[WG_LID % ASAN_MAX_NUM_REPORTS]; - if (atomicCompareAndSet(&SanitizerReport.Flag, Desired, Expected) == - Expected) { + if (atomicCompareAndSet( + &(((__SYCL_GLOBAL__ LaunchInfo *)__AsanLaunchInfo)->ReportFlag), 1, + 0) == 0 && + atomicCompareAndSet(&SanitizerReport.Flag, Desired, Expected) == + Expected) { SanitizerReport.ErrorType = error_type; SanitizerReport.IsRecover = false; @@ -415,8 +418,12 @@ void __asan_internal_report_save( auto &SanitizerReport = ((__SYCL_GLOBAL__ LaunchInfo *)__AsanLaunchInfo) ->SanitizerReport[WG_LID % ASAN_MAX_NUM_REPORTS]; - if (atomicCompareAndSet(&SanitizerReport.Flag, Desired, Expected) == - Expected) { + if ((is_recover || + atomicCompareAndSet( + &(((__SYCL_GLOBAL__ LaunchInfo *)__AsanLaunchInfo)->ReportFlag), 1, + 0) == 0) && + atomicCompareAndSet(&SanitizerReport.Flag, Desired, Expected) == + Expected) { int FileLength = 0; int FuncLength = 0; diff --git a/sycl/cmake/modules/UnifiedRuntimeTag.cmake b/sycl/cmake/modules/UnifiedRuntimeTag.cmake index 9fc715ac24f7c..7a8a290d9fcca 100644 --- a/sycl/cmake/modules/UnifiedRuntimeTag.cmake +++ b/sycl/cmake/modules/UnifiedRuntimeTag.cmake @@ -1,7 +1,5 @@ -# commit d3a518a2a621da6d78f8d25e8460fd3300fff705 -# Merge: ab0a706b 77975b87 -# Author: aarongreig -# Date: Fri Oct 25 16:45:53 2024 +0100 -# Merge pull request #2219 from kbenzie/benie/cl-adapter-opt-core-func -# [CL] Support using old ICD loaders -set(UNIFIED_RUNTIME_TAG d3a518a2a621da6d78f8d25e8460fd3300fff705) +# commit 66ba7970a6badf781226b75a98c9585ef30ea93a +# Author: Maosu Zhao +# Date: Mon Oct 28 18:39:57 2024 +0800 +# [DeviceSanitizer] Add a report flag to LaunchInfo (#2069) +set(UNIFIED_RUNTIME_TAG 66ba7970a6badf781226b75a98c9585ef30ea93a)