blob: 61bdbc6aaa84c128bc6d78d1d66105aef9a15a19 [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>
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -070038
Michael Lentine36273c92014-10-02 09:11:04 -070039#include <fcntl.h>
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__)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -070052
53// ---------------------------------------------------------------------------
54
55#define PAD_SIZE(s) (((s)+3)&~3)
56
Brad Fitzpatricka877cd82010-07-07 16:06:39 -070057// Note: must be kept in sync with android/os/StrictMode.java's PENALTY_GATHER
58#define STRICT_MODE_PENALTY_GATHER 0x100
59
Brad Fitzpatrickd36f4a52010-07-12 11:05:38 -070060// Note: must be kept in sync with android/os/Parcel.java's EX_HAS_REPLY_HEADER
61#define EX_HAS_REPLY_HEADER -128
62
Jeff Brown5707dbf2011-09-23 21:17:56 -070063// Maximum size of a blob to transfer in-place.
64static const size_t IN_PLACE_BLOB_LIMIT = 40 * 1024;
65
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -070066// XXX This can be made public if we want to provide
67// support for typed data.
68struct small_flat_data
69{
70 uint32_t type;
71 uint32_t data;
72};
73
74namespace android {
75
76void acquire_object(const sp<ProcessState>& proc,
77 const flat_binder_object& obj, const void* who)
78{
79 switch (obj.type) {
80 case BINDER_TYPE_BINDER:
81 if (obj.binder) {
82 LOG_REFS("Parcel %p acquiring reference on local %p", who, obj.cookie);
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -080083 reinterpret_cast<IBinder*>(obj.cookie)->incStrong(who);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -070084 }
85 return;
86 case BINDER_TYPE_WEAK_BINDER:
87 if (obj.binder)
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -080088 reinterpret_cast<RefBase::weakref_type*>(obj.binder)->incWeak(who);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -070089 return;
90 case BINDER_TYPE_HANDLE: {
91 const sp<IBinder> b = proc->getStrongProxyForHandle(obj.handle);
92 if (b != NULL) {
93 LOG_REFS("Parcel %p acquiring reference on remote %p", who, b.get());
94 b->incStrong(who);
95 }
96 return;
97 }
98 case BINDER_TYPE_WEAK_HANDLE: {
99 const wp<IBinder> b = proc->getWeakProxyForHandle(obj.handle);
100 if (b != NULL) b.get_refs()->incWeak(who);
101 return;
102 }
103 case BINDER_TYPE_FD: {
104 // intentionally blank -- nothing to do to acquire this, but we do
105 // recognize it as a legitimate object type.
106 return;
107 }
108 }
109
Colin Cross6f4f3ab2014-02-05 17:42:44 -0800110 ALOGD("Invalid object type 0x%08x", obj.type);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700111}
112
113void release_object(const sp<ProcessState>& proc,
114 const flat_binder_object& obj, const void* who)
115{
116 switch (obj.type) {
117 case BINDER_TYPE_BINDER:
118 if (obj.binder) {
119 LOG_REFS("Parcel %p releasing reference on local %p", who, obj.cookie);
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800120 reinterpret_cast<IBinder*>(obj.cookie)->decStrong(who);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700121 }
122 return;
123 case BINDER_TYPE_WEAK_BINDER:
124 if (obj.binder)
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800125 reinterpret_cast<RefBase::weakref_type*>(obj.binder)->decWeak(who);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700126 return;
127 case BINDER_TYPE_HANDLE: {
128 const sp<IBinder> b = proc->getStrongProxyForHandle(obj.handle);
129 if (b != NULL) {
130 LOG_REFS("Parcel %p releasing reference on remote %p", who, b.get());
131 b->decStrong(who);
132 }
133 return;
134 }
135 case BINDER_TYPE_WEAK_HANDLE: {
136 const wp<IBinder> b = proc->getWeakProxyForHandle(obj.handle);
137 if (b != NULL) b.get_refs()->decWeak(who);
138 return;
139 }
140 case BINDER_TYPE_FD: {
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800141 if (obj.cookie != 0) close(obj.handle);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700142 return;
143 }
144 }
145
Colin Cross6f4f3ab2014-02-05 17:42:44 -0800146 ALOGE("Invalid object type 0x%08x", obj.type);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700147}
148
149inline static status_t finish_flatten_binder(
Colin Cross6f4f3ab2014-02-05 17:42:44 -0800150 const sp<IBinder>& /*binder*/, const flat_binder_object& flat, Parcel* out)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700151{
152 return out->writeObject(flat, false);
153}
154
Colin Cross6f4f3ab2014-02-05 17:42:44 -0800155status_t flatten_binder(const sp<ProcessState>& /*proc*/,
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700156 const sp<IBinder>& binder, Parcel* out)
157{
158 flat_binder_object obj;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700159
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700160 obj.flags = 0x7f | FLAT_BINDER_FLAG_ACCEPTS_FDS;
161 if (binder != NULL) {
162 IBinder *local = binder->localBinder();
163 if (!local) {
164 BpBinder *proxy = binder->remoteBinder();
165 if (proxy == NULL) {
Steve Blocke6f43dd2012-01-06 19:20:56 +0000166 ALOGE("null proxy");
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700167 }
168 const int32_t handle = proxy ? proxy->handle() : 0;
169 obj.type = BINDER_TYPE_HANDLE;
Arve Hjønnevåg07fd0f12014-02-18 21:10:29 -0800170 obj.binder = 0; /* Don't pass uninitialized stack data to a remote process */
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700171 obj.handle = handle;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800172 obj.cookie = 0;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700173 } else {
174 obj.type = BINDER_TYPE_BINDER;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800175 obj.binder = reinterpret_cast<uintptr_t>(local->getWeakRefs());
176 obj.cookie = reinterpret_cast<uintptr_t>(local);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700177 }
178 } else {
179 obj.type = BINDER_TYPE_BINDER;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800180 obj.binder = 0;
181 obj.cookie = 0;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700182 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700183
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700184 return finish_flatten_binder(binder, obj, out);
185}
186
Colin Cross6f4f3ab2014-02-05 17:42:44 -0800187status_t flatten_binder(const sp<ProcessState>& /*proc*/,
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700188 const wp<IBinder>& binder, Parcel* out)
189{
190 flat_binder_object obj;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700191
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700192 obj.flags = 0x7f | FLAT_BINDER_FLAG_ACCEPTS_FDS;
193 if (binder != NULL) {
194 sp<IBinder> real = binder.promote();
195 if (real != NULL) {
196 IBinder *local = real->localBinder();
197 if (!local) {
198 BpBinder *proxy = real->remoteBinder();
199 if (proxy == NULL) {
Steve Blocke6f43dd2012-01-06 19:20:56 +0000200 ALOGE("null proxy");
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700201 }
202 const int32_t handle = proxy ? proxy->handle() : 0;
203 obj.type = BINDER_TYPE_WEAK_HANDLE;
Arve Hjønnevåg07fd0f12014-02-18 21:10:29 -0800204 obj.binder = 0; /* Don't pass uninitialized stack data to a remote process */
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700205 obj.handle = handle;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800206 obj.cookie = 0;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700207 } else {
208 obj.type = BINDER_TYPE_WEAK_BINDER;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800209 obj.binder = reinterpret_cast<uintptr_t>(binder.get_refs());
210 obj.cookie = reinterpret_cast<uintptr_t>(binder.unsafe_get());
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700211 }
212 return finish_flatten_binder(real, obj, out);
213 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700214
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700215 // XXX How to deal? In order to flatten the given binder,
216 // we need to probe it for information, which requires a primary
217 // reference... but we don't have one.
218 //
219 // The OpenBinder implementation uses a dynamic_cast<> here,
220 // but we can't do that with the different reference counting
221 // implementation we are using.
Steve Blocke6f43dd2012-01-06 19:20:56 +0000222 ALOGE("Unable to unflatten Binder weak reference!");
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700223 obj.type = BINDER_TYPE_BINDER;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800224 obj.binder = 0;
225 obj.cookie = 0;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700226 return finish_flatten_binder(NULL, obj, out);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700227
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700228 } else {
229 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);
233 }
234}
235
236inline static status_t finish_unflatten_binder(
Colin Cross6f4f3ab2014-02-05 17:42:44 -0800237 BpBinder* /*proxy*/, const flat_binder_object& /*flat*/,
238 const Parcel& /*in*/)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700239{
240 return NO_ERROR;
241}
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700242
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700243status_t unflatten_binder(const sp<ProcessState>& proc,
244 const Parcel& in, sp<IBinder>* out)
245{
246 const flat_binder_object* flat = in.readObject(false);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700247
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700248 if (flat) {
249 switch (flat->type) {
250 case BINDER_TYPE_BINDER:
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800251 *out = reinterpret_cast<IBinder*>(flat->cookie);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700252 return finish_unflatten_binder(NULL, *flat, in);
253 case BINDER_TYPE_HANDLE:
254 *out = proc->getStrongProxyForHandle(flat->handle);
255 return finish_unflatten_binder(
256 static_cast<BpBinder*>(out->get()), *flat, in);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700257 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700258 }
259 return BAD_TYPE;
260}
261
262status_t unflatten_binder(const sp<ProcessState>& proc,
263 const Parcel& in, wp<IBinder>* out)
264{
265 const flat_binder_object* flat = in.readObject(false);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700266
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700267 if (flat) {
268 switch (flat->type) {
269 case BINDER_TYPE_BINDER:
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800270 *out = reinterpret_cast<IBinder*>(flat->cookie);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700271 return finish_unflatten_binder(NULL, *flat, in);
272 case BINDER_TYPE_WEAK_BINDER:
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800273 if (flat->binder != 0) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700274 out->set_object_and_refs(
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800275 reinterpret_cast<IBinder*>(flat->cookie),
276 reinterpret_cast<RefBase::weakref_type*>(flat->binder));
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700277 } else {
278 *out = NULL;
279 }
280 return finish_unflatten_binder(NULL, *flat, in);
281 case BINDER_TYPE_HANDLE:
282 case BINDER_TYPE_WEAK_HANDLE:
283 *out = proc->getWeakProxyForHandle(flat->handle);
284 return finish_unflatten_binder(
285 static_cast<BpBinder*>(out->unsafe_get()), *flat, in);
286 }
287 }
288 return BAD_TYPE;
289}
290
291// ---------------------------------------------------------------------------
292
293Parcel::Parcel()
294{
295 initState();
296}
297
298Parcel::~Parcel()
299{
300 freeDataNoInit();
301}
302
303const uint8_t* Parcel::data() const
304{
305 return mData;
306}
307
308size_t Parcel::dataSize() const
309{
310 return (mDataSize > mDataPos ? mDataSize : mDataPos);
311}
312
313size_t Parcel::dataAvail() const
314{
315 // TODO: decide what to do about the possibility that this can
316 // report an available-data size that exceeds a Java int's max
317 // positive value, causing havoc. Fortunately this will only
318 // happen if someone constructs a Parcel containing more than two
319 // gigabytes of data, which on typical phone hardware is simply
320 // not possible.
321 return dataSize() - dataPosition();
322}
323
324size_t Parcel::dataPosition() const
325{
326 return mDataPos;
327}
328
329size_t Parcel::dataCapacity() const
330{
331 return mDataCapacity;
332}
333
334status_t Parcel::setDataSize(size_t size)
335{
336 status_t err;
337 err = continueWrite(size);
338 if (err == NO_ERROR) {
339 mDataSize = size;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700340 ALOGV("setDataSize Setting data size of %p to %zu", this, mDataSize);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700341 }
342 return err;
343}
344
345void Parcel::setDataPosition(size_t pos) const
346{
347 mDataPos = pos;
348 mNextObjectHint = 0;
349}
350
351status_t Parcel::setDataCapacity(size_t size)
352{
Dianne Hackborn97e2bcd2011-04-13 18:15:56 -0700353 if (size > mDataCapacity) return continueWrite(size);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700354 return NO_ERROR;
355}
356
357status_t Parcel::setData(const uint8_t* buffer, size_t len)
358{
359 status_t err = restartWrite(len);
360 if (err == NO_ERROR) {
361 memcpy(const_cast<uint8_t*>(data()), buffer, len);
362 mDataSize = len;
363 mFdsKnown = false;
364 }
365 return err;
366}
367
Andreas Huber51faf462011-04-13 10:21:56 -0700368status_t Parcel::appendFrom(const Parcel *parcel, size_t offset, size_t len)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700369{
370 const sp<ProcessState> proc(ProcessState::self());
371 status_t err;
Andreas Huber51faf462011-04-13 10:21:56 -0700372 const uint8_t *data = parcel->mData;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800373 const binder_size_t *objects = parcel->mObjects;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700374 size_t size = parcel->mObjectsSize;
375 int startPos = mDataPos;
376 int firstIndex = -1, lastIndex = -2;
377
378 if (len == 0) {
379 return NO_ERROR;
380 }
381
382 // range checks against the source parcel size
383 if ((offset > parcel->mDataSize)
384 || (len > parcel->mDataSize)
385 || (offset + len > parcel->mDataSize)) {
386 return BAD_VALUE;
387 }
388
389 // Count objects in range
390 for (int i = 0; i < (int) size; i++) {
391 size_t off = objects[i];
392 if ((off >= offset) && (off < offset + len)) {
393 if (firstIndex == -1) {
394 firstIndex = i;
395 }
396 lastIndex = i;
397 }
398 }
399 int numObjects = lastIndex - firstIndex + 1;
400
Dianne Hackborn97e2bcd2011-04-13 18:15:56 -0700401 if ((mDataSize+len) > mDataCapacity) {
402 // grow data
403 err = growData(len);
404 if (err != NO_ERROR) {
405 return err;
406 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700407 }
408
409 // append data
410 memcpy(mData + mDataPos, data + offset, len);
411 mDataPos += len;
412 mDataSize += len;
413
Dianne Hackborn8938ed22011-09-28 23:19:47 -0400414 err = NO_ERROR;
415
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700416 if (numObjects > 0) {
417 // grow objects
418 if (mObjectsCapacity < mObjectsSize + numObjects) {
419 int newSize = ((mObjectsSize + numObjects)*3)/2;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800420 binder_size_t *objects =
421 (binder_size_t*)realloc(mObjects, newSize*sizeof(binder_size_t));
422 if (objects == (binder_size_t*)0) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700423 return NO_MEMORY;
424 }
425 mObjects = objects;
426 mObjectsCapacity = newSize;
427 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700428
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700429 // append and acquire objects
430 int idx = mObjectsSize;
431 for (int i = firstIndex; i <= lastIndex; i++) {
432 size_t off = objects[i] - offset + startPos;
433 mObjects[idx++] = off;
434 mObjectsSize++;
435
Dianne Hackborn8af0f822009-05-22 13:20:23 -0700436 flat_binder_object* flat
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700437 = reinterpret_cast<flat_binder_object*>(mData + off);
438 acquire_object(proc, *flat, this);
439
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700440 if (flat->type == BINDER_TYPE_FD) {
Dianne Hackborn8af0f822009-05-22 13:20:23 -0700441 // If this is a file descriptor, we need to dup it so the
442 // new Parcel now owns its own fd, and can declare that we
443 // officially know we have fds.
444 flat->handle = dup(flat->handle);
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800445 flat->cookie = 1;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700446 mHasFds = mFdsKnown = true;
Dianne Hackborn8938ed22011-09-28 23:19:47 -0400447 if (!mAllowFds) {
448 err = FDS_NOT_ALLOWED;
449 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700450 }
451 }
452 }
453
Dianne Hackborn8938ed22011-09-28 23:19:47 -0400454 return err;
455}
456
Dianne Hackborn7746cc32011-10-03 21:09:35 -0700457bool Parcel::pushAllowFds(bool allowFds)
Dianne Hackborn8938ed22011-09-28 23:19:47 -0400458{
459 const bool origValue = mAllowFds;
Dianne Hackborn7746cc32011-10-03 21:09:35 -0700460 if (!allowFds) {
461 mAllowFds = false;
462 }
Dianne Hackborn8938ed22011-09-28 23:19:47 -0400463 return origValue;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700464}
465
Dianne Hackborn7746cc32011-10-03 21:09:35 -0700466void Parcel::restoreAllowFds(bool lastValue)
467{
468 mAllowFds = lastValue;
469}
470
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700471bool Parcel::hasFileDescriptors() const
472{
473 if (!mFdsKnown) {
474 scanForFds();
475 }
476 return mHasFds;
477}
478
Brad Fitzpatrick702ea9d2010-06-18 13:07:53 -0700479// Write RPC headers. (previously just the interface token)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700480status_t Parcel::writeInterfaceToken(const String16& interface)
481{
Brad Fitzpatricka877cd82010-07-07 16:06:39 -0700482 writeInt32(IPCThreadState::self()->getStrictModePolicy() |
483 STRICT_MODE_PENALTY_GATHER);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700484 // currently the interface identification token is just its name as a string
485 return writeString16(interface);
486}
487
Mathias Agopian83c04462009-05-22 19:00:22 -0700488bool Parcel::checkInterface(IBinder* binder) const
489{
Brad Fitzpatrick702ea9d2010-06-18 13:07:53 -0700490 return enforceInterface(binder->getInterfaceDescriptor());
Mathias Agopian83c04462009-05-22 19:00:22 -0700491}
492
Brad Fitzpatricka877cd82010-07-07 16:06:39 -0700493bool Parcel::enforceInterface(const String16& interface,
Brad Fitzpatrick70081a12010-07-27 09:49:11 -0700494 IPCThreadState* threadState) const
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700495{
Brad Fitzpatrick70081a12010-07-27 09:49:11 -0700496 int32_t strictPolicy = readInt32();
497 if (threadState == NULL) {
498 threadState = IPCThreadState::self();
Brad Fitzpatricka877cd82010-07-07 16:06:39 -0700499 }
Brad Fitzpatrick52736032010-08-30 16:01:16 -0700500 if ((threadState->getLastTransactionBinderFlags() &
501 IBinder::FLAG_ONEWAY) != 0) {
502 // For one-way calls, the callee is running entirely
503 // disconnected from the caller, so disable StrictMode entirely.
504 // Not only does disk/network usage not impact the caller, but
505 // there's no way to commuicate back any violations anyway.
506 threadState->setStrictModePolicy(0);
507 } else {
508 threadState->setStrictModePolicy(strictPolicy);
509 }
Mathias Agopian83c04462009-05-22 19:00:22 -0700510 const String16 str(readString16());
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700511 if (str == interface) {
512 return true;
513 } else {
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700514 ALOGW("**** enforceInterface() expected '%s' but read '%s'",
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700515 String8(interface).string(), String8(str).string());
516 return false;
517 }
Brad Fitzpatrick702ea9d2010-06-18 13:07:53 -0700518}
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700519
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800520const binder_size_t* Parcel::objects() const
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700521{
522 return mObjects;
523}
524
525size_t Parcel::objectsCount() const
526{
527 return mObjectsSize;
528}
529
530status_t Parcel::errorCheck() const
531{
532 return mError;
533}
534
535void Parcel::setError(status_t err)
536{
537 mError = err;
538}
539
540status_t Parcel::finishWrite(size_t len)
541{
542 //printf("Finish write of %d\n", len);
543 mDataPos += len;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700544 ALOGV("finishWrite Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700545 if (mDataPos > mDataSize) {
546 mDataSize = mDataPos;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700547 ALOGV("finishWrite Setting data size of %p to %zu", this, mDataSize);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700548 }
549 //printf("New pos=%d, size=%d\n", mDataPos, mDataSize);
550 return NO_ERROR;
551}
552
553status_t Parcel::writeUnpadded(const void* data, size_t len)
554{
555 size_t end = mDataPos + len;
556 if (end < mDataPos) {
557 // integer overflow
558 return BAD_VALUE;
559 }
560
561 if (end <= mDataCapacity) {
562restart_write:
563 memcpy(mData+mDataPos, data, len);
564 return finishWrite(len);
565 }
566
567 status_t err = growData(len);
568 if (err == NO_ERROR) goto restart_write;
569 return err;
570}
571
572status_t Parcel::write(const void* data, size_t len)
573{
574 void* const d = writeInplace(len);
575 if (d) {
576 memcpy(d, data, len);
577 return NO_ERROR;
578 }
579 return mError;
580}
581
582void* Parcel::writeInplace(size_t len)
583{
584 const size_t padded = PAD_SIZE(len);
585
586 // sanity check for integer overflow
587 if (mDataPos+padded < mDataPos) {
588 return NULL;
589 }
590
591 if ((mDataPos+padded) <= mDataCapacity) {
592restart_write:
593 //printf("Writing %ld bytes, padded to %ld\n", len, padded);
594 uint8_t* const data = mData+mDataPos;
595
596 // Need to pad at end?
597 if (padded != len) {
598#if BYTE_ORDER == BIG_ENDIAN
599 static const uint32_t mask[4] = {
600 0x00000000, 0xffffff00, 0xffff0000, 0xff000000
601 };
602#endif
603#if BYTE_ORDER == LITTLE_ENDIAN
604 static const uint32_t mask[4] = {
605 0x00000000, 0x00ffffff, 0x0000ffff, 0x000000ff
606 };
607#endif
608 //printf("Applying pad mask: %p to %p\n", (void*)mask[padded-len],
609 // *reinterpret_cast<void**>(data+padded-4));
610 *reinterpret_cast<uint32_t*>(data+padded-4) &= mask[padded-len];
611 }
612
613 finishWrite(padded);
614 return data;
615 }
616
617 status_t err = growData(padded);
618 if (err == NO_ERROR) goto restart_write;
619 return NULL;
620}
621
622status_t Parcel::writeInt32(int32_t val)
623{
Andreas Huber84a6d042009-08-17 13:33:27 -0700624 return writeAligned(val);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700625}
Marco Nelissen5c0106e2013-10-16 10:57:51 -0700626status_t Parcel::writeInt32Array(size_t len, const int32_t *val) {
627 if (!val) {
628 return writeAligned(-1);
629 }
630 status_t ret = writeAligned(len);
631 if (ret == NO_ERROR) {
632 ret = write(val, len * sizeof(*val));
633 }
634 return ret;
635}
Marco Nelissenf0190bf2014-03-13 14:17:40 -0700636status_t Parcel::writeByteArray(size_t len, const uint8_t *val) {
637 if (!val) {
638 return writeAligned(-1);
639 }
640 status_t ret = writeAligned(len);
641 if (ret == NO_ERROR) {
642 ret = write(val, len * sizeof(*val));
643 }
644 return ret;
645}
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700646
647status_t Parcel::writeInt64(int64_t val)
648{
Andreas Huber84a6d042009-08-17 13:33:27 -0700649 return writeAligned(val);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700650}
651
Serban Constantinescuf683e012013-11-05 16:53:55 +0000652status_t Parcel::writePointer(uintptr_t val)
653{
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800654 return writeAligned<binder_uintptr_t>(val);
Serban Constantinescuf683e012013-11-05 16:53:55 +0000655}
656
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700657status_t Parcel::writeFloat(float val)
658{
Andreas Huber84a6d042009-08-17 13:33:27 -0700659 return writeAligned(val);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700660}
661
Douglas Leungcc1a4bb2013-01-11 15:00:55 -0800662#if defined(__mips__) && defined(__mips_hard_float)
663
664status_t Parcel::writeDouble(double val)
665{
666 union {
667 double d;
668 unsigned long long ll;
669 } u;
670 u.d = val;
671 return writeAligned(u.ll);
672}
673
674#else
675
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700676status_t Parcel::writeDouble(double val)
677{
Andreas Huber84a6d042009-08-17 13:33:27 -0700678 return writeAligned(val);
679}
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700680
Douglas Leungcc1a4bb2013-01-11 15:00:55 -0800681#endif
682
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700683status_t Parcel::writeCString(const char* str)
684{
685 return write(str, strlen(str)+1);
686}
687
688status_t Parcel::writeString8(const String8& str)
689{
690 status_t err = writeInt32(str.bytes());
Pravat Dalbeherad1dff8d2010-12-15 08:40:00 +0100691 // only write string if its length is more than zero characters,
692 // as readString8 will only read if the length field is non-zero.
693 // this is slightly different from how writeString16 works.
694 if (str.bytes() > 0 && err == NO_ERROR) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700695 err = write(str.string(), str.bytes()+1);
696 }
697 return err;
698}
699
700status_t Parcel::writeString16(const String16& str)
701{
702 return writeString16(str.string(), str.size());
703}
704
705status_t Parcel::writeString16(const char16_t* str, size_t len)
706{
707 if (str == NULL) return writeInt32(-1);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700708
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700709 status_t err = writeInt32(len);
710 if (err == NO_ERROR) {
711 len *= sizeof(char16_t);
712 uint8_t* data = (uint8_t*)writeInplace(len+sizeof(char16_t));
713 if (data) {
714 memcpy(data, str, len);
715 *reinterpret_cast<char16_t*>(data+len) = 0;
716 return NO_ERROR;
717 }
718 err = mError;
719 }
720 return err;
721}
722
723status_t Parcel::writeStrongBinder(const sp<IBinder>& val)
724{
725 return flatten_binder(ProcessState::self(), val, this);
726}
727
728status_t Parcel::writeWeakBinder(const wp<IBinder>& val)
729{
730 return flatten_binder(ProcessState::self(), val, this);
731}
732
Mathias Agopiana47f02a2009-05-21 16:29:38 -0700733status_t Parcel::writeNativeHandle(const native_handle* handle)
The Android Open Source Project5f78a482009-01-20 14:03:58 -0800734{
Mathias Agopian1d0a95b2009-07-31 16:12:13 -0700735 if (!handle || handle->version != sizeof(native_handle))
The Android Open Source Project5f78a482009-01-20 14:03:58 -0800736 return BAD_TYPE;
737
738 status_t err;
Mathias Agopiana47f02a2009-05-21 16:29:38 -0700739 err = writeInt32(handle->numFds);
The Android Open Source Project5f78a482009-01-20 14:03:58 -0800740 if (err != NO_ERROR) return err;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800741
Mathias Agopiana47f02a2009-05-21 16:29:38 -0700742 err = writeInt32(handle->numInts);
The Android Open Source Project5f78a482009-01-20 14:03:58 -0800743 if (err != NO_ERROR) return err;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800744
Mathias Agopiana47f02a2009-05-21 16:29:38 -0700745 for (int i=0 ; err==NO_ERROR && i<handle->numFds ; i++)
746 err = writeDupFileDescriptor(handle->data[i]);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800747
748 if (err != NO_ERROR) {
Steve Block9d453682011-12-20 16:23:08 +0000749 ALOGD("write native handle, write dup fd failed");
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800750 return err;
751 }
Mathias Agopiana47f02a2009-05-21 16:29:38 -0700752 err = write(handle->data + handle->numFds, sizeof(int)*handle->numInts);
The Android Open Source Project5f78a482009-01-20 14:03:58 -0800753 return err;
754}
755
Jeff Brown93ff1f92011-11-04 19:01:44 -0700756status_t Parcel::writeFileDescriptor(int fd, bool takeOwnership)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700757{
758 flat_binder_object obj;
759 obj.type = BINDER_TYPE_FD;
760 obj.flags = 0x7f | FLAT_BINDER_FLAG_ACCEPTS_FDS;
Arve Hjønnevåg07fd0f12014-02-18 21:10:29 -0800761 obj.binder = 0; /* Don't pass uninitialized stack data to a remote process */
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700762 obj.handle = fd;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800763 obj.cookie = takeOwnership ? 1 : 0;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700764 return writeObject(obj, true);
765}
766
767status_t Parcel::writeDupFileDescriptor(int fd)
768{
Jeff Brownd341c712011-11-04 20:19:33 -0700769 int dupFd = dup(fd);
770 if (dupFd < 0) {
771 return -errno;
772 }
773 status_t err = writeFileDescriptor(dupFd, true /*takeOwnership*/);
774 if (err) {
775 close(dupFd);
776 }
777 return err;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700778}
779
Mike Lockwoodcbe36fe2013-09-05 08:41:06 -0700780// WARNING: This method must stay in sync with
781// Parcelable.Creator<ParcelFileDescriptor> CREATOR
782// in frameworks/base/core/java/android/os/ParcelFileDescriptor.java
783status_t Parcel::writeParcelFileDescriptor(int fd, int commChannel) {
784 status_t status;
785
786 if (fd < 0) {
787 status = writeInt32(0); // ParcelFileDescriptor is null
788 if (status) return status;
789 } else {
790 status = writeInt32(1); // ParcelFileDescriptor is not null
791 if (status) return status;
792 status = writeDupFileDescriptor(fd);
793 if (status) return status;
794 if (commChannel < 0) {
795 status = writeInt32(0); // commChannel is null
796 if (status) return status;
797 } else {
798 status = writeInt32(1); // commChannel is not null
799 if (status) return status;
800 status = writeDupFileDescriptor(commChannel);
801 }
802 }
803 return status;
804}
805
Jeff Brown5707dbf2011-09-23 21:17:56 -0700806status_t Parcel::writeBlob(size_t len, WritableBlob* outBlob)
807{
808 status_t status;
809
810 if (!mAllowFds || len <= IN_PLACE_BLOB_LIMIT) {
Steve Block6807e592011-10-20 11:56:00 +0100811 ALOGV("writeBlob: write in place");
Jeff Brown5707dbf2011-09-23 21:17:56 -0700812 status = writeInt32(0);
813 if (status) return status;
814
815 void* ptr = writeInplace(len);
816 if (!ptr) return NO_MEMORY;
817
818 outBlob->init(false /*mapped*/, ptr, len);
819 return NO_ERROR;
820 }
821
Steve Block6807e592011-10-20 11:56:00 +0100822 ALOGV("writeBlob: write to ashmem");
Jeff Brown5707dbf2011-09-23 21:17:56 -0700823 int fd = ashmem_create_region("Parcel Blob", len);
824 if (fd < 0) return NO_MEMORY;
825
826 int result = ashmem_set_prot_region(fd, PROT_READ | PROT_WRITE);
827 if (result < 0) {
Jeff Brownec4e0062011-10-10 14:50:10 -0700828 status = result;
Jeff Brown5707dbf2011-09-23 21:17:56 -0700829 } else {
830 void* ptr = ::mmap(NULL, len, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
831 if (ptr == MAP_FAILED) {
832 status = -errno;
833 } else {
834 result = ashmem_set_prot_region(fd, PROT_READ);
835 if (result < 0) {
Jeff Brownec4e0062011-10-10 14:50:10 -0700836 status = result;
Jeff Brown5707dbf2011-09-23 21:17:56 -0700837 } else {
838 status = writeInt32(1);
839 if (!status) {
Jeff Brown93ff1f92011-11-04 19:01:44 -0700840 status = writeFileDescriptor(fd, true /*takeOwnership*/);
Jeff Brown5707dbf2011-09-23 21:17:56 -0700841 if (!status) {
842 outBlob->init(true /*mapped*/, ptr, len);
843 return NO_ERROR;
844 }
845 }
846 }
847 }
848 ::munmap(ptr, len);
849 }
850 ::close(fd);
851 return status;
852}
853
Mathias Agopiane1424282013-07-29 21:24:40 -0700854status_t Parcel::write(const FlattenableHelperInterface& val)
Mathias Agopian98e71dd2010-02-11 17:30:52 -0800855{
856 status_t err;
857
858 // size if needed
Mathias Agopiane1424282013-07-29 21:24:40 -0700859 const size_t len = val.getFlattenedSize();
860 const size_t fd_count = val.getFdCount();
Mathias Agopian98e71dd2010-02-11 17:30:52 -0800861
862 err = this->writeInt32(len);
863 if (err) return err;
864
865 err = this->writeInt32(fd_count);
866 if (err) return err;
867
868 // payload
Mathias Agopiane1424282013-07-29 21:24:40 -0700869 void* const buf = this->writeInplace(PAD_SIZE(len));
Mathias Agopian98e71dd2010-02-11 17:30:52 -0800870 if (buf == NULL)
871 return BAD_VALUE;
872
873 int* fds = NULL;
874 if (fd_count) {
875 fds = new int[fd_count];
876 }
877
878 err = val.flatten(buf, len, fds, fd_count);
879 for (size_t i=0 ; i<fd_count && err==NO_ERROR ; i++) {
880 err = this->writeDupFileDescriptor( fds[i] );
881 }
882
883 if (fd_count) {
884 delete [] fds;
885 }
886
887 return err;
888}
889
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700890status_t Parcel::writeObject(const flat_binder_object& val, bool nullMetaData)
891{
892 const bool enoughData = (mDataPos+sizeof(val)) <= mDataCapacity;
893 const bool enoughObjects = mObjectsSize < mObjectsCapacity;
894 if (enoughData && enoughObjects) {
895restart_write:
896 *reinterpret_cast<flat_binder_object*>(mData+mDataPos) = val;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700897
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700898 // Need to write meta-data?
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800899 if (nullMetaData || val.binder != 0) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700900 mObjects[mObjectsSize] = mDataPos;
901 acquire_object(ProcessState::self(), val, this);
902 mObjectsSize++;
903 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700904
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700905 // remember if it's a file descriptor
906 if (val.type == BINDER_TYPE_FD) {
Dianne Hackborn8938ed22011-09-28 23:19:47 -0400907 if (!mAllowFds) {
908 return FDS_NOT_ALLOWED;
909 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700910 mHasFds = mFdsKnown = true;
911 }
912
913 return finishWrite(sizeof(flat_binder_object));
914 }
915
916 if (!enoughData) {
917 const status_t err = growData(sizeof(val));
918 if (err != NO_ERROR) return err;
919 }
920 if (!enoughObjects) {
921 size_t newSize = ((mObjectsSize+2)*3)/2;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800922 binder_size_t* objects = (binder_size_t*)realloc(mObjects, newSize*sizeof(binder_size_t));
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700923 if (objects == NULL) return NO_MEMORY;
924 mObjects = objects;
925 mObjectsCapacity = newSize;
926 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700927
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700928 goto restart_write;
929}
930
Brad Fitzpatrick837a0d02010-07-13 15:33:35 -0700931status_t Parcel::writeNoException()
932{
933 return writeInt32(0);
934}
935
Colin Cross6f4f3ab2014-02-05 17:42:44 -0800936void Parcel::remove(size_t /*start*/, size_t /*amt*/)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700937{
938 LOG_ALWAYS_FATAL("Parcel::remove() not yet implemented!");
939}
940
941status_t Parcel::read(void* outData, size_t len) const
942{
Kenny Root5b61ad22014-03-17 13:18:16 -0700943 if ((mDataPos+PAD_SIZE(len)) >= mDataPos && (mDataPos+PAD_SIZE(len)) <= mDataSize
944 && len <= PAD_SIZE(len)) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700945 memcpy(outData, mData+mDataPos, len);
946 mDataPos += PAD_SIZE(len);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700947 ALOGV("read Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700948 return NO_ERROR;
949 }
950 return NOT_ENOUGH_DATA;
951}
952
953const void* Parcel::readInplace(size_t len) const
954{
Kenny Root5b61ad22014-03-17 13:18:16 -0700955 if ((mDataPos+PAD_SIZE(len)) >= mDataPos && (mDataPos+PAD_SIZE(len)) <= mDataSize
956 && len <= PAD_SIZE(len)) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700957 const void* data = mData+mDataPos;
958 mDataPos += PAD_SIZE(len);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700959 ALOGV("readInplace Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700960 return data;
961 }
962 return NULL;
963}
964
Andreas Huber84a6d042009-08-17 13:33:27 -0700965template<class T>
966status_t Parcel::readAligned(T *pArg) const {
967 COMPILE_TIME_ASSERT_FUNCTION_SCOPE(PAD_SIZE(sizeof(T)) == sizeof(T));
968
969 if ((mDataPos+sizeof(T)) <= mDataSize) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700970 const void* data = mData+mDataPos;
Andreas Huber84a6d042009-08-17 13:33:27 -0700971 mDataPos += sizeof(T);
972 *pArg = *reinterpret_cast<const T*>(data);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700973 return NO_ERROR;
974 } else {
975 return NOT_ENOUGH_DATA;
976 }
977}
978
Andreas Huber84a6d042009-08-17 13:33:27 -0700979template<class T>
980T Parcel::readAligned() const {
981 T result;
982 if (readAligned(&result) != NO_ERROR) {
983 result = 0;
984 }
985
986 return result;
987}
988
989template<class T>
990status_t Parcel::writeAligned(T val) {
991 COMPILE_TIME_ASSERT_FUNCTION_SCOPE(PAD_SIZE(sizeof(T)) == sizeof(T));
992
993 if ((mDataPos+sizeof(val)) <= mDataCapacity) {
994restart_write:
995 *reinterpret_cast<T*>(mData+mDataPos) = val;
996 return finishWrite(sizeof(val));
997 }
998
999 status_t err = growData(sizeof(val));
1000 if (err == NO_ERROR) goto restart_write;
1001 return err;
1002}
1003
1004status_t Parcel::readInt32(int32_t *pArg) const
1005{
1006 return readAligned(pArg);
1007}
1008
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001009int32_t Parcel::readInt32() const
1010{
Andreas Huber84a6d042009-08-17 13:33:27 -07001011 return readAligned<int32_t>();
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001012}
1013
1014
1015status_t Parcel::readInt64(int64_t *pArg) const
1016{
Andreas Huber84a6d042009-08-17 13:33:27 -07001017 return readAligned(pArg);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001018}
1019
1020
1021int64_t Parcel::readInt64() const
1022{
Andreas Huber84a6d042009-08-17 13:33:27 -07001023 return readAligned<int64_t>();
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001024}
1025
Serban Constantinescuf683e012013-11-05 16:53:55 +00001026status_t Parcel::readPointer(uintptr_t *pArg) const
1027{
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001028 status_t ret;
1029 binder_uintptr_t ptr;
1030 ret = readAligned(&ptr);
1031 if (!ret)
1032 *pArg = ptr;
1033 return ret;
Serban Constantinescuf683e012013-11-05 16:53:55 +00001034}
1035
1036uintptr_t Parcel::readPointer() const
1037{
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001038 return readAligned<binder_uintptr_t>();
Serban Constantinescuf683e012013-11-05 16:53:55 +00001039}
1040
1041
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001042status_t Parcel::readFloat(float *pArg) const
1043{
Andreas Huber84a6d042009-08-17 13:33:27 -07001044 return readAligned(pArg);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001045}
1046
1047
1048float Parcel::readFloat() const
1049{
Andreas Huber84a6d042009-08-17 13:33:27 -07001050 return readAligned<float>();
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001051}
1052
Douglas Leungcc1a4bb2013-01-11 15:00:55 -08001053#if defined(__mips__) && defined(__mips_hard_float)
1054
1055status_t Parcel::readDouble(double *pArg) const
1056{
1057 union {
1058 double d;
1059 unsigned long long ll;
1060 } u;
Narayan Kamath2c68d382014-06-04 15:04:29 +01001061 u.d = 0;
Douglas Leungcc1a4bb2013-01-11 15:00:55 -08001062 status_t status;
1063 status = readAligned(&u.ll);
1064 *pArg = u.d;
1065 return status;
1066}
1067
1068double Parcel::readDouble() const
1069{
1070 union {
1071 double d;
1072 unsigned long long ll;
1073 } u;
1074 u.ll = readAligned<unsigned long long>();
1075 return u.d;
1076}
1077
1078#else
1079
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001080status_t Parcel::readDouble(double *pArg) const
1081{
Andreas Huber84a6d042009-08-17 13:33:27 -07001082 return readAligned(pArg);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001083}
1084
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001085double Parcel::readDouble() const
1086{
Andreas Huber84a6d042009-08-17 13:33:27 -07001087 return readAligned<double>();
1088}
1089
Douglas Leungcc1a4bb2013-01-11 15:00:55 -08001090#endif
1091
Andreas Huber84a6d042009-08-17 13:33:27 -07001092status_t Parcel::readIntPtr(intptr_t *pArg) const
1093{
1094 return readAligned(pArg);
1095}
1096
1097
1098intptr_t Parcel::readIntPtr() const
1099{
1100 return readAligned<intptr_t>();
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001101}
1102
1103
1104const char* Parcel::readCString() const
1105{
1106 const size_t avail = mDataSize-mDataPos;
1107 if (avail > 0) {
1108 const char* str = reinterpret_cast<const char*>(mData+mDataPos);
1109 // is the string's trailing NUL within the parcel's valid bounds?
1110 const char* eos = reinterpret_cast<const char*>(memchr(str, 0, avail));
1111 if (eos) {
1112 const size_t len = eos - str;
1113 mDataPos += PAD_SIZE(len+1);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001114 ALOGV("readCString Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001115 return str;
1116 }
1117 }
1118 return NULL;
1119}
1120
1121String8 Parcel::readString8() const
1122{
1123 int32_t size = readInt32();
1124 // watch for potential int overflow adding 1 for trailing NUL
1125 if (size > 0 && size < INT32_MAX) {
1126 const char* str = (const char*)readInplace(size+1);
1127 if (str) return String8(str, size);
1128 }
1129 return String8();
1130}
1131
1132String16 Parcel::readString16() const
1133{
1134 size_t len;
1135 const char16_t* str = readString16Inplace(&len);
1136 if (str) return String16(str, len);
Steve Blocke6f43dd2012-01-06 19:20:56 +00001137 ALOGE("Reading a NULL string not supported here.");
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001138 return String16();
1139}
1140
1141const char16_t* Parcel::readString16Inplace(size_t* outLen) const
1142{
1143 int32_t size = readInt32();
1144 // watch for potential int overflow from size+1
1145 if (size >= 0 && size < INT32_MAX) {
1146 *outLen = size;
1147 const char16_t* str = (const char16_t*)readInplace((size+1)*sizeof(char16_t));
1148 if (str != NULL) {
1149 return str;
1150 }
1151 }
1152 *outLen = 0;
1153 return NULL;
1154}
1155
1156sp<IBinder> Parcel::readStrongBinder() const
1157{
1158 sp<IBinder> val;
1159 unflatten_binder(ProcessState::self(), *this, &val);
1160 return val;
1161}
1162
1163wp<IBinder> Parcel::readWeakBinder() const
1164{
1165 wp<IBinder> val;
1166 unflatten_binder(ProcessState::self(), *this, &val);
1167 return val;
1168}
1169
Brad Fitzpatrick837a0d02010-07-13 15:33:35 -07001170int32_t Parcel::readExceptionCode() const
1171{
1172 int32_t exception_code = readAligned<int32_t>();
Brad Fitzpatrickd36f4a52010-07-12 11:05:38 -07001173 if (exception_code == EX_HAS_REPLY_HEADER) {
Magnus Strandberg1ba24572011-05-03 15:44:00 +02001174 int32_t header_start = dataPosition();
Brad Fitzpatrickd36f4a52010-07-12 11:05:38 -07001175 int32_t header_size = readAligned<int32_t>();
1176 // Skip over fat responses headers. Not used (or propagated) in
1177 // native code
Magnus Strandberg1ba24572011-05-03 15:44:00 +02001178 setDataPosition(header_start + header_size);
Brad Fitzpatrickd36f4a52010-07-12 11:05:38 -07001179 // And fat response headers are currently only used when there are no
1180 // exceptions, so return no error:
1181 return 0;
1182 }
Brad Fitzpatrick837a0d02010-07-13 15:33:35 -07001183 return exception_code;
1184}
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001185
Mathias Agopiana47f02a2009-05-21 16:29:38 -07001186native_handle* Parcel::readNativeHandle() const
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001187{
1188 int numFds, numInts;
1189 status_t err;
1190 err = readInt32(&numFds);
1191 if (err != NO_ERROR) return 0;
1192 err = readInt32(&numInts);
1193 if (err != NO_ERROR) return 0;
1194
Mathias Agopiana47f02a2009-05-21 16:29:38 -07001195 native_handle* h = native_handle_create(numFds, numInts);
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001196 for (int i=0 ; err==NO_ERROR && i<numFds ; i++) {
Rebecca Schultz Zavin360211f2009-02-13 16:34:38 -08001197 h->data[i] = dup(readFileDescriptor());
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001198 if (h->data[i] < 0) err = BAD_VALUE;
1199 }
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001200 err = read(h->data + numFds, sizeof(int)*numInts);
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001201 if (err != NO_ERROR) {
Mathias Agopiana47f02a2009-05-21 16:29:38 -07001202 native_handle_close(h);
1203 native_handle_delete(h);
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001204 h = 0;
1205 }
1206 return h;
1207}
1208
1209
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001210int Parcel::readFileDescriptor() const
1211{
1212 const flat_binder_object* flat = readObject(true);
1213 if (flat) {
1214 switch (flat->type) {
1215 case BINDER_TYPE_FD:
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001216 //ALOGI("Returning file descriptor %ld from parcel %p", flat->handle, this);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001217 return flat->handle;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001218 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001219 }
1220 return BAD_TYPE;
1221}
1222
Mike Lockwoodcbe36fe2013-09-05 08:41:06 -07001223// WARNING: This method must stay in sync with writeToParcel()
1224// in frameworks/base/core/java/android/os/ParcelFileDescriptor.java
1225int Parcel::readParcelFileDescriptor(int& outCommChannel) const {
1226 int fd;
1227 outCommChannel = -1;
1228
1229 if (readInt32() == 0) {
1230 fd = -1;
1231 } else {
1232 fd = readFileDescriptor();
1233 if (fd >= 0 && readInt32() != 0) {
1234 outCommChannel = readFileDescriptor();
1235 }
1236 }
1237 return fd;
1238}
1239
Jeff Brown5707dbf2011-09-23 21:17:56 -07001240status_t Parcel::readBlob(size_t len, ReadableBlob* outBlob) const
1241{
1242 int32_t useAshmem;
1243 status_t status = readInt32(&useAshmem);
1244 if (status) return status;
1245
1246 if (!useAshmem) {
Steve Block6807e592011-10-20 11:56:00 +01001247 ALOGV("readBlob: read in place");
Jeff Brown5707dbf2011-09-23 21:17:56 -07001248 const void* ptr = readInplace(len);
1249 if (!ptr) return BAD_VALUE;
1250
1251 outBlob->init(false /*mapped*/, const_cast<void*>(ptr), len);
1252 return NO_ERROR;
1253 }
1254
Steve Block6807e592011-10-20 11:56:00 +01001255 ALOGV("readBlob: read from ashmem");
Jeff Brown5707dbf2011-09-23 21:17:56 -07001256 int fd = readFileDescriptor();
1257 if (fd == int(BAD_TYPE)) return BAD_VALUE;
1258
1259 void* ptr = ::mmap(NULL, len, PROT_READ, MAP_SHARED, fd, 0);
Narayan Kamath9ea09752014-10-08 17:35:45 +01001260 if (ptr == MAP_FAILED) return NO_MEMORY;
Jeff Brown5707dbf2011-09-23 21:17:56 -07001261
1262 outBlob->init(true /*mapped*/, ptr, len);
1263 return NO_ERROR;
1264}
1265
Mathias Agopiane1424282013-07-29 21:24:40 -07001266status_t Parcel::read(FlattenableHelperInterface& val) const
Mathias Agopian98e71dd2010-02-11 17:30:52 -08001267{
1268 // size
1269 const size_t len = this->readInt32();
1270 const size_t fd_count = this->readInt32();
1271
1272 // payload
Mathias Agopiane1424282013-07-29 21:24:40 -07001273 void const* const buf = this->readInplace(PAD_SIZE(len));
Mathias Agopian98e71dd2010-02-11 17:30:52 -08001274 if (buf == NULL)
1275 return BAD_VALUE;
1276
1277 int* fds = NULL;
1278 if (fd_count) {
1279 fds = new int[fd_count];
1280 }
1281
1282 status_t err = NO_ERROR;
1283 for (size_t i=0 ; i<fd_count && err==NO_ERROR ; i++) {
Michael Lentine36273c92014-10-02 09:11:04 -07001284 int oldfd = this->readFileDescriptor();
1285 fds[i] = dup(oldfd);
Jun Jiangabf8a2c2014-04-29 14:22:10 +08001286 if (fds[i] < 0) {
Michael Lentine36273c92014-10-02 09:11:04 -07001287 int dupErrno = errno;
Jun Jiangabf8a2c2014-04-29 14:22:10 +08001288 err = BAD_VALUE;
Michael Lentine36273c92014-10-02 09:11:04 -07001289 int flags = fcntl(oldfd, F_GETFD);
1290 int fcntlErrno = errno;
1291 const flat_binder_object* flat = readObject(true);
1292 ALOGE("dup failed in Parcel::read, fd %zu of %zu\n"
1293 " dup(%d) = %d [errno: %d (%s)]\n"
1294 " fcntl(%d, F_GETFD) = %d [errno: %d (%s)]\n"
1295 " flat %p type %d",
1296 i, fd_count,
1297 oldfd, fds[i], dupErrno, strerror(dupErrno),
1298 oldfd, flags, fcntlErrno, strerror(fcntlErrno),
1299 flat, flat ? flat->type : 0);
Jun Jiangabf8a2c2014-04-29 14:22:10 +08001300 }
Mathias Agopian98e71dd2010-02-11 17:30:52 -08001301 }
1302
1303 if (err == NO_ERROR) {
1304 err = val.unflatten(buf, len, fds, fd_count);
1305 }
1306
1307 if (fd_count) {
1308 delete [] fds;
1309 }
1310
1311 return err;
1312}
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001313const flat_binder_object* Parcel::readObject(bool nullMetaData) const
1314{
1315 const size_t DPOS = mDataPos;
1316 if ((DPOS+sizeof(flat_binder_object)) <= mDataSize) {
1317 const flat_binder_object* obj
1318 = reinterpret_cast<const flat_binder_object*>(mData+DPOS);
1319 mDataPos = DPOS + sizeof(flat_binder_object);
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001320 if (!nullMetaData && (obj->cookie == 0 && obj->binder == 0)) {
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001321 // When transferring a NULL object, we don't write it into
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001322 // the object list, so we don't want to check for it when
1323 // reading.
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001324 ALOGV("readObject Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001325 return obj;
1326 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001327
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001328 // Ensure that this object is valid...
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001329 binder_size_t* const OBJS = mObjects;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001330 const size_t N = mObjectsSize;
1331 size_t opos = mNextObjectHint;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001332
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001333 if (N > 0) {
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001334 ALOGV("Parcel %p looking for obj at %zu, hint=%zu",
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001335 this, DPOS, opos);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001336
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001337 // Start at the current hint position, looking for an object at
1338 // the current data position.
1339 if (opos < N) {
1340 while (opos < (N-1) && OBJS[opos] < DPOS) {
1341 opos++;
1342 }
1343 } else {
1344 opos = N-1;
1345 }
1346 if (OBJS[opos] == DPOS) {
1347 // Found it!
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001348 ALOGV("Parcel %p found obj %zu at index %zu with forward search",
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001349 this, DPOS, opos);
1350 mNextObjectHint = opos+1;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001351 ALOGV("readObject Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001352 return obj;
1353 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001354
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001355 // Look backwards for it...
1356 while (opos > 0 && OBJS[opos] > DPOS) {
1357 opos--;
1358 }
1359 if (OBJS[opos] == DPOS) {
1360 // Found it!
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001361 ALOGV("Parcel %p found obj %zu at index %zu with backward search",
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001362 this, DPOS, opos);
1363 mNextObjectHint = opos+1;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001364 ALOGV("readObject Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001365 return obj;
1366 }
1367 }
Colin Cross6f4f3ab2014-02-05 17:42:44 -08001368 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 -07001369 this, DPOS);
1370 }
1371 return NULL;
1372}
1373
1374void Parcel::closeFileDescriptors()
1375{
1376 size_t i = mObjectsSize;
1377 if (i > 0) {
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001378 //ALOGI("Closing file descriptors for %zu objects...", i);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001379 }
1380 while (i > 0) {
1381 i--;
1382 const flat_binder_object* flat
1383 = reinterpret_cast<flat_binder_object*>(mData+mObjects[i]);
1384 if (flat->type == BINDER_TYPE_FD) {
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001385 //ALOGI("Closing fd: %ld", flat->handle);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001386 close(flat->handle);
1387 }
1388 }
1389}
1390
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001391uintptr_t Parcel::ipcData() const
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001392{
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001393 return reinterpret_cast<uintptr_t>(mData);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001394}
1395
1396size_t Parcel::ipcDataSize() const
1397{
1398 return (mDataSize > mDataPos ? mDataSize : mDataPos);
1399}
1400
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001401uintptr_t Parcel::ipcObjects() const
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001402{
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001403 return reinterpret_cast<uintptr_t>(mObjects);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001404}
1405
1406size_t Parcel::ipcObjectsCount() const
1407{
1408 return mObjectsSize;
1409}
1410
1411void Parcel::ipcSetDataReference(const uint8_t* data, size_t dataSize,
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001412 const binder_size_t* objects, size_t objectsCount, release_func relFunc, void* relCookie)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001413{
Arve Hjønnevåg6f286112014-02-19 20:42:13 -08001414 binder_size_t minOffset = 0;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001415 freeDataNoInit();
1416 mError = NO_ERROR;
1417 mData = const_cast<uint8_t*>(data);
1418 mDataSize = mDataCapacity = dataSize;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001419 //ALOGI("setDataReference Setting data size of %p to %lu (pid=%d)", this, mDataSize, getpid());
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001420 mDataPos = 0;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001421 ALOGV("setDataReference Setting data pos of %p to %zu", this, mDataPos);
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001422 mObjects = const_cast<binder_size_t*>(objects);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001423 mObjectsSize = mObjectsCapacity = objectsCount;
1424 mNextObjectHint = 0;
1425 mOwner = relFunc;
1426 mOwnerCookie = relCookie;
Arve Hjønnevågf50b9ea2014-02-13 19:22:08 -08001427 for (size_t i = 0; i < mObjectsSize; i++) {
Arve Hjønnevåg6f286112014-02-19 20:42:13 -08001428 binder_size_t offset = mObjects[i];
Arve Hjønnevågf50b9ea2014-02-13 19:22:08 -08001429 if (offset < minOffset) {
Arve Hjønnevåg6f286112014-02-19 20:42:13 -08001430 ALOGE("%s: bad object offset %"PRIu64" < %"PRIu64"\n",
1431 __func__, (uint64_t)offset, (uint64_t)minOffset);
Arve Hjønnevågf50b9ea2014-02-13 19:22:08 -08001432 mObjectsSize = 0;
1433 break;
1434 }
1435 minOffset = offset + sizeof(flat_binder_object);
1436 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001437 scanForFds();
1438}
1439
Colin Cross6f4f3ab2014-02-05 17:42:44 -08001440void Parcel::print(TextOutput& to, uint32_t /*flags*/) const
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001441{
1442 to << "Parcel(";
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001443
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001444 if (errorCheck() != NO_ERROR) {
1445 const status_t err = errorCheck();
Colin Cross6f4f3ab2014-02-05 17:42:44 -08001446 to << "Error: " << (void*)(intptr_t)err << " \"" << strerror(-err) << "\"";
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001447 } else if (dataSize() > 0) {
1448 const uint8_t* DATA = data();
1449 to << indent << HexDump(DATA, dataSize()) << dedent;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001450 const binder_size_t* OBJS = objects();
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001451 const size_t N = objectsCount();
1452 for (size_t i=0; i<N; i++) {
1453 const flat_binder_object* flat
1454 = reinterpret_cast<const flat_binder_object*>(DATA+OBJS[i]);
1455 to << endl << "Object #" << i << " @ " << (void*)OBJS[i] << ": "
1456 << TypeCode(flat->type & 0x7f7f7f00)
1457 << " = " << flat->binder;
1458 }
1459 } else {
1460 to << "NULL";
1461 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001462
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001463 to << ")";
1464}
1465
1466void Parcel::releaseObjects()
1467{
1468 const sp<ProcessState> proc(ProcessState::self());
1469 size_t i = mObjectsSize;
1470 uint8_t* const data = mData;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001471 binder_size_t* const objects = mObjects;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001472 while (i > 0) {
1473 i--;
1474 const flat_binder_object* flat
1475 = reinterpret_cast<flat_binder_object*>(data+objects[i]);
1476 release_object(proc, *flat, this);
1477 }
1478}
1479
1480void Parcel::acquireObjects()
1481{
1482 const sp<ProcessState> proc(ProcessState::self());
1483 size_t i = mObjectsSize;
1484 uint8_t* const data = mData;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001485 binder_size_t* const objects = mObjects;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001486 while (i > 0) {
1487 i--;
1488 const flat_binder_object* flat
1489 = reinterpret_cast<flat_binder_object*>(data+objects[i]);
1490 acquire_object(proc, *flat, this);
1491 }
1492}
1493
1494void Parcel::freeData()
1495{
1496 freeDataNoInit();
1497 initState();
1498}
1499
1500void Parcel::freeDataNoInit()
1501{
1502 if (mOwner) {
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001503 //ALOGI("Freeing data ref of %p (pid=%d)", this, getpid());
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001504 mOwner(this, mData, mDataSize, mObjects, mObjectsSize, mOwnerCookie);
1505 } else {
1506 releaseObjects();
1507 if (mData) free(mData);
1508 if (mObjects) free(mObjects);
1509 }
1510}
1511
1512status_t Parcel::growData(size_t len)
1513{
1514 size_t newSize = ((mDataSize+len)*3)/2;
1515 return (newSize <= mDataSize)
1516 ? (status_t) NO_MEMORY
1517 : continueWrite(newSize);
1518}
1519
1520status_t Parcel::restartWrite(size_t desired)
1521{
1522 if (mOwner) {
1523 freeData();
1524 return continueWrite(desired);
1525 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001526
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001527 uint8_t* data = (uint8_t*)realloc(mData, desired);
1528 if (!data && desired > mDataCapacity) {
1529 mError = NO_MEMORY;
1530 return NO_MEMORY;
1531 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001532
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001533 releaseObjects();
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001534
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001535 if (data) {
1536 mData = data;
1537 mDataCapacity = desired;
1538 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001539
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001540 mDataSize = mDataPos = 0;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001541 ALOGV("restartWrite Setting data size of %p to %zu", this, mDataSize);
1542 ALOGV("restartWrite Setting data pos of %p to %zu", this, mDataPos);
1543
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001544 free(mObjects);
1545 mObjects = NULL;
1546 mObjectsSize = mObjectsCapacity = 0;
1547 mNextObjectHint = 0;
1548 mHasFds = false;
1549 mFdsKnown = true;
Dianne Hackborn8938ed22011-09-28 23:19:47 -04001550 mAllowFds = true;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001551
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001552 return NO_ERROR;
1553}
1554
1555status_t Parcel::continueWrite(size_t desired)
1556{
1557 // If shrinking, first adjust for any objects that appear
1558 // after the new data size.
1559 size_t objectsSize = mObjectsSize;
1560 if (desired < mDataSize) {
1561 if (desired == 0) {
1562 objectsSize = 0;
1563 } else {
1564 while (objectsSize > 0) {
1565 if (mObjects[objectsSize-1] < desired)
1566 break;
1567 objectsSize--;
1568 }
1569 }
1570 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001571
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001572 if (mOwner) {
1573 // If the size is going to zero, just release the owner's data.
1574 if (desired == 0) {
1575 freeData();
1576 return NO_ERROR;
1577 }
1578
1579 // If there is a different owner, we need to take
1580 // posession.
1581 uint8_t* data = (uint8_t*)malloc(desired);
1582 if (!data) {
1583 mError = NO_MEMORY;
1584 return NO_MEMORY;
1585 }
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001586 binder_size_t* objects = NULL;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001587
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001588 if (objectsSize) {
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001589 objects = (binder_size_t*)malloc(objectsSize*sizeof(binder_size_t));
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001590 if (!objects) {
Hyejin Kim3f727c02013-03-09 11:28:54 +09001591 free(data);
1592
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001593 mError = NO_MEMORY;
1594 return NO_MEMORY;
1595 }
1596
1597 // Little hack to only acquire references on objects
1598 // we will be keeping.
1599 size_t oldObjectsSize = mObjectsSize;
1600 mObjectsSize = objectsSize;
1601 acquireObjects();
1602 mObjectsSize = oldObjectsSize;
1603 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001604
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001605 if (mData) {
1606 memcpy(data, mData, mDataSize < desired ? mDataSize : desired);
1607 }
1608 if (objects && mObjects) {
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001609 memcpy(objects, mObjects, objectsSize*sizeof(binder_size_t));
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001610 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001611 //ALOGI("Freeing data ref of %p (pid=%d)", this, getpid());
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001612 mOwner(this, mData, mDataSize, mObjects, mObjectsSize, mOwnerCookie);
1613 mOwner = NULL;
1614
1615 mData = data;
1616 mObjects = objects;
1617 mDataSize = (mDataSize < desired) ? mDataSize : desired;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001618 ALOGV("continueWrite Setting data size of %p to %zu", this, mDataSize);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001619 mDataCapacity = desired;
1620 mObjectsSize = mObjectsCapacity = objectsSize;
1621 mNextObjectHint = 0;
1622
1623 } else if (mData) {
1624 if (objectsSize < mObjectsSize) {
1625 // Need to release refs on any objects we are dropping.
1626 const sp<ProcessState> proc(ProcessState::self());
1627 for (size_t i=objectsSize; i<mObjectsSize; i++) {
1628 const flat_binder_object* flat
1629 = reinterpret_cast<flat_binder_object*>(mData+mObjects[i]);
1630 if (flat->type == BINDER_TYPE_FD) {
1631 // will need to rescan because we may have lopped off the only FDs
1632 mFdsKnown = false;
1633 }
1634 release_object(proc, *flat, this);
1635 }
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001636 binder_size_t* objects =
1637 (binder_size_t*)realloc(mObjects, objectsSize*sizeof(binder_size_t));
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001638 if (objects) {
1639 mObjects = objects;
1640 }
1641 mObjectsSize = objectsSize;
1642 mNextObjectHint = 0;
1643 }
1644
1645 // We own the data, so we can just do a realloc().
1646 if (desired > mDataCapacity) {
1647 uint8_t* data = (uint8_t*)realloc(mData, desired);
1648 if (data) {
1649 mData = data;
1650 mDataCapacity = desired;
1651 } else if (desired > mDataCapacity) {
1652 mError = NO_MEMORY;
1653 return NO_MEMORY;
1654 }
1655 } else {
Dianne Hackborn97e2bcd2011-04-13 18:15:56 -07001656 if (mDataSize > desired) {
1657 mDataSize = desired;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001658 ALOGV("continueWrite Setting data size of %p to %zu", this, mDataSize);
Dianne Hackborn97e2bcd2011-04-13 18:15:56 -07001659 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001660 if (mDataPos > desired) {
1661 mDataPos = desired;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001662 ALOGV("continueWrite Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001663 }
1664 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001665
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001666 } else {
1667 // This is the first data. Easy!
1668 uint8_t* data = (uint8_t*)malloc(desired);
1669 if (!data) {
1670 mError = NO_MEMORY;
1671 return NO_MEMORY;
1672 }
Hyejin Kim3f727c02013-03-09 11:28:54 +09001673
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001674 if(!(mDataCapacity == 0 && mObjects == NULL
1675 && mObjectsCapacity == 0)) {
Colin Cross6f4f3ab2014-02-05 17:42:44 -08001676 ALOGE("continueWrite: %zu/%p/%zu/%zu", mDataCapacity, mObjects, mObjectsCapacity, desired);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001677 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001678
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001679 mData = data;
1680 mDataSize = mDataPos = 0;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001681 ALOGV("continueWrite Setting data size of %p to %zu", this, mDataSize);
1682 ALOGV("continueWrite Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001683 mDataCapacity = desired;
1684 }
1685
1686 return NO_ERROR;
1687}
1688
1689void Parcel::initState()
1690{
1691 mError = NO_ERROR;
1692 mData = 0;
1693 mDataSize = 0;
1694 mDataCapacity = 0;
1695 mDataPos = 0;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001696 ALOGV("initState Setting data size of %p to %zu", this, mDataSize);
1697 ALOGV("initState Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001698 mObjects = NULL;
1699 mObjectsSize = 0;
1700 mObjectsCapacity = 0;
1701 mNextObjectHint = 0;
1702 mHasFds = false;
1703 mFdsKnown = true;
Dianne Hackborn8938ed22011-09-28 23:19:47 -04001704 mAllowFds = true;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001705 mOwner = NULL;
1706}
1707
1708void Parcel::scanForFds() const
1709{
1710 bool hasFds = false;
1711 for (size_t i=0; i<mObjectsSize; i++) {
1712 const flat_binder_object* flat
1713 = reinterpret_cast<const flat_binder_object*>(mData + mObjects[i]);
1714 if (flat->type == BINDER_TYPE_FD) {
1715 hasFds = true;
1716 break;
1717 }
1718 }
1719 mHasFds = hasFds;
1720 mFdsKnown = true;
1721}
1722
Jeff Brown5707dbf2011-09-23 21:17:56 -07001723// --- Parcel::Blob ---
1724
1725Parcel::Blob::Blob() :
1726 mMapped(false), mData(NULL), mSize(0) {
1727}
1728
1729Parcel::Blob::~Blob() {
1730 release();
1731}
1732
1733void Parcel::Blob::release() {
1734 if (mMapped && mData) {
1735 ::munmap(mData, mSize);
1736 }
1737 clear();
1738}
1739
1740void Parcel::Blob::init(bool mapped, void* data, size_t size) {
1741 mMapped = mapped;
1742 mData = data;
1743 mSize = size;
1744}
1745
1746void Parcel::Blob::clear() {
1747 mMapped = false;
1748 mData = NULL;
1749 mSize = 0;
1750}
1751
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001752}; // namespace android