-
Notifications
You must be signed in to change notification settings - Fork 95
Strict aliasing rules violation #363
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
Comments
Another way could be an attempt to stop violating the strict aliasing rules instead of adding For example,
So the undefined behavior reproducer may be rewritten to
And then the output becomes
with
in If this way becomes considered more preferable than adding |
I know about this issue, and is essentially a duplicate of #295 (but wont close since it was found using different means). I do have a branch thats WIP which will fix this https://github.com/charles-lunarg/vk-bootstrap/tree/generate_feature_struct_comparisons but its still not done since its a non-trivial pile of code that isn't easy to do piece meal. |
But this issue is not related to inconsistent presentation of data in memory between structures, as it is described in #295. In my rerproducer To be clear, this is the issue. https://gist.github.com/shafik/848ae25ee209f698763cffee272a58f8 I haven't tested if anything unexpected happens in the current code regarding this issue, but I see dangerous accesses in at least
There are accesses to |
Ah you are right, the issue you raise is distinct. I want to say that the PR will fix both this issue and the other issue. Put another way, since the original GenericFeatureStruct PR went in, I have learned about how to handle pNext chains in a UB safe fasion and will employ that in the PR. I will try to remember to add |
Thank you, please ping me when it's ready for a review. Just in case, I attach the full list of related warnings I got when compiled with
|
I'm compiling with GCC.
When compiling with
-Wstrict-aliasing=1
warnings level enabled and at least-O2
optimizations level, I see multiple warnings likewarning: dereferencing type-punned pointer might break strict-aliasing rules
. For example, for the linein
VkBootstrap.cpp
. I looked closely and confirmed that accessing data throughVkBaseOutStructure
pointers may be dangerous when data has a type other thanVkBaseOutStructure
.I wrote a reproducer for the undefined behavior. Something like the first 3 lines of code is happening in
VkBootstrap.cpp
, next I'm attempting to abuse the strict aliasing violation.Compiling and running with at least
-O2
optimizations level gives the outputbecause accessing data through
chain[0]->pNext
is undefined behavior here. Even thoughpNext
addresses are physically equal in memory.Adding
-fno-strict-aliasing
to the compilation flags changes the output tobecause now the compiler knows that the strict aliasing rules may be violated and doesn't apply related optimizations.
Should I add
-fno-strict-aliasing
toCMakeLists.txt
? I also see that, for example, Vulkan-Samples https://github.com/KhronosGroup/Vulkan-Samples/blob/main/CMakeLists.txt uses it:And this is KhronosGroup/Vulkan-Samples#1207 why it was introduced.
The text was updated successfully, but these errors were encountered: