Open Quuxplusone opened 5 years ago
Attached testfail.cpp
(1691 bytes, text/x-c++src): Test program source
Attached run.sh
(160 bytes, application/x-shellscript): Build & run script reproducing problem
Adding -fno-strict-aliasing seems to lead to the behavior you expect. Is it legal to alias a char array with a pointer of type mapped_data_t?
(In reply to Ignat Loskutov from comment #2)
> Adding -fno-strict-aliasing seems to lead to the behavior you expect. Is it
> legal to alias a char array with a pointer of type mapped_data_t?
Yes test works as expected with -fno-strict-aliasing.
After further investigation and discussion with colleagues I admit that
strictly speaking test code contains UB (even several UBs of different types,
see http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2018/p0593r2.html).
But code patterns similar to type punning in test code is a common practice
when working with mapped data. Libraries like FlatBuffers doesn't require -fno-
strict-aliasing (https://stackoverflow.com/questions/24330925/does-flatbuffers-
avoid-strict-aliasing-somehow).
Exploiting UB in this case is something that I wouldn't expect from compiler,
because I have no language means to barrier optimizer. I think it's wrong to
force usage of -fno-strict-aliasing on all code when only fraction of it is
liable for UB.
testfail.cpp
(1691 bytes, text/x-c++src)run.sh
(160 bytes, application/x-shellscript)