Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit c6746a7c authored by Steven Moreland's avatar Steven Moreland
Browse files

SharedRefBase: detect double-ownership

If the internal SharedRefBase weak_ptr is promotable while it is being
destroyed, this means that the SharedRefBase object is double-owned, and
something else deleted it. Add an explicit log for this case.

Fixes: 194905504
Test: libbinder_ndk_unit_test
Change-Id: Ib6aa09ff8e659e52eebe96c14d127a6e7c186cff
parent 5d4f84df
Loading
Loading
Loading
Loading
+6 −0
Original line number Diff line number Diff line
@@ -55,6 +55,12 @@ class SharedRefBase {
        std::call_once(mFlagThis, [&]() {
            __assert(__FILE__, __LINE__, "SharedRefBase: no ref created during lifetime");
        });

        if (ref() != nullptr) {
            __assert(__FILE__, __LINE__,
                     "SharedRefBase: destructed but still able to lock weak_ptr. Is this object "
                     "double-owned?");
        }
    }

    /**
+11 −0
Original line number Diff line number Diff line
@@ -224,6 +224,17 @@ bool isServiceRunning(const char* serviceName) {
    return true;
}

TEST(NdkBinder, DetectDoubleOwn) {
    auto badService = ndk::SharedRefBase::make<MyBinderNdkUnitTest>();
    EXPECT_DEATH(std::shared_ptr<MyBinderNdkUnitTest>(badService.get()),
                 "Is this object double-owned?");
}

TEST(NdkBinder, DetectNoSharedRefBaseCreated) {
    EXPECT_DEATH(std::make_shared<MyBinderNdkUnitTest>(),
                 "SharedRefBase: no ref created during lifetime");
}

TEST(NdkBinder, GetServiceThatDoesntExist) {
    sp<IFoo> foo = IFoo::getService("asdfghkl;");
    EXPECT_EQ(nullptr, foo.get());