The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 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 | |
Mathias Agopian | 0795272 | 2009-05-19 19:08:10 -0700 | [diff] [blame] | 20 | #include <binder/Parcel.h> |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 21 | |
Mathias Agopian | 0795272 | 2009-05-19 19:08:10 -0700 | [diff] [blame] | 22 | #include <binder/Binder.h> |
| 23 | #include <binder/BpBinder.h> |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 24 | #include <utils/Debug.h> |
Mathias Agopian | 0795272 | 2009-05-19 19:08:10 -0700 | [diff] [blame] | 25 | #include <binder/ProcessState.h> |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 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 | |
Mathias Agopian | 25ba5b6 | 2009-05-18 15:08:03 -0700 | [diff] [blame] | 32 | #include <private/binder/binder_module.h> |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 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. |
| 51 | struct small_flat_data |
| 52 | { |
| 53 | uint32_t type; |
| 54 | uint32_t data; |
| 55 | }; |
| 56 | |
| 57 | namespace android { |
| 58 | |
| 59 | void 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 | |
| 96 | void 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 | |
| 132 | inline 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 | |
| 138 | status_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 | |
| 169 | status_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 | |
| 217 | inline static status_t finish_unflatten_binder( |
| 218 | BpBinder* proxy, const flat_binder_object& flat, const Parcel& in) |
| 219 | { |
| 220 | return NO_ERROR; |
| 221 | } |
| 222 | |
| 223 | status_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 | |
| 242 | status_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 | |
| 273 | Parcel::Parcel() |
| 274 | { |
| 275 | initState(); |
| 276 | } |
| 277 | |
| 278 | Parcel::~Parcel() |
| 279 | { |
| 280 | freeDataNoInit(); |
| 281 | } |
| 282 | |
| 283 | const uint8_t* Parcel::data() const |
| 284 | { |
| 285 | return mData; |
| 286 | } |
| 287 | |
| 288 | size_t Parcel::dataSize() const |
| 289 | { |
| 290 | return (mDataSize > mDataPos ? mDataSize : mDataPos); |
| 291 | } |
| 292 | |
| 293 | size_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 | |
| 304 | size_t Parcel::dataPosition() const |
| 305 | { |
| 306 | return mDataPos; |
| 307 | } |
| 308 | |
| 309 | size_t Parcel::dataCapacity() const |
| 310 | { |
| 311 | return mDataCapacity; |
| 312 | } |
| 313 | |
| 314 | status_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 | |
| 325 | void Parcel::setDataPosition(size_t pos) const |
| 326 | { |
| 327 | mDataPos = pos; |
| 328 | mNextObjectHint = 0; |
| 329 | } |
| 330 | |
| 331 | status_t Parcel::setDataCapacity(size_t size) |
| 332 | { |
| 333 | if (size > mDataSize) return continueWrite(size); |
| 334 | return NO_ERROR; |
| 335 | } |
| 336 | |
| 337 | status_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 | |
| 348 | status_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 | |
Dianne Hackborn | 6aff905 | 2009-05-22 13:20:23 -0700 | [diff] [blame] | 412 | flat_binder_object* flat |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 413 | = reinterpret_cast<flat_binder_object*>(mData + off); |
| 414 | acquire_object(proc, *flat, this); |
| 415 | |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 416 | if (flat->type == BINDER_TYPE_FD) { |
Dianne Hackborn | 6aff905 | 2009-05-22 13:20:23 -0700 | [diff] [blame] | 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; |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 422 | mHasFds = mFdsKnown = true; |
| 423 | } |
| 424 | } |
| 425 | } |
| 426 | |
| 427 | return NO_ERROR; |
| 428 | } |
| 429 | |
| 430 | bool Parcel::hasFileDescriptors() const |
| 431 | { |
| 432 | if (!mFdsKnown) { |
| 433 | scanForFds(); |
| 434 | } |
| 435 | return mHasFds; |
| 436 | } |
| 437 | |
| 438 | status_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 | |
Mathias Agopian | aaf834a | 2009-05-22 19:00:22 -0700 | [diff] [blame] | 444 | bool Parcel::checkInterface(IBinder* binder) const |
| 445 | { |
| 446 | return enforceInterface(binder->getInterfaceDescriptor()); |
| 447 | } |
| 448 | |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 449 | bool Parcel::enforceInterface(const String16& interface) const |
| 450 | { |
Mathias Agopian | aaf834a | 2009-05-22 19:00:22 -0700 | [diff] [blame] | 451 | const String16 str(readString16()); |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 452 | if (str == interface) { |
| 453 | return true; |
| 454 | } else { |
| 455 | LOGW("**** enforceInterface() expected '%s' but read '%s'\n", |
| 456 | String8(interface).string(), String8(str).string()); |
| 457 | return false; |
| 458 | } |
| 459 | } |
| 460 | |
| 461 | const size_t* Parcel::objects() const |
| 462 | { |
| 463 | return mObjects; |
| 464 | } |
| 465 | |
| 466 | size_t Parcel::objectsCount() const |
| 467 | { |
| 468 | return mObjectsSize; |
| 469 | } |
| 470 | |
| 471 | status_t Parcel::errorCheck() const |
| 472 | { |
| 473 | return mError; |
| 474 | } |
| 475 | |
| 476 | void Parcel::setError(status_t err) |
| 477 | { |
| 478 | mError = err; |
| 479 | } |
| 480 | |
| 481 | status_t Parcel::finishWrite(size_t len) |
| 482 | { |
| 483 | //printf("Finish write of %d\n", len); |
| 484 | mDataPos += len; |
| 485 | LOGV("finishWrite Setting data pos of %p to %d\n", this, mDataPos); |
| 486 | if (mDataPos > mDataSize) { |
| 487 | mDataSize = mDataPos; |
| 488 | LOGV("finishWrite Setting data size of %p to %d\n", this, mDataSize); |
| 489 | } |
| 490 | //printf("New pos=%d, size=%d\n", mDataPos, mDataSize); |
| 491 | return NO_ERROR; |
| 492 | } |
| 493 | |
| 494 | status_t Parcel::writeUnpadded(const void* data, size_t len) |
| 495 | { |
| 496 | size_t end = mDataPos + len; |
| 497 | if (end < mDataPos) { |
| 498 | // integer overflow |
| 499 | return BAD_VALUE; |
| 500 | } |
| 501 | |
| 502 | if (end <= mDataCapacity) { |
| 503 | restart_write: |
| 504 | memcpy(mData+mDataPos, data, len); |
| 505 | return finishWrite(len); |
| 506 | } |
| 507 | |
| 508 | status_t err = growData(len); |
| 509 | if (err == NO_ERROR) goto restart_write; |
| 510 | return err; |
| 511 | } |
| 512 | |
| 513 | status_t Parcel::write(const void* data, size_t len) |
| 514 | { |
| 515 | void* const d = writeInplace(len); |
| 516 | if (d) { |
| 517 | memcpy(d, data, len); |
| 518 | return NO_ERROR; |
| 519 | } |
| 520 | return mError; |
| 521 | } |
| 522 | |
| 523 | void* Parcel::writeInplace(size_t len) |
| 524 | { |
| 525 | const size_t padded = PAD_SIZE(len); |
| 526 | |
| 527 | // sanity check for integer overflow |
| 528 | if (mDataPos+padded < mDataPos) { |
| 529 | return NULL; |
| 530 | } |
| 531 | |
| 532 | if ((mDataPos+padded) <= mDataCapacity) { |
| 533 | restart_write: |
| 534 | //printf("Writing %ld bytes, padded to %ld\n", len, padded); |
| 535 | uint8_t* const data = mData+mDataPos; |
| 536 | |
| 537 | // Need to pad at end? |
| 538 | if (padded != len) { |
| 539 | #if BYTE_ORDER == BIG_ENDIAN |
| 540 | static const uint32_t mask[4] = { |
| 541 | 0x00000000, 0xffffff00, 0xffff0000, 0xff000000 |
| 542 | }; |
| 543 | #endif |
| 544 | #if BYTE_ORDER == LITTLE_ENDIAN |
| 545 | static const uint32_t mask[4] = { |
| 546 | 0x00000000, 0x00ffffff, 0x0000ffff, 0x000000ff |
| 547 | }; |
| 548 | #endif |
| 549 | //printf("Applying pad mask: %p to %p\n", (void*)mask[padded-len], |
| 550 | // *reinterpret_cast<void**>(data+padded-4)); |
| 551 | *reinterpret_cast<uint32_t*>(data+padded-4) &= mask[padded-len]; |
| 552 | } |
| 553 | |
| 554 | finishWrite(padded); |
| 555 | return data; |
| 556 | } |
| 557 | |
| 558 | status_t err = growData(padded); |
| 559 | if (err == NO_ERROR) goto restart_write; |
| 560 | return NULL; |
| 561 | } |
| 562 | |
| 563 | status_t Parcel::writeInt32(int32_t val) |
| 564 | { |
| 565 | if ((mDataPos+sizeof(val)) <= mDataCapacity) { |
| 566 | restart_write: |
| 567 | *reinterpret_cast<int32_t*>(mData+mDataPos) = val; |
| 568 | return finishWrite(sizeof(val)); |
| 569 | } |
| 570 | |
| 571 | status_t err = growData(sizeof(val)); |
| 572 | if (err == NO_ERROR) goto restart_write; |
| 573 | return err; |
| 574 | } |
| 575 | |
| 576 | status_t Parcel::writeInt64(int64_t val) |
| 577 | { |
| 578 | if ((mDataPos+sizeof(val)) <= mDataCapacity) { |
| 579 | restart_write: |
| 580 | *reinterpret_cast<int64_t*>(mData+mDataPos) = val; |
| 581 | return finishWrite(sizeof(val)); |
| 582 | } |
| 583 | |
| 584 | status_t err = growData(sizeof(val)); |
| 585 | if (err == NO_ERROR) goto restart_write; |
| 586 | return err; |
| 587 | } |
| 588 | |
| 589 | status_t Parcel::writeFloat(float val) |
| 590 | { |
| 591 | if ((mDataPos+sizeof(val)) <= mDataCapacity) { |
| 592 | restart_write: |
| 593 | *reinterpret_cast<float*>(mData+mDataPos) = val; |
| 594 | return finishWrite(sizeof(val)); |
| 595 | } |
| 596 | |
| 597 | status_t err = growData(sizeof(val)); |
| 598 | if (err == NO_ERROR) goto restart_write; |
| 599 | return err; |
| 600 | } |
| 601 | |
| 602 | status_t Parcel::writeDouble(double val) |
| 603 | { |
| 604 | if ((mDataPos+sizeof(val)) <= mDataCapacity) { |
| 605 | restart_write: |
| 606 | *reinterpret_cast<double*>(mData+mDataPos) = val; |
| 607 | return finishWrite(sizeof(val)); |
| 608 | } |
| 609 | |
| 610 | status_t err = growData(sizeof(val)); |
| 611 | if (err == NO_ERROR) goto restart_write; |
| 612 | return err; |
| 613 | } |
| 614 | |
| 615 | status_t Parcel::writeCString(const char* str) |
| 616 | { |
| 617 | return write(str, strlen(str)+1); |
| 618 | } |
| 619 | |
| 620 | status_t Parcel::writeString8(const String8& str) |
| 621 | { |
| 622 | status_t err = writeInt32(str.bytes()); |
| 623 | if (err == NO_ERROR) { |
| 624 | err = write(str.string(), str.bytes()+1); |
| 625 | } |
| 626 | return err; |
| 627 | } |
| 628 | |
| 629 | status_t Parcel::writeString16(const String16& str) |
| 630 | { |
| 631 | return writeString16(str.string(), str.size()); |
| 632 | } |
| 633 | |
| 634 | status_t Parcel::writeString16(const char16_t* str, size_t len) |
| 635 | { |
| 636 | if (str == NULL) return writeInt32(-1); |
| 637 | |
| 638 | status_t err = writeInt32(len); |
| 639 | if (err == NO_ERROR) { |
| 640 | len *= sizeof(char16_t); |
| 641 | uint8_t* data = (uint8_t*)writeInplace(len+sizeof(char16_t)); |
| 642 | if (data) { |
| 643 | memcpy(data, str, len); |
| 644 | *reinterpret_cast<char16_t*>(data+len) = 0; |
| 645 | return NO_ERROR; |
| 646 | } |
| 647 | err = mError; |
| 648 | } |
| 649 | return err; |
| 650 | } |
| 651 | |
| 652 | status_t Parcel::writeStrongBinder(const sp<IBinder>& val) |
| 653 | { |
| 654 | return flatten_binder(ProcessState::self(), val, this); |
| 655 | } |
| 656 | |
| 657 | status_t Parcel::writeWeakBinder(const wp<IBinder>& val) |
| 658 | { |
| 659 | return flatten_binder(ProcessState::self(), val, this); |
| 660 | } |
| 661 | |
Mathias Agopian | dfece80 | 2009-05-21 16:29:38 -0700 | [diff] [blame] | 662 | status_t Parcel::writeNativeHandle(const native_handle* handle) |
The Android Open Source Project | 22f7dfd | 2009-01-20 14:03:58 -0800 | [diff] [blame] | 663 | { |
Mathias Agopian | dfece80 | 2009-05-21 16:29:38 -0700 | [diff] [blame] | 664 | if (handle->version != sizeof(native_handle)) |
The Android Open Source Project | 22f7dfd | 2009-01-20 14:03:58 -0800 | [diff] [blame] | 665 | return BAD_TYPE; |
| 666 | |
| 667 | status_t err; |
Mathias Agopian | dfece80 | 2009-05-21 16:29:38 -0700 | [diff] [blame] | 668 | err = writeInt32(handle->numFds); |
The Android Open Source Project | 22f7dfd | 2009-01-20 14:03:58 -0800 | [diff] [blame] | 669 | if (err != NO_ERROR) return err; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 670 | |
Mathias Agopian | dfece80 | 2009-05-21 16:29:38 -0700 | [diff] [blame] | 671 | err = writeInt32(handle->numInts); |
The Android Open Source Project | 22f7dfd | 2009-01-20 14:03:58 -0800 | [diff] [blame] | 672 | if (err != NO_ERROR) return err; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 673 | |
Mathias Agopian | dfece80 | 2009-05-21 16:29:38 -0700 | [diff] [blame] | 674 | for (int i=0 ; err==NO_ERROR && i<handle->numFds ; i++) |
| 675 | err = writeDupFileDescriptor(handle->data[i]); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 676 | |
| 677 | if (err != NO_ERROR) { |
| 678 | LOGD("write native handle, write dup fd failed"); |
| 679 | return err; |
| 680 | } |
Mathias Agopian | dfece80 | 2009-05-21 16:29:38 -0700 | [diff] [blame] | 681 | err = write(handle->data + handle->numFds, sizeof(int)*handle->numInts); |
The Android Open Source Project | 22f7dfd | 2009-01-20 14:03:58 -0800 | [diff] [blame] | 682 | return err; |
| 683 | } |
| 684 | |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 685 | status_t Parcel::writeFileDescriptor(int fd) |
| 686 | { |
| 687 | flat_binder_object obj; |
| 688 | obj.type = BINDER_TYPE_FD; |
| 689 | obj.flags = 0x7f | FLAT_BINDER_FLAG_ACCEPTS_FDS; |
| 690 | obj.handle = fd; |
| 691 | obj.cookie = (void*)0; |
| 692 | return writeObject(obj, true); |
| 693 | } |
| 694 | |
| 695 | status_t Parcel::writeDupFileDescriptor(int fd) |
| 696 | { |
| 697 | flat_binder_object obj; |
| 698 | obj.type = BINDER_TYPE_FD; |
| 699 | obj.flags = 0x7f | FLAT_BINDER_FLAG_ACCEPTS_FDS; |
| 700 | obj.handle = dup(fd); |
| 701 | obj.cookie = (void*)1; |
| 702 | return writeObject(obj, true); |
| 703 | } |
| 704 | |
| 705 | status_t Parcel::writeObject(const flat_binder_object& val, bool nullMetaData) |
| 706 | { |
| 707 | const bool enoughData = (mDataPos+sizeof(val)) <= mDataCapacity; |
| 708 | const bool enoughObjects = mObjectsSize < mObjectsCapacity; |
| 709 | if (enoughData && enoughObjects) { |
| 710 | restart_write: |
| 711 | *reinterpret_cast<flat_binder_object*>(mData+mDataPos) = val; |
| 712 | |
| 713 | // Need to write meta-data? |
| 714 | if (nullMetaData || val.binder != NULL) { |
| 715 | mObjects[mObjectsSize] = mDataPos; |
| 716 | acquire_object(ProcessState::self(), val, this); |
| 717 | mObjectsSize++; |
| 718 | } |
| 719 | |
| 720 | // remember if it's a file descriptor |
| 721 | if (val.type == BINDER_TYPE_FD) { |
| 722 | mHasFds = mFdsKnown = true; |
| 723 | } |
| 724 | |
| 725 | return finishWrite(sizeof(flat_binder_object)); |
| 726 | } |
| 727 | |
| 728 | if (!enoughData) { |
| 729 | const status_t err = growData(sizeof(val)); |
| 730 | if (err != NO_ERROR) return err; |
| 731 | } |
| 732 | if (!enoughObjects) { |
| 733 | size_t newSize = ((mObjectsSize+2)*3)/2; |
| 734 | size_t* objects = (size_t*)realloc(mObjects, newSize*sizeof(size_t)); |
| 735 | if (objects == NULL) return NO_MEMORY; |
| 736 | mObjects = objects; |
| 737 | mObjectsCapacity = newSize; |
| 738 | } |
| 739 | |
| 740 | goto restart_write; |
| 741 | } |
| 742 | |
| 743 | |
| 744 | void Parcel::remove(size_t start, size_t amt) |
| 745 | { |
| 746 | LOG_ALWAYS_FATAL("Parcel::remove() not yet implemented!"); |
| 747 | } |
| 748 | |
| 749 | status_t Parcel::read(void* outData, size_t len) const |
| 750 | { |
| 751 | if ((mDataPos+PAD_SIZE(len)) >= mDataPos && (mDataPos+PAD_SIZE(len)) <= mDataSize) { |
| 752 | memcpy(outData, mData+mDataPos, len); |
| 753 | mDataPos += PAD_SIZE(len); |
| 754 | LOGV("read Setting data pos of %p to %d\n", this, mDataPos); |
| 755 | return NO_ERROR; |
| 756 | } |
| 757 | return NOT_ENOUGH_DATA; |
| 758 | } |
| 759 | |
| 760 | const void* Parcel::readInplace(size_t len) const |
| 761 | { |
| 762 | if ((mDataPos+PAD_SIZE(len)) >= mDataPos && (mDataPos+PAD_SIZE(len)) <= mDataSize) { |
| 763 | const void* data = mData+mDataPos; |
| 764 | mDataPos += PAD_SIZE(len); |
| 765 | LOGV("readInplace Setting data pos of %p to %d\n", this, mDataPos); |
| 766 | return data; |
| 767 | } |
| 768 | return NULL; |
| 769 | } |
| 770 | |
| 771 | status_t Parcel::readInt32(int32_t *pArg) const |
| 772 | { |
| 773 | if ((mDataPos+sizeof(int32_t)) <= mDataSize) { |
| 774 | const void* data = mData+mDataPos; |
| 775 | mDataPos += sizeof(int32_t); |
| 776 | *pArg = *reinterpret_cast<const int32_t*>(data); |
| 777 | return NO_ERROR; |
| 778 | } else { |
| 779 | return NOT_ENOUGH_DATA; |
| 780 | } |
| 781 | } |
| 782 | |
| 783 | int32_t Parcel::readInt32() const |
| 784 | { |
| 785 | if ((mDataPos+sizeof(int32_t)) <= mDataSize) { |
| 786 | const void* data = mData+mDataPos; |
| 787 | mDataPos += sizeof(int32_t); |
| 788 | LOGV("readInt32 Setting data pos of %p to %d\n", this, mDataPos); |
| 789 | return *reinterpret_cast<const int32_t*>(data); |
| 790 | } |
| 791 | return 0; |
| 792 | } |
| 793 | |
| 794 | |
| 795 | status_t Parcel::readInt64(int64_t *pArg) const |
| 796 | { |
| 797 | if ((mDataPos+sizeof(int64_t)) <= mDataSize) { |
| 798 | const void* data = mData+mDataPos; |
| 799 | mDataPos += sizeof(int64_t); |
| 800 | *pArg = *reinterpret_cast<const int64_t*>(data); |
| 801 | LOGV("readInt64 Setting data pos of %p to %d\n", this, mDataPos); |
| 802 | return NO_ERROR; |
| 803 | } else { |
| 804 | return NOT_ENOUGH_DATA; |
| 805 | } |
| 806 | } |
| 807 | |
| 808 | |
| 809 | int64_t Parcel::readInt64() const |
| 810 | { |
| 811 | if ((mDataPos+sizeof(int64_t)) <= mDataSize) { |
| 812 | const void* data = mData+mDataPos; |
| 813 | mDataPos += sizeof(int64_t); |
| 814 | LOGV("readInt64 Setting data pos of %p to %d\n", this, mDataPos); |
| 815 | return *reinterpret_cast<const int64_t*>(data); |
| 816 | } |
| 817 | return 0; |
| 818 | } |
| 819 | |
| 820 | status_t Parcel::readFloat(float *pArg) const |
| 821 | { |
| 822 | if ((mDataPos+sizeof(float)) <= mDataSize) { |
| 823 | const void* data = mData+mDataPos; |
| 824 | mDataPos += sizeof(float); |
| 825 | LOGV("readFloat Setting data pos of %p to %d\n", this, mDataPos); |
| 826 | *pArg = *reinterpret_cast<const float*>(data); |
| 827 | return NO_ERROR; |
| 828 | } else { |
| 829 | return NOT_ENOUGH_DATA; |
| 830 | } |
| 831 | } |
| 832 | |
| 833 | |
| 834 | float Parcel::readFloat() const |
| 835 | { |
| 836 | if ((mDataPos+sizeof(float)) <= mDataSize) { |
| 837 | const void* data = mData+mDataPos; |
| 838 | mDataPos += sizeof(float); |
| 839 | LOGV("readFloat Setting data pos of %p to %d\n", this, mDataPos); |
| 840 | return *reinterpret_cast<const float*>(data); |
| 841 | } |
| 842 | return 0; |
| 843 | } |
| 844 | |
| 845 | status_t Parcel::readDouble(double *pArg) const |
| 846 | { |
| 847 | if ((mDataPos+sizeof(double)) <= mDataSize) { |
| 848 | const void* data = mData+mDataPos; |
| 849 | mDataPos += sizeof(double); |
| 850 | LOGV("readDouble Setting data pos of %p to %d\n", this, mDataPos); |
| 851 | *pArg = *reinterpret_cast<const double*>(data); |
| 852 | return NO_ERROR; |
| 853 | } else { |
| 854 | return NOT_ENOUGH_DATA; |
| 855 | } |
| 856 | } |
| 857 | |
| 858 | |
| 859 | double Parcel::readDouble() const |
| 860 | { |
| 861 | if ((mDataPos+sizeof(double)) <= mDataSize) { |
| 862 | const void* data = mData+mDataPos; |
| 863 | mDataPos += sizeof(double); |
| 864 | LOGV("readDouble Setting data pos of %p to %d\n", this, mDataPos); |
| 865 | return *reinterpret_cast<const double*>(data); |
| 866 | } |
| 867 | return 0; |
| 868 | } |
| 869 | |
| 870 | |
| 871 | const char* Parcel::readCString() const |
| 872 | { |
| 873 | const size_t avail = mDataSize-mDataPos; |
| 874 | if (avail > 0) { |
| 875 | const char* str = reinterpret_cast<const char*>(mData+mDataPos); |
| 876 | // is the string's trailing NUL within the parcel's valid bounds? |
| 877 | const char* eos = reinterpret_cast<const char*>(memchr(str, 0, avail)); |
| 878 | if (eos) { |
| 879 | const size_t len = eos - str; |
| 880 | mDataPos += PAD_SIZE(len+1); |
| 881 | LOGV("readCString Setting data pos of %p to %d\n", this, mDataPos); |
| 882 | return str; |
| 883 | } |
| 884 | } |
| 885 | return NULL; |
| 886 | } |
| 887 | |
| 888 | String8 Parcel::readString8() const |
| 889 | { |
| 890 | int32_t size = readInt32(); |
| 891 | // watch for potential int overflow adding 1 for trailing NUL |
| 892 | if (size > 0 && size < INT32_MAX) { |
| 893 | const char* str = (const char*)readInplace(size+1); |
| 894 | if (str) return String8(str, size); |
| 895 | } |
| 896 | return String8(); |
| 897 | } |
| 898 | |
| 899 | String16 Parcel::readString16() const |
| 900 | { |
| 901 | size_t len; |
| 902 | const char16_t* str = readString16Inplace(&len); |
| 903 | if (str) return String16(str, len); |
| 904 | LOGE("Reading a NULL string not supported here."); |
| 905 | return String16(); |
| 906 | } |
| 907 | |
| 908 | const char16_t* Parcel::readString16Inplace(size_t* outLen) const |
| 909 | { |
| 910 | int32_t size = readInt32(); |
| 911 | // watch for potential int overflow from size+1 |
| 912 | if (size >= 0 && size < INT32_MAX) { |
| 913 | *outLen = size; |
| 914 | const char16_t* str = (const char16_t*)readInplace((size+1)*sizeof(char16_t)); |
| 915 | if (str != NULL) { |
| 916 | return str; |
| 917 | } |
| 918 | } |
| 919 | *outLen = 0; |
| 920 | return NULL; |
| 921 | } |
| 922 | |
| 923 | sp<IBinder> Parcel::readStrongBinder() const |
| 924 | { |
| 925 | sp<IBinder> val; |
| 926 | unflatten_binder(ProcessState::self(), *this, &val); |
| 927 | return val; |
| 928 | } |
| 929 | |
| 930 | wp<IBinder> Parcel::readWeakBinder() const |
| 931 | { |
| 932 | wp<IBinder> val; |
| 933 | unflatten_binder(ProcessState::self(), *this, &val); |
| 934 | return val; |
| 935 | } |
| 936 | |
The Android Open Source Project | 22f7dfd | 2009-01-20 14:03:58 -0800 | [diff] [blame] | 937 | |
Mathias Agopian | dfece80 | 2009-05-21 16:29:38 -0700 | [diff] [blame] | 938 | native_handle* Parcel::readNativeHandle() const |
The Android Open Source Project | 22f7dfd | 2009-01-20 14:03:58 -0800 | [diff] [blame] | 939 | { |
| 940 | int numFds, numInts; |
| 941 | status_t err; |
| 942 | err = readInt32(&numFds); |
| 943 | if (err != NO_ERROR) return 0; |
| 944 | err = readInt32(&numInts); |
| 945 | if (err != NO_ERROR) return 0; |
| 946 | |
Mathias Agopian | dfece80 | 2009-05-21 16:29:38 -0700 | [diff] [blame] | 947 | native_handle* h = native_handle_create(numFds, numInts); |
The Android Open Source Project | 22f7dfd | 2009-01-20 14:03:58 -0800 | [diff] [blame] | 948 | for (int i=0 ; err==NO_ERROR && i<numFds ; i++) { |
Rebecca Schultz Zavin | bc4afde | 2009-02-13 16:34:38 -0800 | [diff] [blame] | 949 | h->data[i] = dup(readFileDescriptor()); |
The Android Open Source Project | 22f7dfd | 2009-01-20 14:03:58 -0800 | [diff] [blame] | 950 | if (h->data[i] < 0) err = BAD_VALUE; |
| 951 | } |
The Android Open Source Project | 22f7dfd | 2009-01-20 14:03:58 -0800 | [diff] [blame] | 952 | err = read(h->data + numFds, sizeof(int)*numInts); |
The Android Open Source Project | 22f7dfd | 2009-01-20 14:03:58 -0800 | [diff] [blame] | 953 | if (err != NO_ERROR) { |
Mathias Agopian | dfece80 | 2009-05-21 16:29:38 -0700 | [diff] [blame] | 954 | native_handle_close(h); |
| 955 | native_handle_delete(h); |
The Android Open Source Project | 22f7dfd | 2009-01-20 14:03:58 -0800 | [diff] [blame] | 956 | h = 0; |
| 957 | } |
| 958 | return h; |
| 959 | } |
| 960 | |
| 961 | |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 962 | int Parcel::readFileDescriptor() const |
| 963 | { |
| 964 | const flat_binder_object* flat = readObject(true); |
| 965 | if (flat) { |
| 966 | switch (flat->type) { |
| 967 | case BINDER_TYPE_FD: |
| 968 | //LOGI("Returning file descriptor %ld from parcel %p\n", flat->handle, this); |
| 969 | return flat->handle; |
| 970 | } |
| 971 | } |
| 972 | return BAD_TYPE; |
| 973 | } |
| 974 | |
| 975 | const flat_binder_object* Parcel::readObject(bool nullMetaData) const |
| 976 | { |
| 977 | const size_t DPOS = mDataPos; |
| 978 | if ((DPOS+sizeof(flat_binder_object)) <= mDataSize) { |
| 979 | const flat_binder_object* obj |
| 980 | = reinterpret_cast<const flat_binder_object*>(mData+DPOS); |
| 981 | mDataPos = DPOS + sizeof(flat_binder_object); |
| 982 | if (!nullMetaData && (obj->cookie == NULL && obj->binder == NULL)) { |
The Android Open Source Project | 22f7dfd | 2009-01-20 14:03:58 -0800 | [diff] [blame] | 983 | // When transferring a NULL object, we don't write it into |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 984 | // the object list, so we don't want to check for it when |
| 985 | // reading. |
| 986 | LOGV("readObject Setting data pos of %p to %d\n", this, mDataPos); |
| 987 | return obj; |
| 988 | } |
| 989 | |
| 990 | // Ensure that this object is valid... |
| 991 | size_t* const OBJS = mObjects; |
| 992 | const size_t N = mObjectsSize; |
| 993 | size_t opos = mNextObjectHint; |
| 994 | |
| 995 | if (N > 0) { |
| 996 | LOGV("Parcel %p looking for obj at %d, hint=%d\n", |
| 997 | this, DPOS, opos); |
| 998 | |
| 999 | // Start at the current hint position, looking for an object at |
| 1000 | // the current data position. |
| 1001 | if (opos < N) { |
| 1002 | while (opos < (N-1) && OBJS[opos] < DPOS) { |
| 1003 | opos++; |
| 1004 | } |
| 1005 | } else { |
| 1006 | opos = N-1; |
| 1007 | } |
| 1008 | if (OBJS[opos] == DPOS) { |
| 1009 | // Found it! |
| 1010 | LOGV("Parcel found obj %d at index %d with forward search", |
| 1011 | this, DPOS, opos); |
| 1012 | mNextObjectHint = opos+1; |
| 1013 | LOGV("readObject Setting data pos of %p to %d\n", this, mDataPos); |
| 1014 | return obj; |
| 1015 | } |
| 1016 | |
| 1017 | // Look backwards for it... |
| 1018 | while (opos > 0 && OBJS[opos] > DPOS) { |
| 1019 | opos--; |
| 1020 | } |
| 1021 | if (OBJS[opos] == DPOS) { |
| 1022 | // Found it! |
| 1023 | LOGV("Parcel found obj %d at index %d with backward search", |
| 1024 | this, DPOS, opos); |
| 1025 | mNextObjectHint = opos+1; |
| 1026 | LOGV("readObject Setting data pos of %p to %d\n", this, mDataPos); |
| 1027 | return obj; |
| 1028 | } |
| 1029 | } |
| 1030 | LOGW("Attempt to read object from Parcel %p at offset %d that is not in the object list", |
| 1031 | this, DPOS); |
| 1032 | } |
| 1033 | return NULL; |
| 1034 | } |
| 1035 | |
| 1036 | void Parcel::closeFileDescriptors() |
| 1037 | { |
| 1038 | size_t i = mObjectsSize; |
| 1039 | if (i > 0) { |
| 1040 | //LOGI("Closing file descriptors for %d objects...", mObjectsSize); |
| 1041 | } |
| 1042 | while (i > 0) { |
| 1043 | i--; |
| 1044 | const flat_binder_object* flat |
| 1045 | = reinterpret_cast<flat_binder_object*>(mData+mObjects[i]); |
| 1046 | if (flat->type == BINDER_TYPE_FD) { |
| 1047 | //LOGI("Closing fd: %ld\n", flat->handle); |
| 1048 | close(flat->handle); |
| 1049 | } |
| 1050 | } |
| 1051 | } |
| 1052 | |
| 1053 | const uint8_t* Parcel::ipcData() const |
| 1054 | { |
| 1055 | return mData; |
| 1056 | } |
| 1057 | |
| 1058 | size_t Parcel::ipcDataSize() const |
| 1059 | { |
| 1060 | return (mDataSize > mDataPos ? mDataSize : mDataPos); |
| 1061 | } |
| 1062 | |
| 1063 | const size_t* Parcel::ipcObjects() const |
| 1064 | { |
| 1065 | return mObjects; |
| 1066 | } |
| 1067 | |
| 1068 | size_t Parcel::ipcObjectsCount() const |
| 1069 | { |
| 1070 | return mObjectsSize; |
| 1071 | } |
| 1072 | |
| 1073 | void Parcel::ipcSetDataReference(const uint8_t* data, size_t dataSize, |
| 1074 | const size_t* objects, size_t objectsCount, release_func relFunc, void* relCookie) |
| 1075 | { |
| 1076 | freeDataNoInit(); |
| 1077 | mError = NO_ERROR; |
| 1078 | mData = const_cast<uint8_t*>(data); |
| 1079 | mDataSize = mDataCapacity = dataSize; |
| 1080 | //LOGI("setDataReference Setting data size of %p to %lu (pid=%d)\n", this, mDataSize, getpid()); |
| 1081 | mDataPos = 0; |
| 1082 | LOGV("setDataReference Setting data pos of %p to %d\n", this, mDataPos); |
| 1083 | mObjects = const_cast<size_t*>(objects); |
| 1084 | mObjectsSize = mObjectsCapacity = objectsCount; |
| 1085 | mNextObjectHint = 0; |
| 1086 | mOwner = relFunc; |
| 1087 | mOwnerCookie = relCookie; |
| 1088 | scanForFds(); |
| 1089 | } |
| 1090 | |
| 1091 | void Parcel::print(TextOutput& to, uint32_t flags) const |
| 1092 | { |
| 1093 | to << "Parcel("; |
| 1094 | |
| 1095 | if (errorCheck() != NO_ERROR) { |
| 1096 | const status_t err = errorCheck(); |
| 1097 | to << "Error: " << (void*)err << " \"" << strerror(-err) << "\""; |
| 1098 | } else if (dataSize() > 0) { |
| 1099 | const uint8_t* DATA = data(); |
| 1100 | to << indent << HexDump(DATA, dataSize()) << dedent; |
| 1101 | const size_t* OBJS = objects(); |
| 1102 | const size_t N = objectsCount(); |
| 1103 | for (size_t i=0; i<N; i++) { |
| 1104 | const flat_binder_object* flat |
| 1105 | = reinterpret_cast<const flat_binder_object*>(DATA+OBJS[i]); |
| 1106 | to << endl << "Object #" << i << " @ " << (void*)OBJS[i] << ": " |
| 1107 | << TypeCode(flat->type & 0x7f7f7f00) |
| 1108 | << " = " << flat->binder; |
| 1109 | } |
| 1110 | } else { |
| 1111 | to << "NULL"; |
| 1112 | } |
| 1113 | |
| 1114 | to << ")"; |
| 1115 | } |
| 1116 | |
| 1117 | void Parcel::releaseObjects() |
| 1118 | { |
| 1119 | const sp<ProcessState> proc(ProcessState::self()); |
| 1120 | size_t i = mObjectsSize; |
| 1121 | uint8_t* const data = mData; |
| 1122 | size_t* const objects = mObjects; |
| 1123 | while (i > 0) { |
| 1124 | i--; |
| 1125 | const flat_binder_object* flat |
| 1126 | = reinterpret_cast<flat_binder_object*>(data+objects[i]); |
| 1127 | release_object(proc, *flat, this); |
| 1128 | } |
| 1129 | } |
| 1130 | |
| 1131 | void Parcel::acquireObjects() |
| 1132 | { |
| 1133 | const sp<ProcessState> proc(ProcessState::self()); |
| 1134 | size_t i = mObjectsSize; |
| 1135 | uint8_t* const data = mData; |
| 1136 | size_t* const objects = mObjects; |
| 1137 | while (i > 0) { |
| 1138 | i--; |
| 1139 | const flat_binder_object* flat |
| 1140 | = reinterpret_cast<flat_binder_object*>(data+objects[i]); |
| 1141 | acquire_object(proc, *flat, this); |
| 1142 | } |
| 1143 | } |
| 1144 | |
| 1145 | void Parcel::freeData() |
| 1146 | { |
| 1147 | freeDataNoInit(); |
| 1148 | initState(); |
| 1149 | } |
| 1150 | |
| 1151 | void Parcel::freeDataNoInit() |
| 1152 | { |
| 1153 | if (mOwner) { |
| 1154 | //LOGI("Freeing data ref of %p (pid=%d)\n", this, getpid()); |
| 1155 | mOwner(this, mData, mDataSize, mObjects, mObjectsSize, mOwnerCookie); |
| 1156 | } else { |
| 1157 | releaseObjects(); |
| 1158 | if (mData) free(mData); |
| 1159 | if (mObjects) free(mObjects); |
| 1160 | } |
| 1161 | } |
| 1162 | |
| 1163 | status_t Parcel::growData(size_t len) |
| 1164 | { |
| 1165 | size_t newSize = ((mDataSize+len)*3)/2; |
| 1166 | return (newSize <= mDataSize) |
| 1167 | ? (status_t) NO_MEMORY |
| 1168 | : continueWrite(newSize); |
| 1169 | } |
| 1170 | |
| 1171 | status_t Parcel::restartWrite(size_t desired) |
| 1172 | { |
| 1173 | if (mOwner) { |
| 1174 | freeData(); |
| 1175 | return continueWrite(desired); |
| 1176 | } |
| 1177 | |
| 1178 | uint8_t* data = (uint8_t*)realloc(mData, desired); |
| 1179 | if (!data && desired > mDataCapacity) { |
| 1180 | mError = NO_MEMORY; |
| 1181 | return NO_MEMORY; |
| 1182 | } |
| 1183 | |
| 1184 | releaseObjects(); |
| 1185 | |
| 1186 | if (data) { |
| 1187 | mData = data; |
| 1188 | mDataCapacity = desired; |
| 1189 | } |
| 1190 | |
| 1191 | mDataSize = mDataPos = 0; |
| 1192 | LOGV("restartWrite Setting data size of %p to %d\n", this, mDataSize); |
| 1193 | LOGV("restartWrite Setting data pos of %p to %d\n", this, mDataPos); |
| 1194 | |
| 1195 | free(mObjects); |
| 1196 | mObjects = NULL; |
| 1197 | mObjectsSize = mObjectsCapacity = 0; |
| 1198 | mNextObjectHint = 0; |
| 1199 | mHasFds = false; |
| 1200 | mFdsKnown = true; |
| 1201 | |
| 1202 | return NO_ERROR; |
| 1203 | } |
| 1204 | |
| 1205 | status_t Parcel::continueWrite(size_t desired) |
| 1206 | { |
| 1207 | // If shrinking, first adjust for any objects that appear |
| 1208 | // after the new data size. |
| 1209 | size_t objectsSize = mObjectsSize; |
| 1210 | if (desired < mDataSize) { |
| 1211 | if (desired == 0) { |
| 1212 | objectsSize = 0; |
| 1213 | } else { |
| 1214 | while (objectsSize > 0) { |
| 1215 | if (mObjects[objectsSize-1] < desired) |
| 1216 | break; |
| 1217 | objectsSize--; |
| 1218 | } |
| 1219 | } |
| 1220 | } |
| 1221 | |
| 1222 | if (mOwner) { |
| 1223 | // If the size is going to zero, just release the owner's data. |
| 1224 | if (desired == 0) { |
| 1225 | freeData(); |
| 1226 | return NO_ERROR; |
| 1227 | } |
| 1228 | |
| 1229 | // If there is a different owner, we need to take |
| 1230 | // posession. |
| 1231 | uint8_t* data = (uint8_t*)malloc(desired); |
| 1232 | if (!data) { |
| 1233 | mError = NO_MEMORY; |
| 1234 | return NO_MEMORY; |
| 1235 | } |
| 1236 | size_t* objects = NULL; |
| 1237 | |
| 1238 | if (objectsSize) { |
| 1239 | objects = (size_t*)malloc(objectsSize*sizeof(size_t)); |
| 1240 | if (!objects) { |
| 1241 | mError = NO_MEMORY; |
| 1242 | return NO_MEMORY; |
| 1243 | } |
| 1244 | |
| 1245 | // Little hack to only acquire references on objects |
| 1246 | // we will be keeping. |
| 1247 | size_t oldObjectsSize = mObjectsSize; |
| 1248 | mObjectsSize = objectsSize; |
| 1249 | acquireObjects(); |
| 1250 | mObjectsSize = oldObjectsSize; |
| 1251 | } |
| 1252 | |
| 1253 | if (mData) { |
| 1254 | memcpy(data, mData, mDataSize < desired ? mDataSize : desired); |
| 1255 | } |
| 1256 | if (objects && mObjects) { |
| 1257 | memcpy(objects, mObjects, objectsSize*sizeof(size_t)); |
| 1258 | } |
| 1259 | //LOGI("Freeing data ref of %p (pid=%d)\n", this, getpid()); |
| 1260 | mOwner(this, mData, mDataSize, mObjects, mObjectsSize, mOwnerCookie); |
| 1261 | mOwner = NULL; |
| 1262 | |
| 1263 | mData = data; |
| 1264 | mObjects = objects; |
| 1265 | mDataSize = (mDataSize < desired) ? mDataSize : desired; |
| 1266 | LOGV("continueWrite Setting data size of %p to %d\n", this, mDataSize); |
| 1267 | mDataCapacity = desired; |
| 1268 | mObjectsSize = mObjectsCapacity = objectsSize; |
| 1269 | mNextObjectHint = 0; |
| 1270 | |
| 1271 | } else if (mData) { |
| 1272 | if (objectsSize < mObjectsSize) { |
| 1273 | // Need to release refs on any objects we are dropping. |
| 1274 | const sp<ProcessState> proc(ProcessState::self()); |
| 1275 | for (size_t i=objectsSize; i<mObjectsSize; i++) { |
| 1276 | const flat_binder_object* flat |
| 1277 | = reinterpret_cast<flat_binder_object*>(mData+mObjects[i]); |
| 1278 | if (flat->type == BINDER_TYPE_FD) { |
| 1279 | // will need to rescan because we may have lopped off the only FDs |
| 1280 | mFdsKnown = false; |
| 1281 | } |
| 1282 | release_object(proc, *flat, this); |
| 1283 | } |
| 1284 | size_t* objects = |
| 1285 | (size_t*)realloc(mObjects, objectsSize*sizeof(size_t)); |
| 1286 | if (objects) { |
| 1287 | mObjects = objects; |
| 1288 | } |
| 1289 | mObjectsSize = objectsSize; |
| 1290 | mNextObjectHint = 0; |
| 1291 | } |
| 1292 | |
| 1293 | // We own the data, so we can just do a realloc(). |
| 1294 | if (desired > mDataCapacity) { |
| 1295 | uint8_t* data = (uint8_t*)realloc(mData, desired); |
| 1296 | if (data) { |
| 1297 | mData = data; |
| 1298 | mDataCapacity = desired; |
| 1299 | } else if (desired > mDataCapacity) { |
| 1300 | mError = NO_MEMORY; |
| 1301 | return NO_MEMORY; |
| 1302 | } |
| 1303 | } else { |
| 1304 | mDataSize = desired; |
| 1305 | LOGV("continueWrite Setting data size of %p to %d\n", this, mDataSize); |
| 1306 | if (mDataPos > desired) { |
| 1307 | mDataPos = desired; |
| 1308 | LOGV("continueWrite Setting data pos of %p to %d\n", this, mDataPos); |
| 1309 | } |
| 1310 | } |
| 1311 | |
| 1312 | } else { |
| 1313 | // This is the first data. Easy! |
| 1314 | uint8_t* data = (uint8_t*)malloc(desired); |
| 1315 | if (!data) { |
| 1316 | mError = NO_MEMORY; |
| 1317 | return NO_MEMORY; |
| 1318 | } |
| 1319 | |
| 1320 | if(!(mDataCapacity == 0 && mObjects == NULL |
| 1321 | && mObjectsCapacity == 0)) { |
| 1322 | LOGE("continueWrite: %d/%p/%d/%d", mDataCapacity, mObjects, mObjectsCapacity, desired); |
| 1323 | } |
| 1324 | |
| 1325 | mData = data; |
| 1326 | mDataSize = mDataPos = 0; |
| 1327 | LOGV("continueWrite Setting data size of %p to %d\n", this, mDataSize); |
| 1328 | LOGV("continueWrite Setting data pos of %p to %d\n", this, mDataPos); |
| 1329 | mDataCapacity = desired; |
| 1330 | } |
| 1331 | |
| 1332 | return NO_ERROR; |
| 1333 | } |
| 1334 | |
| 1335 | void Parcel::initState() |
| 1336 | { |
| 1337 | mError = NO_ERROR; |
| 1338 | mData = 0; |
| 1339 | mDataSize = 0; |
| 1340 | mDataCapacity = 0; |
| 1341 | mDataPos = 0; |
| 1342 | LOGV("initState Setting data size of %p to %d\n", this, mDataSize); |
| 1343 | LOGV("initState Setting data pos of %p to %d\n", this, mDataPos); |
| 1344 | mObjects = NULL; |
| 1345 | mObjectsSize = 0; |
| 1346 | mObjectsCapacity = 0; |
| 1347 | mNextObjectHint = 0; |
| 1348 | mHasFds = false; |
| 1349 | mFdsKnown = true; |
| 1350 | mOwner = NULL; |
| 1351 | } |
| 1352 | |
| 1353 | void Parcel::scanForFds() const |
| 1354 | { |
| 1355 | bool hasFds = false; |
| 1356 | for (size_t i=0; i<mObjectsSize; i++) { |
| 1357 | const flat_binder_object* flat |
| 1358 | = reinterpret_cast<const flat_binder_object*>(mData + mObjects[i]); |
| 1359 | if (flat->type == BINDER_TYPE_FD) { |
| 1360 | hasFds = true; |
| 1361 | break; |
| 1362 | } |
| 1363 | } |
| 1364 | mHasFds = hasFds; |
| 1365 | mFdsKnown = true; |
| 1366 | } |
| 1367 | |
| 1368 | }; // namespace android |