Skip to content

Commit fa6bc3d

Browse files
authored
[DevASAN] Move quarantine into context info for better management (#19602)
Before this change, quarantine is maintained in AsanInterceptor, this will cause sometimes we free a USM pointer with wrong context.
1 parent 46296dc commit fa6bc3d

File tree

2 files changed

+11
-13
lines changed

2 files changed

+11
-13
lines changed

unified-runtime/source/loader/layers/sanitizer/asan/asan_interceptor.cpp

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -25,12 +25,7 @@
2525
namespace ur_sanitizer_layer {
2626
namespace asan {
2727

28-
AsanInterceptor::AsanInterceptor() {
29-
if (getContext()->Options.MaxQuarantineSizeMB) {
30-
m_Quarantine = std::make_unique<Quarantine>(
31-
getContext()->Options.MaxQuarantineSizeMB * 1024 * 1024);
32-
}
33-
}
28+
AsanInterceptor::AsanInterceptor() {}
3429

3530
AsanInterceptor::~AsanInterceptor() {
3631
// We must release these objects before releasing adapters, since
@@ -39,7 +34,6 @@ AsanInterceptor::~AsanInterceptor() {
3934
DeviceInfo->Shadow = nullptr;
4035
}
4136

42-
m_Quarantine = nullptr;
4337
m_MemBufferMap.clear();
4438
m_KernelMap.clear();
4539
m_ContextMap.clear();
@@ -224,7 +218,7 @@ ur_result_t AsanInterceptor::releaseMemory(ur_context_handle_t Context,
224218
}
225219

226220
// If quarantine is disabled, USM is freed immediately
227-
if (!m_Quarantine) {
221+
if (!ContextInfo->m_Quarantine) {
228222
UR_LOG_L(getContext()->logger, DEBUG, "Free: {}",
229223
(void *)AllocInfo->AllocBegin);
230224

@@ -239,7 +233,8 @@ ur_result_t AsanInterceptor::releaseMemory(ur_context_handle_t Context,
239233
}
240234

241235
// If quarantine is enabled, cache it
242-
auto ReleaseList = m_Quarantine->put(AllocInfo->Device, AllocInfoIt);
236+
auto ReleaseList =
237+
ContextInfo->m_Quarantine->put(AllocInfo->Device, AllocInfoIt);
243238
if (ReleaseList.size()) {
244239
std::scoped_lock<ur_shared_mutex> Guard(m_AllocationMapMutex);
245240
for (auto &It : ReleaseList) {

unified-runtime/source/loader/layers/sanitizer/asan/asan_interceptor.hpp

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
#include "asan_allocator.hpp"
1717
#include "asan_buffer.hpp"
1818
#include "asan_libdevice.hpp"
19+
#include "asan_quarantine.hpp"
1920
#include "asan_shadow.hpp"
2021
#include "asan_statistics.hpp"
2122
#include "sanitizer_common/sanitizer_common.hpp"
@@ -33,8 +34,6 @@
3334
namespace ur_sanitizer_layer {
3435
namespace asan {
3536

36-
class Quarantine;
37-
3837
struct AllocInfoList {
3938
std::vector<std::shared_ptr<AllocInfo>> List;
4039
ur_shared_mutex Mutex;
@@ -148,12 +147,18 @@ struct ContextInfo {
148147
std::unordered_map<ur_device_handle_t, std::optional<ManagedQueue>>
149148
InternalQueueMap;
150149

150+
std::optional<Quarantine> m_Quarantine;
151+
151152
AsanStatsWrapper Stats;
152153

153154
explicit ContextInfo(ur_context_handle_t Context) : Handle(Context) {
154155
[[maybe_unused]] auto Result =
155156
getContext()->urDdiTable.Context.pfnRetain(Context);
156157
assert(Result == UR_RESULT_SUCCESS);
158+
if (getContext()->Options.MaxQuarantineSizeMB) {
159+
m_Quarantine.emplace(getContext()->Options.MaxQuarantineSizeMB * 1024 *
160+
1024);
161+
}
157162
}
158163

159164
~ContextInfo();
@@ -404,8 +409,6 @@ class AsanInterceptor {
404409
AllocationMap m_AllocationMap;
405410
ur_shared_mutex m_AllocationMapMutex;
406411

407-
std::unique_ptr<Quarantine> m_Quarantine;
408-
409412
std::unordered_set<ur_adapter_handle_t> m_Adapters;
410413
ur_shared_mutex m_AdaptersMutex;
411414

0 commit comments

Comments
 (0)