blob: a38e3dd86840cea0984cf469dfc0ce70e87e4db6 [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
28#include <utils/Debug.h>
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -070029#include <utils/Log.h>
30#include <utils/String8.h>
31#include <utils/String16.h>
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -070032#include <utils/misc.h>
Mathias Agopian98e71dd2010-02-11 17:30:52 -080033#include <utils/Flattenable.h>
Jeff Brown5707dbf2011-09-23 21:17:56 -070034#include <cutils/ashmem.h>
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -070035
Mathias Agopian208059f2009-05-18 15:08:03 -070036#include <private/binder/binder_module.h>
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -070037
Arve Hjønnevågf50b9ea2014-02-13 19:22:08 -080038#include <inttypes.h>
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -070039#include <stdio.h>
40#include <stdlib.h>
41#include <stdint.h>
Jeff Brown5707dbf2011-09-23 21:17:56 -070042#include <sys/mman.h>
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -070043
44#ifndef INT32_MAX
45#define INT32_MAX ((int32_t)(2147483647))
46#endif
47
48#define LOG_REFS(...)
Steve Block9f760152011-10-12 17:27:03 +010049//#define LOG_REFS(...) ALOG(LOG_DEBUG, "Parcel", __VA_ARGS__)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -070050
51// ---------------------------------------------------------------------------
52
53#define PAD_SIZE(s) (((s)+3)&~3)
54
Brad Fitzpatricka877cd82010-07-07 16:06:39 -070055// Note: must be kept in sync with android/os/StrictMode.java's PENALTY_GATHER
56#define STRICT_MODE_PENALTY_GATHER 0x100
57
Brad Fitzpatrickd36f4a52010-07-12 11:05:38 -070058// Note: must be kept in sync with android/os/Parcel.java's EX_HAS_REPLY_HEADER
59#define EX_HAS_REPLY_HEADER -128
60
Jeff Brown5707dbf2011-09-23 21:17:56 -070061// Maximum size of a blob to transfer in-place.
62static const size_t IN_PLACE_BLOB_LIMIT = 40 * 1024;
63
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -070064// XXX This can be made public if we want to provide
65// support for typed data.
66struct small_flat_data
67{
68 uint32_t type;
69 uint32_t data;
70};
71
72namespace android {
73
74void acquire_object(const sp<ProcessState>& proc,
75 const flat_binder_object& obj, const void* who)
76{
77 switch (obj.type) {
78 case BINDER_TYPE_BINDER:
79 if (obj.binder) {
80 LOG_REFS("Parcel %p acquiring reference on local %p", who, obj.cookie);
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -080081 reinterpret_cast<IBinder*>(obj.cookie)->incStrong(who);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -070082 }
83 return;
84 case BINDER_TYPE_WEAK_BINDER:
85 if (obj.binder)
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -080086 reinterpret_cast<RefBase::weakref_type*>(obj.binder)->incWeak(who);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -070087 return;
88 case BINDER_TYPE_HANDLE: {
89 const sp<IBinder> b = proc->getStrongProxyForHandle(obj.handle);
90 if (b != NULL) {
91 LOG_REFS("Parcel %p acquiring reference on remote %p", who, b.get());
92 b->incStrong(who);
93 }
94 return;
95 }
96 case BINDER_TYPE_WEAK_HANDLE: {
97 const wp<IBinder> b = proc->getWeakProxyForHandle(obj.handle);
98 if (b != NULL) b.get_refs()->incWeak(who);
99 return;
100 }
101 case BINDER_TYPE_FD: {
102 // intentionally blank -- nothing to do to acquire this, but we do
103 // recognize it as a legitimate object type.
104 return;
105 }
106 }
107
Colin Cross6f4f3ab2014-02-05 17:42:44 -0800108 ALOGD("Invalid object type 0x%08x", obj.type);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700109}
110
111void release_object(const sp<ProcessState>& proc,
112 const flat_binder_object& obj, const void* who)
113{
114 switch (obj.type) {
115 case BINDER_TYPE_BINDER:
116 if (obj.binder) {
117 LOG_REFS("Parcel %p releasing reference on local %p", who, obj.cookie);
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800118 reinterpret_cast<IBinder*>(obj.cookie)->decStrong(who);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700119 }
120 return;
121 case BINDER_TYPE_WEAK_BINDER:
122 if (obj.binder)
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800123 reinterpret_cast<RefBase::weakref_type*>(obj.binder)->decWeak(who);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700124 return;
125 case BINDER_TYPE_HANDLE: {
126 const sp<IBinder> b = proc->getStrongProxyForHandle(obj.handle);
127 if (b != NULL) {
128 LOG_REFS("Parcel %p releasing reference on remote %p", who, b.get());
129 b->decStrong(who);
130 }
131 return;
132 }
133 case BINDER_TYPE_WEAK_HANDLE: {
134 const wp<IBinder> b = proc->getWeakProxyForHandle(obj.handle);
135 if (b != NULL) b.get_refs()->decWeak(who);
136 return;
137 }
138 case BINDER_TYPE_FD: {
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800139 if (obj.cookie != 0) close(obj.handle);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700140 return;
141 }
142 }
143
Colin Cross6f4f3ab2014-02-05 17:42:44 -0800144 ALOGE("Invalid object type 0x%08x", obj.type);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700145}
146
147inline static status_t finish_flatten_binder(
Colin Cross6f4f3ab2014-02-05 17:42:44 -0800148 const sp<IBinder>& /*binder*/, const flat_binder_object& flat, Parcel* out)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700149{
150 return out->writeObject(flat, false);
151}
152
Colin Cross6f4f3ab2014-02-05 17:42:44 -0800153status_t flatten_binder(const sp<ProcessState>& /*proc*/,
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700154 const sp<IBinder>& binder, Parcel* out)
155{
156 flat_binder_object obj;
157
158 obj.flags = 0x7f | FLAT_BINDER_FLAG_ACCEPTS_FDS;
159 if (binder != NULL) {
160 IBinder *local = binder->localBinder();
161 if (!local) {
162 BpBinder *proxy = binder->remoteBinder();
163 if (proxy == NULL) {
Steve Blocke6f43dd2012-01-06 19:20:56 +0000164 ALOGE("null proxy");
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700165 }
166 const int32_t handle = proxy ? proxy->handle() : 0;
167 obj.type = BINDER_TYPE_HANDLE;
Arve Hjønnevåg07fd0f12014-02-18 21:10:29 -0800168 obj.binder = 0; /* Don't pass uninitialized stack data to a remote process */
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700169 obj.handle = handle;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800170 obj.cookie = 0;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700171 } else {
172 obj.type = BINDER_TYPE_BINDER;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800173 obj.binder = reinterpret_cast<uintptr_t>(local->getWeakRefs());
174 obj.cookie = reinterpret_cast<uintptr_t>(local);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700175 }
176 } else {
177 obj.type = BINDER_TYPE_BINDER;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800178 obj.binder = 0;
179 obj.cookie = 0;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700180 }
181
182 return finish_flatten_binder(binder, obj, out);
183}
184
Colin Cross6f4f3ab2014-02-05 17:42:44 -0800185status_t flatten_binder(const sp<ProcessState>& /*proc*/,
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700186 const wp<IBinder>& binder, Parcel* out)
187{
188 flat_binder_object obj;
189
190 obj.flags = 0x7f | FLAT_BINDER_FLAG_ACCEPTS_FDS;
191 if (binder != NULL) {
192 sp<IBinder> real = binder.promote();
193 if (real != NULL) {
194 IBinder *local = real->localBinder();
195 if (!local) {
196 BpBinder *proxy = real->remoteBinder();
197 if (proxy == NULL) {
Steve Blocke6f43dd2012-01-06 19:20:56 +0000198 ALOGE("null proxy");
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700199 }
200 const int32_t handle = proxy ? proxy->handle() : 0;
201 obj.type = BINDER_TYPE_WEAK_HANDLE;
Arve Hjønnevåg07fd0f12014-02-18 21:10:29 -0800202 obj.binder = 0; /* Don't pass uninitialized stack data to a remote process */
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700203 obj.handle = handle;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800204 obj.cookie = 0;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700205 } else {
206 obj.type = BINDER_TYPE_WEAK_BINDER;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800207 obj.binder = reinterpret_cast<uintptr_t>(binder.get_refs());
208 obj.cookie = reinterpret_cast<uintptr_t>(binder.unsafe_get());
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700209 }
210 return finish_flatten_binder(real, obj, out);
211 }
212
213 // XXX How to deal? In order to flatten the given binder,
214 // we need to probe it for information, which requires a primary
215 // reference... but we don't have one.
216 //
217 // The OpenBinder implementation uses a dynamic_cast<> here,
218 // but we can't do that with the different reference counting
219 // implementation we are using.
Steve Blocke6f43dd2012-01-06 19:20:56 +0000220 ALOGE("Unable to unflatten Binder weak reference!");
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700221 obj.type = BINDER_TYPE_BINDER;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800222 obj.binder = 0;
223 obj.cookie = 0;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700224 return finish_flatten_binder(NULL, obj, out);
225
226 } else {
227 obj.type = BINDER_TYPE_BINDER;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800228 obj.binder = 0;
229 obj.cookie = 0;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700230 return finish_flatten_binder(NULL, obj, out);
231 }
232}
233
234inline static status_t finish_unflatten_binder(
Colin Cross6f4f3ab2014-02-05 17:42:44 -0800235 BpBinder* /*proxy*/, const flat_binder_object& /*flat*/,
236 const Parcel& /*in*/)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700237{
238 return NO_ERROR;
239}
240
241status_t unflatten_binder(const sp<ProcessState>& proc,
242 const Parcel& in, sp<IBinder>* out)
243{
244 const flat_binder_object* flat = in.readObject(false);
245
246 if (flat) {
247 switch (flat->type) {
248 case BINDER_TYPE_BINDER:
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800249 *out = reinterpret_cast<IBinder*>(flat->cookie);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700250 return finish_unflatten_binder(NULL, *flat, in);
251 case BINDER_TYPE_HANDLE:
252 *out = proc->getStrongProxyForHandle(flat->handle);
253 return finish_unflatten_binder(
254 static_cast<BpBinder*>(out->get()), *flat, in);
255 }
256 }
257 return BAD_TYPE;
258}
259
260status_t unflatten_binder(const sp<ProcessState>& proc,
261 const Parcel& in, wp<IBinder>* out)
262{
263 const flat_binder_object* flat = in.readObject(false);
264
265 if (flat) {
266 switch (flat->type) {
267 case BINDER_TYPE_BINDER:
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800268 *out = reinterpret_cast<IBinder*>(flat->cookie);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700269 return finish_unflatten_binder(NULL, *flat, in);
270 case BINDER_TYPE_WEAK_BINDER:
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800271 if (flat->binder != 0) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700272 out->set_object_and_refs(
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800273 reinterpret_cast<IBinder*>(flat->cookie),
274 reinterpret_cast<RefBase::weakref_type*>(flat->binder));
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700275 } else {
276 *out = NULL;
277 }
278 return finish_unflatten_binder(NULL, *flat, in);
279 case BINDER_TYPE_HANDLE:
280 case BINDER_TYPE_WEAK_HANDLE:
281 *out = proc->getWeakProxyForHandle(flat->handle);
282 return finish_unflatten_binder(
283 static_cast<BpBinder*>(out->unsafe_get()), *flat, in);
284 }
285 }
286 return BAD_TYPE;
287}
288
289// ---------------------------------------------------------------------------
290
291Parcel::Parcel()
292{
293 initState();
294}
295
296Parcel::~Parcel()
297{
298 freeDataNoInit();
299}
300
301const uint8_t* Parcel::data() const
302{
303 return mData;
304}
305
306size_t Parcel::dataSize() const
307{
308 return (mDataSize > mDataPos ? mDataSize : mDataPos);
309}
310
311size_t Parcel::dataAvail() const
312{
313 // TODO: decide what to do about the possibility that this can
314 // report an available-data size that exceeds a Java int's max
315 // positive value, causing havoc. Fortunately this will only
316 // happen if someone constructs a Parcel containing more than two
317 // gigabytes of data, which on typical phone hardware is simply
318 // not possible.
319 return dataSize() - dataPosition();
320}
321
322size_t Parcel::dataPosition() const
323{
324 return mDataPos;
325}
326
327size_t Parcel::dataCapacity() const
328{
329 return mDataCapacity;
330}
331
332status_t Parcel::setDataSize(size_t size)
333{
334 status_t err;
335 err = continueWrite(size);
336 if (err == NO_ERROR) {
337 mDataSize = size;
Steve Block6807e592011-10-20 11:56:00 +0100338 ALOGV("setDataSize Setting data size of %p to %d\n", this, mDataSize);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700339 }
340 return err;
341}
342
343void Parcel::setDataPosition(size_t pos) const
344{
345 mDataPos = pos;
346 mNextObjectHint = 0;
347}
348
349status_t Parcel::setDataCapacity(size_t size)
350{
Dianne Hackborn97e2bcd2011-04-13 18:15:56 -0700351 if (size > mDataCapacity) return continueWrite(size);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700352 return NO_ERROR;
353}
354
355status_t Parcel::setData(const uint8_t* buffer, size_t len)
356{
357 status_t err = restartWrite(len);
358 if (err == NO_ERROR) {
359 memcpy(const_cast<uint8_t*>(data()), buffer, len);
360 mDataSize = len;
361 mFdsKnown = false;
362 }
363 return err;
364}
365
Andreas Huber51faf462011-04-13 10:21:56 -0700366status_t Parcel::appendFrom(const Parcel *parcel, size_t offset, size_t len)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700367{
368 const sp<ProcessState> proc(ProcessState::self());
369 status_t err;
Andreas Huber51faf462011-04-13 10:21:56 -0700370 const uint8_t *data = parcel->mData;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800371 const binder_size_t *objects = parcel->mObjects;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700372 size_t size = parcel->mObjectsSize;
373 int startPos = mDataPos;
374 int firstIndex = -1, lastIndex = -2;
375
376 if (len == 0) {
377 return NO_ERROR;
378 }
379
380 // range checks against the source parcel size
381 if ((offset > parcel->mDataSize)
382 || (len > parcel->mDataSize)
383 || (offset + len > parcel->mDataSize)) {
384 return BAD_VALUE;
385 }
386
387 // Count objects in range
388 for (int i = 0; i < (int) size; i++) {
389 size_t off = objects[i];
390 if ((off >= offset) && (off < offset + len)) {
391 if (firstIndex == -1) {
392 firstIndex = i;
393 }
394 lastIndex = i;
395 }
396 }
397 int numObjects = lastIndex - firstIndex + 1;
398
Dianne Hackborn97e2bcd2011-04-13 18:15:56 -0700399 if ((mDataSize+len) > mDataCapacity) {
400 // grow data
401 err = growData(len);
402 if (err != NO_ERROR) {
403 return err;
404 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700405 }
406
407 // append data
408 memcpy(mData + mDataPos, data + offset, len);
409 mDataPos += len;
410 mDataSize += len;
411
Dianne Hackborn8938ed22011-09-28 23:19:47 -0400412 err = NO_ERROR;
413
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700414 if (numObjects > 0) {
415 // grow objects
416 if (mObjectsCapacity < mObjectsSize + numObjects) {
417 int newSize = ((mObjectsSize + numObjects)*3)/2;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800418 binder_size_t *objects =
419 (binder_size_t*)realloc(mObjects, newSize*sizeof(binder_size_t));
420 if (objects == (binder_size_t*)0) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700421 return NO_MEMORY;
422 }
423 mObjects = objects;
424 mObjectsCapacity = newSize;
425 }
426
427 // append and acquire objects
428 int idx = mObjectsSize;
429 for (int i = firstIndex; i <= lastIndex; i++) {
430 size_t off = objects[i] - offset + startPos;
431 mObjects[idx++] = off;
432 mObjectsSize++;
433
Dianne Hackborn8af0f822009-05-22 13:20:23 -0700434 flat_binder_object* flat
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700435 = reinterpret_cast<flat_binder_object*>(mData + off);
436 acquire_object(proc, *flat, this);
437
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700438 if (flat->type == BINDER_TYPE_FD) {
Dianne Hackborn8af0f822009-05-22 13:20:23 -0700439 // If this is a file descriptor, we need to dup it so the
440 // new Parcel now owns its own fd, and can declare that we
441 // officially know we have fds.
442 flat->handle = dup(flat->handle);
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800443 flat->cookie = 1;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700444 mHasFds = mFdsKnown = true;
Dianne Hackborn8938ed22011-09-28 23:19:47 -0400445 if (!mAllowFds) {
446 err = FDS_NOT_ALLOWED;
447 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700448 }
449 }
450 }
451
Dianne Hackborn8938ed22011-09-28 23:19:47 -0400452 return err;
453}
454
Dianne Hackborn7746cc32011-10-03 21:09:35 -0700455bool Parcel::pushAllowFds(bool allowFds)
Dianne Hackborn8938ed22011-09-28 23:19:47 -0400456{
457 const bool origValue = mAllowFds;
Dianne Hackborn7746cc32011-10-03 21:09:35 -0700458 if (!allowFds) {
459 mAllowFds = false;
460 }
Dianne Hackborn8938ed22011-09-28 23:19:47 -0400461 return origValue;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700462}
463
Dianne Hackborn7746cc32011-10-03 21:09:35 -0700464void Parcel::restoreAllowFds(bool lastValue)
465{
466 mAllowFds = lastValue;
467}
468
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700469bool Parcel::hasFileDescriptors() const
470{
471 if (!mFdsKnown) {
472 scanForFds();
473 }
474 return mHasFds;
475}
476
Brad Fitzpatrick702ea9d2010-06-18 13:07:53 -0700477// Write RPC headers. (previously just the interface token)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700478status_t Parcel::writeInterfaceToken(const String16& interface)
479{
Brad Fitzpatricka877cd82010-07-07 16:06:39 -0700480 writeInt32(IPCThreadState::self()->getStrictModePolicy() |
481 STRICT_MODE_PENALTY_GATHER);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700482 // currently the interface identification token is just its name as a string
483 return writeString16(interface);
484}
485
Mathias Agopian83c04462009-05-22 19:00:22 -0700486bool Parcel::checkInterface(IBinder* binder) const
487{
Brad Fitzpatrick702ea9d2010-06-18 13:07:53 -0700488 return enforceInterface(binder->getInterfaceDescriptor());
Mathias Agopian83c04462009-05-22 19:00:22 -0700489}
490
Brad Fitzpatricka877cd82010-07-07 16:06:39 -0700491bool Parcel::enforceInterface(const String16& interface,
Brad Fitzpatrick70081a12010-07-27 09:49:11 -0700492 IPCThreadState* threadState) const
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700493{
Brad Fitzpatrick70081a12010-07-27 09:49:11 -0700494 int32_t strictPolicy = readInt32();
495 if (threadState == NULL) {
496 threadState = IPCThreadState::self();
Brad Fitzpatricka877cd82010-07-07 16:06:39 -0700497 }
Brad Fitzpatrick52736032010-08-30 16:01:16 -0700498 if ((threadState->getLastTransactionBinderFlags() &
499 IBinder::FLAG_ONEWAY) != 0) {
500 // For one-way calls, the callee is running entirely
501 // disconnected from the caller, so disable StrictMode entirely.
502 // Not only does disk/network usage not impact the caller, but
503 // there's no way to commuicate back any violations anyway.
504 threadState->setStrictModePolicy(0);
505 } else {
506 threadState->setStrictModePolicy(strictPolicy);
507 }
Mathias Agopian83c04462009-05-22 19:00:22 -0700508 const String16 str(readString16());
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700509 if (str == interface) {
510 return true;
511 } else {
Steve Block32397c12012-01-05 23:22:43 +0000512 ALOGW("**** enforceInterface() expected '%s' but read '%s'\n",
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700513 String8(interface).string(), String8(str).string());
514 return false;
515 }
Brad Fitzpatrick702ea9d2010-06-18 13:07:53 -0700516}
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700517
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800518const binder_size_t* Parcel::objects() const
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700519{
520 return mObjects;
521}
522
523size_t Parcel::objectsCount() const
524{
525 return mObjectsSize;
526}
527
528status_t Parcel::errorCheck() const
529{
530 return mError;
531}
532
533void Parcel::setError(status_t err)
534{
535 mError = err;
536}
537
538status_t Parcel::finishWrite(size_t len)
539{
540 //printf("Finish write of %d\n", len);
541 mDataPos += len;
Steve Block6807e592011-10-20 11:56:00 +0100542 ALOGV("finishWrite Setting data pos of %p to %d\n", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700543 if (mDataPos > mDataSize) {
544 mDataSize = mDataPos;
Steve Block6807e592011-10-20 11:56:00 +0100545 ALOGV("finishWrite Setting data size of %p to %d\n", this, mDataSize);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700546 }
547 //printf("New pos=%d, size=%d\n", mDataPos, mDataSize);
548 return NO_ERROR;
549}
550
551status_t Parcel::writeUnpadded(const void* data, size_t len)
552{
553 size_t end = mDataPos + len;
554 if (end < mDataPos) {
555 // integer overflow
556 return BAD_VALUE;
557 }
558
559 if (end <= mDataCapacity) {
560restart_write:
561 memcpy(mData+mDataPos, data, len);
562 return finishWrite(len);
563 }
564
565 status_t err = growData(len);
566 if (err == NO_ERROR) goto restart_write;
567 return err;
568}
569
570status_t Parcel::write(const void* data, size_t len)
571{
572 void* const d = writeInplace(len);
573 if (d) {
574 memcpy(d, data, len);
575 return NO_ERROR;
576 }
577 return mError;
578}
579
580void* Parcel::writeInplace(size_t len)
581{
582 const size_t padded = PAD_SIZE(len);
583
584 // sanity check for integer overflow
585 if (mDataPos+padded < mDataPos) {
586 return NULL;
587 }
588
589 if ((mDataPos+padded) <= mDataCapacity) {
590restart_write:
591 //printf("Writing %ld bytes, padded to %ld\n", len, padded);
592 uint8_t* const data = mData+mDataPos;
593
594 // Need to pad at end?
595 if (padded != len) {
596#if BYTE_ORDER == BIG_ENDIAN
597 static const uint32_t mask[4] = {
598 0x00000000, 0xffffff00, 0xffff0000, 0xff000000
599 };
600#endif
601#if BYTE_ORDER == LITTLE_ENDIAN
602 static const uint32_t mask[4] = {
603 0x00000000, 0x00ffffff, 0x0000ffff, 0x000000ff
604 };
605#endif
606 //printf("Applying pad mask: %p to %p\n", (void*)mask[padded-len],
607 // *reinterpret_cast<void**>(data+padded-4));
608 *reinterpret_cast<uint32_t*>(data+padded-4) &= mask[padded-len];
609 }
610
611 finishWrite(padded);
612 return data;
613 }
614
615 status_t err = growData(padded);
616 if (err == NO_ERROR) goto restart_write;
617 return NULL;
618}
619
620status_t Parcel::writeInt32(int32_t val)
621{
Andreas Huber84a6d042009-08-17 13:33:27 -0700622 return writeAligned(val);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700623}
Marco Nelissen5c0106e2013-10-16 10:57:51 -0700624status_t Parcel::writeInt32Array(size_t len, const int32_t *val) {
625 if (!val) {
626 return writeAligned(-1);
627 }
628 status_t ret = writeAligned(len);
629 if (ret == NO_ERROR) {
630 ret = write(val, len * sizeof(*val));
631 }
632 return ret;
633}
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700634
635status_t Parcel::writeInt64(int64_t val)
636{
Andreas Huber84a6d042009-08-17 13:33:27 -0700637 return writeAligned(val);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700638}
639
Serban Constantinescuf683e012013-11-05 16:53:55 +0000640status_t Parcel::writePointer(uintptr_t val)
641{
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800642 return writeAligned<binder_uintptr_t>(val);
Serban Constantinescuf683e012013-11-05 16:53:55 +0000643}
644
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700645status_t Parcel::writeFloat(float val)
646{
Andreas Huber84a6d042009-08-17 13:33:27 -0700647 return writeAligned(val);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700648}
649
Douglas Leungcc1a4bb2013-01-11 15:00:55 -0800650#if defined(__mips__) && defined(__mips_hard_float)
651
652status_t Parcel::writeDouble(double val)
653{
654 union {
655 double d;
656 unsigned long long ll;
657 } u;
658 u.d = val;
659 return writeAligned(u.ll);
660}
661
662#else
663
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700664status_t Parcel::writeDouble(double val)
665{
Andreas Huber84a6d042009-08-17 13:33:27 -0700666 return writeAligned(val);
667}
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700668
Douglas Leungcc1a4bb2013-01-11 15:00:55 -0800669#endif
670
Andreas Huber84a6d042009-08-17 13:33:27 -0700671status_t Parcel::writeIntPtr(intptr_t val)
672{
673 return writeAligned(val);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700674}
675
676status_t Parcel::writeCString(const char* str)
677{
678 return write(str, strlen(str)+1);
679}
680
681status_t Parcel::writeString8(const String8& str)
682{
683 status_t err = writeInt32(str.bytes());
Pravat Dalbeherad1dff8d2010-12-15 08:40:00 +0100684 // only write string if its length is more than zero characters,
685 // as readString8 will only read if the length field is non-zero.
686 // this is slightly different from how writeString16 works.
687 if (str.bytes() > 0 && err == NO_ERROR) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700688 err = write(str.string(), str.bytes()+1);
689 }
690 return err;
691}
692
693status_t Parcel::writeString16(const String16& str)
694{
695 return writeString16(str.string(), str.size());
696}
697
698status_t Parcel::writeString16(const char16_t* str, size_t len)
699{
700 if (str == NULL) return writeInt32(-1);
701
702 status_t err = writeInt32(len);
703 if (err == NO_ERROR) {
704 len *= sizeof(char16_t);
705 uint8_t* data = (uint8_t*)writeInplace(len+sizeof(char16_t));
706 if (data) {
707 memcpy(data, str, len);
708 *reinterpret_cast<char16_t*>(data+len) = 0;
709 return NO_ERROR;
710 }
711 err = mError;
712 }
713 return err;
714}
715
716status_t Parcel::writeStrongBinder(const sp<IBinder>& val)
717{
718 return flatten_binder(ProcessState::self(), val, this);
719}
720
721status_t Parcel::writeWeakBinder(const wp<IBinder>& val)
722{
723 return flatten_binder(ProcessState::self(), val, this);
724}
725
Mathias Agopiana47f02a2009-05-21 16:29:38 -0700726status_t Parcel::writeNativeHandle(const native_handle* handle)
The Android Open Source Project5f78a482009-01-20 14:03:58 -0800727{
Mathias Agopian1d0a95b2009-07-31 16:12:13 -0700728 if (!handle || handle->version != sizeof(native_handle))
The Android Open Source Project5f78a482009-01-20 14:03:58 -0800729 return BAD_TYPE;
730
731 status_t err;
Mathias Agopiana47f02a2009-05-21 16:29:38 -0700732 err = writeInt32(handle->numFds);
The Android Open Source Project5f78a482009-01-20 14:03:58 -0800733 if (err != NO_ERROR) return err;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800734
Mathias Agopiana47f02a2009-05-21 16:29:38 -0700735 err = writeInt32(handle->numInts);
The Android Open Source Project5f78a482009-01-20 14:03:58 -0800736 if (err != NO_ERROR) return err;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800737
Mathias Agopiana47f02a2009-05-21 16:29:38 -0700738 for (int i=0 ; err==NO_ERROR && i<handle->numFds ; i++)
739 err = writeDupFileDescriptor(handle->data[i]);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800740
741 if (err != NO_ERROR) {
Steve Block9d453682011-12-20 16:23:08 +0000742 ALOGD("write native handle, write dup fd failed");
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800743 return err;
744 }
Mathias Agopiana47f02a2009-05-21 16:29:38 -0700745 err = write(handle->data + handle->numFds, sizeof(int)*handle->numInts);
The Android Open Source Project5f78a482009-01-20 14:03:58 -0800746 return err;
747}
748
Jeff Brown93ff1f92011-11-04 19:01:44 -0700749status_t Parcel::writeFileDescriptor(int fd, bool takeOwnership)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700750{
751 flat_binder_object obj;
752 obj.type = BINDER_TYPE_FD;
753 obj.flags = 0x7f | FLAT_BINDER_FLAG_ACCEPTS_FDS;
Arve Hjønnevåg07fd0f12014-02-18 21:10:29 -0800754 obj.binder = 0; /* Don't pass uninitialized stack data to a remote process */
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700755 obj.handle = fd;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800756 obj.cookie = takeOwnership ? 1 : 0;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700757 return writeObject(obj, true);
758}
759
760status_t Parcel::writeDupFileDescriptor(int fd)
761{
Jeff Brownd341c712011-11-04 20:19:33 -0700762 int dupFd = dup(fd);
763 if (dupFd < 0) {
764 return -errno;
765 }
766 status_t err = writeFileDescriptor(dupFd, true /*takeOwnership*/);
767 if (err) {
768 close(dupFd);
769 }
770 return err;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700771}
772
Mike Lockwoodcbe36fe2013-09-05 08:41:06 -0700773// WARNING: This method must stay in sync with
774// Parcelable.Creator<ParcelFileDescriptor> CREATOR
775// in frameworks/base/core/java/android/os/ParcelFileDescriptor.java
776status_t Parcel::writeParcelFileDescriptor(int fd, int commChannel) {
777 status_t status;
778
779 if (fd < 0) {
780 status = writeInt32(0); // ParcelFileDescriptor is null
781 if (status) return status;
782 } else {
783 status = writeInt32(1); // ParcelFileDescriptor is not null
784 if (status) return status;
785 status = writeDupFileDescriptor(fd);
786 if (status) return status;
787 if (commChannel < 0) {
788 status = writeInt32(0); // commChannel is null
789 if (status) return status;
790 } else {
791 status = writeInt32(1); // commChannel is not null
792 if (status) return status;
793 status = writeDupFileDescriptor(commChannel);
794 }
795 }
796 return status;
797}
798
Jeff Brown5707dbf2011-09-23 21:17:56 -0700799status_t Parcel::writeBlob(size_t len, WritableBlob* outBlob)
800{
801 status_t status;
802
803 if (!mAllowFds || len <= IN_PLACE_BLOB_LIMIT) {
Steve Block6807e592011-10-20 11:56:00 +0100804 ALOGV("writeBlob: write in place");
Jeff Brown5707dbf2011-09-23 21:17:56 -0700805 status = writeInt32(0);
806 if (status) return status;
807
808 void* ptr = writeInplace(len);
809 if (!ptr) return NO_MEMORY;
810
811 outBlob->init(false /*mapped*/, ptr, len);
812 return NO_ERROR;
813 }
814
Steve Block6807e592011-10-20 11:56:00 +0100815 ALOGV("writeBlob: write to ashmem");
Jeff Brown5707dbf2011-09-23 21:17:56 -0700816 int fd = ashmem_create_region("Parcel Blob", len);
817 if (fd < 0) return NO_MEMORY;
818
819 int result = ashmem_set_prot_region(fd, PROT_READ | PROT_WRITE);
820 if (result < 0) {
Jeff Brownec4e0062011-10-10 14:50:10 -0700821 status = result;
Jeff Brown5707dbf2011-09-23 21:17:56 -0700822 } else {
823 void* ptr = ::mmap(NULL, len, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
824 if (ptr == MAP_FAILED) {
825 status = -errno;
826 } else {
827 result = ashmem_set_prot_region(fd, PROT_READ);
828 if (result < 0) {
Jeff Brownec4e0062011-10-10 14:50:10 -0700829 status = result;
Jeff Brown5707dbf2011-09-23 21:17:56 -0700830 } else {
831 status = writeInt32(1);
832 if (!status) {
Jeff Brown93ff1f92011-11-04 19:01:44 -0700833 status = writeFileDescriptor(fd, true /*takeOwnership*/);
Jeff Brown5707dbf2011-09-23 21:17:56 -0700834 if (!status) {
835 outBlob->init(true /*mapped*/, ptr, len);
836 return NO_ERROR;
837 }
838 }
839 }
840 }
841 ::munmap(ptr, len);
842 }
843 ::close(fd);
844 return status;
845}
846
Mathias Agopiane1424282013-07-29 21:24:40 -0700847status_t Parcel::write(const FlattenableHelperInterface& val)
Mathias Agopian98e71dd2010-02-11 17:30:52 -0800848{
849 status_t err;
850
851 // size if needed
Mathias Agopiane1424282013-07-29 21:24:40 -0700852 const size_t len = val.getFlattenedSize();
853 const size_t fd_count = val.getFdCount();
Mathias Agopian98e71dd2010-02-11 17:30:52 -0800854
855 err = this->writeInt32(len);
856 if (err) return err;
857
858 err = this->writeInt32(fd_count);
859 if (err) return err;
860
861 // payload
Mathias Agopiane1424282013-07-29 21:24:40 -0700862 void* const buf = this->writeInplace(PAD_SIZE(len));
Mathias Agopian98e71dd2010-02-11 17:30:52 -0800863 if (buf == NULL)
864 return BAD_VALUE;
865
866 int* fds = NULL;
867 if (fd_count) {
868 fds = new int[fd_count];
869 }
870
871 err = val.flatten(buf, len, fds, fd_count);
872 for (size_t i=0 ; i<fd_count && err==NO_ERROR ; i++) {
873 err = this->writeDupFileDescriptor( fds[i] );
874 }
875
876 if (fd_count) {
877 delete [] fds;
878 }
879
880 return err;
881}
882
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700883status_t Parcel::writeObject(const flat_binder_object& val, bool nullMetaData)
884{
885 const bool enoughData = (mDataPos+sizeof(val)) <= mDataCapacity;
886 const bool enoughObjects = mObjectsSize < mObjectsCapacity;
887 if (enoughData && enoughObjects) {
888restart_write:
889 *reinterpret_cast<flat_binder_object*>(mData+mDataPos) = val;
890
891 // Need to write meta-data?
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800892 if (nullMetaData || val.binder != 0) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700893 mObjects[mObjectsSize] = mDataPos;
894 acquire_object(ProcessState::self(), val, this);
895 mObjectsSize++;
896 }
897
898 // remember if it's a file descriptor
899 if (val.type == BINDER_TYPE_FD) {
Dianne Hackborn8938ed22011-09-28 23:19:47 -0400900 if (!mAllowFds) {
901 return FDS_NOT_ALLOWED;
902 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700903 mHasFds = mFdsKnown = true;
904 }
905
906 return finishWrite(sizeof(flat_binder_object));
907 }
908
909 if (!enoughData) {
910 const status_t err = growData(sizeof(val));
911 if (err != NO_ERROR) return err;
912 }
913 if (!enoughObjects) {
914 size_t newSize = ((mObjectsSize+2)*3)/2;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800915 binder_size_t* objects = (binder_size_t*)realloc(mObjects, newSize*sizeof(binder_size_t));
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700916 if (objects == NULL) return NO_MEMORY;
917 mObjects = objects;
918 mObjectsCapacity = newSize;
919 }
920
921 goto restart_write;
922}
923
Brad Fitzpatrick837a0d02010-07-13 15:33:35 -0700924status_t Parcel::writeNoException()
925{
926 return writeInt32(0);
927}
928
Colin Cross6f4f3ab2014-02-05 17:42:44 -0800929void Parcel::remove(size_t /*start*/, size_t /*amt*/)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700930{
931 LOG_ALWAYS_FATAL("Parcel::remove() not yet implemented!");
932}
933
934status_t Parcel::read(void* outData, size_t len) const
935{
936 if ((mDataPos+PAD_SIZE(len)) >= mDataPos && (mDataPos+PAD_SIZE(len)) <= mDataSize) {
937 memcpy(outData, mData+mDataPos, len);
938 mDataPos += PAD_SIZE(len);
Steve Block6807e592011-10-20 11:56:00 +0100939 ALOGV("read Setting data pos of %p to %d\n", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700940 return NO_ERROR;
941 }
942 return NOT_ENOUGH_DATA;
943}
944
945const void* Parcel::readInplace(size_t len) const
946{
947 if ((mDataPos+PAD_SIZE(len)) >= mDataPos && (mDataPos+PAD_SIZE(len)) <= mDataSize) {
948 const void* data = mData+mDataPos;
949 mDataPos += PAD_SIZE(len);
Steve Block6807e592011-10-20 11:56:00 +0100950 ALOGV("readInplace Setting data pos of %p to %d\n", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700951 return data;
952 }
953 return NULL;
954}
955
Andreas Huber84a6d042009-08-17 13:33:27 -0700956template<class T>
957status_t Parcel::readAligned(T *pArg) const {
958 COMPILE_TIME_ASSERT_FUNCTION_SCOPE(PAD_SIZE(sizeof(T)) == sizeof(T));
959
960 if ((mDataPos+sizeof(T)) <= mDataSize) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700961 const void* data = mData+mDataPos;
Andreas Huber84a6d042009-08-17 13:33:27 -0700962 mDataPos += sizeof(T);
963 *pArg = *reinterpret_cast<const T*>(data);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700964 return NO_ERROR;
965 } else {
966 return NOT_ENOUGH_DATA;
967 }
968}
969
Andreas Huber84a6d042009-08-17 13:33:27 -0700970template<class T>
971T Parcel::readAligned() const {
972 T result;
973 if (readAligned(&result) != NO_ERROR) {
974 result = 0;
975 }
976
977 return result;
978}
979
980template<class T>
981status_t Parcel::writeAligned(T val) {
982 COMPILE_TIME_ASSERT_FUNCTION_SCOPE(PAD_SIZE(sizeof(T)) == sizeof(T));
983
984 if ((mDataPos+sizeof(val)) <= mDataCapacity) {
985restart_write:
986 *reinterpret_cast<T*>(mData+mDataPos) = val;
987 return finishWrite(sizeof(val));
988 }
989
990 status_t err = growData(sizeof(val));
991 if (err == NO_ERROR) goto restart_write;
992 return err;
993}
994
995status_t Parcel::readInt32(int32_t *pArg) const
996{
997 return readAligned(pArg);
998}
999
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001000int32_t Parcel::readInt32() const
1001{
Andreas Huber84a6d042009-08-17 13:33:27 -07001002 return readAligned<int32_t>();
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001003}
1004
1005
1006status_t Parcel::readInt64(int64_t *pArg) const
1007{
Andreas Huber84a6d042009-08-17 13:33:27 -07001008 return readAligned(pArg);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001009}
1010
1011
1012int64_t Parcel::readInt64() const
1013{
Andreas Huber84a6d042009-08-17 13:33:27 -07001014 return readAligned<int64_t>();
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001015}
1016
Serban Constantinescuf683e012013-11-05 16:53:55 +00001017status_t Parcel::readPointer(uintptr_t *pArg) const
1018{
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001019 status_t ret;
1020 binder_uintptr_t ptr;
1021 ret = readAligned(&ptr);
1022 if (!ret)
1023 *pArg = ptr;
1024 return ret;
Serban Constantinescuf683e012013-11-05 16:53:55 +00001025}
1026
1027uintptr_t Parcel::readPointer() const
1028{
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001029 return readAligned<binder_uintptr_t>();
Serban Constantinescuf683e012013-11-05 16:53:55 +00001030}
1031
1032
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001033status_t Parcel::readFloat(float *pArg) const
1034{
Andreas Huber84a6d042009-08-17 13:33:27 -07001035 return readAligned(pArg);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001036}
1037
1038
1039float Parcel::readFloat() const
1040{
Andreas Huber84a6d042009-08-17 13:33:27 -07001041 return readAligned<float>();
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001042}
1043
Douglas Leungcc1a4bb2013-01-11 15:00:55 -08001044#if defined(__mips__) && defined(__mips_hard_float)
1045
1046status_t Parcel::readDouble(double *pArg) const
1047{
1048 union {
1049 double d;
1050 unsigned long long ll;
1051 } u;
1052 status_t status;
1053 status = readAligned(&u.ll);
1054 *pArg = u.d;
1055 return status;
1056}
1057
1058double Parcel::readDouble() const
1059{
1060 union {
1061 double d;
1062 unsigned long long ll;
1063 } u;
1064 u.ll = readAligned<unsigned long long>();
1065 return u.d;
1066}
1067
1068#else
1069
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001070status_t Parcel::readDouble(double *pArg) const
1071{
Andreas Huber84a6d042009-08-17 13:33:27 -07001072 return readAligned(pArg);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001073}
1074
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001075double Parcel::readDouble() const
1076{
Andreas Huber84a6d042009-08-17 13:33:27 -07001077 return readAligned<double>();
1078}
1079
Douglas Leungcc1a4bb2013-01-11 15:00:55 -08001080#endif
1081
Andreas Huber84a6d042009-08-17 13:33:27 -07001082status_t Parcel::readIntPtr(intptr_t *pArg) const
1083{
1084 return readAligned(pArg);
1085}
1086
1087
1088intptr_t Parcel::readIntPtr() const
1089{
1090 return readAligned<intptr_t>();
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001091}
1092
1093
1094const char* Parcel::readCString() const
1095{
1096 const size_t avail = mDataSize-mDataPos;
1097 if (avail > 0) {
1098 const char* str = reinterpret_cast<const char*>(mData+mDataPos);
1099 // is the string's trailing NUL within the parcel's valid bounds?
1100 const char* eos = reinterpret_cast<const char*>(memchr(str, 0, avail));
1101 if (eos) {
1102 const size_t len = eos - str;
1103 mDataPos += PAD_SIZE(len+1);
Steve Block6807e592011-10-20 11:56:00 +01001104 ALOGV("readCString Setting data pos of %p to %d\n", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001105 return str;
1106 }
1107 }
1108 return NULL;
1109}
1110
1111String8 Parcel::readString8() const
1112{
1113 int32_t size = readInt32();
1114 // watch for potential int overflow adding 1 for trailing NUL
1115 if (size > 0 && size < INT32_MAX) {
1116 const char* str = (const char*)readInplace(size+1);
1117 if (str) return String8(str, size);
1118 }
1119 return String8();
1120}
1121
1122String16 Parcel::readString16() const
1123{
1124 size_t len;
1125 const char16_t* str = readString16Inplace(&len);
1126 if (str) return String16(str, len);
Steve Blocke6f43dd2012-01-06 19:20:56 +00001127 ALOGE("Reading a NULL string not supported here.");
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001128 return String16();
1129}
1130
1131const char16_t* Parcel::readString16Inplace(size_t* outLen) const
1132{
1133 int32_t size = readInt32();
1134 // watch for potential int overflow from size+1
1135 if (size >= 0 && size < INT32_MAX) {
1136 *outLen = size;
1137 const char16_t* str = (const char16_t*)readInplace((size+1)*sizeof(char16_t));
1138 if (str != NULL) {
1139 return str;
1140 }
1141 }
1142 *outLen = 0;
1143 return NULL;
1144}
1145
1146sp<IBinder> Parcel::readStrongBinder() const
1147{
1148 sp<IBinder> val;
1149 unflatten_binder(ProcessState::self(), *this, &val);
1150 return val;
1151}
1152
1153wp<IBinder> Parcel::readWeakBinder() const
1154{
1155 wp<IBinder> val;
1156 unflatten_binder(ProcessState::self(), *this, &val);
1157 return val;
1158}
1159
Brad Fitzpatrick837a0d02010-07-13 15:33:35 -07001160int32_t Parcel::readExceptionCode() const
1161{
1162 int32_t exception_code = readAligned<int32_t>();
Brad Fitzpatrickd36f4a52010-07-12 11:05:38 -07001163 if (exception_code == EX_HAS_REPLY_HEADER) {
Magnus Strandberg1ba24572011-05-03 15:44:00 +02001164 int32_t header_start = dataPosition();
Brad Fitzpatrickd36f4a52010-07-12 11:05:38 -07001165 int32_t header_size = readAligned<int32_t>();
1166 // Skip over fat responses headers. Not used (or propagated) in
1167 // native code
Magnus Strandberg1ba24572011-05-03 15:44:00 +02001168 setDataPosition(header_start + header_size);
Brad Fitzpatrickd36f4a52010-07-12 11:05:38 -07001169 // And fat response headers are currently only used when there are no
1170 // exceptions, so return no error:
1171 return 0;
1172 }
Brad Fitzpatrick837a0d02010-07-13 15:33:35 -07001173 return exception_code;
1174}
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001175
Mathias Agopiana47f02a2009-05-21 16:29:38 -07001176native_handle* Parcel::readNativeHandle() const
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001177{
1178 int numFds, numInts;
1179 status_t err;
1180 err = readInt32(&numFds);
1181 if (err != NO_ERROR) return 0;
1182 err = readInt32(&numInts);
1183 if (err != NO_ERROR) return 0;
1184
Mathias Agopiana47f02a2009-05-21 16:29:38 -07001185 native_handle* h = native_handle_create(numFds, numInts);
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001186 for (int i=0 ; err==NO_ERROR && i<numFds ; i++) {
Rebecca Schultz Zavin360211f2009-02-13 16:34:38 -08001187 h->data[i] = dup(readFileDescriptor());
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001188 if (h->data[i] < 0) err = BAD_VALUE;
1189 }
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001190 err = read(h->data + numFds, sizeof(int)*numInts);
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001191 if (err != NO_ERROR) {
Mathias Agopiana47f02a2009-05-21 16:29:38 -07001192 native_handle_close(h);
1193 native_handle_delete(h);
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001194 h = 0;
1195 }
1196 return h;
1197}
1198
1199
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001200int Parcel::readFileDescriptor() const
1201{
1202 const flat_binder_object* flat = readObject(true);
1203 if (flat) {
1204 switch (flat->type) {
1205 case BINDER_TYPE_FD:
Steve Blocka19954a2012-01-04 20:05:49 +00001206 //ALOGI("Returning file descriptor %ld from parcel %p\n", flat->handle, this);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001207 return flat->handle;
1208 }
1209 }
1210 return BAD_TYPE;
1211}
1212
Mike Lockwoodcbe36fe2013-09-05 08:41:06 -07001213// WARNING: This method must stay in sync with writeToParcel()
1214// in frameworks/base/core/java/android/os/ParcelFileDescriptor.java
1215int Parcel::readParcelFileDescriptor(int& outCommChannel) const {
1216 int fd;
1217 outCommChannel = -1;
1218
1219 if (readInt32() == 0) {
1220 fd = -1;
1221 } else {
1222 fd = readFileDescriptor();
1223 if (fd >= 0 && readInt32() != 0) {
1224 outCommChannel = readFileDescriptor();
1225 }
1226 }
1227 return fd;
1228}
1229
Jeff Brown5707dbf2011-09-23 21:17:56 -07001230status_t Parcel::readBlob(size_t len, ReadableBlob* outBlob) const
1231{
1232 int32_t useAshmem;
1233 status_t status = readInt32(&useAshmem);
1234 if (status) return status;
1235
1236 if (!useAshmem) {
Steve Block6807e592011-10-20 11:56:00 +01001237 ALOGV("readBlob: read in place");
Jeff Brown5707dbf2011-09-23 21:17:56 -07001238 const void* ptr = readInplace(len);
1239 if (!ptr) return BAD_VALUE;
1240
1241 outBlob->init(false /*mapped*/, const_cast<void*>(ptr), len);
1242 return NO_ERROR;
1243 }
1244
Steve Block6807e592011-10-20 11:56:00 +01001245 ALOGV("readBlob: read from ashmem");
Jeff Brown5707dbf2011-09-23 21:17:56 -07001246 int fd = readFileDescriptor();
1247 if (fd == int(BAD_TYPE)) return BAD_VALUE;
1248
1249 void* ptr = ::mmap(NULL, len, PROT_READ, MAP_SHARED, fd, 0);
1250 if (!ptr) return NO_MEMORY;
1251
1252 outBlob->init(true /*mapped*/, ptr, len);
1253 return NO_ERROR;
1254}
1255
Mathias Agopiane1424282013-07-29 21:24:40 -07001256status_t Parcel::read(FlattenableHelperInterface& val) const
Mathias Agopian98e71dd2010-02-11 17:30:52 -08001257{
1258 // size
1259 const size_t len = this->readInt32();
1260 const size_t fd_count = this->readInt32();
1261
1262 // payload
Mathias Agopiane1424282013-07-29 21:24:40 -07001263 void const* const buf = this->readInplace(PAD_SIZE(len));
Mathias Agopian98e71dd2010-02-11 17:30:52 -08001264 if (buf == NULL)
1265 return BAD_VALUE;
1266
1267 int* fds = NULL;
1268 if (fd_count) {
1269 fds = new int[fd_count];
1270 }
1271
1272 status_t err = NO_ERROR;
1273 for (size_t i=0 ; i<fd_count && err==NO_ERROR ; i++) {
1274 fds[i] = dup(this->readFileDescriptor());
1275 if (fds[i] < 0) err = BAD_VALUE;
1276 }
1277
1278 if (err == NO_ERROR) {
1279 err = val.unflatten(buf, len, fds, fd_count);
1280 }
1281
1282 if (fd_count) {
1283 delete [] fds;
1284 }
1285
1286 return err;
1287}
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001288const flat_binder_object* Parcel::readObject(bool nullMetaData) const
1289{
1290 const size_t DPOS = mDataPos;
1291 if ((DPOS+sizeof(flat_binder_object)) <= mDataSize) {
1292 const flat_binder_object* obj
1293 = reinterpret_cast<const flat_binder_object*>(mData+DPOS);
1294 mDataPos = DPOS + sizeof(flat_binder_object);
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001295 if (!nullMetaData && (obj->cookie == 0 && obj->binder == 0)) {
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001296 // When transferring a NULL object, we don't write it into
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001297 // the object list, so we don't want to check for it when
1298 // reading.
Steve Block6807e592011-10-20 11:56:00 +01001299 ALOGV("readObject Setting data pos of %p to %d\n", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001300 return obj;
1301 }
1302
1303 // Ensure that this object is valid...
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001304 binder_size_t* const OBJS = mObjects;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001305 const size_t N = mObjectsSize;
1306 size_t opos = mNextObjectHint;
1307
1308 if (N > 0) {
Steve Block6807e592011-10-20 11:56:00 +01001309 ALOGV("Parcel %p looking for obj at %d, hint=%d\n",
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001310 this, DPOS, opos);
1311
1312 // Start at the current hint position, looking for an object at
1313 // the current data position.
1314 if (opos < N) {
1315 while (opos < (N-1) && OBJS[opos] < DPOS) {
1316 opos++;
1317 }
1318 } else {
1319 opos = N-1;
1320 }
1321 if (OBJS[opos] == DPOS) {
1322 // Found it!
Steve Block6807e592011-10-20 11:56:00 +01001323 ALOGV("Parcel found obj %d at index %d with forward search",
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001324 this, DPOS, opos);
1325 mNextObjectHint = opos+1;
Steve Block6807e592011-10-20 11:56:00 +01001326 ALOGV("readObject Setting data pos of %p to %d\n", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001327 return obj;
1328 }
1329
1330 // Look backwards for it...
1331 while (opos > 0 && OBJS[opos] > DPOS) {
1332 opos--;
1333 }
1334 if (OBJS[opos] == DPOS) {
1335 // Found it!
Steve Block6807e592011-10-20 11:56:00 +01001336 ALOGV("Parcel found obj %d at index %d with backward search",
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001337 this, DPOS, opos);
1338 mNextObjectHint = opos+1;
Steve Block6807e592011-10-20 11:56:00 +01001339 ALOGV("readObject Setting data pos of %p to %d\n", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001340 return obj;
1341 }
1342 }
Colin Cross6f4f3ab2014-02-05 17:42:44 -08001343 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 -07001344 this, DPOS);
1345 }
1346 return NULL;
1347}
1348
1349void Parcel::closeFileDescriptors()
1350{
1351 size_t i = mObjectsSize;
1352 if (i > 0) {
Steve Blocka19954a2012-01-04 20:05:49 +00001353 //ALOGI("Closing file descriptors for %d objects...", mObjectsSize);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001354 }
1355 while (i > 0) {
1356 i--;
1357 const flat_binder_object* flat
1358 = reinterpret_cast<flat_binder_object*>(mData+mObjects[i]);
1359 if (flat->type == BINDER_TYPE_FD) {
Steve Blocka19954a2012-01-04 20:05:49 +00001360 //ALOGI("Closing fd: %ld\n", flat->handle);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001361 close(flat->handle);
1362 }
1363 }
1364}
1365
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001366uintptr_t Parcel::ipcData() const
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001367{
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001368 return reinterpret_cast<uintptr_t>(mData);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001369}
1370
1371size_t Parcel::ipcDataSize() const
1372{
1373 return (mDataSize > mDataPos ? mDataSize : mDataPos);
1374}
1375
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001376uintptr_t Parcel::ipcObjects() const
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001377{
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001378 return reinterpret_cast<uintptr_t>(mObjects);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001379}
1380
1381size_t Parcel::ipcObjectsCount() const
1382{
1383 return mObjectsSize;
1384}
1385
1386void Parcel::ipcSetDataReference(const uint8_t* data, size_t dataSize,
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001387 const binder_size_t* objects, size_t objectsCount, release_func relFunc, void* relCookie)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001388{
Arve Hjønnevåg6f286112014-02-19 20:42:13 -08001389 binder_size_t minOffset = 0;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001390 freeDataNoInit();
1391 mError = NO_ERROR;
1392 mData = const_cast<uint8_t*>(data);
1393 mDataSize = mDataCapacity = dataSize;
Steve Blocka19954a2012-01-04 20:05:49 +00001394 //ALOGI("setDataReference Setting data size of %p to %lu (pid=%d)\n", this, mDataSize, getpid());
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001395 mDataPos = 0;
Steve Block6807e592011-10-20 11:56:00 +01001396 ALOGV("setDataReference Setting data pos of %p to %d\n", this, mDataPos);
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001397 mObjects = const_cast<binder_size_t*>(objects);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001398 mObjectsSize = mObjectsCapacity = objectsCount;
1399 mNextObjectHint = 0;
1400 mOwner = relFunc;
1401 mOwnerCookie = relCookie;
Arve Hjønnevågf50b9ea2014-02-13 19:22:08 -08001402 for (size_t i = 0; i < mObjectsSize; i++) {
Arve Hjønnevåg6f286112014-02-19 20:42:13 -08001403 binder_size_t offset = mObjects[i];
Arve Hjønnevågf50b9ea2014-02-13 19:22:08 -08001404 if (offset < minOffset) {
Arve Hjønnevåg6f286112014-02-19 20:42:13 -08001405 ALOGE("%s: bad object offset %"PRIu64" < %"PRIu64"\n",
1406 __func__, (uint64_t)offset, (uint64_t)minOffset);
Arve Hjønnevågf50b9ea2014-02-13 19:22:08 -08001407 mObjectsSize = 0;
1408 break;
1409 }
1410 minOffset = offset + sizeof(flat_binder_object);
1411 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001412 scanForFds();
1413}
1414
Colin Cross6f4f3ab2014-02-05 17:42:44 -08001415void Parcel::print(TextOutput& to, uint32_t /*flags*/) const
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001416{
1417 to << "Parcel(";
1418
1419 if (errorCheck() != NO_ERROR) {
1420 const status_t err = errorCheck();
Colin Cross6f4f3ab2014-02-05 17:42:44 -08001421 to << "Error: " << (void*)(intptr_t)err << " \"" << strerror(-err) << "\"";
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001422 } else if (dataSize() > 0) {
1423 const uint8_t* DATA = data();
1424 to << indent << HexDump(DATA, dataSize()) << dedent;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001425 const binder_size_t* OBJS = objects();
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001426 const size_t N = objectsCount();
1427 for (size_t i=0; i<N; i++) {
1428 const flat_binder_object* flat
1429 = reinterpret_cast<const flat_binder_object*>(DATA+OBJS[i]);
1430 to << endl << "Object #" << i << " @ " << (void*)OBJS[i] << ": "
1431 << TypeCode(flat->type & 0x7f7f7f00)
1432 << " = " << flat->binder;
1433 }
1434 } else {
1435 to << "NULL";
1436 }
1437
1438 to << ")";
1439}
1440
1441void Parcel::releaseObjects()
1442{
1443 const sp<ProcessState> proc(ProcessState::self());
1444 size_t i = mObjectsSize;
1445 uint8_t* const data = mData;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001446 binder_size_t* const objects = mObjects;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001447 while (i > 0) {
1448 i--;
1449 const flat_binder_object* flat
1450 = reinterpret_cast<flat_binder_object*>(data+objects[i]);
1451 release_object(proc, *flat, this);
1452 }
1453}
1454
1455void Parcel::acquireObjects()
1456{
1457 const sp<ProcessState> proc(ProcessState::self());
1458 size_t i = mObjectsSize;
1459 uint8_t* const data = mData;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001460 binder_size_t* const objects = mObjects;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001461 while (i > 0) {
1462 i--;
1463 const flat_binder_object* flat
1464 = reinterpret_cast<flat_binder_object*>(data+objects[i]);
1465 acquire_object(proc, *flat, this);
1466 }
1467}
1468
1469void Parcel::freeData()
1470{
1471 freeDataNoInit();
1472 initState();
1473}
1474
1475void Parcel::freeDataNoInit()
1476{
1477 if (mOwner) {
Steve Blocka19954a2012-01-04 20:05:49 +00001478 //ALOGI("Freeing data ref of %p (pid=%d)\n", this, getpid());
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001479 mOwner(this, mData, mDataSize, mObjects, mObjectsSize, mOwnerCookie);
1480 } else {
1481 releaseObjects();
1482 if (mData) free(mData);
1483 if (mObjects) free(mObjects);
1484 }
1485}
1486
1487status_t Parcel::growData(size_t len)
1488{
1489 size_t newSize = ((mDataSize+len)*3)/2;
1490 return (newSize <= mDataSize)
1491 ? (status_t) NO_MEMORY
1492 : continueWrite(newSize);
1493}
1494
1495status_t Parcel::restartWrite(size_t desired)
1496{
1497 if (mOwner) {
1498 freeData();
1499 return continueWrite(desired);
1500 }
1501
1502 uint8_t* data = (uint8_t*)realloc(mData, desired);
1503 if (!data && desired > mDataCapacity) {
1504 mError = NO_MEMORY;
1505 return NO_MEMORY;
1506 }
1507
1508 releaseObjects();
1509
1510 if (data) {
1511 mData = data;
1512 mDataCapacity = desired;
1513 }
1514
1515 mDataSize = mDataPos = 0;
Steve Block6807e592011-10-20 11:56:00 +01001516 ALOGV("restartWrite Setting data size of %p to %d\n", this, mDataSize);
1517 ALOGV("restartWrite Setting data pos of %p to %d\n", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001518
1519 free(mObjects);
1520 mObjects = NULL;
1521 mObjectsSize = mObjectsCapacity = 0;
1522 mNextObjectHint = 0;
1523 mHasFds = false;
1524 mFdsKnown = true;
Dianne Hackborn8938ed22011-09-28 23:19:47 -04001525 mAllowFds = true;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001526
1527 return NO_ERROR;
1528}
1529
1530status_t Parcel::continueWrite(size_t desired)
1531{
1532 // If shrinking, first adjust for any objects that appear
1533 // after the new data size.
1534 size_t objectsSize = mObjectsSize;
1535 if (desired < mDataSize) {
1536 if (desired == 0) {
1537 objectsSize = 0;
1538 } else {
1539 while (objectsSize > 0) {
1540 if (mObjects[objectsSize-1] < desired)
1541 break;
1542 objectsSize--;
1543 }
1544 }
1545 }
1546
1547 if (mOwner) {
1548 // If the size is going to zero, just release the owner's data.
1549 if (desired == 0) {
1550 freeData();
1551 return NO_ERROR;
1552 }
1553
1554 // If there is a different owner, we need to take
1555 // posession.
1556 uint8_t* data = (uint8_t*)malloc(desired);
1557 if (!data) {
1558 mError = NO_MEMORY;
1559 return NO_MEMORY;
1560 }
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001561 binder_size_t* objects = NULL;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001562
1563 if (objectsSize) {
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001564 objects = (binder_size_t*)malloc(objectsSize*sizeof(binder_size_t));
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001565 if (!objects) {
Hyejin Kim3f727c02013-03-09 11:28:54 +09001566 free(data);
1567
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001568 mError = NO_MEMORY;
1569 return NO_MEMORY;
1570 }
1571
1572 // Little hack to only acquire references on objects
1573 // we will be keeping.
1574 size_t oldObjectsSize = mObjectsSize;
1575 mObjectsSize = objectsSize;
1576 acquireObjects();
1577 mObjectsSize = oldObjectsSize;
1578 }
1579
1580 if (mData) {
1581 memcpy(data, mData, mDataSize < desired ? mDataSize : desired);
1582 }
1583 if (objects && mObjects) {
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001584 memcpy(objects, mObjects, objectsSize*sizeof(binder_size_t));
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001585 }
Steve Blocka19954a2012-01-04 20:05:49 +00001586 //ALOGI("Freeing data ref of %p (pid=%d)\n", this, getpid());
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001587 mOwner(this, mData, mDataSize, mObjects, mObjectsSize, mOwnerCookie);
1588 mOwner = NULL;
1589
1590 mData = data;
1591 mObjects = objects;
1592 mDataSize = (mDataSize < desired) ? mDataSize : desired;
Steve Block6807e592011-10-20 11:56:00 +01001593 ALOGV("continueWrite Setting data size of %p to %d\n", this, mDataSize);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001594 mDataCapacity = desired;
1595 mObjectsSize = mObjectsCapacity = objectsSize;
1596 mNextObjectHint = 0;
1597
1598 } else if (mData) {
1599 if (objectsSize < mObjectsSize) {
1600 // Need to release refs on any objects we are dropping.
1601 const sp<ProcessState> proc(ProcessState::self());
1602 for (size_t i=objectsSize; i<mObjectsSize; i++) {
1603 const flat_binder_object* flat
1604 = reinterpret_cast<flat_binder_object*>(mData+mObjects[i]);
1605 if (flat->type == BINDER_TYPE_FD) {
1606 // will need to rescan because we may have lopped off the only FDs
1607 mFdsKnown = false;
1608 }
1609 release_object(proc, *flat, this);
1610 }
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001611 binder_size_t* objects =
1612 (binder_size_t*)realloc(mObjects, objectsSize*sizeof(binder_size_t));
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001613 if (objects) {
1614 mObjects = objects;
1615 }
1616 mObjectsSize = objectsSize;
1617 mNextObjectHint = 0;
1618 }
1619
1620 // We own the data, so we can just do a realloc().
1621 if (desired > mDataCapacity) {
1622 uint8_t* data = (uint8_t*)realloc(mData, desired);
1623 if (data) {
1624 mData = data;
1625 mDataCapacity = desired;
1626 } else if (desired > mDataCapacity) {
1627 mError = NO_MEMORY;
1628 return NO_MEMORY;
1629 }
1630 } else {
Dianne Hackborn97e2bcd2011-04-13 18:15:56 -07001631 if (mDataSize > desired) {
1632 mDataSize = desired;
Steve Block6807e592011-10-20 11:56:00 +01001633 ALOGV("continueWrite Setting data size of %p to %d\n", this, mDataSize);
Dianne Hackborn97e2bcd2011-04-13 18:15:56 -07001634 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001635 if (mDataPos > desired) {
1636 mDataPos = desired;
Steve Block6807e592011-10-20 11:56:00 +01001637 ALOGV("continueWrite Setting data pos of %p to %d\n", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001638 }
1639 }
1640
1641 } else {
1642 // This is the first data. Easy!
1643 uint8_t* data = (uint8_t*)malloc(desired);
1644 if (!data) {
1645 mError = NO_MEMORY;
1646 return NO_MEMORY;
1647 }
Hyejin Kim3f727c02013-03-09 11:28:54 +09001648
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001649 if(!(mDataCapacity == 0 && mObjects == NULL
1650 && mObjectsCapacity == 0)) {
Colin Cross6f4f3ab2014-02-05 17:42:44 -08001651 ALOGE("continueWrite: %zu/%p/%zu/%zu", mDataCapacity, mObjects, mObjectsCapacity, desired);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001652 }
1653
1654 mData = data;
1655 mDataSize = mDataPos = 0;
Steve Block6807e592011-10-20 11:56:00 +01001656 ALOGV("continueWrite Setting data size of %p to %d\n", this, mDataSize);
1657 ALOGV("continueWrite Setting data pos of %p to %d\n", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001658 mDataCapacity = desired;
1659 }
1660
1661 return NO_ERROR;
1662}
1663
1664void Parcel::initState()
1665{
1666 mError = NO_ERROR;
1667 mData = 0;
1668 mDataSize = 0;
1669 mDataCapacity = 0;
1670 mDataPos = 0;
Steve Block6807e592011-10-20 11:56:00 +01001671 ALOGV("initState Setting data size of %p to %d\n", this, mDataSize);
1672 ALOGV("initState Setting data pos of %p to %d\n", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001673 mObjects = NULL;
1674 mObjectsSize = 0;
1675 mObjectsCapacity = 0;
1676 mNextObjectHint = 0;
1677 mHasFds = false;
1678 mFdsKnown = true;
Dianne Hackborn8938ed22011-09-28 23:19:47 -04001679 mAllowFds = true;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001680 mOwner = NULL;
1681}
1682
1683void Parcel::scanForFds() const
1684{
1685 bool hasFds = false;
1686 for (size_t i=0; i<mObjectsSize; i++) {
1687 const flat_binder_object* flat
1688 = reinterpret_cast<const flat_binder_object*>(mData + mObjects[i]);
1689 if (flat->type == BINDER_TYPE_FD) {
1690 hasFds = true;
1691 break;
1692 }
1693 }
1694 mHasFds = hasFds;
1695 mFdsKnown = true;
1696}
1697
Jeff Brown5707dbf2011-09-23 21:17:56 -07001698// --- Parcel::Blob ---
1699
1700Parcel::Blob::Blob() :
1701 mMapped(false), mData(NULL), mSize(0) {
1702}
1703
1704Parcel::Blob::~Blob() {
1705 release();
1706}
1707
1708void Parcel::Blob::release() {
1709 if (mMapped && mData) {
1710 ::munmap(mData, mSize);
1711 }
1712 clear();
1713}
1714
1715void Parcel::Blob::init(bool mapped, void* data, size_t size) {
1716 mMapped = mapped;
1717 mData = data;
1718 mSize = size;
1719}
1720
1721void Parcel::Blob::clear() {
1722 mMapped = false;
1723 mData = NULL;
1724 mSize = 0;
1725}
1726
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001727}; // namespace android