blob: c7180cee03cd39022947dba9e2ac8fa4e979b384 [file] [log] [blame]
The Android Open Source Project54b6cfa2008-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 Agopian07952722009-05-19 19:08:10 -070020#include <binder/Parcel.h>
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -070021
Brad Fitzpatrick27b3a7a2010-06-18 13:07:53 -070022#include <binder/IPCThreadState.h>
Mathias Agopian07952722009-05-19 19:08:10 -070023#include <binder/Binder.h>
24#include <binder/BpBinder.h>
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -070025#include <utils/Debug.h>
Mathias Agopian07952722009-05-19 19:08:10 -070026#include <binder/ProcessState.h>
The Android Open Source Project54b6cfa2008-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 Agopianc86727f2010-02-11 17:30:52 -080032#include <utils/Flattenable.h>
Jeff Brown6579a9d2011-09-23 21:17:56 -070033#include <cutils/ashmem.h>
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -070034
Mathias Agopian25ba5b62009-05-18 15:08:03 -070035#include <private/binder/binder_module.h>
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -070036
37#include <stdio.h>
38#include <stdlib.h>
39#include <stdint.h>
Jeff Brown6579a9d2011-09-23 21:17:56 -070040#include <sys/mman.h>
The Android Open Source Project54b6cfa2008-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 Fitzpatrick727de402010-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 Fitzpatrick5b747192010-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 Brown6579a9d2011-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 Project54b6cfa2008-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 Hackborn0ffc9882011-04-13 18:15:56 -0700346 if (size > mDataCapacity) return continueWrite(size);
The Android Open Source Project54b6cfa2008-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 Huber0a47f4f2011-04-13 10:21:56 -0700361status_t Parcel::appendFrom(const Parcel *parcel, size_t offset, size_t len)
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700362{
363 const sp<ProcessState> proc(ProcessState::self());
364 status_t err;
Andreas Huber0a47f4f2011-04-13 10:21:56 -0700365 const uint8_t *data = parcel->mData;
366 const size_t *objects = parcel->mObjects;
The Android Open Source Project54b6cfa2008-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 Hackborn0ffc9882011-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 Project54b6cfa2008-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 Hackborn9ecebbf2011-09-28 23:19:47 -0400407 err = NO_ERROR;
408
The Android Open Source Project54b6cfa2008-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 Hackborn6aff9052009-05-22 13:20:23 -0700429 flat_binder_object* flat
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700430 = reinterpret_cast<flat_binder_object*>(mData + off);
431 acquire_object(proc, *flat, this);
432
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700433 if (flat->type == BINDER_TYPE_FD) {
Dianne Hackborn6aff9052009-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 Project54b6cfa2008-10-21 07:00:00 -0700439 mHasFds = mFdsKnown = true;
Dianne Hackborn9ecebbf2011-09-28 23:19:47 -0400440 if (!mAllowFds) {
441 err = FDS_NOT_ALLOWED;
442 }
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700443 }
444 }
445 }
446
Dianne Hackborn9ecebbf2011-09-28 23:19:47 -0400447 return err;
448}
449
Dianne Hackbornc04db7e2011-10-03 21:09:35 -0700450bool Parcel::pushAllowFds(bool allowFds)
Dianne Hackborn9ecebbf2011-09-28 23:19:47 -0400451{
452 const bool origValue = mAllowFds;
Dianne Hackbornc04db7e2011-10-03 21:09:35 -0700453 if (!allowFds) {
454 mAllowFds = false;
455 }
Dianne Hackborn9ecebbf2011-09-28 23:19:47 -0400456 return origValue;
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700457}
458
Dianne Hackbornc04db7e2011-10-03 21:09:35 -0700459void Parcel::restoreAllowFds(bool lastValue)
460{
461 mAllowFds = lastValue;
462}
463
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700464bool Parcel::hasFileDescriptors() const
465{
466 if (!mFdsKnown) {
467 scanForFds();
468 }
469 return mHasFds;
470}
471
Brad Fitzpatrick27b3a7a2010-06-18 13:07:53 -0700472// Write RPC headers. (previously just the interface token)
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700473status_t Parcel::writeInterfaceToken(const String16& interface)
474{
Brad Fitzpatrick727de402010-07-07 16:06:39 -0700475 writeInt32(IPCThreadState::self()->getStrictModePolicy() |
476 STRICT_MODE_PENALTY_GATHER);
The Android Open Source Project54b6cfa2008-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 Agopianaaf834a2009-05-22 19:00:22 -0700481bool Parcel::checkInterface(IBinder* binder) const
482{
Brad Fitzpatrick27b3a7a2010-06-18 13:07:53 -0700483 return enforceInterface(binder->getInterfaceDescriptor());
Mathias Agopianaaf834a2009-05-22 19:00:22 -0700484}
485
Brad Fitzpatrick727de402010-07-07 16:06:39 -0700486bool Parcel::enforceInterface(const String16& interface,
Brad Fitzpatrick7bcad8a32010-07-27 09:49:11 -0700487 IPCThreadState* threadState) const
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700488{
Brad Fitzpatrick7bcad8a32010-07-27 09:49:11 -0700489 int32_t strictPolicy = readInt32();
490 if (threadState == NULL) {
491 threadState = IPCThreadState::self();
Brad Fitzpatrick727de402010-07-07 16:06:39 -0700492 }
Brad Fitzpatrick02343762010-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 Agopianaaf834a2009-05-22 19:00:22 -0700503 const String16 str(readString16());
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700504 if (str == interface) {
505 return true;
506 } else {
507 LOGW("**** enforceInterface() expected '%s' but read '%s'\n",
508 String8(interface).string(), String8(str).string());
509 return false;
510 }
Brad Fitzpatrick27b3a7a2010-06-18 13:07:53 -0700511}
The Android Open Source Project54b6cfa2008-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;
537 LOGV("finishWrite Setting data pos of %p to %d\n", this, mDataPos);
538 if (mDataPos > mDataSize) {
539 mDataSize = mDataPos;
540 LOGV("finishWrite Setting data size of %p to %d\n", this, mDataSize);
541 }
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 Huber2f10ae02009-08-17 13:33:27 -0700617 return writeAligned(val);
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700618}
619
620status_t Parcel::writeInt64(int64_t val)
621{
Andreas Huber2f10ae02009-08-17 13:33:27 -0700622 return writeAligned(val);
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700623}
624
625status_t Parcel::writeFloat(float val)
626{
Andreas Huber2f10ae02009-08-17 13:33:27 -0700627 return writeAligned(val);
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700628}
629
630status_t Parcel::writeDouble(double val)
631{
Andreas Huber2f10ae02009-08-17 13:33:27 -0700632 return writeAligned(val);
633}
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700634
Andreas Huber2f10ae02009-08-17 13:33:27 -0700635status_t Parcel::writeIntPtr(intptr_t val)
636{
637 return writeAligned(val);
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700638}
639
640status_t Parcel::writeCString(const char* str)
641{
642 return write(str, strlen(str)+1);
643}
644
645status_t Parcel::writeString8(const String8& str)
646{
647 status_t err = writeInt32(str.bytes());
Pravat Dalbeherab803c042010-12-15 08:40:00 +0100648 // only write string if its length is more than zero characters,
649 // as readString8 will only read if the length field is non-zero.
650 // this is slightly different from how writeString16 works.
651 if (str.bytes() > 0 && err == NO_ERROR) {
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700652 err = write(str.string(), str.bytes()+1);
653 }
654 return err;
655}
656
657status_t Parcel::writeString16(const String16& str)
658{
659 return writeString16(str.string(), str.size());
660}
661
662status_t Parcel::writeString16(const char16_t* str, size_t len)
663{
664 if (str == NULL) return writeInt32(-1);
665
666 status_t err = writeInt32(len);
667 if (err == NO_ERROR) {
668 len *= sizeof(char16_t);
669 uint8_t* data = (uint8_t*)writeInplace(len+sizeof(char16_t));
670 if (data) {
671 memcpy(data, str, len);
672 *reinterpret_cast<char16_t*>(data+len) = 0;
673 return NO_ERROR;
674 }
675 err = mError;
676 }
677 return err;
678}
679
680status_t Parcel::writeStrongBinder(const sp<IBinder>& val)
681{
682 return flatten_binder(ProcessState::self(), val, this);
683}
684
685status_t Parcel::writeWeakBinder(const wp<IBinder>& val)
686{
687 return flatten_binder(ProcessState::self(), val, this);
688}
689
Mathias Agopiandfece802009-05-21 16:29:38 -0700690status_t Parcel::writeNativeHandle(const native_handle* handle)
The Android Open Source Project22f7dfd2009-01-20 14:03:58 -0800691{
Mathias Agopianf1db4ae2009-07-31 16:12:13 -0700692 if (!handle || handle->version != sizeof(native_handle))
The Android Open Source Project22f7dfd2009-01-20 14:03:58 -0800693 return BAD_TYPE;
694
695 status_t err;
Mathias Agopiandfece802009-05-21 16:29:38 -0700696 err = writeInt32(handle->numFds);
The Android Open Source Project22f7dfd2009-01-20 14:03:58 -0800697 if (err != NO_ERROR) return err;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800698
Mathias Agopiandfece802009-05-21 16:29:38 -0700699 err = writeInt32(handle->numInts);
The Android Open Source Project22f7dfd2009-01-20 14:03:58 -0800700 if (err != NO_ERROR) return err;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800701
Mathias Agopiandfece802009-05-21 16:29:38 -0700702 for (int i=0 ; err==NO_ERROR && i<handle->numFds ; i++)
703 err = writeDupFileDescriptor(handle->data[i]);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800704
705 if (err != NO_ERROR) {
706 LOGD("write native handle, write dup fd failed");
707 return err;
708 }
Mathias Agopiandfece802009-05-21 16:29:38 -0700709 err = write(handle->data + handle->numFds, sizeof(int)*handle->numInts);
The Android Open Source Project22f7dfd2009-01-20 14:03:58 -0800710 return err;
711}
712
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700713status_t Parcel::writeFileDescriptor(int fd)
714{
715 flat_binder_object obj;
716 obj.type = BINDER_TYPE_FD;
717 obj.flags = 0x7f | FLAT_BINDER_FLAG_ACCEPTS_FDS;
718 obj.handle = fd;
719 obj.cookie = (void*)0;
720 return writeObject(obj, true);
721}
722
723status_t Parcel::writeDupFileDescriptor(int fd)
724{
725 flat_binder_object obj;
726 obj.type = BINDER_TYPE_FD;
727 obj.flags = 0x7f | FLAT_BINDER_FLAG_ACCEPTS_FDS;
728 obj.handle = dup(fd);
729 obj.cookie = (void*)1;
730 return writeObject(obj, true);
731}
732
Jeff Brown6579a9d2011-09-23 21:17:56 -0700733status_t Parcel::writeBlob(size_t len, WritableBlob* outBlob)
734{
735 status_t status;
736
737 if (!mAllowFds || len <= IN_PLACE_BLOB_LIMIT) {
738 LOGV("writeBlob: write in place");
739 status = writeInt32(0);
740 if (status) return status;
741
742 void* ptr = writeInplace(len);
743 if (!ptr) return NO_MEMORY;
744
745 outBlob->init(false /*mapped*/, ptr, len);
746 return NO_ERROR;
747 }
748
749 LOGV("writeBlob: write to ashmem");
750 int fd = ashmem_create_region("Parcel Blob", len);
751 if (fd < 0) return NO_MEMORY;
752
753 int result = ashmem_set_prot_region(fd, PROT_READ | PROT_WRITE);
754 if (result < 0) {
Jeff Brown0cde89f2011-10-10 14:50:10 -0700755 status = result;
Jeff Brown6579a9d2011-09-23 21:17:56 -0700756 } else {
757 void* ptr = ::mmap(NULL, len, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
758 if (ptr == MAP_FAILED) {
759 status = -errno;
760 } else {
761 result = ashmem_set_prot_region(fd, PROT_READ);
762 if (result < 0) {
Jeff Brown0cde89f2011-10-10 14:50:10 -0700763 status = result;
Jeff Brown6579a9d2011-09-23 21:17:56 -0700764 } else {
765 status = writeInt32(1);
766 if (!status) {
767 status = writeFileDescriptor(fd);
768 if (!status) {
769 outBlob->init(true /*mapped*/, ptr, len);
770 return NO_ERROR;
771 }
772 }
773 }
774 }
775 ::munmap(ptr, len);
776 }
777 ::close(fd);
778 return status;
779}
780
Mathias Agopianc86727f2010-02-11 17:30:52 -0800781status_t Parcel::write(const Flattenable& val)
782{
783 status_t err;
784
785 // size if needed
786 size_t len = val.getFlattenedSize();
787 size_t fd_count = val.getFdCount();
788
789 err = this->writeInt32(len);
790 if (err) return err;
791
792 err = this->writeInt32(fd_count);
793 if (err) return err;
794
795 // payload
796 void* buf = this->writeInplace(PAD_SIZE(len));
797 if (buf == NULL)
798 return BAD_VALUE;
799
800 int* fds = NULL;
801 if (fd_count) {
802 fds = new int[fd_count];
803 }
804
805 err = val.flatten(buf, len, fds, fd_count);
806 for (size_t i=0 ; i<fd_count && err==NO_ERROR ; i++) {
807 err = this->writeDupFileDescriptor( fds[i] );
808 }
809
810 if (fd_count) {
811 delete [] fds;
812 }
813
814 return err;
815}
816
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700817status_t Parcel::writeObject(const flat_binder_object& val, bool nullMetaData)
818{
819 const bool enoughData = (mDataPos+sizeof(val)) <= mDataCapacity;
820 const bool enoughObjects = mObjectsSize < mObjectsCapacity;
821 if (enoughData && enoughObjects) {
822restart_write:
823 *reinterpret_cast<flat_binder_object*>(mData+mDataPos) = val;
824
825 // Need to write meta-data?
826 if (nullMetaData || val.binder != NULL) {
827 mObjects[mObjectsSize] = mDataPos;
828 acquire_object(ProcessState::self(), val, this);
829 mObjectsSize++;
830 }
831
832 // remember if it's a file descriptor
833 if (val.type == BINDER_TYPE_FD) {
Dianne Hackborn9ecebbf2011-09-28 23:19:47 -0400834 if (!mAllowFds) {
835 return FDS_NOT_ALLOWED;
836 }
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700837 mHasFds = mFdsKnown = true;
838 }
839
840 return finishWrite(sizeof(flat_binder_object));
841 }
842
843 if (!enoughData) {
844 const status_t err = growData(sizeof(val));
845 if (err != NO_ERROR) return err;
846 }
847 if (!enoughObjects) {
848 size_t newSize = ((mObjectsSize+2)*3)/2;
849 size_t* objects = (size_t*)realloc(mObjects, newSize*sizeof(size_t));
850 if (objects == NULL) return NO_MEMORY;
851 mObjects = objects;
852 mObjectsCapacity = newSize;
853 }
854
855 goto restart_write;
856}
857
Brad Fitzpatrickc0a7e692010-07-13 15:33:35 -0700858status_t Parcel::writeNoException()
859{
860 return writeInt32(0);
861}
862
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700863void Parcel::remove(size_t start, size_t amt)
864{
865 LOG_ALWAYS_FATAL("Parcel::remove() not yet implemented!");
866}
867
868status_t Parcel::read(void* outData, size_t len) const
869{
870 if ((mDataPos+PAD_SIZE(len)) >= mDataPos && (mDataPos+PAD_SIZE(len)) <= mDataSize) {
871 memcpy(outData, mData+mDataPos, len);
872 mDataPos += PAD_SIZE(len);
873 LOGV("read Setting data pos of %p to %d\n", this, mDataPos);
874 return NO_ERROR;
875 }
876 return NOT_ENOUGH_DATA;
877}
878
879const void* Parcel::readInplace(size_t len) const
880{
881 if ((mDataPos+PAD_SIZE(len)) >= mDataPos && (mDataPos+PAD_SIZE(len)) <= mDataSize) {
882 const void* data = mData+mDataPos;
883 mDataPos += PAD_SIZE(len);
884 LOGV("readInplace Setting data pos of %p to %d\n", this, mDataPos);
885 return data;
886 }
887 return NULL;
888}
889
Andreas Huber2f10ae02009-08-17 13:33:27 -0700890template<class T>
891status_t Parcel::readAligned(T *pArg) const {
892 COMPILE_TIME_ASSERT_FUNCTION_SCOPE(PAD_SIZE(sizeof(T)) == sizeof(T));
893
894 if ((mDataPos+sizeof(T)) <= mDataSize) {
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700895 const void* data = mData+mDataPos;
Andreas Huber2f10ae02009-08-17 13:33:27 -0700896 mDataPos += sizeof(T);
897 *pArg = *reinterpret_cast<const T*>(data);
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700898 return NO_ERROR;
899 } else {
900 return NOT_ENOUGH_DATA;
901 }
902}
903
Andreas Huber2f10ae02009-08-17 13:33:27 -0700904template<class T>
905T Parcel::readAligned() const {
906 T result;
907 if (readAligned(&result) != NO_ERROR) {
908 result = 0;
909 }
910
911 return result;
912}
913
914template<class T>
915status_t Parcel::writeAligned(T val) {
916 COMPILE_TIME_ASSERT_FUNCTION_SCOPE(PAD_SIZE(sizeof(T)) == sizeof(T));
917
918 if ((mDataPos+sizeof(val)) <= mDataCapacity) {
919restart_write:
920 *reinterpret_cast<T*>(mData+mDataPos) = val;
921 return finishWrite(sizeof(val));
922 }
923
924 status_t err = growData(sizeof(val));
925 if (err == NO_ERROR) goto restart_write;
926 return err;
927}
928
929status_t Parcel::readInt32(int32_t *pArg) const
930{
931 return readAligned(pArg);
932}
933
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700934int32_t Parcel::readInt32() const
935{
Andreas Huber2f10ae02009-08-17 13:33:27 -0700936 return readAligned<int32_t>();
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700937}
938
939
940status_t Parcel::readInt64(int64_t *pArg) const
941{
Andreas Huber2f10ae02009-08-17 13:33:27 -0700942 return readAligned(pArg);
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700943}
944
945
946int64_t Parcel::readInt64() const
947{
Andreas Huber2f10ae02009-08-17 13:33:27 -0700948 return readAligned<int64_t>();
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700949}
950
951status_t Parcel::readFloat(float *pArg) const
952{
Andreas Huber2f10ae02009-08-17 13:33:27 -0700953 return readAligned(pArg);
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700954}
955
956
957float Parcel::readFloat() const
958{
Andreas Huber2f10ae02009-08-17 13:33:27 -0700959 return readAligned<float>();
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700960}
961
962status_t Parcel::readDouble(double *pArg) const
963{
Andreas Huber2f10ae02009-08-17 13:33:27 -0700964 return readAligned(pArg);
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700965}
966
967
968double Parcel::readDouble() const
969{
Andreas Huber2f10ae02009-08-17 13:33:27 -0700970 return readAligned<double>();
971}
972
973status_t Parcel::readIntPtr(intptr_t *pArg) const
974{
975 return readAligned(pArg);
976}
977
978
979intptr_t Parcel::readIntPtr() const
980{
981 return readAligned<intptr_t>();
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700982}
983
984
985const char* Parcel::readCString() const
986{
987 const size_t avail = mDataSize-mDataPos;
988 if (avail > 0) {
989 const char* str = reinterpret_cast<const char*>(mData+mDataPos);
990 // is the string's trailing NUL within the parcel's valid bounds?
991 const char* eos = reinterpret_cast<const char*>(memchr(str, 0, avail));
992 if (eos) {
993 const size_t len = eos - str;
994 mDataPos += PAD_SIZE(len+1);
995 LOGV("readCString Setting data pos of %p to %d\n", this, mDataPos);
996 return str;
997 }
998 }
999 return NULL;
1000}
1001
1002String8 Parcel::readString8() const
1003{
1004 int32_t size = readInt32();
1005 // watch for potential int overflow adding 1 for trailing NUL
1006 if (size > 0 && size < INT32_MAX) {
1007 const char* str = (const char*)readInplace(size+1);
1008 if (str) return String8(str, size);
1009 }
1010 return String8();
1011}
1012
1013String16 Parcel::readString16() const
1014{
1015 size_t len;
1016 const char16_t* str = readString16Inplace(&len);
1017 if (str) return String16(str, len);
1018 LOGE("Reading a NULL string not supported here.");
1019 return String16();
1020}
1021
1022const char16_t* Parcel::readString16Inplace(size_t* outLen) const
1023{
1024 int32_t size = readInt32();
1025 // watch for potential int overflow from size+1
1026 if (size >= 0 && size < INT32_MAX) {
1027 *outLen = size;
1028 const char16_t* str = (const char16_t*)readInplace((size+1)*sizeof(char16_t));
1029 if (str != NULL) {
1030 return str;
1031 }
1032 }
1033 *outLen = 0;
1034 return NULL;
1035}
1036
1037sp<IBinder> Parcel::readStrongBinder() const
1038{
1039 sp<IBinder> val;
1040 unflatten_binder(ProcessState::self(), *this, &val);
1041 return val;
1042}
1043
1044wp<IBinder> Parcel::readWeakBinder() const
1045{
1046 wp<IBinder> val;
1047 unflatten_binder(ProcessState::self(), *this, &val);
1048 return val;
1049}
1050
Brad Fitzpatrickc0a7e692010-07-13 15:33:35 -07001051int32_t Parcel::readExceptionCode() const
1052{
1053 int32_t exception_code = readAligned<int32_t>();
Brad Fitzpatrick5b747192010-07-12 11:05:38 -07001054 if (exception_code == EX_HAS_REPLY_HEADER) {
1055 int32_t header_size = readAligned<int32_t>();
1056 // Skip over fat responses headers. Not used (or propagated) in
1057 // native code
1058 setDataPosition(dataPosition() + header_size);
1059 // And fat response headers are currently only used when there are no
1060 // exceptions, so return no error:
1061 return 0;
1062 }
Brad Fitzpatrickc0a7e692010-07-13 15:33:35 -07001063 return exception_code;
1064}
The Android Open Source Project22f7dfd2009-01-20 14:03:58 -08001065
Mathias Agopiandfece802009-05-21 16:29:38 -07001066native_handle* Parcel::readNativeHandle() const
The Android Open Source Project22f7dfd2009-01-20 14:03:58 -08001067{
1068 int numFds, numInts;
1069 status_t err;
1070 err = readInt32(&numFds);
1071 if (err != NO_ERROR) return 0;
1072 err = readInt32(&numInts);
1073 if (err != NO_ERROR) return 0;
1074
Mathias Agopiandfece802009-05-21 16:29:38 -07001075 native_handle* h = native_handle_create(numFds, numInts);
The Android Open Source Project22f7dfd2009-01-20 14:03:58 -08001076 for (int i=0 ; err==NO_ERROR && i<numFds ; i++) {
Rebecca Schultz Zavinbc4afde2009-02-13 16:34:38 -08001077 h->data[i] = dup(readFileDescriptor());
The Android Open Source Project22f7dfd2009-01-20 14:03:58 -08001078 if (h->data[i] < 0) err = BAD_VALUE;
1079 }
The Android Open Source Project22f7dfd2009-01-20 14:03:58 -08001080 err = read(h->data + numFds, sizeof(int)*numInts);
The Android Open Source Project22f7dfd2009-01-20 14:03:58 -08001081 if (err != NO_ERROR) {
Mathias Agopiandfece802009-05-21 16:29:38 -07001082 native_handle_close(h);
1083 native_handle_delete(h);
The Android Open Source Project22f7dfd2009-01-20 14:03:58 -08001084 h = 0;
1085 }
1086 return h;
1087}
1088
1089
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -07001090int Parcel::readFileDescriptor() const
1091{
1092 const flat_binder_object* flat = readObject(true);
1093 if (flat) {
1094 switch (flat->type) {
1095 case BINDER_TYPE_FD:
1096 //LOGI("Returning file descriptor %ld from parcel %p\n", flat->handle, this);
1097 return flat->handle;
1098 }
1099 }
1100 return BAD_TYPE;
1101}
1102
Jeff Brown6579a9d2011-09-23 21:17:56 -07001103status_t Parcel::readBlob(size_t len, ReadableBlob* outBlob) const
1104{
1105 int32_t useAshmem;
1106 status_t status = readInt32(&useAshmem);
1107 if (status) return status;
1108
1109 if (!useAshmem) {
1110 LOGV("readBlob: read in place");
1111 const void* ptr = readInplace(len);
1112 if (!ptr) return BAD_VALUE;
1113
1114 outBlob->init(false /*mapped*/, const_cast<void*>(ptr), len);
1115 return NO_ERROR;
1116 }
1117
1118 LOGV("readBlob: read from ashmem");
1119 int fd = readFileDescriptor();
1120 if (fd == int(BAD_TYPE)) return BAD_VALUE;
1121
1122 void* ptr = ::mmap(NULL, len, PROT_READ, MAP_SHARED, fd, 0);
1123 if (!ptr) return NO_MEMORY;
1124
1125 outBlob->init(true /*mapped*/, ptr, len);
1126 return NO_ERROR;
1127}
1128
Mathias Agopianc86727f2010-02-11 17:30:52 -08001129status_t Parcel::read(Flattenable& val) const
1130{
1131 // size
1132 const size_t len = this->readInt32();
1133 const size_t fd_count = this->readInt32();
1134
1135 // payload
1136 void const* buf = this->readInplace(PAD_SIZE(len));
1137 if (buf == NULL)
1138 return BAD_VALUE;
1139
1140 int* fds = NULL;
1141 if (fd_count) {
1142 fds = new int[fd_count];
1143 }
1144
1145 status_t err = NO_ERROR;
1146 for (size_t i=0 ; i<fd_count && err==NO_ERROR ; i++) {
1147 fds[i] = dup(this->readFileDescriptor());
1148 if (fds[i] < 0) err = BAD_VALUE;
1149 }
1150
1151 if (err == NO_ERROR) {
1152 err = val.unflatten(buf, len, fds, fd_count);
1153 }
1154
1155 if (fd_count) {
1156 delete [] fds;
1157 }
1158
1159 return err;
1160}
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -07001161const flat_binder_object* Parcel::readObject(bool nullMetaData) const
1162{
1163 const size_t DPOS = mDataPos;
1164 if ((DPOS+sizeof(flat_binder_object)) <= mDataSize) {
1165 const flat_binder_object* obj
1166 = reinterpret_cast<const flat_binder_object*>(mData+DPOS);
1167 mDataPos = DPOS + sizeof(flat_binder_object);
1168 if (!nullMetaData && (obj->cookie == NULL && obj->binder == NULL)) {
The Android Open Source Project22f7dfd2009-01-20 14:03:58 -08001169 // When transferring a NULL object, we don't write it into
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -07001170 // the object list, so we don't want to check for it when
1171 // reading.
1172 LOGV("readObject Setting data pos of %p to %d\n", this, mDataPos);
1173 return obj;
1174 }
1175
1176 // Ensure that this object is valid...
1177 size_t* const OBJS = mObjects;
1178 const size_t N = mObjectsSize;
1179 size_t opos = mNextObjectHint;
1180
1181 if (N > 0) {
1182 LOGV("Parcel %p looking for obj at %d, hint=%d\n",
1183 this, DPOS, opos);
1184
1185 // Start at the current hint position, looking for an object at
1186 // the current data position.
1187 if (opos < N) {
1188 while (opos < (N-1) && OBJS[opos] < DPOS) {
1189 opos++;
1190 }
1191 } else {
1192 opos = N-1;
1193 }
1194 if (OBJS[opos] == DPOS) {
1195 // Found it!
1196 LOGV("Parcel found obj %d at index %d with forward search",
1197 this, DPOS, opos);
1198 mNextObjectHint = opos+1;
1199 LOGV("readObject Setting data pos of %p to %d\n", this, mDataPos);
1200 return obj;
1201 }
1202
1203 // Look backwards for it...
1204 while (opos > 0 && OBJS[opos] > DPOS) {
1205 opos--;
1206 }
1207 if (OBJS[opos] == DPOS) {
1208 // Found it!
1209 LOGV("Parcel found obj %d at index %d with backward search",
1210 this, DPOS, opos);
1211 mNextObjectHint = opos+1;
1212 LOGV("readObject Setting data pos of %p to %d\n", this, mDataPos);
1213 return obj;
1214 }
1215 }
1216 LOGW("Attempt to read object from Parcel %p at offset %d that is not in the object list",
1217 this, DPOS);
1218 }
1219 return NULL;
1220}
1221
1222void Parcel::closeFileDescriptors()
1223{
1224 size_t i = mObjectsSize;
1225 if (i > 0) {
1226 //LOGI("Closing file descriptors for %d objects...", mObjectsSize);
1227 }
1228 while (i > 0) {
1229 i--;
1230 const flat_binder_object* flat
1231 = reinterpret_cast<flat_binder_object*>(mData+mObjects[i]);
1232 if (flat->type == BINDER_TYPE_FD) {
1233 //LOGI("Closing fd: %ld\n", flat->handle);
1234 close(flat->handle);
1235 }
1236 }
1237}
1238
1239const uint8_t* Parcel::ipcData() const
1240{
1241 return mData;
1242}
1243
1244size_t Parcel::ipcDataSize() const
1245{
1246 return (mDataSize > mDataPos ? mDataSize : mDataPos);
1247}
1248
1249const size_t* Parcel::ipcObjects() const
1250{
1251 return mObjects;
1252}
1253
1254size_t Parcel::ipcObjectsCount() const
1255{
1256 return mObjectsSize;
1257}
1258
1259void Parcel::ipcSetDataReference(const uint8_t* data, size_t dataSize,
1260 const size_t* objects, size_t objectsCount, release_func relFunc, void* relCookie)
1261{
1262 freeDataNoInit();
1263 mError = NO_ERROR;
1264 mData = const_cast<uint8_t*>(data);
1265 mDataSize = mDataCapacity = dataSize;
1266 //LOGI("setDataReference Setting data size of %p to %lu (pid=%d)\n", this, mDataSize, getpid());
1267 mDataPos = 0;
1268 LOGV("setDataReference Setting data pos of %p to %d\n", this, mDataPos);
1269 mObjects = const_cast<size_t*>(objects);
1270 mObjectsSize = mObjectsCapacity = objectsCount;
1271 mNextObjectHint = 0;
1272 mOwner = relFunc;
1273 mOwnerCookie = relCookie;
1274 scanForFds();
1275}
1276
1277void Parcel::print(TextOutput& to, uint32_t flags) const
1278{
1279 to << "Parcel(";
1280
1281 if (errorCheck() != NO_ERROR) {
1282 const status_t err = errorCheck();
1283 to << "Error: " << (void*)err << " \"" << strerror(-err) << "\"";
1284 } else if (dataSize() > 0) {
1285 const uint8_t* DATA = data();
1286 to << indent << HexDump(DATA, dataSize()) << dedent;
1287 const size_t* OBJS = objects();
1288 const size_t N = objectsCount();
1289 for (size_t i=0; i<N; i++) {
1290 const flat_binder_object* flat
1291 = reinterpret_cast<const flat_binder_object*>(DATA+OBJS[i]);
1292 to << endl << "Object #" << i << " @ " << (void*)OBJS[i] << ": "
1293 << TypeCode(flat->type & 0x7f7f7f00)
1294 << " = " << flat->binder;
1295 }
1296 } else {
1297 to << "NULL";
1298 }
1299
1300 to << ")";
1301}
1302
1303void Parcel::releaseObjects()
1304{
1305 const sp<ProcessState> proc(ProcessState::self());
1306 size_t i = mObjectsSize;
1307 uint8_t* const data = mData;
1308 size_t* const objects = mObjects;
1309 while (i > 0) {
1310 i--;
1311 const flat_binder_object* flat
1312 = reinterpret_cast<flat_binder_object*>(data+objects[i]);
1313 release_object(proc, *flat, this);
1314 }
1315}
1316
1317void Parcel::acquireObjects()
1318{
1319 const sp<ProcessState> proc(ProcessState::self());
1320 size_t i = mObjectsSize;
1321 uint8_t* const data = mData;
1322 size_t* const objects = mObjects;
1323 while (i > 0) {
1324 i--;
1325 const flat_binder_object* flat
1326 = reinterpret_cast<flat_binder_object*>(data+objects[i]);
1327 acquire_object(proc, *flat, this);
1328 }
1329}
1330
1331void Parcel::freeData()
1332{
1333 freeDataNoInit();
1334 initState();
1335}
1336
1337void Parcel::freeDataNoInit()
1338{
1339 if (mOwner) {
1340 //LOGI("Freeing data ref of %p (pid=%d)\n", this, getpid());
1341 mOwner(this, mData, mDataSize, mObjects, mObjectsSize, mOwnerCookie);
1342 } else {
1343 releaseObjects();
1344 if (mData) free(mData);
1345 if (mObjects) free(mObjects);
1346 }
1347}
1348
1349status_t Parcel::growData(size_t len)
1350{
1351 size_t newSize = ((mDataSize+len)*3)/2;
1352 return (newSize <= mDataSize)
1353 ? (status_t) NO_MEMORY
1354 : continueWrite(newSize);
1355}
1356
1357status_t Parcel::restartWrite(size_t desired)
1358{
1359 if (mOwner) {
1360 freeData();
1361 return continueWrite(desired);
1362 }
1363
1364 uint8_t* data = (uint8_t*)realloc(mData, desired);
1365 if (!data && desired > mDataCapacity) {
1366 mError = NO_MEMORY;
1367 return NO_MEMORY;
1368 }
1369
1370 releaseObjects();
1371
1372 if (data) {
1373 mData = data;
1374 mDataCapacity = desired;
1375 }
1376
1377 mDataSize = mDataPos = 0;
1378 LOGV("restartWrite Setting data size of %p to %d\n", this, mDataSize);
1379 LOGV("restartWrite Setting data pos of %p to %d\n", this, mDataPos);
1380
1381 free(mObjects);
1382 mObjects = NULL;
1383 mObjectsSize = mObjectsCapacity = 0;
1384 mNextObjectHint = 0;
1385 mHasFds = false;
1386 mFdsKnown = true;
Dianne Hackborn9ecebbf2011-09-28 23:19:47 -04001387 mAllowFds = true;
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -07001388
1389 return NO_ERROR;
1390}
1391
1392status_t Parcel::continueWrite(size_t desired)
1393{
1394 // If shrinking, first adjust for any objects that appear
1395 // after the new data size.
1396 size_t objectsSize = mObjectsSize;
1397 if (desired < mDataSize) {
1398 if (desired == 0) {
1399 objectsSize = 0;
1400 } else {
1401 while (objectsSize > 0) {
1402 if (mObjects[objectsSize-1] < desired)
1403 break;
1404 objectsSize--;
1405 }
1406 }
1407 }
1408
1409 if (mOwner) {
1410 // If the size is going to zero, just release the owner's data.
1411 if (desired == 0) {
1412 freeData();
1413 return NO_ERROR;
1414 }
1415
1416 // If there is a different owner, we need to take
1417 // posession.
1418 uint8_t* data = (uint8_t*)malloc(desired);
1419 if (!data) {
1420 mError = NO_MEMORY;
1421 return NO_MEMORY;
1422 }
1423 size_t* objects = NULL;
1424
1425 if (objectsSize) {
1426 objects = (size_t*)malloc(objectsSize*sizeof(size_t));
1427 if (!objects) {
1428 mError = NO_MEMORY;
1429 return NO_MEMORY;
1430 }
1431
1432 // Little hack to only acquire references on objects
1433 // we will be keeping.
1434 size_t oldObjectsSize = mObjectsSize;
1435 mObjectsSize = objectsSize;
1436 acquireObjects();
1437 mObjectsSize = oldObjectsSize;
1438 }
1439
1440 if (mData) {
1441 memcpy(data, mData, mDataSize < desired ? mDataSize : desired);
1442 }
1443 if (objects && mObjects) {
1444 memcpy(objects, mObjects, objectsSize*sizeof(size_t));
1445 }
1446 //LOGI("Freeing data ref of %p (pid=%d)\n", this, getpid());
1447 mOwner(this, mData, mDataSize, mObjects, mObjectsSize, mOwnerCookie);
1448 mOwner = NULL;
1449
1450 mData = data;
1451 mObjects = objects;
1452 mDataSize = (mDataSize < desired) ? mDataSize : desired;
1453 LOGV("continueWrite Setting data size of %p to %d\n", this, mDataSize);
1454 mDataCapacity = desired;
1455 mObjectsSize = mObjectsCapacity = objectsSize;
1456 mNextObjectHint = 0;
1457
1458 } else if (mData) {
1459 if (objectsSize < mObjectsSize) {
1460 // Need to release refs on any objects we are dropping.
1461 const sp<ProcessState> proc(ProcessState::self());
1462 for (size_t i=objectsSize; i<mObjectsSize; i++) {
1463 const flat_binder_object* flat
1464 = reinterpret_cast<flat_binder_object*>(mData+mObjects[i]);
1465 if (flat->type == BINDER_TYPE_FD) {
1466 // will need to rescan because we may have lopped off the only FDs
1467 mFdsKnown = false;
1468 }
1469 release_object(proc, *flat, this);
1470 }
1471 size_t* objects =
1472 (size_t*)realloc(mObjects, objectsSize*sizeof(size_t));
1473 if (objects) {
1474 mObjects = objects;
1475 }
1476 mObjectsSize = objectsSize;
1477 mNextObjectHint = 0;
1478 }
1479
1480 // We own the data, so we can just do a realloc().
1481 if (desired > mDataCapacity) {
1482 uint8_t* data = (uint8_t*)realloc(mData, desired);
1483 if (data) {
1484 mData = data;
1485 mDataCapacity = desired;
1486 } else if (desired > mDataCapacity) {
1487 mError = NO_MEMORY;
1488 return NO_MEMORY;
1489 }
1490 } else {
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07001491 if (mDataSize > desired) {
1492 mDataSize = desired;
1493 LOGV("continueWrite Setting data size of %p to %d\n", this, mDataSize);
1494 }
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -07001495 if (mDataPos > desired) {
1496 mDataPos = desired;
1497 LOGV("continueWrite Setting data pos of %p to %d\n", this, mDataPos);
1498 }
1499 }
1500
1501 } else {
1502 // This is the first data. Easy!
1503 uint8_t* data = (uint8_t*)malloc(desired);
1504 if (!data) {
1505 mError = NO_MEMORY;
1506 return NO_MEMORY;
1507 }
1508
1509 if(!(mDataCapacity == 0 && mObjects == NULL
1510 && mObjectsCapacity == 0)) {
1511 LOGE("continueWrite: %d/%p/%d/%d", mDataCapacity, mObjects, mObjectsCapacity, desired);
1512 }
1513
1514 mData = data;
1515 mDataSize = mDataPos = 0;
1516 LOGV("continueWrite Setting data size of %p to %d\n", this, mDataSize);
1517 LOGV("continueWrite Setting data pos of %p to %d\n", this, mDataPos);
1518 mDataCapacity = desired;
1519 }
1520
1521 return NO_ERROR;
1522}
1523
1524void Parcel::initState()
1525{
1526 mError = NO_ERROR;
1527 mData = 0;
1528 mDataSize = 0;
1529 mDataCapacity = 0;
1530 mDataPos = 0;
1531 LOGV("initState Setting data size of %p to %d\n", this, mDataSize);
1532 LOGV("initState Setting data pos of %p to %d\n", this, mDataPos);
1533 mObjects = NULL;
1534 mObjectsSize = 0;
1535 mObjectsCapacity = 0;
1536 mNextObjectHint = 0;
1537 mHasFds = false;
1538 mFdsKnown = true;
Dianne Hackborn9ecebbf2011-09-28 23:19:47 -04001539 mAllowFds = true;
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -07001540 mOwner = NULL;
1541}
1542
1543void Parcel::scanForFds() const
1544{
1545 bool hasFds = false;
1546 for (size_t i=0; i<mObjectsSize; i++) {
1547 const flat_binder_object* flat
1548 = reinterpret_cast<const flat_binder_object*>(mData + mObjects[i]);
1549 if (flat->type == BINDER_TYPE_FD) {
1550 hasFds = true;
1551 break;
1552 }
1553 }
1554 mHasFds = hasFds;
1555 mFdsKnown = true;
1556}
1557
Jeff Brown6579a9d2011-09-23 21:17:56 -07001558// --- Parcel::Blob ---
1559
1560Parcel::Blob::Blob() :
1561 mMapped(false), mData(NULL), mSize(0) {
1562}
1563
1564Parcel::Blob::~Blob() {
1565 release();
1566}
1567
1568void Parcel::Blob::release() {
1569 if (mMapped && mData) {
1570 ::munmap(mData, mSize);
1571 }
1572 clear();
1573}
1574
1575void Parcel::Blob::init(bool mapped, void* data, size_t size) {
1576 mMapped = mapped;
1577 mData = data;
1578 mSize = size;
1579}
1580
1581void Parcel::Blob::clear() {
1582 mMapped = false;
1583 mData = NULL;
1584 mSize = 0;
1585}
1586
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -07001587}; // namespace android