blob: 45bf63650516a1d62cdeaef951177ca097bf64e3 [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
Christopher Wiley4db672d2015-11-10 09:44:30 -0800313namespace {
314
315template<typename T>
316status_t readTypedVector(std::vector<T>* val, const Parcel* p,
317 status_t(Parcel::*read_func)(T*) const) {
318 val->clear();
319
320 int32_t size;
321 status_t status = p->readInt32(&size);
322
323 if (status != OK) {
324 return status;
325 }
326
327 if (size < 0) {
328 return UNEXPECTED_NULL;
329 }
330
331 val->resize(size);
332
333 for (auto& v: *val) {
334 status = (p->*read_func)(&v);
335
336 if (status != OK) {
337 return status;
338 }
339 }
340
341 return OK;
342}
343
344} // namespace
345
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700346// ---------------------------------------------------------------------------
347
348Parcel::Parcel()
349{
Dianne Hackborn7e790af2014-11-11 12:22:53 -0800350 LOG_ALLOC("Parcel %p: constructing", this);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700351 initState();
352}
353
354Parcel::~Parcel()
355{
356 freeDataNoInit();
Dianne Hackborn7e790af2014-11-11 12:22:53 -0800357 LOG_ALLOC("Parcel %p: destroyed", this);
358}
359
360size_t Parcel::getGlobalAllocSize() {
Dianne Hackborna4cff882014-11-13 17:07:40 -0800361 pthread_mutex_lock(&gParcelGlobalAllocSizeLock);
362 size_t size = gParcelGlobalAllocSize;
363 pthread_mutex_unlock(&gParcelGlobalAllocSizeLock);
364 return size;
Dianne Hackborn7e790af2014-11-11 12:22:53 -0800365}
366
367size_t Parcel::getGlobalAllocCount() {
Dianne Hackborna4cff882014-11-13 17:07:40 -0800368 pthread_mutex_lock(&gParcelGlobalAllocSizeLock);
369 size_t count = gParcelGlobalAllocCount;
370 pthread_mutex_unlock(&gParcelGlobalAllocSizeLock);
371 return count;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700372}
373
374const uint8_t* Parcel::data() const
375{
376 return mData;
377}
378
379size_t Parcel::dataSize() const
380{
381 return (mDataSize > mDataPos ? mDataSize : mDataPos);
382}
383
384size_t Parcel::dataAvail() const
385{
386 // TODO: decide what to do about the possibility that this can
387 // report an available-data size that exceeds a Java int's max
388 // positive value, causing havoc. Fortunately this will only
389 // happen if someone constructs a Parcel containing more than two
390 // gigabytes of data, which on typical phone hardware is simply
391 // not possible.
392 return dataSize() - dataPosition();
393}
394
395size_t Parcel::dataPosition() const
396{
397 return mDataPos;
398}
399
400size_t Parcel::dataCapacity() const
401{
402 return mDataCapacity;
403}
404
405status_t Parcel::setDataSize(size_t size)
406{
Nick Kralevichb6b14232015-04-02 09:36:02 -0700407 if (size > INT32_MAX) {
408 // don't accept size_t values which may have come from an
409 // inadvertent conversion from a negative int.
410 return BAD_VALUE;
411 }
412
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700413 status_t err;
414 err = continueWrite(size);
415 if (err == NO_ERROR) {
416 mDataSize = size;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700417 ALOGV("setDataSize Setting data size of %p to %zu", this, mDataSize);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700418 }
419 return err;
420}
421
422void Parcel::setDataPosition(size_t pos) const
423{
Nick Kralevichb6b14232015-04-02 09:36:02 -0700424 if (pos > INT32_MAX) {
425 // don't accept size_t values which may have come from an
426 // inadvertent conversion from a negative int.
427 abort();
428 }
429
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700430 mDataPos = pos;
431 mNextObjectHint = 0;
432}
433
434status_t Parcel::setDataCapacity(size_t size)
435{
Nick Kralevichb6b14232015-04-02 09:36:02 -0700436 if (size > INT32_MAX) {
437 // don't accept size_t values which may have come from an
438 // inadvertent conversion from a negative int.
439 return BAD_VALUE;
440 }
441
Dianne Hackborn97e2bcd2011-04-13 18:15:56 -0700442 if (size > mDataCapacity) return continueWrite(size);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700443 return NO_ERROR;
444}
445
446status_t Parcel::setData(const uint8_t* buffer, size_t len)
447{
Nick Kralevichb6b14232015-04-02 09:36:02 -0700448 if (len > INT32_MAX) {
449 // don't accept size_t values which may have come from an
450 // inadvertent conversion from a negative int.
451 return BAD_VALUE;
452 }
453
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700454 status_t err = restartWrite(len);
455 if (err == NO_ERROR) {
456 memcpy(const_cast<uint8_t*>(data()), buffer, len);
457 mDataSize = len;
458 mFdsKnown = false;
459 }
460 return err;
461}
462
Andreas Huber51faf462011-04-13 10:21:56 -0700463status_t Parcel::appendFrom(const Parcel *parcel, size_t offset, size_t len)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700464{
465 const sp<ProcessState> proc(ProcessState::self());
466 status_t err;
Andreas Huber51faf462011-04-13 10:21:56 -0700467 const uint8_t *data = parcel->mData;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800468 const binder_size_t *objects = parcel->mObjects;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700469 size_t size = parcel->mObjectsSize;
470 int startPos = mDataPos;
471 int firstIndex = -1, lastIndex = -2;
472
473 if (len == 0) {
474 return NO_ERROR;
475 }
476
Nick Kralevichb6b14232015-04-02 09:36:02 -0700477 if (len > INT32_MAX) {
478 // don't accept size_t values which may have come from an
479 // inadvertent conversion from a negative int.
480 return BAD_VALUE;
481 }
482
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700483 // range checks against the source parcel size
484 if ((offset > parcel->mDataSize)
485 || (len > parcel->mDataSize)
486 || (offset + len > parcel->mDataSize)) {
487 return BAD_VALUE;
488 }
489
490 // Count objects in range
491 for (int i = 0; i < (int) size; i++) {
492 size_t off = objects[i];
Christopher Tate27182be2015-05-27 17:53:02 -0700493 if ((off >= offset) && (off + sizeof(flat_binder_object) <= offset + len)) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700494 if (firstIndex == -1) {
495 firstIndex = i;
496 }
497 lastIndex = i;
498 }
499 }
500 int numObjects = lastIndex - firstIndex + 1;
501
Dianne Hackborn97e2bcd2011-04-13 18:15:56 -0700502 if ((mDataSize+len) > mDataCapacity) {
503 // grow data
504 err = growData(len);
505 if (err != NO_ERROR) {
506 return err;
507 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700508 }
509
510 // append data
511 memcpy(mData + mDataPos, data + offset, len);
512 mDataPos += len;
513 mDataSize += len;
514
Dianne Hackborn8938ed22011-09-28 23:19:47 -0400515 err = NO_ERROR;
516
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700517 if (numObjects > 0) {
518 // grow objects
519 if (mObjectsCapacity < mObjectsSize + numObjects) {
Christopher Tateed7a50c2015-06-08 14:45:14 -0700520 size_t newSize = ((mObjectsSize + numObjects)*3)/2;
521 if (newSize < mObjectsSize) return NO_MEMORY; // overflow
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800522 binder_size_t *objects =
523 (binder_size_t*)realloc(mObjects, newSize*sizeof(binder_size_t));
524 if (objects == (binder_size_t*)0) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700525 return NO_MEMORY;
526 }
527 mObjects = objects;
528 mObjectsCapacity = newSize;
529 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700530
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700531 // append and acquire objects
532 int idx = mObjectsSize;
533 for (int i = firstIndex; i <= lastIndex; i++) {
534 size_t off = objects[i] - offset + startPos;
535 mObjects[idx++] = off;
536 mObjectsSize++;
537
Dianne Hackborn8af0f822009-05-22 13:20:23 -0700538 flat_binder_object* flat
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700539 = reinterpret_cast<flat_binder_object*>(mData + off);
540 acquire_object(proc, *flat, this);
541
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700542 if (flat->type == BINDER_TYPE_FD) {
Dianne Hackborn8af0f822009-05-22 13:20:23 -0700543 // If this is a file descriptor, we need to dup it so the
544 // new Parcel now owns its own fd, and can declare that we
545 // officially know we have fds.
546 flat->handle = dup(flat->handle);
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800547 flat->cookie = 1;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700548 mHasFds = mFdsKnown = true;
Dianne Hackborn8938ed22011-09-28 23:19:47 -0400549 if (!mAllowFds) {
550 err = FDS_NOT_ALLOWED;
551 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700552 }
553 }
554 }
555
Dianne Hackborn8938ed22011-09-28 23:19:47 -0400556 return err;
557}
558
Jeff Brown13b16042014-11-11 16:44:25 -0800559bool Parcel::allowFds() const
560{
561 return mAllowFds;
562}
563
Dianne Hackborn7746cc32011-10-03 21:09:35 -0700564bool Parcel::pushAllowFds(bool allowFds)
Dianne Hackborn8938ed22011-09-28 23:19:47 -0400565{
566 const bool origValue = mAllowFds;
Dianne Hackborn7746cc32011-10-03 21:09:35 -0700567 if (!allowFds) {
568 mAllowFds = false;
569 }
Dianne Hackborn8938ed22011-09-28 23:19:47 -0400570 return origValue;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700571}
572
Dianne Hackborn7746cc32011-10-03 21:09:35 -0700573void Parcel::restoreAllowFds(bool lastValue)
574{
575 mAllowFds = lastValue;
576}
577
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700578bool Parcel::hasFileDescriptors() const
579{
580 if (!mFdsKnown) {
581 scanForFds();
582 }
583 return mHasFds;
584}
585
Brad Fitzpatrick702ea9d2010-06-18 13:07:53 -0700586// Write RPC headers. (previously just the interface token)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700587status_t Parcel::writeInterfaceToken(const String16& interface)
588{
Brad Fitzpatricka877cd82010-07-07 16:06:39 -0700589 writeInt32(IPCThreadState::self()->getStrictModePolicy() |
590 STRICT_MODE_PENALTY_GATHER);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700591 // currently the interface identification token is just its name as a string
592 return writeString16(interface);
593}
594
Mathias Agopian83c04462009-05-22 19:00:22 -0700595bool Parcel::checkInterface(IBinder* binder) const
596{
Brad Fitzpatrick702ea9d2010-06-18 13:07:53 -0700597 return enforceInterface(binder->getInterfaceDescriptor());
Mathias Agopian83c04462009-05-22 19:00:22 -0700598}
599
Brad Fitzpatricka877cd82010-07-07 16:06:39 -0700600bool Parcel::enforceInterface(const String16& interface,
Brad Fitzpatrick70081a12010-07-27 09:49:11 -0700601 IPCThreadState* threadState) const
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700602{
Brad Fitzpatrick70081a12010-07-27 09:49:11 -0700603 int32_t strictPolicy = readInt32();
604 if (threadState == NULL) {
605 threadState = IPCThreadState::self();
Brad Fitzpatricka877cd82010-07-07 16:06:39 -0700606 }
Brad Fitzpatrick52736032010-08-30 16:01:16 -0700607 if ((threadState->getLastTransactionBinderFlags() &
608 IBinder::FLAG_ONEWAY) != 0) {
609 // For one-way calls, the callee is running entirely
610 // disconnected from the caller, so disable StrictMode entirely.
611 // Not only does disk/network usage not impact the caller, but
612 // there's no way to commuicate back any violations anyway.
613 threadState->setStrictModePolicy(0);
614 } else {
615 threadState->setStrictModePolicy(strictPolicy);
616 }
Mathias Agopian83c04462009-05-22 19:00:22 -0700617 const String16 str(readString16());
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700618 if (str == interface) {
619 return true;
620 } else {
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700621 ALOGW("**** enforceInterface() expected '%s' but read '%s'",
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700622 String8(interface).string(), String8(str).string());
623 return false;
624 }
Brad Fitzpatrick702ea9d2010-06-18 13:07:53 -0700625}
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700626
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800627const binder_size_t* Parcel::objects() const
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700628{
629 return mObjects;
630}
631
632size_t Parcel::objectsCount() const
633{
634 return mObjectsSize;
635}
636
637status_t Parcel::errorCheck() const
638{
639 return mError;
640}
641
642void Parcel::setError(status_t err)
643{
644 mError = err;
645}
646
647status_t Parcel::finishWrite(size_t len)
648{
Nick Kralevichb6b14232015-04-02 09:36:02 -0700649 if (len > INT32_MAX) {
650 // don't accept size_t values which may have come from an
651 // inadvertent conversion from a negative int.
652 return BAD_VALUE;
653 }
654
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700655 //printf("Finish write of %d\n", len);
656 mDataPos += len;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700657 ALOGV("finishWrite Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700658 if (mDataPos > mDataSize) {
659 mDataSize = mDataPos;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700660 ALOGV("finishWrite Setting data size of %p to %zu", this, mDataSize);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700661 }
662 //printf("New pos=%d, size=%d\n", mDataPos, mDataSize);
663 return NO_ERROR;
664}
665
666status_t Parcel::writeUnpadded(const void* data, size_t len)
667{
Nick Kralevichb6b14232015-04-02 09:36:02 -0700668 if (len > INT32_MAX) {
669 // don't accept size_t values which may have come from an
670 // inadvertent conversion from a negative int.
671 return BAD_VALUE;
672 }
673
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700674 size_t end = mDataPos + len;
675 if (end < mDataPos) {
676 // integer overflow
677 return BAD_VALUE;
678 }
679
680 if (end <= mDataCapacity) {
681restart_write:
682 memcpy(mData+mDataPos, data, len);
683 return finishWrite(len);
684 }
685
686 status_t err = growData(len);
687 if (err == NO_ERROR) goto restart_write;
688 return err;
689}
690
691status_t Parcel::write(const void* data, size_t len)
692{
Nick Kralevichb6b14232015-04-02 09:36:02 -0700693 if (len > INT32_MAX) {
694 // don't accept size_t values which may have come from an
695 // inadvertent conversion from a negative int.
696 return BAD_VALUE;
697 }
698
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700699 void* const d = writeInplace(len);
700 if (d) {
701 memcpy(d, data, len);
702 return NO_ERROR;
703 }
704 return mError;
705}
706
707void* Parcel::writeInplace(size_t len)
708{
Nick Kralevichb6b14232015-04-02 09:36:02 -0700709 if (len > INT32_MAX) {
710 // don't accept size_t values which may have come from an
711 // inadvertent conversion from a negative int.
712 return NULL;
713 }
714
715 const size_t padded = pad_size(len);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700716
717 // sanity check for integer overflow
718 if (mDataPos+padded < mDataPos) {
719 return NULL;
720 }
721
722 if ((mDataPos+padded) <= mDataCapacity) {
723restart_write:
724 //printf("Writing %ld bytes, padded to %ld\n", len, padded);
725 uint8_t* const data = mData+mDataPos;
726
727 // Need to pad at end?
728 if (padded != len) {
729#if BYTE_ORDER == BIG_ENDIAN
730 static const uint32_t mask[4] = {
731 0x00000000, 0xffffff00, 0xffff0000, 0xff000000
732 };
733#endif
734#if BYTE_ORDER == LITTLE_ENDIAN
735 static const uint32_t mask[4] = {
736 0x00000000, 0x00ffffff, 0x0000ffff, 0x000000ff
737 };
738#endif
739 //printf("Applying pad mask: %p to %p\n", (void*)mask[padded-len],
740 // *reinterpret_cast<void**>(data+padded-4));
741 *reinterpret_cast<uint32_t*>(data+padded-4) &= mask[padded-len];
742 }
743
744 finishWrite(padded);
745 return data;
746 }
747
748 status_t err = growData(padded);
749 if (err == NO_ERROR) goto restart_write;
750 return NULL;
751}
752
Casey Dahlin451ff582015-10-19 18:12:18 -0700753status_t Parcel::writeByteVector(const std::vector<int8_t>& val)
754{
Christopher Wileyf0fc52b2015-10-31 13:22:15 -0700755 status_t status;
Casey Dahlin451ff582015-10-19 18:12:18 -0700756 if (val.size() > std::numeric_limits<int32_t>::max()) {
Christopher Wileyf0fc52b2015-10-31 13:22:15 -0700757 status = BAD_VALUE;
758 return status;
Casey Dahlin451ff582015-10-19 18:12:18 -0700759 }
760
Christopher Wileyf0fc52b2015-10-31 13:22:15 -0700761 status = writeInt32(val.size());
Casey Dahlin451ff582015-10-19 18:12:18 -0700762 if (status != OK) {
763 return status;
764 }
765
Christopher Wileyf0fc52b2015-10-31 13:22:15 -0700766 void* data = writeInplace(val.size());
767 if (!data) {
768 status = BAD_VALUE;
769 return status;
Casey Dahlin451ff582015-10-19 18:12:18 -0700770 }
771
Christopher Wileyf0fc52b2015-10-31 13:22:15 -0700772 memcpy(data, val.data(), val.size());
773 return status;
Casey Dahlin451ff582015-10-19 18:12:18 -0700774}
775
776status_t Parcel::writeInt32Vector(const std::vector<int32_t>& val)
777{
778 if (val.size() > std::numeric_limits<int32_t>::max()) {
779 return BAD_VALUE;
780 }
781
782 status_t status = writeInt32(val.size());
783
784 if (status != OK) {
785 return status;
786 }
787
788 for (const auto& item : val) {
789 status = writeInt32(item);
790
791 if (status != OK) {
792 return status;
793 }
794 }
795
796 return OK;
797}
798
799status_t Parcel::writeInt64Vector(const std::vector<int64_t>& val)
800{
801 if (val.size() > std::numeric_limits<int32_t>::max()) {
802 return BAD_VALUE;
803 }
804
805 status_t status = writeInt32(val.size());
806
807 if (status != OK) {
808 return status;
809 }
810
811 for (const auto& item : val) {
812 status = writeInt64(item);
813
814 if (status != OK) {
815 return status;
816 }
817 }
818
819 return OK;
820}
821
822status_t Parcel::writeFloatVector(const std::vector<float>& val)
823{
824 if (val.size() > std::numeric_limits<int32_t>::max()) {
825 return BAD_VALUE;
826 }
827
828 status_t status = writeInt32(val.size());
829
830 if (status != OK) {
831 return status;
832 }
833
834 for (const auto& item : val) {
835 status = writeFloat(item);
836
837 if (status != OK) {
838 return status;
839 }
840 }
841
842 return OK;
843}
844
845status_t Parcel::writeDoubleVector(const std::vector<double>& val)
846{
847 if (val.size() > std::numeric_limits<int32_t>::max()) {
848 return BAD_VALUE;
849 }
850
851 status_t status = writeInt32(val.size());
852
853 if (status != OK) {
854 return status;
855 }
856
857 for (const auto& item : val) {
858 status = writeDouble(item);
859
860 if (status != OK) {
861 return status;
862 }
863 }
864
865 return OK;
866}
867
868status_t Parcel::writeBoolVector(const std::vector<bool>& val)
869{
870 if (val.size() > std::numeric_limits<int32_t>::max()) {
871 return BAD_VALUE;
872 }
873
874 status_t status = writeInt32(val.size());
875
876 if (status != OK) {
877 return status;
878 }
879
880 for (const auto& item : val) {
881 status = writeBool(item);
882
883 if (status != OK) {
884 return status;
885 }
886 }
887
888 return OK;
889}
890
891status_t Parcel::writeCharVector(const std::vector<char16_t>& val)
892{
893 if (val.size() > std::numeric_limits<int32_t>::max()) {
894 return BAD_VALUE;
895 }
896
897 status_t status = writeInt32(val.size());
898
899 if (status != OK) {
900 return status;
901 }
902
903 for (const auto& item : val) {
904 status = writeChar(item);
905
906 if (status != OK) {
907 return status;
908 }
909 }
910
911 return OK;
912}
913
914status_t Parcel::writeString16Vector(const std::vector<String16>& val)
915{
916 if (val.size() > std::numeric_limits<int32_t>::max()) {
917 return BAD_VALUE;
918 }
919
920 status_t status = writeInt32(val.size());
921
922 if (status != OK) {
923 return status;
924 }
925
926 for (const auto& item : val) {
927 status = writeString16(item);
928
929 if (status != OK) {
930 return status;
931 }
932 }
933
934 return OK;
935}
936
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700937status_t Parcel::writeInt32(int32_t val)
938{
Andreas Huber84a6d042009-08-17 13:33:27 -0700939 return writeAligned(val);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700940}
Dan Stoza41a0f2f2014-12-01 10:01:10 -0800941
942status_t Parcel::writeUint32(uint32_t val)
943{
944 return writeAligned(val);
945}
946
Marco Nelissen5c0106e2013-10-16 10:57:51 -0700947status_t Parcel::writeInt32Array(size_t len, const int32_t *val) {
Nick Kralevichb6b14232015-04-02 09:36:02 -0700948 if (len > INT32_MAX) {
949 // don't accept size_t values which may have come from an
950 // inadvertent conversion from a negative int.
951 return BAD_VALUE;
952 }
953
Marco Nelissen5c0106e2013-10-16 10:57:51 -0700954 if (!val) {
Chad Brubakere59cb432015-06-30 14:03:55 -0700955 return writeInt32(-1);
Marco Nelissen5c0106e2013-10-16 10:57:51 -0700956 }
Chad Brubakere59cb432015-06-30 14:03:55 -0700957 status_t ret = writeInt32(static_cast<uint32_t>(len));
Marco Nelissen5c0106e2013-10-16 10:57:51 -0700958 if (ret == NO_ERROR) {
959 ret = write(val, len * sizeof(*val));
960 }
961 return ret;
962}
Marco Nelissenf0190bf2014-03-13 14:17:40 -0700963status_t Parcel::writeByteArray(size_t len, const uint8_t *val) {
Nick Kralevichb6b14232015-04-02 09:36:02 -0700964 if (len > INT32_MAX) {
965 // don't accept size_t values which may have come from an
966 // inadvertent conversion from a negative int.
967 return BAD_VALUE;
968 }
969
Marco Nelissenf0190bf2014-03-13 14:17:40 -0700970 if (!val) {
Chad Brubakere59cb432015-06-30 14:03:55 -0700971 return writeInt32(-1);
Marco Nelissenf0190bf2014-03-13 14:17:40 -0700972 }
Chad Brubakere59cb432015-06-30 14:03:55 -0700973 status_t ret = writeInt32(static_cast<uint32_t>(len));
Marco Nelissenf0190bf2014-03-13 14:17:40 -0700974 if (ret == NO_ERROR) {
975 ret = write(val, len * sizeof(*val));
976 }
977 return ret;
978}
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700979
Casey Dahlind6848f52015-10-15 15:44:59 -0700980status_t Parcel::writeBool(bool val)
981{
982 return writeInt32(int32_t(val));
983}
984
985status_t Parcel::writeChar(char16_t val)
986{
987 return writeInt32(int32_t(val));
988}
989
990status_t Parcel::writeByte(int8_t val)
991{
992 return writeInt32(int32_t(val));
993}
994
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700995status_t Parcel::writeInt64(int64_t val)
996{
Andreas Huber84a6d042009-08-17 13:33:27 -0700997 return writeAligned(val);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700998}
999
Ronghua Wu2d13afd2015-03-16 11:11:07 -07001000status_t Parcel::writeUint64(uint64_t val)
1001{
1002 return writeAligned(val);
1003}
1004
Serban Constantinescuf683e012013-11-05 16:53:55 +00001005status_t Parcel::writePointer(uintptr_t val)
1006{
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001007 return writeAligned<binder_uintptr_t>(val);
Serban Constantinescuf683e012013-11-05 16:53:55 +00001008}
1009
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001010status_t Parcel::writeFloat(float val)
1011{
Andreas Huber84a6d042009-08-17 13:33:27 -07001012 return writeAligned(val);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001013}
1014
Douglas Leungcc1a4bb2013-01-11 15:00:55 -08001015#if defined(__mips__) && defined(__mips_hard_float)
1016
1017status_t Parcel::writeDouble(double val)
1018{
1019 union {
1020 double d;
1021 unsigned long long ll;
1022 } u;
1023 u.d = val;
1024 return writeAligned(u.ll);
1025}
1026
1027#else
1028
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001029status_t Parcel::writeDouble(double val)
1030{
Andreas Huber84a6d042009-08-17 13:33:27 -07001031 return writeAligned(val);
1032}
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001033
Douglas Leungcc1a4bb2013-01-11 15:00:55 -08001034#endif
1035
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001036status_t Parcel::writeCString(const char* str)
1037{
1038 return write(str, strlen(str)+1);
1039}
1040
1041status_t Parcel::writeString8(const String8& str)
1042{
1043 status_t err = writeInt32(str.bytes());
Pravat Dalbeherad1dff8d2010-12-15 08:40:00 +01001044 // only write string if its length is more than zero characters,
1045 // as readString8 will only read if the length field is non-zero.
1046 // this is slightly different from how writeString16 works.
1047 if (str.bytes() > 0 && err == NO_ERROR) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001048 err = write(str.string(), str.bytes()+1);
1049 }
1050 return err;
1051}
1052
1053status_t Parcel::writeString16(const String16& str)
1054{
1055 return writeString16(str.string(), str.size());
1056}
1057
1058status_t Parcel::writeString16(const char16_t* str, size_t len)
1059{
1060 if (str == NULL) return writeInt32(-1);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001061
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001062 status_t err = writeInt32(len);
1063 if (err == NO_ERROR) {
1064 len *= sizeof(char16_t);
1065 uint8_t* data = (uint8_t*)writeInplace(len+sizeof(char16_t));
1066 if (data) {
1067 memcpy(data, str, len);
1068 *reinterpret_cast<char16_t*>(data+len) = 0;
1069 return NO_ERROR;
1070 }
1071 err = mError;
1072 }
1073 return err;
1074}
1075
1076status_t Parcel::writeStrongBinder(const sp<IBinder>& val)
1077{
1078 return flatten_binder(ProcessState::self(), val, this);
1079}
1080
Casey Dahlineb8e15f2015-11-03 13:50:37 -08001081status_t Parcel::writeStrongBinderVector(const std::vector<sp<IBinder>>& val)
1082{
1083 if (val.size() > std::numeric_limits<int32_t>::max()) {
1084 return BAD_VALUE;
1085 }
1086
1087 status_t status = writeInt32(val.size());
1088
1089 if (status != OK) {
1090 return status;
1091 }
1092
1093 for (const auto& item : val) {
1094 status = writeStrongBinder(item);
1095
1096 if (status != OK) {
1097 return status;
1098 }
1099 }
1100
1101 return OK;
1102}
1103
1104status_t Parcel::readStrongBinderVector(std::vector<sp<IBinder>>* val) const {
Christopher Wiley4db672d2015-11-10 09:44:30 -08001105 return readTypedVector(val, this, &Parcel::readStrongBinder);
Casey Dahlineb8e15f2015-11-03 13:50:37 -08001106}
1107
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001108status_t Parcel::writeWeakBinder(const wp<IBinder>& val)
1109{
1110 return flatten_binder(ProcessState::self(), val, this);
1111}
1112
Mathias Agopiana47f02a2009-05-21 16:29:38 -07001113status_t Parcel::writeNativeHandle(const native_handle* handle)
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001114{
Mathias Agopian1d0a95b2009-07-31 16:12:13 -07001115 if (!handle || handle->version != sizeof(native_handle))
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001116 return BAD_TYPE;
1117
1118 status_t err;
Mathias Agopiana47f02a2009-05-21 16:29:38 -07001119 err = writeInt32(handle->numFds);
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001120 if (err != NO_ERROR) return err;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001121
Mathias Agopiana47f02a2009-05-21 16:29:38 -07001122 err = writeInt32(handle->numInts);
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001123 if (err != NO_ERROR) return err;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001124
Mathias Agopiana47f02a2009-05-21 16:29:38 -07001125 for (int i=0 ; err==NO_ERROR && i<handle->numFds ; i++)
1126 err = writeDupFileDescriptor(handle->data[i]);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001127
1128 if (err != NO_ERROR) {
Steve Block9d453682011-12-20 16:23:08 +00001129 ALOGD("write native handle, write dup fd failed");
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001130 return err;
1131 }
Mathias Agopiana47f02a2009-05-21 16:29:38 -07001132 err = write(handle->data + handle->numFds, sizeof(int)*handle->numInts);
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001133 return err;
1134}
1135
Jeff Brown93ff1f92011-11-04 19:01:44 -07001136status_t Parcel::writeFileDescriptor(int fd, bool takeOwnership)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001137{
1138 flat_binder_object obj;
1139 obj.type = BINDER_TYPE_FD;
1140 obj.flags = 0x7f | FLAT_BINDER_FLAG_ACCEPTS_FDS;
Arve Hjønnevåg07fd0f12014-02-18 21:10:29 -08001141 obj.binder = 0; /* Don't pass uninitialized stack data to a remote process */
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001142 obj.handle = fd;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001143 obj.cookie = takeOwnership ? 1 : 0;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001144 return writeObject(obj, true);
1145}
1146
1147status_t Parcel::writeDupFileDescriptor(int fd)
1148{
Jeff Brownd341c712011-11-04 20:19:33 -07001149 int dupFd = dup(fd);
1150 if (dupFd < 0) {
1151 return -errno;
1152 }
1153 status_t err = writeFileDescriptor(dupFd, true /*takeOwnership*/);
1154 if (err) {
1155 close(dupFd);
1156 }
1157 return err;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001158}
1159
Jeff Brown13b16042014-11-11 16:44:25 -08001160status_t Parcel::writeBlob(size_t len, bool mutableCopy, WritableBlob* outBlob)
Jeff Brown5707dbf2011-09-23 21:17:56 -07001161{
Nick Kralevichb6b14232015-04-02 09:36:02 -07001162 if (len > INT32_MAX) {
1163 // don't accept size_t values which may have come from an
1164 // inadvertent conversion from a negative int.
1165 return BAD_VALUE;
1166 }
1167
Jeff Brown13b16042014-11-11 16:44:25 -08001168 status_t status;
1169 if (!mAllowFds || len <= BLOB_INPLACE_LIMIT) {
Steve Block6807e592011-10-20 11:56:00 +01001170 ALOGV("writeBlob: write in place");
Jeff Brown13b16042014-11-11 16:44:25 -08001171 status = writeInt32(BLOB_INPLACE);
Jeff Brown5707dbf2011-09-23 21:17:56 -07001172 if (status) return status;
1173
1174 void* ptr = writeInplace(len);
1175 if (!ptr) return NO_MEMORY;
1176
Jeff Brown13b16042014-11-11 16:44:25 -08001177 outBlob->init(-1, ptr, len, false);
Jeff Brown5707dbf2011-09-23 21:17:56 -07001178 return NO_ERROR;
1179 }
1180
Steve Block6807e592011-10-20 11:56:00 +01001181 ALOGV("writeBlob: write to ashmem");
Jeff Brown5707dbf2011-09-23 21:17:56 -07001182 int fd = ashmem_create_region("Parcel Blob", len);
1183 if (fd < 0) return NO_MEMORY;
1184
Dan Sandleraa5c2342015-04-10 10:08:45 -04001185 mBlobAshmemSize += len;
1186
Jeff Brown5707dbf2011-09-23 21:17:56 -07001187 int result = ashmem_set_prot_region(fd, PROT_READ | PROT_WRITE);
1188 if (result < 0) {
Jeff Brownec4e0062011-10-10 14:50:10 -07001189 status = result;
Jeff Brown5707dbf2011-09-23 21:17:56 -07001190 } else {
1191 void* ptr = ::mmap(NULL, len, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
1192 if (ptr == MAP_FAILED) {
1193 status = -errno;
1194 } else {
Jeff Brown13b16042014-11-11 16:44:25 -08001195 if (!mutableCopy) {
1196 result = ashmem_set_prot_region(fd, PROT_READ);
1197 }
Jeff Brown5707dbf2011-09-23 21:17:56 -07001198 if (result < 0) {
Jeff Brownec4e0062011-10-10 14:50:10 -07001199 status = result;
Jeff Brown5707dbf2011-09-23 21:17:56 -07001200 } else {
Jeff Brown13b16042014-11-11 16:44:25 -08001201 status = writeInt32(mutableCopy ? BLOB_ASHMEM_MUTABLE : BLOB_ASHMEM_IMMUTABLE);
Jeff Brown5707dbf2011-09-23 21:17:56 -07001202 if (!status) {
Jeff Brown93ff1f92011-11-04 19:01:44 -07001203 status = writeFileDescriptor(fd, true /*takeOwnership*/);
Jeff Brown5707dbf2011-09-23 21:17:56 -07001204 if (!status) {
Jeff Brown13b16042014-11-11 16:44:25 -08001205 outBlob->init(fd, ptr, len, mutableCopy);
Jeff Brown5707dbf2011-09-23 21:17:56 -07001206 return NO_ERROR;
1207 }
1208 }
1209 }
1210 }
1211 ::munmap(ptr, len);
1212 }
1213 ::close(fd);
1214 return status;
1215}
1216
Jeff Brown13b16042014-11-11 16:44:25 -08001217status_t Parcel::writeDupImmutableBlobFileDescriptor(int fd)
1218{
1219 // Must match up with what's done in writeBlob.
1220 if (!mAllowFds) return FDS_NOT_ALLOWED;
1221 status_t status = writeInt32(BLOB_ASHMEM_IMMUTABLE);
1222 if (status) return status;
1223 return writeDupFileDescriptor(fd);
1224}
1225
Mathias Agopiane1424282013-07-29 21:24:40 -07001226status_t Parcel::write(const FlattenableHelperInterface& val)
Mathias Agopian98e71dd2010-02-11 17:30:52 -08001227{
1228 status_t err;
1229
1230 // size if needed
Mathias Agopiane1424282013-07-29 21:24:40 -07001231 const size_t len = val.getFlattenedSize();
1232 const size_t fd_count = val.getFdCount();
Mathias Agopian98e71dd2010-02-11 17:30:52 -08001233
Nick Kralevichb6b14232015-04-02 09:36:02 -07001234 if ((len > INT32_MAX) || (fd_count > INT32_MAX)) {
1235 // don't accept size_t values which may have come from an
1236 // inadvertent conversion from a negative int.
1237 return BAD_VALUE;
1238 }
1239
Mathias Agopian98e71dd2010-02-11 17:30:52 -08001240 err = this->writeInt32(len);
1241 if (err) return err;
1242
1243 err = this->writeInt32(fd_count);
1244 if (err) return err;
1245
1246 // payload
Nick Kralevichb6b14232015-04-02 09:36:02 -07001247 void* const buf = this->writeInplace(pad_size(len));
Mathias Agopian98e71dd2010-02-11 17:30:52 -08001248 if (buf == NULL)
1249 return BAD_VALUE;
1250
1251 int* fds = NULL;
1252 if (fd_count) {
1253 fds = new int[fd_count];
1254 }
1255
1256 err = val.flatten(buf, len, fds, fd_count);
1257 for (size_t i=0 ; i<fd_count && err==NO_ERROR ; i++) {
1258 err = this->writeDupFileDescriptor( fds[i] );
1259 }
1260
1261 if (fd_count) {
1262 delete [] fds;
1263 }
1264
1265 return err;
1266}
1267
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001268status_t Parcel::writeObject(const flat_binder_object& val, bool nullMetaData)
1269{
1270 const bool enoughData = (mDataPos+sizeof(val)) <= mDataCapacity;
1271 const bool enoughObjects = mObjectsSize < mObjectsCapacity;
1272 if (enoughData && enoughObjects) {
1273restart_write:
1274 *reinterpret_cast<flat_binder_object*>(mData+mDataPos) = val;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001275
Christopher Tate98e67d32015-06-03 18:44:15 -07001276 // remember if it's a file descriptor
1277 if (val.type == BINDER_TYPE_FD) {
1278 if (!mAllowFds) {
1279 // fail before modifying our object index
1280 return FDS_NOT_ALLOWED;
1281 }
1282 mHasFds = mFdsKnown = true;
1283 }
1284
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001285 // Need to write meta-data?
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001286 if (nullMetaData || val.binder != 0) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001287 mObjects[mObjectsSize] = mDataPos;
1288 acquire_object(ProcessState::self(), val, this);
1289 mObjectsSize++;
1290 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001291
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001292 return finishWrite(sizeof(flat_binder_object));
1293 }
1294
1295 if (!enoughData) {
1296 const status_t err = growData(sizeof(val));
1297 if (err != NO_ERROR) return err;
1298 }
1299 if (!enoughObjects) {
1300 size_t newSize = ((mObjectsSize+2)*3)/2;
Christopher Tateed7a50c2015-06-08 14:45:14 -07001301 if (newSize < mObjectsSize) return NO_MEMORY; // overflow
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001302 binder_size_t* objects = (binder_size_t*)realloc(mObjects, newSize*sizeof(binder_size_t));
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001303 if (objects == NULL) return NO_MEMORY;
1304 mObjects = objects;
1305 mObjectsCapacity = newSize;
1306 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001307
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001308 goto restart_write;
1309}
1310
Brad Fitzpatrick837a0d02010-07-13 15:33:35 -07001311status_t Parcel::writeNoException()
1312{
1313 return writeInt32(0);
1314}
1315
Colin Cross6f4f3ab2014-02-05 17:42:44 -08001316void Parcel::remove(size_t /*start*/, size_t /*amt*/)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001317{
1318 LOG_ALWAYS_FATAL("Parcel::remove() not yet implemented!");
1319}
1320
1321status_t Parcel::read(void* outData, size_t len) const
1322{
Nick Kralevichb6b14232015-04-02 09:36:02 -07001323 if (len > INT32_MAX) {
1324 // don't accept size_t values which may have come from an
1325 // inadvertent conversion from a negative int.
1326 return BAD_VALUE;
1327 }
1328
1329 if ((mDataPos+pad_size(len)) >= mDataPos && (mDataPos+pad_size(len)) <= mDataSize
1330 && len <= pad_size(len)) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001331 memcpy(outData, mData+mDataPos, len);
Nick Kralevichb6b14232015-04-02 09:36:02 -07001332 mDataPos += pad_size(len);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001333 ALOGV("read Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001334 return NO_ERROR;
1335 }
1336 return NOT_ENOUGH_DATA;
1337}
1338
1339const void* Parcel::readInplace(size_t len) const
1340{
Nick Kralevichb6b14232015-04-02 09:36:02 -07001341 if (len > INT32_MAX) {
1342 // don't accept size_t values which may have come from an
1343 // inadvertent conversion from a negative int.
1344 return NULL;
1345 }
1346
1347 if ((mDataPos+pad_size(len)) >= mDataPos && (mDataPos+pad_size(len)) <= mDataSize
1348 && len <= pad_size(len)) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001349 const void* data = mData+mDataPos;
Nick Kralevichb6b14232015-04-02 09:36:02 -07001350 mDataPos += pad_size(len);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001351 ALOGV("readInplace Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001352 return data;
1353 }
1354 return NULL;
1355}
1356
Andreas Huber84a6d042009-08-17 13:33:27 -07001357template<class T>
1358status_t Parcel::readAligned(T *pArg) const {
Nick Kralevichb6b14232015-04-02 09:36:02 -07001359 COMPILE_TIME_ASSERT_FUNCTION_SCOPE(PAD_SIZE_UNSAFE(sizeof(T)) == sizeof(T));
Andreas Huber84a6d042009-08-17 13:33:27 -07001360
1361 if ((mDataPos+sizeof(T)) <= mDataSize) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001362 const void* data = mData+mDataPos;
Andreas Huber84a6d042009-08-17 13:33:27 -07001363 mDataPos += sizeof(T);
1364 *pArg = *reinterpret_cast<const T*>(data);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001365 return NO_ERROR;
1366 } else {
1367 return NOT_ENOUGH_DATA;
1368 }
1369}
1370
Andreas Huber84a6d042009-08-17 13:33:27 -07001371template<class T>
1372T Parcel::readAligned() const {
1373 T result;
1374 if (readAligned(&result) != NO_ERROR) {
1375 result = 0;
1376 }
1377
1378 return result;
1379}
1380
1381template<class T>
1382status_t Parcel::writeAligned(T val) {
Nick Kralevichb6b14232015-04-02 09:36:02 -07001383 COMPILE_TIME_ASSERT_FUNCTION_SCOPE(PAD_SIZE_UNSAFE(sizeof(T)) == sizeof(T));
Andreas Huber84a6d042009-08-17 13:33:27 -07001384
1385 if ((mDataPos+sizeof(val)) <= mDataCapacity) {
1386restart_write:
1387 *reinterpret_cast<T*>(mData+mDataPos) = val;
1388 return finishWrite(sizeof(val));
1389 }
1390
1391 status_t err = growData(sizeof(val));
1392 if (err == NO_ERROR) goto restart_write;
1393 return err;
1394}
1395
Casey Dahlin451ff582015-10-19 18:12:18 -07001396status_t Parcel::readByteVector(std::vector<int8_t>* val) const {
1397 val->clear();
1398
1399 int32_t size;
1400 status_t status = readInt32(&size);
1401
1402 if (status != OK) {
1403 return status;
1404 }
1405
Christopher Wiley4db672d2015-11-10 09:44:30 -08001406 if (size < 0) {
1407 status = UNEXPECTED_NULL;
1408 return status;
1409 }
1410 if (size_t(size) > dataAvail()) {
Christopher Wileyf0fc52b2015-10-31 13:22:15 -07001411 status = BAD_VALUE;
1412 return status;
Casey Dahlin451ff582015-10-19 18:12:18 -07001413 }
Christopher Wiley4db672d2015-11-10 09:44:30 -08001414
Christopher Wileyf0fc52b2015-10-31 13:22:15 -07001415 const void* data = readInplace(size);
1416 if (!data) {
1417 status = BAD_VALUE;
1418 return status;
1419 }
Casey Dahlin451ff582015-10-19 18:12:18 -07001420 val->resize(size);
Christopher Wileyf0fc52b2015-10-31 13:22:15 -07001421 memcpy(val->data(), data, size);
Casey Dahlin451ff582015-10-19 18:12:18 -07001422
Christopher Wileyf0fc52b2015-10-31 13:22:15 -07001423 return status;
Casey Dahlin451ff582015-10-19 18:12:18 -07001424}
1425
1426status_t Parcel::readInt32Vector(std::vector<int32_t>* val) const {
Christopher Wiley4db672d2015-11-10 09:44:30 -08001427 return readTypedVector(val, this, &Parcel::readInt32);
Casey Dahlin451ff582015-10-19 18:12:18 -07001428}
1429
1430status_t Parcel::readInt64Vector(std::vector<int64_t>* val) const {
Christopher Wiley4db672d2015-11-10 09:44:30 -08001431 return readTypedVector(val, this, &Parcel::readInt64);
Casey Dahlin451ff582015-10-19 18:12:18 -07001432}
1433
1434status_t Parcel::readFloatVector(std::vector<float>* val) const {
Christopher Wiley4db672d2015-11-10 09:44:30 -08001435 return readTypedVector(val, this, &Parcel::readFloat);
Casey Dahlin451ff582015-10-19 18:12:18 -07001436}
1437
1438status_t Parcel::readDoubleVector(std::vector<double>* val) const {
Christopher Wiley4db672d2015-11-10 09:44:30 -08001439 return readTypedVector(val, this, &Parcel::readDouble);
Casey Dahlin451ff582015-10-19 18:12:18 -07001440}
1441
1442status_t Parcel::readBoolVector(std::vector<bool>* val) const {
1443 val->clear();
1444
1445 int32_t size;
1446 status_t status = readInt32(&size);
1447
1448 if (status != OK) {
1449 return status;
1450 }
1451
1452 if (size < 0) {
Christopher Wiley4db672d2015-11-10 09:44:30 -08001453 return UNEXPECTED_NULL;
Casey Dahlin451ff582015-10-19 18:12:18 -07001454 }
1455
1456 val->resize(size);
1457
1458 /* C++ bool handling means a vector of bools isn't necessarily addressable
1459 * (we might use individual bits)
1460 */
Christopher Wiley97887982015-10-27 16:33:47 -07001461 bool data;
1462 for (int32_t i = 0; i < size; ++i) {
Casey Dahlin451ff582015-10-19 18:12:18 -07001463 status = readBool(&data);
1464 (*val)[i] = data;
1465
1466 if (status != OK) {
1467 return status;
1468 }
1469 }
1470
1471 return OK;
1472}
1473
1474status_t Parcel::readCharVector(std::vector<char16_t>* val) const {
Christopher Wiley4db672d2015-11-10 09:44:30 -08001475 return readTypedVector(val, this, &Parcel::readChar);
Casey Dahlin451ff582015-10-19 18:12:18 -07001476}
1477
1478status_t Parcel::readString16Vector(std::vector<String16>* val) const {
Christopher Wiley4db672d2015-11-10 09:44:30 -08001479 return readTypedVector(val, this, &Parcel::readString16);
Casey Dahlin451ff582015-10-19 18:12:18 -07001480}
1481
1482
Andreas Huber84a6d042009-08-17 13:33:27 -07001483status_t Parcel::readInt32(int32_t *pArg) const
1484{
1485 return readAligned(pArg);
1486}
1487
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001488int32_t Parcel::readInt32() const
1489{
Andreas Huber84a6d042009-08-17 13:33:27 -07001490 return readAligned<int32_t>();
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001491}
1492
Dan Stoza41a0f2f2014-12-01 10:01:10 -08001493status_t Parcel::readUint32(uint32_t *pArg) const
1494{
1495 return readAligned(pArg);
1496}
1497
1498uint32_t Parcel::readUint32() const
1499{
1500 return readAligned<uint32_t>();
1501}
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001502
1503status_t Parcel::readInt64(int64_t *pArg) const
1504{
Andreas Huber84a6d042009-08-17 13:33:27 -07001505 return readAligned(pArg);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001506}
1507
1508
1509int64_t Parcel::readInt64() const
1510{
Andreas Huber84a6d042009-08-17 13:33:27 -07001511 return readAligned<int64_t>();
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001512}
1513
Ronghua Wu2d13afd2015-03-16 11:11:07 -07001514status_t Parcel::readUint64(uint64_t *pArg) const
1515{
1516 return readAligned(pArg);
1517}
1518
1519uint64_t Parcel::readUint64() const
1520{
1521 return readAligned<uint64_t>();
1522}
1523
Serban Constantinescuf683e012013-11-05 16:53:55 +00001524status_t Parcel::readPointer(uintptr_t *pArg) const
1525{
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001526 status_t ret;
1527 binder_uintptr_t ptr;
1528 ret = readAligned(&ptr);
1529 if (!ret)
1530 *pArg = ptr;
1531 return ret;
Serban Constantinescuf683e012013-11-05 16:53:55 +00001532}
1533
1534uintptr_t Parcel::readPointer() const
1535{
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001536 return readAligned<binder_uintptr_t>();
Serban Constantinescuf683e012013-11-05 16:53:55 +00001537}
1538
1539
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001540status_t Parcel::readFloat(float *pArg) const
1541{
Andreas Huber84a6d042009-08-17 13:33:27 -07001542 return readAligned(pArg);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001543}
1544
1545
1546float Parcel::readFloat() const
1547{
Andreas Huber84a6d042009-08-17 13:33:27 -07001548 return readAligned<float>();
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001549}
1550
Douglas Leungcc1a4bb2013-01-11 15:00:55 -08001551#if defined(__mips__) && defined(__mips_hard_float)
1552
1553status_t Parcel::readDouble(double *pArg) const
1554{
1555 union {
1556 double d;
1557 unsigned long long ll;
1558 } u;
Narayan Kamath2c68d382014-06-04 15:04:29 +01001559 u.d = 0;
Douglas Leungcc1a4bb2013-01-11 15:00:55 -08001560 status_t status;
1561 status = readAligned(&u.ll);
1562 *pArg = u.d;
1563 return status;
1564}
1565
1566double Parcel::readDouble() const
1567{
1568 union {
1569 double d;
1570 unsigned long long ll;
1571 } u;
1572 u.ll = readAligned<unsigned long long>();
1573 return u.d;
1574}
1575
1576#else
1577
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001578status_t Parcel::readDouble(double *pArg) const
1579{
Andreas Huber84a6d042009-08-17 13:33:27 -07001580 return readAligned(pArg);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001581}
1582
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001583double Parcel::readDouble() const
1584{
Andreas Huber84a6d042009-08-17 13:33:27 -07001585 return readAligned<double>();
1586}
1587
Douglas Leungcc1a4bb2013-01-11 15:00:55 -08001588#endif
1589
Andreas Huber84a6d042009-08-17 13:33:27 -07001590status_t Parcel::readIntPtr(intptr_t *pArg) const
1591{
1592 return readAligned(pArg);
1593}
1594
1595
1596intptr_t Parcel::readIntPtr() const
1597{
1598 return readAligned<intptr_t>();
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001599}
1600
Casey Dahlind6848f52015-10-15 15:44:59 -07001601status_t Parcel::readBool(bool *pArg) const
1602{
1603 int32_t tmp;
1604 status_t ret = readInt32(&tmp);
1605 *pArg = (tmp != 0);
1606 return ret;
1607}
1608
1609bool Parcel::readBool() const
1610{
1611 return readInt32() != 0;
1612}
1613
1614status_t Parcel::readChar(char16_t *pArg) const
1615{
1616 int32_t tmp;
1617 status_t ret = readInt32(&tmp);
1618 *pArg = char16_t(tmp);
1619 return ret;
1620}
1621
1622char16_t Parcel::readChar() const
1623{
1624 return char16_t(readInt32());
1625}
1626
1627status_t Parcel::readByte(int8_t *pArg) const
1628{
1629 int32_t tmp;
1630 status_t ret = readInt32(&tmp);
1631 *pArg = int8_t(tmp);
1632 return ret;
1633}
1634
1635int8_t Parcel::readByte() const
1636{
1637 return int8_t(readInt32());
1638}
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001639
1640const char* Parcel::readCString() const
1641{
1642 const size_t avail = mDataSize-mDataPos;
1643 if (avail > 0) {
1644 const char* str = reinterpret_cast<const char*>(mData+mDataPos);
1645 // is the string's trailing NUL within the parcel's valid bounds?
1646 const char* eos = reinterpret_cast<const char*>(memchr(str, 0, avail));
1647 if (eos) {
1648 const size_t len = eos - str;
Nick Kralevichb6b14232015-04-02 09:36:02 -07001649 mDataPos += pad_size(len+1);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001650 ALOGV("readCString Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001651 return str;
1652 }
1653 }
1654 return NULL;
1655}
1656
1657String8 Parcel::readString8() const
1658{
1659 int32_t size = readInt32();
1660 // watch for potential int overflow adding 1 for trailing NUL
1661 if (size > 0 && size < INT32_MAX) {
1662 const char* str = (const char*)readInplace(size+1);
1663 if (str) return String8(str, size);
1664 }
1665 return String8();
1666}
1667
1668String16 Parcel::readString16() const
1669{
1670 size_t len;
1671 const char16_t* str = readString16Inplace(&len);
1672 if (str) return String16(str, len);
Steve Blocke6f43dd2012-01-06 19:20:56 +00001673 ALOGE("Reading a NULL string not supported here.");
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001674 return String16();
1675}
1676
Casey Dahlin451ff582015-10-19 18:12:18 -07001677status_t Parcel::readString16(String16* pArg) const
1678{
1679 size_t len;
1680 const char16_t* str = readString16Inplace(&len);
1681 if (str) {
Casey Dahlin1515ea12015-10-20 16:26:23 -07001682 pArg->setTo(str, len);
Casey Dahlin451ff582015-10-19 18:12:18 -07001683 return 0;
1684 } else {
1685 *pArg = String16();
Christopher Wiley4db672d2015-11-10 09:44:30 -08001686 return UNEXPECTED_NULL;
Casey Dahlin451ff582015-10-19 18:12:18 -07001687 }
1688}
1689
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001690const char16_t* Parcel::readString16Inplace(size_t* outLen) const
1691{
1692 int32_t size = readInt32();
1693 // watch for potential int overflow from size+1
1694 if (size >= 0 && size < INT32_MAX) {
1695 *outLen = size;
1696 const char16_t* str = (const char16_t*)readInplace((size+1)*sizeof(char16_t));
1697 if (str != NULL) {
1698 return str;
1699 }
1700 }
1701 *outLen = 0;
1702 return NULL;
1703}
1704
Casey Dahlinf0c13772015-10-27 18:33:56 -07001705status_t Parcel::readStrongBinder(sp<IBinder>* val) const
1706{
1707 return unflatten_binder(ProcessState::self(), *this, val);
1708}
1709
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001710sp<IBinder> Parcel::readStrongBinder() const
1711{
1712 sp<IBinder> val;
Casey Dahlinf0c13772015-10-27 18:33:56 -07001713 readStrongBinder(&val);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001714 return val;
1715}
1716
1717wp<IBinder> Parcel::readWeakBinder() const
1718{
1719 wp<IBinder> val;
1720 unflatten_binder(ProcessState::self(), *this, &val);
1721 return val;
1722}
1723
Brad Fitzpatrick837a0d02010-07-13 15:33:35 -07001724int32_t Parcel::readExceptionCode() const
1725{
1726 int32_t exception_code = readAligned<int32_t>();
Brad Fitzpatrickd36f4a52010-07-12 11:05:38 -07001727 if (exception_code == EX_HAS_REPLY_HEADER) {
Magnus Strandberg1ba24572011-05-03 15:44:00 +02001728 int32_t header_start = dataPosition();
Brad Fitzpatrickd36f4a52010-07-12 11:05:38 -07001729 int32_t header_size = readAligned<int32_t>();
1730 // Skip over fat responses headers. Not used (or propagated) in
1731 // native code
Magnus Strandberg1ba24572011-05-03 15:44:00 +02001732 setDataPosition(header_start + header_size);
Brad Fitzpatrickd36f4a52010-07-12 11:05:38 -07001733 // And fat response headers are currently only used when there are no
1734 // exceptions, so return no error:
1735 return 0;
1736 }
Brad Fitzpatrick837a0d02010-07-13 15:33:35 -07001737 return exception_code;
1738}
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001739
Mathias Agopiana47f02a2009-05-21 16:29:38 -07001740native_handle* Parcel::readNativeHandle() const
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001741{
1742 int numFds, numInts;
1743 status_t err;
1744 err = readInt32(&numFds);
1745 if (err != NO_ERROR) return 0;
1746 err = readInt32(&numInts);
1747 if (err != NO_ERROR) return 0;
1748
Mathias Agopiana47f02a2009-05-21 16:29:38 -07001749 native_handle* h = native_handle_create(numFds, numInts);
Adam Lesinskieaac99a2015-05-12 17:35:48 -07001750 if (!h) {
1751 return 0;
1752 }
1753
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001754 for (int i=0 ; err==NO_ERROR && i<numFds ; i++) {
Rebecca Schultz Zavin360211f2009-02-13 16:34:38 -08001755 h->data[i] = dup(readFileDescriptor());
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001756 if (h->data[i] < 0) err = BAD_VALUE;
1757 }
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001758 err = read(h->data + numFds, sizeof(int)*numInts);
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001759 if (err != NO_ERROR) {
Mathias Agopiana47f02a2009-05-21 16:29:38 -07001760 native_handle_close(h);
1761 native_handle_delete(h);
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001762 h = 0;
1763 }
1764 return h;
1765}
1766
1767
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001768int Parcel::readFileDescriptor() const
1769{
1770 const flat_binder_object* flat = readObject(true);
1771 if (flat) {
1772 switch (flat->type) {
1773 case BINDER_TYPE_FD:
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001774 //ALOGI("Returning file descriptor %ld from parcel %p", flat->handle, this);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001775 return flat->handle;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001776 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001777 }
1778 return BAD_TYPE;
1779}
1780
Jeff Brown5707dbf2011-09-23 21:17:56 -07001781status_t Parcel::readBlob(size_t len, ReadableBlob* outBlob) const
1782{
Jeff Brown13b16042014-11-11 16:44:25 -08001783 int32_t blobType;
1784 status_t status = readInt32(&blobType);
Jeff Brown5707dbf2011-09-23 21:17:56 -07001785 if (status) return status;
1786
Jeff Brown13b16042014-11-11 16:44:25 -08001787 if (blobType == BLOB_INPLACE) {
Steve Block6807e592011-10-20 11:56:00 +01001788 ALOGV("readBlob: read in place");
Jeff Brown5707dbf2011-09-23 21:17:56 -07001789 const void* ptr = readInplace(len);
1790 if (!ptr) return BAD_VALUE;
1791
Jeff Brown13b16042014-11-11 16:44:25 -08001792 outBlob->init(-1, const_cast<void*>(ptr), len, false);
Jeff Brown5707dbf2011-09-23 21:17:56 -07001793 return NO_ERROR;
1794 }
1795
Steve Block6807e592011-10-20 11:56:00 +01001796 ALOGV("readBlob: read from ashmem");
Jeff Brown13b16042014-11-11 16:44:25 -08001797 bool isMutable = (blobType == BLOB_ASHMEM_MUTABLE);
Jeff Brown5707dbf2011-09-23 21:17:56 -07001798 int fd = readFileDescriptor();
1799 if (fd == int(BAD_TYPE)) return BAD_VALUE;
1800
Jeff Brown13b16042014-11-11 16:44:25 -08001801 void* ptr = ::mmap(NULL, len, isMutable ? PROT_READ | PROT_WRITE : PROT_READ,
1802 MAP_SHARED, fd, 0);
Narayan Kamath9ea09752014-10-08 17:35:45 +01001803 if (ptr == MAP_FAILED) return NO_MEMORY;
Jeff Brown5707dbf2011-09-23 21:17:56 -07001804
Jeff Brown13b16042014-11-11 16:44:25 -08001805 outBlob->init(fd, ptr, len, isMutable);
Jeff Brown5707dbf2011-09-23 21:17:56 -07001806 return NO_ERROR;
1807}
1808
Mathias Agopiane1424282013-07-29 21:24:40 -07001809status_t Parcel::read(FlattenableHelperInterface& val) const
Mathias Agopian98e71dd2010-02-11 17:30:52 -08001810{
1811 // size
1812 const size_t len = this->readInt32();
1813 const size_t fd_count = this->readInt32();
1814
Nick Kralevichb6b14232015-04-02 09:36:02 -07001815 if (len > INT32_MAX) {
1816 // don't accept size_t values which may have come from an
1817 // inadvertent conversion from a negative int.
1818 return BAD_VALUE;
1819 }
1820
Mathias Agopian98e71dd2010-02-11 17:30:52 -08001821 // payload
Nick Kralevichb6b14232015-04-02 09:36:02 -07001822 void const* const buf = this->readInplace(pad_size(len));
Mathias Agopian98e71dd2010-02-11 17:30:52 -08001823 if (buf == NULL)
1824 return BAD_VALUE;
1825
1826 int* fds = NULL;
1827 if (fd_count) {
1828 fds = new int[fd_count];
1829 }
1830
1831 status_t err = NO_ERROR;
1832 for (size_t i=0 ; i<fd_count && err==NO_ERROR ; i++) {
Jesse Hallfee99042014-11-04 08:36:31 -08001833 fds[i] = dup(this->readFileDescriptor());
Jun Jiangabf8a2c2014-04-29 14:22:10 +08001834 if (fds[i] < 0) {
1835 err = BAD_VALUE;
Jesse Hallfee99042014-11-04 08:36:31 -08001836 ALOGE("dup() failed in Parcel::read, i is %zu, fds[i] is %d, fd_count is %zu, error: %s",
1837 i, fds[i], fd_count, strerror(errno));
Jun Jiangabf8a2c2014-04-29 14:22:10 +08001838 }
Mathias Agopian98e71dd2010-02-11 17:30:52 -08001839 }
1840
1841 if (err == NO_ERROR) {
1842 err = val.unflatten(buf, len, fds, fd_count);
1843 }
1844
1845 if (fd_count) {
1846 delete [] fds;
1847 }
1848
1849 return err;
1850}
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001851const flat_binder_object* Parcel::readObject(bool nullMetaData) const
1852{
1853 const size_t DPOS = mDataPos;
1854 if ((DPOS+sizeof(flat_binder_object)) <= mDataSize) {
1855 const flat_binder_object* obj
1856 = reinterpret_cast<const flat_binder_object*>(mData+DPOS);
1857 mDataPos = DPOS + sizeof(flat_binder_object);
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001858 if (!nullMetaData && (obj->cookie == 0 && obj->binder == 0)) {
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001859 // When transferring a NULL object, we don't write it into
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001860 // the object list, so we don't want to check for it when
1861 // reading.
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001862 ALOGV("readObject Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001863 return obj;
1864 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001865
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001866 // Ensure that this object is valid...
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001867 binder_size_t* const OBJS = mObjects;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001868 const size_t N = mObjectsSize;
1869 size_t opos = mNextObjectHint;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001870
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001871 if (N > 0) {
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001872 ALOGV("Parcel %p looking for obj at %zu, hint=%zu",
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001873 this, DPOS, opos);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001874
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001875 // Start at the current hint position, looking for an object at
1876 // the current data position.
1877 if (opos < N) {
1878 while (opos < (N-1) && OBJS[opos] < DPOS) {
1879 opos++;
1880 }
1881 } else {
1882 opos = N-1;
1883 }
1884 if (OBJS[opos] == DPOS) {
1885 // Found it!
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001886 ALOGV("Parcel %p found obj %zu at index %zu with forward search",
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001887 this, DPOS, opos);
1888 mNextObjectHint = opos+1;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001889 ALOGV("readObject Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001890 return obj;
1891 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001892
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001893 // Look backwards for it...
1894 while (opos > 0 && OBJS[opos] > DPOS) {
1895 opos--;
1896 }
1897 if (OBJS[opos] == DPOS) {
1898 // Found it!
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001899 ALOGV("Parcel %p found obj %zu at index %zu with backward search",
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001900 this, DPOS, opos);
1901 mNextObjectHint = opos+1;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001902 ALOGV("readObject Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001903 return obj;
1904 }
1905 }
Colin Cross6f4f3ab2014-02-05 17:42:44 -08001906 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 -07001907 this, DPOS);
1908 }
1909 return NULL;
1910}
1911
1912void Parcel::closeFileDescriptors()
1913{
1914 size_t i = mObjectsSize;
1915 if (i > 0) {
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001916 //ALOGI("Closing file descriptors for %zu objects...", i);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001917 }
1918 while (i > 0) {
1919 i--;
1920 const flat_binder_object* flat
1921 = reinterpret_cast<flat_binder_object*>(mData+mObjects[i]);
1922 if (flat->type == BINDER_TYPE_FD) {
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001923 //ALOGI("Closing fd: %ld", flat->handle);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001924 close(flat->handle);
1925 }
1926 }
1927}
1928
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001929uintptr_t Parcel::ipcData() const
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001930{
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001931 return reinterpret_cast<uintptr_t>(mData);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001932}
1933
1934size_t Parcel::ipcDataSize() const
1935{
1936 return (mDataSize > mDataPos ? mDataSize : mDataPos);
1937}
1938
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001939uintptr_t Parcel::ipcObjects() const
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001940{
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001941 return reinterpret_cast<uintptr_t>(mObjects);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001942}
1943
1944size_t Parcel::ipcObjectsCount() const
1945{
1946 return mObjectsSize;
1947}
1948
1949void Parcel::ipcSetDataReference(const uint8_t* data, size_t dataSize,
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001950 const binder_size_t* objects, size_t objectsCount, release_func relFunc, void* relCookie)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001951{
Arve Hjønnevåg6f286112014-02-19 20:42:13 -08001952 binder_size_t minOffset = 0;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001953 freeDataNoInit();
1954 mError = NO_ERROR;
1955 mData = const_cast<uint8_t*>(data);
1956 mDataSize = mDataCapacity = dataSize;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001957 //ALOGI("setDataReference Setting data size of %p to %lu (pid=%d)", this, mDataSize, getpid());
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001958 mDataPos = 0;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001959 ALOGV("setDataReference Setting data pos of %p to %zu", this, mDataPos);
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001960 mObjects = const_cast<binder_size_t*>(objects);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001961 mObjectsSize = mObjectsCapacity = objectsCount;
1962 mNextObjectHint = 0;
1963 mOwner = relFunc;
1964 mOwnerCookie = relCookie;
Arve Hjønnevågf50b9ea2014-02-13 19:22:08 -08001965 for (size_t i = 0; i < mObjectsSize; i++) {
Arve Hjønnevåg6f286112014-02-19 20:42:13 -08001966 binder_size_t offset = mObjects[i];
Arve Hjønnevågf50b9ea2014-02-13 19:22:08 -08001967 if (offset < minOffset) {
Dan Albert3bdc5b82014-11-20 11:50:23 -08001968 ALOGE("%s: bad object offset %" PRIu64 " < %" PRIu64 "\n",
Arve Hjønnevåg6f286112014-02-19 20:42:13 -08001969 __func__, (uint64_t)offset, (uint64_t)minOffset);
Arve Hjønnevågf50b9ea2014-02-13 19:22:08 -08001970 mObjectsSize = 0;
1971 break;
1972 }
1973 minOffset = offset + sizeof(flat_binder_object);
1974 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001975 scanForFds();
1976}
1977
Colin Cross6f4f3ab2014-02-05 17:42:44 -08001978void Parcel::print(TextOutput& to, uint32_t /*flags*/) const
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001979{
1980 to << "Parcel(";
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001981
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001982 if (errorCheck() != NO_ERROR) {
1983 const status_t err = errorCheck();
Colin Cross6f4f3ab2014-02-05 17:42:44 -08001984 to << "Error: " << (void*)(intptr_t)err << " \"" << strerror(-err) << "\"";
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001985 } else if (dataSize() > 0) {
1986 const uint8_t* DATA = data();
1987 to << indent << HexDump(DATA, dataSize()) << dedent;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001988 const binder_size_t* OBJS = objects();
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001989 const size_t N = objectsCount();
1990 for (size_t i=0; i<N; i++) {
1991 const flat_binder_object* flat
1992 = reinterpret_cast<const flat_binder_object*>(DATA+OBJS[i]);
1993 to << endl << "Object #" << i << " @ " << (void*)OBJS[i] << ": "
1994 << TypeCode(flat->type & 0x7f7f7f00)
1995 << " = " << flat->binder;
1996 }
1997 } else {
1998 to << "NULL";
1999 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002000
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002001 to << ")";
2002}
2003
2004void Parcel::releaseObjects()
2005{
2006 const sp<ProcessState> proc(ProcessState::self());
2007 size_t i = mObjectsSize;
2008 uint8_t* const data = mData;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002009 binder_size_t* const objects = mObjects;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002010 while (i > 0) {
2011 i--;
2012 const flat_binder_object* flat
2013 = reinterpret_cast<flat_binder_object*>(data+objects[i]);
2014 release_object(proc, *flat, this);
2015 }
2016}
2017
2018void Parcel::acquireObjects()
2019{
2020 const sp<ProcessState> proc(ProcessState::self());
2021 size_t i = mObjectsSize;
2022 uint8_t* const data = mData;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002023 binder_size_t* const objects = mObjects;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002024 while (i > 0) {
2025 i--;
2026 const flat_binder_object* flat
2027 = reinterpret_cast<flat_binder_object*>(data+objects[i]);
2028 acquire_object(proc, *flat, this);
2029 }
2030}
2031
2032void Parcel::freeData()
2033{
2034 freeDataNoInit();
2035 initState();
2036}
2037
2038void Parcel::freeDataNoInit()
2039{
2040 if (mOwner) {
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002041 LOG_ALLOC("Parcel %p: freeing other owner data", this);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002042 //ALOGI("Freeing data ref of %p (pid=%d)", this, getpid());
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002043 mOwner(this, mData, mDataSize, mObjects, mObjectsSize, mOwnerCookie);
2044 } else {
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002045 LOG_ALLOC("Parcel %p: freeing allocated data", this);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002046 releaseObjects();
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002047 if (mData) {
2048 LOG_ALLOC("Parcel %p: freeing with %zu capacity", this, mDataCapacity);
Dianne Hackborna4cff882014-11-13 17:07:40 -08002049 pthread_mutex_lock(&gParcelGlobalAllocSizeLock);
Dan Austin48fd7b42015-09-10 13:46:02 -07002050 if (mDataCapacity <= gParcelGlobalAllocSize) {
2051 gParcelGlobalAllocSize = gParcelGlobalAllocSize - mDataCapacity;
2052 } else {
2053 gParcelGlobalAllocSize = 0;
2054 }
2055 if (gParcelGlobalAllocCount > 0) {
2056 gParcelGlobalAllocCount--;
2057 }
Dianne Hackborna4cff882014-11-13 17:07:40 -08002058 pthread_mutex_unlock(&gParcelGlobalAllocSizeLock);
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002059 free(mData);
2060 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002061 if (mObjects) free(mObjects);
2062 }
2063}
2064
2065status_t Parcel::growData(size_t len)
2066{
Nick Kralevichb6b14232015-04-02 09:36:02 -07002067 if (len > INT32_MAX) {
2068 // don't accept size_t values which may have come from an
2069 // inadvertent conversion from a negative int.
2070 return BAD_VALUE;
2071 }
2072
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002073 size_t newSize = ((mDataSize+len)*3)/2;
2074 return (newSize <= mDataSize)
2075 ? (status_t) NO_MEMORY
2076 : continueWrite(newSize);
2077}
2078
2079status_t Parcel::restartWrite(size_t desired)
2080{
Nick Kralevichb6b14232015-04-02 09:36:02 -07002081 if (desired > INT32_MAX) {
2082 // don't accept size_t values which may have come from an
2083 // inadvertent conversion from a negative int.
2084 return BAD_VALUE;
2085 }
2086
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002087 if (mOwner) {
2088 freeData();
2089 return continueWrite(desired);
2090 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002091
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002092 uint8_t* data = (uint8_t*)realloc(mData, desired);
2093 if (!data && desired > mDataCapacity) {
2094 mError = NO_MEMORY;
2095 return NO_MEMORY;
2096 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002097
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002098 releaseObjects();
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002099
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002100 if (data) {
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002101 LOG_ALLOC("Parcel %p: restart from %zu to %zu capacity", this, mDataCapacity, desired);
Dianne Hackborna4cff882014-11-13 17:07:40 -08002102 pthread_mutex_lock(&gParcelGlobalAllocSizeLock);
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002103 gParcelGlobalAllocSize += desired;
2104 gParcelGlobalAllocSize -= mDataCapacity;
Dianne Hackborna4cff882014-11-13 17:07:40 -08002105 pthread_mutex_unlock(&gParcelGlobalAllocSizeLock);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002106 mData = data;
2107 mDataCapacity = desired;
2108 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002109
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002110 mDataSize = mDataPos = 0;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002111 ALOGV("restartWrite Setting data size of %p to %zu", this, mDataSize);
2112 ALOGV("restartWrite Setting data pos of %p to %zu", this, mDataPos);
2113
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002114 free(mObjects);
2115 mObjects = NULL;
2116 mObjectsSize = mObjectsCapacity = 0;
2117 mNextObjectHint = 0;
2118 mHasFds = false;
2119 mFdsKnown = true;
Dianne Hackborn8938ed22011-09-28 23:19:47 -04002120 mAllowFds = true;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002121
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002122 return NO_ERROR;
2123}
2124
2125status_t Parcel::continueWrite(size_t desired)
2126{
Nick Kralevichb6b14232015-04-02 09:36:02 -07002127 if (desired > INT32_MAX) {
2128 // don't accept size_t values which may have come from an
2129 // inadvertent conversion from a negative int.
2130 return BAD_VALUE;
2131 }
2132
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002133 // If shrinking, first adjust for any objects that appear
2134 // after the new data size.
2135 size_t objectsSize = mObjectsSize;
2136 if (desired < mDataSize) {
2137 if (desired == 0) {
2138 objectsSize = 0;
2139 } else {
2140 while (objectsSize > 0) {
2141 if (mObjects[objectsSize-1] < desired)
2142 break;
2143 objectsSize--;
2144 }
2145 }
2146 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002147
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002148 if (mOwner) {
2149 // If the size is going to zero, just release the owner's data.
2150 if (desired == 0) {
2151 freeData();
2152 return NO_ERROR;
2153 }
2154
2155 // If there is a different owner, we need to take
2156 // posession.
2157 uint8_t* data = (uint8_t*)malloc(desired);
2158 if (!data) {
2159 mError = NO_MEMORY;
2160 return NO_MEMORY;
2161 }
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002162 binder_size_t* objects = NULL;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002163
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002164 if (objectsSize) {
Nick Kraleviche9881a32015-04-28 16:21:30 -07002165 objects = (binder_size_t*)calloc(objectsSize, sizeof(binder_size_t));
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002166 if (!objects) {
Hyejin Kim3f727c02013-03-09 11:28:54 +09002167 free(data);
2168
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002169 mError = NO_MEMORY;
2170 return NO_MEMORY;
2171 }
2172
2173 // Little hack to only acquire references on objects
2174 // we will be keeping.
2175 size_t oldObjectsSize = mObjectsSize;
2176 mObjectsSize = objectsSize;
2177 acquireObjects();
2178 mObjectsSize = oldObjectsSize;
2179 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002180
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002181 if (mData) {
2182 memcpy(data, mData, mDataSize < desired ? mDataSize : desired);
2183 }
2184 if (objects && mObjects) {
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002185 memcpy(objects, mObjects, objectsSize*sizeof(binder_size_t));
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002186 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002187 //ALOGI("Freeing data ref of %p (pid=%d)", this, getpid());
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002188 mOwner(this, mData, mDataSize, mObjects, mObjectsSize, mOwnerCookie);
2189 mOwner = NULL;
2190
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002191 LOG_ALLOC("Parcel %p: taking ownership of %zu capacity", this, desired);
Dianne Hackborna4cff882014-11-13 17:07:40 -08002192 pthread_mutex_lock(&gParcelGlobalAllocSizeLock);
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002193 gParcelGlobalAllocSize += desired;
2194 gParcelGlobalAllocCount++;
Dianne Hackborna4cff882014-11-13 17:07:40 -08002195 pthread_mutex_unlock(&gParcelGlobalAllocSizeLock);
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002196
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002197 mData = data;
2198 mObjects = objects;
2199 mDataSize = (mDataSize < desired) ? mDataSize : desired;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002200 ALOGV("continueWrite Setting data size of %p to %zu", this, mDataSize);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002201 mDataCapacity = desired;
2202 mObjectsSize = mObjectsCapacity = objectsSize;
2203 mNextObjectHint = 0;
2204
2205 } else if (mData) {
2206 if (objectsSize < mObjectsSize) {
2207 // Need to release refs on any objects we are dropping.
2208 const sp<ProcessState> proc(ProcessState::self());
2209 for (size_t i=objectsSize; i<mObjectsSize; i++) {
2210 const flat_binder_object* flat
2211 = reinterpret_cast<flat_binder_object*>(mData+mObjects[i]);
2212 if (flat->type == BINDER_TYPE_FD) {
2213 // will need to rescan because we may have lopped off the only FDs
2214 mFdsKnown = false;
2215 }
2216 release_object(proc, *flat, this);
2217 }
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002218 binder_size_t* objects =
2219 (binder_size_t*)realloc(mObjects, objectsSize*sizeof(binder_size_t));
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002220 if (objects) {
2221 mObjects = objects;
2222 }
2223 mObjectsSize = objectsSize;
2224 mNextObjectHint = 0;
2225 }
2226
2227 // We own the data, so we can just do a realloc().
2228 if (desired > mDataCapacity) {
2229 uint8_t* data = (uint8_t*)realloc(mData, desired);
2230 if (data) {
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002231 LOG_ALLOC("Parcel %p: continue from %zu to %zu capacity", this, mDataCapacity,
2232 desired);
Dianne Hackborna4cff882014-11-13 17:07:40 -08002233 pthread_mutex_lock(&gParcelGlobalAllocSizeLock);
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002234 gParcelGlobalAllocSize += desired;
2235 gParcelGlobalAllocSize -= mDataCapacity;
Dan Austin48fd7b42015-09-10 13:46:02 -07002236 gParcelGlobalAllocCount++;
Dianne Hackborna4cff882014-11-13 17:07:40 -08002237 pthread_mutex_unlock(&gParcelGlobalAllocSizeLock);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002238 mData = data;
2239 mDataCapacity = desired;
2240 } else if (desired > mDataCapacity) {
2241 mError = NO_MEMORY;
2242 return NO_MEMORY;
2243 }
2244 } else {
Dianne Hackborn97e2bcd2011-04-13 18:15:56 -07002245 if (mDataSize > desired) {
2246 mDataSize = desired;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002247 ALOGV("continueWrite Setting data size of %p to %zu", this, mDataSize);
Dianne Hackborn97e2bcd2011-04-13 18:15:56 -07002248 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002249 if (mDataPos > desired) {
2250 mDataPos = desired;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002251 ALOGV("continueWrite Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002252 }
2253 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002254
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002255 } else {
2256 // This is the first data. Easy!
2257 uint8_t* data = (uint8_t*)malloc(desired);
2258 if (!data) {
2259 mError = NO_MEMORY;
2260 return NO_MEMORY;
2261 }
Hyejin Kim3f727c02013-03-09 11:28:54 +09002262
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002263 if(!(mDataCapacity == 0 && mObjects == NULL
2264 && mObjectsCapacity == 0)) {
Colin Cross6f4f3ab2014-02-05 17:42:44 -08002265 ALOGE("continueWrite: %zu/%p/%zu/%zu", mDataCapacity, mObjects, mObjectsCapacity, desired);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002266 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002267
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002268 LOG_ALLOC("Parcel %p: allocating with %zu capacity", this, desired);
Dianne Hackborna4cff882014-11-13 17:07:40 -08002269 pthread_mutex_lock(&gParcelGlobalAllocSizeLock);
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002270 gParcelGlobalAllocSize += desired;
2271 gParcelGlobalAllocCount++;
Dianne Hackborna4cff882014-11-13 17:07:40 -08002272 pthread_mutex_unlock(&gParcelGlobalAllocSizeLock);
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002273
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002274 mData = data;
2275 mDataSize = mDataPos = 0;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002276 ALOGV("continueWrite Setting data size of %p to %zu", this, mDataSize);
2277 ALOGV("continueWrite Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002278 mDataCapacity = desired;
2279 }
2280
2281 return NO_ERROR;
2282}
2283
2284void Parcel::initState()
2285{
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002286 LOG_ALLOC("Parcel %p: initState", this);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002287 mError = NO_ERROR;
2288 mData = 0;
2289 mDataSize = 0;
2290 mDataCapacity = 0;
2291 mDataPos = 0;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002292 ALOGV("initState Setting data size of %p to %zu", this, mDataSize);
2293 ALOGV("initState Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002294 mObjects = NULL;
2295 mObjectsSize = 0;
2296 mObjectsCapacity = 0;
2297 mNextObjectHint = 0;
2298 mHasFds = false;
2299 mFdsKnown = true;
Dianne Hackborn8938ed22011-09-28 23:19:47 -04002300 mAllowFds = true;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002301 mOwner = NULL;
Dan Sandleraa5c2342015-04-10 10:08:45 -04002302 mBlobAshmemSize = 0;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002303}
2304
2305void Parcel::scanForFds() const
2306{
2307 bool hasFds = false;
2308 for (size_t i=0; i<mObjectsSize; i++) {
2309 const flat_binder_object* flat
2310 = reinterpret_cast<const flat_binder_object*>(mData + mObjects[i]);
2311 if (flat->type == BINDER_TYPE_FD) {
2312 hasFds = true;
2313 break;
2314 }
2315 }
2316 mHasFds = hasFds;
2317 mFdsKnown = true;
2318}
2319
Dan Sandleraa5c2342015-04-10 10:08:45 -04002320size_t Parcel::getBlobAshmemSize() const
2321{
2322 return mBlobAshmemSize;
2323}
2324
Jeff Brown5707dbf2011-09-23 21:17:56 -07002325// --- Parcel::Blob ---
2326
2327Parcel::Blob::Blob() :
Jeff Brown13b16042014-11-11 16:44:25 -08002328 mFd(-1), mData(NULL), mSize(0), mMutable(false) {
Jeff Brown5707dbf2011-09-23 21:17:56 -07002329}
2330
2331Parcel::Blob::~Blob() {
2332 release();
2333}
2334
2335void Parcel::Blob::release() {
Jeff Brown13b16042014-11-11 16:44:25 -08002336 if (mFd != -1 && mData) {
Jeff Brown5707dbf2011-09-23 21:17:56 -07002337 ::munmap(mData, mSize);
2338 }
2339 clear();
2340}
2341
Jeff Brown13b16042014-11-11 16:44:25 -08002342void Parcel::Blob::init(int fd, void* data, size_t size, bool isMutable) {
2343 mFd = fd;
Jeff Brown5707dbf2011-09-23 21:17:56 -07002344 mData = data;
2345 mSize = size;
Jeff Brown13b16042014-11-11 16:44:25 -08002346 mMutable = isMutable;
Jeff Brown5707dbf2011-09-23 21:17:56 -07002347}
2348
2349void Parcel::Blob::clear() {
Jeff Brown13b16042014-11-11 16:44:25 -08002350 mFd = -1;
Jeff Brown5707dbf2011-09-23 21:17:56 -07002351 mData = NULL;
2352 mSize = 0;
Jeff Brown13b16042014-11-11 16:44:25 -08002353 mMutable = false;
Jeff Brown5707dbf2011-09-23 21:17:56 -07002354}
2355
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002356}; // namespace android