-
Notifications
You must be signed in to change notification settings - Fork 405
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
Fix undefined behavior in is_zero_byte #7014
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Did the UB or address sanitizer report anything?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I am not a big fan of the header include comments. I find them confusing as usually we will comment what from that header include we are using and here you reversed the dependency. I would prefer if you drop both comments.
Would you remind me if we had a good reason to use that common type rather than just an array of unsigned char or whatnot? |
We just thought it was faster to compare bigger chunks of memory but the function is not really performance-sensitive in its current use. |
I think if we had done |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Blocking for a second: would replacing sizeof with alignof also fix the problem?
I am trying to figure out why this was segfaulting, and now it wouldn't. The only thing I could think of is that you check is_zero_byte
for something like a pair<int,int>
which is not 8-byte aligned. When simply recasting the reference to long long could result in a an unaligned memory access for an 8 byte value, while bit casting to Kokkos::Array<> is ok because it returns a copy?
No, the two are the same for these types.
I mean bit casting just avoids undefined behavior when reinterpreting the memory pointed to. It's not clear to me what instruction the compiler generates that leads to a segfault, though. For reference, the backtrace look somewhat like
|
sizeof and alignof are not the same for types such as |
@dalg24 does this need a bug fix changelog entry for 4.4? It looks like the issue was not newly introduced to develop after the most recent release? |
It was a longstanding issue. I suppose we should have an entry we got rid to UB in the logic to decide whether to do a zero-memset instead of launching a kernel at view initialization and in deep_copy with a scalar argument as the source (fill view with given value). |
would fail with
icpx
and-fp-model=precise -O2 -g -DNDEBUG -fiopenmp
due to the type punning approach used inis_zero_byte
. This also manifests in the test suite withopenmp.numeric_traits_infinity
segfaulting.Using
bitcast
(which requires using Kokkos::Array in turn) instead fixes the issue. An alternative is to always usechar
as a comparison type but I went for minimal modifications.