From 45395fb264f11cb0ecd561115cdf82364c24c63a Mon Sep 17 00:00:00 2001 From: Oliver Old Date: Wed, 19 May 2021 17:31:28 +0200 Subject: [PATCH 1/3] Disable vptr checks for Singleton constructor UBSan complains a lot about the this pointer not being of type T* which is correct during construction, but ultimately doesn't matter. This change disables the vptr check on the Singleton constructor to avoid errors from UBSan. --- OgreMain/include/OgreSingleton.h | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/OgreMain/include/OgreSingleton.h b/OgreMain/include/OgreSingleton.h index be07e9c1198..b1183951c77 100644 --- a/OgreMain/include/OgreSingleton.h +++ b/OgreMain/include/OgreSingleton.h @@ -80,6 +80,12 @@ template class Singleton static T* msSingleton; public: +#if defined(__GNUC__) || defined(__clang__) + // The `static_cast` happens so early in the construction of the inheriting + // classes that the `this` pointer is still detected as the super class + // pointer. Therefore, disabling vptr checks. + __attribute__((no_sanitize("vptr"))) +#endif Singleton(void) { OgreAssert(!msSingleton, "There can be only one singleton"); From d022b354972fcaf0dbceb6185c148e09c95f0f07 Mon Sep 17 00:00:00 2001 From: Oliver Old Date: Wed, 19 May 2021 19:30:32 +0200 Subject: [PATCH 2/3] Fix compilation for GCC < 8 --- OgreMain/include/OgreSingleton.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/OgreMain/include/OgreSingleton.h b/OgreMain/include/OgreSingleton.h index b1183951c77..ddd5d6031ee 100644 --- a/OgreMain/include/OgreSingleton.h +++ b/OgreMain/include/OgreSingleton.h @@ -80,7 +80,7 @@ template class Singleton static T* msSingleton; public: -#if defined(__GNUC__) || defined(__clang__) +#if (defined(__has_attribute) && __has_attribute(no_sanitize)) || (defined(__GNUC__) && __GNUC__ >= 8) // The `static_cast` happens so early in the construction of the inheriting // classes that the `this` pointer is still detected as the super class // pointer. Therefore, disabling vptr checks. From 8c416f1f725cf548383d57771766c3070363cd56 Mon Sep 17 00:00:00 2001 From: Oliver Old Date: Wed, 19 May 2021 22:57:52 +0200 Subject: [PATCH 3/3] Fix preprocessor directives --- OgreMain/include/OgreSingleton.h | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/OgreMain/include/OgreSingleton.h b/OgreMain/include/OgreSingleton.h index ddd5d6031ee..66323a15b1b 100644 --- a/OgreMain/include/OgreSingleton.h +++ b/OgreMain/include/OgreSingleton.h @@ -80,11 +80,13 @@ template class Singleton static T* msSingleton; public: -#if (defined(__has_attribute) && __has_attribute(no_sanitize)) || (defined(__GNUC__) && __GNUC__ >= 8) +#if defined(__has_attribute) +# if __has_attribute(no_sanitize) // The `static_cast` happens so early in the construction of the inheriting // classes that the `this` pointer is still detected as the super class // pointer. Therefore, disabling vptr checks. __attribute__((no_sanitize("vptr"))) +# endif #endif Singleton(void) {