Parcel.cpp revision 4e97f8c62f3eb3c51343e9f4bbd898992a13903d
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/Binder.h> 23#include <binder/BpBinder.h> 24#include <utils/Debug.h> 25#include <binder/ProcessState.h> 26#include <utils/Log.h> 27#include <utils/String8.h> 28#include <utils/String16.h> 29#include <utils/TextOutput.h> 30#include <utils/misc.h> 31 32#include <private/binder/binder_module.h> 33 34#include <stdio.h> 35#include <stdlib.h> 36#include <stdint.h> 37 38#ifndef INT32_MAX 39#define INT32_MAX ((int32_t)(2147483647)) 40#endif 41 42#define LOG_REFS(...) 43//#define LOG_REFS(...) LOG(LOG_DEBUG, "Parcel", __VA_ARGS__) 44 45// --------------------------------------------------------------------------- 46 47#define PAD_SIZE(s) (((s)+3)&~3) 48 49// XXX This can be made public if we want to provide 50// support for typed data. 51struct small_flat_data 52{ 53 uint32_t type; 54 uint32_t data; 55}; 56 57namespace android { 58 59void acquire_object(const sp<ProcessState>& proc, 60 const flat_binder_object& obj, const void* who) 61{ 62 switch (obj.type) { 63 case BINDER_TYPE_BINDER: 64 if (obj.binder) { 65 LOG_REFS("Parcel %p acquiring reference on local %p", who, obj.cookie); 66 static_cast<IBinder*>(obj.cookie)->incStrong(who); 67 } 68 return; 69 case BINDER_TYPE_WEAK_BINDER: 70 if (obj.binder) 71 static_cast<RefBase::weakref_type*>(obj.binder)->incWeak(who); 72 return; 73 case BINDER_TYPE_HANDLE: { 74 const sp<IBinder> b = proc->getStrongProxyForHandle(obj.handle); 75 if (b != NULL) { 76 LOG_REFS("Parcel %p acquiring reference on remote %p", who, b.get()); 77 b->incStrong(who); 78 } 79 return; 80 } 81 case BINDER_TYPE_WEAK_HANDLE: { 82 const wp<IBinder> b = proc->getWeakProxyForHandle(obj.handle); 83 if (b != NULL) b.get_refs()->incWeak(who); 84 return; 85 } 86 case BINDER_TYPE_FD: { 87 // intentionally blank -- nothing to do to acquire this, but we do 88 // recognize it as a legitimate object type. 89 return; 90 } 91 } 92 93 LOGD("Invalid object type 0x%08lx", obj.type); 94} 95 96void release_object(const sp<ProcessState>& proc, 97 const flat_binder_object& obj, const void* who) 98{ 99 switch (obj.type) { 100 case BINDER_TYPE_BINDER: 101 if (obj.binder) { 102 LOG_REFS("Parcel %p releasing reference on local %p", who, obj.cookie); 103 static_cast<IBinder*>(obj.cookie)->decStrong(who); 104 } 105 return; 106 case BINDER_TYPE_WEAK_BINDER: 107 if (obj.binder) 108 static_cast<RefBase::weakref_type*>(obj.binder)->decWeak(who); 109 return; 110 case BINDER_TYPE_HANDLE: { 111 const sp<IBinder> b = proc->getStrongProxyForHandle(obj.handle); 112 if (b != NULL) { 113 LOG_REFS("Parcel %p releasing reference on remote %p", who, b.get()); 114 b->decStrong(who); 115 } 116 return; 117 } 118 case BINDER_TYPE_WEAK_HANDLE: { 119 const wp<IBinder> b = proc->getWeakProxyForHandle(obj.handle); 120 if (b != NULL) b.get_refs()->decWeak(who); 121 return; 122 } 123 case BINDER_TYPE_FD: { 124 if (obj.cookie != (void*)0) close(obj.handle); 125 return; 126 } 127 } 128 129 LOGE("Invalid object type 0x%08lx", obj.type); 130} 131 132inline static status_t finish_flatten_binder( 133 const sp<IBinder>& binder, const flat_binder_object& flat, Parcel* out) 134{ 135 return out->writeObject(flat, false); 136} 137 138status_t flatten_binder(const sp<ProcessState>& proc, 139 const sp<IBinder>& binder, Parcel* out) 140{ 141 flat_binder_object obj; 142 143 obj.flags = 0x7f | FLAT_BINDER_FLAG_ACCEPTS_FDS; 144 if (binder != NULL) { 145 IBinder *local = binder->localBinder(); 146 if (!local) { 147 BpBinder *proxy = binder->remoteBinder(); 148 if (proxy == NULL) { 149 LOGE("null proxy"); 150 } 151 const int32_t handle = proxy ? proxy->handle() : 0; 152 obj.type = BINDER_TYPE_HANDLE; 153 obj.handle = handle; 154 obj.cookie = NULL; 155 } else { 156 obj.type = BINDER_TYPE_BINDER; 157 obj.binder = local->getWeakRefs(); 158 obj.cookie = local; 159 } 160 } else { 161 obj.type = BINDER_TYPE_BINDER; 162 obj.binder = NULL; 163 obj.cookie = NULL; 164 } 165 166 return finish_flatten_binder(binder, obj, out); 167} 168 169status_t flatten_binder(const sp<ProcessState>& proc, 170 const wp<IBinder>& binder, Parcel* out) 171{ 172 flat_binder_object obj; 173 174 obj.flags = 0x7f | FLAT_BINDER_FLAG_ACCEPTS_FDS; 175 if (binder != NULL) { 176 sp<IBinder> real = binder.promote(); 177 if (real != NULL) { 178 IBinder *local = real->localBinder(); 179 if (!local) { 180 BpBinder *proxy = real->remoteBinder(); 181 if (proxy == NULL) { 182 LOGE("null proxy"); 183 } 184 const int32_t handle = proxy ? proxy->handle() : 0; 185 obj.type = BINDER_TYPE_WEAK_HANDLE; 186 obj.handle = handle; 187 obj.cookie = NULL; 188 } else { 189 obj.type = BINDER_TYPE_WEAK_BINDER; 190 obj.binder = binder.get_refs(); 191 obj.cookie = binder.unsafe_get(); 192 } 193 return finish_flatten_binder(real, obj, out); 194 } 195 196 // XXX How to deal? In order to flatten the given binder, 197 // we need to probe it for information, which requires a primary 198 // reference... but we don't have one. 199 // 200 // The OpenBinder implementation uses a dynamic_cast<> here, 201 // but we can't do that with the different reference counting 202 // implementation we are using. 203 LOGE("Unable to unflatten Binder weak reference!"); 204 obj.type = BINDER_TYPE_BINDER; 205 obj.binder = NULL; 206 obj.cookie = NULL; 207 return finish_flatten_binder(NULL, obj, out); 208 209 } else { 210 obj.type = BINDER_TYPE_BINDER; 211 obj.binder = NULL; 212 obj.cookie = NULL; 213 return finish_flatten_binder(NULL, obj, out); 214 } 215} 216 217inline static status_t finish_unflatten_binder( 218 BpBinder* proxy, const flat_binder_object& flat, const Parcel& in) 219{ 220 return NO_ERROR; 221} 222 223status_t unflatten_binder(const sp<ProcessState>& proc, 224 const Parcel& in, sp<IBinder>* out) 225{ 226 const flat_binder_object* flat = in.readObject(false); 227 228 if (flat) { 229 switch (flat->type) { 230 case BINDER_TYPE_BINDER: 231 *out = static_cast<IBinder*>(flat->cookie); 232 return finish_unflatten_binder(NULL, *flat, in); 233 case BINDER_TYPE_HANDLE: 234 *out = proc->getStrongProxyForHandle(flat->handle); 235 return finish_unflatten_binder( 236 static_cast<BpBinder*>(out->get()), *flat, in); 237 } 238 } 239 return BAD_TYPE; 240} 241 242status_t unflatten_binder(const sp<ProcessState>& proc, 243 const Parcel& in, wp<IBinder>* out) 244{ 245 const flat_binder_object* flat = in.readObject(false); 246 247 if (flat) { 248 switch (flat->type) { 249 case BINDER_TYPE_BINDER: 250 *out = static_cast<IBinder*>(flat->cookie); 251 return finish_unflatten_binder(NULL, *flat, in); 252 case BINDER_TYPE_WEAK_BINDER: 253 if (flat->binder != NULL) { 254 out->set_object_and_refs( 255 static_cast<IBinder*>(flat->cookie), 256 static_cast<RefBase::weakref_type*>(flat->binder)); 257 } else { 258 *out = NULL; 259 } 260 return finish_unflatten_binder(NULL, *flat, in); 261 case BINDER_TYPE_HANDLE: 262 case BINDER_TYPE_WEAK_HANDLE: 263 *out = proc->getWeakProxyForHandle(flat->handle); 264 return finish_unflatten_binder( 265 static_cast<BpBinder*>(out->unsafe_get()), *flat, in); 266 } 267 } 268 return BAD_TYPE; 269} 270 271// --------------------------------------------------------------------------- 272 273Parcel::Parcel() 274{ 275 initState(); 276} 277 278Parcel::~Parcel() 279{ 280 freeDataNoInit(); 281} 282 283const uint8_t* Parcel::data() const 284{ 285 return mData; 286} 287 288size_t Parcel::dataSize() const 289{ 290 return (mDataSize > mDataPos ? mDataSize : mDataPos); 291} 292 293size_t Parcel::dataAvail() const 294{ 295 // TODO: decide what to do about the possibility that this can 296 // report an available-data size that exceeds a Java int's max 297 // positive value, causing havoc. Fortunately this will only 298 // happen if someone constructs a Parcel containing more than two 299 // gigabytes of data, which on typical phone hardware is simply 300 // not possible. 301 return dataSize() - dataPosition(); 302} 303 304size_t Parcel::dataPosition() const 305{ 306 return mDataPos; 307} 308 309size_t Parcel::dataCapacity() const 310{ 311 return mDataCapacity; 312} 313 314status_t Parcel::setDataSize(size_t size) 315{ 316 status_t err; 317 err = continueWrite(size); 318 if (err == NO_ERROR) { 319 mDataSize = size; 320 LOGV("setDataSize Setting data size of %p to %d\n", this, mDataSize); 321 } 322 return err; 323} 324 325void Parcel::setDataPosition(size_t pos) const 326{ 327 mDataPos = pos; 328 mNextObjectHint = 0; 329} 330 331status_t Parcel::setDataCapacity(size_t size) 332{ 333 if (size > mDataSize) return continueWrite(size); 334 return NO_ERROR; 335} 336 337status_t Parcel::setData(const uint8_t* buffer, size_t len) 338{ 339 status_t err = restartWrite(len); 340 if (err == NO_ERROR) { 341 memcpy(const_cast<uint8_t*>(data()), buffer, len); 342 mDataSize = len; 343 mFdsKnown = false; 344 } 345 return err; 346} 347 348status_t Parcel::appendFrom(Parcel *parcel, size_t offset, size_t len) 349{ 350 const sp<ProcessState> proc(ProcessState::self()); 351 status_t err; 352 uint8_t *data = parcel->mData; 353 size_t *objects = parcel->mObjects; 354 size_t size = parcel->mObjectsSize; 355 int startPos = mDataPos; 356 int firstIndex = -1, lastIndex = -2; 357 358 if (len == 0) { 359 return NO_ERROR; 360 } 361 362 // range checks against the source parcel size 363 if ((offset > parcel->mDataSize) 364 || (len > parcel->mDataSize) 365 || (offset + len > parcel->mDataSize)) { 366 return BAD_VALUE; 367 } 368 369 // Count objects in range 370 for (int i = 0; i < (int) size; i++) { 371 size_t off = objects[i]; 372 if ((off >= offset) && (off < offset + len)) { 373 if (firstIndex == -1) { 374 firstIndex = i; 375 } 376 lastIndex = i; 377 } 378 } 379 int numObjects = lastIndex - firstIndex + 1; 380 381 // grow data 382 err = growData(len); 383 if (err != NO_ERROR) { 384 return err; 385 } 386 387 // append data 388 memcpy(mData + mDataPos, data + offset, len); 389 mDataPos += len; 390 mDataSize += len; 391 392 if (numObjects > 0) { 393 // grow objects 394 if (mObjectsCapacity < mObjectsSize + numObjects) { 395 int newSize = ((mObjectsSize + numObjects)*3)/2; 396 size_t *objects = 397 (size_t*)realloc(mObjects, newSize*sizeof(size_t)); 398 if (objects == (size_t*)0) { 399 return NO_MEMORY; 400 } 401 mObjects = objects; 402 mObjectsCapacity = newSize; 403 } 404 405 // append and acquire objects 406 int idx = mObjectsSize; 407 for (int i = firstIndex; i <= lastIndex; i++) { 408 size_t off = objects[i] - offset + startPos; 409 mObjects[idx++] = off; 410 mObjectsSize++; 411 412 flat_binder_object* flat 413 = reinterpret_cast<flat_binder_object*>(mData + off); 414 acquire_object(proc, *flat, this); 415 416 if (flat->type == BINDER_TYPE_FD) { 417 // If this is a file descriptor, we need to dup it so the 418 // new Parcel now owns its own fd, and can declare that we 419 // officially know we have fds. 420 flat->handle = dup(flat->handle); 421 flat->cookie = (void*)1; 422 mHasFds = mFdsKnown = true; 423 } 424 } 425 } 426 427 return NO_ERROR; 428} 429 430bool Parcel::hasFileDescriptors() const 431{ 432 if (!mFdsKnown) { 433 scanForFds(); 434 } 435 return mHasFds; 436} 437 438status_t Parcel::writeInterfaceToken(const String16& interface) 439{ 440 // currently the interface identification token is just its name as a string 441 return writeString16(interface); 442} 443 444bool Parcel::enforceInterface(const String16& interface) const 445{ 446 String16 str = readString16(); 447 if (str == interface) { 448 return true; 449 } else { 450 LOGW("**** enforceInterface() expected '%s' but read '%s'\n", 451 String8(interface).string(), String8(str).string()); 452 return false; 453 } 454} 455 456const size_t* Parcel::objects() const 457{ 458 return mObjects; 459} 460 461size_t Parcel::objectsCount() const 462{ 463 return mObjectsSize; 464} 465 466status_t Parcel::errorCheck() const 467{ 468 return mError; 469} 470 471void Parcel::setError(status_t err) 472{ 473 mError = err; 474} 475 476status_t Parcel::finishWrite(size_t len) 477{ 478 //printf("Finish write of %d\n", len); 479 mDataPos += len; 480 LOGV("finishWrite Setting data pos of %p to %d\n", this, mDataPos); 481 if (mDataPos > mDataSize) { 482 mDataSize = mDataPos; 483 LOGV("finishWrite Setting data size of %p to %d\n", this, mDataSize); 484 } 485 //printf("New pos=%d, size=%d\n", mDataPos, mDataSize); 486 return NO_ERROR; 487} 488 489status_t Parcel::writeUnpadded(const void* data, size_t len) 490{ 491 size_t end = mDataPos + len; 492 if (end < mDataPos) { 493 // integer overflow 494 return BAD_VALUE; 495 } 496 497 if (end <= mDataCapacity) { 498restart_write: 499 memcpy(mData+mDataPos, data, len); 500 return finishWrite(len); 501 } 502 503 status_t err = growData(len); 504 if (err == NO_ERROR) goto restart_write; 505 return err; 506} 507 508status_t Parcel::write(const void* data, size_t len) 509{ 510 void* const d = writeInplace(len); 511 if (d) { 512 memcpy(d, data, len); 513 return NO_ERROR; 514 } 515 return mError; 516} 517 518void* Parcel::writeInplace(size_t len) 519{ 520 const size_t padded = PAD_SIZE(len); 521 522 // sanity check for integer overflow 523 if (mDataPos+padded < mDataPos) { 524 return NULL; 525 } 526 527 if ((mDataPos+padded) <= mDataCapacity) { 528restart_write: 529 //printf("Writing %ld bytes, padded to %ld\n", len, padded); 530 uint8_t* const data = mData+mDataPos; 531 532 // Need to pad at end? 533 if (padded != len) { 534#if BYTE_ORDER == BIG_ENDIAN 535 static const uint32_t mask[4] = { 536 0x00000000, 0xffffff00, 0xffff0000, 0xff000000 537 }; 538#endif 539#if BYTE_ORDER == LITTLE_ENDIAN 540 static const uint32_t mask[4] = { 541 0x00000000, 0x00ffffff, 0x0000ffff, 0x000000ff 542 }; 543#endif 544 //printf("Applying pad mask: %p to %p\n", (void*)mask[padded-len], 545 // *reinterpret_cast<void**>(data+padded-4)); 546 *reinterpret_cast<uint32_t*>(data+padded-4) &= mask[padded-len]; 547 } 548 549 finishWrite(padded); 550 return data; 551 } 552 553 status_t err = growData(padded); 554 if (err == NO_ERROR) goto restart_write; 555 return NULL; 556} 557 558status_t Parcel::writeInt32(int32_t val) 559{ 560 if ((mDataPos+sizeof(val)) <= mDataCapacity) { 561restart_write: 562 *reinterpret_cast<int32_t*>(mData+mDataPos) = val; 563 return finishWrite(sizeof(val)); 564 } 565 566 status_t err = growData(sizeof(val)); 567 if (err == NO_ERROR) goto restart_write; 568 return err; 569} 570 571status_t Parcel::writeInt64(int64_t val) 572{ 573 if ((mDataPos+sizeof(val)) <= mDataCapacity) { 574restart_write: 575 *reinterpret_cast<int64_t*>(mData+mDataPos) = val; 576 return finishWrite(sizeof(val)); 577 } 578 579 status_t err = growData(sizeof(val)); 580 if (err == NO_ERROR) goto restart_write; 581 return err; 582} 583 584status_t Parcel::writeFloat(float val) 585{ 586 if ((mDataPos+sizeof(val)) <= mDataCapacity) { 587restart_write: 588 *reinterpret_cast<float*>(mData+mDataPos) = val; 589 return finishWrite(sizeof(val)); 590 } 591 592 status_t err = growData(sizeof(val)); 593 if (err == NO_ERROR) goto restart_write; 594 return err; 595} 596 597status_t Parcel::writeDouble(double val) 598{ 599 if ((mDataPos+sizeof(val)) <= mDataCapacity) { 600restart_write: 601 *reinterpret_cast<double*>(mData+mDataPos) = val; 602 return finishWrite(sizeof(val)); 603 } 604 605 status_t err = growData(sizeof(val)); 606 if (err == NO_ERROR) goto restart_write; 607 return err; 608} 609 610status_t Parcel::writeCString(const char* str) 611{ 612 return write(str, strlen(str)+1); 613} 614 615status_t Parcel::writeString8(const String8& str) 616{ 617 status_t err = writeInt32(str.bytes()); 618 if (err == NO_ERROR) { 619 err = write(str.string(), str.bytes()+1); 620 } 621 return err; 622} 623 624status_t Parcel::writeString16(const String16& str) 625{ 626 return writeString16(str.string(), str.size()); 627} 628 629status_t Parcel::writeString16(const char16_t* str, size_t len) 630{ 631 if (str == NULL) return writeInt32(-1); 632 633 status_t err = writeInt32(len); 634 if (err == NO_ERROR) { 635 len *= sizeof(char16_t); 636 uint8_t* data = (uint8_t*)writeInplace(len+sizeof(char16_t)); 637 if (data) { 638 memcpy(data, str, len); 639 *reinterpret_cast<char16_t*>(data+len) = 0; 640 return NO_ERROR; 641 } 642 err = mError; 643 } 644 return err; 645} 646 647status_t Parcel::writeStrongBinder(const sp<IBinder>& val) 648{ 649 return flatten_binder(ProcessState::self(), val, this); 650} 651 652status_t Parcel::writeWeakBinder(const wp<IBinder>& val) 653{ 654 return flatten_binder(ProcessState::self(), val, this); 655} 656 657status_t Parcel::writeNativeHandle(const native_handle* handle) 658{ 659 if (handle->version != sizeof(native_handle)) 660 return BAD_TYPE; 661 662 status_t err; 663 err = writeInt32(handle->numFds); 664 if (err != NO_ERROR) return err; 665 666 err = writeInt32(handle->numInts); 667 if (err != NO_ERROR) return err; 668 669 for (int i=0 ; err==NO_ERROR && i<handle->numFds ; i++) 670 err = writeDupFileDescriptor(handle->data[i]); 671 672 if (err != NO_ERROR) { 673 LOGD("write native handle, write dup fd failed"); 674 return err; 675 } 676 err = write(handle->data + handle->numFds, sizeof(int)*handle->numInts); 677 return err; 678} 679 680status_t Parcel::writeFileDescriptor(int fd) 681{ 682 flat_binder_object obj; 683 obj.type = BINDER_TYPE_FD; 684 obj.flags = 0x7f | FLAT_BINDER_FLAG_ACCEPTS_FDS; 685 obj.handle = fd; 686 obj.cookie = (void*)0; 687 return writeObject(obj, true); 688} 689 690status_t Parcel::writeDupFileDescriptor(int fd) 691{ 692 flat_binder_object obj; 693 obj.type = BINDER_TYPE_FD; 694 obj.flags = 0x7f | FLAT_BINDER_FLAG_ACCEPTS_FDS; 695 obj.handle = dup(fd); 696 obj.cookie = (void*)1; 697 return writeObject(obj, true); 698} 699 700status_t Parcel::writeObject(const flat_binder_object& val, bool nullMetaData) 701{ 702 const bool enoughData = (mDataPos+sizeof(val)) <= mDataCapacity; 703 const bool enoughObjects = mObjectsSize < mObjectsCapacity; 704 if (enoughData && enoughObjects) { 705restart_write: 706 *reinterpret_cast<flat_binder_object*>(mData+mDataPos) = val; 707 708 // Need to write meta-data? 709 if (nullMetaData || val.binder != NULL) { 710 mObjects[mObjectsSize] = mDataPos; 711 acquire_object(ProcessState::self(), val, this); 712 mObjectsSize++; 713 } 714 715 // remember if it's a file descriptor 716 if (val.type == BINDER_TYPE_FD) { 717 mHasFds = mFdsKnown = true; 718 } 719 720 return finishWrite(sizeof(flat_binder_object)); 721 } 722 723 if (!enoughData) { 724 const status_t err = growData(sizeof(val)); 725 if (err != NO_ERROR) return err; 726 } 727 if (!enoughObjects) { 728 size_t newSize = ((mObjectsSize+2)*3)/2; 729 size_t* objects = (size_t*)realloc(mObjects, newSize*sizeof(size_t)); 730 if (objects == NULL) return NO_MEMORY; 731 mObjects = objects; 732 mObjectsCapacity = newSize; 733 } 734 735 goto restart_write; 736} 737 738 739void Parcel::remove(size_t start, size_t amt) 740{ 741 LOG_ALWAYS_FATAL("Parcel::remove() not yet implemented!"); 742} 743 744status_t Parcel::read(void* outData, size_t len) const 745{ 746 if ((mDataPos+PAD_SIZE(len)) >= mDataPos && (mDataPos+PAD_SIZE(len)) <= mDataSize) { 747 memcpy(outData, mData+mDataPos, len); 748 mDataPos += PAD_SIZE(len); 749 LOGV("read Setting data pos of %p to %d\n", this, mDataPos); 750 return NO_ERROR; 751 } 752 return NOT_ENOUGH_DATA; 753} 754 755const void* Parcel::readInplace(size_t len) const 756{ 757 if ((mDataPos+PAD_SIZE(len)) >= mDataPos && (mDataPos+PAD_SIZE(len)) <= mDataSize) { 758 const void* data = mData+mDataPos; 759 mDataPos += PAD_SIZE(len); 760 LOGV("readInplace Setting data pos of %p to %d\n", this, mDataPos); 761 return data; 762 } 763 return NULL; 764} 765 766status_t Parcel::readInt32(int32_t *pArg) const 767{ 768 if ((mDataPos+sizeof(int32_t)) <= mDataSize) { 769 const void* data = mData+mDataPos; 770 mDataPos += sizeof(int32_t); 771 *pArg = *reinterpret_cast<const int32_t*>(data); 772 return NO_ERROR; 773 } else { 774 return NOT_ENOUGH_DATA; 775 } 776} 777 778int32_t Parcel::readInt32() const 779{ 780 if ((mDataPos+sizeof(int32_t)) <= mDataSize) { 781 const void* data = mData+mDataPos; 782 mDataPos += sizeof(int32_t); 783 LOGV("readInt32 Setting data pos of %p to %d\n", this, mDataPos); 784 return *reinterpret_cast<const int32_t*>(data); 785 } 786 return 0; 787} 788 789 790status_t Parcel::readInt64(int64_t *pArg) const 791{ 792 if ((mDataPos+sizeof(int64_t)) <= mDataSize) { 793 const void* data = mData+mDataPos; 794 mDataPos += sizeof(int64_t); 795 *pArg = *reinterpret_cast<const int64_t*>(data); 796 LOGV("readInt64 Setting data pos of %p to %d\n", this, mDataPos); 797 return NO_ERROR; 798 } else { 799 return NOT_ENOUGH_DATA; 800 } 801} 802 803 804int64_t Parcel::readInt64() const 805{ 806 if ((mDataPos+sizeof(int64_t)) <= mDataSize) { 807 const void* data = mData+mDataPos; 808 mDataPos += sizeof(int64_t); 809 LOGV("readInt64 Setting data pos of %p to %d\n", this, mDataPos); 810 return *reinterpret_cast<const int64_t*>(data); 811 } 812 return 0; 813} 814 815status_t Parcel::readFloat(float *pArg) const 816{ 817 if ((mDataPos+sizeof(float)) <= mDataSize) { 818 const void* data = mData+mDataPos; 819 mDataPos += sizeof(float); 820 LOGV("readFloat Setting data pos of %p to %d\n", this, mDataPos); 821 *pArg = *reinterpret_cast<const float*>(data); 822 return NO_ERROR; 823 } else { 824 return NOT_ENOUGH_DATA; 825 } 826} 827 828 829float Parcel::readFloat() const 830{ 831 if ((mDataPos+sizeof(float)) <= mDataSize) { 832 const void* data = mData+mDataPos; 833 mDataPos += sizeof(float); 834 LOGV("readFloat Setting data pos of %p to %d\n", this, mDataPos); 835 return *reinterpret_cast<const float*>(data); 836 } 837 return 0; 838} 839 840status_t Parcel::readDouble(double *pArg) const 841{ 842 if ((mDataPos+sizeof(double)) <= mDataSize) { 843 const void* data = mData+mDataPos; 844 mDataPos += sizeof(double); 845 LOGV("readDouble Setting data pos of %p to %d\n", this, mDataPos); 846 *pArg = *reinterpret_cast<const double*>(data); 847 return NO_ERROR; 848 } else { 849 return NOT_ENOUGH_DATA; 850 } 851} 852 853 854double Parcel::readDouble() const 855{ 856 if ((mDataPos+sizeof(double)) <= mDataSize) { 857 const void* data = mData+mDataPos; 858 mDataPos += sizeof(double); 859 LOGV("readDouble Setting data pos of %p to %d\n", this, mDataPos); 860 return *reinterpret_cast<const double*>(data); 861 } 862 return 0; 863} 864 865 866const char* Parcel::readCString() const 867{ 868 const size_t avail = mDataSize-mDataPos; 869 if (avail > 0) { 870 const char* str = reinterpret_cast<const char*>(mData+mDataPos); 871 // is the string's trailing NUL within the parcel's valid bounds? 872 const char* eos = reinterpret_cast<const char*>(memchr(str, 0, avail)); 873 if (eos) { 874 const size_t len = eos - str; 875 mDataPos += PAD_SIZE(len+1); 876 LOGV("readCString Setting data pos of %p to %d\n", this, mDataPos); 877 return str; 878 } 879 } 880 return NULL; 881} 882 883String8 Parcel::readString8() const 884{ 885 int32_t size = readInt32(); 886 // watch for potential int overflow adding 1 for trailing NUL 887 if (size > 0 && size < INT32_MAX) { 888 const char* str = (const char*)readInplace(size+1); 889 if (str) return String8(str, size); 890 } 891 return String8(); 892} 893 894String16 Parcel::readString16() const 895{ 896 size_t len; 897 const char16_t* str = readString16Inplace(&len); 898 if (str) return String16(str, len); 899 LOGE("Reading a NULL string not supported here."); 900 return String16(); 901} 902 903const char16_t* Parcel::readString16Inplace(size_t* outLen) const 904{ 905 int32_t size = readInt32(); 906 // watch for potential int overflow from size+1 907 if (size >= 0 && size < INT32_MAX) { 908 *outLen = size; 909 const char16_t* str = (const char16_t*)readInplace((size+1)*sizeof(char16_t)); 910 if (str != NULL) { 911 return str; 912 } 913 } 914 *outLen = 0; 915 return NULL; 916} 917 918sp<IBinder> Parcel::readStrongBinder() const 919{ 920 sp<IBinder> val; 921 unflatten_binder(ProcessState::self(), *this, &val); 922 return val; 923} 924 925wp<IBinder> Parcel::readWeakBinder() const 926{ 927 wp<IBinder> val; 928 unflatten_binder(ProcessState::self(), *this, &val); 929 return val; 930} 931 932 933native_handle* Parcel::readNativeHandle() const 934{ 935 int numFds, numInts; 936 status_t err; 937 err = readInt32(&numFds); 938 if (err != NO_ERROR) return 0; 939 err = readInt32(&numInts); 940 if (err != NO_ERROR) return 0; 941 942 native_handle* h = native_handle_create(numFds, numInts); 943 for (int i=0 ; err==NO_ERROR && i<numFds ; i++) { 944 h->data[i] = dup(readFileDescriptor()); 945 if (h->data[i] < 0) err = BAD_VALUE; 946 } 947 err = read(h->data + numFds, sizeof(int)*numInts); 948 if (err != NO_ERROR) { 949 native_handle_close(h); 950 native_handle_delete(h); 951 h = 0; 952 } 953 return h; 954} 955 956 957int Parcel::readFileDescriptor() const 958{ 959 const flat_binder_object* flat = readObject(true); 960 if (flat) { 961 switch (flat->type) { 962 case BINDER_TYPE_FD: 963 //LOGI("Returning file descriptor %ld from parcel %p\n", flat->handle, this); 964 return flat->handle; 965 } 966 } 967 return BAD_TYPE; 968} 969 970const flat_binder_object* Parcel::readObject(bool nullMetaData) const 971{ 972 const size_t DPOS = mDataPos; 973 if ((DPOS+sizeof(flat_binder_object)) <= mDataSize) { 974 const flat_binder_object* obj 975 = reinterpret_cast<const flat_binder_object*>(mData+DPOS); 976 mDataPos = DPOS + sizeof(flat_binder_object); 977 if (!nullMetaData && (obj->cookie == NULL && obj->binder == NULL)) { 978 // When transferring a NULL object, we don't write it into 979 // the object list, so we don't want to check for it when 980 // reading. 981 LOGV("readObject Setting data pos of %p to %d\n", this, mDataPos); 982 return obj; 983 } 984 985 // Ensure that this object is valid... 986 size_t* const OBJS = mObjects; 987 const size_t N = mObjectsSize; 988 size_t opos = mNextObjectHint; 989 990 if (N > 0) { 991 LOGV("Parcel %p looking for obj at %d, hint=%d\n", 992 this, DPOS, opos); 993 994 // Start at the current hint position, looking for an object at 995 // the current data position. 996 if (opos < N) { 997 while (opos < (N-1) && OBJS[opos] < DPOS) { 998 opos++; 999 } 1000 } else { 1001 opos = N-1; 1002 } 1003 if (OBJS[opos] == DPOS) { 1004 // Found it! 1005 LOGV("Parcel found obj %d at index %d with forward search", 1006 this, DPOS, opos); 1007 mNextObjectHint = opos+1; 1008 LOGV("readObject Setting data pos of %p to %d\n", this, mDataPos); 1009 return obj; 1010 } 1011 1012 // Look backwards for it... 1013 while (opos > 0 && OBJS[opos] > DPOS) { 1014 opos--; 1015 } 1016 if (OBJS[opos] == DPOS) { 1017 // Found it! 1018 LOGV("Parcel found obj %d at index %d with backward search", 1019 this, DPOS, opos); 1020 mNextObjectHint = opos+1; 1021 LOGV("readObject Setting data pos of %p to %d\n", this, mDataPos); 1022 return obj; 1023 } 1024 } 1025 LOGW("Attempt to read object from Parcel %p at offset %d that is not in the object list", 1026 this, DPOS); 1027 } 1028 return NULL; 1029} 1030 1031void Parcel::closeFileDescriptors() 1032{ 1033 size_t i = mObjectsSize; 1034 if (i > 0) { 1035 //LOGI("Closing file descriptors for %d objects...", mObjectsSize); 1036 } 1037 while (i > 0) { 1038 i--; 1039 const flat_binder_object* flat 1040 = reinterpret_cast<flat_binder_object*>(mData+mObjects[i]); 1041 if (flat->type == BINDER_TYPE_FD) { 1042 //LOGI("Closing fd: %ld\n", flat->handle); 1043 close(flat->handle); 1044 } 1045 } 1046} 1047 1048const uint8_t* Parcel::ipcData() const 1049{ 1050 return mData; 1051} 1052 1053size_t Parcel::ipcDataSize() const 1054{ 1055 return (mDataSize > mDataPos ? mDataSize : mDataPos); 1056} 1057 1058const size_t* Parcel::ipcObjects() const 1059{ 1060 return mObjects; 1061} 1062 1063size_t Parcel::ipcObjectsCount() const 1064{ 1065 return mObjectsSize; 1066} 1067 1068void Parcel::ipcSetDataReference(const uint8_t* data, size_t dataSize, 1069 const size_t* objects, size_t objectsCount, release_func relFunc, void* relCookie) 1070{ 1071 freeDataNoInit(); 1072 mError = NO_ERROR; 1073 mData = const_cast<uint8_t*>(data); 1074 mDataSize = mDataCapacity = dataSize; 1075 //LOGI("setDataReference Setting data size of %p to %lu (pid=%d)\n", this, mDataSize, getpid()); 1076 mDataPos = 0; 1077 LOGV("setDataReference Setting data pos of %p to %d\n", this, mDataPos); 1078 mObjects = const_cast<size_t*>(objects); 1079 mObjectsSize = mObjectsCapacity = objectsCount; 1080 mNextObjectHint = 0; 1081 mOwner = relFunc; 1082 mOwnerCookie = relCookie; 1083 scanForFds(); 1084} 1085 1086void Parcel::print(TextOutput& to, uint32_t flags) const 1087{ 1088 to << "Parcel("; 1089 1090 if (errorCheck() != NO_ERROR) { 1091 const status_t err = errorCheck(); 1092 to << "Error: " << (void*)err << " \"" << strerror(-err) << "\""; 1093 } else if (dataSize() > 0) { 1094 const uint8_t* DATA = data(); 1095 to << indent << HexDump(DATA, dataSize()) << dedent; 1096 const size_t* OBJS = objects(); 1097 const size_t N = objectsCount(); 1098 for (size_t i=0; i<N; i++) { 1099 const flat_binder_object* flat 1100 = reinterpret_cast<const flat_binder_object*>(DATA+OBJS[i]); 1101 to << endl << "Object #" << i << " @ " << (void*)OBJS[i] << ": " 1102 << TypeCode(flat->type & 0x7f7f7f00) 1103 << " = " << flat->binder; 1104 } 1105 } else { 1106 to << "NULL"; 1107 } 1108 1109 to << ")"; 1110} 1111 1112void Parcel::releaseObjects() 1113{ 1114 const sp<ProcessState> proc(ProcessState::self()); 1115 size_t i = mObjectsSize; 1116 uint8_t* const data = mData; 1117 size_t* const objects = mObjects; 1118 while (i > 0) { 1119 i--; 1120 const flat_binder_object* flat 1121 = reinterpret_cast<flat_binder_object*>(data+objects[i]); 1122 release_object(proc, *flat, this); 1123 } 1124} 1125 1126void Parcel::acquireObjects() 1127{ 1128 const sp<ProcessState> proc(ProcessState::self()); 1129 size_t i = mObjectsSize; 1130 uint8_t* const data = mData; 1131 size_t* const objects = mObjects; 1132 while (i > 0) { 1133 i--; 1134 const flat_binder_object* flat 1135 = reinterpret_cast<flat_binder_object*>(data+objects[i]); 1136 acquire_object(proc, *flat, this); 1137 } 1138} 1139 1140void Parcel::freeData() 1141{ 1142 freeDataNoInit(); 1143 initState(); 1144} 1145 1146void Parcel::freeDataNoInit() 1147{ 1148 if (mOwner) { 1149 //LOGI("Freeing data ref of %p (pid=%d)\n", this, getpid()); 1150 mOwner(this, mData, mDataSize, mObjects, mObjectsSize, mOwnerCookie); 1151 } else { 1152 releaseObjects(); 1153 if (mData) free(mData); 1154 if (mObjects) free(mObjects); 1155 } 1156} 1157 1158status_t Parcel::growData(size_t len) 1159{ 1160 size_t newSize = ((mDataSize+len)*3)/2; 1161 return (newSize <= mDataSize) 1162 ? (status_t) NO_MEMORY 1163 : continueWrite(newSize); 1164} 1165 1166status_t Parcel::restartWrite(size_t desired) 1167{ 1168 if (mOwner) { 1169 freeData(); 1170 return continueWrite(desired); 1171 } 1172 1173 uint8_t* data = (uint8_t*)realloc(mData, desired); 1174 if (!data && desired > mDataCapacity) { 1175 mError = NO_MEMORY; 1176 return NO_MEMORY; 1177 } 1178 1179 releaseObjects(); 1180 1181 if (data) { 1182 mData = data; 1183 mDataCapacity = desired; 1184 } 1185 1186 mDataSize = mDataPos = 0; 1187 LOGV("restartWrite Setting data size of %p to %d\n", this, mDataSize); 1188 LOGV("restartWrite Setting data pos of %p to %d\n", this, mDataPos); 1189 1190 free(mObjects); 1191 mObjects = NULL; 1192 mObjectsSize = mObjectsCapacity = 0; 1193 mNextObjectHint = 0; 1194 mHasFds = false; 1195 mFdsKnown = true; 1196 1197 return NO_ERROR; 1198} 1199 1200status_t Parcel::continueWrite(size_t desired) 1201{ 1202 // If shrinking, first adjust for any objects that appear 1203 // after the new data size. 1204 size_t objectsSize = mObjectsSize; 1205 if (desired < mDataSize) { 1206 if (desired == 0) { 1207 objectsSize = 0; 1208 } else { 1209 while (objectsSize > 0) { 1210 if (mObjects[objectsSize-1] < desired) 1211 break; 1212 objectsSize--; 1213 } 1214 } 1215 } 1216 1217 if (mOwner) { 1218 // If the size is going to zero, just release the owner's data. 1219 if (desired == 0) { 1220 freeData(); 1221 return NO_ERROR; 1222 } 1223 1224 // If there is a different owner, we need to take 1225 // posession. 1226 uint8_t* data = (uint8_t*)malloc(desired); 1227 if (!data) { 1228 mError = NO_MEMORY; 1229 return NO_MEMORY; 1230 } 1231 size_t* objects = NULL; 1232 1233 if (objectsSize) { 1234 objects = (size_t*)malloc(objectsSize*sizeof(size_t)); 1235 if (!objects) { 1236 mError = NO_MEMORY; 1237 return NO_MEMORY; 1238 } 1239 1240 // Little hack to only acquire references on objects 1241 // we will be keeping. 1242 size_t oldObjectsSize = mObjectsSize; 1243 mObjectsSize = objectsSize; 1244 acquireObjects(); 1245 mObjectsSize = oldObjectsSize; 1246 } 1247 1248 if (mData) { 1249 memcpy(data, mData, mDataSize < desired ? mDataSize : desired); 1250 } 1251 if (objects && mObjects) { 1252 memcpy(objects, mObjects, objectsSize*sizeof(size_t)); 1253 } 1254 //LOGI("Freeing data ref of %p (pid=%d)\n", this, getpid()); 1255 mOwner(this, mData, mDataSize, mObjects, mObjectsSize, mOwnerCookie); 1256 mOwner = NULL; 1257 1258 mData = data; 1259 mObjects = objects; 1260 mDataSize = (mDataSize < desired) ? mDataSize : desired; 1261 LOGV("continueWrite Setting data size of %p to %d\n", this, mDataSize); 1262 mDataCapacity = desired; 1263 mObjectsSize = mObjectsCapacity = objectsSize; 1264 mNextObjectHint = 0; 1265 1266 } else if (mData) { 1267 if (objectsSize < mObjectsSize) { 1268 // Need to release refs on any objects we are dropping. 1269 const sp<ProcessState> proc(ProcessState::self()); 1270 for (size_t i=objectsSize; i<mObjectsSize; i++) { 1271 const flat_binder_object* flat 1272 = reinterpret_cast<flat_binder_object*>(mData+mObjects[i]); 1273 if (flat->type == BINDER_TYPE_FD) { 1274 // will need to rescan because we may have lopped off the only FDs 1275 mFdsKnown = false; 1276 } 1277 release_object(proc, *flat, this); 1278 } 1279 size_t* objects = 1280 (size_t*)realloc(mObjects, objectsSize*sizeof(size_t)); 1281 if (objects) { 1282 mObjects = objects; 1283 } 1284 mObjectsSize = objectsSize; 1285 mNextObjectHint = 0; 1286 } 1287 1288 // We own the data, so we can just do a realloc(). 1289 if (desired > mDataCapacity) { 1290 uint8_t* data = (uint8_t*)realloc(mData, desired); 1291 if (data) { 1292 mData = data; 1293 mDataCapacity = desired; 1294 } else if (desired > mDataCapacity) { 1295 mError = NO_MEMORY; 1296 return NO_MEMORY; 1297 } 1298 } else { 1299 mDataSize = desired; 1300 LOGV("continueWrite Setting data size of %p to %d\n", this, mDataSize); 1301 if (mDataPos > desired) { 1302 mDataPos = desired; 1303 LOGV("continueWrite Setting data pos of %p to %d\n", this, mDataPos); 1304 } 1305 } 1306 1307 } else { 1308 // This is the first data. Easy! 1309 uint8_t* data = (uint8_t*)malloc(desired); 1310 if (!data) { 1311 mError = NO_MEMORY; 1312 return NO_MEMORY; 1313 } 1314 1315 if(!(mDataCapacity == 0 && mObjects == NULL 1316 && mObjectsCapacity == 0)) { 1317 LOGE("continueWrite: %d/%p/%d/%d", mDataCapacity, mObjects, mObjectsCapacity, desired); 1318 } 1319 1320 mData = data; 1321 mDataSize = mDataPos = 0; 1322 LOGV("continueWrite Setting data size of %p to %d\n", this, mDataSize); 1323 LOGV("continueWrite Setting data pos of %p to %d\n", this, mDataPos); 1324 mDataCapacity = desired; 1325 } 1326 1327 return NO_ERROR; 1328} 1329 1330void Parcel::initState() 1331{ 1332 mError = NO_ERROR; 1333 mData = 0; 1334 mDataSize = 0; 1335 mDataCapacity = 0; 1336 mDataPos = 0; 1337 LOGV("initState Setting data size of %p to %d\n", this, mDataSize); 1338 LOGV("initState Setting data pos of %p to %d\n", this, mDataPos); 1339 mObjects = NULL; 1340 mObjectsSize = 0; 1341 mObjectsCapacity = 0; 1342 mNextObjectHint = 0; 1343 mHasFds = false; 1344 mFdsKnown = true; 1345 mOwner = NULL; 1346} 1347 1348void Parcel::scanForFds() const 1349{ 1350 bool hasFds = false; 1351 for (size_t i=0; i<mObjectsSize; i++) { 1352 const flat_binder_object* flat 1353 = reinterpret_cast<const flat_binder_object*>(mData + mObjects[i]); 1354 if (flat->type == BINDER_TYPE_FD) { 1355 hasFds = true; 1356 break; 1357 } 1358 } 1359 mHasFds = hasFds; 1360 mFdsKnown = true; 1361} 1362 1363}; // namespace android 1364