diff --git a/source/loader/CMakeLists.txt b/source/loader/CMakeLists.txt index b740411c61..1837108645 100644 --- a/source/loader/CMakeLists.txt +++ b/source/loader/CMakeLists.txt @@ -131,45 +131,43 @@ if(UR_ENABLE_SANITIZER) target_sources(ur_loader PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/../ur/ur.cpp - ${CMAKE_CURRENT_SOURCE_DIR}/layers/sanitizer/asan_allocator.cpp - ${CMAKE_CURRENT_SOURCE_DIR}/layers/sanitizer/asan_allocator.hpp - ${CMAKE_CURRENT_SOURCE_DIR}/layers/sanitizer/asan_buffer.cpp - ${CMAKE_CURRENT_SOURCE_DIR}/layers/sanitizer/asan_buffer.hpp - ${CMAKE_CURRENT_SOURCE_DIR}/layers/sanitizer/asan_interceptor.cpp - ${CMAKE_CURRENT_SOURCE_DIR}/layers/sanitizer/asan_interceptor.hpp - ${CMAKE_CURRENT_SOURCE_DIR}/layers/sanitizer/asan_libdevice.hpp - ${CMAKE_CURRENT_SOURCE_DIR}/layers/sanitizer/asan_options.cpp - ${CMAKE_CURRENT_SOURCE_DIR}/layers/sanitizer/asan_options.hpp - ${CMAKE_CURRENT_SOURCE_DIR}/layers/sanitizer/asan_quarantine.cpp - ${CMAKE_CURRENT_SOURCE_DIR}/layers/sanitizer/asan_quarantine.hpp - ${CMAKE_CURRENT_SOURCE_DIR}/layers/sanitizer/asan_report.cpp - ${CMAKE_CURRENT_SOURCE_DIR}/layers/sanitizer/asan_report.hpp - ${CMAKE_CURRENT_SOURCE_DIR}/layers/sanitizer/asan_shadow.cpp - ${CMAKE_CURRENT_SOURCE_DIR}/layers/sanitizer/asan_shadow.hpp - ${CMAKE_CURRENT_SOURCE_DIR}/layers/sanitizer/asan_statistics.cpp - ${CMAKE_CURRENT_SOURCE_DIR}/layers/sanitizer/asan_statistics.hpp - ${CMAKE_CURRENT_SOURCE_DIR}/layers/sanitizer/asan_validator.cpp - ${CMAKE_CURRENT_SOURCE_DIR}/layers/sanitizer/asan_validator.hpp - ${CMAKE_CURRENT_SOURCE_DIR}/layers/sanitizer/common.hpp - ${CMAKE_CURRENT_SOURCE_DIR}/layers/sanitizer/stacktrace.cpp - ${CMAKE_CURRENT_SOURCE_DIR}/layers/sanitizer/stacktrace.hpp - ${CMAKE_CURRENT_SOURCE_DIR}/layers/sanitizer/ur_sanddi.cpp + ${CMAKE_CURRENT_SOURCE_DIR}/layers/sanitizer/asan/asan_allocator.cpp + ${CMAKE_CURRENT_SOURCE_DIR}/layers/sanitizer/asan/asan_allocator.hpp + ${CMAKE_CURRENT_SOURCE_DIR}/layers/sanitizer/asan/asan_buffer.cpp + ${CMAKE_CURRENT_SOURCE_DIR}/layers/sanitizer/asan/asan_buffer.hpp + ${CMAKE_CURRENT_SOURCE_DIR}/layers/sanitizer/asan/asan_ddi.cpp + ${CMAKE_CURRENT_SOURCE_DIR}/layers/sanitizer/asan/asan_interceptor.cpp + ${CMAKE_CURRENT_SOURCE_DIR}/layers/sanitizer/asan/asan_interceptor.hpp + ${CMAKE_CURRENT_SOURCE_DIR}/layers/sanitizer/asan/asan_libdevice.hpp + ${CMAKE_CURRENT_SOURCE_DIR}/layers/sanitizer/asan/asan_options.cpp + ${CMAKE_CURRENT_SOURCE_DIR}/layers/sanitizer/asan/asan_options.hpp + ${CMAKE_CURRENT_SOURCE_DIR}/layers/sanitizer/asan/asan_quarantine.cpp + ${CMAKE_CURRENT_SOURCE_DIR}/layers/sanitizer/asan/asan_quarantine.hpp + ${CMAKE_CURRENT_SOURCE_DIR}/layers/sanitizer/asan/asan_report.cpp + ${CMAKE_CURRENT_SOURCE_DIR}/layers/sanitizer/asan/asan_report.hpp + ${CMAKE_CURRENT_SOURCE_DIR}/layers/sanitizer/asan/asan_shadow.cpp + ${CMAKE_CURRENT_SOURCE_DIR}/layers/sanitizer/asan/asan_shadow.hpp + ${CMAKE_CURRENT_SOURCE_DIR}/layers/sanitizer/asan/asan_statistics.cpp + ${CMAKE_CURRENT_SOURCE_DIR}/layers/sanitizer/asan/asan_statistics.hpp + ${CMAKE_CURRENT_SOURCE_DIR}/layers/sanitizer/asan/asan_validator.cpp + ${CMAKE_CURRENT_SOURCE_DIR}/layers/sanitizer/asan/asan_validator.hpp + ${CMAKE_CURRENT_SOURCE_DIR}/layers/sanitizer/sanitizer_common/linux/backtrace.cpp + ${CMAKE_CURRENT_SOURCE_DIR}/layers/sanitizer/sanitizer_common/linux/sanitizer_utils.cpp + ${CMAKE_CURRENT_SOURCE_DIR}/layers/sanitizer/sanitizer_common/sanitizer_allocator.hpp + ${CMAKE_CURRENT_SOURCE_DIR}/layers/sanitizer/sanitizer_common/sanitizer_common.hpp + ${CMAKE_CURRENT_SOURCE_DIR}/layers/sanitizer/sanitizer_common/sanitizer_libdevice.hpp + ${CMAKE_CURRENT_SOURCE_DIR}/layers/sanitizer/sanitizer_common/sanitizer_stacktrace.cpp + ${CMAKE_CURRENT_SOURCE_DIR}/layers/sanitizer/sanitizer_common/sanitizer_stacktrace.hpp + ${CMAKE_CURRENT_SOURCE_DIR}/layers/sanitizer/sanitizer_common/sanitizer_utils.cpp + ${CMAKE_CURRENT_SOURCE_DIR}/layers/sanitizer/sanitizer_common/sanitizer_utils.hpp ${CMAKE_CURRENT_SOURCE_DIR}/layers/sanitizer/ur_sanitizer_layer.cpp ${CMAKE_CURRENT_SOURCE_DIR}/layers/sanitizer/ur_sanitizer_layer.hpp - ${CMAKE_CURRENT_SOURCE_DIR}/layers/sanitizer/ur_sanitizer_utils.cpp - ${CMAKE_CURRENT_SOURCE_DIR}/layers/sanitizer/ur_sanitizer_utils.hpp - ) - - target_sources(ur_loader - PRIVATE - ${CMAKE_CURRENT_SOURCE_DIR}/layers/sanitizer/linux/backtrace.cpp - ${CMAKE_CURRENT_SOURCE_DIR}/layers/sanitizer/linux/sanitizer_utils.cpp ) if(UR_ENABLE_SYMBOLIZER) target_sources(ur_loader PRIVATE - ${CMAKE_CURRENT_SOURCE_DIR}/layers/sanitizer/linux/symbolizer.cpp + ${CMAKE_CURRENT_SOURCE_DIR}/layers/sanitizer/sanitizer_common/linux/symbolizer.cpp ) target_include_directories(ur_loader PRIVATE ${LLVM_INCLUDE_DIRS}) target_link_libraries(ur_loader PRIVATE LLVMSupport LLVMSymbolize) diff --git a/source/loader/layers/sanitizer/asan_allocator.cpp b/source/loader/layers/sanitizer/asan/asan_allocator.cpp similarity index 94% rename from source/loader/layers/sanitizer/asan_allocator.cpp rename to source/loader/layers/sanitizer/asan/asan_allocator.cpp index c95ed728b6..d51cb4438c 100644 --- a/source/loader/layers/sanitizer/asan_allocator.cpp +++ b/source/loader/layers/sanitizer/asan/asan_allocator.cpp @@ -14,6 +14,7 @@ #include "ur_sanitizer_layer.hpp" namespace ur_sanitizer_layer { +namespace asan { void AllocInfo::print() { getContext()->logger.info( @@ -22,4 +23,5 @@ void AllocInfo::print() { (void *)(UserEnd), AllocSize, ToString(Type)); } +} // namespace asan } // namespace ur_sanitizer_layer diff --git a/source/loader/layers/sanitizer/asan_allocator.hpp b/source/loader/layers/sanitizer/asan/asan_allocator.hpp similarity index 57% rename from source/loader/layers/sanitizer/asan_allocator.hpp rename to source/loader/layers/sanitizer/asan/asan_allocator.hpp index 249ef896d0..1eca00171b 100644 --- a/source/loader/layers/sanitizer/asan_allocator.hpp +++ b/source/loader/layers/sanitizer/asan/asan_allocator.hpp @@ -12,22 +12,12 @@ #pragma once -#include "common.hpp" -#include "stacktrace.hpp" - -#include -#include +#include "sanitizer_common/sanitizer_allocator.hpp" +#include "sanitizer_common/sanitizer_common.hpp" +#include "sanitizer_common/sanitizer_stacktrace.hpp" namespace ur_sanitizer_layer { - -enum class AllocType : uint32_t { - UNKNOWN, - DEVICE_USM, - SHARED_USM, - HOST_USM, - MEM_BUFFER, - DEVICE_GLOBAL -}; +namespace asan { struct AllocInfo { uptr AllocBegin = 0; @@ -51,21 +41,5 @@ struct AllocInfo { using AllocationMap = std::map>; using AllocationIterator = AllocationMap::iterator; -inline const char *ToString(AllocType Type) { - switch (Type) { - case AllocType::DEVICE_USM: - return "Device USM"; - case AllocType::HOST_USM: - return "Host USM"; - case AllocType::SHARED_USM: - return "Shared USM"; - case AllocType::MEM_BUFFER: - return "Memory Buffer"; - case AllocType::DEVICE_GLOBAL: - return "Device Global"; - default: - return "Unknown Type"; - } -} - +} // namespace asan } // namespace ur_sanitizer_layer diff --git a/source/loader/layers/sanitizer/asan_buffer.cpp b/source/loader/layers/sanitizer/asan/asan_buffer.cpp similarity index 96% rename from source/loader/layers/sanitizer/asan_buffer.cpp rename to source/loader/layers/sanitizer/asan/asan_buffer.cpp index 9316d68bf4..8dd93a4679 100644 --- a/source/loader/layers/sanitizer/asan_buffer.cpp +++ b/source/loader/layers/sanitizer/asan/asan_buffer.cpp @@ -12,10 +12,11 @@ #include "asan_buffer.hpp" #include "asan_interceptor.hpp" +#include "sanitizer_common/sanitizer_utils.hpp" #include "ur_sanitizer_layer.hpp" -#include "ur_sanitizer_utils.hpp" namespace ur_sanitizer_layer { +namespace asan { ur_result_t EnqueueMemCopyRectHelper( ur_queue_handle_t Queue, char *pSrc, char *pDst, ur_rect_offset_t SrcOffset, @@ -91,7 +92,7 @@ ur_result_t MemBuffer::getHandle(ur_device_handle_t Device, char *&Handle) { ur_usm_desc_t USMDesc{}; USMDesc.align = getAlignment(); ur_usm_pool_handle_t Pool{}; - URes = getContext()->interceptor->allocateMemory( + URes = getAsanInterceptor()->allocateMemory( Context, Device, &USMDesc, Pool, Size, AllocType::MEM_BUFFER, ur_cast(&Allocation)); if (URes != UR_RESULT_SUCCESS) { @@ -129,7 +130,7 @@ ur_result_t MemBuffer::getHandle(ur_device_handle_t Device, char *&Handle) { ur_usm_desc_t USMDesc{}; USMDesc.align = getAlignment(); ur_usm_pool_handle_t Pool{}; - URes = getContext()->interceptor->allocateMemory( + URes = getAsanInterceptor()->allocateMemory( Context, nullptr, &USMDesc, Pool, Size, AllocType::HOST_USM, ur_cast(&HostAllocation)); if (URes != UR_RESULT_SUCCESS) { @@ -174,8 +175,7 @@ ur_result_t MemBuffer::getHandle(ur_device_handle_t Device, char *&Handle) { ur_result_t MemBuffer::free() { for (const auto &[_, Ptr] : Allocations) { - ur_result_t URes = - getContext()->interceptor->releaseMemory(Context, Ptr); + ur_result_t URes = getAsanInterceptor()->releaseMemory(Context, Ptr); if (URes != UR_RESULT_SUCCESS) { getContext()->logger.error("Failed to free buffer handle {}", Ptr); return URes; @@ -200,4 +200,5 @@ size_t MemBuffer::getAlignment() { return Alignment; } +} // namespace asan } // namespace ur_sanitizer_layer diff --git a/source/loader/layers/sanitizer/asan_buffer.hpp b/source/loader/layers/sanitizer/asan/asan_buffer.hpp similarity index 97% rename from source/loader/layers/sanitizer/asan_buffer.hpp rename to source/loader/layers/sanitizer/asan/asan_buffer.hpp index 989ef4249f..46201d9f8d 100644 --- a/source/loader/layers/sanitizer/asan_buffer.hpp +++ b/source/loader/layers/sanitizer/asan/asan_buffer.hpp @@ -16,9 +16,10 @@ #include #include -#include "common.hpp" +#include "ur/ur.hpp" namespace ur_sanitizer_layer { +namespace asan { struct MemBuffer { // Buffer constructor @@ -77,4 +78,5 @@ ur_result_t EnqueueMemCopyRectHelper( bool Blocking, uint32_t NumEventsInWaitList, const ur_event_handle_t *EventWaitList, ur_event_handle_t *Event); +} // namespace asan } // namespace ur_sanitizer_layer diff --git a/source/loader/layers/sanitizer/ur_sanddi.cpp b/source/loader/layers/sanitizer/asan/asan_ddi.cpp similarity index 89% rename from source/loader/layers/sanitizer/ur_sanddi.cpp rename to source/loader/layers/sanitizer/asan/asan_ddi.cpp index 95b1649691..53e86ef49f 100644 --- a/source/loader/layers/sanitizer/ur_sanddi.cpp +++ b/source/loader/layers/sanitizer/asan/asan_ddi.cpp @@ -1,35 +1,37 @@ /* * - * Copyright (C) 2023 Intel Corporation + * Copyright (C) 2024 Intel Corporation * * Part of the Unified-Runtime Project, under the Apache License v2.0 with LLVM Exceptions. * See LICENSE.TXT * SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception * - * @file ur_sanddi.cpp + * @file asan_ddi.cpp * */ +#include "asan_ddi.hpp" #include "asan_interceptor.hpp" #include "asan_options.hpp" -#include "stacktrace.hpp" +#include "sanitizer_common/sanitizer_stacktrace.hpp" +#include "sanitizer_common/sanitizer_utils.hpp" #include "ur_sanitizer_layer.hpp" -#include "ur_sanitizer_utils.hpp" #include namespace ur_sanitizer_layer { +namespace asan { namespace { ur_result_t setupContext(ur_context_handle_t Context, uint32_t numDevices, const ur_device_handle_t *phDevices) { std::shared_ptr CI; - UR_CALL(getContext()->interceptor->insertContext(Context, CI)); + UR_CALL(getAsanInterceptor()->insertContext(Context, CI)); for (uint32_t i = 0; i < numDevices; ++i) { auto hDevice = phDevices[i]; std::shared_ptr DI; - UR_CALL(getContext()->interceptor->insertDevice(hDevice, DI)); + UR_CALL(getAsanInterceptor()->insertDevice(hDevice, DI)); DI->Type = GetDeviceType(Context, hDevice); if (DI->Type == DeviceType::UNKNOWN) { getContext()->logger.error("Unsupport device"); @@ -77,7 +79,7 @@ __urdlllocal ur_result_t UR_APICALL urAdapterGet( if (result == UR_RESULT_SUCCESS && phAdapters) { const uint32_t NumAdapters = pNumAdapters ? *pNumAdapters : NumEntries; for (uint32_t i = 0; i < NumAdapters; ++i) { - UR_CALL(getContext()->interceptor->holdAdapter(phAdapters[i])); + UR_CALL(getAsanInterceptor()->holdAdapter(phAdapters[i])); } } @@ -104,7 +106,7 @@ __urdlllocal ur_result_t UR_APICALL urUSMHostAlloc( getContext()->logger.debug("==== urUSMHostAlloc"); - return getContext()->interceptor->allocateMemory( + return getAsanInterceptor()->allocateMemory( hContext, nullptr, pUSMDesc, pool, size, AllocType::HOST_USM, ppMem); } @@ -129,7 +131,7 @@ __urdlllocal ur_result_t UR_APICALL urUSMDeviceAlloc( getContext()->logger.debug("==== urUSMDeviceAlloc"); - return getContext()->interceptor->allocateMemory( + return getAsanInterceptor()->allocateMemory( hContext, hDevice, pUSMDesc, pool, size, AllocType::DEVICE_USM, ppMem); } @@ -154,7 +156,7 @@ __urdlllocal ur_result_t UR_APICALL urUSMSharedAlloc( getContext()->logger.debug("==== urUSMSharedAlloc"); - return getContext()->interceptor->allocateMemory( + return getAsanInterceptor()->allocateMemory( hContext, hDevice, pUSMDesc, pool, size, AllocType::SHARED_USM, ppMem); } @@ -172,7 +174,7 @@ __urdlllocal ur_result_t UR_APICALL urUSMFree( getContext()->logger.debug("==== urUSMFree"); - return getContext()->interceptor->releaseMemory(hContext, pMem); + return getAsanInterceptor()->releaseMemory(hContext, pMem); } /////////////////////////////////////////////////////////////////////////////// @@ -197,7 +199,7 @@ __urdlllocal ur_result_t UR_APICALL urProgramCreateWithIL( UR_CALL( pfnProgramCreateWithIL(hContext, pIL, length, pProperties, phProgram)); - UR_CALL(getContext()->interceptor->insertProgram(*phProgram)); + UR_CALL(getAsanInterceptor()->insertProgram(*phProgram)); return UR_RESULT_SUCCESS; } @@ -233,7 +235,7 @@ __urdlllocal ur_result_t UR_APICALL urProgramCreateWithBinary( UR_CALL(pfnProgramCreateWithBinary(hContext, numDevices, phDevices, pLengths, ppBinaries, pProperties, phProgram)); - UR_CALL(getContext()->interceptor->insertProgram(*phProgram)); + UR_CALL(getAsanInterceptor()->insertProgram(*phProgram)); return UR_RESULT_SUCCESS; } @@ -260,7 +262,7 @@ __urdlllocal ur_result_t UR_APICALL urProgramCreateWithNativeHandle( UR_CALL(pfnProgramCreateWithNativeHandle(hNativeProgram, hContext, pProperties, phProgram)); - UR_CALL(getContext()->interceptor->insertProgram(*phProgram)); + UR_CALL(getAsanInterceptor()->insertProgram(*phProgram)); return UR_RESULT_SUCCESS; } @@ -281,7 +283,7 @@ __urdlllocal ur_result_t UR_APICALL urProgramRetain( UR_CALL(pfnRetain(hProgram)); - auto ProgramInfo = getContext()->interceptor->getProgramInfo(hProgram); + auto ProgramInfo = getAsanInterceptor()->getProgramInfo(hProgram); UR_ASSERT(ProgramInfo != nullptr, UR_RESULT_ERROR_INVALID_VALUE); ProgramInfo->RefCount++; @@ -305,7 +307,7 @@ __urdlllocal ur_result_t UR_APICALL urProgramBuild( UR_CALL(pfnProgramBuild(hContext, hProgram, pOptions)); - UR_CALL(getContext()->interceptor->registerProgram(hContext, hProgram)); + UR_CALL(getAsanInterceptor()->registerProgram(hContext, hProgram)); return UR_RESULT_SUCCESS; } @@ -329,8 +331,8 @@ __urdlllocal ur_result_t UR_APICALL urProgramBuildExp( getContext()->logger.debug("==== urProgramBuildExp"); UR_CALL(pfnBuildExp(hProgram, numDevices, phDevices, pOptions)); - UR_CALL(getContext()->interceptor->registerProgram(GetContext(hProgram), - hProgram)); + UR_CALL( + getAsanInterceptor()->registerProgram(GetContext(hProgram), hProgram)); return UR_RESULT_SUCCESS; } @@ -357,7 +359,7 @@ __urdlllocal ur_result_t UR_APICALL urProgramLink( UR_CALL(pfnProgramLink(hContext, count, phPrograms, pOptions, phProgram)); - UR_CALL(getContext()->interceptor->registerProgram(hContext, *phProgram)); + UR_CALL(getAsanInterceptor()->registerProgram(hContext, *phProgram)); return UR_RESULT_SUCCESS; } @@ -388,7 +390,7 @@ ur_result_t UR_APICALL urProgramLinkExp( UR_CALL(pfnProgramLinkExp(hContext, numDevices, phDevices, count, phPrograms, pOptions, phProgram)); - UR_CALL(getContext()->interceptor->registerProgram(hContext, *phProgram)); + UR_CALL(getAsanInterceptor()->registerProgram(hContext, *phProgram)); return UR_RESULT_SUCCESS; } @@ -409,11 +411,11 @@ ur_result_t UR_APICALL urProgramRelease( UR_CALL(pfnProgramRelease(hProgram)); - auto ProgramInfo = getContext()->interceptor->getProgramInfo(hProgram); + auto ProgramInfo = getAsanInterceptor()->getProgramInfo(hProgram); UR_ASSERT(ProgramInfo != nullptr, UR_RESULT_ERROR_INVALID_VALUE); if (--ProgramInfo->RefCount == 0) { - UR_CALL(getContext()->interceptor->unregisterProgram(hProgram)); - UR_CALL(getContext()->interceptor->eraseProgram(hProgram)); + UR_CALL(getAsanInterceptor()->unregisterProgram(hProgram)); + UR_CALL(getAsanInterceptor()->eraseProgram(hProgram)); } return UR_RESULT_SUCCESS; @@ -463,8 +465,7 @@ __urdlllocal ur_result_t UR_APICALL urEnqueueKernelLaunch( workDim); UR_CALL(LaunchInfo.initialize()); - UR_CALL(getContext()->interceptor->preLaunchKernel(hKernel, hQueue, - LaunchInfo)); + UR_CALL(getAsanInterceptor()->preLaunchKernel(hKernel, hQueue, LaunchInfo)); ur_event_handle_t hEvent{}; ur_result_t result = @@ -473,8 +474,8 @@ __urdlllocal ur_result_t UR_APICALL urEnqueueKernelLaunch( numEventsInWaitList, phEventWaitList, &hEvent); if (result == UR_RESULT_SUCCESS) { - UR_CALL(getContext()->interceptor->postLaunchKernel(hKernel, hQueue, - LaunchInfo)); + UR_CALL(getAsanInterceptor()->postLaunchKernel(hKernel, hQueue, + LaunchInfo)); } if (phEvent) { @@ -563,7 +564,7 @@ __urdlllocal ur_result_t UR_APICALL urContextRetain( UR_CALL(pfnRetain(hContext)); - auto ContextInfo = getContext()->interceptor->getContextInfo(hContext); + auto ContextInfo = getAsanInterceptor()->getContextInfo(hContext); UR_ASSERT(ContextInfo != nullptr, UR_RESULT_ERROR_INVALID_VALUE); ContextInfo->RefCount++; @@ -585,10 +586,10 @@ __urdlllocal ur_result_t UR_APICALL urContextRelease( UR_CALL(pfnRelease(hContext)); - auto ContextInfo = getContext()->interceptor->getContextInfo(hContext); + auto ContextInfo = getAsanInterceptor()->getContextInfo(hContext); UR_ASSERT(ContextInfo != nullptr, UR_RESULT_ERROR_INVALID_VALUE); if (--ContextInfo->RefCount == 0) { - UR_CALL(getContext()->interceptor->eraseContext(hContext)); + UR_CALL(getAsanInterceptor()->eraseContext(hContext)); } return UR_RESULT_SUCCESS; @@ -631,7 +632,7 @@ __urdlllocal ur_result_t UR_APICALL urMemBufferCreate( if (Host && (flags & UR_MEM_FLAG_ALLOC_COPY_HOST_POINTER)) { std::shared_ptr CtxInfo = - getContext()->interceptor->getContextInfo(hContext); + getAsanInterceptor()->getContextInfo(hContext); for (const auto &hDevice : CtxInfo->DeviceList) { ManagedQueue InternalQueue(hContext, hDevice); char *Handle = nullptr; @@ -641,7 +642,7 @@ __urdlllocal ur_result_t UR_APICALL urMemBufferCreate( } } - ur_result_t result = getContext()->interceptor->insertMemBuffer(pMemBuffer); + ur_result_t result = getAsanInterceptor()->insertMemBuffer(pMemBuffer); *phBuffer = ur_cast(pMemBuffer.get()); return result; @@ -672,7 +673,7 @@ __urdlllocal ur_result_t UR_APICALL urMemGetInfo( getContext()->logger.debug("==== urMemGetInfo"); - if (auto MemBuffer = getContext()->interceptor->getMemBuffer(hMemory)) { + if (auto MemBuffer = getAsanInterceptor()->getMemBuffer(hMemory)) { UrReturnHelper ReturnValue(propSize, pPropValue, pPropSizeRet); switch (propName) { case UR_MEM_INFO_CONTEXT: { @@ -706,7 +707,7 @@ __urdlllocal ur_result_t UR_APICALL urMemRetain( getContext()->logger.debug("==== urMemRetain"); - if (auto MemBuffer = getContext()->interceptor->getMemBuffer(hMem)) { + if (auto MemBuffer = getAsanInterceptor()->getMemBuffer(hMem)) { MemBuffer->RefCount++; } else { UR_CALL(pfnRetain(hMem)); @@ -728,12 +729,12 @@ __urdlllocal ur_result_t UR_APICALL urMemRelease( getContext()->logger.debug("==== urMemRelease"); - if (auto MemBuffer = getContext()->interceptor->getMemBuffer(hMem)) { + if (auto MemBuffer = getAsanInterceptor()->getMemBuffer(hMem)) { if (--MemBuffer->RefCount != 0) { return UR_RESULT_SUCCESS; } UR_CALL(MemBuffer->free()); - UR_CALL(getContext()->interceptor->eraseMemBuffer(hMem)); + UR_CALL(getAsanInterceptor()->eraseMemBuffer(hMem)); } else { UR_CALL(pfnRelease(hMem)); } @@ -761,13 +762,13 @@ __urdlllocal ur_result_t UR_APICALL urMemBufferPartition( getContext()->logger.debug("==== urMemBufferPartition"); - if (auto ParentBuffer = getContext()->interceptor->getMemBuffer(hBuffer)) { + if (auto ParentBuffer = getAsanInterceptor()->getMemBuffer(hBuffer)) { if (ParentBuffer->Size < (pRegion->origin + pRegion->size)) { return UR_RESULT_ERROR_INVALID_BUFFER_SIZE; } std::shared_ptr SubBuffer = std::make_shared( ParentBuffer, pRegion->origin, pRegion->size); - UR_CALL(getContext()->interceptor->insertMemBuffer(SubBuffer)); + UR_CALL(getAsanInterceptor()->insertMemBuffer(SubBuffer)); *phMem = reinterpret_cast(SubBuffer.get()); } else { UR_CALL(pfnBufferPartition(hBuffer, flags, bufferCreateType, pRegion, @@ -793,7 +794,7 @@ __urdlllocal ur_result_t UR_APICALL urMemGetNativeHandle( getContext()->logger.debug("==== urMemGetNativeHandle"); - if (auto MemBuffer = getContext()->interceptor->getMemBuffer(hMem)) { + if (auto MemBuffer = getAsanInterceptor()->getMemBuffer(hMem)) { char *Handle = nullptr; UR_CALL(MemBuffer->getHandle(hDevice, Handle)); *phNativeMem = ur_cast(Handle); @@ -832,7 +833,7 @@ __urdlllocal ur_result_t UR_APICALL urEnqueueMemBufferRead( getContext()->logger.debug("==== urEnqueueMemBufferRead"); - if (auto MemBuffer = getContext()->interceptor->getMemBuffer(hBuffer)) { + if (auto MemBuffer = getAsanInterceptor()->getMemBuffer(hBuffer)) { ur_device_handle_t Device = GetDevice(hQueue); char *pSrc = nullptr; UR_CALL(MemBuffer->getHandle(Device, pSrc)); @@ -878,7 +879,7 @@ __urdlllocal ur_result_t UR_APICALL urEnqueueMemBufferWrite( getContext()->logger.debug("==== urEnqueueMemBufferWrite"); - if (auto MemBuffer = getContext()->interceptor->getMemBuffer(hBuffer)) { + if (auto MemBuffer = getAsanInterceptor()->getMemBuffer(hBuffer)) { ur_device_handle_t Device = GetDevice(hQueue); char *pDst = nullptr; UR_CALL(MemBuffer->getHandle(Device, pDst)); @@ -935,7 +936,7 @@ __urdlllocal ur_result_t UR_APICALL urEnqueueMemBufferReadRect( getContext()->logger.debug("==== urEnqueueMemBufferReadRect"); - if (auto MemBuffer = getContext()->interceptor->getMemBuffer(hBuffer)) { + if (auto MemBuffer = getAsanInterceptor()->getMemBuffer(hBuffer)) { char *SrcHandle = nullptr; ur_device_handle_t Device = GetDevice(hQueue); UR_CALL(MemBuffer->getHandle(Device, SrcHandle)); @@ -999,7 +1000,7 @@ __urdlllocal ur_result_t UR_APICALL urEnqueueMemBufferWriteRect( getContext()->logger.debug("==== urEnqueueMemBufferWriteRect"); - if (auto MemBuffer = getContext()->interceptor->getMemBuffer(hBuffer)) { + if (auto MemBuffer = getAsanInterceptor()->getMemBuffer(hBuffer)) { char *DstHandle = nullptr; ur_device_handle_t Device = GetDevice(hQueue); UR_CALL(MemBuffer->getHandle(Device, DstHandle)); @@ -1048,8 +1049,8 @@ __urdlllocal ur_result_t UR_APICALL urEnqueueMemBufferCopy( getContext()->logger.debug("==== urEnqueueMemBufferCopy"); - auto SrcBuffer = getContext()->interceptor->getMemBuffer(hBufferSrc); - auto DstBuffer = getContext()->interceptor->getMemBuffer(hBufferDst); + auto SrcBuffer = getAsanInterceptor()->getMemBuffer(hBufferSrc); + auto DstBuffer = getAsanInterceptor()->getMemBuffer(hBufferDst); UR_ASSERT((SrcBuffer && DstBuffer) || (!SrcBuffer && !DstBuffer), UR_RESULT_ERROR_INVALID_MEM_OBJECT); @@ -1113,8 +1114,8 @@ __urdlllocal ur_result_t UR_APICALL urEnqueueMemBufferCopyRect( getContext()->logger.debug("==== urEnqueueMemBufferCopyRect"); - auto SrcBuffer = getContext()->interceptor->getMemBuffer(hBufferSrc); - auto DstBuffer = getContext()->interceptor->getMemBuffer(hBufferDst); + auto SrcBuffer = getAsanInterceptor()->getMemBuffer(hBufferSrc); + auto DstBuffer = getAsanInterceptor()->getMemBuffer(hBufferDst); UR_ASSERT((SrcBuffer && DstBuffer) || (!SrcBuffer && !DstBuffer), UR_RESULT_ERROR_INVALID_MEM_OBJECT); @@ -1169,7 +1170,7 @@ __urdlllocal ur_result_t UR_APICALL urEnqueueMemBufferFill( getContext()->logger.debug("==== urEnqueueMemBufferFill"); - if (auto MemBuffer = getContext()->interceptor->getMemBuffer(hBuffer)) { + if (auto MemBuffer = getAsanInterceptor()->getMemBuffer(hBuffer)) { char *Handle = nullptr; ur_device_handle_t Device = GetDevice(hQueue); UR_CALL(MemBuffer->getHandle(Device, Handle)); @@ -1215,7 +1216,7 @@ __urdlllocal ur_result_t UR_APICALL urEnqueueMemBufferMap( getContext()->logger.debug("==== urEnqueueMemBufferMap"); - if (auto MemBuffer = getContext()->interceptor->getMemBuffer(hBuffer)) { + if (auto MemBuffer = getAsanInterceptor()->getMemBuffer(hBuffer)) { // Translate the host access mode info. MemBuffer::AccessMode AccessMode = MemBuffer::UNKNOWN; @@ -1245,7 +1246,7 @@ __urdlllocal ur_result_t UR_APICALL urEnqueueMemBufferMap( ur_usm_desc_t USMDesc{}; USMDesc.align = MemBuffer->getAlignment(); ur_usm_pool_handle_t Pool{}; - UR_CALL(getContext()->interceptor->allocateMemory( + UR_CALL(getAsanInterceptor()->allocateMemory( Context, nullptr, &USMDesc, Pool, size, AllocType::HOST_USM, ppRetMap)); } @@ -1300,7 +1301,7 @@ __urdlllocal ur_result_t UR_APICALL urEnqueueMemUnmap( getContext()->logger.debug("==== urEnqueueMemUnmap"); - if (auto MemBuffer = getContext()->interceptor->getMemBuffer(hMem)) { + if (auto MemBuffer = getAsanInterceptor()->getMemBuffer(hMem)) { MemBuffer::Mapping Mapping{}; { std::scoped_lock Guard(MemBuffer->Mutex); @@ -1323,8 +1324,7 @@ __urdlllocal ur_result_t UR_APICALL urEnqueueMemUnmap( numEventsInWaitList, phEventWaitList, phEvent)); if (!MemBuffer->HostPtr) { - UR_CALL( - getContext()->interceptor->releaseMemory(Context, pMappedPtr)); + UR_CALL(getAsanInterceptor()->releaseMemory(Context, pMappedPtr)); } } else { UR_CALL(pfnMemUnmap(hQueue, hMem, pMappedPtr, numEventsInWaitList, @@ -1351,7 +1351,7 @@ __urdlllocal ur_result_t UR_APICALL urKernelCreate( getContext()->logger.debug("==== urKernelCreate"); UR_CALL(pfnCreate(hProgram, pKernelName, phKernel)); - UR_CALL(getContext()->interceptor->insertKernel(*phKernel)); + UR_CALL(getAsanInterceptor()->insertKernel(*phKernel)); return UR_RESULT_SUCCESS; } @@ -1371,7 +1371,7 @@ __urdlllocal ur_result_t UR_APICALL urKernelRetain( UR_CALL(pfnRetain(hKernel)); - auto KernelInfo = getContext()->interceptor->getKernelInfo(hKernel); + auto KernelInfo = getAsanInterceptor()->getKernelInfo(hKernel); UR_ASSERT(KernelInfo != nullptr, UR_RESULT_ERROR_INVALID_VALUE); KernelInfo->RefCount++; @@ -1392,10 +1392,10 @@ __urdlllocal ur_result_t urKernelRelease( getContext()->logger.debug("==== urKernelRelease"); UR_CALL(pfnRelease(hKernel)); - auto KernelInfo = getContext()->interceptor->getKernelInfo(hKernel); + auto KernelInfo = getAsanInterceptor()->getKernelInfo(hKernel); UR_ASSERT(KernelInfo != nullptr, UR_RESULT_ERROR_INVALID_VALUE); if (--KernelInfo->RefCount == 0) { - UR_CALL(getContext()->interceptor->eraseKernel(hKernel)); + UR_CALL(getAsanInterceptor()->eraseKernel(hKernel)); } return UR_RESULT_SUCCESS; @@ -1422,9 +1422,9 @@ __urdlllocal ur_result_t UR_APICALL urKernelSetArgValue( std::shared_ptr MemBuffer; if (argSize == sizeof(ur_mem_handle_t) && - (MemBuffer = getContext()->interceptor->getMemBuffer( + (MemBuffer = getAsanInterceptor()->getMemBuffer( *ur_cast(pArgValue)))) { - auto KernelInfo = getContext()->interceptor->getKernelInfo(hKernel); + auto KernelInfo = getAsanInterceptor()->getKernelInfo(hKernel); std::scoped_lock Guard(KernelInfo->Mutex); KernelInfo->BufferArgs[argIndex] = std::move(MemBuffer); } else { @@ -1452,8 +1452,8 @@ __urdlllocal ur_result_t UR_APICALL urKernelSetArgMemObj( getContext()->logger.debug("==== urKernelSetArgMemObj"); - if (auto MemBuffer = getContext()->interceptor->getMemBuffer(hArgValue)) { - auto KernelInfo = getContext()->interceptor->getKernelInfo(hKernel); + if (auto MemBuffer = getAsanInterceptor()->getMemBuffer(hArgValue)) { + auto KernelInfo = getAsanInterceptor()->getKernelInfo(hKernel); std::scoped_lock Guard(KernelInfo->Mutex); KernelInfo->BufferArgs[argIndex] = std::move(MemBuffer); } else { @@ -1484,7 +1484,7 @@ __urdlllocal ur_result_t UR_APICALL urKernelSetArgLocal( argSize); { - auto KI = getContext()->interceptor->getKernelInfo(hKernel); + auto KI = getAsanInterceptor()->getKernelInfo(hKernel); std::scoped_lock Guard(KI->Mutex); // TODO: get local variable alignment auto argSizeWithRZ = GetSizeAndRedzoneSizeForLocal( @@ -1520,8 +1520,8 @@ __urdlllocal ur_result_t UR_APICALL urKernelSetArgPointer( "==== urKernelSetArgPointer (argIndex={}, pArgValue={})", argIndex, pArgValue); - if (getContext()->interceptor->getOptions().DetectKernelArguments) { - auto KI = getContext()->interceptor->getKernelInfo(hKernel); + if (getAsanInterceptor()->getOptions().DetectKernelArguments) { + auto KI = getAsanInterceptor()->getKernelInfo(hKernel); std::scoped_lock Guard(KI->Mutex); KI->PointerArgs[argIndex] = {pArgValue, GetCurrentBacktrace()}; } @@ -1558,7 +1558,7 @@ __urdlllocal ur_result_t UR_APICALL urGetGlobalProcAddrTable( ur_result_t result = UR_RESULT_SUCCESS; - pDdiTable->pfnAdapterGet = ur_sanitizer_layer::urAdapterGet; + pDdiTable->pfnAdapterGet = ur_sanitizer_layer::asan::urAdapterGet; return result; } @@ -1588,12 +1588,12 @@ __urdlllocal ur_result_t UR_APICALL urGetContextProcAddrTable( ur_result_t result = UR_RESULT_SUCCESS; - pDdiTable->pfnCreate = ur_sanitizer_layer::urContextCreate; - pDdiTable->pfnRetain = ur_sanitizer_layer::urContextRetain; - pDdiTable->pfnRelease = ur_sanitizer_layer::urContextRelease; + pDdiTable->pfnCreate = ur_sanitizer_layer::asan::urContextCreate; + pDdiTable->pfnRetain = ur_sanitizer_layer::asan::urContextRetain; + pDdiTable->pfnRelease = ur_sanitizer_layer::asan::urContextRelease; pDdiTable->pfnCreateWithNativeHandle = - ur_sanitizer_layer::urContextCreateWithNativeHandle; + ur_sanitizer_layer::asan::urContextCreateWithNativeHandle; return result; } @@ -1621,15 +1621,16 @@ __urdlllocal ur_result_t UR_APICALL urGetProgramProcAddrTable( return UR_RESULT_ERROR_UNSUPPORTED_VERSION; } - pDdiTable->pfnCreateWithIL = ur_sanitizer_layer::urProgramCreateWithIL; + pDdiTable->pfnCreateWithIL = + ur_sanitizer_layer::asan::urProgramCreateWithIL; pDdiTable->pfnCreateWithBinary = - ur_sanitizer_layer::urProgramCreateWithBinary; + ur_sanitizer_layer::asan::urProgramCreateWithBinary; pDdiTable->pfnCreateWithNativeHandle = - ur_sanitizer_layer::urProgramCreateWithNativeHandle; - pDdiTable->pfnBuild = ur_sanitizer_layer::urProgramBuild; - pDdiTable->pfnLink = ur_sanitizer_layer::urProgramLink; - pDdiTable->pfnRetain = ur_sanitizer_layer::urProgramRetain; - pDdiTable->pfnRelease = ur_sanitizer_layer::urProgramRelease; + ur_sanitizer_layer::asan::urProgramCreateWithNativeHandle; + pDdiTable->pfnBuild = ur_sanitizer_layer::asan::urProgramBuild; + pDdiTable->pfnLink = ur_sanitizer_layer::asan::urProgramLink; + pDdiTable->pfnRetain = ur_sanitizer_layer::asan::urProgramRetain; + pDdiTable->pfnRelease = ur_sanitizer_layer::asan::urProgramRelease; return UR_RESULT_SUCCESS; } @@ -1660,13 +1661,14 @@ __urdlllocal ur_result_t UR_APICALL urGetKernelProcAddrTable( ur_result_t result = UR_RESULT_SUCCESS; - pDdiTable->pfnCreate = ur_sanitizer_layer::urKernelCreate; - pDdiTable->pfnRetain = ur_sanitizer_layer::urKernelRetain; - pDdiTable->pfnRelease = ur_sanitizer_layer::urKernelRelease; - pDdiTable->pfnSetArgValue = ur_sanitizer_layer::urKernelSetArgValue; - pDdiTable->pfnSetArgMemObj = ur_sanitizer_layer::urKernelSetArgMemObj; - pDdiTable->pfnSetArgLocal = ur_sanitizer_layer::urKernelSetArgLocal; - pDdiTable->pfnSetArgPointer = ur_sanitizer_layer::urKernelSetArgPointer; + pDdiTable->pfnCreate = ur_sanitizer_layer::asan::urKernelCreate; + pDdiTable->pfnRetain = ur_sanitizer_layer::asan::urKernelRetain; + pDdiTable->pfnRelease = ur_sanitizer_layer::asan::urKernelRelease; + pDdiTable->pfnSetArgValue = ur_sanitizer_layer::asan::urKernelSetArgValue; + pDdiTable->pfnSetArgMemObj = ur_sanitizer_layer::asan::urKernelSetArgMemObj; + pDdiTable->pfnSetArgLocal = ur_sanitizer_layer::asan::urKernelSetArgLocal; + pDdiTable->pfnSetArgPointer = + ur_sanitizer_layer::asan::urKernelSetArgPointer; return result; } @@ -1696,12 +1698,14 @@ __urdlllocal ur_result_t UR_APICALL urGetMemProcAddrTable( ur_result_t result = UR_RESULT_SUCCESS; - pDdiTable->pfnBufferCreate = ur_sanitizer_layer::urMemBufferCreate; - pDdiTable->pfnRetain = ur_sanitizer_layer::urMemRetain; - pDdiTable->pfnRelease = ur_sanitizer_layer::urMemRelease; - pDdiTable->pfnBufferPartition = ur_sanitizer_layer::urMemBufferPartition; - pDdiTable->pfnGetNativeHandle = ur_sanitizer_layer::urMemGetNativeHandle; - pDdiTable->pfnGetInfo = ur_sanitizer_layer::urMemGetInfo; + pDdiTable->pfnBufferCreate = ur_sanitizer_layer::asan::urMemBufferCreate; + pDdiTable->pfnRetain = ur_sanitizer_layer::asan::urMemRetain; + pDdiTable->pfnRelease = ur_sanitizer_layer::asan::urMemRelease; + pDdiTable->pfnBufferPartition = + ur_sanitizer_layer::asan::urMemBufferPartition; + pDdiTable->pfnGetNativeHandle = + ur_sanitizer_layer::asan::urMemGetNativeHandle; + pDdiTable->pfnGetInfo = ur_sanitizer_layer::asan::urMemGetInfo; return result; } @@ -1730,8 +1734,8 @@ __urdlllocal ur_result_t UR_APICALL urGetProgramExpProcAddrTable( ur_result_t result = UR_RESULT_SUCCESS; - pDdiTable->pfnBuildExp = ur_sanitizer_layer::urProgramBuildExp; - pDdiTable->pfnLinkExp = ur_sanitizer_layer::urProgramLinkExp; + pDdiTable->pfnBuildExp = ur_sanitizer_layer::asan::urProgramBuildExp; + pDdiTable->pfnLinkExp = ur_sanitizer_layer::asan::urProgramLinkExp; return result; } @@ -1761,19 +1765,25 @@ __urdlllocal ur_result_t UR_APICALL urGetEnqueueProcAddrTable( ur_result_t result = UR_RESULT_SUCCESS; - pDdiTable->pfnMemBufferRead = ur_sanitizer_layer::urEnqueueMemBufferRead; - pDdiTable->pfnMemBufferWrite = ur_sanitizer_layer::urEnqueueMemBufferWrite; + pDdiTable->pfnMemBufferRead = + ur_sanitizer_layer::asan::urEnqueueMemBufferRead; + pDdiTable->pfnMemBufferWrite = + ur_sanitizer_layer::asan::urEnqueueMemBufferWrite; pDdiTable->pfnMemBufferReadRect = - ur_sanitizer_layer::urEnqueueMemBufferReadRect; + ur_sanitizer_layer::asan::urEnqueueMemBufferReadRect; pDdiTable->pfnMemBufferWriteRect = - ur_sanitizer_layer::urEnqueueMemBufferWriteRect; - pDdiTable->pfnMemBufferCopy = ur_sanitizer_layer::urEnqueueMemBufferCopy; + ur_sanitizer_layer::asan::urEnqueueMemBufferWriteRect; + pDdiTable->pfnMemBufferCopy = + ur_sanitizer_layer::asan::urEnqueueMemBufferCopy; pDdiTable->pfnMemBufferCopyRect = - ur_sanitizer_layer::urEnqueueMemBufferCopyRect; - pDdiTable->pfnMemBufferFill = ur_sanitizer_layer::urEnqueueMemBufferFill; - pDdiTable->pfnMemBufferMap = ur_sanitizer_layer::urEnqueueMemBufferMap; - pDdiTable->pfnMemUnmap = ur_sanitizer_layer::urEnqueueMemUnmap; - pDdiTable->pfnKernelLaunch = ur_sanitizer_layer::urEnqueueKernelLaunch; + ur_sanitizer_layer::asan::urEnqueueMemBufferCopyRect; + pDdiTable->pfnMemBufferFill = + ur_sanitizer_layer::asan::urEnqueueMemBufferFill; + pDdiTable->pfnMemBufferMap = + ur_sanitizer_layer::asan::urEnqueueMemBufferMap; + pDdiTable->pfnMemUnmap = ur_sanitizer_layer::asan::urEnqueueMemUnmap; + pDdiTable->pfnKernelLaunch = + ur_sanitizer_layer::asan::urEnqueueKernelLaunch; return result; } @@ -1803,14 +1813,16 @@ __urdlllocal ur_result_t UR_APICALL urGetUSMProcAddrTable( ur_result_t result = UR_RESULT_SUCCESS; - pDdiTable->pfnDeviceAlloc = ur_sanitizer_layer::urUSMDeviceAlloc; - pDdiTable->pfnHostAlloc = ur_sanitizer_layer::urUSMHostAlloc; - pDdiTable->pfnSharedAlloc = ur_sanitizer_layer::urUSMSharedAlloc; - pDdiTable->pfnFree = ur_sanitizer_layer::urUSMFree; + pDdiTable->pfnDeviceAlloc = ur_sanitizer_layer::asan::urUSMDeviceAlloc; + pDdiTable->pfnHostAlloc = ur_sanitizer_layer::asan::urUSMHostAlloc; + pDdiTable->pfnSharedAlloc = ur_sanitizer_layer::asan::urUSMSharedAlloc; + pDdiTable->pfnFree = ur_sanitizer_layer::asan::urUSMFree; return result; } +} // namespace asan + ur_result_t context_t::init(ur_dditable_t *dditable, const std::set &enabledLayerNames, [[maybe_unused]] codeloc_data codelocData) { @@ -1818,7 +1830,7 @@ ur_result_t context_t::init(ur_dditable_t *dditable, if (enabledLayerNames.count("UR_LAYER_ASAN")) { enabledType = SanitizerType::AddressSanitizer; - interceptor = std::make_unique(); + initAsanInterceptor(); } else if (enabledLayerNames.count("UR_LAYER_MSAN")) { enabledType = SanitizerType::MemorySanitizer; } else if (enabledLayerNames.count("UR_LAYER_TSAN")) { @@ -1830,61 +1842,50 @@ ur_result_t context_t::init(ur_dditable_t *dditable, return result; } - if (enabledType == SanitizerType::AddressSanitizer) { - if (!(dditable->VirtualMem.pfnReserve && dditable->VirtualMem.pfnMap && - dditable->VirtualMem.pfnGranularityGetInfo)) { - die("Some VirtualMem APIs are needed to enable UR_LAYER_ASAN"); - } - - if (!dditable->PhysicalMem.pfnCreate) { - die("Some PhysicalMem APIs are needed to enable UR_LAYER_ASAN"); - } - } - urDdiTable = *dditable; if (UR_RESULT_SUCCESS == result) { - result = ur_sanitizer_layer::urGetGlobalProcAddrTable( + result = ur_sanitizer_layer::asan::urGetGlobalProcAddrTable( UR_API_VERSION_CURRENT, &dditable->Global); } if (UR_RESULT_SUCCESS == result) { - result = ur_sanitizer_layer::urGetContextProcAddrTable( + result = ur_sanitizer_layer::asan::urGetContextProcAddrTable( UR_API_VERSION_CURRENT, &dditable->Context); } if (UR_RESULT_SUCCESS == result) { - result = ur_sanitizer_layer::urGetKernelProcAddrTable( + result = ur_sanitizer_layer::asan::urGetKernelProcAddrTable( UR_API_VERSION_CURRENT, &dditable->Kernel); } if (UR_RESULT_SUCCESS == result) { - result = ur_sanitizer_layer::urGetProgramProcAddrTable( + result = ur_sanitizer_layer::asan::urGetProgramProcAddrTable( UR_API_VERSION_CURRENT, &dditable->Program); } if (UR_RESULT_SUCCESS == result) { - result = ur_sanitizer_layer::urGetKernelProcAddrTable( + result = ur_sanitizer_layer::asan::urGetKernelProcAddrTable( UR_API_VERSION_CURRENT, &dditable->Kernel); } if (UR_RESULT_SUCCESS == result) { - result = ur_sanitizer_layer::urGetMemProcAddrTable( + result = ur_sanitizer_layer::asan::urGetMemProcAddrTable( UR_API_VERSION_CURRENT, &dditable->Mem); } if (UR_RESULT_SUCCESS == result) { - result = ur_sanitizer_layer::urGetProgramExpProcAddrTable( + result = ur_sanitizer_layer::asan::urGetProgramExpProcAddrTable( UR_API_VERSION_CURRENT, &dditable->ProgramExp); } if (UR_RESULT_SUCCESS == result) { - result = ur_sanitizer_layer::urGetEnqueueProcAddrTable( + result = ur_sanitizer_layer::asan::urGetEnqueueProcAddrTable( UR_API_VERSION_CURRENT, &dditable->Enqueue); } if (UR_RESULT_SUCCESS == result) { - result = ur_sanitizer_layer::urGetUSMProcAddrTable( + result = ur_sanitizer_layer::asan::urGetUSMProcAddrTable( UR_API_VERSION_CURRENT, &dditable->USM); } diff --git a/source/loader/layers/sanitizer/asan/asan_ddi.hpp b/source/loader/layers/sanitizer/asan/asan_ddi.hpp new file mode 100644 index 0000000000..735c4409d8 --- /dev/null +++ b/source/loader/layers/sanitizer/asan/asan_ddi.hpp @@ -0,0 +1,20 @@ +/* + * + * Copyright (C) 2024 Intel Corporation + * + * Part of the Unified-Runtime Project, under the Apache License v2.0 with LLVM Exceptions. + * See LICENSE.TXT + * SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception + * + * @file asan_ddi.hpp + * + */ + +#include "ur_ddi.h" + +namespace ur_sanitizer_layer { + +void initAsanInterceptor(); +void destroyAsanInterceptor(); + +} // namespace ur_sanitizer_layer diff --git a/source/loader/layers/sanitizer/asan_interceptor.cpp b/source/loader/layers/sanitizer/asan/asan_interceptor.cpp similarity index 88% rename from source/loader/layers/sanitizer/asan_interceptor.cpp rename to source/loader/layers/sanitizer/asan/asan_interceptor.cpp index c6c7873cca..a64559540c 100644 --- a/source/loader/layers/sanitizer/asan_interceptor.cpp +++ b/source/loader/layers/sanitizer/asan/asan_interceptor.cpp @@ -1,7 +1,7 @@ //===----------------------------------------------------------------------===// /* * - * Copyright (C) 2023 Intel Corporation + * Copyright (C) 2024 Intel Corporation * * Part of the Unified-Runtime Project, under the Apache License v2.0 with LLVM Exceptions. * See LICENSE.TXT @@ -12,17 +12,19 @@ */ #include "asan_interceptor.hpp" +#include "asan_ddi.hpp" #include "asan_options.hpp" #include "asan_quarantine.hpp" #include "asan_report.hpp" #include "asan_shadow.hpp" #include "asan_validator.hpp" -#include "stacktrace.hpp" -#include "ur_sanitizer_utils.hpp" +#include "sanitizer_common/sanitizer_stacktrace.hpp" +#include "sanitizer_common/sanitizer_utils.hpp" namespace ur_sanitizer_layer { +namespace asan { -SanitizerInterceptor::SanitizerInterceptor() { +AsanInterceptor::AsanInterceptor() { if (getOptions().MaxQuarantineSizeMB) { m_Quarantine = std::make_unique( static_cast(getOptions().MaxQuarantineSizeMB) * 1024 * @@ -30,7 +32,7 @@ SanitizerInterceptor::SanitizerInterceptor() { } } -SanitizerInterceptor::~SanitizerInterceptor() { +AsanInterceptor::~AsanInterceptor() { // We must release these objects before releasing adapters, since // they may use the adapter in their destructor for (const auto &[_, DeviceInfo] : m_DeviceMap) { @@ -57,10 +59,12 @@ SanitizerInterceptor::~SanitizerInterceptor() { /// R -- right redzone (0 or more bytes) /// /// ref: "compiler-rt/lib/asan/asan_allocator.cpp" Allocator::Allocate -ur_result_t SanitizerInterceptor::allocateMemory( - ur_context_handle_t Context, ur_device_handle_t Device, - const ur_usm_desc_t *Properties, ur_usm_pool_handle_t Pool, size_t Size, - AllocType Type, void **ResultPtr) { +ur_result_t AsanInterceptor::allocateMemory(ur_context_handle_t Context, + ur_device_handle_t Device, + const ur_usm_desc_t *Properties, + ur_usm_pool_handle_t Pool, + size_t Size, AllocType Type, + void **ResultPtr) { auto ContextInfo = getContextInfo(Context); std::shared_ptr DeviceInfo = @@ -152,8 +156,8 @@ ur_result_t SanitizerInterceptor::allocateMemory( return UR_RESULT_SUCCESS; } -ur_result_t SanitizerInterceptor::releaseMemory(ur_context_handle_t Context, - void *Ptr) { +ur_result_t AsanInterceptor::releaseMemory(ur_context_handle_t Context, + void *Ptr) { auto ContextInfo = getContextInfo(Context); auto Addr = reinterpret_cast(Ptr); @@ -243,9 +247,9 @@ ur_result_t SanitizerInterceptor::releaseMemory(ur_context_handle_t Context, return UR_RESULT_SUCCESS; } -ur_result_t SanitizerInterceptor::preLaunchKernel(ur_kernel_handle_t Kernel, - ur_queue_handle_t Queue, - USMLaunchInfo &LaunchInfo) { +ur_result_t AsanInterceptor::preLaunchKernel(ur_kernel_handle_t Kernel, + ur_queue_handle_t Queue, + USMLaunchInfo &LaunchInfo) { auto Context = GetContext(Queue); auto Device = GetDevice(Queue); auto ContextInfo = getContextInfo(Context); @@ -268,30 +272,30 @@ ur_result_t SanitizerInterceptor::preLaunchKernel(ur_kernel_handle_t Kernel, return UR_RESULT_SUCCESS; } -ur_result_t SanitizerInterceptor::postLaunchKernel(ur_kernel_handle_t Kernel, - ur_queue_handle_t Queue, - USMLaunchInfo &LaunchInfo) { +ur_result_t AsanInterceptor::postLaunchKernel(ur_kernel_handle_t Kernel, + ur_queue_handle_t Queue, + USMLaunchInfo &LaunchInfo) { // FIXME: We must use block operation here, until we support urEventSetCallback auto Result = getContext()->urDdiTable.Queue.pfnFinish(Queue); if (Result == UR_RESULT_SUCCESS) { - for (const auto &AH : LaunchInfo.Data->SanitizerReport) { - if (!AH.Flag) { + for (const auto &Report : LaunchInfo.Data->Report) { + if (!Report.Flag) { continue; } - switch (AH.ErrorType) { - case DeviceSanitizerErrorType::USE_AFTER_FREE: - ReportUseAfterFree(AH, Kernel, GetContext(Queue)); + switch (Report.ErrorTy) { + case ErrorType::USE_AFTER_FREE: + ReportUseAfterFree(Report, Kernel, GetContext(Queue)); break; - case DeviceSanitizerErrorType::OUT_OF_BOUNDS: - case DeviceSanitizerErrorType::MISALIGNED: - case DeviceSanitizerErrorType::NULL_POINTER: - ReportGenericError(AH, Kernel); + case ErrorType::OUT_OF_BOUNDS: + case ErrorType::MISALIGNED: + case ErrorType::NULL_POINTER: + ReportGenericError(Report, Kernel); break; default: - ReportFatalError(AH); + ReportFatalError(Report); } - if (!AH.IsRecover) { + if (!Report.IsRecover) { exitWithErrors(); } } @@ -318,9 +322,9 @@ ur_result_t DeviceInfo::allocShadowMemory(ur_context_handle_t Context) { /// /// ref: https://github.com/google/sanitizers/wiki/AddressSanitizerAlgorithm#mapping ur_result_t -SanitizerInterceptor::enqueueAllocInfo(std::shared_ptr &DeviceInfo, - ur_queue_handle_t Queue, - std::shared_ptr &AI) { +AsanInterceptor::enqueueAllocInfo(std::shared_ptr &DeviceInfo, + ur_queue_handle_t Queue, + std::shared_ptr &AI) { if (AI->IsReleased) { int ShadowByte; switch (AI->Type) { @@ -393,9 +397,10 @@ SanitizerInterceptor::enqueueAllocInfo(std::shared_ptr &DeviceInfo, return UR_RESULT_SUCCESS; } -ur_result_t SanitizerInterceptor::updateShadowMemory( - std::shared_ptr &ContextInfo, - std::shared_ptr &DeviceInfo, ur_queue_handle_t Queue) { +ur_result_t +AsanInterceptor::updateShadowMemory(std::shared_ptr &ContextInfo, + std::shared_ptr &DeviceInfo, + ur_queue_handle_t Queue) { auto &AllocInfos = ContextInfo->AllocInfosMap[DeviceInfo->Handle]; std::scoped_lock Guard(AllocInfos.Mutex); @@ -407,8 +412,8 @@ ur_result_t SanitizerInterceptor::updateShadowMemory( return UR_RESULT_SUCCESS; } -ur_result_t SanitizerInterceptor::registerProgram(ur_context_handle_t Context, - ur_program_handle_t Program) { +ur_result_t AsanInterceptor::registerProgram(ur_context_handle_t Context, + ur_program_handle_t Program) { std::vector Devices = GetDevices(Program); auto ContextInfo = getContextInfo(Context); @@ -469,8 +474,7 @@ ur_result_t SanitizerInterceptor::registerProgram(ur_context_handle_t Context, return UR_RESULT_SUCCESS; } -ur_result_t -SanitizerInterceptor::unregisterProgram(ur_program_handle_t Program) { +ur_result_t AsanInterceptor::unregisterProgram(ur_program_handle_t Program) { auto ProgramInfo = getProgramInfo(Program); std::scoped_lock Guard( @@ -484,9 +488,8 @@ SanitizerInterceptor::unregisterProgram(ur_program_handle_t Program) { return UR_RESULT_SUCCESS; } -ur_result_t -SanitizerInterceptor::insertContext(ur_context_handle_t Context, - std::shared_ptr &CI) { +ur_result_t AsanInterceptor::insertContext(ur_context_handle_t Context, + std::shared_ptr &CI) { std::scoped_lock Guard(m_ContextMapMutex); if (m_ContextMap.find(Context) != m_ContextMap.end()) { @@ -502,7 +505,7 @@ SanitizerInterceptor::insertContext(ur_context_handle_t Context, return UR_RESULT_SUCCESS; } -ur_result_t SanitizerInterceptor::eraseContext(ur_context_handle_t Context) { +ur_result_t AsanInterceptor::eraseContext(ur_context_handle_t Context) { std::scoped_lock Guard(m_ContextMapMutex); assert(m_ContextMap.find(Context) != m_ContextMap.end()); m_ContextMap.erase(Context); @@ -510,9 +513,8 @@ ur_result_t SanitizerInterceptor::eraseContext(ur_context_handle_t Context) { return UR_RESULT_SUCCESS; } -ur_result_t -SanitizerInterceptor::insertDevice(ur_device_handle_t Device, - std::shared_ptr &DI) { +ur_result_t AsanInterceptor::insertDevice(ur_device_handle_t Device, + std::shared_ptr &DI) { std::scoped_lock Guard(m_DeviceMapMutex); if (m_DeviceMap.find(Device) != m_DeviceMap.end()) { @@ -520,7 +522,7 @@ SanitizerInterceptor::insertDevice(ur_device_handle_t Device, return UR_RESULT_SUCCESS; } - DI = std::make_shared(Device); + DI = std::make_shared(Device); DI->IsSupportSharedSystemUSM = GetDeviceUSMCapability( Device, UR_DEVICE_INFO_USM_SYSTEM_SHARED_SUPPORT); @@ -536,7 +538,7 @@ SanitizerInterceptor::insertDevice(ur_device_handle_t Device, return UR_RESULT_SUCCESS; } -ur_result_t SanitizerInterceptor::eraseDevice(ur_device_handle_t Device) { +ur_result_t AsanInterceptor::eraseDevice(ur_device_handle_t Device) { std::scoped_lock Guard(m_DeviceMapMutex); assert(m_DeviceMap.find(Device) != m_DeviceMap.end()); m_DeviceMap.erase(Device); @@ -544,7 +546,7 @@ ur_result_t SanitizerInterceptor::eraseDevice(ur_device_handle_t Device) { return UR_RESULT_SUCCESS; } -ur_result_t SanitizerInterceptor::insertProgram(ur_program_handle_t Program) { +ur_result_t AsanInterceptor::insertProgram(ur_program_handle_t Program) { std::scoped_lock Guard(m_ProgramMapMutex); if (m_ProgramMap.find(Program) != m_ProgramMap.end()) { return UR_RESULT_SUCCESS; @@ -553,14 +555,14 @@ ur_result_t SanitizerInterceptor::insertProgram(ur_program_handle_t Program) { return UR_RESULT_SUCCESS; } -ur_result_t SanitizerInterceptor::eraseProgram(ur_program_handle_t Program) { +ur_result_t AsanInterceptor::eraseProgram(ur_program_handle_t Program) { std::scoped_lock Guard(m_ProgramMapMutex); assert(m_ProgramMap.find(Program) != m_ProgramMap.end()); m_ProgramMap.erase(Program); return UR_RESULT_SUCCESS; } -ur_result_t SanitizerInterceptor::insertKernel(ur_kernel_handle_t Kernel) { +ur_result_t AsanInterceptor::insertKernel(ur_kernel_handle_t Kernel) { std::scoped_lock Guard(m_KernelMapMutex); if (m_KernelMap.find(Kernel) != m_KernelMap.end()) { return UR_RESULT_SUCCESS; @@ -569,7 +571,7 @@ ur_result_t SanitizerInterceptor::insertKernel(ur_kernel_handle_t Kernel) { return UR_RESULT_SUCCESS; } -ur_result_t SanitizerInterceptor::eraseKernel(ur_kernel_handle_t Kernel) { +ur_result_t AsanInterceptor::eraseKernel(ur_kernel_handle_t Kernel) { std::scoped_lock Guard(m_KernelMapMutex); assert(m_KernelMap.find(Kernel) != m_KernelMap.end()); m_KernelMap.erase(Kernel); @@ -577,7 +579,7 @@ ur_result_t SanitizerInterceptor::eraseKernel(ur_kernel_handle_t Kernel) { } ur_result_t -SanitizerInterceptor::insertMemBuffer(std::shared_ptr MemBuffer) { +AsanInterceptor::insertMemBuffer(std::shared_ptr MemBuffer) { std::scoped_lock Guard(m_MemBufferMapMutex); assert(m_MemBufferMap.find(ur_cast(MemBuffer.get())) == m_MemBufferMap.end()); @@ -586,7 +588,7 @@ SanitizerInterceptor::insertMemBuffer(std::shared_ptr MemBuffer) { return UR_RESULT_SUCCESS; } -ur_result_t SanitizerInterceptor::eraseMemBuffer(ur_mem_handle_t MemHandle) { +ur_result_t AsanInterceptor::eraseMemBuffer(ur_mem_handle_t MemHandle) { std::scoped_lock Guard(m_MemBufferMapMutex); assert(m_MemBufferMap.find(MemHandle) != m_MemBufferMap.end()); m_MemBufferMap.erase(MemHandle); @@ -594,7 +596,7 @@ ur_result_t SanitizerInterceptor::eraseMemBuffer(ur_mem_handle_t MemHandle) { } std::shared_ptr -SanitizerInterceptor::getMemBuffer(ur_mem_handle_t MemHandle) { +AsanInterceptor::getMemBuffer(ur_mem_handle_t MemHandle) { std::shared_lock Guard(m_MemBufferMapMutex); if (m_MemBufferMap.find(MemHandle) != m_MemBufferMap.end()) { return m_MemBufferMap[MemHandle]; @@ -602,7 +604,7 @@ SanitizerInterceptor::getMemBuffer(ur_mem_handle_t MemHandle) { return nullptr; } -ur_result_t SanitizerInterceptor::prepareLaunch( +ur_result_t AsanInterceptor::prepareLaunch( std::shared_ptr &ContextInfo, std::shared_ptr &DeviceInfo, ur_queue_handle_t Queue, ur_kernel_handle_t Kernel, USMLaunchInfo &LaunchInfo) { @@ -808,7 +810,7 @@ ur_result_t SanitizerInterceptor::prepareLaunch( } std::optional -SanitizerInterceptor::findAllocInfoByAddress(uptr Address) { +AsanInterceptor::findAllocInfoByAddress(uptr Address) { std::shared_lock Guard(m_AllocationMapMutex); auto It = m_AllocationMap.upper_bound(Address); if (It == m_AllocationMap.begin()) { @@ -823,7 +825,7 @@ SanitizerInterceptor::findAllocInfoByAddress(uptr Address) { } std::vector -SanitizerInterceptor::findAllocInfoByContext(ur_context_handle_t Context) { +AsanInterceptor::findAllocInfoByContext(ur_context_handle_t Context) { std::shared_lock Guard(m_AllocationMapMutex); std::vector AllocInfos; for (auto It = m_AllocationMap.begin(); It != m_AllocationMap.end(); It++) { @@ -843,9 +845,9 @@ ContextInfo::~ContextInfo() { assert(Result == UR_RESULT_SUCCESS); // check memory leaks - if (getContext()->interceptor->isNormalExit()) { + if (getAsanInterceptor()->isNormalExit()) { std::vector AllocInfos = - getContext()->interceptor->findAllocInfoByContext(Handle); + getAsanInterceptor()->findAllocInfoByContext(Handle); for (const auto &It : AllocInfos) { const auto &[_, AI] = *It; if (!AI->IsReleased) { @@ -886,7 +888,7 @@ USMLaunchInfo::~USMLaunchInfo() { [[maybe_unused]] ur_result_t Result; if (Data) { auto Type = GetDeviceType(Context, Device); - auto ContextInfo = getContext()->interceptor->getContextInfo(Context); + auto ContextInfo = getAsanInterceptor()->getContextInfo(Context); if (Type == DeviceType::GPU_PVC || Type == DeviceType::GPU_DG2) { if (Data->PrivateShadowOffset) { ContextInfo->Stats.UpdateShadowFreed( @@ -918,4 +920,24 @@ USMLaunchInfo::~USMLaunchInfo() { assert(Result == UR_RESULT_SUCCESS); } +} // namespace asan + +using namespace asan; + +static AsanInterceptor *interceptor; + +AsanInterceptor *getAsanInterceptor() { return interceptor; } + +void initAsanInterceptor() { + if (interceptor) { + return; + } + interceptor = new AsanInterceptor(); +} + +void destroyAsanInterceptor() { + delete interceptor; + interceptor = nullptr; +} + } // namespace ur_sanitizer_layer diff --git a/source/loader/layers/sanitizer/asan_interceptor.hpp b/source/loader/layers/sanitizer/asan/asan_interceptor.hpp similarity index 97% rename from source/loader/layers/sanitizer/asan_interceptor.hpp rename to source/loader/layers/sanitizer/asan/asan_interceptor.hpp index c1bf710425..1f8d6dab31 100644 --- a/source/loader/layers/sanitizer/asan_interceptor.hpp +++ b/source/loader/layers/sanitizer/asan/asan_interceptor.hpp @@ -1,6 +1,6 @@ /* * - * Copyright (C) 2023 Intel Corporation + * Copyright (C) 2024 Intel Corporation * * Part of the Unified-Runtime Project, under the Apache License v2.0 with LLVM Exceptions. * See LICENSE.TXT @@ -18,7 +18,7 @@ #include "asan_options.hpp" #include "asan_shadow.hpp" #include "asan_statistics.hpp" -#include "common.hpp" +#include "sanitizer_common/sanitizer_common.hpp" #include "ur_sanitizer_layer.hpp" #include @@ -29,6 +29,7 @@ #include namespace ur_sanitizer_layer { +namespace asan { class Quarantine; @@ -186,11 +187,11 @@ struct DeviceGlobalInfo { uptr Addr; }; -class SanitizerInterceptor { +class AsanInterceptor { public: - explicit SanitizerInterceptor(); + explicit AsanInterceptor(); - ~SanitizerInterceptor(); + ~AsanInterceptor(); ur_result_t allocateMemory(ur_context_handle_t Context, ur_device_handle_t Device, @@ -331,4 +332,8 @@ class SanitizerInterceptor { bool m_NormalExit = true; }; +} // namespace asan + +asan::AsanInterceptor *getAsanInterceptor(); + } // namespace ur_sanitizer_layer diff --git a/source/loader/layers/sanitizer/asan_libdevice.hpp b/source/loader/layers/sanitizer/asan/asan_libdevice.hpp similarity index 54% rename from source/loader/layers/sanitizer/asan_libdevice.hpp rename to source/loader/layers/sanitizer/asan/asan_libdevice.hpp index 14e26a5f7e..2c15d99d87 100644 --- a/source/loader/layers/sanitizer/asan_libdevice.hpp +++ b/source/loader/layers/sanitizer/asan/asan_libdevice.hpp @@ -1,47 +1,24 @@ /* * - * Copyright (C) 2023 Intel Corporation + * Copyright (C) 2024 Intel Corporation * * Part of the Unified-Runtime Project, under the Apache License v2.0 with LLVM Exceptions. * See LICENSE.TXT * SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception * - * @file device_sanitizer_report.hpp + * @file asan_libdevice.hpp * */ #pragma once -#include +#include "sanitizer_common/sanitizer_libdevice.hpp" #if !defined(__SPIR__) && !defined(__SPIRV__) namespace ur_sanitizer_layer { #endif // !__SPIR__ && !__SPIRV__ -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 { +struct AsanErrorReport { int Flag = 0; char File[256 + 1] = {}; @@ -60,8 +37,8 @@ struct DeviceSanitizerReport { uintptr_t Address = 0; bool IsWrite = false; uint32_t AccessSize = 0; - DeviceSanitizerMemoryType MemoryType = DeviceSanitizerMemoryType::UNKNOWN; - DeviceSanitizerErrorType ErrorType = DeviceSanitizerErrorType::UNKNOWN; + MemoryType MemoryTy = MemoryType::UNKNOWN; + ErrorType ErrorTy = ErrorType::UNKNOWN; bool IsRecover = false; }; @@ -90,7 +67,7 @@ struct LaunchInfo { uint32_t Debug = 0; int ReportFlag = 0; - DeviceSanitizerReport SanitizerReport[ASAN_MAX_NUM_REPORTS]; + AsanErrorReport Report[ASAN_MAX_NUM_REPORTS]; }; constexpr unsigned ASAN_SHADOW_SCALE = 4; @@ -123,46 +100,6 @@ const int kPrivateRightRedzoneMagic = (char)0xf3; 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"; - } -} - #if !defined(__SPIR__) && !defined(__SPIRV__) } // namespace ur_sanitizer_layer #endif // !__SPIR__ && !__SPIRV__ diff --git a/source/loader/layers/sanitizer/asan_options.cpp b/source/loader/layers/sanitizer/asan/asan_options.cpp similarity index 98% rename from source/loader/layers/sanitizer/asan_options.cpp rename to source/loader/layers/sanitizer/asan/asan_options.cpp index 5c42ab8fca..5953a75c85 100644 --- a/source/loader/layers/sanitizer/asan_options.cpp +++ b/source/loader/layers/sanitizer/asan/asan_options.cpp @@ -11,6 +11,8 @@ */ #include "asan_options.hpp" + +#include "ur/ur.hpp" #include "ur_sanitizer_layer.hpp" #include @@ -18,6 +20,7 @@ #include namespace ur_sanitizer_layer { +namespace asan { AsanOptions::AsanOptions() { std::optional OptionsEnvMap; @@ -139,4 +142,5 @@ AsanOptions::AsanOptions() { } } +} // namespace asan } // namespace ur_sanitizer_layer diff --git a/source/loader/layers/sanitizer/asan_options.hpp b/source/loader/layers/sanitizer/asan/asan_options.hpp similarity index 91% rename from source/loader/layers/sanitizer/asan_options.hpp rename to source/loader/layers/sanitizer/asan/asan_options.hpp index 4c515e28fe..1385fdf6e3 100644 --- a/source/loader/layers/sanitizer/asan_options.hpp +++ b/source/loader/layers/sanitizer/asan/asan_options.hpp @@ -12,9 +12,10 @@ #pragma once -#include "common.hpp" +#include namespace ur_sanitizer_layer { +namespace asan { struct AsanOptions { bool Debug = false; @@ -29,4 +30,5 @@ struct AsanOptions { explicit AsanOptions(); }; +} // namespace asan } // namespace ur_sanitizer_layer diff --git a/source/loader/layers/sanitizer/asan_quarantine.cpp b/source/loader/layers/sanitizer/asan/asan_quarantine.cpp similarity index 96% rename from source/loader/layers/sanitizer/asan_quarantine.cpp rename to source/loader/layers/sanitizer/asan/asan_quarantine.cpp index 9826aeb62a..dab9b45e64 100644 --- a/source/loader/layers/sanitizer/asan_quarantine.cpp +++ b/source/loader/layers/sanitizer/asan/asan_quarantine.cpp @@ -13,6 +13,7 @@ #include "asan_quarantine.hpp" namespace ur_sanitizer_layer { +namespace asan { std::vector Quarantine::put(ur_device_handle_t Device, AllocationIterator &It) { @@ -33,4 +34,5 @@ std::vector Quarantine::put(ur_device_handle_t Device, return DequeueList; } +} // namespace asan } // namespace ur_sanitizer_layer diff --git a/source/loader/layers/sanitizer/asan_quarantine.hpp b/source/loader/layers/sanitizer/asan/asan_quarantine.hpp similarity index 97% rename from source/loader/layers/sanitizer/asan_quarantine.hpp rename to source/loader/layers/sanitizer/asan/asan_quarantine.hpp index 6dc15d382a..af5963dc25 100644 --- a/source/loader/layers/sanitizer/asan_quarantine.hpp +++ b/source/loader/layers/sanitizer/asan/asan_quarantine.hpp @@ -20,6 +20,7 @@ #include namespace ur_sanitizer_layer { +namespace asan { class QuarantineCache { public: @@ -71,4 +72,5 @@ class Quarantine { size_t m_MaxQuarantineSize; }; +} // namespace asan } // namespace ur_sanitizer_layer diff --git a/source/loader/layers/sanitizer/asan_report.cpp b/source/loader/layers/sanitizer/asan/asan_report.cpp similarity index 90% rename from source/loader/layers/sanitizer/asan_report.cpp rename to source/loader/layers/sanitizer/asan/asan_report.cpp index 7843b0f690..fe7c1e0f87 100644 --- a/source/loader/layers/sanitizer/asan_report.cpp +++ b/source/loader/layers/sanitizer/asan/asan_report.cpp @@ -16,10 +16,11 @@ #include "asan_libdevice.hpp" #include "asan_options.hpp" #include "asan_validator.hpp" +#include "sanitizer_common/sanitizer_utils.hpp" #include "ur_sanitizer_layer.hpp" -#include "ur_sanitizer_utils.hpp" namespace ur_sanitizer_layer { +namespace asan { namespace { @@ -89,12 +90,12 @@ void ReportMemoryLeak(const std::shared_ptr &AI) { AI->AllocStack.print(); } -void ReportFatalError(const DeviceSanitizerReport &Report) { +void ReportFatalError(const AsanErrorReport &Report) { getContext()->logger.always("\n====ERROR: DeviceSanitizer: {}", - ToString(Report.ErrorType)); + ToString(Report.ErrorTy)); } -void ReportGenericError(const DeviceSanitizerReport &Report, +void ReportGenericError(const AsanErrorReport &Report, ur_kernel_handle_t Kernel) { const char *File = Report.File[0] ? Report.File : ""; const char *Func = Report.Func[0] ? Report.Func : ""; @@ -103,10 +104,9 @@ void ReportGenericError(const DeviceSanitizerReport &Report, // Try to demangle the kernel name KernelName = DemangleName(KernelName); - getContext()->logger.always("\n====ERROR: DeviceSanitizer: {} on {} ({})", - ToString(Report.ErrorType), - ToString(Report.MemoryType), - (void *)Report.Address); + getContext()->logger.always( + "\n====ERROR: DeviceSanitizer: {} on {} ({})", ToString(Report.ErrorTy), + ToString(Report.MemoryTy), (void *)Report.Address); getContext()->logger.always( "{} of size {} at kernel <{}> LID({}, {}, {}) GID({}, " "{}, {})", @@ -116,7 +116,7 @@ void ReportGenericError(const DeviceSanitizerReport &Report, getContext()->logger.always(" #0 {} {}:{}", Func, File, Report.Line); } -void ReportUseAfterFree(const DeviceSanitizerReport &Report, +void ReportUseAfterFree(const AsanErrorReport &Report, ur_kernel_handle_t Kernel, ur_context_handle_t Context) { const char *File = Report.File[0] ? Report.File : ""; @@ -128,7 +128,7 @@ void ReportUseAfterFree(const DeviceSanitizerReport &Report, getContext()->logger.always( "\n====ERROR: DeviceSanitizer: {} on address {}", - ToString(Report.ErrorType), (void *)Report.Address); + ToString(Report.ErrorTy), (void *)Report.Address); getContext()->logger.always( "{} of size {} at kernel <{}> LID({}, {}, {}) GID({}, " "{}, {})", @@ -138,9 +138,9 @@ void ReportUseAfterFree(const DeviceSanitizerReport &Report, getContext()->logger.always(" #0 {} {}:{}", Func, File, Report.Line); getContext()->logger.always(""); - if (getContext()->interceptor->getOptions().MaxQuarantineSizeMB > 0) { + if (getAsanInterceptor()->getOptions().MaxQuarantineSizeMB > 0) { auto AllocInfoItOp = - getContext()->interceptor->findAllocInfoByAddress(Report.Address); + getAsanInterceptor()->findAllocInfoByAddress(Report.Address); if (!AllocInfoItOp) { getContext()->logger.always( @@ -210,4 +210,5 @@ void ReportInvalidKernelArgument(ur_kernel_handle_t Kernel, uint32_t ArgIndex, } } +} // namespace asan } // namespace ur_sanitizer_layer diff --git a/source/loader/layers/sanitizer/asan_report.hpp b/source/loader/layers/sanitizer/asan/asan_report.hpp similarity index 82% rename from source/loader/layers/sanitizer/asan_report.hpp rename to source/loader/layers/sanitizer/asan/asan_report.hpp index e679b30c5d..c92ef997af 100644 --- a/source/loader/layers/sanitizer/asan_report.hpp +++ b/source/loader/layers/sanitizer/asan/asan_report.hpp @@ -12,15 +12,18 @@ #pragma once -#include "common.hpp" +#include "sanitizer_common/sanitizer_common.hpp" #include namespace ur_sanitizer_layer { -struct DeviceSanitizerReport; -struct AllocInfo; +struct AsanErrorReport; struct StackTrace; + +namespace asan { + +struct AllocInfo; struct ValidateUSMResult; void ReportBadFree(uptr Addr, const StackTrace &stack, @@ -36,16 +39,17 @@ void ReportMemoryLeak(const std::shared_ptr &AI); // This type of error is usually unexpected mistake and doesn't have enough // debug information -void ReportFatalError(const DeviceSanitizerReport &Report); +void ReportFatalError(const AsanErrorReport &Report); -void ReportGenericError(const DeviceSanitizerReport &Report, +void ReportGenericError(const AsanErrorReport &Report, ur_kernel_handle_t Kernel); -void ReportUseAfterFree(const DeviceSanitizerReport &Report, +void ReportUseAfterFree(const AsanErrorReport &Report, ur_kernel_handle_t Kernel, ur_context_handle_t Context); void ReportInvalidKernelArgument(ur_kernel_handle_t Kernel, uint32_t ArgIndex, uptr Addr, const ValidateUSMResult &VR, StackTrace Stack); +} // namespace asan } // namespace ur_sanitizer_layer diff --git a/source/loader/layers/sanitizer/asan_shadow.cpp b/source/loader/layers/sanitizer/asan/asan_shadow.cpp similarity index 98% rename from source/loader/layers/sanitizer/asan_shadow.cpp rename to source/loader/layers/sanitizer/asan/asan_shadow.cpp index 629ce3a491..59897a426f 100644 --- a/source/loader/layers/sanitizer/asan_shadow.cpp +++ b/source/loader/layers/sanitizer/asan/asan_shadow.cpp @@ -13,10 +13,11 @@ #include "asan_shadow.hpp" #include "asan_interceptor.hpp" #include "asan_libdevice.hpp" +#include "sanitizer_common/sanitizer_utils.hpp" #include "ur_sanitizer_layer.hpp" -#include "ur_sanitizer_utils.hpp" namespace ur_sanitizer_layer { +namespace asan { std::shared_ptr GetShadowMemory(ur_context_handle_t Context, ur_device_handle_t Device, @@ -207,7 +208,7 @@ ur_result_t ShadowMemoryGPU::EnqueuePoisonShadow(ur_queue_handle_t Queue, } auto AllocInfoIt = - getContext()->interceptor->findAllocInfoByAddress(Ptr); + getAsanInterceptor()->findAllocInfoByAddress(Ptr); assert(AllocInfoIt); VirtualMemMaps[MappedPtr].second.insert((*AllocInfoIt)->second); } @@ -273,4 +274,5 @@ uptr ShadowMemoryDG2::MemToShadow(uptr Ptr) { } } +} // namespace asan } // namespace ur_sanitizer_layer diff --git a/source/loader/layers/sanitizer/asan_shadow.hpp b/source/loader/layers/sanitizer/asan/asan_shadow.hpp similarity index 97% rename from source/loader/layers/sanitizer/asan_shadow.hpp rename to source/loader/layers/sanitizer/asan/asan_shadow.hpp index 7ae095062a..6e33066b2f 100644 --- a/source/loader/layers/sanitizer/asan_shadow.hpp +++ b/source/loader/layers/sanitizer/asan/asan_shadow.hpp @@ -12,11 +12,13 @@ #pragma once -#include "asan_allocator.hpp" -#include "common.hpp" +#include "asan/asan_allocator.hpp" +#include "sanitizer_common/sanitizer_libdevice.hpp" + #include namespace ur_sanitizer_layer { +namespace asan { struct ShadowMemory { ShadowMemory(ur_context_handle_t Context, ur_device_handle_t Device) @@ -132,4 +134,5 @@ std::shared_ptr GetShadowMemory(ur_context_handle_t Context, ur_device_handle_t Device, DeviceType Type); +} // namespace asan } // namespace ur_sanitizer_layer diff --git a/source/loader/layers/sanitizer/asan_statistics.cpp b/source/loader/layers/sanitizer/asan/asan_statistics.cpp similarity index 96% rename from source/loader/layers/sanitizer/asan_statistics.cpp rename to source/loader/layers/sanitizer/asan/asan_statistics.cpp index 82eef69c44..5525e2aa7c 100644 --- a/source/loader/layers/sanitizer/asan_statistics.cpp +++ b/source/loader/layers/sanitizer/asan/asan_statistics.cpp @@ -17,6 +17,7 @@ #include namespace ur_sanitizer_layer { +namespace asan { struct AsanStats { void UpdateUSMMalloced(uptr MallocedSize, uptr RedzoneSize); @@ -66,7 +67,7 @@ void AsanStats::UpdateUSMFreed(uptr FreedSize) { void AsanStats::UpdateUSMRealFreed(uptr FreedSize, uptr RedzoneSize) { UsmMalloced -= FreedSize; UsmMallocedRedzones -= RedzoneSize; - if (getContext()->interceptor->getOptions().MaxQuarantineSizeMB) { + if (getAsanInterceptor()->getOptions().MaxQuarantineSizeMB) { UsmFreed -= FreedSize; } getContext()->logger.debug( @@ -136,11 +137,12 @@ void AsanStatsWrapper::Print(ur_context_handle_t Context) { } AsanStatsWrapper::AsanStatsWrapper() : Stat(nullptr) { - if (getContext()->interceptor->getOptions().PrintStats) { + if (getAsanInterceptor()->getOptions().PrintStats) { Stat = new AsanStats; } } AsanStatsWrapper::~AsanStatsWrapper() { delete Stat; } +} // namespace asan } // namespace ur_sanitizer_layer diff --git a/source/loader/layers/sanitizer/asan_statistics.hpp b/source/loader/layers/sanitizer/asan/asan_statistics.hpp similarity index 90% rename from source/loader/layers/sanitizer/asan_statistics.hpp rename to source/loader/layers/sanitizer/asan/asan_statistics.hpp index fab30e28c0..ea336880d2 100644 --- a/source/loader/layers/sanitizer/asan_statistics.hpp +++ b/source/loader/layers/sanitizer/asan/asan_statistics.hpp @@ -12,9 +12,10 @@ #pragma once -#include "common.hpp" +#include "sanitizer_common/sanitizer_common.hpp" namespace ur_sanitizer_layer { +namespace asan { struct AsanStats; @@ -36,4 +37,5 @@ struct AsanStatsWrapper { AsanStats *Stat; }; +} // namespace asan } // namespace ur_sanitizer_layer diff --git a/source/loader/layers/sanitizer/asan_validator.cpp b/source/loader/layers/sanitizer/asan/asan_validator.cpp similarity index 90% rename from source/loader/layers/sanitizer/asan_validator.cpp rename to source/loader/layers/sanitizer/asan/asan_validator.cpp index a9f2bd2b17..14c9042e46 100644 --- a/source/loader/layers/sanitizer/asan_validator.cpp +++ b/source/loader/layers/sanitizer/asan/asan_validator.cpp @@ -12,9 +12,10 @@ #include "asan_validator.hpp" #include "asan_interceptor.hpp" -#include "ur_sanitizer_utils.hpp" +#include "sanitizer_common/sanitizer_utils.hpp" namespace ur_sanitizer_layer { +namespace asan { namespace { @@ -38,9 +39,9 @@ ValidateUSMResult ValidateUSMPointer(ur_context_handle_t Context, ur_device_handle_t Device, uptr Ptr) { assert(Ptr != 0 && "Don't validate nullptr here"); - auto AllocInfoItOp = getContext()->interceptor->findAllocInfoByAddress(Ptr); + auto AllocInfoItOp = getAsanInterceptor()->findAllocInfoByAddress(Ptr); if (!AllocInfoItOp) { - auto DI = getContext()->interceptor->getDeviceInfo(Device); + auto DI = getAsanInterceptor()->getDeviceInfo(Device); bool IsSupportSharedSystemUSM = DI->IsSupportSharedSystemUSM; if (IsSupportSharedSystemUSM) { // maybe it's host pointer @@ -74,4 +75,5 @@ ValidateUSMResult ValidateUSMPointer(ur_context_handle_t Context, return ValidateUSMResult::success(); } +} // namespace asan } // namespace ur_sanitizer_layer diff --git a/source/loader/layers/sanitizer/asan_validator.hpp b/source/loader/layers/sanitizer/asan/asan_validator.hpp similarity index 97% rename from source/loader/layers/sanitizer/asan_validator.hpp rename to source/loader/layers/sanitizer/asan/asan_validator.hpp index 52db966562..f3347740ea 100644 --- a/source/loader/layers/sanitizer/asan_validator.hpp +++ b/source/loader/layers/sanitizer/asan/asan_validator.hpp @@ -14,6 +14,7 @@ #include "asan_allocator.hpp" namespace ur_sanitizer_layer { +namespace asan { struct ValidateUSMResult { enum ErrorType { @@ -47,4 +48,5 @@ struct ValidateUSMResult { ValidateUSMResult ValidateUSMPointer(ur_context_handle_t Context, ur_device_handle_t Device, uptr Ptr); +} // namespace asan } // namespace ur_sanitizer_layer diff --git a/source/loader/layers/sanitizer/linux/backtrace.cpp b/source/loader/layers/sanitizer/sanitizer_common/linux/backtrace.cpp similarity index 91% rename from source/loader/layers/sanitizer/linux/backtrace.cpp rename to source/loader/layers/sanitizer/sanitizer_common/linux/backtrace.cpp index b746348205..6015b9d239 100644 --- a/source/loader/layers/sanitizer/linux/backtrace.cpp +++ b/source/loader/layers/sanitizer/sanitizer_common/linux/backtrace.cpp @@ -6,8 +6,10 @@ * See LICENSE.TXT * SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception * + * @file backtrace.cpp + * */ -#include "stacktrace.hpp" +#include "sanitizer_common/sanitizer_stacktrace.hpp" #include #include diff --git a/source/loader/layers/sanitizer/linux/sanitizer_utils.cpp b/source/loader/layers/sanitizer/sanitizer_common/linux/sanitizer_utils.cpp similarity index 92% rename from source/loader/layers/sanitizer/linux/sanitizer_utils.cpp rename to source/loader/layers/sanitizer/sanitizer_common/linux/sanitizer_utils.cpp index d0bc038174..380482ff84 100644 --- a/source/loader/layers/sanitizer/linux/sanitizer_utils.cpp +++ b/source/loader/layers/sanitizer/sanitizer_common/linux/sanitizer_utils.cpp @@ -1,7 +1,6 @@ -//===----------------------------------------------------------------------===// /* * - * Copyright (C) 2023 Intel Corporation + * Copyright (C) 2024 Intel Corporation * * Part of the Unified-Runtime Project, under the Apache License v2.0 with LLVM Exceptions. * See LICENSE.TXT @@ -11,7 +10,7 @@ * */ -#include "common.hpp" +#include "sanitizer_common/sanitizer_common.hpp" #include "ur_sanitizer_layer.hpp" #include diff --git a/source/loader/layers/sanitizer/linux/symbolizer.cpp b/source/loader/layers/sanitizer/sanitizer_common/linux/symbolizer.cpp similarity index 98% rename from source/loader/layers/sanitizer/linux/symbolizer.cpp rename to source/loader/layers/sanitizer/sanitizer_common/linux/symbolizer.cpp index b1968389e5..98dcba9077 100644 --- a/source/loader/layers/sanitizer/linux/symbolizer.cpp +++ b/source/loader/layers/sanitizer/sanitizer_common/linux/symbolizer.cpp @@ -6,6 +6,8 @@ * See LICENSE.TXT * SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception * + * @file symbolizer.cpp + * */ #include "llvm/DebugInfo/Symbolize/DIPrinter.h" #include "llvm/DebugInfo/Symbolize/Symbolize.h" diff --git a/source/loader/layers/sanitizer/sanitizer_common/sanitizer_allocator.hpp b/source/loader/layers/sanitizer/sanitizer_common/sanitizer_allocator.hpp new file mode 100644 index 0000000000..3176757216 --- /dev/null +++ b/source/loader/layers/sanitizer/sanitizer_common/sanitizer_allocator.hpp @@ -0,0 +1,43 @@ +/* + * + * Copyright (C) 2024 Intel Corporation + * + * Part of the Unified-Runtime Project, under the Apache License v2.0 with LLVM Exceptions. + * See LICENSE.TXT + * SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception + * + * @file sanitizer_allocator.hpp + * + */ + +#pragma once + +namespace ur_sanitizer_layer { + +enum class AllocType { + UNKNOWN, + DEVICE_USM, + SHARED_USM, + HOST_USM, + MEM_BUFFER, + DEVICE_GLOBAL +}; + +inline const char *ToString(AllocType Type) { + switch (Type) { + case AllocType::DEVICE_USM: + return "Device USM"; + case AllocType::HOST_USM: + return "Host USM"; + case AllocType::SHARED_USM: + return "Shared USM"; + case AllocType::MEM_BUFFER: + return "Memory Buffer"; + case AllocType::DEVICE_GLOBAL: + return "Device Global"; + default: + return "Unknown Type"; + } +} + +} // namespace ur_sanitizer_layer diff --git a/source/loader/layers/sanitizer/common.hpp b/source/loader/layers/sanitizer/sanitizer_common/sanitizer_common.hpp similarity index 91% rename from source/loader/layers/sanitizer/common.hpp rename to source/loader/layers/sanitizer/sanitizer_common/sanitizer_common.hpp index ea5e33ed4b..147bd23be3 100644 --- a/source/loader/layers/sanitizer/common.hpp +++ b/source/loader/layers/sanitizer/sanitizer_common/sanitizer_common.hpp @@ -1,18 +1,17 @@ /* * - * Copyright (C) 2023 Intel Corporation + * Copyright (C) 2024 Intel Corporation * * Part of the Unified-Runtime Project, under the Apache License v2.0 with LLVM Exceptions. * See LICENSE.TXT * SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception * - * @file common.hpp + * @file sanitizer_common.hpp * */ #pragma once -#include "asan_libdevice.hpp" #include "ur/ur.hpp" #include "ur_ddi.h" @@ -138,21 +137,6 @@ struct SourceInfo { int column = 0; }; -inline const char *ToString(DeviceType Type) { - switch (Type) { - case DeviceType::UNKNOWN: - return "UNKNOWN"; - case DeviceType::CPU: - return "CPU"; - case DeviceType::GPU_PVC: - return "PVC"; - case DeviceType::GPU_DG2: - return "DG2"; - default: - return "UNKNOWN"; - } -} - bool IsInASanContext(); uptr MmapNoReserve(uptr Addr, uptr Size); diff --git a/source/loader/layers/sanitizer/sanitizer_common/sanitizer_libdevice.hpp b/source/loader/layers/sanitizer/sanitizer_common/sanitizer_libdevice.hpp new file mode 100644 index 0000000000..75e3fb9ee4 --- /dev/null +++ b/source/loader/layers/sanitizer/sanitizer_common/sanitizer_libdevice.hpp @@ -0,0 +1,101 @@ +/* + * + * Copyright (C) 2024 Intel Corporation + * + * Part of the Unified-Runtime Project, under the Apache License v2.0 with LLVM Exceptions. + * See LICENSE.TXT + * SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception + * + * @file sanitizer_libdevice.hpp + * + */ + +#pragma once + +#include + +#if !defined(__SPIR__) && !defined(__SPIRV__) +namespace ur_sanitizer_layer { +#endif // !__SPIR__ && !__SPIRV__ + +enum class DeviceType : uint32_t { UNKNOWN = 0, CPU, GPU_PVC, GPU_DG2 }; + +inline const char *ToString(DeviceType Type) { + switch (Type) { + case DeviceType::UNKNOWN: + return "UNKNOWN"; + case DeviceType::CPU: + return "CPU"; + case DeviceType::GPU_PVC: + return "PVC"; + case DeviceType::GPU_DG2: + return "DG2"; + default: + return "UNKNOWN"; + } +} + +enum class ErrorType : int32_t { + UNKNOWN, + OUT_OF_BOUNDS, + MISALIGNED, + USE_AFTER_FREE, + OUT_OF_SHADOW_BOUNDS, + UNKNOWN_DEVICE, + NULL_POINTER, +}; + +inline const char *ToString(ErrorType ErrorType) { + switch (ErrorType) { + case ErrorType::OUT_OF_BOUNDS: + return "out-of-bounds-access"; + case ErrorType::MISALIGNED: + return "misaligned-access"; + case ErrorType::USE_AFTER_FREE: + return "use-after-free"; + case ErrorType::OUT_OF_SHADOW_BOUNDS: + return "out-of-shadow-bounds-access"; + case ErrorType::UNKNOWN_DEVICE: + return "unknown-device"; + case ErrorType::NULL_POINTER: + return "null-pointer-access"; + default: + return "unknown-error"; + } +} + +enum class MemoryType : int32_t { + UNKNOWN, + USM_DEVICE, + USM_HOST, + USM_SHARED, + LOCAL, + PRIVATE, + MEM_BUFFER, + DEVICE_GLOBAL, +}; + +inline const char *ToString(MemoryType MemoryType) { + switch (MemoryType) { + case MemoryType::USM_DEVICE: + return "Device USM"; + case MemoryType::USM_HOST: + return "Host USM"; + case MemoryType::USM_SHARED: + return "Shared USM"; + case MemoryType::LOCAL: + return "Local Memory"; + case MemoryType::PRIVATE: + return "Private Memory"; + case MemoryType::MEM_BUFFER: + return "Memory Buffer"; + case MemoryType::DEVICE_GLOBAL: + return "Device Global"; + default: + return "Unknown Memory"; + } +} + +#if !defined(__SPIR__) && !defined(__SPIRV__) +} // namespace ur_sanitizer_layer +#endif // !__SPIR__ && !__SPIRV__ diff --git a/source/loader/layers/sanitizer/stacktrace.cpp b/source/loader/layers/sanitizer/sanitizer_common/sanitizer_stacktrace.cpp similarity index 92% rename from source/loader/layers/sanitizer/stacktrace.cpp rename to source/loader/layers/sanitizer/sanitizer_common/sanitizer_stacktrace.cpp index ed7f01c9cf..357eff4b77 100644 --- a/source/loader/layers/sanitizer/stacktrace.cpp +++ b/source/loader/layers/sanitizer/sanitizer_common/sanitizer_stacktrace.cpp @@ -6,12 +6,11 @@ * See LICENSE.TXT * SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception * - * @file stacktrace.cpp + * @file sanitizer_stacktrace.cpp * */ -#include "stacktrace.hpp" -#include "asan_interceptor.hpp" +#include "sanitizer_stacktrace.hpp" #include "ur_sanitizer_layer.hpp" extern "C" { @@ -93,10 +92,9 @@ void StackTrace::print() const { BacktraceInfo BI = BacktraceSymbols[i]; // Skip runtime modules - if (!getContext()->interceptor->getOptions().Debug && - (Contains(BI, "libsycl.so") || Contains(BI, "libur_loader.so") || - Contains(BI, "libomptarget.rtl.unified_runtime.so") || - Contains(BI, "libomptarget.so"))) { + if (Contains(BI, "libsycl.so") || Contains(BI, "libur_loader.so") || + Contains(BI, "libomptarget.rtl.unified_runtime.so") || + Contains(BI, "libomptarget.so")) { continue; } diff --git a/source/loader/layers/sanitizer/stacktrace.hpp b/source/loader/layers/sanitizer/sanitizer_common/sanitizer_stacktrace.hpp similarity index 89% rename from source/loader/layers/sanitizer/stacktrace.hpp rename to source/loader/layers/sanitizer/sanitizer_common/sanitizer_stacktrace.hpp index 57811bba01..41443ee78d 100644 --- a/source/loader/layers/sanitizer/stacktrace.hpp +++ b/source/loader/layers/sanitizer/sanitizer_common/sanitizer_stacktrace.hpp @@ -6,13 +6,13 @@ * See LICENSE.TXT * SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception * - * @file stacktrace.hpp + * @file sanitizer_stacktrace.hpp * */ #pragma once -#include "common.hpp" +#include "sanitizer_common.hpp" #include diff --git a/source/loader/layers/sanitizer/ur_sanitizer_utils.cpp b/source/loader/layers/sanitizer/sanitizer_common/sanitizer_utils.cpp similarity index 98% rename from source/loader/layers/sanitizer/ur_sanitizer_utils.cpp rename to source/loader/layers/sanitizer/sanitizer_common/sanitizer_utils.cpp index 53e4326ed4..900eae405b 100644 --- a/source/loader/layers/sanitizer/ur_sanitizer_utils.cpp +++ b/source/loader/layers/sanitizer/sanitizer_common/sanitizer_utils.cpp @@ -6,11 +6,12 @@ * See LICENSE.TXT * SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception * - * @file ur_sanitizer_utils.cpp + * @file sanitizer_utils.cpp * */ -#include "ur_sanitizer_utils.hpp" +#include "sanitizer_utils.hpp" +#include "sanitizer_common/sanitizer_common.hpp" #include "ur_sanitizer_layer.hpp" namespace ur_sanitizer_layer { diff --git a/source/loader/layers/sanitizer/ur_sanitizer_utils.hpp b/source/loader/layers/sanitizer/sanitizer_common/sanitizer_utils.hpp similarity index 95% rename from source/loader/layers/sanitizer/ur_sanitizer_utils.hpp rename to source/loader/layers/sanitizer/sanitizer_common/sanitizer_utils.hpp index a04886e5e5..6fcb05894e 100644 --- a/source/loader/layers/sanitizer/ur_sanitizer_utils.hpp +++ b/source/loader/layers/sanitizer/sanitizer_common/sanitizer_utils.hpp @@ -6,13 +6,17 @@ * See LICENSE.TXT * SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception * - * @file ur_sanitizer_utils.hpp + * @file sanitizer_utils.hpp * */ #pragma once -#include "common.hpp" +#include "sanitizer_libdevice.hpp" +#include "ur_api.h" + +#include +#include namespace ur_sanitizer_layer { diff --git a/source/loader/layers/sanitizer/ur_sanitizer_layer.cpp b/source/loader/layers/sanitizer/ur_sanitizer_layer.cpp index b94235cdf0..d1e00c640c 100644 --- a/source/loader/layers/sanitizer/ur_sanitizer_layer.cpp +++ b/source/loader/layers/sanitizer/ur_sanitizer_layer.cpp @@ -1,6 +1,6 @@ /* * - * Copyright (C) 2023 Intel Corporation + * Copyright (C) 2024 Intel Corporation * * Part of the Unified-Runtime Project, under the Apache License v2.0 with LLVM Exceptions. * See LICENSE.TXT @@ -11,7 +11,7 @@ */ #include "ur_sanitizer_layer.hpp" -#include "asan_interceptor.hpp" +#include "asan/asan_ddi.hpp" namespace ur_sanitizer_layer { context_t *getContext() { return context_t::get_direct(); } @@ -21,7 +21,17 @@ context_t::context_t() : logger(logger::create_logger("sanitizer", false, false, logger::Level::WARN)) {} -ur_result_t context_t::tearDown() { return UR_RESULT_SUCCESS; } +ur_result_t context_t::tearDown() { + switch (enabledType) { + case SanitizerType::AddressSanitizer: + destroyAsanInterceptor(); + break; + default: + break; + } + + return UR_RESULT_SUCCESS; +} /////////////////////////////////////////////////////////////////////////////// context_t::~context_t() {} diff --git a/source/loader/layers/sanitizer/ur_sanitizer_layer.hpp b/source/loader/layers/sanitizer/ur_sanitizer_layer.hpp index e7f704f8a8..55291db82a 100644 --- a/source/loader/layers/sanitizer/ur_sanitizer_layer.hpp +++ b/source/loader/layers/sanitizer/ur_sanitizer_layer.hpp @@ -1,6 +1,6 @@ /* * - * Copyright (C) 2023 Corporation + * Copyright (C) 2024 Intel Corporation * * Part of the Unified-Runtime Project, under the Apache License v2.0 with LLVM Exceptions. * See LICENSE.TXT @@ -13,15 +13,12 @@ #pragma once #include "logger/ur_logger.hpp" -#include "ur/ur.hpp" #include "ur_proxy_layer.hpp" #define SANITIZER_COMP_NAME "sanitizer layer" namespace ur_sanitizer_layer { -class SanitizerInterceptor; - enum class SanitizerType { None, AddressSanitizer, @@ -35,7 +32,6 @@ class __urdlllocal context_t : public proxy_layer_context_t, public: ur_dditable_t urDdiTable = {}; logger::Logger logger; - std::unique_ptr interceptor; SanitizerType enabledType = SanitizerType::None; context_t(); @@ -52,4 +48,5 @@ class __urdlllocal context_t : public proxy_layer_context_t, }; context_t *getContext(); + } // namespace ur_sanitizer_layer