blob: 9552c1cfc5c45ad028b46a934c5fba3bc043d170 [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(...)
47//#define LOG_REFS(...) LOG(LOG_DEBUG, "Parcel", __VA_ARGS__)
48
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
106 LOGD("Invalid object type 0x%08lx", obj.type);
107}
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
142 LOGE("Invalid object type 0x%08lx", obj.type);
143}
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) {
162 LOGE("null proxy");
163 }
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) {
195 LOGE("null proxy");
196 }
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.
216 LOGE("Unable to unflatten Binder weak reference!");
217 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;
333 LOGV("setDataSize Setting data size of %p to %d\n", this, mDataSize);
334 }
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
450bool Parcel::setAllowFds(bool allowFds)
451{
452 const bool origValue = mAllowFds;
453 mAllowFds = allowFds;
454 return origValue;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700455}
456
457bool Parcel::hasFileDescriptors() const
458{
459 if (!mFdsKnown) {
460 scanForFds();
461 }
462 return mHasFds;
463}
464
Brad Fitzpatrick702ea9d2010-06-18 13:07:53 -0700465// Write RPC headers. (previously just the interface token)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700466status_t Parcel::writeInterfaceToken(const String16& interface)
467{
Brad Fitzpatricka877cd82010-07-07 16:06:39 -0700468 writeInt32(IPCThreadState::self()->getStrictModePolicy() |
469 STRICT_MODE_PENALTY_GATHER);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700470 // currently the interface identification token is just its name as a string
471 return writeString16(interface);
472}
473
Mathias Agopian83c04462009-05-22 19:00:22 -0700474bool Parcel::checkInterface(IBinder* binder) const
475{
Brad Fitzpatrick702ea9d2010-06-18 13:07:53 -0700476 return enforceInterface(binder->getInterfaceDescriptor());
Mathias Agopian83c04462009-05-22 19:00:22 -0700477}
478
Brad Fitzpatricka877cd82010-07-07 16:06:39 -0700479bool Parcel::enforceInterface(const String16& interface,
Brad Fitzpatrick70081a12010-07-27 09:49:11 -0700480 IPCThreadState* threadState) const
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700481{
Brad Fitzpatrick70081a12010-07-27 09:49:11 -0700482 int32_t strictPolicy = readInt32();
483 if (threadState == NULL) {
484 threadState = IPCThreadState::self();
Brad Fitzpatricka877cd82010-07-07 16:06:39 -0700485 }
Brad Fitzpatrick52736032010-08-30 16:01:16 -0700486 if ((threadState->getLastTransactionBinderFlags() &
487 IBinder::FLAG_ONEWAY) != 0) {
488 // For one-way calls, the callee is running entirely
489 // disconnected from the caller, so disable StrictMode entirely.
490 // Not only does disk/network usage not impact the caller, but
491 // there's no way to commuicate back any violations anyway.
492 threadState->setStrictModePolicy(0);
493 } else {
494 threadState->setStrictModePolicy(strictPolicy);
495 }
Mathias Agopian83c04462009-05-22 19:00:22 -0700496 const String16 str(readString16());
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700497 if (str == interface) {
498 return true;
499 } else {
500 LOGW("**** enforceInterface() expected '%s' but read '%s'\n",
501 String8(interface).string(), String8(str).string());
502 return false;
503 }
Brad Fitzpatrick702ea9d2010-06-18 13:07:53 -0700504}
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700505
506const size_t* Parcel::objects() const
507{
508 return mObjects;
509}
510
511size_t Parcel::objectsCount() const
512{
513 return mObjectsSize;
514}
515
516status_t Parcel::errorCheck() const
517{
518 return mError;
519}
520
521void Parcel::setError(status_t err)
522{
523 mError = err;
524}
525
526status_t Parcel::finishWrite(size_t len)
527{
528 //printf("Finish write of %d\n", len);
529 mDataPos += len;
530 LOGV("finishWrite Setting data pos of %p to %d\n", this, mDataPos);
531 if (mDataPos > mDataSize) {
532 mDataSize = mDataPos;
533 LOGV("finishWrite Setting data size of %p to %d\n", this, mDataSize);
534 }
535 //printf("New pos=%d, size=%d\n", mDataPos, mDataSize);
536 return NO_ERROR;
537}
538
539status_t Parcel::writeUnpadded(const void* data, size_t len)
540{
541 size_t end = mDataPos + len;
542 if (end < mDataPos) {
543 // integer overflow
544 return BAD_VALUE;
545 }
546
547 if (end <= mDataCapacity) {
548restart_write:
549 memcpy(mData+mDataPos, data, len);
550 return finishWrite(len);
551 }
552
553 status_t err = growData(len);
554 if (err == NO_ERROR) goto restart_write;
555 return err;
556}
557
558status_t Parcel::write(const void* data, size_t len)
559{
560 void* const d = writeInplace(len);
561 if (d) {
562 memcpy(d, data, len);
563 return NO_ERROR;
564 }
565 return mError;
566}
567
568void* Parcel::writeInplace(size_t len)
569{
570 const size_t padded = PAD_SIZE(len);
571
572 // sanity check for integer overflow
573 if (mDataPos+padded < mDataPos) {
574 return NULL;
575 }
576
577 if ((mDataPos+padded) <= mDataCapacity) {
578restart_write:
579 //printf("Writing %ld bytes, padded to %ld\n", len, padded);
580 uint8_t* const data = mData+mDataPos;
581
582 // Need to pad at end?
583 if (padded != len) {
584#if BYTE_ORDER == BIG_ENDIAN
585 static const uint32_t mask[4] = {
586 0x00000000, 0xffffff00, 0xffff0000, 0xff000000
587 };
588#endif
589#if BYTE_ORDER == LITTLE_ENDIAN
590 static const uint32_t mask[4] = {
591 0x00000000, 0x00ffffff, 0x0000ffff, 0x000000ff
592 };
593#endif
594 //printf("Applying pad mask: %p to %p\n", (void*)mask[padded-len],
595 // *reinterpret_cast<void**>(data+padded-4));
596 *reinterpret_cast<uint32_t*>(data+padded-4) &= mask[padded-len];
597 }
598
599 finishWrite(padded);
600 return data;
601 }
602
603 status_t err = growData(padded);
604 if (err == NO_ERROR) goto restart_write;
605 return NULL;
606}
607
608status_t Parcel::writeInt32(int32_t val)
609{
Andreas Huber84a6d042009-08-17 13:33:27 -0700610 return writeAligned(val);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700611}
612
613status_t Parcel::writeInt64(int64_t val)
614{
Andreas Huber84a6d042009-08-17 13:33:27 -0700615 return writeAligned(val);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700616}
617
618status_t Parcel::writeFloat(float val)
619{
Andreas Huber84a6d042009-08-17 13:33:27 -0700620 return writeAligned(val);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700621}
622
623status_t Parcel::writeDouble(double val)
624{
Andreas Huber84a6d042009-08-17 13:33:27 -0700625 return writeAligned(val);
626}
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700627
Andreas Huber84a6d042009-08-17 13:33:27 -0700628status_t Parcel::writeIntPtr(intptr_t val)
629{
630 return writeAligned(val);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700631}
632
633status_t Parcel::writeCString(const char* str)
634{
635 return write(str, strlen(str)+1);
636}
637
638status_t Parcel::writeString8(const String8& str)
639{
640 status_t err = writeInt32(str.bytes());
Pravat Dalbeherad1dff8d2010-12-15 08:40:00 +0100641 // only write string if its length is more than zero characters,
642 // as readString8 will only read if the length field is non-zero.
643 // this is slightly different from how writeString16 works.
644 if (str.bytes() > 0 && err == NO_ERROR) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700645 err = write(str.string(), str.bytes()+1);
646 }
647 return err;
648}
649
650status_t Parcel::writeString16(const String16& str)
651{
652 return writeString16(str.string(), str.size());
653}
654
655status_t Parcel::writeString16(const char16_t* str, size_t len)
656{
657 if (str == NULL) return writeInt32(-1);
658
659 status_t err = writeInt32(len);
660 if (err == NO_ERROR) {
661 len *= sizeof(char16_t);
662 uint8_t* data = (uint8_t*)writeInplace(len+sizeof(char16_t));
663 if (data) {
664 memcpy(data, str, len);
665 *reinterpret_cast<char16_t*>(data+len) = 0;
666 return NO_ERROR;
667 }
668 err = mError;
669 }
670 return err;
671}
672
673status_t Parcel::writeStrongBinder(const sp<IBinder>& val)
674{
675 return flatten_binder(ProcessState::self(), val, this);
676}
677
678status_t Parcel::writeWeakBinder(const wp<IBinder>& val)
679{
680 return flatten_binder(ProcessState::self(), val, this);
681}
682
Mathias Agopiana47f02a2009-05-21 16:29:38 -0700683status_t Parcel::writeNativeHandle(const native_handle* handle)
The Android Open Source Project5f78a482009-01-20 14:03:58 -0800684{
Mathias Agopian1d0a95b2009-07-31 16:12:13 -0700685 if (!handle || handle->version != sizeof(native_handle))
The Android Open Source Project5f78a482009-01-20 14:03:58 -0800686 return BAD_TYPE;
687
688 status_t err;
Mathias Agopiana47f02a2009-05-21 16:29:38 -0700689 err = writeInt32(handle->numFds);
The Android Open Source Project5f78a482009-01-20 14:03:58 -0800690 if (err != NO_ERROR) return err;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800691
Mathias Agopiana47f02a2009-05-21 16:29:38 -0700692 err = writeInt32(handle->numInts);
The Android Open Source Project5f78a482009-01-20 14:03:58 -0800693 if (err != NO_ERROR) return err;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800694
Mathias Agopiana47f02a2009-05-21 16:29:38 -0700695 for (int i=0 ; err==NO_ERROR && i<handle->numFds ; i++)
696 err = writeDupFileDescriptor(handle->data[i]);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800697
698 if (err != NO_ERROR) {
699 LOGD("write native handle, write dup fd failed");
700 return err;
701 }
Mathias Agopiana47f02a2009-05-21 16:29:38 -0700702 err = write(handle->data + handle->numFds, sizeof(int)*handle->numInts);
The Android Open Source Project5f78a482009-01-20 14:03:58 -0800703 return err;
704}
705
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700706status_t Parcel::writeFileDescriptor(int fd)
707{
708 flat_binder_object obj;
709 obj.type = BINDER_TYPE_FD;
710 obj.flags = 0x7f | FLAT_BINDER_FLAG_ACCEPTS_FDS;
711 obj.handle = fd;
712 obj.cookie = (void*)0;
713 return writeObject(obj, true);
714}
715
716status_t Parcel::writeDupFileDescriptor(int fd)
717{
718 flat_binder_object obj;
719 obj.type = BINDER_TYPE_FD;
720 obj.flags = 0x7f | FLAT_BINDER_FLAG_ACCEPTS_FDS;
721 obj.handle = dup(fd);
722 obj.cookie = (void*)1;
723 return writeObject(obj, true);
724}
725
Jeff Brown5707dbf2011-09-23 21:17:56 -0700726status_t Parcel::writeBlob(size_t len, WritableBlob* outBlob)
727{
728 status_t status;
729
730 if (!mAllowFds || len <= IN_PLACE_BLOB_LIMIT) {
731 LOGV("writeBlob: write in place");
732 status = writeInt32(0);
733 if (status) return status;
734
735 void* ptr = writeInplace(len);
736 if (!ptr) return NO_MEMORY;
737
738 outBlob->init(false /*mapped*/, ptr, len);
739 return NO_ERROR;
740 }
741
742 LOGV("writeBlob: write to ashmem");
743 int fd = ashmem_create_region("Parcel Blob", len);
744 if (fd < 0) return NO_MEMORY;
745
746 int result = ashmem_set_prot_region(fd, PROT_READ | PROT_WRITE);
747 if (result < 0) {
748 status = -result;
749 } else {
750 void* ptr = ::mmap(NULL, len, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
751 if (ptr == MAP_FAILED) {
752 status = -errno;
753 } else {
754 result = ashmem_set_prot_region(fd, PROT_READ);
755 if (result < 0) {
756 status = -result;
757 } else {
758 status = writeInt32(1);
759 if (!status) {
760 status = writeFileDescriptor(fd);
761 if (!status) {
762 outBlob->init(true /*mapped*/, ptr, len);
763 return NO_ERROR;
764 }
765 }
766 }
767 }
768 ::munmap(ptr, len);
769 }
770 ::close(fd);
771 return status;
772}
773
Mathias Agopian98e71dd2010-02-11 17:30:52 -0800774status_t Parcel::write(const Flattenable& val)
775{
776 status_t err;
777
778 // size if needed
779 size_t len = val.getFlattenedSize();
780 size_t fd_count = val.getFdCount();
781
782 err = this->writeInt32(len);
783 if (err) return err;
784
785 err = this->writeInt32(fd_count);
786 if (err) return err;
787
788 // payload
789 void* buf = this->writeInplace(PAD_SIZE(len));
790 if (buf == NULL)
791 return BAD_VALUE;
792
793 int* fds = NULL;
794 if (fd_count) {
795 fds = new int[fd_count];
796 }
797
798 err = val.flatten(buf, len, fds, fd_count);
799 for (size_t i=0 ; i<fd_count && err==NO_ERROR ; i++) {
800 err = this->writeDupFileDescriptor( fds[i] );
801 }
802
803 if (fd_count) {
804 delete [] fds;
805 }
806
807 return err;
808}
809
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700810status_t Parcel::writeObject(const flat_binder_object& val, bool nullMetaData)
811{
812 const bool enoughData = (mDataPos+sizeof(val)) <= mDataCapacity;
813 const bool enoughObjects = mObjectsSize < mObjectsCapacity;
814 if (enoughData && enoughObjects) {
815restart_write:
816 *reinterpret_cast<flat_binder_object*>(mData+mDataPos) = val;
817
818 // Need to write meta-data?
819 if (nullMetaData || val.binder != NULL) {
820 mObjects[mObjectsSize] = mDataPos;
821 acquire_object(ProcessState::self(), val, this);
822 mObjectsSize++;
823 }
824
825 // remember if it's a file descriptor
826 if (val.type == BINDER_TYPE_FD) {
Dianne Hackborn8938ed22011-09-28 23:19:47 -0400827 if (!mAllowFds) {
828 return FDS_NOT_ALLOWED;
829 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700830 mHasFds = mFdsKnown = true;
831 }
832
833 return finishWrite(sizeof(flat_binder_object));
834 }
835
836 if (!enoughData) {
837 const status_t err = growData(sizeof(val));
838 if (err != NO_ERROR) return err;
839 }
840 if (!enoughObjects) {
841 size_t newSize = ((mObjectsSize+2)*3)/2;
842 size_t* objects = (size_t*)realloc(mObjects, newSize*sizeof(size_t));
843 if (objects == NULL) return NO_MEMORY;
844 mObjects = objects;
845 mObjectsCapacity = newSize;
846 }
847
848 goto restart_write;
849}
850
Brad Fitzpatrick837a0d02010-07-13 15:33:35 -0700851status_t Parcel::writeNoException()
852{
853 return writeInt32(0);
854}
855
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700856void Parcel::remove(size_t start, size_t amt)
857{
858 LOG_ALWAYS_FATAL("Parcel::remove() not yet implemented!");
859}
860
861status_t Parcel::read(void* outData, size_t len) const
862{
863 if ((mDataPos+PAD_SIZE(len)) >= mDataPos && (mDataPos+PAD_SIZE(len)) <= mDataSize) {
864 memcpy(outData, mData+mDataPos, len);
865 mDataPos += PAD_SIZE(len);
866 LOGV("read Setting data pos of %p to %d\n", this, mDataPos);
867 return NO_ERROR;
868 }
869 return NOT_ENOUGH_DATA;
870}
871
872const void* Parcel::readInplace(size_t len) const
873{
874 if ((mDataPos+PAD_SIZE(len)) >= mDataPos && (mDataPos+PAD_SIZE(len)) <= mDataSize) {
875 const void* data = mData+mDataPos;
876 mDataPos += PAD_SIZE(len);
877 LOGV("readInplace Setting data pos of %p to %d\n", this, mDataPos);
878 return data;
879 }
880 return NULL;
881}
882
Andreas Huber84a6d042009-08-17 13:33:27 -0700883template<class T>
884status_t Parcel::readAligned(T *pArg) const {
885 COMPILE_TIME_ASSERT_FUNCTION_SCOPE(PAD_SIZE(sizeof(T)) == sizeof(T));
886
887 if ((mDataPos+sizeof(T)) <= mDataSize) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700888 const void* data = mData+mDataPos;
Andreas Huber84a6d042009-08-17 13:33:27 -0700889 mDataPos += sizeof(T);
890 *pArg = *reinterpret_cast<const T*>(data);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700891 return NO_ERROR;
892 } else {
893 return NOT_ENOUGH_DATA;
894 }
895}
896
Andreas Huber84a6d042009-08-17 13:33:27 -0700897template<class T>
898T Parcel::readAligned() const {
899 T result;
900 if (readAligned(&result) != NO_ERROR) {
901 result = 0;
902 }
903
904 return result;
905}
906
907template<class T>
908status_t Parcel::writeAligned(T val) {
909 COMPILE_TIME_ASSERT_FUNCTION_SCOPE(PAD_SIZE(sizeof(T)) == sizeof(T));
910
911 if ((mDataPos+sizeof(val)) <= mDataCapacity) {
912restart_write:
913 *reinterpret_cast<T*>(mData+mDataPos) = val;
914 return finishWrite(sizeof(val));
915 }
916
917 status_t err = growData(sizeof(val));
918 if (err == NO_ERROR) goto restart_write;
919 return err;
920}
921
922status_t Parcel::readInt32(int32_t *pArg) const
923{
924 return readAligned(pArg);
925}
926
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700927int32_t Parcel::readInt32() const
928{
Andreas Huber84a6d042009-08-17 13:33:27 -0700929 return readAligned<int32_t>();
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700930}
931
932
933status_t Parcel::readInt64(int64_t *pArg) const
934{
Andreas Huber84a6d042009-08-17 13:33:27 -0700935 return readAligned(pArg);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700936}
937
938
939int64_t Parcel::readInt64() const
940{
Andreas Huber84a6d042009-08-17 13:33:27 -0700941 return readAligned<int64_t>();
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700942}
943
944status_t Parcel::readFloat(float *pArg) const
945{
Andreas Huber84a6d042009-08-17 13:33:27 -0700946 return readAligned(pArg);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700947}
948
949
950float Parcel::readFloat() const
951{
Andreas Huber84a6d042009-08-17 13:33:27 -0700952 return readAligned<float>();
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700953}
954
955status_t Parcel::readDouble(double *pArg) const
956{
Andreas Huber84a6d042009-08-17 13:33:27 -0700957 return readAligned(pArg);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700958}
959
960
961double Parcel::readDouble() const
962{
Andreas Huber84a6d042009-08-17 13:33:27 -0700963 return readAligned<double>();
964}
965
966status_t Parcel::readIntPtr(intptr_t *pArg) const
967{
968 return readAligned(pArg);
969}
970
971
972intptr_t Parcel::readIntPtr() const
973{
974 return readAligned<intptr_t>();
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700975}
976
977
978const char* Parcel::readCString() const
979{
980 const size_t avail = mDataSize-mDataPos;
981 if (avail > 0) {
982 const char* str = reinterpret_cast<const char*>(mData+mDataPos);
983 // is the string's trailing NUL within the parcel's valid bounds?
984 const char* eos = reinterpret_cast<const char*>(memchr(str, 0, avail));
985 if (eos) {
986 const size_t len = eos - str;
987 mDataPos += PAD_SIZE(len+1);
988 LOGV("readCString Setting data pos of %p to %d\n", this, mDataPos);
989 return str;
990 }
991 }
992 return NULL;
993}
994
995String8 Parcel::readString8() const
996{
997 int32_t size = readInt32();
998 // watch for potential int overflow adding 1 for trailing NUL
999 if (size > 0 && size < INT32_MAX) {
1000 const char* str = (const char*)readInplace(size+1);
1001 if (str) return String8(str, size);
1002 }
1003 return String8();
1004}
1005
1006String16 Parcel::readString16() const
1007{
1008 size_t len;
1009 const char16_t* str = readString16Inplace(&len);
1010 if (str) return String16(str, len);
1011 LOGE("Reading a NULL string not supported here.");
1012 return String16();
1013}
1014
1015const char16_t* Parcel::readString16Inplace(size_t* outLen) const
1016{
1017 int32_t size = readInt32();
1018 // watch for potential int overflow from size+1
1019 if (size >= 0 && size < INT32_MAX) {
1020 *outLen = size;
1021 const char16_t* str = (const char16_t*)readInplace((size+1)*sizeof(char16_t));
1022 if (str != NULL) {
1023 return str;
1024 }
1025 }
1026 *outLen = 0;
1027 return NULL;
1028}
1029
1030sp<IBinder> Parcel::readStrongBinder() const
1031{
1032 sp<IBinder> val;
1033 unflatten_binder(ProcessState::self(), *this, &val);
1034 return val;
1035}
1036
1037wp<IBinder> Parcel::readWeakBinder() const
1038{
1039 wp<IBinder> val;
1040 unflatten_binder(ProcessState::self(), *this, &val);
1041 return val;
1042}
1043
Brad Fitzpatrick837a0d02010-07-13 15:33:35 -07001044int32_t Parcel::readExceptionCode() const
1045{
1046 int32_t exception_code = readAligned<int32_t>();
Brad Fitzpatrickd36f4a52010-07-12 11:05:38 -07001047 if (exception_code == EX_HAS_REPLY_HEADER) {
1048 int32_t header_size = readAligned<int32_t>();
1049 // Skip over fat responses headers. Not used (or propagated) in
1050 // native code
1051 setDataPosition(dataPosition() + header_size);
1052 // And fat response headers are currently only used when there are no
1053 // exceptions, so return no error:
1054 return 0;
1055 }
Brad Fitzpatrick837a0d02010-07-13 15:33:35 -07001056 return exception_code;
1057}
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001058
Mathias Agopiana47f02a2009-05-21 16:29:38 -07001059native_handle* Parcel::readNativeHandle() const
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001060{
1061 int numFds, numInts;
1062 status_t err;
1063 err = readInt32(&numFds);
1064 if (err != NO_ERROR) return 0;
1065 err = readInt32(&numInts);
1066 if (err != NO_ERROR) return 0;
1067
Mathias Agopiana47f02a2009-05-21 16:29:38 -07001068 native_handle* h = native_handle_create(numFds, numInts);
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001069 for (int i=0 ; err==NO_ERROR && i<numFds ; i++) {
Rebecca Schultz Zavin360211f2009-02-13 16:34:38 -08001070 h->data[i] = dup(readFileDescriptor());
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001071 if (h->data[i] < 0) err = BAD_VALUE;
1072 }
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001073 err = read(h->data + numFds, sizeof(int)*numInts);
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001074 if (err != NO_ERROR) {
Mathias Agopiana47f02a2009-05-21 16:29:38 -07001075 native_handle_close(h);
1076 native_handle_delete(h);
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001077 h = 0;
1078 }
1079 return h;
1080}
1081
1082
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001083int Parcel::readFileDescriptor() const
1084{
1085 const flat_binder_object* flat = readObject(true);
1086 if (flat) {
1087 switch (flat->type) {
1088 case BINDER_TYPE_FD:
1089 //LOGI("Returning file descriptor %ld from parcel %p\n", flat->handle, this);
1090 return flat->handle;
1091 }
1092 }
1093 return BAD_TYPE;
1094}
1095
Jeff Brown5707dbf2011-09-23 21:17:56 -07001096status_t Parcel::readBlob(size_t len, ReadableBlob* outBlob) const
1097{
1098 int32_t useAshmem;
1099 status_t status = readInt32(&useAshmem);
1100 if (status) return status;
1101
1102 if (!useAshmem) {
1103 LOGV("readBlob: read in place");
1104 const void* ptr = readInplace(len);
1105 if (!ptr) return BAD_VALUE;
1106
1107 outBlob->init(false /*mapped*/, const_cast<void*>(ptr), len);
1108 return NO_ERROR;
1109 }
1110
1111 LOGV("readBlob: read from ashmem");
1112 int fd = readFileDescriptor();
1113 if (fd == int(BAD_TYPE)) return BAD_VALUE;
1114
1115 void* ptr = ::mmap(NULL, len, PROT_READ, MAP_SHARED, fd, 0);
1116 if (!ptr) return NO_MEMORY;
1117
1118 outBlob->init(true /*mapped*/, ptr, len);
1119 return NO_ERROR;
1120}
1121
Mathias Agopian98e71dd2010-02-11 17:30:52 -08001122status_t Parcel::read(Flattenable& val) const
1123{
1124 // size
1125 const size_t len = this->readInt32();
1126 const size_t fd_count = this->readInt32();
1127
1128 // payload
1129 void const* buf = this->readInplace(PAD_SIZE(len));
1130 if (buf == NULL)
1131 return BAD_VALUE;
1132
1133 int* fds = NULL;
1134 if (fd_count) {
1135 fds = new int[fd_count];
1136 }
1137
1138 status_t err = NO_ERROR;
1139 for (size_t i=0 ; i<fd_count && err==NO_ERROR ; i++) {
1140 fds[i] = dup(this->readFileDescriptor());
1141 if (fds[i] < 0) err = BAD_VALUE;
1142 }
1143
1144 if (err == NO_ERROR) {
1145 err = val.unflatten(buf, len, fds, fd_count);
1146 }
1147
1148 if (fd_count) {
1149 delete [] fds;
1150 }
1151
1152 return err;
1153}
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001154const flat_binder_object* Parcel::readObject(bool nullMetaData) const
1155{
1156 const size_t DPOS = mDataPos;
1157 if ((DPOS+sizeof(flat_binder_object)) <= mDataSize) {
1158 const flat_binder_object* obj
1159 = reinterpret_cast<const flat_binder_object*>(mData+DPOS);
1160 mDataPos = DPOS + sizeof(flat_binder_object);
1161 if (!nullMetaData && (obj->cookie == NULL && obj->binder == NULL)) {
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001162 // When transferring a NULL object, we don't write it into
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001163 // the object list, so we don't want to check for it when
1164 // reading.
1165 LOGV("readObject Setting data pos of %p to %d\n", this, mDataPos);
1166 return obj;
1167 }
1168
1169 // Ensure that this object is valid...
1170 size_t* const OBJS = mObjects;
1171 const size_t N = mObjectsSize;
1172 size_t opos = mNextObjectHint;
1173
1174 if (N > 0) {
1175 LOGV("Parcel %p looking for obj at %d, hint=%d\n",
1176 this, DPOS, opos);
1177
1178 // Start at the current hint position, looking for an object at
1179 // the current data position.
1180 if (opos < N) {
1181 while (opos < (N-1) && OBJS[opos] < DPOS) {
1182 opos++;
1183 }
1184 } else {
1185 opos = N-1;
1186 }
1187 if (OBJS[opos] == DPOS) {
1188 // Found it!
1189 LOGV("Parcel found obj %d at index %d with forward search",
1190 this, DPOS, opos);
1191 mNextObjectHint = opos+1;
1192 LOGV("readObject Setting data pos of %p to %d\n", this, mDataPos);
1193 return obj;
1194 }
1195
1196 // Look backwards for it...
1197 while (opos > 0 && OBJS[opos] > DPOS) {
1198 opos--;
1199 }
1200 if (OBJS[opos] == DPOS) {
1201 // Found it!
1202 LOGV("Parcel found obj %d at index %d with backward search",
1203 this, DPOS, opos);
1204 mNextObjectHint = opos+1;
1205 LOGV("readObject Setting data pos of %p to %d\n", this, mDataPos);
1206 return obj;
1207 }
1208 }
1209 LOGW("Attempt to read object from Parcel %p at offset %d that is not in the object list",
1210 this, DPOS);
1211 }
1212 return NULL;
1213}
1214
1215void Parcel::closeFileDescriptors()
1216{
1217 size_t i = mObjectsSize;
1218 if (i > 0) {
1219 //LOGI("Closing file descriptors for %d objects...", mObjectsSize);
1220 }
1221 while (i > 0) {
1222 i--;
1223 const flat_binder_object* flat
1224 = reinterpret_cast<flat_binder_object*>(mData+mObjects[i]);
1225 if (flat->type == BINDER_TYPE_FD) {
1226 //LOGI("Closing fd: %ld\n", flat->handle);
1227 close(flat->handle);
1228 }
1229 }
1230}
1231
1232const uint8_t* Parcel::ipcData() const
1233{
1234 return mData;
1235}
1236
1237size_t Parcel::ipcDataSize() const
1238{
1239 return (mDataSize > mDataPos ? mDataSize : mDataPos);
1240}
1241
1242const size_t* Parcel::ipcObjects() const
1243{
1244 return mObjects;
1245}
1246
1247size_t Parcel::ipcObjectsCount() const
1248{
1249 return mObjectsSize;
1250}
1251
1252void Parcel::ipcSetDataReference(const uint8_t* data, size_t dataSize,
1253 const size_t* objects, size_t objectsCount, release_func relFunc, void* relCookie)
1254{
1255 freeDataNoInit();
1256 mError = NO_ERROR;
1257 mData = const_cast<uint8_t*>(data);
1258 mDataSize = mDataCapacity = dataSize;
1259 //LOGI("setDataReference Setting data size of %p to %lu (pid=%d)\n", this, mDataSize, getpid());
1260 mDataPos = 0;
1261 LOGV("setDataReference Setting data pos of %p to %d\n", this, mDataPos);
1262 mObjects = const_cast<size_t*>(objects);
1263 mObjectsSize = mObjectsCapacity = objectsCount;
1264 mNextObjectHint = 0;
1265 mOwner = relFunc;
1266 mOwnerCookie = relCookie;
1267 scanForFds();
1268}
1269
1270void Parcel::print(TextOutput& to, uint32_t flags) const
1271{
1272 to << "Parcel(";
1273
1274 if (errorCheck() != NO_ERROR) {
1275 const status_t err = errorCheck();
1276 to << "Error: " << (void*)err << " \"" << strerror(-err) << "\"";
1277 } else if (dataSize() > 0) {
1278 const uint8_t* DATA = data();
1279 to << indent << HexDump(DATA, dataSize()) << dedent;
1280 const size_t* OBJS = objects();
1281 const size_t N = objectsCount();
1282 for (size_t i=0; i<N; i++) {
1283 const flat_binder_object* flat
1284 = reinterpret_cast<const flat_binder_object*>(DATA+OBJS[i]);
1285 to << endl << "Object #" << i << " @ " << (void*)OBJS[i] << ": "
1286 << TypeCode(flat->type & 0x7f7f7f00)
1287 << " = " << flat->binder;
1288 }
1289 } else {
1290 to << "NULL";
1291 }
1292
1293 to << ")";
1294}
1295
1296void Parcel::releaseObjects()
1297{
1298 const sp<ProcessState> proc(ProcessState::self());
1299 size_t i = mObjectsSize;
1300 uint8_t* const data = mData;
1301 size_t* const objects = mObjects;
1302 while (i > 0) {
1303 i--;
1304 const flat_binder_object* flat
1305 = reinterpret_cast<flat_binder_object*>(data+objects[i]);
1306 release_object(proc, *flat, this);
1307 }
1308}
1309
1310void Parcel::acquireObjects()
1311{
1312 const sp<ProcessState> proc(ProcessState::self());
1313 size_t i = mObjectsSize;
1314 uint8_t* const data = mData;
1315 size_t* const objects = mObjects;
1316 while (i > 0) {
1317 i--;
1318 const flat_binder_object* flat
1319 = reinterpret_cast<flat_binder_object*>(data+objects[i]);
1320 acquire_object(proc, *flat, this);
1321 }
1322}
1323
1324void Parcel::freeData()
1325{
1326 freeDataNoInit();
1327 initState();
1328}
1329
1330void Parcel::freeDataNoInit()
1331{
1332 if (mOwner) {
1333 //LOGI("Freeing data ref of %p (pid=%d)\n", this, getpid());
1334 mOwner(this, mData, mDataSize, mObjects, mObjectsSize, mOwnerCookie);
1335 } else {
1336 releaseObjects();
1337 if (mData) free(mData);
1338 if (mObjects) free(mObjects);
1339 }
1340}
1341
1342status_t Parcel::growData(size_t len)
1343{
1344 size_t newSize = ((mDataSize+len)*3)/2;
1345 return (newSize <= mDataSize)
1346 ? (status_t) NO_MEMORY
1347 : continueWrite(newSize);
1348}
1349
1350status_t Parcel::restartWrite(size_t desired)
1351{
1352 if (mOwner) {
1353 freeData();
1354 return continueWrite(desired);
1355 }
1356
1357 uint8_t* data = (uint8_t*)realloc(mData, desired);
1358 if (!data && desired > mDataCapacity) {
1359 mError = NO_MEMORY;
1360 return NO_MEMORY;
1361 }
1362
1363 releaseObjects();
1364
1365 if (data) {
1366 mData = data;
1367 mDataCapacity = desired;
1368 }
1369
1370 mDataSize = mDataPos = 0;
1371 LOGV("restartWrite Setting data size of %p to %d\n", this, mDataSize);
1372 LOGV("restartWrite Setting data pos of %p to %d\n", this, mDataPos);
1373
1374 free(mObjects);
1375 mObjects = NULL;
1376 mObjectsSize = mObjectsCapacity = 0;
1377 mNextObjectHint = 0;
1378 mHasFds = false;
1379 mFdsKnown = true;
Dianne Hackborn8938ed22011-09-28 23:19:47 -04001380 mAllowFds = true;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001381
1382 return NO_ERROR;
1383}
1384
1385status_t Parcel::continueWrite(size_t desired)
1386{
1387 // If shrinking, first adjust for any objects that appear
1388 // after the new data size.
1389 size_t objectsSize = mObjectsSize;
1390 if (desired < mDataSize) {
1391 if (desired == 0) {
1392 objectsSize = 0;
1393 } else {
1394 while (objectsSize > 0) {
1395 if (mObjects[objectsSize-1] < desired)
1396 break;
1397 objectsSize--;
1398 }
1399 }
1400 }
1401
1402 if (mOwner) {
1403 // If the size is going to zero, just release the owner's data.
1404 if (desired == 0) {
1405 freeData();
1406 return NO_ERROR;
1407 }
1408
1409 // If there is a different owner, we need to take
1410 // posession.
1411 uint8_t* data = (uint8_t*)malloc(desired);
1412 if (!data) {
1413 mError = NO_MEMORY;
1414 return NO_MEMORY;
1415 }
1416 size_t* objects = NULL;
1417
1418 if (objectsSize) {
1419 objects = (size_t*)malloc(objectsSize*sizeof(size_t));
1420 if (!objects) {
1421 mError = NO_MEMORY;
1422 return NO_MEMORY;
1423 }
1424
1425 // Little hack to only acquire references on objects
1426 // we will be keeping.
1427 size_t oldObjectsSize = mObjectsSize;
1428 mObjectsSize = objectsSize;
1429 acquireObjects();
1430 mObjectsSize = oldObjectsSize;
1431 }
1432
1433 if (mData) {
1434 memcpy(data, mData, mDataSize < desired ? mDataSize : desired);
1435 }
1436 if (objects && mObjects) {
1437 memcpy(objects, mObjects, objectsSize*sizeof(size_t));
1438 }
1439 //LOGI("Freeing data ref of %p (pid=%d)\n", this, getpid());
1440 mOwner(this, mData, mDataSize, mObjects, mObjectsSize, mOwnerCookie);
1441 mOwner = NULL;
1442
1443 mData = data;
1444 mObjects = objects;
1445 mDataSize = (mDataSize < desired) ? mDataSize : desired;
1446 LOGV("continueWrite Setting data size of %p to %d\n", this, mDataSize);
1447 mDataCapacity = desired;
1448 mObjectsSize = mObjectsCapacity = objectsSize;
1449 mNextObjectHint = 0;
1450
1451 } else if (mData) {
1452 if (objectsSize < mObjectsSize) {
1453 // Need to release refs on any objects we are dropping.
1454 const sp<ProcessState> proc(ProcessState::self());
1455 for (size_t i=objectsSize; i<mObjectsSize; i++) {
1456 const flat_binder_object* flat
1457 = reinterpret_cast<flat_binder_object*>(mData+mObjects[i]);
1458 if (flat->type == BINDER_TYPE_FD) {
1459 // will need to rescan because we may have lopped off the only FDs
1460 mFdsKnown = false;
1461 }
1462 release_object(proc, *flat, this);
1463 }
1464 size_t* objects =
1465 (size_t*)realloc(mObjects, objectsSize*sizeof(size_t));
1466 if (objects) {
1467 mObjects = objects;
1468 }
1469 mObjectsSize = objectsSize;
1470 mNextObjectHint = 0;
1471 }
1472
1473 // We own the data, so we can just do a realloc().
1474 if (desired > mDataCapacity) {
1475 uint8_t* data = (uint8_t*)realloc(mData, desired);
1476 if (data) {
1477 mData = data;
1478 mDataCapacity = desired;
1479 } else if (desired > mDataCapacity) {
1480 mError = NO_MEMORY;
1481 return NO_MEMORY;
1482 }
1483 } else {
Dianne Hackborn97e2bcd2011-04-13 18:15:56 -07001484 if (mDataSize > desired) {
1485 mDataSize = desired;
1486 LOGV("continueWrite Setting data size of %p to %d\n", this, mDataSize);
1487 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001488 if (mDataPos > desired) {
1489 mDataPos = desired;
1490 LOGV("continueWrite Setting data pos of %p to %d\n", this, mDataPos);
1491 }
1492 }
1493
1494 } else {
1495 // This is the first data. Easy!
1496 uint8_t* data = (uint8_t*)malloc(desired);
1497 if (!data) {
1498 mError = NO_MEMORY;
1499 return NO_MEMORY;
1500 }
1501
1502 if(!(mDataCapacity == 0 && mObjects == NULL
1503 && mObjectsCapacity == 0)) {
1504 LOGE("continueWrite: %d/%p/%d/%d", mDataCapacity, mObjects, mObjectsCapacity, desired);
1505 }
1506
1507 mData = data;
1508 mDataSize = mDataPos = 0;
1509 LOGV("continueWrite Setting data size of %p to %d\n", this, mDataSize);
1510 LOGV("continueWrite Setting data pos of %p to %d\n", this, mDataPos);
1511 mDataCapacity = desired;
1512 }
1513
1514 return NO_ERROR;
1515}
1516
1517void Parcel::initState()
1518{
1519 mError = NO_ERROR;
1520 mData = 0;
1521 mDataSize = 0;
1522 mDataCapacity = 0;
1523 mDataPos = 0;
1524 LOGV("initState Setting data size of %p to %d\n", this, mDataSize);
1525 LOGV("initState Setting data pos of %p to %d\n", this, mDataPos);
1526 mObjects = NULL;
1527 mObjectsSize = 0;
1528 mObjectsCapacity = 0;
1529 mNextObjectHint = 0;
1530 mHasFds = false;
1531 mFdsKnown = true;
Dianne Hackborn8938ed22011-09-28 23:19:47 -04001532 mAllowFds = true;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001533 mOwner = NULL;
1534}
1535
1536void Parcel::scanForFds() const
1537{
1538 bool hasFds = false;
1539 for (size_t i=0; i<mObjectsSize; i++) {
1540 const flat_binder_object* flat
1541 = reinterpret_cast<const flat_binder_object*>(mData + mObjects[i]);
1542 if (flat->type == BINDER_TYPE_FD) {
1543 hasFds = true;
1544 break;
1545 }
1546 }
1547 mHasFds = hasFds;
1548 mFdsKnown = true;
1549}
1550
Jeff Brown5707dbf2011-09-23 21:17:56 -07001551// --- Parcel::Blob ---
1552
1553Parcel::Blob::Blob() :
1554 mMapped(false), mData(NULL), mSize(0) {
1555}
1556
1557Parcel::Blob::~Blob() {
1558 release();
1559}
1560
1561void Parcel::Blob::release() {
1562 if (mMapped && mData) {
1563 ::munmap(mData, mSize);
1564 }
1565 clear();
1566}
1567
1568void Parcel::Blob::init(bool mapped, void* data, size_t size) {
1569 mMapped = mapped;
1570 mData = data;
1571 mSize = size;
1572}
1573
1574void Parcel::Blob::clear() {
1575 mMapped = false;
1576 mData = NULL;
1577 mSize = 0;
1578}
1579
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001580}; // namespace android