From a86a3569411faff2bff8dc08c6677646d91f2f7e Mon Sep 17 00:00:00 2001 From: Steven Moreland Date: Thu, 1 Aug 2019 23:28:34 +0000 Subject: Reland "Binder: support storing interface stability" This reverts commit e9495af181d56a4ba359539e3ff07d092d6d0a51. Relanding original change 5981dcbe4a6701c1776e9ece08aa83fe01655da1 Reason for revert: reland feature Change-Id: I4be143706996ba7ab19a3e1446fece7b5d18f205 --- libs/binder/Parcel.cpp | 71 ++++++++++++++++++++++++++++++++------------------ 1 file changed, 45 insertions(+), 26 deletions(-) (limited to 'libs/binder/Parcel.cpp') diff --git a/libs/binder/Parcel.cpp b/libs/binder/Parcel.cpp index 55374306f3..0b9849ab11 100644 --- a/libs/binder/Parcel.cpp +++ b/libs/binder/Parcel.cpp @@ -164,14 +164,34 @@ static void release_object(const sp& proc, ALOGE("Invalid object type 0x%08x", obj.hdr.type); } -inline static status_t finish_flatten_binder( - const sp& /*binder*/, const flat_binder_object& flat, Parcel* out) +status_t Parcel::finishFlattenBinder( + const sp& binder, const flat_binder_object& flat) { - return out->writeObject(flat, false); + status_t status = writeObject(flat, false); + if (status != OK) return status; + + return writeInt32(internal::Stability::get(binder.get())); +} + +status_t Parcel::finishUnflattenBinder( + const sp& binder, sp* out) const +{ + int32_t stability; + status_t status = readInt32(&stability); + if (status != OK) return status; + + if (!internal::Stability::check(stability, mRequiredStability)) { + return BAD_TYPE; + } + + status = internal::Stability::set(binder.get(), stability); + if (status != OK) return status; + + *out = binder; + return OK; } -static status_t flatten_binder(const sp& /*proc*/, - const sp& binder, Parcel* out) +status_t Parcel::flattenBinder(const sp& binder) { flat_binder_object obj; @@ -209,30 +229,24 @@ static status_t flatten_binder(const sp& /*proc*/, obj.cookie = 0; } - return finish_flatten_binder(binder, obj, out); + return finishFlattenBinder(binder, obj); } -inline static status_t finish_unflatten_binder( - BpBinder* /*proxy*/, const flat_binder_object& /*flat*/, - const Parcel& /*in*/) +status_t Parcel::unflattenBinder(sp* out) const { - return NO_ERROR; -} - -static status_t unflatten_binder(const sp& proc, - const Parcel& in, sp* out) -{ - const flat_binder_object* flat = in.readObject(false); + const flat_binder_object* flat = readObject(false); if (flat) { switch (flat->hdr.type) { - case BINDER_TYPE_BINDER: - *out = reinterpret_cast(flat->cookie); - return finish_unflatten_binder(nullptr, *flat, in); - case BINDER_TYPE_HANDLE: - *out = proc->getStrongProxyForHandle(flat->handle); - return finish_unflatten_binder( - static_cast(out->get()), *flat, in); + case BINDER_TYPE_BINDER: { + sp binder = reinterpret_cast(flat->cookie); + return finishUnflattenBinder(binder, out); + } + case BINDER_TYPE_HANDLE: { + sp binder = + ProcessState::self()->getStrongProxyForHandle(flat->handle); + return finishUnflattenBinder(binder, out); + } } } return BAD_TYPE; @@ -337,6 +351,10 @@ status_t Parcel::setDataCapacity(size_t size) return NO_ERROR; } +void Parcel::setTransactingBinder(const sp& binder) const { + mRequiredStability = internal::Stability::get(binder.get()); +} + status_t Parcel::setData(const uint8_t* buffer, size_t len) { if (len > INT32_MAX) { @@ -1032,7 +1050,7 @@ status_t Parcel::writeString16(const char16_t* str, size_t len) status_t Parcel::writeStrongBinder(const sp& val) { - return flatten_binder(ProcessState::self(), val, this); + return flattenBinder(val); } status_t Parcel::writeStrongBinderVector(const std::vector>& val) @@ -1978,7 +1996,7 @@ status_t Parcel::readStrongBinder(sp* val) const status_t Parcel::readNullableStrongBinder(sp* val) const { - return unflatten_binder(ProcessState::self(), *this, val); + return unflattenBinder(val); } sp Parcel::readStrongBinder() const @@ -2682,9 +2700,10 @@ void Parcel::initState() mObjectsCapacity = 0; mNextObjectHint = 0; mObjectsSorted = false; + mAllowFds = true; mHasFds = false; mFdsKnown = true; - mAllowFds = true; + mRequiredStability = internal::Stability::UNDECLARED; mOwner = nullptr; mOpenAshmemSize = 0; mWorkSourceRequestHeaderPosition = 0; -- cgit v1.2.3-59-g8ed1b From 7b10226a880b51591e192ff2c0415aa6fa50ce6d Mon Sep 17 00:00:00 2001 From: Steven Moreland Date: Thu, 1 Aug 2019 15:48:43 -0700 Subject: Keep Parcel size stable. The size of 32-bit on arm 'Parcel' was accidentally changed. Fix this by making stability 16-bits (there is not enough space in Parcel right now to keep the 32-bit mask without combining some of the bools). This broke a device in internal master with some 32-bit prebuilts. Bug: 137348710 Test: TH Test: boots on hawk Change-Id: Ifd9aa07ae359557ff94b600c1b64bb565be85dc2 --- libs/binder/Parcel.cpp | 3 +++ libs/binder/include/binder/Stability.h | 2 +- 2 files changed, 4 insertions(+), 1 deletion(-) (limited to 'libs/binder/Parcel.cpp') diff --git a/libs/binder/Parcel.cpp b/libs/binder/Parcel.cpp index 0b9849ab11..ba44c66020 100644 --- a/libs/binder/Parcel.cpp +++ b/libs/binder/Parcel.cpp @@ -77,6 +77,9 @@ static size_t pad_size(size_t s) { namespace android { +// many things compile this into prebuilts on the stack +static_assert(sizeof(Parcel) == 60 || sizeof(Parcel) == 120); + static pthread_mutex_t gParcelGlobalAllocSizeLock = PTHREAD_MUTEX_INITIALIZER; static size_t gParcelGlobalAllocSize = 0; static size_t gParcelGlobalAllocCount = 0; diff --git a/libs/binder/include/binder/Stability.h b/libs/binder/include/binder/Stability.h index 77f066748d..487f480d0e 100644 --- a/libs/binder/include/binder/Stability.h +++ b/libs/binder/include/binder/Stability.h @@ -45,7 +45,7 @@ private: // up the efficiency level of a binder object. So, we expose the underlying type. friend ::android::Parcel; - enum Level : int32_t { + enum Level : int16_t { UNDECLARED = 0, VENDOR = 0b000011, -- cgit v1.2.3-59-g8ed1b