blob: 882130f9adca69be42c9f0a57fc30faf1ed99285 [file] [log] [blame]
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001/*
2 * Copyright (C) 2005 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#define LOG_TAG "Parcel"
18//#define LOG_NDEBUG 0
19
Mathias Agopianc5b2c0b2009-05-19 19:08:10 -070020#include <binder/Parcel.h>
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -070021
Brad Fitzpatrick702ea9d2010-06-18 13:07:53 -070022#include <binder/IPCThreadState.h>
Mathias Agopianc5b2c0b2009-05-19 19:08:10 -070023#include <binder/Binder.h>
24#include <binder/BpBinder.h>
Mathias Agopianc5b2c0b2009-05-19 19:08:10 -070025#include <binder/ProcessState.h>
Mathias Agopian002e1e52013-05-06 20:20:50 -070026#include <binder/TextOutput.h>
27
28#include <utils/Debug.h>
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -070029#include <utils/Log.h>
30#include <utils/String8.h>
31#include <utils/String16.h>
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -070032#include <utils/misc.h>
Mathias Agopian98e71dd2010-02-11 17:30:52 -080033#include <utils/Flattenable.h>
Jeff Brown5707dbf2011-09-23 21:17:56 -070034#include <cutils/ashmem.h>
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -070035
Mathias Agopian208059f2009-05-18 15:08:03 -070036#include <private/binder/binder_module.h>
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -070037
Arve Hjønnevågf50b9ea2014-02-13 19:22:08 -080038#include <inttypes.h>
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -070039#include <stdio.h>
40#include <stdlib.h>
41#include <stdint.h>
Jeff Brown5707dbf2011-09-23 21:17:56 -070042#include <sys/mman.h>
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -070043
44#ifndef INT32_MAX
45#define INT32_MAX ((int32_t)(2147483647))
46#endif
47
48#define LOG_REFS(...)
Steve Block9f760152011-10-12 17:27:03 +010049//#define LOG_REFS(...) ALOG(LOG_DEBUG, "Parcel", __VA_ARGS__)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -070050
51// ---------------------------------------------------------------------------
52
53#define PAD_SIZE(s) (((s)+3)&~3)
54
Brad Fitzpatricka877cd82010-07-07 16:06:39 -070055// Note: must be kept in sync with android/os/StrictMode.java's PENALTY_GATHER
56#define STRICT_MODE_PENALTY_GATHER 0x100
57
Brad Fitzpatrickd36f4a52010-07-12 11:05:38 -070058// Note: must be kept in sync with android/os/Parcel.java's EX_HAS_REPLY_HEADER
59#define EX_HAS_REPLY_HEADER -128
60
Jeff Brown5707dbf2011-09-23 21:17:56 -070061// Maximum size of a blob to transfer in-place.
62static const size_t IN_PLACE_BLOB_LIMIT = 40 * 1024;
63
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -070064// XXX This can be made public if we want to provide
65// support for typed data.
66struct small_flat_data
67{
68 uint32_t type;
69 uint32_t data;
70};
71
72namespace android {
73
74void acquire_object(const sp<ProcessState>& proc,
75 const flat_binder_object& obj, const void* who)
76{
77 switch (obj.type) {
78 case BINDER_TYPE_BINDER:
79 if (obj.binder) {
80 LOG_REFS("Parcel %p acquiring reference on local %p", who, obj.cookie);
81 static_cast<IBinder*>(obj.cookie)->incStrong(who);
82 }
83 return;
84 case BINDER_TYPE_WEAK_BINDER:
85 if (obj.binder)
86 static_cast<RefBase::weakref_type*>(obj.binder)->incWeak(who);
87 return;
88 case BINDER_TYPE_HANDLE: {
89 const sp<IBinder> b = proc->getStrongProxyForHandle(obj.handle);
90 if (b != NULL) {
91 LOG_REFS("Parcel %p acquiring reference on remote %p", who, b.get());
92 b->incStrong(who);
93 }
94 return;
95 }
96 case BINDER_TYPE_WEAK_HANDLE: {
97 const wp<IBinder> b = proc->getWeakProxyForHandle(obj.handle);
98 if (b != NULL) b.get_refs()->incWeak(who);
99 return;
100 }
101 case BINDER_TYPE_FD: {
102 // intentionally blank -- nothing to do to acquire this, but we do
103 // recognize it as a legitimate object type.
104 return;
105 }
106 }
107
Steve Block9d453682011-12-20 16:23:08 +0000108 ALOGD("Invalid object type 0x%08lx", obj.type);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700109}
110
111void release_object(const sp<ProcessState>& proc,
112 const flat_binder_object& obj, const void* who)
113{
114 switch (obj.type) {
115 case BINDER_TYPE_BINDER:
116 if (obj.binder) {
117 LOG_REFS("Parcel %p releasing reference on local %p", who, obj.cookie);
118 static_cast<IBinder*>(obj.cookie)->decStrong(who);
119 }
120 return;
121 case BINDER_TYPE_WEAK_BINDER:
122 if (obj.binder)
123 static_cast<RefBase::weakref_type*>(obj.binder)->decWeak(who);
124 return;
125 case BINDER_TYPE_HANDLE: {
126 const sp<IBinder> b = proc->getStrongProxyForHandle(obj.handle);
127 if (b != NULL) {
128 LOG_REFS("Parcel %p releasing reference on remote %p", who, b.get());
129 b->decStrong(who);
130 }
131 return;
132 }
133 case BINDER_TYPE_WEAK_HANDLE: {
134 const wp<IBinder> b = proc->getWeakProxyForHandle(obj.handle);
135 if (b != NULL) b.get_refs()->decWeak(who);
136 return;
137 }
138 case BINDER_TYPE_FD: {
139 if (obj.cookie != (void*)0) close(obj.handle);
140 return;
141 }
142 }
143
Steve Blocke6f43dd2012-01-06 19:20:56 +0000144 ALOGE("Invalid object type 0x%08lx", obj.type);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700145}
146
147inline static status_t finish_flatten_binder(
148 const sp<IBinder>& binder, const flat_binder_object& flat, Parcel* out)
149{
150 return out->writeObject(flat, false);
151}
152
153status_t flatten_binder(const sp<ProcessState>& proc,
154 const sp<IBinder>& binder, Parcel* out)
155{
156 flat_binder_object obj;
157
158 obj.flags = 0x7f | FLAT_BINDER_FLAG_ACCEPTS_FDS;
159 if (binder != NULL) {
160 IBinder *local = binder->localBinder();
161 if (!local) {
162 BpBinder *proxy = binder->remoteBinder();
163 if (proxy == NULL) {
Steve Blocke6f43dd2012-01-06 19:20:56 +0000164 ALOGE("null proxy");
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700165 }
166 const int32_t handle = proxy ? proxy->handle() : 0;
167 obj.type = BINDER_TYPE_HANDLE;
168 obj.handle = handle;
169 obj.cookie = NULL;
170 } else {
171 obj.type = BINDER_TYPE_BINDER;
172 obj.binder = local->getWeakRefs();
173 obj.cookie = local;
174 }
175 } else {
176 obj.type = BINDER_TYPE_BINDER;
177 obj.binder = NULL;
178 obj.cookie = NULL;
179 }
180
181 return finish_flatten_binder(binder, obj, out);
182}
183
184status_t flatten_binder(const sp<ProcessState>& proc,
185 const wp<IBinder>& binder, Parcel* out)
186{
187 flat_binder_object obj;
188
189 obj.flags = 0x7f | FLAT_BINDER_FLAG_ACCEPTS_FDS;
190 if (binder != NULL) {
191 sp<IBinder> real = binder.promote();
192 if (real != NULL) {
193 IBinder *local = real->localBinder();
194 if (!local) {
195 BpBinder *proxy = real->remoteBinder();
196 if (proxy == NULL) {
Steve Blocke6f43dd2012-01-06 19:20:56 +0000197 ALOGE("null proxy");
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700198 }
199 const int32_t handle = proxy ? proxy->handle() : 0;
200 obj.type = BINDER_TYPE_WEAK_HANDLE;
201 obj.handle = handle;
202 obj.cookie = NULL;
203 } else {
204 obj.type = BINDER_TYPE_WEAK_BINDER;
205 obj.binder = binder.get_refs();
206 obj.cookie = binder.unsafe_get();
207 }
208 return finish_flatten_binder(real, obj, out);
209 }
210
211 // XXX How to deal? In order to flatten the given binder,
212 // we need to probe it for information, which requires a primary
213 // reference... but we don't have one.
214 //
215 // The OpenBinder implementation uses a dynamic_cast<> here,
216 // but we can't do that with the different reference counting
217 // implementation we are using.
Steve Blocke6f43dd2012-01-06 19:20:56 +0000218 ALOGE("Unable to unflatten Binder weak reference!");
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700219 obj.type = BINDER_TYPE_BINDER;
220 obj.binder = NULL;
221 obj.cookie = NULL;
222 return finish_flatten_binder(NULL, obj, out);
223
224 } else {
225 obj.type = BINDER_TYPE_BINDER;
226 obj.binder = NULL;
227 obj.cookie = NULL;
228 return finish_flatten_binder(NULL, obj, out);
229 }
230}
231
232inline static status_t finish_unflatten_binder(
233 BpBinder* proxy, const flat_binder_object& flat, const Parcel& in)
234{
235 return NO_ERROR;
236}
237
238status_t unflatten_binder(const sp<ProcessState>& proc,
239 const Parcel& in, sp<IBinder>* out)
240{
241 const flat_binder_object* flat = in.readObject(false);
242
243 if (flat) {
244 switch (flat->type) {
245 case BINDER_TYPE_BINDER:
246 *out = static_cast<IBinder*>(flat->cookie);
247 return finish_unflatten_binder(NULL, *flat, in);
248 case BINDER_TYPE_HANDLE:
249 *out = proc->getStrongProxyForHandle(flat->handle);
250 return finish_unflatten_binder(
251 static_cast<BpBinder*>(out->get()), *flat, in);
252 }
253 }
254 return BAD_TYPE;
255}
256
257status_t unflatten_binder(const sp<ProcessState>& proc,
258 const Parcel& in, wp<IBinder>* out)
259{
260 const flat_binder_object* flat = in.readObject(false);
261
262 if (flat) {
263 switch (flat->type) {
264 case BINDER_TYPE_BINDER:
265 *out = static_cast<IBinder*>(flat->cookie);
266 return finish_unflatten_binder(NULL, *flat, in);
267 case BINDER_TYPE_WEAK_BINDER:
268 if (flat->binder != NULL) {
269 out->set_object_and_refs(
270 static_cast<IBinder*>(flat->cookie),
271 static_cast<RefBase::weakref_type*>(flat->binder));
272 } else {
273 *out = NULL;
274 }
275 return finish_unflatten_binder(NULL, *flat, in);
276 case BINDER_TYPE_HANDLE:
277 case BINDER_TYPE_WEAK_HANDLE:
278 *out = proc->getWeakProxyForHandle(flat->handle);
279 return finish_unflatten_binder(
280 static_cast<BpBinder*>(out->unsafe_get()), *flat, in);
281 }
282 }
283 return BAD_TYPE;
284}
285
286// ---------------------------------------------------------------------------
287
288Parcel::Parcel()
289{
290 initState();
291}
292
293Parcel::~Parcel()
294{
295 freeDataNoInit();
296}
297
298const uint8_t* Parcel::data() const
299{
300 return mData;
301}
302
303size_t Parcel::dataSize() const
304{
305 return (mDataSize > mDataPos ? mDataSize : mDataPos);
306}
307
308size_t Parcel::dataAvail() const
309{
310 // TODO: decide what to do about the possibility that this can
311 // report an available-data size that exceeds a Java int's max
312 // positive value, causing havoc. Fortunately this will only
313 // happen if someone constructs a Parcel containing more than two
314 // gigabytes of data, which on typical phone hardware is simply
315 // not possible.
316 return dataSize() - dataPosition();
317}
318
319size_t Parcel::dataPosition() const
320{
321 return mDataPos;
322}
323
324size_t Parcel::dataCapacity() const
325{
326 return mDataCapacity;
327}
328
329status_t Parcel::setDataSize(size_t size)
330{
331 status_t err;
332 err = continueWrite(size);
333 if (err == NO_ERROR) {
334 mDataSize = size;
Steve Block6807e592011-10-20 11:56:00 +0100335 ALOGV("setDataSize Setting data size of %p to %d\n", this, mDataSize);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700336 }
337 return err;
338}
339
340void Parcel::setDataPosition(size_t pos) const
341{
342 mDataPos = pos;
343 mNextObjectHint = 0;
344}
345
346status_t Parcel::setDataCapacity(size_t size)
347{
Dianne Hackborn97e2bcd2011-04-13 18:15:56 -0700348 if (size > mDataCapacity) return continueWrite(size);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700349 return NO_ERROR;
350}
351
352status_t Parcel::setData(const uint8_t* buffer, size_t len)
353{
354 status_t err = restartWrite(len);
355 if (err == NO_ERROR) {
356 memcpy(const_cast<uint8_t*>(data()), buffer, len);
357 mDataSize = len;
358 mFdsKnown = false;
359 }
360 return err;
361}
362
Andreas Huber51faf462011-04-13 10:21:56 -0700363status_t Parcel::appendFrom(const Parcel *parcel, size_t offset, size_t len)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700364{
365 const sp<ProcessState> proc(ProcessState::self());
366 status_t err;
Andreas Huber51faf462011-04-13 10:21:56 -0700367 const uint8_t *data = parcel->mData;
368 const size_t *objects = parcel->mObjects;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700369 size_t size = parcel->mObjectsSize;
370 int startPos = mDataPos;
371 int firstIndex = -1, lastIndex = -2;
372
373 if (len == 0) {
374 return NO_ERROR;
375 }
376
377 // range checks against the source parcel size
378 if ((offset > parcel->mDataSize)
379 || (len > parcel->mDataSize)
380 || (offset + len > parcel->mDataSize)) {
381 return BAD_VALUE;
382 }
383
384 // Count objects in range
385 for (int i = 0; i < (int) size; i++) {
386 size_t off = objects[i];
387 if ((off >= offset) && (off < offset + len)) {
388 if (firstIndex == -1) {
389 firstIndex = i;
390 }
391 lastIndex = i;
392 }
393 }
394 int numObjects = lastIndex - firstIndex + 1;
395
Dianne Hackborn97e2bcd2011-04-13 18:15:56 -0700396 if ((mDataSize+len) > mDataCapacity) {
397 // grow data
398 err = growData(len);
399 if (err != NO_ERROR) {
400 return err;
401 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700402 }
403
404 // append data
405 memcpy(mData + mDataPos, data + offset, len);
406 mDataPos += len;
407 mDataSize += len;
408
Dianne Hackborn8938ed22011-09-28 23:19:47 -0400409 err = NO_ERROR;
410
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700411 if (numObjects > 0) {
412 // grow objects
413 if (mObjectsCapacity < mObjectsSize + numObjects) {
414 int newSize = ((mObjectsSize + numObjects)*3)/2;
415 size_t *objects =
416 (size_t*)realloc(mObjects, newSize*sizeof(size_t));
417 if (objects == (size_t*)0) {
418 return NO_MEMORY;
419 }
420 mObjects = objects;
421 mObjectsCapacity = newSize;
422 }
423
424 // append and acquire objects
425 int idx = mObjectsSize;
426 for (int i = firstIndex; i <= lastIndex; i++) {
427 size_t off = objects[i] - offset + startPos;
428 mObjects[idx++] = off;
429 mObjectsSize++;
430
Dianne Hackborn8af0f822009-05-22 13:20:23 -0700431 flat_binder_object* flat
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700432 = reinterpret_cast<flat_binder_object*>(mData + off);
433 acquire_object(proc, *flat, this);
434
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700435 if (flat->type == BINDER_TYPE_FD) {
Dianne Hackborn8af0f822009-05-22 13:20:23 -0700436 // If this is a file descriptor, we need to dup it so the
437 // new Parcel now owns its own fd, and can declare that we
438 // officially know we have fds.
439 flat->handle = dup(flat->handle);
440 flat->cookie = (void*)1;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700441 mHasFds = mFdsKnown = true;
Dianne Hackborn8938ed22011-09-28 23:19:47 -0400442 if (!mAllowFds) {
443 err = FDS_NOT_ALLOWED;
444 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700445 }
446 }
447 }
448
Dianne Hackborn8938ed22011-09-28 23:19:47 -0400449 return err;
450}
451
Dianne Hackborn7746cc32011-10-03 21:09:35 -0700452bool Parcel::pushAllowFds(bool allowFds)
Dianne Hackborn8938ed22011-09-28 23:19:47 -0400453{
454 const bool origValue = mAllowFds;
Dianne Hackborn7746cc32011-10-03 21:09:35 -0700455 if (!allowFds) {
456 mAllowFds = false;
457 }
Dianne Hackborn8938ed22011-09-28 23:19:47 -0400458 return origValue;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700459}
460
Dianne Hackborn7746cc32011-10-03 21:09:35 -0700461void Parcel::restoreAllowFds(bool lastValue)
462{
463 mAllowFds = lastValue;
464}
465
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700466bool Parcel::hasFileDescriptors() const
467{
468 if (!mFdsKnown) {
469 scanForFds();
470 }
471 return mHasFds;
472}
473
Brad Fitzpatrick702ea9d2010-06-18 13:07:53 -0700474// Write RPC headers. (previously just the interface token)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700475status_t Parcel::writeInterfaceToken(const String16& interface)
476{
Brad Fitzpatricka877cd82010-07-07 16:06:39 -0700477 writeInt32(IPCThreadState::self()->getStrictModePolicy() |
478 STRICT_MODE_PENALTY_GATHER);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700479 // currently the interface identification token is just its name as a string
480 return writeString16(interface);
481}
482
Mathias Agopian83c04462009-05-22 19:00:22 -0700483bool Parcel::checkInterface(IBinder* binder) const
484{
Brad Fitzpatrick702ea9d2010-06-18 13:07:53 -0700485 return enforceInterface(binder->getInterfaceDescriptor());
Mathias Agopian83c04462009-05-22 19:00:22 -0700486}
487
Brad Fitzpatricka877cd82010-07-07 16:06:39 -0700488bool Parcel::enforceInterface(const String16& interface,
Brad Fitzpatrick70081a12010-07-27 09:49:11 -0700489 IPCThreadState* threadState) const
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700490{
Brad Fitzpatrick70081a12010-07-27 09:49:11 -0700491 int32_t strictPolicy = readInt32();
492 if (threadState == NULL) {
493 threadState = IPCThreadState::self();
Brad Fitzpatricka877cd82010-07-07 16:06:39 -0700494 }
Brad Fitzpatrick52736032010-08-30 16:01:16 -0700495 if ((threadState->getLastTransactionBinderFlags() &
496 IBinder::FLAG_ONEWAY) != 0) {
497 // For one-way calls, the callee is running entirely
498 // disconnected from the caller, so disable StrictMode entirely.
499 // Not only does disk/network usage not impact the caller, but
500 // there's no way to commuicate back any violations anyway.
501 threadState->setStrictModePolicy(0);
502 } else {
503 threadState->setStrictModePolicy(strictPolicy);
504 }
Mathias Agopian83c04462009-05-22 19:00:22 -0700505 const String16 str(readString16());
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700506 if (str == interface) {
507 return true;
508 } else {
Steve Block32397c12012-01-05 23:22:43 +0000509 ALOGW("**** enforceInterface() expected '%s' but read '%s'\n",
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700510 String8(interface).string(), String8(str).string());
511 return false;
512 }
Brad Fitzpatrick702ea9d2010-06-18 13:07:53 -0700513}
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700514
515const size_t* Parcel::objects() const
516{
517 return mObjects;
518}
519
520size_t Parcel::objectsCount() const
521{
522 return mObjectsSize;
523}
524
525status_t Parcel::errorCheck() const
526{
527 return mError;
528}
529
530void Parcel::setError(status_t err)
531{
532 mError = err;
533}
534
535status_t Parcel::finishWrite(size_t len)
536{
537 //printf("Finish write of %d\n", len);
538 mDataPos += len;
Steve Block6807e592011-10-20 11:56:00 +0100539 ALOGV("finishWrite Setting data pos of %p to %d\n", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700540 if (mDataPos > mDataSize) {
541 mDataSize = mDataPos;
Steve Block6807e592011-10-20 11:56:00 +0100542 ALOGV("finishWrite Setting data size of %p to %d\n", this, mDataSize);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700543 }
544 //printf("New pos=%d, size=%d\n", mDataPos, mDataSize);
545 return NO_ERROR;
546}
547
548status_t Parcel::writeUnpadded(const void* data, size_t len)
549{
550 size_t end = mDataPos + len;
551 if (end < mDataPos) {
552 // integer overflow
553 return BAD_VALUE;
554 }
555
556 if (end <= mDataCapacity) {
557restart_write:
558 memcpy(mData+mDataPos, data, len);
559 return finishWrite(len);
560 }
561
562 status_t err = growData(len);
563 if (err == NO_ERROR) goto restart_write;
564 return err;
565}
566
567status_t Parcel::write(const void* data, size_t len)
568{
569 void* const d = writeInplace(len);
570 if (d) {
571 memcpy(d, data, len);
572 return NO_ERROR;
573 }
574 return mError;
575}
576
577void* Parcel::writeInplace(size_t len)
578{
579 const size_t padded = PAD_SIZE(len);
580
581 // sanity check for integer overflow
582 if (mDataPos+padded < mDataPos) {
583 return NULL;
584 }
585
586 if ((mDataPos+padded) <= mDataCapacity) {
587restart_write:
588 //printf("Writing %ld bytes, padded to %ld\n", len, padded);
589 uint8_t* const data = mData+mDataPos;
590
591 // Need to pad at end?
592 if (padded != len) {
593#if BYTE_ORDER == BIG_ENDIAN
594 static const uint32_t mask[4] = {
595 0x00000000, 0xffffff00, 0xffff0000, 0xff000000
596 };
597#endif
598#if BYTE_ORDER == LITTLE_ENDIAN
599 static const uint32_t mask[4] = {
600 0x00000000, 0x00ffffff, 0x0000ffff, 0x000000ff
601 };
602#endif
603 //printf("Applying pad mask: %p to %p\n", (void*)mask[padded-len],
604 // *reinterpret_cast<void**>(data+padded-4));
605 *reinterpret_cast<uint32_t*>(data+padded-4) &= mask[padded-len];
606 }
607
608 finishWrite(padded);
609 return data;
610 }
611
612 status_t err = growData(padded);
613 if (err == NO_ERROR) goto restart_write;
614 return NULL;
615}
616
617status_t Parcel::writeInt32(int32_t val)
618{
Andreas Huber84a6d042009-08-17 13:33:27 -0700619 return writeAligned(val);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700620}
Marco Nelissen708cc792013-10-16 10:57:51 -0700621status_t Parcel::writeInt32Array(size_t len, const int32_t *val) {
622 if (!val) {
623 return writeAligned(-1);
624 }
625 status_t ret = writeAligned(len);
626 if (ret == NO_ERROR) {
627 ret = write(val, len * sizeof(*val));
628 }
629 return ret;
630}
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700631
632status_t Parcel::writeInt64(int64_t val)
633{
Andreas Huber84a6d042009-08-17 13:33:27 -0700634 return writeAligned(val);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700635}
636
637status_t Parcel::writeFloat(float val)
638{
Andreas Huber84a6d042009-08-17 13:33:27 -0700639 return writeAligned(val);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700640}
641
Douglas Leungcc1a4bb2013-01-11 15:00:55 -0800642#if defined(__mips__) && defined(__mips_hard_float)
643
644status_t Parcel::writeDouble(double val)
645{
646 union {
647 double d;
648 unsigned long long ll;
649 } u;
650 u.d = val;
651 return writeAligned(u.ll);
652}
653
654#else
655
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700656status_t Parcel::writeDouble(double val)
657{
Andreas Huber84a6d042009-08-17 13:33:27 -0700658 return writeAligned(val);
659}
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700660
Douglas Leungcc1a4bb2013-01-11 15:00:55 -0800661#endif
662
Andreas Huber84a6d042009-08-17 13:33:27 -0700663status_t Parcel::writeIntPtr(intptr_t val)
664{
665 return writeAligned(val);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700666}
667
668status_t Parcel::writeCString(const char* str)
669{
670 return write(str, strlen(str)+1);
671}
672
673status_t Parcel::writeString8(const String8& str)
674{
675 status_t err = writeInt32(str.bytes());
Pravat Dalbeherad1dff8d2010-12-15 08:40:00 +0100676 // only write string if its length is more than zero characters,
677 // as readString8 will only read if the length field is non-zero.
678 // this is slightly different from how writeString16 works.
679 if (str.bytes() > 0 && err == NO_ERROR) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700680 err = write(str.string(), str.bytes()+1);
681 }
682 return err;
683}
684
685status_t Parcel::writeString16(const String16& str)
686{
687 return writeString16(str.string(), str.size());
688}
689
690status_t Parcel::writeString16(const char16_t* str, size_t len)
691{
692 if (str == NULL) return writeInt32(-1);
693
694 status_t err = writeInt32(len);
695 if (err == NO_ERROR) {
696 len *= sizeof(char16_t);
697 uint8_t* data = (uint8_t*)writeInplace(len+sizeof(char16_t));
698 if (data) {
699 memcpy(data, str, len);
700 *reinterpret_cast<char16_t*>(data+len) = 0;
701 return NO_ERROR;
702 }
703 err = mError;
704 }
705 return err;
706}
707
708status_t Parcel::writeStrongBinder(const sp<IBinder>& val)
709{
710 return flatten_binder(ProcessState::self(), val, this);
711}
712
713status_t Parcel::writeWeakBinder(const wp<IBinder>& val)
714{
715 return flatten_binder(ProcessState::self(), val, this);
716}
717
Mathias Agopiana47f02a2009-05-21 16:29:38 -0700718status_t Parcel::writeNativeHandle(const native_handle* handle)
The Android Open Source Project5f78a482009-01-20 14:03:58 -0800719{
Mathias Agopian1d0a95b2009-07-31 16:12:13 -0700720 if (!handle || handle->version != sizeof(native_handle))
The Android Open Source Project5f78a482009-01-20 14:03:58 -0800721 return BAD_TYPE;
722
723 status_t err;
Mathias Agopiana47f02a2009-05-21 16:29:38 -0700724 err = writeInt32(handle->numFds);
The Android Open Source Project5f78a482009-01-20 14:03:58 -0800725 if (err != NO_ERROR) return err;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800726
Mathias Agopiana47f02a2009-05-21 16:29:38 -0700727 err = writeInt32(handle->numInts);
The Android Open Source Project5f78a482009-01-20 14:03:58 -0800728 if (err != NO_ERROR) return err;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800729
Mathias Agopiana47f02a2009-05-21 16:29:38 -0700730 for (int i=0 ; err==NO_ERROR && i<handle->numFds ; i++)
731 err = writeDupFileDescriptor(handle->data[i]);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800732
733 if (err != NO_ERROR) {
Steve Block9d453682011-12-20 16:23:08 +0000734 ALOGD("write native handle, write dup fd failed");
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800735 return err;
736 }
Mathias Agopiana47f02a2009-05-21 16:29:38 -0700737 err = write(handle->data + handle->numFds, sizeof(int)*handle->numInts);
The Android Open Source Project5f78a482009-01-20 14:03:58 -0800738 return err;
739}
740
Jeff Brown93ff1f92011-11-04 19:01:44 -0700741status_t Parcel::writeFileDescriptor(int fd, bool takeOwnership)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700742{
743 flat_binder_object obj;
744 obj.type = BINDER_TYPE_FD;
745 obj.flags = 0x7f | FLAT_BINDER_FLAG_ACCEPTS_FDS;
746 obj.handle = fd;
Jeff Brown93ff1f92011-11-04 19:01:44 -0700747 obj.cookie = (void*) (takeOwnership ? 1 : 0);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700748 return writeObject(obj, true);
749}
750
751status_t Parcel::writeDupFileDescriptor(int fd)
752{
Jeff Brownd341c712011-11-04 20:19:33 -0700753 int dupFd = dup(fd);
754 if (dupFd < 0) {
755 return -errno;
756 }
757 status_t err = writeFileDescriptor(dupFd, true /*takeOwnership*/);
758 if (err) {
759 close(dupFd);
760 }
761 return err;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700762}
763
Jeff Brown5707dbf2011-09-23 21:17:56 -0700764status_t Parcel::writeBlob(size_t len, WritableBlob* outBlob)
765{
766 status_t status;
767
768 if (!mAllowFds || len <= IN_PLACE_BLOB_LIMIT) {
Steve Block6807e592011-10-20 11:56:00 +0100769 ALOGV("writeBlob: write in place");
Jeff Brown5707dbf2011-09-23 21:17:56 -0700770 status = writeInt32(0);
771 if (status) return status;
772
773 void* ptr = writeInplace(len);
774 if (!ptr) return NO_MEMORY;
775
776 outBlob->init(false /*mapped*/, ptr, len);
777 return NO_ERROR;
778 }
779
Steve Block6807e592011-10-20 11:56:00 +0100780 ALOGV("writeBlob: write to ashmem");
Jeff Brown5707dbf2011-09-23 21:17:56 -0700781 int fd = ashmem_create_region("Parcel Blob", len);
782 if (fd < 0) return NO_MEMORY;
783
784 int result = ashmem_set_prot_region(fd, PROT_READ | PROT_WRITE);
785 if (result < 0) {
Jeff Brownec4e0062011-10-10 14:50:10 -0700786 status = result;
Jeff Brown5707dbf2011-09-23 21:17:56 -0700787 } else {
788 void* ptr = ::mmap(NULL, len, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
789 if (ptr == MAP_FAILED) {
790 status = -errno;
791 } else {
792 result = ashmem_set_prot_region(fd, PROT_READ);
793 if (result < 0) {
Jeff Brownec4e0062011-10-10 14:50:10 -0700794 status = result;
Jeff Brown5707dbf2011-09-23 21:17:56 -0700795 } else {
796 status = writeInt32(1);
797 if (!status) {
Jeff Brown93ff1f92011-11-04 19:01:44 -0700798 status = writeFileDescriptor(fd, true /*takeOwnership*/);
Jeff Brown5707dbf2011-09-23 21:17:56 -0700799 if (!status) {
800 outBlob->init(true /*mapped*/, ptr, len);
801 return NO_ERROR;
802 }
803 }
804 }
805 }
806 ::munmap(ptr, len);
807 }
808 ::close(fd);
809 return status;
810}
811
Mathias Agopiane1424282013-07-29 21:24:40 -0700812status_t Parcel::write(const FlattenableHelperInterface& val)
Mathias Agopian98e71dd2010-02-11 17:30:52 -0800813{
814 status_t err;
815
816 // size if needed
Mathias Agopiane1424282013-07-29 21:24:40 -0700817 const size_t len = val.getFlattenedSize();
818 const size_t fd_count = val.getFdCount();
Mathias Agopian98e71dd2010-02-11 17:30:52 -0800819
820 err = this->writeInt32(len);
821 if (err) return err;
822
823 err = this->writeInt32(fd_count);
824 if (err) return err;
825
826 // payload
Mathias Agopiane1424282013-07-29 21:24:40 -0700827 void* const buf = this->writeInplace(PAD_SIZE(len));
Mathias Agopian98e71dd2010-02-11 17:30:52 -0800828 if (buf == NULL)
829 return BAD_VALUE;
830
831 int* fds = NULL;
832 if (fd_count) {
833 fds = new int[fd_count];
834 }
835
836 err = val.flatten(buf, len, fds, fd_count);
837 for (size_t i=0 ; i<fd_count && err==NO_ERROR ; i++) {
838 err = this->writeDupFileDescriptor( fds[i] );
839 }
840
841 if (fd_count) {
842 delete [] fds;
843 }
844
845 return err;
846}
847
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700848status_t Parcel::writeObject(const flat_binder_object& val, bool nullMetaData)
849{
850 const bool enoughData = (mDataPos+sizeof(val)) <= mDataCapacity;
851 const bool enoughObjects = mObjectsSize < mObjectsCapacity;
852 if (enoughData && enoughObjects) {
853restart_write:
854 *reinterpret_cast<flat_binder_object*>(mData+mDataPos) = val;
855
856 // Need to write meta-data?
857 if (nullMetaData || val.binder != NULL) {
858 mObjects[mObjectsSize] = mDataPos;
859 acquire_object(ProcessState::self(), val, this);
860 mObjectsSize++;
861 }
862
863 // remember if it's a file descriptor
864 if (val.type == BINDER_TYPE_FD) {
Dianne Hackborn8938ed22011-09-28 23:19:47 -0400865 if (!mAllowFds) {
866 return FDS_NOT_ALLOWED;
867 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700868 mHasFds = mFdsKnown = true;
869 }
870
871 return finishWrite(sizeof(flat_binder_object));
872 }
873
874 if (!enoughData) {
875 const status_t err = growData(sizeof(val));
876 if (err != NO_ERROR) return err;
877 }
878 if (!enoughObjects) {
879 size_t newSize = ((mObjectsSize+2)*3)/2;
880 size_t* objects = (size_t*)realloc(mObjects, newSize*sizeof(size_t));
881 if (objects == NULL) return NO_MEMORY;
882 mObjects = objects;
883 mObjectsCapacity = newSize;
884 }
885
886 goto restart_write;
887}
888
Brad Fitzpatrick837a0d02010-07-13 15:33:35 -0700889status_t Parcel::writeNoException()
890{
891 return writeInt32(0);
892}
893
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700894void Parcel::remove(size_t start, size_t amt)
895{
896 LOG_ALWAYS_FATAL("Parcel::remove() not yet implemented!");
897}
898
899status_t Parcel::read(void* outData, size_t len) const
900{
901 if ((mDataPos+PAD_SIZE(len)) >= mDataPos && (mDataPos+PAD_SIZE(len)) <= mDataSize) {
902 memcpy(outData, mData+mDataPos, len);
903 mDataPos += PAD_SIZE(len);
Steve Block6807e592011-10-20 11:56:00 +0100904 ALOGV("read Setting data pos of %p to %d\n", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700905 return NO_ERROR;
906 }
907 return NOT_ENOUGH_DATA;
908}
909
910const void* Parcel::readInplace(size_t len) const
911{
912 if ((mDataPos+PAD_SIZE(len)) >= mDataPos && (mDataPos+PAD_SIZE(len)) <= mDataSize) {
913 const void* data = mData+mDataPos;
914 mDataPos += PAD_SIZE(len);
Steve Block6807e592011-10-20 11:56:00 +0100915 ALOGV("readInplace Setting data pos of %p to %d\n", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700916 return data;
917 }
918 return NULL;
919}
920
Andreas Huber84a6d042009-08-17 13:33:27 -0700921template<class T>
922status_t Parcel::readAligned(T *pArg) const {
923 COMPILE_TIME_ASSERT_FUNCTION_SCOPE(PAD_SIZE(sizeof(T)) == sizeof(T));
924
925 if ((mDataPos+sizeof(T)) <= mDataSize) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700926 const void* data = mData+mDataPos;
Andreas Huber84a6d042009-08-17 13:33:27 -0700927 mDataPos += sizeof(T);
928 *pArg = *reinterpret_cast<const T*>(data);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700929 return NO_ERROR;
930 } else {
931 return NOT_ENOUGH_DATA;
932 }
933}
934
Andreas Huber84a6d042009-08-17 13:33:27 -0700935template<class T>
936T Parcel::readAligned() const {
937 T result;
938 if (readAligned(&result) != NO_ERROR) {
939 result = 0;
940 }
941
942 return result;
943}
944
945template<class T>
946status_t Parcel::writeAligned(T val) {
947 COMPILE_TIME_ASSERT_FUNCTION_SCOPE(PAD_SIZE(sizeof(T)) == sizeof(T));
948
949 if ((mDataPos+sizeof(val)) <= mDataCapacity) {
950restart_write:
951 *reinterpret_cast<T*>(mData+mDataPos) = val;
952 return finishWrite(sizeof(val));
953 }
954
955 status_t err = growData(sizeof(val));
956 if (err == NO_ERROR) goto restart_write;
957 return err;
958}
959
960status_t Parcel::readInt32(int32_t *pArg) const
961{
962 return readAligned(pArg);
963}
964
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700965int32_t Parcel::readInt32() const
966{
Andreas Huber84a6d042009-08-17 13:33:27 -0700967 return readAligned<int32_t>();
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700968}
969
970
971status_t Parcel::readInt64(int64_t *pArg) const
972{
Andreas Huber84a6d042009-08-17 13:33:27 -0700973 return readAligned(pArg);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700974}
975
976
977int64_t Parcel::readInt64() const
978{
Andreas Huber84a6d042009-08-17 13:33:27 -0700979 return readAligned<int64_t>();
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700980}
981
982status_t Parcel::readFloat(float *pArg) const
983{
Andreas Huber84a6d042009-08-17 13:33:27 -0700984 return readAligned(pArg);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700985}
986
987
988float Parcel::readFloat() const
989{
Andreas Huber84a6d042009-08-17 13:33:27 -0700990 return readAligned<float>();
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700991}
992
Douglas Leungcc1a4bb2013-01-11 15:00:55 -0800993#if defined(__mips__) && defined(__mips_hard_float)
994
995status_t Parcel::readDouble(double *pArg) const
996{
997 union {
998 double d;
999 unsigned long long ll;
1000 } u;
1001 status_t status;
1002 status = readAligned(&u.ll);
1003 *pArg = u.d;
1004 return status;
1005}
1006
1007double Parcel::readDouble() const
1008{
1009 union {
1010 double d;
1011 unsigned long long ll;
1012 } u;
1013 u.ll = readAligned<unsigned long long>();
1014 return u.d;
1015}
1016
1017#else
1018
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001019status_t Parcel::readDouble(double *pArg) const
1020{
Andreas Huber84a6d042009-08-17 13:33:27 -07001021 return readAligned(pArg);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001022}
1023
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001024double Parcel::readDouble() const
1025{
Andreas Huber84a6d042009-08-17 13:33:27 -07001026 return readAligned<double>();
1027}
1028
Douglas Leungcc1a4bb2013-01-11 15:00:55 -08001029#endif
1030
Andreas Huber84a6d042009-08-17 13:33:27 -07001031status_t Parcel::readIntPtr(intptr_t *pArg) const
1032{
1033 return readAligned(pArg);
1034}
1035
1036
1037intptr_t Parcel::readIntPtr() const
1038{
1039 return readAligned<intptr_t>();
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001040}
1041
1042
1043const char* Parcel::readCString() const
1044{
1045 const size_t avail = mDataSize-mDataPos;
1046 if (avail > 0) {
1047 const char* str = reinterpret_cast<const char*>(mData+mDataPos);
1048 // is the string's trailing NUL within the parcel's valid bounds?
1049 const char* eos = reinterpret_cast<const char*>(memchr(str, 0, avail));
1050 if (eos) {
1051 const size_t len = eos - str;
1052 mDataPos += PAD_SIZE(len+1);
Steve Block6807e592011-10-20 11:56:00 +01001053 ALOGV("readCString Setting data pos of %p to %d\n", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001054 return str;
1055 }
1056 }
1057 return NULL;
1058}
1059
1060String8 Parcel::readString8() const
1061{
1062 int32_t size = readInt32();
1063 // watch for potential int overflow adding 1 for trailing NUL
1064 if (size > 0 && size < INT32_MAX) {
1065 const char* str = (const char*)readInplace(size+1);
1066 if (str) return String8(str, size);
1067 }
1068 return String8();
1069}
1070
1071String16 Parcel::readString16() const
1072{
1073 size_t len;
1074 const char16_t* str = readString16Inplace(&len);
1075 if (str) return String16(str, len);
Steve Blocke6f43dd2012-01-06 19:20:56 +00001076 ALOGE("Reading a NULL string not supported here.");
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001077 return String16();
1078}
1079
1080const char16_t* Parcel::readString16Inplace(size_t* outLen) const
1081{
1082 int32_t size = readInt32();
1083 // watch for potential int overflow from size+1
1084 if (size >= 0 && size < INT32_MAX) {
1085 *outLen = size;
1086 const char16_t* str = (const char16_t*)readInplace((size+1)*sizeof(char16_t));
1087 if (str != NULL) {
1088 return str;
1089 }
1090 }
1091 *outLen = 0;
1092 return NULL;
1093}
1094
1095sp<IBinder> Parcel::readStrongBinder() const
1096{
1097 sp<IBinder> val;
1098 unflatten_binder(ProcessState::self(), *this, &val);
1099 return val;
1100}
1101
1102wp<IBinder> Parcel::readWeakBinder() const
1103{
1104 wp<IBinder> val;
1105 unflatten_binder(ProcessState::self(), *this, &val);
1106 return val;
1107}
1108
Brad Fitzpatrick837a0d02010-07-13 15:33:35 -07001109int32_t Parcel::readExceptionCode() const
1110{
1111 int32_t exception_code = readAligned<int32_t>();
Brad Fitzpatrickd36f4a52010-07-12 11:05:38 -07001112 if (exception_code == EX_HAS_REPLY_HEADER) {
Magnus Strandberg1ba24572011-05-03 15:44:00 +02001113 int32_t header_start = dataPosition();
Brad Fitzpatrickd36f4a52010-07-12 11:05:38 -07001114 int32_t header_size = readAligned<int32_t>();
1115 // Skip over fat responses headers. Not used (or propagated) in
1116 // native code
Magnus Strandberg1ba24572011-05-03 15:44:00 +02001117 setDataPosition(header_start + header_size);
Brad Fitzpatrickd36f4a52010-07-12 11:05:38 -07001118 // And fat response headers are currently only used when there are no
1119 // exceptions, so return no error:
1120 return 0;
1121 }
Brad Fitzpatrick837a0d02010-07-13 15:33:35 -07001122 return exception_code;
1123}
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001124
Mathias Agopiana47f02a2009-05-21 16:29:38 -07001125native_handle* Parcel::readNativeHandle() const
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001126{
1127 int numFds, numInts;
1128 status_t err;
1129 err = readInt32(&numFds);
1130 if (err != NO_ERROR) return 0;
1131 err = readInt32(&numInts);
1132 if (err != NO_ERROR) return 0;
1133
Mathias Agopiana47f02a2009-05-21 16:29:38 -07001134 native_handle* h = native_handle_create(numFds, numInts);
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001135 for (int i=0 ; err==NO_ERROR && i<numFds ; i++) {
Rebecca Schultz Zavin360211f2009-02-13 16:34:38 -08001136 h->data[i] = dup(readFileDescriptor());
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001137 if (h->data[i] < 0) err = BAD_VALUE;
1138 }
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001139 err = read(h->data + numFds, sizeof(int)*numInts);
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001140 if (err != NO_ERROR) {
Mathias Agopiana47f02a2009-05-21 16:29:38 -07001141 native_handle_close(h);
1142 native_handle_delete(h);
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001143 h = 0;
1144 }
1145 return h;
1146}
1147
1148
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001149int Parcel::readFileDescriptor() const
1150{
1151 const flat_binder_object* flat = readObject(true);
1152 if (flat) {
1153 switch (flat->type) {
1154 case BINDER_TYPE_FD:
Steve Blocka19954a2012-01-04 20:05:49 +00001155 //ALOGI("Returning file descriptor %ld from parcel %p\n", flat->handle, this);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001156 return flat->handle;
1157 }
1158 }
1159 return BAD_TYPE;
1160}
1161
Jeff Brown5707dbf2011-09-23 21:17:56 -07001162status_t Parcel::readBlob(size_t len, ReadableBlob* outBlob) const
1163{
1164 int32_t useAshmem;
1165 status_t status = readInt32(&useAshmem);
1166 if (status) return status;
1167
1168 if (!useAshmem) {
Steve Block6807e592011-10-20 11:56:00 +01001169 ALOGV("readBlob: read in place");
Jeff Brown5707dbf2011-09-23 21:17:56 -07001170 const void* ptr = readInplace(len);
1171 if (!ptr) return BAD_VALUE;
1172
1173 outBlob->init(false /*mapped*/, const_cast<void*>(ptr), len);
1174 return NO_ERROR;
1175 }
1176
Steve Block6807e592011-10-20 11:56:00 +01001177 ALOGV("readBlob: read from ashmem");
Jeff Brown5707dbf2011-09-23 21:17:56 -07001178 int fd = readFileDescriptor();
1179 if (fd == int(BAD_TYPE)) return BAD_VALUE;
1180
1181 void* ptr = ::mmap(NULL, len, PROT_READ, MAP_SHARED, fd, 0);
1182 if (!ptr) return NO_MEMORY;
1183
1184 outBlob->init(true /*mapped*/, ptr, len);
1185 return NO_ERROR;
1186}
1187
Mathias Agopiane1424282013-07-29 21:24:40 -07001188status_t Parcel::read(FlattenableHelperInterface& val) const
Mathias Agopian98e71dd2010-02-11 17:30:52 -08001189{
1190 // size
1191 const size_t len = this->readInt32();
1192 const size_t fd_count = this->readInt32();
1193
1194 // payload
Mathias Agopiane1424282013-07-29 21:24:40 -07001195 void const* const buf = this->readInplace(PAD_SIZE(len));
Mathias Agopian98e71dd2010-02-11 17:30:52 -08001196 if (buf == NULL)
1197 return BAD_VALUE;
1198
1199 int* fds = NULL;
1200 if (fd_count) {
1201 fds = new int[fd_count];
1202 }
1203
1204 status_t err = NO_ERROR;
1205 for (size_t i=0 ; i<fd_count && err==NO_ERROR ; i++) {
1206 fds[i] = dup(this->readFileDescriptor());
1207 if (fds[i] < 0) err = BAD_VALUE;
1208 }
1209
1210 if (err == NO_ERROR) {
1211 err = val.unflatten(buf, len, fds, fd_count);
1212 }
1213
1214 if (fd_count) {
1215 delete [] fds;
1216 }
1217
1218 return err;
1219}
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001220const flat_binder_object* Parcel::readObject(bool nullMetaData) const
1221{
1222 const size_t DPOS = mDataPos;
1223 if ((DPOS+sizeof(flat_binder_object)) <= mDataSize) {
1224 const flat_binder_object* obj
1225 = reinterpret_cast<const flat_binder_object*>(mData+DPOS);
1226 mDataPos = DPOS + sizeof(flat_binder_object);
1227 if (!nullMetaData && (obj->cookie == NULL && obj->binder == NULL)) {
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001228 // When transferring a NULL object, we don't write it into
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001229 // the object list, so we don't want to check for it when
1230 // reading.
Steve Block6807e592011-10-20 11:56:00 +01001231 ALOGV("readObject Setting data pos of %p to %d\n", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001232 return obj;
1233 }
1234
1235 // Ensure that this object is valid...
1236 size_t* const OBJS = mObjects;
1237 const size_t N = mObjectsSize;
1238 size_t opos = mNextObjectHint;
1239
1240 if (N > 0) {
Steve Block6807e592011-10-20 11:56:00 +01001241 ALOGV("Parcel %p looking for obj at %d, hint=%d\n",
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001242 this, DPOS, opos);
1243
1244 // Start at the current hint position, looking for an object at
1245 // the current data position.
1246 if (opos < N) {
1247 while (opos < (N-1) && OBJS[opos] < DPOS) {
1248 opos++;
1249 }
1250 } else {
1251 opos = N-1;
1252 }
1253 if (OBJS[opos] == DPOS) {
1254 // Found it!
Steve Block6807e592011-10-20 11:56:00 +01001255 ALOGV("Parcel found obj %d at index %d with forward search",
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001256 this, DPOS, opos);
1257 mNextObjectHint = opos+1;
Steve Block6807e592011-10-20 11:56:00 +01001258 ALOGV("readObject Setting data pos of %p to %d\n", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001259 return obj;
1260 }
1261
1262 // Look backwards for it...
1263 while (opos > 0 && OBJS[opos] > DPOS) {
1264 opos--;
1265 }
1266 if (OBJS[opos] == DPOS) {
1267 // Found it!
Steve Block6807e592011-10-20 11:56:00 +01001268 ALOGV("Parcel found obj %d at index %d with backward search",
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001269 this, DPOS, opos);
1270 mNextObjectHint = opos+1;
Steve Block6807e592011-10-20 11:56:00 +01001271 ALOGV("readObject Setting data pos of %p to %d\n", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001272 return obj;
1273 }
1274 }
Steve Block32397c12012-01-05 23:22:43 +00001275 ALOGW("Attempt to read object from Parcel %p at offset %d that is not in the object list",
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001276 this, DPOS);
1277 }
1278 return NULL;
1279}
1280
1281void Parcel::closeFileDescriptors()
1282{
1283 size_t i = mObjectsSize;
1284 if (i > 0) {
Steve Blocka19954a2012-01-04 20:05:49 +00001285 //ALOGI("Closing file descriptors for %d objects...", mObjectsSize);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001286 }
1287 while (i > 0) {
1288 i--;
1289 const flat_binder_object* flat
1290 = reinterpret_cast<flat_binder_object*>(mData+mObjects[i]);
1291 if (flat->type == BINDER_TYPE_FD) {
Steve Blocka19954a2012-01-04 20:05:49 +00001292 //ALOGI("Closing fd: %ld\n", flat->handle);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001293 close(flat->handle);
1294 }
1295 }
1296}
1297
1298const uint8_t* Parcel::ipcData() const
1299{
1300 return mData;
1301}
1302
1303size_t Parcel::ipcDataSize() const
1304{
1305 return (mDataSize > mDataPos ? mDataSize : mDataPos);
1306}
1307
1308const size_t* Parcel::ipcObjects() const
1309{
1310 return mObjects;
1311}
1312
1313size_t Parcel::ipcObjectsCount() const
1314{
1315 return mObjectsSize;
1316}
1317
1318void Parcel::ipcSetDataReference(const uint8_t* data, size_t dataSize,
1319 const size_t* objects, size_t objectsCount, release_func relFunc, void* relCookie)
1320{
Arve Hjønnevågf50b9ea2014-02-13 19:22:08 -08001321 binder_size_t minOffset = 0;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001322 freeDataNoInit();
1323 mError = NO_ERROR;
1324 mData = const_cast<uint8_t*>(data);
1325 mDataSize = mDataCapacity = dataSize;
Steve Blocka19954a2012-01-04 20:05:49 +00001326 //ALOGI("setDataReference Setting data size of %p to %lu (pid=%d)\n", this, mDataSize, getpid());
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001327 mDataPos = 0;
Steve Block6807e592011-10-20 11:56:00 +01001328 ALOGV("setDataReference Setting data pos of %p to %d\n", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001329 mObjects = const_cast<size_t*>(objects);
1330 mObjectsSize = mObjectsCapacity = objectsCount;
1331 mNextObjectHint = 0;
1332 mOwner = relFunc;
1333 mOwnerCookie = relCookie;
Arve Hjønnevågf50b9ea2014-02-13 19:22:08 -08001334 for (size_t i = 0; i < mObjectsSize; i++) {
1335 binder_size_t offset = mObjects[i];
1336 if (offset < minOffset) {
1337 ALOGE("%s: bad object offset %"PRIu64" < %"PRIu64"\n",
1338 __func__, (uint64_t)offset, (uint64_t)minOffset);
1339 mObjectsSize = 0;
1340 break;
1341 }
1342 minOffset = offset + sizeof(flat_binder_object);
1343 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001344 scanForFds();
1345}
1346
1347void Parcel::print(TextOutput& to, uint32_t flags) const
1348{
1349 to << "Parcel(";
1350
1351 if (errorCheck() != NO_ERROR) {
1352 const status_t err = errorCheck();
1353 to << "Error: " << (void*)err << " \"" << strerror(-err) << "\"";
1354 } else if (dataSize() > 0) {
1355 const uint8_t* DATA = data();
1356 to << indent << HexDump(DATA, dataSize()) << dedent;
1357 const size_t* OBJS = objects();
1358 const size_t N = objectsCount();
1359 for (size_t i=0; i<N; i++) {
1360 const flat_binder_object* flat
1361 = reinterpret_cast<const flat_binder_object*>(DATA+OBJS[i]);
1362 to << endl << "Object #" << i << " @ " << (void*)OBJS[i] << ": "
1363 << TypeCode(flat->type & 0x7f7f7f00)
1364 << " = " << flat->binder;
1365 }
1366 } else {
1367 to << "NULL";
1368 }
1369
1370 to << ")";
1371}
1372
1373void Parcel::releaseObjects()
1374{
1375 const sp<ProcessState> proc(ProcessState::self());
1376 size_t i = mObjectsSize;
1377 uint8_t* const data = mData;
1378 size_t* const objects = mObjects;
1379 while (i > 0) {
1380 i--;
1381 const flat_binder_object* flat
1382 = reinterpret_cast<flat_binder_object*>(data+objects[i]);
1383 release_object(proc, *flat, this);
1384 }
1385}
1386
1387void Parcel::acquireObjects()
1388{
1389 const sp<ProcessState> proc(ProcessState::self());
1390 size_t i = mObjectsSize;
1391 uint8_t* const data = mData;
1392 size_t* const objects = mObjects;
1393 while (i > 0) {
1394 i--;
1395 const flat_binder_object* flat
1396 = reinterpret_cast<flat_binder_object*>(data+objects[i]);
1397 acquire_object(proc, *flat, this);
1398 }
1399}
1400
1401void Parcel::freeData()
1402{
1403 freeDataNoInit();
1404 initState();
1405}
1406
1407void Parcel::freeDataNoInit()
1408{
1409 if (mOwner) {
Steve Blocka19954a2012-01-04 20:05:49 +00001410 //ALOGI("Freeing data ref of %p (pid=%d)\n", this, getpid());
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001411 mOwner(this, mData, mDataSize, mObjects, mObjectsSize, mOwnerCookie);
1412 } else {
1413 releaseObjects();
1414 if (mData) free(mData);
1415 if (mObjects) free(mObjects);
1416 }
1417}
1418
1419status_t Parcel::growData(size_t len)
1420{
1421 size_t newSize = ((mDataSize+len)*3)/2;
1422 return (newSize <= mDataSize)
1423 ? (status_t) NO_MEMORY
1424 : continueWrite(newSize);
1425}
1426
1427status_t Parcel::restartWrite(size_t desired)
1428{
1429 if (mOwner) {
1430 freeData();
1431 return continueWrite(desired);
1432 }
1433
1434 uint8_t* data = (uint8_t*)realloc(mData, desired);
1435 if (!data && desired > mDataCapacity) {
1436 mError = NO_MEMORY;
1437 return NO_MEMORY;
1438 }
1439
1440 releaseObjects();
1441
1442 if (data) {
1443 mData = data;
1444 mDataCapacity = desired;
1445 }
1446
1447 mDataSize = mDataPos = 0;
Steve Block6807e592011-10-20 11:56:00 +01001448 ALOGV("restartWrite Setting data size of %p to %d\n", this, mDataSize);
1449 ALOGV("restartWrite Setting data pos of %p to %d\n", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001450
1451 free(mObjects);
1452 mObjects = NULL;
1453 mObjectsSize = mObjectsCapacity = 0;
1454 mNextObjectHint = 0;
1455 mHasFds = false;
1456 mFdsKnown = true;
Dianne Hackborn8938ed22011-09-28 23:19:47 -04001457 mAllowFds = true;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001458
1459 return NO_ERROR;
1460}
1461
1462status_t Parcel::continueWrite(size_t desired)
1463{
1464 // If shrinking, first adjust for any objects that appear
1465 // after the new data size.
1466 size_t objectsSize = mObjectsSize;
1467 if (desired < mDataSize) {
1468 if (desired == 0) {
1469 objectsSize = 0;
1470 } else {
1471 while (objectsSize > 0) {
1472 if (mObjects[objectsSize-1] < desired)
1473 break;
1474 objectsSize--;
1475 }
1476 }
1477 }
1478
1479 if (mOwner) {
1480 // If the size is going to zero, just release the owner's data.
1481 if (desired == 0) {
1482 freeData();
1483 return NO_ERROR;
1484 }
1485
1486 // If there is a different owner, we need to take
1487 // posession.
1488 uint8_t* data = (uint8_t*)malloc(desired);
1489 if (!data) {
1490 mError = NO_MEMORY;
1491 return NO_MEMORY;
1492 }
1493 size_t* objects = NULL;
1494
1495 if (objectsSize) {
1496 objects = (size_t*)malloc(objectsSize*sizeof(size_t));
1497 if (!objects) {
Hyejin Kim3f727c02013-03-09 11:28:54 +09001498 free(data);
1499
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001500 mError = NO_MEMORY;
1501 return NO_MEMORY;
1502 }
1503
1504 // Little hack to only acquire references on objects
1505 // we will be keeping.
1506 size_t oldObjectsSize = mObjectsSize;
1507 mObjectsSize = objectsSize;
1508 acquireObjects();
1509 mObjectsSize = oldObjectsSize;
1510 }
1511
1512 if (mData) {
1513 memcpy(data, mData, mDataSize < desired ? mDataSize : desired);
1514 }
1515 if (objects && mObjects) {
1516 memcpy(objects, mObjects, objectsSize*sizeof(size_t));
1517 }
Steve Blocka19954a2012-01-04 20:05:49 +00001518 //ALOGI("Freeing data ref of %p (pid=%d)\n", this, getpid());
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001519 mOwner(this, mData, mDataSize, mObjects, mObjectsSize, mOwnerCookie);
1520 mOwner = NULL;
1521
1522 mData = data;
1523 mObjects = objects;
1524 mDataSize = (mDataSize < desired) ? mDataSize : desired;
Steve Block6807e592011-10-20 11:56:00 +01001525 ALOGV("continueWrite Setting data size of %p to %d\n", this, mDataSize);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001526 mDataCapacity = desired;
1527 mObjectsSize = mObjectsCapacity = objectsSize;
1528 mNextObjectHint = 0;
1529
1530 } else if (mData) {
1531 if (objectsSize < mObjectsSize) {
1532 // Need to release refs on any objects we are dropping.
1533 const sp<ProcessState> proc(ProcessState::self());
1534 for (size_t i=objectsSize; i<mObjectsSize; i++) {
1535 const flat_binder_object* flat
1536 = reinterpret_cast<flat_binder_object*>(mData+mObjects[i]);
1537 if (flat->type == BINDER_TYPE_FD) {
1538 // will need to rescan because we may have lopped off the only FDs
1539 mFdsKnown = false;
1540 }
1541 release_object(proc, *flat, this);
1542 }
1543 size_t* objects =
1544 (size_t*)realloc(mObjects, objectsSize*sizeof(size_t));
1545 if (objects) {
1546 mObjects = objects;
1547 }
1548 mObjectsSize = objectsSize;
1549 mNextObjectHint = 0;
1550 }
1551
1552 // We own the data, so we can just do a realloc().
1553 if (desired > mDataCapacity) {
1554 uint8_t* data = (uint8_t*)realloc(mData, desired);
1555 if (data) {
1556 mData = data;
1557 mDataCapacity = desired;
1558 } else if (desired > mDataCapacity) {
1559 mError = NO_MEMORY;
1560 return NO_MEMORY;
1561 }
1562 } else {
Dianne Hackborn97e2bcd2011-04-13 18:15:56 -07001563 if (mDataSize > desired) {
1564 mDataSize = desired;
Steve Block6807e592011-10-20 11:56:00 +01001565 ALOGV("continueWrite Setting data size of %p to %d\n", this, mDataSize);
Dianne Hackborn97e2bcd2011-04-13 18:15:56 -07001566 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001567 if (mDataPos > desired) {
1568 mDataPos = desired;
Steve Block6807e592011-10-20 11:56:00 +01001569 ALOGV("continueWrite Setting data pos of %p to %d\n", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001570 }
1571 }
1572
1573 } else {
1574 // This is the first data. Easy!
1575 uint8_t* data = (uint8_t*)malloc(desired);
1576 if (!data) {
1577 mError = NO_MEMORY;
1578 return NO_MEMORY;
1579 }
Hyejin Kim3f727c02013-03-09 11:28:54 +09001580
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001581 if(!(mDataCapacity == 0 && mObjects == NULL
1582 && mObjectsCapacity == 0)) {
Steve Blocke6f43dd2012-01-06 19:20:56 +00001583 ALOGE("continueWrite: %d/%p/%d/%d", mDataCapacity, mObjects, mObjectsCapacity, desired);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001584 }
1585
1586 mData = data;
1587 mDataSize = mDataPos = 0;
Steve Block6807e592011-10-20 11:56:00 +01001588 ALOGV("continueWrite Setting data size of %p to %d\n", this, mDataSize);
1589 ALOGV("continueWrite Setting data pos of %p to %d\n", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001590 mDataCapacity = desired;
1591 }
1592
1593 return NO_ERROR;
1594}
1595
1596void Parcel::initState()
1597{
1598 mError = NO_ERROR;
1599 mData = 0;
1600 mDataSize = 0;
1601 mDataCapacity = 0;
1602 mDataPos = 0;
Steve Block6807e592011-10-20 11:56:00 +01001603 ALOGV("initState Setting data size of %p to %d\n", this, mDataSize);
1604 ALOGV("initState Setting data pos of %p to %d\n", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001605 mObjects = NULL;
1606 mObjectsSize = 0;
1607 mObjectsCapacity = 0;
1608 mNextObjectHint = 0;
1609 mHasFds = false;
1610 mFdsKnown = true;
Dianne Hackborn8938ed22011-09-28 23:19:47 -04001611 mAllowFds = true;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001612 mOwner = NULL;
1613}
1614
1615void Parcel::scanForFds() const
1616{
1617 bool hasFds = false;
1618 for (size_t i=0; i<mObjectsSize; i++) {
1619 const flat_binder_object* flat
1620 = reinterpret_cast<const flat_binder_object*>(mData + mObjects[i]);
1621 if (flat->type == BINDER_TYPE_FD) {
1622 hasFds = true;
1623 break;
1624 }
1625 }
1626 mHasFds = hasFds;
1627 mFdsKnown = true;
1628}
1629
Jeff Brown5707dbf2011-09-23 21:17:56 -07001630// --- Parcel::Blob ---
1631
1632Parcel::Blob::Blob() :
1633 mMapped(false), mData(NULL), mSize(0) {
1634}
1635
1636Parcel::Blob::~Blob() {
1637 release();
1638}
1639
1640void Parcel::Blob::release() {
1641 if (mMapped && mData) {
1642 ::munmap(mData, mSize);
1643 }
1644 clear();
1645}
1646
1647void Parcel::Blob::init(bool mapped, void* data, size_t size) {
1648 mMapped = mapped;
1649 mData = data;
1650 mSize = size;
1651}
1652
1653void Parcel::Blob::clear() {
1654 mMapped = false;
1655 mData = NULL;
1656 mSize = 0;
1657}
1658
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001659}; // namespace android