google / googletest

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

[Bug]: StrictMock construction crashed when included in benchmark fixture #4517

Closed rikiwow closed 5 months ago

rikiwow commented 5 months ago

Describe the issue

If benchmark Fixture includes StrictMock class, running it will get Segmentation fault. The crash seems happens when constructing StrictMock objects. Please check the gdb backtrace at the end.

Steps to reproduce the problem

1. export compiler path and sysroot path

export OECORE_TARGET_SYSROOT=/fakepath/sdk-qemux86-64/tmp/sysroots/qemux86-64 export OECORE_NATIVE_SYSROOT=/fakepath/sdk-qemux86-64/tmp/sysroots/x86_64

2. Build command:

${OECORE_NATIVE_SYSROOT}/usr/bin/x86_64-pc-linux-gnu-g++ --sysroot=${OECORE_TARGET_SYSROOT} -Wl,-rpath-link,${OECORE_TARGET_SYSROOT}/usr/lib64 -Wl,-rpath,${OECORE_TARGET_SYSROOT}/usr/lib64 -rdynamic -Wl,--dynamic-linker=${OECORE_TARGET_SYSROOT}/usr/lib64/ld-linux-x86-64.so.2 main.cpp -o gmockTest -rdynamic -L${OECORE_TARGET_SYSROOT}/usr/lib64 -L. -lgtest -lgmock -lpthread -lbenchmark

3. run gmockTest built from step 2

Note: The compiler, google test and google benchmark are installed by using Yocto.

main.cpp:

#include "gmock/gmock.h"
#include <benchmark/benchmark.h>
using namespace testing;

class Foo {
public:
    virtual ~Foo() {}
    virtual void set() const = 0;
};

class MockFoo : public Foo {
    public:
        MOCK_METHOD(void, set, (), (const, override));
};

class TestBM: public benchmark::Fixture
{
    public:
        void SetUp(::benchmark::State& state) {
        }

        void TearDown(::benchmark::State& state) {
        }
        StrictMock<MockFoo> mock; //remove this line, testing pass
};

BENCHMARK_DEFINE_F(TestBM, TestcaseName)(benchmark::State& state)
{
    for (auto _ : state)
    {
        int sum = 0;
        for (int i=0; i< 100; i++)
            sum += i;
    }
}

BENCHMARK_REGISTER_F(TestBM, TestcaseName);

BENCHMARK_MAIN();

What version of GoogleTest are you using?

1.11.0

What operating system and version are you using?

Red Hat Enterprise Linux Server release 7.6 (Maipo)

What compiler and version are you using?

x86_64-pc-linux-gnu-g++ (GCC) 10.2.0

What build system are you using?

Yocto

Additional context

benchmark version: 1.6.1

gdb backtrace:

(gdb) bt

0 0x00007ffff609bdee in std::_Rb_tree_decrement(std::_Rb_tree_node_base*) ()

from /fakepath/sdk-qemux86-64/tmp/sysroots/qemux86-64/usr/lib64/libstdc++.so.6 https://github.com/google/benchmark/issues/1 0x0000000000736f0b in std::_Rb_tree_iterator<std::pair<void const const, testing::internal::CallReaction> >::operator--() () https://github.com/google/benchmark/issues/2 0x0000000000736e60 in std::_Rb_tree<void const, std::pair<void const const, testing::internal::CallReaction>, std::_Select1st<std::pair<void const const, testing::internal::CallReaction> >, std::less<void const>, std::allocator<std::pair<void const const, testing::internal::CallReaction> > >::_M_get_insert_unique_pos(void const const&) () https://github.com/google/benchmark/pull/3 0x0000000000734fdb in std::_Rb_tree<void const, std::pair<void const const, testing::internal::CallReaction>, std::_Select1st<std::pair<void const const, testing::internal::CallReaction> >, std::less<void const>, std::allocator<std::pair<void const const, testing::internal::CallReaction> > >::_M_get_insert_hint_unique_pos(std::_Rb_tree_const_iterator<std::pair<void const const, testing::internal::CallReaction> >, void const const&) () https://github.com/google/benchmark/pull/4 0x0000000000733108 in std::_Rb_tree_iterator<std::pair<void const const, testing::internal::CallReaction> > std::_Rb_tree<void const, std::pair<void const const, testing::internal::CallReaction>, std::_Select1st<std::pair<void const const, testing::internal::CallReaction> >, std::less<void const>, std::allocator<std::pair<void const const, testing::internal::CallReaction> > >::_M_emplace_hint_unique<std::piecewise_construct_t const&, std::tuple<void const const&>, std::tuple<> >(std::_Rb_tree_const_iterator<std::pair<void const const, testing::internal::CallReaction> >, std::piecewise_construct_t const&, std::tuple<void const const&>&&, std::tuple<>&&) () https://github.com/google/benchmark/issues/5 0x0000000000730e10 in std::map<void const, testing::internal::CallReaction, std::less<void const>, std::allocator<std::pair<void const const, testing::internal::CallReaction> > >::operator[](void const const&) () https://github.com/google/benchmark/pull/6 0x000000000072c0a3 in testing::(anonymous namespace)::SetReactionOnUninterestingCalls(void const, testing::internal::CallReaction) () https://github.com/google/benchmark/issues/7 0x000000000072c133 in testing::Mock::FailUninterestingCalls(void const*) () https://github.com/google/benchmark/pull/8 0x0000000000663941 in TestBM_TestcaseName_Benchmark::TestBM_TestcaseName_Benchmark() () https://github.com/google/benchmark/pull/9 0x00000000005b68e7 in _GLOBALsub_I_fake.cpp () https://github.com/google/benchmark/pull/10 0x00007ffff5d24fee in libc_start_main () from /fakepath/sdk-qemux86-64/tmp/sysroots/qemux86-64/usr/lib64/libc.so.6 https://github.com/google/benchmark/issues/11 0x00000000005bc615 in _start ()

derekmauro commented 5 months ago

What version of GoogleTest are you using? 1.11.0

This is an old and unsupported version. I believe the issue you are encountering is related to C++'s infamous static initialization order fiasco and is fixed in the latest version.