ez8-co / emock

🐞 下一代C/C++跨平台mock库 (Next generation cross-platform mock library for C/C++)
Apache License 2.0
120 stars 35 forks source link

wsl ubuntu环境下mock 类成员函数 出现段错误 #38

Open klaushhp opened 1 year ago

klaushhp commented 1 year ago

Program received signal SIGSEGV, Segmentation fault. __memmove_avx_unaligned_erms () at ../sysdeps/x86_64/multiarch/memmove-vec-unaligned-erms.S:312 312 ../sysdeps/x86_64/multiarch/memmove-vec-unaligned-erms.S: No such file or directory. (gdb) bt

0 __memmove_avx_unaligned_erms () at ../sysdeps/x86_64/multiarch/memmove-vec-unaligned-erms.S:312

1 0x000000000812ffae in emock::JmpOnlyApiHook::JmpOnlyApiHook(void const, void const, void const*) ()

2 0x000000000812c5b6 in emock::ApiHook::ApiHook(void const, void const, void const*) ()

3 0x000000000812c287 in emock::ApiHookKey::ApiHookKey(void const, emock::ApiHookHolder, bool) ()

4 0x000000000812be27 in emock::HookMockObjectImpl::addMethod(std::__cxx11::basic_string<char, std::char_traits, std::allocator > const&, void const, emock::ApiHookHolder, emock::InvocationMockerNamespace*, bool) ()

5 0x000000000812c005 in emock::HookMockObject::method(std::__cxx11::basic_string<char, std::char_traits, std::allocator > const&, void const, emock::ApiHookHolder, bool) ()

6 0x00000000080aaa81 in emock::InvocationMockBuilderGetter emock::mockAPI<void, Foo, int>(std::__cxx11::basic_string<char, std::char_traits, std::allocator > const&, void (Foo::*)(int)) ()

7 0x00000000080a95f6 in member_func_ut_member_func_Test::TestBody() ()

8 0x00000000080f85b6 in void testing::internal::HandleSehExceptionsInMethodIfSupported<testing::Test, void>(testing::Test, void (testing::Test::)(), char const*) ()

9 0x00000000080f0349 in void testing::internal::HandleExceptionsInMethodIfSupported<testing::Test, void>(testing::Test, void (testing::Test::)(), char const*) ()

10 0x00000000080ca286 in testing::Test::Run() ()

11 0x00000000080cad55 in testing::TestInfo::Run() ()

12 0x00000000080cb72b in testing::TestSuite::Run() ()

13 0x00000000080db6b6 in testing::internal::UnitTestImpl::RunAllTests() ()

14 0x00000000080f9beb in bool testing::internal::HandleSehExceptionsInMethodIfSupported<testing::internal::UnitTestImpl, bool>(testing::internal::UnitTestImpl, bool (testing::internal::UnitTestImpl::)(), char const*) ()

15 0x00000000080f1657 in bool testing::internal::HandleExceptionsInMethodIfSupported<testing::internal::UnitTestImpl, bool>(testing::internal::UnitTestImpl, bool (testing::internal::UnitTestImpl::)(), char const*) ()

16 0x00000000080d9cb0 in testing::UnitTest::Run() ()

17 0x00000000080a9e70 in RUN_ALL_TESTS() ()

18 0x00000000080a9754 in main ()

(gdb)

klaushhp commented 1 year ago

环境 x86_64
4.4.0-19041-Microsoft

klaushhp commented 1 year ago

commit 55db580faa82ef98882a97dfc9a6424b91733a35

Robin-1978 commented 1 year ago

ubuntu 22.04 gcc 12.1.0 出现同样的问题

0 __memmove_avx_unaligned_erms () at ../sysdeps/x86_64/multiarch/memmove-vec-unaligned-erms.S:416

1 0x000055555561d4b7 in emock::JmpOnlyApiHookImpl::saveOriginalData (this=0x55555563ed50)

at /home/robin/.cache/bazel/_bazel_robin/389f81c4c4ae302f3b55a547146dfe65/sandbox/linux-sandbox/20/execroot/gs_map/external/emock/src/JmpOnlyApiHook.cpp:57

2 emock::JmpOnlyApiHookImpl::startHook (this=0x55555563ed50)

at /home/robin/.cache/bazel/_bazel_robin/389f81c4c4ae302f3b55a547146dfe65/sandbox/linux-sandbox/20/execroot/gs_map/external/emock/src/JmpOnlyApiHook.cpp:63

3 emock::JmpOnlyApiHookImpl::JmpOnlyApiHookImpl (stub=0x55555559fa56 <emock::ApiHookFunctor<int (MyUsing, int, int), 10u>::hook(MyUsing, int, int)>,

trampoline=0x0, api=0x4a5a6, this=0x55555563ed50)
at /home/robin/.cache/bazel/_bazel_robin/389f81c4c4ae302f3b55a547146dfe65/sandbox/linux-sandbox/20/execroot/gs_map/external/emock/src/JmpOnlyApiHook.cpp:44

4 emock::JmpOnlyApiHook::JmpOnlyApiHook (this=this@entry=0x55555563ed30, api=api@entry=0x4a5a6, trampoline=trampoline@entry=0x0,

stub=stub@entry=0x55555559fa56 <emock::ApiHookFunctor<int (MyUsing*, int, int), 10u>::hook(MyUsing*, int, int)>)
at /home/robin/.cache/bazel/_bazel_robin/389f81c4c4ae302f3b55a547146dfe65/sandbox/linux-sandbox/20/execroot/gs_map/external/emock/src/JmpOnlyApiHook.cpp:91

5 0x000055555561d69f in emock::ApiHookImpl::ApiHookImpl (

stub=0x55555559fa56 <emock::ApiHookFunctor<int (MyUsing*, int, int), 10u>::hook(MyUsing*, int, int)>, trampoline=0x0, api=0x4a5a6, this=0x55555563ed30)
at /home/robin/.cache/bazel/_bazel_robin/389f81c4c4ae302f3b55a547146dfe65/sandbox/linux-sandbox/20/execroot/gs_map/external/emock/src/ApiHook.cpp:34

6 emock::ApiHook::ApiHook (this=this@entry=0x55555563bef0, api=api@entry=0x4a5a6, trampoline=0x0,

stub=stub@entry=0x55555559fa56 <emock::ApiHookFunctor<int (MyUsing*, int, int), 10u>::hook(MyUsing*, int, int)>)
at /home/robin/.cache/bazel/_bazel_robin/389f81c4c4ae302f3b55a547146dfe65/sandbox/linux-sandbox/20/execroot/gs_map/external/emock/src/ApiHook.cpp:50

7 0x000055555561cd3d in emock::ApiHookKey::ApiHookKey (this=this@entry=0x55555563bd30, api=api@entry=0x4a5a6, holder=holder@entry=0x55555563bed0,

isMemFun=isMemFun@entry=true)
at /home/robin/.cache/bazel/_bazel_robin/389f81c4c4ae302f3b55a547146dfe65/sandbox/linux-sandbox/20/execroot/gs_map/external/emock/src/ApiHookKey.cpp:39

8 0x000055555561c7a8 in emock::HookMockObjectImpl::addMethod (this=0x55555563b780, name="&MyUsing::Add", api=0x4a5a6, hookHolder=0x55555563bed0,

ns=0x555555626060 <emock::GlobalMockObject::instance>, ns@entry=0x55555563bed0, isMemFun=<optimized out>)
at /home/robin/.cache/bazel/_bazel_robin/389f81c4c4ae302f3b55a547146dfe65/sandbox/linux-sandbox/20/execroot/gs_map/external/emock/src/HookMockObject.cpp:88

9 0x000055555561c958 in emock::HookMockObjectImpl::getMethod (this=, name="&MyUsing::Add", api=, hookHolder=,

ns=0x55555563bed0, isMemFun=<optimized out>)
at /home/robin/.cache/bazel/_bazel_robin/389f81c4c4ae302f3b55a547146dfe65/sandbox/linux-sandbox/20/execroot/gs_map/external/emock/src/HookMockObject.cpp:110

--Type for more, q to quit, c to continue without paging--Quit

Robin-1978 commented 1 year ago

代码:

include <gtest/gtest.h>

include <emock/emock.hpp>

class MyUsing { public: int Add(int a, int b) { return a + b; } };

class MyRunner { public: int Run(int a) { MyUsing my; return my.Add(a, a * a); } };

int add_stub(int a, int b) { return a+b; }

class MyRunnerTest : public testing::Test { public: protected: virtual void SetUp(){}; void TearDown() override{}; };

TEST_F(MyRunnerTest, TestRun) { MyRunner my; EMOCK(&MyUsing::Add) .stubs() .will(returnValue(2)); EXPECT_EQ(2, my.Run(1)); }

int main(int argc, char *argv[]) { printf("Running main() from %s\n", FILE); testing::InitGoogleTest(&argc, argv); // ros::init(argc, argv, "gs_map_test");

return RUN_ALL_TESTS(); }

Robin-1978 commented 1 year ago

查了一下, u.p = SymbolRetriever::getMethodAddress(u.p, typeid(api), name);\ 这个函数取得的函数地址只有5五位数,非真正有效的内存地址,所以 memcpy直接挂了。

scholarsC commented 3 months ago

我也遇到同样的问题,根本不能用啊,这个还没有解决吗?