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