blob: 572c2845462c250504f8a68d5d04fa7ba5c62da9 [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
Mark Salyzynabed7f72016-01-27 08:02:48 -080020#include <errno.h>
Mark Salyzyn70f36652016-02-02 10:27:03 -080021#include <fcntl.h>
Mark Salyzynabed7f72016-01-27 08:02:48 -080022#include <inttypes.h>
Mark Salyzyn70f36652016-02-02 10:27:03 -080023#include <pthread.h>
Mark Salyzynabed7f72016-01-27 08:02:48 -080024#include <stdint.h>
25#include <stdio.h>
26#include <stdlib.h>
27#include <sys/mman.h>
Mark Salyzyneab2afc2016-01-27 08:02:48 -080028#include <sys/stat.h>
29#include <sys/types.h>
Christopher Tatee4e0ae82016-03-24 16:03:44 -070030#include <sys/resource.h>
Mark Salyzyneab2afc2016-01-27 08:02:48 -080031#include <unistd.h>
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -070032
Mathias Agopianc5b2c0b2009-05-19 19:08:10 -070033#include <binder/Binder.h>
34#include <binder/BpBinder.h>
Mark Salyzynabed7f72016-01-27 08:02:48 -080035#include <binder/IPCThreadState.h>
36#include <binder/Parcel.h>
Mathias Agopianc5b2c0b2009-05-19 19:08:10 -070037#include <binder/ProcessState.h>
Christopher Wiley09eb7492015-11-09 15:06:15 -080038#include <binder/Status.h>
Mathias Agopian002e1e52013-05-06 20:20:50 -070039#include <binder/TextOutput.h>
40
Mark Salyzynabed7f72016-01-27 08:02:48 -080041#include <cutils/ashmem.h>
Mathias Agopian002e1e52013-05-06 20:20:50 -070042#include <utils/Debug.h>
Mark Salyzynabed7f72016-01-27 08:02:48 -080043#include <utils/Flattenable.h>
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -070044#include <utils/Log.h>
Mark Salyzynabed7f72016-01-27 08:02:48 -080045#include <utils/misc.h>
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -070046#include <utils/String8.h>
47#include <utils/String16.h>
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -070048
Mathias Agopian208059f2009-05-18 15:08:03 -070049#include <private/binder/binder_module.h>
Dianne Hackborn7e790af2014-11-11 12:22:53 -080050#include <private/binder/Static.h>
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -070051
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -070052#ifndef INT32_MAX
53#define INT32_MAX ((int32_t)(2147483647))
54#endif
55
56#define LOG_REFS(...)
Mark Salyzyne93390b2016-01-27 08:02:48 -080057//#define LOG_REFS(...) ALOG(LOG_DEBUG, LOG_TAG, __VA_ARGS__)
Dianne Hackborn7e790af2014-11-11 12:22:53 -080058#define LOG_ALLOC(...)
Mark Salyzyne93390b2016-01-27 08:02:48 -080059//#define LOG_ALLOC(...) ALOG(LOG_DEBUG, LOG_TAG, __VA_ARGS__)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -070060
61// ---------------------------------------------------------------------------
62
Nick Kralevichb6b14232015-04-02 09:36:02 -070063// This macro should never be used at runtime, as a too large value
64// of s could cause an integer overflow. Instead, you should always
65// use the wrapper function pad_size()
66#define PAD_SIZE_UNSAFE(s) (((s)+3)&~3)
67
68static size_t pad_size(size_t s) {
69 if (s > (SIZE_T_MAX - 3)) {
70 abort();
71 }
72 return PAD_SIZE_UNSAFE(s);
73}
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -070074
Brad Fitzpatricka877cd82010-07-07 16:06:39 -070075// Note: must be kept in sync with android/os/StrictMode.java's PENALTY_GATHER
Jeff Sharkey0c1f5cb2014-12-18 10:26:57 -080076#define STRICT_MODE_PENALTY_GATHER (0x40 << 16)
Brad Fitzpatricka877cd82010-07-07 16:06:39 -070077
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -070078// XXX This can be made public if we want to provide
79// support for typed data.
80struct small_flat_data
81{
82 uint32_t type;
83 uint32_t data;
84};
85
86namespace android {
87
Dianne Hackborna4cff882014-11-13 17:07:40 -080088static pthread_mutex_t gParcelGlobalAllocSizeLock = PTHREAD_MUTEX_INITIALIZER;
89static size_t gParcelGlobalAllocSize = 0;
90static size_t gParcelGlobalAllocCount = 0;
91
Christopher Tatee4e0ae82016-03-24 16:03:44 -070092static size_t gMaxFds = 0;
93
Jeff Brown13b16042014-11-11 16:44:25 -080094// Maximum size of a blob to transfer in-place.
95static const size_t BLOB_INPLACE_LIMIT = 16 * 1024;
96
97enum {
98 BLOB_INPLACE = 0,
99 BLOB_ASHMEM_IMMUTABLE = 1,
100 BLOB_ASHMEM_MUTABLE = 2,
101};
102
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700103void acquire_object(const sp<ProcessState>& proc,
Adrian Rooscbf37262015-10-22 16:12:53 -0700104 const flat_binder_object& obj, const void* who, size_t* outAshmemSize)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700105{
106 switch (obj.type) {
107 case BINDER_TYPE_BINDER:
108 if (obj.binder) {
109 LOG_REFS("Parcel %p acquiring reference on local %p", who, obj.cookie);
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800110 reinterpret_cast<IBinder*>(obj.cookie)->incStrong(who);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700111 }
112 return;
113 case BINDER_TYPE_WEAK_BINDER:
114 if (obj.binder)
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800115 reinterpret_cast<RefBase::weakref_type*>(obj.binder)->incWeak(who);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700116 return;
117 case BINDER_TYPE_HANDLE: {
118 const sp<IBinder> b = proc->getStrongProxyForHandle(obj.handle);
119 if (b != NULL) {
120 LOG_REFS("Parcel %p acquiring reference on remote %p", who, b.get());
121 b->incStrong(who);
122 }
123 return;
124 }
125 case BINDER_TYPE_WEAK_HANDLE: {
126 const wp<IBinder> b = proc->getWeakProxyForHandle(obj.handle);
127 if (b != NULL) b.get_refs()->incWeak(who);
128 return;
129 }
130 case BINDER_TYPE_FD: {
Mark Salyzyn80589362016-08-23 16:15:04 -0700131 if ((obj.cookie != 0) && (outAshmemSize != NULL) && ashmem_valid(obj.handle)) {
132 // If we own an ashmem fd, keep track of how much memory it refers to.
133 int size = ashmem_get_size_region(obj.handle);
134 if (size > 0) {
135 *outAshmemSize += size;
Adrian Rooscbf37262015-10-22 16:12:53 -0700136 }
137 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700138 return;
139 }
140 }
141
Colin Cross6f4f3ab2014-02-05 17:42:44 -0800142 ALOGD("Invalid object type 0x%08x", obj.type);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700143}
144
Adrian Roos6bb31142015-10-22 16:46:12 -0700145void acquire_object(const sp<ProcessState>& proc,
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700146 const flat_binder_object& obj, const void* who)
147{
Adrian Roos6bb31142015-10-22 16:46:12 -0700148 acquire_object(proc, obj, who, NULL);
149}
150
151static void release_object(const sp<ProcessState>& proc,
Adrian Rooscbf37262015-10-22 16:12:53 -0700152 const flat_binder_object& obj, const void* who, size_t* outAshmemSize)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700153{
154 switch (obj.type) {
155 case BINDER_TYPE_BINDER:
156 if (obj.binder) {
157 LOG_REFS("Parcel %p releasing reference on local %p", who, obj.cookie);
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800158 reinterpret_cast<IBinder*>(obj.cookie)->decStrong(who);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700159 }
160 return;
161 case BINDER_TYPE_WEAK_BINDER:
162 if (obj.binder)
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800163 reinterpret_cast<RefBase::weakref_type*>(obj.binder)->decWeak(who);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700164 return;
165 case BINDER_TYPE_HANDLE: {
166 const sp<IBinder> b = proc->getStrongProxyForHandle(obj.handle);
167 if (b != NULL) {
168 LOG_REFS("Parcel %p releasing reference on remote %p", who, b.get());
169 b->decStrong(who);
170 }
171 return;
172 }
173 case BINDER_TYPE_WEAK_HANDLE: {
174 const wp<IBinder> b = proc->getWeakProxyForHandle(obj.handle);
175 if (b != NULL) b.get_refs()->decWeak(who);
176 return;
177 }
178 case BINDER_TYPE_FD: {
Mark Salyzynb454d8f2016-01-27 08:02:48 -0800179 if (obj.cookie != 0) { // owned
Mark Salyzyn80589362016-08-23 16:15:04 -0700180 if ((outAshmemSize != NULL) && ashmem_valid(obj.handle)) {
181 int size = ashmem_get_size_region(obj.handle);
182 if (size > 0) {
183 *outAshmemSize -= size;
Adrian Roos6bb31142015-10-22 16:46:12 -0700184 }
Adrian Roos6bb31142015-10-22 16:46:12 -0700185 }
Mark Salyzynb454d8f2016-01-27 08:02:48 -0800186
187 close(obj.handle);
Adrian Rooscbf37262015-10-22 16:12:53 -0700188 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700189 return;
190 }
191 }
192
Colin Cross6f4f3ab2014-02-05 17:42:44 -0800193 ALOGE("Invalid object type 0x%08x", obj.type);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700194}
195
Adrian Roos6bb31142015-10-22 16:46:12 -0700196void release_object(const sp<ProcessState>& proc,
197 const flat_binder_object& obj, const void* who)
198{
199 release_object(proc, obj, who, NULL);
200}
201
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700202inline static status_t finish_flatten_binder(
Colin Cross6f4f3ab2014-02-05 17:42:44 -0800203 const sp<IBinder>& /*binder*/, const flat_binder_object& flat, Parcel* out)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700204{
205 return out->writeObject(flat, false);
206}
207
Colin Cross6f4f3ab2014-02-05 17:42:44 -0800208status_t flatten_binder(const sp<ProcessState>& /*proc*/,
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700209 const sp<IBinder>& binder, Parcel* out)
210{
211 flat_binder_object obj;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700212
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700213 obj.flags = 0x7f | FLAT_BINDER_FLAG_ACCEPTS_FDS;
214 if (binder != NULL) {
215 IBinder *local = binder->localBinder();
216 if (!local) {
217 BpBinder *proxy = binder->remoteBinder();
218 if (proxy == NULL) {
Steve Blocke6f43dd2012-01-06 19:20:56 +0000219 ALOGE("null proxy");
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700220 }
221 const int32_t handle = proxy ? proxy->handle() : 0;
222 obj.type = BINDER_TYPE_HANDLE;
Arve Hjønnevåg07fd0f12014-02-18 21:10:29 -0800223 obj.binder = 0; /* Don't pass uninitialized stack data to a remote process */
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700224 obj.handle = handle;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800225 obj.cookie = 0;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700226 } else {
227 obj.type = BINDER_TYPE_BINDER;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800228 obj.binder = reinterpret_cast<uintptr_t>(local->getWeakRefs());
229 obj.cookie = reinterpret_cast<uintptr_t>(local);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700230 }
231 } else {
232 obj.type = BINDER_TYPE_BINDER;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800233 obj.binder = 0;
234 obj.cookie = 0;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700235 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700236
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700237 return finish_flatten_binder(binder, obj, out);
238}
239
Colin Cross6f4f3ab2014-02-05 17:42:44 -0800240status_t flatten_binder(const sp<ProcessState>& /*proc*/,
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700241 const wp<IBinder>& binder, Parcel* out)
242{
243 flat_binder_object obj;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700244
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700245 obj.flags = 0x7f | FLAT_BINDER_FLAG_ACCEPTS_FDS;
246 if (binder != NULL) {
247 sp<IBinder> real = binder.promote();
248 if (real != NULL) {
249 IBinder *local = real->localBinder();
250 if (!local) {
251 BpBinder *proxy = real->remoteBinder();
252 if (proxy == NULL) {
Steve Blocke6f43dd2012-01-06 19:20:56 +0000253 ALOGE("null proxy");
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700254 }
255 const int32_t handle = proxy ? proxy->handle() : 0;
256 obj.type = BINDER_TYPE_WEAK_HANDLE;
Arve Hjønnevåg07fd0f12014-02-18 21:10:29 -0800257 obj.binder = 0; /* Don't pass uninitialized stack data to a remote process */
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700258 obj.handle = handle;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800259 obj.cookie = 0;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700260 } else {
261 obj.type = BINDER_TYPE_WEAK_BINDER;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800262 obj.binder = reinterpret_cast<uintptr_t>(binder.get_refs());
263 obj.cookie = reinterpret_cast<uintptr_t>(binder.unsafe_get());
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700264 }
265 return finish_flatten_binder(real, obj, out);
266 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700267
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700268 // XXX How to deal? In order to flatten the given binder,
269 // we need to probe it for information, which requires a primary
270 // reference... but we don't have one.
271 //
272 // The OpenBinder implementation uses a dynamic_cast<> here,
273 // but we can't do that with the different reference counting
274 // implementation we are using.
Steve Blocke6f43dd2012-01-06 19:20:56 +0000275 ALOGE("Unable to unflatten Binder weak reference!");
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700276 obj.type = BINDER_TYPE_BINDER;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800277 obj.binder = 0;
278 obj.cookie = 0;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700279 return finish_flatten_binder(NULL, obj, out);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700280
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700281 } else {
282 obj.type = BINDER_TYPE_BINDER;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800283 obj.binder = 0;
284 obj.cookie = 0;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700285 return finish_flatten_binder(NULL, obj, out);
286 }
287}
288
289inline static status_t finish_unflatten_binder(
Colin Cross6f4f3ab2014-02-05 17:42:44 -0800290 BpBinder* /*proxy*/, const flat_binder_object& /*flat*/,
291 const Parcel& /*in*/)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700292{
293 return NO_ERROR;
294}
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700295
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700296status_t unflatten_binder(const sp<ProcessState>& proc,
297 const Parcel& in, sp<IBinder>* out)
298{
299 const flat_binder_object* flat = in.readObject(false);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700300
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700301 if (flat) {
302 switch (flat->type) {
303 case BINDER_TYPE_BINDER:
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800304 *out = reinterpret_cast<IBinder*>(flat->cookie);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700305 return finish_unflatten_binder(NULL, *flat, in);
306 case BINDER_TYPE_HANDLE:
307 *out = proc->getStrongProxyForHandle(flat->handle);
308 return finish_unflatten_binder(
309 static_cast<BpBinder*>(out->get()), *flat, in);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700310 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700311 }
312 return BAD_TYPE;
313}
314
315status_t unflatten_binder(const sp<ProcessState>& proc,
316 const Parcel& in, wp<IBinder>* out)
317{
318 const flat_binder_object* flat = in.readObject(false);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700319
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700320 if (flat) {
321 switch (flat->type) {
322 case BINDER_TYPE_BINDER:
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800323 *out = reinterpret_cast<IBinder*>(flat->cookie);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700324 return finish_unflatten_binder(NULL, *flat, in);
325 case BINDER_TYPE_WEAK_BINDER:
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800326 if (flat->binder != 0) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700327 out->set_object_and_refs(
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800328 reinterpret_cast<IBinder*>(flat->cookie),
329 reinterpret_cast<RefBase::weakref_type*>(flat->binder));
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700330 } else {
331 *out = NULL;
332 }
333 return finish_unflatten_binder(NULL, *flat, in);
334 case BINDER_TYPE_HANDLE:
335 case BINDER_TYPE_WEAK_HANDLE:
336 *out = proc->getWeakProxyForHandle(flat->handle);
337 return finish_unflatten_binder(
338 static_cast<BpBinder*>(out->unsafe_get()), *flat, in);
339 }
340 }
341 return BAD_TYPE;
342}
343
344// ---------------------------------------------------------------------------
345
346Parcel::Parcel()
347{
Dianne Hackborn7e790af2014-11-11 12:22:53 -0800348 LOG_ALLOC("Parcel %p: constructing", this);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700349 initState();
350}
351
352Parcel::~Parcel()
353{
354 freeDataNoInit();
Dianne Hackborn7e790af2014-11-11 12:22:53 -0800355 LOG_ALLOC("Parcel %p: destroyed", this);
356}
357
358size_t Parcel::getGlobalAllocSize() {
Dianne Hackborna4cff882014-11-13 17:07:40 -0800359 pthread_mutex_lock(&gParcelGlobalAllocSizeLock);
360 size_t size = gParcelGlobalAllocSize;
361 pthread_mutex_unlock(&gParcelGlobalAllocSizeLock);
362 return size;
Dianne Hackborn7e790af2014-11-11 12:22:53 -0800363}
364
365size_t Parcel::getGlobalAllocCount() {
Dianne Hackborna4cff882014-11-13 17:07:40 -0800366 pthread_mutex_lock(&gParcelGlobalAllocSizeLock);
367 size_t count = gParcelGlobalAllocCount;
368 pthread_mutex_unlock(&gParcelGlobalAllocSizeLock);
369 return count;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700370}
371
372const uint8_t* Parcel::data() const
373{
374 return mData;
375}
376
377size_t Parcel::dataSize() const
378{
379 return (mDataSize > mDataPos ? mDataSize : mDataPos);
380}
381
382size_t Parcel::dataAvail() const
383{
Nick Kralevichcfe27de2015-09-16 09:49:15 -0700384 size_t result = dataSize() - dataPosition();
385 if (result > INT32_MAX) {
386 abort();
387 }
388 return result;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700389}
390
391size_t Parcel::dataPosition() const
392{
393 return mDataPos;
394}
395
396size_t Parcel::dataCapacity() const
397{
398 return mDataCapacity;
399}
400
401status_t Parcel::setDataSize(size_t size)
402{
Nick Kralevichb6b14232015-04-02 09:36:02 -0700403 if (size > INT32_MAX) {
404 // don't accept size_t values which may have come from an
405 // inadvertent conversion from a negative int.
406 return BAD_VALUE;
407 }
408
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700409 status_t err;
410 err = continueWrite(size);
411 if (err == NO_ERROR) {
412 mDataSize = size;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700413 ALOGV("setDataSize Setting data size of %p to %zu", this, mDataSize);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700414 }
415 return err;
416}
417
418void Parcel::setDataPosition(size_t pos) const
419{
Nick Kralevichb6b14232015-04-02 09:36:02 -0700420 if (pos > INT32_MAX) {
421 // don't accept size_t values which may have come from an
422 // inadvertent conversion from a negative int.
423 abort();
424 }
425
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700426 mDataPos = pos;
427 mNextObjectHint = 0;
428}
429
430status_t Parcel::setDataCapacity(size_t size)
431{
Nick Kralevichb6b14232015-04-02 09:36:02 -0700432 if (size > INT32_MAX) {
433 // don't accept size_t values which may have come from an
434 // inadvertent conversion from a negative int.
435 return BAD_VALUE;
436 }
437
Dianne Hackborn97e2bcd2011-04-13 18:15:56 -0700438 if (size > mDataCapacity) return continueWrite(size);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700439 return NO_ERROR;
440}
441
442status_t Parcel::setData(const uint8_t* buffer, size_t len)
443{
Nick Kralevichb6b14232015-04-02 09:36:02 -0700444 if (len > INT32_MAX) {
445 // don't accept size_t values which may have come from an
446 // inadvertent conversion from a negative int.
447 return BAD_VALUE;
448 }
449
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700450 status_t err = restartWrite(len);
451 if (err == NO_ERROR) {
452 memcpy(const_cast<uint8_t*>(data()), buffer, len);
453 mDataSize = len;
454 mFdsKnown = false;
455 }
456 return err;
457}
458
Andreas Huber51faf462011-04-13 10:21:56 -0700459status_t Parcel::appendFrom(const Parcel *parcel, size_t offset, size_t len)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700460{
461 const sp<ProcessState> proc(ProcessState::self());
462 status_t err;
Andreas Huber51faf462011-04-13 10:21:56 -0700463 const uint8_t *data = parcel->mData;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800464 const binder_size_t *objects = parcel->mObjects;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700465 size_t size = parcel->mObjectsSize;
466 int startPos = mDataPos;
467 int firstIndex = -1, lastIndex = -2;
468
469 if (len == 0) {
470 return NO_ERROR;
471 }
472
Nick Kralevichb6b14232015-04-02 09:36:02 -0700473 if (len > INT32_MAX) {
474 // don't accept size_t values which may have come from an
475 // inadvertent conversion from a negative int.
476 return BAD_VALUE;
477 }
478
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700479 // range checks against the source parcel size
480 if ((offset > parcel->mDataSize)
481 || (len > parcel->mDataSize)
482 || (offset + len > parcel->mDataSize)) {
483 return BAD_VALUE;
484 }
485
486 // Count objects in range
487 for (int i = 0; i < (int) size; i++) {
488 size_t off = objects[i];
Christopher Tate27182be2015-05-27 17:53:02 -0700489 if ((off >= offset) && (off + sizeof(flat_binder_object) <= offset + len)) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700490 if (firstIndex == -1) {
491 firstIndex = i;
492 }
493 lastIndex = i;
494 }
495 }
496 int numObjects = lastIndex - firstIndex + 1;
497
Dianne Hackborn97e2bcd2011-04-13 18:15:56 -0700498 if ((mDataSize+len) > mDataCapacity) {
499 // grow data
500 err = growData(len);
501 if (err != NO_ERROR) {
502 return err;
503 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700504 }
505
506 // append data
507 memcpy(mData + mDataPos, data + offset, len);
508 mDataPos += len;
509 mDataSize += len;
510
Dianne Hackborn8938ed22011-09-28 23:19:47 -0400511 err = NO_ERROR;
512
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700513 if (numObjects > 0) {
514 // grow objects
515 if (mObjectsCapacity < mObjectsSize + numObjects) {
Christopher Tateed7a50c2015-06-08 14:45:14 -0700516 size_t newSize = ((mObjectsSize + numObjects)*3)/2;
517 if (newSize < mObjectsSize) return NO_MEMORY; // overflow
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800518 binder_size_t *objects =
519 (binder_size_t*)realloc(mObjects, newSize*sizeof(binder_size_t));
520 if (objects == (binder_size_t*)0) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700521 return NO_MEMORY;
522 }
523 mObjects = objects;
524 mObjectsCapacity = newSize;
525 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700526
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700527 // append and acquire objects
528 int idx = mObjectsSize;
529 for (int i = firstIndex; i <= lastIndex; i++) {
530 size_t off = objects[i] - offset + startPos;
531 mObjects[idx++] = off;
532 mObjectsSize++;
533
Dianne Hackborn8af0f822009-05-22 13:20:23 -0700534 flat_binder_object* flat
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700535 = reinterpret_cast<flat_binder_object*>(mData + off);
Adrian Rooscbf37262015-10-22 16:12:53 -0700536 acquire_object(proc, *flat, this, &mOpenAshmemSize);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700537
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700538 if (flat->type == BINDER_TYPE_FD) {
Dianne Hackborn8af0f822009-05-22 13:20:23 -0700539 // If this is a file descriptor, we need to dup it so the
540 // new Parcel now owns its own fd, and can declare that we
541 // officially know we have fds.
542 flat->handle = dup(flat->handle);
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800543 flat->cookie = 1;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700544 mHasFds = mFdsKnown = true;
Dianne Hackborn8938ed22011-09-28 23:19:47 -0400545 if (!mAllowFds) {
546 err = FDS_NOT_ALLOWED;
547 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700548 }
549 }
550 }
551
Dianne Hackborn8938ed22011-09-28 23:19:47 -0400552 return err;
553}
554
Jeff Brown13b16042014-11-11 16:44:25 -0800555bool Parcel::allowFds() const
556{
557 return mAllowFds;
558}
559
Dianne Hackborn7746cc32011-10-03 21:09:35 -0700560bool Parcel::pushAllowFds(bool allowFds)
Dianne Hackborn8938ed22011-09-28 23:19:47 -0400561{
562 const bool origValue = mAllowFds;
Dianne Hackborn7746cc32011-10-03 21:09:35 -0700563 if (!allowFds) {
564 mAllowFds = false;
565 }
Dianne Hackborn8938ed22011-09-28 23:19:47 -0400566 return origValue;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700567}
568
Dianne Hackborn7746cc32011-10-03 21:09:35 -0700569void Parcel::restoreAllowFds(bool lastValue)
570{
571 mAllowFds = lastValue;
572}
573
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700574bool Parcel::hasFileDescriptors() const
575{
576 if (!mFdsKnown) {
577 scanForFds();
578 }
579 return mHasFds;
580}
581
Brad Fitzpatrick702ea9d2010-06-18 13:07:53 -0700582// Write RPC headers. (previously just the interface token)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700583status_t Parcel::writeInterfaceToken(const String16& interface)
584{
Brad Fitzpatricka877cd82010-07-07 16:06:39 -0700585 writeInt32(IPCThreadState::self()->getStrictModePolicy() |
586 STRICT_MODE_PENALTY_GATHER);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700587 // currently the interface identification token is just its name as a string
588 return writeString16(interface);
589}
590
Mathias Agopian83c04462009-05-22 19:00:22 -0700591bool Parcel::checkInterface(IBinder* binder) const
592{
Brad Fitzpatrick702ea9d2010-06-18 13:07:53 -0700593 return enforceInterface(binder->getInterfaceDescriptor());
Mathias Agopian83c04462009-05-22 19:00:22 -0700594}
595
Brad Fitzpatricka877cd82010-07-07 16:06:39 -0700596bool Parcel::enforceInterface(const String16& interface,
Brad Fitzpatrick70081a12010-07-27 09:49:11 -0700597 IPCThreadState* threadState) const
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700598{
Brad Fitzpatrick70081a12010-07-27 09:49:11 -0700599 int32_t strictPolicy = readInt32();
600 if (threadState == NULL) {
601 threadState = IPCThreadState::self();
Brad Fitzpatricka877cd82010-07-07 16:06:39 -0700602 }
Brad Fitzpatrick52736032010-08-30 16:01:16 -0700603 if ((threadState->getLastTransactionBinderFlags() &
604 IBinder::FLAG_ONEWAY) != 0) {
605 // For one-way calls, the callee is running entirely
606 // disconnected from the caller, so disable StrictMode entirely.
607 // Not only does disk/network usage not impact the caller, but
608 // there's no way to commuicate back any violations anyway.
609 threadState->setStrictModePolicy(0);
610 } else {
611 threadState->setStrictModePolicy(strictPolicy);
612 }
Mathias Agopian83c04462009-05-22 19:00:22 -0700613 const String16 str(readString16());
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700614 if (str == interface) {
615 return true;
616 } else {
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700617 ALOGW("**** enforceInterface() expected '%s' but read '%s'",
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700618 String8(interface).string(), String8(str).string());
619 return false;
620 }
Brad Fitzpatrick702ea9d2010-06-18 13:07:53 -0700621}
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700622
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800623const binder_size_t* Parcel::objects() const
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700624{
625 return mObjects;
626}
627
628size_t Parcel::objectsCount() const
629{
630 return mObjectsSize;
631}
632
633status_t Parcel::errorCheck() const
634{
635 return mError;
636}
637
638void Parcel::setError(status_t err)
639{
640 mError = err;
641}
642
643status_t Parcel::finishWrite(size_t len)
644{
Nick Kralevichb6b14232015-04-02 09:36:02 -0700645 if (len > INT32_MAX) {
646 // don't accept size_t values which may have come from an
647 // inadvertent conversion from a negative int.
648 return BAD_VALUE;
649 }
650
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700651 //printf("Finish write of %d\n", len);
652 mDataPos += len;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700653 ALOGV("finishWrite Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700654 if (mDataPos > mDataSize) {
655 mDataSize = mDataPos;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700656 ALOGV("finishWrite Setting data size of %p to %zu", this, mDataSize);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700657 }
658 //printf("New pos=%d, size=%d\n", mDataPos, mDataSize);
659 return NO_ERROR;
660}
661
662status_t Parcel::writeUnpadded(const void* data, size_t len)
663{
Nick Kralevichb6b14232015-04-02 09:36:02 -0700664 if (len > INT32_MAX) {
665 // don't accept size_t values which may have come from an
666 // inadvertent conversion from a negative int.
667 return BAD_VALUE;
668 }
669
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700670 size_t end = mDataPos + len;
671 if (end < mDataPos) {
672 // integer overflow
673 return BAD_VALUE;
674 }
675
676 if (end <= mDataCapacity) {
677restart_write:
678 memcpy(mData+mDataPos, data, len);
679 return finishWrite(len);
680 }
681
682 status_t err = growData(len);
683 if (err == NO_ERROR) goto restart_write;
684 return err;
685}
686
687status_t Parcel::write(const void* data, size_t len)
688{
Nick Kralevichb6b14232015-04-02 09:36:02 -0700689 if (len > INT32_MAX) {
690 // don't accept size_t values which may have come from an
691 // inadvertent conversion from a negative int.
692 return BAD_VALUE;
693 }
694
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700695 void* const d = writeInplace(len);
696 if (d) {
697 memcpy(d, data, len);
698 return NO_ERROR;
699 }
700 return mError;
701}
702
703void* Parcel::writeInplace(size_t len)
704{
Nick Kralevichb6b14232015-04-02 09:36:02 -0700705 if (len > INT32_MAX) {
706 // don't accept size_t values which may have come from an
707 // inadvertent conversion from a negative int.
708 return NULL;
709 }
710
711 const size_t padded = pad_size(len);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700712
713 // sanity check for integer overflow
714 if (mDataPos+padded < mDataPos) {
715 return NULL;
716 }
717
718 if ((mDataPos+padded) <= mDataCapacity) {
719restart_write:
720 //printf("Writing %ld bytes, padded to %ld\n", len, padded);
721 uint8_t* const data = mData+mDataPos;
722
723 // Need to pad at end?
724 if (padded != len) {
725#if BYTE_ORDER == BIG_ENDIAN
726 static const uint32_t mask[4] = {
727 0x00000000, 0xffffff00, 0xffff0000, 0xff000000
728 };
729#endif
730#if BYTE_ORDER == LITTLE_ENDIAN
731 static const uint32_t mask[4] = {
732 0x00000000, 0x00ffffff, 0x0000ffff, 0x000000ff
733 };
734#endif
735 //printf("Applying pad mask: %p to %p\n", (void*)mask[padded-len],
736 // *reinterpret_cast<void**>(data+padded-4));
737 *reinterpret_cast<uint32_t*>(data+padded-4) &= mask[padded-len];
738 }
739
740 finishWrite(padded);
741 return data;
742 }
743
744 status_t err = growData(padded);
745 if (err == NO_ERROR) goto restart_write;
746 return NULL;
747}
748
Christopher Wiley9a5e32f2016-01-28 16:56:53 -0800749status_t Parcel::writeUtf8AsUtf16(const std::string& str) {
750 const uint8_t* strData = (uint8_t*)str.data();
751 const size_t strLen= str.length();
752 const ssize_t utf16Len = utf8_to_utf16_length(strData, strLen);
Sergio Girof4607432016-07-21 14:46:35 +0100753 if (utf16Len < 0 || utf16Len > std::numeric_limits<int32_t>::max()) {
Christopher Wiley9a5e32f2016-01-28 16:56:53 -0800754 return BAD_VALUE;
755 }
756
757 status_t err = writeInt32(utf16Len);
758 if (err) {
759 return err;
760 }
761
762 // Allocate enough bytes to hold our converted string and its terminating NULL.
763 void* dst = writeInplace((utf16Len + 1) * sizeof(char16_t));
764 if (!dst) {
765 return NO_MEMORY;
766 }
767
Sergio Girof4607432016-07-21 14:46:35 +0100768 utf8_to_utf16(strData, strLen, (char16_t*)dst, (size_t) utf16Len + 1);
Christopher Wiley9a5e32f2016-01-28 16:56:53 -0800769
770 return NO_ERROR;
771}
772
773status_t Parcel::writeUtf8AsUtf16(const std::unique_ptr<std::string>& str) {
774 if (!str) {
775 return writeInt32(-1);
776 }
777 return writeUtf8AsUtf16(*str);
778}
779
Casey Dahlin185d3442016-02-09 11:08:35 -0800780namespace {
Casey Dahlinb9872622015-11-25 15:09:45 -0800781
Casey Dahlin185d3442016-02-09 11:08:35 -0800782template<typename T>
783status_t writeByteVectorInternal(Parcel* parcel, const std::vector<T>& val)
Casey Dahlin451ff582015-10-19 18:12:18 -0700784{
Christopher Wileyf0fc52b2015-10-31 13:22:15 -0700785 status_t status;
Casey Dahlin451ff582015-10-19 18:12:18 -0700786 if (val.size() > std::numeric_limits<int32_t>::max()) {
Christopher Wileyf0fc52b2015-10-31 13:22:15 -0700787 status = BAD_VALUE;
788 return status;
Casey Dahlin451ff582015-10-19 18:12:18 -0700789 }
790
Casey Dahlin185d3442016-02-09 11:08:35 -0800791 status = parcel->writeInt32(val.size());
Casey Dahlin451ff582015-10-19 18:12:18 -0700792 if (status != OK) {
793 return status;
794 }
795
Casey Dahlin185d3442016-02-09 11:08:35 -0800796 void* data = parcel->writeInplace(val.size());
Christopher Wileyf0fc52b2015-10-31 13:22:15 -0700797 if (!data) {
798 status = BAD_VALUE;
799 return status;
Casey Dahlin451ff582015-10-19 18:12:18 -0700800 }
801
Christopher Wileyf0fc52b2015-10-31 13:22:15 -0700802 memcpy(data, val.data(), val.size());
803 return status;
Casey Dahlin451ff582015-10-19 18:12:18 -0700804}
805
Casey Dahlin185d3442016-02-09 11:08:35 -0800806template<typename T>
807status_t writeByteVectorInternalPtr(Parcel* parcel,
808 const std::unique_ptr<std::vector<T>>& val)
809{
810 if (!val) {
811 return parcel->writeInt32(-1);
812 }
813
814 return writeByteVectorInternal(parcel, *val);
815}
816
817} // namespace
818
819status_t Parcel::writeByteVector(const std::vector<int8_t>& val) {
820 return writeByteVectorInternal(this, val);
821}
822
823status_t Parcel::writeByteVector(const std::unique_ptr<std::vector<int8_t>>& val)
824{
825 return writeByteVectorInternalPtr(this, val);
826}
827
828status_t Parcel::writeByteVector(const std::vector<uint8_t>& val) {
829 return writeByteVectorInternal(this, val);
830}
831
832status_t Parcel::writeByteVector(const std::unique_ptr<std::vector<uint8_t>>& val)
833{
834 return writeByteVectorInternalPtr(this, val);
835}
836
Casey Dahlin451ff582015-10-19 18:12:18 -0700837status_t Parcel::writeInt32Vector(const std::vector<int32_t>& val)
838{
Christopher Wiley03d1eb62015-11-19 06:42:40 -0800839 return writeTypedVector(val, &Parcel::writeInt32);
Casey Dahlin451ff582015-10-19 18:12:18 -0700840}
841
Casey Dahlinb9872622015-11-25 15:09:45 -0800842status_t Parcel::writeInt32Vector(const std::unique_ptr<std::vector<int32_t>>& val)
843{
844 return writeNullableTypedVector(val, &Parcel::writeInt32);
845}
846
Casey Dahlin451ff582015-10-19 18:12:18 -0700847status_t Parcel::writeInt64Vector(const std::vector<int64_t>& val)
848{
Christopher Wiley03d1eb62015-11-19 06:42:40 -0800849 return writeTypedVector(val, &Parcel::writeInt64);
Casey Dahlin451ff582015-10-19 18:12:18 -0700850}
851
Casey Dahlinb9872622015-11-25 15:09:45 -0800852status_t Parcel::writeInt64Vector(const std::unique_ptr<std::vector<int64_t>>& val)
853{
854 return writeNullableTypedVector(val, &Parcel::writeInt64);
855}
856
Casey Dahlin451ff582015-10-19 18:12:18 -0700857status_t Parcel::writeFloatVector(const std::vector<float>& val)
858{
Christopher Wiley03d1eb62015-11-19 06:42:40 -0800859 return writeTypedVector(val, &Parcel::writeFloat);
Casey Dahlin451ff582015-10-19 18:12:18 -0700860}
861
Casey Dahlinb9872622015-11-25 15:09:45 -0800862status_t Parcel::writeFloatVector(const std::unique_ptr<std::vector<float>>& val)
863{
864 return writeNullableTypedVector(val, &Parcel::writeFloat);
865}
866
Casey Dahlin451ff582015-10-19 18:12:18 -0700867status_t Parcel::writeDoubleVector(const std::vector<double>& val)
868{
Christopher Wiley03d1eb62015-11-19 06:42:40 -0800869 return writeTypedVector(val, &Parcel::writeDouble);
Casey Dahlin451ff582015-10-19 18:12:18 -0700870}
871
Casey Dahlinb9872622015-11-25 15:09:45 -0800872status_t Parcel::writeDoubleVector(const std::unique_ptr<std::vector<double>>& val)
873{
874 return writeNullableTypedVector(val, &Parcel::writeDouble);
875}
876
Casey Dahlin451ff582015-10-19 18:12:18 -0700877status_t Parcel::writeBoolVector(const std::vector<bool>& val)
878{
Christopher Wiley03d1eb62015-11-19 06:42:40 -0800879 return writeTypedVector(val, &Parcel::writeBool);
Casey Dahlin451ff582015-10-19 18:12:18 -0700880}
881
Casey Dahlinb9872622015-11-25 15:09:45 -0800882status_t Parcel::writeBoolVector(const std::unique_ptr<std::vector<bool>>& val)
883{
884 return writeNullableTypedVector(val, &Parcel::writeBool);
885}
886
Casey Dahlin451ff582015-10-19 18:12:18 -0700887status_t Parcel::writeCharVector(const std::vector<char16_t>& val)
888{
Christopher Wiley03d1eb62015-11-19 06:42:40 -0800889 return writeTypedVector(val, &Parcel::writeChar);
Casey Dahlin451ff582015-10-19 18:12:18 -0700890}
891
Casey Dahlinb9872622015-11-25 15:09:45 -0800892status_t Parcel::writeCharVector(const std::unique_ptr<std::vector<char16_t>>& val)
893{
894 return writeNullableTypedVector(val, &Parcel::writeChar);
895}
896
Casey Dahlin451ff582015-10-19 18:12:18 -0700897status_t Parcel::writeString16Vector(const std::vector<String16>& val)
898{
Christopher Wiley03d1eb62015-11-19 06:42:40 -0800899 return writeTypedVector(val, &Parcel::writeString16);
Casey Dahlin451ff582015-10-19 18:12:18 -0700900}
901
Casey Dahlinb9872622015-11-25 15:09:45 -0800902status_t Parcel::writeString16Vector(
903 const std::unique_ptr<std::vector<std::unique_ptr<String16>>>& val)
904{
905 return writeNullableTypedVector(val, &Parcel::writeString16);
906}
907
Christopher Wiley9a5e32f2016-01-28 16:56:53 -0800908status_t Parcel::writeUtf8VectorAsUtf16Vector(
909 const std::unique_ptr<std::vector<std::unique_ptr<std::string>>>& val) {
910 return writeNullableTypedVector(val, &Parcel::writeUtf8AsUtf16);
911}
912
913status_t Parcel::writeUtf8VectorAsUtf16Vector(const std::vector<std::string>& val) {
914 return writeTypedVector(val, &Parcel::writeUtf8AsUtf16);
915}
916
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700917status_t Parcel::writeInt32(int32_t val)
918{
Andreas Huber84a6d042009-08-17 13:33:27 -0700919 return writeAligned(val);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700920}
Dan Stoza41a0f2f2014-12-01 10:01:10 -0800921
922status_t Parcel::writeUint32(uint32_t val)
923{
924 return writeAligned(val);
925}
926
Marco Nelissen5c0106e2013-10-16 10:57:51 -0700927status_t Parcel::writeInt32Array(size_t len, const int32_t *val) {
Nick Kralevichb6b14232015-04-02 09:36:02 -0700928 if (len > INT32_MAX) {
929 // don't accept size_t values which may have come from an
930 // inadvertent conversion from a negative int.
931 return BAD_VALUE;
932 }
933
Marco Nelissen5c0106e2013-10-16 10:57:51 -0700934 if (!val) {
Chad Brubakere59cb432015-06-30 14:03:55 -0700935 return writeInt32(-1);
Marco Nelissen5c0106e2013-10-16 10:57:51 -0700936 }
Chad Brubakere59cb432015-06-30 14:03:55 -0700937 status_t ret = writeInt32(static_cast<uint32_t>(len));
Marco Nelissen5c0106e2013-10-16 10:57:51 -0700938 if (ret == NO_ERROR) {
939 ret = write(val, len * sizeof(*val));
940 }
941 return ret;
942}
Marco Nelissenf0190bf2014-03-13 14:17:40 -0700943status_t Parcel::writeByteArray(size_t len, const uint8_t *val) {
Nick Kralevichb6b14232015-04-02 09:36:02 -0700944 if (len > INT32_MAX) {
945 // don't accept size_t values which may have come from an
946 // inadvertent conversion from a negative int.
947 return BAD_VALUE;
948 }
949
Marco Nelissenf0190bf2014-03-13 14:17:40 -0700950 if (!val) {
Chad Brubakere59cb432015-06-30 14:03:55 -0700951 return writeInt32(-1);
Marco Nelissenf0190bf2014-03-13 14:17:40 -0700952 }
Chad Brubakere59cb432015-06-30 14:03:55 -0700953 status_t ret = writeInt32(static_cast<uint32_t>(len));
Marco Nelissenf0190bf2014-03-13 14:17:40 -0700954 if (ret == NO_ERROR) {
955 ret = write(val, len * sizeof(*val));
956 }
957 return ret;
958}
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700959
Casey Dahlind6848f52015-10-15 15:44:59 -0700960status_t Parcel::writeBool(bool val)
961{
962 return writeInt32(int32_t(val));
963}
964
965status_t Parcel::writeChar(char16_t val)
966{
967 return writeInt32(int32_t(val));
968}
969
970status_t Parcel::writeByte(int8_t val)
971{
972 return writeInt32(int32_t(val));
973}
974
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700975status_t Parcel::writeInt64(int64_t val)
976{
Andreas Huber84a6d042009-08-17 13:33:27 -0700977 return writeAligned(val);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700978}
979
Ronghua Wu2d13afd2015-03-16 11:11:07 -0700980status_t Parcel::writeUint64(uint64_t val)
981{
982 return writeAligned(val);
983}
984
Serban Constantinescuf683e012013-11-05 16:53:55 +0000985status_t Parcel::writePointer(uintptr_t val)
986{
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800987 return writeAligned<binder_uintptr_t>(val);
Serban Constantinescuf683e012013-11-05 16:53:55 +0000988}
989
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700990status_t Parcel::writeFloat(float val)
991{
Andreas Huber84a6d042009-08-17 13:33:27 -0700992 return writeAligned(val);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700993}
994
Douglas Leungcc1a4bb2013-01-11 15:00:55 -0800995#if defined(__mips__) && defined(__mips_hard_float)
996
997status_t Parcel::writeDouble(double val)
998{
999 union {
1000 double d;
1001 unsigned long long ll;
1002 } u;
1003 u.d = val;
1004 return writeAligned(u.ll);
1005}
1006
1007#else
1008
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001009status_t Parcel::writeDouble(double val)
1010{
Andreas Huber84a6d042009-08-17 13:33:27 -07001011 return writeAligned(val);
1012}
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001013
Douglas Leungcc1a4bb2013-01-11 15:00:55 -08001014#endif
1015
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001016status_t Parcel::writeCString(const char* str)
1017{
1018 return write(str, strlen(str)+1);
1019}
1020
1021status_t Parcel::writeString8(const String8& str)
1022{
1023 status_t err = writeInt32(str.bytes());
Pravat Dalbeherad1dff8d2010-12-15 08:40:00 +01001024 // only write string if its length is more than zero characters,
1025 // as readString8 will only read if the length field is non-zero.
1026 // this is slightly different from how writeString16 works.
1027 if (str.bytes() > 0 && err == NO_ERROR) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001028 err = write(str.string(), str.bytes()+1);
1029 }
1030 return err;
1031}
1032
Casey Dahlinb9872622015-11-25 15:09:45 -08001033status_t Parcel::writeString16(const std::unique_ptr<String16>& str)
1034{
1035 if (!str) {
1036 return writeInt32(-1);
1037 }
1038
1039 return writeString16(*str);
1040}
1041
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001042status_t Parcel::writeString16(const String16& str)
1043{
1044 return writeString16(str.string(), str.size());
1045}
1046
1047status_t Parcel::writeString16(const char16_t* str, size_t len)
1048{
1049 if (str == NULL) return writeInt32(-1);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001050
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001051 status_t err = writeInt32(len);
1052 if (err == NO_ERROR) {
1053 len *= sizeof(char16_t);
1054 uint8_t* data = (uint8_t*)writeInplace(len+sizeof(char16_t));
1055 if (data) {
1056 memcpy(data, str, len);
1057 *reinterpret_cast<char16_t*>(data+len) = 0;
1058 return NO_ERROR;
1059 }
1060 err = mError;
1061 }
1062 return err;
1063}
1064
1065status_t Parcel::writeStrongBinder(const sp<IBinder>& val)
1066{
1067 return flatten_binder(ProcessState::self(), val, this);
1068}
1069
Casey Dahlineb8e15f2015-11-03 13:50:37 -08001070status_t Parcel::writeStrongBinderVector(const std::vector<sp<IBinder>>& val)
1071{
Christopher Wiley03d1eb62015-11-19 06:42:40 -08001072 return writeTypedVector(val, &Parcel::writeStrongBinder);
Casey Dahlineb8e15f2015-11-03 13:50:37 -08001073}
1074
Casey Dahlinb9872622015-11-25 15:09:45 -08001075status_t Parcel::writeStrongBinderVector(const std::unique_ptr<std::vector<sp<IBinder>>>& val)
1076{
1077 return writeNullableTypedVector(val, &Parcel::writeStrongBinder);
1078}
1079
1080status_t Parcel::readStrongBinderVector(std::unique_ptr<std::vector<sp<IBinder>>>* val) const {
Christopher Wiley35d77ca2016-03-08 10:49:51 -08001081 return readNullableTypedVector(val, &Parcel::readNullableStrongBinder);
Casey Dahlinb9872622015-11-25 15:09:45 -08001082}
1083
Casey Dahlineb8e15f2015-11-03 13:50:37 -08001084status_t Parcel::readStrongBinderVector(std::vector<sp<IBinder>>* val) const {
Christopher Wiley03d1eb62015-11-19 06:42:40 -08001085 return readTypedVector(val, &Parcel::readStrongBinder);
Casey Dahlineb8e15f2015-11-03 13:50:37 -08001086}
1087
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001088status_t Parcel::writeWeakBinder(const wp<IBinder>& val)
1089{
1090 return flatten_binder(ProcessState::self(), val, this);
1091}
1092
Casey Dahlinb9872622015-11-25 15:09:45 -08001093status_t Parcel::writeRawNullableParcelable(const Parcelable* parcelable) {
1094 if (!parcelable) {
1095 return writeInt32(0);
1096 }
1097
1098 return writeParcelable(*parcelable);
1099}
1100
Christopher Wiley97f048d2015-11-19 06:49:05 -08001101status_t Parcel::writeParcelable(const Parcelable& parcelable) {
1102 status_t status = writeInt32(1); // parcelable is not null.
1103 if (status != OK) {
1104 return status;
1105 }
1106 return parcelable.writeToParcel(this);
1107}
1108
Mathias Agopiana47f02a2009-05-21 16:29:38 -07001109status_t Parcel::writeNativeHandle(const native_handle* handle)
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001110{
Mathias Agopian1d0a95b2009-07-31 16:12:13 -07001111 if (!handle || handle->version != sizeof(native_handle))
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001112 return BAD_TYPE;
1113
1114 status_t err;
Mathias Agopiana47f02a2009-05-21 16:29:38 -07001115 err = writeInt32(handle->numFds);
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001116 if (err != NO_ERROR) return err;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001117
Mathias Agopiana47f02a2009-05-21 16:29:38 -07001118 err = writeInt32(handle->numInts);
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001119 if (err != NO_ERROR) return err;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001120
Mathias Agopiana47f02a2009-05-21 16:29:38 -07001121 for (int i=0 ; err==NO_ERROR && i<handle->numFds ; i++)
1122 err = writeDupFileDescriptor(handle->data[i]);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001123
1124 if (err != NO_ERROR) {
Steve Block9d453682011-12-20 16:23:08 +00001125 ALOGD("write native handle, write dup fd failed");
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001126 return err;
1127 }
Mathias Agopiana47f02a2009-05-21 16:29:38 -07001128 err = write(handle->data + handle->numFds, sizeof(int)*handle->numInts);
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001129 return err;
1130}
1131
Jeff Brown93ff1f92011-11-04 19:01:44 -07001132status_t Parcel::writeFileDescriptor(int fd, bool takeOwnership)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001133{
1134 flat_binder_object obj;
1135 obj.type = BINDER_TYPE_FD;
1136 obj.flags = 0x7f | FLAT_BINDER_FLAG_ACCEPTS_FDS;
Arve Hjønnevåg07fd0f12014-02-18 21:10:29 -08001137 obj.binder = 0; /* Don't pass uninitialized stack data to a remote process */
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001138 obj.handle = fd;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001139 obj.cookie = takeOwnership ? 1 : 0;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001140 return writeObject(obj, true);
1141}
1142
1143status_t Parcel::writeDupFileDescriptor(int fd)
1144{
Jeff Brownd341c712011-11-04 20:19:33 -07001145 int dupFd = dup(fd);
1146 if (dupFd < 0) {
1147 return -errno;
1148 }
1149 status_t err = writeFileDescriptor(dupFd, true /*takeOwnership*/);
Casey Dahlin06673e32015-11-23 13:24:23 -08001150 if (err != OK) {
Jeff Brownd341c712011-11-04 20:19:33 -07001151 close(dupFd);
1152 }
1153 return err;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001154}
1155
Christopher Wiley2cf19952016-04-11 11:09:37 -07001156status_t Parcel::writeUniqueFileDescriptor(const base::unique_fd& fd) {
Casey Dahlin06673e32015-11-23 13:24:23 -08001157 return writeDupFileDescriptor(fd.get());
1158}
1159
Christopher Wiley2cf19952016-04-11 11:09:37 -07001160status_t Parcel::writeUniqueFileDescriptorVector(const std::vector<base::unique_fd>& val) {
Casey Dahlin06673e32015-11-23 13:24:23 -08001161 return writeTypedVector(val, &Parcel::writeUniqueFileDescriptor);
1162}
1163
Christopher Wiley2cf19952016-04-11 11:09:37 -07001164status_t Parcel::writeUniqueFileDescriptorVector(const std::unique_ptr<std::vector<base::unique_fd>>& val) {
Casey Dahlinb9872622015-11-25 15:09:45 -08001165 return writeNullableTypedVector(val, &Parcel::writeUniqueFileDescriptor);
1166}
1167
Jeff Brown13b16042014-11-11 16:44:25 -08001168status_t Parcel::writeBlob(size_t len, bool mutableCopy, WritableBlob* outBlob)
Jeff Brown5707dbf2011-09-23 21:17:56 -07001169{
Nick Kralevichb6b14232015-04-02 09:36:02 -07001170 if (len > INT32_MAX) {
1171 // don't accept size_t values which may have come from an
1172 // inadvertent conversion from a negative int.
1173 return BAD_VALUE;
1174 }
1175
Jeff Brown13b16042014-11-11 16:44:25 -08001176 status_t status;
1177 if (!mAllowFds || len <= BLOB_INPLACE_LIMIT) {
Steve Block6807e592011-10-20 11:56:00 +01001178 ALOGV("writeBlob: write in place");
Jeff Brown13b16042014-11-11 16:44:25 -08001179 status = writeInt32(BLOB_INPLACE);
Jeff Brown5707dbf2011-09-23 21:17:56 -07001180 if (status) return status;
1181
1182 void* ptr = writeInplace(len);
1183 if (!ptr) return NO_MEMORY;
1184
Jeff Brown13b16042014-11-11 16:44:25 -08001185 outBlob->init(-1, ptr, len, false);
Jeff Brown5707dbf2011-09-23 21:17:56 -07001186 return NO_ERROR;
1187 }
1188
Steve Block6807e592011-10-20 11:56:00 +01001189 ALOGV("writeBlob: write to ashmem");
Jeff Brown5707dbf2011-09-23 21:17:56 -07001190 int fd = ashmem_create_region("Parcel Blob", len);
1191 if (fd < 0) return NO_MEMORY;
1192
1193 int result = ashmem_set_prot_region(fd, PROT_READ | PROT_WRITE);
1194 if (result < 0) {
Jeff Brownec4e0062011-10-10 14:50:10 -07001195 status = result;
Jeff Brown5707dbf2011-09-23 21:17:56 -07001196 } else {
1197 void* ptr = ::mmap(NULL, len, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
1198 if (ptr == MAP_FAILED) {
1199 status = -errno;
1200 } else {
Jeff Brown13b16042014-11-11 16:44:25 -08001201 if (!mutableCopy) {
1202 result = ashmem_set_prot_region(fd, PROT_READ);
1203 }
Jeff Brown5707dbf2011-09-23 21:17:56 -07001204 if (result < 0) {
Jeff Brownec4e0062011-10-10 14:50:10 -07001205 status = result;
Jeff Brown5707dbf2011-09-23 21:17:56 -07001206 } else {
Jeff Brown13b16042014-11-11 16:44:25 -08001207 status = writeInt32(mutableCopy ? BLOB_ASHMEM_MUTABLE : BLOB_ASHMEM_IMMUTABLE);
Jeff Brown5707dbf2011-09-23 21:17:56 -07001208 if (!status) {
Jeff Brown93ff1f92011-11-04 19:01:44 -07001209 status = writeFileDescriptor(fd, true /*takeOwnership*/);
Jeff Brown5707dbf2011-09-23 21:17:56 -07001210 if (!status) {
Jeff Brown13b16042014-11-11 16:44:25 -08001211 outBlob->init(fd, ptr, len, mutableCopy);
Jeff Brown5707dbf2011-09-23 21:17:56 -07001212 return NO_ERROR;
1213 }
1214 }
1215 }
1216 }
1217 ::munmap(ptr, len);
1218 }
1219 ::close(fd);
1220 return status;
1221}
1222
Jeff Brown13b16042014-11-11 16:44:25 -08001223status_t Parcel::writeDupImmutableBlobFileDescriptor(int fd)
1224{
1225 // Must match up with what's done in writeBlob.
1226 if (!mAllowFds) return FDS_NOT_ALLOWED;
1227 status_t status = writeInt32(BLOB_ASHMEM_IMMUTABLE);
1228 if (status) return status;
1229 return writeDupFileDescriptor(fd);
1230}
1231
Mathias Agopiane1424282013-07-29 21:24:40 -07001232status_t Parcel::write(const FlattenableHelperInterface& val)
Mathias Agopian98e71dd2010-02-11 17:30:52 -08001233{
1234 status_t err;
1235
1236 // size if needed
Mathias Agopiane1424282013-07-29 21:24:40 -07001237 const size_t len = val.getFlattenedSize();
1238 const size_t fd_count = val.getFdCount();
Mathias Agopian98e71dd2010-02-11 17:30:52 -08001239
Christopher Tatee4e0ae82016-03-24 16:03:44 -07001240 if ((len > INT32_MAX) || (fd_count >= gMaxFds)) {
Nick Kralevichb6b14232015-04-02 09:36:02 -07001241 // don't accept size_t values which may have come from an
1242 // inadvertent conversion from a negative int.
1243 return BAD_VALUE;
1244 }
1245
Mathias Agopian98e71dd2010-02-11 17:30:52 -08001246 err = this->writeInt32(len);
1247 if (err) return err;
1248
1249 err = this->writeInt32(fd_count);
1250 if (err) return err;
1251
1252 // payload
Nick Kralevichb6b14232015-04-02 09:36:02 -07001253 void* const buf = this->writeInplace(pad_size(len));
Mathias Agopian98e71dd2010-02-11 17:30:52 -08001254 if (buf == NULL)
1255 return BAD_VALUE;
1256
1257 int* fds = NULL;
1258 if (fd_count) {
Christopher Tatee4e0ae82016-03-24 16:03:44 -07001259 fds = new (std::nothrow) int[fd_count];
1260 if (fds == nullptr) {
1261 ALOGE("write: failed to allocate requested %zu fds", fd_count);
1262 return BAD_VALUE;
1263 }
Mathias Agopian98e71dd2010-02-11 17:30:52 -08001264 }
1265
1266 err = val.flatten(buf, len, fds, fd_count);
1267 for (size_t i=0 ; i<fd_count && err==NO_ERROR ; i++) {
1268 err = this->writeDupFileDescriptor( fds[i] );
1269 }
1270
1271 if (fd_count) {
1272 delete [] fds;
1273 }
1274
1275 return err;
1276}
1277
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001278status_t Parcel::writeObject(const flat_binder_object& val, bool nullMetaData)
1279{
1280 const bool enoughData = (mDataPos+sizeof(val)) <= mDataCapacity;
1281 const bool enoughObjects = mObjectsSize < mObjectsCapacity;
1282 if (enoughData && enoughObjects) {
1283restart_write:
1284 *reinterpret_cast<flat_binder_object*>(mData+mDataPos) = val;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001285
Christopher Tate98e67d32015-06-03 18:44:15 -07001286 // remember if it's a file descriptor
1287 if (val.type == BINDER_TYPE_FD) {
1288 if (!mAllowFds) {
1289 // fail before modifying our object index
1290 return FDS_NOT_ALLOWED;
1291 }
1292 mHasFds = mFdsKnown = true;
1293 }
1294
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001295 // Need to write meta-data?
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001296 if (nullMetaData || val.binder != 0) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001297 mObjects[mObjectsSize] = mDataPos;
Adrian Rooscbf37262015-10-22 16:12:53 -07001298 acquire_object(ProcessState::self(), val, this, &mOpenAshmemSize);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001299 mObjectsSize++;
1300 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001301
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001302 return finishWrite(sizeof(flat_binder_object));
1303 }
1304
1305 if (!enoughData) {
1306 const status_t err = growData(sizeof(val));
1307 if (err != NO_ERROR) return err;
1308 }
1309 if (!enoughObjects) {
1310 size_t newSize = ((mObjectsSize+2)*3)/2;
Christopher Tateed7a50c2015-06-08 14:45:14 -07001311 if (newSize < mObjectsSize) return NO_MEMORY; // overflow
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001312 binder_size_t* objects = (binder_size_t*)realloc(mObjects, newSize*sizeof(binder_size_t));
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001313 if (objects == NULL) return NO_MEMORY;
1314 mObjects = objects;
1315 mObjectsCapacity = newSize;
1316 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001317
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001318 goto restart_write;
1319}
1320
Brad Fitzpatrick837a0d02010-07-13 15:33:35 -07001321status_t Parcel::writeNoException()
1322{
Christopher Wiley09eb7492015-11-09 15:06:15 -08001323 binder::Status status;
1324 return status.writeToParcel(this);
Brad Fitzpatrick837a0d02010-07-13 15:33:35 -07001325}
1326
Colin Cross6f4f3ab2014-02-05 17:42:44 -08001327void Parcel::remove(size_t /*start*/, size_t /*amt*/)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001328{
1329 LOG_ALWAYS_FATAL("Parcel::remove() not yet implemented!");
1330}
1331
1332status_t Parcel::read(void* outData, size_t len) const
1333{
Nick Kralevichb6b14232015-04-02 09:36:02 -07001334 if (len > INT32_MAX) {
1335 // don't accept size_t values which may have come from an
1336 // inadvertent conversion from a negative int.
1337 return BAD_VALUE;
1338 }
1339
1340 if ((mDataPos+pad_size(len)) >= mDataPos && (mDataPos+pad_size(len)) <= mDataSize
1341 && len <= pad_size(len)) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001342 memcpy(outData, mData+mDataPos, len);
Nick Kralevichb6b14232015-04-02 09:36:02 -07001343 mDataPos += pad_size(len);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001344 ALOGV("read Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001345 return NO_ERROR;
1346 }
1347 return NOT_ENOUGH_DATA;
1348}
1349
1350const void* Parcel::readInplace(size_t len) const
1351{
Nick Kralevichb6b14232015-04-02 09:36:02 -07001352 if (len > INT32_MAX) {
1353 // don't accept size_t values which may have come from an
1354 // inadvertent conversion from a negative int.
1355 return NULL;
1356 }
1357
1358 if ((mDataPos+pad_size(len)) >= mDataPos && (mDataPos+pad_size(len)) <= mDataSize
1359 && len <= pad_size(len)) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001360 const void* data = mData+mDataPos;
Nick Kralevichb6b14232015-04-02 09:36:02 -07001361 mDataPos += pad_size(len);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001362 ALOGV("readInplace Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001363 return data;
1364 }
1365 return NULL;
1366}
1367
Andreas Huber84a6d042009-08-17 13:33:27 -07001368template<class T>
1369status_t Parcel::readAligned(T *pArg) const {
Nick Kralevichb6b14232015-04-02 09:36:02 -07001370 COMPILE_TIME_ASSERT_FUNCTION_SCOPE(PAD_SIZE_UNSAFE(sizeof(T)) == sizeof(T));
Andreas Huber84a6d042009-08-17 13:33:27 -07001371
1372 if ((mDataPos+sizeof(T)) <= mDataSize) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001373 const void* data = mData+mDataPos;
Andreas Huber84a6d042009-08-17 13:33:27 -07001374 mDataPos += sizeof(T);
1375 *pArg = *reinterpret_cast<const T*>(data);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001376 return NO_ERROR;
1377 } else {
1378 return NOT_ENOUGH_DATA;
1379 }
1380}
1381
Andreas Huber84a6d042009-08-17 13:33:27 -07001382template<class T>
1383T Parcel::readAligned() const {
1384 T result;
1385 if (readAligned(&result) != NO_ERROR) {
1386 result = 0;
1387 }
1388
1389 return result;
1390}
1391
1392template<class T>
1393status_t Parcel::writeAligned(T val) {
Nick Kralevichb6b14232015-04-02 09:36:02 -07001394 COMPILE_TIME_ASSERT_FUNCTION_SCOPE(PAD_SIZE_UNSAFE(sizeof(T)) == sizeof(T));
Andreas Huber84a6d042009-08-17 13:33:27 -07001395
1396 if ((mDataPos+sizeof(val)) <= mDataCapacity) {
1397restart_write:
1398 *reinterpret_cast<T*>(mData+mDataPos) = val;
1399 return finishWrite(sizeof(val));
1400 }
1401
1402 status_t err = growData(sizeof(val));
1403 if (err == NO_ERROR) goto restart_write;
1404 return err;
1405}
1406
Casey Dahlin185d3442016-02-09 11:08:35 -08001407namespace {
1408
1409template<typename T>
1410status_t readByteVectorInternal(const Parcel* parcel,
1411 std::vector<T>* val) {
Casey Dahlin451ff582015-10-19 18:12:18 -07001412 val->clear();
1413
1414 int32_t size;
Casey Dahlin185d3442016-02-09 11:08:35 -08001415 status_t status = parcel->readInt32(&size);
Casey Dahlin451ff582015-10-19 18:12:18 -07001416
1417 if (status != OK) {
1418 return status;
1419 }
1420
Christopher Wiley4db672d2015-11-10 09:44:30 -08001421 if (size < 0) {
1422 status = UNEXPECTED_NULL;
1423 return status;
1424 }
Casey Dahlin185d3442016-02-09 11:08:35 -08001425 if (size_t(size) > parcel->dataAvail()) {
Christopher Wileyf0fc52b2015-10-31 13:22:15 -07001426 status = BAD_VALUE;
1427 return status;
Casey Dahlin451ff582015-10-19 18:12:18 -07001428 }
Christopher Wiley4db672d2015-11-10 09:44:30 -08001429
Paul Lietar433e87b2016-09-16 10:39:32 -07001430 T* data = const_cast<T*>(reinterpret_cast<const T*>(parcel->readInplace(size)));
Christopher Wileyf0fc52b2015-10-31 13:22:15 -07001431 if (!data) {
1432 status = BAD_VALUE;
1433 return status;
1434 }
Paul Lietar433e87b2016-09-16 10:39:32 -07001435 val->reserve(size);
1436 val->insert(val->end(), data, data + size);
Casey Dahlin451ff582015-10-19 18:12:18 -07001437
Christopher Wileyf0fc52b2015-10-31 13:22:15 -07001438 return status;
Casey Dahlin451ff582015-10-19 18:12:18 -07001439}
1440
Casey Dahlin185d3442016-02-09 11:08:35 -08001441template<typename T>
1442status_t readByteVectorInternalPtr(
1443 const Parcel* parcel,
1444 std::unique_ptr<std::vector<T>>* val) {
1445 const int32_t start = parcel->dataPosition();
Casey Dahlinb9872622015-11-25 15:09:45 -08001446 int32_t size;
Casey Dahlin185d3442016-02-09 11:08:35 -08001447 status_t status = parcel->readInt32(&size);
Casey Dahlinb9872622015-11-25 15:09:45 -08001448 val->reset();
1449
1450 if (status != OK || size < 0) {
1451 return status;
1452 }
1453
Casey Dahlin185d3442016-02-09 11:08:35 -08001454 parcel->setDataPosition(start);
Christopher Tatee4e0ae82016-03-24 16:03:44 -07001455 val->reset(new (std::nothrow) std::vector<T>());
Casey Dahlinb9872622015-11-25 15:09:45 -08001456
Casey Dahlin185d3442016-02-09 11:08:35 -08001457 status = readByteVectorInternal(parcel, val->get());
Casey Dahlinb9872622015-11-25 15:09:45 -08001458
1459 if (status != OK) {
1460 val->reset();
1461 }
1462
1463 return status;
1464}
1465
Casey Dahlin185d3442016-02-09 11:08:35 -08001466} // namespace
1467
1468status_t Parcel::readByteVector(std::vector<int8_t>* val) const {
1469 return readByteVectorInternal(this, val);
1470}
1471
1472status_t Parcel::readByteVector(std::vector<uint8_t>* val) const {
1473 return readByteVectorInternal(this, val);
1474}
1475
1476status_t Parcel::readByteVector(std::unique_ptr<std::vector<int8_t>>* val) const {
1477 return readByteVectorInternalPtr(this, val);
1478}
1479
1480status_t Parcel::readByteVector(std::unique_ptr<std::vector<uint8_t>>* val) const {
1481 return readByteVectorInternalPtr(this, val);
1482}
1483
Casey Dahlinb9872622015-11-25 15:09:45 -08001484status_t Parcel::readInt32Vector(std::unique_ptr<std::vector<int32_t>>* val) const {
1485 return readNullableTypedVector(val, &Parcel::readInt32);
1486}
1487
Casey Dahlin451ff582015-10-19 18:12:18 -07001488status_t Parcel::readInt32Vector(std::vector<int32_t>* val) const {
Christopher Wiley03d1eb62015-11-19 06:42:40 -08001489 return readTypedVector(val, &Parcel::readInt32);
Casey Dahlin451ff582015-10-19 18:12:18 -07001490}
1491
Casey Dahlinb9872622015-11-25 15:09:45 -08001492status_t Parcel::readInt64Vector(std::unique_ptr<std::vector<int64_t>>* val) const {
1493 return readNullableTypedVector(val, &Parcel::readInt64);
1494}
1495
Casey Dahlin451ff582015-10-19 18:12:18 -07001496status_t Parcel::readInt64Vector(std::vector<int64_t>* val) const {
Christopher Wiley03d1eb62015-11-19 06:42:40 -08001497 return readTypedVector(val, &Parcel::readInt64);
Casey Dahlin451ff582015-10-19 18:12:18 -07001498}
1499
Casey Dahlinb9872622015-11-25 15:09:45 -08001500status_t Parcel::readFloatVector(std::unique_ptr<std::vector<float>>* val) const {
1501 return readNullableTypedVector(val, &Parcel::readFloat);
1502}
1503
Casey Dahlin451ff582015-10-19 18:12:18 -07001504status_t Parcel::readFloatVector(std::vector<float>* val) const {
Christopher Wiley03d1eb62015-11-19 06:42:40 -08001505 return readTypedVector(val, &Parcel::readFloat);
Casey Dahlin451ff582015-10-19 18:12:18 -07001506}
1507
Casey Dahlinb9872622015-11-25 15:09:45 -08001508status_t Parcel::readDoubleVector(std::unique_ptr<std::vector<double>>* val) const {
1509 return readNullableTypedVector(val, &Parcel::readDouble);
1510}
1511
Casey Dahlin451ff582015-10-19 18:12:18 -07001512status_t Parcel::readDoubleVector(std::vector<double>* val) const {
Christopher Wiley03d1eb62015-11-19 06:42:40 -08001513 return readTypedVector(val, &Parcel::readDouble);
Casey Dahlin451ff582015-10-19 18:12:18 -07001514}
1515
Casey Dahlinb9872622015-11-25 15:09:45 -08001516status_t Parcel::readBoolVector(std::unique_ptr<std::vector<bool>>* val) const {
1517 const int32_t start = dataPosition();
1518 int32_t size;
1519 status_t status = readInt32(&size);
1520 val->reset();
Casey Dahlin451ff582015-10-19 18:12:18 -07001521
Casey Dahlinb9872622015-11-25 15:09:45 -08001522 if (status != OK || size < 0) {
1523 return status;
1524 }
1525
1526 setDataPosition(start);
Christopher Tatee4e0ae82016-03-24 16:03:44 -07001527 val->reset(new (std::nothrow) std::vector<bool>());
Casey Dahlinb9872622015-11-25 15:09:45 -08001528
1529 status = readBoolVector(val->get());
1530
1531 if (status != OK) {
1532 val->reset();
1533 }
1534
1535 return status;
1536}
1537
1538status_t Parcel::readBoolVector(std::vector<bool>* val) const {
Casey Dahlin451ff582015-10-19 18:12:18 -07001539 int32_t size;
1540 status_t status = readInt32(&size);
1541
1542 if (status != OK) {
1543 return status;
1544 }
1545
1546 if (size < 0) {
Christopher Wiley4db672d2015-11-10 09:44:30 -08001547 return UNEXPECTED_NULL;
Casey Dahlin451ff582015-10-19 18:12:18 -07001548 }
1549
1550 val->resize(size);
1551
1552 /* C++ bool handling means a vector of bools isn't necessarily addressable
1553 * (we might use individual bits)
1554 */
Christopher Wiley97887982015-10-27 16:33:47 -07001555 bool data;
1556 for (int32_t i = 0; i < size; ++i) {
Casey Dahlin451ff582015-10-19 18:12:18 -07001557 status = readBool(&data);
1558 (*val)[i] = data;
1559
1560 if (status != OK) {
1561 return status;
1562 }
1563 }
1564
1565 return OK;
1566}
1567
Casey Dahlinb9872622015-11-25 15:09:45 -08001568status_t Parcel::readCharVector(std::unique_ptr<std::vector<char16_t>>* val) const {
1569 return readNullableTypedVector(val, &Parcel::readChar);
1570}
1571
Casey Dahlin451ff582015-10-19 18:12:18 -07001572status_t Parcel::readCharVector(std::vector<char16_t>* val) const {
Christopher Wiley03d1eb62015-11-19 06:42:40 -08001573 return readTypedVector(val, &Parcel::readChar);
Casey Dahlin451ff582015-10-19 18:12:18 -07001574}
1575
Casey Dahlinb9872622015-11-25 15:09:45 -08001576status_t Parcel::readString16Vector(
1577 std::unique_ptr<std::vector<std::unique_ptr<String16>>>* val) const {
1578 return readNullableTypedVector(val, &Parcel::readString16);
1579}
1580
Casey Dahlin451ff582015-10-19 18:12:18 -07001581status_t Parcel::readString16Vector(std::vector<String16>* val) const {
Christopher Wiley03d1eb62015-11-19 06:42:40 -08001582 return readTypedVector(val, &Parcel::readString16);
Casey Dahlin451ff582015-10-19 18:12:18 -07001583}
1584
Christopher Wiley9a5e32f2016-01-28 16:56:53 -08001585status_t Parcel::readUtf8VectorFromUtf16Vector(
1586 std::unique_ptr<std::vector<std::unique_ptr<std::string>>>* val) const {
1587 return readNullableTypedVector(val, &Parcel::readUtf8FromUtf16);
1588}
1589
1590status_t Parcel::readUtf8VectorFromUtf16Vector(std::vector<std::string>* val) const {
1591 return readTypedVector(val, &Parcel::readUtf8FromUtf16);
1592}
Casey Dahlin451ff582015-10-19 18:12:18 -07001593
Andreas Huber84a6d042009-08-17 13:33:27 -07001594status_t Parcel::readInt32(int32_t *pArg) const
1595{
1596 return readAligned(pArg);
1597}
1598
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001599int32_t Parcel::readInt32() const
1600{
Andreas Huber84a6d042009-08-17 13:33:27 -07001601 return readAligned<int32_t>();
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001602}
1603
Dan Stoza41a0f2f2014-12-01 10:01:10 -08001604status_t Parcel::readUint32(uint32_t *pArg) const
1605{
1606 return readAligned(pArg);
1607}
1608
1609uint32_t Parcel::readUint32() const
1610{
1611 return readAligned<uint32_t>();
1612}
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001613
1614status_t Parcel::readInt64(int64_t *pArg) const
1615{
Andreas Huber84a6d042009-08-17 13:33:27 -07001616 return readAligned(pArg);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001617}
1618
1619
1620int64_t Parcel::readInt64() const
1621{
Andreas Huber84a6d042009-08-17 13:33:27 -07001622 return readAligned<int64_t>();
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001623}
1624
Ronghua Wu2d13afd2015-03-16 11:11:07 -07001625status_t Parcel::readUint64(uint64_t *pArg) const
1626{
1627 return readAligned(pArg);
1628}
1629
1630uint64_t Parcel::readUint64() const
1631{
1632 return readAligned<uint64_t>();
1633}
1634
Serban Constantinescuf683e012013-11-05 16:53:55 +00001635status_t Parcel::readPointer(uintptr_t *pArg) const
1636{
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001637 status_t ret;
1638 binder_uintptr_t ptr;
1639 ret = readAligned(&ptr);
1640 if (!ret)
1641 *pArg = ptr;
1642 return ret;
Serban Constantinescuf683e012013-11-05 16:53:55 +00001643}
1644
1645uintptr_t Parcel::readPointer() const
1646{
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001647 return readAligned<binder_uintptr_t>();
Serban Constantinescuf683e012013-11-05 16:53:55 +00001648}
1649
1650
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001651status_t Parcel::readFloat(float *pArg) const
1652{
Andreas Huber84a6d042009-08-17 13:33:27 -07001653 return readAligned(pArg);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001654}
1655
1656
1657float Parcel::readFloat() const
1658{
Andreas Huber84a6d042009-08-17 13:33:27 -07001659 return readAligned<float>();
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001660}
1661
Douglas Leungcc1a4bb2013-01-11 15:00:55 -08001662#if defined(__mips__) && defined(__mips_hard_float)
1663
1664status_t Parcel::readDouble(double *pArg) const
1665{
1666 union {
1667 double d;
1668 unsigned long long ll;
1669 } u;
Narayan Kamath2c68d382014-06-04 15:04:29 +01001670 u.d = 0;
Douglas Leungcc1a4bb2013-01-11 15:00:55 -08001671 status_t status;
1672 status = readAligned(&u.ll);
1673 *pArg = u.d;
1674 return status;
1675}
1676
1677double Parcel::readDouble() const
1678{
1679 union {
1680 double d;
1681 unsigned long long ll;
1682 } u;
1683 u.ll = readAligned<unsigned long long>();
1684 return u.d;
1685}
1686
1687#else
1688
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001689status_t Parcel::readDouble(double *pArg) const
1690{
Andreas Huber84a6d042009-08-17 13:33:27 -07001691 return readAligned(pArg);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001692}
1693
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001694double Parcel::readDouble() const
1695{
Andreas Huber84a6d042009-08-17 13:33:27 -07001696 return readAligned<double>();
1697}
1698
Douglas Leungcc1a4bb2013-01-11 15:00:55 -08001699#endif
1700
Andreas Huber84a6d042009-08-17 13:33:27 -07001701status_t Parcel::readIntPtr(intptr_t *pArg) const
1702{
1703 return readAligned(pArg);
1704}
1705
1706
1707intptr_t Parcel::readIntPtr() const
1708{
1709 return readAligned<intptr_t>();
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001710}
1711
Casey Dahlind6848f52015-10-15 15:44:59 -07001712status_t Parcel::readBool(bool *pArg) const
1713{
1714 int32_t tmp;
1715 status_t ret = readInt32(&tmp);
1716 *pArg = (tmp != 0);
1717 return ret;
1718}
1719
1720bool Parcel::readBool() const
1721{
1722 return readInt32() != 0;
1723}
1724
1725status_t Parcel::readChar(char16_t *pArg) const
1726{
1727 int32_t tmp;
1728 status_t ret = readInt32(&tmp);
1729 *pArg = char16_t(tmp);
1730 return ret;
1731}
1732
1733char16_t Parcel::readChar() const
1734{
1735 return char16_t(readInt32());
1736}
1737
1738status_t Parcel::readByte(int8_t *pArg) const
1739{
1740 int32_t tmp;
1741 status_t ret = readInt32(&tmp);
1742 *pArg = int8_t(tmp);
1743 return ret;
1744}
1745
1746int8_t Parcel::readByte() const
1747{
1748 return int8_t(readInt32());
1749}
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001750
Christopher Wiley9a5e32f2016-01-28 16:56:53 -08001751status_t Parcel::readUtf8FromUtf16(std::string* str) const {
1752 size_t utf16Size = 0;
1753 const char16_t* src = readString16Inplace(&utf16Size);
1754 if (!src) {
1755 return UNEXPECTED_NULL;
1756 }
1757
1758 // Save ourselves the trouble, we're done.
1759 if (utf16Size == 0u) {
1760 str->clear();
1761 return NO_ERROR;
1762 }
1763
Sergio Giro9b39ebe2016-06-28 18:19:33 +01001764 // Allow for closing '\0'
1765 ssize_t utf8Size = utf16_to_utf8_length(src, utf16Size) + 1;
1766 if (utf8Size < 1) {
Christopher Wiley9a5e32f2016-01-28 16:56:53 -08001767 return BAD_VALUE;
1768 }
1769 // Note that while it is probably safe to assume string::resize keeps a
Sergio Giro9b39ebe2016-06-28 18:19:33 +01001770 // spare byte around for the trailing null, we still pass the size including the trailing null
Christopher Wiley9a5e32f2016-01-28 16:56:53 -08001771 str->resize(utf8Size);
Sergio Giro9b39ebe2016-06-28 18:19:33 +01001772 utf16_to_utf8(src, utf16Size, &((*str)[0]), utf8Size);
1773 str->resize(utf8Size - 1);
Christopher Wiley9a5e32f2016-01-28 16:56:53 -08001774 return NO_ERROR;
1775}
1776
1777status_t Parcel::readUtf8FromUtf16(std::unique_ptr<std::string>* str) const {
1778 const int32_t start = dataPosition();
1779 int32_t size;
1780 status_t status = readInt32(&size);
1781 str->reset();
1782
1783 if (status != OK || size < 0) {
1784 return status;
1785 }
1786
1787 setDataPosition(start);
Christopher Tatee4e0ae82016-03-24 16:03:44 -07001788 str->reset(new (std::nothrow) std::string());
Christopher Wiley9a5e32f2016-01-28 16:56:53 -08001789 return readUtf8FromUtf16(str->get());
1790}
1791
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001792const char* Parcel::readCString() const
1793{
1794 const size_t avail = mDataSize-mDataPos;
1795 if (avail > 0) {
1796 const char* str = reinterpret_cast<const char*>(mData+mDataPos);
1797 // is the string's trailing NUL within the parcel's valid bounds?
1798 const char* eos = reinterpret_cast<const char*>(memchr(str, 0, avail));
1799 if (eos) {
1800 const size_t len = eos - str;
Nick Kralevichb6b14232015-04-02 09:36:02 -07001801 mDataPos += pad_size(len+1);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001802 ALOGV("readCString Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001803 return str;
1804 }
1805 }
1806 return NULL;
1807}
1808
1809String8 Parcel::readString8() const
1810{
Roshan Pius87b64d22016-07-18 12:51:02 -07001811 String8 retString;
1812 status_t status = readString8(&retString);
1813 if (status != OK) {
1814 // We don't care about errors here, so just return an empty string.
1815 return String8();
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001816 }
Roshan Pius87b64d22016-07-18 12:51:02 -07001817 return retString;
1818}
1819
1820status_t Parcel::readString8(String8* pArg) const
1821{
1822 int32_t size;
1823 status_t status = readInt32(&size);
1824 if (status != OK) {
1825 return status;
1826 }
1827 // watch for potential int overflow from size+1
1828 if (size < 0 || size >= INT32_MAX) {
1829 return BAD_VALUE;
1830 }
1831 // |writeString8| writes nothing for empty string.
1832 if (size == 0) {
1833 *pArg = String8();
1834 return OK;
1835 }
1836 const char* str = (const char*)readInplace(size + 1);
1837 if (str == NULL) {
1838 return BAD_VALUE;
1839 }
1840 pArg->setTo(str, size);
1841 return OK;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001842}
1843
1844String16 Parcel::readString16() const
1845{
1846 size_t len;
1847 const char16_t* str = readString16Inplace(&len);
1848 if (str) return String16(str, len);
Steve Blocke6f43dd2012-01-06 19:20:56 +00001849 ALOGE("Reading a NULL string not supported here.");
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001850 return String16();
1851}
1852
Casey Dahlinb9872622015-11-25 15:09:45 -08001853status_t Parcel::readString16(std::unique_ptr<String16>* pArg) const
1854{
1855 const int32_t start = dataPosition();
1856 int32_t size;
1857 status_t status = readInt32(&size);
1858 pArg->reset();
1859
1860 if (status != OK || size < 0) {
1861 return status;
1862 }
1863
1864 setDataPosition(start);
Christopher Tatee4e0ae82016-03-24 16:03:44 -07001865 pArg->reset(new (std::nothrow) String16());
Casey Dahlinb9872622015-11-25 15:09:45 -08001866
1867 status = readString16(pArg->get());
1868
1869 if (status != OK) {
1870 pArg->reset();
1871 }
1872
1873 return status;
1874}
1875
Casey Dahlin451ff582015-10-19 18:12:18 -07001876status_t Parcel::readString16(String16* pArg) const
1877{
1878 size_t len;
1879 const char16_t* str = readString16Inplace(&len);
1880 if (str) {
Casey Dahlin1515ea12015-10-20 16:26:23 -07001881 pArg->setTo(str, len);
Casey Dahlin451ff582015-10-19 18:12:18 -07001882 return 0;
1883 } else {
1884 *pArg = String16();
Christopher Wiley4db672d2015-11-10 09:44:30 -08001885 return UNEXPECTED_NULL;
Casey Dahlin451ff582015-10-19 18:12:18 -07001886 }
1887}
1888
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001889const char16_t* Parcel::readString16Inplace(size_t* outLen) const
1890{
1891 int32_t size = readInt32();
1892 // watch for potential int overflow from size+1
1893 if (size >= 0 && size < INT32_MAX) {
1894 *outLen = size;
1895 const char16_t* str = (const char16_t*)readInplace((size+1)*sizeof(char16_t));
1896 if (str != NULL) {
1897 return str;
1898 }
1899 }
1900 *outLen = 0;
1901 return NULL;
1902}
1903
Casey Dahlinf0c13772015-10-27 18:33:56 -07001904status_t Parcel::readStrongBinder(sp<IBinder>* val) const
1905{
Christopher Wiley35d77ca2016-03-08 10:49:51 -08001906 status_t status = readNullableStrongBinder(val);
1907 if (status == OK && !val->get()) {
1908 status = UNEXPECTED_NULL;
1909 }
1910 return status;
1911}
1912
1913status_t Parcel::readNullableStrongBinder(sp<IBinder>* val) const
1914{
Casey Dahlinf0c13772015-10-27 18:33:56 -07001915 return unflatten_binder(ProcessState::self(), *this, val);
1916}
1917
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001918sp<IBinder> Parcel::readStrongBinder() const
1919{
1920 sp<IBinder> val;
Christopher Wiley35d77ca2016-03-08 10:49:51 -08001921 // Note that a lot of code in Android reads binders by hand with this
1922 // method, and that code has historically been ok with getting nullptr
1923 // back (while ignoring error codes).
1924 readNullableStrongBinder(&val);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001925 return val;
1926}
1927
1928wp<IBinder> Parcel::readWeakBinder() const
1929{
1930 wp<IBinder> val;
1931 unflatten_binder(ProcessState::self(), *this, &val);
1932 return val;
1933}
1934
Christopher Wiley97f048d2015-11-19 06:49:05 -08001935status_t Parcel::readParcelable(Parcelable* parcelable) const {
1936 int32_t have_parcelable = 0;
1937 status_t status = readInt32(&have_parcelable);
1938 if (status != OK) {
1939 return status;
1940 }
1941 if (!have_parcelable) {
1942 return UNEXPECTED_NULL;
1943 }
1944 return parcelable->readFromParcel(this);
1945}
1946
Brad Fitzpatrick837a0d02010-07-13 15:33:35 -07001947int32_t Parcel::readExceptionCode() const
1948{
Christopher Wiley09eb7492015-11-09 15:06:15 -08001949 binder::Status status;
1950 status.readFromParcel(*this);
1951 return status.exceptionCode();
Brad Fitzpatrick837a0d02010-07-13 15:33:35 -07001952}
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001953
Mathias Agopiana47f02a2009-05-21 16:29:38 -07001954native_handle* Parcel::readNativeHandle() const
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001955{
1956 int numFds, numInts;
1957 status_t err;
1958 err = readInt32(&numFds);
1959 if (err != NO_ERROR) return 0;
1960 err = readInt32(&numInts);
1961 if (err != NO_ERROR) return 0;
1962
Mathias Agopiana47f02a2009-05-21 16:29:38 -07001963 native_handle* h = native_handle_create(numFds, numInts);
Adam Lesinskieaac99a2015-05-12 17:35:48 -07001964 if (!h) {
1965 return 0;
1966 }
1967
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001968 for (int i=0 ; err==NO_ERROR && i<numFds ; i++) {
Rebecca Schultz Zavin360211f2009-02-13 16:34:38 -08001969 h->data[i] = dup(readFileDescriptor());
Marco Nelissen1de79662016-04-26 08:44:09 -07001970 if (h->data[i] < 0) {
1971 for (int j = 0; j < i; j++) {
1972 close(h->data[j]);
1973 }
1974 native_handle_delete(h);
1975 return 0;
1976 }
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001977 }
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001978 err = read(h->data + numFds, sizeof(int)*numInts);
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001979 if (err != NO_ERROR) {
Mathias Agopiana47f02a2009-05-21 16:29:38 -07001980 native_handle_close(h);
1981 native_handle_delete(h);
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001982 h = 0;
1983 }
1984 return h;
1985}
1986
1987
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001988int Parcel::readFileDescriptor() const
1989{
1990 const flat_binder_object* flat = readObject(true);
Casey Dahlin06673e32015-11-23 13:24:23 -08001991
1992 if (flat && flat->type == BINDER_TYPE_FD) {
1993 return flat->handle;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001994 }
Casey Dahlin06673e32015-11-23 13:24:23 -08001995
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001996 return BAD_TYPE;
1997}
1998
Christopher Wiley2cf19952016-04-11 11:09:37 -07001999status_t Parcel::readUniqueFileDescriptor(base::unique_fd* val) const
Casey Dahlin06673e32015-11-23 13:24:23 -08002000{
2001 int got = readFileDescriptor();
2002
2003 if (got == BAD_TYPE) {
2004 return BAD_TYPE;
2005 }
2006
2007 val->reset(dup(got));
2008
2009 if (val->get() < 0) {
2010 return BAD_VALUE;
2011 }
2012
2013 return OK;
2014}
2015
2016
Christopher Wiley2cf19952016-04-11 11:09:37 -07002017status_t Parcel::readUniqueFileDescriptorVector(std::unique_ptr<std::vector<base::unique_fd>>* val) const {
Casey Dahlinb9872622015-11-25 15:09:45 -08002018 return readNullableTypedVector(val, &Parcel::readUniqueFileDescriptor);
2019}
2020
Christopher Wiley2cf19952016-04-11 11:09:37 -07002021status_t Parcel::readUniqueFileDescriptorVector(std::vector<base::unique_fd>* val) const {
Casey Dahlin06673e32015-11-23 13:24:23 -08002022 return readTypedVector(val, &Parcel::readUniqueFileDescriptor);
2023}
2024
Jeff Brown5707dbf2011-09-23 21:17:56 -07002025status_t Parcel::readBlob(size_t len, ReadableBlob* outBlob) const
2026{
Jeff Brown13b16042014-11-11 16:44:25 -08002027 int32_t blobType;
2028 status_t status = readInt32(&blobType);
Jeff Brown5707dbf2011-09-23 21:17:56 -07002029 if (status) return status;
2030
Jeff Brown13b16042014-11-11 16:44:25 -08002031 if (blobType == BLOB_INPLACE) {
Steve Block6807e592011-10-20 11:56:00 +01002032 ALOGV("readBlob: read in place");
Jeff Brown5707dbf2011-09-23 21:17:56 -07002033 const void* ptr = readInplace(len);
2034 if (!ptr) return BAD_VALUE;
2035
Jeff Brown13b16042014-11-11 16:44:25 -08002036 outBlob->init(-1, const_cast<void*>(ptr), len, false);
Jeff Brown5707dbf2011-09-23 21:17:56 -07002037 return NO_ERROR;
2038 }
2039
Steve Block6807e592011-10-20 11:56:00 +01002040 ALOGV("readBlob: read from ashmem");
Jeff Brown13b16042014-11-11 16:44:25 -08002041 bool isMutable = (blobType == BLOB_ASHMEM_MUTABLE);
Jeff Brown5707dbf2011-09-23 21:17:56 -07002042 int fd = readFileDescriptor();
2043 if (fd == int(BAD_TYPE)) return BAD_VALUE;
2044
Jeff Brown13b16042014-11-11 16:44:25 -08002045 void* ptr = ::mmap(NULL, len, isMutable ? PROT_READ | PROT_WRITE : PROT_READ,
2046 MAP_SHARED, fd, 0);
Narayan Kamath9ea09752014-10-08 17:35:45 +01002047 if (ptr == MAP_FAILED) return NO_MEMORY;
Jeff Brown5707dbf2011-09-23 21:17:56 -07002048
Jeff Brown13b16042014-11-11 16:44:25 -08002049 outBlob->init(fd, ptr, len, isMutable);
Jeff Brown5707dbf2011-09-23 21:17:56 -07002050 return NO_ERROR;
2051}
2052
Mathias Agopiane1424282013-07-29 21:24:40 -07002053status_t Parcel::read(FlattenableHelperInterface& val) const
Mathias Agopian98e71dd2010-02-11 17:30:52 -08002054{
2055 // size
2056 const size_t len = this->readInt32();
2057 const size_t fd_count = this->readInt32();
2058
Christopher Tatee4e0ae82016-03-24 16:03:44 -07002059 if ((len > INT32_MAX) || (fd_count >= gMaxFds)) {
Nick Kralevichb6b14232015-04-02 09:36:02 -07002060 // don't accept size_t values which may have come from an
2061 // inadvertent conversion from a negative int.
2062 return BAD_VALUE;
2063 }
2064
Mathias Agopian98e71dd2010-02-11 17:30:52 -08002065 // payload
Nick Kralevichb6b14232015-04-02 09:36:02 -07002066 void const* const buf = this->readInplace(pad_size(len));
Mathias Agopian98e71dd2010-02-11 17:30:52 -08002067 if (buf == NULL)
2068 return BAD_VALUE;
2069
2070 int* fds = NULL;
2071 if (fd_count) {
Christopher Tatee4e0ae82016-03-24 16:03:44 -07002072 fds = new (std::nothrow) int[fd_count];
2073 if (fds == nullptr) {
2074 ALOGE("read: failed to allocate requested %zu fds", fd_count);
2075 return BAD_VALUE;
2076 }
Mathias Agopian98e71dd2010-02-11 17:30:52 -08002077 }
2078
2079 status_t err = NO_ERROR;
2080 for (size_t i=0 ; i<fd_count && err==NO_ERROR ; i++) {
Jesse Hallfee99042014-11-04 08:36:31 -08002081 fds[i] = dup(this->readFileDescriptor());
Jun Jiangabf8a2c2014-04-29 14:22:10 +08002082 if (fds[i] < 0) {
2083 err = BAD_VALUE;
Jesse Hallfee99042014-11-04 08:36:31 -08002084 ALOGE("dup() failed in Parcel::read, i is %zu, fds[i] is %d, fd_count is %zu, error: %s",
2085 i, fds[i], fd_count, strerror(errno));
Jun Jiangabf8a2c2014-04-29 14:22:10 +08002086 }
Mathias Agopian98e71dd2010-02-11 17:30:52 -08002087 }
2088
2089 if (err == NO_ERROR) {
2090 err = val.unflatten(buf, len, fds, fd_count);
2091 }
2092
2093 if (fd_count) {
2094 delete [] fds;
2095 }
2096
2097 return err;
2098}
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002099const flat_binder_object* Parcel::readObject(bool nullMetaData) const
2100{
2101 const size_t DPOS = mDataPos;
2102 if ((DPOS+sizeof(flat_binder_object)) <= mDataSize) {
2103 const flat_binder_object* obj
2104 = reinterpret_cast<const flat_binder_object*>(mData+DPOS);
2105 mDataPos = DPOS + sizeof(flat_binder_object);
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002106 if (!nullMetaData && (obj->cookie == 0 && obj->binder == 0)) {
The Android Open Source Project5f78a482009-01-20 14:03:58 -08002107 // When transferring a NULL object, we don't write it into
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002108 // the object list, so we don't want to check for it when
2109 // reading.
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002110 ALOGV("readObject Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002111 return obj;
2112 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002113
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002114 // Ensure that this object is valid...
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002115 binder_size_t* const OBJS = mObjects;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002116 const size_t N = mObjectsSize;
2117 size_t opos = mNextObjectHint;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002118
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002119 if (N > 0) {
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002120 ALOGV("Parcel %p looking for obj at %zu, hint=%zu",
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002121 this, DPOS, opos);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002122
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002123 // Start at the current hint position, looking for an object at
2124 // the current data position.
2125 if (opos < N) {
2126 while (opos < (N-1) && OBJS[opos] < DPOS) {
2127 opos++;
2128 }
2129 } else {
2130 opos = N-1;
2131 }
2132 if (OBJS[opos] == DPOS) {
2133 // Found it!
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002134 ALOGV("Parcel %p found obj %zu at index %zu with forward search",
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002135 this, DPOS, opos);
2136 mNextObjectHint = opos+1;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002137 ALOGV("readObject Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002138 return obj;
2139 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002140
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002141 // Look backwards for it...
2142 while (opos > 0 && OBJS[opos] > DPOS) {
2143 opos--;
2144 }
2145 if (OBJS[opos] == DPOS) {
2146 // Found it!
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002147 ALOGV("Parcel %p found obj %zu at index %zu with backward search",
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002148 this, DPOS, opos);
2149 mNextObjectHint = opos+1;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002150 ALOGV("readObject Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002151 return obj;
2152 }
2153 }
Colin Cross6f4f3ab2014-02-05 17:42:44 -08002154 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 -07002155 this, DPOS);
2156 }
2157 return NULL;
2158}
2159
2160void Parcel::closeFileDescriptors()
2161{
2162 size_t i = mObjectsSize;
2163 if (i > 0) {
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002164 //ALOGI("Closing file descriptors for %zu objects...", i);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002165 }
2166 while (i > 0) {
2167 i--;
2168 const flat_binder_object* flat
2169 = reinterpret_cast<flat_binder_object*>(mData+mObjects[i]);
2170 if (flat->type == BINDER_TYPE_FD) {
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002171 //ALOGI("Closing fd: %ld", flat->handle);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002172 close(flat->handle);
2173 }
2174 }
2175}
2176
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002177uintptr_t Parcel::ipcData() const
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002178{
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002179 return reinterpret_cast<uintptr_t>(mData);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002180}
2181
2182size_t Parcel::ipcDataSize() const
2183{
2184 return (mDataSize > mDataPos ? mDataSize : mDataPos);
2185}
2186
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002187uintptr_t Parcel::ipcObjects() const
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002188{
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002189 return reinterpret_cast<uintptr_t>(mObjects);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002190}
2191
2192size_t Parcel::ipcObjectsCount() const
2193{
2194 return mObjectsSize;
2195}
2196
2197void Parcel::ipcSetDataReference(const uint8_t* data, size_t dataSize,
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002198 const binder_size_t* objects, size_t objectsCount, release_func relFunc, void* relCookie)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002199{
Arve Hjønnevåg6f286112014-02-19 20:42:13 -08002200 binder_size_t minOffset = 0;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002201 freeDataNoInit();
2202 mError = NO_ERROR;
2203 mData = const_cast<uint8_t*>(data);
2204 mDataSize = mDataCapacity = dataSize;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002205 //ALOGI("setDataReference Setting data size of %p to %lu (pid=%d)", this, mDataSize, getpid());
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002206 mDataPos = 0;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002207 ALOGV("setDataReference Setting data pos of %p to %zu", this, mDataPos);
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002208 mObjects = const_cast<binder_size_t*>(objects);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002209 mObjectsSize = mObjectsCapacity = objectsCount;
2210 mNextObjectHint = 0;
2211 mOwner = relFunc;
2212 mOwnerCookie = relCookie;
Arve Hjønnevågf50b9ea2014-02-13 19:22:08 -08002213 for (size_t i = 0; i < mObjectsSize; i++) {
Arve Hjønnevåg6f286112014-02-19 20:42:13 -08002214 binder_size_t offset = mObjects[i];
Arve Hjønnevågf50b9ea2014-02-13 19:22:08 -08002215 if (offset < minOffset) {
Dan Albert3bdc5b82014-11-20 11:50:23 -08002216 ALOGE("%s: bad object offset %" PRIu64 " < %" PRIu64 "\n",
Arve Hjønnevåg6f286112014-02-19 20:42:13 -08002217 __func__, (uint64_t)offset, (uint64_t)minOffset);
Arve Hjønnevågf50b9ea2014-02-13 19:22:08 -08002218 mObjectsSize = 0;
2219 break;
2220 }
2221 minOffset = offset + sizeof(flat_binder_object);
2222 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002223 scanForFds();
2224}
2225
Colin Cross6f4f3ab2014-02-05 17:42:44 -08002226void Parcel::print(TextOutput& to, uint32_t /*flags*/) const
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002227{
2228 to << "Parcel(";
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002229
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002230 if (errorCheck() != NO_ERROR) {
2231 const status_t err = errorCheck();
Colin Cross6f4f3ab2014-02-05 17:42:44 -08002232 to << "Error: " << (void*)(intptr_t)err << " \"" << strerror(-err) << "\"";
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002233 } else if (dataSize() > 0) {
2234 const uint8_t* DATA = data();
2235 to << indent << HexDump(DATA, dataSize()) << dedent;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002236 const binder_size_t* OBJS = objects();
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002237 const size_t N = objectsCount();
2238 for (size_t i=0; i<N; i++) {
2239 const flat_binder_object* flat
2240 = reinterpret_cast<const flat_binder_object*>(DATA+OBJS[i]);
2241 to << endl << "Object #" << i << " @ " << (void*)OBJS[i] << ": "
2242 << TypeCode(flat->type & 0x7f7f7f00)
2243 << " = " << flat->binder;
2244 }
2245 } else {
2246 to << "NULL";
2247 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002248
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002249 to << ")";
2250}
2251
2252void Parcel::releaseObjects()
2253{
2254 const sp<ProcessState> proc(ProcessState::self());
2255 size_t i = mObjectsSize;
2256 uint8_t* const data = mData;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002257 binder_size_t* const objects = mObjects;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002258 while (i > 0) {
2259 i--;
2260 const flat_binder_object* flat
2261 = reinterpret_cast<flat_binder_object*>(data+objects[i]);
Adrian Rooscbf37262015-10-22 16:12:53 -07002262 release_object(proc, *flat, this, &mOpenAshmemSize);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002263 }
2264}
2265
2266void Parcel::acquireObjects()
2267{
2268 const sp<ProcessState> proc(ProcessState::self());
2269 size_t i = mObjectsSize;
2270 uint8_t* const data = mData;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002271 binder_size_t* const objects = mObjects;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002272 while (i > 0) {
2273 i--;
2274 const flat_binder_object* flat
2275 = reinterpret_cast<flat_binder_object*>(data+objects[i]);
Adrian Rooscbf37262015-10-22 16:12:53 -07002276 acquire_object(proc, *flat, this, &mOpenAshmemSize);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002277 }
2278}
2279
2280void Parcel::freeData()
2281{
2282 freeDataNoInit();
2283 initState();
2284}
2285
2286void Parcel::freeDataNoInit()
2287{
2288 if (mOwner) {
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002289 LOG_ALLOC("Parcel %p: freeing other owner data", this);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002290 //ALOGI("Freeing data ref of %p (pid=%d)", this, getpid());
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002291 mOwner(this, mData, mDataSize, mObjects, mObjectsSize, mOwnerCookie);
2292 } else {
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002293 LOG_ALLOC("Parcel %p: freeing allocated data", this);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002294 releaseObjects();
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002295 if (mData) {
2296 LOG_ALLOC("Parcel %p: freeing with %zu capacity", this, mDataCapacity);
Dianne Hackborna4cff882014-11-13 17:07:40 -08002297 pthread_mutex_lock(&gParcelGlobalAllocSizeLock);
Dan Austin48fd7b42015-09-10 13:46:02 -07002298 if (mDataCapacity <= gParcelGlobalAllocSize) {
2299 gParcelGlobalAllocSize = gParcelGlobalAllocSize - mDataCapacity;
2300 } else {
2301 gParcelGlobalAllocSize = 0;
2302 }
2303 if (gParcelGlobalAllocCount > 0) {
2304 gParcelGlobalAllocCount--;
2305 }
Dianne Hackborna4cff882014-11-13 17:07:40 -08002306 pthread_mutex_unlock(&gParcelGlobalAllocSizeLock);
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002307 free(mData);
2308 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002309 if (mObjects) free(mObjects);
2310 }
2311}
2312
2313status_t Parcel::growData(size_t len)
2314{
Nick Kralevichb6b14232015-04-02 09:36:02 -07002315 if (len > INT32_MAX) {
2316 // don't accept size_t values which may have come from an
2317 // inadvertent conversion from a negative int.
2318 return BAD_VALUE;
2319 }
2320
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002321 size_t newSize = ((mDataSize+len)*3)/2;
2322 return (newSize <= mDataSize)
2323 ? (status_t) NO_MEMORY
2324 : continueWrite(newSize);
2325}
2326
2327status_t Parcel::restartWrite(size_t desired)
2328{
Nick Kralevichb6b14232015-04-02 09:36:02 -07002329 if (desired > INT32_MAX) {
2330 // don't accept size_t values which may have come from an
2331 // inadvertent conversion from a negative int.
2332 return BAD_VALUE;
2333 }
2334
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002335 if (mOwner) {
2336 freeData();
2337 return continueWrite(desired);
2338 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002339
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002340 uint8_t* data = (uint8_t*)realloc(mData, desired);
2341 if (!data && desired > mDataCapacity) {
2342 mError = NO_MEMORY;
2343 return NO_MEMORY;
2344 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002345
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002346 releaseObjects();
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002347
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002348 if (data) {
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002349 LOG_ALLOC("Parcel %p: restart from %zu to %zu capacity", this, mDataCapacity, desired);
Dianne Hackborna4cff882014-11-13 17:07:40 -08002350 pthread_mutex_lock(&gParcelGlobalAllocSizeLock);
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002351 gParcelGlobalAllocSize += desired;
2352 gParcelGlobalAllocSize -= mDataCapacity;
Colin Cross83ec65e2015-12-08 17:15:50 -08002353 if (!mData) {
2354 gParcelGlobalAllocCount++;
2355 }
Dianne Hackborna4cff882014-11-13 17:07:40 -08002356 pthread_mutex_unlock(&gParcelGlobalAllocSizeLock);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002357 mData = data;
2358 mDataCapacity = desired;
2359 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002360
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002361 mDataSize = mDataPos = 0;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002362 ALOGV("restartWrite Setting data size of %p to %zu", this, mDataSize);
2363 ALOGV("restartWrite Setting data pos of %p to %zu", this, mDataPos);
2364
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002365 free(mObjects);
2366 mObjects = NULL;
2367 mObjectsSize = mObjectsCapacity = 0;
2368 mNextObjectHint = 0;
2369 mHasFds = false;
2370 mFdsKnown = true;
Dianne Hackborn8938ed22011-09-28 23:19:47 -04002371 mAllowFds = true;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002372
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002373 return NO_ERROR;
2374}
2375
2376status_t Parcel::continueWrite(size_t desired)
2377{
Nick Kralevichb6b14232015-04-02 09:36:02 -07002378 if (desired > INT32_MAX) {
2379 // don't accept size_t values which may have come from an
2380 // inadvertent conversion from a negative int.
2381 return BAD_VALUE;
2382 }
2383
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002384 // If shrinking, first adjust for any objects that appear
2385 // after the new data size.
2386 size_t objectsSize = mObjectsSize;
2387 if (desired < mDataSize) {
2388 if (desired == 0) {
2389 objectsSize = 0;
2390 } else {
2391 while (objectsSize > 0) {
2392 if (mObjects[objectsSize-1] < desired)
2393 break;
2394 objectsSize--;
2395 }
2396 }
2397 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002398
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002399 if (mOwner) {
2400 // If the size is going to zero, just release the owner's data.
2401 if (desired == 0) {
2402 freeData();
2403 return NO_ERROR;
2404 }
2405
2406 // If there is a different owner, we need to take
2407 // posession.
2408 uint8_t* data = (uint8_t*)malloc(desired);
2409 if (!data) {
2410 mError = NO_MEMORY;
2411 return NO_MEMORY;
2412 }
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002413 binder_size_t* objects = NULL;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002414
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002415 if (objectsSize) {
Nick Kraleviche9881a32015-04-28 16:21:30 -07002416 objects = (binder_size_t*)calloc(objectsSize, sizeof(binder_size_t));
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002417 if (!objects) {
Hyejin Kim3f727c02013-03-09 11:28:54 +09002418 free(data);
2419
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002420 mError = NO_MEMORY;
2421 return NO_MEMORY;
2422 }
2423
2424 // Little hack to only acquire references on objects
2425 // we will be keeping.
2426 size_t oldObjectsSize = mObjectsSize;
2427 mObjectsSize = objectsSize;
2428 acquireObjects();
2429 mObjectsSize = oldObjectsSize;
2430 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002431
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002432 if (mData) {
2433 memcpy(data, mData, mDataSize < desired ? mDataSize : desired);
2434 }
2435 if (objects && mObjects) {
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002436 memcpy(objects, mObjects, objectsSize*sizeof(binder_size_t));
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002437 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002438 //ALOGI("Freeing data ref of %p (pid=%d)", this, getpid());
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002439 mOwner(this, mData, mDataSize, mObjects, mObjectsSize, mOwnerCookie);
2440 mOwner = NULL;
2441
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002442 LOG_ALLOC("Parcel %p: taking ownership of %zu capacity", this, desired);
Dianne Hackborna4cff882014-11-13 17:07:40 -08002443 pthread_mutex_lock(&gParcelGlobalAllocSizeLock);
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002444 gParcelGlobalAllocSize += desired;
2445 gParcelGlobalAllocCount++;
Dianne Hackborna4cff882014-11-13 17:07:40 -08002446 pthread_mutex_unlock(&gParcelGlobalAllocSizeLock);
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002447
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002448 mData = data;
2449 mObjects = objects;
2450 mDataSize = (mDataSize < desired) ? mDataSize : desired;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002451 ALOGV("continueWrite Setting data size of %p to %zu", this, mDataSize);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002452 mDataCapacity = desired;
2453 mObjectsSize = mObjectsCapacity = objectsSize;
2454 mNextObjectHint = 0;
2455
2456 } else if (mData) {
2457 if (objectsSize < mObjectsSize) {
2458 // Need to release refs on any objects we are dropping.
2459 const sp<ProcessState> proc(ProcessState::self());
2460 for (size_t i=objectsSize; i<mObjectsSize; i++) {
2461 const flat_binder_object* flat
2462 = reinterpret_cast<flat_binder_object*>(mData+mObjects[i]);
2463 if (flat->type == BINDER_TYPE_FD) {
2464 // will need to rescan because we may have lopped off the only FDs
2465 mFdsKnown = false;
2466 }
Adrian Rooscbf37262015-10-22 16:12:53 -07002467 release_object(proc, *flat, this, &mOpenAshmemSize);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002468 }
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002469 binder_size_t* objects =
2470 (binder_size_t*)realloc(mObjects, objectsSize*sizeof(binder_size_t));
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002471 if (objects) {
2472 mObjects = objects;
2473 }
2474 mObjectsSize = objectsSize;
2475 mNextObjectHint = 0;
2476 }
2477
2478 // We own the data, so we can just do a realloc().
2479 if (desired > mDataCapacity) {
2480 uint8_t* data = (uint8_t*)realloc(mData, desired);
2481 if (data) {
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002482 LOG_ALLOC("Parcel %p: continue from %zu to %zu capacity", this, mDataCapacity,
2483 desired);
Dianne Hackborna4cff882014-11-13 17:07:40 -08002484 pthread_mutex_lock(&gParcelGlobalAllocSizeLock);
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002485 gParcelGlobalAllocSize += desired;
2486 gParcelGlobalAllocSize -= mDataCapacity;
Dianne Hackborna4cff882014-11-13 17:07:40 -08002487 pthread_mutex_unlock(&gParcelGlobalAllocSizeLock);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002488 mData = data;
2489 mDataCapacity = desired;
2490 } else if (desired > mDataCapacity) {
2491 mError = NO_MEMORY;
2492 return NO_MEMORY;
2493 }
2494 } else {
Dianne Hackborn97e2bcd2011-04-13 18:15:56 -07002495 if (mDataSize > desired) {
2496 mDataSize = desired;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002497 ALOGV("continueWrite Setting data size of %p to %zu", this, mDataSize);
Dianne Hackborn97e2bcd2011-04-13 18:15:56 -07002498 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002499 if (mDataPos > desired) {
2500 mDataPos = desired;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002501 ALOGV("continueWrite Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002502 }
2503 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002504
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002505 } else {
2506 // This is the first data. Easy!
2507 uint8_t* data = (uint8_t*)malloc(desired);
2508 if (!data) {
2509 mError = NO_MEMORY;
2510 return NO_MEMORY;
2511 }
Hyejin Kim3f727c02013-03-09 11:28:54 +09002512
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002513 if(!(mDataCapacity == 0 && mObjects == NULL
2514 && mObjectsCapacity == 0)) {
Colin Cross6f4f3ab2014-02-05 17:42:44 -08002515 ALOGE("continueWrite: %zu/%p/%zu/%zu", mDataCapacity, mObjects, mObjectsCapacity, desired);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002516 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002517
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002518 LOG_ALLOC("Parcel %p: allocating with %zu capacity", this, desired);
Dianne Hackborna4cff882014-11-13 17:07:40 -08002519 pthread_mutex_lock(&gParcelGlobalAllocSizeLock);
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002520 gParcelGlobalAllocSize += desired;
2521 gParcelGlobalAllocCount++;
Dianne Hackborna4cff882014-11-13 17:07:40 -08002522 pthread_mutex_unlock(&gParcelGlobalAllocSizeLock);
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002523
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002524 mData = data;
2525 mDataSize = mDataPos = 0;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002526 ALOGV("continueWrite Setting data size of %p to %zu", this, mDataSize);
2527 ALOGV("continueWrite Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002528 mDataCapacity = desired;
2529 }
2530
2531 return NO_ERROR;
2532}
2533
2534void Parcel::initState()
2535{
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002536 LOG_ALLOC("Parcel %p: initState", this);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002537 mError = NO_ERROR;
2538 mData = 0;
2539 mDataSize = 0;
2540 mDataCapacity = 0;
2541 mDataPos = 0;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002542 ALOGV("initState Setting data size of %p to %zu", this, mDataSize);
2543 ALOGV("initState Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002544 mObjects = NULL;
2545 mObjectsSize = 0;
2546 mObjectsCapacity = 0;
2547 mNextObjectHint = 0;
2548 mHasFds = false;
2549 mFdsKnown = true;
Dianne Hackborn8938ed22011-09-28 23:19:47 -04002550 mAllowFds = true;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002551 mOwner = NULL;
Adrian Rooscbf37262015-10-22 16:12:53 -07002552 mOpenAshmemSize = 0;
Christopher Tatee4e0ae82016-03-24 16:03:44 -07002553
2554 // racing multiple init leads only to multiple identical write
2555 if (gMaxFds == 0) {
2556 struct rlimit result;
2557 if (!getrlimit(RLIMIT_NOFILE, &result)) {
2558 gMaxFds = (size_t)result.rlim_cur;
Christopher Tatebf14e942016-03-25 14:16:24 -07002559 //ALOGI("parcel fd limit set to %zu", gMaxFds);
Christopher Tatee4e0ae82016-03-24 16:03:44 -07002560 } else {
2561 ALOGW("Unable to getrlimit: %s", strerror(errno));
2562 gMaxFds = 1024;
2563 }
2564 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002565}
2566
2567void Parcel::scanForFds() const
2568{
2569 bool hasFds = false;
2570 for (size_t i=0; i<mObjectsSize; i++) {
2571 const flat_binder_object* flat
2572 = reinterpret_cast<const flat_binder_object*>(mData + mObjects[i]);
2573 if (flat->type == BINDER_TYPE_FD) {
2574 hasFds = true;
2575 break;
2576 }
2577 }
2578 mHasFds = hasFds;
2579 mFdsKnown = true;
2580}
2581
Dan Sandleraa5c2342015-04-10 10:08:45 -04002582size_t Parcel::getBlobAshmemSize() const
2583{
Adrian Roos6bb31142015-10-22 16:46:12 -07002584 // This used to return the size of all blobs that were written to ashmem, now we're returning
2585 // the ashmem currently referenced by this Parcel, which should be equivalent.
2586 // TODO: Remove method once ABI can be changed.
2587 return mOpenAshmemSize;
Dan Sandleraa5c2342015-04-10 10:08:45 -04002588}
2589
Adrian Rooscbf37262015-10-22 16:12:53 -07002590size_t Parcel::getOpenAshmemSize() const
2591{
2592 return mOpenAshmemSize;
Jeff Brown5707dbf2011-09-23 21:17:56 -07002593}
2594
2595// --- Parcel::Blob ---
2596
2597Parcel::Blob::Blob() :
Jeff Brown13b16042014-11-11 16:44:25 -08002598 mFd(-1), mData(NULL), mSize(0), mMutable(false) {
Jeff Brown5707dbf2011-09-23 21:17:56 -07002599}
2600
2601Parcel::Blob::~Blob() {
2602 release();
2603}
2604
2605void Parcel::Blob::release() {
Jeff Brown13b16042014-11-11 16:44:25 -08002606 if (mFd != -1 && mData) {
Jeff Brown5707dbf2011-09-23 21:17:56 -07002607 ::munmap(mData, mSize);
2608 }
2609 clear();
2610}
2611
Jeff Brown13b16042014-11-11 16:44:25 -08002612void Parcel::Blob::init(int fd, void* data, size_t size, bool isMutable) {
2613 mFd = fd;
Jeff Brown5707dbf2011-09-23 21:17:56 -07002614 mData = data;
2615 mSize = size;
Jeff Brown13b16042014-11-11 16:44:25 -08002616 mMutable = isMutable;
Jeff Brown5707dbf2011-09-23 21:17:56 -07002617}
2618
2619void Parcel::Blob::clear() {
Jeff Brown13b16042014-11-11 16:44:25 -08002620 mFd = -1;
Jeff Brown5707dbf2011-09-23 21:17:56 -07002621 mData = NULL;
2622 mSize = 0;
Jeff Brown13b16042014-11-11 16:44:25 -08002623 mMutable = false;
Jeff Brown5707dbf2011-09-23 21:17:56 -07002624}
2625
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002626}; // namespace android