Skip to content

Commit ff5f9b8

Browse files
authored
[SYCL] Fix memory access inside deallocated region when using default accessor (#20632)
This PR addresses the root cause behind #20423. When an accessor that has not been constructed with a `handler` instance is passed to the `set_args` function of a `handler` such as a default accessor, the lifetime of its dynamically allocated resources can potentially be shorter than the lifetime of the `handler` itself which causes problems, for instance, in the `finalize` function of the `handler` where we may end up accessing freed memory.
1 parent 5cd2c2a commit ff5f9b8

File tree

2 files changed

+5
-3
lines changed

2 files changed

+5
-3
lines changed

sycl/include/sycl/handler.hpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -679,6 +679,8 @@ class __SYCL_EXPORT handler {
679679
accessor<DataT, Dims, AccessMode, AccessTarget, IsPlaceholder> &&Arg) {
680680
detail::AccessorBaseHost *AccBase = (detail::AccessorBaseHost *)&Arg;
681681
const detail::AccessorImplPtr &AccImpl = detail::getSyclObjImpl(*AccBase);
682+
// Ensure the data of AccImpl lives at least as long as the handler.
683+
addLifetimeSharedPtrStorage(AccImpl);
682684
detail::AccessorImplHost *Req = AccImpl.get();
683685
// Add accessor to the list of arguments.
684686
addArg(detail::kernel_param_kind_t::kind_accessor, Req,

sycl/test-e2e/FreeFunctionKernels/enum_parameter.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,26 +39,26 @@ int main() {
3939

4040
bool flag1, flag2, flag3;
4141
flag1 = (flag2 = (flag3 = false));
42-
rAccType acc1;
43-
wAccType acc2;
44-
rwAccType acc3;
4542
{
4643
sycl::buffer<bool, 1> flagBuffer1(&flag1, 1);
4744
sycl::buffer<bool, 1> flagBuffer2(&flag2, 1);
4845
sycl::buffer<bool, 1> flagBuffer3(&flag3, 1);
4946
Queue.submit([&](sycl::handler &Handler) {
47+
rAccType acc1;
5048
flagType flagAcc1{flagBuffer1, Handler};
5149
Handler.set_args(acc1, rMode, flagAcc1);
5250
Handler.single_task(Kernel1);
5351
});
5452

5553
Queue.submit([&](sycl::handler &Handler) {
54+
wAccType acc2;
5655
flagType flagAcc2{flagBuffer2, Handler};
5756
Handler.set_args(acc2, wMode, flagAcc2);
5857
Handler.single_task(Kernel2);
5958
});
6059

6160
Queue.submit([&](sycl::handler &Handler) {
61+
rwAccType acc3;
6262
flagType flagAcc3{flagBuffer3, Handler};
6363
Handler.set_args(acc3, rwMode, flagAcc3);
6464
Handler.single_task(Kernel3);

0 commit comments

Comments
 (0)