blob: c808948e0f7941dd93f199518b914fb4d81ee858 [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 Dahlin5f062562015-11-13 13:46:29 -0800753namespace {
754
755template<typename T, typename U>
756status_t unsafeWriteTypedVector(const std::vector<T>& val, Parcel* p,
757 status_t(Parcel::*write_func)(U)) {
758 if (val.size() > std::numeric_limits<int32_t>::max()) {
759 return BAD_VALUE;
760 }
761
762 status_t status = p->writeInt32(val.size());
763
764 if (status != OK) {
765 return status;
766 }
767
768 for (const auto& item : val) {
769 status = (p->*write_func)(item);
770
771 if (status != OK) {
772 return status;
773 }
774 }
775
776 return OK;
777}
778
779template<typename T>
780status_t writeTypedVector(const std::vector<T>& val, Parcel* p,
781 status_t(Parcel::*write_func)(const T&)) {
782 return unsafeWriteTypedVector(val, p, write_func);
783}
784
785template<typename T>
786status_t writeTypedVector(const std::vector<T>& val, Parcel* p,
787 status_t(Parcel::*write_func)(T)) {
788 return unsafeWriteTypedVector(val, p, write_func);
789}
790
791} // namespace
792
Casey Dahlin451ff582015-10-19 18:12:18 -0700793status_t Parcel::writeByteVector(const std::vector<int8_t>& val)
794{
Christopher Wileyf0fc52b2015-10-31 13:22:15 -0700795 status_t status;
Casey Dahlin451ff582015-10-19 18:12:18 -0700796 if (val.size() > std::numeric_limits<int32_t>::max()) {
Christopher Wileyf0fc52b2015-10-31 13:22:15 -0700797 status = BAD_VALUE;
798 return status;
Casey Dahlin451ff582015-10-19 18:12:18 -0700799 }
800
Christopher Wileyf0fc52b2015-10-31 13:22:15 -0700801 status = writeInt32(val.size());
Casey Dahlin451ff582015-10-19 18:12:18 -0700802 if (status != OK) {
803 return status;
804 }
805
Christopher Wileyf0fc52b2015-10-31 13:22:15 -0700806 void* data = writeInplace(val.size());
807 if (!data) {
808 status = BAD_VALUE;
809 return status;
Casey Dahlin451ff582015-10-19 18:12:18 -0700810 }
811
Christopher Wileyf0fc52b2015-10-31 13:22:15 -0700812 memcpy(data, val.data(), val.size());
813 return status;
Casey Dahlin451ff582015-10-19 18:12:18 -0700814}
815
816status_t Parcel::writeInt32Vector(const std::vector<int32_t>& val)
817{
Casey Dahlin5f062562015-11-13 13:46:29 -0800818 return writeTypedVector(val, this, &Parcel::writeInt32);
Casey Dahlin451ff582015-10-19 18:12:18 -0700819}
820
821status_t Parcel::writeInt64Vector(const std::vector<int64_t>& val)
822{
Casey Dahlin5f062562015-11-13 13:46:29 -0800823 return writeTypedVector(val, this, &Parcel::writeInt64);
Casey Dahlin451ff582015-10-19 18:12:18 -0700824}
825
826status_t Parcel::writeFloatVector(const std::vector<float>& val)
827{
Casey Dahlin5f062562015-11-13 13:46:29 -0800828 return writeTypedVector(val, this, &Parcel::writeFloat);
Casey Dahlin451ff582015-10-19 18:12:18 -0700829}
830
831status_t Parcel::writeDoubleVector(const std::vector<double>& val)
832{
Casey Dahlin5f062562015-11-13 13:46:29 -0800833 return writeTypedVector(val, this, &Parcel::writeDouble);
Casey Dahlin451ff582015-10-19 18:12:18 -0700834}
835
836status_t Parcel::writeBoolVector(const std::vector<bool>& val)
837{
Casey Dahlin5f062562015-11-13 13:46:29 -0800838 return writeTypedVector(val, this, &Parcel::writeBool);
Casey Dahlin451ff582015-10-19 18:12:18 -0700839}
840
841status_t Parcel::writeCharVector(const std::vector<char16_t>& val)
842{
Casey Dahlin5f062562015-11-13 13:46:29 -0800843 return writeTypedVector(val, this, &Parcel::writeChar);
Casey Dahlin451ff582015-10-19 18:12:18 -0700844}
845
846status_t Parcel::writeString16Vector(const std::vector<String16>& val)
847{
Casey Dahlin5f062562015-11-13 13:46:29 -0800848 return writeTypedVector(val, this, &Parcel::writeString16);
Casey Dahlin451ff582015-10-19 18:12:18 -0700849}
850
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700851status_t Parcel::writeInt32(int32_t val)
852{
Andreas Huber84a6d042009-08-17 13:33:27 -0700853 return writeAligned(val);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700854}
Dan Stoza41a0f2f2014-12-01 10:01:10 -0800855
856status_t Parcel::writeUint32(uint32_t val)
857{
858 return writeAligned(val);
859}
860
Marco Nelissen5c0106e2013-10-16 10:57:51 -0700861status_t Parcel::writeInt32Array(size_t len, const int32_t *val) {
Nick Kralevichb6b14232015-04-02 09:36:02 -0700862 if (len > INT32_MAX) {
863 // don't accept size_t values which may have come from an
864 // inadvertent conversion from a negative int.
865 return BAD_VALUE;
866 }
867
Marco Nelissen5c0106e2013-10-16 10:57:51 -0700868 if (!val) {
Chad Brubakere59cb432015-06-30 14:03:55 -0700869 return writeInt32(-1);
Marco Nelissen5c0106e2013-10-16 10:57:51 -0700870 }
Chad Brubakere59cb432015-06-30 14:03:55 -0700871 status_t ret = writeInt32(static_cast<uint32_t>(len));
Marco Nelissen5c0106e2013-10-16 10:57:51 -0700872 if (ret == NO_ERROR) {
873 ret = write(val, len * sizeof(*val));
874 }
875 return ret;
876}
Marco Nelissenf0190bf2014-03-13 14:17:40 -0700877status_t Parcel::writeByteArray(size_t len, const uint8_t *val) {
Nick Kralevichb6b14232015-04-02 09:36:02 -0700878 if (len > INT32_MAX) {
879 // don't accept size_t values which may have come from an
880 // inadvertent conversion from a negative int.
881 return BAD_VALUE;
882 }
883
Marco Nelissenf0190bf2014-03-13 14:17:40 -0700884 if (!val) {
Chad Brubakere59cb432015-06-30 14:03:55 -0700885 return writeInt32(-1);
Marco Nelissenf0190bf2014-03-13 14:17:40 -0700886 }
Chad Brubakere59cb432015-06-30 14:03:55 -0700887 status_t ret = writeInt32(static_cast<uint32_t>(len));
Marco Nelissenf0190bf2014-03-13 14:17:40 -0700888 if (ret == NO_ERROR) {
889 ret = write(val, len * sizeof(*val));
890 }
891 return ret;
892}
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700893
Casey Dahlind6848f52015-10-15 15:44:59 -0700894status_t Parcel::writeBool(bool val)
895{
896 return writeInt32(int32_t(val));
897}
898
899status_t Parcel::writeChar(char16_t val)
900{
901 return writeInt32(int32_t(val));
902}
903
904status_t Parcel::writeByte(int8_t val)
905{
906 return writeInt32(int32_t(val));
907}
908
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700909status_t Parcel::writeInt64(int64_t val)
910{
Andreas Huber84a6d042009-08-17 13:33:27 -0700911 return writeAligned(val);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700912}
913
Ronghua Wu2d13afd2015-03-16 11:11:07 -0700914status_t Parcel::writeUint64(uint64_t val)
915{
916 return writeAligned(val);
917}
918
Serban Constantinescuf683e012013-11-05 16:53:55 +0000919status_t Parcel::writePointer(uintptr_t val)
920{
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800921 return writeAligned<binder_uintptr_t>(val);
Serban Constantinescuf683e012013-11-05 16:53:55 +0000922}
923
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700924status_t Parcel::writeFloat(float val)
925{
Andreas Huber84a6d042009-08-17 13:33:27 -0700926 return writeAligned(val);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700927}
928
Douglas Leungcc1a4bb2013-01-11 15:00:55 -0800929#if defined(__mips__) && defined(__mips_hard_float)
930
931status_t Parcel::writeDouble(double val)
932{
933 union {
934 double d;
935 unsigned long long ll;
936 } u;
937 u.d = val;
938 return writeAligned(u.ll);
939}
940
941#else
942
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700943status_t Parcel::writeDouble(double val)
944{
Andreas Huber84a6d042009-08-17 13:33:27 -0700945 return writeAligned(val);
946}
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700947
Douglas Leungcc1a4bb2013-01-11 15:00:55 -0800948#endif
949
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700950status_t Parcel::writeCString(const char* str)
951{
952 return write(str, strlen(str)+1);
953}
954
955status_t Parcel::writeString8(const String8& str)
956{
957 status_t err = writeInt32(str.bytes());
Pravat Dalbeherad1dff8d2010-12-15 08:40:00 +0100958 // only write string if its length is more than zero characters,
959 // as readString8 will only read if the length field is non-zero.
960 // this is slightly different from how writeString16 works.
961 if (str.bytes() > 0 && err == NO_ERROR) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700962 err = write(str.string(), str.bytes()+1);
963 }
964 return err;
965}
966
967status_t Parcel::writeString16(const String16& str)
968{
969 return writeString16(str.string(), str.size());
970}
971
972status_t Parcel::writeString16(const char16_t* str, size_t len)
973{
974 if (str == NULL) return writeInt32(-1);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700975
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700976 status_t err = writeInt32(len);
977 if (err == NO_ERROR) {
978 len *= sizeof(char16_t);
979 uint8_t* data = (uint8_t*)writeInplace(len+sizeof(char16_t));
980 if (data) {
981 memcpy(data, str, len);
982 *reinterpret_cast<char16_t*>(data+len) = 0;
983 return NO_ERROR;
984 }
985 err = mError;
986 }
987 return err;
988}
989
990status_t Parcel::writeStrongBinder(const sp<IBinder>& val)
991{
992 return flatten_binder(ProcessState::self(), val, this);
993}
994
Casey Dahlineb8e15f2015-11-03 13:50:37 -0800995status_t Parcel::writeStrongBinderVector(const std::vector<sp<IBinder>>& val)
996{
Casey Dahlin5f062562015-11-13 13:46:29 -0800997 return writeTypedVector(val, this, &Parcel::writeStrongBinder);
Casey Dahlineb8e15f2015-11-03 13:50:37 -0800998}
999
1000status_t Parcel::readStrongBinderVector(std::vector<sp<IBinder>>* val) const {
Christopher Wiley4db672d2015-11-10 09:44:30 -08001001 return readTypedVector(val, this, &Parcel::readStrongBinder);
Casey Dahlineb8e15f2015-11-03 13:50:37 -08001002}
1003
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001004status_t Parcel::writeWeakBinder(const wp<IBinder>& val)
1005{
1006 return flatten_binder(ProcessState::self(), val, this);
1007}
1008
Mathias Agopiana47f02a2009-05-21 16:29:38 -07001009status_t Parcel::writeNativeHandle(const native_handle* handle)
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001010{
Mathias Agopian1d0a95b2009-07-31 16:12:13 -07001011 if (!handle || handle->version != sizeof(native_handle))
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001012 return BAD_TYPE;
1013
1014 status_t err;
Mathias Agopiana47f02a2009-05-21 16:29:38 -07001015 err = writeInt32(handle->numFds);
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001016 if (err != NO_ERROR) return err;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001017
Mathias Agopiana47f02a2009-05-21 16:29:38 -07001018 err = writeInt32(handle->numInts);
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001019 if (err != NO_ERROR) return err;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001020
Mathias Agopiana47f02a2009-05-21 16:29:38 -07001021 for (int i=0 ; err==NO_ERROR && i<handle->numFds ; i++)
1022 err = writeDupFileDescriptor(handle->data[i]);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001023
1024 if (err != NO_ERROR) {
Steve Block9d453682011-12-20 16:23:08 +00001025 ALOGD("write native handle, write dup fd failed");
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001026 return err;
1027 }
Mathias Agopiana47f02a2009-05-21 16:29:38 -07001028 err = write(handle->data + handle->numFds, sizeof(int)*handle->numInts);
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001029 return err;
1030}
1031
Jeff Brown93ff1f92011-11-04 19:01:44 -07001032status_t Parcel::writeFileDescriptor(int fd, bool takeOwnership)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001033{
1034 flat_binder_object obj;
1035 obj.type = BINDER_TYPE_FD;
1036 obj.flags = 0x7f | FLAT_BINDER_FLAG_ACCEPTS_FDS;
Arve Hjønnevåg07fd0f12014-02-18 21:10:29 -08001037 obj.binder = 0; /* Don't pass uninitialized stack data to a remote process */
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001038 obj.handle = fd;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001039 obj.cookie = takeOwnership ? 1 : 0;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001040 return writeObject(obj, true);
1041}
1042
1043status_t Parcel::writeDupFileDescriptor(int fd)
1044{
Jeff Brownd341c712011-11-04 20:19:33 -07001045 int dupFd = dup(fd);
1046 if (dupFd < 0) {
1047 return -errno;
1048 }
1049 status_t err = writeFileDescriptor(dupFd, true /*takeOwnership*/);
1050 if (err) {
1051 close(dupFd);
1052 }
1053 return err;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001054}
1055
Jeff Brown13b16042014-11-11 16:44:25 -08001056status_t Parcel::writeBlob(size_t len, bool mutableCopy, WritableBlob* outBlob)
Jeff Brown5707dbf2011-09-23 21:17:56 -07001057{
Nick Kralevichb6b14232015-04-02 09:36:02 -07001058 if (len > INT32_MAX) {
1059 // don't accept size_t values which may have come from an
1060 // inadvertent conversion from a negative int.
1061 return BAD_VALUE;
1062 }
1063
Jeff Brown13b16042014-11-11 16:44:25 -08001064 status_t status;
1065 if (!mAllowFds || len <= BLOB_INPLACE_LIMIT) {
Steve Block6807e592011-10-20 11:56:00 +01001066 ALOGV("writeBlob: write in place");
Jeff Brown13b16042014-11-11 16:44:25 -08001067 status = writeInt32(BLOB_INPLACE);
Jeff Brown5707dbf2011-09-23 21:17:56 -07001068 if (status) return status;
1069
1070 void* ptr = writeInplace(len);
1071 if (!ptr) return NO_MEMORY;
1072
Jeff Brown13b16042014-11-11 16:44:25 -08001073 outBlob->init(-1, ptr, len, false);
Jeff Brown5707dbf2011-09-23 21:17:56 -07001074 return NO_ERROR;
1075 }
1076
Steve Block6807e592011-10-20 11:56:00 +01001077 ALOGV("writeBlob: write to ashmem");
Jeff Brown5707dbf2011-09-23 21:17:56 -07001078 int fd = ashmem_create_region("Parcel Blob", len);
1079 if (fd < 0) return NO_MEMORY;
1080
Dan Sandleraa5c2342015-04-10 10:08:45 -04001081 mBlobAshmemSize += len;
1082
Jeff Brown5707dbf2011-09-23 21:17:56 -07001083 int result = ashmem_set_prot_region(fd, PROT_READ | PROT_WRITE);
1084 if (result < 0) {
Jeff Brownec4e0062011-10-10 14:50:10 -07001085 status = result;
Jeff Brown5707dbf2011-09-23 21:17:56 -07001086 } else {
1087 void* ptr = ::mmap(NULL, len, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
1088 if (ptr == MAP_FAILED) {
1089 status = -errno;
1090 } else {
Jeff Brown13b16042014-11-11 16:44:25 -08001091 if (!mutableCopy) {
1092 result = ashmem_set_prot_region(fd, PROT_READ);
1093 }
Jeff Brown5707dbf2011-09-23 21:17:56 -07001094 if (result < 0) {
Jeff Brownec4e0062011-10-10 14:50:10 -07001095 status = result;
Jeff Brown5707dbf2011-09-23 21:17:56 -07001096 } else {
Jeff Brown13b16042014-11-11 16:44:25 -08001097 status = writeInt32(mutableCopy ? BLOB_ASHMEM_MUTABLE : BLOB_ASHMEM_IMMUTABLE);
Jeff Brown5707dbf2011-09-23 21:17:56 -07001098 if (!status) {
Jeff Brown93ff1f92011-11-04 19:01:44 -07001099 status = writeFileDescriptor(fd, true /*takeOwnership*/);
Jeff Brown5707dbf2011-09-23 21:17:56 -07001100 if (!status) {
Jeff Brown13b16042014-11-11 16:44:25 -08001101 outBlob->init(fd, ptr, len, mutableCopy);
Jeff Brown5707dbf2011-09-23 21:17:56 -07001102 return NO_ERROR;
1103 }
1104 }
1105 }
1106 }
1107 ::munmap(ptr, len);
1108 }
1109 ::close(fd);
1110 return status;
1111}
1112
Jeff Brown13b16042014-11-11 16:44:25 -08001113status_t Parcel::writeDupImmutableBlobFileDescriptor(int fd)
1114{
1115 // Must match up with what's done in writeBlob.
1116 if (!mAllowFds) return FDS_NOT_ALLOWED;
1117 status_t status = writeInt32(BLOB_ASHMEM_IMMUTABLE);
1118 if (status) return status;
1119 return writeDupFileDescriptor(fd);
1120}
1121
Mathias Agopiane1424282013-07-29 21:24:40 -07001122status_t Parcel::write(const FlattenableHelperInterface& val)
Mathias Agopian98e71dd2010-02-11 17:30:52 -08001123{
1124 status_t err;
1125
1126 // size if needed
Mathias Agopiane1424282013-07-29 21:24:40 -07001127 const size_t len = val.getFlattenedSize();
1128 const size_t fd_count = val.getFdCount();
Mathias Agopian98e71dd2010-02-11 17:30:52 -08001129
Nick Kralevichb6b14232015-04-02 09:36:02 -07001130 if ((len > INT32_MAX) || (fd_count > INT32_MAX)) {
1131 // don't accept size_t values which may have come from an
1132 // inadvertent conversion from a negative int.
1133 return BAD_VALUE;
1134 }
1135
Mathias Agopian98e71dd2010-02-11 17:30:52 -08001136 err = this->writeInt32(len);
1137 if (err) return err;
1138
1139 err = this->writeInt32(fd_count);
1140 if (err) return err;
1141
1142 // payload
Nick Kralevichb6b14232015-04-02 09:36:02 -07001143 void* const buf = this->writeInplace(pad_size(len));
Mathias Agopian98e71dd2010-02-11 17:30:52 -08001144 if (buf == NULL)
1145 return BAD_VALUE;
1146
1147 int* fds = NULL;
1148 if (fd_count) {
1149 fds = new int[fd_count];
1150 }
1151
1152 err = val.flatten(buf, len, fds, fd_count);
1153 for (size_t i=0 ; i<fd_count && err==NO_ERROR ; i++) {
1154 err = this->writeDupFileDescriptor( fds[i] );
1155 }
1156
1157 if (fd_count) {
1158 delete [] fds;
1159 }
1160
1161 return err;
1162}
1163
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001164status_t Parcel::writeObject(const flat_binder_object& val, bool nullMetaData)
1165{
1166 const bool enoughData = (mDataPos+sizeof(val)) <= mDataCapacity;
1167 const bool enoughObjects = mObjectsSize < mObjectsCapacity;
1168 if (enoughData && enoughObjects) {
1169restart_write:
1170 *reinterpret_cast<flat_binder_object*>(mData+mDataPos) = val;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001171
Christopher Tate98e67d32015-06-03 18:44:15 -07001172 // remember if it's a file descriptor
1173 if (val.type == BINDER_TYPE_FD) {
1174 if (!mAllowFds) {
1175 // fail before modifying our object index
1176 return FDS_NOT_ALLOWED;
1177 }
1178 mHasFds = mFdsKnown = true;
1179 }
1180
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001181 // Need to write meta-data?
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001182 if (nullMetaData || val.binder != 0) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001183 mObjects[mObjectsSize] = mDataPos;
1184 acquire_object(ProcessState::self(), val, this);
1185 mObjectsSize++;
1186 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001187
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001188 return finishWrite(sizeof(flat_binder_object));
1189 }
1190
1191 if (!enoughData) {
1192 const status_t err = growData(sizeof(val));
1193 if (err != NO_ERROR) return err;
1194 }
1195 if (!enoughObjects) {
1196 size_t newSize = ((mObjectsSize+2)*3)/2;
Christopher Tateed7a50c2015-06-08 14:45:14 -07001197 if (newSize < mObjectsSize) return NO_MEMORY; // overflow
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001198 binder_size_t* objects = (binder_size_t*)realloc(mObjects, newSize*sizeof(binder_size_t));
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001199 if (objects == NULL) return NO_MEMORY;
1200 mObjects = objects;
1201 mObjectsCapacity = newSize;
1202 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001203
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001204 goto restart_write;
1205}
1206
Brad Fitzpatrick837a0d02010-07-13 15:33:35 -07001207status_t Parcel::writeNoException()
1208{
1209 return writeInt32(0);
1210}
1211
Colin Cross6f4f3ab2014-02-05 17:42:44 -08001212void Parcel::remove(size_t /*start*/, size_t /*amt*/)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001213{
1214 LOG_ALWAYS_FATAL("Parcel::remove() not yet implemented!");
1215}
1216
1217status_t Parcel::read(void* outData, size_t len) const
1218{
Nick Kralevichb6b14232015-04-02 09:36:02 -07001219 if (len > INT32_MAX) {
1220 // don't accept size_t values which may have come from an
1221 // inadvertent conversion from a negative int.
1222 return BAD_VALUE;
1223 }
1224
1225 if ((mDataPos+pad_size(len)) >= mDataPos && (mDataPos+pad_size(len)) <= mDataSize
1226 && len <= pad_size(len)) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001227 memcpy(outData, mData+mDataPos, len);
Nick Kralevichb6b14232015-04-02 09:36:02 -07001228 mDataPos += pad_size(len);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001229 ALOGV("read Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001230 return NO_ERROR;
1231 }
1232 return NOT_ENOUGH_DATA;
1233}
1234
1235const void* Parcel::readInplace(size_t len) const
1236{
Nick Kralevichb6b14232015-04-02 09:36:02 -07001237 if (len > INT32_MAX) {
1238 // don't accept size_t values which may have come from an
1239 // inadvertent conversion from a negative int.
1240 return NULL;
1241 }
1242
1243 if ((mDataPos+pad_size(len)) >= mDataPos && (mDataPos+pad_size(len)) <= mDataSize
1244 && len <= pad_size(len)) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001245 const void* data = mData+mDataPos;
Nick Kralevichb6b14232015-04-02 09:36:02 -07001246 mDataPos += pad_size(len);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001247 ALOGV("readInplace Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001248 return data;
1249 }
1250 return NULL;
1251}
1252
Andreas Huber84a6d042009-08-17 13:33:27 -07001253template<class T>
1254status_t Parcel::readAligned(T *pArg) const {
Nick Kralevichb6b14232015-04-02 09:36:02 -07001255 COMPILE_TIME_ASSERT_FUNCTION_SCOPE(PAD_SIZE_UNSAFE(sizeof(T)) == sizeof(T));
Andreas Huber84a6d042009-08-17 13:33:27 -07001256
1257 if ((mDataPos+sizeof(T)) <= mDataSize) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001258 const void* data = mData+mDataPos;
Andreas Huber84a6d042009-08-17 13:33:27 -07001259 mDataPos += sizeof(T);
1260 *pArg = *reinterpret_cast<const T*>(data);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001261 return NO_ERROR;
1262 } else {
1263 return NOT_ENOUGH_DATA;
1264 }
1265}
1266
Andreas Huber84a6d042009-08-17 13:33:27 -07001267template<class T>
1268T Parcel::readAligned() const {
1269 T result;
1270 if (readAligned(&result) != NO_ERROR) {
1271 result = 0;
1272 }
1273
1274 return result;
1275}
1276
1277template<class T>
1278status_t Parcel::writeAligned(T val) {
Nick Kralevichb6b14232015-04-02 09:36:02 -07001279 COMPILE_TIME_ASSERT_FUNCTION_SCOPE(PAD_SIZE_UNSAFE(sizeof(T)) == sizeof(T));
Andreas Huber84a6d042009-08-17 13:33:27 -07001280
1281 if ((mDataPos+sizeof(val)) <= mDataCapacity) {
1282restart_write:
1283 *reinterpret_cast<T*>(mData+mDataPos) = val;
1284 return finishWrite(sizeof(val));
1285 }
1286
1287 status_t err = growData(sizeof(val));
1288 if (err == NO_ERROR) goto restart_write;
1289 return err;
1290}
1291
Casey Dahlin451ff582015-10-19 18:12:18 -07001292status_t Parcel::readByteVector(std::vector<int8_t>* val) const {
1293 val->clear();
1294
1295 int32_t size;
1296 status_t status = readInt32(&size);
1297
1298 if (status != OK) {
1299 return status;
1300 }
1301
Christopher Wiley4db672d2015-11-10 09:44:30 -08001302 if (size < 0) {
1303 status = UNEXPECTED_NULL;
1304 return status;
1305 }
1306 if (size_t(size) > dataAvail()) {
Christopher Wileyf0fc52b2015-10-31 13:22:15 -07001307 status = BAD_VALUE;
1308 return status;
Casey Dahlin451ff582015-10-19 18:12:18 -07001309 }
Christopher Wiley4db672d2015-11-10 09:44:30 -08001310
Christopher Wileyf0fc52b2015-10-31 13:22:15 -07001311 const void* data = readInplace(size);
1312 if (!data) {
1313 status = BAD_VALUE;
1314 return status;
1315 }
Casey Dahlin451ff582015-10-19 18:12:18 -07001316 val->resize(size);
Christopher Wileyf0fc52b2015-10-31 13:22:15 -07001317 memcpy(val->data(), data, size);
Casey Dahlin451ff582015-10-19 18:12:18 -07001318
Christopher Wileyf0fc52b2015-10-31 13:22:15 -07001319 return status;
Casey Dahlin451ff582015-10-19 18:12:18 -07001320}
1321
1322status_t Parcel::readInt32Vector(std::vector<int32_t>* val) const {
Christopher Wiley4db672d2015-11-10 09:44:30 -08001323 return readTypedVector(val, this, &Parcel::readInt32);
Casey Dahlin451ff582015-10-19 18:12:18 -07001324}
1325
1326status_t Parcel::readInt64Vector(std::vector<int64_t>* val) const {
Christopher Wiley4db672d2015-11-10 09:44:30 -08001327 return readTypedVector(val, this, &Parcel::readInt64);
Casey Dahlin451ff582015-10-19 18:12:18 -07001328}
1329
1330status_t Parcel::readFloatVector(std::vector<float>* val) const {
Christopher Wiley4db672d2015-11-10 09:44:30 -08001331 return readTypedVector(val, this, &Parcel::readFloat);
Casey Dahlin451ff582015-10-19 18:12:18 -07001332}
1333
1334status_t Parcel::readDoubleVector(std::vector<double>* val) const {
Christopher Wiley4db672d2015-11-10 09:44:30 -08001335 return readTypedVector(val, this, &Parcel::readDouble);
Casey Dahlin451ff582015-10-19 18:12:18 -07001336}
1337
1338status_t Parcel::readBoolVector(std::vector<bool>* val) const {
1339 val->clear();
1340
1341 int32_t size;
1342 status_t status = readInt32(&size);
1343
1344 if (status != OK) {
1345 return status;
1346 }
1347
1348 if (size < 0) {
Christopher Wiley4db672d2015-11-10 09:44:30 -08001349 return UNEXPECTED_NULL;
Casey Dahlin451ff582015-10-19 18:12:18 -07001350 }
1351
1352 val->resize(size);
1353
1354 /* C++ bool handling means a vector of bools isn't necessarily addressable
1355 * (we might use individual bits)
1356 */
Christopher Wiley97887982015-10-27 16:33:47 -07001357 bool data;
1358 for (int32_t i = 0; i < size; ++i) {
Casey Dahlin451ff582015-10-19 18:12:18 -07001359 status = readBool(&data);
1360 (*val)[i] = data;
1361
1362 if (status != OK) {
1363 return status;
1364 }
1365 }
1366
1367 return OK;
1368}
1369
1370status_t Parcel::readCharVector(std::vector<char16_t>* val) const {
Christopher Wiley4db672d2015-11-10 09:44:30 -08001371 return readTypedVector(val, this, &Parcel::readChar);
Casey Dahlin451ff582015-10-19 18:12:18 -07001372}
1373
1374status_t Parcel::readString16Vector(std::vector<String16>* val) const {
Christopher Wiley4db672d2015-11-10 09:44:30 -08001375 return readTypedVector(val, this, &Parcel::readString16);
Casey Dahlin451ff582015-10-19 18:12:18 -07001376}
1377
1378
Andreas Huber84a6d042009-08-17 13:33:27 -07001379status_t Parcel::readInt32(int32_t *pArg) const
1380{
1381 return readAligned(pArg);
1382}
1383
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001384int32_t Parcel::readInt32() const
1385{
Andreas Huber84a6d042009-08-17 13:33:27 -07001386 return readAligned<int32_t>();
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001387}
1388
Dan Stoza41a0f2f2014-12-01 10:01:10 -08001389status_t Parcel::readUint32(uint32_t *pArg) const
1390{
1391 return readAligned(pArg);
1392}
1393
1394uint32_t Parcel::readUint32() const
1395{
1396 return readAligned<uint32_t>();
1397}
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001398
1399status_t Parcel::readInt64(int64_t *pArg) const
1400{
Andreas Huber84a6d042009-08-17 13:33:27 -07001401 return readAligned(pArg);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001402}
1403
1404
1405int64_t Parcel::readInt64() const
1406{
Andreas Huber84a6d042009-08-17 13:33:27 -07001407 return readAligned<int64_t>();
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001408}
1409
Ronghua Wu2d13afd2015-03-16 11:11:07 -07001410status_t Parcel::readUint64(uint64_t *pArg) const
1411{
1412 return readAligned(pArg);
1413}
1414
1415uint64_t Parcel::readUint64() const
1416{
1417 return readAligned<uint64_t>();
1418}
1419
Serban Constantinescuf683e012013-11-05 16:53:55 +00001420status_t Parcel::readPointer(uintptr_t *pArg) const
1421{
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001422 status_t ret;
1423 binder_uintptr_t ptr;
1424 ret = readAligned(&ptr);
1425 if (!ret)
1426 *pArg = ptr;
1427 return ret;
Serban Constantinescuf683e012013-11-05 16:53:55 +00001428}
1429
1430uintptr_t Parcel::readPointer() const
1431{
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001432 return readAligned<binder_uintptr_t>();
Serban Constantinescuf683e012013-11-05 16:53:55 +00001433}
1434
1435
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001436status_t Parcel::readFloat(float *pArg) const
1437{
Andreas Huber84a6d042009-08-17 13:33:27 -07001438 return readAligned(pArg);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001439}
1440
1441
1442float Parcel::readFloat() const
1443{
Andreas Huber84a6d042009-08-17 13:33:27 -07001444 return readAligned<float>();
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001445}
1446
Douglas Leungcc1a4bb2013-01-11 15:00:55 -08001447#if defined(__mips__) && defined(__mips_hard_float)
1448
1449status_t Parcel::readDouble(double *pArg) const
1450{
1451 union {
1452 double d;
1453 unsigned long long ll;
1454 } u;
Narayan Kamath2c68d382014-06-04 15:04:29 +01001455 u.d = 0;
Douglas Leungcc1a4bb2013-01-11 15:00:55 -08001456 status_t status;
1457 status = readAligned(&u.ll);
1458 *pArg = u.d;
1459 return status;
1460}
1461
1462double Parcel::readDouble() const
1463{
1464 union {
1465 double d;
1466 unsigned long long ll;
1467 } u;
1468 u.ll = readAligned<unsigned long long>();
1469 return u.d;
1470}
1471
1472#else
1473
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001474status_t Parcel::readDouble(double *pArg) const
1475{
Andreas Huber84a6d042009-08-17 13:33:27 -07001476 return readAligned(pArg);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001477}
1478
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001479double Parcel::readDouble() const
1480{
Andreas Huber84a6d042009-08-17 13:33:27 -07001481 return readAligned<double>();
1482}
1483
Douglas Leungcc1a4bb2013-01-11 15:00:55 -08001484#endif
1485
Andreas Huber84a6d042009-08-17 13:33:27 -07001486status_t Parcel::readIntPtr(intptr_t *pArg) const
1487{
1488 return readAligned(pArg);
1489}
1490
1491
1492intptr_t Parcel::readIntPtr() const
1493{
1494 return readAligned<intptr_t>();
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001495}
1496
Casey Dahlind6848f52015-10-15 15:44:59 -07001497status_t Parcel::readBool(bool *pArg) const
1498{
1499 int32_t tmp;
1500 status_t ret = readInt32(&tmp);
1501 *pArg = (tmp != 0);
1502 return ret;
1503}
1504
1505bool Parcel::readBool() const
1506{
1507 return readInt32() != 0;
1508}
1509
1510status_t Parcel::readChar(char16_t *pArg) const
1511{
1512 int32_t tmp;
1513 status_t ret = readInt32(&tmp);
1514 *pArg = char16_t(tmp);
1515 return ret;
1516}
1517
1518char16_t Parcel::readChar() const
1519{
1520 return char16_t(readInt32());
1521}
1522
1523status_t Parcel::readByte(int8_t *pArg) const
1524{
1525 int32_t tmp;
1526 status_t ret = readInt32(&tmp);
1527 *pArg = int8_t(tmp);
1528 return ret;
1529}
1530
1531int8_t Parcel::readByte() const
1532{
1533 return int8_t(readInt32());
1534}
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001535
1536const char* Parcel::readCString() const
1537{
1538 const size_t avail = mDataSize-mDataPos;
1539 if (avail > 0) {
1540 const char* str = reinterpret_cast<const char*>(mData+mDataPos);
1541 // is the string's trailing NUL within the parcel's valid bounds?
1542 const char* eos = reinterpret_cast<const char*>(memchr(str, 0, avail));
1543 if (eos) {
1544 const size_t len = eos - str;
Nick Kralevichb6b14232015-04-02 09:36:02 -07001545 mDataPos += pad_size(len+1);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001546 ALOGV("readCString Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001547 return str;
1548 }
1549 }
1550 return NULL;
1551}
1552
1553String8 Parcel::readString8() const
1554{
1555 int32_t size = readInt32();
1556 // watch for potential int overflow adding 1 for trailing NUL
1557 if (size > 0 && size < INT32_MAX) {
1558 const char* str = (const char*)readInplace(size+1);
1559 if (str) return String8(str, size);
1560 }
1561 return String8();
1562}
1563
1564String16 Parcel::readString16() const
1565{
1566 size_t len;
1567 const char16_t* str = readString16Inplace(&len);
1568 if (str) return String16(str, len);
Steve Blocke6f43dd2012-01-06 19:20:56 +00001569 ALOGE("Reading a NULL string not supported here.");
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001570 return String16();
1571}
1572
Casey Dahlin451ff582015-10-19 18:12:18 -07001573status_t Parcel::readString16(String16* pArg) const
1574{
1575 size_t len;
1576 const char16_t* str = readString16Inplace(&len);
1577 if (str) {
Casey Dahlin1515ea12015-10-20 16:26:23 -07001578 pArg->setTo(str, len);
Casey Dahlin451ff582015-10-19 18:12:18 -07001579 return 0;
1580 } else {
1581 *pArg = String16();
Christopher Wiley4db672d2015-11-10 09:44:30 -08001582 return UNEXPECTED_NULL;
Casey Dahlin451ff582015-10-19 18:12:18 -07001583 }
1584}
1585
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001586const char16_t* Parcel::readString16Inplace(size_t* outLen) const
1587{
1588 int32_t size = readInt32();
1589 // watch for potential int overflow from size+1
1590 if (size >= 0 && size < INT32_MAX) {
1591 *outLen = size;
1592 const char16_t* str = (const char16_t*)readInplace((size+1)*sizeof(char16_t));
1593 if (str != NULL) {
1594 return str;
1595 }
1596 }
1597 *outLen = 0;
1598 return NULL;
1599}
1600
Casey Dahlinf0c13772015-10-27 18:33:56 -07001601status_t Parcel::readStrongBinder(sp<IBinder>* val) const
1602{
1603 return unflatten_binder(ProcessState::self(), *this, val);
1604}
1605
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001606sp<IBinder> Parcel::readStrongBinder() const
1607{
1608 sp<IBinder> val;
Casey Dahlinf0c13772015-10-27 18:33:56 -07001609 readStrongBinder(&val);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001610 return val;
1611}
1612
1613wp<IBinder> Parcel::readWeakBinder() const
1614{
1615 wp<IBinder> val;
1616 unflatten_binder(ProcessState::self(), *this, &val);
1617 return val;
1618}
1619
Brad Fitzpatrick837a0d02010-07-13 15:33:35 -07001620int32_t Parcel::readExceptionCode() const
1621{
1622 int32_t exception_code = readAligned<int32_t>();
Brad Fitzpatrickd36f4a52010-07-12 11:05:38 -07001623 if (exception_code == EX_HAS_REPLY_HEADER) {
Magnus Strandberg1ba24572011-05-03 15:44:00 +02001624 int32_t header_start = dataPosition();
Brad Fitzpatrickd36f4a52010-07-12 11:05:38 -07001625 int32_t header_size = readAligned<int32_t>();
1626 // Skip over fat responses headers. Not used (or propagated) in
1627 // native code
Magnus Strandberg1ba24572011-05-03 15:44:00 +02001628 setDataPosition(header_start + header_size);
Brad Fitzpatrickd36f4a52010-07-12 11:05:38 -07001629 // And fat response headers are currently only used when there are no
1630 // exceptions, so return no error:
1631 return 0;
1632 }
Brad Fitzpatrick837a0d02010-07-13 15:33:35 -07001633 return exception_code;
1634}
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001635
Mathias Agopiana47f02a2009-05-21 16:29:38 -07001636native_handle* Parcel::readNativeHandle() const
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001637{
1638 int numFds, numInts;
1639 status_t err;
1640 err = readInt32(&numFds);
1641 if (err != NO_ERROR) return 0;
1642 err = readInt32(&numInts);
1643 if (err != NO_ERROR) return 0;
1644
Mathias Agopiana47f02a2009-05-21 16:29:38 -07001645 native_handle* h = native_handle_create(numFds, numInts);
Adam Lesinskieaac99a2015-05-12 17:35:48 -07001646 if (!h) {
1647 return 0;
1648 }
1649
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001650 for (int i=0 ; err==NO_ERROR && i<numFds ; i++) {
Rebecca Schultz Zavin360211f2009-02-13 16:34:38 -08001651 h->data[i] = dup(readFileDescriptor());
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001652 if (h->data[i] < 0) err = BAD_VALUE;
1653 }
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001654 err = read(h->data + numFds, sizeof(int)*numInts);
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001655 if (err != NO_ERROR) {
Mathias Agopiana47f02a2009-05-21 16:29:38 -07001656 native_handle_close(h);
1657 native_handle_delete(h);
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001658 h = 0;
1659 }
1660 return h;
1661}
1662
1663
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001664int Parcel::readFileDescriptor() const
1665{
1666 const flat_binder_object* flat = readObject(true);
1667 if (flat) {
1668 switch (flat->type) {
1669 case BINDER_TYPE_FD:
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001670 //ALOGI("Returning file descriptor %ld from parcel %p", flat->handle, this);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001671 return flat->handle;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001672 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001673 }
1674 return BAD_TYPE;
1675}
1676
Jeff Brown5707dbf2011-09-23 21:17:56 -07001677status_t Parcel::readBlob(size_t len, ReadableBlob* outBlob) const
1678{
Jeff Brown13b16042014-11-11 16:44:25 -08001679 int32_t blobType;
1680 status_t status = readInt32(&blobType);
Jeff Brown5707dbf2011-09-23 21:17:56 -07001681 if (status) return status;
1682
Jeff Brown13b16042014-11-11 16:44:25 -08001683 if (blobType == BLOB_INPLACE) {
Steve Block6807e592011-10-20 11:56:00 +01001684 ALOGV("readBlob: read in place");
Jeff Brown5707dbf2011-09-23 21:17:56 -07001685 const void* ptr = readInplace(len);
1686 if (!ptr) return BAD_VALUE;
1687
Jeff Brown13b16042014-11-11 16:44:25 -08001688 outBlob->init(-1, const_cast<void*>(ptr), len, false);
Jeff Brown5707dbf2011-09-23 21:17:56 -07001689 return NO_ERROR;
1690 }
1691
Steve Block6807e592011-10-20 11:56:00 +01001692 ALOGV("readBlob: read from ashmem");
Jeff Brown13b16042014-11-11 16:44:25 -08001693 bool isMutable = (blobType == BLOB_ASHMEM_MUTABLE);
Jeff Brown5707dbf2011-09-23 21:17:56 -07001694 int fd = readFileDescriptor();
1695 if (fd == int(BAD_TYPE)) return BAD_VALUE;
1696
Jeff Brown13b16042014-11-11 16:44:25 -08001697 void* ptr = ::mmap(NULL, len, isMutable ? PROT_READ | PROT_WRITE : PROT_READ,
1698 MAP_SHARED, fd, 0);
Narayan Kamath9ea09752014-10-08 17:35:45 +01001699 if (ptr == MAP_FAILED) return NO_MEMORY;
Jeff Brown5707dbf2011-09-23 21:17:56 -07001700
Jeff Brown13b16042014-11-11 16:44:25 -08001701 outBlob->init(fd, ptr, len, isMutable);
Jeff Brown5707dbf2011-09-23 21:17:56 -07001702 return NO_ERROR;
1703}
1704
Mathias Agopiane1424282013-07-29 21:24:40 -07001705status_t Parcel::read(FlattenableHelperInterface& val) const
Mathias Agopian98e71dd2010-02-11 17:30:52 -08001706{
1707 // size
1708 const size_t len = this->readInt32();
1709 const size_t fd_count = this->readInt32();
1710
Nick Kralevichb6b14232015-04-02 09:36:02 -07001711 if (len > INT32_MAX) {
1712 // don't accept size_t values which may have come from an
1713 // inadvertent conversion from a negative int.
1714 return BAD_VALUE;
1715 }
1716
Mathias Agopian98e71dd2010-02-11 17:30:52 -08001717 // payload
Nick Kralevichb6b14232015-04-02 09:36:02 -07001718 void const* const buf = this->readInplace(pad_size(len));
Mathias Agopian98e71dd2010-02-11 17:30:52 -08001719 if (buf == NULL)
1720 return BAD_VALUE;
1721
1722 int* fds = NULL;
1723 if (fd_count) {
1724 fds = new int[fd_count];
1725 }
1726
1727 status_t err = NO_ERROR;
1728 for (size_t i=0 ; i<fd_count && err==NO_ERROR ; i++) {
Jesse Hallfee99042014-11-04 08:36:31 -08001729 fds[i] = dup(this->readFileDescriptor());
Jun Jiangabf8a2c2014-04-29 14:22:10 +08001730 if (fds[i] < 0) {
1731 err = BAD_VALUE;
Jesse Hallfee99042014-11-04 08:36:31 -08001732 ALOGE("dup() failed in Parcel::read, i is %zu, fds[i] is %d, fd_count is %zu, error: %s",
1733 i, fds[i], fd_count, strerror(errno));
Jun Jiangabf8a2c2014-04-29 14:22:10 +08001734 }
Mathias Agopian98e71dd2010-02-11 17:30:52 -08001735 }
1736
1737 if (err == NO_ERROR) {
1738 err = val.unflatten(buf, len, fds, fd_count);
1739 }
1740
1741 if (fd_count) {
1742 delete [] fds;
1743 }
1744
1745 return err;
1746}
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001747const flat_binder_object* Parcel::readObject(bool nullMetaData) const
1748{
1749 const size_t DPOS = mDataPos;
1750 if ((DPOS+sizeof(flat_binder_object)) <= mDataSize) {
1751 const flat_binder_object* obj
1752 = reinterpret_cast<const flat_binder_object*>(mData+DPOS);
1753 mDataPos = DPOS + sizeof(flat_binder_object);
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001754 if (!nullMetaData && (obj->cookie == 0 && obj->binder == 0)) {
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001755 // When transferring a NULL object, we don't write it into
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001756 // the object list, so we don't want to check for it when
1757 // reading.
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001758 ALOGV("readObject Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001759 return obj;
1760 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001761
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001762 // Ensure that this object is valid...
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001763 binder_size_t* const OBJS = mObjects;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001764 const size_t N = mObjectsSize;
1765 size_t opos = mNextObjectHint;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001766
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001767 if (N > 0) {
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001768 ALOGV("Parcel %p looking for obj at %zu, hint=%zu",
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001769 this, DPOS, opos);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001770
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001771 // Start at the current hint position, looking for an object at
1772 // the current data position.
1773 if (opos < N) {
1774 while (opos < (N-1) && OBJS[opos] < DPOS) {
1775 opos++;
1776 }
1777 } else {
1778 opos = N-1;
1779 }
1780 if (OBJS[opos] == DPOS) {
1781 // Found it!
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001782 ALOGV("Parcel %p found obj %zu at index %zu with forward search",
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001783 this, DPOS, opos);
1784 mNextObjectHint = opos+1;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001785 ALOGV("readObject Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001786 return obj;
1787 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001788
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001789 // Look backwards for it...
1790 while (opos > 0 && OBJS[opos] > DPOS) {
1791 opos--;
1792 }
1793 if (OBJS[opos] == DPOS) {
1794 // Found it!
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001795 ALOGV("Parcel %p found obj %zu at index %zu with backward search",
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001796 this, DPOS, opos);
1797 mNextObjectHint = opos+1;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001798 ALOGV("readObject Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001799 return obj;
1800 }
1801 }
Colin Cross6f4f3ab2014-02-05 17:42:44 -08001802 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 -07001803 this, DPOS);
1804 }
1805 return NULL;
1806}
1807
1808void Parcel::closeFileDescriptors()
1809{
1810 size_t i = mObjectsSize;
1811 if (i > 0) {
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001812 //ALOGI("Closing file descriptors for %zu objects...", i);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001813 }
1814 while (i > 0) {
1815 i--;
1816 const flat_binder_object* flat
1817 = reinterpret_cast<flat_binder_object*>(mData+mObjects[i]);
1818 if (flat->type == BINDER_TYPE_FD) {
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001819 //ALOGI("Closing fd: %ld", flat->handle);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001820 close(flat->handle);
1821 }
1822 }
1823}
1824
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001825uintptr_t Parcel::ipcData() const
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001826{
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001827 return reinterpret_cast<uintptr_t>(mData);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001828}
1829
1830size_t Parcel::ipcDataSize() const
1831{
1832 return (mDataSize > mDataPos ? mDataSize : mDataPos);
1833}
1834
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001835uintptr_t Parcel::ipcObjects() const
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001836{
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001837 return reinterpret_cast<uintptr_t>(mObjects);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001838}
1839
1840size_t Parcel::ipcObjectsCount() const
1841{
1842 return mObjectsSize;
1843}
1844
1845void Parcel::ipcSetDataReference(const uint8_t* data, size_t dataSize,
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001846 const binder_size_t* objects, size_t objectsCount, release_func relFunc, void* relCookie)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001847{
Arve Hjønnevåg6f286112014-02-19 20:42:13 -08001848 binder_size_t minOffset = 0;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001849 freeDataNoInit();
1850 mError = NO_ERROR;
1851 mData = const_cast<uint8_t*>(data);
1852 mDataSize = mDataCapacity = dataSize;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001853 //ALOGI("setDataReference Setting data size of %p to %lu (pid=%d)", this, mDataSize, getpid());
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001854 mDataPos = 0;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001855 ALOGV("setDataReference Setting data pos of %p to %zu", this, mDataPos);
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001856 mObjects = const_cast<binder_size_t*>(objects);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001857 mObjectsSize = mObjectsCapacity = objectsCount;
1858 mNextObjectHint = 0;
1859 mOwner = relFunc;
1860 mOwnerCookie = relCookie;
Arve Hjønnevågf50b9ea2014-02-13 19:22:08 -08001861 for (size_t i = 0; i < mObjectsSize; i++) {
Arve Hjønnevåg6f286112014-02-19 20:42:13 -08001862 binder_size_t offset = mObjects[i];
Arve Hjønnevågf50b9ea2014-02-13 19:22:08 -08001863 if (offset < minOffset) {
Dan Albert3bdc5b82014-11-20 11:50:23 -08001864 ALOGE("%s: bad object offset %" PRIu64 " < %" PRIu64 "\n",
Arve Hjønnevåg6f286112014-02-19 20:42:13 -08001865 __func__, (uint64_t)offset, (uint64_t)minOffset);
Arve Hjønnevågf50b9ea2014-02-13 19:22:08 -08001866 mObjectsSize = 0;
1867 break;
1868 }
1869 minOffset = offset + sizeof(flat_binder_object);
1870 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001871 scanForFds();
1872}
1873
Colin Cross6f4f3ab2014-02-05 17:42:44 -08001874void Parcel::print(TextOutput& to, uint32_t /*flags*/) const
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001875{
1876 to << "Parcel(";
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001877
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001878 if (errorCheck() != NO_ERROR) {
1879 const status_t err = errorCheck();
Colin Cross6f4f3ab2014-02-05 17:42:44 -08001880 to << "Error: " << (void*)(intptr_t)err << " \"" << strerror(-err) << "\"";
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001881 } else if (dataSize() > 0) {
1882 const uint8_t* DATA = data();
1883 to << indent << HexDump(DATA, dataSize()) << dedent;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001884 const binder_size_t* OBJS = objects();
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001885 const size_t N = objectsCount();
1886 for (size_t i=0; i<N; i++) {
1887 const flat_binder_object* flat
1888 = reinterpret_cast<const flat_binder_object*>(DATA+OBJS[i]);
1889 to << endl << "Object #" << i << " @ " << (void*)OBJS[i] << ": "
1890 << TypeCode(flat->type & 0x7f7f7f00)
1891 << " = " << flat->binder;
1892 }
1893 } else {
1894 to << "NULL";
1895 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001896
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001897 to << ")";
1898}
1899
1900void Parcel::releaseObjects()
1901{
1902 const sp<ProcessState> proc(ProcessState::self());
1903 size_t i = mObjectsSize;
1904 uint8_t* const data = mData;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001905 binder_size_t* const objects = mObjects;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001906 while (i > 0) {
1907 i--;
1908 const flat_binder_object* flat
1909 = reinterpret_cast<flat_binder_object*>(data+objects[i]);
1910 release_object(proc, *flat, this);
1911 }
1912}
1913
1914void Parcel::acquireObjects()
1915{
1916 const sp<ProcessState> proc(ProcessState::self());
1917 size_t i = mObjectsSize;
1918 uint8_t* const data = mData;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001919 binder_size_t* const objects = mObjects;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001920 while (i > 0) {
1921 i--;
1922 const flat_binder_object* flat
1923 = reinterpret_cast<flat_binder_object*>(data+objects[i]);
1924 acquire_object(proc, *flat, this);
1925 }
1926}
1927
1928void Parcel::freeData()
1929{
1930 freeDataNoInit();
1931 initState();
1932}
1933
1934void Parcel::freeDataNoInit()
1935{
1936 if (mOwner) {
Dianne Hackborn7e790af2014-11-11 12:22:53 -08001937 LOG_ALLOC("Parcel %p: freeing other owner data", this);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001938 //ALOGI("Freeing data ref of %p (pid=%d)", this, getpid());
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001939 mOwner(this, mData, mDataSize, mObjects, mObjectsSize, mOwnerCookie);
1940 } else {
Dianne Hackborn7e790af2014-11-11 12:22:53 -08001941 LOG_ALLOC("Parcel %p: freeing allocated data", this);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001942 releaseObjects();
Dianne Hackborn7e790af2014-11-11 12:22:53 -08001943 if (mData) {
1944 LOG_ALLOC("Parcel %p: freeing with %zu capacity", this, mDataCapacity);
Dianne Hackborna4cff882014-11-13 17:07:40 -08001945 pthread_mutex_lock(&gParcelGlobalAllocSizeLock);
Dan Austin48fd7b42015-09-10 13:46:02 -07001946 if (mDataCapacity <= gParcelGlobalAllocSize) {
1947 gParcelGlobalAllocSize = gParcelGlobalAllocSize - mDataCapacity;
1948 } else {
1949 gParcelGlobalAllocSize = 0;
1950 }
1951 if (gParcelGlobalAllocCount > 0) {
1952 gParcelGlobalAllocCount--;
1953 }
Dianne Hackborna4cff882014-11-13 17:07:40 -08001954 pthread_mutex_unlock(&gParcelGlobalAllocSizeLock);
Dianne Hackborn7e790af2014-11-11 12:22:53 -08001955 free(mData);
1956 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001957 if (mObjects) free(mObjects);
1958 }
1959}
1960
1961status_t Parcel::growData(size_t len)
1962{
Nick Kralevichb6b14232015-04-02 09:36:02 -07001963 if (len > INT32_MAX) {
1964 // don't accept size_t values which may have come from an
1965 // inadvertent conversion from a negative int.
1966 return BAD_VALUE;
1967 }
1968
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001969 size_t newSize = ((mDataSize+len)*3)/2;
1970 return (newSize <= mDataSize)
1971 ? (status_t) NO_MEMORY
1972 : continueWrite(newSize);
1973}
1974
1975status_t Parcel::restartWrite(size_t desired)
1976{
Nick Kralevichb6b14232015-04-02 09:36:02 -07001977 if (desired > INT32_MAX) {
1978 // don't accept size_t values which may have come from an
1979 // inadvertent conversion from a negative int.
1980 return BAD_VALUE;
1981 }
1982
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001983 if (mOwner) {
1984 freeData();
1985 return continueWrite(desired);
1986 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001987
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001988 uint8_t* data = (uint8_t*)realloc(mData, desired);
1989 if (!data && desired > mDataCapacity) {
1990 mError = NO_MEMORY;
1991 return NO_MEMORY;
1992 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001993
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001994 releaseObjects();
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001995
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001996 if (data) {
Dianne Hackborn7e790af2014-11-11 12:22:53 -08001997 LOG_ALLOC("Parcel %p: restart from %zu to %zu capacity", this, mDataCapacity, desired);
Dianne Hackborna4cff882014-11-13 17:07:40 -08001998 pthread_mutex_lock(&gParcelGlobalAllocSizeLock);
Dianne Hackborn7e790af2014-11-11 12:22:53 -08001999 gParcelGlobalAllocSize += desired;
2000 gParcelGlobalAllocSize -= mDataCapacity;
Dianne Hackborna4cff882014-11-13 17:07:40 -08002001 pthread_mutex_unlock(&gParcelGlobalAllocSizeLock);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002002 mData = data;
2003 mDataCapacity = desired;
2004 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002005
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002006 mDataSize = mDataPos = 0;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002007 ALOGV("restartWrite Setting data size of %p to %zu", this, mDataSize);
2008 ALOGV("restartWrite Setting data pos of %p to %zu", this, mDataPos);
2009
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002010 free(mObjects);
2011 mObjects = NULL;
2012 mObjectsSize = mObjectsCapacity = 0;
2013 mNextObjectHint = 0;
2014 mHasFds = false;
2015 mFdsKnown = true;
Dianne Hackborn8938ed22011-09-28 23:19:47 -04002016 mAllowFds = true;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002017
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002018 return NO_ERROR;
2019}
2020
2021status_t Parcel::continueWrite(size_t desired)
2022{
Nick Kralevichb6b14232015-04-02 09:36:02 -07002023 if (desired > INT32_MAX) {
2024 // don't accept size_t values which may have come from an
2025 // inadvertent conversion from a negative int.
2026 return BAD_VALUE;
2027 }
2028
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002029 // If shrinking, first adjust for any objects that appear
2030 // after the new data size.
2031 size_t objectsSize = mObjectsSize;
2032 if (desired < mDataSize) {
2033 if (desired == 0) {
2034 objectsSize = 0;
2035 } else {
2036 while (objectsSize > 0) {
2037 if (mObjects[objectsSize-1] < desired)
2038 break;
2039 objectsSize--;
2040 }
2041 }
2042 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002043
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002044 if (mOwner) {
2045 // If the size is going to zero, just release the owner's data.
2046 if (desired == 0) {
2047 freeData();
2048 return NO_ERROR;
2049 }
2050
2051 // If there is a different owner, we need to take
2052 // posession.
2053 uint8_t* data = (uint8_t*)malloc(desired);
2054 if (!data) {
2055 mError = NO_MEMORY;
2056 return NO_MEMORY;
2057 }
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002058 binder_size_t* objects = NULL;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002059
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002060 if (objectsSize) {
Nick Kraleviche9881a32015-04-28 16:21:30 -07002061 objects = (binder_size_t*)calloc(objectsSize, sizeof(binder_size_t));
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002062 if (!objects) {
Hyejin Kim3f727c02013-03-09 11:28:54 +09002063 free(data);
2064
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002065 mError = NO_MEMORY;
2066 return NO_MEMORY;
2067 }
2068
2069 // Little hack to only acquire references on objects
2070 // we will be keeping.
2071 size_t oldObjectsSize = mObjectsSize;
2072 mObjectsSize = objectsSize;
2073 acquireObjects();
2074 mObjectsSize = oldObjectsSize;
2075 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002076
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002077 if (mData) {
2078 memcpy(data, mData, mDataSize < desired ? mDataSize : desired);
2079 }
2080 if (objects && mObjects) {
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002081 memcpy(objects, mObjects, objectsSize*sizeof(binder_size_t));
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002082 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002083 //ALOGI("Freeing data ref of %p (pid=%d)", this, getpid());
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002084 mOwner(this, mData, mDataSize, mObjects, mObjectsSize, mOwnerCookie);
2085 mOwner = NULL;
2086
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002087 LOG_ALLOC("Parcel %p: taking ownership of %zu capacity", this, desired);
Dianne Hackborna4cff882014-11-13 17:07:40 -08002088 pthread_mutex_lock(&gParcelGlobalAllocSizeLock);
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002089 gParcelGlobalAllocSize += desired;
2090 gParcelGlobalAllocCount++;
Dianne Hackborna4cff882014-11-13 17:07:40 -08002091 pthread_mutex_unlock(&gParcelGlobalAllocSizeLock);
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002092
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002093 mData = data;
2094 mObjects = objects;
2095 mDataSize = (mDataSize < desired) ? mDataSize : desired;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002096 ALOGV("continueWrite Setting data size of %p to %zu", this, mDataSize);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002097 mDataCapacity = desired;
2098 mObjectsSize = mObjectsCapacity = objectsSize;
2099 mNextObjectHint = 0;
2100
2101 } else if (mData) {
2102 if (objectsSize < mObjectsSize) {
2103 // Need to release refs on any objects we are dropping.
2104 const sp<ProcessState> proc(ProcessState::self());
2105 for (size_t i=objectsSize; i<mObjectsSize; i++) {
2106 const flat_binder_object* flat
2107 = reinterpret_cast<flat_binder_object*>(mData+mObjects[i]);
2108 if (flat->type == BINDER_TYPE_FD) {
2109 // will need to rescan because we may have lopped off the only FDs
2110 mFdsKnown = false;
2111 }
2112 release_object(proc, *flat, this);
2113 }
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002114 binder_size_t* objects =
2115 (binder_size_t*)realloc(mObjects, objectsSize*sizeof(binder_size_t));
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002116 if (objects) {
2117 mObjects = objects;
2118 }
2119 mObjectsSize = objectsSize;
2120 mNextObjectHint = 0;
2121 }
2122
2123 // We own the data, so we can just do a realloc().
2124 if (desired > mDataCapacity) {
2125 uint8_t* data = (uint8_t*)realloc(mData, desired);
2126 if (data) {
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002127 LOG_ALLOC("Parcel %p: continue from %zu to %zu capacity", this, mDataCapacity,
2128 desired);
Dianne Hackborna4cff882014-11-13 17:07:40 -08002129 pthread_mutex_lock(&gParcelGlobalAllocSizeLock);
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002130 gParcelGlobalAllocSize += desired;
2131 gParcelGlobalAllocSize -= mDataCapacity;
Dan Austin48fd7b42015-09-10 13:46:02 -07002132 gParcelGlobalAllocCount++;
Dianne Hackborna4cff882014-11-13 17:07:40 -08002133 pthread_mutex_unlock(&gParcelGlobalAllocSizeLock);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002134 mData = data;
2135 mDataCapacity = desired;
2136 } else if (desired > mDataCapacity) {
2137 mError = NO_MEMORY;
2138 return NO_MEMORY;
2139 }
2140 } else {
Dianne Hackborn97e2bcd2011-04-13 18:15:56 -07002141 if (mDataSize > desired) {
2142 mDataSize = desired;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002143 ALOGV("continueWrite Setting data size of %p to %zu", this, mDataSize);
Dianne Hackborn97e2bcd2011-04-13 18:15:56 -07002144 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002145 if (mDataPos > desired) {
2146 mDataPos = desired;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002147 ALOGV("continueWrite Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002148 }
2149 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002150
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002151 } else {
2152 // This is the first data. Easy!
2153 uint8_t* data = (uint8_t*)malloc(desired);
2154 if (!data) {
2155 mError = NO_MEMORY;
2156 return NO_MEMORY;
2157 }
Hyejin Kim3f727c02013-03-09 11:28:54 +09002158
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002159 if(!(mDataCapacity == 0 && mObjects == NULL
2160 && mObjectsCapacity == 0)) {
Colin Cross6f4f3ab2014-02-05 17:42:44 -08002161 ALOGE("continueWrite: %zu/%p/%zu/%zu", mDataCapacity, mObjects, mObjectsCapacity, desired);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002162 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002163
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002164 LOG_ALLOC("Parcel %p: allocating with %zu capacity", this, desired);
Dianne Hackborna4cff882014-11-13 17:07:40 -08002165 pthread_mutex_lock(&gParcelGlobalAllocSizeLock);
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002166 gParcelGlobalAllocSize += desired;
2167 gParcelGlobalAllocCount++;
Dianne Hackborna4cff882014-11-13 17:07:40 -08002168 pthread_mutex_unlock(&gParcelGlobalAllocSizeLock);
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002169
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002170 mData = data;
2171 mDataSize = mDataPos = 0;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002172 ALOGV("continueWrite Setting data size of %p to %zu", this, mDataSize);
2173 ALOGV("continueWrite Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002174 mDataCapacity = desired;
2175 }
2176
2177 return NO_ERROR;
2178}
2179
2180void Parcel::initState()
2181{
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002182 LOG_ALLOC("Parcel %p: initState", this);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002183 mError = NO_ERROR;
2184 mData = 0;
2185 mDataSize = 0;
2186 mDataCapacity = 0;
2187 mDataPos = 0;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002188 ALOGV("initState Setting data size of %p to %zu", this, mDataSize);
2189 ALOGV("initState Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002190 mObjects = NULL;
2191 mObjectsSize = 0;
2192 mObjectsCapacity = 0;
2193 mNextObjectHint = 0;
2194 mHasFds = false;
2195 mFdsKnown = true;
Dianne Hackborn8938ed22011-09-28 23:19:47 -04002196 mAllowFds = true;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002197 mOwner = NULL;
Dan Sandleraa5c2342015-04-10 10:08:45 -04002198 mBlobAshmemSize = 0;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002199}
2200
2201void Parcel::scanForFds() const
2202{
2203 bool hasFds = false;
2204 for (size_t i=0; i<mObjectsSize; i++) {
2205 const flat_binder_object* flat
2206 = reinterpret_cast<const flat_binder_object*>(mData + mObjects[i]);
2207 if (flat->type == BINDER_TYPE_FD) {
2208 hasFds = true;
2209 break;
2210 }
2211 }
2212 mHasFds = hasFds;
2213 mFdsKnown = true;
2214}
2215
Dan Sandleraa5c2342015-04-10 10:08:45 -04002216size_t Parcel::getBlobAshmemSize() const
2217{
2218 return mBlobAshmemSize;
2219}
2220
Jeff Brown5707dbf2011-09-23 21:17:56 -07002221// --- Parcel::Blob ---
2222
2223Parcel::Blob::Blob() :
Jeff Brown13b16042014-11-11 16:44:25 -08002224 mFd(-1), mData(NULL), mSize(0), mMutable(false) {
Jeff Brown5707dbf2011-09-23 21:17:56 -07002225}
2226
2227Parcel::Blob::~Blob() {
2228 release();
2229}
2230
2231void Parcel::Blob::release() {
Jeff Brown13b16042014-11-11 16:44:25 -08002232 if (mFd != -1 && mData) {
Jeff Brown5707dbf2011-09-23 21:17:56 -07002233 ::munmap(mData, mSize);
2234 }
2235 clear();
2236}
2237
Jeff Brown13b16042014-11-11 16:44:25 -08002238void Parcel::Blob::init(int fd, void* data, size_t size, bool isMutable) {
2239 mFd = fd;
Jeff Brown5707dbf2011-09-23 21:17:56 -07002240 mData = data;
2241 mSize = size;
Jeff Brown13b16042014-11-11 16:44:25 -08002242 mMutable = isMutable;
Jeff Brown5707dbf2011-09-23 21:17:56 -07002243}
2244
2245void Parcel::Blob::clear() {
Jeff Brown13b16042014-11-11 16:44:25 -08002246 mFd = -1;
Jeff Brown5707dbf2011-09-23 21:17:56 -07002247 mData = NULL;
2248 mSize = 0;
Jeff Brown13b16042014-11-11 16:44:25 -08002249 mMutable = false;
Jeff Brown5707dbf2011-09-23 21:17:56 -07002250}
2251
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002252}; // namespace android