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