Boost C++ Libraries: Ticket #12773: WINDOWS- Boost thread 1.63.0 strict aliasing warnings https://svn.boost.org/trac10/ticket/12773 <p> I'm cross-compiling boost on Linux, having 32-bit mingw64 as a target. </p> <p> I'm still seeing strict aliasing complaints on boost 1.63.0 thread/win32/shared_mutex.hpp. </p> <p> As gcc 6 has even stricter aliasing rules, I'm afraid that these may be harmful, so I came up with the attached patch - which is ugly, but works. </p> en-us Boost C++ Libraries /htdocs/site/boost.png https://svn.boost.org/trac10/ticket/12773 Trac 1.4.3 Alexandre Pereira Nunes <alexandre.nunes@…> Thu, 19 Jan 2017 13:35:38 GMT attachment set https://svn.boost.org/trac10/ticket/12773 https://svn.boost.org/trac10/ticket/12773 <ul> <li><strong>attachment</strong> → <span class="trac-field-new">win32_shared_mutex.patch</span> </li> </ul> <p> Path fixing the warning/potential misbehavior </p> Ticket Alexandre Pereira Nunes <alexandre.nunes@…> Thu, 19 Jan 2017 14:32:59 GMT attachment set https://svn.boost.org/trac10/ticket/12773 https://svn.boost.org/trac10/ticket/12773 <ul> <li><strong>attachment</strong> → <span class="trac-field-new">win32_shared_mutex_punning.patch</span> </li> </ul> <p> Newer patch, uses type punning union. </p> Ticket Alexandre Pereira Nunes <alexandre.nunes@…> Thu, 19 Jan 2017 14:34:50 GMT <link>https://svn.boost.org/trac10/ticket/12773#comment:1 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/12773#comment:1</guid> <description> <p> The win32_shared_mutex.patch didn't work with gcc 6.3.0. I came up with a type-punning union instead, but I'm unsure the anonymous union is portable. One can come up with a better idea for that case. Works for me™ </p> </description> <category>Ticket</category> </item> <item> <dc:creator>viboes</dc:creator> <pubDate>Sun, 29 Jan 2017 16:32:27 GMT</pubDate> <title>summary changed https://svn.boost.org/trac10/ticket/12773#comment:2 https://svn.boost.org/trac10/ticket/12773#comment:2 <ul> <li><strong>summary</strong> <span class="trac-field-old">Boost thread 1.63.0 strict aliasing warnings</span> → <span class="trac-field-new">WINDOWS- Boost thread 1.63.0 strict aliasing warnings</span> </li> </ul> Ticket viboes Sun, 29 Jan 2017 16:36:14 GMT <link>https://svn.boost.org/trac10/ticket/12773#comment:3 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/12773#comment:3</guid> <description> <p> Could you report explicitly the strict aliasing complaints? </p> </description> <category>Ticket</category> </item> <item> <dc:creator>viboes</dc:creator> <pubDate>Sun, 19 Feb 2017 11:37:50 GMT</pubDate> <title/> <link>https://svn.boost.org/trac10/ticket/12773#comment:4 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/12773#comment:4</guid> <description> <p> Ok, I see from the patch where the strict aliasing problem could come from. I don't think the union should solve the issue as we introduce then UB. </p> <p> The safe way is to check each one of the bit fields. </p> <p> Please, add here the report you got. </p> </description> <category>Ticket</category> </item> <item> <author>alexandre.nunes@…</author> <pubDate>Sun, 19 Feb 2017 13:29:04 GMT</pubDate> <title/> <link>https://svn.boost.org/trac10/ticket/12773#comment:5 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/12773#comment:5</guid> <description> <p> You're absolutely right that type-punning leads to UB, and so does current implementation. It seems to me that other than checking member-by-member, the other option would be to - providing the structure is sufficiently sane - use std::memcmp() to check the data. Most compilers can optimize the call away, when that's safe. </p> <p> Here's the compiler report: </p> <pre class="wiki"> gcc.compile.c++ bin.v2/libs/type_erasure/build/gcc-mingw-6.3/release/link-static/runtime-link-static/target-os-windows/threadapi-win32/threading-multi/dynamic_binding.o In file included from ./boost/thread/shared_mutex.hpp:18:0, from libs/type_erasure/src/dynamic_binding.cpp:14: ./boost/thread/win32/shared_mutex.hpp: In instantiation of 'T boost::shared_mutex::interlocked_compare_exchange(T*, T, T) [with T = boost::shared_mutex::state_data]': ./boost/thread/win32/shared_mutex.hpp:130:103: required from here ./boost/thread/win32/shared_mutex.hpp:51:63: error: dereferencing type-punned pointer will break strict-aliasing rules [-Werror=strict-aliasing] *reinterpret_cast&lt;long*&gt;(&amp;new_value), ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ./boost/thread/win32/shared_mutex.hpp:52:63: error: dereferencing type-punned pointer will break strict-aliasing rules [-Werror=strict-aliasing] *reinterpret_cast&lt;long*&gt;(&amp;comparand)); ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ./boost/thread/win32/shared_mutex.hpp:53:20: error: dereferencing type-punned pointer will break strict-aliasing rules [-Werror=strict-aliasing] return *reinterpret_cast&lt;T const*&gt;(&amp;res); ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ./boost/thread/win32/shared_mutex.hpp:53:52: error: dereferencing type-punned pointer will break strict-aliasing rules [-Werror=strict-aliasing] return *reinterpret_cast&lt;T const*&gt;(&amp;res); ^ cc1plus: some warnings being treated as errors </pre> </description> <category>Ticket</category> </item> <item> <dc:creator>anonymous</dc:creator> <pubDate>Sun, 19 Feb 2017 13:34:41 GMT</pubDate> <title/> <link>https://svn.boost.org/trac10/ticket/12773#comment:6 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/12773#comment:6</guid> <description> <p> IIRC there was also other similar reports in this code (line 39): </p> <pre class="wiki">friend bool operator==(state_data const&amp; lhs,state_data const&amp; rhs) { return *reinterpret_cast&lt;unsigned const*&gt;(&amp;lhs)==*reinterpret_cast&lt;unsigned const*&gt;(&amp;rhs); } </pre><p> But these happened further down the road, after "fixing" the other errors. </p> </description> <category>Ticket</category> </item> <item> <dc:creator>viboes</dc:creator> <pubDate>Fri, 24 Feb 2017 14:32:25 GMT</pubDate> <title/> <link>https://svn.boost.org/trac10/ticket/12773#comment:7 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/12773#comment:7</guid> <description> <p> Thanks for the reported errors. </p> </description> <category>Ticket</category> </item> <item> <dc:creator>viboes</dc:creator> <pubDate>Tue, 14 Nov 2017 20:54:51 GMT</pubDate> <title>owner, status changed https://svn.boost.org/trac10/ticket/12773#comment:8 https://svn.boost.org/trac10/ticket/12773#comment:8 <ul> <li><strong>owner</strong> changed from <span class="trac-author">Anthony Williams</span> to <span class="trac-author">viboes</span> </li> <li><strong>status</strong> <span class="trac-field-old">new</span> → <span class="trac-field-new">assigned</span> </li> </ul> Ticket