blob: b8f5436491bff3a745394548d2351744843d729c [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,
Adrian Rooscbf37262015-10-22 16:12:53 -070099 const flat_binder_object& obj, const void* who, size_t* outAshmemSize)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700100{
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: {
Adrian Rooscbf37262015-10-22 16:12:53 -0700126 if (obj.cookie != 0) {
Adrian Roos6bb31142015-10-22 16:46:12 -0700127 if (outAshmemSize != NULL) {
128 // If we own an ashmem fd, keep track of how much memory it refers to.
129 int size = ashmem_get_size_region(obj.handle);
130 if (size > 0) {
131 *outAshmemSize += size;
132 }
Adrian Rooscbf37262015-10-22 16:12:53 -0700133 }
134 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700135 return;
136 }
137 }
138
Colin Cross6f4f3ab2014-02-05 17:42:44 -0800139 ALOGD("Invalid object type 0x%08x", obj.type);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700140}
141
Adrian Roos6bb31142015-10-22 16:46:12 -0700142void acquire_object(const sp<ProcessState>& proc,
143 const flat_binder_object& obj, const void* who)
144{
145 acquire_object(proc, obj, who, NULL);
146}
147
148static void release_object(const sp<ProcessState>& proc,
Adrian Rooscbf37262015-10-22 16:12:53 -0700149 const flat_binder_object& obj, const void* who, size_t* outAshmemSize)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700150{
151 switch (obj.type) {
152 case BINDER_TYPE_BINDER:
153 if (obj.binder) {
154 LOG_REFS("Parcel %p releasing reference on local %p", who, obj.cookie);
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800155 reinterpret_cast<IBinder*>(obj.cookie)->decStrong(who);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700156 }
157 return;
158 case BINDER_TYPE_WEAK_BINDER:
159 if (obj.binder)
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800160 reinterpret_cast<RefBase::weakref_type*>(obj.binder)->decWeak(who);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700161 return;
162 case BINDER_TYPE_HANDLE: {
163 const sp<IBinder> b = proc->getStrongProxyForHandle(obj.handle);
164 if (b != NULL) {
165 LOG_REFS("Parcel %p releasing reference on remote %p", who, b.get());
166 b->decStrong(who);
167 }
168 return;
169 }
170 case BINDER_TYPE_WEAK_HANDLE: {
171 const wp<IBinder> b = proc->getWeakProxyForHandle(obj.handle);
172 if (b != NULL) b.get_refs()->decWeak(who);
173 return;
174 }
175 case BINDER_TYPE_FD: {
Adrian Roos6bb31142015-10-22 16:46:12 -0700176 if (outAshmemSize != NULL) {
177 if (obj.cookie != 0) {
178 int size = ashmem_get_size_region(obj.handle);
179 if (size > 0) {
180 *outAshmemSize -= size;
181 }
Adrian Rooscbf37262015-10-22 16:12:53 -0700182
Adrian Roos6bb31142015-10-22 16:46:12 -0700183 close(obj.handle);
184 }
Adrian Rooscbf37262015-10-22 16:12:53 -0700185 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700186 return;
187 }
188 }
189
Colin Cross6f4f3ab2014-02-05 17:42:44 -0800190 ALOGE("Invalid object type 0x%08x", obj.type);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700191}
192
Adrian Roos6bb31142015-10-22 16:46:12 -0700193void release_object(const sp<ProcessState>& proc,
194 const flat_binder_object& obj, const void* who)
195{
196 release_object(proc, obj, who, NULL);
197}
198
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700199inline static status_t finish_flatten_binder(
Colin Cross6f4f3ab2014-02-05 17:42:44 -0800200 const sp<IBinder>& /*binder*/, const flat_binder_object& flat, Parcel* out)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700201{
202 return out->writeObject(flat, false);
203}
204
Colin Cross6f4f3ab2014-02-05 17:42:44 -0800205status_t flatten_binder(const sp<ProcessState>& /*proc*/,
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700206 const sp<IBinder>& binder, Parcel* out)
207{
208 flat_binder_object obj;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700209
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700210 obj.flags = 0x7f | FLAT_BINDER_FLAG_ACCEPTS_FDS;
211 if (binder != NULL) {
212 IBinder *local = binder->localBinder();
213 if (!local) {
214 BpBinder *proxy = binder->remoteBinder();
215 if (proxy == NULL) {
Steve Blocke6f43dd2012-01-06 19:20:56 +0000216 ALOGE("null proxy");
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700217 }
218 const int32_t handle = proxy ? proxy->handle() : 0;
219 obj.type = BINDER_TYPE_HANDLE;
Arve Hjønnevåg07fd0f12014-02-18 21:10:29 -0800220 obj.binder = 0; /* Don't pass uninitialized stack data to a remote process */
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700221 obj.handle = handle;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800222 obj.cookie = 0;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700223 } else {
224 obj.type = BINDER_TYPE_BINDER;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800225 obj.binder = reinterpret_cast<uintptr_t>(local->getWeakRefs());
226 obj.cookie = reinterpret_cast<uintptr_t>(local);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700227 }
228 } else {
229 obj.type = BINDER_TYPE_BINDER;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800230 obj.binder = 0;
231 obj.cookie = 0;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700232 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700233
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700234 return finish_flatten_binder(binder, obj, out);
235}
236
Colin Cross6f4f3ab2014-02-05 17:42:44 -0800237status_t flatten_binder(const sp<ProcessState>& /*proc*/,
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700238 const wp<IBinder>& binder, Parcel* out)
239{
240 flat_binder_object obj;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700241
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700242 obj.flags = 0x7f | FLAT_BINDER_FLAG_ACCEPTS_FDS;
243 if (binder != NULL) {
244 sp<IBinder> real = binder.promote();
245 if (real != NULL) {
246 IBinder *local = real->localBinder();
247 if (!local) {
248 BpBinder *proxy = real->remoteBinder();
249 if (proxy == NULL) {
Steve Blocke6f43dd2012-01-06 19:20:56 +0000250 ALOGE("null proxy");
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700251 }
252 const int32_t handle = proxy ? proxy->handle() : 0;
253 obj.type = BINDER_TYPE_WEAK_HANDLE;
Arve Hjønnevåg07fd0f12014-02-18 21:10:29 -0800254 obj.binder = 0; /* Don't pass uninitialized stack data to a remote process */
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700255 obj.handle = handle;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800256 obj.cookie = 0;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700257 } else {
258 obj.type = BINDER_TYPE_WEAK_BINDER;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800259 obj.binder = reinterpret_cast<uintptr_t>(binder.get_refs());
260 obj.cookie = reinterpret_cast<uintptr_t>(binder.unsafe_get());
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700261 }
262 return finish_flatten_binder(real, obj, out);
263 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700264
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700265 // XXX How to deal? In order to flatten the given binder,
266 // we need to probe it for information, which requires a primary
267 // reference... but we don't have one.
268 //
269 // The OpenBinder implementation uses a dynamic_cast<> here,
270 // but we can't do that with the different reference counting
271 // implementation we are using.
Steve Blocke6f43dd2012-01-06 19:20:56 +0000272 ALOGE("Unable to unflatten Binder weak reference!");
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700273 obj.type = BINDER_TYPE_BINDER;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800274 obj.binder = 0;
275 obj.cookie = 0;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700276 return finish_flatten_binder(NULL, obj, out);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700277
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700278 } else {
279 obj.type = BINDER_TYPE_BINDER;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800280 obj.binder = 0;
281 obj.cookie = 0;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700282 return finish_flatten_binder(NULL, obj, out);
283 }
284}
285
286inline static status_t finish_unflatten_binder(
Colin Cross6f4f3ab2014-02-05 17:42:44 -0800287 BpBinder* /*proxy*/, const flat_binder_object& /*flat*/,
288 const Parcel& /*in*/)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700289{
290 return NO_ERROR;
291}
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700292
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700293status_t unflatten_binder(const sp<ProcessState>& proc,
294 const Parcel& in, sp<IBinder>* out)
295{
296 const flat_binder_object* flat = in.readObject(false);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700297
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700298 if (flat) {
299 switch (flat->type) {
300 case BINDER_TYPE_BINDER:
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800301 *out = reinterpret_cast<IBinder*>(flat->cookie);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700302 return finish_unflatten_binder(NULL, *flat, in);
303 case BINDER_TYPE_HANDLE:
304 *out = proc->getStrongProxyForHandle(flat->handle);
305 return finish_unflatten_binder(
306 static_cast<BpBinder*>(out->get()), *flat, in);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700307 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700308 }
309 return BAD_TYPE;
310}
311
312status_t unflatten_binder(const sp<ProcessState>& proc,
313 const Parcel& in, wp<IBinder>* out)
314{
315 const flat_binder_object* flat = in.readObject(false);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700316
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700317 if (flat) {
318 switch (flat->type) {
319 case BINDER_TYPE_BINDER:
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800320 *out = reinterpret_cast<IBinder*>(flat->cookie);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700321 return finish_unflatten_binder(NULL, *flat, in);
322 case BINDER_TYPE_WEAK_BINDER:
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800323 if (flat->binder != 0) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700324 out->set_object_and_refs(
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800325 reinterpret_cast<IBinder*>(flat->cookie),
326 reinterpret_cast<RefBase::weakref_type*>(flat->binder));
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700327 } else {
328 *out = NULL;
329 }
330 return finish_unflatten_binder(NULL, *flat, in);
331 case BINDER_TYPE_HANDLE:
332 case BINDER_TYPE_WEAK_HANDLE:
333 *out = proc->getWeakProxyForHandle(flat->handle);
334 return finish_unflatten_binder(
335 static_cast<BpBinder*>(out->unsafe_get()), *flat, in);
336 }
337 }
338 return BAD_TYPE;
339}
340
Christopher Wiley6264c4c2015-11-10 09:44:30 -0800341namespace {
342
343template<typename T>
344status_t readTypedVector(std::vector<T>* val, const Parcel* p,
345 status_t(Parcel::*read_func)(T*) const) {
346 val->clear();
347
348 int32_t size;
349 status_t status = p->readInt32(&size);
350
351 if (status != OK) {
352 return status;
353 }
354
355 if (size < 0) {
356 return UNEXPECTED_NULL;
357 }
358
359 val->resize(size);
360
361 for (auto& v: *val) {
362 status = (p->*read_func)(&v);
363
364 if (status != OK) {
365 return status;
366 }
367 }
368
369 return OK;
370}
371
372} // namespace
373
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700374// ---------------------------------------------------------------------------
375
376Parcel::Parcel()
377{
Dianne Hackborn7e790af2014-11-11 12:22:53 -0800378 LOG_ALLOC("Parcel %p: constructing", this);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700379 initState();
380}
381
382Parcel::~Parcel()
383{
384 freeDataNoInit();
Dianne Hackborn7e790af2014-11-11 12:22:53 -0800385 LOG_ALLOC("Parcel %p: destroyed", this);
386}
387
388size_t Parcel::getGlobalAllocSize() {
Dianne Hackborna4cff882014-11-13 17:07:40 -0800389 pthread_mutex_lock(&gParcelGlobalAllocSizeLock);
390 size_t size = gParcelGlobalAllocSize;
391 pthread_mutex_unlock(&gParcelGlobalAllocSizeLock);
392 return size;
Dianne Hackborn7e790af2014-11-11 12:22:53 -0800393}
394
395size_t Parcel::getGlobalAllocCount() {
Dianne Hackborna4cff882014-11-13 17:07:40 -0800396 pthread_mutex_lock(&gParcelGlobalAllocSizeLock);
397 size_t count = gParcelGlobalAllocCount;
398 pthread_mutex_unlock(&gParcelGlobalAllocSizeLock);
399 return count;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700400}
401
402const uint8_t* Parcel::data() const
403{
404 return mData;
405}
406
407size_t Parcel::dataSize() const
408{
409 return (mDataSize > mDataPos ? mDataSize : mDataPos);
410}
411
412size_t Parcel::dataAvail() const
413{
Nick Kralevichcfe27de2015-09-16 09:49:15 -0700414 size_t result = dataSize() - dataPosition();
415 if (result > INT32_MAX) {
416 abort();
417 }
418 return result;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700419}
420
421size_t Parcel::dataPosition() const
422{
423 return mDataPos;
424}
425
426size_t Parcel::dataCapacity() const
427{
428 return mDataCapacity;
429}
430
431status_t Parcel::setDataSize(size_t size)
432{
Nick Kralevichb6b14232015-04-02 09:36:02 -0700433 if (size > INT32_MAX) {
434 // don't accept size_t values which may have come from an
435 // inadvertent conversion from a negative int.
436 return BAD_VALUE;
437 }
438
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700439 status_t err;
440 err = continueWrite(size);
441 if (err == NO_ERROR) {
442 mDataSize = size;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700443 ALOGV("setDataSize Setting data size of %p to %zu", this, mDataSize);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700444 }
445 return err;
446}
447
448void Parcel::setDataPosition(size_t pos) const
449{
Nick Kralevichb6b14232015-04-02 09:36:02 -0700450 if (pos > INT32_MAX) {
451 // don't accept size_t values which may have come from an
452 // inadvertent conversion from a negative int.
453 abort();
454 }
455
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700456 mDataPos = pos;
457 mNextObjectHint = 0;
458}
459
460status_t Parcel::setDataCapacity(size_t size)
461{
Nick Kralevichb6b14232015-04-02 09:36:02 -0700462 if (size > INT32_MAX) {
463 // don't accept size_t values which may have come from an
464 // inadvertent conversion from a negative int.
465 return BAD_VALUE;
466 }
467
Dianne Hackborn97e2bcd2011-04-13 18:15:56 -0700468 if (size > mDataCapacity) return continueWrite(size);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700469 return NO_ERROR;
470}
471
472status_t Parcel::setData(const uint8_t* buffer, size_t len)
473{
Nick Kralevichb6b14232015-04-02 09:36:02 -0700474 if (len > INT32_MAX) {
475 // don't accept size_t values which may have come from an
476 // inadvertent conversion from a negative int.
477 return BAD_VALUE;
478 }
479
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700480 status_t err = restartWrite(len);
481 if (err == NO_ERROR) {
482 memcpy(const_cast<uint8_t*>(data()), buffer, len);
483 mDataSize = len;
484 mFdsKnown = false;
485 }
486 return err;
487}
488
Andreas Huber51faf462011-04-13 10:21:56 -0700489status_t Parcel::appendFrom(const Parcel *parcel, size_t offset, size_t len)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700490{
491 const sp<ProcessState> proc(ProcessState::self());
492 status_t err;
Andreas Huber51faf462011-04-13 10:21:56 -0700493 const uint8_t *data = parcel->mData;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800494 const binder_size_t *objects = parcel->mObjects;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700495 size_t size = parcel->mObjectsSize;
496 int startPos = mDataPos;
497 int firstIndex = -1, lastIndex = -2;
498
499 if (len == 0) {
500 return NO_ERROR;
501 }
502
Nick Kralevichb6b14232015-04-02 09:36:02 -0700503 if (len > INT32_MAX) {
504 // don't accept size_t values which may have come from an
505 // inadvertent conversion from a negative int.
506 return BAD_VALUE;
507 }
508
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700509 // range checks against the source parcel size
510 if ((offset > parcel->mDataSize)
511 || (len > parcel->mDataSize)
512 || (offset + len > parcel->mDataSize)) {
513 return BAD_VALUE;
514 }
515
516 // Count objects in range
517 for (int i = 0; i < (int) size; i++) {
518 size_t off = objects[i];
Christopher Tate27182be2015-05-27 17:53:02 -0700519 if ((off >= offset) && (off + sizeof(flat_binder_object) <= offset + len)) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700520 if (firstIndex == -1) {
521 firstIndex = i;
522 }
523 lastIndex = i;
524 }
525 }
526 int numObjects = lastIndex - firstIndex + 1;
527
Dianne Hackborn97e2bcd2011-04-13 18:15:56 -0700528 if ((mDataSize+len) > mDataCapacity) {
529 // grow data
530 err = growData(len);
531 if (err != NO_ERROR) {
532 return err;
533 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700534 }
535
536 // append data
537 memcpy(mData + mDataPos, data + offset, len);
538 mDataPos += len;
539 mDataSize += len;
540
Dianne Hackborn8938ed22011-09-28 23:19:47 -0400541 err = NO_ERROR;
542
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700543 if (numObjects > 0) {
544 // grow objects
545 if (mObjectsCapacity < mObjectsSize + numObjects) {
Christopher Tateed7a50c2015-06-08 14:45:14 -0700546 size_t newSize = ((mObjectsSize + numObjects)*3)/2;
547 if (newSize < mObjectsSize) return NO_MEMORY; // overflow
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800548 binder_size_t *objects =
549 (binder_size_t*)realloc(mObjects, newSize*sizeof(binder_size_t));
550 if (objects == (binder_size_t*)0) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700551 return NO_MEMORY;
552 }
553 mObjects = objects;
554 mObjectsCapacity = newSize;
555 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700556
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700557 // append and acquire objects
558 int idx = mObjectsSize;
559 for (int i = firstIndex; i <= lastIndex; i++) {
560 size_t off = objects[i] - offset + startPos;
561 mObjects[idx++] = off;
562 mObjectsSize++;
563
Dianne Hackborn8af0f822009-05-22 13:20:23 -0700564 flat_binder_object* flat
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700565 = reinterpret_cast<flat_binder_object*>(mData + off);
Adrian Rooscbf37262015-10-22 16:12:53 -0700566 acquire_object(proc, *flat, this, &mOpenAshmemSize);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700567
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700568 if (flat->type == BINDER_TYPE_FD) {
Dianne Hackborn8af0f822009-05-22 13:20:23 -0700569 // If this is a file descriptor, we need to dup it so the
570 // new Parcel now owns its own fd, and can declare that we
571 // officially know we have fds.
572 flat->handle = dup(flat->handle);
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800573 flat->cookie = 1;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700574 mHasFds = mFdsKnown = true;
Dianne Hackborn8938ed22011-09-28 23:19:47 -0400575 if (!mAllowFds) {
576 err = FDS_NOT_ALLOWED;
577 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700578 }
579 }
580 }
581
Dianne Hackborn8938ed22011-09-28 23:19:47 -0400582 return err;
583}
584
Jeff Brown13b16042014-11-11 16:44:25 -0800585bool Parcel::allowFds() const
586{
587 return mAllowFds;
588}
589
Dianne Hackborn7746cc32011-10-03 21:09:35 -0700590bool Parcel::pushAllowFds(bool allowFds)
Dianne Hackborn8938ed22011-09-28 23:19:47 -0400591{
592 const bool origValue = mAllowFds;
Dianne Hackborn7746cc32011-10-03 21:09:35 -0700593 if (!allowFds) {
594 mAllowFds = false;
595 }
Dianne Hackborn8938ed22011-09-28 23:19:47 -0400596 return origValue;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700597}
598
Dianne Hackborn7746cc32011-10-03 21:09:35 -0700599void Parcel::restoreAllowFds(bool lastValue)
600{
601 mAllowFds = lastValue;
602}
603
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700604bool Parcel::hasFileDescriptors() const
605{
606 if (!mFdsKnown) {
607 scanForFds();
608 }
609 return mHasFds;
610}
611
Brad Fitzpatrick702ea9d2010-06-18 13:07:53 -0700612// Write RPC headers. (previously just the interface token)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700613status_t Parcel::writeInterfaceToken(const String16& interface)
614{
Brad Fitzpatricka877cd82010-07-07 16:06:39 -0700615 writeInt32(IPCThreadState::self()->getStrictModePolicy() |
616 STRICT_MODE_PENALTY_GATHER);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700617 // currently the interface identification token is just its name as a string
618 return writeString16(interface);
619}
620
Mathias Agopian83c04462009-05-22 19:00:22 -0700621bool Parcel::checkInterface(IBinder* binder) const
622{
Brad Fitzpatrick702ea9d2010-06-18 13:07:53 -0700623 return enforceInterface(binder->getInterfaceDescriptor());
Mathias Agopian83c04462009-05-22 19:00:22 -0700624}
625
Brad Fitzpatricka877cd82010-07-07 16:06:39 -0700626bool Parcel::enforceInterface(const String16& interface,
Brad Fitzpatrick70081a12010-07-27 09:49:11 -0700627 IPCThreadState* threadState) const
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700628{
Brad Fitzpatrick70081a12010-07-27 09:49:11 -0700629 int32_t strictPolicy = readInt32();
630 if (threadState == NULL) {
631 threadState = IPCThreadState::self();
Brad Fitzpatricka877cd82010-07-07 16:06:39 -0700632 }
Brad Fitzpatrick52736032010-08-30 16:01:16 -0700633 if ((threadState->getLastTransactionBinderFlags() &
634 IBinder::FLAG_ONEWAY) != 0) {
635 // For one-way calls, the callee is running entirely
636 // disconnected from the caller, so disable StrictMode entirely.
637 // Not only does disk/network usage not impact the caller, but
638 // there's no way to commuicate back any violations anyway.
639 threadState->setStrictModePolicy(0);
640 } else {
641 threadState->setStrictModePolicy(strictPolicy);
642 }
Mathias Agopian83c04462009-05-22 19:00:22 -0700643 const String16 str(readString16());
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700644 if (str == interface) {
645 return true;
646 } else {
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700647 ALOGW("**** enforceInterface() expected '%s' but read '%s'",
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700648 String8(interface).string(), String8(str).string());
649 return false;
650 }
Brad Fitzpatrick702ea9d2010-06-18 13:07:53 -0700651}
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700652
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800653const binder_size_t* Parcel::objects() const
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700654{
655 return mObjects;
656}
657
658size_t Parcel::objectsCount() const
659{
660 return mObjectsSize;
661}
662
663status_t Parcel::errorCheck() const
664{
665 return mError;
666}
667
668void Parcel::setError(status_t err)
669{
670 mError = err;
671}
672
673status_t Parcel::finishWrite(size_t len)
674{
Nick Kralevichb6b14232015-04-02 09:36:02 -0700675 if (len > INT32_MAX) {
676 // don't accept size_t values which may have come from an
677 // inadvertent conversion from a negative int.
678 return BAD_VALUE;
679 }
680
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700681 //printf("Finish write of %d\n", len);
682 mDataPos += len;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700683 ALOGV("finishWrite Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700684 if (mDataPos > mDataSize) {
685 mDataSize = mDataPos;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700686 ALOGV("finishWrite Setting data size of %p to %zu", this, mDataSize);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700687 }
688 //printf("New pos=%d, size=%d\n", mDataPos, mDataSize);
689 return NO_ERROR;
690}
691
692status_t Parcel::writeUnpadded(const void* data, size_t len)
693{
Nick Kralevichb6b14232015-04-02 09:36:02 -0700694 if (len > INT32_MAX) {
695 // don't accept size_t values which may have come from an
696 // inadvertent conversion from a negative int.
697 return BAD_VALUE;
698 }
699
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700700 size_t end = mDataPos + len;
701 if (end < mDataPos) {
702 // integer overflow
703 return BAD_VALUE;
704 }
705
706 if (end <= mDataCapacity) {
707restart_write:
708 memcpy(mData+mDataPos, data, len);
709 return finishWrite(len);
710 }
711
712 status_t err = growData(len);
713 if (err == NO_ERROR) goto restart_write;
714 return err;
715}
716
717status_t Parcel::write(const void* data, size_t len)
718{
Nick Kralevichb6b14232015-04-02 09:36:02 -0700719 if (len > INT32_MAX) {
720 // don't accept size_t values which may have come from an
721 // inadvertent conversion from a negative int.
722 return BAD_VALUE;
723 }
724
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700725 void* const d = writeInplace(len);
726 if (d) {
727 memcpy(d, data, len);
728 return NO_ERROR;
729 }
730 return mError;
731}
732
733void* Parcel::writeInplace(size_t len)
734{
Nick Kralevichb6b14232015-04-02 09:36:02 -0700735 if (len > INT32_MAX) {
736 // don't accept size_t values which may have come from an
737 // inadvertent conversion from a negative int.
738 return NULL;
739 }
740
741 const size_t padded = pad_size(len);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700742
743 // sanity check for integer overflow
744 if (mDataPos+padded < mDataPos) {
745 return NULL;
746 }
747
748 if ((mDataPos+padded) <= mDataCapacity) {
749restart_write:
750 //printf("Writing %ld bytes, padded to %ld\n", len, padded);
751 uint8_t* const data = mData+mDataPos;
752
753 // Need to pad at end?
754 if (padded != len) {
755#if BYTE_ORDER == BIG_ENDIAN
756 static const uint32_t mask[4] = {
757 0x00000000, 0xffffff00, 0xffff0000, 0xff000000
758 };
759#endif
760#if BYTE_ORDER == LITTLE_ENDIAN
761 static const uint32_t mask[4] = {
762 0x00000000, 0x00ffffff, 0x0000ffff, 0x000000ff
763 };
764#endif
765 //printf("Applying pad mask: %p to %p\n", (void*)mask[padded-len],
766 // *reinterpret_cast<void**>(data+padded-4));
767 *reinterpret_cast<uint32_t*>(data+padded-4) &= mask[padded-len];
768 }
769
770 finishWrite(padded);
771 return data;
772 }
773
774 status_t err = growData(padded);
775 if (err == NO_ERROR) goto restart_write;
776 return NULL;
777}
778
Casey Dahlin32d29a52015-11-13 13:46:29 -0800779namespace {
780
781template<typename T, typename U>
782status_t unsafeWriteTypedVector(const std::vector<T>& val, Parcel* p,
783 status_t(Parcel::*write_func)(U)) {
784 if (val.size() > std::numeric_limits<int32_t>::max()) {
785 return BAD_VALUE;
786 }
787
788 status_t status = p->writeInt32(val.size());
789
790 if (status != OK) {
791 return status;
792 }
793
794 for (const auto& item : val) {
795 status = (p->*write_func)(item);
796
797 if (status != OK) {
798 return status;
799 }
800 }
801
802 return OK;
803}
804
805template<typename T>
806status_t writeTypedVector(const std::vector<T>& val, Parcel* p,
807 status_t(Parcel::*write_func)(const T&)) {
808 return unsafeWriteTypedVector(val, p, write_func);
809}
810
811template<typename T>
812status_t writeTypedVector(const std::vector<T>& val, Parcel* p,
813 status_t(Parcel::*write_func)(T)) {
814 return unsafeWriteTypedVector(val, p, write_func);
815}
816
817} // namespace
818
Casey Dahlin08bb1452015-10-19 18:12:18 -0700819status_t Parcel::writeByteVector(const std::vector<int8_t>& val)
820{
Christopher Wiley714a3ab2015-10-31 13:22:15 -0700821 status_t status;
Casey Dahlin08bb1452015-10-19 18:12:18 -0700822 if (val.size() > std::numeric_limits<int32_t>::max()) {
Christopher Wiley714a3ab2015-10-31 13:22:15 -0700823 status = BAD_VALUE;
824 return status;
Casey Dahlin08bb1452015-10-19 18:12:18 -0700825 }
826
Christopher Wiley714a3ab2015-10-31 13:22:15 -0700827 status = writeInt32(val.size());
Casey Dahlin08bb1452015-10-19 18:12:18 -0700828 if (status != OK) {
829 return status;
830 }
831
Christopher Wiley714a3ab2015-10-31 13:22:15 -0700832 void* data = writeInplace(val.size());
833 if (!data) {
834 status = BAD_VALUE;
835 return status;
Casey Dahlin08bb1452015-10-19 18:12:18 -0700836 }
837
Christopher Wiley714a3ab2015-10-31 13:22:15 -0700838 memcpy(data, val.data(), val.size());
839 return status;
Casey Dahlin08bb1452015-10-19 18:12:18 -0700840}
841
842status_t Parcel::writeInt32Vector(const std::vector<int32_t>& val)
843{
Casey Dahlin32d29a52015-11-13 13:46:29 -0800844 return writeTypedVector(val, this, &Parcel::writeInt32);
Casey Dahlin08bb1452015-10-19 18:12:18 -0700845}
846
847status_t Parcel::writeInt64Vector(const std::vector<int64_t>& val)
848{
Casey Dahlin32d29a52015-11-13 13:46:29 -0800849 return writeTypedVector(val, this, &Parcel::writeInt64);
Casey Dahlin08bb1452015-10-19 18:12:18 -0700850}
851
852status_t Parcel::writeFloatVector(const std::vector<float>& val)
853{
Casey Dahlin32d29a52015-11-13 13:46:29 -0800854 return writeTypedVector(val, this, &Parcel::writeFloat);
Casey Dahlin08bb1452015-10-19 18:12:18 -0700855}
856
857status_t Parcel::writeDoubleVector(const std::vector<double>& val)
858{
Casey Dahlin32d29a52015-11-13 13:46:29 -0800859 return writeTypedVector(val, this, &Parcel::writeDouble);
Casey Dahlin08bb1452015-10-19 18:12:18 -0700860}
861
862status_t Parcel::writeBoolVector(const std::vector<bool>& val)
863{
Casey Dahlin32d29a52015-11-13 13:46:29 -0800864 return writeTypedVector(val, this, &Parcel::writeBool);
Casey Dahlin08bb1452015-10-19 18:12:18 -0700865}
866
867status_t Parcel::writeCharVector(const std::vector<char16_t>& val)
868{
Casey Dahlin32d29a52015-11-13 13:46:29 -0800869 return writeTypedVector(val, this, &Parcel::writeChar);
Casey Dahlin08bb1452015-10-19 18:12:18 -0700870}
871
872status_t Parcel::writeString16Vector(const std::vector<String16>& val)
873{
Casey Dahlin32d29a52015-11-13 13:46:29 -0800874 return writeTypedVector(val, this, &Parcel::writeString16);
Casey Dahlin08bb1452015-10-19 18:12:18 -0700875}
876
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700877status_t Parcel::writeInt32(int32_t val)
878{
Andreas Huber84a6d042009-08-17 13:33:27 -0700879 return writeAligned(val);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700880}
Dan Stoza41a0f2f2014-12-01 10:01:10 -0800881
882status_t Parcel::writeUint32(uint32_t val)
883{
884 return writeAligned(val);
885}
886
Marco Nelissen5c0106e2013-10-16 10:57:51 -0700887status_t Parcel::writeInt32Array(size_t len, const int32_t *val) {
Nick Kralevichb6b14232015-04-02 09:36:02 -0700888 if (len > INT32_MAX) {
889 // don't accept size_t values which may have come from an
890 // inadvertent conversion from a negative int.
891 return BAD_VALUE;
892 }
893
Marco Nelissen5c0106e2013-10-16 10:57:51 -0700894 if (!val) {
Chad Brubakere59cb432015-06-30 14:03:55 -0700895 return writeInt32(-1);
Marco Nelissen5c0106e2013-10-16 10:57:51 -0700896 }
Chad Brubakere59cb432015-06-30 14:03:55 -0700897 status_t ret = writeInt32(static_cast<uint32_t>(len));
Marco Nelissen5c0106e2013-10-16 10:57:51 -0700898 if (ret == NO_ERROR) {
899 ret = write(val, len * sizeof(*val));
900 }
901 return ret;
902}
Marco Nelissenf0190bf2014-03-13 14:17:40 -0700903status_t Parcel::writeByteArray(size_t len, const uint8_t *val) {
Nick Kralevichb6b14232015-04-02 09:36:02 -0700904 if (len > INT32_MAX) {
905 // don't accept size_t values which may have come from an
906 // inadvertent conversion from a negative int.
907 return BAD_VALUE;
908 }
909
Marco Nelissenf0190bf2014-03-13 14:17:40 -0700910 if (!val) {
Chad Brubakere59cb432015-06-30 14:03:55 -0700911 return writeInt32(-1);
Marco Nelissenf0190bf2014-03-13 14:17:40 -0700912 }
Chad Brubakere59cb432015-06-30 14:03:55 -0700913 status_t ret = writeInt32(static_cast<uint32_t>(len));
Marco Nelissenf0190bf2014-03-13 14:17:40 -0700914 if (ret == NO_ERROR) {
915 ret = write(val, len * sizeof(*val));
916 }
917 return ret;
918}
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700919
Casey Dahlina3774912015-10-15 15:44:59 -0700920status_t Parcel::writeBool(bool val)
921{
922 return writeInt32(int32_t(val));
923}
924
925status_t Parcel::writeChar(char16_t val)
926{
927 return writeInt32(int32_t(val));
928}
929
930status_t Parcel::writeByte(int8_t val)
931{
932 return writeInt32(int32_t(val));
933}
934
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700935status_t Parcel::writeInt64(int64_t val)
936{
Andreas Huber84a6d042009-08-17 13:33:27 -0700937 return writeAligned(val);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700938}
939
Ronghua Wu2d13afd2015-03-16 11:11:07 -0700940status_t Parcel::writeUint64(uint64_t val)
941{
942 return writeAligned(val);
943}
944
Serban Constantinescuf683e012013-11-05 16:53:55 +0000945status_t Parcel::writePointer(uintptr_t val)
946{
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800947 return writeAligned<binder_uintptr_t>(val);
Serban Constantinescuf683e012013-11-05 16:53:55 +0000948}
949
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700950status_t Parcel::writeFloat(float val)
951{
Andreas Huber84a6d042009-08-17 13:33:27 -0700952 return writeAligned(val);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700953}
954
Douglas Leungcc1a4bb2013-01-11 15:00:55 -0800955#if defined(__mips__) && defined(__mips_hard_float)
956
957status_t Parcel::writeDouble(double val)
958{
959 union {
960 double d;
961 unsigned long long ll;
962 } u;
963 u.d = val;
964 return writeAligned(u.ll);
965}
966
967#else
968
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700969status_t Parcel::writeDouble(double val)
970{
Andreas Huber84a6d042009-08-17 13:33:27 -0700971 return writeAligned(val);
972}
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700973
Douglas Leungcc1a4bb2013-01-11 15:00:55 -0800974#endif
975
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700976status_t Parcel::writeCString(const char* str)
977{
978 return write(str, strlen(str)+1);
979}
980
981status_t Parcel::writeString8(const String8& str)
982{
983 status_t err = writeInt32(str.bytes());
Pravat Dalbeherad1dff8d2010-12-15 08:40:00 +0100984 // only write string if its length is more than zero characters,
985 // as readString8 will only read if the length field is non-zero.
986 // this is slightly different from how writeString16 works.
987 if (str.bytes() > 0 && err == NO_ERROR) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700988 err = write(str.string(), str.bytes()+1);
989 }
990 return err;
991}
992
993status_t Parcel::writeString16(const String16& str)
994{
995 return writeString16(str.string(), str.size());
996}
997
998status_t Parcel::writeString16(const char16_t* str, size_t len)
999{
1000 if (str == NULL) return writeInt32(-1);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001001
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001002 status_t err = writeInt32(len);
1003 if (err == NO_ERROR) {
1004 len *= sizeof(char16_t);
1005 uint8_t* data = (uint8_t*)writeInplace(len+sizeof(char16_t));
1006 if (data) {
1007 memcpy(data, str, len);
1008 *reinterpret_cast<char16_t*>(data+len) = 0;
1009 return NO_ERROR;
1010 }
1011 err = mError;
1012 }
1013 return err;
1014}
1015
1016status_t Parcel::writeStrongBinder(const sp<IBinder>& val)
1017{
1018 return flatten_binder(ProcessState::self(), val, this);
1019}
1020
Casey Dahlin3e4b6d22015-11-03 13:50:37 -08001021status_t Parcel::writeStrongBinderVector(const std::vector<sp<IBinder>>& val)
1022{
Casey Dahlin32d29a52015-11-13 13:46:29 -08001023 return writeTypedVector(val, this, &Parcel::writeStrongBinder);
Casey Dahlin3e4b6d22015-11-03 13:50:37 -08001024}
1025
1026status_t Parcel::readStrongBinderVector(std::vector<sp<IBinder>>* val) const {
Christopher Wiley6264c4c2015-11-10 09:44:30 -08001027 return readTypedVector(val, this, &Parcel::readStrongBinder);
Casey Dahlin3e4b6d22015-11-03 13:50:37 -08001028}
1029
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001030status_t Parcel::writeWeakBinder(const wp<IBinder>& val)
1031{
1032 return flatten_binder(ProcessState::self(), val, this);
1033}
1034
Mathias Agopiana47f02a2009-05-21 16:29:38 -07001035status_t Parcel::writeNativeHandle(const native_handle* handle)
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001036{
Mathias Agopian1d0a95b2009-07-31 16:12:13 -07001037 if (!handle || handle->version != sizeof(native_handle))
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001038 return BAD_TYPE;
1039
1040 status_t err;
Mathias Agopiana47f02a2009-05-21 16:29:38 -07001041 err = writeInt32(handle->numFds);
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001042 if (err != NO_ERROR) return err;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001043
Mathias Agopiana47f02a2009-05-21 16:29:38 -07001044 err = writeInt32(handle->numInts);
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001045 if (err != NO_ERROR) return err;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001046
Mathias Agopiana47f02a2009-05-21 16:29:38 -07001047 for (int i=0 ; err==NO_ERROR && i<handle->numFds ; i++)
1048 err = writeDupFileDescriptor(handle->data[i]);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001049
1050 if (err != NO_ERROR) {
Steve Block9d453682011-12-20 16:23:08 +00001051 ALOGD("write native handle, write dup fd failed");
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001052 return err;
1053 }
Mathias Agopiana47f02a2009-05-21 16:29:38 -07001054 err = write(handle->data + handle->numFds, sizeof(int)*handle->numInts);
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001055 return err;
1056}
1057
Jeff Brown93ff1f92011-11-04 19:01:44 -07001058status_t Parcel::writeFileDescriptor(int fd, bool takeOwnership)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001059{
1060 flat_binder_object obj;
1061 obj.type = BINDER_TYPE_FD;
1062 obj.flags = 0x7f | FLAT_BINDER_FLAG_ACCEPTS_FDS;
Arve Hjønnevåg07fd0f12014-02-18 21:10:29 -08001063 obj.binder = 0; /* Don't pass uninitialized stack data to a remote process */
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001064 obj.handle = fd;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001065 obj.cookie = takeOwnership ? 1 : 0;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001066 return writeObject(obj, true);
1067}
1068
1069status_t Parcel::writeDupFileDescriptor(int fd)
1070{
Jeff Brownd341c712011-11-04 20:19:33 -07001071 int dupFd = dup(fd);
1072 if (dupFd < 0) {
1073 return -errno;
1074 }
1075 status_t err = writeFileDescriptor(dupFd, true /*takeOwnership*/);
1076 if (err) {
1077 close(dupFd);
1078 }
1079 return err;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001080}
1081
Jeff Brown13b16042014-11-11 16:44:25 -08001082status_t Parcel::writeBlob(size_t len, bool mutableCopy, WritableBlob* outBlob)
Jeff Brown5707dbf2011-09-23 21:17:56 -07001083{
Nick Kralevichb6b14232015-04-02 09:36:02 -07001084 if (len > INT32_MAX) {
1085 // don't accept size_t values which may have come from an
1086 // inadvertent conversion from a negative int.
1087 return BAD_VALUE;
1088 }
1089
Jeff Brown13b16042014-11-11 16:44:25 -08001090 status_t status;
1091 if (!mAllowFds || len <= BLOB_INPLACE_LIMIT) {
Steve Block6807e592011-10-20 11:56:00 +01001092 ALOGV("writeBlob: write in place");
Jeff Brown13b16042014-11-11 16:44:25 -08001093 status = writeInt32(BLOB_INPLACE);
Jeff Brown5707dbf2011-09-23 21:17:56 -07001094 if (status) return status;
1095
1096 void* ptr = writeInplace(len);
1097 if (!ptr) return NO_MEMORY;
1098
Jeff Brown13b16042014-11-11 16:44:25 -08001099 outBlob->init(-1, ptr, len, false);
Jeff Brown5707dbf2011-09-23 21:17:56 -07001100 return NO_ERROR;
1101 }
1102
Steve Block6807e592011-10-20 11:56:00 +01001103 ALOGV("writeBlob: write to ashmem");
Jeff Brown5707dbf2011-09-23 21:17:56 -07001104 int fd = ashmem_create_region("Parcel Blob", len);
1105 if (fd < 0) return NO_MEMORY;
1106
1107 int result = ashmem_set_prot_region(fd, PROT_READ | PROT_WRITE);
1108 if (result < 0) {
Jeff Brownec4e0062011-10-10 14:50:10 -07001109 status = result;
Jeff Brown5707dbf2011-09-23 21:17:56 -07001110 } else {
1111 void* ptr = ::mmap(NULL, len, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
1112 if (ptr == MAP_FAILED) {
1113 status = -errno;
1114 } else {
Jeff Brown13b16042014-11-11 16:44:25 -08001115 if (!mutableCopy) {
1116 result = ashmem_set_prot_region(fd, PROT_READ);
1117 }
Jeff Brown5707dbf2011-09-23 21:17:56 -07001118 if (result < 0) {
Jeff Brownec4e0062011-10-10 14:50:10 -07001119 status = result;
Jeff Brown5707dbf2011-09-23 21:17:56 -07001120 } else {
Jeff Brown13b16042014-11-11 16:44:25 -08001121 status = writeInt32(mutableCopy ? BLOB_ASHMEM_MUTABLE : BLOB_ASHMEM_IMMUTABLE);
Jeff Brown5707dbf2011-09-23 21:17:56 -07001122 if (!status) {
Jeff Brown93ff1f92011-11-04 19:01:44 -07001123 status = writeFileDescriptor(fd, true /*takeOwnership*/);
Jeff Brown5707dbf2011-09-23 21:17:56 -07001124 if (!status) {
Jeff Brown13b16042014-11-11 16:44:25 -08001125 outBlob->init(fd, ptr, len, mutableCopy);
Jeff Brown5707dbf2011-09-23 21:17:56 -07001126 return NO_ERROR;
1127 }
1128 }
1129 }
1130 }
1131 ::munmap(ptr, len);
1132 }
1133 ::close(fd);
1134 return status;
1135}
1136
Jeff Brown13b16042014-11-11 16:44:25 -08001137status_t Parcel::writeDupImmutableBlobFileDescriptor(int fd)
1138{
1139 // Must match up with what's done in writeBlob.
1140 if (!mAllowFds) return FDS_NOT_ALLOWED;
1141 status_t status = writeInt32(BLOB_ASHMEM_IMMUTABLE);
1142 if (status) return status;
1143 return writeDupFileDescriptor(fd);
1144}
1145
Mathias Agopiane1424282013-07-29 21:24:40 -07001146status_t Parcel::write(const FlattenableHelperInterface& val)
Mathias Agopian98e71dd2010-02-11 17:30:52 -08001147{
1148 status_t err;
1149
1150 // size if needed
Mathias Agopiane1424282013-07-29 21:24:40 -07001151 const size_t len = val.getFlattenedSize();
1152 const size_t fd_count = val.getFdCount();
Mathias Agopian98e71dd2010-02-11 17:30:52 -08001153
Nick Kralevichb6b14232015-04-02 09:36:02 -07001154 if ((len > INT32_MAX) || (fd_count > INT32_MAX)) {
1155 // don't accept size_t values which may have come from an
1156 // inadvertent conversion from a negative int.
1157 return BAD_VALUE;
1158 }
1159
Mathias Agopian98e71dd2010-02-11 17:30:52 -08001160 err = this->writeInt32(len);
1161 if (err) return err;
1162
1163 err = this->writeInt32(fd_count);
1164 if (err) return err;
1165
1166 // payload
Nick Kralevichb6b14232015-04-02 09:36:02 -07001167 void* const buf = this->writeInplace(pad_size(len));
Mathias Agopian98e71dd2010-02-11 17:30:52 -08001168 if (buf == NULL)
1169 return BAD_VALUE;
1170
1171 int* fds = NULL;
1172 if (fd_count) {
1173 fds = new int[fd_count];
1174 }
1175
1176 err = val.flatten(buf, len, fds, fd_count);
1177 for (size_t i=0 ; i<fd_count && err==NO_ERROR ; i++) {
1178 err = this->writeDupFileDescriptor( fds[i] );
1179 }
1180
1181 if (fd_count) {
1182 delete [] fds;
1183 }
1184
1185 return err;
1186}
1187
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001188status_t Parcel::writeObject(const flat_binder_object& val, bool nullMetaData)
1189{
1190 const bool enoughData = (mDataPos+sizeof(val)) <= mDataCapacity;
1191 const bool enoughObjects = mObjectsSize < mObjectsCapacity;
1192 if (enoughData && enoughObjects) {
1193restart_write:
1194 *reinterpret_cast<flat_binder_object*>(mData+mDataPos) = val;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001195
Christopher Tate98e67d32015-06-03 18:44:15 -07001196 // remember if it's a file descriptor
1197 if (val.type == BINDER_TYPE_FD) {
1198 if (!mAllowFds) {
1199 // fail before modifying our object index
1200 return FDS_NOT_ALLOWED;
1201 }
1202 mHasFds = mFdsKnown = true;
1203 }
1204
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001205 // Need to write meta-data?
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001206 if (nullMetaData || val.binder != 0) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001207 mObjects[mObjectsSize] = mDataPos;
Adrian Rooscbf37262015-10-22 16:12:53 -07001208 acquire_object(ProcessState::self(), val, this, &mOpenAshmemSize);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001209 mObjectsSize++;
1210 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001211
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001212 return finishWrite(sizeof(flat_binder_object));
1213 }
1214
1215 if (!enoughData) {
1216 const status_t err = growData(sizeof(val));
1217 if (err != NO_ERROR) return err;
1218 }
1219 if (!enoughObjects) {
1220 size_t newSize = ((mObjectsSize+2)*3)/2;
Christopher Tateed7a50c2015-06-08 14:45:14 -07001221 if (newSize < mObjectsSize) return NO_MEMORY; // overflow
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001222 binder_size_t* objects = (binder_size_t*)realloc(mObjects, newSize*sizeof(binder_size_t));
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001223 if (objects == NULL) return NO_MEMORY;
1224 mObjects = objects;
1225 mObjectsCapacity = newSize;
1226 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001227
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001228 goto restart_write;
1229}
1230
Brad Fitzpatrick837a0d02010-07-13 15:33:35 -07001231status_t Parcel::writeNoException()
1232{
1233 return writeInt32(0);
1234}
1235
Colin Cross6f4f3ab2014-02-05 17:42:44 -08001236void Parcel::remove(size_t /*start*/, size_t /*amt*/)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001237{
1238 LOG_ALWAYS_FATAL("Parcel::remove() not yet implemented!");
1239}
1240
1241status_t Parcel::read(void* outData, size_t len) const
1242{
Nick Kralevichb6b14232015-04-02 09:36:02 -07001243 if (len > INT32_MAX) {
1244 // don't accept size_t values which may have come from an
1245 // inadvertent conversion from a negative int.
1246 return BAD_VALUE;
1247 }
1248
1249 if ((mDataPos+pad_size(len)) >= mDataPos && (mDataPos+pad_size(len)) <= mDataSize
1250 && len <= pad_size(len)) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001251 memcpy(outData, mData+mDataPos, len);
Nick Kralevichb6b14232015-04-02 09:36:02 -07001252 mDataPos += pad_size(len);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001253 ALOGV("read Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001254 return NO_ERROR;
1255 }
1256 return NOT_ENOUGH_DATA;
1257}
1258
1259const void* Parcel::readInplace(size_t len) const
1260{
Nick Kralevichb6b14232015-04-02 09:36:02 -07001261 if (len > INT32_MAX) {
1262 // don't accept size_t values which may have come from an
1263 // inadvertent conversion from a negative int.
1264 return NULL;
1265 }
1266
1267 if ((mDataPos+pad_size(len)) >= mDataPos && (mDataPos+pad_size(len)) <= mDataSize
1268 && len <= pad_size(len)) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001269 const void* data = mData+mDataPos;
Nick Kralevichb6b14232015-04-02 09:36:02 -07001270 mDataPos += pad_size(len);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001271 ALOGV("readInplace Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001272 return data;
1273 }
1274 return NULL;
1275}
1276
Andreas Huber84a6d042009-08-17 13:33:27 -07001277template<class T>
1278status_t Parcel::readAligned(T *pArg) const {
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(T)) <= mDataSize) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001282 const void* data = mData+mDataPos;
Andreas Huber84a6d042009-08-17 13:33:27 -07001283 mDataPos += sizeof(T);
1284 *pArg = *reinterpret_cast<const T*>(data);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001285 return NO_ERROR;
1286 } else {
1287 return NOT_ENOUGH_DATA;
1288 }
1289}
1290
Andreas Huber84a6d042009-08-17 13:33:27 -07001291template<class T>
1292T Parcel::readAligned() const {
1293 T result;
1294 if (readAligned(&result) != NO_ERROR) {
1295 result = 0;
1296 }
1297
1298 return result;
1299}
1300
1301template<class T>
1302status_t Parcel::writeAligned(T val) {
Nick Kralevichb6b14232015-04-02 09:36:02 -07001303 COMPILE_TIME_ASSERT_FUNCTION_SCOPE(PAD_SIZE_UNSAFE(sizeof(T)) == sizeof(T));
Andreas Huber84a6d042009-08-17 13:33:27 -07001304
1305 if ((mDataPos+sizeof(val)) <= mDataCapacity) {
1306restart_write:
1307 *reinterpret_cast<T*>(mData+mDataPos) = val;
1308 return finishWrite(sizeof(val));
1309 }
1310
1311 status_t err = growData(sizeof(val));
1312 if (err == NO_ERROR) goto restart_write;
1313 return err;
1314}
1315
Casey Dahlin08bb1452015-10-19 18:12:18 -07001316status_t Parcel::readByteVector(std::vector<int8_t>* val) const {
1317 val->clear();
1318
1319 int32_t size;
1320 status_t status = readInt32(&size);
1321
1322 if (status != OK) {
1323 return status;
1324 }
1325
Christopher Wiley6264c4c2015-11-10 09:44:30 -08001326 if (size < 0) {
1327 status = UNEXPECTED_NULL;
1328 return status;
1329 }
1330 if (size_t(size) > dataAvail()) {
Christopher Wiley714a3ab2015-10-31 13:22:15 -07001331 status = BAD_VALUE;
1332 return status;
Casey Dahlin08bb1452015-10-19 18:12:18 -07001333 }
Christopher Wiley6264c4c2015-11-10 09:44:30 -08001334
Christopher Wiley714a3ab2015-10-31 13:22:15 -07001335 const void* data = readInplace(size);
1336 if (!data) {
1337 status = BAD_VALUE;
1338 return status;
1339 }
Casey Dahlin08bb1452015-10-19 18:12:18 -07001340 val->resize(size);
Christopher Wiley714a3ab2015-10-31 13:22:15 -07001341 memcpy(val->data(), data, size);
Casey Dahlin08bb1452015-10-19 18:12:18 -07001342
Christopher Wiley714a3ab2015-10-31 13:22:15 -07001343 return status;
Casey Dahlin08bb1452015-10-19 18:12:18 -07001344}
1345
1346status_t Parcel::readInt32Vector(std::vector<int32_t>* val) const {
Christopher Wiley6264c4c2015-11-10 09:44:30 -08001347 return readTypedVector(val, this, &Parcel::readInt32);
Casey Dahlin08bb1452015-10-19 18:12:18 -07001348}
1349
1350status_t Parcel::readInt64Vector(std::vector<int64_t>* val) const {
Christopher Wiley6264c4c2015-11-10 09:44:30 -08001351 return readTypedVector(val, this, &Parcel::readInt64);
Casey Dahlin08bb1452015-10-19 18:12:18 -07001352}
1353
1354status_t Parcel::readFloatVector(std::vector<float>* val) const {
Christopher Wiley6264c4c2015-11-10 09:44:30 -08001355 return readTypedVector(val, this, &Parcel::readFloat);
Casey Dahlin08bb1452015-10-19 18:12:18 -07001356}
1357
1358status_t Parcel::readDoubleVector(std::vector<double>* val) const {
Christopher Wiley6264c4c2015-11-10 09:44:30 -08001359 return readTypedVector(val, this, &Parcel::readDouble);
Casey Dahlin08bb1452015-10-19 18:12:18 -07001360}
1361
1362status_t Parcel::readBoolVector(std::vector<bool>* val) const {
1363 val->clear();
1364
1365 int32_t size;
1366 status_t status = readInt32(&size);
1367
1368 if (status != OK) {
1369 return status;
1370 }
1371
1372 if (size < 0) {
Christopher Wiley6264c4c2015-11-10 09:44:30 -08001373 return UNEXPECTED_NULL;
Casey Dahlin08bb1452015-10-19 18:12:18 -07001374 }
1375
1376 val->resize(size);
1377
1378 /* C++ bool handling means a vector of bools isn't necessarily addressable
1379 * (we might use individual bits)
1380 */
Christopher Wiley489931b2015-10-27 16:33:47 -07001381 bool data;
1382 for (int32_t i = 0; i < size; ++i) {
Casey Dahlin08bb1452015-10-19 18:12:18 -07001383 status = readBool(&data);
1384 (*val)[i] = data;
1385
1386 if (status != OK) {
1387 return status;
1388 }
1389 }
1390
1391 return OK;
1392}
1393
1394status_t Parcel::readCharVector(std::vector<char16_t>* val) const {
Christopher Wiley6264c4c2015-11-10 09:44:30 -08001395 return readTypedVector(val, this, &Parcel::readChar);
Casey Dahlin08bb1452015-10-19 18:12:18 -07001396}
1397
1398status_t Parcel::readString16Vector(std::vector<String16>* val) const {
Christopher Wiley6264c4c2015-11-10 09:44:30 -08001399 return readTypedVector(val, this, &Parcel::readString16);
Casey Dahlin08bb1452015-10-19 18:12:18 -07001400}
1401
1402
Andreas Huber84a6d042009-08-17 13:33:27 -07001403status_t Parcel::readInt32(int32_t *pArg) const
1404{
1405 return readAligned(pArg);
1406}
1407
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001408int32_t Parcel::readInt32() const
1409{
Andreas Huber84a6d042009-08-17 13:33:27 -07001410 return readAligned<int32_t>();
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001411}
1412
Dan Stoza41a0f2f2014-12-01 10:01:10 -08001413status_t Parcel::readUint32(uint32_t *pArg) const
1414{
1415 return readAligned(pArg);
1416}
1417
1418uint32_t Parcel::readUint32() const
1419{
1420 return readAligned<uint32_t>();
1421}
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001422
1423status_t Parcel::readInt64(int64_t *pArg) const
1424{
Andreas Huber84a6d042009-08-17 13:33:27 -07001425 return readAligned(pArg);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001426}
1427
1428
1429int64_t Parcel::readInt64() const
1430{
Andreas Huber84a6d042009-08-17 13:33:27 -07001431 return readAligned<int64_t>();
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001432}
1433
Ronghua Wu2d13afd2015-03-16 11:11:07 -07001434status_t Parcel::readUint64(uint64_t *pArg) const
1435{
1436 return readAligned(pArg);
1437}
1438
1439uint64_t Parcel::readUint64() const
1440{
1441 return readAligned<uint64_t>();
1442}
1443
Serban Constantinescuf683e012013-11-05 16:53:55 +00001444status_t Parcel::readPointer(uintptr_t *pArg) const
1445{
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001446 status_t ret;
1447 binder_uintptr_t ptr;
1448 ret = readAligned(&ptr);
1449 if (!ret)
1450 *pArg = ptr;
1451 return ret;
Serban Constantinescuf683e012013-11-05 16:53:55 +00001452}
1453
1454uintptr_t Parcel::readPointer() const
1455{
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001456 return readAligned<binder_uintptr_t>();
Serban Constantinescuf683e012013-11-05 16:53:55 +00001457}
1458
1459
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001460status_t Parcel::readFloat(float *pArg) const
1461{
Andreas Huber84a6d042009-08-17 13:33:27 -07001462 return readAligned(pArg);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001463}
1464
1465
1466float Parcel::readFloat() const
1467{
Andreas Huber84a6d042009-08-17 13:33:27 -07001468 return readAligned<float>();
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001469}
1470
Douglas Leungcc1a4bb2013-01-11 15:00:55 -08001471#if defined(__mips__) && defined(__mips_hard_float)
1472
1473status_t Parcel::readDouble(double *pArg) const
1474{
1475 union {
1476 double d;
1477 unsigned long long ll;
1478 } u;
Narayan Kamath2c68d382014-06-04 15:04:29 +01001479 u.d = 0;
Douglas Leungcc1a4bb2013-01-11 15:00:55 -08001480 status_t status;
1481 status = readAligned(&u.ll);
1482 *pArg = u.d;
1483 return status;
1484}
1485
1486double Parcel::readDouble() const
1487{
1488 union {
1489 double d;
1490 unsigned long long ll;
1491 } u;
1492 u.ll = readAligned<unsigned long long>();
1493 return u.d;
1494}
1495
1496#else
1497
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001498status_t Parcel::readDouble(double *pArg) const
1499{
Andreas Huber84a6d042009-08-17 13:33:27 -07001500 return readAligned(pArg);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001501}
1502
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001503double Parcel::readDouble() const
1504{
Andreas Huber84a6d042009-08-17 13:33:27 -07001505 return readAligned<double>();
1506}
1507
Douglas Leungcc1a4bb2013-01-11 15:00:55 -08001508#endif
1509
Andreas Huber84a6d042009-08-17 13:33:27 -07001510status_t Parcel::readIntPtr(intptr_t *pArg) const
1511{
1512 return readAligned(pArg);
1513}
1514
1515
1516intptr_t Parcel::readIntPtr() const
1517{
1518 return readAligned<intptr_t>();
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001519}
1520
Casey Dahlina3774912015-10-15 15:44:59 -07001521status_t Parcel::readBool(bool *pArg) const
1522{
1523 int32_t tmp;
1524 status_t ret = readInt32(&tmp);
1525 *pArg = (tmp != 0);
1526 return ret;
1527}
1528
1529bool Parcel::readBool() const
1530{
1531 return readInt32() != 0;
1532}
1533
1534status_t Parcel::readChar(char16_t *pArg) const
1535{
1536 int32_t tmp;
1537 status_t ret = readInt32(&tmp);
1538 *pArg = char16_t(tmp);
1539 return ret;
1540}
1541
1542char16_t Parcel::readChar() const
1543{
1544 return char16_t(readInt32());
1545}
1546
1547status_t Parcel::readByte(int8_t *pArg) const
1548{
1549 int32_t tmp;
1550 status_t ret = readInt32(&tmp);
1551 *pArg = int8_t(tmp);
1552 return ret;
1553}
1554
1555int8_t Parcel::readByte() const
1556{
1557 return int8_t(readInt32());
1558}
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001559
1560const char* Parcel::readCString() const
1561{
1562 const size_t avail = mDataSize-mDataPos;
1563 if (avail > 0) {
1564 const char* str = reinterpret_cast<const char*>(mData+mDataPos);
1565 // is the string's trailing NUL within the parcel's valid bounds?
1566 const char* eos = reinterpret_cast<const char*>(memchr(str, 0, avail));
1567 if (eos) {
1568 const size_t len = eos - str;
Nick Kralevichb6b14232015-04-02 09:36:02 -07001569 mDataPos += pad_size(len+1);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001570 ALOGV("readCString Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001571 return str;
1572 }
1573 }
1574 return NULL;
1575}
1576
1577String8 Parcel::readString8() const
1578{
1579 int32_t size = readInt32();
1580 // watch for potential int overflow adding 1 for trailing NUL
1581 if (size > 0 && size < INT32_MAX) {
1582 const char* str = (const char*)readInplace(size+1);
1583 if (str) return String8(str, size);
1584 }
1585 return String8();
1586}
1587
1588String16 Parcel::readString16() const
1589{
1590 size_t len;
1591 const char16_t* str = readString16Inplace(&len);
1592 if (str) return String16(str, len);
Steve Blocke6f43dd2012-01-06 19:20:56 +00001593 ALOGE("Reading a NULL string not supported here.");
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001594 return String16();
1595}
1596
Casey Dahlin08bb1452015-10-19 18:12:18 -07001597status_t Parcel::readString16(String16* pArg) const
1598{
1599 size_t len;
1600 const char16_t* str = readString16Inplace(&len);
1601 if (str) {
Casey Dahlina5bcc952015-10-20 16:26:23 -07001602 pArg->setTo(str, len);
Casey Dahlin08bb1452015-10-19 18:12:18 -07001603 return 0;
1604 } else {
1605 *pArg = String16();
Christopher Wiley6264c4c2015-11-10 09:44:30 -08001606 return UNEXPECTED_NULL;
Casey Dahlin08bb1452015-10-19 18:12:18 -07001607 }
1608}
1609
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001610const char16_t* Parcel::readString16Inplace(size_t* outLen) const
1611{
1612 int32_t size = readInt32();
1613 // watch for potential int overflow from size+1
1614 if (size >= 0 && size < INT32_MAX) {
1615 *outLen = size;
1616 const char16_t* str = (const char16_t*)readInplace((size+1)*sizeof(char16_t));
1617 if (str != NULL) {
1618 return str;
1619 }
1620 }
1621 *outLen = 0;
1622 return NULL;
1623}
1624
Casey Dahlin930a5722015-10-27 18:33:56 -07001625status_t Parcel::readStrongBinder(sp<IBinder>* val) const
1626{
1627 return unflatten_binder(ProcessState::self(), *this, val);
1628}
1629
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001630sp<IBinder> Parcel::readStrongBinder() const
1631{
1632 sp<IBinder> val;
Casey Dahlin930a5722015-10-27 18:33:56 -07001633 readStrongBinder(&val);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001634 return val;
1635}
1636
1637wp<IBinder> Parcel::readWeakBinder() const
1638{
1639 wp<IBinder> val;
1640 unflatten_binder(ProcessState::self(), *this, &val);
1641 return val;
1642}
1643
Brad Fitzpatrick837a0d02010-07-13 15:33:35 -07001644int32_t Parcel::readExceptionCode() const
1645{
1646 int32_t exception_code = readAligned<int32_t>();
Brad Fitzpatrickd36f4a52010-07-12 11:05:38 -07001647 if (exception_code == EX_HAS_REPLY_HEADER) {
Magnus Strandberg1ba24572011-05-03 15:44:00 +02001648 int32_t header_start = dataPosition();
Brad Fitzpatrickd36f4a52010-07-12 11:05:38 -07001649 int32_t header_size = readAligned<int32_t>();
1650 // Skip over fat responses headers. Not used (or propagated) in
1651 // native code
Magnus Strandberg1ba24572011-05-03 15:44:00 +02001652 setDataPosition(header_start + header_size);
Brad Fitzpatrickd36f4a52010-07-12 11:05:38 -07001653 // And fat response headers are currently only used when there are no
1654 // exceptions, so return no error:
1655 return 0;
1656 }
Brad Fitzpatrick837a0d02010-07-13 15:33:35 -07001657 return exception_code;
1658}
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001659
Mathias Agopiana47f02a2009-05-21 16:29:38 -07001660native_handle* Parcel::readNativeHandle() const
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001661{
1662 int numFds, numInts;
1663 status_t err;
1664 err = readInt32(&numFds);
1665 if (err != NO_ERROR) return 0;
1666 err = readInt32(&numInts);
1667 if (err != NO_ERROR) return 0;
1668
Mathias Agopiana47f02a2009-05-21 16:29:38 -07001669 native_handle* h = native_handle_create(numFds, numInts);
Adam Lesinskieaac99a2015-05-12 17:35:48 -07001670 if (!h) {
1671 return 0;
1672 }
1673
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001674 for (int i=0 ; err==NO_ERROR && i<numFds ; i++) {
Rebecca Schultz Zavin360211f2009-02-13 16:34:38 -08001675 h->data[i] = dup(readFileDescriptor());
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001676 if (h->data[i] < 0) err = BAD_VALUE;
1677 }
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001678 err = read(h->data + numFds, sizeof(int)*numInts);
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001679 if (err != NO_ERROR) {
Mathias Agopiana47f02a2009-05-21 16:29:38 -07001680 native_handle_close(h);
1681 native_handle_delete(h);
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001682 h = 0;
1683 }
1684 return h;
1685}
1686
1687
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001688int Parcel::readFileDescriptor() const
1689{
1690 const flat_binder_object* flat = readObject(true);
1691 if (flat) {
1692 switch (flat->type) {
1693 case BINDER_TYPE_FD:
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001694 //ALOGI("Returning file descriptor %ld from parcel %p", flat->handle, this);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001695 return flat->handle;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001696 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001697 }
1698 return BAD_TYPE;
1699}
1700
Jeff Brown5707dbf2011-09-23 21:17:56 -07001701status_t Parcel::readBlob(size_t len, ReadableBlob* outBlob) const
1702{
Jeff Brown13b16042014-11-11 16:44:25 -08001703 int32_t blobType;
1704 status_t status = readInt32(&blobType);
Jeff Brown5707dbf2011-09-23 21:17:56 -07001705 if (status) return status;
1706
Jeff Brown13b16042014-11-11 16:44:25 -08001707 if (blobType == BLOB_INPLACE) {
Steve Block6807e592011-10-20 11:56:00 +01001708 ALOGV("readBlob: read in place");
Jeff Brown5707dbf2011-09-23 21:17:56 -07001709 const void* ptr = readInplace(len);
1710 if (!ptr) return BAD_VALUE;
1711
Jeff Brown13b16042014-11-11 16:44:25 -08001712 outBlob->init(-1, const_cast<void*>(ptr), len, false);
Jeff Brown5707dbf2011-09-23 21:17:56 -07001713 return NO_ERROR;
1714 }
1715
Steve Block6807e592011-10-20 11:56:00 +01001716 ALOGV("readBlob: read from ashmem");
Jeff Brown13b16042014-11-11 16:44:25 -08001717 bool isMutable = (blobType == BLOB_ASHMEM_MUTABLE);
Jeff Brown5707dbf2011-09-23 21:17:56 -07001718 int fd = readFileDescriptor();
1719 if (fd == int(BAD_TYPE)) return BAD_VALUE;
1720
Jeff Brown13b16042014-11-11 16:44:25 -08001721 void* ptr = ::mmap(NULL, len, isMutable ? PROT_READ | PROT_WRITE : PROT_READ,
1722 MAP_SHARED, fd, 0);
Narayan Kamath9ea09752014-10-08 17:35:45 +01001723 if (ptr == MAP_FAILED) return NO_MEMORY;
Jeff Brown5707dbf2011-09-23 21:17:56 -07001724
Jeff Brown13b16042014-11-11 16:44:25 -08001725 outBlob->init(fd, ptr, len, isMutable);
Jeff Brown5707dbf2011-09-23 21:17:56 -07001726 return NO_ERROR;
1727}
1728
Mathias Agopiane1424282013-07-29 21:24:40 -07001729status_t Parcel::read(FlattenableHelperInterface& val) const
Mathias Agopian98e71dd2010-02-11 17:30:52 -08001730{
1731 // size
1732 const size_t len = this->readInt32();
1733 const size_t fd_count = this->readInt32();
1734
Nick Kralevichb6b14232015-04-02 09:36:02 -07001735 if (len > INT32_MAX) {
1736 // don't accept size_t values which may have come from an
1737 // inadvertent conversion from a negative int.
1738 return BAD_VALUE;
1739 }
1740
Mathias Agopian98e71dd2010-02-11 17:30:52 -08001741 // payload
Nick Kralevichb6b14232015-04-02 09:36:02 -07001742 void const* const buf = this->readInplace(pad_size(len));
Mathias Agopian98e71dd2010-02-11 17:30:52 -08001743 if (buf == NULL)
1744 return BAD_VALUE;
1745
1746 int* fds = NULL;
1747 if (fd_count) {
1748 fds = new int[fd_count];
1749 }
1750
1751 status_t err = NO_ERROR;
1752 for (size_t i=0 ; i<fd_count && err==NO_ERROR ; i++) {
Jesse Hallfee99042014-11-04 08:36:31 -08001753 fds[i] = dup(this->readFileDescriptor());
Jun Jiangabf8a2c2014-04-29 14:22:10 +08001754 if (fds[i] < 0) {
1755 err = BAD_VALUE;
Jesse Hallfee99042014-11-04 08:36:31 -08001756 ALOGE("dup() failed in Parcel::read, i is %zu, fds[i] is %d, fd_count is %zu, error: %s",
1757 i, fds[i], fd_count, strerror(errno));
Jun Jiangabf8a2c2014-04-29 14:22:10 +08001758 }
Mathias Agopian98e71dd2010-02-11 17:30:52 -08001759 }
1760
1761 if (err == NO_ERROR) {
1762 err = val.unflatten(buf, len, fds, fd_count);
1763 }
1764
1765 if (fd_count) {
1766 delete [] fds;
1767 }
1768
1769 return err;
1770}
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001771const flat_binder_object* Parcel::readObject(bool nullMetaData) const
1772{
1773 const size_t DPOS = mDataPos;
1774 if ((DPOS+sizeof(flat_binder_object)) <= mDataSize) {
1775 const flat_binder_object* obj
1776 = reinterpret_cast<const flat_binder_object*>(mData+DPOS);
1777 mDataPos = DPOS + sizeof(flat_binder_object);
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001778 if (!nullMetaData && (obj->cookie == 0 && obj->binder == 0)) {
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001779 // When transferring a NULL object, we don't write it into
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001780 // the object list, so we don't want to check for it when
1781 // reading.
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001782 ALOGV("readObject Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001783 return obj;
1784 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001785
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001786 // Ensure that this object is valid...
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001787 binder_size_t* const OBJS = mObjects;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001788 const size_t N = mObjectsSize;
1789 size_t opos = mNextObjectHint;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001790
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001791 if (N > 0) {
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001792 ALOGV("Parcel %p looking for obj at %zu, hint=%zu",
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001793 this, DPOS, opos);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001794
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001795 // Start at the current hint position, looking for an object at
1796 // the current data position.
1797 if (opos < N) {
1798 while (opos < (N-1) && OBJS[opos] < DPOS) {
1799 opos++;
1800 }
1801 } else {
1802 opos = N-1;
1803 }
1804 if (OBJS[opos] == DPOS) {
1805 // Found it!
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001806 ALOGV("Parcel %p found obj %zu at index %zu with forward search",
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001807 this, DPOS, opos);
1808 mNextObjectHint = opos+1;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001809 ALOGV("readObject Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001810 return obj;
1811 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001812
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001813 // Look backwards for it...
1814 while (opos > 0 && OBJS[opos] > DPOS) {
1815 opos--;
1816 }
1817 if (OBJS[opos] == DPOS) {
1818 // Found it!
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001819 ALOGV("Parcel %p found obj %zu at index %zu with backward search",
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001820 this, DPOS, opos);
1821 mNextObjectHint = opos+1;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001822 ALOGV("readObject Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001823 return obj;
1824 }
1825 }
Colin Cross6f4f3ab2014-02-05 17:42:44 -08001826 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 -07001827 this, DPOS);
1828 }
1829 return NULL;
1830}
1831
1832void Parcel::closeFileDescriptors()
1833{
1834 size_t i = mObjectsSize;
1835 if (i > 0) {
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001836 //ALOGI("Closing file descriptors for %zu objects...", i);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001837 }
1838 while (i > 0) {
1839 i--;
1840 const flat_binder_object* flat
1841 = reinterpret_cast<flat_binder_object*>(mData+mObjects[i]);
1842 if (flat->type == BINDER_TYPE_FD) {
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001843 //ALOGI("Closing fd: %ld", flat->handle);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001844 close(flat->handle);
1845 }
1846 }
1847}
1848
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001849uintptr_t Parcel::ipcData() const
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001850{
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001851 return reinterpret_cast<uintptr_t>(mData);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001852}
1853
1854size_t Parcel::ipcDataSize() const
1855{
1856 return (mDataSize > mDataPos ? mDataSize : mDataPos);
1857}
1858
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001859uintptr_t Parcel::ipcObjects() const
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001860{
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001861 return reinterpret_cast<uintptr_t>(mObjects);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001862}
1863
1864size_t Parcel::ipcObjectsCount() const
1865{
1866 return mObjectsSize;
1867}
1868
1869void Parcel::ipcSetDataReference(const uint8_t* data, size_t dataSize,
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001870 const binder_size_t* objects, size_t objectsCount, release_func relFunc, void* relCookie)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001871{
Arve Hjønnevåg6f286112014-02-19 20:42:13 -08001872 binder_size_t minOffset = 0;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001873 freeDataNoInit();
1874 mError = NO_ERROR;
1875 mData = const_cast<uint8_t*>(data);
1876 mDataSize = mDataCapacity = dataSize;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001877 //ALOGI("setDataReference Setting data size of %p to %lu (pid=%d)", this, mDataSize, getpid());
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001878 mDataPos = 0;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001879 ALOGV("setDataReference Setting data pos of %p to %zu", this, mDataPos);
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001880 mObjects = const_cast<binder_size_t*>(objects);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001881 mObjectsSize = mObjectsCapacity = objectsCount;
1882 mNextObjectHint = 0;
1883 mOwner = relFunc;
1884 mOwnerCookie = relCookie;
Arve Hjønnevågf50b9ea2014-02-13 19:22:08 -08001885 for (size_t i = 0; i < mObjectsSize; i++) {
Arve Hjønnevåg6f286112014-02-19 20:42:13 -08001886 binder_size_t offset = mObjects[i];
Arve Hjønnevågf50b9ea2014-02-13 19:22:08 -08001887 if (offset < minOffset) {
Dan Albert3bdc5b82014-11-20 11:50:23 -08001888 ALOGE("%s: bad object offset %" PRIu64 " < %" PRIu64 "\n",
Arve Hjønnevåg6f286112014-02-19 20:42:13 -08001889 __func__, (uint64_t)offset, (uint64_t)minOffset);
Arve Hjønnevågf50b9ea2014-02-13 19:22:08 -08001890 mObjectsSize = 0;
1891 break;
1892 }
1893 minOffset = offset + sizeof(flat_binder_object);
1894 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001895 scanForFds();
1896}
1897
Colin Cross6f4f3ab2014-02-05 17:42:44 -08001898void Parcel::print(TextOutput& to, uint32_t /*flags*/) const
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001899{
1900 to << "Parcel(";
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001901
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001902 if (errorCheck() != NO_ERROR) {
1903 const status_t err = errorCheck();
Colin Cross6f4f3ab2014-02-05 17:42:44 -08001904 to << "Error: " << (void*)(intptr_t)err << " \"" << strerror(-err) << "\"";
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001905 } else if (dataSize() > 0) {
1906 const uint8_t* DATA = data();
1907 to << indent << HexDump(DATA, dataSize()) << dedent;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001908 const binder_size_t* OBJS = objects();
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001909 const size_t N = objectsCount();
1910 for (size_t i=0; i<N; i++) {
1911 const flat_binder_object* flat
1912 = reinterpret_cast<const flat_binder_object*>(DATA+OBJS[i]);
1913 to << endl << "Object #" << i << " @ " << (void*)OBJS[i] << ": "
1914 << TypeCode(flat->type & 0x7f7f7f00)
1915 << " = " << flat->binder;
1916 }
1917 } else {
1918 to << "NULL";
1919 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001920
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001921 to << ")";
1922}
1923
1924void Parcel::releaseObjects()
1925{
1926 const sp<ProcessState> proc(ProcessState::self());
1927 size_t i = mObjectsSize;
1928 uint8_t* const data = mData;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001929 binder_size_t* const objects = mObjects;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001930 while (i > 0) {
1931 i--;
1932 const flat_binder_object* flat
1933 = reinterpret_cast<flat_binder_object*>(data+objects[i]);
Adrian Rooscbf37262015-10-22 16:12:53 -07001934 release_object(proc, *flat, this, &mOpenAshmemSize);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001935 }
1936}
1937
1938void Parcel::acquireObjects()
1939{
1940 const sp<ProcessState> proc(ProcessState::self());
1941 size_t i = mObjectsSize;
1942 uint8_t* const data = mData;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001943 binder_size_t* const objects = mObjects;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001944 while (i > 0) {
1945 i--;
1946 const flat_binder_object* flat
1947 = reinterpret_cast<flat_binder_object*>(data+objects[i]);
Adrian Rooscbf37262015-10-22 16:12:53 -07001948 acquire_object(proc, *flat, this, &mOpenAshmemSize);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001949 }
1950}
1951
1952void Parcel::freeData()
1953{
1954 freeDataNoInit();
1955 initState();
1956}
1957
1958void Parcel::freeDataNoInit()
1959{
1960 if (mOwner) {
Dianne Hackborn7e790af2014-11-11 12:22:53 -08001961 LOG_ALLOC("Parcel %p: freeing other owner data", this);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001962 //ALOGI("Freeing data ref of %p (pid=%d)", this, getpid());
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001963 mOwner(this, mData, mDataSize, mObjects, mObjectsSize, mOwnerCookie);
1964 } else {
Dianne Hackborn7e790af2014-11-11 12:22:53 -08001965 LOG_ALLOC("Parcel %p: freeing allocated data", this);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001966 releaseObjects();
Dianne Hackborn7e790af2014-11-11 12:22:53 -08001967 if (mData) {
1968 LOG_ALLOC("Parcel %p: freeing with %zu capacity", this, mDataCapacity);
Dianne Hackborna4cff882014-11-13 17:07:40 -08001969 pthread_mutex_lock(&gParcelGlobalAllocSizeLock);
Dan Austinf28b2952015-09-10 13:46:02 -07001970 if (mDataCapacity <= gParcelGlobalAllocSize) {
1971 gParcelGlobalAllocSize = gParcelGlobalAllocSize - mDataCapacity;
1972 } else {
1973 gParcelGlobalAllocSize = 0;
1974 }
1975 if (gParcelGlobalAllocCount > 0) {
1976 gParcelGlobalAllocCount--;
1977 }
Dianne Hackborna4cff882014-11-13 17:07:40 -08001978 pthread_mutex_unlock(&gParcelGlobalAllocSizeLock);
Dianne Hackborn7e790af2014-11-11 12:22:53 -08001979 free(mData);
1980 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001981 if (mObjects) free(mObjects);
1982 }
1983}
1984
1985status_t Parcel::growData(size_t len)
1986{
Nick Kralevichb6b14232015-04-02 09:36:02 -07001987 if (len > INT32_MAX) {
1988 // don't accept size_t values which may have come from an
1989 // inadvertent conversion from a negative int.
1990 return BAD_VALUE;
1991 }
1992
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001993 size_t newSize = ((mDataSize+len)*3)/2;
1994 return (newSize <= mDataSize)
1995 ? (status_t) NO_MEMORY
1996 : continueWrite(newSize);
1997}
1998
1999status_t Parcel::restartWrite(size_t desired)
2000{
Nick Kralevichb6b14232015-04-02 09:36:02 -07002001 if (desired > INT32_MAX) {
2002 // don't accept size_t values which may have come from an
2003 // inadvertent conversion from a negative int.
2004 return BAD_VALUE;
2005 }
2006
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002007 if (mOwner) {
2008 freeData();
2009 return continueWrite(desired);
2010 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002011
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002012 uint8_t* data = (uint8_t*)realloc(mData, desired);
2013 if (!data && desired > mDataCapacity) {
2014 mError = NO_MEMORY;
2015 return NO_MEMORY;
2016 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002017
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002018 releaseObjects();
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002019
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002020 if (data) {
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002021 LOG_ALLOC("Parcel %p: restart from %zu to %zu capacity", this, mDataCapacity, desired);
Dianne Hackborna4cff882014-11-13 17:07:40 -08002022 pthread_mutex_lock(&gParcelGlobalAllocSizeLock);
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002023 gParcelGlobalAllocSize += desired;
2024 gParcelGlobalAllocSize -= mDataCapacity;
Dianne Hackborna4cff882014-11-13 17:07:40 -08002025 pthread_mutex_unlock(&gParcelGlobalAllocSizeLock);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002026 mData = data;
2027 mDataCapacity = desired;
2028 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002029
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002030 mDataSize = mDataPos = 0;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002031 ALOGV("restartWrite Setting data size of %p to %zu", this, mDataSize);
2032 ALOGV("restartWrite Setting data pos of %p to %zu", this, mDataPos);
2033
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002034 free(mObjects);
2035 mObjects = NULL;
2036 mObjectsSize = mObjectsCapacity = 0;
2037 mNextObjectHint = 0;
2038 mHasFds = false;
2039 mFdsKnown = true;
Dianne Hackborn8938ed22011-09-28 23:19:47 -04002040 mAllowFds = true;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002041
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002042 return NO_ERROR;
2043}
2044
2045status_t Parcel::continueWrite(size_t desired)
2046{
Nick Kralevichb6b14232015-04-02 09:36:02 -07002047 if (desired > INT32_MAX) {
2048 // don't accept size_t values which may have come from an
2049 // inadvertent conversion from a negative int.
2050 return BAD_VALUE;
2051 }
2052
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002053 // If shrinking, first adjust for any objects that appear
2054 // after the new data size.
2055 size_t objectsSize = mObjectsSize;
2056 if (desired < mDataSize) {
2057 if (desired == 0) {
2058 objectsSize = 0;
2059 } else {
2060 while (objectsSize > 0) {
2061 if (mObjects[objectsSize-1] < desired)
2062 break;
2063 objectsSize--;
2064 }
2065 }
2066 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002067
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002068 if (mOwner) {
2069 // If the size is going to zero, just release the owner's data.
2070 if (desired == 0) {
2071 freeData();
2072 return NO_ERROR;
2073 }
2074
2075 // If there is a different owner, we need to take
2076 // posession.
2077 uint8_t* data = (uint8_t*)malloc(desired);
2078 if (!data) {
2079 mError = NO_MEMORY;
2080 return NO_MEMORY;
2081 }
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002082 binder_size_t* objects = NULL;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002083
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002084 if (objectsSize) {
Nick Kraleviche9881a32015-04-28 16:21:30 -07002085 objects = (binder_size_t*)calloc(objectsSize, sizeof(binder_size_t));
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002086 if (!objects) {
Hyejin Kim3f727c02013-03-09 11:28:54 +09002087 free(data);
2088
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002089 mError = NO_MEMORY;
2090 return NO_MEMORY;
2091 }
2092
2093 // Little hack to only acquire references on objects
2094 // we will be keeping.
2095 size_t oldObjectsSize = mObjectsSize;
2096 mObjectsSize = objectsSize;
2097 acquireObjects();
2098 mObjectsSize = oldObjectsSize;
2099 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002100
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002101 if (mData) {
2102 memcpy(data, mData, mDataSize < desired ? mDataSize : desired);
2103 }
2104 if (objects && mObjects) {
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002105 memcpy(objects, mObjects, objectsSize*sizeof(binder_size_t));
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002106 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002107 //ALOGI("Freeing data ref of %p (pid=%d)", this, getpid());
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002108 mOwner(this, mData, mDataSize, mObjects, mObjectsSize, mOwnerCookie);
2109 mOwner = NULL;
2110
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002111 LOG_ALLOC("Parcel %p: taking ownership of %zu capacity", this, desired);
Dianne Hackborna4cff882014-11-13 17:07:40 -08002112 pthread_mutex_lock(&gParcelGlobalAllocSizeLock);
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002113 gParcelGlobalAllocSize += desired;
2114 gParcelGlobalAllocCount++;
Dianne Hackborna4cff882014-11-13 17:07:40 -08002115 pthread_mutex_unlock(&gParcelGlobalAllocSizeLock);
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002116
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002117 mData = data;
2118 mObjects = objects;
2119 mDataSize = (mDataSize < desired) ? mDataSize : desired;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002120 ALOGV("continueWrite Setting data size of %p to %zu", this, mDataSize);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002121 mDataCapacity = desired;
2122 mObjectsSize = mObjectsCapacity = objectsSize;
2123 mNextObjectHint = 0;
2124
2125 } else if (mData) {
2126 if (objectsSize < mObjectsSize) {
2127 // Need to release refs on any objects we are dropping.
2128 const sp<ProcessState> proc(ProcessState::self());
2129 for (size_t i=objectsSize; i<mObjectsSize; i++) {
2130 const flat_binder_object* flat
2131 = reinterpret_cast<flat_binder_object*>(mData+mObjects[i]);
2132 if (flat->type == BINDER_TYPE_FD) {
2133 // will need to rescan because we may have lopped off the only FDs
2134 mFdsKnown = false;
2135 }
Adrian Rooscbf37262015-10-22 16:12:53 -07002136 release_object(proc, *flat, this, &mOpenAshmemSize);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002137 }
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002138 binder_size_t* objects =
2139 (binder_size_t*)realloc(mObjects, objectsSize*sizeof(binder_size_t));
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002140 if (objects) {
2141 mObjects = objects;
2142 }
2143 mObjectsSize = objectsSize;
2144 mNextObjectHint = 0;
2145 }
2146
2147 // We own the data, so we can just do a realloc().
2148 if (desired > mDataCapacity) {
2149 uint8_t* data = (uint8_t*)realloc(mData, desired);
2150 if (data) {
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002151 LOG_ALLOC("Parcel %p: continue from %zu to %zu capacity", this, mDataCapacity,
2152 desired);
Dianne Hackborna4cff882014-11-13 17:07:40 -08002153 pthread_mutex_lock(&gParcelGlobalAllocSizeLock);
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002154 gParcelGlobalAllocSize += desired;
2155 gParcelGlobalAllocSize -= mDataCapacity;
Dan Austinf28b2952015-09-10 13:46:02 -07002156 gParcelGlobalAllocCount++;
Dianne Hackborna4cff882014-11-13 17:07:40 -08002157 pthread_mutex_unlock(&gParcelGlobalAllocSizeLock);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002158 mData = data;
2159 mDataCapacity = desired;
2160 } else if (desired > mDataCapacity) {
2161 mError = NO_MEMORY;
2162 return NO_MEMORY;
2163 }
2164 } else {
Dianne Hackborn97e2bcd2011-04-13 18:15:56 -07002165 if (mDataSize > desired) {
2166 mDataSize = desired;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002167 ALOGV("continueWrite Setting data size of %p to %zu", this, mDataSize);
Dianne Hackborn97e2bcd2011-04-13 18:15:56 -07002168 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002169 if (mDataPos > desired) {
2170 mDataPos = desired;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002171 ALOGV("continueWrite Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002172 }
2173 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002174
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002175 } else {
2176 // This is the first data. Easy!
2177 uint8_t* data = (uint8_t*)malloc(desired);
2178 if (!data) {
2179 mError = NO_MEMORY;
2180 return NO_MEMORY;
2181 }
Hyejin Kim3f727c02013-03-09 11:28:54 +09002182
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002183 if(!(mDataCapacity == 0 && mObjects == NULL
2184 && mObjectsCapacity == 0)) {
Colin Cross6f4f3ab2014-02-05 17:42:44 -08002185 ALOGE("continueWrite: %zu/%p/%zu/%zu", mDataCapacity, mObjects, mObjectsCapacity, desired);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002186 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002187
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002188 LOG_ALLOC("Parcel %p: allocating with %zu capacity", this, desired);
Dianne Hackborna4cff882014-11-13 17:07:40 -08002189 pthread_mutex_lock(&gParcelGlobalAllocSizeLock);
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002190 gParcelGlobalAllocSize += desired;
2191 gParcelGlobalAllocCount++;
Dianne Hackborna4cff882014-11-13 17:07:40 -08002192 pthread_mutex_unlock(&gParcelGlobalAllocSizeLock);
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002193
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002194 mData = data;
2195 mDataSize = mDataPos = 0;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002196 ALOGV("continueWrite Setting data size of %p to %zu", this, mDataSize);
2197 ALOGV("continueWrite Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002198 mDataCapacity = desired;
2199 }
2200
2201 return NO_ERROR;
2202}
2203
2204void Parcel::initState()
2205{
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002206 LOG_ALLOC("Parcel %p: initState", this);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002207 mError = NO_ERROR;
2208 mData = 0;
2209 mDataSize = 0;
2210 mDataCapacity = 0;
2211 mDataPos = 0;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002212 ALOGV("initState Setting data size of %p to %zu", this, mDataSize);
2213 ALOGV("initState Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002214 mObjects = NULL;
2215 mObjectsSize = 0;
2216 mObjectsCapacity = 0;
2217 mNextObjectHint = 0;
2218 mHasFds = false;
2219 mFdsKnown = true;
Dianne Hackborn8938ed22011-09-28 23:19:47 -04002220 mAllowFds = true;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002221 mOwner = NULL;
Adrian Rooscbf37262015-10-22 16:12:53 -07002222 mOpenAshmemSize = 0;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002223}
2224
2225void Parcel::scanForFds() const
2226{
2227 bool hasFds = false;
2228 for (size_t i=0; i<mObjectsSize; i++) {
2229 const flat_binder_object* flat
2230 = reinterpret_cast<const flat_binder_object*>(mData + mObjects[i]);
2231 if (flat->type == BINDER_TYPE_FD) {
2232 hasFds = true;
2233 break;
2234 }
2235 }
2236 mHasFds = hasFds;
2237 mFdsKnown = true;
2238}
2239
Dan Sandleraa5c2342015-04-10 10:08:45 -04002240size_t Parcel::getBlobAshmemSize() const
2241{
Adrian Roos6bb31142015-10-22 16:46:12 -07002242 // This used to return the size of all blobs that were written to ashmem, now we're returning
2243 // the ashmem currently referenced by this Parcel, which should be equivalent.
2244 // TODO: Remove method once ABI can be changed.
2245 return mOpenAshmemSize;
Dan Sandleraa5c2342015-04-10 10:08:45 -04002246}
2247
Adrian Rooscbf37262015-10-22 16:12:53 -07002248size_t Parcel::getOpenAshmemSize() const
2249{
2250 return mOpenAshmemSize;
2251}
2252
Jeff Brown5707dbf2011-09-23 21:17:56 -07002253// --- Parcel::Blob ---
2254
2255Parcel::Blob::Blob() :
Jeff Brown13b16042014-11-11 16:44:25 -08002256 mFd(-1), mData(NULL), mSize(0), mMutable(false) {
Jeff Brown5707dbf2011-09-23 21:17:56 -07002257}
2258
2259Parcel::Blob::~Blob() {
2260 release();
2261}
2262
2263void Parcel::Blob::release() {
Jeff Brown13b16042014-11-11 16:44:25 -08002264 if (mFd != -1 && mData) {
Jeff Brown5707dbf2011-09-23 21:17:56 -07002265 ::munmap(mData, mSize);
2266 }
2267 clear();
2268}
2269
Jeff Brown13b16042014-11-11 16:44:25 -08002270void Parcel::Blob::init(int fd, void* data, size_t size, bool isMutable) {
2271 mFd = fd;
Jeff Brown5707dbf2011-09-23 21:17:56 -07002272 mData = data;
2273 mSize = size;
Jeff Brown13b16042014-11-11 16:44:25 -08002274 mMutable = isMutable;
Jeff Brown5707dbf2011-09-23 21:17:56 -07002275}
2276
2277void Parcel::Blob::clear() {
Jeff Brown13b16042014-11-11 16:44:25 -08002278 mFd = -1;
Jeff Brown5707dbf2011-09-23 21:17:56 -07002279 mData = NULL;
2280 mSize = 0;
Jeff Brown13b16042014-11-11 16:44:25 -08002281 mMutable = false;
Jeff Brown5707dbf2011-09-23 21:17:56 -07002282}
2283
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002284}; // namespace android