blob: 27a50a6a8c577137bd28ea849ebcae9f9180be4d [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
Arve Hjønnevågf50b9ea2014-02-13 19:22:08 -080039#include <inttypes.h>
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -070040#include <stdio.h>
41#include <stdlib.h>
42#include <stdint.h>
Jeff Brown5707dbf2011-09-23 21:17:56 -070043#include <sys/mman.h>
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -070044
45#ifndef INT32_MAX
46#define INT32_MAX ((int32_t)(2147483647))
47#endif
48
49#define LOG_REFS(...)
Steve Block9f760152011-10-12 17:27:03 +010050//#define LOG_REFS(...) ALOG(LOG_DEBUG, "Parcel", __VA_ARGS__)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -070051
52// ---------------------------------------------------------------------------
53
54#define PAD_SIZE(s) (((s)+3)&~3)
55
Brad Fitzpatricka877cd82010-07-07 16:06:39 -070056// Note: must be kept in sync with android/os/StrictMode.java's PENALTY_GATHER
57#define STRICT_MODE_PENALTY_GATHER 0x100
58
Brad Fitzpatrickd36f4a52010-07-12 11:05:38 -070059// Note: must be kept in sync with android/os/Parcel.java's EX_HAS_REPLY_HEADER
60#define EX_HAS_REPLY_HEADER -128
61
Jeff Brown5707dbf2011-09-23 21:17:56 -070062// Maximum size of a blob to transfer in-place.
63static const size_t IN_PLACE_BLOB_LIMIT = 40 * 1024;
64
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -070065// XXX This can be made public if we want to provide
66// support for typed data.
67struct small_flat_data
68{
69 uint32_t type;
70 uint32_t data;
71};
72
73namespace android {
74
75void acquire_object(const sp<ProcessState>& proc,
76 const flat_binder_object& obj, const void* who)
77{
78 switch (obj.type) {
79 case BINDER_TYPE_BINDER:
80 if (obj.binder) {
81 LOG_REFS("Parcel %p acquiring reference on local %p", who, obj.cookie);
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -080082 reinterpret_cast<IBinder*>(obj.cookie)->incStrong(who);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -070083 }
84 return;
85 case BINDER_TYPE_WEAK_BINDER:
86 if (obj.binder)
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -080087 reinterpret_cast<RefBase::weakref_type*>(obj.binder)->incWeak(who);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -070088 return;
89 case BINDER_TYPE_HANDLE: {
90 const sp<IBinder> b = proc->getStrongProxyForHandle(obj.handle);
91 if (b != NULL) {
92 LOG_REFS("Parcel %p acquiring reference on remote %p", who, b.get());
93 b->incStrong(who);
94 }
95 return;
96 }
97 case BINDER_TYPE_WEAK_HANDLE: {
98 const wp<IBinder> b = proc->getWeakProxyForHandle(obj.handle);
99 if (b != NULL) b.get_refs()->incWeak(who);
100 return;
101 }
102 case BINDER_TYPE_FD: {
103 // intentionally blank -- nothing to do to acquire this, but we do
104 // recognize it as a legitimate object type.
105 return;
106 }
107 }
108
Colin Cross6f4f3ab2014-02-05 17:42:44 -0800109 ALOGD("Invalid object type 0x%08x", obj.type);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700110}
111
112void release_object(const sp<ProcessState>& proc,
113 const flat_binder_object& obj, const void* who)
114{
115 switch (obj.type) {
116 case BINDER_TYPE_BINDER:
117 if (obj.binder) {
118 LOG_REFS("Parcel %p releasing reference on local %p", who, obj.cookie);
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800119 reinterpret_cast<IBinder*>(obj.cookie)->decStrong(who);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700120 }
121 return;
122 case BINDER_TYPE_WEAK_BINDER:
123 if (obj.binder)
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800124 reinterpret_cast<RefBase::weakref_type*>(obj.binder)->decWeak(who);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700125 return;
126 case BINDER_TYPE_HANDLE: {
127 const sp<IBinder> b = proc->getStrongProxyForHandle(obj.handle);
128 if (b != NULL) {
129 LOG_REFS("Parcel %p releasing reference on remote %p", who, b.get());
130 b->decStrong(who);
131 }
132 return;
133 }
134 case BINDER_TYPE_WEAK_HANDLE: {
135 const wp<IBinder> b = proc->getWeakProxyForHandle(obj.handle);
136 if (b != NULL) b.get_refs()->decWeak(who);
137 return;
138 }
139 case BINDER_TYPE_FD: {
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800140 if (obj.cookie != 0) close(obj.handle);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700141 return;
142 }
143 }
144
Colin Cross6f4f3ab2014-02-05 17:42:44 -0800145 ALOGE("Invalid object type 0x%08x", obj.type);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700146}
147
148inline static status_t finish_flatten_binder(
Colin Cross6f4f3ab2014-02-05 17:42:44 -0800149 const sp<IBinder>& /*binder*/, const flat_binder_object& flat, Parcel* out)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700150{
151 return out->writeObject(flat, false);
152}
153
Colin Cross6f4f3ab2014-02-05 17:42:44 -0800154status_t flatten_binder(const sp<ProcessState>& /*proc*/,
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700155 const sp<IBinder>& binder, Parcel* out)
156{
157 flat_binder_object obj;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700158
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700159 obj.flags = 0x7f | FLAT_BINDER_FLAG_ACCEPTS_FDS;
160 if (binder != NULL) {
161 IBinder *local = binder->localBinder();
162 if (!local) {
163 BpBinder *proxy = binder->remoteBinder();
164 if (proxy == NULL) {
Steve Blocke6f43dd2012-01-06 19:20:56 +0000165 ALOGE("null proxy");
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700166 }
167 const int32_t handle = proxy ? proxy->handle() : 0;
168 obj.type = BINDER_TYPE_HANDLE;
Arve Hjønnevåg07fd0f12014-02-18 21:10:29 -0800169 obj.binder = 0; /* Don't pass uninitialized stack data to a remote process */
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700170 obj.handle = handle;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800171 obj.cookie = 0;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700172 } else {
173 obj.type = BINDER_TYPE_BINDER;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800174 obj.binder = reinterpret_cast<uintptr_t>(local->getWeakRefs());
175 obj.cookie = reinterpret_cast<uintptr_t>(local);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700176 }
177 } else {
178 obj.type = BINDER_TYPE_BINDER;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800179 obj.binder = 0;
180 obj.cookie = 0;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700181 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700182
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700183 return finish_flatten_binder(binder, obj, out);
184}
185
Colin Cross6f4f3ab2014-02-05 17:42:44 -0800186status_t flatten_binder(const sp<ProcessState>& /*proc*/,
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700187 const wp<IBinder>& binder, Parcel* out)
188{
189 flat_binder_object obj;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700190
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700191 obj.flags = 0x7f | FLAT_BINDER_FLAG_ACCEPTS_FDS;
192 if (binder != NULL) {
193 sp<IBinder> real = binder.promote();
194 if (real != NULL) {
195 IBinder *local = real->localBinder();
196 if (!local) {
197 BpBinder *proxy = real->remoteBinder();
198 if (proxy == NULL) {
Steve Blocke6f43dd2012-01-06 19:20:56 +0000199 ALOGE("null proxy");
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700200 }
201 const int32_t handle = proxy ? proxy->handle() : 0;
202 obj.type = BINDER_TYPE_WEAK_HANDLE;
Arve Hjønnevåg07fd0f12014-02-18 21:10:29 -0800203 obj.binder = 0; /* Don't pass uninitialized stack data to a remote process */
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700204 obj.handle = handle;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800205 obj.cookie = 0;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700206 } else {
207 obj.type = BINDER_TYPE_WEAK_BINDER;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800208 obj.binder = reinterpret_cast<uintptr_t>(binder.get_refs());
209 obj.cookie = reinterpret_cast<uintptr_t>(binder.unsafe_get());
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700210 }
211 return finish_flatten_binder(real, obj, out);
212 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700213
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700214 // XXX How to deal? In order to flatten the given binder,
215 // we need to probe it for information, which requires a primary
216 // reference... but we don't have one.
217 //
218 // The OpenBinder implementation uses a dynamic_cast<> here,
219 // but we can't do that with the different reference counting
220 // implementation we are using.
Steve Blocke6f43dd2012-01-06 19:20:56 +0000221 ALOGE("Unable to unflatten Binder weak reference!");
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700222 obj.type = BINDER_TYPE_BINDER;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800223 obj.binder = 0;
224 obj.cookie = 0;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700225 return finish_flatten_binder(NULL, obj, out);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700226
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700227 } else {
228 obj.type = BINDER_TYPE_BINDER;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800229 obj.binder = 0;
230 obj.cookie = 0;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700231 return finish_flatten_binder(NULL, obj, out);
232 }
233}
234
235inline static status_t finish_unflatten_binder(
Colin Cross6f4f3ab2014-02-05 17:42:44 -0800236 BpBinder* /*proxy*/, const flat_binder_object& /*flat*/,
237 const Parcel& /*in*/)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700238{
239 return NO_ERROR;
240}
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700241
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700242status_t unflatten_binder(const sp<ProcessState>& proc,
243 const Parcel& in, sp<IBinder>* out)
244{
245 const flat_binder_object* flat = in.readObject(false);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700246
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700247 if (flat) {
248 switch (flat->type) {
249 case BINDER_TYPE_BINDER:
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800250 *out = reinterpret_cast<IBinder*>(flat->cookie);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700251 return finish_unflatten_binder(NULL, *flat, in);
252 case BINDER_TYPE_HANDLE:
253 *out = proc->getStrongProxyForHandle(flat->handle);
254 return finish_unflatten_binder(
255 static_cast<BpBinder*>(out->get()), *flat, in);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700256 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700257 }
258 return BAD_TYPE;
259}
260
261status_t unflatten_binder(const sp<ProcessState>& proc,
262 const Parcel& in, wp<IBinder>* out)
263{
264 const flat_binder_object* flat = in.readObject(false);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700265
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700266 if (flat) {
267 switch (flat->type) {
268 case BINDER_TYPE_BINDER:
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800269 *out = reinterpret_cast<IBinder*>(flat->cookie);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700270 return finish_unflatten_binder(NULL, *flat, in);
271 case BINDER_TYPE_WEAK_BINDER:
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800272 if (flat->binder != 0) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700273 out->set_object_and_refs(
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800274 reinterpret_cast<IBinder*>(flat->cookie),
275 reinterpret_cast<RefBase::weakref_type*>(flat->binder));
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700276 } else {
277 *out = NULL;
278 }
279 return finish_unflatten_binder(NULL, *flat, in);
280 case BINDER_TYPE_HANDLE:
281 case BINDER_TYPE_WEAK_HANDLE:
282 *out = proc->getWeakProxyForHandle(flat->handle);
283 return finish_unflatten_binder(
284 static_cast<BpBinder*>(out->unsafe_get()), *flat, in);
285 }
286 }
287 return BAD_TYPE;
288}
289
290// ---------------------------------------------------------------------------
291
292Parcel::Parcel()
293{
294 initState();
295}
296
297Parcel::~Parcel()
298{
299 freeDataNoInit();
300}
301
302const uint8_t* Parcel::data() const
303{
304 return mData;
305}
306
307size_t Parcel::dataSize() const
308{
309 return (mDataSize > mDataPos ? mDataSize : mDataPos);
310}
311
312size_t Parcel::dataAvail() const
313{
314 // TODO: decide what to do about the possibility that this can
315 // report an available-data size that exceeds a Java int's max
316 // positive value, causing havoc. Fortunately this will only
317 // happen if someone constructs a Parcel containing more than two
318 // gigabytes of data, which on typical phone hardware is simply
319 // not possible.
320 return dataSize() - dataPosition();
321}
322
323size_t Parcel::dataPosition() const
324{
325 return mDataPos;
326}
327
328size_t Parcel::dataCapacity() const
329{
330 return mDataCapacity;
331}
332
333status_t Parcel::setDataSize(size_t size)
334{
335 status_t err;
336 err = continueWrite(size);
337 if (err == NO_ERROR) {
338 mDataSize = size;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700339 ALOGV("setDataSize Setting data size of %p to %zu", this, mDataSize);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700340 }
341 return err;
342}
343
344void Parcel::setDataPosition(size_t pos) const
345{
346 mDataPos = pos;
347 mNextObjectHint = 0;
348}
349
350status_t Parcel::setDataCapacity(size_t size)
351{
Dianne Hackborn97e2bcd2011-04-13 18:15:56 -0700352 if (size > mDataCapacity) return continueWrite(size);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700353 return NO_ERROR;
354}
355
356status_t Parcel::setData(const uint8_t* buffer, size_t len)
357{
358 status_t err = restartWrite(len);
359 if (err == NO_ERROR) {
360 memcpy(const_cast<uint8_t*>(data()), buffer, len);
361 mDataSize = len;
362 mFdsKnown = false;
363 }
364 return err;
365}
366
Andreas Huber51faf462011-04-13 10:21:56 -0700367status_t Parcel::appendFrom(const Parcel *parcel, size_t offset, size_t len)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700368{
369 const sp<ProcessState> proc(ProcessState::self());
370 status_t err;
Andreas Huber51faf462011-04-13 10:21:56 -0700371 const uint8_t *data = parcel->mData;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800372 const binder_size_t *objects = parcel->mObjects;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700373 size_t size = parcel->mObjectsSize;
374 int startPos = mDataPos;
375 int firstIndex = -1, lastIndex = -2;
376
377 if (len == 0) {
378 return NO_ERROR;
379 }
380
381 // range checks against the source parcel size
382 if ((offset > parcel->mDataSize)
383 || (len > parcel->mDataSize)
384 || (offset + len > parcel->mDataSize)) {
385 return BAD_VALUE;
386 }
387
388 // Count objects in range
389 for (int i = 0; i < (int) size; i++) {
390 size_t off = objects[i];
391 if ((off >= offset) && (off < offset + len)) {
392 if (firstIndex == -1) {
393 firstIndex = i;
394 }
395 lastIndex = i;
396 }
397 }
398 int numObjects = lastIndex - firstIndex + 1;
399
Dianne Hackborn97e2bcd2011-04-13 18:15:56 -0700400 if ((mDataSize+len) > mDataCapacity) {
401 // grow data
402 err = growData(len);
403 if (err != NO_ERROR) {
404 return err;
405 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700406 }
407
408 // append data
409 memcpy(mData + mDataPos, data + offset, len);
410 mDataPos += len;
411 mDataSize += len;
412
Dianne Hackborn8938ed22011-09-28 23:19:47 -0400413 err = NO_ERROR;
414
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700415 if (numObjects > 0) {
416 // grow objects
417 if (mObjectsCapacity < mObjectsSize + numObjects) {
418 int newSize = ((mObjectsSize + numObjects)*3)/2;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800419 binder_size_t *objects =
420 (binder_size_t*)realloc(mObjects, newSize*sizeof(binder_size_t));
421 if (objects == (binder_size_t*)0) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700422 return NO_MEMORY;
423 }
424 mObjects = objects;
425 mObjectsCapacity = newSize;
426 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700427
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700428 // append and acquire objects
429 int idx = mObjectsSize;
430 for (int i = firstIndex; i <= lastIndex; i++) {
431 size_t off = objects[i] - offset + startPos;
432 mObjects[idx++] = off;
433 mObjectsSize++;
434
Dianne Hackborn8af0f822009-05-22 13:20:23 -0700435 flat_binder_object* flat
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700436 = reinterpret_cast<flat_binder_object*>(mData + off);
437 acquire_object(proc, *flat, this);
438
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700439 if (flat->type == BINDER_TYPE_FD) {
Dianne Hackborn8af0f822009-05-22 13:20:23 -0700440 // If this is a file descriptor, we need to dup it so the
441 // new Parcel now owns its own fd, and can declare that we
442 // officially know we have fds.
443 flat->handle = dup(flat->handle);
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800444 flat->cookie = 1;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700445 mHasFds = mFdsKnown = true;
Dianne Hackborn8938ed22011-09-28 23:19:47 -0400446 if (!mAllowFds) {
447 err = FDS_NOT_ALLOWED;
448 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700449 }
450 }
451 }
452
Dianne Hackborn8938ed22011-09-28 23:19:47 -0400453 return err;
454}
455
Dianne Hackborn7746cc32011-10-03 21:09:35 -0700456bool Parcel::pushAllowFds(bool allowFds)
Dianne Hackborn8938ed22011-09-28 23:19:47 -0400457{
458 const bool origValue = mAllowFds;
Dianne Hackborn7746cc32011-10-03 21:09:35 -0700459 if (!allowFds) {
460 mAllowFds = false;
461 }
Dianne Hackborn8938ed22011-09-28 23:19:47 -0400462 return origValue;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700463}
464
Dianne Hackborn7746cc32011-10-03 21:09:35 -0700465void Parcel::restoreAllowFds(bool lastValue)
466{
467 mAllowFds = lastValue;
468}
469
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700470bool Parcel::hasFileDescriptors() const
471{
472 if (!mFdsKnown) {
473 scanForFds();
474 }
475 return mHasFds;
476}
477
Brad Fitzpatrick702ea9d2010-06-18 13:07:53 -0700478// Write RPC headers. (previously just the interface token)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700479status_t Parcel::writeInterfaceToken(const String16& interface)
480{
Brad Fitzpatricka877cd82010-07-07 16:06:39 -0700481 writeInt32(IPCThreadState::self()->getStrictModePolicy() |
482 STRICT_MODE_PENALTY_GATHER);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700483 // currently the interface identification token is just its name as a string
484 return writeString16(interface);
485}
486
Mathias Agopian83c04462009-05-22 19:00:22 -0700487bool Parcel::checkInterface(IBinder* binder) const
488{
Brad Fitzpatrick702ea9d2010-06-18 13:07:53 -0700489 return enforceInterface(binder->getInterfaceDescriptor());
Mathias Agopian83c04462009-05-22 19:00:22 -0700490}
491
Brad Fitzpatricka877cd82010-07-07 16:06:39 -0700492bool Parcel::enforceInterface(const String16& interface,
Brad Fitzpatrick70081a12010-07-27 09:49:11 -0700493 IPCThreadState* threadState) const
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700494{
Brad Fitzpatrick70081a12010-07-27 09:49:11 -0700495 int32_t strictPolicy = readInt32();
496 if (threadState == NULL) {
497 threadState = IPCThreadState::self();
Brad Fitzpatricka877cd82010-07-07 16:06:39 -0700498 }
Brad Fitzpatrick52736032010-08-30 16:01:16 -0700499 if ((threadState->getLastTransactionBinderFlags() &
500 IBinder::FLAG_ONEWAY) != 0) {
501 // For one-way calls, the callee is running entirely
502 // disconnected from the caller, so disable StrictMode entirely.
503 // Not only does disk/network usage not impact the caller, but
504 // there's no way to commuicate back any violations anyway.
505 threadState->setStrictModePolicy(0);
506 } else {
507 threadState->setStrictModePolicy(strictPolicy);
508 }
Mathias Agopian83c04462009-05-22 19:00:22 -0700509 const String16 str(readString16());
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700510 if (str == interface) {
511 return true;
512 } else {
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700513 ALOGW("**** enforceInterface() expected '%s' but read '%s'",
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700514 String8(interface).string(), String8(str).string());
515 return false;
516 }
Brad Fitzpatrick702ea9d2010-06-18 13:07:53 -0700517}
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700518
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800519const binder_size_t* Parcel::objects() const
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700520{
521 return mObjects;
522}
523
524size_t Parcel::objectsCount() const
525{
526 return mObjectsSize;
527}
528
529status_t Parcel::errorCheck() const
530{
531 return mError;
532}
533
534void Parcel::setError(status_t err)
535{
536 mError = err;
537}
538
539status_t Parcel::finishWrite(size_t len)
540{
541 //printf("Finish write of %d\n", len);
542 mDataPos += len;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700543 ALOGV("finishWrite Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700544 if (mDataPos > mDataSize) {
545 mDataSize = mDataPos;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700546 ALOGV("finishWrite Setting data size of %p to %zu", this, mDataSize);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700547 }
548 //printf("New pos=%d, size=%d\n", mDataPos, mDataSize);
549 return NO_ERROR;
550}
551
552status_t Parcel::writeUnpadded(const void* data, size_t len)
553{
554 size_t end = mDataPos + len;
555 if (end < mDataPos) {
556 // integer overflow
557 return BAD_VALUE;
558 }
559
560 if (end <= mDataCapacity) {
561restart_write:
562 memcpy(mData+mDataPos, data, len);
563 return finishWrite(len);
564 }
565
566 status_t err = growData(len);
567 if (err == NO_ERROR) goto restart_write;
568 return err;
569}
570
571status_t Parcel::write(const void* data, size_t len)
572{
573 void* const d = writeInplace(len);
574 if (d) {
575 memcpy(d, data, len);
576 return NO_ERROR;
577 }
578 return mError;
579}
580
581void* Parcel::writeInplace(size_t len)
582{
583 const size_t padded = PAD_SIZE(len);
584
585 // sanity check for integer overflow
586 if (mDataPos+padded < mDataPos) {
587 return NULL;
588 }
589
590 if ((mDataPos+padded) <= mDataCapacity) {
591restart_write:
592 //printf("Writing %ld bytes, padded to %ld\n", len, padded);
593 uint8_t* const data = mData+mDataPos;
594
595 // Need to pad at end?
596 if (padded != len) {
597#if BYTE_ORDER == BIG_ENDIAN
598 static const uint32_t mask[4] = {
599 0x00000000, 0xffffff00, 0xffff0000, 0xff000000
600 };
601#endif
602#if BYTE_ORDER == LITTLE_ENDIAN
603 static const uint32_t mask[4] = {
604 0x00000000, 0x00ffffff, 0x0000ffff, 0x000000ff
605 };
606#endif
607 //printf("Applying pad mask: %p to %p\n", (void*)mask[padded-len],
608 // *reinterpret_cast<void**>(data+padded-4));
609 *reinterpret_cast<uint32_t*>(data+padded-4) &= mask[padded-len];
610 }
611
612 finishWrite(padded);
613 return data;
614 }
615
616 status_t err = growData(padded);
617 if (err == NO_ERROR) goto restart_write;
618 return NULL;
619}
620
621status_t Parcel::writeInt32(int32_t val)
622{
Andreas Huber84a6d042009-08-17 13:33:27 -0700623 return writeAligned(val);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700624}
Marco Nelissen708cc792013-10-16 10:57:51 -0700625status_t Parcel::writeInt32Array(size_t len, const int32_t *val) {
626 if (!val) {
627 return writeAligned(-1);
628 }
629 status_t ret = writeAligned(len);
630 if (ret == NO_ERROR) {
631 ret = write(val, len * sizeof(*val));
632 }
633 return ret;
634}
Marco Nelissenf0190bf2014-03-13 14:17:40 -0700635status_t Parcel::writeByteArray(size_t len, const uint8_t *val) {
636 if (!val) {
637 return writeAligned(-1);
638 }
639 status_t ret = writeAligned(len);
640 if (ret == NO_ERROR) {
641 ret = write(val, len * sizeof(*val));
642 }
643 return ret;
644}
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700645
646status_t Parcel::writeInt64(int64_t val)
647{
Andreas Huber84a6d042009-08-17 13:33:27 -0700648 return writeAligned(val);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700649}
650
Serban Constantinescuf683e012013-11-05 16:53:55 +0000651status_t Parcel::writePointer(uintptr_t val)
652{
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800653 return writeAligned<binder_uintptr_t>(val);
Serban Constantinescuf683e012013-11-05 16:53:55 +0000654}
655
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700656status_t Parcel::writeFloat(float val)
657{
Andreas Huber84a6d042009-08-17 13:33:27 -0700658 return writeAligned(val);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700659}
660
Douglas Leungcc1a4bb2013-01-11 15:00:55 -0800661#if defined(__mips__) && defined(__mips_hard_float)
662
663status_t Parcel::writeDouble(double val)
664{
665 union {
666 double d;
667 unsigned long long ll;
668 } u;
669 u.d = val;
670 return writeAligned(u.ll);
671}
672
673#else
674
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700675status_t Parcel::writeDouble(double val)
676{
Andreas Huber84a6d042009-08-17 13:33:27 -0700677 return writeAligned(val);
678}
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700679
Douglas Leungcc1a4bb2013-01-11 15:00:55 -0800680#endif
681
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700682status_t Parcel::writeCString(const char* str)
683{
684 return write(str, strlen(str)+1);
685}
686
687status_t Parcel::writeString8(const String8& str)
688{
689 status_t err = writeInt32(str.bytes());
Pravat Dalbeherad1dff8d2010-12-15 08:40:00 +0100690 // only write string if its length is more than zero characters,
691 // as readString8 will only read if the length field is non-zero.
692 // this is slightly different from how writeString16 works.
693 if (str.bytes() > 0 && err == NO_ERROR) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700694 err = write(str.string(), str.bytes()+1);
695 }
696 return err;
697}
698
699status_t Parcel::writeString16(const String16& str)
700{
701 return writeString16(str.string(), str.size());
702}
703
704status_t Parcel::writeString16(const char16_t* str, size_t len)
705{
706 if (str == NULL) return writeInt32(-1);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700707
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700708 status_t err = writeInt32(len);
709 if (err == NO_ERROR) {
710 len *= sizeof(char16_t);
711 uint8_t* data = (uint8_t*)writeInplace(len+sizeof(char16_t));
712 if (data) {
713 memcpy(data, str, len);
714 *reinterpret_cast<char16_t*>(data+len) = 0;
715 return NO_ERROR;
716 }
717 err = mError;
718 }
719 return err;
720}
721
722status_t Parcel::writeStrongBinder(const sp<IBinder>& val)
723{
724 return flatten_binder(ProcessState::self(), val, this);
725}
726
727status_t Parcel::writeWeakBinder(const wp<IBinder>& val)
728{
729 return flatten_binder(ProcessState::self(), val, this);
730}
731
Mathias Agopiana47f02a2009-05-21 16:29:38 -0700732status_t Parcel::writeNativeHandle(const native_handle* handle)
The Android Open Source Project5f78a482009-01-20 14:03:58 -0800733{
Mathias Agopian1d0a95b2009-07-31 16:12:13 -0700734 if (!handle || handle->version != sizeof(native_handle))
The Android Open Source Project5f78a482009-01-20 14:03:58 -0800735 return BAD_TYPE;
736
737 status_t err;
Mathias Agopiana47f02a2009-05-21 16:29:38 -0700738 err = writeInt32(handle->numFds);
The Android Open Source Project5f78a482009-01-20 14:03:58 -0800739 if (err != NO_ERROR) return err;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800740
Mathias Agopiana47f02a2009-05-21 16:29:38 -0700741 err = writeInt32(handle->numInts);
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 for (int i=0 ; err==NO_ERROR && i<handle->numFds ; i++)
745 err = writeDupFileDescriptor(handle->data[i]);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800746
747 if (err != NO_ERROR) {
Steve Block9d453682011-12-20 16:23:08 +0000748 ALOGD("write native handle, write dup fd failed");
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800749 return err;
750 }
Mathias Agopiana47f02a2009-05-21 16:29:38 -0700751 err = write(handle->data + handle->numFds, sizeof(int)*handle->numInts);
The Android Open Source Project5f78a482009-01-20 14:03:58 -0800752 return err;
753}
754
Jeff Brown93ff1f92011-11-04 19:01:44 -0700755status_t Parcel::writeFileDescriptor(int fd, bool takeOwnership)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700756{
757 flat_binder_object obj;
758 obj.type = BINDER_TYPE_FD;
759 obj.flags = 0x7f | FLAT_BINDER_FLAG_ACCEPTS_FDS;
Arve Hjønnevåg07fd0f12014-02-18 21:10:29 -0800760 obj.binder = 0; /* Don't pass uninitialized stack data to a remote process */
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700761 obj.handle = fd;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800762 obj.cookie = takeOwnership ? 1 : 0;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700763 return writeObject(obj, true);
764}
765
766status_t Parcel::writeDupFileDescriptor(int fd)
767{
Jeff Brownd341c712011-11-04 20:19:33 -0700768 int dupFd = dup(fd);
769 if (dupFd < 0) {
770 return -errno;
771 }
772 status_t err = writeFileDescriptor(dupFd, true /*takeOwnership*/);
773 if (err) {
774 close(dupFd);
775 }
776 return err;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700777}
778
Jeff Brown5707dbf2011-09-23 21:17:56 -0700779status_t Parcel::writeBlob(size_t len, WritableBlob* outBlob)
780{
781 status_t status;
782
783 if (!mAllowFds || len <= IN_PLACE_BLOB_LIMIT) {
Steve Block6807e592011-10-20 11:56:00 +0100784 ALOGV("writeBlob: write in place");
Jeff Brown5707dbf2011-09-23 21:17:56 -0700785 status = writeInt32(0);
786 if (status) return status;
787
788 void* ptr = writeInplace(len);
789 if (!ptr) return NO_MEMORY;
790
791 outBlob->init(false /*mapped*/, ptr, len);
792 return NO_ERROR;
793 }
794
Steve Block6807e592011-10-20 11:56:00 +0100795 ALOGV("writeBlob: write to ashmem");
Jeff Brown5707dbf2011-09-23 21:17:56 -0700796 int fd = ashmem_create_region("Parcel Blob", len);
797 if (fd < 0) return NO_MEMORY;
798
799 int result = ashmem_set_prot_region(fd, PROT_READ | PROT_WRITE);
800 if (result < 0) {
Jeff Brownec4e0062011-10-10 14:50:10 -0700801 status = result;
Jeff Brown5707dbf2011-09-23 21:17:56 -0700802 } else {
803 void* ptr = ::mmap(NULL, len, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
804 if (ptr == MAP_FAILED) {
805 status = -errno;
806 } else {
807 result = ashmem_set_prot_region(fd, PROT_READ);
808 if (result < 0) {
Jeff Brownec4e0062011-10-10 14:50:10 -0700809 status = result;
Jeff Brown5707dbf2011-09-23 21:17:56 -0700810 } else {
811 status = writeInt32(1);
812 if (!status) {
Jeff Brown93ff1f92011-11-04 19:01:44 -0700813 status = writeFileDescriptor(fd, true /*takeOwnership*/);
Jeff Brown5707dbf2011-09-23 21:17:56 -0700814 if (!status) {
815 outBlob->init(true /*mapped*/, ptr, len);
816 return NO_ERROR;
817 }
818 }
819 }
820 }
821 ::munmap(ptr, len);
822 }
823 ::close(fd);
824 return status;
825}
826
Mathias Agopiane1424282013-07-29 21:24:40 -0700827status_t Parcel::write(const FlattenableHelperInterface& val)
Mathias Agopian98e71dd2010-02-11 17:30:52 -0800828{
829 status_t err;
830
831 // size if needed
Mathias Agopiane1424282013-07-29 21:24:40 -0700832 const size_t len = val.getFlattenedSize();
833 const size_t fd_count = val.getFdCount();
Mathias Agopian98e71dd2010-02-11 17:30:52 -0800834
835 err = this->writeInt32(len);
836 if (err) return err;
837
838 err = this->writeInt32(fd_count);
839 if (err) return err;
840
841 // payload
Mathias Agopiane1424282013-07-29 21:24:40 -0700842 void* const buf = this->writeInplace(PAD_SIZE(len));
Mathias Agopian98e71dd2010-02-11 17:30:52 -0800843 if (buf == NULL)
844 return BAD_VALUE;
845
846 int* fds = NULL;
847 if (fd_count) {
848 fds = new int[fd_count];
849 }
850
851 err = val.flatten(buf, len, fds, fd_count);
852 for (size_t i=0 ; i<fd_count && err==NO_ERROR ; i++) {
853 err = this->writeDupFileDescriptor( fds[i] );
854 }
855
856 if (fd_count) {
857 delete [] fds;
858 }
859
860 return err;
861}
862
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700863status_t Parcel::writeObject(const flat_binder_object& val, bool nullMetaData)
864{
865 const bool enoughData = (mDataPos+sizeof(val)) <= mDataCapacity;
866 const bool enoughObjects = mObjectsSize < mObjectsCapacity;
867 if (enoughData && enoughObjects) {
868restart_write:
869 *reinterpret_cast<flat_binder_object*>(mData+mDataPos) = val;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700870
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700871 // Need to write meta-data?
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800872 if (nullMetaData || val.binder != 0) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700873 mObjects[mObjectsSize] = mDataPos;
874 acquire_object(ProcessState::self(), val, this);
875 mObjectsSize++;
876 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700877
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700878 // remember if it's a file descriptor
879 if (val.type == BINDER_TYPE_FD) {
Dianne Hackborn8938ed22011-09-28 23:19:47 -0400880 if (!mAllowFds) {
881 return FDS_NOT_ALLOWED;
882 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700883 mHasFds = mFdsKnown = true;
884 }
885
886 return finishWrite(sizeof(flat_binder_object));
887 }
888
889 if (!enoughData) {
890 const status_t err = growData(sizeof(val));
891 if (err != NO_ERROR) return err;
892 }
893 if (!enoughObjects) {
894 size_t newSize = ((mObjectsSize+2)*3)/2;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800895 binder_size_t* objects = (binder_size_t*)realloc(mObjects, newSize*sizeof(binder_size_t));
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700896 if (objects == NULL) return NO_MEMORY;
897 mObjects = objects;
898 mObjectsCapacity = newSize;
899 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700900
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700901 goto restart_write;
902}
903
Brad Fitzpatrick837a0d02010-07-13 15:33:35 -0700904status_t Parcel::writeNoException()
905{
906 return writeInt32(0);
907}
908
Colin Cross6f4f3ab2014-02-05 17:42:44 -0800909void Parcel::remove(size_t /*start*/, size_t /*amt*/)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700910{
911 LOG_ALWAYS_FATAL("Parcel::remove() not yet implemented!");
912}
913
914status_t Parcel::read(void* outData, size_t len) const
915{
Kenny Root5b61ad22014-03-17 13:18:16 -0700916 if ((mDataPos+PAD_SIZE(len)) >= mDataPos && (mDataPos+PAD_SIZE(len)) <= mDataSize
917 && len <= PAD_SIZE(len)) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700918 memcpy(outData, mData+mDataPos, len);
919 mDataPos += PAD_SIZE(len);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700920 ALOGV("read Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700921 return NO_ERROR;
922 }
923 return NOT_ENOUGH_DATA;
924}
925
926const void* Parcel::readInplace(size_t len) const
927{
Kenny Root5b61ad22014-03-17 13:18:16 -0700928 if ((mDataPos+PAD_SIZE(len)) >= mDataPos && (mDataPos+PAD_SIZE(len)) <= mDataSize
929 && len <= PAD_SIZE(len)) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700930 const void* data = mData+mDataPos;
931 mDataPos += PAD_SIZE(len);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700932 ALOGV("readInplace Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700933 return data;
934 }
935 return NULL;
936}
937
Andreas Huber84a6d042009-08-17 13:33:27 -0700938template<class T>
939status_t Parcel::readAligned(T *pArg) const {
940 COMPILE_TIME_ASSERT_FUNCTION_SCOPE(PAD_SIZE(sizeof(T)) == sizeof(T));
941
942 if ((mDataPos+sizeof(T)) <= mDataSize) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700943 const void* data = mData+mDataPos;
Andreas Huber84a6d042009-08-17 13:33:27 -0700944 mDataPos += sizeof(T);
945 *pArg = *reinterpret_cast<const T*>(data);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700946 return NO_ERROR;
947 } else {
948 return NOT_ENOUGH_DATA;
949 }
950}
951
Andreas Huber84a6d042009-08-17 13:33:27 -0700952template<class T>
953T Parcel::readAligned() const {
954 T result;
955 if (readAligned(&result) != NO_ERROR) {
956 result = 0;
957 }
958
959 return result;
960}
961
962template<class T>
963status_t Parcel::writeAligned(T val) {
964 COMPILE_TIME_ASSERT_FUNCTION_SCOPE(PAD_SIZE(sizeof(T)) == sizeof(T));
965
966 if ((mDataPos+sizeof(val)) <= mDataCapacity) {
967restart_write:
968 *reinterpret_cast<T*>(mData+mDataPos) = val;
969 return finishWrite(sizeof(val));
970 }
971
972 status_t err = growData(sizeof(val));
973 if (err == NO_ERROR) goto restart_write;
974 return err;
975}
976
977status_t Parcel::readInt32(int32_t *pArg) const
978{
979 return readAligned(pArg);
980}
981
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700982int32_t Parcel::readInt32() const
983{
Andreas Huber84a6d042009-08-17 13:33:27 -0700984 return readAligned<int32_t>();
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700985}
986
987
988status_t Parcel::readInt64(int64_t *pArg) const
989{
Andreas Huber84a6d042009-08-17 13:33:27 -0700990 return readAligned(pArg);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700991}
992
993
994int64_t Parcel::readInt64() const
995{
Andreas Huber84a6d042009-08-17 13:33:27 -0700996 return readAligned<int64_t>();
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700997}
998
Serban Constantinescuf683e012013-11-05 16:53:55 +0000999status_t Parcel::readPointer(uintptr_t *pArg) const
1000{
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001001 status_t ret;
1002 binder_uintptr_t ptr;
1003 ret = readAligned(&ptr);
1004 if (!ret)
1005 *pArg = ptr;
1006 return ret;
Serban Constantinescuf683e012013-11-05 16:53:55 +00001007}
1008
1009uintptr_t Parcel::readPointer() const
1010{
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001011 return readAligned<binder_uintptr_t>();
Serban Constantinescuf683e012013-11-05 16:53:55 +00001012}
1013
1014
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001015status_t Parcel::readFloat(float *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
1021float Parcel::readFloat() const
1022{
Andreas Huber84a6d042009-08-17 13:33:27 -07001023 return readAligned<float>();
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001024}
1025
Douglas Leungcc1a4bb2013-01-11 15:00:55 -08001026#if defined(__mips__) && defined(__mips_hard_float)
1027
1028status_t Parcel::readDouble(double *pArg) const
1029{
1030 union {
1031 double d;
1032 unsigned long long ll;
1033 } u;
Narayan Kamath2f1f7422014-06-04 15:04:29 +01001034 u.d = 0;
Douglas Leungcc1a4bb2013-01-11 15:00:55 -08001035 status_t status;
1036 status = readAligned(&u.ll);
1037 *pArg = u.d;
1038 return status;
1039}
1040
1041double Parcel::readDouble() const
1042{
1043 union {
1044 double d;
1045 unsigned long long ll;
1046 } u;
1047 u.ll = readAligned<unsigned long long>();
1048 return u.d;
1049}
1050
1051#else
1052
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001053status_t Parcel::readDouble(double *pArg) const
1054{
Andreas Huber84a6d042009-08-17 13:33:27 -07001055 return readAligned(pArg);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001056}
1057
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001058double Parcel::readDouble() const
1059{
Andreas Huber84a6d042009-08-17 13:33:27 -07001060 return readAligned<double>();
1061}
1062
Douglas Leungcc1a4bb2013-01-11 15:00:55 -08001063#endif
1064
Andreas Huber84a6d042009-08-17 13:33:27 -07001065status_t Parcel::readIntPtr(intptr_t *pArg) const
1066{
1067 return readAligned(pArg);
1068}
1069
1070
1071intptr_t Parcel::readIntPtr() const
1072{
1073 return readAligned<intptr_t>();
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001074}
1075
1076
1077const char* Parcel::readCString() const
1078{
1079 const size_t avail = mDataSize-mDataPos;
1080 if (avail > 0) {
1081 const char* str = reinterpret_cast<const char*>(mData+mDataPos);
1082 // is the string's trailing NUL within the parcel's valid bounds?
1083 const char* eos = reinterpret_cast<const char*>(memchr(str, 0, avail));
1084 if (eos) {
1085 const size_t len = eos - str;
1086 mDataPos += PAD_SIZE(len+1);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001087 ALOGV("readCString Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001088 return str;
1089 }
1090 }
1091 return NULL;
1092}
1093
1094String8 Parcel::readString8() const
1095{
1096 int32_t size = readInt32();
1097 // watch for potential int overflow adding 1 for trailing NUL
1098 if (size > 0 && size < INT32_MAX) {
1099 const char* str = (const char*)readInplace(size+1);
1100 if (str) return String8(str, size);
1101 }
1102 return String8();
1103}
1104
1105String16 Parcel::readString16() const
1106{
1107 size_t len;
1108 const char16_t* str = readString16Inplace(&len);
1109 if (str) return String16(str, len);
Steve Blocke6f43dd2012-01-06 19:20:56 +00001110 ALOGE("Reading a NULL string not supported here.");
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001111 return String16();
1112}
1113
1114const char16_t* Parcel::readString16Inplace(size_t* outLen) const
1115{
1116 int32_t size = readInt32();
1117 // watch for potential int overflow from size+1
1118 if (size >= 0 && size < INT32_MAX) {
1119 *outLen = size;
1120 const char16_t* str = (const char16_t*)readInplace((size+1)*sizeof(char16_t));
1121 if (str != NULL) {
1122 return str;
1123 }
1124 }
1125 *outLen = 0;
1126 return NULL;
1127}
1128
1129sp<IBinder> Parcel::readStrongBinder() const
1130{
1131 sp<IBinder> val;
1132 unflatten_binder(ProcessState::self(), *this, &val);
1133 return val;
1134}
1135
1136wp<IBinder> Parcel::readWeakBinder() const
1137{
1138 wp<IBinder> val;
1139 unflatten_binder(ProcessState::self(), *this, &val);
1140 return val;
1141}
1142
Brad Fitzpatrick837a0d02010-07-13 15:33:35 -07001143int32_t Parcel::readExceptionCode() const
1144{
1145 int32_t exception_code = readAligned<int32_t>();
Brad Fitzpatrickd36f4a52010-07-12 11:05:38 -07001146 if (exception_code == EX_HAS_REPLY_HEADER) {
Magnus Strandberg1ba24572011-05-03 15:44:00 +02001147 int32_t header_start = dataPosition();
Brad Fitzpatrickd36f4a52010-07-12 11:05:38 -07001148 int32_t header_size = readAligned<int32_t>();
1149 // Skip over fat responses headers. Not used (or propagated) in
1150 // native code
Magnus Strandberg1ba24572011-05-03 15:44:00 +02001151 setDataPosition(header_start + header_size);
Brad Fitzpatrickd36f4a52010-07-12 11:05:38 -07001152 // And fat response headers are currently only used when there are no
1153 // exceptions, so return no error:
1154 return 0;
1155 }
Brad Fitzpatrick837a0d02010-07-13 15:33:35 -07001156 return exception_code;
1157}
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001158
Mathias Agopiana47f02a2009-05-21 16:29:38 -07001159native_handle* Parcel::readNativeHandle() const
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001160{
1161 int numFds, numInts;
1162 status_t err;
1163 err = readInt32(&numFds);
1164 if (err != NO_ERROR) return 0;
1165 err = readInt32(&numInts);
1166 if (err != NO_ERROR) return 0;
1167
Mathias Agopiana47f02a2009-05-21 16:29:38 -07001168 native_handle* h = native_handle_create(numFds, numInts);
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001169 for (int i=0 ; err==NO_ERROR && i<numFds ; i++) {
Rebecca Schultz Zavin360211f2009-02-13 16:34:38 -08001170 h->data[i] = dup(readFileDescriptor());
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001171 if (h->data[i] < 0) err = BAD_VALUE;
1172 }
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001173 err = read(h->data + numFds, sizeof(int)*numInts);
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001174 if (err != NO_ERROR) {
Mathias Agopiana47f02a2009-05-21 16:29:38 -07001175 native_handle_close(h);
1176 native_handle_delete(h);
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001177 h = 0;
1178 }
1179 return h;
1180}
1181
1182
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001183int Parcel::readFileDescriptor() const
1184{
1185 const flat_binder_object* flat = readObject(true);
1186 if (flat) {
1187 switch (flat->type) {
1188 case BINDER_TYPE_FD:
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001189 //ALOGI("Returning file descriptor %ld from parcel %p", flat->handle, this);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001190 return flat->handle;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001191 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001192 }
1193 return BAD_TYPE;
1194}
1195
Jeff Brown5707dbf2011-09-23 21:17:56 -07001196status_t Parcel::readBlob(size_t len, ReadableBlob* outBlob) const
1197{
1198 int32_t useAshmem;
1199 status_t status = readInt32(&useAshmem);
1200 if (status) return status;
1201
1202 if (!useAshmem) {
Steve Block6807e592011-10-20 11:56:00 +01001203 ALOGV("readBlob: read in place");
Jeff Brown5707dbf2011-09-23 21:17:56 -07001204 const void* ptr = readInplace(len);
1205 if (!ptr) return BAD_VALUE;
1206
1207 outBlob->init(false /*mapped*/, const_cast<void*>(ptr), len);
1208 return NO_ERROR;
1209 }
1210
Steve Block6807e592011-10-20 11:56:00 +01001211 ALOGV("readBlob: read from ashmem");
Jeff Brown5707dbf2011-09-23 21:17:56 -07001212 int fd = readFileDescriptor();
1213 if (fd == int(BAD_TYPE)) return BAD_VALUE;
1214
1215 void* ptr = ::mmap(NULL, len, PROT_READ, MAP_SHARED, fd, 0);
1216 if (!ptr) return NO_MEMORY;
1217
1218 outBlob->init(true /*mapped*/, ptr, len);
1219 return NO_ERROR;
1220}
1221
Mathias Agopiane1424282013-07-29 21:24:40 -07001222status_t Parcel::read(FlattenableHelperInterface& val) const
Mathias Agopian98e71dd2010-02-11 17:30:52 -08001223{
1224 // size
1225 const size_t len = this->readInt32();
1226 const size_t fd_count = this->readInt32();
1227
1228 // payload
Mathias Agopiane1424282013-07-29 21:24:40 -07001229 void const* const buf = this->readInplace(PAD_SIZE(len));
Mathias Agopian98e71dd2010-02-11 17:30:52 -08001230 if (buf == NULL)
1231 return BAD_VALUE;
1232
1233 int* fds = NULL;
1234 if (fd_count) {
1235 fds = new int[fd_count];
1236 }
1237
1238 status_t err = NO_ERROR;
1239 for (size_t i=0 ; i<fd_count && err==NO_ERROR ; i++) {
1240 fds[i] = dup(this->readFileDescriptor());
Jun Jiangabf8a2c2014-04-29 14:22:10 +08001241 if (fds[i] < 0) {
1242 err = BAD_VALUE;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001243 ALOGE("dup() failed in Parcel::read, i is %zu, fds[i] is %d, fd_count is %zu, error: %s",
Jun Jiangabf8a2c2014-04-29 14:22:10 +08001244 i, fds[i], fd_count, strerror(errno));
1245 }
Mathias Agopian98e71dd2010-02-11 17:30:52 -08001246 }
1247
1248 if (err == NO_ERROR) {
1249 err = val.unflatten(buf, len, fds, fd_count);
1250 }
1251
1252 if (fd_count) {
1253 delete [] fds;
1254 }
1255
1256 return err;
1257}
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001258const flat_binder_object* Parcel::readObject(bool nullMetaData) const
1259{
1260 const size_t DPOS = mDataPos;
1261 if ((DPOS+sizeof(flat_binder_object)) <= mDataSize) {
1262 const flat_binder_object* obj
1263 = reinterpret_cast<const flat_binder_object*>(mData+DPOS);
1264 mDataPos = DPOS + sizeof(flat_binder_object);
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001265 if (!nullMetaData && (obj->cookie == 0 && obj->binder == 0)) {
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001266 // When transferring a NULL object, we don't write it into
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001267 // the object list, so we don't want to check for it when
1268 // reading.
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001269 ALOGV("readObject Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001270 return obj;
1271 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001272
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001273 // Ensure that this object is valid...
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001274 binder_size_t* const OBJS = mObjects;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001275 const size_t N = mObjectsSize;
1276 size_t opos = mNextObjectHint;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001277
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001278 if (N > 0) {
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001279 ALOGV("Parcel %p looking for obj at %zu, hint=%zu",
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001280 this, DPOS, opos);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001281
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001282 // Start at the current hint position, looking for an object at
1283 // the current data position.
1284 if (opos < N) {
1285 while (opos < (N-1) && OBJS[opos] < DPOS) {
1286 opos++;
1287 }
1288 } else {
1289 opos = N-1;
1290 }
1291 if (OBJS[opos] == DPOS) {
1292 // Found it!
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001293 ALOGV("Parcel %p found obj %zu at index %zu with forward search",
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001294 this, DPOS, opos);
1295 mNextObjectHint = opos+1;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001296 ALOGV("readObject Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001297 return obj;
1298 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001299
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001300 // Look backwards for it...
1301 while (opos > 0 && OBJS[opos] > DPOS) {
1302 opos--;
1303 }
1304 if (OBJS[opos] == DPOS) {
1305 // Found it!
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001306 ALOGV("Parcel %p found obj %zu at index %zu with backward search",
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001307 this, DPOS, opos);
1308 mNextObjectHint = opos+1;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001309 ALOGV("readObject Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001310 return obj;
1311 }
1312 }
Colin Cross6f4f3ab2014-02-05 17:42:44 -08001313 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 -07001314 this, DPOS);
1315 }
1316 return NULL;
1317}
1318
1319void Parcel::closeFileDescriptors()
1320{
1321 size_t i = mObjectsSize;
1322 if (i > 0) {
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001323 //ALOGI("Closing file descriptors for %zu objects...", i);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001324 }
1325 while (i > 0) {
1326 i--;
1327 const flat_binder_object* flat
1328 = reinterpret_cast<flat_binder_object*>(mData+mObjects[i]);
1329 if (flat->type == BINDER_TYPE_FD) {
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001330 //ALOGI("Closing fd: %ld", flat->handle);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001331 close(flat->handle);
1332 }
1333 }
1334}
1335
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001336uintptr_t Parcel::ipcData() const
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001337{
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001338 return reinterpret_cast<uintptr_t>(mData);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001339}
1340
1341size_t Parcel::ipcDataSize() const
1342{
1343 return (mDataSize > mDataPos ? mDataSize : mDataPos);
1344}
1345
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001346uintptr_t Parcel::ipcObjects() const
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001347{
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001348 return reinterpret_cast<uintptr_t>(mObjects);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001349}
1350
1351size_t Parcel::ipcObjectsCount() const
1352{
1353 return mObjectsSize;
1354}
1355
1356void Parcel::ipcSetDataReference(const uint8_t* data, size_t dataSize,
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001357 const binder_size_t* objects, size_t objectsCount, release_func relFunc, void* relCookie)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001358{
Arve Hjønnevåg67903292014-02-19 15:35:52 -08001359 size_t minOffset = 0;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001360 freeDataNoInit();
1361 mError = NO_ERROR;
1362 mData = const_cast<uint8_t*>(data);
1363 mDataSize = mDataCapacity = dataSize;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001364 //ALOGI("setDataReference Setting data size of %p to %lu (pid=%d)", this, mDataSize, getpid());
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001365 mDataPos = 0;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001366 ALOGV("setDataReference Setting data pos of %p to %zu", this, mDataPos);
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001367 mObjects = const_cast<binder_size_t*>(objects);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001368 mObjectsSize = mObjectsCapacity = objectsCount;
1369 mNextObjectHint = 0;
1370 mOwner = relFunc;
1371 mOwnerCookie = relCookie;
Arve Hjønnevågf50b9ea2014-02-13 19:22:08 -08001372 for (size_t i = 0; i < mObjectsSize; i++) {
Arve Hjønnevåg67903292014-02-19 15:35:52 -08001373 size_t offset = mObjects[i];
Arve Hjønnevågf50b9ea2014-02-13 19:22:08 -08001374 if (offset < minOffset) {
Arve Hjønnevåg67903292014-02-19 15:35:52 -08001375 ALOGE("%s: bad object offset %zu < %zu\n",
1376 __func__, offset, minOffset);
Arve Hjønnevågf50b9ea2014-02-13 19:22:08 -08001377 mObjectsSize = 0;
1378 break;
1379 }
1380 minOffset = offset + sizeof(flat_binder_object);
1381 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001382 scanForFds();
1383}
1384
Colin Cross6f4f3ab2014-02-05 17:42:44 -08001385void Parcel::print(TextOutput& to, uint32_t /*flags*/) const
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001386{
1387 to << "Parcel(";
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001388
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001389 if (errorCheck() != NO_ERROR) {
1390 const status_t err = errorCheck();
Colin Cross6f4f3ab2014-02-05 17:42:44 -08001391 to << "Error: " << (void*)(intptr_t)err << " \"" << strerror(-err) << "\"";
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001392 } else if (dataSize() > 0) {
1393 const uint8_t* DATA = data();
1394 to << indent << HexDump(DATA, dataSize()) << dedent;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001395 const binder_size_t* OBJS = objects();
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001396 const size_t N = objectsCount();
1397 for (size_t i=0; i<N; i++) {
1398 const flat_binder_object* flat
1399 = reinterpret_cast<const flat_binder_object*>(DATA+OBJS[i]);
1400 to << endl << "Object #" << i << " @ " << (void*)OBJS[i] << ": "
1401 << TypeCode(flat->type & 0x7f7f7f00)
1402 << " = " << flat->binder;
1403 }
1404 } else {
1405 to << "NULL";
1406 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001407
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001408 to << ")";
1409}
1410
1411void Parcel::releaseObjects()
1412{
1413 const sp<ProcessState> proc(ProcessState::self());
1414 size_t i = mObjectsSize;
1415 uint8_t* const data = mData;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001416 binder_size_t* const objects = mObjects;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001417 while (i > 0) {
1418 i--;
1419 const flat_binder_object* flat
1420 = reinterpret_cast<flat_binder_object*>(data+objects[i]);
1421 release_object(proc, *flat, this);
1422 }
1423}
1424
1425void Parcel::acquireObjects()
1426{
1427 const sp<ProcessState> proc(ProcessState::self());
1428 size_t i = mObjectsSize;
1429 uint8_t* const data = mData;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001430 binder_size_t* const objects = mObjects;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001431 while (i > 0) {
1432 i--;
1433 const flat_binder_object* flat
1434 = reinterpret_cast<flat_binder_object*>(data+objects[i]);
1435 acquire_object(proc, *flat, this);
1436 }
1437}
1438
1439void Parcel::freeData()
1440{
1441 freeDataNoInit();
1442 initState();
1443}
1444
1445void Parcel::freeDataNoInit()
1446{
1447 if (mOwner) {
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001448 //ALOGI("Freeing data ref of %p (pid=%d)", this, getpid());
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001449 mOwner(this, mData, mDataSize, mObjects, mObjectsSize, mOwnerCookie);
1450 } else {
1451 releaseObjects();
1452 if (mData) free(mData);
1453 if (mObjects) free(mObjects);
1454 }
1455}
1456
1457status_t Parcel::growData(size_t len)
1458{
1459 size_t newSize = ((mDataSize+len)*3)/2;
1460 return (newSize <= mDataSize)
1461 ? (status_t) NO_MEMORY
1462 : continueWrite(newSize);
1463}
1464
1465status_t Parcel::restartWrite(size_t desired)
1466{
1467 if (mOwner) {
1468 freeData();
1469 return continueWrite(desired);
1470 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001471
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001472 uint8_t* data = (uint8_t*)realloc(mData, desired);
1473 if (!data && desired > mDataCapacity) {
1474 mError = NO_MEMORY;
1475 return NO_MEMORY;
1476 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001477
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001478 releaseObjects();
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001479
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001480 if (data) {
1481 mData = data;
1482 mDataCapacity = desired;
1483 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001484
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001485 mDataSize = mDataPos = 0;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001486 ALOGV("restartWrite Setting data size of %p to %zu", this, mDataSize);
1487 ALOGV("restartWrite Setting data pos of %p to %zu", this, mDataPos);
1488
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001489 free(mObjects);
1490 mObjects = NULL;
1491 mObjectsSize = mObjectsCapacity = 0;
1492 mNextObjectHint = 0;
1493 mHasFds = false;
1494 mFdsKnown = true;
Dianne Hackborn8938ed22011-09-28 23:19:47 -04001495 mAllowFds = true;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001496
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001497 return NO_ERROR;
1498}
1499
1500status_t Parcel::continueWrite(size_t desired)
1501{
1502 // If shrinking, first adjust for any objects that appear
1503 // after the new data size.
1504 size_t objectsSize = mObjectsSize;
1505 if (desired < mDataSize) {
1506 if (desired == 0) {
1507 objectsSize = 0;
1508 } else {
1509 while (objectsSize > 0) {
1510 if (mObjects[objectsSize-1] < desired)
1511 break;
1512 objectsSize--;
1513 }
1514 }
1515 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001516
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001517 if (mOwner) {
1518 // If the size is going to zero, just release the owner's data.
1519 if (desired == 0) {
1520 freeData();
1521 return NO_ERROR;
1522 }
1523
1524 // If there is a different owner, we need to take
1525 // posession.
1526 uint8_t* data = (uint8_t*)malloc(desired);
1527 if (!data) {
1528 mError = NO_MEMORY;
1529 return NO_MEMORY;
1530 }
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001531 binder_size_t* objects = NULL;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001532
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001533 if (objectsSize) {
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001534 objects = (binder_size_t*)malloc(objectsSize*sizeof(binder_size_t));
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001535 if (!objects) {
Hyejin Kim3f727c02013-03-09 11:28:54 +09001536 free(data);
1537
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001538 mError = NO_MEMORY;
1539 return NO_MEMORY;
1540 }
1541
1542 // Little hack to only acquire references on objects
1543 // we will be keeping.
1544 size_t oldObjectsSize = mObjectsSize;
1545 mObjectsSize = objectsSize;
1546 acquireObjects();
1547 mObjectsSize = oldObjectsSize;
1548 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001549
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001550 if (mData) {
1551 memcpy(data, mData, mDataSize < desired ? mDataSize : desired);
1552 }
1553 if (objects && mObjects) {
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001554 memcpy(objects, mObjects, objectsSize*sizeof(binder_size_t));
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001555 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001556 //ALOGI("Freeing data ref of %p (pid=%d)", this, getpid());
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001557 mOwner(this, mData, mDataSize, mObjects, mObjectsSize, mOwnerCookie);
1558 mOwner = NULL;
1559
1560 mData = data;
1561 mObjects = objects;
1562 mDataSize = (mDataSize < desired) ? mDataSize : desired;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001563 ALOGV("continueWrite Setting data size of %p to %zu", this, mDataSize);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001564 mDataCapacity = desired;
1565 mObjectsSize = mObjectsCapacity = objectsSize;
1566 mNextObjectHint = 0;
1567
1568 } else if (mData) {
1569 if (objectsSize < mObjectsSize) {
1570 // Need to release refs on any objects we are dropping.
1571 const sp<ProcessState> proc(ProcessState::self());
1572 for (size_t i=objectsSize; i<mObjectsSize; i++) {
1573 const flat_binder_object* flat
1574 = reinterpret_cast<flat_binder_object*>(mData+mObjects[i]);
1575 if (flat->type == BINDER_TYPE_FD) {
1576 // will need to rescan because we may have lopped off the only FDs
1577 mFdsKnown = false;
1578 }
1579 release_object(proc, *flat, this);
1580 }
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001581 binder_size_t* objects =
1582 (binder_size_t*)realloc(mObjects, objectsSize*sizeof(binder_size_t));
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001583 if (objects) {
1584 mObjects = objects;
1585 }
1586 mObjectsSize = objectsSize;
1587 mNextObjectHint = 0;
1588 }
1589
1590 // We own the data, so we can just do a realloc().
1591 if (desired > mDataCapacity) {
1592 uint8_t* data = (uint8_t*)realloc(mData, desired);
1593 if (data) {
1594 mData = data;
1595 mDataCapacity = desired;
1596 } else if (desired > mDataCapacity) {
1597 mError = NO_MEMORY;
1598 return NO_MEMORY;
1599 }
1600 } else {
Dianne Hackborn97e2bcd2011-04-13 18:15:56 -07001601 if (mDataSize > desired) {
1602 mDataSize = desired;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001603 ALOGV("continueWrite Setting data size of %p to %zu", this, mDataSize);
Dianne Hackborn97e2bcd2011-04-13 18:15:56 -07001604 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001605 if (mDataPos > desired) {
1606 mDataPos = desired;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001607 ALOGV("continueWrite Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001608 }
1609 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001610
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001611 } else {
1612 // This is the first data. Easy!
1613 uint8_t* data = (uint8_t*)malloc(desired);
1614 if (!data) {
1615 mError = NO_MEMORY;
1616 return NO_MEMORY;
1617 }
Hyejin Kim3f727c02013-03-09 11:28:54 +09001618
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001619 if(!(mDataCapacity == 0 && mObjects == NULL
1620 && mObjectsCapacity == 0)) {
Colin Cross6f4f3ab2014-02-05 17:42:44 -08001621 ALOGE("continueWrite: %zu/%p/%zu/%zu", mDataCapacity, mObjects, mObjectsCapacity, desired);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001622 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001623
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001624 mData = data;
1625 mDataSize = mDataPos = 0;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001626 ALOGV("continueWrite Setting data size of %p to %zu", this, mDataSize);
1627 ALOGV("continueWrite Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001628 mDataCapacity = desired;
1629 }
1630
1631 return NO_ERROR;
1632}
1633
1634void Parcel::initState()
1635{
1636 mError = NO_ERROR;
1637 mData = 0;
1638 mDataSize = 0;
1639 mDataCapacity = 0;
1640 mDataPos = 0;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001641 ALOGV("initState Setting data size of %p to %zu", this, mDataSize);
1642 ALOGV("initState Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001643 mObjects = NULL;
1644 mObjectsSize = 0;
1645 mObjectsCapacity = 0;
1646 mNextObjectHint = 0;
1647 mHasFds = false;
1648 mFdsKnown = true;
Dianne Hackborn8938ed22011-09-28 23:19:47 -04001649 mAllowFds = true;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001650 mOwner = NULL;
1651}
1652
1653void Parcel::scanForFds() const
1654{
1655 bool hasFds = false;
1656 for (size_t i=0; i<mObjectsSize; i++) {
1657 const flat_binder_object* flat
1658 = reinterpret_cast<const flat_binder_object*>(mData + mObjects[i]);
1659 if (flat->type == BINDER_TYPE_FD) {
1660 hasFds = true;
1661 break;
1662 }
1663 }
1664 mHasFds = hasFds;
1665 mFdsKnown = true;
1666}
1667
Jeff Brown5707dbf2011-09-23 21:17:56 -07001668// --- Parcel::Blob ---
1669
1670Parcel::Blob::Blob() :
1671 mMapped(false), mData(NULL), mSize(0) {
1672}
1673
1674Parcel::Blob::~Blob() {
1675 release();
1676}
1677
1678void Parcel::Blob::release() {
1679 if (mMapped && mData) {
1680 ::munmap(mData, mSize);
1681 }
1682 clear();
1683}
1684
1685void Parcel::Blob::init(bool mapped, void* data, size_t size) {
1686 mMapped = mapped;
1687 mData = data;
1688 mSize = size;
1689}
1690
1691void Parcel::Blob::clear() {
1692 mMapped = false;
1693 mData = NULL;
1694 mSize = 0;
1695}
1696
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001697}; // namespace android