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