Skip to content
Merged
4 changes: 4 additions & 0 deletions source/loader/layers/sanitizer/asan_interceptor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,10 @@ SanitizerInterceptor::SanitizerInterceptor(logger::Logger &logger)
SanitizerInterceptor::~SanitizerInterceptor() {
DestroyShadowMemoryOnCPU();
DestroyShadowMemoryOnPVC();

for (auto Adapter : m_Adapters) {
getContext()->urDdiTable.Global.pfnAdapterRelease(Adapter);
}
}

/// The memory chunk allocated from the underlying allocator looks like this:
Expand Down
8 changes: 8 additions & 0 deletions source/loader/layers/sanitizer/asan_interceptor.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,12 @@ class SanitizerInterceptor {
ur_result_t eraseMemBuffer(ur_mem_handle_t MemHandle);
std::shared_ptr<MemBuffer> getMemBuffer(ur_mem_handle_t MemHandle);

ur_result_t holdAdapter(ur_adapter_handle_t Adapter) {
UR_CALL(getContext()->urDdiTable.Global.pfnAdapterRetain(Adapter));
m_Adapters.push_back(Adapter);
return UR_RESULT_SUCCESS;
}

std::optional<AllocationIterator> findAllocInfoByAddress(uptr Address);

std::shared_ptr<ContextInfo> getContextInfo(ur_context_handle_t Context) {
Expand Down Expand Up @@ -262,6 +268,8 @@ class SanitizerInterceptor {

std::unique_ptr<Quarantine> m_Quarantine;
logger::Logger &logger;

std::vector<ur_adapter_handle_t> m_Adapters;
};

} // namespace ur_sanitizer_layer
69 changes: 69 additions & 0 deletions source/loader/layers/sanitizer/ur_sanddi.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,38 @@ ur_result_t setupContext(ur_context_handle_t Context, uint32_t numDevices,

} // namespace

///////////////////////////////////////////////////////////////////////////////
/// @brief Intercept function for urAdapterGet
__urdlllocal ur_result_t UR_APICALL urAdapterGet(
uint32_t
NumEntries, ///< [in] the number of adapters to be added to phAdapters.
///< If phAdapters is not NULL, then NumEntries should be greater than
///< zero, otherwise ::UR_RESULT_ERROR_INVALID_SIZE,
///< will be returned.
ur_adapter_handle_t *
phAdapters, ///< [out][optional][range(0, NumEntries)] array of handle of adapters.
///< If NumEntries is less than the number of adapters available, then
///< ::urAdapterGet shall only retrieve that number of platforms.
uint32_t *
pNumAdapters ///< [out][optional] returns the total number of adapters available.
) {
auto pfnAdapterGet = getContext()->urDdiTable.Global.pfnAdapterGet;

if (nullptr == pfnAdapterGet) {
return UR_RESULT_ERROR_UNSUPPORTED_FEATURE;
}

ur_result_t result = pfnAdapterGet(NumEntries, phAdapters, pNumAdapters);
if (result == UR_RESULT_SUCCESS) {
const uint32_t NumAdapters = pNumAdapters ? *pNumAdapters : NumEntries;
for (uint32_t i = 0; i < NumAdapters; ++i) {
UR_CALL(getContext()->interceptor->holdAdapter(phAdapters[i]));

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If urAdapterGet() get called multiple times, then we would have duplicated handles.

@AllanZyne AllanZyne Aug 2, 2024

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maybe we needn't hold the handle of adapter, it seems ur loader has already hold this.
I'll investigate this.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

every time you call urAdapterGet, an internal reference count is incremented and you need to decrement it using urAdapterRelease.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed

}
}

return result;
}

///////////////////////////////////////////////////////////////////////////////
/// @brief Intercept function for urUSMHostAlloc
__urdlllocal ur_result_t UR_APICALL urUSMHostAlloc(
Expand Down Expand Up @@ -1274,6 +1306,38 @@ __urdlllocal ur_result_t UR_APICALL urKernelSetArgLocal(
return result;
}

///////////////////////////////////////////////////////////////////////////////
/// @brief Exported function for filling application's Global table
/// with current process' addresses
///
/// @returns
/// - ::UR_RESULT_SUCCESS
/// - ::UR_RESULT_ERROR_INVALID_NULL_POINTER
/// - ::UR_RESULT_ERROR_UNSUPPORTED_VERSION
__urdlllocal ur_result_t UR_APICALL urGetGlobalProcAddrTable(
ur_api_version_t version, ///< [in] API version requested
ur_global_dditable_t
*pDdiTable ///< [in,out] pointer to table of DDI function pointers
) {
auto &dditable = ur_sanitizer_layer::getContext()->urDdiTable.Global;

if (nullptr == pDdiTable) {
return UR_RESULT_ERROR_INVALID_NULL_POINTER;
}

if (UR_MAJOR_VERSION(ur_sanitizer_layer::getContext()->version) !=
UR_MAJOR_VERSION(version) ||
UR_MINOR_VERSION(ur_sanitizer_layer::getContext()->version) >
UR_MINOR_VERSION(version)) {
return UR_RESULT_ERROR_UNSUPPORTED_VERSION;
}

ur_result_t result = UR_RESULT_SUCCESS;

pDdiTable->pfnAdapterGet = ur_sanitizer_layer::urAdapterGet;

return result;
}
///////////////////////////////////////////////////////////////////////////////
Comment thread
aarongreig marked this conversation as resolved.
/// @brief Exported function for filling application's Context table
/// with current process' addresses
Expand Down Expand Up @@ -1545,6 +1609,11 @@ ur_result_t context_t::init(ur_dditable_t *dditable,

urDdiTable = *dditable;

if (UR_RESULT_SUCCESS == result) {
result = ur_sanitizer_layer::urGetGlobalProcAddrTable(
UR_API_VERSION_CURRENT, &dditable->Global);
}

if (UR_RESULT_SUCCESS == result) {
result = ur_sanitizer_layer::urGetContextProcAddrTable(
UR_API_VERSION_CURRENT, &dditable->Context);
Expand Down