blob: e5a287157dc8e7fc69a328f5876e85721fbde7a5 [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}
Marco Nelissenf0190bf2014-03-13 14:17:40 -0700631status_t Parcel::writeByteArray(size_t len, const uint8_t *val) {
632 if (!val) {
633 return writeAligned(-1);
634 }
635 status_t ret = writeAligned(len);
636 if (ret == NO_ERROR) {
637 ret = write(val, len * sizeof(*val));
638 }
639 return ret;
640}
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700641
642status_t Parcel::writeInt64(int64_t val)
643{
Andreas Huber84a6d042009-08-17 13:33:27 -0700644 return writeAligned(val);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700645}
646
647status_t Parcel::writeFloat(float val)
648{
Andreas Huber84a6d042009-08-17 13:33:27 -0700649 return writeAligned(val);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700650}
651
Douglas Leungcc1a4bb2013-01-11 15:00:55 -0800652#if defined(__mips__) && defined(__mips_hard_float)
653
654status_t Parcel::writeDouble(double val)
655{
656 union {
657 double d;
658 unsigned long long ll;
659 } u;
660 u.d = val;
661 return writeAligned(u.ll);
662}
663
664#else
665
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700666status_t Parcel::writeDouble(double val)
667{
Andreas Huber84a6d042009-08-17 13:33:27 -0700668 return writeAligned(val);
669}
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700670
Douglas Leungcc1a4bb2013-01-11 15:00:55 -0800671#endif
672
Andreas Huber84a6d042009-08-17 13:33:27 -0700673status_t Parcel::writeIntPtr(intptr_t val)
674{
675 return writeAligned(val);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700676}
677
678status_t Parcel::writeCString(const char* str)
679{
680 return write(str, strlen(str)+1);
681}
682
683status_t Parcel::writeString8(const String8& str)
684{
685 status_t err = writeInt32(str.bytes());
Pravat Dalbeherad1dff8d2010-12-15 08:40:00 +0100686 // only write string if its length is more than zero characters,
687 // as readString8 will only read if the length field is non-zero.
688 // this is slightly different from how writeString16 works.
689 if (str.bytes() > 0 && err == NO_ERROR) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700690 err = write(str.string(), str.bytes()+1);
691 }
692 return err;
693}
694
695status_t Parcel::writeString16(const String16& str)
696{
697 return writeString16(str.string(), str.size());
698}
699
700status_t Parcel::writeString16(const char16_t* str, size_t len)
701{
702 if (str == NULL) return writeInt32(-1);
703
704 status_t err = writeInt32(len);
705 if (err == NO_ERROR) {
706 len *= sizeof(char16_t);
707 uint8_t* data = (uint8_t*)writeInplace(len+sizeof(char16_t));
708 if (data) {
709 memcpy(data, str, len);
710 *reinterpret_cast<char16_t*>(data+len) = 0;
711 return NO_ERROR;
712 }
713 err = mError;
714 }
715 return err;
716}
717
718status_t Parcel::writeStrongBinder(const sp<IBinder>& val)
719{
720 return flatten_binder(ProcessState::self(), val, this);
721}
722
723status_t Parcel::writeWeakBinder(const wp<IBinder>& val)
724{
725 return flatten_binder(ProcessState::self(), val, this);
726}
727
Mathias Agopiana47f02a2009-05-21 16:29:38 -0700728status_t Parcel::writeNativeHandle(const native_handle* handle)
The Android Open Source Project5f78a482009-01-20 14:03:58 -0800729{
Mathias Agopian1d0a95b2009-07-31 16:12:13 -0700730 if (!handle || handle->version != sizeof(native_handle))
The Android Open Source Project5f78a482009-01-20 14:03:58 -0800731 return BAD_TYPE;
732
733 status_t err;
Mathias Agopiana47f02a2009-05-21 16:29:38 -0700734 err = writeInt32(handle->numFds);
The Android Open Source Project5f78a482009-01-20 14:03:58 -0800735 if (err != NO_ERROR) return err;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800736
Mathias Agopiana47f02a2009-05-21 16:29:38 -0700737 err = writeInt32(handle->numInts);
The Android Open Source Project5f78a482009-01-20 14:03:58 -0800738 if (err != NO_ERROR) return err;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800739
Mathias Agopiana47f02a2009-05-21 16:29:38 -0700740 for (int i=0 ; err==NO_ERROR && i<handle->numFds ; i++)
741 err = writeDupFileDescriptor(handle->data[i]);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800742
743 if (err != NO_ERROR) {
Steve Block9d453682011-12-20 16:23:08 +0000744 ALOGD("write native handle, write dup fd failed");
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800745 return err;
746 }
Mathias Agopiana47f02a2009-05-21 16:29:38 -0700747 err = write(handle->data + handle->numFds, sizeof(int)*handle->numInts);
The Android Open Source Project5f78a482009-01-20 14:03:58 -0800748 return err;
749}
750
Jeff Brown93ff1f92011-11-04 19:01:44 -0700751status_t Parcel::writeFileDescriptor(int fd, bool takeOwnership)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700752{
753 flat_binder_object obj;
754 obj.type = BINDER_TYPE_FD;
755 obj.flags = 0x7f | FLAT_BINDER_FLAG_ACCEPTS_FDS;
756 obj.handle = fd;
Jeff Brown93ff1f92011-11-04 19:01:44 -0700757 obj.cookie = (void*) (takeOwnership ? 1 : 0);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700758 return writeObject(obj, true);
759}
760
761status_t Parcel::writeDupFileDescriptor(int fd)
762{
Jeff Brownd341c712011-11-04 20:19:33 -0700763 int dupFd = dup(fd);
764 if (dupFd < 0) {
765 return -errno;
766 }
767 status_t err = writeFileDescriptor(dupFd, true /*takeOwnership*/);
768 if (err) {
769 close(dupFd);
770 }
771 return err;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700772}
773
Jeff Brown5707dbf2011-09-23 21:17:56 -0700774status_t Parcel::writeBlob(size_t len, WritableBlob* outBlob)
775{
776 status_t status;
777
778 if (!mAllowFds || len <= IN_PLACE_BLOB_LIMIT) {
Steve Block6807e592011-10-20 11:56:00 +0100779 ALOGV("writeBlob: write in place");
Jeff Brown5707dbf2011-09-23 21:17:56 -0700780 status = writeInt32(0);
781 if (status) return status;
782
783 void* ptr = writeInplace(len);
784 if (!ptr) return NO_MEMORY;
785
786 outBlob->init(false /*mapped*/, ptr, len);
787 return NO_ERROR;
788 }
789
Steve Block6807e592011-10-20 11:56:00 +0100790 ALOGV("writeBlob: write to ashmem");
Jeff Brown5707dbf2011-09-23 21:17:56 -0700791 int fd = ashmem_create_region("Parcel Blob", len);
792 if (fd < 0) return NO_MEMORY;
793
794 int result = ashmem_set_prot_region(fd, PROT_READ | PROT_WRITE);
795 if (result < 0) {
Jeff Brownec4e0062011-10-10 14:50:10 -0700796 status = result;
Jeff Brown5707dbf2011-09-23 21:17:56 -0700797 } else {
798 void* ptr = ::mmap(NULL, len, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
799 if (ptr == MAP_FAILED) {
800 status = -errno;
801 } else {
802 result = ashmem_set_prot_region(fd, PROT_READ);
803 if (result < 0) {
Jeff Brownec4e0062011-10-10 14:50:10 -0700804 status = result;
Jeff Brown5707dbf2011-09-23 21:17:56 -0700805 } else {
806 status = writeInt32(1);
807 if (!status) {
Jeff Brown93ff1f92011-11-04 19:01:44 -0700808 status = writeFileDescriptor(fd, true /*takeOwnership*/);
Jeff Brown5707dbf2011-09-23 21:17:56 -0700809 if (!status) {
810 outBlob->init(true /*mapped*/, ptr, len);
811 return NO_ERROR;
812 }
813 }
814 }
815 }
816 ::munmap(ptr, len);
817 }
818 ::close(fd);
819 return status;
820}
821
Mathias Agopiane1424282013-07-29 21:24:40 -0700822status_t Parcel::write(const FlattenableHelperInterface& val)
Mathias Agopian98e71dd2010-02-11 17:30:52 -0800823{
824 status_t err;
825
826 // size if needed
Mathias Agopiane1424282013-07-29 21:24:40 -0700827 const size_t len = val.getFlattenedSize();
828 const size_t fd_count = val.getFdCount();
Mathias Agopian98e71dd2010-02-11 17:30:52 -0800829
830 err = this->writeInt32(len);
831 if (err) return err;
832
833 err = this->writeInt32(fd_count);
834 if (err) return err;
835
836 // payload
Mathias Agopiane1424282013-07-29 21:24:40 -0700837 void* const buf = this->writeInplace(PAD_SIZE(len));
Mathias Agopian98e71dd2010-02-11 17:30:52 -0800838 if (buf == NULL)
839 return BAD_VALUE;
840
841 int* fds = NULL;
842 if (fd_count) {
843 fds = new int[fd_count];
844 }
845
846 err = val.flatten(buf, len, fds, fd_count);
847 for (size_t i=0 ; i<fd_count && err==NO_ERROR ; i++) {
848 err = this->writeDupFileDescriptor( fds[i] );
849 }
850
851 if (fd_count) {
852 delete [] fds;
853 }
854
855 return err;
856}
857
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700858status_t Parcel::writeObject(const flat_binder_object& val, bool nullMetaData)
859{
860 const bool enoughData = (mDataPos+sizeof(val)) <= mDataCapacity;
861 const bool enoughObjects = mObjectsSize < mObjectsCapacity;
862 if (enoughData && enoughObjects) {
863restart_write:
864 *reinterpret_cast<flat_binder_object*>(mData+mDataPos) = val;
865
866 // Need to write meta-data?
867 if (nullMetaData || val.binder != NULL) {
868 mObjects[mObjectsSize] = mDataPos;
869 acquire_object(ProcessState::self(), val, this);
870 mObjectsSize++;
871 }
872
873 // remember if it's a file descriptor
874 if (val.type == BINDER_TYPE_FD) {
Dianne Hackborn8938ed22011-09-28 23:19:47 -0400875 if (!mAllowFds) {
876 return FDS_NOT_ALLOWED;
877 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700878 mHasFds = mFdsKnown = true;
879 }
880
881 return finishWrite(sizeof(flat_binder_object));
882 }
883
884 if (!enoughData) {
885 const status_t err = growData(sizeof(val));
886 if (err != NO_ERROR) return err;
887 }
888 if (!enoughObjects) {
889 size_t newSize = ((mObjectsSize+2)*3)/2;
890 size_t* objects = (size_t*)realloc(mObjects, newSize*sizeof(size_t));
891 if (objects == NULL) return NO_MEMORY;
892 mObjects = objects;
893 mObjectsCapacity = newSize;
894 }
895
896 goto restart_write;
897}
898
Brad Fitzpatrick837a0d02010-07-13 15:33:35 -0700899status_t Parcel::writeNoException()
900{
901 return writeInt32(0);
902}
903
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700904void Parcel::remove(size_t start, size_t amt)
905{
906 LOG_ALWAYS_FATAL("Parcel::remove() not yet implemented!");
907}
908
909status_t Parcel::read(void* outData, size_t len) const
910{
Kenny Root5b61ad22014-03-17 13:18:16 -0700911 if ((mDataPos+PAD_SIZE(len)) >= mDataPos && (mDataPos+PAD_SIZE(len)) <= mDataSize
912 && len <= PAD_SIZE(len)) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700913 memcpy(outData, mData+mDataPos, len);
914 mDataPos += PAD_SIZE(len);
Steve Block6807e592011-10-20 11:56:00 +0100915 ALOGV("read Setting data pos of %p to %d\n", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700916 return NO_ERROR;
917 }
918 return NOT_ENOUGH_DATA;
919}
920
921const void* Parcel::readInplace(size_t len) const
922{
Kenny Root5b61ad22014-03-17 13:18:16 -0700923 if ((mDataPos+PAD_SIZE(len)) >= mDataPos && (mDataPos+PAD_SIZE(len)) <= mDataSize
924 && len <= PAD_SIZE(len)) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700925 const void* data = mData+mDataPos;
926 mDataPos += PAD_SIZE(len);
Steve Block6807e592011-10-20 11:56:00 +0100927 ALOGV("readInplace Setting data pos of %p to %d\n", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700928 return data;
929 }
930 return NULL;
931}
932
Andreas Huber84a6d042009-08-17 13:33:27 -0700933template<class T>
934status_t Parcel::readAligned(T *pArg) const {
935 COMPILE_TIME_ASSERT_FUNCTION_SCOPE(PAD_SIZE(sizeof(T)) == sizeof(T));
936
937 if ((mDataPos+sizeof(T)) <= mDataSize) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700938 const void* data = mData+mDataPos;
Andreas Huber84a6d042009-08-17 13:33:27 -0700939 mDataPos += sizeof(T);
940 *pArg = *reinterpret_cast<const T*>(data);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700941 return NO_ERROR;
942 } else {
943 return NOT_ENOUGH_DATA;
944 }
945}
946
Andreas Huber84a6d042009-08-17 13:33:27 -0700947template<class T>
948T Parcel::readAligned() const {
949 T result;
950 if (readAligned(&result) != NO_ERROR) {
951 result = 0;
952 }
953
954 return result;
955}
956
957template<class T>
958status_t Parcel::writeAligned(T val) {
959 COMPILE_TIME_ASSERT_FUNCTION_SCOPE(PAD_SIZE(sizeof(T)) == sizeof(T));
960
961 if ((mDataPos+sizeof(val)) <= mDataCapacity) {
962restart_write:
963 *reinterpret_cast<T*>(mData+mDataPos) = val;
964 return finishWrite(sizeof(val));
965 }
966
967 status_t err = growData(sizeof(val));
968 if (err == NO_ERROR) goto restart_write;
969 return err;
970}
971
972status_t Parcel::readInt32(int32_t *pArg) const
973{
974 return readAligned(pArg);
975}
976
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700977int32_t Parcel::readInt32() const
978{
Andreas Huber84a6d042009-08-17 13:33:27 -0700979 return readAligned<int32_t>();
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700980}
981
982
983status_t Parcel::readInt64(int64_t *pArg) const
984{
Andreas Huber84a6d042009-08-17 13:33:27 -0700985 return readAligned(pArg);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700986}
987
988
989int64_t Parcel::readInt64() const
990{
Andreas Huber84a6d042009-08-17 13:33:27 -0700991 return readAligned<int64_t>();
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700992}
993
994status_t Parcel::readFloat(float *pArg) const
995{
Andreas Huber84a6d042009-08-17 13:33:27 -0700996 return readAligned(pArg);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700997}
998
999
1000float Parcel::readFloat() const
1001{
Andreas Huber84a6d042009-08-17 13:33:27 -07001002 return readAligned<float>();
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001003}
1004
Douglas Leungcc1a4bb2013-01-11 15:00:55 -08001005#if defined(__mips__) && defined(__mips_hard_float)
1006
1007status_t Parcel::readDouble(double *pArg) const
1008{
1009 union {
1010 double d;
1011 unsigned long long ll;
1012 } u;
1013 status_t status;
1014 status = readAligned(&u.ll);
1015 *pArg = u.d;
1016 return status;
1017}
1018
1019double Parcel::readDouble() const
1020{
1021 union {
1022 double d;
1023 unsigned long long ll;
1024 } u;
1025 u.ll = readAligned<unsigned long long>();
1026 return u.d;
1027}
1028
1029#else
1030
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001031status_t Parcel::readDouble(double *pArg) const
1032{
Andreas Huber84a6d042009-08-17 13:33:27 -07001033 return readAligned(pArg);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001034}
1035
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001036double Parcel::readDouble() const
1037{
Andreas Huber84a6d042009-08-17 13:33:27 -07001038 return readAligned<double>();
1039}
1040
Douglas Leungcc1a4bb2013-01-11 15:00:55 -08001041#endif
1042
Andreas Huber84a6d042009-08-17 13:33:27 -07001043status_t Parcel::readIntPtr(intptr_t *pArg) const
1044{
1045 return readAligned(pArg);
1046}
1047
1048
1049intptr_t Parcel::readIntPtr() const
1050{
1051 return readAligned<intptr_t>();
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001052}
1053
1054
1055const char* Parcel::readCString() const
1056{
1057 const size_t avail = mDataSize-mDataPos;
1058 if (avail > 0) {
1059 const char* str = reinterpret_cast<const char*>(mData+mDataPos);
1060 // is the string's trailing NUL within the parcel's valid bounds?
1061 const char* eos = reinterpret_cast<const char*>(memchr(str, 0, avail));
1062 if (eos) {
1063 const size_t len = eos - str;
1064 mDataPos += PAD_SIZE(len+1);
Steve Block6807e592011-10-20 11:56:00 +01001065 ALOGV("readCString Setting data pos of %p to %d\n", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001066 return str;
1067 }
1068 }
1069 return NULL;
1070}
1071
1072String8 Parcel::readString8() const
1073{
1074 int32_t size = readInt32();
1075 // watch for potential int overflow adding 1 for trailing NUL
1076 if (size > 0 && size < INT32_MAX) {
1077 const char* str = (const char*)readInplace(size+1);
1078 if (str) return String8(str, size);
1079 }
1080 return String8();
1081}
1082
1083String16 Parcel::readString16() const
1084{
1085 size_t len;
1086 const char16_t* str = readString16Inplace(&len);
1087 if (str) return String16(str, len);
Steve Blocke6f43dd2012-01-06 19:20:56 +00001088 ALOGE("Reading a NULL string not supported here.");
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001089 return String16();
1090}
1091
1092const char16_t* Parcel::readString16Inplace(size_t* outLen) const
1093{
1094 int32_t size = readInt32();
1095 // watch for potential int overflow from size+1
1096 if (size >= 0 && size < INT32_MAX) {
1097 *outLen = size;
1098 const char16_t* str = (const char16_t*)readInplace((size+1)*sizeof(char16_t));
1099 if (str != NULL) {
1100 return str;
1101 }
1102 }
1103 *outLen = 0;
1104 return NULL;
1105}
1106
1107sp<IBinder> Parcel::readStrongBinder() const
1108{
1109 sp<IBinder> val;
1110 unflatten_binder(ProcessState::self(), *this, &val);
1111 return val;
1112}
1113
1114wp<IBinder> Parcel::readWeakBinder() const
1115{
1116 wp<IBinder> val;
1117 unflatten_binder(ProcessState::self(), *this, &val);
1118 return val;
1119}
1120
Brad Fitzpatrick837a0d02010-07-13 15:33:35 -07001121int32_t Parcel::readExceptionCode() const
1122{
1123 int32_t exception_code = readAligned<int32_t>();
Brad Fitzpatrickd36f4a52010-07-12 11:05:38 -07001124 if (exception_code == EX_HAS_REPLY_HEADER) {
Magnus Strandberg1ba24572011-05-03 15:44:00 +02001125 int32_t header_start = dataPosition();
Brad Fitzpatrickd36f4a52010-07-12 11:05:38 -07001126 int32_t header_size = readAligned<int32_t>();
1127 // Skip over fat responses headers. Not used (or propagated) in
1128 // native code
Magnus Strandberg1ba24572011-05-03 15:44:00 +02001129 setDataPosition(header_start + header_size);
Brad Fitzpatrickd36f4a52010-07-12 11:05:38 -07001130 // And fat response headers are currently only used when there are no
1131 // exceptions, so return no error:
1132 return 0;
1133 }
Brad Fitzpatrick837a0d02010-07-13 15:33:35 -07001134 return exception_code;
1135}
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001136
Mathias Agopiana47f02a2009-05-21 16:29:38 -07001137native_handle* Parcel::readNativeHandle() const
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001138{
1139 int numFds, numInts;
1140 status_t err;
1141 err = readInt32(&numFds);
1142 if (err != NO_ERROR) return 0;
1143 err = readInt32(&numInts);
1144 if (err != NO_ERROR) return 0;
1145
Mathias Agopiana47f02a2009-05-21 16:29:38 -07001146 native_handle* h = native_handle_create(numFds, numInts);
Adam Lesinski4ff0cb42015-05-12 17:35:48 -07001147 if (!h) {
1148 return 0;
1149 }
1150
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001151 for (int i=0 ; err==NO_ERROR && i<numFds ; i++) {
Rebecca Schultz Zavin360211f2009-02-13 16:34:38 -08001152 h->data[i] = dup(readFileDescriptor());
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001153 if (h->data[i] < 0) err = BAD_VALUE;
1154 }
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001155 err = read(h->data + numFds, sizeof(int)*numInts);
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001156 if (err != NO_ERROR) {
Mathias Agopiana47f02a2009-05-21 16:29:38 -07001157 native_handle_close(h);
1158 native_handle_delete(h);
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001159 h = 0;
1160 }
1161 return h;
1162}
1163
1164
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001165int Parcel::readFileDescriptor() const
1166{
1167 const flat_binder_object* flat = readObject(true);
1168 if (flat) {
1169 switch (flat->type) {
1170 case BINDER_TYPE_FD:
Steve Blocka19954a2012-01-04 20:05:49 +00001171 //ALOGI("Returning file descriptor %ld from parcel %p\n", flat->handle, this);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001172 return flat->handle;
1173 }
1174 }
1175 return BAD_TYPE;
1176}
1177
Jeff Brown5707dbf2011-09-23 21:17:56 -07001178status_t Parcel::readBlob(size_t len, ReadableBlob* outBlob) const
1179{
1180 int32_t useAshmem;
1181 status_t status = readInt32(&useAshmem);
1182 if (status) return status;
1183
1184 if (!useAshmem) {
Steve Block6807e592011-10-20 11:56:00 +01001185 ALOGV("readBlob: read in place");
Jeff Brown5707dbf2011-09-23 21:17:56 -07001186 const void* ptr = readInplace(len);
1187 if (!ptr) return BAD_VALUE;
1188
1189 outBlob->init(false /*mapped*/, const_cast<void*>(ptr), len);
1190 return NO_ERROR;
1191 }
1192
Steve Block6807e592011-10-20 11:56:00 +01001193 ALOGV("readBlob: read from ashmem");
Jeff Brown5707dbf2011-09-23 21:17:56 -07001194 int fd = readFileDescriptor();
1195 if (fd == int(BAD_TYPE)) return BAD_VALUE;
1196
1197 void* ptr = ::mmap(NULL, len, PROT_READ, MAP_SHARED, fd, 0);
1198 if (!ptr) return NO_MEMORY;
1199
1200 outBlob->init(true /*mapped*/, ptr, len);
1201 return NO_ERROR;
1202}
1203
Mathias Agopiane1424282013-07-29 21:24:40 -07001204status_t Parcel::read(FlattenableHelperInterface& val) const
Mathias Agopian98e71dd2010-02-11 17:30:52 -08001205{
1206 // size
1207 const size_t len = this->readInt32();
1208 const size_t fd_count = this->readInt32();
1209
1210 // payload
Mathias Agopiane1424282013-07-29 21:24:40 -07001211 void const* const buf = this->readInplace(PAD_SIZE(len));
Mathias Agopian98e71dd2010-02-11 17:30:52 -08001212 if (buf == NULL)
1213 return BAD_VALUE;
1214
1215 int* fds = NULL;
1216 if (fd_count) {
1217 fds = new int[fd_count];
1218 }
1219
1220 status_t err = NO_ERROR;
1221 for (size_t i=0 ; i<fd_count && err==NO_ERROR ; i++) {
1222 fds[i] = dup(this->readFileDescriptor());
1223 if (fds[i] < 0) err = BAD_VALUE;
1224 }
1225
1226 if (err == NO_ERROR) {
1227 err = val.unflatten(buf, len, fds, fd_count);
1228 }
1229
1230 if (fd_count) {
1231 delete [] fds;
1232 }
1233
1234 return err;
1235}
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001236const flat_binder_object* Parcel::readObject(bool nullMetaData) const
1237{
1238 const size_t DPOS = mDataPos;
1239 if ((DPOS+sizeof(flat_binder_object)) <= mDataSize) {
1240 const flat_binder_object* obj
1241 = reinterpret_cast<const flat_binder_object*>(mData+DPOS);
1242 mDataPos = DPOS + sizeof(flat_binder_object);
1243 if (!nullMetaData && (obj->cookie == NULL && obj->binder == NULL)) {
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001244 // When transferring a NULL object, we don't write it into
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001245 // the object list, so we don't want to check for it when
1246 // reading.
Steve Block6807e592011-10-20 11:56:00 +01001247 ALOGV("readObject Setting data pos of %p to %d\n", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001248 return obj;
1249 }
1250
1251 // Ensure that this object is valid...
1252 size_t* const OBJS = mObjects;
1253 const size_t N = mObjectsSize;
1254 size_t opos = mNextObjectHint;
1255
1256 if (N > 0) {
Steve Block6807e592011-10-20 11:56:00 +01001257 ALOGV("Parcel %p looking for obj at %d, hint=%d\n",
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001258 this, DPOS, opos);
1259
1260 // Start at the current hint position, looking for an object at
1261 // the current data position.
1262 if (opos < N) {
1263 while (opos < (N-1) && OBJS[opos] < DPOS) {
1264 opos++;
1265 }
1266 } else {
1267 opos = N-1;
1268 }
1269 if (OBJS[opos] == DPOS) {
1270 // Found it!
Steve Block6807e592011-10-20 11:56:00 +01001271 ALOGV("Parcel found obj %d at index %d with forward search",
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001272 this, DPOS, opos);
1273 mNextObjectHint = opos+1;
Steve Block6807e592011-10-20 11:56:00 +01001274 ALOGV("readObject Setting data pos of %p to %d\n", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001275 return obj;
1276 }
1277
1278 // Look backwards for it...
1279 while (opos > 0 && OBJS[opos] > DPOS) {
1280 opos--;
1281 }
1282 if (OBJS[opos] == DPOS) {
1283 // Found it!
Steve Block6807e592011-10-20 11:56:00 +01001284 ALOGV("Parcel found obj %d at index %d with backward search",
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001285 this, DPOS, opos);
1286 mNextObjectHint = opos+1;
Steve Block6807e592011-10-20 11:56:00 +01001287 ALOGV("readObject Setting data pos of %p to %d\n", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001288 return obj;
1289 }
1290 }
Steve Block32397c12012-01-05 23:22:43 +00001291 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 -07001292 this, DPOS);
1293 }
1294 return NULL;
1295}
1296
1297void Parcel::closeFileDescriptors()
1298{
1299 size_t i = mObjectsSize;
1300 if (i > 0) {
Steve Blocka19954a2012-01-04 20:05:49 +00001301 //ALOGI("Closing file descriptors for %d objects...", mObjectsSize);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001302 }
1303 while (i > 0) {
1304 i--;
1305 const flat_binder_object* flat
1306 = reinterpret_cast<flat_binder_object*>(mData+mObjects[i]);
1307 if (flat->type == BINDER_TYPE_FD) {
Steve Blocka19954a2012-01-04 20:05:49 +00001308 //ALOGI("Closing fd: %ld\n", flat->handle);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001309 close(flat->handle);
1310 }
1311 }
1312}
1313
1314const uint8_t* Parcel::ipcData() const
1315{
1316 return mData;
1317}
1318
1319size_t Parcel::ipcDataSize() const
1320{
1321 return (mDataSize > mDataPos ? mDataSize : mDataPos);
1322}
1323
1324const size_t* Parcel::ipcObjects() const
1325{
1326 return mObjects;
1327}
1328
1329size_t Parcel::ipcObjectsCount() const
1330{
1331 return mObjectsSize;
1332}
1333
1334void Parcel::ipcSetDataReference(const uint8_t* data, size_t dataSize,
1335 const size_t* objects, size_t objectsCount, release_func relFunc, void* relCookie)
1336{
Arve Hjønnevåg67903292014-02-19 15:35:52 -08001337 size_t minOffset = 0;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001338 freeDataNoInit();
1339 mError = NO_ERROR;
1340 mData = const_cast<uint8_t*>(data);
1341 mDataSize = mDataCapacity = dataSize;
Steve Blocka19954a2012-01-04 20:05:49 +00001342 //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 -07001343 mDataPos = 0;
Steve Block6807e592011-10-20 11:56:00 +01001344 ALOGV("setDataReference Setting data pos of %p to %d\n", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001345 mObjects = const_cast<size_t*>(objects);
1346 mObjectsSize = mObjectsCapacity = objectsCount;
1347 mNextObjectHint = 0;
1348 mOwner = relFunc;
1349 mOwnerCookie = relCookie;
Arve Hjønnevågf50b9ea2014-02-13 19:22:08 -08001350 for (size_t i = 0; i < mObjectsSize; i++) {
Arve Hjønnevåg67903292014-02-19 15:35:52 -08001351 size_t offset = mObjects[i];
Arve Hjønnevågf50b9ea2014-02-13 19:22:08 -08001352 if (offset < minOffset) {
Arve Hjønnevåg67903292014-02-19 15:35:52 -08001353 ALOGE("%s: bad object offset %zu < %zu\n",
1354 __func__, offset, minOffset);
Arve Hjønnevågf50b9ea2014-02-13 19:22:08 -08001355 mObjectsSize = 0;
1356 break;
1357 }
1358 minOffset = offset + sizeof(flat_binder_object);
1359 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001360 scanForFds();
1361}
1362
1363void Parcel::print(TextOutput& to, uint32_t flags) const
1364{
1365 to << "Parcel(";
1366
1367 if (errorCheck() != NO_ERROR) {
1368 const status_t err = errorCheck();
1369 to << "Error: " << (void*)err << " \"" << strerror(-err) << "\"";
1370 } else if (dataSize() > 0) {
1371 const uint8_t* DATA = data();
1372 to << indent << HexDump(DATA, dataSize()) << dedent;
1373 const size_t* OBJS = objects();
1374 const size_t N = objectsCount();
1375 for (size_t i=0; i<N; i++) {
1376 const flat_binder_object* flat
1377 = reinterpret_cast<const flat_binder_object*>(DATA+OBJS[i]);
1378 to << endl << "Object #" << i << " @ " << (void*)OBJS[i] << ": "
1379 << TypeCode(flat->type & 0x7f7f7f00)
1380 << " = " << flat->binder;
1381 }
1382 } else {
1383 to << "NULL";
1384 }
1385
1386 to << ")";
1387}
1388
1389void Parcel::releaseObjects()
1390{
1391 const sp<ProcessState> proc(ProcessState::self());
1392 size_t i = mObjectsSize;
1393 uint8_t* const data = mData;
1394 size_t* const objects = mObjects;
1395 while (i > 0) {
1396 i--;
1397 const flat_binder_object* flat
1398 = reinterpret_cast<flat_binder_object*>(data+objects[i]);
1399 release_object(proc, *flat, this);
1400 }
1401}
1402
1403void Parcel::acquireObjects()
1404{
1405 const sp<ProcessState> proc(ProcessState::self());
1406 size_t i = mObjectsSize;
1407 uint8_t* const data = mData;
1408 size_t* const objects = mObjects;
1409 while (i > 0) {
1410 i--;
1411 const flat_binder_object* flat
1412 = reinterpret_cast<flat_binder_object*>(data+objects[i]);
1413 acquire_object(proc, *flat, this);
1414 }
1415}
1416
1417void Parcel::freeData()
1418{
1419 freeDataNoInit();
1420 initState();
1421}
1422
1423void Parcel::freeDataNoInit()
1424{
1425 if (mOwner) {
Steve Blocka19954a2012-01-04 20:05:49 +00001426 //ALOGI("Freeing data ref of %p (pid=%d)\n", this, getpid());
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001427 mOwner(this, mData, mDataSize, mObjects, mObjectsSize, mOwnerCookie);
1428 } else {
1429 releaseObjects();
1430 if (mData) free(mData);
1431 if (mObjects) free(mObjects);
1432 }
1433}
1434
1435status_t Parcel::growData(size_t len)
1436{
1437 size_t newSize = ((mDataSize+len)*3)/2;
1438 return (newSize <= mDataSize)
1439 ? (status_t) NO_MEMORY
1440 : continueWrite(newSize);
1441}
1442
1443status_t Parcel::restartWrite(size_t desired)
1444{
1445 if (mOwner) {
1446 freeData();
1447 return continueWrite(desired);
1448 }
1449
1450 uint8_t* data = (uint8_t*)realloc(mData, desired);
1451 if (!data && desired > mDataCapacity) {
1452 mError = NO_MEMORY;
1453 return NO_MEMORY;
1454 }
1455
1456 releaseObjects();
1457
1458 if (data) {
1459 mData = data;
1460 mDataCapacity = desired;
1461 }
1462
1463 mDataSize = mDataPos = 0;
Steve Block6807e592011-10-20 11:56:00 +01001464 ALOGV("restartWrite Setting data size of %p to %d\n", this, mDataSize);
1465 ALOGV("restartWrite Setting data pos of %p to %d\n", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001466
1467 free(mObjects);
1468 mObjects = NULL;
1469 mObjectsSize = mObjectsCapacity = 0;
1470 mNextObjectHint = 0;
1471 mHasFds = false;
1472 mFdsKnown = true;
Dianne Hackborn8938ed22011-09-28 23:19:47 -04001473 mAllowFds = true;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001474
1475 return NO_ERROR;
1476}
1477
1478status_t Parcel::continueWrite(size_t desired)
1479{
1480 // If shrinking, first adjust for any objects that appear
1481 // after the new data size.
1482 size_t objectsSize = mObjectsSize;
1483 if (desired < mDataSize) {
1484 if (desired == 0) {
1485 objectsSize = 0;
1486 } else {
1487 while (objectsSize > 0) {
1488 if (mObjects[objectsSize-1] < desired)
1489 break;
1490 objectsSize--;
1491 }
1492 }
1493 }
1494
1495 if (mOwner) {
1496 // If the size is going to zero, just release the owner's data.
1497 if (desired == 0) {
1498 freeData();
1499 return NO_ERROR;
1500 }
1501
1502 // If there is a different owner, we need to take
1503 // posession.
1504 uint8_t* data = (uint8_t*)malloc(desired);
1505 if (!data) {
1506 mError = NO_MEMORY;
1507 return NO_MEMORY;
1508 }
1509 size_t* objects = NULL;
1510
1511 if (objectsSize) {
1512 objects = (size_t*)malloc(objectsSize*sizeof(size_t));
1513 if (!objects) {
Hyejin Kim3f727c02013-03-09 11:28:54 +09001514 free(data);
1515
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001516 mError = NO_MEMORY;
1517 return NO_MEMORY;
1518 }
1519
1520 // Little hack to only acquire references on objects
1521 // we will be keeping.
1522 size_t oldObjectsSize = mObjectsSize;
1523 mObjectsSize = objectsSize;
1524 acquireObjects();
1525 mObjectsSize = oldObjectsSize;
1526 }
1527
1528 if (mData) {
1529 memcpy(data, mData, mDataSize < desired ? mDataSize : desired);
1530 }
1531 if (objects && mObjects) {
1532 memcpy(objects, mObjects, objectsSize*sizeof(size_t));
1533 }
Steve Blocka19954a2012-01-04 20:05:49 +00001534 //ALOGI("Freeing data ref of %p (pid=%d)\n", this, getpid());
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001535 mOwner(this, mData, mDataSize, mObjects, mObjectsSize, mOwnerCookie);
1536 mOwner = NULL;
1537
1538 mData = data;
1539 mObjects = objects;
1540 mDataSize = (mDataSize < desired) ? mDataSize : desired;
Steve Block6807e592011-10-20 11:56:00 +01001541 ALOGV("continueWrite Setting data size of %p to %d\n", this, mDataSize);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001542 mDataCapacity = desired;
1543 mObjectsSize = mObjectsCapacity = objectsSize;
1544 mNextObjectHint = 0;
1545
1546 } else if (mData) {
1547 if (objectsSize < mObjectsSize) {
1548 // Need to release refs on any objects we are dropping.
1549 const sp<ProcessState> proc(ProcessState::self());
1550 for (size_t i=objectsSize; i<mObjectsSize; i++) {
1551 const flat_binder_object* flat
1552 = reinterpret_cast<flat_binder_object*>(mData+mObjects[i]);
1553 if (flat->type == BINDER_TYPE_FD) {
1554 // will need to rescan because we may have lopped off the only FDs
1555 mFdsKnown = false;
1556 }
1557 release_object(proc, *flat, this);
1558 }
1559 size_t* objects =
1560 (size_t*)realloc(mObjects, objectsSize*sizeof(size_t));
1561 if (objects) {
1562 mObjects = objects;
1563 }
1564 mObjectsSize = objectsSize;
1565 mNextObjectHint = 0;
1566 }
1567
1568 // We own the data, so we can just do a realloc().
1569 if (desired > mDataCapacity) {
1570 uint8_t* data = (uint8_t*)realloc(mData, desired);
1571 if (data) {
1572 mData = data;
1573 mDataCapacity = desired;
1574 } else if (desired > mDataCapacity) {
1575 mError = NO_MEMORY;
1576 return NO_MEMORY;
1577 }
1578 } else {
Dianne Hackborn97e2bcd2011-04-13 18:15:56 -07001579 if (mDataSize > desired) {
1580 mDataSize = desired;
Steve Block6807e592011-10-20 11:56:00 +01001581 ALOGV("continueWrite Setting data size of %p to %d\n", this, mDataSize);
Dianne Hackborn97e2bcd2011-04-13 18:15:56 -07001582 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001583 if (mDataPos > desired) {
1584 mDataPos = desired;
Steve Block6807e592011-10-20 11:56:00 +01001585 ALOGV("continueWrite Setting data pos of %p to %d\n", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001586 }
1587 }
1588
1589 } else {
1590 // This is the first data. Easy!
1591 uint8_t* data = (uint8_t*)malloc(desired);
1592 if (!data) {
1593 mError = NO_MEMORY;
1594 return NO_MEMORY;
1595 }
Hyejin Kim3f727c02013-03-09 11:28:54 +09001596
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001597 if(!(mDataCapacity == 0 && mObjects == NULL
1598 && mObjectsCapacity == 0)) {
Steve Blocke6f43dd2012-01-06 19:20:56 +00001599 ALOGE("continueWrite: %d/%p/%d/%d", mDataCapacity, mObjects, mObjectsCapacity, desired);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001600 }
1601
1602 mData = data;
1603 mDataSize = mDataPos = 0;
Steve Block6807e592011-10-20 11:56:00 +01001604 ALOGV("continueWrite Setting data size of %p to %d\n", this, mDataSize);
1605 ALOGV("continueWrite Setting data pos of %p to %d\n", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001606 mDataCapacity = desired;
1607 }
1608
1609 return NO_ERROR;
1610}
1611
1612void Parcel::initState()
1613{
1614 mError = NO_ERROR;
1615 mData = 0;
1616 mDataSize = 0;
1617 mDataCapacity = 0;
1618 mDataPos = 0;
Steve Block6807e592011-10-20 11:56:00 +01001619 ALOGV("initState Setting data size of %p to %d\n", this, mDataSize);
1620 ALOGV("initState Setting data pos of %p to %d\n", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001621 mObjects = NULL;
1622 mObjectsSize = 0;
1623 mObjectsCapacity = 0;
1624 mNextObjectHint = 0;
1625 mHasFds = false;
1626 mFdsKnown = true;
Dianne Hackborn8938ed22011-09-28 23:19:47 -04001627 mAllowFds = true;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001628 mOwner = NULL;
1629}
1630
1631void Parcel::scanForFds() const
1632{
1633 bool hasFds = false;
1634 for (size_t i=0; i<mObjectsSize; i++) {
1635 const flat_binder_object* flat
1636 = reinterpret_cast<const flat_binder_object*>(mData + mObjects[i]);
1637 if (flat->type == BINDER_TYPE_FD) {
1638 hasFds = true;
1639 break;
1640 }
1641 }
1642 mHasFds = hasFds;
1643 mFdsKnown = true;
1644}
1645
Jeff Brown5707dbf2011-09-23 21:17:56 -07001646// --- Parcel::Blob ---
1647
1648Parcel::Blob::Blob() :
1649 mMapped(false), mData(NULL), mSize(0) {
1650}
1651
1652Parcel::Blob::~Blob() {
1653 release();
1654}
1655
1656void Parcel::Blob::release() {
1657 if (mMapped && mData) {
1658 ::munmap(mData, mSize);
1659 }
1660 clear();
1661}
1662
1663void Parcel::Blob::init(bool mapped, void* data, size_t size) {
1664 mMapped = mapped;
1665 mData = data;
1666 mSize = size;
1667}
1668
1669void Parcel::Blob::clear() {
1670 mMapped = false;
1671 mData = NULL;
1672 mSize = 0;
1673}
1674
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001675}; // namespace android