blob: 470fc5973500542e38cb040647dcf9e5cac321b3 [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
Dianne Hackborn1941a402016-08-29 12:30:43 -07001156status_t Parcel::writeParcelFileDescriptor(int fd, bool takeOwnership)
1157{
1158 writeInt32(0);
1159 return writeFileDescriptor(fd, takeOwnership);
1160}
1161
Christopher Wiley2cf19952016-04-11 11:09:37 -07001162status_t Parcel::writeUniqueFileDescriptor(const base::unique_fd& fd) {
Casey Dahlin06673e32015-11-23 13:24:23 -08001163 return writeDupFileDescriptor(fd.get());
1164}
1165
Christopher Wiley2cf19952016-04-11 11:09:37 -07001166status_t Parcel::writeUniqueFileDescriptorVector(const std::vector<base::unique_fd>& val) {
Casey Dahlin06673e32015-11-23 13:24:23 -08001167 return writeTypedVector(val, &Parcel::writeUniqueFileDescriptor);
1168}
1169
Christopher Wiley2cf19952016-04-11 11:09:37 -07001170status_t Parcel::writeUniqueFileDescriptorVector(const std::unique_ptr<std::vector<base::unique_fd>>& val) {
Casey Dahlinb9872622015-11-25 15:09:45 -08001171 return writeNullableTypedVector(val, &Parcel::writeUniqueFileDescriptor);
1172}
1173
Jeff Brown13b16042014-11-11 16:44:25 -08001174status_t Parcel::writeBlob(size_t len, bool mutableCopy, WritableBlob* outBlob)
Jeff Brown5707dbf2011-09-23 21:17:56 -07001175{
Nick Kralevichb6b14232015-04-02 09:36:02 -07001176 if (len > INT32_MAX) {
1177 // don't accept size_t values which may have come from an
1178 // inadvertent conversion from a negative int.
1179 return BAD_VALUE;
1180 }
1181
Jeff Brown13b16042014-11-11 16:44:25 -08001182 status_t status;
1183 if (!mAllowFds || len <= BLOB_INPLACE_LIMIT) {
Steve Block6807e592011-10-20 11:56:00 +01001184 ALOGV("writeBlob: write in place");
Jeff Brown13b16042014-11-11 16:44:25 -08001185 status = writeInt32(BLOB_INPLACE);
Jeff Brown5707dbf2011-09-23 21:17:56 -07001186 if (status) return status;
1187
1188 void* ptr = writeInplace(len);
1189 if (!ptr) return NO_MEMORY;
1190
Jeff Brown13b16042014-11-11 16:44:25 -08001191 outBlob->init(-1, ptr, len, false);
Jeff Brown5707dbf2011-09-23 21:17:56 -07001192 return NO_ERROR;
1193 }
1194
Steve Block6807e592011-10-20 11:56:00 +01001195 ALOGV("writeBlob: write to ashmem");
Jeff Brown5707dbf2011-09-23 21:17:56 -07001196 int fd = ashmem_create_region("Parcel Blob", len);
1197 if (fd < 0) return NO_MEMORY;
1198
1199 int result = ashmem_set_prot_region(fd, PROT_READ | PROT_WRITE);
1200 if (result < 0) {
Jeff Brownec4e0062011-10-10 14:50:10 -07001201 status = result;
Jeff Brown5707dbf2011-09-23 21:17:56 -07001202 } else {
1203 void* ptr = ::mmap(NULL, len, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
1204 if (ptr == MAP_FAILED) {
1205 status = -errno;
1206 } else {
Jeff Brown13b16042014-11-11 16:44:25 -08001207 if (!mutableCopy) {
1208 result = ashmem_set_prot_region(fd, PROT_READ);
1209 }
Jeff Brown5707dbf2011-09-23 21:17:56 -07001210 if (result < 0) {
Jeff Brownec4e0062011-10-10 14:50:10 -07001211 status = result;
Jeff Brown5707dbf2011-09-23 21:17:56 -07001212 } else {
Jeff Brown13b16042014-11-11 16:44:25 -08001213 status = writeInt32(mutableCopy ? BLOB_ASHMEM_MUTABLE : BLOB_ASHMEM_IMMUTABLE);
Jeff Brown5707dbf2011-09-23 21:17:56 -07001214 if (!status) {
Jeff Brown93ff1f92011-11-04 19:01:44 -07001215 status = writeFileDescriptor(fd, true /*takeOwnership*/);
Jeff Brown5707dbf2011-09-23 21:17:56 -07001216 if (!status) {
Jeff Brown13b16042014-11-11 16:44:25 -08001217 outBlob->init(fd, ptr, len, mutableCopy);
Jeff Brown5707dbf2011-09-23 21:17:56 -07001218 return NO_ERROR;
1219 }
1220 }
1221 }
1222 }
1223 ::munmap(ptr, len);
1224 }
1225 ::close(fd);
1226 return status;
1227}
1228
Jeff Brown13b16042014-11-11 16:44:25 -08001229status_t Parcel::writeDupImmutableBlobFileDescriptor(int fd)
1230{
1231 // Must match up with what's done in writeBlob.
1232 if (!mAllowFds) return FDS_NOT_ALLOWED;
1233 status_t status = writeInt32(BLOB_ASHMEM_IMMUTABLE);
1234 if (status) return status;
1235 return writeDupFileDescriptor(fd);
1236}
1237
Mathias Agopiane1424282013-07-29 21:24:40 -07001238status_t Parcel::write(const FlattenableHelperInterface& val)
Mathias Agopian98e71dd2010-02-11 17:30:52 -08001239{
1240 status_t err;
1241
1242 // size if needed
Mathias Agopiane1424282013-07-29 21:24:40 -07001243 const size_t len = val.getFlattenedSize();
1244 const size_t fd_count = val.getFdCount();
Mathias Agopian98e71dd2010-02-11 17:30:52 -08001245
Christopher Tatee4e0ae82016-03-24 16:03:44 -07001246 if ((len > INT32_MAX) || (fd_count >= gMaxFds)) {
Nick Kralevichb6b14232015-04-02 09:36:02 -07001247 // don't accept size_t values which may have come from an
1248 // inadvertent conversion from a negative int.
1249 return BAD_VALUE;
1250 }
1251
Mathias Agopian98e71dd2010-02-11 17:30:52 -08001252 err = this->writeInt32(len);
1253 if (err) return err;
1254
1255 err = this->writeInt32(fd_count);
1256 if (err) return err;
1257
1258 // payload
Nick Kralevichb6b14232015-04-02 09:36:02 -07001259 void* const buf = this->writeInplace(pad_size(len));
Mathias Agopian98e71dd2010-02-11 17:30:52 -08001260 if (buf == NULL)
1261 return BAD_VALUE;
1262
1263 int* fds = NULL;
1264 if (fd_count) {
Christopher Tatee4e0ae82016-03-24 16:03:44 -07001265 fds = new (std::nothrow) int[fd_count];
1266 if (fds == nullptr) {
1267 ALOGE("write: failed to allocate requested %zu fds", fd_count);
1268 return BAD_VALUE;
1269 }
Mathias Agopian98e71dd2010-02-11 17:30:52 -08001270 }
1271
1272 err = val.flatten(buf, len, fds, fd_count);
1273 for (size_t i=0 ; i<fd_count && err==NO_ERROR ; i++) {
1274 err = this->writeDupFileDescriptor( fds[i] );
1275 }
1276
1277 if (fd_count) {
1278 delete [] fds;
1279 }
1280
1281 return err;
1282}
1283
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001284status_t Parcel::writeObject(const flat_binder_object& val, bool nullMetaData)
1285{
1286 const bool enoughData = (mDataPos+sizeof(val)) <= mDataCapacity;
1287 const bool enoughObjects = mObjectsSize < mObjectsCapacity;
1288 if (enoughData && enoughObjects) {
1289restart_write:
1290 *reinterpret_cast<flat_binder_object*>(mData+mDataPos) = val;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001291
Christopher Tate98e67d32015-06-03 18:44:15 -07001292 // remember if it's a file descriptor
1293 if (val.type == BINDER_TYPE_FD) {
1294 if (!mAllowFds) {
1295 // fail before modifying our object index
1296 return FDS_NOT_ALLOWED;
1297 }
1298 mHasFds = mFdsKnown = true;
1299 }
1300
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001301 // Need to write meta-data?
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001302 if (nullMetaData || val.binder != 0) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001303 mObjects[mObjectsSize] = mDataPos;
Adrian Rooscbf37262015-10-22 16:12:53 -07001304 acquire_object(ProcessState::self(), val, this, &mOpenAshmemSize);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001305 mObjectsSize++;
1306 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001307
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001308 return finishWrite(sizeof(flat_binder_object));
1309 }
1310
1311 if (!enoughData) {
1312 const status_t err = growData(sizeof(val));
1313 if (err != NO_ERROR) return err;
1314 }
1315 if (!enoughObjects) {
1316 size_t newSize = ((mObjectsSize+2)*3)/2;
Christopher Tateed7a50c2015-06-08 14:45:14 -07001317 if (newSize < mObjectsSize) return NO_MEMORY; // overflow
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001318 binder_size_t* objects = (binder_size_t*)realloc(mObjects, newSize*sizeof(binder_size_t));
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001319 if (objects == NULL) return NO_MEMORY;
1320 mObjects = objects;
1321 mObjectsCapacity = newSize;
1322 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001323
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001324 goto restart_write;
1325}
1326
Brad Fitzpatrick837a0d02010-07-13 15:33:35 -07001327status_t Parcel::writeNoException()
1328{
Christopher Wiley09eb7492015-11-09 15:06:15 -08001329 binder::Status status;
1330 return status.writeToParcel(this);
Brad Fitzpatrick837a0d02010-07-13 15:33:35 -07001331}
1332
Colin Cross6f4f3ab2014-02-05 17:42:44 -08001333void Parcel::remove(size_t /*start*/, size_t /*amt*/)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001334{
1335 LOG_ALWAYS_FATAL("Parcel::remove() not yet implemented!");
1336}
1337
1338status_t Parcel::read(void* outData, size_t len) const
1339{
Nick Kralevichb6b14232015-04-02 09:36:02 -07001340 if (len > INT32_MAX) {
1341 // don't accept size_t values which may have come from an
1342 // inadvertent conversion from a negative int.
1343 return BAD_VALUE;
1344 }
1345
1346 if ((mDataPos+pad_size(len)) >= mDataPos && (mDataPos+pad_size(len)) <= mDataSize
1347 && len <= pad_size(len)) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001348 memcpy(outData, mData+mDataPos, len);
Nick Kralevichb6b14232015-04-02 09:36:02 -07001349 mDataPos += pad_size(len);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001350 ALOGV("read Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001351 return NO_ERROR;
1352 }
1353 return NOT_ENOUGH_DATA;
1354}
1355
1356const void* Parcel::readInplace(size_t len) const
1357{
Nick Kralevichb6b14232015-04-02 09:36:02 -07001358 if (len > INT32_MAX) {
1359 // don't accept size_t values which may have come from an
1360 // inadvertent conversion from a negative int.
1361 return NULL;
1362 }
1363
1364 if ((mDataPos+pad_size(len)) >= mDataPos && (mDataPos+pad_size(len)) <= mDataSize
1365 && len <= pad_size(len)) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001366 const void* data = mData+mDataPos;
Nick Kralevichb6b14232015-04-02 09:36:02 -07001367 mDataPos += pad_size(len);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001368 ALOGV("readInplace Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001369 return data;
1370 }
1371 return NULL;
1372}
1373
Andreas Huber84a6d042009-08-17 13:33:27 -07001374template<class T>
1375status_t Parcel::readAligned(T *pArg) const {
Nick Kralevichb6b14232015-04-02 09:36:02 -07001376 COMPILE_TIME_ASSERT_FUNCTION_SCOPE(PAD_SIZE_UNSAFE(sizeof(T)) == sizeof(T));
Andreas Huber84a6d042009-08-17 13:33:27 -07001377
1378 if ((mDataPos+sizeof(T)) <= mDataSize) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001379 const void* data = mData+mDataPos;
Andreas Huber84a6d042009-08-17 13:33:27 -07001380 mDataPos += sizeof(T);
1381 *pArg = *reinterpret_cast<const T*>(data);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001382 return NO_ERROR;
1383 } else {
1384 return NOT_ENOUGH_DATA;
1385 }
1386}
1387
Andreas Huber84a6d042009-08-17 13:33:27 -07001388template<class T>
1389T Parcel::readAligned() const {
1390 T result;
1391 if (readAligned(&result) != NO_ERROR) {
1392 result = 0;
1393 }
1394
1395 return result;
1396}
1397
1398template<class T>
1399status_t Parcel::writeAligned(T val) {
Nick Kralevichb6b14232015-04-02 09:36:02 -07001400 COMPILE_TIME_ASSERT_FUNCTION_SCOPE(PAD_SIZE_UNSAFE(sizeof(T)) == sizeof(T));
Andreas Huber84a6d042009-08-17 13:33:27 -07001401
1402 if ((mDataPos+sizeof(val)) <= mDataCapacity) {
1403restart_write:
1404 *reinterpret_cast<T*>(mData+mDataPos) = val;
1405 return finishWrite(sizeof(val));
1406 }
1407
1408 status_t err = growData(sizeof(val));
1409 if (err == NO_ERROR) goto restart_write;
1410 return err;
1411}
1412
Casey Dahlin185d3442016-02-09 11:08:35 -08001413namespace {
1414
1415template<typename T>
1416status_t readByteVectorInternal(const Parcel* parcel,
1417 std::vector<T>* val) {
Casey Dahlin451ff582015-10-19 18:12:18 -07001418 val->clear();
1419
1420 int32_t size;
Casey Dahlin185d3442016-02-09 11:08:35 -08001421 status_t status = parcel->readInt32(&size);
Casey Dahlin451ff582015-10-19 18:12:18 -07001422
1423 if (status != OK) {
1424 return status;
1425 }
1426
Christopher Wiley4db672d2015-11-10 09:44:30 -08001427 if (size < 0) {
1428 status = UNEXPECTED_NULL;
1429 return status;
1430 }
Casey Dahlin185d3442016-02-09 11:08:35 -08001431 if (size_t(size) > parcel->dataAvail()) {
Christopher Wileyf0fc52b2015-10-31 13:22:15 -07001432 status = BAD_VALUE;
1433 return status;
Casey Dahlin451ff582015-10-19 18:12:18 -07001434 }
Christopher Wiley4db672d2015-11-10 09:44:30 -08001435
Paul Lietar433e87b2016-09-16 10:39:32 -07001436 T* data = const_cast<T*>(reinterpret_cast<const T*>(parcel->readInplace(size)));
Christopher Wileyf0fc52b2015-10-31 13:22:15 -07001437 if (!data) {
1438 status = BAD_VALUE;
1439 return status;
1440 }
Paul Lietar433e87b2016-09-16 10:39:32 -07001441 val->reserve(size);
1442 val->insert(val->end(), data, data + size);
Casey Dahlin451ff582015-10-19 18:12:18 -07001443
Christopher Wileyf0fc52b2015-10-31 13:22:15 -07001444 return status;
Casey Dahlin451ff582015-10-19 18:12:18 -07001445}
1446
Casey Dahlin185d3442016-02-09 11:08:35 -08001447template<typename T>
1448status_t readByteVectorInternalPtr(
1449 const Parcel* parcel,
1450 std::unique_ptr<std::vector<T>>* val) {
1451 const int32_t start = parcel->dataPosition();
Casey Dahlinb9872622015-11-25 15:09:45 -08001452 int32_t size;
Casey Dahlin185d3442016-02-09 11:08:35 -08001453 status_t status = parcel->readInt32(&size);
Casey Dahlinb9872622015-11-25 15:09:45 -08001454 val->reset();
1455
1456 if (status != OK || size < 0) {
1457 return status;
1458 }
1459
Casey Dahlin185d3442016-02-09 11:08:35 -08001460 parcel->setDataPosition(start);
Christopher Tatee4e0ae82016-03-24 16:03:44 -07001461 val->reset(new (std::nothrow) std::vector<T>());
Casey Dahlinb9872622015-11-25 15:09:45 -08001462
Casey Dahlin185d3442016-02-09 11:08:35 -08001463 status = readByteVectorInternal(parcel, val->get());
Casey Dahlinb9872622015-11-25 15:09:45 -08001464
1465 if (status != OK) {
1466 val->reset();
1467 }
1468
1469 return status;
1470}
1471
Casey Dahlin185d3442016-02-09 11:08:35 -08001472} // namespace
1473
1474status_t Parcel::readByteVector(std::vector<int8_t>* val) const {
1475 return readByteVectorInternal(this, val);
1476}
1477
1478status_t Parcel::readByteVector(std::vector<uint8_t>* val) const {
1479 return readByteVectorInternal(this, val);
1480}
1481
1482status_t Parcel::readByteVector(std::unique_ptr<std::vector<int8_t>>* val) const {
1483 return readByteVectorInternalPtr(this, val);
1484}
1485
1486status_t Parcel::readByteVector(std::unique_ptr<std::vector<uint8_t>>* val) const {
1487 return readByteVectorInternalPtr(this, val);
1488}
1489
Casey Dahlinb9872622015-11-25 15:09:45 -08001490status_t Parcel::readInt32Vector(std::unique_ptr<std::vector<int32_t>>* val) const {
1491 return readNullableTypedVector(val, &Parcel::readInt32);
1492}
1493
Casey Dahlin451ff582015-10-19 18:12:18 -07001494status_t Parcel::readInt32Vector(std::vector<int32_t>* val) const {
Christopher Wiley03d1eb62015-11-19 06:42:40 -08001495 return readTypedVector(val, &Parcel::readInt32);
Casey Dahlin451ff582015-10-19 18:12:18 -07001496}
1497
Casey Dahlinb9872622015-11-25 15:09:45 -08001498status_t Parcel::readInt64Vector(std::unique_ptr<std::vector<int64_t>>* val) const {
1499 return readNullableTypedVector(val, &Parcel::readInt64);
1500}
1501
Casey Dahlin451ff582015-10-19 18:12:18 -07001502status_t Parcel::readInt64Vector(std::vector<int64_t>* val) const {
Christopher Wiley03d1eb62015-11-19 06:42:40 -08001503 return readTypedVector(val, &Parcel::readInt64);
Casey Dahlin451ff582015-10-19 18:12:18 -07001504}
1505
Casey Dahlinb9872622015-11-25 15:09:45 -08001506status_t Parcel::readFloatVector(std::unique_ptr<std::vector<float>>* val) const {
1507 return readNullableTypedVector(val, &Parcel::readFloat);
1508}
1509
Casey Dahlin451ff582015-10-19 18:12:18 -07001510status_t Parcel::readFloatVector(std::vector<float>* val) const {
Christopher Wiley03d1eb62015-11-19 06:42:40 -08001511 return readTypedVector(val, &Parcel::readFloat);
Casey Dahlin451ff582015-10-19 18:12:18 -07001512}
1513
Casey Dahlinb9872622015-11-25 15:09:45 -08001514status_t Parcel::readDoubleVector(std::unique_ptr<std::vector<double>>* val) const {
1515 return readNullableTypedVector(val, &Parcel::readDouble);
1516}
1517
Casey Dahlin451ff582015-10-19 18:12:18 -07001518status_t Parcel::readDoubleVector(std::vector<double>* val) const {
Christopher Wiley03d1eb62015-11-19 06:42:40 -08001519 return readTypedVector(val, &Parcel::readDouble);
Casey Dahlin451ff582015-10-19 18:12:18 -07001520}
1521
Casey Dahlinb9872622015-11-25 15:09:45 -08001522status_t Parcel::readBoolVector(std::unique_ptr<std::vector<bool>>* val) const {
1523 const int32_t start = dataPosition();
1524 int32_t size;
1525 status_t status = readInt32(&size);
1526 val->reset();
Casey Dahlin451ff582015-10-19 18:12:18 -07001527
Casey Dahlinb9872622015-11-25 15:09:45 -08001528 if (status != OK || size < 0) {
1529 return status;
1530 }
1531
1532 setDataPosition(start);
Christopher Tatee4e0ae82016-03-24 16:03:44 -07001533 val->reset(new (std::nothrow) std::vector<bool>());
Casey Dahlinb9872622015-11-25 15:09:45 -08001534
1535 status = readBoolVector(val->get());
1536
1537 if (status != OK) {
1538 val->reset();
1539 }
1540
1541 return status;
1542}
1543
1544status_t Parcel::readBoolVector(std::vector<bool>* val) const {
Casey Dahlin451ff582015-10-19 18:12:18 -07001545 int32_t size;
1546 status_t status = readInt32(&size);
1547
1548 if (status != OK) {
1549 return status;
1550 }
1551
1552 if (size < 0) {
Christopher Wiley4db672d2015-11-10 09:44:30 -08001553 return UNEXPECTED_NULL;
Casey Dahlin451ff582015-10-19 18:12:18 -07001554 }
1555
1556 val->resize(size);
1557
1558 /* C++ bool handling means a vector of bools isn't necessarily addressable
1559 * (we might use individual bits)
1560 */
Christopher Wiley97887982015-10-27 16:33:47 -07001561 bool data;
1562 for (int32_t i = 0; i < size; ++i) {
Casey Dahlin451ff582015-10-19 18:12:18 -07001563 status = readBool(&data);
1564 (*val)[i] = data;
1565
1566 if (status != OK) {
1567 return status;
1568 }
1569 }
1570
1571 return OK;
1572}
1573
Casey Dahlinb9872622015-11-25 15:09:45 -08001574status_t Parcel::readCharVector(std::unique_ptr<std::vector<char16_t>>* val) const {
1575 return readNullableTypedVector(val, &Parcel::readChar);
1576}
1577
Casey Dahlin451ff582015-10-19 18:12:18 -07001578status_t Parcel::readCharVector(std::vector<char16_t>* val) const {
Christopher Wiley03d1eb62015-11-19 06:42:40 -08001579 return readTypedVector(val, &Parcel::readChar);
Casey Dahlin451ff582015-10-19 18:12:18 -07001580}
1581
Casey Dahlinb9872622015-11-25 15:09:45 -08001582status_t Parcel::readString16Vector(
1583 std::unique_ptr<std::vector<std::unique_ptr<String16>>>* val) const {
1584 return readNullableTypedVector(val, &Parcel::readString16);
1585}
1586
Casey Dahlin451ff582015-10-19 18:12:18 -07001587status_t Parcel::readString16Vector(std::vector<String16>* val) const {
Christopher Wiley03d1eb62015-11-19 06:42:40 -08001588 return readTypedVector(val, &Parcel::readString16);
Casey Dahlin451ff582015-10-19 18:12:18 -07001589}
1590
Christopher Wiley9a5e32f2016-01-28 16:56:53 -08001591status_t Parcel::readUtf8VectorFromUtf16Vector(
1592 std::unique_ptr<std::vector<std::unique_ptr<std::string>>>* val) const {
1593 return readNullableTypedVector(val, &Parcel::readUtf8FromUtf16);
1594}
1595
1596status_t Parcel::readUtf8VectorFromUtf16Vector(std::vector<std::string>* val) const {
1597 return readTypedVector(val, &Parcel::readUtf8FromUtf16);
1598}
Casey Dahlin451ff582015-10-19 18:12:18 -07001599
Andreas Huber84a6d042009-08-17 13:33:27 -07001600status_t Parcel::readInt32(int32_t *pArg) const
1601{
1602 return readAligned(pArg);
1603}
1604
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001605int32_t Parcel::readInt32() const
1606{
Andreas Huber84a6d042009-08-17 13:33:27 -07001607 return readAligned<int32_t>();
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001608}
1609
Dan Stoza41a0f2f2014-12-01 10:01:10 -08001610status_t Parcel::readUint32(uint32_t *pArg) const
1611{
1612 return readAligned(pArg);
1613}
1614
1615uint32_t Parcel::readUint32() const
1616{
1617 return readAligned<uint32_t>();
1618}
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001619
1620status_t Parcel::readInt64(int64_t *pArg) const
1621{
Andreas Huber84a6d042009-08-17 13:33:27 -07001622 return readAligned(pArg);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001623}
1624
1625
1626int64_t Parcel::readInt64() const
1627{
Andreas Huber84a6d042009-08-17 13:33:27 -07001628 return readAligned<int64_t>();
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001629}
1630
Ronghua Wu2d13afd2015-03-16 11:11:07 -07001631status_t Parcel::readUint64(uint64_t *pArg) const
1632{
1633 return readAligned(pArg);
1634}
1635
1636uint64_t Parcel::readUint64() const
1637{
1638 return readAligned<uint64_t>();
1639}
1640
Serban Constantinescuf683e012013-11-05 16:53:55 +00001641status_t Parcel::readPointer(uintptr_t *pArg) const
1642{
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001643 status_t ret;
1644 binder_uintptr_t ptr;
1645 ret = readAligned(&ptr);
1646 if (!ret)
1647 *pArg = ptr;
1648 return ret;
Serban Constantinescuf683e012013-11-05 16:53:55 +00001649}
1650
1651uintptr_t Parcel::readPointer() const
1652{
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001653 return readAligned<binder_uintptr_t>();
Serban Constantinescuf683e012013-11-05 16:53:55 +00001654}
1655
1656
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001657status_t Parcel::readFloat(float *pArg) const
1658{
Andreas Huber84a6d042009-08-17 13:33:27 -07001659 return readAligned(pArg);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001660}
1661
1662
1663float Parcel::readFloat() const
1664{
Andreas Huber84a6d042009-08-17 13:33:27 -07001665 return readAligned<float>();
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001666}
1667
Douglas Leungcc1a4bb2013-01-11 15:00:55 -08001668#if defined(__mips__) && defined(__mips_hard_float)
1669
1670status_t Parcel::readDouble(double *pArg) const
1671{
1672 union {
1673 double d;
1674 unsigned long long ll;
1675 } u;
Narayan Kamath2c68d382014-06-04 15:04:29 +01001676 u.d = 0;
Douglas Leungcc1a4bb2013-01-11 15:00:55 -08001677 status_t status;
1678 status = readAligned(&u.ll);
1679 *pArg = u.d;
1680 return status;
1681}
1682
1683double Parcel::readDouble() const
1684{
1685 union {
1686 double d;
1687 unsigned long long ll;
1688 } u;
1689 u.ll = readAligned<unsigned long long>();
1690 return u.d;
1691}
1692
1693#else
1694
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001695status_t Parcel::readDouble(double *pArg) const
1696{
Andreas Huber84a6d042009-08-17 13:33:27 -07001697 return readAligned(pArg);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001698}
1699
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001700double Parcel::readDouble() const
1701{
Andreas Huber84a6d042009-08-17 13:33:27 -07001702 return readAligned<double>();
1703}
1704
Douglas Leungcc1a4bb2013-01-11 15:00:55 -08001705#endif
1706
Andreas Huber84a6d042009-08-17 13:33:27 -07001707status_t Parcel::readIntPtr(intptr_t *pArg) const
1708{
1709 return readAligned(pArg);
1710}
1711
1712
1713intptr_t Parcel::readIntPtr() const
1714{
1715 return readAligned<intptr_t>();
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001716}
1717
Casey Dahlind6848f52015-10-15 15:44:59 -07001718status_t Parcel::readBool(bool *pArg) const
1719{
1720 int32_t tmp;
1721 status_t ret = readInt32(&tmp);
1722 *pArg = (tmp != 0);
1723 return ret;
1724}
1725
1726bool Parcel::readBool() const
1727{
1728 return readInt32() != 0;
1729}
1730
1731status_t Parcel::readChar(char16_t *pArg) const
1732{
1733 int32_t tmp;
1734 status_t ret = readInt32(&tmp);
1735 *pArg = char16_t(tmp);
1736 return ret;
1737}
1738
1739char16_t Parcel::readChar() const
1740{
1741 return char16_t(readInt32());
1742}
1743
1744status_t Parcel::readByte(int8_t *pArg) const
1745{
1746 int32_t tmp;
1747 status_t ret = readInt32(&tmp);
1748 *pArg = int8_t(tmp);
1749 return ret;
1750}
1751
1752int8_t Parcel::readByte() const
1753{
1754 return int8_t(readInt32());
1755}
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001756
Christopher Wiley9a5e32f2016-01-28 16:56:53 -08001757status_t Parcel::readUtf8FromUtf16(std::string* str) const {
1758 size_t utf16Size = 0;
1759 const char16_t* src = readString16Inplace(&utf16Size);
1760 if (!src) {
1761 return UNEXPECTED_NULL;
1762 }
1763
1764 // Save ourselves the trouble, we're done.
1765 if (utf16Size == 0u) {
1766 str->clear();
1767 return NO_ERROR;
1768 }
1769
Sergio Giro9b39ebe2016-06-28 18:19:33 +01001770 // Allow for closing '\0'
1771 ssize_t utf8Size = utf16_to_utf8_length(src, utf16Size) + 1;
1772 if (utf8Size < 1) {
Christopher Wiley9a5e32f2016-01-28 16:56:53 -08001773 return BAD_VALUE;
1774 }
1775 // Note that while it is probably safe to assume string::resize keeps a
Sergio Giro9b39ebe2016-06-28 18:19:33 +01001776 // spare byte around for the trailing null, we still pass the size including the trailing null
Christopher Wiley9a5e32f2016-01-28 16:56:53 -08001777 str->resize(utf8Size);
Sergio Giro9b39ebe2016-06-28 18:19:33 +01001778 utf16_to_utf8(src, utf16Size, &((*str)[0]), utf8Size);
1779 str->resize(utf8Size - 1);
Christopher Wiley9a5e32f2016-01-28 16:56:53 -08001780 return NO_ERROR;
1781}
1782
1783status_t Parcel::readUtf8FromUtf16(std::unique_ptr<std::string>* str) const {
1784 const int32_t start = dataPosition();
1785 int32_t size;
1786 status_t status = readInt32(&size);
1787 str->reset();
1788
1789 if (status != OK || size < 0) {
1790 return status;
1791 }
1792
1793 setDataPosition(start);
Christopher Tatee4e0ae82016-03-24 16:03:44 -07001794 str->reset(new (std::nothrow) std::string());
Christopher Wiley9a5e32f2016-01-28 16:56:53 -08001795 return readUtf8FromUtf16(str->get());
1796}
1797
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001798const char* Parcel::readCString() const
1799{
1800 const size_t avail = mDataSize-mDataPos;
1801 if (avail > 0) {
1802 const char* str = reinterpret_cast<const char*>(mData+mDataPos);
1803 // is the string's trailing NUL within the parcel's valid bounds?
1804 const char* eos = reinterpret_cast<const char*>(memchr(str, 0, avail));
1805 if (eos) {
1806 const size_t len = eos - str;
Nick Kralevichb6b14232015-04-02 09:36:02 -07001807 mDataPos += pad_size(len+1);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001808 ALOGV("readCString Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001809 return str;
1810 }
1811 }
1812 return NULL;
1813}
1814
1815String8 Parcel::readString8() const
1816{
Roshan Pius87b64d22016-07-18 12:51:02 -07001817 String8 retString;
1818 status_t status = readString8(&retString);
1819 if (status != OK) {
1820 // We don't care about errors here, so just return an empty string.
1821 return String8();
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001822 }
Roshan Pius87b64d22016-07-18 12:51:02 -07001823 return retString;
1824}
1825
1826status_t Parcel::readString8(String8* pArg) const
1827{
1828 int32_t size;
1829 status_t status = readInt32(&size);
1830 if (status != OK) {
1831 return status;
1832 }
1833 // watch for potential int overflow from size+1
1834 if (size < 0 || size >= INT32_MAX) {
1835 return BAD_VALUE;
1836 }
1837 // |writeString8| writes nothing for empty string.
1838 if (size == 0) {
1839 *pArg = String8();
1840 return OK;
1841 }
1842 const char* str = (const char*)readInplace(size + 1);
1843 if (str == NULL) {
1844 return BAD_VALUE;
1845 }
1846 pArg->setTo(str, size);
1847 return OK;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001848}
1849
1850String16 Parcel::readString16() const
1851{
1852 size_t len;
1853 const char16_t* str = readString16Inplace(&len);
1854 if (str) return String16(str, len);
Steve Blocke6f43dd2012-01-06 19:20:56 +00001855 ALOGE("Reading a NULL string not supported here.");
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001856 return String16();
1857}
1858
Casey Dahlinb9872622015-11-25 15:09:45 -08001859status_t Parcel::readString16(std::unique_ptr<String16>* pArg) const
1860{
1861 const int32_t start = dataPosition();
1862 int32_t size;
1863 status_t status = readInt32(&size);
1864 pArg->reset();
1865
1866 if (status != OK || size < 0) {
1867 return status;
1868 }
1869
1870 setDataPosition(start);
Christopher Tatee4e0ae82016-03-24 16:03:44 -07001871 pArg->reset(new (std::nothrow) String16());
Casey Dahlinb9872622015-11-25 15:09:45 -08001872
1873 status = readString16(pArg->get());
1874
1875 if (status != OK) {
1876 pArg->reset();
1877 }
1878
1879 return status;
1880}
1881
Casey Dahlin451ff582015-10-19 18:12:18 -07001882status_t Parcel::readString16(String16* pArg) const
1883{
1884 size_t len;
1885 const char16_t* str = readString16Inplace(&len);
1886 if (str) {
Casey Dahlin1515ea12015-10-20 16:26:23 -07001887 pArg->setTo(str, len);
Casey Dahlin451ff582015-10-19 18:12:18 -07001888 return 0;
1889 } else {
1890 *pArg = String16();
Christopher Wiley4db672d2015-11-10 09:44:30 -08001891 return UNEXPECTED_NULL;
Casey Dahlin451ff582015-10-19 18:12:18 -07001892 }
1893}
1894
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001895const char16_t* Parcel::readString16Inplace(size_t* outLen) const
1896{
1897 int32_t size = readInt32();
1898 // watch for potential int overflow from size+1
1899 if (size >= 0 && size < INT32_MAX) {
1900 *outLen = size;
1901 const char16_t* str = (const char16_t*)readInplace((size+1)*sizeof(char16_t));
1902 if (str != NULL) {
1903 return str;
1904 }
1905 }
1906 *outLen = 0;
1907 return NULL;
1908}
1909
Casey Dahlinf0c13772015-10-27 18:33:56 -07001910status_t Parcel::readStrongBinder(sp<IBinder>* val) const
1911{
Christopher Wiley35d77ca2016-03-08 10:49:51 -08001912 status_t status = readNullableStrongBinder(val);
1913 if (status == OK && !val->get()) {
1914 status = UNEXPECTED_NULL;
1915 }
1916 return status;
1917}
1918
1919status_t Parcel::readNullableStrongBinder(sp<IBinder>* val) const
1920{
Casey Dahlinf0c13772015-10-27 18:33:56 -07001921 return unflatten_binder(ProcessState::self(), *this, val);
1922}
1923
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001924sp<IBinder> Parcel::readStrongBinder() const
1925{
1926 sp<IBinder> val;
Christopher Wiley35d77ca2016-03-08 10:49:51 -08001927 // Note that a lot of code in Android reads binders by hand with this
1928 // method, and that code has historically been ok with getting nullptr
1929 // back (while ignoring error codes).
1930 readNullableStrongBinder(&val);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001931 return val;
1932}
1933
1934wp<IBinder> Parcel::readWeakBinder() const
1935{
1936 wp<IBinder> val;
1937 unflatten_binder(ProcessState::self(), *this, &val);
1938 return val;
1939}
1940
Christopher Wiley97f048d2015-11-19 06:49:05 -08001941status_t Parcel::readParcelable(Parcelable* parcelable) const {
1942 int32_t have_parcelable = 0;
1943 status_t status = readInt32(&have_parcelable);
1944 if (status != OK) {
1945 return status;
1946 }
1947 if (!have_parcelable) {
1948 return UNEXPECTED_NULL;
1949 }
1950 return parcelable->readFromParcel(this);
1951}
1952
Brad Fitzpatrick837a0d02010-07-13 15:33:35 -07001953int32_t Parcel::readExceptionCode() const
1954{
Christopher Wiley09eb7492015-11-09 15:06:15 -08001955 binder::Status status;
1956 status.readFromParcel(*this);
1957 return status.exceptionCode();
Brad Fitzpatrick837a0d02010-07-13 15:33:35 -07001958}
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001959
Mathias Agopiana47f02a2009-05-21 16:29:38 -07001960native_handle* Parcel::readNativeHandle() const
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001961{
1962 int numFds, numInts;
1963 status_t err;
1964 err = readInt32(&numFds);
1965 if (err != NO_ERROR) return 0;
1966 err = readInt32(&numInts);
1967 if (err != NO_ERROR) return 0;
1968
Mathias Agopiana47f02a2009-05-21 16:29:38 -07001969 native_handle* h = native_handle_create(numFds, numInts);
Adam Lesinskieaac99a2015-05-12 17:35:48 -07001970 if (!h) {
1971 return 0;
1972 }
1973
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001974 for (int i=0 ; err==NO_ERROR && i<numFds ; i++) {
Rebecca Schultz Zavin360211f2009-02-13 16:34:38 -08001975 h->data[i] = dup(readFileDescriptor());
Marco Nelissen1de79662016-04-26 08:44:09 -07001976 if (h->data[i] < 0) {
1977 for (int j = 0; j < i; j++) {
1978 close(h->data[j]);
1979 }
1980 native_handle_delete(h);
1981 return 0;
1982 }
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001983 }
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001984 err = read(h->data + numFds, sizeof(int)*numInts);
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001985 if (err != NO_ERROR) {
Mathias Agopiana47f02a2009-05-21 16:29:38 -07001986 native_handle_close(h);
1987 native_handle_delete(h);
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001988 h = 0;
1989 }
1990 return h;
1991}
1992
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001993int Parcel::readFileDescriptor() const
1994{
1995 const flat_binder_object* flat = readObject(true);
Casey Dahlin06673e32015-11-23 13:24:23 -08001996
1997 if (flat && flat->type == BINDER_TYPE_FD) {
1998 return flat->handle;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001999 }
Casey Dahlin06673e32015-11-23 13:24:23 -08002000
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002001 return BAD_TYPE;
2002}
2003
Dianne Hackborn1941a402016-08-29 12:30:43 -07002004int Parcel::readParcelFileDescriptor() const
2005{
2006 int32_t hasComm = readInt32();
2007 int fd = readFileDescriptor();
2008 if (hasComm != 0) {
2009 // skip
2010 readFileDescriptor();
2011 }
2012 return fd;
2013}
2014
Christopher Wiley2cf19952016-04-11 11:09:37 -07002015status_t Parcel::readUniqueFileDescriptor(base::unique_fd* val) const
Casey Dahlin06673e32015-11-23 13:24:23 -08002016{
2017 int got = readFileDescriptor();
2018
2019 if (got == BAD_TYPE) {
2020 return BAD_TYPE;
2021 }
2022
2023 val->reset(dup(got));
2024
2025 if (val->get() < 0) {
2026 return BAD_VALUE;
2027 }
2028
2029 return OK;
2030}
2031
2032
Christopher Wiley2cf19952016-04-11 11:09:37 -07002033status_t Parcel::readUniqueFileDescriptorVector(std::unique_ptr<std::vector<base::unique_fd>>* val) const {
Casey Dahlinb9872622015-11-25 15:09:45 -08002034 return readNullableTypedVector(val, &Parcel::readUniqueFileDescriptor);
2035}
2036
Christopher Wiley2cf19952016-04-11 11:09:37 -07002037status_t Parcel::readUniqueFileDescriptorVector(std::vector<base::unique_fd>* val) const {
Casey Dahlin06673e32015-11-23 13:24:23 -08002038 return readTypedVector(val, &Parcel::readUniqueFileDescriptor);
2039}
2040
Jeff Brown5707dbf2011-09-23 21:17:56 -07002041status_t Parcel::readBlob(size_t len, ReadableBlob* outBlob) const
2042{
Jeff Brown13b16042014-11-11 16:44:25 -08002043 int32_t blobType;
2044 status_t status = readInt32(&blobType);
Jeff Brown5707dbf2011-09-23 21:17:56 -07002045 if (status) return status;
2046
Jeff Brown13b16042014-11-11 16:44:25 -08002047 if (blobType == BLOB_INPLACE) {
Steve Block6807e592011-10-20 11:56:00 +01002048 ALOGV("readBlob: read in place");
Jeff Brown5707dbf2011-09-23 21:17:56 -07002049 const void* ptr = readInplace(len);
2050 if (!ptr) return BAD_VALUE;
2051
Jeff Brown13b16042014-11-11 16:44:25 -08002052 outBlob->init(-1, const_cast<void*>(ptr), len, false);
Jeff Brown5707dbf2011-09-23 21:17:56 -07002053 return NO_ERROR;
2054 }
2055
Steve Block6807e592011-10-20 11:56:00 +01002056 ALOGV("readBlob: read from ashmem");
Jeff Brown13b16042014-11-11 16:44:25 -08002057 bool isMutable = (blobType == BLOB_ASHMEM_MUTABLE);
Jeff Brown5707dbf2011-09-23 21:17:56 -07002058 int fd = readFileDescriptor();
2059 if (fd == int(BAD_TYPE)) return BAD_VALUE;
2060
Jeff Brown13b16042014-11-11 16:44:25 -08002061 void* ptr = ::mmap(NULL, len, isMutable ? PROT_READ | PROT_WRITE : PROT_READ,
2062 MAP_SHARED, fd, 0);
Narayan Kamath9ea09752014-10-08 17:35:45 +01002063 if (ptr == MAP_FAILED) return NO_MEMORY;
Jeff Brown5707dbf2011-09-23 21:17:56 -07002064
Jeff Brown13b16042014-11-11 16:44:25 -08002065 outBlob->init(fd, ptr, len, isMutable);
Jeff Brown5707dbf2011-09-23 21:17:56 -07002066 return NO_ERROR;
2067}
2068
Mathias Agopiane1424282013-07-29 21:24:40 -07002069status_t Parcel::read(FlattenableHelperInterface& val) const
Mathias Agopian98e71dd2010-02-11 17:30:52 -08002070{
2071 // size
2072 const size_t len = this->readInt32();
2073 const size_t fd_count = this->readInt32();
2074
Christopher Tatee4e0ae82016-03-24 16:03:44 -07002075 if ((len > INT32_MAX) || (fd_count >= gMaxFds)) {
Nick Kralevichb6b14232015-04-02 09:36:02 -07002076 // don't accept size_t values which may have come from an
2077 // inadvertent conversion from a negative int.
2078 return BAD_VALUE;
2079 }
2080
Mathias Agopian98e71dd2010-02-11 17:30:52 -08002081 // payload
Nick Kralevichb6b14232015-04-02 09:36:02 -07002082 void const* const buf = this->readInplace(pad_size(len));
Mathias Agopian98e71dd2010-02-11 17:30:52 -08002083 if (buf == NULL)
2084 return BAD_VALUE;
2085
2086 int* fds = NULL;
2087 if (fd_count) {
Christopher Tatee4e0ae82016-03-24 16:03:44 -07002088 fds = new (std::nothrow) int[fd_count];
2089 if (fds == nullptr) {
2090 ALOGE("read: failed to allocate requested %zu fds", fd_count);
2091 return BAD_VALUE;
2092 }
Mathias Agopian98e71dd2010-02-11 17:30:52 -08002093 }
2094
2095 status_t err = NO_ERROR;
2096 for (size_t i=0 ; i<fd_count && err==NO_ERROR ; i++) {
Fabien Sanglardd84ff312016-10-21 10:58:26 -07002097 int fd = this->readFileDescriptor();
2098 if (fd < 0 || ((fds[i] = dup(fd)) < 0)) {
Jun Jiangabf8a2c2014-04-29 14:22:10 +08002099 err = BAD_VALUE;
Jesse Hallfee99042014-11-04 08:36:31 -08002100 ALOGE("dup() failed in Parcel::read, i is %zu, fds[i] is %d, fd_count is %zu, error: %s",
Fabien Sanglardd84ff312016-10-21 10:58:26 -07002101 i, fds[i], fd_count, strerror(fd < 0 ? -fd : errno));
2102 // Close all the file descriptors that were dup-ed.
2103 for (size_t j=0; j<i ;j++) {
2104 close(fds[j]);
2105 }
Jun Jiangabf8a2c2014-04-29 14:22:10 +08002106 }
Mathias Agopian98e71dd2010-02-11 17:30:52 -08002107 }
2108
2109 if (err == NO_ERROR) {
2110 err = val.unflatten(buf, len, fds, fd_count);
2111 }
2112
2113 if (fd_count) {
2114 delete [] fds;
2115 }
2116
2117 return err;
2118}
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002119const flat_binder_object* Parcel::readObject(bool nullMetaData) const
2120{
2121 const size_t DPOS = mDataPos;
2122 if ((DPOS+sizeof(flat_binder_object)) <= mDataSize) {
2123 const flat_binder_object* obj
2124 = reinterpret_cast<const flat_binder_object*>(mData+DPOS);
2125 mDataPos = DPOS + sizeof(flat_binder_object);
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002126 if (!nullMetaData && (obj->cookie == 0 && obj->binder == 0)) {
The Android Open Source Project5f78a482009-01-20 14:03:58 -08002127 // When transferring a NULL object, we don't write it into
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002128 // the object list, so we don't want to check for it when
2129 // reading.
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002130 ALOGV("readObject Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002131 return obj;
2132 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002133
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002134 // Ensure that this object is valid...
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002135 binder_size_t* const OBJS = mObjects;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002136 const size_t N = mObjectsSize;
2137 size_t opos = mNextObjectHint;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002138
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002139 if (N > 0) {
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002140 ALOGV("Parcel %p looking for obj at %zu, hint=%zu",
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002141 this, DPOS, opos);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002142
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002143 // Start at the current hint position, looking for an object at
2144 // the current data position.
2145 if (opos < N) {
2146 while (opos < (N-1) && OBJS[opos] < DPOS) {
2147 opos++;
2148 }
2149 } else {
2150 opos = N-1;
2151 }
2152 if (OBJS[opos] == DPOS) {
2153 // Found it!
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002154 ALOGV("Parcel %p found obj %zu at index %zu with forward search",
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002155 this, DPOS, opos);
2156 mNextObjectHint = opos+1;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002157 ALOGV("readObject Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002158 return obj;
2159 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002160
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002161 // Look backwards for it...
2162 while (opos > 0 && OBJS[opos] > DPOS) {
2163 opos--;
2164 }
2165 if (OBJS[opos] == DPOS) {
2166 // Found it!
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002167 ALOGV("Parcel %p found obj %zu at index %zu with backward search",
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002168 this, DPOS, opos);
2169 mNextObjectHint = opos+1;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002170 ALOGV("readObject Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002171 return obj;
2172 }
2173 }
Colin Cross6f4f3ab2014-02-05 17:42:44 -08002174 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 -07002175 this, DPOS);
2176 }
2177 return NULL;
2178}
2179
2180void Parcel::closeFileDescriptors()
2181{
2182 size_t i = mObjectsSize;
2183 if (i > 0) {
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002184 //ALOGI("Closing file descriptors for %zu objects...", i);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002185 }
2186 while (i > 0) {
2187 i--;
2188 const flat_binder_object* flat
2189 = reinterpret_cast<flat_binder_object*>(mData+mObjects[i]);
2190 if (flat->type == BINDER_TYPE_FD) {
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002191 //ALOGI("Closing fd: %ld", flat->handle);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002192 close(flat->handle);
2193 }
2194 }
2195}
2196
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002197uintptr_t Parcel::ipcData() const
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002198{
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002199 return reinterpret_cast<uintptr_t>(mData);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002200}
2201
2202size_t Parcel::ipcDataSize() const
2203{
2204 return (mDataSize > mDataPos ? mDataSize : mDataPos);
2205}
2206
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002207uintptr_t Parcel::ipcObjects() const
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002208{
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002209 return reinterpret_cast<uintptr_t>(mObjects);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002210}
2211
2212size_t Parcel::ipcObjectsCount() const
2213{
2214 return mObjectsSize;
2215}
2216
2217void Parcel::ipcSetDataReference(const uint8_t* data, size_t dataSize,
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002218 const binder_size_t* objects, size_t objectsCount, release_func relFunc, void* relCookie)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002219{
Arve Hjønnevåg6f286112014-02-19 20:42:13 -08002220 binder_size_t minOffset = 0;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002221 freeDataNoInit();
2222 mError = NO_ERROR;
2223 mData = const_cast<uint8_t*>(data);
2224 mDataSize = mDataCapacity = dataSize;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002225 //ALOGI("setDataReference Setting data size of %p to %lu (pid=%d)", this, mDataSize, getpid());
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002226 mDataPos = 0;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002227 ALOGV("setDataReference Setting data pos of %p to %zu", this, mDataPos);
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002228 mObjects = const_cast<binder_size_t*>(objects);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002229 mObjectsSize = mObjectsCapacity = objectsCount;
2230 mNextObjectHint = 0;
2231 mOwner = relFunc;
2232 mOwnerCookie = relCookie;
Arve Hjønnevågf50b9ea2014-02-13 19:22:08 -08002233 for (size_t i = 0; i < mObjectsSize; i++) {
Arve Hjønnevåg6f286112014-02-19 20:42:13 -08002234 binder_size_t offset = mObjects[i];
Arve Hjønnevågf50b9ea2014-02-13 19:22:08 -08002235 if (offset < minOffset) {
Dan Albert3bdc5b82014-11-20 11:50:23 -08002236 ALOGE("%s: bad object offset %" PRIu64 " < %" PRIu64 "\n",
Arve Hjønnevåg6f286112014-02-19 20:42:13 -08002237 __func__, (uint64_t)offset, (uint64_t)minOffset);
Arve Hjønnevågf50b9ea2014-02-13 19:22:08 -08002238 mObjectsSize = 0;
2239 break;
2240 }
2241 minOffset = offset + sizeof(flat_binder_object);
2242 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002243 scanForFds();
2244}
2245
Colin Cross6f4f3ab2014-02-05 17:42:44 -08002246void Parcel::print(TextOutput& to, uint32_t /*flags*/) const
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002247{
2248 to << "Parcel(";
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002249
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002250 if (errorCheck() != NO_ERROR) {
2251 const status_t err = errorCheck();
Colin Cross6f4f3ab2014-02-05 17:42:44 -08002252 to << "Error: " << (void*)(intptr_t)err << " \"" << strerror(-err) << "\"";
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002253 } else if (dataSize() > 0) {
2254 const uint8_t* DATA = data();
2255 to << indent << HexDump(DATA, dataSize()) << dedent;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002256 const binder_size_t* OBJS = objects();
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002257 const size_t N = objectsCount();
2258 for (size_t i=0; i<N; i++) {
2259 const flat_binder_object* flat
2260 = reinterpret_cast<const flat_binder_object*>(DATA+OBJS[i]);
2261 to << endl << "Object #" << i << " @ " << (void*)OBJS[i] << ": "
2262 << TypeCode(flat->type & 0x7f7f7f00)
2263 << " = " << flat->binder;
2264 }
2265 } else {
2266 to << "NULL";
2267 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002268
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002269 to << ")";
2270}
2271
2272void Parcel::releaseObjects()
2273{
2274 const sp<ProcessState> proc(ProcessState::self());
2275 size_t i = mObjectsSize;
2276 uint8_t* const data = mData;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002277 binder_size_t* const objects = mObjects;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002278 while (i > 0) {
2279 i--;
2280 const flat_binder_object* flat
2281 = reinterpret_cast<flat_binder_object*>(data+objects[i]);
Adrian Rooscbf37262015-10-22 16:12:53 -07002282 release_object(proc, *flat, this, &mOpenAshmemSize);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002283 }
2284}
2285
2286void Parcel::acquireObjects()
2287{
2288 const sp<ProcessState> proc(ProcessState::self());
2289 size_t i = mObjectsSize;
2290 uint8_t* const data = mData;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002291 binder_size_t* const objects = mObjects;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002292 while (i > 0) {
2293 i--;
2294 const flat_binder_object* flat
2295 = reinterpret_cast<flat_binder_object*>(data+objects[i]);
Adrian Rooscbf37262015-10-22 16:12:53 -07002296 acquire_object(proc, *flat, this, &mOpenAshmemSize);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002297 }
2298}
2299
2300void Parcel::freeData()
2301{
2302 freeDataNoInit();
2303 initState();
2304}
2305
2306void Parcel::freeDataNoInit()
2307{
2308 if (mOwner) {
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002309 LOG_ALLOC("Parcel %p: freeing other owner data", this);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002310 //ALOGI("Freeing data ref of %p (pid=%d)", this, getpid());
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002311 mOwner(this, mData, mDataSize, mObjects, mObjectsSize, mOwnerCookie);
2312 } else {
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002313 LOG_ALLOC("Parcel %p: freeing allocated data", this);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002314 releaseObjects();
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002315 if (mData) {
2316 LOG_ALLOC("Parcel %p: freeing with %zu capacity", this, mDataCapacity);
Dianne Hackborna4cff882014-11-13 17:07:40 -08002317 pthread_mutex_lock(&gParcelGlobalAllocSizeLock);
Dan Austin48fd7b42015-09-10 13:46:02 -07002318 if (mDataCapacity <= gParcelGlobalAllocSize) {
2319 gParcelGlobalAllocSize = gParcelGlobalAllocSize - mDataCapacity;
2320 } else {
2321 gParcelGlobalAllocSize = 0;
2322 }
2323 if (gParcelGlobalAllocCount > 0) {
2324 gParcelGlobalAllocCount--;
2325 }
Dianne Hackborna4cff882014-11-13 17:07:40 -08002326 pthread_mutex_unlock(&gParcelGlobalAllocSizeLock);
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002327 free(mData);
2328 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002329 if (mObjects) free(mObjects);
2330 }
2331}
2332
2333status_t Parcel::growData(size_t len)
2334{
Nick Kralevichb6b14232015-04-02 09:36:02 -07002335 if (len > INT32_MAX) {
2336 // don't accept size_t values which may have come from an
2337 // inadvertent conversion from a negative int.
2338 return BAD_VALUE;
2339 }
2340
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002341 size_t newSize = ((mDataSize+len)*3)/2;
2342 return (newSize <= mDataSize)
2343 ? (status_t) NO_MEMORY
2344 : continueWrite(newSize);
2345}
2346
2347status_t Parcel::restartWrite(size_t desired)
2348{
Nick Kralevichb6b14232015-04-02 09:36:02 -07002349 if (desired > INT32_MAX) {
2350 // don't accept size_t values which may have come from an
2351 // inadvertent conversion from a negative int.
2352 return BAD_VALUE;
2353 }
2354
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002355 if (mOwner) {
2356 freeData();
2357 return continueWrite(desired);
2358 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002359
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002360 uint8_t* data = (uint8_t*)realloc(mData, desired);
2361 if (!data && desired > mDataCapacity) {
2362 mError = NO_MEMORY;
2363 return NO_MEMORY;
2364 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002365
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002366 releaseObjects();
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002367
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002368 if (data) {
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002369 LOG_ALLOC("Parcel %p: restart from %zu to %zu capacity", this, mDataCapacity, desired);
Dianne Hackborna4cff882014-11-13 17:07:40 -08002370 pthread_mutex_lock(&gParcelGlobalAllocSizeLock);
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002371 gParcelGlobalAllocSize += desired;
2372 gParcelGlobalAllocSize -= mDataCapacity;
Colin Cross83ec65e2015-12-08 17:15:50 -08002373 if (!mData) {
2374 gParcelGlobalAllocCount++;
2375 }
Dianne Hackborna4cff882014-11-13 17:07:40 -08002376 pthread_mutex_unlock(&gParcelGlobalAllocSizeLock);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002377 mData = data;
2378 mDataCapacity = desired;
2379 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002380
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002381 mDataSize = mDataPos = 0;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002382 ALOGV("restartWrite Setting data size of %p to %zu", this, mDataSize);
2383 ALOGV("restartWrite Setting data pos of %p to %zu", this, mDataPos);
2384
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002385 free(mObjects);
2386 mObjects = NULL;
2387 mObjectsSize = mObjectsCapacity = 0;
2388 mNextObjectHint = 0;
2389 mHasFds = false;
2390 mFdsKnown = true;
Dianne Hackborn8938ed22011-09-28 23:19:47 -04002391 mAllowFds = true;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002392
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002393 return NO_ERROR;
2394}
2395
2396status_t Parcel::continueWrite(size_t desired)
2397{
Nick Kralevichb6b14232015-04-02 09:36:02 -07002398 if (desired > INT32_MAX) {
2399 // don't accept size_t values which may have come from an
2400 // inadvertent conversion from a negative int.
2401 return BAD_VALUE;
2402 }
2403
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002404 // If shrinking, first adjust for any objects that appear
2405 // after the new data size.
2406 size_t objectsSize = mObjectsSize;
2407 if (desired < mDataSize) {
2408 if (desired == 0) {
2409 objectsSize = 0;
2410 } else {
2411 while (objectsSize > 0) {
2412 if (mObjects[objectsSize-1] < desired)
2413 break;
2414 objectsSize--;
2415 }
2416 }
2417 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002418
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002419 if (mOwner) {
2420 // If the size is going to zero, just release the owner's data.
2421 if (desired == 0) {
2422 freeData();
2423 return NO_ERROR;
2424 }
2425
2426 // If there is a different owner, we need to take
2427 // posession.
2428 uint8_t* data = (uint8_t*)malloc(desired);
2429 if (!data) {
2430 mError = NO_MEMORY;
2431 return NO_MEMORY;
2432 }
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002433 binder_size_t* objects = NULL;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002434
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002435 if (objectsSize) {
Nick Kraleviche9881a32015-04-28 16:21:30 -07002436 objects = (binder_size_t*)calloc(objectsSize, sizeof(binder_size_t));
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002437 if (!objects) {
Hyejin Kim3f727c02013-03-09 11:28:54 +09002438 free(data);
2439
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002440 mError = NO_MEMORY;
2441 return NO_MEMORY;
2442 }
2443
2444 // Little hack to only acquire references on objects
2445 // we will be keeping.
2446 size_t oldObjectsSize = mObjectsSize;
2447 mObjectsSize = objectsSize;
2448 acquireObjects();
2449 mObjectsSize = oldObjectsSize;
2450 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002451
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002452 if (mData) {
2453 memcpy(data, mData, mDataSize < desired ? mDataSize : desired);
2454 }
2455 if (objects && mObjects) {
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002456 memcpy(objects, mObjects, objectsSize*sizeof(binder_size_t));
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002457 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002458 //ALOGI("Freeing data ref of %p (pid=%d)", this, getpid());
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002459 mOwner(this, mData, mDataSize, mObjects, mObjectsSize, mOwnerCookie);
2460 mOwner = NULL;
2461
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002462 LOG_ALLOC("Parcel %p: taking ownership of %zu capacity", this, desired);
Dianne Hackborna4cff882014-11-13 17:07:40 -08002463 pthread_mutex_lock(&gParcelGlobalAllocSizeLock);
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002464 gParcelGlobalAllocSize += desired;
2465 gParcelGlobalAllocCount++;
Dianne Hackborna4cff882014-11-13 17:07:40 -08002466 pthread_mutex_unlock(&gParcelGlobalAllocSizeLock);
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002467
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002468 mData = data;
2469 mObjects = objects;
2470 mDataSize = (mDataSize < desired) ? mDataSize : desired;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002471 ALOGV("continueWrite Setting data size of %p to %zu", this, mDataSize);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002472 mDataCapacity = desired;
2473 mObjectsSize = mObjectsCapacity = objectsSize;
2474 mNextObjectHint = 0;
2475
2476 } else if (mData) {
2477 if (objectsSize < mObjectsSize) {
2478 // Need to release refs on any objects we are dropping.
2479 const sp<ProcessState> proc(ProcessState::self());
2480 for (size_t i=objectsSize; i<mObjectsSize; i++) {
2481 const flat_binder_object* flat
2482 = reinterpret_cast<flat_binder_object*>(mData+mObjects[i]);
2483 if (flat->type == BINDER_TYPE_FD) {
2484 // will need to rescan because we may have lopped off the only FDs
2485 mFdsKnown = false;
2486 }
Adrian Rooscbf37262015-10-22 16:12:53 -07002487 release_object(proc, *flat, this, &mOpenAshmemSize);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002488 }
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002489 binder_size_t* objects =
2490 (binder_size_t*)realloc(mObjects, objectsSize*sizeof(binder_size_t));
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002491 if (objects) {
2492 mObjects = objects;
2493 }
2494 mObjectsSize = objectsSize;
2495 mNextObjectHint = 0;
2496 }
2497
2498 // We own the data, so we can just do a realloc().
2499 if (desired > mDataCapacity) {
2500 uint8_t* data = (uint8_t*)realloc(mData, desired);
2501 if (data) {
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002502 LOG_ALLOC("Parcel %p: continue from %zu to %zu capacity", this, mDataCapacity,
2503 desired);
Dianne Hackborna4cff882014-11-13 17:07:40 -08002504 pthread_mutex_lock(&gParcelGlobalAllocSizeLock);
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002505 gParcelGlobalAllocSize += desired;
2506 gParcelGlobalAllocSize -= mDataCapacity;
Dianne Hackborna4cff882014-11-13 17:07:40 -08002507 pthread_mutex_unlock(&gParcelGlobalAllocSizeLock);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002508 mData = data;
2509 mDataCapacity = desired;
2510 } else if (desired > mDataCapacity) {
2511 mError = NO_MEMORY;
2512 return NO_MEMORY;
2513 }
2514 } else {
Dianne Hackborn97e2bcd2011-04-13 18:15:56 -07002515 if (mDataSize > desired) {
2516 mDataSize = desired;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002517 ALOGV("continueWrite Setting data size of %p to %zu", this, mDataSize);
Dianne Hackborn97e2bcd2011-04-13 18:15:56 -07002518 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002519 if (mDataPos > desired) {
2520 mDataPos = desired;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002521 ALOGV("continueWrite Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002522 }
2523 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002524
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002525 } else {
2526 // This is the first data. Easy!
2527 uint8_t* data = (uint8_t*)malloc(desired);
2528 if (!data) {
2529 mError = NO_MEMORY;
2530 return NO_MEMORY;
2531 }
Hyejin Kim3f727c02013-03-09 11:28:54 +09002532
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002533 if(!(mDataCapacity == 0 && mObjects == NULL
2534 && mObjectsCapacity == 0)) {
Colin Cross6f4f3ab2014-02-05 17:42:44 -08002535 ALOGE("continueWrite: %zu/%p/%zu/%zu", mDataCapacity, mObjects, mObjectsCapacity, desired);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002536 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002537
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002538 LOG_ALLOC("Parcel %p: allocating with %zu capacity", this, desired);
Dianne Hackborna4cff882014-11-13 17:07:40 -08002539 pthread_mutex_lock(&gParcelGlobalAllocSizeLock);
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002540 gParcelGlobalAllocSize += desired;
2541 gParcelGlobalAllocCount++;
Dianne Hackborna4cff882014-11-13 17:07:40 -08002542 pthread_mutex_unlock(&gParcelGlobalAllocSizeLock);
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002543
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002544 mData = data;
2545 mDataSize = mDataPos = 0;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002546 ALOGV("continueWrite Setting data size of %p to %zu", this, mDataSize);
2547 ALOGV("continueWrite Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002548 mDataCapacity = desired;
2549 }
2550
2551 return NO_ERROR;
2552}
2553
2554void Parcel::initState()
2555{
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002556 LOG_ALLOC("Parcel %p: initState", this);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002557 mError = NO_ERROR;
2558 mData = 0;
2559 mDataSize = 0;
2560 mDataCapacity = 0;
2561 mDataPos = 0;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002562 ALOGV("initState Setting data size of %p to %zu", this, mDataSize);
2563 ALOGV("initState Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002564 mObjects = NULL;
2565 mObjectsSize = 0;
2566 mObjectsCapacity = 0;
2567 mNextObjectHint = 0;
2568 mHasFds = false;
2569 mFdsKnown = true;
Dianne Hackborn8938ed22011-09-28 23:19:47 -04002570 mAllowFds = true;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002571 mOwner = NULL;
Adrian Rooscbf37262015-10-22 16:12:53 -07002572 mOpenAshmemSize = 0;
Christopher Tatee4e0ae82016-03-24 16:03:44 -07002573
2574 // racing multiple init leads only to multiple identical write
2575 if (gMaxFds == 0) {
2576 struct rlimit result;
2577 if (!getrlimit(RLIMIT_NOFILE, &result)) {
2578 gMaxFds = (size_t)result.rlim_cur;
Christopher Tatebf14e942016-03-25 14:16:24 -07002579 //ALOGI("parcel fd limit set to %zu", gMaxFds);
Christopher Tatee4e0ae82016-03-24 16:03:44 -07002580 } else {
2581 ALOGW("Unable to getrlimit: %s", strerror(errno));
2582 gMaxFds = 1024;
2583 }
2584 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002585}
2586
2587void Parcel::scanForFds() const
2588{
2589 bool hasFds = false;
2590 for (size_t i=0; i<mObjectsSize; i++) {
2591 const flat_binder_object* flat
2592 = reinterpret_cast<const flat_binder_object*>(mData + mObjects[i]);
2593 if (flat->type == BINDER_TYPE_FD) {
2594 hasFds = true;
2595 break;
2596 }
2597 }
2598 mHasFds = hasFds;
2599 mFdsKnown = true;
2600}
2601
Dan Sandleraa5c2342015-04-10 10:08:45 -04002602size_t Parcel::getBlobAshmemSize() const
2603{
Adrian Roos6bb31142015-10-22 16:46:12 -07002604 // This used to return the size of all blobs that were written to ashmem, now we're returning
2605 // the ashmem currently referenced by this Parcel, which should be equivalent.
2606 // TODO: Remove method once ABI can be changed.
2607 return mOpenAshmemSize;
Dan Sandleraa5c2342015-04-10 10:08:45 -04002608}
2609
Adrian Rooscbf37262015-10-22 16:12:53 -07002610size_t Parcel::getOpenAshmemSize() const
2611{
2612 return mOpenAshmemSize;
Jeff Brown5707dbf2011-09-23 21:17:56 -07002613}
2614
2615// --- Parcel::Blob ---
2616
2617Parcel::Blob::Blob() :
Jeff Brown13b16042014-11-11 16:44:25 -08002618 mFd(-1), mData(NULL), mSize(0), mMutable(false) {
Jeff Brown5707dbf2011-09-23 21:17:56 -07002619}
2620
2621Parcel::Blob::~Blob() {
2622 release();
2623}
2624
2625void Parcel::Blob::release() {
Jeff Brown13b16042014-11-11 16:44:25 -08002626 if (mFd != -1 && mData) {
Jeff Brown5707dbf2011-09-23 21:17:56 -07002627 ::munmap(mData, mSize);
2628 }
2629 clear();
2630}
2631
Jeff Brown13b16042014-11-11 16:44:25 -08002632void Parcel::Blob::init(int fd, void* data, size_t size, bool isMutable) {
2633 mFd = fd;
Jeff Brown5707dbf2011-09-23 21:17:56 -07002634 mData = data;
2635 mSize = size;
Jeff Brown13b16042014-11-11 16:44:25 -08002636 mMutable = isMutable;
Jeff Brown5707dbf2011-09-23 21:17:56 -07002637}
2638
2639void Parcel::Blob::clear() {
Jeff Brown13b16042014-11-11 16:44:25 -08002640 mFd = -1;
Jeff Brown5707dbf2011-09-23 21:17:56 -07002641 mData = NULL;
2642 mSize = 0;
Jeff Brown13b16042014-11-11 16:44:25 -08002643 mMutable = false;
Jeff Brown5707dbf2011-09-23 21:17:56 -07002644}
2645
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002646}; // namespace android