blob: 55374306f3e1409d0e424ef2c073a47daa9c826e [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>
Steven Morelanda4853cd2019-07-12 15:44:37 -070050#include "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 Sharkey05827be2018-06-26 10:52:38 -060076#define STRICT_MODE_PENALTY_GATHER (1 << 31)
Brad Fitzpatricka877cd82010-07-07 16:06:39 -070077
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -070078namespace android {
79
Dianne Hackborna4cff882014-11-13 17:07:40 -080080static pthread_mutex_t gParcelGlobalAllocSizeLock = PTHREAD_MUTEX_INITIALIZER;
81static size_t gParcelGlobalAllocSize = 0;
82static size_t gParcelGlobalAllocCount = 0;
83
Christopher Tatee4e0ae82016-03-24 16:03:44 -070084static size_t gMaxFds = 0;
85
Jeff Brown13b16042014-11-11 16:44:25 -080086// Maximum size of a blob to transfer in-place.
87static const size_t BLOB_INPLACE_LIMIT = 16 * 1024;
88
89enum {
90 BLOB_INPLACE = 0,
91 BLOB_ASHMEM_IMMUTABLE = 1,
92 BLOB_ASHMEM_MUTABLE = 2,
93};
94
Steven Morelandb1c81202019-04-05 18:49:55 -070095static void acquire_object(const sp<ProcessState>& proc,
Adrian Rooscbf37262015-10-22 16:12:53 -070096 const flat_binder_object& obj, const void* who, size_t* outAshmemSize)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -070097{
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -070098 switch (obj.hdr.type) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -070099 case BINDER_TYPE_BINDER:
100 if (obj.binder) {
101 LOG_REFS("Parcel %p acquiring reference on local %p", who, obj.cookie);
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800102 reinterpret_cast<IBinder*>(obj.cookie)->incStrong(who);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700103 }
104 return;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700105 case BINDER_TYPE_HANDLE: {
106 const sp<IBinder> b = proc->getStrongProxyForHandle(obj.handle);
Yi Kong91635562018-06-07 14:38:36 -0700107 if (b != nullptr) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700108 LOG_REFS("Parcel %p acquiring reference on remote %p", who, b.get());
109 b->incStrong(who);
110 }
111 return;
112 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700113 case BINDER_TYPE_FD: {
Jorim Jaggi150b4ef2018-07-13 11:18:30 +0000114 if ((obj.cookie != 0) && (outAshmemSize != nullptr) && ashmem_valid(obj.handle)) {
Mark Salyzyn80589362016-08-23 16:15:04 -0700115 // If we own an ashmem fd, keep track of how much memory it refers to.
116 int size = ashmem_get_size_region(obj.handle);
117 if (size > 0) {
118 *outAshmemSize += size;
Adrian Rooscbf37262015-10-22 16:12:53 -0700119 }
120 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700121 return;
122 }
123 }
124
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -0700125 ALOGD("Invalid object type 0x%08x", obj.hdr.type);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700126}
127
Adrian Roos6bb31142015-10-22 16:46:12 -0700128static void release_object(const sp<ProcessState>& proc,
Adrian Rooscbf37262015-10-22 16:12:53 -0700129 const flat_binder_object& obj, const void* who, size_t* outAshmemSize)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700130{
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -0700131 switch (obj.hdr.type) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700132 case BINDER_TYPE_BINDER:
133 if (obj.binder) {
134 LOG_REFS("Parcel %p releasing reference on local %p", who, obj.cookie);
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800135 reinterpret_cast<IBinder*>(obj.cookie)->decStrong(who);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700136 }
137 return;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700138 case BINDER_TYPE_HANDLE: {
139 const sp<IBinder> b = proc->getStrongProxyForHandle(obj.handle);
Yi Kong91635562018-06-07 14:38:36 -0700140 if (b != nullptr) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700141 LOG_REFS("Parcel %p releasing reference on remote %p", who, b.get());
142 b->decStrong(who);
143 }
144 return;
145 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700146 case BINDER_TYPE_FD: {
Mark Salyzynb454d8f2016-01-27 08:02:48 -0800147 if (obj.cookie != 0) { // owned
Jorim Jaggi150b4ef2018-07-13 11:18:30 +0000148 if ((outAshmemSize != nullptr) && ashmem_valid(obj.handle)) {
Mark Salyzyn80589362016-08-23 16:15:04 -0700149 int size = ashmem_get_size_region(obj.handle);
150 if (size > 0) {
Tri Voaa6e1112019-01-29 13:23:46 -0800151 // ashmem size might have changed since last time it was accounted for, e.g.
152 // in acquire_object(). Value of *outAshmemSize is not critical since we are
153 // releasing the object anyway. Check for integer overflow condition.
154 *outAshmemSize -= std::min(*outAshmemSize, static_cast<size_t>(size));
Adrian Roos6bb31142015-10-22 16:46:12 -0700155 }
Adrian Roos6bb31142015-10-22 16:46:12 -0700156 }
Mark Salyzynb454d8f2016-01-27 08:02:48 -0800157
158 close(obj.handle);
Adrian Rooscbf37262015-10-22 16:12:53 -0700159 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700160 return;
161 }
162 }
163
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -0700164 ALOGE("Invalid object type 0x%08x", obj.hdr.type);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700165}
166
167inline static status_t finish_flatten_binder(
Colin Cross6f4f3ab2014-02-05 17:42:44 -0800168 const sp<IBinder>& /*binder*/, const flat_binder_object& flat, Parcel* out)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700169{
170 return out->writeObject(flat, false);
171}
172
Steven Morelandb1c81202019-04-05 18:49:55 -0700173static status_t flatten_binder(const sp<ProcessState>& /*proc*/,
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700174 const sp<IBinder>& binder, Parcel* out)
175{
176 flat_binder_object obj;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700177
Martijn Coenen2b631742017-05-05 11:16:59 -0700178 if (IPCThreadState::self()->backgroundSchedulingDisabled()) {
179 /* minimum priority for all nodes is nice 0 */
180 obj.flags = FLAT_BINDER_FLAG_ACCEPTS_FDS;
181 } else {
182 /* minimum priority for all nodes is MAX_NICE(19) */
183 obj.flags = 0x13 | FLAT_BINDER_FLAG_ACCEPTS_FDS;
184 }
185
Yi Kong91635562018-06-07 14:38:36 -0700186 if (binder != nullptr) {
Steven Morelandf0212002018-12-26 13:59:23 -0800187 BBinder *local = binder->localBinder();
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700188 if (!local) {
189 BpBinder *proxy = binder->remoteBinder();
Yi Kong91635562018-06-07 14:38:36 -0700190 if (proxy == nullptr) {
Steve Blocke6f43dd2012-01-06 19:20:56 +0000191 ALOGE("null proxy");
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700192 }
193 const int32_t handle = proxy ? proxy->handle() : 0;
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -0700194 obj.hdr.type = BINDER_TYPE_HANDLE;
Arve Hjønnevåg07fd0f12014-02-18 21:10:29 -0800195 obj.binder = 0; /* Don't pass uninitialized stack data to a remote process */
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700196 obj.handle = handle;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800197 obj.cookie = 0;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700198 } else {
Steven Morelandf0212002018-12-26 13:59:23 -0800199 if (local->isRequestingSid()) {
200 obj.flags |= FLAT_BINDER_FLAG_TXN_SECURITY_CTX;
201 }
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -0700202 obj.hdr.type = BINDER_TYPE_BINDER;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800203 obj.binder = reinterpret_cast<uintptr_t>(local->getWeakRefs());
204 obj.cookie = reinterpret_cast<uintptr_t>(local);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700205 }
206 } else {
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -0700207 obj.hdr.type = BINDER_TYPE_BINDER;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800208 obj.binder = 0;
209 obj.cookie = 0;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700210 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700211
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700212 return finish_flatten_binder(binder, obj, out);
213}
214
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700215inline static status_t finish_unflatten_binder(
Colin Cross6f4f3ab2014-02-05 17:42:44 -0800216 BpBinder* /*proxy*/, const flat_binder_object& /*flat*/,
217 const Parcel& /*in*/)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700218{
219 return NO_ERROR;
220}
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700221
Steven Morelandb1c81202019-04-05 18:49:55 -0700222static status_t unflatten_binder(const sp<ProcessState>& proc,
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700223 const Parcel& in, sp<IBinder>* out)
224{
225 const flat_binder_object* flat = in.readObject(false);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700226
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700227 if (flat) {
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -0700228 switch (flat->hdr.type) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700229 case BINDER_TYPE_BINDER:
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800230 *out = reinterpret_cast<IBinder*>(flat->cookie);
Yi Kong91635562018-06-07 14:38:36 -0700231 return finish_unflatten_binder(nullptr, *flat, in);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700232 case BINDER_TYPE_HANDLE:
233 *out = proc->getStrongProxyForHandle(flat->handle);
234 return finish_unflatten_binder(
235 static_cast<BpBinder*>(out->get()), *flat, in);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700236 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700237 }
238 return BAD_TYPE;
239}
240
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700241// ---------------------------------------------------------------------------
242
243Parcel::Parcel()
244{
Dianne Hackborn7e790af2014-11-11 12:22:53 -0800245 LOG_ALLOC("Parcel %p: constructing", this);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700246 initState();
247}
248
249Parcel::~Parcel()
250{
251 freeDataNoInit();
Dianne Hackborn7e790af2014-11-11 12:22:53 -0800252 LOG_ALLOC("Parcel %p: destroyed", this);
253}
254
255size_t Parcel::getGlobalAllocSize() {
Dianne Hackborna4cff882014-11-13 17:07:40 -0800256 pthread_mutex_lock(&gParcelGlobalAllocSizeLock);
257 size_t size = gParcelGlobalAllocSize;
258 pthread_mutex_unlock(&gParcelGlobalAllocSizeLock);
259 return size;
Dianne Hackborn7e790af2014-11-11 12:22:53 -0800260}
261
262size_t Parcel::getGlobalAllocCount() {
Dianne Hackborna4cff882014-11-13 17:07:40 -0800263 pthread_mutex_lock(&gParcelGlobalAllocSizeLock);
264 size_t count = gParcelGlobalAllocCount;
265 pthread_mutex_unlock(&gParcelGlobalAllocSizeLock);
266 return count;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700267}
268
269const uint8_t* Parcel::data() const
270{
271 return mData;
272}
273
274size_t Parcel::dataSize() const
275{
276 return (mDataSize > mDataPos ? mDataSize : mDataPos);
277}
278
279size_t Parcel::dataAvail() const
280{
Nick Kralevichcfe27de2015-09-16 09:49:15 -0700281 size_t result = dataSize() - dataPosition();
282 if (result > INT32_MAX) {
283 abort();
284 }
285 return result;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700286}
287
288size_t Parcel::dataPosition() const
289{
290 return mDataPos;
291}
292
293size_t Parcel::dataCapacity() const
294{
295 return mDataCapacity;
296}
297
298status_t Parcel::setDataSize(size_t size)
299{
Nick Kralevichb6b14232015-04-02 09:36:02 -0700300 if (size > INT32_MAX) {
301 // don't accept size_t values which may have come from an
302 // inadvertent conversion from a negative int.
303 return BAD_VALUE;
304 }
305
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700306 status_t err;
307 err = continueWrite(size);
308 if (err == NO_ERROR) {
309 mDataSize = size;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700310 ALOGV("setDataSize Setting data size of %p to %zu", this, mDataSize);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700311 }
312 return err;
313}
314
315void Parcel::setDataPosition(size_t pos) const
316{
Nick Kralevichb6b14232015-04-02 09:36:02 -0700317 if (pos > INT32_MAX) {
318 // don't accept size_t values which may have come from an
319 // inadvertent conversion from a negative int.
320 abort();
321 }
322
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700323 mDataPos = pos;
324 mNextObjectHint = 0;
Michael Wachenschwanzc5176812017-11-17 18:25:05 -0800325 mObjectsSorted = false;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700326}
327
328status_t Parcel::setDataCapacity(size_t size)
329{
Nick Kralevichb6b14232015-04-02 09:36:02 -0700330 if (size > INT32_MAX) {
331 // don't accept size_t values which may have come from an
332 // inadvertent conversion from a negative int.
333 return BAD_VALUE;
334 }
335
Dianne Hackborn97e2bcd2011-04-13 18:15:56 -0700336 if (size > mDataCapacity) return continueWrite(size);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700337 return NO_ERROR;
338}
339
340status_t Parcel::setData(const uint8_t* buffer, size_t len)
341{
Nick Kralevichb6b14232015-04-02 09:36:02 -0700342 if (len > INT32_MAX) {
343 // don't accept size_t values which may have come from an
344 // inadvertent conversion from a negative int.
345 return BAD_VALUE;
346 }
347
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700348 status_t err = restartWrite(len);
349 if (err == NO_ERROR) {
350 memcpy(const_cast<uint8_t*>(data()), buffer, len);
351 mDataSize = len;
352 mFdsKnown = false;
353 }
354 return err;
355}
356
Andreas Huber51faf462011-04-13 10:21:56 -0700357status_t Parcel::appendFrom(const Parcel *parcel, size_t offset, size_t len)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700358{
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700359 status_t err;
Andreas Huber51faf462011-04-13 10:21:56 -0700360 const uint8_t *data = parcel->mData;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800361 const binder_size_t *objects = parcel->mObjects;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700362 size_t size = parcel->mObjectsSize;
363 int startPos = mDataPos;
364 int firstIndex = -1, lastIndex = -2;
365
366 if (len == 0) {
367 return NO_ERROR;
368 }
369
Nick Kralevichb6b14232015-04-02 09:36:02 -0700370 if (len > INT32_MAX) {
371 // don't accept size_t values which may have come from an
372 // inadvertent conversion from a negative int.
373 return BAD_VALUE;
374 }
375
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700376 // range checks against the source parcel size
377 if ((offset > parcel->mDataSize)
378 || (len > parcel->mDataSize)
379 || (offset + len > parcel->mDataSize)) {
380 return BAD_VALUE;
381 }
382
383 // Count objects in range
384 for (int i = 0; i < (int) size; i++) {
385 size_t off = objects[i];
Christopher Tate27182be2015-05-27 17:53:02 -0700386 if ((off >= offset) && (off + sizeof(flat_binder_object) <= offset + len)) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700387 if (firstIndex == -1) {
388 firstIndex = i;
389 }
390 lastIndex = i;
391 }
392 }
393 int numObjects = lastIndex - firstIndex + 1;
394
Dianne Hackborn97e2bcd2011-04-13 18:15:56 -0700395 if ((mDataSize+len) > mDataCapacity) {
396 // grow data
397 err = growData(len);
398 if (err != NO_ERROR) {
399 return err;
400 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700401 }
402
403 // append data
404 memcpy(mData + mDataPos, data + offset, len);
405 mDataPos += len;
406 mDataSize += len;
407
Dianne Hackborn8938ed22011-09-28 23:19:47 -0400408 err = NO_ERROR;
409
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700410 if (numObjects > 0) {
Martijn Coenen69390d42018-10-22 15:18:10 +0200411 const sp<ProcessState> proc(ProcessState::self());
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700412 // grow objects
413 if (mObjectsCapacity < mObjectsSize + numObjects) {
Christopher Tateed7a50c2015-06-08 14:45:14 -0700414 size_t newSize = ((mObjectsSize + numObjects)*3)/2;
Christopher Tate44235112016-11-03 13:32:41 -0700415 if (newSize*sizeof(binder_size_t) < mObjectsSize) return NO_MEMORY; // overflow
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800416 binder_size_t *objects =
417 (binder_size_t*)realloc(mObjects, newSize*sizeof(binder_size_t));
Yi Kong91635562018-06-07 14:38:36 -0700418 if (objects == (binder_size_t*)nullptr) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700419 return NO_MEMORY;
420 }
421 mObjects = objects;
422 mObjectsCapacity = newSize;
423 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700424
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700425 // append and acquire objects
426 int idx = mObjectsSize;
427 for (int i = firstIndex; i <= lastIndex; i++) {
428 size_t off = objects[i] - offset + startPos;
429 mObjects[idx++] = off;
430 mObjectsSize++;
431
Dianne Hackborn8af0f822009-05-22 13:20:23 -0700432 flat_binder_object* flat
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700433 = reinterpret_cast<flat_binder_object*>(mData + off);
Adrian Rooscbf37262015-10-22 16:12:53 -0700434 acquire_object(proc, *flat, this, &mOpenAshmemSize);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700435
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -0700436 if (flat->hdr.type == BINDER_TYPE_FD) {
Dianne Hackborn8af0f822009-05-22 13:20:23 -0700437 // If this is a file descriptor, we need to dup it so the
438 // new Parcel now owns its own fd, and can declare that we
439 // officially know we have fds.
Nick Kralevichec9ec7d2016-12-17 19:47:27 -0800440 flat->handle = fcntl(flat->handle, F_DUPFD_CLOEXEC, 0);
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800441 flat->cookie = 1;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700442 mHasFds = mFdsKnown = true;
Dianne Hackborn8938ed22011-09-28 23:19:47 -0400443 if (!mAllowFds) {
444 err = FDS_NOT_ALLOWED;
445 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700446 }
447 }
448 }
449
Dianne Hackborn8938ed22011-09-28 23:19:47 -0400450 return err;
451}
452
Dianne Hackborn15feb9b2017-04-10 15:34:35 -0700453int Parcel::compareData(const Parcel& other) {
454 size_t size = dataSize();
455 if (size != other.dataSize()) {
456 return size < other.dataSize() ? -1 : 1;
457 }
458 return memcmp(data(), other.data(), size);
459}
460
Jeff Brown13b16042014-11-11 16:44:25 -0800461bool Parcel::allowFds() const
462{
463 return mAllowFds;
464}
465
Dianne Hackborn7746cc32011-10-03 21:09:35 -0700466bool Parcel::pushAllowFds(bool allowFds)
Dianne Hackborn8938ed22011-09-28 23:19:47 -0400467{
468 const bool origValue = mAllowFds;
Dianne Hackborn7746cc32011-10-03 21:09:35 -0700469 if (!allowFds) {
470 mAllowFds = false;
471 }
Dianne Hackborn8938ed22011-09-28 23:19:47 -0400472 return origValue;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700473}
474
Dianne Hackborn7746cc32011-10-03 21:09:35 -0700475void Parcel::restoreAllowFds(bool lastValue)
476{
477 mAllowFds = lastValue;
478}
479
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700480bool Parcel::hasFileDescriptors() const
481{
482 if (!mFdsKnown) {
483 scanForFds();
484 }
485 return mHasFds;
486}
487
Olivier Gaillarddc848a02019-01-30 17:10:44 +0000488void Parcel::updateWorkSourceRequestHeaderPosition() const {
489 // Only update the request headers once. We only want to point
490 // to the first headers read/written.
491 if (!mRequestHeaderPresent) {
492 mWorkSourceRequestHeaderPosition = dataPosition();
493 mRequestHeaderPresent = true;
494 }
495}
496
Steven Moreland0f452742019-07-31 15:50:51 +0000497#ifdef __ANDROID_VNDK__
498constexpr int32_t kHeader = B_PACK_CHARS('V', 'N', 'D', 'R');
499#else
500constexpr int32_t kHeader = B_PACK_CHARS('S', 'Y', 'S', 'T');
501#endif
502
Brad Fitzpatrick702ea9d2010-06-18 13:07:53 -0700503// Write RPC headers. (previously just the interface token)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700504status_t Parcel::writeInterfaceToken(const String16& interface)
505{
Olivier Gaillard91a04802018-11-14 17:32:41 +0000506 const IPCThreadState* threadState = IPCThreadState::self();
507 writeInt32(threadState->getStrictModePolicy() | STRICT_MODE_PENALTY_GATHER);
Olivier Gaillarddc848a02019-01-30 17:10:44 +0000508 updateWorkSourceRequestHeaderPosition();
Olivier Gaillard91a04802018-11-14 17:32:41 +0000509 writeInt32(threadState->shouldPropagateWorkSource() ?
510 threadState->getCallingWorkSourceUid() : IPCThreadState::kUnsetWorkSource);
Steven Moreland0f452742019-07-31 15:50:51 +0000511 writeInt32(kHeader);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700512 // currently the interface identification token is just its name as a string
513 return writeString16(interface);
514}
515
Olivier Gaillarddc848a02019-01-30 17:10:44 +0000516bool Parcel::replaceCallingWorkSourceUid(uid_t uid)
517{
518 if (!mRequestHeaderPresent) {
519 return false;
520 }
521
522 const size_t initialPosition = dataPosition();
523 setDataPosition(mWorkSourceRequestHeaderPosition);
524 status_t err = writeInt32(uid);
525 setDataPosition(initialPosition);
526 return err == NO_ERROR;
527}
528
529uid_t Parcel::readCallingWorkSourceUid()
530{
531 if (!mRequestHeaderPresent) {
532 return IPCThreadState::kUnsetWorkSource;
533 }
534
535 const size_t initialPosition = dataPosition();
536 setDataPosition(mWorkSourceRequestHeaderPosition);
537 uid_t uid = readInt32();
538 setDataPosition(initialPosition);
539 return uid;
540}
541
Mathias Agopian83c04462009-05-22 19:00:22 -0700542bool Parcel::checkInterface(IBinder* binder) const
543{
Brad Fitzpatrick702ea9d2010-06-18 13:07:53 -0700544 return enforceInterface(binder->getInterfaceDescriptor());
Mathias Agopian83c04462009-05-22 19:00:22 -0700545}
546
Brad Fitzpatricka877cd82010-07-07 16:06:39 -0700547bool Parcel::enforceInterface(const String16& interface,
Brad Fitzpatrick70081a12010-07-27 09:49:11 -0700548 IPCThreadState* threadState) const
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700549{
Olivier Gaillard0e0f1de2018-08-16 14:04:09 +0100550 // StrictModePolicy.
Brad Fitzpatrick70081a12010-07-27 09:49:11 -0700551 int32_t strictPolicy = readInt32();
Yi Kong91635562018-06-07 14:38:36 -0700552 if (threadState == nullptr) {
Brad Fitzpatrick70081a12010-07-27 09:49:11 -0700553 threadState = IPCThreadState::self();
Brad Fitzpatricka877cd82010-07-07 16:06:39 -0700554 }
Brad Fitzpatrick52736032010-08-30 16:01:16 -0700555 if ((threadState->getLastTransactionBinderFlags() &
556 IBinder::FLAG_ONEWAY) != 0) {
557 // For one-way calls, the callee is running entirely
558 // disconnected from the caller, so disable StrictMode entirely.
559 // Not only does disk/network usage not impact the caller, but
560 // there's no way to commuicate back any violations anyway.
561 threadState->setStrictModePolicy(0);
562 } else {
563 threadState->setStrictModePolicy(strictPolicy);
564 }
Olivier Gaillard0e0f1de2018-08-16 14:04:09 +0100565 // WorkSource.
Olivier Gaillarddc848a02019-01-30 17:10:44 +0000566 updateWorkSourceRequestHeaderPosition();
Olivier Gaillard0e0f1de2018-08-16 14:04:09 +0100567 int32_t workSource = readInt32();
Olivier Gaillard91a04802018-11-14 17:32:41 +0000568 threadState->setCallingWorkSourceUidWithoutPropagation(workSource);
Steven Moreland0f452742019-07-31 15:50:51 +0000569 // vendor header
570 int32_t header = readInt32();
571 if (header != kHeader) {
572 ALOGE("Expecting header 0x%x but found 0x%x. Mixing copies of libbinder?", kHeader, header);
573 return false;
574 }
Olivier Gaillard0e0f1de2018-08-16 14:04:09 +0100575 // Interface descriptor.
Mathias Agopian83c04462009-05-22 19:00:22 -0700576 const String16 str(readString16());
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700577 if (str == interface) {
578 return true;
579 } else {
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700580 ALOGW("**** enforceInterface() expected '%s' but read '%s'",
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700581 String8(interface).string(), String8(str).string());
582 return false;
583 }
Brad Fitzpatrick702ea9d2010-06-18 13:07:53 -0700584}
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700585
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700586size_t Parcel::objectsCount() const
587{
588 return mObjectsSize;
589}
590
591status_t Parcel::errorCheck() const
592{
593 return mError;
594}
595
596void Parcel::setError(status_t err)
597{
598 mError = err;
599}
600
601status_t Parcel::finishWrite(size_t len)
602{
Nick Kralevichb6b14232015-04-02 09:36:02 -0700603 if (len > INT32_MAX) {
604 // don't accept size_t values which may have come from an
605 // inadvertent conversion from a negative int.
606 return BAD_VALUE;
607 }
608
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700609 //printf("Finish write of %d\n", len);
610 mDataPos += len;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700611 ALOGV("finishWrite Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700612 if (mDataPos > mDataSize) {
613 mDataSize = mDataPos;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700614 ALOGV("finishWrite Setting data size of %p to %zu", this, mDataSize);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700615 }
616 //printf("New pos=%d, size=%d\n", mDataPos, mDataSize);
617 return NO_ERROR;
618}
619
620status_t Parcel::writeUnpadded(const void* data, size_t len)
621{
Nick Kralevichb6b14232015-04-02 09:36:02 -0700622 if (len > INT32_MAX) {
623 // don't accept size_t values which may have come from an
624 // inadvertent conversion from a negative int.
625 return BAD_VALUE;
626 }
627
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700628 size_t end = mDataPos + len;
629 if (end < mDataPos) {
630 // integer overflow
631 return BAD_VALUE;
632 }
633
634 if (end <= mDataCapacity) {
635restart_write:
636 memcpy(mData+mDataPos, data, len);
637 return finishWrite(len);
638 }
639
640 status_t err = growData(len);
641 if (err == NO_ERROR) goto restart_write;
642 return err;
643}
644
645status_t Parcel::write(const void* data, size_t len)
646{
Nick Kralevichb6b14232015-04-02 09:36:02 -0700647 if (len > INT32_MAX) {
648 // don't accept size_t values which may have come from an
649 // inadvertent conversion from a negative int.
650 return BAD_VALUE;
651 }
652
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700653 void* const d = writeInplace(len);
654 if (d) {
655 memcpy(d, data, len);
656 return NO_ERROR;
657 }
658 return mError;
659}
660
661void* Parcel::writeInplace(size_t len)
662{
Nick Kralevichb6b14232015-04-02 09:36:02 -0700663 if (len > INT32_MAX) {
664 // don't accept size_t values which may have come from an
665 // inadvertent conversion from a negative int.
Yi Kong91635562018-06-07 14:38:36 -0700666 return nullptr;
Nick Kralevichb6b14232015-04-02 09:36:02 -0700667 }
668
669 const size_t padded = pad_size(len);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700670
671 // sanity check for integer overflow
672 if (mDataPos+padded < mDataPos) {
Yi Kong91635562018-06-07 14:38:36 -0700673 return nullptr;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700674 }
675
676 if ((mDataPos+padded) <= mDataCapacity) {
677restart_write:
678 //printf("Writing %ld bytes, padded to %ld\n", len, padded);
679 uint8_t* const data = mData+mDataPos;
680
681 // Need to pad at end?
682 if (padded != len) {
683#if BYTE_ORDER == BIG_ENDIAN
684 static const uint32_t mask[4] = {
685 0x00000000, 0xffffff00, 0xffff0000, 0xff000000
686 };
687#endif
688#if BYTE_ORDER == LITTLE_ENDIAN
689 static const uint32_t mask[4] = {
690 0x00000000, 0x00ffffff, 0x0000ffff, 0x000000ff
691 };
692#endif
693 //printf("Applying pad mask: %p to %p\n", (void*)mask[padded-len],
694 // *reinterpret_cast<void**>(data+padded-4));
695 *reinterpret_cast<uint32_t*>(data+padded-4) &= mask[padded-len];
696 }
697
698 finishWrite(padded);
699 return data;
700 }
701
702 status_t err = growData(padded);
703 if (err == NO_ERROR) goto restart_write;
Yi Kong91635562018-06-07 14:38:36 -0700704 return nullptr;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700705}
706
Christopher Wiley9a5e32f2016-01-28 16:56:53 -0800707status_t Parcel::writeUtf8AsUtf16(const std::string& str) {
708 const uint8_t* strData = (uint8_t*)str.data();
709 const size_t strLen= str.length();
710 const ssize_t utf16Len = utf8_to_utf16_length(strData, strLen);
Sergio Girof4607432016-07-21 14:46:35 +0100711 if (utf16Len < 0 || utf16Len > std::numeric_limits<int32_t>::max()) {
Christopher Wiley9a5e32f2016-01-28 16:56:53 -0800712 return BAD_VALUE;
713 }
714
715 status_t err = writeInt32(utf16Len);
716 if (err) {
717 return err;
718 }
719
720 // Allocate enough bytes to hold our converted string and its terminating NULL.
721 void* dst = writeInplace((utf16Len + 1) * sizeof(char16_t));
722 if (!dst) {
723 return NO_MEMORY;
724 }
725
Sergio Girof4607432016-07-21 14:46:35 +0100726 utf8_to_utf16(strData, strLen, (char16_t*)dst, (size_t) utf16Len + 1);
Christopher Wiley9a5e32f2016-01-28 16:56:53 -0800727
728 return NO_ERROR;
729}
730
731status_t Parcel::writeUtf8AsUtf16(const std::unique_ptr<std::string>& str) {
732 if (!str) {
733 return writeInt32(-1);
734 }
735 return writeUtf8AsUtf16(*str);
736}
737
Casey Dahlin185d3442016-02-09 11:08:35 -0800738namespace {
Casey Dahlinb9872622015-11-25 15:09:45 -0800739
Casey Dahlin185d3442016-02-09 11:08:35 -0800740template<typename T>
741status_t writeByteVectorInternal(Parcel* parcel, const std::vector<T>& val)
Casey Dahlin451ff582015-10-19 18:12:18 -0700742{
Christopher Wileyf0fc52b2015-10-31 13:22:15 -0700743 status_t status;
Casey Dahlin451ff582015-10-19 18:12:18 -0700744 if (val.size() > std::numeric_limits<int32_t>::max()) {
Christopher Wileyf0fc52b2015-10-31 13:22:15 -0700745 status = BAD_VALUE;
746 return status;
Casey Dahlin451ff582015-10-19 18:12:18 -0700747 }
748
Casey Dahlin185d3442016-02-09 11:08:35 -0800749 status = parcel->writeInt32(val.size());
Casey Dahlin451ff582015-10-19 18:12:18 -0700750 if (status != OK) {
751 return status;
752 }
753
Casey Dahlin185d3442016-02-09 11:08:35 -0800754 void* data = parcel->writeInplace(val.size());
Christopher Wileyf0fc52b2015-10-31 13:22:15 -0700755 if (!data) {
756 status = BAD_VALUE;
757 return status;
Casey Dahlin451ff582015-10-19 18:12:18 -0700758 }
759
Christopher Wileyf0fc52b2015-10-31 13:22:15 -0700760 memcpy(data, val.data(), val.size());
761 return status;
Casey Dahlin451ff582015-10-19 18:12:18 -0700762}
763
Casey Dahlin185d3442016-02-09 11:08:35 -0800764template<typename T>
765status_t writeByteVectorInternalPtr(Parcel* parcel,
766 const std::unique_ptr<std::vector<T>>& val)
767{
768 if (!val) {
769 return parcel->writeInt32(-1);
770 }
771
772 return writeByteVectorInternal(parcel, *val);
773}
774
775} // namespace
776
777status_t Parcel::writeByteVector(const std::vector<int8_t>& val) {
778 return writeByteVectorInternal(this, val);
779}
780
781status_t Parcel::writeByteVector(const std::unique_ptr<std::vector<int8_t>>& val)
782{
783 return writeByteVectorInternalPtr(this, val);
784}
785
786status_t Parcel::writeByteVector(const std::vector<uint8_t>& val) {
787 return writeByteVectorInternal(this, val);
788}
789
790status_t Parcel::writeByteVector(const std::unique_ptr<std::vector<uint8_t>>& val)
791{
792 return writeByteVectorInternalPtr(this, val);
793}
794
Casey Dahlin451ff582015-10-19 18:12:18 -0700795status_t Parcel::writeInt32Vector(const std::vector<int32_t>& val)
796{
Christopher Wiley03d1eb62015-11-19 06:42:40 -0800797 return writeTypedVector(val, &Parcel::writeInt32);
Casey Dahlin451ff582015-10-19 18:12:18 -0700798}
799
Casey Dahlinb9872622015-11-25 15:09:45 -0800800status_t Parcel::writeInt32Vector(const std::unique_ptr<std::vector<int32_t>>& val)
801{
802 return writeNullableTypedVector(val, &Parcel::writeInt32);
803}
804
Casey Dahlin451ff582015-10-19 18:12:18 -0700805status_t Parcel::writeInt64Vector(const std::vector<int64_t>& val)
806{
Christopher Wiley03d1eb62015-11-19 06:42:40 -0800807 return writeTypedVector(val, &Parcel::writeInt64);
Casey Dahlin451ff582015-10-19 18:12:18 -0700808}
809
Casey Dahlinb9872622015-11-25 15:09:45 -0800810status_t Parcel::writeInt64Vector(const std::unique_ptr<std::vector<int64_t>>& val)
811{
812 return writeNullableTypedVector(val, &Parcel::writeInt64);
813}
814
Kevin DuBois2f82d5b2018-12-05 12:56:10 -0800815status_t Parcel::writeUint64Vector(const std::vector<uint64_t>& val)
816{
817 return writeTypedVector(val, &Parcel::writeUint64);
818}
819
820status_t Parcel::writeUint64Vector(const std::unique_ptr<std::vector<uint64_t>>& val)
821{
822 return writeNullableTypedVector(val, &Parcel::writeUint64);
823}
824
Casey Dahlin451ff582015-10-19 18:12:18 -0700825status_t Parcel::writeFloatVector(const std::vector<float>& val)
826{
Christopher Wiley03d1eb62015-11-19 06:42:40 -0800827 return writeTypedVector(val, &Parcel::writeFloat);
Casey Dahlin451ff582015-10-19 18:12:18 -0700828}
829
Casey Dahlinb9872622015-11-25 15:09:45 -0800830status_t Parcel::writeFloatVector(const std::unique_ptr<std::vector<float>>& val)
831{
832 return writeNullableTypedVector(val, &Parcel::writeFloat);
833}
834
Casey Dahlin451ff582015-10-19 18:12:18 -0700835status_t Parcel::writeDoubleVector(const std::vector<double>& val)
836{
Christopher Wiley03d1eb62015-11-19 06:42:40 -0800837 return writeTypedVector(val, &Parcel::writeDouble);
Casey Dahlin451ff582015-10-19 18:12:18 -0700838}
839
Casey Dahlinb9872622015-11-25 15:09:45 -0800840status_t Parcel::writeDoubleVector(const std::unique_ptr<std::vector<double>>& val)
841{
842 return writeNullableTypedVector(val, &Parcel::writeDouble);
843}
844
Casey Dahlin451ff582015-10-19 18:12:18 -0700845status_t Parcel::writeBoolVector(const std::vector<bool>& val)
846{
Christopher Wiley03d1eb62015-11-19 06:42:40 -0800847 return writeTypedVector(val, &Parcel::writeBool);
Casey Dahlin451ff582015-10-19 18:12:18 -0700848}
849
Casey Dahlinb9872622015-11-25 15:09:45 -0800850status_t Parcel::writeBoolVector(const std::unique_ptr<std::vector<bool>>& val)
851{
852 return writeNullableTypedVector(val, &Parcel::writeBool);
853}
854
Casey Dahlin451ff582015-10-19 18:12:18 -0700855status_t Parcel::writeCharVector(const std::vector<char16_t>& val)
856{
Christopher Wiley03d1eb62015-11-19 06:42:40 -0800857 return writeTypedVector(val, &Parcel::writeChar);
Casey Dahlin451ff582015-10-19 18:12:18 -0700858}
859
Casey Dahlinb9872622015-11-25 15:09:45 -0800860status_t Parcel::writeCharVector(const std::unique_ptr<std::vector<char16_t>>& val)
861{
862 return writeNullableTypedVector(val, &Parcel::writeChar);
863}
864
Casey Dahlin451ff582015-10-19 18:12:18 -0700865status_t Parcel::writeString16Vector(const std::vector<String16>& val)
866{
Christopher Wiley03d1eb62015-11-19 06:42:40 -0800867 return writeTypedVector(val, &Parcel::writeString16);
Casey Dahlin451ff582015-10-19 18:12:18 -0700868}
869
Casey Dahlinb9872622015-11-25 15:09:45 -0800870status_t Parcel::writeString16Vector(
871 const std::unique_ptr<std::vector<std::unique_ptr<String16>>>& val)
872{
873 return writeNullableTypedVector(val, &Parcel::writeString16);
874}
875
Christopher Wiley9a5e32f2016-01-28 16:56:53 -0800876status_t Parcel::writeUtf8VectorAsUtf16Vector(
877 const std::unique_ptr<std::vector<std::unique_ptr<std::string>>>& val) {
878 return writeNullableTypedVector(val, &Parcel::writeUtf8AsUtf16);
879}
880
881status_t Parcel::writeUtf8VectorAsUtf16Vector(const std::vector<std::string>& val) {
882 return writeTypedVector(val, &Parcel::writeUtf8AsUtf16);
883}
884
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700885status_t Parcel::writeInt32(int32_t val)
886{
Andreas Huber84a6d042009-08-17 13:33:27 -0700887 return writeAligned(val);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700888}
Dan Stoza41a0f2f2014-12-01 10:01:10 -0800889
890status_t Parcel::writeUint32(uint32_t val)
891{
892 return writeAligned(val);
893}
894
Marco Nelissen5c0106e2013-10-16 10:57:51 -0700895status_t Parcel::writeInt32Array(size_t len, const int32_t *val) {
Nick Kralevichb6b14232015-04-02 09:36:02 -0700896 if (len > INT32_MAX) {
897 // don't accept size_t values which may have come from an
898 // inadvertent conversion from a negative int.
899 return BAD_VALUE;
900 }
901
Marco Nelissen5c0106e2013-10-16 10:57:51 -0700902 if (!val) {
Chad Brubakere59cb432015-06-30 14:03:55 -0700903 return writeInt32(-1);
Marco Nelissen5c0106e2013-10-16 10:57:51 -0700904 }
Chad Brubakere59cb432015-06-30 14:03:55 -0700905 status_t ret = writeInt32(static_cast<uint32_t>(len));
Marco Nelissen5c0106e2013-10-16 10:57:51 -0700906 if (ret == NO_ERROR) {
907 ret = write(val, len * sizeof(*val));
908 }
909 return ret;
910}
Marco Nelissenf0190bf2014-03-13 14:17:40 -0700911status_t Parcel::writeByteArray(size_t len, const uint8_t *val) {
Nick Kralevichb6b14232015-04-02 09:36:02 -0700912 if (len > INT32_MAX) {
913 // don't accept size_t values which may have come from an
914 // inadvertent conversion from a negative int.
915 return BAD_VALUE;
916 }
917
Marco Nelissenf0190bf2014-03-13 14:17:40 -0700918 if (!val) {
Chad Brubakere59cb432015-06-30 14:03:55 -0700919 return writeInt32(-1);
Marco Nelissenf0190bf2014-03-13 14:17:40 -0700920 }
Chad Brubakere59cb432015-06-30 14:03:55 -0700921 status_t ret = writeInt32(static_cast<uint32_t>(len));
Marco Nelissenf0190bf2014-03-13 14:17:40 -0700922 if (ret == NO_ERROR) {
923 ret = write(val, len * sizeof(*val));
924 }
925 return ret;
926}
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700927
Casey Dahlind6848f52015-10-15 15:44:59 -0700928status_t Parcel::writeBool(bool val)
929{
930 return writeInt32(int32_t(val));
931}
932
933status_t Parcel::writeChar(char16_t val)
934{
935 return writeInt32(int32_t(val));
936}
937
938status_t Parcel::writeByte(int8_t val)
939{
940 return writeInt32(int32_t(val));
941}
942
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700943status_t Parcel::writeInt64(int64_t val)
944{
Andreas Huber84a6d042009-08-17 13:33:27 -0700945 return writeAligned(val);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700946}
947
Ronghua Wu2d13afd2015-03-16 11:11:07 -0700948status_t Parcel::writeUint64(uint64_t val)
949{
950 return writeAligned(val);
951}
952
Serban Constantinescuf683e012013-11-05 16:53:55 +0000953status_t Parcel::writePointer(uintptr_t val)
954{
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800955 return writeAligned<binder_uintptr_t>(val);
Serban Constantinescuf683e012013-11-05 16:53:55 +0000956}
957
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700958status_t Parcel::writeFloat(float val)
959{
Andreas Huber84a6d042009-08-17 13:33:27 -0700960 return writeAligned(val);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700961}
962
Douglas Leungcc1a4bb2013-01-11 15:00:55 -0800963#if defined(__mips__) && defined(__mips_hard_float)
964
965status_t Parcel::writeDouble(double val)
966{
967 union {
968 double d;
969 unsigned long long ll;
970 } u;
971 u.d = val;
972 return writeAligned(u.ll);
973}
974
975#else
976
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700977status_t Parcel::writeDouble(double val)
978{
Andreas Huber84a6d042009-08-17 13:33:27 -0700979 return writeAligned(val);
980}
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700981
Douglas Leungcc1a4bb2013-01-11 15:00:55 -0800982#endif
983
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700984status_t Parcel::writeCString(const char* str)
985{
986 return write(str, strlen(str)+1);
987}
988
989status_t Parcel::writeString8(const String8& str)
990{
991 status_t err = writeInt32(str.bytes());
Pravat Dalbeherad1dff8d2010-12-15 08:40:00 +0100992 // only write string if its length is more than zero characters,
993 // as readString8 will only read if the length field is non-zero.
994 // this is slightly different from how writeString16 works.
995 if (str.bytes() > 0 && err == NO_ERROR) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700996 err = write(str.string(), str.bytes()+1);
997 }
998 return err;
999}
1000
Casey Dahlinb9872622015-11-25 15:09:45 -08001001status_t Parcel::writeString16(const std::unique_ptr<String16>& str)
1002{
1003 if (!str) {
1004 return writeInt32(-1);
1005 }
1006
1007 return writeString16(*str);
1008}
1009
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001010status_t Parcel::writeString16(const String16& str)
1011{
1012 return writeString16(str.string(), str.size());
1013}
1014
1015status_t Parcel::writeString16(const char16_t* str, size_t len)
1016{
Yi Kong91635562018-06-07 14:38:36 -07001017 if (str == nullptr) return writeInt32(-1);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001018
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001019 status_t err = writeInt32(len);
1020 if (err == NO_ERROR) {
1021 len *= sizeof(char16_t);
1022 uint8_t* data = (uint8_t*)writeInplace(len+sizeof(char16_t));
1023 if (data) {
1024 memcpy(data, str, len);
1025 *reinterpret_cast<char16_t*>(data+len) = 0;
1026 return NO_ERROR;
1027 }
1028 err = mError;
1029 }
1030 return err;
1031}
1032
1033status_t Parcel::writeStrongBinder(const sp<IBinder>& val)
1034{
1035 return flatten_binder(ProcessState::self(), val, this);
1036}
1037
Casey Dahlineb8e15f2015-11-03 13:50:37 -08001038status_t Parcel::writeStrongBinderVector(const std::vector<sp<IBinder>>& val)
1039{
Christopher Wiley03d1eb62015-11-19 06:42:40 -08001040 return writeTypedVector(val, &Parcel::writeStrongBinder);
Casey Dahlineb8e15f2015-11-03 13:50:37 -08001041}
1042
Casey Dahlinb9872622015-11-25 15:09:45 -08001043status_t Parcel::writeStrongBinderVector(const std::unique_ptr<std::vector<sp<IBinder>>>& val)
1044{
1045 return writeNullableTypedVector(val, &Parcel::writeStrongBinder);
1046}
1047
1048status_t Parcel::readStrongBinderVector(std::unique_ptr<std::vector<sp<IBinder>>>* val) const {
Christopher Wiley35d77ca2016-03-08 10:49:51 -08001049 return readNullableTypedVector(val, &Parcel::readNullableStrongBinder);
Casey Dahlinb9872622015-11-25 15:09:45 -08001050}
1051
Casey Dahlineb8e15f2015-11-03 13:50:37 -08001052status_t Parcel::readStrongBinderVector(std::vector<sp<IBinder>>* val) const {
Christopher Wiley03d1eb62015-11-19 06:42:40 -08001053 return readTypedVector(val, &Parcel::readStrongBinder);
Casey Dahlineb8e15f2015-11-03 13:50:37 -08001054}
1055
Casey Dahlinb9872622015-11-25 15:09:45 -08001056status_t Parcel::writeRawNullableParcelable(const Parcelable* parcelable) {
1057 if (!parcelable) {
1058 return writeInt32(0);
1059 }
1060
1061 return writeParcelable(*parcelable);
1062}
1063
Christopher Wiley97f048d2015-11-19 06:49:05 -08001064status_t Parcel::writeParcelable(const Parcelable& parcelable) {
1065 status_t status = writeInt32(1); // parcelable is not null.
1066 if (status != OK) {
1067 return status;
1068 }
1069 return parcelable.writeToParcel(this);
1070}
1071
Mathias Agopiana47f02a2009-05-21 16:29:38 -07001072status_t Parcel::writeNativeHandle(const native_handle* handle)
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001073{
Mathias Agopian1d0a95b2009-07-31 16:12:13 -07001074 if (!handle || handle->version != sizeof(native_handle))
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001075 return BAD_TYPE;
1076
1077 status_t err;
Mathias Agopiana47f02a2009-05-21 16:29:38 -07001078 err = writeInt32(handle->numFds);
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001079 if (err != NO_ERROR) return err;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001080
Mathias Agopiana47f02a2009-05-21 16:29:38 -07001081 err = writeInt32(handle->numInts);
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001082 if (err != NO_ERROR) return err;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001083
Mathias Agopiana47f02a2009-05-21 16:29:38 -07001084 for (int i=0 ; err==NO_ERROR && i<handle->numFds ; i++)
1085 err = writeDupFileDescriptor(handle->data[i]);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001086
1087 if (err != NO_ERROR) {
Steve Block9d453682011-12-20 16:23:08 +00001088 ALOGD("write native handle, write dup fd failed");
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001089 return err;
1090 }
Mathias Agopiana47f02a2009-05-21 16:29:38 -07001091 err = write(handle->data + handle->numFds, sizeof(int)*handle->numInts);
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001092 return err;
1093}
1094
Jeff Brown93ff1f92011-11-04 19:01:44 -07001095status_t Parcel::writeFileDescriptor(int fd, bool takeOwnership)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001096{
1097 flat_binder_object obj;
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -07001098 obj.hdr.type = BINDER_TYPE_FD;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001099 obj.flags = 0x7f | FLAT_BINDER_FLAG_ACCEPTS_FDS;
Arve Hjønnevåg07fd0f12014-02-18 21:10:29 -08001100 obj.binder = 0; /* Don't pass uninitialized stack data to a remote process */
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001101 obj.handle = fd;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001102 obj.cookie = takeOwnership ? 1 : 0;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001103 return writeObject(obj, true);
1104}
1105
1106status_t Parcel::writeDupFileDescriptor(int fd)
1107{
Nick Kralevichec9ec7d2016-12-17 19:47:27 -08001108 int dupFd = fcntl(fd, F_DUPFD_CLOEXEC, 0);
Jeff Brownd341c712011-11-04 20:19:33 -07001109 if (dupFd < 0) {
1110 return -errno;
1111 }
1112 status_t err = writeFileDescriptor(dupFd, true /*takeOwnership*/);
Casey Dahlin06673e32015-11-23 13:24:23 -08001113 if (err != OK) {
Jeff Brownd341c712011-11-04 20:19:33 -07001114 close(dupFd);
1115 }
1116 return err;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001117}
1118
Dianne Hackborn1941a402016-08-29 12:30:43 -07001119status_t Parcel::writeParcelFileDescriptor(int fd, bool takeOwnership)
1120{
1121 writeInt32(0);
1122 return writeFileDescriptor(fd, takeOwnership);
1123}
1124
Ryo Hashimotobf551892018-05-31 16:58:35 +09001125status_t Parcel::writeDupParcelFileDescriptor(int fd)
1126{
1127 int dupFd = fcntl(fd, F_DUPFD_CLOEXEC, 0);
1128 if (dupFd < 0) {
1129 return -errno;
1130 }
1131 status_t err = writeParcelFileDescriptor(dupFd, true /*takeOwnership*/);
1132 if (err != OK) {
1133 close(dupFd);
1134 }
1135 return err;
1136}
1137
Christopher Wiley2cf19952016-04-11 11:09:37 -07001138status_t Parcel::writeUniqueFileDescriptor(const base::unique_fd& fd) {
Casey Dahlin06673e32015-11-23 13:24:23 -08001139 return writeDupFileDescriptor(fd.get());
1140}
1141
Christopher Wiley2cf19952016-04-11 11:09:37 -07001142status_t Parcel::writeUniqueFileDescriptorVector(const std::vector<base::unique_fd>& val) {
Casey Dahlin06673e32015-11-23 13:24:23 -08001143 return writeTypedVector(val, &Parcel::writeUniqueFileDescriptor);
1144}
1145
Christopher Wiley2cf19952016-04-11 11:09:37 -07001146status_t Parcel::writeUniqueFileDescriptorVector(const std::unique_ptr<std::vector<base::unique_fd>>& val) {
Casey Dahlinb9872622015-11-25 15:09:45 -08001147 return writeNullableTypedVector(val, &Parcel::writeUniqueFileDescriptor);
1148}
1149
Jeff Brown13b16042014-11-11 16:44:25 -08001150status_t Parcel::writeBlob(size_t len, bool mutableCopy, WritableBlob* outBlob)
Jeff Brown5707dbf2011-09-23 21:17:56 -07001151{
Nick Kralevichb6b14232015-04-02 09:36:02 -07001152 if (len > INT32_MAX) {
1153 // don't accept size_t values which may have come from an
1154 // inadvertent conversion from a negative int.
1155 return BAD_VALUE;
1156 }
1157
Jeff Brown13b16042014-11-11 16:44:25 -08001158 status_t status;
1159 if (!mAllowFds || len <= BLOB_INPLACE_LIMIT) {
Steve Block6807e592011-10-20 11:56:00 +01001160 ALOGV("writeBlob: write in place");
Jeff Brown13b16042014-11-11 16:44:25 -08001161 status = writeInt32(BLOB_INPLACE);
Jeff Brown5707dbf2011-09-23 21:17:56 -07001162 if (status) return status;
1163
1164 void* ptr = writeInplace(len);
1165 if (!ptr) return NO_MEMORY;
1166
Jeff Brown13b16042014-11-11 16:44:25 -08001167 outBlob->init(-1, ptr, len, false);
Jeff Brown5707dbf2011-09-23 21:17:56 -07001168 return NO_ERROR;
1169 }
1170
Steve Block6807e592011-10-20 11:56:00 +01001171 ALOGV("writeBlob: write to ashmem");
Jeff Brown5707dbf2011-09-23 21:17:56 -07001172 int fd = ashmem_create_region("Parcel Blob", len);
1173 if (fd < 0) return NO_MEMORY;
1174
1175 int result = ashmem_set_prot_region(fd, PROT_READ | PROT_WRITE);
1176 if (result < 0) {
Jeff Brownec4e0062011-10-10 14:50:10 -07001177 status = result;
Jeff Brown5707dbf2011-09-23 21:17:56 -07001178 } else {
Yi Kong91635562018-06-07 14:38:36 -07001179 void* ptr = ::mmap(nullptr, len, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
Jeff Brown5707dbf2011-09-23 21:17:56 -07001180 if (ptr == MAP_FAILED) {
1181 status = -errno;
1182 } else {
Jeff Brown13b16042014-11-11 16:44:25 -08001183 if (!mutableCopy) {
1184 result = ashmem_set_prot_region(fd, PROT_READ);
1185 }
Jeff Brown5707dbf2011-09-23 21:17:56 -07001186 if (result < 0) {
Jeff Brownec4e0062011-10-10 14:50:10 -07001187 status = result;
Jeff Brown5707dbf2011-09-23 21:17:56 -07001188 } else {
Jeff Brown13b16042014-11-11 16:44:25 -08001189 status = writeInt32(mutableCopy ? BLOB_ASHMEM_MUTABLE : BLOB_ASHMEM_IMMUTABLE);
Jeff Brown5707dbf2011-09-23 21:17:56 -07001190 if (!status) {
Jeff Brown93ff1f92011-11-04 19:01:44 -07001191 status = writeFileDescriptor(fd, true /*takeOwnership*/);
Jeff Brown5707dbf2011-09-23 21:17:56 -07001192 if (!status) {
Jeff Brown13b16042014-11-11 16:44:25 -08001193 outBlob->init(fd, ptr, len, mutableCopy);
Jeff Brown5707dbf2011-09-23 21:17:56 -07001194 return NO_ERROR;
1195 }
1196 }
1197 }
1198 }
1199 ::munmap(ptr, len);
1200 }
1201 ::close(fd);
1202 return status;
1203}
1204
Jeff Brown13b16042014-11-11 16:44:25 -08001205status_t Parcel::writeDupImmutableBlobFileDescriptor(int fd)
1206{
1207 // Must match up with what's done in writeBlob.
1208 if (!mAllowFds) return FDS_NOT_ALLOWED;
1209 status_t status = writeInt32(BLOB_ASHMEM_IMMUTABLE);
1210 if (status) return status;
1211 return writeDupFileDescriptor(fd);
1212}
1213
Mathias Agopiane1424282013-07-29 21:24:40 -07001214status_t Parcel::write(const FlattenableHelperInterface& val)
Mathias Agopian98e71dd2010-02-11 17:30:52 -08001215{
1216 status_t err;
1217
1218 // size if needed
Mathias Agopiane1424282013-07-29 21:24:40 -07001219 const size_t len = val.getFlattenedSize();
1220 const size_t fd_count = val.getFdCount();
Mathias Agopian98e71dd2010-02-11 17:30:52 -08001221
Christopher Tatee4e0ae82016-03-24 16:03:44 -07001222 if ((len > INT32_MAX) || (fd_count >= gMaxFds)) {
Nick Kralevichb6b14232015-04-02 09:36:02 -07001223 // don't accept size_t values which may have come from an
1224 // inadvertent conversion from a negative int.
1225 return BAD_VALUE;
1226 }
1227
Mathias Agopian98e71dd2010-02-11 17:30:52 -08001228 err = this->writeInt32(len);
1229 if (err) return err;
1230
1231 err = this->writeInt32(fd_count);
1232 if (err) return err;
1233
1234 // payload
Martijn Coenenf8542382018-04-04 11:46:56 +02001235 void* const buf = this->writeInplace(len);
Yi Kong91635562018-06-07 14:38:36 -07001236 if (buf == nullptr)
Mathias Agopian98e71dd2010-02-11 17:30:52 -08001237 return BAD_VALUE;
1238
Yi Kong91635562018-06-07 14:38:36 -07001239 int* fds = nullptr;
Mathias Agopian98e71dd2010-02-11 17:30:52 -08001240 if (fd_count) {
Christopher Tatee4e0ae82016-03-24 16:03:44 -07001241 fds = new (std::nothrow) int[fd_count];
1242 if (fds == nullptr) {
1243 ALOGE("write: failed to allocate requested %zu fds", fd_count);
1244 return BAD_VALUE;
1245 }
Mathias Agopian98e71dd2010-02-11 17:30:52 -08001246 }
1247
1248 err = val.flatten(buf, len, fds, fd_count);
1249 for (size_t i=0 ; i<fd_count && err==NO_ERROR ; i++) {
1250 err = this->writeDupFileDescriptor( fds[i] );
1251 }
1252
1253 if (fd_count) {
1254 delete [] fds;
1255 }
1256
1257 return err;
1258}
1259
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001260status_t Parcel::writeObject(const flat_binder_object& val, bool nullMetaData)
1261{
1262 const bool enoughData = (mDataPos+sizeof(val)) <= mDataCapacity;
1263 const bool enoughObjects = mObjectsSize < mObjectsCapacity;
1264 if (enoughData && enoughObjects) {
1265restart_write:
1266 *reinterpret_cast<flat_binder_object*>(mData+mDataPos) = val;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001267
Christopher Tate98e67d32015-06-03 18:44:15 -07001268 // remember if it's a file descriptor
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -07001269 if (val.hdr.type == BINDER_TYPE_FD) {
Christopher Tate98e67d32015-06-03 18:44:15 -07001270 if (!mAllowFds) {
1271 // fail before modifying our object index
1272 return FDS_NOT_ALLOWED;
1273 }
1274 mHasFds = mFdsKnown = true;
1275 }
1276
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001277 // Need to write meta-data?
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001278 if (nullMetaData || val.binder != 0) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001279 mObjects[mObjectsSize] = mDataPos;
Adrian Rooscbf37262015-10-22 16:12:53 -07001280 acquire_object(ProcessState::self(), val, this, &mOpenAshmemSize);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001281 mObjectsSize++;
1282 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001283
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001284 return finishWrite(sizeof(flat_binder_object));
1285 }
1286
1287 if (!enoughData) {
1288 const status_t err = growData(sizeof(val));
1289 if (err != NO_ERROR) return err;
1290 }
1291 if (!enoughObjects) {
1292 size_t newSize = ((mObjectsSize+2)*3)/2;
Christopher Tate44235112016-11-03 13:32:41 -07001293 if (newSize*sizeof(binder_size_t) < mObjectsSize) return NO_MEMORY; // overflow
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001294 binder_size_t* objects = (binder_size_t*)realloc(mObjects, newSize*sizeof(binder_size_t));
Yi Kong91635562018-06-07 14:38:36 -07001295 if (objects == nullptr) return NO_MEMORY;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001296 mObjects = objects;
1297 mObjectsCapacity = newSize;
1298 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001299
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001300 goto restart_write;
1301}
1302
Brad Fitzpatrick837a0d02010-07-13 15:33:35 -07001303status_t Parcel::writeNoException()
1304{
Christopher Wiley09eb7492015-11-09 15:06:15 -08001305 binder::Status status;
1306 return status.writeToParcel(this);
Brad Fitzpatrick837a0d02010-07-13 15:33:35 -07001307}
1308
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08001309status_t Parcel::validateReadData(size_t upperBound) const
1310{
1311 // Don't allow non-object reads on object data
1312 if (mObjectsSorted || mObjectsSize <= 1) {
1313data_sorted:
1314 // Expect to check only against the next object
1315 if (mNextObjectHint < mObjectsSize && upperBound > mObjects[mNextObjectHint]) {
1316 // For some reason the current read position is greater than the next object
1317 // hint. Iterate until we find the right object
1318 size_t nextObject = mNextObjectHint;
1319 do {
1320 if (mDataPos < mObjects[nextObject] + sizeof(flat_binder_object)) {
1321 // Requested info overlaps with an object
1322 ALOGE("Attempt to read from protected data in Parcel %p", this);
1323 return PERMISSION_DENIED;
1324 }
1325 nextObject++;
1326 } while (nextObject < mObjectsSize && upperBound > mObjects[nextObject]);
1327 mNextObjectHint = nextObject;
1328 }
1329 return NO_ERROR;
1330 }
1331 // Quickly determine if mObjects is sorted.
1332 binder_size_t* currObj = mObjects + mObjectsSize - 1;
1333 binder_size_t* prevObj = currObj;
1334 while (currObj > mObjects) {
1335 prevObj--;
1336 if(*prevObj > *currObj) {
1337 goto data_unsorted;
1338 }
1339 currObj--;
1340 }
1341 mObjectsSorted = true;
1342 goto data_sorted;
1343
1344data_unsorted:
1345 // Insertion Sort mObjects
1346 // Great for mostly sorted lists. If randomly sorted or reverse ordered mObjects become common,
1347 // switch to std::sort(mObjects, mObjects + mObjectsSize);
1348 for (binder_size_t* iter0 = mObjects + 1; iter0 < mObjects + mObjectsSize; iter0++) {
1349 binder_size_t temp = *iter0;
1350 binder_size_t* iter1 = iter0 - 1;
1351 while (iter1 >= mObjects && *iter1 > temp) {
1352 *(iter1 + 1) = *iter1;
1353 iter1--;
1354 }
1355 *(iter1 + 1) = temp;
1356 }
1357 mNextObjectHint = 0;
1358 mObjectsSorted = true;
1359 goto data_sorted;
1360}
1361
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001362status_t Parcel::read(void* outData, size_t len) const
1363{
Nick Kralevichb6b14232015-04-02 09:36:02 -07001364 if (len > INT32_MAX) {
1365 // don't accept size_t values which may have come from an
1366 // inadvertent conversion from a negative int.
1367 return BAD_VALUE;
1368 }
1369
1370 if ((mDataPos+pad_size(len)) >= mDataPos && (mDataPos+pad_size(len)) <= mDataSize
1371 && len <= pad_size(len)) {
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08001372 if (mObjectsSize > 0) {
1373 status_t err = validateReadData(mDataPos + pad_size(len));
Michael Wachenschwanza17f0222018-04-17 16:52:40 -07001374 if(err != NO_ERROR) {
1375 // Still increment the data position by the expected length
1376 mDataPos += pad_size(len);
1377 ALOGV("read Setting data pos of %p to %zu", this, mDataPos);
1378 return err;
1379 }
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08001380 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001381 memcpy(outData, mData+mDataPos, len);
Nick Kralevichb6b14232015-04-02 09:36:02 -07001382 mDataPos += pad_size(len);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001383 ALOGV("read Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001384 return NO_ERROR;
1385 }
1386 return NOT_ENOUGH_DATA;
1387}
1388
1389const void* Parcel::readInplace(size_t len) const
1390{
Nick Kralevichb6b14232015-04-02 09:36:02 -07001391 if (len > INT32_MAX) {
1392 // don't accept size_t values which may have come from an
1393 // inadvertent conversion from a negative int.
Yi Kong91635562018-06-07 14:38:36 -07001394 return nullptr;
Nick Kralevichb6b14232015-04-02 09:36:02 -07001395 }
1396
1397 if ((mDataPos+pad_size(len)) >= mDataPos && (mDataPos+pad_size(len)) <= mDataSize
1398 && len <= pad_size(len)) {
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08001399 if (mObjectsSize > 0) {
1400 status_t err = validateReadData(mDataPos + pad_size(len));
Michael Wachenschwanza17f0222018-04-17 16:52:40 -07001401 if(err != NO_ERROR) {
1402 // Still increment the data position by the expected length
1403 mDataPos += pad_size(len);
1404 ALOGV("readInplace Setting data pos of %p to %zu", this, mDataPos);
Yi Kong91635562018-06-07 14:38:36 -07001405 return nullptr;
Michael Wachenschwanza17f0222018-04-17 16:52:40 -07001406 }
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08001407 }
1408
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001409 const void* data = mData+mDataPos;
Nick Kralevichb6b14232015-04-02 09:36:02 -07001410 mDataPos += pad_size(len);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001411 ALOGV("readInplace Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001412 return data;
1413 }
Yi Kong91635562018-06-07 14:38:36 -07001414 return nullptr;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001415}
1416
Andreas Huber84a6d042009-08-17 13:33:27 -07001417template<class T>
1418status_t Parcel::readAligned(T *pArg) const {
Nick Kralevichb6b14232015-04-02 09:36:02 -07001419 COMPILE_TIME_ASSERT_FUNCTION_SCOPE(PAD_SIZE_UNSAFE(sizeof(T)) == sizeof(T));
Andreas Huber84a6d042009-08-17 13:33:27 -07001420
1421 if ((mDataPos+sizeof(T)) <= mDataSize) {
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08001422 if (mObjectsSize > 0) {
1423 status_t err = validateReadData(mDataPos + sizeof(T));
Michael Wachenschwanza17f0222018-04-17 16:52:40 -07001424 if(err != NO_ERROR) {
1425 // Still increment the data position by the expected length
1426 mDataPos += sizeof(T);
1427 return err;
1428 }
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08001429 }
1430
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001431 const void* data = mData+mDataPos;
Andreas Huber84a6d042009-08-17 13:33:27 -07001432 mDataPos += sizeof(T);
1433 *pArg = *reinterpret_cast<const T*>(data);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001434 return NO_ERROR;
1435 } else {
1436 return NOT_ENOUGH_DATA;
1437 }
1438}
1439
Andreas Huber84a6d042009-08-17 13:33:27 -07001440template<class T>
1441T Parcel::readAligned() const {
1442 T result;
1443 if (readAligned(&result) != NO_ERROR) {
1444 result = 0;
1445 }
1446
1447 return result;
1448}
1449
1450template<class T>
1451status_t Parcel::writeAligned(T val) {
Nick Kralevichb6b14232015-04-02 09:36:02 -07001452 COMPILE_TIME_ASSERT_FUNCTION_SCOPE(PAD_SIZE_UNSAFE(sizeof(T)) == sizeof(T));
Andreas Huber84a6d042009-08-17 13:33:27 -07001453
1454 if ((mDataPos+sizeof(val)) <= mDataCapacity) {
1455restart_write:
1456 *reinterpret_cast<T*>(mData+mDataPos) = val;
1457 return finishWrite(sizeof(val));
1458 }
1459
1460 status_t err = growData(sizeof(val));
1461 if (err == NO_ERROR) goto restart_write;
1462 return err;
1463}
1464
Casey Dahlin185d3442016-02-09 11:08:35 -08001465namespace {
1466
1467template<typename T>
1468status_t readByteVectorInternal(const Parcel* parcel,
1469 std::vector<T>* val) {
Casey Dahlin451ff582015-10-19 18:12:18 -07001470 val->clear();
1471
1472 int32_t size;
Casey Dahlin185d3442016-02-09 11:08:35 -08001473 status_t status = parcel->readInt32(&size);
Casey Dahlin451ff582015-10-19 18:12:18 -07001474
1475 if (status != OK) {
1476 return status;
1477 }
1478
Christopher Wiley4db672d2015-11-10 09:44:30 -08001479 if (size < 0) {
1480 status = UNEXPECTED_NULL;
1481 return status;
1482 }
Casey Dahlin185d3442016-02-09 11:08:35 -08001483 if (size_t(size) > parcel->dataAvail()) {
Christopher Wileyf0fc52b2015-10-31 13:22:15 -07001484 status = BAD_VALUE;
1485 return status;
Casey Dahlin451ff582015-10-19 18:12:18 -07001486 }
Christopher Wiley4db672d2015-11-10 09:44:30 -08001487
Paul Lietar433e87b2016-09-16 10:39:32 -07001488 T* data = const_cast<T*>(reinterpret_cast<const T*>(parcel->readInplace(size)));
Christopher Wileyf0fc52b2015-10-31 13:22:15 -07001489 if (!data) {
1490 status = BAD_VALUE;
1491 return status;
1492 }
Paul Lietar433e87b2016-09-16 10:39:32 -07001493 val->reserve(size);
1494 val->insert(val->end(), data, data + size);
Casey Dahlin451ff582015-10-19 18:12:18 -07001495
Christopher Wileyf0fc52b2015-10-31 13:22:15 -07001496 return status;
Casey Dahlin451ff582015-10-19 18:12:18 -07001497}
1498
Casey Dahlin185d3442016-02-09 11:08:35 -08001499template<typename T>
1500status_t readByteVectorInternalPtr(
1501 const Parcel* parcel,
1502 std::unique_ptr<std::vector<T>>* val) {
1503 const int32_t start = parcel->dataPosition();
Casey Dahlinb9872622015-11-25 15:09:45 -08001504 int32_t size;
Casey Dahlin185d3442016-02-09 11:08:35 -08001505 status_t status = parcel->readInt32(&size);
Casey Dahlinb9872622015-11-25 15:09:45 -08001506 val->reset();
1507
1508 if (status != OK || size < 0) {
1509 return status;
1510 }
1511
Casey Dahlin185d3442016-02-09 11:08:35 -08001512 parcel->setDataPosition(start);
Christopher Tatee4e0ae82016-03-24 16:03:44 -07001513 val->reset(new (std::nothrow) std::vector<T>());
Casey Dahlinb9872622015-11-25 15:09:45 -08001514
Casey Dahlin185d3442016-02-09 11:08:35 -08001515 status = readByteVectorInternal(parcel, val->get());
Casey Dahlinb9872622015-11-25 15:09:45 -08001516
1517 if (status != OK) {
1518 val->reset();
1519 }
1520
1521 return status;
1522}
1523
Casey Dahlin185d3442016-02-09 11:08:35 -08001524} // namespace
1525
1526status_t Parcel::readByteVector(std::vector<int8_t>* val) const {
1527 return readByteVectorInternal(this, val);
1528}
1529
1530status_t Parcel::readByteVector(std::vector<uint8_t>* val) const {
1531 return readByteVectorInternal(this, val);
1532}
1533
1534status_t Parcel::readByteVector(std::unique_ptr<std::vector<int8_t>>* val) const {
1535 return readByteVectorInternalPtr(this, val);
1536}
1537
1538status_t Parcel::readByteVector(std::unique_ptr<std::vector<uint8_t>>* val) const {
1539 return readByteVectorInternalPtr(this, val);
1540}
1541
Casey Dahlinb9872622015-11-25 15:09:45 -08001542status_t Parcel::readInt32Vector(std::unique_ptr<std::vector<int32_t>>* val) const {
1543 return readNullableTypedVector(val, &Parcel::readInt32);
1544}
1545
Casey Dahlin451ff582015-10-19 18:12:18 -07001546status_t Parcel::readInt32Vector(std::vector<int32_t>* val) const {
Christopher Wiley03d1eb62015-11-19 06:42:40 -08001547 return readTypedVector(val, &Parcel::readInt32);
Casey Dahlin451ff582015-10-19 18:12:18 -07001548}
1549
Casey Dahlinb9872622015-11-25 15:09:45 -08001550status_t Parcel::readInt64Vector(std::unique_ptr<std::vector<int64_t>>* val) const {
1551 return readNullableTypedVector(val, &Parcel::readInt64);
1552}
1553
Casey Dahlin451ff582015-10-19 18:12:18 -07001554status_t Parcel::readInt64Vector(std::vector<int64_t>* val) const {
Christopher Wiley03d1eb62015-11-19 06:42:40 -08001555 return readTypedVector(val, &Parcel::readInt64);
Casey Dahlin451ff582015-10-19 18:12:18 -07001556}
1557
Kevin DuBois2f82d5b2018-12-05 12:56:10 -08001558status_t Parcel::readUint64Vector(std::unique_ptr<std::vector<uint64_t>>* val) const {
1559 return readNullableTypedVector(val, &Parcel::readUint64);
1560}
1561
1562status_t Parcel::readUint64Vector(std::vector<uint64_t>* val) const {
1563 return readTypedVector(val, &Parcel::readUint64);
1564}
1565
Casey Dahlinb9872622015-11-25 15:09:45 -08001566status_t Parcel::readFloatVector(std::unique_ptr<std::vector<float>>* val) const {
1567 return readNullableTypedVector(val, &Parcel::readFloat);
1568}
1569
Casey Dahlin451ff582015-10-19 18:12:18 -07001570status_t Parcel::readFloatVector(std::vector<float>* val) const {
Christopher Wiley03d1eb62015-11-19 06:42:40 -08001571 return readTypedVector(val, &Parcel::readFloat);
Casey Dahlin451ff582015-10-19 18:12:18 -07001572}
1573
Casey Dahlinb9872622015-11-25 15:09:45 -08001574status_t Parcel::readDoubleVector(std::unique_ptr<std::vector<double>>* val) const {
1575 return readNullableTypedVector(val, &Parcel::readDouble);
1576}
1577
Casey Dahlin451ff582015-10-19 18:12:18 -07001578status_t Parcel::readDoubleVector(std::vector<double>* val) const {
Christopher Wiley03d1eb62015-11-19 06:42:40 -08001579 return readTypedVector(val, &Parcel::readDouble);
Casey Dahlin451ff582015-10-19 18:12:18 -07001580}
1581
Casey Dahlinb9872622015-11-25 15:09:45 -08001582status_t Parcel::readBoolVector(std::unique_ptr<std::vector<bool>>* val) const {
1583 const int32_t start = dataPosition();
1584 int32_t size;
1585 status_t status = readInt32(&size);
1586 val->reset();
Casey Dahlin451ff582015-10-19 18:12:18 -07001587
Casey Dahlinb9872622015-11-25 15:09:45 -08001588 if (status != OK || size < 0) {
1589 return status;
1590 }
1591
1592 setDataPosition(start);
Christopher Tatee4e0ae82016-03-24 16:03:44 -07001593 val->reset(new (std::nothrow) std::vector<bool>());
Casey Dahlinb9872622015-11-25 15:09:45 -08001594
1595 status = readBoolVector(val->get());
1596
1597 if (status != OK) {
1598 val->reset();
1599 }
1600
1601 return status;
1602}
1603
1604status_t Parcel::readBoolVector(std::vector<bool>* val) const {
Casey Dahlin451ff582015-10-19 18:12:18 -07001605 int32_t size;
1606 status_t status = readInt32(&size);
1607
1608 if (status != OK) {
1609 return status;
1610 }
1611
1612 if (size < 0) {
Christopher Wiley4db672d2015-11-10 09:44:30 -08001613 return UNEXPECTED_NULL;
Casey Dahlin451ff582015-10-19 18:12:18 -07001614 }
1615
1616 val->resize(size);
1617
1618 /* C++ bool handling means a vector of bools isn't necessarily addressable
1619 * (we might use individual bits)
1620 */
Christopher Wiley97887982015-10-27 16:33:47 -07001621 bool data;
1622 for (int32_t i = 0; i < size; ++i) {
Casey Dahlin451ff582015-10-19 18:12:18 -07001623 status = readBool(&data);
1624 (*val)[i] = data;
1625
1626 if (status != OK) {
1627 return status;
1628 }
1629 }
1630
1631 return OK;
1632}
1633
Casey Dahlinb9872622015-11-25 15:09:45 -08001634status_t Parcel::readCharVector(std::unique_ptr<std::vector<char16_t>>* val) const {
1635 return readNullableTypedVector(val, &Parcel::readChar);
1636}
1637
Casey Dahlin451ff582015-10-19 18:12:18 -07001638status_t Parcel::readCharVector(std::vector<char16_t>* val) const {
Christopher Wiley03d1eb62015-11-19 06:42:40 -08001639 return readTypedVector(val, &Parcel::readChar);
Casey Dahlin451ff582015-10-19 18:12:18 -07001640}
1641
Casey Dahlinb9872622015-11-25 15:09:45 -08001642status_t Parcel::readString16Vector(
1643 std::unique_ptr<std::vector<std::unique_ptr<String16>>>* val) const {
1644 return readNullableTypedVector(val, &Parcel::readString16);
1645}
1646
Casey Dahlin451ff582015-10-19 18:12:18 -07001647status_t Parcel::readString16Vector(std::vector<String16>* val) const {
Christopher Wiley03d1eb62015-11-19 06:42:40 -08001648 return readTypedVector(val, &Parcel::readString16);
Casey Dahlin451ff582015-10-19 18:12:18 -07001649}
1650
Christopher Wiley9a5e32f2016-01-28 16:56:53 -08001651status_t Parcel::readUtf8VectorFromUtf16Vector(
1652 std::unique_ptr<std::vector<std::unique_ptr<std::string>>>* val) const {
1653 return readNullableTypedVector(val, &Parcel::readUtf8FromUtf16);
1654}
1655
1656status_t Parcel::readUtf8VectorFromUtf16Vector(std::vector<std::string>* val) const {
1657 return readTypedVector(val, &Parcel::readUtf8FromUtf16);
1658}
Casey Dahlin451ff582015-10-19 18:12:18 -07001659
Andreas Huber84a6d042009-08-17 13:33:27 -07001660status_t Parcel::readInt32(int32_t *pArg) const
1661{
1662 return readAligned(pArg);
1663}
1664
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001665int32_t Parcel::readInt32() const
1666{
Andreas Huber84a6d042009-08-17 13:33:27 -07001667 return readAligned<int32_t>();
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001668}
1669
Dan Stoza41a0f2f2014-12-01 10:01:10 -08001670status_t Parcel::readUint32(uint32_t *pArg) const
1671{
1672 return readAligned(pArg);
1673}
1674
1675uint32_t Parcel::readUint32() const
1676{
1677 return readAligned<uint32_t>();
1678}
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001679
1680status_t Parcel::readInt64(int64_t *pArg) const
1681{
Andreas Huber84a6d042009-08-17 13:33:27 -07001682 return readAligned(pArg);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001683}
1684
1685
1686int64_t Parcel::readInt64() const
1687{
Andreas Huber84a6d042009-08-17 13:33:27 -07001688 return readAligned<int64_t>();
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001689}
1690
Ronghua Wu2d13afd2015-03-16 11:11:07 -07001691status_t Parcel::readUint64(uint64_t *pArg) const
1692{
1693 return readAligned(pArg);
1694}
1695
1696uint64_t Parcel::readUint64() const
1697{
1698 return readAligned<uint64_t>();
1699}
1700
Serban Constantinescuf683e012013-11-05 16:53:55 +00001701status_t Parcel::readPointer(uintptr_t *pArg) const
1702{
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001703 status_t ret;
1704 binder_uintptr_t ptr;
1705 ret = readAligned(&ptr);
1706 if (!ret)
1707 *pArg = ptr;
1708 return ret;
Serban Constantinescuf683e012013-11-05 16:53:55 +00001709}
1710
1711uintptr_t Parcel::readPointer() const
1712{
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001713 return readAligned<binder_uintptr_t>();
Serban Constantinescuf683e012013-11-05 16:53:55 +00001714}
1715
1716
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001717status_t Parcel::readFloat(float *pArg) const
1718{
Andreas Huber84a6d042009-08-17 13:33:27 -07001719 return readAligned(pArg);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001720}
1721
1722
1723float Parcel::readFloat() const
1724{
Andreas Huber84a6d042009-08-17 13:33:27 -07001725 return readAligned<float>();
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001726}
1727
Douglas Leungcc1a4bb2013-01-11 15:00:55 -08001728#if defined(__mips__) && defined(__mips_hard_float)
1729
1730status_t Parcel::readDouble(double *pArg) const
1731{
1732 union {
1733 double d;
1734 unsigned long long ll;
1735 } u;
Narayan Kamath2c68d382014-06-04 15:04:29 +01001736 u.d = 0;
Douglas Leungcc1a4bb2013-01-11 15:00:55 -08001737 status_t status;
1738 status = readAligned(&u.ll);
1739 *pArg = u.d;
1740 return status;
1741}
1742
1743double Parcel::readDouble() const
1744{
1745 union {
1746 double d;
1747 unsigned long long ll;
1748 } u;
1749 u.ll = readAligned<unsigned long long>();
1750 return u.d;
1751}
1752
1753#else
1754
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001755status_t Parcel::readDouble(double *pArg) const
1756{
Andreas Huber84a6d042009-08-17 13:33:27 -07001757 return readAligned(pArg);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001758}
1759
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001760double Parcel::readDouble() const
1761{
Andreas Huber84a6d042009-08-17 13:33:27 -07001762 return readAligned<double>();
1763}
1764
Douglas Leungcc1a4bb2013-01-11 15:00:55 -08001765#endif
1766
Andreas Huber84a6d042009-08-17 13:33:27 -07001767status_t Parcel::readIntPtr(intptr_t *pArg) const
1768{
1769 return readAligned(pArg);
1770}
1771
1772
1773intptr_t Parcel::readIntPtr() const
1774{
1775 return readAligned<intptr_t>();
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001776}
1777
Casey Dahlind6848f52015-10-15 15:44:59 -07001778status_t Parcel::readBool(bool *pArg) const
1779{
Manoj Gupta6eb62052017-07-12 10:29:15 -07001780 int32_t tmp = 0;
Casey Dahlind6848f52015-10-15 15:44:59 -07001781 status_t ret = readInt32(&tmp);
1782 *pArg = (tmp != 0);
1783 return ret;
1784}
1785
1786bool Parcel::readBool() const
1787{
1788 return readInt32() != 0;
1789}
1790
1791status_t Parcel::readChar(char16_t *pArg) const
1792{
Manoj Gupta6eb62052017-07-12 10:29:15 -07001793 int32_t tmp = 0;
Casey Dahlind6848f52015-10-15 15:44:59 -07001794 status_t ret = readInt32(&tmp);
1795 *pArg = char16_t(tmp);
1796 return ret;
1797}
1798
1799char16_t Parcel::readChar() const
1800{
1801 return char16_t(readInt32());
1802}
1803
1804status_t Parcel::readByte(int8_t *pArg) const
1805{
Manoj Gupta6eb62052017-07-12 10:29:15 -07001806 int32_t tmp = 0;
Casey Dahlind6848f52015-10-15 15:44:59 -07001807 status_t ret = readInt32(&tmp);
1808 *pArg = int8_t(tmp);
1809 return ret;
1810}
1811
1812int8_t Parcel::readByte() const
1813{
1814 return int8_t(readInt32());
1815}
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001816
Christopher Wiley9a5e32f2016-01-28 16:56:53 -08001817status_t Parcel::readUtf8FromUtf16(std::string* str) const {
1818 size_t utf16Size = 0;
1819 const char16_t* src = readString16Inplace(&utf16Size);
1820 if (!src) {
1821 return UNEXPECTED_NULL;
1822 }
1823
1824 // Save ourselves the trouble, we're done.
1825 if (utf16Size == 0u) {
1826 str->clear();
1827 return NO_ERROR;
1828 }
1829
Sergio Giro9b39ebe2016-06-28 18:19:33 +01001830 // Allow for closing '\0'
1831 ssize_t utf8Size = utf16_to_utf8_length(src, utf16Size) + 1;
1832 if (utf8Size < 1) {
Christopher Wiley9a5e32f2016-01-28 16:56:53 -08001833 return BAD_VALUE;
1834 }
1835 // Note that while it is probably safe to assume string::resize keeps a
Sergio Giro9b39ebe2016-06-28 18:19:33 +01001836 // spare byte around for the trailing null, we still pass the size including the trailing null
Christopher Wiley9a5e32f2016-01-28 16:56:53 -08001837 str->resize(utf8Size);
Sergio Giro9b39ebe2016-06-28 18:19:33 +01001838 utf16_to_utf8(src, utf16Size, &((*str)[0]), utf8Size);
1839 str->resize(utf8Size - 1);
Christopher Wiley9a5e32f2016-01-28 16:56:53 -08001840 return NO_ERROR;
1841}
1842
1843status_t Parcel::readUtf8FromUtf16(std::unique_ptr<std::string>* str) const {
1844 const int32_t start = dataPosition();
1845 int32_t size;
1846 status_t status = readInt32(&size);
1847 str->reset();
1848
1849 if (status != OK || size < 0) {
1850 return status;
1851 }
1852
1853 setDataPosition(start);
Christopher Tatee4e0ae82016-03-24 16:03:44 -07001854 str->reset(new (std::nothrow) std::string());
Christopher Wiley9a5e32f2016-01-28 16:56:53 -08001855 return readUtf8FromUtf16(str->get());
1856}
1857
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001858const char* Parcel::readCString() const
1859{
Steven Morelandd0d4b582019-05-17 13:14:06 -07001860 if (mDataPos < mDataSize) {
1861 const size_t avail = mDataSize-mDataPos;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001862 const char* str = reinterpret_cast<const char*>(mData+mDataPos);
1863 // is the string's trailing NUL within the parcel's valid bounds?
1864 const char* eos = reinterpret_cast<const char*>(memchr(str, 0, avail));
1865 if (eos) {
1866 const size_t len = eos - str;
Nick Kralevichb6b14232015-04-02 09:36:02 -07001867 mDataPos += pad_size(len+1);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001868 ALOGV("readCString Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001869 return str;
1870 }
1871 }
Yi Kong91635562018-06-07 14:38:36 -07001872 return nullptr;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001873}
1874
1875String8 Parcel::readString8() const
1876{
Roshan Pius87b64d22016-07-18 12:51:02 -07001877 String8 retString;
1878 status_t status = readString8(&retString);
1879 if (status != OK) {
1880 // We don't care about errors here, so just return an empty string.
1881 return String8();
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001882 }
Roshan Pius87b64d22016-07-18 12:51:02 -07001883 return retString;
1884}
1885
1886status_t Parcel::readString8(String8* pArg) const
1887{
1888 int32_t size;
1889 status_t status = readInt32(&size);
1890 if (status != OK) {
1891 return status;
1892 }
1893 // watch for potential int overflow from size+1
1894 if (size < 0 || size >= INT32_MAX) {
1895 return BAD_VALUE;
1896 }
1897 // |writeString8| writes nothing for empty string.
1898 if (size == 0) {
1899 *pArg = String8();
1900 return OK;
1901 }
1902 const char* str = (const char*)readInplace(size + 1);
Yi Kong91635562018-06-07 14:38:36 -07001903 if (str == nullptr) {
Roshan Pius87b64d22016-07-18 12:51:02 -07001904 return BAD_VALUE;
1905 }
1906 pArg->setTo(str, size);
1907 return OK;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001908}
1909
1910String16 Parcel::readString16() const
1911{
1912 size_t len;
1913 const char16_t* str = readString16Inplace(&len);
1914 if (str) return String16(str, len);
Steve Blocke6f43dd2012-01-06 19:20:56 +00001915 ALOGE("Reading a NULL string not supported here.");
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001916 return String16();
1917}
1918
Casey Dahlinb9872622015-11-25 15:09:45 -08001919status_t Parcel::readString16(std::unique_ptr<String16>* pArg) const
1920{
1921 const int32_t start = dataPosition();
1922 int32_t size;
1923 status_t status = readInt32(&size);
1924 pArg->reset();
1925
1926 if (status != OK || size < 0) {
1927 return status;
1928 }
1929
1930 setDataPosition(start);
Christopher Tatee4e0ae82016-03-24 16:03:44 -07001931 pArg->reset(new (std::nothrow) String16());
Casey Dahlinb9872622015-11-25 15:09:45 -08001932
1933 status = readString16(pArg->get());
1934
1935 if (status != OK) {
1936 pArg->reset();
1937 }
1938
1939 return status;
1940}
1941
Casey Dahlin451ff582015-10-19 18:12:18 -07001942status_t Parcel::readString16(String16* pArg) const
1943{
1944 size_t len;
1945 const char16_t* str = readString16Inplace(&len);
1946 if (str) {
Casey Dahlin1515ea12015-10-20 16:26:23 -07001947 pArg->setTo(str, len);
Casey Dahlin451ff582015-10-19 18:12:18 -07001948 return 0;
1949 } else {
1950 *pArg = String16();
Christopher Wiley4db672d2015-11-10 09:44:30 -08001951 return UNEXPECTED_NULL;
Casey Dahlin451ff582015-10-19 18:12:18 -07001952 }
1953}
1954
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001955const char16_t* Parcel::readString16Inplace(size_t* outLen) const
1956{
1957 int32_t size = readInt32();
1958 // watch for potential int overflow from size+1
1959 if (size >= 0 && size < INT32_MAX) {
1960 *outLen = size;
1961 const char16_t* str = (const char16_t*)readInplace((size+1)*sizeof(char16_t));
Yi Kong91635562018-06-07 14:38:36 -07001962 if (str != nullptr) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001963 return str;
1964 }
1965 }
1966 *outLen = 0;
Yi Kong91635562018-06-07 14:38:36 -07001967 return nullptr;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001968}
1969
Casey Dahlinf0c13772015-10-27 18:33:56 -07001970status_t Parcel::readStrongBinder(sp<IBinder>* val) const
1971{
Christopher Wiley35d77ca2016-03-08 10:49:51 -08001972 status_t status = readNullableStrongBinder(val);
1973 if (status == OK && !val->get()) {
1974 status = UNEXPECTED_NULL;
1975 }
1976 return status;
1977}
1978
1979status_t Parcel::readNullableStrongBinder(sp<IBinder>* val) const
1980{
Casey Dahlinf0c13772015-10-27 18:33:56 -07001981 return unflatten_binder(ProcessState::self(), *this, val);
1982}
1983
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001984sp<IBinder> Parcel::readStrongBinder() const
1985{
1986 sp<IBinder> val;
Christopher Wiley35d77ca2016-03-08 10:49:51 -08001987 // Note that a lot of code in Android reads binders by hand with this
1988 // method, and that code has historically been ok with getting nullptr
1989 // back (while ignoring error codes).
1990 readNullableStrongBinder(&val);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001991 return val;
1992}
1993
Christopher Wiley97f048d2015-11-19 06:49:05 -08001994status_t Parcel::readParcelable(Parcelable* parcelable) const {
1995 int32_t have_parcelable = 0;
1996 status_t status = readInt32(&have_parcelable);
1997 if (status != OK) {
1998 return status;
1999 }
2000 if (!have_parcelable) {
2001 return UNEXPECTED_NULL;
2002 }
2003 return parcelable->readFromParcel(this);
2004}
2005
Brad Fitzpatrick837a0d02010-07-13 15:33:35 -07002006int32_t Parcel::readExceptionCode() const
2007{
Christopher Wiley09eb7492015-11-09 15:06:15 -08002008 binder::Status status;
2009 status.readFromParcel(*this);
2010 return status.exceptionCode();
Brad Fitzpatrick837a0d02010-07-13 15:33:35 -07002011}
The Android Open Source Project5f78a482009-01-20 14:03:58 -08002012
Mathias Agopiana47f02a2009-05-21 16:29:38 -07002013native_handle* Parcel::readNativeHandle() const
The Android Open Source Project5f78a482009-01-20 14:03:58 -08002014{
2015 int numFds, numInts;
2016 status_t err;
2017 err = readInt32(&numFds);
Yi Kong91635562018-06-07 14:38:36 -07002018 if (err != NO_ERROR) return nullptr;
The Android Open Source Project5f78a482009-01-20 14:03:58 -08002019 err = readInt32(&numInts);
Yi Kong91635562018-06-07 14:38:36 -07002020 if (err != NO_ERROR) return nullptr;
The Android Open Source Project5f78a482009-01-20 14:03:58 -08002021
Mathias Agopiana47f02a2009-05-21 16:29:38 -07002022 native_handle* h = native_handle_create(numFds, numInts);
Adam Lesinskieaac99a2015-05-12 17:35:48 -07002023 if (!h) {
Yi Kong91635562018-06-07 14:38:36 -07002024 return nullptr;
Adam Lesinskieaac99a2015-05-12 17:35:48 -07002025 }
2026
The Android Open Source Project5f78a482009-01-20 14:03:58 -08002027 for (int i=0 ; err==NO_ERROR && i<numFds ; i++) {
Nick Kralevichec9ec7d2016-12-17 19:47:27 -08002028 h->data[i] = fcntl(readFileDescriptor(), F_DUPFD_CLOEXEC, 0);
Marco Nelissen1de79662016-04-26 08:44:09 -07002029 if (h->data[i] < 0) {
2030 for (int j = 0; j < i; j++) {
2031 close(h->data[j]);
2032 }
2033 native_handle_delete(h);
Yi Kong91635562018-06-07 14:38:36 -07002034 return nullptr;
Marco Nelissen1de79662016-04-26 08:44:09 -07002035 }
The Android Open Source Project5f78a482009-01-20 14:03:58 -08002036 }
The Android Open Source Project5f78a482009-01-20 14:03:58 -08002037 err = read(h->data + numFds, sizeof(int)*numInts);
The Android Open Source Project5f78a482009-01-20 14:03:58 -08002038 if (err != NO_ERROR) {
Mathias Agopiana47f02a2009-05-21 16:29:38 -07002039 native_handle_close(h);
2040 native_handle_delete(h);
Yi Kong91635562018-06-07 14:38:36 -07002041 h = nullptr;
The Android Open Source Project5f78a482009-01-20 14:03:58 -08002042 }
2043 return h;
2044}
2045
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002046int Parcel::readFileDescriptor() const
2047{
2048 const flat_binder_object* flat = readObject(true);
Casey Dahlin06673e32015-11-23 13:24:23 -08002049
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -07002050 if (flat && flat->hdr.type == BINDER_TYPE_FD) {
Casey Dahlin06673e32015-11-23 13:24:23 -08002051 return flat->handle;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002052 }
Casey Dahlin06673e32015-11-23 13:24:23 -08002053
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002054 return BAD_TYPE;
2055}
2056
Dianne Hackborn1941a402016-08-29 12:30:43 -07002057int Parcel::readParcelFileDescriptor() const
2058{
2059 int32_t hasComm = readInt32();
2060 int fd = readFileDescriptor();
2061 if (hasComm != 0) {
Steven Morelandb73806a2018-11-12 19:35:47 -08002062 // detach (owned by the binder driver)
2063 int comm = readFileDescriptor();
2064
2065 // warning: this must be kept in sync with:
2066 // frameworks/base/core/java/android/os/ParcelFileDescriptor.java
2067 enum ParcelFileDescriptorStatus {
2068 DETACHED = 2,
2069 };
2070
2071#if BYTE_ORDER == BIG_ENDIAN
2072 const int32_t message = ParcelFileDescriptorStatus::DETACHED;
2073#endif
2074#if BYTE_ORDER == LITTLE_ENDIAN
2075 const int32_t message = __builtin_bswap32(ParcelFileDescriptorStatus::DETACHED);
2076#endif
2077
2078 ssize_t written = TEMP_FAILURE_RETRY(
2079 ::write(comm, &message, sizeof(message)));
2080
2081 if (written == -1 || written != sizeof(message)) {
2082 ALOGW("Failed to detach ParcelFileDescriptor written: %zd err: %s",
2083 written, strerror(errno));
2084 return BAD_TYPE;
2085 }
Dianne Hackborn1941a402016-08-29 12:30:43 -07002086 }
2087 return fd;
2088}
2089
Christopher Wiley2cf19952016-04-11 11:09:37 -07002090status_t Parcel::readUniqueFileDescriptor(base::unique_fd* val) const
Casey Dahlin06673e32015-11-23 13:24:23 -08002091{
2092 int got = readFileDescriptor();
2093
2094 if (got == BAD_TYPE) {
2095 return BAD_TYPE;
2096 }
2097
Nick Kralevichec9ec7d2016-12-17 19:47:27 -08002098 val->reset(fcntl(got, F_DUPFD_CLOEXEC, 0));
Casey Dahlin06673e32015-11-23 13:24:23 -08002099
2100 if (val->get() < 0) {
2101 return BAD_VALUE;
2102 }
2103
2104 return OK;
2105}
2106
Ryo Hashimotobf551892018-05-31 16:58:35 +09002107status_t Parcel::readUniqueParcelFileDescriptor(base::unique_fd* val) const
2108{
2109 int got = readParcelFileDescriptor();
2110
2111 if (got == BAD_TYPE) {
2112 return BAD_TYPE;
2113 }
2114
2115 val->reset(fcntl(got, F_DUPFD_CLOEXEC, 0));
2116
2117 if (val->get() < 0) {
2118 return BAD_VALUE;
2119 }
2120
2121 return OK;
2122}
Casey Dahlin06673e32015-11-23 13:24:23 -08002123
Christopher Wiley2cf19952016-04-11 11:09:37 -07002124status_t Parcel::readUniqueFileDescriptorVector(std::unique_ptr<std::vector<base::unique_fd>>* val) const {
Casey Dahlinb9872622015-11-25 15:09:45 -08002125 return readNullableTypedVector(val, &Parcel::readUniqueFileDescriptor);
2126}
2127
Christopher Wiley2cf19952016-04-11 11:09:37 -07002128status_t Parcel::readUniqueFileDescriptorVector(std::vector<base::unique_fd>* val) const {
Casey Dahlin06673e32015-11-23 13:24:23 -08002129 return readTypedVector(val, &Parcel::readUniqueFileDescriptor);
2130}
2131
Jeff Brown5707dbf2011-09-23 21:17:56 -07002132status_t Parcel::readBlob(size_t len, ReadableBlob* outBlob) const
2133{
Jeff Brown13b16042014-11-11 16:44:25 -08002134 int32_t blobType;
2135 status_t status = readInt32(&blobType);
Jeff Brown5707dbf2011-09-23 21:17:56 -07002136 if (status) return status;
2137
Jeff Brown13b16042014-11-11 16:44:25 -08002138 if (blobType == BLOB_INPLACE) {
Steve Block6807e592011-10-20 11:56:00 +01002139 ALOGV("readBlob: read in place");
Jeff Brown5707dbf2011-09-23 21:17:56 -07002140 const void* ptr = readInplace(len);
2141 if (!ptr) return BAD_VALUE;
2142
Jeff Brown13b16042014-11-11 16:44:25 -08002143 outBlob->init(-1, const_cast<void*>(ptr), len, false);
Jeff Brown5707dbf2011-09-23 21:17:56 -07002144 return NO_ERROR;
2145 }
2146
Steve Block6807e592011-10-20 11:56:00 +01002147 ALOGV("readBlob: read from ashmem");
Jeff Brown13b16042014-11-11 16:44:25 -08002148 bool isMutable = (blobType == BLOB_ASHMEM_MUTABLE);
Jeff Brown5707dbf2011-09-23 21:17:56 -07002149 int fd = readFileDescriptor();
2150 if (fd == int(BAD_TYPE)) return BAD_VALUE;
2151
Jorim Jaggi150b4ef2018-07-13 11:18:30 +00002152 if (!ashmem_valid(fd)) {
2153 ALOGE("invalid fd");
2154 return BAD_VALUE;
2155 }
Marco Nelissen7a96ec42018-06-06 07:37:46 -07002156 int size = ashmem_get_size_region(fd);
2157 if (size < 0 || size_t(size) < len) {
Jorim Jaggi150b4ef2018-07-13 11:18:30 +00002158 ALOGE("request size %zu does not match fd size %d", len, size);
Marco Nelissen7a96ec42018-06-06 07:37:46 -07002159 return BAD_VALUE;
2160 }
Yi Kong91635562018-06-07 14:38:36 -07002161 void* ptr = ::mmap(nullptr, len, isMutable ? PROT_READ | PROT_WRITE : PROT_READ,
Jeff Brown13b16042014-11-11 16:44:25 -08002162 MAP_SHARED, fd, 0);
Narayan Kamath9ea09752014-10-08 17:35:45 +01002163 if (ptr == MAP_FAILED) return NO_MEMORY;
Jeff Brown5707dbf2011-09-23 21:17:56 -07002164
Jeff Brown13b16042014-11-11 16:44:25 -08002165 outBlob->init(fd, ptr, len, isMutable);
Jeff Brown5707dbf2011-09-23 21:17:56 -07002166 return NO_ERROR;
2167}
2168
Mathias Agopiane1424282013-07-29 21:24:40 -07002169status_t Parcel::read(FlattenableHelperInterface& val) const
Mathias Agopian98e71dd2010-02-11 17:30:52 -08002170{
2171 // size
2172 const size_t len = this->readInt32();
2173 const size_t fd_count = this->readInt32();
2174
Christopher Tatee4e0ae82016-03-24 16:03:44 -07002175 if ((len > INT32_MAX) || (fd_count >= gMaxFds)) {
Nick Kralevichb6b14232015-04-02 09:36:02 -07002176 // don't accept size_t values which may have come from an
2177 // inadvertent conversion from a negative int.
2178 return BAD_VALUE;
2179 }
2180
Mathias Agopian98e71dd2010-02-11 17:30:52 -08002181 // payload
Nick Kralevichb6b14232015-04-02 09:36:02 -07002182 void const* const buf = this->readInplace(pad_size(len));
Yi Kong91635562018-06-07 14:38:36 -07002183 if (buf == nullptr)
Mathias Agopian98e71dd2010-02-11 17:30:52 -08002184 return BAD_VALUE;
2185
Yi Kong91635562018-06-07 14:38:36 -07002186 int* fds = nullptr;
Mathias Agopian98e71dd2010-02-11 17:30:52 -08002187 if (fd_count) {
Christopher Tatee4e0ae82016-03-24 16:03:44 -07002188 fds = new (std::nothrow) int[fd_count];
2189 if (fds == nullptr) {
2190 ALOGE("read: failed to allocate requested %zu fds", fd_count);
2191 return BAD_VALUE;
2192 }
Mathias Agopian98e71dd2010-02-11 17:30:52 -08002193 }
2194
2195 status_t err = NO_ERROR;
2196 for (size_t i=0 ; i<fd_count && err==NO_ERROR ; i++) {
Fabien Sanglardd84ff312016-10-21 10:58:26 -07002197 int fd = this->readFileDescriptor();
Nick Kralevichec9ec7d2016-12-17 19:47:27 -08002198 if (fd < 0 || ((fds[i] = fcntl(fd, F_DUPFD_CLOEXEC, 0)) < 0)) {
Jun Jiangabf8a2c2014-04-29 14:22:10 +08002199 err = BAD_VALUE;
Nick Kralevichec9ec7d2016-12-17 19:47:27 -08002200 ALOGE("fcntl(F_DUPFD_CLOEXEC) failed in Parcel::read, i is %zu, fds[i] is %d, fd_count is %zu, error: %s",
Fabien Sanglardd84ff312016-10-21 10:58:26 -07002201 i, fds[i], fd_count, strerror(fd < 0 ? -fd : errno));
2202 // Close all the file descriptors that were dup-ed.
2203 for (size_t j=0; j<i ;j++) {
2204 close(fds[j]);
2205 }
Jun Jiangabf8a2c2014-04-29 14:22:10 +08002206 }
Mathias Agopian98e71dd2010-02-11 17:30:52 -08002207 }
2208
2209 if (err == NO_ERROR) {
2210 err = val.unflatten(buf, len, fds, fd_count);
2211 }
2212
2213 if (fd_count) {
2214 delete [] fds;
2215 }
2216
2217 return err;
2218}
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002219const flat_binder_object* Parcel::readObject(bool nullMetaData) const
2220{
2221 const size_t DPOS = mDataPos;
2222 if ((DPOS+sizeof(flat_binder_object)) <= mDataSize) {
2223 const flat_binder_object* obj
2224 = reinterpret_cast<const flat_binder_object*>(mData+DPOS);
2225 mDataPos = DPOS + sizeof(flat_binder_object);
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002226 if (!nullMetaData && (obj->cookie == 0 && obj->binder == 0)) {
The Android Open Source Project5f78a482009-01-20 14:03:58 -08002227 // When transferring a NULL object, we don't write it into
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002228 // the object list, so we don't want to check for it when
2229 // reading.
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002230 ALOGV("readObject Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002231 return obj;
2232 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002233
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002234 // Ensure that this object is valid...
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002235 binder_size_t* const OBJS = mObjects;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002236 const size_t N = mObjectsSize;
2237 size_t opos = mNextObjectHint;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002238
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002239 if (N > 0) {
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002240 ALOGV("Parcel %p looking for obj at %zu, hint=%zu",
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002241 this, DPOS, opos);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002242
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002243 // Start at the current hint position, looking for an object at
2244 // the current data position.
2245 if (opos < N) {
2246 while (opos < (N-1) && OBJS[opos] < DPOS) {
2247 opos++;
2248 }
2249 } else {
2250 opos = N-1;
2251 }
2252 if (OBJS[opos] == DPOS) {
2253 // Found it!
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002254 ALOGV("Parcel %p found obj %zu at index %zu with forward search",
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002255 this, DPOS, opos);
2256 mNextObjectHint = opos+1;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002257 ALOGV("readObject Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002258 return obj;
2259 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002260
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002261 // Look backwards for it...
2262 while (opos > 0 && OBJS[opos] > DPOS) {
2263 opos--;
2264 }
2265 if (OBJS[opos] == DPOS) {
2266 // Found it!
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002267 ALOGV("Parcel %p found obj %zu at index %zu with backward search",
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002268 this, DPOS, opos);
2269 mNextObjectHint = opos+1;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002270 ALOGV("readObject Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002271 return obj;
2272 }
2273 }
Colin Cross6f4f3ab2014-02-05 17:42:44 -08002274 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 -07002275 this, DPOS);
2276 }
Yi Kong91635562018-06-07 14:38:36 -07002277 return nullptr;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002278}
2279
2280void Parcel::closeFileDescriptors()
2281{
2282 size_t i = mObjectsSize;
2283 if (i > 0) {
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002284 //ALOGI("Closing file descriptors for %zu objects...", i);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002285 }
2286 while (i > 0) {
2287 i--;
2288 const flat_binder_object* flat
2289 = reinterpret_cast<flat_binder_object*>(mData+mObjects[i]);
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -07002290 if (flat->hdr.type == BINDER_TYPE_FD) {
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002291 //ALOGI("Closing fd: %ld", flat->handle);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002292 close(flat->handle);
2293 }
2294 }
2295}
2296
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002297uintptr_t Parcel::ipcData() const
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002298{
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002299 return reinterpret_cast<uintptr_t>(mData);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002300}
2301
2302size_t Parcel::ipcDataSize() const
2303{
2304 return (mDataSize > mDataPos ? mDataSize : mDataPos);
2305}
2306
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002307uintptr_t Parcel::ipcObjects() const
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002308{
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002309 return reinterpret_cast<uintptr_t>(mObjects);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002310}
2311
2312size_t Parcel::ipcObjectsCount() const
2313{
2314 return mObjectsSize;
2315}
2316
2317void Parcel::ipcSetDataReference(const uint8_t* data, size_t dataSize,
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002318 const binder_size_t* objects, size_t objectsCount, release_func relFunc, void* relCookie)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002319{
Arve Hjønnevåg6f286112014-02-19 20:42:13 -08002320 binder_size_t minOffset = 0;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002321 freeDataNoInit();
2322 mError = NO_ERROR;
2323 mData = const_cast<uint8_t*>(data);
2324 mDataSize = mDataCapacity = dataSize;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002325 //ALOGI("setDataReference Setting data size of %p to %lu (pid=%d)", this, mDataSize, getpid());
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002326 mDataPos = 0;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002327 ALOGV("setDataReference Setting data pos of %p to %zu", this, mDataPos);
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002328 mObjects = const_cast<binder_size_t*>(objects);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002329 mObjectsSize = mObjectsCapacity = objectsCount;
2330 mNextObjectHint = 0;
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08002331 mObjectsSorted = false;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002332 mOwner = relFunc;
2333 mOwnerCookie = relCookie;
Arve Hjønnevågf50b9ea2014-02-13 19:22:08 -08002334 for (size_t i = 0; i < mObjectsSize; i++) {
Arve Hjønnevåg6f286112014-02-19 20:42:13 -08002335 binder_size_t offset = mObjects[i];
Arve Hjønnevågf50b9ea2014-02-13 19:22:08 -08002336 if (offset < minOffset) {
Dan Albert3bdc5b82014-11-20 11:50:23 -08002337 ALOGE("%s: bad object offset %" PRIu64 " < %" PRIu64 "\n",
Arve Hjønnevåg6f286112014-02-19 20:42:13 -08002338 __func__, (uint64_t)offset, (uint64_t)minOffset);
Arve Hjønnevågf50b9ea2014-02-13 19:22:08 -08002339 mObjectsSize = 0;
2340 break;
2341 }
2342 minOffset = offset + sizeof(flat_binder_object);
2343 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002344 scanForFds();
2345}
2346
Colin Cross6f4f3ab2014-02-05 17:42:44 -08002347void Parcel::print(TextOutput& to, uint32_t /*flags*/) const
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002348{
2349 to << "Parcel(";
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002350
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002351 if (errorCheck() != NO_ERROR) {
2352 const status_t err = errorCheck();
Colin Cross6f4f3ab2014-02-05 17:42:44 -08002353 to << "Error: " << (void*)(intptr_t)err << " \"" << strerror(-err) << "\"";
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002354 } else if (dataSize() > 0) {
2355 const uint8_t* DATA = data();
2356 to << indent << HexDump(DATA, dataSize()) << dedent;
Steven Moreland8bd01352019-07-15 16:36:14 -07002357 const binder_size_t* OBJS = mObjects;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002358 const size_t N = objectsCount();
2359 for (size_t i=0; i<N; i++) {
2360 const flat_binder_object* flat
2361 = reinterpret_cast<const flat_binder_object*>(DATA+OBJS[i]);
2362 to << endl << "Object #" << i << " @ " << (void*)OBJS[i] << ": "
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -07002363 << TypeCode(flat->hdr.type & 0x7f7f7f00)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002364 << " = " << flat->binder;
2365 }
2366 } else {
2367 to << "NULL";
2368 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002369
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002370 to << ")";
2371}
2372
2373void Parcel::releaseObjects()
2374{
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002375 size_t i = mObjectsSize;
Martijn Coenen69390d42018-10-22 15:18:10 +02002376 if (i == 0) {
2377 return;
2378 }
2379 sp<ProcessState> proc(ProcessState::self());
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002380 uint8_t* const data = mData;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002381 binder_size_t* const objects = mObjects;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002382 while (i > 0) {
2383 i--;
2384 const flat_binder_object* flat
2385 = reinterpret_cast<flat_binder_object*>(data+objects[i]);
Adrian Rooscbf37262015-10-22 16:12:53 -07002386 release_object(proc, *flat, this, &mOpenAshmemSize);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002387 }
2388}
2389
2390void Parcel::acquireObjects()
2391{
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002392 size_t i = mObjectsSize;
Martijn Coenen69390d42018-10-22 15:18:10 +02002393 if (i == 0) {
2394 return;
2395 }
2396 const sp<ProcessState> proc(ProcessState::self());
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002397 uint8_t* const data = mData;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002398 binder_size_t* const objects = mObjects;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002399 while (i > 0) {
2400 i--;
2401 const flat_binder_object* flat
2402 = reinterpret_cast<flat_binder_object*>(data+objects[i]);
Adrian Rooscbf37262015-10-22 16:12:53 -07002403 acquire_object(proc, *flat, this, &mOpenAshmemSize);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002404 }
2405}
2406
2407void Parcel::freeData()
2408{
2409 freeDataNoInit();
2410 initState();
2411}
2412
2413void Parcel::freeDataNoInit()
2414{
2415 if (mOwner) {
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002416 LOG_ALLOC("Parcel %p: freeing other owner data", this);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002417 //ALOGI("Freeing data ref of %p (pid=%d)", this, getpid());
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002418 mOwner(this, mData, mDataSize, mObjects, mObjectsSize, mOwnerCookie);
2419 } else {
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002420 LOG_ALLOC("Parcel %p: freeing allocated data", this);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002421 releaseObjects();
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002422 if (mData) {
2423 LOG_ALLOC("Parcel %p: freeing with %zu capacity", this, mDataCapacity);
Dianne Hackborna4cff882014-11-13 17:07:40 -08002424 pthread_mutex_lock(&gParcelGlobalAllocSizeLock);
Dan Austin48fd7b42015-09-10 13:46:02 -07002425 if (mDataCapacity <= gParcelGlobalAllocSize) {
2426 gParcelGlobalAllocSize = gParcelGlobalAllocSize - mDataCapacity;
2427 } else {
2428 gParcelGlobalAllocSize = 0;
2429 }
2430 if (gParcelGlobalAllocCount > 0) {
2431 gParcelGlobalAllocCount--;
2432 }
Dianne Hackborna4cff882014-11-13 17:07:40 -08002433 pthread_mutex_unlock(&gParcelGlobalAllocSizeLock);
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002434 free(mData);
2435 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002436 if (mObjects) free(mObjects);
2437 }
2438}
2439
2440status_t Parcel::growData(size_t len)
2441{
Nick Kralevichb6b14232015-04-02 09:36:02 -07002442 if (len > INT32_MAX) {
2443 // don't accept size_t values which may have come from an
2444 // inadvertent conversion from a negative int.
2445 return BAD_VALUE;
2446 }
2447
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002448 size_t newSize = ((mDataSize+len)*3)/2;
2449 return (newSize <= mDataSize)
2450 ? (status_t) NO_MEMORY
2451 : continueWrite(newSize);
2452}
2453
2454status_t Parcel::restartWrite(size_t desired)
2455{
Nick Kralevichb6b14232015-04-02 09:36:02 -07002456 if (desired > INT32_MAX) {
2457 // don't accept size_t values which may have come from an
2458 // inadvertent conversion from a negative int.
2459 return BAD_VALUE;
2460 }
2461
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002462 if (mOwner) {
2463 freeData();
2464 return continueWrite(desired);
2465 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002466
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002467 uint8_t* data = (uint8_t*)realloc(mData, desired);
2468 if (!data && desired > mDataCapacity) {
2469 mError = NO_MEMORY;
2470 return NO_MEMORY;
2471 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002472
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002473 releaseObjects();
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002474
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002475 if (data) {
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002476 LOG_ALLOC("Parcel %p: restart from %zu to %zu capacity", this, mDataCapacity, desired);
Dianne Hackborna4cff882014-11-13 17:07:40 -08002477 pthread_mutex_lock(&gParcelGlobalAllocSizeLock);
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002478 gParcelGlobalAllocSize += desired;
2479 gParcelGlobalAllocSize -= mDataCapacity;
Colin Cross83ec65e2015-12-08 17:15:50 -08002480 if (!mData) {
2481 gParcelGlobalAllocCount++;
2482 }
Dianne Hackborna4cff882014-11-13 17:07:40 -08002483 pthread_mutex_unlock(&gParcelGlobalAllocSizeLock);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002484 mData = data;
2485 mDataCapacity = desired;
2486 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002487
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002488 mDataSize = mDataPos = 0;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002489 ALOGV("restartWrite Setting data size of %p to %zu", this, mDataSize);
2490 ALOGV("restartWrite Setting data pos of %p to %zu", this, mDataPos);
2491
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002492 free(mObjects);
Yi Kong91635562018-06-07 14:38:36 -07002493 mObjects = nullptr;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002494 mObjectsSize = mObjectsCapacity = 0;
2495 mNextObjectHint = 0;
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08002496 mObjectsSorted = false;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002497 mHasFds = false;
2498 mFdsKnown = true;
Dianne Hackborn8938ed22011-09-28 23:19:47 -04002499 mAllowFds = true;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002500
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002501 return NO_ERROR;
2502}
2503
2504status_t Parcel::continueWrite(size_t desired)
2505{
Nick Kralevichb6b14232015-04-02 09:36:02 -07002506 if (desired > INT32_MAX) {
2507 // don't accept size_t values which may have come from an
2508 // inadvertent conversion from a negative int.
2509 return BAD_VALUE;
2510 }
2511
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002512 // If shrinking, first adjust for any objects that appear
2513 // after the new data size.
2514 size_t objectsSize = mObjectsSize;
2515 if (desired < mDataSize) {
2516 if (desired == 0) {
2517 objectsSize = 0;
2518 } else {
2519 while (objectsSize > 0) {
Michael Wachenschwanza6541632017-05-18 22:08:32 +00002520 if (mObjects[objectsSize-1] < desired)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002521 break;
2522 objectsSize--;
2523 }
2524 }
2525 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002526
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002527 if (mOwner) {
2528 // If the size is going to zero, just release the owner's data.
2529 if (desired == 0) {
2530 freeData();
2531 return NO_ERROR;
2532 }
2533
2534 // If there is a different owner, we need to take
2535 // posession.
2536 uint8_t* data = (uint8_t*)malloc(desired);
2537 if (!data) {
2538 mError = NO_MEMORY;
2539 return NO_MEMORY;
2540 }
Yi Kong91635562018-06-07 14:38:36 -07002541 binder_size_t* objects = nullptr;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002542
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002543 if (objectsSize) {
Nick Kraleviche9881a32015-04-28 16:21:30 -07002544 objects = (binder_size_t*)calloc(objectsSize, sizeof(binder_size_t));
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002545 if (!objects) {
Hyejin Kim3f727c02013-03-09 11:28:54 +09002546 free(data);
2547
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002548 mError = NO_MEMORY;
2549 return NO_MEMORY;
2550 }
2551
2552 // Little hack to only acquire references on objects
2553 // we will be keeping.
2554 size_t oldObjectsSize = mObjectsSize;
2555 mObjectsSize = objectsSize;
2556 acquireObjects();
2557 mObjectsSize = oldObjectsSize;
2558 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002559
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002560 if (mData) {
2561 memcpy(data, mData, mDataSize < desired ? mDataSize : desired);
2562 }
2563 if (objects && mObjects) {
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002564 memcpy(objects, mObjects, objectsSize*sizeof(binder_size_t));
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002565 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002566 //ALOGI("Freeing data ref of %p (pid=%d)", this, getpid());
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002567 mOwner(this, mData, mDataSize, mObjects, mObjectsSize, mOwnerCookie);
Yi Kong91635562018-06-07 14:38:36 -07002568 mOwner = nullptr;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002569
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002570 LOG_ALLOC("Parcel %p: taking ownership of %zu capacity", this, desired);
Dianne Hackborna4cff882014-11-13 17:07:40 -08002571 pthread_mutex_lock(&gParcelGlobalAllocSizeLock);
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002572 gParcelGlobalAllocSize += desired;
2573 gParcelGlobalAllocCount++;
Dianne Hackborna4cff882014-11-13 17:07:40 -08002574 pthread_mutex_unlock(&gParcelGlobalAllocSizeLock);
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002575
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002576 mData = data;
2577 mObjects = objects;
2578 mDataSize = (mDataSize < desired) ? mDataSize : desired;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002579 ALOGV("continueWrite Setting data size of %p to %zu", this, mDataSize);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002580 mDataCapacity = desired;
2581 mObjectsSize = mObjectsCapacity = objectsSize;
2582 mNextObjectHint = 0;
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08002583 mObjectsSorted = false;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002584
2585 } else if (mData) {
2586 if (objectsSize < mObjectsSize) {
2587 // Need to release refs on any objects we are dropping.
2588 const sp<ProcessState> proc(ProcessState::self());
2589 for (size_t i=objectsSize; i<mObjectsSize; i++) {
2590 const flat_binder_object* flat
2591 = reinterpret_cast<flat_binder_object*>(mData+mObjects[i]);
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -07002592 if (flat->hdr.type == BINDER_TYPE_FD) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002593 // will need to rescan because we may have lopped off the only FDs
2594 mFdsKnown = false;
2595 }
Adrian Rooscbf37262015-10-22 16:12:53 -07002596 release_object(proc, *flat, this, &mOpenAshmemSize);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002597 }
Michael Wachenschwanz6af27a82019-06-03 17:24:51 -07002598
2599 if (objectsSize == 0) {
2600 free(mObjects);
2601 mObjects = nullptr;
2602 } else {
2603 binder_size_t* objects =
2604 (binder_size_t*)realloc(mObjects, objectsSize*sizeof(binder_size_t));
2605 if (objects) {
2606 mObjects = objects;
2607 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002608 }
2609 mObjectsSize = objectsSize;
2610 mNextObjectHint = 0;
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08002611 mObjectsSorted = false;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002612 }
2613
2614 // We own the data, so we can just do a realloc().
2615 if (desired > mDataCapacity) {
2616 uint8_t* data = (uint8_t*)realloc(mData, desired);
2617 if (data) {
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002618 LOG_ALLOC("Parcel %p: continue from %zu to %zu capacity", this, mDataCapacity,
2619 desired);
Dianne Hackborna4cff882014-11-13 17:07:40 -08002620 pthread_mutex_lock(&gParcelGlobalAllocSizeLock);
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002621 gParcelGlobalAllocSize += desired;
2622 gParcelGlobalAllocSize -= mDataCapacity;
Dianne Hackborna4cff882014-11-13 17:07:40 -08002623 pthread_mutex_unlock(&gParcelGlobalAllocSizeLock);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002624 mData = data;
2625 mDataCapacity = desired;
Ganesh Mahendranade89892017-09-28 16:56:03 +08002626 } else {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002627 mError = NO_MEMORY;
2628 return NO_MEMORY;
2629 }
2630 } else {
Dianne Hackborn97e2bcd2011-04-13 18:15:56 -07002631 if (mDataSize > desired) {
2632 mDataSize = desired;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002633 ALOGV("continueWrite Setting data size of %p to %zu", this, mDataSize);
Dianne Hackborn97e2bcd2011-04-13 18:15:56 -07002634 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002635 if (mDataPos > desired) {
2636 mDataPos = desired;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002637 ALOGV("continueWrite Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002638 }
2639 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002640
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002641 } else {
2642 // This is the first data. Easy!
2643 uint8_t* data = (uint8_t*)malloc(desired);
2644 if (!data) {
2645 mError = NO_MEMORY;
2646 return NO_MEMORY;
2647 }
Hyejin Kim3f727c02013-03-09 11:28:54 +09002648
Yi Kong91635562018-06-07 14:38:36 -07002649 if(!(mDataCapacity == 0 && mObjects == nullptr
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002650 && mObjectsCapacity == 0)) {
Colin Cross6f4f3ab2014-02-05 17:42:44 -08002651 ALOGE("continueWrite: %zu/%p/%zu/%zu", mDataCapacity, mObjects, mObjectsCapacity, desired);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002652 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002653
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002654 LOG_ALLOC("Parcel %p: allocating with %zu capacity", this, desired);
Dianne Hackborna4cff882014-11-13 17:07:40 -08002655 pthread_mutex_lock(&gParcelGlobalAllocSizeLock);
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002656 gParcelGlobalAllocSize += desired;
2657 gParcelGlobalAllocCount++;
Dianne Hackborna4cff882014-11-13 17:07:40 -08002658 pthread_mutex_unlock(&gParcelGlobalAllocSizeLock);
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002659
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002660 mData = data;
2661 mDataSize = mDataPos = 0;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002662 ALOGV("continueWrite Setting data size of %p to %zu", this, mDataSize);
2663 ALOGV("continueWrite Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002664 mDataCapacity = desired;
2665 }
2666
2667 return NO_ERROR;
2668}
2669
2670void Parcel::initState()
2671{
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002672 LOG_ALLOC("Parcel %p: initState", this);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002673 mError = NO_ERROR;
Yi Kong91635562018-06-07 14:38:36 -07002674 mData = nullptr;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002675 mDataSize = 0;
2676 mDataCapacity = 0;
2677 mDataPos = 0;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002678 ALOGV("initState Setting data size of %p to %zu", this, mDataSize);
2679 ALOGV("initState Setting data pos of %p to %zu", this, mDataPos);
Yi Kong91635562018-06-07 14:38:36 -07002680 mObjects = nullptr;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002681 mObjectsSize = 0;
2682 mObjectsCapacity = 0;
2683 mNextObjectHint = 0;
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08002684 mObjectsSorted = false;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002685 mHasFds = false;
2686 mFdsKnown = true;
Dianne Hackborn8938ed22011-09-28 23:19:47 -04002687 mAllowFds = true;
Yi Kong91635562018-06-07 14:38:36 -07002688 mOwner = nullptr;
Adrian Rooscbf37262015-10-22 16:12:53 -07002689 mOpenAshmemSize = 0;
Olivier Gaillarddc848a02019-01-30 17:10:44 +00002690 mWorkSourceRequestHeaderPosition = 0;
2691 mRequestHeaderPresent = false;
Christopher Tatee4e0ae82016-03-24 16:03:44 -07002692
2693 // racing multiple init leads only to multiple identical write
2694 if (gMaxFds == 0) {
2695 struct rlimit result;
2696 if (!getrlimit(RLIMIT_NOFILE, &result)) {
2697 gMaxFds = (size_t)result.rlim_cur;
Christopher Tatebf14e942016-03-25 14:16:24 -07002698 //ALOGI("parcel fd limit set to %zu", gMaxFds);
Christopher Tatee4e0ae82016-03-24 16:03:44 -07002699 } else {
2700 ALOGW("Unable to getrlimit: %s", strerror(errno));
2701 gMaxFds = 1024;
2702 }
2703 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002704}
2705
2706void Parcel::scanForFds() const
2707{
2708 bool hasFds = false;
2709 for (size_t i=0; i<mObjectsSize; i++) {
2710 const flat_binder_object* flat
2711 = reinterpret_cast<const flat_binder_object*>(mData + mObjects[i]);
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -07002712 if (flat->hdr.type == BINDER_TYPE_FD) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002713 hasFds = true;
2714 break;
2715 }
2716 }
2717 mHasFds = hasFds;
2718 mFdsKnown = true;
2719}
2720
Dan Sandleraa5c2342015-04-10 10:08:45 -04002721size_t Parcel::getBlobAshmemSize() const
2722{
Adrian Roos6bb31142015-10-22 16:46:12 -07002723 // This used to return the size of all blobs that were written to ashmem, now we're returning
2724 // the ashmem currently referenced by this Parcel, which should be equivalent.
2725 // TODO: Remove method once ABI can be changed.
2726 return mOpenAshmemSize;
Dan Sandleraa5c2342015-04-10 10:08:45 -04002727}
2728
Adrian Rooscbf37262015-10-22 16:12:53 -07002729size_t Parcel::getOpenAshmemSize() const
2730{
2731 return mOpenAshmemSize;
Jeff Brown5707dbf2011-09-23 21:17:56 -07002732}
2733
2734// --- Parcel::Blob ---
2735
2736Parcel::Blob::Blob() :
Yi Kong91635562018-06-07 14:38:36 -07002737 mFd(-1), mData(nullptr), mSize(0), mMutable(false) {
Jeff Brown5707dbf2011-09-23 21:17:56 -07002738}
2739
2740Parcel::Blob::~Blob() {
2741 release();
2742}
2743
2744void Parcel::Blob::release() {
Jeff Brown13b16042014-11-11 16:44:25 -08002745 if (mFd != -1 && mData) {
Jeff Brown5707dbf2011-09-23 21:17:56 -07002746 ::munmap(mData, mSize);
2747 }
2748 clear();
2749}
2750
Jeff Brown13b16042014-11-11 16:44:25 -08002751void Parcel::Blob::init(int fd, void* data, size_t size, bool isMutable) {
2752 mFd = fd;
Jeff Brown5707dbf2011-09-23 21:17:56 -07002753 mData = data;
2754 mSize = size;
Jeff Brown13b16042014-11-11 16:44:25 -08002755 mMutable = isMutable;
Jeff Brown5707dbf2011-09-23 21:17:56 -07002756}
2757
2758void Parcel::Blob::clear() {
Jeff Brown13b16042014-11-11 16:44:25 -08002759 mFd = -1;
Yi Kong91635562018-06-07 14:38:36 -07002760 mData = nullptr;
Jeff Brown5707dbf2011-09-23 21:17:56 -07002761 mSize = 0;
Jeff Brown13b16042014-11-11 16:44:25 -08002762 mMutable = false;
Jeff Brown5707dbf2011-09-23 21:17:56 -07002763}
2764
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002765}; // namespace android