blob: d4dd8c7f29d63823aa79d26b9779283311c2fa45 [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
Nick Kralevichb6b14232015-04-02 09:36:02 -070057// This macro should never be used at runtime, as a too large value
58// of s could cause an integer overflow. Instead, you should always
59// use the wrapper function pad_size()
60#define PAD_SIZE_UNSAFE(s) (((s)+3)&~3)
61
62static size_t pad_size(size_t s) {
63 if (s > (SIZE_T_MAX - 3)) {
64 abort();
65 }
66 return PAD_SIZE_UNSAFE(s);
67}
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -070068
Brad Fitzpatricka877cd82010-07-07 16:06:39 -070069// Note: must be kept in sync with android/os/StrictMode.java's PENALTY_GATHER
Jeff Sharkey0c1f5cb2014-12-18 10:26:57 -080070#define STRICT_MODE_PENALTY_GATHER (0x40 << 16)
Brad Fitzpatricka877cd82010-07-07 16:06:39 -070071
Brad Fitzpatrickd36f4a52010-07-12 11:05:38 -070072// Note: must be kept in sync with android/os/Parcel.java's EX_HAS_REPLY_HEADER
73#define EX_HAS_REPLY_HEADER -128
74
Jeff Brown5707dbf2011-09-23 21:17:56 -070075// Maximum size of a blob to transfer in-place.
76static const size_t IN_PLACE_BLOB_LIMIT = 40 * 1024;
77
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -070078// XXX This can be made public if we want to provide
79// support for typed data.
80struct small_flat_data
81{
82 uint32_t type;
83 uint32_t data;
84};
85
86namespace android {
87
Dianne Hackborna4cff882014-11-13 17:07:40 -080088static pthread_mutex_t gParcelGlobalAllocSizeLock = PTHREAD_MUTEX_INITIALIZER;
89static size_t gParcelGlobalAllocSize = 0;
90static size_t gParcelGlobalAllocCount = 0;
91
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -070092void acquire_object(const sp<ProcessState>& proc,
93 const flat_binder_object& obj, const void* who)
94{
95 switch (obj.type) {
96 case BINDER_TYPE_BINDER:
97 if (obj.binder) {
98 LOG_REFS("Parcel %p acquiring reference on local %p", who, obj.cookie);
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -080099 reinterpret_cast<IBinder*>(obj.cookie)->incStrong(who);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700100 }
101 return;
102 case BINDER_TYPE_WEAK_BINDER:
103 if (obj.binder)
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800104 reinterpret_cast<RefBase::weakref_type*>(obj.binder)->incWeak(who);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700105 return;
106 case BINDER_TYPE_HANDLE: {
107 const sp<IBinder> b = proc->getStrongProxyForHandle(obj.handle);
108 if (b != NULL) {
109 LOG_REFS("Parcel %p acquiring reference on remote %p", who, b.get());
110 b->incStrong(who);
111 }
112 return;
113 }
114 case BINDER_TYPE_WEAK_HANDLE: {
115 const wp<IBinder> b = proc->getWeakProxyForHandle(obj.handle);
116 if (b != NULL) b.get_refs()->incWeak(who);
117 return;
118 }
119 case BINDER_TYPE_FD: {
120 // intentionally blank -- nothing to do to acquire this, but we do
121 // recognize it as a legitimate object type.
122 return;
123 }
124 }
125
Colin Cross6f4f3ab2014-02-05 17:42:44 -0800126 ALOGD("Invalid object type 0x%08x", obj.type);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700127}
128
129void release_object(const sp<ProcessState>& proc,
130 const flat_binder_object& obj, const void* who)
131{
132 switch (obj.type) {
133 case BINDER_TYPE_BINDER:
134 if (obj.binder) {
135 LOG_REFS("Parcel %p releasing reference on local %p", who, obj.cookie);
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800136 reinterpret_cast<IBinder*>(obj.cookie)->decStrong(who);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700137 }
138 return;
139 case BINDER_TYPE_WEAK_BINDER:
140 if (obj.binder)
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800141 reinterpret_cast<RefBase::weakref_type*>(obj.binder)->decWeak(who);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700142 return;
143 case BINDER_TYPE_HANDLE: {
144 const sp<IBinder> b = proc->getStrongProxyForHandle(obj.handle);
145 if (b != NULL) {
146 LOG_REFS("Parcel %p releasing reference on remote %p", who, b.get());
147 b->decStrong(who);
148 }
149 return;
150 }
151 case BINDER_TYPE_WEAK_HANDLE: {
152 const wp<IBinder> b = proc->getWeakProxyForHandle(obj.handle);
153 if (b != NULL) b.get_refs()->decWeak(who);
154 return;
155 }
156 case BINDER_TYPE_FD: {
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800157 if (obj.cookie != 0) close(obj.handle);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700158 return;
159 }
160 }
161
Colin Cross6f4f3ab2014-02-05 17:42:44 -0800162 ALOGE("Invalid object type 0x%08x", obj.type);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700163}
164
165inline static status_t finish_flatten_binder(
Colin Cross6f4f3ab2014-02-05 17:42:44 -0800166 const sp<IBinder>& /*binder*/, const flat_binder_object& flat, Parcel* out)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700167{
168 return out->writeObject(flat, false);
169}
170
Colin Cross6f4f3ab2014-02-05 17:42:44 -0800171status_t flatten_binder(const sp<ProcessState>& /*proc*/,
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700172 const sp<IBinder>& binder, Parcel* out)
173{
174 flat_binder_object obj;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700175
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700176 obj.flags = 0x7f | FLAT_BINDER_FLAG_ACCEPTS_FDS;
177 if (binder != NULL) {
178 IBinder *local = binder->localBinder();
179 if (!local) {
180 BpBinder *proxy = binder->remoteBinder();
181 if (proxy == NULL) {
Steve Blocke6f43dd2012-01-06 19:20:56 +0000182 ALOGE("null proxy");
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700183 }
184 const int32_t handle = proxy ? proxy->handle() : 0;
185 obj.type = BINDER_TYPE_HANDLE;
Arve Hjønnevåg07fd0f12014-02-18 21:10:29 -0800186 obj.binder = 0; /* Don't pass uninitialized stack data to a remote process */
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700187 obj.handle = handle;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800188 obj.cookie = 0;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700189 } else {
190 obj.type = BINDER_TYPE_BINDER;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800191 obj.binder = reinterpret_cast<uintptr_t>(local->getWeakRefs());
192 obj.cookie = reinterpret_cast<uintptr_t>(local);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700193 }
194 } else {
195 obj.type = BINDER_TYPE_BINDER;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800196 obj.binder = 0;
197 obj.cookie = 0;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700198 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700199
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700200 return finish_flatten_binder(binder, obj, out);
201}
202
Colin Cross6f4f3ab2014-02-05 17:42:44 -0800203status_t flatten_binder(const sp<ProcessState>& /*proc*/,
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700204 const wp<IBinder>& binder, Parcel* out)
205{
206 flat_binder_object obj;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700207
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700208 obj.flags = 0x7f | FLAT_BINDER_FLAG_ACCEPTS_FDS;
209 if (binder != NULL) {
210 sp<IBinder> real = binder.promote();
211 if (real != NULL) {
212 IBinder *local = real->localBinder();
213 if (!local) {
214 BpBinder *proxy = real->remoteBinder();
215 if (proxy == NULL) {
Steve Blocke6f43dd2012-01-06 19:20:56 +0000216 ALOGE("null proxy");
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700217 }
218 const int32_t handle = proxy ? proxy->handle() : 0;
219 obj.type = BINDER_TYPE_WEAK_HANDLE;
Arve Hjønnevåg07fd0f12014-02-18 21:10:29 -0800220 obj.binder = 0; /* Don't pass uninitialized stack data to a remote process */
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700221 obj.handle = handle;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800222 obj.cookie = 0;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700223 } else {
224 obj.type = BINDER_TYPE_WEAK_BINDER;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800225 obj.binder = reinterpret_cast<uintptr_t>(binder.get_refs());
226 obj.cookie = reinterpret_cast<uintptr_t>(binder.unsafe_get());
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700227 }
228 return finish_flatten_binder(real, obj, out);
229 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700230
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700231 // XXX How to deal? In order to flatten the given binder,
232 // we need to probe it for information, which requires a primary
233 // reference... but we don't have one.
234 //
235 // The OpenBinder implementation uses a dynamic_cast<> here,
236 // but we can't do that with the different reference counting
237 // implementation we are using.
Steve Blocke6f43dd2012-01-06 19:20:56 +0000238 ALOGE("Unable to unflatten Binder weak reference!");
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700239 obj.type = BINDER_TYPE_BINDER;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800240 obj.binder = 0;
241 obj.cookie = 0;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700242 return finish_flatten_binder(NULL, obj, out);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700243
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700244 } else {
245 obj.type = BINDER_TYPE_BINDER;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800246 obj.binder = 0;
247 obj.cookie = 0;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700248 return finish_flatten_binder(NULL, obj, out);
249 }
250}
251
252inline static status_t finish_unflatten_binder(
Colin Cross6f4f3ab2014-02-05 17:42:44 -0800253 BpBinder* /*proxy*/, const flat_binder_object& /*flat*/,
254 const Parcel& /*in*/)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700255{
256 return NO_ERROR;
257}
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700258
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700259status_t unflatten_binder(const sp<ProcessState>& proc,
260 const Parcel& in, sp<IBinder>* out)
261{
262 const flat_binder_object* flat = in.readObject(false);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700263
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700264 if (flat) {
265 switch (flat->type) {
266 case BINDER_TYPE_BINDER:
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800267 *out = reinterpret_cast<IBinder*>(flat->cookie);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700268 return finish_unflatten_binder(NULL, *flat, in);
269 case BINDER_TYPE_HANDLE:
270 *out = proc->getStrongProxyForHandle(flat->handle);
271 return finish_unflatten_binder(
272 static_cast<BpBinder*>(out->get()), *flat, in);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700273 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700274 }
275 return BAD_TYPE;
276}
277
278status_t unflatten_binder(const sp<ProcessState>& proc,
279 const Parcel& in, wp<IBinder>* out)
280{
281 const flat_binder_object* flat = in.readObject(false);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700282
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700283 if (flat) {
284 switch (flat->type) {
285 case BINDER_TYPE_BINDER:
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800286 *out = reinterpret_cast<IBinder*>(flat->cookie);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700287 return finish_unflatten_binder(NULL, *flat, in);
288 case BINDER_TYPE_WEAK_BINDER:
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800289 if (flat->binder != 0) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700290 out->set_object_and_refs(
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800291 reinterpret_cast<IBinder*>(flat->cookie),
292 reinterpret_cast<RefBase::weakref_type*>(flat->binder));
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700293 } else {
294 *out = NULL;
295 }
296 return finish_unflatten_binder(NULL, *flat, in);
297 case BINDER_TYPE_HANDLE:
298 case BINDER_TYPE_WEAK_HANDLE:
299 *out = proc->getWeakProxyForHandle(flat->handle);
300 return finish_unflatten_binder(
301 static_cast<BpBinder*>(out->unsafe_get()), *flat, in);
302 }
303 }
304 return BAD_TYPE;
305}
306
307// ---------------------------------------------------------------------------
308
309Parcel::Parcel()
310{
Dianne Hackborn7e790af2014-11-11 12:22:53 -0800311 LOG_ALLOC("Parcel %p: constructing", this);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700312 initState();
313}
314
315Parcel::~Parcel()
316{
317 freeDataNoInit();
Dianne Hackborn7e790af2014-11-11 12:22:53 -0800318 LOG_ALLOC("Parcel %p: destroyed", this);
319}
320
321size_t Parcel::getGlobalAllocSize() {
Dianne Hackborna4cff882014-11-13 17:07:40 -0800322 pthread_mutex_lock(&gParcelGlobalAllocSizeLock);
323 size_t size = gParcelGlobalAllocSize;
324 pthread_mutex_unlock(&gParcelGlobalAllocSizeLock);
325 return size;
Dianne Hackborn7e790af2014-11-11 12:22:53 -0800326}
327
328size_t Parcel::getGlobalAllocCount() {
Dianne Hackborna4cff882014-11-13 17:07:40 -0800329 pthread_mutex_lock(&gParcelGlobalAllocSizeLock);
330 size_t count = gParcelGlobalAllocCount;
331 pthread_mutex_unlock(&gParcelGlobalAllocSizeLock);
332 return count;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700333}
334
335const uint8_t* Parcel::data() const
336{
337 return mData;
338}
339
340size_t Parcel::dataSize() const
341{
342 return (mDataSize > mDataPos ? mDataSize : mDataPos);
343}
344
345size_t Parcel::dataAvail() const
346{
347 // TODO: decide what to do about the possibility that this can
348 // report an available-data size that exceeds a Java int's max
349 // positive value, causing havoc. Fortunately this will only
350 // happen if someone constructs a Parcel containing more than two
351 // gigabytes of data, which on typical phone hardware is simply
352 // not possible.
353 return dataSize() - dataPosition();
354}
355
356size_t Parcel::dataPosition() const
357{
358 return mDataPos;
359}
360
361size_t Parcel::dataCapacity() const
362{
363 return mDataCapacity;
364}
365
366status_t Parcel::setDataSize(size_t size)
367{
Nick Kralevichb6b14232015-04-02 09:36:02 -0700368 if (size > INT32_MAX) {
369 // don't accept size_t values which may have come from an
370 // inadvertent conversion from a negative int.
371 return BAD_VALUE;
372 }
373
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700374 status_t err;
375 err = continueWrite(size);
376 if (err == NO_ERROR) {
377 mDataSize = size;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700378 ALOGV("setDataSize Setting data size of %p to %zu", this, mDataSize);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700379 }
380 return err;
381}
382
383void Parcel::setDataPosition(size_t pos) const
384{
Nick Kralevichb6b14232015-04-02 09:36:02 -0700385 if (pos > INT32_MAX) {
386 // don't accept size_t values which may have come from an
387 // inadvertent conversion from a negative int.
388 abort();
389 }
390
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700391 mDataPos = pos;
392 mNextObjectHint = 0;
393}
394
395status_t Parcel::setDataCapacity(size_t size)
396{
Nick Kralevichb6b14232015-04-02 09:36:02 -0700397 if (size > INT32_MAX) {
398 // don't accept size_t values which may have come from an
399 // inadvertent conversion from a negative int.
400 return BAD_VALUE;
401 }
402
Dianne Hackborn97e2bcd2011-04-13 18:15:56 -0700403 if (size > mDataCapacity) return continueWrite(size);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700404 return NO_ERROR;
405}
406
407status_t Parcel::setData(const uint8_t* buffer, size_t len)
408{
Nick Kralevichb6b14232015-04-02 09:36:02 -0700409 if (len > INT32_MAX) {
410 // don't accept size_t values which may have come from an
411 // inadvertent conversion from a negative int.
412 return BAD_VALUE;
413 }
414
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700415 status_t err = restartWrite(len);
416 if (err == NO_ERROR) {
417 memcpy(const_cast<uint8_t*>(data()), buffer, len);
418 mDataSize = len;
419 mFdsKnown = false;
420 }
421 return err;
422}
423
Andreas Huber51faf462011-04-13 10:21:56 -0700424status_t Parcel::appendFrom(const Parcel *parcel, size_t offset, size_t len)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700425{
426 const sp<ProcessState> proc(ProcessState::self());
427 status_t err;
Andreas Huber51faf462011-04-13 10:21:56 -0700428 const uint8_t *data = parcel->mData;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800429 const binder_size_t *objects = parcel->mObjects;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700430 size_t size = parcel->mObjectsSize;
431 int startPos = mDataPos;
432 int firstIndex = -1, lastIndex = -2;
433
434 if (len == 0) {
435 return NO_ERROR;
436 }
437
Nick Kralevichb6b14232015-04-02 09:36:02 -0700438 if (len > INT32_MAX) {
439 // don't accept size_t values which may have come from an
440 // inadvertent conversion from a negative int.
441 return BAD_VALUE;
442 }
443
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700444 // range checks against the source parcel size
445 if ((offset > parcel->mDataSize)
446 || (len > parcel->mDataSize)
447 || (offset + len > parcel->mDataSize)) {
448 return BAD_VALUE;
449 }
450
451 // Count objects in range
452 for (int i = 0; i < (int) size; i++) {
453 size_t off = objects[i];
454 if ((off >= offset) && (off < offset + len)) {
455 if (firstIndex == -1) {
456 firstIndex = i;
457 }
458 lastIndex = i;
459 }
460 }
461 int numObjects = lastIndex - firstIndex + 1;
462
Dianne Hackborn97e2bcd2011-04-13 18:15:56 -0700463 if ((mDataSize+len) > mDataCapacity) {
464 // grow data
465 err = growData(len);
466 if (err != NO_ERROR) {
467 return err;
468 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700469 }
470
471 // append data
472 memcpy(mData + mDataPos, data + offset, len);
473 mDataPos += len;
474 mDataSize += len;
475
Dianne Hackborn8938ed22011-09-28 23:19:47 -0400476 err = NO_ERROR;
477
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700478 if (numObjects > 0) {
479 // grow objects
480 if (mObjectsCapacity < mObjectsSize + numObjects) {
481 int newSize = ((mObjectsSize + numObjects)*3)/2;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800482 binder_size_t *objects =
483 (binder_size_t*)realloc(mObjects, newSize*sizeof(binder_size_t));
484 if (objects == (binder_size_t*)0) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700485 return NO_MEMORY;
486 }
487 mObjects = objects;
488 mObjectsCapacity = newSize;
489 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700490
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700491 // append and acquire objects
492 int idx = mObjectsSize;
493 for (int i = firstIndex; i <= lastIndex; i++) {
494 size_t off = objects[i] - offset + startPos;
495 mObjects[idx++] = off;
496 mObjectsSize++;
497
Dianne Hackborn8af0f822009-05-22 13:20:23 -0700498 flat_binder_object* flat
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700499 = reinterpret_cast<flat_binder_object*>(mData + off);
500 acquire_object(proc, *flat, this);
501
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700502 if (flat->type == BINDER_TYPE_FD) {
Dianne Hackborn8af0f822009-05-22 13:20:23 -0700503 // If this is a file descriptor, we need to dup it so the
504 // new Parcel now owns its own fd, and can declare that we
505 // officially know we have fds.
506 flat->handle = dup(flat->handle);
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800507 flat->cookie = 1;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700508 mHasFds = mFdsKnown = true;
Dianne Hackborn8938ed22011-09-28 23:19:47 -0400509 if (!mAllowFds) {
510 err = FDS_NOT_ALLOWED;
511 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700512 }
513 }
514 }
515
Dianne Hackborn8938ed22011-09-28 23:19:47 -0400516 return err;
517}
518
Dianne Hackborn7746cc32011-10-03 21:09:35 -0700519bool Parcel::pushAllowFds(bool allowFds)
Dianne Hackborn8938ed22011-09-28 23:19:47 -0400520{
521 const bool origValue = mAllowFds;
Dianne Hackborn7746cc32011-10-03 21:09:35 -0700522 if (!allowFds) {
523 mAllowFds = false;
524 }
Dianne Hackborn8938ed22011-09-28 23:19:47 -0400525 return origValue;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700526}
527
Dianne Hackborn7746cc32011-10-03 21:09:35 -0700528void Parcel::restoreAllowFds(bool lastValue)
529{
530 mAllowFds = lastValue;
531}
532
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700533bool Parcel::hasFileDescriptors() const
534{
535 if (!mFdsKnown) {
536 scanForFds();
537 }
538 return mHasFds;
539}
540
Brad Fitzpatrick702ea9d2010-06-18 13:07:53 -0700541// Write RPC headers. (previously just the interface token)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700542status_t Parcel::writeInterfaceToken(const String16& interface)
543{
Brad Fitzpatricka877cd82010-07-07 16:06:39 -0700544 writeInt32(IPCThreadState::self()->getStrictModePolicy() |
545 STRICT_MODE_PENALTY_GATHER);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700546 // currently the interface identification token is just its name as a string
547 return writeString16(interface);
548}
549
Mathias Agopian83c04462009-05-22 19:00:22 -0700550bool Parcel::checkInterface(IBinder* binder) const
551{
Brad Fitzpatrick702ea9d2010-06-18 13:07:53 -0700552 return enforceInterface(binder->getInterfaceDescriptor());
Mathias Agopian83c04462009-05-22 19:00:22 -0700553}
554
Brad Fitzpatricka877cd82010-07-07 16:06:39 -0700555bool Parcel::enforceInterface(const String16& interface,
Brad Fitzpatrick70081a12010-07-27 09:49:11 -0700556 IPCThreadState* threadState) const
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700557{
Brad Fitzpatrick70081a12010-07-27 09:49:11 -0700558 int32_t strictPolicy = readInt32();
559 if (threadState == NULL) {
560 threadState = IPCThreadState::self();
Brad Fitzpatricka877cd82010-07-07 16:06:39 -0700561 }
Brad Fitzpatrick52736032010-08-30 16:01:16 -0700562 if ((threadState->getLastTransactionBinderFlags() &
563 IBinder::FLAG_ONEWAY) != 0) {
564 // For one-way calls, the callee is running entirely
565 // disconnected from the caller, so disable StrictMode entirely.
566 // Not only does disk/network usage not impact the caller, but
567 // there's no way to commuicate back any violations anyway.
568 threadState->setStrictModePolicy(0);
569 } else {
570 threadState->setStrictModePolicy(strictPolicy);
571 }
Mathias Agopian83c04462009-05-22 19:00:22 -0700572 const String16 str(readString16());
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700573 if (str == interface) {
574 return true;
575 } else {
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700576 ALOGW("**** enforceInterface() expected '%s' but read '%s'",
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700577 String8(interface).string(), String8(str).string());
578 return false;
579 }
Brad Fitzpatrick702ea9d2010-06-18 13:07:53 -0700580}
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700581
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800582const binder_size_t* Parcel::objects() const
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700583{
584 return mObjects;
585}
586
587size_t Parcel::objectsCount() const
588{
589 return mObjectsSize;
590}
591
592status_t Parcel::errorCheck() const
593{
594 return mError;
595}
596
597void Parcel::setError(status_t err)
598{
599 mError = err;
600}
601
602status_t Parcel::finishWrite(size_t len)
603{
Nick Kralevichb6b14232015-04-02 09:36:02 -0700604 if (len > INT32_MAX) {
605 // don't accept size_t values which may have come from an
606 // inadvertent conversion from a negative int.
607 return BAD_VALUE;
608 }
609
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700610 //printf("Finish write of %d\n", len);
611 mDataPos += len;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700612 ALOGV("finishWrite Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700613 if (mDataPos > mDataSize) {
614 mDataSize = mDataPos;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700615 ALOGV("finishWrite Setting data size of %p to %zu", this, mDataSize);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700616 }
617 //printf("New pos=%d, size=%d\n", mDataPos, mDataSize);
618 return NO_ERROR;
619}
620
621status_t Parcel::writeUnpadded(const void* data, size_t len)
622{
Nick Kralevichb6b14232015-04-02 09:36:02 -0700623 if (len > INT32_MAX) {
624 // don't accept size_t values which may have come from an
625 // inadvertent conversion from a negative int.
626 return BAD_VALUE;
627 }
628
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700629 size_t end = mDataPos + len;
630 if (end < mDataPos) {
631 // integer overflow
632 return BAD_VALUE;
633 }
634
635 if (end <= mDataCapacity) {
636restart_write:
637 memcpy(mData+mDataPos, data, len);
638 return finishWrite(len);
639 }
640
641 status_t err = growData(len);
642 if (err == NO_ERROR) goto restart_write;
643 return err;
644}
645
646status_t Parcel::write(const void* data, size_t len)
647{
Nick Kralevichb6b14232015-04-02 09:36:02 -0700648 if (len > INT32_MAX) {
649 // don't accept size_t values which may have come from an
650 // inadvertent conversion from a negative int.
651 return BAD_VALUE;
652 }
653
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700654 void* const d = writeInplace(len);
655 if (d) {
656 memcpy(d, data, len);
657 return NO_ERROR;
658 }
659 return mError;
660}
661
662void* Parcel::writeInplace(size_t len)
663{
Nick Kralevichb6b14232015-04-02 09:36:02 -0700664 if (len > INT32_MAX) {
665 // don't accept size_t values which may have come from an
666 // inadvertent conversion from a negative int.
667 return NULL;
668 }
669
670 const size_t padded = pad_size(len);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700671
672 // sanity check for integer overflow
673 if (mDataPos+padded < mDataPos) {
674 return NULL;
675 }
676
677 if ((mDataPos+padded) <= mDataCapacity) {
678restart_write:
679 //printf("Writing %ld bytes, padded to %ld\n", len, padded);
680 uint8_t* const data = mData+mDataPos;
681
682 // Need to pad at end?
683 if (padded != len) {
684#if BYTE_ORDER == BIG_ENDIAN
685 static const uint32_t mask[4] = {
686 0x00000000, 0xffffff00, 0xffff0000, 0xff000000
687 };
688#endif
689#if BYTE_ORDER == LITTLE_ENDIAN
690 static const uint32_t mask[4] = {
691 0x00000000, 0x00ffffff, 0x0000ffff, 0x000000ff
692 };
693#endif
694 //printf("Applying pad mask: %p to %p\n", (void*)mask[padded-len],
695 // *reinterpret_cast<void**>(data+padded-4));
696 *reinterpret_cast<uint32_t*>(data+padded-4) &= mask[padded-len];
697 }
698
699 finishWrite(padded);
700 return data;
701 }
702
703 status_t err = growData(padded);
704 if (err == NO_ERROR) goto restart_write;
705 return NULL;
706}
707
708status_t Parcel::writeInt32(int32_t val)
709{
Andreas Huber84a6d042009-08-17 13:33:27 -0700710 return writeAligned(val);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700711}
Dan Stoza41a0f2f2014-12-01 10:01:10 -0800712
713status_t Parcel::writeUint32(uint32_t val)
714{
715 return writeAligned(val);
716}
717
Marco Nelissen5c0106e2013-10-16 10:57:51 -0700718status_t Parcel::writeInt32Array(size_t len, const int32_t *val) {
Nick Kralevichb6b14232015-04-02 09:36:02 -0700719 if (len > INT32_MAX) {
720 // don't accept size_t values which may have come from an
721 // inadvertent conversion from a negative int.
722 return BAD_VALUE;
723 }
724
Marco Nelissen5c0106e2013-10-16 10:57:51 -0700725 if (!val) {
726 return writeAligned(-1);
727 }
728 status_t ret = writeAligned(len);
729 if (ret == NO_ERROR) {
730 ret = write(val, len * sizeof(*val));
731 }
732 return ret;
733}
Marco Nelissenf0190bf2014-03-13 14:17:40 -0700734status_t Parcel::writeByteArray(size_t len, const uint8_t *val) {
Nick Kralevichb6b14232015-04-02 09:36:02 -0700735 if (len > INT32_MAX) {
736 // don't accept size_t values which may have come from an
737 // inadvertent conversion from a negative int.
738 return BAD_VALUE;
739 }
740
Marco Nelissenf0190bf2014-03-13 14:17:40 -0700741 if (!val) {
742 return writeAligned(-1);
743 }
744 status_t ret = writeAligned(len);
745 if (ret == NO_ERROR) {
746 ret = write(val, len * sizeof(*val));
747 }
748 return ret;
749}
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700750
751status_t Parcel::writeInt64(int64_t val)
752{
Andreas Huber84a6d042009-08-17 13:33:27 -0700753 return writeAligned(val);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700754}
755
Ronghua Wu2d13afd2015-03-16 11:11:07 -0700756status_t Parcel::writeUint64(uint64_t val)
757{
758 return writeAligned(val);
759}
760
Serban Constantinescuf683e012013-11-05 16:53:55 +0000761status_t Parcel::writePointer(uintptr_t val)
762{
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800763 return writeAligned<binder_uintptr_t>(val);
Serban Constantinescuf683e012013-11-05 16:53:55 +0000764}
765
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700766status_t Parcel::writeFloat(float val)
767{
Andreas Huber84a6d042009-08-17 13:33:27 -0700768 return writeAligned(val);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700769}
770
Douglas Leungcc1a4bb2013-01-11 15:00:55 -0800771#if defined(__mips__) && defined(__mips_hard_float)
772
773status_t Parcel::writeDouble(double val)
774{
775 union {
776 double d;
777 unsigned long long ll;
778 } u;
779 u.d = val;
780 return writeAligned(u.ll);
781}
782
783#else
784
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700785status_t Parcel::writeDouble(double val)
786{
Andreas Huber84a6d042009-08-17 13:33:27 -0700787 return writeAligned(val);
788}
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700789
Douglas Leungcc1a4bb2013-01-11 15:00:55 -0800790#endif
791
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700792status_t Parcel::writeCString(const char* str)
793{
794 return write(str, strlen(str)+1);
795}
796
797status_t Parcel::writeString8(const String8& str)
798{
799 status_t err = writeInt32(str.bytes());
Pravat Dalbeherad1dff8d2010-12-15 08:40:00 +0100800 // only write string if its length is more than zero characters,
801 // as readString8 will only read if the length field is non-zero.
802 // this is slightly different from how writeString16 works.
803 if (str.bytes() > 0 && err == NO_ERROR) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700804 err = write(str.string(), str.bytes()+1);
805 }
806 return err;
807}
808
809status_t Parcel::writeString16(const String16& str)
810{
811 return writeString16(str.string(), str.size());
812}
813
814status_t Parcel::writeString16(const char16_t* str, size_t len)
815{
816 if (str == NULL) return writeInt32(-1);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700817
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700818 status_t err = writeInt32(len);
819 if (err == NO_ERROR) {
820 len *= sizeof(char16_t);
821 uint8_t* data = (uint8_t*)writeInplace(len+sizeof(char16_t));
822 if (data) {
823 memcpy(data, str, len);
824 *reinterpret_cast<char16_t*>(data+len) = 0;
825 return NO_ERROR;
826 }
827 err = mError;
828 }
829 return err;
830}
831
832status_t Parcel::writeStrongBinder(const sp<IBinder>& val)
833{
834 return flatten_binder(ProcessState::self(), val, this);
835}
836
837status_t Parcel::writeWeakBinder(const wp<IBinder>& val)
838{
839 return flatten_binder(ProcessState::self(), val, this);
840}
841
Mathias Agopiana47f02a2009-05-21 16:29:38 -0700842status_t Parcel::writeNativeHandle(const native_handle* handle)
The Android Open Source Project5f78a482009-01-20 14:03:58 -0800843{
Mathias Agopian1d0a95b2009-07-31 16:12:13 -0700844 if (!handle || handle->version != sizeof(native_handle))
The Android Open Source Project5f78a482009-01-20 14:03:58 -0800845 return BAD_TYPE;
846
847 status_t err;
Mathias Agopiana47f02a2009-05-21 16:29:38 -0700848 err = writeInt32(handle->numFds);
The Android Open Source Project5f78a482009-01-20 14:03:58 -0800849 if (err != NO_ERROR) return err;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800850
Mathias Agopiana47f02a2009-05-21 16:29:38 -0700851 err = writeInt32(handle->numInts);
The Android Open Source Project5f78a482009-01-20 14:03:58 -0800852 if (err != NO_ERROR) return err;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800853
Mathias Agopiana47f02a2009-05-21 16:29:38 -0700854 for (int i=0 ; err==NO_ERROR && i<handle->numFds ; i++)
855 err = writeDupFileDescriptor(handle->data[i]);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800856
857 if (err != NO_ERROR) {
Steve Block9d453682011-12-20 16:23:08 +0000858 ALOGD("write native handle, write dup fd failed");
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800859 return err;
860 }
Mathias Agopiana47f02a2009-05-21 16:29:38 -0700861 err = write(handle->data + handle->numFds, sizeof(int)*handle->numInts);
The Android Open Source Project5f78a482009-01-20 14:03:58 -0800862 return err;
863}
864
Jeff Brown93ff1f92011-11-04 19:01:44 -0700865status_t Parcel::writeFileDescriptor(int fd, bool takeOwnership)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700866{
867 flat_binder_object obj;
868 obj.type = BINDER_TYPE_FD;
869 obj.flags = 0x7f | FLAT_BINDER_FLAG_ACCEPTS_FDS;
Arve Hjønnevåg07fd0f12014-02-18 21:10:29 -0800870 obj.binder = 0; /* Don't pass uninitialized stack data to a remote process */
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700871 obj.handle = fd;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800872 obj.cookie = takeOwnership ? 1 : 0;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700873 return writeObject(obj, true);
874}
875
876status_t Parcel::writeDupFileDescriptor(int fd)
877{
Jeff Brownd341c712011-11-04 20:19:33 -0700878 int dupFd = dup(fd);
879 if (dupFd < 0) {
880 return -errno;
881 }
882 status_t err = writeFileDescriptor(dupFd, true /*takeOwnership*/);
883 if (err) {
884 close(dupFd);
885 }
886 return err;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700887}
888
Mike Lockwoodcbe36fe2013-09-05 08:41:06 -0700889// WARNING: This method must stay in sync with
890// Parcelable.Creator<ParcelFileDescriptor> CREATOR
891// in frameworks/base/core/java/android/os/ParcelFileDescriptor.java
892status_t Parcel::writeParcelFileDescriptor(int fd, int commChannel) {
893 status_t status;
894
895 if (fd < 0) {
896 status = writeInt32(0); // ParcelFileDescriptor is null
897 if (status) return status;
898 } else {
899 status = writeInt32(1); // ParcelFileDescriptor is not null
900 if (status) return status;
901 status = writeDupFileDescriptor(fd);
902 if (status) return status;
903 if (commChannel < 0) {
904 status = writeInt32(0); // commChannel is null
905 if (status) return status;
906 } else {
907 status = writeInt32(1); // commChannel is not null
908 if (status) return status;
909 status = writeDupFileDescriptor(commChannel);
910 }
911 }
912 return status;
913}
914
Jeff Brown5707dbf2011-09-23 21:17:56 -0700915status_t Parcel::writeBlob(size_t len, WritableBlob* outBlob)
916{
917 status_t status;
918
Nick Kralevichb6b14232015-04-02 09:36:02 -0700919 if (len > INT32_MAX) {
920 // don't accept size_t values which may have come from an
921 // inadvertent conversion from a negative int.
922 return BAD_VALUE;
923 }
924
Jeff Brown5707dbf2011-09-23 21:17:56 -0700925 if (!mAllowFds || len <= IN_PLACE_BLOB_LIMIT) {
Steve Block6807e592011-10-20 11:56:00 +0100926 ALOGV("writeBlob: write in place");
Jeff Brown5707dbf2011-09-23 21:17:56 -0700927 status = writeInt32(0);
928 if (status) return status;
929
930 void* ptr = writeInplace(len);
931 if (!ptr) return NO_MEMORY;
932
933 outBlob->init(false /*mapped*/, ptr, len);
934 return NO_ERROR;
935 }
936
Steve Block6807e592011-10-20 11:56:00 +0100937 ALOGV("writeBlob: write to ashmem");
Jeff Brown5707dbf2011-09-23 21:17:56 -0700938 int fd = ashmem_create_region("Parcel Blob", len);
939 if (fd < 0) return NO_MEMORY;
940
941 int result = ashmem_set_prot_region(fd, PROT_READ | PROT_WRITE);
942 if (result < 0) {
Jeff Brownec4e0062011-10-10 14:50:10 -0700943 status = result;
Jeff Brown5707dbf2011-09-23 21:17:56 -0700944 } else {
945 void* ptr = ::mmap(NULL, len, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
946 if (ptr == MAP_FAILED) {
947 status = -errno;
948 } else {
949 result = ashmem_set_prot_region(fd, PROT_READ);
950 if (result < 0) {
Jeff Brownec4e0062011-10-10 14:50:10 -0700951 status = result;
Jeff Brown5707dbf2011-09-23 21:17:56 -0700952 } else {
953 status = writeInt32(1);
954 if (!status) {
Jeff Brown93ff1f92011-11-04 19:01:44 -0700955 status = writeFileDescriptor(fd, true /*takeOwnership*/);
Jeff Brown5707dbf2011-09-23 21:17:56 -0700956 if (!status) {
957 outBlob->init(true /*mapped*/, ptr, len);
958 return NO_ERROR;
959 }
960 }
961 }
962 }
963 ::munmap(ptr, len);
964 }
965 ::close(fd);
966 return status;
967}
968
Mathias Agopiane1424282013-07-29 21:24:40 -0700969status_t Parcel::write(const FlattenableHelperInterface& val)
Mathias Agopian98e71dd2010-02-11 17:30:52 -0800970{
971 status_t err;
972
973 // size if needed
Mathias Agopiane1424282013-07-29 21:24:40 -0700974 const size_t len = val.getFlattenedSize();
975 const size_t fd_count = val.getFdCount();
Mathias Agopian98e71dd2010-02-11 17:30:52 -0800976
Nick Kralevichb6b14232015-04-02 09:36:02 -0700977 if ((len > INT32_MAX) || (fd_count > INT32_MAX)) {
978 // don't accept size_t values which may have come from an
979 // inadvertent conversion from a negative int.
980 return BAD_VALUE;
981 }
982
Mathias Agopian98e71dd2010-02-11 17:30:52 -0800983 err = this->writeInt32(len);
984 if (err) return err;
985
986 err = this->writeInt32(fd_count);
987 if (err) return err;
988
989 // payload
Nick Kralevichb6b14232015-04-02 09:36:02 -0700990 void* const buf = this->writeInplace(pad_size(len));
Mathias Agopian98e71dd2010-02-11 17:30:52 -0800991 if (buf == NULL)
992 return BAD_VALUE;
993
994 int* fds = NULL;
995 if (fd_count) {
996 fds = new int[fd_count];
997 }
998
999 err = val.flatten(buf, len, fds, fd_count);
1000 for (size_t i=0 ; i<fd_count && err==NO_ERROR ; i++) {
1001 err = this->writeDupFileDescriptor( fds[i] );
1002 }
1003
1004 if (fd_count) {
1005 delete [] fds;
1006 }
1007
1008 return err;
1009}
1010
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001011status_t Parcel::writeObject(const flat_binder_object& val, bool nullMetaData)
1012{
1013 const bool enoughData = (mDataPos+sizeof(val)) <= mDataCapacity;
1014 const bool enoughObjects = mObjectsSize < mObjectsCapacity;
1015 if (enoughData && enoughObjects) {
1016restart_write:
1017 *reinterpret_cast<flat_binder_object*>(mData+mDataPos) = val;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001018
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001019 // Need to write meta-data?
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001020 if (nullMetaData || val.binder != 0) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001021 mObjects[mObjectsSize] = mDataPos;
1022 acquire_object(ProcessState::self(), val, this);
1023 mObjectsSize++;
1024 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001025
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001026 // remember if it's a file descriptor
1027 if (val.type == BINDER_TYPE_FD) {
Dianne Hackborn8938ed22011-09-28 23:19:47 -04001028 if (!mAllowFds) {
1029 return FDS_NOT_ALLOWED;
1030 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001031 mHasFds = mFdsKnown = true;
1032 }
1033
1034 return finishWrite(sizeof(flat_binder_object));
1035 }
1036
1037 if (!enoughData) {
1038 const status_t err = growData(sizeof(val));
1039 if (err != NO_ERROR) return err;
1040 }
1041 if (!enoughObjects) {
1042 size_t newSize = ((mObjectsSize+2)*3)/2;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001043 binder_size_t* objects = (binder_size_t*)realloc(mObjects, newSize*sizeof(binder_size_t));
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001044 if (objects == NULL) return NO_MEMORY;
1045 mObjects = objects;
1046 mObjectsCapacity = newSize;
1047 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001048
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001049 goto restart_write;
1050}
1051
Brad Fitzpatrick837a0d02010-07-13 15:33:35 -07001052status_t Parcel::writeNoException()
1053{
1054 return writeInt32(0);
1055}
1056
Colin Cross6f4f3ab2014-02-05 17:42:44 -08001057void Parcel::remove(size_t /*start*/, size_t /*amt*/)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001058{
1059 LOG_ALWAYS_FATAL("Parcel::remove() not yet implemented!");
1060}
1061
1062status_t Parcel::read(void* outData, size_t len) const
1063{
Nick Kralevichb6b14232015-04-02 09:36:02 -07001064 if (len > INT32_MAX) {
1065 // don't accept size_t values which may have come from an
1066 // inadvertent conversion from a negative int.
1067 return BAD_VALUE;
1068 }
1069
1070 if ((mDataPos+pad_size(len)) >= mDataPos && (mDataPos+pad_size(len)) <= mDataSize
1071 && len <= pad_size(len)) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001072 memcpy(outData, mData+mDataPos, len);
Nick Kralevichb6b14232015-04-02 09:36:02 -07001073 mDataPos += pad_size(len);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001074 ALOGV("read Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001075 return NO_ERROR;
1076 }
1077 return NOT_ENOUGH_DATA;
1078}
1079
1080const void* Parcel::readInplace(size_t len) const
1081{
Nick Kralevichb6b14232015-04-02 09:36:02 -07001082 if (len > INT32_MAX) {
1083 // don't accept size_t values which may have come from an
1084 // inadvertent conversion from a negative int.
1085 return NULL;
1086 }
1087
1088 if ((mDataPos+pad_size(len)) >= mDataPos && (mDataPos+pad_size(len)) <= mDataSize
1089 && len <= pad_size(len)) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001090 const void* data = mData+mDataPos;
Nick Kralevichb6b14232015-04-02 09:36:02 -07001091 mDataPos += pad_size(len);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001092 ALOGV("readInplace Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001093 return data;
1094 }
1095 return NULL;
1096}
1097
Andreas Huber84a6d042009-08-17 13:33:27 -07001098template<class T>
1099status_t Parcel::readAligned(T *pArg) const {
Nick Kralevichb6b14232015-04-02 09:36:02 -07001100 COMPILE_TIME_ASSERT_FUNCTION_SCOPE(PAD_SIZE_UNSAFE(sizeof(T)) == sizeof(T));
Andreas Huber84a6d042009-08-17 13:33:27 -07001101
1102 if ((mDataPos+sizeof(T)) <= mDataSize) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001103 const void* data = mData+mDataPos;
Andreas Huber84a6d042009-08-17 13:33:27 -07001104 mDataPos += sizeof(T);
1105 *pArg = *reinterpret_cast<const T*>(data);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001106 return NO_ERROR;
1107 } else {
1108 return NOT_ENOUGH_DATA;
1109 }
1110}
1111
Andreas Huber84a6d042009-08-17 13:33:27 -07001112template<class T>
1113T Parcel::readAligned() const {
1114 T result;
1115 if (readAligned(&result) != NO_ERROR) {
1116 result = 0;
1117 }
1118
1119 return result;
1120}
1121
1122template<class T>
1123status_t Parcel::writeAligned(T val) {
Nick Kralevichb6b14232015-04-02 09:36:02 -07001124 COMPILE_TIME_ASSERT_FUNCTION_SCOPE(PAD_SIZE_UNSAFE(sizeof(T)) == sizeof(T));
Andreas Huber84a6d042009-08-17 13:33:27 -07001125
1126 if ((mDataPos+sizeof(val)) <= mDataCapacity) {
1127restart_write:
1128 *reinterpret_cast<T*>(mData+mDataPos) = val;
1129 return finishWrite(sizeof(val));
1130 }
1131
1132 status_t err = growData(sizeof(val));
1133 if (err == NO_ERROR) goto restart_write;
1134 return err;
1135}
1136
1137status_t Parcel::readInt32(int32_t *pArg) const
1138{
1139 return readAligned(pArg);
1140}
1141
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001142int32_t Parcel::readInt32() const
1143{
Andreas Huber84a6d042009-08-17 13:33:27 -07001144 return readAligned<int32_t>();
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001145}
1146
Dan Stoza41a0f2f2014-12-01 10:01:10 -08001147status_t Parcel::readUint32(uint32_t *pArg) const
1148{
1149 return readAligned(pArg);
1150}
1151
1152uint32_t Parcel::readUint32() const
1153{
1154 return readAligned<uint32_t>();
1155}
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001156
1157status_t Parcel::readInt64(int64_t *pArg) const
1158{
Andreas Huber84a6d042009-08-17 13:33:27 -07001159 return readAligned(pArg);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001160}
1161
1162
1163int64_t Parcel::readInt64() const
1164{
Andreas Huber84a6d042009-08-17 13:33:27 -07001165 return readAligned<int64_t>();
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001166}
1167
Ronghua Wu2d13afd2015-03-16 11:11:07 -07001168status_t Parcel::readUint64(uint64_t *pArg) const
1169{
1170 return readAligned(pArg);
1171}
1172
1173uint64_t Parcel::readUint64() const
1174{
1175 return readAligned<uint64_t>();
1176}
1177
Serban Constantinescuf683e012013-11-05 16:53:55 +00001178status_t Parcel::readPointer(uintptr_t *pArg) const
1179{
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001180 status_t ret;
1181 binder_uintptr_t ptr;
1182 ret = readAligned(&ptr);
1183 if (!ret)
1184 *pArg = ptr;
1185 return ret;
Serban Constantinescuf683e012013-11-05 16:53:55 +00001186}
1187
1188uintptr_t Parcel::readPointer() const
1189{
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001190 return readAligned<binder_uintptr_t>();
Serban Constantinescuf683e012013-11-05 16:53:55 +00001191}
1192
1193
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001194status_t Parcel::readFloat(float *pArg) const
1195{
Andreas Huber84a6d042009-08-17 13:33:27 -07001196 return readAligned(pArg);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001197}
1198
1199
1200float Parcel::readFloat() const
1201{
Andreas Huber84a6d042009-08-17 13:33:27 -07001202 return readAligned<float>();
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001203}
1204
Douglas Leungcc1a4bb2013-01-11 15:00:55 -08001205#if defined(__mips__) && defined(__mips_hard_float)
1206
1207status_t Parcel::readDouble(double *pArg) const
1208{
1209 union {
1210 double d;
1211 unsigned long long ll;
1212 } u;
Narayan Kamath2c68d382014-06-04 15:04:29 +01001213 u.d = 0;
Douglas Leungcc1a4bb2013-01-11 15:00:55 -08001214 status_t status;
1215 status = readAligned(&u.ll);
1216 *pArg = u.d;
1217 return status;
1218}
1219
1220double Parcel::readDouble() const
1221{
1222 union {
1223 double d;
1224 unsigned long long ll;
1225 } u;
1226 u.ll = readAligned<unsigned long long>();
1227 return u.d;
1228}
1229
1230#else
1231
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001232status_t Parcel::readDouble(double *pArg) const
1233{
Andreas Huber84a6d042009-08-17 13:33:27 -07001234 return readAligned(pArg);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001235}
1236
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001237double Parcel::readDouble() const
1238{
Andreas Huber84a6d042009-08-17 13:33:27 -07001239 return readAligned<double>();
1240}
1241
Douglas Leungcc1a4bb2013-01-11 15:00:55 -08001242#endif
1243
Andreas Huber84a6d042009-08-17 13:33:27 -07001244status_t Parcel::readIntPtr(intptr_t *pArg) const
1245{
1246 return readAligned(pArg);
1247}
1248
1249
1250intptr_t Parcel::readIntPtr() const
1251{
1252 return readAligned<intptr_t>();
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001253}
1254
1255
1256const char* Parcel::readCString() const
1257{
1258 const size_t avail = mDataSize-mDataPos;
1259 if (avail > 0) {
1260 const char* str = reinterpret_cast<const char*>(mData+mDataPos);
1261 // is the string's trailing NUL within the parcel's valid bounds?
1262 const char* eos = reinterpret_cast<const char*>(memchr(str, 0, avail));
1263 if (eos) {
1264 const size_t len = eos - str;
Nick Kralevichb6b14232015-04-02 09:36:02 -07001265 mDataPos += pad_size(len+1);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001266 ALOGV("readCString Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001267 return str;
1268 }
1269 }
1270 return NULL;
1271}
1272
1273String8 Parcel::readString8() const
1274{
1275 int32_t size = readInt32();
1276 // watch for potential int overflow adding 1 for trailing NUL
1277 if (size > 0 && size < INT32_MAX) {
1278 const char* str = (const char*)readInplace(size+1);
1279 if (str) return String8(str, size);
1280 }
1281 return String8();
1282}
1283
1284String16 Parcel::readString16() const
1285{
1286 size_t len;
1287 const char16_t* str = readString16Inplace(&len);
1288 if (str) return String16(str, len);
Steve Blocke6f43dd2012-01-06 19:20:56 +00001289 ALOGE("Reading a NULL string not supported here.");
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001290 return String16();
1291}
1292
1293const char16_t* Parcel::readString16Inplace(size_t* outLen) const
1294{
1295 int32_t size = readInt32();
1296 // watch for potential int overflow from size+1
1297 if (size >= 0 && size < INT32_MAX) {
1298 *outLen = size;
1299 const char16_t* str = (const char16_t*)readInplace((size+1)*sizeof(char16_t));
1300 if (str != NULL) {
1301 return str;
1302 }
1303 }
1304 *outLen = 0;
1305 return NULL;
1306}
1307
1308sp<IBinder> Parcel::readStrongBinder() const
1309{
1310 sp<IBinder> val;
1311 unflatten_binder(ProcessState::self(), *this, &val);
1312 return val;
1313}
1314
1315wp<IBinder> Parcel::readWeakBinder() const
1316{
1317 wp<IBinder> val;
1318 unflatten_binder(ProcessState::self(), *this, &val);
1319 return val;
1320}
1321
Brad Fitzpatrick837a0d02010-07-13 15:33:35 -07001322int32_t Parcel::readExceptionCode() const
1323{
1324 int32_t exception_code = readAligned<int32_t>();
Brad Fitzpatrickd36f4a52010-07-12 11:05:38 -07001325 if (exception_code == EX_HAS_REPLY_HEADER) {
Magnus Strandberg1ba24572011-05-03 15:44:00 +02001326 int32_t header_start = dataPosition();
Brad Fitzpatrickd36f4a52010-07-12 11:05:38 -07001327 int32_t header_size = readAligned<int32_t>();
1328 // Skip over fat responses headers. Not used (or propagated) in
1329 // native code
Magnus Strandberg1ba24572011-05-03 15:44:00 +02001330 setDataPosition(header_start + header_size);
Brad Fitzpatrickd36f4a52010-07-12 11:05:38 -07001331 // And fat response headers are currently only used when there are no
1332 // exceptions, so return no error:
1333 return 0;
1334 }
Brad Fitzpatrick837a0d02010-07-13 15:33:35 -07001335 return exception_code;
1336}
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001337
Mathias Agopiana47f02a2009-05-21 16:29:38 -07001338native_handle* Parcel::readNativeHandle() const
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001339{
1340 int numFds, numInts;
1341 status_t err;
1342 err = readInt32(&numFds);
1343 if (err != NO_ERROR) return 0;
1344 err = readInt32(&numInts);
1345 if (err != NO_ERROR) return 0;
1346
Mathias Agopiana47f02a2009-05-21 16:29:38 -07001347 native_handle* h = native_handle_create(numFds, numInts);
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001348 for (int i=0 ; err==NO_ERROR && i<numFds ; i++) {
Rebecca Schultz Zavin360211f2009-02-13 16:34:38 -08001349 h->data[i] = dup(readFileDescriptor());
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001350 if (h->data[i] < 0) err = BAD_VALUE;
1351 }
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001352 err = read(h->data + numFds, sizeof(int)*numInts);
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001353 if (err != NO_ERROR) {
Mathias Agopiana47f02a2009-05-21 16:29:38 -07001354 native_handle_close(h);
1355 native_handle_delete(h);
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001356 h = 0;
1357 }
1358 return h;
1359}
1360
1361
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001362int Parcel::readFileDescriptor() const
1363{
1364 const flat_binder_object* flat = readObject(true);
1365 if (flat) {
1366 switch (flat->type) {
1367 case BINDER_TYPE_FD:
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001368 //ALOGI("Returning file descriptor %ld from parcel %p", flat->handle, this);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001369 return flat->handle;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001370 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001371 }
1372 return BAD_TYPE;
1373}
1374
Mike Lockwoodcbe36fe2013-09-05 08:41:06 -07001375// WARNING: This method must stay in sync with writeToParcel()
1376// in frameworks/base/core/java/android/os/ParcelFileDescriptor.java
1377int Parcel::readParcelFileDescriptor(int& outCommChannel) const {
1378 int fd;
1379 outCommChannel = -1;
1380
1381 if (readInt32() == 0) {
1382 fd = -1;
1383 } else {
1384 fd = readFileDescriptor();
1385 if (fd >= 0 && readInt32() != 0) {
1386 outCommChannel = readFileDescriptor();
1387 }
1388 }
1389 return fd;
1390}
1391
Jeff Brown5707dbf2011-09-23 21:17:56 -07001392status_t Parcel::readBlob(size_t len, ReadableBlob* outBlob) const
1393{
1394 int32_t useAshmem;
1395 status_t status = readInt32(&useAshmem);
1396 if (status) return status;
1397
1398 if (!useAshmem) {
Steve Block6807e592011-10-20 11:56:00 +01001399 ALOGV("readBlob: read in place");
Jeff Brown5707dbf2011-09-23 21:17:56 -07001400 const void* ptr = readInplace(len);
1401 if (!ptr) return BAD_VALUE;
1402
1403 outBlob->init(false /*mapped*/, const_cast<void*>(ptr), len);
1404 return NO_ERROR;
1405 }
1406
Steve Block6807e592011-10-20 11:56:00 +01001407 ALOGV("readBlob: read from ashmem");
Jeff Brown5707dbf2011-09-23 21:17:56 -07001408 int fd = readFileDescriptor();
1409 if (fd == int(BAD_TYPE)) return BAD_VALUE;
1410
1411 void* ptr = ::mmap(NULL, len, PROT_READ, MAP_SHARED, fd, 0);
Narayan Kamath9ea09752014-10-08 17:35:45 +01001412 if (ptr == MAP_FAILED) return NO_MEMORY;
Jeff Brown5707dbf2011-09-23 21:17:56 -07001413
1414 outBlob->init(true /*mapped*/, ptr, len);
1415 return NO_ERROR;
1416}
1417
Mathias Agopiane1424282013-07-29 21:24:40 -07001418status_t Parcel::read(FlattenableHelperInterface& val) const
Mathias Agopian98e71dd2010-02-11 17:30:52 -08001419{
1420 // size
1421 const size_t len = this->readInt32();
1422 const size_t fd_count = this->readInt32();
1423
Nick Kralevichb6b14232015-04-02 09:36:02 -07001424 if (len > INT32_MAX) {
1425 // don't accept size_t values which may have come from an
1426 // inadvertent conversion from a negative int.
1427 return BAD_VALUE;
1428 }
1429
Mathias Agopian98e71dd2010-02-11 17:30:52 -08001430 // payload
Nick Kralevichb6b14232015-04-02 09:36:02 -07001431 void const* const buf = this->readInplace(pad_size(len));
Mathias Agopian98e71dd2010-02-11 17:30:52 -08001432 if (buf == NULL)
1433 return BAD_VALUE;
1434
1435 int* fds = NULL;
1436 if (fd_count) {
1437 fds = new int[fd_count];
1438 }
1439
1440 status_t err = NO_ERROR;
1441 for (size_t i=0 ; i<fd_count && err==NO_ERROR ; i++) {
Jesse Hallfee99042014-11-04 08:36:31 -08001442 fds[i] = dup(this->readFileDescriptor());
Jun Jiangabf8a2c2014-04-29 14:22:10 +08001443 if (fds[i] < 0) {
1444 err = BAD_VALUE;
Jesse Hallfee99042014-11-04 08:36:31 -08001445 ALOGE("dup() failed in Parcel::read, i is %zu, fds[i] is %d, fd_count is %zu, error: %s",
1446 i, fds[i], fd_count, strerror(errno));
Jun Jiangabf8a2c2014-04-29 14:22:10 +08001447 }
Mathias Agopian98e71dd2010-02-11 17:30:52 -08001448 }
1449
1450 if (err == NO_ERROR) {
1451 err = val.unflatten(buf, len, fds, fd_count);
1452 }
1453
1454 if (fd_count) {
1455 delete [] fds;
1456 }
1457
1458 return err;
1459}
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001460const flat_binder_object* Parcel::readObject(bool nullMetaData) const
1461{
1462 const size_t DPOS = mDataPos;
1463 if ((DPOS+sizeof(flat_binder_object)) <= mDataSize) {
1464 const flat_binder_object* obj
1465 = reinterpret_cast<const flat_binder_object*>(mData+DPOS);
1466 mDataPos = DPOS + sizeof(flat_binder_object);
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001467 if (!nullMetaData && (obj->cookie == 0 && obj->binder == 0)) {
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001468 // When transferring a NULL object, we don't write it into
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001469 // the object list, so we don't want to check for it when
1470 // reading.
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001471 ALOGV("readObject Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001472 return obj;
1473 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001474
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001475 // Ensure that this object is valid...
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001476 binder_size_t* const OBJS = mObjects;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001477 const size_t N = mObjectsSize;
1478 size_t opos = mNextObjectHint;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001479
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001480 if (N > 0) {
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001481 ALOGV("Parcel %p looking for obj at %zu, hint=%zu",
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001482 this, DPOS, opos);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001483
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001484 // Start at the current hint position, looking for an object at
1485 // the current data position.
1486 if (opos < N) {
1487 while (opos < (N-1) && OBJS[opos] < DPOS) {
1488 opos++;
1489 }
1490 } else {
1491 opos = N-1;
1492 }
1493 if (OBJS[opos] == DPOS) {
1494 // Found it!
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001495 ALOGV("Parcel %p found obj %zu at index %zu with forward search",
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001496 this, DPOS, opos);
1497 mNextObjectHint = opos+1;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001498 ALOGV("readObject Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001499 return obj;
1500 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001501
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001502 // Look backwards for it...
1503 while (opos > 0 && OBJS[opos] > DPOS) {
1504 opos--;
1505 }
1506 if (OBJS[opos] == DPOS) {
1507 // Found it!
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001508 ALOGV("Parcel %p found obj %zu at index %zu with backward search",
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001509 this, DPOS, opos);
1510 mNextObjectHint = opos+1;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001511 ALOGV("readObject Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001512 return obj;
1513 }
1514 }
Colin Cross6f4f3ab2014-02-05 17:42:44 -08001515 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 -07001516 this, DPOS);
1517 }
1518 return NULL;
1519}
1520
1521void Parcel::closeFileDescriptors()
1522{
1523 size_t i = mObjectsSize;
1524 if (i > 0) {
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001525 //ALOGI("Closing file descriptors for %zu objects...", i);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001526 }
1527 while (i > 0) {
1528 i--;
1529 const flat_binder_object* flat
1530 = reinterpret_cast<flat_binder_object*>(mData+mObjects[i]);
1531 if (flat->type == BINDER_TYPE_FD) {
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001532 //ALOGI("Closing fd: %ld", flat->handle);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001533 close(flat->handle);
1534 }
1535 }
1536}
1537
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001538uintptr_t Parcel::ipcData() const
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001539{
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001540 return reinterpret_cast<uintptr_t>(mData);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001541}
1542
1543size_t Parcel::ipcDataSize() const
1544{
1545 return (mDataSize > mDataPos ? mDataSize : mDataPos);
1546}
1547
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001548uintptr_t Parcel::ipcObjects() const
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001549{
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001550 return reinterpret_cast<uintptr_t>(mObjects);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001551}
1552
1553size_t Parcel::ipcObjectsCount() const
1554{
1555 return mObjectsSize;
1556}
1557
1558void Parcel::ipcSetDataReference(const uint8_t* data, size_t dataSize,
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001559 const binder_size_t* objects, size_t objectsCount, release_func relFunc, void* relCookie)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001560{
Arve Hjønnevåg6f286112014-02-19 20:42:13 -08001561 binder_size_t minOffset = 0;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001562 freeDataNoInit();
1563 mError = NO_ERROR;
1564 mData = const_cast<uint8_t*>(data);
1565 mDataSize = mDataCapacity = dataSize;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001566 //ALOGI("setDataReference Setting data size of %p to %lu (pid=%d)", this, mDataSize, getpid());
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001567 mDataPos = 0;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001568 ALOGV("setDataReference Setting data pos of %p to %zu", this, mDataPos);
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001569 mObjects = const_cast<binder_size_t*>(objects);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001570 mObjectsSize = mObjectsCapacity = objectsCount;
1571 mNextObjectHint = 0;
1572 mOwner = relFunc;
1573 mOwnerCookie = relCookie;
Arve Hjønnevågf50b9ea2014-02-13 19:22:08 -08001574 for (size_t i = 0; i < mObjectsSize; i++) {
Arve Hjønnevåg6f286112014-02-19 20:42:13 -08001575 binder_size_t offset = mObjects[i];
Arve Hjønnevågf50b9ea2014-02-13 19:22:08 -08001576 if (offset < minOffset) {
Dan Albert3bdc5b82014-11-20 11:50:23 -08001577 ALOGE("%s: bad object offset %" PRIu64 " < %" PRIu64 "\n",
Arve Hjønnevåg6f286112014-02-19 20:42:13 -08001578 __func__, (uint64_t)offset, (uint64_t)minOffset);
Arve Hjønnevågf50b9ea2014-02-13 19:22:08 -08001579 mObjectsSize = 0;
1580 break;
1581 }
1582 minOffset = offset + sizeof(flat_binder_object);
1583 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001584 scanForFds();
1585}
1586
Colin Cross6f4f3ab2014-02-05 17:42:44 -08001587void Parcel::print(TextOutput& to, uint32_t /*flags*/) const
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001588{
1589 to << "Parcel(";
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001590
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001591 if (errorCheck() != NO_ERROR) {
1592 const status_t err = errorCheck();
Colin Cross6f4f3ab2014-02-05 17:42:44 -08001593 to << "Error: " << (void*)(intptr_t)err << " \"" << strerror(-err) << "\"";
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001594 } else if (dataSize() > 0) {
1595 const uint8_t* DATA = data();
1596 to << indent << HexDump(DATA, dataSize()) << dedent;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001597 const binder_size_t* OBJS = objects();
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001598 const size_t N = objectsCount();
1599 for (size_t i=0; i<N; i++) {
1600 const flat_binder_object* flat
1601 = reinterpret_cast<const flat_binder_object*>(DATA+OBJS[i]);
1602 to << endl << "Object #" << i << " @ " << (void*)OBJS[i] << ": "
1603 << TypeCode(flat->type & 0x7f7f7f00)
1604 << " = " << flat->binder;
1605 }
1606 } else {
1607 to << "NULL";
1608 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001609
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001610 to << ")";
1611}
1612
1613void Parcel::releaseObjects()
1614{
1615 const sp<ProcessState> proc(ProcessState::self());
1616 size_t i = mObjectsSize;
1617 uint8_t* const data = mData;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001618 binder_size_t* const objects = mObjects;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001619 while (i > 0) {
1620 i--;
1621 const flat_binder_object* flat
1622 = reinterpret_cast<flat_binder_object*>(data+objects[i]);
1623 release_object(proc, *flat, this);
1624 }
1625}
1626
1627void Parcel::acquireObjects()
1628{
1629 const sp<ProcessState> proc(ProcessState::self());
1630 size_t i = mObjectsSize;
1631 uint8_t* const data = mData;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001632 binder_size_t* const objects = mObjects;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001633 while (i > 0) {
1634 i--;
1635 const flat_binder_object* flat
1636 = reinterpret_cast<flat_binder_object*>(data+objects[i]);
1637 acquire_object(proc, *flat, this);
1638 }
1639}
1640
1641void Parcel::freeData()
1642{
1643 freeDataNoInit();
1644 initState();
1645}
1646
1647void Parcel::freeDataNoInit()
1648{
1649 if (mOwner) {
Dianne Hackborn7e790af2014-11-11 12:22:53 -08001650 LOG_ALLOC("Parcel %p: freeing other owner data", this);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001651 //ALOGI("Freeing data ref of %p (pid=%d)", this, getpid());
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001652 mOwner(this, mData, mDataSize, mObjects, mObjectsSize, mOwnerCookie);
1653 } else {
Dianne Hackborn7e790af2014-11-11 12:22:53 -08001654 LOG_ALLOC("Parcel %p: freeing allocated data", this);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001655 releaseObjects();
Dianne Hackborn7e790af2014-11-11 12:22:53 -08001656 if (mData) {
1657 LOG_ALLOC("Parcel %p: freeing with %zu capacity", this, mDataCapacity);
Dianne Hackborna4cff882014-11-13 17:07:40 -08001658 pthread_mutex_lock(&gParcelGlobalAllocSizeLock);
Dianne Hackborn7e790af2014-11-11 12:22:53 -08001659 gParcelGlobalAllocSize -= mDataCapacity;
1660 gParcelGlobalAllocCount--;
Dianne Hackborna4cff882014-11-13 17:07:40 -08001661 pthread_mutex_unlock(&gParcelGlobalAllocSizeLock);
Dianne Hackborn7e790af2014-11-11 12:22:53 -08001662 free(mData);
1663 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001664 if (mObjects) free(mObjects);
1665 }
1666}
1667
1668status_t Parcel::growData(size_t len)
1669{
Nick Kralevichb6b14232015-04-02 09:36:02 -07001670 if (len > INT32_MAX) {
1671 // don't accept size_t values which may have come from an
1672 // inadvertent conversion from a negative int.
1673 return BAD_VALUE;
1674 }
1675
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001676 size_t newSize = ((mDataSize+len)*3)/2;
1677 return (newSize <= mDataSize)
1678 ? (status_t) NO_MEMORY
1679 : continueWrite(newSize);
1680}
1681
1682status_t Parcel::restartWrite(size_t desired)
1683{
Nick Kralevichb6b14232015-04-02 09:36:02 -07001684 if (desired > INT32_MAX) {
1685 // don't accept size_t values which may have come from an
1686 // inadvertent conversion from a negative int.
1687 return BAD_VALUE;
1688 }
1689
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001690 if (mOwner) {
1691 freeData();
1692 return continueWrite(desired);
1693 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001694
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001695 uint8_t* data = (uint8_t*)realloc(mData, desired);
1696 if (!data && desired > mDataCapacity) {
1697 mError = NO_MEMORY;
1698 return NO_MEMORY;
1699 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001700
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001701 releaseObjects();
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001702
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001703 if (data) {
Dianne Hackborn7e790af2014-11-11 12:22:53 -08001704 LOG_ALLOC("Parcel %p: restart from %zu to %zu capacity", this, mDataCapacity, desired);
Dianne Hackborna4cff882014-11-13 17:07:40 -08001705 pthread_mutex_lock(&gParcelGlobalAllocSizeLock);
Dianne Hackborn7e790af2014-11-11 12:22:53 -08001706 gParcelGlobalAllocSize += desired;
1707 gParcelGlobalAllocSize -= mDataCapacity;
Dianne Hackborna4cff882014-11-13 17:07:40 -08001708 pthread_mutex_unlock(&gParcelGlobalAllocSizeLock);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001709 mData = data;
1710 mDataCapacity = desired;
1711 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001712
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001713 mDataSize = mDataPos = 0;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001714 ALOGV("restartWrite Setting data size of %p to %zu", this, mDataSize);
1715 ALOGV("restartWrite Setting data pos of %p to %zu", this, mDataPos);
1716
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001717 free(mObjects);
1718 mObjects = NULL;
1719 mObjectsSize = mObjectsCapacity = 0;
1720 mNextObjectHint = 0;
1721 mHasFds = false;
1722 mFdsKnown = true;
Dianne Hackborn8938ed22011-09-28 23:19:47 -04001723 mAllowFds = true;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001724
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001725 return NO_ERROR;
1726}
1727
1728status_t Parcel::continueWrite(size_t desired)
1729{
Nick Kralevichb6b14232015-04-02 09:36:02 -07001730 if (desired > INT32_MAX) {
1731 // don't accept size_t values which may have come from an
1732 // inadvertent conversion from a negative int.
1733 return BAD_VALUE;
1734 }
1735
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001736 // If shrinking, first adjust for any objects that appear
1737 // after the new data size.
1738 size_t objectsSize = mObjectsSize;
1739 if (desired < mDataSize) {
1740 if (desired == 0) {
1741 objectsSize = 0;
1742 } else {
1743 while (objectsSize > 0) {
1744 if (mObjects[objectsSize-1] < desired)
1745 break;
1746 objectsSize--;
1747 }
1748 }
1749 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001750
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001751 if (mOwner) {
1752 // If the size is going to zero, just release the owner's data.
1753 if (desired == 0) {
1754 freeData();
1755 return NO_ERROR;
1756 }
1757
1758 // If there is a different owner, we need to take
1759 // posession.
1760 uint8_t* data = (uint8_t*)malloc(desired);
1761 if (!data) {
1762 mError = NO_MEMORY;
1763 return NO_MEMORY;
1764 }
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001765 binder_size_t* objects = NULL;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001766
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001767 if (objectsSize) {
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001768 objects = (binder_size_t*)malloc(objectsSize*sizeof(binder_size_t));
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001769 if (!objects) {
Hyejin Kim3f727c02013-03-09 11:28:54 +09001770 free(data);
1771
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001772 mError = NO_MEMORY;
1773 return NO_MEMORY;
1774 }
1775
1776 // Little hack to only acquire references on objects
1777 // we will be keeping.
1778 size_t oldObjectsSize = mObjectsSize;
1779 mObjectsSize = objectsSize;
1780 acquireObjects();
1781 mObjectsSize = oldObjectsSize;
1782 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001783
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001784 if (mData) {
1785 memcpy(data, mData, mDataSize < desired ? mDataSize : desired);
1786 }
1787 if (objects && mObjects) {
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001788 memcpy(objects, mObjects, objectsSize*sizeof(binder_size_t));
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001789 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001790 //ALOGI("Freeing data ref of %p (pid=%d)", this, getpid());
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001791 mOwner(this, mData, mDataSize, mObjects, mObjectsSize, mOwnerCookie);
1792 mOwner = NULL;
1793
Dianne Hackborn7e790af2014-11-11 12:22:53 -08001794 LOG_ALLOC("Parcel %p: taking ownership of %zu capacity", this, desired);
Dianne Hackborna4cff882014-11-13 17:07:40 -08001795 pthread_mutex_lock(&gParcelGlobalAllocSizeLock);
Dianne Hackborn7e790af2014-11-11 12:22:53 -08001796 gParcelGlobalAllocSize += desired;
1797 gParcelGlobalAllocCount++;
Dianne Hackborna4cff882014-11-13 17:07:40 -08001798 pthread_mutex_unlock(&gParcelGlobalAllocSizeLock);
Dianne Hackborn7e790af2014-11-11 12:22:53 -08001799
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001800 mData = data;
1801 mObjects = objects;
1802 mDataSize = (mDataSize < desired) ? mDataSize : desired;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001803 ALOGV("continueWrite Setting data size of %p to %zu", this, mDataSize);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001804 mDataCapacity = desired;
1805 mObjectsSize = mObjectsCapacity = objectsSize;
1806 mNextObjectHint = 0;
1807
1808 } else if (mData) {
1809 if (objectsSize < mObjectsSize) {
1810 // Need to release refs on any objects we are dropping.
1811 const sp<ProcessState> proc(ProcessState::self());
1812 for (size_t i=objectsSize; i<mObjectsSize; i++) {
1813 const flat_binder_object* flat
1814 = reinterpret_cast<flat_binder_object*>(mData+mObjects[i]);
1815 if (flat->type == BINDER_TYPE_FD) {
1816 // will need to rescan because we may have lopped off the only FDs
1817 mFdsKnown = false;
1818 }
1819 release_object(proc, *flat, this);
1820 }
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001821 binder_size_t* objects =
1822 (binder_size_t*)realloc(mObjects, objectsSize*sizeof(binder_size_t));
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001823 if (objects) {
1824 mObjects = objects;
1825 }
1826 mObjectsSize = objectsSize;
1827 mNextObjectHint = 0;
1828 }
1829
1830 // We own the data, so we can just do a realloc().
1831 if (desired > mDataCapacity) {
1832 uint8_t* data = (uint8_t*)realloc(mData, desired);
1833 if (data) {
Dianne Hackborn7e790af2014-11-11 12:22:53 -08001834 LOG_ALLOC("Parcel %p: continue from %zu to %zu capacity", this, mDataCapacity,
1835 desired);
Dianne Hackborna4cff882014-11-13 17:07:40 -08001836 pthread_mutex_lock(&gParcelGlobalAllocSizeLock);
Dianne Hackborn7e790af2014-11-11 12:22:53 -08001837 gParcelGlobalAllocSize += desired;
1838 gParcelGlobalAllocSize -= mDataCapacity;
Dianne Hackborna4cff882014-11-13 17:07:40 -08001839 pthread_mutex_unlock(&gParcelGlobalAllocSizeLock);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001840 mData = data;
1841 mDataCapacity = desired;
1842 } else if (desired > mDataCapacity) {
1843 mError = NO_MEMORY;
1844 return NO_MEMORY;
1845 }
1846 } else {
Dianne Hackborn97e2bcd2011-04-13 18:15:56 -07001847 if (mDataSize > desired) {
1848 mDataSize = desired;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001849 ALOGV("continueWrite Setting data size of %p to %zu", this, mDataSize);
Dianne Hackborn97e2bcd2011-04-13 18:15:56 -07001850 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001851 if (mDataPos > desired) {
1852 mDataPos = desired;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001853 ALOGV("continueWrite Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001854 }
1855 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001856
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001857 } else {
1858 // This is the first data. Easy!
1859 uint8_t* data = (uint8_t*)malloc(desired);
1860 if (!data) {
1861 mError = NO_MEMORY;
1862 return NO_MEMORY;
1863 }
Hyejin Kim3f727c02013-03-09 11:28:54 +09001864
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001865 if(!(mDataCapacity == 0 && mObjects == NULL
1866 && mObjectsCapacity == 0)) {
Colin Cross6f4f3ab2014-02-05 17:42:44 -08001867 ALOGE("continueWrite: %zu/%p/%zu/%zu", mDataCapacity, mObjects, mObjectsCapacity, desired);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001868 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001869
Dianne Hackborn7e790af2014-11-11 12:22:53 -08001870 LOG_ALLOC("Parcel %p: allocating with %zu capacity", this, desired);
Dianne Hackborna4cff882014-11-13 17:07:40 -08001871 pthread_mutex_lock(&gParcelGlobalAllocSizeLock);
Dianne Hackborn7e790af2014-11-11 12:22:53 -08001872 gParcelGlobalAllocSize += desired;
1873 gParcelGlobalAllocCount++;
Dianne Hackborna4cff882014-11-13 17:07:40 -08001874 pthread_mutex_unlock(&gParcelGlobalAllocSizeLock);
Dianne Hackborn7e790af2014-11-11 12:22:53 -08001875
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001876 mData = data;
1877 mDataSize = mDataPos = 0;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001878 ALOGV("continueWrite Setting data size of %p to %zu", this, mDataSize);
1879 ALOGV("continueWrite Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001880 mDataCapacity = desired;
1881 }
1882
1883 return NO_ERROR;
1884}
1885
1886void Parcel::initState()
1887{
Dianne Hackborn7e790af2014-11-11 12:22:53 -08001888 LOG_ALLOC("Parcel %p: initState", this);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001889 mError = NO_ERROR;
1890 mData = 0;
1891 mDataSize = 0;
1892 mDataCapacity = 0;
1893 mDataPos = 0;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001894 ALOGV("initState Setting data size of %p to %zu", this, mDataSize);
1895 ALOGV("initState Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001896 mObjects = NULL;
1897 mObjectsSize = 0;
1898 mObjectsCapacity = 0;
1899 mNextObjectHint = 0;
1900 mHasFds = false;
1901 mFdsKnown = true;
Dianne Hackborn8938ed22011-09-28 23:19:47 -04001902 mAllowFds = true;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001903 mOwner = NULL;
1904}
1905
1906void Parcel::scanForFds() const
1907{
1908 bool hasFds = false;
1909 for (size_t i=0; i<mObjectsSize; i++) {
1910 const flat_binder_object* flat
1911 = reinterpret_cast<const flat_binder_object*>(mData + mObjects[i]);
1912 if (flat->type == BINDER_TYPE_FD) {
1913 hasFds = true;
1914 break;
1915 }
1916 }
1917 mHasFds = hasFds;
1918 mFdsKnown = true;
1919}
1920
Jeff Brown5707dbf2011-09-23 21:17:56 -07001921// --- Parcel::Blob ---
1922
1923Parcel::Blob::Blob() :
1924 mMapped(false), mData(NULL), mSize(0) {
1925}
1926
1927Parcel::Blob::~Blob() {
1928 release();
1929}
1930
1931void Parcel::Blob::release() {
1932 if (mMapped && mData) {
1933 ::munmap(mData, mSize);
1934 }
1935 clear();
1936}
1937
1938void Parcel::Blob::init(bool mapped, void* data, size_t size) {
1939 mMapped = mapped;
1940 mData = data;
1941 mSize = size;
1942}
1943
1944void Parcel::Blob::clear() {
1945 mMapped = false;
1946 mData = NULL;
1947 mSize = 0;
1948}
1949
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001950}; // namespace android