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