blob: 03bcf01e52100d22af426038a155b1b41f80066e [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
38#include <stdio.h>
39#include <stdlib.h>
40#include <stdint.h>
Jeff Brown5707dbf2011-09-23 21:17:56 -070041#include <sys/mman.h>
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -070042
43#ifndef INT32_MAX
44#define INT32_MAX ((int32_t)(2147483647))
45#endif
46
47#define LOG_REFS(...)
Steve Block9f760152011-10-12 17:27:03 +010048//#define LOG_REFS(...) ALOG(LOG_DEBUG, "Parcel", __VA_ARGS__)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -070049
50// ---------------------------------------------------------------------------
51
52#define PAD_SIZE(s) (((s)+3)&~3)
53
Brad Fitzpatricka877cd82010-07-07 16:06:39 -070054// Note: must be kept in sync with android/os/StrictMode.java's PENALTY_GATHER
55#define STRICT_MODE_PENALTY_GATHER 0x100
56
Brad Fitzpatrickd36f4a52010-07-12 11:05:38 -070057// Note: must be kept in sync with android/os/Parcel.java's EX_HAS_REPLY_HEADER
58#define EX_HAS_REPLY_HEADER -128
59
Jeff Brown5707dbf2011-09-23 21:17:56 -070060// Maximum size of a blob to transfer in-place.
61static const size_t IN_PLACE_BLOB_LIMIT = 40 * 1024;
62
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -070063// XXX This can be made public if we want to provide
64// support for typed data.
65struct small_flat_data
66{
67 uint32_t type;
68 uint32_t data;
69};
70
71namespace android {
72
73void acquire_object(const sp<ProcessState>& proc,
74 const flat_binder_object& obj, const void* who)
75{
76 switch (obj.type) {
77 case BINDER_TYPE_BINDER:
78 if (obj.binder) {
79 LOG_REFS("Parcel %p acquiring reference on local %p", who, obj.cookie);
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -080080 reinterpret_cast<IBinder*>(obj.cookie)->incStrong(who);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -070081 }
82 return;
83 case BINDER_TYPE_WEAK_BINDER:
84 if (obj.binder)
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -080085 reinterpret_cast<RefBase::weakref_type*>(obj.binder)->incWeak(who);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -070086 return;
87 case BINDER_TYPE_HANDLE: {
88 const sp<IBinder> b = proc->getStrongProxyForHandle(obj.handle);
89 if (b != NULL) {
90 LOG_REFS("Parcel %p acquiring reference on remote %p", who, b.get());
91 b->incStrong(who);
92 }
93 return;
94 }
95 case BINDER_TYPE_WEAK_HANDLE: {
96 const wp<IBinder> b = proc->getWeakProxyForHandle(obj.handle);
97 if (b != NULL) b.get_refs()->incWeak(who);
98 return;
99 }
100 case BINDER_TYPE_FD: {
101 // intentionally blank -- nothing to do to acquire this, but we do
102 // recognize it as a legitimate object type.
103 return;
104 }
105 }
106
Colin Cross6f4f3ab2014-02-05 17:42:44 -0800107 ALOGD("Invalid object type 0x%08x", obj.type);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700108}
109
110void release_object(const sp<ProcessState>& proc,
111 const flat_binder_object& obj, const void* who)
112{
113 switch (obj.type) {
114 case BINDER_TYPE_BINDER:
115 if (obj.binder) {
116 LOG_REFS("Parcel %p releasing reference on local %p", who, obj.cookie);
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800117 reinterpret_cast<IBinder*>(obj.cookie)->decStrong(who);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700118 }
119 return;
120 case BINDER_TYPE_WEAK_BINDER:
121 if (obj.binder)
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800122 reinterpret_cast<RefBase::weakref_type*>(obj.binder)->decWeak(who);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700123 return;
124 case BINDER_TYPE_HANDLE: {
125 const sp<IBinder> b = proc->getStrongProxyForHandle(obj.handle);
126 if (b != NULL) {
127 LOG_REFS("Parcel %p releasing reference on remote %p", who, b.get());
128 b->decStrong(who);
129 }
130 return;
131 }
132 case BINDER_TYPE_WEAK_HANDLE: {
133 const wp<IBinder> b = proc->getWeakProxyForHandle(obj.handle);
134 if (b != NULL) b.get_refs()->decWeak(who);
135 return;
136 }
137 case BINDER_TYPE_FD: {
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800138 if (obj.cookie != 0) close(obj.handle);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700139 return;
140 }
141 }
142
Colin Cross6f4f3ab2014-02-05 17:42:44 -0800143 ALOGE("Invalid object type 0x%08x", obj.type);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700144}
145
146inline static status_t finish_flatten_binder(
Colin Cross6f4f3ab2014-02-05 17:42:44 -0800147 const sp<IBinder>& /*binder*/, const flat_binder_object& flat, Parcel* out)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700148{
149 return out->writeObject(flat, false);
150}
151
Colin Cross6f4f3ab2014-02-05 17:42:44 -0800152status_t flatten_binder(const sp<ProcessState>& /*proc*/,
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700153 const sp<IBinder>& binder, Parcel* out)
154{
155 flat_binder_object obj;
156
157 obj.flags = 0x7f | FLAT_BINDER_FLAG_ACCEPTS_FDS;
158 if (binder != NULL) {
159 IBinder *local = binder->localBinder();
160 if (!local) {
161 BpBinder *proxy = binder->remoteBinder();
162 if (proxy == NULL) {
Steve Blocke6f43dd2012-01-06 19:20:56 +0000163 ALOGE("null proxy");
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700164 }
165 const int32_t handle = proxy ? proxy->handle() : 0;
166 obj.type = BINDER_TYPE_HANDLE;
167 obj.handle = handle;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800168 obj.cookie = 0;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700169 } else {
170 obj.type = BINDER_TYPE_BINDER;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800171 obj.binder = reinterpret_cast<uintptr_t>(local->getWeakRefs());
172 obj.cookie = reinterpret_cast<uintptr_t>(local);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700173 }
174 } else {
175 obj.type = BINDER_TYPE_BINDER;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800176 obj.binder = 0;
177 obj.cookie = 0;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700178 }
179
180 return finish_flatten_binder(binder, obj, out);
181}
182
Colin Cross6f4f3ab2014-02-05 17:42:44 -0800183status_t flatten_binder(const sp<ProcessState>& /*proc*/,
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700184 const wp<IBinder>& binder, Parcel* out)
185{
186 flat_binder_object obj;
187
188 obj.flags = 0x7f | FLAT_BINDER_FLAG_ACCEPTS_FDS;
189 if (binder != NULL) {
190 sp<IBinder> real = binder.promote();
191 if (real != NULL) {
192 IBinder *local = real->localBinder();
193 if (!local) {
194 BpBinder *proxy = real->remoteBinder();
195 if (proxy == NULL) {
Steve Blocke6f43dd2012-01-06 19:20:56 +0000196 ALOGE("null proxy");
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700197 }
198 const int32_t handle = proxy ? proxy->handle() : 0;
199 obj.type = BINDER_TYPE_WEAK_HANDLE;
200 obj.handle = handle;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800201 obj.cookie = 0;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700202 } else {
203 obj.type = BINDER_TYPE_WEAK_BINDER;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800204 obj.binder = reinterpret_cast<uintptr_t>(binder.get_refs());
205 obj.cookie = reinterpret_cast<uintptr_t>(binder.unsafe_get());
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700206 }
207 return finish_flatten_binder(real, obj, out);
208 }
209
210 // XXX How to deal? In order to flatten the given binder,
211 // we need to probe it for information, which requires a primary
212 // reference... but we don't have one.
213 //
214 // The OpenBinder implementation uses a dynamic_cast<> here,
215 // but we can't do that with the different reference counting
216 // implementation we are using.
Steve Blocke6f43dd2012-01-06 19:20:56 +0000217 ALOGE("Unable to unflatten Binder weak reference!");
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700218 obj.type = BINDER_TYPE_BINDER;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800219 obj.binder = 0;
220 obj.cookie = 0;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700221 return finish_flatten_binder(NULL, obj, out);
222
223 } else {
224 obj.type = BINDER_TYPE_BINDER;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800225 obj.binder = 0;
226 obj.cookie = 0;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700227 return finish_flatten_binder(NULL, obj, out);
228 }
229}
230
231inline static status_t finish_unflatten_binder(
Colin Cross6f4f3ab2014-02-05 17:42:44 -0800232 BpBinder* /*proxy*/, const flat_binder_object& /*flat*/,
233 const Parcel& /*in*/)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700234{
235 return NO_ERROR;
236}
237
238status_t unflatten_binder(const sp<ProcessState>& proc,
239 const Parcel& in, sp<IBinder>* out)
240{
241 const flat_binder_object* flat = in.readObject(false);
242
243 if (flat) {
244 switch (flat->type) {
245 case BINDER_TYPE_BINDER:
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800246 *out = reinterpret_cast<IBinder*>(flat->cookie);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700247 return finish_unflatten_binder(NULL, *flat, in);
248 case BINDER_TYPE_HANDLE:
249 *out = proc->getStrongProxyForHandle(flat->handle);
250 return finish_unflatten_binder(
251 static_cast<BpBinder*>(out->get()), *flat, in);
252 }
253 }
254 return BAD_TYPE;
255}
256
257status_t unflatten_binder(const sp<ProcessState>& proc,
258 const Parcel& in, wp<IBinder>* out)
259{
260 const flat_binder_object* flat = in.readObject(false);
261
262 if (flat) {
263 switch (flat->type) {
264 case BINDER_TYPE_BINDER:
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800265 *out = reinterpret_cast<IBinder*>(flat->cookie);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700266 return finish_unflatten_binder(NULL, *flat, in);
267 case BINDER_TYPE_WEAK_BINDER:
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800268 if (flat->binder != 0) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700269 out->set_object_and_refs(
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800270 reinterpret_cast<IBinder*>(flat->cookie),
271 reinterpret_cast<RefBase::weakref_type*>(flat->binder));
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700272 } else {
273 *out = NULL;
274 }
275 return finish_unflatten_binder(NULL, *flat, in);
276 case BINDER_TYPE_HANDLE:
277 case BINDER_TYPE_WEAK_HANDLE:
278 *out = proc->getWeakProxyForHandle(flat->handle);
279 return finish_unflatten_binder(
280 static_cast<BpBinder*>(out->unsafe_get()), *flat, in);
281 }
282 }
283 return BAD_TYPE;
284}
285
286// ---------------------------------------------------------------------------
287
288Parcel::Parcel()
289{
290 initState();
291}
292
293Parcel::~Parcel()
294{
295 freeDataNoInit();
296}
297
298const uint8_t* Parcel::data() const
299{
300 return mData;
301}
302
303size_t Parcel::dataSize() const
304{
305 return (mDataSize > mDataPos ? mDataSize : mDataPos);
306}
307
308size_t Parcel::dataAvail() const
309{
310 // TODO: decide what to do about the possibility that this can
311 // report an available-data size that exceeds a Java int's max
312 // positive value, causing havoc. Fortunately this will only
313 // happen if someone constructs a Parcel containing more than two
314 // gigabytes of data, which on typical phone hardware is simply
315 // not possible.
316 return dataSize() - dataPosition();
317}
318
319size_t Parcel::dataPosition() const
320{
321 return mDataPos;
322}
323
324size_t Parcel::dataCapacity() const
325{
326 return mDataCapacity;
327}
328
329status_t Parcel::setDataSize(size_t size)
330{
331 status_t err;
332 err = continueWrite(size);
333 if (err == NO_ERROR) {
334 mDataSize = size;
Steve Block6807e592011-10-20 11:56:00 +0100335 ALOGV("setDataSize Setting data size of %p to %d\n", this, mDataSize);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700336 }
337 return err;
338}
339
340void Parcel::setDataPosition(size_t pos) const
341{
342 mDataPos = pos;
343 mNextObjectHint = 0;
344}
345
346status_t Parcel::setDataCapacity(size_t size)
347{
Dianne Hackborn97e2bcd2011-04-13 18:15:56 -0700348 if (size > mDataCapacity) return continueWrite(size);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700349 return NO_ERROR;
350}
351
352status_t Parcel::setData(const uint8_t* buffer, size_t len)
353{
354 status_t err = restartWrite(len);
355 if (err == NO_ERROR) {
356 memcpy(const_cast<uint8_t*>(data()), buffer, len);
357 mDataSize = len;
358 mFdsKnown = false;
359 }
360 return err;
361}
362
Andreas Huber51faf462011-04-13 10:21:56 -0700363status_t Parcel::appendFrom(const Parcel *parcel, size_t offset, size_t len)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700364{
365 const sp<ProcessState> proc(ProcessState::self());
366 status_t err;
Andreas Huber51faf462011-04-13 10:21:56 -0700367 const uint8_t *data = parcel->mData;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800368 const binder_size_t *objects = parcel->mObjects;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700369 size_t size = parcel->mObjectsSize;
370 int startPos = mDataPos;
371 int firstIndex = -1, lastIndex = -2;
372
373 if (len == 0) {
374 return NO_ERROR;
375 }
376
377 // range checks against the source parcel size
378 if ((offset > parcel->mDataSize)
379 || (len > parcel->mDataSize)
380 || (offset + len > parcel->mDataSize)) {
381 return BAD_VALUE;
382 }
383
384 // Count objects in range
385 for (int i = 0; i < (int) size; i++) {
386 size_t off = objects[i];
387 if ((off >= offset) && (off < offset + len)) {
388 if (firstIndex == -1) {
389 firstIndex = i;
390 }
391 lastIndex = i;
392 }
393 }
394 int numObjects = lastIndex - firstIndex + 1;
395
Dianne Hackborn97e2bcd2011-04-13 18:15:56 -0700396 if ((mDataSize+len) > mDataCapacity) {
397 // grow data
398 err = growData(len);
399 if (err != NO_ERROR) {
400 return err;
401 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700402 }
403
404 // append data
405 memcpy(mData + mDataPos, data + offset, len);
406 mDataPos += len;
407 mDataSize += len;
408
Dianne Hackborn8938ed22011-09-28 23:19:47 -0400409 err = NO_ERROR;
410
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700411 if (numObjects > 0) {
412 // grow objects
413 if (mObjectsCapacity < mObjectsSize + numObjects) {
414 int newSize = ((mObjectsSize + numObjects)*3)/2;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800415 binder_size_t *objects =
416 (binder_size_t*)realloc(mObjects, newSize*sizeof(binder_size_t));
417 if (objects == (binder_size_t*)0) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700418 return NO_MEMORY;
419 }
420 mObjects = objects;
421 mObjectsCapacity = newSize;
422 }
423
424 // append and acquire objects
425 int idx = mObjectsSize;
426 for (int i = firstIndex; i <= lastIndex; i++) {
427 size_t off = objects[i] - offset + startPos;
428 mObjects[idx++] = off;
429 mObjectsSize++;
430
Dianne Hackborn8af0f822009-05-22 13:20:23 -0700431 flat_binder_object* flat
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700432 = reinterpret_cast<flat_binder_object*>(mData + off);
433 acquire_object(proc, *flat, this);
434
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700435 if (flat->type == BINDER_TYPE_FD) {
Dianne Hackborn8af0f822009-05-22 13:20:23 -0700436 // If this is a file descriptor, we need to dup it so the
437 // new Parcel now owns its own fd, and can declare that we
438 // officially know we have fds.
439 flat->handle = dup(flat->handle);
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800440 flat->cookie = 1;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700441 mHasFds = mFdsKnown = true;
Dianne Hackborn8938ed22011-09-28 23:19:47 -0400442 if (!mAllowFds) {
443 err = FDS_NOT_ALLOWED;
444 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700445 }
446 }
447 }
448
Dianne Hackborn8938ed22011-09-28 23:19:47 -0400449 return err;
450}
451
Dianne Hackborn7746cc32011-10-03 21:09:35 -0700452bool Parcel::pushAllowFds(bool allowFds)
Dianne Hackborn8938ed22011-09-28 23:19:47 -0400453{
454 const bool origValue = mAllowFds;
Dianne Hackborn7746cc32011-10-03 21:09:35 -0700455 if (!allowFds) {
456 mAllowFds = false;
457 }
Dianne Hackborn8938ed22011-09-28 23:19:47 -0400458 return origValue;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700459}
460
Dianne Hackborn7746cc32011-10-03 21:09:35 -0700461void Parcel::restoreAllowFds(bool lastValue)
462{
463 mAllowFds = lastValue;
464}
465
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700466bool Parcel::hasFileDescriptors() const
467{
468 if (!mFdsKnown) {
469 scanForFds();
470 }
471 return mHasFds;
472}
473
Brad Fitzpatrick702ea9d2010-06-18 13:07:53 -0700474// Write RPC headers. (previously just the interface token)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700475status_t Parcel::writeInterfaceToken(const String16& interface)
476{
Brad Fitzpatricka877cd82010-07-07 16:06:39 -0700477 writeInt32(IPCThreadState::self()->getStrictModePolicy() |
478 STRICT_MODE_PENALTY_GATHER);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700479 // currently the interface identification token is just its name as a string
480 return writeString16(interface);
481}
482
Mathias Agopian83c04462009-05-22 19:00:22 -0700483bool Parcel::checkInterface(IBinder* binder) const
484{
Brad Fitzpatrick702ea9d2010-06-18 13:07:53 -0700485 return enforceInterface(binder->getInterfaceDescriptor());
Mathias Agopian83c04462009-05-22 19:00:22 -0700486}
487
Brad Fitzpatricka877cd82010-07-07 16:06:39 -0700488bool Parcel::enforceInterface(const String16& interface,
Brad Fitzpatrick70081a12010-07-27 09:49:11 -0700489 IPCThreadState* threadState) const
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700490{
Brad Fitzpatrick70081a12010-07-27 09:49:11 -0700491 int32_t strictPolicy = readInt32();
492 if (threadState == NULL) {
493 threadState = IPCThreadState::self();
Brad Fitzpatricka877cd82010-07-07 16:06:39 -0700494 }
Brad Fitzpatrick52736032010-08-30 16:01:16 -0700495 if ((threadState->getLastTransactionBinderFlags() &
496 IBinder::FLAG_ONEWAY) != 0) {
497 // For one-way calls, the callee is running entirely
498 // disconnected from the caller, so disable StrictMode entirely.
499 // Not only does disk/network usage not impact the caller, but
500 // there's no way to commuicate back any violations anyway.
501 threadState->setStrictModePolicy(0);
502 } else {
503 threadState->setStrictModePolicy(strictPolicy);
504 }
Mathias Agopian83c04462009-05-22 19:00:22 -0700505 const String16 str(readString16());
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700506 if (str == interface) {
507 return true;
508 } else {
Steve Block32397c12012-01-05 23:22:43 +0000509 ALOGW("**** enforceInterface() expected '%s' but read '%s'\n",
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700510 String8(interface).string(), String8(str).string());
511 return false;
512 }
Brad Fitzpatrick702ea9d2010-06-18 13:07:53 -0700513}
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700514
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800515const binder_size_t* Parcel::objects() const
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700516{
517 return mObjects;
518}
519
520size_t Parcel::objectsCount() const
521{
522 return mObjectsSize;
523}
524
525status_t Parcel::errorCheck() const
526{
527 return mError;
528}
529
530void Parcel::setError(status_t err)
531{
532 mError = err;
533}
534
535status_t Parcel::finishWrite(size_t len)
536{
537 //printf("Finish write of %d\n", len);
538 mDataPos += len;
Steve Block6807e592011-10-20 11:56:00 +0100539 ALOGV("finishWrite Setting data pos of %p to %d\n", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700540 if (mDataPos > mDataSize) {
541 mDataSize = mDataPos;
Steve Block6807e592011-10-20 11:56:00 +0100542 ALOGV("finishWrite Setting data size of %p to %d\n", this, mDataSize);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700543 }
544 //printf("New pos=%d, size=%d\n", mDataPos, mDataSize);
545 return NO_ERROR;
546}
547
548status_t Parcel::writeUnpadded(const void* data, size_t len)
549{
550 size_t end = mDataPos + len;
551 if (end < mDataPos) {
552 // integer overflow
553 return BAD_VALUE;
554 }
555
556 if (end <= mDataCapacity) {
557restart_write:
558 memcpy(mData+mDataPos, data, len);
559 return finishWrite(len);
560 }
561
562 status_t err = growData(len);
563 if (err == NO_ERROR) goto restart_write;
564 return err;
565}
566
567status_t Parcel::write(const void* data, size_t len)
568{
569 void* const d = writeInplace(len);
570 if (d) {
571 memcpy(d, data, len);
572 return NO_ERROR;
573 }
574 return mError;
575}
576
577void* Parcel::writeInplace(size_t len)
578{
579 const size_t padded = PAD_SIZE(len);
580
581 // sanity check for integer overflow
582 if (mDataPos+padded < mDataPos) {
583 return NULL;
584 }
585
586 if ((mDataPos+padded) <= mDataCapacity) {
587restart_write:
588 //printf("Writing %ld bytes, padded to %ld\n", len, padded);
589 uint8_t* const data = mData+mDataPos;
590
591 // Need to pad at end?
592 if (padded != len) {
593#if BYTE_ORDER == BIG_ENDIAN
594 static const uint32_t mask[4] = {
595 0x00000000, 0xffffff00, 0xffff0000, 0xff000000
596 };
597#endif
598#if BYTE_ORDER == LITTLE_ENDIAN
599 static const uint32_t mask[4] = {
600 0x00000000, 0x00ffffff, 0x0000ffff, 0x000000ff
601 };
602#endif
603 //printf("Applying pad mask: %p to %p\n", (void*)mask[padded-len],
604 // *reinterpret_cast<void**>(data+padded-4));
605 *reinterpret_cast<uint32_t*>(data+padded-4) &= mask[padded-len];
606 }
607
608 finishWrite(padded);
609 return data;
610 }
611
612 status_t err = growData(padded);
613 if (err == NO_ERROR) goto restart_write;
614 return NULL;
615}
616
617status_t Parcel::writeInt32(int32_t val)
618{
Andreas Huber84a6d042009-08-17 13:33:27 -0700619 return writeAligned(val);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700620}
Marco Nelissen708cc792013-10-16 10:57:51 -0700621status_t Parcel::writeInt32Array(size_t len, const int32_t *val) {
622 if (!val) {
623 return writeAligned(-1);
624 }
625 status_t ret = writeAligned(len);
626 if (ret == NO_ERROR) {
627 ret = write(val, len * sizeof(*val));
628 }
629 return ret;
630}
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700631
632status_t Parcel::writeInt64(int64_t val)
633{
Andreas Huber84a6d042009-08-17 13:33:27 -0700634 return writeAligned(val);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700635}
636
Serban Constantinescuf683e012013-11-05 16:53:55 +0000637status_t Parcel::writePointer(uintptr_t val)
638{
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800639 return writeAligned<binder_uintptr_t>(val);
Serban Constantinescuf683e012013-11-05 16:53:55 +0000640}
641
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700642status_t Parcel::writeFloat(float val)
643{
Andreas Huber84a6d042009-08-17 13:33:27 -0700644 return writeAligned(val);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700645}
646
Douglas Leungcc1a4bb2013-01-11 15:00:55 -0800647#if defined(__mips__) && defined(__mips_hard_float)
648
649status_t Parcel::writeDouble(double val)
650{
651 union {
652 double d;
653 unsigned long long ll;
654 } u;
655 u.d = val;
656 return writeAligned(u.ll);
657}
658
659#else
660
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700661status_t Parcel::writeDouble(double val)
662{
Andreas Huber84a6d042009-08-17 13:33:27 -0700663 return writeAligned(val);
664}
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700665
Douglas Leungcc1a4bb2013-01-11 15:00:55 -0800666#endif
667
Andreas Huber84a6d042009-08-17 13:33:27 -0700668status_t Parcel::writeIntPtr(intptr_t val)
669{
670 return writeAligned(val);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700671}
672
673status_t Parcel::writeCString(const char* str)
674{
675 return write(str, strlen(str)+1);
676}
677
678status_t Parcel::writeString8(const String8& str)
679{
680 status_t err = writeInt32(str.bytes());
Pravat Dalbeherad1dff8d2010-12-15 08:40:00 +0100681 // only write string if its length is more than zero characters,
682 // as readString8 will only read if the length field is non-zero.
683 // this is slightly different from how writeString16 works.
684 if (str.bytes() > 0 && err == NO_ERROR) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700685 err = write(str.string(), str.bytes()+1);
686 }
687 return err;
688}
689
690status_t Parcel::writeString16(const String16& str)
691{
692 return writeString16(str.string(), str.size());
693}
694
695status_t Parcel::writeString16(const char16_t* str, size_t len)
696{
697 if (str == NULL) return writeInt32(-1);
698
699 status_t err = writeInt32(len);
700 if (err == NO_ERROR) {
701 len *= sizeof(char16_t);
702 uint8_t* data = (uint8_t*)writeInplace(len+sizeof(char16_t));
703 if (data) {
704 memcpy(data, str, len);
705 *reinterpret_cast<char16_t*>(data+len) = 0;
706 return NO_ERROR;
707 }
708 err = mError;
709 }
710 return err;
711}
712
713status_t Parcel::writeStrongBinder(const sp<IBinder>& val)
714{
715 return flatten_binder(ProcessState::self(), val, this);
716}
717
718status_t Parcel::writeWeakBinder(const wp<IBinder>& val)
719{
720 return flatten_binder(ProcessState::self(), val, this);
721}
722
Mathias Agopiana47f02a2009-05-21 16:29:38 -0700723status_t Parcel::writeNativeHandle(const native_handle* handle)
The Android Open Source Project5f78a482009-01-20 14:03:58 -0800724{
Mathias Agopian1d0a95b2009-07-31 16:12:13 -0700725 if (!handle || handle->version != sizeof(native_handle))
The Android Open Source Project5f78a482009-01-20 14:03:58 -0800726 return BAD_TYPE;
727
728 status_t err;
Mathias Agopiana47f02a2009-05-21 16:29:38 -0700729 err = writeInt32(handle->numFds);
The Android Open Source Project5f78a482009-01-20 14:03:58 -0800730 if (err != NO_ERROR) return err;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800731
Mathias Agopiana47f02a2009-05-21 16:29:38 -0700732 err = writeInt32(handle->numInts);
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 for (int i=0 ; err==NO_ERROR && i<handle->numFds ; i++)
736 err = writeDupFileDescriptor(handle->data[i]);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800737
738 if (err != NO_ERROR) {
Steve Block9d453682011-12-20 16:23:08 +0000739 ALOGD("write native handle, write dup fd failed");
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800740 return err;
741 }
Mathias Agopiana47f02a2009-05-21 16:29:38 -0700742 err = write(handle->data + handle->numFds, sizeof(int)*handle->numInts);
The Android Open Source Project5f78a482009-01-20 14:03:58 -0800743 return err;
744}
745
Jeff Brown93ff1f92011-11-04 19:01:44 -0700746status_t Parcel::writeFileDescriptor(int fd, bool takeOwnership)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700747{
748 flat_binder_object obj;
749 obj.type = BINDER_TYPE_FD;
750 obj.flags = 0x7f | FLAT_BINDER_FLAG_ACCEPTS_FDS;
751 obj.handle = fd;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800752 obj.cookie = takeOwnership ? 1 : 0;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700753 return writeObject(obj, true);
754}
755
756status_t Parcel::writeDupFileDescriptor(int fd)
757{
Jeff Brownd341c712011-11-04 20:19:33 -0700758 int dupFd = dup(fd);
759 if (dupFd < 0) {
760 return -errno;
761 }
762 status_t err = writeFileDescriptor(dupFd, true /*takeOwnership*/);
763 if (err) {
764 close(dupFd);
765 }
766 return err;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700767}
768
Jeff Brown5707dbf2011-09-23 21:17:56 -0700769status_t Parcel::writeBlob(size_t len, WritableBlob* outBlob)
770{
771 status_t status;
772
773 if (!mAllowFds || len <= IN_PLACE_BLOB_LIMIT) {
Steve Block6807e592011-10-20 11:56:00 +0100774 ALOGV("writeBlob: write in place");
Jeff Brown5707dbf2011-09-23 21:17:56 -0700775 status = writeInt32(0);
776 if (status) return status;
777
778 void* ptr = writeInplace(len);
779 if (!ptr) return NO_MEMORY;
780
781 outBlob->init(false /*mapped*/, ptr, len);
782 return NO_ERROR;
783 }
784
Steve Block6807e592011-10-20 11:56:00 +0100785 ALOGV("writeBlob: write to ashmem");
Jeff Brown5707dbf2011-09-23 21:17:56 -0700786 int fd = ashmem_create_region("Parcel Blob", len);
787 if (fd < 0) return NO_MEMORY;
788
789 int result = ashmem_set_prot_region(fd, PROT_READ | PROT_WRITE);
790 if (result < 0) {
Jeff Brownec4e0062011-10-10 14:50:10 -0700791 status = result;
Jeff Brown5707dbf2011-09-23 21:17:56 -0700792 } else {
793 void* ptr = ::mmap(NULL, len, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
794 if (ptr == MAP_FAILED) {
795 status = -errno;
796 } else {
797 result = ashmem_set_prot_region(fd, PROT_READ);
798 if (result < 0) {
Jeff Brownec4e0062011-10-10 14:50:10 -0700799 status = result;
Jeff Brown5707dbf2011-09-23 21:17:56 -0700800 } else {
801 status = writeInt32(1);
802 if (!status) {
Jeff Brown93ff1f92011-11-04 19:01:44 -0700803 status = writeFileDescriptor(fd, true /*takeOwnership*/);
Jeff Brown5707dbf2011-09-23 21:17:56 -0700804 if (!status) {
805 outBlob->init(true /*mapped*/, ptr, len);
806 return NO_ERROR;
807 }
808 }
809 }
810 }
811 ::munmap(ptr, len);
812 }
813 ::close(fd);
814 return status;
815}
816
Mathias Agopiane1424282013-07-29 21:24:40 -0700817status_t Parcel::write(const FlattenableHelperInterface& val)
Mathias Agopian98e71dd2010-02-11 17:30:52 -0800818{
819 status_t err;
820
821 // size if needed
Mathias Agopiane1424282013-07-29 21:24:40 -0700822 const size_t len = val.getFlattenedSize();
823 const size_t fd_count = val.getFdCount();
Mathias Agopian98e71dd2010-02-11 17:30:52 -0800824
825 err = this->writeInt32(len);
826 if (err) return err;
827
828 err = this->writeInt32(fd_count);
829 if (err) return err;
830
831 // payload
Mathias Agopiane1424282013-07-29 21:24:40 -0700832 void* const buf = this->writeInplace(PAD_SIZE(len));
Mathias Agopian98e71dd2010-02-11 17:30:52 -0800833 if (buf == NULL)
834 return BAD_VALUE;
835
836 int* fds = NULL;
837 if (fd_count) {
838 fds = new int[fd_count];
839 }
840
841 err = val.flatten(buf, len, fds, fd_count);
842 for (size_t i=0 ; i<fd_count && err==NO_ERROR ; i++) {
843 err = this->writeDupFileDescriptor( fds[i] );
844 }
845
846 if (fd_count) {
847 delete [] fds;
848 }
849
850 return err;
851}
852
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700853status_t Parcel::writeObject(const flat_binder_object& val, bool nullMetaData)
854{
855 const bool enoughData = (mDataPos+sizeof(val)) <= mDataCapacity;
856 const bool enoughObjects = mObjectsSize < mObjectsCapacity;
857 if (enoughData && enoughObjects) {
858restart_write:
859 *reinterpret_cast<flat_binder_object*>(mData+mDataPos) = val;
860
861 // Need to write meta-data?
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800862 if (nullMetaData || val.binder != 0) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700863 mObjects[mObjectsSize] = mDataPos;
864 acquire_object(ProcessState::self(), val, this);
865 mObjectsSize++;
866 }
867
868 // remember if it's a file descriptor
869 if (val.type == BINDER_TYPE_FD) {
Dianne Hackborn8938ed22011-09-28 23:19:47 -0400870 if (!mAllowFds) {
871 return FDS_NOT_ALLOWED;
872 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700873 mHasFds = mFdsKnown = true;
874 }
875
876 return finishWrite(sizeof(flat_binder_object));
877 }
878
879 if (!enoughData) {
880 const status_t err = growData(sizeof(val));
881 if (err != NO_ERROR) return err;
882 }
883 if (!enoughObjects) {
884 size_t newSize = ((mObjectsSize+2)*3)/2;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800885 binder_size_t* objects = (binder_size_t*)realloc(mObjects, newSize*sizeof(binder_size_t));
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700886 if (objects == NULL) return NO_MEMORY;
887 mObjects = objects;
888 mObjectsCapacity = newSize;
889 }
890
891 goto restart_write;
892}
893
Brad Fitzpatrick837a0d02010-07-13 15:33:35 -0700894status_t Parcel::writeNoException()
895{
896 return writeInt32(0);
897}
898
Colin Cross6f4f3ab2014-02-05 17:42:44 -0800899void Parcel::remove(size_t /*start*/, size_t /*amt*/)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700900{
901 LOG_ALWAYS_FATAL("Parcel::remove() not yet implemented!");
902}
903
904status_t Parcel::read(void* outData, size_t len) const
905{
906 if ((mDataPos+PAD_SIZE(len)) >= mDataPos && (mDataPos+PAD_SIZE(len)) <= mDataSize) {
907 memcpy(outData, mData+mDataPos, len);
908 mDataPos += PAD_SIZE(len);
Steve Block6807e592011-10-20 11:56:00 +0100909 ALOGV("read Setting data pos of %p to %d\n", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700910 return NO_ERROR;
911 }
912 return NOT_ENOUGH_DATA;
913}
914
915const void* Parcel::readInplace(size_t len) const
916{
917 if ((mDataPos+PAD_SIZE(len)) >= mDataPos && (mDataPos+PAD_SIZE(len)) <= mDataSize) {
918 const void* data = mData+mDataPos;
919 mDataPos += PAD_SIZE(len);
Steve Block6807e592011-10-20 11:56:00 +0100920 ALOGV("readInplace Setting data pos of %p to %d\n", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700921 return data;
922 }
923 return NULL;
924}
925
Andreas Huber84a6d042009-08-17 13:33:27 -0700926template<class T>
927status_t Parcel::readAligned(T *pArg) const {
928 COMPILE_TIME_ASSERT_FUNCTION_SCOPE(PAD_SIZE(sizeof(T)) == sizeof(T));
929
930 if ((mDataPos+sizeof(T)) <= mDataSize) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700931 const void* data = mData+mDataPos;
Andreas Huber84a6d042009-08-17 13:33:27 -0700932 mDataPos += sizeof(T);
933 *pArg = *reinterpret_cast<const T*>(data);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700934 return NO_ERROR;
935 } else {
936 return NOT_ENOUGH_DATA;
937 }
938}
939
Andreas Huber84a6d042009-08-17 13:33:27 -0700940template<class T>
941T Parcel::readAligned() const {
942 T result;
943 if (readAligned(&result) != NO_ERROR) {
944 result = 0;
945 }
946
947 return result;
948}
949
950template<class T>
951status_t Parcel::writeAligned(T val) {
952 COMPILE_TIME_ASSERT_FUNCTION_SCOPE(PAD_SIZE(sizeof(T)) == sizeof(T));
953
954 if ((mDataPos+sizeof(val)) <= mDataCapacity) {
955restart_write:
956 *reinterpret_cast<T*>(mData+mDataPos) = val;
957 return finishWrite(sizeof(val));
958 }
959
960 status_t err = growData(sizeof(val));
961 if (err == NO_ERROR) goto restart_write;
962 return err;
963}
964
965status_t Parcel::readInt32(int32_t *pArg) const
966{
967 return readAligned(pArg);
968}
969
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700970int32_t Parcel::readInt32() const
971{
Andreas Huber84a6d042009-08-17 13:33:27 -0700972 return readAligned<int32_t>();
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700973}
974
975
976status_t Parcel::readInt64(int64_t *pArg) const
977{
Andreas Huber84a6d042009-08-17 13:33:27 -0700978 return readAligned(pArg);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700979}
980
981
982int64_t Parcel::readInt64() const
983{
Andreas Huber84a6d042009-08-17 13:33:27 -0700984 return readAligned<int64_t>();
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700985}
986
Serban Constantinescuf683e012013-11-05 16:53:55 +0000987status_t Parcel::readPointer(uintptr_t *pArg) const
988{
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800989 status_t ret;
990 binder_uintptr_t ptr;
991 ret = readAligned(&ptr);
992 if (!ret)
993 *pArg = ptr;
994 return ret;
Serban Constantinescuf683e012013-11-05 16:53:55 +0000995}
996
997uintptr_t Parcel::readPointer() const
998{
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800999 return readAligned<binder_uintptr_t>();
Serban Constantinescuf683e012013-11-05 16:53:55 +00001000}
1001
1002
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001003status_t Parcel::readFloat(float *pArg) const
1004{
Andreas Huber84a6d042009-08-17 13:33:27 -07001005 return readAligned(pArg);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001006}
1007
1008
1009float Parcel::readFloat() const
1010{
Andreas Huber84a6d042009-08-17 13:33:27 -07001011 return readAligned<float>();
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001012}
1013
Douglas Leungcc1a4bb2013-01-11 15:00:55 -08001014#if defined(__mips__) && defined(__mips_hard_float)
1015
1016status_t Parcel::readDouble(double *pArg) const
1017{
1018 union {
1019 double d;
1020 unsigned long long ll;
1021 } u;
1022 status_t status;
1023 status = readAligned(&u.ll);
1024 *pArg = u.d;
1025 return status;
1026}
1027
1028double Parcel::readDouble() const
1029{
1030 union {
1031 double d;
1032 unsigned long long ll;
1033 } u;
1034 u.ll = readAligned<unsigned long long>();
1035 return u.d;
1036}
1037
1038#else
1039
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001040status_t Parcel::readDouble(double *pArg) const
1041{
Andreas Huber84a6d042009-08-17 13:33:27 -07001042 return readAligned(pArg);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001043}
1044
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001045double Parcel::readDouble() const
1046{
Andreas Huber84a6d042009-08-17 13:33:27 -07001047 return readAligned<double>();
1048}
1049
Douglas Leungcc1a4bb2013-01-11 15:00:55 -08001050#endif
1051
Andreas Huber84a6d042009-08-17 13:33:27 -07001052status_t Parcel::readIntPtr(intptr_t *pArg) const
1053{
1054 return readAligned(pArg);
1055}
1056
1057
1058intptr_t Parcel::readIntPtr() const
1059{
1060 return readAligned<intptr_t>();
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001061}
1062
1063
1064const char* Parcel::readCString() const
1065{
1066 const size_t avail = mDataSize-mDataPos;
1067 if (avail > 0) {
1068 const char* str = reinterpret_cast<const char*>(mData+mDataPos);
1069 // is the string's trailing NUL within the parcel's valid bounds?
1070 const char* eos = reinterpret_cast<const char*>(memchr(str, 0, avail));
1071 if (eos) {
1072 const size_t len = eos - str;
1073 mDataPos += PAD_SIZE(len+1);
Steve Block6807e592011-10-20 11:56:00 +01001074 ALOGV("readCString Setting data pos of %p to %d\n", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001075 return str;
1076 }
1077 }
1078 return NULL;
1079}
1080
1081String8 Parcel::readString8() const
1082{
1083 int32_t size = readInt32();
1084 // watch for potential int overflow adding 1 for trailing NUL
1085 if (size > 0 && size < INT32_MAX) {
1086 const char* str = (const char*)readInplace(size+1);
1087 if (str) return String8(str, size);
1088 }
1089 return String8();
1090}
1091
1092String16 Parcel::readString16() const
1093{
1094 size_t len;
1095 const char16_t* str = readString16Inplace(&len);
1096 if (str) return String16(str, len);
Steve Blocke6f43dd2012-01-06 19:20:56 +00001097 ALOGE("Reading a NULL string not supported here.");
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001098 return String16();
1099}
1100
1101const char16_t* Parcel::readString16Inplace(size_t* outLen) const
1102{
1103 int32_t size = readInt32();
1104 // watch for potential int overflow from size+1
1105 if (size >= 0 && size < INT32_MAX) {
1106 *outLen = size;
1107 const char16_t* str = (const char16_t*)readInplace((size+1)*sizeof(char16_t));
1108 if (str != NULL) {
1109 return str;
1110 }
1111 }
1112 *outLen = 0;
1113 return NULL;
1114}
1115
1116sp<IBinder> Parcel::readStrongBinder() const
1117{
1118 sp<IBinder> val;
1119 unflatten_binder(ProcessState::self(), *this, &val);
1120 return val;
1121}
1122
1123wp<IBinder> Parcel::readWeakBinder() const
1124{
1125 wp<IBinder> val;
1126 unflatten_binder(ProcessState::self(), *this, &val);
1127 return val;
1128}
1129
Brad Fitzpatrick837a0d02010-07-13 15:33:35 -07001130int32_t Parcel::readExceptionCode() const
1131{
1132 int32_t exception_code = readAligned<int32_t>();
Brad Fitzpatrickd36f4a52010-07-12 11:05:38 -07001133 if (exception_code == EX_HAS_REPLY_HEADER) {
Magnus Strandberg1ba24572011-05-03 15:44:00 +02001134 int32_t header_start = dataPosition();
Brad Fitzpatrickd36f4a52010-07-12 11:05:38 -07001135 int32_t header_size = readAligned<int32_t>();
1136 // Skip over fat responses headers. Not used (or propagated) in
1137 // native code
Magnus Strandberg1ba24572011-05-03 15:44:00 +02001138 setDataPosition(header_start + header_size);
Brad Fitzpatrickd36f4a52010-07-12 11:05:38 -07001139 // And fat response headers are currently only used when there are no
1140 // exceptions, so return no error:
1141 return 0;
1142 }
Brad Fitzpatrick837a0d02010-07-13 15:33:35 -07001143 return exception_code;
1144}
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001145
Mathias Agopiana47f02a2009-05-21 16:29:38 -07001146native_handle* Parcel::readNativeHandle() const
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001147{
1148 int numFds, numInts;
1149 status_t err;
1150 err = readInt32(&numFds);
1151 if (err != NO_ERROR) return 0;
1152 err = readInt32(&numInts);
1153 if (err != NO_ERROR) return 0;
1154
Mathias Agopiana47f02a2009-05-21 16:29:38 -07001155 native_handle* h = native_handle_create(numFds, numInts);
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001156 for (int i=0 ; err==NO_ERROR && i<numFds ; i++) {
Rebecca Schultz Zavin360211f2009-02-13 16:34:38 -08001157 h->data[i] = dup(readFileDescriptor());
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001158 if (h->data[i] < 0) err = BAD_VALUE;
1159 }
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001160 err = read(h->data + numFds, sizeof(int)*numInts);
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001161 if (err != NO_ERROR) {
Mathias Agopiana47f02a2009-05-21 16:29:38 -07001162 native_handle_close(h);
1163 native_handle_delete(h);
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001164 h = 0;
1165 }
1166 return h;
1167}
1168
1169
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001170int Parcel::readFileDescriptor() const
1171{
1172 const flat_binder_object* flat = readObject(true);
1173 if (flat) {
1174 switch (flat->type) {
1175 case BINDER_TYPE_FD:
Steve Blocka19954a2012-01-04 20:05:49 +00001176 //ALOGI("Returning file descriptor %ld from parcel %p\n", flat->handle, this);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001177 return flat->handle;
1178 }
1179 }
1180 return BAD_TYPE;
1181}
1182
Jeff Brown5707dbf2011-09-23 21:17:56 -07001183status_t Parcel::readBlob(size_t len, ReadableBlob* outBlob) const
1184{
1185 int32_t useAshmem;
1186 status_t status = readInt32(&useAshmem);
1187 if (status) return status;
1188
1189 if (!useAshmem) {
Steve Block6807e592011-10-20 11:56:00 +01001190 ALOGV("readBlob: read in place");
Jeff Brown5707dbf2011-09-23 21:17:56 -07001191 const void* ptr = readInplace(len);
1192 if (!ptr) return BAD_VALUE;
1193
1194 outBlob->init(false /*mapped*/, const_cast<void*>(ptr), len);
1195 return NO_ERROR;
1196 }
1197
Steve Block6807e592011-10-20 11:56:00 +01001198 ALOGV("readBlob: read from ashmem");
Jeff Brown5707dbf2011-09-23 21:17:56 -07001199 int fd = readFileDescriptor();
1200 if (fd == int(BAD_TYPE)) return BAD_VALUE;
1201
1202 void* ptr = ::mmap(NULL, len, PROT_READ, MAP_SHARED, fd, 0);
1203 if (!ptr) return NO_MEMORY;
1204
1205 outBlob->init(true /*mapped*/, ptr, len);
1206 return NO_ERROR;
1207}
1208
Mathias Agopiane1424282013-07-29 21:24:40 -07001209status_t Parcel::read(FlattenableHelperInterface& val) const
Mathias Agopian98e71dd2010-02-11 17:30:52 -08001210{
1211 // size
1212 const size_t len = this->readInt32();
1213 const size_t fd_count = this->readInt32();
1214
1215 // payload
Mathias Agopiane1424282013-07-29 21:24:40 -07001216 void const* const buf = this->readInplace(PAD_SIZE(len));
Mathias Agopian98e71dd2010-02-11 17:30:52 -08001217 if (buf == NULL)
1218 return BAD_VALUE;
1219
1220 int* fds = NULL;
1221 if (fd_count) {
1222 fds = new int[fd_count];
1223 }
1224
1225 status_t err = NO_ERROR;
1226 for (size_t i=0 ; i<fd_count && err==NO_ERROR ; i++) {
1227 fds[i] = dup(this->readFileDescriptor());
1228 if (fds[i] < 0) err = BAD_VALUE;
1229 }
1230
1231 if (err == NO_ERROR) {
1232 err = val.unflatten(buf, len, fds, fd_count);
1233 }
1234
1235 if (fd_count) {
1236 delete [] fds;
1237 }
1238
1239 return err;
1240}
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001241const flat_binder_object* Parcel::readObject(bool nullMetaData) const
1242{
1243 const size_t DPOS = mDataPos;
1244 if ((DPOS+sizeof(flat_binder_object)) <= mDataSize) {
1245 const flat_binder_object* obj
1246 = reinterpret_cast<const flat_binder_object*>(mData+DPOS);
1247 mDataPos = DPOS + sizeof(flat_binder_object);
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001248 if (!nullMetaData && (obj->cookie == 0 && obj->binder == 0)) {
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001249 // When transferring a NULL object, we don't write it into
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001250 // the object list, so we don't want to check for it when
1251 // reading.
Steve Block6807e592011-10-20 11:56:00 +01001252 ALOGV("readObject Setting data pos of %p to %d\n", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001253 return obj;
1254 }
1255
1256 // Ensure that this object is valid...
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001257 binder_size_t* const OBJS = mObjects;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001258 const size_t N = mObjectsSize;
1259 size_t opos = mNextObjectHint;
1260
1261 if (N > 0) {
Steve Block6807e592011-10-20 11:56:00 +01001262 ALOGV("Parcel %p looking for obj at %d, hint=%d\n",
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001263 this, DPOS, opos);
1264
1265 // Start at the current hint position, looking for an object at
1266 // the current data position.
1267 if (opos < N) {
1268 while (opos < (N-1) && OBJS[opos] < DPOS) {
1269 opos++;
1270 }
1271 } else {
1272 opos = N-1;
1273 }
1274 if (OBJS[opos] == DPOS) {
1275 // Found it!
Steve Block6807e592011-10-20 11:56:00 +01001276 ALOGV("Parcel found obj %d at index %d with forward search",
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001277 this, DPOS, opos);
1278 mNextObjectHint = opos+1;
Steve Block6807e592011-10-20 11:56:00 +01001279 ALOGV("readObject Setting data pos of %p to %d\n", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001280 return obj;
1281 }
1282
1283 // Look backwards for it...
1284 while (opos > 0 && OBJS[opos] > DPOS) {
1285 opos--;
1286 }
1287 if (OBJS[opos] == DPOS) {
1288 // Found it!
Steve Block6807e592011-10-20 11:56:00 +01001289 ALOGV("Parcel found obj %d at index %d with backward search",
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001290 this, DPOS, opos);
1291 mNextObjectHint = opos+1;
Steve Block6807e592011-10-20 11:56:00 +01001292 ALOGV("readObject Setting data pos of %p to %d\n", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001293 return obj;
1294 }
1295 }
Colin Cross6f4f3ab2014-02-05 17:42:44 -08001296 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 -07001297 this, DPOS);
1298 }
1299 return NULL;
1300}
1301
1302void Parcel::closeFileDescriptors()
1303{
1304 size_t i = mObjectsSize;
1305 if (i > 0) {
Steve Blocka19954a2012-01-04 20:05:49 +00001306 //ALOGI("Closing file descriptors for %d objects...", mObjectsSize);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001307 }
1308 while (i > 0) {
1309 i--;
1310 const flat_binder_object* flat
1311 = reinterpret_cast<flat_binder_object*>(mData+mObjects[i]);
1312 if (flat->type == BINDER_TYPE_FD) {
Steve Blocka19954a2012-01-04 20:05:49 +00001313 //ALOGI("Closing fd: %ld\n", flat->handle);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001314 close(flat->handle);
1315 }
1316 }
1317}
1318
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001319uintptr_t Parcel::ipcData() const
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001320{
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001321 return reinterpret_cast<uintptr_t>(mData);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001322}
1323
1324size_t Parcel::ipcDataSize() const
1325{
1326 return (mDataSize > mDataPos ? mDataSize : mDataPos);
1327}
1328
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001329uintptr_t Parcel::ipcObjects() const
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001330{
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001331 return reinterpret_cast<uintptr_t>(mObjects);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001332}
1333
1334size_t Parcel::ipcObjectsCount() const
1335{
1336 return mObjectsSize;
1337}
1338
1339void Parcel::ipcSetDataReference(const uint8_t* data, size_t dataSize,
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001340 const binder_size_t* objects, size_t objectsCount, release_func relFunc, void* relCookie)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001341{
1342 freeDataNoInit();
1343 mError = NO_ERROR;
1344 mData = const_cast<uint8_t*>(data);
1345 mDataSize = mDataCapacity = dataSize;
Steve Blocka19954a2012-01-04 20:05:49 +00001346 //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 -07001347 mDataPos = 0;
Steve Block6807e592011-10-20 11:56:00 +01001348 ALOGV("setDataReference Setting data pos of %p to %d\n", this, mDataPos);
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001349 mObjects = const_cast<binder_size_t*>(objects);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001350 mObjectsSize = mObjectsCapacity = objectsCount;
1351 mNextObjectHint = 0;
1352 mOwner = relFunc;
1353 mOwnerCookie = relCookie;
1354 scanForFds();
1355}
1356
Colin Cross6f4f3ab2014-02-05 17:42:44 -08001357void Parcel::print(TextOutput& to, uint32_t /*flags*/) const
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001358{
1359 to << "Parcel(";
1360
1361 if (errorCheck() != NO_ERROR) {
1362 const status_t err = errorCheck();
Colin Cross6f4f3ab2014-02-05 17:42:44 -08001363 to << "Error: " << (void*)(intptr_t)err << " \"" << strerror(-err) << "\"";
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001364 } else if (dataSize() > 0) {
1365 const uint8_t* DATA = data();
1366 to << indent << HexDump(DATA, dataSize()) << dedent;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001367 const binder_size_t* OBJS = objects();
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001368 const size_t N = objectsCount();
1369 for (size_t i=0; i<N; i++) {
1370 const flat_binder_object* flat
1371 = reinterpret_cast<const flat_binder_object*>(DATA+OBJS[i]);
1372 to << endl << "Object #" << i << " @ " << (void*)OBJS[i] << ": "
1373 << TypeCode(flat->type & 0x7f7f7f00)
1374 << " = " << flat->binder;
1375 }
1376 } else {
1377 to << "NULL";
1378 }
1379
1380 to << ")";
1381}
1382
1383void Parcel::releaseObjects()
1384{
1385 const sp<ProcessState> proc(ProcessState::self());
1386 size_t i = mObjectsSize;
1387 uint8_t* const data = mData;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001388 binder_size_t* const objects = mObjects;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001389 while (i > 0) {
1390 i--;
1391 const flat_binder_object* flat
1392 = reinterpret_cast<flat_binder_object*>(data+objects[i]);
1393 release_object(proc, *flat, this);
1394 }
1395}
1396
1397void Parcel::acquireObjects()
1398{
1399 const sp<ProcessState> proc(ProcessState::self());
1400 size_t i = mObjectsSize;
1401 uint8_t* const data = mData;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001402 binder_size_t* const objects = mObjects;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001403 while (i > 0) {
1404 i--;
1405 const flat_binder_object* flat
1406 = reinterpret_cast<flat_binder_object*>(data+objects[i]);
1407 acquire_object(proc, *flat, this);
1408 }
1409}
1410
1411void Parcel::freeData()
1412{
1413 freeDataNoInit();
1414 initState();
1415}
1416
1417void Parcel::freeDataNoInit()
1418{
1419 if (mOwner) {
Steve Blocka19954a2012-01-04 20:05:49 +00001420 //ALOGI("Freeing data ref of %p (pid=%d)\n", this, getpid());
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001421 mOwner(this, mData, mDataSize, mObjects, mObjectsSize, mOwnerCookie);
1422 } else {
1423 releaseObjects();
1424 if (mData) free(mData);
1425 if (mObjects) free(mObjects);
1426 }
1427}
1428
1429status_t Parcel::growData(size_t len)
1430{
1431 size_t newSize = ((mDataSize+len)*3)/2;
1432 return (newSize <= mDataSize)
1433 ? (status_t) NO_MEMORY
1434 : continueWrite(newSize);
1435}
1436
1437status_t Parcel::restartWrite(size_t desired)
1438{
1439 if (mOwner) {
1440 freeData();
1441 return continueWrite(desired);
1442 }
1443
1444 uint8_t* data = (uint8_t*)realloc(mData, desired);
1445 if (!data && desired > mDataCapacity) {
1446 mError = NO_MEMORY;
1447 return NO_MEMORY;
1448 }
1449
1450 releaseObjects();
1451
1452 if (data) {
1453 mData = data;
1454 mDataCapacity = desired;
1455 }
1456
1457 mDataSize = mDataPos = 0;
Steve Block6807e592011-10-20 11:56:00 +01001458 ALOGV("restartWrite Setting data size of %p to %d\n", this, mDataSize);
1459 ALOGV("restartWrite Setting data pos of %p to %d\n", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001460
1461 free(mObjects);
1462 mObjects = NULL;
1463 mObjectsSize = mObjectsCapacity = 0;
1464 mNextObjectHint = 0;
1465 mHasFds = false;
1466 mFdsKnown = true;
Dianne Hackborn8938ed22011-09-28 23:19:47 -04001467 mAllowFds = true;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001468
1469 return NO_ERROR;
1470}
1471
1472status_t Parcel::continueWrite(size_t desired)
1473{
1474 // If shrinking, first adjust for any objects that appear
1475 // after the new data size.
1476 size_t objectsSize = mObjectsSize;
1477 if (desired < mDataSize) {
1478 if (desired == 0) {
1479 objectsSize = 0;
1480 } else {
1481 while (objectsSize > 0) {
1482 if (mObjects[objectsSize-1] < desired)
1483 break;
1484 objectsSize--;
1485 }
1486 }
1487 }
1488
1489 if (mOwner) {
1490 // If the size is going to zero, just release the owner's data.
1491 if (desired == 0) {
1492 freeData();
1493 return NO_ERROR;
1494 }
1495
1496 // If there is a different owner, we need to take
1497 // posession.
1498 uint8_t* data = (uint8_t*)malloc(desired);
1499 if (!data) {
1500 mError = NO_MEMORY;
1501 return NO_MEMORY;
1502 }
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001503 binder_size_t* objects = NULL;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001504
1505 if (objectsSize) {
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001506 objects = (binder_size_t*)malloc(objectsSize*sizeof(binder_size_t));
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001507 if (!objects) {
Hyejin Kim3f727c02013-03-09 11:28:54 +09001508 free(data);
1509
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001510 mError = NO_MEMORY;
1511 return NO_MEMORY;
1512 }
1513
1514 // Little hack to only acquire references on objects
1515 // we will be keeping.
1516 size_t oldObjectsSize = mObjectsSize;
1517 mObjectsSize = objectsSize;
1518 acquireObjects();
1519 mObjectsSize = oldObjectsSize;
1520 }
1521
1522 if (mData) {
1523 memcpy(data, mData, mDataSize < desired ? mDataSize : desired);
1524 }
1525 if (objects && mObjects) {
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001526 memcpy(objects, mObjects, objectsSize*sizeof(binder_size_t));
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001527 }
Steve Blocka19954a2012-01-04 20:05:49 +00001528 //ALOGI("Freeing data ref of %p (pid=%d)\n", this, getpid());
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001529 mOwner(this, mData, mDataSize, mObjects, mObjectsSize, mOwnerCookie);
1530 mOwner = NULL;
1531
1532 mData = data;
1533 mObjects = objects;
1534 mDataSize = (mDataSize < desired) ? mDataSize : desired;
Steve Block6807e592011-10-20 11:56:00 +01001535 ALOGV("continueWrite Setting data size of %p to %d\n", this, mDataSize);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001536 mDataCapacity = desired;
1537 mObjectsSize = mObjectsCapacity = objectsSize;
1538 mNextObjectHint = 0;
1539
1540 } else if (mData) {
1541 if (objectsSize < mObjectsSize) {
1542 // Need to release refs on any objects we are dropping.
1543 const sp<ProcessState> proc(ProcessState::self());
1544 for (size_t i=objectsSize; i<mObjectsSize; i++) {
1545 const flat_binder_object* flat
1546 = reinterpret_cast<flat_binder_object*>(mData+mObjects[i]);
1547 if (flat->type == BINDER_TYPE_FD) {
1548 // will need to rescan because we may have lopped off the only FDs
1549 mFdsKnown = false;
1550 }
1551 release_object(proc, *flat, this);
1552 }
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001553 binder_size_t* objects =
1554 (binder_size_t*)realloc(mObjects, objectsSize*sizeof(binder_size_t));
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001555 if (objects) {
1556 mObjects = objects;
1557 }
1558 mObjectsSize = objectsSize;
1559 mNextObjectHint = 0;
1560 }
1561
1562 // We own the data, so we can just do a realloc().
1563 if (desired > mDataCapacity) {
1564 uint8_t* data = (uint8_t*)realloc(mData, desired);
1565 if (data) {
1566 mData = data;
1567 mDataCapacity = desired;
1568 } else if (desired > mDataCapacity) {
1569 mError = NO_MEMORY;
1570 return NO_MEMORY;
1571 }
1572 } else {
Dianne Hackborn97e2bcd2011-04-13 18:15:56 -07001573 if (mDataSize > desired) {
1574 mDataSize = desired;
Steve Block6807e592011-10-20 11:56:00 +01001575 ALOGV("continueWrite Setting data size of %p to %d\n", this, mDataSize);
Dianne Hackborn97e2bcd2011-04-13 18:15:56 -07001576 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001577 if (mDataPos > desired) {
1578 mDataPos = desired;
Steve Block6807e592011-10-20 11:56:00 +01001579 ALOGV("continueWrite Setting data pos of %p to %d\n", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001580 }
1581 }
1582
1583 } else {
1584 // This is the first data. Easy!
1585 uint8_t* data = (uint8_t*)malloc(desired);
1586 if (!data) {
1587 mError = NO_MEMORY;
1588 return NO_MEMORY;
1589 }
Hyejin Kim3f727c02013-03-09 11:28:54 +09001590
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001591 if(!(mDataCapacity == 0 && mObjects == NULL
1592 && mObjectsCapacity == 0)) {
Colin Cross6f4f3ab2014-02-05 17:42:44 -08001593 ALOGE("continueWrite: %zu/%p/%zu/%zu", mDataCapacity, mObjects, mObjectsCapacity, desired);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001594 }
1595
1596 mData = data;
1597 mDataSize = mDataPos = 0;
Steve Block6807e592011-10-20 11:56:00 +01001598 ALOGV("continueWrite Setting data size of %p to %d\n", this, mDataSize);
1599 ALOGV("continueWrite Setting data pos of %p to %d\n", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001600 mDataCapacity = desired;
1601 }
1602
1603 return NO_ERROR;
1604}
1605
1606void Parcel::initState()
1607{
1608 mError = NO_ERROR;
1609 mData = 0;
1610 mDataSize = 0;
1611 mDataCapacity = 0;
1612 mDataPos = 0;
Steve Block6807e592011-10-20 11:56:00 +01001613 ALOGV("initState Setting data size of %p to %d\n", this, mDataSize);
1614 ALOGV("initState Setting data pos of %p to %d\n", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001615 mObjects = NULL;
1616 mObjectsSize = 0;
1617 mObjectsCapacity = 0;
1618 mNextObjectHint = 0;
1619 mHasFds = false;
1620 mFdsKnown = true;
Dianne Hackborn8938ed22011-09-28 23:19:47 -04001621 mAllowFds = true;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001622 mOwner = NULL;
1623}
1624
1625void Parcel::scanForFds() const
1626{
1627 bool hasFds = false;
1628 for (size_t i=0; i<mObjectsSize; i++) {
1629 const flat_binder_object* flat
1630 = reinterpret_cast<const flat_binder_object*>(mData + mObjects[i]);
1631 if (flat->type == BINDER_TYPE_FD) {
1632 hasFds = true;
1633 break;
1634 }
1635 }
1636 mHasFds = hasFds;
1637 mFdsKnown = true;
1638}
1639
Jeff Brown5707dbf2011-09-23 21:17:56 -07001640// --- Parcel::Blob ---
1641
1642Parcel::Blob::Blob() :
1643 mMapped(false), mData(NULL), mSize(0) {
1644}
1645
1646Parcel::Blob::~Blob() {
1647 release();
1648}
1649
1650void Parcel::Blob::release() {
1651 if (mMapped && mData) {
1652 ::munmap(mData, mSize);
1653 }
1654 clear();
1655}
1656
1657void Parcel::Blob::init(bool mapped, void* data, size_t size) {
1658 mMapped = mapped;
1659 mData = data;
1660 mSize = size;
1661}
1662
1663void Parcel::Blob::clear() {
1664 mMapped = false;
1665 mData = NULL;
1666 mSize = 0;
1667}
1668
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001669}; // namespace android