简体   繁体   English

使用gtest EXPECT_CALL时的竞争条件段错误,而另一个期望是使用相同的方法

[英]Race condition segfault when using gtest EXPECT_CALL while another expectation is exercising the same method

I've encountered a race condition segfault when using GTest with mocks and multithreading. 当使用带有模拟和多线程的GTest时,我遇到了竞争条件段错误。 I'm using GTest 1.8 on CentOS 6.9, G++ 6.3.1. 我在CentOS 6.9,G ++ 6.3.1上使用GTest 1.8。 It is possible to trigger a segfault by calling EXPECT_CALL on a mock with a given method, whilst another thread is calling that method on the same mock. 可以通过使用给定方法在模拟上调用EXPECT_CALL来触发段错误,而另一个线程在同一个模拟上调用该方法。 Here is a minimal example: 这是一个最小的例子:

#include <gmock/gmock.h>
#include <thread>
#include <atomic>

class MyMock
{
 public:
  MOCK_METHOD1(myMethod, void(int n));
};

TEST(MyClassTest, one)
{
  MyMock mock;
  std::atomic<int> g_arg(5);
  bool keepRunning = true;
  bool isRunning = false;
  bool canEnd = false;
  EXPECT_CALL(mock, myMethod(5))
  .Times(::testing::AtLeast(1))
  .WillRepeatedly(::testing::InvokeWithoutArgs([&isRunning](){ isRunning = true; }));
  std::thread t1([&mock, &keepRunning, &g_arg]()
  {
    while (keepRunning)
    {
      mock.myMethod(g_arg.load());
    }
  });
  while (!isRunning) {}
  EXPECT_CALL(mock, myMethod(6))
  .Times(::testing::AtLeast(1))
  .WillRepeatedly(::testing::InvokeWithoutArgs([&canEnd](){ canEnd = true; }));
  g_arg.store(6);
  while (!canEnd) {}
  keepRunning = false;
  t1.join();
}

It can be compiled with: g++ -lpthread -lgmock_main -g Gtest_test.cpp -o Gtest_test 它可以编译为: g++ -lpthread -lgmock_main -g Gtest_test.cpp -o Gtest_test

Sometimes the issue occurs immediately, sometimes you do a few hundred runs with no issue. 有时问题会立即发生,有时你会做几百次没有问题。 I have been running it like this: gdb --args ./Gtest_test --gtest_repeat=-1 --gtest_break_on_failure 我一直在运行它: gdb --args ./Gtest_test --gtest_repeat=-1 --gtest_break_on_failure

Here is some output from GDB, when the segfault occurs: 当发生段错误时,以下是GDB的一些输出:

Repeating all tests (iteration 10) . . .

[==========] Running 1 test from 1 test case.
[----------] Global test environment set-up.
[----------] 1 test from MyClassTest
[ RUN      ] MyClassTest.one
[New Thread 0x7ffff6d8b700 (LWP 14092)]

Thread 11 "Gtest_test" received signal SIGSEGV, Segmentation fault.
[Switching to Thread 0x7ffff6d8b700 (LWP 14092)]
0x00007ffff7bc8470 in pthread_mutex_lock () from /lib64/libpthread.so.0
Missing separate debuginfos, use: debuginfo-install libgcc-4.4.7-18.el6.x86_64 libstdc++-4.4.7-18.el6.x86_64
(gdb) bt
#0  0x00007ffff7bc8470 in pthread_mutex_lock () from /lib64/libpthread.so.0
#1  0x0000000000407b5b in testing::internal::MutexBase::Lock (this=0x98) at /usr/include/gtest/internal/gtest-port.h:1928
#2  0x0000000000407da8 in testing::internal::GTestMutexLock::GTestMutexLock (this=0x7ffff6d8a540, mutex=0x98) at /usr/include/gtest/internal/gtest-port.h:1999
#3  0x00007ffff7979628 in testing::internal::ExpectationBase::CheckActionCountIfNotDone() const () from /usr/lib64/libgmock_main.so
#4  0x000000000040ce9c in testing::internal::TypedExpectation<void (int)>::ShouldHandleArguments(std::tuple<int> const&) const (this=0x0, args=std::tuple containing = {...}) at /usr/include/gmock/gmock-spec-builders.h:1100
#5  0x000000000040c91a in testing::internal::FunctionMockerBase<void (int)>::FindMatchingExpectationLocked(std::tuple<int> const&) const (this=0x7fffffffd850, args=std::tuple containing = {...})
    at /usr/include/gmock/gmock-spec-builders.h:1723
#6  0x000000000040c569 in testing::internal::FunctionMockerBase<void (int)>::UntypedFindMatchingExpectation(void const*, void const**, bool*, std::ostream*, std::ostream*) (this=0x7fffffffd850, untyped_args=0x7ffff6d8ad90, 
    untyped_action=0x7ffff6d8a7e8, is_excessive=0x7ffff6d8ac4f, what=0x7ffff6d8aae0, why=0x7ffff6d8a970) at /usr/include/gmock/gmock-spec-builders.h:1687
#7  0x00007ffff797a212 in testing::internal::UntypedFunctionMockerBase::UntypedInvokeWith(void const*) () from /usr/lib64/libgmock_main.so
#8  0x00000000004096cd in testing::internal::FunctionMockerBase<void (int)>::InvokeWith(std::tuple<int> const&) (this=0x7fffffffd850, args=std::tuple containing = {...}) at /usr/include/gmock/gmock-spec-builders.h:1585
#9  0x0000000000408ba1 in testing::internal::FunctionMocker<void (int)>::Invoke(int) (this=0x7fffffffd850, a1=5) at /usr/include/gmock/gmock-generated-function-mockers.h:101
#10 0x00000000004087a8 in MyMock::myMethod (this=0x7fffffffd850, gmock_a1=5) at Gtest_test.cpp:11
#11 0x0000000000406e25 in MyClassTest_one_Test::<lambda()>::operator()(void) const (__closure=0x618d48) at Gtest_test.cpp:28
#12 0x0000000000407a86 in std::_Bind_simple<MyClassTest_one_Test::TestBody()::<lambda()>()>::_M_invoke<>(std::_Index_tuple<>) (this=0x618d48) at /opt/rh/devtoolset-6/root/usr/include/c++/6.3.1/functional:1391
#13 0x00000000004079e7 in std::_Bind_simple<MyClassTest_one_Test::TestBody()::<lambda()>()>::operator()(void) (this=0x618d48) at /opt/rh/devtoolset-6/root/usr/include/c++/6.3.1/functional:1380
#14 0x0000000000407972 in std::thread::_State_impl<std::_Bind_simple<MyClassTest_one_Test::TestBody()::<lambda()>()> >::_M_run(void) (this=0x618d40) at /opt/rh/devtoolset-6/root/usr/include/c++/6.3.1/thread:197
#15 0x000000000040e33f in execute_native_thread_routine ()
#16 0x00007ffff7bc6aa1 in start_thread () from /lib64/libpthread.so.0
#17 0x00007ffff6e74bcd in clone () from /lib64/libc.so.6
(gdb) info threads 
  Id   Target Id         Frame 
  1    Thread 0x7ffff7fdc720 (LWP 14079) "Gtest_test" MyClassTest_one_Test::TestBody (this=0x618510) at Gtest_test.cpp:36
* 11   Thread 0x7ffff6d8b700 (LWP 14092) "Gtest_test" 0x00007ffff7bc8470 in pthread_mutex_lock () from /lib64/libpthread.so.0
(gdb) t 1
[Switching to thread 1 (Thread 0x7ffff7fdc720 (LWP 14079))]
#0  MyClassTest_one_Test::TestBody (this=0x618510) at Gtest_test.cpp:36
36    while (!canEnd) {}
(gdb) bt
#0  MyClassTest_one_Test::TestBody (this=0x618510) at Gtest_test.cpp:36
#1  0x00007ffff7968edf in void testing::internal::HandleSehExceptionsInMethodIfSupported<testing::Test, void>(testing::Test*, void (testing::Test::*)(), char const*) () from /usr/lib64/libgmock_main.so
#2  0x00007ffff7962cd4 in void testing::internal::HandleExceptionsInMethodIfSupported<testing::Test, void>(testing::Test*, void (testing::Test::*)(), char const*) () from /usr/lib64/libgmock_main.so
#3  0x00007ffff7947a7c in testing::Test::Run() () from /usr/lib64/libgmock_main.so
#4  0x00007ffff7948326 in testing::TestInfo::Run() () from /usr/lib64/libgmock_main.so
#5  0x00007ffff79489ff in testing::TestCase::Run() () from /usr/lib64/libgmock_main.so
#6  0x00007ffff794f5c9 in testing::internal::UnitTestImpl::RunAllTests() () from /usr/lib64/libgmock_main.so
#7  0x00007ffff796a2f2 in bool testing::internal::HandleSehExceptionsInMethodIfSupported<testing::internal::UnitTestImpl, bool>(testing::internal::UnitTestImpl*, bool (testing::internal::UnitTestImpl::*)(), char const*) ()
   from /usr/lib64/libgmock_main.so
#8  0x00007ffff7963a48 in bool testing::internal::HandleExceptionsInMethodIfSupported<testing::internal::UnitTestImpl, bool>(testing::internal::UnitTestImpl*, bool (testing::internal::UnitTestImpl::*)(), char const*) ()
   from /usr/lib64/libgmock_main.so
#9  0x00007ffff794e17f in testing::UnitTest::Run() () from /usr/lib64/libgmock_main.so
#10 0x00007ffff798702e in RUN_ALL_TESTS() () from /usr/lib64/libgmock_main.so
#11 0x00007ffff7986fbd in main () from /usr/lib64/libgmock_main.so
#12 0x00007ffff6daad1d in __libc_start_main () from /lib64/libc.so.6
#13 0x0000000000406cc9 in _start ()

I'm aware that I can work around the issue by adding all my EXPECT_CALLs before I start the thread, but has anybody else encountered it? 我知道我可以通过在启动线程之前添加所有EXPECT_CALL来解决这个问题,但还有其他人遇到过吗? Is it a real bug, and is there a fix? 这是一个真正的错误,是否有修复?

Google Mock docs explicitly prohibit interleaving expectation setting and calling mocks: Google Mock 文档明确禁止交错期望设置和调用模拟:

Important note: Google Mock requires expectations to be set before the mock functions are called, otherwise the behavior is undefined. 重要说明:Google Mock要求在调用模拟函数之前设置期望值,否则行为未定义。 In particular, you mustn't interleave EXPECT_CALL()s and calls to the mock functions. 特别是,您不能交错EXPECT_CALL()和调用mock函数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM