-
Notifications
You must be signed in to change notification settings - Fork 125
[DeviceSanitizer] Support detecting out-of-bounds errors on sycl::buffer #1533
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
Merged
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
110ac2c
[DeviceSanitizer] Support detecting out-of-bounds errors on sycl::buffer
zhaomaosu 5ce247e
Fix comments mismatch
zhaomaosu 2b11666
Merge branch 'main' into sanitizer-buffer
zhaomaosu f70f14c
Apply suggestions
zhaomaosu 7dcabee
Merge branch 'main' into sanitizer-buffer
zhaomaosu 251b3dc
Merge branch 'main' into sanitizer-buffer
zhaomaosu 1ba7d3c
Merge branch 'main' into sanitizer-buffer
zhaomaosu b944210
Merge branch 'main' into sanitizer-buffer
zhaomaosu File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,137 @@ | ||
/* | ||
* | ||
* 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_buffer.cpp | ||
* | ||
*/ | ||
|
||
#include "asan_buffer.hpp" | ||
#include "asan_interceptor.hpp" | ||
#include "ur_sanitizer_layer.hpp" | ||
#include "ur_sanitizer_utils.hpp" | ||
|
||
namespace ur_sanitizer_layer { | ||
|
||
ur_result_t EnqueueMemCopyRectHelper( | ||
ur_queue_handle_t Queue, char *pSrc, char *pDst, ur_rect_offset_t SrcOffset, | ||
ur_rect_offset_t DstOffset, ur_rect_region_t Region, size_t SrcRowPitch, | ||
size_t SrcSlicePitch, size_t DstRowPitch, size_t DstSlicePitch, | ||
bool Blocking, uint32_t NumEventsInWaitList, | ||
const ur_event_handle_t *EventWaitList, ur_event_handle_t *Event) { | ||
// If user doesn't determine src/dst row pitch and slice pitch, just use | ||
// region for it. | ||
if (SrcRowPitch == 0) { | ||
SrcRowPitch = Region.width; | ||
} | ||
|
||
if (SrcSlicePitch == 0) { | ||
SrcSlicePitch = SrcRowPitch * Region.height; | ||
} | ||
|
||
if (DstRowPitch == 0) { | ||
DstRowPitch = Region.width; | ||
} | ||
|
||
if (DstSlicePitch == 0) { | ||
DstSlicePitch = DstRowPitch * Region.height; | ||
} | ||
|
||
// Calculate the src and dst addresses that actually will be copied. | ||
char *SrcOrigin = pSrc + SrcOffset.x + SrcRowPitch * SrcOffset.y + | ||
SrcSlicePitch * SrcOffset.z; | ||
char *DstOrigin = pDst + DstOffset.x + DstRowPitch * DstOffset.y + | ||
DstSlicePitch * DstOffset.z; | ||
|
||
std::vector<ur_event_handle_t> Events; | ||
Events.reserve(Region.depth); | ||
// For now, USM doesn't support 3D memory copy operation, so we can only | ||
// loop call 2D memory copy function to implement it. | ||
for (size_t i = 0; i < Region.depth; i++) { | ||
ur_event_handle_t NewEvent{}; | ||
UR_CALL(context.urDdiTable.Enqueue.pfnUSMMemcpy2D( | ||
Queue, Blocking, DstOrigin + (i * DstSlicePitch), DstRowPitch, | ||
SrcOrigin + (i * SrcSlicePitch), SrcRowPitch, Region.width, | ||
Region.height, NumEventsInWaitList, EventWaitList, &NewEvent)); | ||
|
||
Events.push_back(NewEvent); | ||
} | ||
|
||
UR_CALL(context.urDdiTable.Enqueue.pfnEventsWait(Queue, Events.size(), | ||
Events.data(), Event)); | ||
|
||
return UR_RESULT_SUCCESS; | ||
} | ||
|
||
ur_result_t MemBuffer::getHandle(ur_device_handle_t Device, char *&Handle) { | ||
// Sub-buffers don't maintain own allocations but rely on parent buffer. | ||
if (SubBuffer) { | ||
UR_CALL(SubBuffer->Parent->getHandle(Device, Handle)); | ||
Handle += SubBuffer->Origin; | ||
return UR_RESULT_SUCCESS; | ||
} | ||
|
||
auto &Allocation = Allocations[Device]; | ||
if (!Allocation) { | ||
ur_usm_desc_t USMDesc{}; | ||
USMDesc.align = getAlignment(); | ||
ur_usm_pool_handle_t Pool{}; | ||
ur_result_t URes = context.interceptor->allocateMemory( | ||
Context, Device, &USMDesc, Pool, Size, AllocType::MEM_BUFFER, | ||
ur_cast<void **>(&Allocation)); | ||
if (URes != UR_RESULT_SUCCESS) { | ||
context.logger.error( | ||
"Failed to allocate {} bytes memory for buffer {}", Size, this); | ||
return URes; | ||
} | ||
|
||
if (HostPtr) { | ||
ManagedQueue Queue(Context, Device); | ||
URes = context.urDdiTable.Enqueue.pfnUSMMemcpy( | ||
Queue, true, Allocation, HostPtr, Size, 0, nullptr, nullptr); | ||
if (URes != UR_RESULT_SUCCESS) { | ||
context.logger.error("Failed to copy {} bytes data from host " | ||
"pointer {} to buffer {}", | ||
Size, HostPtr, this); | ||
return URes; | ||
} | ||
} | ||
} | ||
|
||
Handle = Allocation; | ||
|
||
return UR_RESULT_SUCCESS; | ||
} | ||
|
||
ur_result_t MemBuffer::free() { | ||
for (const auto &[_, Ptr] : Allocations) { | ||
ur_result_t URes = context.interceptor->releaseMemory(Context, Ptr); | ||
if (URes != UR_RESULT_SUCCESS) { | ||
context.logger.error("Failed to free buffer handle {}", Ptr); | ||
return URes; | ||
} | ||
} | ||
Allocations.clear(); | ||
return UR_RESULT_SUCCESS; | ||
} | ||
|
||
size_t MemBuffer::getAlignment() { | ||
// Choose an alignment that is at most 128 and is the next power of 2 | ||
// for sizes less than 128. | ||
// TODO: If we don't set the alignment size explicitly, the device will | ||
// usually choose a very large size (more than 1k). Then sanitizer will | ||
// allocate extra unnessary memory. Not sure if this will impact | ||
// performance. | ||
size_t MsbIdx = 63 - __builtin_clz(Size); | ||
size_t Alignment = (1 << (MsbIdx + 1)); | ||
if (Alignment > 128) { | ||
Alignment = 128; | ||
} | ||
return Alignment; | ||
} | ||
|
||
} // namespace ur_sanitizer_layer |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
/* | ||
* | ||
* 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_buffer.hpp | ||
* | ||
*/ | ||
|
||
#pragma once | ||
|
||
#include <atomic> | ||
#include <memory> | ||
#include <optional> | ||
|
||
#include "common.hpp" | ||
|
||
namespace ur_sanitizer_layer { | ||
|
||
struct MemBuffer { | ||
// Buffer constructor | ||
MemBuffer(ur_context_handle_t Context, size_t Size, char *HostPtr) | ||
: Context(Context), Size(Size), HostPtr(HostPtr) {} | ||
|
||
// Sub-buffer constructor | ||
MemBuffer(std::shared_ptr<MemBuffer> Parent, size_t Origin, size_t Size) | ||
: Context(Parent->Context), Size(Size), SubBuffer{{Parent, Origin}} {} | ||
|
||
ur_result_t getHandle(ur_device_handle_t Device, char *&Handle); | ||
|
||
ur_result_t free(); | ||
|
||
size_t getAlignment(); | ||
|
||
std::unordered_map<ur_device_handle_t, char *> Allocations; | ||
|
||
enum AccessMode { UNKNOWN, READ_WRITE, READ_ONLY, WRITE_ONLY }; | ||
|
||
struct Mapping { | ||
size_t Offset; | ||
size_t Size; | ||
}; | ||
|
||
std::unordered_map<void *, Mapping> Mappings; | ||
|
||
ur_context_handle_t Context; | ||
|
||
size_t Size; | ||
|
||
char *HostPtr{}; | ||
|
||
struct SubBuffer_t { | ||
std::shared_ptr<MemBuffer> Parent; | ||
size_t Origin; | ||
}; | ||
|
||
std::optional<SubBuffer_t> SubBuffer; | ||
|
||
std::atomic<int32_t> RefCount = 1; | ||
|
||
ur_shared_mutex Mutex; | ||
}; | ||
|
||
ur_result_t EnqueueMemCopyRectHelper( | ||
ur_queue_handle_t Queue, char *pSrc, char *pDst, ur_rect_offset_t SrcOffset, | ||
ur_rect_offset_t DstOffset, ur_rect_region_t Region, size_t SrcRowPitch, | ||
size_t SrcSlicePitch, size_t DstRowPitch, size_t DstSlicePitch, | ||
bool Blocking, uint32_t NumEventsInWaitList, | ||
const ur_event_handle_t *EventWaitList, ur_event_handle_t *Event); | ||
|
||
} // namespace ur_sanitizer_layer |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.