blob: 8315c58843eb5711ff11f825771c44b4eb1a25e3 [file] [log] [blame]
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001/*
2 * Copyright (C) 2005 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#define LOG_TAG "Parcel"
18//#define LOG_NDEBUG 0
19
Mathias Agopianc5b2c0b2009-05-19 19:08:10 -070020#include <binder/Parcel.h>
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -070021
Brad Fitzpatrick702ea9d2010-06-18 13:07:53 -070022#include <binder/IPCThreadState.h>
Mathias Agopianc5b2c0b2009-05-19 19:08:10 -070023#include <binder/Binder.h>
24#include <binder/BpBinder.h>
Mathias Agopianc5b2c0b2009-05-19 19:08:10 -070025#include <binder/ProcessState.h>
Mathias Agopian002e1e52013-05-06 20:20:50 -070026#include <binder/TextOutput.h>
27
Jun Jiangabf8a2c2014-04-29 14:22:10 +080028#include <errno.h>
Mathias Agopian002e1e52013-05-06 20:20:50 -070029#include <utils/Debug.h>
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -070030#include <utils/Log.h>
31#include <utils/String8.h>
32#include <utils/String16.h>
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -070033#include <utils/misc.h>
Mathias Agopian98e71dd2010-02-11 17:30:52 -080034#include <utils/Flattenable.h>
Jeff Brown5707dbf2011-09-23 21:17:56 -070035#include <cutils/ashmem.h>
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -070036
Mathias Agopian208059f2009-05-18 15:08:03 -070037#include <private/binder/binder_module.h>
Dianne Hackborn7e790af2014-11-11 12:22:53 -080038#include <private/binder/Static.h>
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -070039
Arve Hjønnevågf50b9ea2014-02-13 19:22:08 -080040#include <inttypes.h>
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -070041#include <stdio.h>
42#include <stdlib.h>
43#include <stdint.h>
Jeff Brown5707dbf2011-09-23 21:17:56 -070044#include <sys/mman.h>
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -070045
46#ifndef INT32_MAX
47#define INT32_MAX ((int32_t)(2147483647))
48#endif
49
50#define LOG_REFS(...)
Steve Block9f760152011-10-12 17:27:03 +010051//#define LOG_REFS(...) ALOG(LOG_DEBUG, "Parcel", __VA_ARGS__)
Dianne Hackborn7e790af2014-11-11 12:22:53 -080052#define LOG_ALLOC(...)
53//#define LOG_ALLOC(...) ALOG(LOG_DEBUG, "Parcel", __VA_ARGS__)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -070054
55// ---------------------------------------------------------------------------
56
Nick Kralevichb6b14232015-04-02 09:36:02 -070057// This macro should never be used at runtime, as a too large value
58// of s could cause an integer overflow. Instead, you should always
59// use the wrapper function pad_size()
60#define PAD_SIZE_UNSAFE(s) (((s)+3)&~3)
61
62static size_t pad_size(size_t s) {
63 if (s > (SIZE_T_MAX - 3)) {
64 abort();
65 }
66 return PAD_SIZE_UNSAFE(s);
67}
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -070068
Brad Fitzpatricka877cd82010-07-07 16:06:39 -070069// Note: must be kept in sync with android/os/StrictMode.java's PENALTY_GATHER
Jeff Sharkey0c1f5cb2014-12-18 10:26:57 -080070#define STRICT_MODE_PENALTY_GATHER (0x40 << 16)
Brad Fitzpatricka877cd82010-07-07 16:06:39 -070071
Brad Fitzpatrickd36f4a52010-07-12 11:05:38 -070072// Note: must be kept in sync with android/os/Parcel.java's EX_HAS_REPLY_HEADER
73#define EX_HAS_REPLY_HEADER -128
74
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -070075// XXX This can be made public if we want to provide
76// support for typed data.
77struct small_flat_data
78{
79 uint32_t type;
80 uint32_t data;
81};
82
83namespace android {
84
Dianne Hackborna4cff882014-11-13 17:07:40 -080085static pthread_mutex_t gParcelGlobalAllocSizeLock = PTHREAD_MUTEX_INITIALIZER;
86static size_t gParcelGlobalAllocSize = 0;
87static size_t gParcelGlobalAllocCount = 0;
88
Jeff Brown13b16042014-11-11 16:44:25 -080089// Maximum size of a blob to transfer in-place.
90static const size_t BLOB_INPLACE_LIMIT = 16 * 1024;
91
92enum {
93 BLOB_INPLACE = 0,
94 BLOB_ASHMEM_IMMUTABLE = 1,
95 BLOB_ASHMEM_MUTABLE = 2,
96};
97
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -070098void acquire_object(const sp<ProcessState>& proc,
99 const flat_binder_object& obj, const void* who)
100{
101 switch (obj.type) {
102 case BINDER_TYPE_BINDER:
103 if (obj.binder) {
104 LOG_REFS("Parcel %p acquiring reference on local %p", who, obj.cookie);
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800105 reinterpret_cast<IBinder*>(obj.cookie)->incStrong(who);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700106 }
107 return;
108 case BINDER_TYPE_WEAK_BINDER:
109 if (obj.binder)
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800110 reinterpret_cast<RefBase::weakref_type*>(obj.binder)->incWeak(who);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700111 return;
112 case BINDER_TYPE_HANDLE: {
113 const sp<IBinder> b = proc->getStrongProxyForHandle(obj.handle);
114 if (b != NULL) {
115 LOG_REFS("Parcel %p acquiring reference on remote %p", who, b.get());
116 b->incStrong(who);
117 }
118 return;
119 }
120 case BINDER_TYPE_WEAK_HANDLE: {
121 const wp<IBinder> b = proc->getWeakProxyForHandle(obj.handle);
122 if (b != NULL) b.get_refs()->incWeak(who);
123 return;
124 }
125 case BINDER_TYPE_FD: {
126 // intentionally blank -- nothing to do to acquire this, but we do
127 // recognize it as a legitimate object type.
128 return;
129 }
130 }
131
Colin Cross6f4f3ab2014-02-05 17:42:44 -0800132 ALOGD("Invalid object type 0x%08x", obj.type);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700133}
134
135void release_object(const sp<ProcessState>& proc,
136 const flat_binder_object& obj, const void* who)
137{
138 switch (obj.type) {
139 case BINDER_TYPE_BINDER:
140 if (obj.binder) {
141 LOG_REFS("Parcel %p releasing reference on local %p", who, obj.cookie);
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800142 reinterpret_cast<IBinder*>(obj.cookie)->decStrong(who);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700143 }
144 return;
145 case BINDER_TYPE_WEAK_BINDER:
146 if (obj.binder)
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800147 reinterpret_cast<RefBase::weakref_type*>(obj.binder)->decWeak(who);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700148 return;
149 case BINDER_TYPE_HANDLE: {
150 const sp<IBinder> b = proc->getStrongProxyForHandle(obj.handle);
151 if (b != NULL) {
152 LOG_REFS("Parcel %p releasing reference on remote %p", who, b.get());
153 b->decStrong(who);
154 }
155 return;
156 }
157 case BINDER_TYPE_WEAK_HANDLE: {
158 const wp<IBinder> b = proc->getWeakProxyForHandle(obj.handle);
159 if (b != NULL) b.get_refs()->decWeak(who);
160 return;
161 }
162 case BINDER_TYPE_FD: {
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800163 if (obj.cookie != 0) close(obj.handle);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700164 return;
165 }
166 }
167
Colin Cross6f4f3ab2014-02-05 17:42:44 -0800168 ALOGE("Invalid object type 0x%08x", obj.type);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700169}
170
171inline static status_t finish_flatten_binder(
Colin Cross6f4f3ab2014-02-05 17:42:44 -0800172 const sp<IBinder>& /*binder*/, const flat_binder_object& flat, Parcel* out)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700173{
174 return out->writeObject(flat, false);
175}
176
Colin Cross6f4f3ab2014-02-05 17:42:44 -0800177status_t flatten_binder(const sp<ProcessState>& /*proc*/,
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700178 const sp<IBinder>& binder, Parcel* out)
179{
180 flat_binder_object obj;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700181
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700182 obj.flags = 0x7f | FLAT_BINDER_FLAG_ACCEPTS_FDS;
183 if (binder != NULL) {
184 IBinder *local = binder->localBinder();
185 if (!local) {
186 BpBinder *proxy = binder->remoteBinder();
187 if (proxy == NULL) {
Steve Blocke6f43dd2012-01-06 19:20:56 +0000188 ALOGE("null proxy");
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700189 }
190 const int32_t handle = proxy ? proxy->handle() : 0;
191 obj.type = BINDER_TYPE_HANDLE;
Arve Hjønnevåg07fd0f12014-02-18 21:10:29 -0800192 obj.binder = 0; /* Don't pass uninitialized stack data to a remote process */
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700193 obj.handle = handle;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800194 obj.cookie = 0;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700195 } else {
196 obj.type = BINDER_TYPE_BINDER;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800197 obj.binder = reinterpret_cast<uintptr_t>(local->getWeakRefs());
198 obj.cookie = reinterpret_cast<uintptr_t>(local);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700199 }
200 } else {
201 obj.type = BINDER_TYPE_BINDER;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800202 obj.binder = 0;
203 obj.cookie = 0;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700204 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700205
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700206 return finish_flatten_binder(binder, obj, out);
207}
208
Colin Cross6f4f3ab2014-02-05 17:42:44 -0800209status_t flatten_binder(const sp<ProcessState>& /*proc*/,
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700210 const wp<IBinder>& binder, Parcel* out)
211{
212 flat_binder_object obj;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700213
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700214 obj.flags = 0x7f | FLAT_BINDER_FLAG_ACCEPTS_FDS;
215 if (binder != NULL) {
216 sp<IBinder> real = binder.promote();
217 if (real != NULL) {
218 IBinder *local = real->localBinder();
219 if (!local) {
220 BpBinder *proxy = real->remoteBinder();
221 if (proxy == NULL) {
Steve Blocke6f43dd2012-01-06 19:20:56 +0000222 ALOGE("null proxy");
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700223 }
224 const int32_t handle = proxy ? proxy->handle() : 0;
225 obj.type = BINDER_TYPE_WEAK_HANDLE;
Arve Hjønnevåg07fd0f12014-02-18 21:10:29 -0800226 obj.binder = 0; /* Don't pass uninitialized stack data to a remote process */
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700227 obj.handle = handle;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800228 obj.cookie = 0;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700229 } else {
230 obj.type = BINDER_TYPE_WEAK_BINDER;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800231 obj.binder = reinterpret_cast<uintptr_t>(binder.get_refs());
232 obj.cookie = reinterpret_cast<uintptr_t>(binder.unsafe_get());
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700233 }
234 return finish_flatten_binder(real, obj, out);
235 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700236
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700237 // XXX How to deal? In order to flatten the given binder,
238 // we need to probe it for information, which requires a primary
239 // reference... but we don't have one.
240 //
241 // The OpenBinder implementation uses a dynamic_cast<> here,
242 // but we can't do that with the different reference counting
243 // implementation we are using.
Steve Blocke6f43dd2012-01-06 19:20:56 +0000244 ALOGE("Unable to unflatten Binder weak reference!");
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700245 obj.type = BINDER_TYPE_BINDER;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800246 obj.binder = 0;
247 obj.cookie = 0;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700248 return finish_flatten_binder(NULL, obj, out);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700249
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700250 } else {
251 obj.type = BINDER_TYPE_BINDER;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800252 obj.binder = 0;
253 obj.cookie = 0;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700254 return finish_flatten_binder(NULL, obj, out);
255 }
256}
257
258inline static status_t finish_unflatten_binder(
Colin Cross6f4f3ab2014-02-05 17:42:44 -0800259 BpBinder* /*proxy*/, const flat_binder_object& /*flat*/,
260 const Parcel& /*in*/)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700261{
262 return NO_ERROR;
263}
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700264
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700265status_t unflatten_binder(const sp<ProcessState>& proc,
266 const Parcel& in, sp<IBinder>* out)
267{
268 const flat_binder_object* flat = in.readObject(false);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700269
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700270 if (flat) {
271 switch (flat->type) {
272 case BINDER_TYPE_BINDER:
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800273 *out = reinterpret_cast<IBinder*>(flat->cookie);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700274 return finish_unflatten_binder(NULL, *flat, in);
275 case BINDER_TYPE_HANDLE:
276 *out = proc->getStrongProxyForHandle(flat->handle);
277 return finish_unflatten_binder(
278 static_cast<BpBinder*>(out->get()), *flat, in);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700279 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700280 }
281 return BAD_TYPE;
282}
283
284status_t unflatten_binder(const sp<ProcessState>& proc,
285 const Parcel& in, wp<IBinder>* out)
286{
287 const flat_binder_object* flat = in.readObject(false);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700288
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700289 if (flat) {
290 switch (flat->type) {
291 case BINDER_TYPE_BINDER:
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800292 *out = reinterpret_cast<IBinder*>(flat->cookie);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700293 return finish_unflatten_binder(NULL, *flat, in);
294 case BINDER_TYPE_WEAK_BINDER:
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800295 if (flat->binder != 0) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700296 out->set_object_and_refs(
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800297 reinterpret_cast<IBinder*>(flat->cookie),
298 reinterpret_cast<RefBase::weakref_type*>(flat->binder));
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700299 } else {
300 *out = NULL;
301 }
302 return finish_unflatten_binder(NULL, *flat, in);
303 case BINDER_TYPE_HANDLE:
304 case BINDER_TYPE_WEAK_HANDLE:
305 *out = proc->getWeakProxyForHandle(flat->handle);
306 return finish_unflatten_binder(
307 static_cast<BpBinder*>(out->unsafe_get()), *flat, in);
308 }
309 }
310 return BAD_TYPE;
311}
312
313// ---------------------------------------------------------------------------
314
315Parcel::Parcel()
316{
Dianne Hackborn7e790af2014-11-11 12:22:53 -0800317 LOG_ALLOC("Parcel %p: constructing", this);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700318 initState();
319}
320
321Parcel::~Parcel()
322{
323 freeDataNoInit();
Dianne Hackborn7e790af2014-11-11 12:22:53 -0800324 LOG_ALLOC("Parcel %p: destroyed", this);
325}
326
327size_t Parcel::getGlobalAllocSize() {
Dianne Hackborna4cff882014-11-13 17:07:40 -0800328 pthread_mutex_lock(&gParcelGlobalAllocSizeLock);
329 size_t size = gParcelGlobalAllocSize;
330 pthread_mutex_unlock(&gParcelGlobalAllocSizeLock);
331 return size;
Dianne Hackborn7e790af2014-11-11 12:22:53 -0800332}
333
334size_t Parcel::getGlobalAllocCount() {
Dianne Hackborna4cff882014-11-13 17:07:40 -0800335 pthread_mutex_lock(&gParcelGlobalAllocSizeLock);
336 size_t count = gParcelGlobalAllocCount;
337 pthread_mutex_unlock(&gParcelGlobalAllocSizeLock);
338 return count;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700339}
340
341const uint8_t* Parcel::data() const
342{
343 return mData;
344}
345
346size_t Parcel::dataSize() const
347{
348 return (mDataSize > mDataPos ? mDataSize : mDataPos);
349}
350
351size_t Parcel::dataAvail() const
352{
Nick Kralevich3f6b7022015-09-16 09:49:15 -0700353 size_t result = dataSize() - dataPosition();
354 if (result > INT32_MAX) {
355 abort();
356 }
357 return result;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700358}
359
360size_t Parcel::dataPosition() const
361{
362 return mDataPos;
363}
364
365size_t Parcel::dataCapacity() const
366{
367 return mDataCapacity;
368}
369
370status_t Parcel::setDataSize(size_t size)
371{
Nick Kralevichb6b14232015-04-02 09:36:02 -0700372 if (size > INT32_MAX) {
373 // don't accept size_t values which may have come from an
374 // inadvertent conversion from a negative int.
375 return BAD_VALUE;
376 }
377
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700378 status_t err;
379 err = continueWrite(size);
380 if (err == NO_ERROR) {
381 mDataSize = size;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700382 ALOGV("setDataSize Setting data size of %p to %zu", this, mDataSize);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700383 }
384 return err;
385}
386
387void Parcel::setDataPosition(size_t pos) const
388{
Nick Kralevichb6b14232015-04-02 09:36:02 -0700389 if (pos > INT32_MAX) {
390 // don't accept size_t values which may have come from an
391 // inadvertent conversion from a negative int.
392 abort();
393 }
394
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700395 mDataPos = pos;
396 mNextObjectHint = 0;
397}
398
399status_t Parcel::setDataCapacity(size_t size)
400{
Nick Kralevichb6b14232015-04-02 09:36:02 -0700401 if (size > INT32_MAX) {
402 // don't accept size_t values which may have come from an
403 // inadvertent conversion from a negative int.
404 return BAD_VALUE;
405 }
406
Dianne Hackborn97e2bcd2011-04-13 18:15:56 -0700407 if (size > mDataCapacity) return continueWrite(size);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700408 return NO_ERROR;
409}
410
411status_t Parcel::setData(const uint8_t* buffer, size_t len)
412{
Nick Kralevichb6b14232015-04-02 09:36:02 -0700413 if (len > INT32_MAX) {
414 // don't accept size_t values which may have come from an
415 // inadvertent conversion from a negative int.
416 return BAD_VALUE;
417 }
418
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700419 status_t err = restartWrite(len);
420 if (err == NO_ERROR) {
421 memcpy(const_cast<uint8_t*>(data()), buffer, len);
422 mDataSize = len;
423 mFdsKnown = false;
424 }
425 return err;
426}
427
Andreas Huber51faf462011-04-13 10:21:56 -0700428status_t Parcel::appendFrom(const Parcel *parcel, size_t offset, size_t len)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700429{
430 const sp<ProcessState> proc(ProcessState::self());
431 status_t err;
Andreas Huber51faf462011-04-13 10:21:56 -0700432 const uint8_t *data = parcel->mData;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800433 const binder_size_t *objects = parcel->mObjects;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700434 size_t size = parcel->mObjectsSize;
435 int startPos = mDataPos;
436 int firstIndex = -1, lastIndex = -2;
437
438 if (len == 0) {
439 return NO_ERROR;
440 }
441
Nick Kralevichb6b14232015-04-02 09:36:02 -0700442 if (len > INT32_MAX) {
443 // don't accept size_t values which may have come from an
444 // inadvertent conversion from a negative int.
445 return BAD_VALUE;
446 }
447
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700448 // range checks against the source parcel size
449 if ((offset > parcel->mDataSize)
450 || (len > parcel->mDataSize)
451 || (offset + len > parcel->mDataSize)) {
452 return BAD_VALUE;
453 }
454
455 // Count objects in range
456 for (int i = 0; i < (int) size; i++) {
457 size_t off = objects[i];
Christopher Tate27182be2015-05-27 17:53:02 -0700458 if ((off >= offset) && (off + sizeof(flat_binder_object) <= offset + len)) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700459 if (firstIndex == -1) {
460 firstIndex = i;
461 }
462 lastIndex = i;
463 }
464 }
465 int numObjects = lastIndex - firstIndex + 1;
466
Dianne Hackborn97e2bcd2011-04-13 18:15:56 -0700467 if ((mDataSize+len) > mDataCapacity) {
468 // grow data
469 err = growData(len);
470 if (err != NO_ERROR) {
471 return err;
472 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700473 }
474
475 // append data
476 memcpy(mData + mDataPos, data + offset, len);
477 mDataPos += len;
478 mDataSize += len;
479
Dianne Hackborn8938ed22011-09-28 23:19:47 -0400480 err = NO_ERROR;
481
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700482 if (numObjects > 0) {
483 // grow objects
484 if (mObjectsCapacity < mObjectsSize + numObjects) {
Christopher Tateed7a50c2015-06-08 14:45:14 -0700485 size_t newSize = ((mObjectsSize + numObjects)*3)/2;
486 if (newSize < mObjectsSize) return NO_MEMORY; // overflow
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800487 binder_size_t *objects =
488 (binder_size_t*)realloc(mObjects, newSize*sizeof(binder_size_t));
489 if (objects == (binder_size_t*)0) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700490 return NO_MEMORY;
491 }
492 mObjects = objects;
493 mObjectsCapacity = newSize;
494 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700495
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700496 // append and acquire objects
497 int idx = mObjectsSize;
498 for (int i = firstIndex; i <= lastIndex; i++) {
499 size_t off = objects[i] - offset + startPos;
500 mObjects[idx++] = off;
501 mObjectsSize++;
502
Dianne Hackborn8af0f822009-05-22 13:20:23 -0700503 flat_binder_object* flat
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700504 = reinterpret_cast<flat_binder_object*>(mData + off);
505 acquire_object(proc, *flat, this);
506
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700507 if (flat->type == BINDER_TYPE_FD) {
Dianne Hackborn8af0f822009-05-22 13:20:23 -0700508 // If this is a file descriptor, we need to dup it so the
509 // new Parcel now owns its own fd, and can declare that we
510 // officially know we have fds.
511 flat->handle = dup(flat->handle);
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800512 flat->cookie = 1;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700513 mHasFds = mFdsKnown = true;
Dianne Hackborn8938ed22011-09-28 23:19:47 -0400514 if (!mAllowFds) {
515 err = FDS_NOT_ALLOWED;
516 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700517 }
518 }
519 }
520
Dianne Hackborn8938ed22011-09-28 23:19:47 -0400521 return err;
522}
523
Jeff Brown13b16042014-11-11 16:44:25 -0800524bool Parcel::allowFds() const
525{
526 return mAllowFds;
527}
528
Dianne Hackborn7746cc32011-10-03 21:09:35 -0700529bool Parcel::pushAllowFds(bool allowFds)
Dianne Hackborn8938ed22011-09-28 23:19:47 -0400530{
531 const bool origValue = mAllowFds;
Dianne Hackborn7746cc32011-10-03 21:09:35 -0700532 if (!allowFds) {
533 mAllowFds = false;
534 }
Dianne Hackborn8938ed22011-09-28 23:19:47 -0400535 return origValue;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700536}
537
Dianne Hackborn7746cc32011-10-03 21:09:35 -0700538void Parcel::restoreAllowFds(bool lastValue)
539{
540 mAllowFds = lastValue;
541}
542
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700543bool Parcel::hasFileDescriptors() const
544{
545 if (!mFdsKnown) {
546 scanForFds();
547 }
548 return mHasFds;
549}
550
Brad Fitzpatrick702ea9d2010-06-18 13:07:53 -0700551// Write RPC headers. (previously just the interface token)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700552status_t Parcel::writeInterfaceToken(const String16& interface)
553{
Brad Fitzpatricka877cd82010-07-07 16:06:39 -0700554 writeInt32(IPCThreadState::self()->getStrictModePolicy() |
555 STRICT_MODE_PENALTY_GATHER);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700556 // currently the interface identification token is just its name as a string
557 return writeString16(interface);
558}
559
Mathias Agopian83c04462009-05-22 19:00:22 -0700560bool Parcel::checkInterface(IBinder* binder) const
561{
Brad Fitzpatrick702ea9d2010-06-18 13:07:53 -0700562 return enforceInterface(binder->getInterfaceDescriptor());
Mathias Agopian83c04462009-05-22 19:00:22 -0700563}
564
Brad Fitzpatricka877cd82010-07-07 16:06:39 -0700565bool Parcel::enforceInterface(const String16& interface,
Brad Fitzpatrick70081a12010-07-27 09:49:11 -0700566 IPCThreadState* threadState) const
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700567{
Brad Fitzpatrick70081a12010-07-27 09:49:11 -0700568 int32_t strictPolicy = readInt32();
569 if (threadState == NULL) {
570 threadState = IPCThreadState::self();
Brad Fitzpatricka877cd82010-07-07 16:06:39 -0700571 }
Brad Fitzpatrick52736032010-08-30 16:01:16 -0700572 if ((threadState->getLastTransactionBinderFlags() &
573 IBinder::FLAG_ONEWAY) != 0) {
574 // For one-way calls, the callee is running entirely
575 // disconnected from the caller, so disable StrictMode entirely.
576 // Not only does disk/network usage not impact the caller, but
577 // there's no way to commuicate back any violations anyway.
578 threadState->setStrictModePolicy(0);
579 } else {
580 threadState->setStrictModePolicy(strictPolicy);
581 }
Mathias Agopian83c04462009-05-22 19:00:22 -0700582 const String16 str(readString16());
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700583 if (str == interface) {
584 return true;
585 } else {
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700586 ALOGW("**** enforceInterface() expected '%s' but read '%s'",
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700587 String8(interface).string(), String8(str).string());
588 return false;
589 }
Brad Fitzpatrick702ea9d2010-06-18 13:07:53 -0700590}
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700591
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800592const binder_size_t* Parcel::objects() const
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700593{
594 return mObjects;
595}
596
597size_t Parcel::objectsCount() const
598{
599 return mObjectsSize;
600}
601
602status_t Parcel::errorCheck() const
603{
604 return mError;
605}
606
607void Parcel::setError(status_t err)
608{
609 mError = err;
610}
611
612status_t Parcel::finishWrite(size_t len)
613{
Nick Kralevichb6b14232015-04-02 09:36:02 -0700614 if (len > INT32_MAX) {
615 // don't accept size_t values which may have come from an
616 // inadvertent conversion from a negative int.
617 return BAD_VALUE;
618 }
619
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700620 //printf("Finish write of %d\n", len);
621 mDataPos += len;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700622 ALOGV("finishWrite Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700623 if (mDataPos > mDataSize) {
624 mDataSize = mDataPos;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700625 ALOGV("finishWrite Setting data size of %p to %zu", this, mDataSize);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700626 }
627 //printf("New pos=%d, size=%d\n", mDataPos, mDataSize);
628 return NO_ERROR;
629}
630
631status_t Parcel::writeUnpadded(const void* data, size_t len)
632{
Nick Kralevichb6b14232015-04-02 09:36:02 -0700633 if (len > INT32_MAX) {
634 // don't accept size_t values which may have come from an
635 // inadvertent conversion from a negative int.
636 return BAD_VALUE;
637 }
638
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700639 size_t end = mDataPos + len;
640 if (end < mDataPos) {
641 // integer overflow
642 return BAD_VALUE;
643 }
644
645 if (end <= mDataCapacity) {
646restart_write:
647 memcpy(mData+mDataPos, data, len);
648 return finishWrite(len);
649 }
650
651 status_t err = growData(len);
652 if (err == NO_ERROR) goto restart_write;
653 return err;
654}
655
656status_t Parcel::write(const void* data, size_t len)
657{
Nick Kralevichb6b14232015-04-02 09:36:02 -0700658 if (len > INT32_MAX) {
659 // don't accept size_t values which may have come from an
660 // inadvertent conversion from a negative int.
661 return BAD_VALUE;
662 }
663
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700664 void* const d = writeInplace(len);
665 if (d) {
666 memcpy(d, data, len);
667 return NO_ERROR;
668 }
669 return mError;
670}
671
672void* Parcel::writeInplace(size_t len)
673{
Nick Kralevichb6b14232015-04-02 09:36:02 -0700674 if (len > INT32_MAX) {
675 // don't accept size_t values which may have come from an
676 // inadvertent conversion from a negative int.
677 return NULL;
678 }
679
680 const size_t padded = pad_size(len);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700681
682 // sanity check for integer overflow
683 if (mDataPos+padded < mDataPos) {
684 return NULL;
685 }
686
687 if ((mDataPos+padded) <= mDataCapacity) {
688restart_write:
689 //printf("Writing %ld bytes, padded to %ld\n", len, padded);
690 uint8_t* const data = mData+mDataPos;
691
692 // Need to pad at end?
693 if (padded != len) {
694#if BYTE_ORDER == BIG_ENDIAN
695 static const uint32_t mask[4] = {
696 0x00000000, 0xffffff00, 0xffff0000, 0xff000000
697 };
698#endif
699#if BYTE_ORDER == LITTLE_ENDIAN
700 static const uint32_t mask[4] = {
701 0x00000000, 0x00ffffff, 0x0000ffff, 0x000000ff
702 };
703#endif
704 //printf("Applying pad mask: %p to %p\n", (void*)mask[padded-len],
705 // *reinterpret_cast<void**>(data+padded-4));
706 *reinterpret_cast<uint32_t*>(data+padded-4) &= mask[padded-len];
707 }
708
709 finishWrite(padded);
710 return data;
711 }
712
713 status_t err = growData(padded);
714 if (err == NO_ERROR) goto restart_write;
715 return NULL;
716}
717
718status_t Parcel::writeInt32(int32_t val)
719{
Andreas Huber84a6d042009-08-17 13:33:27 -0700720 return writeAligned(val);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700721}
Dan Stoza41a0f2f2014-12-01 10:01:10 -0800722
723status_t Parcel::writeUint32(uint32_t val)
724{
725 return writeAligned(val);
726}
727
Marco Nelissen5c0106e2013-10-16 10:57:51 -0700728status_t Parcel::writeInt32Array(size_t len, const int32_t *val) {
Nick Kralevichb6b14232015-04-02 09:36:02 -0700729 if (len > INT32_MAX) {
730 // don't accept size_t values which may have come from an
731 // inadvertent conversion from a negative int.
732 return BAD_VALUE;
733 }
734
Marco Nelissen5c0106e2013-10-16 10:57:51 -0700735 if (!val) {
Chad Brubakere59cb432015-06-30 14:03:55 -0700736 return writeInt32(-1);
Marco Nelissen5c0106e2013-10-16 10:57:51 -0700737 }
Chad Brubakere59cb432015-06-30 14:03:55 -0700738 status_t ret = writeInt32(static_cast<uint32_t>(len));
Marco Nelissen5c0106e2013-10-16 10:57:51 -0700739 if (ret == NO_ERROR) {
740 ret = write(val, len * sizeof(*val));
741 }
742 return ret;
743}
Marco Nelissenf0190bf2014-03-13 14:17:40 -0700744status_t Parcel::writeByteArray(size_t len, const uint8_t *val) {
Nick Kralevichb6b14232015-04-02 09:36:02 -0700745 if (len > INT32_MAX) {
746 // don't accept size_t values which may have come from an
747 // inadvertent conversion from a negative int.
748 return BAD_VALUE;
749 }
750
Marco Nelissenf0190bf2014-03-13 14:17:40 -0700751 if (!val) {
Chad Brubakere59cb432015-06-30 14:03:55 -0700752 return writeInt32(-1);
Marco Nelissenf0190bf2014-03-13 14:17:40 -0700753 }
Chad Brubakere59cb432015-06-30 14:03:55 -0700754 status_t ret = writeInt32(static_cast<uint32_t>(len));
Marco Nelissenf0190bf2014-03-13 14:17:40 -0700755 if (ret == NO_ERROR) {
756 ret = write(val, len * sizeof(*val));
757 }
758 return ret;
759}
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700760
761status_t Parcel::writeInt64(int64_t val)
762{
Andreas Huber84a6d042009-08-17 13:33:27 -0700763 return writeAligned(val);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700764}
765
Ronghua Wu2d13afd2015-03-16 11:11:07 -0700766status_t Parcel::writeUint64(uint64_t val)
767{
768 return writeAligned(val);
769}
770
Serban Constantinescuf683e012013-11-05 16:53:55 +0000771status_t Parcel::writePointer(uintptr_t val)
772{
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800773 return writeAligned<binder_uintptr_t>(val);
Serban Constantinescuf683e012013-11-05 16:53:55 +0000774}
775
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700776status_t Parcel::writeFloat(float val)
777{
Andreas Huber84a6d042009-08-17 13:33:27 -0700778 return writeAligned(val);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700779}
780
Douglas Leungcc1a4bb2013-01-11 15:00:55 -0800781#if defined(__mips__) && defined(__mips_hard_float)
782
783status_t Parcel::writeDouble(double val)
784{
785 union {
786 double d;
787 unsigned long long ll;
788 } u;
789 u.d = val;
790 return writeAligned(u.ll);
791}
792
793#else
794
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700795status_t Parcel::writeDouble(double val)
796{
Andreas Huber84a6d042009-08-17 13:33:27 -0700797 return writeAligned(val);
798}
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700799
Douglas Leungcc1a4bb2013-01-11 15:00:55 -0800800#endif
801
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700802status_t Parcel::writeCString(const char* str)
803{
804 return write(str, strlen(str)+1);
805}
806
807status_t Parcel::writeString8(const String8& str)
808{
809 status_t err = writeInt32(str.bytes());
Pravat Dalbeherad1dff8d2010-12-15 08:40:00 +0100810 // only write string if its length is more than zero characters,
811 // as readString8 will only read if the length field is non-zero.
812 // this is slightly different from how writeString16 works.
813 if (str.bytes() > 0 && err == NO_ERROR) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700814 err = write(str.string(), str.bytes()+1);
815 }
816 return err;
817}
818
819status_t Parcel::writeString16(const String16& str)
820{
821 return writeString16(str.string(), str.size());
822}
823
824status_t Parcel::writeString16(const char16_t* str, size_t len)
825{
826 if (str == NULL) return writeInt32(-1);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700827
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700828 status_t err = writeInt32(len);
829 if (err == NO_ERROR) {
830 len *= sizeof(char16_t);
831 uint8_t* data = (uint8_t*)writeInplace(len+sizeof(char16_t));
832 if (data) {
833 memcpy(data, str, len);
834 *reinterpret_cast<char16_t*>(data+len) = 0;
835 return NO_ERROR;
836 }
837 err = mError;
838 }
839 return err;
840}
841
842status_t Parcel::writeStrongBinder(const sp<IBinder>& val)
843{
844 return flatten_binder(ProcessState::self(), val, this);
845}
846
847status_t Parcel::writeWeakBinder(const wp<IBinder>& val)
848{
849 return flatten_binder(ProcessState::self(), val, this);
850}
851
Mathias Agopiana47f02a2009-05-21 16:29:38 -0700852status_t Parcel::writeNativeHandle(const native_handle* handle)
The Android Open Source Project5f78a482009-01-20 14:03:58 -0800853{
Mathias Agopian1d0a95b2009-07-31 16:12:13 -0700854 if (!handle || handle->version != sizeof(native_handle))
The Android Open Source Project5f78a482009-01-20 14:03:58 -0800855 return BAD_TYPE;
856
857 status_t err;
Mathias Agopiana47f02a2009-05-21 16:29:38 -0700858 err = writeInt32(handle->numFds);
The Android Open Source Project5f78a482009-01-20 14:03:58 -0800859 if (err != NO_ERROR) return err;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800860
Mathias Agopiana47f02a2009-05-21 16:29:38 -0700861 err = writeInt32(handle->numInts);
The Android Open Source Project5f78a482009-01-20 14:03:58 -0800862 if (err != NO_ERROR) return err;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800863
Mathias Agopiana47f02a2009-05-21 16:29:38 -0700864 for (int i=0 ; err==NO_ERROR && i<handle->numFds ; i++)
865 err = writeDupFileDescriptor(handle->data[i]);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800866
867 if (err != NO_ERROR) {
Steve Block9d453682011-12-20 16:23:08 +0000868 ALOGD("write native handle, write dup fd failed");
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800869 return err;
870 }
Mathias Agopiana47f02a2009-05-21 16:29:38 -0700871 err = write(handle->data + handle->numFds, sizeof(int)*handle->numInts);
The Android Open Source Project5f78a482009-01-20 14:03:58 -0800872 return err;
873}
874
Jeff Brown93ff1f92011-11-04 19:01:44 -0700875status_t Parcel::writeFileDescriptor(int fd, bool takeOwnership)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700876{
877 flat_binder_object obj;
878 obj.type = BINDER_TYPE_FD;
879 obj.flags = 0x7f | FLAT_BINDER_FLAG_ACCEPTS_FDS;
Arve Hjønnevåg07fd0f12014-02-18 21:10:29 -0800880 obj.binder = 0; /* Don't pass uninitialized stack data to a remote process */
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700881 obj.handle = fd;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800882 obj.cookie = takeOwnership ? 1 : 0;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700883 return writeObject(obj, true);
884}
885
886status_t Parcel::writeDupFileDescriptor(int fd)
887{
Jeff Brownd341c712011-11-04 20:19:33 -0700888 int dupFd = dup(fd);
889 if (dupFd < 0) {
890 return -errno;
891 }
892 status_t err = writeFileDescriptor(dupFd, true /*takeOwnership*/);
893 if (err) {
894 close(dupFd);
895 }
896 return err;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700897}
898
Jeff Brown13b16042014-11-11 16:44:25 -0800899status_t Parcel::writeBlob(size_t len, bool mutableCopy, WritableBlob* outBlob)
Jeff Brown5707dbf2011-09-23 21:17:56 -0700900{
Nick Kralevichb6b14232015-04-02 09:36:02 -0700901 if (len > INT32_MAX) {
902 // don't accept size_t values which may have come from an
903 // inadvertent conversion from a negative int.
904 return BAD_VALUE;
905 }
906
Jeff Brown13b16042014-11-11 16:44:25 -0800907 status_t status;
908 if (!mAllowFds || len <= BLOB_INPLACE_LIMIT) {
Steve Block6807e592011-10-20 11:56:00 +0100909 ALOGV("writeBlob: write in place");
Jeff Brown13b16042014-11-11 16:44:25 -0800910 status = writeInt32(BLOB_INPLACE);
Jeff Brown5707dbf2011-09-23 21:17:56 -0700911 if (status) return status;
912
913 void* ptr = writeInplace(len);
914 if (!ptr) return NO_MEMORY;
915
Jeff Brown13b16042014-11-11 16:44:25 -0800916 outBlob->init(-1, ptr, len, false);
Jeff Brown5707dbf2011-09-23 21:17:56 -0700917 return NO_ERROR;
918 }
919
Steve Block6807e592011-10-20 11:56:00 +0100920 ALOGV("writeBlob: write to ashmem");
Jeff Brown5707dbf2011-09-23 21:17:56 -0700921 int fd = ashmem_create_region("Parcel Blob", len);
922 if (fd < 0) return NO_MEMORY;
923
Dan Sandleraa5c2342015-04-10 10:08:45 -0400924 mBlobAshmemSize += len;
925
Jeff Brown5707dbf2011-09-23 21:17:56 -0700926 int result = ashmem_set_prot_region(fd, PROT_READ | PROT_WRITE);
927 if (result < 0) {
Jeff Brownec4e0062011-10-10 14:50:10 -0700928 status = result;
Jeff Brown5707dbf2011-09-23 21:17:56 -0700929 } else {
930 void* ptr = ::mmap(NULL, len, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
931 if (ptr == MAP_FAILED) {
932 status = -errno;
933 } else {
Jeff Brown13b16042014-11-11 16:44:25 -0800934 if (!mutableCopy) {
935 result = ashmem_set_prot_region(fd, PROT_READ);
936 }
Jeff Brown5707dbf2011-09-23 21:17:56 -0700937 if (result < 0) {
Jeff Brownec4e0062011-10-10 14:50:10 -0700938 status = result;
Jeff Brown5707dbf2011-09-23 21:17:56 -0700939 } else {
Jeff Brown13b16042014-11-11 16:44:25 -0800940 status = writeInt32(mutableCopy ? BLOB_ASHMEM_MUTABLE : BLOB_ASHMEM_IMMUTABLE);
Jeff Brown5707dbf2011-09-23 21:17:56 -0700941 if (!status) {
Jeff Brown93ff1f92011-11-04 19:01:44 -0700942 status = writeFileDescriptor(fd, true /*takeOwnership*/);
Jeff Brown5707dbf2011-09-23 21:17:56 -0700943 if (!status) {
Jeff Brown13b16042014-11-11 16:44:25 -0800944 outBlob->init(fd, ptr, len, mutableCopy);
Jeff Brown5707dbf2011-09-23 21:17:56 -0700945 return NO_ERROR;
946 }
947 }
948 }
949 }
950 ::munmap(ptr, len);
951 }
952 ::close(fd);
953 return status;
954}
955
Jeff Brown13b16042014-11-11 16:44:25 -0800956status_t Parcel::writeDupImmutableBlobFileDescriptor(int fd)
957{
958 // Must match up with what's done in writeBlob.
959 if (!mAllowFds) return FDS_NOT_ALLOWED;
960 status_t status = writeInt32(BLOB_ASHMEM_IMMUTABLE);
961 if (status) return status;
962 return writeDupFileDescriptor(fd);
963}
964
Mathias Agopiane1424282013-07-29 21:24:40 -0700965status_t Parcel::write(const FlattenableHelperInterface& val)
Mathias Agopian98e71dd2010-02-11 17:30:52 -0800966{
967 status_t err;
968
969 // size if needed
Mathias Agopiane1424282013-07-29 21:24:40 -0700970 const size_t len = val.getFlattenedSize();
971 const size_t fd_count = val.getFdCount();
Mathias Agopian98e71dd2010-02-11 17:30:52 -0800972
Nick Kralevichb6b14232015-04-02 09:36:02 -0700973 if ((len > INT32_MAX) || (fd_count > INT32_MAX)) {
974 // don't accept size_t values which may have come from an
975 // inadvertent conversion from a negative int.
976 return BAD_VALUE;
977 }
978
Mathias Agopian98e71dd2010-02-11 17:30:52 -0800979 err = this->writeInt32(len);
980 if (err) return err;
981
982 err = this->writeInt32(fd_count);
983 if (err) return err;
984
985 // payload
Nick Kralevichb6b14232015-04-02 09:36:02 -0700986 void* const buf = this->writeInplace(pad_size(len));
Mathias Agopian98e71dd2010-02-11 17:30:52 -0800987 if (buf == NULL)
988 return BAD_VALUE;
989
990 int* fds = NULL;
991 if (fd_count) {
992 fds = new int[fd_count];
993 }
994
995 err = val.flatten(buf, len, fds, fd_count);
996 for (size_t i=0 ; i<fd_count && err==NO_ERROR ; i++) {
997 err = this->writeDupFileDescriptor( fds[i] );
998 }
999
1000 if (fd_count) {
1001 delete [] fds;
1002 }
1003
1004 return err;
1005}
1006
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001007status_t Parcel::writeObject(const flat_binder_object& val, bool nullMetaData)
1008{
1009 const bool enoughData = (mDataPos+sizeof(val)) <= mDataCapacity;
1010 const bool enoughObjects = mObjectsSize < mObjectsCapacity;
1011 if (enoughData && enoughObjects) {
1012restart_write:
1013 *reinterpret_cast<flat_binder_object*>(mData+mDataPos) = val;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001014
Christopher Tate98e67d32015-06-03 18:44:15 -07001015 // remember if it's a file descriptor
1016 if (val.type == BINDER_TYPE_FD) {
1017 if (!mAllowFds) {
1018 // fail before modifying our object index
1019 return FDS_NOT_ALLOWED;
1020 }
1021 mHasFds = mFdsKnown = true;
1022 }
1023
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001024 // Need to write meta-data?
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001025 if (nullMetaData || val.binder != 0) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001026 mObjects[mObjectsSize] = mDataPos;
1027 acquire_object(ProcessState::self(), val, this);
1028 mObjectsSize++;
1029 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001030
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001031 return finishWrite(sizeof(flat_binder_object));
1032 }
1033
1034 if (!enoughData) {
1035 const status_t err = growData(sizeof(val));
1036 if (err != NO_ERROR) return err;
1037 }
1038 if (!enoughObjects) {
1039 size_t newSize = ((mObjectsSize+2)*3)/2;
Christopher Tateed7a50c2015-06-08 14:45:14 -07001040 if (newSize < mObjectsSize) return NO_MEMORY; // overflow
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001041 binder_size_t* objects = (binder_size_t*)realloc(mObjects, newSize*sizeof(binder_size_t));
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001042 if (objects == NULL) return NO_MEMORY;
1043 mObjects = objects;
1044 mObjectsCapacity = newSize;
1045 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001046
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001047 goto restart_write;
1048}
1049
Brad Fitzpatrick837a0d02010-07-13 15:33:35 -07001050status_t Parcel::writeNoException()
1051{
1052 return writeInt32(0);
1053}
1054
Colin Cross6f4f3ab2014-02-05 17:42:44 -08001055void Parcel::remove(size_t /*start*/, size_t /*amt*/)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001056{
1057 LOG_ALWAYS_FATAL("Parcel::remove() not yet implemented!");
1058}
1059
1060status_t Parcel::read(void* outData, size_t len) const
1061{
Nick Kralevichb6b14232015-04-02 09:36:02 -07001062 if (len > INT32_MAX) {
1063 // don't accept size_t values which may have come from an
1064 // inadvertent conversion from a negative int.
1065 return BAD_VALUE;
1066 }
1067
1068 if ((mDataPos+pad_size(len)) >= mDataPos && (mDataPos+pad_size(len)) <= mDataSize
1069 && len <= pad_size(len)) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001070 memcpy(outData, mData+mDataPos, len);
Nick Kralevichb6b14232015-04-02 09:36:02 -07001071 mDataPos += pad_size(len);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001072 ALOGV("read Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001073 return NO_ERROR;
1074 }
1075 return NOT_ENOUGH_DATA;
1076}
1077
1078const void* Parcel::readInplace(size_t len) const
1079{
Nick Kralevichb6b14232015-04-02 09:36:02 -07001080 if (len > INT32_MAX) {
1081 // don't accept size_t values which may have come from an
1082 // inadvertent conversion from a negative int.
1083 return NULL;
1084 }
1085
1086 if ((mDataPos+pad_size(len)) >= mDataPos && (mDataPos+pad_size(len)) <= mDataSize
1087 && len <= pad_size(len)) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001088 const void* data = mData+mDataPos;
Nick Kralevichb6b14232015-04-02 09:36:02 -07001089 mDataPos += pad_size(len);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001090 ALOGV("readInplace Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001091 return data;
1092 }
1093 return NULL;
1094}
1095
Andreas Huber84a6d042009-08-17 13:33:27 -07001096template<class T>
1097status_t Parcel::readAligned(T *pArg) const {
Nick Kralevichb6b14232015-04-02 09:36:02 -07001098 COMPILE_TIME_ASSERT_FUNCTION_SCOPE(PAD_SIZE_UNSAFE(sizeof(T)) == sizeof(T));
Andreas Huber84a6d042009-08-17 13:33:27 -07001099
1100 if ((mDataPos+sizeof(T)) <= mDataSize) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001101 const void* data = mData+mDataPos;
Andreas Huber84a6d042009-08-17 13:33:27 -07001102 mDataPos += sizeof(T);
1103 *pArg = *reinterpret_cast<const T*>(data);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001104 return NO_ERROR;
1105 } else {
1106 return NOT_ENOUGH_DATA;
1107 }
1108}
1109
Andreas Huber84a6d042009-08-17 13:33:27 -07001110template<class T>
1111T Parcel::readAligned() const {
1112 T result;
1113 if (readAligned(&result) != NO_ERROR) {
1114 result = 0;
1115 }
1116
1117 return result;
1118}
1119
1120template<class T>
1121status_t Parcel::writeAligned(T val) {
Nick Kralevichb6b14232015-04-02 09:36:02 -07001122 COMPILE_TIME_ASSERT_FUNCTION_SCOPE(PAD_SIZE_UNSAFE(sizeof(T)) == sizeof(T));
Andreas Huber84a6d042009-08-17 13:33:27 -07001123
1124 if ((mDataPos+sizeof(val)) <= mDataCapacity) {
1125restart_write:
1126 *reinterpret_cast<T*>(mData+mDataPos) = val;
1127 return finishWrite(sizeof(val));
1128 }
1129
1130 status_t err = growData(sizeof(val));
1131 if (err == NO_ERROR) goto restart_write;
1132 return err;
1133}
1134
1135status_t Parcel::readInt32(int32_t *pArg) const
1136{
1137 return readAligned(pArg);
1138}
1139
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001140int32_t Parcel::readInt32() const
1141{
Andreas Huber84a6d042009-08-17 13:33:27 -07001142 return readAligned<int32_t>();
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001143}
1144
Dan Stoza41a0f2f2014-12-01 10:01:10 -08001145status_t Parcel::readUint32(uint32_t *pArg) const
1146{
1147 return readAligned(pArg);
1148}
1149
1150uint32_t Parcel::readUint32() const
1151{
1152 return readAligned<uint32_t>();
1153}
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001154
1155status_t Parcel::readInt64(int64_t *pArg) const
1156{
Andreas Huber84a6d042009-08-17 13:33:27 -07001157 return readAligned(pArg);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001158}
1159
1160
1161int64_t Parcel::readInt64() const
1162{
Andreas Huber84a6d042009-08-17 13:33:27 -07001163 return readAligned<int64_t>();
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001164}
1165
Ronghua Wu2d13afd2015-03-16 11:11:07 -07001166status_t Parcel::readUint64(uint64_t *pArg) const
1167{
1168 return readAligned(pArg);
1169}
1170
1171uint64_t Parcel::readUint64() const
1172{
1173 return readAligned<uint64_t>();
1174}
1175
Serban Constantinescuf683e012013-11-05 16:53:55 +00001176status_t Parcel::readPointer(uintptr_t *pArg) const
1177{
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001178 status_t ret;
1179 binder_uintptr_t ptr;
1180 ret = readAligned(&ptr);
1181 if (!ret)
1182 *pArg = ptr;
1183 return ret;
Serban Constantinescuf683e012013-11-05 16:53:55 +00001184}
1185
1186uintptr_t Parcel::readPointer() const
1187{
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001188 return readAligned<binder_uintptr_t>();
Serban Constantinescuf683e012013-11-05 16:53:55 +00001189}
1190
1191
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001192status_t Parcel::readFloat(float *pArg) const
1193{
Andreas Huber84a6d042009-08-17 13:33:27 -07001194 return readAligned(pArg);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001195}
1196
1197
1198float Parcel::readFloat() const
1199{
Andreas Huber84a6d042009-08-17 13:33:27 -07001200 return readAligned<float>();
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001201}
1202
Douglas Leungcc1a4bb2013-01-11 15:00:55 -08001203#if defined(__mips__) && defined(__mips_hard_float)
1204
1205status_t Parcel::readDouble(double *pArg) const
1206{
1207 union {
1208 double d;
1209 unsigned long long ll;
1210 } u;
Narayan Kamath2c68d382014-06-04 15:04:29 +01001211 u.d = 0;
Douglas Leungcc1a4bb2013-01-11 15:00:55 -08001212 status_t status;
1213 status = readAligned(&u.ll);
1214 *pArg = u.d;
1215 return status;
1216}
1217
1218double Parcel::readDouble() const
1219{
1220 union {
1221 double d;
1222 unsigned long long ll;
1223 } u;
1224 u.ll = readAligned<unsigned long long>();
1225 return u.d;
1226}
1227
1228#else
1229
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001230status_t Parcel::readDouble(double *pArg) const
1231{
Andreas Huber84a6d042009-08-17 13:33:27 -07001232 return readAligned(pArg);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001233}
1234
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001235double Parcel::readDouble() const
1236{
Andreas Huber84a6d042009-08-17 13:33:27 -07001237 return readAligned<double>();
1238}
1239
Douglas Leungcc1a4bb2013-01-11 15:00:55 -08001240#endif
1241
Andreas Huber84a6d042009-08-17 13:33:27 -07001242status_t Parcel::readIntPtr(intptr_t *pArg) const
1243{
1244 return readAligned(pArg);
1245}
1246
1247
1248intptr_t Parcel::readIntPtr() const
1249{
1250 return readAligned<intptr_t>();
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001251}
1252
1253
1254const char* Parcel::readCString() const
1255{
1256 const size_t avail = mDataSize-mDataPos;
1257 if (avail > 0) {
1258 const char* str = reinterpret_cast<const char*>(mData+mDataPos);
1259 // is the string's trailing NUL within the parcel's valid bounds?
1260 const char* eos = reinterpret_cast<const char*>(memchr(str, 0, avail));
1261 if (eos) {
1262 const size_t len = eos - str;
Nick Kralevichb6b14232015-04-02 09:36:02 -07001263 mDataPos += pad_size(len+1);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001264 ALOGV("readCString Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001265 return str;
1266 }
1267 }
1268 return NULL;
1269}
1270
1271String8 Parcel::readString8() const
1272{
1273 int32_t size = readInt32();
1274 // watch for potential int overflow adding 1 for trailing NUL
1275 if (size > 0 && size < INT32_MAX) {
1276 const char* str = (const char*)readInplace(size+1);
1277 if (str) return String8(str, size);
1278 }
1279 return String8();
1280}
1281
1282String16 Parcel::readString16() const
1283{
1284 size_t len;
1285 const char16_t* str = readString16Inplace(&len);
1286 if (str) return String16(str, len);
Steve Blocke6f43dd2012-01-06 19:20:56 +00001287 ALOGE("Reading a NULL string not supported here.");
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001288 return String16();
1289}
1290
1291const char16_t* Parcel::readString16Inplace(size_t* outLen) const
1292{
1293 int32_t size = readInt32();
1294 // watch for potential int overflow from size+1
1295 if (size >= 0 && size < INT32_MAX) {
1296 *outLen = size;
1297 const char16_t* str = (const char16_t*)readInplace((size+1)*sizeof(char16_t));
1298 if (str != NULL) {
1299 return str;
1300 }
1301 }
1302 *outLen = 0;
1303 return NULL;
1304}
1305
1306sp<IBinder> Parcel::readStrongBinder() const
1307{
1308 sp<IBinder> val;
1309 unflatten_binder(ProcessState::self(), *this, &val);
1310 return val;
1311}
1312
1313wp<IBinder> Parcel::readWeakBinder() const
1314{
1315 wp<IBinder> val;
1316 unflatten_binder(ProcessState::self(), *this, &val);
1317 return val;
1318}
1319
Brad Fitzpatrick837a0d02010-07-13 15:33:35 -07001320int32_t Parcel::readExceptionCode() const
1321{
1322 int32_t exception_code = readAligned<int32_t>();
Brad Fitzpatrickd36f4a52010-07-12 11:05:38 -07001323 if (exception_code == EX_HAS_REPLY_HEADER) {
Magnus Strandberg1ba24572011-05-03 15:44:00 +02001324 int32_t header_start = dataPosition();
Brad Fitzpatrickd36f4a52010-07-12 11:05:38 -07001325 int32_t header_size = readAligned<int32_t>();
1326 // Skip over fat responses headers. Not used (or propagated) in
1327 // native code
Magnus Strandberg1ba24572011-05-03 15:44:00 +02001328 setDataPosition(header_start + header_size);
Brad Fitzpatrickd36f4a52010-07-12 11:05:38 -07001329 // And fat response headers are currently only used when there are no
1330 // exceptions, so return no error:
1331 return 0;
1332 }
Brad Fitzpatrick837a0d02010-07-13 15:33:35 -07001333 return exception_code;
1334}
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001335
Mathias Agopiana47f02a2009-05-21 16:29:38 -07001336native_handle* Parcel::readNativeHandle() const
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001337{
1338 int numFds, numInts;
1339 status_t err;
1340 err = readInt32(&numFds);
1341 if (err != NO_ERROR) return 0;
1342 err = readInt32(&numInts);
1343 if (err != NO_ERROR) return 0;
1344
Mathias Agopiana47f02a2009-05-21 16:29:38 -07001345 native_handle* h = native_handle_create(numFds, numInts);
Adam Lesinskieaac99a2015-05-12 17:35:48 -07001346 if (!h) {
1347 return 0;
1348 }
1349
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001350 for (int i=0 ; err==NO_ERROR && i<numFds ; i++) {
Rebecca Schultz Zavin360211f2009-02-13 16:34:38 -08001351 h->data[i] = dup(readFileDescriptor());
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001352 if (h->data[i] < 0) err = BAD_VALUE;
1353 }
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001354 err = read(h->data + numFds, sizeof(int)*numInts);
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001355 if (err != NO_ERROR) {
Mathias Agopiana47f02a2009-05-21 16:29:38 -07001356 native_handle_close(h);
1357 native_handle_delete(h);
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001358 h = 0;
1359 }
1360 return h;
1361}
1362
1363
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001364int Parcel::readFileDescriptor() const
1365{
1366 const flat_binder_object* flat = readObject(true);
1367 if (flat) {
1368 switch (flat->type) {
1369 case BINDER_TYPE_FD:
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001370 //ALOGI("Returning file descriptor %ld from parcel %p", flat->handle, this);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001371 return flat->handle;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001372 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001373 }
1374 return BAD_TYPE;
1375}
1376
Jeff Brown5707dbf2011-09-23 21:17:56 -07001377status_t Parcel::readBlob(size_t len, ReadableBlob* outBlob) const
1378{
Jeff Brown13b16042014-11-11 16:44:25 -08001379 int32_t blobType;
1380 status_t status = readInt32(&blobType);
Jeff Brown5707dbf2011-09-23 21:17:56 -07001381 if (status) return status;
1382
Jeff Brown13b16042014-11-11 16:44:25 -08001383 if (blobType == BLOB_INPLACE) {
Steve Block6807e592011-10-20 11:56:00 +01001384 ALOGV("readBlob: read in place");
Jeff Brown5707dbf2011-09-23 21:17:56 -07001385 const void* ptr = readInplace(len);
1386 if (!ptr) return BAD_VALUE;
1387
Jeff Brown13b16042014-11-11 16:44:25 -08001388 outBlob->init(-1, const_cast<void*>(ptr), len, false);
Jeff Brown5707dbf2011-09-23 21:17:56 -07001389 return NO_ERROR;
1390 }
1391
Steve Block6807e592011-10-20 11:56:00 +01001392 ALOGV("readBlob: read from ashmem");
Jeff Brown13b16042014-11-11 16:44:25 -08001393 bool isMutable = (blobType == BLOB_ASHMEM_MUTABLE);
Jeff Brown5707dbf2011-09-23 21:17:56 -07001394 int fd = readFileDescriptor();
1395 if (fd == int(BAD_TYPE)) return BAD_VALUE;
1396
Jeff Brown13b16042014-11-11 16:44:25 -08001397 void* ptr = ::mmap(NULL, len, isMutable ? PROT_READ | PROT_WRITE : PROT_READ,
1398 MAP_SHARED, fd, 0);
Narayan Kamath9ea09752014-10-08 17:35:45 +01001399 if (ptr == MAP_FAILED) return NO_MEMORY;
Jeff Brown5707dbf2011-09-23 21:17:56 -07001400
Jeff Brown13b16042014-11-11 16:44:25 -08001401 outBlob->init(fd, ptr, len, isMutable);
Jeff Brown5707dbf2011-09-23 21:17:56 -07001402 return NO_ERROR;
1403}
1404
Mathias Agopiane1424282013-07-29 21:24:40 -07001405status_t Parcel::read(FlattenableHelperInterface& val) const
Mathias Agopian98e71dd2010-02-11 17:30:52 -08001406{
1407 // size
1408 const size_t len = this->readInt32();
1409 const size_t fd_count = this->readInt32();
1410
Nick Kralevichb6b14232015-04-02 09:36:02 -07001411 if (len > INT32_MAX) {
1412 // don't accept size_t values which may have come from an
1413 // inadvertent conversion from a negative int.
1414 return BAD_VALUE;
1415 }
1416
Mathias Agopian98e71dd2010-02-11 17:30:52 -08001417 // payload
Nick Kralevichb6b14232015-04-02 09:36:02 -07001418 void const* const buf = this->readInplace(pad_size(len));
Mathias Agopian98e71dd2010-02-11 17:30:52 -08001419 if (buf == NULL)
1420 return BAD_VALUE;
1421
1422 int* fds = NULL;
1423 if (fd_count) {
1424 fds = new int[fd_count];
1425 }
1426
1427 status_t err = NO_ERROR;
1428 for (size_t i=0 ; i<fd_count && err==NO_ERROR ; i++) {
Jesse Hallfee99042014-11-04 08:36:31 -08001429 fds[i] = dup(this->readFileDescriptor());
Jun Jiangabf8a2c2014-04-29 14:22:10 +08001430 if (fds[i] < 0) {
1431 err = BAD_VALUE;
Jesse Hallfee99042014-11-04 08:36:31 -08001432 ALOGE("dup() failed in Parcel::read, i is %zu, fds[i] is %d, fd_count is %zu, error: %s",
1433 i, fds[i], fd_count, strerror(errno));
Jun Jiangabf8a2c2014-04-29 14:22:10 +08001434 }
Mathias Agopian98e71dd2010-02-11 17:30:52 -08001435 }
1436
1437 if (err == NO_ERROR) {
1438 err = val.unflatten(buf, len, fds, fd_count);
1439 }
1440
1441 if (fd_count) {
1442 delete [] fds;
1443 }
1444
1445 return err;
1446}
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001447const flat_binder_object* Parcel::readObject(bool nullMetaData) const
1448{
1449 const size_t DPOS = mDataPos;
1450 if ((DPOS+sizeof(flat_binder_object)) <= mDataSize) {
1451 const flat_binder_object* obj
1452 = reinterpret_cast<const flat_binder_object*>(mData+DPOS);
1453 mDataPos = DPOS + sizeof(flat_binder_object);
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001454 if (!nullMetaData && (obj->cookie == 0 && obj->binder == 0)) {
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001455 // When transferring a NULL object, we don't write it into
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001456 // the object list, so we don't want to check for it when
1457 // reading.
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001458 ALOGV("readObject Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001459 return obj;
1460 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001461
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001462 // Ensure that this object is valid...
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001463 binder_size_t* const OBJS = mObjects;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001464 const size_t N = mObjectsSize;
1465 size_t opos = mNextObjectHint;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001466
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001467 if (N > 0) {
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001468 ALOGV("Parcel %p looking for obj at %zu, hint=%zu",
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001469 this, DPOS, opos);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001470
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001471 // Start at the current hint position, looking for an object at
1472 // the current data position.
1473 if (opos < N) {
1474 while (opos < (N-1) && OBJS[opos] < DPOS) {
1475 opos++;
1476 }
1477 } else {
1478 opos = N-1;
1479 }
1480 if (OBJS[opos] == DPOS) {
1481 // Found it!
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001482 ALOGV("Parcel %p found obj %zu at index %zu with forward search",
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001483 this, DPOS, opos);
1484 mNextObjectHint = opos+1;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001485 ALOGV("readObject Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001486 return obj;
1487 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001488
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001489 // Look backwards for it...
1490 while (opos > 0 && OBJS[opos] > DPOS) {
1491 opos--;
1492 }
1493 if (OBJS[opos] == DPOS) {
1494 // Found it!
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001495 ALOGV("Parcel %p found obj %zu at index %zu with backward search",
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001496 this, DPOS, opos);
1497 mNextObjectHint = opos+1;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001498 ALOGV("readObject Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001499 return obj;
1500 }
1501 }
Colin Cross6f4f3ab2014-02-05 17:42:44 -08001502 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 -07001503 this, DPOS);
1504 }
1505 return NULL;
1506}
1507
1508void Parcel::closeFileDescriptors()
1509{
1510 size_t i = mObjectsSize;
1511 if (i > 0) {
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001512 //ALOGI("Closing file descriptors for %zu objects...", i);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001513 }
1514 while (i > 0) {
1515 i--;
1516 const flat_binder_object* flat
1517 = reinterpret_cast<flat_binder_object*>(mData+mObjects[i]);
1518 if (flat->type == BINDER_TYPE_FD) {
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001519 //ALOGI("Closing fd: %ld", flat->handle);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001520 close(flat->handle);
1521 }
1522 }
1523}
1524
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001525uintptr_t Parcel::ipcData() const
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001526{
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001527 return reinterpret_cast<uintptr_t>(mData);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001528}
1529
1530size_t Parcel::ipcDataSize() const
1531{
1532 return (mDataSize > mDataPos ? mDataSize : mDataPos);
1533}
1534
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001535uintptr_t Parcel::ipcObjects() const
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001536{
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001537 return reinterpret_cast<uintptr_t>(mObjects);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001538}
1539
1540size_t Parcel::ipcObjectsCount() const
1541{
1542 return mObjectsSize;
1543}
1544
1545void Parcel::ipcSetDataReference(const uint8_t* data, size_t dataSize,
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001546 const binder_size_t* objects, size_t objectsCount, release_func relFunc, void* relCookie)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001547{
Arve Hjønnevåg6f286112014-02-19 20:42:13 -08001548 binder_size_t minOffset = 0;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001549 freeDataNoInit();
1550 mError = NO_ERROR;
1551 mData = const_cast<uint8_t*>(data);
1552 mDataSize = mDataCapacity = dataSize;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001553 //ALOGI("setDataReference Setting data size of %p to %lu (pid=%d)", this, mDataSize, getpid());
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001554 mDataPos = 0;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001555 ALOGV("setDataReference Setting data pos of %p to %zu", this, mDataPos);
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001556 mObjects = const_cast<binder_size_t*>(objects);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001557 mObjectsSize = mObjectsCapacity = objectsCount;
1558 mNextObjectHint = 0;
1559 mOwner = relFunc;
1560 mOwnerCookie = relCookie;
Arve Hjønnevågf50b9ea2014-02-13 19:22:08 -08001561 for (size_t i = 0; i < mObjectsSize; i++) {
Arve Hjønnevåg6f286112014-02-19 20:42:13 -08001562 binder_size_t offset = mObjects[i];
Arve Hjønnevågf50b9ea2014-02-13 19:22:08 -08001563 if (offset < minOffset) {
Dan Albert3bdc5b82014-11-20 11:50:23 -08001564 ALOGE("%s: bad object offset %" PRIu64 " < %" PRIu64 "\n",
Arve Hjønnevåg6f286112014-02-19 20:42:13 -08001565 __func__, (uint64_t)offset, (uint64_t)minOffset);
Arve Hjønnevågf50b9ea2014-02-13 19:22:08 -08001566 mObjectsSize = 0;
1567 break;
1568 }
1569 minOffset = offset + sizeof(flat_binder_object);
1570 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001571 scanForFds();
1572}
1573
Colin Cross6f4f3ab2014-02-05 17:42:44 -08001574void Parcel::print(TextOutput& to, uint32_t /*flags*/) const
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001575{
1576 to << "Parcel(";
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001577
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001578 if (errorCheck() != NO_ERROR) {
1579 const status_t err = errorCheck();
Colin Cross6f4f3ab2014-02-05 17:42:44 -08001580 to << "Error: " << (void*)(intptr_t)err << " \"" << strerror(-err) << "\"";
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001581 } else if (dataSize() > 0) {
1582 const uint8_t* DATA = data();
1583 to << indent << HexDump(DATA, dataSize()) << dedent;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001584 const binder_size_t* OBJS = objects();
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001585 const size_t N = objectsCount();
1586 for (size_t i=0; i<N; i++) {
1587 const flat_binder_object* flat
1588 = reinterpret_cast<const flat_binder_object*>(DATA+OBJS[i]);
1589 to << endl << "Object #" << i << " @ " << (void*)OBJS[i] << ": "
1590 << TypeCode(flat->type & 0x7f7f7f00)
1591 << " = " << flat->binder;
1592 }
1593 } else {
1594 to << "NULL";
1595 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001596
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001597 to << ")";
1598}
1599
1600void Parcel::releaseObjects()
1601{
1602 const sp<ProcessState> proc(ProcessState::self());
1603 size_t i = mObjectsSize;
1604 uint8_t* const data = mData;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001605 binder_size_t* const objects = mObjects;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001606 while (i > 0) {
1607 i--;
1608 const flat_binder_object* flat
1609 = reinterpret_cast<flat_binder_object*>(data+objects[i]);
1610 release_object(proc, *flat, this);
1611 }
1612}
1613
1614void Parcel::acquireObjects()
1615{
1616 const sp<ProcessState> proc(ProcessState::self());
1617 size_t i = mObjectsSize;
1618 uint8_t* const data = mData;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001619 binder_size_t* const objects = mObjects;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001620 while (i > 0) {
1621 i--;
1622 const flat_binder_object* flat
1623 = reinterpret_cast<flat_binder_object*>(data+objects[i]);
1624 acquire_object(proc, *flat, this);
1625 }
1626}
1627
1628void Parcel::freeData()
1629{
1630 freeDataNoInit();
1631 initState();
1632}
1633
1634void Parcel::freeDataNoInit()
1635{
1636 if (mOwner) {
Dianne Hackborn7e790af2014-11-11 12:22:53 -08001637 LOG_ALLOC("Parcel %p: freeing other owner data", this);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001638 //ALOGI("Freeing data ref of %p (pid=%d)", this, getpid());
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001639 mOwner(this, mData, mDataSize, mObjects, mObjectsSize, mOwnerCookie);
1640 } else {
Dianne Hackborn7e790af2014-11-11 12:22:53 -08001641 LOG_ALLOC("Parcel %p: freeing allocated data", this);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001642 releaseObjects();
Dianne Hackborn7e790af2014-11-11 12:22:53 -08001643 if (mData) {
1644 LOG_ALLOC("Parcel %p: freeing with %zu capacity", this, mDataCapacity);
Dianne Hackborna4cff882014-11-13 17:07:40 -08001645 pthread_mutex_lock(&gParcelGlobalAllocSizeLock);
Dan Austin48fd7b42015-09-10 13:46:02 -07001646 if (mDataCapacity <= gParcelGlobalAllocSize) {
1647 gParcelGlobalAllocSize = gParcelGlobalAllocSize - mDataCapacity;
1648 } else {
1649 gParcelGlobalAllocSize = 0;
1650 }
1651 if (gParcelGlobalAllocCount > 0) {
1652 gParcelGlobalAllocCount--;
1653 }
Dianne Hackborna4cff882014-11-13 17:07:40 -08001654 pthread_mutex_unlock(&gParcelGlobalAllocSizeLock);
Dianne Hackborn7e790af2014-11-11 12:22:53 -08001655 free(mData);
1656 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001657 if (mObjects) free(mObjects);
1658 }
1659}
1660
1661status_t Parcel::growData(size_t len)
1662{
Nick Kralevichb6b14232015-04-02 09:36:02 -07001663 if (len > INT32_MAX) {
1664 // don't accept size_t values which may have come from an
1665 // inadvertent conversion from a negative int.
1666 return BAD_VALUE;
1667 }
1668
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001669 size_t newSize = ((mDataSize+len)*3)/2;
1670 return (newSize <= mDataSize)
1671 ? (status_t) NO_MEMORY
1672 : continueWrite(newSize);
1673}
1674
1675status_t Parcel::restartWrite(size_t desired)
1676{
Nick Kralevichb6b14232015-04-02 09:36:02 -07001677 if (desired > INT32_MAX) {
1678 // don't accept size_t values which may have come from an
1679 // inadvertent conversion from a negative int.
1680 return BAD_VALUE;
1681 }
1682
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001683 if (mOwner) {
1684 freeData();
1685 return continueWrite(desired);
1686 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001687
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001688 uint8_t* data = (uint8_t*)realloc(mData, desired);
1689 if (!data && desired > mDataCapacity) {
1690 mError = NO_MEMORY;
1691 return NO_MEMORY;
1692 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001693
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001694 releaseObjects();
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001695
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001696 if (data) {
Dianne Hackborn7e790af2014-11-11 12:22:53 -08001697 LOG_ALLOC("Parcel %p: restart from %zu to %zu capacity", this, mDataCapacity, desired);
Dianne Hackborna4cff882014-11-13 17:07:40 -08001698 pthread_mutex_lock(&gParcelGlobalAllocSizeLock);
Dianne Hackborn7e790af2014-11-11 12:22:53 -08001699 gParcelGlobalAllocSize += desired;
1700 gParcelGlobalAllocSize -= mDataCapacity;
Dianne Hackborna4cff882014-11-13 17:07:40 -08001701 pthread_mutex_unlock(&gParcelGlobalAllocSizeLock);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001702 mData = data;
1703 mDataCapacity = desired;
1704 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001705
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001706 mDataSize = mDataPos = 0;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001707 ALOGV("restartWrite Setting data size of %p to %zu", this, mDataSize);
1708 ALOGV("restartWrite Setting data pos of %p to %zu", this, mDataPos);
1709
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001710 free(mObjects);
1711 mObjects = NULL;
1712 mObjectsSize = mObjectsCapacity = 0;
1713 mNextObjectHint = 0;
1714 mHasFds = false;
1715 mFdsKnown = true;
Dianne Hackborn8938ed22011-09-28 23:19:47 -04001716 mAllowFds = true;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001717
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001718 return NO_ERROR;
1719}
1720
1721status_t Parcel::continueWrite(size_t desired)
1722{
Nick Kralevichb6b14232015-04-02 09:36:02 -07001723 if (desired > INT32_MAX) {
1724 // don't accept size_t values which may have come from an
1725 // inadvertent conversion from a negative int.
1726 return BAD_VALUE;
1727 }
1728
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001729 // If shrinking, first adjust for any objects that appear
1730 // after the new data size.
1731 size_t objectsSize = mObjectsSize;
1732 if (desired < mDataSize) {
1733 if (desired == 0) {
1734 objectsSize = 0;
1735 } else {
1736 while (objectsSize > 0) {
1737 if (mObjects[objectsSize-1] < desired)
1738 break;
1739 objectsSize--;
1740 }
1741 }
1742 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001743
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001744 if (mOwner) {
1745 // If the size is going to zero, just release the owner's data.
1746 if (desired == 0) {
1747 freeData();
1748 return NO_ERROR;
1749 }
1750
1751 // If there is a different owner, we need to take
1752 // posession.
1753 uint8_t* data = (uint8_t*)malloc(desired);
1754 if (!data) {
1755 mError = NO_MEMORY;
1756 return NO_MEMORY;
1757 }
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001758 binder_size_t* objects = NULL;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001759
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001760 if (objectsSize) {
Nick Kraleviche9881a32015-04-28 16:21:30 -07001761 objects = (binder_size_t*)calloc(objectsSize, sizeof(binder_size_t));
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001762 if (!objects) {
Hyejin Kim3f727c02013-03-09 11:28:54 +09001763 free(data);
1764
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001765 mError = NO_MEMORY;
1766 return NO_MEMORY;
1767 }
1768
1769 // Little hack to only acquire references on objects
1770 // we will be keeping.
1771 size_t oldObjectsSize = mObjectsSize;
1772 mObjectsSize = objectsSize;
1773 acquireObjects();
1774 mObjectsSize = oldObjectsSize;
1775 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001776
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001777 if (mData) {
1778 memcpy(data, mData, mDataSize < desired ? mDataSize : desired);
1779 }
1780 if (objects && mObjects) {
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001781 memcpy(objects, mObjects, objectsSize*sizeof(binder_size_t));
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001782 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001783 //ALOGI("Freeing data ref of %p (pid=%d)", this, getpid());
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001784 mOwner(this, mData, mDataSize, mObjects, mObjectsSize, mOwnerCookie);
1785 mOwner = NULL;
1786
Dianne Hackborn7e790af2014-11-11 12:22:53 -08001787 LOG_ALLOC("Parcel %p: taking ownership of %zu capacity", this, desired);
Dianne Hackborna4cff882014-11-13 17:07:40 -08001788 pthread_mutex_lock(&gParcelGlobalAllocSizeLock);
Dianne Hackborn7e790af2014-11-11 12:22:53 -08001789 gParcelGlobalAllocSize += desired;
1790 gParcelGlobalAllocCount++;
Dianne Hackborna4cff882014-11-13 17:07:40 -08001791 pthread_mutex_unlock(&gParcelGlobalAllocSizeLock);
Dianne Hackborn7e790af2014-11-11 12:22:53 -08001792
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001793 mData = data;
1794 mObjects = objects;
1795 mDataSize = (mDataSize < desired) ? mDataSize : desired;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001796 ALOGV("continueWrite Setting data size of %p to %zu", this, mDataSize);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001797 mDataCapacity = desired;
1798 mObjectsSize = mObjectsCapacity = objectsSize;
1799 mNextObjectHint = 0;
1800
1801 } else if (mData) {
1802 if (objectsSize < mObjectsSize) {
1803 // Need to release refs on any objects we are dropping.
1804 const sp<ProcessState> proc(ProcessState::self());
1805 for (size_t i=objectsSize; i<mObjectsSize; i++) {
1806 const flat_binder_object* flat
1807 = reinterpret_cast<flat_binder_object*>(mData+mObjects[i]);
1808 if (flat->type == BINDER_TYPE_FD) {
1809 // will need to rescan because we may have lopped off the only FDs
1810 mFdsKnown = false;
1811 }
1812 release_object(proc, *flat, this);
1813 }
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001814 binder_size_t* objects =
1815 (binder_size_t*)realloc(mObjects, objectsSize*sizeof(binder_size_t));
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001816 if (objects) {
1817 mObjects = objects;
1818 }
1819 mObjectsSize = objectsSize;
1820 mNextObjectHint = 0;
1821 }
1822
1823 // We own the data, so we can just do a realloc().
1824 if (desired > mDataCapacity) {
1825 uint8_t* data = (uint8_t*)realloc(mData, desired);
1826 if (data) {
Dianne Hackborn7e790af2014-11-11 12:22:53 -08001827 LOG_ALLOC("Parcel %p: continue from %zu to %zu capacity", this, mDataCapacity,
1828 desired);
Dianne Hackborna4cff882014-11-13 17:07:40 -08001829 pthread_mutex_lock(&gParcelGlobalAllocSizeLock);
Dianne Hackborn7e790af2014-11-11 12:22:53 -08001830 gParcelGlobalAllocSize += desired;
1831 gParcelGlobalAllocSize -= mDataCapacity;
Dan Austin48fd7b42015-09-10 13:46:02 -07001832 gParcelGlobalAllocCount++;
Dianne Hackborna4cff882014-11-13 17:07:40 -08001833 pthread_mutex_unlock(&gParcelGlobalAllocSizeLock);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001834 mData = data;
1835 mDataCapacity = desired;
1836 } else if (desired > mDataCapacity) {
1837 mError = NO_MEMORY;
1838 return NO_MEMORY;
1839 }
1840 } else {
Dianne Hackborn97e2bcd2011-04-13 18:15:56 -07001841 if (mDataSize > desired) {
1842 mDataSize = desired;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001843 ALOGV("continueWrite Setting data size of %p to %zu", this, mDataSize);
Dianne Hackborn97e2bcd2011-04-13 18:15:56 -07001844 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001845 if (mDataPos > desired) {
1846 mDataPos = desired;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001847 ALOGV("continueWrite Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001848 }
1849 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001850
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001851 } else {
1852 // This is the first data. Easy!
1853 uint8_t* data = (uint8_t*)malloc(desired);
1854 if (!data) {
1855 mError = NO_MEMORY;
1856 return NO_MEMORY;
1857 }
Hyejin Kim3f727c02013-03-09 11:28:54 +09001858
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001859 if(!(mDataCapacity == 0 && mObjects == NULL
1860 && mObjectsCapacity == 0)) {
Colin Cross6f4f3ab2014-02-05 17:42:44 -08001861 ALOGE("continueWrite: %zu/%p/%zu/%zu", mDataCapacity, mObjects, mObjectsCapacity, desired);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001862 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001863
Dianne Hackborn7e790af2014-11-11 12:22:53 -08001864 LOG_ALLOC("Parcel %p: allocating with %zu capacity", this, desired);
Dianne Hackborna4cff882014-11-13 17:07:40 -08001865 pthread_mutex_lock(&gParcelGlobalAllocSizeLock);
Dianne Hackborn7e790af2014-11-11 12:22:53 -08001866 gParcelGlobalAllocSize += desired;
1867 gParcelGlobalAllocCount++;
Dianne Hackborna4cff882014-11-13 17:07:40 -08001868 pthread_mutex_unlock(&gParcelGlobalAllocSizeLock);
Dianne Hackborn7e790af2014-11-11 12:22:53 -08001869
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001870 mData = data;
1871 mDataSize = mDataPos = 0;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001872 ALOGV("continueWrite Setting data size of %p to %zu", this, mDataSize);
1873 ALOGV("continueWrite Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001874 mDataCapacity = desired;
1875 }
1876
1877 return NO_ERROR;
1878}
1879
1880void Parcel::initState()
1881{
Dianne Hackborn7e790af2014-11-11 12:22:53 -08001882 LOG_ALLOC("Parcel %p: initState", this);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001883 mError = NO_ERROR;
1884 mData = 0;
1885 mDataSize = 0;
1886 mDataCapacity = 0;
1887 mDataPos = 0;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001888 ALOGV("initState Setting data size of %p to %zu", this, mDataSize);
1889 ALOGV("initState Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001890 mObjects = NULL;
1891 mObjectsSize = 0;
1892 mObjectsCapacity = 0;
1893 mNextObjectHint = 0;
1894 mHasFds = false;
1895 mFdsKnown = true;
Dianne Hackborn8938ed22011-09-28 23:19:47 -04001896 mAllowFds = true;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001897 mOwner = NULL;
Dan Sandleraa5c2342015-04-10 10:08:45 -04001898 mBlobAshmemSize = 0;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001899}
1900
1901void Parcel::scanForFds() const
1902{
1903 bool hasFds = false;
1904 for (size_t i=0; i<mObjectsSize; i++) {
1905 const flat_binder_object* flat
1906 = reinterpret_cast<const flat_binder_object*>(mData + mObjects[i]);
1907 if (flat->type == BINDER_TYPE_FD) {
1908 hasFds = true;
1909 break;
1910 }
1911 }
1912 mHasFds = hasFds;
1913 mFdsKnown = true;
1914}
1915
Dan Sandleraa5c2342015-04-10 10:08:45 -04001916size_t Parcel::getBlobAshmemSize() const
1917{
1918 return mBlobAshmemSize;
1919}
1920
Jeff Brown5707dbf2011-09-23 21:17:56 -07001921// --- Parcel::Blob ---
1922
1923Parcel::Blob::Blob() :
Jeff Brown13b16042014-11-11 16:44:25 -08001924 mFd(-1), mData(NULL), mSize(0), mMutable(false) {
Jeff Brown5707dbf2011-09-23 21:17:56 -07001925}
1926
1927Parcel::Blob::~Blob() {
1928 release();
1929}
1930
1931void Parcel::Blob::release() {
Jeff Brown13b16042014-11-11 16:44:25 -08001932 if (mFd != -1 && mData) {
Jeff Brown5707dbf2011-09-23 21:17:56 -07001933 ::munmap(mData, mSize);
1934 }
1935 clear();
1936}
1937
Jeff Brown13b16042014-11-11 16:44:25 -08001938void Parcel::Blob::init(int fd, void* data, size_t size, bool isMutable) {
1939 mFd = fd;
Jeff Brown5707dbf2011-09-23 21:17:56 -07001940 mData = data;
1941 mSize = size;
Jeff Brown13b16042014-11-11 16:44:25 -08001942 mMutable = isMutable;
Jeff Brown5707dbf2011-09-23 21:17:56 -07001943}
1944
1945void Parcel::Blob::clear() {
Jeff Brown13b16042014-11-11 16:44:25 -08001946 mFd = -1;
Jeff Brown5707dbf2011-09-23 21:17:56 -07001947 mData = NULL;
1948 mSize = 0;
Jeff Brown13b16042014-11-11 16:44:25 -08001949 mMutable = false;
Jeff Brown5707dbf2011-09-23 21:17:56 -07001950}
1951
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001952}; // namespace android