blob: eef14313442ac0d7229450aece7e8f091d86a845 [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 Dahlin08bb1452015-10-19 18:12:18 -0700779status_t Parcel::writeByteVector(const std::vector<int8_t>& val)
780{
Christopher Wiley714a3ab2015-10-31 13:22:15 -0700781 status_t status;
Casey Dahlin08bb1452015-10-19 18:12:18 -0700782 if (val.size() > std::numeric_limits<int32_t>::max()) {
Christopher Wiley714a3ab2015-10-31 13:22:15 -0700783 status = BAD_VALUE;
784 return status;
Casey Dahlin08bb1452015-10-19 18:12:18 -0700785 }
786
Christopher Wiley714a3ab2015-10-31 13:22:15 -0700787 status = writeInt32(val.size());
Casey Dahlin08bb1452015-10-19 18:12:18 -0700788 if (status != OK) {
789 return status;
790 }
791
Christopher Wiley714a3ab2015-10-31 13:22:15 -0700792 void* data = writeInplace(val.size());
793 if (!data) {
794 status = BAD_VALUE;
795 return status;
Casey Dahlin08bb1452015-10-19 18:12:18 -0700796 }
797
Christopher Wiley714a3ab2015-10-31 13:22:15 -0700798 memcpy(data, val.data(), val.size());
799 return status;
Casey Dahlin08bb1452015-10-19 18:12:18 -0700800}
801
802status_t Parcel::writeInt32Vector(const std::vector<int32_t>& val)
803{
804 if (val.size() > std::numeric_limits<int32_t>::max()) {
805 return BAD_VALUE;
806 }
807
808 status_t status = writeInt32(val.size());
809
810 if (status != OK) {
811 return status;
812 }
813
814 for (const auto& item : val) {
815 status = writeInt32(item);
816
817 if (status != OK) {
818 return status;
819 }
820 }
821
822 return OK;
823}
824
825status_t Parcel::writeInt64Vector(const std::vector<int64_t>& val)
826{
827 if (val.size() > std::numeric_limits<int32_t>::max()) {
828 return BAD_VALUE;
829 }
830
831 status_t status = writeInt32(val.size());
832
833 if (status != OK) {
834 return status;
835 }
836
837 for (const auto& item : val) {
838 status = writeInt64(item);
839
840 if (status != OK) {
841 return status;
842 }
843 }
844
845 return OK;
846}
847
848status_t Parcel::writeFloatVector(const std::vector<float>& val)
849{
850 if (val.size() > std::numeric_limits<int32_t>::max()) {
851 return BAD_VALUE;
852 }
853
854 status_t status = writeInt32(val.size());
855
856 if (status != OK) {
857 return status;
858 }
859
860 for (const auto& item : val) {
861 status = writeFloat(item);
862
863 if (status != OK) {
864 return status;
865 }
866 }
867
868 return OK;
869}
870
871status_t Parcel::writeDoubleVector(const std::vector<double>& val)
872{
873 if (val.size() > std::numeric_limits<int32_t>::max()) {
874 return BAD_VALUE;
875 }
876
877 status_t status = writeInt32(val.size());
878
879 if (status != OK) {
880 return status;
881 }
882
883 for (const auto& item : val) {
884 status = writeDouble(item);
885
886 if (status != OK) {
887 return status;
888 }
889 }
890
891 return OK;
892}
893
894status_t Parcel::writeBoolVector(const std::vector<bool>& val)
895{
896 if (val.size() > std::numeric_limits<int32_t>::max()) {
897 return BAD_VALUE;
898 }
899
900 status_t status = writeInt32(val.size());
901
902 if (status != OK) {
903 return status;
904 }
905
906 for (const auto& item : val) {
907 status = writeBool(item);
908
909 if (status != OK) {
910 return status;
911 }
912 }
913
914 return OK;
915}
916
917status_t Parcel::writeCharVector(const std::vector<char16_t>& val)
918{
919 if (val.size() > std::numeric_limits<int32_t>::max()) {
920 return BAD_VALUE;
921 }
922
923 status_t status = writeInt32(val.size());
924
925 if (status != OK) {
926 return status;
927 }
928
929 for (const auto& item : val) {
930 status = writeChar(item);
931
932 if (status != OK) {
933 return status;
934 }
935 }
936
937 return OK;
938}
939
940status_t Parcel::writeString16Vector(const std::vector<String16>& val)
941{
942 if (val.size() > std::numeric_limits<int32_t>::max()) {
943 return BAD_VALUE;
944 }
945
946 status_t status = writeInt32(val.size());
947
948 if (status != OK) {
949 return status;
950 }
951
952 for (const auto& item : val) {
953 status = writeString16(item);
954
955 if (status != OK) {
956 return status;
957 }
958 }
959
960 return OK;
961}
962
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700963status_t Parcel::writeInt32(int32_t val)
964{
Andreas Huber84a6d042009-08-17 13:33:27 -0700965 return writeAligned(val);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700966}
Dan Stoza41a0f2f2014-12-01 10:01:10 -0800967
968status_t Parcel::writeUint32(uint32_t val)
969{
970 return writeAligned(val);
971}
972
Marco Nelissen5c0106e2013-10-16 10:57:51 -0700973status_t Parcel::writeInt32Array(size_t len, const int32_t *val) {
Nick Kralevichb6b14232015-04-02 09:36:02 -0700974 if (len > INT32_MAX) {
975 // don't accept size_t values which may have come from an
976 // inadvertent conversion from a negative int.
977 return BAD_VALUE;
978 }
979
Marco Nelissen5c0106e2013-10-16 10:57:51 -0700980 if (!val) {
Chad Brubakere59cb432015-06-30 14:03:55 -0700981 return writeInt32(-1);
Marco Nelissen5c0106e2013-10-16 10:57:51 -0700982 }
Chad Brubakere59cb432015-06-30 14:03:55 -0700983 status_t ret = writeInt32(static_cast<uint32_t>(len));
Marco Nelissen5c0106e2013-10-16 10:57:51 -0700984 if (ret == NO_ERROR) {
985 ret = write(val, len * sizeof(*val));
986 }
987 return ret;
988}
Marco Nelissenf0190bf2014-03-13 14:17:40 -0700989status_t Parcel::writeByteArray(size_t len, const uint8_t *val) {
Nick Kralevichb6b14232015-04-02 09:36:02 -0700990 if (len > INT32_MAX) {
991 // don't accept size_t values which may have come from an
992 // inadvertent conversion from a negative int.
993 return BAD_VALUE;
994 }
995
Marco Nelissenf0190bf2014-03-13 14:17:40 -0700996 if (!val) {
Chad Brubakere59cb432015-06-30 14:03:55 -0700997 return writeInt32(-1);
Marco Nelissenf0190bf2014-03-13 14:17:40 -0700998 }
Chad Brubakere59cb432015-06-30 14:03:55 -0700999 status_t ret = writeInt32(static_cast<uint32_t>(len));
Marco Nelissenf0190bf2014-03-13 14:17:40 -07001000 if (ret == NO_ERROR) {
1001 ret = write(val, len * sizeof(*val));
1002 }
1003 return ret;
1004}
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001005
Casey Dahlina3774912015-10-15 15:44:59 -07001006status_t Parcel::writeBool(bool val)
1007{
1008 return writeInt32(int32_t(val));
1009}
1010
1011status_t Parcel::writeChar(char16_t val)
1012{
1013 return writeInt32(int32_t(val));
1014}
1015
1016status_t Parcel::writeByte(int8_t val)
1017{
1018 return writeInt32(int32_t(val));
1019}
1020
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001021status_t Parcel::writeInt64(int64_t val)
1022{
Andreas Huber84a6d042009-08-17 13:33:27 -07001023 return writeAligned(val);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001024}
1025
Ronghua Wu2d13afd2015-03-16 11:11:07 -07001026status_t Parcel::writeUint64(uint64_t val)
1027{
1028 return writeAligned(val);
1029}
1030
Serban Constantinescuf683e012013-11-05 16:53:55 +00001031status_t Parcel::writePointer(uintptr_t val)
1032{
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001033 return writeAligned<binder_uintptr_t>(val);
Serban Constantinescuf683e012013-11-05 16:53:55 +00001034}
1035
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001036status_t Parcel::writeFloat(float val)
1037{
Andreas Huber84a6d042009-08-17 13:33:27 -07001038 return writeAligned(val);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001039}
1040
Douglas Leungcc1a4bb2013-01-11 15:00:55 -08001041#if defined(__mips__) && defined(__mips_hard_float)
1042
1043status_t Parcel::writeDouble(double val)
1044{
1045 union {
1046 double d;
1047 unsigned long long ll;
1048 } u;
1049 u.d = val;
1050 return writeAligned(u.ll);
1051}
1052
1053#else
1054
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001055status_t Parcel::writeDouble(double val)
1056{
Andreas Huber84a6d042009-08-17 13:33:27 -07001057 return writeAligned(val);
1058}
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001059
Douglas Leungcc1a4bb2013-01-11 15:00:55 -08001060#endif
1061
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001062status_t Parcel::writeCString(const char* str)
1063{
1064 return write(str, strlen(str)+1);
1065}
1066
1067status_t Parcel::writeString8(const String8& str)
1068{
1069 status_t err = writeInt32(str.bytes());
Pravat Dalbeherad1dff8d2010-12-15 08:40:00 +01001070 // only write string if its length is more than zero characters,
1071 // as readString8 will only read if the length field is non-zero.
1072 // this is slightly different from how writeString16 works.
1073 if (str.bytes() > 0 && err == NO_ERROR) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001074 err = write(str.string(), str.bytes()+1);
1075 }
1076 return err;
1077}
1078
1079status_t Parcel::writeString16(const String16& str)
1080{
1081 return writeString16(str.string(), str.size());
1082}
1083
1084status_t Parcel::writeString16(const char16_t* str, size_t len)
1085{
1086 if (str == NULL) return writeInt32(-1);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001087
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001088 status_t err = writeInt32(len);
1089 if (err == NO_ERROR) {
1090 len *= sizeof(char16_t);
1091 uint8_t* data = (uint8_t*)writeInplace(len+sizeof(char16_t));
1092 if (data) {
1093 memcpy(data, str, len);
1094 *reinterpret_cast<char16_t*>(data+len) = 0;
1095 return NO_ERROR;
1096 }
1097 err = mError;
1098 }
1099 return err;
1100}
1101
1102status_t Parcel::writeStrongBinder(const sp<IBinder>& val)
1103{
1104 return flatten_binder(ProcessState::self(), val, this);
1105}
1106
Casey Dahlin3e4b6d22015-11-03 13:50:37 -08001107status_t Parcel::writeStrongBinderVector(const std::vector<sp<IBinder>>& val)
1108{
1109 if (val.size() > std::numeric_limits<int32_t>::max()) {
1110 return BAD_VALUE;
1111 }
1112
1113 status_t status = writeInt32(val.size());
1114
1115 if (status != OK) {
1116 return status;
1117 }
1118
1119 for (const auto& item : val) {
1120 status = writeStrongBinder(item);
1121
1122 if (status != OK) {
1123 return status;
1124 }
1125 }
1126
1127 return OK;
1128}
1129
1130status_t Parcel::readStrongBinderVector(std::vector<sp<IBinder>>* val) const {
Christopher Wiley6264c4c2015-11-10 09:44:30 -08001131 return readTypedVector(val, this, &Parcel::readStrongBinder);
Casey Dahlin3e4b6d22015-11-03 13:50:37 -08001132}
1133
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001134status_t Parcel::writeWeakBinder(const wp<IBinder>& val)
1135{
1136 return flatten_binder(ProcessState::self(), val, this);
1137}
1138
Mathias Agopiana47f02a2009-05-21 16:29:38 -07001139status_t Parcel::writeNativeHandle(const native_handle* handle)
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001140{
Mathias Agopian1d0a95b2009-07-31 16:12:13 -07001141 if (!handle || handle->version != sizeof(native_handle))
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001142 return BAD_TYPE;
1143
1144 status_t err;
Mathias Agopiana47f02a2009-05-21 16:29:38 -07001145 err = writeInt32(handle->numFds);
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001146 if (err != NO_ERROR) return err;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001147
Mathias Agopiana47f02a2009-05-21 16:29:38 -07001148 err = writeInt32(handle->numInts);
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001149 if (err != NO_ERROR) return err;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001150
Mathias Agopiana47f02a2009-05-21 16:29:38 -07001151 for (int i=0 ; err==NO_ERROR && i<handle->numFds ; i++)
1152 err = writeDupFileDescriptor(handle->data[i]);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001153
1154 if (err != NO_ERROR) {
Steve Block9d453682011-12-20 16:23:08 +00001155 ALOGD("write native handle, write dup fd failed");
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001156 return err;
1157 }
Mathias Agopiana47f02a2009-05-21 16:29:38 -07001158 err = write(handle->data + handle->numFds, sizeof(int)*handle->numInts);
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001159 return err;
1160}
1161
Jeff Brown93ff1f92011-11-04 19:01:44 -07001162status_t Parcel::writeFileDescriptor(int fd, bool takeOwnership)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001163{
1164 flat_binder_object obj;
1165 obj.type = BINDER_TYPE_FD;
1166 obj.flags = 0x7f | FLAT_BINDER_FLAG_ACCEPTS_FDS;
Arve Hjønnevåg07fd0f12014-02-18 21:10:29 -08001167 obj.binder = 0; /* Don't pass uninitialized stack data to a remote process */
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001168 obj.handle = fd;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001169 obj.cookie = takeOwnership ? 1 : 0;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001170 return writeObject(obj, true);
1171}
1172
1173status_t Parcel::writeDupFileDescriptor(int fd)
1174{
Jeff Brownd341c712011-11-04 20:19:33 -07001175 int dupFd = dup(fd);
1176 if (dupFd < 0) {
1177 return -errno;
1178 }
1179 status_t err = writeFileDescriptor(dupFd, true /*takeOwnership*/);
1180 if (err) {
1181 close(dupFd);
1182 }
1183 return err;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001184}
1185
Jeff Brown13b16042014-11-11 16:44:25 -08001186status_t Parcel::writeBlob(size_t len, bool mutableCopy, WritableBlob* outBlob)
Jeff Brown5707dbf2011-09-23 21:17:56 -07001187{
Nick Kralevichb6b14232015-04-02 09:36:02 -07001188 if (len > INT32_MAX) {
1189 // don't accept size_t values which may have come from an
1190 // inadvertent conversion from a negative int.
1191 return BAD_VALUE;
1192 }
1193
Jeff Brown13b16042014-11-11 16:44:25 -08001194 status_t status;
1195 if (!mAllowFds || len <= BLOB_INPLACE_LIMIT) {
Steve Block6807e592011-10-20 11:56:00 +01001196 ALOGV("writeBlob: write in place");
Jeff Brown13b16042014-11-11 16:44:25 -08001197 status = writeInt32(BLOB_INPLACE);
Jeff Brown5707dbf2011-09-23 21:17:56 -07001198 if (status) return status;
1199
1200 void* ptr = writeInplace(len);
1201 if (!ptr) return NO_MEMORY;
1202
Jeff Brown13b16042014-11-11 16:44:25 -08001203 outBlob->init(-1, ptr, len, false);
Jeff Brown5707dbf2011-09-23 21:17:56 -07001204 return NO_ERROR;
1205 }
1206
Steve Block6807e592011-10-20 11:56:00 +01001207 ALOGV("writeBlob: write to ashmem");
Jeff Brown5707dbf2011-09-23 21:17:56 -07001208 int fd = ashmem_create_region("Parcel Blob", len);
1209 if (fd < 0) return NO_MEMORY;
1210
1211 int result = ashmem_set_prot_region(fd, PROT_READ | PROT_WRITE);
1212 if (result < 0) {
Jeff Brownec4e0062011-10-10 14:50:10 -07001213 status = result;
Jeff Brown5707dbf2011-09-23 21:17:56 -07001214 } else {
1215 void* ptr = ::mmap(NULL, len, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
1216 if (ptr == MAP_FAILED) {
1217 status = -errno;
1218 } else {
Jeff Brown13b16042014-11-11 16:44:25 -08001219 if (!mutableCopy) {
1220 result = ashmem_set_prot_region(fd, PROT_READ);
1221 }
Jeff Brown5707dbf2011-09-23 21:17:56 -07001222 if (result < 0) {
Jeff Brownec4e0062011-10-10 14:50:10 -07001223 status = result;
Jeff Brown5707dbf2011-09-23 21:17:56 -07001224 } else {
Jeff Brown13b16042014-11-11 16:44:25 -08001225 status = writeInt32(mutableCopy ? BLOB_ASHMEM_MUTABLE : BLOB_ASHMEM_IMMUTABLE);
Jeff Brown5707dbf2011-09-23 21:17:56 -07001226 if (!status) {
Jeff Brown93ff1f92011-11-04 19:01:44 -07001227 status = writeFileDescriptor(fd, true /*takeOwnership*/);
Jeff Brown5707dbf2011-09-23 21:17:56 -07001228 if (!status) {
Jeff Brown13b16042014-11-11 16:44:25 -08001229 outBlob->init(fd, ptr, len, mutableCopy);
Jeff Brown5707dbf2011-09-23 21:17:56 -07001230 return NO_ERROR;
1231 }
1232 }
1233 }
1234 }
1235 ::munmap(ptr, len);
1236 }
1237 ::close(fd);
1238 return status;
1239}
1240
Jeff Brown13b16042014-11-11 16:44:25 -08001241status_t Parcel::writeDupImmutableBlobFileDescriptor(int fd)
1242{
1243 // Must match up with what's done in writeBlob.
1244 if (!mAllowFds) return FDS_NOT_ALLOWED;
1245 status_t status = writeInt32(BLOB_ASHMEM_IMMUTABLE);
1246 if (status) return status;
1247 return writeDupFileDescriptor(fd);
1248}
1249
Mathias Agopiane1424282013-07-29 21:24:40 -07001250status_t Parcel::write(const FlattenableHelperInterface& val)
Mathias Agopian98e71dd2010-02-11 17:30:52 -08001251{
1252 status_t err;
1253
1254 // size if needed
Mathias Agopiane1424282013-07-29 21:24:40 -07001255 const size_t len = val.getFlattenedSize();
1256 const size_t fd_count = val.getFdCount();
Mathias Agopian98e71dd2010-02-11 17:30:52 -08001257
Nick Kralevichb6b14232015-04-02 09:36:02 -07001258 if ((len > INT32_MAX) || (fd_count > INT32_MAX)) {
1259 // don't accept size_t values which may have come from an
1260 // inadvertent conversion from a negative int.
1261 return BAD_VALUE;
1262 }
1263
Mathias Agopian98e71dd2010-02-11 17:30:52 -08001264 err = this->writeInt32(len);
1265 if (err) return err;
1266
1267 err = this->writeInt32(fd_count);
1268 if (err) return err;
1269
1270 // payload
Nick Kralevichb6b14232015-04-02 09:36:02 -07001271 void* const buf = this->writeInplace(pad_size(len));
Mathias Agopian98e71dd2010-02-11 17:30:52 -08001272 if (buf == NULL)
1273 return BAD_VALUE;
1274
1275 int* fds = NULL;
1276 if (fd_count) {
1277 fds = new int[fd_count];
1278 }
1279
1280 err = val.flatten(buf, len, fds, fd_count);
1281 for (size_t i=0 ; i<fd_count && err==NO_ERROR ; i++) {
1282 err = this->writeDupFileDescriptor( fds[i] );
1283 }
1284
1285 if (fd_count) {
1286 delete [] fds;
1287 }
1288
1289 return err;
1290}
1291
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001292status_t Parcel::writeObject(const flat_binder_object& val, bool nullMetaData)
1293{
1294 const bool enoughData = (mDataPos+sizeof(val)) <= mDataCapacity;
1295 const bool enoughObjects = mObjectsSize < mObjectsCapacity;
1296 if (enoughData && enoughObjects) {
1297restart_write:
1298 *reinterpret_cast<flat_binder_object*>(mData+mDataPos) = val;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001299
Christopher Tate98e67d32015-06-03 18:44:15 -07001300 // remember if it's a file descriptor
1301 if (val.type == BINDER_TYPE_FD) {
1302 if (!mAllowFds) {
1303 // fail before modifying our object index
1304 return FDS_NOT_ALLOWED;
1305 }
1306 mHasFds = mFdsKnown = true;
1307 }
1308
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001309 // Need to write meta-data?
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001310 if (nullMetaData || val.binder != 0) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001311 mObjects[mObjectsSize] = mDataPos;
Adrian Rooscbf37262015-10-22 16:12:53 -07001312 acquire_object(ProcessState::self(), val, this, &mOpenAshmemSize);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001313 mObjectsSize++;
1314 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001315
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001316 return finishWrite(sizeof(flat_binder_object));
1317 }
1318
1319 if (!enoughData) {
1320 const status_t err = growData(sizeof(val));
1321 if (err != NO_ERROR) return err;
1322 }
1323 if (!enoughObjects) {
1324 size_t newSize = ((mObjectsSize+2)*3)/2;
Christopher Tateed7a50c2015-06-08 14:45:14 -07001325 if (newSize < mObjectsSize) return NO_MEMORY; // overflow
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001326 binder_size_t* objects = (binder_size_t*)realloc(mObjects, newSize*sizeof(binder_size_t));
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001327 if (objects == NULL) return NO_MEMORY;
1328 mObjects = objects;
1329 mObjectsCapacity = newSize;
1330 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001331
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001332 goto restart_write;
1333}
1334
Brad Fitzpatrick837a0d02010-07-13 15:33:35 -07001335status_t Parcel::writeNoException()
1336{
1337 return writeInt32(0);
1338}
1339
Colin Cross6f4f3ab2014-02-05 17:42:44 -08001340void Parcel::remove(size_t /*start*/, size_t /*amt*/)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001341{
1342 LOG_ALWAYS_FATAL("Parcel::remove() not yet implemented!");
1343}
1344
1345status_t Parcel::read(void* outData, size_t len) const
1346{
Nick Kralevichb6b14232015-04-02 09:36:02 -07001347 if (len > INT32_MAX) {
1348 // don't accept size_t values which may have come from an
1349 // inadvertent conversion from a negative int.
1350 return BAD_VALUE;
1351 }
1352
1353 if ((mDataPos+pad_size(len)) >= mDataPos && (mDataPos+pad_size(len)) <= mDataSize
1354 && len <= pad_size(len)) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001355 memcpy(outData, mData+mDataPos, len);
Nick Kralevichb6b14232015-04-02 09:36:02 -07001356 mDataPos += pad_size(len);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001357 ALOGV("read Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001358 return NO_ERROR;
1359 }
1360 return NOT_ENOUGH_DATA;
1361}
1362
1363const void* Parcel::readInplace(size_t len) const
1364{
Nick Kralevichb6b14232015-04-02 09:36:02 -07001365 if (len > INT32_MAX) {
1366 // don't accept size_t values which may have come from an
1367 // inadvertent conversion from a negative int.
1368 return NULL;
1369 }
1370
1371 if ((mDataPos+pad_size(len)) >= mDataPos && (mDataPos+pad_size(len)) <= mDataSize
1372 && len <= pad_size(len)) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001373 const void* data = mData+mDataPos;
Nick Kralevichb6b14232015-04-02 09:36:02 -07001374 mDataPos += pad_size(len);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001375 ALOGV("readInplace Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001376 return data;
1377 }
1378 return NULL;
1379}
1380
Andreas Huber84a6d042009-08-17 13:33:27 -07001381template<class T>
1382status_t Parcel::readAligned(T *pArg) const {
Nick Kralevichb6b14232015-04-02 09:36:02 -07001383 COMPILE_TIME_ASSERT_FUNCTION_SCOPE(PAD_SIZE_UNSAFE(sizeof(T)) == sizeof(T));
Andreas Huber84a6d042009-08-17 13:33:27 -07001384
1385 if ((mDataPos+sizeof(T)) <= mDataSize) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001386 const void* data = mData+mDataPos;
Andreas Huber84a6d042009-08-17 13:33:27 -07001387 mDataPos += sizeof(T);
1388 *pArg = *reinterpret_cast<const T*>(data);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001389 return NO_ERROR;
1390 } else {
1391 return NOT_ENOUGH_DATA;
1392 }
1393}
1394
Andreas Huber84a6d042009-08-17 13:33:27 -07001395template<class T>
1396T Parcel::readAligned() const {
1397 T result;
1398 if (readAligned(&result) != NO_ERROR) {
1399 result = 0;
1400 }
1401
1402 return result;
1403}
1404
1405template<class T>
1406status_t Parcel::writeAligned(T val) {
Nick Kralevichb6b14232015-04-02 09:36:02 -07001407 COMPILE_TIME_ASSERT_FUNCTION_SCOPE(PAD_SIZE_UNSAFE(sizeof(T)) == sizeof(T));
Andreas Huber84a6d042009-08-17 13:33:27 -07001408
1409 if ((mDataPos+sizeof(val)) <= mDataCapacity) {
1410restart_write:
1411 *reinterpret_cast<T*>(mData+mDataPos) = val;
1412 return finishWrite(sizeof(val));
1413 }
1414
1415 status_t err = growData(sizeof(val));
1416 if (err == NO_ERROR) goto restart_write;
1417 return err;
1418}
1419
Casey Dahlin08bb1452015-10-19 18:12:18 -07001420status_t Parcel::readByteVector(std::vector<int8_t>* val) const {
1421 val->clear();
1422
1423 int32_t size;
1424 status_t status = readInt32(&size);
1425
1426 if (status != OK) {
1427 return status;
1428 }
1429
Christopher Wiley6264c4c2015-11-10 09:44:30 -08001430 if (size < 0) {
1431 status = UNEXPECTED_NULL;
1432 return status;
1433 }
1434 if (size_t(size) > dataAvail()) {
Christopher Wiley714a3ab2015-10-31 13:22:15 -07001435 status = BAD_VALUE;
1436 return status;
Casey Dahlin08bb1452015-10-19 18:12:18 -07001437 }
Christopher Wiley6264c4c2015-11-10 09:44:30 -08001438
Christopher Wiley714a3ab2015-10-31 13:22:15 -07001439 const void* data = readInplace(size);
1440 if (!data) {
1441 status = BAD_VALUE;
1442 return status;
1443 }
Casey Dahlin08bb1452015-10-19 18:12:18 -07001444 val->resize(size);
Christopher Wiley714a3ab2015-10-31 13:22:15 -07001445 memcpy(val->data(), data, size);
Casey Dahlin08bb1452015-10-19 18:12:18 -07001446
Christopher Wiley714a3ab2015-10-31 13:22:15 -07001447 return status;
Casey Dahlin08bb1452015-10-19 18:12:18 -07001448}
1449
1450status_t Parcel::readInt32Vector(std::vector<int32_t>* val) const {
Christopher Wiley6264c4c2015-11-10 09:44:30 -08001451 return readTypedVector(val, this, &Parcel::readInt32);
Casey Dahlin08bb1452015-10-19 18:12:18 -07001452}
1453
1454status_t Parcel::readInt64Vector(std::vector<int64_t>* val) const {
Christopher Wiley6264c4c2015-11-10 09:44:30 -08001455 return readTypedVector(val, this, &Parcel::readInt64);
Casey Dahlin08bb1452015-10-19 18:12:18 -07001456}
1457
1458status_t Parcel::readFloatVector(std::vector<float>* val) const {
Christopher Wiley6264c4c2015-11-10 09:44:30 -08001459 return readTypedVector(val, this, &Parcel::readFloat);
Casey Dahlin08bb1452015-10-19 18:12:18 -07001460}
1461
1462status_t Parcel::readDoubleVector(std::vector<double>* val) const {
Christopher Wiley6264c4c2015-11-10 09:44:30 -08001463 return readTypedVector(val, this, &Parcel::readDouble);
Casey Dahlin08bb1452015-10-19 18:12:18 -07001464}
1465
1466status_t Parcel::readBoolVector(std::vector<bool>* val) const {
1467 val->clear();
1468
1469 int32_t size;
1470 status_t status = readInt32(&size);
1471
1472 if (status != OK) {
1473 return status;
1474 }
1475
1476 if (size < 0) {
Christopher Wiley6264c4c2015-11-10 09:44:30 -08001477 return UNEXPECTED_NULL;
Casey Dahlin08bb1452015-10-19 18:12:18 -07001478 }
1479
1480 val->resize(size);
1481
1482 /* C++ bool handling means a vector of bools isn't necessarily addressable
1483 * (we might use individual bits)
1484 */
Christopher Wiley489931b2015-10-27 16:33:47 -07001485 bool data;
1486 for (int32_t i = 0; i < size; ++i) {
Casey Dahlin08bb1452015-10-19 18:12:18 -07001487 status = readBool(&data);
1488 (*val)[i] = data;
1489
1490 if (status != OK) {
1491 return status;
1492 }
1493 }
1494
1495 return OK;
1496}
1497
1498status_t Parcel::readCharVector(std::vector<char16_t>* val) const {
Christopher Wiley6264c4c2015-11-10 09:44:30 -08001499 return readTypedVector(val, this, &Parcel::readChar);
Casey Dahlin08bb1452015-10-19 18:12:18 -07001500}
1501
1502status_t Parcel::readString16Vector(std::vector<String16>* val) const {
Christopher Wiley6264c4c2015-11-10 09:44:30 -08001503 return readTypedVector(val, this, &Parcel::readString16);
Casey Dahlin08bb1452015-10-19 18:12:18 -07001504}
1505
1506
Andreas Huber84a6d042009-08-17 13:33:27 -07001507status_t Parcel::readInt32(int32_t *pArg) const
1508{
1509 return readAligned(pArg);
1510}
1511
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001512int32_t Parcel::readInt32() const
1513{
Andreas Huber84a6d042009-08-17 13:33:27 -07001514 return readAligned<int32_t>();
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001515}
1516
Dan Stoza41a0f2f2014-12-01 10:01:10 -08001517status_t Parcel::readUint32(uint32_t *pArg) const
1518{
1519 return readAligned(pArg);
1520}
1521
1522uint32_t Parcel::readUint32() const
1523{
1524 return readAligned<uint32_t>();
1525}
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001526
1527status_t Parcel::readInt64(int64_t *pArg) const
1528{
Andreas Huber84a6d042009-08-17 13:33:27 -07001529 return readAligned(pArg);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001530}
1531
1532
1533int64_t Parcel::readInt64() const
1534{
Andreas Huber84a6d042009-08-17 13:33:27 -07001535 return readAligned<int64_t>();
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001536}
1537
Ronghua Wu2d13afd2015-03-16 11:11:07 -07001538status_t Parcel::readUint64(uint64_t *pArg) const
1539{
1540 return readAligned(pArg);
1541}
1542
1543uint64_t Parcel::readUint64() const
1544{
1545 return readAligned<uint64_t>();
1546}
1547
Serban Constantinescuf683e012013-11-05 16:53:55 +00001548status_t Parcel::readPointer(uintptr_t *pArg) const
1549{
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001550 status_t ret;
1551 binder_uintptr_t ptr;
1552 ret = readAligned(&ptr);
1553 if (!ret)
1554 *pArg = ptr;
1555 return ret;
Serban Constantinescuf683e012013-11-05 16:53:55 +00001556}
1557
1558uintptr_t Parcel::readPointer() const
1559{
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001560 return readAligned<binder_uintptr_t>();
Serban Constantinescuf683e012013-11-05 16:53:55 +00001561}
1562
1563
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001564status_t Parcel::readFloat(float *pArg) const
1565{
Andreas Huber84a6d042009-08-17 13:33:27 -07001566 return readAligned(pArg);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001567}
1568
1569
1570float Parcel::readFloat() const
1571{
Andreas Huber84a6d042009-08-17 13:33:27 -07001572 return readAligned<float>();
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001573}
1574
Douglas Leungcc1a4bb2013-01-11 15:00:55 -08001575#if defined(__mips__) && defined(__mips_hard_float)
1576
1577status_t Parcel::readDouble(double *pArg) const
1578{
1579 union {
1580 double d;
1581 unsigned long long ll;
1582 } u;
Narayan Kamath2c68d382014-06-04 15:04:29 +01001583 u.d = 0;
Douglas Leungcc1a4bb2013-01-11 15:00:55 -08001584 status_t status;
1585 status = readAligned(&u.ll);
1586 *pArg = u.d;
1587 return status;
1588}
1589
1590double Parcel::readDouble() const
1591{
1592 union {
1593 double d;
1594 unsigned long long ll;
1595 } u;
1596 u.ll = readAligned<unsigned long long>();
1597 return u.d;
1598}
1599
1600#else
1601
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001602status_t Parcel::readDouble(double *pArg) const
1603{
Andreas Huber84a6d042009-08-17 13:33:27 -07001604 return readAligned(pArg);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001605}
1606
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001607double Parcel::readDouble() const
1608{
Andreas Huber84a6d042009-08-17 13:33:27 -07001609 return readAligned<double>();
1610}
1611
Douglas Leungcc1a4bb2013-01-11 15:00:55 -08001612#endif
1613
Andreas Huber84a6d042009-08-17 13:33:27 -07001614status_t Parcel::readIntPtr(intptr_t *pArg) const
1615{
1616 return readAligned(pArg);
1617}
1618
1619
1620intptr_t Parcel::readIntPtr() const
1621{
1622 return readAligned<intptr_t>();
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001623}
1624
Casey Dahlina3774912015-10-15 15:44:59 -07001625status_t Parcel::readBool(bool *pArg) const
1626{
1627 int32_t tmp;
1628 status_t ret = readInt32(&tmp);
1629 *pArg = (tmp != 0);
1630 return ret;
1631}
1632
1633bool Parcel::readBool() const
1634{
1635 return readInt32() != 0;
1636}
1637
1638status_t Parcel::readChar(char16_t *pArg) const
1639{
1640 int32_t tmp;
1641 status_t ret = readInt32(&tmp);
1642 *pArg = char16_t(tmp);
1643 return ret;
1644}
1645
1646char16_t Parcel::readChar() const
1647{
1648 return char16_t(readInt32());
1649}
1650
1651status_t Parcel::readByte(int8_t *pArg) const
1652{
1653 int32_t tmp;
1654 status_t ret = readInt32(&tmp);
1655 *pArg = int8_t(tmp);
1656 return ret;
1657}
1658
1659int8_t Parcel::readByte() const
1660{
1661 return int8_t(readInt32());
1662}
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001663
1664const char* Parcel::readCString() const
1665{
1666 const size_t avail = mDataSize-mDataPos;
1667 if (avail > 0) {
1668 const char* str = reinterpret_cast<const char*>(mData+mDataPos);
1669 // is the string's trailing NUL within the parcel's valid bounds?
1670 const char* eos = reinterpret_cast<const char*>(memchr(str, 0, avail));
1671 if (eos) {
1672 const size_t len = eos - str;
Nick Kralevichb6b14232015-04-02 09:36:02 -07001673 mDataPos += pad_size(len+1);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001674 ALOGV("readCString Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001675 return str;
1676 }
1677 }
1678 return NULL;
1679}
1680
1681String8 Parcel::readString8() const
1682{
1683 int32_t size = readInt32();
1684 // watch for potential int overflow adding 1 for trailing NUL
1685 if (size > 0 && size < INT32_MAX) {
1686 const char* str = (const char*)readInplace(size+1);
1687 if (str) return String8(str, size);
1688 }
1689 return String8();
1690}
1691
1692String16 Parcel::readString16() const
1693{
1694 size_t len;
1695 const char16_t* str = readString16Inplace(&len);
1696 if (str) return String16(str, len);
Steve Blocke6f43dd2012-01-06 19:20:56 +00001697 ALOGE("Reading a NULL string not supported here.");
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001698 return String16();
1699}
1700
Casey Dahlin08bb1452015-10-19 18:12:18 -07001701status_t Parcel::readString16(String16* pArg) const
1702{
1703 size_t len;
1704 const char16_t* str = readString16Inplace(&len);
1705 if (str) {
Casey Dahlina5bcc952015-10-20 16:26:23 -07001706 pArg->setTo(str, len);
Casey Dahlin08bb1452015-10-19 18:12:18 -07001707 return 0;
1708 } else {
1709 *pArg = String16();
Christopher Wiley6264c4c2015-11-10 09:44:30 -08001710 return UNEXPECTED_NULL;
Casey Dahlin08bb1452015-10-19 18:12:18 -07001711 }
1712}
1713
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001714const char16_t* Parcel::readString16Inplace(size_t* outLen) const
1715{
1716 int32_t size = readInt32();
1717 // watch for potential int overflow from size+1
1718 if (size >= 0 && size < INT32_MAX) {
1719 *outLen = size;
1720 const char16_t* str = (const char16_t*)readInplace((size+1)*sizeof(char16_t));
1721 if (str != NULL) {
1722 return str;
1723 }
1724 }
1725 *outLen = 0;
1726 return NULL;
1727}
1728
Casey Dahlin930a5722015-10-27 18:33:56 -07001729status_t Parcel::readStrongBinder(sp<IBinder>* val) const
1730{
1731 return unflatten_binder(ProcessState::self(), *this, val);
1732}
1733
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001734sp<IBinder> Parcel::readStrongBinder() const
1735{
1736 sp<IBinder> val;
Casey Dahlin930a5722015-10-27 18:33:56 -07001737 readStrongBinder(&val);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001738 return val;
1739}
1740
1741wp<IBinder> Parcel::readWeakBinder() const
1742{
1743 wp<IBinder> val;
1744 unflatten_binder(ProcessState::self(), *this, &val);
1745 return val;
1746}
1747
Brad Fitzpatrick837a0d02010-07-13 15:33:35 -07001748int32_t Parcel::readExceptionCode() const
1749{
1750 int32_t exception_code = readAligned<int32_t>();
Brad Fitzpatrickd36f4a52010-07-12 11:05:38 -07001751 if (exception_code == EX_HAS_REPLY_HEADER) {
Magnus Strandberg1ba24572011-05-03 15:44:00 +02001752 int32_t header_start = dataPosition();
Brad Fitzpatrickd36f4a52010-07-12 11:05:38 -07001753 int32_t header_size = readAligned<int32_t>();
1754 // Skip over fat responses headers. Not used (or propagated) in
1755 // native code
Magnus Strandberg1ba24572011-05-03 15:44:00 +02001756 setDataPosition(header_start + header_size);
Brad Fitzpatrickd36f4a52010-07-12 11:05:38 -07001757 // And fat response headers are currently only used when there are no
1758 // exceptions, so return no error:
1759 return 0;
1760 }
Brad Fitzpatrick837a0d02010-07-13 15:33:35 -07001761 return exception_code;
1762}
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001763
Mathias Agopiana47f02a2009-05-21 16:29:38 -07001764native_handle* Parcel::readNativeHandle() const
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001765{
1766 int numFds, numInts;
1767 status_t err;
1768 err = readInt32(&numFds);
1769 if (err != NO_ERROR) return 0;
1770 err = readInt32(&numInts);
1771 if (err != NO_ERROR) return 0;
1772
Mathias Agopiana47f02a2009-05-21 16:29:38 -07001773 native_handle* h = native_handle_create(numFds, numInts);
Adam Lesinskieaac99a2015-05-12 17:35:48 -07001774 if (!h) {
1775 return 0;
1776 }
1777
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001778 for (int i=0 ; err==NO_ERROR && i<numFds ; i++) {
Rebecca Schultz Zavin360211f2009-02-13 16:34:38 -08001779 h->data[i] = dup(readFileDescriptor());
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001780 if (h->data[i] < 0) err = BAD_VALUE;
1781 }
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001782 err = read(h->data + numFds, sizeof(int)*numInts);
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001783 if (err != NO_ERROR) {
Mathias Agopiana47f02a2009-05-21 16:29:38 -07001784 native_handle_close(h);
1785 native_handle_delete(h);
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001786 h = 0;
1787 }
1788 return h;
1789}
1790
1791
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001792int Parcel::readFileDescriptor() const
1793{
1794 const flat_binder_object* flat = readObject(true);
1795 if (flat) {
1796 switch (flat->type) {
1797 case BINDER_TYPE_FD:
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001798 //ALOGI("Returning file descriptor %ld from parcel %p", flat->handle, this);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001799 return flat->handle;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001800 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001801 }
1802 return BAD_TYPE;
1803}
1804
Jeff Brown5707dbf2011-09-23 21:17:56 -07001805status_t Parcel::readBlob(size_t len, ReadableBlob* outBlob) const
1806{
Jeff Brown13b16042014-11-11 16:44:25 -08001807 int32_t blobType;
1808 status_t status = readInt32(&blobType);
Jeff Brown5707dbf2011-09-23 21:17:56 -07001809 if (status) return status;
1810
Jeff Brown13b16042014-11-11 16:44:25 -08001811 if (blobType == BLOB_INPLACE) {
Steve Block6807e592011-10-20 11:56:00 +01001812 ALOGV("readBlob: read in place");
Jeff Brown5707dbf2011-09-23 21:17:56 -07001813 const void* ptr = readInplace(len);
1814 if (!ptr) return BAD_VALUE;
1815
Jeff Brown13b16042014-11-11 16:44:25 -08001816 outBlob->init(-1, const_cast<void*>(ptr), len, false);
Jeff Brown5707dbf2011-09-23 21:17:56 -07001817 return NO_ERROR;
1818 }
1819
Steve Block6807e592011-10-20 11:56:00 +01001820 ALOGV("readBlob: read from ashmem");
Jeff Brown13b16042014-11-11 16:44:25 -08001821 bool isMutable = (blobType == BLOB_ASHMEM_MUTABLE);
Jeff Brown5707dbf2011-09-23 21:17:56 -07001822 int fd = readFileDescriptor();
1823 if (fd == int(BAD_TYPE)) return BAD_VALUE;
1824
Jeff Brown13b16042014-11-11 16:44:25 -08001825 void* ptr = ::mmap(NULL, len, isMutable ? PROT_READ | PROT_WRITE : PROT_READ,
1826 MAP_SHARED, fd, 0);
Narayan Kamath9ea09752014-10-08 17:35:45 +01001827 if (ptr == MAP_FAILED) return NO_MEMORY;
Jeff Brown5707dbf2011-09-23 21:17:56 -07001828
Jeff Brown13b16042014-11-11 16:44:25 -08001829 outBlob->init(fd, ptr, len, isMutable);
Jeff Brown5707dbf2011-09-23 21:17:56 -07001830 return NO_ERROR;
1831}
1832
Mathias Agopiane1424282013-07-29 21:24:40 -07001833status_t Parcel::read(FlattenableHelperInterface& val) const
Mathias Agopian98e71dd2010-02-11 17:30:52 -08001834{
1835 // size
1836 const size_t len = this->readInt32();
1837 const size_t fd_count = this->readInt32();
1838
Nick Kralevichb6b14232015-04-02 09:36:02 -07001839 if (len > INT32_MAX) {
1840 // don't accept size_t values which may have come from an
1841 // inadvertent conversion from a negative int.
1842 return BAD_VALUE;
1843 }
1844
Mathias Agopian98e71dd2010-02-11 17:30:52 -08001845 // payload
Nick Kralevichb6b14232015-04-02 09:36:02 -07001846 void const* const buf = this->readInplace(pad_size(len));
Mathias Agopian98e71dd2010-02-11 17:30:52 -08001847 if (buf == NULL)
1848 return BAD_VALUE;
1849
1850 int* fds = NULL;
1851 if (fd_count) {
1852 fds = new int[fd_count];
1853 }
1854
1855 status_t err = NO_ERROR;
1856 for (size_t i=0 ; i<fd_count && err==NO_ERROR ; i++) {
Jesse Hallfee99042014-11-04 08:36:31 -08001857 fds[i] = dup(this->readFileDescriptor());
Jun Jiangabf8a2c2014-04-29 14:22:10 +08001858 if (fds[i] < 0) {
1859 err = BAD_VALUE;
Jesse Hallfee99042014-11-04 08:36:31 -08001860 ALOGE("dup() failed in Parcel::read, i is %zu, fds[i] is %d, fd_count is %zu, error: %s",
1861 i, fds[i], fd_count, strerror(errno));
Jun Jiangabf8a2c2014-04-29 14:22:10 +08001862 }
Mathias Agopian98e71dd2010-02-11 17:30:52 -08001863 }
1864
1865 if (err == NO_ERROR) {
1866 err = val.unflatten(buf, len, fds, fd_count);
1867 }
1868
1869 if (fd_count) {
1870 delete [] fds;
1871 }
1872
1873 return err;
1874}
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001875const flat_binder_object* Parcel::readObject(bool nullMetaData) const
1876{
1877 const size_t DPOS = mDataPos;
1878 if ((DPOS+sizeof(flat_binder_object)) <= mDataSize) {
1879 const flat_binder_object* obj
1880 = reinterpret_cast<const flat_binder_object*>(mData+DPOS);
1881 mDataPos = DPOS + sizeof(flat_binder_object);
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001882 if (!nullMetaData && (obj->cookie == 0 && obj->binder == 0)) {
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001883 // When transferring a NULL object, we don't write it into
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001884 // the object list, so we don't want to check for it when
1885 // reading.
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001886 ALOGV("readObject Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001887 return obj;
1888 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001889
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001890 // Ensure that this object is valid...
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001891 binder_size_t* const OBJS = mObjects;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001892 const size_t N = mObjectsSize;
1893 size_t opos = mNextObjectHint;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001894
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001895 if (N > 0) {
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001896 ALOGV("Parcel %p looking for obj at %zu, hint=%zu",
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001897 this, DPOS, opos);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001898
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001899 // Start at the current hint position, looking for an object at
1900 // the current data position.
1901 if (opos < N) {
1902 while (opos < (N-1) && OBJS[opos] < DPOS) {
1903 opos++;
1904 }
1905 } else {
1906 opos = N-1;
1907 }
1908 if (OBJS[opos] == DPOS) {
1909 // Found it!
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001910 ALOGV("Parcel %p found obj %zu at index %zu with forward search",
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001911 this, DPOS, opos);
1912 mNextObjectHint = opos+1;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001913 ALOGV("readObject Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001914 return obj;
1915 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001916
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001917 // Look backwards for it...
1918 while (opos > 0 && OBJS[opos] > DPOS) {
1919 opos--;
1920 }
1921 if (OBJS[opos] == DPOS) {
1922 // Found it!
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001923 ALOGV("Parcel %p found obj %zu at index %zu with backward search",
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001924 this, DPOS, opos);
1925 mNextObjectHint = opos+1;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001926 ALOGV("readObject Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001927 return obj;
1928 }
1929 }
Colin Cross6f4f3ab2014-02-05 17:42:44 -08001930 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 -07001931 this, DPOS);
1932 }
1933 return NULL;
1934}
1935
1936void Parcel::closeFileDescriptors()
1937{
1938 size_t i = mObjectsSize;
1939 if (i > 0) {
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001940 //ALOGI("Closing file descriptors for %zu objects...", i);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001941 }
1942 while (i > 0) {
1943 i--;
1944 const flat_binder_object* flat
1945 = reinterpret_cast<flat_binder_object*>(mData+mObjects[i]);
1946 if (flat->type == BINDER_TYPE_FD) {
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001947 //ALOGI("Closing fd: %ld", flat->handle);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001948 close(flat->handle);
1949 }
1950 }
1951}
1952
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001953uintptr_t Parcel::ipcData() const
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001954{
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001955 return reinterpret_cast<uintptr_t>(mData);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001956}
1957
1958size_t Parcel::ipcDataSize() const
1959{
1960 return (mDataSize > mDataPos ? mDataSize : mDataPos);
1961}
1962
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001963uintptr_t Parcel::ipcObjects() const
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001964{
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001965 return reinterpret_cast<uintptr_t>(mObjects);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001966}
1967
1968size_t Parcel::ipcObjectsCount() const
1969{
1970 return mObjectsSize;
1971}
1972
1973void Parcel::ipcSetDataReference(const uint8_t* data, size_t dataSize,
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001974 const binder_size_t* objects, size_t objectsCount, release_func relFunc, void* relCookie)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001975{
Arve Hjønnevåg6f286112014-02-19 20:42:13 -08001976 binder_size_t minOffset = 0;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001977 freeDataNoInit();
1978 mError = NO_ERROR;
1979 mData = const_cast<uint8_t*>(data);
1980 mDataSize = mDataCapacity = dataSize;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001981 //ALOGI("setDataReference Setting data size of %p to %lu (pid=%d)", this, mDataSize, getpid());
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001982 mDataPos = 0;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001983 ALOGV("setDataReference Setting data pos of %p to %zu", this, mDataPos);
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001984 mObjects = const_cast<binder_size_t*>(objects);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001985 mObjectsSize = mObjectsCapacity = objectsCount;
1986 mNextObjectHint = 0;
1987 mOwner = relFunc;
1988 mOwnerCookie = relCookie;
Arve Hjønnevågf50b9ea2014-02-13 19:22:08 -08001989 for (size_t i = 0; i < mObjectsSize; i++) {
Arve Hjønnevåg6f286112014-02-19 20:42:13 -08001990 binder_size_t offset = mObjects[i];
Arve Hjønnevågf50b9ea2014-02-13 19:22:08 -08001991 if (offset < minOffset) {
Dan Albert3bdc5b82014-11-20 11:50:23 -08001992 ALOGE("%s: bad object offset %" PRIu64 " < %" PRIu64 "\n",
Arve Hjønnevåg6f286112014-02-19 20:42:13 -08001993 __func__, (uint64_t)offset, (uint64_t)minOffset);
Arve Hjønnevågf50b9ea2014-02-13 19:22:08 -08001994 mObjectsSize = 0;
1995 break;
1996 }
1997 minOffset = offset + sizeof(flat_binder_object);
1998 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001999 scanForFds();
2000}
2001
Colin Cross6f4f3ab2014-02-05 17:42:44 -08002002void Parcel::print(TextOutput& to, uint32_t /*flags*/) const
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002003{
2004 to << "Parcel(";
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002005
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002006 if (errorCheck() != NO_ERROR) {
2007 const status_t err = errorCheck();
Colin Cross6f4f3ab2014-02-05 17:42:44 -08002008 to << "Error: " << (void*)(intptr_t)err << " \"" << strerror(-err) << "\"";
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002009 } else if (dataSize() > 0) {
2010 const uint8_t* DATA = data();
2011 to << indent << HexDump(DATA, dataSize()) << dedent;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002012 const binder_size_t* OBJS = objects();
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002013 const size_t N = objectsCount();
2014 for (size_t i=0; i<N; i++) {
2015 const flat_binder_object* flat
2016 = reinterpret_cast<const flat_binder_object*>(DATA+OBJS[i]);
2017 to << endl << "Object #" << i << " @ " << (void*)OBJS[i] << ": "
2018 << TypeCode(flat->type & 0x7f7f7f00)
2019 << " = " << flat->binder;
2020 }
2021 } else {
2022 to << "NULL";
2023 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002024
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002025 to << ")";
2026}
2027
2028void Parcel::releaseObjects()
2029{
2030 const sp<ProcessState> proc(ProcessState::self());
2031 size_t i = mObjectsSize;
2032 uint8_t* const data = mData;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002033 binder_size_t* const objects = mObjects;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002034 while (i > 0) {
2035 i--;
2036 const flat_binder_object* flat
2037 = reinterpret_cast<flat_binder_object*>(data+objects[i]);
Adrian Rooscbf37262015-10-22 16:12:53 -07002038 release_object(proc, *flat, this, &mOpenAshmemSize);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002039 }
2040}
2041
2042void Parcel::acquireObjects()
2043{
2044 const sp<ProcessState> proc(ProcessState::self());
2045 size_t i = mObjectsSize;
2046 uint8_t* const data = mData;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002047 binder_size_t* const objects = mObjects;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002048 while (i > 0) {
2049 i--;
2050 const flat_binder_object* flat
2051 = reinterpret_cast<flat_binder_object*>(data+objects[i]);
Adrian Rooscbf37262015-10-22 16:12:53 -07002052 acquire_object(proc, *flat, this, &mOpenAshmemSize);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002053 }
2054}
2055
2056void Parcel::freeData()
2057{
2058 freeDataNoInit();
2059 initState();
2060}
2061
2062void Parcel::freeDataNoInit()
2063{
2064 if (mOwner) {
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002065 LOG_ALLOC("Parcel %p: freeing other owner data", this);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002066 //ALOGI("Freeing data ref of %p (pid=%d)", this, getpid());
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002067 mOwner(this, mData, mDataSize, mObjects, mObjectsSize, mOwnerCookie);
2068 } else {
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002069 LOG_ALLOC("Parcel %p: freeing allocated data", this);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002070 releaseObjects();
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002071 if (mData) {
2072 LOG_ALLOC("Parcel %p: freeing with %zu capacity", this, mDataCapacity);
Dianne Hackborna4cff882014-11-13 17:07:40 -08002073 pthread_mutex_lock(&gParcelGlobalAllocSizeLock);
Dan Austinf28b2952015-09-10 13:46:02 -07002074 if (mDataCapacity <= gParcelGlobalAllocSize) {
2075 gParcelGlobalAllocSize = gParcelGlobalAllocSize - mDataCapacity;
2076 } else {
2077 gParcelGlobalAllocSize = 0;
2078 }
2079 if (gParcelGlobalAllocCount > 0) {
2080 gParcelGlobalAllocCount--;
2081 }
Dianne Hackborna4cff882014-11-13 17:07:40 -08002082 pthread_mutex_unlock(&gParcelGlobalAllocSizeLock);
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002083 free(mData);
2084 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002085 if (mObjects) free(mObjects);
2086 }
2087}
2088
2089status_t Parcel::growData(size_t len)
2090{
Nick Kralevichb6b14232015-04-02 09:36:02 -07002091 if (len > INT32_MAX) {
2092 // don't accept size_t values which may have come from an
2093 // inadvertent conversion from a negative int.
2094 return BAD_VALUE;
2095 }
2096
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002097 size_t newSize = ((mDataSize+len)*3)/2;
2098 return (newSize <= mDataSize)
2099 ? (status_t) NO_MEMORY
2100 : continueWrite(newSize);
2101}
2102
2103status_t Parcel::restartWrite(size_t desired)
2104{
Nick Kralevichb6b14232015-04-02 09:36:02 -07002105 if (desired > INT32_MAX) {
2106 // don't accept size_t values which may have come from an
2107 // inadvertent conversion from a negative int.
2108 return BAD_VALUE;
2109 }
2110
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002111 if (mOwner) {
2112 freeData();
2113 return continueWrite(desired);
2114 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002115
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002116 uint8_t* data = (uint8_t*)realloc(mData, desired);
2117 if (!data && desired > mDataCapacity) {
2118 mError = NO_MEMORY;
2119 return NO_MEMORY;
2120 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002121
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002122 releaseObjects();
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002123
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002124 if (data) {
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002125 LOG_ALLOC("Parcel %p: restart from %zu to %zu capacity", this, mDataCapacity, desired);
Dianne Hackborna4cff882014-11-13 17:07:40 -08002126 pthread_mutex_lock(&gParcelGlobalAllocSizeLock);
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002127 gParcelGlobalAllocSize += desired;
2128 gParcelGlobalAllocSize -= mDataCapacity;
Dianne Hackborna4cff882014-11-13 17:07:40 -08002129 pthread_mutex_unlock(&gParcelGlobalAllocSizeLock);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002130 mData = data;
2131 mDataCapacity = desired;
2132 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002133
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002134 mDataSize = mDataPos = 0;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002135 ALOGV("restartWrite Setting data size of %p to %zu", this, mDataSize);
2136 ALOGV("restartWrite Setting data pos of %p to %zu", this, mDataPos);
2137
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002138 free(mObjects);
2139 mObjects = NULL;
2140 mObjectsSize = mObjectsCapacity = 0;
2141 mNextObjectHint = 0;
2142 mHasFds = false;
2143 mFdsKnown = true;
Dianne Hackborn8938ed22011-09-28 23:19:47 -04002144 mAllowFds = true;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002145
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002146 return NO_ERROR;
2147}
2148
2149status_t Parcel::continueWrite(size_t desired)
2150{
Nick Kralevichb6b14232015-04-02 09:36:02 -07002151 if (desired > INT32_MAX) {
2152 // don't accept size_t values which may have come from an
2153 // inadvertent conversion from a negative int.
2154 return BAD_VALUE;
2155 }
2156
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002157 // If shrinking, first adjust for any objects that appear
2158 // after the new data size.
2159 size_t objectsSize = mObjectsSize;
2160 if (desired < mDataSize) {
2161 if (desired == 0) {
2162 objectsSize = 0;
2163 } else {
2164 while (objectsSize > 0) {
2165 if (mObjects[objectsSize-1] < desired)
2166 break;
2167 objectsSize--;
2168 }
2169 }
2170 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002171
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002172 if (mOwner) {
2173 // If the size is going to zero, just release the owner's data.
2174 if (desired == 0) {
2175 freeData();
2176 return NO_ERROR;
2177 }
2178
2179 // If there is a different owner, we need to take
2180 // posession.
2181 uint8_t* data = (uint8_t*)malloc(desired);
2182 if (!data) {
2183 mError = NO_MEMORY;
2184 return NO_MEMORY;
2185 }
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002186 binder_size_t* objects = NULL;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002187
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002188 if (objectsSize) {
Nick Kraleviche9881a32015-04-28 16:21:30 -07002189 objects = (binder_size_t*)calloc(objectsSize, sizeof(binder_size_t));
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002190 if (!objects) {
Hyejin Kim3f727c02013-03-09 11:28:54 +09002191 free(data);
2192
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002193 mError = NO_MEMORY;
2194 return NO_MEMORY;
2195 }
2196
2197 // Little hack to only acquire references on objects
2198 // we will be keeping.
2199 size_t oldObjectsSize = mObjectsSize;
2200 mObjectsSize = objectsSize;
2201 acquireObjects();
2202 mObjectsSize = oldObjectsSize;
2203 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002204
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002205 if (mData) {
2206 memcpy(data, mData, mDataSize < desired ? mDataSize : desired);
2207 }
2208 if (objects && mObjects) {
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002209 memcpy(objects, mObjects, objectsSize*sizeof(binder_size_t));
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002210 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002211 //ALOGI("Freeing data ref of %p (pid=%d)", this, getpid());
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002212 mOwner(this, mData, mDataSize, mObjects, mObjectsSize, mOwnerCookie);
2213 mOwner = NULL;
2214
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002215 LOG_ALLOC("Parcel %p: taking ownership of %zu capacity", this, desired);
Dianne Hackborna4cff882014-11-13 17:07:40 -08002216 pthread_mutex_lock(&gParcelGlobalAllocSizeLock);
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002217 gParcelGlobalAllocSize += desired;
2218 gParcelGlobalAllocCount++;
Dianne Hackborna4cff882014-11-13 17:07:40 -08002219 pthread_mutex_unlock(&gParcelGlobalAllocSizeLock);
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002220
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002221 mData = data;
2222 mObjects = objects;
2223 mDataSize = (mDataSize < desired) ? mDataSize : desired;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002224 ALOGV("continueWrite Setting data size of %p to %zu", this, mDataSize);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002225 mDataCapacity = desired;
2226 mObjectsSize = mObjectsCapacity = objectsSize;
2227 mNextObjectHint = 0;
2228
2229 } else if (mData) {
2230 if (objectsSize < mObjectsSize) {
2231 // Need to release refs on any objects we are dropping.
2232 const sp<ProcessState> proc(ProcessState::self());
2233 for (size_t i=objectsSize; i<mObjectsSize; i++) {
2234 const flat_binder_object* flat
2235 = reinterpret_cast<flat_binder_object*>(mData+mObjects[i]);
2236 if (flat->type == BINDER_TYPE_FD) {
2237 // will need to rescan because we may have lopped off the only FDs
2238 mFdsKnown = false;
2239 }
Adrian Rooscbf37262015-10-22 16:12:53 -07002240 release_object(proc, *flat, this, &mOpenAshmemSize);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002241 }
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002242 binder_size_t* objects =
2243 (binder_size_t*)realloc(mObjects, objectsSize*sizeof(binder_size_t));
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002244 if (objects) {
2245 mObjects = objects;
2246 }
2247 mObjectsSize = objectsSize;
2248 mNextObjectHint = 0;
2249 }
2250
2251 // We own the data, so we can just do a realloc().
2252 if (desired > mDataCapacity) {
2253 uint8_t* data = (uint8_t*)realloc(mData, desired);
2254 if (data) {
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002255 LOG_ALLOC("Parcel %p: continue from %zu to %zu capacity", this, mDataCapacity,
2256 desired);
Dianne Hackborna4cff882014-11-13 17:07:40 -08002257 pthread_mutex_lock(&gParcelGlobalAllocSizeLock);
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002258 gParcelGlobalAllocSize += desired;
2259 gParcelGlobalAllocSize -= mDataCapacity;
Dan Austinf28b2952015-09-10 13:46:02 -07002260 gParcelGlobalAllocCount++;
Dianne Hackborna4cff882014-11-13 17:07:40 -08002261 pthread_mutex_unlock(&gParcelGlobalAllocSizeLock);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002262 mData = data;
2263 mDataCapacity = desired;
2264 } else if (desired > mDataCapacity) {
2265 mError = NO_MEMORY;
2266 return NO_MEMORY;
2267 }
2268 } else {
Dianne Hackborn97e2bcd2011-04-13 18:15:56 -07002269 if (mDataSize > desired) {
2270 mDataSize = desired;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002271 ALOGV("continueWrite Setting data size of %p to %zu", this, mDataSize);
Dianne Hackborn97e2bcd2011-04-13 18:15:56 -07002272 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002273 if (mDataPos > desired) {
2274 mDataPos = desired;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002275 ALOGV("continueWrite Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002276 }
2277 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002278
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002279 } else {
2280 // This is the first data. Easy!
2281 uint8_t* data = (uint8_t*)malloc(desired);
2282 if (!data) {
2283 mError = NO_MEMORY;
2284 return NO_MEMORY;
2285 }
Hyejin Kim3f727c02013-03-09 11:28:54 +09002286
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002287 if(!(mDataCapacity == 0 && mObjects == NULL
2288 && mObjectsCapacity == 0)) {
Colin Cross6f4f3ab2014-02-05 17:42:44 -08002289 ALOGE("continueWrite: %zu/%p/%zu/%zu", mDataCapacity, mObjects, mObjectsCapacity, desired);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002290 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002291
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002292 LOG_ALLOC("Parcel %p: allocating with %zu capacity", this, desired);
Dianne Hackborna4cff882014-11-13 17:07:40 -08002293 pthread_mutex_lock(&gParcelGlobalAllocSizeLock);
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002294 gParcelGlobalAllocSize += desired;
2295 gParcelGlobalAllocCount++;
Dianne Hackborna4cff882014-11-13 17:07:40 -08002296 pthread_mutex_unlock(&gParcelGlobalAllocSizeLock);
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002297
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002298 mData = data;
2299 mDataSize = mDataPos = 0;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002300 ALOGV("continueWrite Setting data size of %p to %zu", this, mDataSize);
2301 ALOGV("continueWrite Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002302 mDataCapacity = desired;
2303 }
2304
2305 return NO_ERROR;
2306}
2307
2308void Parcel::initState()
2309{
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002310 LOG_ALLOC("Parcel %p: initState", this);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002311 mError = NO_ERROR;
2312 mData = 0;
2313 mDataSize = 0;
2314 mDataCapacity = 0;
2315 mDataPos = 0;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002316 ALOGV("initState Setting data size of %p to %zu", this, mDataSize);
2317 ALOGV("initState Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002318 mObjects = NULL;
2319 mObjectsSize = 0;
2320 mObjectsCapacity = 0;
2321 mNextObjectHint = 0;
2322 mHasFds = false;
2323 mFdsKnown = true;
Dianne Hackborn8938ed22011-09-28 23:19:47 -04002324 mAllowFds = true;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002325 mOwner = NULL;
Adrian Rooscbf37262015-10-22 16:12:53 -07002326 mOpenAshmemSize = 0;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002327}
2328
2329void Parcel::scanForFds() const
2330{
2331 bool hasFds = false;
2332 for (size_t i=0; i<mObjectsSize; i++) {
2333 const flat_binder_object* flat
2334 = reinterpret_cast<const flat_binder_object*>(mData + mObjects[i]);
2335 if (flat->type == BINDER_TYPE_FD) {
2336 hasFds = true;
2337 break;
2338 }
2339 }
2340 mHasFds = hasFds;
2341 mFdsKnown = true;
2342}
2343
Dan Sandleraa5c2342015-04-10 10:08:45 -04002344size_t Parcel::getBlobAshmemSize() const
2345{
Adrian Roos6bb31142015-10-22 16:46:12 -07002346 // This used to return the size of all blobs that were written to ashmem, now we're returning
2347 // the ashmem currently referenced by this Parcel, which should be equivalent.
2348 // TODO: Remove method once ABI can be changed.
2349 return mOpenAshmemSize;
Dan Sandleraa5c2342015-04-10 10:08:45 -04002350}
2351
Adrian Rooscbf37262015-10-22 16:12:53 -07002352size_t Parcel::getOpenAshmemSize() const
2353{
2354 return mOpenAshmemSize;
2355}
2356
Jeff Brown5707dbf2011-09-23 21:17:56 -07002357// --- Parcel::Blob ---
2358
2359Parcel::Blob::Blob() :
Jeff Brown13b16042014-11-11 16:44:25 -08002360 mFd(-1), mData(NULL), mSize(0), mMutable(false) {
Jeff Brown5707dbf2011-09-23 21:17:56 -07002361}
2362
2363Parcel::Blob::~Blob() {
2364 release();
2365}
2366
2367void Parcel::Blob::release() {
Jeff Brown13b16042014-11-11 16:44:25 -08002368 if (mFd != -1 && mData) {
Jeff Brown5707dbf2011-09-23 21:17:56 -07002369 ::munmap(mData, mSize);
2370 }
2371 clear();
2372}
2373
Jeff Brown13b16042014-11-11 16:44:25 -08002374void Parcel::Blob::init(int fd, void* data, size_t size, bool isMutable) {
2375 mFd = fd;
Jeff Brown5707dbf2011-09-23 21:17:56 -07002376 mData = data;
2377 mSize = size;
Jeff Brown13b16042014-11-11 16:44:25 -08002378 mMutable = isMutable;
Jeff Brown5707dbf2011-09-23 21:17:56 -07002379}
2380
2381void Parcel::Blob::clear() {
Jeff Brown13b16042014-11-11 16:44:25 -08002382 mFd = -1;
Jeff Brown5707dbf2011-09-23 21:17:56 -07002383 mData = NULL;
2384 mSize = 0;
Jeff Brown13b16042014-11-11 16:44:25 -08002385 mMutable = false;
Jeff Brown5707dbf2011-09-23 21:17:56 -07002386}
2387
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002388}; // namespace android