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

Commit 9f163e25 authored by Steven Moreland's avatar Steven Moreland Committed by android-build-merger
Browse files

Merge "Set stability for unset interfaces." into stage-aosp-master

am: e0ab6fcc

Change-Id: Ie6265ef70ac78add83d92b461bd0425fc1638f45
parents 834e30da e0ab6fcc
Loading
Loading
Loading
Loading
+4 −2
Original line number Original line Diff line number Diff line
@@ -170,6 +170,8 @@ static void release_object(const sp<ProcessState>& proc,
status_t Parcel::finishFlattenBinder(
status_t Parcel::finishFlattenBinder(
    const sp<IBinder>& binder, const flat_binder_object& flat)
    const sp<IBinder>& binder, const flat_binder_object& flat)
{
{
    internal::Stability::tryMarkCompilationUnit(binder.get());

    status_t status = writeObject(flat, false);
    status_t status = writeObject(flat, false);
    if (status != OK) return status;
    if (status != OK) return status;


@@ -183,11 +185,11 @@ status_t Parcel::finishUnflattenBinder(
    status_t status = readInt32(&stability);
    status_t status = readInt32(&stability);
    if (status != OK) return status;
    if (status != OK) return status;


    if (!internal::Stability::check(stability, mRequiredStability)) {
    if (binder != nullptr && !internal::Stability::check(stability, mRequiredStability)) {
        return BAD_TYPE;
        return BAD_TYPE;
    }
    }


    status = internal::Stability::set(binder.get(), stability);
    status = internal::Stability::set(binder.get(), stability, true /*log*/);
    if (status != OK) return status;
    if (status != OK) return status;


    *out = binder;
    *out = binder;
+17 −17
Original line number Original line Diff line number Diff line
@@ -13,29 +13,26 @@
 * See the License for the specific language governing permissions and
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * limitations under the License.
 */
 */

#include <binder/Stability.h>
#include <binder/Stability.h>


namespace android {
namespace android {
namespace internal {
namespace internal {


void Stability::markCompilationUnit(IBinder* binder) {
void Stability::markCompilationUnit(IBinder* binder) {
#ifdef __ANDROID_VNDK__
    status_t result = set(binder, kLocalStability, true /*log*/);
constexpr Stability::Level kLocalStability = Stability::Level::VENDOR;
#else
constexpr Stability::Level kLocalStability = Stability::Level::SYSTEM;
#endif

    status_t result = set(binder, kLocalStability);
    LOG_ALWAYS_FATAL_IF(result != OK, "Should only mark known object.");
    LOG_ALWAYS_FATAL_IF(result != OK, "Should only mark known object.");
}
}


void Stability::markVintf(IBinder* binder) {
void Stability::markVintf(IBinder* binder) {
    status_t result = set(binder, Level::VINTF);
    status_t result = set(binder, Level::VINTF, true /*log*/);
    LOG_ALWAYS_FATAL_IF(result != OK, "Should only mark known object.");
    LOG_ALWAYS_FATAL_IF(result != OK, "Should only mark known object.");
}
}


status_t Stability::set(IBinder* binder, int32_t stability) {
void Stability::tryMarkCompilationUnit(IBinder* binder) {
    (void) set(binder, kLocalStability, false /*log*/);
}

status_t Stability::set(IBinder* binder, int32_t stability, bool log) {
    Level currentStability = get(binder);
    Level currentStability = get(binder);


    // null binder is always written w/ 'UNDECLARED' stability
    // null binder is always written w/ 'UNDECLARED' stability
@@ -43,23 +40,26 @@ status_t Stability::set(IBinder* binder, int32_t stability) {
        if (stability == UNDECLARED) {
        if (stability == UNDECLARED) {
            return OK;
            return OK;
        } else {
        } else {
            ALOGE("Null binder written with stability %s.", stabilityString(stability).c_str());
            if (log) {
                ALOGE("Null binder written with stability %s.",
                    stabilityString(stability).c_str());
            }
            return BAD_TYPE;
            return BAD_TYPE;
        }
        }
    }
    }


    if (!isDeclaredStability(stability)) {
    if (!isDeclaredStability(stability)) {
        // There are UNDECLARED sets because some binder interfaces don't set their stability, and
        if (log) {
        // then UNDECLARED stability is sent on the other side.
        if (stability != UNDECLARED) {
            ALOGE("Can only set known stability, not %d.", stability);
            ALOGE("Can only set known stability, not %d.", stability);
            return BAD_TYPE;
        }
        }
        return BAD_TYPE;
    }
    }


    if (currentStability != Level::UNDECLARED && currentStability != stability) {
    if (currentStability != Level::UNDECLARED && currentStability != stability) {
        if (log) {
            ALOGE("Interface being set with %s but it is already marked as %s.",
            ALOGE("Interface being set with %s but it is already marked as %s.",
                stabilityString(stability).c_str(), stabilityString(stability).c_str());
                stabilityString(stability).c_str(), stabilityString(stability).c_str());
        }
        return BAD_TYPE;
        return BAD_TYPE;
    }
    }


+9 −1
Original line number Original line Diff line number Diff line
@@ -45,6 +45,8 @@ private:
    // up the efficiency level of a binder object. So, we expose the underlying type.
    // up the efficiency level of a binder object. So, we expose the underlying type.
    friend ::android::Parcel;
    friend ::android::Parcel;


    static void tryMarkCompilationUnit(IBinder* binder);

    enum Level : int16_t {
    enum Level : int16_t {
        UNDECLARED = 0,
        UNDECLARED = 0,


@@ -53,9 +55,15 @@ private:
        VINTF = 0b111111,
        VINTF = 0b111111,
    };
    };


#ifdef __ANDROID_VNDK__
    static constexpr Level kLocalStability = Level::VENDOR;
#else
    static constexpr Level kLocalStability = Level::SYSTEM;
#endif

    // applies stability to binder if stability level is known
    // applies stability to binder if stability level is known
    __attribute__((warn_unused_result))
    __attribute__((warn_unused_result))
    static status_t set(IBinder* binder, int32_t stability);
    static status_t set(IBinder* binder, int32_t stability, bool log);


    static Level get(IBinder* binder);
    static Level get(IBinder* binder);


+13 −24
Original line number Original line Diff line number Diff line
@@ -89,30 +89,16 @@ public:
    }
    }
};
};


void checkNoStabilityServer(const sp<IBinderStabilityTest>& unkemptServer) {
void checkLocalStabilityBinder(const sp<IBinderStabilityTest>& complServer) {
    EXPECT_TRUE(unkemptServer->sendBinder(new BBinder()).isOk());
    // this binder should automatically be set to local stability
    EXPECT_TRUE(unkemptServer->sendBinder(getCompilationUnitStability()).isOk());
    EXPECT_TRUE(complServer->sendBinder(new BBinder()).isOk());
    EXPECT_TRUE(unkemptServer->sendBinder(getVintfStability()).isOk());

    sp<IBinder> out;
    EXPECT_TRUE(unkemptServer->returnNoStabilityBinder(&out).isOk());
    EXPECT_NE(nullptr, out.get());

    EXPECT_TRUE(unkemptServer->returnLocalStabilityBinder(&out).isOk());
    EXPECT_NE(nullptr, out.get());

    EXPECT_TRUE(unkemptServer->returnVintfStabilityBinder(&out).isOk());
    EXPECT_NE(nullptr, out.get());
}

void checkLowStabilityServer(const sp<IBinderStabilityTest>& complServer) {
    EXPECT_FALSE(complServer->sendBinder(new BBinder()).isOk());
    EXPECT_TRUE(complServer->sendBinder(getCompilationUnitStability()).isOk());
    EXPECT_TRUE(complServer->sendBinder(getCompilationUnitStability()).isOk());
    EXPECT_TRUE(complServer->sendBinder(getVintfStability()).isOk());
    EXPECT_TRUE(complServer->sendBinder(getVintfStability()).isOk());


    sp<IBinder> out;
    sp<IBinder> out;
    EXPECT_FALSE(complServer->returnNoStabilityBinder(&out).isOk());
    // should automatically be set to local stability
    EXPECT_EQ(nullptr, out.get());
    EXPECT_TRUE(complServer->returnNoStabilityBinder(&out).isOk());
    EXPECT_NE(nullptr, out.get());


    EXPECT_TRUE(complServer->returnLocalStabilityBinder(&out).isOk());
    EXPECT_TRUE(complServer->returnLocalStabilityBinder(&out).isOk());
    EXPECT_NE(nullptr, out.get());
    EXPECT_NE(nullptr, out.get());
@@ -142,13 +128,15 @@ TEST(BinderStability, LocalNoStabilityServer) {
    // or was written by hand.
    // or was written by hand.
    auto server = BadStabilityTester::getNoStabilityServer();
    auto server = BadStabilityTester::getNoStabilityServer();
    ASSERT_NE(nullptr, IInterface::asBinder(server)->localBinder());
    ASSERT_NE(nullptr, IInterface::asBinder(server)->localBinder());
    checkNoStabilityServer(server);

    // it should be considered to have local stability
    checkLocalStabilityBinder(server);
}
}


TEST(BinderStability, LocalLowStabilityServer) {
TEST(BinderStability, LocalLowStabilityServer) {
    auto server = BadStabilityTester::getCompilationUnitStabilityServer();
    auto server = BadStabilityTester::getCompilationUnitStabilityServer();
    ASSERT_NE(nullptr, IInterface::asBinder(server)->localBinder());
    ASSERT_NE(nullptr, IInterface::asBinder(server)->localBinder());
    checkLowStabilityServer(server);
    checkLocalStabilityBinder(server);
}
}


TEST(BinderStability, LocalHighStabilityServer) {
TEST(BinderStability, LocalHighStabilityServer) {
@@ -164,7 +152,8 @@ TEST(BinderStability, RemoteNoStabilityServer) {
    ASSERT_NE(nullptr, remoteServer.get());
    ASSERT_NE(nullptr, remoteServer.get());
    ASSERT_NE(nullptr, IInterface::asBinder(remoteServer)->remoteBinder());
    ASSERT_NE(nullptr, IInterface::asBinder(remoteServer)->remoteBinder());


    checkNoStabilityServer(remoteServer);
    // it should be considered to have local stability
    checkLocalStabilityBinder(remoteServer);
}
}


TEST(BinderStability, RemoteLowStabilityServer) {
TEST(BinderStability, RemoteLowStabilityServer) {
@@ -174,7 +163,7 @@ TEST(BinderStability, RemoteLowStabilityServer) {
    ASSERT_NE(nullptr, remoteServer.get());
    ASSERT_NE(nullptr, remoteServer.get());
    ASSERT_NE(nullptr, IInterface::asBinder(remoteServer)->remoteBinder());
    ASSERT_NE(nullptr, IInterface::asBinder(remoteServer)->remoteBinder());


    checkLowStabilityServer(remoteServer);
    checkLocalStabilityBinder(remoteServer);
}
}


TEST(BinderStability, RemoteVintfServer) {
TEST(BinderStability, RemoteVintfServer) {