blob: db1fc5c2651dc3d58eb71627b8727e4d7008c47b [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{
353 // TODO: decide what to do about the possibility that this can
354 // report an available-data size that exceeds a Java int's max
355 // positive value, causing havoc. Fortunately this will only
356 // happen if someone constructs a Parcel containing more than two
357 // gigabytes of data, which on typical phone hardware is simply
358 // not possible.
359 return dataSize() - dataPosition();
360}
361
362size_t Parcel::dataPosition() const
363{
364 return mDataPos;
365}
366
367size_t Parcel::dataCapacity() const
368{
369 return mDataCapacity;
370}
371
372status_t Parcel::setDataSize(size_t size)
373{
Nick Kralevichb6b14232015-04-02 09:36:02 -0700374 if (size > INT32_MAX) {
375 // don't accept size_t values which may have come from an
376 // inadvertent conversion from a negative int.
377 return BAD_VALUE;
378 }
379
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700380 status_t err;
381 err = continueWrite(size);
382 if (err == NO_ERROR) {
383 mDataSize = size;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700384 ALOGV("setDataSize Setting data size of %p to %zu", this, mDataSize);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700385 }
386 return err;
387}
388
389void Parcel::setDataPosition(size_t pos) const
390{
Nick Kralevichb6b14232015-04-02 09:36:02 -0700391 if (pos > INT32_MAX) {
392 // don't accept size_t values which may have come from an
393 // inadvertent conversion from a negative int.
394 abort();
395 }
396
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700397 mDataPos = pos;
398 mNextObjectHint = 0;
399}
400
401status_t Parcel::setDataCapacity(size_t size)
402{
Nick Kralevichb6b14232015-04-02 09:36:02 -0700403 if (size > INT32_MAX) {
404 // don't accept size_t values which may have come from an
405 // inadvertent conversion from a negative int.
406 return BAD_VALUE;
407 }
408
Dianne Hackborn97e2bcd2011-04-13 18:15:56 -0700409 if (size > mDataCapacity) return continueWrite(size);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700410 return NO_ERROR;
411}
412
413status_t Parcel::setData(const uint8_t* buffer, size_t len)
414{
Nick Kralevichb6b14232015-04-02 09:36:02 -0700415 if (len > INT32_MAX) {
416 // don't accept size_t values which may have come from an
417 // inadvertent conversion from a negative int.
418 return BAD_VALUE;
419 }
420
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700421 status_t err = restartWrite(len);
422 if (err == NO_ERROR) {
423 memcpy(const_cast<uint8_t*>(data()), buffer, len);
424 mDataSize = len;
425 mFdsKnown = false;
426 }
427 return err;
428}
429
Andreas Huber51faf462011-04-13 10:21:56 -0700430status_t Parcel::appendFrom(const Parcel *parcel, size_t offset, size_t len)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700431{
432 const sp<ProcessState> proc(ProcessState::self());
433 status_t err;
Andreas Huber51faf462011-04-13 10:21:56 -0700434 const uint8_t *data = parcel->mData;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800435 const binder_size_t *objects = parcel->mObjects;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700436 size_t size = parcel->mObjectsSize;
437 int startPos = mDataPos;
438 int firstIndex = -1, lastIndex = -2;
439
440 if (len == 0) {
441 return NO_ERROR;
442 }
443
Nick Kralevichb6b14232015-04-02 09:36:02 -0700444 if (len > INT32_MAX) {
445 // don't accept size_t values which may have come from an
446 // inadvertent conversion from a negative int.
447 return BAD_VALUE;
448 }
449
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700450 // range checks against the source parcel size
451 if ((offset > parcel->mDataSize)
452 || (len > parcel->mDataSize)
453 || (offset + len > parcel->mDataSize)) {
454 return BAD_VALUE;
455 }
456
457 // Count objects in range
458 for (int i = 0; i < (int) size; i++) {
459 size_t off = objects[i];
Christopher Tate27182be2015-05-27 17:53:02 -0700460 if ((off >= offset) && (off + sizeof(flat_binder_object) <= offset + len)) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700461 if (firstIndex == -1) {
462 firstIndex = i;
463 }
464 lastIndex = i;
465 }
466 }
467 int numObjects = lastIndex - firstIndex + 1;
468
Dianne Hackborn97e2bcd2011-04-13 18:15:56 -0700469 if ((mDataSize+len) > mDataCapacity) {
470 // grow data
471 err = growData(len);
472 if (err != NO_ERROR) {
473 return err;
474 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700475 }
476
477 // append data
478 memcpy(mData + mDataPos, data + offset, len);
479 mDataPos += len;
480 mDataSize += len;
481
Dianne Hackborn8938ed22011-09-28 23:19:47 -0400482 err = NO_ERROR;
483
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700484 if (numObjects > 0) {
485 // grow objects
486 if (mObjectsCapacity < mObjectsSize + numObjects) {
Christopher Tateed7a50c2015-06-08 14:45:14 -0700487 size_t newSize = ((mObjectsSize + numObjects)*3)/2;
488 if (newSize < mObjectsSize) return NO_MEMORY; // overflow
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800489 binder_size_t *objects =
490 (binder_size_t*)realloc(mObjects, newSize*sizeof(binder_size_t));
491 if (objects == (binder_size_t*)0) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700492 return NO_MEMORY;
493 }
494 mObjects = objects;
495 mObjectsCapacity = newSize;
496 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700497
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700498 // append and acquire objects
499 int idx = mObjectsSize;
500 for (int i = firstIndex; i <= lastIndex; i++) {
501 size_t off = objects[i] - offset + startPos;
502 mObjects[idx++] = off;
503 mObjectsSize++;
504
Dianne Hackborn8af0f822009-05-22 13:20:23 -0700505 flat_binder_object* flat
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700506 = reinterpret_cast<flat_binder_object*>(mData + off);
507 acquire_object(proc, *flat, this);
508
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700509 if (flat->type == BINDER_TYPE_FD) {
Dianne Hackborn8af0f822009-05-22 13:20:23 -0700510 // If this is a file descriptor, we need to dup it so the
511 // new Parcel now owns its own fd, and can declare that we
512 // officially know we have fds.
513 flat->handle = dup(flat->handle);
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800514 flat->cookie = 1;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700515 mHasFds = mFdsKnown = true;
Dianne Hackborn8938ed22011-09-28 23:19:47 -0400516 if (!mAllowFds) {
517 err = FDS_NOT_ALLOWED;
518 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700519 }
520 }
521 }
522
Dianne Hackborn8938ed22011-09-28 23:19:47 -0400523 return err;
524}
525
Jeff Brown13b16042014-11-11 16:44:25 -0800526bool Parcel::allowFds() const
527{
528 return mAllowFds;
529}
530
Dianne Hackborn7746cc32011-10-03 21:09:35 -0700531bool Parcel::pushAllowFds(bool allowFds)
Dianne Hackborn8938ed22011-09-28 23:19:47 -0400532{
533 const bool origValue = mAllowFds;
Dianne Hackborn7746cc32011-10-03 21:09:35 -0700534 if (!allowFds) {
535 mAllowFds = false;
536 }
Dianne Hackborn8938ed22011-09-28 23:19:47 -0400537 return origValue;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700538}
539
Dianne Hackborn7746cc32011-10-03 21:09:35 -0700540void Parcel::restoreAllowFds(bool lastValue)
541{
542 mAllowFds = lastValue;
543}
544
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700545bool Parcel::hasFileDescriptors() const
546{
547 if (!mFdsKnown) {
548 scanForFds();
549 }
550 return mHasFds;
551}
552
Brad Fitzpatrick702ea9d2010-06-18 13:07:53 -0700553// Write RPC headers. (previously just the interface token)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700554status_t Parcel::writeInterfaceToken(const String16& interface)
555{
Brad Fitzpatricka877cd82010-07-07 16:06:39 -0700556 writeInt32(IPCThreadState::self()->getStrictModePolicy() |
557 STRICT_MODE_PENALTY_GATHER);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700558 // currently the interface identification token is just its name as a string
559 return writeString16(interface);
560}
561
Mathias Agopian83c04462009-05-22 19:00:22 -0700562bool Parcel::checkInterface(IBinder* binder) const
563{
Brad Fitzpatrick702ea9d2010-06-18 13:07:53 -0700564 return enforceInterface(binder->getInterfaceDescriptor());
Mathias Agopian83c04462009-05-22 19:00:22 -0700565}
566
Brad Fitzpatricka877cd82010-07-07 16:06:39 -0700567bool Parcel::enforceInterface(const String16& interface,
Brad Fitzpatrick70081a12010-07-27 09:49:11 -0700568 IPCThreadState* threadState) const
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700569{
Brad Fitzpatrick70081a12010-07-27 09:49:11 -0700570 int32_t strictPolicy = readInt32();
571 if (threadState == NULL) {
572 threadState = IPCThreadState::self();
Brad Fitzpatricka877cd82010-07-07 16:06:39 -0700573 }
Brad Fitzpatrick52736032010-08-30 16:01:16 -0700574 if ((threadState->getLastTransactionBinderFlags() &
575 IBinder::FLAG_ONEWAY) != 0) {
576 // For one-way calls, the callee is running entirely
577 // disconnected from the caller, so disable StrictMode entirely.
578 // Not only does disk/network usage not impact the caller, but
579 // there's no way to commuicate back any violations anyway.
580 threadState->setStrictModePolicy(0);
581 } else {
582 threadState->setStrictModePolicy(strictPolicy);
583 }
Mathias Agopian83c04462009-05-22 19:00:22 -0700584 const String16 str(readString16());
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700585 if (str == interface) {
586 return true;
587 } else {
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700588 ALOGW("**** enforceInterface() expected '%s' but read '%s'",
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700589 String8(interface).string(), String8(str).string());
590 return false;
591 }
Brad Fitzpatrick702ea9d2010-06-18 13:07:53 -0700592}
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700593
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800594const binder_size_t* Parcel::objects() const
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700595{
596 return mObjects;
597}
598
599size_t Parcel::objectsCount() const
600{
601 return mObjectsSize;
602}
603
604status_t Parcel::errorCheck() const
605{
606 return mError;
607}
608
609void Parcel::setError(status_t err)
610{
611 mError = err;
612}
613
614status_t Parcel::finishWrite(size_t len)
615{
Nick Kralevichb6b14232015-04-02 09:36:02 -0700616 if (len > INT32_MAX) {
617 // don't accept size_t values which may have come from an
618 // inadvertent conversion from a negative int.
619 return BAD_VALUE;
620 }
621
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700622 //printf("Finish write of %d\n", len);
623 mDataPos += len;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700624 ALOGV("finishWrite Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700625 if (mDataPos > mDataSize) {
626 mDataSize = mDataPos;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700627 ALOGV("finishWrite Setting data size of %p to %zu", this, mDataSize);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700628 }
629 //printf("New pos=%d, size=%d\n", mDataPos, mDataSize);
630 return NO_ERROR;
631}
632
633status_t Parcel::writeUnpadded(const void* data, size_t len)
634{
Nick Kralevichb6b14232015-04-02 09:36:02 -0700635 if (len > INT32_MAX) {
636 // don't accept size_t values which may have come from an
637 // inadvertent conversion from a negative int.
638 return BAD_VALUE;
639 }
640
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700641 size_t end = mDataPos + len;
642 if (end < mDataPos) {
643 // integer overflow
644 return BAD_VALUE;
645 }
646
647 if (end <= mDataCapacity) {
648restart_write:
649 memcpy(mData+mDataPos, data, len);
650 return finishWrite(len);
651 }
652
653 status_t err = growData(len);
654 if (err == NO_ERROR) goto restart_write;
655 return err;
656}
657
658status_t Parcel::write(const void* data, size_t len)
659{
Nick Kralevichb6b14232015-04-02 09:36:02 -0700660 if (len > INT32_MAX) {
661 // don't accept size_t values which may have come from an
662 // inadvertent conversion from a negative int.
663 return BAD_VALUE;
664 }
665
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700666 void* const d = writeInplace(len);
667 if (d) {
668 memcpy(d, data, len);
669 return NO_ERROR;
670 }
671 return mError;
672}
673
674void* Parcel::writeInplace(size_t len)
675{
Nick Kralevichb6b14232015-04-02 09:36:02 -0700676 if (len > INT32_MAX) {
677 // don't accept size_t values which may have come from an
678 // inadvertent conversion from a negative int.
679 return NULL;
680 }
681
682 const size_t padded = pad_size(len);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700683
684 // sanity check for integer overflow
685 if (mDataPos+padded < mDataPos) {
686 return NULL;
687 }
688
689 if ((mDataPos+padded) <= mDataCapacity) {
690restart_write:
691 //printf("Writing %ld bytes, padded to %ld\n", len, padded);
692 uint8_t* const data = mData+mDataPos;
693
694 // Need to pad at end?
695 if (padded != len) {
696#if BYTE_ORDER == BIG_ENDIAN
697 static const uint32_t mask[4] = {
698 0x00000000, 0xffffff00, 0xffff0000, 0xff000000
699 };
700#endif
701#if BYTE_ORDER == LITTLE_ENDIAN
702 static const uint32_t mask[4] = {
703 0x00000000, 0x00ffffff, 0x0000ffff, 0x000000ff
704 };
705#endif
706 //printf("Applying pad mask: %p to %p\n", (void*)mask[padded-len],
707 // *reinterpret_cast<void**>(data+padded-4));
708 *reinterpret_cast<uint32_t*>(data+padded-4) &= mask[padded-len];
709 }
710
711 finishWrite(padded);
712 return data;
713 }
714
715 status_t err = growData(padded);
716 if (err == NO_ERROR) goto restart_write;
717 return NULL;
718}
719
Casey Dahlin451ff582015-10-19 18:12:18 -0700720status_t Parcel::writeByteVector(const std::vector<int8_t>& val)
721{
Christopher Wileyf0fc52b2015-10-31 13:22:15 -0700722 status_t status;
Casey Dahlin451ff582015-10-19 18:12:18 -0700723 if (val.size() > std::numeric_limits<int32_t>::max()) {
Christopher Wileyf0fc52b2015-10-31 13:22:15 -0700724 status = BAD_VALUE;
725 return status;
Casey Dahlin451ff582015-10-19 18:12:18 -0700726 }
727
Christopher Wileyf0fc52b2015-10-31 13:22:15 -0700728 status = writeInt32(val.size());
Casey Dahlin451ff582015-10-19 18:12:18 -0700729 if (status != OK) {
730 return status;
731 }
732
Christopher Wileyf0fc52b2015-10-31 13:22:15 -0700733 void* data = writeInplace(val.size());
734 if (!data) {
735 status = BAD_VALUE;
736 return status;
Casey Dahlin451ff582015-10-19 18:12:18 -0700737 }
738
Christopher Wileyf0fc52b2015-10-31 13:22:15 -0700739 memcpy(data, val.data(), val.size());
740 return status;
Casey Dahlin451ff582015-10-19 18:12:18 -0700741}
742
743status_t Parcel::writeInt32Vector(const std::vector<int32_t>& val)
744{
745 if (val.size() > std::numeric_limits<int32_t>::max()) {
746 return BAD_VALUE;
747 }
748
749 status_t status = writeInt32(val.size());
750
751 if (status != OK) {
752 return status;
753 }
754
755 for (const auto& item : val) {
756 status = writeInt32(item);
757
758 if (status != OK) {
759 return status;
760 }
761 }
762
763 return OK;
764}
765
766status_t Parcel::writeInt64Vector(const std::vector<int64_t>& val)
767{
768 if (val.size() > std::numeric_limits<int32_t>::max()) {
769 return BAD_VALUE;
770 }
771
772 status_t status = writeInt32(val.size());
773
774 if (status != OK) {
775 return status;
776 }
777
778 for (const auto& item : val) {
779 status = writeInt64(item);
780
781 if (status != OK) {
782 return status;
783 }
784 }
785
786 return OK;
787}
788
789status_t Parcel::writeFloatVector(const std::vector<float>& val)
790{
791 if (val.size() > std::numeric_limits<int32_t>::max()) {
792 return BAD_VALUE;
793 }
794
795 status_t status = writeInt32(val.size());
796
797 if (status != OK) {
798 return status;
799 }
800
801 for (const auto& item : val) {
802 status = writeFloat(item);
803
804 if (status != OK) {
805 return status;
806 }
807 }
808
809 return OK;
810}
811
812status_t Parcel::writeDoubleVector(const std::vector<double>& val)
813{
814 if (val.size() > std::numeric_limits<int32_t>::max()) {
815 return BAD_VALUE;
816 }
817
818 status_t status = writeInt32(val.size());
819
820 if (status != OK) {
821 return status;
822 }
823
824 for (const auto& item : val) {
825 status = writeDouble(item);
826
827 if (status != OK) {
828 return status;
829 }
830 }
831
832 return OK;
833}
834
835status_t Parcel::writeBoolVector(const std::vector<bool>& val)
836{
837 if (val.size() > std::numeric_limits<int32_t>::max()) {
838 return BAD_VALUE;
839 }
840
841 status_t status = writeInt32(val.size());
842
843 if (status != OK) {
844 return status;
845 }
846
847 for (const auto& item : val) {
848 status = writeBool(item);
849
850 if (status != OK) {
851 return status;
852 }
853 }
854
855 return OK;
856}
857
858status_t Parcel::writeCharVector(const std::vector<char16_t>& val)
859{
860 if (val.size() > std::numeric_limits<int32_t>::max()) {
861 return BAD_VALUE;
862 }
863
864 status_t status = writeInt32(val.size());
865
866 if (status != OK) {
867 return status;
868 }
869
870 for (const auto& item : val) {
871 status = writeChar(item);
872
873 if (status != OK) {
874 return status;
875 }
876 }
877
878 return OK;
879}
880
881status_t Parcel::writeString16Vector(const std::vector<String16>& val)
882{
883 if (val.size() > std::numeric_limits<int32_t>::max()) {
884 return BAD_VALUE;
885 }
886
887 status_t status = writeInt32(val.size());
888
889 if (status != OK) {
890 return status;
891 }
892
893 for (const auto& item : val) {
894 status = writeString16(item);
895
896 if (status != OK) {
897 return status;
898 }
899 }
900
901 return OK;
902}
903
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700904status_t Parcel::writeInt32(int32_t val)
905{
Andreas Huber84a6d042009-08-17 13:33:27 -0700906 return writeAligned(val);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700907}
Dan Stoza41a0f2f2014-12-01 10:01:10 -0800908
909status_t Parcel::writeUint32(uint32_t val)
910{
911 return writeAligned(val);
912}
913
Marco Nelissen5c0106e2013-10-16 10:57:51 -0700914status_t Parcel::writeInt32Array(size_t len, const int32_t *val) {
Nick Kralevichb6b14232015-04-02 09:36:02 -0700915 if (len > INT32_MAX) {
916 // don't accept size_t values which may have come from an
917 // inadvertent conversion from a negative int.
918 return BAD_VALUE;
919 }
920
Marco Nelissen5c0106e2013-10-16 10:57:51 -0700921 if (!val) {
Chad Brubakere59cb432015-06-30 14:03:55 -0700922 return writeInt32(-1);
Marco Nelissen5c0106e2013-10-16 10:57:51 -0700923 }
Chad Brubakere59cb432015-06-30 14:03:55 -0700924 status_t ret = writeInt32(static_cast<uint32_t>(len));
Marco Nelissen5c0106e2013-10-16 10:57:51 -0700925 if (ret == NO_ERROR) {
926 ret = write(val, len * sizeof(*val));
927 }
928 return ret;
929}
Marco Nelissenf0190bf2014-03-13 14:17:40 -0700930status_t Parcel::writeByteArray(size_t len, const uint8_t *val) {
Nick Kralevichb6b14232015-04-02 09:36:02 -0700931 if (len > INT32_MAX) {
932 // don't accept size_t values which may have come from an
933 // inadvertent conversion from a negative int.
934 return BAD_VALUE;
935 }
936
Marco Nelissenf0190bf2014-03-13 14:17:40 -0700937 if (!val) {
Chad Brubakere59cb432015-06-30 14:03:55 -0700938 return writeInt32(-1);
Marco Nelissenf0190bf2014-03-13 14:17:40 -0700939 }
Chad Brubakere59cb432015-06-30 14:03:55 -0700940 status_t ret = writeInt32(static_cast<uint32_t>(len));
Marco Nelissenf0190bf2014-03-13 14:17:40 -0700941 if (ret == NO_ERROR) {
942 ret = write(val, len * sizeof(*val));
943 }
944 return ret;
945}
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700946
Casey Dahlind6848f52015-10-15 15:44:59 -0700947status_t Parcel::writeBool(bool val)
948{
949 return writeInt32(int32_t(val));
950}
951
952status_t Parcel::writeChar(char16_t val)
953{
954 return writeInt32(int32_t(val));
955}
956
957status_t Parcel::writeByte(int8_t val)
958{
959 return writeInt32(int32_t(val));
960}
961
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700962status_t Parcel::writeInt64(int64_t val)
963{
Andreas Huber84a6d042009-08-17 13:33:27 -0700964 return writeAligned(val);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700965}
966
Ronghua Wu2d13afd2015-03-16 11:11:07 -0700967status_t Parcel::writeUint64(uint64_t val)
968{
969 return writeAligned(val);
970}
971
Serban Constantinescuf683e012013-11-05 16:53:55 +0000972status_t Parcel::writePointer(uintptr_t val)
973{
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800974 return writeAligned<binder_uintptr_t>(val);
Serban Constantinescuf683e012013-11-05 16:53:55 +0000975}
976
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700977status_t Parcel::writeFloat(float val)
978{
Andreas Huber84a6d042009-08-17 13:33:27 -0700979 return writeAligned(val);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700980}
981
Douglas Leungcc1a4bb2013-01-11 15:00:55 -0800982#if defined(__mips__) && defined(__mips_hard_float)
983
984status_t Parcel::writeDouble(double val)
985{
986 union {
987 double d;
988 unsigned long long ll;
989 } u;
990 u.d = val;
991 return writeAligned(u.ll);
992}
993
994#else
995
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700996status_t Parcel::writeDouble(double val)
997{
Andreas Huber84a6d042009-08-17 13:33:27 -0700998 return writeAligned(val);
999}
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001000
Douglas Leungcc1a4bb2013-01-11 15:00:55 -08001001#endif
1002
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001003status_t Parcel::writeCString(const char* str)
1004{
1005 return write(str, strlen(str)+1);
1006}
1007
1008status_t Parcel::writeString8(const String8& str)
1009{
1010 status_t err = writeInt32(str.bytes());
Pravat Dalbeherad1dff8d2010-12-15 08:40:00 +01001011 // only write string if its length is more than zero characters,
1012 // as readString8 will only read if the length field is non-zero.
1013 // this is slightly different from how writeString16 works.
1014 if (str.bytes() > 0 && err == NO_ERROR) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001015 err = write(str.string(), str.bytes()+1);
1016 }
1017 return err;
1018}
1019
1020status_t Parcel::writeString16(const String16& str)
1021{
1022 return writeString16(str.string(), str.size());
1023}
1024
1025status_t Parcel::writeString16(const char16_t* str, size_t len)
1026{
1027 if (str == NULL) return writeInt32(-1);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001028
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001029 status_t err = writeInt32(len);
1030 if (err == NO_ERROR) {
1031 len *= sizeof(char16_t);
1032 uint8_t* data = (uint8_t*)writeInplace(len+sizeof(char16_t));
1033 if (data) {
1034 memcpy(data, str, len);
1035 *reinterpret_cast<char16_t*>(data+len) = 0;
1036 return NO_ERROR;
1037 }
1038 err = mError;
1039 }
1040 return err;
1041}
1042
1043status_t Parcel::writeStrongBinder(const sp<IBinder>& val)
1044{
1045 return flatten_binder(ProcessState::self(), val, this);
1046}
1047
Casey Dahlineb8e15f2015-11-03 13:50:37 -08001048status_t Parcel::writeStrongBinderVector(const std::vector<sp<IBinder>>& val)
1049{
1050 if (val.size() > std::numeric_limits<int32_t>::max()) {
1051 return BAD_VALUE;
1052 }
1053
1054 status_t status = writeInt32(val.size());
1055
1056 if (status != OK) {
1057 return status;
1058 }
1059
1060 for (const auto& item : val) {
1061 status = writeStrongBinder(item);
1062
1063 if (status != OK) {
1064 return status;
1065 }
1066 }
1067
1068 return OK;
1069}
1070
1071status_t Parcel::readStrongBinderVector(std::vector<sp<IBinder>>* val) const {
1072 val->clear();
1073
1074 int32_t size;
1075 status_t status = readInt32(&size);
1076
1077 if (status != OK) {
1078 return status;
1079 }
1080
1081 if (size < 0) {
1082 return BAD_VALUE;
1083 }
1084
1085 val->resize(size);
1086
1087 for (auto& v : *val) {
1088 status = readStrongBinder(&v);
1089
1090 if (status != OK) {
1091 return status;
1092 }
1093 }
1094
1095 return OK;
1096}
1097
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001098status_t Parcel::writeWeakBinder(const wp<IBinder>& val)
1099{
1100 return flatten_binder(ProcessState::self(), val, this);
1101}
1102
Mathias Agopiana47f02a2009-05-21 16:29:38 -07001103status_t Parcel::writeNativeHandle(const native_handle* handle)
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001104{
Mathias Agopian1d0a95b2009-07-31 16:12:13 -07001105 if (!handle || handle->version != sizeof(native_handle))
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001106 return BAD_TYPE;
1107
1108 status_t err;
Mathias Agopiana47f02a2009-05-21 16:29:38 -07001109 err = writeInt32(handle->numFds);
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001110 if (err != NO_ERROR) return err;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001111
Mathias Agopiana47f02a2009-05-21 16:29:38 -07001112 err = writeInt32(handle->numInts);
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001113 if (err != NO_ERROR) return err;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001114
Mathias Agopiana47f02a2009-05-21 16:29:38 -07001115 for (int i=0 ; err==NO_ERROR && i<handle->numFds ; i++)
1116 err = writeDupFileDescriptor(handle->data[i]);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001117
1118 if (err != NO_ERROR) {
Steve Block9d453682011-12-20 16:23:08 +00001119 ALOGD("write native handle, write dup fd failed");
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001120 return err;
1121 }
Mathias Agopiana47f02a2009-05-21 16:29:38 -07001122 err = write(handle->data + handle->numFds, sizeof(int)*handle->numInts);
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001123 return err;
1124}
1125
Jeff Brown93ff1f92011-11-04 19:01:44 -07001126status_t Parcel::writeFileDescriptor(int fd, bool takeOwnership)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001127{
1128 flat_binder_object obj;
1129 obj.type = BINDER_TYPE_FD;
1130 obj.flags = 0x7f | FLAT_BINDER_FLAG_ACCEPTS_FDS;
Arve Hjønnevåg07fd0f12014-02-18 21:10:29 -08001131 obj.binder = 0; /* Don't pass uninitialized stack data to a remote process */
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001132 obj.handle = fd;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001133 obj.cookie = takeOwnership ? 1 : 0;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001134 return writeObject(obj, true);
1135}
1136
1137status_t Parcel::writeDupFileDescriptor(int fd)
1138{
Jeff Brownd341c712011-11-04 20:19:33 -07001139 int dupFd = dup(fd);
1140 if (dupFd < 0) {
1141 return -errno;
1142 }
1143 status_t err = writeFileDescriptor(dupFd, true /*takeOwnership*/);
1144 if (err) {
1145 close(dupFd);
1146 }
1147 return err;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001148}
1149
Jeff Brown13b16042014-11-11 16:44:25 -08001150status_t Parcel::writeBlob(size_t len, bool mutableCopy, WritableBlob* outBlob)
Jeff Brown5707dbf2011-09-23 21:17:56 -07001151{
Nick Kralevichb6b14232015-04-02 09:36:02 -07001152 if (len > INT32_MAX) {
1153 // don't accept size_t values which may have come from an
1154 // inadvertent conversion from a negative int.
1155 return BAD_VALUE;
1156 }
1157
Jeff Brown13b16042014-11-11 16:44:25 -08001158 status_t status;
1159 if (!mAllowFds || len <= BLOB_INPLACE_LIMIT) {
Steve Block6807e592011-10-20 11:56:00 +01001160 ALOGV("writeBlob: write in place");
Jeff Brown13b16042014-11-11 16:44:25 -08001161 status = writeInt32(BLOB_INPLACE);
Jeff Brown5707dbf2011-09-23 21:17:56 -07001162 if (status) return status;
1163
1164 void* ptr = writeInplace(len);
1165 if (!ptr) return NO_MEMORY;
1166
Jeff Brown13b16042014-11-11 16:44:25 -08001167 outBlob->init(-1, ptr, len, false);
Jeff Brown5707dbf2011-09-23 21:17:56 -07001168 return NO_ERROR;
1169 }
1170
Steve Block6807e592011-10-20 11:56:00 +01001171 ALOGV("writeBlob: write to ashmem");
Jeff Brown5707dbf2011-09-23 21:17:56 -07001172 int fd = ashmem_create_region("Parcel Blob", len);
1173 if (fd < 0) return NO_MEMORY;
1174
Dan Sandleraa5c2342015-04-10 10:08:45 -04001175 mBlobAshmemSize += len;
1176
Jeff Brown5707dbf2011-09-23 21:17:56 -07001177 int result = ashmem_set_prot_region(fd, PROT_READ | PROT_WRITE);
1178 if (result < 0) {
Jeff Brownec4e0062011-10-10 14:50:10 -07001179 status = result;
Jeff Brown5707dbf2011-09-23 21:17:56 -07001180 } else {
1181 void* ptr = ::mmap(NULL, len, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
1182 if (ptr == MAP_FAILED) {
1183 status = -errno;
1184 } else {
Jeff Brown13b16042014-11-11 16:44:25 -08001185 if (!mutableCopy) {
1186 result = ashmem_set_prot_region(fd, PROT_READ);
1187 }
Jeff Brown5707dbf2011-09-23 21:17:56 -07001188 if (result < 0) {
Jeff Brownec4e0062011-10-10 14:50:10 -07001189 status = result;
Jeff Brown5707dbf2011-09-23 21:17:56 -07001190 } else {
Jeff Brown13b16042014-11-11 16:44:25 -08001191 status = writeInt32(mutableCopy ? BLOB_ASHMEM_MUTABLE : BLOB_ASHMEM_IMMUTABLE);
Jeff Brown5707dbf2011-09-23 21:17:56 -07001192 if (!status) {
Jeff Brown93ff1f92011-11-04 19:01:44 -07001193 status = writeFileDescriptor(fd, true /*takeOwnership*/);
Jeff Brown5707dbf2011-09-23 21:17:56 -07001194 if (!status) {
Jeff Brown13b16042014-11-11 16:44:25 -08001195 outBlob->init(fd, ptr, len, mutableCopy);
Jeff Brown5707dbf2011-09-23 21:17:56 -07001196 return NO_ERROR;
1197 }
1198 }
1199 }
1200 }
1201 ::munmap(ptr, len);
1202 }
1203 ::close(fd);
1204 return status;
1205}
1206
Jeff Brown13b16042014-11-11 16:44:25 -08001207status_t Parcel::writeDupImmutableBlobFileDescriptor(int fd)
1208{
1209 // Must match up with what's done in writeBlob.
1210 if (!mAllowFds) return FDS_NOT_ALLOWED;
1211 status_t status = writeInt32(BLOB_ASHMEM_IMMUTABLE);
1212 if (status) return status;
1213 return writeDupFileDescriptor(fd);
1214}
1215
Mathias Agopiane1424282013-07-29 21:24:40 -07001216status_t Parcel::write(const FlattenableHelperInterface& val)
Mathias Agopian98e71dd2010-02-11 17:30:52 -08001217{
1218 status_t err;
1219
1220 // size if needed
Mathias Agopiane1424282013-07-29 21:24:40 -07001221 const size_t len = val.getFlattenedSize();
1222 const size_t fd_count = val.getFdCount();
Mathias Agopian98e71dd2010-02-11 17:30:52 -08001223
Nick Kralevichb6b14232015-04-02 09:36:02 -07001224 if ((len > INT32_MAX) || (fd_count > INT32_MAX)) {
1225 // don't accept size_t values which may have come from an
1226 // inadvertent conversion from a negative int.
1227 return BAD_VALUE;
1228 }
1229
Mathias Agopian98e71dd2010-02-11 17:30:52 -08001230 err = this->writeInt32(len);
1231 if (err) return err;
1232
1233 err = this->writeInt32(fd_count);
1234 if (err) return err;
1235
1236 // payload
Nick Kralevichb6b14232015-04-02 09:36:02 -07001237 void* const buf = this->writeInplace(pad_size(len));
Mathias Agopian98e71dd2010-02-11 17:30:52 -08001238 if (buf == NULL)
1239 return BAD_VALUE;
1240
1241 int* fds = NULL;
1242 if (fd_count) {
1243 fds = new int[fd_count];
1244 }
1245
1246 err = val.flatten(buf, len, fds, fd_count);
1247 for (size_t i=0 ; i<fd_count && err==NO_ERROR ; i++) {
1248 err = this->writeDupFileDescriptor( fds[i] );
1249 }
1250
1251 if (fd_count) {
1252 delete [] fds;
1253 }
1254
1255 return err;
1256}
1257
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001258status_t Parcel::writeObject(const flat_binder_object& val, bool nullMetaData)
1259{
1260 const bool enoughData = (mDataPos+sizeof(val)) <= mDataCapacity;
1261 const bool enoughObjects = mObjectsSize < mObjectsCapacity;
1262 if (enoughData && enoughObjects) {
1263restart_write:
1264 *reinterpret_cast<flat_binder_object*>(mData+mDataPos) = val;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001265
Christopher Tate98e67d32015-06-03 18:44:15 -07001266 // remember if it's a file descriptor
1267 if (val.type == BINDER_TYPE_FD) {
1268 if (!mAllowFds) {
1269 // fail before modifying our object index
1270 return FDS_NOT_ALLOWED;
1271 }
1272 mHasFds = mFdsKnown = true;
1273 }
1274
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001275 // Need to write meta-data?
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001276 if (nullMetaData || val.binder != 0) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001277 mObjects[mObjectsSize] = mDataPos;
1278 acquire_object(ProcessState::self(), val, this);
1279 mObjectsSize++;
1280 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001281
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001282 return finishWrite(sizeof(flat_binder_object));
1283 }
1284
1285 if (!enoughData) {
1286 const status_t err = growData(sizeof(val));
1287 if (err != NO_ERROR) return err;
1288 }
1289 if (!enoughObjects) {
1290 size_t newSize = ((mObjectsSize+2)*3)/2;
Christopher Tateed7a50c2015-06-08 14:45:14 -07001291 if (newSize < mObjectsSize) return NO_MEMORY; // overflow
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001292 binder_size_t* objects = (binder_size_t*)realloc(mObjects, newSize*sizeof(binder_size_t));
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001293 if (objects == NULL) return NO_MEMORY;
1294 mObjects = objects;
1295 mObjectsCapacity = newSize;
1296 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001297
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001298 goto restart_write;
1299}
1300
Brad Fitzpatrick837a0d02010-07-13 15:33:35 -07001301status_t Parcel::writeNoException()
1302{
1303 return writeInt32(0);
1304}
1305
Colin Cross6f4f3ab2014-02-05 17:42:44 -08001306void Parcel::remove(size_t /*start*/, size_t /*amt*/)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001307{
1308 LOG_ALWAYS_FATAL("Parcel::remove() not yet implemented!");
1309}
1310
1311status_t Parcel::read(void* outData, size_t len) const
1312{
Nick Kralevichb6b14232015-04-02 09:36:02 -07001313 if (len > INT32_MAX) {
1314 // don't accept size_t values which may have come from an
1315 // inadvertent conversion from a negative int.
1316 return BAD_VALUE;
1317 }
1318
1319 if ((mDataPos+pad_size(len)) >= mDataPos && (mDataPos+pad_size(len)) <= mDataSize
1320 && len <= pad_size(len)) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001321 memcpy(outData, mData+mDataPos, len);
Nick Kralevichb6b14232015-04-02 09:36:02 -07001322 mDataPos += pad_size(len);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001323 ALOGV("read Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001324 return NO_ERROR;
1325 }
1326 return NOT_ENOUGH_DATA;
1327}
1328
1329const void* Parcel::readInplace(size_t len) const
1330{
Nick Kralevichb6b14232015-04-02 09:36:02 -07001331 if (len > INT32_MAX) {
1332 // don't accept size_t values which may have come from an
1333 // inadvertent conversion from a negative int.
1334 return NULL;
1335 }
1336
1337 if ((mDataPos+pad_size(len)) >= mDataPos && (mDataPos+pad_size(len)) <= mDataSize
1338 && len <= pad_size(len)) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001339 const void* data = mData+mDataPos;
Nick Kralevichb6b14232015-04-02 09:36:02 -07001340 mDataPos += pad_size(len);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001341 ALOGV("readInplace Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001342 return data;
1343 }
1344 return NULL;
1345}
1346
Andreas Huber84a6d042009-08-17 13:33:27 -07001347template<class T>
1348status_t Parcel::readAligned(T *pArg) const {
Nick Kralevichb6b14232015-04-02 09:36:02 -07001349 COMPILE_TIME_ASSERT_FUNCTION_SCOPE(PAD_SIZE_UNSAFE(sizeof(T)) == sizeof(T));
Andreas Huber84a6d042009-08-17 13:33:27 -07001350
1351 if ((mDataPos+sizeof(T)) <= mDataSize) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001352 const void* data = mData+mDataPos;
Andreas Huber84a6d042009-08-17 13:33:27 -07001353 mDataPos += sizeof(T);
1354 *pArg = *reinterpret_cast<const T*>(data);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001355 return NO_ERROR;
1356 } else {
1357 return NOT_ENOUGH_DATA;
1358 }
1359}
1360
Andreas Huber84a6d042009-08-17 13:33:27 -07001361template<class T>
1362T Parcel::readAligned() const {
1363 T result;
1364 if (readAligned(&result) != NO_ERROR) {
1365 result = 0;
1366 }
1367
1368 return result;
1369}
1370
1371template<class T>
1372status_t Parcel::writeAligned(T val) {
Nick Kralevichb6b14232015-04-02 09:36:02 -07001373 COMPILE_TIME_ASSERT_FUNCTION_SCOPE(PAD_SIZE_UNSAFE(sizeof(T)) == sizeof(T));
Andreas Huber84a6d042009-08-17 13:33:27 -07001374
1375 if ((mDataPos+sizeof(val)) <= mDataCapacity) {
1376restart_write:
1377 *reinterpret_cast<T*>(mData+mDataPos) = val;
1378 return finishWrite(sizeof(val));
1379 }
1380
1381 status_t err = growData(sizeof(val));
1382 if (err == NO_ERROR) goto restart_write;
1383 return err;
1384}
1385
Casey Dahlin451ff582015-10-19 18:12:18 -07001386status_t Parcel::readByteVector(std::vector<int8_t>* val) const {
1387 val->clear();
1388
1389 int32_t size;
1390 status_t status = readInt32(&size);
1391
1392 if (status != OK) {
1393 return status;
1394 }
1395
Christopher Wileyf0fc52b2015-10-31 13:22:15 -07001396 if (size < 0 || size_t(size) > dataAvail()) {
1397 status = BAD_VALUE;
1398 return status;
Casey Dahlin451ff582015-10-19 18:12:18 -07001399 }
Christopher Wileyf0fc52b2015-10-31 13:22:15 -07001400 const void* data = readInplace(size);
1401 if (!data) {
1402 status = BAD_VALUE;
1403 return status;
1404 }
Casey Dahlin451ff582015-10-19 18:12:18 -07001405 val->resize(size);
Christopher Wileyf0fc52b2015-10-31 13:22:15 -07001406 memcpy(val->data(), data, size);
Casey Dahlin451ff582015-10-19 18:12:18 -07001407
Christopher Wileyf0fc52b2015-10-31 13:22:15 -07001408 return status;
Casey Dahlin451ff582015-10-19 18:12:18 -07001409}
1410
1411status_t Parcel::readInt32Vector(std::vector<int32_t>* val) const {
1412 val->clear();
1413
1414 int32_t size;
1415 status_t status = readInt32(&size);
1416
1417 if (status != OK) {
1418 return status;
1419 }
1420
1421 if (size < 0) {
1422 return BAD_VALUE;
1423 }
1424
1425 val->resize(size);
1426
1427 for (auto& v: *val) {
1428 status = readInt32(&v);
1429
1430 if (status != OK) {
1431 return status;
1432 }
1433 }
1434
1435 return OK;
1436}
1437
1438status_t Parcel::readInt64Vector(std::vector<int64_t>* val) const {
1439 val->clear();
1440
1441 int32_t size;
1442 status_t status = readInt32(&size);
1443
1444 if (status != OK) {
1445 return status;
1446 }
1447
1448 if (size < 0) {
1449 return BAD_VALUE;
1450 }
1451
1452 val->resize(size);
1453
1454 for (auto& v : *val) {
1455 status = readInt64(&v);
1456
1457 if (status != OK) {
1458 return status;
1459 }
1460 }
1461
1462 return OK;
1463}
1464
1465status_t Parcel::readFloatVector(std::vector<float>* val) const {
1466 val->clear();
1467
1468 int32_t size;
1469 status_t status = readInt32(&size);
1470
1471 if (status != OK) {
1472 return status;
1473 }
1474
1475 if (size < 0) {
1476 return BAD_VALUE;
1477 }
1478
1479 val->resize(size);
1480
1481 for (auto& v : *val) {
1482 status = readFloat(&v);
1483
1484 if (status != OK) {
1485 return status;
1486 }
1487 }
1488
1489 return OK;
1490}
1491
1492status_t Parcel::readDoubleVector(std::vector<double>* val) const {
1493 val->clear();
1494
1495 int32_t size;
1496 status_t status = readInt32(&size);
1497
1498 if (status != OK) {
1499 return status;
1500 }
1501
1502 if (size < 0) {
1503 return BAD_VALUE;
1504 }
1505
1506 val->resize(size);
1507
1508 for (auto& v : *val) {
1509 status = readDouble(&v);
1510
1511 if (status != OK) {
1512 return status;
1513 }
1514 }
1515
1516 return OK;
1517}
1518
1519status_t Parcel::readBoolVector(std::vector<bool>* val) const {
1520 val->clear();
1521
1522 int32_t size;
1523 status_t status = readInt32(&size);
1524
1525 if (status != OK) {
1526 return status;
1527 }
1528
1529 if (size < 0) {
1530 return BAD_VALUE;
1531 }
1532
1533 val->resize(size);
1534
1535 /* C++ bool handling means a vector of bools isn't necessarily addressable
1536 * (we might use individual bits)
1537 */
Christopher Wiley97887982015-10-27 16:33:47 -07001538 bool data;
1539 for (int32_t i = 0; i < size; ++i) {
Casey Dahlin451ff582015-10-19 18:12:18 -07001540 status = readBool(&data);
1541 (*val)[i] = data;
1542
1543 if (status != OK) {
1544 return status;
1545 }
1546 }
1547
1548 return OK;
1549}
1550
1551status_t Parcel::readCharVector(std::vector<char16_t>* val) const {
1552 val->clear();
1553
1554 int32_t size;
1555 status_t status = readInt32(&size);
1556
1557 if (status != OK) {
1558 return status;
1559 }
1560
1561 if (size < 0) {
1562 return BAD_VALUE;
1563 }
1564
1565 val->resize(size);
1566
1567 for (auto& v : *val) {
1568 status = readChar(&v);
1569
1570 if (status != OK) {
1571 return status;
1572 }
1573 }
1574
1575 return OK;
1576}
1577
1578status_t Parcel::readString16Vector(std::vector<String16>* val) const {
1579 val->clear();
1580
1581 int32_t size;
1582 status_t status = readInt32(&size);
1583
1584 if (status != OK) {
1585 return status;
1586 }
1587
1588 if (size < 0) {
1589 return BAD_VALUE;
1590 }
1591
1592 val->reserve(size);
1593
1594 while (size-- > 0) {
1595 const char16_t *data;
1596 size_t size;
1597 data = readString16Inplace(&size);
1598
1599 if (data == nullptr) {
1600 return UNKNOWN_ERROR;
1601 }
1602
1603 val->emplace_back(data, size);
1604 }
1605
1606 return OK;
1607}
1608
1609
Andreas Huber84a6d042009-08-17 13:33:27 -07001610status_t Parcel::readInt32(int32_t *pArg) const
1611{
1612 return readAligned(pArg);
1613}
1614
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001615int32_t Parcel::readInt32() const
1616{
Andreas Huber84a6d042009-08-17 13:33:27 -07001617 return readAligned<int32_t>();
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001618}
1619
Dan Stoza41a0f2f2014-12-01 10:01:10 -08001620status_t Parcel::readUint32(uint32_t *pArg) const
1621{
1622 return readAligned(pArg);
1623}
1624
1625uint32_t Parcel::readUint32() const
1626{
1627 return readAligned<uint32_t>();
1628}
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001629
1630status_t Parcel::readInt64(int64_t *pArg) const
1631{
Andreas Huber84a6d042009-08-17 13:33:27 -07001632 return readAligned(pArg);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001633}
1634
1635
1636int64_t Parcel::readInt64() const
1637{
Andreas Huber84a6d042009-08-17 13:33:27 -07001638 return readAligned<int64_t>();
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001639}
1640
Ronghua Wu2d13afd2015-03-16 11:11:07 -07001641status_t Parcel::readUint64(uint64_t *pArg) const
1642{
1643 return readAligned(pArg);
1644}
1645
1646uint64_t Parcel::readUint64() const
1647{
1648 return readAligned<uint64_t>();
1649}
1650
Serban Constantinescuf683e012013-11-05 16:53:55 +00001651status_t Parcel::readPointer(uintptr_t *pArg) const
1652{
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001653 status_t ret;
1654 binder_uintptr_t ptr;
1655 ret = readAligned(&ptr);
1656 if (!ret)
1657 *pArg = ptr;
1658 return ret;
Serban Constantinescuf683e012013-11-05 16:53:55 +00001659}
1660
1661uintptr_t Parcel::readPointer() const
1662{
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001663 return readAligned<binder_uintptr_t>();
Serban Constantinescuf683e012013-11-05 16:53:55 +00001664}
1665
1666
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001667status_t Parcel::readFloat(float *pArg) const
1668{
Andreas Huber84a6d042009-08-17 13:33:27 -07001669 return readAligned(pArg);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001670}
1671
1672
1673float Parcel::readFloat() const
1674{
Andreas Huber84a6d042009-08-17 13:33:27 -07001675 return readAligned<float>();
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001676}
1677
Douglas Leungcc1a4bb2013-01-11 15:00:55 -08001678#if defined(__mips__) && defined(__mips_hard_float)
1679
1680status_t Parcel::readDouble(double *pArg) const
1681{
1682 union {
1683 double d;
1684 unsigned long long ll;
1685 } u;
Narayan Kamath2c68d382014-06-04 15:04:29 +01001686 u.d = 0;
Douglas Leungcc1a4bb2013-01-11 15:00:55 -08001687 status_t status;
1688 status = readAligned(&u.ll);
1689 *pArg = u.d;
1690 return status;
1691}
1692
1693double Parcel::readDouble() const
1694{
1695 union {
1696 double d;
1697 unsigned long long ll;
1698 } u;
1699 u.ll = readAligned<unsigned long long>();
1700 return u.d;
1701}
1702
1703#else
1704
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001705status_t Parcel::readDouble(double *pArg) const
1706{
Andreas Huber84a6d042009-08-17 13:33:27 -07001707 return readAligned(pArg);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001708}
1709
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001710double Parcel::readDouble() const
1711{
Andreas Huber84a6d042009-08-17 13:33:27 -07001712 return readAligned<double>();
1713}
1714
Douglas Leungcc1a4bb2013-01-11 15:00:55 -08001715#endif
1716
Andreas Huber84a6d042009-08-17 13:33:27 -07001717status_t Parcel::readIntPtr(intptr_t *pArg) const
1718{
1719 return readAligned(pArg);
1720}
1721
1722
1723intptr_t Parcel::readIntPtr() const
1724{
1725 return readAligned<intptr_t>();
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001726}
1727
Casey Dahlind6848f52015-10-15 15:44:59 -07001728status_t Parcel::readBool(bool *pArg) const
1729{
1730 int32_t tmp;
1731 status_t ret = readInt32(&tmp);
1732 *pArg = (tmp != 0);
1733 return ret;
1734}
1735
1736bool Parcel::readBool() const
1737{
1738 return readInt32() != 0;
1739}
1740
1741status_t Parcel::readChar(char16_t *pArg) const
1742{
1743 int32_t tmp;
1744 status_t ret = readInt32(&tmp);
1745 *pArg = char16_t(tmp);
1746 return ret;
1747}
1748
1749char16_t Parcel::readChar() const
1750{
1751 return char16_t(readInt32());
1752}
1753
1754status_t Parcel::readByte(int8_t *pArg) const
1755{
1756 int32_t tmp;
1757 status_t ret = readInt32(&tmp);
1758 *pArg = int8_t(tmp);
1759 return ret;
1760}
1761
1762int8_t Parcel::readByte() const
1763{
1764 return int8_t(readInt32());
1765}
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001766
1767const char* Parcel::readCString() const
1768{
1769 const size_t avail = mDataSize-mDataPos;
1770 if (avail > 0) {
1771 const char* str = reinterpret_cast<const char*>(mData+mDataPos);
1772 // is the string's trailing NUL within the parcel's valid bounds?
1773 const char* eos = reinterpret_cast<const char*>(memchr(str, 0, avail));
1774 if (eos) {
1775 const size_t len = eos - str;
Nick Kralevichb6b14232015-04-02 09:36:02 -07001776 mDataPos += pad_size(len+1);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001777 ALOGV("readCString Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001778 return str;
1779 }
1780 }
1781 return NULL;
1782}
1783
1784String8 Parcel::readString8() const
1785{
1786 int32_t size = readInt32();
1787 // watch for potential int overflow adding 1 for trailing NUL
1788 if (size > 0 && size < INT32_MAX) {
1789 const char* str = (const char*)readInplace(size+1);
1790 if (str) return String8(str, size);
1791 }
1792 return String8();
1793}
1794
1795String16 Parcel::readString16() const
1796{
1797 size_t len;
1798 const char16_t* str = readString16Inplace(&len);
1799 if (str) return String16(str, len);
Steve Blocke6f43dd2012-01-06 19:20:56 +00001800 ALOGE("Reading a NULL string not supported here.");
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001801 return String16();
1802}
1803
Casey Dahlin451ff582015-10-19 18:12:18 -07001804status_t Parcel::readString16(String16* pArg) const
1805{
1806 size_t len;
1807 const char16_t* str = readString16Inplace(&len);
1808 if (str) {
Casey Dahlin1515ea12015-10-20 16:26:23 -07001809 pArg->setTo(str, len);
Casey Dahlin451ff582015-10-19 18:12:18 -07001810 return 0;
1811 } else {
1812 *pArg = String16();
1813 return UNKNOWN_ERROR;
1814 }
1815}
1816
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001817const char16_t* Parcel::readString16Inplace(size_t* outLen) const
1818{
1819 int32_t size = readInt32();
1820 // watch for potential int overflow from size+1
1821 if (size >= 0 && size < INT32_MAX) {
1822 *outLen = size;
1823 const char16_t* str = (const char16_t*)readInplace((size+1)*sizeof(char16_t));
1824 if (str != NULL) {
1825 return str;
1826 }
1827 }
1828 *outLen = 0;
1829 return NULL;
1830}
1831
Casey Dahlinf0c13772015-10-27 18:33:56 -07001832status_t Parcel::readStrongBinder(sp<IBinder>* val) const
1833{
1834 return unflatten_binder(ProcessState::self(), *this, val);
1835}
1836
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001837sp<IBinder> Parcel::readStrongBinder() const
1838{
1839 sp<IBinder> val;
Casey Dahlinf0c13772015-10-27 18:33:56 -07001840 readStrongBinder(&val);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001841 return val;
1842}
1843
1844wp<IBinder> Parcel::readWeakBinder() const
1845{
1846 wp<IBinder> val;
1847 unflatten_binder(ProcessState::self(), *this, &val);
1848 return val;
1849}
1850
Brad Fitzpatrick837a0d02010-07-13 15:33:35 -07001851int32_t Parcel::readExceptionCode() const
1852{
1853 int32_t exception_code = readAligned<int32_t>();
Brad Fitzpatrickd36f4a52010-07-12 11:05:38 -07001854 if (exception_code == EX_HAS_REPLY_HEADER) {
Magnus Strandberg1ba24572011-05-03 15:44:00 +02001855 int32_t header_start = dataPosition();
Brad Fitzpatrickd36f4a52010-07-12 11:05:38 -07001856 int32_t header_size = readAligned<int32_t>();
1857 // Skip over fat responses headers. Not used (or propagated) in
1858 // native code
Magnus Strandberg1ba24572011-05-03 15:44:00 +02001859 setDataPosition(header_start + header_size);
Brad Fitzpatrickd36f4a52010-07-12 11:05:38 -07001860 // And fat response headers are currently only used when there are no
1861 // exceptions, so return no error:
1862 return 0;
1863 }
Brad Fitzpatrick837a0d02010-07-13 15:33:35 -07001864 return exception_code;
1865}
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001866
Mathias Agopiana47f02a2009-05-21 16:29:38 -07001867native_handle* Parcel::readNativeHandle() const
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001868{
1869 int numFds, numInts;
1870 status_t err;
1871 err = readInt32(&numFds);
1872 if (err != NO_ERROR) return 0;
1873 err = readInt32(&numInts);
1874 if (err != NO_ERROR) return 0;
1875
Mathias Agopiana47f02a2009-05-21 16:29:38 -07001876 native_handle* h = native_handle_create(numFds, numInts);
Adam Lesinskieaac99a2015-05-12 17:35:48 -07001877 if (!h) {
1878 return 0;
1879 }
1880
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001881 for (int i=0 ; err==NO_ERROR && i<numFds ; i++) {
Rebecca Schultz Zavin360211f2009-02-13 16:34:38 -08001882 h->data[i] = dup(readFileDescriptor());
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001883 if (h->data[i] < 0) err = BAD_VALUE;
1884 }
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001885 err = read(h->data + numFds, sizeof(int)*numInts);
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001886 if (err != NO_ERROR) {
Mathias Agopiana47f02a2009-05-21 16:29:38 -07001887 native_handle_close(h);
1888 native_handle_delete(h);
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001889 h = 0;
1890 }
1891 return h;
1892}
1893
1894
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001895int Parcel::readFileDescriptor() const
1896{
1897 const flat_binder_object* flat = readObject(true);
1898 if (flat) {
1899 switch (flat->type) {
1900 case BINDER_TYPE_FD:
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001901 //ALOGI("Returning file descriptor %ld from parcel %p", flat->handle, this);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001902 return flat->handle;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001903 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001904 }
1905 return BAD_TYPE;
1906}
1907
Jeff Brown5707dbf2011-09-23 21:17:56 -07001908status_t Parcel::readBlob(size_t len, ReadableBlob* outBlob) const
1909{
Jeff Brown13b16042014-11-11 16:44:25 -08001910 int32_t blobType;
1911 status_t status = readInt32(&blobType);
Jeff Brown5707dbf2011-09-23 21:17:56 -07001912 if (status) return status;
1913
Jeff Brown13b16042014-11-11 16:44:25 -08001914 if (blobType == BLOB_INPLACE) {
Steve Block6807e592011-10-20 11:56:00 +01001915 ALOGV("readBlob: read in place");
Jeff Brown5707dbf2011-09-23 21:17:56 -07001916 const void* ptr = readInplace(len);
1917 if (!ptr) return BAD_VALUE;
1918
Jeff Brown13b16042014-11-11 16:44:25 -08001919 outBlob->init(-1, const_cast<void*>(ptr), len, false);
Jeff Brown5707dbf2011-09-23 21:17:56 -07001920 return NO_ERROR;
1921 }
1922
Steve Block6807e592011-10-20 11:56:00 +01001923 ALOGV("readBlob: read from ashmem");
Jeff Brown13b16042014-11-11 16:44:25 -08001924 bool isMutable = (blobType == BLOB_ASHMEM_MUTABLE);
Jeff Brown5707dbf2011-09-23 21:17:56 -07001925 int fd = readFileDescriptor();
1926 if (fd == int(BAD_TYPE)) return BAD_VALUE;
1927
Jeff Brown13b16042014-11-11 16:44:25 -08001928 void* ptr = ::mmap(NULL, len, isMutable ? PROT_READ | PROT_WRITE : PROT_READ,
1929 MAP_SHARED, fd, 0);
Narayan Kamath9ea09752014-10-08 17:35:45 +01001930 if (ptr == MAP_FAILED) return NO_MEMORY;
Jeff Brown5707dbf2011-09-23 21:17:56 -07001931
Jeff Brown13b16042014-11-11 16:44:25 -08001932 outBlob->init(fd, ptr, len, isMutable);
Jeff Brown5707dbf2011-09-23 21:17:56 -07001933 return NO_ERROR;
1934}
1935
Mathias Agopiane1424282013-07-29 21:24:40 -07001936status_t Parcel::read(FlattenableHelperInterface& val) const
Mathias Agopian98e71dd2010-02-11 17:30:52 -08001937{
1938 // size
1939 const size_t len = this->readInt32();
1940 const size_t fd_count = this->readInt32();
1941
Nick Kralevichb6b14232015-04-02 09:36:02 -07001942 if (len > INT32_MAX) {
1943 // don't accept size_t values which may have come from an
1944 // inadvertent conversion from a negative int.
1945 return BAD_VALUE;
1946 }
1947
Mathias Agopian98e71dd2010-02-11 17:30:52 -08001948 // payload
Nick Kralevichb6b14232015-04-02 09:36:02 -07001949 void const* const buf = this->readInplace(pad_size(len));
Mathias Agopian98e71dd2010-02-11 17:30:52 -08001950 if (buf == NULL)
1951 return BAD_VALUE;
1952
1953 int* fds = NULL;
1954 if (fd_count) {
1955 fds = new int[fd_count];
1956 }
1957
1958 status_t err = NO_ERROR;
1959 for (size_t i=0 ; i<fd_count && err==NO_ERROR ; i++) {
Jesse Hallfee99042014-11-04 08:36:31 -08001960 fds[i] = dup(this->readFileDescriptor());
Jun Jiangabf8a2c2014-04-29 14:22:10 +08001961 if (fds[i] < 0) {
1962 err = BAD_VALUE;
Jesse Hallfee99042014-11-04 08:36:31 -08001963 ALOGE("dup() failed in Parcel::read, i is %zu, fds[i] is %d, fd_count is %zu, error: %s",
1964 i, fds[i], fd_count, strerror(errno));
Jun Jiangabf8a2c2014-04-29 14:22:10 +08001965 }
Mathias Agopian98e71dd2010-02-11 17:30:52 -08001966 }
1967
1968 if (err == NO_ERROR) {
1969 err = val.unflatten(buf, len, fds, fd_count);
1970 }
1971
1972 if (fd_count) {
1973 delete [] fds;
1974 }
1975
1976 return err;
1977}
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001978const flat_binder_object* Parcel::readObject(bool nullMetaData) const
1979{
1980 const size_t DPOS = mDataPos;
1981 if ((DPOS+sizeof(flat_binder_object)) <= mDataSize) {
1982 const flat_binder_object* obj
1983 = reinterpret_cast<const flat_binder_object*>(mData+DPOS);
1984 mDataPos = DPOS + sizeof(flat_binder_object);
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001985 if (!nullMetaData && (obj->cookie == 0 && obj->binder == 0)) {
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001986 // When transferring a NULL object, we don't write it into
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001987 // the object list, so we don't want to check for it when
1988 // reading.
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001989 ALOGV("readObject Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001990 return obj;
1991 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001992
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001993 // Ensure that this object is valid...
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001994 binder_size_t* const OBJS = mObjects;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001995 const size_t N = mObjectsSize;
1996 size_t opos = mNextObjectHint;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001997
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001998 if (N > 0) {
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001999 ALOGV("Parcel %p looking for obj at %zu, hint=%zu",
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002000 this, DPOS, opos);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002001
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002002 // Start at the current hint position, looking for an object at
2003 // the current data position.
2004 if (opos < N) {
2005 while (opos < (N-1) && OBJS[opos] < DPOS) {
2006 opos++;
2007 }
2008 } else {
2009 opos = N-1;
2010 }
2011 if (OBJS[opos] == DPOS) {
2012 // Found it!
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002013 ALOGV("Parcel %p found obj %zu at index %zu with forward search",
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002014 this, DPOS, opos);
2015 mNextObjectHint = opos+1;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002016 ALOGV("readObject Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002017 return obj;
2018 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002019
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002020 // Look backwards for it...
2021 while (opos > 0 && OBJS[opos] > DPOS) {
2022 opos--;
2023 }
2024 if (OBJS[opos] == DPOS) {
2025 // Found it!
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002026 ALOGV("Parcel %p found obj %zu at index %zu with backward search",
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002027 this, DPOS, opos);
2028 mNextObjectHint = opos+1;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002029 ALOGV("readObject Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002030 return obj;
2031 }
2032 }
Colin Cross6f4f3ab2014-02-05 17:42:44 -08002033 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 -07002034 this, DPOS);
2035 }
2036 return NULL;
2037}
2038
2039void Parcel::closeFileDescriptors()
2040{
2041 size_t i = mObjectsSize;
2042 if (i > 0) {
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002043 //ALOGI("Closing file descriptors for %zu objects...", i);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002044 }
2045 while (i > 0) {
2046 i--;
2047 const flat_binder_object* flat
2048 = reinterpret_cast<flat_binder_object*>(mData+mObjects[i]);
2049 if (flat->type == BINDER_TYPE_FD) {
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002050 //ALOGI("Closing fd: %ld", flat->handle);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002051 close(flat->handle);
2052 }
2053 }
2054}
2055
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002056uintptr_t Parcel::ipcData() const
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002057{
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002058 return reinterpret_cast<uintptr_t>(mData);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002059}
2060
2061size_t Parcel::ipcDataSize() const
2062{
2063 return (mDataSize > mDataPos ? mDataSize : mDataPos);
2064}
2065
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002066uintptr_t Parcel::ipcObjects() const
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002067{
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002068 return reinterpret_cast<uintptr_t>(mObjects);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002069}
2070
2071size_t Parcel::ipcObjectsCount() const
2072{
2073 return mObjectsSize;
2074}
2075
2076void Parcel::ipcSetDataReference(const uint8_t* data, size_t dataSize,
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002077 const binder_size_t* objects, size_t objectsCount, release_func relFunc, void* relCookie)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002078{
Arve Hjønnevåg6f286112014-02-19 20:42:13 -08002079 binder_size_t minOffset = 0;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002080 freeDataNoInit();
2081 mError = NO_ERROR;
2082 mData = const_cast<uint8_t*>(data);
2083 mDataSize = mDataCapacity = dataSize;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002084 //ALOGI("setDataReference Setting data size of %p to %lu (pid=%d)", this, mDataSize, getpid());
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002085 mDataPos = 0;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002086 ALOGV("setDataReference Setting data pos of %p to %zu", this, mDataPos);
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002087 mObjects = const_cast<binder_size_t*>(objects);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002088 mObjectsSize = mObjectsCapacity = objectsCount;
2089 mNextObjectHint = 0;
2090 mOwner = relFunc;
2091 mOwnerCookie = relCookie;
Arve Hjønnevågf50b9ea2014-02-13 19:22:08 -08002092 for (size_t i = 0; i < mObjectsSize; i++) {
Arve Hjønnevåg6f286112014-02-19 20:42:13 -08002093 binder_size_t offset = mObjects[i];
Arve Hjønnevågf50b9ea2014-02-13 19:22:08 -08002094 if (offset < minOffset) {
Dan Albert3bdc5b82014-11-20 11:50:23 -08002095 ALOGE("%s: bad object offset %" PRIu64 " < %" PRIu64 "\n",
Arve Hjønnevåg6f286112014-02-19 20:42:13 -08002096 __func__, (uint64_t)offset, (uint64_t)minOffset);
Arve Hjønnevågf50b9ea2014-02-13 19:22:08 -08002097 mObjectsSize = 0;
2098 break;
2099 }
2100 minOffset = offset + sizeof(flat_binder_object);
2101 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002102 scanForFds();
2103}
2104
Colin Cross6f4f3ab2014-02-05 17:42:44 -08002105void Parcel::print(TextOutput& to, uint32_t /*flags*/) const
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002106{
2107 to << "Parcel(";
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002108
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002109 if (errorCheck() != NO_ERROR) {
2110 const status_t err = errorCheck();
Colin Cross6f4f3ab2014-02-05 17:42:44 -08002111 to << "Error: " << (void*)(intptr_t)err << " \"" << strerror(-err) << "\"";
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002112 } else if (dataSize() > 0) {
2113 const uint8_t* DATA = data();
2114 to << indent << HexDump(DATA, dataSize()) << dedent;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002115 const binder_size_t* OBJS = objects();
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002116 const size_t N = objectsCount();
2117 for (size_t i=0; i<N; i++) {
2118 const flat_binder_object* flat
2119 = reinterpret_cast<const flat_binder_object*>(DATA+OBJS[i]);
2120 to << endl << "Object #" << i << " @ " << (void*)OBJS[i] << ": "
2121 << TypeCode(flat->type & 0x7f7f7f00)
2122 << " = " << flat->binder;
2123 }
2124 } else {
2125 to << "NULL";
2126 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002127
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002128 to << ")";
2129}
2130
2131void Parcel::releaseObjects()
2132{
2133 const sp<ProcessState> proc(ProcessState::self());
2134 size_t i = mObjectsSize;
2135 uint8_t* const data = mData;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002136 binder_size_t* const objects = mObjects;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002137 while (i > 0) {
2138 i--;
2139 const flat_binder_object* flat
2140 = reinterpret_cast<flat_binder_object*>(data+objects[i]);
2141 release_object(proc, *flat, this);
2142 }
2143}
2144
2145void Parcel::acquireObjects()
2146{
2147 const sp<ProcessState> proc(ProcessState::self());
2148 size_t i = mObjectsSize;
2149 uint8_t* const data = mData;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002150 binder_size_t* const objects = mObjects;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002151 while (i > 0) {
2152 i--;
2153 const flat_binder_object* flat
2154 = reinterpret_cast<flat_binder_object*>(data+objects[i]);
2155 acquire_object(proc, *flat, this);
2156 }
2157}
2158
2159void Parcel::freeData()
2160{
2161 freeDataNoInit();
2162 initState();
2163}
2164
2165void Parcel::freeDataNoInit()
2166{
2167 if (mOwner) {
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002168 LOG_ALLOC("Parcel %p: freeing other owner data", this);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002169 //ALOGI("Freeing data ref of %p (pid=%d)", this, getpid());
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002170 mOwner(this, mData, mDataSize, mObjects, mObjectsSize, mOwnerCookie);
2171 } else {
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002172 LOG_ALLOC("Parcel %p: freeing allocated data", this);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002173 releaseObjects();
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002174 if (mData) {
2175 LOG_ALLOC("Parcel %p: freeing with %zu capacity", this, mDataCapacity);
Dianne Hackborna4cff882014-11-13 17:07:40 -08002176 pthread_mutex_lock(&gParcelGlobalAllocSizeLock);
Dan Austin48fd7b42015-09-10 13:46:02 -07002177 if (mDataCapacity <= gParcelGlobalAllocSize) {
2178 gParcelGlobalAllocSize = gParcelGlobalAllocSize - mDataCapacity;
2179 } else {
2180 gParcelGlobalAllocSize = 0;
2181 }
2182 if (gParcelGlobalAllocCount > 0) {
2183 gParcelGlobalAllocCount--;
2184 }
Dianne Hackborna4cff882014-11-13 17:07:40 -08002185 pthread_mutex_unlock(&gParcelGlobalAllocSizeLock);
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002186 free(mData);
2187 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002188 if (mObjects) free(mObjects);
2189 }
2190}
2191
2192status_t Parcel::growData(size_t len)
2193{
Nick Kralevichb6b14232015-04-02 09:36:02 -07002194 if (len > INT32_MAX) {
2195 // don't accept size_t values which may have come from an
2196 // inadvertent conversion from a negative int.
2197 return BAD_VALUE;
2198 }
2199
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002200 size_t newSize = ((mDataSize+len)*3)/2;
2201 return (newSize <= mDataSize)
2202 ? (status_t) NO_MEMORY
2203 : continueWrite(newSize);
2204}
2205
2206status_t Parcel::restartWrite(size_t desired)
2207{
Nick Kralevichb6b14232015-04-02 09:36:02 -07002208 if (desired > INT32_MAX) {
2209 // don't accept size_t values which may have come from an
2210 // inadvertent conversion from a negative int.
2211 return BAD_VALUE;
2212 }
2213
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002214 if (mOwner) {
2215 freeData();
2216 return continueWrite(desired);
2217 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002218
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002219 uint8_t* data = (uint8_t*)realloc(mData, desired);
2220 if (!data && desired > mDataCapacity) {
2221 mError = NO_MEMORY;
2222 return NO_MEMORY;
2223 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002224
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002225 releaseObjects();
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002226
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002227 if (data) {
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002228 LOG_ALLOC("Parcel %p: restart from %zu to %zu capacity", this, mDataCapacity, desired);
Dianne Hackborna4cff882014-11-13 17:07:40 -08002229 pthread_mutex_lock(&gParcelGlobalAllocSizeLock);
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002230 gParcelGlobalAllocSize += desired;
2231 gParcelGlobalAllocSize -= mDataCapacity;
Dianne Hackborna4cff882014-11-13 17:07:40 -08002232 pthread_mutex_unlock(&gParcelGlobalAllocSizeLock);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002233 mData = data;
2234 mDataCapacity = desired;
2235 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002236
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002237 mDataSize = mDataPos = 0;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002238 ALOGV("restartWrite Setting data size of %p to %zu", this, mDataSize);
2239 ALOGV("restartWrite Setting data pos of %p to %zu", this, mDataPos);
2240
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002241 free(mObjects);
2242 mObjects = NULL;
2243 mObjectsSize = mObjectsCapacity = 0;
2244 mNextObjectHint = 0;
2245 mHasFds = false;
2246 mFdsKnown = true;
Dianne Hackborn8938ed22011-09-28 23:19:47 -04002247 mAllowFds = true;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002248
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002249 return NO_ERROR;
2250}
2251
2252status_t Parcel::continueWrite(size_t desired)
2253{
Nick Kralevichb6b14232015-04-02 09:36:02 -07002254 if (desired > INT32_MAX) {
2255 // don't accept size_t values which may have come from an
2256 // inadvertent conversion from a negative int.
2257 return BAD_VALUE;
2258 }
2259
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002260 // If shrinking, first adjust for any objects that appear
2261 // after the new data size.
2262 size_t objectsSize = mObjectsSize;
2263 if (desired < mDataSize) {
2264 if (desired == 0) {
2265 objectsSize = 0;
2266 } else {
2267 while (objectsSize > 0) {
2268 if (mObjects[objectsSize-1] < desired)
2269 break;
2270 objectsSize--;
2271 }
2272 }
2273 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002274
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002275 if (mOwner) {
2276 // If the size is going to zero, just release the owner's data.
2277 if (desired == 0) {
2278 freeData();
2279 return NO_ERROR;
2280 }
2281
2282 // If there is a different owner, we need to take
2283 // posession.
2284 uint8_t* data = (uint8_t*)malloc(desired);
2285 if (!data) {
2286 mError = NO_MEMORY;
2287 return NO_MEMORY;
2288 }
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002289 binder_size_t* objects = NULL;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002290
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002291 if (objectsSize) {
Nick Kraleviche9881a32015-04-28 16:21:30 -07002292 objects = (binder_size_t*)calloc(objectsSize, sizeof(binder_size_t));
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002293 if (!objects) {
Hyejin Kim3f727c02013-03-09 11:28:54 +09002294 free(data);
2295
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002296 mError = NO_MEMORY;
2297 return NO_MEMORY;
2298 }
2299
2300 // Little hack to only acquire references on objects
2301 // we will be keeping.
2302 size_t oldObjectsSize = mObjectsSize;
2303 mObjectsSize = objectsSize;
2304 acquireObjects();
2305 mObjectsSize = oldObjectsSize;
2306 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002307
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002308 if (mData) {
2309 memcpy(data, mData, mDataSize < desired ? mDataSize : desired);
2310 }
2311 if (objects && mObjects) {
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002312 memcpy(objects, mObjects, objectsSize*sizeof(binder_size_t));
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002313 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002314 //ALOGI("Freeing data ref of %p (pid=%d)", this, getpid());
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002315 mOwner(this, mData, mDataSize, mObjects, mObjectsSize, mOwnerCookie);
2316 mOwner = NULL;
2317
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002318 LOG_ALLOC("Parcel %p: taking ownership of %zu capacity", this, desired);
Dianne Hackborna4cff882014-11-13 17:07:40 -08002319 pthread_mutex_lock(&gParcelGlobalAllocSizeLock);
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002320 gParcelGlobalAllocSize += desired;
2321 gParcelGlobalAllocCount++;
Dianne Hackborna4cff882014-11-13 17:07:40 -08002322 pthread_mutex_unlock(&gParcelGlobalAllocSizeLock);
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002323
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002324 mData = data;
2325 mObjects = objects;
2326 mDataSize = (mDataSize < desired) ? mDataSize : desired;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002327 ALOGV("continueWrite Setting data size of %p to %zu", this, mDataSize);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002328 mDataCapacity = desired;
2329 mObjectsSize = mObjectsCapacity = objectsSize;
2330 mNextObjectHint = 0;
2331
2332 } else if (mData) {
2333 if (objectsSize < mObjectsSize) {
2334 // Need to release refs on any objects we are dropping.
2335 const sp<ProcessState> proc(ProcessState::self());
2336 for (size_t i=objectsSize; i<mObjectsSize; i++) {
2337 const flat_binder_object* flat
2338 = reinterpret_cast<flat_binder_object*>(mData+mObjects[i]);
2339 if (flat->type == BINDER_TYPE_FD) {
2340 // will need to rescan because we may have lopped off the only FDs
2341 mFdsKnown = false;
2342 }
2343 release_object(proc, *flat, this);
2344 }
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002345 binder_size_t* objects =
2346 (binder_size_t*)realloc(mObjects, objectsSize*sizeof(binder_size_t));
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002347 if (objects) {
2348 mObjects = objects;
2349 }
2350 mObjectsSize = objectsSize;
2351 mNextObjectHint = 0;
2352 }
2353
2354 // We own the data, so we can just do a realloc().
2355 if (desired > mDataCapacity) {
2356 uint8_t* data = (uint8_t*)realloc(mData, desired);
2357 if (data) {
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002358 LOG_ALLOC("Parcel %p: continue from %zu to %zu capacity", this, mDataCapacity,
2359 desired);
Dianne Hackborna4cff882014-11-13 17:07:40 -08002360 pthread_mutex_lock(&gParcelGlobalAllocSizeLock);
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002361 gParcelGlobalAllocSize += desired;
2362 gParcelGlobalAllocSize -= mDataCapacity;
Dan Austin48fd7b42015-09-10 13:46:02 -07002363 gParcelGlobalAllocCount++;
Dianne Hackborna4cff882014-11-13 17:07:40 -08002364 pthread_mutex_unlock(&gParcelGlobalAllocSizeLock);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002365 mData = data;
2366 mDataCapacity = desired;
2367 } else if (desired > mDataCapacity) {
2368 mError = NO_MEMORY;
2369 return NO_MEMORY;
2370 }
2371 } else {
Dianne Hackborn97e2bcd2011-04-13 18:15:56 -07002372 if (mDataSize > desired) {
2373 mDataSize = desired;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002374 ALOGV("continueWrite Setting data size of %p to %zu", this, mDataSize);
Dianne Hackborn97e2bcd2011-04-13 18:15:56 -07002375 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002376 if (mDataPos > desired) {
2377 mDataPos = desired;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002378 ALOGV("continueWrite Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002379 }
2380 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002381
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002382 } else {
2383 // This is the first data. Easy!
2384 uint8_t* data = (uint8_t*)malloc(desired);
2385 if (!data) {
2386 mError = NO_MEMORY;
2387 return NO_MEMORY;
2388 }
Hyejin Kim3f727c02013-03-09 11:28:54 +09002389
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002390 if(!(mDataCapacity == 0 && mObjects == NULL
2391 && mObjectsCapacity == 0)) {
Colin Cross6f4f3ab2014-02-05 17:42:44 -08002392 ALOGE("continueWrite: %zu/%p/%zu/%zu", mDataCapacity, mObjects, mObjectsCapacity, desired);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002393 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002394
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002395 LOG_ALLOC("Parcel %p: allocating with %zu capacity", this, desired);
Dianne Hackborna4cff882014-11-13 17:07:40 -08002396 pthread_mutex_lock(&gParcelGlobalAllocSizeLock);
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002397 gParcelGlobalAllocSize += desired;
2398 gParcelGlobalAllocCount++;
Dianne Hackborna4cff882014-11-13 17:07:40 -08002399 pthread_mutex_unlock(&gParcelGlobalAllocSizeLock);
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002400
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002401 mData = data;
2402 mDataSize = mDataPos = 0;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002403 ALOGV("continueWrite Setting data size of %p to %zu", this, mDataSize);
2404 ALOGV("continueWrite Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002405 mDataCapacity = desired;
2406 }
2407
2408 return NO_ERROR;
2409}
2410
2411void Parcel::initState()
2412{
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002413 LOG_ALLOC("Parcel %p: initState", this);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002414 mError = NO_ERROR;
2415 mData = 0;
2416 mDataSize = 0;
2417 mDataCapacity = 0;
2418 mDataPos = 0;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002419 ALOGV("initState Setting data size of %p to %zu", this, mDataSize);
2420 ALOGV("initState Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002421 mObjects = NULL;
2422 mObjectsSize = 0;
2423 mObjectsCapacity = 0;
2424 mNextObjectHint = 0;
2425 mHasFds = false;
2426 mFdsKnown = true;
Dianne Hackborn8938ed22011-09-28 23:19:47 -04002427 mAllowFds = true;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002428 mOwner = NULL;
Dan Sandleraa5c2342015-04-10 10:08:45 -04002429 mBlobAshmemSize = 0;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002430}
2431
2432void Parcel::scanForFds() const
2433{
2434 bool hasFds = false;
2435 for (size_t i=0; i<mObjectsSize; i++) {
2436 const flat_binder_object* flat
2437 = reinterpret_cast<const flat_binder_object*>(mData + mObjects[i]);
2438 if (flat->type == BINDER_TYPE_FD) {
2439 hasFds = true;
2440 break;
2441 }
2442 }
2443 mHasFds = hasFds;
2444 mFdsKnown = true;
2445}
2446
Dan Sandleraa5c2342015-04-10 10:08:45 -04002447size_t Parcel::getBlobAshmemSize() const
2448{
2449 return mBlobAshmemSize;
2450}
2451
Jeff Brown5707dbf2011-09-23 21:17:56 -07002452// --- Parcel::Blob ---
2453
2454Parcel::Blob::Blob() :
Jeff Brown13b16042014-11-11 16:44:25 -08002455 mFd(-1), mData(NULL), mSize(0), mMutable(false) {
Jeff Brown5707dbf2011-09-23 21:17:56 -07002456}
2457
2458Parcel::Blob::~Blob() {
2459 release();
2460}
2461
2462void Parcel::Blob::release() {
Jeff Brown13b16042014-11-11 16:44:25 -08002463 if (mFd != -1 && mData) {
Jeff Brown5707dbf2011-09-23 21:17:56 -07002464 ::munmap(mData, mSize);
2465 }
2466 clear();
2467}
2468
Jeff Brown13b16042014-11-11 16:44:25 -08002469void Parcel::Blob::init(int fd, void* data, size_t size, bool isMutable) {
2470 mFd = fd;
Jeff Brown5707dbf2011-09-23 21:17:56 -07002471 mData = data;
2472 mSize = size;
Jeff Brown13b16042014-11-11 16:44:25 -08002473 mMutable = isMutable;
Jeff Brown5707dbf2011-09-23 21:17:56 -07002474}
2475
2476void Parcel::Blob::clear() {
Jeff Brown13b16042014-11-11 16:44:25 -08002477 mFd = -1;
Jeff Brown5707dbf2011-09-23 21:17:56 -07002478 mData = NULL;
2479 mSize = 0;
Jeff Brown13b16042014-11-11 16:44:25 -08002480 mMutable = false;
Jeff Brown5707dbf2011-09-23 21:17:56 -07002481}
2482
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002483}; // namespace android