blob: 88e20e4af5821ae008d4f663bdc13f4198985fbe [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;
168 obj.handle = handle;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800169 obj.cookie = 0;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700170 } else {
171 obj.type = BINDER_TYPE_BINDER;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800172 obj.binder = reinterpret_cast<uintptr_t>(local->getWeakRefs());
173 obj.cookie = reinterpret_cast<uintptr_t>(local);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700174 }
175 } else {
176 obj.type = BINDER_TYPE_BINDER;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800177 obj.binder = 0;
178 obj.cookie = 0;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700179 }
180
181 return finish_flatten_binder(binder, obj, out);
182}
183
Colin Cross6f4f3ab2014-02-05 17:42:44 -0800184status_t flatten_binder(const sp<ProcessState>& /*proc*/,
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700185 const wp<IBinder>& binder, Parcel* out)
186{
187 flat_binder_object obj;
188
189 obj.flags = 0x7f | FLAT_BINDER_FLAG_ACCEPTS_FDS;
190 if (binder != NULL) {
191 sp<IBinder> real = binder.promote();
192 if (real != NULL) {
193 IBinder *local = real->localBinder();
194 if (!local) {
195 BpBinder *proxy = real->remoteBinder();
196 if (proxy == NULL) {
Steve Blocke6f43dd2012-01-06 19:20:56 +0000197 ALOGE("null proxy");
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700198 }
199 const int32_t handle = proxy ? proxy->handle() : 0;
200 obj.type = BINDER_TYPE_WEAK_HANDLE;
201 obj.handle = handle;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800202 obj.cookie = 0;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700203 } else {
204 obj.type = BINDER_TYPE_WEAK_BINDER;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800205 obj.binder = reinterpret_cast<uintptr_t>(binder.get_refs());
206 obj.cookie = reinterpret_cast<uintptr_t>(binder.unsafe_get());
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700207 }
208 return finish_flatten_binder(real, obj, out);
209 }
210
211 // XXX How to deal? In order to flatten the given binder,
212 // we need to probe it for information, which requires a primary
213 // reference... but we don't have one.
214 //
215 // The OpenBinder implementation uses a dynamic_cast<> here,
216 // but we can't do that with the different reference counting
217 // implementation we are using.
Steve Blocke6f43dd2012-01-06 19:20:56 +0000218 ALOGE("Unable to unflatten Binder weak reference!");
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700219 obj.type = BINDER_TYPE_BINDER;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800220 obj.binder = 0;
221 obj.cookie = 0;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700222 return finish_flatten_binder(NULL, obj, out);
223
224 } else {
225 obj.type = BINDER_TYPE_BINDER;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800226 obj.binder = 0;
227 obj.cookie = 0;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700228 return finish_flatten_binder(NULL, obj, out);
229 }
230}
231
232inline static status_t finish_unflatten_binder(
Colin Cross6f4f3ab2014-02-05 17:42:44 -0800233 BpBinder* /*proxy*/, const flat_binder_object& /*flat*/,
234 const Parcel& /*in*/)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700235{
236 return NO_ERROR;
237}
238
239status_t unflatten_binder(const sp<ProcessState>& proc,
240 const Parcel& in, sp<IBinder>* out)
241{
242 const flat_binder_object* flat = in.readObject(false);
243
244 if (flat) {
245 switch (flat->type) {
246 case BINDER_TYPE_BINDER:
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800247 *out = reinterpret_cast<IBinder*>(flat->cookie);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700248 return finish_unflatten_binder(NULL, *flat, in);
249 case BINDER_TYPE_HANDLE:
250 *out = proc->getStrongProxyForHandle(flat->handle);
251 return finish_unflatten_binder(
252 static_cast<BpBinder*>(out->get()), *flat, in);
253 }
254 }
255 return BAD_TYPE;
256}
257
258status_t unflatten_binder(const sp<ProcessState>& proc,
259 const Parcel& in, wp<IBinder>* out)
260{
261 const flat_binder_object* flat = in.readObject(false);
262
263 if (flat) {
264 switch (flat->type) {
265 case BINDER_TYPE_BINDER:
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800266 *out = reinterpret_cast<IBinder*>(flat->cookie);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700267 return finish_unflatten_binder(NULL, *flat, in);
268 case BINDER_TYPE_WEAK_BINDER:
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800269 if (flat->binder != 0) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700270 out->set_object_and_refs(
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800271 reinterpret_cast<IBinder*>(flat->cookie),
272 reinterpret_cast<RefBase::weakref_type*>(flat->binder));
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700273 } else {
274 *out = NULL;
275 }
276 return finish_unflatten_binder(NULL, *flat, in);
277 case BINDER_TYPE_HANDLE:
278 case BINDER_TYPE_WEAK_HANDLE:
279 *out = proc->getWeakProxyForHandle(flat->handle);
280 return finish_unflatten_binder(
281 static_cast<BpBinder*>(out->unsafe_get()), *flat, in);
282 }
283 }
284 return BAD_TYPE;
285}
286
287// ---------------------------------------------------------------------------
288
289Parcel::Parcel()
290{
291 initState();
292}
293
294Parcel::~Parcel()
295{
296 freeDataNoInit();
297}
298
299const uint8_t* Parcel::data() const
300{
301 return mData;
302}
303
304size_t Parcel::dataSize() const
305{
306 return (mDataSize > mDataPos ? mDataSize : mDataPos);
307}
308
309size_t Parcel::dataAvail() const
310{
311 // TODO: decide what to do about the possibility that this can
312 // report an available-data size that exceeds a Java int's max
313 // positive value, causing havoc. Fortunately this will only
314 // happen if someone constructs a Parcel containing more than two
315 // gigabytes of data, which on typical phone hardware is simply
316 // not possible.
317 return dataSize() - dataPosition();
318}
319
320size_t Parcel::dataPosition() const
321{
322 return mDataPos;
323}
324
325size_t Parcel::dataCapacity() const
326{
327 return mDataCapacity;
328}
329
330status_t Parcel::setDataSize(size_t size)
331{
332 status_t err;
333 err = continueWrite(size);
334 if (err == NO_ERROR) {
335 mDataSize = size;
Steve Block6807e592011-10-20 11:56:00 +0100336 ALOGV("setDataSize Setting data size of %p to %d\n", this, mDataSize);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700337 }
338 return err;
339}
340
341void Parcel::setDataPosition(size_t pos) const
342{
343 mDataPos = pos;
344 mNextObjectHint = 0;
345}
346
347status_t Parcel::setDataCapacity(size_t size)
348{
Dianne Hackborn97e2bcd2011-04-13 18:15:56 -0700349 if (size > mDataCapacity) return continueWrite(size);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700350 return NO_ERROR;
351}
352
353status_t Parcel::setData(const uint8_t* buffer, size_t len)
354{
355 status_t err = restartWrite(len);
356 if (err == NO_ERROR) {
357 memcpy(const_cast<uint8_t*>(data()), buffer, len);
358 mDataSize = len;
359 mFdsKnown = false;
360 }
361 return err;
362}
363
Andreas Huber51faf462011-04-13 10:21:56 -0700364status_t Parcel::appendFrom(const Parcel *parcel, size_t offset, size_t len)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700365{
366 const sp<ProcessState> proc(ProcessState::self());
367 status_t err;
Andreas Huber51faf462011-04-13 10:21:56 -0700368 const uint8_t *data = parcel->mData;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800369 const binder_size_t *objects = parcel->mObjects;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700370 size_t size = parcel->mObjectsSize;
371 int startPos = mDataPos;
372 int firstIndex = -1, lastIndex = -2;
373
374 if (len == 0) {
375 return NO_ERROR;
376 }
377
378 // range checks against the source parcel size
379 if ((offset > parcel->mDataSize)
380 || (len > parcel->mDataSize)
381 || (offset + len > parcel->mDataSize)) {
382 return BAD_VALUE;
383 }
384
385 // Count objects in range
386 for (int i = 0; i < (int) size; i++) {
387 size_t off = objects[i];
388 if ((off >= offset) && (off < offset + len)) {
389 if (firstIndex == -1) {
390 firstIndex = i;
391 }
392 lastIndex = i;
393 }
394 }
395 int numObjects = lastIndex - firstIndex + 1;
396
Dianne Hackborn97e2bcd2011-04-13 18:15:56 -0700397 if ((mDataSize+len) > mDataCapacity) {
398 // grow data
399 err = growData(len);
400 if (err != NO_ERROR) {
401 return err;
402 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700403 }
404
405 // append data
406 memcpy(mData + mDataPos, data + offset, len);
407 mDataPos += len;
408 mDataSize += len;
409
Dianne Hackborn8938ed22011-09-28 23:19:47 -0400410 err = NO_ERROR;
411
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700412 if (numObjects > 0) {
413 // grow objects
414 if (mObjectsCapacity < mObjectsSize + numObjects) {
415 int newSize = ((mObjectsSize + numObjects)*3)/2;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800416 binder_size_t *objects =
417 (binder_size_t*)realloc(mObjects, newSize*sizeof(binder_size_t));
418 if (objects == (binder_size_t*)0) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700419 return NO_MEMORY;
420 }
421 mObjects = objects;
422 mObjectsCapacity = newSize;
423 }
424
425 // append and acquire objects
426 int idx = mObjectsSize;
427 for (int i = firstIndex; i <= lastIndex; i++) {
428 size_t off = objects[i] - offset + startPos;
429 mObjects[idx++] = off;
430 mObjectsSize++;
431
Dianne Hackborn8af0f822009-05-22 13:20:23 -0700432 flat_binder_object* flat
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700433 = reinterpret_cast<flat_binder_object*>(mData + off);
434 acquire_object(proc, *flat, this);
435
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700436 if (flat->type == BINDER_TYPE_FD) {
Dianne Hackborn8af0f822009-05-22 13:20:23 -0700437 // If this is a file descriptor, we need to dup it so the
438 // new Parcel now owns its own fd, and can declare that we
439 // officially know we have fds.
440 flat->handle = dup(flat->handle);
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800441 flat->cookie = 1;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700442 mHasFds = mFdsKnown = true;
Dianne Hackborn8938ed22011-09-28 23:19:47 -0400443 if (!mAllowFds) {
444 err = FDS_NOT_ALLOWED;
445 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700446 }
447 }
448 }
449
Dianne Hackborn8938ed22011-09-28 23:19:47 -0400450 return err;
451}
452
Dianne Hackborn7746cc32011-10-03 21:09:35 -0700453bool Parcel::pushAllowFds(bool allowFds)
Dianne Hackborn8938ed22011-09-28 23:19:47 -0400454{
455 const bool origValue = mAllowFds;
Dianne Hackborn7746cc32011-10-03 21:09:35 -0700456 if (!allowFds) {
457 mAllowFds = false;
458 }
Dianne Hackborn8938ed22011-09-28 23:19:47 -0400459 return origValue;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700460}
461
Dianne Hackborn7746cc32011-10-03 21:09:35 -0700462void Parcel::restoreAllowFds(bool lastValue)
463{
464 mAllowFds = lastValue;
465}
466
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700467bool Parcel::hasFileDescriptors() const
468{
469 if (!mFdsKnown) {
470 scanForFds();
471 }
472 return mHasFds;
473}
474
Brad Fitzpatrick702ea9d2010-06-18 13:07:53 -0700475// Write RPC headers. (previously just the interface token)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700476status_t Parcel::writeInterfaceToken(const String16& interface)
477{
Brad Fitzpatricka877cd82010-07-07 16:06:39 -0700478 writeInt32(IPCThreadState::self()->getStrictModePolicy() |
479 STRICT_MODE_PENALTY_GATHER);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700480 // currently the interface identification token is just its name as a string
481 return writeString16(interface);
482}
483
Mathias Agopian83c04462009-05-22 19:00:22 -0700484bool Parcel::checkInterface(IBinder* binder) const
485{
Brad Fitzpatrick702ea9d2010-06-18 13:07:53 -0700486 return enforceInterface(binder->getInterfaceDescriptor());
Mathias Agopian83c04462009-05-22 19:00:22 -0700487}
488
Brad Fitzpatricka877cd82010-07-07 16:06:39 -0700489bool Parcel::enforceInterface(const String16& interface,
Brad Fitzpatrick70081a12010-07-27 09:49:11 -0700490 IPCThreadState* threadState) const
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700491{
Brad Fitzpatrick70081a12010-07-27 09:49:11 -0700492 int32_t strictPolicy = readInt32();
493 if (threadState == NULL) {
494 threadState = IPCThreadState::self();
Brad Fitzpatricka877cd82010-07-07 16:06:39 -0700495 }
Brad Fitzpatrick52736032010-08-30 16:01:16 -0700496 if ((threadState->getLastTransactionBinderFlags() &
497 IBinder::FLAG_ONEWAY) != 0) {
498 // For one-way calls, the callee is running entirely
499 // disconnected from the caller, so disable StrictMode entirely.
500 // Not only does disk/network usage not impact the caller, but
501 // there's no way to commuicate back any violations anyway.
502 threadState->setStrictModePolicy(0);
503 } else {
504 threadState->setStrictModePolicy(strictPolicy);
505 }
Mathias Agopian83c04462009-05-22 19:00:22 -0700506 const String16 str(readString16());
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700507 if (str == interface) {
508 return true;
509 } else {
Steve Block32397c12012-01-05 23:22:43 +0000510 ALOGW("**** enforceInterface() expected '%s' but read '%s'\n",
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700511 String8(interface).string(), String8(str).string());
512 return false;
513 }
Brad Fitzpatrick702ea9d2010-06-18 13:07:53 -0700514}
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700515
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800516const binder_size_t* Parcel::objects() const
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700517{
518 return mObjects;
519}
520
521size_t Parcel::objectsCount() const
522{
523 return mObjectsSize;
524}
525
526status_t Parcel::errorCheck() const
527{
528 return mError;
529}
530
531void Parcel::setError(status_t err)
532{
533 mError = err;
534}
535
536status_t Parcel::finishWrite(size_t len)
537{
538 //printf("Finish write of %d\n", len);
539 mDataPos += len;
Steve Block6807e592011-10-20 11:56:00 +0100540 ALOGV("finishWrite Setting data pos of %p to %d\n", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700541 if (mDataPos > mDataSize) {
542 mDataSize = mDataPos;
Steve Block6807e592011-10-20 11:56:00 +0100543 ALOGV("finishWrite Setting data size of %p to %d\n", this, mDataSize);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700544 }
545 //printf("New pos=%d, size=%d\n", mDataPos, mDataSize);
546 return NO_ERROR;
547}
548
549status_t Parcel::writeUnpadded(const void* data, size_t len)
550{
551 size_t end = mDataPos + len;
552 if (end < mDataPos) {
553 // integer overflow
554 return BAD_VALUE;
555 }
556
557 if (end <= mDataCapacity) {
558restart_write:
559 memcpy(mData+mDataPos, data, len);
560 return finishWrite(len);
561 }
562
563 status_t err = growData(len);
564 if (err == NO_ERROR) goto restart_write;
565 return err;
566}
567
568status_t Parcel::write(const void* data, size_t len)
569{
570 void* const d = writeInplace(len);
571 if (d) {
572 memcpy(d, data, len);
573 return NO_ERROR;
574 }
575 return mError;
576}
577
578void* Parcel::writeInplace(size_t len)
579{
580 const size_t padded = PAD_SIZE(len);
581
582 // sanity check for integer overflow
583 if (mDataPos+padded < mDataPos) {
584 return NULL;
585 }
586
587 if ((mDataPos+padded) <= mDataCapacity) {
588restart_write:
589 //printf("Writing %ld bytes, padded to %ld\n", len, padded);
590 uint8_t* const data = mData+mDataPos;
591
592 // Need to pad at end?
593 if (padded != len) {
594#if BYTE_ORDER == BIG_ENDIAN
595 static const uint32_t mask[4] = {
596 0x00000000, 0xffffff00, 0xffff0000, 0xff000000
597 };
598#endif
599#if BYTE_ORDER == LITTLE_ENDIAN
600 static const uint32_t mask[4] = {
601 0x00000000, 0x00ffffff, 0x0000ffff, 0x000000ff
602 };
603#endif
604 //printf("Applying pad mask: %p to %p\n", (void*)mask[padded-len],
605 // *reinterpret_cast<void**>(data+padded-4));
606 *reinterpret_cast<uint32_t*>(data+padded-4) &= mask[padded-len];
607 }
608
609 finishWrite(padded);
610 return data;
611 }
612
613 status_t err = growData(padded);
614 if (err == NO_ERROR) goto restart_write;
615 return NULL;
616}
617
618status_t Parcel::writeInt32(int32_t val)
619{
Andreas Huber84a6d042009-08-17 13:33:27 -0700620 return writeAligned(val);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700621}
Marco Nelissen708cc792013-10-16 10:57:51 -0700622status_t Parcel::writeInt32Array(size_t len, const int32_t *val) {
623 if (!val) {
624 return writeAligned(-1);
625 }
626 status_t ret = writeAligned(len);
627 if (ret == NO_ERROR) {
628 ret = write(val, len * sizeof(*val));
629 }
630 return ret;
631}
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700632
633status_t Parcel::writeInt64(int64_t val)
634{
Andreas Huber84a6d042009-08-17 13:33:27 -0700635 return writeAligned(val);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700636}
637
Serban Constantinescuf683e012013-11-05 16:53:55 +0000638status_t Parcel::writePointer(uintptr_t val)
639{
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800640 return writeAligned<binder_uintptr_t>(val);
Serban Constantinescuf683e012013-11-05 16:53:55 +0000641}
642
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700643status_t Parcel::writeFloat(float val)
644{
Andreas Huber84a6d042009-08-17 13:33:27 -0700645 return writeAligned(val);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700646}
647
Douglas Leungcc1a4bb2013-01-11 15:00:55 -0800648#if defined(__mips__) && defined(__mips_hard_float)
649
650status_t Parcel::writeDouble(double val)
651{
652 union {
653 double d;
654 unsigned long long ll;
655 } u;
656 u.d = val;
657 return writeAligned(u.ll);
658}
659
660#else
661
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700662status_t Parcel::writeDouble(double val)
663{
Andreas Huber84a6d042009-08-17 13:33:27 -0700664 return writeAligned(val);
665}
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700666
Douglas Leungcc1a4bb2013-01-11 15:00:55 -0800667#endif
668
Andreas Huber84a6d042009-08-17 13:33:27 -0700669status_t Parcel::writeIntPtr(intptr_t val)
670{
671 return writeAligned(val);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700672}
673
674status_t Parcel::writeCString(const char* str)
675{
676 return write(str, strlen(str)+1);
677}
678
679status_t Parcel::writeString8(const String8& str)
680{
681 status_t err = writeInt32(str.bytes());
Pravat Dalbeherad1dff8d2010-12-15 08:40:00 +0100682 // only write string if its length is more than zero characters,
683 // as readString8 will only read if the length field is non-zero.
684 // this is slightly different from how writeString16 works.
685 if (str.bytes() > 0 && err == NO_ERROR) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700686 err = write(str.string(), str.bytes()+1);
687 }
688 return err;
689}
690
691status_t Parcel::writeString16(const String16& str)
692{
693 return writeString16(str.string(), str.size());
694}
695
696status_t Parcel::writeString16(const char16_t* str, size_t len)
697{
698 if (str == NULL) return writeInt32(-1);
699
700 status_t err = writeInt32(len);
701 if (err == NO_ERROR) {
702 len *= sizeof(char16_t);
703 uint8_t* data = (uint8_t*)writeInplace(len+sizeof(char16_t));
704 if (data) {
705 memcpy(data, str, len);
706 *reinterpret_cast<char16_t*>(data+len) = 0;
707 return NO_ERROR;
708 }
709 err = mError;
710 }
711 return err;
712}
713
714status_t Parcel::writeStrongBinder(const sp<IBinder>& val)
715{
716 return flatten_binder(ProcessState::self(), val, this);
717}
718
719status_t Parcel::writeWeakBinder(const wp<IBinder>& val)
720{
721 return flatten_binder(ProcessState::self(), val, this);
722}
723
Mathias Agopiana47f02a2009-05-21 16:29:38 -0700724status_t Parcel::writeNativeHandle(const native_handle* handle)
The Android Open Source Project5f78a482009-01-20 14:03:58 -0800725{
Mathias Agopian1d0a95b2009-07-31 16:12:13 -0700726 if (!handle || handle->version != sizeof(native_handle))
The Android Open Source Project5f78a482009-01-20 14:03:58 -0800727 return BAD_TYPE;
728
729 status_t err;
Mathias Agopiana47f02a2009-05-21 16:29:38 -0700730 err = writeInt32(handle->numFds);
The Android Open Source Project5f78a482009-01-20 14:03:58 -0800731 if (err != NO_ERROR) return err;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800732
Mathias Agopiana47f02a2009-05-21 16:29:38 -0700733 err = writeInt32(handle->numInts);
The Android Open Source Project5f78a482009-01-20 14:03:58 -0800734 if (err != NO_ERROR) return err;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800735
Mathias Agopiana47f02a2009-05-21 16:29:38 -0700736 for (int i=0 ; err==NO_ERROR && i<handle->numFds ; i++)
737 err = writeDupFileDescriptor(handle->data[i]);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800738
739 if (err != NO_ERROR) {
Steve Block9d453682011-12-20 16:23:08 +0000740 ALOGD("write native handle, write dup fd failed");
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800741 return err;
742 }
Mathias Agopiana47f02a2009-05-21 16:29:38 -0700743 err = write(handle->data + handle->numFds, sizeof(int)*handle->numInts);
The Android Open Source Project5f78a482009-01-20 14:03:58 -0800744 return err;
745}
746
Jeff Brown93ff1f92011-11-04 19:01:44 -0700747status_t Parcel::writeFileDescriptor(int fd, bool takeOwnership)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700748{
749 flat_binder_object obj;
750 obj.type = BINDER_TYPE_FD;
751 obj.flags = 0x7f | FLAT_BINDER_FLAG_ACCEPTS_FDS;
752 obj.handle = fd;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800753 obj.cookie = takeOwnership ? 1 : 0;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700754 return writeObject(obj, true);
755}
756
757status_t Parcel::writeDupFileDescriptor(int fd)
758{
Jeff Brownd341c712011-11-04 20:19:33 -0700759 int dupFd = dup(fd);
760 if (dupFd < 0) {
761 return -errno;
762 }
763 status_t err = writeFileDescriptor(dupFd, true /*takeOwnership*/);
764 if (err) {
765 close(dupFd);
766 }
767 return err;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700768}
769
Jeff Brown5707dbf2011-09-23 21:17:56 -0700770status_t Parcel::writeBlob(size_t len, WritableBlob* outBlob)
771{
772 status_t status;
773
774 if (!mAllowFds || len <= IN_PLACE_BLOB_LIMIT) {
Steve Block6807e592011-10-20 11:56:00 +0100775 ALOGV("writeBlob: write in place");
Jeff Brown5707dbf2011-09-23 21:17:56 -0700776 status = writeInt32(0);
777 if (status) return status;
778
779 void* ptr = writeInplace(len);
780 if (!ptr) return NO_MEMORY;
781
782 outBlob->init(false /*mapped*/, ptr, len);
783 return NO_ERROR;
784 }
785
Steve Block6807e592011-10-20 11:56:00 +0100786 ALOGV("writeBlob: write to ashmem");
Jeff Brown5707dbf2011-09-23 21:17:56 -0700787 int fd = ashmem_create_region("Parcel Blob", len);
788 if (fd < 0) return NO_MEMORY;
789
790 int result = ashmem_set_prot_region(fd, PROT_READ | PROT_WRITE);
791 if (result < 0) {
Jeff Brownec4e0062011-10-10 14:50:10 -0700792 status = result;
Jeff Brown5707dbf2011-09-23 21:17:56 -0700793 } else {
794 void* ptr = ::mmap(NULL, len, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
795 if (ptr == MAP_FAILED) {
796 status = -errno;
797 } else {
798 result = ashmem_set_prot_region(fd, PROT_READ);
799 if (result < 0) {
Jeff Brownec4e0062011-10-10 14:50:10 -0700800 status = result;
Jeff Brown5707dbf2011-09-23 21:17:56 -0700801 } else {
802 status = writeInt32(1);
803 if (!status) {
Jeff Brown93ff1f92011-11-04 19:01:44 -0700804 status = writeFileDescriptor(fd, true /*takeOwnership*/);
Jeff Brown5707dbf2011-09-23 21:17:56 -0700805 if (!status) {
806 outBlob->init(true /*mapped*/, ptr, len);
807 return NO_ERROR;
808 }
809 }
810 }
811 }
812 ::munmap(ptr, len);
813 }
814 ::close(fd);
815 return status;
816}
817
Mathias Agopiane1424282013-07-29 21:24:40 -0700818status_t Parcel::write(const FlattenableHelperInterface& val)
Mathias Agopian98e71dd2010-02-11 17:30:52 -0800819{
820 status_t err;
821
822 // size if needed
Mathias Agopiane1424282013-07-29 21:24:40 -0700823 const size_t len = val.getFlattenedSize();
824 const size_t fd_count = val.getFdCount();
Mathias Agopian98e71dd2010-02-11 17:30:52 -0800825
826 err = this->writeInt32(len);
827 if (err) return err;
828
829 err = this->writeInt32(fd_count);
830 if (err) return err;
831
832 // payload
Mathias Agopiane1424282013-07-29 21:24:40 -0700833 void* const buf = this->writeInplace(PAD_SIZE(len));
Mathias Agopian98e71dd2010-02-11 17:30:52 -0800834 if (buf == NULL)
835 return BAD_VALUE;
836
837 int* fds = NULL;
838 if (fd_count) {
839 fds = new int[fd_count];
840 }
841
842 err = val.flatten(buf, len, fds, fd_count);
843 for (size_t i=0 ; i<fd_count && err==NO_ERROR ; i++) {
844 err = this->writeDupFileDescriptor( fds[i] );
845 }
846
847 if (fd_count) {
848 delete [] fds;
849 }
850
851 return err;
852}
853
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700854status_t Parcel::writeObject(const flat_binder_object& val, bool nullMetaData)
855{
856 const bool enoughData = (mDataPos+sizeof(val)) <= mDataCapacity;
857 const bool enoughObjects = mObjectsSize < mObjectsCapacity;
858 if (enoughData && enoughObjects) {
859restart_write:
860 *reinterpret_cast<flat_binder_object*>(mData+mDataPos) = val;
861
862 // Need to write meta-data?
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800863 if (nullMetaData || val.binder != 0) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700864 mObjects[mObjectsSize] = mDataPos;
865 acquire_object(ProcessState::self(), val, this);
866 mObjectsSize++;
867 }
868
869 // remember if it's a file descriptor
870 if (val.type == BINDER_TYPE_FD) {
Dianne Hackborn8938ed22011-09-28 23:19:47 -0400871 if (!mAllowFds) {
872 return FDS_NOT_ALLOWED;
873 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700874 mHasFds = mFdsKnown = true;
875 }
876
877 return finishWrite(sizeof(flat_binder_object));
878 }
879
880 if (!enoughData) {
881 const status_t err = growData(sizeof(val));
882 if (err != NO_ERROR) return err;
883 }
884 if (!enoughObjects) {
885 size_t newSize = ((mObjectsSize+2)*3)/2;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800886 binder_size_t* objects = (binder_size_t*)realloc(mObjects, newSize*sizeof(binder_size_t));
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700887 if (objects == NULL) return NO_MEMORY;
888 mObjects = objects;
889 mObjectsCapacity = newSize;
890 }
891
892 goto restart_write;
893}
894
Brad Fitzpatrick837a0d02010-07-13 15:33:35 -0700895status_t Parcel::writeNoException()
896{
897 return writeInt32(0);
898}
899
Colin Cross6f4f3ab2014-02-05 17:42:44 -0800900void Parcel::remove(size_t /*start*/, size_t /*amt*/)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700901{
902 LOG_ALWAYS_FATAL("Parcel::remove() not yet implemented!");
903}
904
905status_t Parcel::read(void* outData, size_t len) const
906{
907 if ((mDataPos+PAD_SIZE(len)) >= mDataPos && (mDataPos+PAD_SIZE(len)) <= mDataSize) {
908 memcpy(outData, mData+mDataPos, len);
909 mDataPos += PAD_SIZE(len);
Steve Block6807e592011-10-20 11:56:00 +0100910 ALOGV("read Setting data pos of %p to %d\n", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700911 return NO_ERROR;
912 }
913 return NOT_ENOUGH_DATA;
914}
915
916const void* Parcel::readInplace(size_t len) const
917{
918 if ((mDataPos+PAD_SIZE(len)) >= mDataPos && (mDataPos+PAD_SIZE(len)) <= mDataSize) {
919 const void* data = mData+mDataPos;
920 mDataPos += PAD_SIZE(len);
Steve Block6807e592011-10-20 11:56:00 +0100921 ALOGV("readInplace Setting data pos of %p to %d\n", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700922 return data;
923 }
924 return NULL;
925}
926
Andreas Huber84a6d042009-08-17 13:33:27 -0700927template<class T>
928status_t Parcel::readAligned(T *pArg) const {
929 COMPILE_TIME_ASSERT_FUNCTION_SCOPE(PAD_SIZE(sizeof(T)) == sizeof(T));
930
931 if ((mDataPos+sizeof(T)) <= mDataSize) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700932 const void* data = mData+mDataPos;
Andreas Huber84a6d042009-08-17 13:33:27 -0700933 mDataPos += sizeof(T);
934 *pArg = *reinterpret_cast<const T*>(data);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700935 return NO_ERROR;
936 } else {
937 return NOT_ENOUGH_DATA;
938 }
939}
940
Andreas Huber84a6d042009-08-17 13:33:27 -0700941template<class T>
942T Parcel::readAligned() const {
943 T result;
944 if (readAligned(&result) != NO_ERROR) {
945 result = 0;
946 }
947
948 return result;
949}
950
951template<class T>
952status_t Parcel::writeAligned(T val) {
953 COMPILE_TIME_ASSERT_FUNCTION_SCOPE(PAD_SIZE(sizeof(T)) == sizeof(T));
954
955 if ((mDataPos+sizeof(val)) <= mDataCapacity) {
956restart_write:
957 *reinterpret_cast<T*>(mData+mDataPos) = val;
958 return finishWrite(sizeof(val));
959 }
960
961 status_t err = growData(sizeof(val));
962 if (err == NO_ERROR) goto restart_write;
963 return err;
964}
965
966status_t Parcel::readInt32(int32_t *pArg) const
967{
968 return readAligned(pArg);
969}
970
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700971int32_t Parcel::readInt32() const
972{
Andreas Huber84a6d042009-08-17 13:33:27 -0700973 return readAligned<int32_t>();
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700974}
975
976
977status_t Parcel::readInt64(int64_t *pArg) const
978{
Andreas Huber84a6d042009-08-17 13:33:27 -0700979 return readAligned(pArg);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700980}
981
982
983int64_t Parcel::readInt64() const
984{
Andreas Huber84a6d042009-08-17 13:33:27 -0700985 return readAligned<int64_t>();
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700986}
987
Serban Constantinescuf683e012013-11-05 16:53:55 +0000988status_t Parcel::readPointer(uintptr_t *pArg) const
989{
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800990 status_t ret;
991 binder_uintptr_t ptr;
992 ret = readAligned(&ptr);
993 if (!ret)
994 *pArg = ptr;
995 return ret;
Serban Constantinescuf683e012013-11-05 16:53:55 +0000996}
997
998uintptr_t Parcel::readPointer() const
999{
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001000 return readAligned<binder_uintptr_t>();
Serban Constantinescuf683e012013-11-05 16:53:55 +00001001}
1002
1003
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001004status_t Parcel::readFloat(float *pArg) const
1005{
Andreas Huber84a6d042009-08-17 13:33:27 -07001006 return readAligned(pArg);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001007}
1008
1009
1010float Parcel::readFloat() const
1011{
Andreas Huber84a6d042009-08-17 13:33:27 -07001012 return readAligned<float>();
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001013}
1014
Douglas Leungcc1a4bb2013-01-11 15:00:55 -08001015#if defined(__mips__) && defined(__mips_hard_float)
1016
1017status_t Parcel::readDouble(double *pArg) const
1018{
1019 union {
1020 double d;
1021 unsigned long long ll;
1022 } u;
1023 status_t status;
1024 status = readAligned(&u.ll);
1025 *pArg = u.d;
1026 return status;
1027}
1028
1029double Parcel::readDouble() const
1030{
1031 union {
1032 double d;
1033 unsigned long long ll;
1034 } u;
1035 u.ll = readAligned<unsigned long long>();
1036 return u.d;
1037}
1038
1039#else
1040
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001041status_t Parcel::readDouble(double *pArg) const
1042{
Andreas Huber84a6d042009-08-17 13:33:27 -07001043 return readAligned(pArg);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001044}
1045
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001046double Parcel::readDouble() const
1047{
Andreas Huber84a6d042009-08-17 13:33:27 -07001048 return readAligned<double>();
1049}
1050
Douglas Leungcc1a4bb2013-01-11 15:00:55 -08001051#endif
1052
Andreas Huber84a6d042009-08-17 13:33:27 -07001053status_t Parcel::readIntPtr(intptr_t *pArg) const
1054{
1055 return readAligned(pArg);
1056}
1057
1058
1059intptr_t Parcel::readIntPtr() const
1060{
1061 return readAligned<intptr_t>();
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001062}
1063
1064
1065const char* Parcel::readCString() const
1066{
1067 const size_t avail = mDataSize-mDataPos;
1068 if (avail > 0) {
1069 const char* str = reinterpret_cast<const char*>(mData+mDataPos);
1070 // is the string's trailing NUL within the parcel's valid bounds?
1071 const char* eos = reinterpret_cast<const char*>(memchr(str, 0, avail));
1072 if (eos) {
1073 const size_t len = eos - str;
1074 mDataPos += PAD_SIZE(len+1);
Steve Block6807e592011-10-20 11:56:00 +01001075 ALOGV("readCString Setting data pos of %p to %d\n", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001076 return str;
1077 }
1078 }
1079 return NULL;
1080}
1081
1082String8 Parcel::readString8() const
1083{
1084 int32_t size = readInt32();
1085 // watch for potential int overflow adding 1 for trailing NUL
1086 if (size > 0 && size < INT32_MAX) {
1087 const char* str = (const char*)readInplace(size+1);
1088 if (str) return String8(str, size);
1089 }
1090 return String8();
1091}
1092
1093String16 Parcel::readString16() const
1094{
1095 size_t len;
1096 const char16_t* str = readString16Inplace(&len);
1097 if (str) return String16(str, len);
Steve Blocke6f43dd2012-01-06 19:20:56 +00001098 ALOGE("Reading a NULL string not supported here.");
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001099 return String16();
1100}
1101
1102const char16_t* Parcel::readString16Inplace(size_t* outLen) const
1103{
1104 int32_t size = readInt32();
1105 // watch for potential int overflow from size+1
1106 if (size >= 0 && size < INT32_MAX) {
1107 *outLen = size;
1108 const char16_t* str = (const char16_t*)readInplace((size+1)*sizeof(char16_t));
1109 if (str != NULL) {
1110 return str;
1111 }
1112 }
1113 *outLen = 0;
1114 return NULL;
1115}
1116
1117sp<IBinder> Parcel::readStrongBinder() const
1118{
1119 sp<IBinder> val;
1120 unflatten_binder(ProcessState::self(), *this, &val);
1121 return val;
1122}
1123
1124wp<IBinder> Parcel::readWeakBinder() const
1125{
1126 wp<IBinder> val;
1127 unflatten_binder(ProcessState::self(), *this, &val);
1128 return val;
1129}
1130
Brad Fitzpatrick837a0d02010-07-13 15:33:35 -07001131int32_t Parcel::readExceptionCode() const
1132{
1133 int32_t exception_code = readAligned<int32_t>();
Brad Fitzpatrickd36f4a52010-07-12 11:05:38 -07001134 if (exception_code == EX_HAS_REPLY_HEADER) {
Magnus Strandberg1ba24572011-05-03 15:44:00 +02001135 int32_t header_start = dataPosition();
Brad Fitzpatrickd36f4a52010-07-12 11:05:38 -07001136 int32_t header_size = readAligned<int32_t>();
1137 // Skip over fat responses headers. Not used (or propagated) in
1138 // native code
Magnus Strandberg1ba24572011-05-03 15:44:00 +02001139 setDataPosition(header_start + header_size);
Brad Fitzpatrickd36f4a52010-07-12 11:05:38 -07001140 // And fat response headers are currently only used when there are no
1141 // exceptions, so return no error:
1142 return 0;
1143 }
Brad Fitzpatrick837a0d02010-07-13 15:33:35 -07001144 return exception_code;
1145}
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001146
Mathias Agopiana47f02a2009-05-21 16:29:38 -07001147native_handle* Parcel::readNativeHandle() const
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001148{
1149 int numFds, numInts;
1150 status_t err;
1151 err = readInt32(&numFds);
1152 if (err != NO_ERROR) return 0;
1153 err = readInt32(&numInts);
1154 if (err != NO_ERROR) return 0;
1155
Mathias Agopiana47f02a2009-05-21 16:29:38 -07001156 native_handle* h = native_handle_create(numFds, numInts);
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001157 for (int i=0 ; err==NO_ERROR && i<numFds ; i++) {
Rebecca Schultz Zavin360211f2009-02-13 16:34:38 -08001158 h->data[i] = dup(readFileDescriptor());
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001159 if (h->data[i] < 0) err = BAD_VALUE;
1160 }
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001161 err = read(h->data + numFds, sizeof(int)*numInts);
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001162 if (err != NO_ERROR) {
Mathias Agopiana47f02a2009-05-21 16:29:38 -07001163 native_handle_close(h);
1164 native_handle_delete(h);
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001165 h = 0;
1166 }
1167 return h;
1168}
1169
1170
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001171int Parcel::readFileDescriptor() const
1172{
1173 const flat_binder_object* flat = readObject(true);
1174 if (flat) {
1175 switch (flat->type) {
1176 case BINDER_TYPE_FD:
Steve Blocka19954a2012-01-04 20:05:49 +00001177 //ALOGI("Returning file descriptor %ld from parcel %p\n", flat->handle, this);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001178 return flat->handle;
1179 }
1180 }
1181 return BAD_TYPE;
1182}
1183
Jeff Brown5707dbf2011-09-23 21:17:56 -07001184status_t Parcel::readBlob(size_t len, ReadableBlob* outBlob) const
1185{
1186 int32_t useAshmem;
1187 status_t status = readInt32(&useAshmem);
1188 if (status) return status;
1189
1190 if (!useAshmem) {
Steve Block6807e592011-10-20 11:56:00 +01001191 ALOGV("readBlob: read in place");
Jeff Brown5707dbf2011-09-23 21:17:56 -07001192 const void* ptr = readInplace(len);
1193 if (!ptr) return BAD_VALUE;
1194
1195 outBlob->init(false /*mapped*/, const_cast<void*>(ptr), len);
1196 return NO_ERROR;
1197 }
1198
Steve Block6807e592011-10-20 11:56:00 +01001199 ALOGV("readBlob: read from ashmem");
Jeff Brown5707dbf2011-09-23 21:17:56 -07001200 int fd = readFileDescriptor();
1201 if (fd == int(BAD_TYPE)) return BAD_VALUE;
1202
1203 void* ptr = ::mmap(NULL, len, PROT_READ, MAP_SHARED, fd, 0);
1204 if (!ptr) return NO_MEMORY;
1205
1206 outBlob->init(true /*mapped*/, ptr, len);
1207 return NO_ERROR;
1208}
1209
Mathias Agopiane1424282013-07-29 21:24:40 -07001210status_t Parcel::read(FlattenableHelperInterface& val) const
Mathias Agopian98e71dd2010-02-11 17:30:52 -08001211{
1212 // size
1213 const size_t len = this->readInt32();
1214 const size_t fd_count = this->readInt32();
1215
1216 // payload
Mathias Agopiane1424282013-07-29 21:24:40 -07001217 void const* const buf = this->readInplace(PAD_SIZE(len));
Mathias Agopian98e71dd2010-02-11 17:30:52 -08001218 if (buf == NULL)
1219 return BAD_VALUE;
1220
1221 int* fds = NULL;
1222 if (fd_count) {
1223 fds = new int[fd_count];
1224 }
1225
1226 status_t err = NO_ERROR;
1227 for (size_t i=0 ; i<fd_count && err==NO_ERROR ; i++) {
1228 fds[i] = dup(this->readFileDescriptor());
1229 if (fds[i] < 0) err = BAD_VALUE;
1230 }
1231
1232 if (err == NO_ERROR) {
1233 err = val.unflatten(buf, len, fds, fd_count);
1234 }
1235
1236 if (fd_count) {
1237 delete [] fds;
1238 }
1239
1240 return err;
1241}
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001242const flat_binder_object* Parcel::readObject(bool nullMetaData) const
1243{
1244 const size_t DPOS = mDataPos;
1245 if ((DPOS+sizeof(flat_binder_object)) <= mDataSize) {
1246 const flat_binder_object* obj
1247 = reinterpret_cast<const flat_binder_object*>(mData+DPOS);
1248 mDataPos = DPOS + sizeof(flat_binder_object);
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001249 if (!nullMetaData && (obj->cookie == 0 && obj->binder == 0)) {
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001250 // When transferring a NULL object, we don't write it into
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001251 // the object list, so we don't want to check for it when
1252 // reading.
Steve Block6807e592011-10-20 11:56:00 +01001253 ALOGV("readObject Setting data pos of %p to %d\n", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001254 return obj;
1255 }
1256
1257 // Ensure that this object is valid...
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001258 binder_size_t* const OBJS = mObjects;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001259 const size_t N = mObjectsSize;
1260 size_t opos = mNextObjectHint;
1261
1262 if (N > 0) {
Steve Block6807e592011-10-20 11:56:00 +01001263 ALOGV("Parcel %p looking for obj at %d, hint=%d\n",
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001264 this, DPOS, opos);
1265
1266 // Start at the current hint position, looking for an object at
1267 // the current data position.
1268 if (opos < N) {
1269 while (opos < (N-1) && OBJS[opos] < DPOS) {
1270 opos++;
1271 }
1272 } else {
1273 opos = N-1;
1274 }
1275 if (OBJS[opos] == DPOS) {
1276 // Found it!
Steve Block6807e592011-10-20 11:56:00 +01001277 ALOGV("Parcel found obj %d at index %d with forward search",
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001278 this, DPOS, opos);
1279 mNextObjectHint = opos+1;
Steve Block6807e592011-10-20 11:56:00 +01001280 ALOGV("readObject Setting data pos of %p to %d\n", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001281 return obj;
1282 }
1283
1284 // Look backwards for it...
1285 while (opos > 0 && OBJS[opos] > DPOS) {
1286 opos--;
1287 }
1288 if (OBJS[opos] == DPOS) {
1289 // Found it!
Steve Block6807e592011-10-20 11:56:00 +01001290 ALOGV("Parcel found obj %d at index %d with backward search",
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001291 this, DPOS, opos);
1292 mNextObjectHint = opos+1;
Steve Block6807e592011-10-20 11:56:00 +01001293 ALOGV("readObject Setting data pos of %p to %d\n", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001294 return obj;
1295 }
1296 }
Colin Cross6f4f3ab2014-02-05 17:42:44 -08001297 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 -07001298 this, DPOS);
1299 }
1300 return NULL;
1301}
1302
1303void Parcel::closeFileDescriptors()
1304{
1305 size_t i = mObjectsSize;
1306 if (i > 0) {
Steve Blocka19954a2012-01-04 20:05:49 +00001307 //ALOGI("Closing file descriptors for %d objects...", mObjectsSize);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001308 }
1309 while (i > 0) {
1310 i--;
1311 const flat_binder_object* flat
1312 = reinterpret_cast<flat_binder_object*>(mData+mObjects[i]);
1313 if (flat->type == BINDER_TYPE_FD) {
Steve Blocka19954a2012-01-04 20:05:49 +00001314 //ALOGI("Closing fd: %ld\n", flat->handle);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001315 close(flat->handle);
1316 }
1317 }
1318}
1319
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001320uintptr_t Parcel::ipcData() const
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001321{
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001322 return reinterpret_cast<uintptr_t>(mData);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001323}
1324
1325size_t Parcel::ipcDataSize() const
1326{
1327 return (mDataSize > mDataPos ? mDataSize : mDataPos);
1328}
1329
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001330uintptr_t Parcel::ipcObjects() const
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001331{
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001332 return reinterpret_cast<uintptr_t>(mObjects);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001333}
1334
1335size_t Parcel::ipcObjectsCount() const
1336{
1337 return mObjectsSize;
1338}
1339
1340void Parcel::ipcSetDataReference(const uint8_t* data, size_t dataSize,
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001341 const binder_size_t* objects, size_t objectsCount, release_func relFunc, void* relCookie)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001342{
Arve Hjønnevåg67903292014-02-19 15:35:52 -08001343 size_t minOffset = 0;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001344 freeDataNoInit();
1345 mError = NO_ERROR;
1346 mData = const_cast<uint8_t*>(data);
1347 mDataSize = mDataCapacity = dataSize;
Steve Blocka19954a2012-01-04 20:05:49 +00001348 //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 -07001349 mDataPos = 0;
Steve Block6807e592011-10-20 11:56:00 +01001350 ALOGV("setDataReference Setting data pos of %p to %d\n", this, mDataPos);
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001351 mObjects = const_cast<binder_size_t*>(objects);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001352 mObjectsSize = mObjectsCapacity = objectsCount;
1353 mNextObjectHint = 0;
1354 mOwner = relFunc;
1355 mOwnerCookie = relCookie;
Arve Hjønnevågf50b9ea2014-02-13 19:22:08 -08001356 for (size_t i = 0; i < mObjectsSize; i++) {
Arve Hjønnevåg67903292014-02-19 15:35:52 -08001357 size_t offset = mObjects[i];
Arve Hjønnevågf50b9ea2014-02-13 19:22:08 -08001358 if (offset < minOffset) {
Arve Hjønnevåg67903292014-02-19 15:35:52 -08001359 ALOGE("%s: bad object offset %zu < %zu\n",
1360 __func__, offset, minOffset);
Arve Hjønnevågf50b9ea2014-02-13 19:22:08 -08001361 mObjectsSize = 0;
1362 break;
1363 }
1364 minOffset = offset + sizeof(flat_binder_object);
1365 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001366 scanForFds();
1367}
1368
Colin Cross6f4f3ab2014-02-05 17:42:44 -08001369void Parcel::print(TextOutput& to, uint32_t /*flags*/) const
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001370{
1371 to << "Parcel(";
1372
1373 if (errorCheck() != NO_ERROR) {
1374 const status_t err = errorCheck();
Colin Cross6f4f3ab2014-02-05 17:42:44 -08001375 to << "Error: " << (void*)(intptr_t)err << " \"" << strerror(-err) << "\"";
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001376 } else if (dataSize() > 0) {
1377 const uint8_t* DATA = data();
1378 to << indent << HexDump(DATA, dataSize()) << dedent;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001379 const binder_size_t* OBJS = objects();
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001380 const size_t N = objectsCount();
1381 for (size_t i=0; i<N; i++) {
1382 const flat_binder_object* flat
1383 = reinterpret_cast<const flat_binder_object*>(DATA+OBJS[i]);
1384 to << endl << "Object #" << i << " @ " << (void*)OBJS[i] << ": "
1385 << TypeCode(flat->type & 0x7f7f7f00)
1386 << " = " << flat->binder;
1387 }
1388 } else {
1389 to << "NULL";
1390 }
1391
1392 to << ")";
1393}
1394
1395void Parcel::releaseObjects()
1396{
1397 const sp<ProcessState> proc(ProcessState::self());
1398 size_t i = mObjectsSize;
1399 uint8_t* const data = mData;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001400 binder_size_t* const objects = mObjects;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001401 while (i > 0) {
1402 i--;
1403 const flat_binder_object* flat
1404 = reinterpret_cast<flat_binder_object*>(data+objects[i]);
1405 release_object(proc, *flat, this);
1406 }
1407}
1408
1409void Parcel::acquireObjects()
1410{
1411 const sp<ProcessState> proc(ProcessState::self());
1412 size_t i = mObjectsSize;
1413 uint8_t* const data = mData;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001414 binder_size_t* const objects = mObjects;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001415 while (i > 0) {
1416 i--;
1417 const flat_binder_object* flat
1418 = reinterpret_cast<flat_binder_object*>(data+objects[i]);
1419 acquire_object(proc, *flat, this);
1420 }
1421}
1422
1423void Parcel::freeData()
1424{
1425 freeDataNoInit();
1426 initState();
1427}
1428
1429void Parcel::freeDataNoInit()
1430{
1431 if (mOwner) {
Steve Blocka19954a2012-01-04 20:05:49 +00001432 //ALOGI("Freeing data ref of %p (pid=%d)\n", this, getpid());
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001433 mOwner(this, mData, mDataSize, mObjects, mObjectsSize, mOwnerCookie);
1434 } else {
1435 releaseObjects();
1436 if (mData) free(mData);
1437 if (mObjects) free(mObjects);
1438 }
1439}
1440
1441status_t Parcel::growData(size_t len)
1442{
1443 size_t newSize = ((mDataSize+len)*3)/2;
1444 return (newSize <= mDataSize)
1445 ? (status_t) NO_MEMORY
1446 : continueWrite(newSize);
1447}
1448
1449status_t Parcel::restartWrite(size_t desired)
1450{
1451 if (mOwner) {
1452 freeData();
1453 return continueWrite(desired);
1454 }
1455
1456 uint8_t* data = (uint8_t*)realloc(mData, desired);
1457 if (!data && desired > mDataCapacity) {
1458 mError = NO_MEMORY;
1459 return NO_MEMORY;
1460 }
1461
1462 releaseObjects();
1463
1464 if (data) {
1465 mData = data;
1466 mDataCapacity = desired;
1467 }
1468
1469 mDataSize = mDataPos = 0;
Steve Block6807e592011-10-20 11:56:00 +01001470 ALOGV("restartWrite Setting data size of %p to %d\n", this, mDataSize);
1471 ALOGV("restartWrite Setting data pos of %p to %d\n", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001472
1473 free(mObjects);
1474 mObjects = NULL;
1475 mObjectsSize = mObjectsCapacity = 0;
1476 mNextObjectHint = 0;
1477 mHasFds = false;
1478 mFdsKnown = true;
Dianne Hackborn8938ed22011-09-28 23:19:47 -04001479 mAllowFds = true;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001480
1481 return NO_ERROR;
1482}
1483
1484status_t Parcel::continueWrite(size_t desired)
1485{
1486 // If shrinking, first adjust for any objects that appear
1487 // after the new data size.
1488 size_t objectsSize = mObjectsSize;
1489 if (desired < mDataSize) {
1490 if (desired == 0) {
1491 objectsSize = 0;
1492 } else {
1493 while (objectsSize > 0) {
1494 if (mObjects[objectsSize-1] < desired)
1495 break;
1496 objectsSize--;
1497 }
1498 }
1499 }
1500
1501 if (mOwner) {
1502 // If the size is going to zero, just release the owner's data.
1503 if (desired == 0) {
1504 freeData();
1505 return NO_ERROR;
1506 }
1507
1508 // If there is a different owner, we need to take
1509 // posession.
1510 uint8_t* data = (uint8_t*)malloc(desired);
1511 if (!data) {
1512 mError = NO_MEMORY;
1513 return NO_MEMORY;
1514 }
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001515 binder_size_t* objects = NULL;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001516
1517 if (objectsSize) {
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001518 objects = (binder_size_t*)malloc(objectsSize*sizeof(binder_size_t));
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001519 if (!objects) {
Hyejin Kim3f727c02013-03-09 11:28:54 +09001520 free(data);
1521
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001522 mError = NO_MEMORY;
1523 return NO_MEMORY;
1524 }
1525
1526 // Little hack to only acquire references on objects
1527 // we will be keeping.
1528 size_t oldObjectsSize = mObjectsSize;
1529 mObjectsSize = objectsSize;
1530 acquireObjects();
1531 mObjectsSize = oldObjectsSize;
1532 }
1533
1534 if (mData) {
1535 memcpy(data, mData, mDataSize < desired ? mDataSize : desired);
1536 }
1537 if (objects && mObjects) {
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001538 memcpy(objects, mObjects, objectsSize*sizeof(binder_size_t));
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001539 }
Steve Blocka19954a2012-01-04 20:05:49 +00001540 //ALOGI("Freeing data ref of %p (pid=%d)\n", this, getpid());
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001541 mOwner(this, mData, mDataSize, mObjects, mObjectsSize, mOwnerCookie);
1542 mOwner = NULL;
1543
1544 mData = data;
1545 mObjects = objects;
1546 mDataSize = (mDataSize < desired) ? mDataSize : desired;
Steve Block6807e592011-10-20 11:56:00 +01001547 ALOGV("continueWrite Setting data size of %p to %d\n", this, mDataSize);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001548 mDataCapacity = desired;
1549 mObjectsSize = mObjectsCapacity = objectsSize;
1550 mNextObjectHint = 0;
1551
1552 } else if (mData) {
1553 if (objectsSize < mObjectsSize) {
1554 // Need to release refs on any objects we are dropping.
1555 const sp<ProcessState> proc(ProcessState::self());
1556 for (size_t i=objectsSize; i<mObjectsSize; i++) {
1557 const flat_binder_object* flat
1558 = reinterpret_cast<flat_binder_object*>(mData+mObjects[i]);
1559 if (flat->type == BINDER_TYPE_FD) {
1560 // will need to rescan because we may have lopped off the only FDs
1561 mFdsKnown = false;
1562 }
1563 release_object(proc, *flat, this);
1564 }
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001565 binder_size_t* objects =
1566 (binder_size_t*)realloc(mObjects, objectsSize*sizeof(binder_size_t));
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001567 if (objects) {
1568 mObjects = objects;
1569 }
1570 mObjectsSize = objectsSize;
1571 mNextObjectHint = 0;
1572 }
1573
1574 // We own the data, so we can just do a realloc().
1575 if (desired > mDataCapacity) {
1576 uint8_t* data = (uint8_t*)realloc(mData, desired);
1577 if (data) {
1578 mData = data;
1579 mDataCapacity = desired;
1580 } else if (desired > mDataCapacity) {
1581 mError = NO_MEMORY;
1582 return NO_MEMORY;
1583 }
1584 } else {
Dianne Hackborn97e2bcd2011-04-13 18:15:56 -07001585 if (mDataSize > desired) {
1586 mDataSize = desired;
Steve Block6807e592011-10-20 11:56:00 +01001587 ALOGV("continueWrite Setting data size of %p to %d\n", this, mDataSize);
Dianne Hackborn97e2bcd2011-04-13 18:15:56 -07001588 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001589 if (mDataPos > desired) {
1590 mDataPos = desired;
Steve Block6807e592011-10-20 11:56:00 +01001591 ALOGV("continueWrite Setting data pos of %p to %d\n", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001592 }
1593 }
1594
1595 } else {
1596 // This is the first data. Easy!
1597 uint8_t* data = (uint8_t*)malloc(desired);
1598 if (!data) {
1599 mError = NO_MEMORY;
1600 return NO_MEMORY;
1601 }
Hyejin Kim3f727c02013-03-09 11:28:54 +09001602
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001603 if(!(mDataCapacity == 0 && mObjects == NULL
1604 && mObjectsCapacity == 0)) {
Colin Cross6f4f3ab2014-02-05 17:42:44 -08001605 ALOGE("continueWrite: %zu/%p/%zu/%zu", mDataCapacity, mObjects, mObjectsCapacity, desired);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001606 }
1607
1608 mData = data;
1609 mDataSize = mDataPos = 0;
Steve Block6807e592011-10-20 11:56:00 +01001610 ALOGV("continueWrite Setting data size of %p to %d\n", this, mDataSize);
1611 ALOGV("continueWrite Setting data pos of %p to %d\n", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001612 mDataCapacity = desired;
1613 }
1614
1615 return NO_ERROR;
1616}
1617
1618void Parcel::initState()
1619{
1620 mError = NO_ERROR;
1621 mData = 0;
1622 mDataSize = 0;
1623 mDataCapacity = 0;
1624 mDataPos = 0;
Steve Block6807e592011-10-20 11:56:00 +01001625 ALOGV("initState Setting data size of %p to %d\n", this, mDataSize);
1626 ALOGV("initState Setting data pos of %p to %d\n", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001627 mObjects = NULL;
1628 mObjectsSize = 0;
1629 mObjectsCapacity = 0;
1630 mNextObjectHint = 0;
1631 mHasFds = false;
1632 mFdsKnown = true;
Dianne Hackborn8938ed22011-09-28 23:19:47 -04001633 mAllowFds = true;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001634 mOwner = NULL;
1635}
1636
1637void Parcel::scanForFds() const
1638{
1639 bool hasFds = false;
1640 for (size_t i=0; i<mObjectsSize; i++) {
1641 const flat_binder_object* flat
1642 = reinterpret_cast<const flat_binder_object*>(mData + mObjects[i]);
1643 if (flat->type == BINDER_TYPE_FD) {
1644 hasFds = true;
1645 break;
1646 }
1647 }
1648 mHasFds = hasFds;
1649 mFdsKnown = true;
1650}
1651
Jeff Brown5707dbf2011-09-23 21:17:56 -07001652// --- Parcel::Blob ---
1653
1654Parcel::Blob::Blob() :
1655 mMapped(false), mData(NULL), mSize(0) {
1656}
1657
1658Parcel::Blob::~Blob() {
1659 release();
1660}
1661
1662void Parcel::Blob::release() {
1663 if (mMapped && mData) {
1664 ::munmap(mData, mSize);
1665 }
1666 clear();
1667}
1668
1669void Parcel::Blob::init(bool mapped, void* data, size_t size) {
1670 mMapped = mapped;
1671 mData = data;
1672 mSize = size;
1673}
1674
1675void Parcel::Blob::clear() {
1676 mMapped = false;
1677 mData = NULL;
1678 mSize = 0;
1679}
1680
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001681}; // namespace android