Boost C++ Libraries: Ticket #11989: gcc 5.3 optimizer mangles non_blocking_recvfrom https://svn.boost.org/trac10/ticket/11989 <p> Our Kea project uses boost::asio. When compiled with gcc 5.3.1 and -O2, the optimizer is incorrectly removing tests, against a variable which can be changed, from one of the boost::asio functions. </p> <p> The affected function is: </p> <pre class="wiki">bool non_blocking_recvfrom(socket_type s, buf* bufs, size_t count, int flags, socket_addr_type* addr, std::size_t* addrlen, boost::system::error_code&amp; ec, size_t&amp; bytes_transferred) </pre><p> in the file: </p> <blockquote> <p> /usr/include/boost/asio/detail/impl/socket_ops.ipp </p> </blockquote> <p> where the tests for "ec" shown below are optmized out: </p> <pre class="wiki"> : // Retry operation if interrupted by signal. if (ec == boost::asio::error::interrupted) continue; // Check if we need to run the operation again. if (ec == boost::asio::error::would_block || ec == boost::asio::error::try_again) return false; : </pre><p> This causes the function to always return true. </p> <p> I have also opened a ticket with the good people at GCC, ticket <a class="missing ticket">#69789</a>. This occurred with Boost 1.58, though I suspect the issue is present in 1.60 as the code for the function appears to be unchanged. </p> <p> Additional information: </p> <ol><li>gcc --version output: </li></ol><p> Using built-in specs. COLLECT_GCC=/usr/bin/gcc COLLECT_LTO_WRAPPER=/usr/libexec/gcc/x86_64-redhat-linux/5.3.1/lto-wrapper Target: x86_64-redhat-linux Configured with: ../configure --enable-bootstrap --enable-languages=c,c++,objc,obj-c++,fortran,ada,go,lto --prefix=/usr --mandir=/usr/share/man --infodir=/usr/share/info --enable-shared --enable-threads=posix --enable-checking=release --enable-multilib --with-system-zlib --enable-<span class="underline">cxa_atexit --disable-libunwind-exceptions --enable-gnu-unique-object --enable-linker-build-id --with-linker-hash-style=gnu --enable-plugin --enable-initfini-array --disable-libgcj --with-isl --enable-libmpx --enable-gnu-indirect-function --with-tune=generic --with-arch_32=i686 --build=x86_64-redhat-linux Thread model: posix gcc version 5.3.1 20151207 (Red Hat 5.3.1-2) (GCC) </span></p> <ol start="2"><li>OS info: </li></ol><p> Linux fedora23-64-1 4.3.4-300.fc23.x86_64 <a class="closed ticket" href="https://svn.boost.org/trac10/ticket/1" title="#1: Bugs: boost.build causes ftjam to segfault (closed: Wont Fix)">#1</a> SMP Mon Jan 25 13:39:23 UTC 2016 x86_64 x86_64 x86_64 GNU/Linux </p> <ol start="3"><li>Compiler command line: </li></ol><p> g++ -DHAVE_CONFIG_H -I. -I../../../.. -I../../../../src/lib -I../../../../src/lib -DTEST_DATA_DIR=\"./testdata\" -I/opt/gtest/gtest-1.7.0 -I/opt/gtest/gtest-1.7.0/include -DOS_LINUX -I../../../../ext/coroutine -DBOOST_ASIO_HEADER_ONLY -DBOOST_ASIO_DISABLE_THREADS=1 -DBOOST_ERROR_CODE_HEADER_ONLY -DBOOST_SYSTEM_NO_DEPRECATED -Wall -Wextra -Wnon-virtual-dtor -Wwrite-strings -Woverloaded-virtual -Wno-sign-compare -pthread -Werror -fPIC -Wno-missing-field-initializers -Wno-unused-parameter -g -O2 -save-temps -MT run_unittests-udp_socket_unittest.o -MD -MP -MF .deps/run_unittests-udp_socket_unittest.Tpo -c -o run_unittests-udp_socket_unittest.o <code>test -f 'udp_socket_unittest.cc' || echo './'</code>udp_socket_unittest.cc </p> en-us Boost C++ Libraries /htdocs/site/boost.png https://svn.boost.org/trac10/ticket/11989 Trac 1.4.3 tmark@… Tue, 23 Feb 2016 11:35:16 GMT <link>https://svn.boost.org/trac10/ticket/11989#comment:1 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/11989#comment:1</guid> <description> <p> After further analysis we've discovered that the optimizer does not remove the tests, it alters the comparison of (ec == enumerated value). If one replaces the "ec" with "ec.value()" in the expressions, such that you are explicitly comparing the interger error value the code works correctly when optimized: </p> <pre class="wiki"> : // Retry operation if interrupted by signal. if (ec.value() == boost::asio::error::interrupted) continue; // Check if we need to run the operation again. if (ec.value() == boost::asio::error::would_block || ec.value() == boost::asio::error::try_again) return false; : </pre><p> Alternatively, adding an inline operator: </p> <pre class="wiki"> inline friend bool operator==( const error_code &amp; lhs, const int &amp; rhs ) BOOST_SYSTEM_NOEXCEPT { return lhs.m_val == rhs; } </pre><p> to error_code class also cures the issue. </p> </description> <category>Ticket</category> </item> <item> <dc:creator>tomasz(at)isc(dot)org</dc:creator> <pubDate>Fri, 19 Aug 2016 08:00:27 GMT</pubDate> <title/> <link>https://svn.boost.org/trac10/ticket/11989#comment:2 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/11989#comment:2</guid> <description> <p> I confirm the issue still manifests itself on libboost 1.58 when compiled using g++ 5.4.0-6ubuntu1~16.04.2. That's the stock version available in Ubuntu 16.04 </p> </description> <category>Ticket</category> </item> </channel> </rss>