googletest icon indicating copy to clipboard operation
googletest copied to clipboard

UBSan: Invalid vptr when compiling with no-rtti

Open chriselrod opened this issue 2 years ago • 4 comments

Describe the issue

src/googletest/src/gtest.cc:2727:32: runtime error: member call on address 0x55ef35a2f690 which does not point to an object of type 'TestFactoryBase'
0x55ef35a2f690: note: object has invalid vptr
 00 00 00 00  70 7a c7 34 ef 55 00 00  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  51 00 00 00
              ^~~~~~~~~~~~~~~~~~~~~~~
              invalid vptr

Steps to reproduce the problem

Here is a CI run that produced the problem: https://github.com/JuliaSIMD/LoopModels/actions/runs/3849067677/jobs/6557615496

CMakeLists.txt

CPMFindPackage(
  NAME googletest
  GITHUB_REPOSITORY google/googletest
  GIT_TAG release-1.12.1
  VERSION 1.12.1
  OPTIONS "INSTALL_GTEST OFF" "BUILD_GMOCK OFF" "gtest_hide_internal_symbols ON"
)

This failure appears consistently across computers.

What version of GoogleTest are you using?

1.12.1, but I also tried main.

What operating system and version are you using?

Fedora 37, and Ubuntu 22.04 (CI).

What compiler and version are you using?

Problem appears with both.

> gcc -v
Using built-in specs.
COLLECT_GCC=gcc
COLLECT_LTO_WRAPPER=/usr/libexec/gcc/x86_64-redhat-linux/12/lto-wrapper
OFFLOAD_TARGET_NAMES=nvptx-none
OFFLOAD_TARGET_DEFAULT=1
Target: x86_64-redhat-linux
Configured with: ../configure --enable-bootstrap --enable-languages=c,c++,fortran,objc,obj-c++,ada,go,d,lto --prefix=/usr --mandir=/usr/share/man --infodir=/usr/share/info --with-bugurl=http://bugzilla.redhat.com/bugzilla --enable-shared --enable-threads=posix --enable-checking=release --enable-multilib --with-system-zlib --enable-__cxa_atexit --disable-libunwind-exceptions --enable-gnu-unique-object --enable-linker-build-id --with-gcc-major-version-only --enable-libstdcxx-backtrace --with-linker-hash-style=gnu --enable-plugin --enable-initfini-array --with-isl=/builddir/build/BUILD/gcc-12.2.1-20221121/obj-x86_64-redhat-linux/isl-install --enable-offload-targets=nvptx-none --without-cuda-driver --enable-offload-defaulted --enable-gnu-indirect-function --enable-cet --with-tune=generic --with-arch_32=i686 --build=x86_64-redhat-linux --with-build-config=bootstrap-lto --enable-link-serialization=1
Thread model: posix
Supported LTO compression algorithms: zlib zstd
gcc version 12.2.1 20221121 (Red Hat 12.2.1-4) (GCC) 

> clang -v
clang version 15.0.6 (Fedora 15.0.6-2.fc37)
Target: x86_64-redhat-linux-gnu
Thread model: posix
InstalledDir: /usr/bin
Found candidate GCC installation: /usr/bin/../lib/gcc/i686-redhat-linux/12
Found candidate GCC installation: /usr/bin/../lib/gcc/x86_64-redhat-linux/12
Selected GCC installation: /usr/bin/../lib/gcc/x86_64-redhat-linux/12
Candidate multilib: .;@m64
Candidate multilib: 32;@m32
Selected multilib: .;@m64

What build system are you using?

> cmake --version
cmake version 3.25.1

CMake suite maintained and supported by Kitware (kitware.com/cmake).

Additional context

I spent a little time trying to produce a minimal example, but could not reproduce the problem yet.

Note that the error points to this destructor.

chriselrod avatar Jan 05 '23 18:01 chriselrod

Setting UBSAN_OPTIONS='print_stacktrace=1':

/home/chriselrod/.cache/CPM/googletest/a36d6fcbd7356d396ea479e6b11f4b9760ad0c04/googletest/src/gtest.cc:2727:32: runtime error: member call on address 0x000001b19500 which does not point to an object of type 'TestFactoryBase'
0x000001b19500: note: object has invalid vptr
 00 00 00 00  f8 ed 78 00 00 00 00 00  00 00 00 00 00 00 00 00  20 00 00 00 00 00 00 00  21 00 00 00
              ^~~~~~~~~~~~~~~~~~~~~~~
              invalid vptr
    #0 0x6d12e7 in testing::TestInfo::~TestInfo() /home/chriselrod/.cache/CPM/googletest/a36d6fcbd7356d396ea479e6b11f4b9760ad0c04/googletest/src/gtest.cc:2727
    #1 0x70dc6b in Delete<testing::TestInfo> /home/chriselrod/.cache/CPM/googletest/a36d6fcbd7356d396ea479e6b11f4b9760ad0c04/googletest/src/gtest-internal-inl.h:334
    #2 0x756c77 in void (*std::for_each<__gnu_cxx::__normal_iterator<testing::TestInfo* const*, std::vector<testing::TestInfo*, std::allocator<testing::TestInfo*> > >, void (*)(testing::TestInfo*)>(__gnu_cxx::__normal_iterator<testing::TestInfo* const*, std::vector<testing::TestInfo*, std::allocator<testing::TestInfo*> > >, __gnu_cxx::__normal_iterator<testing::TestInfo* const*, std::vector<testing::TestInfo*, std::allocator<testing::TestInfo*> > >, void (*)(testing::TestInfo*)))(testing::TestInfo*) /usr/include/c++/12/bits/stl_algo.h:3787
    #3 0x73ffe7 in void testing::internal::ForEach<std::vector<testing::TestInfo*, std::allocator<testing::TestInfo*> >, void (*)(testing::TestInfo*)>(std::vector<testing::TestInfo*, std::allocator<testing::TestInfo*> > const&, void (*)(testing::TestInfo*)) /home/chriselrod/.cache/CPM/googletest/a36d6fcbd7356d396ea479e6b11f4b9760ad0c04/googletest/src/gtest-internal-inl.h:286
    #4 0x6d4c87 in testing::TestSuite::~TestSuite() /home/chriselrod/.cache/CPM/googletest/a36d6fcbd7356d396ea479e6b11f4b9760ad0c04/googletest/src/gtest.cc:2960
    #5 0x6d4f33 in testing::TestSuite::~TestSuite() /home/chriselrod/.cache/CPM/googletest/a36d6fcbd7356d396ea479e6b11f4b9760ad0c04/googletest/src/gtest.cc:2961
    #6 0x70df17 in Delete<testing::TestSuite> /home/chriselrod/.cache/CPM/googletest/a36d6fcbd7356d396ea479e6b11f4b9760ad0c04/googletest/src/gtest-internal-inl.h:334
    #7 0x74929d in void (*std::for_each<__gnu_cxx::__normal_iterator<testing::TestSuite* const*, std::vector<testing::TestSuite*, std::allocator<testing::TestSuite*> > >, void (*)(testing::TestSuite*)>(__gnu_cxx::__normal_iterator<testing::TestSuite* const*, std::vector<testing::TestSuite*, std::allocator<testing::TestSuite*> > >, __gnu_cxx::__normal_iterator<testing::TestSuite* const*, std::vector<testing::TestSuite*, std::allocator<testing::TestSuite*> > >, void (*)(testing::TestSuite*)))(testing::TestSuite*) /usr/include/c++/12/bits/stl_algo.h:3787
    #8 0x72d249 in void testing::internal::ForEach<std::vector<testing::TestSuite*, std::allocator<testing::TestSuite*> >, void (*)(testing::TestSuite*)>(std::vector<testing::TestSuite*, std::allocator<testing::TestSuite*> > const&, void (*)(testing::TestSuite*)) /home/chriselrod/.cache/CPM/googletest/a36d6fcbd7356d396ea479e6b11f4b9760ad0c04/googletest/src/gtest-internal-inl.h:286
    #9 0x6fd361 in testing::internal::UnitTestImpl::~UnitTestImpl() /home/chriselrod/.cache/CPM/googletest/a36d6fcbd7356d396ea479e6b11f4b9760ad0c04/googletest/src/gtest.cc:5545
    #10 0x6fdf0d in testing::internal::UnitTestImpl::~UnitTestImpl() /home/chriselrod/.cache/CPM/googletest/a36d6fcbd7356d396ea479e6b11f4b9760ad0c04/googletest/src/gtest.cc:5551
    #11 0x6fabe4 in testing::UnitTest::~UnitTest() /home/chriselrod/.cache/CPM/googletest/a36d6fcbd7356d396ea479e6b11f4b9760ad0c04/googletest/src/gtest.cc:5496
    #12 0x7f53dfc620e4 in __run_exit_handlers (/lib64/libc.so.6+0x3f0e4)
    #13 0x7f53dfc6225f in exit (/lib64/libc.so.6+0x3f25f)
    #14 0x7f53dfc4a516 in __libc_start_call_main (/lib64/libc.so.6+0x27516)
    #15 0x7f53dfc4a5c8 in __libc_start_main_alias_1 (/lib64/libc.so.6+0x275c8)
    #16 0x40c564 in _start (/home/chriselrod/Documents/progwork/cxx/LoopModels/buildgcc/test/LoopModelsTests+0x40c564)

chriselrod avatar Jan 08 '23 02:01 chriselrod

Also perhaps worth pointing out that this "runtime error" appears during the build (or linking?) step, and not when actually running the tests.

chriselrod avatar Jan 08 '23 03:01 chriselrod

Ah, it is because I'm using -fno-rtti.

-fsanitize=vptr: Use of an object whose vptr indicates that it is of the wrong dynamic type, or that its lifetime has not begun or has ended. Incompatible with -fno-rtti. Link must be performed by clang++, not clang, to make sure C++-specific parts of the runtime library and C++ standard libraries are present.

https://clang.llvm.org/docs/UndefinedBehaviorSanitizer.html

chriselrod avatar Jan 08 '23 03:01 chriselrod

My workaround is to enable rtti when building tests when ubsan is enabled.

Perhaps we could add an attribute to disable ubsan on the d'tor? https://clang.llvm.org/docs/AttributeReference.html#disable-sanitizer-instrumentation

chriselrod avatar Jan 08 '23 04:01 chriselrod