blob: 1e7df6c435bed95e3560a145a3f3c0198ce53801 [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>
Jesse Hallccf851f2014-10-06 09:49:45 -070029#include <utils/CallStack.h>
Mathias Agopian002e1e52013-05-06 20:20:50 -070030#include <utils/Debug.h>
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -070031#include <utils/Log.h>
32#include <utils/String8.h>
33#include <utils/String16.h>
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -070034#include <utils/misc.h>
Mathias Agopian98e71dd2010-02-11 17:30:52 -080035#include <utils/Flattenable.h>
Jeff Brown5707dbf2011-09-23 21:17:56 -070036#include <cutils/ashmem.h>
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -070037
Mathias Agopian208059f2009-05-18 15:08:03 -070038#include <private/binder/binder_module.h>
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -070039
Michael Lentine36273c92014-10-02 09:11:04 -070040#include <fcntl.h>
Arve Hjønnevågf50b9ea2014-02-13 19:22:08 -080041#include <inttypes.h>
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -070042#include <stdio.h>
43#include <stdlib.h>
44#include <stdint.h>
Jeff Brown5707dbf2011-09-23 21:17:56 -070045#include <sys/mman.h>
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -070046
47#ifndef INT32_MAX
48#define INT32_MAX ((int32_t)(2147483647))
49#endif
50
51#define LOG_REFS(...)
Steve Block9f760152011-10-12 17:27:03 +010052//#define LOG_REFS(...) ALOG(LOG_DEBUG, "Parcel", __VA_ARGS__)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -070053
54// ---------------------------------------------------------------------------
55
56#define PAD_SIZE(s) (((s)+3)&~3)
57
Brad Fitzpatricka877cd82010-07-07 16:06:39 -070058// Note: must be kept in sync with android/os/StrictMode.java's PENALTY_GATHER
59#define STRICT_MODE_PENALTY_GATHER 0x100
60
Brad Fitzpatrickd36f4a52010-07-12 11:05:38 -070061// Note: must be kept in sync with android/os/Parcel.java's EX_HAS_REPLY_HEADER
62#define EX_HAS_REPLY_HEADER -128
63
Jeff Brown5707dbf2011-09-23 21:17:56 -070064// Maximum size of a blob to transfer in-place.
65static const size_t IN_PLACE_BLOB_LIMIT = 40 * 1024;
66
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -070067// XXX This can be made public if we want to provide
68// support for typed data.
69struct small_flat_data
70{
71 uint32_t type;
72 uint32_t data;
73};
74
75namespace android {
76
77void acquire_object(const sp<ProcessState>& proc,
78 const flat_binder_object& obj, const void* who)
79{
80 switch (obj.type) {
81 case BINDER_TYPE_BINDER:
82 if (obj.binder) {
83 LOG_REFS("Parcel %p acquiring reference on local %p", who, obj.cookie);
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -080084 reinterpret_cast<IBinder*>(obj.cookie)->incStrong(who);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -070085 }
86 return;
87 case BINDER_TYPE_WEAK_BINDER:
88 if (obj.binder)
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -080089 reinterpret_cast<RefBase::weakref_type*>(obj.binder)->incWeak(who);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -070090 return;
91 case BINDER_TYPE_HANDLE: {
92 const sp<IBinder> b = proc->getStrongProxyForHandle(obj.handle);
93 if (b != NULL) {
94 LOG_REFS("Parcel %p acquiring reference on remote %p", who, b.get());
95 b->incStrong(who);
96 }
97 return;
98 }
99 case BINDER_TYPE_WEAK_HANDLE: {
100 const wp<IBinder> b = proc->getWeakProxyForHandle(obj.handle);
101 if (b != NULL) b.get_refs()->incWeak(who);
102 return;
103 }
104 case BINDER_TYPE_FD: {
105 // intentionally blank -- nothing to do to acquire this, but we do
106 // recognize it as a legitimate object type.
107 return;
108 }
109 }
110
Colin Cross6f4f3ab2014-02-05 17:42:44 -0800111 ALOGD("Invalid object type 0x%08x", obj.type);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700112}
113
114void release_object(const sp<ProcessState>& proc,
115 const flat_binder_object& obj, const void* who)
116{
117 switch (obj.type) {
118 case BINDER_TYPE_BINDER:
119 if (obj.binder) {
120 LOG_REFS("Parcel %p releasing reference on local %p", who, obj.cookie);
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800121 reinterpret_cast<IBinder*>(obj.cookie)->decStrong(who);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700122 }
123 return;
124 case BINDER_TYPE_WEAK_BINDER:
125 if (obj.binder)
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800126 reinterpret_cast<RefBase::weakref_type*>(obj.binder)->decWeak(who);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700127 return;
128 case BINDER_TYPE_HANDLE: {
129 const sp<IBinder> b = proc->getStrongProxyForHandle(obj.handle);
130 if (b != NULL) {
131 LOG_REFS("Parcel %p releasing reference on remote %p", who, b.get());
132 b->decStrong(who);
133 }
134 return;
135 }
136 case BINDER_TYPE_WEAK_HANDLE: {
137 const wp<IBinder> b = proc->getWeakProxyForHandle(obj.handle);
138 if (b != NULL) b.get_refs()->decWeak(who);
139 return;
140 }
141 case BINDER_TYPE_FD: {
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800142 if (obj.cookie != 0) close(obj.handle);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700143 return;
144 }
145 }
146
Colin Cross6f4f3ab2014-02-05 17:42:44 -0800147 ALOGE("Invalid object type 0x%08x", obj.type);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700148}
149
150inline static status_t finish_flatten_binder(
Colin Cross6f4f3ab2014-02-05 17:42:44 -0800151 const sp<IBinder>& /*binder*/, const flat_binder_object& flat, Parcel* out)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700152{
153 return out->writeObject(flat, false);
154}
155
Colin Cross6f4f3ab2014-02-05 17:42:44 -0800156status_t flatten_binder(const sp<ProcessState>& /*proc*/,
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700157 const sp<IBinder>& binder, Parcel* out)
158{
159 flat_binder_object obj;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700160
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700161 obj.flags = 0x7f | FLAT_BINDER_FLAG_ACCEPTS_FDS;
162 if (binder != NULL) {
163 IBinder *local = binder->localBinder();
164 if (!local) {
165 BpBinder *proxy = binder->remoteBinder();
166 if (proxy == NULL) {
Steve Blocke6f43dd2012-01-06 19:20:56 +0000167 ALOGE("null proxy");
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700168 }
169 const int32_t handle = proxy ? proxy->handle() : 0;
170 obj.type = BINDER_TYPE_HANDLE;
Arve Hjønnevåg07fd0f12014-02-18 21:10:29 -0800171 obj.binder = 0; /* Don't pass uninitialized stack data to a remote process */
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700172 obj.handle = handle;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800173 obj.cookie = 0;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700174 } else {
175 obj.type = BINDER_TYPE_BINDER;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800176 obj.binder = reinterpret_cast<uintptr_t>(local->getWeakRefs());
177 obj.cookie = reinterpret_cast<uintptr_t>(local);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700178 }
179 } else {
180 obj.type = BINDER_TYPE_BINDER;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800181 obj.binder = 0;
182 obj.cookie = 0;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700183 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700184
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700185 return finish_flatten_binder(binder, obj, out);
186}
187
Colin Cross6f4f3ab2014-02-05 17:42:44 -0800188status_t flatten_binder(const sp<ProcessState>& /*proc*/,
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700189 const wp<IBinder>& binder, Parcel* out)
190{
191 flat_binder_object obj;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700192
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700193 obj.flags = 0x7f | FLAT_BINDER_FLAG_ACCEPTS_FDS;
194 if (binder != NULL) {
195 sp<IBinder> real = binder.promote();
196 if (real != NULL) {
197 IBinder *local = real->localBinder();
198 if (!local) {
199 BpBinder *proxy = real->remoteBinder();
200 if (proxy == NULL) {
Steve Blocke6f43dd2012-01-06 19:20:56 +0000201 ALOGE("null proxy");
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700202 }
203 const int32_t handle = proxy ? proxy->handle() : 0;
204 obj.type = BINDER_TYPE_WEAK_HANDLE;
Arve Hjønnevåg07fd0f12014-02-18 21:10:29 -0800205 obj.binder = 0; /* Don't pass uninitialized stack data to a remote process */
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700206 obj.handle = handle;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800207 obj.cookie = 0;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700208 } else {
209 obj.type = BINDER_TYPE_WEAK_BINDER;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800210 obj.binder = reinterpret_cast<uintptr_t>(binder.get_refs());
211 obj.cookie = reinterpret_cast<uintptr_t>(binder.unsafe_get());
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700212 }
213 return finish_flatten_binder(real, obj, out);
214 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700215
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700216 // XXX How to deal? In order to flatten the given binder,
217 // we need to probe it for information, which requires a primary
218 // reference... but we don't have one.
219 //
220 // The OpenBinder implementation uses a dynamic_cast<> here,
221 // but we can't do that with the different reference counting
222 // implementation we are using.
Steve Blocke6f43dd2012-01-06 19:20:56 +0000223 ALOGE("Unable to unflatten Binder weak reference!");
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700224 obj.type = BINDER_TYPE_BINDER;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800225 obj.binder = 0;
226 obj.cookie = 0;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700227 return finish_flatten_binder(NULL, obj, out);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700228
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700229 } else {
230 obj.type = BINDER_TYPE_BINDER;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800231 obj.binder = 0;
232 obj.cookie = 0;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700233 return finish_flatten_binder(NULL, obj, out);
234 }
235}
236
237inline static status_t finish_unflatten_binder(
Colin Cross6f4f3ab2014-02-05 17:42:44 -0800238 BpBinder* /*proxy*/, const flat_binder_object& /*flat*/,
239 const Parcel& /*in*/)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700240{
241 return NO_ERROR;
242}
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700243
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700244status_t unflatten_binder(const sp<ProcessState>& proc,
245 const Parcel& in, sp<IBinder>* out)
246{
247 const flat_binder_object* flat = in.readObject(false);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700248
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700249 if (flat) {
250 switch (flat->type) {
251 case BINDER_TYPE_BINDER:
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800252 *out = reinterpret_cast<IBinder*>(flat->cookie);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700253 return finish_unflatten_binder(NULL, *flat, in);
254 case BINDER_TYPE_HANDLE:
255 *out = proc->getStrongProxyForHandle(flat->handle);
256 return finish_unflatten_binder(
257 static_cast<BpBinder*>(out->get()), *flat, in);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700258 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700259 }
260 return BAD_TYPE;
261}
262
263status_t unflatten_binder(const sp<ProcessState>& proc,
264 const Parcel& in, wp<IBinder>* out)
265{
266 const flat_binder_object* flat = in.readObject(false);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700267
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700268 if (flat) {
269 switch (flat->type) {
270 case BINDER_TYPE_BINDER:
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800271 *out = reinterpret_cast<IBinder*>(flat->cookie);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700272 return finish_unflatten_binder(NULL, *flat, in);
273 case BINDER_TYPE_WEAK_BINDER:
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800274 if (flat->binder != 0) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700275 out->set_object_and_refs(
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800276 reinterpret_cast<IBinder*>(flat->cookie),
277 reinterpret_cast<RefBase::weakref_type*>(flat->binder));
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700278 } else {
279 *out = NULL;
280 }
281 return finish_unflatten_binder(NULL, *flat, in);
282 case BINDER_TYPE_HANDLE:
283 case BINDER_TYPE_WEAK_HANDLE:
284 *out = proc->getWeakProxyForHandle(flat->handle);
285 return finish_unflatten_binder(
286 static_cast<BpBinder*>(out->unsafe_get()), *flat, in);
287 }
288 }
289 return BAD_TYPE;
290}
291
292// ---------------------------------------------------------------------------
293
294Parcel::Parcel()
295{
296 initState();
297}
298
299Parcel::~Parcel()
300{
301 freeDataNoInit();
302}
303
304const uint8_t* Parcel::data() const
305{
306 return mData;
307}
308
309size_t Parcel::dataSize() const
310{
311 return (mDataSize > mDataPos ? mDataSize : mDataPos);
312}
313
314size_t Parcel::dataAvail() const
315{
316 // TODO: decide what to do about the possibility that this can
317 // report an available-data size that exceeds a Java int's max
318 // positive value, causing havoc. Fortunately this will only
319 // happen if someone constructs a Parcel containing more than two
320 // gigabytes of data, which on typical phone hardware is simply
321 // not possible.
322 return dataSize() - dataPosition();
323}
324
325size_t Parcel::dataPosition() const
326{
327 return mDataPos;
328}
329
330size_t Parcel::dataCapacity() const
331{
332 return mDataCapacity;
333}
334
335status_t Parcel::setDataSize(size_t size)
336{
337 status_t err;
338 err = continueWrite(size);
339 if (err == NO_ERROR) {
340 mDataSize = size;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700341 ALOGV("setDataSize Setting data size of %p to %zu", this, mDataSize);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700342 }
343 return err;
344}
345
346void Parcel::setDataPosition(size_t pos) const
347{
348 mDataPos = pos;
349 mNextObjectHint = 0;
350}
351
352status_t Parcel::setDataCapacity(size_t size)
353{
Dianne Hackborn97e2bcd2011-04-13 18:15:56 -0700354 if (size > mDataCapacity) return continueWrite(size);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700355 return NO_ERROR;
356}
357
358status_t Parcel::setData(const uint8_t* buffer, size_t len)
359{
360 status_t err = restartWrite(len);
361 if (err == NO_ERROR) {
362 memcpy(const_cast<uint8_t*>(data()), buffer, len);
363 mDataSize = len;
364 mFdsKnown = false;
365 }
366 return err;
367}
368
Andreas Huber51faf462011-04-13 10:21:56 -0700369status_t Parcel::appendFrom(const Parcel *parcel, size_t offset, size_t len)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700370{
371 const sp<ProcessState> proc(ProcessState::self());
372 status_t err;
Andreas Huber51faf462011-04-13 10:21:56 -0700373 const uint8_t *data = parcel->mData;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800374 const binder_size_t *objects = parcel->mObjects;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700375 size_t size = parcel->mObjectsSize;
376 int startPos = mDataPos;
377 int firstIndex = -1, lastIndex = -2;
378
379 if (len == 0) {
380 return NO_ERROR;
381 }
382
383 // range checks against the source parcel size
384 if ((offset > parcel->mDataSize)
385 || (len > parcel->mDataSize)
386 || (offset + len > parcel->mDataSize)) {
387 return BAD_VALUE;
388 }
389
390 // Count objects in range
391 for (int i = 0; i < (int) size; i++) {
392 size_t off = objects[i];
Christopher Tate1b8a2f82015-05-27 17:53:02 -0700393 if ((off >= offset) && (off + sizeof(flat_binder_object) <= offset + len)) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700394 if (firstIndex == -1) {
395 firstIndex = i;
396 }
397 lastIndex = i;
398 }
399 }
400 int numObjects = lastIndex - firstIndex + 1;
401
Dianne Hackborn97e2bcd2011-04-13 18:15:56 -0700402 if ((mDataSize+len) > mDataCapacity) {
403 // grow data
404 err = growData(len);
405 if (err != NO_ERROR) {
406 return err;
407 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700408 }
409
410 // append data
411 memcpy(mData + mDataPos, data + offset, len);
412 mDataPos += len;
413 mDataSize += len;
414
Dianne Hackborn8938ed22011-09-28 23:19:47 -0400415 err = NO_ERROR;
416
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700417 if (numObjects > 0) {
418 // grow objects
419 if (mObjectsCapacity < mObjectsSize + numObjects) {
Christopher Tate8b643072016-11-03 13:32:41 -0700420 size_t newSize = ((mObjectsSize + numObjects)*3)/2;
421 if (newSize*sizeof(binder_size_t) < mObjectsSize) return NO_MEMORY; // overflow
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800422 binder_size_t *objects =
423 (binder_size_t*)realloc(mObjects, newSize*sizeof(binder_size_t));
424 if (objects == (binder_size_t*)0) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700425 return NO_MEMORY;
426 }
427 mObjects = objects;
428 mObjectsCapacity = newSize;
429 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700430
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700431 // append and acquire objects
432 int idx = mObjectsSize;
433 for (int i = firstIndex; i <= lastIndex; i++) {
434 size_t off = objects[i] - offset + startPos;
435 mObjects[idx++] = off;
436 mObjectsSize++;
437
Dianne Hackborn8af0f822009-05-22 13:20:23 -0700438 flat_binder_object* flat
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700439 = reinterpret_cast<flat_binder_object*>(mData + off);
440 acquire_object(proc, *flat, this);
441
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700442 if (flat->type == BINDER_TYPE_FD) {
Dianne Hackborn8af0f822009-05-22 13:20:23 -0700443 // If this is a file descriptor, we need to dup it so the
444 // new Parcel now owns its own fd, and can declare that we
445 // officially know we have fds.
446 flat->handle = dup(flat->handle);
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800447 flat->cookie = 1;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700448 mHasFds = mFdsKnown = true;
Dianne Hackborn8938ed22011-09-28 23:19:47 -0400449 if (!mAllowFds) {
450 err = FDS_NOT_ALLOWED;
451 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700452 }
453 }
454 }
455
Dianne Hackborn8938ed22011-09-28 23:19:47 -0400456 return err;
457}
458
Dianne Hackborn7746cc32011-10-03 21:09:35 -0700459bool Parcel::pushAllowFds(bool allowFds)
Dianne Hackborn8938ed22011-09-28 23:19:47 -0400460{
461 const bool origValue = mAllowFds;
Dianne Hackborn7746cc32011-10-03 21:09:35 -0700462 if (!allowFds) {
463 mAllowFds = false;
464 }
Dianne Hackborn8938ed22011-09-28 23:19:47 -0400465 return origValue;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700466}
467
Dianne Hackborn7746cc32011-10-03 21:09:35 -0700468void Parcel::restoreAllowFds(bool lastValue)
469{
470 mAllowFds = lastValue;
471}
472
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700473bool Parcel::hasFileDescriptors() const
474{
475 if (!mFdsKnown) {
476 scanForFds();
477 }
478 return mHasFds;
479}
480
Brad Fitzpatrick702ea9d2010-06-18 13:07:53 -0700481// Write RPC headers. (previously just the interface token)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700482status_t Parcel::writeInterfaceToken(const String16& interface)
483{
Brad Fitzpatricka877cd82010-07-07 16:06:39 -0700484 writeInt32(IPCThreadState::self()->getStrictModePolicy() |
485 STRICT_MODE_PENALTY_GATHER);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700486 // currently the interface identification token is just its name as a string
487 return writeString16(interface);
488}
489
Mathias Agopian83c04462009-05-22 19:00:22 -0700490bool Parcel::checkInterface(IBinder* binder) const
491{
Brad Fitzpatrick702ea9d2010-06-18 13:07:53 -0700492 return enforceInterface(binder->getInterfaceDescriptor());
Mathias Agopian83c04462009-05-22 19:00:22 -0700493}
494
Brad Fitzpatricka877cd82010-07-07 16:06:39 -0700495bool Parcel::enforceInterface(const String16& interface,
Brad Fitzpatrick70081a12010-07-27 09:49:11 -0700496 IPCThreadState* threadState) const
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700497{
Brad Fitzpatrick70081a12010-07-27 09:49:11 -0700498 int32_t strictPolicy = readInt32();
499 if (threadState == NULL) {
500 threadState = IPCThreadState::self();
Brad Fitzpatricka877cd82010-07-07 16:06:39 -0700501 }
Brad Fitzpatrick52736032010-08-30 16:01:16 -0700502 if ((threadState->getLastTransactionBinderFlags() &
503 IBinder::FLAG_ONEWAY) != 0) {
504 // For one-way calls, the callee is running entirely
505 // disconnected from the caller, so disable StrictMode entirely.
506 // Not only does disk/network usage not impact the caller, but
507 // there's no way to commuicate back any violations anyway.
508 threadState->setStrictModePolicy(0);
509 } else {
510 threadState->setStrictModePolicy(strictPolicy);
511 }
Mathias Agopian83c04462009-05-22 19:00:22 -0700512 const String16 str(readString16());
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700513 if (str == interface) {
514 return true;
515 } else {
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700516 ALOGW("**** enforceInterface() expected '%s' but read '%s'",
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700517 String8(interface).string(), String8(str).string());
518 return false;
519 }
Brad Fitzpatrick702ea9d2010-06-18 13:07:53 -0700520}
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700521
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800522const binder_size_t* Parcel::objects() const
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700523{
524 return mObjects;
525}
526
527size_t Parcel::objectsCount() const
528{
529 return mObjectsSize;
530}
531
532status_t Parcel::errorCheck() const
533{
534 return mError;
535}
536
537void Parcel::setError(status_t err)
538{
539 mError = err;
540}
541
542status_t Parcel::finishWrite(size_t len)
543{
544 //printf("Finish write of %d\n", len);
545 mDataPos += len;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700546 ALOGV("finishWrite Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700547 if (mDataPos > mDataSize) {
548 mDataSize = mDataPos;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700549 ALOGV("finishWrite Setting data size of %p to %zu", this, mDataSize);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700550 }
551 //printf("New pos=%d, size=%d\n", mDataPos, mDataSize);
552 return NO_ERROR;
553}
554
555status_t Parcel::writeUnpadded(const void* data, size_t len)
556{
557 size_t end = mDataPos + len;
558 if (end < mDataPos) {
559 // integer overflow
560 return BAD_VALUE;
561 }
562
563 if (end <= mDataCapacity) {
564restart_write:
565 memcpy(mData+mDataPos, data, len);
566 return finishWrite(len);
567 }
568
569 status_t err = growData(len);
570 if (err == NO_ERROR) goto restart_write;
571 return err;
572}
573
574status_t Parcel::write(const void* data, size_t len)
575{
576 void* const d = writeInplace(len);
577 if (d) {
578 memcpy(d, data, len);
579 return NO_ERROR;
580 }
581 return mError;
582}
583
584void* Parcel::writeInplace(size_t len)
585{
586 const size_t padded = PAD_SIZE(len);
587
588 // sanity check for integer overflow
589 if (mDataPos+padded < mDataPos) {
590 return NULL;
591 }
592
593 if ((mDataPos+padded) <= mDataCapacity) {
594restart_write:
595 //printf("Writing %ld bytes, padded to %ld\n", len, padded);
596 uint8_t* const data = mData+mDataPos;
597
598 // Need to pad at end?
599 if (padded != len) {
600#if BYTE_ORDER == BIG_ENDIAN
601 static const uint32_t mask[4] = {
602 0x00000000, 0xffffff00, 0xffff0000, 0xff000000
603 };
604#endif
605#if BYTE_ORDER == LITTLE_ENDIAN
606 static const uint32_t mask[4] = {
607 0x00000000, 0x00ffffff, 0x0000ffff, 0x000000ff
608 };
609#endif
610 //printf("Applying pad mask: %p to %p\n", (void*)mask[padded-len],
611 // *reinterpret_cast<void**>(data+padded-4));
612 *reinterpret_cast<uint32_t*>(data+padded-4) &= mask[padded-len];
613 }
614
615 finishWrite(padded);
616 return data;
617 }
618
619 status_t err = growData(padded);
620 if (err == NO_ERROR) goto restart_write;
621 return NULL;
622}
623
624status_t Parcel::writeInt32(int32_t val)
625{
Andreas Huber84a6d042009-08-17 13:33:27 -0700626 return writeAligned(val);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700627}
Marco Nelissen5c0106e2013-10-16 10:57:51 -0700628status_t Parcel::writeInt32Array(size_t len, const int32_t *val) {
629 if (!val) {
630 return writeAligned(-1);
631 }
632 status_t ret = writeAligned(len);
633 if (ret == NO_ERROR) {
634 ret = write(val, len * sizeof(*val));
635 }
636 return ret;
637}
Marco Nelissenf0190bf2014-03-13 14:17:40 -0700638status_t Parcel::writeByteArray(size_t len, const uint8_t *val) {
639 if (!val) {
640 return writeAligned(-1);
641 }
642 status_t ret = writeAligned(len);
643 if (ret == NO_ERROR) {
644 ret = write(val, len * sizeof(*val));
645 }
646 return ret;
647}
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700648
649status_t Parcel::writeInt64(int64_t val)
650{
Andreas Huber84a6d042009-08-17 13:33:27 -0700651 return writeAligned(val);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700652}
653
Serban Constantinescuf683e012013-11-05 16:53:55 +0000654status_t Parcel::writePointer(uintptr_t val)
655{
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800656 return writeAligned<binder_uintptr_t>(val);
Serban Constantinescuf683e012013-11-05 16:53:55 +0000657}
658
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700659status_t Parcel::writeFloat(float val)
660{
Andreas Huber84a6d042009-08-17 13:33:27 -0700661 return writeAligned(val);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700662}
663
Douglas Leungcc1a4bb2013-01-11 15:00:55 -0800664#if defined(__mips__) && defined(__mips_hard_float)
665
666status_t Parcel::writeDouble(double val)
667{
668 union {
669 double d;
670 unsigned long long ll;
671 } u;
672 u.d = val;
673 return writeAligned(u.ll);
674}
675
676#else
677
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700678status_t Parcel::writeDouble(double val)
679{
Andreas Huber84a6d042009-08-17 13:33:27 -0700680 return writeAligned(val);
681}
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700682
Douglas Leungcc1a4bb2013-01-11 15:00:55 -0800683#endif
684
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700685status_t Parcel::writeCString(const char* str)
686{
687 return write(str, strlen(str)+1);
688}
689
690status_t Parcel::writeString8(const String8& str)
691{
692 status_t err = writeInt32(str.bytes());
Pravat Dalbeherad1dff8d2010-12-15 08:40:00 +0100693 // only write string if its length is more than zero characters,
694 // as readString8 will only read if the length field is non-zero.
695 // this is slightly different from how writeString16 works.
696 if (str.bytes() > 0 && err == NO_ERROR) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700697 err = write(str.string(), str.bytes()+1);
698 }
699 return err;
700}
701
702status_t Parcel::writeString16(const String16& str)
703{
704 return writeString16(str.string(), str.size());
705}
706
707status_t Parcel::writeString16(const char16_t* str, size_t len)
708{
709 if (str == NULL) return writeInt32(-1);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700710
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700711 status_t err = writeInt32(len);
712 if (err == NO_ERROR) {
713 len *= sizeof(char16_t);
714 uint8_t* data = (uint8_t*)writeInplace(len+sizeof(char16_t));
715 if (data) {
716 memcpy(data, str, len);
717 *reinterpret_cast<char16_t*>(data+len) = 0;
718 return NO_ERROR;
719 }
720 err = mError;
721 }
722 return err;
723}
724
725status_t Parcel::writeStrongBinder(const sp<IBinder>& val)
726{
727 return flatten_binder(ProcessState::self(), val, this);
728}
729
730status_t Parcel::writeWeakBinder(const wp<IBinder>& val)
731{
732 return flatten_binder(ProcessState::self(), val, this);
733}
734
Mathias Agopiana47f02a2009-05-21 16:29:38 -0700735status_t Parcel::writeNativeHandle(const native_handle* handle)
The Android Open Source Project5f78a482009-01-20 14:03:58 -0800736{
Mathias Agopian1d0a95b2009-07-31 16:12:13 -0700737 if (!handle || handle->version != sizeof(native_handle))
The Android Open Source Project5f78a482009-01-20 14:03:58 -0800738 return BAD_TYPE;
739
740 status_t err;
Mathias Agopiana47f02a2009-05-21 16:29:38 -0700741 err = writeInt32(handle->numFds);
The Android Open Source Project5f78a482009-01-20 14:03:58 -0800742 if (err != NO_ERROR) return err;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800743
Mathias Agopiana47f02a2009-05-21 16:29:38 -0700744 err = writeInt32(handle->numInts);
The Android Open Source Project5f78a482009-01-20 14:03:58 -0800745 if (err != NO_ERROR) return err;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800746
Mathias Agopiana47f02a2009-05-21 16:29:38 -0700747 for (int i=0 ; err==NO_ERROR && i<handle->numFds ; i++)
748 err = writeDupFileDescriptor(handle->data[i]);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800749
750 if (err != NO_ERROR) {
Steve Block9d453682011-12-20 16:23:08 +0000751 ALOGD("write native handle, write dup fd failed");
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800752 return err;
753 }
Mathias Agopiana47f02a2009-05-21 16:29:38 -0700754 err = write(handle->data + handle->numFds, sizeof(int)*handle->numInts);
The Android Open Source Project5f78a482009-01-20 14:03:58 -0800755 return err;
756}
757
Jeff Brown93ff1f92011-11-04 19:01:44 -0700758status_t Parcel::writeFileDescriptor(int fd, bool takeOwnership)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700759{
760 flat_binder_object obj;
761 obj.type = BINDER_TYPE_FD;
762 obj.flags = 0x7f | FLAT_BINDER_FLAG_ACCEPTS_FDS;
Arve Hjønnevåg07fd0f12014-02-18 21:10:29 -0800763 obj.binder = 0; /* Don't pass uninitialized stack data to a remote process */
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700764 obj.handle = fd;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800765 obj.cookie = takeOwnership ? 1 : 0;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700766 return writeObject(obj, true);
767}
768
769status_t Parcel::writeDupFileDescriptor(int fd)
770{
Jeff Brownd341c712011-11-04 20:19:33 -0700771 int dupFd = dup(fd);
Jesse Hallccf851f2014-10-06 09:49:45 -0700772
773 { // Temporary extra debug validation for b/17477219: a Parcel recipient is
774 // getting a positive but invalid fd unexpectedly. Trying to track down
775 // where it's coming from.
776 int dupErrno = dupFd < 0 ? errno : 0;
777 int fdFlags = fcntl(fd, F_GETFD);
778 int fdFlagsErrno = fdFlags == -1 ? errno : 0;
779 int dupFlags = fcntl(dupFd, F_GETFD);
780 int dupFlagsErrno = dupFlags == -1 ? errno : 0;
781 if (dupFd < 0 || fdFlags == -1 || dupFlags == -1) {
782 ALOGE("Parcel::writeDupFileDescriptor failed:\n"
783 " fd=%d flags=%d err=%d(%s)\n"
784 " dupFd=%d dupErr=%d(%s) flags=%d err=%d(%s)",
785 fd, fdFlags, fdFlagsErrno, strerror(fdFlagsErrno),
786 dupFd, dupErrno, strerror(dupErrno),
787 dupFlags, dupFlagsErrno, strerror(dupFlagsErrno));
788 if (fd < 0 || fdFlags == -1) {
789 CallStack(LOG_TAG);
790 }
791 return -errno;
792 }
793 }
794
Jeff Brownd341c712011-11-04 20:19:33 -0700795 if (dupFd < 0) {
796 return -errno;
797 }
798 status_t err = writeFileDescriptor(dupFd, true /*takeOwnership*/);
799 if (err) {
800 close(dupFd);
801 }
802 return err;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700803}
804
Mike Lockwoodcbe36fe2013-09-05 08:41:06 -0700805// WARNING: This method must stay in sync with
806// Parcelable.Creator<ParcelFileDescriptor> CREATOR
807// in frameworks/base/core/java/android/os/ParcelFileDescriptor.java
808status_t Parcel::writeParcelFileDescriptor(int fd, int commChannel) {
809 status_t status;
810
811 if (fd < 0) {
812 status = writeInt32(0); // ParcelFileDescriptor is null
813 if (status) return status;
814 } else {
815 status = writeInt32(1); // ParcelFileDescriptor is not null
816 if (status) return status;
817 status = writeDupFileDescriptor(fd);
818 if (status) return status;
819 if (commChannel < 0) {
820 status = writeInt32(0); // commChannel is null
821 if (status) return status;
822 } else {
823 status = writeInt32(1); // commChannel is not null
824 if (status) return status;
825 status = writeDupFileDescriptor(commChannel);
826 }
827 }
828 return status;
829}
830
Jeff Brown5707dbf2011-09-23 21:17:56 -0700831status_t Parcel::writeBlob(size_t len, WritableBlob* outBlob)
832{
833 status_t status;
834
835 if (!mAllowFds || len <= IN_PLACE_BLOB_LIMIT) {
Steve Block6807e592011-10-20 11:56:00 +0100836 ALOGV("writeBlob: write in place");
Jeff Brown5707dbf2011-09-23 21:17:56 -0700837 status = writeInt32(0);
838 if (status) return status;
839
840 void* ptr = writeInplace(len);
841 if (!ptr) return NO_MEMORY;
842
843 outBlob->init(false /*mapped*/, ptr, len);
844 return NO_ERROR;
845 }
846
Steve Block6807e592011-10-20 11:56:00 +0100847 ALOGV("writeBlob: write to ashmem");
Jeff Brown5707dbf2011-09-23 21:17:56 -0700848 int fd = ashmem_create_region("Parcel Blob", len);
849 if (fd < 0) return NO_MEMORY;
850
851 int result = ashmem_set_prot_region(fd, PROT_READ | PROT_WRITE);
852 if (result < 0) {
Jeff Brownec4e0062011-10-10 14:50:10 -0700853 status = result;
Jeff Brown5707dbf2011-09-23 21:17:56 -0700854 } else {
855 void* ptr = ::mmap(NULL, len, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
856 if (ptr == MAP_FAILED) {
857 status = -errno;
858 } else {
859 result = ashmem_set_prot_region(fd, PROT_READ);
860 if (result < 0) {
Jeff Brownec4e0062011-10-10 14:50:10 -0700861 status = result;
Jeff Brown5707dbf2011-09-23 21:17:56 -0700862 } else {
863 status = writeInt32(1);
864 if (!status) {
Jeff Brown93ff1f92011-11-04 19:01:44 -0700865 status = writeFileDescriptor(fd, true /*takeOwnership*/);
Jeff Brown5707dbf2011-09-23 21:17:56 -0700866 if (!status) {
867 outBlob->init(true /*mapped*/, ptr, len);
868 return NO_ERROR;
869 }
870 }
871 }
872 }
873 ::munmap(ptr, len);
874 }
875 ::close(fd);
876 return status;
877}
878
Mathias Agopiane1424282013-07-29 21:24:40 -0700879status_t Parcel::write(const FlattenableHelperInterface& val)
Mathias Agopian98e71dd2010-02-11 17:30:52 -0800880{
881 status_t err;
882
883 // size if needed
Mathias Agopiane1424282013-07-29 21:24:40 -0700884 const size_t len = val.getFlattenedSize();
885 const size_t fd_count = val.getFdCount();
Mathias Agopian98e71dd2010-02-11 17:30:52 -0800886
887 err = this->writeInt32(len);
888 if (err) return err;
889
890 err = this->writeInt32(fd_count);
891 if (err) return err;
892
893 // payload
Mathias Agopiane1424282013-07-29 21:24:40 -0700894 void* const buf = this->writeInplace(PAD_SIZE(len));
Mathias Agopian98e71dd2010-02-11 17:30:52 -0800895 if (buf == NULL)
896 return BAD_VALUE;
897
898 int* fds = NULL;
899 if (fd_count) {
900 fds = new int[fd_count];
901 }
902
903 err = val.flatten(buf, len, fds, fd_count);
904 for (size_t i=0 ; i<fd_count && err==NO_ERROR ; i++) {
905 err = this->writeDupFileDescriptor( fds[i] );
906 }
907
908 if (fd_count) {
909 delete [] fds;
910 }
911
912 return err;
913}
914
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700915status_t Parcel::writeObject(const flat_binder_object& val, bool nullMetaData)
916{
917 const bool enoughData = (mDataPos+sizeof(val)) <= mDataCapacity;
918 const bool enoughObjects = mObjectsSize < mObjectsCapacity;
919 if (enoughData && enoughObjects) {
920restart_write:
921 *reinterpret_cast<flat_binder_object*>(mData+mDataPos) = val;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700922
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700923 // Need to write meta-data?
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800924 if (nullMetaData || val.binder != 0) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700925 mObjects[mObjectsSize] = mDataPos;
926 acquire_object(ProcessState::self(), val, this);
927 mObjectsSize++;
928 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700929
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700930 // remember if it's a file descriptor
931 if (val.type == BINDER_TYPE_FD) {
Dianne Hackborn8938ed22011-09-28 23:19:47 -0400932 if (!mAllowFds) {
933 return FDS_NOT_ALLOWED;
934 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700935 mHasFds = mFdsKnown = true;
936 }
937
938 return finishWrite(sizeof(flat_binder_object));
939 }
940
941 if (!enoughData) {
942 const status_t err = growData(sizeof(val));
943 if (err != NO_ERROR) return err;
944 }
945 if (!enoughObjects) {
946 size_t newSize = ((mObjectsSize+2)*3)/2;
Christopher Tate8b643072016-11-03 13:32:41 -0700947 if (newSize*sizeof(binder_size_t) < mObjectsSize) return NO_MEMORY; // overflow
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800948 binder_size_t* objects = (binder_size_t*)realloc(mObjects, newSize*sizeof(binder_size_t));
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700949 if (objects == NULL) return NO_MEMORY;
950 mObjects = objects;
951 mObjectsCapacity = newSize;
952 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700953
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700954 goto restart_write;
955}
956
Brad Fitzpatrick837a0d02010-07-13 15:33:35 -0700957status_t Parcel::writeNoException()
958{
959 return writeInt32(0);
960}
961
Colin Cross6f4f3ab2014-02-05 17:42:44 -0800962void Parcel::remove(size_t /*start*/, size_t /*amt*/)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700963{
964 LOG_ALWAYS_FATAL("Parcel::remove() not yet implemented!");
965}
966
967status_t Parcel::read(void* outData, size_t len) const
968{
Kenny Root5b61ad22014-03-17 13:18:16 -0700969 if ((mDataPos+PAD_SIZE(len)) >= mDataPos && (mDataPos+PAD_SIZE(len)) <= mDataSize
970 && len <= PAD_SIZE(len)) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700971 memcpy(outData, mData+mDataPos, len);
972 mDataPos += PAD_SIZE(len);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700973 ALOGV("read Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700974 return NO_ERROR;
975 }
976 return NOT_ENOUGH_DATA;
977}
978
979const void* Parcel::readInplace(size_t len) const
980{
Kenny Root5b61ad22014-03-17 13:18:16 -0700981 if ((mDataPos+PAD_SIZE(len)) >= mDataPos && (mDataPos+PAD_SIZE(len)) <= mDataSize
982 && len <= PAD_SIZE(len)) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700983 const void* data = mData+mDataPos;
984 mDataPos += PAD_SIZE(len);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700985 ALOGV("readInplace Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700986 return data;
987 }
988 return NULL;
989}
990
Andreas Huber84a6d042009-08-17 13:33:27 -0700991template<class T>
992status_t Parcel::readAligned(T *pArg) const {
993 COMPILE_TIME_ASSERT_FUNCTION_SCOPE(PAD_SIZE(sizeof(T)) == sizeof(T));
994
995 if ((mDataPos+sizeof(T)) <= mDataSize) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700996 const void* data = mData+mDataPos;
Andreas Huber84a6d042009-08-17 13:33:27 -0700997 mDataPos += sizeof(T);
998 *pArg = *reinterpret_cast<const T*>(data);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700999 return NO_ERROR;
1000 } else {
1001 return NOT_ENOUGH_DATA;
1002 }
1003}
1004
Andreas Huber84a6d042009-08-17 13:33:27 -07001005template<class T>
1006T Parcel::readAligned() const {
1007 T result;
1008 if (readAligned(&result) != NO_ERROR) {
1009 result = 0;
1010 }
1011
1012 return result;
1013}
1014
1015template<class T>
1016status_t Parcel::writeAligned(T val) {
1017 COMPILE_TIME_ASSERT_FUNCTION_SCOPE(PAD_SIZE(sizeof(T)) == sizeof(T));
1018
1019 if ((mDataPos+sizeof(val)) <= mDataCapacity) {
1020restart_write:
1021 *reinterpret_cast<T*>(mData+mDataPos) = val;
1022 return finishWrite(sizeof(val));
1023 }
1024
1025 status_t err = growData(sizeof(val));
1026 if (err == NO_ERROR) goto restart_write;
1027 return err;
1028}
1029
1030status_t Parcel::readInt32(int32_t *pArg) const
1031{
1032 return readAligned(pArg);
1033}
1034
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001035int32_t Parcel::readInt32() const
1036{
Andreas Huber84a6d042009-08-17 13:33:27 -07001037 return readAligned<int32_t>();
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001038}
1039
1040
1041status_t Parcel::readInt64(int64_t *pArg) const
1042{
Andreas Huber84a6d042009-08-17 13:33:27 -07001043 return readAligned(pArg);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001044}
1045
1046
1047int64_t Parcel::readInt64() const
1048{
Andreas Huber84a6d042009-08-17 13:33:27 -07001049 return readAligned<int64_t>();
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001050}
1051
Serban Constantinescuf683e012013-11-05 16:53:55 +00001052status_t Parcel::readPointer(uintptr_t *pArg) const
1053{
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001054 status_t ret;
1055 binder_uintptr_t ptr;
1056 ret = readAligned(&ptr);
1057 if (!ret)
1058 *pArg = ptr;
1059 return ret;
Serban Constantinescuf683e012013-11-05 16:53:55 +00001060}
1061
1062uintptr_t Parcel::readPointer() const
1063{
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001064 return readAligned<binder_uintptr_t>();
Serban Constantinescuf683e012013-11-05 16:53:55 +00001065}
1066
1067
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001068status_t Parcel::readFloat(float *pArg) const
1069{
Andreas Huber84a6d042009-08-17 13:33:27 -07001070 return readAligned(pArg);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001071}
1072
1073
1074float Parcel::readFloat() const
1075{
Andreas Huber84a6d042009-08-17 13:33:27 -07001076 return readAligned<float>();
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001077}
1078
Douglas Leungcc1a4bb2013-01-11 15:00:55 -08001079#if defined(__mips__) && defined(__mips_hard_float)
1080
1081status_t Parcel::readDouble(double *pArg) const
1082{
1083 union {
1084 double d;
1085 unsigned long long ll;
1086 } u;
Narayan Kamath2c68d382014-06-04 15:04:29 +01001087 u.d = 0;
Douglas Leungcc1a4bb2013-01-11 15:00:55 -08001088 status_t status;
1089 status = readAligned(&u.ll);
1090 *pArg = u.d;
1091 return status;
1092}
1093
1094double Parcel::readDouble() const
1095{
1096 union {
1097 double d;
1098 unsigned long long ll;
1099 } u;
1100 u.ll = readAligned<unsigned long long>();
1101 return u.d;
1102}
1103
1104#else
1105
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001106status_t Parcel::readDouble(double *pArg) const
1107{
Andreas Huber84a6d042009-08-17 13:33:27 -07001108 return readAligned(pArg);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001109}
1110
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001111double Parcel::readDouble() const
1112{
Andreas Huber84a6d042009-08-17 13:33:27 -07001113 return readAligned<double>();
1114}
1115
Douglas Leungcc1a4bb2013-01-11 15:00:55 -08001116#endif
1117
Andreas Huber84a6d042009-08-17 13:33:27 -07001118status_t Parcel::readIntPtr(intptr_t *pArg) const
1119{
1120 return readAligned(pArg);
1121}
1122
1123
1124intptr_t Parcel::readIntPtr() const
1125{
1126 return readAligned<intptr_t>();
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001127}
1128
1129
1130const char* Parcel::readCString() const
1131{
1132 const size_t avail = mDataSize-mDataPos;
1133 if (avail > 0) {
1134 const char* str = reinterpret_cast<const char*>(mData+mDataPos);
1135 // is the string's trailing NUL within the parcel's valid bounds?
1136 const char* eos = reinterpret_cast<const char*>(memchr(str, 0, avail));
1137 if (eos) {
1138 const size_t len = eos - str;
1139 mDataPos += PAD_SIZE(len+1);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001140 ALOGV("readCString Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001141 return str;
1142 }
1143 }
1144 return NULL;
1145}
1146
1147String8 Parcel::readString8() const
1148{
1149 int32_t size = readInt32();
1150 // watch for potential int overflow adding 1 for trailing NUL
1151 if (size > 0 && size < INT32_MAX) {
1152 const char* str = (const char*)readInplace(size+1);
1153 if (str) return String8(str, size);
1154 }
1155 return String8();
1156}
1157
1158String16 Parcel::readString16() const
1159{
1160 size_t len;
1161 const char16_t* str = readString16Inplace(&len);
1162 if (str) return String16(str, len);
Steve Blocke6f43dd2012-01-06 19:20:56 +00001163 ALOGE("Reading a NULL string not supported here.");
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001164 return String16();
1165}
1166
1167const char16_t* Parcel::readString16Inplace(size_t* outLen) const
1168{
1169 int32_t size = readInt32();
1170 // watch for potential int overflow from size+1
1171 if (size >= 0 && size < INT32_MAX) {
1172 *outLen = size;
1173 const char16_t* str = (const char16_t*)readInplace((size+1)*sizeof(char16_t));
1174 if (str != NULL) {
1175 return str;
1176 }
1177 }
1178 *outLen = 0;
1179 return NULL;
1180}
1181
1182sp<IBinder> Parcel::readStrongBinder() const
1183{
1184 sp<IBinder> val;
1185 unflatten_binder(ProcessState::self(), *this, &val);
1186 return val;
1187}
1188
1189wp<IBinder> Parcel::readWeakBinder() const
1190{
1191 wp<IBinder> val;
1192 unflatten_binder(ProcessState::self(), *this, &val);
1193 return val;
1194}
1195
Brad Fitzpatrick837a0d02010-07-13 15:33:35 -07001196int32_t Parcel::readExceptionCode() const
1197{
1198 int32_t exception_code = readAligned<int32_t>();
Brad Fitzpatrickd36f4a52010-07-12 11:05:38 -07001199 if (exception_code == EX_HAS_REPLY_HEADER) {
Magnus Strandberg1ba24572011-05-03 15:44:00 +02001200 int32_t header_start = dataPosition();
Brad Fitzpatrickd36f4a52010-07-12 11:05:38 -07001201 int32_t header_size = readAligned<int32_t>();
1202 // Skip over fat responses headers. Not used (or propagated) in
1203 // native code
Magnus Strandberg1ba24572011-05-03 15:44:00 +02001204 setDataPosition(header_start + header_size);
Brad Fitzpatrickd36f4a52010-07-12 11:05:38 -07001205 // And fat response headers are currently only used when there are no
1206 // exceptions, so return no error:
1207 return 0;
1208 }
Brad Fitzpatrick837a0d02010-07-13 15:33:35 -07001209 return exception_code;
1210}
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001211
Mathias Agopiana47f02a2009-05-21 16:29:38 -07001212native_handle* Parcel::readNativeHandle() const
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001213{
1214 int numFds, numInts;
1215 status_t err;
1216 err = readInt32(&numFds);
1217 if (err != NO_ERROR) return 0;
1218 err = readInt32(&numInts);
1219 if (err != NO_ERROR) return 0;
1220
Mathias Agopiana47f02a2009-05-21 16:29:38 -07001221 native_handle* h = native_handle_create(numFds, numInts);
Adam Lesinski4ff0cb42015-05-12 17:35:48 -07001222 if (!h) {
1223 return 0;
1224 }
1225
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001226 for (int i=0 ; err==NO_ERROR && i<numFds ; i++) {
Rebecca Schultz Zavin360211f2009-02-13 16:34:38 -08001227 h->data[i] = dup(readFileDescriptor());
Marco Nelissen1de79662016-04-26 08:44:09 -07001228 if (h->data[i] < 0) {
1229 for (int j = 0; j < i; j++) {
1230 close(h->data[j]);
1231 }
1232 native_handle_delete(h);
1233 return 0;
1234 }
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001235 }
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001236 err = read(h->data + numFds, sizeof(int)*numInts);
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001237 if (err != NO_ERROR) {
Mathias Agopiana47f02a2009-05-21 16:29:38 -07001238 native_handle_close(h);
1239 native_handle_delete(h);
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001240 h = 0;
1241 }
1242 return h;
1243}
1244
1245
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001246int Parcel::readFileDescriptor() const
1247{
1248 const flat_binder_object* flat = readObject(true);
1249 if (flat) {
1250 switch (flat->type) {
1251 case BINDER_TYPE_FD:
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001252 //ALOGI("Returning file descriptor %ld from parcel %p", flat->handle, this);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001253 return flat->handle;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001254 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001255 }
1256 return BAD_TYPE;
1257}
1258
Mike Lockwoodcbe36fe2013-09-05 08:41:06 -07001259// WARNING: This method must stay in sync with writeToParcel()
1260// in frameworks/base/core/java/android/os/ParcelFileDescriptor.java
1261int Parcel::readParcelFileDescriptor(int& outCommChannel) const {
1262 int fd;
1263 outCommChannel = -1;
1264
1265 if (readInt32() == 0) {
1266 fd = -1;
1267 } else {
1268 fd = readFileDescriptor();
1269 if (fd >= 0 && readInt32() != 0) {
1270 outCommChannel = readFileDescriptor();
1271 }
1272 }
1273 return fd;
1274}
1275
Jeff Brown5707dbf2011-09-23 21:17:56 -07001276status_t Parcel::readBlob(size_t len, ReadableBlob* outBlob) const
1277{
1278 int32_t useAshmem;
1279 status_t status = readInt32(&useAshmem);
1280 if (status) return status;
1281
1282 if (!useAshmem) {
Steve Block6807e592011-10-20 11:56:00 +01001283 ALOGV("readBlob: read in place");
Jeff Brown5707dbf2011-09-23 21:17:56 -07001284 const void* ptr = readInplace(len);
1285 if (!ptr) return BAD_VALUE;
1286
1287 outBlob->init(false /*mapped*/, const_cast<void*>(ptr), len);
1288 return NO_ERROR;
1289 }
1290
Steve Block6807e592011-10-20 11:56:00 +01001291 ALOGV("readBlob: read from ashmem");
Jeff Brown5707dbf2011-09-23 21:17:56 -07001292 int fd = readFileDescriptor();
1293 if (fd == int(BAD_TYPE)) return BAD_VALUE;
1294
1295 void* ptr = ::mmap(NULL, len, PROT_READ, MAP_SHARED, fd, 0);
Narayan Kamath9ea09752014-10-08 17:35:45 +01001296 if (ptr == MAP_FAILED) return NO_MEMORY;
Jeff Brown5707dbf2011-09-23 21:17:56 -07001297
1298 outBlob->init(true /*mapped*/, ptr, len);
1299 return NO_ERROR;
1300}
1301
Mathias Agopiane1424282013-07-29 21:24:40 -07001302status_t Parcel::read(FlattenableHelperInterface& val) const
Mathias Agopian98e71dd2010-02-11 17:30:52 -08001303{
1304 // size
1305 const size_t len = this->readInt32();
1306 const size_t fd_count = this->readInt32();
1307
1308 // payload
Mathias Agopiane1424282013-07-29 21:24:40 -07001309 void const* const buf = this->readInplace(PAD_SIZE(len));
Mathias Agopian98e71dd2010-02-11 17:30:52 -08001310 if (buf == NULL)
1311 return BAD_VALUE;
1312
1313 int* fds = NULL;
1314 if (fd_count) {
1315 fds = new int[fd_count];
1316 }
1317
1318 status_t err = NO_ERROR;
1319 for (size_t i=0 ; i<fd_count && err==NO_ERROR ; i++) {
Michael Lentine36273c92014-10-02 09:11:04 -07001320 int oldfd = this->readFileDescriptor();
1321 fds[i] = dup(oldfd);
Jun Jiangabf8a2c2014-04-29 14:22:10 +08001322 if (fds[i] < 0) {
Michael Lentine36273c92014-10-02 09:11:04 -07001323 int dupErrno = errno;
Jun Jiangabf8a2c2014-04-29 14:22:10 +08001324 err = BAD_VALUE;
Michael Lentine36273c92014-10-02 09:11:04 -07001325 int flags = fcntl(oldfd, F_GETFD);
1326 int fcntlErrno = errno;
1327 const flat_binder_object* flat = readObject(true);
1328 ALOGE("dup failed in Parcel::read, fd %zu of %zu\n"
1329 " dup(%d) = %d [errno: %d (%s)]\n"
1330 " fcntl(%d, F_GETFD) = %d [errno: %d (%s)]\n"
1331 " flat %p type %d",
1332 i, fd_count,
1333 oldfd, fds[i], dupErrno, strerror(dupErrno),
1334 oldfd, flags, fcntlErrno, strerror(fcntlErrno),
1335 flat, flat ? flat->type : 0);
Jesse Hallccf851f2014-10-06 09:49:45 -07001336 CallStack(LOG_TAG);
Jun Jiangabf8a2c2014-04-29 14:22:10 +08001337 }
Mathias Agopian98e71dd2010-02-11 17:30:52 -08001338 }
1339
1340 if (err == NO_ERROR) {
1341 err = val.unflatten(buf, len, fds, fd_count);
1342 }
1343
1344 if (fd_count) {
1345 delete [] fds;
1346 }
1347
1348 return err;
1349}
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001350const flat_binder_object* Parcel::readObject(bool nullMetaData) const
1351{
1352 const size_t DPOS = mDataPos;
1353 if ((DPOS+sizeof(flat_binder_object)) <= mDataSize) {
1354 const flat_binder_object* obj
1355 = reinterpret_cast<const flat_binder_object*>(mData+DPOS);
1356 mDataPos = DPOS + sizeof(flat_binder_object);
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001357 if (!nullMetaData && (obj->cookie == 0 && obj->binder == 0)) {
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001358 // When transferring a NULL object, we don't write it into
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001359 // the object list, so we don't want to check for it when
1360 // reading.
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001361 ALOGV("readObject Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001362 return obj;
1363 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001364
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001365 // Ensure that this object is valid...
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001366 binder_size_t* const OBJS = mObjects;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001367 const size_t N = mObjectsSize;
1368 size_t opos = mNextObjectHint;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001369
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001370 if (N > 0) {
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001371 ALOGV("Parcel %p looking for obj at %zu, hint=%zu",
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001372 this, DPOS, opos);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001373
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001374 // Start at the current hint position, looking for an object at
1375 // the current data position.
1376 if (opos < N) {
1377 while (opos < (N-1) && OBJS[opos] < DPOS) {
1378 opos++;
1379 }
1380 } else {
1381 opos = N-1;
1382 }
1383 if (OBJS[opos] == DPOS) {
1384 // Found it!
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001385 ALOGV("Parcel %p found obj %zu at index %zu with forward search",
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001386 this, DPOS, opos);
1387 mNextObjectHint = opos+1;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001388 ALOGV("readObject Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001389 return obj;
1390 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001391
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001392 // Look backwards for it...
1393 while (opos > 0 && OBJS[opos] > DPOS) {
1394 opos--;
1395 }
1396 if (OBJS[opos] == DPOS) {
1397 // Found it!
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001398 ALOGV("Parcel %p found obj %zu at index %zu with backward search",
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001399 this, DPOS, opos);
1400 mNextObjectHint = opos+1;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001401 ALOGV("readObject Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001402 return obj;
1403 }
1404 }
Colin Cross6f4f3ab2014-02-05 17:42:44 -08001405 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 -07001406 this, DPOS);
1407 }
1408 return NULL;
1409}
1410
1411void Parcel::closeFileDescriptors()
1412{
1413 size_t i = mObjectsSize;
1414 if (i > 0) {
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001415 //ALOGI("Closing file descriptors for %zu objects...", i);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001416 }
1417 while (i > 0) {
1418 i--;
1419 const flat_binder_object* flat
1420 = reinterpret_cast<flat_binder_object*>(mData+mObjects[i]);
1421 if (flat->type == BINDER_TYPE_FD) {
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001422 //ALOGI("Closing fd: %ld", flat->handle);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001423 close(flat->handle);
1424 }
1425 }
1426}
1427
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001428uintptr_t Parcel::ipcData() const
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001429{
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001430 return reinterpret_cast<uintptr_t>(mData);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001431}
1432
1433size_t Parcel::ipcDataSize() const
1434{
1435 return (mDataSize > mDataPos ? mDataSize : mDataPos);
1436}
1437
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001438uintptr_t Parcel::ipcObjects() const
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001439{
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001440 return reinterpret_cast<uintptr_t>(mObjects);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001441}
1442
1443size_t Parcel::ipcObjectsCount() const
1444{
1445 return mObjectsSize;
1446}
1447
1448void Parcel::ipcSetDataReference(const uint8_t* data, size_t dataSize,
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001449 const binder_size_t* objects, size_t objectsCount, release_func relFunc, void* relCookie)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001450{
Arve Hjønnevåg6f286112014-02-19 20:42:13 -08001451 binder_size_t minOffset = 0;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001452 freeDataNoInit();
1453 mError = NO_ERROR;
1454 mData = const_cast<uint8_t*>(data);
1455 mDataSize = mDataCapacity = dataSize;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001456 //ALOGI("setDataReference Setting data size of %p to %lu (pid=%d)", this, mDataSize, getpid());
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001457 mDataPos = 0;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001458 ALOGV("setDataReference Setting data pos of %p to %zu", this, mDataPos);
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001459 mObjects = const_cast<binder_size_t*>(objects);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001460 mObjectsSize = mObjectsCapacity = objectsCount;
1461 mNextObjectHint = 0;
1462 mOwner = relFunc;
1463 mOwnerCookie = relCookie;
Arve Hjønnevågf50b9ea2014-02-13 19:22:08 -08001464 for (size_t i = 0; i < mObjectsSize; i++) {
Arve Hjønnevåg6f286112014-02-19 20:42:13 -08001465 binder_size_t offset = mObjects[i];
Arve Hjønnevågf50b9ea2014-02-13 19:22:08 -08001466 if (offset < minOffset) {
Arve Hjønnevåg6f286112014-02-19 20:42:13 -08001467 ALOGE("%s: bad object offset %"PRIu64" < %"PRIu64"\n",
1468 __func__, (uint64_t)offset, (uint64_t)minOffset);
Arve Hjønnevågf50b9ea2014-02-13 19:22:08 -08001469 mObjectsSize = 0;
1470 break;
1471 }
1472 minOffset = offset + sizeof(flat_binder_object);
1473 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001474 scanForFds();
1475}
1476
Colin Cross6f4f3ab2014-02-05 17:42:44 -08001477void Parcel::print(TextOutput& to, uint32_t /*flags*/) const
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001478{
1479 to << "Parcel(";
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001480
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001481 if (errorCheck() != NO_ERROR) {
1482 const status_t err = errorCheck();
Colin Cross6f4f3ab2014-02-05 17:42:44 -08001483 to << "Error: " << (void*)(intptr_t)err << " \"" << strerror(-err) << "\"";
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001484 } else if (dataSize() > 0) {
1485 const uint8_t* DATA = data();
1486 to << indent << HexDump(DATA, dataSize()) << dedent;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001487 const binder_size_t* OBJS = objects();
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001488 const size_t N = objectsCount();
1489 for (size_t i=0; i<N; i++) {
1490 const flat_binder_object* flat
1491 = reinterpret_cast<const flat_binder_object*>(DATA+OBJS[i]);
1492 to << endl << "Object #" << i << " @ " << (void*)OBJS[i] << ": "
1493 << TypeCode(flat->type & 0x7f7f7f00)
1494 << " = " << flat->binder;
1495 }
1496 } else {
1497 to << "NULL";
1498 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001499
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001500 to << ")";
1501}
1502
1503void Parcel::releaseObjects()
1504{
1505 const sp<ProcessState> proc(ProcessState::self());
1506 size_t i = mObjectsSize;
1507 uint8_t* const data = mData;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001508 binder_size_t* const objects = mObjects;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001509 while (i > 0) {
1510 i--;
1511 const flat_binder_object* flat
1512 = reinterpret_cast<flat_binder_object*>(data+objects[i]);
1513 release_object(proc, *flat, this);
1514 }
1515}
1516
1517void Parcel::acquireObjects()
1518{
1519 const sp<ProcessState> proc(ProcessState::self());
1520 size_t i = mObjectsSize;
1521 uint8_t* const data = mData;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001522 binder_size_t* const objects = mObjects;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001523 while (i > 0) {
1524 i--;
1525 const flat_binder_object* flat
1526 = reinterpret_cast<flat_binder_object*>(data+objects[i]);
1527 acquire_object(proc, *flat, this);
1528 }
1529}
1530
1531void Parcel::freeData()
1532{
1533 freeDataNoInit();
1534 initState();
1535}
1536
1537void Parcel::freeDataNoInit()
1538{
1539 if (mOwner) {
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001540 //ALOGI("Freeing data ref of %p (pid=%d)", this, getpid());
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001541 mOwner(this, mData, mDataSize, mObjects, mObjectsSize, mOwnerCookie);
1542 } else {
1543 releaseObjects();
1544 if (mData) free(mData);
1545 if (mObjects) free(mObjects);
1546 }
1547}
1548
1549status_t Parcel::growData(size_t len)
1550{
1551 size_t newSize = ((mDataSize+len)*3)/2;
1552 return (newSize <= mDataSize)
1553 ? (status_t) NO_MEMORY
1554 : continueWrite(newSize);
1555}
1556
1557status_t Parcel::restartWrite(size_t desired)
1558{
1559 if (mOwner) {
1560 freeData();
1561 return continueWrite(desired);
1562 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001563
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001564 uint8_t* data = (uint8_t*)realloc(mData, desired);
1565 if (!data && desired > mDataCapacity) {
1566 mError = NO_MEMORY;
1567 return NO_MEMORY;
1568 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001569
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001570 releaseObjects();
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001571
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001572 if (data) {
1573 mData = data;
1574 mDataCapacity = desired;
1575 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001576
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001577 mDataSize = mDataPos = 0;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001578 ALOGV("restartWrite Setting data size of %p to %zu", this, mDataSize);
1579 ALOGV("restartWrite Setting data pos of %p to %zu", this, mDataPos);
1580
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001581 free(mObjects);
1582 mObjects = NULL;
1583 mObjectsSize = mObjectsCapacity = 0;
1584 mNextObjectHint = 0;
1585 mHasFds = false;
1586 mFdsKnown = true;
Dianne Hackborn8938ed22011-09-28 23:19:47 -04001587 mAllowFds = true;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001588
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001589 return NO_ERROR;
1590}
1591
1592status_t Parcel::continueWrite(size_t desired)
1593{
1594 // If shrinking, first adjust for any objects that appear
1595 // after the new data size.
1596 size_t objectsSize = mObjectsSize;
1597 if (desired < mDataSize) {
1598 if (desired == 0) {
1599 objectsSize = 0;
1600 } else {
1601 while (objectsSize > 0) {
1602 if (mObjects[objectsSize-1] < desired)
1603 break;
1604 objectsSize--;
1605 }
1606 }
1607 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001608
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001609 if (mOwner) {
1610 // If the size is going to zero, just release the owner's data.
1611 if (desired == 0) {
1612 freeData();
1613 return NO_ERROR;
1614 }
1615
1616 // If there is a different owner, we need to take
1617 // posession.
1618 uint8_t* data = (uint8_t*)malloc(desired);
1619 if (!data) {
1620 mError = NO_MEMORY;
1621 return NO_MEMORY;
1622 }
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001623 binder_size_t* objects = NULL;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001624
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001625 if (objectsSize) {
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001626 objects = (binder_size_t*)malloc(objectsSize*sizeof(binder_size_t));
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001627 if (!objects) {
Hyejin Kim3f727c02013-03-09 11:28:54 +09001628 free(data);
1629
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001630 mError = NO_MEMORY;
1631 return NO_MEMORY;
1632 }
1633
1634 // Little hack to only acquire references on objects
1635 // we will be keeping.
1636 size_t oldObjectsSize = mObjectsSize;
1637 mObjectsSize = objectsSize;
1638 acquireObjects();
1639 mObjectsSize = oldObjectsSize;
1640 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001641
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001642 if (mData) {
1643 memcpy(data, mData, mDataSize < desired ? mDataSize : desired);
1644 }
1645 if (objects && mObjects) {
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001646 memcpy(objects, mObjects, objectsSize*sizeof(binder_size_t));
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001647 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001648 //ALOGI("Freeing data ref of %p (pid=%d)", this, getpid());
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001649 mOwner(this, mData, mDataSize, mObjects, mObjectsSize, mOwnerCookie);
1650 mOwner = NULL;
1651
1652 mData = data;
1653 mObjects = objects;
1654 mDataSize = (mDataSize < desired) ? mDataSize : desired;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001655 ALOGV("continueWrite Setting data size of %p to %zu", this, mDataSize);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001656 mDataCapacity = desired;
1657 mObjectsSize = mObjectsCapacity = objectsSize;
1658 mNextObjectHint = 0;
1659
1660 } else if (mData) {
1661 if (objectsSize < mObjectsSize) {
1662 // Need to release refs on any objects we are dropping.
1663 const sp<ProcessState> proc(ProcessState::self());
1664 for (size_t i=objectsSize; i<mObjectsSize; i++) {
1665 const flat_binder_object* flat
1666 = reinterpret_cast<flat_binder_object*>(mData+mObjects[i]);
1667 if (flat->type == BINDER_TYPE_FD) {
1668 // will need to rescan because we may have lopped off the only FDs
1669 mFdsKnown = false;
1670 }
1671 release_object(proc, *flat, this);
1672 }
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001673 binder_size_t* objects =
1674 (binder_size_t*)realloc(mObjects, objectsSize*sizeof(binder_size_t));
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001675 if (objects) {
1676 mObjects = objects;
1677 }
1678 mObjectsSize = objectsSize;
1679 mNextObjectHint = 0;
1680 }
1681
1682 // We own the data, so we can just do a realloc().
1683 if (desired > mDataCapacity) {
1684 uint8_t* data = (uint8_t*)realloc(mData, desired);
1685 if (data) {
1686 mData = data;
1687 mDataCapacity = desired;
1688 } else if (desired > mDataCapacity) {
1689 mError = NO_MEMORY;
1690 return NO_MEMORY;
1691 }
1692 } else {
Dianne Hackborn97e2bcd2011-04-13 18:15:56 -07001693 if (mDataSize > desired) {
1694 mDataSize = desired;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001695 ALOGV("continueWrite Setting data size of %p to %zu", this, mDataSize);
Dianne Hackborn97e2bcd2011-04-13 18:15:56 -07001696 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001697 if (mDataPos > desired) {
1698 mDataPos = desired;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001699 ALOGV("continueWrite Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001700 }
1701 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001702
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001703 } else {
1704 // This is the first data. Easy!
1705 uint8_t* data = (uint8_t*)malloc(desired);
1706 if (!data) {
1707 mError = NO_MEMORY;
1708 return NO_MEMORY;
1709 }
Hyejin Kim3f727c02013-03-09 11:28:54 +09001710
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001711 if(!(mDataCapacity == 0 && mObjects == NULL
1712 && mObjectsCapacity == 0)) {
Colin Cross6f4f3ab2014-02-05 17:42:44 -08001713 ALOGE("continueWrite: %zu/%p/%zu/%zu", mDataCapacity, mObjects, mObjectsCapacity, desired);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001714 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001715
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001716 mData = data;
1717 mDataSize = mDataPos = 0;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001718 ALOGV("continueWrite Setting data size of %p to %zu", this, mDataSize);
1719 ALOGV("continueWrite Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001720 mDataCapacity = desired;
1721 }
1722
1723 return NO_ERROR;
1724}
1725
1726void Parcel::initState()
1727{
1728 mError = NO_ERROR;
1729 mData = 0;
1730 mDataSize = 0;
1731 mDataCapacity = 0;
1732 mDataPos = 0;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001733 ALOGV("initState Setting data size of %p to %zu", this, mDataSize);
1734 ALOGV("initState Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001735 mObjects = NULL;
1736 mObjectsSize = 0;
1737 mObjectsCapacity = 0;
1738 mNextObjectHint = 0;
1739 mHasFds = false;
1740 mFdsKnown = true;
Dianne Hackborn8938ed22011-09-28 23:19:47 -04001741 mAllowFds = true;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001742 mOwner = NULL;
1743}
1744
1745void Parcel::scanForFds() const
1746{
1747 bool hasFds = false;
1748 for (size_t i=0; i<mObjectsSize; i++) {
1749 const flat_binder_object* flat
1750 = reinterpret_cast<const flat_binder_object*>(mData + mObjects[i]);
1751 if (flat->type == BINDER_TYPE_FD) {
1752 hasFds = true;
1753 break;
1754 }
1755 }
1756 mHasFds = hasFds;
1757 mFdsKnown = true;
1758}
1759
Jeff Brown5707dbf2011-09-23 21:17:56 -07001760// --- Parcel::Blob ---
1761
1762Parcel::Blob::Blob() :
1763 mMapped(false), mData(NULL), mSize(0) {
1764}
1765
1766Parcel::Blob::~Blob() {
1767 release();
1768}
1769
1770void Parcel::Blob::release() {
1771 if (mMapped && mData) {
1772 ::munmap(mData, mSize);
1773 }
1774 clear();
1775}
1776
1777void Parcel::Blob::init(bool mapped, void* data, size_t size) {
1778 mMapped = mapped;
1779 mData = data;
1780 mSize = size;
1781}
1782
1783void Parcel::Blob::clear() {
1784 mMapped = false;
1785 mData = NULL;
1786 mSize = 0;
1787}
1788
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001789}; // namespace android