Skip to content

[SYCL] Avoid strict aliasing violation when reading from byte arrays #5537

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

Conversation

steffenlarsen
Copy link
Contributor

When reading properties produced by sycl-post-link, the runtime will cast a byte-array pointer to another pointer of a diffrent type and read from the resulting pointer. This is a violation of the strict aliasing rule and as such is UB. These changes replace the violating reads with memcpys from the byte arrays.

Note: This is a code-quality change and should have no effect on current stability. Thank you @psamolysov-intel for pointing out the UB.

When reading properties produced by sycl-post-link, the runtime will
cast a byte-array pointer to another pointer of a diffrent type and
read from the resulting pointer. This is a violation of the strict
aliasing rule and as such is UB. These changes replace the violating
reads with memcpys from the byte arrays.

Signed-off-by: Steffen Larsen <steffen.larsen@intel.com>
@steffenlarsen steffenlarsen requested a review from a team as a code owner February 10, 2022 13:40
@steffenlarsen steffenlarsen requested review from alexbatashev and a user February 10, 2022 13:40
ghost
ghost previously approved these changes Feb 10, 2022
Copy link

@ghost ghost left a comment

Choose a reason for hiding this comment

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

Thank you @steffenlarsen Good job! LGTM.

auto *It = reinterpret_cast<const std::uint32_t *>(&Descriptors[8]);
auto *End = reinterpret_cast<const std::uint32_t *>(&Descriptors[0] +
Descriptors.size());
const uint8_t *It = &Descriptors[8];
Copy link
Contributor

@cperkinsintel cperkinsintel Feb 10, 2022

Choose a reason for hiding this comment

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

DeviceBinaryProperty has a .asUint32() method. Couldn't we call that above, instead of .asByteArray() and then avoid the reinterpret cast on that front? (A: no. that's for a single uint32, not an array like the other )

Alternately, instead of inserting std::memcpy down below, I wonder if we could use sycl::bit_cast ?

I know the reinterpret cast for pointers means we potentially have UB, but given that we are getting raw binary data from the PI, are we really avoiding any issues by switching from reinterpet_cast to memcpy into local vars?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Alternately, instead of inserting std::memcpy down below, I wonder if we could use sycl::bit_cast ?

I believe sycl::bit_cast asserts that To and From have the same size, so we can't use it here sadly.

I know the reinterpret cast for pointers means we potentially have UB, but given that we are getting raw binary data from the PI, are we really avoiding any issues by switching from reinterpet_cast to memcpy into local vars?

Currently there isn't must difference between the two, except we assume that the data sycl-post-link generates has the same structure as the types we use. If this starts failing it is on us. However, relying on UB means we are at the compiler's mercy and any day it could change. Do I think it's likely to happen? No, but it would likely be a huge headache if it ever does and making this change seems like a small price to avoid future pains. Better safe than sorry!

@bader bader requested review from cperkinsintel and removed request for alexbatashev October 7, 2022 11:23
Signed-off-by: Larsen, Steffen <steffen.larsen@intel.com>
@steffenlarsen steffenlarsen marked this pull request as draft October 11, 2022 16:57
@steffenlarsen
Copy link
Contributor Author

Per offline discussion with @aelovikov-intel this is being moved to draft to make further improvements.

@aelovikov-intel
Copy link
Contributor

Per offline discussion with @aelovikov-intel this is being moved to draft to make further improvements.

An alternative approach: #7023.

@steffenlarsen
Copy link
Contributor Author

#7023 has been merged. This is no longer needed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants