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