blob: 98a3dd7be6689b0b48c2099117440ae09f0833bf [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
Jun Jiangabf8a2c2014-04-29 14:22:10 +080028#include <errno.h>
Mathias Agopian002e1e52013-05-06 20:20:50 -070029#include <utils/Debug.h>
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -070030#include <utils/Log.h>
31#include <utils/String8.h>
32#include <utils/String16.h>
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -070033#include <utils/misc.h>
Mathias Agopian98e71dd2010-02-11 17:30:52 -080034#include <utils/Flattenable.h>
Jeff Brown5707dbf2011-09-23 21:17:56 -070035#include <cutils/ashmem.h>
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -070036
Mathias Agopian208059f2009-05-18 15:08:03 -070037#include <private/binder/binder_module.h>
Dianne Hackborn7e790af2014-11-11 12:22:53 -080038#include <private/binder/Static.h>
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -070039
Arve Hjønnevågf50b9ea2014-02-13 19:22:08 -080040#include <inttypes.h>
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -070041#include <stdio.h>
42#include <stdlib.h>
43#include <stdint.h>
Jeff Brown5707dbf2011-09-23 21:17:56 -070044#include <sys/mman.h>
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -070045
46#ifndef INT32_MAX
47#define INT32_MAX ((int32_t)(2147483647))
48#endif
49
50#define LOG_REFS(...)
Steve Block9f760152011-10-12 17:27:03 +010051//#define LOG_REFS(...) ALOG(LOG_DEBUG, "Parcel", __VA_ARGS__)
Dianne Hackborn7e790af2014-11-11 12:22:53 -080052#define LOG_ALLOC(...)
53//#define LOG_ALLOC(...) ALOG(LOG_DEBUG, "Parcel", __VA_ARGS__)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -070054
55// ---------------------------------------------------------------------------
56
57#define PAD_SIZE(s) (((s)+3)&~3)
58
Brad Fitzpatricka877cd82010-07-07 16:06:39 -070059// Note: must be kept in sync with android/os/StrictMode.java's PENALTY_GATHER
60#define STRICT_MODE_PENALTY_GATHER 0x100
61
Brad Fitzpatrickd36f4a52010-07-12 11:05:38 -070062// Note: must be kept in sync with android/os/Parcel.java's EX_HAS_REPLY_HEADER
63#define EX_HAS_REPLY_HEADER -128
64
Jeff Brown5707dbf2011-09-23 21:17:56 -070065// Maximum size of a blob to transfer in-place.
66static const size_t IN_PLACE_BLOB_LIMIT = 40 * 1024;
67
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -070068// XXX This can be made public if we want to provide
69// support for typed data.
70struct small_flat_data
71{
72 uint32_t type;
73 uint32_t data;
74};
75
76namespace android {
77
78void acquire_object(const sp<ProcessState>& proc,
79 const flat_binder_object& obj, const void* who)
80{
81 switch (obj.type) {
82 case BINDER_TYPE_BINDER:
83 if (obj.binder) {
84 LOG_REFS("Parcel %p acquiring reference on local %p", who, obj.cookie);
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -080085 reinterpret_cast<IBinder*>(obj.cookie)->incStrong(who);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -070086 }
87 return;
88 case BINDER_TYPE_WEAK_BINDER:
89 if (obj.binder)
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -080090 reinterpret_cast<RefBase::weakref_type*>(obj.binder)->incWeak(who);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -070091 return;
92 case BINDER_TYPE_HANDLE: {
93 const sp<IBinder> b = proc->getStrongProxyForHandle(obj.handle);
94 if (b != NULL) {
95 LOG_REFS("Parcel %p acquiring reference on remote %p", who, b.get());
96 b->incStrong(who);
97 }
98 return;
99 }
100 case BINDER_TYPE_WEAK_HANDLE: {
101 const wp<IBinder> b = proc->getWeakProxyForHandle(obj.handle);
102 if (b != NULL) b.get_refs()->incWeak(who);
103 return;
104 }
105 case BINDER_TYPE_FD: {
106 // intentionally blank -- nothing to do to acquire this, but we do
107 // recognize it as a legitimate object type.
108 return;
109 }
110 }
111
Colin Cross6f4f3ab2014-02-05 17:42:44 -0800112 ALOGD("Invalid object type 0x%08x", obj.type);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700113}
114
115void release_object(const sp<ProcessState>& proc,
116 const flat_binder_object& obj, const void* who)
117{
118 switch (obj.type) {
119 case BINDER_TYPE_BINDER:
120 if (obj.binder) {
121 LOG_REFS("Parcel %p releasing reference on local %p", who, obj.cookie);
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800122 reinterpret_cast<IBinder*>(obj.cookie)->decStrong(who);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700123 }
124 return;
125 case BINDER_TYPE_WEAK_BINDER:
126 if (obj.binder)
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800127 reinterpret_cast<RefBase::weakref_type*>(obj.binder)->decWeak(who);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700128 return;
129 case BINDER_TYPE_HANDLE: {
130 const sp<IBinder> b = proc->getStrongProxyForHandle(obj.handle);
131 if (b != NULL) {
132 LOG_REFS("Parcel %p releasing reference on remote %p", who, b.get());
133 b->decStrong(who);
134 }
135 return;
136 }
137 case BINDER_TYPE_WEAK_HANDLE: {
138 const wp<IBinder> b = proc->getWeakProxyForHandle(obj.handle);
139 if (b != NULL) b.get_refs()->decWeak(who);
140 return;
141 }
142 case BINDER_TYPE_FD: {
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800143 if (obj.cookie != 0) close(obj.handle);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700144 return;
145 }
146 }
147
Colin Cross6f4f3ab2014-02-05 17:42:44 -0800148 ALOGE("Invalid object type 0x%08x", obj.type);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700149}
150
151inline static status_t finish_flatten_binder(
Colin Cross6f4f3ab2014-02-05 17:42:44 -0800152 const sp<IBinder>& /*binder*/, const flat_binder_object& flat, Parcel* out)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700153{
154 return out->writeObject(flat, false);
155}
156
Colin Cross6f4f3ab2014-02-05 17:42:44 -0800157status_t flatten_binder(const sp<ProcessState>& /*proc*/,
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700158 const sp<IBinder>& binder, Parcel* out)
159{
160 flat_binder_object obj;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700161
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700162 obj.flags = 0x7f | FLAT_BINDER_FLAG_ACCEPTS_FDS;
163 if (binder != NULL) {
164 IBinder *local = binder->localBinder();
165 if (!local) {
166 BpBinder *proxy = binder->remoteBinder();
167 if (proxy == NULL) {
Steve Blocke6f43dd2012-01-06 19:20:56 +0000168 ALOGE("null proxy");
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700169 }
170 const int32_t handle = proxy ? proxy->handle() : 0;
171 obj.type = BINDER_TYPE_HANDLE;
Arve Hjønnevåg07fd0f12014-02-18 21:10:29 -0800172 obj.binder = 0; /* Don't pass uninitialized stack data to a remote process */
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700173 obj.handle = handle;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800174 obj.cookie = 0;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700175 } else {
176 obj.type = BINDER_TYPE_BINDER;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800177 obj.binder = reinterpret_cast<uintptr_t>(local->getWeakRefs());
178 obj.cookie = reinterpret_cast<uintptr_t>(local);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700179 }
180 } else {
181 obj.type = BINDER_TYPE_BINDER;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800182 obj.binder = 0;
183 obj.cookie = 0;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700184 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700185
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700186 return finish_flatten_binder(binder, obj, out);
187}
188
Colin Cross6f4f3ab2014-02-05 17:42:44 -0800189status_t flatten_binder(const sp<ProcessState>& /*proc*/,
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700190 const wp<IBinder>& binder, Parcel* out)
191{
192 flat_binder_object obj;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700193
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700194 obj.flags = 0x7f | FLAT_BINDER_FLAG_ACCEPTS_FDS;
195 if (binder != NULL) {
196 sp<IBinder> real = binder.promote();
197 if (real != NULL) {
198 IBinder *local = real->localBinder();
199 if (!local) {
200 BpBinder *proxy = real->remoteBinder();
201 if (proxy == NULL) {
Steve Blocke6f43dd2012-01-06 19:20:56 +0000202 ALOGE("null proxy");
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700203 }
204 const int32_t handle = proxy ? proxy->handle() : 0;
205 obj.type = BINDER_TYPE_WEAK_HANDLE;
Arve Hjønnevåg07fd0f12014-02-18 21:10:29 -0800206 obj.binder = 0; /* Don't pass uninitialized stack data to a remote process */
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700207 obj.handle = handle;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800208 obj.cookie = 0;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700209 } else {
210 obj.type = BINDER_TYPE_WEAK_BINDER;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800211 obj.binder = reinterpret_cast<uintptr_t>(binder.get_refs());
212 obj.cookie = reinterpret_cast<uintptr_t>(binder.unsafe_get());
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700213 }
214 return finish_flatten_binder(real, obj, out);
215 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700216
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700217 // XXX How to deal? In order to flatten the given binder,
218 // we need to probe it for information, which requires a primary
219 // reference... but we don't have one.
220 //
221 // The OpenBinder implementation uses a dynamic_cast<> here,
222 // but we can't do that with the different reference counting
223 // implementation we are using.
Steve Blocke6f43dd2012-01-06 19:20:56 +0000224 ALOGE("Unable to unflatten Binder weak reference!");
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700225 obj.type = BINDER_TYPE_BINDER;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800226 obj.binder = 0;
227 obj.cookie = 0;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700228 return finish_flatten_binder(NULL, obj, out);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700229
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700230 } else {
231 obj.type = BINDER_TYPE_BINDER;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800232 obj.binder = 0;
233 obj.cookie = 0;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700234 return finish_flatten_binder(NULL, obj, out);
235 }
236}
237
238inline static status_t finish_unflatten_binder(
Colin Cross6f4f3ab2014-02-05 17:42:44 -0800239 BpBinder* /*proxy*/, const flat_binder_object& /*flat*/,
240 const Parcel& /*in*/)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700241{
242 return NO_ERROR;
243}
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700244
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700245status_t unflatten_binder(const sp<ProcessState>& proc,
246 const Parcel& in, sp<IBinder>* out)
247{
248 const flat_binder_object* flat = in.readObject(false);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700249
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700250 if (flat) {
251 switch (flat->type) {
252 case BINDER_TYPE_BINDER:
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800253 *out = reinterpret_cast<IBinder*>(flat->cookie);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700254 return finish_unflatten_binder(NULL, *flat, in);
255 case BINDER_TYPE_HANDLE:
256 *out = proc->getStrongProxyForHandle(flat->handle);
257 return finish_unflatten_binder(
258 static_cast<BpBinder*>(out->get()), *flat, in);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700259 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700260 }
261 return BAD_TYPE;
262}
263
264status_t unflatten_binder(const sp<ProcessState>& proc,
265 const Parcel& in, wp<IBinder>* out)
266{
267 const flat_binder_object* flat = in.readObject(false);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700268
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700269 if (flat) {
270 switch (flat->type) {
271 case BINDER_TYPE_BINDER:
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800272 *out = reinterpret_cast<IBinder*>(flat->cookie);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700273 return finish_unflatten_binder(NULL, *flat, in);
274 case BINDER_TYPE_WEAK_BINDER:
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800275 if (flat->binder != 0) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700276 out->set_object_and_refs(
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800277 reinterpret_cast<IBinder*>(flat->cookie),
278 reinterpret_cast<RefBase::weakref_type*>(flat->binder));
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700279 } else {
280 *out = NULL;
281 }
282 return finish_unflatten_binder(NULL, *flat, in);
283 case BINDER_TYPE_HANDLE:
284 case BINDER_TYPE_WEAK_HANDLE:
285 *out = proc->getWeakProxyForHandle(flat->handle);
286 return finish_unflatten_binder(
287 static_cast<BpBinder*>(out->unsafe_get()), *flat, in);
288 }
289 }
290 return BAD_TYPE;
291}
292
293// ---------------------------------------------------------------------------
294
295Parcel::Parcel()
296{
Dianne Hackborn7e790af2014-11-11 12:22:53 -0800297 LOG_ALLOC("Parcel %p: constructing", this);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700298 initState();
299}
300
301Parcel::~Parcel()
302{
303 freeDataNoInit();
Dianne Hackborn7e790af2014-11-11 12:22:53 -0800304 LOG_ALLOC("Parcel %p: destroyed", this);
305}
306
307size_t Parcel::getGlobalAllocSize() {
308 AutoMutex _l(gParcelGlobalAllocSizeLock);
309 return gParcelGlobalAllocSize;
310}
311
312size_t Parcel::getGlobalAllocCount() {
313 AutoMutex _l(gParcelGlobalAllocSizeLock);
314 return gParcelGlobalAllocCount;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700315}
316
317const uint8_t* Parcel::data() const
318{
319 return mData;
320}
321
322size_t Parcel::dataSize() const
323{
324 return (mDataSize > mDataPos ? mDataSize : mDataPos);
325}
326
327size_t Parcel::dataAvail() const
328{
329 // TODO: decide what to do about the possibility that this can
330 // report an available-data size that exceeds a Java int's max
331 // positive value, causing havoc. Fortunately this will only
332 // happen if someone constructs a Parcel containing more than two
333 // gigabytes of data, which on typical phone hardware is simply
334 // not possible.
335 return dataSize() - dataPosition();
336}
337
338size_t Parcel::dataPosition() const
339{
340 return mDataPos;
341}
342
343size_t Parcel::dataCapacity() const
344{
345 return mDataCapacity;
346}
347
348status_t Parcel::setDataSize(size_t size)
349{
350 status_t err;
351 err = continueWrite(size);
352 if (err == NO_ERROR) {
353 mDataSize = size;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700354 ALOGV("setDataSize Setting data size of %p to %zu", this, mDataSize);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700355 }
356 return err;
357}
358
359void Parcel::setDataPosition(size_t pos) const
360{
361 mDataPos = pos;
362 mNextObjectHint = 0;
363}
364
365status_t Parcel::setDataCapacity(size_t size)
366{
Dianne Hackborn97e2bcd2011-04-13 18:15:56 -0700367 if (size > mDataCapacity) return continueWrite(size);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700368 return NO_ERROR;
369}
370
371status_t Parcel::setData(const uint8_t* buffer, size_t len)
372{
373 status_t err = restartWrite(len);
374 if (err == NO_ERROR) {
375 memcpy(const_cast<uint8_t*>(data()), buffer, len);
376 mDataSize = len;
377 mFdsKnown = false;
378 }
379 return err;
380}
381
Andreas Huber51faf462011-04-13 10:21:56 -0700382status_t Parcel::appendFrom(const Parcel *parcel, size_t offset, size_t len)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700383{
384 const sp<ProcessState> proc(ProcessState::self());
385 status_t err;
Andreas Huber51faf462011-04-13 10:21:56 -0700386 const uint8_t *data = parcel->mData;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800387 const binder_size_t *objects = parcel->mObjects;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700388 size_t size = parcel->mObjectsSize;
389 int startPos = mDataPos;
390 int firstIndex = -1, lastIndex = -2;
391
392 if (len == 0) {
393 return NO_ERROR;
394 }
395
396 // range checks against the source parcel size
397 if ((offset > parcel->mDataSize)
398 || (len > parcel->mDataSize)
399 || (offset + len > parcel->mDataSize)) {
400 return BAD_VALUE;
401 }
402
403 // Count objects in range
404 for (int i = 0; i < (int) size; i++) {
405 size_t off = objects[i];
406 if ((off >= offset) && (off < offset + len)) {
407 if (firstIndex == -1) {
408 firstIndex = i;
409 }
410 lastIndex = i;
411 }
412 }
413 int numObjects = lastIndex - firstIndex + 1;
414
Dianne Hackborn97e2bcd2011-04-13 18:15:56 -0700415 if ((mDataSize+len) > mDataCapacity) {
416 // grow data
417 err = growData(len);
418 if (err != NO_ERROR) {
419 return err;
420 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700421 }
422
423 // append data
424 memcpy(mData + mDataPos, data + offset, len);
425 mDataPos += len;
426 mDataSize += len;
427
Dianne Hackborn8938ed22011-09-28 23:19:47 -0400428 err = NO_ERROR;
429
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700430 if (numObjects > 0) {
431 // grow objects
432 if (mObjectsCapacity < mObjectsSize + numObjects) {
433 int newSize = ((mObjectsSize + numObjects)*3)/2;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800434 binder_size_t *objects =
435 (binder_size_t*)realloc(mObjects, newSize*sizeof(binder_size_t));
436 if (objects == (binder_size_t*)0) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700437 return NO_MEMORY;
438 }
439 mObjects = objects;
440 mObjectsCapacity = newSize;
441 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700442
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700443 // append and acquire objects
444 int idx = mObjectsSize;
445 for (int i = firstIndex; i <= lastIndex; i++) {
446 size_t off = objects[i] - offset + startPos;
447 mObjects[idx++] = off;
448 mObjectsSize++;
449
Dianne Hackborn8af0f822009-05-22 13:20:23 -0700450 flat_binder_object* flat
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700451 = reinterpret_cast<flat_binder_object*>(mData + off);
452 acquire_object(proc, *flat, this);
453
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700454 if (flat->type == BINDER_TYPE_FD) {
Dianne Hackborn8af0f822009-05-22 13:20:23 -0700455 // If this is a file descriptor, we need to dup it so the
456 // new Parcel now owns its own fd, and can declare that we
457 // officially know we have fds.
458 flat->handle = dup(flat->handle);
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800459 flat->cookie = 1;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700460 mHasFds = mFdsKnown = true;
Dianne Hackborn8938ed22011-09-28 23:19:47 -0400461 if (!mAllowFds) {
462 err = FDS_NOT_ALLOWED;
463 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700464 }
465 }
466 }
467
Dianne Hackborn8938ed22011-09-28 23:19:47 -0400468 return err;
469}
470
Dianne Hackborn7746cc32011-10-03 21:09:35 -0700471bool Parcel::pushAllowFds(bool allowFds)
Dianne Hackborn8938ed22011-09-28 23:19:47 -0400472{
473 const bool origValue = mAllowFds;
Dianne Hackborn7746cc32011-10-03 21:09:35 -0700474 if (!allowFds) {
475 mAllowFds = false;
476 }
Dianne Hackborn8938ed22011-09-28 23:19:47 -0400477 return origValue;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700478}
479
Dianne Hackborn7746cc32011-10-03 21:09:35 -0700480void Parcel::restoreAllowFds(bool lastValue)
481{
482 mAllowFds = lastValue;
483}
484
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700485bool Parcel::hasFileDescriptors() const
486{
487 if (!mFdsKnown) {
488 scanForFds();
489 }
490 return mHasFds;
491}
492
Brad Fitzpatrick702ea9d2010-06-18 13:07:53 -0700493// Write RPC headers. (previously just the interface token)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700494status_t Parcel::writeInterfaceToken(const String16& interface)
495{
Brad Fitzpatricka877cd82010-07-07 16:06:39 -0700496 writeInt32(IPCThreadState::self()->getStrictModePolicy() |
497 STRICT_MODE_PENALTY_GATHER);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700498 // currently the interface identification token is just its name as a string
499 return writeString16(interface);
500}
501
Mathias Agopian83c04462009-05-22 19:00:22 -0700502bool Parcel::checkInterface(IBinder* binder) const
503{
Brad Fitzpatrick702ea9d2010-06-18 13:07:53 -0700504 return enforceInterface(binder->getInterfaceDescriptor());
Mathias Agopian83c04462009-05-22 19:00:22 -0700505}
506
Brad Fitzpatricka877cd82010-07-07 16:06:39 -0700507bool Parcel::enforceInterface(const String16& interface,
Brad Fitzpatrick70081a12010-07-27 09:49:11 -0700508 IPCThreadState* threadState) const
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700509{
Brad Fitzpatrick70081a12010-07-27 09:49:11 -0700510 int32_t strictPolicy = readInt32();
511 if (threadState == NULL) {
512 threadState = IPCThreadState::self();
Brad Fitzpatricka877cd82010-07-07 16:06:39 -0700513 }
Brad Fitzpatrick52736032010-08-30 16:01:16 -0700514 if ((threadState->getLastTransactionBinderFlags() &
515 IBinder::FLAG_ONEWAY) != 0) {
516 // For one-way calls, the callee is running entirely
517 // disconnected from the caller, so disable StrictMode entirely.
518 // Not only does disk/network usage not impact the caller, but
519 // there's no way to commuicate back any violations anyway.
520 threadState->setStrictModePolicy(0);
521 } else {
522 threadState->setStrictModePolicy(strictPolicy);
523 }
Mathias Agopian83c04462009-05-22 19:00:22 -0700524 const String16 str(readString16());
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700525 if (str == interface) {
526 return true;
527 } else {
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700528 ALOGW("**** enforceInterface() expected '%s' but read '%s'",
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700529 String8(interface).string(), String8(str).string());
530 return false;
531 }
Brad Fitzpatrick702ea9d2010-06-18 13:07:53 -0700532}
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700533
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800534const binder_size_t* Parcel::objects() const
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700535{
536 return mObjects;
537}
538
539size_t Parcel::objectsCount() const
540{
541 return mObjectsSize;
542}
543
544status_t Parcel::errorCheck() const
545{
546 return mError;
547}
548
549void Parcel::setError(status_t err)
550{
551 mError = err;
552}
553
554status_t Parcel::finishWrite(size_t len)
555{
556 //printf("Finish write of %d\n", len);
557 mDataPos += len;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700558 ALOGV("finishWrite Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700559 if (mDataPos > mDataSize) {
560 mDataSize = mDataPos;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700561 ALOGV("finishWrite Setting data size of %p to %zu", this, mDataSize);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700562 }
563 //printf("New pos=%d, size=%d\n", mDataPos, mDataSize);
564 return NO_ERROR;
565}
566
567status_t Parcel::writeUnpadded(const void* data, size_t len)
568{
569 size_t end = mDataPos + len;
570 if (end < mDataPos) {
571 // integer overflow
572 return BAD_VALUE;
573 }
574
575 if (end <= mDataCapacity) {
576restart_write:
577 memcpy(mData+mDataPos, data, len);
578 return finishWrite(len);
579 }
580
581 status_t err = growData(len);
582 if (err == NO_ERROR) goto restart_write;
583 return err;
584}
585
586status_t Parcel::write(const void* data, size_t len)
587{
588 void* const d = writeInplace(len);
589 if (d) {
590 memcpy(d, data, len);
591 return NO_ERROR;
592 }
593 return mError;
594}
595
596void* Parcel::writeInplace(size_t len)
597{
598 const size_t padded = PAD_SIZE(len);
599
600 // sanity check for integer overflow
601 if (mDataPos+padded < mDataPos) {
602 return NULL;
603 }
604
605 if ((mDataPos+padded) <= mDataCapacity) {
606restart_write:
607 //printf("Writing %ld bytes, padded to %ld\n", len, padded);
608 uint8_t* const data = mData+mDataPos;
609
610 // Need to pad at end?
611 if (padded != len) {
612#if BYTE_ORDER == BIG_ENDIAN
613 static const uint32_t mask[4] = {
614 0x00000000, 0xffffff00, 0xffff0000, 0xff000000
615 };
616#endif
617#if BYTE_ORDER == LITTLE_ENDIAN
618 static const uint32_t mask[4] = {
619 0x00000000, 0x00ffffff, 0x0000ffff, 0x000000ff
620 };
621#endif
622 //printf("Applying pad mask: %p to %p\n", (void*)mask[padded-len],
623 // *reinterpret_cast<void**>(data+padded-4));
624 *reinterpret_cast<uint32_t*>(data+padded-4) &= mask[padded-len];
625 }
626
627 finishWrite(padded);
628 return data;
629 }
630
631 status_t err = growData(padded);
632 if (err == NO_ERROR) goto restart_write;
633 return NULL;
634}
635
636status_t Parcel::writeInt32(int32_t val)
637{
Andreas Huber84a6d042009-08-17 13:33:27 -0700638 return writeAligned(val);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700639}
Marco Nelissen5c0106e2013-10-16 10:57:51 -0700640status_t Parcel::writeInt32Array(size_t len, const int32_t *val) {
641 if (!val) {
642 return writeAligned(-1);
643 }
644 status_t ret = writeAligned(len);
645 if (ret == NO_ERROR) {
646 ret = write(val, len * sizeof(*val));
647 }
648 return ret;
649}
Marco Nelissenf0190bf2014-03-13 14:17:40 -0700650status_t Parcel::writeByteArray(size_t len, const uint8_t *val) {
651 if (!val) {
652 return writeAligned(-1);
653 }
654 status_t ret = writeAligned(len);
655 if (ret == NO_ERROR) {
656 ret = write(val, len * sizeof(*val));
657 }
658 return ret;
659}
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700660
661status_t Parcel::writeInt64(int64_t val)
662{
Andreas Huber84a6d042009-08-17 13:33:27 -0700663 return writeAligned(val);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700664}
665
Serban Constantinescuf683e012013-11-05 16:53:55 +0000666status_t Parcel::writePointer(uintptr_t val)
667{
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800668 return writeAligned<binder_uintptr_t>(val);
Serban Constantinescuf683e012013-11-05 16:53:55 +0000669}
670
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700671status_t Parcel::writeFloat(float val)
672{
Andreas Huber84a6d042009-08-17 13:33:27 -0700673 return writeAligned(val);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700674}
675
Douglas Leungcc1a4bb2013-01-11 15:00:55 -0800676#if defined(__mips__) && defined(__mips_hard_float)
677
678status_t Parcel::writeDouble(double val)
679{
680 union {
681 double d;
682 unsigned long long ll;
683 } u;
684 u.d = val;
685 return writeAligned(u.ll);
686}
687
688#else
689
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700690status_t Parcel::writeDouble(double val)
691{
Andreas Huber84a6d042009-08-17 13:33:27 -0700692 return writeAligned(val);
693}
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700694
Douglas Leungcc1a4bb2013-01-11 15:00:55 -0800695#endif
696
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700697status_t Parcel::writeCString(const char* str)
698{
699 return write(str, strlen(str)+1);
700}
701
702status_t Parcel::writeString8(const String8& str)
703{
704 status_t err = writeInt32(str.bytes());
Pravat Dalbeherad1dff8d2010-12-15 08:40:00 +0100705 // only write string if its length is more than zero characters,
706 // as readString8 will only read if the length field is non-zero.
707 // this is slightly different from how writeString16 works.
708 if (str.bytes() > 0 && err == NO_ERROR) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700709 err = write(str.string(), str.bytes()+1);
710 }
711 return err;
712}
713
714status_t Parcel::writeString16(const String16& str)
715{
716 return writeString16(str.string(), str.size());
717}
718
719status_t Parcel::writeString16(const char16_t* str, size_t len)
720{
721 if (str == NULL) return writeInt32(-1);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700722
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700723 status_t err = writeInt32(len);
724 if (err == NO_ERROR) {
725 len *= sizeof(char16_t);
726 uint8_t* data = (uint8_t*)writeInplace(len+sizeof(char16_t));
727 if (data) {
728 memcpy(data, str, len);
729 *reinterpret_cast<char16_t*>(data+len) = 0;
730 return NO_ERROR;
731 }
732 err = mError;
733 }
734 return err;
735}
736
737status_t Parcel::writeStrongBinder(const sp<IBinder>& val)
738{
739 return flatten_binder(ProcessState::self(), val, this);
740}
741
742status_t Parcel::writeWeakBinder(const wp<IBinder>& val)
743{
744 return flatten_binder(ProcessState::self(), val, this);
745}
746
Mathias Agopiana47f02a2009-05-21 16:29:38 -0700747status_t Parcel::writeNativeHandle(const native_handle* handle)
The Android Open Source Project5f78a482009-01-20 14:03:58 -0800748{
Mathias Agopian1d0a95b2009-07-31 16:12:13 -0700749 if (!handle || handle->version != sizeof(native_handle))
The Android Open Source Project5f78a482009-01-20 14:03:58 -0800750 return BAD_TYPE;
751
752 status_t err;
Mathias Agopiana47f02a2009-05-21 16:29:38 -0700753 err = writeInt32(handle->numFds);
The Android Open Source Project5f78a482009-01-20 14:03:58 -0800754 if (err != NO_ERROR) return err;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800755
Mathias Agopiana47f02a2009-05-21 16:29:38 -0700756 err = writeInt32(handle->numInts);
The Android Open Source Project5f78a482009-01-20 14:03:58 -0800757 if (err != NO_ERROR) return err;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800758
Mathias Agopiana47f02a2009-05-21 16:29:38 -0700759 for (int i=0 ; err==NO_ERROR && i<handle->numFds ; i++)
760 err = writeDupFileDescriptor(handle->data[i]);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800761
762 if (err != NO_ERROR) {
Steve Block9d453682011-12-20 16:23:08 +0000763 ALOGD("write native handle, write dup fd failed");
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800764 return err;
765 }
Mathias Agopiana47f02a2009-05-21 16:29:38 -0700766 err = write(handle->data + handle->numFds, sizeof(int)*handle->numInts);
The Android Open Source Project5f78a482009-01-20 14:03:58 -0800767 return err;
768}
769
Jeff Brown93ff1f92011-11-04 19:01:44 -0700770status_t Parcel::writeFileDescriptor(int fd, bool takeOwnership)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700771{
772 flat_binder_object obj;
773 obj.type = BINDER_TYPE_FD;
774 obj.flags = 0x7f | FLAT_BINDER_FLAG_ACCEPTS_FDS;
Arve Hjønnevåg07fd0f12014-02-18 21:10:29 -0800775 obj.binder = 0; /* Don't pass uninitialized stack data to a remote process */
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700776 obj.handle = fd;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800777 obj.cookie = takeOwnership ? 1 : 0;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700778 return writeObject(obj, true);
779}
780
781status_t Parcel::writeDupFileDescriptor(int fd)
782{
Jeff Brownd341c712011-11-04 20:19:33 -0700783 int dupFd = dup(fd);
784 if (dupFd < 0) {
785 return -errno;
786 }
787 status_t err = writeFileDescriptor(dupFd, true /*takeOwnership*/);
788 if (err) {
789 close(dupFd);
790 }
791 return err;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700792}
793
Mike Lockwoodcbe36fe2013-09-05 08:41:06 -0700794// WARNING: This method must stay in sync with
795// Parcelable.Creator<ParcelFileDescriptor> CREATOR
796// in frameworks/base/core/java/android/os/ParcelFileDescriptor.java
797status_t Parcel::writeParcelFileDescriptor(int fd, int commChannel) {
798 status_t status;
799
800 if (fd < 0) {
801 status = writeInt32(0); // ParcelFileDescriptor is null
802 if (status) return status;
803 } else {
804 status = writeInt32(1); // ParcelFileDescriptor is not null
805 if (status) return status;
806 status = writeDupFileDescriptor(fd);
807 if (status) return status;
808 if (commChannel < 0) {
809 status = writeInt32(0); // commChannel is null
810 if (status) return status;
811 } else {
812 status = writeInt32(1); // commChannel is not null
813 if (status) return status;
814 status = writeDupFileDescriptor(commChannel);
815 }
816 }
817 return status;
818}
819
Jeff Brown5707dbf2011-09-23 21:17:56 -0700820status_t Parcel::writeBlob(size_t len, WritableBlob* outBlob)
821{
822 status_t status;
823
824 if (!mAllowFds || len <= IN_PLACE_BLOB_LIMIT) {
Steve Block6807e592011-10-20 11:56:00 +0100825 ALOGV("writeBlob: write in place");
Jeff Brown5707dbf2011-09-23 21:17:56 -0700826 status = writeInt32(0);
827 if (status) return status;
828
829 void* ptr = writeInplace(len);
830 if (!ptr) return NO_MEMORY;
831
832 outBlob->init(false /*mapped*/, ptr, len);
833 return NO_ERROR;
834 }
835
Steve Block6807e592011-10-20 11:56:00 +0100836 ALOGV("writeBlob: write to ashmem");
Jeff Brown5707dbf2011-09-23 21:17:56 -0700837 int fd = ashmem_create_region("Parcel Blob", len);
838 if (fd < 0) return NO_MEMORY;
839
840 int result = ashmem_set_prot_region(fd, PROT_READ | PROT_WRITE);
841 if (result < 0) {
Jeff Brownec4e0062011-10-10 14:50:10 -0700842 status = result;
Jeff Brown5707dbf2011-09-23 21:17:56 -0700843 } else {
844 void* ptr = ::mmap(NULL, len, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
845 if (ptr == MAP_FAILED) {
846 status = -errno;
847 } else {
848 result = ashmem_set_prot_region(fd, PROT_READ);
849 if (result < 0) {
Jeff Brownec4e0062011-10-10 14:50:10 -0700850 status = result;
Jeff Brown5707dbf2011-09-23 21:17:56 -0700851 } else {
852 status = writeInt32(1);
853 if (!status) {
Jeff Brown93ff1f92011-11-04 19:01:44 -0700854 status = writeFileDescriptor(fd, true /*takeOwnership*/);
Jeff Brown5707dbf2011-09-23 21:17:56 -0700855 if (!status) {
856 outBlob->init(true /*mapped*/, ptr, len);
857 return NO_ERROR;
858 }
859 }
860 }
861 }
862 ::munmap(ptr, len);
863 }
864 ::close(fd);
865 return status;
866}
867
Mathias Agopiane1424282013-07-29 21:24:40 -0700868status_t Parcel::write(const FlattenableHelperInterface& val)
Mathias Agopian98e71dd2010-02-11 17:30:52 -0800869{
870 status_t err;
871
872 // size if needed
Mathias Agopiane1424282013-07-29 21:24:40 -0700873 const size_t len = val.getFlattenedSize();
874 const size_t fd_count = val.getFdCount();
Mathias Agopian98e71dd2010-02-11 17:30:52 -0800875
876 err = this->writeInt32(len);
877 if (err) return err;
878
879 err = this->writeInt32(fd_count);
880 if (err) return err;
881
882 // payload
Mathias Agopiane1424282013-07-29 21:24:40 -0700883 void* const buf = this->writeInplace(PAD_SIZE(len));
Mathias Agopian98e71dd2010-02-11 17:30:52 -0800884 if (buf == NULL)
885 return BAD_VALUE;
886
887 int* fds = NULL;
888 if (fd_count) {
889 fds = new int[fd_count];
890 }
891
892 err = val.flatten(buf, len, fds, fd_count);
893 for (size_t i=0 ; i<fd_count && err==NO_ERROR ; i++) {
894 err = this->writeDupFileDescriptor( fds[i] );
895 }
896
897 if (fd_count) {
898 delete [] fds;
899 }
900
901 return err;
902}
903
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700904status_t Parcel::writeObject(const flat_binder_object& val, bool nullMetaData)
905{
906 const bool enoughData = (mDataPos+sizeof(val)) <= mDataCapacity;
907 const bool enoughObjects = mObjectsSize < mObjectsCapacity;
908 if (enoughData && enoughObjects) {
909restart_write:
910 *reinterpret_cast<flat_binder_object*>(mData+mDataPos) = val;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700911
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700912 // Need to write meta-data?
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800913 if (nullMetaData || val.binder != 0) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700914 mObjects[mObjectsSize] = mDataPos;
915 acquire_object(ProcessState::self(), val, this);
916 mObjectsSize++;
917 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700918
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700919 // remember if it's a file descriptor
920 if (val.type == BINDER_TYPE_FD) {
Dianne Hackborn8938ed22011-09-28 23:19:47 -0400921 if (!mAllowFds) {
922 return FDS_NOT_ALLOWED;
923 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700924 mHasFds = mFdsKnown = true;
925 }
926
927 return finishWrite(sizeof(flat_binder_object));
928 }
929
930 if (!enoughData) {
931 const status_t err = growData(sizeof(val));
932 if (err != NO_ERROR) return err;
933 }
934 if (!enoughObjects) {
935 size_t newSize = ((mObjectsSize+2)*3)/2;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800936 binder_size_t* objects = (binder_size_t*)realloc(mObjects, newSize*sizeof(binder_size_t));
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700937 if (objects == NULL) return NO_MEMORY;
938 mObjects = objects;
939 mObjectsCapacity = newSize;
940 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700941
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700942 goto restart_write;
943}
944
Brad Fitzpatrick837a0d02010-07-13 15:33:35 -0700945status_t Parcel::writeNoException()
946{
947 return writeInt32(0);
948}
949
Colin Cross6f4f3ab2014-02-05 17:42:44 -0800950void Parcel::remove(size_t /*start*/, size_t /*amt*/)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700951{
952 LOG_ALWAYS_FATAL("Parcel::remove() not yet implemented!");
953}
954
955status_t Parcel::read(void* outData, size_t len) const
956{
Kenny Root5b61ad22014-03-17 13:18:16 -0700957 if ((mDataPos+PAD_SIZE(len)) >= mDataPos && (mDataPos+PAD_SIZE(len)) <= mDataSize
958 && len <= PAD_SIZE(len)) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700959 memcpy(outData, mData+mDataPos, len);
960 mDataPos += PAD_SIZE(len);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700961 ALOGV("read Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700962 return NO_ERROR;
963 }
964 return NOT_ENOUGH_DATA;
965}
966
967const void* Parcel::readInplace(size_t len) const
968{
Kenny Root5b61ad22014-03-17 13:18:16 -0700969 if ((mDataPos+PAD_SIZE(len)) >= mDataPos && (mDataPos+PAD_SIZE(len)) <= mDataSize
970 && len <= PAD_SIZE(len)) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700971 const void* data = mData+mDataPos;
972 mDataPos += PAD_SIZE(len);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700973 ALOGV("readInplace Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700974 return data;
975 }
976 return NULL;
977}
978
Andreas Huber84a6d042009-08-17 13:33:27 -0700979template<class T>
980status_t Parcel::readAligned(T *pArg) const {
981 COMPILE_TIME_ASSERT_FUNCTION_SCOPE(PAD_SIZE(sizeof(T)) == sizeof(T));
982
983 if ((mDataPos+sizeof(T)) <= mDataSize) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700984 const void* data = mData+mDataPos;
Andreas Huber84a6d042009-08-17 13:33:27 -0700985 mDataPos += sizeof(T);
986 *pArg = *reinterpret_cast<const T*>(data);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700987 return NO_ERROR;
988 } else {
989 return NOT_ENOUGH_DATA;
990 }
991}
992
Andreas Huber84a6d042009-08-17 13:33:27 -0700993template<class T>
994T Parcel::readAligned() const {
995 T result;
996 if (readAligned(&result) != NO_ERROR) {
997 result = 0;
998 }
999
1000 return result;
1001}
1002
1003template<class T>
1004status_t Parcel::writeAligned(T val) {
1005 COMPILE_TIME_ASSERT_FUNCTION_SCOPE(PAD_SIZE(sizeof(T)) == sizeof(T));
1006
1007 if ((mDataPos+sizeof(val)) <= mDataCapacity) {
1008restart_write:
1009 *reinterpret_cast<T*>(mData+mDataPos) = val;
1010 return finishWrite(sizeof(val));
1011 }
1012
1013 status_t err = growData(sizeof(val));
1014 if (err == NO_ERROR) goto restart_write;
1015 return err;
1016}
1017
1018status_t Parcel::readInt32(int32_t *pArg) const
1019{
1020 return readAligned(pArg);
1021}
1022
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001023int32_t Parcel::readInt32() const
1024{
Andreas Huber84a6d042009-08-17 13:33:27 -07001025 return readAligned<int32_t>();
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001026}
1027
1028
1029status_t Parcel::readInt64(int64_t *pArg) const
1030{
Andreas Huber84a6d042009-08-17 13:33:27 -07001031 return readAligned(pArg);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001032}
1033
1034
1035int64_t Parcel::readInt64() const
1036{
Andreas Huber84a6d042009-08-17 13:33:27 -07001037 return readAligned<int64_t>();
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001038}
1039
Serban Constantinescuf683e012013-11-05 16:53:55 +00001040status_t Parcel::readPointer(uintptr_t *pArg) const
1041{
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001042 status_t ret;
1043 binder_uintptr_t ptr;
1044 ret = readAligned(&ptr);
1045 if (!ret)
1046 *pArg = ptr;
1047 return ret;
Serban Constantinescuf683e012013-11-05 16:53:55 +00001048}
1049
1050uintptr_t Parcel::readPointer() const
1051{
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001052 return readAligned<binder_uintptr_t>();
Serban Constantinescuf683e012013-11-05 16:53:55 +00001053}
1054
1055
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001056status_t Parcel::readFloat(float *pArg) const
1057{
Andreas Huber84a6d042009-08-17 13:33:27 -07001058 return readAligned(pArg);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001059}
1060
1061
1062float Parcel::readFloat() const
1063{
Andreas Huber84a6d042009-08-17 13:33:27 -07001064 return readAligned<float>();
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001065}
1066
Douglas Leungcc1a4bb2013-01-11 15:00:55 -08001067#if defined(__mips__) && defined(__mips_hard_float)
1068
1069status_t Parcel::readDouble(double *pArg) const
1070{
1071 union {
1072 double d;
1073 unsigned long long ll;
1074 } u;
Narayan Kamath2c68d382014-06-04 15:04:29 +01001075 u.d = 0;
Douglas Leungcc1a4bb2013-01-11 15:00:55 -08001076 status_t status;
1077 status = readAligned(&u.ll);
1078 *pArg = u.d;
1079 return status;
1080}
1081
1082double Parcel::readDouble() const
1083{
1084 union {
1085 double d;
1086 unsigned long long ll;
1087 } u;
1088 u.ll = readAligned<unsigned long long>();
1089 return u.d;
1090}
1091
1092#else
1093
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001094status_t Parcel::readDouble(double *pArg) const
1095{
Andreas Huber84a6d042009-08-17 13:33:27 -07001096 return readAligned(pArg);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001097}
1098
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001099double Parcel::readDouble() const
1100{
Andreas Huber84a6d042009-08-17 13:33:27 -07001101 return readAligned<double>();
1102}
1103
Douglas Leungcc1a4bb2013-01-11 15:00:55 -08001104#endif
1105
Andreas Huber84a6d042009-08-17 13:33:27 -07001106status_t Parcel::readIntPtr(intptr_t *pArg) const
1107{
1108 return readAligned(pArg);
1109}
1110
1111
1112intptr_t Parcel::readIntPtr() const
1113{
1114 return readAligned<intptr_t>();
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001115}
1116
1117
1118const char* Parcel::readCString() const
1119{
1120 const size_t avail = mDataSize-mDataPos;
1121 if (avail > 0) {
1122 const char* str = reinterpret_cast<const char*>(mData+mDataPos);
1123 // is the string's trailing NUL within the parcel's valid bounds?
1124 const char* eos = reinterpret_cast<const char*>(memchr(str, 0, avail));
1125 if (eos) {
1126 const size_t len = eos - str;
1127 mDataPos += PAD_SIZE(len+1);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001128 ALOGV("readCString Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001129 return str;
1130 }
1131 }
1132 return NULL;
1133}
1134
1135String8 Parcel::readString8() const
1136{
1137 int32_t size = readInt32();
1138 // watch for potential int overflow adding 1 for trailing NUL
1139 if (size > 0 && size < INT32_MAX) {
1140 const char* str = (const char*)readInplace(size+1);
1141 if (str) return String8(str, size);
1142 }
1143 return String8();
1144}
1145
1146String16 Parcel::readString16() const
1147{
1148 size_t len;
1149 const char16_t* str = readString16Inplace(&len);
1150 if (str) return String16(str, len);
Steve Blocke6f43dd2012-01-06 19:20:56 +00001151 ALOGE("Reading a NULL string not supported here.");
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001152 return String16();
1153}
1154
1155const char16_t* Parcel::readString16Inplace(size_t* outLen) const
1156{
1157 int32_t size = readInt32();
1158 // watch for potential int overflow from size+1
1159 if (size >= 0 && size < INT32_MAX) {
1160 *outLen = size;
1161 const char16_t* str = (const char16_t*)readInplace((size+1)*sizeof(char16_t));
1162 if (str != NULL) {
1163 return str;
1164 }
1165 }
1166 *outLen = 0;
1167 return NULL;
1168}
1169
1170sp<IBinder> Parcel::readStrongBinder() const
1171{
1172 sp<IBinder> val;
1173 unflatten_binder(ProcessState::self(), *this, &val);
1174 return val;
1175}
1176
1177wp<IBinder> Parcel::readWeakBinder() const
1178{
1179 wp<IBinder> val;
1180 unflatten_binder(ProcessState::self(), *this, &val);
1181 return val;
1182}
1183
Brad Fitzpatrick837a0d02010-07-13 15:33:35 -07001184int32_t Parcel::readExceptionCode() const
1185{
1186 int32_t exception_code = readAligned<int32_t>();
Brad Fitzpatrickd36f4a52010-07-12 11:05:38 -07001187 if (exception_code == EX_HAS_REPLY_HEADER) {
Magnus Strandberg1ba24572011-05-03 15:44:00 +02001188 int32_t header_start = dataPosition();
Brad Fitzpatrickd36f4a52010-07-12 11:05:38 -07001189 int32_t header_size = readAligned<int32_t>();
1190 // Skip over fat responses headers. Not used (or propagated) in
1191 // native code
Magnus Strandberg1ba24572011-05-03 15:44:00 +02001192 setDataPosition(header_start + header_size);
Brad Fitzpatrickd36f4a52010-07-12 11:05:38 -07001193 // And fat response headers are currently only used when there are no
1194 // exceptions, so return no error:
1195 return 0;
1196 }
Brad Fitzpatrick837a0d02010-07-13 15:33:35 -07001197 return exception_code;
1198}
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001199
Mathias Agopiana47f02a2009-05-21 16:29:38 -07001200native_handle* Parcel::readNativeHandle() const
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001201{
1202 int numFds, numInts;
1203 status_t err;
1204 err = readInt32(&numFds);
1205 if (err != NO_ERROR) return 0;
1206 err = readInt32(&numInts);
1207 if (err != NO_ERROR) return 0;
1208
Mathias Agopiana47f02a2009-05-21 16:29:38 -07001209 native_handle* h = native_handle_create(numFds, numInts);
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001210 for (int i=0 ; err==NO_ERROR && i<numFds ; i++) {
Rebecca Schultz Zavin360211f2009-02-13 16:34:38 -08001211 h->data[i] = dup(readFileDescriptor());
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001212 if (h->data[i] < 0) err = BAD_VALUE;
1213 }
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001214 err = read(h->data + numFds, sizeof(int)*numInts);
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001215 if (err != NO_ERROR) {
Mathias Agopiana47f02a2009-05-21 16:29:38 -07001216 native_handle_close(h);
1217 native_handle_delete(h);
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001218 h = 0;
1219 }
1220 return h;
1221}
1222
1223
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001224int Parcel::readFileDescriptor() const
1225{
1226 const flat_binder_object* flat = readObject(true);
1227 if (flat) {
1228 switch (flat->type) {
1229 case BINDER_TYPE_FD:
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001230 //ALOGI("Returning file descriptor %ld from parcel %p", flat->handle, this);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001231 return flat->handle;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001232 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001233 }
1234 return BAD_TYPE;
1235}
1236
Mike Lockwoodcbe36fe2013-09-05 08:41:06 -07001237// WARNING: This method must stay in sync with writeToParcel()
1238// in frameworks/base/core/java/android/os/ParcelFileDescriptor.java
1239int Parcel::readParcelFileDescriptor(int& outCommChannel) const {
1240 int fd;
1241 outCommChannel = -1;
1242
1243 if (readInt32() == 0) {
1244 fd = -1;
1245 } else {
1246 fd = readFileDescriptor();
1247 if (fd >= 0 && readInt32() != 0) {
1248 outCommChannel = readFileDescriptor();
1249 }
1250 }
1251 return fd;
1252}
1253
Jeff Brown5707dbf2011-09-23 21:17:56 -07001254status_t Parcel::readBlob(size_t len, ReadableBlob* outBlob) const
1255{
1256 int32_t useAshmem;
1257 status_t status = readInt32(&useAshmem);
1258 if (status) return status;
1259
1260 if (!useAshmem) {
Steve Block6807e592011-10-20 11:56:00 +01001261 ALOGV("readBlob: read in place");
Jeff Brown5707dbf2011-09-23 21:17:56 -07001262 const void* ptr = readInplace(len);
1263 if (!ptr) return BAD_VALUE;
1264
1265 outBlob->init(false /*mapped*/, const_cast<void*>(ptr), len);
1266 return NO_ERROR;
1267 }
1268
Steve Block6807e592011-10-20 11:56:00 +01001269 ALOGV("readBlob: read from ashmem");
Jeff Brown5707dbf2011-09-23 21:17:56 -07001270 int fd = readFileDescriptor();
1271 if (fd == int(BAD_TYPE)) return BAD_VALUE;
1272
1273 void* ptr = ::mmap(NULL, len, PROT_READ, MAP_SHARED, fd, 0);
Narayan Kamath9ea09752014-10-08 17:35:45 +01001274 if (ptr == MAP_FAILED) return NO_MEMORY;
Jeff Brown5707dbf2011-09-23 21:17:56 -07001275
1276 outBlob->init(true /*mapped*/, ptr, len);
1277 return NO_ERROR;
1278}
1279
Mathias Agopiane1424282013-07-29 21:24:40 -07001280status_t Parcel::read(FlattenableHelperInterface& val) const
Mathias Agopian98e71dd2010-02-11 17:30:52 -08001281{
1282 // size
1283 const size_t len = this->readInt32();
1284 const size_t fd_count = this->readInt32();
1285
1286 // payload
Mathias Agopiane1424282013-07-29 21:24:40 -07001287 void const* const buf = this->readInplace(PAD_SIZE(len));
Mathias Agopian98e71dd2010-02-11 17:30:52 -08001288 if (buf == NULL)
1289 return BAD_VALUE;
1290
1291 int* fds = NULL;
1292 if (fd_count) {
1293 fds = new int[fd_count];
1294 }
1295
1296 status_t err = NO_ERROR;
1297 for (size_t i=0 ; i<fd_count && err==NO_ERROR ; i++) {
Jesse Hallfee99042014-11-04 08:36:31 -08001298 fds[i] = dup(this->readFileDescriptor());
Jun Jiangabf8a2c2014-04-29 14:22:10 +08001299 if (fds[i] < 0) {
1300 err = BAD_VALUE;
Jesse Hallfee99042014-11-04 08:36:31 -08001301 ALOGE("dup() failed in Parcel::read, i is %zu, fds[i] is %d, fd_count is %zu, error: %s",
1302 i, fds[i], fd_count, strerror(errno));
Jun Jiangabf8a2c2014-04-29 14:22:10 +08001303 }
Mathias Agopian98e71dd2010-02-11 17:30:52 -08001304 }
1305
1306 if (err == NO_ERROR) {
1307 err = val.unflatten(buf, len, fds, fd_count);
1308 }
1309
1310 if (fd_count) {
1311 delete [] fds;
1312 }
1313
1314 return err;
1315}
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001316const flat_binder_object* Parcel::readObject(bool nullMetaData) const
1317{
1318 const size_t DPOS = mDataPos;
1319 if ((DPOS+sizeof(flat_binder_object)) <= mDataSize) {
1320 const flat_binder_object* obj
1321 = reinterpret_cast<const flat_binder_object*>(mData+DPOS);
1322 mDataPos = DPOS + sizeof(flat_binder_object);
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001323 if (!nullMetaData && (obj->cookie == 0 && obj->binder == 0)) {
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001324 // When transferring a NULL object, we don't write it into
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001325 // the object list, so we don't want to check for it when
1326 // reading.
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001327 ALOGV("readObject Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001328 return obj;
1329 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001330
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001331 // Ensure that this object is valid...
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001332 binder_size_t* const OBJS = mObjects;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001333 const size_t N = mObjectsSize;
1334 size_t opos = mNextObjectHint;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001335
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001336 if (N > 0) {
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001337 ALOGV("Parcel %p looking for obj at %zu, hint=%zu",
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001338 this, DPOS, opos);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001339
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001340 // Start at the current hint position, looking for an object at
1341 // the current data position.
1342 if (opos < N) {
1343 while (opos < (N-1) && OBJS[opos] < DPOS) {
1344 opos++;
1345 }
1346 } else {
1347 opos = N-1;
1348 }
1349 if (OBJS[opos] == DPOS) {
1350 // Found it!
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001351 ALOGV("Parcel %p found obj %zu at index %zu with forward search",
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001352 this, DPOS, opos);
1353 mNextObjectHint = opos+1;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001354 ALOGV("readObject Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001355 return obj;
1356 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001357
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001358 // Look backwards for it...
1359 while (opos > 0 && OBJS[opos] > DPOS) {
1360 opos--;
1361 }
1362 if (OBJS[opos] == DPOS) {
1363 // Found it!
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001364 ALOGV("Parcel %p found obj %zu at index %zu with backward search",
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001365 this, DPOS, opos);
1366 mNextObjectHint = opos+1;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001367 ALOGV("readObject Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001368 return obj;
1369 }
1370 }
Colin Cross6f4f3ab2014-02-05 17:42:44 -08001371 ALOGW("Attempt to read object from Parcel %p at offset %zu that is not in the object list",
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001372 this, DPOS);
1373 }
1374 return NULL;
1375}
1376
1377void Parcel::closeFileDescriptors()
1378{
1379 size_t i = mObjectsSize;
1380 if (i > 0) {
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001381 //ALOGI("Closing file descriptors for %zu objects...", i);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001382 }
1383 while (i > 0) {
1384 i--;
1385 const flat_binder_object* flat
1386 = reinterpret_cast<flat_binder_object*>(mData+mObjects[i]);
1387 if (flat->type == BINDER_TYPE_FD) {
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001388 //ALOGI("Closing fd: %ld", flat->handle);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001389 close(flat->handle);
1390 }
1391 }
1392}
1393
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001394uintptr_t Parcel::ipcData() const
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001395{
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001396 return reinterpret_cast<uintptr_t>(mData);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001397}
1398
1399size_t Parcel::ipcDataSize() const
1400{
1401 return (mDataSize > mDataPos ? mDataSize : mDataPos);
1402}
1403
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001404uintptr_t Parcel::ipcObjects() const
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001405{
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001406 return reinterpret_cast<uintptr_t>(mObjects);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001407}
1408
1409size_t Parcel::ipcObjectsCount() const
1410{
1411 return mObjectsSize;
1412}
1413
1414void Parcel::ipcSetDataReference(const uint8_t* data, size_t dataSize,
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001415 const binder_size_t* objects, size_t objectsCount, release_func relFunc, void* relCookie)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001416{
Arve Hjønnevåg6f286112014-02-19 20:42:13 -08001417 binder_size_t minOffset = 0;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001418 freeDataNoInit();
1419 mError = NO_ERROR;
1420 mData = const_cast<uint8_t*>(data);
1421 mDataSize = mDataCapacity = dataSize;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001422 //ALOGI("setDataReference Setting data size of %p to %lu (pid=%d)", this, mDataSize, getpid());
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001423 mDataPos = 0;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001424 ALOGV("setDataReference Setting data pos of %p to %zu", this, mDataPos);
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001425 mObjects = const_cast<binder_size_t*>(objects);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001426 mObjectsSize = mObjectsCapacity = objectsCount;
1427 mNextObjectHint = 0;
1428 mOwner = relFunc;
1429 mOwnerCookie = relCookie;
Arve Hjønnevågf50b9ea2014-02-13 19:22:08 -08001430 for (size_t i = 0; i < mObjectsSize; i++) {
Arve Hjønnevåg6f286112014-02-19 20:42:13 -08001431 binder_size_t offset = mObjects[i];
Arve Hjønnevågf50b9ea2014-02-13 19:22:08 -08001432 if (offset < minOffset) {
Arve Hjønnevåg6f286112014-02-19 20:42:13 -08001433 ALOGE("%s: bad object offset %"PRIu64" < %"PRIu64"\n",
1434 __func__, (uint64_t)offset, (uint64_t)minOffset);
Arve Hjønnevågf50b9ea2014-02-13 19:22:08 -08001435 mObjectsSize = 0;
1436 break;
1437 }
1438 minOffset = offset + sizeof(flat_binder_object);
1439 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001440 scanForFds();
1441}
1442
Colin Cross6f4f3ab2014-02-05 17:42:44 -08001443void Parcel::print(TextOutput& to, uint32_t /*flags*/) const
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001444{
1445 to << "Parcel(";
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001446
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001447 if (errorCheck() != NO_ERROR) {
1448 const status_t err = errorCheck();
Colin Cross6f4f3ab2014-02-05 17:42:44 -08001449 to << "Error: " << (void*)(intptr_t)err << " \"" << strerror(-err) << "\"";
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001450 } else if (dataSize() > 0) {
1451 const uint8_t* DATA = data();
1452 to << indent << HexDump(DATA, dataSize()) << dedent;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001453 const binder_size_t* OBJS = objects();
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001454 const size_t N = objectsCount();
1455 for (size_t i=0; i<N; i++) {
1456 const flat_binder_object* flat
1457 = reinterpret_cast<const flat_binder_object*>(DATA+OBJS[i]);
1458 to << endl << "Object #" << i << " @ " << (void*)OBJS[i] << ": "
1459 << TypeCode(flat->type & 0x7f7f7f00)
1460 << " = " << flat->binder;
1461 }
1462 } else {
1463 to << "NULL";
1464 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001465
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001466 to << ")";
1467}
1468
1469void Parcel::releaseObjects()
1470{
1471 const sp<ProcessState> proc(ProcessState::self());
1472 size_t i = mObjectsSize;
1473 uint8_t* const data = mData;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001474 binder_size_t* const objects = mObjects;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001475 while (i > 0) {
1476 i--;
1477 const flat_binder_object* flat
1478 = reinterpret_cast<flat_binder_object*>(data+objects[i]);
1479 release_object(proc, *flat, this);
1480 }
1481}
1482
1483void Parcel::acquireObjects()
1484{
1485 const sp<ProcessState> proc(ProcessState::self());
1486 size_t i = mObjectsSize;
1487 uint8_t* const data = mData;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001488 binder_size_t* const objects = mObjects;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001489 while (i > 0) {
1490 i--;
1491 const flat_binder_object* flat
1492 = reinterpret_cast<flat_binder_object*>(data+objects[i]);
1493 acquire_object(proc, *flat, this);
1494 }
1495}
1496
1497void Parcel::freeData()
1498{
1499 freeDataNoInit();
1500 initState();
1501}
1502
1503void Parcel::freeDataNoInit()
1504{
1505 if (mOwner) {
Dianne Hackborn7e790af2014-11-11 12:22:53 -08001506 LOG_ALLOC("Parcel %p: freeing other owner data", this);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001507 //ALOGI("Freeing data ref of %p (pid=%d)", this, getpid());
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001508 mOwner(this, mData, mDataSize, mObjects, mObjectsSize, mOwnerCookie);
1509 } else {
Dianne Hackborn7e790af2014-11-11 12:22:53 -08001510 LOG_ALLOC("Parcel %p: freeing allocated data", this);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001511 releaseObjects();
Dianne Hackborn7e790af2014-11-11 12:22:53 -08001512 if (mData) {
1513 LOG_ALLOC("Parcel %p: freeing with %zu capacity", this, mDataCapacity);
1514 gParcelGlobalAllocSizeLock.lock();
1515 gParcelGlobalAllocSize -= mDataCapacity;
1516 gParcelGlobalAllocCount--;
1517 gParcelGlobalAllocSizeLock.unlock();
1518 free(mData);
1519 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001520 if (mObjects) free(mObjects);
1521 }
1522}
1523
1524status_t Parcel::growData(size_t len)
1525{
1526 size_t newSize = ((mDataSize+len)*3)/2;
1527 return (newSize <= mDataSize)
1528 ? (status_t) NO_MEMORY
1529 : continueWrite(newSize);
1530}
1531
1532status_t Parcel::restartWrite(size_t desired)
1533{
1534 if (mOwner) {
1535 freeData();
1536 return continueWrite(desired);
1537 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001538
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001539 uint8_t* data = (uint8_t*)realloc(mData, desired);
1540 if (!data && desired > mDataCapacity) {
1541 mError = NO_MEMORY;
1542 return NO_MEMORY;
1543 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001544
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001545 releaseObjects();
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001546
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001547 if (data) {
Dianne Hackborn7e790af2014-11-11 12:22:53 -08001548 LOG_ALLOC("Parcel %p: restart from %zu to %zu capacity", this, mDataCapacity, desired);
1549 gParcelGlobalAllocSizeLock.lock();
1550 gParcelGlobalAllocSize += desired;
1551 gParcelGlobalAllocSize -= mDataCapacity;
1552 gParcelGlobalAllocSizeLock.unlock();
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001553 mData = data;
1554 mDataCapacity = desired;
1555 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001556
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001557 mDataSize = mDataPos = 0;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001558 ALOGV("restartWrite Setting data size of %p to %zu", this, mDataSize);
1559 ALOGV("restartWrite Setting data pos of %p to %zu", this, mDataPos);
1560
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001561 free(mObjects);
1562 mObjects = NULL;
1563 mObjectsSize = mObjectsCapacity = 0;
1564 mNextObjectHint = 0;
1565 mHasFds = false;
1566 mFdsKnown = true;
Dianne Hackborn8938ed22011-09-28 23:19:47 -04001567 mAllowFds = true;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001568
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001569 return NO_ERROR;
1570}
1571
1572status_t Parcel::continueWrite(size_t desired)
1573{
1574 // If shrinking, first adjust for any objects that appear
1575 // after the new data size.
1576 size_t objectsSize = mObjectsSize;
1577 if (desired < mDataSize) {
1578 if (desired == 0) {
1579 objectsSize = 0;
1580 } else {
1581 while (objectsSize > 0) {
1582 if (mObjects[objectsSize-1] < desired)
1583 break;
1584 objectsSize--;
1585 }
1586 }
1587 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001588
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001589 if (mOwner) {
1590 // If the size is going to zero, just release the owner's data.
1591 if (desired == 0) {
1592 freeData();
1593 return NO_ERROR;
1594 }
1595
1596 // If there is a different owner, we need to take
1597 // posession.
1598 uint8_t* data = (uint8_t*)malloc(desired);
1599 if (!data) {
1600 mError = NO_MEMORY;
1601 return NO_MEMORY;
1602 }
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001603 binder_size_t* objects = NULL;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001604
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001605 if (objectsSize) {
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001606 objects = (binder_size_t*)malloc(objectsSize*sizeof(binder_size_t));
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001607 if (!objects) {
Hyejin Kim3f727c02013-03-09 11:28:54 +09001608 free(data);
1609
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001610 mError = NO_MEMORY;
1611 return NO_MEMORY;
1612 }
1613
1614 // Little hack to only acquire references on objects
1615 // we will be keeping.
1616 size_t oldObjectsSize = mObjectsSize;
1617 mObjectsSize = objectsSize;
1618 acquireObjects();
1619 mObjectsSize = oldObjectsSize;
1620 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001621
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001622 if (mData) {
1623 memcpy(data, mData, mDataSize < desired ? mDataSize : desired);
1624 }
1625 if (objects && mObjects) {
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001626 memcpy(objects, mObjects, objectsSize*sizeof(binder_size_t));
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001627 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001628 //ALOGI("Freeing data ref of %p (pid=%d)", this, getpid());
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001629 mOwner(this, mData, mDataSize, mObjects, mObjectsSize, mOwnerCookie);
1630 mOwner = NULL;
1631
Dianne Hackborn7e790af2014-11-11 12:22:53 -08001632 LOG_ALLOC("Parcel %p: taking ownership of %zu capacity", this, desired);
1633 gParcelGlobalAllocSizeLock.lock();
1634 gParcelGlobalAllocSize += desired;
1635 gParcelGlobalAllocCount++;
1636 gParcelGlobalAllocSizeLock.unlock();
1637
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001638 mData = data;
1639 mObjects = objects;
1640 mDataSize = (mDataSize < desired) ? mDataSize : desired;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001641 ALOGV("continueWrite Setting data size of %p to %zu", this, mDataSize);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001642 mDataCapacity = desired;
1643 mObjectsSize = mObjectsCapacity = objectsSize;
1644 mNextObjectHint = 0;
1645
1646 } else if (mData) {
1647 if (objectsSize < mObjectsSize) {
1648 // Need to release refs on any objects we are dropping.
1649 const sp<ProcessState> proc(ProcessState::self());
1650 for (size_t i=objectsSize; i<mObjectsSize; i++) {
1651 const flat_binder_object* flat
1652 = reinterpret_cast<flat_binder_object*>(mData+mObjects[i]);
1653 if (flat->type == BINDER_TYPE_FD) {
1654 // will need to rescan because we may have lopped off the only FDs
1655 mFdsKnown = false;
1656 }
1657 release_object(proc, *flat, this);
1658 }
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001659 binder_size_t* objects =
1660 (binder_size_t*)realloc(mObjects, objectsSize*sizeof(binder_size_t));
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001661 if (objects) {
1662 mObjects = objects;
1663 }
1664 mObjectsSize = objectsSize;
1665 mNextObjectHint = 0;
1666 }
1667
1668 // We own the data, so we can just do a realloc().
1669 if (desired > mDataCapacity) {
1670 uint8_t* data = (uint8_t*)realloc(mData, desired);
1671 if (data) {
Dianne Hackborn7e790af2014-11-11 12:22:53 -08001672 LOG_ALLOC("Parcel %p: continue from %zu to %zu capacity", this, mDataCapacity,
1673 desired);
1674 gParcelGlobalAllocSizeLock.lock();
1675 gParcelGlobalAllocSize += desired;
1676 gParcelGlobalAllocSize -= mDataCapacity;
1677 gParcelGlobalAllocSizeLock.unlock();
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001678 mData = data;
1679 mDataCapacity = desired;
1680 } else if (desired > mDataCapacity) {
1681 mError = NO_MEMORY;
1682 return NO_MEMORY;
1683 }
1684 } else {
Dianne Hackborn97e2bcd2011-04-13 18:15:56 -07001685 if (mDataSize > desired) {
1686 mDataSize = desired;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001687 ALOGV("continueWrite Setting data size of %p to %zu", this, mDataSize);
Dianne Hackborn97e2bcd2011-04-13 18:15:56 -07001688 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001689 if (mDataPos > desired) {
1690 mDataPos = desired;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001691 ALOGV("continueWrite Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001692 }
1693 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001694
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001695 } else {
1696 // This is the first data. Easy!
1697 uint8_t* data = (uint8_t*)malloc(desired);
1698 if (!data) {
1699 mError = NO_MEMORY;
1700 return NO_MEMORY;
1701 }
Hyejin Kim3f727c02013-03-09 11:28:54 +09001702
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001703 if(!(mDataCapacity == 0 && mObjects == NULL
1704 && mObjectsCapacity == 0)) {
Colin Cross6f4f3ab2014-02-05 17:42:44 -08001705 ALOGE("continueWrite: %zu/%p/%zu/%zu", mDataCapacity, mObjects, mObjectsCapacity, desired);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001706 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001707
Dianne Hackborn7e790af2014-11-11 12:22:53 -08001708 LOG_ALLOC("Parcel %p: allocating with %zu capacity", this, desired);
1709 gParcelGlobalAllocSizeLock.lock();
1710 gParcelGlobalAllocSize += desired;
1711 gParcelGlobalAllocCount++;
1712 gParcelGlobalAllocSizeLock.unlock();
1713
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001714 mData = data;
1715 mDataSize = mDataPos = 0;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001716 ALOGV("continueWrite Setting data size of %p to %zu", this, mDataSize);
1717 ALOGV("continueWrite Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001718 mDataCapacity = desired;
1719 }
1720
1721 return NO_ERROR;
1722}
1723
1724void Parcel::initState()
1725{
Dianne Hackborn7e790af2014-11-11 12:22:53 -08001726 LOG_ALLOC("Parcel %p: initState", this);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001727 mError = NO_ERROR;
1728 mData = 0;
1729 mDataSize = 0;
1730 mDataCapacity = 0;
1731 mDataPos = 0;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001732 ALOGV("initState Setting data size of %p to %zu", this, mDataSize);
1733 ALOGV("initState Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001734 mObjects = NULL;
1735 mObjectsSize = 0;
1736 mObjectsCapacity = 0;
1737 mNextObjectHint = 0;
1738 mHasFds = false;
1739 mFdsKnown = true;
Dianne Hackborn8938ed22011-09-28 23:19:47 -04001740 mAllowFds = true;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001741 mOwner = NULL;
1742}
1743
1744void Parcel::scanForFds() const
1745{
1746 bool hasFds = false;
1747 for (size_t i=0; i<mObjectsSize; i++) {
1748 const flat_binder_object* flat
1749 = reinterpret_cast<const flat_binder_object*>(mData + mObjects[i]);
1750 if (flat->type == BINDER_TYPE_FD) {
1751 hasFds = true;
1752 break;
1753 }
1754 }
1755 mHasFds = hasFds;
1756 mFdsKnown = true;
1757}
1758
Jeff Brown5707dbf2011-09-23 21:17:56 -07001759// --- Parcel::Blob ---
1760
1761Parcel::Blob::Blob() :
1762 mMapped(false), mData(NULL), mSize(0) {
1763}
1764
1765Parcel::Blob::~Blob() {
1766 release();
1767}
1768
1769void Parcel::Blob::release() {
1770 if (mMapped && mData) {
1771 ::munmap(mData, mSize);
1772 }
1773 clear();
1774}
1775
1776void Parcel::Blob::init(bool mapped, void* data, size_t size) {
1777 mMapped = mapped;
1778 mData = data;
1779 mSize = size;
1780}
1781
1782void Parcel::Blob::clear() {
1783 mMapped = false;
1784 mData = NULL;
1785 mSize = 0;
1786}
1787
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001788}; // namespace android