cpp-stub icon indicating copy to clipboard operation
cpp-stub copied to clipboard

A segmentation fault occurs when it compiles the UT on the arm64 platform.

Open KindleKin opened this issue 2 years ago • 5 comments

root@centos:/home/xxx/cpp-stub/ut_examples/gtest/ut/src# ll total 12 drwxr-xr-x. 4 root root 87 Feb 4 07:57 ./ drwxr-xr-x. 5 root root 43 Feb 3 10:12 ../ -rw-r--r--. 1 root root 588 Feb 4 07:31 Makefile drwxr-xr-x. 2 root root 25 Feb 4 07:57 fixtures/ -rw-r--r--. 1 root root 220 Feb 3 09:56 main.cpp drwxr-xr-x. 2 root root 26 Feb 4 07:57 mocks/ -rw-r--r--. 1 root root 3233 Feb 4 07:27 test_obj.cpp root@centos:/home/xxx/cpp-stub/ut_examples/gtest/ut/src# make g++ -std=c++11 -g -no-pie -fno-stack-protector -Wall -Wno-unused-function -Wno-unused-variable -Wno-pmf-conversions -I../include -Imocks -Ifixtures -I../../src -I../../../../src -c -MMD test_obj.cpp -o test_obj.o g++ -std=c++11 -g -no-pie -fno-stack-protector -Wall -Wno-unused-function -Wno-unused-variable -Wno-pmf-conversions -I../include -Imocks -Ifixtures -I../../src -I../../../../src -c -MMD main.cpp -o main.o g++ -std=c++11 -g -no-pie -fno-stack-protector -Wall -Wno-unused-function -Wno-unused-variable -Wno-pmf-conversions ./test_obj.o ./main.o -L../lib -lgtest -lgmock -lm -lpthread -o ut root@centos69203:/home/xxx/cpp-stub/ut_examples/gtest/ut/src# ./ut --gtest_filter=get_number_test.returns_correct_value Note: Google Test filter = get_number_test.returns_correct_value [==========] Running 1 test from 1 test case. [----------] Global test environment set-up. [----------] 1 test from get_number_test [ RUN ] get_number_test.returns_correct_value Segmentation fault (core dumped) root@centos:/home/xxx/cpp-stub/ut_examples/gtest/ut/src# ll total 4340 drwxr-xr-x. 4 root root 179 Feb 4 07:58 ./ drwxr-xr-x. 5 root root 43 Feb 3 10:12 ../ -rw-r--r--. 1 root root 588 Feb 4 07:31 Makefile -rw-------. 1 root root 2031616 Feb 4 07:58 core.90826 drwxr-xr-x. 2 root root 25 Feb 4 07:57 fixtures/ -rw-r--r--. 1 root root 220 Feb 3 09:56 main.cpp -rw-r--r--. 1 root root 1700 Feb 4 07:58 main.d -rw-r--r--. 1 root root 116440 Feb 4 07:58 main.o drwxr-xr-x. 2 root root 26 Feb 4 07:57 mocks/ -rw-r--r--. 1 root root 3233 Feb 4 07:27 test_obj.cpp -rw-r--r--. 1 root root 1850 Feb 4 07:58 test_obj.d -rw-r--r--. 1 root root 1479800 Feb 4 07:58 test_obj.o -rwxr-xr-x. 1 root root 844208 Feb 4 07:58 ut* root@centos:/home/xxx/cpp-stub/ut_examples/gtest/ut/src# gdb ut core.90826 GNU gdb (Ubuntu 8.1.1-0ubuntu1) 8.1.1 Copyright (C) 2018 Free Software Foundation, Inc. License GPLv3+: GNU GPL version 3 or later http://gnu.org/licenses/gpl.html This is free software: you are free to change and redistribute it. There is NO WARRANTY, to the extent permitted by law. Type "show copying" and "show warranty" for details. This GDB was configured as "aarch64-linux-gnu". Type "show configuration" for configuration details. For bug reporting instructions, please see: http://www.gnu.org/software/gdb/bugs/. Find the GDB manual and other documentation resources online at: http://www.gnu.org/software/gdb/documentation/. For help, type "help". Type "apropos word" to search for commands related to "word"... Reading symbols from ut...done. [New LWP 90826] [Thread debugging using libthread_db enabled] Using host libthread_db library "/lib/aarch64-linux-gnu/libthread_db.so.1". Core was generated by `./ut --gtest_filter=get_number_test.returns_correct_value'. Program terminated with signal SIGSEGV, Segmentation fault. #0 0x000000000040cf88 in Stub::set<int (Obj::)(), int ()(void*)> (this=0xffffdb3e9b10, addr=&virtual Obj::get_number(), addr_stub=0x40a17c <get_number_stub(void*)>) at ../../../../src/stub.h:305

warning: Source file is more recent than executable. 305 std::memcpy(pstub->code_buf, fn, CODESIZE_MIN); (gdb) bt #0 0x000000000040cf88 in Stub::set<int (Obj::)(), int ()(void*)> (this=0xffffdb3e9b10, addr=&virtual Obj::get_number(), addr_stub=0x40a17c <get_number_stub(void*)>) at ../../../../src/stub.h:305 #1 0x000000000040a1cc in get_number_test_returns_correct_value_Test::TestBody (this=0x1b103450) at test_obj.cpp:75 #2 0x0000ffff7f434548 in void testing::internal::HandleSehExceptionsInMethodIfSupported<testing::Test, void>(testing::Test*, void (testing::Test::)(), char const) () from /usr/local/gtest/lib/libgtest.so #3 0x0000ffff7f42dbd8 in void testing::internal::HandleExceptionsInMethodIfSupported<testing::Test, void>(testing::Test*, void (testing::Test::)(), char const) () from /usr/local/gtest/lib/libgtest.so #4 0x0000ffff7f40c050 in testing::Test::Run() () from /usr/local/gtest/lib/libgtest.so #5 0x0000ffff7f40c93c in testing::TestInfo::Run() () from /usr/local/gtest/lib/libgtest.so #6 0x0000ffff7f40cff0 in testing::TestCase::Run() () from /usr/local/gtest/lib/libgtest.so #7 0x0000ffff7f417160 in testing::internal::UnitTestImpl::RunAllTests() () from /usr/local/gtest/lib/libgtest.so #8 0x0000ffff7f4357cc in bool testing::internal::HandleSehExceptionsInMethodIfSupported<testing::internal::UnitTestImpl, bool>(testing::internal::UnitTestImpl*, bool (testing::internal::UnitTestImpl::)(), char const) () from /usr/local/gtest/lib/libgtest.so #9 0x0000ffff7f42eb10 in bool testing::internal::HandleExceptionsInMethodIfSupported<testing::internal::UnitTestImpl, bool>(testing::internal::UnitTestImpl*, bool (testing::internal::UnitTestImpl::)(), char const) () from /usr/local/gtest/lib/libgtest.so #10 0x0000ffff7f415ce0 in testing::UnitTest::Run() () from /usr/local/gtest/lib/libgtest.so #11 0x00000000004182ac in RUN_ALL_TESTS () at ../include/gtest/gtest.h:2341 #12 0x0000000000418218 in main (argc=1, argv=0xffffdb3ea008) at main.cpp:12 (gdb) q root@centos:/home/xxx/cpp-stub/ut_examples/gtest/ut/src# readelf -h ut ELF Header: Magic: 7f 45 4c 46 02 01 01 03 00 00 00 00 00 00 00 00 Class: ELF64 Data: 2's complement, little endian Version: 1 (current) OS/ABI: UNIX - GNU ABI Version: 0 Type: EXEC (Executable file) Machine: AArch64 Version: 0x1 Entry point address: 0x409e70 Start of program headers: 64 (bytes into file) Start of section headers: 841840 (bytes into file) Flags: 0x0 Size of this header: 64 (bytes) Size of program headers: 56 (bytes) Number of program headers: 9 Size of section headers: 64 (bytes) Number of section headers: 37 Section header string table index: 36

KindleKin avatar Feb 06 '23 02:02 KindleKin

-lgtest -lgmock is not arm64 library. You need to compile gtest on the ARM platform

coolxv avatar Feb 06 '23 06:02 coolxv

-lgtest -lgmock is not arm64 library. You need to compile gtest on the ARM platform

yes, I have changed the lib of gtest and gmock, otherwise can't build successfully, the gtest framework work well, the error happend in Stub::set() method

Edmund-Hu avatar Feb 08 '23 06:02 Edmund-Hu

addr=&virtual Obj::get_number()

why is addr virtual?

coolxv avatar Feb 09 '23 00:02 coolxv

addr=&virtual Obj::get_number()

why is addr virtual?

ok,that's a mistake when I debug the gmock funciton. after change it to normal function, the case passed but when i try to stub other kind of funtion, I suffer other segmentation fault an again.

Edmund-Hu avatar Feb 11 '23 03:02 Edmund-Hu

https://github.com/coolxv/cpp-stub/tree/master/test
I test cases passed all on my x86_64 machine, but faild on arm64 root@/home/xxx/cpp-stub/test# ./test_addr_any_linux I am foo foo() => 0x402104 I am foo_stub I am foo I am foo I am foo root@/home/xxx/cpp-stub/test# ./test_addr_lambda_linux foo lambda:3 I am foo foo()::{lambda(int)#1}::operator()(int) const => 0x402104 I am foo_lambda_stub:3 I am foo root@/home/xxx/cpp-stub/test# ./test_class_member_function Segmentation fault (core dumped) root@/home/xxx/cppstub/test# ./test_constructor_function_linux Segmentation fault (core dumped) root@/home/xxx/cppstub/test# ./test_dtor_function_linux Segmentation fault (core dumped) root@/home/xxx/cppstub/test# ./test_dynamic_library_linux I am printf_stub root@/home/xxx/cppstub/test# ./test_function Segmentation fault (core dumped) root@/home/xxx/cppstub/test# ./test_functor_linux Segmentation fault (core dumped) root@/home/xxx/cppstub/test# ./test_object_member_function_linux Segmentation fault (core dumped) root@/home/xxx/cppstub/test# ./test_overload_function_linux Segmentation fault (core dumped) root@/home/xxx/cppstub/test# ./test_private_member_function_linux I am A_foo 1 I am A_bar 10 Segmentation fault (core dumped) root@/home/xxx/cppstub/test# ./test_template_function_linux Segmentation fault (core dumped) root@/home/xxx/cppstub/test# ./test_variadic_function Average of 2, 3, 4, 5 = I am foo 3.5 Segmentation fault (core dumped) root@/home/xxx/cppstub/test# ./test_virtual_function_linux Segmentation fault (core dumped) root@/home/xxx/cppstub/test# ./test_virtual_overload_function_linux Segmentation fault (core dumped)

typical error is info :

Core was generated by `./test_class_member_function'. Program terminated with signal SIGSEGV, Segmentation fault. #0 elf_machine_fixup_plt (map=, t=, refsym=, sym=0x2680010, reloc=, value=281473718517384, reloc_addr=0x414000) at ../sysdeps/aarch64/dl-machine.h:220 220 ../sysdeps/aarch64/dl-machine.h: No such file or directory. (gdb) bt #0 elf_machine_fixup_plt (map=, t=, refsym=, sym=0x2680010, reloc=, value=281473718517384, reloc_addr=0x414000) at ../sysdeps/aarch64/dl-machine.h:220 #1 _dl_fixup (l=, reloc_arg=) at dl-runtime.c:149 #2 0x0000ffffb5172f94 in _dl_runtime_resolve () at ../sysdeps/aarch64/dl-trampoline.S:98 #3 0x0000000000402298 in std::_Rb_tree<char*, std::pair<char* const, func_stub*>, std::_Select1st<std::pair<char* const, func_stub*> >, std::less<char*>, std::allocator<std::pair<char* const, func_stub*> > >::_M_insert_node (this=0xffffffef3e10, __x=0x0, __p=0xffffffef3e18, __z=0x2691ea0) at /usr/include/c++/7/bits/stl_tree.h:2304 #4 0x0000000000401d90 in std::_Rb_tree<char*, std::pair<char* const, func_stub*>, std::_Select1st<std::pair<char* const, func_stub*> >, std::less<char*>, std::allocator<std::pair<char* const, func_stub*> > >::_M_emplace_unique<std::pair<char*, func_stub*> > (this=0xffffffef3e10) at /usr/include/c++/7/bits/stl_tree.h:2358 #5 0x0000000000401bdc in std::map<char*, func_stub*, std::less<char*>, std::allocator<std::pair<char* const, func_stub*> > >::insert<std::pair<char*, func_stub*> > (this=0xffffffef3e10, __x=...) at /usr/include/c++/7/bits/stl_map.h:809 #6 0x000000000040185c in Stub::set<int ()(int), int ()(int)> (this=0xffffffef3e08, addr=0x4013cc <A::foo(int)>, addr_stub=0x400ef4 <foo_stub(int)>) at ../src/stub.h:335 #7 0x0000000000400f64 in main () at test_class_member_function.cpp:23

Core was generated by `./test_constructor_function_linux'. Program terminated with signal SIGSEGV, Segmentation fault. #0 0x0000000000401828 in Stub::set<void*, void*> (this=0xffffcc8ed488, addr=0xffffffffa05825c4, addr_stub=0xffffffffa05825c4) at ../src/stub.h:305 305 std::memcpy(pstub->code_buf, fn, CODESIZE_MIN);

Edmund-Hu avatar Feb 11 '23 03:02 Edmund-Hu