Skip to content

Commit

Permalink
Improved UBSAN support for gcc too + added a macro to disable tests t…
Browse files Browse the repository at this point in the history
…hat have to trigger UBSAN.
  • Loading branch information
FranckRJ committed Apr 16, 2023
1 parent f0befa8 commit 04d9638
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 12 deletions.
6 changes: 6 additions & 0 deletions include/mockutils/Macros.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,9 @@
#else
# define FAKEIT_CPLUSPLUS __cplusplus
#endif

#ifdef __GNUG__
# define FAKEIT_DISARM_UBSAN __attribute__((no_sanitize("undefined")))
#else
# define FAKEIT_DISARM_UBSAN
#endif
5 changes: 2 additions & 3 deletions include/mockutils/VTUtils.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

#include <functional>
#include <type_traits>
#include "mockutils/Macros.hpp"
#include "mockutils/VirtualOffestSelector.hpp"
#include "mockutils/union_cast.hpp"

Expand Down Expand Up @@ -37,10 +38,8 @@ namespace fakeit {
#endif

template<typename C>
FAKEIT_DISARM_UBSAN
static typename std::enable_if<std::has_virtual_destructor<C>::value, unsigned int>::type
#if defined(__clang__)
__attribute__((no_sanitize("undefined")))
#endif
getDestructorOffset() {
VirtualOffsetSelector offsetSelctor;
union_cast<C *>(&offsetSelctor)->~C();
Expand Down
4 changes: 4 additions & 0 deletions tests/default_behaviore_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,13 @@ struct DefaultBehavioreTests : tpunit::TestFixture {
TEST(DefaultBehavioreTests::DefaultBeaviorOfVoidFunctionsIsToDoNothing), //
TEST(DefaultBehavioreTests::ReturnByValue_ReturnDefaultConstructedObject), //
TEST(DefaultBehavioreTests::ReturnByValue_ThrowExceptionIfNotDefaultConstructible), //
#ifndef FAKEIT_DISABLE_UBSAN_TRIGGERING_TESTS
TEST(DefaultBehavioreTests::ReturnByReference_ReturnReferenceToNullIfAbstract), //
#endif
TEST(DefaultBehavioreTests::ReturnByReference_ReturnReferenceToDefaultConstructedObject), //
#ifndef FAKEIT_DISABLE_UBSAN_TRIGGERING_TESTS
TEST(DefaultBehavioreTests::ReturnByReference_ReturnReferenceToNullIfNotDefaultConstructible), //
#endif
TEST(DefaultBehavioreTests::ReturnPtr_NullPtrIfPtrToAbstract),
TEST(DefaultBehavioreTests::production_shared_ptr_mock_used_in_invocation)
//TEST(DefaultBehavioreTests::should_survive_delete_of_mock_instance_by_user)
Expand Down
30 changes: 23 additions & 7 deletions tests/referece_types_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -57,30 +57,41 @@ struct ReferenceTypesTests: tpunit::TestFixture {

void implicitStubbingDefaultReturnValues() {
Mock<ReferenceInterface> mock;
#ifndef FAKEIT_DISABLE_UBSAN_TRIGGERING_TESTS
Fake( //
Method(mock,returnIntByRef), //
Method(mock,returnAbstractTypeByRef), //
Method(mock,returnConcreteTypeByRef) //
);
#else
Fake( //
Method(mock,returnIntByRef), //
Method(mock,returnConcreteTypeByRef) //
);
#endif

ReferenceInterface & i = mock.get();

// Fundamental types are initiated to 0.
// Return a reference to the default value.
ASSERT_EQUAL(0, i.returnIntByRef());

// Concrete types types are initiated by default ctor.
// Concrete types are initiated by default ctor.
// Return a reference to the default value.
ASSERT_EQUAL(ConcreteType(), i.returnConcreteTypeByRef());

#ifndef FAKEIT_DISABLE_UBSAN_TRIGGERING_TESTS
// For abstract types return a reference to nullptr.
ASSERT_EQUAL(nullptr, &i.returnAbstractTypeByRef());
#endif
}

void explicitStubbingDefualtReturnValues() {
Mock<ReferenceInterface> mock; //
When(Method(mock,returnIntByRef)).Return(); //
#ifndef FAKEIT_DISABLE_UBSAN_TRIGGERING_TESTS
When(Method(mock,returnAbstractTypeByRef)).Return(); //
#endif
When(Method(mock,returnConcreteTypeByRef)).Return(); //

ReferenceInterface & i = mock.get();
Expand All @@ -89,12 +100,14 @@ struct ReferenceTypesTests: tpunit::TestFixture {
// Return a reference to the default value.
ASSERT_EQUAL(0, i.returnIntByRef());

// Concrete types types are initiated by default ctor.
// Concrete types are initiated by default ctor.
// Return a reference to the default value.
ASSERT_EQUAL(ConcreteType(), i.returnConcreteTypeByRef());

#ifndef FAKEIT_DISABLE_UBSAN_TRIGGERING_TESTS
// For abstract types return a reference to nullptr.
ASSERT_EQUAL(nullptr, &i.returnAbstractTypeByRef());
#endif
}

void explicitStubbingReturnValues() {
Expand All @@ -113,7 +126,7 @@ struct ReferenceTypesTests: tpunit::TestFixture {
// Return a reference to the default value.
ASSERT_EQUAL(1, i.returnIntByRef());

// Concrete types types are initiated by default ctor.
// Concrete types are initiated by default ctor.
// Return a reference to the default value.
ASSERT_EQUAL(&c, &i.returnConcreteTypeByRef());

Expand All @@ -137,7 +150,7 @@ struct ReferenceTypesTests: tpunit::TestFixture {
// Return a reference to the default value.
ASSERT_EQUAL(1, i.returnIntByRef());

// Concrete types types are initiated by default ctor.
// Concrete types are initiated by default ctor.
// Return a reference to the default value.
ASSERT_EQUAL(&c, &i.returnConcreteTypeByRef());

Expand All @@ -161,7 +174,7 @@ struct ReferenceTypesTests: tpunit::TestFixture {
// Return a reference to the default value.
ASSERT_EQUAL(1, i.returnIntByRef());

// Concrete types types are initiated by default ctor.
// Concrete types are initiated by default ctor.
// Return a reference to the default value.
ASSERT_EQUAL(&c, &i.returnConcreteTypeByRef());

Expand All @@ -172,7 +185,9 @@ struct ReferenceTypesTests: tpunit::TestFixture {
void explicitStubbingDefualtReturnValues_with_AlwaysReturn() {
Mock<ReferenceInterface> mock;
When(Method(mock,returnIntByRef)).AlwaysReturn();
#ifndef FAKEIT_DISABLE_UBSAN_TRIGGERING_TESTS
When(Method(mock,returnAbstractTypeByRef)).AlwaysReturn(); //
#endif
When(Method(mock,returnConcreteTypeByRef)).AlwaysReturn(); //

ReferenceInterface & i = mock.get();
Expand All @@ -181,13 +196,14 @@ struct ReferenceTypesTests: tpunit::TestFixture {
// Return a reference to the default value.
ASSERT_EQUAL(0, i.returnIntByRef());

// Concrete types types are initiated by default ctor.
// Concrete types are initiated by default ctor.
// Return a reference to the default value.
ASSERT_EQUAL(ConcreteType(), i.returnConcreteTypeByRef());

#ifndef FAKEIT_DISABLE_UBSAN_TRIGGERING_TESTS
// For abstract types return a reference to nullptr.
ASSERT_EQUAL(nullptr, &i.returnAbstractTypeByRef());
#endif
}

} __ReferenceTypesTests;

2 changes: 0 additions & 2 deletions tests/stubbing_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@
#include <string>
#include <queue>

#include "mockutils/Macros.hpp"

#include "tpunit++.hpp"
#include "fakeit.hpp"

Expand Down

0 comments on commit 04d9638

Please sign in to comment.