blob: 3ff3410fea50225462b32719fa25162ecef90c7b [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
Dianne Hackborna4cff882014-11-13 17:07:40 -080078static pthread_mutex_t gParcelGlobalAllocSizeLock = PTHREAD_MUTEX_INITIALIZER;
79static size_t gParcelGlobalAllocSize = 0;
80static size_t gParcelGlobalAllocCount = 0;
81
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -070082void acquire_object(const sp<ProcessState>& proc,
83 const flat_binder_object& obj, const void* who)
84{
85 switch (obj.type) {
86 case BINDER_TYPE_BINDER:
87 if (obj.binder) {
88 LOG_REFS("Parcel %p acquiring reference on local %p", who, obj.cookie);
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -080089 reinterpret_cast<IBinder*>(obj.cookie)->incStrong(who);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -070090 }
91 return;
92 case BINDER_TYPE_WEAK_BINDER:
93 if (obj.binder)
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -080094 reinterpret_cast<RefBase::weakref_type*>(obj.binder)->incWeak(who);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -070095 return;
96 case BINDER_TYPE_HANDLE: {
97 const sp<IBinder> b = proc->getStrongProxyForHandle(obj.handle);
98 if (b != NULL) {
99 LOG_REFS("Parcel %p acquiring reference on remote %p", who, b.get());
100 b->incStrong(who);
101 }
102 return;
103 }
104 case BINDER_TYPE_WEAK_HANDLE: {
105 const wp<IBinder> b = proc->getWeakProxyForHandle(obj.handle);
106 if (b != NULL) b.get_refs()->incWeak(who);
107 return;
108 }
109 case BINDER_TYPE_FD: {
110 // intentionally blank -- nothing to do to acquire this, but we do
111 // recognize it as a legitimate object type.
112 return;
113 }
114 }
115
Colin Cross6f4f3ab2014-02-05 17:42:44 -0800116 ALOGD("Invalid object type 0x%08x", obj.type);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700117}
118
119void release_object(const sp<ProcessState>& proc,
120 const flat_binder_object& obj, const void* who)
121{
122 switch (obj.type) {
123 case BINDER_TYPE_BINDER:
124 if (obj.binder) {
125 LOG_REFS("Parcel %p releasing reference on local %p", who, obj.cookie);
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800126 reinterpret_cast<IBinder*>(obj.cookie)->decStrong(who);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700127 }
128 return;
129 case BINDER_TYPE_WEAK_BINDER:
130 if (obj.binder)
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800131 reinterpret_cast<RefBase::weakref_type*>(obj.binder)->decWeak(who);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700132 return;
133 case BINDER_TYPE_HANDLE: {
134 const sp<IBinder> b = proc->getStrongProxyForHandle(obj.handle);
135 if (b != NULL) {
136 LOG_REFS("Parcel %p releasing reference on remote %p", who, b.get());
137 b->decStrong(who);
138 }
139 return;
140 }
141 case BINDER_TYPE_WEAK_HANDLE: {
142 const wp<IBinder> b = proc->getWeakProxyForHandle(obj.handle);
143 if (b != NULL) b.get_refs()->decWeak(who);
144 return;
145 }
146 case BINDER_TYPE_FD: {
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800147 if (obj.cookie != 0) close(obj.handle);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700148 return;
149 }
150 }
151
Colin Cross6f4f3ab2014-02-05 17:42:44 -0800152 ALOGE("Invalid object type 0x%08x", obj.type);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700153}
154
155inline static status_t finish_flatten_binder(
Colin Cross6f4f3ab2014-02-05 17:42:44 -0800156 const sp<IBinder>& /*binder*/, const flat_binder_object& flat, Parcel* out)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700157{
158 return out->writeObject(flat, false);
159}
160
Colin Cross6f4f3ab2014-02-05 17:42:44 -0800161status_t flatten_binder(const sp<ProcessState>& /*proc*/,
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700162 const sp<IBinder>& binder, Parcel* out)
163{
164 flat_binder_object obj;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700165
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700166 obj.flags = 0x7f | FLAT_BINDER_FLAG_ACCEPTS_FDS;
167 if (binder != NULL) {
168 IBinder *local = binder->localBinder();
169 if (!local) {
170 BpBinder *proxy = binder->remoteBinder();
171 if (proxy == NULL) {
Steve Blocke6f43dd2012-01-06 19:20:56 +0000172 ALOGE("null proxy");
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700173 }
174 const int32_t handle = proxy ? proxy->handle() : 0;
175 obj.type = BINDER_TYPE_HANDLE;
Arve Hjønnevåg07fd0f12014-02-18 21:10:29 -0800176 obj.binder = 0; /* Don't pass uninitialized stack data to a remote process */
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700177 obj.handle = handle;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800178 obj.cookie = 0;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700179 } else {
180 obj.type = BINDER_TYPE_BINDER;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800181 obj.binder = reinterpret_cast<uintptr_t>(local->getWeakRefs());
182 obj.cookie = reinterpret_cast<uintptr_t>(local);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700183 }
184 } else {
185 obj.type = BINDER_TYPE_BINDER;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800186 obj.binder = 0;
187 obj.cookie = 0;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700188 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700189
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700190 return finish_flatten_binder(binder, obj, out);
191}
192
Colin Cross6f4f3ab2014-02-05 17:42:44 -0800193status_t flatten_binder(const sp<ProcessState>& /*proc*/,
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700194 const wp<IBinder>& binder, Parcel* out)
195{
196 flat_binder_object obj;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700197
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700198 obj.flags = 0x7f | FLAT_BINDER_FLAG_ACCEPTS_FDS;
199 if (binder != NULL) {
200 sp<IBinder> real = binder.promote();
201 if (real != NULL) {
202 IBinder *local = real->localBinder();
203 if (!local) {
204 BpBinder *proxy = real->remoteBinder();
205 if (proxy == NULL) {
Steve Blocke6f43dd2012-01-06 19:20:56 +0000206 ALOGE("null proxy");
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700207 }
208 const int32_t handle = proxy ? proxy->handle() : 0;
209 obj.type = BINDER_TYPE_WEAK_HANDLE;
Arve Hjønnevåg07fd0f12014-02-18 21:10:29 -0800210 obj.binder = 0; /* Don't pass uninitialized stack data to a remote process */
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700211 obj.handle = handle;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800212 obj.cookie = 0;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700213 } else {
214 obj.type = BINDER_TYPE_WEAK_BINDER;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800215 obj.binder = reinterpret_cast<uintptr_t>(binder.get_refs());
216 obj.cookie = reinterpret_cast<uintptr_t>(binder.unsafe_get());
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700217 }
218 return finish_flatten_binder(real, obj, out);
219 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700220
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700221 // XXX How to deal? In order to flatten the given binder,
222 // we need to probe it for information, which requires a primary
223 // reference... but we don't have one.
224 //
225 // The OpenBinder implementation uses a dynamic_cast<> here,
226 // but we can't do that with the different reference counting
227 // implementation we are using.
Steve Blocke6f43dd2012-01-06 19:20:56 +0000228 ALOGE("Unable to unflatten Binder weak reference!");
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700229 obj.type = BINDER_TYPE_BINDER;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800230 obj.binder = 0;
231 obj.cookie = 0;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700232 return finish_flatten_binder(NULL, obj, out);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700233
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700234 } else {
235 obj.type = BINDER_TYPE_BINDER;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800236 obj.binder = 0;
237 obj.cookie = 0;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700238 return finish_flatten_binder(NULL, obj, out);
239 }
240}
241
242inline static status_t finish_unflatten_binder(
Colin Cross6f4f3ab2014-02-05 17:42:44 -0800243 BpBinder* /*proxy*/, const flat_binder_object& /*flat*/,
244 const Parcel& /*in*/)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700245{
246 return NO_ERROR;
247}
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700248
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700249status_t unflatten_binder(const sp<ProcessState>& proc,
250 const Parcel& in, sp<IBinder>* out)
251{
252 const flat_binder_object* flat = in.readObject(false);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700253
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700254 if (flat) {
255 switch (flat->type) {
256 case BINDER_TYPE_BINDER:
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800257 *out = reinterpret_cast<IBinder*>(flat->cookie);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700258 return finish_unflatten_binder(NULL, *flat, in);
259 case BINDER_TYPE_HANDLE:
260 *out = proc->getStrongProxyForHandle(flat->handle);
261 return finish_unflatten_binder(
262 static_cast<BpBinder*>(out->get()), *flat, in);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700263 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700264 }
265 return BAD_TYPE;
266}
267
268status_t unflatten_binder(const sp<ProcessState>& proc,
269 const Parcel& in, wp<IBinder>* out)
270{
271 const flat_binder_object* flat = in.readObject(false);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700272
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700273 if (flat) {
274 switch (flat->type) {
275 case BINDER_TYPE_BINDER:
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800276 *out = reinterpret_cast<IBinder*>(flat->cookie);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700277 return finish_unflatten_binder(NULL, *flat, in);
278 case BINDER_TYPE_WEAK_BINDER:
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800279 if (flat->binder != 0) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700280 out->set_object_and_refs(
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800281 reinterpret_cast<IBinder*>(flat->cookie),
282 reinterpret_cast<RefBase::weakref_type*>(flat->binder));
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700283 } else {
284 *out = NULL;
285 }
286 return finish_unflatten_binder(NULL, *flat, in);
287 case BINDER_TYPE_HANDLE:
288 case BINDER_TYPE_WEAK_HANDLE:
289 *out = proc->getWeakProxyForHandle(flat->handle);
290 return finish_unflatten_binder(
291 static_cast<BpBinder*>(out->unsafe_get()), *flat, in);
292 }
293 }
294 return BAD_TYPE;
295}
296
297// ---------------------------------------------------------------------------
298
299Parcel::Parcel()
300{
Dianne Hackborn7e790af2014-11-11 12:22:53 -0800301 LOG_ALLOC("Parcel %p: constructing", this);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700302 initState();
303}
304
305Parcel::~Parcel()
306{
307 freeDataNoInit();
Dianne Hackborn7e790af2014-11-11 12:22:53 -0800308 LOG_ALLOC("Parcel %p: destroyed", this);
309}
310
311size_t Parcel::getGlobalAllocSize() {
Dianne Hackborna4cff882014-11-13 17:07:40 -0800312 pthread_mutex_lock(&gParcelGlobalAllocSizeLock);
313 size_t size = gParcelGlobalAllocSize;
314 pthread_mutex_unlock(&gParcelGlobalAllocSizeLock);
315 return size;
Dianne Hackborn7e790af2014-11-11 12:22:53 -0800316}
317
318size_t Parcel::getGlobalAllocCount() {
Dianne Hackborna4cff882014-11-13 17:07:40 -0800319 pthread_mutex_lock(&gParcelGlobalAllocSizeLock);
320 size_t count = gParcelGlobalAllocCount;
321 pthread_mutex_unlock(&gParcelGlobalAllocSizeLock);
322 return count;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700323}
324
325const uint8_t* Parcel::data() const
326{
327 return mData;
328}
329
330size_t Parcel::dataSize() const
331{
332 return (mDataSize > mDataPos ? mDataSize : mDataPos);
333}
334
335size_t Parcel::dataAvail() const
336{
337 // TODO: decide what to do about the possibility that this can
338 // report an available-data size that exceeds a Java int's max
339 // positive value, causing havoc. Fortunately this will only
340 // happen if someone constructs a Parcel containing more than two
341 // gigabytes of data, which on typical phone hardware is simply
342 // not possible.
343 return dataSize() - dataPosition();
344}
345
346size_t Parcel::dataPosition() const
347{
348 return mDataPos;
349}
350
351size_t Parcel::dataCapacity() const
352{
353 return mDataCapacity;
354}
355
356status_t Parcel::setDataSize(size_t size)
357{
358 status_t err;
359 err = continueWrite(size);
360 if (err == NO_ERROR) {
361 mDataSize = size;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700362 ALOGV("setDataSize Setting data size of %p to %zu", this, mDataSize);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700363 }
364 return err;
365}
366
367void Parcel::setDataPosition(size_t pos) const
368{
369 mDataPos = pos;
370 mNextObjectHint = 0;
371}
372
373status_t Parcel::setDataCapacity(size_t size)
374{
Dianne Hackborn97e2bcd2011-04-13 18:15:56 -0700375 if (size > mDataCapacity) return continueWrite(size);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700376 return NO_ERROR;
377}
378
379status_t Parcel::setData(const uint8_t* buffer, size_t len)
380{
381 status_t err = restartWrite(len);
382 if (err == NO_ERROR) {
383 memcpy(const_cast<uint8_t*>(data()), buffer, len);
384 mDataSize = len;
385 mFdsKnown = false;
386 }
387 return err;
388}
389
Andreas Huber51faf462011-04-13 10:21:56 -0700390status_t Parcel::appendFrom(const Parcel *parcel, size_t offset, size_t len)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700391{
392 const sp<ProcessState> proc(ProcessState::self());
393 status_t err;
Andreas Huber51faf462011-04-13 10:21:56 -0700394 const uint8_t *data = parcel->mData;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800395 const binder_size_t *objects = parcel->mObjects;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700396 size_t size = parcel->mObjectsSize;
397 int startPos = mDataPos;
398 int firstIndex = -1, lastIndex = -2;
399
400 if (len == 0) {
401 return NO_ERROR;
402 }
403
404 // range checks against the source parcel size
405 if ((offset > parcel->mDataSize)
406 || (len > parcel->mDataSize)
407 || (offset + len > parcel->mDataSize)) {
408 return BAD_VALUE;
409 }
410
411 // Count objects in range
412 for (int i = 0; i < (int) size; i++) {
413 size_t off = objects[i];
Christopher Tatee3cb9bc2015-05-27 17:53:02 -0700414 if ((off >= offset) && (off + sizeof(flat_binder_object) <= offset + len)) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700415 if (firstIndex == -1) {
416 firstIndex = i;
417 }
418 lastIndex = i;
419 }
420 }
421 int numObjects = lastIndex - firstIndex + 1;
422
Dianne Hackborn97e2bcd2011-04-13 18:15:56 -0700423 if ((mDataSize+len) > mDataCapacity) {
424 // grow data
425 err = growData(len);
426 if (err != NO_ERROR) {
427 return err;
428 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700429 }
430
431 // append data
432 memcpy(mData + mDataPos, data + offset, len);
433 mDataPos += len;
434 mDataSize += len;
435
Dianne Hackborn8938ed22011-09-28 23:19:47 -0400436 err = NO_ERROR;
437
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700438 if (numObjects > 0) {
439 // grow objects
440 if (mObjectsCapacity < mObjectsSize + numObjects) {
441 int newSize = ((mObjectsSize + numObjects)*3)/2;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800442 binder_size_t *objects =
443 (binder_size_t*)realloc(mObjects, newSize*sizeof(binder_size_t));
444 if (objects == (binder_size_t*)0) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700445 return NO_MEMORY;
446 }
447 mObjects = objects;
448 mObjectsCapacity = newSize;
449 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700450
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700451 // append and acquire objects
452 int idx = mObjectsSize;
453 for (int i = firstIndex; i <= lastIndex; i++) {
454 size_t off = objects[i] - offset + startPos;
455 mObjects[idx++] = off;
456 mObjectsSize++;
457
Dianne Hackborn8af0f822009-05-22 13:20:23 -0700458 flat_binder_object* flat
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700459 = reinterpret_cast<flat_binder_object*>(mData + off);
460 acquire_object(proc, *flat, this);
461
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700462 if (flat->type == BINDER_TYPE_FD) {
Dianne Hackborn8af0f822009-05-22 13:20:23 -0700463 // If this is a file descriptor, we need to dup it so the
464 // new Parcel now owns its own fd, and can declare that we
465 // officially know we have fds.
466 flat->handle = dup(flat->handle);
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800467 flat->cookie = 1;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700468 mHasFds = mFdsKnown = true;
Dianne Hackborn8938ed22011-09-28 23:19:47 -0400469 if (!mAllowFds) {
470 err = FDS_NOT_ALLOWED;
471 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700472 }
473 }
474 }
475
Dianne Hackborn8938ed22011-09-28 23:19:47 -0400476 return err;
477}
478
Dianne Hackborn7746cc32011-10-03 21:09:35 -0700479bool Parcel::pushAllowFds(bool allowFds)
Dianne Hackborn8938ed22011-09-28 23:19:47 -0400480{
481 const bool origValue = mAllowFds;
Dianne Hackborn7746cc32011-10-03 21:09:35 -0700482 if (!allowFds) {
483 mAllowFds = false;
484 }
Dianne Hackborn8938ed22011-09-28 23:19:47 -0400485 return origValue;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700486}
487
Dianne Hackborn7746cc32011-10-03 21:09:35 -0700488void Parcel::restoreAllowFds(bool lastValue)
489{
490 mAllowFds = lastValue;
491}
492
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700493bool Parcel::hasFileDescriptors() const
494{
495 if (!mFdsKnown) {
496 scanForFds();
497 }
498 return mHasFds;
499}
500
Brad Fitzpatrick702ea9d2010-06-18 13:07:53 -0700501// Write RPC headers. (previously just the interface token)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700502status_t Parcel::writeInterfaceToken(const String16& interface)
503{
Brad Fitzpatricka877cd82010-07-07 16:06:39 -0700504 writeInt32(IPCThreadState::self()->getStrictModePolicy() |
505 STRICT_MODE_PENALTY_GATHER);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700506 // currently the interface identification token is just its name as a string
507 return writeString16(interface);
508}
509
Mathias Agopian83c04462009-05-22 19:00:22 -0700510bool Parcel::checkInterface(IBinder* binder) const
511{
Brad Fitzpatrick702ea9d2010-06-18 13:07:53 -0700512 return enforceInterface(binder->getInterfaceDescriptor());
Mathias Agopian83c04462009-05-22 19:00:22 -0700513}
514
Brad Fitzpatricka877cd82010-07-07 16:06:39 -0700515bool Parcel::enforceInterface(const String16& interface,
Brad Fitzpatrick70081a12010-07-27 09:49:11 -0700516 IPCThreadState* threadState) const
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700517{
Brad Fitzpatrick70081a12010-07-27 09:49:11 -0700518 int32_t strictPolicy = readInt32();
519 if (threadState == NULL) {
520 threadState = IPCThreadState::self();
Brad Fitzpatricka877cd82010-07-07 16:06:39 -0700521 }
Brad Fitzpatrick52736032010-08-30 16:01:16 -0700522 if ((threadState->getLastTransactionBinderFlags() &
523 IBinder::FLAG_ONEWAY) != 0) {
524 // For one-way calls, the callee is running entirely
525 // disconnected from the caller, so disable StrictMode entirely.
526 // Not only does disk/network usage not impact the caller, but
527 // there's no way to commuicate back any violations anyway.
528 threadState->setStrictModePolicy(0);
529 } else {
530 threadState->setStrictModePolicy(strictPolicy);
531 }
Mathias Agopian83c04462009-05-22 19:00:22 -0700532 const String16 str(readString16());
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700533 if (str == interface) {
534 return true;
535 } else {
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700536 ALOGW("**** enforceInterface() expected '%s' but read '%s'",
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700537 String8(interface).string(), String8(str).string());
538 return false;
539 }
Brad Fitzpatrick702ea9d2010-06-18 13:07:53 -0700540}
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700541
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800542const binder_size_t* Parcel::objects() const
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700543{
544 return mObjects;
545}
546
547size_t Parcel::objectsCount() const
548{
549 return mObjectsSize;
550}
551
552status_t Parcel::errorCheck() const
553{
554 return mError;
555}
556
557void Parcel::setError(status_t err)
558{
559 mError = err;
560}
561
562status_t Parcel::finishWrite(size_t len)
563{
564 //printf("Finish write of %d\n", len);
565 mDataPos += len;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700566 ALOGV("finishWrite Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700567 if (mDataPos > mDataSize) {
568 mDataSize = mDataPos;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700569 ALOGV("finishWrite Setting data size of %p to %zu", this, mDataSize);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700570 }
571 //printf("New pos=%d, size=%d\n", mDataPos, mDataSize);
572 return NO_ERROR;
573}
574
575status_t Parcel::writeUnpadded(const void* data, size_t len)
576{
577 size_t end = mDataPos + len;
578 if (end < mDataPos) {
579 // integer overflow
580 return BAD_VALUE;
581 }
582
583 if (end <= mDataCapacity) {
584restart_write:
585 memcpy(mData+mDataPos, data, len);
586 return finishWrite(len);
587 }
588
589 status_t err = growData(len);
590 if (err == NO_ERROR) goto restart_write;
591 return err;
592}
593
594status_t Parcel::write(const void* data, size_t len)
595{
596 void* const d = writeInplace(len);
597 if (d) {
598 memcpy(d, data, len);
599 return NO_ERROR;
600 }
601 return mError;
602}
603
604void* Parcel::writeInplace(size_t len)
605{
606 const size_t padded = PAD_SIZE(len);
607
608 // sanity check for integer overflow
609 if (mDataPos+padded < mDataPos) {
610 return NULL;
611 }
612
613 if ((mDataPos+padded) <= mDataCapacity) {
614restart_write:
615 //printf("Writing %ld bytes, padded to %ld\n", len, padded);
616 uint8_t* const data = mData+mDataPos;
617
618 // Need to pad at end?
619 if (padded != len) {
620#if BYTE_ORDER == BIG_ENDIAN
621 static const uint32_t mask[4] = {
622 0x00000000, 0xffffff00, 0xffff0000, 0xff000000
623 };
624#endif
625#if BYTE_ORDER == LITTLE_ENDIAN
626 static const uint32_t mask[4] = {
627 0x00000000, 0x00ffffff, 0x0000ffff, 0x000000ff
628 };
629#endif
630 //printf("Applying pad mask: %p to %p\n", (void*)mask[padded-len],
631 // *reinterpret_cast<void**>(data+padded-4));
632 *reinterpret_cast<uint32_t*>(data+padded-4) &= mask[padded-len];
633 }
634
635 finishWrite(padded);
636 return data;
637 }
638
639 status_t err = growData(padded);
640 if (err == NO_ERROR) goto restart_write;
641 return NULL;
642}
643
644status_t Parcel::writeInt32(int32_t val)
645{
Andreas Huber84a6d042009-08-17 13:33:27 -0700646 return writeAligned(val);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700647}
Marco Nelissen5c0106e2013-10-16 10:57:51 -0700648status_t Parcel::writeInt32Array(size_t len, const int32_t *val) {
649 if (!val) {
650 return writeAligned(-1);
651 }
652 status_t ret = writeAligned(len);
653 if (ret == NO_ERROR) {
654 ret = write(val, len * sizeof(*val));
655 }
656 return ret;
657}
Marco Nelissenf0190bf2014-03-13 14:17:40 -0700658status_t Parcel::writeByteArray(size_t len, const uint8_t *val) {
659 if (!val) {
660 return writeAligned(-1);
661 }
662 status_t ret = writeAligned(len);
663 if (ret == NO_ERROR) {
664 ret = write(val, len * sizeof(*val));
665 }
666 return ret;
667}
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700668
669status_t Parcel::writeInt64(int64_t val)
670{
Andreas Huber84a6d042009-08-17 13:33:27 -0700671 return writeAligned(val);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700672}
673
Serban Constantinescuf683e012013-11-05 16:53:55 +0000674status_t Parcel::writePointer(uintptr_t val)
675{
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800676 return writeAligned<binder_uintptr_t>(val);
Serban Constantinescuf683e012013-11-05 16:53:55 +0000677}
678
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700679status_t Parcel::writeFloat(float val)
680{
Andreas Huber84a6d042009-08-17 13:33:27 -0700681 return writeAligned(val);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700682}
683
Douglas Leungcc1a4bb2013-01-11 15:00:55 -0800684#if defined(__mips__) && defined(__mips_hard_float)
685
686status_t Parcel::writeDouble(double val)
687{
688 union {
689 double d;
690 unsigned long long ll;
691 } u;
692 u.d = val;
693 return writeAligned(u.ll);
694}
695
696#else
697
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700698status_t Parcel::writeDouble(double val)
699{
Andreas Huber84a6d042009-08-17 13:33:27 -0700700 return writeAligned(val);
701}
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700702
Douglas Leungcc1a4bb2013-01-11 15:00:55 -0800703#endif
704
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700705status_t Parcel::writeCString(const char* str)
706{
707 return write(str, strlen(str)+1);
708}
709
710status_t Parcel::writeString8(const String8& str)
711{
712 status_t err = writeInt32(str.bytes());
Pravat Dalbeherad1dff8d2010-12-15 08:40:00 +0100713 // only write string if its length is more than zero characters,
714 // as readString8 will only read if the length field is non-zero.
715 // this is slightly different from how writeString16 works.
716 if (str.bytes() > 0 && err == NO_ERROR) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700717 err = write(str.string(), str.bytes()+1);
718 }
719 return err;
720}
721
722status_t Parcel::writeString16(const String16& str)
723{
724 return writeString16(str.string(), str.size());
725}
726
727status_t Parcel::writeString16(const char16_t* str, size_t len)
728{
729 if (str == NULL) return writeInt32(-1);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700730
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700731 status_t err = writeInt32(len);
732 if (err == NO_ERROR) {
733 len *= sizeof(char16_t);
734 uint8_t* data = (uint8_t*)writeInplace(len+sizeof(char16_t));
735 if (data) {
736 memcpy(data, str, len);
737 *reinterpret_cast<char16_t*>(data+len) = 0;
738 return NO_ERROR;
739 }
740 err = mError;
741 }
742 return err;
743}
744
745status_t Parcel::writeStrongBinder(const sp<IBinder>& val)
746{
747 return flatten_binder(ProcessState::self(), val, this);
748}
749
750status_t Parcel::writeWeakBinder(const wp<IBinder>& val)
751{
752 return flatten_binder(ProcessState::self(), val, this);
753}
754
Mathias Agopiana47f02a2009-05-21 16:29:38 -0700755status_t Parcel::writeNativeHandle(const native_handle* handle)
The Android Open Source Project5f78a482009-01-20 14:03:58 -0800756{
Mathias Agopian1d0a95b2009-07-31 16:12:13 -0700757 if (!handle || handle->version != sizeof(native_handle))
The Android Open Source Project5f78a482009-01-20 14:03:58 -0800758 return BAD_TYPE;
759
760 status_t err;
Mathias Agopiana47f02a2009-05-21 16:29:38 -0700761 err = writeInt32(handle->numFds);
The Android Open Source Project5f78a482009-01-20 14:03:58 -0800762 if (err != NO_ERROR) return err;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800763
Mathias Agopiana47f02a2009-05-21 16:29:38 -0700764 err = writeInt32(handle->numInts);
The Android Open Source Project5f78a482009-01-20 14:03:58 -0800765 if (err != NO_ERROR) return err;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800766
Mathias Agopiana47f02a2009-05-21 16:29:38 -0700767 for (int i=0 ; err==NO_ERROR && i<handle->numFds ; i++)
768 err = writeDupFileDescriptor(handle->data[i]);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800769
770 if (err != NO_ERROR) {
Steve Block9d453682011-12-20 16:23:08 +0000771 ALOGD("write native handle, write dup fd failed");
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800772 return err;
773 }
Mathias Agopiana47f02a2009-05-21 16:29:38 -0700774 err = write(handle->data + handle->numFds, sizeof(int)*handle->numInts);
The Android Open Source Project5f78a482009-01-20 14:03:58 -0800775 return err;
776}
777
Jeff Brown93ff1f92011-11-04 19:01:44 -0700778status_t Parcel::writeFileDescriptor(int fd, bool takeOwnership)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700779{
780 flat_binder_object obj;
781 obj.type = BINDER_TYPE_FD;
782 obj.flags = 0x7f | FLAT_BINDER_FLAG_ACCEPTS_FDS;
Arve Hjønnevåg07fd0f12014-02-18 21:10:29 -0800783 obj.binder = 0; /* Don't pass uninitialized stack data to a remote process */
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700784 obj.handle = fd;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800785 obj.cookie = takeOwnership ? 1 : 0;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700786 return writeObject(obj, true);
787}
788
789status_t Parcel::writeDupFileDescriptor(int fd)
790{
Jeff Brownd341c712011-11-04 20:19:33 -0700791 int dupFd = dup(fd);
792 if (dupFd < 0) {
793 return -errno;
794 }
795 status_t err = writeFileDescriptor(dupFd, true /*takeOwnership*/);
796 if (err) {
797 close(dupFd);
798 }
799 return err;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700800}
801
Mike Lockwoodcbe36fe2013-09-05 08:41:06 -0700802// WARNING: This method must stay in sync with
803// Parcelable.Creator<ParcelFileDescriptor> CREATOR
804// in frameworks/base/core/java/android/os/ParcelFileDescriptor.java
805status_t Parcel::writeParcelFileDescriptor(int fd, int commChannel) {
806 status_t status;
807
808 if (fd < 0) {
809 status = writeInt32(0); // ParcelFileDescriptor is null
810 if (status) return status;
811 } else {
812 status = writeInt32(1); // ParcelFileDescriptor is not null
813 if (status) return status;
814 status = writeDupFileDescriptor(fd);
815 if (status) return status;
816 if (commChannel < 0) {
817 status = writeInt32(0); // commChannel is null
818 if (status) return status;
819 } else {
820 status = writeInt32(1); // commChannel is not null
821 if (status) return status;
822 status = writeDupFileDescriptor(commChannel);
823 }
824 }
825 return status;
826}
827
Jeff Brown5707dbf2011-09-23 21:17:56 -0700828status_t Parcel::writeBlob(size_t len, WritableBlob* outBlob)
829{
830 status_t status;
831
832 if (!mAllowFds || len <= IN_PLACE_BLOB_LIMIT) {
Steve Block6807e592011-10-20 11:56:00 +0100833 ALOGV("writeBlob: write in place");
Jeff Brown5707dbf2011-09-23 21:17:56 -0700834 status = writeInt32(0);
835 if (status) return status;
836
837 void* ptr = writeInplace(len);
838 if (!ptr) return NO_MEMORY;
839
840 outBlob->init(false /*mapped*/, ptr, len);
841 return NO_ERROR;
842 }
843
Steve Block6807e592011-10-20 11:56:00 +0100844 ALOGV("writeBlob: write to ashmem");
Jeff Brown5707dbf2011-09-23 21:17:56 -0700845 int fd = ashmem_create_region("Parcel Blob", len);
846 if (fd < 0) return NO_MEMORY;
847
848 int result = ashmem_set_prot_region(fd, PROT_READ | PROT_WRITE);
849 if (result < 0) {
Jeff Brownec4e0062011-10-10 14:50:10 -0700850 status = result;
Jeff Brown5707dbf2011-09-23 21:17:56 -0700851 } else {
852 void* ptr = ::mmap(NULL, len, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
853 if (ptr == MAP_FAILED) {
854 status = -errno;
855 } else {
856 result = ashmem_set_prot_region(fd, PROT_READ);
857 if (result < 0) {
Jeff Brownec4e0062011-10-10 14:50:10 -0700858 status = result;
Jeff Brown5707dbf2011-09-23 21:17:56 -0700859 } else {
860 status = writeInt32(1);
861 if (!status) {
Jeff Brown93ff1f92011-11-04 19:01:44 -0700862 status = writeFileDescriptor(fd, true /*takeOwnership*/);
Jeff Brown5707dbf2011-09-23 21:17:56 -0700863 if (!status) {
864 outBlob->init(true /*mapped*/, ptr, len);
865 return NO_ERROR;
866 }
867 }
868 }
869 }
870 ::munmap(ptr, len);
871 }
872 ::close(fd);
873 return status;
874}
875
Mathias Agopiane1424282013-07-29 21:24:40 -0700876status_t Parcel::write(const FlattenableHelperInterface& val)
Mathias Agopian98e71dd2010-02-11 17:30:52 -0800877{
878 status_t err;
879
880 // size if needed
Mathias Agopiane1424282013-07-29 21:24:40 -0700881 const size_t len = val.getFlattenedSize();
882 const size_t fd_count = val.getFdCount();
Mathias Agopian98e71dd2010-02-11 17:30:52 -0800883
884 err = this->writeInt32(len);
885 if (err) return err;
886
887 err = this->writeInt32(fd_count);
888 if (err) return err;
889
890 // payload
Mathias Agopiane1424282013-07-29 21:24:40 -0700891 void* const buf = this->writeInplace(PAD_SIZE(len));
Mathias Agopian98e71dd2010-02-11 17:30:52 -0800892 if (buf == NULL)
893 return BAD_VALUE;
894
895 int* fds = NULL;
896 if (fd_count) {
897 fds = new int[fd_count];
898 }
899
900 err = val.flatten(buf, len, fds, fd_count);
901 for (size_t i=0 ; i<fd_count && err==NO_ERROR ; i++) {
902 err = this->writeDupFileDescriptor( fds[i] );
903 }
904
905 if (fd_count) {
906 delete [] fds;
907 }
908
909 return err;
910}
911
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700912status_t Parcel::writeObject(const flat_binder_object& val, bool nullMetaData)
913{
914 const bool enoughData = (mDataPos+sizeof(val)) <= mDataCapacity;
915 const bool enoughObjects = mObjectsSize < mObjectsCapacity;
916 if (enoughData && enoughObjects) {
917restart_write:
918 *reinterpret_cast<flat_binder_object*>(mData+mDataPos) = val;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700919
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700920 // Need to write meta-data?
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800921 if (nullMetaData || val.binder != 0) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700922 mObjects[mObjectsSize] = mDataPos;
923 acquire_object(ProcessState::self(), val, this);
924 mObjectsSize++;
925 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700926
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700927 // remember if it's a file descriptor
928 if (val.type == BINDER_TYPE_FD) {
Dianne Hackborn8938ed22011-09-28 23:19:47 -0400929 if (!mAllowFds) {
930 return FDS_NOT_ALLOWED;
931 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700932 mHasFds = mFdsKnown = true;
933 }
934
935 return finishWrite(sizeof(flat_binder_object));
936 }
937
938 if (!enoughData) {
939 const status_t err = growData(sizeof(val));
940 if (err != NO_ERROR) return err;
941 }
942 if (!enoughObjects) {
943 size_t newSize = ((mObjectsSize+2)*3)/2;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800944 binder_size_t* objects = (binder_size_t*)realloc(mObjects, newSize*sizeof(binder_size_t));
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700945 if (objects == NULL) return NO_MEMORY;
946 mObjects = objects;
947 mObjectsCapacity = newSize;
948 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700949
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700950 goto restart_write;
951}
952
Brad Fitzpatrick837a0d02010-07-13 15:33:35 -0700953status_t Parcel::writeNoException()
954{
955 return writeInt32(0);
956}
957
Colin Cross6f4f3ab2014-02-05 17:42:44 -0800958void Parcel::remove(size_t /*start*/, size_t /*amt*/)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700959{
960 LOG_ALWAYS_FATAL("Parcel::remove() not yet implemented!");
961}
962
963status_t Parcel::read(void* outData, size_t len) const
964{
Kenny Root5b61ad22014-03-17 13:18:16 -0700965 if ((mDataPos+PAD_SIZE(len)) >= mDataPos && (mDataPos+PAD_SIZE(len)) <= mDataSize
966 && len <= PAD_SIZE(len)) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700967 memcpy(outData, mData+mDataPos, len);
968 mDataPos += PAD_SIZE(len);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700969 ALOGV("read Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700970 return NO_ERROR;
971 }
972 return NOT_ENOUGH_DATA;
973}
974
975const void* Parcel::readInplace(size_t len) const
976{
Kenny Root5b61ad22014-03-17 13:18:16 -0700977 if ((mDataPos+PAD_SIZE(len)) >= mDataPos && (mDataPos+PAD_SIZE(len)) <= mDataSize
978 && len <= PAD_SIZE(len)) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700979 const void* data = mData+mDataPos;
980 mDataPos += PAD_SIZE(len);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700981 ALOGV("readInplace Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700982 return data;
983 }
984 return NULL;
985}
986
Andreas Huber84a6d042009-08-17 13:33:27 -0700987template<class T>
988status_t Parcel::readAligned(T *pArg) const {
989 COMPILE_TIME_ASSERT_FUNCTION_SCOPE(PAD_SIZE(sizeof(T)) == sizeof(T));
990
991 if ((mDataPos+sizeof(T)) <= mDataSize) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700992 const void* data = mData+mDataPos;
Andreas Huber84a6d042009-08-17 13:33:27 -0700993 mDataPos += sizeof(T);
994 *pArg = *reinterpret_cast<const T*>(data);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700995 return NO_ERROR;
996 } else {
997 return NOT_ENOUGH_DATA;
998 }
999}
1000
Andreas Huber84a6d042009-08-17 13:33:27 -07001001template<class T>
1002T Parcel::readAligned() const {
1003 T result;
1004 if (readAligned(&result) != NO_ERROR) {
1005 result = 0;
1006 }
1007
1008 return result;
1009}
1010
1011template<class T>
1012status_t Parcel::writeAligned(T val) {
1013 COMPILE_TIME_ASSERT_FUNCTION_SCOPE(PAD_SIZE(sizeof(T)) == sizeof(T));
1014
1015 if ((mDataPos+sizeof(val)) <= mDataCapacity) {
1016restart_write:
1017 *reinterpret_cast<T*>(mData+mDataPos) = val;
1018 return finishWrite(sizeof(val));
1019 }
1020
1021 status_t err = growData(sizeof(val));
1022 if (err == NO_ERROR) goto restart_write;
1023 return err;
1024}
1025
1026status_t Parcel::readInt32(int32_t *pArg) const
1027{
1028 return readAligned(pArg);
1029}
1030
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001031int32_t Parcel::readInt32() const
1032{
Andreas Huber84a6d042009-08-17 13:33:27 -07001033 return readAligned<int32_t>();
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001034}
1035
1036
1037status_t Parcel::readInt64(int64_t *pArg) const
1038{
Andreas Huber84a6d042009-08-17 13:33:27 -07001039 return readAligned(pArg);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001040}
1041
1042
1043int64_t Parcel::readInt64() const
1044{
Andreas Huber84a6d042009-08-17 13:33:27 -07001045 return readAligned<int64_t>();
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001046}
1047
Serban Constantinescuf683e012013-11-05 16:53:55 +00001048status_t Parcel::readPointer(uintptr_t *pArg) const
1049{
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001050 status_t ret;
1051 binder_uintptr_t ptr;
1052 ret = readAligned(&ptr);
1053 if (!ret)
1054 *pArg = ptr;
1055 return ret;
Serban Constantinescuf683e012013-11-05 16:53:55 +00001056}
1057
1058uintptr_t Parcel::readPointer() const
1059{
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001060 return readAligned<binder_uintptr_t>();
Serban Constantinescuf683e012013-11-05 16:53:55 +00001061}
1062
1063
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001064status_t Parcel::readFloat(float *pArg) const
1065{
Andreas Huber84a6d042009-08-17 13:33:27 -07001066 return readAligned(pArg);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001067}
1068
1069
1070float Parcel::readFloat() const
1071{
Andreas Huber84a6d042009-08-17 13:33:27 -07001072 return readAligned<float>();
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001073}
1074
Douglas Leungcc1a4bb2013-01-11 15:00:55 -08001075#if defined(__mips__) && defined(__mips_hard_float)
1076
1077status_t Parcel::readDouble(double *pArg) const
1078{
1079 union {
1080 double d;
1081 unsigned long long ll;
1082 } u;
Narayan Kamath2c68d382014-06-04 15:04:29 +01001083 u.d = 0;
Douglas Leungcc1a4bb2013-01-11 15:00:55 -08001084 status_t status;
1085 status = readAligned(&u.ll);
1086 *pArg = u.d;
1087 return status;
1088}
1089
1090double Parcel::readDouble() const
1091{
1092 union {
1093 double d;
1094 unsigned long long ll;
1095 } u;
1096 u.ll = readAligned<unsigned long long>();
1097 return u.d;
1098}
1099
1100#else
1101
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001102status_t Parcel::readDouble(double *pArg) const
1103{
Andreas Huber84a6d042009-08-17 13:33:27 -07001104 return readAligned(pArg);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001105}
1106
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001107double Parcel::readDouble() const
1108{
Andreas Huber84a6d042009-08-17 13:33:27 -07001109 return readAligned<double>();
1110}
1111
Douglas Leungcc1a4bb2013-01-11 15:00:55 -08001112#endif
1113
Andreas Huber84a6d042009-08-17 13:33:27 -07001114status_t Parcel::readIntPtr(intptr_t *pArg) const
1115{
1116 return readAligned(pArg);
1117}
1118
1119
1120intptr_t Parcel::readIntPtr() const
1121{
1122 return readAligned<intptr_t>();
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001123}
1124
1125
1126const char* Parcel::readCString() const
1127{
1128 const size_t avail = mDataSize-mDataPos;
1129 if (avail > 0) {
1130 const char* str = reinterpret_cast<const char*>(mData+mDataPos);
1131 // is the string's trailing NUL within the parcel's valid bounds?
1132 const char* eos = reinterpret_cast<const char*>(memchr(str, 0, avail));
1133 if (eos) {
1134 const size_t len = eos - str;
1135 mDataPos += PAD_SIZE(len+1);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001136 ALOGV("readCString Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001137 return str;
1138 }
1139 }
1140 return NULL;
1141}
1142
1143String8 Parcel::readString8() const
1144{
1145 int32_t size = readInt32();
1146 // watch for potential int overflow adding 1 for trailing NUL
1147 if (size > 0 && size < INT32_MAX) {
1148 const char* str = (const char*)readInplace(size+1);
1149 if (str) return String8(str, size);
1150 }
1151 return String8();
1152}
1153
1154String16 Parcel::readString16() const
1155{
1156 size_t len;
1157 const char16_t* str = readString16Inplace(&len);
1158 if (str) return String16(str, len);
Steve Blocke6f43dd2012-01-06 19:20:56 +00001159 ALOGE("Reading a NULL string not supported here.");
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001160 return String16();
1161}
1162
1163const char16_t* Parcel::readString16Inplace(size_t* outLen) const
1164{
1165 int32_t size = readInt32();
1166 // watch for potential int overflow from size+1
1167 if (size >= 0 && size < INT32_MAX) {
1168 *outLen = size;
1169 const char16_t* str = (const char16_t*)readInplace((size+1)*sizeof(char16_t));
1170 if (str != NULL) {
1171 return str;
1172 }
1173 }
1174 *outLen = 0;
1175 return NULL;
1176}
1177
1178sp<IBinder> Parcel::readStrongBinder() const
1179{
1180 sp<IBinder> val;
1181 unflatten_binder(ProcessState::self(), *this, &val);
1182 return val;
1183}
1184
1185wp<IBinder> Parcel::readWeakBinder() const
1186{
1187 wp<IBinder> val;
1188 unflatten_binder(ProcessState::self(), *this, &val);
1189 return val;
1190}
1191
Brad Fitzpatrick837a0d02010-07-13 15:33:35 -07001192int32_t Parcel::readExceptionCode() const
1193{
1194 int32_t exception_code = readAligned<int32_t>();
Brad Fitzpatrickd36f4a52010-07-12 11:05:38 -07001195 if (exception_code == EX_HAS_REPLY_HEADER) {
Magnus Strandberg1ba24572011-05-03 15:44:00 +02001196 int32_t header_start = dataPosition();
Brad Fitzpatrickd36f4a52010-07-12 11:05:38 -07001197 int32_t header_size = readAligned<int32_t>();
1198 // Skip over fat responses headers. Not used (or propagated) in
1199 // native code
Magnus Strandberg1ba24572011-05-03 15:44:00 +02001200 setDataPosition(header_start + header_size);
Brad Fitzpatrickd36f4a52010-07-12 11:05:38 -07001201 // And fat response headers are currently only used when there are no
1202 // exceptions, so return no error:
1203 return 0;
1204 }
Brad Fitzpatrick837a0d02010-07-13 15:33:35 -07001205 return exception_code;
1206}
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001207
Mathias Agopiana47f02a2009-05-21 16:29:38 -07001208native_handle* Parcel::readNativeHandle() const
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001209{
1210 int numFds, numInts;
1211 status_t err;
1212 err = readInt32(&numFds);
1213 if (err != NO_ERROR) return 0;
1214 err = readInt32(&numInts);
1215 if (err != NO_ERROR) return 0;
1216
Mathias Agopiana47f02a2009-05-21 16:29:38 -07001217 native_handle* h = native_handle_create(numFds, numInts);
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001218 for (int i=0 ; err==NO_ERROR && i<numFds ; i++) {
Rebecca Schultz Zavin360211f2009-02-13 16:34:38 -08001219 h->data[i] = dup(readFileDescriptor());
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001220 if (h->data[i] < 0) err = BAD_VALUE;
1221 }
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001222 err = read(h->data + numFds, sizeof(int)*numInts);
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001223 if (err != NO_ERROR) {
Mathias Agopiana47f02a2009-05-21 16:29:38 -07001224 native_handle_close(h);
1225 native_handle_delete(h);
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001226 h = 0;
1227 }
1228 return h;
1229}
1230
1231
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001232int Parcel::readFileDescriptor() const
1233{
1234 const flat_binder_object* flat = readObject(true);
1235 if (flat) {
1236 switch (flat->type) {
1237 case BINDER_TYPE_FD:
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001238 //ALOGI("Returning file descriptor %ld from parcel %p", flat->handle, this);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001239 return flat->handle;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001240 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001241 }
1242 return BAD_TYPE;
1243}
1244
Mike Lockwoodcbe36fe2013-09-05 08:41:06 -07001245// WARNING: This method must stay in sync with writeToParcel()
1246// in frameworks/base/core/java/android/os/ParcelFileDescriptor.java
1247int Parcel::readParcelFileDescriptor(int& outCommChannel) const {
1248 int fd;
1249 outCommChannel = -1;
1250
1251 if (readInt32() == 0) {
1252 fd = -1;
1253 } else {
1254 fd = readFileDescriptor();
1255 if (fd >= 0 && readInt32() != 0) {
1256 outCommChannel = readFileDescriptor();
1257 }
1258 }
1259 return fd;
1260}
1261
Jeff Brown5707dbf2011-09-23 21:17:56 -07001262status_t Parcel::readBlob(size_t len, ReadableBlob* outBlob) const
1263{
1264 int32_t useAshmem;
1265 status_t status = readInt32(&useAshmem);
1266 if (status) return status;
1267
1268 if (!useAshmem) {
Steve Block6807e592011-10-20 11:56:00 +01001269 ALOGV("readBlob: read in place");
Jeff Brown5707dbf2011-09-23 21:17:56 -07001270 const void* ptr = readInplace(len);
1271 if (!ptr) return BAD_VALUE;
1272
1273 outBlob->init(false /*mapped*/, const_cast<void*>(ptr), len);
1274 return NO_ERROR;
1275 }
1276
Steve Block6807e592011-10-20 11:56:00 +01001277 ALOGV("readBlob: read from ashmem");
Jeff Brown5707dbf2011-09-23 21:17:56 -07001278 int fd = readFileDescriptor();
1279 if (fd == int(BAD_TYPE)) return BAD_VALUE;
1280
1281 void* ptr = ::mmap(NULL, len, PROT_READ, MAP_SHARED, fd, 0);
Narayan Kamath9ea09752014-10-08 17:35:45 +01001282 if (ptr == MAP_FAILED) return NO_MEMORY;
Jeff Brown5707dbf2011-09-23 21:17:56 -07001283
1284 outBlob->init(true /*mapped*/, ptr, len);
1285 return NO_ERROR;
1286}
1287
Mathias Agopiane1424282013-07-29 21:24:40 -07001288status_t Parcel::read(FlattenableHelperInterface& val) const
Mathias Agopian98e71dd2010-02-11 17:30:52 -08001289{
1290 // size
1291 const size_t len = this->readInt32();
1292 const size_t fd_count = this->readInt32();
1293
1294 // payload
Mathias Agopiane1424282013-07-29 21:24:40 -07001295 void const* const buf = this->readInplace(PAD_SIZE(len));
Mathias Agopian98e71dd2010-02-11 17:30:52 -08001296 if (buf == NULL)
1297 return BAD_VALUE;
1298
1299 int* fds = NULL;
1300 if (fd_count) {
1301 fds = new int[fd_count];
1302 }
1303
1304 status_t err = NO_ERROR;
1305 for (size_t i=0 ; i<fd_count && err==NO_ERROR ; i++) {
Jesse Hallfee99042014-11-04 08:36:31 -08001306 fds[i] = dup(this->readFileDescriptor());
Jun Jiangabf8a2c2014-04-29 14:22:10 +08001307 if (fds[i] < 0) {
1308 err = BAD_VALUE;
Jesse Hallfee99042014-11-04 08:36:31 -08001309 ALOGE("dup() failed in Parcel::read, i is %zu, fds[i] is %d, fd_count is %zu, error: %s",
1310 i, fds[i], fd_count, strerror(errno));
Jun Jiangabf8a2c2014-04-29 14:22:10 +08001311 }
Mathias Agopian98e71dd2010-02-11 17:30:52 -08001312 }
1313
1314 if (err == NO_ERROR) {
1315 err = val.unflatten(buf, len, fds, fd_count);
1316 }
1317
1318 if (fd_count) {
1319 delete [] fds;
1320 }
1321
1322 return err;
1323}
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001324const flat_binder_object* Parcel::readObject(bool nullMetaData) const
1325{
1326 const size_t DPOS = mDataPos;
1327 if ((DPOS+sizeof(flat_binder_object)) <= mDataSize) {
1328 const flat_binder_object* obj
1329 = reinterpret_cast<const flat_binder_object*>(mData+DPOS);
1330 mDataPos = DPOS + sizeof(flat_binder_object);
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001331 if (!nullMetaData && (obj->cookie == 0 && obj->binder == 0)) {
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001332 // When transferring a NULL object, we don't write it into
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001333 // the object list, so we don't want to check for it when
1334 // reading.
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001335 ALOGV("readObject Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001336 return obj;
1337 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001338
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001339 // Ensure that this object is valid...
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001340 binder_size_t* const OBJS = mObjects;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001341 const size_t N = mObjectsSize;
1342 size_t opos = mNextObjectHint;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001343
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001344 if (N > 0) {
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001345 ALOGV("Parcel %p looking for obj at %zu, hint=%zu",
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001346 this, DPOS, opos);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001347
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001348 // Start at the current hint position, looking for an object at
1349 // the current data position.
1350 if (opos < N) {
1351 while (opos < (N-1) && OBJS[opos] < DPOS) {
1352 opos++;
1353 }
1354 } else {
1355 opos = N-1;
1356 }
1357 if (OBJS[opos] == DPOS) {
1358 // Found it!
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001359 ALOGV("Parcel %p found obj %zu at index %zu with forward search",
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001360 this, DPOS, opos);
1361 mNextObjectHint = opos+1;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001362 ALOGV("readObject Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001363 return obj;
1364 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001365
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001366 // Look backwards for it...
1367 while (opos > 0 && OBJS[opos] > DPOS) {
1368 opos--;
1369 }
1370 if (OBJS[opos] == DPOS) {
1371 // Found it!
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001372 ALOGV("Parcel %p found obj %zu at index %zu with backward search",
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001373 this, DPOS, opos);
1374 mNextObjectHint = opos+1;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001375 ALOGV("readObject Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001376 return obj;
1377 }
1378 }
Colin Cross6f4f3ab2014-02-05 17:42:44 -08001379 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 -07001380 this, DPOS);
1381 }
1382 return NULL;
1383}
1384
1385void Parcel::closeFileDescriptors()
1386{
1387 size_t i = mObjectsSize;
1388 if (i > 0) {
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001389 //ALOGI("Closing file descriptors for %zu objects...", i);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001390 }
1391 while (i > 0) {
1392 i--;
1393 const flat_binder_object* flat
1394 = reinterpret_cast<flat_binder_object*>(mData+mObjects[i]);
1395 if (flat->type == BINDER_TYPE_FD) {
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001396 //ALOGI("Closing fd: %ld", flat->handle);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001397 close(flat->handle);
1398 }
1399 }
1400}
1401
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001402uintptr_t Parcel::ipcData() const
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001403{
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001404 return reinterpret_cast<uintptr_t>(mData);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001405}
1406
1407size_t Parcel::ipcDataSize() const
1408{
1409 return (mDataSize > mDataPos ? mDataSize : mDataPos);
1410}
1411
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001412uintptr_t Parcel::ipcObjects() const
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001413{
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001414 return reinterpret_cast<uintptr_t>(mObjects);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001415}
1416
1417size_t Parcel::ipcObjectsCount() const
1418{
1419 return mObjectsSize;
1420}
1421
1422void Parcel::ipcSetDataReference(const uint8_t* data, size_t dataSize,
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001423 const binder_size_t* objects, size_t objectsCount, release_func relFunc, void* relCookie)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001424{
Arve Hjønnevåg6f286112014-02-19 20:42:13 -08001425 binder_size_t minOffset = 0;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001426 freeDataNoInit();
1427 mError = NO_ERROR;
1428 mData = const_cast<uint8_t*>(data);
1429 mDataSize = mDataCapacity = dataSize;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001430 //ALOGI("setDataReference Setting data size of %p to %lu (pid=%d)", this, mDataSize, getpid());
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001431 mDataPos = 0;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001432 ALOGV("setDataReference Setting data pos of %p to %zu", this, mDataPos);
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001433 mObjects = const_cast<binder_size_t*>(objects);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001434 mObjectsSize = mObjectsCapacity = objectsCount;
1435 mNextObjectHint = 0;
1436 mOwner = relFunc;
1437 mOwnerCookie = relCookie;
Arve Hjønnevågf50b9ea2014-02-13 19:22:08 -08001438 for (size_t i = 0; i < mObjectsSize; i++) {
Arve Hjønnevåg6f286112014-02-19 20:42:13 -08001439 binder_size_t offset = mObjects[i];
Arve Hjønnevågf50b9ea2014-02-13 19:22:08 -08001440 if (offset < minOffset) {
Arve Hjønnevåg6f286112014-02-19 20:42:13 -08001441 ALOGE("%s: bad object offset %"PRIu64" < %"PRIu64"\n",
1442 __func__, (uint64_t)offset, (uint64_t)minOffset);
Arve Hjønnevågf50b9ea2014-02-13 19:22:08 -08001443 mObjectsSize = 0;
1444 break;
1445 }
1446 minOffset = offset + sizeof(flat_binder_object);
1447 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001448 scanForFds();
1449}
1450
Colin Cross6f4f3ab2014-02-05 17:42:44 -08001451void Parcel::print(TextOutput& to, uint32_t /*flags*/) const
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001452{
1453 to << "Parcel(";
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001454
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001455 if (errorCheck() != NO_ERROR) {
1456 const status_t err = errorCheck();
Colin Cross6f4f3ab2014-02-05 17:42:44 -08001457 to << "Error: " << (void*)(intptr_t)err << " \"" << strerror(-err) << "\"";
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001458 } else if (dataSize() > 0) {
1459 const uint8_t* DATA = data();
1460 to << indent << HexDump(DATA, dataSize()) << dedent;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001461 const binder_size_t* OBJS = objects();
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001462 const size_t N = objectsCount();
1463 for (size_t i=0; i<N; i++) {
1464 const flat_binder_object* flat
1465 = reinterpret_cast<const flat_binder_object*>(DATA+OBJS[i]);
1466 to << endl << "Object #" << i << " @ " << (void*)OBJS[i] << ": "
1467 << TypeCode(flat->type & 0x7f7f7f00)
1468 << " = " << flat->binder;
1469 }
1470 } else {
1471 to << "NULL";
1472 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001473
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001474 to << ")";
1475}
1476
1477void Parcel::releaseObjects()
1478{
1479 const sp<ProcessState> proc(ProcessState::self());
1480 size_t i = mObjectsSize;
1481 uint8_t* const data = mData;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001482 binder_size_t* const objects = mObjects;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001483 while (i > 0) {
1484 i--;
1485 const flat_binder_object* flat
1486 = reinterpret_cast<flat_binder_object*>(data+objects[i]);
1487 release_object(proc, *flat, this);
1488 }
1489}
1490
1491void Parcel::acquireObjects()
1492{
1493 const sp<ProcessState> proc(ProcessState::self());
1494 size_t i = mObjectsSize;
1495 uint8_t* const data = mData;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001496 binder_size_t* const objects = mObjects;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001497 while (i > 0) {
1498 i--;
1499 const flat_binder_object* flat
1500 = reinterpret_cast<flat_binder_object*>(data+objects[i]);
1501 acquire_object(proc, *flat, this);
1502 }
1503}
1504
1505void Parcel::freeData()
1506{
1507 freeDataNoInit();
1508 initState();
1509}
1510
1511void Parcel::freeDataNoInit()
1512{
1513 if (mOwner) {
Dianne Hackborn7e790af2014-11-11 12:22:53 -08001514 LOG_ALLOC("Parcel %p: freeing other owner data", this);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001515 //ALOGI("Freeing data ref of %p (pid=%d)", this, getpid());
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001516 mOwner(this, mData, mDataSize, mObjects, mObjectsSize, mOwnerCookie);
1517 } else {
Dianne Hackborn7e790af2014-11-11 12:22:53 -08001518 LOG_ALLOC("Parcel %p: freeing allocated data", this);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001519 releaseObjects();
Dianne Hackborn7e790af2014-11-11 12:22:53 -08001520 if (mData) {
1521 LOG_ALLOC("Parcel %p: freeing with %zu capacity", this, mDataCapacity);
Dianne Hackborna4cff882014-11-13 17:07:40 -08001522 pthread_mutex_lock(&gParcelGlobalAllocSizeLock);
Dianne Hackborn7e790af2014-11-11 12:22:53 -08001523 gParcelGlobalAllocSize -= mDataCapacity;
1524 gParcelGlobalAllocCount--;
Dianne Hackborna4cff882014-11-13 17:07:40 -08001525 pthread_mutex_unlock(&gParcelGlobalAllocSizeLock);
Dianne Hackborn7e790af2014-11-11 12:22:53 -08001526 free(mData);
1527 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001528 if (mObjects) free(mObjects);
1529 }
1530}
1531
1532status_t Parcel::growData(size_t len)
1533{
1534 size_t newSize = ((mDataSize+len)*3)/2;
1535 return (newSize <= mDataSize)
1536 ? (status_t) NO_MEMORY
1537 : continueWrite(newSize);
1538}
1539
1540status_t Parcel::restartWrite(size_t desired)
1541{
1542 if (mOwner) {
1543 freeData();
1544 return continueWrite(desired);
1545 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001546
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001547 uint8_t* data = (uint8_t*)realloc(mData, desired);
1548 if (!data && desired > mDataCapacity) {
1549 mError = NO_MEMORY;
1550 return NO_MEMORY;
1551 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001552
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001553 releaseObjects();
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001554
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001555 if (data) {
Dianne Hackborn7e790af2014-11-11 12:22:53 -08001556 LOG_ALLOC("Parcel %p: restart from %zu to %zu capacity", this, mDataCapacity, desired);
Dianne Hackborna4cff882014-11-13 17:07:40 -08001557 pthread_mutex_lock(&gParcelGlobalAllocSizeLock);
Dianne Hackborn7e790af2014-11-11 12:22:53 -08001558 gParcelGlobalAllocSize += desired;
1559 gParcelGlobalAllocSize -= mDataCapacity;
Dianne Hackborna4cff882014-11-13 17:07:40 -08001560 pthread_mutex_unlock(&gParcelGlobalAllocSizeLock);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001561 mData = data;
1562 mDataCapacity = desired;
1563 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001564
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001565 mDataSize = mDataPos = 0;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001566 ALOGV("restartWrite Setting data size of %p to %zu", this, mDataSize);
1567 ALOGV("restartWrite Setting data pos of %p to %zu", this, mDataPos);
1568
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001569 free(mObjects);
1570 mObjects = NULL;
1571 mObjectsSize = mObjectsCapacity = 0;
1572 mNextObjectHint = 0;
1573 mHasFds = false;
1574 mFdsKnown = true;
Dianne Hackborn8938ed22011-09-28 23:19:47 -04001575 mAllowFds = true;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001576
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001577 return NO_ERROR;
1578}
1579
1580status_t Parcel::continueWrite(size_t desired)
1581{
1582 // If shrinking, first adjust for any objects that appear
1583 // after the new data size.
1584 size_t objectsSize = mObjectsSize;
1585 if (desired < mDataSize) {
1586 if (desired == 0) {
1587 objectsSize = 0;
1588 } else {
1589 while (objectsSize > 0) {
1590 if (mObjects[objectsSize-1] < desired)
1591 break;
1592 objectsSize--;
1593 }
1594 }
1595 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001596
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001597 if (mOwner) {
1598 // If the size is going to zero, just release the owner's data.
1599 if (desired == 0) {
1600 freeData();
1601 return NO_ERROR;
1602 }
1603
1604 // If there is a different owner, we need to take
1605 // posession.
1606 uint8_t* data = (uint8_t*)malloc(desired);
1607 if (!data) {
1608 mError = NO_MEMORY;
1609 return NO_MEMORY;
1610 }
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001611 binder_size_t* objects = NULL;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001612
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001613 if (objectsSize) {
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001614 objects = (binder_size_t*)malloc(objectsSize*sizeof(binder_size_t));
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001615 if (!objects) {
Hyejin Kim3f727c02013-03-09 11:28:54 +09001616 free(data);
1617
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001618 mError = NO_MEMORY;
1619 return NO_MEMORY;
1620 }
1621
1622 // Little hack to only acquire references on objects
1623 // we will be keeping.
1624 size_t oldObjectsSize = mObjectsSize;
1625 mObjectsSize = objectsSize;
1626 acquireObjects();
1627 mObjectsSize = oldObjectsSize;
1628 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001629
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001630 if (mData) {
1631 memcpy(data, mData, mDataSize < desired ? mDataSize : desired);
1632 }
1633 if (objects && mObjects) {
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001634 memcpy(objects, mObjects, objectsSize*sizeof(binder_size_t));
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001635 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001636 //ALOGI("Freeing data ref of %p (pid=%d)", this, getpid());
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001637 mOwner(this, mData, mDataSize, mObjects, mObjectsSize, mOwnerCookie);
1638 mOwner = NULL;
1639
Dianne Hackborn7e790af2014-11-11 12:22:53 -08001640 LOG_ALLOC("Parcel %p: taking ownership of %zu capacity", this, desired);
Dianne Hackborna4cff882014-11-13 17:07:40 -08001641 pthread_mutex_lock(&gParcelGlobalAllocSizeLock);
Dianne Hackborn7e790af2014-11-11 12:22:53 -08001642 gParcelGlobalAllocSize += desired;
1643 gParcelGlobalAllocCount++;
Dianne Hackborna4cff882014-11-13 17:07:40 -08001644 pthread_mutex_unlock(&gParcelGlobalAllocSizeLock);
Dianne Hackborn7e790af2014-11-11 12:22:53 -08001645
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001646 mData = data;
1647 mObjects = objects;
1648 mDataSize = (mDataSize < desired) ? mDataSize : desired;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001649 ALOGV("continueWrite Setting data size of %p to %zu", this, mDataSize);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001650 mDataCapacity = desired;
1651 mObjectsSize = mObjectsCapacity = objectsSize;
1652 mNextObjectHint = 0;
1653
1654 } else if (mData) {
1655 if (objectsSize < mObjectsSize) {
1656 // Need to release refs on any objects we are dropping.
1657 const sp<ProcessState> proc(ProcessState::self());
1658 for (size_t i=objectsSize; i<mObjectsSize; i++) {
1659 const flat_binder_object* flat
1660 = reinterpret_cast<flat_binder_object*>(mData+mObjects[i]);
1661 if (flat->type == BINDER_TYPE_FD) {
1662 // will need to rescan because we may have lopped off the only FDs
1663 mFdsKnown = false;
1664 }
1665 release_object(proc, *flat, this);
1666 }
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001667 binder_size_t* objects =
1668 (binder_size_t*)realloc(mObjects, objectsSize*sizeof(binder_size_t));
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001669 if (objects) {
1670 mObjects = objects;
1671 }
1672 mObjectsSize = objectsSize;
1673 mNextObjectHint = 0;
1674 }
1675
1676 // We own the data, so we can just do a realloc().
1677 if (desired > mDataCapacity) {
1678 uint8_t* data = (uint8_t*)realloc(mData, desired);
1679 if (data) {
Dianne Hackborn7e790af2014-11-11 12:22:53 -08001680 LOG_ALLOC("Parcel %p: continue from %zu to %zu capacity", this, mDataCapacity,
1681 desired);
Dianne Hackborna4cff882014-11-13 17:07:40 -08001682 pthread_mutex_lock(&gParcelGlobalAllocSizeLock);
Dianne Hackborn7e790af2014-11-11 12:22:53 -08001683 gParcelGlobalAllocSize += desired;
1684 gParcelGlobalAllocSize -= mDataCapacity;
Dianne Hackborna4cff882014-11-13 17:07:40 -08001685 pthread_mutex_unlock(&gParcelGlobalAllocSizeLock);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001686 mData = data;
1687 mDataCapacity = desired;
1688 } else if (desired > mDataCapacity) {
1689 mError = NO_MEMORY;
1690 return NO_MEMORY;
1691 }
1692 } else {
Dianne Hackborn97e2bcd2011-04-13 18:15:56 -07001693 if (mDataSize > desired) {
1694 mDataSize = desired;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001695 ALOGV("continueWrite Setting data size of %p to %zu", this, mDataSize);
Dianne Hackborn97e2bcd2011-04-13 18:15:56 -07001696 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001697 if (mDataPos > desired) {
1698 mDataPos = desired;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001699 ALOGV("continueWrite Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001700 }
1701 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001702
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001703 } else {
1704 // This is the first data. Easy!
1705 uint8_t* data = (uint8_t*)malloc(desired);
1706 if (!data) {
1707 mError = NO_MEMORY;
1708 return NO_MEMORY;
1709 }
Hyejin Kim3f727c02013-03-09 11:28:54 +09001710
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001711 if(!(mDataCapacity == 0 && mObjects == NULL
1712 && mObjectsCapacity == 0)) {
Colin Cross6f4f3ab2014-02-05 17:42:44 -08001713 ALOGE("continueWrite: %zu/%p/%zu/%zu", mDataCapacity, mObjects, mObjectsCapacity, desired);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001714 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001715
Dianne Hackborn7e790af2014-11-11 12:22:53 -08001716 LOG_ALLOC("Parcel %p: allocating with %zu capacity", this, desired);
Dianne Hackborna4cff882014-11-13 17:07:40 -08001717 pthread_mutex_lock(&gParcelGlobalAllocSizeLock);
Dianne Hackborn7e790af2014-11-11 12:22:53 -08001718 gParcelGlobalAllocSize += desired;
1719 gParcelGlobalAllocCount++;
Dianne Hackborna4cff882014-11-13 17:07:40 -08001720 pthread_mutex_unlock(&gParcelGlobalAllocSizeLock);
Dianne Hackborn7e790af2014-11-11 12:22:53 -08001721
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001722 mData = data;
1723 mDataSize = mDataPos = 0;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001724 ALOGV("continueWrite Setting data size of %p to %zu", this, mDataSize);
1725 ALOGV("continueWrite Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001726 mDataCapacity = desired;
1727 }
1728
1729 return NO_ERROR;
1730}
1731
1732void Parcel::initState()
1733{
Dianne Hackborn7e790af2014-11-11 12:22:53 -08001734 LOG_ALLOC("Parcel %p: initState", this);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001735 mError = NO_ERROR;
1736 mData = 0;
1737 mDataSize = 0;
1738 mDataCapacity = 0;
1739 mDataPos = 0;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001740 ALOGV("initState Setting data size of %p to %zu", this, mDataSize);
1741 ALOGV("initState Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001742 mObjects = NULL;
1743 mObjectsSize = 0;
1744 mObjectsCapacity = 0;
1745 mNextObjectHint = 0;
1746 mHasFds = false;
1747 mFdsKnown = true;
Dianne Hackborn8938ed22011-09-28 23:19:47 -04001748 mAllowFds = true;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001749 mOwner = NULL;
1750}
1751
1752void Parcel::scanForFds() const
1753{
1754 bool hasFds = false;
1755 for (size_t i=0; i<mObjectsSize; i++) {
1756 const flat_binder_object* flat
1757 = reinterpret_cast<const flat_binder_object*>(mData + mObjects[i]);
1758 if (flat->type == BINDER_TYPE_FD) {
1759 hasFds = true;
1760 break;
1761 }
1762 }
1763 mHasFds = hasFds;
1764 mFdsKnown = true;
1765}
1766
Jeff Brown5707dbf2011-09-23 21:17:56 -07001767// --- Parcel::Blob ---
1768
1769Parcel::Blob::Blob() :
1770 mMapped(false), mData(NULL), mSize(0) {
1771}
1772
1773Parcel::Blob::~Blob() {
1774 release();
1775}
1776
1777void Parcel::Blob::release() {
1778 if (mMapped && mData) {
1779 ::munmap(mData, mSize);
1780 }
1781 clear();
1782}
1783
1784void Parcel::Blob::init(bool mapped, void* data, size_t size) {
1785 mMapped = mapped;
1786 mData = data;
1787 mSize = size;
1788}
1789
1790void Parcel::Blob::clear() {
1791 mMapped = false;
1792 mData = NULL;
1793 mSize = 0;
1794}
1795
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001796}; // namespace android