Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[UR][Layer] Add Sanitizer Layer #1074

Merged
merged 78 commits into from
Jan 17, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
78 commits
Select commit Hold shift + click to select a range
4c9fe03
Add UR Sanitizer Layer
AllanZyne Nov 1, 2023
d6e589b
UR Sanitizer: Add shadow memory alloc & memset
AllanZyne Nov 1, 2023
67fcd5e
[UR][CUDA][L0][HIP] Add virtual memory adapter implementations
steffenlarsen Oct 9, 2023
1096b0f
Remove duplicate CUDA UR_DEVICE_INFO_VIRTUAL_MEMORY_SUPPORT case
steffenlarsen Oct 9, 2023
c36bd72
Fix return of check error
steffenlarsen Oct 9, 2023
6ba94ac
Fix common include in cuda device.hpp
steffenlarsen Oct 10, 2023
9ae8a99
Zero-initialize CUmemAccessDesc
steffenlarsen Oct 11, 2023
9f90e43
Add missing newline
steffenlarsen Oct 11, 2023
336ce89
Fix wrongful use of UR_ASSERT in cuda implementation
steffenlarsen Oct 13, 2023
84a3afa
an -> a
steffenlarsen Oct 13, 2023
a8dadc1
UR Sanitizer: fix bug
AllanZyne Nov 2, 2023
4d63350
UR Sanitizer: Add membuffer
AllanZyne Nov 3, 2023
25b1183
UR Sanitizer: Add membuffer red zone marker
AllanZyne Nov 3, 2023
e6bc306
UR Sanitizer: add membuffer check
AllanZyne Nov 6, 2023
23fb1b3
UR log
AllanZyne Nov 9, 2023
cb8a761
UR change directory
AllanZyne Nov 13, 2023
5c44b1d
UR refactor code
AllanZyne Nov 13, 2023
a377c94
UR refactor code
AllanZyne Nov 13, 2023
96eea7a
UR: WIP partition membuffer
AllanZyne Nov 14, 2023
0a72ea9
UR: clean code
AllanZyne Nov 14, 2023
8fa86dd
UR: clean code
AllanZyne Nov 14, 2023
d9a51a7
Revert "an -> a"
AllanZyne Nov 14, 2023
730bf45
Revert "Fix wrongful use of UR_ASSERT in cuda implementation"
AllanZyne Nov 14, 2023
fed541a
UR: refactor
AllanZyne Nov 14, 2023
ad2b1a7
Revert "Add missing newline"
AllanZyne Nov 14, 2023
dec5f32
Revert "Zero-initialize CUmemAccessDesc"
AllanZyne Nov 14, 2023
ca58886
Revert "Fix common include in cuda device.hpp"
AllanZyne Nov 14, 2023
11cca13
Revert "Fix return of check error"
AllanZyne Nov 14, 2023
3e6194e
Revert "Remove duplicate CUDA UR_DEVICE_INFO_VIRTUAL_MEMORY_SUPPORT c…
AllanZyne Nov 14, 2023
89febf0
Revert "[UR][CUDA][L0][HIP] Add virtual memory adapter implementations"
AllanZyne Nov 14, 2023
1b81df9
UR: revert gitignore
AllanZyne Nov 14, 2023
030400d
UR: cmake option
AllanZyne Nov 14, 2023
7208518
UR: clean code
AllanZyne Nov 14, 2023
668a11e
UR: clean code
AllanZyne Nov 14, 2023
f623135
UR: clean code
AllanZyne Nov 14, 2023
7be8667
UR: clean code
AllanZyne Nov 14, 2023
e4a47ac
UR: clean code
AllanZyne Nov 14, 2023
61c0ca9
UR: llvm license
AllanZyne Nov 14, 2023
0ad1bb1
UR: llvm license
AllanZyne Nov 14, 2023
cc4402a
UR: once
AllanZyne Nov 14, 2023
bf358c1
UR: clean code
AllanZyne Nov 14, 2023
d5e7946
UR: clean includes
AllanZyne Nov 14, 2023
50666b7
UR: clean comment
AllanZyne Nov 14, 2023
de03096
UR: rename files
AllanZyne Nov 14, 2023
b47ca29
UR: update readme
AllanZyne Nov 14, 2023
1e2b6a0
UR: fix according to PR comments
AllanZyne Nov 15, 2023
42f6755
UR: UR_ENABLE_SANITIZER=ON default
AllanZyne Nov 15, 2023
5aaba09
ignore VS clangd files
AllanZyne Nov 21, 2023
8dc90fd
fix comments
AllanZyne Nov 21, 2023
3b819f6
resolve comments
AllanZyne Nov 21, 2023
54220e9
resolve comments
AllanZyne Nov 21, 2023
2328b48
Merge branch 'main' into sanitizer-pr
AllanZyne Dec 8, 2023
e59d7c7
fix build errors
AllanZyne Dec 8, 2023
bba6f82
add basic test
AllanZyne Dec 8, 2023
a084650
fix warning & remove tests
AllanZyne Dec 9, 2023
1148ccb
fix warning
AllanZyne Dec 10, 2023
fa92f72
Merge branch 'main' into sanitizer-pr
AllanZyne Dec 10, 2023
524a83d
fix warning
AllanZyne Dec 10, 2023
e9a2093
fix fprintf
AllanZyne Dec 12, 2023
a1c4ddc
add some comments
AllanZyne Dec 12, 2023
71d8657
Merge branch 'main' into sanitizer-pr
AllanZyne Dec 13, 2023
40b5ef6
fix logger
AllanZyne Dec 19, 2023
0b00798
improve logger
AllanZyne Dec 19, 2023
b29930b
Merge branch 'main' into sanitizer-pr
AllanZyne Dec 19, 2023
c42bd9a
don't print prefix when logger::Level::QUIET
AllanZyne Dec 21, 2023
bc8cdb2
small fix
AllanZyne Dec 21, 2023
3bede09
small fix
AllanZyne Dec 21, 2023
5dc2d5c
fix KernelNameBuf
AllanZyne Dec 21, 2023
30c0bc1
follow the review comments
AllanZyne Jan 2, 2024
86d4056
fix build
AllanZyne Jan 2, 2024
49e2f4f
follow the review comments
AllanZyne Jan 2, 2024
79998b9
follow the review comments
AllanZyne Jan 2, 2024
6cc4913
fix urEnqueueKernelLaunch & remove host deviceinfo
AllanZyne Jan 4, 2024
577ddd3
fix build
AllanZyne Jan 5, 2024
4ee0e10
Merge branch 'main' into sanitizer-pr
AllanZyne Jan 5, 2024
dc127d5
fix build
AllanZyne Jan 9, 2024
b59d931
Merge branch 'main' into sanitizer-pr
AllanZyne Jan 16, 2024
28315db
fix build error
AllanZyne Jan 17, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 18 additions & 34 deletions source/loader/layers/sanitizer/asan_interceptor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -88,11 +88,14 @@ ur_result_t SanitizerInterceptor::allocateMemory(
assert(Alignment == 0 || IsPowerOfTwo(Alignment));

auto ContextInfo = getContextInfo(Context);
// Device is nullptr if Type == USMMemoryType::HOST
auto DeviceInfo = ContextInfo->getDeviceInfo(Device);
std::shared_ptr<DeviceInfo> DeviceInfo;
if (Device) {
DeviceInfo = ContextInfo->getDeviceInfo(Device);
Copy link
Contributor

@wenju-he wenju-he Jan 4, 2024

Choose a reason for hiding this comment

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

nit: brace not needed for single line code according to llvm coding style. What's the coding style using in UR? Please also see other changes in this commit.

Is the 4-space indent aligning with UR coding style?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

What's the coding style using in UR? Please also see other changes in this commit.

I searched some of UR codes, they add braces for single line code as well.

Is the 4-space indent aligning with UR coding style?

Yes.

}

if (Alignment == 0) {
Alignment = DeviceInfo->Alignment;
Alignment =
DeviceInfo ? DeviceInfo->Alignment : ASAN_SHADOW_GRANULARITY;
}

// Copy from LLVM compiler-rt/lib/asan
Expand Down Expand Up @@ -133,9 +136,15 @@ ur_result_t SanitizerInterceptor::allocateMemory(
USMAllocInfo{AllocBegin, UserBegin, UserEnd, NeededSize, Type});

// For updating shadow memory
{
if (DeviceInfo) { // device/shared USM
std::scoped_lock<ur_shared_mutex> Guard(DeviceInfo->Mutex);
DeviceInfo->AllocInfos.emplace_back(AllocInfo);
Copy link
Contributor

Choose a reason for hiding this comment

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

I don't think AllocInfos is ever removed from (apart from the clear on kernel launch). It's not impossible for software to do some sort of allocate/deallocate cycle prior to running any kernel, causing duplicate entries in AllocInfos. This might be especially problematic for host allocations where this is never cleared.

Copy link
Contributor Author

@AllanZyne AllanZyne Dec 10, 2023

Choose a reason for hiding this comment

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

Yes, thanks for pointing this issue.
As what I mentioned in the above comment, currently I haven't finished the releasing logic.
In fact, the releasing logic needs much more consideration, so I'll leave it in another patch.

} else { // host USM's AllocInfo needs to insert into all devices
for (auto &pair : ContextInfo->DeviceMap) {
auto DeviceInfo = pair.second;
std::scoped_lock<ur_shared_mutex> Guard(DeviceInfo->Mutex);
DeviceInfo->AllocInfos.emplace_back(AllocInfo);
}
}

// For memory release
Expand Down Expand Up @@ -185,9 +194,7 @@ ur_result_t SanitizerInterceptor::releaseMemory(ur_context_handle_t Context,

bool SanitizerInterceptor::preLaunchKernel(ur_kernel_handle_t Kernel,
ur_queue_handle_t Queue,
ur_event_handle_t *Event) {
assert(Event != nullptr);

ur_event_handle_t &Event) {
AllanZyne marked this conversation as resolved.
Show resolved Hide resolved
prepareLaunch(Queue, Kernel);

UR_CALL(updateShadowMemory(Queue));
Expand All @@ -198,15 +205,15 @@ bool SanitizerInterceptor::preLaunchKernel(ur_kernel_handle_t Kernel,
auto QueueInfo = ContextInfo->getQueueInfo(Queue);

std::scoped_lock<ur_mutex> Guard(QueueInfo->Mutex);
*Event = QueueInfo->LastEvent;
Event = QueueInfo->LastEvent;
QueueInfo->LastEvent = nullptr;

return true;
}

void SanitizerInterceptor::postLaunchKernel(ur_kernel_handle_t Kernel,
ur_queue_handle_t Queue,
ur_event_handle_t *Event) {
ur_event_handle_t &Event) {
auto Program = getProgram(Kernel);
ur_event_handle_t ReadEvent{};

Expand All @@ -215,16 +222,10 @@ void SanitizerInterceptor::postLaunchKernel(ur_kernel_handle_t Kernel,
auto Result = context.urDdiTable.Enqueue.pfnDeviceGlobalVariableRead(
Queue, Program, kSPIR_DeviceSanitizerReportMem, true,
sizeof(SPIR_DeviceSanitizerReportMem), 0,
&SPIR_DeviceSanitizerReportMem, Event ? 1 : 0, Event, &ReadEvent);
&SPIR_DeviceSanitizerReportMem, 1, &Event, &ReadEvent);

if (Result == UR_RESULT_SUCCESS) {
if (Event) {
*Event = ReadEvent;
} else {
[[maybe_unused]] auto Result =
context.urDdiTable.Event.pfnWait(1, &ReadEvent);
assert(Result == UR_RESULT_SUCCESS);
}
Event = ReadEvent;

auto AH = &SPIR_DeviceSanitizerReportMem;
if (!AH->Flag) {
Expand Down Expand Up @@ -476,12 +477,6 @@ ur_result_t SanitizerInterceptor::updateShadowMemory(ur_queue_handle_t Queue) {

ur_event_handle_t LastEvent = QueueInfo->LastEvent;

// FIXME: Always update host USM, but it'd be better to update host USM
// selectively, or each devices once
for (auto &AllocInfo : HostInfo->AllocInfos) {
UR_CALL(enqueueAllocInfo(Context, Device, Queue, AllocInfo, LastEvent));
}

for (auto &AllocInfo : DeviceInfo->AllocInfos) {
UR_CALL(enqueueAllocInfo(Context, Device, Queue, AllocInfo, LastEvent));
}
Expand All @@ -495,17 +490,6 @@ ur_result_t SanitizerInterceptor::updateShadowMemory(ur_queue_handle_t Queue) {
ur_result_t SanitizerInterceptor::insertContext(ur_context_handle_t Context) {
auto ContextInfo = std::make_shared<ur_sanitizer_layer::ContextInfo>();

// Host Device
auto DeviceInfo = std::make_shared<ur_sanitizer_layer::DeviceInfo>();
DeviceInfo->Type = DeviceType::CPU;
DeviceInfo->Alignment = ASAN_SHADOW_GRANULARITY;

// TODO: Check if host asan is enabled
DeviceInfo->ShadowOffset = 0;
DeviceInfo->ShadowOffsetEnd = 0;

ContextInfo->DeviceMap.emplace(nullptr, std::move(DeviceInfo));

std::scoped_lock<ur_shared_mutex> Guard(m_ContextMapMutex);
assert(m_ContextMap.find(Context) == m_ContextMap.end());
m_ContextMap.emplace(Context, std::move(ContextInfo));
AllanZyne marked this conversation as resolved.
Show resolved Hide resolved
Expand Down
5 changes: 2 additions & 3 deletions source/loader/layers/sanitizer/asan_interceptor.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,6 @@ struct ContextInfo {
}

ur_shared_mutex Mutex;
// Note: nullptr is host device
std::unordered_map<ur_device_handle_t, std::shared_ptr<DeviceInfo>>
DeviceMap;
std::unordered_map<ur_queue_handle_t, std::shared_ptr<QueueInfo>> QueueMap;
Expand All @@ -92,9 +91,9 @@ class SanitizerInterceptor {
ur_result_t releaseMemory(ur_context_handle_t Context, void *Ptr);

bool preLaunchKernel(ur_kernel_handle_t Kernel, ur_queue_handle_t Queue,
ur_event_handle_t *Event);
ur_event_handle_t &Event);
void postLaunchKernel(ur_kernel_handle_t Kernel, ur_queue_handle_t Queue,
ur_event_handle_t *Event);
ur_event_handle_t &Event);

ur_result_t insertContext(ur_context_handle_t Context);
ur_result_t eraseContext(ur_context_handle_t Context);
Expand Down
17 changes: 11 additions & 6 deletions source/loader/layers/sanitizer/ur_sanddi.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -181,24 +181,29 @@ __urdlllocal ur_result_t UR_APICALL urEnqueueKernelLaunch(
return UR_RESULT_ERROR_UNSUPPORTED_FEATURE;
}

ur_event_handle_t lk_event{};
ur_event_handle_t hPreEvent{};
std::vector<ur_event_handle_t> events(numEventsInWaitList + 1);
for (unsigned i = 0; i < numEventsInWaitList; ++i) {
events.push_back(phEventWaitList[i]);
}

// launchKernel must append to num_events_in_wait_list, not prepend
context.interceptor->preLaunchKernel(hKernel, hQueue, &lk_event);
if (lk_event) {
events.push_back(lk_event);
context.interceptor->preLaunchKernel(hKernel, hQueue, hPreEvent);
if (hPreEvent) {
events.push_back(hPreEvent);
}

ur_event_handle_t hEvent{};
ur_result_t result = pfnKernelLaunch(
hQueue, hKernel, workDim, pGlobalWorkOffset, pGlobalWorkSize,
pLocalWorkSize, numEventsInWaitList, phEventWaitList, phEvent);
pLocalWorkSize, numEventsInWaitList, phEventWaitList, &hEvent);

if (result == UR_RESULT_SUCCESS) {
context.interceptor->postLaunchKernel(hKernel, hQueue, phEvent);
context.interceptor->postLaunchKernel(hKernel, hQueue, hEvent);
}

if (phEvent) {
*phEvent = hEvent;
}

return result;
Expand Down
Loading