blob: 3100a581118b4716d21b72d9a1a51c40462a7f2c [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 Tate2f340be2015-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) {
Christopher Tate8b643072016-11-03 13:32:41 -0700441 size_t newSize = ((mObjectsSize + numObjects)*3)/2;
442 if (newSize*sizeof(binder_size_t) < mObjectsSize) return NO_MEMORY; // overflow
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800443 binder_size_t *objects =
444 (binder_size_t*)realloc(mObjects, newSize*sizeof(binder_size_t));
445 if (objects == (binder_size_t*)0) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700446 return NO_MEMORY;
447 }
448 mObjects = objects;
449 mObjectsCapacity = newSize;
450 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700451
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700452 // append and acquire objects
453 int idx = mObjectsSize;
454 for (int i = firstIndex; i <= lastIndex; i++) {
455 size_t off = objects[i] - offset + startPos;
456 mObjects[idx++] = off;
457 mObjectsSize++;
458
Dianne Hackborn8af0f822009-05-22 13:20:23 -0700459 flat_binder_object* flat
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700460 = reinterpret_cast<flat_binder_object*>(mData + off);
461 acquire_object(proc, *flat, this);
462
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700463 if (flat->type == BINDER_TYPE_FD) {
Dianne Hackborn8af0f822009-05-22 13:20:23 -0700464 // If this is a file descriptor, we need to dup it so the
465 // new Parcel now owns its own fd, and can declare that we
466 // officially know we have fds.
467 flat->handle = dup(flat->handle);
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800468 flat->cookie = 1;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700469 mHasFds = mFdsKnown = true;
Dianne Hackborn8938ed22011-09-28 23:19:47 -0400470 if (!mAllowFds) {
471 err = FDS_NOT_ALLOWED;
472 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700473 }
474 }
475 }
476
Dianne Hackborn8938ed22011-09-28 23:19:47 -0400477 return err;
478}
479
Dianne Hackborn7746cc32011-10-03 21:09:35 -0700480bool Parcel::pushAllowFds(bool allowFds)
Dianne Hackborn8938ed22011-09-28 23:19:47 -0400481{
482 const bool origValue = mAllowFds;
Dianne Hackborn7746cc32011-10-03 21:09:35 -0700483 if (!allowFds) {
484 mAllowFds = false;
485 }
Dianne Hackborn8938ed22011-09-28 23:19:47 -0400486 return origValue;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700487}
488
Dianne Hackborn7746cc32011-10-03 21:09:35 -0700489void Parcel::restoreAllowFds(bool lastValue)
490{
491 mAllowFds = lastValue;
492}
493
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700494bool Parcel::hasFileDescriptors() const
495{
496 if (!mFdsKnown) {
497 scanForFds();
498 }
499 return mHasFds;
500}
501
Brad Fitzpatrick702ea9d2010-06-18 13:07:53 -0700502// Write RPC headers. (previously just the interface token)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700503status_t Parcel::writeInterfaceToken(const String16& interface)
504{
Brad Fitzpatricka877cd82010-07-07 16:06:39 -0700505 writeInt32(IPCThreadState::self()->getStrictModePolicy() |
506 STRICT_MODE_PENALTY_GATHER);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700507 // currently the interface identification token is just its name as a string
508 return writeString16(interface);
509}
510
Mathias Agopian83c04462009-05-22 19:00:22 -0700511bool Parcel::checkInterface(IBinder* binder) const
512{
Brad Fitzpatrick702ea9d2010-06-18 13:07:53 -0700513 return enforceInterface(binder->getInterfaceDescriptor());
Mathias Agopian83c04462009-05-22 19:00:22 -0700514}
515
Brad Fitzpatricka877cd82010-07-07 16:06:39 -0700516bool Parcel::enforceInterface(const String16& interface,
Brad Fitzpatrick70081a12010-07-27 09:49:11 -0700517 IPCThreadState* threadState) const
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700518{
Brad Fitzpatrick70081a12010-07-27 09:49:11 -0700519 int32_t strictPolicy = readInt32();
520 if (threadState == NULL) {
521 threadState = IPCThreadState::self();
Brad Fitzpatricka877cd82010-07-07 16:06:39 -0700522 }
Brad Fitzpatrick52736032010-08-30 16:01:16 -0700523 if ((threadState->getLastTransactionBinderFlags() &
524 IBinder::FLAG_ONEWAY) != 0) {
525 // For one-way calls, the callee is running entirely
526 // disconnected from the caller, so disable StrictMode entirely.
527 // Not only does disk/network usage not impact the caller, but
528 // there's no way to commuicate back any violations anyway.
529 threadState->setStrictModePolicy(0);
530 } else {
531 threadState->setStrictModePolicy(strictPolicy);
532 }
Mathias Agopian83c04462009-05-22 19:00:22 -0700533 const String16 str(readString16());
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700534 if (str == interface) {
535 return true;
536 } else {
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700537 ALOGW("**** enforceInterface() expected '%s' but read '%s'",
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700538 String8(interface).string(), String8(str).string());
539 return false;
540 }
Brad Fitzpatrick702ea9d2010-06-18 13:07:53 -0700541}
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700542
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800543const binder_size_t* Parcel::objects() const
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700544{
545 return mObjects;
546}
547
548size_t Parcel::objectsCount() const
549{
550 return mObjectsSize;
551}
552
553status_t Parcel::errorCheck() const
554{
555 return mError;
556}
557
558void Parcel::setError(status_t err)
559{
560 mError = err;
561}
562
563status_t Parcel::finishWrite(size_t len)
564{
565 //printf("Finish write of %d\n", len);
566 mDataPos += len;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700567 ALOGV("finishWrite Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700568 if (mDataPos > mDataSize) {
569 mDataSize = mDataPos;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700570 ALOGV("finishWrite Setting data size of %p to %zu", this, mDataSize);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700571 }
572 //printf("New pos=%d, size=%d\n", mDataPos, mDataSize);
573 return NO_ERROR;
574}
575
576status_t Parcel::writeUnpadded(const void* data, size_t len)
577{
578 size_t end = mDataPos + len;
579 if (end < mDataPos) {
580 // integer overflow
581 return BAD_VALUE;
582 }
583
584 if (end <= mDataCapacity) {
585restart_write:
586 memcpy(mData+mDataPos, data, len);
587 return finishWrite(len);
588 }
589
590 status_t err = growData(len);
591 if (err == NO_ERROR) goto restart_write;
592 return err;
593}
594
595status_t Parcel::write(const void* data, size_t len)
596{
597 void* const d = writeInplace(len);
598 if (d) {
599 memcpy(d, data, len);
600 return NO_ERROR;
601 }
602 return mError;
603}
604
605void* Parcel::writeInplace(size_t len)
606{
607 const size_t padded = PAD_SIZE(len);
608
609 // sanity check for integer overflow
610 if (mDataPos+padded < mDataPos) {
611 return NULL;
612 }
613
614 if ((mDataPos+padded) <= mDataCapacity) {
615restart_write:
616 //printf("Writing %ld bytes, padded to %ld\n", len, padded);
617 uint8_t* const data = mData+mDataPos;
618
619 // Need to pad at end?
620 if (padded != len) {
621#if BYTE_ORDER == BIG_ENDIAN
622 static const uint32_t mask[4] = {
623 0x00000000, 0xffffff00, 0xffff0000, 0xff000000
624 };
625#endif
626#if BYTE_ORDER == LITTLE_ENDIAN
627 static const uint32_t mask[4] = {
628 0x00000000, 0x00ffffff, 0x0000ffff, 0x000000ff
629 };
630#endif
631 //printf("Applying pad mask: %p to %p\n", (void*)mask[padded-len],
632 // *reinterpret_cast<void**>(data+padded-4));
633 *reinterpret_cast<uint32_t*>(data+padded-4) &= mask[padded-len];
634 }
635
636 finishWrite(padded);
637 return data;
638 }
639
640 status_t err = growData(padded);
641 if (err == NO_ERROR) goto restart_write;
642 return NULL;
643}
644
645status_t Parcel::writeInt32(int32_t val)
646{
Andreas Huber84a6d042009-08-17 13:33:27 -0700647 return writeAligned(val);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700648}
Marco Nelissen5c0106e2013-10-16 10:57:51 -0700649status_t Parcel::writeInt32Array(size_t len, const int32_t *val) {
650 if (!val) {
651 return writeAligned(-1);
652 }
653 status_t ret = writeAligned(len);
654 if (ret == NO_ERROR) {
655 ret = write(val, len * sizeof(*val));
656 }
657 return ret;
658}
Marco Nelissenf0190bf2014-03-13 14:17:40 -0700659status_t Parcel::writeByteArray(size_t len, const uint8_t *val) {
660 if (!val) {
661 return writeAligned(-1);
662 }
663 status_t ret = writeAligned(len);
664 if (ret == NO_ERROR) {
665 ret = write(val, len * sizeof(*val));
666 }
667 return ret;
668}
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700669
670status_t Parcel::writeInt64(int64_t val)
671{
Andreas Huber84a6d042009-08-17 13:33:27 -0700672 return writeAligned(val);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700673}
674
Serban Constantinescuf683e012013-11-05 16:53:55 +0000675status_t Parcel::writePointer(uintptr_t val)
676{
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800677 return writeAligned<binder_uintptr_t>(val);
Serban Constantinescuf683e012013-11-05 16:53:55 +0000678}
679
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700680status_t Parcel::writeFloat(float val)
681{
Andreas Huber84a6d042009-08-17 13:33:27 -0700682 return writeAligned(val);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700683}
684
Douglas Leungcc1a4bb2013-01-11 15:00:55 -0800685#if defined(__mips__) && defined(__mips_hard_float)
686
687status_t Parcel::writeDouble(double val)
688{
689 union {
690 double d;
691 unsigned long long ll;
692 } u;
693 u.d = val;
694 return writeAligned(u.ll);
695}
696
697#else
698
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700699status_t Parcel::writeDouble(double val)
700{
Andreas Huber84a6d042009-08-17 13:33:27 -0700701 return writeAligned(val);
702}
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700703
Douglas Leungcc1a4bb2013-01-11 15:00:55 -0800704#endif
705
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700706status_t Parcel::writeCString(const char* str)
707{
708 return write(str, strlen(str)+1);
709}
710
711status_t Parcel::writeString8(const String8& str)
712{
713 status_t err = writeInt32(str.bytes());
Pravat Dalbeherad1dff8d2010-12-15 08:40:00 +0100714 // only write string if its length is more than zero characters,
715 // as readString8 will only read if the length field is non-zero.
716 // this is slightly different from how writeString16 works.
717 if (str.bytes() > 0 && err == NO_ERROR) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700718 err = write(str.string(), str.bytes()+1);
719 }
720 return err;
721}
722
723status_t Parcel::writeString16(const String16& str)
724{
725 return writeString16(str.string(), str.size());
726}
727
728status_t Parcel::writeString16(const char16_t* str, size_t len)
729{
730 if (str == NULL) return writeInt32(-1);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700731
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700732 status_t err = writeInt32(len);
733 if (err == NO_ERROR) {
734 len *= sizeof(char16_t);
735 uint8_t* data = (uint8_t*)writeInplace(len+sizeof(char16_t));
736 if (data) {
737 memcpy(data, str, len);
738 *reinterpret_cast<char16_t*>(data+len) = 0;
739 return NO_ERROR;
740 }
741 err = mError;
742 }
743 return err;
744}
745
746status_t Parcel::writeStrongBinder(const sp<IBinder>& val)
747{
748 return flatten_binder(ProcessState::self(), val, this);
749}
750
751status_t Parcel::writeWeakBinder(const wp<IBinder>& val)
752{
753 return flatten_binder(ProcessState::self(), val, this);
754}
755
Mathias Agopiana47f02a2009-05-21 16:29:38 -0700756status_t Parcel::writeNativeHandle(const native_handle* handle)
The Android Open Source Project5f78a482009-01-20 14:03:58 -0800757{
Mathias Agopian1d0a95b2009-07-31 16:12:13 -0700758 if (!handle || handle->version != sizeof(native_handle))
The Android Open Source Project5f78a482009-01-20 14:03:58 -0800759 return BAD_TYPE;
760
761 status_t err;
Mathias Agopiana47f02a2009-05-21 16:29:38 -0700762 err = writeInt32(handle->numFds);
The Android Open Source Project5f78a482009-01-20 14:03:58 -0800763 if (err != NO_ERROR) return err;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800764
Mathias Agopiana47f02a2009-05-21 16:29:38 -0700765 err = writeInt32(handle->numInts);
The Android Open Source Project5f78a482009-01-20 14:03:58 -0800766 if (err != NO_ERROR) return err;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800767
Mathias Agopiana47f02a2009-05-21 16:29:38 -0700768 for (int i=0 ; err==NO_ERROR && i<handle->numFds ; i++)
769 err = writeDupFileDescriptor(handle->data[i]);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800770
771 if (err != NO_ERROR) {
Steve Block9d453682011-12-20 16:23:08 +0000772 ALOGD("write native handle, write dup fd failed");
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800773 return err;
774 }
Mathias Agopiana47f02a2009-05-21 16:29:38 -0700775 err = write(handle->data + handle->numFds, sizeof(int)*handle->numInts);
The Android Open Source Project5f78a482009-01-20 14:03:58 -0800776 return err;
777}
778
Jeff Brown93ff1f92011-11-04 19:01:44 -0700779status_t Parcel::writeFileDescriptor(int fd, bool takeOwnership)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700780{
781 flat_binder_object obj;
782 obj.type = BINDER_TYPE_FD;
783 obj.flags = 0x7f | FLAT_BINDER_FLAG_ACCEPTS_FDS;
Arve Hjønnevåg07fd0f12014-02-18 21:10:29 -0800784 obj.binder = 0; /* Don't pass uninitialized stack data to a remote process */
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700785 obj.handle = fd;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800786 obj.cookie = takeOwnership ? 1 : 0;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700787 return writeObject(obj, true);
788}
789
790status_t Parcel::writeDupFileDescriptor(int fd)
791{
Jeff Brownd341c712011-11-04 20:19:33 -0700792 int dupFd = dup(fd);
793 if (dupFd < 0) {
794 return -errno;
795 }
796 status_t err = writeFileDescriptor(dupFd, true /*takeOwnership*/);
797 if (err) {
798 close(dupFd);
799 }
800 return err;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700801}
802
Mike Lockwoodcbe36fe2013-09-05 08:41:06 -0700803// WARNING: This method must stay in sync with
804// Parcelable.Creator<ParcelFileDescriptor> CREATOR
805// in frameworks/base/core/java/android/os/ParcelFileDescriptor.java
806status_t Parcel::writeParcelFileDescriptor(int fd, int commChannel) {
807 status_t status;
808
809 if (fd < 0) {
810 status = writeInt32(0); // ParcelFileDescriptor is null
811 if (status) return status;
812 } else {
813 status = writeInt32(1); // ParcelFileDescriptor is not null
814 if (status) return status;
815 status = writeDupFileDescriptor(fd);
816 if (status) return status;
817 if (commChannel < 0) {
818 status = writeInt32(0); // commChannel is null
819 if (status) return status;
820 } else {
821 status = writeInt32(1); // commChannel is not null
822 if (status) return status;
823 status = writeDupFileDescriptor(commChannel);
824 }
825 }
826 return status;
827}
828
Jeff Brown5707dbf2011-09-23 21:17:56 -0700829status_t Parcel::writeBlob(size_t len, WritableBlob* outBlob)
830{
831 status_t status;
832
833 if (!mAllowFds || len <= IN_PLACE_BLOB_LIMIT) {
Steve Block6807e592011-10-20 11:56:00 +0100834 ALOGV("writeBlob: write in place");
Jeff Brown5707dbf2011-09-23 21:17:56 -0700835 status = writeInt32(0);
836 if (status) return status;
837
838 void* ptr = writeInplace(len);
839 if (!ptr) return NO_MEMORY;
840
841 outBlob->init(false /*mapped*/, ptr, len);
842 return NO_ERROR;
843 }
844
Steve Block6807e592011-10-20 11:56:00 +0100845 ALOGV("writeBlob: write to ashmem");
Jeff Brown5707dbf2011-09-23 21:17:56 -0700846 int fd = ashmem_create_region("Parcel Blob", len);
847 if (fd < 0) return NO_MEMORY;
848
849 int result = ashmem_set_prot_region(fd, PROT_READ | PROT_WRITE);
850 if (result < 0) {
Jeff Brownec4e0062011-10-10 14:50:10 -0700851 status = result;
Jeff Brown5707dbf2011-09-23 21:17:56 -0700852 } else {
853 void* ptr = ::mmap(NULL, len, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
854 if (ptr == MAP_FAILED) {
855 status = -errno;
856 } else {
857 result = ashmem_set_prot_region(fd, PROT_READ);
858 if (result < 0) {
Jeff Brownec4e0062011-10-10 14:50:10 -0700859 status = result;
Jeff Brown5707dbf2011-09-23 21:17:56 -0700860 } else {
861 status = writeInt32(1);
862 if (!status) {
Jeff Brown93ff1f92011-11-04 19:01:44 -0700863 status = writeFileDescriptor(fd, true /*takeOwnership*/);
Jeff Brown5707dbf2011-09-23 21:17:56 -0700864 if (!status) {
865 outBlob->init(true /*mapped*/, ptr, len);
866 return NO_ERROR;
867 }
868 }
869 }
870 }
871 ::munmap(ptr, len);
872 }
873 ::close(fd);
874 return status;
875}
876
Mathias Agopiane1424282013-07-29 21:24:40 -0700877status_t Parcel::write(const FlattenableHelperInterface& val)
Mathias Agopian98e71dd2010-02-11 17:30:52 -0800878{
879 status_t err;
880
881 // size if needed
Mathias Agopiane1424282013-07-29 21:24:40 -0700882 const size_t len = val.getFlattenedSize();
883 const size_t fd_count = val.getFdCount();
Mathias Agopian98e71dd2010-02-11 17:30:52 -0800884
885 err = this->writeInt32(len);
886 if (err) return err;
887
888 err = this->writeInt32(fd_count);
889 if (err) return err;
890
891 // payload
Mathias Agopiane1424282013-07-29 21:24:40 -0700892 void* const buf = this->writeInplace(PAD_SIZE(len));
Mathias Agopian98e71dd2010-02-11 17:30:52 -0800893 if (buf == NULL)
894 return BAD_VALUE;
895
896 int* fds = NULL;
897 if (fd_count) {
898 fds = new int[fd_count];
899 }
900
901 err = val.flatten(buf, len, fds, fd_count);
902 for (size_t i=0 ; i<fd_count && err==NO_ERROR ; i++) {
903 err = this->writeDupFileDescriptor( fds[i] );
904 }
905
906 if (fd_count) {
907 delete [] fds;
908 }
909
910 return err;
911}
912
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700913status_t Parcel::writeObject(const flat_binder_object& val, bool nullMetaData)
914{
915 const bool enoughData = (mDataPos+sizeof(val)) <= mDataCapacity;
916 const bool enoughObjects = mObjectsSize < mObjectsCapacity;
917 if (enoughData && enoughObjects) {
918restart_write:
919 *reinterpret_cast<flat_binder_object*>(mData+mDataPos) = val;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700920
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700921 // Need to write meta-data?
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800922 if (nullMetaData || val.binder != 0) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700923 mObjects[mObjectsSize] = mDataPos;
924 acquire_object(ProcessState::self(), val, this);
925 mObjectsSize++;
926 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700927
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700928 // remember if it's a file descriptor
929 if (val.type == BINDER_TYPE_FD) {
Dianne Hackborn8938ed22011-09-28 23:19:47 -0400930 if (!mAllowFds) {
931 return FDS_NOT_ALLOWED;
932 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700933 mHasFds = mFdsKnown = true;
934 }
935
936 return finishWrite(sizeof(flat_binder_object));
937 }
938
939 if (!enoughData) {
940 const status_t err = growData(sizeof(val));
941 if (err != NO_ERROR) return err;
942 }
943 if (!enoughObjects) {
944 size_t newSize = ((mObjectsSize+2)*3)/2;
Christopher Tate8b643072016-11-03 13:32:41 -0700945 if (newSize*sizeof(binder_size_t) < mObjectsSize) return NO_MEMORY; // overflow
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800946 binder_size_t* objects = (binder_size_t*)realloc(mObjects, newSize*sizeof(binder_size_t));
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700947 if (objects == NULL) return NO_MEMORY;
948 mObjects = objects;
949 mObjectsCapacity = newSize;
950 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700951
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700952 goto restart_write;
953}
954
Brad Fitzpatrick837a0d02010-07-13 15:33:35 -0700955status_t Parcel::writeNoException()
956{
957 return writeInt32(0);
958}
959
Colin Cross6f4f3ab2014-02-05 17:42:44 -0800960void Parcel::remove(size_t /*start*/, size_t /*amt*/)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700961{
962 LOG_ALWAYS_FATAL("Parcel::remove() not yet implemented!");
963}
964
965status_t Parcel::read(void* outData, size_t len) const
966{
Kenny Root5b61ad22014-03-17 13:18:16 -0700967 if ((mDataPos+PAD_SIZE(len)) >= mDataPos && (mDataPos+PAD_SIZE(len)) <= mDataSize
968 && len <= PAD_SIZE(len)) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700969 memcpy(outData, mData+mDataPos, len);
970 mDataPos += PAD_SIZE(len);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700971 ALOGV("read Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700972 return NO_ERROR;
973 }
974 return NOT_ENOUGH_DATA;
975}
976
977const void* Parcel::readInplace(size_t len) const
978{
Kenny Root5b61ad22014-03-17 13:18:16 -0700979 if ((mDataPos+PAD_SIZE(len)) >= mDataPos && (mDataPos+PAD_SIZE(len)) <= mDataSize
980 && len <= PAD_SIZE(len)) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700981 const void* data = mData+mDataPos;
982 mDataPos += PAD_SIZE(len);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700983 ALOGV("readInplace Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700984 return data;
985 }
986 return NULL;
987}
988
Andreas Huber84a6d042009-08-17 13:33:27 -0700989template<class T>
990status_t Parcel::readAligned(T *pArg) const {
991 COMPILE_TIME_ASSERT_FUNCTION_SCOPE(PAD_SIZE(sizeof(T)) == sizeof(T));
992
993 if ((mDataPos+sizeof(T)) <= mDataSize) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700994 const void* data = mData+mDataPos;
Andreas Huber84a6d042009-08-17 13:33:27 -0700995 mDataPos += sizeof(T);
996 *pArg = *reinterpret_cast<const T*>(data);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700997 return NO_ERROR;
998 } else {
999 return NOT_ENOUGH_DATA;
1000 }
1001}
1002
Andreas Huber84a6d042009-08-17 13:33:27 -07001003template<class T>
1004T Parcel::readAligned() const {
1005 T result;
1006 if (readAligned(&result) != NO_ERROR) {
1007 result = 0;
1008 }
1009
1010 return result;
1011}
1012
1013template<class T>
1014status_t Parcel::writeAligned(T val) {
1015 COMPILE_TIME_ASSERT_FUNCTION_SCOPE(PAD_SIZE(sizeof(T)) == sizeof(T));
1016
1017 if ((mDataPos+sizeof(val)) <= mDataCapacity) {
1018restart_write:
1019 *reinterpret_cast<T*>(mData+mDataPos) = val;
1020 return finishWrite(sizeof(val));
1021 }
1022
1023 status_t err = growData(sizeof(val));
1024 if (err == NO_ERROR) goto restart_write;
1025 return err;
1026}
1027
1028status_t Parcel::readInt32(int32_t *pArg) const
1029{
1030 return readAligned(pArg);
1031}
1032
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001033int32_t Parcel::readInt32() const
1034{
Andreas Huber84a6d042009-08-17 13:33:27 -07001035 return readAligned<int32_t>();
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001036}
1037
1038
1039status_t Parcel::readInt64(int64_t *pArg) const
1040{
Andreas Huber84a6d042009-08-17 13:33:27 -07001041 return readAligned(pArg);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001042}
1043
1044
1045int64_t Parcel::readInt64() const
1046{
Andreas Huber84a6d042009-08-17 13:33:27 -07001047 return readAligned<int64_t>();
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001048}
1049
Serban Constantinescuf683e012013-11-05 16:53:55 +00001050status_t Parcel::readPointer(uintptr_t *pArg) const
1051{
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001052 status_t ret;
1053 binder_uintptr_t ptr;
1054 ret = readAligned(&ptr);
1055 if (!ret)
1056 *pArg = ptr;
1057 return ret;
Serban Constantinescuf683e012013-11-05 16:53:55 +00001058}
1059
1060uintptr_t Parcel::readPointer() const
1061{
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001062 return readAligned<binder_uintptr_t>();
Serban Constantinescuf683e012013-11-05 16:53:55 +00001063}
1064
1065
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001066status_t Parcel::readFloat(float *pArg) const
1067{
Andreas Huber84a6d042009-08-17 13:33:27 -07001068 return readAligned(pArg);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001069}
1070
1071
1072float Parcel::readFloat() const
1073{
Andreas Huber84a6d042009-08-17 13:33:27 -07001074 return readAligned<float>();
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001075}
1076
Douglas Leungcc1a4bb2013-01-11 15:00:55 -08001077#if defined(__mips__) && defined(__mips_hard_float)
1078
1079status_t Parcel::readDouble(double *pArg) const
1080{
1081 union {
1082 double d;
1083 unsigned long long ll;
1084 } u;
Narayan Kamath2c68d382014-06-04 15:04:29 +01001085 u.d = 0;
Douglas Leungcc1a4bb2013-01-11 15:00:55 -08001086 status_t status;
1087 status = readAligned(&u.ll);
1088 *pArg = u.d;
1089 return status;
1090}
1091
1092double Parcel::readDouble() const
1093{
1094 union {
1095 double d;
1096 unsigned long long ll;
1097 } u;
1098 u.ll = readAligned<unsigned long long>();
1099 return u.d;
1100}
1101
1102#else
1103
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001104status_t Parcel::readDouble(double *pArg) const
1105{
Andreas Huber84a6d042009-08-17 13:33:27 -07001106 return readAligned(pArg);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001107}
1108
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001109double Parcel::readDouble() const
1110{
Andreas Huber84a6d042009-08-17 13:33:27 -07001111 return readAligned<double>();
1112}
1113
Douglas Leungcc1a4bb2013-01-11 15:00:55 -08001114#endif
1115
Andreas Huber84a6d042009-08-17 13:33:27 -07001116status_t Parcel::readIntPtr(intptr_t *pArg) const
1117{
1118 return readAligned(pArg);
1119}
1120
1121
1122intptr_t Parcel::readIntPtr() const
1123{
1124 return readAligned<intptr_t>();
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001125}
1126
1127
1128const char* Parcel::readCString() const
1129{
1130 const size_t avail = mDataSize-mDataPos;
1131 if (avail > 0) {
1132 const char* str = reinterpret_cast<const char*>(mData+mDataPos);
1133 // is the string's trailing NUL within the parcel's valid bounds?
1134 const char* eos = reinterpret_cast<const char*>(memchr(str, 0, avail));
1135 if (eos) {
1136 const size_t len = eos - str;
1137 mDataPos += PAD_SIZE(len+1);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001138 ALOGV("readCString Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001139 return str;
1140 }
1141 }
1142 return NULL;
1143}
1144
1145String8 Parcel::readString8() const
1146{
1147 int32_t size = readInt32();
1148 // watch for potential int overflow adding 1 for trailing NUL
1149 if (size > 0 && size < INT32_MAX) {
1150 const char* str = (const char*)readInplace(size+1);
1151 if (str) return String8(str, size);
1152 }
1153 return String8();
1154}
1155
1156String16 Parcel::readString16() const
1157{
1158 size_t len;
1159 const char16_t* str = readString16Inplace(&len);
1160 if (str) return String16(str, len);
Steve Blocke6f43dd2012-01-06 19:20:56 +00001161 ALOGE("Reading a NULL string not supported here.");
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001162 return String16();
1163}
1164
1165const char16_t* Parcel::readString16Inplace(size_t* outLen) const
1166{
1167 int32_t size = readInt32();
1168 // watch for potential int overflow from size+1
1169 if (size >= 0 && size < INT32_MAX) {
1170 *outLen = size;
1171 const char16_t* str = (const char16_t*)readInplace((size+1)*sizeof(char16_t));
1172 if (str != NULL) {
1173 return str;
1174 }
1175 }
1176 *outLen = 0;
1177 return NULL;
1178}
1179
1180sp<IBinder> Parcel::readStrongBinder() const
1181{
1182 sp<IBinder> val;
1183 unflatten_binder(ProcessState::self(), *this, &val);
1184 return val;
1185}
1186
1187wp<IBinder> Parcel::readWeakBinder() const
1188{
1189 wp<IBinder> val;
1190 unflatten_binder(ProcessState::self(), *this, &val);
1191 return val;
1192}
1193
Brad Fitzpatrick837a0d02010-07-13 15:33:35 -07001194int32_t Parcel::readExceptionCode() const
1195{
1196 int32_t exception_code = readAligned<int32_t>();
Brad Fitzpatrickd36f4a52010-07-12 11:05:38 -07001197 if (exception_code == EX_HAS_REPLY_HEADER) {
Magnus Strandberg1ba24572011-05-03 15:44:00 +02001198 int32_t header_start = dataPosition();
Brad Fitzpatrickd36f4a52010-07-12 11:05:38 -07001199 int32_t header_size = readAligned<int32_t>();
1200 // Skip over fat responses headers. Not used (or propagated) in
1201 // native code
Magnus Strandberg1ba24572011-05-03 15:44:00 +02001202 setDataPosition(header_start + header_size);
Brad Fitzpatrickd36f4a52010-07-12 11:05:38 -07001203 // And fat response headers are currently only used when there are no
1204 // exceptions, so return no error:
1205 return 0;
1206 }
Brad Fitzpatrick837a0d02010-07-13 15:33:35 -07001207 return exception_code;
1208}
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001209
Mathias Agopiana47f02a2009-05-21 16:29:38 -07001210native_handle* Parcel::readNativeHandle() const
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001211{
1212 int numFds, numInts;
1213 status_t err;
1214 err = readInt32(&numFds);
1215 if (err != NO_ERROR) return 0;
1216 err = readInt32(&numInts);
1217 if (err != NO_ERROR) return 0;
1218
Mathias Agopiana47f02a2009-05-21 16:29:38 -07001219 native_handle* h = native_handle_create(numFds, numInts);
Adam Lesinski4ff0cb42015-05-12 17:35:48 -07001220 if (!h) {
1221 return 0;
1222 }
1223
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001224 for (int i=0 ; err==NO_ERROR && i<numFds ; i++) {
Rebecca Schultz Zavin360211f2009-02-13 16:34:38 -08001225 h->data[i] = dup(readFileDescriptor());
Marco Nelissen1de79662016-04-26 08:44:09 -07001226 if (h->data[i] < 0) {
1227 for (int j = 0; j < i; j++) {
1228 close(h->data[j]);
1229 }
1230 native_handle_delete(h);
1231 return 0;
1232 }
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001233 }
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001234 err = read(h->data + numFds, sizeof(int)*numInts);
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001235 if (err != NO_ERROR) {
Mathias Agopiana47f02a2009-05-21 16:29:38 -07001236 native_handle_close(h);
1237 native_handle_delete(h);
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001238 h = 0;
1239 }
1240 return h;
1241}
1242
1243
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001244int Parcel::readFileDescriptor() const
1245{
1246 const flat_binder_object* flat = readObject(true);
1247 if (flat) {
1248 switch (flat->type) {
1249 case BINDER_TYPE_FD:
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001250 //ALOGI("Returning file descriptor %ld from parcel %p", flat->handle, this);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001251 return flat->handle;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001252 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001253 }
1254 return BAD_TYPE;
1255}
1256
Mike Lockwoodcbe36fe2013-09-05 08:41:06 -07001257// WARNING: This method must stay in sync with writeToParcel()
1258// in frameworks/base/core/java/android/os/ParcelFileDescriptor.java
1259int Parcel::readParcelFileDescriptor(int& outCommChannel) const {
1260 int fd;
1261 outCommChannel = -1;
1262
1263 if (readInt32() == 0) {
1264 fd = -1;
1265 } else {
1266 fd = readFileDescriptor();
1267 if (fd >= 0 && readInt32() != 0) {
1268 outCommChannel = readFileDescriptor();
1269 }
1270 }
1271 return fd;
1272}
1273
Jeff Brown5707dbf2011-09-23 21:17:56 -07001274status_t Parcel::readBlob(size_t len, ReadableBlob* outBlob) const
1275{
1276 int32_t useAshmem;
1277 status_t status = readInt32(&useAshmem);
1278 if (status) return status;
1279
1280 if (!useAshmem) {
Steve Block6807e592011-10-20 11:56:00 +01001281 ALOGV("readBlob: read in place");
Jeff Brown5707dbf2011-09-23 21:17:56 -07001282 const void* ptr = readInplace(len);
1283 if (!ptr) return BAD_VALUE;
1284
1285 outBlob->init(false /*mapped*/, const_cast<void*>(ptr), len);
1286 return NO_ERROR;
1287 }
1288
Steve Block6807e592011-10-20 11:56:00 +01001289 ALOGV("readBlob: read from ashmem");
Jeff Brown5707dbf2011-09-23 21:17:56 -07001290 int fd = readFileDescriptor();
1291 if (fd == int(BAD_TYPE)) return BAD_VALUE;
1292
1293 void* ptr = ::mmap(NULL, len, PROT_READ, MAP_SHARED, fd, 0);
Narayan Kamath9ea09752014-10-08 17:35:45 +01001294 if (ptr == MAP_FAILED) return NO_MEMORY;
Jeff Brown5707dbf2011-09-23 21:17:56 -07001295
1296 outBlob->init(true /*mapped*/, ptr, len);
1297 return NO_ERROR;
1298}
1299
Mathias Agopiane1424282013-07-29 21:24:40 -07001300status_t Parcel::read(FlattenableHelperInterface& val) const
Mathias Agopian98e71dd2010-02-11 17:30:52 -08001301{
1302 // size
1303 const size_t len = this->readInt32();
1304 const size_t fd_count = this->readInt32();
1305
1306 // payload
Mathias Agopiane1424282013-07-29 21:24:40 -07001307 void const* const buf = this->readInplace(PAD_SIZE(len));
Mathias Agopian98e71dd2010-02-11 17:30:52 -08001308 if (buf == NULL)
1309 return BAD_VALUE;
1310
1311 int* fds = NULL;
1312 if (fd_count) {
1313 fds = new int[fd_count];
1314 }
1315
1316 status_t err = NO_ERROR;
1317 for (size_t i=0 ; i<fd_count && err==NO_ERROR ; i++) {
Jesse Hallfee99042014-11-04 08:36:31 -08001318 fds[i] = dup(this->readFileDescriptor());
Jun Jiangabf8a2c2014-04-29 14:22:10 +08001319 if (fds[i] < 0) {
1320 err = BAD_VALUE;
Jesse Hallfee99042014-11-04 08:36:31 -08001321 ALOGE("dup() failed in Parcel::read, i is %zu, fds[i] is %d, fd_count is %zu, error: %s",
1322 i, fds[i], fd_count, strerror(errno));
Jun Jiangabf8a2c2014-04-29 14:22:10 +08001323 }
Mathias Agopian98e71dd2010-02-11 17:30:52 -08001324 }
1325
1326 if (err == NO_ERROR) {
1327 err = val.unflatten(buf, len, fds, fd_count);
1328 }
1329
1330 if (fd_count) {
1331 delete [] fds;
1332 }
1333
1334 return err;
1335}
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001336const flat_binder_object* Parcel::readObject(bool nullMetaData) const
1337{
1338 const size_t DPOS = mDataPos;
1339 if ((DPOS+sizeof(flat_binder_object)) <= mDataSize) {
1340 const flat_binder_object* obj
1341 = reinterpret_cast<const flat_binder_object*>(mData+DPOS);
1342 mDataPos = DPOS + sizeof(flat_binder_object);
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001343 if (!nullMetaData && (obj->cookie == 0 && obj->binder == 0)) {
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001344 // When transferring a NULL object, we don't write it into
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001345 // the object list, so we don't want to check for it when
1346 // reading.
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001347 ALOGV("readObject Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001348 return obj;
1349 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001350
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001351 // Ensure that this object is valid...
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001352 binder_size_t* const OBJS = mObjects;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001353 const size_t N = mObjectsSize;
1354 size_t opos = mNextObjectHint;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001355
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001356 if (N > 0) {
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001357 ALOGV("Parcel %p looking for obj at %zu, hint=%zu",
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001358 this, DPOS, opos);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001359
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001360 // Start at the current hint position, looking for an object at
1361 // the current data position.
1362 if (opos < N) {
1363 while (opos < (N-1) && OBJS[opos] < DPOS) {
1364 opos++;
1365 }
1366 } else {
1367 opos = N-1;
1368 }
1369 if (OBJS[opos] == DPOS) {
1370 // Found it!
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001371 ALOGV("Parcel %p found obj %zu at index %zu with forward search",
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001372 this, DPOS, opos);
1373 mNextObjectHint = opos+1;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001374 ALOGV("readObject Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001375 return obj;
1376 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001377
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001378 // Look backwards for it...
1379 while (opos > 0 && OBJS[opos] > DPOS) {
1380 opos--;
1381 }
1382 if (OBJS[opos] == DPOS) {
1383 // Found it!
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001384 ALOGV("Parcel %p found obj %zu at index %zu with backward search",
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001385 this, DPOS, opos);
1386 mNextObjectHint = opos+1;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001387 ALOGV("readObject Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001388 return obj;
1389 }
1390 }
Colin Cross6f4f3ab2014-02-05 17:42:44 -08001391 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 -07001392 this, DPOS);
1393 }
1394 return NULL;
1395}
1396
1397void Parcel::closeFileDescriptors()
1398{
1399 size_t i = mObjectsSize;
1400 if (i > 0) {
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001401 //ALOGI("Closing file descriptors for %zu objects...", i);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001402 }
1403 while (i > 0) {
1404 i--;
1405 const flat_binder_object* flat
1406 = reinterpret_cast<flat_binder_object*>(mData+mObjects[i]);
1407 if (flat->type == BINDER_TYPE_FD) {
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001408 //ALOGI("Closing fd: %ld", flat->handle);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001409 close(flat->handle);
1410 }
1411 }
1412}
1413
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001414uintptr_t Parcel::ipcData() const
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001415{
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001416 return reinterpret_cast<uintptr_t>(mData);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001417}
1418
1419size_t Parcel::ipcDataSize() const
1420{
1421 return (mDataSize > mDataPos ? mDataSize : mDataPos);
1422}
1423
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001424uintptr_t Parcel::ipcObjects() const
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001425{
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001426 return reinterpret_cast<uintptr_t>(mObjects);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001427}
1428
1429size_t Parcel::ipcObjectsCount() const
1430{
1431 return mObjectsSize;
1432}
1433
1434void Parcel::ipcSetDataReference(const uint8_t* data, size_t dataSize,
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001435 const binder_size_t* objects, size_t objectsCount, release_func relFunc, void* relCookie)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001436{
Arve Hjønnevåg6f286112014-02-19 20:42:13 -08001437 binder_size_t minOffset = 0;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001438 freeDataNoInit();
1439 mError = NO_ERROR;
1440 mData = const_cast<uint8_t*>(data);
1441 mDataSize = mDataCapacity = dataSize;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001442 //ALOGI("setDataReference Setting data size of %p to %lu (pid=%d)", this, mDataSize, getpid());
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001443 mDataPos = 0;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001444 ALOGV("setDataReference Setting data pos of %p to %zu", this, mDataPos);
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001445 mObjects = const_cast<binder_size_t*>(objects);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001446 mObjectsSize = mObjectsCapacity = objectsCount;
1447 mNextObjectHint = 0;
1448 mOwner = relFunc;
1449 mOwnerCookie = relCookie;
Arve Hjønnevågf50b9ea2014-02-13 19:22:08 -08001450 for (size_t i = 0; i < mObjectsSize; i++) {
Arve Hjønnevåg6f286112014-02-19 20:42:13 -08001451 binder_size_t offset = mObjects[i];
Arve Hjønnevågf50b9ea2014-02-13 19:22:08 -08001452 if (offset < minOffset) {
Arve Hjønnevåg6f286112014-02-19 20:42:13 -08001453 ALOGE("%s: bad object offset %"PRIu64" < %"PRIu64"\n",
1454 __func__, (uint64_t)offset, (uint64_t)minOffset);
Arve Hjønnevågf50b9ea2014-02-13 19:22:08 -08001455 mObjectsSize = 0;
1456 break;
1457 }
1458 minOffset = offset + sizeof(flat_binder_object);
1459 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001460 scanForFds();
1461}
1462
Colin Cross6f4f3ab2014-02-05 17:42:44 -08001463void Parcel::print(TextOutput& to, uint32_t /*flags*/) const
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001464{
1465 to << "Parcel(";
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001466
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001467 if (errorCheck() != NO_ERROR) {
1468 const status_t err = errorCheck();
Colin Cross6f4f3ab2014-02-05 17:42:44 -08001469 to << "Error: " << (void*)(intptr_t)err << " \"" << strerror(-err) << "\"";
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001470 } else if (dataSize() > 0) {
1471 const uint8_t* DATA = data();
1472 to << indent << HexDump(DATA, dataSize()) << dedent;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001473 const binder_size_t* OBJS = objects();
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001474 const size_t N = objectsCount();
1475 for (size_t i=0; i<N; i++) {
1476 const flat_binder_object* flat
1477 = reinterpret_cast<const flat_binder_object*>(DATA+OBJS[i]);
1478 to << endl << "Object #" << i << " @ " << (void*)OBJS[i] << ": "
1479 << TypeCode(flat->type & 0x7f7f7f00)
1480 << " = " << flat->binder;
1481 }
1482 } else {
1483 to << "NULL";
1484 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001485
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001486 to << ")";
1487}
1488
1489void Parcel::releaseObjects()
1490{
1491 const sp<ProcessState> proc(ProcessState::self());
1492 size_t i = mObjectsSize;
1493 uint8_t* const data = mData;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001494 binder_size_t* const objects = mObjects;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001495 while (i > 0) {
1496 i--;
1497 const flat_binder_object* flat
1498 = reinterpret_cast<flat_binder_object*>(data+objects[i]);
1499 release_object(proc, *flat, this);
1500 }
1501}
1502
1503void Parcel::acquireObjects()
1504{
1505 const sp<ProcessState> proc(ProcessState::self());
1506 size_t i = mObjectsSize;
1507 uint8_t* const data = mData;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001508 binder_size_t* const objects = mObjects;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001509 while (i > 0) {
1510 i--;
1511 const flat_binder_object* flat
1512 = reinterpret_cast<flat_binder_object*>(data+objects[i]);
1513 acquire_object(proc, *flat, this);
1514 }
1515}
1516
1517void Parcel::freeData()
1518{
1519 freeDataNoInit();
1520 initState();
1521}
1522
1523void Parcel::freeDataNoInit()
1524{
1525 if (mOwner) {
Dianne Hackborn7e790af2014-11-11 12:22:53 -08001526 LOG_ALLOC("Parcel %p: freeing other owner data", this);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001527 //ALOGI("Freeing data ref of %p (pid=%d)", this, getpid());
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001528 mOwner(this, mData, mDataSize, mObjects, mObjectsSize, mOwnerCookie);
1529 } else {
Dianne Hackborn7e790af2014-11-11 12:22:53 -08001530 LOG_ALLOC("Parcel %p: freeing allocated data", this);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001531 releaseObjects();
Dianne Hackborn7e790af2014-11-11 12:22:53 -08001532 if (mData) {
1533 LOG_ALLOC("Parcel %p: freeing with %zu capacity", this, mDataCapacity);
Dianne Hackborna4cff882014-11-13 17:07:40 -08001534 pthread_mutex_lock(&gParcelGlobalAllocSizeLock);
Dianne Hackborn7e790af2014-11-11 12:22:53 -08001535 gParcelGlobalAllocSize -= mDataCapacity;
1536 gParcelGlobalAllocCount--;
Dianne Hackborna4cff882014-11-13 17:07:40 -08001537 pthread_mutex_unlock(&gParcelGlobalAllocSizeLock);
Dianne Hackborn7e790af2014-11-11 12:22:53 -08001538 free(mData);
1539 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001540 if (mObjects) free(mObjects);
1541 }
1542}
1543
1544status_t Parcel::growData(size_t len)
1545{
1546 size_t newSize = ((mDataSize+len)*3)/2;
1547 return (newSize <= mDataSize)
1548 ? (status_t) NO_MEMORY
1549 : continueWrite(newSize);
1550}
1551
1552status_t Parcel::restartWrite(size_t desired)
1553{
1554 if (mOwner) {
1555 freeData();
1556 return continueWrite(desired);
1557 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001558
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001559 uint8_t* data = (uint8_t*)realloc(mData, desired);
1560 if (!data && desired > mDataCapacity) {
1561 mError = NO_MEMORY;
1562 return NO_MEMORY;
1563 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001564
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001565 releaseObjects();
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001566
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001567 if (data) {
Dianne Hackborn7e790af2014-11-11 12:22:53 -08001568 LOG_ALLOC("Parcel %p: restart from %zu to %zu capacity", this, mDataCapacity, desired);
Dianne Hackborna4cff882014-11-13 17:07:40 -08001569 pthread_mutex_lock(&gParcelGlobalAllocSizeLock);
Dianne Hackborn7e790af2014-11-11 12:22:53 -08001570 gParcelGlobalAllocSize += desired;
1571 gParcelGlobalAllocSize -= mDataCapacity;
Dianne Hackborna4cff882014-11-13 17:07:40 -08001572 pthread_mutex_unlock(&gParcelGlobalAllocSizeLock);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001573 mData = data;
1574 mDataCapacity = desired;
1575 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001576
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001577 mDataSize = mDataPos = 0;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001578 ALOGV("restartWrite Setting data size of %p to %zu", this, mDataSize);
1579 ALOGV("restartWrite Setting data pos of %p to %zu", this, mDataPos);
1580
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001581 free(mObjects);
1582 mObjects = NULL;
1583 mObjectsSize = mObjectsCapacity = 0;
1584 mNextObjectHint = 0;
1585 mHasFds = false;
1586 mFdsKnown = true;
Dianne Hackborn8938ed22011-09-28 23:19:47 -04001587 mAllowFds = true;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001588
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001589 return NO_ERROR;
1590}
1591
1592status_t Parcel::continueWrite(size_t desired)
1593{
1594 // If shrinking, first adjust for any objects that appear
1595 // after the new data size.
1596 size_t objectsSize = mObjectsSize;
1597 if (desired < mDataSize) {
1598 if (desired == 0) {
1599 objectsSize = 0;
1600 } else {
1601 while (objectsSize > 0) {
1602 if (mObjects[objectsSize-1] < desired)
1603 break;
1604 objectsSize--;
1605 }
1606 }
1607 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001608
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001609 if (mOwner) {
1610 // If the size is going to zero, just release the owner's data.
1611 if (desired == 0) {
1612 freeData();
1613 return NO_ERROR;
1614 }
1615
1616 // If there is a different owner, we need to take
1617 // posession.
1618 uint8_t* data = (uint8_t*)malloc(desired);
1619 if (!data) {
1620 mError = NO_MEMORY;
1621 return NO_MEMORY;
1622 }
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001623 binder_size_t* objects = NULL;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001624
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001625 if (objectsSize) {
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001626 objects = (binder_size_t*)malloc(objectsSize*sizeof(binder_size_t));
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001627 if (!objects) {
Hyejin Kim3f727c02013-03-09 11:28:54 +09001628 free(data);
1629
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001630 mError = NO_MEMORY;
1631 return NO_MEMORY;
1632 }
1633
1634 // Little hack to only acquire references on objects
1635 // we will be keeping.
1636 size_t oldObjectsSize = mObjectsSize;
1637 mObjectsSize = objectsSize;
1638 acquireObjects();
1639 mObjectsSize = oldObjectsSize;
1640 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001641
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001642 if (mData) {
1643 memcpy(data, mData, mDataSize < desired ? mDataSize : desired);
1644 }
1645 if (objects && mObjects) {
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001646 memcpy(objects, mObjects, objectsSize*sizeof(binder_size_t));
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001647 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001648 //ALOGI("Freeing data ref of %p (pid=%d)", this, getpid());
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001649 mOwner(this, mData, mDataSize, mObjects, mObjectsSize, mOwnerCookie);
1650 mOwner = NULL;
1651
Dianne Hackborn7e790af2014-11-11 12:22:53 -08001652 LOG_ALLOC("Parcel %p: taking ownership of %zu capacity", this, desired);
Dianne Hackborna4cff882014-11-13 17:07:40 -08001653 pthread_mutex_lock(&gParcelGlobalAllocSizeLock);
Dianne Hackborn7e790af2014-11-11 12:22:53 -08001654 gParcelGlobalAllocSize += desired;
1655 gParcelGlobalAllocCount++;
Dianne Hackborna4cff882014-11-13 17:07:40 -08001656 pthread_mutex_unlock(&gParcelGlobalAllocSizeLock);
Dianne Hackborn7e790af2014-11-11 12:22:53 -08001657
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001658 mData = data;
1659 mObjects = objects;
1660 mDataSize = (mDataSize < desired) ? mDataSize : desired;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001661 ALOGV("continueWrite Setting data size of %p to %zu", this, mDataSize);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001662 mDataCapacity = desired;
1663 mObjectsSize = mObjectsCapacity = objectsSize;
1664 mNextObjectHint = 0;
1665
1666 } else if (mData) {
1667 if (objectsSize < mObjectsSize) {
1668 // Need to release refs on any objects we are dropping.
1669 const sp<ProcessState> proc(ProcessState::self());
1670 for (size_t i=objectsSize; i<mObjectsSize; i++) {
1671 const flat_binder_object* flat
1672 = reinterpret_cast<flat_binder_object*>(mData+mObjects[i]);
1673 if (flat->type == BINDER_TYPE_FD) {
1674 // will need to rescan because we may have lopped off the only FDs
1675 mFdsKnown = false;
1676 }
1677 release_object(proc, *flat, this);
1678 }
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001679 binder_size_t* objects =
1680 (binder_size_t*)realloc(mObjects, objectsSize*sizeof(binder_size_t));
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001681 if (objects) {
1682 mObjects = objects;
1683 }
1684 mObjectsSize = objectsSize;
1685 mNextObjectHint = 0;
1686 }
1687
1688 // We own the data, so we can just do a realloc().
1689 if (desired > mDataCapacity) {
1690 uint8_t* data = (uint8_t*)realloc(mData, desired);
1691 if (data) {
Dianne Hackborn7e790af2014-11-11 12:22:53 -08001692 LOG_ALLOC("Parcel %p: continue from %zu to %zu capacity", this, mDataCapacity,
1693 desired);
Dianne Hackborna4cff882014-11-13 17:07:40 -08001694 pthread_mutex_lock(&gParcelGlobalAllocSizeLock);
Dianne Hackborn7e790af2014-11-11 12:22:53 -08001695 gParcelGlobalAllocSize += desired;
1696 gParcelGlobalAllocSize -= mDataCapacity;
Dianne Hackborna4cff882014-11-13 17:07:40 -08001697 pthread_mutex_unlock(&gParcelGlobalAllocSizeLock);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001698 mData = data;
1699 mDataCapacity = desired;
1700 } else if (desired > mDataCapacity) {
1701 mError = NO_MEMORY;
1702 return NO_MEMORY;
1703 }
1704 } else {
Dianne Hackborn97e2bcd2011-04-13 18:15:56 -07001705 if (mDataSize > desired) {
1706 mDataSize = desired;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001707 ALOGV("continueWrite Setting data size of %p to %zu", this, mDataSize);
Dianne Hackborn97e2bcd2011-04-13 18:15:56 -07001708 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001709 if (mDataPos > desired) {
1710 mDataPos = desired;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001711 ALOGV("continueWrite Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001712 }
1713 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001714
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001715 } else {
1716 // This is the first data. Easy!
1717 uint8_t* data = (uint8_t*)malloc(desired);
1718 if (!data) {
1719 mError = NO_MEMORY;
1720 return NO_MEMORY;
1721 }
Hyejin Kim3f727c02013-03-09 11:28:54 +09001722
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001723 if(!(mDataCapacity == 0 && mObjects == NULL
1724 && mObjectsCapacity == 0)) {
Colin Cross6f4f3ab2014-02-05 17:42:44 -08001725 ALOGE("continueWrite: %zu/%p/%zu/%zu", mDataCapacity, mObjects, mObjectsCapacity, desired);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001726 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001727
Dianne Hackborn7e790af2014-11-11 12:22:53 -08001728 LOG_ALLOC("Parcel %p: allocating with %zu capacity", this, desired);
Dianne Hackborna4cff882014-11-13 17:07:40 -08001729 pthread_mutex_lock(&gParcelGlobalAllocSizeLock);
Dianne Hackborn7e790af2014-11-11 12:22:53 -08001730 gParcelGlobalAllocSize += desired;
1731 gParcelGlobalAllocCount++;
Dianne Hackborna4cff882014-11-13 17:07:40 -08001732 pthread_mutex_unlock(&gParcelGlobalAllocSizeLock);
Dianne Hackborn7e790af2014-11-11 12:22:53 -08001733
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001734 mData = data;
1735 mDataSize = mDataPos = 0;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001736 ALOGV("continueWrite Setting data size of %p to %zu", this, mDataSize);
1737 ALOGV("continueWrite Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001738 mDataCapacity = desired;
1739 }
1740
1741 return NO_ERROR;
1742}
1743
1744void Parcel::initState()
1745{
Dianne Hackborn7e790af2014-11-11 12:22:53 -08001746 LOG_ALLOC("Parcel %p: initState", this);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001747 mError = NO_ERROR;
1748 mData = 0;
1749 mDataSize = 0;
1750 mDataCapacity = 0;
1751 mDataPos = 0;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001752 ALOGV("initState Setting data size of %p to %zu", this, mDataSize);
1753 ALOGV("initState Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001754 mObjects = NULL;
1755 mObjectsSize = 0;
1756 mObjectsCapacity = 0;
1757 mNextObjectHint = 0;
1758 mHasFds = false;
1759 mFdsKnown = true;
Dianne Hackborn8938ed22011-09-28 23:19:47 -04001760 mAllowFds = true;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001761 mOwner = NULL;
1762}
1763
1764void Parcel::scanForFds() const
1765{
1766 bool hasFds = false;
1767 for (size_t i=0; i<mObjectsSize; i++) {
1768 const flat_binder_object* flat
1769 = reinterpret_cast<const flat_binder_object*>(mData + mObjects[i]);
1770 if (flat->type == BINDER_TYPE_FD) {
1771 hasFds = true;
1772 break;
1773 }
1774 }
1775 mHasFds = hasFds;
1776 mFdsKnown = true;
1777}
1778
Jeff Brown5707dbf2011-09-23 21:17:56 -07001779// --- Parcel::Blob ---
1780
1781Parcel::Blob::Blob() :
1782 mMapped(false), mData(NULL), mSize(0) {
1783}
1784
1785Parcel::Blob::~Blob() {
1786 release();
1787}
1788
1789void Parcel::Blob::release() {
1790 if (mMapped && mData) {
1791 ::munmap(mData, mSize);
1792 }
1793 clear();
1794}
1795
1796void Parcel::Blob::init(bool mapped, void* data, size_t size) {
1797 mMapped = mapped;
1798 mData = data;
1799 mSize = size;
1800}
1801
1802void Parcel::Blob::clear() {
1803 mMapped = false;
1804 mData = NULL;
1805 mSize = 0;
1806}
1807
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001808}; // namespace android