Upgrading to G++ 4.8 - exception_ptr.h Does not support exception propagation

半世苍凉 提交于 2020-01-11 10:36:21

问题


I'm trying to recompile a huge legacy app with g++ 4.8 in order debug a glibc detected memory corruption problem (using AddressSanitizer). Previously we used g++ 4.4.7.

However, compilation fails with:

/opt/rh/devtoolset-2/root/usr/include/c++/4.8.2/bits/exception_ptr.h:40:4: error: #error This platform does not support exception propagation.

while compiling a custom exception handler (I guess). The custom exception handler uses exception_ptr in only one place:

void reportOtherException(void) const
{
    std::exception_ptr p = std::current_exception();
    std::string s = (p != 0 ? p.__cxa_exception_type()->name() : "null");

    printf("DvMain Bad Exception: '%s'\n", s.c_str());
    mErrorReporter(0, DvLog::WARNING, 0, Dv::NO_PROFILE, 0, DvLog::UNHANDLED_OTHER_EXCEPTION);
}

And reportOtherException() is used like this:

try
{
    // Catch and log uncaught exceptions, then exit.
    catch (const std::bad_exception& e) { exHandler.reportBadException(e);      }
    catch (const std::exception& e)     { exHandler.reportStandardException(e); }
    catch (...)                         { exHandler.reportOtherException();     }
}

I'm pretty new to C++ and don't know what the error even means. Worked with 4.4.7 and doesn't work with 4.8.

Any pointers on what needs to be changed to compile on 4.8?

EDIT I

Here's some additional info:

g++ --version
g++ (GCC) 4.8.2 20140120 (Red Hat 4.8.2-15)

Minimum Code

DvComDefaultExceptionHandler_test.h

#include "DvCommon.h"
#include "evt/DvEvt.h"
#include "log/DvLog.h"
#include "com/DvComErrorReporter.h"

#include <new>
#include <exception>
#include <sys/time.h>
#include <sys/resource.h>
#include <stdlib.h>
#include <unistd.h>
#include <malloc.h>
#include <bits/exception_ptr.h>

class DvComDefaultExceptionHandler
{
public:
    DvComDefaultExceptionHandler(const DvComErrorReporter& er) {}
    ~DvComDefaultExceptionHandler() {   }

    void reportOtherException(void) const
    {
        std::exception_ptr p = std::current_exception();
    }

private:

    static const DvComDefaultExceptionHandler*  mpInstance;
};

DvComDefaultExceptionHandler_test.cpp

#include "DvCommon.h"
#include "com/DvComDefaultExceptionHandler_test.h"


// Pointer to the single instance of the DvComDefaultExceptionHandler class.
const DvComDefaultExceptionHandler*
DvComDefaultExceptionHandler::mpInstance = 0;

Compile command and output

g++ -c -g -O0  -DDEBUG -Wall -Wextra -Wno-sign-compare -Wcast-align 
--ftemplate-depth-32 -march=native -ggdb -fPIC -Iinclude -I../../include 
-I../../src -I/usr/include/libxml2 -D_GNU_SOURCE   
-I/mnt/swdevel/DVMon/source_build/ext/ACE -D__ACE_INLINE__ 
-I/usr/local/include -I/usr/lib/qt-3.3/include 
-o DvComDefaultExceptionHandler.o DvComDefaultExceptionHandler_test.cpp
In file included from ../../include/com/DvComDefaultExceptionHandler_test.h:76:0,
                 from DvComDefaultExceptionHandler_test.cpp:13:
/opt/rh/devtoolset-2/root/usr/include/c++/4.8.2/bits/exception_ptr.h:40:4: error: #error This platform does not support exception propagation.
 #  error This platform does not support exception propagation.

EDIT II

Tracing through the include files comes down to the value of __GCC_ATOMIC_INT_LOCK_FREE. Running this simple program prints '2' as the value for __GCC_ATOMIC_INT_LOCK_FREE.

int
main(int argc, char **argv)
{
    printf("__GCC_ATOMIC_INT_LOCK_FREE = %d\n", __GCC_ATOMIC_INT_LOCK_FREE);
}

G++ VERSION:

$ g++ --version
g++ (GCC) 4.8.2 20140120 (Red Hat 4.8.2-15)

EDIT II

I've tried it with g++ 6.3.1, running on a Centos 7 VM. Still the same problem.

Source File - one line only

#include <bits/exception_ptr.h>

Compile command: g++ -c -o test.o test.cpp


回答1:


First of all, including <bits/exception_ptr.h> directly is technically unsupported. It says so right in the header file. This worked in GCC 4.4 by more or less by accident. The C++11 migration of this header file broke it because for namespace reasons, C++98 code cannot use the ATOMIC_INT_LOCK_FREE macro, and the header does not work anymore.

In GCC 7, this was fixed (again accidentally) as part of this bug:

  • std::future broken on armel

The trick to include <bits/exception_ptr.h> directly should work again in this version.

This means that your options are:

  1. Compile your code in C++11 or later mode (C++14 with GCC 6 recommended).
  2. Upgrade to DTS 7 with GCC 7 if and when it becomes available, which has the upstream fix which re-enables the C++98 hack.
  3. Wrap the use of std::exception_ptr in an opaque type, compile its implementation in C++11 or later mode, and keep the rest of the system in C++98 mode.
  4. Use another hack, perhaps like this one:

    #include <exception>
    #ifndef ATOMIC_INT_LOCK_FREE
    # define ATOMIC_INT_LOCK_FREE __GCC_ATOMIC_INT_LOCK_FREE
    #endif
    #include <bits/exception_ptr.h>
    

    Again, this is completely unsupported, but it should not be any worse than what you have today (with GCC 4.4).




回答2:


I was able to reproduce your issue using dockerized Centos6, with gcc 4.8.2. After upgrading dev tools to version 6 (gcc 6.3.1), your code compiled without any problem. Try upgrading dev tools using these steps (suggested for testing only):

  • add sclo centos6 repository via adding file /etc/yum.repos.d/devtools-sclo.repo :

    [testing-devtools]
    name=devtools multiple for CentOS 
    baseurl=http://mirror.centos.org/centos/6/sclo/x86_64/rh/
    gpgcheck=0
    
  • install devtoolset-6 packages:

    yum install devtoolset-6-binutils devtoolset-6-gcc-c++

  • set bash environment to new version:

    scl enable devtoolset-6 bash

Now try recompiling your base example and full source.

NOTE: This same repository contains packages for devtoolset-3 and devtoolset-4 also. Very easy to try if ever needed.



来源:https://stackoverflow.com/questions/45400971/upgrading-to-g-4-8-exception-ptr-h-does-not-support-exception-propagation

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!