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