Parcel.cpp revision 4db672ddccb02b2bd70ddb4812c00a5060174c93
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 753status_t Parcel::writeByteVector(const std::vector<int8_t>& val) 754{ 755 status_t status; 756 if (val.size() > std::numeric_limits<int32_t>::max()) { 757 status = BAD_VALUE; 758 return status; 759 } 760 761 status = writeInt32(val.size()); 762 if (status != OK) { 763 return status; 764 } 765 766 void* data = writeInplace(val.size()); 767 if (!data) { 768 status = BAD_VALUE; 769 return status; 770 } 771 772 memcpy(data, val.data(), val.size()); 773 return status; 774} 775 776status_t Parcel::writeInt32Vector(const std::vector<int32_t>& val) 777{ 778 if (val.size() > std::numeric_limits<int32_t>::max()) { 779 return BAD_VALUE; 780 } 781 782 status_t status = writeInt32(val.size()); 783 784 if (status != OK) { 785 return status; 786 } 787 788 for (const auto& item : val) { 789 status = writeInt32(item); 790 791 if (status != OK) { 792 return status; 793 } 794 } 795 796 return OK; 797} 798 799status_t Parcel::writeInt64Vector(const std::vector<int64_t>& val) 800{ 801 if (val.size() > std::numeric_limits<int32_t>::max()) { 802 return BAD_VALUE; 803 } 804 805 status_t status = writeInt32(val.size()); 806 807 if (status != OK) { 808 return status; 809 } 810 811 for (const auto& item : val) { 812 status = writeInt64(item); 813 814 if (status != OK) { 815 return status; 816 } 817 } 818 819 return OK; 820} 821 822status_t Parcel::writeFloatVector(const std::vector<float>& val) 823{ 824 if (val.size() > std::numeric_limits<int32_t>::max()) { 825 return BAD_VALUE; 826 } 827 828 status_t status = writeInt32(val.size()); 829 830 if (status != OK) { 831 return status; 832 } 833 834 for (const auto& item : val) { 835 status = writeFloat(item); 836 837 if (status != OK) { 838 return status; 839 } 840 } 841 842 return OK; 843} 844 845status_t Parcel::writeDoubleVector(const std::vector<double>& val) 846{ 847 if (val.size() > std::numeric_limits<int32_t>::max()) { 848 return BAD_VALUE; 849 } 850 851 status_t status = writeInt32(val.size()); 852 853 if (status != OK) { 854 return status; 855 } 856 857 for (const auto& item : val) { 858 status = writeDouble(item); 859 860 if (status != OK) { 861 return status; 862 } 863 } 864 865 return OK; 866} 867 868status_t Parcel::writeBoolVector(const std::vector<bool>& val) 869{ 870 if (val.size() > std::numeric_limits<int32_t>::max()) { 871 return BAD_VALUE; 872 } 873 874 status_t status = writeInt32(val.size()); 875 876 if (status != OK) { 877 return status; 878 } 879 880 for (const auto& item : val) { 881 status = writeBool(item); 882 883 if (status != OK) { 884 return status; 885 } 886 } 887 888 return OK; 889} 890 891status_t Parcel::writeCharVector(const std::vector<char16_t>& val) 892{ 893 if (val.size() > std::numeric_limits<int32_t>::max()) { 894 return BAD_VALUE; 895 } 896 897 status_t status = writeInt32(val.size()); 898 899 if (status != OK) { 900 return status; 901 } 902 903 for (const auto& item : val) { 904 status = writeChar(item); 905 906 if (status != OK) { 907 return status; 908 } 909 } 910 911 return OK; 912} 913 914status_t Parcel::writeString16Vector(const std::vector<String16>& val) 915{ 916 if (val.size() > std::numeric_limits<int32_t>::max()) { 917 return BAD_VALUE; 918 } 919 920 status_t status = writeInt32(val.size()); 921 922 if (status != OK) { 923 return status; 924 } 925 926 for (const auto& item : val) { 927 status = writeString16(item); 928 929 if (status != OK) { 930 return status; 931 } 932 } 933 934 return OK; 935} 936 937status_t Parcel::writeInt32(int32_t val) 938{ 939 return writeAligned(val); 940} 941 942status_t Parcel::writeUint32(uint32_t val) 943{ 944 return writeAligned(val); 945} 946 947status_t Parcel::writeInt32Array(size_t len, const int32_t *val) { 948 if (len > INT32_MAX) { 949 // don't accept size_t values which may have come from an 950 // inadvertent conversion from a negative int. 951 return BAD_VALUE; 952 } 953 954 if (!val) { 955 return writeInt32(-1); 956 } 957 status_t ret = writeInt32(static_cast<uint32_t>(len)); 958 if (ret == NO_ERROR) { 959 ret = write(val, len * sizeof(*val)); 960 } 961 return ret; 962} 963status_t Parcel::writeByteArray(size_t len, const uint8_t *val) { 964 if (len > INT32_MAX) { 965 // don't accept size_t values which may have come from an 966 // inadvertent conversion from a negative int. 967 return BAD_VALUE; 968 } 969 970 if (!val) { 971 return writeInt32(-1); 972 } 973 status_t ret = writeInt32(static_cast<uint32_t>(len)); 974 if (ret == NO_ERROR) { 975 ret = write(val, len * sizeof(*val)); 976 } 977 return ret; 978} 979 980status_t Parcel::writeBool(bool val) 981{ 982 return writeInt32(int32_t(val)); 983} 984 985status_t Parcel::writeChar(char16_t val) 986{ 987 return writeInt32(int32_t(val)); 988} 989 990status_t Parcel::writeByte(int8_t val) 991{ 992 return writeInt32(int32_t(val)); 993} 994 995status_t Parcel::writeInt64(int64_t val) 996{ 997 return writeAligned(val); 998} 999 1000status_t Parcel::writeUint64(uint64_t val) 1001{ 1002 return writeAligned(val); 1003} 1004 1005status_t Parcel::writePointer(uintptr_t val) 1006{ 1007 return writeAligned<binder_uintptr_t>(val); 1008} 1009 1010status_t Parcel::writeFloat(float val) 1011{ 1012 return writeAligned(val); 1013} 1014 1015#if defined(__mips__) && defined(__mips_hard_float) 1016 1017status_t Parcel::writeDouble(double val) 1018{ 1019 union { 1020 double d; 1021 unsigned long long ll; 1022 } u; 1023 u.d = val; 1024 return writeAligned(u.ll); 1025} 1026 1027#else 1028 1029status_t Parcel::writeDouble(double val) 1030{ 1031 return writeAligned(val); 1032} 1033 1034#endif 1035 1036status_t Parcel::writeCString(const char* str) 1037{ 1038 return write(str, strlen(str)+1); 1039} 1040 1041status_t Parcel::writeString8(const String8& str) 1042{ 1043 status_t err = writeInt32(str.bytes()); 1044 // only write string if its length is more than zero characters, 1045 // as readString8 will only read if the length field is non-zero. 1046 // this is slightly different from how writeString16 works. 1047 if (str.bytes() > 0 && err == NO_ERROR) { 1048 err = write(str.string(), str.bytes()+1); 1049 } 1050 return err; 1051} 1052 1053status_t Parcel::writeString16(const String16& str) 1054{ 1055 return writeString16(str.string(), str.size()); 1056} 1057 1058status_t Parcel::writeString16(const char16_t* str, size_t len) 1059{ 1060 if (str == NULL) return writeInt32(-1); 1061 1062 status_t err = writeInt32(len); 1063 if (err == NO_ERROR) { 1064 len *= sizeof(char16_t); 1065 uint8_t* data = (uint8_t*)writeInplace(len+sizeof(char16_t)); 1066 if (data) { 1067 memcpy(data, str, len); 1068 *reinterpret_cast<char16_t*>(data+len) = 0; 1069 return NO_ERROR; 1070 } 1071 err = mError; 1072 } 1073 return err; 1074} 1075 1076status_t Parcel::writeStrongBinder(const sp<IBinder>& val) 1077{ 1078 return flatten_binder(ProcessState::self(), val, this); 1079} 1080 1081status_t Parcel::writeStrongBinderVector(const std::vector<sp<IBinder>>& val) 1082{ 1083 if (val.size() > std::numeric_limits<int32_t>::max()) { 1084 return BAD_VALUE; 1085 } 1086 1087 status_t status = writeInt32(val.size()); 1088 1089 if (status != OK) { 1090 return status; 1091 } 1092 1093 for (const auto& item : val) { 1094 status = writeStrongBinder(item); 1095 1096 if (status != OK) { 1097 return status; 1098 } 1099 } 1100 1101 return OK; 1102} 1103 1104status_t Parcel::readStrongBinderVector(std::vector<sp<IBinder>>* val) const { 1105 return readTypedVector(val, this, &Parcel::readStrongBinder); 1106} 1107 1108status_t Parcel::writeWeakBinder(const wp<IBinder>& val) 1109{ 1110 return flatten_binder(ProcessState::self(), val, this); 1111} 1112 1113status_t Parcel::writeNativeHandle(const native_handle* handle) 1114{ 1115 if (!handle || handle->version != sizeof(native_handle)) 1116 return BAD_TYPE; 1117 1118 status_t err; 1119 err = writeInt32(handle->numFds); 1120 if (err != NO_ERROR) return err; 1121 1122 err = writeInt32(handle->numInts); 1123 if (err != NO_ERROR) return err; 1124 1125 for (int i=0 ; err==NO_ERROR && i<handle->numFds ; i++) 1126 err = writeDupFileDescriptor(handle->data[i]); 1127 1128 if (err != NO_ERROR) { 1129 ALOGD("write native handle, write dup fd failed"); 1130 return err; 1131 } 1132 err = write(handle->data + handle->numFds, sizeof(int)*handle->numInts); 1133 return err; 1134} 1135 1136status_t Parcel::writeFileDescriptor(int fd, bool takeOwnership) 1137{ 1138 flat_binder_object obj; 1139 obj.type = BINDER_TYPE_FD; 1140 obj.flags = 0x7f | FLAT_BINDER_FLAG_ACCEPTS_FDS; 1141 obj.binder = 0; /* Don't pass uninitialized stack data to a remote process */ 1142 obj.handle = fd; 1143 obj.cookie = takeOwnership ? 1 : 0; 1144 return writeObject(obj, true); 1145} 1146 1147status_t Parcel::writeDupFileDescriptor(int fd) 1148{ 1149 int dupFd = dup(fd); 1150 if (dupFd < 0) { 1151 return -errno; 1152 } 1153 status_t err = writeFileDescriptor(dupFd, true /*takeOwnership*/); 1154 if (err) { 1155 close(dupFd); 1156 } 1157 return err; 1158} 1159 1160status_t Parcel::writeBlob(size_t len, bool mutableCopy, WritableBlob* outBlob) 1161{ 1162 if (len > INT32_MAX) { 1163 // don't accept size_t values which may have come from an 1164 // inadvertent conversion from a negative int. 1165 return BAD_VALUE; 1166 } 1167 1168 status_t status; 1169 if (!mAllowFds || len <= BLOB_INPLACE_LIMIT) { 1170 ALOGV("writeBlob: write in place"); 1171 status = writeInt32(BLOB_INPLACE); 1172 if (status) return status; 1173 1174 void* ptr = writeInplace(len); 1175 if (!ptr) return NO_MEMORY; 1176 1177 outBlob->init(-1, ptr, len, false); 1178 return NO_ERROR; 1179 } 1180 1181 ALOGV("writeBlob: write to ashmem"); 1182 int fd = ashmem_create_region("Parcel Blob", len); 1183 if (fd < 0) return NO_MEMORY; 1184 1185 mBlobAshmemSize += len; 1186 1187 int result = ashmem_set_prot_region(fd, PROT_READ | PROT_WRITE); 1188 if (result < 0) { 1189 status = result; 1190 } else { 1191 void* ptr = ::mmap(NULL, len, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0); 1192 if (ptr == MAP_FAILED) { 1193 status = -errno; 1194 } else { 1195 if (!mutableCopy) { 1196 result = ashmem_set_prot_region(fd, PROT_READ); 1197 } 1198 if (result < 0) { 1199 status = result; 1200 } else { 1201 status = writeInt32(mutableCopy ? BLOB_ASHMEM_MUTABLE : BLOB_ASHMEM_IMMUTABLE); 1202 if (!status) { 1203 status = writeFileDescriptor(fd, true /*takeOwnership*/); 1204 if (!status) { 1205 outBlob->init(fd, ptr, len, mutableCopy); 1206 return NO_ERROR; 1207 } 1208 } 1209 } 1210 } 1211 ::munmap(ptr, len); 1212 } 1213 ::close(fd); 1214 return status; 1215} 1216 1217status_t Parcel::writeDupImmutableBlobFileDescriptor(int fd) 1218{ 1219 // Must match up with what's done in writeBlob. 1220 if (!mAllowFds) return FDS_NOT_ALLOWED; 1221 status_t status = writeInt32(BLOB_ASHMEM_IMMUTABLE); 1222 if (status) return status; 1223 return writeDupFileDescriptor(fd); 1224} 1225 1226status_t Parcel::write(const FlattenableHelperInterface& val) 1227{ 1228 status_t err; 1229 1230 // size if needed 1231 const size_t len = val.getFlattenedSize(); 1232 const size_t fd_count = val.getFdCount(); 1233 1234 if ((len > INT32_MAX) || (fd_count > INT32_MAX)) { 1235 // don't accept size_t values which may have come from an 1236 // inadvertent conversion from a negative int. 1237 return BAD_VALUE; 1238 } 1239 1240 err = this->writeInt32(len); 1241 if (err) return err; 1242 1243 err = this->writeInt32(fd_count); 1244 if (err) return err; 1245 1246 // payload 1247 void* const buf = this->writeInplace(pad_size(len)); 1248 if (buf == NULL) 1249 return BAD_VALUE; 1250 1251 int* fds = NULL; 1252 if (fd_count) { 1253 fds = new int[fd_count]; 1254 } 1255 1256 err = val.flatten(buf, len, fds, fd_count); 1257 for (size_t i=0 ; i<fd_count && err==NO_ERROR ; i++) { 1258 err = this->writeDupFileDescriptor( fds[i] ); 1259 } 1260 1261 if (fd_count) { 1262 delete [] fds; 1263 } 1264 1265 return err; 1266} 1267 1268status_t Parcel::writeObject(const flat_binder_object& val, bool nullMetaData) 1269{ 1270 const bool enoughData = (mDataPos+sizeof(val)) <= mDataCapacity; 1271 const bool enoughObjects = mObjectsSize < mObjectsCapacity; 1272 if (enoughData && enoughObjects) { 1273restart_write: 1274 *reinterpret_cast<flat_binder_object*>(mData+mDataPos) = val; 1275 1276 // remember if it's a file descriptor 1277 if (val.type == BINDER_TYPE_FD) { 1278 if (!mAllowFds) { 1279 // fail before modifying our object index 1280 return FDS_NOT_ALLOWED; 1281 } 1282 mHasFds = mFdsKnown = true; 1283 } 1284 1285 // Need to write meta-data? 1286 if (nullMetaData || val.binder != 0) { 1287 mObjects[mObjectsSize] = mDataPos; 1288 acquire_object(ProcessState::self(), val, this); 1289 mObjectsSize++; 1290 } 1291 1292 return finishWrite(sizeof(flat_binder_object)); 1293 } 1294 1295 if (!enoughData) { 1296 const status_t err = growData(sizeof(val)); 1297 if (err != NO_ERROR) return err; 1298 } 1299 if (!enoughObjects) { 1300 size_t newSize = ((mObjectsSize+2)*3)/2; 1301 if (newSize < mObjectsSize) return NO_MEMORY; // overflow 1302 binder_size_t* objects = (binder_size_t*)realloc(mObjects, newSize*sizeof(binder_size_t)); 1303 if (objects == NULL) return NO_MEMORY; 1304 mObjects = objects; 1305 mObjectsCapacity = newSize; 1306 } 1307 1308 goto restart_write; 1309} 1310 1311status_t Parcel::writeNoException() 1312{ 1313 return writeInt32(0); 1314} 1315 1316void Parcel::remove(size_t /*start*/, size_t /*amt*/) 1317{ 1318 LOG_ALWAYS_FATAL("Parcel::remove() not yet implemented!"); 1319} 1320 1321status_t Parcel::read(void* outData, size_t len) const 1322{ 1323 if (len > INT32_MAX) { 1324 // don't accept size_t values which may have come from an 1325 // inadvertent conversion from a negative int. 1326 return BAD_VALUE; 1327 } 1328 1329 if ((mDataPos+pad_size(len)) >= mDataPos && (mDataPos+pad_size(len)) <= mDataSize 1330 && len <= pad_size(len)) { 1331 memcpy(outData, mData+mDataPos, len); 1332 mDataPos += pad_size(len); 1333 ALOGV("read Setting data pos of %p to %zu", this, mDataPos); 1334 return NO_ERROR; 1335 } 1336 return NOT_ENOUGH_DATA; 1337} 1338 1339const void* Parcel::readInplace(size_t len) const 1340{ 1341 if (len > INT32_MAX) { 1342 // don't accept size_t values which may have come from an 1343 // inadvertent conversion from a negative int. 1344 return NULL; 1345 } 1346 1347 if ((mDataPos+pad_size(len)) >= mDataPos && (mDataPos+pad_size(len)) <= mDataSize 1348 && len <= pad_size(len)) { 1349 const void* data = mData+mDataPos; 1350 mDataPos += pad_size(len); 1351 ALOGV("readInplace Setting data pos of %p to %zu", this, mDataPos); 1352 return data; 1353 } 1354 return NULL; 1355} 1356 1357template<class T> 1358status_t Parcel::readAligned(T *pArg) const { 1359 COMPILE_TIME_ASSERT_FUNCTION_SCOPE(PAD_SIZE_UNSAFE(sizeof(T)) == sizeof(T)); 1360 1361 if ((mDataPos+sizeof(T)) <= mDataSize) { 1362 const void* data = mData+mDataPos; 1363 mDataPos += sizeof(T); 1364 *pArg = *reinterpret_cast<const T*>(data); 1365 return NO_ERROR; 1366 } else { 1367 return NOT_ENOUGH_DATA; 1368 } 1369} 1370 1371template<class T> 1372T Parcel::readAligned() const { 1373 T result; 1374 if (readAligned(&result) != NO_ERROR) { 1375 result = 0; 1376 } 1377 1378 return result; 1379} 1380 1381template<class T> 1382status_t Parcel::writeAligned(T val) { 1383 COMPILE_TIME_ASSERT_FUNCTION_SCOPE(PAD_SIZE_UNSAFE(sizeof(T)) == sizeof(T)); 1384 1385 if ((mDataPos+sizeof(val)) <= mDataCapacity) { 1386restart_write: 1387 *reinterpret_cast<T*>(mData+mDataPos) = val; 1388 return finishWrite(sizeof(val)); 1389 } 1390 1391 status_t err = growData(sizeof(val)); 1392 if (err == NO_ERROR) goto restart_write; 1393 return err; 1394} 1395 1396status_t Parcel::readByteVector(std::vector<int8_t>* val) const { 1397 val->clear(); 1398 1399 int32_t size; 1400 status_t status = readInt32(&size); 1401 1402 if (status != OK) { 1403 return status; 1404 } 1405 1406 if (size < 0) { 1407 status = UNEXPECTED_NULL; 1408 return status; 1409 } 1410 if (size_t(size) > dataAvail()) { 1411 status = BAD_VALUE; 1412 return status; 1413 } 1414 1415 const void* data = readInplace(size); 1416 if (!data) { 1417 status = BAD_VALUE; 1418 return status; 1419 } 1420 val->resize(size); 1421 memcpy(val->data(), data, size); 1422 1423 return status; 1424} 1425 1426status_t Parcel::readInt32Vector(std::vector<int32_t>* val) const { 1427 return readTypedVector(val, this, &Parcel::readInt32); 1428} 1429 1430status_t Parcel::readInt64Vector(std::vector<int64_t>* val) const { 1431 return readTypedVector(val, this, &Parcel::readInt64); 1432} 1433 1434status_t Parcel::readFloatVector(std::vector<float>* val) const { 1435 return readTypedVector(val, this, &Parcel::readFloat); 1436} 1437 1438status_t Parcel::readDoubleVector(std::vector<double>* val) const { 1439 return readTypedVector(val, this, &Parcel::readDouble); 1440} 1441 1442status_t Parcel::readBoolVector(std::vector<bool>* val) const { 1443 val->clear(); 1444 1445 int32_t size; 1446 status_t status = readInt32(&size); 1447 1448 if (status != OK) { 1449 return status; 1450 } 1451 1452 if (size < 0) { 1453 return UNEXPECTED_NULL; 1454 } 1455 1456 val->resize(size); 1457 1458 /* C++ bool handling means a vector of bools isn't necessarily addressable 1459 * (we might use individual bits) 1460 */ 1461 bool data; 1462 for (int32_t i = 0; i < size; ++i) { 1463 status = readBool(&data); 1464 (*val)[i] = data; 1465 1466 if (status != OK) { 1467 return status; 1468 } 1469 } 1470 1471 return OK; 1472} 1473 1474status_t Parcel::readCharVector(std::vector<char16_t>* val) const { 1475 return readTypedVector(val, this, &Parcel::readChar); 1476} 1477 1478status_t Parcel::readString16Vector(std::vector<String16>* val) const { 1479 return readTypedVector(val, this, &Parcel::readString16); 1480} 1481 1482 1483status_t Parcel::readInt32(int32_t *pArg) const 1484{ 1485 return readAligned(pArg); 1486} 1487 1488int32_t Parcel::readInt32() const 1489{ 1490 return readAligned<int32_t>(); 1491} 1492 1493status_t Parcel::readUint32(uint32_t *pArg) const 1494{ 1495 return readAligned(pArg); 1496} 1497 1498uint32_t Parcel::readUint32() const 1499{ 1500 return readAligned<uint32_t>(); 1501} 1502 1503status_t Parcel::readInt64(int64_t *pArg) const 1504{ 1505 return readAligned(pArg); 1506} 1507 1508 1509int64_t Parcel::readInt64() const 1510{ 1511 return readAligned<int64_t>(); 1512} 1513 1514status_t Parcel::readUint64(uint64_t *pArg) const 1515{ 1516 return readAligned(pArg); 1517} 1518 1519uint64_t Parcel::readUint64() const 1520{ 1521 return readAligned<uint64_t>(); 1522} 1523 1524status_t Parcel::readPointer(uintptr_t *pArg) const 1525{ 1526 status_t ret; 1527 binder_uintptr_t ptr; 1528 ret = readAligned(&ptr); 1529 if (!ret) 1530 *pArg = ptr; 1531 return ret; 1532} 1533 1534uintptr_t Parcel::readPointer() const 1535{ 1536 return readAligned<binder_uintptr_t>(); 1537} 1538 1539 1540status_t Parcel::readFloat(float *pArg) const 1541{ 1542 return readAligned(pArg); 1543} 1544 1545 1546float Parcel::readFloat() const 1547{ 1548 return readAligned<float>(); 1549} 1550 1551#if defined(__mips__) && defined(__mips_hard_float) 1552 1553status_t Parcel::readDouble(double *pArg) const 1554{ 1555 union { 1556 double d; 1557 unsigned long long ll; 1558 } u; 1559 u.d = 0; 1560 status_t status; 1561 status = readAligned(&u.ll); 1562 *pArg = u.d; 1563 return status; 1564} 1565 1566double Parcel::readDouble() const 1567{ 1568 union { 1569 double d; 1570 unsigned long long ll; 1571 } u; 1572 u.ll = readAligned<unsigned long long>(); 1573 return u.d; 1574} 1575 1576#else 1577 1578status_t Parcel::readDouble(double *pArg) const 1579{ 1580 return readAligned(pArg); 1581} 1582 1583double Parcel::readDouble() const 1584{ 1585 return readAligned<double>(); 1586} 1587 1588#endif 1589 1590status_t Parcel::readIntPtr(intptr_t *pArg) const 1591{ 1592 return readAligned(pArg); 1593} 1594 1595 1596intptr_t Parcel::readIntPtr() const 1597{ 1598 return readAligned<intptr_t>(); 1599} 1600 1601status_t Parcel::readBool(bool *pArg) const 1602{ 1603 int32_t tmp; 1604 status_t ret = readInt32(&tmp); 1605 *pArg = (tmp != 0); 1606 return ret; 1607} 1608 1609bool Parcel::readBool() const 1610{ 1611 return readInt32() != 0; 1612} 1613 1614status_t Parcel::readChar(char16_t *pArg) const 1615{ 1616 int32_t tmp; 1617 status_t ret = readInt32(&tmp); 1618 *pArg = char16_t(tmp); 1619 return ret; 1620} 1621 1622char16_t Parcel::readChar() const 1623{ 1624 return char16_t(readInt32()); 1625} 1626 1627status_t Parcel::readByte(int8_t *pArg) const 1628{ 1629 int32_t tmp; 1630 status_t ret = readInt32(&tmp); 1631 *pArg = int8_t(tmp); 1632 return ret; 1633} 1634 1635int8_t Parcel::readByte() const 1636{ 1637 return int8_t(readInt32()); 1638} 1639 1640const char* Parcel::readCString() const 1641{ 1642 const size_t avail = mDataSize-mDataPos; 1643 if (avail > 0) { 1644 const char* str = reinterpret_cast<const char*>(mData+mDataPos); 1645 // is the string's trailing NUL within the parcel's valid bounds? 1646 const char* eos = reinterpret_cast<const char*>(memchr(str, 0, avail)); 1647 if (eos) { 1648 const size_t len = eos - str; 1649 mDataPos += pad_size(len+1); 1650 ALOGV("readCString Setting data pos of %p to %zu", this, mDataPos); 1651 return str; 1652 } 1653 } 1654 return NULL; 1655} 1656 1657String8 Parcel::readString8() const 1658{ 1659 int32_t size = readInt32(); 1660 // watch for potential int overflow adding 1 for trailing NUL 1661 if (size > 0 && size < INT32_MAX) { 1662 const char* str = (const char*)readInplace(size+1); 1663 if (str) return String8(str, size); 1664 } 1665 return String8(); 1666} 1667 1668String16 Parcel::readString16() const 1669{ 1670 size_t len; 1671 const char16_t* str = readString16Inplace(&len); 1672 if (str) return String16(str, len); 1673 ALOGE("Reading a NULL string not supported here."); 1674 return String16(); 1675} 1676 1677status_t Parcel::readString16(String16* pArg) const 1678{ 1679 size_t len; 1680 const char16_t* str = readString16Inplace(&len); 1681 if (str) { 1682 pArg->setTo(str, len); 1683 return 0; 1684 } else { 1685 *pArg = String16(); 1686 return UNEXPECTED_NULL; 1687 } 1688} 1689 1690const char16_t* Parcel::readString16Inplace(size_t* outLen) const 1691{ 1692 int32_t size = readInt32(); 1693 // watch for potential int overflow from size+1 1694 if (size >= 0 && size < INT32_MAX) { 1695 *outLen = size; 1696 const char16_t* str = (const char16_t*)readInplace((size+1)*sizeof(char16_t)); 1697 if (str != NULL) { 1698 return str; 1699 } 1700 } 1701 *outLen = 0; 1702 return NULL; 1703} 1704 1705status_t Parcel::readStrongBinder(sp<IBinder>* val) const 1706{ 1707 return unflatten_binder(ProcessState::self(), *this, val); 1708} 1709 1710sp<IBinder> Parcel::readStrongBinder() const 1711{ 1712 sp<IBinder> val; 1713 readStrongBinder(&val); 1714 return val; 1715} 1716 1717wp<IBinder> Parcel::readWeakBinder() const 1718{ 1719 wp<IBinder> val; 1720 unflatten_binder(ProcessState::self(), *this, &val); 1721 return val; 1722} 1723 1724int32_t Parcel::readExceptionCode() const 1725{ 1726 int32_t exception_code = readAligned<int32_t>(); 1727 if (exception_code == EX_HAS_REPLY_HEADER) { 1728 int32_t header_start = dataPosition(); 1729 int32_t header_size = readAligned<int32_t>(); 1730 // Skip over fat responses headers. Not used (or propagated) in 1731 // native code 1732 setDataPosition(header_start + header_size); 1733 // And fat response headers are currently only used when there are no 1734 // exceptions, so return no error: 1735 return 0; 1736 } 1737 return exception_code; 1738} 1739 1740native_handle* Parcel::readNativeHandle() const 1741{ 1742 int numFds, numInts; 1743 status_t err; 1744 err = readInt32(&numFds); 1745 if (err != NO_ERROR) return 0; 1746 err = readInt32(&numInts); 1747 if (err != NO_ERROR) return 0; 1748 1749 native_handle* h = native_handle_create(numFds, numInts); 1750 if (!h) { 1751 return 0; 1752 } 1753 1754 for (int i=0 ; err==NO_ERROR && i<numFds ; i++) { 1755 h->data[i] = dup(readFileDescriptor()); 1756 if (h->data[i] < 0) err = BAD_VALUE; 1757 } 1758 err = read(h->data + numFds, sizeof(int)*numInts); 1759 if (err != NO_ERROR) { 1760 native_handle_close(h); 1761 native_handle_delete(h); 1762 h = 0; 1763 } 1764 return h; 1765} 1766 1767 1768int Parcel::readFileDescriptor() const 1769{ 1770 const flat_binder_object* flat = readObject(true); 1771 if (flat) { 1772 switch (flat->type) { 1773 case BINDER_TYPE_FD: 1774 //ALOGI("Returning file descriptor %ld from parcel %p", flat->handle, this); 1775 return flat->handle; 1776 } 1777 } 1778 return BAD_TYPE; 1779} 1780 1781status_t Parcel::readBlob(size_t len, ReadableBlob* outBlob) const 1782{ 1783 int32_t blobType; 1784 status_t status = readInt32(&blobType); 1785 if (status) return status; 1786 1787 if (blobType == BLOB_INPLACE) { 1788 ALOGV("readBlob: read in place"); 1789 const void* ptr = readInplace(len); 1790 if (!ptr) return BAD_VALUE; 1791 1792 outBlob->init(-1, const_cast<void*>(ptr), len, false); 1793 return NO_ERROR; 1794 } 1795 1796 ALOGV("readBlob: read from ashmem"); 1797 bool isMutable = (blobType == BLOB_ASHMEM_MUTABLE); 1798 int fd = readFileDescriptor(); 1799 if (fd == int(BAD_TYPE)) return BAD_VALUE; 1800 1801 void* ptr = ::mmap(NULL, len, isMutable ? PROT_READ | PROT_WRITE : PROT_READ, 1802 MAP_SHARED, fd, 0); 1803 if (ptr == MAP_FAILED) return NO_MEMORY; 1804 1805 outBlob->init(fd, ptr, len, isMutable); 1806 return NO_ERROR; 1807} 1808 1809status_t Parcel::read(FlattenableHelperInterface& val) const 1810{ 1811 // size 1812 const size_t len = this->readInt32(); 1813 const size_t fd_count = this->readInt32(); 1814 1815 if (len > INT32_MAX) { 1816 // don't accept size_t values which may have come from an 1817 // inadvertent conversion from a negative int. 1818 return BAD_VALUE; 1819 } 1820 1821 // payload 1822 void const* const buf = this->readInplace(pad_size(len)); 1823 if (buf == NULL) 1824 return BAD_VALUE; 1825 1826 int* fds = NULL; 1827 if (fd_count) { 1828 fds = new int[fd_count]; 1829 } 1830 1831 status_t err = NO_ERROR; 1832 for (size_t i=0 ; i<fd_count && err==NO_ERROR ; i++) { 1833 fds[i] = dup(this->readFileDescriptor()); 1834 if (fds[i] < 0) { 1835 err = BAD_VALUE; 1836 ALOGE("dup() failed in Parcel::read, i is %zu, fds[i] is %d, fd_count is %zu, error: %s", 1837 i, fds[i], fd_count, strerror(errno)); 1838 } 1839 } 1840 1841 if (err == NO_ERROR) { 1842 err = val.unflatten(buf, len, fds, fd_count); 1843 } 1844 1845 if (fd_count) { 1846 delete [] fds; 1847 } 1848 1849 return err; 1850} 1851const flat_binder_object* Parcel::readObject(bool nullMetaData) const 1852{ 1853 const size_t DPOS = mDataPos; 1854 if ((DPOS+sizeof(flat_binder_object)) <= mDataSize) { 1855 const flat_binder_object* obj 1856 = reinterpret_cast<const flat_binder_object*>(mData+DPOS); 1857 mDataPos = DPOS + sizeof(flat_binder_object); 1858 if (!nullMetaData && (obj->cookie == 0 && obj->binder == 0)) { 1859 // When transferring a NULL object, we don't write it into 1860 // the object list, so we don't want to check for it when 1861 // reading. 1862 ALOGV("readObject Setting data pos of %p to %zu", this, mDataPos); 1863 return obj; 1864 } 1865 1866 // Ensure that this object is valid... 1867 binder_size_t* const OBJS = mObjects; 1868 const size_t N = mObjectsSize; 1869 size_t opos = mNextObjectHint; 1870 1871 if (N > 0) { 1872 ALOGV("Parcel %p looking for obj at %zu, hint=%zu", 1873 this, DPOS, opos); 1874 1875 // Start at the current hint position, looking for an object at 1876 // the current data position. 1877 if (opos < N) { 1878 while (opos < (N-1) && OBJS[opos] < DPOS) { 1879 opos++; 1880 } 1881 } else { 1882 opos = N-1; 1883 } 1884 if (OBJS[opos] == DPOS) { 1885 // Found it! 1886 ALOGV("Parcel %p found obj %zu at index %zu with forward search", 1887 this, DPOS, opos); 1888 mNextObjectHint = opos+1; 1889 ALOGV("readObject Setting data pos of %p to %zu", this, mDataPos); 1890 return obj; 1891 } 1892 1893 // Look backwards for it... 1894 while (opos > 0 && OBJS[opos] > DPOS) { 1895 opos--; 1896 } 1897 if (OBJS[opos] == DPOS) { 1898 // Found it! 1899 ALOGV("Parcel %p found obj %zu at index %zu with backward search", 1900 this, DPOS, opos); 1901 mNextObjectHint = opos+1; 1902 ALOGV("readObject Setting data pos of %p to %zu", this, mDataPos); 1903 return obj; 1904 } 1905 } 1906 ALOGW("Attempt to read object from Parcel %p at offset %zu that is not in the object list", 1907 this, DPOS); 1908 } 1909 return NULL; 1910} 1911 1912void Parcel::closeFileDescriptors() 1913{ 1914 size_t i = mObjectsSize; 1915 if (i > 0) { 1916 //ALOGI("Closing file descriptors for %zu objects...", i); 1917 } 1918 while (i > 0) { 1919 i--; 1920 const flat_binder_object* flat 1921 = reinterpret_cast<flat_binder_object*>(mData+mObjects[i]); 1922 if (flat->type == BINDER_TYPE_FD) { 1923 //ALOGI("Closing fd: %ld", flat->handle); 1924 close(flat->handle); 1925 } 1926 } 1927} 1928 1929uintptr_t Parcel::ipcData() const 1930{ 1931 return reinterpret_cast<uintptr_t>(mData); 1932} 1933 1934size_t Parcel::ipcDataSize() const 1935{ 1936 return (mDataSize > mDataPos ? mDataSize : mDataPos); 1937} 1938 1939uintptr_t Parcel::ipcObjects() const 1940{ 1941 return reinterpret_cast<uintptr_t>(mObjects); 1942} 1943 1944size_t Parcel::ipcObjectsCount() const 1945{ 1946 return mObjectsSize; 1947} 1948 1949void Parcel::ipcSetDataReference(const uint8_t* data, size_t dataSize, 1950 const binder_size_t* objects, size_t objectsCount, release_func relFunc, void* relCookie) 1951{ 1952 binder_size_t minOffset = 0; 1953 freeDataNoInit(); 1954 mError = NO_ERROR; 1955 mData = const_cast<uint8_t*>(data); 1956 mDataSize = mDataCapacity = dataSize; 1957 //ALOGI("setDataReference Setting data size of %p to %lu (pid=%d)", this, mDataSize, getpid()); 1958 mDataPos = 0; 1959 ALOGV("setDataReference Setting data pos of %p to %zu", this, mDataPos); 1960 mObjects = const_cast<binder_size_t*>(objects); 1961 mObjectsSize = mObjectsCapacity = objectsCount; 1962 mNextObjectHint = 0; 1963 mOwner = relFunc; 1964 mOwnerCookie = relCookie; 1965 for (size_t i = 0; i < mObjectsSize; i++) { 1966 binder_size_t offset = mObjects[i]; 1967 if (offset < minOffset) { 1968 ALOGE("%s: bad object offset %" PRIu64 " < %" PRIu64 "\n", 1969 __func__, (uint64_t)offset, (uint64_t)minOffset); 1970 mObjectsSize = 0; 1971 break; 1972 } 1973 minOffset = offset + sizeof(flat_binder_object); 1974 } 1975 scanForFds(); 1976} 1977 1978void Parcel::print(TextOutput& to, uint32_t /*flags*/) const 1979{ 1980 to << "Parcel("; 1981 1982 if (errorCheck() != NO_ERROR) { 1983 const status_t err = errorCheck(); 1984 to << "Error: " << (void*)(intptr_t)err << " \"" << strerror(-err) << "\""; 1985 } else if (dataSize() > 0) { 1986 const uint8_t* DATA = data(); 1987 to << indent << HexDump(DATA, dataSize()) << dedent; 1988 const binder_size_t* OBJS = objects(); 1989 const size_t N = objectsCount(); 1990 for (size_t i=0; i<N; i++) { 1991 const flat_binder_object* flat 1992 = reinterpret_cast<const flat_binder_object*>(DATA+OBJS[i]); 1993 to << endl << "Object #" << i << " @ " << (void*)OBJS[i] << ": " 1994 << TypeCode(flat->type & 0x7f7f7f00) 1995 << " = " << flat->binder; 1996 } 1997 } else { 1998 to << "NULL"; 1999 } 2000 2001 to << ")"; 2002} 2003 2004void Parcel::releaseObjects() 2005{ 2006 const sp<ProcessState> proc(ProcessState::self()); 2007 size_t i = mObjectsSize; 2008 uint8_t* const data = mData; 2009 binder_size_t* const objects = mObjects; 2010 while (i > 0) { 2011 i--; 2012 const flat_binder_object* flat 2013 = reinterpret_cast<flat_binder_object*>(data+objects[i]); 2014 release_object(proc, *flat, this); 2015 } 2016} 2017 2018void Parcel::acquireObjects() 2019{ 2020 const sp<ProcessState> proc(ProcessState::self()); 2021 size_t i = mObjectsSize; 2022 uint8_t* const data = mData; 2023 binder_size_t* const objects = mObjects; 2024 while (i > 0) { 2025 i--; 2026 const flat_binder_object* flat 2027 = reinterpret_cast<flat_binder_object*>(data+objects[i]); 2028 acquire_object(proc, *flat, this); 2029 } 2030} 2031 2032void Parcel::freeData() 2033{ 2034 freeDataNoInit(); 2035 initState(); 2036} 2037 2038void Parcel::freeDataNoInit() 2039{ 2040 if (mOwner) { 2041 LOG_ALLOC("Parcel %p: freeing other owner data", this); 2042 //ALOGI("Freeing data ref of %p (pid=%d)", this, getpid()); 2043 mOwner(this, mData, mDataSize, mObjects, mObjectsSize, mOwnerCookie); 2044 } else { 2045 LOG_ALLOC("Parcel %p: freeing allocated data", this); 2046 releaseObjects(); 2047 if (mData) { 2048 LOG_ALLOC("Parcel %p: freeing with %zu capacity", this, mDataCapacity); 2049 pthread_mutex_lock(&gParcelGlobalAllocSizeLock); 2050 if (mDataCapacity <= gParcelGlobalAllocSize) { 2051 gParcelGlobalAllocSize = gParcelGlobalAllocSize - mDataCapacity; 2052 } else { 2053 gParcelGlobalAllocSize = 0; 2054 } 2055 if (gParcelGlobalAllocCount > 0) { 2056 gParcelGlobalAllocCount--; 2057 } 2058 pthread_mutex_unlock(&gParcelGlobalAllocSizeLock); 2059 free(mData); 2060 } 2061 if (mObjects) free(mObjects); 2062 } 2063} 2064 2065status_t Parcel::growData(size_t len) 2066{ 2067 if (len > INT32_MAX) { 2068 // don't accept size_t values which may have come from an 2069 // inadvertent conversion from a negative int. 2070 return BAD_VALUE; 2071 } 2072 2073 size_t newSize = ((mDataSize+len)*3)/2; 2074 return (newSize <= mDataSize) 2075 ? (status_t) NO_MEMORY 2076 : continueWrite(newSize); 2077} 2078 2079status_t Parcel::restartWrite(size_t desired) 2080{ 2081 if (desired > INT32_MAX) { 2082 // don't accept size_t values which may have come from an 2083 // inadvertent conversion from a negative int. 2084 return BAD_VALUE; 2085 } 2086 2087 if (mOwner) { 2088 freeData(); 2089 return continueWrite(desired); 2090 } 2091 2092 uint8_t* data = (uint8_t*)realloc(mData, desired); 2093 if (!data && desired > mDataCapacity) { 2094 mError = NO_MEMORY; 2095 return NO_MEMORY; 2096 } 2097 2098 releaseObjects(); 2099 2100 if (data) { 2101 LOG_ALLOC("Parcel %p: restart from %zu to %zu capacity", this, mDataCapacity, desired); 2102 pthread_mutex_lock(&gParcelGlobalAllocSizeLock); 2103 gParcelGlobalAllocSize += desired; 2104 gParcelGlobalAllocSize -= mDataCapacity; 2105 pthread_mutex_unlock(&gParcelGlobalAllocSizeLock); 2106 mData = data; 2107 mDataCapacity = desired; 2108 } 2109 2110 mDataSize = mDataPos = 0; 2111 ALOGV("restartWrite Setting data size of %p to %zu", this, mDataSize); 2112 ALOGV("restartWrite Setting data pos of %p to %zu", this, mDataPos); 2113 2114 free(mObjects); 2115 mObjects = NULL; 2116 mObjectsSize = mObjectsCapacity = 0; 2117 mNextObjectHint = 0; 2118 mHasFds = false; 2119 mFdsKnown = true; 2120 mAllowFds = true; 2121 2122 return NO_ERROR; 2123} 2124 2125status_t Parcel::continueWrite(size_t desired) 2126{ 2127 if (desired > INT32_MAX) { 2128 // don't accept size_t values which may have come from an 2129 // inadvertent conversion from a negative int. 2130 return BAD_VALUE; 2131 } 2132 2133 // If shrinking, first adjust for any objects that appear 2134 // after the new data size. 2135 size_t objectsSize = mObjectsSize; 2136 if (desired < mDataSize) { 2137 if (desired == 0) { 2138 objectsSize = 0; 2139 } else { 2140 while (objectsSize > 0) { 2141 if (mObjects[objectsSize-1] < desired) 2142 break; 2143 objectsSize--; 2144 } 2145 } 2146 } 2147 2148 if (mOwner) { 2149 // If the size is going to zero, just release the owner's data. 2150 if (desired == 0) { 2151 freeData(); 2152 return NO_ERROR; 2153 } 2154 2155 // If there is a different owner, we need to take 2156 // posession. 2157 uint8_t* data = (uint8_t*)malloc(desired); 2158 if (!data) { 2159 mError = NO_MEMORY; 2160 return NO_MEMORY; 2161 } 2162 binder_size_t* objects = NULL; 2163 2164 if (objectsSize) { 2165 objects = (binder_size_t*)calloc(objectsSize, sizeof(binder_size_t)); 2166 if (!objects) { 2167 free(data); 2168 2169 mError = NO_MEMORY; 2170 return NO_MEMORY; 2171 } 2172 2173 // Little hack to only acquire references on objects 2174 // we will be keeping. 2175 size_t oldObjectsSize = mObjectsSize; 2176 mObjectsSize = objectsSize; 2177 acquireObjects(); 2178 mObjectsSize = oldObjectsSize; 2179 } 2180 2181 if (mData) { 2182 memcpy(data, mData, mDataSize < desired ? mDataSize : desired); 2183 } 2184 if (objects && mObjects) { 2185 memcpy(objects, mObjects, objectsSize*sizeof(binder_size_t)); 2186 } 2187 //ALOGI("Freeing data ref of %p (pid=%d)", this, getpid()); 2188 mOwner(this, mData, mDataSize, mObjects, mObjectsSize, mOwnerCookie); 2189 mOwner = NULL; 2190 2191 LOG_ALLOC("Parcel %p: taking ownership of %zu capacity", this, desired); 2192 pthread_mutex_lock(&gParcelGlobalAllocSizeLock); 2193 gParcelGlobalAllocSize += desired; 2194 gParcelGlobalAllocCount++; 2195 pthread_mutex_unlock(&gParcelGlobalAllocSizeLock); 2196 2197 mData = data; 2198 mObjects = objects; 2199 mDataSize = (mDataSize < desired) ? mDataSize : desired; 2200 ALOGV("continueWrite Setting data size of %p to %zu", this, mDataSize); 2201 mDataCapacity = desired; 2202 mObjectsSize = mObjectsCapacity = objectsSize; 2203 mNextObjectHint = 0; 2204 2205 } else if (mData) { 2206 if (objectsSize < mObjectsSize) { 2207 // Need to release refs on any objects we are dropping. 2208 const sp<ProcessState> proc(ProcessState::self()); 2209 for (size_t i=objectsSize; i<mObjectsSize; i++) { 2210 const flat_binder_object* flat 2211 = reinterpret_cast<flat_binder_object*>(mData+mObjects[i]); 2212 if (flat->type == BINDER_TYPE_FD) { 2213 // will need to rescan because we may have lopped off the only FDs 2214 mFdsKnown = false; 2215 } 2216 release_object(proc, *flat, this); 2217 } 2218 binder_size_t* objects = 2219 (binder_size_t*)realloc(mObjects, objectsSize*sizeof(binder_size_t)); 2220 if (objects) { 2221 mObjects = objects; 2222 } 2223 mObjectsSize = objectsSize; 2224 mNextObjectHint = 0; 2225 } 2226 2227 // We own the data, so we can just do a realloc(). 2228 if (desired > mDataCapacity) { 2229 uint8_t* data = (uint8_t*)realloc(mData, desired); 2230 if (data) { 2231 LOG_ALLOC("Parcel %p: continue from %zu to %zu capacity", this, mDataCapacity, 2232 desired); 2233 pthread_mutex_lock(&gParcelGlobalAllocSizeLock); 2234 gParcelGlobalAllocSize += desired; 2235 gParcelGlobalAllocSize -= mDataCapacity; 2236 gParcelGlobalAllocCount++; 2237 pthread_mutex_unlock(&gParcelGlobalAllocSizeLock); 2238 mData = data; 2239 mDataCapacity = desired; 2240 } else if (desired > mDataCapacity) { 2241 mError = NO_MEMORY; 2242 return NO_MEMORY; 2243 } 2244 } else { 2245 if (mDataSize > desired) { 2246 mDataSize = desired; 2247 ALOGV("continueWrite Setting data size of %p to %zu", this, mDataSize); 2248 } 2249 if (mDataPos > desired) { 2250 mDataPos = desired; 2251 ALOGV("continueWrite Setting data pos of %p to %zu", this, mDataPos); 2252 } 2253 } 2254 2255 } else { 2256 // This is the first data. Easy! 2257 uint8_t* data = (uint8_t*)malloc(desired); 2258 if (!data) { 2259 mError = NO_MEMORY; 2260 return NO_MEMORY; 2261 } 2262 2263 if(!(mDataCapacity == 0 && mObjects == NULL 2264 && mObjectsCapacity == 0)) { 2265 ALOGE("continueWrite: %zu/%p/%zu/%zu", mDataCapacity, mObjects, mObjectsCapacity, desired); 2266 } 2267 2268 LOG_ALLOC("Parcel %p: allocating with %zu capacity", this, desired); 2269 pthread_mutex_lock(&gParcelGlobalAllocSizeLock); 2270 gParcelGlobalAllocSize += desired; 2271 gParcelGlobalAllocCount++; 2272 pthread_mutex_unlock(&gParcelGlobalAllocSizeLock); 2273 2274 mData = data; 2275 mDataSize = mDataPos = 0; 2276 ALOGV("continueWrite Setting data size of %p to %zu", this, mDataSize); 2277 ALOGV("continueWrite Setting data pos of %p to %zu", this, mDataPos); 2278 mDataCapacity = desired; 2279 } 2280 2281 return NO_ERROR; 2282} 2283 2284void Parcel::initState() 2285{ 2286 LOG_ALLOC("Parcel %p: initState", this); 2287 mError = NO_ERROR; 2288 mData = 0; 2289 mDataSize = 0; 2290 mDataCapacity = 0; 2291 mDataPos = 0; 2292 ALOGV("initState Setting data size of %p to %zu", this, mDataSize); 2293 ALOGV("initState Setting data pos of %p to %zu", this, mDataPos); 2294 mObjects = NULL; 2295 mObjectsSize = 0; 2296 mObjectsCapacity = 0; 2297 mNextObjectHint = 0; 2298 mHasFds = false; 2299 mFdsKnown = true; 2300 mAllowFds = true; 2301 mOwner = NULL; 2302 mBlobAshmemSize = 0; 2303} 2304 2305void Parcel::scanForFds() const 2306{ 2307 bool hasFds = false; 2308 for (size_t i=0; i<mObjectsSize; i++) { 2309 const flat_binder_object* flat 2310 = reinterpret_cast<const flat_binder_object*>(mData + mObjects[i]); 2311 if (flat->type == BINDER_TYPE_FD) { 2312 hasFds = true; 2313 break; 2314 } 2315 } 2316 mHasFds = hasFds; 2317 mFdsKnown = true; 2318} 2319 2320size_t Parcel::getBlobAshmemSize() const 2321{ 2322 return mBlobAshmemSize; 2323} 2324 2325// --- Parcel::Blob --- 2326 2327Parcel::Blob::Blob() : 2328 mFd(-1), mData(NULL), mSize(0), mMutable(false) { 2329} 2330 2331Parcel::Blob::~Blob() { 2332 release(); 2333} 2334 2335void Parcel::Blob::release() { 2336 if (mFd != -1 && mData) { 2337 ::munmap(mData, mSize); 2338 } 2339 clear(); 2340} 2341 2342void Parcel::Blob::init(int fd, void* data, size_t size, bool isMutable) { 2343 mFd = fd; 2344 mData = data; 2345 mSize = size; 2346 mMutable = isMutable; 2347} 2348 2349void Parcel::Blob::clear() { 2350 mFd = -1; 2351 mData = NULL; 2352 mSize = 0; 2353 mMutable = false; 2354} 2355 2356}; // namespace android 2357