Skip to content

Multiple inheritance in interfaces leads to segmentation faults with GMock #74

@mstaz

Description

@mstaz

When mocking an interface that inherits from multiple other interfaces, segmentation fault happens on EXPECT_CALL or call itself.
Example:

struct a
{
	virtual ~a() = default;
	virtual void func_a() = 0;
};
struct b
{
	virtual ~b() = default;
	virtual void func_b() = 0;
};
struct ab : public a, public b
{
	~ab() override = default;
};

auto mock = std::make_shared<testing::StrictGMock<ab>>();
std::shared_ptr<ab> obj = object(mock);
EXPECT_CALL(*mock, (func_a)()); // works as expected
obj->func_a(); // works as expected
EXPECT_CALL(*mock, (func_b)()); // may already crash
obj->func_b(); // latest here crash happens

When mocking or calling a function on the second interface a crash happens. I played around a little bit and observed crashes on EXPECT_CALL as well as on function call itself.
To me it seems that offset calculation for vtable doesn't work reliable for such cases. However unfortunately I couldn't find the exact reason for the issue. It may also depend on the used compiler. I observed the issue with clang-13.

This may be a duplicate of issue #12, however I'm unsure as the description is not very detailed there.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions