2 * Copyright (C) 2005 The Android Open Source Project
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 #define LOG_TAG "Parcel"
18 //#define LOG_NDEBUG 0
29 #include <sys/types.h>
30 #include <sys/resource.h>
33 #include <binder/Binder.h>
34 #include <binder/BpBinder.h>
35 #include <binder/IPCThreadState.h>
36 #include <binder/Parcel.h>
37 #include <binder/ProcessState.h>
38 #include <binder/Status.h>
39 #include <binder/TextOutput.h>
40 #include <binder/Value.h>
42 #include <cutils/ashmem.h>
43 #include <utils/Debug.h>
44 #include <utils/Flattenable.h>
45 #include <utils/Log.h>
46 #include <utils/misc.h>
47 #include <utils/String8.h>
48 #include <utils/String16.h>
50 #include <private/binder/binder_module.h>
51 #include <private/binder/Static.h>
54 #define INT32_MAX ((int32_t)(2147483647))
58 //#define LOG_REFS(...) ALOG(LOG_DEBUG, LOG_TAG, __VA_ARGS__)
59 #define LOG_ALLOC(...)
60 //#define LOG_ALLOC(...) ALOG(LOG_DEBUG, LOG_TAG, __VA_ARGS__)
62 // ---------------------------------------------------------------------------
64 // This macro should never be used at runtime, as a too large value
65 // of s could cause an integer overflow. Instead, you should always
66 // use the wrapper function pad_size()
67 #define PAD_SIZE_UNSAFE(s) (((s)+3)&~3)
69 static size_t pad_size(size_t s) {
70 if (s > (SIZE_T_MAX - 3)) {
73 return PAD_SIZE_UNSAFE(s);
76 // Note: must be kept in sync with android/os/StrictMode.java's PENALTY_GATHER
77 #define STRICT_MODE_PENALTY_GATHER (0x40 << 16)
79 // XXX This can be made public if we want to provide
80 // support for typed data.
81 struct small_flat_data
89 static pthread_mutex_t gParcelGlobalAllocSizeLock = PTHREAD_MUTEX_INITIALIZER;
90 static size_t gParcelGlobalAllocSize = 0;
91 static size_t gParcelGlobalAllocCount = 0;
93 static size_t gMaxFds = 0;
95 // Maximum size of a blob to transfer in-place.
96 static const size_t BLOB_INPLACE_LIMIT = 16 * 1024;
100 BLOB_ASHMEM_IMMUTABLE = 1,
101 BLOB_ASHMEM_MUTABLE = 2,
104 void acquire_object(const sp<ProcessState>& proc,
105 const flat_binder_object& obj, const void* who, size_t* outAshmemSize)
108 case BINDER_TYPE_BINDER:
110 LOG_REFS("Parcel %p acquiring reference on local %p", who, obj.cookie);
111 reinterpret_cast<IBinder*>(obj.cookie)->incStrong(who);
114 case BINDER_TYPE_WEAK_BINDER:
116 reinterpret_cast<RefBase::weakref_type*>(obj.binder)->incWeak(who);
118 case BINDER_TYPE_HANDLE: {
119 const sp<IBinder> b = proc->getStrongProxyForHandle(obj.handle);
121 LOG_REFS("Parcel %p acquiring reference on remote %p", who, b.get());
126 case BINDER_TYPE_WEAK_HANDLE: {
127 const wp<IBinder> b = proc->getWeakProxyForHandle(obj.handle);
128 if (b != NULL) b.get_refs()->incWeak(who);
131 case BINDER_TYPE_FD: {
132 if ((obj.cookie != 0) && (outAshmemSize != NULL) && ashmem_valid(obj.handle)) {
133 // If we own an ashmem fd, keep track of how much memory it refers to.
134 int size = ashmem_get_size_region(obj.handle);
136 *outAshmemSize += size;
143 ALOGD("Invalid object type 0x%08x", obj.type);
146 void acquire_object(const sp<ProcessState>& proc,
147 const flat_binder_object& obj, const void* who)
149 acquire_object(proc, obj, who, NULL);
152 static void release_object(const sp<ProcessState>& proc,
153 const flat_binder_object& obj, const void* who, size_t* outAshmemSize)
156 case BINDER_TYPE_BINDER:
158 LOG_REFS("Parcel %p releasing reference on local %p", who, obj.cookie);
159 reinterpret_cast<IBinder*>(obj.cookie)->decStrong(who);
162 case BINDER_TYPE_WEAK_BINDER:
164 reinterpret_cast<RefBase::weakref_type*>(obj.binder)->decWeak(who);
166 case BINDER_TYPE_HANDLE: {
167 const sp<IBinder> b = proc->getStrongProxyForHandle(obj.handle);
169 LOG_REFS("Parcel %p releasing reference on remote %p", who, b.get());
174 case BINDER_TYPE_WEAK_HANDLE: {
175 const wp<IBinder> b = proc->getWeakProxyForHandle(obj.handle);
176 if (b != NULL) b.get_refs()->decWeak(who);
179 case BINDER_TYPE_FD: {
180 if (obj.cookie != 0) { // owned
181 if ((outAshmemSize != NULL) && ashmem_valid(obj.handle)) {
182 int size = ashmem_get_size_region(obj.handle);
184 *outAshmemSize -= size;
194 ALOGE("Invalid object type 0x%08x", obj.type);
197 void release_object(const sp<ProcessState>& proc,
198 const flat_binder_object& obj, const void* who)
200 release_object(proc, obj, who, NULL);
203 inline static status_t finish_flatten_binder(
204 const sp<IBinder>& /*binder*/, const flat_binder_object& flat, Parcel* out)
206 return out->writeObject(flat, false);
209 status_t flatten_binder(const sp<ProcessState>& /*proc*/,
210 const sp<IBinder>& binder, Parcel* out)
212 flat_binder_object obj;
214 if (IPCThreadState::self()->backgroundSchedulingDisabled()) {
215 /* minimum priority for all nodes is nice 0 */
216 obj.flags = FLAT_BINDER_FLAG_ACCEPTS_FDS;
218 /* minimum priority for all nodes is MAX_NICE(19) */
219 obj.flags = 0x13 | FLAT_BINDER_FLAG_ACCEPTS_FDS;
222 if (binder != NULL) {
223 IBinder *local = binder->localBinder();
225 BpBinder *proxy = binder->remoteBinder();
229 const int32_t handle = proxy ? proxy->handle() : 0;
230 obj.type = BINDER_TYPE_HANDLE;
231 obj.binder = 0; /* Don't pass uninitialized stack data to a remote process */
235 obj.type = BINDER_TYPE_BINDER;
236 obj.binder = reinterpret_cast<uintptr_t>(local->getWeakRefs());
237 obj.cookie = reinterpret_cast<uintptr_t>(local);
240 obj.type = BINDER_TYPE_BINDER;
245 return finish_flatten_binder(binder, obj, out);
248 status_t flatten_binder(const sp<ProcessState>& /*proc*/,
249 const wp<IBinder>& binder, Parcel* out)
251 flat_binder_object obj;
253 obj.flags = 0x7f | FLAT_BINDER_FLAG_ACCEPTS_FDS;
254 if (binder != NULL) {
255 sp<IBinder> real = binder.promote();
257 IBinder *local = real->localBinder();
259 BpBinder *proxy = real->remoteBinder();
263 const int32_t handle = proxy ? proxy->handle() : 0;
264 obj.type = BINDER_TYPE_WEAK_HANDLE;
265 obj.binder = 0; /* Don't pass uninitialized stack data to a remote process */
269 obj.type = BINDER_TYPE_WEAK_BINDER;
270 obj.binder = reinterpret_cast<uintptr_t>(binder.get_refs());
271 obj.cookie = reinterpret_cast<uintptr_t>(binder.unsafe_get());
273 return finish_flatten_binder(real, obj, out);
276 // XXX How to deal? In order to flatten the given binder,
277 // we need to probe it for information, which requires a primary
278 // reference... but we don't have one.
280 // The OpenBinder implementation uses a dynamic_cast<> here,
281 // but we can't do that with the different reference counting
282 // implementation we are using.
283 ALOGE("Unable to unflatten Binder weak reference!");
284 obj.type = BINDER_TYPE_BINDER;
287 return finish_flatten_binder(NULL, obj, out);
290 obj.type = BINDER_TYPE_BINDER;
293 return finish_flatten_binder(NULL, obj, out);
297 inline static status_t finish_unflatten_binder(
298 BpBinder* /*proxy*/, const flat_binder_object& /*flat*/,
299 const Parcel& /*in*/)
304 status_t unflatten_binder(const sp<ProcessState>& proc,
305 const Parcel& in, sp<IBinder>* out)
307 const flat_binder_object* flat = in.readObject(false);
310 switch (flat->type) {
311 case BINDER_TYPE_BINDER:
312 *out = reinterpret_cast<IBinder*>(flat->cookie);
313 return finish_unflatten_binder(NULL, *flat, in);
314 case BINDER_TYPE_HANDLE:
315 *out = proc->getStrongProxyForHandle(flat->handle);
316 return finish_unflatten_binder(
317 static_cast<BpBinder*>(out->get()), *flat, in);
323 status_t unflatten_binder(const sp<ProcessState>& proc,
324 const Parcel& in, wp<IBinder>* out)
326 const flat_binder_object* flat = in.readObject(false);
329 switch (flat->type) {
330 case BINDER_TYPE_BINDER:
331 *out = reinterpret_cast<IBinder*>(flat->cookie);
332 return finish_unflatten_binder(NULL, *flat, in);
333 case BINDER_TYPE_WEAK_BINDER:
334 if (flat->binder != 0) {
335 out->set_object_and_refs(
336 reinterpret_cast<IBinder*>(flat->cookie),
337 reinterpret_cast<RefBase::weakref_type*>(flat->binder));
341 return finish_unflatten_binder(NULL, *flat, in);
342 case BINDER_TYPE_HANDLE:
343 case BINDER_TYPE_WEAK_HANDLE:
344 *out = proc->getWeakProxyForHandle(flat->handle);
345 return finish_unflatten_binder(
346 static_cast<BpBinder*>(out->unsafe_get()), *flat, in);
352 // ---------------------------------------------------------------------------
356 LOG_ALLOC("Parcel %p: constructing", this);
363 LOG_ALLOC("Parcel %p: destroyed", this);
366 size_t Parcel::getGlobalAllocSize() {
367 pthread_mutex_lock(&gParcelGlobalAllocSizeLock);
368 size_t size = gParcelGlobalAllocSize;
369 pthread_mutex_unlock(&gParcelGlobalAllocSizeLock);
373 size_t Parcel::getGlobalAllocCount() {
374 pthread_mutex_lock(&gParcelGlobalAllocSizeLock);
375 size_t count = gParcelGlobalAllocCount;
376 pthread_mutex_unlock(&gParcelGlobalAllocSizeLock);
380 const uint8_t* Parcel::data() const
385 size_t Parcel::dataSize() const
387 return (mDataSize > mDataPos ? mDataSize : mDataPos);
390 size_t Parcel::dataAvail() const
392 size_t result = dataSize() - dataPosition();
393 if (result > INT32_MAX) {
399 size_t Parcel::dataPosition() const
404 size_t Parcel::dataCapacity() const
406 return mDataCapacity;
409 status_t Parcel::setDataSize(size_t size)
411 if (size > INT32_MAX) {
412 // don't accept size_t values which may have come from an
413 // inadvertent conversion from a negative int.
418 err = continueWrite(size);
419 if (err == NO_ERROR) {
421 ALOGV("setDataSize Setting data size of %p to %zu", this, mDataSize);
426 void Parcel::setDataPosition(size_t pos) const
428 if (pos > INT32_MAX) {
429 // don't accept size_t values which may have come from an
430 // inadvertent conversion from a negative int.
438 status_t Parcel::setDataCapacity(size_t size)
440 if (size > INT32_MAX) {
441 // don't accept size_t values which may have come from an
442 // inadvertent conversion from a negative int.
446 if (size > mDataCapacity) return continueWrite(size);
450 status_t Parcel::setData(const uint8_t* buffer, size_t len)
452 if (len > INT32_MAX) {
453 // don't accept size_t values which may have come from an
454 // inadvertent conversion from a negative int.
458 status_t err = restartWrite(len);
459 if (err == NO_ERROR) {
460 memcpy(const_cast<uint8_t*>(data()), buffer, len);
467 status_t Parcel::appendFrom(const Parcel *parcel, size_t offset, size_t len)
469 const sp<ProcessState> proc(ProcessState::self());
471 const uint8_t *data = parcel->mData;
472 const binder_size_t *objects = parcel->mObjects;
473 size_t size = parcel->mObjectsSize;
474 int startPos = mDataPos;
475 int firstIndex = -1, lastIndex = -2;
481 if (len > INT32_MAX) {
482 // don't accept size_t values which may have come from an
483 // inadvertent conversion from a negative int.
487 // range checks against the source parcel size
488 if ((offset > parcel->mDataSize)
489 || (len > parcel->mDataSize)
490 || (offset + len > parcel->mDataSize)) {
494 // Count objects in range
495 for (int i = 0; i < (int) size; i++) {
496 size_t off = objects[i];
497 if ((off >= offset) && (off + sizeof(flat_binder_object) <= offset + len)) {
498 if (firstIndex == -1) {
504 int numObjects = lastIndex - firstIndex + 1;
506 if ((mDataSize+len) > mDataCapacity) {
509 if (err != NO_ERROR) {
515 memcpy(mData + mDataPos, data + offset, len);
521 if (numObjects > 0) {
523 if (mObjectsCapacity < mObjectsSize + numObjects) {
524 size_t newSize = ((mObjectsSize + numObjects)*3)/2;
525 if (newSize*sizeof(binder_size_t) < mObjectsSize) return NO_MEMORY; // overflow
526 binder_size_t *objects =
527 (binder_size_t*)realloc(mObjects, newSize*sizeof(binder_size_t));
528 if (objects == (binder_size_t*)0) {
532 mObjectsCapacity = newSize;
535 // append and acquire objects
536 int idx = mObjectsSize;
537 for (int i = firstIndex; i <= lastIndex; i++) {
538 size_t off = objects[i] - offset + startPos;
539 mObjects[idx++] = off;
542 flat_binder_object* flat
543 = reinterpret_cast<flat_binder_object*>(mData + off);
544 acquire_object(proc, *flat, this, &mOpenAshmemSize);
546 if (flat->type == BINDER_TYPE_FD) {
547 // If this is a file descriptor, we need to dup it so the
548 // new Parcel now owns its own fd, and can declare that we
549 // officially know we have fds.
550 flat->handle = fcntl(flat->handle, F_DUPFD_CLOEXEC, 0);
552 mHasFds = mFdsKnown = true;
554 err = FDS_NOT_ALLOWED;
563 int Parcel::compareData(const Parcel& other) {
564 size_t size = dataSize();
565 if (size != other.dataSize()) {
566 return size < other.dataSize() ? -1 : 1;
568 return memcmp(data(), other.data(), size);
571 bool Parcel::allowFds() const
576 bool Parcel::pushAllowFds(bool allowFds)
578 const bool origValue = mAllowFds;
585 void Parcel::restoreAllowFds(bool lastValue)
587 mAllowFds = lastValue;
590 bool Parcel::hasFileDescriptors() const
598 // Write RPC headers. (previously just the interface token)
599 status_t Parcel::writeInterfaceToken(const String16& interface)
601 writeInt32(IPCThreadState::self()->getStrictModePolicy() |
602 STRICT_MODE_PENALTY_GATHER);
603 // currently the interface identification token is just its name as a string
604 return writeString16(interface);
607 bool Parcel::checkInterface(IBinder* binder) const
609 return enforceInterface(binder->getInterfaceDescriptor());
612 bool Parcel::enforceInterface(const String16& interface,
613 IPCThreadState* threadState) const
615 int32_t strictPolicy = readInt32();
616 if (threadState == NULL) {
617 threadState = IPCThreadState::self();
619 if ((threadState->getLastTransactionBinderFlags() &
620 IBinder::FLAG_ONEWAY) != 0) {
621 // For one-way calls, the callee is running entirely
622 // disconnected from the caller, so disable StrictMode entirely.
623 // Not only does disk/network usage not impact the caller, but
624 // there's no way to commuicate back any violations anyway.
625 threadState->setStrictModePolicy(0);
627 threadState->setStrictModePolicy(strictPolicy);
629 const String16 str(readString16());
630 if (str == interface) {
633 ALOGW("**** enforceInterface() expected '%s' but read '%s'",
634 String8(interface).string(), String8(str).string());
639 const binder_size_t* Parcel::objects() const
644 size_t Parcel::objectsCount() const
649 status_t Parcel::errorCheck() const
654 void Parcel::setError(status_t err)
659 status_t Parcel::finishWrite(size_t len)
661 if (len > INT32_MAX) {
662 // don't accept size_t values which may have come from an
663 // inadvertent conversion from a negative int.
667 //printf("Finish write of %d\n", len);
669 ALOGV("finishWrite Setting data pos of %p to %zu", this, mDataPos);
670 if (mDataPos > mDataSize) {
671 mDataSize = mDataPos;
672 ALOGV("finishWrite Setting data size of %p to %zu", this, mDataSize);
674 //printf("New pos=%d, size=%d\n", mDataPos, mDataSize);
678 status_t Parcel::writeUnpadded(const void* data, size_t len)
680 if (len > INT32_MAX) {
681 // don't accept size_t values which may have come from an
682 // inadvertent conversion from a negative int.
686 size_t end = mDataPos + len;
687 if (end < mDataPos) {
692 if (end <= mDataCapacity) {
694 memcpy(mData+mDataPos, data, len);
695 return finishWrite(len);
698 status_t err = growData(len);
699 if (err == NO_ERROR) goto restart_write;
703 status_t Parcel::write(const void* data, size_t len)
705 if (len > INT32_MAX) {
706 // don't accept size_t values which may have come from an
707 // inadvertent conversion from a negative int.
711 void* const d = writeInplace(len);
713 memcpy(d, data, len);
719 void* Parcel::writeInplace(size_t len)
721 if (len > INT32_MAX) {
722 // don't accept size_t values which may have come from an
723 // inadvertent conversion from a negative int.
727 const size_t padded = pad_size(len);
729 // sanity check for integer overflow
730 if (mDataPos+padded < mDataPos) {
734 if ((mDataPos+padded) <= mDataCapacity) {
736 //printf("Writing %ld bytes, padded to %ld\n", len, padded);
737 uint8_t* const data = mData+mDataPos;
739 // Need to pad at end?
741 #if BYTE_ORDER == BIG_ENDIAN
742 static const uint32_t mask[4] = {
743 0x00000000, 0xffffff00, 0xffff0000, 0xff000000
746 #if BYTE_ORDER == LITTLE_ENDIAN
747 static const uint32_t mask[4] = {
748 0x00000000, 0x00ffffff, 0x0000ffff, 0x000000ff
751 //printf("Applying pad mask: %p to %p\n", (void*)mask[padded-len],
752 // *reinterpret_cast<void**>(data+padded-4));
753 *reinterpret_cast<uint32_t*>(data+padded-4) &= mask[padded-len];
760 status_t err = growData(padded);
761 if (err == NO_ERROR) goto restart_write;
765 status_t Parcel::writeUtf8AsUtf16(const std::string& str) {
766 const uint8_t* strData = (uint8_t*)str.data();
767 const size_t strLen= str.length();
768 const ssize_t utf16Len = utf8_to_utf16_length(strData, strLen);
769 if (utf16Len < 0 || utf16Len > std::numeric_limits<int32_t>::max()) {
773 status_t err = writeInt32(utf16Len);
778 // Allocate enough bytes to hold our converted string and its terminating NULL.
779 void* dst = writeInplace((utf16Len + 1) * sizeof(char16_t));
784 utf8_to_utf16(strData, strLen, (char16_t*)dst, (size_t) utf16Len + 1);
789 status_t Parcel::writeUtf8AsUtf16(const std::unique_ptr<std::string>& str) {
791 return writeInt32(-1);
793 return writeUtf8AsUtf16(*str);
799 status_t writeByteVectorInternal(Parcel* parcel, const std::vector<T>& val)
802 if (val.size() > std::numeric_limits<int32_t>::max()) {
807 status = parcel->writeInt32(val.size());
812 void* data = parcel->writeInplace(val.size());
818 memcpy(data, val.data(), val.size());
823 status_t writeByteVectorInternalPtr(Parcel* parcel,
824 const std::unique_ptr<std::vector<T>>& val)
827 return parcel->writeInt32(-1);
830 return writeByteVectorInternal(parcel, *val);
835 status_t Parcel::writeByteVector(const std::vector<int8_t>& val) {
836 return writeByteVectorInternal(this, val);
839 status_t Parcel::writeByteVector(const std::unique_ptr<std::vector<int8_t>>& val)
841 return writeByteVectorInternalPtr(this, val);
844 status_t Parcel::writeByteVector(const std::vector<uint8_t>& val) {
845 return writeByteVectorInternal(this, val);
848 status_t Parcel::writeByteVector(const std::unique_ptr<std::vector<uint8_t>>& val)
850 return writeByteVectorInternalPtr(this, val);
853 status_t Parcel::writeInt32Vector(const std::vector<int32_t>& val)
855 return writeTypedVector(val, &Parcel::writeInt32);
858 status_t Parcel::writeInt32Vector(const std::unique_ptr<std::vector<int32_t>>& val)
860 return writeNullableTypedVector(val, &Parcel::writeInt32);
863 status_t Parcel::writeInt64Vector(const std::vector<int64_t>& val)
865 return writeTypedVector(val, &Parcel::writeInt64);
868 status_t Parcel::writeInt64Vector(const std::unique_ptr<std::vector<int64_t>>& val)
870 return writeNullableTypedVector(val, &Parcel::writeInt64);
873 status_t Parcel::writeFloatVector(const std::vector<float>& val)
875 return writeTypedVector(val, &Parcel::writeFloat);
878 status_t Parcel::writeFloatVector(const std::unique_ptr<std::vector<float>>& val)
880 return writeNullableTypedVector(val, &Parcel::writeFloat);
883 status_t Parcel::writeDoubleVector(const std::vector<double>& val)
885 return writeTypedVector(val, &Parcel::writeDouble);
888 status_t Parcel::writeDoubleVector(const std::unique_ptr<std::vector<double>>& val)
890 return writeNullableTypedVector(val, &Parcel::writeDouble);
893 status_t Parcel::writeBoolVector(const std::vector<bool>& val)
895 return writeTypedVector(val, &Parcel::writeBool);
898 status_t Parcel::writeBoolVector(const std::unique_ptr<std::vector<bool>>& val)
900 return writeNullableTypedVector(val, &Parcel::writeBool);
903 status_t Parcel::writeCharVector(const std::vector<char16_t>& val)
905 return writeTypedVector(val, &Parcel::writeChar);
908 status_t Parcel::writeCharVector(const std::unique_ptr<std::vector<char16_t>>& val)
910 return writeNullableTypedVector(val, &Parcel::writeChar);
913 status_t Parcel::writeString16Vector(const std::vector<String16>& val)
915 return writeTypedVector(val, &Parcel::writeString16);
918 status_t Parcel::writeString16Vector(
919 const std::unique_ptr<std::vector<std::unique_ptr<String16>>>& val)
921 return writeNullableTypedVector(val, &Parcel::writeString16);
924 status_t Parcel::writeUtf8VectorAsUtf16Vector(
925 const std::unique_ptr<std::vector<std::unique_ptr<std::string>>>& val) {
926 return writeNullableTypedVector(val, &Parcel::writeUtf8AsUtf16);
929 status_t Parcel::writeUtf8VectorAsUtf16Vector(const std::vector<std::string>& val) {
930 return writeTypedVector(val, &Parcel::writeUtf8AsUtf16);
933 status_t Parcel::writeInt32(int32_t val)
935 return writeAligned(val);
938 status_t Parcel::writeUint32(uint32_t val)
940 return writeAligned(val);
943 status_t Parcel::writeInt32Array(size_t len, const int32_t *val) {
944 if (len > INT32_MAX) {
945 // don't accept size_t values which may have come from an
946 // inadvertent conversion from a negative int.
951 return writeInt32(-1);
953 status_t ret = writeInt32(static_cast<uint32_t>(len));
954 if (ret == NO_ERROR) {
955 ret = write(val, len * sizeof(*val));
959 status_t Parcel::writeByteArray(size_t len, const uint8_t *val) {
960 if (len > INT32_MAX) {
961 // don't accept size_t values which may have come from an
962 // inadvertent conversion from a negative int.
967 return writeInt32(-1);
969 status_t ret = writeInt32(static_cast<uint32_t>(len));
970 if (ret == NO_ERROR) {
971 ret = write(val, len * sizeof(*val));
976 status_t Parcel::writeBool(bool val)
978 return writeInt32(int32_t(val));
981 status_t Parcel::writeChar(char16_t val)
983 return writeInt32(int32_t(val));
986 status_t Parcel::writeByte(int8_t val)
988 return writeInt32(int32_t(val));
991 status_t Parcel::writeInt64(int64_t val)
993 return writeAligned(val);
996 status_t Parcel::writeUint64(uint64_t val)
998 return writeAligned(val);
1001 status_t Parcel::writePointer(uintptr_t val)
1003 return writeAligned<binder_uintptr_t>(val);
1006 status_t Parcel::writeFloat(float val)
1008 return writeAligned(val);
1011 #if defined(__mips__) && defined(__mips_hard_float)
1013 status_t Parcel::writeDouble(double val)
1017 unsigned long long ll;
1020 return writeAligned(u.ll);
1025 status_t Parcel::writeDouble(double val)
1027 return writeAligned(val);
1032 status_t Parcel::writeCString(const char* str)
1034 return write(str, strlen(str)+1);
1037 status_t Parcel::writeString8(const String8& str)
1039 status_t err = writeInt32(str.bytes());
1040 // only write string if its length is more than zero characters,
1041 // as readString8 will only read if the length field is non-zero.
1042 // this is slightly different from how writeString16 works.
1043 if (str.bytes() > 0 && err == NO_ERROR) {
1044 err = write(str.string(), str.bytes()+1);
1049 status_t Parcel::writeString16(const std::unique_ptr<String16>& str)
1052 return writeInt32(-1);
1055 return writeString16(*str);
1058 status_t Parcel::writeString16(const String16& str)
1060 return writeString16(str.string(), str.size());
1063 status_t Parcel::writeString16(const char16_t* str, size_t len)
1065 if (str == NULL) return writeInt32(-1);
1067 status_t err = writeInt32(len);
1068 if (err == NO_ERROR) {
1069 len *= sizeof(char16_t);
1070 uint8_t* data = (uint8_t*)writeInplace(len+sizeof(char16_t));
1072 memcpy(data, str, len);
1073 *reinterpret_cast<char16_t*>(data+len) = 0;
1081 status_t Parcel::writeStrongBinder(const sp<IBinder>& val)
1083 return flatten_binder(ProcessState::self(), val, this);
1086 status_t Parcel::writeStrongBinderVector(const std::vector<sp<IBinder>>& val)
1088 return writeTypedVector(val, &Parcel::writeStrongBinder);
1091 status_t Parcel::writeStrongBinderVector(const std::unique_ptr<std::vector<sp<IBinder>>>& val)
1093 return writeNullableTypedVector(val, &Parcel::writeStrongBinder);
1096 status_t Parcel::readStrongBinderVector(std::unique_ptr<std::vector<sp<IBinder>>>* val) const {
1097 return readNullableTypedVector(val, &Parcel::readNullableStrongBinder);
1100 status_t Parcel::readStrongBinderVector(std::vector<sp<IBinder>>* val) const {
1101 return readTypedVector(val, &Parcel::readStrongBinder);
1104 status_t Parcel::writeWeakBinder(const wp<IBinder>& val)
1106 return flatten_binder(ProcessState::self(), val, this);
1109 status_t Parcel::writeRawNullableParcelable(const Parcelable* parcelable) {
1111 return writeInt32(0);
1114 return writeParcelable(*parcelable);
1117 status_t Parcel::writeParcelable(const Parcelable& parcelable) {
1118 status_t status = writeInt32(1); // parcelable is not null.
1122 return parcelable.writeToParcel(this);
1125 status_t Parcel::writeValue(const binder::Value& value) {
1126 return value.writeToParcel(this);
1129 status_t Parcel::writeNativeHandle(const native_handle* handle)
1131 if (!handle || handle->version != sizeof(native_handle))
1135 err = writeInt32(handle->numFds);
1136 if (err != NO_ERROR) return err;
1138 err = writeInt32(handle->numInts);
1139 if (err != NO_ERROR) return err;
1141 for (int i=0 ; err==NO_ERROR && i<handle->numFds ; i++)
1142 err = writeDupFileDescriptor(handle->data[i]);
1144 if (err != NO_ERROR) {
1145 ALOGD("write native handle, write dup fd failed");
1148 err = write(handle->data + handle->numFds, sizeof(int)*handle->numInts);
1152 status_t Parcel::writeFileDescriptor(int fd, bool takeOwnership)
1154 flat_binder_object obj;
1155 obj.type = BINDER_TYPE_FD;
1156 obj.flags = 0x7f | FLAT_BINDER_FLAG_ACCEPTS_FDS;
1157 obj.binder = 0; /* Don't pass uninitialized stack data to a remote process */
1159 obj.cookie = takeOwnership ? 1 : 0;
1160 return writeObject(obj, true);
1163 status_t Parcel::writeDupFileDescriptor(int fd)
1165 int dupFd = fcntl(fd, F_DUPFD_CLOEXEC, 0);
1169 status_t err = writeFileDescriptor(dupFd, true /*takeOwnership*/);
1176 status_t Parcel::writeParcelFileDescriptor(int fd, bool takeOwnership)
1179 return writeFileDescriptor(fd, takeOwnership);
1182 status_t Parcel::writeUniqueFileDescriptor(const base::unique_fd& fd) {
1183 return writeDupFileDescriptor(fd.get());
1186 status_t Parcel::writeUniqueFileDescriptorVector(const std::vector<base::unique_fd>& val) {
1187 return writeTypedVector(val, &Parcel::writeUniqueFileDescriptor);
1190 status_t Parcel::writeUniqueFileDescriptorVector(const std::unique_ptr<std::vector<base::unique_fd>>& val) {
1191 return writeNullableTypedVector(val, &Parcel::writeUniqueFileDescriptor);
1194 status_t Parcel::writeBlob(size_t len, bool mutableCopy, WritableBlob* outBlob)
1196 if (len > INT32_MAX) {
1197 // don't accept size_t values which may have come from an
1198 // inadvertent conversion from a negative int.
1203 if (!mAllowFds || len <= BLOB_INPLACE_LIMIT) {
1204 ALOGV("writeBlob: write in place");
1205 status = writeInt32(BLOB_INPLACE);
1206 if (status) return status;
1208 void* ptr = writeInplace(len);
1209 if (!ptr) return NO_MEMORY;
1211 outBlob->init(-1, ptr, len, false);
1215 ALOGV("writeBlob: write to ashmem");
1216 int fd = ashmem_create_region("Parcel Blob", len);
1217 if (fd < 0) return NO_MEMORY;
1219 int result = ashmem_set_prot_region(fd, PROT_READ | PROT_WRITE);
1223 void* ptr = ::mmap(NULL, len, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
1224 if (ptr == MAP_FAILED) {
1228 result = ashmem_set_prot_region(fd, PROT_READ);
1233 status = writeInt32(mutableCopy ? BLOB_ASHMEM_MUTABLE : BLOB_ASHMEM_IMMUTABLE);
1235 status = writeFileDescriptor(fd, true /*takeOwnership*/);
1237 outBlob->init(fd, ptr, len, mutableCopy);
1249 status_t Parcel::writeDupImmutableBlobFileDescriptor(int fd)
1251 // Must match up with what's done in writeBlob.
1252 if (!mAllowFds) return FDS_NOT_ALLOWED;
1253 status_t status = writeInt32(BLOB_ASHMEM_IMMUTABLE);
1254 if (status) return status;
1255 return writeDupFileDescriptor(fd);
1258 status_t Parcel::write(const FlattenableHelperInterface& val)
1263 const size_t len = val.getFlattenedSize();
1264 const size_t fd_count = val.getFdCount();
1266 if ((len > INT32_MAX) || (fd_count >= gMaxFds)) {
1267 // don't accept size_t values which may have come from an
1268 // inadvertent conversion from a negative int.
1272 err = this->writeInt32(len);
1273 if (err) return err;
1275 err = this->writeInt32(fd_count);
1276 if (err) return err;
1279 void* const buf = this->writeInplace(pad_size(len));
1285 fds = new (std::nothrow) int[fd_count];
1286 if (fds == nullptr) {
1287 ALOGE("write: failed to allocate requested %zu fds", fd_count);
1292 err = val.flatten(buf, len, fds, fd_count);
1293 for (size_t i=0 ; i<fd_count && err==NO_ERROR ; i++) {
1294 err = this->writeDupFileDescriptor( fds[i] );
1304 status_t Parcel::writeObject(const flat_binder_object& val, bool nullMetaData)
1306 const bool enoughData = (mDataPos+sizeof(val)) <= mDataCapacity;
1307 const bool enoughObjects = mObjectsSize < mObjectsCapacity;
1308 if (enoughData && enoughObjects) {
1310 *reinterpret_cast<flat_binder_object*>(mData+mDataPos) = val;
1312 // remember if it's a file descriptor
1313 if (val.type == BINDER_TYPE_FD) {
1315 // fail before modifying our object index
1316 return FDS_NOT_ALLOWED;
1318 mHasFds = mFdsKnown = true;
1321 // Need to write meta-data?
1322 if (nullMetaData || val.binder != 0) {
1323 mObjects[mObjectsSize] = mDataPos;
1324 acquire_object(ProcessState::self(), val, this, &mOpenAshmemSize);
1328 return finishWrite(sizeof(flat_binder_object));
1332 const status_t err = growData(sizeof(val));
1333 if (err != NO_ERROR) return err;
1335 if (!enoughObjects) {
1336 size_t newSize = ((mObjectsSize+2)*3)/2;
1337 if (newSize*sizeof(binder_size_t) < mObjectsSize) return NO_MEMORY; // overflow
1338 binder_size_t* objects = (binder_size_t*)realloc(mObjects, newSize*sizeof(binder_size_t));
1339 if (objects == NULL) return NO_MEMORY;
1341 mObjectsCapacity = newSize;
1347 status_t Parcel::writeNoException()
1349 binder::Status status;
1350 return status.writeToParcel(this);
1353 status_t Parcel::writeMap(const ::android::binder::Map& map_in)
1356 using ::android::binder::Value;
1357 using ::android::binder::Map;
1359 Map::const_iterator iter;
1362 ret = writeInt32(map_in.size());
1364 if (ret != NO_ERROR) {
1368 for (iter = map_in.begin(); iter != map_in.end(); ++iter) {
1369 ret = writeValue(Value(iter->first));
1370 if (ret != NO_ERROR) {
1374 ret = writeValue(iter->second);
1375 if (ret != NO_ERROR) {
1383 status_t Parcel::writeNullableMap(const std::unique_ptr<binder::Map>& map)
1386 return writeInt32(-1);
1389 return writeMap(*map.get());
1392 status_t Parcel::readMap(::android::binder::Map* map_out)const
1395 using ::android::String16;
1396 using ::android::String8;
1397 using ::android::binder::Value;
1398 using ::android::binder::Map;
1400 status_t ret = NO_ERROR;
1403 ret = readInt32(&count);
1404 if (ret != NO_ERROR) {
1409 ALOGE("readMap: Unexpected count: %d", count);
1410 return (count == -1)
1421 ret = readValue(&value);
1422 if (ret != NO_ERROR) {
1426 if (!value.getString(&key)) {
1427 ALOGE("readMap: Key type not a string (parcelType = %d)", value.parcelType());
1431 ret = readValue(&value);
1432 if (ret != NO_ERROR) {
1436 (*map_out)[key] = value;
1442 status_t Parcel::readNullableMap(std::unique_ptr<binder::Map>* map) const
1444 const size_t start = dataPosition();
1446 status_t status = readInt32(&count);
1449 if (status != OK || count == -1) {
1453 setDataPosition(start);
1454 map->reset(new binder::Map());
1456 status = readMap(map->get());
1467 void Parcel::remove(size_t /*start*/, size_t /*amt*/)
1469 LOG_ALWAYS_FATAL("Parcel::remove() not yet implemented!");
1472 status_t Parcel::read(void* outData, size_t len) const
1474 if (len > INT32_MAX) {
1475 // don't accept size_t values which may have come from an
1476 // inadvertent conversion from a negative int.
1480 if ((mDataPos+pad_size(len)) >= mDataPos && (mDataPos+pad_size(len)) <= mDataSize
1481 && len <= pad_size(len)) {
1482 memcpy(outData, mData+mDataPos, len);
1483 mDataPos += pad_size(len);
1484 ALOGV("read Setting data pos of %p to %zu", this, mDataPos);
1487 return NOT_ENOUGH_DATA;
1490 const void* Parcel::readInplace(size_t len) const
1492 if (len > INT32_MAX) {
1493 // don't accept size_t values which may have come from an
1494 // inadvertent conversion from a negative int.
1498 if ((mDataPos+pad_size(len)) >= mDataPos && (mDataPos+pad_size(len)) <= mDataSize
1499 && len <= pad_size(len)) {
1500 const void* data = mData+mDataPos;
1501 mDataPos += pad_size(len);
1502 ALOGV("readInplace Setting data pos of %p to %zu", this, mDataPos);
1509 status_t Parcel::readAligned(T *pArg) const {
1510 COMPILE_TIME_ASSERT_FUNCTION_SCOPE(PAD_SIZE_UNSAFE(sizeof(T)) == sizeof(T));
1512 if ((mDataPos+sizeof(T)) <= mDataSize) {
1513 const void* data = mData+mDataPos;
1514 mDataPos += sizeof(T);
1515 *pArg = *reinterpret_cast<const T*>(data);
1518 return NOT_ENOUGH_DATA;
1523 T Parcel::readAligned() const {
1525 if (readAligned(&result) != NO_ERROR) {
1533 status_t Parcel::writeAligned(T val) {
1534 COMPILE_TIME_ASSERT_FUNCTION_SCOPE(PAD_SIZE_UNSAFE(sizeof(T)) == sizeof(T));
1536 if ((mDataPos+sizeof(val)) <= mDataCapacity) {
1538 *reinterpret_cast<T*>(mData+mDataPos) = val;
1539 return finishWrite(sizeof(val));
1542 status_t err = growData(sizeof(val));
1543 if (err == NO_ERROR) goto restart_write;
1549 template<typename T>
1550 status_t readByteVectorInternal(const Parcel* parcel,
1551 std::vector<T>* val) {
1555 status_t status = parcel->readInt32(&size);
1562 status = UNEXPECTED_NULL;
1565 if (size_t(size) > parcel->dataAvail()) {
1570 T* data = const_cast<T*>(reinterpret_cast<const T*>(parcel->readInplace(size)));
1576 val->insert(val->end(), data, data + size);
1581 template<typename T>
1582 status_t readByteVectorInternalPtr(
1583 const Parcel* parcel,
1584 std::unique_ptr<std::vector<T>>* val) {
1585 const int32_t start = parcel->dataPosition();
1587 status_t status = parcel->readInt32(&size);
1590 if (status != OK || size < 0) {
1594 parcel->setDataPosition(start);
1595 val->reset(new (std::nothrow) std::vector<T>());
1597 status = readByteVectorInternal(parcel, val->get());
1608 status_t Parcel::readByteVector(std::vector<int8_t>* val) const {
1609 return readByteVectorInternal(this, val);
1612 status_t Parcel::readByteVector(std::vector<uint8_t>* val) const {
1613 return readByteVectorInternal(this, val);
1616 status_t Parcel::readByteVector(std::unique_ptr<std::vector<int8_t>>* val) const {
1617 return readByteVectorInternalPtr(this, val);
1620 status_t Parcel::readByteVector(std::unique_ptr<std::vector<uint8_t>>* val) const {
1621 return readByteVectorInternalPtr(this, val);
1624 status_t Parcel::readInt32Vector(std::unique_ptr<std::vector<int32_t>>* val) const {
1625 return readNullableTypedVector(val, &Parcel::readInt32);
1628 status_t Parcel::readInt32Vector(std::vector<int32_t>* val) const {
1629 return readTypedVector(val, &Parcel::readInt32);
1632 status_t Parcel::readInt64Vector(std::unique_ptr<std::vector<int64_t>>* val) const {
1633 return readNullableTypedVector(val, &Parcel::readInt64);
1636 status_t Parcel::readInt64Vector(std::vector<int64_t>* val) const {
1637 return readTypedVector(val, &Parcel::readInt64);
1640 status_t Parcel::readFloatVector(std::unique_ptr<std::vector<float>>* val) const {
1641 return readNullableTypedVector(val, &Parcel::readFloat);
1644 status_t Parcel::readFloatVector(std::vector<float>* val) const {
1645 return readTypedVector(val, &Parcel::readFloat);
1648 status_t Parcel::readDoubleVector(std::unique_ptr<std::vector<double>>* val) const {
1649 return readNullableTypedVector(val, &Parcel::readDouble);
1652 status_t Parcel::readDoubleVector(std::vector<double>* val) const {
1653 return readTypedVector(val, &Parcel::readDouble);
1656 status_t Parcel::readBoolVector(std::unique_ptr<std::vector<bool>>* val) const {
1657 const int32_t start = dataPosition();
1659 status_t status = readInt32(&size);
1662 if (status != OK || size < 0) {
1666 setDataPosition(start);
1667 val->reset(new (std::nothrow) std::vector<bool>());
1669 status = readBoolVector(val->get());
1678 status_t Parcel::readBoolVector(std::vector<bool>* val) const {
1680 status_t status = readInt32(&size);
1687 return UNEXPECTED_NULL;
1692 /* C++ bool handling means a vector of bools isn't necessarily addressable
1693 * (we might use individual bits)
1696 for (int32_t i = 0; i < size; ++i) {
1697 status = readBool(&data);
1708 status_t Parcel::readCharVector(std::unique_ptr<std::vector<char16_t>>* val) const {
1709 return readNullableTypedVector(val, &Parcel::readChar);
1712 status_t Parcel::readCharVector(std::vector<char16_t>* val) const {
1713 return readTypedVector(val, &Parcel::readChar);
1716 status_t Parcel::readString16Vector(
1717 std::unique_ptr<std::vector<std::unique_ptr<String16>>>* val) const {
1718 return readNullableTypedVector(val, &Parcel::readString16);
1721 status_t Parcel::readString16Vector(std::vector<String16>* val) const {
1722 return readTypedVector(val, &Parcel::readString16);
1725 status_t Parcel::readUtf8VectorFromUtf16Vector(
1726 std::unique_ptr<std::vector<std::unique_ptr<std::string>>>* val) const {
1727 return readNullableTypedVector(val, &Parcel::readUtf8FromUtf16);
1730 status_t Parcel::readUtf8VectorFromUtf16Vector(std::vector<std::string>* val) const {
1731 return readTypedVector(val, &Parcel::readUtf8FromUtf16);
1734 status_t Parcel::readInt32(int32_t *pArg) const
1736 return readAligned(pArg);
1739 int32_t Parcel::readInt32() const
1741 return readAligned<int32_t>();
1744 status_t Parcel::readUint32(uint32_t *pArg) const
1746 return readAligned(pArg);
1749 uint32_t Parcel::readUint32() const
1751 return readAligned<uint32_t>();
1754 status_t Parcel::readInt64(int64_t *pArg) const
1756 return readAligned(pArg);
1760 int64_t Parcel::readInt64() const
1762 return readAligned<int64_t>();
1765 status_t Parcel::readUint64(uint64_t *pArg) const
1767 return readAligned(pArg);
1770 uint64_t Parcel::readUint64() const
1772 return readAligned<uint64_t>();
1775 status_t Parcel::readPointer(uintptr_t *pArg) const
1778 binder_uintptr_t ptr;
1779 ret = readAligned(&ptr);
1785 uintptr_t Parcel::readPointer() const
1787 return readAligned<binder_uintptr_t>();
1791 status_t Parcel::readFloat(float *pArg) const
1793 return readAligned(pArg);
1797 float Parcel::readFloat() const
1799 return readAligned<float>();
1802 #if defined(__mips__) && defined(__mips_hard_float)
1804 status_t Parcel::readDouble(double *pArg) const
1808 unsigned long long ll;
1812 status = readAligned(&u.ll);
1817 double Parcel::readDouble() const
1821 unsigned long long ll;
1823 u.ll = readAligned<unsigned long long>();
1829 status_t Parcel::readDouble(double *pArg) const
1831 return readAligned(pArg);
1834 double Parcel::readDouble() const
1836 return readAligned<double>();
1841 status_t Parcel::readIntPtr(intptr_t *pArg) const
1843 return readAligned(pArg);
1847 intptr_t Parcel::readIntPtr() const
1849 return readAligned<intptr_t>();
1852 status_t Parcel::readBool(bool *pArg) const
1855 status_t ret = readInt32(&tmp);
1860 bool Parcel::readBool() const
1862 return readInt32() != 0;
1865 status_t Parcel::readChar(char16_t *pArg) const
1868 status_t ret = readInt32(&tmp);
1869 *pArg = char16_t(tmp);
1873 char16_t Parcel::readChar() const
1875 return char16_t(readInt32());
1878 status_t Parcel::readByte(int8_t *pArg) const
1881 status_t ret = readInt32(&tmp);
1882 *pArg = int8_t(tmp);
1886 int8_t Parcel::readByte() const
1888 return int8_t(readInt32());
1891 status_t Parcel::readUtf8FromUtf16(std::string* str) const {
1892 size_t utf16Size = 0;
1893 const char16_t* src = readString16Inplace(&utf16Size);
1895 return UNEXPECTED_NULL;
1898 // Save ourselves the trouble, we're done.
1899 if (utf16Size == 0u) {
1904 // Allow for closing '\0'
1905 ssize_t utf8Size = utf16_to_utf8_length(src, utf16Size) + 1;
1909 // Note that while it is probably safe to assume string::resize keeps a
1910 // spare byte around for the trailing null, we still pass the size including the trailing null
1911 str->resize(utf8Size);
1912 utf16_to_utf8(src, utf16Size, &((*str)[0]), utf8Size);
1913 str->resize(utf8Size - 1);
1917 status_t Parcel::readUtf8FromUtf16(std::unique_ptr<std::string>* str) const {
1918 const int32_t start = dataPosition();
1920 status_t status = readInt32(&size);
1923 if (status != OK || size < 0) {
1927 setDataPosition(start);
1928 str->reset(new (std::nothrow) std::string());
1929 return readUtf8FromUtf16(str->get());
1932 const char* Parcel::readCString() const
1934 const size_t avail = mDataSize-mDataPos;
1936 const char* str = reinterpret_cast<const char*>(mData+mDataPos);
1937 // is the string's trailing NUL within the parcel's valid bounds?
1938 const char* eos = reinterpret_cast<const char*>(memchr(str, 0, avail));
1940 const size_t len = eos - str;
1941 mDataPos += pad_size(len+1);
1942 ALOGV("readCString Setting data pos of %p to %zu", this, mDataPos);
1949 String8 Parcel::readString8() const
1952 status_t status = readString8(&retString);
1954 // We don't care about errors here, so just return an empty string.
1960 status_t Parcel::readString8(String8* pArg) const
1963 status_t status = readInt32(&size);
1967 // watch for potential int overflow from size+1
1968 if (size < 0 || size >= INT32_MAX) {
1971 // |writeString8| writes nothing for empty string.
1976 const char* str = (const char*)readInplace(size + 1);
1980 pArg->setTo(str, size);
1984 String16 Parcel::readString16() const
1987 const char16_t* str = readString16Inplace(&len);
1988 if (str) return String16(str, len);
1989 ALOGE("Reading a NULL string not supported here.");
1993 status_t Parcel::readString16(std::unique_ptr<String16>* pArg) const
1995 const int32_t start = dataPosition();
1997 status_t status = readInt32(&size);
2000 if (status != OK || size < 0) {
2004 setDataPosition(start);
2005 pArg->reset(new (std::nothrow) String16());
2007 status = readString16(pArg->get());
2016 status_t Parcel::readString16(String16* pArg) const
2019 const char16_t* str = readString16Inplace(&len);
2021 pArg->setTo(str, len);
2025 return UNEXPECTED_NULL;
2029 const char16_t* Parcel::readString16Inplace(size_t* outLen) const
2031 int32_t size = readInt32();
2032 // watch for potential int overflow from size+1
2033 if (size >= 0 && size < INT32_MAX) {
2035 const char16_t* str = (const char16_t*)readInplace((size+1)*sizeof(char16_t));
2044 status_t Parcel::readStrongBinder(sp<IBinder>* val) const
2046 status_t status = readNullableStrongBinder(val);
2047 if (status == OK && !val->get()) {
2048 status = UNEXPECTED_NULL;
2053 status_t Parcel::readNullableStrongBinder(sp<IBinder>* val) const
2055 return unflatten_binder(ProcessState::self(), *this, val);
2058 sp<IBinder> Parcel::readStrongBinder() const
2061 // Note that a lot of code in Android reads binders by hand with this
2062 // method, and that code has historically been ok with getting nullptr
2063 // back (while ignoring error codes).
2064 readNullableStrongBinder(&val);
2068 wp<IBinder> Parcel::readWeakBinder() const
2071 unflatten_binder(ProcessState::self(), *this, &val);
2075 status_t Parcel::readParcelable(Parcelable* parcelable) const {
2076 int32_t have_parcelable = 0;
2077 status_t status = readInt32(&have_parcelable);
2081 if (!have_parcelable) {
2082 return UNEXPECTED_NULL;
2084 return parcelable->readFromParcel(this);
2087 status_t Parcel::readValue(binder::Value* value) const {
2088 return value->readFromParcel(this);
2091 int32_t Parcel::readExceptionCode() const
2093 binder::Status status;
2094 status.readFromParcel(*this);
2095 return status.exceptionCode();
2098 native_handle* Parcel::readNativeHandle() const
2100 int numFds, numInts;
2102 err = readInt32(&numFds);
2103 if (err != NO_ERROR) return 0;
2104 err = readInt32(&numInts);
2105 if (err != NO_ERROR) return 0;
2107 native_handle* h = native_handle_create(numFds, numInts);
2112 for (int i=0 ; err==NO_ERROR && i<numFds ; i++) {
2113 h->data[i] = fcntl(readFileDescriptor(), F_DUPFD_CLOEXEC, 0);
2114 if (h->data[i] < 0) {
2115 for (int j = 0; j < i; j++) {
2118 native_handle_delete(h);
2122 err = read(h->data + numFds, sizeof(int)*numInts);
2123 if (err != NO_ERROR) {
2124 native_handle_close(h);
2125 native_handle_delete(h);
2131 int Parcel::readFileDescriptor() const
2133 const flat_binder_object* flat = readObject(true);
2135 if (flat && flat->type == BINDER_TYPE_FD) {
2136 return flat->handle;
2142 int Parcel::readParcelFileDescriptor() const
2144 int32_t hasComm = readInt32();
2145 int fd = readFileDescriptor();
2148 readFileDescriptor();
2153 status_t Parcel::readUniqueFileDescriptor(base::unique_fd* val) const
2155 int got = readFileDescriptor();
2157 if (got == BAD_TYPE) {
2161 val->reset(fcntl(got, F_DUPFD_CLOEXEC, 0));
2163 if (val->get() < 0) {
2171 status_t Parcel::readUniqueFileDescriptorVector(std::unique_ptr<std::vector<base::unique_fd>>* val) const {
2172 return readNullableTypedVector(val, &Parcel::readUniqueFileDescriptor);
2175 status_t Parcel::readUniqueFileDescriptorVector(std::vector<base::unique_fd>* val) const {
2176 return readTypedVector(val, &Parcel::readUniqueFileDescriptor);
2179 status_t Parcel::readBlob(size_t len, ReadableBlob* outBlob) const
2182 status_t status = readInt32(&blobType);
2183 if (status) return status;
2185 if (blobType == BLOB_INPLACE) {
2186 ALOGV("readBlob: read in place");
2187 const void* ptr = readInplace(len);
2188 if (!ptr) return BAD_VALUE;
2190 outBlob->init(-1, const_cast<void*>(ptr), len, false);
2194 ALOGV("readBlob: read from ashmem");
2195 bool isMutable = (blobType == BLOB_ASHMEM_MUTABLE);
2196 int fd = readFileDescriptor();
2197 if (fd == int(BAD_TYPE)) return BAD_VALUE;
2199 void* ptr = ::mmap(NULL, len, isMutable ? PROT_READ | PROT_WRITE : PROT_READ,
2201 if (ptr == MAP_FAILED) return NO_MEMORY;
2203 outBlob->init(fd, ptr, len, isMutable);
2207 status_t Parcel::read(FlattenableHelperInterface& val) const
2210 const size_t len = this->readInt32();
2211 const size_t fd_count = this->readInt32();
2213 if ((len > INT32_MAX) || (fd_count >= gMaxFds)) {
2214 // don't accept size_t values which may have come from an
2215 // inadvertent conversion from a negative int.
2220 void const* const buf = this->readInplace(pad_size(len));
2226 fds = new (std::nothrow) int[fd_count];
2227 if (fds == nullptr) {
2228 ALOGE("read: failed to allocate requested %zu fds", fd_count);
2233 status_t err = NO_ERROR;
2234 for (size_t i=0 ; i<fd_count && err==NO_ERROR ; i++) {
2235 int fd = this->readFileDescriptor();
2236 if (fd < 0 || ((fds[i] = fcntl(fd, F_DUPFD_CLOEXEC, 0)) < 0)) {
2238 ALOGE("fcntl(F_DUPFD_CLOEXEC) failed in Parcel::read, i is %zu, fds[i] is %d, fd_count is %zu, error: %s",
2239 i, fds[i], fd_count, strerror(fd < 0 ? -fd : errno));
2240 // Close all the file descriptors that were dup-ed.
2241 for (size_t j=0; j<i ;j++) {
2247 if (err == NO_ERROR) {
2248 err = val.unflatten(buf, len, fds, fd_count);
2257 const flat_binder_object* Parcel::readObject(bool nullMetaData) const
2259 const size_t DPOS = mDataPos;
2260 if ((DPOS+sizeof(flat_binder_object)) <= mDataSize) {
2261 const flat_binder_object* obj
2262 = reinterpret_cast<const flat_binder_object*>(mData+DPOS);
2263 mDataPos = DPOS + sizeof(flat_binder_object);
2264 if (!nullMetaData && (obj->cookie == 0 && obj->binder == 0)) {
2265 // When transferring a NULL object, we don't write it into
2266 // the object list, so we don't want to check for it when
2268 ALOGV("readObject Setting data pos of %p to %zu", this, mDataPos);
2272 // Ensure that this object is valid...
2273 binder_size_t* const OBJS = mObjects;
2274 const size_t N = mObjectsSize;
2275 size_t opos = mNextObjectHint;
2278 ALOGV("Parcel %p looking for obj at %zu, hint=%zu",
2281 // Start at the current hint position, looking for an object at
2282 // the current data position.
2284 while (opos < (N-1) && OBJS[opos] < DPOS) {
2290 if (OBJS[opos] == DPOS) {
2292 ALOGV("Parcel %p found obj %zu at index %zu with forward search",
2294 mNextObjectHint = opos+1;
2295 ALOGV("readObject Setting data pos of %p to %zu", this, mDataPos);
2299 // Look backwards for it...
2300 while (opos > 0 && OBJS[opos] > DPOS) {
2303 if (OBJS[opos] == DPOS) {
2305 ALOGV("Parcel %p found obj %zu at index %zu with backward search",
2307 mNextObjectHint = opos+1;
2308 ALOGV("readObject Setting data pos of %p to %zu", this, mDataPos);
2312 ALOGW("Attempt to read object from Parcel %p at offset %zu that is not in the object list",
2318 void Parcel::closeFileDescriptors()
2320 size_t i = mObjectsSize;
2322 //ALOGI("Closing file descriptors for %zu objects...", i);
2326 const flat_binder_object* flat
2327 = reinterpret_cast<flat_binder_object*>(mData+mObjects[i]);
2328 if (flat->type == BINDER_TYPE_FD) {
2329 //ALOGI("Closing fd: %ld", flat->handle);
2330 close(flat->handle);
2335 uintptr_t Parcel::ipcData() const
2337 return reinterpret_cast<uintptr_t>(mData);
2340 size_t Parcel::ipcDataSize() const
2342 return (mDataSize > mDataPos ? mDataSize : mDataPos);
2345 uintptr_t Parcel::ipcObjects() const
2347 return reinterpret_cast<uintptr_t>(mObjects);
2350 size_t Parcel::ipcObjectsCount() const
2352 return mObjectsSize;
2355 void Parcel::ipcSetDataReference(const uint8_t* data, size_t dataSize,
2356 const binder_size_t* objects, size_t objectsCount, release_func relFunc, void* relCookie)
2358 binder_size_t minOffset = 0;
2361 mData = const_cast<uint8_t*>(data);
2362 mDataSize = mDataCapacity = dataSize;
2363 //ALOGI("setDataReference Setting data size of %p to %lu (pid=%d)", this, mDataSize, getpid());
2365 ALOGV("setDataReference Setting data pos of %p to %zu", this, mDataPos);
2366 mObjects = const_cast<binder_size_t*>(objects);
2367 mObjectsSize = mObjectsCapacity = objectsCount;
2368 mNextObjectHint = 0;
2370 mOwnerCookie = relCookie;
2371 for (size_t i = 0; i < mObjectsSize; i++) {
2372 binder_size_t offset = mObjects[i];
2373 if (offset < minOffset) {
2374 ALOGE("%s: bad object offset %" PRIu64 " < %" PRIu64 "\n",
2375 __func__, (uint64_t)offset, (uint64_t)minOffset);
2379 minOffset = offset + sizeof(flat_binder_object);
2384 void Parcel::print(TextOutput& to, uint32_t /*flags*/) const
2388 if (errorCheck() != NO_ERROR) {
2389 const status_t err = errorCheck();
2390 to << "Error: " << (void*)(intptr_t)err << " \"" << strerror(-err) << "\"";
2391 } else if (dataSize() > 0) {
2392 const uint8_t* DATA = data();
2393 to << indent << HexDump(DATA, dataSize()) << dedent;
2394 const binder_size_t* OBJS = objects();
2395 const size_t N = objectsCount();
2396 for (size_t i=0; i<N; i++) {
2397 const flat_binder_object* flat
2398 = reinterpret_cast<const flat_binder_object*>(DATA+OBJS[i]);
2399 to << endl << "Object #" << i << " @ " << (void*)OBJS[i] << ": "
2400 << TypeCode(flat->type & 0x7f7f7f00)
2401 << " = " << flat->binder;
2410 void Parcel::releaseObjects()
2412 const sp<ProcessState> proc(ProcessState::self());
2413 size_t i = mObjectsSize;
2414 uint8_t* const data = mData;
2415 binder_size_t* const objects = mObjects;
2418 const flat_binder_object* flat
2419 = reinterpret_cast<flat_binder_object*>(data+objects[i]);
2420 release_object(proc, *flat, this, &mOpenAshmemSize);
2424 void Parcel::acquireObjects()
2426 const sp<ProcessState> proc(ProcessState::self());
2427 size_t i = mObjectsSize;
2428 uint8_t* const data = mData;
2429 binder_size_t* const objects = mObjects;
2432 const flat_binder_object* flat
2433 = reinterpret_cast<flat_binder_object*>(data+objects[i]);
2434 acquire_object(proc, *flat, this, &mOpenAshmemSize);
2438 void Parcel::freeData()
2444 void Parcel::freeDataNoInit()
2447 LOG_ALLOC("Parcel %p: freeing other owner data", this);
2448 //ALOGI("Freeing data ref of %p (pid=%d)", this, getpid());
2449 mOwner(this, mData, mDataSize, mObjects, mObjectsSize, mOwnerCookie);
2451 LOG_ALLOC("Parcel %p: freeing allocated data", this);
2454 LOG_ALLOC("Parcel %p: freeing with %zu capacity", this, mDataCapacity);
2455 pthread_mutex_lock(&gParcelGlobalAllocSizeLock);
2456 if (mDataCapacity <= gParcelGlobalAllocSize) {
2457 gParcelGlobalAllocSize = gParcelGlobalAllocSize - mDataCapacity;
2459 gParcelGlobalAllocSize = 0;
2461 if (gParcelGlobalAllocCount > 0) {
2462 gParcelGlobalAllocCount--;
2464 pthread_mutex_unlock(&gParcelGlobalAllocSizeLock);
2467 if (mObjects) free(mObjects);
2471 status_t Parcel::growData(size_t len)
2473 if (len > INT32_MAX) {
2474 // don't accept size_t values which may have come from an
2475 // inadvertent conversion from a negative int.
2479 size_t newSize = ((mDataSize+len)*3)/2;
2480 return (newSize <= mDataSize)
2481 ? (status_t) NO_MEMORY
2482 : continueWrite(newSize);
2485 status_t Parcel::restartWrite(size_t desired)
2487 if (desired > INT32_MAX) {
2488 // don't accept size_t values which may have come from an
2489 // inadvertent conversion from a negative int.
2495 return continueWrite(desired);
2498 uint8_t* data = (uint8_t*)realloc(mData, desired);
2499 if (!data && desired > mDataCapacity) {
2507 LOG_ALLOC("Parcel %p: restart from %zu to %zu capacity", this, mDataCapacity, desired);
2508 pthread_mutex_lock(&gParcelGlobalAllocSizeLock);
2509 gParcelGlobalAllocSize += desired;
2510 gParcelGlobalAllocSize -= mDataCapacity;
2512 gParcelGlobalAllocCount++;
2514 pthread_mutex_unlock(&gParcelGlobalAllocSizeLock);
2516 mDataCapacity = desired;
2519 mDataSize = mDataPos = 0;
2520 ALOGV("restartWrite Setting data size of %p to %zu", this, mDataSize);
2521 ALOGV("restartWrite Setting data pos of %p to %zu", this, mDataPos);
2525 mObjectsSize = mObjectsCapacity = 0;
2526 mNextObjectHint = 0;
2534 status_t Parcel::continueWrite(size_t desired)
2536 if (desired > INT32_MAX) {
2537 // don't accept size_t values which may have come from an
2538 // inadvertent conversion from a negative int.
2542 // If shrinking, first adjust for any objects that appear
2543 // after the new data size.
2544 size_t objectsSize = mObjectsSize;
2545 if (desired < mDataSize) {
2549 while (objectsSize > 0) {
2550 if (mObjects[objectsSize-1] < desired)
2558 // If the size is going to zero, just release the owner's data.
2564 // If there is a different owner, we need to take
2566 uint8_t* data = (uint8_t*)malloc(desired);
2571 binder_size_t* objects = NULL;
2574 objects = (binder_size_t*)calloc(objectsSize, sizeof(binder_size_t));
2582 // Little hack to only acquire references on objects
2583 // we will be keeping.
2584 size_t oldObjectsSize = mObjectsSize;
2585 mObjectsSize = objectsSize;
2587 mObjectsSize = oldObjectsSize;
2591 memcpy(data, mData, mDataSize < desired ? mDataSize : desired);
2593 if (objects && mObjects) {
2594 memcpy(objects, mObjects, objectsSize*sizeof(binder_size_t));
2596 //ALOGI("Freeing data ref of %p (pid=%d)", this, getpid());
2597 mOwner(this, mData, mDataSize, mObjects, mObjectsSize, mOwnerCookie);
2600 LOG_ALLOC("Parcel %p: taking ownership of %zu capacity", this, desired);
2601 pthread_mutex_lock(&gParcelGlobalAllocSizeLock);
2602 gParcelGlobalAllocSize += desired;
2603 gParcelGlobalAllocCount++;
2604 pthread_mutex_unlock(&gParcelGlobalAllocSizeLock);
2608 mDataSize = (mDataSize < desired) ? mDataSize : desired;
2609 ALOGV("continueWrite Setting data size of %p to %zu", this, mDataSize);
2610 mDataCapacity = desired;
2611 mObjectsSize = mObjectsCapacity = objectsSize;
2612 mNextObjectHint = 0;
2615 if (objectsSize < mObjectsSize) {
2616 // Need to release refs on any objects we are dropping.
2617 const sp<ProcessState> proc(ProcessState::self());
2618 for (size_t i=objectsSize; i<mObjectsSize; i++) {
2619 const flat_binder_object* flat
2620 = reinterpret_cast<flat_binder_object*>(mData+mObjects[i]);
2621 if (flat->type == BINDER_TYPE_FD) {
2622 // will need to rescan because we may have lopped off the only FDs
2625 release_object(proc, *flat, this, &mOpenAshmemSize);
2627 binder_size_t* objects =
2628 (binder_size_t*)realloc(mObjects, objectsSize*sizeof(binder_size_t));
2632 mObjectsSize = objectsSize;
2633 mNextObjectHint = 0;
2636 // We own the data, so we can just do a realloc().
2637 if (desired > mDataCapacity) {
2638 uint8_t* data = (uint8_t*)realloc(mData, desired);
2640 LOG_ALLOC("Parcel %p: continue from %zu to %zu capacity", this, mDataCapacity,
2642 pthread_mutex_lock(&gParcelGlobalAllocSizeLock);
2643 gParcelGlobalAllocSize += desired;
2644 gParcelGlobalAllocSize -= mDataCapacity;
2645 pthread_mutex_unlock(&gParcelGlobalAllocSizeLock);
2647 mDataCapacity = desired;
2648 } else if (desired > mDataCapacity) {
2653 if (mDataSize > desired) {
2654 mDataSize = desired;
2655 ALOGV("continueWrite Setting data size of %p to %zu", this, mDataSize);
2657 if (mDataPos > desired) {
2659 ALOGV("continueWrite Setting data pos of %p to %zu", this, mDataPos);
2664 // This is the first data. Easy!
2665 uint8_t* data = (uint8_t*)malloc(desired);
2671 if(!(mDataCapacity == 0 && mObjects == NULL
2672 && mObjectsCapacity == 0)) {
2673 ALOGE("continueWrite: %zu/%p/%zu/%zu", mDataCapacity, mObjects, mObjectsCapacity, desired);
2676 LOG_ALLOC("Parcel %p: allocating with %zu capacity", this, desired);
2677 pthread_mutex_lock(&gParcelGlobalAllocSizeLock);
2678 gParcelGlobalAllocSize += desired;
2679 gParcelGlobalAllocCount++;
2680 pthread_mutex_unlock(&gParcelGlobalAllocSizeLock);
2683 mDataSize = mDataPos = 0;
2684 ALOGV("continueWrite Setting data size of %p to %zu", this, mDataSize);
2685 ALOGV("continueWrite Setting data pos of %p to %zu", this, mDataPos);
2686 mDataCapacity = desired;
2692 void Parcel::initState()
2694 LOG_ALLOC("Parcel %p: initState", this);
2700 ALOGV("initState Setting data size of %p to %zu", this, mDataSize);
2701 ALOGV("initState Setting data pos of %p to %zu", this, mDataPos);
2704 mObjectsCapacity = 0;
2705 mNextObjectHint = 0;
2710 mOpenAshmemSize = 0;
2712 // racing multiple init leads only to multiple identical write
2714 struct rlimit result;
2715 if (!getrlimit(RLIMIT_NOFILE, &result)) {
2716 gMaxFds = (size_t)result.rlim_cur;
2717 //ALOGI("parcel fd limit set to %zu", gMaxFds);
2719 ALOGW("Unable to getrlimit: %s", strerror(errno));
2725 void Parcel::scanForFds() const
2727 bool hasFds = false;
2728 for (size_t i=0; i<mObjectsSize; i++) {
2729 const flat_binder_object* flat
2730 = reinterpret_cast<const flat_binder_object*>(mData + mObjects[i]);
2731 if (flat->type == BINDER_TYPE_FD) {
2740 size_t Parcel::getBlobAshmemSize() const
2742 // This used to return the size of all blobs that were written to ashmem, now we're returning
2743 // the ashmem currently referenced by this Parcel, which should be equivalent.
2744 // TODO: Remove method once ABI can be changed.
2745 return mOpenAshmemSize;
2748 size_t Parcel::getOpenAshmemSize() const
2750 return mOpenAshmemSize;
2753 // --- Parcel::Blob ---
2755 Parcel::Blob::Blob() :
2756 mFd(-1), mData(NULL), mSize(0), mMutable(false) {
2759 Parcel::Blob::~Blob() {
2763 void Parcel::Blob::release() {
2764 if (mFd != -1 && mData) {
2765 ::munmap(mData, mSize);
2770 void Parcel::Blob::init(int fd, void* data, size_t size, bool isMutable) {
2774 mMutable = isMutable;
2777 void Parcel::Blob::clear() {
2784 }; // namespace android