google / googletest

GoogleTest - Google Testing and Mocking Framework
https://google.github.io/googletest/
BSD 3-Clause "New" or "Revised" License
33.84k stars 10k forks source link

Test fail due to SEGFAULT using GNU 9.3.0 #3506

Closed langroodi closed 2 years ago

langroodi commented 2 years ago

I have added a new test to my project as follows which caused a segmentation fault while running 'ctest' command:

    TEST(LoggingFrameworkTest, LogMethod)
    {
        const std::string cAppId{"APP01"};
        const LogMode cLogMode{LogMode::kConsole};
        const std::string cCtxId{"CTX01"};
        const std::string cCtxDescription{"Default Test Context"};
        const LogLevel cLogLevel{LogLevel::kWarn};

        LoggingFramework _loggingFramework =
            LoggingFramework::Create(cAppId, cLogMode); // Calling static factory pattern function

       Logger _logger =
            _loggingFramework.CreateLogger(cCtxId, cCtxDescription);

        LogStream _logStream;

        ASSERT_NO_THROW(
            _loggingFramework.Log(_logger, cLogLevel, _logStream));
    }

at this part of the code:

    LoggingFramework LoggingFramework::Create(
        std::string appId,
        LogMode logMode,
        LogLevel logLevel,
        std::string appDescription)
    {
        if (logMode == LogMode::kFile)
        {
            throw std::invalid_argument(
                "File logging mode is not feasible within this constructor override.");
        }

        if (logMode == LogMode::kConsole)
        {
            sink::LogSink *_logSink =
                new sink::ConsoleLogSink(appId, appDescription);
            LoggingFramework _result(_logSink, logLevel); // <-- Error occurs here

            return _result;
        }
        else
        {
            throw std::invalid_argument(
                "The log mode is not currently supported.");
        }
    }

The code has been compiled in the Debug mode via GCC 9.3.0 x86-64 Linux-GNU. I even disabled the PTHREADS using _gtest_disablepthreads option, but it did not solve the issue. Following is the link to my CMakeLists: https://github.com/langroodi/Adaptive-AUTOSAR/blob/master/CMakeLists.txt

When I changed the compiler type in my pipeline from GCC to Clang 10.0 the issue disappeared. The whole project build workspace is generated by CMake 3.16.

I am still using this commit: 8d51ffdfab10b3fba636ae69bc03da4b54f8c235 of Google Test without using any object mocking. So, I am not sure the issue is solved in the newer commit or not. Or maybe there is a error in my code.

If you need more information, I would be happy to provide that.

asoffer commented 2 years ago

Without seeing the constructor for the LoggingFramework object or the stack trace from the segfault we will not be able to help here. Could you produce a Compiler Explorer link reproducing the issue?

langroodi commented 2 years ago

Without seeing the constructor for the LoggingFramework object or the stack trace from the segfault we will not be able to help here. Could you produce a Compiler Explorer link reproducing the issue?

This is the LoggingFramework constructor:

LoggingFramework::LoggingFramework(
            sink::LogSink *logSink,
            LogLevel logLevel) : mLogSink{logSink},
                                 mDefaultLogLevel{logLevel}
{
}

in which the member initializer assigns a pointer and sets the mDefaultLogLevel enum class.

Following is also the SEGFAULT stack trace generated by the backtrace function while GCC rdynamic switch was enabled:

Error: signal 11:
/tmp/Adaptive-AUTOSAR/build/ara_log_test(_ZN3ara3log7handlerEi+0x38)[0x5555555d7a7b]
/lib/x86_64-linux-gnu/libc.so.6(+0x46210)[0x7ffff7a8f210]
/tmp/Adaptive-AUTOSAR/build/ara_log_test(_ZN3ara3log16LoggingFramework3LogERKNS0_6LoggerENS0_8LogLevelERKNS0_9LogStreamE+0x87)[0x5555555da501]
/tmp/Adaptive-AUTOSAR/build/ara_log_test(_ZN3ara3log35LoggingFrameworkTest_LogMethod_Test8TestBodyEv+0x285)[0x5555555d7d53]
/tmp/Adaptive-AUTOSAR/build/ara_log_test(_ZN7testing8internal38HandleSehExceptionsInMethodIfSupportedINS_4TestEvEET0_PT_MS4_FS3_vEPKc+0x69)[0x555555619314]
/tmp/Adaptive-AUTOSAR/build/ara_log_test(_ZN7testing8internal35HandleExceptionsInMethodIfSupportedINS_4TestEvEET0_PT_MS4_FS3_vEPKc+0x5e)[0x555555611101]
/tmp/Adaptive-AUTOSAR/build/ara_log_test(_ZN7testing4Test3RunEv+0xf2)[0x5555555e5a04]
/tmp/Adaptive-AUTOSAR/build/ara_log_test(_ZN7testing8TestInfo3RunEv+0x140)[0x5555555e6446]
/tmp/Adaptive-AUTOSAR/build/ara_log_test(_ZN7testing9TestSuite3RunEv+0x149)[0x5555555e6d43]
/tmp/Adaptive-AUTOSAR/build/ara_log_test(_ZN7testing8internal12UnitTestImpl11RunAllTestsEv+0x457)[0x5555555f67e3]
/tmp/Adaptive-AUTOSAR/build/ara_log_test(_ZN7testing8internal38HandleSehExceptionsInMethodIfSupportedINS0_12UnitTestImplEbEET0_PT_MS4_FS3_vEPKc+0x69)[0x55555561a842]
/tmp/Adaptive-AUTOSAR/build/ara_log_test(_ZN7testing8internal35HandleExceptionsInMethodIfSupportedINS0_12UnitTestImplEbEET0_PT_MS4_FS3_vEPKc+0x5e)[0x55555561233f]
/tmp/Adaptive-AUTOSAR/build/ara_log_test(_ZN7testing8UnitTest3RunEv+0xc4)[0x5555555f4f10]
/tmp/Adaptive-AUTOSAR/build/ara_log_test(_Z13RUN_ALL_TESTSv+0x15)[0x5555555d8c56]
/tmp/Adaptive-AUTOSAR/build/ara_log_test(main+0x43)[0x5555555d8bd8]
/lib/x86_64-linux-gnu/libc.so.6(__libc_start_main+0xf3)[0x7ffff7a700b3]
/tmp/Adaptive-AUTOSAR/build/ara_log_test(_start+0x2e)[0x5555555d1c7e]
[1] + Done                       "/usr/bin/gdb" --interpreter=mi --tty=${DbgTerm} 0<"/tmp/Microsoft-MIEngine-In-usbfsss4.dmm" 1>"/tmp/Microsoft-MIEngine-Out-sxeofyfd.d4i"

This is also the link to the test file which can be built and executed, but I am not sure that Compiler Explorer can support GTest for reproducing the same situation.

asoffer commented 2 years ago

The segfault seems to be in the Log member function, which is only passed objects constructed that are not related to googletest, making me think it's likely unrelated to googletest. Given that, I'm going to close this. If you find evidence that the crash is related to googletest, please reopen.

Runing the code with something like Address Sanitizer might be instructive.