Parcel.cpp revision 5f0625695ce6999ec70f5699f70a8a0fa84a1bf6
1/* 2 * Copyright (C) 2005 The Android Open Source Project 3 * 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 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 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. 15 */ 16 17#define LOG_TAG "Parcel" 18//#define LOG_NDEBUG 0 19 20#include <binder/Parcel.h> 21 22#include <binder/IPCThreadState.h> 23#include <binder/Binder.h> 24#include <binder/BpBinder.h> 25#include <binder/ProcessState.h> 26#include <binder/TextOutput.h> 27 28#include <errno.h> 29#include <utils/Debug.h> 30#include <utils/Log.h> 31#include <utils/String8.h> 32#include <utils/String16.h> 33#include <utils/misc.h> 34#include <utils/Flattenable.h> 35#include <cutils/ashmem.h> 36 37#include <private/binder/binder_module.h> 38#include <private/binder/Static.h> 39 40#include <inttypes.h> 41#include <stdio.h> 42#include <stdlib.h> 43#include <stdint.h> 44#include <sys/mman.h> 45 46#ifndef INT32_MAX 47#define INT32_MAX ((int32_t)(2147483647)) 48#endif 49 50#define LOG_REFS(...) 51//#define LOG_REFS(...) ALOG(LOG_DEBUG, "Parcel", __VA_ARGS__) 52#define LOG_ALLOC(...) 53//#define LOG_ALLOC(...) ALOG(LOG_DEBUG, "Parcel", __VA_ARGS__) 54 55// --------------------------------------------------------------------------- 56 57// This macro should never be used at runtime, as a too large value 58// of s could cause an integer overflow. Instead, you should always 59// use the wrapper function pad_size() 60#define PAD_SIZE_UNSAFE(s) (((s)+3)&~3) 61 62static size_t pad_size(size_t s) { 63 if (s > (SIZE_T_MAX - 3)) { 64 abort(); 65 } 66 return PAD_SIZE_UNSAFE(s); 67} 68 69// Note: must be kept in sync with android/os/StrictMode.java's PENALTY_GATHER 70#define STRICT_MODE_PENALTY_GATHER (0x40 << 16) 71 72// Note: must be kept in sync with android/os/Parcel.java's EX_HAS_REPLY_HEADER 73#define EX_HAS_REPLY_HEADER -128 74 75// XXX This can be made public if we want to provide 76// support for typed data. 77struct small_flat_data 78{ 79 uint32_t type; 80 uint32_t data; 81}; 82 83namespace android { 84 85static pthread_mutex_t gParcelGlobalAllocSizeLock = PTHREAD_MUTEX_INITIALIZER; 86static size_t gParcelGlobalAllocSize = 0; 87static size_t gParcelGlobalAllocCount = 0; 88 89// Maximum size of a blob to transfer in-place. 90static const size_t BLOB_INPLACE_LIMIT = 16 * 1024; 91 92enum { 93 BLOB_INPLACE = 0, 94 BLOB_ASHMEM_IMMUTABLE = 1, 95 BLOB_ASHMEM_MUTABLE = 2, 96}; 97 98void acquire_object(const sp<ProcessState>& proc, 99 const flat_binder_object& obj, const void* who) 100{ 101 switch (obj.type) { 102 case BINDER_TYPE_BINDER: 103 if (obj.binder) { 104 LOG_REFS("Parcel %p acquiring reference on local %p", who, obj.cookie); 105 reinterpret_cast<IBinder*>(obj.cookie)->incStrong(who); 106 } 107 return; 108 case BINDER_TYPE_WEAK_BINDER: 109 if (obj.binder) 110 reinterpret_cast<RefBase::weakref_type*>(obj.binder)->incWeak(who); 111 return; 112 case BINDER_TYPE_HANDLE: { 113 const sp<IBinder> b = proc->getStrongProxyForHandle(obj.handle); 114 if (b != NULL) { 115 LOG_REFS("Parcel %p acquiring reference on remote %p", who, b.get()); 116 b->incStrong(who); 117 } 118 return; 119 } 120 case BINDER_TYPE_WEAK_HANDLE: { 121 const wp<IBinder> b = proc->getWeakProxyForHandle(obj.handle); 122 if (b != NULL) b.get_refs()->incWeak(who); 123 return; 124 } 125 case BINDER_TYPE_FD: { 126 // intentionally blank -- nothing to do to acquire this, but we do 127 // recognize it as a legitimate object type. 128 return; 129 } 130 } 131 132 ALOGD("Invalid object type 0x%08x", obj.type); 133} 134 135void release_object(const sp<ProcessState>& proc, 136 const flat_binder_object& obj, const void* who) 137{ 138 switch (obj.type) { 139 case BINDER_TYPE_BINDER: 140 if (obj.binder) { 141 LOG_REFS("Parcel %p releasing reference on local %p", who, obj.cookie); 142 reinterpret_cast<IBinder*>(obj.cookie)->decStrong(who); 143 } 144 return; 145 case BINDER_TYPE_WEAK_BINDER: 146 if (obj.binder) 147 reinterpret_cast<RefBase::weakref_type*>(obj.binder)->decWeak(who); 148 return; 149 case BINDER_TYPE_HANDLE: { 150 const sp<IBinder> b = proc->getStrongProxyForHandle(obj.handle); 151 if (b != NULL) { 152 LOG_REFS("Parcel %p releasing reference on remote %p", who, b.get()); 153 b->decStrong(who); 154 } 155 return; 156 } 157 case BINDER_TYPE_WEAK_HANDLE: { 158 const wp<IBinder> b = proc->getWeakProxyForHandle(obj.handle); 159 if (b != NULL) b.get_refs()->decWeak(who); 160 return; 161 } 162 case BINDER_TYPE_FD: { 163 if (obj.cookie != 0) close(obj.handle); 164 return; 165 } 166 } 167 168 ALOGE("Invalid object type 0x%08x", obj.type); 169} 170 171inline static status_t finish_flatten_binder( 172 const sp<IBinder>& /*binder*/, const flat_binder_object& flat, Parcel* out) 173{ 174 return out->writeObject(flat, false); 175} 176 177status_t flatten_binder(const sp<ProcessState>& /*proc*/, 178 const sp<IBinder>& binder, Parcel* out) 179{ 180 flat_binder_object obj; 181 182 obj.flags = 0x7f | FLAT_BINDER_FLAG_ACCEPTS_FDS; 183 if (binder != NULL) { 184 IBinder *local = binder->localBinder(); 185 if (!local) { 186 BpBinder *proxy = binder->remoteBinder(); 187 if (proxy == NULL) { 188 ALOGE("null proxy"); 189 } 190 const int32_t handle = proxy ? proxy->handle() : 0; 191 obj.type = BINDER_TYPE_HANDLE; 192 obj.binder = 0; /* Don't pass uninitialized stack data to a remote process */ 193 obj.handle = handle; 194 obj.cookie = 0; 195 } else { 196 obj.type = BINDER_TYPE_BINDER; 197 obj.binder = reinterpret_cast<uintptr_t>(local->getWeakRefs()); 198 obj.cookie = reinterpret_cast<uintptr_t>(local); 199 } 200 } else { 201 obj.type = BINDER_TYPE_BINDER; 202 obj.binder = 0; 203 obj.cookie = 0; 204 } 205 206 return finish_flatten_binder(binder, obj, out); 207} 208 209status_t flatten_binder(const sp<ProcessState>& /*proc*/, 210 const wp<IBinder>& binder, Parcel* out) 211{ 212 flat_binder_object obj; 213 214 obj.flags = 0x7f | FLAT_BINDER_FLAG_ACCEPTS_FDS; 215 if (binder != NULL) { 216 sp<IBinder> real = binder.promote(); 217 if (real != NULL) { 218 IBinder *local = real->localBinder(); 219 if (!local) { 220 BpBinder *proxy = real->remoteBinder(); 221 if (proxy == NULL) { 222 ALOGE("null proxy"); 223 } 224 const int32_t handle = proxy ? proxy->handle() : 0; 225 obj.type = BINDER_TYPE_WEAK_HANDLE; 226 obj.binder = 0; /* Don't pass uninitialized stack data to a remote process */ 227 obj.handle = handle; 228 obj.cookie = 0; 229 } else { 230 obj.type = BINDER_TYPE_WEAK_BINDER; 231 obj.binder = reinterpret_cast<uintptr_t>(binder.get_refs()); 232 obj.cookie = reinterpret_cast<uintptr_t>(binder.unsafe_get()); 233 } 234 return finish_flatten_binder(real, obj, out); 235 } 236 237 // XXX How to deal? In order to flatten the given binder, 238 // we need to probe it for information, which requires a primary 239 // reference... but we don't have one. 240 // 241 // The OpenBinder implementation uses a dynamic_cast<> here, 242 // but we can't do that with the different reference counting 243 // implementation we are using. 244 ALOGE("Unable to unflatten Binder weak reference!"); 245 obj.type = BINDER_TYPE_BINDER; 246 obj.binder = 0; 247 obj.cookie = 0; 248 return finish_flatten_binder(NULL, obj, out); 249 250 } else { 251 obj.type = BINDER_TYPE_BINDER; 252 obj.binder = 0; 253 obj.cookie = 0; 254 return finish_flatten_binder(NULL, obj, out); 255 } 256} 257 258inline static status_t finish_unflatten_binder( 259 BpBinder* /*proxy*/, const flat_binder_object& /*flat*/, 260 const Parcel& /*in*/) 261{ 262 return NO_ERROR; 263} 264 265status_t unflatten_binder(const sp<ProcessState>& proc, 266 const Parcel& in, sp<IBinder>* out) 267{ 268 const flat_binder_object* flat = in.readObject(false); 269 270 if (flat) { 271 switch (flat->type) { 272 case BINDER_TYPE_BINDER: 273 *out = reinterpret_cast<IBinder*>(flat->cookie); 274 return finish_unflatten_binder(NULL, *flat, in); 275 case BINDER_TYPE_HANDLE: 276 *out = proc->getStrongProxyForHandle(flat->handle); 277 return finish_unflatten_binder( 278 static_cast<BpBinder*>(out->get()), *flat, in); 279 } 280 } 281 return BAD_TYPE; 282} 283 284status_t unflatten_binder(const sp<ProcessState>& proc, 285 const Parcel& in, wp<IBinder>* out) 286{ 287 const flat_binder_object* flat = in.readObject(false); 288 289 if (flat) { 290 switch (flat->type) { 291 case BINDER_TYPE_BINDER: 292 *out = reinterpret_cast<IBinder*>(flat->cookie); 293 return finish_unflatten_binder(NULL, *flat, in); 294 case BINDER_TYPE_WEAK_BINDER: 295 if (flat->binder != 0) { 296 out->set_object_and_refs( 297 reinterpret_cast<IBinder*>(flat->cookie), 298 reinterpret_cast<RefBase::weakref_type*>(flat->binder)); 299 } else { 300 *out = NULL; 301 } 302 return finish_unflatten_binder(NULL, *flat, in); 303 case BINDER_TYPE_HANDLE: 304 case BINDER_TYPE_WEAK_HANDLE: 305 *out = proc->getWeakProxyForHandle(flat->handle); 306 return finish_unflatten_binder( 307 static_cast<BpBinder*>(out->unsafe_get()), *flat, in); 308 } 309 } 310 return BAD_TYPE; 311} 312 313namespace { 314 315template<typename T> 316status_t readTypedVector(std::vector<T>* val, const Parcel* p, 317 status_t(Parcel::*read_func)(T*) const) { 318 val->clear(); 319 320 int32_t size; 321 status_t status = p->readInt32(&size); 322 323 if (status != OK) { 324 return status; 325 } 326 327 if (size < 0) { 328 return UNEXPECTED_NULL; 329 } 330 331 val->resize(size); 332 333 for (auto& v: *val) { 334 status = (p->*read_func)(&v); 335 336 if (status != OK) { 337 return status; 338 } 339 } 340 341 return OK; 342} 343 344} // namespace 345 346// --------------------------------------------------------------------------- 347 348Parcel::Parcel() 349{ 350 LOG_ALLOC("Parcel %p: constructing", this); 351 initState(); 352} 353 354Parcel::~Parcel() 355{ 356 freeDataNoInit(); 357 LOG_ALLOC("Parcel %p: destroyed", this); 358} 359 360size_t Parcel::getGlobalAllocSize() { 361 pthread_mutex_lock(&gParcelGlobalAllocSizeLock); 362 size_t size = gParcelGlobalAllocSize; 363 pthread_mutex_unlock(&gParcelGlobalAllocSizeLock); 364 return size; 365} 366 367size_t Parcel::getGlobalAllocCount() { 368 pthread_mutex_lock(&gParcelGlobalAllocSizeLock); 369 size_t count = gParcelGlobalAllocCount; 370 pthread_mutex_unlock(&gParcelGlobalAllocSizeLock); 371 return count; 372} 373 374const uint8_t* Parcel::data() const 375{ 376 return mData; 377} 378 379size_t Parcel::dataSize() const 380{ 381 return (mDataSize > mDataPos ? mDataSize : mDataPos); 382} 383 384size_t Parcel::dataAvail() const 385{ 386 // TODO: decide what to do about the possibility that this can 387 // report an available-data size that exceeds a Java int's max 388 // positive value, causing havoc. Fortunately this will only 389 // happen if someone constructs a Parcel containing more than two 390 // gigabytes of data, which on typical phone hardware is simply 391 // not possible. 392 return dataSize() - dataPosition(); 393} 394 395size_t Parcel::dataPosition() const 396{ 397 return mDataPos; 398} 399 400size_t Parcel::dataCapacity() const 401{ 402 return mDataCapacity; 403} 404 405status_t Parcel::setDataSize(size_t size) 406{ 407 if (size > INT32_MAX) { 408 // don't accept size_t values which may have come from an 409 // inadvertent conversion from a negative int. 410 return BAD_VALUE; 411 } 412 413 status_t err; 414 err = continueWrite(size); 415 if (err == NO_ERROR) { 416 mDataSize = size; 417 ALOGV("setDataSize Setting data size of %p to %zu", this, mDataSize); 418 } 419 return err; 420} 421 422void Parcel::setDataPosition(size_t pos) const 423{ 424 if (pos > INT32_MAX) { 425 // don't accept size_t values which may have come from an 426 // inadvertent conversion from a negative int. 427 abort(); 428 } 429 430 mDataPos = pos; 431 mNextObjectHint = 0; 432} 433 434status_t Parcel::setDataCapacity(size_t size) 435{ 436 if (size > INT32_MAX) { 437 // don't accept size_t values which may have come from an 438 // inadvertent conversion from a negative int. 439 return BAD_VALUE; 440 } 441 442 if (size > mDataCapacity) return continueWrite(size); 443 return NO_ERROR; 444} 445 446status_t Parcel::setData(const uint8_t* buffer, size_t len) 447{ 448 if (len > INT32_MAX) { 449 // don't accept size_t values which may have come from an 450 // inadvertent conversion from a negative int. 451 return BAD_VALUE; 452 } 453 454 status_t err = restartWrite(len); 455 if (err == NO_ERROR) { 456 memcpy(const_cast<uint8_t*>(data()), buffer, len); 457 mDataSize = len; 458 mFdsKnown = false; 459 } 460 return err; 461} 462 463status_t Parcel::appendFrom(const Parcel *parcel, size_t offset, size_t len) 464{ 465 const sp<ProcessState> proc(ProcessState::self()); 466 status_t err; 467 const uint8_t *data = parcel->mData; 468 const binder_size_t *objects = parcel->mObjects; 469 size_t size = parcel->mObjectsSize; 470 int startPos = mDataPos; 471 int firstIndex = -1, lastIndex = -2; 472 473 if (len == 0) { 474 return NO_ERROR; 475 } 476 477 if (len > INT32_MAX) { 478 // don't accept size_t values which may have come from an 479 // inadvertent conversion from a negative int. 480 return BAD_VALUE; 481 } 482 483 // range checks against the source parcel size 484 if ((offset > parcel->mDataSize) 485 || (len > parcel->mDataSize) 486 || (offset + len > parcel->mDataSize)) { 487 return BAD_VALUE; 488 } 489 490 // Count objects in range 491 for (int i = 0; i < (int) size; i++) { 492 size_t off = objects[i]; 493 if ((off >= offset) && (off + sizeof(flat_binder_object) <= offset + len)) { 494 if (firstIndex == -1) { 495 firstIndex = i; 496 } 497 lastIndex = i; 498 } 499 } 500 int numObjects = lastIndex - firstIndex + 1; 501 502 if ((mDataSize+len) > mDataCapacity) { 503 // grow data 504 err = growData(len); 505 if (err != NO_ERROR) { 506 return err; 507 } 508 } 509 510 // append data 511 memcpy(mData + mDataPos, data + offset, len); 512 mDataPos += len; 513 mDataSize += len; 514 515 err = NO_ERROR; 516 517 if (numObjects > 0) { 518 // grow objects 519 if (mObjectsCapacity < mObjectsSize + numObjects) { 520 size_t newSize = ((mObjectsSize + numObjects)*3)/2; 521 if (newSize < mObjectsSize) return NO_MEMORY; // overflow 522 binder_size_t *objects = 523 (binder_size_t*)realloc(mObjects, newSize*sizeof(binder_size_t)); 524 if (objects == (binder_size_t*)0) { 525 return NO_MEMORY; 526 } 527 mObjects = objects; 528 mObjectsCapacity = newSize; 529 } 530 531 // append and acquire objects 532 int idx = mObjectsSize; 533 for (int i = firstIndex; i <= lastIndex; i++) { 534 size_t off = objects[i] - offset + startPos; 535 mObjects[idx++] = off; 536 mObjectsSize++; 537 538 flat_binder_object* flat 539 = reinterpret_cast<flat_binder_object*>(mData + off); 540 acquire_object(proc, *flat, this); 541 542 if (flat->type == BINDER_TYPE_FD) { 543 // If this is a file descriptor, we need to dup it so the 544 // new Parcel now owns its own fd, and can declare that we 545 // officially know we have fds. 546 flat->handle = dup(flat->handle); 547 flat->cookie = 1; 548 mHasFds = mFdsKnown = true; 549 if (!mAllowFds) { 550 err = FDS_NOT_ALLOWED; 551 } 552 } 553 } 554 } 555 556 return err; 557} 558 559bool Parcel::allowFds() const 560{ 561 return mAllowFds; 562} 563 564bool Parcel::pushAllowFds(bool allowFds) 565{ 566 const bool origValue = mAllowFds; 567 if (!allowFds) { 568 mAllowFds = false; 569 } 570 return origValue; 571} 572 573void Parcel::restoreAllowFds(bool lastValue) 574{ 575 mAllowFds = lastValue; 576} 577 578bool Parcel::hasFileDescriptors() const 579{ 580 if (!mFdsKnown) { 581 scanForFds(); 582 } 583 return mHasFds; 584} 585 586// Write RPC headers. (previously just the interface token) 587status_t Parcel::writeInterfaceToken(const String16& interface) 588{ 589 writeInt32(IPCThreadState::self()->getStrictModePolicy() | 590 STRICT_MODE_PENALTY_GATHER); 591 // currently the interface identification token is just its name as a string 592 return writeString16(interface); 593} 594 595bool Parcel::checkInterface(IBinder* binder) const 596{ 597 return enforceInterface(binder->getInterfaceDescriptor()); 598} 599 600bool Parcel::enforceInterface(const String16& interface, 601 IPCThreadState* threadState) const 602{ 603 int32_t strictPolicy = readInt32(); 604 if (threadState == NULL) { 605 threadState = IPCThreadState::self(); 606 } 607 if ((threadState->getLastTransactionBinderFlags() & 608 IBinder::FLAG_ONEWAY) != 0) { 609 // For one-way calls, the callee is running entirely 610 // disconnected from the caller, so disable StrictMode entirely. 611 // Not only does disk/network usage not impact the caller, but 612 // there's no way to commuicate back any violations anyway. 613 threadState->setStrictModePolicy(0); 614 } else { 615 threadState->setStrictModePolicy(strictPolicy); 616 } 617 const String16 str(readString16()); 618 if (str == interface) { 619 return true; 620 } else { 621 ALOGW("**** enforceInterface() expected '%s' but read '%s'", 622 String8(interface).string(), String8(str).string()); 623 return false; 624 } 625} 626 627const binder_size_t* Parcel::objects() const 628{ 629 return mObjects; 630} 631 632size_t Parcel::objectsCount() const 633{ 634 return mObjectsSize; 635} 636 637status_t Parcel::errorCheck() const 638{ 639 return mError; 640} 641 642void Parcel::setError(status_t err) 643{ 644 mError = err; 645} 646 647status_t Parcel::finishWrite(size_t len) 648{ 649 if (len > INT32_MAX) { 650 // don't accept size_t values which may have come from an 651 // inadvertent conversion from a negative int. 652 return BAD_VALUE; 653 } 654 655 //printf("Finish write of %d\n", len); 656 mDataPos += len; 657 ALOGV("finishWrite Setting data pos of %p to %zu", this, mDataPos); 658 if (mDataPos > mDataSize) { 659 mDataSize = mDataPos; 660 ALOGV("finishWrite Setting data size of %p to %zu", this, mDataSize); 661 } 662 //printf("New pos=%d, size=%d\n", mDataPos, mDataSize); 663 return NO_ERROR; 664} 665 666status_t Parcel::writeUnpadded(const void* data, size_t len) 667{ 668 if (len > INT32_MAX) { 669 // don't accept size_t values which may have come from an 670 // inadvertent conversion from a negative int. 671 return BAD_VALUE; 672 } 673 674 size_t end = mDataPos + len; 675 if (end < mDataPos) { 676 // integer overflow 677 return BAD_VALUE; 678 } 679 680 if (end <= mDataCapacity) { 681restart_write: 682 memcpy(mData+mDataPos, data, len); 683 return finishWrite(len); 684 } 685 686 status_t err = growData(len); 687 if (err == NO_ERROR) goto restart_write; 688 return err; 689} 690 691status_t Parcel::write(const void* data, size_t len) 692{ 693 if (len > INT32_MAX) { 694 // don't accept size_t values which may have come from an 695 // inadvertent conversion from a negative int. 696 return BAD_VALUE; 697 } 698 699 void* const d = writeInplace(len); 700 if (d) { 701 memcpy(d, data, len); 702 return NO_ERROR; 703 } 704 return mError; 705} 706 707void* Parcel::writeInplace(size_t len) 708{ 709 if (len > INT32_MAX) { 710 // don't accept size_t values which may have come from an 711 // inadvertent conversion from a negative int. 712 return NULL; 713 } 714 715 const size_t padded = pad_size(len); 716 717 // sanity check for integer overflow 718 if (mDataPos+padded < mDataPos) { 719 return NULL; 720 } 721 722 if ((mDataPos+padded) <= mDataCapacity) { 723restart_write: 724 //printf("Writing %ld bytes, padded to %ld\n", len, padded); 725 uint8_t* const data = mData+mDataPos; 726 727 // Need to pad at end? 728 if (padded != len) { 729#if BYTE_ORDER == BIG_ENDIAN 730 static const uint32_t mask[4] = { 731 0x00000000, 0xffffff00, 0xffff0000, 0xff000000 732 }; 733#endif 734#if BYTE_ORDER == LITTLE_ENDIAN 735 static const uint32_t mask[4] = { 736 0x00000000, 0x00ffffff, 0x0000ffff, 0x000000ff 737 }; 738#endif 739 //printf("Applying pad mask: %p to %p\n", (void*)mask[padded-len], 740 // *reinterpret_cast<void**>(data+padded-4)); 741 *reinterpret_cast<uint32_t*>(data+padded-4) &= mask[padded-len]; 742 } 743 744 finishWrite(padded); 745 return data; 746 } 747 748 status_t err = growData(padded); 749 if (err == NO_ERROR) goto restart_write; 750 return NULL; 751} 752 753namespace { 754 755template<typename T, typename U> 756status_t unsafeWriteTypedVector(const std::vector<T>& val, Parcel* p, 757 status_t(Parcel::*write_func)(U)) { 758 if (val.size() > std::numeric_limits<int32_t>::max()) { 759 return BAD_VALUE; 760 } 761 762 status_t status = p->writeInt32(val.size()); 763 764 if (status != OK) { 765 return status; 766 } 767 768 for (const auto& item : val) { 769 status = (p->*write_func)(item); 770 771 if (status != OK) { 772 return status; 773 } 774 } 775 776 return OK; 777} 778 779template<typename T> 780status_t writeTypedVector(const std::vector<T>& val, Parcel* p, 781 status_t(Parcel::*write_func)(const T&)) { 782 return unsafeWriteTypedVector(val, p, write_func); 783} 784 785template<typename T> 786status_t writeTypedVector(const std::vector<T>& val, Parcel* p, 787 status_t(Parcel::*write_func)(T)) { 788 return unsafeWriteTypedVector(val, p, write_func); 789} 790 791} // namespace 792 793status_t Parcel::writeByteVector(const std::vector<int8_t>& val) 794{ 795 status_t status; 796 if (val.size() > std::numeric_limits<int32_t>::max()) { 797 status = BAD_VALUE; 798 return status; 799 } 800 801 status = writeInt32(val.size()); 802 if (status != OK) { 803 return status; 804 } 805 806 void* data = writeInplace(val.size()); 807 if (!data) { 808 status = BAD_VALUE; 809 return status; 810 } 811 812 memcpy(data, val.data(), val.size()); 813 return status; 814} 815 816status_t Parcel::writeInt32Vector(const std::vector<int32_t>& val) 817{ 818 return writeTypedVector(val, this, &Parcel::writeInt32); 819} 820 821status_t Parcel::writeInt64Vector(const std::vector<int64_t>& val) 822{ 823 return writeTypedVector(val, this, &Parcel::writeInt64); 824} 825 826status_t Parcel::writeFloatVector(const std::vector<float>& val) 827{ 828 return writeTypedVector(val, this, &Parcel::writeFloat); 829} 830 831status_t Parcel::writeDoubleVector(const std::vector<double>& val) 832{ 833 return writeTypedVector(val, this, &Parcel::writeDouble); 834} 835 836status_t Parcel::writeBoolVector(const std::vector<bool>& val) 837{ 838 return writeTypedVector(val, this, &Parcel::writeBool); 839} 840 841status_t Parcel::writeCharVector(const std::vector<char16_t>& val) 842{ 843 return writeTypedVector(val, this, &Parcel::writeChar); 844} 845 846status_t Parcel::writeString16Vector(const std::vector<String16>& val) 847{ 848 return writeTypedVector(val, this, &Parcel::writeString16); 849} 850 851status_t Parcel::writeInt32(int32_t val) 852{ 853 return writeAligned(val); 854} 855 856status_t Parcel::writeUint32(uint32_t val) 857{ 858 return writeAligned(val); 859} 860 861status_t Parcel::writeInt32Array(size_t len, const int32_t *val) { 862 if (len > INT32_MAX) { 863 // don't accept size_t values which may have come from an 864 // inadvertent conversion from a negative int. 865 return BAD_VALUE; 866 } 867 868 if (!val) { 869 return writeInt32(-1); 870 } 871 status_t ret = writeInt32(static_cast<uint32_t>(len)); 872 if (ret == NO_ERROR) { 873 ret = write(val, len * sizeof(*val)); 874 } 875 return ret; 876} 877status_t Parcel::writeByteArray(size_t len, const uint8_t *val) { 878 if (len > INT32_MAX) { 879 // don't accept size_t values which may have come from an 880 // inadvertent conversion from a negative int. 881 return BAD_VALUE; 882 } 883 884 if (!val) { 885 return writeInt32(-1); 886 } 887 status_t ret = writeInt32(static_cast<uint32_t>(len)); 888 if (ret == NO_ERROR) { 889 ret = write(val, len * sizeof(*val)); 890 } 891 return ret; 892} 893 894status_t Parcel::writeBool(bool val) 895{ 896 return writeInt32(int32_t(val)); 897} 898 899status_t Parcel::writeChar(char16_t val) 900{ 901 return writeInt32(int32_t(val)); 902} 903 904status_t Parcel::writeByte(int8_t val) 905{ 906 return writeInt32(int32_t(val)); 907} 908 909status_t Parcel::writeInt64(int64_t val) 910{ 911 return writeAligned(val); 912} 913 914status_t Parcel::writeUint64(uint64_t val) 915{ 916 return writeAligned(val); 917} 918 919status_t Parcel::writePointer(uintptr_t val) 920{ 921 return writeAligned<binder_uintptr_t>(val); 922} 923 924status_t Parcel::writeFloat(float val) 925{ 926 return writeAligned(val); 927} 928 929#if defined(__mips__) && defined(__mips_hard_float) 930 931status_t Parcel::writeDouble(double val) 932{ 933 union { 934 double d; 935 unsigned long long ll; 936 } u; 937 u.d = val; 938 return writeAligned(u.ll); 939} 940 941#else 942 943status_t Parcel::writeDouble(double val) 944{ 945 return writeAligned(val); 946} 947 948#endif 949 950status_t Parcel::writeCString(const char* str) 951{ 952 return write(str, strlen(str)+1); 953} 954 955status_t Parcel::writeString8(const String8& str) 956{ 957 status_t err = writeInt32(str.bytes()); 958 // only write string if its length is more than zero characters, 959 // as readString8 will only read if the length field is non-zero. 960 // this is slightly different from how writeString16 works. 961 if (str.bytes() > 0 && err == NO_ERROR) { 962 err = write(str.string(), str.bytes()+1); 963 } 964 return err; 965} 966 967status_t Parcel::writeString16(const String16& str) 968{ 969 return writeString16(str.string(), str.size()); 970} 971 972status_t Parcel::writeString16(const char16_t* str, size_t len) 973{ 974 if (str == NULL) return writeInt32(-1); 975 976 status_t err = writeInt32(len); 977 if (err == NO_ERROR) { 978 len *= sizeof(char16_t); 979 uint8_t* data = (uint8_t*)writeInplace(len+sizeof(char16_t)); 980 if (data) { 981 memcpy(data, str, len); 982 *reinterpret_cast<char16_t*>(data+len) = 0; 983 return NO_ERROR; 984 } 985 err = mError; 986 } 987 return err; 988} 989 990status_t Parcel::writeStrongBinder(const sp<IBinder>& val) 991{ 992 return flatten_binder(ProcessState::self(), val, this); 993} 994 995status_t Parcel::writeStrongBinderVector(const std::vector<sp<IBinder>>& val) 996{ 997 return writeTypedVector(val, this, &Parcel::writeStrongBinder); 998} 999 1000status_t Parcel::readStrongBinderVector(std::vector<sp<IBinder>>* val) const { 1001 return readTypedVector(val, this, &Parcel::readStrongBinder); 1002} 1003 1004status_t Parcel::writeWeakBinder(const wp<IBinder>& val) 1005{ 1006 return flatten_binder(ProcessState::self(), val, this); 1007} 1008 1009status_t Parcel::writeNativeHandle(const native_handle* handle) 1010{ 1011 if (!handle || handle->version != sizeof(native_handle)) 1012 return BAD_TYPE; 1013 1014 status_t err; 1015 err = writeInt32(handle->numFds); 1016 if (err != NO_ERROR) return err; 1017 1018 err = writeInt32(handle->numInts); 1019 if (err != NO_ERROR) return err; 1020 1021 for (int i=0 ; err==NO_ERROR && i<handle->numFds ; i++) 1022 err = writeDupFileDescriptor(handle->data[i]); 1023 1024 if (err != NO_ERROR) { 1025 ALOGD("write native handle, write dup fd failed"); 1026 return err; 1027 } 1028 err = write(handle->data + handle->numFds, sizeof(int)*handle->numInts); 1029 return err; 1030} 1031 1032status_t Parcel::writeFileDescriptor(int fd, bool takeOwnership) 1033{ 1034 flat_binder_object obj; 1035 obj.type = BINDER_TYPE_FD; 1036 obj.flags = 0x7f | FLAT_BINDER_FLAG_ACCEPTS_FDS; 1037 obj.binder = 0; /* Don't pass uninitialized stack data to a remote process */ 1038 obj.handle = fd; 1039 obj.cookie = takeOwnership ? 1 : 0; 1040 return writeObject(obj, true); 1041} 1042 1043status_t Parcel::writeDupFileDescriptor(int fd) 1044{ 1045 int dupFd = dup(fd); 1046 if (dupFd < 0) { 1047 return -errno; 1048 } 1049 status_t err = writeFileDescriptor(dupFd, true /*takeOwnership*/); 1050 if (err) { 1051 close(dupFd); 1052 } 1053 return err; 1054} 1055 1056status_t Parcel::writeBlob(size_t len, bool mutableCopy, WritableBlob* outBlob) 1057{ 1058 if (len > INT32_MAX) { 1059 // don't accept size_t values which may have come from an 1060 // inadvertent conversion from a negative int. 1061 return BAD_VALUE; 1062 } 1063 1064 status_t status; 1065 if (!mAllowFds || len <= BLOB_INPLACE_LIMIT) { 1066 ALOGV("writeBlob: write in place"); 1067 status = writeInt32(BLOB_INPLACE); 1068 if (status) return status; 1069 1070 void* ptr = writeInplace(len); 1071 if (!ptr) return NO_MEMORY; 1072 1073 outBlob->init(-1, ptr, len, false); 1074 return NO_ERROR; 1075 } 1076 1077 ALOGV("writeBlob: write to ashmem"); 1078 int fd = ashmem_create_region("Parcel Blob", len); 1079 if (fd < 0) return NO_MEMORY; 1080 1081 mBlobAshmemSize += len; 1082 1083 int result = ashmem_set_prot_region(fd, PROT_READ | PROT_WRITE); 1084 if (result < 0) { 1085 status = result; 1086 } else { 1087 void* ptr = ::mmap(NULL, len, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0); 1088 if (ptr == MAP_FAILED) { 1089 status = -errno; 1090 } else { 1091 if (!mutableCopy) { 1092 result = ashmem_set_prot_region(fd, PROT_READ); 1093 } 1094 if (result < 0) { 1095 status = result; 1096 } else { 1097 status = writeInt32(mutableCopy ? BLOB_ASHMEM_MUTABLE : BLOB_ASHMEM_IMMUTABLE); 1098 if (!status) { 1099 status = writeFileDescriptor(fd, true /*takeOwnership*/); 1100 if (!status) { 1101 outBlob->init(fd, ptr, len, mutableCopy); 1102 return NO_ERROR; 1103 } 1104 } 1105 } 1106 } 1107 ::munmap(ptr, len); 1108 } 1109 ::close(fd); 1110 return status; 1111} 1112 1113status_t Parcel::writeDupImmutableBlobFileDescriptor(int fd) 1114{ 1115 // Must match up with what's done in writeBlob. 1116 if (!mAllowFds) return FDS_NOT_ALLOWED; 1117 status_t status = writeInt32(BLOB_ASHMEM_IMMUTABLE); 1118 if (status) return status; 1119 return writeDupFileDescriptor(fd); 1120} 1121 1122status_t Parcel::write(const FlattenableHelperInterface& val) 1123{ 1124 status_t err; 1125 1126 // size if needed 1127 const size_t len = val.getFlattenedSize(); 1128 const size_t fd_count = val.getFdCount(); 1129 1130 if ((len > INT32_MAX) || (fd_count > INT32_MAX)) { 1131 // don't accept size_t values which may have come from an 1132 // inadvertent conversion from a negative int. 1133 return BAD_VALUE; 1134 } 1135 1136 err = this->writeInt32(len); 1137 if (err) return err; 1138 1139 err = this->writeInt32(fd_count); 1140 if (err) return err; 1141 1142 // payload 1143 void* const buf = this->writeInplace(pad_size(len)); 1144 if (buf == NULL) 1145 return BAD_VALUE; 1146 1147 int* fds = NULL; 1148 if (fd_count) { 1149 fds = new int[fd_count]; 1150 } 1151 1152 err = val.flatten(buf, len, fds, fd_count); 1153 for (size_t i=0 ; i<fd_count && err==NO_ERROR ; i++) { 1154 err = this->writeDupFileDescriptor( fds[i] ); 1155 } 1156 1157 if (fd_count) { 1158 delete [] fds; 1159 } 1160 1161 return err; 1162} 1163 1164status_t Parcel::writeObject(const flat_binder_object& val, bool nullMetaData) 1165{ 1166 const bool enoughData = (mDataPos+sizeof(val)) <= mDataCapacity; 1167 const bool enoughObjects = mObjectsSize < mObjectsCapacity; 1168 if (enoughData && enoughObjects) { 1169restart_write: 1170 *reinterpret_cast<flat_binder_object*>(mData+mDataPos) = val; 1171 1172 // remember if it's a file descriptor 1173 if (val.type == BINDER_TYPE_FD) { 1174 if (!mAllowFds) { 1175 // fail before modifying our object index 1176 return FDS_NOT_ALLOWED; 1177 } 1178 mHasFds = mFdsKnown = true; 1179 } 1180 1181 // Need to write meta-data? 1182 if (nullMetaData || val.binder != 0) { 1183 mObjects[mObjectsSize] = mDataPos; 1184 acquire_object(ProcessState::self(), val, this); 1185 mObjectsSize++; 1186 } 1187 1188 return finishWrite(sizeof(flat_binder_object)); 1189 } 1190 1191 if (!enoughData) { 1192 const status_t err = growData(sizeof(val)); 1193 if (err != NO_ERROR) return err; 1194 } 1195 if (!enoughObjects) { 1196 size_t newSize = ((mObjectsSize+2)*3)/2; 1197 if (newSize < mObjectsSize) return NO_MEMORY; // overflow 1198 binder_size_t* objects = (binder_size_t*)realloc(mObjects, newSize*sizeof(binder_size_t)); 1199 if (objects == NULL) return NO_MEMORY; 1200 mObjects = objects; 1201 mObjectsCapacity = newSize; 1202 } 1203 1204 goto restart_write; 1205} 1206 1207status_t Parcel::writeNoException() 1208{ 1209 return writeInt32(0); 1210} 1211 1212void Parcel::remove(size_t /*start*/, size_t /*amt*/) 1213{ 1214 LOG_ALWAYS_FATAL("Parcel::remove() not yet implemented!"); 1215} 1216 1217status_t Parcel::read(void* outData, size_t len) const 1218{ 1219 if (len > INT32_MAX) { 1220 // don't accept size_t values which may have come from an 1221 // inadvertent conversion from a negative int. 1222 return BAD_VALUE; 1223 } 1224 1225 if ((mDataPos+pad_size(len)) >= mDataPos && (mDataPos+pad_size(len)) <= mDataSize 1226 && len <= pad_size(len)) { 1227 memcpy(outData, mData+mDataPos, len); 1228 mDataPos += pad_size(len); 1229 ALOGV("read Setting data pos of %p to %zu", this, mDataPos); 1230 return NO_ERROR; 1231 } 1232 return NOT_ENOUGH_DATA; 1233} 1234 1235const void* Parcel::readInplace(size_t len) const 1236{ 1237 if (len > INT32_MAX) { 1238 // don't accept size_t values which may have come from an 1239 // inadvertent conversion from a negative int. 1240 return NULL; 1241 } 1242 1243 if ((mDataPos+pad_size(len)) >= mDataPos && (mDataPos+pad_size(len)) <= mDataSize 1244 && len <= pad_size(len)) { 1245 const void* data = mData+mDataPos; 1246 mDataPos += pad_size(len); 1247 ALOGV("readInplace Setting data pos of %p to %zu", this, mDataPos); 1248 return data; 1249 } 1250 return NULL; 1251} 1252 1253template<class T> 1254status_t Parcel::readAligned(T *pArg) const { 1255 COMPILE_TIME_ASSERT_FUNCTION_SCOPE(PAD_SIZE_UNSAFE(sizeof(T)) == sizeof(T)); 1256 1257 if ((mDataPos+sizeof(T)) <= mDataSize) { 1258 const void* data = mData+mDataPos; 1259 mDataPos += sizeof(T); 1260 *pArg = *reinterpret_cast<const T*>(data); 1261 return NO_ERROR; 1262 } else { 1263 return NOT_ENOUGH_DATA; 1264 } 1265} 1266 1267template<class T> 1268T Parcel::readAligned() const { 1269 T result; 1270 if (readAligned(&result) != NO_ERROR) { 1271 result = 0; 1272 } 1273 1274 return result; 1275} 1276 1277template<class T> 1278status_t Parcel::writeAligned(T val) { 1279 COMPILE_TIME_ASSERT_FUNCTION_SCOPE(PAD_SIZE_UNSAFE(sizeof(T)) == sizeof(T)); 1280 1281 if ((mDataPos+sizeof(val)) <= mDataCapacity) { 1282restart_write: 1283 *reinterpret_cast<T*>(mData+mDataPos) = val; 1284 return finishWrite(sizeof(val)); 1285 } 1286 1287 status_t err = growData(sizeof(val)); 1288 if (err == NO_ERROR) goto restart_write; 1289 return err; 1290} 1291 1292status_t Parcel::readByteVector(std::vector<int8_t>* val) const { 1293 val->clear(); 1294 1295 int32_t size; 1296 status_t status = readInt32(&size); 1297 1298 if (status != OK) { 1299 return status; 1300 } 1301 1302 if (size < 0) { 1303 status = UNEXPECTED_NULL; 1304 return status; 1305 } 1306 if (size_t(size) > dataAvail()) { 1307 status = BAD_VALUE; 1308 return status; 1309 } 1310 1311 const void* data = readInplace(size); 1312 if (!data) { 1313 status = BAD_VALUE; 1314 return status; 1315 } 1316 val->resize(size); 1317 memcpy(val->data(), data, size); 1318 1319 return status; 1320} 1321 1322status_t Parcel::readInt32Vector(std::vector<int32_t>* val) const { 1323 return readTypedVector(val, this, &Parcel::readInt32); 1324} 1325 1326status_t Parcel::readInt64Vector(std::vector<int64_t>* val) const { 1327 return readTypedVector(val, this, &Parcel::readInt64); 1328} 1329 1330status_t Parcel::readFloatVector(std::vector<float>* val) const { 1331 return readTypedVector(val, this, &Parcel::readFloat); 1332} 1333 1334status_t Parcel::readDoubleVector(std::vector<double>* val) const { 1335 return readTypedVector(val, this, &Parcel::readDouble); 1336} 1337 1338status_t Parcel::readBoolVector(std::vector<bool>* val) const { 1339 val->clear(); 1340 1341 int32_t size; 1342 status_t status = readInt32(&size); 1343 1344 if (status != OK) { 1345 return status; 1346 } 1347 1348 if (size < 0) { 1349 return UNEXPECTED_NULL; 1350 } 1351 1352 val->resize(size); 1353 1354 /* C++ bool handling means a vector of bools isn't necessarily addressable 1355 * (we might use individual bits) 1356 */ 1357 bool data; 1358 for (int32_t i = 0; i < size; ++i) { 1359 status = readBool(&data); 1360 (*val)[i] = data; 1361 1362 if (status != OK) { 1363 return status; 1364 } 1365 } 1366 1367 return OK; 1368} 1369 1370status_t Parcel::readCharVector(std::vector<char16_t>* val) const { 1371 return readTypedVector(val, this, &Parcel::readChar); 1372} 1373 1374status_t Parcel::readString16Vector(std::vector<String16>* val) const { 1375 return readTypedVector(val, this, &Parcel::readString16); 1376} 1377 1378 1379status_t Parcel::readInt32(int32_t *pArg) const 1380{ 1381 return readAligned(pArg); 1382} 1383 1384int32_t Parcel::readInt32() const 1385{ 1386 return readAligned<int32_t>(); 1387} 1388 1389status_t Parcel::readUint32(uint32_t *pArg) const 1390{ 1391 return readAligned(pArg); 1392} 1393 1394uint32_t Parcel::readUint32() const 1395{ 1396 return readAligned<uint32_t>(); 1397} 1398 1399status_t Parcel::readInt64(int64_t *pArg) const 1400{ 1401 return readAligned(pArg); 1402} 1403 1404 1405int64_t Parcel::readInt64() const 1406{ 1407 return readAligned<int64_t>(); 1408} 1409 1410status_t Parcel::readUint64(uint64_t *pArg) const 1411{ 1412 return readAligned(pArg); 1413} 1414 1415uint64_t Parcel::readUint64() const 1416{ 1417 return readAligned<uint64_t>(); 1418} 1419 1420status_t Parcel::readPointer(uintptr_t *pArg) const 1421{ 1422 status_t ret; 1423 binder_uintptr_t ptr; 1424 ret = readAligned(&ptr); 1425 if (!ret) 1426 *pArg = ptr; 1427 return ret; 1428} 1429 1430uintptr_t Parcel::readPointer() const 1431{ 1432 return readAligned<binder_uintptr_t>(); 1433} 1434 1435 1436status_t Parcel::readFloat(float *pArg) const 1437{ 1438 return readAligned(pArg); 1439} 1440 1441 1442float Parcel::readFloat() const 1443{ 1444 return readAligned<float>(); 1445} 1446 1447#if defined(__mips__) && defined(__mips_hard_float) 1448 1449status_t Parcel::readDouble(double *pArg) const 1450{ 1451 union { 1452 double d; 1453 unsigned long long ll; 1454 } u; 1455 u.d = 0; 1456 status_t status; 1457 status = readAligned(&u.ll); 1458 *pArg = u.d; 1459 return status; 1460} 1461 1462double Parcel::readDouble() const 1463{ 1464 union { 1465 double d; 1466 unsigned long long ll; 1467 } u; 1468 u.ll = readAligned<unsigned long long>(); 1469 return u.d; 1470} 1471 1472#else 1473 1474status_t Parcel::readDouble(double *pArg) const 1475{ 1476 return readAligned(pArg); 1477} 1478 1479double Parcel::readDouble() const 1480{ 1481 return readAligned<double>(); 1482} 1483 1484#endif 1485 1486status_t Parcel::readIntPtr(intptr_t *pArg) const 1487{ 1488 return readAligned(pArg); 1489} 1490 1491 1492intptr_t Parcel::readIntPtr() const 1493{ 1494 return readAligned<intptr_t>(); 1495} 1496 1497status_t Parcel::readBool(bool *pArg) const 1498{ 1499 int32_t tmp; 1500 status_t ret = readInt32(&tmp); 1501 *pArg = (tmp != 0); 1502 return ret; 1503} 1504 1505bool Parcel::readBool() const 1506{ 1507 return readInt32() != 0; 1508} 1509 1510status_t Parcel::readChar(char16_t *pArg) const 1511{ 1512 int32_t tmp; 1513 status_t ret = readInt32(&tmp); 1514 *pArg = char16_t(tmp); 1515 return ret; 1516} 1517 1518char16_t Parcel::readChar() const 1519{ 1520 return char16_t(readInt32()); 1521} 1522 1523status_t Parcel::readByte(int8_t *pArg) const 1524{ 1525 int32_t tmp; 1526 status_t ret = readInt32(&tmp); 1527 *pArg = int8_t(tmp); 1528 return ret; 1529} 1530 1531int8_t Parcel::readByte() const 1532{ 1533 return int8_t(readInt32()); 1534} 1535 1536const char* Parcel::readCString() const 1537{ 1538 const size_t avail = mDataSize-mDataPos; 1539 if (avail > 0) { 1540 const char* str = reinterpret_cast<const char*>(mData+mDataPos); 1541 // is the string's trailing NUL within the parcel's valid bounds? 1542 const char* eos = reinterpret_cast<const char*>(memchr(str, 0, avail)); 1543 if (eos) { 1544 const size_t len = eos - str; 1545 mDataPos += pad_size(len+1); 1546 ALOGV("readCString Setting data pos of %p to %zu", this, mDataPos); 1547 return str; 1548 } 1549 } 1550 return NULL; 1551} 1552 1553String8 Parcel::readString8() const 1554{ 1555 int32_t size = readInt32(); 1556 // watch for potential int overflow adding 1 for trailing NUL 1557 if (size > 0 && size < INT32_MAX) { 1558 const char* str = (const char*)readInplace(size+1); 1559 if (str) return String8(str, size); 1560 } 1561 return String8(); 1562} 1563 1564String16 Parcel::readString16() const 1565{ 1566 size_t len; 1567 const char16_t* str = readString16Inplace(&len); 1568 if (str) return String16(str, len); 1569 ALOGE("Reading a NULL string not supported here."); 1570 return String16(); 1571} 1572 1573status_t Parcel::readString16(String16* pArg) const 1574{ 1575 size_t len; 1576 const char16_t* str = readString16Inplace(&len); 1577 if (str) { 1578 pArg->setTo(str, len); 1579 return 0; 1580 } else { 1581 *pArg = String16(); 1582 return UNEXPECTED_NULL; 1583 } 1584} 1585 1586const char16_t* Parcel::readString16Inplace(size_t* outLen) const 1587{ 1588 int32_t size = readInt32(); 1589 // watch for potential int overflow from size+1 1590 if (size >= 0 && size < INT32_MAX) { 1591 *outLen = size; 1592 const char16_t* str = (const char16_t*)readInplace((size+1)*sizeof(char16_t)); 1593 if (str != NULL) { 1594 return str; 1595 } 1596 } 1597 *outLen = 0; 1598 return NULL; 1599} 1600 1601status_t Parcel::readStrongBinder(sp<IBinder>* val) const 1602{ 1603 return unflatten_binder(ProcessState::self(), *this, val); 1604} 1605 1606sp<IBinder> Parcel::readStrongBinder() const 1607{ 1608 sp<IBinder> val; 1609 readStrongBinder(&val); 1610 return val; 1611} 1612 1613wp<IBinder> Parcel::readWeakBinder() const 1614{ 1615 wp<IBinder> val; 1616 unflatten_binder(ProcessState::self(), *this, &val); 1617 return val; 1618} 1619 1620int32_t Parcel::readExceptionCode() const 1621{ 1622 int32_t exception_code = readAligned<int32_t>(); 1623 if (exception_code == EX_HAS_REPLY_HEADER) { 1624 int32_t header_start = dataPosition(); 1625 int32_t header_size = readAligned<int32_t>(); 1626 // Skip over fat responses headers. Not used (or propagated) in 1627 // native code 1628 setDataPosition(header_start + header_size); 1629 // And fat response headers are currently only used when there are no 1630 // exceptions, so return no error: 1631 return 0; 1632 } 1633 return exception_code; 1634} 1635 1636native_handle* Parcel::readNativeHandle() const 1637{ 1638 int numFds, numInts; 1639 status_t err; 1640 err = readInt32(&numFds); 1641 if (err != NO_ERROR) return 0; 1642 err = readInt32(&numInts); 1643 if (err != NO_ERROR) return 0; 1644 1645 native_handle* h = native_handle_create(numFds, numInts); 1646 if (!h) { 1647 return 0; 1648 } 1649 1650 for (int i=0 ; err==NO_ERROR && i<numFds ; i++) { 1651 h->data[i] = dup(readFileDescriptor()); 1652 if (h->data[i] < 0) err = BAD_VALUE; 1653 } 1654 err = read(h->data + numFds, sizeof(int)*numInts); 1655 if (err != NO_ERROR) { 1656 native_handle_close(h); 1657 native_handle_delete(h); 1658 h = 0; 1659 } 1660 return h; 1661} 1662 1663 1664int Parcel::readFileDescriptor() const 1665{ 1666 const flat_binder_object* flat = readObject(true); 1667 if (flat) { 1668 switch (flat->type) { 1669 case BINDER_TYPE_FD: 1670 //ALOGI("Returning file descriptor %ld from parcel %p", flat->handle, this); 1671 return flat->handle; 1672 } 1673 } 1674 return BAD_TYPE; 1675} 1676 1677status_t Parcel::readBlob(size_t len, ReadableBlob* outBlob) const 1678{ 1679 int32_t blobType; 1680 status_t status = readInt32(&blobType); 1681 if (status) return status; 1682 1683 if (blobType == BLOB_INPLACE) { 1684 ALOGV("readBlob: read in place"); 1685 const void* ptr = readInplace(len); 1686 if (!ptr) return BAD_VALUE; 1687 1688 outBlob->init(-1, const_cast<void*>(ptr), len, false); 1689 return NO_ERROR; 1690 } 1691 1692 ALOGV("readBlob: read from ashmem"); 1693 bool isMutable = (blobType == BLOB_ASHMEM_MUTABLE); 1694 int fd = readFileDescriptor(); 1695 if (fd == int(BAD_TYPE)) return BAD_VALUE; 1696 1697 void* ptr = ::mmap(NULL, len, isMutable ? PROT_READ | PROT_WRITE : PROT_READ, 1698 MAP_SHARED, fd, 0); 1699 if (ptr == MAP_FAILED) return NO_MEMORY; 1700 1701 outBlob->init(fd, ptr, len, isMutable); 1702 return NO_ERROR; 1703} 1704 1705status_t Parcel::read(FlattenableHelperInterface& val) const 1706{ 1707 // size 1708 const size_t len = this->readInt32(); 1709 const size_t fd_count = this->readInt32(); 1710 1711 if (len > INT32_MAX) { 1712 // don't accept size_t values which may have come from an 1713 // inadvertent conversion from a negative int. 1714 return BAD_VALUE; 1715 } 1716 1717 // payload 1718 void const* const buf = this->readInplace(pad_size(len)); 1719 if (buf == NULL) 1720 return BAD_VALUE; 1721 1722 int* fds = NULL; 1723 if (fd_count) { 1724 fds = new int[fd_count]; 1725 } 1726 1727 status_t err = NO_ERROR; 1728 for (size_t i=0 ; i<fd_count && err==NO_ERROR ; i++) { 1729 fds[i] = dup(this->readFileDescriptor()); 1730 if (fds[i] < 0) { 1731 err = BAD_VALUE; 1732 ALOGE("dup() failed in Parcel::read, i is %zu, fds[i] is %d, fd_count is %zu, error: %s", 1733 i, fds[i], fd_count, strerror(errno)); 1734 } 1735 } 1736 1737 if (err == NO_ERROR) { 1738 err = val.unflatten(buf, len, fds, fd_count); 1739 } 1740 1741 if (fd_count) { 1742 delete [] fds; 1743 } 1744 1745 return err; 1746} 1747const flat_binder_object* Parcel::readObject(bool nullMetaData) const 1748{ 1749 const size_t DPOS = mDataPos; 1750 if ((DPOS+sizeof(flat_binder_object)) <= mDataSize) { 1751 const flat_binder_object* obj 1752 = reinterpret_cast<const flat_binder_object*>(mData+DPOS); 1753 mDataPos = DPOS + sizeof(flat_binder_object); 1754 if (!nullMetaData && (obj->cookie == 0 && obj->binder == 0)) { 1755 // When transferring a NULL object, we don't write it into 1756 // the object list, so we don't want to check for it when 1757 // reading. 1758 ALOGV("readObject Setting data pos of %p to %zu", this, mDataPos); 1759 return obj; 1760 } 1761 1762 // Ensure that this object is valid... 1763 binder_size_t* const OBJS = mObjects; 1764 const size_t N = mObjectsSize; 1765 size_t opos = mNextObjectHint; 1766 1767 if (N > 0) { 1768 ALOGV("Parcel %p looking for obj at %zu, hint=%zu", 1769 this, DPOS, opos); 1770 1771 // Start at the current hint position, looking for an object at 1772 // the current data position. 1773 if (opos < N) { 1774 while (opos < (N-1) && OBJS[opos] < DPOS) { 1775 opos++; 1776 } 1777 } else { 1778 opos = N-1; 1779 } 1780 if (OBJS[opos] == DPOS) { 1781 // Found it! 1782 ALOGV("Parcel %p found obj %zu at index %zu with forward search", 1783 this, DPOS, opos); 1784 mNextObjectHint = opos+1; 1785 ALOGV("readObject Setting data pos of %p to %zu", this, mDataPos); 1786 return obj; 1787 } 1788 1789 // Look backwards for it... 1790 while (opos > 0 && OBJS[opos] > DPOS) { 1791 opos--; 1792 } 1793 if (OBJS[opos] == DPOS) { 1794 // Found it! 1795 ALOGV("Parcel %p found obj %zu at index %zu with backward search", 1796 this, DPOS, opos); 1797 mNextObjectHint = opos+1; 1798 ALOGV("readObject Setting data pos of %p to %zu", this, mDataPos); 1799 return obj; 1800 } 1801 } 1802 ALOGW("Attempt to read object from Parcel %p at offset %zu that is not in the object list", 1803 this, DPOS); 1804 } 1805 return NULL; 1806} 1807 1808void Parcel::closeFileDescriptors() 1809{ 1810 size_t i = mObjectsSize; 1811 if (i > 0) { 1812 //ALOGI("Closing file descriptors for %zu objects...", i); 1813 } 1814 while (i > 0) { 1815 i--; 1816 const flat_binder_object* flat 1817 = reinterpret_cast<flat_binder_object*>(mData+mObjects[i]); 1818 if (flat->type == BINDER_TYPE_FD) { 1819 //ALOGI("Closing fd: %ld", flat->handle); 1820 close(flat->handle); 1821 } 1822 } 1823} 1824 1825uintptr_t Parcel::ipcData() const 1826{ 1827 return reinterpret_cast<uintptr_t>(mData); 1828} 1829 1830size_t Parcel::ipcDataSize() const 1831{ 1832 return (mDataSize > mDataPos ? mDataSize : mDataPos); 1833} 1834 1835uintptr_t Parcel::ipcObjects() const 1836{ 1837 return reinterpret_cast<uintptr_t>(mObjects); 1838} 1839 1840size_t Parcel::ipcObjectsCount() const 1841{ 1842 return mObjectsSize; 1843} 1844 1845void Parcel::ipcSetDataReference(const uint8_t* data, size_t dataSize, 1846 const binder_size_t* objects, size_t objectsCount, release_func relFunc, void* relCookie) 1847{ 1848 binder_size_t minOffset = 0; 1849 freeDataNoInit(); 1850 mError = NO_ERROR; 1851 mData = const_cast<uint8_t*>(data); 1852 mDataSize = mDataCapacity = dataSize; 1853 //ALOGI("setDataReference Setting data size of %p to %lu (pid=%d)", this, mDataSize, getpid()); 1854 mDataPos = 0; 1855 ALOGV("setDataReference Setting data pos of %p to %zu", this, mDataPos); 1856 mObjects = const_cast<binder_size_t*>(objects); 1857 mObjectsSize = mObjectsCapacity = objectsCount; 1858 mNextObjectHint = 0; 1859 mOwner = relFunc; 1860 mOwnerCookie = relCookie; 1861 for (size_t i = 0; i < mObjectsSize; i++) { 1862 binder_size_t offset = mObjects[i]; 1863 if (offset < minOffset) { 1864 ALOGE("%s: bad object offset %" PRIu64 " < %" PRIu64 "\n", 1865 __func__, (uint64_t)offset, (uint64_t)minOffset); 1866 mObjectsSize = 0; 1867 break; 1868 } 1869 minOffset = offset + sizeof(flat_binder_object); 1870 } 1871 scanForFds(); 1872} 1873 1874void Parcel::print(TextOutput& to, uint32_t /*flags*/) const 1875{ 1876 to << "Parcel("; 1877 1878 if (errorCheck() != NO_ERROR) { 1879 const status_t err = errorCheck(); 1880 to << "Error: " << (void*)(intptr_t)err << " \"" << strerror(-err) << "\""; 1881 } else if (dataSize() > 0) { 1882 const uint8_t* DATA = data(); 1883 to << indent << HexDump(DATA, dataSize()) << dedent; 1884 const binder_size_t* OBJS = objects(); 1885 const size_t N = objectsCount(); 1886 for (size_t i=0; i<N; i++) { 1887 const flat_binder_object* flat 1888 = reinterpret_cast<const flat_binder_object*>(DATA+OBJS[i]); 1889 to << endl << "Object #" << i << " @ " << (void*)OBJS[i] << ": " 1890 << TypeCode(flat->type & 0x7f7f7f00) 1891 << " = " << flat->binder; 1892 } 1893 } else { 1894 to << "NULL"; 1895 } 1896 1897 to << ")"; 1898} 1899 1900void Parcel::releaseObjects() 1901{ 1902 const sp<ProcessState> proc(ProcessState::self()); 1903 size_t i = mObjectsSize; 1904 uint8_t* const data = mData; 1905 binder_size_t* const objects = mObjects; 1906 while (i > 0) { 1907 i--; 1908 const flat_binder_object* flat 1909 = reinterpret_cast<flat_binder_object*>(data+objects[i]); 1910 release_object(proc, *flat, this); 1911 } 1912} 1913 1914void Parcel::acquireObjects() 1915{ 1916 const sp<ProcessState> proc(ProcessState::self()); 1917 size_t i = mObjectsSize; 1918 uint8_t* const data = mData; 1919 binder_size_t* const objects = mObjects; 1920 while (i > 0) { 1921 i--; 1922 const flat_binder_object* flat 1923 = reinterpret_cast<flat_binder_object*>(data+objects[i]); 1924 acquire_object(proc, *flat, this); 1925 } 1926} 1927 1928void Parcel::freeData() 1929{ 1930 freeDataNoInit(); 1931 initState(); 1932} 1933 1934void Parcel::freeDataNoInit() 1935{ 1936 if (mOwner) { 1937 LOG_ALLOC("Parcel %p: freeing other owner data", this); 1938 //ALOGI("Freeing data ref of %p (pid=%d)", this, getpid()); 1939 mOwner(this, mData, mDataSize, mObjects, mObjectsSize, mOwnerCookie); 1940 } else { 1941 LOG_ALLOC("Parcel %p: freeing allocated data", this); 1942 releaseObjects(); 1943 if (mData) { 1944 LOG_ALLOC("Parcel %p: freeing with %zu capacity", this, mDataCapacity); 1945 pthread_mutex_lock(&gParcelGlobalAllocSizeLock); 1946 if (mDataCapacity <= gParcelGlobalAllocSize) { 1947 gParcelGlobalAllocSize = gParcelGlobalAllocSize - mDataCapacity; 1948 } else { 1949 gParcelGlobalAllocSize = 0; 1950 } 1951 if (gParcelGlobalAllocCount > 0) { 1952 gParcelGlobalAllocCount--; 1953 } 1954 pthread_mutex_unlock(&gParcelGlobalAllocSizeLock); 1955 free(mData); 1956 } 1957 if (mObjects) free(mObjects); 1958 } 1959} 1960 1961status_t Parcel::growData(size_t len) 1962{ 1963 if (len > INT32_MAX) { 1964 // don't accept size_t values which may have come from an 1965 // inadvertent conversion from a negative int. 1966 return BAD_VALUE; 1967 } 1968 1969 size_t newSize = ((mDataSize+len)*3)/2; 1970 return (newSize <= mDataSize) 1971 ? (status_t) NO_MEMORY 1972 : continueWrite(newSize); 1973} 1974 1975status_t Parcel::restartWrite(size_t desired) 1976{ 1977 if (desired > INT32_MAX) { 1978 // don't accept size_t values which may have come from an 1979 // inadvertent conversion from a negative int. 1980 return BAD_VALUE; 1981 } 1982 1983 if (mOwner) { 1984 freeData(); 1985 return continueWrite(desired); 1986 } 1987 1988 uint8_t* data = (uint8_t*)realloc(mData, desired); 1989 if (!data && desired > mDataCapacity) { 1990 mError = NO_MEMORY; 1991 return NO_MEMORY; 1992 } 1993 1994 releaseObjects(); 1995 1996 if (data) { 1997 LOG_ALLOC("Parcel %p: restart from %zu to %zu capacity", this, mDataCapacity, desired); 1998 pthread_mutex_lock(&gParcelGlobalAllocSizeLock); 1999 gParcelGlobalAllocSize += desired; 2000 gParcelGlobalAllocSize -= mDataCapacity; 2001 pthread_mutex_unlock(&gParcelGlobalAllocSizeLock); 2002 mData = data; 2003 mDataCapacity = desired; 2004 } 2005 2006 mDataSize = mDataPos = 0; 2007 ALOGV("restartWrite Setting data size of %p to %zu", this, mDataSize); 2008 ALOGV("restartWrite Setting data pos of %p to %zu", this, mDataPos); 2009 2010 free(mObjects); 2011 mObjects = NULL; 2012 mObjectsSize = mObjectsCapacity = 0; 2013 mNextObjectHint = 0; 2014 mHasFds = false; 2015 mFdsKnown = true; 2016 mAllowFds = true; 2017 2018 return NO_ERROR; 2019} 2020 2021status_t Parcel::continueWrite(size_t desired) 2022{ 2023 if (desired > INT32_MAX) { 2024 // don't accept size_t values which may have come from an 2025 // inadvertent conversion from a negative int. 2026 return BAD_VALUE; 2027 } 2028 2029 // If shrinking, first adjust for any objects that appear 2030 // after the new data size. 2031 size_t objectsSize = mObjectsSize; 2032 if (desired < mDataSize) { 2033 if (desired == 0) { 2034 objectsSize = 0; 2035 } else { 2036 while (objectsSize > 0) { 2037 if (mObjects[objectsSize-1] < desired) 2038 break; 2039 objectsSize--; 2040 } 2041 } 2042 } 2043 2044 if (mOwner) { 2045 // If the size is going to zero, just release the owner's data. 2046 if (desired == 0) { 2047 freeData(); 2048 return NO_ERROR; 2049 } 2050 2051 // If there is a different owner, we need to take 2052 // posession. 2053 uint8_t* data = (uint8_t*)malloc(desired); 2054 if (!data) { 2055 mError = NO_MEMORY; 2056 return NO_MEMORY; 2057 } 2058 binder_size_t* objects = NULL; 2059 2060 if (objectsSize) { 2061 objects = (binder_size_t*)calloc(objectsSize, sizeof(binder_size_t)); 2062 if (!objects) { 2063 free(data); 2064 2065 mError = NO_MEMORY; 2066 return NO_MEMORY; 2067 } 2068 2069 // Little hack to only acquire references on objects 2070 // we will be keeping. 2071 size_t oldObjectsSize = mObjectsSize; 2072 mObjectsSize = objectsSize; 2073 acquireObjects(); 2074 mObjectsSize = oldObjectsSize; 2075 } 2076 2077 if (mData) { 2078 memcpy(data, mData, mDataSize < desired ? mDataSize : desired); 2079 } 2080 if (objects && mObjects) { 2081 memcpy(objects, mObjects, objectsSize*sizeof(binder_size_t)); 2082 } 2083 //ALOGI("Freeing data ref of %p (pid=%d)", this, getpid()); 2084 mOwner(this, mData, mDataSize, mObjects, mObjectsSize, mOwnerCookie); 2085 mOwner = NULL; 2086 2087 LOG_ALLOC("Parcel %p: taking ownership of %zu capacity", this, desired); 2088 pthread_mutex_lock(&gParcelGlobalAllocSizeLock); 2089 gParcelGlobalAllocSize += desired; 2090 gParcelGlobalAllocCount++; 2091 pthread_mutex_unlock(&gParcelGlobalAllocSizeLock); 2092 2093 mData = data; 2094 mObjects = objects; 2095 mDataSize = (mDataSize < desired) ? mDataSize : desired; 2096 ALOGV("continueWrite Setting data size of %p to %zu", this, mDataSize); 2097 mDataCapacity = desired; 2098 mObjectsSize = mObjectsCapacity = objectsSize; 2099 mNextObjectHint = 0; 2100 2101 } else if (mData) { 2102 if (objectsSize < mObjectsSize) { 2103 // Need to release refs on any objects we are dropping. 2104 const sp<ProcessState> proc(ProcessState::self()); 2105 for (size_t i=objectsSize; i<mObjectsSize; i++) { 2106 const flat_binder_object* flat 2107 = reinterpret_cast<flat_binder_object*>(mData+mObjects[i]); 2108 if (flat->type == BINDER_TYPE_FD) { 2109 // will need to rescan because we may have lopped off the only FDs 2110 mFdsKnown = false; 2111 } 2112 release_object(proc, *flat, this); 2113 } 2114 binder_size_t* objects = 2115 (binder_size_t*)realloc(mObjects, objectsSize*sizeof(binder_size_t)); 2116 if (objects) { 2117 mObjects = objects; 2118 } 2119 mObjectsSize = objectsSize; 2120 mNextObjectHint = 0; 2121 } 2122 2123 // We own the data, so we can just do a realloc(). 2124 if (desired > mDataCapacity) { 2125 uint8_t* data = (uint8_t*)realloc(mData, desired); 2126 if (data) { 2127 LOG_ALLOC("Parcel %p: continue from %zu to %zu capacity", this, mDataCapacity, 2128 desired); 2129 pthread_mutex_lock(&gParcelGlobalAllocSizeLock); 2130 gParcelGlobalAllocSize += desired; 2131 gParcelGlobalAllocSize -= mDataCapacity; 2132 gParcelGlobalAllocCount++; 2133 pthread_mutex_unlock(&gParcelGlobalAllocSizeLock); 2134 mData = data; 2135 mDataCapacity = desired; 2136 } else if (desired > mDataCapacity) { 2137 mError = NO_MEMORY; 2138 return NO_MEMORY; 2139 } 2140 } else { 2141 if (mDataSize > desired) { 2142 mDataSize = desired; 2143 ALOGV("continueWrite Setting data size of %p to %zu", this, mDataSize); 2144 } 2145 if (mDataPos > desired) { 2146 mDataPos = desired; 2147 ALOGV("continueWrite Setting data pos of %p to %zu", this, mDataPos); 2148 } 2149 } 2150 2151 } else { 2152 // This is the first data. Easy! 2153 uint8_t* data = (uint8_t*)malloc(desired); 2154 if (!data) { 2155 mError = NO_MEMORY; 2156 return NO_MEMORY; 2157 } 2158 2159 if(!(mDataCapacity == 0 && mObjects == NULL 2160 && mObjectsCapacity == 0)) { 2161 ALOGE("continueWrite: %zu/%p/%zu/%zu", mDataCapacity, mObjects, mObjectsCapacity, desired); 2162 } 2163 2164 LOG_ALLOC("Parcel %p: allocating with %zu capacity", this, desired); 2165 pthread_mutex_lock(&gParcelGlobalAllocSizeLock); 2166 gParcelGlobalAllocSize += desired; 2167 gParcelGlobalAllocCount++; 2168 pthread_mutex_unlock(&gParcelGlobalAllocSizeLock); 2169 2170 mData = data; 2171 mDataSize = mDataPos = 0; 2172 ALOGV("continueWrite Setting data size of %p to %zu", this, mDataSize); 2173 ALOGV("continueWrite Setting data pos of %p to %zu", this, mDataPos); 2174 mDataCapacity = desired; 2175 } 2176 2177 return NO_ERROR; 2178} 2179 2180void Parcel::initState() 2181{ 2182 LOG_ALLOC("Parcel %p: initState", this); 2183 mError = NO_ERROR; 2184 mData = 0; 2185 mDataSize = 0; 2186 mDataCapacity = 0; 2187 mDataPos = 0; 2188 ALOGV("initState Setting data size of %p to %zu", this, mDataSize); 2189 ALOGV("initState Setting data pos of %p to %zu", this, mDataPos); 2190 mObjects = NULL; 2191 mObjectsSize = 0; 2192 mObjectsCapacity = 0; 2193 mNextObjectHint = 0; 2194 mHasFds = false; 2195 mFdsKnown = true; 2196 mAllowFds = true; 2197 mOwner = NULL; 2198 mBlobAshmemSize = 0; 2199} 2200 2201void Parcel::scanForFds() const 2202{ 2203 bool hasFds = false; 2204 for (size_t i=0; i<mObjectsSize; i++) { 2205 const flat_binder_object* flat 2206 = reinterpret_cast<const flat_binder_object*>(mData + mObjects[i]); 2207 if (flat->type == BINDER_TYPE_FD) { 2208 hasFds = true; 2209 break; 2210 } 2211 } 2212 mHasFds = hasFds; 2213 mFdsKnown = true; 2214} 2215 2216size_t Parcel::getBlobAshmemSize() const 2217{ 2218 return mBlobAshmemSize; 2219} 2220 2221// --- Parcel::Blob --- 2222 2223Parcel::Blob::Blob() : 2224 mFd(-1), mData(NULL), mSize(0), mMutable(false) { 2225} 2226 2227Parcel::Blob::~Blob() { 2228 release(); 2229} 2230 2231void Parcel::Blob::release() { 2232 if (mFd != -1 && mData) { 2233 ::munmap(mData, mSize); 2234 } 2235 clear(); 2236} 2237 2238void Parcel::Blob::init(int fd, void* data, size_t size, bool isMutable) { 2239 mFd = fd; 2240 mData = data; 2241 mSize = size; 2242 mMutable = isMutable; 2243} 2244 2245void Parcel::Blob::clear() { 2246 mFd = -1; 2247 mData = NULL; 2248 mSize = 0; 2249 mMutable = false; 2250} 2251 2252}; // namespace android 2253