blob: cb90b836831a096dc50e050f54e14883e5122914 [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>
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -070025#include <utils/Debug.h>
Mathias Agopianc5b2c0b2009-05-19 19:08:10 -070026#include <binder/ProcessState.h>
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -070027#include <utils/Log.h>
28#include <utils/String8.h>
29#include <utils/String16.h>
30#include <utils/TextOutput.h>
31#include <utils/misc.h>
Mathias Agopian98e71dd2010-02-11 17:30:52 -080032#include <utils/Flattenable.h>
Jeff Brown5707dbf2011-09-23 21:17:56 -070033#include <cutils/ashmem.h>
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -070034
Mathias Agopian208059f2009-05-18 15:08:03 -070035#include <private/binder/binder_module.h>
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -070036
37#include <stdio.h>
38#include <stdlib.h>
39#include <stdint.h>
Jeff Brown5707dbf2011-09-23 21:17:56 -070040#include <sys/mman.h>
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -070041
42#ifndef INT32_MAX
43#define INT32_MAX ((int32_t)(2147483647))
44#endif
45
46#define LOG_REFS(...)
Steve Block9f760152011-10-12 17:27:03 +010047//#define LOG_REFS(...) ALOG(LOG_DEBUG, "Parcel", __VA_ARGS__)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -070048
49// ---------------------------------------------------------------------------
50
51#define PAD_SIZE(s) (((s)+3)&~3)
52
Brad Fitzpatricka877cd82010-07-07 16:06:39 -070053// Note: must be kept in sync with android/os/StrictMode.java's PENALTY_GATHER
54#define STRICT_MODE_PENALTY_GATHER 0x100
55
Brad Fitzpatrickd36f4a52010-07-12 11:05:38 -070056// Note: must be kept in sync with android/os/Parcel.java's EX_HAS_REPLY_HEADER
57#define EX_HAS_REPLY_HEADER -128
58
Jeff Brown5707dbf2011-09-23 21:17:56 -070059// Maximum size of a blob to transfer in-place.
60static const size_t IN_PLACE_BLOB_LIMIT = 40 * 1024;
61
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -070062// XXX This can be made public if we want to provide
63// support for typed data.
64struct small_flat_data
65{
66 uint32_t type;
67 uint32_t data;
68};
69
70namespace android {
71
72void acquire_object(const sp<ProcessState>& proc,
73 const flat_binder_object& obj, const void* who)
74{
75 switch (obj.type) {
76 case BINDER_TYPE_BINDER:
77 if (obj.binder) {
78 LOG_REFS("Parcel %p acquiring reference on local %p", who, obj.cookie);
79 static_cast<IBinder*>(obj.cookie)->incStrong(who);
80 }
81 return;
82 case BINDER_TYPE_WEAK_BINDER:
83 if (obj.binder)
84 static_cast<RefBase::weakref_type*>(obj.binder)->incWeak(who);
85 return;
86 case BINDER_TYPE_HANDLE: {
87 const sp<IBinder> b = proc->getStrongProxyForHandle(obj.handle);
88 if (b != NULL) {
89 LOG_REFS("Parcel %p acquiring reference on remote %p", who, b.get());
90 b->incStrong(who);
91 }
92 return;
93 }
94 case BINDER_TYPE_WEAK_HANDLE: {
95 const wp<IBinder> b = proc->getWeakProxyForHandle(obj.handle);
96 if (b != NULL) b.get_refs()->incWeak(who);
97 return;
98 }
99 case BINDER_TYPE_FD: {
100 // intentionally blank -- nothing to do to acquire this, but we do
101 // recognize it as a legitimate object type.
102 return;
103 }
104 }
105
Steve Block9d453682011-12-20 16:23:08 +0000106 ALOGD("Invalid object type 0x%08lx", obj.type);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700107}
108
109void release_object(const sp<ProcessState>& proc,
110 const flat_binder_object& obj, const void* who)
111{
112 switch (obj.type) {
113 case BINDER_TYPE_BINDER:
114 if (obj.binder) {
115 LOG_REFS("Parcel %p releasing reference on local %p", who, obj.cookie);
116 static_cast<IBinder*>(obj.cookie)->decStrong(who);
117 }
118 return;
119 case BINDER_TYPE_WEAK_BINDER:
120 if (obj.binder)
121 static_cast<RefBase::weakref_type*>(obj.binder)->decWeak(who);
122 return;
123 case BINDER_TYPE_HANDLE: {
124 const sp<IBinder> b = proc->getStrongProxyForHandle(obj.handle);
125 if (b != NULL) {
126 LOG_REFS("Parcel %p releasing reference on remote %p", who, b.get());
127 b->decStrong(who);
128 }
129 return;
130 }
131 case BINDER_TYPE_WEAK_HANDLE: {
132 const wp<IBinder> b = proc->getWeakProxyForHandle(obj.handle);
133 if (b != NULL) b.get_refs()->decWeak(who);
134 return;
135 }
136 case BINDER_TYPE_FD: {
137 if (obj.cookie != (void*)0) close(obj.handle);
138 return;
139 }
140 }
141
Steve Blocke6f43dd2012-01-06 19:20:56 +0000142 ALOGE("Invalid object type 0x%08lx", obj.type);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700143}
144
145inline static status_t finish_flatten_binder(
146 const sp<IBinder>& binder, const flat_binder_object& flat, Parcel* out)
147{
148 return out->writeObject(flat, false);
149}
150
151status_t flatten_binder(const sp<ProcessState>& proc,
152 const sp<IBinder>& binder, Parcel* out)
153{
154 flat_binder_object obj;
155
156 obj.flags = 0x7f | FLAT_BINDER_FLAG_ACCEPTS_FDS;
157 if (binder != NULL) {
158 IBinder *local = binder->localBinder();
159 if (!local) {
160 BpBinder *proxy = binder->remoteBinder();
161 if (proxy == NULL) {
Steve Blocke6f43dd2012-01-06 19:20:56 +0000162 ALOGE("null proxy");
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700163 }
164 const int32_t handle = proxy ? proxy->handle() : 0;
165 obj.type = BINDER_TYPE_HANDLE;
166 obj.handle = handle;
167 obj.cookie = NULL;
168 } else {
169 obj.type = BINDER_TYPE_BINDER;
170 obj.binder = local->getWeakRefs();
171 obj.cookie = local;
172 }
173 } else {
174 obj.type = BINDER_TYPE_BINDER;
175 obj.binder = NULL;
176 obj.cookie = NULL;
177 }
178
179 return finish_flatten_binder(binder, obj, out);
180}
181
182status_t flatten_binder(const sp<ProcessState>& proc,
183 const wp<IBinder>& binder, Parcel* out)
184{
185 flat_binder_object obj;
186
187 obj.flags = 0x7f | FLAT_BINDER_FLAG_ACCEPTS_FDS;
188 if (binder != NULL) {
189 sp<IBinder> real = binder.promote();
190 if (real != NULL) {
191 IBinder *local = real->localBinder();
192 if (!local) {
193 BpBinder *proxy = real->remoteBinder();
194 if (proxy == NULL) {
Steve Blocke6f43dd2012-01-06 19:20:56 +0000195 ALOGE("null proxy");
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700196 }
197 const int32_t handle = proxy ? proxy->handle() : 0;
198 obj.type = BINDER_TYPE_WEAK_HANDLE;
199 obj.handle = handle;
200 obj.cookie = NULL;
201 } else {
202 obj.type = BINDER_TYPE_WEAK_BINDER;
203 obj.binder = binder.get_refs();
204 obj.cookie = binder.unsafe_get();
205 }
206 return finish_flatten_binder(real, obj, out);
207 }
208
209 // XXX How to deal? In order to flatten the given binder,
210 // we need to probe it for information, which requires a primary
211 // reference... but we don't have one.
212 //
213 // The OpenBinder implementation uses a dynamic_cast<> here,
214 // but we can't do that with the different reference counting
215 // implementation we are using.
Steve Blocke6f43dd2012-01-06 19:20:56 +0000216 ALOGE("Unable to unflatten Binder weak reference!");
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700217 obj.type = BINDER_TYPE_BINDER;
218 obj.binder = NULL;
219 obj.cookie = NULL;
220 return finish_flatten_binder(NULL, obj, out);
221
222 } else {
223 obj.type = BINDER_TYPE_BINDER;
224 obj.binder = NULL;
225 obj.cookie = NULL;
226 return finish_flatten_binder(NULL, obj, out);
227 }
228}
229
230inline static status_t finish_unflatten_binder(
231 BpBinder* proxy, const flat_binder_object& flat, const Parcel& in)
232{
233 return NO_ERROR;
234}
235
236status_t unflatten_binder(const sp<ProcessState>& proc,
237 const Parcel& in, sp<IBinder>* out)
238{
239 const flat_binder_object* flat = in.readObject(false);
240
241 if (flat) {
242 switch (flat->type) {
243 case BINDER_TYPE_BINDER:
244 *out = static_cast<IBinder*>(flat->cookie);
245 return finish_unflatten_binder(NULL, *flat, in);
246 case BINDER_TYPE_HANDLE:
247 *out = proc->getStrongProxyForHandle(flat->handle);
248 return finish_unflatten_binder(
249 static_cast<BpBinder*>(out->get()), *flat, in);
250 }
251 }
252 return BAD_TYPE;
253}
254
255status_t unflatten_binder(const sp<ProcessState>& proc,
256 const Parcel& in, wp<IBinder>* out)
257{
258 const flat_binder_object* flat = in.readObject(false);
259
260 if (flat) {
261 switch (flat->type) {
262 case BINDER_TYPE_BINDER:
263 *out = static_cast<IBinder*>(flat->cookie);
264 return finish_unflatten_binder(NULL, *flat, in);
265 case BINDER_TYPE_WEAK_BINDER:
266 if (flat->binder != NULL) {
267 out->set_object_and_refs(
268 static_cast<IBinder*>(flat->cookie),
269 static_cast<RefBase::weakref_type*>(flat->binder));
270 } else {
271 *out = NULL;
272 }
273 return finish_unflatten_binder(NULL, *flat, in);
274 case BINDER_TYPE_HANDLE:
275 case BINDER_TYPE_WEAK_HANDLE:
276 *out = proc->getWeakProxyForHandle(flat->handle);
277 return finish_unflatten_binder(
278 static_cast<BpBinder*>(out->unsafe_get()), *flat, in);
279 }
280 }
281 return BAD_TYPE;
282}
283
284// ---------------------------------------------------------------------------
285
286Parcel::Parcel()
287{
288 initState();
289}
290
291Parcel::~Parcel()
292{
293 freeDataNoInit();
294}
295
296const uint8_t* Parcel::data() const
297{
298 return mData;
299}
300
301size_t Parcel::dataSize() const
302{
303 return (mDataSize > mDataPos ? mDataSize : mDataPos);
304}
305
306size_t Parcel::dataAvail() const
307{
308 // TODO: decide what to do about the possibility that this can
309 // report an available-data size that exceeds a Java int's max
310 // positive value, causing havoc. Fortunately this will only
311 // happen if someone constructs a Parcel containing more than two
312 // gigabytes of data, which on typical phone hardware is simply
313 // not possible.
314 return dataSize() - dataPosition();
315}
316
317size_t Parcel::dataPosition() const
318{
319 return mDataPos;
320}
321
322size_t Parcel::dataCapacity() const
323{
324 return mDataCapacity;
325}
326
327status_t Parcel::setDataSize(size_t size)
328{
329 status_t err;
330 err = continueWrite(size);
331 if (err == NO_ERROR) {
332 mDataSize = size;
Steve Block6807e592011-10-20 11:56:00 +0100333 ALOGV("setDataSize Setting data size of %p to %d\n", this, mDataSize);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700334 }
335 return err;
336}
337
338void Parcel::setDataPosition(size_t pos) const
339{
340 mDataPos = pos;
341 mNextObjectHint = 0;
342}
343
344status_t Parcel::setDataCapacity(size_t size)
345{
Dianne Hackborn97e2bcd2011-04-13 18:15:56 -0700346 if (size > mDataCapacity) return continueWrite(size);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700347 return NO_ERROR;
348}
349
350status_t Parcel::setData(const uint8_t* buffer, size_t len)
351{
352 status_t err = restartWrite(len);
353 if (err == NO_ERROR) {
354 memcpy(const_cast<uint8_t*>(data()), buffer, len);
355 mDataSize = len;
356 mFdsKnown = false;
357 }
358 return err;
359}
360
Andreas Huber51faf462011-04-13 10:21:56 -0700361status_t Parcel::appendFrom(const Parcel *parcel, size_t offset, size_t len)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700362{
363 const sp<ProcessState> proc(ProcessState::self());
364 status_t err;
Andreas Huber51faf462011-04-13 10:21:56 -0700365 const uint8_t *data = parcel->mData;
366 const size_t *objects = parcel->mObjects;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700367 size_t size = parcel->mObjectsSize;
368 int startPos = mDataPos;
369 int firstIndex = -1, lastIndex = -2;
370
371 if (len == 0) {
372 return NO_ERROR;
373 }
374
375 // range checks against the source parcel size
376 if ((offset > parcel->mDataSize)
377 || (len > parcel->mDataSize)
378 || (offset + len > parcel->mDataSize)) {
379 return BAD_VALUE;
380 }
381
382 // Count objects in range
383 for (int i = 0; i < (int) size; i++) {
384 size_t off = objects[i];
385 if ((off >= offset) && (off < offset + len)) {
386 if (firstIndex == -1) {
387 firstIndex = i;
388 }
389 lastIndex = i;
390 }
391 }
392 int numObjects = lastIndex - firstIndex + 1;
393
Dianne Hackborn97e2bcd2011-04-13 18:15:56 -0700394 if ((mDataSize+len) > mDataCapacity) {
395 // grow data
396 err = growData(len);
397 if (err != NO_ERROR) {
398 return err;
399 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700400 }
401
402 // append data
403 memcpy(mData + mDataPos, data + offset, len);
404 mDataPos += len;
405 mDataSize += len;
406
Dianne Hackborn8938ed22011-09-28 23:19:47 -0400407 err = NO_ERROR;
408
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700409 if (numObjects > 0) {
410 // grow objects
411 if (mObjectsCapacity < mObjectsSize + numObjects) {
412 int newSize = ((mObjectsSize + numObjects)*3)/2;
413 size_t *objects =
414 (size_t*)realloc(mObjects, newSize*sizeof(size_t));
415 if (objects == (size_t*)0) {
416 return NO_MEMORY;
417 }
418 mObjects = objects;
419 mObjectsCapacity = newSize;
420 }
421
422 // append and acquire objects
423 int idx = mObjectsSize;
424 for (int i = firstIndex; i <= lastIndex; i++) {
425 size_t off = objects[i] - offset + startPos;
426 mObjects[idx++] = off;
427 mObjectsSize++;
428
Dianne Hackborn8af0f822009-05-22 13:20:23 -0700429 flat_binder_object* flat
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700430 = reinterpret_cast<flat_binder_object*>(mData + off);
431 acquire_object(proc, *flat, this);
432
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700433 if (flat->type == BINDER_TYPE_FD) {
Dianne Hackborn8af0f822009-05-22 13:20:23 -0700434 // If this is a file descriptor, we need to dup it so the
435 // new Parcel now owns its own fd, and can declare that we
436 // officially know we have fds.
437 flat->handle = dup(flat->handle);
438 flat->cookie = (void*)1;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700439 mHasFds = mFdsKnown = true;
Dianne Hackborn8938ed22011-09-28 23:19:47 -0400440 if (!mAllowFds) {
441 err = FDS_NOT_ALLOWED;
442 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700443 }
444 }
445 }
446
Dianne Hackborn8938ed22011-09-28 23:19:47 -0400447 return err;
448}
449
Dianne Hackborn7746cc32011-10-03 21:09:35 -0700450bool Parcel::pushAllowFds(bool allowFds)
Dianne Hackborn8938ed22011-09-28 23:19:47 -0400451{
452 const bool origValue = mAllowFds;
Dianne Hackborn7746cc32011-10-03 21:09:35 -0700453 if (!allowFds) {
454 mAllowFds = false;
455 }
Dianne Hackborn8938ed22011-09-28 23:19:47 -0400456 return origValue;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700457}
458
Dianne Hackborn7746cc32011-10-03 21:09:35 -0700459void Parcel::restoreAllowFds(bool lastValue)
460{
461 mAllowFds = lastValue;
462}
463
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700464bool Parcel::hasFileDescriptors() const
465{
466 if (!mFdsKnown) {
467 scanForFds();
468 }
469 return mHasFds;
470}
471
Brad Fitzpatrick702ea9d2010-06-18 13:07:53 -0700472// Write RPC headers. (previously just the interface token)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700473status_t Parcel::writeInterfaceToken(const String16& interface)
474{
Brad Fitzpatricka877cd82010-07-07 16:06:39 -0700475 writeInt32(IPCThreadState::self()->getStrictModePolicy() |
476 STRICT_MODE_PENALTY_GATHER);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700477 // currently the interface identification token is just its name as a string
478 return writeString16(interface);
479}
480
Mathias Agopian83c04462009-05-22 19:00:22 -0700481bool Parcel::checkInterface(IBinder* binder) const
482{
Brad Fitzpatrick702ea9d2010-06-18 13:07:53 -0700483 return enforceInterface(binder->getInterfaceDescriptor());
Mathias Agopian83c04462009-05-22 19:00:22 -0700484}
485
Brad Fitzpatricka877cd82010-07-07 16:06:39 -0700486bool Parcel::enforceInterface(const String16& interface,
Brad Fitzpatrick70081a12010-07-27 09:49:11 -0700487 IPCThreadState* threadState) const
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700488{
Brad Fitzpatrick70081a12010-07-27 09:49:11 -0700489 int32_t strictPolicy = readInt32();
490 if (threadState == NULL) {
491 threadState = IPCThreadState::self();
Brad Fitzpatricka877cd82010-07-07 16:06:39 -0700492 }
Brad Fitzpatrick52736032010-08-30 16:01:16 -0700493 if ((threadState->getLastTransactionBinderFlags() &
494 IBinder::FLAG_ONEWAY) != 0) {
495 // For one-way calls, the callee is running entirely
496 // disconnected from the caller, so disable StrictMode entirely.
497 // Not only does disk/network usage not impact the caller, but
498 // there's no way to commuicate back any violations anyway.
499 threadState->setStrictModePolicy(0);
500 } else {
501 threadState->setStrictModePolicy(strictPolicy);
502 }
Mathias Agopian83c04462009-05-22 19:00:22 -0700503 const String16 str(readString16());
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700504 if (str == interface) {
505 return true;
506 } else {
Steve Block32397c12012-01-05 23:22:43 +0000507 ALOGW("**** enforceInterface() expected '%s' but read '%s'\n",
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700508 String8(interface).string(), String8(str).string());
509 return false;
510 }
Brad Fitzpatrick702ea9d2010-06-18 13:07:53 -0700511}
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700512
513const size_t* Parcel::objects() const
514{
515 return mObjects;
516}
517
518size_t Parcel::objectsCount() const
519{
520 return mObjectsSize;
521}
522
523status_t Parcel::errorCheck() const
524{
525 return mError;
526}
527
528void Parcel::setError(status_t err)
529{
530 mError = err;
531}
532
533status_t Parcel::finishWrite(size_t len)
534{
535 //printf("Finish write of %d\n", len);
536 mDataPos += len;
Steve Block6807e592011-10-20 11:56:00 +0100537 ALOGV("finishWrite Setting data pos of %p to %d\n", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700538 if (mDataPos > mDataSize) {
539 mDataSize = mDataPos;
Steve Block6807e592011-10-20 11:56:00 +0100540 ALOGV("finishWrite Setting data size of %p to %d\n", this, mDataSize);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700541 }
542 //printf("New pos=%d, size=%d\n", mDataPos, mDataSize);
543 return NO_ERROR;
544}
545
546status_t Parcel::writeUnpadded(const void* data, size_t len)
547{
548 size_t end = mDataPos + len;
549 if (end < mDataPos) {
550 // integer overflow
551 return BAD_VALUE;
552 }
553
554 if (end <= mDataCapacity) {
555restart_write:
556 memcpy(mData+mDataPos, data, len);
557 return finishWrite(len);
558 }
559
560 status_t err = growData(len);
561 if (err == NO_ERROR) goto restart_write;
562 return err;
563}
564
565status_t Parcel::write(const void* data, size_t len)
566{
567 void* const d = writeInplace(len);
568 if (d) {
569 memcpy(d, data, len);
570 return NO_ERROR;
571 }
572 return mError;
573}
574
575void* Parcel::writeInplace(size_t len)
576{
577 const size_t padded = PAD_SIZE(len);
578
579 // sanity check for integer overflow
580 if (mDataPos+padded < mDataPos) {
581 return NULL;
582 }
583
584 if ((mDataPos+padded) <= mDataCapacity) {
585restart_write:
586 //printf("Writing %ld bytes, padded to %ld\n", len, padded);
587 uint8_t* const data = mData+mDataPos;
588
589 // Need to pad at end?
590 if (padded != len) {
591#if BYTE_ORDER == BIG_ENDIAN
592 static const uint32_t mask[4] = {
593 0x00000000, 0xffffff00, 0xffff0000, 0xff000000
594 };
595#endif
596#if BYTE_ORDER == LITTLE_ENDIAN
597 static const uint32_t mask[4] = {
598 0x00000000, 0x00ffffff, 0x0000ffff, 0x000000ff
599 };
600#endif
601 //printf("Applying pad mask: %p to %p\n", (void*)mask[padded-len],
602 // *reinterpret_cast<void**>(data+padded-4));
603 *reinterpret_cast<uint32_t*>(data+padded-4) &= mask[padded-len];
604 }
605
606 finishWrite(padded);
607 return data;
608 }
609
610 status_t err = growData(padded);
611 if (err == NO_ERROR) goto restart_write;
612 return NULL;
613}
614
615status_t Parcel::writeInt32(int32_t val)
616{
Andreas Huber84a6d042009-08-17 13:33:27 -0700617 return writeAligned(val);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700618}
619
Marco Nelissen37b44962014-03-13 14:17:40 -0700620status_t Parcel::writeByteArray(size_t len, const uint8_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}
630
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700631status_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
641status_t Parcel::writeDouble(double val)
642{
Andreas Huber84a6d042009-08-17 13:33:27 -0700643 return writeAligned(val);
644}
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700645
Andreas Huber84a6d042009-08-17 13:33:27 -0700646status_t Parcel::writeIntPtr(intptr_t val)
647{
648 return writeAligned(val);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700649}
650
651status_t Parcel::writeCString(const char* str)
652{
653 return write(str, strlen(str)+1);
654}
655
656status_t Parcel::writeString8(const String8& str)
657{
658 status_t err = writeInt32(str.bytes());
Pravat Dalbeherad1dff8d2010-12-15 08:40:00 +0100659 // only write string if its length is more than zero characters,
660 // as readString8 will only read if the length field is non-zero.
661 // this is slightly different from how writeString16 works.
662 if (str.bytes() > 0 && err == NO_ERROR) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700663 err = write(str.string(), str.bytes()+1);
664 }
665 return err;
666}
667
668status_t Parcel::writeString16(const String16& str)
669{
670 return writeString16(str.string(), str.size());
671}
672
673status_t Parcel::writeString16(const char16_t* str, size_t len)
674{
675 if (str == NULL) return writeInt32(-1);
676
677 status_t err = writeInt32(len);
678 if (err == NO_ERROR) {
679 len *= sizeof(char16_t);
680 uint8_t* data = (uint8_t*)writeInplace(len+sizeof(char16_t));
681 if (data) {
682 memcpy(data, str, len);
683 *reinterpret_cast<char16_t*>(data+len) = 0;
684 return NO_ERROR;
685 }
686 err = mError;
687 }
688 return err;
689}
690
691status_t Parcel::writeStrongBinder(const sp<IBinder>& val)
692{
693 return flatten_binder(ProcessState::self(), val, this);
694}
695
696status_t Parcel::writeWeakBinder(const wp<IBinder>& val)
697{
698 return flatten_binder(ProcessState::self(), val, this);
699}
700
Mathias Agopiana47f02a2009-05-21 16:29:38 -0700701status_t Parcel::writeNativeHandle(const native_handle* handle)
The Android Open Source Project5f78a482009-01-20 14:03:58 -0800702{
Mathias Agopian1d0a95b2009-07-31 16:12:13 -0700703 if (!handle || handle->version != sizeof(native_handle))
The Android Open Source Project5f78a482009-01-20 14:03:58 -0800704 return BAD_TYPE;
705
706 status_t err;
Mathias Agopiana47f02a2009-05-21 16:29:38 -0700707 err = writeInt32(handle->numFds);
The Android Open Source Project5f78a482009-01-20 14:03:58 -0800708 if (err != NO_ERROR) return err;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800709
Mathias Agopiana47f02a2009-05-21 16:29:38 -0700710 err = writeInt32(handle->numInts);
The Android Open Source Project5f78a482009-01-20 14:03:58 -0800711 if (err != NO_ERROR) return err;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800712
Mathias Agopiana47f02a2009-05-21 16:29:38 -0700713 for (int i=0 ; err==NO_ERROR && i<handle->numFds ; i++)
714 err = writeDupFileDescriptor(handle->data[i]);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800715
716 if (err != NO_ERROR) {
Steve Block9d453682011-12-20 16:23:08 +0000717 ALOGD("write native handle, write dup fd failed");
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800718 return err;
719 }
Mathias Agopiana47f02a2009-05-21 16:29:38 -0700720 err = write(handle->data + handle->numFds, sizeof(int)*handle->numInts);
The Android Open Source Project5f78a482009-01-20 14:03:58 -0800721 return err;
722}
723
Jeff Brown93ff1f92011-11-04 19:01:44 -0700724status_t Parcel::writeFileDescriptor(int fd, bool takeOwnership)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700725{
726 flat_binder_object obj;
727 obj.type = BINDER_TYPE_FD;
728 obj.flags = 0x7f | FLAT_BINDER_FLAG_ACCEPTS_FDS;
729 obj.handle = fd;
Jeff Brown93ff1f92011-11-04 19:01:44 -0700730 obj.cookie = (void*) (takeOwnership ? 1 : 0);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700731 return writeObject(obj, true);
732}
733
734status_t Parcel::writeDupFileDescriptor(int fd)
735{
Jeff Brownd341c712011-11-04 20:19:33 -0700736 int dupFd = dup(fd);
737 if (dupFd < 0) {
738 return -errno;
739 }
740 status_t err = writeFileDescriptor(dupFd, true /*takeOwnership*/);
741 if (err) {
742 close(dupFd);
743 }
744 return err;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700745}
746
Jeff Brown5707dbf2011-09-23 21:17:56 -0700747status_t Parcel::writeBlob(size_t len, WritableBlob* outBlob)
748{
749 status_t status;
750
751 if (!mAllowFds || len <= IN_PLACE_BLOB_LIMIT) {
Steve Block6807e592011-10-20 11:56:00 +0100752 ALOGV("writeBlob: write in place");
Jeff Brown5707dbf2011-09-23 21:17:56 -0700753 status = writeInt32(0);
754 if (status) return status;
755
756 void* ptr = writeInplace(len);
757 if (!ptr) return NO_MEMORY;
758
759 outBlob->init(false /*mapped*/, ptr, len);
760 return NO_ERROR;
761 }
762
Steve Block6807e592011-10-20 11:56:00 +0100763 ALOGV("writeBlob: write to ashmem");
Jeff Brown5707dbf2011-09-23 21:17:56 -0700764 int fd = ashmem_create_region("Parcel Blob", len);
765 if (fd < 0) return NO_MEMORY;
766
767 int result = ashmem_set_prot_region(fd, PROT_READ | PROT_WRITE);
768 if (result < 0) {
Jeff Brownec4e0062011-10-10 14:50:10 -0700769 status = result;
Jeff Brown5707dbf2011-09-23 21:17:56 -0700770 } else {
771 void* ptr = ::mmap(NULL, len, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
772 if (ptr == MAP_FAILED) {
773 status = -errno;
774 } else {
775 result = ashmem_set_prot_region(fd, PROT_READ);
776 if (result < 0) {
Jeff Brownec4e0062011-10-10 14:50:10 -0700777 status = result;
Jeff Brown5707dbf2011-09-23 21:17:56 -0700778 } else {
779 status = writeInt32(1);
780 if (!status) {
Jeff Brown93ff1f92011-11-04 19:01:44 -0700781 status = writeFileDescriptor(fd, true /*takeOwnership*/);
Jeff Brown5707dbf2011-09-23 21:17:56 -0700782 if (!status) {
783 outBlob->init(true /*mapped*/, ptr, len);
784 return NO_ERROR;
785 }
786 }
787 }
788 }
789 ::munmap(ptr, len);
790 }
791 ::close(fd);
792 return status;
793}
794
Mathias Agopian98e71dd2010-02-11 17:30:52 -0800795status_t Parcel::write(const Flattenable& val)
796{
797 status_t err;
798
799 // size if needed
800 size_t len = val.getFlattenedSize();
801 size_t fd_count = val.getFdCount();
802
803 err = this->writeInt32(len);
804 if (err) return err;
805
806 err = this->writeInt32(fd_count);
807 if (err) return err;
808
809 // payload
810 void* buf = this->writeInplace(PAD_SIZE(len));
811 if (buf == NULL)
812 return BAD_VALUE;
813
814 int* fds = NULL;
815 if (fd_count) {
816 fds = new int[fd_count];
817 }
818
819 err = val.flatten(buf, len, fds, fd_count);
820 for (size_t i=0 ; i<fd_count && err==NO_ERROR ; i++) {
821 err = this->writeDupFileDescriptor( fds[i] );
822 }
823
824 if (fd_count) {
825 delete [] fds;
826 }
827
828 return err;
829}
830
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700831status_t Parcel::writeObject(const flat_binder_object& val, bool nullMetaData)
832{
833 const bool enoughData = (mDataPos+sizeof(val)) <= mDataCapacity;
834 const bool enoughObjects = mObjectsSize < mObjectsCapacity;
835 if (enoughData && enoughObjects) {
836restart_write:
837 *reinterpret_cast<flat_binder_object*>(mData+mDataPos) = val;
838
839 // Need to write meta-data?
840 if (nullMetaData || val.binder != NULL) {
841 mObjects[mObjectsSize] = mDataPos;
842 acquire_object(ProcessState::self(), val, this);
843 mObjectsSize++;
844 }
845
846 // remember if it's a file descriptor
847 if (val.type == BINDER_TYPE_FD) {
Dianne Hackborn8938ed22011-09-28 23:19:47 -0400848 if (!mAllowFds) {
849 return FDS_NOT_ALLOWED;
850 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700851 mHasFds = mFdsKnown = true;
852 }
853
854 return finishWrite(sizeof(flat_binder_object));
855 }
856
857 if (!enoughData) {
858 const status_t err = growData(sizeof(val));
859 if (err != NO_ERROR) return err;
860 }
861 if (!enoughObjects) {
862 size_t newSize = ((mObjectsSize+2)*3)/2;
863 size_t* objects = (size_t*)realloc(mObjects, newSize*sizeof(size_t));
864 if (objects == NULL) return NO_MEMORY;
865 mObjects = objects;
866 mObjectsCapacity = newSize;
867 }
868
869 goto restart_write;
870}
871
Brad Fitzpatrick837a0d02010-07-13 15:33:35 -0700872status_t Parcel::writeNoException()
873{
874 return writeInt32(0);
875}
876
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700877void Parcel::remove(size_t start, size_t amt)
878{
879 LOG_ALWAYS_FATAL("Parcel::remove() not yet implemented!");
880}
881
882status_t Parcel::read(void* outData, size_t len) const
883{
884 if ((mDataPos+PAD_SIZE(len)) >= mDataPos && (mDataPos+PAD_SIZE(len)) <= mDataSize) {
885 memcpy(outData, mData+mDataPos, len);
886 mDataPos += PAD_SIZE(len);
Steve Block6807e592011-10-20 11:56:00 +0100887 ALOGV("read Setting data pos of %p to %d\n", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700888 return NO_ERROR;
889 }
890 return NOT_ENOUGH_DATA;
891}
892
893const void* Parcel::readInplace(size_t len) const
894{
895 if ((mDataPos+PAD_SIZE(len)) >= mDataPos && (mDataPos+PAD_SIZE(len)) <= mDataSize) {
896 const void* data = mData+mDataPos;
897 mDataPos += PAD_SIZE(len);
Steve Block6807e592011-10-20 11:56:00 +0100898 ALOGV("readInplace Setting data pos of %p to %d\n", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700899 return data;
900 }
901 return NULL;
902}
903
Andreas Huber84a6d042009-08-17 13:33:27 -0700904template<class T>
905status_t Parcel::readAligned(T *pArg) const {
906 COMPILE_TIME_ASSERT_FUNCTION_SCOPE(PAD_SIZE(sizeof(T)) == sizeof(T));
907
908 if ((mDataPos+sizeof(T)) <= mDataSize) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700909 const void* data = mData+mDataPos;
Andreas Huber84a6d042009-08-17 13:33:27 -0700910 mDataPos += sizeof(T);
911 *pArg = *reinterpret_cast<const T*>(data);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700912 return NO_ERROR;
913 } else {
914 return NOT_ENOUGH_DATA;
915 }
916}
917
Andreas Huber84a6d042009-08-17 13:33:27 -0700918template<class T>
919T Parcel::readAligned() const {
920 T result;
921 if (readAligned(&result) != NO_ERROR) {
922 result = 0;
923 }
924
925 return result;
926}
927
928template<class T>
929status_t Parcel::writeAligned(T val) {
930 COMPILE_TIME_ASSERT_FUNCTION_SCOPE(PAD_SIZE(sizeof(T)) == sizeof(T));
931
932 if ((mDataPos+sizeof(val)) <= mDataCapacity) {
933restart_write:
934 *reinterpret_cast<T*>(mData+mDataPos) = val;
935 return finishWrite(sizeof(val));
936 }
937
938 status_t err = growData(sizeof(val));
939 if (err == NO_ERROR) goto restart_write;
940 return err;
941}
942
943status_t Parcel::readInt32(int32_t *pArg) const
944{
945 return readAligned(pArg);
946}
947
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700948int32_t Parcel::readInt32() const
949{
Andreas Huber84a6d042009-08-17 13:33:27 -0700950 return readAligned<int32_t>();
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700951}
952
953
954status_t Parcel::readInt64(int64_t *pArg) const
955{
Andreas Huber84a6d042009-08-17 13:33:27 -0700956 return readAligned(pArg);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700957}
958
959
960int64_t Parcel::readInt64() const
961{
Andreas Huber84a6d042009-08-17 13:33:27 -0700962 return readAligned<int64_t>();
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700963}
964
965status_t Parcel::readFloat(float *pArg) const
966{
Andreas Huber84a6d042009-08-17 13:33:27 -0700967 return readAligned(pArg);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700968}
969
970
971float Parcel::readFloat() const
972{
Andreas Huber84a6d042009-08-17 13:33:27 -0700973 return readAligned<float>();
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700974}
975
976status_t Parcel::readDouble(double *pArg) const
977{
Andreas Huber84a6d042009-08-17 13:33:27 -0700978 return readAligned(pArg);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700979}
980
981
982double Parcel::readDouble() const
983{
Andreas Huber84a6d042009-08-17 13:33:27 -0700984 return readAligned<double>();
985}
986
987status_t Parcel::readIntPtr(intptr_t *pArg) const
988{
989 return readAligned(pArg);
990}
991
992
993intptr_t Parcel::readIntPtr() const
994{
995 return readAligned<intptr_t>();
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700996}
997
998
999const char* Parcel::readCString() const
1000{
1001 const size_t avail = mDataSize-mDataPos;
1002 if (avail > 0) {
1003 const char* str = reinterpret_cast<const char*>(mData+mDataPos);
1004 // is the string's trailing NUL within the parcel's valid bounds?
1005 const char* eos = reinterpret_cast<const char*>(memchr(str, 0, avail));
1006 if (eos) {
1007 const size_t len = eos - str;
1008 mDataPos += PAD_SIZE(len+1);
Steve Block6807e592011-10-20 11:56:00 +01001009 ALOGV("readCString Setting data pos of %p to %d\n", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001010 return str;
1011 }
1012 }
1013 return NULL;
1014}
1015
1016String8 Parcel::readString8() const
1017{
1018 int32_t size = readInt32();
1019 // watch for potential int overflow adding 1 for trailing NUL
1020 if (size > 0 && size < INT32_MAX) {
1021 const char* str = (const char*)readInplace(size+1);
1022 if (str) return String8(str, size);
1023 }
1024 return String8();
1025}
1026
1027String16 Parcel::readString16() const
1028{
1029 size_t len;
1030 const char16_t* str = readString16Inplace(&len);
1031 if (str) return String16(str, len);
Steve Blocke6f43dd2012-01-06 19:20:56 +00001032 ALOGE("Reading a NULL string not supported here.");
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001033 return String16();
1034}
1035
1036const char16_t* Parcel::readString16Inplace(size_t* outLen) const
1037{
1038 int32_t size = readInt32();
1039 // watch for potential int overflow from size+1
1040 if (size >= 0 && size < INT32_MAX) {
1041 *outLen = size;
1042 const char16_t* str = (const char16_t*)readInplace((size+1)*sizeof(char16_t));
1043 if (str != NULL) {
1044 return str;
1045 }
1046 }
1047 *outLen = 0;
1048 return NULL;
1049}
1050
1051sp<IBinder> Parcel::readStrongBinder() const
1052{
1053 sp<IBinder> val;
1054 unflatten_binder(ProcessState::self(), *this, &val);
1055 return val;
1056}
1057
1058wp<IBinder> Parcel::readWeakBinder() const
1059{
1060 wp<IBinder> val;
1061 unflatten_binder(ProcessState::self(), *this, &val);
1062 return val;
1063}
1064
Brad Fitzpatrick837a0d02010-07-13 15:33:35 -07001065int32_t Parcel::readExceptionCode() const
1066{
1067 int32_t exception_code = readAligned<int32_t>();
Brad Fitzpatrickd36f4a52010-07-12 11:05:38 -07001068 if (exception_code == EX_HAS_REPLY_HEADER) {
Magnus Strandberg1ba24572011-05-03 15:44:00 +02001069 int32_t header_start = dataPosition();
Brad Fitzpatrickd36f4a52010-07-12 11:05:38 -07001070 int32_t header_size = readAligned<int32_t>();
1071 // Skip over fat responses headers. Not used (or propagated) in
1072 // native code
Magnus Strandberg1ba24572011-05-03 15:44:00 +02001073 setDataPosition(header_start + header_size);
Brad Fitzpatrickd36f4a52010-07-12 11:05:38 -07001074 // And fat response headers are currently only used when there are no
1075 // exceptions, so return no error:
1076 return 0;
1077 }
Brad Fitzpatrick837a0d02010-07-13 15:33:35 -07001078 return exception_code;
1079}
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001080
Mathias Agopiana47f02a2009-05-21 16:29:38 -07001081native_handle* Parcel::readNativeHandle() const
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001082{
1083 int numFds, numInts;
1084 status_t err;
1085 err = readInt32(&numFds);
1086 if (err != NO_ERROR) return 0;
1087 err = readInt32(&numInts);
1088 if (err != NO_ERROR) return 0;
1089
Mathias Agopiana47f02a2009-05-21 16:29:38 -07001090 native_handle* h = native_handle_create(numFds, numInts);
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001091 for (int i=0 ; err==NO_ERROR && i<numFds ; i++) {
Rebecca Schultz Zavin360211f2009-02-13 16:34:38 -08001092 h->data[i] = dup(readFileDescriptor());
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001093 if (h->data[i] < 0) err = BAD_VALUE;
1094 }
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001095 err = read(h->data + numFds, sizeof(int)*numInts);
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001096 if (err != NO_ERROR) {
Mathias Agopiana47f02a2009-05-21 16:29:38 -07001097 native_handle_close(h);
1098 native_handle_delete(h);
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001099 h = 0;
1100 }
1101 return h;
1102}
1103
1104
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001105int Parcel::readFileDescriptor() const
1106{
1107 const flat_binder_object* flat = readObject(true);
1108 if (flat) {
1109 switch (flat->type) {
1110 case BINDER_TYPE_FD:
Steve Blocka19954a2012-01-04 20:05:49 +00001111 //ALOGI("Returning file descriptor %ld from parcel %p\n", flat->handle, this);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001112 return flat->handle;
1113 }
1114 }
1115 return BAD_TYPE;
1116}
1117
Jeff Brown5707dbf2011-09-23 21:17:56 -07001118status_t Parcel::readBlob(size_t len, ReadableBlob* outBlob) const
1119{
1120 int32_t useAshmem;
1121 status_t status = readInt32(&useAshmem);
1122 if (status) return status;
1123
1124 if (!useAshmem) {
Steve Block6807e592011-10-20 11:56:00 +01001125 ALOGV("readBlob: read in place");
Jeff Brown5707dbf2011-09-23 21:17:56 -07001126 const void* ptr = readInplace(len);
1127 if (!ptr) return BAD_VALUE;
1128
1129 outBlob->init(false /*mapped*/, const_cast<void*>(ptr), len);
1130 return NO_ERROR;
1131 }
1132
Steve Block6807e592011-10-20 11:56:00 +01001133 ALOGV("readBlob: read from ashmem");
Jeff Brown5707dbf2011-09-23 21:17:56 -07001134 int fd = readFileDescriptor();
1135 if (fd == int(BAD_TYPE)) return BAD_VALUE;
1136
1137 void* ptr = ::mmap(NULL, len, PROT_READ, MAP_SHARED, fd, 0);
1138 if (!ptr) return NO_MEMORY;
1139
1140 outBlob->init(true /*mapped*/, ptr, len);
1141 return NO_ERROR;
1142}
1143
Mathias Agopian98e71dd2010-02-11 17:30:52 -08001144status_t Parcel::read(Flattenable& val) const
1145{
1146 // size
1147 const size_t len = this->readInt32();
1148 const size_t fd_count = this->readInt32();
1149
1150 // payload
1151 void const* buf = this->readInplace(PAD_SIZE(len));
1152 if (buf == NULL)
1153 return BAD_VALUE;
1154
1155 int* fds = NULL;
1156 if (fd_count) {
1157 fds = new int[fd_count];
1158 }
1159
1160 status_t err = NO_ERROR;
1161 for (size_t i=0 ; i<fd_count && err==NO_ERROR ; i++) {
1162 fds[i] = dup(this->readFileDescriptor());
1163 if (fds[i] < 0) err = BAD_VALUE;
1164 }
1165
1166 if (err == NO_ERROR) {
1167 err = val.unflatten(buf, len, fds, fd_count);
1168 }
1169
1170 if (fd_count) {
1171 delete [] fds;
1172 }
1173
1174 return err;
1175}
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001176const flat_binder_object* Parcel::readObject(bool nullMetaData) const
1177{
1178 const size_t DPOS = mDataPos;
1179 if ((DPOS+sizeof(flat_binder_object)) <= mDataSize) {
1180 const flat_binder_object* obj
1181 = reinterpret_cast<const flat_binder_object*>(mData+DPOS);
1182 mDataPos = DPOS + sizeof(flat_binder_object);
1183 if (!nullMetaData && (obj->cookie == NULL && obj->binder == NULL)) {
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001184 // When transferring a NULL object, we don't write it into
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001185 // the object list, so we don't want to check for it when
1186 // reading.
Steve Block6807e592011-10-20 11:56:00 +01001187 ALOGV("readObject Setting data pos of %p to %d\n", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001188 return obj;
1189 }
1190
1191 // Ensure that this object is valid...
1192 size_t* const OBJS = mObjects;
1193 const size_t N = mObjectsSize;
1194 size_t opos = mNextObjectHint;
1195
1196 if (N > 0) {
Steve Block6807e592011-10-20 11:56:00 +01001197 ALOGV("Parcel %p looking for obj at %d, hint=%d\n",
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001198 this, DPOS, opos);
1199
1200 // Start at the current hint position, looking for an object at
1201 // the current data position.
1202 if (opos < N) {
1203 while (opos < (N-1) && OBJS[opos] < DPOS) {
1204 opos++;
1205 }
1206 } else {
1207 opos = N-1;
1208 }
1209 if (OBJS[opos] == DPOS) {
1210 // Found it!
Steve Block6807e592011-10-20 11:56:00 +01001211 ALOGV("Parcel found obj %d at index %d with forward search",
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001212 this, DPOS, opos);
1213 mNextObjectHint = opos+1;
Steve Block6807e592011-10-20 11:56:00 +01001214 ALOGV("readObject Setting data pos of %p to %d\n", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001215 return obj;
1216 }
1217
1218 // Look backwards for it...
1219 while (opos > 0 && OBJS[opos] > DPOS) {
1220 opos--;
1221 }
1222 if (OBJS[opos] == DPOS) {
1223 // Found it!
Steve Block6807e592011-10-20 11:56:00 +01001224 ALOGV("Parcel found obj %d at index %d with backward search",
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001225 this, DPOS, opos);
1226 mNextObjectHint = opos+1;
Steve Block6807e592011-10-20 11:56:00 +01001227 ALOGV("readObject Setting data pos of %p to %d\n", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001228 return obj;
1229 }
1230 }
Steve Block32397c12012-01-05 23:22:43 +00001231 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 -07001232 this, DPOS);
1233 }
1234 return NULL;
1235}
1236
1237void Parcel::closeFileDescriptors()
1238{
1239 size_t i = mObjectsSize;
1240 if (i > 0) {
Steve Blocka19954a2012-01-04 20:05:49 +00001241 //ALOGI("Closing file descriptors for %d objects...", mObjectsSize);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001242 }
1243 while (i > 0) {
1244 i--;
1245 const flat_binder_object* flat
1246 = reinterpret_cast<flat_binder_object*>(mData+mObjects[i]);
1247 if (flat->type == BINDER_TYPE_FD) {
Steve Blocka19954a2012-01-04 20:05:49 +00001248 //ALOGI("Closing fd: %ld\n", flat->handle);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001249 close(flat->handle);
1250 }
1251 }
1252}
1253
1254const uint8_t* Parcel::ipcData() const
1255{
1256 return mData;
1257}
1258
1259size_t Parcel::ipcDataSize() const
1260{
1261 return (mDataSize > mDataPos ? mDataSize : mDataPos);
1262}
1263
1264const size_t* Parcel::ipcObjects() const
1265{
1266 return mObjects;
1267}
1268
1269size_t Parcel::ipcObjectsCount() const
1270{
1271 return mObjectsSize;
1272}
1273
1274void Parcel::ipcSetDataReference(const uint8_t* data, size_t dataSize,
1275 const size_t* objects, size_t objectsCount, release_func relFunc, void* relCookie)
1276{
1277 freeDataNoInit();
1278 mError = NO_ERROR;
1279 mData = const_cast<uint8_t*>(data);
1280 mDataSize = mDataCapacity = dataSize;
Steve Blocka19954a2012-01-04 20:05:49 +00001281 //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 -07001282 mDataPos = 0;
Steve Block6807e592011-10-20 11:56:00 +01001283 ALOGV("setDataReference Setting data pos of %p to %d\n", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001284 mObjects = const_cast<size_t*>(objects);
1285 mObjectsSize = mObjectsCapacity = objectsCount;
1286 mNextObjectHint = 0;
1287 mOwner = relFunc;
1288 mOwnerCookie = relCookie;
1289 scanForFds();
1290}
1291
1292void Parcel::print(TextOutput& to, uint32_t flags) const
1293{
1294 to << "Parcel(";
1295
1296 if (errorCheck() != NO_ERROR) {
1297 const status_t err = errorCheck();
1298 to << "Error: " << (void*)err << " \"" << strerror(-err) << "\"";
1299 } else if (dataSize() > 0) {
1300 const uint8_t* DATA = data();
1301 to << indent << HexDump(DATA, dataSize()) << dedent;
1302 const size_t* OBJS = objects();
1303 const size_t N = objectsCount();
1304 for (size_t i=0; i<N; i++) {
1305 const flat_binder_object* flat
1306 = reinterpret_cast<const flat_binder_object*>(DATA+OBJS[i]);
1307 to << endl << "Object #" << i << " @ " << (void*)OBJS[i] << ": "
1308 << TypeCode(flat->type & 0x7f7f7f00)
1309 << " = " << flat->binder;
1310 }
1311 } else {
1312 to << "NULL";
1313 }
1314
1315 to << ")";
1316}
1317
1318void Parcel::releaseObjects()
1319{
1320 const sp<ProcessState> proc(ProcessState::self());
1321 size_t i = mObjectsSize;
1322 uint8_t* const data = mData;
1323 size_t* const objects = mObjects;
1324 while (i > 0) {
1325 i--;
1326 const flat_binder_object* flat
1327 = reinterpret_cast<flat_binder_object*>(data+objects[i]);
1328 release_object(proc, *flat, this);
1329 }
1330}
1331
1332void Parcel::acquireObjects()
1333{
1334 const sp<ProcessState> proc(ProcessState::self());
1335 size_t i = mObjectsSize;
1336 uint8_t* const data = mData;
1337 size_t* const objects = mObjects;
1338 while (i > 0) {
1339 i--;
1340 const flat_binder_object* flat
1341 = reinterpret_cast<flat_binder_object*>(data+objects[i]);
1342 acquire_object(proc, *flat, this);
1343 }
1344}
1345
1346void Parcel::freeData()
1347{
1348 freeDataNoInit();
1349 initState();
1350}
1351
1352void Parcel::freeDataNoInit()
1353{
1354 if (mOwner) {
Steve Blocka19954a2012-01-04 20:05:49 +00001355 //ALOGI("Freeing data ref of %p (pid=%d)\n", this, getpid());
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001356 mOwner(this, mData, mDataSize, mObjects, mObjectsSize, mOwnerCookie);
1357 } else {
1358 releaseObjects();
1359 if (mData) free(mData);
1360 if (mObjects) free(mObjects);
1361 }
1362}
1363
1364status_t Parcel::growData(size_t len)
1365{
1366 size_t newSize = ((mDataSize+len)*3)/2;
1367 return (newSize <= mDataSize)
1368 ? (status_t) NO_MEMORY
1369 : continueWrite(newSize);
1370}
1371
1372status_t Parcel::restartWrite(size_t desired)
1373{
1374 if (mOwner) {
1375 freeData();
1376 return continueWrite(desired);
1377 }
1378
1379 uint8_t* data = (uint8_t*)realloc(mData, desired);
1380 if (!data && desired > mDataCapacity) {
1381 mError = NO_MEMORY;
1382 return NO_MEMORY;
1383 }
1384
1385 releaseObjects();
1386
1387 if (data) {
1388 mData = data;
1389 mDataCapacity = desired;
1390 }
1391
1392 mDataSize = mDataPos = 0;
Steve Block6807e592011-10-20 11:56:00 +01001393 ALOGV("restartWrite Setting data size of %p to %d\n", this, mDataSize);
1394 ALOGV("restartWrite Setting data pos of %p to %d\n", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001395
1396 free(mObjects);
1397 mObjects = NULL;
1398 mObjectsSize = mObjectsCapacity = 0;
1399 mNextObjectHint = 0;
1400 mHasFds = false;
1401 mFdsKnown = true;
Dianne Hackborn8938ed22011-09-28 23:19:47 -04001402 mAllowFds = true;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001403
1404 return NO_ERROR;
1405}
1406
1407status_t Parcel::continueWrite(size_t desired)
1408{
1409 // If shrinking, first adjust for any objects that appear
1410 // after the new data size.
1411 size_t objectsSize = mObjectsSize;
1412 if (desired < mDataSize) {
1413 if (desired == 0) {
1414 objectsSize = 0;
1415 } else {
1416 while (objectsSize > 0) {
1417 if (mObjects[objectsSize-1] < desired)
1418 break;
1419 objectsSize--;
1420 }
1421 }
1422 }
1423
1424 if (mOwner) {
1425 // If the size is going to zero, just release the owner's data.
1426 if (desired == 0) {
1427 freeData();
1428 return NO_ERROR;
1429 }
1430
1431 // If there is a different owner, we need to take
1432 // posession.
1433 uint8_t* data = (uint8_t*)malloc(desired);
1434 if (!data) {
1435 mError = NO_MEMORY;
1436 return NO_MEMORY;
1437 }
1438 size_t* objects = NULL;
1439
1440 if (objectsSize) {
1441 objects = (size_t*)malloc(objectsSize*sizeof(size_t));
1442 if (!objects) {
1443 mError = NO_MEMORY;
1444 return NO_MEMORY;
1445 }
1446
1447 // Little hack to only acquire references on objects
1448 // we will be keeping.
1449 size_t oldObjectsSize = mObjectsSize;
1450 mObjectsSize = objectsSize;
1451 acquireObjects();
1452 mObjectsSize = oldObjectsSize;
1453 }
1454
1455 if (mData) {
1456 memcpy(data, mData, mDataSize < desired ? mDataSize : desired);
1457 }
1458 if (objects && mObjects) {
1459 memcpy(objects, mObjects, objectsSize*sizeof(size_t));
1460 }
Steve Blocka19954a2012-01-04 20:05:49 +00001461 //ALOGI("Freeing data ref of %p (pid=%d)\n", this, getpid());
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001462 mOwner(this, mData, mDataSize, mObjects, mObjectsSize, mOwnerCookie);
1463 mOwner = NULL;
1464
1465 mData = data;
1466 mObjects = objects;
1467 mDataSize = (mDataSize < desired) ? mDataSize : desired;
Steve Block6807e592011-10-20 11:56:00 +01001468 ALOGV("continueWrite Setting data size of %p to %d\n", this, mDataSize);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001469 mDataCapacity = desired;
1470 mObjectsSize = mObjectsCapacity = objectsSize;
1471 mNextObjectHint = 0;
1472
1473 } else if (mData) {
1474 if (objectsSize < mObjectsSize) {
1475 // Need to release refs on any objects we are dropping.
1476 const sp<ProcessState> proc(ProcessState::self());
1477 for (size_t i=objectsSize; i<mObjectsSize; i++) {
1478 const flat_binder_object* flat
1479 = reinterpret_cast<flat_binder_object*>(mData+mObjects[i]);
1480 if (flat->type == BINDER_TYPE_FD) {
1481 // will need to rescan because we may have lopped off the only FDs
1482 mFdsKnown = false;
1483 }
1484 release_object(proc, *flat, this);
1485 }
1486 size_t* objects =
1487 (size_t*)realloc(mObjects, objectsSize*sizeof(size_t));
1488 if (objects) {
1489 mObjects = objects;
1490 }
1491 mObjectsSize = objectsSize;
1492 mNextObjectHint = 0;
1493 }
1494
1495 // We own the data, so we can just do a realloc().
1496 if (desired > mDataCapacity) {
1497 uint8_t* data = (uint8_t*)realloc(mData, desired);
1498 if (data) {
1499 mData = data;
1500 mDataCapacity = desired;
1501 } else if (desired > mDataCapacity) {
1502 mError = NO_MEMORY;
1503 return NO_MEMORY;
1504 }
1505 } else {
Dianne Hackborn97e2bcd2011-04-13 18:15:56 -07001506 if (mDataSize > desired) {
1507 mDataSize = desired;
Steve Block6807e592011-10-20 11:56:00 +01001508 ALOGV("continueWrite Setting data size of %p to %d\n", this, mDataSize);
Dianne Hackborn97e2bcd2011-04-13 18:15:56 -07001509 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001510 if (mDataPos > desired) {
1511 mDataPos = desired;
Steve Block6807e592011-10-20 11:56:00 +01001512 ALOGV("continueWrite Setting data pos of %p to %d\n", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001513 }
1514 }
1515
1516 } else {
1517 // This is the first data. Easy!
1518 uint8_t* data = (uint8_t*)malloc(desired);
1519 if (!data) {
1520 mError = NO_MEMORY;
1521 return NO_MEMORY;
1522 }
1523
1524 if(!(mDataCapacity == 0 && mObjects == NULL
1525 && mObjectsCapacity == 0)) {
Steve Blocke6f43dd2012-01-06 19:20:56 +00001526 ALOGE("continueWrite: %d/%p/%d/%d", mDataCapacity, mObjects, mObjectsCapacity, desired);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001527 }
1528
1529 mData = data;
1530 mDataSize = mDataPos = 0;
Steve Block6807e592011-10-20 11:56:00 +01001531 ALOGV("continueWrite Setting data size of %p to %d\n", this, mDataSize);
1532 ALOGV("continueWrite Setting data pos of %p to %d\n", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001533 mDataCapacity = desired;
1534 }
1535
1536 return NO_ERROR;
1537}
1538
1539void Parcel::initState()
1540{
1541 mError = NO_ERROR;
1542 mData = 0;
1543 mDataSize = 0;
1544 mDataCapacity = 0;
1545 mDataPos = 0;
Steve Block6807e592011-10-20 11:56:00 +01001546 ALOGV("initState Setting data size of %p to %d\n", this, mDataSize);
1547 ALOGV("initState Setting data pos of %p to %d\n", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001548 mObjects = NULL;
1549 mObjectsSize = 0;
1550 mObjectsCapacity = 0;
1551 mNextObjectHint = 0;
1552 mHasFds = false;
1553 mFdsKnown = true;
Dianne Hackborn8938ed22011-09-28 23:19:47 -04001554 mAllowFds = true;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001555 mOwner = NULL;
1556}
1557
1558void Parcel::scanForFds() const
1559{
1560 bool hasFds = false;
1561 for (size_t i=0; i<mObjectsSize; i++) {
1562 const flat_binder_object* flat
1563 = reinterpret_cast<const flat_binder_object*>(mData + mObjects[i]);
1564 if (flat->type == BINDER_TYPE_FD) {
1565 hasFds = true;
1566 break;
1567 }
1568 }
1569 mHasFds = hasFds;
1570 mFdsKnown = true;
1571}
1572
Jeff Brown5707dbf2011-09-23 21:17:56 -07001573// --- Parcel::Blob ---
1574
1575Parcel::Blob::Blob() :
1576 mMapped(false), mData(NULL), mSize(0) {
1577}
1578
1579Parcel::Blob::~Blob() {
1580 release();
1581}
1582
1583void Parcel::Blob::release() {
1584 if (mMapped && mData) {
1585 ::munmap(mData, mSize);
1586 }
1587 clear();
1588}
1589
1590void Parcel::Blob::init(bool mapped, void* data, size_t size) {
1591 mMapped = mapped;
1592 mData = data;
1593 mSize = size;
1594}
1595
1596void Parcel::Blob::clear() {
1597 mMapped = false;
1598 mData = NULL;
1599 mSize = 0;
1600}
1601
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001602}; // namespace android