Opened 7 years ago

Last modified 6 years ago

#11989 new Bugs

gcc 5.3 optimizer mangles non_blocking_recvfrom

Reported by: tmark@… Owned by: chris_kohlhoff
Milestone: To Be Determined Component: asio
Version: Boost 1.58.0 Severity: Optimization
Keywords: Cc:

Description

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.

The affected function is:

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& ec, size_t& bytes_transferred)

in the file:

/usr/include/boost/asio/detail/impl/socket_ops.ipp

where the tests for "ec" shown below are optmized out:

     :

    // 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;

     :

This causes the function to always return true.

I have also opened a ticket with the good people at GCC, ticket #69789. 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.

Additional information:

  1. gcc --version output:

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-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)

  1. OS info:

Linux fedora23-64-1 4.3.4-300.fc23.x86_64 #1 SMP Mon Jan 25 13:39:23 UTC 2016 x86_64 x86_64 x86_64 GNU/Linux

  1. Compiler command line:

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 test -f 'udp_socket_unittest.cc' || echo './'udp_socket_unittest.cc

Change History (2)

comment:1 by tmark@…, 7 years ago

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:

     :

    // 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;

     :

Alternatively, adding an inline operator:

      inline friend bool operator==( const error_code & lhs,
                                     const int & rhs ) BOOST_SYSTEM_NOEXCEPT
      {
        return lhs.m_val == rhs;
      }

to error_code class also cures the issue.

comment:2 by tomasz(at)isc(dot)org, 6 years ago

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

Note: See TracTickets for help on using tickets.