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