Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

[Bug]: Address Sanitizer Errors #4717

Open
RafaelLaya opened this issue Feb 3, 2025 · 0 comments
Open

[Bug]: Address Sanitizer Errors #4717

RafaelLaya opened this issue Feb 3, 2025 · 0 comments

Comments

@RafaelLaya
Copy link

Describe the issue

It seems like the address sanitizer currently finds a false-positive in gtest regarding validation of vtable pointers. The code is correct but the compiler can't know this when RTTI is disabled. Other parts of the code already seem to work around lack of RTTI through GTEST_HAS_RTTI.

A simple fix or work around would be as described below and disable the sanitizer in the impacted areas of the code.

This is filed as an issue rather than a direct PR since:

  • There may be arguments to instead fix this on the Clang side
  • Since this is a somewhat minor issue then I didn't feel like going through the trouble of figuring out the CLA process described in CONTRIBUTING.md
in gtest.h
   // Deletes self.  We deliberately pick an unusual name for this
   // internal method to avoid clashing with names used in user TESTs.
-  void DeleteSelf_() { delete this; }
+  GTEST_ATTRIBUTE_NO_SANITIZE_VPTR void DeleteSelf_() { delete this; }
 
in gtest-port.h
+#if GTEST_HAS_RTTI && (defined(__clang__) || defined (__GNUC__)) 
+#define GTEST_ATTRIBUTE_NO_SANITIZE_VPTR __attribute__((no_sanitize("vptr")))
+#else
+#define GTEST_ATTRIBUTE_NO_SANITIZE_VPTR
+#endif

in gtest.cc
 // exceptions in the same function.  Therefore, we provide a separate
 // wrapper function for handling SEH exceptions.)
 template <class T, typename Result>
-Result HandleSehExceptionsInMethodIfSupported(T* object, Result (T::*method)(),
+GTEST_ATTRIBUTE_NO_SANITIZE_VPTR Result HandleSehExceptionsInMethodIfSupported(T* object, Result (T::*method)(),
                                               const char* location) {
 
 // Destructs a TestInfo object.
-TestInfo::~TestInfo() { delete factory_; }
+GTEST_ATTRIBUTE_NO_SANITIZE_VPTR TestInfo::~TestInfo() { delete factory_; }
 
-void TestInfo::Run() {
+GTEST_ATTRIBUTE_NO_SANITIZE_VPTR void TestInfo::Run() {
   TestEventListener* repeater = UnitTest::GetInstance()->listeners().repeater();
   if (!should_run_) {
     if (is_disabled_ && matches_filter_) repeater->OnTestDisabled(*this);

Steps to reproduce the problem

Simply create any gtest with the address sanitizer enabled and pass -fno-rtti into the build

What version of GoogleTest are you using?

Exactly 1.14.0

What operating system and version are you using?

Fedora

What compiler and version are you using?

clang version 18.1.8 (Fedora 18.1.8-1.fc40)
Target: x86_64-redhat-linux-gnu
Thread model: posix

What build system are you using?

cmake version 3.30.2

Additional context

No response

# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant