Parcel.cpp revision 4ba492f23b6fb0d5a95174cc194ce04e1497c0d7
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 <utils/Debug.h> 29#include <utils/Log.h> 30#include <utils/String8.h> 31#include <utils/String16.h> 32#include <utils/misc.h> 33#include <utils/Flattenable.h> 34#include <cutils/ashmem.h> 35 36#include <private/binder/binder_module.h> 37 38#include <stdio.h> 39#include <stdlib.h> 40#include <stdint.h> 41#include <sys/mman.h> 42 43#ifndef INT32_MAX 44#define INT32_MAX ((int32_t)(2147483647)) 45#endif 46 47#define LOG_REFS(...) 48//#define LOG_REFS(...) ALOG(LOG_DEBUG, "Parcel", __VA_ARGS__) 49 50// --------------------------------------------------------------------------- 51 52#define PAD_SIZE(s) (((s)+3)&~3) 53 54// Note: must be kept in sync with android/os/StrictMode.java's PENALTY_GATHER 55#define STRICT_MODE_PENALTY_GATHER 0x100 56 57// Note: must be kept in sync with android/os/Parcel.java's EX_HAS_REPLY_HEADER 58#define EX_HAS_REPLY_HEADER -128 59 60// Maximum size of a blob to transfer in-place. 61static const size_t IN_PLACE_BLOB_LIMIT = 40 * 1024; 62 63// XXX This can be made public if we want to provide 64// support for typed data. 65struct small_flat_data 66{ 67 uint32_t type; 68 uint32_t data; 69}; 70 71namespace android { 72 73void acquire_object(const sp<ProcessState>& proc, 74 const flat_binder_object& obj, const void* who) 75{ 76 switch (obj.type) { 77 case BINDER_TYPE_BINDER: 78 if (obj.binder) { 79 LOG_REFS("Parcel %p acquiring reference on local %p", who, obj.cookie); 80 reinterpret_cast<IBinder*>(obj.cookie)->incStrong(who); 81 } 82 return; 83 case BINDER_TYPE_WEAK_BINDER: 84 if (obj.binder) 85 reinterpret_cast<RefBase::weakref_type*>(obj.binder)->incWeak(who); 86 return; 87 case BINDER_TYPE_HANDLE: { 88 const sp<IBinder> b = proc->getStrongProxyForHandle(obj.handle); 89 if (b != NULL) { 90 LOG_REFS("Parcel %p acquiring reference on remote %p", who, b.get()); 91 b->incStrong(who); 92 } 93 return; 94 } 95 case BINDER_TYPE_WEAK_HANDLE: { 96 const wp<IBinder> b = proc->getWeakProxyForHandle(obj.handle); 97 if (b != NULL) b.get_refs()->incWeak(who); 98 return; 99 } 100 case BINDER_TYPE_FD: { 101 // intentionally blank -- nothing to do to acquire this, but we do 102 // recognize it as a legitimate object type. 103 return; 104 } 105 } 106 107 ALOGD("Invalid object type 0x%08x", obj.type); 108} 109 110void release_object(const sp<ProcessState>& proc, 111 const flat_binder_object& obj, const void* who) 112{ 113 switch (obj.type) { 114 case BINDER_TYPE_BINDER: 115 if (obj.binder) { 116 LOG_REFS("Parcel %p releasing reference on local %p", who, obj.cookie); 117 reinterpret_cast<IBinder*>(obj.cookie)->decStrong(who); 118 } 119 return; 120 case BINDER_TYPE_WEAK_BINDER: 121 if (obj.binder) 122 reinterpret_cast<RefBase::weakref_type*>(obj.binder)->decWeak(who); 123 return; 124 case BINDER_TYPE_HANDLE: { 125 const sp<IBinder> b = proc->getStrongProxyForHandle(obj.handle); 126 if (b != NULL) { 127 LOG_REFS("Parcel %p releasing reference on remote %p", who, b.get()); 128 b->decStrong(who); 129 } 130 return; 131 } 132 case BINDER_TYPE_WEAK_HANDLE: { 133 const wp<IBinder> b = proc->getWeakProxyForHandle(obj.handle); 134 if (b != NULL) b.get_refs()->decWeak(who); 135 return; 136 } 137 case BINDER_TYPE_FD: { 138 if (obj.cookie != 0) close(obj.handle); 139 return; 140 } 141 } 142 143 ALOGE("Invalid object type 0x%08x", obj.type); 144} 145 146inline static status_t finish_flatten_binder( 147 const sp<IBinder>& /*binder*/, const flat_binder_object& flat, Parcel* out) 148{ 149 return out->writeObject(flat, false); 150} 151 152status_t flatten_binder(const sp<ProcessState>& /*proc*/, 153 const sp<IBinder>& binder, Parcel* out) 154{ 155 flat_binder_object obj; 156 157 obj.flags = 0x7f | FLAT_BINDER_FLAG_ACCEPTS_FDS; 158 if (binder != NULL) { 159 IBinder *local = binder->localBinder(); 160 if (!local) { 161 BpBinder *proxy = binder->remoteBinder(); 162 if (proxy == NULL) { 163 ALOGE("null proxy"); 164 } 165 const int32_t handle = proxy ? proxy->handle() : 0; 166 obj.type = BINDER_TYPE_HANDLE; 167 obj.handle = handle; 168 obj.cookie = 0; 169 } else { 170 obj.type = BINDER_TYPE_BINDER; 171 obj.binder = reinterpret_cast<uintptr_t>(local->getWeakRefs()); 172 obj.cookie = reinterpret_cast<uintptr_t>(local); 173 } 174 } else { 175 obj.type = BINDER_TYPE_BINDER; 176 obj.binder = 0; 177 obj.cookie = 0; 178 } 179 180 return finish_flatten_binder(binder, obj, out); 181} 182 183status_t flatten_binder(const sp<ProcessState>& /*proc*/, 184 const wp<IBinder>& binder, Parcel* out) 185{ 186 flat_binder_object obj; 187 188 obj.flags = 0x7f | FLAT_BINDER_FLAG_ACCEPTS_FDS; 189 if (binder != NULL) { 190 sp<IBinder> real = binder.promote(); 191 if (real != NULL) { 192 IBinder *local = real->localBinder(); 193 if (!local) { 194 BpBinder *proxy = real->remoteBinder(); 195 if (proxy == NULL) { 196 ALOGE("null proxy"); 197 } 198 const int32_t handle = proxy ? proxy->handle() : 0; 199 obj.type = BINDER_TYPE_WEAK_HANDLE; 200 obj.handle = handle; 201 obj.cookie = 0; 202 } else { 203 obj.type = BINDER_TYPE_WEAK_BINDER; 204 obj.binder = reinterpret_cast<uintptr_t>(binder.get_refs()); 205 obj.cookie = reinterpret_cast<uintptr_t>(binder.unsafe_get()); 206 } 207 return finish_flatten_binder(real, obj, out); 208 } 209 210 // XXX How to deal? In order to flatten the given binder, 211 // we need to probe it for information, which requires a primary 212 // reference... but we don't have one. 213 // 214 // The OpenBinder implementation uses a dynamic_cast<> here, 215 // but we can't do that with the different reference counting 216 // implementation we are using. 217 ALOGE("Unable to unflatten Binder weak reference!"); 218 obj.type = BINDER_TYPE_BINDER; 219 obj.binder = 0; 220 obj.cookie = 0; 221 return finish_flatten_binder(NULL, obj, out); 222 223 } else { 224 obj.type = BINDER_TYPE_BINDER; 225 obj.binder = 0; 226 obj.cookie = 0; 227 return finish_flatten_binder(NULL, obj, out); 228 } 229} 230 231inline static status_t finish_unflatten_binder( 232 BpBinder* /*proxy*/, const flat_binder_object& /*flat*/, 233 const Parcel& /*in*/) 234{ 235 return NO_ERROR; 236} 237 238status_t unflatten_binder(const sp<ProcessState>& proc, 239 const Parcel& in, sp<IBinder>* out) 240{ 241 const flat_binder_object* flat = in.readObject(false); 242 243 if (flat) { 244 switch (flat->type) { 245 case BINDER_TYPE_BINDER: 246 *out = reinterpret_cast<IBinder*>(flat->cookie); 247 return finish_unflatten_binder(NULL, *flat, in); 248 case BINDER_TYPE_HANDLE: 249 *out = proc->getStrongProxyForHandle(flat->handle); 250 return finish_unflatten_binder( 251 static_cast<BpBinder*>(out->get()), *flat, in); 252 } 253 } 254 return BAD_TYPE; 255} 256 257status_t unflatten_binder(const sp<ProcessState>& proc, 258 const Parcel& in, wp<IBinder>* out) 259{ 260 const flat_binder_object* flat = in.readObject(false); 261 262 if (flat) { 263 switch (flat->type) { 264 case BINDER_TYPE_BINDER: 265 *out = reinterpret_cast<IBinder*>(flat->cookie); 266 return finish_unflatten_binder(NULL, *flat, in); 267 case BINDER_TYPE_WEAK_BINDER: 268 if (flat->binder != 0) { 269 out->set_object_and_refs( 270 reinterpret_cast<IBinder*>(flat->cookie), 271 reinterpret_cast<RefBase::weakref_type*>(flat->binder)); 272 } else { 273 *out = NULL; 274 } 275 return finish_unflatten_binder(NULL, *flat, in); 276 case BINDER_TYPE_HANDLE: 277 case BINDER_TYPE_WEAK_HANDLE: 278 *out = proc->getWeakProxyForHandle(flat->handle); 279 return finish_unflatten_binder( 280 static_cast<BpBinder*>(out->unsafe_get()), *flat, in); 281 } 282 } 283 return BAD_TYPE; 284} 285 286// --------------------------------------------------------------------------- 287 288Parcel::Parcel() 289{ 290 initState(); 291} 292 293Parcel::~Parcel() 294{ 295 freeDataNoInit(); 296} 297 298const uint8_t* Parcel::data() const 299{ 300 return mData; 301} 302 303size_t Parcel::dataSize() const 304{ 305 return (mDataSize > mDataPos ? mDataSize : mDataPos); 306} 307 308size_t Parcel::dataAvail() const 309{ 310 // TODO: decide what to do about the possibility that this can 311 // report an available-data size that exceeds a Java int's max 312 // positive value, causing havoc. Fortunately this will only 313 // happen if someone constructs a Parcel containing more than two 314 // gigabytes of data, which on typical phone hardware is simply 315 // not possible. 316 return dataSize() - dataPosition(); 317} 318 319size_t Parcel::dataPosition() const 320{ 321 return mDataPos; 322} 323 324size_t Parcel::dataCapacity() const 325{ 326 return mDataCapacity; 327} 328 329status_t Parcel::setDataSize(size_t size) 330{ 331 status_t err; 332 err = continueWrite(size); 333 if (err == NO_ERROR) { 334 mDataSize = size; 335 ALOGV("setDataSize Setting data size of %p to %d\n", this, mDataSize); 336 } 337 return err; 338} 339 340void Parcel::setDataPosition(size_t pos) const 341{ 342 mDataPos = pos; 343 mNextObjectHint = 0; 344} 345 346status_t Parcel::setDataCapacity(size_t size) 347{ 348 if (size > mDataCapacity) return continueWrite(size); 349 return NO_ERROR; 350} 351 352status_t Parcel::setData(const uint8_t* buffer, size_t len) 353{ 354 status_t err = restartWrite(len); 355 if (err == NO_ERROR) { 356 memcpy(const_cast<uint8_t*>(data()), buffer, len); 357 mDataSize = len; 358 mFdsKnown = false; 359 } 360 return err; 361} 362 363status_t Parcel::appendFrom(const Parcel *parcel, size_t offset, size_t len) 364{ 365 const sp<ProcessState> proc(ProcessState::self()); 366 status_t err; 367 const uint8_t *data = parcel->mData; 368 const binder_size_t *objects = parcel->mObjects; 369 size_t size = parcel->mObjectsSize; 370 int startPos = mDataPos; 371 int firstIndex = -1, lastIndex = -2; 372 373 if (len == 0) { 374 return NO_ERROR; 375 } 376 377 // range checks against the source parcel size 378 if ((offset > parcel->mDataSize) 379 || (len > parcel->mDataSize) 380 || (offset + len > parcel->mDataSize)) { 381 return BAD_VALUE; 382 } 383 384 // Count objects in range 385 for (int i = 0; i < (int) size; i++) { 386 size_t off = objects[i]; 387 if ((off >= offset) && (off < offset + len)) { 388 if (firstIndex == -1) { 389 firstIndex = i; 390 } 391 lastIndex = i; 392 } 393 } 394 int numObjects = lastIndex - firstIndex + 1; 395 396 if ((mDataSize+len) > mDataCapacity) { 397 // grow data 398 err = growData(len); 399 if (err != NO_ERROR) { 400 return err; 401 } 402 } 403 404 // append data 405 memcpy(mData + mDataPos, data + offset, len); 406 mDataPos += len; 407 mDataSize += len; 408 409 err = NO_ERROR; 410 411 if (numObjects > 0) { 412 // grow objects 413 if (mObjectsCapacity < mObjectsSize + numObjects) { 414 int newSize = ((mObjectsSize + numObjects)*3)/2; 415 binder_size_t *objects = 416 (binder_size_t*)realloc(mObjects, newSize*sizeof(binder_size_t)); 417 if (objects == (binder_size_t*)0) { 418 return NO_MEMORY; 419 } 420 mObjects = objects; 421 mObjectsCapacity = newSize; 422 } 423 424 // append and acquire objects 425 int idx = mObjectsSize; 426 for (int i = firstIndex; i <= lastIndex; i++) { 427 size_t off = objects[i] - offset + startPos; 428 mObjects[idx++] = off; 429 mObjectsSize++; 430 431 flat_binder_object* flat 432 = reinterpret_cast<flat_binder_object*>(mData + off); 433 acquire_object(proc, *flat, this); 434 435 if (flat->type == BINDER_TYPE_FD) { 436 // If this is a file descriptor, we need to dup it so the 437 // new Parcel now owns its own fd, and can declare that we 438 // officially know we have fds. 439 flat->handle = dup(flat->handle); 440 flat->cookie = 1; 441 mHasFds = mFdsKnown = true; 442 if (!mAllowFds) { 443 err = FDS_NOT_ALLOWED; 444 } 445 } 446 } 447 } 448 449 return err; 450} 451 452bool Parcel::pushAllowFds(bool allowFds) 453{ 454 const bool origValue = mAllowFds; 455 if (!allowFds) { 456 mAllowFds = false; 457 } 458 return origValue; 459} 460 461void Parcel::restoreAllowFds(bool lastValue) 462{ 463 mAllowFds = lastValue; 464} 465 466bool Parcel::hasFileDescriptors() const 467{ 468 if (!mFdsKnown) { 469 scanForFds(); 470 } 471 return mHasFds; 472} 473 474// Write RPC headers. (previously just the interface token) 475status_t Parcel::writeInterfaceToken(const String16& interface) 476{ 477 writeInt32(IPCThreadState::self()->getStrictModePolicy() | 478 STRICT_MODE_PENALTY_GATHER); 479 // currently the interface identification token is just its name as a string 480 return writeString16(interface); 481} 482 483bool Parcel::checkInterface(IBinder* binder) const 484{ 485 return enforceInterface(binder->getInterfaceDescriptor()); 486} 487 488bool Parcel::enforceInterface(const String16& interface, 489 IPCThreadState* threadState) const 490{ 491 int32_t strictPolicy = readInt32(); 492 if (threadState == NULL) { 493 threadState = IPCThreadState::self(); 494 } 495 if ((threadState->getLastTransactionBinderFlags() & 496 IBinder::FLAG_ONEWAY) != 0) { 497 // For one-way calls, the callee is running entirely 498 // disconnected from the caller, so disable StrictMode entirely. 499 // Not only does disk/network usage not impact the caller, but 500 // there's no way to commuicate back any violations anyway. 501 threadState->setStrictModePolicy(0); 502 } else { 503 threadState->setStrictModePolicy(strictPolicy); 504 } 505 const String16 str(readString16()); 506 if (str == interface) { 507 return true; 508 } else { 509 ALOGW("**** enforceInterface() expected '%s' but read '%s'\n", 510 String8(interface).string(), String8(str).string()); 511 return false; 512 } 513} 514 515const binder_size_t* Parcel::objects() const 516{ 517 return mObjects; 518} 519 520size_t Parcel::objectsCount() const 521{ 522 return mObjectsSize; 523} 524 525status_t Parcel::errorCheck() const 526{ 527 return mError; 528} 529 530void Parcel::setError(status_t err) 531{ 532 mError = err; 533} 534 535status_t Parcel::finishWrite(size_t len) 536{ 537 //printf("Finish write of %d\n", len); 538 mDataPos += len; 539 ALOGV("finishWrite Setting data pos of %p to %d\n", this, mDataPos); 540 if (mDataPos > mDataSize) { 541 mDataSize = mDataPos; 542 ALOGV("finishWrite Setting data size of %p to %d\n", this, mDataSize); 543 } 544 //printf("New pos=%d, size=%d\n", mDataPos, mDataSize); 545 return NO_ERROR; 546} 547 548status_t Parcel::writeUnpadded(const void* data, size_t len) 549{ 550 size_t end = mDataPos + len; 551 if (end < mDataPos) { 552 // integer overflow 553 return BAD_VALUE; 554 } 555 556 if (end <= mDataCapacity) { 557restart_write: 558 memcpy(mData+mDataPos, data, len); 559 return finishWrite(len); 560 } 561 562 status_t err = growData(len); 563 if (err == NO_ERROR) goto restart_write; 564 return err; 565} 566 567status_t Parcel::write(const void* data, size_t len) 568{ 569 void* const d = writeInplace(len); 570 if (d) { 571 memcpy(d, data, len); 572 return NO_ERROR; 573 } 574 return mError; 575} 576 577void* Parcel::writeInplace(size_t len) 578{ 579 const size_t padded = PAD_SIZE(len); 580 581 // sanity check for integer overflow 582 if (mDataPos+padded < mDataPos) { 583 return NULL; 584 } 585 586 if ((mDataPos+padded) <= mDataCapacity) { 587restart_write: 588 //printf("Writing %ld bytes, padded to %ld\n", len, padded); 589 uint8_t* const data = mData+mDataPos; 590 591 // Need to pad at end? 592 if (padded != len) { 593#if BYTE_ORDER == BIG_ENDIAN 594 static const uint32_t mask[4] = { 595 0x00000000, 0xffffff00, 0xffff0000, 0xff000000 596 }; 597#endif 598#if BYTE_ORDER == LITTLE_ENDIAN 599 static const uint32_t mask[4] = { 600 0x00000000, 0x00ffffff, 0x0000ffff, 0x000000ff 601 }; 602#endif 603 //printf("Applying pad mask: %p to %p\n", (void*)mask[padded-len], 604 // *reinterpret_cast<void**>(data+padded-4)); 605 *reinterpret_cast<uint32_t*>(data+padded-4) &= mask[padded-len]; 606 } 607 608 finishWrite(padded); 609 return data; 610 } 611 612 status_t err = growData(padded); 613 if (err == NO_ERROR) goto restart_write; 614 return NULL; 615} 616 617status_t Parcel::writeInt32(int32_t val) 618{ 619 return writeAligned(val); 620} 621status_t Parcel::writeInt32Array(size_t len, const int32_t *val) { 622 if (!val) { 623 return writeAligned(-1); 624 } 625 status_t ret = writeAligned(len); 626 if (ret == NO_ERROR) { 627 ret = write(val, len * sizeof(*val)); 628 } 629 return ret; 630} 631 632status_t Parcel::writeInt64(int64_t val) 633{ 634 return writeAligned(val); 635} 636 637status_t Parcel::writePointer(uintptr_t val) 638{ 639 return writeAligned<binder_uintptr_t>(val); 640} 641 642status_t Parcel::writeFloat(float val) 643{ 644 return writeAligned(val); 645} 646 647#if defined(__mips__) && defined(__mips_hard_float) 648 649status_t Parcel::writeDouble(double val) 650{ 651 union { 652 double d; 653 unsigned long long ll; 654 } u; 655 u.d = val; 656 return writeAligned(u.ll); 657} 658 659#else 660 661status_t Parcel::writeDouble(double val) 662{ 663 return writeAligned(val); 664} 665 666#endif 667 668status_t Parcel::writeIntPtr(intptr_t val) 669{ 670 return writeAligned(val); 671} 672 673status_t Parcel::writeCString(const char* str) 674{ 675 return write(str, strlen(str)+1); 676} 677 678status_t Parcel::writeString8(const String8& str) 679{ 680 status_t err = writeInt32(str.bytes()); 681 // only write string if its length is more than zero characters, 682 // as readString8 will only read if the length field is non-zero. 683 // this is slightly different from how writeString16 works. 684 if (str.bytes() > 0 && err == NO_ERROR) { 685 err = write(str.string(), str.bytes()+1); 686 } 687 return err; 688} 689 690status_t Parcel::writeString16(const String16& str) 691{ 692 return writeString16(str.string(), str.size()); 693} 694 695status_t Parcel::writeString16(const char16_t* str, size_t len) 696{ 697 if (str == NULL) return writeInt32(-1); 698 699 status_t err = writeInt32(len); 700 if (err == NO_ERROR) { 701 len *= sizeof(char16_t); 702 uint8_t* data = (uint8_t*)writeInplace(len+sizeof(char16_t)); 703 if (data) { 704 memcpy(data, str, len); 705 *reinterpret_cast<char16_t*>(data+len) = 0; 706 return NO_ERROR; 707 } 708 err = mError; 709 } 710 return err; 711} 712 713status_t Parcel::writeStrongBinder(const sp<IBinder>& val) 714{ 715 return flatten_binder(ProcessState::self(), val, this); 716} 717 718status_t Parcel::writeWeakBinder(const wp<IBinder>& val) 719{ 720 return flatten_binder(ProcessState::self(), val, this); 721} 722 723status_t Parcel::writeNativeHandle(const native_handle* handle) 724{ 725 if (!handle || handle->version != sizeof(native_handle)) 726 return BAD_TYPE; 727 728 status_t err; 729 err = writeInt32(handle->numFds); 730 if (err != NO_ERROR) return err; 731 732 err = writeInt32(handle->numInts); 733 if (err != NO_ERROR) return err; 734 735 for (int i=0 ; err==NO_ERROR && i<handle->numFds ; i++) 736 err = writeDupFileDescriptor(handle->data[i]); 737 738 if (err != NO_ERROR) { 739 ALOGD("write native handle, write dup fd failed"); 740 return err; 741 } 742 err = write(handle->data + handle->numFds, sizeof(int)*handle->numInts); 743 return err; 744} 745 746status_t Parcel::writeFileDescriptor(int fd, bool takeOwnership) 747{ 748 flat_binder_object obj; 749 obj.type = BINDER_TYPE_FD; 750 obj.flags = 0x7f | FLAT_BINDER_FLAG_ACCEPTS_FDS; 751 obj.handle = fd; 752 obj.cookie = takeOwnership ? 1 : 0; 753 return writeObject(obj, true); 754} 755 756status_t Parcel::writeDupFileDescriptor(int fd) 757{ 758 int dupFd = dup(fd); 759 if (dupFd < 0) { 760 return -errno; 761 } 762 status_t err = writeFileDescriptor(dupFd, true /*takeOwnership*/); 763 if (err) { 764 close(dupFd); 765 } 766 return err; 767} 768 769// WARNING: This method must stay in sync with 770// Parcelable.Creator<ParcelFileDescriptor> CREATOR 771// in frameworks/base/core/java/android/os/ParcelFileDescriptor.java 772status_t Parcel::writeParcelFileDescriptor(int fd, int commChannel) { 773 status_t status; 774 775 if (fd < 0) { 776 status = writeInt32(0); // ParcelFileDescriptor is null 777 if (status) return status; 778 } else { 779 status = writeInt32(1); // ParcelFileDescriptor is not null 780 if (status) return status; 781 status = writeDupFileDescriptor(fd); 782 if (status) return status; 783 if (commChannel < 0) { 784 status = writeInt32(0); // commChannel is null 785 if (status) return status; 786 } else { 787 status = writeInt32(1); // commChannel is not null 788 if (status) return status; 789 status = writeDupFileDescriptor(commChannel); 790 } 791 } 792 return status; 793} 794 795status_t Parcel::writeBlob(size_t len, WritableBlob* outBlob) 796{ 797 status_t status; 798 799 if (!mAllowFds || len <= IN_PLACE_BLOB_LIMIT) { 800 ALOGV("writeBlob: write in place"); 801 status = writeInt32(0); 802 if (status) return status; 803 804 void* ptr = writeInplace(len); 805 if (!ptr) return NO_MEMORY; 806 807 outBlob->init(false /*mapped*/, ptr, len); 808 return NO_ERROR; 809 } 810 811 ALOGV("writeBlob: write to ashmem"); 812 int fd = ashmem_create_region("Parcel Blob", len); 813 if (fd < 0) return NO_MEMORY; 814 815 int result = ashmem_set_prot_region(fd, PROT_READ | PROT_WRITE); 816 if (result < 0) { 817 status = result; 818 } else { 819 void* ptr = ::mmap(NULL, len, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0); 820 if (ptr == MAP_FAILED) { 821 status = -errno; 822 } else { 823 result = ashmem_set_prot_region(fd, PROT_READ); 824 if (result < 0) { 825 status = result; 826 } else { 827 status = writeInt32(1); 828 if (!status) { 829 status = writeFileDescriptor(fd, true /*takeOwnership*/); 830 if (!status) { 831 outBlob->init(true /*mapped*/, ptr, len); 832 return NO_ERROR; 833 } 834 } 835 } 836 } 837 ::munmap(ptr, len); 838 } 839 ::close(fd); 840 return status; 841} 842 843status_t Parcel::write(const FlattenableHelperInterface& val) 844{ 845 status_t err; 846 847 // size if needed 848 const size_t len = val.getFlattenedSize(); 849 const size_t fd_count = val.getFdCount(); 850 851 err = this->writeInt32(len); 852 if (err) return err; 853 854 err = this->writeInt32(fd_count); 855 if (err) return err; 856 857 // payload 858 void* const buf = this->writeInplace(PAD_SIZE(len)); 859 if (buf == NULL) 860 return BAD_VALUE; 861 862 int* fds = NULL; 863 if (fd_count) { 864 fds = new int[fd_count]; 865 } 866 867 err = val.flatten(buf, len, fds, fd_count); 868 for (size_t i=0 ; i<fd_count && err==NO_ERROR ; i++) { 869 err = this->writeDupFileDescriptor( fds[i] ); 870 } 871 872 if (fd_count) { 873 delete [] fds; 874 } 875 876 return err; 877} 878 879status_t Parcel::writeObject(const flat_binder_object& val, bool nullMetaData) 880{ 881 const bool enoughData = (mDataPos+sizeof(val)) <= mDataCapacity; 882 const bool enoughObjects = mObjectsSize < mObjectsCapacity; 883 if (enoughData && enoughObjects) { 884restart_write: 885 *reinterpret_cast<flat_binder_object*>(mData+mDataPos) = val; 886 887 // Need to write meta-data? 888 if (nullMetaData || val.binder != 0) { 889 mObjects[mObjectsSize] = mDataPos; 890 acquire_object(ProcessState::self(), val, this); 891 mObjectsSize++; 892 } 893 894 // remember if it's a file descriptor 895 if (val.type == BINDER_TYPE_FD) { 896 if (!mAllowFds) { 897 return FDS_NOT_ALLOWED; 898 } 899 mHasFds = mFdsKnown = true; 900 } 901 902 return finishWrite(sizeof(flat_binder_object)); 903 } 904 905 if (!enoughData) { 906 const status_t err = growData(sizeof(val)); 907 if (err != NO_ERROR) return err; 908 } 909 if (!enoughObjects) { 910 size_t newSize = ((mObjectsSize+2)*3)/2; 911 binder_size_t* objects = (binder_size_t*)realloc(mObjects, newSize*sizeof(binder_size_t)); 912 if (objects == NULL) return NO_MEMORY; 913 mObjects = objects; 914 mObjectsCapacity = newSize; 915 } 916 917 goto restart_write; 918} 919 920status_t Parcel::writeNoException() 921{ 922 return writeInt32(0); 923} 924 925void Parcel::remove(size_t /*start*/, size_t /*amt*/) 926{ 927 LOG_ALWAYS_FATAL("Parcel::remove() not yet implemented!"); 928} 929 930status_t Parcel::read(void* outData, size_t len) const 931{ 932 if ((mDataPos+PAD_SIZE(len)) >= mDataPos && (mDataPos+PAD_SIZE(len)) <= mDataSize) { 933 memcpy(outData, mData+mDataPos, len); 934 mDataPos += PAD_SIZE(len); 935 ALOGV("read Setting data pos of %p to %d\n", this, mDataPos); 936 return NO_ERROR; 937 } 938 return NOT_ENOUGH_DATA; 939} 940 941const void* Parcel::readInplace(size_t len) const 942{ 943 if ((mDataPos+PAD_SIZE(len)) >= mDataPos && (mDataPos+PAD_SIZE(len)) <= mDataSize) { 944 const void* data = mData+mDataPos; 945 mDataPos += PAD_SIZE(len); 946 ALOGV("readInplace Setting data pos of %p to %d\n", this, mDataPos); 947 return data; 948 } 949 return NULL; 950} 951 952template<class T> 953status_t Parcel::readAligned(T *pArg) const { 954 COMPILE_TIME_ASSERT_FUNCTION_SCOPE(PAD_SIZE(sizeof(T)) == sizeof(T)); 955 956 if ((mDataPos+sizeof(T)) <= mDataSize) { 957 const void* data = mData+mDataPos; 958 mDataPos += sizeof(T); 959 *pArg = *reinterpret_cast<const T*>(data); 960 return NO_ERROR; 961 } else { 962 return NOT_ENOUGH_DATA; 963 } 964} 965 966template<class T> 967T Parcel::readAligned() const { 968 T result; 969 if (readAligned(&result) != NO_ERROR) { 970 result = 0; 971 } 972 973 return result; 974} 975 976template<class T> 977status_t Parcel::writeAligned(T val) { 978 COMPILE_TIME_ASSERT_FUNCTION_SCOPE(PAD_SIZE(sizeof(T)) == sizeof(T)); 979 980 if ((mDataPos+sizeof(val)) <= mDataCapacity) { 981restart_write: 982 *reinterpret_cast<T*>(mData+mDataPos) = val; 983 return finishWrite(sizeof(val)); 984 } 985 986 status_t err = growData(sizeof(val)); 987 if (err == NO_ERROR) goto restart_write; 988 return err; 989} 990 991status_t Parcel::readInt32(int32_t *pArg) const 992{ 993 return readAligned(pArg); 994} 995 996int32_t Parcel::readInt32() const 997{ 998 return readAligned<int32_t>(); 999} 1000 1001 1002status_t Parcel::readInt64(int64_t *pArg) const 1003{ 1004 return readAligned(pArg); 1005} 1006 1007 1008int64_t Parcel::readInt64() const 1009{ 1010 return readAligned<int64_t>(); 1011} 1012 1013status_t Parcel::readPointer(uintptr_t *pArg) const 1014{ 1015 status_t ret; 1016 binder_uintptr_t ptr; 1017 ret = readAligned(&ptr); 1018 if (!ret) 1019 *pArg = ptr; 1020 return ret; 1021} 1022 1023uintptr_t Parcel::readPointer() const 1024{ 1025 return readAligned<binder_uintptr_t>(); 1026} 1027 1028 1029status_t Parcel::readFloat(float *pArg) const 1030{ 1031 return readAligned(pArg); 1032} 1033 1034 1035float Parcel::readFloat() const 1036{ 1037 return readAligned<float>(); 1038} 1039 1040#if defined(__mips__) && defined(__mips_hard_float) 1041 1042status_t Parcel::readDouble(double *pArg) const 1043{ 1044 union { 1045 double d; 1046 unsigned long long ll; 1047 } u; 1048 status_t status; 1049 status = readAligned(&u.ll); 1050 *pArg = u.d; 1051 return status; 1052} 1053 1054double Parcel::readDouble() const 1055{ 1056 union { 1057 double d; 1058 unsigned long long ll; 1059 } u; 1060 u.ll = readAligned<unsigned long long>(); 1061 return u.d; 1062} 1063 1064#else 1065 1066status_t Parcel::readDouble(double *pArg) const 1067{ 1068 return readAligned(pArg); 1069} 1070 1071double Parcel::readDouble() const 1072{ 1073 return readAligned<double>(); 1074} 1075 1076#endif 1077 1078status_t Parcel::readIntPtr(intptr_t *pArg) const 1079{ 1080 return readAligned(pArg); 1081} 1082 1083 1084intptr_t Parcel::readIntPtr() const 1085{ 1086 return readAligned<intptr_t>(); 1087} 1088 1089 1090const char* Parcel::readCString() const 1091{ 1092 const size_t avail = mDataSize-mDataPos; 1093 if (avail > 0) { 1094 const char* str = reinterpret_cast<const char*>(mData+mDataPos); 1095 // is the string's trailing NUL within the parcel's valid bounds? 1096 const char* eos = reinterpret_cast<const char*>(memchr(str, 0, avail)); 1097 if (eos) { 1098 const size_t len = eos - str; 1099 mDataPos += PAD_SIZE(len+1); 1100 ALOGV("readCString Setting data pos of %p to %d\n", this, mDataPos); 1101 return str; 1102 } 1103 } 1104 return NULL; 1105} 1106 1107String8 Parcel::readString8() const 1108{ 1109 int32_t size = readInt32(); 1110 // watch for potential int overflow adding 1 for trailing NUL 1111 if (size > 0 && size < INT32_MAX) { 1112 const char* str = (const char*)readInplace(size+1); 1113 if (str) return String8(str, size); 1114 } 1115 return String8(); 1116} 1117 1118String16 Parcel::readString16() const 1119{ 1120 size_t len; 1121 const char16_t* str = readString16Inplace(&len); 1122 if (str) return String16(str, len); 1123 ALOGE("Reading a NULL string not supported here."); 1124 return String16(); 1125} 1126 1127const char16_t* Parcel::readString16Inplace(size_t* outLen) const 1128{ 1129 int32_t size = readInt32(); 1130 // watch for potential int overflow from size+1 1131 if (size >= 0 && size < INT32_MAX) { 1132 *outLen = size; 1133 const char16_t* str = (const char16_t*)readInplace((size+1)*sizeof(char16_t)); 1134 if (str != NULL) { 1135 return str; 1136 } 1137 } 1138 *outLen = 0; 1139 return NULL; 1140} 1141 1142sp<IBinder> Parcel::readStrongBinder() const 1143{ 1144 sp<IBinder> val; 1145 unflatten_binder(ProcessState::self(), *this, &val); 1146 return val; 1147} 1148 1149wp<IBinder> Parcel::readWeakBinder() const 1150{ 1151 wp<IBinder> val; 1152 unflatten_binder(ProcessState::self(), *this, &val); 1153 return val; 1154} 1155 1156int32_t Parcel::readExceptionCode() const 1157{ 1158 int32_t exception_code = readAligned<int32_t>(); 1159 if (exception_code == EX_HAS_REPLY_HEADER) { 1160 int32_t header_start = dataPosition(); 1161 int32_t header_size = readAligned<int32_t>(); 1162 // Skip over fat responses headers. Not used (or propagated) in 1163 // native code 1164 setDataPosition(header_start + header_size); 1165 // And fat response headers are currently only used when there are no 1166 // exceptions, so return no error: 1167 return 0; 1168 } 1169 return exception_code; 1170} 1171 1172native_handle* Parcel::readNativeHandle() const 1173{ 1174 int numFds, numInts; 1175 status_t err; 1176 err = readInt32(&numFds); 1177 if (err != NO_ERROR) return 0; 1178 err = readInt32(&numInts); 1179 if (err != NO_ERROR) return 0; 1180 1181 native_handle* h = native_handle_create(numFds, numInts); 1182 for (int i=0 ; err==NO_ERROR && i<numFds ; i++) { 1183 h->data[i] = dup(readFileDescriptor()); 1184 if (h->data[i] < 0) err = BAD_VALUE; 1185 } 1186 err = read(h->data + numFds, sizeof(int)*numInts); 1187 if (err != NO_ERROR) { 1188 native_handle_close(h); 1189 native_handle_delete(h); 1190 h = 0; 1191 } 1192 return h; 1193} 1194 1195 1196int Parcel::readFileDescriptor() const 1197{ 1198 const flat_binder_object* flat = readObject(true); 1199 if (flat) { 1200 switch (flat->type) { 1201 case BINDER_TYPE_FD: 1202 //ALOGI("Returning file descriptor %ld from parcel %p\n", flat->handle, this); 1203 return flat->handle; 1204 } 1205 } 1206 return BAD_TYPE; 1207} 1208 1209// WARNING: This method must stay in sync with writeToParcel() 1210// in frameworks/base/core/java/android/os/ParcelFileDescriptor.java 1211int Parcel::readParcelFileDescriptor(int& outCommChannel) const { 1212 int fd; 1213 outCommChannel = -1; 1214 1215 if (readInt32() == 0) { 1216 fd = -1; 1217 } else { 1218 fd = readFileDescriptor(); 1219 if (fd >= 0 && readInt32() != 0) { 1220 outCommChannel = readFileDescriptor(); 1221 } 1222 } 1223 return fd; 1224} 1225 1226status_t Parcel::readBlob(size_t len, ReadableBlob* outBlob) const 1227{ 1228 int32_t useAshmem; 1229 status_t status = readInt32(&useAshmem); 1230 if (status) return status; 1231 1232 if (!useAshmem) { 1233 ALOGV("readBlob: read in place"); 1234 const void* ptr = readInplace(len); 1235 if (!ptr) return BAD_VALUE; 1236 1237 outBlob->init(false /*mapped*/, const_cast<void*>(ptr), len); 1238 return NO_ERROR; 1239 } 1240 1241 ALOGV("readBlob: read from ashmem"); 1242 int fd = readFileDescriptor(); 1243 if (fd == int(BAD_TYPE)) return BAD_VALUE; 1244 1245 void* ptr = ::mmap(NULL, len, PROT_READ, MAP_SHARED, fd, 0); 1246 if (!ptr) return NO_MEMORY; 1247 1248 outBlob->init(true /*mapped*/, ptr, len); 1249 return NO_ERROR; 1250} 1251 1252status_t Parcel::read(FlattenableHelperInterface& val) const 1253{ 1254 // size 1255 const size_t len = this->readInt32(); 1256 const size_t fd_count = this->readInt32(); 1257 1258 // payload 1259 void const* const buf = this->readInplace(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 status_t err = NO_ERROR; 1269 for (size_t i=0 ; i<fd_count && err==NO_ERROR ; i++) { 1270 fds[i] = dup(this->readFileDescriptor()); 1271 if (fds[i] < 0) err = BAD_VALUE; 1272 } 1273 1274 if (err == NO_ERROR) { 1275 err = val.unflatten(buf, len, fds, fd_count); 1276 } 1277 1278 if (fd_count) { 1279 delete [] fds; 1280 } 1281 1282 return err; 1283} 1284const flat_binder_object* Parcel::readObject(bool nullMetaData) const 1285{ 1286 const size_t DPOS = mDataPos; 1287 if ((DPOS+sizeof(flat_binder_object)) <= mDataSize) { 1288 const flat_binder_object* obj 1289 = reinterpret_cast<const flat_binder_object*>(mData+DPOS); 1290 mDataPos = DPOS + sizeof(flat_binder_object); 1291 if (!nullMetaData && (obj->cookie == 0 && obj->binder == 0)) { 1292 // When transferring a NULL object, we don't write it into 1293 // the object list, so we don't want to check for it when 1294 // reading. 1295 ALOGV("readObject Setting data pos of %p to %d\n", this, mDataPos); 1296 return obj; 1297 } 1298 1299 // Ensure that this object is valid... 1300 binder_size_t* const OBJS = mObjects; 1301 const size_t N = mObjectsSize; 1302 size_t opos = mNextObjectHint; 1303 1304 if (N > 0) { 1305 ALOGV("Parcel %p looking for obj at %d, hint=%d\n", 1306 this, DPOS, opos); 1307 1308 // Start at the current hint position, looking for an object at 1309 // the current data position. 1310 if (opos < N) { 1311 while (opos < (N-1) && OBJS[opos] < DPOS) { 1312 opos++; 1313 } 1314 } else { 1315 opos = N-1; 1316 } 1317 if (OBJS[opos] == DPOS) { 1318 // Found it! 1319 ALOGV("Parcel found obj %d at index %d with forward search", 1320 this, DPOS, opos); 1321 mNextObjectHint = opos+1; 1322 ALOGV("readObject Setting data pos of %p to %d\n", this, mDataPos); 1323 return obj; 1324 } 1325 1326 // Look backwards for it... 1327 while (opos > 0 && OBJS[opos] > DPOS) { 1328 opos--; 1329 } 1330 if (OBJS[opos] == DPOS) { 1331 // Found it! 1332 ALOGV("Parcel found obj %d at index %d with backward search", 1333 this, DPOS, opos); 1334 mNextObjectHint = opos+1; 1335 ALOGV("readObject Setting data pos of %p to %d\n", this, mDataPos); 1336 return obj; 1337 } 1338 } 1339 ALOGW("Attempt to read object from Parcel %p at offset %zu that is not in the object list", 1340 this, DPOS); 1341 } 1342 return NULL; 1343} 1344 1345void Parcel::closeFileDescriptors() 1346{ 1347 size_t i = mObjectsSize; 1348 if (i > 0) { 1349 //ALOGI("Closing file descriptors for %d objects...", mObjectsSize); 1350 } 1351 while (i > 0) { 1352 i--; 1353 const flat_binder_object* flat 1354 = reinterpret_cast<flat_binder_object*>(mData+mObjects[i]); 1355 if (flat->type == BINDER_TYPE_FD) { 1356 //ALOGI("Closing fd: %ld\n", flat->handle); 1357 close(flat->handle); 1358 } 1359 } 1360} 1361 1362uintptr_t Parcel::ipcData() const 1363{ 1364 return reinterpret_cast<uintptr_t>(mData); 1365} 1366 1367size_t Parcel::ipcDataSize() const 1368{ 1369 return (mDataSize > mDataPos ? mDataSize : mDataPos); 1370} 1371 1372uintptr_t Parcel::ipcObjects() const 1373{ 1374 return reinterpret_cast<uintptr_t>(mObjects); 1375} 1376 1377size_t Parcel::ipcObjectsCount() const 1378{ 1379 return mObjectsSize; 1380} 1381 1382void Parcel::ipcSetDataReference(const uint8_t* data, size_t dataSize, 1383 const binder_size_t* objects, size_t objectsCount, release_func relFunc, void* relCookie) 1384{ 1385 freeDataNoInit(); 1386 mError = NO_ERROR; 1387 mData = const_cast<uint8_t*>(data); 1388 mDataSize = mDataCapacity = dataSize; 1389 //ALOGI("setDataReference Setting data size of %p to %lu (pid=%d)\n", this, mDataSize, getpid()); 1390 mDataPos = 0; 1391 ALOGV("setDataReference Setting data pos of %p to %d\n", this, mDataPos); 1392 mObjects = const_cast<binder_size_t*>(objects); 1393 mObjectsSize = mObjectsCapacity = objectsCount; 1394 mNextObjectHint = 0; 1395 mOwner = relFunc; 1396 mOwnerCookie = relCookie; 1397 scanForFds(); 1398} 1399 1400void Parcel::print(TextOutput& to, uint32_t /*flags*/) const 1401{ 1402 to << "Parcel("; 1403 1404 if (errorCheck() != NO_ERROR) { 1405 const status_t err = errorCheck(); 1406 to << "Error: " << (void*)(intptr_t)err << " \"" << strerror(-err) << "\""; 1407 } else if (dataSize() > 0) { 1408 const uint8_t* DATA = data(); 1409 to << indent << HexDump(DATA, dataSize()) << dedent; 1410 const binder_size_t* OBJS = objects(); 1411 const size_t N = objectsCount(); 1412 for (size_t i=0; i<N; i++) { 1413 const flat_binder_object* flat 1414 = reinterpret_cast<const flat_binder_object*>(DATA+OBJS[i]); 1415 to << endl << "Object #" << i << " @ " << (void*)OBJS[i] << ": " 1416 << TypeCode(flat->type & 0x7f7f7f00) 1417 << " = " << flat->binder; 1418 } 1419 } else { 1420 to << "NULL"; 1421 } 1422 1423 to << ")"; 1424} 1425 1426void Parcel::releaseObjects() 1427{ 1428 const sp<ProcessState> proc(ProcessState::self()); 1429 size_t i = mObjectsSize; 1430 uint8_t* const data = mData; 1431 binder_size_t* const objects = mObjects; 1432 while (i > 0) { 1433 i--; 1434 const flat_binder_object* flat 1435 = reinterpret_cast<flat_binder_object*>(data+objects[i]); 1436 release_object(proc, *flat, this); 1437 } 1438} 1439 1440void Parcel::acquireObjects() 1441{ 1442 const sp<ProcessState> proc(ProcessState::self()); 1443 size_t i = mObjectsSize; 1444 uint8_t* const data = mData; 1445 binder_size_t* const objects = mObjects; 1446 while (i > 0) { 1447 i--; 1448 const flat_binder_object* flat 1449 = reinterpret_cast<flat_binder_object*>(data+objects[i]); 1450 acquire_object(proc, *flat, this); 1451 } 1452} 1453 1454void Parcel::freeData() 1455{ 1456 freeDataNoInit(); 1457 initState(); 1458} 1459 1460void Parcel::freeDataNoInit() 1461{ 1462 if (mOwner) { 1463 //ALOGI("Freeing data ref of %p (pid=%d)\n", this, getpid()); 1464 mOwner(this, mData, mDataSize, mObjects, mObjectsSize, mOwnerCookie); 1465 } else { 1466 releaseObjects(); 1467 if (mData) free(mData); 1468 if (mObjects) free(mObjects); 1469 } 1470} 1471 1472status_t Parcel::growData(size_t len) 1473{ 1474 size_t newSize = ((mDataSize+len)*3)/2; 1475 return (newSize <= mDataSize) 1476 ? (status_t) NO_MEMORY 1477 : continueWrite(newSize); 1478} 1479 1480status_t Parcel::restartWrite(size_t desired) 1481{ 1482 if (mOwner) { 1483 freeData(); 1484 return continueWrite(desired); 1485 } 1486 1487 uint8_t* data = (uint8_t*)realloc(mData, desired); 1488 if (!data && desired > mDataCapacity) { 1489 mError = NO_MEMORY; 1490 return NO_MEMORY; 1491 } 1492 1493 releaseObjects(); 1494 1495 if (data) { 1496 mData = data; 1497 mDataCapacity = desired; 1498 } 1499 1500 mDataSize = mDataPos = 0; 1501 ALOGV("restartWrite Setting data size of %p to %d\n", this, mDataSize); 1502 ALOGV("restartWrite Setting data pos of %p to %d\n", this, mDataPos); 1503 1504 free(mObjects); 1505 mObjects = NULL; 1506 mObjectsSize = mObjectsCapacity = 0; 1507 mNextObjectHint = 0; 1508 mHasFds = false; 1509 mFdsKnown = true; 1510 mAllowFds = true; 1511 1512 return NO_ERROR; 1513} 1514 1515status_t Parcel::continueWrite(size_t desired) 1516{ 1517 // If shrinking, first adjust for any objects that appear 1518 // after the new data size. 1519 size_t objectsSize = mObjectsSize; 1520 if (desired < mDataSize) { 1521 if (desired == 0) { 1522 objectsSize = 0; 1523 } else { 1524 while (objectsSize > 0) { 1525 if (mObjects[objectsSize-1] < desired) 1526 break; 1527 objectsSize--; 1528 } 1529 } 1530 } 1531 1532 if (mOwner) { 1533 // If the size is going to zero, just release the owner's data. 1534 if (desired == 0) { 1535 freeData(); 1536 return NO_ERROR; 1537 } 1538 1539 // If there is a different owner, we need to take 1540 // posession. 1541 uint8_t* data = (uint8_t*)malloc(desired); 1542 if (!data) { 1543 mError = NO_MEMORY; 1544 return NO_MEMORY; 1545 } 1546 binder_size_t* objects = NULL; 1547 1548 if (objectsSize) { 1549 objects = (binder_size_t*)malloc(objectsSize*sizeof(binder_size_t)); 1550 if (!objects) { 1551 free(data); 1552 1553 mError = NO_MEMORY; 1554 return NO_MEMORY; 1555 } 1556 1557 // Little hack to only acquire references on objects 1558 // we will be keeping. 1559 size_t oldObjectsSize = mObjectsSize; 1560 mObjectsSize = objectsSize; 1561 acquireObjects(); 1562 mObjectsSize = oldObjectsSize; 1563 } 1564 1565 if (mData) { 1566 memcpy(data, mData, mDataSize < desired ? mDataSize : desired); 1567 } 1568 if (objects && mObjects) { 1569 memcpy(objects, mObjects, objectsSize*sizeof(binder_size_t)); 1570 } 1571 //ALOGI("Freeing data ref of %p (pid=%d)\n", this, getpid()); 1572 mOwner(this, mData, mDataSize, mObjects, mObjectsSize, mOwnerCookie); 1573 mOwner = NULL; 1574 1575 mData = data; 1576 mObjects = objects; 1577 mDataSize = (mDataSize < desired) ? mDataSize : desired; 1578 ALOGV("continueWrite Setting data size of %p to %d\n", this, mDataSize); 1579 mDataCapacity = desired; 1580 mObjectsSize = mObjectsCapacity = objectsSize; 1581 mNextObjectHint = 0; 1582 1583 } else if (mData) { 1584 if (objectsSize < mObjectsSize) { 1585 // Need to release refs on any objects we are dropping. 1586 const sp<ProcessState> proc(ProcessState::self()); 1587 for (size_t i=objectsSize; i<mObjectsSize; i++) { 1588 const flat_binder_object* flat 1589 = reinterpret_cast<flat_binder_object*>(mData+mObjects[i]); 1590 if (flat->type == BINDER_TYPE_FD) { 1591 // will need to rescan because we may have lopped off the only FDs 1592 mFdsKnown = false; 1593 } 1594 release_object(proc, *flat, this); 1595 } 1596 binder_size_t* objects = 1597 (binder_size_t*)realloc(mObjects, objectsSize*sizeof(binder_size_t)); 1598 if (objects) { 1599 mObjects = objects; 1600 } 1601 mObjectsSize = objectsSize; 1602 mNextObjectHint = 0; 1603 } 1604 1605 // We own the data, so we can just do a realloc(). 1606 if (desired > mDataCapacity) { 1607 uint8_t* data = (uint8_t*)realloc(mData, desired); 1608 if (data) { 1609 mData = data; 1610 mDataCapacity = desired; 1611 } else if (desired > mDataCapacity) { 1612 mError = NO_MEMORY; 1613 return NO_MEMORY; 1614 } 1615 } else { 1616 if (mDataSize > desired) { 1617 mDataSize = desired; 1618 ALOGV("continueWrite Setting data size of %p to %d\n", this, mDataSize); 1619 } 1620 if (mDataPos > desired) { 1621 mDataPos = desired; 1622 ALOGV("continueWrite Setting data pos of %p to %d\n", this, mDataPos); 1623 } 1624 } 1625 1626 } else { 1627 // This is the first data. Easy! 1628 uint8_t* data = (uint8_t*)malloc(desired); 1629 if (!data) { 1630 mError = NO_MEMORY; 1631 return NO_MEMORY; 1632 } 1633 1634 if(!(mDataCapacity == 0 && mObjects == NULL 1635 && mObjectsCapacity == 0)) { 1636 ALOGE("continueWrite: %zu/%p/%zu/%zu", mDataCapacity, mObjects, mObjectsCapacity, desired); 1637 } 1638 1639 mData = data; 1640 mDataSize = mDataPos = 0; 1641 ALOGV("continueWrite Setting data size of %p to %d\n", this, mDataSize); 1642 ALOGV("continueWrite Setting data pos of %p to %d\n", this, mDataPos); 1643 mDataCapacity = desired; 1644 } 1645 1646 return NO_ERROR; 1647} 1648 1649void Parcel::initState() 1650{ 1651 mError = NO_ERROR; 1652 mData = 0; 1653 mDataSize = 0; 1654 mDataCapacity = 0; 1655 mDataPos = 0; 1656 ALOGV("initState Setting data size of %p to %d\n", this, mDataSize); 1657 ALOGV("initState Setting data pos of %p to %d\n", this, mDataPos); 1658 mObjects = NULL; 1659 mObjectsSize = 0; 1660 mObjectsCapacity = 0; 1661 mNextObjectHint = 0; 1662 mHasFds = false; 1663 mFdsKnown = true; 1664 mAllowFds = true; 1665 mOwner = NULL; 1666} 1667 1668void Parcel::scanForFds() const 1669{ 1670 bool hasFds = false; 1671 for (size_t i=0; i<mObjectsSize; i++) { 1672 const flat_binder_object* flat 1673 = reinterpret_cast<const flat_binder_object*>(mData + mObjects[i]); 1674 if (flat->type == BINDER_TYPE_FD) { 1675 hasFds = true; 1676 break; 1677 } 1678 } 1679 mHasFds = hasFds; 1680 mFdsKnown = true; 1681} 1682 1683// --- Parcel::Blob --- 1684 1685Parcel::Blob::Blob() : 1686 mMapped(false), mData(NULL), mSize(0) { 1687} 1688 1689Parcel::Blob::~Blob() { 1690 release(); 1691} 1692 1693void Parcel::Blob::release() { 1694 if (mMapped && mData) { 1695 ::munmap(mData, mSize); 1696 } 1697 clear(); 1698} 1699 1700void Parcel::Blob::init(bool mapped, void* data, size_t size) { 1701 mMapped = mapped; 1702 mData = data; 1703 mSize = size; 1704} 1705 1706void Parcel::Blob::clear() { 1707 mMapped = false; 1708 mData = NULL; 1709 mSize = 0; 1710} 1711 1712}; // namespace android 1713