blob: 4cd2fe18648c1c5abf48508cb0624faafe753871 [file] [log] [blame]
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001/*
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 Agopianc5b2c0b2009-05-19 19:08:10 -070020#include <binder/Parcel.h>
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -070021
Brad Fitzpatrick702ea9d2010-06-18 13:07:53 -070022#include <binder/IPCThreadState.h>
Mathias Agopianc5b2c0b2009-05-19 19:08:10 -070023#include <binder/Binder.h>
24#include <binder/BpBinder.h>
Mathias Agopianc5b2c0b2009-05-19 19:08:10 -070025#include <binder/ProcessState.h>
Mathias Agopian002e1e52013-05-06 20:20:50 -070026#include <binder/TextOutput.h>
27
28#include <utils/Debug.h>
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -070029#include <utils/Log.h>
30#include <utils/String8.h>
31#include <utils/String16.h>
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -070032#include <utils/misc.h>
Mathias Agopian98e71dd2010-02-11 17:30:52 -080033#include <utils/Flattenable.h>
Jeff Brown5707dbf2011-09-23 21:17:56 -070034#include <cutils/ashmem.h>
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -070035
Mathias Agopian208059f2009-05-18 15:08:03 -070036#include <private/binder/binder_module.h>
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -070037
38#include <stdio.h>
39#include <stdlib.h>
40#include <stdint.h>
Jeff Brown5707dbf2011-09-23 21:17:56 -070041#include <sys/mman.h>
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -070042
43#ifndef INT32_MAX
44#define INT32_MAX ((int32_t)(2147483647))
45#endif
46
47#define LOG_REFS(...)
Steve Block9f760152011-10-12 17:27:03 +010048//#define LOG_REFS(...) ALOG(LOG_DEBUG, "Parcel", __VA_ARGS__)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -070049
50// ---------------------------------------------------------------------------
51
52#define PAD_SIZE(s) (((s)+3)&~3)
53
Brad Fitzpatricka877cd82010-07-07 16:06:39 -070054// Note: must be kept in sync with android/os/StrictMode.java's PENALTY_GATHER
55#define STRICT_MODE_PENALTY_GATHER 0x100
56
Brad Fitzpatrickd36f4a52010-07-12 11:05:38 -070057// 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 Brown5707dbf2011-09-23 21:17:56 -070060// Maximum size of a blob to transfer in-place.
61static const size_t IN_PLACE_BLOB_LIMIT = 40 * 1024;
62
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -070063// XXX This can be made public if we want to provide
64// support for typed data.
65struct small_flat_data
66{
67 uint32_t type;
68 uint32_t data;
69};
70
71namespace android {
72
73void 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 Block9d453682011-12-20 16:23:08 +0000107 ALOGD("Invalid object type 0x%08lx", obj.type);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700108}
109
110void 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 Blocke6f43dd2012-01-06 19:20:56 +0000143 ALOGE("Invalid object type 0x%08lx", obj.type);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700144}
145
146inline 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
152status_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 Blocke6f43dd2012-01-06 19:20:56 +0000163 ALOGE("null proxy");
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700164 }
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
183status_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 Blocke6f43dd2012-01-06 19:20:56 +0000196 ALOGE("null proxy");
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700197 }
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 Blocke6f43dd2012-01-06 19:20:56 +0000217 ALOGE("Unable to unflatten Binder weak reference!");
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700218 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
231inline static status_t finish_unflatten_binder(
232 BpBinder* proxy, const flat_binder_object& flat, const Parcel& in)
233{
234 return NO_ERROR;
235}
236
237status_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
256status_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
287Parcel::Parcel()
288{
289 initState();
290}
291
292Parcel::~Parcel()
293{
294 freeDataNoInit();
295}
296
297const uint8_t* Parcel::data() const
298{
299 return mData;
300}
301
302size_t Parcel::dataSize() const
303{
304 return (mDataSize > mDataPos ? mDataSize : mDataPos);
305}
306
307size_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
318size_t Parcel::dataPosition() const
319{
320 return mDataPos;
321}
322
323size_t Parcel::dataCapacity() const
324{
325 return mDataCapacity;
326}
327
328status_t Parcel::setDataSize(size_t size)
329{
330 status_t err;
331 err = continueWrite(size);
332 if (err == NO_ERROR) {
333 mDataSize = size;
Steve Block6807e592011-10-20 11:56:00 +0100334 ALOGV("setDataSize Setting data size of %p to %d\n", this, mDataSize);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700335 }
336 return err;
337}
338
339void Parcel::setDataPosition(size_t pos) const
340{
341 mDataPos = pos;
342 mNextObjectHint = 0;
343}
344
345status_t Parcel::setDataCapacity(size_t size)
346{
Dianne Hackborn97e2bcd2011-04-13 18:15:56 -0700347 if (size > mDataCapacity) return continueWrite(size);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700348 return NO_ERROR;
349}
350
351status_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 Huber51faf462011-04-13 10:21:56 -0700362status_t Parcel::appendFrom(const Parcel *parcel, size_t offset, size_t len)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700363{
364 const sp<ProcessState> proc(ProcessState::self());
365 status_t err;
Andreas Huber51faf462011-04-13 10:21:56 -0700366 const uint8_t *data = parcel->mData;
367 const size_t *objects = parcel->mObjects;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700368 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 Hackborn97e2bcd2011-04-13 18:15:56 -0700395 if ((mDataSize+len) > mDataCapacity) {
396 // grow data
397 err = growData(len);
398 if (err != NO_ERROR) {
399 return err;
400 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700401 }
402
403 // append data
404 memcpy(mData + mDataPos, data + offset, len);
405 mDataPos += len;
406 mDataSize += len;
407
Dianne Hackborn8938ed22011-09-28 23:19:47 -0400408 err = NO_ERROR;
409
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700410 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 Hackborn8af0f822009-05-22 13:20:23 -0700430 flat_binder_object* flat
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700431 = reinterpret_cast<flat_binder_object*>(mData + off);
432 acquire_object(proc, *flat, this);
433
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700434 if (flat->type == BINDER_TYPE_FD) {
Dianne Hackborn8af0f822009-05-22 13:20:23 -0700435 // 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 Project7c1b96a2008-10-21 07:00:00 -0700440 mHasFds = mFdsKnown = true;
Dianne Hackborn8938ed22011-09-28 23:19:47 -0400441 if (!mAllowFds) {
442 err = FDS_NOT_ALLOWED;
443 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700444 }
445 }
446 }
447
Dianne Hackborn8938ed22011-09-28 23:19:47 -0400448 return err;
449}
450
Dianne Hackborn7746cc32011-10-03 21:09:35 -0700451bool Parcel::pushAllowFds(bool allowFds)
Dianne Hackborn8938ed22011-09-28 23:19:47 -0400452{
453 const bool origValue = mAllowFds;
Dianne Hackborn7746cc32011-10-03 21:09:35 -0700454 if (!allowFds) {
455 mAllowFds = false;
456 }
Dianne Hackborn8938ed22011-09-28 23:19:47 -0400457 return origValue;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700458}
459
Dianne Hackborn7746cc32011-10-03 21:09:35 -0700460void Parcel::restoreAllowFds(bool lastValue)
461{
462 mAllowFds = lastValue;
463}
464
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700465bool Parcel::hasFileDescriptors() const
466{
467 if (!mFdsKnown) {
468 scanForFds();
469 }
470 return mHasFds;
471}
472
Brad Fitzpatrick702ea9d2010-06-18 13:07:53 -0700473// Write RPC headers. (previously just the interface token)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700474status_t Parcel::writeInterfaceToken(const String16& interface)
475{
Brad Fitzpatricka877cd82010-07-07 16:06:39 -0700476 writeInt32(IPCThreadState::self()->getStrictModePolicy() |
477 STRICT_MODE_PENALTY_GATHER);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700478 // currently the interface identification token is just its name as a string
479 return writeString16(interface);
480}
481
Mathias Agopian83c04462009-05-22 19:00:22 -0700482bool Parcel::checkInterface(IBinder* binder) const
483{
Brad Fitzpatrick702ea9d2010-06-18 13:07:53 -0700484 return enforceInterface(binder->getInterfaceDescriptor());
Mathias Agopian83c04462009-05-22 19:00:22 -0700485}
486
Brad Fitzpatricka877cd82010-07-07 16:06:39 -0700487bool Parcel::enforceInterface(const String16& interface,
Brad Fitzpatrick70081a12010-07-27 09:49:11 -0700488 IPCThreadState* threadState) const
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700489{
Brad Fitzpatrick70081a12010-07-27 09:49:11 -0700490 int32_t strictPolicy = readInt32();
491 if (threadState == NULL) {
492 threadState = IPCThreadState::self();
Brad Fitzpatricka877cd82010-07-07 16:06:39 -0700493 }
Brad Fitzpatrick52736032010-08-30 16:01:16 -0700494 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 Agopian83c04462009-05-22 19:00:22 -0700504 const String16 str(readString16());
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700505 if (str == interface) {
506 return true;
507 } else {
Steve Block32397c12012-01-05 23:22:43 +0000508 ALOGW("**** enforceInterface() expected '%s' but read '%s'\n",
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700509 String8(interface).string(), String8(str).string());
510 return false;
511 }
Brad Fitzpatrick702ea9d2010-06-18 13:07:53 -0700512}
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700513
514const size_t* Parcel::objects() const
515{
516 return mObjects;
517}
518
519size_t Parcel::objectsCount() const
520{
521 return mObjectsSize;
522}
523
524status_t Parcel::errorCheck() const
525{
526 return mError;
527}
528
529void Parcel::setError(status_t err)
530{
531 mError = err;
532}
533
534status_t Parcel::finishWrite(size_t len)
535{
536 //printf("Finish write of %d\n", len);
537 mDataPos += len;
Steve Block6807e592011-10-20 11:56:00 +0100538 ALOGV("finishWrite Setting data pos of %p to %d\n", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700539 if (mDataPos > mDataSize) {
540 mDataSize = mDataPos;
Steve Block6807e592011-10-20 11:56:00 +0100541 ALOGV("finishWrite Setting data size of %p to %d\n", this, mDataSize);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700542 }
543 //printf("New pos=%d, size=%d\n", mDataPos, mDataSize);
544 return NO_ERROR;
545}
546
547status_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) {
556restart_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
566status_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
576void* 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) {
586restart_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
616status_t Parcel::writeInt32(int32_t val)
617{
Andreas Huber84a6d042009-08-17 13:33:27 -0700618 return writeAligned(val);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700619}
Marco Nelissen5c0106e2013-10-16 10:57:51 -0700620status_t Parcel::writeInt32Array(size_t len, const int32_t *val) {
621 if (!val) {
622 return writeAligned(-1);
623 }
624 status_t ret = writeAligned(len);
625 if (ret == NO_ERROR) {
626 ret = write(val, len * sizeof(*val));
627 }
628 return ret;
629}
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700630
631status_t Parcel::writeInt64(int64_t val)
632{
Andreas Huber84a6d042009-08-17 13:33:27 -0700633 return writeAligned(val);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700634}
635
636status_t Parcel::writeFloat(float val)
637{
Andreas Huber84a6d042009-08-17 13:33:27 -0700638 return writeAligned(val);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700639}
640
Douglas Leungcc1a4bb2013-01-11 15:00:55 -0800641#if defined(__mips__) && defined(__mips_hard_float)
642
643status_t Parcel::writeDouble(double val)
644{
645 union {
646 double d;
647 unsigned long long ll;
648 } u;
649 u.d = val;
650 return writeAligned(u.ll);
651}
652
653#else
654
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700655status_t Parcel::writeDouble(double val)
656{
Andreas Huber84a6d042009-08-17 13:33:27 -0700657 return writeAligned(val);
658}
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700659
Douglas Leungcc1a4bb2013-01-11 15:00:55 -0800660#endif
661
Andreas Huber84a6d042009-08-17 13:33:27 -0700662status_t Parcel::writeIntPtr(intptr_t val)
663{
664 return writeAligned(val);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700665}
666
667status_t Parcel::writeCString(const char* str)
668{
669 return write(str, strlen(str)+1);
670}
671
672status_t Parcel::writeString8(const String8& str)
673{
674 status_t err = writeInt32(str.bytes());
Pravat Dalbeherad1dff8d2010-12-15 08:40:00 +0100675 // only write string if its length is more than zero characters,
676 // as readString8 will only read if the length field is non-zero.
677 // this is slightly different from how writeString16 works.
678 if (str.bytes() > 0 && err == NO_ERROR) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700679 err = write(str.string(), str.bytes()+1);
680 }
681 return err;
682}
683
684status_t Parcel::writeString16(const String16& str)
685{
686 return writeString16(str.string(), str.size());
687}
688
689status_t Parcel::writeString16(const char16_t* str, size_t len)
690{
691 if (str == NULL) return writeInt32(-1);
692
693 status_t err = writeInt32(len);
694 if (err == NO_ERROR) {
695 len *= sizeof(char16_t);
696 uint8_t* data = (uint8_t*)writeInplace(len+sizeof(char16_t));
697 if (data) {
698 memcpy(data, str, len);
699 *reinterpret_cast<char16_t*>(data+len) = 0;
700 return NO_ERROR;
701 }
702 err = mError;
703 }
704 return err;
705}
706
707status_t Parcel::writeStrongBinder(const sp<IBinder>& val)
708{
709 return flatten_binder(ProcessState::self(), val, this);
710}
711
712status_t Parcel::writeWeakBinder(const wp<IBinder>& val)
713{
714 return flatten_binder(ProcessState::self(), val, this);
715}
716
Mathias Agopiana47f02a2009-05-21 16:29:38 -0700717status_t Parcel::writeNativeHandle(const native_handle* handle)
The Android Open Source Project5f78a482009-01-20 14:03:58 -0800718{
Mathias Agopian1d0a95b2009-07-31 16:12:13 -0700719 if (!handle || handle->version != sizeof(native_handle))
The Android Open Source Project5f78a482009-01-20 14:03:58 -0800720 return BAD_TYPE;
721
722 status_t err;
Mathias Agopiana47f02a2009-05-21 16:29:38 -0700723 err = writeInt32(handle->numFds);
The Android Open Source Project5f78a482009-01-20 14:03:58 -0800724 if (err != NO_ERROR) return err;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800725
Mathias Agopiana47f02a2009-05-21 16:29:38 -0700726 err = writeInt32(handle->numInts);
The Android Open Source Project5f78a482009-01-20 14:03:58 -0800727 if (err != NO_ERROR) return err;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800728
Mathias Agopiana47f02a2009-05-21 16:29:38 -0700729 for (int i=0 ; err==NO_ERROR && i<handle->numFds ; i++)
730 err = writeDupFileDescriptor(handle->data[i]);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800731
732 if (err != NO_ERROR) {
Steve Block9d453682011-12-20 16:23:08 +0000733 ALOGD("write native handle, write dup fd failed");
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800734 return err;
735 }
Mathias Agopiana47f02a2009-05-21 16:29:38 -0700736 err = write(handle->data + handle->numFds, sizeof(int)*handle->numInts);
The Android Open Source Project5f78a482009-01-20 14:03:58 -0800737 return err;
738}
739
Jeff Brown93ff1f92011-11-04 19:01:44 -0700740status_t Parcel::writeFileDescriptor(int fd, bool takeOwnership)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700741{
742 flat_binder_object obj;
743 obj.type = BINDER_TYPE_FD;
744 obj.flags = 0x7f | FLAT_BINDER_FLAG_ACCEPTS_FDS;
745 obj.handle = fd;
Jeff Brown93ff1f92011-11-04 19:01:44 -0700746 obj.cookie = (void*) (takeOwnership ? 1 : 0);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700747 return writeObject(obj, true);
748}
749
750status_t Parcel::writeDupFileDescriptor(int fd)
751{
Jeff Brownd341c712011-11-04 20:19:33 -0700752 int dupFd = dup(fd);
753 if (dupFd < 0) {
754 return -errno;
755 }
756 status_t err = writeFileDescriptor(dupFd, true /*takeOwnership*/);
757 if (err) {
758 close(dupFd);
759 }
760 return err;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700761}
762
Mike Lockwoodcbe36fe2013-09-05 08:41:06 -0700763// WARNING: This method must stay in sync with
764// Parcelable.Creator<ParcelFileDescriptor> CREATOR
765// in frameworks/base/core/java/android/os/ParcelFileDescriptor.java
766status_t Parcel::writeParcelFileDescriptor(int fd, int commChannel) {
767 status_t status;
768
769 if (fd < 0) {
770 status = writeInt32(0); // ParcelFileDescriptor is null
771 if (status) return status;
772 } else {
773 status = writeInt32(1); // ParcelFileDescriptor is not null
774 if (status) return status;
775 status = writeDupFileDescriptor(fd);
776 if (status) return status;
777 if (commChannel < 0) {
778 status = writeInt32(0); // commChannel is null
779 if (status) return status;
780 } else {
781 status = writeInt32(1); // commChannel is not null
782 if (status) return status;
783 status = writeDupFileDescriptor(commChannel);
784 }
785 }
786 return status;
787}
788
Jeff Brown5707dbf2011-09-23 21:17:56 -0700789status_t Parcel::writeBlob(size_t len, WritableBlob* outBlob)
790{
791 status_t status;
792
793 if (!mAllowFds || len <= IN_PLACE_BLOB_LIMIT) {
Steve Block6807e592011-10-20 11:56:00 +0100794 ALOGV("writeBlob: write in place");
Jeff Brown5707dbf2011-09-23 21:17:56 -0700795 status = writeInt32(0);
796 if (status) return status;
797
798 void* ptr = writeInplace(len);
799 if (!ptr) return NO_MEMORY;
800
801 outBlob->init(false /*mapped*/, ptr, len);
802 return NO_ERROR;
803 }
804
Steve Block6807e592011-10-20 11:56:00 +0100805 ALOGV("writeBlob: write to ashmem");
Jeff Brown5707dbf2011-09-23 21:17:56 -0700806 int fd = ashmem_create_region("Parcel Blob", len);
807 if (fd < 0) return NO_MEMORY;
808
809 int result = ashmem_set_prot_region(fd, PROT_READ | PROT_WRITE);
810 if (result < 0) {
Jeff Brownec4e0062011-10-10 14:50:10 -0700811 status = result;
Jeff Brown5707dbf2011-09-23 21:17:56 -0700812 } else {
813 void* ptr = ::mmap(NULL, len, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
814 if (ptr == MAP_FAILED) {
815 status = -errno;
816 } else {
817 result = ashmem_set_prot_region(fd, PROT_READ);
818 if (result < 0) {
Jeff Brownec4e0062011-10-10 14:50:10 -0700819 status = result;
Jeff Brown5707dbf2011-09-23 21:17:56 -0700820 } else {
821 status = writeInt32(1);
822 if (!status) {
Jeff Brown93ff1f92011-11-04 19:01:44 -0700823 status = writeFileDescriptor(fd, true /*takeOwnership*/);
Jeff Brown5707dbf2011-09-23 21:17:56 -0700824 if (!status) {
825 outBlob->init(true /*mapped*/, ptr, len);
826 return NO_ERROR;
827 }
828 }
829 }
830 }
831 ::munmap(ptr, len);
832 }
833 ::close(fd);
834 return status;
835}
836
Mathias Agopiane1424282013-07-29 21:24:40 -0700837status_t Parcel::write(const FlattenableHelperInterface& val)
Mathias Agopian98e71dd2010-02-11 17:30:52 -0800838{
839 status_t err;
840
841 // size if needed
Mathias Agopiane1424282013-07-29 21:24:40 -0700842 const size_t len = val.getFlattenedSize();
843 const size_t fd_count = val.getFdCount();
Mathias Agopian98e71dd2010-02-11 17:30:52 -0800844
845 err = this->writeInt32(len);
846 if (err) return err;
847
848 err = this->writeInt32(fd_count);
849 if (err) return err;
850
851 // payload
Mathias Agopiane1424282013-07-29 21:24:40 -0700852 void* const buf = this->writeInplace(PAD_SIZE(len));
Mathias Agopian98e71dd2010-02-11 17:30:52 -0800853 if (buf == NULL)
854 return BAD_VALUE;
855
856 int* fds = NULL;
857 if (fd_count) {
858 fds = new int[fd_count];
859 }
860
861 err = val.flatten(buf, len, fds, fd_count);
862 for (size_t i=0 ; i<fd_count && err==NO_ERROR ; i++) {
863 err = this->writeDupFileDescriptor( fds[i] );
864 }
865
866 if (fd_count) {
867 delete [] fds;
868 }
869
870 return err;
871}
872
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700873status_t Parcel::writeObject(const flat_binder_object& val, bool nullMetaData)
874{
875 const bool enoughData = (mDataPos+sizeof(val)) <= mDataCapacity;
876 const bool enoughObjects = mObjectsSize < mObjectsCapacity;
877 if (enoughData && enoughObjects) {
878restart_write:
879 *reinterpret_cast<flat_binder_object*>(mData+mDataPos) = val;
880
881 // Need to write meta-data?
882 if (nullMetaData || val.binder != NULL) {
883 mObjects[mObjectsSize] = mDataPos;
884 acquire_object(ProcessState::self(), val, this);
885 mObjectsSize++;
886 }
887
888 // remember if it's a file descriptor
889 if (val.type == BINDER_TYPE_FD) {
Dianne Hackborn8938ed22011-09-28 23:19:47 -0400890 if (!mAllowFds) {
891 return FDS_NOT_ALLOWED;
892 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700893 mHasFds = mFdsKnown = true;
894 }
895
896 return finishWrite(sizeof(flat_binder_object));
897 }
898
899 if (!enoughData) {
900 const status_t err = growData(sizeof(val));
901 if (err != NO_ERROR) return err;
902 }
903 if (!enoughObjects) {
904 size_t newSize = ((mObjectsSize+2)*3)/2;
905 size_t* objects = (size_t*)realloc(mObjects, newSize*sizeof(size_t));
906 if (objects == NULL) return NO_MEMORY;
907 mObjects = objects;
908 mObjectsCapacity = newSize;
909 }
910
911 goto restart_write;
912}
913
Brad Fitzpatrick837a0d02010-07-13 15:33:35 -0700914status_t Parcel::writeNoException()
915{
916 return writeInt32(0);
917}
918
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700919void Parcel::remove(size_t start, size_t amt)
920{
921 LOG_ALWAYS_FATAL("Parcel::remove() not yet implemented!");
922}
923
924status_t Parcel::read(void* outData, size_t len) const
925{
926 if ((mDataPos+PAD_SIZE(len)) >= mDataPos && (mDataPos+PAD_SIZE(len)) <= mDataSize) {
927 memcpy(outData, mData+mDataPos, len);
928 mDataPos += PAD_SIZE(len);
Steve Block6807e592011-10-20 11:56:00 +0100929 ALOGV("read Setting data pos of %p to %d\n", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700930 return NO_ERROR;
931 }
932 return NOT_ENOUGH_DATA;
933}
934
935const void* Parcel::readInplace(size_t len) const
936{
937 if ((mDataPos+PAD_SIZE(len)) >= mDataPos && (mDataPos+PAD_SIZE(len)) <= mDataSize) {
938 const void* data = mData+mDataPos;
939 mDataPos += PAD_SIZE(len);
Steve Block6807e592011-10-20 11:56:00 +0100940 ALOGV("readInplace Setting data pos of %p to %d\n", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700941 return data;
942 }
943 return NULL;
944}
945
Andreas Huber84a6d042009-08-17 13:33:27 -0700946template<class T>
947status_t Parcel::readAligned(T *pArg) const {
948 COMPILE_TIME_ASSERT_FUNCTION_SCOPE(PAD_SIZE(sizeof(T)) == sizeof(T));
949
950 if ((mDataPos+sizeof(T)) <= mDataSize) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700951 const void* data = mData+mDataPos;
Andreas Huber84a6d042009-08-17 13:33:27 -0700952 mDataPos += sizeof(T);
953 *pArg = *reinterpret_cast<const T*>(data);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700954 return NO_ERROR;
955 } else {
956 return NOT_ENOUGH_DATA;
957 }
958}
959
Andreas Huber84a6d042009-08-17 13:33:27 -0700960template<class T>
961T Parcel::readAligned() const {
962 T result;
963 if (readAligned(&result) != NO_ERROR) {
964 result = 0;
965 }
966
967 return result;
968}
969
970template<class T>
971status_t Parcel::writeAligned(T val) {
972 COMPILE_TIME_ASSERT_FUNCTION_SCOPE(PAD_SIZE(sizeof(T)) == sizeof(T));
973
974 if ((mDataPos+sizeof(val)) <= mDataCapacity) {
975restart_write:
976 *reinterpret_cast<T*>(mData+mDataPos) = val;
977 return finishWrite(sizeof(val));
978 }
979
980 status_t err = growData(sizeof(val));
981 if (err == NO_ERROR) goto restart_write;
982 return err;
983}
984
985status_t Parcel::readInt32(int32_t *pArg) const
986{
987 return readAligned(pArg);
988}
989
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700990int32_t Parcel::readInt32() const
991{
Andreas Huber84a6d042009-08-17 13:33:27 -0700992 return readAligned<int32_t>();
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700993}
994
995
996status_t Parcel::readInt64(int64_t *pArg) const
997{
Andreas Huber84a6d042009-08-17 13:33:27 -0700998 return readAligned(pArg);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700999}
1000
1001
1002int64_t Parcel::readInt64() const
1003{
Andreas Huber84a6d042009-08-17 13:33:27 -07001004 return readAligned<int64_t>();
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001005}
1006
1007status_t Parcel::readFloat(float *pArg) const
1008{
Andreas Huber84a6d042009-08-17 13:33:27 -07001009 return readAligned(pArg);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001010}
1011
1012
1013float Parcel::readFloat() const
1014{
Andreas Huber84a6d042009-08-17 13:33:27 -07001015 return readAligned<float>();
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001016}
1017
Douglas Leungcc1a4bb2013-01-11 15:00:55 -08001018#if defined(__mips__) && defined(__mips_hard_float)
1019
1020status_t Parcel::readDouble(double *pArg) const
1021{
1022 union {
1023 double d;
1024 unsigned long long ll;
1025 } u;
1026 status_t status;
1027 status = readAligned(&u.ll);
1028 *pArg = u.d;
1029 return status;
1030}
1031
1032double Parcel::readDouble() const
1033{
1034 union {
1035 double d;
1036 unsigned long long ll;
1037 } u;
1038 u.ll = readAligned<unsigned long long>();
1039 return u.d;
1040}
1041
1042#else
1043
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001044status_t Parcel::readDouble(double *pArg) const
1045{
Andreas Huber84a6d042009-08-17 13:33:27 -07001046 return readAligned(pArg);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001047}
1048
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001049double Parcel::readDouble() const
1050{
Andreas Huber84a6d042009-08-17 13:33:27 -07001051 return readAligned<double>();
1052}
1053
Douglas Leungcc1a4bb2013-01-11 15:00:55 -08001054#endif
1055
Andreas Huber84a6d042009-08-17 13:33:27 -07001056status_t Parcel::readIntPtr(intptr_t *pArg) const
1057{
1058 return readAligned(pArg);
1059}
1060
1061
1062intptr_t Parcel::readIntPtr() const
1063{
1064 return readAligned<intptr_t>();
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001065}
1066
1067
1068const char* Parcel::readCString() const
1069{
1070 const size_t avail = mDataSize-mDataPos;
1071 if (avail > 0) {
1072 const char* str = reinterpret_cast<const char*>(mData+mDataPos);
1073 // is the string's trailing NUL within the parcel's valid bounds?
1074 const char* eos = reinterpret_cast<const char*>(memchr(str, 0, avail));
1075 if (eos) {
1076 const size_t len = eos - str;
1077 mDataPos += PAD_SIZE(len+1);
Steve Block6807e592011-10-20 11:56:00 +01001078 ALOGV("readCString Setting data pos of %p to %d\n", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001079 return str;
1080 }
1081 }
1082 return NULL;
1083}
1084
1085String8 Parcel::readString8() const
1086{
1087 int32_t size = readInt32();
1088 // watch for potential int overflow adding 1 for trailing NUL
1089 if (size > 0 && size < INT32_MAX) {
1090 const char* str = (const char*)readInplace(size+1);
1091 if (str) return String8(str, size);
1092 }
1093 return String8();
1094}
1095
1096String16 Parcel::readString16() const
1097{
1098 size_t len;
1099 const char16_t* str = readString16Inplace(&len);
1100 if (str) return String16(str, len);
Steve Blocke6f43dd2012-01-06 19:20:56 +00001101 ALOGE("Reading a NULL string not supported here.");
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001102 return String16();
1103}
1104
1105const char16_t* Parcel::readString16Inplace(size_t* outLen) const
1106{
1107 int32_t size = readInt32();
1108 // watch for potential int overflow from size+1
1109 if (size >= 0 && size < INT32_MAX) {
1110 *outLen = size;
1111 const char16_t* str = (const char16_t*)readInplace((size+1)*sizeof(char16_t));
1112 if (str != NULL) {
1113 return str;
1114 }
1115 }
1116 *outLen = 0;
1117 return NULL;
1118}
1119
1120sp<IBinder> Parcel::readStrongBinder() const
1121{
1122 sp<IBinder> val;
1123 unflatten_binder(ProcessState::self(), *this, &val);
1124 return val;
1125}
1126
1127wp<IBinder> Parcel::readWeakBinder() const
1128{
1129 wp<IBinder> val;
1130 unflatten_binder(ProcessState::self(), *this, &val);
1131 return val;
1132}
1133
Brad Fitzpatrick837a0d02010-07-13 15:33:35 -07001134int32_t Parcel::readExceptionCode() const
1135{
1136 int32_t exception_code = readAligned<int32_t>();
Brad Fitzpatrickd36f4a52010-07-12 11:05:38 -07001137 if (exception_code == EX_HAS_REPLY_HEADER) {
Magnus Strandberg1ba24572011-05-03 15:44:00 +02001138 int32_t header_start = dataPosition();
Brad Fitzpatrickd36f4a52010-07-12 11:05:38 -07001139 int32_t header_size = readAligned<int32_t>();
1140 // Skip over fat responses headers. Not used (or propagated) in
1141 // native code
Magnus Strandberg1ba24572011-05-03 15:44:00 +02001142 setDataPosition(header_start + header_size);
Brad Fitzpatrickd36f4a52010-07-12 11:05:38 -07001143 // And fat response headers are currently only used when there are no
1144 // exceptions, so return no error:
1145 return 0;
1146 }
Brad Fitzpatrick837a0d02010-07-13 15:33:35 -07001147 return exception_code;
1148}
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001149
Mathias Agopiana47f02a2009-05-21 16:29:38 -07001150native_handle* Parcel::readNativeHandle() const
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001151{
1152 int numFds, numInts;
1153 status_t err;
1154 err = readInt32(&numFds);
1155 if (err != NO_ERROR) return 0;
1156 err = readInt32(&numInts);
1157 if (err != NO_ERROR) return 0;
1158
Mathias Agopiana47f02a2009-05-21 16:29:38 -07001159 native_handle* h = native_handle_create(numFds, numInts);
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001160 for (int i=0 ; err==NO_ERROR && i<numFds ; i++) {
Rebecca Schultz Zavin360211f2009-02-13 16:34:38 -08001161 h->data[i] = dup(readFileDescriptor());
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001162 if (h->data[i] < 0) err = BAD_VALUE;
1163 }
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001164 err = read(h->data + numFds, sizeof(int)*numInts);
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001165 if (err != NO_ERROR) {
Mathias Agopiana47f02a2009-05-21 16:29:38 -07001166 native_handle_close(h);
1167 native_handle_delete(h);
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001168 h = 0;
1169 }
1170 return h;
1171}
1172
1173
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001174int Parcel::readFileDescriptor() const
1175{
1176 const flat_binder_object* flat = readObject(true);
1177 if (flat) {
1178 switch (flat->type) {
1179 case BINDER_TYPE_FD:
Steve Blocka19954a2012-01-04 20:05:49 +00001180 //ALOGI("Returning file descriptor %ld from parcel %p\n", flat->handle, this);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001181 return flat->handle;
1182 }
1183 }
1184 return BAD_TYPE;
1185}
1186
Mike Lockwoodcbe36fe2013-09-05 08:41:06 -07001187// WARNING: This method must stay in sync with writeToParcel()
1188// in frameworks/base/core/java/android/os/ParcelFileDescriptor.java
1189int Parcel::readParcelFileDescriptor(int& outCommChannel) const {
1190 int fd;
1191 outCommChannel = -1;
1192
1193 if (readInt32() == 0) {
1194 fd = -1;
1195 } else {
1196 fd = readFileDescriptor();
1197 if (fd >= 0 && readInt32() != 0) {
1198 outCommChannel = readFileDescriptor();
1199 }
1200 }
1201 return fd;
1202}
1203
Jeff Brown5707dbf2011-09-23 21:17:56 -07001204status_t Parcel::readBlob(size_t len, ReadableBlob* outBlob) const
1205{
1206 int32_t useAshmem;
1207 status_t status = readInt32(&useAshmem);
1208 if (status) return status;
1209
1210 if (!useAshmem) {
Steve Block6807e592011-10-20 11:56:00 +01001211 ALOGV("readBlob: read in place");
Jeff Brown5707dbf2011-09-23 21:17:56 -07001212 const void* ptr = readInplace(len);
1213 if (!ptr) return BAD_VALUE;
1214
1215 outBlob->init(false /*mapped*/, const_cast<void*>(ptr), len);
1216 return NO_ERROR;
1217 }
1218
Steve Block6807e592011-10-20 11:56:00 +01001219 ALOGV("readBlob: read from ashmem");
Jeff Brown5707dbf2011-09-23 21:17:56 -07001220 int fd = readFileDescriptor();
1221 if (fd == int(BAD_TYPE)) return BAD_VALUE;
1222
1223 void* ptr = ::mmap(NULL, len, PROT_READ, MAP_SHARED, fd, 0);
1224 if (!ptr) return NO_MEMORY;
1225
1226 outBlob->init(true /*mapped*/, ptr, len);
1227 return NO_ERROR;
1228}
1229
Mathias Agopiane1424282013-07-29 21:24:40 -07001230status_t Parcel::read(FlattenableHelperInterface& val) const
Mathias Agopian98e71dd2010-02-11 17:30:52 -08001231{
1232 // size
1233 const size_t len = this->readInt32();
1234 const size_t fd_count = this->readInt32();
1235
1236 // payload
Mathias Agopiane1424282013-07-29 21:24:40 -07001237 void const* const buf = this->readInplace(PAD_SIZE(len));
Mathias Agopian98e71dd2010-02-11 17:30:52 -08001238 if (buf == NULL)
1239 return BAD_VALUE;
1240
1241 int* fds = NULL;
1242 if (fd_count) {
1243 fds = new int[fd_count];
1244 }
1245
1246 status_t err = NO_ERROR;
1247 for (size_t i=0 ; i<fd_count && err==NO_ERROR ; i++) {
1248 fds[i] = dup(this->readFileDescriptor());
1249 if (fds[i] < 0) err = BAD_VALUE;
1250 }
1251
1252 if (err == NO_ERROR) {
1253 err = val.unflatten(buf, len, fds, fd_count);
1254 }
1255
1256 if (fd_count) {
1257 delete [] fds;
1258 }
1259
1260 return err;
1261}
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001262const flat_binder_object* Parcel::readObject(bool nullMetaData) const
1263{
1264 const size_t DPOS = mDataPos;
1265 if ((DPOS+sizeof(flat_binder_object)) <= mDataSize) {
1266 const flat_binder_object* obj
1267 = reinterpret_cast<const flat_binder_object*>(mData+DPOS);
1268 mDataPos = DPOS + sizeof(flat_binder_object);
1269 if (!nullMetaData && (obj->cookie == NULL && obj->binder == NULL)) {
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001270 // When transferring a NULL object, we don't write it into
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001271 // the object list, so we don't want to check for it when
1272 // reading.
Steve Block6807e592011-10-20 11:56:00 +01001273 ALOGV("readObject Setting data pos of %p to %d\n", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001274 return obj;
1275 }
1276
1277 // Ensure that this object is valid...
1278 size_t* const OBJS = mObjects;
1279 const size_t N = mObjectsSize;
1280 size_t opos = mNextObjectHint;
1281
1282 if (N > 0) {
Steve Block6807e592011-10-20 11:56:00 +01001283 ALOGV("Parcel %p looking for obj at %d, hint=%d\n",
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001284 this, DPOS, opos);
1285
1286 // Start at the current hint position, looking for an object at
1287 // the current data position.
1288 if (opos < N) {
1289 while (opos < (N-1) && OBJS[opos] < DPOS) {
1290 opos++;
1291 }
1292 } else {
1293 opos = N-1;
1294 }
1295 if (OBJS[opos] == DPOS) {
1296 // Found it!
Steve Block6807e592011-10-20 11:56:00 +01001297 ALOGV("Parcel found obj %d at index %d with forward search",
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001298 this, DPOS, opos);
1299 mNextObjectHint = opos+1;
Steve Block6807e592011-10-20 11:56:00 +01001300 ALOGV("readObject Setting data pos of %p to %d\n", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001301 return obj;
1302 }
1303
1304 // Look backwards for it...
1305 while (opos > 0 && OBJS[opos] > DPOS) {
1306 opos--;
1307 }
1308 if (OBJS[opos] == DPOS) {
1309 // Found it!
Steve Block6807e592011-10-20 11:56:00 +01001310 ALOGV("Parcel found obj %d at index %d with backward search",
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001311 this, DPOS, opos);
1312 mNextObjectHint = opos+1;
Steve Block6807e592011-10-20 11:56:00 +01001313 ALOGV("readObject Setting data pos of %p to %d\n", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001314 return obj;
1315 }
1316 }
Steve Block32397c12012-01-05 23:22:43 +00001317 ALOGW("Attempt to read object from Parcel %p at offset %d that is not in the object list",
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001318 this, DPOS);
1319 }
1320 return NULL;
1321}
1322
1323void Parcel::closeFileDescriptors()
1324{
1325 size_t i = mObjectsSize;
1326 if (i > 0) {
Steve Blocka19954a2012-01-04 20:05:49 +00001327 //ALOGI("Closing file descriptors for %d objects...", mObjectsSize);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001328 }
1329 while (i > 0) {
1330 i--;
1331 const flat_binder_object* flat
1332 = reinterpret_cast<flat_binder_object*>(mData+mObjects[i]);
1333 if (flat->type == BINDER_TYPE_FD) {
Steve Blocka19954a2012-01-04 20:05:49 +00001334 //ALOGI("Closing fd: %ld\n", flat->handle);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001335 close(flat->handle);
1336 }
1337 }
1338}
1339
1340const uint8_t* Parcel::ipcData() const
1341{
1342 return mData;
1343}
1344
1345size_t Parcel::ipcDataSize() const
1346{
1347 return (mDataSize > mDataPos ? mDataSize : mDataPos);
1348}
1349
1350const size_t* Parcel::ipcObjects() const
1351{
1352 return mObjects;
1353}
1354
1355size_t Parcel::ipcObjectsCount() const
1356{
1357 return mObjectsSize;
1358}
1359
1360void Parcel::ipcSetDataReference(const uint8_t* data, size_t dataSize,
1361 const size_t* objects, size_t objectsCount, release_func relFunc, void* relCookie)
1362{
1363 freeDataNoInit();
1364 mError = NO_ERROR;
1365 mData = const_cast<uint8_t*>(data);
1366 mDataSize = mDataCapacity = dataSize;
Steve Blocka19954a2012-01-04 20:05:49 +00001367 //ALOGI("setDataReference Setting data size of %p to %lu (pid=%d)\n", this, mDataSize, getpid());
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001368 mDataPos = 0;
Steve Block6807e592011-10-20 11:56:00 +01001369 ALOGV("setDataReference Setting data pos of %p to %d\n", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001370 mObjects = const_cast<size_t*>(objects);
1371 mObjectsSize = mObjectsCapacity = objectsCount;
1372 mNextObjectHint = 0;
1373 mOwner = relFunc;
1374 mOwnerCookie = relCookie;
1375 scanForFds();
1376}
1377
1378void Parcel::print(TextOutput& to, uint32_t flags) const
1379{
1380 to << "Parcel(";
1381
1382 if (errorCheck() != NO_ERROR) {
1383 const status_t err = errorCheck();
1384 to << "Error: " << (void*)err << " \"" << strerror(-err) << "\"";
1385 } else if (dataSize() > 0) {
1386 const uint8_t* DATA = data();
1387 to << indent << HexDump(DATA, dataSize()) << dedent;
1388 const size_t* OBJS = objects();
1389 const size_t N = objectsCount();
1390 for (size_t i=0; i<N; i++) {
1391 const flat_binder_object* flat
1392 = reinterpret_cast<const flat_binder_object*>(DATA+OBJS[i]);
1393 to << endl << "Object #" << i << " @ " << (void*)OBJS[i] << ": "
1394 << TypeCode(flat->type & 0x7f7f7f00)
1395 << " = " << flat->binder;
1396 }
1397 } else {
1398 to << "NULL";
1399 }
1400
1401 to << ")";
1402}
1403
1404void Parcel::releaseObjects()
1405{
1406 const sp<ProcessState> proc(ProcessState::self());
1407 size_t i = mObjectsSize;
1408 uint8_t* const data = mData;
1409 size_t* const objects = mObjects;
1410 while (i > 0) {
1411 i--;
1412 const flat_binder_object* flat
1413 = reinterpret_cast<flat_binder_object*>(data+objects[i]);
1414 release_object(proc, *flat, this);
1415 }
1416}
1417
1418void Parcel::acquireObjects()
1419{
1420 const sp<ProcessState> proc(ProcessState::self());
1421 size_t i = mObjectsSize;
1422 uint8_t* const data = mData;
1423 size_t* const objects = mObjects;
1424 while (i > 0) {
1425 i--;
1426 const flat_binder_object* flat
1427 = reinterpret_cast<flat_binder_object*>(data+objects[i]);
1428 acquire_object(proc, *flat, this);
1429 }
1430}
1431
1432void Parcel::freeData()
1433{
1434 freeDataNoInit();
1435 initState();
1436}
1437
1438void Parcel::freeDataNoInit()
1439{
1440 if (mOwner) {
Steve Blocka19954a2012-01-04 20:05:49 +00001441 //ALOGI("Freeing data ref of %p (pid=%d)\n", this, getpid());
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001442 mOwner(this, mData, mDataSize, mObjects, mObjectsSize, mOwnerCookie);
1443 } else {
1444 releaseObjects();
1445 if (mData) free(mData);
1446 if (mObjects) free(mObjects);
1447 }
1448}
1449
1450status_t Parcel::growData(size_t len)
1451{
1452 size_t newSize = ((mDataSize+len)*3)/2;
1453 return (newSize <= mDataSize)
1454 ? (status_t) NO_MEMORY
1455 : continueWrite(newSize);
1456}
1457
1458status_t Parcel::restartWrite(size_t desired)
1459{
1460 if (mOwner) {
1461 freeData();
1462 return continueWrite(desired);
1463 }
1464
1465 uint8_t* data = (uint8_t*)realloc(mData, desired);
1466 if (!data && desired > mDataCapacity) {
1467 mError = NO_MEMORY;
1468 return NO_MEMORY;
1469 }
1470
1471 releaseObjects();
1472
1473 if (data) {
1474 mData = data;
1475 mDataCapacity = desired;
1476 }
1477
1478 mDataSize = mDataPos = 0;
Steve Block6807e592011-10-20 11:56:00 +01001479 ALOGV("restartWrite Setting data size of %p to %d\n", this, mDataSize);
1480 ALOGV("restartWrite Setting data pos of %p to %d\n", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001481
1482 free(mObjects);
1483 mObjects = NULL;
1484 mObjectsSize = mObjectsCapacity = 0;
1485 mNextObjectHint = 0;
1486 mHasFds = false;
1487 mFdsKnown = true;
Dianne Hackborn8938ed22011-09-28 23:19:47 -04001488 mAllowFds = true;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001489
1490 return NO_ERROR;
1491}
1492
1493status_t Parcel::continueWrite(size_t desired)
1494{
1495 // If shrinking, first adjust for any objects that appear
1496 // after the new data size.
1497 size_t objectsSize = mObjectsSize;
1498 if (desired < mDataSize) {
1499 if (desired == 0) {
1500 objectsSize = 0;
1501 } else {
1502 while (objectsSize > 0) {
1503 if (mObjects[objectsSize-1] < desired)
1504 break;
1505 objectsSize--;
1506 }
1507 }
1508 }
1509
1510 if (mOwner) {
1511 // If the size is going to zero, just release the owner's data.
1512 if (desired == 0) {
1513 freeData();
1514 return NO_ERROR;
1515 }
1516
1517 // If there is a different owner, we need to take
1518 // posession.
1519 uint8_t* data = (uint8_t*)malloc(desired);
1520 if (!data) {
1521 mError = NO_MEMORY;
1522 return NO_MEMORY;
1523 }
1524 size_t* objects = NULL;
1525
1526 if (objectsSize) {
1527 objects = (size_t*)malloc(objectsSize*sizeof(size_t));
1528 if (!objects) {
Hyejin Kim3f727c02013-03-09 11:28:54 +09001529 free(data);
1530
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001531 mError = NO_MEMORY;
1532 return NO_MEMORY;
1533 }
1534
1535 // Little hack to only acquire references on objects
1536 // we will be keeping.
1537 size_t oldObjectsSize = mObjectsSize;
1538 mObjectsSize = objectsSize;
1539 acquireObjects();
1540 mObjectsSize = oldObjectsSize;
1541 }
1542
1543 if (mData) {
1544 memcpy(data, mData, mDataSize < desired ? mDataSize : desired);
1545 }
1546 if (objects && mObjects) {
1547 memcpy(objects, mObjects, objectsSize*sizeof(size_t));
1548 }
Steve Blocka19954a2012-01-04 20:05:49 +00001549 //ALOGI("Freeing data ref of %p (pid=%d)\n", this, getpid());
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001550 mOwner(this, mData, mDataSize, mObjects, mObjectsSize, mOwnerCookie);
1551 mOwner = NULL;
1552
1553 mData = data;
1554 mObjects = objects;
1555 mDataSize = (mDataSize < desired) ? mDataSize : desired;
Steve Block6807e592011-10-20 11:56:00 +01001556 ALOGV("continueWrite Setting data size of %p to %d\n", this, mDataSize);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001557 mDataCapacity = desired;
1558 mObjectsSize = mObjectsCapacity = objectsSize;
1559 mNextObjectHint = 0;
1560
1561 } else if (mData) {
1562 if (objectsSize < mObjectsSize) {
1563 // Need to release refs on any objects we are dropping.
1564 const sp<ProcessState> proc(ProcessState::self());
1565 for (size_t i=objectsSize; i<mObjectsSize; i++) {
1566 const flat_binder_object* flat
1567 = reinterpret_cast<flat_binder_object*>(mData+mObjects[i]);
1568 if (flat->type == BINDER_TYPE_FD) {
1569 // will need to rescan because we may have lopped off the only FDs
1570 mFdsKnown = false;
1571 }
1572 release_object(proc, *flat, this);
1573 }
1574 size_t* objects =
1575 (size_t*)realloc(mObjects, objectsSize*sizeof(size_t));
1576 if (objects) {
1577 mObjects = objects;
1578 }
1579 mObjectsSize = objectsSize;
1580 mNextObjectHint = 0;
1581 }
1582
1583 // We own the data, so we can just do a realloc().
1584 if (desired > mDataCapacity) {
1585 uint8_t* data = (uint8_t*)realloc(mData, desired);
1586 if (data) {
1587 mData = data;
1588 mDataCapacity = desired;
1589 } else if (desired > mDataCapacity) {
1590 mError = NO_MEMORY;
1591 return NO_MEMORY;
1592 }
1593 } else {
Dianne Hackborn97e2bcd2011-04-13 18:15:56 -07001594 if (mDataSize > desired) {
1595 mDataSize = desired;
Steve Block6807e592011-10-20 11:56:00 +01001596 ALOGV("continueWrite Setting data size of %p to %d\n", this, mDataSize);
Dianne Hackborn97e2bcd2011-04-13 18:15:56 -07001597 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001598 if (mDataPos > desired) {
1599 mDataPos = desired;
Steve Block6807e592011-10-20 11:56:00 +01001600 ALOGV("continueWrite Setting data pos of %p to %d\n", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001601 }
1602 }
1603
1604 } else {
1605 // This is the first data. Easy!
1606 uint8_t* data = (uint8_t*)malloc(desired);
1607 if (!data) {
1608 mError = NO_MEMORY;
1609 return NO_MEMORY;
1610 }
Hyejin Kim3f727c02013-03-09 11:28:54 +09001611
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001612 if(!(mDataCapacity == 0 && mObjects == NULL
1613 && mObjectsCapacity == 0)) {
Steve Blocke6f43dd2012-01-06 19:20:56 +00001614 ALOGE("continueWrite: %d/%p/%d/%d", mDataCapacity, mObjects, mObjectsCapacity, desired);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001615 }
1616
1617 mData = data;
1618 mDataSize = mDataPos = 0;
Steve Block6807e592011-10-20 11:56:00 +01001619 ALOGV("continueWrite Setting data size of %p to %d\n", this, mDataSize);
1620 ALOGV("continueWrite Setting data pos of %p to %d\n", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001621 mDataCapacity = desired;
1622 }
1623
1624 return NO_ERROR;
1625}
1626
1627void Parcel::initState()
1628{
1629 mError = NO_ERROR;
1630 mData = 0;
1631 mDataSize = 0;
1632 mDataCapacity = 0;
1633 mDataPos = 0;
Steve Block6807e592011-10-20 11:56:00 +01001634 ALOGV("initState Setting data size of %p to %d\n", this, mDataSize);
1635 ALOGV("initState Setting data pos of %p to %d\n", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001636 mObjects = NULL;
1637 mObjectsSize = 0;
1638 mObjectsCapacity = 0;
1639 mNextObjectHint = 0;
1640 mHasFds = false;
1641 mFdsKnown = true;
Dianne Hackborn8938ed22011-09-28 23:19:47 -04001642 mAllowFds = true;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001643 mOwner = NULL;
1644}
1645
1646void Parcel::scanForFds() const
1647{
1648 bool hasFds = false;
1649 for (size_t i=0; i<mObjectsSize; i++) {
1650 const flat_binder_object* flat
1651 = reinterpret_cast<const flat_binder_object*>(mData + mObjects[i]);
1652 if (flat->type == BINDER_TYPE_FD) {
1653 hasFds = true;
1654 break;
1655 }
1656 }
1657 mHasFds = hasFds;
1658 mFdsKnown = true;
1659}
1660
Jeff Brown5707dbf2011-09-23 21:17:56 -07001661// --- Parcel::Blob ---
1662
1663Parcel::Blob::Blob() :
1664 mMapped(false), mData(NULL), mSize(0) {
1665}
1666
1667Parcel::Blob::~Blob() {
1668 release();
1669}
1670
1671void Parcel::Blob::release() {
1672 if (mMapped && mData) {
1673 ::munmap(mData, mSize);
1674 }
1675 clear();
1676}
1677
1678void Parcel::Blob::init(bool mapped, void* data, size_t size) {
1679 mMapped = mapped;
1680 mData = data;
1681 mSize = size;
1682}
1683
1684void Parcel::Blob::clear() {
1685 mMapped = false;
1686 mData = NULL;
1687 mSize = 0;
1688}
1689
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001690}; // namespace android